merge: tidy up isolation of conflicts.
[wiggle/upstream.git] / demo / md.c
blob3b8f0f892cfe46844bf2a9d6bdc02a72907cade5
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>
45 #include <linux/poll.h>
46 #include <linux/mutex.h>
47 #include <linux/ctype.h>
49 #include <linux/init.h>
51 #include <linux/file.h>
53 #ifdef CONFIG_KMOD
54 #include <linux/kmod.h>
55 #endif
57 #include <asm/unaligned.h>
59 #define MAJOR_NR MD_MAJOR
60 #define MD_DRIVER
62 /* 63 partitions with the alternate major number (mdp) */
63 #define MdpMinorShift 6
65 #define DEBUG 0
66 #define dprintk(x...) ((void)(DEBUG && printk(x)))
69 #ifndef MODULE
70 static void autostart_arrays (int part);
71 #endif
73 static LIST_HEAD(pers_list);
74 static DEFINE_SPINLOCK(pers_lock);
76 static void md_print_devices(void);
78 #define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
81 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
82 * is 1000 KB/sec, so the extra system load does not show up that much.
83 * Increase it if you want to have more _guaranteed_ speed. Note that
84 * the RAID driver will use the maximum available bandwidth if the IO
85 * subsystem is idle. There is also an 'absolute maximum' reconstruction
86 * speed limit - in case reconstruction slows down your system despite
87 * idle IO detection.
89 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
90 * or /sys/block/mdX/md/sync_speed_{min,max}
93 static int sysctl_speed_limit_min = 1000;
94 static int sysctl_speed_limit_max = 200000;
95 static inline int speed_min(mddev_t *mddev)
97 return mddev->sync_speed_min ?
98 mddev->sync_speed_min : sysctl_speed_limit_min;
101 static inline int speed_max(mddev_t *mddev)
103 return mddev->sync_speed_max ?
104 mddev->sync_speed_max : sysctl_speed_limit_max;
107 static struct ctl_table_header *raid_table_header;
109 static ctl_table raid_table[] = {
111 .ctl_name = DEV_RAID_SPEED_LIMIT_MIN,
112 .procname = "speed_limit_min",
113 .data = &sysctl_speed_limit_min,
114 .maxlen = sizeof(int),
115 .mode = S_IRUGO|S_IWUSR,
116 .proc_handler = &proc_dointvec,
119 .ctl_name = DEV_RAID_SPEED_LIMIT_MAX,
120 .procname = "speed_limit_max",
121 .data = &sysctl_speed_limit_max,
122 .maxlen = sizeof(int),
123 .mode = S_IRUGO|S_IWUSR,
124 .proc_handler = &proc_dointvec,
126 { .ctl_name = 0 }
129 static ctl_table raid_dir_table[] = {
131 .ctl_name = DEV_RAID,
132 .procname = "raid",
133 .maxlen = 0,
134 .mode = S_IRUGO|S_IWUGO,
135 .child = raid_table,
137 { .ctl_name = 0 }
140 static ctl_table raid_root_table[] = {
142 .ctl_name = CTL_DEV,
143 .procname = "dev",
144 .maxlen = 0,
145 .mode = 0555,
146 .child = raid_dir_table,
148 { .ctl_name = 0 }
151 static struct block_device_operations md_fops;
153 static int start_readonly;
156 * We have a system wide 'event count' that is incremented
157 * on any 'interesting' event, and readers of /proc/mdstat
158 * can use 'poll' or 'select' to find out when the event
159 * count increases.
161 * Events are:
162 * start array, stop array, error, add device, remove device,
163 * start build, activate spare
165 static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
166 static atomic_t md_event_count;
167 void md_new_event(mddev_t *mddev)
169 atomic_inc(&md_event_count);
170 wake_up(&md_event_waiters);
171 sysfs_notify(&mddev->kobj, NULL, "sync_action");
173 EXPORT_SYMBOL_GPL(md_new_event);
175 /* Alternate version that can be called from interrupts
176 * when calling sysfs_notify isn't needed.
178 void md_new_event_inintr(mddev_t *mddev)
180 atomic_inc(&md_event_count);
181 wake_up(&md_event_waiters);
185 * Enables to iterate over all existing md arrays
186 * all_mddevs_lock protects this list.
188 static LIST_HEAD(all_mddevs);
189 static DEFINE_SPINLOCK(all_mddevs_lock);
193 * iterates through all used mddevs in the system.
194 * We take care to grab the all_mddevs_lock whenever navigating
195 * the list, and to always hold a refcount when unlocked.
196 * Any code which breaks out of this loop while own
197 * a reference to the current mddev and must mddev_put it.
199 #define ITERATE_MDDEV(mddev,tmp) \
201 for (({ spin_lock(&all_mddevs_lock); \
202 tmp = all_mddevs.next; \
203 mddev = NULL;}); \
204 ({ if (tmp != &all_mddevs) \
205 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
206 spin_unlock(&all_mddevs_lock); \
207 if (mddev) mddev_put(mddev); \
208 mddev = list_entry(tmp, mddev_t, all_mddevs); \
209 tmp != &all_mddevs;}); \
210 ({ spin_lock(&all_mddevs_lock); \
211 tmp = tmp->next;}) \
215 static int md_fail_request (request_queue_t *q, struct bio *bio)
217 bio_io_error(bio, bio->bi_size);
218 return 0;
221 static inline mddev_t *mddev_get(mddev_t *mddev)
223 atomic_inc(&mddev->active);
224 return mddev;
227 static void mddev_put(mddev_t *mddev)
229 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
230 return;
231 if (!mddev->raid_disks && list_empty(&mddev->disks)) {
232 list_del(&mddev->all_mddevs);
233 spin_unlock(&all_mddevs_lock);
234 blk_cleanup_queue(mddev->queue);
235 kobject_unregister(&mddev->kobj);
236 } else
237 spin_unlock(&all_mddevs_lock);
240 static mddev_t * mddev_find(dev_t unit)
242 mddev_t *mddev, *new = NULL;
244 retry:
245 spin_lock(&all_mddevs_lock);
246 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
247 if (mddev->unit == unit) {
248 mddev_get(mddev);
249 spin_unlock(&all_mddevs_lock);
250 kfree(new);
251 return mddev;
254 if (new) {
255 list_add(&new->all_mddevs, &all_mddevs);
256 spin_unlock(&all_mddevs_lock);
257 return new;
259 spin_unlock(&all_mddevs_lock);
261 new = kzalloc(sizeof(*new), GFP_KERNEL);
262 if (!new)
263 return NULL;
265 new->unit = unit;
266 if (MAJOR(unit) == MD_MAJOR)
267 new->md_minor = MINOR(unit);
268 else
269 new->md_minor = MINOR(unit) >> MdpMinorShift;
271 mutex_init(&new->reconfig_mutex);
272 INIT_LIST_HEAD(&new->disks);
273 INIT_LIST_HEAD(&new->all_mddevs);
274 init_timer(&new->safemode_timer);
275 atomic_set(&new->active, 1);
276 spin_lock_init(&new->write_lock);
277 init_waitqueue_head(&new->sb_wait);
278 new->resync_max = MaxSector;
280 new->queue = blk_alloc_queue(GFP_KERNEL);
281 if (!new->queue) {
282 kfree(new);
283 return NULL;
285 set_bit(QUEUE_FLAG_CLUSTER, &new->queue->queue_flags);
287 blk_queue_make_request(new->queue, md_fail_request);
289 goto retry;
292 static inline int mddev_lock(mddev_t * mddev)
294 return mutex_lock_interruptible(&mddev->reconfig_mutex);
297 static inline int mddev_trylock(mddev_t * mddev)
299 return mutex_trylock(&mddev->reconfig_mutex);
302 static inline void mddev_unlock(mddev_t * mddev)
304 mutex_unlock(&mddev->reconfig_mutex);
306 md_wakeup_thread(mddev->thread);
309 static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
311 mdk_rdev_t * rdev;
312 struct list_head *tmp;
314 ITERATE_RDEV(mddev,rdev,tmp) {
315 if (rdev->desc_nr == nr)
316 return rdev;
318 return NULL;
321 static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
323 struct list_head *tmp;
324 mdk_rdev_t *rdev;
326 ITERATE_RDEV(mddev,rdev,tmp) {
327 if (rdev->bdev->bd_dev == dev)
328 return rdev;
330 return NULL;
333 static struct mdk_personality *find_pers(int level, char *clevel)
335 struct mdk_personality *pers;
336 list_for_each_entry(pers, &pers_list, list) {
337 if (level != LEVEL_NONE && pers->level == level)
338 return pers;
339 if (strcmp(pers->name, clevel)==0)
340 return pers;
342 return NULL;
345 static inline sector_t calc_dev_sboffset(struct block_device *bdev)
347 sector_t size = bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
348 return MD_NEW_SIZE_BLOCKS(size);
351 static sector_t calc_dev_size(mdk_rdev_t *rdev, unsigned chunk_size)
353 sector_t size;
355 size = rdev->sb_offset;
357 if (chunk_size)
358 size &= ~((sector_t)chunk_size/1024 - 1);
359 return size;
362 static int alloc_disk_sb(mdk_rdev_t * rdev)
364 if (rdev->sb_page)
365 MD_BUG();
367 rdev->sb_page = alloc_page(GFP_KERNEL);
368 if (!rdev->sb_page) {
369 printk(KERN_ALERT "md: out of memory.\n");
370 return -EINVAL;
373 return 0;
376 static void free_disk_sb(mdk_rdev_t * rdev)
378 if (rdev->sb_page) {
379 put_page(rdev->sb_page);
380 rdev->sb_loaded = 0;
381 rdev->sb_page = NULL;
382 rdev->sb_offset = 0;
383 rdev->size = 0;
388 static int super_written(struct bio *bio, unsigned int bytes_done, int error)
390 mdk_rdev_t *rdev = bio->bi_private;
391 mddev_t *mddev = rdev->mddev;
392 if (bio->bi_size)
393 return 1;
395 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags))
396 md_error(mddev, rdev);
398 if (atomic_dec_and_test(&mddev->pending_writes))
399 wake_up(&mddev->sb_wait);
400 bio_put(bio);
401 return 0;
404 static int super_written_barrier(struct bio *bio, unsigned int bytes_done, int error)
406 struct bio *bio2 = bio->bi_private;
407 mdk_rdev_t *rdev = bio2->bi_private;
408 mddev_t *mddev = rdev->mddev;
409 if (bio->bi_size)
410 return 1;
412 if (!test_bit(BIO_UPTODATE, &bio->bi_flags) &&
413 error == -EOPNOTSUPP) {
414 unsigned long flags;
415 /* barriers don't appear to be supported :-( */
416 set_bit(BarriersNotsupp, &rdev->flags);
417 mddev->barriers_work = 0;
418 spin_lock_irqsave(&mddev->write_lock, flags);
419 bio2->bi_next = mddev->biolist;
420 mddev->biolist = bio2;
421 spin_unlock_irqrestore(&mddev->write_lock, flags);
422 wake_up(&mddev->sb_wait);
423 bio_put(bio);
424 return 0;
426 bio_put(bio2);
427 bio->bi_private = rdev;
428 return super_written(bio, bytes_done, error);
431 void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
432 sector_t sector, int size, struct page *page)
434 /* write first size bytes of page to sector of rdev
435 * Increment mddev->pending_writes before returning
436 * and decrement it on completion, waking up sb_wait
437 * if zero is reached.
438 * If an error occurred, call md_error
440 * As we might need to resubmit the request if BIO_RW_BARRIER
441 * causes ENOTSUPP, we allocate a spare bio...
443 struct bio *bio = bio_alloc(GFP_NOIO, 1);
444 int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNC);
446 bio->bi_bdev = rdev->bdev;
447 bio->bi_sector = sector;
448 bio_add_page(bio, page, size, 0);
449 bio->bi_private = rdev;
450 bio->bi_end_io = super_written;
451 bio->bi_rw = rw;
453 atomic_inc(&mddev->pending_writes);
454 if (!test_bit(BarriersNotsupp, &rdev->flags)) {
455 struct bio *rbio;
456 rw |= (1<<BIO_RW_BARRIER);
457 rbio = bio_clone(bio, GFP_NOIO);
458 rbio->bi_private = bio;
459 rbio->bi_end_io = super_written_barrier;
460 submit_bio(rw, rbio);
461 } else
462 submit_bio(rw, bio);
465 void md_super_wait(mddev_t *mddev)
467 /* wait for all superblock writes that were scheduled to complete.
468 * if any had to be retried (due to BARRIER problems), retry them
470 DEFINE_WAIT(wq);
471 for(;;) {
472 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
473 if (atomic_read(&mddev->pending_writes)==0)
474 break;
475 while (mddev->biolist) {
476 struct bio *bio;
477 spin_lock_irq(&mddev->write_lock);
478 bio = mddev->biolist;
479 mddev->biolist = bio->bi_next ;
480 bio->bi_next = NULL;
481 spin_unlock_irq(&mddev->write_lock);
482 submit_bio(bio->bi_rw, bio);
484 schedule();
486 finish_wait(&mddev->sb_wait, &wq);
489 static int bi_complete(struct bio *bio, unsigned int bytes_done, int error)
491 if (bio->bi_size)
492 return 1;
494 complete((struct completion*)bio->bi_private);
495 return 0;
498 int sync_page_io(struct block_device *bdev, sector_t sector, int size,
499 struct page *page, int rw)
501 struct bio *bio = bio_alloc(GFP_NOIO, 1);
502 struct completion event;
503 int ret;
505 rw |= (1 << BIO_RW_SYNC);
507 bio->bi_bdev = bdev;
508 bio->bi_sector = sector;
509 bio_add_page(bio, page, size, 0);
510 init_completion(&event);
511 bio->bi_private = &event;
512 bio->bi_end_io = bi_complete;
513 submit_bio(rw, bio);
514 wait_for_completion(&event);
516 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
517 bio_put(bio);
518 return ret;
520 EXPORT_SYMBOL_GPL(sync_page_io);
522 static int read_disk_sb(mdk_rdev_t * rdev, int size)
524 char b[BDEVNAME_SIZE];
525 if (!rdev->sb_page) {
526 MD_BUG();
527 return -EINVAL;
529 if (rdev->sb_loaded)
530 return 0;
533 if (!sync_page_io(rdev->bdev, rdev->sb_offset<<1, size, rdev->sb_page, READ))
534 goto fail;
535 rdev->sb_loaded = 1;
536 return 0;
538 fail:
539 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
540 bdevname(rdev->bdev,b));
541 return -EINVAL;
544 static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
546 if ( (sb1->set_uuid0 == sb2->set_uuid0) &&
547 (sb1->set_uuid1 == sb2->set_uuid1) &&
548 (sb1->set_uuid2 == sb2->set_uuid2) &&
549 (sb1->set_uuid3 == sb2->set_uuid3))
551 return 1;
553 return 0;
557 static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
559 int ret;
560 mdp_super_t *tmp1, *tmp2;
562 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
563 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
565 if (!tmp1 || !tmp2) {
566 ret = 0;
567 printk(KERN_INFO "md.c: sb1 is not equal to sb2!\n");
568 goto abort;
571 *tmp1 = *sb1;
572 *tmp2 = *sb2;
575 * nr_disks is not constant
577 tmp1->nr_disks = 0;
578 tmp2->nr_disks = 0;
580 if (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4))
581 ret = 0;
582 else
583 ret = 1;
585 abort:
586 kfree(tmp1);
587 kfree(tmp2);
588 return ret;
591 static unsigned int calc_sb_csum(mdp_super_t * sb)
593 unsigned int disk_csum, csum;
595 disk_csum = sb->sb_csum;
596 sb->sb_csum = 0;
597 csum = csum_partial((void *)sb, MD_SB_BYTES, 0);
598 sb->sb_csum = disk_csum;
599 return csum;
604 * Handle superblock details.
605 * We want to be able to handle multiple superblock formats
606 * so we have a common interface to them all, and an array of
607 * different handlers.
608 * We rely on user-space to write the initial superblock, and support
609 * reading and updating of superblocks.
610 * Interface methods are:
611 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
612 * loads and validates a superblock on dev.
613 * if refdev != NULL, compare superblocks on both devices
614 * Return:
615 * 0 - dev has a superblock that is compatible with refdev
616 * 1 - dev has a superblock that is compatible and newer than refdev
617 * so dev should be used as the refdev in future
618 * -EINVAL superblock incompatible or invalid
619 * -othererror e.g. -EIO
621 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
622 * Verify that dev is acceptable into mddev.
623 * The first time, mddev->raid_disks will be 0, and data from
624 * dev should be merged in. Subsequent calls check that dev
625 * is new enough. Return 0 or -EINVAL
627 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
628 * Update the superblock for rdev with data in mddev
629 * This does not write to disc.
633 struct super_type {
634 char *name;
635 struct module *owner;
636 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version);
637 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
638 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
642 * load_super for 0.90.0
644 static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
646 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
647 mdp_super_t *sb;
648 int ret;
649 sector_t sb_offset;
652 * Calculate the position of the superblock,
653 * it's at the end of the disk.
655 * It also happens to be a multiple of 4Kb.
657 sb_offset = calc_dev_sboffset(rdev->bdev);
658 rdev->sb_offset = sb_offset;
660 ret = read_disk_sb(rdev, MD_SB_BYTES);
661 if (ret) return ret;
663 ret = -EINVAL;
665 bdevname(rdev->bdev, b);
666 sb = (mdp_super_t*)page_address(rdev->sb_page);
668 if (sb->md_magic != MD_SB_MAGIC) {
669 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
671 goto abort;
674 if (sb->major_version != 0 ||
675 sb->minor_version < 90 ||
676 sb->minor_version > 91) {
677 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
678 sb->major_version, sb->minor_version,
680 goto abort;
683 if (sb->raid_disks <= 0)
684 goto abort;
686 if (csum_fold(calc_sb_csum(sb)) != csum_fold(sb->sb_csum)) {
687 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
689 goto abort;
692 rdev->preferred_minor = sb->md_minor;
693 rdev->data_offset = 0;
694 rdev->sb_size = MD_SB_BYTES;
696 if (sb->level == LEVEL_MULTIPATH)
697 rdev->desc_nr = -1;
698 else
699 rdev->desc_nr = sb->this_disk.number;
701 if (refdev == 0)
702 ret = 1;
703 else {
704 __u64 ev1, ev2;
705 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
706 if (!uuid_equal(refsb, sb)) {
707 printk(KERN_WARNING "md: %s has different UUID to %s\n",
708 b, bdevname(refdev->bdev,b2));
709 goto abort;
711 if (!sb_equal(refsb, sb)) {
712 printk(KERN_WARNING "md: %s has same UUID"
713 " but different superblock to %s\n",
714 b, bdevname(refdev->bdev, b2));
715 goto abort;
717 ev1 = md_event(sb);
718 ev2 = md_event(refsb);
719 if (ev1 > ev2)
720 ret = 1;
721 else
722 ret = 0;
724 rdev->size = calc_dev_size(rdev, sb->chunk_size);
726 if (rdev->size < sb->size && sb->level > 1)
727 /* "this cannot possibly happen" ... */
728 ret = -EINVAL;
730 abort:
731 return ret;
735 * validate_super for 0.90.0
737 static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
739 mdp_disk_t *desc;
740 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
741 __u64 ev1 = md_event(sb);
743 rdev->raid_disk = -1;
744 rdev->flags = 0;
745 if (mddev->raid_disks == 0) {
746 mddev->major_version = 0;
747 mddev->minor_version = sb->minor_version;
748 mddev->patch_version = sb->patch_version;
749 mddev->persistent = 1;
750 mddev->external = 0;
751 mddev->chunk_size = sb->chunk_size;
752 mddev->ctime = sb->ctime;
753 mddev->utime = sb->utime;
754 mddev->level = sb->level;
755 mddev->clevel[0] = 0;
756 mddev->layout = sb->layout;
757 mddev->raid_disks = sb->raid_disks;
758 mddev->size = sb->size;
759 mddev->events = ev1;
760 mddev->bitmap_offset = 0;
761 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
763 if (mddev->minor_version >= 91) {
764 mddev->reshape_position = sb->reshape_position;
765 mddev->delta_disks = sb->delta_disks;
766 mddev->new_level = sb->new_level;
767 mddev->new_layout = sb->new_layout;
768 mddev->new_chunk = sb->new_chunk;
769 } else {
770 mddev->reshape_position = MaxSector;
771 mddev->delta_disks = 0;
772 mddev->new_level = mddev->level;
773 mddev->new_layout = mddev->layout;
774 mddev->new_chunk = mddev->chunk_size;
777 if (sb->state & (1<<MD_SB_CLEAN))
778 mddev->recovery_cp = MaxSector;
779 else {
780 if (sb->events_hi == sb->cp_events_hi &&
781 sb->events_lo == sb->cp_events_lo) {
782 mddev->recovery_cp = sb->recovery_cp;
783 } else
784 mddev->recovery_cp = 0;
787 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
788 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
789 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
790 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
792 mddev->max_disks = MD_SB_DISKS;
794 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
795 mddev->bitmap_file == NULL) {
796 if (mddev->level != 1 && mddev->level != 4
797 && mddev->level != 5 && mddev->level != 6
798 && mddev->level != 10) {
799 /* FIXME use a better test */
800 printk(KERN_WARNING "md: bitmaps not supported for this level.\n");
801 return -EINVAL;
803 mddev->bitmap_offset = mddev->default_bitmap_offset;
806 } else if (mddev->pers == NULL) {
807 /* Insist on good event counter while assembling */
808 ++ev1;
809 if (ev1 < mddev->events)
810 return -EINVAL;
811 } else if (mddev->bitmap) {
812 /* if adding to array with a bitmap, then we can accept an
813 * older device ... but not too old.
815 if (ev1 < mddev->bitmap->events_cleared)
816 return 0;
817 } else {
818 if (ev1 < mddev->events)
819 /* just a hot-add of a new device, leave raid_disk at -1 */
820 return 0;
823 if (mddev->level != LEVEL_MULTIPATH) {
824 desc = sb->disks + rdev->desc_nr;
826 if (desc->state & (1<<MD_DISK_FAULTY))
827 set_bit(Faulty, &rdev->flags);
828 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
829 desc->raid_disk < mddev->raid_disks */) {
830 set_bit(In_sync, &rdev->flags);
831 rdev->raid_disk = desc->raid_disk;
833 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
834 set_bit(WriteMostly, &rdev->flags);
835 } else /* MULTIPATH are always insync */
836 set_bit(In_sync, &rdev->flags);
837 return 0;
841 * sync_super for 0.90.0
843 static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
845 mdp_super_t *sb;
846 struct list_head *tmp;
847 mdk_rdev_t *rdev2;
848 int next_spare = mddev->raid_disks;
851 /* make rdev->sb match mddev data..
853 * 1/ zero out disks
854 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
855 * 3/ any empty disks < next_spare become removed
857 * disks[0] gets initialised to REMOVED because
858 * we cannot be sure from other fields if it has
859 * been initialised or not.
861 int i;
862 int active=0, working=0,failed=0,spare=0,nr_disks=0;
864 rdev->sb_size = MD_SB_BYTES;
866 sb = (mdp_super_t*)page_address(rdev->sb_page);
868 memset(sb, 0, sizeof(*sb));
870 sb->md_magic = MD_SB_MAGIC;
871 sb->major_version = mddev->major_version;
872 sb->patch_version = mddev->patch_version;
873 sb->gvalid_words = 0; /* ignored */
874 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
875 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
876 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
877 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
879 sb->ctime = mddev->ctime;
880 sb->level = mddev->level;
881 sb->size = mddev->size;
882 sb->raid_disks = mddev->raid_disks;
883 sb->md_minor = mddev->md_minor;
884 sb->not_persistent = 0;
885 sb->utime = mddev->utime;
886 sb->state = 0;
887 sb->events_hi = (mddev->events>>32);
888 sb->events_lo = (u32)mddev->events;
890 if (mddev->reshape_position == MaxSector)
891 sb->minor_version = 90;
892 else {
893 sb->minor_version = 91;
894 sb->reshape_position = mddev->reshape_position;
895 sb->new_level = mddev->new_level;
896 sb->delta_disks = mddev->delta_disks;
897 sb->new_layout = mddev->new_layout;
898 sb->new_chunk = mddev->new_chunk;
900 mddev->minor_version = sb->minor_version;
901 if (mddev->in_sync)
903 sb->recovery_cp = mddev->recovery_cp;
904 sb->cp_events_hi = (mddev->events>>32);
905 sb->cp_events_lo = (u32)mddev->events;
906 if (mddev->recovery_cp == MaxSector)
907 sb->state = (1<< MD_SB_CLEAN);
908 } else
909 sb->recovery_cp = 0;
911 sb->layout = mddev->layout;
912 sb->chunk_size = mddev->chunk_size;
914 if (mddev->bitmap && mddev->bitmap_file == NULL)
915 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
917 sb->disks[0].state = (1<<MD_DISK_REMOVED);
918 ITERATE_RDEV(mddev,rdev2,tmp) {
919 mdp_disk_t *d;
920 int desc_nr;
921 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
922 && !test_bit(Faulty, &rdev2->flags))
923 desc_nr = rdev2->raid_disk;
924 else
925 desc_nr = next_spare++;
926 rdev2->desc_nr = desc_nr;
927 d = &sb->disks[rdev2->desc_nr];
928 nr_disks++;
929 d->number = rdev2->desc_nr;
930 d->major = MAJOR(rdev2->bdev->bd_dev);
931 d->minor = MINOR(rdev2->bdev->bd_dev);
932 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
933 && !test_bit(Faulty, &rdev2->flags))
934 d->raid_disk = rdev2->raid_disk;
935 else
936 d->raid_disk = rdev2->desc_nr; /* compatibility */
937 if (test_bit(Faulty, &rdev2->flags))
938 d->state = (1<<MD_DISK_FAULTY);
939 else if (test_bit(In_sync, &rdev2->flags)) {
940 d->state = (1<<MD_DISK_ACTIVE);
941 d->state |= (1<<MD_DISK_SYNC);
942 active++;
943 working++;
944 } else {
945 d->state = 0;
946 spare++;
947 working++;
949 if (test_bit(WriteMostly, &rdev2->flags))
950 d->state |= (1<<MD_DISK_WRITEMOSTLY);
952 /* now set the "removed" and "faulty" bits on any missing devices */
953 for (i=0 ; i < mddev->raid_disks ; i++) {
954 mdp_disk_t *d = &sb->disks[i];
955 if (d->state == 0 && d->number == 0) {
956 d->number = i;
957 d->raid_disk = i;
958 d->state = (1<<MD_DISK_REMOVED);
959 d->state |= (1<<MD_DISK_FAULTY);
960 failed++;
963 sb->nr_disks = nr_disks;
964 sb->active_disks = active;
965 sb->working_disks = working;
966 sb->failed_disks = failed;
967 sb->spare_disks = spare;
969 sb->this_disk = sb->disks[rdev->desc_nr];
970 sb->sb_csum = calc_sb_csum(sb);
974 * version 1 superblock
977 static unsigned int calc_sb_1_csum(struct mdp_superblock_1 * sb)
979 unsigned int disk_csum, csum;
980 unsigned long long newcsum;
981 int size = 256 + le32_to_cpu(sb->max_dev)*2;
982 unsigned int *isuper = (unsigned int*)sb;
983 int i;
985 disk_csum = sb->sb_csum;
986 sb->sb_csum = 0;
987 newcsum = 0;
988 for (i=0; size>=4; size -= 4 )
989 newcsum += le32_to_cpu(*isuper++);
991 if (size == 2)
992 newcsum += le16_to_cpu(*(unsigned short*) isuper);
994 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
995 sb->sb_csum = disk_csum;
996 return cpu_to_le32(csum);
999 static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
1001 struct mdp_superblock_1 *sb;
1002 int ret;
1003 sector_t sb_offset;
1004 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1005 int bmask;
1008 * Calculate the position of the superblock.
1009 * It is always aligned to a 4K boundary and
1010 * depeding on minor_version, it can be:
1011 * 0: At least 8K, but less than 12K, from end of device
1012 * 1: At start of device
1013 * 2: 4K from start of device.
1015 switch(minor_version) {
1016 case 0:
1017 sb_offset = rdev->bdev->bd_inode->i_size >> 9;
1018 sb_offset -= 8*2;
1019 sb_offset &= ~(sector_t)(4*2-1);
1020 /* convert from sectors to K */
1021 sb_offset /= 2;
1022 break;
1023 case 1:
1024 sb_offset = 0;
1025 break;
1026 case 2:
1027 sb_offset = 4;
1028 break;
1029 default:
1030 return -EINVAL;
1032 rdev->sb_offset = sb_offset;
1034 /* superblock is rarely larger than 1K, but it can be larger,
1035 * and it is safe to read 4k, so we do that
1037 ret = read_disk_sb(rdev, 4096);
1038 if (ret) return ret;
1041 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1043 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1044 sb->major_version != cpu_to_le32(1) ||
1045 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
1046 le64_to_cpu(sb->super_offset) != (rdev->sb_offset<<1) ||
1047 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
1048 return -EINVAL;
1050 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1051 printk("md: invalid superblock checksum on %s\n",
1052 bdevname(rdev->bdev,b));
1053 return -EINVAL;
1055 if (le64_to_cpu(sb->data_size) < 10) {
1056 printk("md: data_size too small on %s\n",
1057 bdevname(rdev->bdev,b));
1058 return -EINVAL;
1060 rdev->preferred_minor = 0xffff;
1061 rdev->data_offset = le64_to_cpu(sb->data_offset);
1062 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
1064 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
1065 bmask = queue_hardsect_size(rdev->bdev->bd_disk->queue)-1;
1066 if (rdev->sb_size & bmask)
1067 rdev-> sb_size = (rdev->sb_size | bmask)+1;
1069 if (refdev == 0)
1070 ret = 1;
1071 else {
1072 __u64 ev1, ev2;
1073 struct mdp_superblock_1 *refsb =
1074 (struct mdp_superblock_1*)page_address(refdev->sb_page);
1076 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1077 sb->level != refsb->level ||
1078 sb->layout != refsb->layout ||
1079 sb->chunksize != refsb->chunksize) {
1080 printk(KERN_WARNING "md: %s has strangely different"
1081 " superblock to %s\n",
1082 bdevname(rdev->bdev,b),
1083 bdevname(refdev->bdev,b2));
1084 return -EINVAL;
1086 ev1 = le64_to_cpu(sb->events);
1087 ev2 = le64_to_cpu(refsb->events);
1089 if (ev1 > ev2)
1090 ret = 1;
1091 else
1092 ret = 0;
1094 if (minor_version)
1095 rdev->size = ((rdev->bdev->bd_inode->i_size>>9) - le64_to_cpu(sb->data_offset)) / 2;
1096 else
1097 rdev->size = rdev->sb_offset;
1098 if (rdev->size < le64_to_cpu(sb->data_size)/2)
1099 return -EINVAL;
1100 rdev->size = le64_to_cpu(sb->data_size)/2;
1101 if (le32_to_cpu(sb->chunksize))
1102 rdev->size &= ~((sector_t)le32_to_cpu(sb->chunksize)/2 - 1);
1104 if (le32_to_cpu(sb->size) > rdev->size*2)
1105 return -EINVAL;
1106 return ret;
1109 static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1111 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1112 __u64 ev1 = le64_to_cpu(sb->events);
1114 rdev->raid_disk = -1;
1115 rdev->flags = 0;
1116 if (mddev->raid_disks == 0) {
1117 mddev->major_version = 1;
1118 mddev->patch_version = 0;
1119 mddev->persistent = 1;
1120 mddev->external = 0;
1121 mddev->chunk_size = le32_to_cpu(sb->chunksize) << 9;
1122 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1123 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1124 mddev->level = le32_to_cpu(sb->level);
1125 mddev->clevel[0] = 0;
1126 mddev->layout = le32_to_cpu(sb->layout);
1127 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
1128 mddev->size = le64_to_cpu(sb->size)/2;
1129 mddev->events = ev1;
1130 mddev->bitmap_offset = 0;
1131 mddev->default_bitmap_offset = 1024 >> 9;
1133 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1134 memcpy(mddev->uuid, sb->set_uuid, 16);
1136 mddev->max_disks = (4096-256)/2;
1138 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
1139 mddev->bitmap_file == NULL ) {
1140 if (mddev->level != 1 && mddev->level != 5 && mddev->level != 6
1141 && mddev->level != 10) {
1142 printk(KERN_WARNING "md: bitmaps not supported for this level.\n");
1143 return -EINVAL;
1145 mddev->bitmap_offset = (__s32)le32_to_cpu(sb->bitmap_offset);
1147 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1148 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1149 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1150 mddev->new_level = le32_to_cpu(sb->new_level);
1151 mddev->new_layout = le32_to_cpu(sb->new_layout);
1152 mddev->new_chunk = le32_to_cpu(sb->new_chunk)<<9;
1153 } else {
1154 mddev->reshape_position = MaxSector;
1155 mddev->delta_disks = 0;
1156 mddev->new_level = mddev->level;
1157 mddev->new_layout = mddev->layout;
1158 mddev->new_chunk = mddev->chunk_size;
1161 } else if (mddev->pers == NULL) {
1162 /* Insist of good event counter while assembling */
1163 ++ev1;
1164 if (ev1 < mddev->events)
1165 return -EINVAL;
1166 } else if (mddev->bitmap) {
1167 /* If adding to array with a bitmap, then we can accept an
1168 * older device, but not too old.
1170 if (ev1 < mddev->bitmap->events_cleared)
1171 return 0;
1172 } else {
1173 if (ev1 < mddev->events)
1174 /* just a hot-add of a new device, leave raid_disk at -1 */
1175 return 0;
1177 if (mddev->level != LEVEL_MULTIPATH) {
1178 int role;
1179 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1180 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
1181 switch(role) {
1182 case 0xffff: /* spare */
1183 break;
1184 case 0xfffe: /* faulty */
1185 set_bit(Faulty, &rdev->flags);
1186 break;
1187 default:
1188 if ((le32_to_cpu(sb->feature_map) &
1189 MD_FEATURE_RECOVERY_OFFSET))
1190 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1191 else
1192 set_bit(In_sync, &rdev->flags);
1193 rdev->raid_disk = role;
1194 break;
1196 if (sb->devflags & WriteMostly1)
1197 set_bit(WriteMostly, &rdev->flags);
1198 } else /* MULTIPATH are always insync */
1199 set_bit(In_sync, &rdev->flags);
1201 return 0;
1204 static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1206 struct mdp_superblock_1 *sb;
1207 struct list_head *tmp;
1208 mdk_rdev_t *rdev2;
1209 int max_dev, i;
1210 /* make rdev->sb match mddev and rdev data. */
1212 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1214 sb->feature_map = 0;
1215 sb->pad0 = 0;
1216 sb->recovery_offset = cpu_to_le64(0);
1217 memset(sb->pad1, 0, sizeof(sb->pad1));
1218 memset(sb->pad2, 0, sizeof(sb->pad2));
1219 memset(sb->pad3, 0, sizeof(sb->pad3));
1221 sb->utime = cpu_to_le64((__u64)mddev->utime);
1222 sb->events = cpu_to_le64(mddev->events);
1223 if (mddev->in_sync)
1224 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1225 else
1226 sb->resync_offset = cpu_to_le64(0);
1228 sb->cnt_corrected_read = atomic_read(&rdev->corrected_errors);
1230 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
1231 sb->size = cpu_to_le64(mddev->size<<1);
1233 if (mddev->bitmap && mddev->bitmap_file == NULL) {
1234 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_offset);
1235 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
1238 if (rdev->raid_disk >= 0 &&
1239 !test_bit(In_sync, &rdev->flags) &&
1240 rdev->recovery_offset > 0) {
1241 sb->feature_map |= cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1242 sb->recovery_offset = cpu_to_le64(rdev->recovery_offset);
1245 if (mddev->reshape_position != MaxSector) {
1246 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1247 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1248 sb->new_layout = cpu_to_le32(mddev->new_layout);
1249 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1250 sb->new_level = cpu_to_le32(mddev->new_level);
1251 sb->new_chunk = cpu_to_le32(mddev->new_chunk>>9);
1254 max_dev = 0;
1255 ITERATE_RDEV(mddev,rdev2,tmp)
1256 if (rdev2->desc_nr+1 > max_dev)
1257 max_dev = rdev2->desc_nr+1;
1259 sb->max_dev = cpu_to_le32(max_dev);
1260 for (i=0; i<max_dev;i++)
1261 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1263 ITERATE_RDEV(mddev,rdev2,tmp) {
1264 i = rdev2->desc_nr;
1265 if (test_bit(Faulty, &rdev2->flags))
1266 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1267 else if (test_bit(In_sync, &rdev2->flags))
1268 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
1269 else if (rdev2->raid_disk >= 0 && rdev2->recovery_offset > 0)
1270 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
1271 else
1272 sb->dev_roles[i] = cpu_to_le16(0xffff);
1275 sb->sb_csum = calc_sb_1_csum(sb);
1279 static struct super_type super_types[] = {
1280 [0] = {
1281 .name = "0.90.0",
1282 .owner = THIS_MODULE,
1283 .load_super = super_90_load,
1284 .validate_super = super_90_validate,
1285 .sync_super = super_90_sync,
1287 [1] = {
1288 .name = "md-1",
1289 .owner = THIS_MODULE,
1290 .load_super = super_1_load,
1291 .validate_super = super_1_validate,
1292 .sync_super = super_1_sync,
1296 static mdk_rdev_t * match_dev_unit(mddev_t *mddev, mdk_rdev_t *dev)
1298 struct list_head *tmp;
1299 mdk_rdev_t *rdev;
1301 ITERATE_RDEV(mddev,rdev,tmp)
1302 if (rdev->bdev->bd_contains == dev->bdev->bd_contains)
1303 return rdev;
1305 return NULL;
1308 static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1310 struct list_head *tmp;
1311 mdk_rdev_t *rdev;
1313 ITERATE_RDEV(mddev1,rdev,tmp)
1314 if (match_dev_unit(mddev2, rdev))
1315 return 1;
1317 return 0;
1320 static LIST_HEAD(pending_raid_disks);
1322 static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1324 mdk_rdev_t *same_pdev;
1325 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1326 struct kobject *ko;
1327 char *s;
1329 if (rdev->mddev) {
1330 MD_BUG();
1331 return -EINVAL;
1333 /* make sure rdev->size exceeds mddev->size */
1334 if (rdev->size && (mddev->size == 0 || rdev->size < mddev->size)) {
1335 if (mddev->pers)
1336 /* Cannot change size, so fail */
1337 return -ENOSPC;
1338 else
1339 mddev->size = rdev->size;
1341 same_pdev = match_dev_unit(mddev, rdev);
1342 if (same_pdev)
1343 printk(KERN_WARNING
1344 "%s: WARNING: %s appears to be on the same physical"
1345 " disk as %s. True\n protection against single-disk"
1346 " failure might be compromised.\n",
1347 mdname(mddev), bdevname(rdev->bdev,b),
1348 bdevname(same_pdev->bdev,b2));
1350 /* Verify rdev->desc_nr is unique.
1351 * If it is -1, assign a free number, else
1352 * check number is not in use
1354 if (rdev->desc_nr < 0) {
1355 int choice = 0;
1356 if (mddev->pers) choice = mddev->raid_disks;
1357 while (find_rdev_nr(mddev, choice))
1358 choice++;
1359 rdev->desc_nr = choice;
1360 } else {
1361 if (find_rdev_nr(mddev, rdev->desc_nr))
1362 return -EBUSY;
1364 bdevname(rdev->bdev,b);
1365 if (kobject_set_name(&rdev->kobj, "dev-%s", b) < 0)
1366 return -ENOMEM;
1367 while ( (s=strchr(rdev->kobj.k_name, '/')) != NULL)
1368 *s = '!';
1370 list_add(&rdev->same_set, &mddev->disks);
1371 rdev->mddev = mddev;
1372 printk(KERN_INFO "md: bind<%s>\n", b);
1374 rdev->kobj.parent = &mddev->kobj;
1375 kobject_add(&rdev->kobj);
1377 if (rdev->bdev->bd_part)
1378 ko = &rdev->bdev->bd_part->kobj;
1379 else
1380 ko = &rdev->bdev->bd_disk->kobj;
1381 sysfs_create_link(&rdev->kobj, ko, "block");
1382 bd_claim_by_disk(rdev->bdev, rdev, mddev->gendisk);
1383 return 0;
1386 static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1388 char b[BDEVNAME_SIZE];
1389 if (!rdev->mddev) {
1390 MD_BUG();
1391 return;
1393 bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk);
1394 list_del_init(&rdev->same_set);
1395 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1396 rdev->mddev = NULL;
1397 sysfs_remove_link(&rdev->kobj, "block");
1398 kobject_del(&rdev->kobj);
1402 * prevent the device from being mounted, repartitioned or
1403 * otherwise reused by a RAID array (or any other kernel
1404 * subsystem), by bd_claiming the device.
1406 static int lock_rdev(mdk_rdev_t *rdev, dev_t dev)
1408 int err = 0;
1409 struct block_device *bdev;
1410 char b[BDEVNAME_SIZE];
1412 bdev = open_partition_by_devnum(dev, FMODE_READ|FMODE_WRITE);
1413 if (IS_ERR(bdev)) {
1414 printk(KERN_ERR "md: could not open %s.\n",
1415 __bdevname(dev, b));
1416 return PTR_ERR(bdev);
1418 err = bd_claim(bdev, rdev);
1419 if (err) {
1420 printk(KERN_ERR "md: could not bd_claim %s.\n",
1421 bdevname(bdev, b));
1422 blkdev_put_partition(bdev);
1423 return err;
1425 rdev->bdev = bdev;
1426 return err;
1429 static void unlock_rdev(mdk_rdev_t *rdev)
1431 struct block_device *bdev = rdev->bdev;
1432 rdev->bdev = NULL;
1433 if (!bdev)
1434 MD_BUG();
1435 bd_release(bdev);
1436 blkdev_put_partition(bdev);
1439 void md_autodetect_dev(dev_t dev);
1441 static void export_rdev(mdk_rdev_t * rdev)
1443 char b[BDEVNAME_SIZE];
1444 printk(KERN_INFO "md: export_rdev(%s)\n",
1445 bdevname(rdev->bdev,b));
1446 if (rdev->mddev)
1447 MD_BUG();
1448 free_disk_sb(rdev);
1449 list_del_init(&rdev->same_set);
1450 #ifndef MODULE
1451 md_autodetect_dev(rdev->bdev->bd_dev);
1452 #endif
1453 unlock_rdev(rdev);
1454 kobject_put(&rdev->kobj);
1457 static void kick_rdev_from_array(mdk_rdev_t * rdev)
1459 unbind_rdev_from_array(rdev);
1460 export_rdev(rdev);
1463 static void export_array(mddev_t *mddev)
1465 struct list_head *tmp;
1466 mdk_rdev_t *rdev;
1468 ITERATE_RDEV(mddev,rdev,tmp) {
1469 if (!rdev->mddev) {
1470 MD_BUG();
1471 continue;
1473 kick_rdev_from_array(rdev);
1475 if (!list_empty(&mddev->disks))
1476 MD_BUG();
1477 mddev->raid_disks = 0;
1478 mddev->major_version = 0;
1481 static void print_desc(mdp_disk_t *desc)
1483 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1484 desc->major,desc->minor,desc->raid_disk,desc->state);
1487 static void print_sb(mdp_super_t *sb)
1489 int i;
1491 printk(KERN_INFO
1492 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1493 sb->major_version, sb->minor_version, sb->patch_version,
1494 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1495 sb->ctime);
1496 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1497 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1498 sb->md_minor, sb->layout, sb->chunk_size);
1499 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1500 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1501 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1502 sb->failed_disks, sb->spare_disks,
1503 sb->sb_csum, (unsigned long)sb->events_lo);
1505 printk(KERN_INFO);
1506 for (i = 0; i < MD_SB_DISKS; i++) {
1507 mdp_disk_t *desc;
1509 desc = sb->disks + i;
1510 if (desc->number || desc->major || desc->minor ||
1511 desc->raid_disk || (desc->state && (desc->state != 4))) {
1512 printk(" D %2d: ", i);
1513 print_desc(desc);
1516 printk(KERN_INFO "md: THIS: ");
1517 print_desc(&sb->this_disk);
1521 static void print_rdev(mdk_rdev_t *rdev)
1523 char b[BDEVNAME_SIZE];
1524 printk(KERN_INFO "md: rdev %s, SZ:%08llu F:%d S:%d DN:%u\n",
1525 bdevname(rdev->bdev,b), (unsigned long long)rdev->size,
1526 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
1527 rdev->desc_nr);
1528 if (rdev->sb_loaded) {
1529 printk(KERN_INFO "md: rdev superblock:\n");
1530 print_sb((mdp_super_t*)page_address(rdev->sb_page));
1531 } else
1532 printk(KERN_INFO "md: no rdev superblock!\n");
1535 static void md_print_devices(void)
1537 struct list_head *tmp, *tmp2;
1538 mdk_rdev_t *rdev;
1539 mddev_t *mddev;
1540 char b[BDEVNAME_SIZE];
1542 printk("\n");
1543 printk("md: **********************************\n");
1544 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
1545 printk("md: **********************************\n");
1546 ITERATE_MDDEV(mddev,tmp) {
1548 if (mddev->bitmap)
1549 bitmap_print_sb(mddev->bitmap);
1550 else
1551 printk("%s: ", mdname(mddev));
1552 ITERATE_RDEV(mddev,rdev,tmp2)
1553 printk("<%s>", bdevname(rdev->bdev,b));
1554 printk("\n");
1556 ITERATE_RDEV(mddev,rdev,tmp2)
1557 print_rdev(rdev);
1559 printk("md: **********************************\n");
1560 printk("\n");
1564 static void sync_sbs(mddev_t * mddev, int nospares)
1566 /* Update each superblock (in-memory image), but
1567 * if we are allowed to, skip spares which already
1568 * have the right event counter, or have one earlier
1569 * (which would mean they aren't being marked as dirty
1570 * with the rest of the array)
1572 mdk_rdev_t *rdev;
1573 struct list_head *tmp;
1575 ITERATE_RDEV(mddev,rdev,tmp) {
1576 if (rdev->sb_events == mddev->events ||
1577 (nospares &&
1578 rdev->raid_disk < 0 &&
1579 (rdev->sb_events&1)==0 &&
1580 rdev->sb_events+1 == mddev->events)) {
1581 /* Don't update this superblock */
1582 rdev->sb_loaded = 2;
1583 } else {
1584 super_types[mddev->major_version].
1585 sync_super(mddev, rdev);
1586 rdev->sb_loaded = 1;
1591 void md_update_sb(mddev_t * mddev)
1593 int err;
1594 struct list_head *tmp;
1595 mdk_rdev_t *rdev;
1596 int sync_req;
1597 int nospares = 0;
1599 repeat:
1600 spin_lock_irq(&mddev->write_lock);
1601 sync_req = mddev->in_sync;
1602 mddev->utime = get_seconds();
1603 if (mddev->sb_dirty == 3)
1604 /* just a clean<-> dirty transition, possibly leave spares alone,
1605 * though if events isn't the right even/odd, we will have to do
1606 * spares after all
1608 nospares = 1;
1610 /* If this is just a dirty<->clean transition, and the array is clean
1611 * and 'events' is odd, we can roll back to the previous clean state */
1612 if (mddev->sb_dirty == 3
1613 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
1614 && (mddev->events & 1))
1615 mddev->events--;
1616 else {
1617 /* otherwise we have to go forward and ... */
1618 mddev->events ++;
1619 if (!mddev->in_sync || mddev->recovery_cp != MaxSector) { /* not clean */
1620 /* .. if the array isn't clean, insist on an odd 'events' */
1621 if ((mddev->events&1)==0) {
1622 mddev->events++;
1623 nospares = 0;
1625 } else {
1626 /* otherwise insist on an even 'events' (for clean states) */
1627 if ((mddev->events&1)) {
1628 mddev->events++;
1629 nospares = 0;
1634 if (!mddev->events) {
1636 * oops, this 64-bit counter should never wrap.
1637 * Either we are in around ~1 trillion A.C., assuming
1638 * 1 reboot per second, or we have a bug:
1640 MD_BUG();
1641 mddev->events --;
1645 * do not write anything to disk if using
1646 * nonpersistent superblocks
1648 if (!mddev->persistent) {
1649 if (!mddev->external)
1650 mddev->sb_dirty = 0;
1651 spin_unlock_irq(&mddev->write_lock);
1652 wake_up(&mddev->sb_wait);
1653 return;
1655 mddev->sb_dirty = 2;
1656 sync_sbs(mddev, nospares);
1657 spin_unlock_irq(&mddev->write_lock);
1659 dprintk(KERN_INFO
1660 "md: updating %s RAID superblock on device (in sync %d)\n",
1661 mdname(mddev),mddev->in_sync);
1663 err = bitmap_update_sb(mddev->bitmap);
1664 ITERATE_RDEV(mddev,rdev,tmp) {
1665 char b[BDEVNAME_SIZE];
1666 dprintk(KERN_INFO "md: ");
1667 if (rdev->sb_loaded != 1)
1668 continue; /* no noise on spare devices */
1669 if (test_bit(Faulty, &rdev->flags))
1670 dprintk("(skipping faulty ");
1672 dprintk("%s ", bdevname(rdev->bdev,b));
1673 if (!test_bit(Faulty, &rdev->flags)) {
1674 md_super_write(mddev,rdev,
1675 rdev->sb_offset<<1, rdev->sb_size,
1676 rdev->sb_page);
1677 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
1678 bdevname(rdev->bdev,b),
1679 (unsigned long long)rdev->sb_offset);
1680 rdev->sb_events = mddev->events;
1682 } else
1683 dprintk(")\n");
1684 if (mddev->level == LEVEL_MULTIPATH)
1685 /* only need to write one superblock... */
1686 break;
1688 md_super_wait(mddev);
1689 /* if there was a failure, sb_dirty was set to 1, and we re-write super */
1691 spin_lock_irq(&mddev->write_lock);
1692 if (mddev->in_sync != sync_req|| mddev->sb_dirty == 1) {
1693 /* have to write it out again */
1694 spin_unlock_irq(&mddev->write_lock);
1695 goto repeat;
1697 mddev->sb_dirty = 0;
1698 spin_unlock_irq(&mddev->write_lock);
1699 wake_up(&mddev->sb_wait);
1702 EXPORT_SYMBOL_GPL(md_update_sb);
1704 /* words written to sysfs files may, or my not, be \n terminated.
1705 * We want to accept with case. For this we use cmd_match.
1707 static int cmd_match(const char *cmd, const char *str)
1709 /* See if cmd, written into a sysfs file, matches
1710 * str. They must either be the same, or cmd can
1711 * have a trailing newline
1713 while (*cmd && *str && *cmd == *str) {
1714 cmd++;
1715 str++;
1717 if (*cmd == '\n')
1718 cmd++;
1719 if (*str || *cmd)
1720 return 0;
1721 return 1;
1724 struct rdev_sysfs_entry {
1725 struct attribute attr;
1726 ssize_t (*show)(mdk_rdev_t *, char *);
1727 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
1730 static ssize_t
1731 state_show(mdk_rdev_t *rdev, char *page)
1733 char *sep = "";
1734 int len=0;
1736 if (test_bit(Faulty, &rdev->flags)) {
1737 len+= sprintf(page+len, "%sfaulty",sep);
1738 sep = ",";
1740 if (test_bit(In_sync, &rdev->flags)) {
1741 len += sprintf(page+len, "%sin_sync",sep);
1742 sep = ",";
1744 if (test_bit(WriteMostly, &rdev->flags)) {
1745 len += sprintf(page+len, "%swrite_mostly",sep);
1746 sep = ",";
1748 if (!test_bit(Faulty, &rdev->flags) &&
1749 !test_bit(In_sync, &rdev->flags)) {
1750 len += sprintf(page+len, "%sspare", sep);
1751 sep = ",";
1753 return len+sprintf(page+len, "\n");
1756 static ssize_t
1757 state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1759 /* can write
1760 * faulty - simulates and error
1761 * remove - disconnects the device
1762 * writemostly - sets write_mostly
1763 * -writemostly - clears write_mostly
1765 int err = -EINVAL;
1766 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
1767 md_error(rdev->mddev, rdev);
1768 err = 0;
1769 } else if (cmd_match(buf, "remove")) {
1770 if (rdev->raid_disk >= 0)
1771 err = -EBUSY;
1772 else {
1773 mddev_t *mddev = rdev->mddev;
1774 kick_rdev_from_array(rdev);
1775 md_update_sb(mddev);
1776 md_new_event(mddev);
1777 err = 0;
1779 } else if (cmd_match(buf, "writemostly")) {
1780 set_bit(WriteMostly, &rdev->flags);
1781 err = 0;
1782 } else if (cmd_match(buf, "-writemostly")) {
1783 clear_bit(WriteMostly, &rdev->flags);
1784 err = 0;
1786 return err ? err : len;
1788 static struct rdev_sysfs_entry rdev_state =
1789 __ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
1791 static ssize_t
1792 super_show(mdk_rdev_t *rdev, char *page)
1794 if (rdev->sb_loaded && rdev->sb_size) {
1795 memcpy(page, page_address(rdev->sb_page), rdev->sb_size);
1796 return rdev->sb_size;
1797 } else
1798 return 0;
1800 static struct rdev_sysfs_entry rdev_super = __ATTR_RO(super);
1802 static ssize_t
1803 errors_show(mdk_rdev_t *rdev, char *page)
1805 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
1808 static ssize_t
1809 errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1811 char *e;
1812 unsigned long n = simple_strtoul(buf, &e, 10);
1813 if (*buf && (*e == 0 || *e == '\n')) {
1814 atomic_set(&rdev->corrected_errors, n);
1815 return len;
1817 return -EINVAL;
1819 static struct rdev_sysfs_entry rdev_errors =
1820 __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
1822 static ssize_t
1823 slot_show(mdk_rdev_t *rdev, char *page)
1825 if (rdev->raid_disk < 0)
1826 return sprintf(page, "none\n");
1827 else
1828 return sprintf(page, "%d\n", rdev->raid_disk);
1831 static ssize_t
1832 slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1834 char *e;
1835 int slot = simple_strtoul(buf, &e, 10);
1836 if (strncmp(buf, "none", 4)==0)
1837 slot = -1;
1838 else if (e==buf || (*e && *e!= '\n'))
1839 return -EINVAL;
1840 if (rdev->mddev->pers)
1841 /* Cannot set slot in active array (yet) */
1842 return -EBUSY;
1843 if (slot >= rdev->mddev->raid_disks)
1844 return -ENOSPC;
1845 rdev->raid_disk = slot;
1846 /* assume it is working */
1847 rdev->flags = 0;
1848 set_bit(In_sync, &rdev->flags);
1849 return len;
1853 static struct rdev_sysfs_entry rdev_slot =
1854 __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
1856 static ssize_t
1857 offset_show(mdk_rdev_t *rdev, char *page)
1859 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
1862 static ssize_t
1863 offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1865 char *e;
1866 unsigned long long offset = simple_strtoull(buf, &e, 10);
1867 if (e==buf || (*e && *e != '\n'))
1868 return -EINVAL;
1869 if (rdev->mddev->pers)
1870 return -EBUSY;
1871 rdev->data_offset = offset;
1872 return len;
1875 static struct rdev_sysfs_entry rdev_offset =
1876 __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
1878 static ssize_t
1879 rdev_size_show(mdk_rdev_t *rdev, char *page)
1881 return sprintf(page, "%llu\n", (unsigned long long)rdev->size);
1884 static ssize_t
1885 rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1887 char *e;
1888 unsigned long long size = simple_strtoull(buf, &e, 10);
1889 if (e==buf || (*e && *e != '\n'))
1890 return -EINVAL;
1891 if (rdev->mddev->pers)
1892 return -EBUSY;
1893 rdev->size = size;
1894 if (size < rdev->mddev->size || rdev->mddev->size == 0)
1895 rdev->mddev->size = size;
1896 return len;
1899 static struct rdev_sysfs_entry rdev_size =
1900 __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
1902 static struct attribute *rdev_default_attrs[] = {
1903 &rdev_state.attr,
1904 &rdev_super.attr,
1905 &rdev_errors.attr,
1906 &rdev_slot.attr,
1907 &rdev_offset.attr,
1908 &rdev_size.attr,
1909 NULL,
1911 static ssize_t
1912 rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
1914 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
1915 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
1917 if (!entry->show)
1918 return -EIO;
1919 return entry->show(rdev, page);
1922 static ssize_t
1923 rdev_attr_store(struct kobject *kobj, struct attribute *attr,
1924 const char *page, size_t length)
1926 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
1927 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
1929 if (!entry->store)
1930 return -EIO;
1931 if (!capable(CAP_SYS_ADMIN))
1932 return -EACCES;
1933 return entry->store(rdev, page, length);
1936 static void rdev_free(struct kobject *ko)
1938 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
1939 kfree(rdev);
1941 static struct sysfs_ops rdev_sysfs_ops = {
1942 .show = rdev_attr_show,
1943 .store = rdev_attr_store,
1945 static struct kobj_type rdev_ktype = {
1946 .release = rdev_free,
1947 .sysfs_ops = &rdev_sysfs_ops,
1948 .default_attrs = rdev_default_attrs,
1952 * Import a device. If 'super_format' >= 0, then sanity check the superblock
1954 * mark the device faulty if:
1956 * - the device is nonexistent (zero size)
1957 * - the device has no valid superblock
1959 * a faulty rdev _never_ has rdev->sb set.
1961 static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
1963 char b[BDEVNAME_SIZE];
1964 int err;
1965 mdk_rdev_t *rdev;
1966 sector_t size;
1968 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
1969 if (!rdev) {
1970 printk(KERN_ERR "md: could not alloc mem for new device!\n");
1971 return ERR_PTR(-ENOMEM);
1974 if ((err = alloc_disk_sb(rdev)))
1975 goto abort_free;
1977 err = lock_rdev(rdev, newdev);
1978 if (err)
1979 goto abort_free;
1981 rdev->kobj.parent = NULL;
1982 rdev->kobj.ktype = &rdev_ktype;
1983 kobject_init(&rdev->kobj);
1985 rdev->desc_nr = -1;
1986 rdev->flags = 0;
1987 rdev->data_offset = 0;
1988 rdev->sb_events = 0;
1989 atomic_set(&rdev->nr_pending, 0);
1990 atomic_set(&rdev->read_errors, 0);
1991 atomic_set(&rdev->corrected_errors, 0);
1993 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
1994 if (!size) {
1995 printk(KERN_WARNING
1996 "md: %s has zero or unknown size, marking faulty!\n",
1997 bdevname(rdev->bdev,b));
1998 err = -EINVAL;
1999 goto abort_free;
2002 if (super_format >= 0) {
2003 err = super_types[super_format].
2004 load_super(rdev, NULL, super_minor);
2005 if (err == -EINVAL) {
2006 printk(KERN_WARNING
2007 "md: %s has invalid sb, not importing!\n",
2008 bdevname(rdev->bdev,b));
2009 goto abort_free;
2011 if (err < 0) {
2012 printk(KERN_WARNING
2013 "md: could not read %s's sb, not importing!\n",
2014 bdevname(rdev->bdev,b));
2015 goto abort_free;
2018 INIT_LIST_HEAD(&rdev->same_set);
2020 return rdev;
2022 abort_free:
2023 if (rdev->sb_page) {
2024 if (rdev->bdev)
2025 unlock_rdev(rdev);
2026 free_disk_sb(rdev);
2028 kfree(rdev);
2029 return ERR_PTR(err);
2033 * Check a full RAID array for plausibility
2037 static void analyze_sbs(mddev_t * mddev)
2039 int i;
2040 struct list_head *tmp;
2041 mdk_rdev_t *rdev, *freshest;
2042 char b[BDEVNAME_SIZE];
2044 freshest = NULL;
2045 ITERATE_RDEV(mddev,rdev,tmp)
2046 switch (super_types[mddev->major_version].
2047 load_super(rdev, freshest, mddev->minor_version)) {
2048 case 1:
2049 freshest = rdev;
2050 break;
2051 case 0:
2052 break;
2053 default:
2054 printk( KERN_ERR \
2055 "md: fatal superblock inconsistency in %s"
2056 " -- removing from array\n",
2057 bdevname(rdev->bdev,b));
2058 kick_rdev_from_array(rdev);
2062 super_types[mddev->major_version].
2063 validate_super(mddev, freshest);
2065 i = 0;
2066 ITERATE_RDEV(mddev,rdev,tmp) {
2067 if (rdev != freshest)
2068 if (super_types[mddev->major_version].
2069 validate_super(mddev, rdev)) {
2070 printk(KERN_WARNING "md: kicking non-fresh %s"
2071 " from array!\n",
2072 bdevname(rdev->bdev,b));
2073 kick_rdev_from_array(rdev);
2074 continue;
2076 if (mddev->level == LEVEL_MULTIPATH) {
2077 rdev->desc_nr = i++;
2078 rdev->raid_disk = rdev->desc_nr;
2079 set_bit(In_sync, &rdev->flags);
2085 if (mddev->recovery_cp != MaxSector &&
2086 mddev->level >= 1)
2087 printk(KERN_ERR "md: %s: raid array is not clean"
2088 " -- starting background reconstruction\n",
2089 mdname(mddev));
2093 static ssize_t
2094 safe_delay_show(mddev_t *mddev, char *page)
2096 int msec = (mddev->safemode_delay*1000)/HZ;
2097 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
2099 static ssize_t
2100 safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
2102 int scale=1;
2103 int dot=0;
2104 int i;
2105 unsigned long msec;
2106 char buf[30];
2107 char *e;
2108 /* remove a period, and count digits after it */
2109 if (len >= sizeof(buf))
2110 return -EINVAL;
2111 strlcpy(buf, cbuf, len);
2112 buf[len] = 0;
2113 for (i=0; i<len; i++) {
2114 if (dot) {
2115 if (isdigit(buf[i])) {
2116 buf[i-1] = buf[i];
2117 scale *= 10;
2119 buf[i] = 0;
2120 } else if (buf[i] == '.') {
2121 dot=1;
2122 buf[i] = 0;
2125 msec = simple_strtoul(buf, &e, 10);
2126 if (e == buf || (*e && *e != '\n'))
2127 return -EINVAL;
2128 msec = (msec * 1000) / scale;
2129 if (msec == 0)
2130 mddev->safemode_delay = 0;
2131 else {
2132 mddev->safemode_delay = (msec*HZ)/1000;
2133 if (mddev->safemode_delay == 0)
2134 mddev->safemode_delay = 1;
2136 return len;
2138 static struct md_sysfs_entry md_safe_delay =
2139 __ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
2141 static ssize_t
2142 level_show(mddev_t *mddev, char *page)
2144 struct mdk_personality *p = mddev->pers;
2145 if (p)
2146 return sprintf(page, "%s\n", p->name);
2147 else if (mddev->clevel[0])
2148 return sprintf(page, "%s\n", mddev->clevel);
2149 else if (mddev->level != LEVEL_NONE)
2150 return sprintf(page, "%d\n", mddev->level);
2151 else
2152 return 0;
2155 static ssize_t
2156 level_store(mddev_t *mddev, const char *buf, size_t len)
2158 int rv = len;
2159 if (mddev->pers)
2160 return -EBUSY;
2161 if (len == 0)
2162 return 0;
2163 if (len >= sizeof(mddev->clevel))
2164 return -ENOSPC;
2165 strncpy(mddev->clevel, buf, len);
2166 if (mddev->clevel[len-1] == '\n')
2167 len--;
2168 mddev->clevel[len] = 0;
2169 mddev->level = LEVEL_NONE;
2170 return rv;
2173 static struct md_sysfs_entry md_level =
2174 __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
2177 static ssize_t
2178 layout_show(mddev_t *mddev, char *page)
2180 /* just a number, not meaningful for all levels */
2181 return sprintf(page, "%d\n", mddev->layout);
2184 static ssize_t
2185 layout_store(mddev_t *mddev, const char *buf, size_t len)
2187 char *e;
2188 unsigned long n = simple_strtoul(buf, &e, 10);
2189 if (mddev->pers)
2190 return -EBUSY;
2192 if (!*buf || (*e && *e != '\n'))
2193 return -EINVAL;
2195 mddev->layout = n;
2196 return len;
2198 static struct md_sysfs_entry md_layout =
2199 __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
2202 static ssize_t
2203 raid_disks_show(mddev_t *mddev, char *page)
2205 if (mddev->raid_disks == 0)
2206 return 0;
2207 return sprintf(page, "%d\n", mddev->raid_disks);
2210 static int update_raid_disks(mddev_t *mddev, int raid_disks);
2212 static ssize_t
2213 raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
2215 /* can only set raid_disks if array is not yet active */
2216 char *e;
2217 int rv = 0;
2218 unsigned long n = simple_strtoul(buf, &e, 10);
2220 if (!*buf || (*e && *e != '\n'))
2221 return -EINVAL;
2223 if (mddev->pers)
2224 rv = update_raid_disks(mddev, n);
2225 else
2226 mddev->raid_disks = n;
2227 return rv ? rv : len;
2229 static struct md_sysfs_entry md_raid_disks =
2230 __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
2232 static ssize_t
2233 chunk_size_show(mddev_t *mddev, char *page)
2235 return sprintf(page, "%d\n", mddev->chunk_size);
2238 static ssize_t
2239 chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
2241 /* can only set chunk_size if array is not yet active */
2242 char *e;
2243 unsigned long n = simple_strtoul(buf, &e, 10);
2245 if (mddev->pers)
2246 return -EBUSY;
2247 if (!*buf || (*e && *e != '\n'))
2248 return -EINVAL;
2250 mddev->chunk_size = n;
2251 return len;
2253 static struct md_sysfs_entry md_chunk_size =
2254 __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
2256 static ssize_t
2257 resync_start_show(mddev_t *mddev, char *page)
2259 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
2262 static ssize_t
2263 resync_start_store(mddev_t *mddev, const char *buf, size_t len)
2265 /* can only set chunk_size if array is not yet active */
2266 char *e;
2267 unsigned long long n = simple_strtoull(buf, &e, 10);
2269 if (mddev->pers)
2270 return -EBUSY;
2271 if (!*buf || (*e && *e != '\n'))
2272 return -EINVAL;
2274 mddev->recovery_cp = n;
2275 return len;
2277 static struct md_sysfs_entry md_resync_start =
2278 __ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
2281 * The array state can be:
2283 * clear
2284 * No devices, no size, no level
2285 * Equivalent to STOP_ARRAY ioctl
2286 * inactive
2287 * May have some settings, but array is not active
2288 * all IO results in error
2289 * When written, doesn't tear down array, but just stops it
2290 * suspended (not supported yet)
2291 * All IO requests will block. The array can be reconfigured.
2292 * Writing this, if accepted, will block until array is quiessent
2293 * readonly
2294 * no resync can happen. no superblocks get written.
2295 * write requests fail
2296 * read-auto
2297 * like readonly, but behaves like 'clean' on a write request.
2299 * clean - no pending writes, but otherwise active.
2300 * When written to inactive array, starts without resync
2301 * If a write request arrives then
2302 * if metadata is known, mark 'dirty' and switch to 'active'.
2303 * if not known, block and switch to write-pending
2304 * If written to an active array that has pending writes, then fails.
2305 * active
2306 * fully active: IO and resync can be happening.
2307 * When written to inactive array, starts with resync
2309 * write-pending
2310 * clean, but writes are blocked waiting for 'active' to be written.
2312 * active-idle
2313 * like active, but no writes have been seen for a while (100msec).
2316 enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
2317 write_pending, active_idle, bad_word};
2318 char *array_states[] = {
2319 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
2320 "write-pending", "active-idle", NULL };
2322 static int match_word(const char *word, char **list)
2324 int n;
2325 for (n=0; list[n]; n++)
2326 if (cmd_match(word, list[n]))
2327 break;
2328 return n;
2331 static ssize_t
2332 array_state_show(mddev_t *mddev, char *page)
2334 enum array_state st = inactive;
2336 if (mddev->pers)
2337 switch(mddev->ro) {
2338 case 1:
2339 st = readonly;
2340 break;
2341 case 2:
2342 st = read_auto;
2343 break;
2344 case 0:
2345 if (mddev->in_sync)
2346 st = clean;
2347 else if (mddev->sb_dirty)
2348 st = write_pending;
2349 else if (mddev->safemode)
2350 st = active_idle;
2351 else
2352 st = active;
2354 else {
2355 if (list_empty(&mddev->disks) &&
2356 mddev->raid_disks == 0 &&
2357 mddev->size == 0)
2358 st = clear;
2359 else
2360 st = inactive;
2362 return sprintf(page, "%s\n", array_states[st]);
2365 static int do_md_stop(mddev_t * mddev, int ro);
2366 static int do_md_run(mddev_t * mddev);
2367 static int restart_array(mddev_t *mddev);
2369 static ssize_t
2370 array_state_store(mddev_t *mddev, const char *buf, size_t len)
2372 int err = -EINVAL;
2373 enum array_state st = match_word(buf, array_states);
2374 switch(st) {
2375 case bad_word:
2376 break;
2377 case clear:
2378 /* stopping an active array */
2379 if (atomic_read(&mddev->active) > 1)
2380 return -EBUSY;
2381 err = do_md_stop(mddev, 0);
2382 break;
2383 case inactive:
2384 /* stopping an active array */
2385 if (mddev->pers) {
2386 if (atomic_read(&mddev->active) > 1)
2387 return -EBUSY;
2388 err = do_md_stop(mddev, 2);
2389 } else
2390 err = 0; /* already inactive */
2391 break;
2392 case suspended:
2393 break; /* not supported yet */
2394 case readonly:
2395 if (mddev->pers)
2396 err = do_md_stop(mddev, 1);
2397 else {
2398 mddev->ro = 1;
2399 err = do_md_run(mddev);
2401 break;
2402 case read_auto:
2403 /* stopping an active array */
2404 if (mddev->pers) {
2405 err = do_md_stop(mddev, 1);
2406 if (err == 0)
2407 mddev->ro = 2; /* FIXME mark devices writable */
2408 } else {
2409 mddev->ro = 2;
2410 err = do_md_run(mddev);
2412 break;
2413 case clean:
2414 if (mddev->pers) {
2415 restart_array(mddev);
2416 spin_lock_irq(&mddev->write_lock);
2417 if (atomic_read(&mddev->writes_pending) == 0) {
2418 if (mddev->in_sync == 0) {
2419 mddev->in_sync = 1;
2420 if (mddev->persistent)
2421 mddev->sb_dirty = 1;
2423 err = 0;
2424 } else
2425 err = -EBUSY;
2426 spin_unlock_irq(&mddev->write_lock);
2427 } else {
2428 mddev->ro = 0;
2429 mddev->recovery_cp = MaxSector;
2430 err = do_md_run(mddev);
2432 break;
2433 case active:
2434 if (mddev->pers) {
2435 restart_array(mddev);
2436 mddev->sb_dirty = 0;
2437 wake_up(&mddev->sb_wait);
2438 err = 0;
2439 } else {
2440 mddev->ro = 0;
2441 err = do_md_run(mddev);
2443 break;
2444 case write_pending:
2445 case active_idle:
2446 /* these cannot be set */
2447 break;
2449 if (err)
2450 return err;
2451 else
2452 return len;
2454 static struct md_sysfs_entry md_array_state =
2455 __ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
2457 static ssize_t
2458 null_show(mddev_t *mddev, char *page)
2460 return -EINVAL;
2463 static ssize_t
2464 new_dev_store(mddev_t *mddev, const char *buf, size_t len)
2466 /* buf must be %d:%d\n? giving major and minor numbers */
2467 /* The new device is added to the array.
2468 * If the array has a persistent superblock, we read the
2469 * superblock to initialise info and check validity.
2470 * Otherwise, only checking done is that in bind_rdev_to_array,
2471 * which mainly checks size.
2473 char *e;
2474 int major = simple_strtoul(buf, &e, 10);
2475 int minor;
2476 dev_t dev;
2477 mdk_rdev_t *rdev;
2478 int err;
2480 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
2481 return -EINVAL;
2482 minor = simple_strtoul(e+1, &e, 10);
2483 if (*e && *e != '\n')
2484 return -EINVAL;
2485 dev = MKDEV(major, minor);
2486 if (major != MAJOR(dev) ||
2487 minor != MINOR(dev))
2488 return -EOVERFLOW;
2491 if (mddev->persistent) {
2492 rdev = md_import_device(dev, mddev->major_version,
2493 mddev->minor_version);
2494 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
2495 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
2496 mdk_rdev_t, same_set);
2497 err = super_types[mddev->major_version]
2498 .load_super(rdev, rdev0, mddev->minor_version);
2499 if (err < 0)
2500 goto out;
2502 } else
2503 rdev = md_import_device(dev, -1, -1);
2505 if (IS_ERR(rdev))
2506 return PTR_ERR(rdev);
2507 err = bind_rdev_to_array(rdev, mddev);
2508 out:
2509 if (err)
2510 export_rdev(rdev);
2511 return err ? err : len;
2514 static struct md_sysfs_entry md_new_device =
2515 __ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
2517 static ssize_t
2518 size_show(mddev_t *mddev, char *page)
2520 return sprintf(page, "%llu\n", (unsigned long long)mddev->size);
2523 static int update_size(mddev_t *mddev, unsigned long size);
2525 static ssize_t
2526 size_store(mddev_t *mddev, const char *buf, size_t len)
2528 /* If array is inactive, we can reduce the component size, but
2529 * not increase it (except from 0).
2530 * If array is active, we can try an on-line resize
2532 char *e;
2533 int err = 0;
2534 unsigned long long size = simple_strtoull(buf, &e, 10);
2535 if (!*buf || *buf == '\n' ||
2536 (*e && *e != '\n'))
2537 return -EINVAL;
2539 if (mddev->pers) {
2540 err = update_size(mddev, size);
2541 md_update_sb(mddev);
2542 } else {
2543 if (mddev->size == 0 ||
2544 mddev->size > size)
2545 mddev->size = size;
2546 else
2547 err = -ENOSPC;
2549 return err ? err : len;
2552 static struct md_sysfs_entry md_size =
2553 __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
2556 /* Metdata version.
2557 * This is one of
2558 * 'none' for arrays with no metadata (good luck...)
2559 * 'external' for arrays with externally managed metadata,
2560 * or N.M for internally known formats
2562 static ssize_t
2563 metadata_show(mddev_t *mddev, char *page)
2565 if (mddev->persistent)
2566 return sprintf(page, "%d.%d\n",
2567 mddev->major_version, mddev->minor_version);
2568 else if (mddev->external)
2569 return sprintf(page, "external\n");
2570 else
2571 return sprintf(page, "none\n");
2574 static ssize_t
2575 metadata_store(mddev_t *mddev, const char *buf, size_t len)
2577 int major, minor;
2578 char *e;
2579 if (!list_empty(&mddev->disks))
2580 return -EBUSY;
2582 if (cmd_match(buf, "none")) {
2583 mddev->persistent = 0;
2584 mddev->external = 0;
2585 mddev->major_version = 0;
2586 mddev->minor_version = 90;
2587 return len;
2589 if (cmd_match(buf, "external")) {
2590 mddev->persistent = 0;
2591 mddev->external = 1;
2592 mddev->major_version = 0;
2593 mddev->minor_version = 90;
2594 return len;
2596 major = simple_strtoul(buf, &e, 10);
2597 if (e==buf || *e != '.')
2598 return -EINVAL;
2599 buf = e+1;
2600 minor = simple_strtoul(buf, &e, 10);
2601 if (e==buf || *e != '\n')
2602 return -EINVAL;
2603 if (major >= sizeof(super_types)/sizeof(super_types[0]) ||
2604 super_types[major].name == NULL)
2605 return -ENOENT;
2606 mddev->major_version = major;
2607 mddev->minor_version = minor;
2608 mddev->persistent = 1;
2609 mddev->external = 0;
2610 return len;
2613 static struct md_sysfs_entry md_metadata =
2614 __ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
2616 static ssize_t
2617 action_show(mddev_t *mddev, char *page)
2619 char *type = "idle";
2620 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
2621 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) {
2622 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
2623 type = "reshape";
2624 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
2625 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
2626 type = "resync";
2627 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
2628 type = "check";
2629 else
2630 type = "repair";
2631 } else
2632 type = "recover";
2634 return sprintf(page, "%s\n", type);
2637 static ssize_t
2638 action_store(mddev_t *mddev, const char *page, size_t len)
2640 if (!mddev->pers || !mddev->pers->sync_request)
2641 return -EINVAL;
2643 if (cmd_match(page, "idle")) {
2644 if (mddev->sync_thread) {
2645 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2646 md_unregister_thread(mddev->sync_thread);
2647 mddev->sync_thread = NULL;
2648 mddev->recovery = 0;
2650 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
2651 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
2652 return -EBUSY;
2653 else if (cmd_match(page, "resync") || cmd_match(page, "recover"))
2654 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2655 else if (cmd_match(page, "reshape")) {
2656 int err;
2657 if (mddev->pers->start_reshape == NULL)
2658 return -EINVAL;
2659 err = mddev->pers->start_reshape(mddev);
2660 if (err)
2661 return err;
2662 } else {
2663 if (cmd_match(page, "check"))
2664 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
2665 else if (!cmd_match(page, "repair"))
2666 return -EINVAL;
2667 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
2668 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
2670 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2671 md_wakeup_thread(mddev->thread);
2672 return len;
2675 static ssize_t
2676 mismatch_cnt_show(mddev_t *mddev, char *page)
2678 return sprintf(page, "%llu\n",
2679 (unsigned long long) mddev->resync_mismatches);
2682 static struct md_sysfs_entry md_scan_mode =
2683 __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
2686 static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
2688 static ssize_t
2689 sync_min_show(mddev_t *mddev, char *page)
2691 return sprintf(page, "%d (%s)\n", speed_min(mddev),
2692 mddev->sync_speed_min ? "local": "system");
2695 static ssize_t
2696 sync_min_store(mddev_t *mddev, const char *buf, size_t len)
2698 int min;
2699 char *e;
2700 if (strncmp(buf, "system", 6)==0) {
2701 mddev->sync_speed_min = 0;
2702 return len;
2704 min = simple_strtoul(buf, &e, 10);
2705 if (buf == e || (*e && *e != '\n') || min <= 0)
2706 return -EINVAL;
2707 mddev->sync_speed_min = min;
2708 return len;
2711 static struct md_sysfs_entry md_sync_min =
2712 __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
2714 static ssize_t
2715 sync_max_show(mddev_t *mddev, char *page)
2717 return sprintf(page, "%d (%s)\n", speed_max(mddev),
2718 mddev->sync_speed_max ? "local": "system");
2721 static ssize_t
2722 sync_max_store(mddev_t *mddev, const char *buf, size_t len)
2724 int max;
2725 char *e;
2726 if (strncmp(buf, "system", 6)==0) {
2727 mddev->sync_speed_max = 0;
2728 return len;
2730 max = simple_strtoul(buf, &e, 10);
2731 if (buf == e || (*e && *e != '\n') || max <= 0)
2732 return -EINVAL;
2733 mddev->sync_speed_max = max;
2734 return len;
2737 static struct md_sysfs_entry md_sync_max =
2738 __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
2741 static ssize_t
2742 sync_speed_show(mddev_t *mddev, char *page)
2744 unsigned long resync, dt, db;
2745 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active));
2746 dt = ((jiffies - mddev->resync_mark) / HZ);
2747 if (!dt) dt++;
2748 db = resync - (mddev->resync_mark_cnt);
2749 return sprintf(page, "%ld\n", db/dt/2); /* K/sec */
2752 static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
2754 static ssize_t
2755 sync_completed_show(mddev_t *mddev, char *page)
2757 unsigned long max_blocks, resync;
2759 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
2760 max_blocks = mddev->resync_max_sectors;
2761 else
2762 max_blocks = mddev->size << 1;
2764 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active));
2765 return sprintf(page, "%lu / %lu\n", resync, max_blocks);
2768 static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
2770 static ssize_t
2771 max_sync_show(mddev_t *mddev, char *page)
2773 if (mddev->resync_max == MaxSector)
2774 return sprintf(page, "max\n");
2775 else
2776 return sprintf(page, "%llu\n",
2777 (unsigned long long)mddev->resync_max);
2779 static ssize_t
2780 max_sync_store(mddev_t *mddev, const char *buf, size_t len)
2782 if (strncmp(buf, "max", 3)==0)
2783 mddev->resync_max = MaxSector;
2784 else {
2785 char *ep;
2786 unsigned long long max = simple_strtoull(buf, &ep, 10);
2787 if (ep == buf || (*ep != 0 && *ep != '\n'))
2788 return -EINVAL;
2789 if (max < mddev->resync_max &&
2790 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2791 return -EBUSY;
2793 /* Must be a multiple of chunk_size */
2794 if (mddev->chunk_size) {
2795 if (max & (sector_t)((mddev->chunk_size>>9)-1))
2796 return -EINVAL;
2798 mddev->resync_max = max;
2800 wake_up(&mddev->recovery_wait);
2801 return len;
2804 static struct md_sysfs_entry md_max_sync =
2805 __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
2807 static ssize_t
2808 suspend_lo_show(mddev_t *mddev, char *page)
2810 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
2813 static ssize_t
2814 suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
2816 char *e;
2817 unsigned long long new = simple_strtoull(buf, &e, 10);
2819 if (mddev->pers->quiesce == NULL)
2820 return -EINVAL;
2821 if (buf == e || (*e && *e != '\n'))
2822 return -EINVAL;
2823 if (new >= mddev->suspend_hi ||
2824 (new > mddev->suspend_lo && new < mddev->suspend_hi)) {
2825 mddev->suspend_lo = new;
2826 mddev->pers->quiesce(mddev, 2);
2827 return len;
2828 } else
2829 return -EINVAL;
2831 static struct md_sysfs_entry md_suspend_lo =
2832 __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
2835 static ssize_t
2836 suspend_hi_show(mddev_t *mddev, char *page)
2838 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
2841 static ssize_t
2842 suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
2844 char *e;
2845 unsigned long long new = simple_strtoull(buf, &e, 10);
2847 if (mddev->pers->quiesce == NULL)
2848 return -EINVAL;
2849 if (buf == e || (*e && *e != '\n'))
2850 return -EINVAL;
2851 if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) ||
2852 (new > mddev->suspend_lo && new > mddev->suspend_hi)) {
2853 mddev->suspend_hi = new;
2854 mddev->pers->quiesce(mddev, 1);
2855 mddev->pers->quiesce(mddev, 0);
2856 return len;
2857 } else
2858 return -EINVAL;
2860 static struct md_sysfs_entry md_suspend_hi =
2861 __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
2864 static struct attribute *md_default_attrs[] = {
2865 &md_level.attr,
2866 &md_layout.attr,
2867 &md_raid_disks.attr,
2868 &md_chunk_size.attr,
2869 &md_size.attr,
2870 &md_resync_start.attr,
2871 &md_metadata.attr,
2872 &md_new_device.attr,
2873 &md_safe_delay.attr,
2874 &md_array_state.attr,
2875 NULL,
2878 static struct attribute *md_redundancy_attrs[] = {
2879 &md_scan_mode.attr,
2880 &md_mismatches.attr,
2881 &md_sync_min.attr,
2882 &md_sync_max.attr,
2883 &md_sync_speed.attr,
2884 &md_sync_completed.attr,
2885 &md_max_sync.attr,
2886 &md_suspend_lo.attr,
2887 &md_suspend_hi.attr,
2888 NULL,
2890 static struct attribute_group md_redundancy_group = {
2891 .name = NULL,
2892 .attrs = md_redundancy_attrs,
2896 static ssize_t
2897 md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2899 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
2900 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
2901 ssize_t rv;
2903 if (!entry->show)
2904 return -EIO;
2905 rv = mddev_lock(mddev);
2906 if (!rv) {
2907 rv = entry->show(mddev, page);
2908 mddev_unlock(mddev);
2910 return rv;
2913 static ssize_t
2914 md_attr_store(struct kobject *kobj, struct attribute *attr,
2915 const char *page, size_t length)
2917 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
2918 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
2919 ssize_t rv;
2921 if (!entry->store)
2922 return -EIO;
2923 if (!capable(CAP_SYS_ADMIN))
2924 return -EACCES;
2925 rv = mddev_lock(mddev);
2926 if (!rv) {
2927 rv = entry->store(mddev, page, length);
2928 mddev_unlock(mddev);
2930 return rv;
2933 static void md_free(struct kobject *ko)
2935 mddev_t *mddev = container_of(ko, mddev_t, kobj);
2936 kfree(mddev);
2939 static struct sysfs_ops md_sysfs_ops = {
2940 .show = md_attr_show,
2941 .store = md_attr_store,
2943 static struct kobj_type md_ktype = {
2944 .release = md_free,
2945 .sysfs_ops = &md_sysfs_ops,
2946 .default_attrs = md_default_attrs,
2949 int mdp_major = 0;
2951 static struct kobject *md_probe(dev_t dev, int *part, void *data)
2953 static DEFINE_MUTEX(disks_mutex);
2954 mddev_t *mddev = mddev_find(dev);
2955 struct gendisk *disk;
2956 int partitioned = (MAJOR(dev) != MD_MAJOR);
2957 int shift = partitioned ? MdpMinorShift : 0;
2958 int unit = MINOR(dev) >> shift;
2960 if (!mddev)
2961 return NULL;
2963 mutex_lock(&disks_mutex);
2964 if (mddev->gendisk) {
2965 mutex_unlock(&disks_mutex);
2966 mddev_put(mddev);
2967 return NULL;
2969 disk = alloc_disk(1 << shift);
2970 if (!disk) {
2971 mutex_unlock(&disks_mutex);
2972 mddev_put(mddev);
2973 return NULL;
2975 disk->major = MAJOR(dev);
2976 disk->first_minor = unit << shift;
2977 if (partitioned) {
2978 sprintf(disk->disk_name, "md_d%d", unit);
2979 sprintf(disk->devfs_name, "md/d%d", unit);
2980 } else {
2981 sprintf(disk->disk_name, "md%d", unit);
2982 sprintf(disk->devfs_name, "md/%d", unit);
2984 disk->fops = &md_fops;
2985 disk->private_data = mddev;
2986 disk->queue = mddev->queue;
2987 add_disk(disk);
2988 mddev->gendisk = disk;
2989 mutex_unlock(&disks_mutex);
2990 mddev->kobj.parent = &disk->kobj;
2991 mddev->kobj.k_name = NULL;
2992 snprintf(mddev->kobj.name, KOBJ_NAME_LEN, "%s", "md");
2993 mddev->kobj.ktype = &md_ktype;
2994 kobject_register(&mddev->kobj);
2995 return NULL;
2998 static void md_safemode_timeout(unsigned long data)
3000 mddev_t *mddev = (mddev_t *) data;
3002 mddev->safemode = 1;
3003 md_wakeup_thread(mddev->thread);
3006 static int start_dirty_degraded;
3008 static int do_md_run(mddev_t * mddev)
3010 int err;
3011 int chunk_size;
3012 struct list_head *tmp;
3013 mdk_rdev_t *rdev;
3014 struct gendisk *disk;
3015 struct mdk_personality *pers;
3016 char b[BDEVNAME_SIZE];
3018 if (list_empty(&mddev->disks))
3019 /* cannot run an array with no devices.. */
3020 return -EINVAL;
3022 if (mddev->pers)
3023 return -EBUSY;
3026 * Analyze all RAID superblock(s)
3028 if (!mddev->raid_disks)
3029 analyze_sbs(mddev);
3031 chunk_size = mddev->chunk_size;
3033 if (chunk_size) {
3034 if (chunk_size > MAX_CHUNK_SIZE) {
3035 printk(KERN_ERR "too big chunk_size: %d > %d\n",
3036 chunk_size, MAX_CHUNK_SIZE);
3037 return -EINVAL;
3040 * chunk-size has to be a power of 2 and multiples of PAGE_SIZE
3042 if ( (1 << ffz(~chunk_size)) != chunk_size) {
3043 printk(KERN_ERR "chunk_size of %d not valid\n", chunk_size);
3044 return -EINVAL;
3046 if (chunk_size < PAGE_SIZE) {
3047 printk(KERN_ERR "too small chunk_size: %d < %ld\n",
3048 chunk_size, PAGE_SIZE);
3049 return -EINVAL;
3052 /* devices must have minimum size of one chunk */
3053 ITERATE_RDEV(mddev,rdev,tmp) {
3054 if (test_bit(Faulty, &rdev->flags))
3055 continue;
3056 if (rdev->size < chunk_size / 1024) {
3057 printk(KERN_WARNING
3058 "md: Dev %s smaller than chunk_size:"
3059 " %lluk < %dk\n",
3060 bdevname(rdev->bdev,b),
3061 (unsigned long long)rdev->size,
3062 chunk_size / 1024);
3063 return -EINVAL;
3068 #ifdef CONFIG_KMOD
3069 if (mddev->level != LEVEL_NONE)
3070 request_module("md-level-%d", mddev->level);
3071 else if (mddev->clevel[0])
3072 request_module("md-%s", mddev->clevel);
3073 #endif
3076 * Drop all container device buffers, from now on
3077 * the only valid external interface is through the md
3078 * device.
3079 * Also find largest hardsector size
3081 ITERATE_RDEV(mddev,rdev,tmp) {
3082 if (test_bit(Faulty, &rdev->flags))
3083 continue;
3084 sync_blockdev(rdev->bdev);
3085 invalidate_bdev(rdev->bdev, 0);
3088 md_probe(mddev->unit, NULL, NULL);
3089 disk = mddev->gendisk;
3090 if (!disk)
3091 return -ENOMEM;
3093 spin_lock(&pers_lock);
3094 pers = find_pers(mddev->level, mddev->clevel);
3095 if (!pers || !try_module_get(pers->owner)) {
3096 spin_unlock(&pers_lock);
3097 if (mddev->level != LEVEL_NONE)
3098 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
3099 mddev->level);
3100 else
3101 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
3102 mddev->clevel);
3103 return -EINVAL;
3105 mddev->pers = pers;
3106 spin_unlock(&pers_lock);
3107 mddev->level = pers->level;
3108 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3110 if (mddev->reshape_position != MaxSector &&
3111 pers->start_reshape == NULL) {
3112 /* This personality cannot handle reshaping... */
3113 mddev->pers = NULL;
3114 module_put(pers->owner);
3115 return -EINVAL;
3118 mddev->recovery = 0;
3119 mddev->resync_max_sectors = mddev->size << 1; /* may be over-ridden by personality */
3120 mddev->barriers_work = 1;
3121 mddev->ok_start_degraded = start_dirty_degraded;
3123 if (start_readonly)
3124 mddev->ro = 2; /* read-only, but switch on first write */
3126 err = mddev->pers->run(mddev);
3127 if (!err && mddev->pers->sync_request) {
3128 err = bitmap_create(mddev);
3129 if (err) {
3130 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
3131 mdname(mddev), err);
3132 mddev->pers->stop(mddev);
3135 if (err) {
3136 printk(KERN_ERR "md: pers->run() failed ...\n");
3137 module_put(mddev->pers->owner);
3138 mddev->pers = NULL;
3139 bitmap_destroy(mddev);
3140 return err;
3142 if (mddev->pers->sync_request)
3143 sysfs_create_group(&mddev->kobj, &md_redundancy_group);
3144 else if (mddev->ro == 2) /* auto-readonly not meaningful */
3145 mddev->ro = 0;
3147 atomic_set(&mddev->writes_pending,0);
3148 mddev->safemode = 0;
3149 mddev->safemode_timer.function = md_safemode_timeout;
3150 mddev->safemode_timer.data = (unsigned long) mddev;
3151 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
3152 mddev->in_sync = 1;
3154 ITERATE_RDEV(mddev,rdev,tmp)
3155 if (rdev->raid_disk >= 0) {
3156 char nm[20];
3157 sprintf(nm, "rd%d", rdev->raid_disk);
3158 sysfs_create_link(&mddev->kobj, &rdev->kobj, nm);
3161 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3162 md_wakeup_thread(mddev->thread);
3164 if (mddev->sb_dirty)
3165 md_update_sb(mddev);
3167 set_capacity(disk, mddev->array_size<<1);
3169 /* If we call blk_queue_make_request here, it will
3170 * re-initialise max_sectors etc which may have been
3171 * refined inside -> run. So just set the bits we need to set.
3172 * Most initialisation happended when we called
3173 * blk_queue_make_request(..., md_fail_request)
3174 * earlier.
3176 mddev->queue->queuedata = mddev;
3177 mddev->queue->make_request_fn = mddev->pers->make_request;
3179 /* If there is a partially-recovered drive we need to
3180 * start recovery here. If we leave it to md_check_recovery,
3181 * it will remove the drives and not do the right thing
3183 if (mddev->degraded) {
3184 struct list_head *rtmp;
3185 int spares = 0;
3186 ITERATE_RDEV(mddev,rdev,rtmp)
3187 if (rdev->raid_disk >= 0 &&
3188 !test_bit(In_sync, &rdev->flags) &&
3189 !test_bit(Faulty, &rdev->flags))
3190 /* complete an interrupted recovery */
3191 spares++;
3192 if (spares && mddev->pers->sync_request) {
3193 mddev->recovery = 0;
3194 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
3195 mddev->sync_thread = md_register_thread(md_do_sync,
3196 mddev,
3197 "%s_resync");
3198 if (!mddev->sync_thread) {
3199 printk(KERN_ERR "%s: could not start resync"
3200 " thread...\n",
3201 mdname(mddev));
3202 /* leave the spares where they are, it shouldn't hurt */
3203 mddev->recovery = 0;
3204 } else
3205 md_wakeup_thread(mddev->sync_thread);
3209 mddev->changed = 1;
3210 md_new_event(mddev);
3211 return 0;
3214 static int restart_array(mddev_t *mddev)
3216 struct gendisk *disk = mddev->gendisk;
3217 int err;
3220 * Complain if it has no devices
3222 err = -ENXIO;
3223 if (list_empty(&mddev->disks))
3224 goto out;
3226 if (mddev->pers) {
3227 err = -EBUSY;
3228 if (!mddev->ro)
3229 goto out;
3231 mddev->safemode = 0;
3232 mddev->ro = 0;
3233 set_disk_ro(disk, 0);
3235 printk(KERN_INFO "md: %s switched to read-write mode.\n",
3236 mdname(mddev));
3238 * Kick recovery or resync if necessary
3240 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3241 md_wakeup_thread(mddev->thread);
3242 md_wakeup_thread(mddev->sync_thread);
3243 err = 0;
3244 } else
3245 err = -EINVAL;
3247 out:
3248 return err;
3251 /* similar to deny_write_access, but accounts for our holding a reference
3252 * to the file ourselves */
3253 static int deny_bitmap_write_access(struct file * file)
3255 struct inode *inode = file->f_mapping->host;
3257 spin_lock(&inode->i_lock);
3258 if (atomic_read(&inode->i_writecount) > 1) {
3259 spin_unlock(&inode->i_lock);
3260 return -ETXTBSY;
3262 atomic_set(&inode->i_writecount, -1);
3263 spin_unlock(&inode->i_lock);
3265 return 0;
3268 static void restore_bitmap_write_access(struct file *file)
3270 struct inode *inode = file->f_mapping->host;
3272 spin_lock(&inode->i_lock);
3273 atomic_set(&inode->i_writecount, 1);
3274 spin_unlock(&inode->i_lock);
3277 /* mode:
3278 * 0 - completely stop and dis-assemble array
3279 * 1 - switch to readonly
3280 * 2 - stop but do not disassemble array
3282 static int do_md_stop(mddev_t * mddev, int mode)
3284 int err = 0;
3285 struct gendisk *disk = mddev->gendisk;
3287 if (mddev->pers) {
3288 if (atomic_read(&mddev->active)>2) {
3289 printk("md: %s still in use.\n",mdname(mddev));
3290 return -EBUSY;
3293 if (mddev->sync_thread) {
3294 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3295 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3296 md_unregister_thread(mddev->sync_thread);
3297 mddev->sync_thread = NULL;
3300 del_timer_sync(&mddev->safemode_timer);
3302 invalidate_partition(disk, 0);
3304 switch(mode) {
3305 case 1: /* readonly */
3306 err = -ENXIO;
3307 if (mddev->ro==1)
3308 goto out;
3309 mddev->ro = 1;
3310 break;
3311 case 0: /* disassemble */
3312 case 2: /* stop */
3313 bitmap_flush(mddev);
3314 md_super_wait(mddev);
3315 if (mddev->ro)
3316 set_disk_ro(disk, 0);
3317 blk_queue_make_request(mddev->queue, md_fail_request);
3318 mddev->pers->stop(mddev);
3319 if (mddev->pers->sync_request)
3320 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
3322 module_put(mddev->pers->owner);
3323 mddev->pers = NULL;
3324 if (mddev->ro)
3325 mddev->ro = 0;
3327 if (!mddev->in_sync || mddev->sb_dirty) {
3328 /* mark array as shutdown cleanly */
3329 mddev->in_sync = 1;
3330 md_update_sb(mddev);
3332 if (mode == 1)
3333 set_disk_ro(disk, 1);
3334 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3338 * Free resources if final stop
3340 if (mode == 0) {
3341 mdk_rdev_t *rdev;
3342 struct list_head *tmp;
3343 struct gendisk *disk;
3344 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
3346 bitmap_destroy(mddev);
3347 if (mddev->bitmap_file) {
3348 restore_bitmap_write_access(mddev->bitmap_file);
3349 fput(mddev->bitmap_file);
3350 mddev->bitmap_file = NULL;
3352 mddev->bitmap_offset = 0;
3354 ITERATE_RDEV(mddev,rdev,tmp)
3355 if (rdev->raid_disk >= 0) {
3356 char nm[20];
3357 sprintf(nm, "rd%d", rdev->raid_disk);
3358 sysfs_remove_link(&mddev->kobj, nm);
3361 export_array(mddev);
3363 mddev->array_size = 0;
3364 mddev->size = 0;
3365 mddev->raid_disks = 0;
3366 mddev->recovery_cp = 0;
3367 mddev->resync_max = MaxSector;
3369 disk = mddev->gendisk;
3370 if (disk)
3371 set_capacity(disk, 0);
3372 mddev->changed = 1;
3373 } else if (mddev->pers)
3374 printk(KERN_INFO "md: %s switched to read-only mode.\n",
3375 mdname(mddev));
3376 err = 0;
3377 md_new_event(mddev);
3378 out:
3379 return err;
3382 static void autorun_array(mddev_t *mddev)
3384 mdk_rdev_t *rdev;
3385 struct list_head *tmp;
3386 int err;
3388 if (list_empty(&mddev->disks))
3389 return;
3391 printk(KERN_INFO "md: running: ");
3393 ITERATE_RDEV(mddev,rdev,tmp) {
3394 char b[BDEVNAME_SIZE];
3395 printk("<%s>", bdevname(rdev->bdev,b));
3397 printk("\n");
3399 err = do_md_run (mddev);
3400 if (err) {
3401 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
3402 do_md_stop (mddev, 0);
3407 * lets try to run arrays based on all disks that have arrived
3408 * until now. (those are in pending_raid_disks)
3410 * the method: pick the first pending disk, collect all disks with
3411 * the same UUID, remove all from the pending list and put them into
3412 * the 'same_array' list. Then order this list based on superblock
3413 * update time (freshest comes first), kick out 'old' disks and
3414 * compare superblocks. If everything's fine then run it.
3416 * If "unit" is allocated, then bump its reference count
3418 static void autorun_devices(int part)
3420 struct list_head *tmp;
3421 mdk_rdev_t *rdev0, *rdev;
3422 mddev_t *mddev;
3423 char b[BDEVNAME_SIZE];
3425 printk(KERN_INFO "md: autorun ...\n");
3426 while (!list_empty(&pending_raid_disks)) {
3427 dev_t dev;
3428 LIST_HEAD(candidates);
3429 rdev0 = list_entry(pending_raid_disks.next,
3430 mdk_rdev_t, same_set);
3432 printk(KERN_INFO "md: considering %s ...\n",
3433 bdevname(rdev0->bdev,b));
3434 INIT_LIST_HEAD(&candidates);
3435 ITERATE_RDEV_PENDING(rdev,tmp)
3436 if (super_90_load(rdev, rdev0, 0) >= 0) {
3437 printk(KERN_INFO "md: adding %s ...\n",
3438 bdevname(rdev->bdev,b));
3439 list_move(&rdev->same_set, &candidates);
3442 * now we have a set of devices, with all of them having
3443 * mostly sane superblocks. It's time to allocate the
3444 * mddev.
3446 if (rdev0->preferred_minor < 0 || rdev0->preferred_minor >= MAX_MD_DEVS) {
3447 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
3448 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
3449 break;
3451 if (part)
3452 dev = MKDEV(mdp_major,
3453 rdev0->preferred_minor << MdpMinorShift);
3454 else
3455 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
3457 md_probe(dev, NULL, NULL);
3458 mddev = mddev_find(dev);
3459 if (!mddev) {
3460 printk(KERN_ERR
3461 "md: cannot allocate memory for md drive.\n");
3462 break;
3464 if (mddev_lock(mddev))
3465 printk(KERN_WARNING "md: %s locked, cannot run\n",
3466 mdname(mddev));
3467 else if (mddev->raid_disks || mddev->major_version
3468 || !list_empty(&mddev->disks)) {
3469 printk(KERN_WARNING
3470 "md: %s already running, cannot run %s\n",
3471 mdname(mddev), bdevname(rdev0->bdev,b));
3472 mddev_unlock(mddev);
3473 } else {
3474 printk(KERN_INFO "md: created %s\n", mdname(mddev));
3475 ITERATE_RDEV_GENERIC(candidates,rdev,tmp) {
3476 list_del_init(&rdev->same_set);
3477 if (bind_rdev_to_array(rdev, mddev))
3478 export_rdev(rdev);
3480 autorun_array(mddev);
3481 mddev_unlock(mddev);
3483 /* on success, candidates will be empty, on error
3484 * it won't...
3486 ITERATE_RDEV_GENERIC(candidates,rdev,tmp)
3487 export_rdev(rdev);
3488 mddev_put(mddev);
3490 printk(KERN_INFO "md: ... autorun DONE.\n");
3494 * import RAID devices based on one partition
3495 * if possible, the array gets run as well.
3498 static int autostart_array(dev_t startdev)
3500 char b[BDEVNAME_SIZE];
3501 int err = -EINVAL, i;
3502 mdp_super_t *sb = NULL;
3503 mdk_rdev_t *start_rdev = NULL, *rdev;
3505 start_rdev = md_import_device(startdev, 0, 0);
3506 if (IS_ERR(start_rdev))
3507 return err;
3510 /* NOTE: this can only work for 0.90.0 superblocks */
3511 sb = (mdp_super_t*)page_address(start_rdev->sb_page);
3512 if (sb->major_version != 0 ||
3513 sb->minor_version != 90 ) {
3514 printk(KERN_WARNING "md: can only autostart 0.90.0 arrays\n");
3515 export_rdev(start_rdev);
3516 return err;
3519 if (test_bit(Faulty, &start_rdev->flags)) {
3520 printk(KERN_WARNING
3521 "md: can not autostart based on faulty %s!\n",
3522 bdevname(start_rdev->bdev,b));
3523 export_rdev(start_rdev);
3524 return err;
3526 list_add(&start_rdev->same_set, &pending_raid_disks);
3528 for (i = 0; i < MD_SB_DISKS; i++) {
3529 mdp_disk_t *desc = sb->disks + i;
3530 dev_t dev = MKDEV(desc->major, desc->minor);
3532 if (!dev)
3533 continue;
3534 if (dev == startdev)
3535 continue;
3536 if (MAJOR(dev) != desc->major || MINOR(dev) != desc->minor)
3537 continue;
3538 rdev = md_import_device(dev, 0, 0);
3539 if (IS_ERR(rdev))
3540 continue;
3542 list_add(&rdev->same_set, &pending_raid_disks);
3546 * possibly return codes
3548 autorun_devices(0);
3549 return 0;
3554 static int get_version(void __user * arg)
3556 mdu_version_t ver;
3558 ver.major = MD_MAJOR_VERSION;
3559 ver.minor = MD_MINOR_VERSION;
3560 ver.patchlevel = MD_PATCHLEVEL_VERSION;
3562 if (copy_to_user(arg, &ver, sizeof(ver)))
3563 return -EFAULT;
3565 return 0;
3568 static int get_array_info(mddev_t * mddev, void __user * arg)
3570 mdu_array_info_t info;
3571 int nr,working,active,failed,spare;
3572 mdk_rdev_t *rdev;
3573 struct list_head *tmp;
3575 nr=working=active=failed=spare=0;
3576 ITERATE_RDEV(mddev,rdev,tmp) {
3577 nr++;
3578 if (test_bit(Faulty, &rdev->flags))
3579 failed++;
3580 else {
3581 working++;
3582 if (test_bit(In_sync, &rdev->flags))
3583 active++;
3584 else
3585 spare++;
3589 info.major_version = mddev->major_version;
3590 info.minor_version = mddev->minor_version;
3591 info.patch_version = MD_PATCHLEVEL_VERSION;
3592 info.ctime = mddev->ctime;
3593 info.level = mddev->level;
3594 info.size = mddev->size;
3595 if (info.size != mddev->size) /* overflow */
3596 info.size = -1;
3597 info.nr_disks = nr;
3598 info.raid_disks = mddev->raid_disks;
3599 info.md_minor = mddev->md_minor;
3600 info.not_persistent= !mddev->persistent;
3602 info.utime = mddev->utime;
3603 info.state = 0;
3604 if (mddev->in_sync)
3605 info.state = (1<<MD_SB_CLEAN);
3606 if (mddev->bitmap && mddev->bitmap_offset)
3607 info.state = (1<<MD_SB_BITMAP_PRESENT);
3608 info.active_disks = active;
3609 info.working_disks = working;
3610 info.failed_disks = failed;
3611 info.spare_disks = spare;
3613 info.layout = mddev->layout;
3614 info.chunk_size = mddev->chunk_size;
3616 if (copy_to_user(arg, &info, sizeof(info)))
3617 return -EFAULT;
3619 return 0;
3622 static int get_bitmap_file(mddev_t * mddev, void __user * arg)
3624 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
3625 char *ptr, *buf = NULL;
3626 int err = -ENOMEM;
3628 file = kmalloc(sizeof(*file), GFP_KERNEL);
3629 if (!file)
3630 goto out;
3632 /* bitmap disabled, zero the first byte and copy out */
3633 if (!mddev->bitmap || !mddev->bitmap->file) {
3634 file->pathname[0] = '\0';
3635 goto copy_out;
3638 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
3639 if (!buf)
3640 goto out;
3642 ptr = file_path(mddev->bitmap->file, buf, sizeof(file->pathname));
3643 if (!ptr)
3644 goto out;
3646 strcpy(file->pathname, ptr);
3648 copy_out:
3649 err = 0;
3650 if (copy_to_user(arg, file, sizeof(*file)))
3651 err = -EFAULT;
3652 out:
3653 kfree(buf);
3654 kfree(file);
3655 return err;
3658 static int get_disk_info(mddev_t * mddev, void __user * arg)
3660 mdu_disk_info_t info;
3661 unsigned int nr;
3662 mdk_rdev_t *rdev;
3664 if (copy_from_user(&info, arg, sizeof(info)))
3665 return -EFAULT;
3667 nr = info.number;
3669 rdev = find_rdev_nr(mddev, nr);
3670 if (rdev) {
3671 info.major = MAJOR(rdev->bdev->bd_dev);
3672 info.minor = MINOR(rdev->bdev->bd_dev);
3673 info.raid_disk = rdev->raid_disk;
3674 info.state = 0;
3675 if (test_bit(Faulty, &rdev->flags))
3676 info.state |= (1<<MD_DISK_FAULTY);
3677 else if (test_bit(In_sync, &rdev->flags)) {
3678 info.state |= (1<<MD_DISK_ACTIVE);
3679 info.state |= (1<<MD_DISK_SYNC);
3681 if (test_bit(WriteMostly, &rdev->flags))
3682 info.state |= (1<<MD_DISK_WRITEMOSTLY);
3683 } else {
3684 info.major = info.minor = 0;
3685 info.raid_disk = -1;
3686 info.state = (1<<MD_DISK_REMOVED);
3689 if (copy_to_user(arg, &info, sizeof(info)))
3690 return -EFAULT;
3692 return 0;
3695 static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
3697 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
3698 mdk_rdev_t *rdev;
3699 dev_t dev = MKDEV(info->major,info->minor);
3701 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
3702 return -EOVERFLOW;
3704 if (!mddev->raid_disks) {
3705 int err;
3706 /* expecting a device which has a superblock */
3707 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
3708 if (IS_ERR(rdev)) {
3709 printk(KERN_WARNING
3710 "md: md_import_device returned %ld\n",
3711 PTR_ERR(rdev));
3712 return PTR_ERR(rdev);
3714 if (!list_empty(&mddev->disks)) {
3715 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
3716 mdk_rdev_t, same_set);
3717 int err = super_types[mddev->major_version]
3718 .load_super(rdev, rdev0, mddev->minor_version);
3719 if (err < 0) {
3720 printk(KERN_WARNING
3721 "md: %s has different UUID to %s\n",
3722 bdevname(rdev->bdev,b),
3723 bdevname(rdev0->bdev,b2));
3724 export_rdev(rdev);
3725 return -EINVAL;
3728 err = bind_rdev_to_array(rdev, mddev);
3729 if (err)
3730 export_rdev(rdev);
3731 return err;
3735 * add_new_disk can be used once the array is assembled
3736 * to add "hot spares". They must already have a superblock
3737 * written
3739 if (mddev->pers) {
3740 int err;
3741 if (!mddev->pers->hot_add_disk) {
3742 printk(KERN_WARNING
3743 "%s: personality does not support diskops!\n",
3744 mdname(mddev));
3745 return -EINVAL;
3747 if (mddev->persistent)
3748 rdev = md_import_device(dev, mddev->major_version,
3749 mddev->minor_version);
3750 else
3751 rdev = md_import_device(dev, -1, -1);
3752 if (IS_ERR(rdev)) {
3753 printk(KERN_WARNING
3754 "md: md_import_device returned %ld\n",
3755 PTR_ERR(rdev));
3756 return PTR_ERR(rdev);
3758 /* set save_raid_disk if appropriate */
3759 if (!mddev->persistent) {
3760 if (info->state & (1<<MD_DISK_SYNC) &&
3761 info->raid_disk < mddev->raid_disks)
3762 rdev->raid_disk = info->raid_disk;
3763 else
3764 rdev->raid_disk = -1;
3765 } else
3766 super_types[mddev->major_version].
3767 validate_super(mddev, rdev);
3768 rdev->saved_raid_disk = rdev->raid_disk;
3770 clear_bit(In_sync, &rdev->flags); /* just to be sure */
3771 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
3772 set_bit(WriteMostly, &rdev->flags);
3774 rdev->raid_disk = -1;
3775 err = bind_rdev_to_array(rdev, mddev);
3776 if (!err && !mddev->pers->hot_remove_disk) {
3777 /* If there is hot_add_disk but no hot_remove_disk
3778 * then added disks for geometry changes,
3779 * and should be added immediately.
3781 super_types[mddev->major_version].
3782 validate_super(mddev, rdev);
3783 err = mddev->pers->hot_add_disk(mddev, rdev);
3784 if (err)
3785 unbind_rdev_from_array(rdev);
3787 if (err)
3788 export_rdev(rdev);
3790 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3791 md_wakeup_thread(mddev->thread);
3792 return err;
3795 /* otherwise, add_new_disk is only allowed
3796 * for major_version==0 superblocks
3798 if (mddev->major_version != 0) {
3799 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
3800 mdname(mddev));
3801 return -EINVAL;
3804 if (!(info->state & (1<<MD_DISK_FAULTY))) {
3805 int err;
3806 rdev = md_import_device (dev, -1, 0);
3807 if (IS_ERR(rdev)) {
3808 printk(KERN_WARNING
3809 "md: error, md_import_device() returned %ld\n",
3810 PTR_ERR(rdev));
3811 return PTR_ERR(rdev);
3813 rdev->desc_nr = info->number;
3814 if (info->raid_disk < mddev->raid_disks)
3815 rdev->raid_disk = info->raid_disk;
3816 else
3817 rdev->raid_disk = -1;
3819 rdev->flags = 0;
3821 if (rdev->raid_disk < mddev->raid_disks)
3822 if (info->state & (1<<MD_DISK_SYNC))
3823 set_bit(In_sync, &rdev->flags);
3825 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
3826 set_bit(WriteMostly, &rdev->flags);
3828 if (!mddev->persistent) {
3829 printk(KERN_INFO "md: nonpersistent superblock ...\n");
3830 rdev->sb_offset = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
3831 } else
3832 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
3833 rdev->size = calc_dev_size(rdev, mddev->chunk_size);
3835 err = bind_rdev_to_array(rdev, mddev);
3836 if (err) {
3837 export_rdev(rdev);
3838 return err;
3842 return 0;
3845 static int hot_remove_disk(mddev_t * mddev, dev_t dev)
3847 char b[BDEVNAME_SIZE];
3848 mdk_rdev_t *rdev;
3850 if (!mddev->pers)
3851 return -ENODEV;
3853 rdev = find_rdev(mddev, dev);
3854 if (!rdev)
3855 return -ENXIO;
3857 if (rdev->raid_disk >= 0)
3858 goto busy;
3860 kick_rdev_from_array(rdev);
3861 md_update_sb(mddev);
3862 md_new_event(mddev);
3864 return 0;
3865 busy:
3866 printk(KERN_WARNING "md: cannot remove active disk %s from %s ... \n",
3867 bdevname(rdev->bdev,b), mdname(mddev));
3868 return -EBUSY;
3871 static int hot_add_disk(mddev_t * mddev, dev_t dev)
3873 char b[BDEVNAME_SIZE];
3874 int err;
3875 unsigned int size;
3876 mdk_rdev_t *rdev;
3878 if (!mddev->pers)
3879 return -ENODEV;
3881 if (mddev->major_version != 0) {
3882 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
3883 " version-0 superblocks.\n",
3884 mdname(mddev));
3885 return -EINVAL;
3887 if (!mddev->pers->hot_add_disk) {
3888 printk(KERN_WARNING
3889 "%s: personality does not support diskops!\n",
3890 mdname(mddev));
3891 return -EINVAL;
3894 rdev = md_import_device (dev, -1, 0);
3895 if (IS_ERR(rdev)) {
3896 printk(KERN_WARNING
3897 "md: error, md_import_device() returned %ld\n",
3898 PTR_ERR(rdev));
3899 return -EINVAL;
3902 if (mddev->persistent)
3903 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
3904 else
3905 rdev->sb_offset =
3906 rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
3908 size = calc_dev_size(rdev, mddev->chunk_size);
3909 rdev->size = size;
3911 if (test_bit(Faulty, &rdev->flags)) {
3912 printk(KERN_WARNING
3913 "md: can not hot-add faulty %s disk to %s!\n",
3914 bdevname(rdev->bdev,b), mdname(mddev));
3915 err = -EINVAL;
3916 goto abort_export;
3918 clear_bit(In_sync, &rdev->flags);
3919 rdev->desc_nr = -1;
3920 err = bind_rdev_to_array(rdev, mddev);
3921 if (err)
3922 goto abort_export;
3925 * The rest should better be atomic, we can have disk failures
3926 * noticed in interrupt contexts ...
3929 if (rdev->desc_nr == mddev->max_disks) {
3930 printk(KERN_WARNING "%s: can not hot-add to full array!\n",
3931 mdname(mddev));
3932 err = -EBUSY;
3933 goto abort_unbind_export;
3936 rdev->raid_disk = -1;
3938 md_update_sb(mddev);
3941 * Kick recovery, maybe this spare has to be added to the
3942 * array immediately.
3944 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3945 md_wakeup_thread(mddev->thread);
3946 md_new_event(mddev);
3947 return 0;
3949 abort_unbind_export:
3950 unbind_rdev_from_array(rdev);
3952 abort_export:
3953 export_rdev(rdev);
3954 return err;
3957 static int set_bitmap_file(mddev_t *mddev, int fd)
3959 int err;
3961 if (mddev->pers) {
3962 if (!mddev->pers->quiesce)
3963 return -EBUSY;
3964 if (mddev->recovery || mddev->sync_thread)
3965 return -EBUSY;
3966 /* we should be able to change the bitmap.. */
3970 if (fd >= 0) {
3971 if (mddev->bitmap)
3972 return -EEXIST; /* cannot add when bitmap is present */
3973 mddev->bitmap_file = fget(fd);
3975 if (mddev->bitmap_file == NULL) {
3976 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
3977 mdname(mddev));
3978 return -EBADF;
3981 err = deny_bitmap_write_access(mddev->bitmap_file);
3982 if (err) {
3983 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
3984 mdname(mddev));
3985 fput(mddev->bitmap_file);
3986 mddev->bitmap_file = NULL;
3987 return err;
3989 mddev->bitmap_offset = 0; /* file overrides offset */
3990 } else if (mddev->bitmap == NULL)
3991 return -ENOENT; /* cannot remove what isn't there */
3992 err = 0;
3993 if (mddev->pers) {
3994 mddev->pers->quiesce(mddev, 1);
3995 if (fd >= 0)
3996 err = bitmap_create(mddev);
3997 if (fd < 0 || err) {
3998 bitmap_destroy(mddev);
3999 fd = -1; /* make sure to put the file */
4001 mddev->pers->quiesce(mddev, 0);
4003 if (fd < 0) {
4004 if (mddev->bitmap_file) {
4005 restore_bitmap_write_access(mddev->bitmap_file);
4006 fput(mddev->bitmap_file);
4008 mddev->bitmap_file = NULL;
4011 return err;
4015 * set_array_info is used two different ways
4016 * The original usage is when creating a new array.
4017 * In this usage, raid_disks is > 0 and it together with
4018 * level, size, not_persistent,layout,chunksize determine the
4019 * shape of the array.
4020 * This will always create an array with a type-0.90.0 superblock.
4021 * The newer usage is when assembling an array.
4022 * In this case raid_disks will be 0, and the major_version field is
4023 * use to determine which style super-blocks are to be found on the devices.
4024 * The minor and patch _version numbers are also kept incase the
4025 * super_block handler wishes to interpret them.
4027 static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
4030 if (info->raid_disks == 0) {
4031 /* just setting version number for superblock loading */
4032 if (info->major_version < 0 ||
4033 info->major_version >= sizeof(super_types)/sizeof(super_types[0]) ||
4034 super_types[info->major_version].name == NULL) {
4035 /* maybe try to auto-load a module? */
4036 printk(KERN_INFO
4037 "md: superblock version %d not known\n",
4038 info->major_version);
4039 return -EINVAL;
4041 mddev->major_version = info->major_version;
4042 mddev->minor_version = info->minor_version;
4043 mddev->patch_version = info->patch_version;
4044 return 0;
4046 mddev->major_version = MD_MAJOR_VERSION;
4047 mddev->minor_version = MD_MINOR_VERSION;
4048 mddev->patch_version = MD_PATCHLEVEL_VERSION;
4049 mddev->ctime = get_seconds();
4051 mddev->level = info->level;
4052 mddev->clevel[0] = 0;
4053 mddev->size = info->size;
4054 mddev->raid_disks = info->raid_disks;
4055 /* don't set md_minor, it is determined by which /dev/md* was
4056 * openned
4058 if (info->state & (1<<MD_SB_CLEAN))
4059 mddev->recovery_cp = MaxSector;
4060 else
4061 mddev->recovery_cp = 0;
4062 mddev->persistent = ! info->not_persistent;
4063 mddev->external = 0;
4065 mddev->layout = info->layout;
4066 mddev->chunk_size = info->chunk_size;
4068 mddev->max_disks = MD_SB_DISKS;
4070 if (mddev->persistent)
4071 mddev->sb_dirty = 1;
4073 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
4074 mddev->bitmap_offset = 0;
4076 mddev->reshape_position = MaxSector;
4079 * Generate a 128 bit UUID
4081 get_random_bytes(mddev->uuid, 16);
4083 mddev->new_level = mddev->level;
4084 mddev->new_chunk = mddev->chunk_size;
4085 mddev->new_layout = mddev->layout;
4086 mddev->delta_disks = 0;
4088 return 0;
4091 static int update_size(mddev_t *mddev, unsigned long size)
4093 mdk_rdev_t * rdev;
4094 int rv;
4095 struct list_head *tmp;
4096 int fit = (size == 0);
4098 if (mddev->pers->resize == NULL)
4099 return -EINVAL;
4100 /* The "size" is the amount of each device that is used.
4101 * This can only make sense for arrays with redundancy.
4102 * linear and raid0 always use whatever space is available
4103 * We can only consider changing the size if no resync
4104 * or reconstruction is happening, and if the new size
4105 * is acceptable. It must fit before the sb_offset or,
4106 * if that is <data_offset, it must fit before the
4107 * size of each device.
4108 * If size is zero, we find the largest size that fits.
4110 if (mddev->sync_thread)
4111 return -EBUSY;
4112 ITERATE_RDEV(mddev,rdev,tmp) {
4113 sector_t avail;
4114 if (rdev->sb_offset > rdev->data_offset)
4115 avail = (rdev->sb_offset*2) - rdev->data_offset;
4116 else
4117 avail = get_capacity(rdev->bdev->bd_disk)
4118 - rdev->data_offset;
4119 if (fit && (size == 0 || size > avail/2))
4120 size = avail/2;
4121 if (avail < ((sector_t)size << 1))
4122 return -ENOSPC;
4124 rv = mddev->pers->resize(mddev, (sector_t)size *2);
4125 if (!rv) {
4126 struct block_device *bdev;
4128 bdev = bdget_disk(mddev->gendisk, 0);
4129 if (bdev) {
4130 mutex_lock(&bdev->bd_inode->i_mutex);
4131 i_size_write(bdev->bd_inode, (loff_t)mddev->array_size << 10);
4132 mutex_unlock(&bdev->bd_inode->i_mutex);
4133 bdput(bdev);
4136 return rv;
4139 static int update_raid_disks(mddev_t *mddev, int raid_disks)
4141 int rv;
4142 /* change the number of raid disks */
4143 if (mddev->pers->check_reshape == NULL)
4144 return -EINVAL;
4145 if (raid_disks <= 0 ||
4146 raid_disks >= mddev->max_disks)
4147 return -EINVAL;
4148 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
4149 return -EBUSY;
4150 mddev->delta_disks = raid_disks - mddev->raid_disks;
4152 rv = mddev->pers->check_reshape(mddev);
4153 return rv;
4158 * update_array_info is used to change the configuration of an
4159 * on-line array.
4160 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
4161 * fields in the info are checked against the array.
4162 * Any differences that cannot be handled will cause an error.
4163 * Normally, only one change can be managed at a time.
4165 static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
4167 int rv = 0;
4168 int cnt = 0;
4169 int state = 0;
4171 /* calculate expected state,ignoring low bits */
4172 if (mddev->bitmap && mddev->bitmap_offset)
4173 state |= (1 << MD_SB_BITMAP_PRESENT);
4175 if (mddev->major_version != info->major_version ||
4176 mddev->minor_version != info->minor_version ||
4177 /* mddev->patch_version != info->patch_version || */
4178 mddev->ctime != info->ctime ||
4179 mddev->level != info->level ||
4180 /* mddev->layout != info->layout || */
4181 !mddev->persistent != info->not_persistent||
4182 mddev->chunk_size != info->chunk_size ||
4183 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
4184 ((state^info->state) & 0xfffffe00)
4186 return -EINVAL;
4187 /* Check there is only one change */
4188 if (info->size >= 0 && mddev->size != info->size) cnt++;
4189 if (mddev->raid_disks != info->raid_disks) cnt++;
4190 if (mddev->layout != info->layout) cnt++;
4191 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) cnt++;
4192 if (cnt == 0) return 0;
4193 if (cnt > 1) return -EINVAL;
4195 if (mddev->layout != info->layout) {
4196 /* Change layout
4197 * we don't need to do anything at the md level, the
4198 * personality will take care of it all.
4200 if (mddev->pers->reconfig == NULL)
4201 return -EINVAL;
4202 else
4203 return mddev->pers->reconfig(mddev, info->layout, -1);
4205 if (info->size >= 0 && mddev->size != info->size)
4206 rv = update_size(mddev, info->size);
4208 if (mddev->raid_disks != info->raid_disks)
4209 rv = update_raid_disks(mddev, info->raid_disks);
4211 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
4212 if (mddev->pers->quiesce == NULL)
4213 return -EINVAL;
4214 if (mddev->recovery || mddev->sync_thread)
4215 return -EBUSY;
4216 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
4217 /* add the bitmap */
4218 if (mddev->bitmap)
4219 return -EEXIST;
4220 if (mddev->default_bitmap_offset == 0)
4221 return -EINVAL;
4222 mddev->bitmap_offset = mddev->default_bitmap_offset;
4223 mddev->pers->quiesce(mddev, 1);
4224 rv = bitmap_create(mddev);
4225 if (rv)
4226 bitmap_destroy(mddev);
4227 mddev->pers->quiesce(mddev, 0);
4228 } else {
4229 /* remove the bitmap */
4230 if (!mddev->bitmap)
4231 return -ENOENT;
4232 if (mddev->bitmap->file)
4233 return -EINVAL;
4234 mddev->pers->quiesce(mddev, 1);
4235 bitmap_destroy(mddev);
4236 mddev->pers->quiesce(mddev, 0);
4237 mddev->bitmap_offset = 0;
4240 md_update_sb(mddev);
4241 return rv;
4244 static int set_disk_faulty(mddev_t *mddev, dev_t dev)
4246 mdk_rdev_t *rdev;
4248 if (mddev->pers == NULL)
4249 return -ENODEV;
4251 rdev = find_rdev(mddev, dev);
4252 if (!rdev)
4253 return -ENODEV;
4255 md_error(mddev, rdev);
4256 return 0;
4259 static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
4261 mddev_t *mddev = bdev->bd_disk->private_data;
4263 geo->heads = 2;
4264 geo->sectors = 4;
4265 geo->cylinders = get_capacity(mddev->gendisk) / 8;
4266 return 0;
4269 static int md_ioctl(struct inode *inode, struct file *file,
4270 unsigned int cmd, unsigned long arg)
4272 int err = 0;
4273 void __user *argp = (void __user *)arg;
4274 mddev_t *mddev = NULL;
4276 if (!capable(CAP_SYS_ADMIN))
4277 return -EACCES;
4280 * Commands dealing with the RAID driver but not any
4281 * particular array:
4283 switch (cmd)
4285 case RAID_VERSION:
4286 err = get_version(argp);
4287 goto done;
4289 case PRINT_RAID_DEBUG:
4290 err = 0;
4291 md_print_devices();
4292 goto done;
4294 #ifndef MODULE
4295 case RAID_AUTORUN:
4296 err = 0;
4297 autostart_arrays(arg);
4298 goto done;
4299 #endif
4300 default:;
4304 * Commands creating/starting a new array:
4307 mddev = inode->i_bdev->bd_disk->private_data;
4309 if (!mddev) {
4310 BUG();
4311 goto abort;
4315 if (cmd == START_ARRAY) {
4316 /* START_ARRAY doesn't need to lock the array as autostart_array
4317 * does the locking, and it could even be a different array
4319 static int cnt = 3;
4320 if (cnt > 0 ) {
4321 printk(KERN_WARNING
4322 "md: %s(pid %d) used deprecated START_ARRAY ioctl. "
4323 "This will not be supported beyond July 2006\n",
4324 current->comm, current->pid);
4325 cnt--;
4327 err = autostart_array(new_decode_dev(arg));
4328 if (err) {
4329 printk(KERN_WARNING "md: autostart failed!\n");
4330 goto abort;
4332 goto done;
4335 err = mddev_lock(mddev);
4336 if (err) {
4337 printk(KERN_INFO
4338 "md: ioctl lock interrupted, reason %d, cmd %d\n",
4339 err, cmd);
4340 goto abort;
4343 switch (cmd)
4345 case SET_ARRAY_INFO:
4347 mdu_array_info_t info;
4348 if (!arg)
4349 memset(&info, 0, sizeof(info));
4350 else if (copy_from_user(&info, argp, sizeof(info))) {
4351 err = -EFAULT;
4352 goto abort_unlock;
4354 if (mddev->pers) {
4355 err = update_array_info(mddev, &info);
4356 if (err) {
4357 printk(KERN_WARNING "md: couldn't update"
4358 " array info. %d\n", err);
4359 goto abort_unlock;
4361 goto done_unlock;
4363 if (!list_empty(&mddev->disks)) {
4364 printk(KERN_WARNING
4365 "md: array %s already has disks!\n",
4366 mdname(mddev));
4367 err = -EBUSY;
4368 goto abort_unlock;
4370 if (mddev->raid_disks) {
4371 printk(KERN_WARNING
4372 "md: array %s already initialised!\n",
4373 mdname(mddev));
4374 err = -EBUSY;
4375 goto abort_unlock;
4377 err = set_array_info(mddev, &info);
4378 if (err) {
4379 printk(KERN_WARNING "md: couldn't set"
4380 " array info. %d\n", err);
4381 goto abort_unlock;
4384 goto done_unlock;
4386 default:;
4390 * Commands querying/configuring an existing array:
4392 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
4393 * RUN_ARRAY, and SET_BITMAP_FILE are allowed */
4394 if (!mddev->raid_disks && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
4395 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE) {
4396 err = -ENODEV;
4397 goto abort_unlock;
4401 * Commands even a read-only array can execute:
4403 switch (cmd)
4405 case GET_ARRAY_INFO:
4406 err = get_array_info(mddev, argp);
4407 goto done_unlock;
4409 case GET_BITMAP_FILE:
4410 err = get_bitmap_file(mddev, argp);
4411 goto done_unlock;
4413 case GET_DISK_INFO:
4414 err = get_disk_info(mddev, argp);
4415 goto done_unlock;
4417 case RESTART_ARRAY_RW:
4418 err = restart_array(mddev);
4419 goto done_unlock;
4421 case STOP_ARRAY:
4422 err = do_md_stop (mddev, 0);
4423 goto done_unlock;
4425 case STOP_ARRAY_RO:
4426 err = do_md_stop (mddev, 1);
4427 goto done_unlock;
4430 * We have a problem here : there is no easy way to give a CHS
4431 * virtual geometry. We currently pretend that we have a 2 heads
4432 * 4 sectors (with a BIG number of cylinders...). This drives
4433 * dosfs just mad... ;-)
4438 * The remaining ioctls are changing the state of the
4439 * superblock, so we do not allow them on read-only arrays.
4440 * However non-MD ioctls (e.g. get-size) will still come through
4441 * here and hit the 'default' below, so only disallow
4442 * 'md' ioctls, and switch to rw mode if started auto-readonly.
4444 if (_IOC_TYPE(cmd) == MD_MAJOR &&
4445 mddev->ro && mddev->pers) {
4446 if (mddev->ro == 2) {
4447 mddev->ro = 0;
4448 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4449 md_wakeup_thread(mddev->thread);
4451 } else {
4452 err = -EROFS;
4453 goto abort_unlock;
4457 switch (cmd)
4459 case ADD_NEW_DISK:
4461 mdu_disk_info_t info;
4462 if (copy_from_user(&info, argp, sizeof(info)))
4463 err = -EFAULT;
4464 else
4465 err = add_new_disk(mddev, &info);
4466 goto done_unlock;
4469 case HOT_REMOVE_DISK:
4470 err = hot_remove_disk(mddev, new_decode_dev(arg));
4471 goto done_unlock;
4473 case HOT_ADD_DISK:
4474 err = hot_add_disk(mddev, new_decode_dev(arg));
4475 goto done_unlock;
4477 case SET_DISK_FAULTY:
4478 err = set_disk_faulty(mddev, new_decode_dev(arg));
4479 goto done_unlock;
4481 case RUN_ARRAY:
4482 err = do_md_run (mddev);
4483 goto done_unlock;
4485 case SET_BITMAP_FILE:
4486 err = set_bitmap_file(mddev, (int)arg);
4487 goto done_unlock;
4489 default:
4490 err = -EINVAL;
4491 goto abort_unlock;
4494 done_unlock:
4495 abort_unlock:
4496 mddev_unlock(mddev);
4498 return err;
4499 done:
4500 if (err)
4501 MD_BUG();
4502 abort:
4503 return err;
4506 static int md_open(struct inode *inode, struct file *file)
4509 * Succeed if we can lock the mddev, which confirms that
4510 * it isn't being stopped right now.
4512 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
4513 int err;
4515 if ((err = mddev_lock(mddev)))
4516 goto out;
4518 err = 0;
4519 mddev_get(mddev);
4520 mddev_unlock(mddev);
4522 check_disk_change(inode->i_bdev);
4523 out:
4524 return err;
4527 static int md_release(struct inode *inode, struct file * file)
4529 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
4531 if (!mddev)
4532 BUG();
4533 mddev_put(mddev);
4535 return 0;
4538 static int md_media_changed(struct gendisk *disk)
4540 mddev_t *mddev = disk->private_data;
4542 return mddev->changed;
4545 static int md_revalidate(struct gendisk *disk)
4547 mddev_t *mddev = disk->private_data;
4549 mddev->changed = 0;
4550 return 0;
4552 static struct block_device_operations md_fops =
4554 .owner = THIS_MODULE,
4555 .open = md_open,
4556 .release = md_release,
4557 .ioctl = md_ioctl,
4558 .getgeo = md_getgeo,
4559 .media_changed = md_media_changed,
4560 .revalidate_disk= md_revalidate,
4563 static int md_thread(void * arg)
4565 mdk_thread_t *thread = arg;
4568 * md_thread is a 'system-thread', it's priority should be very
4569 * high. We avoid resource deadlocks individually in each
4570 * raid personality. (RAID5 does preallocation) We also use RR and
4571 * the very same RT priority as kswapd, thus we will never get
4572 * into a priority inversion deadlock.
4574 * we definitely have to have equal or higher priority than
4575 * bdflush, otherwise bdflush will deadlock if there are too
4576 * many dirty RAID5 blocks.
4579 allow_signal(SIGKILL);
4580 while (!kthread_should_stop()) {
4582 /* We need to wait INTERRUPTIBLE so that
4583 * we don't add to the load-average.
4584 * That means we need to be sure no signals are
4585 * pending
4587 if (signal_pending(current))
4588 flush_signals(current);
4590 wait_event_interruptible_timeout
4591 (thread->wqueue,
4592 test_bit(THREAD_WAKEUP, &thread->flags)
4593 || kthread_should_stop(),
4594 thread->timeout);
4595 try_to_freeze();
4597 clear_bit(THREAD_WAKEUP, &thread->flags);
4599 thread->run(thread->mddev);
4602 return 0;
4605 void md_wakeup_thread(mdk_thread_t *thread)
4607 if (thread) {
4608 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
4609 set_bit(THREAD_WAKEUP, &thread->flags);
4610 wake_up(&thread->wqueue);
4614 mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
4615 const char *name)
4617 mdk_thread_t *thread;
4619 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
4620 if (!thread)
4621 return NULL;
4623 init_waitqueue_head(&thread->wqueue);
4625 thread->run = run;
4626 thread->mddev = mddev;
4627 thread->timeout = MAX_SCHEDULE_TIMEOUT;
4628 thread->tsk = kthread_run(md_thread, thread, name, mdname(thread->mddev));
4629 if (IS_ERR(thread->tsk)) {
4630 kfree(thread);
4631 return NULL;
4633 return thread;
4636 void md_unregister_thread(mdk_thread_t *thread)
4638 dprintk("interrupting MD-thread pid %d\n", thread->tsk->pid);
4640 kthread_stop(thread->tsk);
4641 kfree(thread);
4644 void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
4646 if (!mddev) {
4647 MD_BUG();
4648 return;
4651 if (!rdev || test_bit(Faulty, &rdev->flags))
4652 return;
4654 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
4655 mdname(mddev),
4656 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
4657 __builtin_return_address(0),__builtin_return_address(1),
4658 __builtin_return_address(2),__builtin_return_address(3));
4660 if (!mddev->pers->error_handler)
4661 return;
4662 mddev->pers->error_handler(mddev,rdev);
4663 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4664 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4665 md_wakeup_thread(mddev->thread);
4666 md_new_event_inintr(mddev);
4669 /* seq_file implementation /proc/mdstat */
4671 static void status_unused(struct seq_file *seq)
4673 int i = 0;
4674 mdk_rdev_t *rdev;
4675 struct list_head *tmp;
4677 seq_printf(seq, "unused devices: ");
4679 ITERATE_RDEV_PENDING(rdev,tmp) {
4680 char b[BDEVNAME_SIZE];
4681 i++;
4682 seq_printf(seq, "%s ",
4683 bdevname(rdev->bdev,b));
4685 if (!i)
4686 seq_printf(seq, "<none>");
4688 seq_printf(seq, "\n");
4692 static void status_resync(struct seq_file *seq, mddev_t * mddev)
4694 sector_t max_blocks, resync, res;
4695 unsigned long dt, db, rt;
4696 int scale;
4697 unsigned int per_milli;
4699 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active))/2;
4701 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
4702 max_blocks = mddev->resync_max_sectors >> 1;
4703 else
4704 max_blocks = mddev->size;
4707 * Should not happen.
4709 if (!max_blocks) {
4710 MD_BUG();
4711 return;
4713 /* Pick 'scale' such that (resync>>scale)*1000 will fit
4714 * in a sector_t, and (max_blocks>>scale) will fit in a
4715 * u32, as those are the requirements for sector_div.
4716 * Thus 'scale' must be at least 10
4718 scale = 10;
4719 if (sizeof(sector_t) > sizeof(unsigned long)) {
4720 while ( max_blocks/2 > (1ULL<<(scale+32)))
4721 scale++;
4723 res = (resync>>scale)*1000;
4724 sector_div(res, (u32)((max_blocks>>scale)+1));
4726 per_milli = res;
4728 int i, x = per_milli/50, y = 20-x;
4729 seq_printf(seq, "[");
4730 for (i = 0; i < x; i++)
4731 seq_printf(seq, "=");
4732 seq_printf(seq, ">");
4733 for (i = 0; i < y; i++)
4734 seq_printf(seq, ".");
4735 seq_printf(seq, "] ");
4737 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
4738 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
4739 "reshape" :
4740 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
4741 "resync" : "recovery")),
4742 per_milli/10, per_milli % 10,
4743 (unsigned long long) resync,
4744 (unsigned long long) max_blocks);
4747 * We do not want to overflow, so the order of operands and
4748 * the * 100 / 100 trick are important. We do a +1 to be
4749 * safe against division by zero. We only estimate anyway.
4751 * dt: time from mark until now
4752 * db: blocks written from mark until now
4753 * rt: remaining time
4755 dt = ((jiffies - mddev->resync_mark) / HZ);
4756 if (!dt) dt++;
4757 db = resync - (mddev->resync_mark_cnt/2);
4758 rt = (dt * ((unsigned long)(max_blocks-resync) / (db/100+1)))/100;
4760 seq_printf(seq, " finish=%lu.%lumin", rt / 60, (rt % 60)/6);
4762 seq_printf(seq, " speed=%ldK/sec", db/dt);
4765 static void *md_seq_start(struct seq_file *seq, loff_t *pos)
4767 struct list_head *tmp;
4768 loff_t l = *pos;
4769 mddev_t *mddev;
4771 if (l >= 0x10000)
4772 return NULL;
4773 if (!l--)
4774 /* header */
4775 return (void*)1;
4777 spin_lock(&all_mddevs_lock);
4778 list_for_each(tmp,&all_mddevs)
4779 if (!l--) {
4780 mddev = list_entry(tmp, mddev_t, all_mddevs);
4781 mddev_get(mddev);
4782 spin_unlock(&all_mddevs_lock);
4783 return mddev;
4785 spin_unlock(&all_mddevs_lock);
4786 if (!l--)
4787 return (void*)2;/* tail */
4788 return NULL;
4791 static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
4793 struct list_head *tmp;
4794 mddev_t *next_mddev, *mddev = v;
4796 ++*pos;
4797 if (v == (void*)2)
4798 return NULL;
4800 spin_lock(&all_mddevs_lock);
4801 if (v == (void*)1)
4802 tmp = all_mddevs.next;
4803 else
4804 tmp = mddev->all_mddevs.next;
4805 if (tmp != &all_mddevs)
4806 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
4807 else {
4808 next_mddev = (void*)2;
4809 *pos = 0x10000;
4811 spin_unlock(&all_mddevs_lock);
4813 if (v != (void*)1)
4814 mddev_put(mddev);
4815 return next_mddev;
4819 static void md_seq_stop(struct seq_file *seq, void *v)
4821 mddev_t *mddev = v;
4823 if (mddev && v != (void*)1 && v != (void*)2)
4824 mddev_put(mddev);
4827 struct mdstat_info {
4828 int event;
4831 static int md_seq_show(struct seq_file *seq, void *v)
4833 mddev_t *mddev = v;
4834 sector_t size;
4835 struct list_head *tmp2;
4836 mdk_rdev_t *rdev;
4837 struct mdstat_info *mi = seq->private;
4838 struct bitmap *bitmap;
4840 if (v == (void*)1) {
4841 struct mdk_personality *pers;
4842 seq_printf(seq, "Personalities : ");
4843 spin_lock(&pers_lock);
4844 list_for_each_entry(pers, &pers_list, list)
4845 seq_printf(seq, "[%s] ", pers->name);
4847 spin_unlock(&pers_lock);
4848 seq_printf(seq, "\n");
4849 mi->event = atomic_read(&md_event_count);
4850 return 0;
4852 if (v == (void*)2) {
4853 status_unused(seq);
4854 return 0;
4857 if (mddev_lock(mddev) < 0)
4858 return -EINTR;
4860 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
4861 seq_printf(seq, "%s : %sactive", mdname(mddev),
4862 mddev->pers ? "" : "in");
4863 if (mddev->pers) {
4864 if (mddev->ro==1)
4865 seq_printf(seq, " (read-only)");
4866 if (mddev->ro==2)
4867 seq_printf(seq, "(auto-read-only)");
4868 seq_printf(seq, " %s", mddev->pers->name);
4871 size = 0;
4872 ITERATE_RDEV(mddev,rdev,tmp2) {
4873 char b[BDEVNAME_SIZE];
4874 seq_printf(seq, " %s[%d]",
4875 bdevname(rdev->bdev,b), rdev->desc_nr);
4876 if (test_bit(WriteMostly, &rdev->flags))
4877 seq_printf(seq, "(W)");
4878 if (test_bit(Faulty, &rdev->flags)) {
4879 seq_printf(seq, "(F)");
4880 continue;
4881 } else if (rdev->raid_disk < 0)
4882 seq_printf(seq, "(S)"); /* spare */
4883 size += rdev->size;
4886 if (!list_empty(&mddev->disks)) {
4887 if (mddev->pers)
4888 seq_printf(seq, "\n %llu blocks",
4889 (unsigned long long)mddev->array_size);
4890 else
4891 seq_printf(seq, "\n %llu blocks",
4892 (unsigned long long)size);
4894 if (mddev->persistent) {
4895 if (mddev->major_version != 0 ||
4896 mddev->minor_version != 90) {
4897 seq_printf(seq," super %d.%d",
4898 mddev->major_version,
4899 mddev->minor_version);
4901 } else
4902 seq_printf(seq, " super non-persistent");
4904 if (mddev->pers) {
4905 mddev->pers->status (seq, mddev);
4906 seq_printf(seq, "\n ");
4907 if (mddev->pers->sync_request) {
4908 if (mddev->curr_resync > 2) {
4909 status_resync (seq, mddev);
4910 seq_printf(seq, "\n ");
4911 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
4912 seq_printf(seq, "\tresync=DELAYED\n ");
4913 else if (mddev->recovery_cp < MaxSector)
4914 seq_printf(seq, "\tresync=PENDING\n ");
4916 } else
4917 seq_printf(seq, "\n ");
4919 if ((bitmap = mddev->bitmap)) {
4920 unsigned long chunk_kb;
4921 unsigned long flags;
4922 spin_lock_irqsave(&bitmap->lock, flags);
4923 chunk_kb = bitmap->chunksize >> 10;
4924 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
4925 "%lu%s chunk",
4926 bitmap->pages - bitmap->missing_pages,
4927 bitmap->pages,
4928 (bitmap->pages - bitmap->missing_pages)
4929 << (PAGE_SHIFT - 10),
4930 chunk_kb ? chunk_kb : bitmap->chunksize,
4931 chunk_kb ? "KB" : "B");
4932 if (bitmap->file) {
4933 seq_printf(seq, ", file: ");
4934 seq_path(seq, bitmap->file->f_vfsmnt,
4935 bitmap->file->f_dentry," \t\n");
4938 seq_printf(seq, "\n");
4939 spin_unlock_irqrestore(&bitmap->lock, flags);
4942 seq_printf(seq, "\n");
4944 mddev_unlock(mddev);
4946 return 0;
4949 static struct seq_operations md_seq_ops = {
4950 .start = md_seq_start,
4951 .next = md_seq_next,
4952 .stop = md_seq_stop,
4953 .show = md_seq_show,
4956 static int md_seq_open(struct inode *inode, struct file *file)
4958 int error;
4959 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
4960 if (mi == NULL)
4961 return -ENOMEM;
4963 error = seq_open(file, &md_seq_ops);
4964 if (error)
4965 kfree(mi);
4966 else {
4967 struct seq_file *p = file->private_data;
4968 p->private = mi;
4969 mi->event = atomic_read(&md_event_count);
4971 return error;
4974 static int md_seq_release(struct inode *inode, struct file *file)
4976 struct seq_file *m = file->private_data;
4977 struct mdstat_info *mi = m->private;
4978 m->private = NULL;
4979 kfree(mi);
4980 return seq_release(inode, file);
4983 static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
4985 struct seq_file *m = filp->private_data;
4986 struct mdstat_info *mi = m->private;
4987 int mask;
4989 poll_wait(filp, &md_event_waiters, wait);
4991 /* always allow read */
4992 mask = POLLIN | POLLRDNORM;
4994 if (mi->event != atomic_read(&md_event_count))
4995 mask |= POLLERR | POLLPRI;
4996 return mask;
4999 static struct file_operations md_seq_fops = {
5000 .open = md_seq_open,
5001 .read = seq_read,
5002 .llseek = seq_lseek,
5003 .release = md_seq_release,
5004 .poll = mdstat_poll,
5007 int register_md_personality(struct mdk_personality *p)
5009 spin_lock(&pers_lock);
5010 list_add_tail(&p->list, &pers_list);
5011 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
5012 spin_unlock(&pers_lock);
5013 return 0;
5016 int unregister_md_personality(struct mdk_personality *p)
5018 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
5019 spin_lock(&pers_lock);
5020 list_del_init(&p->list);
5021 spin_unlock(&pers_lock);
5022 return 0;
5025 static int is_mddev_idle(mddev_t *mddev)
5027 mdk_rdev_t * rdev;
5028 struct list_head *tmp;
5029 int idle;
5030 unsigned long curr_events;
5032 idle = 1;
5033 ITERATE_RDEV(mddev,rdev,tmp) {
5034 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
5035 curr_events = disk_stat_read(disk, sectors[0]) +
5036 disk_stat_read(disk, sectors[1]) -
5037 atomic_read(&disk->sync_io);
5038 /* The difference between curr_events and last_events
5039 * will be affected by any new non-sync IO (making
5040 * curr_events bigger) and any difference in the amount of
5041 * in-flight syncio (making current_events bigger or smaller)
5042 * The amount in-flight is currently limited to
5043 * 32*64K in raid1/10 and 256*PAGE_SIZE in raid5/6
5044 * which is at most 4096 sectors.
5045 * These numbers are fairly fragile and should be made
5046 * more robust, probably by enforcing the
5047 * 'window size' that md_do_sync sort-of uses.
5049 * Note: the following is an unsigned comparison.
5051 if ((curr_events - rdev->last_events + 4096) > 8192) {
5052 rdev->last_events = curr_events;
5053 idle = 0;
5056 return idle;
5059 void md_done_sync(mddev_t *mddev, int blocks, int ok)
5061 /* another "blocks" (512byte) blocks have been synced */
5062 atomic_sub(blocks, &mddev->recovery_active);
5063 wake_up(&mddev->recovery_wait);
5064 if (!ok) {
5065 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
5066 md_wakeup_thread(mddev->thread);
5067 // stop recovery, signal do_sync ....
5072 /* md_write_start(mddev, bi)
5073 * If we need to update some array metadata (e.g. 'active' flag
5074 * in superblock) before writing, schedule a superblock update
5075 * and wait for it to complete.
5077 void md_write_start(mddev_t *mddev, struct bio *bi)
5079 if (bio_data_dir(bi) != WRITE)
5080 return;
5082 BUG_ON(mddev->ro == 1);
5083 if (mddev->ro == 2) {
5084 /* need to switch to read/write */
5085 mddev->ro = 0;
5086 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5087 md_wakeup_thread(mddev->thread);
5089 atomic_inc(&mddev->writes_pending);
5090 if (mddev->in_sync) {
5091 spin_lock_irq(&mddev->write_lock);
5092 if (mddev->in_sync) {
5093 mddev->in_sync = 0;
5094 mddev->sb_dirty = 3;
5095 md_wakeup_thread(mddev->thread);
5097 spin_unlock_irq(&mddev->write_lock);
5099 wait_event(mddev->sb_wait, mddev->sb_dirty==0);
5102 void md_write_end(mddev_t *mddev)
5104 if (atomic_dec_and_test(&mddev->writes_pending)) {
5105 if (mddev->safemode == 2)
5106 md_wakeup_thread(mddev->thread);
5107 else if (mddev->safemode_delay)
5108 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
5112 static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
5114 #define SYNC_MARKS 10
5115 #define SYNC_MARK_STEP (3*HZ)
5116 void md_do_sync(mddev_t *mddev)
5118 mddev_t *mddev2;
5119 unsigned int currspeed = 0,
5120 window;
5121 sector_t max_sectors,j, io_sectors;
5122 unsigned long mark[SYNC_MARKS];
5123 sector_t mark_cnt[SYNC_MARKS];
5124 int last_mark,m;
5125 struct list_head *tmp;
5126 sector_t last_check;
5127 int skipped = 0;
5128 struct list_head *rtmp;
5129 mdk_rdev_t *rdev;
5131 /* just incase thread restarts... */
5132 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
5133 return;
5134 if (mddev->ro) /* never try to sync a read-only array */
5135 return;
5137 /* we overload curr_resync somewhat here.
5138 * 0 == not engaged in resync at all
5139 * 2 == checking that there is no conflict with another sync
5140 * 1 == like 2, but have yielded to allow conflicting resync to
5141 * commense
5142 * other == active in resync - this many blocks
5144 * Before starting a resync we must have set curr_resync to
5145 * 2, and then checked that every "conflicting" array has curr_resync
5146 * less than ours. When we find one that is the same or higher
5147 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
5148 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
5149 * This will mean we have to start checking from the beginning again.
5153 do {
5154 mddev->curr_resync = 2;
5156 try_again:
5157 if (kthread_should_stop()) {
5158 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5159 goto skip;
5161 ITERATE_MDDEV(mddev2,tmp) {
5162 if (mddev2 == mddev)
5163 continue;
5164 if (mddev2->curr_resync &&
5165 match_mddev_units(mddev,mddev2)) {
5166 DEFINE_WAIT(wq);
5167 if (mddev < mddev2 && mddev->curr_resync == 2) {
5168 /* arbitrarily yield */
5169 mddev->curr_resync = 1;
5170 wake_up(&resync_wait);
5172 if (mddev > mddev2 && mddev->curr_resync == 1)
5173 /* no need to wait here, we can wait the next
5174 * time 'round when curr_resync == 2
5176 continue;
5177 prepare_to_wait(&resync_wait, &wq, TASK_UNINTERRUPTIBLE);
5178 if (!kthread_should_stop() &&
5179 mddev2->curr_resync >= mddev->curr_resync) {
5180 printk(KERN_INFO "md: delaying resync of %s"
5181 " until %s has finished resync (they"
5182 " share one or more physical units)\n",
5183 mdname(mddev), mdname(mddev2));
5184 mddev_put(mddev2);
5185 schedule();
5186 finish_wait(&resync_wait, &wq);
5187 goto try_again;
5189 finish_wait(&resync_wait, &wq);
5192 } while (mddev->curr_resync < 2);
5194 j = 0;
5195 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
5196 /* resync follows the size requested by the personality,
5197 * which defaults to physical size, but can be virtual size
5199 max_sectors = mddev->resync_max_sectors;
5200 mddev->resync_mismatches = 0;
5201 /* we don't use the checkpoint if there's a bitmap */
5202 if (!mddev->bitmap &&
5203 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
5204 j = mddev->recovery_cp;
5205 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
5206 max_sectors = mddev->size << 1;
5207 else {
5208 /* recovery follows the physical size of devices */
5209 max_sectors = mddev->size << 1;
5210 j = MaxSector;
5211 ITERATE_RDEV(mddev,rdev,rtmp)
5212 if (rdev->raid_disk >= 0 &&
5213 !test_bit(Faulty, &rdev->flags) &&
5214 !test_bit(In_sync, &rdev->flags) &&
5215 rdev->recovery_offset < j)
5216 j = rdev->recovery_offset;
5219 printk(KERN_INFO "md: syncing RAID array %s\n", mdname(mddev));
5220 printk(KERN_INFO "md: minimum _guaranteed_ reconstruction speed:"
5221 " %d KB/sec/disc.\n", speed_min(mddev));
5222 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
5223 "(but not more than %d KB/sec) for reconstruction.\n",
5224 speed_max(mddev));
5226 is_mddev_idle(mddev); /* this also initializes IO event counters */
5228 io_sectors = 0;
5229 for (m = 0; m < SYNC_MARKS; m++) {
5230 mark[m] = jiffies;
5231 mark_cnt[m] = io_sectors;
5233 last_mark = 0;
5234 mddev->resync_mark = mark[last_mark];
5235 mddev->resync_mark_cnt = mark_cnt[last_mark];
5238 * Tune reconstruction:
5240 window = 32*(PAGE_SIZE/512);
5241 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
5242 window/2,(unsigned long long) max_sectors/2);
5244 atomic_set(&mddev->recovery_active, 0);
5245 init_waitqueue_head(&mddev->recovery_wait);
5246 last_check = 0;
5248 if (j>2) {
5249 printk(KERN_INFO
5250 "md: resuming recovery of %s from checkpoint.\n",
5251 mdname(mddev));
5252 mddev->curr_resync = j;
5255 while (j < max_sectors) {
5256 sector_t sectors;
5258 skipped = 0;
5259 if (j >= mddev->resync_max) {
5260 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
5261 wait_event(mddev->recovery_wait,
5262 mddev->resync_max > j || kthread_should_stop());
5264 if (kthread_should_stop())
5265 goto interrupted;
5266 sectors = mddev->pers->sync_request(mddev, j, &skipped,
5267 currspeed < speed_min(mddev));
5268 if (sectors == 0) {
5269 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
5270 goto out;
5273 if (!skipped) { /* actual IO requested */
5274 io_sectors += sectors;
5275 atomic_add(sectors, &mddev->recovery_active);
5278 j += sectors;
5279 if (j>1) mddev->curr_resync = j;
5280 if (last_check == 0)
5281 /* this is the earliers that rebuilt will be
5282 * visible in /proc/mdstat
5284 md_new_event(mddev);
5286 if (last_check + window > io_sectors || j == max_sectors)
5287 continue;
5289 last_check = io_sectors;
5291 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery) ||
5292 test_bit(MD_RECOVERY_ERR, &mddev->recovery))
5293 break;
5295 repeat:
5296 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
5297 /* step marks */
5298 int next = (last_mark+1) % SYNC_MARKS;
5300 mddev->resync_mark = mark[next];
5301 mddev->resync_mark_cnt = mark_cnt[next];
5302 mark[next] = jiffies;
5303 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
5304 last_mark = next;
5308 if (kthread_should_stop())
5309 goto interrupted;
5313 * this loop exits only if either when we are slower than
5314 * the 'hard' speed limit, or the system was IO-idle for
5315 * a jiffy.
5316 * the system might be non-idle CPU-wise, but we only care
5317 * about not overloading the IO subsystem. (things like an
5318 * e2fsck being done on the RAID array should execute fast)
5320 mddev->queue->unplug_fn(mddev->queue);
5321 cond_resched();
5323 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
5324 /((jiffies-mddev->resync_mark)/HZ +1) +1;
5326 if (currspeed > speed_min(mddev)) {
5327 if ((currspeed > speed_max(mddev)) ||
5328 !is_mddev_idle(mddev)) {
5329 msleep(500);
5330 goto repeat;
5334 printk(KERN_INFO "md: %s: sync done.\n",mdname(mddev));
5336 * this also signals 'finished resyncing' to md_stop
5338 out:
5339 mddev->queue->unplug_fn(mddev->queue);
5341 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
5343 /* tell personality that we are finished */
5344 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
5346 if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) &&
5347 test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
5348 !test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
5349 mddev->curr_resync > 2) {
5350 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
5351 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
5352 if (mddev->curr_resync >= mddev->recovery_cp) {
5353 printk(KERN_INFO
5354 "md: checkpointing recovery of %s.\n",
5355 mdname(mddev));
5356 mddev->recovery_cp = mddev->curr_resync;
5358 } else
5359 mddev->recovery_cp = MaxSector;
5360 } else {
5361 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
5362 mddev->curr_resync = MaxSector;
5363 ITERATE_RDEV(mddev,rdev,rtmp)
5364 if (rdev->raid_disk >= 0 &&
5365 !test_bit(Faulty, &rdev->flags) &&
5366 !test_bit(In_sync, &rdev->flags) &&
5367 rdev->recovery_offset < mddev->curr_resync)
5368 rdev->recovery_offset = mddev->curr_resync;
5372 skip:
5373 mddev->curr_resync = 0;
5374 mddev->resync_max = MaxSector;
5375 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
5376 wake_up(&resync_wait);
5377 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
5378 md_wakeup_thread(mddev->thread);
5379 return;
5381 interrupted:
5383 * got a signal, exit.
5385 printk(KERN_INFO
5386 "md: md_do_sync() got signal ... exiting\n");
5387 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5388 goto out;
5391 EXPORT_SYMBOL_GPL(md_do_sync);
5395 * This routine is regularly called by all per-raid-array threads to
5396 * deal with generic issues like resync and super-block update.
5397 * Raid personalities that don't have a thread (linear/raid0) do not
5398 * need this as they never do any recovery or update the superblock.
5400 * It does not do any resync itself, but rather "forks" off other threads
5401 * to do that as needed.
5402 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
5403 * "->recovery" and create a thread at ->sync_thread.
5404 * When the thread finishes it sets MD_RECOVERY_DONE (and might set MD_RECOVERY_ERR)
5405 * and wakeups up this thread which will reap the thread and finish up.
5406 * This thread also removes any faulty devices (with nr_pending == 0).
5408 * The overall approach is:
5409 * 1/ if the superblock needs updating, update it.
5410 * 2/ If a recovery thread is running, don't do anything else.
5411 * 3/ If recovery has finished, clean up, possibly marking spares active.
5412 * 4/ If there are any faulty devices, remove them.
5413 * 5/ If array is degraded, try to add spares devices
5414 * 6/ If array has spares or is not in-sync, start a resync thread.
5416 void md_check_recovery(mddev_t *mddev)
5418 mdk_rdev_t *rdev;
5419 struct list_head *rtmp;
5422 if (mddev->bitmap)
5423 bitmap_daemon_work(mddev->bitmap);
5425 if (mddev->ro)
5426 return;
5428 if (signal_pending(current)) {
5429 if (mddev->pers->sync_request) {
5430 printk(KERN_INFO "md: %s in immediate safe mode\n",
5431 mdname(mddev));
5432 mddev->safemode = 2;
5434 flush_signals(current);
5437 if ( ! (
5438 (mddev->sb_dirty && !mddev->external) ||
5439 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
5440 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
5441 (mddev->safemode == 1) ||
5442 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
5443 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
5445 return;
5447 if (mddev_trylock(mddev)) {
5448 int spares =0;
5450 spin_lock_irq(&mddev->write_lock);
5451 if (mddev->safemode && !atomic_read(&mddev->writes_pending) &&
5452 !mddev->in_sync && mddev->recovery_cp == MaxSector) {
5453 mddev->in_sync = 1;
5454 if (mddev->persistent)
5455 mddev->sb_dirty = 3;
5457 if (mddev->safemode == 1)
5458 mddev->safemode = 0;
5459 spin_unlock_irq(&mddev->write_lock);
5461 if (mddev->sb_dirty)
5462 md_update_sb(mddev);
5465 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
5466 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
5467 /* resync/recovery still happening */
5468 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5469 goto unlock;
5471 if (mddev->sync_thread) {
5472 /* resync has finished, collect result */
5473 md_unregister_thread(mddev->sync_thread);
5474 mddev->sync_thread = NULL;
5475 if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) &&
5476 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
5477 /* success...*/
5478 /* activate any spares */
5479 mddev->pers->spare_active(mddev);
5481 md_update_sb(mddev);
5483 /* if array is no-longer degraded, then any saved_raid_disk
5484 * information must be scrapped
5486 if (!mddev->degraded)
5487 ITERATE_RDEV(mddev,rdev,rtmp)
5488 rdev->saved_raid_disk = -1;
5490 mddev->recovery = 0;
5491 /* flag recovery needed just to double check */
5492 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5493 md_new_event(mddev);
5494 goto unlock;
5496 /* Clear some bits that don't mean anything, but
5497 * might be left set
5499 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5500 clear_bit(MD_RECOVERY_ERR, &mddev->recovery);
5501 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
5502 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
5504 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
5505 goto unlock;
5506 /* no recovery is running.
5507 * remove any failed drives, then
5508 * add spares if possible.
5509 * Spare are also removed and re-added, to allow
5510 * the personality to fail the re-add.
5512 ITERATE_RDEV(mddev,rdev,rtmp)
5513 if (rdev->raid_disk >= 0 &&
5514 (test_bit(Faulty, &rdev->flags) || ! test_bit(In_sync, &rdev->flags)) &&
5515 atomic_read(&rdev->nr_pending)==0) {
5516 if (mddev->pers->hot_remove_disk(mddev, rdev->raid_disk)==0) {
5517 char nm[20];
5518 sprintf(nm,"rd%d", rdev->raid_disk);
5519 sysfs_remove_link(&mddev->kobj, nm);
5520 rdev->raid_disk = -1;
5524 if (mddev->degraded) {
5525 ITERATE_RDEV(mddev,rdev,rtmp)
5526 if (rdev->raid_disk < 0
5527 && !test_bit(Faulty, &rdev->flags)) {
5528 rdev->recovery_offset = 0;
5529 if (mddev->pers->hot_add_disk(mddev,rdev)) {
5530 char nm[20];
5531 sprintf(nm, "rd%d", rdev->raid_disk);
5532 sysfs_create_link(&mddev->kobj, &rdev->kobj, nm);
5533 spares++;
5534 md_new_event(mddev);
5535 } else
5536 break;
5540 if (spares) {
5541 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5542 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5543 } else if (mddev->recovery_cp < MaxSector) {
5544 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5545 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
5546 /* nothing to be done ... */
5547 goto unlock;
5549 if (mddev->pers->sync_request) {
5550 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5551 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
5552 /* We are adding a device or devices to an array
5553 * which has the bitmap stored on all devices.
5554 * So make sure all bitmap pages get written
5556 bitmap_write_all(mddev->bitmap);
5558 mddev->sync_thread = md_register_thread(md_do_sync,
5559 mddev,
5560 "%s_resync");
5561 if (!mddev->sync_thread) {
5562 printk(KERN_ERR "%s: could not start resync"
5563 " thread...\n",
5564 mdname(mddev));
5565 /* leave the spares where they are, it shouldn't hurt */
5566 mddev->recovery = 0;
5567 } else
5568 md_wakeup_thread(mddev->sync_thread);
5569 md_new_event(mddev);
5571 unlock:
5572 mddev_unlock(mddev);
5576 static int md_notify_reboot(struct notifier_block *this,
5577 unsigned long code, void *x)
5579 struct list_head *tmp;
5580 mddev_t *mddev;
5582 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
5584 printk(KERN_INFO "md: stopping all md devices.\n");
5586 ITERATE_MDDEV(mddev,tmp)
5587 if (mddev_trylock(mddev)) {
5588 do_md_stop (mddev, 1);
5589 mddev_unlock(mddev);
5592 * certain more exotic SCSI devices are known to be
5593 * volatile wrt too early system reboots. While the
5594 * right place to handle this issue is the given
5595 * driver, we do want to have a safe RAID driver ...
5597 mdelay(1000*1);
5599 return NOTIFY_DONE;
5602 static struct notifier_block md_notifier = {
5603 .notifier_call = md_notify_reboot,
5604 .next = NULL,
5605 .priority = INT_MAX, /* before any real devices */
5608 static void md_geninit(void)
5610 struct proc_dir_entry *p;
5612 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
5614 p = create_proc_entry("mdstat", S_IRUGO, NULL);
5615 if (p)
5616 p->proc_fops = &md_seq_fops;
5619 static int __init md_init(void)
5621 int minor;
5623 printk(KERN_INFO "md: md driver %d.%d.%d MAX_MD_DEVS=%d,"
5624 " MD_SB_DISKS=%d\n",
5625 MD_MAJOR_VERSION, MD_MINOR_VERSION,
5626 MD_PATCHLEVEL_VERSION, MAX_MD_DEVS, MD_SB_DISKS);
5627 printk(KERN_INFO "md: bitmap version %d.%d\n", BITMAP_MAJOR_HI,
5628 BITMAP_MINOR);
5630 if (register_blkdev(MAJOR_NR, "md"))
5631 return -1;
5632 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
5633 unregister_blkdev(MAJOR_NR, "md");
5634 return -1;
5636 devfs_mk_dir("md");
5637 blk_register_region(MKDEV(MAJOR_NR, 0), MAX_MD_DEVS, THIS_MODULE,
5638 md_probe, NULL, NULL);
5639 blk_register_region(MKDEV(mdp_major, 0), MAX_MD_DEVS<<MdpMinorShift, THIS_MODULE,
5640 md_probe, NULL, NULL);
5642 for (minor=0; minor < MAX_MD_DEVS; ++minor)
5643 devfs_mk_bdev(MKDEV(MAJOR_NR, minor),
5644 S_IFBLK|S_IRUSR|S_IWUSR,
5645 "md/%d", minor);
5647 for (minor=0; minor < MAX_MD_DEVS; ++minor)
5648 devfs_mk_bdev(MKDEV(mdp_major, minor<<MdpMinorShift),
5649 S_IFBLK|S_IRUSR|S_IWUSR,
5650 "md/mdp%d", minor);
5653 register_reboot_notifier(&md_notifier);
5654 raid_table_header = register_sysctl_table(raid_root_table, 1);
5656 md_geninit();
5657 return (0);
5661 #ifndef MODULE
5664 * Searches all registered partitions for autorun RAID arrays
5665 * at boot time.
5667 static dev_t detected_devices[128];
5668 static int dev_cnt;
5670 void md_autodetect_dev(dev_t dev)
5672 if (dev_cnt >= 0 && dev_cnt < 127)
5673 detected_devices[dev_cnt++] = dev;
5677 static void autostart_arrays(int part)
5679 mdk_rdev_t *rdev;
5680 int i;
5682 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
5684 for (i = 0; i < dev_cnt; i++) {
5685 dev_t dev = detected_devices[i];
5687 rdev = md_import_device(dev,0, 0);
5688 if (IS_ERR(rdev))
5689 continue;
5691 if (test_bit(Faulty, &rdev->flags)) {
5692 MD_BUG();
5693 continue;
5695 list_add(&rdev->same_set, &pending_raid_disks);
5697 dev_cnt = 0;
5699 autorun_devices(part);
5702 #endif
5704 static __exit void md_exit(void)
5706 mddev_t *mddev;
5707 struct list_head *tmp;
5708 int i;
5709 blk_unregister_region(MKDEV(MAJOR_NR,0), MAX_MD_DEVS);
5710 blk_unregister_region(MKDEV(mdp_major,0), MAX_MD_DEVS << MdpMinorShift);
5711 for (i=0; i < MAX_MD_DEVS; i++)
5712 devfs_remove("md/%d", i);
5713 for (i=0; i < MAX_MD_DEVS; i++)
5714 devfs_remove("md/d%d", i);
5716 devfs_remove("md");
5718 unregister_blkdev(MAJOR_NR,"md");
5719 unregister_blkdev(mdp_major, "mdp");
5720 unregister_reboot_notifier(&md_notifier);
5721 unregister_sysctl_table(raid_table_header);
5722 remove_proc_entry("mdstat", NULL);
5723 ITERATE_MDDEV(mddev,tmp) {
5724 struct gendisk *disk = mddev->gendisk;
5725 if (!disk)
5726 continue;
5727 export_array(mddev);
5728 del_gendisk(disk);
5729 put_disk(disk);
5730 mddev->gendisk = NULL;
5731 mddev_put(mddev);
5735 module_init(md_init)
5736 module_exit(md_exit)
5738 static int get_ro(char *buffer, struct kernel_param *kp)
5740 return sprintf(buffer, "%d", start_readonly);
5742 static int set_ro(const char *val, struct kernel_param *kp)
5744 char *e;
5745 int num = simple_strtoul(val, &e, 10);
5746 if (*val && (*e == '\0' || *e == '\n')) {
5747 start_readonly = num;
5748 return 0;
5750 return -EINVAL;
5753 module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
5754 module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
5757 EXPORT_SYMBOL(register_md_personality);
5758 EXPORT_SYMBOL(unregister_md_personality);
5759 EXPORT_SYMBOL(md_error);
5760 EXPORT_SYMBOL(md_done_sync);
5761 EXPORT_SYMBOL(md_write_start);
5762 EXPORT_SYMBOL(md_write_end);
5763 EXPORT_SYMBOL(md_register_thread);
5764 EXPORT_SYMBOL(md_unregister_thread);
5765 EXPORT_SYMBOL(md_wakeup_thread);
5766 EXPORT_SYMBOL(md_check_recovery);
5767 MODULE_LICENSE("GPL");
5768 MODULE_ALIAS("md");
5769 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);