USB: serial: ftdi_sio: adding support for TavIR STK500
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / md / md.c
blob697a2c7bff2c2fd490b431d6466f2d92b8c016a2
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/kthread.h>
36 #include <linux/blkdev.h>
37 #include <linux/sysctl.h>
38 #include <linux/seq_file.h>
39 #include <linux/buffer_head.h> /* for invalidate_bdev */
40 #include <linux/poll.h>
41 #include <linux/ctype.h>
42 #include <linux/hdreg.h>
43 #include <linux/proc_fs.h>
44 #include <linux/random.h>
45 #include <linux/reboot.h>
46 #include <linux/file.h>
47 #include <linux/delay.h>
48 #include <linux/raid/md_p.h>
49 #include <linux/raid/md_u.h>
50 #include "md.h"
51 #include "bitmap.h"
53 #define DEBUG 0
54 #define dprintk(x...) ((void)(DEBUG && printk(x)))
57 #ifndef MODULE
58 static void autostart_arrays(int part);
59 #endif
61 static LIST_HEAD(pers_list);
62 static DEFINE_SPINLOCK(pers_lock);
64 static void md_print_devices(void);
66 static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
68 #define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
71 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
72 * is 1000 KB/sec, so the extra system load does not show up that much.
73 * Increase it if you want to have more _guaranteed_ speed. Note that
74 * the RAID driver will use the maximum available bandwidth if the IO
75 * subsystem is idle. There is also an 'absolute maximum' reconstruction
76 * speed limit - in case reconstruction slows down your system despite
77 * idle IO detection.
79 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
80 * or /sys/block/mdX/md/sync_speed_{min,max}
83 static int sysctl_speed_limit_min = 1000;
84 static int sysctl_speed_limit_max = 200000;
85 static inline int speed_min(mddev_t *mddev)
87 return mddev->sync_speed_min ?
88 mddev->sync_speed_min : sysctl_speed_limit_min;
91 static inline int speed_max(mddev_t *mddev)
93 return mddev->sync_speed_max ?
94 mddev->sync_speed_max : sysctl_speed_limit_max;
97 static struct ctl_table_header *raid_table_header;
99 static ctl_table raid_table[] = {
101 .ctl_name = DEV_RAID_SPEED_LIMIT_MIN,
102 .procname = "speed_limit_min",
103 .data = &sysctl_speed_limit_min,
104 .maxlen = sizeof(int),
105 .mode = S_IRUGO|S_IWUSR,
106 .proc_handler = &proc_dointvec,
109 .ctl_name = DEV_RAID_SPEED_LIMIT_MAX,
110 .procname = "speed_limit_max",
111 .data = &sysctl_speed_limit_max,
112 .maxlen = sizeof(int),
113 .mode = S_IRUGO|S_IWUSR,
114 .proc_handler = &proc_dointvec,
116 { .ctl_name = 0 }
119 static ctl_table raid_dir_table[] = {
121 .ctl_name = DEV_RAID,
122 .procname = "raid",
123 .maxlen = 0,
124 .mode = S_IRUGO|S_IXUGO,
125 .child = raid_table,
127 { .ctl_name = 0 }
130 static ctl_table raid_root_table[] = {
132 .ctl_name = CTL_DEV,
133 .procname = "dev",
134 .maxlen = 0,
135 .mode = 0555,
136 .child = raid_dir_table,
138 { .ctl_name = 0 }
141 static const struct block_device_operations md_fops;
143 static int start_readonly;
146 * We have a system wide 'event count' that is incremented
147 * on any 'interesting' event, and readers of /proc/mdstat
148 * can use 'poll' or 'select' to find out when the event
149 * count increases.
151 * Events are:
152 * start array, stop array, error, add device, remove device,
153 * start build, activate spare
155 static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
156 static atomic_t md_event_count;
157 void md_new_event(mddev_t *mddev)
159 atomic_inc(&md_event_count);
160 wake_up(&md_event_waiters);
162 EXPORT_SYMBOL_GPL(md_new_event);
164 /* Alternate version that can be called from interrupts
165 * when calling sysfs_notify isn't needed.
167 static void md_new_event_inintr(mddev_t *mddev)
169 atomic_inc(&md_event_count);
170 wake_up(&md_event_waiters);
174 * Enables to iterate over all existing md arrays
175 * all_mddevs_lock protects this list.
177 static LIST_HEAD(all_mddevs);
178 static DEFINE_SPINLOCK(all_mddevs_lock);
182 * iterates through all used mddevs in the system.
183 * We take care to grab the all_mddevs_lock whenever navigating
184 * the list, and to always hold a refcount when unlocked.
185 * Any code which breaks out of this loop while own
186 * a reference to the current mddev and must mddev_put it.
188 #define for_each_mddev(mddev,tmp) \
190 for (({ spin_lock(&all_mddevs_lock); \
191 tmp = all_mddevs.next; \
192 mddev = NULL;}); \
193 ({ if (tmp != &all_mddevs) \
194 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
195 spin_unlock(&all_mddevs_lock); \
196 if (mddev) mddev_put(mddev); \
197 mddev = list_entry(tmp, mddev_t, all_mddevs); \
198 tmp != &all_mddevs;}); \
199 ({ spin_lock(&all_mddevs_lock); \
200 tmp = tmp->next;}) \
204 /* Rather than calling directly into the personality make_request function,
205 * IO requests come here first so that we can check if the device is
206 * being suspended pending a reconfiguration.
207 * We hold a refcount over the call to ->make_request. By the time that
208 * call has finished, the bio has been linked into some internal structure
209 * and so is visible to ->quiesce(), so we don't need the refcount any more.
211 static int md_make_request(struct request_queue *q, struct bio *bio)
213 mddev_t *mddev = q->queuedata;
214 int rv;
215 if (mddev == NULL || mddev->pers == NULL) {
216 bio_io_error(bio);
217 return 0;
219 rcu_read_lock();
220 if (mddev->suspended) {
221 DEFINE_WAIT(__wait);
222 for (;;) {
223 prepare_to_wait(&mddev->sb_wait, &__wait,
224 TASK_UNINTERRUPTIBLE);
225 if (!mddev->suspended)
226 break;
227 rcu_read_unlock();
228 schedule();
229 rcu_read_lock();
231 finish_wait(&mddev->sb_wait, &__wait);
233 atomic_inc(&mddev->active_io);
234 rcu_read_unlock();
235 rv = mddev->pers->make_request(q, bio);
236 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
237 wake_up(&mddev->sb_wait);
239 return rv;
242 static void mddev_suspend(mddev_t *mddev)
244 BUG_ON(mddev->suspended);
245 mddev->suspended = 1;
246 synchronize_rcu();
247 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
248 mddev->pers->quiesce(mddev, 1);
249 md_unregister_thread(mddev->thread);
250 mddev->thread = NULL;
251 /* we now know that no code is executing in the personality module,
252 * except possibly the tail end of a ->bi_end_io function, but that
253 * is certain to complete before the module has a chance to get
254 * unloaded
258 static void mddev_resume(mddev_t *mddev)
260 mddev->suspended = 0;
261 wake_up(&mddev->sb_wait);
262 mddev->pers->quiesce(mddev, 0);
265 int mddev_congested(mddev_t *mddev, int bits)
267 return mddev->suspended;
269 EXPORT_SYMBOL(mddev_congested);
272 static inline mddev_t *mddev_get(mddev_t *mddev)
274 atomic_inc(&mddev->active);
275 return mddev;
278 static void mddev_delayed_delete(struct work_struct *ws);
280 static void mddev_put(mddev_t *mddev)
282 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
283 return;
284 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
285 mddev->ctime == 0 && !mddev->hold_active) {
286 /* Array is not configured at all, and not held active,
287 * so destroy it */
288 list_del(&mddev->all_mddevs);
289 if (mddev->gendisk) {
290 /* we did a probe so need to clean up.
291 * Call schedule_work inside the spinlock
292 * so that flush_scheduled_work() after
293 * mddev_find will succeed in waiting for the
294 * work to be done.
296 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
297 schedule_work(&mddev->del_work);
298 } else
299 kfree(mddev);
301 spin_unlock(&all_mddevs_lock);
304 static mddev_t * mddev_find(dev_t unit)
306 mddev_t *mddev, *new = NULL;
308 if (unit && MAJOR(unit) != MD_MAJOR)
309 unit &= ~((1<<MdpMinorShift)-1);
311 retry:
312 spin_lock(&all_mddevs_lock);
314 if (unit) {
315 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
316 if (mddev->unit == unit) {
317 mddev_get(mddev);
318 spin_unlock(&all_mddevs_lock);
319 kfree(new);
320 return mddev;
323 if (new) {
324 list_add(&new->all_mddevs, &all_mddevs);
325 spin_unlock(&all_mddevs_lock);
326 new->hold_active = UNTIL_IOCTL;
327 return new;
329 } else if (new) {
330 /* find an unused unit number */
331 static int next_minor = 512;
332 int start = next_minor;
333 int is_free = 0;
334 int dev = 0;
335 while (!is_free) {
336 dev = MKDEV(MD_MAJOR, next_minor);
337 next_minor++;
338 if (next_minor > MINORMASK)
339 next_minor = 0;
340 if (next_minor == start) {
341 /* Oh dear, all in use. */
342 spin_unlock(&all_mddevs_lock);
343 kfree(new);
344 return NULL;
347 is_free = 1;
348 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
349 if (mddev->unit == dev) {
350 is_free = 0;
351 break;
354 new->unit = dev;
355 new->md_minor = MINOR(dev);
356 new->hold_active = UNTIL_STOP;
357 list_add(&new->all_mddevs, &all_mddevs);
358 spin_unlock(&all_mddevs_lock);
359 return new;
361 spin_unlock(&all_mddevs_lock);
363 new = kzalloc(sizeof(*new), GFP_KERNEL);
364 if (!new)
365 return NULL;
367 new->unit = unit;
368 if (MAJOR(unit) == MD_MAJOR)
369 new->md_minor = MINOR(unit);
370 else
371 new->md_minor = MINOR(unit) >> MdpMinorShift;
373 mutex_init(&new->open_mutex);
374 mutex_init(&new->reconfig_mutex);
375 mutex_init(&new->bitmap_mutex);
376 INIT_LIST_HEAD(&new->disks);
377 INIT_LIST_HEAD(&new->all_mddevs);
378 init_timer(&new->safemode_timer);
379 atomic_set(&new->active, 1);
380 atomic_set(&new->openers, 0);
381 atomic_set(&new->active_io, 0);
382 spin_lock_init(&new->write_lock);
383 init_waitqueue_head(&new->sb_wait);
384 init_waitqueue_head(&new->recovery_wait);
385 new->reshape_position = MaxSector;
386 new->resync_min = 0;
387 new->resync_max = MaxSector;
388 new->level = LEVEL_NONE;
390 goto retry;
393 static inline int mddev_lock(mddev_t * mddev)
395 return mutex_lock_interruptible(&mddev->reconfig_mutex);
398 static inline int mddev_is_locked(mddev_t *mddev)
400 return mutex_is_locked(&mddev->reconfig_mutex);
403 static inline int mddev_trylock(mddev_t * mddev)
405 return mutex_trylock(&mddev->reconfig_mutex);
408 static inline void mddev_unlock(mddev_t * mddev)
410 mutex_unlock(&mddev->reconfig_mutex);
412 md_wakeup_thread(mddev->thread);
415 static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
417 mdk_rdev_t *rdev;
419 list_for_each_entry(rdev, &mddev->disks, same_set)
420 if (rdev->desc_nr == nr)
421 return rdev;
423 return NULL;
426 static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
428 mdk_rdev_t *rdev;
430 list_for_each_entry(rdev, &mddev->disks, same_set)
431 if (rdev->bdev->bd_dev == dev)
432 return rdev;
434 return NULL;
437 static struct mdk_personality *find_pers(int level, char *clevel)
439 struct mdk_personality *pers;
440 list_for_each_entry(pers, &pers_list, list) {
441 if (level != LEVEL_NONE && pers->level == level)
442 return pers;
443 if (strcmp(pers->name, clevel)==0)
444 return pers;
446 return NULL;
449 /* return the offset of the super block in 512byte sectors */
450 static inline sector_t calc_dev_sboffset(struct block_device *bdev)
452 sector_t num_sectors = bdev->bd_inode->i_size / 512;
453 return MD_NEW_SIZE_SECTORS(num_sectors);
456 static int alloc_disk_sb(mdk_rdev_t * rdev)
458 if (rdev->sb_page)
459 MD_BUG();
461 rdev->sb_page = alloc_page(GFP_KERNEL);
462 if (!rdev->sb_page) {
463 printk(KERN_ALERT "md: out of memory.\n");
464 return -ENOMEM;
467 return 0;
470 static void free_disk_sb(mdk_rdev_t * rdev)
472 if (rdev->sb_page) {
473 put_page(rdev->sb_page);
474 rdev->sb_loaded = 0;
475 rdev->sb_page = NULL;
476 rdev->sb_start = 0;
477 rdev->sectors = 0;
482 static void super_written(struct bio *bio, int error)
484 mdk_rdev_t *rdev = bio->bi_private;
485 mddev_t *mddev = rdev->mddev;
487 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
488 printk("md: super_written gets error=%d, uptodate=%d\n",
489 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
490 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
491 md_error(mddev, rdev);
494 if (atomic_dec_and_test(&mddev->pending_writes))
495 wake_up(&mddev->sb_wait);
496 bio_put(bio);
499 static void super_written_barrier(struct bio *bio, int error)
501 struct bio *bio2 = bio->bi_private;
502 mdk_rdev_t *rdev = bio2->bi_private;
503 mddev_t *mddev = rdev->mddev;
505 if (!test_bit(BIO_UPTODATE, &bio->bi_flags) &&
506 error == -EOPNOTSUPP) {
507 unsigned long flags;
508 /* barriers don't appear to be supported :-( */
509 set_bit(BarriersNotsupp, &rdev->flags);
510 mddev->barriers_work = 0;
511 spin_lock_irqsave(&mddev->write_lock, flags);
512 bio2->bi_next = mddev->biolist;
513 mddev->biolist = bio2;
514 spin_unlock_irqrestore(&mddev->write_lock, flags);
515 wake_up(&mddev->sb_wait);
516 bio_put(bio);
517 } else {
518 bio_put(bio2);
519 bio->bi_private = rdev;
520 super_written(bio, error);
524 void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
525 sector_t sector, int size, struct page *page)
527 /* write first size bytes of page to sector of rdev
528 * Increment mddev->pending_writes before returning
529 * and decrement it on completion, waking up sb_wait
530 * if zero is reached.
531 * If an error occurred, call md_error
533 * As we might need to resubmit the request if BIO_RW_BARRIER
534 * causes ENOTSUPP, we allocate a spare bio...
536 struct bio *bio = bio_alloc(GFP_NOIO, 1);
537 int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNCIO) | (1<<BIO_RW_UNPLUG);
539 bio->bi_bdev = rdev->bdev;
540 bio->bi_sector = sector;
541 bio_add_page(bio, page, size, 0);
542 bio->bi_private = rdev;
543 bio->bi_end_io = super_written;
544 bio->bi_rw = rw;
546 atomic_inc(&mddev->pending_writes);
547 if (!test_bit(BarriersNotsupp, &rdev->flags)) {
548 struct bio *rbio;
549 rw |= (1<<BIO_RW_BARRIER);
550 rbio = bio_clone(bio, GFP_NOIO);
551 rbio->bi_private = bio;
552 rbio->bi_end_io = super_written_barrier;
553 submit_bio(rw, rbio);
554 } else
555 submit_bio(rw, bio);
558 void md_super_wait(mddev_t *mddev)
560 /* wait for all superblock writes that were scheduled to complete.
561 * if any had to be retried (due to BARRIER problems), retry them
563 DEFINE_WAIT(wq);
564 for(;;) {
565 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
566 if (atomic_read(&mddev->pending_writes)==0)
567 break;
568 while (mddev->biolist) {
569 struct bio *bio;
570 spin_lock_irq(&mddev->write_lock);
571 bio = mddev->biolist;
572 mddev->biolist = bio->bi_next ;
573 bio->bi_next = NULL;
574 spin_unlock_irq(&mddev->write_lock);
575 submit_bio(bio->bi_rw, bio);
577 schedule();
579 finish_wait(&mddev->sb_wait, &wq);
582 static void bi_complete(struct bio *bio, int error)
584 complete((struct completion*)bio->bi_private);
587 int sync_page_io(struct block_device *bdev, sector_t sector, int size,
588 struct page *page, int rw)
590 struct bio *bio = bio_alloc(GFP_NOIO, 1);
591 struct completion event;
592 int ret;
594 rw |= (1 << BIO_RW_SYNCIO) | (1 << BIO_RW_UNPLUG);
596 bio->bi_bdev = bdev;
597 bio->bi_sector = sector;
598 bio_add_page(bio, page, size, 0);
599 init_completion(&event);
600 bio->bi_private = &event;
601 bio->bi_end_io = bi_complete;
602 submit_bio(rw, bio);
603 wait_for_completion(&event);
605 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
606 bio_put(bio);
607 return ret;
609 EXPORT_SYMBOL_GPL(sync_page_io);
611 static int read_disk_sb(mdk_rdev_t * rdev, int size)
613 char b[BDEVNAME_SIZE];
614 if (!rdev->sb_page) {
615 MD_BUG();
616 return -EINVAL;
618 if (rdev->sb_loaded)
619 return 0;
622 if (!sync_page_io(rdev->bdev, rdev->sb_start, size, rdev->sb_page, READ))
623 goto fail;
624 rdev->sb_loaded = 1;
625 return 0;
627 fail:
628 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
629 bdevname(rdev->bdev,b));
630 return -EINVAL;
633 static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
635 return sb1->set_uuid0 == sb2->set_uuid0 &&
636 sb1->set_uuid1 == sb2->set_uuid1 &&
637 sb1->set_uuid2 == sb2->set_uuid2 &&
638 sb1->set_uuid3 == sb2->set_uuid3;
641 static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
643 int ret;
644 mdp_super_t *tmp1, *tmp2;
646 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
647 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
649 if (!tmp1 || !tmp2) {
650 ret = 0;
651 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
652 goto abort;
655 *tmp1 = *sb1;
656 *tmp2 = *sb2;
659 * nr_disks is not constant
661 tmp1->nr_disks = 0;
662 tmp2->nr_disks = 0;
664 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
665 abort:
666 kfree(tmp1);
667 kfree(tmp2);
668 return ret;
672 static u32 md_csum_fold(u32 csum)
674 csum = (csum & 0xffff) + (csum >> 16);
675 return (csum & 0xffff) + (csum >> 16);
678 static unsigned int calc_sb_csum(mdp_super_t * sb)
680 u64 newcsum = 0;
681 u32 *sb32 = (u32*)sb;
682 int i;
683 unsigned int disk_csum, csum;
685 disk_csum = sb->sb_csum;
686 sb->sb_csum = 0;
688 for (i = 0; i < MD_SB_BYTES/4 ; i++)
689 newcsum += sb32[i];
690 csum = (newcsum & 0xffffffff) + (newcsum>>32);
693 #ifdef CONFIG_ALPHA
694 /* This used to use csum_partial, which was wrong for several
695 * reasons including that different results are returned on
696 * different architectures. It isn't critical that we get exactly
697 * the same return value as before (we always csum_fold before
698 * testing, and that removes any differences). However as we
699 * know that csum_partial always returned a 16bit value on
700 * alphas, do a fold to maximise conformity to previous behaviour.
702 sb->sb_csum = md_csum_fold(disk_csum);
703 #else
704 sb->sb_csum = disk_csum;
705 #endif
706 return csum;
711 * Handle superblock details.
712 * We want to be able to handle multiple superblock formats
713 * so we have a common interface to them all, and an array of
714 * different handlers.
715 * We rely on user-space to write the initial superblock, and support
716 * reading and updating of superblocks.
717 * Interface methods are:
718 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
719 * loads and validates a superblock on dev.
720 * if refdev != NULL, compare superblocks on both devices
721 * Return:
722 * 0 - dev has a superblock that is compatible with refdev
723 * 1 - dev has a superblock that is compatible and newer than refdev
724 * so dev should be used as the refdev in future
725 * -EINVAL superblock incompatible or invalid
726 * -othererror e.g. -EIO
728 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
729 * Verify that dev is acceptable into mddev.
730 * The first time, mddev->raid_disks will be 0, and data from
731 * dev should be merged in. Subsequent calls check that dev
732 * is new enough. Return 0 or -EINVAL
734 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
735 * Update the superblock for rdev with data in mddev
736 * This does not write to disc.
740 struct super_type {
741 char *name;
742 struct module *owner;
743 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev,
744 int minor_version);
745 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
746 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
747 unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev,
748 sector_t num_sectors);
752 * Check that the given mddev has no bitmap.
754 * This function is called from the run method of all personalities that do not
755 * support bitmaps. It prints an error message and returns non-zero if mddev
756 * has a bitmap. Otherwise, it returns 0.
759 int md_check_no_bitmap(mddev_t *mddev)
761 if (!mddev->bitmap_file && !mddev->bitmap_offset)
762 return 0;
763 printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
764 mdname(mddev), mddev->pers->name);
765 return 1;
767 EXPORT_SYMBOL(md_check_no_bitmap);
770 * load_super for 0.90.0
772 static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
774 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
775 mdp_super_t *sb;
776 int ret;
779 * Calculate the position of the superblock (512byte sectors),
780 * it's at the end of the disk.
782 * It also happens to be a multiple of 4Kb.
784 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
786 ret = read_disk_sb(rdev, MD_SB_BYTES);
787 if (ret) return ret;
789 ret = -EINVAL;
791 bdevname(rdev->bdev, b);
792 sb = (mdp_super_t*)page_address(rdev->sb_page);
794 if (sb->md_magic != MD_SB_MAGIC) {
795 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
797 goto abort;
800 if (sb->major_version != 0 ||
801 sb->minor_version < 90 ||
802 sb->minor_version > 91) {
803 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
804 sb->major_version, sb->minor_version,
806 goto abort;
809 if (sb->raid_disks <= 0)
810 goto abort;
812 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
813 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
815 goto abort;
818 rdev->preferred_minor = sb->md_minor;
819 rdev->data_offset = 0;
820 rdev->sb_size = MD_SB_BYTES;
822 if (sb->level == LEVEL_MULTIPATH)
823 rdev->desc_nr = -1;
824 else
825 rdev->desc_nr = sb->this_disk.number;
827 if (!refdev) {
828 ret = 1;
829 } else {
830 __u64 ev1, ev2;
831 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
832 if (!uuid_equal(refsb, sb)) {
833 printk(KERN_WARNING "md: %s has different UUID to %s\n",
834 b, bdevname(refdev->bdev,b2));
835 goto abort;
837 if (!sb_equal(refsb, sb)) {
838 printk(KERN_WARNING "md: %s has same UUID"
839 " but different superblock to %s\n",
840 b, bdevname(refdev->bdev, b2));
841 goto abort;
843 ev1 = md_event(sb);
844 ev2 = md_event(refsb);
845 if (ev1 > ev2)
846 ret = 1;
847 else
848 ret = 0;
850 rdev->sectors = rdev->sb_start;
852 if (rdev->sectors < sb->size * 2 && sb->level > 1)
853 /* "this cannot possibly happen" ... */
854 ret = -EINVAL;
856 abort:
857 return ret;
861 * validate_super for 0.90.0
863 static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
865 mdp_disk_t *desc;
866 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
867 __u64 ev1 = md_event(sb);
869 rdev->raid_disk = -1;
870 clear_bit(Faulty, &rdev->flags);
871 clear_bit(In_sync, &rdev->flags);
872 clear_bit(WriteMostly, &rdev->flags);
873 clear_bit(BarriersNotsupp, &rdev->flags);
875 if (mddev->raid_disks == 0) {
876 mddev->major_version = 0;
877 mddev->minor_version = sb->minor_version;
878 mddev->patch_version = sb->patch_version;
879 mddev->external = 0;
880 mddev->chunk_sectors = sb->chunk_size >> 9;
881 mddev->ctime = sb->ctime;
882 mddev->utime = sb->utime;
883 mddev->level = sb->level;
884 mddev->clevel[0] = 0;
885 mddev->layout = sb->layout;
886 mddev->raid_disks = sb->raid_disks;
887 mddev->dev_sectors = sb->size * 2;
888 mddev->events = ev1;
889 mddev->bitmap_offset = 0;
890 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
892 if (mddev->minor_version >= 91) {
893 mddev->reshape_position = sb->reshape_position;
894 mddev->delta_disks = sb->delta_disks;
895 mddev->new_level = sb->new_level;
896 mddev->new_layout = sb->new_layout;
897 mddev->new_chunk_sectors = sb->new_chunk >> 9;
898 } else {
899 mddev->reshape_position = MaxSector;
900 mddev->delta_disks = 0;
901 mddev->new_level = mddev->level;
902 mddev->new_layout = mddev->layout;
903 mddev->new_chunk_sectors = mddev->chunk_sectors;
906 if (sb->state & (1<<MD_SB_CLEAN))
907 mddev->recovery_cp = MaxSector;
908 else {
909 if (sb->events_hi == sb->cp_events_hi &&
910 sb->events_lo == sb->cp_events_lo) {
911 mddev->recovery_cp = sb->recovery_cp;
912 } else
913 mddev->recovery_cp = 0;
916 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
917 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
918 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
919 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
921 mddev->max_disks = MD_SB_DISKS;
923 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
924 mddev->bitmap_file == NULL)
925 mddev->bitmap_offset = mddev->default_bitmap_offset;
927 } else if (mddev->pers == NULL) {
928 /* Insist on good event counter while assembling */
929 ++ev1;
930 if (ev1 < mddev->events)
931 return -EINVAL;
932 } else if (mddev->bitmap) {
933 /* if adding to array with a bitmap, then we can accept an
934 * older device ... but not too old.
936 if (ev1 < mddev->bitmap->events_cleared)
937 return 0;
938 } else {
939 if (ev1 < mddev->events)
940 /* just a hot-add of a new device, leave raid_disk at -1 */
941 return 0;
944 if (mddev->level != LEVEL_MULTIPATH) {
945 desc = sb->disks + rdev->desc_nr;
947 if (desc->state & (1<<MD_DISK_FAULTY))
948 set_bit(Faulty, &rdev->flags);
949 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
950 desc->raid_disk < mddev->raid_disks */) {
951 set_bit(In_sync, &rdev->flags);
952 rdev->raid_disk = desc->raid_disk;
953 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
954 /* active but not in sync implies recovery up to
955 * reshape position. We don't know exactly where
956 * that is, so set to zero for now */
957 if (mddev->minor_version >= 91) {
958 rdev->recovery_offset = 0;
959 rdev->raid_disk = desc->raid_disk;
962 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
963 set_bit(WriteMostly, &rdev->flags);
964 } else /* MULTIPATH are always insync */
965 set_bit(In_sync, &rdev->flags);
966 return 0;
970 * sync_super for 0.90.0
972 static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
974 mdp_super_t *sb;
975 mdk_rdev_t *rdev2;
976 int next_spare = mddev->raid_disks;
979 /* make rdev->sb match mddev data..
981 * 1/ zero out disks
982 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
983 * 3/ any empty disks < next_spare become removed
985 * disks[0] gets initialised to REMOVED because
986 * we cannot be sure from other fields if it has
987 * been initialised or not.
989 int i;
990 int active=0, working=0,failed=0,spare=0,nr_disks=0;
992 rdev->sb_size = MD_SB_BYTES;
994 sb = (mdp_super_t*)page_address(rdev->sb_page);
996 memset(sb, 0, sizeof(*sb));
998 sb->md_magic = MD_SB_MAGIC;
999 sb->major_version = mddev->major_version;
1000 sb->patch_version = mddev->patch_version;
1001 sb->gvalid_words = 0; /* ignored */
1002 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1003 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1004 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1005 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1007 sb->ctime = mddev->ctime;
1008 sb->level = mddev->level;
1009 sb->size = mddev->dev_sectors / 2;
1010 sb->raid_disks = mddev->raid_disks;
1011 sb->md_minor = mddev->md_minor;
1012 sb->not_persistent = 0;
1013 sb->utime = mddev->utime;
1014 sb->state = 0;
1015 sb->events_hi = (mddev->events>>32);
1016 sb->events_lo = (u32)mddev->events;
1018 if (mddev->reshape_position == MaxSector)
1019 sb->minor_version = 90;
1020 else {
1021 sb->minor_version = 91;
1022 sb->reshape_position = mddev->reshape_position;
1023 sb->new_level = mddev->new_level;
1024 sb->delta_disks = mddev->delta_disks;
1025 sb->new_layout = mddev->new_layout;
1026 sb->new_chunk = mddev->new_chunk_sectors << 9;
1028 mddev->minor_version = sb->minor_version;
1029 if (mddev->in_sync)
1031 sb->recovery_cp = mddev->recovery_cp;
1032 sb->cp_events_hi = (mddev->events>>32);
1033 sb->cp_events_lo = (u32)mddev->events;
1034 if (mddev->recovery_cp == MaxSector)
1035 sb->state = (1<< MD_SB_CLEAN);
1036 } else
1037 sb->recovery_cp = 0;
1039 sb->layout = mddev->layout;
1040 sb->chunk_size = mddev->chunk_sectors << 9;
1042 if (mddev->bitmap && mddev->bitmap_file == NULL)
1043 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1045 sb->disks[0].state = (1<<MD_DISK_REMOVED);
1046 list_for_each_entry(rdev2, &mddev->disks, same_set) {
1047 mdp_disk_t *d;
1048 int desc_nr;
1049 int is_active = test_bit(In_sync, &rdev2->flags);
1051 if (rdev2->raid_disk >= 0 &&
1052 sb->minor_version >= 91)
1053 /* we have nowhere to store the recovery_offset,
1054 * but if it is not below the reshape_position,
1055 * we can piggy-back on that.
1057 is_active = 1;
1058 if (rdev2->raid_disk < 0 ||
1059 test_bit(Faulty, &rdev2->flags))
1060 is_active = 0;
1061 if (is_active)
1062 desc_nr = rdev2->raid_disk;
1063 else
1064 desc_nr = next_spare++;
1065 rdev2->desc_nr = desc_nr;
1066 d = &sb->disks[rdev2->desc_nr];
1067 nr_disks++;
1068 d->number = rdev2->desc_nr;
1069 d->major = MAJOR(rdev2->bdev->bd_dev);
1070 d->minor = MINOR(rdev2->bdev->bd_dev);
1071 if (is_active)
1072 d->raid_disk = rdev2->raid_disk;
1073 else
1074 d->raid_disk = rdev2->desc_nr; /* compatibility */
1075 if (test_bit(Faulty, &rdev2->flags))
1076 d->state = (1<<MD_DISK_FAULTY);
1077 else if (is_active) {
1078 d->state = (1<<MD_DISK_ACTIVE);
1079 if (test_bit(In_sync, &rdev2->flags))
1080 d->state |= (1<<MD_DISK_SYNC);
1081 active++;
1082 working++;
1083 } else {
1084 d->state = 0;
1085 spare++;
1086 working++;
1088 if (test_bit(WriteMostly, &rdev2->flags))
1089 d->state |= (1<<MD_DISK_WRITEMOSTLY);
1091 /* now set the "removed" and "faulty" bits on any missing devices */
1092 for (i=0 ; i < mddev->raid_disks ; i++) {
1093 mdp_disk_t *d = &sb->disks[i];
1094 if (d->state == 0 && d->number == 0) {
1095 d->number = i;
1096 d->raid_disk = i;
1097 d->state = (1<<MD_DISK_REMOVED);
1098 d->state |= (1<<MD_DISK_FAULTY);
1099 failed++;
1102 sb->nr_disks = nr_disks;
1103 sb->active_disks = active;
1104 sb->working_disks = working;
1105 sb->failed_disks = failed;
1106 sb->spare_disks = spare;
1108 sb->this_disk = sb->disks[rdev->desc_nr];
1109 sb->sb_csum = calc_sb_csum(sb);
1113 * rdev_size_change for 0.90.0
1115 static unsigned long long
1116 super_90_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
1118 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
1119 return 0; /* component must fit device */
1120 if (rdev->mddev->bitmap_offset)
1121 return 0; /* can't move bitmap */
1122 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
1123 if (!num_sectors || num_sectors > rdev->sb_start)
1124 num_sectors = rdev->sb_start;
1125 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
1126 rdev->sb_page);
1127 md_super_wait(rdev->mddev);
1128 return num_sectors;
1133 * version 1 superblock
1136 static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
1138 __le32 disk_csum;
1139 u32 csum;
1140 unsigned long long newcsum;
1141 int size = 256 + le32_to_cpu(sb->max_dev)*2;
1142 __le32 *isuper = (__le32*)sb;
1143 int i;
1145 disk_csum = sb->sb_csum;
1146 sb->sb_csum = 0;
1147 newcsum = 0;
1148 for (i=0; size>=4; size -= 4 )
1149 newcsum += le32_to_cpu(*isuper++);
1151 if (size == 2)
1152 newcsum += le16_to_cpu(*(__le16*) isuper);
1154 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1155 sb->sb_csum = disk_csum;
1156 return cpu_to_le32(csum);
1159 static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
1161 struct mdp_superblock_1 *sb;
1162 int ret;
1163 sector_t sb_start;
1164 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1165 int bmask;
1168 * Calculate the position of the superblock in 512byte sectors.
1169 * It is always aligned to a 4K boundary and
1170 * depeding on minor_version, it can be:
1171 * 0: At least 8K, but less than 12K, from end of device
1172 * 1: At start of device
1173 * 2: 4K from start of device.
1175 switch(minor_version) {
1176 case 0:
1177 sb_start = rdev->bdev->bd_inode->i_size >> 9;
1178 sb_start -= 8*2;
1179 sb_start &= ~(sector_t)(4*2-1);
1180 break;
1181 case 1:
1182 sb_start = 0;
1183 break;
1184 case 2:
1185 sb_start = 8;
1186 break;
1187 default:
1188 return -EINVAL;
1190 rdev->sb_start = sb_start;
1192 /* superblock is rarely larger than 1K, but it can be larger,
1193 * and it is safe to read 4k, so we do that
1195 ret = read_disk_sb(rdev, 4096);
1196 if (ret) return ret;
1199 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1201 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1202 sb->major_version != cpu_to_le32(1) ||
1203 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
1204 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
1205 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
1206 return -EINVAL;
1208 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1209 printk("md: invalid superblock checksum on %s\n",
1210 bdevname(rdev->bdev,b));
1211 return -EINVAL;
1213 if (le64_to_cpu(sb->data_size) < 10) {
1214 printk("md: data_size too small on %s\n",
1215 bdevname(rdev->bdev,b));
1216 return -EINVAL;
1219 rdev->preferred_minor = 0xffff;
1220 rdev->data_offset = le64_to_cpu(sb->data_offset);
1221 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
1223 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
1224 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1225 if (rdev->sb_size & bmask)
1226 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1228 if (minor_version
1229 && rdev->data_offset < sb_start + (rdev->sb_size/512))
1230 return -EINVAL;
1232 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1233 rdev->desc_nr = -1;
1234 else
1235 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1237 if (!refdev) {
1238 ret = 1;
1239 } else {
1240 __u64 ev1, ev2;
1241 struct mdp_superblock_1 *refsb =
1242 (struct mdp_superblock_1*)page_address(refdev->sb_page);
1244 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1245 sb->level != refsb->level ||
1246 sb->layout != refsb->layout ||
1247 sb->chunksize != refsb->chunksize) {
1248 printk(KERN_WARNING "md: %s has strangely different"
1249 " superblock to %s\n",
1250 bdevname(rdev->bdev,b),
1251 bdevname(refdev->bdev,b2));
1252 return -EINVAL;
1254 ev1 = le64_to_cpu(sb->events);
1255 ev2 = le64_to_cpu(refsb->events);
1257 if (ev1 > ev2)
1258 ret = 1;
1259 else
1260 ret = 0;
1262 if (minor_version)
1263 rdev->sectors = (rdev->bdev->bd_inode->i_size >> 9) -
1264 le64_to_cpu(sb->data_offset);
1265 else
1266 rdev->sectors = rdev->sb_start;
1267 if (rdev->sectors < le64_to_cpu(sb->data_size))
1268 return -EINVAL;
1269 rdev->sectors = le64_to_cpu(sb->data_size);
1270 if (le64_to_cpu(sb->size) > rdev->sectors)
1271 return -EINVAL;
1272 return ret;
1275 static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1277 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1278 __u64 ev1 = le64_to_cpu(sb->events);
1280 rdev->raid_disk = -1;
1281 clear_bit(Faulty, &rdev->flags);
1282 clear_bit(In_sync, &rdev->flags);
1283 clear_bit(WriteMostly, &rdev->flags);
1284 clear_bit(BarriersNotsupp, &rdev->flags);
1286 if (mddev->raid_disks == 0) {
1287 mddev->major_version = 1;
1288 mddev->patch_version = 0;
1289 mddev->external = 0;
1290 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
1291 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1292 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1293 mddev->level = le32_to_cpu(sb->level);
1294 mddev->clevel[0] = 0;
1295 mddev->layout = le32_to_cpu(sb->layout);
1296 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
1297 mddev->dev_sectors = le64_to_cpu(sb->size);
1298 mddev->events = ev1;
1299 mddev->bitmap_offset = 0;
1300 mddev->default_bitmap_offset = 1024 >> 9;
1302 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1303 memcpy(mddev->uuid, sb->set_uuid, 16);
1305 mddev->max_disks = (4096-256)/2;
1307 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
1308 mddev->bitmap_file == NULL )
1309 mddev->bitmap_offset = (__s32)le32_to_cpu(sb->bitmap_offset);
1311 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1312 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1313 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1314 mddev->new_level = le32_to_cpu(sb->new_level);
1315 mddev->new_layout = le32_to_cpu(sb->new_layout);
1316 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
1317 } else {
1318 mddev->reshape_position = MaxSector;
1319 mddev->delta_disks = 0;
1320 mddev->new_level = mddev->level;
1321 mddev->new_layout = mddev->layout;
1322 mddev->new_chunk_sectors = mddev->chunk_sectors;
1325 } else if (mddev->pers == NULL) {
1326 /* Insist of good event counter while assembling */
1327 ++ev1;
1328 if (ev1 < mddev->events)
1329 return -EINVAL;
1330 } else if (mddev->bitmap) {
1331 /* If adding to array with a bitmap, then we can accept an
1332 * older device, but not too old.
1334 if (ev1 < mddev->bitmap->events_cleared)
1335 return 0;
1336 } else {
1337 if (ev1 < mddev->events)
1338 /* just a hot-add of a new device, leave raid_disk at -1 */
1339 return 0;
1341 if (mddev->level != LEVEL_MULTIPATH) {
1342 int role;
1343 if (rdev->desc_nr < 0 ||
1344 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1345 role = 0xffff;
1346 rdev->desc_nr = -1;
1347 } else
1348 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
1349 switch(role) {
1350 case 0xffff: /* spare */
1351 break;
1352 case 0xfffe: /* faulty */
1353 set_bit(Faulty, &rdev->flags);
1354 break;
1355 default:
1356 if ((le32_to_cpu(sb->feature_map) &
1357 MD_FEATURE_RECOVERY_OFFSET))
1358 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1359 else
1360 set_bit(In_sync, &rdev->flags);
1361 rdev->raid_disk = role;
1362 break;
1364 if (sb->devflags & WriteMostly1)
1365 set_bit(WriteMostly, &rdev->flags);
1366 } else /* MULTIPATH are always insync */
1367 set_bit(In_sync, &rdev->flags);
1369 return 0;
1372 static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1374 struct mdp_superblock_1 *sb;
1375 mdk_rdev_t *rdev2;
1376 int max_dev, i;
1377 /* make rdev->sb match mddev and rdev data. */
1379 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1381 sb->feature_map = 0;
1382 sb->pad0 = 0;
1383 sb->recovery_offset = cpu_to_le64(0);
1384 memset(sb->pad1, 0, sizeof(sb->pad1));
1385 memset(sb->pad2, 0, sizeof(sb->pad2));
1386 memset(sb->pad3, 0, sizeof(sb->pad3));
1388 sb->utime = cpu_to_le64((__u64)mddev->utime);
1389 sb->events = cpu_to_le64(mddev->events);
1390 if (mddev->in_sync)
1391 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1392 else
1393 sb->resync_offset = cpu_to_le64(0);
1395 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
1397 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
1398 sb->size = cpu_to_le64(mddev->dev_sectors);
1399 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
1400 sb->level = cpu_to_le32(mddev->level);
1401 sb->layout = cpu_to_le32(mddev->layout);
1403 if (mddev->bitmap && mddev->bitmap_file == NULL) {
1404 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_offset);
1405 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
1408 if (rdev->raid_disk >= 0 &&
1409 !test_bit(In_sync, &rdev->flags)) {
1410 if (rdev->recovery_offset > 0) {
1411 sb->feature_map |=
1412 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1413 sb->recovery_offset =
1414 cpu_to_le64(rdev->recovery_offset);
1418 if (mddev->reshape_position != MaxSector) {
1419 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1420 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1421 sb->new_layout = cpu_to_le32(mddev->new_layout);
1422 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1423 sb->new_level = cpu_to_le32(mddev->new_level);
1424 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
1427 max_dev = 0;
1428 list_for_each_entry(rdev2, &mddev->disks, same_set)
1429 if (rdev2->desc_nr+1 > max_dev)
1430 max_dev = rdev2->desc_nr+1;
1432 if (max_dev > le32_to_cpu(sb->max_dev)) {
1433 int bmask;
1434 sb->max_dev = cpu_to_le32(max_dev);
1435 rdev->sb_size = max_dev * 2 + 256;
1436 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1437 if (rdev->sb_size & bmask)
1438 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1440 for (i=0; i<max_dev;i++)
1441 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1443 list_for_each_entry(rdev2, &mddev->disks, same_set) {
1444 i = rdev2->desc_nr;
1445 if (test_bit(Faulty, &rdev2->flags))
1446 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1447 else if (test_bit(In_sync, &rdev2->flags))
1448 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
1449 else if (rdev2->raid_disk >= 0 && rdev2->recovery_offset > 0)
1450 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
1451 else
1452 sb->dev_roles[i] = cpu_to_le16(0xffff);
1455 sb->sb_csum = calc_sb_1_csum(sb);
1458 static unsigned long long
1459 super_1_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
1461 struct mdp_superblock_1 *sb;
1462 sector_t max_sectors;
1463 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
1464 return 0; /* component must fit device */
1465 if (rdev->sb_start < rdev->data_offset) {
1466 /* minor versions 1 and 2; superblock before data */
1467 max_sectors = rdev->bdev->bd_inode->i_size >> 9;
1468 max_sectors -= rdev->data_offset;
1469 if (!num_sectors || num_sectors > max_sectors)
1470 num_sectors = max_sectors;
1471 } else if (rdev->mddev->bitmap_offset) {
1472 /* minor version 0 with bitmap we can't move */
1473 return 0;
1474 } else {
1475 /* minor version 0; superblock after data */
1476 sector_t sb_start;
1477 sb_start = (rdev->bdev->bd_inode->i_size >> 9) - 8*2;
1478 sb_start &= ~(sector_t)(4*2 - 1);
1479 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
1480 if (!num_sectors || num_sectors > max_sectors)
1481 num_sectors = max_sectors;
1482 rdev->sb_start = sb_start;
1484 sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page);
1485 sb->data_size = cpu_to_le64(num_sectors);
1486 sb->super_offset = rdev->sb_start;
1487 sb->sb_csum = calc_sb_1_csum(sb);
1488 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
1489 rdev->sb_page);
1490 md_super_wait(rdev->mddev);
1491 return num_sectors;
1494 static struct super_type super_types[] = {
1495 [0] = {
1496 .name = "0.90.0",
1497 .owner = THIS_MODULE,
1498 .load_super = super_90_load,
1499 .validate_super = super_90_validate,
1500 .sync_super = super_90_sync,
1501 .rdev_size_change = super_90_rdev_size_change,
1503 [1] = {
1504 .name = "md-1",
1505 .owner = THIS_MODULE,
1506 .load_super = super_1_load,
1507 .validate_super = super_1_validate,
1508 .sync_super = super_1_sync,
1509 .rdev_size_change = super_1_rdev_size_change,
1513 static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1515 mdk_rdev_t *rdev, *rdev2;
1517 rcu_read_lock();
1518 rdev_for_each_rcu(rdev, mddev1)
1519 rdev_for_each_rcu(rdev2, mddev2)
1520 if (rdev->bdev->bd_contains ==
1521 rdev2->bdev->bd_contains) {
1522 rcu_read_unlock();
1523 return 1;
1525 rcu_read_unlock();
1526 return 0;
1529 static LIST_HEAD(pending_raid_disks);
1532 * Try to register data integrity profile for an mddev
1534 * This is called when an array is started and after a disk has been kicked
1535 * from the array. It only succeeds if all working and active component devices
1536 * are integrity capable with matching profiles.
1538 int md_integrity_register(mddev_t *mddev)
1540 mdk_rdev_t *rdev, *reference = NULL;
1542 if (list_empty(&mddev->disks))
1543 return 0; /* nothing to do */
1544 if (blk_get_integrity(mddev->gendisk))
1545 return 0; /* already registered */
1546 list_for_each_entry(rdev, &mddev->disks, same_set) {
1547 /* skip spares and non-functional disks */
1548 if (test_bit(Faulty, &rdev->flags))
1549 continue;
1550 if (rdev->raid_disk < 0)
1551 continue;
1553 * If at least one rdev is not integrity capable, we can not
1554 * enable data integrity for the md device.
1556 if (!bdev_get_integrity(rdev->bdev))
1557 return -EINVAL;
1558 if (!reference) {
1559 /* Use the first rdev as the reference */
1560 reference = rdev;
1561 continue;
1563 /* does this rdev's profile match the reference profile? */
1564 if (blk_integrity_compare(reference->bdev->bd_disk,
1565 rdev->bdev->bd_disk) < 0)
1566 return -EINVAL;
1569 * All component devices are integrity capable and have matching
1570 * profiles, register the common profile for the md device.
1572 if (blk_integrity_register(mddev->gendisk,
1573 bdev_get_integrity(reference->bdev)) != 0) {
1574 printk(KERN_ERR "md: failed to register integrity for %s\n",
1575 mdname(mddev));
1576 return -EINVAL;
1578 printk(KERN_NOTICE "md: data integrity on %s enabled\n",
1579 mdname(mddev));
1580 return 0;
1582 EXPORT_SYMBOL(md_integrity_register);
1584 /* Disable data integrity if non-capable/non-matching disk is being added */
1585 void md_integrity_add_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
1587 struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev);
1588 struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk);
1590 if (!bi_mddev) /* nothing to do */
1591 return;
1592 if (rdev->raid_disk < 0) /* skip spares */
1593 return;
1594 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
1595 rdev->bdev->bd_disk) >= 0)
1596 return;
1597 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
1598 blk_integrity_unregister(mddev->gendisk);
1600 EXPORT_SYMBOL(md_integrity_add_rdev);
1602 static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1604 char b[BDEVNAME_SIZE];
1605 struct kobject *ko;
1606 char *s;
1607 int err;
1609 if (rdev->mddev) {
1610 MD_BUG();
1611 return -EINVAL;
1614 /* prevent duplicates */
1615 if (find_rdev(mddev, rdev->bdev->bd_dev))
1616 return -EEXIST;
1618 /* make sure rdev->sectors exceeds mddev->dev_sectors */
1619 if (rdev->sectors && (mddev->dev_sectors == 0 ||
1620 rdev->sectors < mddev->dev_sectors)) {
1621 if (mddev->pers) {
1622 /* Cannot change size, so fail
1623 * If mddev->level <= 0, then we don't care
1624 * about aligning sizes (e.g. linear)
1626 if (mddev->level > 0)
1627 return -ENOSPC;
1628 } else
1629 mddev->dev_sectors = rdev->sectors;
1632 /* Verify rdev->desc_nr is unique.
1633 * If it is -1, assign a free number, else
1634 * check number is not in use
1636 if (rdev->desc_nr < 0) {
1637 int choice = 0;
1638 if (mddev->pers) choice = mddev->raid_disks;
1639 while (find_rdev_nr(mddev, choice))
1640 choice++;
1641 rdev->desc_nr = choice;
1642 } else {
1643 if (find_rdev_nr(mddev, rdev->desc_nr))
1644 return -EBUSY;
1646 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
1647 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
1648 mdname(mddev), mddev->max_disks);
1649 return -EBUSY;
1651 bdevname(rdev->bdev,b);
1652 while ( (s=strchr(b, '/')) != NULL)
1653 *s = '!';
1655 rdev->mddev = mddev;
1656 printk(KERN_INFO "md: bind<%s>\n", b);
1658 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
1659 goto fail;
1661 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
1662 if ((err = sysfs_create_link(&rdev->kobj, ko, "block"))) {
1663 kobject_del(&rdev->kobj);
1664 goto fail;
1666 rdev->sysfs_state = sysfs_get_dirent(rdev->kobj.sd, "state");
1668 list_add_rcu(&rdev->same_set, &mddev->disks);
1669 bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk);
1671 /* May as well allow recovery to be retried once */
1672 mddev->recovery_disabled = 0;
1674 return 0;
1676 fail:
1677 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
1678 b, mdname(mddev));
1679 return err;
1682 static void md_delayed_delete(struct work_struct *ws)
1684 mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work);
1685 kobject_del(&rdev->kobj);
1686 kobject_put(&rdev->kobj);
1689 static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1691 char b[BDEVNAME_SIZE];
1692 if (!rdev->mddev) {
1693 MD_BUG();
1694 return;
1696 bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk);
1697 list_del_rcu(&rdev->same_set);
1698 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1699 rdev->mddev = NULL;
1700 sysfs_remove_link(&rdev->kobj, "block");
1701 sysfs_put(rdev->sysfs_state);
1702 rdev->sysfs_state = NULL;
1703 /* We need to delay this, otherwise we can deadlock when
1704 * writing to 'remove' to "dev/state". We also need
1705 * to delay it due to rcu usage.
1707 synchronize_rcu();
1708 INIT_WORK(&rdev->del_work, md_delayed_delete);
1709 kobject_get(&rdev->kobj);
1710 schedule_work(&rdev->del_work);
1714 * prevent the device from being mounted, repartitioned or
1715 * otherwise reused by a RAID array (or any other kernel
1716 * subsystem), by bd_claiming the device.
1718 static int lock_rdev(mdk_rdev_t *rdev, dev_t dev, int shared)
1720 int err = 0;
1721 struct block_device *bdev;
1722 char b[BDEVNAME_SIZE];
1724 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
1725 if (IS_ERR(bdev)) {
1726 printk(KERN_ERR "md: could not open %s.\n",
1727 __bdevname(dev, b));
1728 return PTR_ERR(bdev);
1730 err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev);
1731 if (err) {
1732 printk(KERN_ERR "md: could not bd_claim %s.\n",
1733 bdevname(bdev, b));
1734 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
1735 return err;
1737 if (!shared)
1738 set_bit(AllReserved, &rdev->flags);
1739 rdev->bdev = bdev;
1740 return err;
1743 static void unlock_rdev(mdk_rdev_t *rdev)
1745 struct block_device *bdev = rdev->bdev;
1746 rdev->bdev = NULL;
1747 if (!bdev)
1748 MD_BUG();
1749 bd_release(bdev);
1750 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
1753 void md_autodetect_dev(dev_t dev);
1755 static void export_rdev(mdk_rdev_t * rdev)
1757 char b[BDEVNAME_SIZE];
1758 printk(KERN_INFO "md: export_rdev(%s)\n",
1759 bdevname(rdev->bdev,b));
1760 if (rdev->mddev)
1761 MD_BUG();
1762 free_disk_sb(rdev);
1763 #ifndef MODULE
1764 if (test_bit(AutoDetected, &rdev->flags))
1765 md_autodetect_dev(rdev->bdev->bd_dev);
1766 #endif
1767 unlock_rdev(rdev);
1768 kobject_put(&rdev->kobj);
1771 static void kick_rdev_from_array(mdk_rdev_t * rdev)
1773 unbind_rdev_from_array(rdev);
1774 export_rdev(rdev);
1777 static void export_array(mddev_t *mddev)
1779 mdk_rdev_t *rdev, *tmp;
1781 rdev_for_each(rdev, tmp, mddev) {
1782 if (!rdev->mddev) {
1783 MD_BUG();
1784 continue;
1786 kick_rdev_from_array(rdev);
1788 if (!list_empty(&mddev->disks))
1789 MD_BUG();
1790 mddev->raid_disks = 0;
1791 mddev->major_version = 0;
1794 static void print_desc(mdp_disk_t *desc)
1796 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1797 desc->major,desc->minor,desc->raid_disk,desc->state);
1800 static void print_sb_90(mdp_super_t *sb)
1802 int i;
1804 printk(KERN_INFO
1805 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1806 sb->major_version, sb->minor_version, sb->patch_version,
1807 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1808 sb->ctime);
1809 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1810 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1811 sb->md_minor, sb->layout, sb->chunk_size);
1812 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1813 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1814 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1815 sb->failed_disks, sb->spare_disks,
1816 sb->sb_csum, (unsigned long)sb->events_lo);
1818 printk(KERN_INFO);
1819 for (i = 0; i < MD_SB_DISKS; i++) {
1820 mdp_disk_t *desc;
1822 desc = sb->disks + i;
1823 if (desc->number || desc->major || desc->minor ||
1824 desc->raid_disk || (desc->state && (desc->state != 4))) {
1825 printk(" D %2d: ", i);
1826 print_desc(desc);
1829 printk(KERN_INFO "md: THIS: ");
1830 print_desc(&sb->this_disk);
1833 static void print_sb_1(struct mdp_superblock_1 *sb)
1835 __u8 *uuid;
1837 uuid = sb->set_uuid;
1838 printk(KERN_INFO
1839 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%02x%02x%02x%02x"
1840 ":%02x%02x:%02x%02x:%02x%02x:%02x%02x%02x%02x%02x%02x>\n"
1841 "md: Name: \"%s\" CT:%llu\n",
1842 le32_to_cpu(sb->major_version),
1843 le32_to_cpu(sb->feature_map),
1844 uuid[0], uuid[1], uuid[2], uuid[3],
1845 uuid[4], uuid[5], uuid[6], uuid[7],
1846 uuid[8], uuid[9], uuid[10], uuid[11],
1847 uuid[12], uuid[13], uuid[14], uuid[15],
1848 sb->set_name,
1849 (unsigned long long)le64_to_cpu(sb->ctime)
1850 & MD_SUPERBLOCK_1_TIME_SEC_MASK);
1852 uuid = sb->device_uuid;
1853 printk(KERN_INFO
1854 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
1855 " RO:%llu\n"
1856 "md: Dev:%08x UUID: %02x%02x%02x%02x:%02x%02x:%02x%02x:%02x%02x"
1857 ":%02x%02x%02x%02x%02x%02x\n"
1858 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
1859 "md: (MaxDev:%u) \n",
1860 le32_to_cpu(sb->level),
1861 (unsigned long long)le64_to_cpu(sb->size),
1862 le32_to_cpu(sb->raid_disks),
1863 le32_to_cpu(sb->layout),
1864 le32_to_cpu(sb->chunksize),
1865 (unsigned long long)le64_to_cpu(sb->data_offset),
1866 (unsigned long long)le64_to_cpu(sb->data_size),
1867 (unsigned long long)le64_to_cpu(sb->super_offset),
1868 (unsigned long long)le64_to_cpu(sb->recovery_offset),
1869 le32_to_cpu(sb->dev_number),
1870 uuid[0], uuid[1], uuid[2], uuid[3],
1871 uuid[4], uuid[5], uuid[6], uuid[7],
1872 uuid[8], uuid[9], uuid[10], uuid[11],
1873 uuid[12], uuid[13], uuid[14], uuid[15],
1874 sb->devflags,
1875 (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK,
1876 (unsigned long long)le64_to_cpu(sb->events),
1877 (unsigned long long)le64_to_cpu(sb->resync_offset),
1878 le32_to_cpu(sb->sb_csum),
1879 le32_to_cpu(sb->max_dev)
1883 static void print_rdev(mdk_rdev_t *rdev, int major_version)
1885 char b[BDEVNAME_SIZE];
1886 printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
1887 bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors,
1888 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
1889 rdev->desc_nr);
1890 if (rdev->sb_loaded) {
1891 printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version);
1892 switch (major_version) {
1893 case 0:
1894 print_sb_90((mdp_super_t*)page_address(rdev->sb_page));
1895 break;
1896 case 1:
1897 print_sb_1((struct mdp_superblock_1 *)page_address(rdev->sb_page));
1898 break;
1900 } else
1901 printk(KERN_INFO "md: no rdev superblock!\n");
1904 static void md_print_devices(void)
1906 struct list_head *tmp;
1907 mdk_rdev_t *rdev;
1908 mddev_t *mddev;
1909 char b[BDEVNAME_SIZE];
1911 printk("\n");
1912 printk("md: **********************************\n");
1913 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
1914 printk("md: **********************************\n");
1915 for_each_mddev(mddev, tmp) {
1917 if (mddev->bitmap)
1918 bitmap_print_sb(mddev->bitmap);
1919 else
1920 printk("%s: ", mdname(mddev));
1921 list_for_each_entry(rdev, &mddev->disks, same_set)
1922 printk("<%s>", bdevname(rdev->bdev,b));
1923 printk("\n");
1925 list_for_each_entry(rdev, &mddev->disks, same_set)
1926 print_rdev(rdev, mddev->major_version);
1928 printk("md: **********************************\n");
1929 printk("\n");
1933 static void sync_sbs(mddev_t * mddev, int nospares)
1935 /* Update each superblock (in-memory image), but
1936 * if we are allowed to, skip spares which already
1937 * have the right event counter, or have one earlier
1938 * (which would mean they aren't being marked as dirty
1939 * with the rest of the array)
1941 mdk_rdev_t *rdev;
1943 /* First make sure individual recovery_offsets are correct */
1944 list_for_each_entry(rdev, &mddev->disks, same_set) {
1945 if (rdev->raid_disk >= 0 &&
1946 !test_bit(In_sync, &rdev->flags) &&
1947 mddev->curr_resync_completed > rdev->recovery_offset)
1948 rdev->recovery_offset = mddev->curr_resync_completed;
1951 list_for_each_entry(rdev, &mddev->disks, same_set) {
1952 if (rdev->sb_events == mddev->events ||
1953 (nospares &&
1954 rdev->raid_disk < 0 &&
1955 (rdev->sb_events&1)==0 &&
1956 rdev->sb_events+1 == mddev->events)) {
1957 /* Don't update this superblock */
1958 rdev->sb_loaded = 2;
1959 } else {
1960 super_types[mddev->major_version].
1961 sync_super(mddev, rdev);
1962 rdev->sb_loaded = 1;
1967 static void md_update_sb(mddev_t * mddev, int force_change)
1969 mdk_rdev_t *rdev;
1970 int sync_req;
1971 int nospares = 0;
1973 mddev->utime = get_seconds();
1974 if (mddev->external)
1975 return;
1976 repeat:
1977 spin_lock_irq(&mddev->write_lock);
1979 set_bit(MD_CHANGE_PENDING, &mddev->flags);
1980 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
1981 force_change = 1;
1982 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
1983 /* just a clean<-> dirty transition, possibly leave spares alone,
1984 * though if events isn't the right even/odd, we will have to do
1985 * spares after all
1987 nospares = 1;
1988 if (force_change)
1989 nospares = 0;
1990 if (mddev->degraded)
1991 /* If the array is degraded, then skipping spares is both
1992 * dangerous and fairly pointless.
1993 * Dangerous because a device that was removed from the array
1994 * might have a event_count that still looks up-to-date,
1995 * so it can be re-added without a resync.
1996 * Pointless because if there are any spares to skip,
1997 * then a recovery will happen and soon that array won't
1998 * be degraded any more and the spare can go back to sleep then.
2000 nospares = 0;
2002 sync_req = mddev->in_sync;
2004 /* If this is just a dirty<->clean transition, and the array is clean
2005 * and 'events' is odd, we can roll back to the previous clean state */
2006 if (nospares
2007 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
2008 && (mddev->events & 1)
2009 && mddev->events != 1)
2010 mddev->events--;
2011 else {
2012 /* otherwise we have to go forward and ... */
2013 mddev->events ++;
2014 if (!mddev->in_sync || mddev->recovery_cp != MaxSector) { /* not clean */
2015 /* .. if the array isn't clean, an 'even' event must also go
2016 * to spares. */
2017 if ((mddev->events&1)==0) {
2018 nospares = 0;
2019 sync_req = 2; /* force a second update to get the
2020 * even/odd in sync */
2022 } else {
2023 /* otherwise an 'odd' event must go to spares */
2024 if ((mddev->events&1)) {
2025 nospares = 0;
2026 sync_req = 2; /* force a second update to get the
2027 * even/odd in sync */
2032 if (!mddev->events) {
2034 * oops, this 64-bit counter should never wrap.
2035 * Either we are in around ~1 trillion A.C., assuming
2036 * 1 reboot per second, or we have a bug:
2038 MD_BUG();
2039 mddev->events --;
2043 * do not write anything to disk if using
2044 * nonpersistent superblocks
2046 if (!mddev->persistent) {
2047 if (!mddev->external)
2048 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
2050 spin_unlock_irq(&mddev->write_lock);
2051 wake_up(&mddev->sb_wait);
2052 return;
2054 sync_sbs(mddev, nospares);
2055 spin_unlock_irq(&mddev->write_lock);
2057 dprintk(KERN_INFO
2058 "md: updating %s RAID superblock on device (in sync %d)\n",
2059 mdname(mddev),mddev->in_sync);
2061 bitmap_update_sb(mddev->bitmap);
2062 list_for_each_entry(rdev, &mddev->disks, same_set) {
2063 char b[BDEVNAME_SIZE];
2064 dprintk(KERN_INFO "md: ");
2065 if (rdev->sb_loaded != 1)
2066 continue; /* no noise on spare devices */
2067 if (test_bit(Faulty, &rdev->flags))
2068 dprintk("(skipping faulty ");
2070 dprintk("%s ", bdevname(rdev->bdev,b));
2071 if (!test_bit(Faulty, &rdev->flags)) {
2072 md_super_write(mddev,rdev,
2073 rdev->sb_start, rdev->sb_size,
2074 rdev->sb_page);
2075 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
2076 bdevname(rdev->bdev,b),
2077 (unsigned long long)rdev->sb_start);
2078 rdev->sb_events = mddev->events;
2080 } else
2081 dprintk(")\n");
2082 if (mddev->level == LEVEL_MULTIPATH)
2083 /* only need to write one superblock... */
2084 break;
2086 md_super_wait(mddev);
2087 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
2089 spin_lock_irq(&mddev->write_lock);
2090 if (mddev->in_sync != sync_req ||
2091 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
2092 /* have to write it out again */
2093 spin_unlock_irq(&mddev->write_lock);
2094 goto repeat;
2096 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
2097 spin_unlock_irq(&mddev->write_lock);
2098 wake_up(&mddev->sb_wait);
2099 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2100 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
2104 /* words written to sysfs files may, or may not, be \n terminated.
2105 * We want to accept with case. For this we use cmd_match.
2107 static int cmd_match(const char *cmd, const char *str)
2109 /* See if cmd, written into a sysfs file, matches
2110 * str. They must either be the same, or cmd can
2111 * have a trailing newline
2113 while (*cmd && *str && *cmd == *str) {
2114 cmd++;
2115 str++;
2117 if (*cmd == '\n')
2118 cmd++;
2119 if (*str || *cmd)
2120 return 0;
2121 return 1;
2124 struct rdev_sysfs_entry {
2125 struct attribute attr;
2126 ssize_t (*show)(mdk_rdev_t *, char *);
2127 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
2130 static ssize_t
2131 state_show(mdk_rdev_t *rdev, char *page)
2133 char *sep = "";
2134 size_t len = 0;
2136 if (test_bit(Faulty, &rdev->flags)) {
2137 len+= sprintf(page+len, "%sfaulty",sep);
2138 sep = ",";
2140 if (test_bit(In_sync, &rdev->flags)) {
2141 len += sprintf(page+len, "%sin_sync",sep);
2142 sep = ",";
2144 if (test_bit(WriteMostly, &rdev->flags)) {
2145 len += sprintf(page+len, "%swrite_mostly",sep);
2146 sep = ",";
2148 if (test_bit(Blocked, &rdev->flags)) {
2149 len += sprintf(page+len, "%sblocked", sep);
2150 sep = ",";
2152 if (!test_bit(Faulty, &rdev->flags) &&
2153 !test_bit(In_sync, &rdev->flags)) {
2154 len += sprintf(page+len, "%sspare", sep);
2155 sep = ",";
2157 return len+sprintf(page+len, "\n");
2160 static ssize_t
2161 state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2163 /* can write
2164 * faulty - simulates and error
2165 * remove - disconnects the device
2166 * writemostly - sets write_mostly
2167 * -writemostly - clears write_mostly
2168 * blocked - sets the Blocked flag
2169 * -blocked - clears the Blocked flag
2170 * insync - sets Insync providing device isn't active
2172 int err = -EINVAL;
2173 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2174 md_error(rdev->mddev, rdev);
2175 err = 0;
2176 } else if (cmd_match(buf, "remove")) {
2177 if (rdev->raid_disk >= 0)
2178 err = -EBUSY;
2179 else {
2180 mddev_t *mddev = rdev->mddev;
2181 kick_rdev_from_array(rdev);
2182 if (mddev->pers)
2183 md_update_sb(mddev, 1);
2184 md_new_event(mddev);
2185 err = 0;
2187 } else if (cmd_match(buf, "writemostly")) {
2188 set_bit(WriteMostly, &rdev->flags);
2189 err = 0;
2190 } else if (cmd_match(buf, "-writemostly")) {
2191 clear_bit(WriteMostly, &rdev->flags);
2192 err = 0;
2193 } else if (cmd_match(buf, "blocked")) {
2194 set_bit(Blocked, &rdev->flags);
2195 err = 0;
2196 } else if (cmd_match(buf, "-blocked")) {
2197 clear_bit(Blocked, &rdev->flags);
2198 wake_up(&rdev->blocked_wait);
2199 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2200 md_wakeup_thread(rdev->mddev->thread);
2202 err = 0;
2203 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2204 set_bit(In_sync, &rdev->flags);
2205 err = 0;
2207 if (!err && rdev->sysfs_state)
2208 sysfs_notify_dirent(rdev->sysfs_state);
2209 return err ? err : len;
2211 static struct rdev_sysfs_entry rdev_state =
2212 __ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
2214 static ssize_t
2215 errors_show(mdk_rdev_t *rdev, char *page)
2217 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2220 static ssize_t
2221 errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2223 char *e;
2224 unsigned long n = simple_strtoul(buf, &e, 10);
2225 if (*buf && (*e == 0 || *e == '\n')) {
2226 atomic_set(&rdev->corrected_errors, n);
2227 return len;
2229 return -EINVAL;
2231 static struct rdev_sysfs_entry rdev_errors =
2232 __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
2234 static ssize_t
2235 slot_show(mdk_rdev_t *rdev, char *page)
2237 if (rdev->raid_disk < 0)
2238 return sprintf(page, "none\n");
2239 else
2240 return sprintf(page, "%d\n", rdev->raid_disk);
2243 static ssize_t
2244 slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2246 char *e;
2247 int err;
2248 char nm[20];
2249 int slot = simple_strtoul(buf, &e, 10);
2250 if (strncmp(buf, "none", 4)==0)
2251 slot = -1;
2252 else if (e==buf || (*e && *e!= '\n'))
2253 return -EINVAL;
2254 if (rdev->mddev->pers && slot == -1) {
2255 /* Setting 'slot' on an active array requires also
2256 * updating the 'rd%d' link, and communicating
2257 * with the personality with ->hot_*_disk.
2258 * For now we only support removing
2259 * failed/spare devices. This normally happens automatically,
2260 * but not when the metadata is externally managed.
2262 if (rdev->raid_disk == -1)
2263 return -EEXIST;
2264 /* personality does all needed checks */
2265 if (rdev->mddev->pers->hot_add_disk == NULL)
2266 return -EINVAL;
2267 err = rdev->mddev->pers->
2268 hot_remove_disk(rdev->mddev, rdev->raid_disk);
2269 if (err)
2270 return err;
2271 sprintf(nm, "rd%d", rdev->raid_disk);
2272 sysfs_remove_link(&rdev->mddev->kobj, nm);
2273 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2274 md_wakeup_thread(rdev->mddev->thread);
2275 } else if (rdev->mddev->pers) {
2276 mdk_rdev_t *rdev2;
2277 /* Activating a spare .. or possibly reactivating
2278 * if we ever get bitmaps working here.
2281 if (rdev->raid_disk != -1)
2282 return -EBUSY;
2284 if (rdev->mddev->pers->hot_add_disk == NULL)
2285 return -EINVAL;
2287 list_for_each_entry(rdev2, &rdev->mddev->disks, same_set)
2288 if (rdev2->raid_disk == slot)
2289 return -EEXIST;
2291 rdev->raid_disk = slot;
2292 if (test_bit(In_sync, &rdev->flags))
2293 rdev->saved_raid_disk = slot;
2294 else
2295 rdev->saved_raid_disk = -1;
2296 err = rdev->mddev->pers->
2297 hot_add_disk(rdev->mddev, rdev);
2298 if (err) {
2299 rdev->raid_disk = -1;
2300 return err;
2301 } else
2302 sysfs_notify_dirent(rdev->sysfs_state);
2303 sprintf(nm, "rd%d", rdev->raid_disk);
2304 if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm))
2305 printk(KERN_WARNING
2306 "md: cannot register "
2307 "%s for %s\n",
2308 nm, mdname(rdev->mddev));
2310 /* don't wakeup anyone, leave that to userspace. */
2311 } else {
2312 if (slot >= rdev->mddev->raid_disks)
2313 return -ENOSPC;
2314 rdev->raid_disk = slot;
2315 /* assume it is working */
2316 clear_bit(Faulty, &rdev->flags);
2317 clear_bit(WriteMostly, &rdev->flags);
2318 set_bit(In_sync, &rdev->flags);
2319 sysfs_notify_dirent(rdev->sysfs_state);
2321 return len;
2325 static struct rdev_sysfs_entry rdev_slot =
2326 __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
2328 static ssize_t
2329 offset_show(mdk_rdev_t *rdev, char *page)
2331 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
2334 static ssize_t
2335 offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2337 char *e;
2338 unsigned long long offset = simple_strtoull(buf, &e, 10);
2339 if (e==buf || (*e && *e != '\n'))
2340 return -EINVAL;
2341 if (rdev->mddev->pers && rdev->raid_disk >= 0)
2342 return -EBUSY;
2343 if (rdev->sectors && rdev->mddev->external)
2344 /* Must set offset before size, so overlap checks
2345 * can be sane */
2346 return -EBUSY;
2347 rdev->data_offset = offset;
2348 return len;
2351 static struct rdev_sysfs_entry rdev_offset =
2352 __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
2354 static ssize_t
2355 rdev_size_show(mdk_rdev_t *rdev, char *page)
2357 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
2360 static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2362 /* check if two start/length pairs overlap */
2363 if (s1+l1 <= s2)
2364 return 0;
2365 if (s2+l2 <= s1)
2366 return 0;
2367 return 1;
2370 static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2372 unsigned long long blocks;
2373 sector_t new;
2375 if (strict_strtoull(buf, 10, &blocks) < 0)
2376 return -EINVAL;
2378 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2379 return -EINVAL; /* sector conversion overflow */
2381 new = blocks * 2;
2382 if (new != blocks * 2)
2383 return -EINVAL; /* unsigned long long to sector_t overflow */
2385 *sectors = new;
2386 return 0;
2389 static ssize_t
2390 rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2392 mddev_t *my_mddev = rdev->mddev;
2393 sector_t oldsectors = rdev->sectors;
2394 sector_t sectors;
2396 if (strict_blocks_to_sectors(buf, &sectors) < 0)
2397 return -EINVAL;
2398 if (my_mddev->pers && rdev->raid_disk >= 0) {
2399 if (my_mddev->persistent) {
2400 sectors = super_types[my_mddev->major_version].
2401 rdev_size_change(rdev, sectors);
2402 if (!sectors)
2403 return -EBUSY;
2404 } else if (!sectors)
2405 sectors = (rdev->bdev->bd_inode->i_size >> 9) -
2406 rdev->data_offset;
2408 if (sectors < my_mddev->dev_sectors)
2409 return -EINVAL; /* component must fit device */
2411 rdev->sectors = sectors;
2412 if (sectors > oldsectors && my_mddev->external) {
2413 /* need to check that all other rdevs with the same ->bdev
2414 * do not overlap. We need to unlock the mddev to avoid
2415 * a deadlock. We have already changed rdev->sectors, and if
2416 * we have to change it back, we will have the lock again.
2418 mddev_t *mddev;
2419 int overlap = 0;
2420 struct list_head *tmp;
2422 mddev_unlock(my_mddev);
2423 for_each_mddev(mddev, tmp) {
2424 mdk_rdev_t *rdev2;
2426 mddev_lock(mddev);
2427 list_for_each_entry(rdev2, &mddev->disks, same_set)
2428 if (test_bit(AllReserved, &rdev2->flags) ||
2429 (rdev->bdev == rdev2->bdev &&
2430 rdev != rdev2 &&
2431 overlaps(rdev->data_offset, rdev->sectors,
2432 rdev2->data_offset,
2433 rdev2->sectors))) {
2434 overlap = 1;
2435 break;
2437 mddev_unlock(mddev);
2438 if (overlap) {
2439 mddev_put(mddev);
2440 break;
2443 mddev_lock(my_mddev);
2444 if (overlap) {
2445 /* Someone else could have slipped in a size
2446 * change here, but doing so is just silly.
2447 * We put oldsectors back because we *know* it is
2448 * safe, and trust userspace not to race with
2449 * itself
2451 rdev->sectors = oldsectors;
2452 return -EBUSY;
2455 return len;
2458 static struct rdev_sysfs_entry rdev_size =
2459 __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
2461 static struct attribute *rdev_default_attrs[] = {
2462 &rdev_state.attr,
2463 &rdev_errors.attr,
2464 &rdev_slot.attr,
2465 &rdev_offset.attr,
2466 &rdev_size.attr,
2467 NULL,
2469 static ssize_t
2470 rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2472 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2473 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
2474 mddev_t *mddev = rdev->mddev;
2475 ssize_t rv;
2477 if (!entry->show)
2478 return -EIO;
2480 rv = mddev ? mddev_lock(mddev) : -EBUSY;
2481 if (!rv) {
2482 if (rdev->mddev == NULL)
2483 rv = -EBUSY;
2484 else
2485 rv = entry->show(rdev, page);
2486 mddev_unlock(mddev);
2488 return rv;
2491 static ssize_t
2492 rdev_attr_store(struct kobject *kobj, struct attribute *attr,
2493 const char *page, size_t length)
2495 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2496 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
2497 ssize_t rv;
2498 mddev_t *mddev = rdev->mddev;
2500 if (!entry->store)
2501 return -EIO;
2502 if (!capable(CAP_SYS_ADMIN))
2503 return -EACCES;
2504 rv = mddev ? mddev_lock(mddev): -EBUSY;
2505 if (!rv) {
2506 if (rdev->mddev == NULL)
2507 rv = -EBUSY;
2508 else
2509 rv = entry->store(rdev, page, length);
2510 mddev_unlock(mddev);
2512 return rv;
2515 static void rdev_free(struct kobject *ko)
2517 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
2518 kfree(rdev);
2520 static struct sysfs_ops rdev_sysfs_ops = {
2521 .show = rdev_attr_show,
2522 .store = rdev_attr_store,
2524 static struct kobj_type rdev_ktype = {
2525 .release = rdev_free,
2526 .sysfs_ops = &rdev_sysfs_ops,
2527 .default_attrs = rdev_default_attrs,
2531 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2533 * mark the device faulty if:
2535 * - the device is nonexistent (zero size)
2536 * - the device has no valid superblock
2538 * a faulty rdev _never_ has rdev->sb set.
2540 static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
2542 char b[BDEVNAME_SIZE];
2543 int err;
2544 mdk_rdev_t *rdev;
2545 sector_t size;
2547 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
2548 if (!rdev) {
2549 printk(KERN_ERR "md: could not alloc mem for new device!\n");
2550 return ERR_PTR(-ENOMEM);
2553 if ((err = alloc_disk_sb(rdev)))
2554 goto abort_free;
2556 err = lock_rdev(rdev, newdev, super_format == -2);
2557 if (err)
2558 goto abort_free;
2560 kobject_init(&rdev->kobj, &rdev_ktype);
2562 rdev->desc_nr = -1;
2563 rdev->saved_raid_disk = -1;
2564 rdev->raid_disk = -1;
2565 rdev->flags = 0;
2566 rdev->data_offset = 0;
2567 rdev->sb_events = 0;
2568 atomic_set(&rdev->nr_pending, 0);
2569 atomic_set(&rdev->read_errors, 0);
2570 atomic_set(&rdev->corrected_errors, 0);
2572 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2573 if (!size) {
2574 printk(KERN_WARNING
2575 "md: %s has zero or unknown size, marking faulty!\n",
2576 bdevname(rdev->bdev,b));
2577 err = -EINVAL;
2578 goto abort_free;
2581 if (super_format >= 0) {
2582 err = super_types[super_format].
2583 load_super(rdev, NULL, super_minor);
2584 if (err == -EINVAL) {
2585 printk(KERN_WARNING
2586 "md: %s does not have a valid v%d.%d "
2587 "superblock, not importing!\n",
2588 bdevname(rdev->bdev,b),
2589 super_format, super_minor);
2590 goto abort_free;
2592 if (err < 0) {
2593 printk(KERN_WARNING
2594 "md: could not read %s's sb, not importing!\n",
2595 bdevname(rdev->bdev,b));
2596 goto abort_free;
2600 INIT_LIST_HEAD(&rdev->same_set);
2601 init_waitqueue_head(&rdev->blocked_wait);
2603 return rdev;
2605 abort_free:
2606 if (rdev->sb_page) {
2607 if (rdev->bdev)
2608 unlock_rdev(rdev);
2609 free_disk_sb(rdev);
2611 kfree(rdev);
2612 return ERR_PTR(err);
2616 * Check a full RAID array for plausibility
2620 static void analyze_sbs(mddev_t * mddev)
2622 int i;
2623 mdk_rdev_t *rdev, *freshest, *tmp;
2624 char b[BDEVNAME_SIZE];
2626 freshest = NULL;
2627 rdev_for_each(rdev, tmp, mddev)
2628 switch (super_types[mddev->major_version].
2629 load_super(rdev, freshest, mddev->minor_version)) {
2630 case 1:
2631 freshest = rdev;
2632 break;
2633 case 0:
2634 break;
2635 default:
2636 printk( KERN_ERR \
2637 "md: fatal superblock inconsistency in %s"
2638 " -- removing from array\n",
2639 bdevname(rdev->bdev,b));
2640 kick_rdev_from_array(rdev);
2644 super_types[mddev->major_version].
2645 validate_super(mddev, freshest);
2647 i = 0;
2648 rdev_for_each(rdev, tmp, mddev) {
2649 if (rdev->desc_nr >= mddev->max_disks ||
2650 i > mddev->max_disks) {
2651 printk(KERN_WARNING
2652 "md: %s: %s: only %d devices permitted\n",
2653 mdname(mddev), bdevname(rdev->bdev, b),
2654 mddev->max_disks);
2655 kick_rdev_from_array(rdev);
2656 continue;
2658 if (rdev != freshest)
2659 if (super_types[mddev->major_version].
2660 validate_super(mddev, rdev)) {
2661 printk(KERN_WARNING "md: kicking non-fresh %s"
2662 " from array!\n",
2663 bdevname(rdev->bdev,b));
2664 kick_rdev_from_array(rdev);
2665 continue;
2667 if (mddev->level == LEVEL_MULTIPATH) {
2668 rdev->desc_nr = i++;
2669 rdev->raid_disk = rdev->desc_nr;
2670 set_bit(In_sync, &rdev->flags);
2671 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
2672 rdev->raid_disk = -1;
2673 clear_bit(In_sync, &rdev->flags);
2678 static void md_safemode_timeout(unsigned long data);
2680 static ssize_t
2681 safe_delay_show(mddev_t *mddev, char *page)
2683 int msec = (mddev->safemode_delay*1000)/HZ;
2684 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
2686 static ssize_t
2687 safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
2689 int scale=1;
2690 int dot=0;
2691 int i;
2692 unsigned long msec;
2693 char buf[30];
2695 /* remove a period, and count digits after it */
2696 if (len >= sizeof(buf))
2697 return -EINVAL;
2698 strlcpy(buf, cbuf, sizeof(buf));
2699 for (i=0; i<len; i++) {
2700 if (dot) {
2701 if (isdigit(buf[i])) {
2702 buf[i-1] = buf[i];
2703 scale *= 10;
2705 buf[i] = 0;
2706 } else if (buf[i] == '.') {
2707 dot=1;
2708 buf[i] = 0;
2711 if (strict_strtoul(buf, 10, &msec) < 0)
2712 return -EINVAL;
2713 msec = (msec * 1000) / scale;
2714 if (msec == 0)
2715 mddev->safemode_delay = 0;
2716 else {
2717 unsigned long old_delay = mddev->safemode_delay;
2718 mddev->safemode_delay = (msec*HZ)/1000;
2719 if (mddev->safemode_delay == 0)
2720 mddev->safemode_delay = 1;
2721 if (mddev->safemode_delay < old_delay)
2722 md_safemode_timeout((unsigned long)mddev);
2724 return len;
2726 static struct md_sysfs_entry md_safe_delay =
2727 __ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
2729 static ssize_t
2730 level_show(mddev_t *mddev, char *page)
2732 struct mdk_personality *p = mddev->pers;
2733 if (p)
2734 return sprintf(page, "%s\n", p->name);
2735 else if (mddev->clevel[0])
2736 return sprintf(page, "%s\n", mddev->clevel);
2737 else if (mddev->level != LEVEL_NONE)
2738 return sprintf(page, "%d\n", mddev->level);
2739 else
2740 return 0;
2743 static ssize_t
2744 level_store(mddev_t *mddev, const char *buf, size_t len)
2746 char level[16];
2747 ssize_t rv = len;
2748 struct mdk_personality *pers;
2749 void *priv;
2750 mdk_rdev_t *rdev;
2752 if (mddev->pers == NULL) {
2753 if (len == 0)
2754 return 0;
2755 if (len >= sizeof(mddev->clevel))
2756 return -ENOSPC;
2757 strncpy(mddev->clevel, buf, len);
2758 if (mddev->clevel[len-1] == '\n')
2759 len--;
2760 mddev->clevel[len] = 0;
2761 mddev->level = LEVEL_NONE;
2762 return rv;
2765 /* request to change the personality. Need to ensure:
2766 * - array is not engaged in resync/recovery/reshape
2767 * - old personality can be suspended
2768 * - new personality will access other array.
2771 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
2772 return -EBUSY;
2774 if (!mddev->pers->quiesce) {
2775 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
2776 mdname(mddev), mddev->pers->name);
2777 return -EINVAL;
2780 /* Now find the new personality */
2781 if (len == 0 || len >= sizeof(level))
2782 return -EINVAL;
2783 strncpy(level, buf, len);
2784 if (level[len-1] == '\n')
2785 len--;
2786 level[len] = 0;
2788 request_module("md-%s", level);
2789 spin_lock(&pers_lock);
2790 pers = find_pers(LEVEL_NONE, level);
2791 if (!pers || !try_module_get(pers->owner)) {
2792 spin_unlock(&pers_lock);
2793 printk(KERN_WARNING "md: personality %s not loaded\n", level);
2794 return -EINVAL;
2796 spin_unlock(&pers_lock);
2798 if (pers == mddev->pers) {
2799 /* Nothing to do! */
2800 module_put(pers->owner);
2801 return rv;
2803 if (!pers->takeover) {
2804 module_put(pers->owner);
2805 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
2806 mdname(mddev), level);
2807 return -EINVAL;
2810 /* ->takeover must set new_* and/or delta_disks
2811 * if it succeeds, and may set them when it fails.
2813 priv = pers->takeover(mddev);
2814 if (IS_ERR(priv)) {
2815 mddev->new_level = mddev->level;
2816 mddev->new_layout = mddev->layout;
2817 mddev->new_chunk_sectors = mddev->chunk_sectors;
2818 mddev->raid_disks -= mddev->delta_disks;
2819 mddev->delta_disks = 0;
2820 module_put(pers->owner);
2821 printk(KERN_WARNING "md: %s: %s would not accept array\n",
2822 mdname(mddev), level);
2823 return PTR_ERR(priv);
2826 /* Looks like we have a winner */
2827 mddev_suspend(mddev);
2828 mddev->pers->stop(mddev);
2829 module_put(mddev->pers->owner);
2830 /* Invalidate devices that are now superfluous */
2831 list_for_each_entry(rdev, &mddev->disks, same_set)
2832 if (rdev->raid_disk >= mddev->raid_disks) {
2833 rdev->raid_disk = -1;
2834 clear_bit(In_sync, &rdev->flags);
2836 mddev->pers = pers;
2837 mddev->private = priv;
2838 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
2839 mddev->level = mddev->new_level;
2840 mddev->layout = mddev->new_layout;
2841 mddev->chunk_sectors = mddev->new_chunk_sectors;
2842 mddev->delta_disks = 0;
2843 pers->run(mddev);
2844 mddev_resume(mddev);
2845 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2846 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2847 md_wakeup_thread(mddev->thread);
2848 return rv;
2851 static struct md_sysfs_entry md_level =
2852 __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
2855 static ssize_t
2856 layout_show(mddev_t *mddev, char *page)
2858 /* just a number, not meaningful for all levels */
2859 if (mddev->reshape_position != MaxSector &&
2860 mddev->layout != mddev->new_layout)
2861 return sprintf(page, "%d (%d)\n",
2862 mddev->new_layout, mddev->layout);
2863 return sprintf(page, "%d\n", mddev->layout);
2866 static ssize_t
2867 layout_store(mddev_t *mddev, const char *buf, size_t len)
2869 char *e;
2870 unsigned long n = simple_strtoul(buf, &e, 10);
2872 if (!*buf || (*e && *e != '\n'))
2873 return -EINVAL;
2875 if (mddev->pers) {
2876 int err;
2877 if (mddev->pers->check_reshape == NULL)
2878 return -EBUSY;
2879 mddev->new_layout = n;
2880 err = mddev->pers->check_reshape(mddev);
2881 if (err) {
2882 mddev->new_layout = mddev->layout;
2883 return err;
2885 } else {
2886 mddev->new_layout = n;
2887 if (mddev->reshape_position == MaxSector)
2888 mddev->layout = n;
2890 return len;
2892 static struct md_sysfs_entry md_layout =
2893 __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
2896 static ssize_t
2897 raid_disks_show(mddev_t *mddev, char *page)
2899 if (mddev->raid_disks == 0)
2900 return 0;
2901 if (mddev->reshape_position != MaxSector &&
2902 mddev->delta_disks != 0)
2903 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
2904 mddev->raid_disks - mddev->delta_disks);
2905 return sprintf(page, "%d\n", mddev->raid_disks);
2908 static int update_raid_disks(mddev_t *mddev, int raid_disks);
2910 static ssize_t
2911 raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
2913 char *e;
2914 int rv = 0;
2915 unsigned long n = simple_strtoul(buf, &e, 10);
2917 if (!*buf || (*e && *e != '\n'))
2918 return -EINVAL;
2920 if (mddev->pers)
2921 rv = update_raid_disks(mddev, n);
2922 else if (mddev->reshape_position != MaxSector) {
2923 int olddisks = mddev->raid_disks - mddev->delta_disks;
2924 mddev->delta_disks = n - olddisks;
2925 mddev->raid_disks = n;
2926 } else
2927 mddev->raid_disks = n;
2928 return rv ? rv : len;
2930 static struct md_sysfs_entry md_raid_disks =
2931 __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
2933 static ssize_t
2934 chunk_size_show(mddev_t *mddev, char *page)
2936 if (mddev->reshape_position != MaxSector &&
2937 mddev->chunk_sectors != mddev->new_chunk_sectors)
2938 return sprintf(page, "%d (%d)\n",
2939 mddev->new_chunk_sectors << 9,
2940 mddev->chunk_sectors << 9);
2941 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
2944 static ssize_t
2945 chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
2947 char *e;
2948 unsigned long n = simple_strtoul(buf, &e, 10);
2950 if (!*buf || (*e && *e != '\n'))
2951 return -EINVAL;
2953 if (mddev->pers) {
2954 int err;
2955 if (mddev->pers->check_reshape == NULL)
2956 return -EBUSY;
2957 mddev->new_chunk_sectors = n >> 9;
2958 err = mddev->pers->check_reshape(mddev);
2959 if (err) {
2960 mddev->new_chunk_sectors = mddev->chunk_sectors;
2961 return err;
2963 } else {
2964 mddev->new_chunk_sectors = n >> 9;
2965 if (mddev->reshape_position == MaxSector)
2966 mddev->chunk_sectors = n >> 9;
2968 return len;
2970 static struct md_sysfs_entry md_chunk_size =
2971 __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
2973 static ssize_t
2974 resync_start_show(mddev_t *mddev, char *page)
2976 if (mddev->recovery_cp == MaxSector)
2977 return sprintf(page, "none\n");
2978 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
2981 static ssize_t
2982 resync_start_store(mddev_t *mddev, const char *buf, size_t len)
2984 char *e;
2985 unsigned long long n = simple_strtoull(buf, &e, 10);
2987 if (mddev->pers)
2988 return -EBUSY;
2989 if (!*buf || (*e && *e != '\n'))
2990 return -EINVAL;
2992 mddev->recovery_cp = n;
2993 return len;
2995 static struct md_sysfs_entry md_resync_start =
2996 __ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
2999 * The array state can be:
3001 * clear
3002 * No devices, no size, no level
3003 * Equivalent to STOP_ARRAY ioctl
3004 * inactive
3005 * May have some settings, but array is not active
3006 * all IO results in error
3007 * When written, doesn't tear down array, but just stops it
3008 * suspended (not supported yet)
3009 * All IO requests will block. The array can be reconfigured.
3010 * Writing this, if accepted, will block until array is quiescent
3011 * readonly
3012 * no resync can happen. no superblocks get written.
3013 * write requests fail
3014 * read-auto
3015 * like readonly, but behaves like 'clean' on a write request.
3017 * clean - no pending writes, but otherwise active.
3018 * When written to inactive array, starts without resync
3019 * If a write request arrives then
3020 * if metadata is known, mark 'dirty' and switch to 'active'.
3021 * if not known, block and switch to write-pending
3022 * If written to an active array that has pending writes, then fails.
3023 * active
3024 * fully active: IO and resync can be happening.
3025 * When written to inactive array, starts with resync
3027 * write-pending
3028 * clean, but writes are blocked waiting for 'active' to be written.
3030 * active-idle
3031 * like active, but no writes have been seen for a while (100msec).
3034 enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3035 write_pending, active_idle, bad_word};
3036 static char *array_states[] = {
3037 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3038 "write-pending", "active-idle", NULL };
3040 static int match_word(const char *word, char **list)
3042 int n;
3043 for (n=0; list[n]; n++)
3044 if (cmd_match(word, list[n]))
3045 break;
3046 return n;
3049 static ssize_t
3050 array_state_show(mddev_t *mddev, char *page)
3052 enum array_state st = inactive;
3054 if (mddev->pers)
3055 switch(mddev->ro) {
3056 case 1:
3057 st = readonly;
3058 break;
3059 case 2:
3060 st = read_auto;
3061 break;
3062 case 0:
3063 if (mddev->in_sync)
3064 st = clean;
3065 else if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
3066 st = write_pending;
3067 else if (mddev->safemode)
3068 st = active_idle;
3069 else
3070 st = active;
3072 else {
3073 if (list_empty(&mddev->disks) &&
3074 mddev->raid_disks == 0 &&
3075 mddev->dev_sectors == 0)
3076 st = clear;
3077 else
3078 st = inactive;
3080 return sprintf(page, "%s\n", array_states[st]);
3083 static int do_md_stop(mddev_t * mddev, int ro, int is_open);
3084 static int do_md_run(mddev_t * mddev);
3085 static int restart_array(mddev_t *mddev);
3087 static ssize_t
3088 array_state_store(mddev_t *mddev, const char *buf, size_t len)
3090 int err = -EINVAL;
3091 enum array_state st = match_word(buf, array_states);
3092 switch(st) {
3093 case bad_word:
3094 break;
3095 case clear:
3096 /* stopping an active array */
3097 if (atomic_read(&mddev->openers) > 0)
3098 return -EBUSY;
3099 err = do_md_stop(mddev, 0, 0);
3100 break;
3101 case inactive:
3102 /* stopping an active array */
3103 if (mddev->pers) {
3104 if (atomic_read(&mddev->openers) > 0)
3105 return -EBUSY;
3106 err = do_md_stop(mddev, 2, 0);
3107 } else
3108 err = 0; /* already inactive */
3109 break;
3110 case suspended:
3111 break; /* not supported yet */
3112 case readonly:
3113 if (mddev->pers)
3114 err = do_md_stop(mddev, 1, 0);
3115 else {
3116 mddev->ro = 1;
3117 set_disk_ro(mddev->gendisk, 1);
3118 err = do_md_run(mddev);
3120 break;
3121 case read_auto:
3122 if (mddev->pers) {
3123 if (mddev->ro == 0)
3124 err = do_md_stop(mddev, 1, 0);
3125 else if (mddev->ro == 1)
3126 err = restart_array(mddev);
3127 if (err == 0) {
3128 mddev->ro = 2;
3129 set_disk_ro(mddev->gendisk, 0);
3131 } else {
3132 mddev->ro = 2;
3133 err = do_md_run(mddev);
3135 break;
3136 case clean:
3137 if (mddev->pers) {
3138 restart_array(mddev);
3139 spin_lock_irq(&mddev->write_lock);
3140 if (atomic_read(&mddev->writes_pending) == 0) {
3141 if (mddev->in_sync == 0) {
3142 mddev->in_sync = 1;
3143 if (mddev->safemode == 1)
3144 mddev->safemode = 0;
3145 if (mddev->persistent)
3146 set_bit(MD_CHANGE_CLEAN,
3147 &mddev->flags);
3149 err = 0;
3150 } else
3151 err = -EBUSY;
3152 spin_unlock_irq(&mddev->write_lock);
3153 } else
3154 err = -EINVAL;
3155 break;
3156 case active:
3157 if (mddev->pers) {
3158 restart_array(mddev);
3159 if (mddev->external)
3160 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
3161 wake_up(&mddev->sb_wait);
3162 err = 0;
3163 } else {
3164 mddev->ro = 0;
3165 set_disk_ro(mddev->gendisk, 0);
3166 err = do_md_run(mddev);
3168 break;
3169 case write_pending:
3170 case active_idle:
3171 /* these cannot be set */
3172 break;
3174 if (err)
3175 return err;
3176 else {
3177 sysfs_notify_dirent(mddev->sysfs_state);
3178 return len;
3181 static struct md_sysfs_entry md_array_state =
3182 __ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
3184 static ssize_t
3185 null_show(mddev_t *mddev, char *page)
3187 return -EINVAL;
3190 static ssize_t
3191 new_dev_store(mddev_t *mddev, const char *buf, size_t len)
3193 /* buf must be %d:%d\n? giving major and minor numbers */
3194 /* The new device is added to the array.
3195 * If the array has a persistent superblock, we read the
3196 * superblock to initialise info and check validity.
3197 * Otherwise, only checking done is that in bind_rdev_to_array,
3198 * which mainly checks size.
3200 char *e;
3201 int major = simple_strtoul(buf, &e, 10);
3202 int minor;
3203 dev_t dev;
3204 mdk_rdev_t *rdev;
3205 int err;
3207 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
3208 return -EINVAL;
3209 minor = simple_strtoul(e+1, &e, 10);
3210 if (*e && *e != '\n')
3211 return -EINVAL;
3212 dev = MKDEV(major, minor);
3213 if (major != MAJOR(dev) ||
3214 minor != MINOR(dev))
3215 return -EOVERFLOW;
3218 if (mddev->persistent) {
3219 rdev = md_import_device(dev, mddev->major_version,
3220 mddev->minor_version);
3221 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
3222 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
3223 mdk_rdev_t, same_set);
3224 err = super_types[mddev->major_version]
3225 .load_super(rdev, rdev0, mddev->minor_version);
3226 if (err < 0)
3227 goto out;
3229 } else if (mddev->external)
3230 rdev = md_import_device(dev, -2, -1);
3231 else
3232 rdev = md_import_device(dev, -1, -1);
3234 if (IS_ERR(rdev))
3235 return PTR_ERR(rdev);
3236 err = bind_rdev_to_array(rdev, mddev);
3237 out:
3238 if (err)
3239 export_rdev(rdev);
3240 return err ? err : len;
3243 static struct md_sysfs_entry md_new_device =
3244 __ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
3246 static ssize_t
3247 bitmap_store(mddev_t *mddev, const char *buf, size_t len)
3249 char *end;
3250 unsigned long chunk, end_chunk;
3252 if (!mddev->bitmap)
3253 goto out;
3254 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3255 while (*buf) {
3256 chunk = end_chunk = simple_strtoul(buf, &end, 0);
3257 if (buf == end) break;
3258 if (*end == '-') { /* range */
3259 buf = end + 1;
3260 end_chunk = simple_strtoul(buf, &end, 0);
3261 if (buf == end) break;
3263 if (*end && !isspace(*end)) break;
3264 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
3265 buf = end;
3266 while (isspace(*buf)) buf++;
3268 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
3269 out:
3270 return len;
3273 static struct md_sysfs_entry md_bitmap =
3274 __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
3276 static ssize_t
3277 size_show(mddev_t *mddev, char *page)
3279 return sprintf(page, "%llu\n",
3280 (unsigned long long)mddev->dev_sectors / 2);
3283 static int update_size(mddev_t *mddev, sector_t num_sectors);
3285 static ssize_t
3286 size_store(mddev_t *mddev, const char *buf, size_t len)
3288 /* If array is inactive, we can reduce the component size, but
3289 * not increase it (except from 0).
3290 * If array is active, we can try an on-line resize
3292 sector_t sectors;
3293 int err = strict_blocks_to_sectors(buf, &sectors);
3295 if (err < 0)
3296 return err;
3297 if (mddev->pers) {
3298 err = update_size(mddev, sectors);
3299 md_update_sb(mddev, 1);
3300 } else {
3301 if (mddev->dev_sectors == 0 ||
3302 mddev->dev_sectors > sectors)
3303 mddev->dev_sectors = sectors;
3304 else
3305 err = -ENOSPC;
3307 return err ? err : len;
3310 static struct md_sysfs_entry md_size =
3311 __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
3314 /* Metdata version.
3315 * This is one of
3316 * 'none' for arrays with no metadata (good luck...)
3317 * 'external' for arrays with externally managed metadata,
3318 * or N.M for internally known formats
3320 static ssize_t
3321 metadata_show(mddev_t *mddev, char *page)
3323 if (mddev->persistent)
3324 return sprintf(page, "%d.%d\n",
3325 mddev->major_version, mddev->minor_version);
3326 else if (mddev->external)
3327 return sprintf(page, "external:%s\n", mddev->metadata_type);
3328 else
3329 return sprintf(page, "none\n");
3332 static ssize_t
3333 metadata_store(mddev_t *mddev, const char *buf, size_t len)
3335 int major, minor;
3336 char *e;
3337 /* Changing the details of 'external' metadata is
3338 * always permitted. Otherwise there must be
3339 * no devices attached to the array.
3341 if (mddev->external && strncmp(buf, "external:", 9) == 0)
3343 else if (!list_empty(&mddev->disks))
3344 return -EBUSY;
3346 if (cmd_match(buf, "none")) {
3347 mddev->persistent = 0;
3348 mddev->external = 0;
3349 mddev->major_version = 0;
3350 mddev->minor_version = 90;
3351 return len;
3353 if (strncmp(buf, "external:", 9) == 0) {
3354 size_t namelen = len-9;
3355 if (namelen >= sizeof(mddev->metadata_type))
3356 namelen = sizeof(mddev->metadata_type)-1;
3357 strncpy(mddev->metadata_type, buf+9, namelen);
3358 mddev->metadata_type[namelen] = 0;
3359 if (namelen && mddev->metadata_type[namelen-1] == '\n')
3360 mddev->metadata_type[--namelen] = 0;
3361 mddev->persistent = 0;
3362 mddev->external = 1;
3363 mddev->major_version = 0;
3364 mddev->minor_version = 90;
3365 return len;
3367 major = simple_strtoul(buf, &e, 10);
3368 if (e==buf || *e != '.')
3369 return -EINVAL;
3370 buf = e+1;
3371 minor = simple_strtoul(buf, &e, 10);
3372 if (e==buf || (*e && *e != '\n') )
3373 return -EINVAL;
3374 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
3375 return -ENOENT;
3376 mddev->major_version = major;
3377 mddev->minor_version = minor;
3378 mddev->persistent = 1;
3379 mddev->external = 0;
3380 return len;
3383 static struct md_sysfs_entry md_metadata =
3384 __ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
3386 static ssize_t
3387 action_show(mddev_t *mddev, char *page)
3389 char *type = "idle";
3390 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
3391 type = "frozen";
3392 else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3393 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
3394 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
3395 type = "reshape";
3396 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
3397 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
3398 type = "resync";
3399 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
3400 type = "check";
3401 else
3402 type = "repair";
3403 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
3404 type = "recover";
3406 return sprintf(page, "%s\n", type);
3409 static ssize_t
3410 action_store(mddev_t *mddev, const char *page, size_t len)
3412 if (!mddev->pers || !mddev->pers->sync_request)
3413 return -EINVAL;
3415 if (cmd_match(page, "frozen"))
3416 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3417 else
3418 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3420 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
3421 if (mddev->sync_thread) {
3422 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3423 md_unregister_thread(mddev->sync_thread);
3424 mddev->sync_thread = NULL;
3425 mddev->recovery = 0;
3427 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3428 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
3429 return -EBUSY;
3430 else if (cmd_match(page, "resync"))
3431 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3432 else if (cmd_match(page, "recover")) {
3433 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
3434 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3435 } else if (cmd_match(page, "reshape")) {
3436 int err;
3437 if (mddev->pers->start_reshape == NULL)
3438 return -EINVAL;
3439 err = mddev->pers->start_reshape(mddev);
3440 if (err)
3441 return err;
3442 sysfs_notify(&mddev->kobj, NULL, "degraded");
3443 } else {
3444 if (cmd_match(page, "check"))
3445 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
3446 else if (!cmd_match(page, "repair"))
3447 return -EINVAL;
3448 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
3449 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
3451 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3452 md_wakeup_thread(mddev->thread);
3453 sysfs_notify_dirent(mddev->sysfs_action);
3454 return len;
3457 static ssize_t
3458 mismatch_cnt_show(mddev_t *mddev, char *page)
3460 return sprintf(page, "%llu\n",
3461 (unsigned long long) mddev->resync_mismatches);
3464 static struct md_sysfs_entry md_scan_mode =
3465 __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
3468 static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
3470 static ssize_t
3471 sync_min_show(mddev_t *mddev, char *page)
3473 return sprintf(page, "%d (%s)\n", speed_min(mddev),
3474 mddev->sync_speed_min ? "local": "system");
3477 static ssize_t
3478 sync_min_store(mddev_t *mddev, const char *buf, size_t len)
3480 int min;
3481 char *e;
3482 if (strncmp(buf, "system", 6)==0) {
3483 mddev->sync_speed_min = 0;
3484 return len;
3486 min = simple_strtoul(buf, &e, 10);
3487 if (buf == e || (*e && *e != '\n') || min <= 0)
3488 return -EINVAL;
3489 mddev->sync_speed_min = min;
3490 return len;
3493 static struct md_sysfs_entry md_sync_min =
3494 __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
3496 static ssize_t
3497 sync_max_show(mddev_t *mddev, char *page)
3499 return sprintf(page, "%d (%s)\n", speed_max(mddev),
3500 mddev->sync_speed_max ? "local": "system");
3503 static ssize_t
3504 sync_max_store(mddev_t *mddev, const char *buf, size_t len)
3506 int max;
3507 char *e;
3508 if (strncmp(buf, "system", 6)==0) {
3509 mddev->sync_speed_max = 0;
3510 return len;
3512 max = simple_strtoul(buf, &e, 10);
3513 if (buf == e || (*e && *e != '\n') || max <= 0)
3514 return -EINVAL;
3515 mddev->sync_speed_max = max;
3516 return len;
3519 static struct md_sysfs_entry md_sync_max =
3520 __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
3522 static ssize_t
3523 degraded_show(mddev_t *mddev, char *page)
3525 return sprintf(page, "%d\n", mddev->degraded);
3527 static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
3529 static ssize_t
3530 sync_force_parallel_show(mddev_t *mddev, char *page)
3532 return sprintf(page, "%d\n", mddev->parallel_resync);
3535 static ssize_t
3536 sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len)
3538 long n;
3540 if (strict_strtol(buf, 10, &n))
3541 return -EINVAL;
3543 if (n != 0 && n != 1)
3544 return -EINVAL;
3546 mddev->parallel_resync = n;
3548 if (mddev->sync_thread)
3549 wake_up(&resync_wait);
3551 return len;
3554 /* force parallel resync, even with shared block devices */
3555 static struct md_sysfs_entry md_sync_force_parallel =
3556 __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
3557 sync_force_parallel_show, sync_force_parallel_store);
3559 static ssize_t
3560 sync_speed_show(mddev_t *mddev, char *page)
3562 unsigned long resync, dt, db;
3563 if (mddev->curr_resync == 0)
3564 return sprintf(page, "none\n");
3565 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
3566 dt = (jiffies - mddev->resync_mark) / HZ;
3567 if (!dt) dt++;
3568 db = resync - mddev->resync_mark_cnt;
3569 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
3572 static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
3574 static ssize_t
3575 sync_completed_show(mddev_t *mddev, char *page)
3577 unsigned long max_sectors, resync;
3579 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3580 return sprintf(page, "none\n");
3582 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
3583 max_sectors = mddev->resync_max_sectors;
3584 else
3585 max_sectors = mddev->dev_sectors;
3587 resync = mddev->curr_resync_completed;
3588 return sprintf(page, "%lu / %lu\n", resync, max_sectors);
3591 static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
3593 static ssize_t
3594 min_sync_show(mddev_t *mddev, char *page)
3596 return sprintf(page, "%llu\n",
3597 (unsigned long long)mddev->resync_min);
3599 static ssize_t
3600 min_sync_store(mddev_t *mddev, const char *buf, size_t len)
3602 unsigned long long min;
3603 if (strict_strtoull(buf, 10, &min))
3604 return -EINVAL;
3605 if (min > mddev->resync_max)
3606 return -EINVAL;
3607 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3608 return -EBUSY;
3610 /* Must be a multiple of chunk_size */
3611 if (mddev->chunk_sectors) {
3612 sector_t temp = min;
3613 if (sector_div(temp, mddev->chunk_sectors))
3614 return -EINVAL;
3616 mddev->resync_min = min;
3618 return len;
3621 static struct md_sysfs_entry md_min_sync =
3622 __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
3624 static ssize_t
3625 max_sync_show(mddev_t *mddev, char *page)
3627 if (mddev->resync_max == MaxSector)
3628 return sprintf(page, "max\n");
3629 else
3630 return sprintf(page, "%llu\n",
3631 (unsigned long long)mddev->resync_max);
3633 static ssize_t
3634 max_sync_store(mddev_t *mddev, const char *buf, size_t len)
3636 if (strncmp(buf, "max", 3) == 0)
3637 mddev->resync_max = MaxSector;
3638 else {
3639 unsigned long long max;
3640 if (strict_strtoull(buf, 10, &max))
3641 return -EINVAL;
3642 if (max < mddev->resync_min)
3643 return -EINVAL;
3644 if (max < mddev->resync_max &&
3645 mddev->ro == 0 &&
3646 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3647 return -EBUSY;
3649 /* Must be a multiple of chunk_size */
3650 if (mddev->chunk_sectors) {
3651 sector_t temp = max;
3652 if (sector_div(temp, mddev->chunk_sectors))
3653 return -EINVAL;
3655 mddev->resync_max = max;
3657 wake_up(&mddev->recovery_wait);
3658 return len;
3661 static struct md_sysfs_entry md_max_sync =
3662 __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
3664 static ssize_t
3665 suspend_lo_show(mddev_t *mddev, char *page)
3667 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
3670 static ssize_t
3671 suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
3673 char *e;
3674 unsigned long long new = simple_strtoull(buf, &e, 10);
3676 if (mddev->pers == NULL ||
3677 mddev->pers->quiesce == NULL)
3678 return -EINVAL;
3679 if (buf == e || (*e && *e != '\n'))
3680 return -EINVAL;
3681 if (new >= mddev->suspend_hi ||
3682 (new > mddev->suspend_lo && new < mddev->suspend_hi)) {
3683 mddev->suspend_lo = new;
3684 mddev->pers->quiesce(mddev, 2);
3685 return len;
3686 } else
3687 return -EINVAL;
3689 static struct md_sysfs_entry md_suspend_lo =
3690 __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
3693 static ssize_t
3694 suspend_hi_show(mddev_t *mddev, char *page)
3696 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
3699 static ssize_t
3700 suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
3702 char *e;
3703 unsigned long long new = simple_strtoull(buf, &e, 10);
3705 if (mddev->pers == NULL ||
3706 mddev->pers->quiesce == NULL)
3707 return -EINVAL;
3708 if (buf == e || (*e && *e != '\n'))
3709 return -EINVAL;
3710 if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) ||
3711 (new > mddev->suspend_lo && new > mddev->suspend_hi)) {
3712 mddev->suspend_hi = new;
3713 mddev->pers->quiesce(mddev, 1);
3714 mddev->pers->quiesce(mddev, 0);
3715 return len;
3716 } else
3717 return -EINVAL;
3719 static struct md_sysfs_entry md_suspend_hi =
3720 __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
3722 static ssize_t
3723 reshape_position_show(mddev_t *mddev, char *page)
3725 if (mddev->reshape_position != MaxSector)
3726 return sprintf(page, "%llu\n",
3727 (unsigned long long)mddev->reshape_position);
3728 strcpy(page, "none\n");
3729 return 5;
3732 static ssize_t
3733 reshape_position_store(mddev_t *mddev, const char *buf, size_t len)
3735 char *e;
3736 unsigned long long new = simple_strtoull(buf, &e, 10);
3737 if (mddev->pers)
3738 return -EBUSY;
3739 if (buf == e || (*e && *e != '\n'))
3740 return -EINVAL;
3741 mddev->reshape_position = new;
3742 mddev->delta_disks = 0;
3743 mddev->new_level = mddev->level;
3744 mddev->new_layout = mddev->layout;
3745 mddev->new_chunk_sectors = mddev->chunk_sectors;
3746 return len;
3749 static struct md_sysfs_entry md_reshape_position =
3750 __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
3751 reshape_position_store);
3753 static ssize_t
3754 array_size_show(mddev_t *mddev, char *page)
3756 if (mddev->external_size)
3757 return sprintf(page, "%llu\n",
3758 (unsigned long long)mddev->array_sectors/2);
3759 else
3760 return sprintf(page, "default\n");
3763 static ssize_t
3764 array_size_store(mddev_t *mddev, const char *buf, size_t len)
3766 sector_t sectors;
3768 if (strncmp(buf, "default", 7) == 0) {
3769 if (mddev->pers)
3770 sectors = mddev->pers->size(mddev, 0, 0);
3771 else
3772 sectors = mddev->array_sectors;
3774 mddev->external_size = 0;
3775 } else {
3776 if (strict_blocks_to_sectors(buf, &sectors) < 0)
3777 return -EINVAL;
3778 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
3779 return -E2BIG;
3781 mddev->external_size = 1;
3784 mddev->array_sectors = sectors;
3785 set_capacity(mddev->gendisk, mddev->array_sectors);
3786 if (mddev->pers)
3787 revalidate_disk(mddev->gendisk);
3789 return len;
3792 static struct md_sysfs_entry md_array_size =
3793 __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
3794 array_size_store);
3796 static struct attribute *md_default_attrs[] = {
3797 &md_level.attr,
3798 &md_layout.attr,
3799 &md_raid_disks.attr,
3800 &md_chunk_size.attr,
3801 &md_size.attr,
3802 &md_resync_start.attr,
3803 &md_metadata.attr,
3804 &md_new_device.attr,
3805 &md_safe_delay.attr,
3806 &md_array_state.attr,
3807 &md_reshape_position.attr,
3808 &md_array_size.attr,
3809 NULL,
3812 static struct attribute *md_redundancy_attrs[] = {
3813 &md_scan_mode.attr,
3814 &md_mismatches.attr,
3815 &md_sync_min.attr,
3816 &md_sync_max.attr,
3817 &md_sync_speed.attr,
3818 &md_sync_force_parallel.attr,
3819 &md_sync_completed.attr,
3820 &md_min_sync.attr,
3821 &md_max_sync.attr,
3822 &md_suspend_lo.attr,
3823 &md_suspend_hi.attr,
3824 &md_bitmap.attr,
3825 &md_degraded.attr,
3826 NULL,
3828 static struct attribute_group md_redundancy_group = {
3829 .name = NULL,
3830 .attrs = md_redundancy_attrs,
3834 static ssize_t
3835 md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3837 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
3838 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
3839 ssize_t rv;
3841 if (!entry->show)
3842 return -EIO;
3843 rv = mddev_lock(mddev);
3844 if (!rv) {
3845 rv = entry->show(mddev, page);
3846 mddev_unlock(mddev);
3848 return rv;
3851 static ssize_t
3852 md_attr_store(struct kobject *kobj, struct attribute *attr,
3853 const char *page, size_t length)
3855 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
3856 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
3857 ssize_t rv;
3859 if (!entry->store)
3860 return -EIO;
3861 if (!capable(CAP_SYS_ADMIN))
3862 return -EACCES;
3863 rv = mddev_lock(mddev);
3864 if (mddev->hold_active == UNTIL_IOCTL)
3865 mddev->hold_active = 0;
3866 if (!rv) {
3867 rv = entry->store(mddev, page, length);
3868 mddev_unlock(mddev);
3870 return rv;
3873 static void md_free(struct kobject *ko)
3875 mddev_t *mddev = container_of(ko, mddev_t, kobj);
3877 if (mddev->sysfs_state)
3878 sysfs_put(mddev->sysfs_state);
3880 if (mddev->gendisk) {
3881 del_gendisk(mddev->gendisk);
3882 put_disk(mddev->gendisk);
3884 if (mddev->queue)
3885 blk_cleanup_queue(mddev->queue);
3887 kfree(mddev);
3890 static struct sysfs_ops md_sysfs_ops = {
3891 .show = md_attr_show,
3892 .store = md_attr_store,
3894 static struct kobj_type md_ktype = {
3895 .release = md_free,
3896 .sysfs_ops = &md_sysfs_ops,
3897 .default_attrs = md_default_attrs,
3900 int mdp_major = 0;
3902 static void mddev_delayed_delete(struct work_struct *ws)
3904 mddev_t *mddev = container_of(ws, mddev_t, del_work);
3906 if (mddev->private == &md_redundancy_group) {
3907 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
3908 if (mddev->sysfs_action)
3909 sysfs_put(mddev->sysfs_action);
3910 mddev->sysfs_action = NULL;
3911 mddev->private = NULL;
3913 kobject_del(&mddev->kobj);
3914 kobject_put(&mddev->kobj);
3917 static int md_alloc(dev_t dev, char *name)
3919 static DEFINE_MUTEX(disks_mutex);
3920 mddev_t *mddev = mddev_find(dev);
3921 struct gendisk *disk;
3922 int partitioned;
3923 int shift;
3924 int unit;
3925 int error;
3927 if (!mddev)
3928 return -ENODEV;
3930 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
3931 shift = partitioned ? MdpMinorShift : 0;
3932 unit = MINOR(mddev->unit) >> shift;
3934 /* wait for any previous instance if this device
3935 * to be completed removed (mddev_delayed_delete).
3937 flush_scheduled_work();
3939 mutex_lock(&disks_mutex);
3940 error = -EEXIST;
3941 if (mddev->gendisk)
3942 goto abort;
3944 if (name) {
3945 /* Need to ensure that 'name' is not a duplicate.
3947 mddev_t *mddev2;
3948 spin_lock(&all_mddevs_lock);
3950 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
3951 if (mddev2->gendisk &&
3952 strcmp(mddev2->gendisk->disk_name, name) == 0) {
3953 spin_unlock(&all_mddevs_lock);
3954 goto abort;
3956 spin_unlock(&all_mddevs_lock);
3959 error = -ENOMEM;
3960 mddev->queue = blk_alloc_queue(GFP_KERNEL);
3961 if (!mddev->queue)
3962 goto abort;
3963 mddev->queue->queuedata = mddev;
3965 blk_queue_make_request(mddev->queue, md_make_request);
3967 disk = alloc_disk(1 << shift);
3968 if (!disk) {
3969 blk_cleanup_queue(mddev->queue);
3970 mddev->queue = NULL;
3971 goto abort;
3973 disk->major = MAJOR(mddev->unit);
3974 disk->first_minor = unit << shift;
3975 if (name)
3976 strcpy(disk->disk_name, name);
3977 else if (partitioned)
3978 sprintf(disk->disk_name, "md_d%d", unit);
3979 else
3980 sprintf(disk->disk_name, "md%d", unit);
3981 disk->fops = &md_fops;
3982 disk->private_data = mddev;
3983 disk->queue = mddev->queue;
3984 /* Allow extended partitions. This makes the
3985 * 'mdp' device redundant, but we can't really
3986 * remove it now.
3988 disk->flags |= GENHD_FL_EXT_DEVT;
3989 add_disk(disk);
3990 mddev->gendisk = disk;
3991 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
3992 &disk_to_dev(disk)->kobj, "%s", "md");
3993 if (error) {
3994 /* This isn't possible, but as kobject_init_and_add is marked
3995 * __must_check, we must do something with the result
3997 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
3998 disk->disk_name);
3999 error = 0;
4001 abort:
4002 mutex_unlock(&disks_mutex);
4003 if (!error) {
4004 kobject_uevent(&mddev->kobj, KOBJ_ADD);
4005 mddev->sysfs_state = sysfs_get_dirent(mddev->kobj.sd, "array_state");
4007 mddev_put(mddev);
4008 return error;
4011 static struct kobject *md_probe(dev_t dev, int *part, void *data)
4013 md_alloc(dev, NULL);
4014 return NULL;
4017 static int add_named_array(const char *val, struct kernel_param *kp)
4019 /* val must be "md_*" where * is not all digits.
4020 * We allocate an array with a large free minor number, and
4021 * set the name to val. val must not already be an active name.
4023 int len = strlen(val);
4024 char buf[DISK_NAME_LEN];
4026 while (len && val[len-1] == '\n')
4027 len--;
4028 if (len >= DISK_NAME_LEN)
4029 return -E2BIG;
4030 strlcpy(buf, val, len+1);
4031 if (strncmp(buf, "md_", 3) != 0)
4032 return -EINVAL;
4033 return md_alloc(0, buf);
4036 static void md_safemode_timeout(unsigned long data)
4038 mddev_t *mddev = (mddev_t *) data;
4040 if (!atomic_read(&mddev->writes_pending)) {
4041 mddev->safemode = 1;
4042 if (mddev->external)
4043 sysfs_notify_dirent(mddev->sysfs_state);
4045 md_wakeup_thread(mddev->thread);
4048 static int start_dirty_degraded;
4050 static int do_md_run(mddev_t * mddev)
4052 int err;
4053 mdk_rdev_t *rdev;
4054 struct gendisk *disk;
4055 struct mdk_personality *pers;
4057 if (list_empty(&mddev->disks))
4058 /* cannot run an array with no devices.. */
4059 return -EINVAL;
4061 if (mddev->pers)
4062 return -EBUSY;
4065 * Analyze all RAID superblock(s)
4067 if (!mddev->raid_disks) {
4068 if (!mddev->persistent)
4069 return -EINVAL;
4070 analyze_sbs(mddev);
4073 if (mddev->level != LEVEL_NONE)
4074 request_module("md-level-%d", mddev->level);
4075 else if (mddev->clevel[0])
4076 request_module("md-%s", mddev->clevel);
4079 * Drop all container device buffers, from now on
4080 * the only valid external interface is through the md
4081 * device.
4083 list_for_each_entry(rdev, &mddev->disks, same_set) {
4084 if (test_bit(Faulty, &rdev->flags))
4085 continue;
4086 sync_blockdev(rdev->bdev);
4087 invalidate_bdev(rdev->bdev);
4089 /* perform some consistency tests on the device.
4090 * We don't want the data to overlap the metadata,
4091 * Internal Bitmap issues have been handled elsewhere.
4093 if (rdev->data_offset < rdev->sb_start) {
4094 if (mddev->dev_sectors &&
4095 rdev->data_offset + mddev->dev_sectors
4096 > rdev->sb_start) {
4097 printk("md: %s: data overlaps metadata\n",
4098 mdname(mddev));
4099 return -EINVAL;
4101 } else {
4102 if (rdev->sb_start + rdev->sb_size/512
4103 > rdev->data_offset) {
4104 printk("md: %s: metadata overlaps data\n",
4105 mdname(mddev));
4106 return -EINVAL;
4109 sysfs_notify_dirent(rdev->sysfs_state);
4112 md_probe(mddev->unit, NULL, NULL);
4113 disk = mddev->gendisk;
4114 if (!disk)
4115 return -ENOMEM;
4117 spin_lock(&pers_lock);
4118 pers = find_pers(mddev->level, mddev->clevel);
4119 if (!pers || !try_module_get(pers->owner)) {
4120 spin_unlock(&pers_lock);
4121 if (mddev->level != LEVEL_NONE)
4122 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
4123 mddev->level);
4124 else
4125 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
4126 mddev->clevel);
4127 return -EINVAL;
4129 mddev->pers = pers;
4130 spin_unlock(&pers_lock);
4131 if (mddev->level != pers->level) {
4132 mddev->level = pers->level;
4133 mddev->new_level = pers->level;
4135 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
4137 if (mddev->reshape_position != MaxSector &&
4138 pers->start_reshape == NULL) {
4139 /* This personality cannot handle reshaping... */
4140 mddev->pers = NULL;
4141 module_put(pers->owner);
4142 return -EINVAL;
4145 if (pers->sync_request) {
4146 /* Warn if this is a potentially silly
4147 * configuration.
4149 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4150 mdk_rdev_t *rdev2;
4151 int warned = 0;
4153 list_for_each_entry(rdev, &mddev->disks, same_set)
4154 list_for_each_entry(rdev2, &mddev->disks, same_set) {
4155 if (rdev < rdev2 &&
4156 rdev->bdev->bd_contains ==
4157 rdev2->bdev->bd_contains) {
4158 printk(KERN_WARNING
4159 "%s: WARNING: %s appears to be"
4160 " on the same physical disk as"
4161 " %s.\n",
4162 mdname(mddev),
4163 bdevname(rdev->bdev,b),
4164 bdevname(rdev2->bdev,b2));
4165 warned = 1;
4169 if (warned)
4170 printk(KERN_WARNING
4171 "True protection against single-disk"
4172 " failure might be compromised.\n");
4175 mddev->recovery = 0;
4176 /* may be over-ridden by personality */
4177 mddev->resync_max_sectors = mddev->dev_sectors;
4179 mddev->barriers_work = 1;
4180 mddev->ok_start_degraded = start_dirty_degraded;
4182 if (start_readonly && mddev->ro == 0)
4183 mddev->ro = 2; /* read-only, but switch on first write */
4185 err = mddev->pers->run(mddev);
4186 if (err)
4187 printk(KERN_ERR "md: pers->run() failed ...\n");
4188 else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
4189 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
4190 " but 'external_size' not in effect?\n", __func__);
4191 printk(KERN_ERR
4192 "md: invalid array_size %llu > default size %llu\n",
4193 (unsigned long long)mddev->array_sectors / 2,
4194 (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
4195 err = -EINVAL;
4196 mddev->pers->stop(mddev);
4198 if (err == 0 && mddev->pers->sync_request) {
4199 err = bitmap_create(mddev);
4200 if (err) {
4201 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
4202 mdname(mddev), err);
4203 mddev->pers->stop(mddev);
4206 if (err) {
4207 module_put(mddev->pers->owner);
4208 mddev->pers = NULL;
4209 bitmap_destroy(mddev);
4210 return err;
4212 if (mddev->pers->sync_request) {
4213 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
4214 printk(KERN_WARNING
4215 "md: cannot register extra attributes for %s\n",
4216 mdname(mddev));
4217 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
4218 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
4219 mddev->ro = 0;
4221 atomic_set(&mddev->writes_pending,0);
4222 mddev->safemode = 0;
4223 mddev->safemode_timer.function = md_safemode_timeout;
4224 mddev->safemode_timer.data = (unsigned long) mddev;
4225 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
4226 mddev->in_sync = 1;
4228 list_for_each_entry(rdev, &mddev->disks, same_set)
4229 if (rdev->raid_disk >= 0) {
4230 char nm[20];
4231 sprintf(nm, "rd%d", rdev->raid_disk);
4232 if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
4233 printk("md: cannot register %s for %s\n",
4234 nm, mdname(mddev));
4237 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4239 if (mddev->flags)
4240 md_update_sb(mddev, 0);
4242 set_capacity(disk, mddev->array_sectors);
4244 /* If there is a partially-recovered drive we need to
4245 * start recovery here. If we leave it to md_check_recovery,
4246 * it will remove the drives and not do the right thing
4248 if (mddev->degraded && !mddev->sync_thread) {
4249 int spares = 0;
4250 list_for_each_entry(rdev, &mddev->disks, same_set)
4251 if (rdev->raid_disk >= 0 &&
4252 !test_bit(In_sync, &rdev->flags) &&
4253 !test_bit(Faulty, &rdev->flags))
4254 /* complete an interrupted recovery */
4255 spares++;
4256 if (spares && mddev->pers->sync_request) {
4257 mddev->recovery = 0;
4258 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
4259 mddev->sync_thread = md_register_thread(md_do_sync,
4260 mddev,
4261 "resync");
4262 if (!mddev->sync_thread) {
4263 printk(KERN_ERR "%s: could not start resync"
4264 " thread...\n",
4265 mdname(mddev));
4266 /* leave the spares where they are, it shouldn't hurt */
4267 mddev->recovery = 0;
4271 md_wakeup_thread(mddev->thread);
4272 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
4274 revalidate_disk(mddev->gendisk);
4275 mddev->changed = 1;
4276 md_new_event(mddev);
4277 sysfs_notify_dirent(mddev->sysfs_state);
4278 if (mddev->sysfs_action)
4279 sysfs_notify_dirent(mddev->sysfs_action);
4280 sysfs_notify(&mddev->kobj, NULL, "degraded");
4281 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
4282 return 0;
4285 static int restart_array(mddev_t *mddev)
4287 struct gendisk *disk = mddev->gendisk;
4289 /* Complain if it has no devices */
4290 if (list_empty(&mddev->disks))
4291 return -ENXIO;
4292 if (!mddev->pers)
4293 return -EINVAL;
4294 if (!mddev->ro)
4295 return -EBUSY;
4296 mddev->safemode = 0;
4297 mddev->ro = 0;
4298 set_disk_ro(disk, 0);
4299 printk(KERN_INFO "md: %s switched to read-write mode.\n",
4300 mdname(mddev));
4301 /* Kick recovery or resync if necessary */
4302 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4303 md_wakeup_thread(mddev->thread);
4304 md_wakeup_thread(mddev->sync_thread);
4305 sysfs_notify_dirent(mddev->sysfs_state);
4306 return 0;
4309 /* similar to deny_write_access, but accounts for our holding a reference
4310 * to the file ourselves */
4311 static int deny_bitmap_write_access(struct file * file)
4313 struct inode *inode = file->f_mapping->host;
4315 spin_lock(&inode->i_lock);
4316 if (atomic_read(&inode->i_writecount) > 1) {
4317 spin_unlock(&inode->i_lock);
4318 return -ETXTBSY;
4320 atomic_set(&inode->i_writecount, -1);
4321 spin_unlock(&inode->i_lock);
4323 return 0;
4326 static void restore_bitmap_write_access(struct file *file)
4328 struct inode *inode = file->f_mapping->host;
4330 spin_lock(&inode->i_lock);
4331 atomic_set(&inode->i_writecount, 1);
4332 spin_unlock(&inode->i_lock);
4335 /* mode:
4336 * 0 - completely stop and dis-assemble array
4337 * 1 - switch to readonly
4338 * 2 - stop but do not disassemble array
4340 static int do_md_stop(mddev_t * mddev, int mode, int is_open)
4342 int err = 0;
4343 struct gendisk *disk = mddev->gendisk;
4344 mdk_rdev_t *rdev;
4346 mutex_lock(&mddev->open_mutex);
4347 if (atomic_read(&mddev->openers) > is_open) {
4348 printk("md: %s still in use.\n",mdname(mddev));
4349 err = -EBUSY;
4350 } else if (mddev->pers) {
4352 if (mddev->sync_thread) {
4353 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4354 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4355 md_unregister_thread(mddev->sync_thread);
4356 mddev->sync_thread = NULL;
4359 del_timer_sync(&mddev->safemode_timer);
4361 switch(mode) {
4362 case 1: /* readonly */
4363 err = -ENXIO;
4364 if (mddev->ro==1)
4365 goto out;
4366 mddev->ro = 1;
4367 break;
4368 case 0: /* disassemble */
4369 case 2: /* stop */
4370 bitmap_flush(mddev);
4371 md_super_wait(mddev);
4372 if (mddev->ro)
4373 set_disk_ro(disk, 0);
4375 mddev->pers->stop(mddev);
4376 mddev->queue->merge_bvec_fn = NULL;
4377 mddev->queue->unplug_fn = NULL;
4378 mddev->queue->backing_dev_info.congested_fn = NULL;
4379 module_put(mddev->pers->owner);
4380 if (mddev->pers->sync_request)
4381 mddev->private = &md_redundancy_group;
4382 mddev->pers = NULL;
4383 /* tell userspace to handle 'inactive' */
4384 sysfs_notify_dirent(mddev->sysfs_state);
4386 list_for_each_entry(rdev, &mddev->disks, same_set)
4387 if (rdev->raid_disk >= 0) {
4388 char nm[20];
4389 sprintf(nm, "rd%d", rdev->raid_disk);
4390 sysfs_remove_link(&mddev->kobj, nm);
4393 set_capacity(disk, 0);
4394 mddev->changed = 1;
4396 if (mddev->ro)
4397 mddev->ro = 0;
4399 if (!mddev->in_sync || mddev->flags) {
4400 /* mark array as shutdown cleanly */
4401 mddev->in_sync = 1;
4402 md_update_sb(mddev, 1);
4404 if (mode == 1)
4405 set_disk_ro(disk, 1);
4406 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4407 err = 0;
4409 out:
4410 mutex_unlock(&mddev->open_mutex);
4411 if (err)
4412 return err;
4414 * Free resources if final stop
4416 if (mode == 0) {
4418 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
4420 bitmap_destroy(mddev);
4421 if (mddev->bitmap_file) {
4422 restore_bitmap_write_access(mddev->bitmap_file);
4423 fput(mddev->bitmap_file);
4424 mddev->bitmap_file = NULL;
4426 mddev->bitmap_offset = 0;
4428 /* make sure all md_delayed_delete calls have finished */
4429 flush_scheduled_work();
4431 export_array(mddev);
4433 mddev->array_sectors = 0;
4434 mddev->external_size = 0;
4435 mddev->dev_sectors = 0;
4436 mddev->raid_disks = 0;
4437 mddev->recovery_cp = 0;
4438 mddev->resync_min = 0;
4439 mddev->resync_max = MaxSector;
4440 mddev->reshape_position = MaxSector;
4441 mddev->external = 0;
4442 mddev->persistent = 0;
4443 mddev->level = LEVEL_NONE;
4444 mddev->clevel[0] = 0;
4445 mddev->flags = 0;
4446 mddev->ro = 0;
4447 mddev->metadata_type[0] = 0;
4448 mddev->chunk_sectors = 0;
4449 mddev->ctime = mddev->utime = 0;
4450 mddev->layout = 0;
4451 mddev->max_disks = 0;
4452 mddev->events = 0;
4453 mddev->delta_disks = 0;
4454 mddev->new_level = LEVEL_NONE;
4455 mddev->new_layout = 0;
4456 mddev->new_chunk_sectors = 0;
4457 mddev->curr_resync = 0;
4458 mddev->resync_mismatches = 0;
4459 mddev->suspend_lo = mddev->suspend_hi = 0;
4460 mddev->sync_speed_min = mddev->sync_speed_max = 0;
4461 mddev->recovery = 0;
4462 mddev->in_sync = 0;
4463 mddev->changed = 0;
4464 mddev->degraded = 0;
4465 mddev->barriers_work = 0;
4466 mddev->safemode = 0;
4467 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
4468 if (mddev->hold_active == UNTIL_STOP)
4469 mddev->hold_active = 0;
4471 } else if (mddev->pers)
4472 printk(KERN_INFO "md: %s switched to read-only mode.\n",
4473 mdname(mddev));
4474 err = 0;
4475 blk_integrity_unregister(disk);
4476 md_new_event(mddev);
4477 sysfs_notify_dirent(mddev->sysfs_state);
4478 return err;
4481 #ifndef MODULE
4482 static void autorun_array(mddev_t *mddev)
4484 mdk_rdev_t *rdev;
4485 int err;
4487 if (list_empty(&mddev->disks))
4488 return;
4490 printk(KERN_INFO "md: running: ");
4492 list_for_each_entry(rdev, &mddev->disks, same_set) {
4493 char b[BDEVNAME_SIZE];
4494 printk("<%s>", bdevname(rdev->bdev,b));
4496 printk("\n");
4498 err = do_md_run(mddev);
4499 if (err) {
4500 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
4501 do_md_stop(mddev, 0, 0);
4506 * lets try to run arrays based on all disks that have arrived
4507 * until now. (those are in pending_raid_disks)
4509 * the method: pick the first pending disk, collect all disks with
4510 * the same UUID, remove all from the pending list and put them into
4511 * the 'same_array' list. Then order this list based on superblock
4512 * update time (freshest comes first), kick out 'old' disks and
4513 * compare superblocks. If everything's fine then run it.
4515 * If "unit" is allocated, then bump its reference count
4517 static void autorun_devices(int part)
4519 mdk_rdev_t *rdev0, *rdev, *tmp;
4520 mddev_t *mddev;
4521 char b[BDEVNAME_SIZE];
4523 printk(KERN_INFO "md: autorun ...\n");
4524 while (!list_empty(&pending_raid_disks)) {
4525 int unit;
4526 dev_t dev;
4527 LIST_HEAD(candidates);
4528 rdev0 = list_entry(pending_raid_disks.next,
4529 mdk_rdev_t, same_set);
4531 printk(KERN_INFO "md: considering %s ...\n",
4532 bdevname(rdev0->bdev,b));
4533 INIT_LIST_HEAD(&candidates);
4534 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
4535 if (super_90_load(rdev, rdev0, 0) >= 0) {
4536 printk(KERN_INFO "md: adding %s ...\n",
4537 bdevname(rdev->bdev,b));
4538 list_move(&rdev->same_set, &candidates);
4541 * now we have a set of devices, with all of them having
4542 * mostly sane superblocks. It's time to allocate the
4543 * mddev.
4545 if (part) {
4546 dev = MKDEV(mdp_major,
4547 rdev0->preferred_minor << MdpMinorShift);
4548 unit = MINOR(dev) >> MdpMinorShift;
4549 } else {
4550 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
4551 unit = MINOR(dev);
4553 if (rdev0->preferred_minor != unit) {
4554 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
4555 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
4556 break;
4559 md_probe(dev, NULL, NULL);
4560 mddev = mddev_find(dev);
4561 if (!mddev || !mddev->gendisk) {
4562 if (mddev)
4563 mddev_put(mddev);
4564 printk(KERN_ERR
4565 "md: cannot allocate memory for md drive.\n");
4566 break;
4568 if (mddev_lock(mddev))
4569 printk(KERN_WARNING "md: %s locked, cannot run\n",
4570 mdname(mddev));
4571 else if (mddev->raid_disks || mddev->major_version
4572 || !list_empty(&mddev->disks)) {
4573 printk(KERN_WARNING
4574 "md: %s already running, cannot run %s\n",
4575 mdname(mddev), bdevname(rdev0->bdev,b));
4576 mddev_unlock(mddev);
4577 } else {
4578 printk(KERN_INFO "md: created %s\n", mdname(mddev));
4579 mddev->persistent = 1;
4580 rdev_for_each_list(rdev, tmp, &candidates) {
4581 list_del_init(&rdev->same_set);
4582 if (bind_rdev_to_array(rdev, mddev))
4583 export_rdev(rdev);
4585 autorun_array(mddev);
4586 mddev_unlock(mddev);
4588 /* on success, candidates will be empty, on error
4589 * it won't...
4591 rdev_for_each_list(rdev, tmp, &candidates) {
4592 list_del_init(&rdev->same_set);
4593 export_rdev(rdev);
4595 mddev_put(mddev);
4597 printk(KERN_INFO "md: ... autorun DONE.\n");
4599 #endif /* !MODULE */
4601 static int get_version(void __user * arg)
4603 mdu_version_t ver;
4605 ver.major = MD_MAJOR_VERSION;
4606 ver.minor = MD_MINOR_VERSION;
4607 ver.patchlevel = MD_PATCHLEVEL_VERSION;
4609 if (copy_to_user(arg, &ver, sizeof(ver)))
4610 return -EFAULT;
4612 return 0;
4615 static int get_array_info(mddev_t * mddev, void __user * arg)
4617 mdu_array_info_t info;
4618 int nr,working,insync,failed,spare;
4619 mdk_rdev_t *rdev;
4621 nr=working=insync=failed=spare=0;
4622 list_for_each_entry(rdev, &mddev->disks, same_set) {
4623 nr++;
4624 if (test_bit(Faulty, &rdev->flags))
4625 failed++;
4626 else {
4627 working++;
4628 if (test_bit(In_sync, &rdev->flags))
4629 insync++;
4630 else
4631 spare++;
4635 info.major_version = mddev->major_version;
4636 info.minor_version = mddev->minor_version;
4637 info.patch_version = MD_PATCHLEVEL_VERSION;
4638 info.ctime = mddev->ctime;
4639 info.level = mddev->level;
4640 info.size = mddev->dev_sectors / 2;
4641 if (info.size != mddev->dev_sectors / 2) /* overflow */
4642 info.size = -1;
4643 info.nr_disks = nr;
4644 info.raid_disks = mddev->raid_disks;
4645 info.md_minor = mddev->md_minor;
4646 info.not_persistent= !mddev->persistent;
4648 info.utime = mddev->utime;
4649 info.state = 0;
4650 if (mddev->in_sync)
4651 info.state = (1<<MD_SB_CLEAN);
4652 if (mddev->bitmap && mddev->bitmap_offset)
4653 info.state = (1<<MD_SB_BITMAP_PRESENT);
4654 info.active_disks = insync;
4655 info.working_disks = working;
4656 info.failed_disks = failed;
4657 info.spare_disks = spare;
4659 info.layout = mddev->layout;
4660 info.chunk_size = mddev->chunk_sectors << 9;
4662 if (copy_to_user(arg, &info, sizeof(info)))
4663 return -EFAULT;
4665 return 0;
4668 static int get_bitmap_file(mddev_t * mddev, void __user * arg)
4670 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
4671 char *ptr, *buf = NULL;
4672 int err = -ENOMEM;
4674 if (md_allow_write(mddev))
4675 file = kmalloc(sizeof(*file), GFP_NOIO);
4676 else
4677 file = kmalloc(sizeof(*file), GFP_KERNEL);
4679 if (!file)
4680 goto out;
4682 /* bitmap disabled, zero the first byte and copy out */
4683 if (!mddev->bitmap || !mddev->bitmap->file) {
4684 file->pathname[0] = '\0';
4685 goto copy_out;
4688 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
4689 if (!buf)
4690 goto out;
4692 ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname));
4693 if (IS_ERR(ptr))
4694 goto out;
4696 strcpy(file->pathname, ptr);
4698 copy_out:
4699 err = 0;
4700 if (copy_to_user(arg, file, sizeof(*file)))
4701 err = -EFAULT;
4702 out:
4703 kfree(buf);
4704 kfree(file);
4705 return err;
4708 static int get_disk_info(mddev_t * mddev, void __user * arg)
4710 mdu_disk_info_t info;
4711 mdk_rdev_t *rdev;
4713 if (copy_from_user(&info, arg, sizeof(info)))
4714 return -EFAULT;
4716 rdev = find_rdev_nr(mddev, info.number);
4717 if (rdev) {
4718 info.major = MAJOR(rdev->bdev->bd_dev);
4719 info.minor = MINOR(rdev->bdev->bd_dev);
4720 info.raid_disk = rdev->raid_disk;
4721 info.state = 0;
4722 if (test_bit(Faulty, &rdev->flags))
4723 info.state |= (1<<MD_DISK_FAULTY);
4724 else if (test_bit(In_sync, &rdev->flags)) {
4725 info.state |= (1<<MD_DISK_ACTIVE);
4726 info.state |= (1<<MD_DISK_SYNC);
4728 if (test_bit(WriteMostly, &rdev->flags))
4729 info.state |= (1<<MD_DISK_WRITEMOSTLY);
4730 } else {
4731 info.major = info.minor = 0;
4732 info.raid_disk = -1;
4733 info.state = (1<<MD_DISK_REMOVED);
4736 if (copy_to_user(arg, &info, sizeof(info)))
4737 return -EFAULT;
4739 return 0;
4742 static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
4744 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4745 mdk_rdev_t *rdev;
4746 dev_t dev = MKDEV(info->major,info->minor);
4748 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
4749 return -EOVERFLOW;
4751 if (!mddev->raid_disks) {
4752 int err;
4753 /* expecting a device which has a superblock */
4754 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
4755 if (IS_ERR(rdev)) {
4756 printk(KERN_WARNING
4757 "md: md_import_device returned %ld\n",
4758 PTR_ERR(rdev));
4759 return PTR_ERR(rdev);
4761 if (!list_empty(&mddev->disks)) {
4762 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
4763 mdk_rdev_t, same_set);
4764 err = super_types[mddev->major_version]
4765 .load_super(rdev, rdev0, mddev->minor_version);
4766 if (err < 0) {
4767 printk(KERN_WARNING
4768 "md: %s has different UUID to %s\n",
4769 bdevname(rdev->bdev,b),
4770 bdevname(rdev0->bdev,b2));
4771 export_rdev(rdev);
4772 return -EINVAL;
4775 err = bind_rdev_to_array(rdev, mddev);
4776 if (err)
4777 export_rdev(rdev);
4778 return err;
4782 * add_new_disk can be used once the array is assembled
4783 * to add "hot spares". They must already have a superblock
4784 * written
4786 if (mddev->pers) {
4787 int err;
4788 if (!mddev->pers->hot_add_disk) {
4789 printk(KERN_WARNING
4790 "%s: personality does not support diskops!\n",
4791 mdname(mddev));
4792 return -EINVAL;
4794 if (mddev->persistent)
4795 rdev = md_import_device(dev, mddev->major_version,
4796 mddev->minor_version);
4797 else
4798 rdev = md_import_device(dev, -1, -1);
4799 if (IS_ERR(rdev)) {
4800 printk(KERN_WARNING
4801 "md: md_import_device returned %ld\n",
4802 PTR_ERR(rdev));
4803 return PTR_ERR(rdev);
4805 /* set saved_raid_disk if appropriate */
4806 if (!mddev->persistent) {
4807 if (info->state & (1<<MD_DISK_SYNC) &&
4808 info->raid_disk < mddev->raid_disks) {
4809 rdev->raid_disk = info->raid_disk;
4810 set_bit(In_sync, &rdev->flags);
4811 } else
4812 rdev->raid_disk = -1;
4813 } else
4814 super_types[mddev->major_version].
4815 validate_super(mddev, rdev);
4816 if (test_bit(In_sync, &rdev->flags))
4817 rdev->saved_raid_disk = rdev->raid_disk;
4818 else
4819 rdev->saved_raid_disk = -1;
4821 clear_bit(In_sync, &rdev->flags); /* just to be sure */
4822 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
4823 set_bit(WriteMostly, &rdev->flags);
4824 else
4825 clear_bit(WriteMostly, &rdev->flags);
4827 rdev->raid_disk = -1;
4828 err = bind_rdev_to_array(rdev, mddev);
4829 if (!err && !mddev->pers->hot_remove_disk) {
4830 /* If there is hot_add_disk but no hot_remove_disk
4831 * then added disks for geometry changes,
4832 * and should be added immediately.
4834 super_types[mddev->major_version].
4835 validate_super(mddev, rdev);
4836 err = mddev->pers->hot_add_disk(mddev, rdev);
4837 if (err)
4838 unbind_rdev_from_array(rdev);
4840 if (err)
4841 export_rdev(rdev);
4842 else
4843 sysfs_notify_dirent(rdev->sysfs_state);
4845 md_update_sb(mddev, 1);
4846 if (mddev->degraded)
4847 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
4848 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4849 md_wakeup_thread(mddev->thread);
4850 return err;
4853 /* otherwise, add_new_disk is only allowed
4854 * for major_version==0 superblocks
4856 if (mddev->major_version != 0) {
4857 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
4858 mdname(mddev));
4859 return -EINVAL;
4862 if (!(info->state & (1<<MD_DISK_FAULTY))) {
4863 int err;
4864 rdev = md_import_device(dev, -1, 0);
4865 if (IS_ERR(rdev)) {
4866 printk(KERN_WARNING
4867 "md: error, md_import_device() returned %ld\n",
4868 PTR_ERR(rdev));
4869 return PTR_ERR(rdev);
4871 rdev->desc_nr = info->number;
4872 if (info->raid_disk < mddev->raid_disks)
4873 rdev->raid_disk = info->raid_disk;
4874 else
4875 rdev->raid_disk = -1;
4877 if (rdev->raid_disk < mddev->raid_disks)
4878 if (info->state & (1<<MD_DISK_SYNC))
4879 set_bit(In_sync, &rdev->flags);
4881 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
4882 set_bit(WriteMostly, &rdev->flags);
4884 if (!mddev->persistent) {
4885 printk(KERN_INFO "md: nonpersistent superblock ...\n");
4886 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
4887 } else
4888 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
4889 rdev->sectors = rdev->sb_start;
4891 err = bind_rdev_to_array(rdev, mddev);
4892 if (err) {
4893 export_rdev(rdev);
4894 return err;
4898 return 0;
4901 static int hot_remove_disk(mddev_t * mddev, dev_t dev)
4903 char b[BDEVNAME_SIZE];
4904 mdk_rdev_t *rdev;
4906 rdev = find_rdev(mddev, dev);
4907 if (!rdev)
4908 return -ENXIO;
4910 if (rdev->raid_disk >= 0)
4911 goto busy;
4913 kick_rdev_from_array(rdev);
4914 md_update_sb(mddev, 1);
4915 md_new_event(mddev);
4917 return 0;
4918 busy:
4919 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
4920 bdevname(rdev->bdev,b), mdname(mddev));
4921 return -EBUSY;
4924 static int hot_add_disk(mddev_t * mddev, dev_t dev)
4926 char b[BDEVNAME_SIZE];
4927 int err;
4928 mdk_rdev_t *rdev;
4930 if (!mddev->pers)
4931 return -ENODEV;
4933 if (mddev->major_version != 0) {
4934 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
4935 " version-0 superblocks.\n",
4936 mdname(mddev));
4937 return -EINVAL;
4939 if (!mddev->pers->hot_add_disk) {
4940 printk(KERN_WARNING
4941 "%s: personality does not support diskops!\n",
4942 mdname(mddev));
4943 return -EINVAL;
4946 rdev = md_import_device(dev, -1, 0);
4947 if (IS_ERR(rdev)) {
4948 printk(KERN_WARNING
4949 "md: error, md_import_device() returned %ld\n",
4950 PTR_ERR(rdev));
4951 return -EINVAL;
4954 if (mddev->persistent)
4955 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
4956 else
4957 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
4959 rdev->sectors = rdev->sb_start;
4961 if (test_bit(Faulty, &rdev->flags)) {
4962 printk(KERN_WARNING
4963 "md: can not hot-add faulty %s disk to %s!\n",
4964 bdevname(rdev->bdev,b), mdname(mddev));
4965 err = -EINVAL;
4966 goto abort_export;
4968 clear_bit(In_sync, &rdev->flags);
4969 rdev->desc_nr = -1;
4970 rdev->saved_raid_disk = -1;
4971 err = bind_rdev_to_array(rdev, mddev);
4972 if (err)
4973 goto abort_export;
4976 * The rest should better be atomic, we can have disk failures
4977 * noticed in interrupt contexts ...
4980 rdev->raid_disk = -1;
4982 md_update_sb(mddev, 1);
4985 * Kick recovery, maybe this spare has to be added to the
4986 * array immediately.
4988 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4989 md_wakeup_thread(mddev->thread);
4990 md_new_event(mddev);
4991 return 0;
4993 abort_export:
4994 export_rdev(rdev);
4995 return err;
4998 static int set_bitmap_file(mddev_t *mddev, int fd)
5000 int err;
5002 if (mddev->pers) {
5003 if (!mddev->pers->quiesce)
5004 return -EBUSY;
5005 if (mddev->recovery || mddev->sync_thread)
5006 return -EBUSY;
5007 /* we should be able to change the bitmap.. */
5011 if (fd >= 0) {
5012 if (mddev->bitmap)
5013 return -EEXIST; /* cannot add when bitmap is present */
5014 mddev->bitmap_file = fget(fd);
5016 if (mddev->bitmap_file == NULL) {
5017 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
5018 mdname(mddev));
5019 return -EBADF;
5022 err = deny_bitmap_write_access(mddev->bitmap_file);
5023 if (err) {
5024 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
5025 mdname(mddev));
5026 fput(mddev->bitmap_file);
5027 mddev->bitmap_file = NULL;
5028 return err;
5030 mddev->bitmap_offset = 0; /* file overrides offset */
5031 } else if (mddev->bitmap == NULL)
5032 return -ENOENT; /* cannot remove what isn't there */
5033 err = 0;
5034 if (mddev->pers) {
5035 mddev->pers->quiesce(mddev, 1);
5036 if (fd >= 0)
5037 err = bitmap_create(mddev);
5038 if (fd < 0 || err) {
5039 bitmap_destroy(mddev);
5040 fd = -1; /* make sure to put the file */
5042 mddev->pers->quiesce(mddev, 0);
5044 if (fd < 0) {
5045 if (mddev->bitmap_file) {
5046 restore_bitmap_write_access(mddev->bitmap_file);
5047 fput(mddev->bitmap_file);
5049 mddev->bitmap_file = NULL;
5052 return err;
5056 * set_array_info is used two different ways
5057 * The original usage is when creating a new array.
5058 * In this usage, raid_disks is > 0 and it together with
5059 * level, size, not_persistent,layout,chunksize determine the
5060 * shape of the array.
5061 * This will always create an array with a type-0.90.0 superblock.
5062 * The newer usage is when assembling an array.
5063 * In this case raid_disks will be 0, and the major_version field is
5064 * use to determine which style super-blocks are to be found on the devices.
5065 * The minor and patch _version numbers are also kept incase the
5066 * super_block handler wishes to interpret them.
5068 static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
5071 if (info->raid_disks == 0) {
5072 /* just setting version number for superblock loading */
5073 if (info->major_version < 0 ||
5074 info->major_version >= ARRAY_SIZE(super_types) ||
5075 super_types[info->major_version].name == NULL) {
5076 /* maybe try to auto-load a module? */
5077 printk(KERN_INFO
5078 "md: superblock version %d not known\n",
5079 info->major_version);
5080 return -EINVAL;
5082 mddev->major_version = info->major_version;
5083 mddev->minor_version = info->minor_version;
5084 mddev->patch_version = info->patch_version;
5085 mddev->persistent = !info->not_persistent;
5086 /* ensure mddev_put doesn't delete this now that there
5087 * is some minimal configuration.
5089 mddev->ctime = get_seconds();
5090 return 0;
5092 mddev->major_version = MD_MAJOR_VERSION;
5093 mddev->minor_version = MD_MINOR_VERSION;
5094 mddev->patch_version = MD_PATCHLEVEL_VERSION;
5095 mddev->ctime = get_seconds();
5097 mddev->level = info->level;
5098 mddev->clevel[0] = 0;
5099 mddev->dev_sectors = 2 * (sector_t)info->size;
5100 mddev->raid_disks = info->raid_disks;
5101 /* don't set md_minor, it is determined by which /dev/md* was
5102 * openned
5104 if (info->state & (1<<MD_SB_CLEAN))
5105 mddev->recovery_cp = MaxSector;
5106 else
5107 mddev->recovery_cp = 0;
5108 mddev->persistent = ! info->not_persistent;
5109 mddev->external = 0;
5111 mddev->layout = info->layout;
5112 mddev->chunk_sectors = info->chunk_size >> 9;
5114 mddev->max_disks = MD_SB_DISKS;
5116 if (mddev->persistent)
5117 mddev->flags = 0;
5118 set_bit(MD_CHANGE_DEVS, &mddev->flags);
5120 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
5121 mddev->bitmap_offset = 0;
5123 mddev->reshape_position = MaxSector;
5126 * Generate a 128 bit UUID
5128 get_random_bytes(mddev->uuid, 16);
5130 mddev->new_level = mddev->level;
5131 mddev->new_chunk_sectors = mddev->chunk_sectors;
5132 mddev->new_layout = mddev->layout;
5133 mddev->delta_disks = 0;
5135 return 0;
5138 void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors)
5140 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
5142 if (mddev->external_size)
5143 return;
5145 mddev->array_sectors = array_sectors;
5147 EXPORT_SYMBOL(md_set_array_sectors);
5149 static int update_size(mddev_t *mddev, sector_t num_sectors)
5151 mdk_rdev_t *rdev;
5152 int rv;
5153 int fit = (num_sectors == 0);
5155 if (mddev->pers->resize == NULL)
5156 return -EINVAL;
5157 /* The "num_sectors" is the number of sectors of each device that
5158 * is used. This can only make sense for arrays with redundancy.
5159 * linear and raid0 always use whatever space is available. We can only
5160 * consider changing this number if no resync or reconstruction is
5161 * happening, and if the new size is acceptable. It must fit before the
5162 * sb_start or, if that is <data_offset, it must fit before the size
5163 * of each device. If num_sectors is zero, we find the largest size
5164 * that fits.
5167 if (mddev->sync_thread)
5168 return -EBUSY;
5169 if (mddev->bitmap)
5170 /* Sorry, cannot grow a bitmap yet, just remove it,
5171 * grow, and re-add.
5173 return -EBUSY;
5174 list_for_each_entry(rdev, &mddev->disks, same_set) {
5175 sector_t avail = rdev->sectors;
5177 if (fit && (num_sectors == 0 || num_sectors > avail))
5178 num_sectors = avail;
5179 if (avail < num_sectors)
5180 return -ENOSPC;
5182 rv = mddev->pers->resize(mddev, num_sectors);
5183 if (!rv)
5184 revalidate_disk(mddev->gendisk);
5185 return rv;
5188 static int update_raid_disks(mddev_t *mddev, int raid_disks)
5190 int rv;
5191 /* change the number of raid disks */
5192 if (mddev->pers->check_reshape == NULL)
5193 return -EINVAL;
5194 if (raid_disks <= 0 ||
5195 raid_disks >= mddev->max_disks)
5196 return -EINVAL;
5197 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
5198 return -EBUSY;
5199 mddev->delta_disks = raid_disks - mddev->raid_disks;
5201 rv = mddev->pers->check_reshape(mddev);
5202 return rv;
5207 * update_array_info is used to change the configuration of an
5208 * on-line array.
5209 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
5210 * fields in the info are checked against the array.
5211 * Any differences that cannot be handled will cause an error.
5212 * Normally, only one change can be managed at a time.
5214 static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
5216 int rv = 0;
5217 int cnt = 0;
5218 int state = 0;
5220 /* calculate expected state,ignoring low bits */
5221 if (mddev->bitmap && mddev->bitmap_offset)
5222 state |= (1 << MD_SB_BITMAP_PRESENT);
5224 if (mddev->major_version != info->major_version ||
5225 mddev->minor_version != info->minor_version ||
5226 /* mddev->patch_version != info->patch_version || */
5227 mddev->ctime != info->ctime ||
5228 mddev->level != info->level ||
5229 /* mddev->layout != info->layout || */
5230 !mddev->persistent != info->not_persistent||
5231 mddev->chunk_sectors != info->chunk_size >> 9 ||
5232 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
5233 ((state^info->state) & 0xfffffe00)
5235 return -EINVAL;
5236 /* Check there is only one change */
5237 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
5238 cnt++;
5239 if (mddev->raid_disks != info->raid_disks)
5240 cnt++;
5241 if (mddev->layout != info->layout)
5242 cnt++;
5243 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
5244 cnt++;
5245 if (cnt == 0)
5246 return 0;
5247 if (cnt > 1)
5248 return -EINVAL;
5250 if (mddev->layout != info->layout) {
5251 /* Change layout
5252 * we don't need to do anything at the md level, the
5253 * personality will take care of it all.
5255 if (mddev->pers->check_reshape == NULL)
5256 return -EINVAL;
5257 else {
5258 mddev->new_layout = info->layout;
5259 rv = mddev->pers->check_reshape(mddev);
5260 if (rv)
5261 mddev->new_layout = mddev->layout;
5262 return rv;
5265 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
5266 rv = update_size(mddev, (sector_t)info->size * 2);
5268 if (mddev->raid_disks != info->raid_disks)
5269 rv = update_raid_disks(mddev, info->raid_disks);
5271 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
5272 if (mddev->pers->quiesce == NULL)
5273 return -EINVAL;
5274 if (mddev->recovery || mddev->sync_thread)
5275 return -EBUSY;
5276 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
5277 /* add the bitmap */
5278 if (mddev->bitmap)
5279 return -EEXIST;
5280 if (mddev->default_bitmap_offset == 0)
5281 return -EINVAL;
5282 mddev->bitmap_offset = mddev->default_bitmap_offset;
5283 mddev->pers->quiesce(mddev, 1);
5284 rv = bitmap_create(mddev);
5285 if (rv)
5286 bitmap_destroy(mddev);
5287 mddev->pers->quiesce(mddev, 0);
5288 } else {
5289 /* remove the bitmap */
5290 if (!mddev->bitmap)
5291 return -ENOENT;
5292 if (mddev->bitmap->file)
5293 return -EINVAL;
5294 mddev->pers->quiesce(mddev, 1);
5295 bitmap_destroy(mddev);
5296 mddev->pers->quiesce(mddev, 0);
5297 mddev->bitmap_offset = 0;
5300 md_update_sb(mddev, 1);
5301 return rv;
5304 static int set_disk_faulty(mddev_t *mddev, dev_t dev)
5306 mdk_rdev_t *rdev;
5308 if (mddev->pers == NULL)
5309 return -ENODEV;
5311 rdev = find_rdev(mddev, dev);
5312 if (!rdev)
5313 return -ENODEV;
5315 md_error(mddev, rdev);
5316 return 0;
5320 * We have a problem here : there is no easy way to give a CHS
5321 * virtual geometry. We currently pretend that we have a 2 heads
5322 * 4 sectors (with a BIG number of cylinders...). This drives
5323 * dosfs just mad... ;-)
5325 static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
5327 mddev_t *mddev = bdev->bd_disk->private_data;
5329 geo->heads = 2;
5330 geo->sectors = 4;
5331 geo->cylinders = get_capacity(mddev->gendisk) / 8;
5332 return 0;
5335 static int md_ioctl(struct block_device *bdev, fmode_t mode,
5336 unsigned int cmd, unsigned long arg)
5338 int err = 0;
5339 void __user *argp = (void __user *)arg;
5340 mddev_t *mddev = NULL;
5341 int ro;
5343 if (!capable(CAP_SYS_ADMIN))
5344 return -EACCES;
5347 * Commands dealing with the RAID driver but not any
5348 * particular array:
5350 switch (cmd)
5352 case RAID_VERSION:
5353 err = get_version(argp);
5354 goto done;
5356 case PRINT_RAID_DEBUG:
5357 err = 0;
5358 md_print_devices();
5359 goto done;
5361 #ifndef MODULE
5362 case RAID_AUTORUN:
5363 err = 0;
5364 autostart_arrays(arg);
5365 goto done;
5366 #endif
5367 default:;
5371 * Commands creating/starting a new array:
5374 mddev = bdev->bd_disk->private_data;
5376 if (!mddev) {
5377 BUG();
5378 goto abort;
5381 err = mddev_lock(mddev);
5382 if (err) {
5383 printk(KERN_INFO
5384 "md: ioctl lock interrupted, reason %d, cmd %d\n",
5385 err, cmd);
5386 goto abort;
5389 switch (cmd)
5391 case SET_ARRAY_INFO:
5393 mdu_array_info_t info;
5394 if (!arg)
5395 memset(&info, 0, sizeof(info));
5396 else if (copy_from_user(&info, argp, sizeof(info))) {
5397 err = -EFAULT;
5398 goto abort_unlock;
5400 if (mddev->pers) {
5401 err = update_array_info(mddev, &info);
5402 if (err) {
5403 printk(KERN_WARNING "md: couldn't update"
5404 " array info. %d\n", err);
5405 goto abort_unlock;
5407 goto done_unlock;
5409 if (!list_empty(&mddev->disks)) {
5410 printk(KERN_WARNING
5411 "md: array %s already has disks!\n",
5412 mdname(mddev));
5413 err = -EBUSY;
5414 goto abort_unlock;
5416 if (mddev->raid_disks) {
5417 printk(KERN_WARNING
5418 "md: array %s already initialised!\n",
5419 mdname(mddev));
5420 err = -EBUSY;
5421 goto abort_unlock;
5423 err = set_array_info(mddev, &info);
5424 if (err) {
5425 printk(KERN_WARNING "md: couldn't set"
5426 " array info. %d\n", err);
5427 goto abort_unlock;
5430 goto done_unlock;
5432 default:;
5436 * Commands querying/configuring an existing array:
5438 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
5439 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
5440 if ((!mddev->raid_disks && !mddev->external)
5441 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
5442 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
5443 && cmd != GET_BITMAP_FILE) {
5444 err = -ENODEV;
5445 goto abort_unlock;
5449 * Commands even a read-only array can execute:
5451 switch (cmd)
5453 case GET_ARRAY_INFO:
5454 err = get_array_info(mddev, argp);
5455 goto done_unlock;
5457 case GET_BITMAP_FILE:
5458 err = get_bitmap_file(mddev, argp);
5459 goto done_unlock;
5461 case GET_DISK_INFO:
5462 err = get_disk_info(mddev, argp);
5463 goto done_unlock;
5465 case RESTART_ARRAY_RW:
5466 err = restart_array(mddev);
5467 goto done_unlock;
5469 case STOP_ARRAY:
5470 err = do_md_stop(mddev, 0, 1);
5471 goto done_unlock;
5473 case STOP_ARRAY_RO:
5474 err = do_md_stop(mddev, 1, 1);
5475 goto done_unlock;
5477 case BLKROSET:
5478 if (get_user(ro, (int __user *)(arg))) {
5479 err = -EFAULT;
5480 goto done_unlock;
5482 err = -EINVAL;
5484 /* if the bdev is going readonly the value of mddev->ro
5485 * does not matter, no writes are coming
5487 if (ro)
5488 goto done_unlock;
5490 /* are we are already prepared for writes? */
5491 if (mddev->ro != 1)
5492 goto done_unlock;
5494 /* transitioning to readauto need only happen for
5495 * arrays that call md_write_start
5497 if (mddev->pers) {
5498 err = restart_array(mddev);
5499 if (err == 0) {
5500 mddev->ro = 2;
5501 set_disk_ro(mddev->gendisk, 0);
5504 goto done_unlock;
5508 * The remaining ioctls are changing the state of the
5509 * superblock, so we do not allow them on read-only arrays.
5510 * However non-MD ioctls (e.g. get-size) will still come through
5511 * here and hit the 'default' below, so only disallow
5512 * 'md' ioctls, and switch to rw mode if started auto-readonly.
5514 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
5515 if (mddev->ro == 2) {
5516 mddev->ro = 0;
5517 sysfs_notify_dirent(mddev->sysfs_state);
5518 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5519 md_wakeup_thread(mddev->thread);
5520 } else {
5521 err = -EROFS;
5522 goto abort_unlock;
5526 switch (cmd)
5528 case ADD_NEW_DISK:
5530 mdu_disk_info_t info;
5531 if (copy_from_user(&info, argp, sizeof(info)))
5532 err = -EFAULT;
5533 else
5534 err = add_new_disk(mddev, &info);
5535 goto done_unlock;
5538 case HOT_REMOVE_DISK:
5539 err = hot_remove_disk(mddev, new_decode_dev(arg));
5540 goto done_unlock;
5542 case HOT_ADD_DISK:
5543 err = hot_add_disk(mddev, new_decode_dev(arg));
5544 goto done_unlock;
5546 case SET_DISK_FAULTY:
5547 err = set_disk_faulty(mddev, new_decode_dev(arg));
5548 goto done_unlock;
5550 case RUN_ARRAY:
5551 err = do_md_run(mddev);
5552 goto done_unlock;
5554 case SET_BITMAP_FILE:
5555 err = set_bitmap_file(mddev, (int)arg);
5556 goto done_unlock;
5558 default:
5559 err = -EINVAL;
5560 goto abort_unlock;
5563 done_unlock:
5564 abort_unlock:
5565 if (mddev->hold_active == UNTIL_IOCTL &&
5566 err != -EINVAL)
5567 mddev->hold_active = 0;
5568 mddev_unlock(mddev);
5570 return err;
5571 done:
5572 if (err)
5573 MD_BUG();
5574 abort:
5575 return err;
5578 static int md_open(struct block_device *bdev, fmode_t mode)
5581 * Succeed if we can lock the mddev, which confirms that
5582 * it isn't being stopped right now.
5584 mddev_t *mddev = mddev_find(bdev->bd_dev);
5585 int err;
5587 if (mddev->gendisk != bdev->bd_disk) {
5588 /* we are racing with mddev_put which is discarding this
5589 * bd_disk.
5591 mddev_put(mddev);
5592 /* Wait until bdev->bd_disk is definitely gone */
5593 flush_scheduled_work();
5594 /* Then retry the open from the top */
5595 return -ERESTARTSYS;
5597 BUG_ON(mddev != bdev->bd_disk->private_data);
5599 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
5600 goto out;
5602 err = 0;
5603 atomic_inc(&mddev->openers);
5604 mutex_unlock(&mddev->open_mutex);
5606 check_disk_change(bdev);
5607 out:
5608 return err;
5611 static int md_release(struct gendisk *disk, fmode_t mode)
5613 mddev_t *mddev = disk->private_data;
5615 BUG_ON(!mddev);
5616 atomic_dec(&mddev->openers);
5617 mddev_put(mddev);
5619 return 0;
5622 static int md_media_changed(struct gendisk *disk)
5624 mddev_t *mddev = disk->private_data;
5626 return mddev->changed;
5629 static int md_revalidate(struct gendisk *disk)
5631 mddev_t *mddev = disk->private_data;
5633 mddev->changed = 0;
5634 return 0;
5636 static const struct block_device_operations md_fops =
5638 .owner = THIS_MODULE,
5639 .open = md_open,
5640 .release = md_release,
5641 .ioctl = md_ioctl,
5642 .getgeo = md_getgeo,
5643 .media_changed = md_media_changed,
5644 .revalidate_disk= md_revalidate,
5647 static int md_thread(void * arg)
5649 mdk_thread_t *thread = arg;
5652 * md_thread is a 'system-thread', it's priority should be very
5653 * high. We avoid resource deadlocks individually in each
5654 * raid personality. (RAID5 does preallocation) We also use RR and
5655 * the very same RT priority as kswapd, thus we will never get
5656 * into a priority inversion deadlock.
5658 * we definitely have to have equal or higher priority than
5659 * bdflush, otherwise bdflush will deadlock if there are too
5660 * many dirty RAID5 blocks.
5663 allow_signal(SIGKILL);
5664 while (!kthread_should_stop()) {
5666 /* We need to wait INTERRUPTIBLE so that
5667 * we don't add to the load-average.
5668 * That means we need to be sure no signals are
5669 * pending
5671 if (signal_pending(current))
5672 flush_signals(current);
5674 wait_event_interruptible_timeout
5675 (thread->wqueue,
5676 test_bit(THREAD_WAKEUP, &thread->flags)
5677 || kthread_should_stop(),
5678 thread->timeout);
5680 clear_bit(THREAD_WAKEUP, &thread->flags);
5682 thread->run(thread->mddev);
5685 return 0;
5688 void md_wakeup_thread(mdk_thread_t *thread)
5690 if (thread) {
5691 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
5692 set_bit(THREAD_WAKEUP, &thread->flags);
5693 wake_up(&thread->wqueue);
5697 mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
5698 const char *name)
5700 mdk_thread_t *thread;
5702 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
5703 if (!thread)
5704 return NULL;
5706 init_waitqueue_head(&thread->wqueue);
5708 thread->run = run;
5709 thread->mddev = mddev;
5710 thread->timeout = MAX_SCHEDULE_TIMEOUT;
5711 thread->tsk = kthread_run(md_thread, thread,
5712 "%s_%s",
5713 mdname(thread->mddev),
5714 name ?: mddev->pers->name);
5715 if (IS_ERR(thread->tsk)) {
5716 kfree(thread);
5717 return NULL;
5719 return thread;
5722 void md_unregister_thread(mdk_thread_t *thread)
5724 if (!thread)
5725 return;
5726 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
5728 kthread_stop(thread->tsk);
5729 kfree(thread);
5732 void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
5734 if (!mddev) {
5735 MD_BUG();
5736 return;
5739 if (!rdev || test_bit(Faulty, &rdev->flags))
5740 return;
5742 if (mddev->external)
5743 set_bit(Blocked, &rdev->flags);
5745 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
5746 mdname(mddev),
5747 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
5748 __builtin_return_address(0),__builtin_return_address(1),
5749 __builtin_return_address(2),__builtin_return_address(3));
5751 if (!mddev->pers)
5752 return;
5753 if (!mddev->pers->error_handler)
5754 return;
5755 mddev->pers->error_handler(mddev,rdev);
5756 if (mddev->degraded)
5757 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
5758 set_bit(StateChanged, &rdev->flags);
5759 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5760 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5761 md_wakeup_thread(mddev->thread);
5762 md_new_event_inintr(mddev);
5765 /* seq_file implementation /proc/mdstat */
5767 static void status_unused(struct seq_file *seq)
5769 int i = 0;
5770 mdk_rdev_t *rdev;
5772 seq_printf(seq, "unused devices: ");
5774 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
5775 char b[BDEVNAME_SIZE];
5776 i++;
5777 seq_printf(seq, "%s ",
5778 bdevname(rdev->bdev,b));
5780 if (!i)
5781 seq_printf(seq, "<none>");
5783 seq_printf(seq, "\n");
5787 static void status_resync(struct seq_file *seq, mddev_t * mddev)
5789 sector_t max_sectors, resync, res;
5790 unsigned long dt, db;
5791 sector_t rt;
5792 int scale;
5793 unsigned int per_milli;
5795 resync = mddev->curr_resync - atomic_read(&mddev->recovery_active);
5797 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
5798 max_sectors = mddev->resync_max_sectors;
5799 else
5800 max_sectors = mddev->dev_sectors;
5803 * Should not happen.
5805 if (!max_sectors) {
5806 MD_BUG();
5807 return;
5809 /* Pick 'scale' such that (resync>>scale)*1000 will fit
5810 * in a sector_t, and (max_sectors>>scale) will fit in a
5811 * u32, as those are the requirements for sector_div.
5812 * Thus 'scale' must be at least 10
5814 scale = 10;
5815 if (sizeof(sector_t) > sizeof(unsigned long)) {
5816 while ( max_sectors/2 > (1ULL<<(scale+32)))
5817 scale++;
5819 res = (resync>>scale)*1000;
5820 sector_div(res, (u32)((max_sectors>>scale)+1));
5822 per_milli = res;
5824 int i, x = per_milli/50, y = 20-x;
5825 seq_printf(seq, "[");
5826 for (i = 0; i < x; i++)
5827 seq_printf(seq, "=");
5828 seq_printf(seq, ">");
5829 for (i = 0; i < y; i++)
5830 seq_printf(seq, ".");
5831 seq_printf(seq, "] ");
5833 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
5834 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
5835 "reshape" :
5836 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
5837 "check" :
5838 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
5839 "resync" : "recovery"))),
5840 per_milli/10, per_milli % 10,
5841 (unsigned long long) resync/2,
5842 (unsigned long long) max_sectors/2);
5845 * dt: time from mark until now
5846 * db: blocks written from mark until now
5847 * rt: remaining time
5849 * rt is a sector_t, so could be 32bit or 64bit.
5850 * So we divide before multiply in case it is 32bit and close
5851 * to the limit.
5852 * We scale the divisor (db) by 32 to avoid loosing precision
5853 * near the end of resync when the number of remaining sectors
5854 * is close to 'db'.
5855 * We then divide rt by 32 after multiplying by db to compensate.
5856 * The '+1' avoids division by zero if db is very small.
5858 dt = ((jiffies - mddev->resync_mark) / HZ);
5859 if (!dt) dt++;
5860 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
5861 - mddev->resync_mark_cnt;
5863 rt = max_sectors - resync; /* number of remaining sectors */
5864 sector_div(rt, db/32+1);
5865 rt *= dt;
5866 rt >>= 5;
5868 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
5869 ((unsigned long)rt % 60)/6);
5871 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
5874 static void *md_seq_start(struct seq_file *seq, loff_t *pos)
5876 struct list_head *tmp;
5877 loff_t l = *pos;
5878 mddev_t *mddev;
5880 if (l >= 0x10000)
5881 return NULL;
5882 if (!l--)
5883 /* header */
5884 return (void*)1;
5886 spin_lock(&all_mddevs_lock);
5887 list_for_each(tmp,&all_mddevs)
5888 if (!l--) {
5889 mddev = list_entry(tmp, mddev_t, all_mddevs);
5890 mddev_get(mddev);
5891 spin_unlock(&all_mddevs_lock);
5892 return mddev;
5894 spin_unlock(&all_mddevs_lock);
5895 if (!l--)
5896 return (void*)2;/* tail */
5897 return NULL;
5900 static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
5902 struct list_head *tmp;
5903 mddev_t *next_mddev, *mddev = v;
5905 ++*pos;
5906 if (v == (void*)2)
5907 return NULL;
5909 spin_lock(&all_mddevs_lock);
5910 if (v == (void*)1)
5911 tmp = all_mddevs.next;
5912 else
5913 tmp = mddev->all_mddevs.next;
5914 if (tmp != &all_mddevs)
5915 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
5916 else {
5917 next_mddev = (void*)2;
5918 *pos = 0x10000;
5920 spin_unlock(&all_mddevs_lock);
5922 if (v != (void*)1)
5923 mddev_put(mddev);
5924 return next_mddev;
5928 static void md_seq_stop(struct seq_file *seq, void *v)
5930 mddev_t *mddev = v;
5932 if (mddev && v != (void*)1 && v != (void*)2)
5933 mddev_put(mddev);
5936 struct mdstat_info {
5937 int event;
5940 static int md_seq_show(struct seq_file *seq, void *v)
5942 mddev_t *mddev = v;
5943 sector_t sectors;
5944 mdk_rdev_t *rdev;
5945 struct mdstat_info *mi = seq->private;
5946 struct bitmap *bitmap;
5948 if (v == (void*)1) {
5949 struct mdk_personality *pers;
5950 seq_printf(seq, "Personalities : ");
5951 spin_lock(&pers_lock);
5952 list_for_each_entry(pers, &pers_list, list)
5953 seq_printf(seq, "[%s] ", pers->name);
5955 spin_unlock(&pers_lock);
5956 seq_printf(seq, "\n");
5957 mi->event = atomic_read(&md_event_count);
5958 return 0;
5960 if (v == (void*)2) {
5961 status_unused(seq);
5962 return 0;
5965 if (mddev_lock(mddev) < 0)
5966 return -EINTR;
5968 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
5969 seq_printf(seq, "%s : %sactive", mdname(mddev),
5970 mddev->pers ? "" : "in");
5971 if (mddev->pers) {
5972 if (mddev->ro==1)
5973 seq_printf(seq, " (read-only)");
5974 if (mddev->ro==2)
5975 seq_printf(seq, " (auto-read-only)");
5976 seq_printf(seq, " %s", mddev->pers->name);
5979 sectors = 0;
5980 list_for_each_entry(rdev, &mddev->disks, same_set) {
5981 char b[BDEVNAME_SIZE];
5982 seq_printf(seq, " %s[%d]",
5983 bdevname(rdev->bdev,b), rdev->desc_nr);
5984 if (test_bit(WriteMostly, &rdev->flags))
5985 seq_printf(seq, "(W)");
5986 if (test_bit(Faulty, &rdev->flags)) {
5987 seq_printf(seq, "(F)");
5988 continue;
5989 } else if (rdev->raid_disk < 0)
5990 seq_printf(seq, "(S)"); /* spare */
5991 sectors += rdev->sectors;
5994 if (!list_empty(&mddev->disks)) {
5995 if (mddev->pers)
5996 seq_printf(seq, "\n %llu blocks",
5997 (unsigned long long)
5998 mddev->array_sectors / 2);
5999 else
6000 seq_printf(seq, "\n %llu blocks",
6001 (unsigned long long)sectors / 2);
6003 if (mddev->persistent) {
6004 if (mddev->major_version != 0 ||
6005 mddev->minor_version != 90) {
6006 seq_printf(seq," super %d.%d",
6007 mddev->major_version,
6008 mddev->minor_version);
6010 } else if (mddev->external)
6011 seq_printf(seq, " super external:%s",
6012 mddev->metadata_type);
6013 else
6014 seq_printf(seq, " super non-persistent");
6016 if (mddev->pers) {
6017 mddev->pers->status(seq, mddev);
6018 seq_printf(seq, "\n ");
6019 if (mddev->pers->sync_request) {
6020 if (mddev->curr_resync > 2) {
6021 status_resync(seq, mddev);
6022 seq_printf(seq, "\n ");
6023 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
6024 seq_printf(seq, "\tresync=DELAYED\n ");
6025 else if (mddev->recovery_cp < MaxSector)
6026 seq_printf(seq, "\tresync=PENDING\n ");
6028 } else
6029 seq_printf(seq, "\n ");
6031 if ((bitmap = mddev->bitmap)) {
6032 unsigned long chunk_kb;
6033 unsigned long flags;
6034 spin_lock_irqsave(&bitmap->lock, flags);
6035 chunk_kb = bitmap->chunksize >> 10;
6036 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
6037 "%lu%s chunk",
6038 bitmap->pages - bitmap->missing_pages,
6039 bitmap->pages,
6040 (bitmap->pages - bitmap->missing_pages)
6041 << (PAGE_SHIFT - 10),
6042 chunk_kb ? chunk_kb : bitmap->chunksize,
6043 chunk_kb ? "KB" : "B");
6044 if (bitmap->file) {
6045 seq_printf(seq, ", file: ");
6046 seq_path(seq, &bitmap->file->f_path, " \t\n");
6049 seq_printf(seq, "\n");
6050 spin_unlock_irqrestore(&bitmap->lock, flags);
6053 seq_printf(seq, "\n");
6055 mddev_unlock(mddev);
6057 return 0;
6060 static const struct seq_operations md_seq_ops = {
6061 .start = md_seq_start,
6062 .next = md_seq_next,
6063 .stop = md_seq_stop,
6064 .show = md_seq_show,
6067 static int md_seq_open(struct inode *inode, struct file *file)
6069 int error;
6070 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
6071 if (mi == NULL)
6072 return -ENOMEM;
6074 error = seq_open(file, &md_seq_ops);
6075 if (error)
6076 kfree(mi);
6077 else {
6078 struct seq_file *p = file->private_data;
6079 p->private = mi;
6080 mi->event = atomic_read(&md_event_count);
6082 return error;
6085 static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
6087 struct seq_file *m = filp->private_data;
6088 struct mdstat_info *mi = m->private;
6089 int mask;
6091 poll_wait(filp, &md_event_waiters, wait);
6093 /* always allow read */
6094 mask = POLLIN | POLLRDNORM;
6096 if (mi->event != atomic_read(&md_event_count))
6097 mask |= POLLERR | POLLPRI;
6098 return mask;
6101 static const struct file_operations md_seq_fops = {
6102 .owner = THIS_MODULE,
6103 .open = md_seq_open,
6104 .read = seq_read,
6105 .llseek = seq_lseek,
6106 .release = seq_release_private,
6107 .poll = mdstat_poll,
6110 int register_md_personality(struct mdk_personality *p)
6112 spin_lock(&pers_lock);
6113 list_add_tail(&p->list, &pers_list);
6114 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
6115 spin_unlock(&pers_lock);
6116 return 0;
6119 int unregister_md_personality(struct mdk_personality *p)
6121 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
6122 spin_lock(&pers_lock);
6123 list_del_init(&p->list);
6124 spin_unlock(&pers_lock);
6125 return 0;
6128 static int is_mddev_idle(mddev_t *mddev, int init)
6130 mdk_rdev_t * rdev;
6131 int idle;
6132 int curr_events;
6134 idle = 1;
6135 rcu_read_lock();
6136 rdev_for_each_rcu(rdev, mddev) {
6137 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
6138 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
6139 (int)part_stat_read(&disk->part0, sectors[1]) -
6140 atomic_read(&disk->sync_io);
6141 /* sync IO will cause sync_io to increase before the disk_stats
6142 * as sync_io is counted when a request starts, and
6143 * disk_stats is counted when it completes.
6144 * So resync activity will cause curr_events to be smaller than
6145 * when there was no such activity.
6146 * non-sync IO will cause disk_stat to increase without
6147 * increasing sync_io so curr_events will (eventually)
6148 * be larger than it was before. Once it becomes
6149 * substantially larger, the test below will cause
6150 * the array to appear non-idle, and resync will slow
6151 * down.
6152 * If there is a lot of outstanding resync activity when
6153 * we set last_event to curr_events, then all that activity
6154 * completing might cause the array to appear non-idle
6155 * and resync will be slowed down even though there might
6156 * not have been non-resync activity. This will only
6157 * happen once though. 'last_events' will soon reflect
6158 * the state where there is little or no outstanding
6159 * resync requests, and further resync activity will
6160 * always make curr_events less than last_events.
6163 if (init || curr_events - rdev->last_events > 64) {
6164 rdev->last_events = curr_events;
6165 idle = 0;
6168 rcu_read_unlock();
6169 return idle;
6172 void md_done_sync(mddev_t *mddev, int blocks, int ok)
6174 /* another "blocks" (512byte) blocks have been synced */
6175 atomic_sub(blocks, &mddev->recovery_active);
6176 wake_up(&mddev->recovery_wait);
6177 if (!ok) {
6178 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6179 md_wakeup_thread(mddev->thread);
6180 // stop recovery, signal do_sync ....
6185 /* md_write_start(mddev, bi)
6186 * If we need to update some array metadata (e.g. 'active' flag
6187 * in superblock) before writing, schedule a superblock update
6188 * and wait for it to complete.
6190 void md_write_start(mddev_t *mddev, struct bio *bi)
6192 int did_change = 0;
6193 if (bio_data_dir(bi) != WRITE)
6194 return;
6196 BUG_ON(mddev->ro == 1);
6197 if (mddev->ro == 2) {
6198 /* need to switch to read/write */
6199 mddev->ro = 0;
6200 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6201 md_wakeup_thread(mddev->thread);
6202 md_wakeup_thread(mddev->sync_thread);
6203 did_change = 1;
6205 atomic_inc(&mddev->writes_pending);
6206 if (mddev->safemode == 1)
6207 mddev->safemode = 0;
6208 if (mddev->in_sync) {
6209 spin_lock_irq(&mddev->write_lock);
6210 if (mddev->in_sync) {
6211 mddev->in_sync = 0;
6212 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6213 md_wakeup_thread(mddev->thread);
6214 did_change = 1;
6216 spin_unlock_irq(&mddev->write_lock);
6218 if (did_change)
6219 sysfs_notify_dirent(mddev->sysfs_state);
6220 wait_event(mddev->sb_wait,
6221 !test_bit(MD_CHANGE_CLEAN, &mddev->flags) &&
6222 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
6225 void md_write_end(mddev_t *mddev)
6227 if (atomic_dec_and_test(&mddev->writes_pending)) {
6228 if (mddev->safemode == 2)
6229 md_wakeup_thread(mddev->thread);
6230 else if (mddev->safemode_delay)
6231 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
6235 /* md_allow_write(mddev)
6236 * Calling this ensures that the array is marked 'active' so that writes
6237 * may proceed without blocking. It is important to call this before
6238 * attempting a GFP_KERNEL allocation while holding the mddev lock.
6239 * Must be called with mddev_lock held.
6241 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
6242 * is dropped, so return -EAGAIN after notifying userspace.
6244 int md_allow_write(mddev_t *mddev)
6246 if (!mddev->pers)
6247 return 0;
6248 if (mddev->ro)
6249 return 0;
6250 if (!mddev->pers->sync_request)
6251 return 0;
6253 spin_lock_irq(&mddev->write_lock);
6254 if (mddev->in_sync) {
6255 mddev->in_sync = 0;
6256 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6257 if (mddev->safemode_delay &&
6258 mddev->safemode == 0)
6259 mddev->safemode = 1;
6260 spin_unlock_irq(&mddev->write_lock);
6261 md_update_sb(mddev, 0);
6262 sysfs_notify_dirent(mddev->sysfs_state);
6263 } else
6264 spin_unlock_irq(&mddev->write_lock);
6266 if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
6267 return -EAGAIN;
6268 else
6269 return 0;
6271 EXPORT_SYMBOL_GPL(md_allow_write);
6273 #define SYNC_MARKS 10
6274 #define SYNC_MARK_STEP (3*HZ)
6275 void md_do_sync(mddev_t *mddev)
6277 mddev_t *mddev2;
6278 unsigned int currspeed = 0,
6279 window;
6280 sector_t max_sectors,j, io_sectors;
6281 unsigned long mark[SYNC_MARKS];
6282 sector_t mark_cnt[SYNC_MARKS];
6283 int last_mark,m;
6284 struct list_head *tmp;
6285 sector_t last_check;
6286 int skipped = 0;
6287 mdk_rdev_t *rdev;
6288 char *desc;
6290 /* just incase thread restarts... */
6291 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
6292 return;
6293 if (mddev->ro) /* never try to sync a read-only array */
6294 return;
6296 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6297 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
6298 desc = "data-check";
6299 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6300 desc = "requested-resync";
6301 else
6302 desc = "resync";
6303 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
6304 desc = "reshape";
6305 else
6306 desc = "recovery";
6308 /* we overload curr_resync somewhat here.
6309 * 0 == not engaged in resync at all
6310 * 2 == checking that there is no conflict with another sync
6311 * 1 == like 2, but have yielded to allow conflicting resync to
6312 * commense
6313 * other == active in resync - this many blocks
6315 * Before starting a resync we must have set curr_resync to
6316 * 2, and then checked that every "conflicting" array has curr_resync
6317 * less than ours. When we find one that is the same or higher
6318 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
6319 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
6320 * This will mean we have to start checking from the beginning again.
6324 do {
6325 mddev->curr_resync = 2;
6327 try_again:
6328 if (kthread_should_stop()) {
6329 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6330 goto skip;
6332 for_each_mddev(mddev2, tmp) {
6333 if (mddev2 == mddev)
6334 continue;
6335 if (!mddev->parallel_resync
6336 && mddev2->curr_resync
6337 && match_mddev_units(mddev, mddev2)) {
6338 DEFINE_WAIT(wq);
6339 if (mddev < mddev2 && mddev->curr_resync == 2) {
6340 /* arbitrarily yield */
6341 mddev->curr_resync = 1;
6342 wake_up(&resync_wait);
6344 if (mddev > mddev2 && mddev->curr_resync == 1)
6345 /* no need to wait here, we can wait the next
6346 * time 'round when curr_resync == 2
6348 continue;
6349 /* We need to wait 'interruptible' so as not to
6350 * contribute to the load average, and not to
6351 * be caught by 'softlockup'
6353 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
6354 if (!kthread_should_stop() &&
6355 mddev2->curr_resync >= mddev->curr_resync) {
6356 printk(KERN_INFO "md: delaying %s of %s"
6357 " until %s has finished (they"
6358 " share one or more physical units)\n",
6359 desc, mdname(mddev), mdname(mddev2));
6360 mddev_put(mddev2);
6361 if (signal_pending(current))
6362 flush_signals(current);
6363 schedule();
6364 finish_wait(&resync_wait, &wq);
6365 goto try_again;
6367 finish_wait(&resync_wait, &wq);
6370 } while (mddev->curr_resync < 2);
6372 j = 0;
6373 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6374 /* resync follows the size requested by the personality,
6375 * which defaults to physical size, but can be virtual size
6377 max_sectors = mddev->resync_max_sectors;
6378 mddev->resync_mismatches = 0;
6379 /* we don't use the checkpoint if there's a bitmap */
6380 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6381 j = mddev->resync_min;
6382 else if (!mddev->bitmap)
6383 j = mddev->recovery_cp;
6385 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
6386 max_sectors = mddev->dev_sectors;
6387 else {
6388 /* recovery follows the physical size of devices */
6389 max_sectors = mddev->dev_sectors;
6390 j = MaxSector;
6391 list_for_each_entry(rdev, &mddev->disks, same_set)
6392 if (rdev->raid_disk >= 0 &&
6393 !test_bit(Faulty, &rdev->flags) &&
6394 !test_bit(In_sync, &rdev->flags) &&
6395 rdev->recovery_offset < j)
6396 j = rdev->recovery_offset;
6399 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
6400 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
6401 " %d KB/sec/disk.\n", speed_min(mddev));
6402 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
6403 "(but not more than %d KB/sec) for %s.\n",
6404 speed_max(mddev), desc);
6406 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
6408 io_sectors = 0;
6409 for (m = 0; m < SYNC_MARKS; m++) {
6410 mark[m] = jiffies;
6411 mark_cnt[m] = io_sectors;
6413 last_mark = 0;
6414 mddev->resync_mark = mark[last_mark];
6415 mddev->resync_mark_cnt = mark_cnt[last_mark];
6418 * Tune reconstruction:
6420 window = 32*(PAGE_SIZE/512);
6421 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
6422 window/2,(unsigned long long) max_sectors/2);
6424 atomic_set(&mddev->recovery_active, 0);
6425 last_check = 0;
6427 if (j>2) {
6428 printk(KERN_INFO
6429 "md: resuming %s of %s from checkpoint.\n",
6430 desc, mdname(mddev));
6431 mddev->curr_resync = j;
6434 while (j < max_sectors) {
6435 sector_t sectors;
6437 skipped = 0;
6439 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
6440 ((mddev->curr_resync > mddev->curr_resync_completed &&
6441 (mddev->curr_resync - mddev->curr_resync_completed)
6442 > (max_sectors >> 4)) ||
6443 (j - mddev->curr_resync_completed)*2
6444 >= mddev->resync_max - mddev->curr_resync_completed
6445 )) {
6446 /* time to update curr_resync_completed */
6447 blk_unplug(mddev->queue);
6448 wait_event(mddev->recovery_wait,
6449 atomic_read(&mddev->recovery_active) == 0);
6450 mddev->curr_resync_completed =
6451 mddev->curr_resync;
6452 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6453 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
6456 while (j >= mddev->resync_max && !kthread_should_stop()) {
6457 /* As this condition is controlled by user-space,
6458 * we can block indefinitely, so use '_interruptible'
6459 * to avoid triggering warnings.
6461 flush_signals(current); /* just in case */
6462 wait_event_interruptible(mddev->recovery_wait,
6463 mddev->resync_max > j
6464 || kthread_should_stop());
6467 if (kthread_should_stop())
6468 goto interrupted;
6470 sectors = mddev->pers->sync_request(mddev, j, &skipped,
6471 currspeed < speed_min(mddev));
6472 if (sectors == 0) {
6473 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6474 goto out;
6477 if (!skipped) { /* actual IO requested */
6478 io_sectors += sectors;
6479 atomic_add(sectors, &mddev->recovery_active);
6482 j += sectors;
6483 if (j>1) mddev->curr_resync = j;
6484 mddev->curr_mark_cnt = io_sectors;
6485 if (last_check == 0)
6486 /* this is the earliers that rebuilt will be
6487 * visible in /proc/mdstat
6489 md_new_event(mddev);
6491 if (last_check + window > io_sectors || j == max_sectors)
6492 continue;
6494 last_check = io_sectors;
6496 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6497 break;
6499 repeat:
6500 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
6501 /* step marks */
6502 int next = (last_mark+1) % SYNC_MARKS;
6504 mddev->resync_mark = mark[next];
6505 mddev->resync_mark_cnt = mark_cnt[next];
6506 mark[next] = jiffies;
6507 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
6508 last_mark = next;
6512 if (kthread_should_stop())
6513 goto interrupted;
6517 * this loop exits only if either when we are slower than
6518 * the 'hard' speed limit, or the system was IO-idle for
6519 * a jiffy.
6520 * the system might be non-idle CPU-wise, but we only care
6521 * about not overloading the IO subsystem. (things like an
6522 * e2fsck being done on the RAID array should execute fast)
6524 blk_unplug(mddev->queue);
6525 cond_resched();
6527 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
6528 /((jiffies-mddev->resync_mark)/HZ +1) +1;
6530 if (currspeed > speed_min(mddev)) {
6531 if ((currspeed > speed_max(mddev)) ||
6532 !is_mddev_idle(mddev, 0)) {
6533 msleep(500);
6534 goto repeat;
6538 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
6540 * this also signals 'finished resyncing' to md_stop
6542 out:
6543 blk_unplug(mddev->queue);
6545 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
6547 /* tell personality that we are finished */
6548 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
6550 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
6551 mddev->curr_resync > 2) {
6552 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6553 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6554 if (mddev->curr_resync >= mddev->recovery_cp) {
6555 printk(KERN_INFO
6556 "md: checkpointing %s of %s.\n",
6557 desc, mdname(mddev));
6558 mddev->recovery_cp = mddev->curr_resync;
6560 } else
6561 mddev->recovery_cp = MaxSector;
6562 } else {
6563 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6564 mddev->curr_resync = MaxSector;
6565 list_for_each_entry(rdev, &mddev->disks, same_set)
6566 if (rdev->raid_disk >= 0 &&
6567 !test_bit(Faulty, &rdev->flags) &&
6568 !test_bit(In_sync, &rdev->flags) &&
6569 rdev->recovery_offset < mddev->curr_resync)
6570 rdev->recovery_offset = mddev->curr_resync;
6573 set_bit(MD_CHANGE_DEVS, &mddev->flags);
6575 skip:
6576 mddev->curr_resync = 0;
6577 mddev->curr_resync_completed = 0;
6578 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6579 /* We completed so max setting can be forgotten. */
6580 mddev->resync_max = MaxSector;
6581 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
6582 wake_up(&resync_wait);
6583 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
6584 md_wakeup_thread(mddev->thread);
6585 return;
6587 interrupted:
6589 * got a signal, exit.
6591 printk(KERN_INFO
6592 "md: md_do_sync() got signal ... exiting\n");
6593 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6594 goto out;
6597 EXPORT_SYMBOL_GPL(md_do_sync);
6600 static int remove_and_add_spares(mddev_t *mddev)
6602 mdk_rdev_t *rdev;
6603 int spares = 0;
6605 mddev->curr_resync_completed = 0;
6607 list_for_each_entry(rdev, &mddev->disks, same_set)
6608 if (rdev->raid_disk >= 0 &&
6609 !test_bit(Blocked, &rdev->flags) &&
6610 (test_bit(Faulty, &rdev->flags) ||
6611 ! test_bit(In_sync, &rdev->flags)) &&
6612 atomic_read(&rdev->nr_pending)==0) {
6613 if (mddev->pers->hot_remove_disk(
6614 mddev, rdev->raid_disk)==0) {
6615 char nm[20];
6616 sprintf(nm,"rd%d", rdev->raid_disk);
6617 sysfs_remove_link(&mddev->kobj, nm);
6618 rdev->raid_disk = -1;
6622 if (mddev->degraded && ! mddev->ro && !mddev->recovery_disabled) {
6623 list_for_each_entry(rdev, &mddev->disks, same_set) {
6624 if (rdev->raid_disk >= 0 &&
6625 !test_bit(In_sync, &rdev->flags) &&
6626 !test_bit(Blocked, &rdev->flags))
6627 spares++;
6628 if (rdev->raid_disk < 0
6629 && !test_bit(Faulty, &rdev->flags)) {
6630 rdev->recovery_offset = 0;
6631 if (mddev->pers->
6632 hot_add_disk(mddev, rdev) == 0) {
6633 char nm[20];
6634 sprintf(nm, "rd%d", rdev->raid_disk);
6635 if (sysfs_create_link(&mddev->kobj,
6636 &rdev->kobj, nm))
6637 printk(KERN_WARNING
6638 "md: cannot register "
6639 "%s for %s\n",
6640 nm, mdname(mddev));
6641 spares++;
6642 md_new_event(mddev);
6643 } else
6644 break;
6648 return spares;
6651 * This routine is regularly called by all per-raid-array threads to
6652 * deal with generic issues like resync and super-block update.
6653 * Raid personalities that don't have a thread (linear/raid0) do not
6654 * need this as they never do any recovery or update the superblock.
6656 * It does not do any resync itself, but rather "forks" off other threads
6657 * to do that as needed.
6658 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
6659 * "->recovery" and create a thread at ->sync_thread.
6660 * When the thread finishes it sets MD_RECOVERY_DONE
6661 * and wakeups up this thread which will reap the thread and finish up.
6662 * This thread also removes any faulty devices (with nr_pending == 0).
6664 * The overall approach is:
6665 * 1/ if the superblock needs updating, update it.
6666 * 2/ If a recovery thread is running, don't do anything else.
6667 * 3/ If recovery has finished, clean up, possibly marking spares active.
6668 * 4/ If there are any faulty devices, remove them.
6669 * 5/ If array is degraded, try to add spares devices
6670 * 6/ If array has spares or is not in-sync, start a resync thread.
6672 void md_check_recovery(mddev_t *mddev)
6674 mdk_rdev_t *rdev;
6677 if (mddev->bitmap)
6678 bitmap_daemon_work(mddev);
6680 if (mddev->ro)
6681 return;
6683 if (signal_pending(current)) {
6684 if (mddev->pers->sync_request && !mddev->external) {
6685 printk(KERN_INFO "md: %s in immediate safe mode\n",
6686 mdname(mddev));
6687 mddev->safemode = 2;
6689 flush_signals(current);
6692 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
6693 return;
6694 if ( ! (
6695 (mddev->flags && !mddev->external) ||
6696 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
6697 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
6698 (mddev->external == 0 && mddev->safemode == 1) ||
6699 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
6700 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
6702 return;
6704 if (mddev_trylock(mddev)) {
6705 int spares = 0;
6707 if (mddev->ro) {
6708 /* Only thing we do on a ro array is remove
6709 * failed devices.
6711 remove_and_add_spares(mddev);
6712 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6713 goto unlock;
6716 if (!mddev->external) {
6717 int did_change = 0;
6718 spin_lock_irq(&mddev->write_lock);
6719 if (mddev->safemode &&
6720 !atomic_read(&mddev->writes_pending) &&
6721 !mddev->in_sync &&
6722 mddev->recovery_cp == MaxSector) {
6723 mddev->in_sync = 1;
6724 did_change = 1;
6725 if (mddev->persistent)
6726 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6728 if (mddev->safemode == 1)
6729 mddev->safemode = 0;
6730 spin_unlock_irq(&mddev->write_lock);
6731 if (did_change)
6732 sysfs_notify_dirent(mddev->sysfs_state);
6735 if (mddev->flags)
6736 md_update_sb(mddev, 0);
6738 list_for_each_entry(rdev, &mddev->disks, same_set)
6739 if (test_and_clear_bit(StateChanged, &rdev->flags))
6740 sysfs_notify_dirent(rdev->sysfs_state);
6743 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
6744 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
6745 /* resync/recovery still happening */
6746 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6747 goto unlock;
6749 if (mddev->sync_thread) {
6750 /* resync has finished, collect result */
6751 md_unregister_thread(mddev->sync_thread);
6752 mddev->sync_thread = NULL;
6753 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
6754 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
6755 /* success...*/
6756 /* activate any spares */
6757 if (mddev->pers->spare_active(mddev))
6758 sysfs_notify(&mddev->kobj, NULL,
6759 "degraded");
6761 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
6762 mddev->pers->finish_reshape)
6763 mddev->pers->finish_reshape(mddev);
6764 md_update_sb(mddev, 1);
6766 /* if array is no-longer degraded, then any saved_raid_disk
6767 * information must be scrapped
6769 if (!mddev->degraded)
6770 list_for_each_entry(rdev, &mddev->disks, same_set)
6771 rdev->saved_raid_disk = -1;
6773 mddev->recovery = 0;
6774 /* flag recovery needed just to double check */
6775 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6776 sysfs_notify_dirent(mddev->sysfs_action);
6777 md_new_event(mddev);
6778 goto unlock;
6780 /* Set RUNNING before clearing NEEDED to avoid
6781 * any transients in the value of "sync_action".
6783 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6784 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6785 /* Clear some bits that don't mean anything, but
6786 * might be left set
6788 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
6789 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
6791 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
6792 goto unlock;
6793 /* no recovery is running.
6794 * remove any failed drives, then
6795 * add spares if possible.
6796 * Spare are also removed and re-added, to allow
6797 * the personality to fail the re-add.
6800 if (mddev->reshape_position != MaxSector) {
6801 if (mddev->pers->check_reshape == NULL ||
6802 mddev->pers->check_reshape(mddev) != 0)
6803 /* Cannot proceed */
6804 goto unlock;
6805 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
6806 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
6807 } else if ((spares = remove_and_add_spares(mddev))) {
6808 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
6809 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
6810 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
6811 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
6812 } else if (mddev->recovery_cp < MaxSector) {
6813 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
6814 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
6815 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
6816 /* nothing to be done ... */
6817 goto unlock;
6819 if (mddev->pers->sync_request) {
6820 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
6821 /* We are adding a device or devices to an array
6822 * which has the bitmap stored on all devices.
6823 * So make sure all bitmap pages get written
6825 bitmap_write_all(mddev->bitmap);
6827 mddev->sync_thread = md_register_thread(md_do_sync,
6828 mddev,
6829 "resync");
6830 if (!mddev->sync_thread) {
6831 printk(KERN_ERR "%s: could not start resync"
6832 " thread...\n",
6833 mdname(mddev));
6834 /* leave the spares where they are, it shouldn't hurt */
6835 mddev->recovery = 0;
6836 } else
6837 md_wakeup_thread(mddev->sync_thread);
6838 sysfs_notify_dirent(mddev->sysfs_action);
6839 md_new_event(mddev);
6841 unlock:
6842 if (!mddev->sync_thread) {
6843 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6844 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
6845 &mddev->recovery))
6846 if (mddev->sysfs_action)
6847 sysfs_notify_dirent(mddev->sysfs_action);
6849 mddev_unlock(mddev);
6853 void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
6855 sysfs_notify_dirent(rdev->sysfs_state);
6856 wait_event_timeout(rdev->blocked_wait,
6857 !test_bit(Blocked, &rdev->flags),
6858 msecs_to_jiffies(5000));
6859 rdev_dec_pending(rdev, mddev);
6861 EXPORT_SYMBOL(md_wait_for_blocked_rdev);
6863 static int md_notify_reboot(struct notifier_block *this,
6864 unsigned long code, void *x)
6866 struct list_head *tmp;
6867 mddev_t *mddev;
6869 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
6871 printk(KERN_INFO "md: stopping all md devices.\n");
6873 for_each_mddev(mddev, tmp)
6874 if (mddev_trylock(mddev)) {
6875 /* Force a switch to readonly even array
6876 * appears to still be in use. Hence
6877 * the '100'.
6879 do_md_stop(mddev, 1, 100);
6880 mddev_unlock(mddev);
6883 * certain more exotic SCSI devices are known to be
6884 * volatile wrt too early system reboots. While the
6885 * right place to handle this issue is the given
6886 * driver, we do want to have a safe RAID driver ...
6888 mdelay(1000*1);
6890 return NOTIFY_DONE;
6893 static struct notifier_block md_notifier = {
6894 .notifier_call = md_notify_reboot,
6895 .next = NULL,
6896 .priority = INT_MAX, /* before any real devices */
6899 static void md_geninit(void)
6901 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
6903 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
6906 static int __init md_init(void)
6908 if (register_blkdev(MD_MAJOR, "md"))
6909 return -1;
6910 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
6911 unregister_blkdev(MD_MAJOR, "md");
6912 return -1;
6914 blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE,
6915 md_probe, NULL, NULL);
6916 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
6917 md_probe, NULL, NULL);
6919 register_reboot_notifier(&md_notifier);
6920 raid_table_header = register_sysctl_table(raid_root_table);
6922 md_geninit();
6923 return 0;
6927 #ifndef MODULE
6930 * Searches all registered partitions for autorun RAID arrays
6931 * at boot time.
6934 static LIST_HEAD(all_detected_devices);
6935 struct detected_devices_node {
6936 struct list_head list;
6937 dev_t dev;
6940 void md_autodetect_dev(dev_t dev)
6942 struct detected_devices_node *node_detected_dev;
6944 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
6945 if (node_detected_dev) {
6946 node_detected_dev->dev = dev;
6947 list_add_tail(&node_detected_dev->list, &all_detected_devices);
6948 } else {
6949 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
6950 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
6955 static void autostart_arrays(int part)
6957 mdk_rdev_t *rdev;
6958 struct detected_devices_node *node_detected_dev;
6959 dev_t dev;
6960 int i_scanned, i_passed;
6962 i_scanned = 0;
6963 i_passed = 0;
6965 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
6967 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
6968 i_scanned++;
6969 node_detected_dev = list_entry(all_detected_devices.next,
6970 struct detected_devices_node, list);
6971 list_del(&node_detected_dev->list);
6972 dev = node_detected_dev->dev;
6973 kfree(node_detected_dev);
6974 rdev = md_import_device(dev,0, 90);
6975 if (IS_ERR(rdev))
6976 continue;
6978 if (test_bit(Faulty, &rdev->flags)) {
6979 MD_BUG();
6980 continue;
6982 set_bit(AutoDetected, &rdev->flags);
6983 list_add(&rdev->same_set, &pending_raid_disks);
6984 i_passed++;
6987 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
6988 i_scanned, i_passed);
6990 autorun_devices(part);
6993 #endif /* !MODULE */
6995 static __exit void md_exit(void)
6997 mddev_t *mddev;
6998 struct list_head *tmp;
7000 blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS);
7001 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
7003 unregister_blkdev(MD_MAJOR,"md");
7004 unregister_blkdev(mdp_major, "mdp");
7005 unregister_reboot_notifier(&md_notifier);
7006 unregister_sysctl_table(raid_table_header);
7007 remove_proc_entry("mdstat", NULL);
7008 for_each_mddev(mddev, tmp) {
7009 export_array(mddev);
7010 mddev->hold_active = 0;
7014 subsys_initcall(md_init);
7015 module_exit(md_exit)
7017 static int get_ro(char *buffer, struct kernel_param *kp)
7019 return sprintf(buffer, "%d", start_readonly);
7021 static int set_ro(const char *val, struct kernel_param *kp)
7023 char *e;
7024 int num = simple_strtoul(val, &e, 10);
7025 if (*val && (*e == '\0' || *e == '\n')) {
7026 start_readonly = num;
7027 return 0;
7029 return -EINVAL;
7032 module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
7033 module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
7035 module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
7037 EXPORT_SYMBOL(register_md_personality);
7038 EXPORT_SYMBOL(unregister_md_personality);
7039 EXPORT_SYMBOL(md_error);
7040 EXPORT_SYMBOL(md_done_sync);
7041 EXPORT_SYMBOL(md_write_start);
7042 EXPORT_SYMBOL(md_write_end);
7043 EXPORT_SYMBOL(md_register_thread);
7044 EXPORT_SYMBOL(md_unregister_thread);
7045 EXPORT_SYMBOL(md_wakeup_thread);
7046 EXPORT_SYMBOL(md_check_recovery);
7047 MODULE_LICENSE("GPL");
7048 MODULE_ALIAS("md");
7049 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);