thinkpad-acpi: don't fail to load the entire module due to ALSA problems
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / md / md.c
blob08f7471d015039dc125fac9c7f0bc79a0594eb02
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 retry:
309 spin_lock(&all_mddevs_lock);
311 if (unit) {
312 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
313 if (mddev->unit == unit) {
314 mddev_get(mddev);
315 spin_unlock(&all_mddevs_lock);
316 kfree(new);
317 return mddev;
320 if (new) {
321 list_add(&new->all_mddevs, &all_mddevs);
322 spin_unlock(&all_mddevs_lock);
323 new->hold_active = UNTIL_IOCTL;
324 return new;
326 } else if (new) {
327 /* find an unused unit number */
328 static int next_minor = 512;
329 int start = next_minor;
330 int is_free = 0;
331 int dev = 0;
332 while (!is_free) {
333 dev = MKDEV(MD_MAJOR, next_minor);
334 next_minor++;
335 if (next_minor > MINORMASK)
336 next_minor = 0;
337 if (next_minor == start) {
338 /* Oh dear, all in use. */
339 spin_unlock(&all_mddevs_lock);
340 kfree(new);
341 return NULL;
344 is_free = 1;
345 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
346 if (mddev->unit == dev) {
347 is_free = 0;
348 break;
351 new->unit = dev;
352 new->md_minor = MINOR(dev);
353 new->hold_active = UNTIL_STOP;
354 list_add(&new->all_mddevs, &all_mddevs);
355 spin_unlock(&all_mddevs_lock);
356 return new;
358 spin_unlock(&all_mddevs_lock);
360 new = kzalloc(sizeof(*new), GFP_KERNEL);
361 if (!new)
362 return NULL;
364 new->unit = unit;
365 if (MAJOR(unit) == MD_MAJOR)
366 new->md_minor = MINOR(unit);
367 else
368 new->md_minor = MINOR(unit) >> MdpMinorShift;
370 mutex_init(&new->open_mutex);
371 mutex_init(&new->reconfig_mutex);
372 mutex_init(&new->bitmap_mutex);
373 INIT_LIST_HEAD(&new->disks);
374 INIT_LIST_HEAD(&new->all_mddevs);
375 init_timer(&new->safemode_timer);
376 atomic_set(&new->active, 1);
377 atomic_set(&new->openers, 0);
378 atomic_set(&new->active_io, 0);
379 spin_lock_init(&new->write_lock);
380 init_waitqueue_head(&new->sb_wait);
381 init_waitqueue_head(&new->recovery_wait);
382 new->reshape_position = MaxSector;
383 new->resync_min = 0;
384 new->resync_max = MaxSector;
385 new->level = LEVEL_NONE;
387 goto retry;
390 static inline int mddev_lock(mddev_t * mddev)
392 return mutex_lock_interruptible(&mddev->reconfig_mutex);
395 static inline int mddev_is_locked(mddev_t *mddev)
397 return mutex_is_locked(&mddev->reconfig_mutex);
400 static inline int mddev_trylock(mddev_t * mddev)
402 return mutex_trylock(&mddev->reconfig_mutex);
405 static inline void mddev_unlock(mddev_t * mddev)
407 mutex_unlock(&mddev->reconfig_mutex);
409 md_wakeup_thread(mddev->thread);
412 static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
414 mdk_rdev_t *rdev;
416 list_for_each_entry(rdev, &mddev->disks, same_set)
417 if (rdev->desc_nr == nr)
418 return rdev;
420 return NULL;
423 static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
425 mdk_rdev_t *rdev;
427 list_for_each_entry(rdev, &mddev->disks, same_set)
428 if (rdev->bdev->bd_dev == dev)
429 return rdev;
431 return NULL;
434 static struct mdk_personality *find_pers(int level, char *clevel)
436 struct mdk_personality *pers;
437 list_for_each_entry(pers, &pers_list, list) {
438 if (level != LEVEL_NONE && pers->level == level)
439 return pers;
440 if (strcmp(pers->name, clevel)==0)
441 return pers;
443 return NULL;
446 /* return the offset of the super block in 512byte sectors */
447 static inline sector_t calc_dev_sboffset(struct block_device *bdev)
449 sector_t num_sectors = bdev->bd_inode->i_size / 512;
450 return MD_NEW_SIZE_SECTORS(num_sectors);
453 static int alloc_disk_sb(mdk_rdev_t * rdev)
455 if (rdev->sb_page)
456 MD_BUG();
458 rdev->sb_page = alloc_page(GFP_KERNEL);
459 if (!rdev->sb_page) {
460 printk(KERN_ALERT "md: out of memory.\n");
461 return -ENOMEM;
464 return 0;
467 static void free_disk_sb(mdk_rdev_t * rdev)
469 if (rdev->sb_page) {
470 put_page(rdev->sb_page);
471 rdev->sb_loaded = 0;
472 rdev->sb_page = NULL;
473 rdev->sb_start = 0;
474 rdev->sectors = 0;
479 static void super_written(struct bio *bio, int error)
481 mdk_rdev_t *rdev = bio->bi_private;
482 mddev_t *mddev = rdev->mddev;
484 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
485 printk("md: super_written gets error=%d, uptodate=%d\n",
486 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
487 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
488 md_error(mddev, rdev);
491 if (atomic_dec_and_test(&mddev->pending_writes))
492 wake_up(&mddev->sb_wait);
493 bio_put(bio);
496 static void super_written_barrier(struct bio *bio, int error)
498 struct bio *bio2 = bio->bi_private;
499 mdk_rdev_t *rdev = bio2->bi_private;
500 mddev_t *mddev = rdev->mddev;
502 if (!test_bit(BIO_UPTODATE, &bio->bi_flags) &&
503 error == -EOPNOTSUPP) {
504 unsigned long flags;
505 /* barriers don't appear to be supported :-( */
506 set_bit(BarriersNotsupp, &rdev->flags);
507 mddev->barriers_work = 0;
508 spin_lock_irqsave(&mddev->write_lock, flags);
509 bio2->bi_next = mddev->biolist;
510 mddev->biolist = bio2;
511 spin_unlock_irqrestore(&mddev->write_lock, flags);
512 wake_up(&mddev->sb_wait);
513 bio_put(bio);
514 } else {
515 bio_put(bio2);
516 bio->bi_private = rdev;
517 super_written(bio, error);
521 void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
522 sector_t sector, int size, struct page *page)
524 /* write first size bytes of page to sector of rdev
525 * Increment mddev->pending_writes before returning
526 * and decrement it on completion, waking up sb_wait
527 * if zero is reached.
528 * If an error occurred, call md_error
530 * As we might need to resubmit the request if BIO_RW_BARRIER
531 * causes ENOTSUPP, we allocate a spare bio...
533 struct bio *bio = bio_alloc(GFP_NOIO, 1);
534 int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNCIO) | (1<<BIO_RW_UNPLUG);
536 bio->bi_bdev = rdev->bdev;
537 bio->bi_sector = sector;
538 bio_add_page(bio, page, size, 0);
539 bio->bi_private = rdev;
540 bio->bi_end_io = super_written;
541 bio->bi_rw = rw;
543 atomic_inc(&mddev->pending_writes);
544 if (!test_bit(BarriersNotsupp, &rdev->flags)) {
545 struct bio *rbio;
546 rw |= (1<<BIO_RW_BARRIER);
547 rbio = bio_clone(bio, GFP_NOIO);
548 rbio->bi_private = bio;
549 rbio->bi_end_io = super_written_barrier;
550 submit_bio(rw, rbio);
551 } else
552 submit_bio(rw, bio);
555 void md_super_wait(mddev_t *mddev)
557 /* wait for all superblock writes that were scheduled to complete.
558 * if any had to be retried (due to BARRIER problems), retry them
560 DEFINE_WAIT(wq);
561 for(;;) {
562 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
563 if (atomic_read(&mddev->pending_writes)==0)
564 break;
565 while (mddev->biolist) {
566 struct bio *bio;
567 spin_lock_irq(&mddev->write_lock);
568 bio = mddev->biolist;
569 mddev->biolist = bio->bi_next ;
570 bio->bi_next = NULL;
571 spin_unlock_irq(&mddev->write_lock);
572 submit_bio(bio->bi_rw, bio);
574 schedule();
576 finish_wait(&mddev->sb_wait, &wq);
579 static void bi_complete(struct bio *bio, int error)
581 complete((struct completion*)bio->bi_private);
584 int sync_page_io(struct block_device *bdev, sector_t sector, int size,
585 struct page *page, int rw)
587 struct bio *bio = bio_alloc(GFP_NOIO, 1);
588 struct completion event;
589 int ret;
591 rw |= (1 << BIO_RW_SYNCIO) | (1 << BIO_RW_UNPLUG);
593 bio->bi_bdev = bdev;
594 bio->bi_sector = sector;
595 bio_add_page(bio, page, size, 0);
596 init_completion(&event);
597 bio->bi_private = &event;
598 bio->bi_end_io = bi_complete;
599 submit_bio(rw, bio);
600 wait_for_completion(&event);
602 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
603 bio_put(bio);
604 return ret;
606 EXPORT_SYMBOL_GPL(sync_page_io);
608 static int read_disk_sb(mdk_rdev_t * rdev, int size)
610 char b[BDEVNAME_SIZE];
611 if (!rdev->sb_page) {
612 MD_BUG();
613 return -EINVAL;
615 if (rdev->sb_loaded)
616 return 0;
619 if (!sync_page_io(rdev->bdev, rdev->sb_start, size, rdev->sb_page, READ))
620 goto fail;
621 rdev->sb_loaded = 1;
622 return 0;
624 fail:
625 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
626 bdevname(rdev->bdev,b));
627 return -EINVAL;
630 static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
632 return sb1->set_uuid0 == sb2->set_uuid0 &&
633 sb1->set_uuid1 == sb2->set_uuid1 &&
634 sb1->set_uuid2 == sb2->set_uuid2 &&
635 sb1->set_uuid3 == sb2->set_uuid3;
638 static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
640 int ret;
641 mdp_super_t *tmp1, *tmp2;
643 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
644 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
646 if (!tmp1 || !tmp2) {
647 ret = 0;
648 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
649 goto abort;
652 *tmp1 = *sb1;
653 *tmp2 = *sb2;
656 * nr_disks is not constant
658 tmp1->nr_disks = 0;
659 tmp2->nr_disks = 0;
661 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
662 abort:
663 kfree(tmp1);
664 kfree(tmp2);
665 return ret;
669 static u32 md_csum_fold(u32 csum)
671 csum = (csum & 0xffff) + (csum >> 16);
672 return (csum & 0xffff) + (csum >> 16);
675 static unsigned int calc_sb_csum(mdp_super_t * sb)
677 u64 newcsum = 0;
678 u32 *sb32 = (u32*)sb;
679 int i;
680 unsigned int disk_csum, csum;
682 disk_csum = sb->sb_csum;
683 sb->sb_csum = 0;
685 for (i = 0; i < MD_SB_BYTES/4 ; i++)
686 newcsum += sb32[i];
687 csum = (newcsum & 0xffffffff) + (newcsum>>32);
690 #ifdef CONFIG_ALPHA
691 /* This used to use csum_partial, which was wrong for several
692 * reasons including that different results are returned on
693 * different architectures. It isn't critical that we get exactly
694 * the same return value as before (we always csum_fold before
695 * testing, and that removes any differences). However as we
696 * know that csum_partial always returned a 16bit value on
697 * alphas, do a fold to maximise conformity to previous behaviour.
699 sb->sb_csum = md_csum_fold(disk_csum);
700 #else
701 sb->sb_csum = disk_csum;
702 #endif
703 return csum;
708 * Handle superblock details.
709 * We want to be able to handle multiple superblock formats
710 * so we have a common interface to them all, and an array of
711 * different handlers.
712 * We rely on user-space to write the initial superblock, and support
713 * reading and updating of superblocks.
714 * Interface methods are:
715 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
716 * loads and validates a superblock on dev.
717 * if refdev != NULL, compare superblocks on both devices
718 * Return:
719 * 0 - dev has a superblock that is compatible with refdev
720 * 1 - dev has a superblock that is compatible and newer than refdev
721 * so dev should be used as the refdev in future
722 * -EINVAL superblock incompatible or invalid
723 * -othererror e.g. -EIO
725 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
726 * Verify that dev is acceptable into mddev.
727 * The first time, mddev->raid_disks will be 0, and data from
728 * dev should be merged in. Subsequent calls check that dev
729 * is new enough. Return 0 or -EINVAL
731 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
732 * Update the superblock for rdev with data in mddev
733 * This does not write to disc.
737 struct super_type {
738 char *name;
739 struct module *owner;
740 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev,
741 int minor_version);
742 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
743 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
744 unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev,
745 sector_t num_sectors);
749 * Check that the given mddev has no bitmap.
751 * This function is called from the run method of all personalities that do not
752 * support bitmaps. It prints an error message and returns non-zero if mddev
753 * has a bitmap. Otherwise, it returns 0.
756 int md_check_no_bitmap(mddev_t *mddev)
758 if (!mddev->bitmap_file && !mddev->bitmap_offset)
759 return 0;
760 printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
761 mdname(mddev), mddev->pers->name);
762 return 1;
764 EXPORT_SYMBOL(md_check_no_bitmap);
767 * load_super for 0.90.0
769 static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
771 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
772 mdp_super_t *sb;
773 int ret;
776 * Calculate the position of the superblock (512byte sectors),
777 * it's at the end of the disk.
779 * It also happens to be a multiple of 4Kb.
781 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
783 ret = read_disk_sb(rdev, MD_SB_BYTES);
784 if (ret) return ret;
786 ret = -EINVAL;
788 bdevname(rdev->bdev, b);
789 sb = (mdp_super_t*)page_address(rdev->sb_page);
791 if (sb->md_magic != MD_SB_MAGIC) {
792 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
794 goto abort;
797 if (sb->major_version != 0 ||
798 sb->minor_version < 90 ||
799 sb->minor_version > 91) {
800 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
801 sb->major_version, sb->minor_version,
803 goto abort;
806 if (sb->raid_disks <= 0)
807 goto abort;
809 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
810 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
812 goto abort;
815 rdev->preferred_minor = sb->md_minor;
816 rdev->data_offset = 0;
817 rdev->sb_size = MD_SB_BYTES;
819 if (sb->level == LEVEL_MULTIPATH)
820 rdev->desc_nr = -1;
821 else
822 rdev->desc_nr = sb->this_disk.number;
824 if (!refdev) {
825 ret = 1;
826 } else {
827 __u64 ev1, ev2;
828 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
829 if (!uuid_equal(refsb, sb)) {
830 printk(KERN_WARNING "md: %s has different UUID to %s\n",
831 b, bdevname(refdev->bdev,b2));
832 goto abort;
834 if (!sb_equal(refsb, sb)) {
835 printk(KERN_WARNING "md: %s has same UUID"
836 " but different superblock to %s\n",
837 b, bdevname(refdev->bdev, b2));
838 goto abort;
840 ev1 = md_event(sb);
841 ev2 = md_event(refsb);
842 if (ev1 > ev2)
843 ret = 1;
844 else
845 ret = 0;
847 rdev->sectors = rdev->sb_start;
849 if (rdev->sectors < sb->size * 2 && sb->level > 1)
850 /* "this cannot possibly happen" ... */
851 ret = -EINVAL;
853 abort:
854 return ret;
858 * validate_super for 0.90.0
860 static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
862 mdp_disk_t *desc;
863 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
864 __u64 ev1 = md_event(sb);
866 rdev->raid_disk = -1;
867 clear_bit(Faulty, &rdev->flags);
868 clear_bit(In_sync, &rdev->flags);
869 clear_bit(WriteMostly, &rdev->flags);
870 clear_bit(BarriersNotsupp, &rdev->flags);
872 if (mddev->raid_disks == 0) {
873 mddev->major_version = 0;
874 mddev->minor_version = sb->minor_version;
875 mddev->patch_version = sb->patch_version;
876 mddev->external = 0;
877 mddev->chunk_sectors = sb->chunk_size >> 9;
878 mddev->ctime = sb->ctime;
879 mddev->utime = sb->utime;
880 mddev->level = sb->level;
881 mddev->clevel[0] = 0;
882 mddev->layout = sb->layout;
883 mddev->raid_disks = sb->raid_disks;
884 mddev->dev_sectors = sb->size * 2;
885 mddev->events = ev1;
886 mddev->bitmap_offset = 0;
887 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
889 if (mddev->minor_version >= 91) {
890 mddev->reshape_position = sb->reshape_position;
891 mddev->delta_disks = sb->delta_disks;
892 mddev->new_level = sb->new_level;
893 mddev->new_layout = sb->new_layout;
894 mddev->new_chunk_sectors = sb->new_chunk >> 9;
895 } else {
896 mddev->reshape_position = MaxSector;
897 mddev->delta_disks = 0;
898 mddev->new_level = mddev->level;
899 mddev->new_layout = mddev->layout;
900 mddev->new_chunk_sectors = mddev->chunk_sectors;
903 if (sb->state & (1<<MD_SB_CLEAN))
904 mddev->recovery_cp = MaxSector;
905 else {
906 if (sb->events_hi == sb->cp_events_hi &&
907 sb->events_lo == sb->cp_events_lo) {
908 mddev->recovery_cp = sb->recovery_cp;
909 } else
910 mddev->recovery_cp = 0;
913 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
914 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
915 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
916 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
918 mddev->max_disks = MD_SB_DISKS;
920 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
921 mddev->bitmap_file == NULL)
922 mddev->bitmap_offset = mddev->default_bitmap_offset;
924 } else if (mddev->pers == NULL) {
925 /* Insist on good event counter while assembling */
926 ++ev1;
927 if (ev1 < mddev->events)
928 return -EINVAL;
929 } else if (mddev->bitmap) {
930 /* if adding to array with a bitmap, then we can accept an
931 * older device ... but not too old.
933 if (ev1 < mddev->bitmap->events_cleared)
934 return 0;
935 } else {
936 if (ev1 < mddev->events)
937 /* just a hot-add of a new device, leave raid_disk at -1 */
938 return 0;
941 if (mddev->level != LEVEL_MULTIPATH) {
942 desc = sb->disks + rdev->desc_nr;
944 if (desc->state & (1<<MD_DISK_FAULTY))
945 set_bit(Faulty, &rdev->flags);
946 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
947 desc->raid_disk < mddev->raid_disks */) {
948 set_bit(In_sync, &rdev->flags);
949 rdev->raid_disk = desc->raid_disk;
950 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
951 /* active but not in sync implies recovery up to
952 * reshape position. We don't know exactly where
953 * that is, so set to zero for now */
954 if (mddev->minor_version >= 91) {
955 rdev->recovery_offset = 0;
956 rdev->raid_disk = desc->raid_disk;
959 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
960 set_bit(WriteMostly, &rdev->flags);
961 } else /* MULTIPATH are always insync */
962 set_bit(In_sync, &rdev->flags);
963 return 0;
967 * sync_super for 0.90.0
969 static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
971 mdp_super_t *sb;
972 mdk_rdev_t *rdev2;
973 int next_spare = mddev->raid_disks;
976 /* make rdev->sb match mddev data..
978 * 1/ zero out disks
979 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
980 * 3/ any empty disks < next_spare become removed
982 * disks[0] gets initialised to REMOVED because
983 * we cannot be sure from other fields if it has
984 * been initialised or not.
986 int i;
987 int active=0, working=0,failed=0,spare=0,nr_disks=0;
989 rdev->sb_size = MD_SB_BYTES;
991 sb = (mdp_super_t*)page_address(rdev->sb_page);
993 memset(sb, 0, sizeof(*sb));
995 sb->md_magic = MD_SB_MAGIC;
996 sb->major_version = mddev->major_version;
997 sb->patch_version = mddev->patch_version;
998 sb->gvalid_words = 0; /* ignored */
999 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1000 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1001 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1002 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1004 sb->ctime = mddev->ctime;
1005 sb->level = mddev->level;
1006 sb->size = mddev->dev_sectors / 2;
1007 sb->raid_disks = mddev->raid_disks;
1008 sb->md_minor = mddev->md_minor;
1009 sb->not_persistent = 0;
1010 sb->utime = mddev->utime;
1011 sb->state = 0;
1012 sb->events_hi = (mddev->events>>32);
1013 sb->events_lo = (u32)mddev->events;
1015 if (mddev->reshape_position == MaxSector)
1016 sb->minor_version = 90;
1017 else {
1018 sb->minor_version = 91;
1019 sb->reshape_position = mddev->reshape_position;
1020 sb->new_level = mddev->new_level;
1021 sb->delta_disks = mddev->delta_disks;
1022 sb->new_layout = mddev->new_layout;
1023 sb->new_chunk = mddev->new_chunk_sectors << 9;
1025 mddev->minor_version = sb->minor_version;
1026 if (mddev->in_sync)
1028 sb->recovery_cp = mddev->recovery_cp;
1029 sb->cp_events_hi = (mddev->events>>32);
1030 sb->cp_events_lo = (u32)mddev->events;
1031 if (mddev->recovery_cp == MaxSector)
1032 sb->state = (1<< MD_SB_CLEAN);
1033 } else
1034 sb->recovery_cp = 0;
1036 sb->layout = mddev->layout;
1037 sb->chunk_size = mddev->chunk_sectors << 9;
1039 if (mddev->bitmap && mddev->bitmap_file == NULL)
1040 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1042 sb->disks[0].state = (1<<MD_DISK_REMOVED);
1043 list_for_each_entry(rdev2, &mddev->disks, same_set) {
1044 mdp_disk_t *d;
1045 int desc_nr;
1046 int is_active = test_bit(In_sync, &rdev2->flags);
1048 if (rdev2->raid_disk >= 0 &&
1049 sb->minor_version >= 91)
1050 /* we have nowhere to store the recovery_offset,
1051 * but if it is not below the reshape_position,
1052 * we can piggy-back on that.
1054 is_active = 1;
1055 if (rdev2->raid_disk < 0 ||
1056 test_bit(Faulty, &rdev2->flags))
1057 is_active = 0;
1058 if (is_active)
1059 desc_nr = rdev2->raid_disk;
1060 else
1061 desc_nr = next_spare++;
1062 rdev2->desc_nr = desc_nr;
1063 d = &sb->disks[rdev2->desc_nr];
1064 nr_disks++;
1065 d->number = rdev2->desc_nr;
1066 d->major = MAJOR(rdev2->bdev->bd_dev);
1067 d->minor = MINOR(rdev2->bdev->bd_dev);
1068 if (is_active)
1069 d->raid_disk = rdev2->raid_disk;
1070 else
1071 d->raid_disk = rdev2->desc_nr; /* compatibility */
1072 if (test_bit(Faulty, &rdev2->flags))
1073 d->state = (1<<MD_DISK_FAULTY);
1074 else if (is_active) {
1075 d->state = (1<<MD_DISK_ACTIVE);
1076 if (test_bit(In_sync, &rdev2->flags))
1077 d->state |= (1<<MD_DISK_SYNC);
1078 active++;
1079 working++;
1080 } else {
1081 d->state = 0;
1082 spare++;
1083 working++;
1085 if (test_bit(WriteMostly, &rdev2->flags))
1086 d->state |= (1<<MD_DISK_WRITEMOSTLY);
1088 /* now set the "removed" and "faulty" bits on any missing devices */
1089 for (i=0 ; i < mddev->raid_disks ; i++) {
1090 mdp_disk_t *d = &sb->disks[i];
1091 if (d->state == 0 && d->number == 0) {
1092 d->number = i;
1093 d->raid_disk = i;
1094 d->state = (1<<MD_DISK_REMOVED);
1095 d->state |= (1<<MD_DISK_FAULTY);
1096 failed++;
1099 sb->nr_disks = nr_disks;
1100 sb->active_disks = active;
1101 sb->working_disks = working;
1102 sb->failed_disks = failed;
1103 sb->spare_disks = spare;
1105 sb->this_disk = sb->disks[rdev->desc_nr];
1106 sb->sb_csum = calc_sb_csum(sb);
1110 * rdev_size_change for 0.90.0
1112 static unsigned long long
1113 super_90_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
1115 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
1116 return 0; /* component must fit device */
1117 if (rdev->mddev->bitmap_offset)
1118 return 0; /* can't move bitmap */
1119 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
1120 if (!num_sectors || num_sectors > rdev->sb_start)
1121 num_sectors = rdev->sb_start;
1122 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
1123 rdev->sb_page);
1124 md_super_wait(rdev->mddev);
1125 return num_sectors / 2; /* kB for sysfs */
1130 * version 1 superblock
1133 static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
1135 __le32 disk_csum;
1136 u32 csum;
1137 unsigned long long newcsum;
1138 int size = 256 + le32_to_cpu(sb->max_dev)*2;
1139 __le32 *isuper = (__le32*)sb;
1140 int i;
1142 disk_csum = sb->sb_csum;
1143 sb->sb_csum = 0;
1144 newcsum = 0;
1145 for (i=0; size>=4; size -= 4 )
1146 newcsum += le32_to_cpu(*isuper++);
1148 if (size == 2)
1149 newcsum += le16_to_cpu(*(__le16*) isuper);
1151 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1152 sb->sb_csum = disk_csum;
1153 return cpu_to_le32(csum);
1156 static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
1158 struct mdp_superblock_1 *sb;
1159 int ret;
1160 sector_t sb_start;
1161 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1162 int bmask;
1165 * Calculate the position of the superblock in 512byte sectors.
1166 * It is always aligned to a 4K boundary and
1167 * depeding on minor_version, it can be:
1168 * 0: At least 8K, but less than 12K, from end of device
1169 * 1: At start of device
1170 * 2: 4K from start of device.
1172 switch(minor_version) {
1173 case 0:
1174 sb_start = rdev->bdev->bd_inode->i_size >> 9;
1175 sb_start -= 8*2;
1176 sb_start &= ~(sector_t)(4*2-1);
1177 break;
1178 case 1:
1179 sb_start = 0;
1180 break;
1181 case 2:
1182 sb_start = 8;
1183 break;
1184 default:
1185 return -EINVAL;
1187 rdev->sb_start = sb_start;
1189 /* superblock is rarely larger than 1K, but it can be larger,
1190 * and it is safe to read 4k, so we do that
1192 ret = read_disk_sb(rdev, 4096);
1193 if (ret) return ret;
1196 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1198 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1199 sb->major_version != cpu_to_le32(1) ||
1200 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
1201 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
1202 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
1203 return -EINVAL;
1205 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1206 printk("md: invalid superblock checksum on %s\n",
1207 bdevname(rdev->bdev,b));
1208 return -EINVAL;
1210 if (le64_to_cpu(sb->data_size) < 10) {
1211 printk("md: data_size too small on %s\n",
1212 bdevname(rdev->bdev,b));
1213 return -EINVAL;
1216 rdev->preferred_minor = 0xffff;
1217 rdev->data_offset = le64_to_cpu(sb->data_offset);
1218 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
1220 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
1221 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1222 if (rdev->sb_size & bmask)
1223 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1225 if (minor_version
1226 && rdev->data_offset < sb_start + (rdev->sb_size/512))
1227 return -EINVAL;
1229 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1230 rdev->desc_nr = -1;
1231 else
1232 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1234 if (!refdev) {
1235 ret = 1;
1236 } else {
1237 __u64 ev1, ev2;
1238 struct mdp_superblock_1 *refsb =
1239 (struct mdp_superblock_1*)page_address(refdev->sb_page);
1241 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1242 sb->level != refsb->level ||
1243 sb->layout != refsb->layout ||
1244 sb->chunksize != refsb->chunksize) {
1245 printk(KERN_WARNING "md: %s has strangely different"
1246 " superblock to %s\n",
1247 bdevname(rdev->bdev,b),
1248 bdevname(refdev->bdev,b2));
1249 return -EINVAL;
1251 ev1 = le64_to_cpu(sb->events);
1252 ev2 = le64_to_cpu(refsb->events);
1254 if (ev1 > ev2)
1255 ret = 1;
1256 else
1257 ret = 0;
1259 if (minor_version)
1260 rdev->sectors = (rdev->bdev->bd_inode->i_size >> 9) -
1261 le64_to_cpu(sb->data_offset);
1262 else
1263 rdev->sectors = rdev->sb_start;
1264 if (rdev->sectors < le64_to_cpu(sb->data_size))
1265 return -EINVAL;
1266 rdev->sectors = le64_to_cpu(sb->data_size);
1267 if (le64_to_cpu(sb->size) > rdev->sectors)
1268 return -EINVAL;
1269 return ret;
1272 static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1274 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1275 __u64 ev1 = le64_to_cpu(sb->events);
1277 rdev->raid_disk = -1;
1278 clear_bit(Faulty, &rdev->flags);
1279 clear_bit(In_sync, &rdev->flags);
1280 clear_bit(WriteMostly, &rdev->flags);
1281 clear_bit(BarriersNotsupp, &rdev->flags);
1283 if (mddev->raid_disks == 0) {
1284 mddev->major_version = 1;
1285 mddev->patch_version = 0;
1286 mddev->external = 0;
1287 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
1288 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1289 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1290 mddev->level = le32_to_cpu(sb->level);
1291 mddev->clevel[0] = 0;
1292 mddev->layout = le32_to_cpu(sb->layout);
1293 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
1294 mddev->dev_sectors = le64_to_cpu(sb->size);
1295 mddev->events = ev1;
1296 mddev->bitmap_offset = 0;
1297 mddev->default_bitmap_offset = 1024 >> 9;
1299 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1300 memcpy(mddev->uuid, sb->set_uuid, 16);
1302 mddev->max_disks = (4096-256)/2;
1304 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
1305 mddev->bitmap_file == NULL )
1306 mddev->bitmap_offset = (__s32)le32_to_cpu(sb->bitmap_offset);
1308 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1309 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1310 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1311 mddev->new_level = le32_to_cpu(sb->new_level);
1312 mddev->new_layout = le32_to_cpu(sb->new_layout);
1313 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
1314 } else {
1315 mddev->reshape_position = MaxSector;
1316 mddev->delta_disks = 0;
1317 mddev->new_level = mddev->level;
1318 mddev->new_layout = mddev->layout;
1319 mddev->new_chunk_sectors = mddev->chunk_sectors;
1322 } else if (mddev->pers == NULL) {
1323 /* Insist of good event counter while assembling */
1324 ++ev1;
1325 if (ev1 < mddev->events)
1326 return -EINVAL;
1327 } else if (mddev->bitmap) {
1328 /* If adding to array with a bitmap, then we can accept an
1329 * older device, but not too old.
1331 if (ev1 < mddev->bitmap->events_cleared)
1332 return 0;
1333 } else {
1334 if (ev1 < mddev->events)
1335 /* just a hot-add of a new device, leave raid_disk at -1 */
1336 return 0;
1338 if (mddev->level != LEVEL_MULTIPATH) {
1339 int role;
1340 if (rdev->desc_nr < 0 ||
1341 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1342 role = 0xffff;
1343 rdev->desc_nr = -1;
1344 } else
1345 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
1346 switch(role) {
1347 case 0xffff: /* spare */
1348 break;
1349 case 0xfffe: /* faulty */
1350 set_bit(Faulty, &rdev->flags);
1351 break;
1352 default:
1353 if ((le32_to_cpu(sb->feature_map) &
1354 MD_FEATURE_RECOVERY_OFFSET))
1355 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1356 else
1357 set_bit(In_sync, &rdev->flags);
1358 rdev->raid_disk = role;
1359 break;
1361 if (sb->devflags & WriteMostly1)
1362 set_bit(WriteMostly, &rdev->flags);
1363 } else /* MULTIPATH are always insync */
1364 set_bit(In_sync, &rdev->flags);
1366 return 0;
1369 static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1371 struct mdp_superblock_1 *sb;
1372 mdk_rdev_t *rdev2;
1373 int max_dev, i;
1374 /* make rdev->sb match mddev and rdev data. */
1376 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1378 sb->feature_map = 0;
1379 sb->pad0 = 0;
1380 sb->recovery_offset = cpu_to_le64(0);
1381 memset(sb->pad1, 0, sizeof(sb->pad1));
1382 memset(sb->pad2, 0, sizeof(sb->pad2));
1383 memset(sb->pad3, 0, sizeof(sb->pad3));
1385 sb->utime = cpu_to_le64((__u64)mddev->utime);
1386 sb->events = cpu_to_le64(mddev->events);
1387 if (mddev->in_sync)
1388 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1389 else
1390 sb->resync_offset = cpu_to_le64(0);
1392 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
1394 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
1395 sb->size = cpu_to_le64(mddev->dev_sectors);
1396 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
1397 sb->level = cpu_to_le32(mddev->level);
1398 sb->layout = cpu_to_le32(mddev->layout);
1400 if (mddev->bitmap && mddev->bitmap_file == NULL) {
1401 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_offset);
1402 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
1405 if (rdev->raid_disk >= 0 &&
1406 !test_bit(In_sync, &rdev->flags)) {
1407 if (rdev->recovery_offset > 0) {
1408 sb->feature_map |=
1409 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1410 sb->recovery_offset =
1411 cpu_to_le64(rdev->recovery_offset);
1415 if (mddev->reshape_position != MaxSector) {
1416 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1417 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1418 sb->new_layout = cpu_to_le32(mddev->new_layout);
1419 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1420 sb->new_level = cpu_to_le32(mddev->new_level);
1421 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
1424 max_dev = 0;
1425 list_for_each_entry(rdev2, &mddev->disks, same_set)
1426 if (rdev2->desc_nr+1 > max_dev)
1427 max_dev = rdev2->desc_nr+1;
1429 if (max_dev > le32_to_cpu(sb->max_dev)) {
1430 int bmask;
1431 sb->max_dev = cpu_to_le32(max_dev);
1432 rdev->sb_size = max_dev * 2 + 256;
1433 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1434 if (rdev->sb_size & bmask)
1435 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1437 for (i=0; i<max_dev;i++)
1438 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1440 list_for_each_entry(rdev2, &mddev->disks, same_set) {
1441 i = rdev2->desc_nr;
1442 if (test_bit(Faulty, &rdev2->flags))
1443 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1444 else if (test_bit(In_sync, &rdev2->flags))
1445 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
1446 else if (rdev2->raid_disk >= 0 && rdev2->recovery_offset > 0)
1447 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
1448 else
1449 sb->dev_roles[i] = cpu_to_le16(0xffff);
1452 sb->sb_csum = calc_sb_1_csum(sb);
1455 static unsigned long long
1456 super_1_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
1458 struct mdp_superblock_1 *sb;
1459 sector_t max_sectors;
1460 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
1461 return 0; /* component must fit device */
1462 if (rdev->sb_start < rdev->data_offset) {
1463 /* minor versions 1 and 2; superblock before data */
1464 max_sectors = rdev->bdev->bd_inode->i_size >> 9;
1465 max_sectors -= rdev->data_offset;
1466 if (!num_sectors || num_sectors > max_sectors)
1467 num_sectors = max_sectors;
1468 } else if (rdev->mddev->bitmap_offset) {
1469 /* minor version 0 with bitmap we can't move */
1470 return 0;
1471 } else {
1472 /* minor version 0; superblock after data */
1473 sector_t sb_start;
1474 sb_start = (rdev->bdev->bd_inode->i_size >> 9) - 8*2;
1475 sb_start &= ~(sector_t)(4*2 - 1);
1476 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
1477 if (!num_sectors || num_sectors > max_sectors)
1478 num_sectors = max_sectors;
1479 rdev->sb_start = sb_start;
1481 sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page);
1482 sb->data_size = cpu_to_le64(num_sectors);
1483 sb->super_offset = rdev->sb_start;
1484 sb->sb_csum = calc_sb_1_csum(sb);
1485 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
1486 rdev->sb_page);
1487 md_super_wait(rdev->mddev);
1488 return num_sectors / 2; /* kB for sysfs */
1491 static struct super_type super_types[] = {
1492 [0] = {
1493 .name = "0.90.0",
1494 .owner = THIS_MODULE,
1495 .load_super = super_90_load,
1496 .validate_super = super_90_validate,
1497 .sync_super = super_90_sync,
1498 .rdev_size_change = super_90_rdev_size_change,
1500 [1] = {
1501 .name = "md-1",
1502 .owner = THIS_MODULE,
1503 .load_super = super_1_load,
1504 .validate_super = super_1_validate,
1505 .sync_super = super_1_sync,
1506 .rdev_size_change = super_1_rdev_size_change,
1510 static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1512 mdk_rdev_t *rdev, *rdev2;
1514 rcu_read_lock();
1515 rdev_for_each_rcu(rdev, mddev1)
1516 rdev_for_each_rcu(rdev2, mddev2)
1517 if (rdev->bdev->bd_contains ==
1518 rdev2->bdev->bd_contains) {
1519 rcu_read_unlock();
1520 return 1;
1522 rcu_read_unlock();
1523 return 0;
1526 static LIST_HEAD(pending_raid_disks);
1529 * Try to register data integrity profile for an mddev
1531 * This is called when an array is started and after a disk has been kicked
1532 * from the array. It only succeeds if all working and active component devices
1533 * are integrity capable with matching profiles.
1535 int md_integrity_register(mddev_t *mddev)
1537 mdk_rdev_t *rdev, *reference = NULL;
1539 if (list_empty(&mddev->disks))
1540 return 0; /* nothing to do */
1541 if (blk_get_integrity(mddev->gendisk))
1542 return 0; /* already registered */
1543 list_for_each_entry(rdev, &mddev->disks, same_set) {
1544 /* skip spares and non-functional disks */
1545 if (test_bit(Faulty, &rdev->flags))
1546 continue;
1547 if (rdev->raid_disk < 0)
1548 continue;
1550 * If at least one rdev is not integrity capable, we can not
1551 * enable data integrity for the md device.
1553 if (!bdev_get_integrity(rdev->bdev))
1554 return -EINVAL;
1555 if (!reference) {
1556 /* Use the first rdev as the reference */
1557 reference = rdev;
1558 continue;
1560 /* does this rdev's profile match the reference profile? */
1561 if (blk_integrity_compare(reference->bdev->bd_disk,
1562 rdev->bdev->bd_disk) < 0)
1563 return -EINVAL;
1566 * All component devices are integrity capable and have matching
1567 * profiles, register the common profile for the md device.
1569 if (blk_integrity_register(mddev->gendisk,
1570 bdev_get_integrity(reference->bdev)) != 0) {
1571 printk(KERN_ERR "md: failed to register integrity for %s\n",
1572 mdname(mddev));
1573 return -EINVAL;
1575 printk(KERN_NOTICE "md: data integrity on %s enabled\n",
1576 mdname(mddev));
1577 return 0;
1579 EXPORT_SYMBOL(md_integrity_register);
1581 /* Disable data integrity if non-capable/non-matching disk is being added */
1582 void md_integrity_add_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
1584 struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev);
1585 struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk);
1587 if (!bi_mddev) /* nothing to do */
1588 return;
1589 if (rdev->raid_disk < 0) /* skip spares */
1590 return;
1591 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
1592 rdev->bdev->bd_disk) >= 0)
1593 return;
1594 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
1595 blk_integrity_unregister(mddev->gendisk);
1597 EXPORT_SYMBOL(md_integrity_add_rdev);
1599 static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1601 char b[BDEVNAME_SIZE];
1602 struct kobject *ko;
1603 char *s;
1604 int err;
1606 if (rdev->mddev) {
1607 MD_BUG();
1608 return -EINVAL;
1611 /* prevent duplicates */
1612 if (find_rdev(mddev, rdev->bdev->bd_dev))
1613 return -EEXIST;
1615 /* make sure rdev->sectors exceeds mddev->dev_sectors */
1616 if (rdev->sectors && (mddev->dev_sectors == 0 ||
1617 rdev->sectors < mddev->dev_sectors)) {
1618 if (mddev->pers) {
1619 /* Cannot change size, so fail
1620 * If mddev->level <= 0, then we don't care
1621 * about aligning sizes (e.g. linear)
1623 if (mddev->level > 0)
1624 return -ENOSPC;
1625 } else
1626 mddev->dev_sectors = rdev->sectors;
1629 /* Verify rdev->desc_nr is unique.
1630 * If it is -1, assign a free number, else
1631 * check number is not in use
1633 if (rdev->desc_nr < 0) {
1634 int choice = 0;
1635 if (mddev->pers) choice = mddev->raid_disks;
1636 while (find_rdev_nr(mddev, choice))
1637 choice++;
1638 rdev->desc_nr = choice;
1639 } else {
1640 if (find_rdev_nr(mddev, rdev->desc_nr))
1641 return -EBUSY;
1643 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
1644 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
1645 mdname(mddev), mddev->max_disks);
1646 return -EBUSY;
1648 bdevname(rdev->bdev,b);
1649 while ( (s=strchr(b, '/')) != NULL)
1650 *s = '!';
1652 rdev->mddev = mddev;
1653 printk(KERN_INFO "md: bind<%s>\n", b);
1655 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
1656 goto fail;
1658 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
1659 if ((err = sysfs_create_link(&rdev->kobj, ko, "block"))) {
1660 kobject_del(&rdev->kobj);
1661 goto fail;
1663 rdev->sysfs_state = sysfs_get_dirent(rdev->kobj.sd, "state");
1665 list_add_rcu(&rdev->same_set, &mddev->disks);
1666 bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk);
1668 /* May as well allow recovery to be retried once */
1669 mddev->recovery_disabled = 0;
1671 return 0;
1673 fail:
1674 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
1675 b, mdname(mddev));
1676 return err;
1679 static void md_delayed_delete(struct work_struct *ws)
1681 mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work);
1682 kobject_del(&rdev->kobj);
1683 kobject_put(&rdev->kobj);
1686 static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1688 char b[BDEVNAME_SIZE];
1689 if (!rdev->mddev) {
1690 MD_BUG();
1691 return;
1693 bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk);
1694 list_del_rcu(&rdev->same_set);
1695 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1696 rdev->mddev = NULL;
1697 sysfs_remove_link(&rdev->kobj, "block");
1698 sysfs_put(rdev->sysfs_state);
1699 rdev->sysfs_state = NULL;
1700 /* We need to delay this, otherwise we can deadlock when
1701 * writing to 'remove' to "dev/state". We also need
1702 * to delay it due to rcu usage.
1704 synchronize_rcu();
1705 INIT_WORK(&rdev->del_work, md_delayed_delete);
1706 kobject_get(&rdev->kobj);
1707 schedule_work(&rdev->del_work);
1711 * prevent the device from being mounted, repartitioned or
1712 * otherwise reused by a RAID array (or any other kernel
1713 * subsystem), by bd_claiming the device.
1715 static int lock_rdev(mdk_rdev_t *rdev, dev_t dev, int shared)
1717 int err = 0;
1718 struct block_device *bdev;
1719 char b[BDEVNAME_SIZE];
1721 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
1722 if (IS_ERR(bdev)) {
1723 printk(KERN_ERR "md: could not open %s.\n",
1724 __bdevname(dev, b));
1725 return PTR_ERR(bdev);
1727 err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev);
1728 if (err) {
1729 printk(KERN_ERR "md: could not bd_claim %s.\n",
1730 bdevname(bdev, b));
1731 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
1732 return err;
1734 if (!shared)
1735 set_bit(AllReserved, &rdev->flags);
1736 rdev->bdev = bdev;
1737 return err;
1740 static void unlock_rdev(mdk_rdev_t *rdev)
1742 struct block_device *bdev = rdev->bdev;
1743 rdev->bdev = NULL;
1744 if (!bdev)
1745 MD_BUG();
1746 bd_release(bdev);
1747 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
1750 void md_autodetect_dev(dev_t dev);
1752 static void export_rdev(mdk_rdev_t * rdev)
1754 char b[BDEVNAME_SIZE];
1755 printk(KERN_INFO "md: export_rdev(%s)\n",
1756 bdevname(rdev->bdev,b));
1757 if (rdev->mddev)
1758 MD_BUG();
1759 free_disk_sb(rdev);
1760 #ifndef MODULE
1761 if (test_bit(AutoDetected, &rdev->flags))
1762 md_autodetect_dev(rdev->bdev->bd_dev);
1763 #endif
1764 unlock_rdev(rdev);
1765 kobject_put(&rdev->kobj);
1768 static void kick_rdev_from_array(mdk_rdev_t * rdev)
1770 unbind_rdev_from_array(rdev);
1771 export_rdev(rdev);
1774 static void export_array(mddev_t *mddev)
1776 mdk_rdev_t *rdev, *tmp;
1778 rdev_for_each(rdev, tmp, mddev) {
1779 if (!rdev->mddev) {
1780 MD_BUG();
1781 continue;
1783 kick_rdev_from_array(rdev);
1785 if (!list_empty(&mddev->disks))
1786 MD_BUG();
1787 mddev->raid_disks = 0;
1788 mddev->major_version = 0;
1791 static void print_desc(mdp_disk_t *desc)
1793 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1794 desc->major,desc->minor,desc->raid_disk,desc->state);
1797 static void print_sb_90(mdp_super_t *sb)
1799 int i;
1801 printk(KERN_INFO
1802 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1803 sb->major_version, sb->minor_version, sb->patch_version,
1804 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1805 sb->ctime);
1806 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1807 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1808 sb->md_minor, sb->layout, sb->chunk_size);
1809 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1810 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1811 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1812 sb->failed_disks, sb->spare_disks,
1813 sb->sb_csum, (unsigned long)sb->events_lo);
1815 printk(KERN_INFO);
1816 for (i = 0; i < MD_SB_DISKS; i++) {
1817 mdp_disk_t *desc;
1819 desc = sb->disks + i;
1820 if (desc->number || desc->major || desc->minor ||
1821 desc->raid_disk || (desc->state && (desc->state != 4))) {
1822 printk(" D %2d: ", i);
1823 print_desc(desc);
1826 printk(KERN_INFO "md: THIS: ");
1827 print_desc(&sb->this_disk);
1830 static void print_sb_1(struct mdp_superblock_1 *sb)
1832 __u8 *uuid;
1834 uuid = sb->set_uuid;
1835 printk(KERN_INFO
1836 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%02x%02x%02x%02x"
1837 ":%02x%02x:%02x%02x:%02x%02x:%02x%02x%02x%02x%02x%02x>\n"
1838 "md: Name: \"%s\" CT:%llu\n",
1839 le32_to_cpu(sb->major_version),
1840 le32_to_cpu(sb->feature_map),
1841 uuid[0], uuid[1], uuid[2], uuid[3],
1842 uuid[4], uuid[5], uuid[6], uuid[7],
1843 uuid[8], uuid[9], uuid[10], uuid[11],
1844 uuid[12], uuid[13], uuid[14], uuid[15],
1845 sb->set_name,
1846 (unsigned long long)le64_to_cpu(sb->ctime)
1847 & MD_SUPERBLOCK_1_TIME_SEC_MASK);
1849 uuid = sb->device_uuid;
1850 printk(KERN_INFO
1851 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
1852 " RO:%llu\n"
1853 "md: Dev:%08x UUID: %02x%02x%02x%02x:%02x%02x:%02x%02x:%02x%02x"
1854 ":%02x%02x%02x%02x%02x%02x\n"
1855 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
1856 "md: (MaxDev:%u) \n",
1857 le32_to_cpu(sb->level),
1858 (unsigned long long)le64_to_cpu(sb->size),
1859 le32_to_cpu(sb->raid_disks),
1860 le32_to_cpu(sb->layout),
1861 le32_to_cpu(sb->chunksize),
1862 (unsigned long long)le64_to_cpu(sb->data_offset),
1863 (unsigned long long)le64_to_cpu(sb->data_size),
1864 (unsigned long long)le64_to_cpu(sb->super_offset),
1865 (unsigned long long)le64_to_cpu(sb->recovery_offset),
1866 le32_to_cpu(sb->dev_number),
1867 uuid[0], uuid[1], uuid[2], uuid[3],
1868 uuid[4], uuid[5], uuid[6], uuid[7],
1869 uuid[8], uuid[9], uuid[10], uuid[11],
1870 uuid[12], uuid[13], uuid[14], uuid[15],
1871 sb->devflags,
1872 (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK,
1873 (unsigned long long)le64_to_cpu(sb->events),
1874 (unsigned long long)le64_to_cpu(sb->resync_offset),
1875 le32_to_cpu(sb->sb_csum),
1876 le32_to_cpu(sb->max_dev)
1880 static void print_rdev(mdk_rdev_t *rdev, int major_version)
1882 char b[BDEVNAME_SIZE];
1883 printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
1884 bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors,
1885 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
1886 rdev->desc_nr);
1887 if (rdev->sb_loaded) {
1888 printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version);
1889 switch (major_version) {
1890 case 0:
1891 print_sb_90((mdp_super_t*)page_address(rdev->sb_page));
1892 break;
1893 case 1:
1894 print_sb_1((struct mdp_superblock_1 *)page_address(rdev->sb_page));
1895 break;
1897 } else
1898 printk(KERN_INFO "md: no rdev superblock!\n");
1901 static void md_print_devices(void)
1903 struct list_head *tmp;
1904 mdk_rdev_t *rdev;
1905 mddev_t *mddev;
1906 char b[BDEVNAME_SIZE];
1908 printk("\n");
1909 printk("md: **********************************\n");
1910 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
1911 printk("md: **********************************\n");
1912 for_each_mddev(mddev, tmp) {
1914 if (mddev->bitmap)
1915 bitmap_print_sb(mddev->bitmap);
1916 else
1917 printk("%s: ", mdname(mddev));
1918 list_for_each_entry(rdev, &mddev->disks, same_set)
1919 printk("<%s>", bdevname(rdev->bdev,b));
1920 printk("\n");
1922 list_for_each_entry(rdev, &mddev->disks, same_set)
1923 print_rdev(rdev, mddev->major_version);
1925 printk("md: **********************************\n");
1926 printk("\n");
1930 static void sync_sbs(mddev_t * mddev, int nospares)
1932 /* Update each superblock (in-memory image), but
1933 * if we are allowed to, skip spares which already
1934 * have the right event counter, or have one earlier
1935 * (which would mean they aren't being marked as dirty
1936 * with the rest of the array)
1938 mdk_rdev_t *rdev;
1940 /* First make sure individual recovery_offsets are correct */
1941 list_for_each_entry(rdev, &mddev->disks, same_set) {
1942 if (rdev->raid_disk >= 0 &&
1943 !test_bit(In_sync, &rdev->flags) &&
1944 mddev->curr_resync_completed > rdev->recovery_offset)
1945 rdev->recovery_offset = mddev->curr_resync_completed;
1948 list_for_each_entry(rdev, &mddev->disks, same_set) {
1949 if (rdev->sb_events == mddev->events ||
1950 (nospares &&
1951 rdev->raid_disk < 0 &&
1952 (rdev->sb_events&1)==0 &&
1953 rdev->sb_events+1 == mddev->events)) {
1954 /* Don't update this superblock */
1955 rdev->sb_loaded = 2;
1956 } else {
1957 super_types[mddev->major_version].
1958 sync_super(mddev, rdev);
1959 rdev->sb_loaded = 1;
1964 static void md_update_sb(mddev_t * mddev, int force_change)
1966 mdk_rdev_t *rdev;
1967 int sync_req;
1968 int nospares = 0;
1970 mddev->utime = get_seconds();
1971 if (mddev->external)
1972 return;
1973 repeat:
1974 spin_lock_irq(&mddev->write_lock);
1976 set_bit(MD_CHANGE_PENDING, &mddev->flags);
1977 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
1978 force_change = 1;
1979 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
1980 /* just a clean<-> dirty transition, possibly leave spares alone,
1981 * though if events isn't the right even/odd, we will have to do
1982 * spares after all
1984 nospares = 1;
1985 if (force_change)
1986 nospares = 0;
1987 if (mddev->degraded)
1988 /* If the array is degraded, then skipping spares is both
1989 * dangerous and fairly pointless.
1990 * Dangerous because a device that was removed from the array
1991 * might have a event_count that still looks up-to-date,
1992 * so it can be re-added without a resync.
1993 * Pointless because if there are any spares to skip,
1994 * then a recovery will happen and soon that array won't
1995 * be degraded any more and the spare can go back to sleep then.
1997 nospares = 0;
1999 sync_req = mddev->in_sync;
2001 /* If this is just a dirty<->clean transition, and the array is clean
2002 * and 'events' is odd, we can roll back to the previous clean state */
2003 if (nospares
2004 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
2005 && (mddev->events & 1)
2006 && mddev->events != 1)
2007 mddev->events--;
2008 else {
2009 /* otherwise we have to go forward and ... */
2010 mddev->events ++;
2011 if (!mddev->in_sync || mddev->recovery_cp != MaxSector) { /* not clean */
2012 /* .. if the array isn't clean, an 'even' event must also go
2013 * to spares. */
2014 if ((mddev->events&1)==0)
2015 nospares = 0;
2016 } else {
2017 /* otherwise an 'odd' event must go to spares */
2018 if ((mddev->events&1))
2019 nospares = 0;
2023 if (!mddev->events) {
2025 * oops, this 64-bit counter should never wrap.
2026 * Either we are in around ~1 trillion A.C., assuming
2027 * 1 reboot per second, or we have a bug:
2029 MD_BUG();
2030 mddev->events --;
2034 * do not write anything to disk if using
2035 * nonpersistent superblocks
2037 if (!mddev->persistent) {
2038 if (!mddev->external)
2039 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
2041 spin_unlock_irq(&mddev->write_lock);
2042 wake_up(&mddev->sb_wait);
2043 return;
2045 sync_sbs(mddev, nospares);
2046 spin_unlock_irq(&mddev->write_lock);
2048 dprintk(KERN_INFO
2049 "md: updating %s RAID superblock on device (in sync %d)\n",
2050 mdname(mddev),mddev->in_sync);
2052 bitmap_update_sb(mddev->bitmap);
2053 list_for_each_entry(rdev, &mddev->disks, same_set) {
2054 char b[BDEVNAME_SIZE];
2055 dprintk(KERN_INFO "md: ");
2056 if (rdev->sb_loaded != 1)
2057 continue; /* no noise on spare devices */
2058 if (test_bit(Faulty, &rdev->flags))
2059 dprintk("(skipping faulty ");
2061 dprintk("%s ", bdevname(rdev->bdev,b));
2062 if (!test_bit(Faulty, &rdev->flags)) {
2063 md_super_write(mddev,rdev,
2064 rdev->sb_start, rdev->sb_size,
2065 rdev->sb_page);
2066 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
2067 bdevname(rdev->bdev,b),
2068 (unsigned long long)rdev->sb_start);
2069 rdev->sb_events = mddev->events;
2071 } else
2072 dprintk(")\n");
2073 if (mddev->level == LEVEL_MULTIPATH)
2074 /* only need to write one superblock... */
2075 break;
2077 md_super_wait(mddev);
2078 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
2080 spin_lock_irq(&mddev->write_lock);
2081 if (mddev->in_sync != sync_req ||
2082 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
2083 /* have to write it out again */
2084 spin_unlock_irq(&mddev->write_lock);
2085 goto repeat;
2087 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
2088 spin_unlock_irq(&mddev->write_lock);
2089 wake_up(&mddev->sb_wait);
2090 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2091 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
2095 /* words written to sysfs files may, or may not, be \n terminated.
2096 * We want to accept with case. For this we use cmd_match.
2098 static int cmd_match(const char *cmd, const char *str)
2100 /* See if cmd, written into a sysfs file, matches
2101 * str. They must either be the same, or cmd can
2102 * have a trailing newline
2104 while (*cmd && *str && *cmd == *str) {
2105 cmd++;
2106 str++;
2108 if (*cmd == '\n')
2109 cmd++;
2110 if (*str || *cmd)
2111 return 0;
2112 return 1;
2115 struct rdev_sysfs_entry {
2116 struct attribute attr;
2117 ssize_t (*show)(mdk_rdev_t *, char *);
2118 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
2121 static ssize_t
2122 state_show(mdk_rdev_t *rdev, char *page)
2124 char *sep = "";
2125 size_t len = 0;
2127 if (test_bit(Faulty, &rdev->flags)) {
2128 len+= sprintf(page+len, "%sfaulty",sep);
2129 sep = ",";
2131 if (test_bit(In_sync, &rdev->flags)) {
2132 len += sprintf(page+len, "%sin_sync",sep);
2133 sep = ",";
2135 if (test_bit(WriteMostly, &rdev->flags)) {
2136 len += sprintf(page+len, "%swrite_mostly",sep);
2137 sep = ",";
2139 if (test_bit(Blocked, &rdev->flags)) {
2140 len += sprintf(page+len, "%sblocked", sep);
2141 sep = ",";
2143 if (!test_bit(Faulty, &rdev->flags) &&
2144 !test_bit(In_sync, &rdev->flags)) {
2145 len += sprintf(page+len, "%sspare", sep);
2146 sep = ",";
2148 return len+sprintf(page+len, "\n");
2151 static ssize_t
2152 state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2154 /* can write
2155 * faulty - simulates and error
2156 * remove - disconnects the device
2157 * writemostly - sets write_mostly
2158 * -writemostly - clears write_mostly
2159 * blocked - sets the Blocked flag
2160 * -blocked - clears the Blocked flag
2161 * insync - sets Insync providing device isn't active
2163 int err = -EINVAL;
2164 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2165 md_error(rdev->mddev, rdev);
2166 err = 0;
2167 } else if (cmd_match(buf, "remove")) {
2168 if (rdev->raid_disk >= 0)
2169 err = -EBUSY;
2170 else {
2171 mddev_t *mddev = rdev->mddev;
2172 kick_rdev_from_array(rdev);
2173 if (mddev->pers)
2174 md_update_sb(mddev, 1);
2175 md_new_event(mddev);
2176 err = 0;
2178 } else if (cmd_match(buf, "writemostly")) {
2179 set_bit(WriteMostly, &rdev->flags);
2180 err = 0;
2181 } else if (cmd_match(buf, "-writemostly")) {
2182 clear_bit(WriteMostly, &rdev->flags);
2183 err = 0;
2184 } else if (cmd_match(buf, "blocked")) {
2185 set_bit(Blocked, &rdev->flags);
2186 err = 0;
2187 } else if (cmd_match(buf, "-blocked")) {
2188 clear_bit(Blocked, &rdev->flags);
2189 wake_up(&rdev->blocked_wait);
2190 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2191 md_wakeup_thread(rdev->mddev->thread);
2193 err = 0;
2194 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2195 set_bit(In_sync, &rdev->flags);
2196 err = 0;
2198 if (!err && rdev->sysfs_state)
2199 sysfs_notify_dirent(rdev->sysfs_state);
2200 return err ? err : len;
2202 static struct rdev_sysfs_entry rdev_state =
2203 __ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
2205 static ssize_t
2206 errors_show(mdk_rdev_t *rdev, char *page)
2208 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2211 static ssize_t
2212 errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2214 char *e;
2215 unsigned long n = simple_strtoul(buf, &e, 10);
2216 if (*buf && (*e == 0 || *e == '\n')) {
2217 atomic_set(&rdev->corrected_errors, n);
2218 return len;
2220 return -EINVAL;
2222 static struct rdev_sysfs_entry rdev_errors =
2223 __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
2225 static ssize_t
2226 slot_show(mdk_rdev_t *rdev, char *page)
2228 if (rdev->raid_disk < 0)
2229 return sprintf(page, "none\n");
2230 else
2231 return sprintf(page, "%d\n", rdev->raid_disk);
2234 static ssize_t
2235 slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2237 char *e;
2238 int err;
2239 char nm[20];
2240 int slot = simple_strtoul(buf, &e, 10);
2241 if (strncmp(buf, "none", 4)==0)
2242 slot = -1;
2243 else if (e==buf || (*e && *e!= '\n'))
2244 return -EINVAL;
2245 if (rdev->mddev->pers && slot == -1) {
2246 /* Setting 'slot' on an active array requires also
2247 * updating the 'rd%d' link, and communicating
2248 * with the personality with ->hot_*_disk.
2249 * For now we only support removing
2250 * failed/spare devices. This normally happens automatically,
2251 * but not when the metadata is externally managed.
2253 if (rdev->raid_disk == -1)
2254 return -EEXIST;
2255 /* personality does all needed checks */
2256 if (rdev->mddev->pers->hot_add_disk == NULL)
2257 return -EINVAL;
2258 err = rdev->mddev->pers->
2259 hot_remove_disk(rdev->mddev, rdev->raid_disk);
2260 if (err)
2261 return err;
2262 sprintf(nm, "rd%d", rdev->raid_disk);
2263 sysfs_remove_link(&rdev->mddev->kobj, nm);
2264 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2265 md_wakeup_thread(rdev->mddev->thread);
2266 } else if (rdev->mddev->pers) {
2267 mdk_rdev_t *rdev2;
2268 /* Activating a spare .. or possibly reactivating
2269 * if we ever get bitmaps working here.
2272 if (rdev->raid_disk != -1)
2273 return -EBUSY;
2275 if (rdev->mddev->pers->hot_add_disk == NULL)
2276 return -EINVAL;
2278 list_for_each_entry(rdev2, &rdev->mddev->disks, same_set)
2279 if (rdev2->raid_disk == slot)
2280 return -EEXIST;
2282 rdev->raid_disk = slot;
2283 if (test_bit(In_sync, &rdev->flags))
2284 rdev->saved_raid_disk = slot;
2285 else
2286 rdev->saved_raid_disk = -1;
2287 err = rdev->mddev->pers->
2288 hot_add_disk(rdev->mddev, rdev);
2289 if (err) {
2290 rdev->raid_disk = -1;
2291 return err;
2292 } else
2293 sysfs_notify_dirent(rdev->sysfs_state);
2294 sprintf(nm, "rd%d", rdev->raid_disk);
2295 if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm))
2296 printk(KERN_WARNING
2297 "md: cannot register "
2298 "%s for %s\n",
2299 nm, mdname(rdev->mddev));
2301 /* don't wakeup anyone, leave that to userspace. */
2302 } else {
2303 if (slot >= rdev->mddev->raid_disks)
2304 return -ENOSPC;
2305 rdev->raid_disk = slot;
2306 /* assume it is working */
2307 clear_bit(Faulty, &rdev->flags);
2308 clear_bit(WriteMostly, &rdev->flags);
2309 set_bit(In_sync, &rdev->flags);
2310 sysfs_notify_dirent(rdev->sysfs_state);
2312 return len;
2316 static struct rdev_sysfs_entry rdev_slot =
2317 __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
2319 static ssize_t
2320 offset_show(mdk_rdev_t *rdev, char *page)
2322 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
2325 static ssize_t
2326 offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2328 char *e;
2329 unsigned long long offset = simple_strtoull(buf, &e, 10);
2330 if (e==buf || (*e && *e != '\n'))
2331 return -EINVAL;
2332 if (rdev->mddev->pers && rdev->raid_disk >= 0)
2333 return -EBUSY;
2334 if (rdev->sectors && rdev->mddev->external)
2335 /* Must set offset before size, so overlap checks
2336 * can be sane */
2337 return -EBUSY;
2338 rdev->data_offset = offset;
2339 return len;
2342 static struct rdev_sysfs_entry rdev_offset =
2343 __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
2345 static ssize_t
2346 rdev_size_show(mdk_rdev_t *rdev, char *page)
2348 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
2351 static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2353 /* check if two start/length pairs overlap */
2354 if (s1+l1 <= s2)
2355 return 0;
2356 if (s2+l2 <= s1)
2357 return 0;
2358 return 1;
2361 static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2363 unsigned long long blocks;
2364 sector_t new;
2366 if (strict_strtoull(buf, 10, &blocks) < 0)
2367 return -EINVAL;
2369 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2370 return -EINVAL; /* sector conversion overflow */
2372 new = blocks * 2;
2373 if (new != blocks * 2)
2374 return -EINVAL; /* unsigned long long to sector_t overflow */
2376 *sectors = new;
2377 return 0;
2380 static ssize_t
2381 rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2383 mddev_t *my_mddev = rdev->mddev;
2384 sector_t oldsectors = rdev->sectors;
2385 sector_t sectors;
2387 if (strict_blocks_to_sectors(buf, &sectors) < 0)
2388 return -EINVAL;
2389 if (my_mddev->pers && rdev->raid_disk >= 0) {
2390 if (my_mddev->persistent) {
2391 sectors = super_types[my_mddev->major_version].
2392 rdev_size_change(rdev, sectors);
2393 if (!sectors)
2394 return -EBUSY;
2395 } else if (!sectors)
2396 sectors = (rdev->bdev->bd_inode->i_size >> 9) -
2397 rdev->data_offset;
2399 if (sectors < my_mddev->dev_sectors)
2400 return -EINVAL; /* component must fit device */
2402 rdev->sectors = sectors;
2403 if (sectors > oldsectors && my_mddev->external) {
2404 /* need to check that all other rdevs with the same ->bdev
2405 * do not overlap. We need to unlock the mddev to avoid
2406 * a deadlock. We have already changed rdev->sectors, and if
2407 * we have to change it back, we will have the lock again.
2409 mddev_t *mddev;
2410 int overlap = 0;
2411 struct list_head *tmp;
2413 mddev_unlock(my_mddev);
2414 for_each_mddev(mddev, tmp) {
2415 mdk_rdev_t *rdev2;
2417 mddev_lock(mddev);
2418 list_for_each_entry(rdev2, &mddev->disks, same_set)
2419 if (test_bit(AllReserved, &rdev2->flags) ||
2420 (rdev->bdev == rdev2->bdev &&
2421 rdev != rdev2 &&
2422 overlaps(rdev->data_offset, rdev->sectors,
2423 rdev2->data_offset,
2424 rdev2->sectors))) {
2425 overlap = 1;
2426 break;
2428 mddev_unlock(mddev);
2429 if (overlap) {
2430 mddev_put(mddev);
2431 break;
2434 mddev_lock(my_mddev);
2435 if (overlap) {
2436 /* Someone else could have slipped in a size
2437 * change here, but doing so is just silly.
2438 * We put oldsectors back because we *know* it is
2439 * safe, and trust userspace not to race with
2440 * itself
2442 rdev->sectors = oldsectors;
2443 return -EBUSY;
2446 return len;
2449 static struct rdev_sysfs_entry rdev_size =
2450 __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
2452 static struct attribute *rdev_default_attrs[] = {
2453 &rdev_state.attr,
2454 &rdev_errors.attr,
2455 &rdev_slot.attr,
2456 &rdev_offset.attr,
2457 &rdev_size.attr,
2458 NULL,
2460 static ssize_t
2461 rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2463 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2464 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
2465 mddev_t *mddev = rdev->mddev;
2466 ssize_t rv;
2468 if (!entry->show)
2469 return -EIO;
2471 rv = mddev ? mddev_lock(mddev) : -EBUSY;
2472 if (!rv) {
2473 if (rdev->mddev == NULL)
2474 rv = -EBUSY;
2475 else
2476 rv = entry->show(rdev, page);
2477 mddev_unlock(mddev);
2479 return rv;
2482 static ssize_t
2483 rdev_attr_store(struct kobject *kobj, struct attribute *attr,
2484 const char *page, size_t length)
2486 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2487 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
2488 ssize_t rv;
2489 mddev_t *mddev = rdev->mddev;
2491 if (!entry->store)
2492 return -EIO;
2493 if (!capable(CAP_SYS_ADMIN))
2494 return -EACCES;
2495 rv = mddev ? mddev_lock(mddev): -EBUSY;
2496 if (!rv) {
2497 if (rdev->mddev == NULL)
2498 rv = -EBUSY;
2499 else
2500 rv = entry->store(rdev, page, length);
2501 mddev_unlock(mddev);
2503 return rv;
2506 static void rdev_free(struct kobject *ko)
2508 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
2509 kfree(rdev);
2511 static struct sysfs_ops rdev_sysfs_ops = {
2512 .show = rdev_attr_show,
2513 .store = rdev_attr_store,
2515 static struct kobj_type rdev_ktype = {
2516 .release = rdev_free,
2517 .sysfs_ops = &rdev_sysfs_ops,
2518 .default_attrs = rdev_default_attrs,
2522 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2524 * mark the device faulty if:
2526 * - the device is nonexistent (zero size)
2527 * - the device has no valid superblock
2529 * a faulty rdev _never_ has rdev->sb set.
2531 static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
2533 char b[BDEVNAME_SIZE];
2534 int err;
2535 mdk_rdev_t *rdev;
2536 sector_t size;
2538 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
2539 if (!rdev) {
2540 printk(KERN_ERR "md: could not alloc mem for new device!\n");
2541 return ERR_PTR(-ENOMEM);
2544 if ((err = alloc_disk_sb(rdev)))
2545 goto abort_free;
2547 err = lock_rdev(rdev, newdev, super_format == -2);
2548 if (err)
2549 goto abort_free;
2551 kobject_init(&rdev->kobj, &rdev_ktype);
2553 rdev->desc_nr = -1;
2554 rdev->saved_raid_disk = -1;
2555 rdev->raid_disk = -1;
2556 rdev->flags = 0;
2557 rdev->data_offset = 0;
2558 rdev->sb_events = 0;
2559 atomic_set(&rdev->nr_pending, 0);
2560 atomic_set(&rdev->read_errors, 0);
2561 atomic_set(&rdev->corrected_errors, 0);
2563 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2564 if (!size) {
2565 printk(KERN_WARNING
2566 "md: %s has zero or unknown size, marking faulty!\n",
2567 bdevname(rdev->bdev,b));
2568 err = -EINVAL;
2569 goto abort_free;
2572 if (super_format >= 0) {
2573 err = super_types[super_format].
2574 load_super(rdev, NULL, super_minor);
2575 if (err == -EINVAL) {
2576 printk(KERN_WARNING
2577 "md: %s does not have a valid v%d.%d "
2578 "superblock, not importing!\n",
2579 bdevname(rdev->bdev,b),
2580 super_format, super_minor);
2581 goto abort_free;
2583 if (err < 0) {
2584 printk(KERN_WARNING
2585 "md: could not read %s's sb, not importing!\n",
2586 bdevname(rdev->bdev,b));
2587 goto abort_free;
2591 INIT_LIST_HEAD(&rdev->same_set);
2592 init_waitqueue_head(&rdev->blocked_wait);
2594 return rdev;
2596 abort_free:
2597 if (rdev->sb_page) {
2598 if (rdev->bdev)
2599 unlock_rdev(rdev);
2600 free_disk_sb(rdev);
2602 kfree(rdev);
2603 return ERR_PTR(err);
2607 * Check a full RAID array for plausibility
2611 static void analyze_sbs(mddev_t * mddev)
2613 int i;
2614 mdk_rdev_t *rdev, *freshest, *tmp;
2615 char b[BDEVNAME_SIZE];
2617 freshest = NULL;
2618 rdev_for_each(rdev, tmp, mddev)
2619 switch (super_types[mddev->major_version].
2620 load_super(rdev, freshest, mddev->minor_version)) {
2621 case 1:
2622 freshest = rdev;
2623 break;
2624 case 0:
2625 break;
2626 default:
2627 printk( KERN_ERR \
2628 "md: fatal superblock inconsistency in %s"
2629 " -- removing from array\n",
2630 bdevname(rdev->bdev,b));
2631 kick_rdev_from_array(rdev);
2635 super_types[mddev->major_version].
2636 validate_super(mddev, freshest);
2638 i = 0;
2639 rdev_for_each(rdev, tmp, mddev) {
2640 if (rdev->desc_nr >= mddev->max_disks ||
2641 i > mddev->max_disks) {
2642 printk(KERN_WARNING
2643 "md: %s: %s: only %d devices permitted\n",
2644 mdname(mddev), bdevname(rdev->bdev, b),
2645 mddev->max_disks);
2646 kick_rdev_from_array(rdev);
2647 continue;
2649 if (rdev != freshest)
2650 if (super_types[mddev->major_version].
2651 validate_super(mddev, rdev)) {
2652 printk(KERN_WARNING "md: kicking non-fresh %s"
2653 " from array!\n",
2654 bdevname(rdev->bdev,b));
2655 kick_rdev_from_array(rdev);
2656 continue;
2658 if (mddev->level == LEVEL_MULTIPATH) {
2659 rdev->desc_nr = i++;
2660 rdev->raid_disk = rdev->desc_nr;
2661 set_bit(In_sync, &rdev->flags);
2662 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
2663 rdev->raid_disk = -1;
2664 clear_bit(In_sync, &rdev->flags);
2669 static void md_safemode_timeout(unsigned long data);
2671 static ssize_t
2672 safe_delay_show(mddev_t *mddev, char *page)
2674 int msec = (mddev->safemode_delay*1000)/HZ;
2675 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
2677 static ssize_t
2678 safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
2680 int scale=1;
2681 int dot=0;
2682 int i;
2683 unsigned long msec;
2684 char buf[30];
2686 /* remove a period, and count digits after it */
2687 if (len >= sizeof(buf))
2688 return -EINVAL;
2689 strlcpy(buf, cbuf, sizeof(buf));
2690 for (i=0; i<len; i++) {
2691 if (dot) {
2692 if (isdigit(buf[i])) {
2693 buf[i-1] = buf[i];
2694 scale *= 10;
2696 buf[i] = 0;
2697 } else if (buf[i] == '.') {
2698 dot=1;
2699 buf[i] = 0;
2702 if (strict_strtoul(buf, 10, &msec) < 0)
2703 return -EINVAL;
2704 msec = (msec * 1000) / scale;
2705 if (msec == 0)
2706 mddev->safemode_delay = 0;
2707 else {
2708 unsigned long old_delay = mddev->safemode_delay;
2709 mddev->safemode_delay = (msec*HZ)/1000;
2710 if (mddev->safemode_delay == 0)
2711 mddev->safemode_delay = 1;
2712 if (mddev->safemode_delay < old_delay)
2713 md_safemode_timeout((unsigned long)mddev);
2715 return len;
2717 static struct md_sysfs_entry md_safe_delay =
2718 __ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
2720 static ssize_t
2721 level_show(mddev_t *mddev, char *page)
2723 struct mdk_personality *p = mddev->pers;
2724 if (p)
2725 return sprintf(page, "%s\n", p->name);
2726 else if (mddev->clevel[0])
2727 return sprintf(page, "%s\n", mddev->clevel);
2728 else if (mddev->level != LEVEL_NONE)
2729 return sprintf(page, "%d\n", mddev->level);
2730 else
2731 return 0;
2734 static ssize_t
2735 level_store(mddev_t *mddev, const char *buf, size_t len)
2737 char level[16];
2738 ssize_t rv = len;
2739 struct mdk_personality *pers;
2740 void *priv;
2741 mdk_rdev_t *rdev;
2743 if (mddev->pers == NULL) {
2744 if (len == 0)
2745 return 0;
2746 if (len >= sizeof(mddev->clevel))
2747 return -ENOSPC;
2748 strncpy(mddev->clevel, buf, len);
2749 if (mddev->clevel[len-1] == '\n')
2750 len--;
2751 mddev->clevel[len] = 0;
2752 mddev->level = LEVEL_NONE;
2753 return rv;
2756 /* request to change the personality. Need to ensure:
2757 * - array is not engaged in resync/recovery/reshape
2758 * - old personality can be suspended
2759 * - new personality will access other array.
2762 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
2763 return -EBUSY;
2765 if (!mddev->pers->quiesce) {
2766 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
2767 mdname(mddev), mddev->pers->name);
2768 return -EINVAL;
2771 /* Now find the new personality */
2772 if (len == 0 || len >= sizeof(level))
2773 return -EINVAL;
2774 strncpy(level, buf, len);
2775 if (level[len-1] == '\n')
2776 len--;
2777 level[len] = 0;
2779 request_module("md-%s", level);
2780 spin_lock(&pers_lock);
2781 pers = find_pers(LEVEL_NONE, level);
2782 if (!pers || !try_module_get(pers->owner)) {
2783 spin_unlock(&pers_lock);
2784 printk(KERN_WARNING "md: personality %s not loaded\n", level);
2785 return -EINVAL;
2787 spin_unlock(&pers_lock);
2789 if (pers == mddev->pers) {
2790 /* Nothing to do! */
2791 module_put(pers->owner);
2792 return rv;
2794 if (!pers->takeover) {
2795 module_put(pers->owner);
2796 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
2797 mdname(mddev), level);
2798 return -EINVAL;
2801 /* ->takeover must set new_* and/or delta_disks
2802 * if it succeeds, and may set them when it fails.
2804 priv = pers->takeover(mddev);
2805 if (IS_ERR(priv)) {
2806 mddev->new_level = mddev->level;
2807 mddev->new_layout = mddev->layout;
2808 mddev->new_chunk_sectors = mddev->chunk_sectors;
2809 mddev->raid_disks -= mddev->delta_disks;
2810 mddev->delta_disks = 0;
2811 module_put(pers->owner);
2812 printk(KERN_WARNING "md: %s: %s would not accept array\n",
2813 mdname(mddev), level);
2814 return PTR_ERR(priv);
2817 /* Looks like we have a winner */
2818 mddev_suspend(mddev);
2819 mddev->pers->stop(mddev);
2820 module_put(mddev->pers->owner);
2821 /* Invalidate devices that are now superfluous */
2822 list_for_each_entry(rdev, &mddev->disks, same_set)
2823 if (rdev->raid_disk >= mddev->raid_disks) {
2824 rdev->raid_disk = -1;
2825 clear_bit(In_sync, &rdev->flags);
2827 mddev->pers = pers;
2828 mddev->private = priv;
2829 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
2830 mddev->level = mddev->new_level;
2831 mddev->layout = mddev->new_layout;
2832 mddev->chunk_sectors = mddev->new_chunk_sectors;
2833 mddev->delta_disks = 0;
2834 pers->run(mddev);
2835 mddev_resume(mddev);
2836 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2837 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2838 md_wakeup_thread(mddev->thread);
2839 return rv;
2842 static struct md_sysfs_entry md_level =
2843 __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
2846 static ssize_t
2847 layout_show(mddev_t *mddev, char *page)
2849 /* just a number, not meaningful for all levels */
2850 if (mddev->reshape_position != MaxSector &&
2851 mddev->layout != mddev->new_layout)
2852 return sprintf(page, "%d (%d)\n",
2853 mddev->new_layout, mddev->layout);
2854 return sprintf(page, "%d\n", mddev->layout);
2857 static ssize_t
2858 layout_store(mddev_t *mddev, const char *buf, size_t len)
2860 char *e;
2861 unsigned long n = simple_strtoul(buf, &e, 10);
2863 if (!*buf || (*e && *e != '\n'))
2864 return -EINVAL;
2866 if (mddev->pers) {
2867 int err;
2868 if (mddev->pers->check_reshape == NULL)
2869 return -EBUSY;
2870 mddev->new_layout = n;
2871 err = mddev->pers->check_reshape(mddev);
2872 if (err) {
2873 mddev->new_layout = mddev->layout;
2874 return err;
2876 } else {
2877 mddev->new_layout = n;
2878 if (mddev->reshape_position == MaxSector)
2879 mddev->layout = n;
2881 return len;
2883 static struct md_sysfs_entry md_layout =
2884 __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
2887 static ssize_t
2888 raid_disks_show(mddev_t *mddev, char *page)
2890 if (mddev->raid_disks == 0)
2891 return 0;
2892 if (mddev->reshape_position != MaxSector &&
2893 mddev->delta_disks != 0)
2894 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
2895 mddev->raid_disks - mddev->delta_disks);
2896 return sprintf(page, "%d\n", mddev->raid_disks);
2899 static int update_raid_disks(mddev_t *mddev, int raid_disks);
2901 static ssize_t
2902 raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
2904 char *e;
2905 int rv = 0;
2906 unsigned long n = simple_strtoul(buf, &e, 10);
2908 if (!*buf || (*e && *e != '\n'))
2909 return -EINVAL;
2911 if (mddev->pers)
2912 rv = update_raid_disks(mddev, n);
2913 else if (mddev->reshape_position != MaxSector) {
2914 int olddisks = mddev->raid_disks - mddev->delta_disks;
2915 mddev->delta_disks = n - olddisks;
2916 mddev->raid_disks = n;
2917 } else
2918 mddev->raid_disks = n;
2919 return rv ? rv : len;
2921 static struct md_sysfs_entry md_raid_disks =
2922 __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
2924 static ssize_t
2925 chunk_size_show(mddev_t *mddev, char *page)
2927 if (mddev->reshape_position != MaxSector &&
2928 mddev->chunk_sectors != mddev->new_chunk_sectors)
2929 return sprintf(page, "%d (%d)\n",
2930 mddev->new_chunk_sectors << 9,
2931 mddev->chunk_sectors << 9);
2932 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
2935 static ssize_t
2936 chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
2938 char *e;
2939 unsigned long n = simple_strtoul(buf, &e, 10);
2941 if (!*buf || (*e && *e != '\n'))
2942 return -EINVAL;
2944 if (mddev->pers) {
2945 int err;
2946 if (mddev->pers->check_reshape == NULL)
2947 return -EBUSY;
2948 mddev->new_chunk_sectors = n >> 9;
2949 err = mddev->pers->check_reshape(mddev);
2950 if (err) {
2951 mddev->new_chunk_sectors = mddev->chunk_sectors;
2952 return err;
2954 } else {
2955 mddev->new_chunk_sectors = n >> 9;
2956 if (mddev->reshape_position == MaxSector)
2957 mddev->chunk_sectors = n >> 9;
2959 return len;
2961 static struct md_sysfs_entry md_chunk_size =
2962 __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
2964 static ssize_t
2965 resync_start_show(mddev_t *mddev, char *page)
2967 if (mddev->recovery_cp == MaxSector)
2968 return sprintf(page, "none\n");
2969 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
2972 static ssize_t
2973 resync_start_store(mddev_t *mddev, const char *buf, size_t len)
2975 char *e;
2976 unsigned long long n = simple_strtoull(buf, &e, 10);
2978 if (mddev->pers)
2979 return -EBUSY;
2980 if (!*buf || (*e && *e != '\n'))
2981 return -EINVAL;
2983 mddev->recovery_cp = n;
2984 return len;
2986 static struct md_sysfs_entry md_resync_start =
2987 __ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
2990 * The array state can be:
2992 * clear
2993 * No devices, no size, no level
2994 * Equivalent to STOP_ARRAY ioctl
2995 * inactive
2996 * May have some settings, but array is not active
2997 * all IO results in error
2998 * When written, doesn't tear down array, but just stops it
2999 * suspended (not supported yet)
3000 * All IO requests will block. The array can be reconfigured.
3001 * Writing this, if accepted, will block until array is quiescent
3002 * readonly
3003 * no resync can happen. no superblocks get written.
3004 * write requests fail
3005 * read-auto
3006 * like readonly, but behaves like 'clean' on a write request.
3008 * clean - no pending writes, but otherwise active.
3009 * When written to inactive array, starts without resync
3010 * If a write request arrives then
3011 * if metadata is known, mark 'dirty' and switch to 'active'.
3012 * if not known, block and switch to write-pending
3013 * If written to an active array that has pending writes, then fails.
3014 * active
3015 * fully active: IO and resync can be happening.
3016 * When written to inactive array, starts with resync
3018 * write-pending
3019 * clean, but writes are blocked waiting for 'active' to be written.
3021 * active-idle
3022 * like active, but no writes have been seen for a while (100msec).
3025 enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3026 write_pending, active_idle, bad_word};
3027 static char *array_states[] = {
3028 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3029 "write-pending", "active-idle", NULL };
3031 static int match_word(const char *word, char **list)
3033 int n;
3034 for (n=0; list[n]; n++)
3035 if (cmd_match(word, list[n]))
3036 break;
3037 return n;
3040 static ssize_t
3041 array_state_show(mddev_t *mddev, char *page)
3043 enum array_state st = inactive;
3045 if (mddev->pers)
3046 switch(mddev->ro) {
3047 case 1:
3048 st = readonly;
3049 break;
3050 case 2:
3051 st = read_auto;
3052 break;
3053 case 0:
3054 if (mddev->in_sync)
3055 st = clean;
3056 else if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
3057 st = write_pending;
3058 else if (mddev->safemode)
3059 st = active_idle;
3060 else
3061 st = active;
3063 else {
3064 if (list_empty(&mddev->disks) &&
3065 mddev->raid_disks == 0 &&
3066 mddev->dev_sectors == 0)
3067 st = clear;
3068 else
3069 st = inactive;
3071 return sprintf(page, "%s\n", array_states[st]);
3074 static int do_md_stop(mddev_t * mddev, int ro, int is_open);
3075 static int do_md_run(mddev_t * mddev);
3076 static int restart_array(mddev_t *mddev);
3078 static ssize_t
3079 array_state_store(mddev_t *mddev, const char *buf, size_t len)
3081 int err = -EINVAL;
3082 enum array_state st = match_word(buf, array_states);
3083 switch(st) {
3084 case bad_word:
3085 break;
3086 case clear:
3087 /* stopping an active array */
3088 if (atomic_read(&mddev->openers) > 0)
3089 return -EBUSY;
3090 err = do_md_stop(mddev, 0, 0);
3091 break;
3092 case inactive:
3093 /* stopping an active array */
3094 if (mddev->pers) {
3095 if (atomic_read(&mddev->openers) > 0)
3096 return -EBUSY;
3097 err = do_md_stop(mddev, 2, 0);
3098 } else
3099 err = 0; /* already inactive */
3100 break;
3101 case suspended:
3102 break; /* not supported yet */
3103 case readonly:
3104 if (mddev->pers)
3105 err = do_md_stop(mddev, 1, 0);
3106 else {
3107 mddev->ro = 1;
3108 set_disk_ro(mddev->gendisk, 1);
3109 err = do_md_run(mddev);
3111 break;
3112 case read_auto:
3113 if (mddev->pers) {
3114 if (mddev->ro == 0)
3115 err = do_md_stop(mddev, 1, 0);
3116 else if (mddev->ro == 1)
3117 err = restart_array(mddev);
3118 if (err == 0) {
3119 mddev->ro = 2;
3120 set_disk_ro(mddev->gendisk, 0);
3122 } else {
3123 mddev->ro = 2;
3124 err = do_md_run(mddev);
3126 break;
3127 case clean:
3128 if (mddev->pers) {
3129 restart_array(mddev);
3130 spin_lock_irq(&mddev->write_lock);
3131 if (atomic_read(&mddev->writes_pending) == 0) {
3132 if (mddev->in_sync == 0) {
3133 mddev->in_sync = 1;
3134 if (mddev->safemode == 1)
3135 mddev->safemode = 0;
3136 if (mddev->persistent)
3137 set_bit(MD_CHANGE_CLEAN,
3138 &mddev->flags);
3140 err = 0;
3141 } else
3142 err = -EBUSY;
3143 spin_unlock_irq(&mddev->write_lock);
3144 } else
3145 err = -EINVAL;
3146 break;
3147 case active:
3148 if (mddev->pers) {
3149 restart_array(mddev);
3150 if (mddev->external)
3151 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
3152 wake_up(&mddev->sb_wait);
3153 err = 0;
3154 } else {
3155 mddev->ro = 0;
3156 set_disk_ro(mddev->gendisk, 0);
3157 err = do_md_run(mddev);
3159 break;
3160 case write_pending:
3161 case active_idle:
3162 /* these cannot be set */
3163 break;
3165 if (err)
3166 return err;
3167 else {
3168 sysfs_notify_dirent(mddev->sysfs_state);
3169 return len;
3172 static struct md_sysfs_entry md_array_state =
3173 __ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
3175 static ssize_t
3176 null_show(mddev_t *mddev, char *page)
3178 return -EINVAL;
3181 static ssize_t
3182 new_dev_store(mddev_t *mddev, const char *buf, size_t len)
3184 /* buf must be %d:%d\n? giving major and minor numbers */
3185 /* The new device is added to the array.
3186 * If the array has a persistent superblock, we read the
3187 * superblock to initialise info and check validity.
3188 * Otherwise, only checking done is that in bind_rdev_to_array,
3189 * which mainly checks size.
3191 char *e;
3192 int major = simple_strtoul(buf, &e, 10);
3193 int minor;
3194 dev_t dev;
3195 mdk_rdev_t *rdev;
3196 int err;
3198 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
3199 return -EINVAL;
3200 minor = simple_strtoul(e+1, &e, 10);
3201 if (*e && *e != '\n')
3202 return -EINVAL;
3203 dev = MKDEV(major, minor);
3204 if (major != MAJOR(dev) ||
3205 minor != MINOR(dev))
3206 return -EOVERFLOW;
3209 if (mddev->persistent) {
3210 rdev = md_import_device(dev, mddev->major_version,
3211 mddev->minor_version);
3212 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
3213 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
3214 mdk_rdev_t, same_set);
3215 err = super_types[mddev->major_version]
3216 .load_super(rdev, rdev0, mddev->minor_version);
3217 if (err < 0)
3218 goto out;
3220 } else if (mddev->external)
3221 rdev = md_import_device(dev, -2, -1);
3222 else
3223 rdev = md_import_device(dev, -1, -1);
3225 if (IS_ERR(rdev))
3226 return PTR_ERR(rdev);
3227 err = bind_rdev_to_array(rdev, mddev);
3228 out:
3229 if (err)
3230 export_rdev(rdev);
3231 return err ? err : len;
3234 static struct md_sysfs_entry md_new_device =
3235 __ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
3237 static ssize_t
3238 bitmap_store(mddev_t *mddev, const char *buf, size_t len)
3240 char *end;
3241 unsigned long chunk, end_chunk;
3243 if (!mddev->bitmap)
3244 goto out;
3245 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3246 while (*buf) {
3247 chunk = end_chunk = simple_strtoul(buf, &end, 0);
3248 if (buf == end) break;
3249 if (*end == '-') { /* range */
3250 buf = end + 1;
3251 end_chunk = simple_strtoul(buf, &end, 0);
3252 if (buf == end) break;
3254 if (*end && !isspace(*end)) break;
3255 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
3256 buf = end;
3257 while (isspace(*buf)) buf++;
3259 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
3260 out:
3261 return len;
3264 static struct md_sysfs_entry md_bitmap =
3265 __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
3267 static ssize_t
3268 size_show(mddev_t *mddev, char *page)
3270 return sprintf(page, "%llu\n",
3271 (unsigned long long)mddev->dev_sectors / 2);
3274 static int update_size(mddev_t *mddev, sector_t num_sectors);
3276 static ssize_t
3277 size_store(mddev_t *mddev, const char *buf, size_t len)
3279 /* If array is inactive, we can reduce the component size, but
3280 * not increase it (except from 0).
3281 * If array is active, we can try an on-line resize
3283 sector_t sectors;
3284 int err = strict_blocks_to_sectors(buf, &sectors);
3286 if (err < 0)
3287 return err;
3288 if (mddev->pers) {
3289 err = update_size(mddev, sectors);
3290 md_update_sb(mddev, 1);
3291 } else {
3292 if (mddev->dev_sectors == 0 ||
3293 mddev->dev_sectors > sectors)
3294 mddev->dev_sectors = sectors;
3295 else
3296 err = -ENOSPC;
3298 return err ? err : len;
3301 static struct md_sysfs_entry md_size =
3302 __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
3305 /* Metdata version.
3306 * This is one of
3307 * 'none' for arrays with no metadata (good luck...)
3308 * 'external' for arrays with externally managed metadata,
3309 * or N.M for internally known formats
3311 static ssize_t
3312 metadata_show(mddev_t *mddev, char *page)
3314 if (mddev->persistent)
3315 return sprintf(page, "%d.%d\n",
3316 mddev->major_version, mddev->minor_version);
3317 else if (mddev->external)
3318 return sprintf(page, "external:%s\n", mddev->metadata_type);
3319 else
3320 return sprintf(page, "none\n");
3323 static ssize_t
3324 metadata_store(mddev_t *mddev, const char *buf, size_t len)
3326 int major, minor;
3327 char *e;
3328 /* Changing the details of 'external' metadata is
3329 * always permitted. Otherwise there must be
3330 * no devices attached to the array.
3332 if (mddev->external && strncmp(buf, "external:", 9) == 0)
3334 else if (!list_empty(&mddev->disks))
3335 return -EBUSY;
3337 if (cmd_match(buf, "none")) {
3338 mddev->persistent = 0;
3339 mddev->external = 0;
3340 mddev->major_version = 0;
3341 mddev->minor_version = 90;
3342 return len;
3344 if (strncmp(buf, "external:", 9) == 0) {
3345 size_t namelen = len-9;
3346 if (namelen >= sizeof(mddev->metadata_type))
3347 namelen = sizeof(mddev->metadata_type)-1;
3348 strncpy(mddev->metadata_type, buf+9, namelen);
3349 mddev->metadata_type[namelen] = 0;
3350 if (namelen && mddev->metadata_type[namelen-1] == '\n')
3351 mddev->metadata_type[--namelen] = 0;
3352 mddev->persistent = 0;
3353 mddev->external = 1;
3354 mddev->major_version = 0;
3355 mddev->minor_version = 90;
3356 return len;
3358 major = simple_strtoul(buf, &e, 10);
3359 if (e==buf || *e != '.')
3360 return -EINVAL;
3361 buf = e+1;
3362 minor = simple_strtoul(buf, &e, 10);
3363 if (e==buf || (*e && *e != '\n') )
3364 return -EINVAL;
3365 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
3366 return -ENOENT;
3367 mddev->major_version = major;
3368 mddev->minor_version = minor;
3369 mddev->persistent = 1;
3370 mddev->external = 0;
3371 return len;
3374 static struct md_sysfs_entry md_metadata =
3375 __ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
3377 static ssize_t
3378 action_show(mddev_t *mddev, char *page)
3380 char *type = "idle";
3381 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
3382 type = "frozen";
3383 else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3384 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
3385 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
3386 type = "reshape";
3387 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
3388 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
3389 type = "resync";
3390 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
3391 type = "check";
3392 else
3393 type = "repair";
3394 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
3395 type = "recover";
3397 return sprintf(page, "%s\n", type);
3400 static ssize_t
3401 action_store(mddev_t *mddev, const char *page, size_t len)
3403 if (!mddev->pers || !mddev->pers->sync_request)
3404 return -EINVAL;
3406 if (cmd_match(page, "frozen"))
3407 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3408 else
3409 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3411 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
3412 if (mddev->sync_thread) {
3413 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3414 md_unregister_thread(mddev->sync_thread);
3415 mddev->sync_thread = NULL;
3416 mddev->recovery = 0;
3418 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3419 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
3420 return -EBUSY;
3421 else if (cmd_match(page, "resync"))
3422 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3423 else if (cmd_match(page, "recover")) {
3424 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
3425 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3426 } else if (cmd_match(page, "reshape")) {
3427 int err;
3428 if (mddev->pers->start_reshape == NULL)
3429 return -EINVAL;
3430 err = mddev->pers->start_reshape(mddev);
3431 if (err)
3432 return err;
3433 sysfs_notify(&mddev->kobj, NULL, "degraded");
3434 } else {
3435 if (cmd_match(page, "check"))
3436 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
3437 else if (!cmd_match(page, "repair"))
3438 return -EINVAL;
3439 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
3440 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
3442 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3443 md_wakeup_thread(mddev->thread);
3444 sysfs_notify_dirent(mddev->sysfs_action);
3445 return len;
3448 static ssize_t
3449 mismatch_cnt_show(mddev_t *mddev, char *page)
3451 return sprintf(page, "%llu\n",
3452 (unsigned long long) mddev->resync_mismatches);
3455 static struct md_sysfs_entry md_scan_mode =
3456 __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
3459 static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
3461 static ssize_t
3462 sync_min_show(mddev_t *mddev, char *page)
3464 return sprintf(page, "%d (%s)\n", speed_min(mddev),
3465 mddev->sync_speed_min ? "local": "system");
3468 static ssize_t
3469 sync_min_store(mddev_t *mddev, const char *buf, size_t len)
3471 int min;
3472 char *e;
3473 if (strncmp(buf, "system", 6)==0) {
3474 mddev->sync_speed_min = 0;
3475 return len;
3477 min = simple_strtoul(buf, &e, 10);
3478 if (buf == e || (*e && *e != '\n') || min <= 0)
3479 return -EINVAL;
3480 mddev->sync_speed_min = min;
3481 return len;
3484 static struct md_sysfs_entry md_sync_min =
3485 __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
3487 static ssize_t
3488 sync_max_show(mddev_t *mddev, char *page)
3490 return sprintf(page, "%d (%s)\n", speed_max(mddev),
3491 mddev->sync_speed_max ? "local": "system");
3494 static ssize_t
3495 sync_max_store(mddev_t *mddev, const char *buf, size_t len)
3497 int max;
3498 char *e;
3499 if (strncmp(buf, "system", 6)==0) {
3500 mddev->sync_speed_max = 0;
3501 return len;
3503 max = simple_strtoul(buf, &e, 10);
3504 if (buf == e || (*e && *e != '\n') || max <= 0)
3505 return -EINVAL;
3506 mddev->sync_speed_max = max;
3507 return len;
3510 static struct md_sysfs_entry md_sync_max =
3511 __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
3513 static ssize_t
3514 degraded_show(mddev_t *mddev, char *page)
3516 return sprintf(page, "%d\n", mddev->degraded);
3518 static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
3520 static ssize_t
3521 sync_force_parallel_show(mddev_t *mddev, char *page)
3523 return sprintf(page, "%d\n", mddev->parallel_resync);
3526 static ssize_t
3527 sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len)
3529 long n;
3531 if (strict_strtol(buf, 10, &n))
3532 return -EINVAL;
3534 if (n != 0 && n != 1)
3535 return -EINVAL;
3537 mddev->parallel_resync = n;
3539 if (mddev->sync_thread)
3540 wake_up(&resync_wait);
3542 return len;
3545 /* force parallel resync, even with shared block devices */
3546 static struct md_sysfs_entry md_sync_force_parallel =
3547 __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
3548 sync_force_parallel_show, sync_force_parallel_store);
3550 static ssize_t
3551 sync_speed_show(mddev_t *mddev, char *page)
3553 unsigned long resync, dt, db;
3554 if (mddev->curr_resync == 0)
3555 return sprintf(page, "none\n");
3556 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
3557 dt = (jiffies - mddev->resync_mark) / HZ;
3558 if (!dt) dt++;
3559 db = resync - mddev->resync_mark_cnt;
3560 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
3563 static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
3565 static ssize_t
3566 sync_completed_show(mddev_t *mddev, char *page)
3568 unsigned long max_sectors, resync;
3570 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3571 return sprintf(page, "none\n");
3573 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
3574 max_sectors = mddev->resync_max_sectors;
3575 else
3576 max_sectors = mddev->dev_sectors;
3578 resync = mddev->curr_resync_completed;
3579 return sprintf(page, "%lu / %lu\n", resync, max_sectors);
3582 static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
3584 static ssize_t
3585 min_sync_show(mddev_t *mddev, char *page)
3587 return sprintf(page, "%llu\n",
3588 (unsigned long long)mddev->resync_min);
3590 static ssize_t
3591 min_sync_store(mddev_t *mddev, const char *buf, size_t len)
3593 unsigned long long min;
3594 if (strict_strtoull(buf, 10, &min))
3595 return -EINVAL;
3596 if (min > mddev->resync_max)
3597 return -EINVAL;
3598 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3599 return -EBUSY;
3601 /* Must be a multiple of chunk_size */
3602 if (mddev->chunk_sectors) {
3603 sector_t temp = min;
3604 if (sector_div(temp, mddev->chunk_sectors))
3605 return -EINVAL;
3607 mddev->resync_min = min;
3609 return len;
3612 static struct md_sysfs_entry md_min_sync =
3613 __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
3615 static ssize_t
3616 max_sync_show(mddev_t *mddev, char *page)
3618 if (mddev->resync_max == MaxSector)
3619 return sprintf(page, "max\n");
3620 else
3621 return sprintf(page, "%llu\n",
3622 (unsigned long long)mddev->resync_max);
3624 static ssize_t
3625 max_sync_store(mddev_t *mddev, const char *buf, size_t len)
3627 if (strncmp(buf, "max", 3) == 0)
3628 mddev->resync_max = MaxSector;
3629 else {
3630 unsigned long long max;
3631 if (strict_strtoull(buf, 10, &max))
3632 return -EINVAL;
3633 if (max < mddev->resync_min)
3634 return -EINVAL;
3635 if (max < mddev->resync_max &&
3636 mddev->ro == 0 &&
3637 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3638 return -EBUSY;
3640 /* Must be a multiple of chunk_size */
3641 if (mddev->chunk_sectors) {
3642 sector_t temp = max;
3643 if (sector_div(temp, mddev->chunk_sectors))
3644 return -EINVAL;
3646 mddev->resync_max = max;
3648 wake_up(&mddev->recovery_wait);
3649 return len;
3652 static struct md_sysfs_entry md_max_sync =
3653 __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
3655 static ssize_t
3656 suspend_lo_show(mddev_t *mddev, char *page)
3658 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
3661 static ssize_t
3662 suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
3664 char *e;
3665 unsigned long long new = simple_strtoull(buf, &e, 10);
3667 if (mddev->pers == NULL ||
3668 mddev->pers->quiesce == NULL)
3669 return -EINVAL;
3670 if (buf == e || (*e && *e != '\n'))
3671 return -EINVAL;
3672 if (new >= mddev->suspend_hi ||
3673 (new > mddev->suspend_lo && new < mddev->suspend_hi)) {
3674 mddev->suspend_lo = new;
3675 mddev->pers->quiesce(mddev, 2);
3676 return len;
3677 } else
3678 return -EINVAL;
3680 static struct md_sysfs_entry md_suspend_lo =
3681 __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
3684 static ssize_t
3685 suspend_hi_show(mddev_t *mddev, char *page)
3687 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
3690 static ssize_t
3691 suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
3693 char *e;
3694 unsigned long long new = simple_strtoull(buf, &e, 10);
3696 if (mddev->pers == NULL ||
3697 mddev->pers->quiesce == NULL)
3698 return -EINVAL;
3699 if (buf == e || (*e && *e != '\n'))
3700 return -EINVAL;
3701 if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) ||
3702 (new > mddev->suspend_lo && new > mddev->suspend_hi)) {
3703 mddev->suspend_hi = new;
3704 mddev->pers->quiesce(mddev, 1);
3705 mddev->pers->quiesce(mddev, 0);
3706 return len;
3707 } else
3708 return -EINVAL;
3710 static struct md_sysfs_entry md_suspend_hi =
3711 __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
3713 static ssize_t
3714 reshape_position_show(mddev_t *mddev, char *page)
3716 if (mddev->reshape_position != MaxSector)
3717 return sprintf(page, "%llu\n",
3718 (unsigned long long)mddev->reshape_position);
3719 strcpy(page, "none\n");
3720 return 5;
3723 static ssize_t
3724 reshape_position_store(mddev_t *mddev, const char *buf, size_t len)
3726 char *e;
3727 unsigned long long new = simple_strtoull(buf, &e, 10);
3728 if (mddev->pers)
3729 return -EBUSY;
3730 if (buf == e || (*e && *e != '\n'))
3731 return -EINVAL;
3732 mddev->reshape_position = new;
3733 mddev->delta_disks = 0;
3734 mddev->new_level = mddev->level;
3735 mddev->new_layout = mddev->layout;
3736 mddev->new_chunk_sectors = mddev->chunk_sectors;
3737 return len;
3740 static struct md_sysfs_entry md_reshape_position =
3741 __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
3742 reshape_position_store);
3744 static ssize_t
3745 array_size_show(mddev_t *mddev, char *page)
3747 if (mddev->external_size)
3748 return sprintf(page, "%llu\n",
3749 (unsigned long long)mddev->array_sectors/2);
3750 else
3751 return sprintf(page, "default\n");
3754 static ssize_t
3755 array_size_store(mddev_t *mddev, const char *buf, size_t len)
3757 sector_t sectors;
3759 if (strncmp(buf, "default", 7) == 0) {
3760 if (mddev->pers)
3761 sectors = mddev->pers->size(mddev, 0, 0);
3762 else
3763 sectors = mddev->array_sectors;
3765 mddev->external_size = 0;
3766 } else {
3767 if (strict_blocks_to_sectors(buf, &sectors) < 0)
3768 return -EINVAL;
3769 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
3770 return -E2BIG;
3772 mddev->external_size = 1;
3775 mddev->array_sectors = sectors;
3776 set_capacity(mddev->gendisk, mddev->array_sectors);
3777 if (mddev->pers)
3778 revalidate_disk(mddev->gendisk);
3780 return len;
3783 static struct md_sysfs_entry md_array_size =
3784 __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
3785 array_size_store);
3787 static struct attribute *md_default_attrs[] = {
3788 &md_level.attr,
3789 &md_layout.attr,
3790 &md_raid_disks.attr,
3791 &md_chunk_size.attr,
3792 &md_size.attr,
3793 &md_resync_start.attr,
3794 &md_metadata.attr,
3795 &md_new_device.attr,
3796 &md_safe_delay.attr,
3797 &md_array_state.attr,
3798 &md_reshape_position.attr,
3799 &md_array_size.attr,
3800 NULL,
3803 static struct attribute *md_redundancy_attrs[] = {
3804 &md_scan_mode.attr,
3805 &md_mismatches.attr,
3806 &md_sync_min.attr,
3807 &md_sync_max.attr,
3808 &md_sync_speed.attr,
3809 &md_sync_force_parallel.attr,
3810 &md_sync_completed.attr,
3811 &md_min_sync.attr,
3812 &md_max_sync.attr,
3813 &md_suspend_lo.attr,
3814 &md_suspend_hi.attr,
3815 &md_bitmap.attr,
3816 &md_degraded.attr,
3817 NULL,
3819 static struct attribute_group md_redundancy_group = {
3820 .name = NULL,
3821 .attrs = md_redundancy_attrs,
3825 static ssize_t
3826 md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3828 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
3829 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
3830 ssize_t rv;
3832 if (!entry->show)
3833 return -EIO;
3834 rv = mddev_lock(mddev);
3835 if (!rv) {
3836 rv = entry->show(mddev, page);
3837 mddev_unlock(mddev);
3839 return rv;
3842 static ssize_t
3843 md_attr_store(struct kobject *kobj, struct attribute *attr,
3844 const char *page, size_t length)
3846 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
3847 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
3848 ssize_t rv;
3850 if (!entry->store)
3851 return -EIO;
3852 if (!capable(CAP_SYS_ADMIN))
3853 return -EACCES;
3854 rv = mddev_lock(mddev);
3855 if (mddev->hold_active == UNTIL_IOCTL)
3856 mddev->hold_active = 0;
3857 if (!rv) {
3858 rv = entry->store(mddev, page, length);
3859 mddev_unlock(mddev);
3861 return rv;
3864 static void md_free(struct kobject *ko)
3866 mddev_t *mddev = container_of(ko, mddev_t, kobj);
3868 if (mddev->sysfs_state)
3869 sysfs_put(mddev->sysfs_state);
3871 if (mddev->gendisk) {
3872 del_gendisk(mddev->gendisk);
3873 put_disk(mddev->gendisk);
3875 if (mddev->queue)
3876 blk_cleanup_queue(mddev->queue);
3878 kfree(mddev);
3881 static struct sysfs_ops md_sysfs_ops = {
3882 .show = md_attr_show,
3883 .store = md_attr_store,
3885 static struct kobj_type md_ktype = {
3886 .release = md_free,
3887 .sysfs_ops = &md_sysfs_ops,
3888 .default_attrs = md_default_attrs,
3891 int mdp_major = 0;
3893 static void mddev_delayed_delete(struct work_struct *ws)
3895 mddev_t *mddev = container_of(ws, mddev_t, del_work);
3897 if (mddev->private == &md_redundancy_group) {
3898 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
3899 if (mddev->sysfs_action)
3900 sysfs_put(mddev->sysfs_action);
3901 mddev->sysfs_action = NULL;
3902 mddev->private = NULL;
3904 kobject_del(&mddev->kobj);
3905 kobject_put(&mddev->kobj);
3908 static int md_alloc(dev_t dev, char *name)
3910 static DEFINE_MUTEX(disks_mutex);
3911 mddev_t *mddev = mddev_find(dev);
3912 struct gendisk *disk;
3913 int partitioned;
3914 int shift;
3915 int unit;
3916 int error;
3918 if (!mddev)
3919 return -ENODEV;
3921 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
3922 shift = partitioned ? MdpMinorShift : 0;
3923 unit = MINOR(mddev->unit) >> shift;
3925 /* wait for any previous instance if this device
3926 * to be completed removed (mddev_delayed_delete).
3928 flush_scheduled_work();
3930 mutex_lock(&disks_mutex);
3931 error = -EEXIST;
3932 if (mddev->gendisk)
3933 goto abort;
3935 if (name) {
3936 /* Need to ensure that 'name' is not a duplicate.
3938 mddev_t *mddev2;
3939 spin_lock(&all_mddevs_lock);
3941 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
3942 if (mddev2->gendisk &&
3943 strcmp(mddev2->gendisk->disk_name, name) == 0) {
3944 spin_unlock(&all_mddevs_lock);
3945 goto abort;
3947 spin_unlock(&all_mddevs_lock);
3950 error = -ENOMEM;
3951 mddev->queue = blk_alloc_queue(GFP_KERNEL);
3952 if (!mddev->queue)
3953 goto abort;
3954 mddev->queue->queuedata = mddev;
3956 /* Can be unlocked because the queue is new: no concurrency */
3957 queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, mddev->queue);
3959 blk_queue_make_request(mddev->queue, md_make_request);
3961 disk = alloc_disk(1 << shift);
3962 if (!disk) {
3963 blk_cleanup_queue(mddev->queue);
3964 mddev->queue = NULL;
3965 goto abort;
3967 disk->major = MAJOR(mddev->unit);
3968 disk->first_minor = unit << shift;
3969 if (name)
3970 strcpy(disk->disk_name, name);
3971 else if (partitioned)
3972 sprintf(disk->disk_name, "md_d%d", unit);
3973 else
3974 sprintf(disk->disk_name, "md%d", unit);
3975 disk->fops = &md_fops;
3976 disk->private_data = mddev;
3977 disk->queue = mddev->queue;
3978 /* Allow extended partitions. This makes the
3979 * 'mdp' device redundant, but we can't really
3980 * remove it now.
3982 disk->flags |= GENHD_FL_EXT_DEVT;
3983 add_disk(disk);
3984 mddev->gendisk = disk;
3985 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
3986 &disk_to_dev(disk)->kobj, "%s", "md");
3987 if (error) {
3988 /* This isn't possible, but as kobject_init_and_add is marked
3989 * __must_check, we must do something with the result
3991 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
3992 disk->disk_name);
3993 error = 0;
3995 abort:
3996 mutex_unlock(&disks_mutex);
3997 if (!error) {
3998 kobject_uevent(&mddev->kobj, KOBJ_ADD);
3999 mddev->sysfs_state = sysfs_get_dirent(mddev->kobj.sd, "array_state");
4001 mddev_put(mddev);
4002 return error;
4005 static struct kobject *md_probe(dev_t dev, int *part, void *data)
4007 md_alloc(dev, NULL);
4008 return NULL;
4011 static int add_named_array(const char *val, struct kernel_param *kp)
4013 /* val must be "md_*" where * is not all digits.
4014 * We allocate an array with a large free minor number, and
4015 * set the name to val. val must not already be an active name.
4017 int len = strlen(val);
4018 char buf[DISK_NAME_LEN];
4020 while (len && val[len-1] == '\n')
4021 len--;
4022 if (len >= DISK_NAME_LEN)
4023 return -E2BIG;
4024 strlcpy(buf, val, len+1);
4025 if (strncmp(buf, "md_", 3) != 0)
4026 return -EINVAL;
4027 return md_alloc(0, buf);
4030 static void md_safemode_timeout(unsigned long data)
4032 mddev_t *mddev = (mddev_t *) data;
4034 if (!atomic_read(&mddev->writes_pending)) {
4035 mddev->safemode = 1;
4036 if (mddev->external)
4037 sysfs_notify_dirent(mddev->sysfs_state);
4039 md_wakeup_thread(mddev->thread);
4042 static int start_dirty_degraded;
4044 static int do_md_run(mddev_t * mddev)
4046 int err;
4047 mdk_rdev_t *rdev;
4048 struct gendisk *disk;
4049 struct mdk_personality *pers;
4051 if (list_empty(&mddev->disks))
4052 /* cannot run an array with no devices.. */
4053 return -EINVAL;
4055 if (mddev->pers)
4056 return -EBUSY;
4059 * Analyze all RAID superblock(s)
4061 if (!mddev->raid_disks) {
4062 if (!mddev->persistent)
4063 return -EINVAL;
4064 analyze_sbs(mddev);
4067 if (mddev->level != LEVEL_NONE)
4068 request_module("md-level-%d", mddev->level);
4069 else if (mddev->clevel[0])
4070 request_module("md-%s", mddev->clevel);
4073 * Drop all container device buffers, from now on
4074 * the only valid external interface is through the md
4075 * device.
4077 list_for_each_entry(rdev, &mddev->disks, same_set) {
4078 if (test_bit(Faulty, &rdev->flags))
4079 continue;
4080 sync_blockdev(rdev->bdev);
4081 invalidate_bdev(rdev->bdev);
4083 /* perform some consistency tests on the device.
4084 * We don't want the data to overlap the metadata,
4085 * Internal Bitmap issues have been handled elsewhere.
4087 if (rdev->data_offset < rdev->sb_start) {
4088 if (mddev->dev_sectors &&
4089 rdev->data_offset + mddev->dev_sectors
4090 > rdev->sb_start) {
4091 printk("md: %s: data overlaps metadata\n",
4092 mdname(mddev));
4093 return -EINVAL;
4095 } else {
4096 if (rdev->sb_start + rdev->sb_size/512
4097 > rdev->data_offset) {
4098 printk("md: %s: metadata overlaps data\n",
4099 mdname(mddev));
4100 return -EINVAL;
4103 sysfs_notify_dirent(rdev->sysfs_state);
4106 md_probe(mddev->unit, NULL, NULL);
4107 disk = mddev->gendisk;
4108 if (!disk)
4109 return -ENOMEM;
4111 spin_lock(&pers_lock);
4112 pers = find_pers(mddev->level, mddev->clevel);
4113 if (!pers || !try_module_get(pers->owner)) {
4114 spin_unlock(&pers_lock);
4115 if (mddev->level != LEVEL_NONE)
4116 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
4117 mddev->level);
4118 else
4119 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
4120 mddev->clevel);
4121 return -EINVAL;
4123 mddev->pers = pers;
4124 spin_unlock(&pers_lock);
4125 if (mddev->level != pers->level) {
4126 mddev->level = pers->level;
4127 mddev->new_level = pers->level;
4129 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
4131 if (mddev->reshape_position != MaxSector &&
4132 pers->start_reshape == NULL) {
4133 /* This personality cannot handle reshaping... */
4134 mddev->pers = NULL;
4135 module_put(pers->owner);
4136 return -EINVAL;
4139 if (pers->sync_request) {
4140 /* Warn if this is a potentially silly
4141 * configuration.
4143 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4144 mdk_rdev_t *rdev2;
4145 int warned = 0;
4147 list_for_each_entry(rdev, &mddev->disks, same_set)
4148 list_for_each_entry(rdev2, &mddev->disks, same_set) {
4149 if (rdev < rdev2 &&
4150 rdev->bdev->bd_contains ==
4151 rdev2->bdev->bd_contains) {
4152 printk(KERN_WARNING
4153 "%s: WARNING: %s appears to be"
4154 " on the same physical disk as"
4155 " %s.\n",
4156 mdname(mddev),
4157 bdevname(rdev->bdev,b),
4158 bdevname(rdev2->bdev,b2));
4159 warned = 1;
4163 if (warned)
4164 printk(KERN_WARNING
4165 "True protection against single-disk"
4166 " failure might be compromised.\n");
4169 mddev->recovery = 0;
4170 /* may be over-ridden by personality */
4171 mddev->resync_max_sectors = mddev->dev_sectors;
4173 mddev->barriers_work = 1;
4174 mddev->ok_start_degraded = start_dirty_degraded;
4176 if (start_readonly && mddev->ro == 0)
4177 mddev->ro = 2; /* read-only, but switch on first write */
4179 err = mddev->pers->run(mddev);
4180 if (err)
4181 printk(KERN_ERR "md: pers->run() failed ...\n");
4182 else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
4183 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
4184 " but 'external_size' not in effect?\n", __func__);
4185 printk(KERN_ERR
4186 "md: invalid array_size %llu > default size %llu\n",
4187 (unsigned long long)mddev->array_sectors / 2,
4188 (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
4189 err = -EINVAL;
4190 mddev->pers->stop(mddev);
4192 if (err == 0 && mddev->pers->sync_request) {
4193 err = bitmap_create(mddev);
4194 if (err) {
4195 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
4196 mdname(mddev), err);
4197 mddev->pers->stop(mddev);
4200 if (err) {
4201 module_put(mddev->pers->owner);
4202 mddev->pers = NULL;
4203 bitmap_destroy(mddev);
4204 return err;
4206 if (mddev->pers->sync_request) {
4207 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
4208 printk(KERN_WARNING
4209 "md: cannot register extra attributes for %s\n",
4210 mdname(mddev));
4211 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
4212 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
4213 mddev->ro = 0;
4215 atomic_set(&mddev->writes_pending,0);
4216 mddev->safemode = 0;
4217 mddev->safemode_timer.function = md_safemode_timeout;
4218 mddev->safemode_timer.data = (unsigned long) mddev;
4219 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
4220 mddev->in_sync = 1;
4222 list_for_each_entry(rdev, &mddev->disks, same_set)
4223 if (rdev->raid_disk >= 0) {
4224 char nm[20];
4225 sprintf(nm, "rd%d", rdev->raid_disk);
4226 if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
4227 printk("md: cannot register %s for %s\n",
4228 nm, mdname(mddev));
4231 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4233 if (mddev->flags)
4234 md_update_sb(mddev, 0);
4236 set_capacity(disk, mddev->array_sectors);
4238 /* If there is a partially-recovered drive we need to
4239 * start recovery here. If we leave it to md_check_recovery,
4240 * it will remove the drives and not do the right thing
4242 if (mddev->degraded && !mddev->sync_thread) {
4243 int spares = 0;
4244 list_for_each_entry(rdev, &mddev->disks, same_set)
4245 if (rdev->raid_disk >= 0 &&
4246 !test_bit(In_sync, &rdev->flags) &&
4247 !test_bit(Faulty, &rdev->flags))
4248 /* complete an interrupted recovery */
4249 spares++;
4250 if (spares && mddev->pers->sync_request) {
4251 mddev->recovery = 0;
4252 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
4253 mddev->sync_thread = md_register_thread(md_do_sync,
4254 mddev,
4255 "resync");
4256 if (!mddev->sync_thread) {
4257 printk(KERN_ERR "%s: could not start resync"
4258 " thread...\n",
4259 mdname(mddev));
4260 /* leave the spares where they are, it shouldn't hurt */
4261 mddev->recovery = 0;
4265 md_wakeup_thread(mddev->thread);
4266 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
4268 revalidate_disk(mddev->gendisk);
4269 mddev->changed = 1;
4270 md_new_event(mddev);
4271 sysfs_notify_dirent(mddev->sysfs_state);
4272 if (mddev->sysfs_action)
4273 sysfs_notify_dirent(mddev->sysfs_action);
4274 sysfs_notify(&mddev->kobj, NULL, "degraded");
4275 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
4276 return 0;
4279 static int restart_array(mddev_t *mddev)
4281 struct gendisk *disk = mddev->gendisk;
4283 /* Complain if it has no devices */
4284 if (list_empty(&mddev->disks))
4285 return -ENXIO;
4286 if (!mddev->pers)
4287 return -EINVAL;
4288 if (!mddev->ro)
4289 return -EBUSY;
4290 mddev->safemode = 0;
4291 mddev->ro = 0;
4292 set_disk_ro(disk, 0);
4293 printk(KERN_INFO "md: %s switched to read-write mode.\n",
4294 mdname(mddev));
4295 /* Kick recovery or resync if necessary */
4296 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4297 md_wakeup_thread(mddev->thread);
4298 md_wakeup_thread(mddev->sync_thread);
4299 sysfs_notify_dirent(mddev->sysfs_state);
4300 return 0;
4303 /* similar to deny_write_access, but accounts for our holding a reference
4304 * to the file ourselves */
4305 static int deny_bitmap_write_access(struct file * file)
4307 struct inode *inode = file->f_mapping->host;
4309 spin_lock(&inode->i_lock);
4310 if (atomic_read(&inode->i_writecount) > 1) {
4311 spin_unlock(&inode->i_lock);
4312 return -ETXTBSY;
4314 atomic_set(&inode->i_writecount, -1);
4315 spin_unlock(&inode->i_lock);
4317 return 0;
4320 static void restore_bitmap_write_access(struct file *file)
4322 struct inode *inode = file->f_mapping->host;
4324 spin_lock(&inode->i_lock);
4325 atomic_set(&inode->i_writecount, 1);
4326 spin_unlock(&inode->i_lock);
4329 /* mode:
4330 * 0 - completely stop and dis-assemble array
4331 * 1 - switch to readonly
4332 * 2 - stop but do not disassemble array
4334 static int do_md_stop(mddev_t * mddev, int mode, int is_open)
4336 int err = 0;
4337 struct gendisk *disk = mddev->gendisk;
4338 mdk_rdev_t *rdev;
4340 mutex_lock(&mddev->open_mutex);
4341 if (atomic_read(&mddev->openers) > is_open) {
4342 printk("md: %s still in use.\n",mdname(mddev));
4343 err = -EBUSY;
4344 } else if (mddev->pers) {
4346 if (mddev->sync_thread) {
4347 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4348 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4349 md_unregister_thread(mddev->sync_thread);
4350 mddev->sync_thread = NULL;
4353 del_timer_sync(&mddev->safemode_timer);
4355 switch(mode) {
4356 case 1: /* readonly */
4357 err = -ENXIO;
4358 if (mddev->ro==1)
4359 goto out;
4360 mddev->ro = 1;
4361 break;
4362 case 0: /* disassemble */
4363 case 2: /* stop */
4364 bitmap_flush(mddev);
4365 md_super_wait(mddev);
4366 if (mddev->ro)
4367 set_disk_ro(disk, 0);
4369 mddev->pers->stop(mddev);
4370 mddev->queue->merge_bvec_fn = NULL;
4371 mddev->queue->unplug_fn = NULL;
4372 mddev->queue->backing_dev_info.congested_fn = NULL;
4373 module_put(mddev->pers->owner);
4374 if (mddev->pers->sync_request)
4375 mddev->private = &md_redundancy_group;
4376 mddev->pers = NULL;
4377 /* tell userspace to handle 'inactive' */
4378 sysfs_notify_dirent(mddev->sysfs_state);
4380 list_for_each_entry(rdev, &mddev->disks, same_set)
4381 if (rdev->raid_disk >= 0) {
4382 char nm[20];
4383 sprintf(nm, "rd%d", rdev->raid_disk);
4384 sysfs_remove_link(&mddev->kobj, nm);
4387 set_capacity(disk, 0);
4388 mddev->changed = 1;
4390 if (mddev->ro)
4391 mddev->ro = 0;
4393 if (!mddev->in_sync || mddev->flags) {
4394 /* mark array as shutdown cleanly */
4395 mddev->in_sync = 1;
4396 md_update_sb(mddev, 1);
4398 if (mode == 1)
4399 set_disk_ro(disk, 1);
4400 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4401 err = 0;
4403 out:
4404 mutex_unlock(&mddev->open_mutex);
4405 if (err)
4406 return err;
4408 * Free resources if final stop
4410 if (mode == 0) {
4412 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
4414 bitmap_destroy(mddev);
4415 if (mddev->bitmap_file) {
4416 restore_bitmap_write_access(mddev->bitmap_file);
4417 fput(mddev->bitmap_file);
4418 mddev->bitmap_file = NULL;
4420 mddev->bitmap_offset = 0;
4422 /* make sure all md_delayed_delete calls have finished */
4423 flush_scheduled_work();
4425 export_array(mddev);
4427 mddev->array_sectors = 0;
4428 mddev->external_size = 0;
4429 mddev->dev_sectors = 0;
4430 mddev->raid_disks = 0;
4431 mddev->recovery_cp = 0;
4432 mddev->resync_min = 0;
4433 mddev->resync_max = MaxSector;
4434 mddev->reshape_position = MaxSector;
4435 mddev->external = 0;
4436 mddev->persistent = 0;
4437 mddev->level = LEVEL_NONE;
4438 mddev->clevel[0] = 0;
4439 mddev->flags = 0;
4440 mddev->ro = 0;
4441 mddev->metadata_type[0] = 0;
4442 mddev->chunk_sectors = 0;
4443 mddev->ctime = mddev->utime = 0;
4444 mddev->layout = 0;
4445 mddev->max_disks = 0;
4446 mddev->events = 0;
4447 mddev->delta_disks = 0;
4448 mddev->new_level = LEVEL_NONE;
4449 mddev->new_layout = 0;
4450 mddev->new_chunk_sectors = 0;
4451 mddev->curr_resync = 0;
4452 mddev->resync_mismatches = 0;
4453 mddev->suspend_lo = mddev->suspend_hi = 0;
4454 mddev->sync_speed_min = mddev->sync_speed_max = 0;
4455 mddev->recovery = 0;
4456 mddev->in_sync = 0;
4457 mddev->changed = 0;
4458 mddev->degraded = 0;
4459 mddev->barriers_work = 0;
4460 mddev->safemode = 0;
4461 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
4462 if (mddev->hold_active == UNTIL_STOP)
4463 mddev->hold_active = 0;
4465 } else if (mddev->pers)
4466 printk(KERN_INFO "md: %s switched to read-only mode.\n",
4467 mdname(mddev));
4468 err = 0;
4469 blk_integrity_unregister(disk);
4470 md_new_event(mddev);
4471 sysfs_notify_dirent(mddev->sysfs_state);
4472 return err;
4475 #ifndef MODULE
4476 static void autorun_array(mddev_t *mddev)
4478 mdk_rdev_t *rdev;
4479 int err;
4481 if (list_empty(&mddev->disks))
4482 return;
4484 printk(KERN_INFO "md: running: ");
4486 list_for_each_entry(rdev, &mddev->disks, same_set) {
4487 char b[BDEVNAME_SIZE];
4488 printk("<%s>", bdevname(rdev->bdev,b));
4490 printk("\n");
4492 err = do_md_run(mddev);
4493 if (err) {
4494 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
4495 do_md_stop(mddev, 0, 0);
4500 * lets try to run arrays based on all disks that have arrived
4501 * until now. (those are in pending_raid_disks)
4503 * the method: pick the first pending disk, collect all disks with
4504 * the same UUID, remove all from the pending list and put them into
4505 * the 'same_array' list. Then order this list based on superblock
4506 * update time (freshest comes first), kick out 'old' disks and
4507 * compare superblocks. If everything's fine then run it.
4509 * If "unit" is allocated, then bump its reference count
4511 static void autorun_devices(int part)
4513 mdk_rdev_t *rdev0, *rdev, *tmp;
4514 mddev_t *mddev;
4515 char b[BDEVNAME_SIZE];
4517 printk(KERN_INFO "md: autorun ...\n");
4518 while (!list_empty(&pending_raid_disks)) {
4519 int unit;
4520 dev_t dev;
4521 LIST_HEAD(candidates);
4522 rdev0 = list_entry(pending_raid_disks.next,
4523 mdk_rdev_t, same_set);
4525 printk(KERN_INFO "md: considering %s ...\n",
4526 bdevname(rdev0->bdev,b));
4527 INIT_LIST_HEAD(&candidates);
4528 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
4529 if (super_90_load(rdev, rdev0, 0) >= 0) {
4530 printk(KERN_INFO "md: adding %s ...\n",
4531 bdevname(rdev->bdev,b));
4532 list_move(&rdev->same_set, &candidates);
4535 * now we have a set of devices, with all of them having
4536 * mostly sane superblocks. It's time to allocate the
4537 * mddev.
4539 if (part) {
4540 dev = MKDEV(mdp_major,
4541 rdev0->preferred_minor << MdpMinorShift);
4542 unit = MINOR(dev) >> MdpMinorShift;
4543 } else {
4544 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
4545 unit = MINOR(dev);
4547 if (rdev0->preferred_minor != unit) {
4548 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
4549 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
4550 break;
4553 md_probe(dev, NULL, NULL);
4554 mddev = mddev_find(dev);
4555 if (!mddev || !mddev->gendisk) {
4556 if (mddev)
4557 mddev_put(mddev);
4558 printk(KERN_ERR
4559 "md: cannot allocate memory for md drive.\n");
4560 break;
4562 if (mddev_lock(mddev))
4563 printk(KERN_WARNING "md: %s locked, cannot run\n",
4564 mdname(mddev));
4565 else if (mddev->raid_disks || mddev->major_version
4566 || !list_empty(&mddev->disks)) {
4567 printk(KERN_WARNING
4568 "md: %s already running, cannot run %s\n",
4569 mdname(mddev), bdevname(rdev0->bdev,b));
4570 mddev_unlock(mddev);
4571 } else {
4572 printk(KERN_INFO "md: created %s\n", mdname(mddev));
4573 mddev->persistent = 1;
4574 rdev_for_each_list(rdev, tmp, &candidates) {
4575 list_del_init(&rdev->same_set);
4576 if (bind_rdev_to_array(rdev, mddev))
4577 export_rdev(rdev);
4579 autorun_array(mddev);
4580 mddev_unlock(mddev);
4582 /* on success, candidates will be empty, on error
4583 * it won't...
4585 rdev_for_each_list(rdev, tmp, &candidates) {
4586 list_del_init(&rdev->same_set);
4587 export_rdev(rdev);
4589 mddev_put(mddev);
4591 printk(KERN_INFO "md: ... autorun DONE.\n");
4593 #endif /* !MODULE */
4595 static int get_version(void __user * arg)
4597 mdu_version_t ver;
4599 ver.major = MD_MAJOR_VERSION;
4600 ver.minor = MD_MINOR_VERSION;
4601 ver.patchlevel = MD_PATCHLEVEL_VERSION;
4603 if (copy_to_user(arg, &ver, sizeof(ver)))
4604 return -EFAULT;
4606 return 0;
4609 static int get_array_info(mddev_t * mddev, void __user * arg)
4611 mdu_array_info_t info;
4612 int nr,working,insync,failed,spare;
4613 mdk_rdev_t *rdev;
4615 nr=working=insync=failed=spare=0;
4616 list_for_each_entry(rdev, &mddev->disks, same_set) {
4617 nr++;
4618 if (test_bit(Faulty, &rdev->flags))
4619 failed++;
4620 else {
4621 working++;
4622 if (test_bit(In_sync, &rdev->flags))
4623 insync++;
4624 else
4625 spare++;
4629 info.major_version = mddev->major_version;
4630 info.minor_version = mddev->minor_version;
4631 info.patch_version = MD_PATCHLEVEL_VERSION;
4632 info.ctime = mddev->ctime;
4633 info.level = mddev->level;
4634 info.size = mddev->dev_sectors / 2;
4635 if (info.size != mddev->dev_sectors / 2) /* overflow */
4636 info.size = -1;
4637 info.nr_disks = nr;
4638 info.raid_disks = mddev->raid_disks;
4639 info.md_minor = mddev->md_minor;
4640 info.not_persistent= !mddev->persistent;
4642 info.utime = mddev->utime;
4643 info.state = 0;
4644 if (mddev->in_sync)
4645 info.state = (1<<MD_SB_CLEAN);
4646 if (mddev->bitmap && mddev->bitmap_offset)
4647 info.state = (1<<MD_SB_BITMAP_PRESENT);
4648 info.active_disks = insync;
4649 info.working_disks = working;
4650 info.failed_disks = failed;
4651 info.spare_disks = spare;
4653 info.layout = mddev->layout;
4654 info.chunk_size = mddev->chunk_sectors << 9;
4656 if (copy_to_user(arg, &info, sizeof(info)))
4657 return -EFAULT;
4659 return 0;
4662 static int get_bitmap_file(mddev_t * mddev, void __user * arg)
4664 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
4665 char *ptr, *buf = NULL;
4666 int err = -ENOMEM;
4668 if (md_allow_write(mddev))
4669 file = kmalloc(sizeof(*file), GFP_NOIO);
4670 else
4671 file = kmalloc(sizeof(*file), GFP_KERNEL);
4673 if (!file)
4674 goto out;
4676 /* bitmap disabled, zero the first byte and copy out */
4677 if (!mddev->bitmap || !mddev->bitmap->file) {
4678 file->pathname[0] = '\0';
4679 goto copy_out;
4682 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
4683 if (!buf)
4684 goto out;
4686 ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname));
4687 if (IS_ERR(ptr))
4688 goto out;
4690 strcpy(file->pathname, ptr);
4692 copy_out:
4693 err = 0;
4694 if (copy_to_user(arg, file, sizeof(*file)))
4695 err = -EFAULT;
4696 out:
4697 kfree(buf);
4698 kfree(file);
4699 return err;
4702 static int get_disk_info(mddev_t * mddev, void __user * arg)
4704 mdu_disk_info_t info;
4705 mdk_rdev_t *rdev;
4707 if (copy_from_user(&info, arg, sizeof(info)))
4708 return -EFAULT;
4710 rdev = find_rdev_nr(mddev, info.number);
4711 if (rdev) {
4712 info.major = MAJOR(rdev->bdev->bd_dev);
4713 info.minor = MINOR(rdev->bdev->bd_dev);
4714 info.raid_disk = rdev->raid_disk;
4715 info.state = 0;
4716 if (test_bit(Faulty, &rdev->flags))
4717 info.state |= (1<<MD_DISK_FAULTY);
4718 else if (test_bit(In_sync, &rdev->flags)) {
4719 info.state |= (1<<MD_DISK_ACTIVE);
4720 info.state |= (1<<MD_DISK_SYNC);
4722 if (test_bit(WriteMostly, &rdev->flags))
4723 info.state |= (1<<MD_DISK_WRITEMOSTLY);
4724 } else {
4725 info.major = info.minor = 0;
4726 info.raid_disk = -1;
4727 info.state = (1<<MD_DISK_REMOVED);
4730 if (copy_to_user(arg, &info, sizeof(info)))
4731 return -EFAULT;
4733 return 0;
4736 static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
4738 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4739 mdk_rdev_t *rdev;
4740 dev_t dev = MKDEV(info->major,info->minor);
4742 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
4743 return -EOVERFLOW;
4745 if (!mddev->raid_disks) {
4746 int err;
4747 /* expecting a device which has a superblock */
4748 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
4749 if (IS_ERR(rdev)) {
4750 printk(KERN_WARNING
4751 "md: md_import_device returned %ld\n",
4752 PTR_ERR(rdev));
4753 return PTR_ERR(rdev);
4755 if (!list_empty(&mddev->disks)) {
4756 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
4757 mdk_rdev_t, same_set);
4758 err = super_types[mddev->major_version]
4759 .load_super(rdev, rdev0, mddev->minor_version);
4760 if (err < 0) {
4761 printk(KERN_WARNING
4762 "md: %s has different UUID to %s\n",
4763 bdevname(rdev->bdev,b),
4764 bdevname(rdev0->bdev,b2));
4765 export_rdev(rdev);
4766 return -EINVAL;
4769 err = bind_rdev_to_array(rdev, mddev);
4770 if (err)
4771 export_rdev(rdev);
4772 return err;
4776 * add_new_disk can be used once the array is assembled
4777 * to add "hot spares". They must already have a superblock
4778 * written
4780 if (mddev->pers) {
4781 int err;
4782 if (!mddev->pers->hot_add_disk) {
4783 printk(KERN_WARNING
4784 "%s: personality does not support diskops!\n",
4785 mdname(mddev));
4786 return -EINVAL;
4788 if (mddev->persistent)
4789 rdev = md_import_device(dev, mddev->major_version,
4790 mddev->minor_version);
4791 else
4792 rdev = md_import_device(dev, -1, -1);
4793 if (IS_ERR(rdev)) {
4794 printk(KERN_WARNING
4795 "md: md_import_device returned %ld\n",
4796 PTR_ERR(rdev));
4797 return PTR_ERR(rdev);
4799 /* set save_raid_disk if appropriate */
4800 if (!mddev->persistent) {
4801 if (info->state & (1<<MD_DISK_SYNC) &&
4802 info->raid_disk < mddev->raid_disks)
4803 rdev->raid_disk = info->raid_disk;
4804 else
4805 rdev->raid_disk = -1;
4806 } else
4807 super_types[mddev->major_version].
4808 validate_super(mddev, rdev);
4809 rdev->saved_raid_disk = rdev->raid_disk;
4811 clear_bit(In_sync, &rdev->flags); /* just to be sure */
4812 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
4813 set_bit(WriteMostly, &rdev->flags);
4814 else
4815 clear_bit(WriteMostly, &rdev->flags);
4817 rdev->raid_disk = -1;
4818 err = bind_rdev_to_array(rdev, mddev);
4819 if (!err && !mddev->pers->hot_remove_disk) {
4820 /* If there is hot_add_disk but no hot_remove_disk
4821 * then added disks for geometry changes,
4822 * and should be added immediately.
4824 super_types[mddev->major_version].
4825 validate_super(mddev, rdev);
4826 err = mddev->pers->hot_add_disk(mddev, rdev);
4827 if (err)
4828 unbind_rdev_from_array(rdev);
4830 if (err)
4831 export_rdev(rdev);
4832 else
4833 sysfs_notify_dirent(rdev->sysfs_state);
4835 md_update_sb(mddev, 1);
4836 if (mddev->degraded)
4837 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
4838 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4839 md_wakeup_thread(mddev->thread);
4840 return err;
4843 /* otherwise, add_new_disk is only allowed
4844 * for major_version==0 superblocks
4846 if (mddev->major_version != 0) {
4847 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
4848 mdname(mddev));
4849 return -EINVAL;
4852 if (!(info->state & (1<<MD_DISK_FAULTY))) {
4853 int err;
4854 rdev = md_import_device(dev, -1, 0);
4855 if (IS_ERR(rdev)) {
4856 printk(KERN_WARNING
4857 "md: error, md_import_device() returned %ld\n",
4858 PTR_ERR(rdev));
4859 return PTR_ERR(rdev);
4861 rdev->desc_nr = info->number;
4862 if (info->raid_disk < mddev->raid_disks)
4863 rdev->raid_disk = info->raid_disk;
4864 else
4865 rdev->raid_disk = -1;
4867 if (rdev->raid_disk < mddev->raid_disks)
4868 if (info->state & (1<<MD_DISK_SYNC))
4869 set_bit(In_sync, &rdev->flags);
4871 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
4872 set_bit(WriteMostly, &rdev->flags);
4874 if (!mddev->persistent) {
4875 printk(KERN_INFO "md: nonpersistent superblock ...\n");
4876 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
4877 } else
4878 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
4879 rdev->sectors = rdev->sb_start;
4881 err = bind_rdev_to_array(rdev, mddev);
4882 if (err) {
4883 export_rdev(rdev);
4884 return err;
4888 return 0;
4891 static int hot_remove_disk(mddev_t * mddev, dev_t dev)
4893 char b[BDEVNAME_SIZE];
4894 mdk_rdev_t *rdev;
4896 rdev = find_rdev(mddev, dev);
4897 if (!rdev)
4898 return -ENXIO;
4900 if (rdev->raid_disk >= 0)
4901 goto busy;
4903 kick_rdev_from_array(rdev);
4904 md_update_sb(mddev, 1);
4905 md_new_event(mddev);
4907 return 0;
4908 busy:
4909 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
4910 bdevname(rdev->bdev,b), mdname(mddev));
4911 return -EBUSY;
4914 static int hot_add_disk(mddev_t * mddev, dev_t dev)
4916 char b[BDEVNAME_SIZE];
4917 int err;
4918 mdk_rdev_t *rdev;
4920 if (!mddev->pers)
4921 return -ENODEV;
4923 if (mddev->major_version != 0) {
4924 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
4925 " version-0 superblocks.\n",
4926 mdname(mddev));
4927 return -EINVAL;
4929 if (!mddev->pers->hot_add_disk) {
4930 printk(KERN_WARNING
4931 "%s: personality does not support diskops!\n",
4932 mdname(mddev));
4933 return -EINVAL;
4936 rdev = md_import_device(dev, -1, 0);
4937 if (IS_ERR(rdev)) {
4938 printk(KERN_WARNING
4939 "md: error, md_import_device() returned %ld\n",
4940 PTR_ERR(rdev));
4941 return -EINVAL;
4944 if (mddev->persistent)
4945 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
4946 else
4947 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
4949 rdev->sectors = rdev->sb_start;
4951 if (test_bit(Faulty, &rdev->flags)) {
4952 printk(KERN_WARNING
4953 "md: can not hot-add faulty %s disk to %s!\n",
4954 bdevname(rdev->bdev,b), mdname(mddev));
4955 err = -EINVAL;
4956 goto abort_export;
4958 clear_bit(In_sync, &rdev->flags);
4959 rdev->desc_nr = -1;
4960 rdev->saved_raid_disk = -1;
4961 err = bind_rdev_to_array(rdev, mddev);
4962 if (err)
4963 goto abort_export;
4966 * The rest should better be atomic, we can have disk failures
4967 * noticed in interrupt contexts ...
4970 rdev->raid_disk = -1;
4972 md_update_sb(mddev, 1);
4975 * Kick recovery, maybe this spare has to be added to the
4976 * array immediately.
4978 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4979 md_wakeup_thread(mddev->thread);
4980 md_new_event(mddev);
4981 return 0;
4983 abort_export:
4984 export_rdev(rdev);
4985 return err;
4988 static int set_bitmap_file(mddev_t *mddev, int fd)
4990 int err;
4992 if (mddev->pers) {
4993 if (!mddev->pers->quiesce)
4994 return -EBUSY;
4995 if (mddev->recovery || mddev->sync_thread)
4996 return -EBUSY;
4997 /* we should be able to change the bitmap.. */
5001 if (fd >= 0) {
5002 if (mddev->bitmap)
5003 return -EEXIST; /* cannot add when bitmap is present */
5004 mddev->bitmap_file = fget(fd);
5006 if (mddev->bitmap_file == NULL) {
5007 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
5008 mdname(mddev));
5009 return -EBADF;
5012 err = deny_bitmap_write_access(mddev->bitmap_file);
5013 if (err) {
5014 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
5015 mdname(mddev));
5016 fput(mddev->bitmap_file);
5017 mddev->bitmap_file = NULL;
5018 return err;
5020 mddev->bitmap_offset = 0; /* file overrides offset */
5021 } else if (mddev->bitmap == NULL)
5022 return -ENOENT; /* cannot remove what isn't there */
5023 err = 0;
5024 if (mddev->pers) {
5025 mddev->pers->quiesce(mddev, 1);
5026 if (fd >= 0)
5027 err = bitmap_create(mddev);
5028 if (fd < 0 || err) {
5029 bitmap_destroy(mddev);
5030 fd = -1; /* make sure to put the file */
5032 mddev->pers->quiesce(mddev, 0);
5034 if (fd < 0) {
5035 if (mddev->bitmap_file) {
5036 restore_bitmap_write_access(mddev->bitmap_file);
5037 fput(mddev->bitmap_file);
5039 mddev->bitmap_file = NULL;
5042 return err;
5046 * set_array_info is used two different ways
5047 * The original usage is when creating a new array.
5048 * In this usage, raid_disks is > 0 and it together with
5049 * level, size, not_persistent,layout,chunksize determine the
5050 * shape of the array.
5051 * This will always create an array with a type-0.90.0 superblock.
5052 * The newer usage is when assembling an array.
5053 * In this case raid_disks will be 0, and the major_version field is
5054 * use to determine which style super-blocks are to be found on the devices.
5055 * The minor and patch _version numbers are also kept incase the
5056 * super_block handler wishes to interpret them.
5058 static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
5061 if (info->raid_disks == 0) {
5062 /* just setting version number for superblock loading */
5063 if (info->major_version < 0 ||
5064 info->major_version >= ARRAY_SIZE(super_types) ||
5065 super_types[info->major_version].name == NULL) {
5066 /* maybe try to auto-load a module? */
5067 printk(KERN_INFO
5068 "md: superblock version %d not known\n",
5069 info->major_version);
5070 return -EINVAL;
5072 mddev->major_version = info->major_version;
5073 mddev->minor_version = info->minor_version;
5074 mddev->patch_version = info->patch_version;
5075 mddev->persistent = !info->not_persistent;
5076 /* ensure mddev_put doesn't delete this now that there
5077 * is some minimal configuration.
5079 mddev->ctime = get_seconds();
5080 return 0;
5082 mddev->major_version = MD_MAJOR_VERSION;
5083 mddev->minor_version = MD_MINOR_VERSION;
5084 mddev->patch_version = MD_PATCHLEVEL_VERSION;
5085 mddev->ctime = get_seconds();
5087 mddev->level = info->level;
5088 mddev->clevel[0] = 0;
5089 mddev->dev_sectors = 2 * (sector_t)info->size;
5090 mddev->raid_disks = info->raid_disks;
5091 /* don't set md_minor, it is determined by which /dev/md* was
5092 * openned
5094 if (info->state & (1<<MD_SB_CLEAN))
5095 mddev->recovery_cp = MaxSector;
5096 else
5097 mddev->recovery_cp = 0;
5098 mddev->persistent = ! info->not_persistent;
5099 mddev->external = 0;
5101 mddev->layout = info->layout;
5102 mddev->chunk_sectors = info->chunk_size >> 9;
5104 mddev->max_disks = MD_SB_DISKS;
5106 if (mddev->persistent)
5107 mddev->flags = 0;
5108 set_bit(MD_CHANGE_DEVS, &mddev->flags);
5110 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
5111 mddev->bitmap_offset = 0;
5113 mddev->reshape_position = MaxSector;
5116 * Generate a 128 bit UUID
5118 get_random_bytes(mddev->uuid, 16);
5120 mddev->new_level = mddev->level;
5121 mddev->new_chunk_sectors = mddev->chunk_sectors;
5122 mddev->new_layout = mddev->layout;
5123 mddev->delta_disks = 0;
5125 return 0;
5128 void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors)
5130 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
5132 if (mddev->external_size)
5133 return;
5135 mddev->array_sectors = array_sectors;
5137 EXPORT_SYMBOL(md_set_array_sectors);
5139 static int update_size(mddev_t *mddev, sector_t num_sectors)
5141 mdk_rdev_t *rdev;
5142 int rv;
5143 int fit = (num_sectors == 0);
5145 if (mddev->pers->resize == NULL)
5146 return -EINVAL;
5147 /* The "num_sectors" is the number of sectors of each device that
5148 * is used. This can only make sense for arrays with redundancy.
5149 * linear and raid0 always use whatever space is available. We can only
5150 * consider changing this number if no resync or reconstruction is
5151 * happening, and if the new size is acceptable. It must fit before the
5152 * sb_start or, if that is <data_offset, it must fit before the size
5153 * of each device. If num_sectors is zero, we find the largest size
5154 * that fits.
5157 if (mddev->sync_thread)
5158 return -EBUSY;
5159 if (mddev->bitmap)
5160 /* Sorry, cannot grow a bitmap yet, just remove it,
5161 * grow, and re-add.
5163 return -EBUSY;
5164 list_for_each_entry(rdev, &mddev->disks, same_set) {
5165 sector_t avail = rdev->sectors;
5167 if (fit && (num_sectors == 0 || num_sectors > avail))
5168 num_sectors = avail;
5169 if (avail < num_sectors)
5170 return -ENOSPC;
5172 rv = mddev->pers->resize(mddev, num_sectors);
5173 if (!rv)
5174 revalidate_disk(mddev->gendisk);
5175 return rv;
5178 static int update_raid_disks(mddev_t *mddev, int raid_disks)
5180 int rv;
5181 /* change the number of raid disks */
5182 if (mddev->pers->check_reshape == NULL)
5183 return -EINVAL;
5184 if (raid_disks <= 0 ||
5185 raid_disks >= mddev->max_disks)
5186 return -EINVAL;
5187 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
5188 return -EBUSY;
5189 mddev->delta_disks = raid_disks - mddev->raid_disks;
5191 rv = mddev->pers->check_reshape(mddev);
5192 return rv;
5197 * update_array_info is used to change the configuration of an
5198 * on-line array.
5199 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
5200 * fields in the info are checked against the array.
5201 * Any differences that cannot be handled will cause an error.
5202 * Normally, only one change can be managed at a time.
5204 static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
5206 int rv = 0;
5207 int cnt = 0;
5208 int state = 0;
5210 /* calculate expected state,ignoring low bits */
5211 if (mddev->bitmap && mddev->bitmap_offset)
5212 state |= (1 << MD_SB_BITMAP_PRESENT);
5214 if (mddev->major_version != info->major_version ||
5215 mddev->minor_version != info->minor_version ||
5216 /* mddev->patch_version != info->patch_version || */
5217 mddev->ctime != info->ctime ||
5218 mddev->level != info->level ||
5219 /* mddev->layout != info->layout || */
5220 !mddev->persistent != info->not_persistent||
5221 mddev->chunk_sectors != info->chunk_size >> 9 ||
5222 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
5223 ((state^info->state) & 0xfffffe00)
5225 return -EINVAL;
5226 /* Check there is only one change */
5227 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
5228 cnt++;
5229 if (mddev->raid_disks != info->raid_disks)
5230 cnt++;
5231 if (mddev->layout != info->layout)
5232 cnt++;
5233 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
5234 cnt++;
5235 if (cnt == 0)
5236 return 0;
5237 if (cnt > 1)
5238 return -EINVAL;
5240 if (mddev->layout != info->layout) {
5241 /* Change layout
5242 * we don't need to do anything at the md level, the
5243 * personality will take care of it all.
5245 if (mddev->pers->check_reshape == NULL)
5246 return -EINVAL;
5247 else {
5248 mddev->new_layout = info->layout;
5249 rv = mddev->pers->check_reshape(mddev);
5250 if (rv)
5251 mddev->new_layout = mddev->layout;
5252 return rv;
5255 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
5256 rv = update_size(mddev, (sector_t)info->size * 2);
5258 if (mddev->raid_disks != info->raid_disks)
5259 rv = update_raid_disks(mddev, info->raid_disks);
5261 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
5262 if (mddev->pers->quiesce == NULL)
5263 return -EINVAL;
5264 if (mddev->recovery || mddev->sync_thread)
5265 return -EBUSY;
5266 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
5267 /* add the bitmap */
5268 if (mddev->bitmap)
5269 return -EEXIST;
5270 if (mddev->default_bitmap_offset == 0)
5271 return -EINVAL;
5272 mddev->bitmap_offset = mddev->default_bitmap_offset;
5273 mddev->pers->quiesce(mddev, 1);
5274 rv = bitmap_create(mddev);
5275 if (rv)
5276 bitmap_destroy(mddev);
5277 mddev->pers->quiesce(mddev, 0);
5278 } else {
5279 /* remove the bitmap */
5280 if (!mddev->bitmap)
5281 return -ENOENT;
5282 if (mddev->bitmap->file)
5283 return -EINVAL;
5284 mddev->pers->quiesce(mddev, 1);
5285 bitmap_destroy(mddev);
5286 mddev->pers->quiesce(mddev, 0);
5287 mddev->bitmap_offset = 0;
5290 md_update_sb(mddev, 1);
5291 return rv;
5294 static int set_disk_faulty(mddev_t *mddev, dev_t dev)
5296 mdk_rdev_t *rdev;
5298 if (mddev->pers == NULL)
5299 return -ENODEV;
5301 rdev = find_rdev(mddev, dev);
5302 if (!rdev)
5303 return -ENODEV;
5305 md_error(mddev, rdev);
5306 return 0;
5310 * We have a problem here : there is no easy way to give a CHS
5311 * virtual geometry. We currently pretend that we have a 2 heads
5312 * 4 sectors (with a BIG number of cylinders...). This drives
5313 * dosfs just mad... ;-)
5315 static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
5317 mddev_t *mddev = bdev->bd_disk->private_data;
5319 geo->heads = 2;
5320 geo->sectors = 4;
5321 geo->cylinders = get_capacity(mddev->gendisk) / 8;
5322 return 0;
5325 static int md_ioctl(struct block_device *bdev, fmode_t mode,
5326 unsigned int cmd, unsigned long arg)
5328 int err = 0;
5329 void __user *argp = (void __user *)arg;
5330 mddev_t *mddev = NULL;
5332 if (!capable(CAP_SYS_ADMIN))
5333 return -EACCES;
5336 * Commands dealing with the RAID driver but not any
5337 * particular array:
5339 switch (cmd)
5341 case RAID_VERSION:
5342 err = get_version(argp);
5343 goto done;
5345 case PRINT_RAID_DEBUG:
5346 err = 0;
5347 md_print_devices();
5348 goto done;
5350 #ifndef MODULE
5351 case RAID_AUTORUN:
5352 err = 0;
5353 autostart_arrays(arg);
5354 goto done;
5355 #endif
5356 default:;
5360 * Commands creating/starting a new array:
5363 mddev = bdev->bd_disk->private_data;
5365 if (!mddev) {
5366 BUG();
5367 goto abort;
5370 err = mddev_lock(mddev);
5371 if (err) {
5372 printk(KERN_INFO
5373 "md: ioctl lock interrupted, reason %d, cmd %d\n",
5374 err, cmd);
5375 goto abort;
5378 switch (cmd)
5380 case SET_ARRAY_INFO:
5382 mdu_array_info_t info;
5383 if (!arg)
5384 memset(&info, 0, sizeof(info));
5385 else if (copy_from_user(&info, argp, sizeof(info))) {
5386 err = -EFAULT;
5387 goto abort_unlock;
5389 if (mddev->pers) {
5390 err = update_array_info(mddev, &info);
5391 if (err) {
5392 printk(KERN_WARNING "md: couldn't update"
5393 " array info. %d\n", err);
5394 goto abort_unlock;
5396 goto done_unlock;
5398 if (!list_empty(&mddev->disks)) {
5399 printk(KERN_WARNING
5400 "md: array %s already has disks!\n",
5401 mdname(mddev));
5402 err = -EBUSY;
5403 goto abort_unlock;
5405 if (mddev->raid_disks) {
5406 printk(KERN_WARNING
5407 "md: array %s already initialised!\n",
5408 mdname(mddev));
5409 err = -EBUSY;
5410 goto abort_unlock;
5412 err = set_array_info(mddev, &info);
5413 if (err) {
5414 printk(KERN_WARNING "md: couldn't set"
5415 " array info. %d\n", err);
5416 goto abort_unlock;
5419 goto done_unlock;
5421 default:;
5425 * Commands querying/configuring an existing array:
5427 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
5428 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
5429 if ((!mddev->raid_disks && !mddev->external)
5430 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
5431 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
5432 && cmd != GET_BITMAP_FILE) {
5433 err = -ENODEV;
5434 goto abort_unlock;
5438 * Commands even a read-only array can execute:
5440 switch (cmd)
5442 case GET_ARRAY_INFO:
5443 err = get_array_info(mddev, argp);
5444 goto done_unlock;
5446 case GET_BITMAP_FILE:
5447 err = get_bitmap_file(mddev, argp);
5448 goto done_unlock;
5450 case GET_DISK_INFO:
5451 err = get_disk_info(mddev, argp);
5452 goto done_unlock;
5454 case RESTART_ARRAY_RW:
5455 err = restart_array(mddev);
5456 goto done_unlock;
5458 case STOP_ARRAY:
5459 err = do_md_stop(mddev, 0, 1);
5460 goto done_unlock;
5462 case STOP_ARRAY_RO:
5463 err = do_md_stop(mddev, 1, 1);
5464 goto done_unlock;
5469 * The remaining ioctls are changing the state of the
5470 * superblock, so we do not allow them on read-only arrays.
5471 * However non-MD ioctls (e.g. get-size) will still come through
5472 * here and hit the 'default' below, so only disallow
5473 * 'md' ioctls, and switch to rw mode if started auto-readonly.
5475 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
5476 if (mddev->ro == 2) {
5477 mddev->ro = 0;
5478 sysfs_notify_dirent(mddev->sysfs_state);
5479 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5480 md_wakeup_thread(mddev->thread);
5481 } else {
5482 err = -EROFS;
5483 goto abort_unlock;
5487 switch (cmd)
5489 case ADD_NEW_DISK:
5491 mdu_disk_info_t info;
5492 if (copy_from_user(&info, argp, sizeof(info)))
5493 err = -EFAULT;
5494 else
5495 err = add_new_disk(mddev, &info);
5496 goto done_unlock;
5499 case HOT_REMOVE_DISK:
5500 err = hot_remove_disk(mddev, new_decode_dev(arg));
5501 goto done_unlock;
5503 case HOT_ADD_DISK:
5504 err = hot_add_disk(mddev, new_decode_dev(arg));
5505 goto done_unlock;
5507 case SET_DISK_FAULTY:
5508 err = set_disk_faulty(mddev, new_decode_dev(arg));
5509 goto done_unlock;
5511 case RUN_ARRAY:
5512 err = do_md_run(mddev);
5513 goto done_unlock;
5515 case SET_BITMAP_FILE:
5516 err = set_bitmap_file(mddev, (int)arg);
5517 goto done_unlock;
5519 default:
5520 err = -EINVAL;
5521 goto abort_unlock;
5524 done_unlock:
5525 abort_unlock:
5526 if (mddev->hold_active == UNTIL_IOCTL &&
5527 err != -EINVAL)
5528 mddev->hold_active = 0;
5529 mddev_unlock(mddev);
5531 return err;
5532 done:
5533 if (err)
5534 MD_BUG();
5535 abort:
5536 return err;
5539 static int md_open(struct block_device *bdev, fmode_t mode)
5542 * Succeed if we can lock the mddev, which confirms that
5543 * it isn't being stopped right now.
5545 mddev_t *mddev = mddev_find(bdev->bd_dev);
5546 int err;
5548 if (mddev->gendisk != bdev->bd_disk) {
5549 /* we are racing with mddev_put which is discarding this
5550 * bd_disk.
5552 mddev_put(mddev);
5553 /* Wait until bdev->bd_disk is definitely gone */
5554 flush_scheduled_work();
5555 /* Then retry the open from the top */
5556 return -ERESTARTSYS;
5558 BUG_ON(mddev != bdev->bd_disk->private_data);
5560 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
5561 goto out;
5563 err = 0;
5564 atomic_inc(&mddev->openers);
5565 mutex_unlock(&mddev->open_mutex);
5567 check_disk_change(bdev);
5568 out:
5569 return err;
5572 static int md_release(struct gendisk *disk, fmode_t mode)
5574 mddev_t *mddev = disk->private_data;
5576 BUG_ON(!mddev);
5577 atomic_dec(&mddev->openers);
5578 mddev_put(mddev);
5580 return 0;
5583 static int md_media_changed(struct gendisk *disk)
5585 mddev_t *mddev = disk->private_data;
5587 return mddev->changed;
5590 static int md_revalidate(struct gendisk *disk)
5592 mddev_t *mddev = disk->private_data;
5594 mddev->changed = 0;
5595 return 0;
5597 static const struct block_device_operations md_fops =
5599 .owner = THIS_MODULE,
5600 .open = md_open,
5601 .release = md_release,
5602 .ioctl = md_ioctl,
5603 .getgeo = md_getgeo,
5604 .media_changed = md_media_changed,
5605 .revalidate_disk= md_revalidate,
5608 static int md_thread(void * arg)
5610 mdk_thread_t *thread = arg;
5613 * md_thread is a 'system-thread', it's priority should be very
5614 * high. We avoid resource deadlocks individually in each
5615 * raid personality. (RAID5 does preallocation) We also use RR and
5616 * the very same RT priority as kswapd, thus we will never get
5617 * into a priority inversion deadlock.
5619 * we definitely have to have equal or higher priority than
5620 * bdflush, otherwise bdflush will deadlock if there are too
5621 * many dirty RAID5 blocks.
5624 allow_signal(SIGKILL);
5625 while (!kthread_should_stop()) {
5627 /* We need to wait INTERRUPTIBLE so that
5628 * we don't add to the load-average.
5629 * That means we need to be sure no signals are
5630 * pending
5632 if (signal_pending(current))
5633 flush_signals(current);
5635 wait_event_interruptible_timeout
5636 (thread->wqueue,
5637 test_bit(THREAD_WAKEUP, &thread->flags)
5638 || kthread_should_stop(),
5639 thread->timeout);
5641 clear_bit(THREAD_WAKEUP, &thread->flags);
5643 thread->run(thread->mddev);
5646 return 0;
5649 void md_wakeup_thread(mdk_thread_t *thread)
5651 if (thread) {
5652 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
5653 set_bit(THREAD_WAKEUP, &thread->flags);
5654 wake_up(&thread->wqueue);
5658 mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
5659 const char *name)
5661 mdk_thread_t *thread;
5663 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
5664 if (!thread)
5665 return NULL;
5667 init_waitqueue_head(&thread->wqueue);
5669 thread->run = run;
5670 thread->mddev = mddev;
5671 thread->timeout = MAX_SCHEDULE_TIMEOUT;
5672 thread->tsk = kthread_run(md_thread, thread,
5673 "%s_%s",
5674 mdname(thread->mddev),
5675 name ?: mddev->pers->name);
5676 if (IS_ERR(thread->tsk)) {
5677 kfree(thread);
5678 return NULL;
5680 return thread;
5683 void md_unregister_thread(mdk_thread_t *thread)
5685 if (!thread)
5686 return;
5687 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
5689 kthread_stop(thread->tsk);
5690 kfree(thread);
5693 void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
5695 if (!mddev) {
5696 MD_BUG();
5697 return;
5700 if (!rdev || test_bit(Faulty, &rdev->flags))
5701 return;
5703 if (mddev->external)
5704 set_bit(Blocked, &rdev->flags);
5706 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
5707 mdname(mddev),
5708 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
5709 __builtin_return_address(0),__builtin_return_address(1),
5710 __builtin_return_address(2),__builtin_return_address(3));
5712 if (!mddev->pers)
5713 return;
5714 if (!mddev->pers->error_handler)
5715 return;
5716 mddev->pers->error_handler(mddev,rdev);
5717 if (mddev->degraded)
5718 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
5719 set_bit(StateChanged, &rdev->flags);
5720 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5721 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5722 md_wakeup_thread(mddev->thread);
5723 md_new_event_inintr(mddev);
5726 /* seq_file implementation /proc/mdstat */
5728 static void status_unused(struct seq_file *seq)
5730 int i = 0;
5731 mdk_rdev_t *rdev;
5733 seq_printf(seq, "unused devices: ");
5735 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
5736 char b[BDEVNAME_SIZE];
5737 i++;
5738 seq_printf(seq, "%s ",
5739 bdevname(rdev->bdev,b));
5741 if (!i)
5742 seq_printf(seq, "<none>");
5744 seq_printf(seq, "\n");
5748 static void status_resync(struct seq_file *seq, mddev_t * mddev)
5750 sector_t max_sectors, resync, res;
5751 unsigned long dt, db;
5752 sector_t rt;
5753 int scale;
5754 unsigned int per_milli;
5756 resync = mddev->curr_resync - atomic_read(&mddev->recovery_active);
5758 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
5759 max_sectors = mddev->resync_max_sectors;
5760 else
5761 max_sectors = mddev->dev_sectors;
5764 * Should not happen.
5766 if (!max_sectors) {
5767 MD_BUG();
5768 return;
5770 /* Pick 'scale' such that (resync>>scale)*1000 will fit
5771 * in a sector_t, and (max_sectors>>scale) will fit in a
5772 * u32, as those are the requirements for sector_div.
5773 * Thus 'scale' must be at least 10
5775 scale = 10;
5776 if (sizeof(sector_t) > sizeof(unsigned long)) {
5777 while ( max_sectors/2 > (1ULL<<(scale+32)))
5778 scale++;
5780 res = (resync>>scale)*1000;
5781 sector_div(res, (u32)((max_sectors>>scale)+1));
5783 per_milli = res;
5785 int i, x = per_milli/50, y = 20-x;
5786 seq_printf(seq, "[");
5787 for (i = 0; i < x; i++)
5788 seq_printf(seq, "=");
5789 seq_printf(seq, ">");
5790 for (i = 0; i < y; i++)
5791 seq_printf(seq, ".");
5792 seq_printf(seq, "] ");
5794 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
5795 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
5796 "reshape" :
5797 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
5798 "check" :
5799 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
5800 "resync" : "recovery"))),
5801 per_milli/10, per_milli % 10,
5802 (unsigned long long) resync/2,
5803 (unsigned long long) max_sectors/2);
5806 * dt: time from mark until now
5807 * db: blocks written from mark until now
5808 * rt: remaining time
5810 * rt is a sector_t, so could be 32bit or 64bit.
5811 * So we divide before multiply in case it is 32bit and close
5812 * to the limit.
5813 * We scale the divisor (db) by 32 to avoid loosing precision
5814 * near the end of resync when the number of remaining sectors
5815 * is close to 'db'.
5816 * We then divide rt by 32 after multiplying by db to compensate.
5817 * The '+1' avoids division by zero if db is very small.
5819 dt = ((jiffies - mddev->resync_mark) / HZ);
5820 if (!dt) dt++;
5821 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
5822 - mddev->resync_mark_cnt;
5824 rt = max_sectors - resync; /* number of remaining sectors */
5825 sector_div(rt, db/32+1);
5826 rt *= dt;
5827 rt >>= 5;
5829 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
5830 ((unsigned long)rt % 60)/6);
5832 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
5835 static void *md_seq_start(struct seq_file *seq, loff_t *pos)
5837 struct list_head *tmp;
5838 loff_t l = *pos;
5839 mddev_t *mddev;
5841 if (l >= 0x10000)
5842 return NULL;
5843 if (!l--)
5844 /* header */
5845 return (void*)1;
5847 spin_lock(&all_mddevs_lock);
5848 list_for_each(tmp,&all_mddevs)
5849 if (!l--) {
5850 mddev = list_entry(tmp, mddev_t, all_mddevs);
5851 mddev_get(mddev);
5852 spin_unlock(&all_mddevs_lock);
5853 return mddev;
5855 spin_unlock(&all_mddevs_lock);
5856 if (!l--)
5857 return (void*)2;/* tail */
5858 return NULL;
5861 static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
5863 struct list_head *tmp;
5864 mddev_t *next_mddev, *mddev = v;
5866 ++*pos;
5867 if (v == (void*)2)
5868 return NULL;
5870 spin_lock(&all_mddevs_lock);
5871 if (v == (void*)1)
5872 tmp = all_mddevs.next;
5873 else
5874 tmp = mddev->all_mddevs.next;
5875 if (tmp != &all_mddevs)
5876 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
5877 else {
5878 next_mddev = (void*)2;
5879 *pos = 0x10000;
5881 spin_unlock(&all_mddevs_lock);
5883 if (v != (void*)1)
5884 mddev_put(mddev);
5885 return next_mddev;
5889 static void md_seq_stop(struct seq_file *seq, void *v)
5891 mddev_t *mddev = v;
5893 if (mddev && v != (void*)1 && v != (void*)2)
5894 mddev_put(mddev);
5897 struct mdstat_info {
5898 int event;
5901 static int md_seq_show(struct seq_file *seq, void *v)
5903 mddev_t *mddev = v;
5904 sector_t sectors;
5905 mdk_rdev_t *rdev;
5906 struct mdstat_info *mi = seq->private;
5907 struct bitmap *bitmap;
5909 if (v == (void*)1) {
5910 struct mdk_personality *pers;
5911 seq_printf(seq, "Personalities : ");
5912 spin_lock(&pers_lock);
5913 list_for_each_entry(pers, &pers_list, list)
5914 seq_printf(seq, "[%s] ", pers->name);
5916 spin_unlock(&pers_lock);
5917 seq_printf(seq, "\n");
5918 mi->event = atomic_read(&md_event_count);
5919 return 0;
5921 if (v == (void*)2) {
5922 status_unused(seq);
5923 return 0;
5926 if (mddev_lock(mddev) < 0)
5927 return -EINTR;
5929 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
5930 seq_printf(seq, "%s : %sactive", mdname(mddev),
5931 mddev->pers ? "" : "in");
5932 if (mddev->pers) {
5933 if (mddev->ro==1)
5934 seq_printf(seq, " (read-only)");
5935 if (mddev->ro==2)
5936 seq_printf(seq, " (auto-read-only)");
5937 seq_printf(seq, " %s", mddev->pers->name);
5940 sectors = 0;
5941 list_for_each_entry(rdev, &mddev->disks, same_set) {
5942 char b[BDEVNAME_SIZE];
5943 seq_printf(seq, " %s[%d]",
5944 bdevname(rdev->bdev,b), rdev->desc_nr);
5945 if (test_bit(WriteMostly, &rdev->flags))
5946 seq_printf(seq, "(W)");
5947 if (test_bit(Faulty, &rdev->flags)) {
5948 seq_printf(seq, "(F)");
5949 continue;
5950 } else if (rdev->raid_disk < 0)
5951 seq_printf(seq, "(S)"); /* spare */
5952 sectors += rdev->sectors;
5955 if (!list_empty(&mddev->disks)) {
5956 if (mddev->pers)
5957 seq_printf(seq, "\n %llu blocks",
5958 (unsigned long long)
5959 mddev->array_sectors / 2);
5960 else
5961 seq_printf(seq, "\n %llu blocks",
5962 (unsigned long long)sectors / 2);
5964 if (mddev->persistent) {
5965 if (mddev->major_version != 0 ||
5966 mddev->minor_version != 90) {
5967 seq_printf(seq," super %d.%d",
5968 mddev->major_version,
5969 mddev->minor_version);
5971 } else if (mddev->external)
5972 seq_printf(seq, " super external:%s",
5973 mddev->metadata_type);
5974 else
5975 seq_printf(seq, " super non-persistent");
5977 if (mddev->pers) {
5978 mddev->pers->status(seq, mddev);
5979 seq_printf(seq, "\n ");
5980 if (mddev->pers->sync_request) {
5981 if (mddev->curr_resync > 2) {
5982 status_resync(seq, mddev);
5983 seq_printf(seq, "\n ");
5984 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
5985 seq_printf(seq, "\tresync=DELAYED\n ");
5986 else if (mddev->recovery_cp < MaxSector)
5987 seq_printf(seq, "\tresync=PENDING\n ");
5989 } else
5990 seq_printf(seq, "\n ");
5992 if ((bitmap = mddev->bitmap)) {
5993 unsigned long chunk_kb;
5994 unsigned long flags;
5995 spin_lock_irqsave(&bitmap->lock, flags);
5996 chunk_kb = bitmap->chunksize >> 10;
5997 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
5998 "%lu%s chunk",
5999 bitmap->pages - bitmap->missing_pages,
6000 bitmap->pages,
6001 (bitmap->pages - bitmap->missing_pages)
6002 << (PAGE_SHIFT - 10),
6003 chunk_kb ? chunk_kb : bitmap->chunksize,
6004 chunk_kb ? "KB" : "B");
6005 if (bitmap->file) {
6006 seq_printf(seq, ", file: ");
6007 seq_path(seq, &bitmap->file->f_path, " \t\n");
6010 seq_printf(seq, "\n");
6011 spin_unlock_irqrestore(&bitmap->lock, flags);
6014 seq_printf(seq, "\n");
6016 mddev_unlock(mddev);
6018 return 0;
6021 static const struct seq_operations md_seq_ops = {
6022 .start = md_seq_start,
6023 .next = md_seq_next,
6024 .stop = md_seq_stop,
6025 .show = md_seq_show,
6028 static int md_seq_open(struct inode *inode, struct file *file)
6030 int error;
6031 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
6032 if (mi == NULL)
6033 return -ENOMEM;
6035 error = seq_open(file, &md_seq_ops);
6036 if (error)
6037 kfree(mi);
6038 else {
6039 struct seq_file *p = file->private_data;
6040 p->private = mi;
6041 mi->event = atomic_read(&md_event_count);
6043 return error;
6046 static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
6048 struct seq_file *m = filp->private_data;
6049 struct mdstat_info *mi = m->private;
6050 int mask;
6052 poll_wait(filp, &md_event_waiters, wait);
6054 /* always allow read */
6055 mask = POLLIN | POLLRDNORM;
6057 if (mi->event != atomic_read(&md_event_count))
6058 mask |= POLLERR | POLLPRI;
6059 return mask;
6062 static const struct file_operations md_seq_fops = {
6063 .owner = THIS_MODULE,
6064 .open = md_seq_open,
6065 .read = seq_read,
6066 .llseek = seq_lseek,
6067 .release = seq_release_private,
6068 .poll = mdstat_poll,
6071 int register_md_personality(struct mdk_personality *p)
6073 spin_lock(&pers_lock);
6074 list_add_tail(&p->list, &pers_list);
6075 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
6076 spin_unlock(&pers_lock);
6077 return 0;
6080 int unregister_md_personality(struct mdk_personality *p)
6082 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
6083 spin_lock(&pers_lock);
6084 list_del_init(&p->list);
6085 spin_unlock(&pers_lock);
6086 return 0;
6089 static int is_mddev_idle(mddev_t *mddev, int init)
6091 mdk_rdev_t * rdev;
6092 int idle;
6093 int curr_events;
6095 idle = 1;
6096 rcu_read_lock();
6097 rdev_for_each_rcu(rdev, mddev) {
6098 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
6099 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
6100 (int)part_stat_read(&disk->part0, sectors[1]) -
6101 atomic_read(&disk->sync_io);
6102 /* sync IO will cause sync_io to increase before the disk_stats
6103 * as sync_io is counted when a request starts, and
6104 * disk_stats is counted when it completes.
6105 * So resync activity will cause curr_events to be smaller than
6106 * when there was no such activity.
6107 * non-sync IO will cause disk_stat to increase without
6108 * increasing sync_io so curr_events will (eventually)
6109 * be larger than it was before. Once it becomes
6110 * substantially larger, the test below will cause
6111 * the array to appear non-idle, and resync will slow
6112 * down.
6113 * If there is a lot of outstanding resync activity when
6114 * we set last_event to curr_events, then all that activity
6115 * completing might cause the array to appear non-idle
6116 * and resync will be slowed down even though there might
6117 * not have been non-resync activity. This will only
6118 * happen once though. 'last_events' will soon reflect
6119 * the state where there is little or no outstanding
6120 * resync requests, and further resync activity will
6121 * always make curr_events less than last_events.
6124 if (init || curr_events - rdev->last_events > 64) {
6125 rdev->last_events = curr_events;
6126 idle = 0;
6129 rcu_read_unlock();
6130 return idle;
6133 void md_done_sync(mddev_t *mddev, int blocks, int ok)
6135 /* another "blocks" (512byte) blocks have been synced */
6136 atomic_sub(blocks, &mddev->recovery_active);
6137 wake_up(&mddev->recovery_wait);
6138 if (!ok) {
6139 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6140 md_wakeup_thread(mddev->thread);
6141 // stop recovery, signal do_sync ....
6146 /* md_write_start(mddev, bi)
6147 * If we need to update some array metadata (e.g. 'active' flag
6148 * in superblock) before writing, schedule a superblock update
6149 * and wait for it to complete.
6151 void md_write_start(mddev_t *mddev, struct bio *bi)
6153 int did_change = 0;
6154 if (bio_data_dir(bi) != WRITE)
6155 return;
6157 BUG_ON(mddev->ro == 1);
6158 if (mddev->ro == 2) {
6159 /* need to switch to read/write */
6160 mddev->ro = 0;
6161 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6162 md_wakeup_thread(mddev->thread);
6163 md_wakeup_thread(mddev->sync_thread);
6164 did_change = 1;
6166 atomic_inc(&mddev->writes_pending);
6167 if (mddev->safemode == 1)
6168 mddev->safemode = 0;
6169 if (mddev->in_sync) {
6170 spin_lock_irq(&mddev->write_lock);
6171 if (mddev->in_sync) {
6172 mddev->in_sync = 0;
6173 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6174 md_wakeup_thread(mddev->thread);
6175 did_change = 1;
6177 spin_unlock_irq(&mddev->write_lock);
6179 if (did_change)
6180 sysfs_notify_dirent(mddev->sysfs_state);
6181 wait_event(mddev->sb_wait,
6182 !test_bit(MD_CHANGE_CLEAN, &mddev->flags) &&
6183 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
6186 void md_write_end(mddev_t *mddev)
6188 if (atomic_dec_and_test(&mddev->writes_pending)) {
6189 if (mddev->safemode == 2)
6190 md_wakeup_thread(mddev->thread);
6191 else if (mddev->safemode_delay)
6192 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
6196 /* md_allow_write(mddev)
6197 * Calling this ensures that the array is marked 'active' so that writes
6198 * may proceed without blocking. It is important to call this before
6199 * attempting a GFP_KERNEL allocation while holding the mddev lock.
6200 * Must be called with mddev_lock held.
6202 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
6203 * is dropped, so return -EAGAIN after notifying userspace.
6205 int md_allow_write(mddev_t *mddev)
6207 if (!mddev->pers)
6208 return 0;
6209 if (mddev->ro)
6210 return 0;
6211 if (!mddev->pers->sync_request)
6212 return 0;
6214 spin_lock_irq(&mddev->write_lock);
6215 if (mddev->in_sync) {
6216 mddev->in_sync = 0;
6217 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6218 if (mddev->safemode_delay &&
6219 mddev->safemode == 0)
6220 mddev->safemode = 1;
6221 spin_unlock_irq(&mddev->write_lock);
6222 md_update_sb(mddev, 0);
6223 sysfs_notify_dirent(mddev->sysfs_state);
6224 } else
6225 spin_unlock_irq(&mddev->write_lock);
6227 if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
6228 return -EAGAIN;
6229 else
6230 return 0;
6232 EXPORT_SYMBOL_GPL(md_allow_write);
6234 #define SYNC_MARKS 10
6235 #define SYNC_MARK_STEP (3*HZ)
6236 void md_do_sync(mddev_t *mddev)
6238 mddev_t *mddev2;
6239 unsigned int currspeed = 0,
6240 window;
6241 sector_t max_sectors,j, io_sectors;
6242 unsigned long mark[SYNC_MARKS];
6243 sector_t mark_cnt[SYNC_MARKS];
6244 int last_mark,m;
6245 struct list_head *tmp;
6246 sector_t last_check;
6247 int skipped = 0;
6248 mdk_rdev_t *rdev;
6249 char *desc;
6251 /* just incase thread restarts... */
6252 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
6253 return;
6254 if (mddev->ro) /* never try to sync a read-only array */
6255 return;
6257 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6258 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
6259 desc = "data-check";
6260 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6261 desc = "requested-resync";
6262 else
6263 desc = "resync";
6264 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
6265 desc = "reshape";
6266 else
6267 desc = "recovery";
6269 /* we overload curr_resync somewhat here.
6270 * 0 == not engaged in resync at all
6271 * 2 == checking that there is no conflict with another sync
6272 * 1 == like 2, but have yielded to allow conflicting resync to
6273 * commense
6274 * other == active in resync - this many blocks
6276 * Before starting a resync we must have set curr_resync to
6277 * 2, and then checked that every "conflicting" array has curr_resync
6278 * less than ours. When we find one that is the same or higher
6279 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
6280 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
6281 * This will mean we have to start checking from the beginning again.
6285 do {
6286 mddev->curr_resync = 2;
6288 try_again:
6289 if (kthread_should_stop()) {
6290 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6291 goto skip;
6293 for_each_mddev(mddev2, tmp) {
6294 if (mddev2 == mddev)
6295 continue;
6296 if (!mddev->parallel_resync
6297 && mddev2->curr_resync
6298 && match_mddev_units(mddev, mddev2)) {
6299 DEFINE_WAIT(wq);
6300 if (mddev < mddev2 && mddev->curr_resync == 2) {
6301 /* arbitrarily yield */
6302 mddev->curr_resync = 1;
6303 wake_up(&resync_wait);
6305 if (mddev > mddev2 && mddev->curr_resync == 1)
6306 /* no need to wait here, we can wait the next
6307 * time 'round when curr_resync == 2
6309 continue;
6310 /* We need to wait 'interruptible' so as not to
6311 * contribute to the load average, and not to
6312 * be caught by 'softlockup'
6314 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
6315 if (!kthread_should_stop() &&
6316 mddev2->curr_resync >= mddev->curr_resync) {
6317 printk(KERN_INFO "md: delaying %s of %s"
6318 " until %s has finished (they"
6319 " share one or more physical units)\n",
6320 desc, mdname(mddev), mdname(mddev2));
6321 mddev_put(mddev2);
6322 if (signal_pending(current))
6323 flush_signals(current);
6324 schedule();
6325 finish_wait(&resync_wait, &wq);
6326 goto try_again;
6328 finish_wait(&resync_wait, &wq);
6331 } while (mddev->curr_resync < 2);
6333 j = 0;
6334 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6335 /* resync follows the size requested by the personality,
6336 * which defaults to physical size, but can be virtual size
6338 max_sectors = mddev->resync_max_sectors;
6339 mddev->resync_mismatches = 0;
6340 /* we don't use the checkpoint if there's a bitmap */
6341 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6342 j = mddev->resync_min;
6343 else if (!mddev->bitmap)
6344 j = mddev->recovery_cp;
6346 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
6347 max_sectors = mddev->dev_sectors;
6348 else {
6349 /* recovery follows the physical size of devices */
6350 max_sectors = mddev->dev_sectors;
6351 j = MaxSector;
6352 list_for_each_entry(rdev, &mddev->disks, same_set)
6353 if (rdev->raid_disk >= 0 &&
6354 !test_bit(Faulty, &rdev->flags) &&
6355 !test_bit(In_sync, &rdev->flags) &&
6356 rdev->recovery_offset < j)
6357 j = rdev->recovery_offset;
6360 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
6361 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
6362 " %d KB/sec/disk.\n", speed_min(mddev));
6363 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
6364 "(but not more than %d KB/sec) for %s.\n",
6365 speed_max(mddev), desc);
6367 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
6369 io_sectors = 0;
6370 for (m = 0; m < SYNC_MARKS; m++) {
6371 mark[m] = jiffies;
6372 mark_cnt[m] = io_sectors;
6374 last_mark = 0;
6375 mddev->resync_mark = mark[last_mark];
6376 mddev->resync_mark_cnt = mark_cnt[last_mark];
6379 * Tune reconstruction:
6381 window = 32*(PAGE_SIZE/512);
6382 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
6383 window/2,(unsigned long long) max_sectors/2);
6385 atomic_set(&mddev->recovery_active, 0);
6386 last_check = 0;
6388 if (j>2) {
6389 printk(KERN_INFO
6390 "md: resuming %s of %s from checkpoint.\n",
6391 desc, mdname(mddev));
6392 mddev->curr_resync = j;
6395 while (j < max_sectors) {
6396 sector_t sectors;
6398 skipped = 0;
6400 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
6401 ((mddev->curr_resync > mddev->curr_resync_completed &&
6402 (mddev->curr_resync - mddev->curr_resync_completed)
6403 > (max_sectors >> 4)) ||
6404 (j - mddev->curr_resync_completed)*2
6405 >= mddev->resync_max - mddev->curr_resync_completed
6406 )) {
6407 /* time to update curr_resync_completed */
6408 blk_unplug(mddev->queue);
6409 wait_event(mddev->recovery_wait,
6410 atomic_read(&mddev->recovery_active) == 0);
6411 mddev->curr_resync_completed =
6412 mddev->curr_resync;
6413 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6414 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
6417 while (j >= mddev->resync_max && !kthread_should_stop()) {
6418 /* As this condition is controlled by user-space,
6419 * we can block indefinitely, so use '_interruptible'
6420 * to avoid triggering warnings.
6422 flush_signals(current); /* just in case */
6423 wait_event_interruptible(mddev->recovery_wait,
6424 mddev->resync_max > j
6425 || kthread_should_stop());
6428 if (kthread_should_stop())
6429 goto interrupted;
6431 sectors = mddev->pers->sync_request(mddev, j, &skipped,
6432 currspeed < speed_min(mddev));
6433 if (sectors == 0) {
6434 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6435 goto out;
6438 if (!skipped) { /* actual IO requested */
6439 io_sectors += sectors;
6440 atomic_add(sectors, &mddev->recovery_active);
6443 j += sectors;
6444 if (j>1) mddev->curr_resync = j;
6445 mddev->curr_mark_cnt = io_sectors;
6446 if (last_check == 0)
6447 /* this is the earliers that rebuilt will be
6448 * visible in /proc/mdstat
6450 md_new_event(mddev);
6452 if (last_check + window > io_sectors || j == max_sectors)
6453 continue;
6455 last_check = io_sectors;
6457 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6458 break;
6460 repeat:
6461 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
6462 /* step marks */
6463 int next = (last_mark+1) % SYNC_MARKS;
6465 mddev->resync_mark = mark[next];
6466 mddev->resync_mark_cnt = mark_cnt[next];
6467 mark[next] = jiffies;
6468 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
6469 last_mark = next;
6473 if (kthread_should_stop())
6474 goto interrupted;
6478 * this loop exits only if either when we are slower than
6479 * the 'hard' speed limit, or the system was IO-idle for
6480 * a jiffy.
6481 * the system might be non-idle CPU-wise, but we only care
6482 * about not overloading the IO subsystem. (things like an
6483 * e2fsck being done on the RAID array should execute fast)
6485 blk_unplug(mddev->queue);
6486 cond_resched();
6488 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
6489 /((jiffies-mddev->resync_mark)/HZ +1) +1;
6491 if (currspeed > speed_min(mddev)) {
6492 if ((currspeed > speed_max(mddev)) ||
6493 !is_mddev_idle(mddev, 0)) {
6494 msleep(500);
6495 goto repeat;
6499 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
6501 * this also signals 'finished resyncing' to md_stop
6503 out:
6504 blk_unplug(mddev->queue);
6506 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
6508 /* tell personality that we are finished */
6509 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
6511 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
6512 mddev->curr_resync > 2) {
6513 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6514 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6515 if (mddev->curr_resync >= mddev->recovery_cp) {
6516 printk(KERN_INFO
6517 "md: checkpointing %s of %s.\n",
6518 desc, mdname(mddev));
6519 mddev->recovery_cp = mddev->curr_resync;
6521 } else
6522 mddev->recovery_cp = MaxSector;
6523 } else {
6524 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6525 mddev->curr_resync = MaxSector;
6526 list_for_each_entry(rdev, &mddev->disks, same_set)
6527 if (rdev->raid_disk >= 0 &&
6528 !test_bit(Faulty, &rdev->flags) &&
6529 !test_bit(In_sync, &rdev->flags) &&
6530 rdev->recovery_offset < mddev->curr_resync)
6531 rdev->recovery_offset = mddev->curr_resync;
6534 set_bit(MD_CHANGE_DEVS, &mddev->flags);
6536 skip:
6537 mddev->curr_resync = 0;
6538 mddev->curr_resync_completed = 0;
6539 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6540 /* We completed so max setting can be forgotten. */
6541 mddev->resync_max = MaxSector;
6542 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
6543 wake_up(&resync_wait);
6544 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
6545 md_wakeup_thread(mddev->thread);
6546 return;
6548 interrupted:
6550 * got a signal, exit.
6552 printk(KERN_INFO
6553 "md: md_do_sync() got signal ... exiting\n");
6554 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6555 goto out;
6558 EXPORT_SYMBOL_GPL(md_do_sync);
6561 static int remove_and_add_spares(mddev_t *mddev)
6563 mdk_rdev_t *rdev;
6564 int spares = 0;
6566 mddev->curr_resync_completed = 0;
6568 list_for_each_entry(rdev, &mddev->disks, same_set)
6569 if (rdev->raid_disk >= 0 &&
6570 !test_bit(Blocked, &rdev->flags) &&
6571 (test_bit(Faulty, &rdev->flags) ||
6572 ! test_bit(In_sync, &rdev->flags)) &&
6573 atomic_read(&rdev->nr_pending)==0) {
6574 if (mddev->pers->hot_remove_disk(
6575 mddev, rdev->raid_disk)==0) {
6576 char nm[20];
6577 sprintf(nm,"rd%d", rdev->raid_disk);
6578 sysfs_remove_link(&mddev->kobj, nm);
6579 rdev->raid_disk = -1;
6583 if (mddev->degraded && ! mddev->ro && !mddev->recovery_disabled) {
6584 list_for_each_entry(rdev, &mddev->disks, same_set) {
6585 if (rdev->raid_disk >= 0 &&
6586 !test_bit(In_sync, &rdev->flags) &&
6587 !test_bit(Blocked, &rdev->flags))
6588 spares++;
6589 if (rdev->raid_disk < 0
6590 && !test_bit(Faulty, &rdev->flags)) {
6591 rdev->recovery_offset = 0;
6592 if (mddev->pers->
6593 hot_add_disk(mddev, rdev) == 0) {
6594 char nm[20];
6595 sprintf(nm, "rd%d", rdev->raid_disk);
6596 if (sysfs_create_link(&mddev->kobj,
6597 &rdev->kobj, nm))
6598 printk(KERN_WARNING
6599 "md: cannot register "
6600 "%s for %s\n",
6601 nm, mdname(mddev));
6602 spares++;
6603 md_new_event(mddev);
6604 } else
6605 break;
6609 return spares;
6612 * This routine is regularly called by all per-raid-array threads to
6613 * deal with generic issues like resync and super-block update.
6614 * Raid personalities that don't have a thread (linear/raid0) do not
6615 * need this as they never do any recovery or update the superblock.
6617 * It does not do any resync itself, but rather "forks" off other threads
6618 * to do that as needed.
6619 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
6620 * "->recovery" and create a thread at ->sync_thread.
6621 * When the thread finishes it sets MD_RECOVERY_DONE
6622 * and wakeups up this thread which will reap the thread and finish up.
6623 * This thread also removes any faulty devices (with nr_pending == 0).
6625 * The overall approach is:
6626 * 1/ if the superblock needs updating, update it.
6627 * 2/ If a recovery thread is running, don't do anything else.
6628 * 3/ If recovery has finished, clean up, possibly marking spares active.
6629 * 4/ If there are any faulty devices, remove them.
6630 * 5/ If array is degraded, try to add spares devices
6631 * 6/ If array has spares or is not in-sync, start a resync thread.
6633 void md_check_recovery(mddev_t *mddev)
6635 mdk_rdev_t *rdev;
6638 if (mddev->bitmap)
6639 bitmap_daemon_work(mddev);
6641 if (mddev->ro)
6642 return;
6644 if (signal_pending(current)) {
6645 if (mddev->pers->sync_request && !mddev->external) {
6646 printk(KERN_INFO "md: %s in immediate safe mode\n",
6647 mdname(mddev));
6648 mddev->safemode = 2;
6650 flush_signals(current);
6653 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
6654 return;
6655 if ( ! (
6656 (mddev->flags && !mddev->external) ||
6657 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
6658 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
6659 (mddev->external == 0 && mddev->safemode == 1) ||
6660 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
6661 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
6663 return;
6665 if (mddev_trylock(mddev)) {
6666 int spares = 0;
6668 if (mddev->ro) {
6669 /* Only thing we do on a ro array is remove
6670 * failed devices.
6672 remove_and_add_spares(mddev);
6673 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6674 goto unlock;
6677 if (!mddev->external) {
6678 int did_change = 0;
6679 spin_lock_irq(&mddev->write_lock);
6680 if (mddev->safemode &&
6681 !atomic_read(&mddev->writes_pending) &&
6682 !mddev->in_sync &&
6683 mddev->recovery_cp == MaxSector) {
6684 mddev->in_sync = 1;
6685 did_change = 1;
6686 if (mddev->persistent)
6687 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6689 if (mddev->safemode == 1)
6690 mddev->safemode = 0;
6691 spin_unlock_irq(&mddev->write_lock);
6692 if (did_change)
6693 sysfs_notify_dirent(mddev->sysfs_state);
6696 if (mddev->flags)
6697 md_update_sb(mddev, 0);
6699 list_for_each_entry(rdev, &mddev->disks, same_set)
6700 if (test_and_clear_bit(StateChanged, &rdev->flags))
6701 sysfs_notify_dirent(rdev->sysfs_state);
6704 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
6705 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
6706 /* resync/recovery still happening */
6707 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6708 goto unlock;
6710 if (mddev->sync_thread) {
6711 /* resync has finished, collect result */
6712 md_unregister_thread(mddev->sync_thread);
6713 mddev->sync_thread = NULL;
6714 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
6715 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
6716 /* success...*/
6717 /* activate any spares */
6718 if (mddev->pers->spare_active(mddev))
6719 sysfs_notify(&mddev->kobj, NULL,
6720 "degraded");
6722 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
6723 mddev->pers->finish_reshape)
6724 mddev->pers->finish_reshape(mddev);
6725 md_update_sb(mddev, 1);
6727 /* if array is no-longer degraded, then any saved_raid_disk
6728 * information must be scrapped
6730 if (!mddev->degraded)
6731 list_for_each_entry(rdev, &mddev->disks, same_set)
6732 rdev->saved_raid_disk = -1;
6734 mddev->recovery = 0;
6735 /* flag recovery needed just to double check */
6736 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6737 sysfs_notify_dirent(mddev->sysfs_action);
6738 md_new_event(mddev);
6739 goto unlock;
6741 /* Set RUNNING before clearing NEEDED to avoid
6742 * any transients in the value of "sync_action".
6744 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6745 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6746 /* Clear some bits that don't mean anything, but
6747 * might be left set
6749 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
6750 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
6752 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
6753 goto unlock;
6754 /* no recovery is running.
6755 * remove any failed drives, then
6756 * add spares if possible.
6757 * Spare are also removed and re-added, to allow
6758 * the personality to fail the re-add.
6761 if (mddev->reshape_position != MaxSector) {
6762 if (mddev->pers->check_reshape == NULL ||
6763 mddev->pers->check_reshape(mddev) != 0)
6764 /* Cannot proceed */
6765 goto unlock;
6766 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
6767 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
6768 } else if ((spares = remove_and_add_spares(mddev))) {
6769 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
6770 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
6771 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
6772 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
6773 } else if (mddev->recovery_cp < MaxSector) {
6774 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
6775 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
6776 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
6777 /* nothing to be done ... */
6778 goto unlock;
6780 if (mddev->pers->sync_request) {
6781 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
6782 /* We are adding a device or devices to an array
6783 * which has the bitmap stored on all devices.
6784 * So make sure all bitmap pages get written
6786 bitmap_write_all(mddev->bitmap);
6788 mddev->sync_thread = md_register_thread(md_do_sync,
6789 mddev,
6790 "resync");
6791 if (!mddev->sync_thread) {
6792 printk(KERN_ERR "%s: could not start resync"
6793 " thread...\n",
6794 mdname(mddev));
6795 /* leave the spares where they are, it shouldn't hurt */
6796 mddev->recovery = 0;
6797 } else
6798 md_wakeup_thread(mddev->sync_thread);
6799 sysfs_notify_dirent(mddev->sysfs_action);
6800 md_new_event(mddev);
6802 unlock:
6803 if (!mddev->sync_thread) {
6804 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6805 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
6806 &mddev->recovery))
6807 if (mddev->sysfs_action)
6808 sysfs_notify_dirent(mddev->sysfs_action);
6810 mddev_unlock(mddev);
6814 void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
6816 sysfs_notify_dirent(rdev->sysfs_state);
6817 wait_event_timeout(rdev->blocked_wait,
6818 !test_bit(Blocked, &rdev->flags),
6819 msecs_to_jiffies(5000));
6820 rdev_dec_pending(rdev, mddev);
6822 EXPORT_SYMBOL(md_wait_for_blocked_rdev);
6824 static int md_notify_reboot(struct notifier_block *this,
6825 unsigned long code, void *x)
6827 struct list_head *tmp;
6828 mddev_t *mddev;
6830 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
6832 printk(KERN_INFO "md: stopping all md devices.\n");
6834 for_each_mddev(mddev, tmp)
6835 if (mddev_trylock(mddev)) {
6836 /* Force a switch to readonly even array
6837 * appears to still be in use. Hence
6838 * the '100'.
6840 do_md_stop(mddev, 1, 100);
6841 mddev_unlock(mddev);
6844 * certain more exotic SCSI devices are known to be
6845 * volatile wrt too early system reboots. While the
6846 * right place to handle this issue is the given
6847 * driver, we do want to have a safe RAID driver ...
6849 mdelay(1000*1);
6851 return NOTIFY_DONE;
6854 static struct notifier_block md_notifier = {
6855 .notifier_call = md_notify_reboot,
6856 .next = NULL,
6857 .priority = INT_MAX, /* before any real devices */
6860 static void md_geninit(void)
6862 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
6864 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
6867 static int __init md_init(void)
6869 if (register_blkdev(MD_MAJOR, "md"))
6870 return -1;
6871 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
6872 unregister_blkdev(MD_MAJOR, "md");
6873 return -1;
6875 blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE,
6876 md_probe, NULL, NULL);
6877 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
6878 md_probe, NULL, NULL);
6880 register_reboot_notifier(&md_notifier);
6881 raid_table_header = register_sysctl_table(raid_root_table);
6883 md_geninit();
6884 return 0;
6888 #ifndef MODULE
6891 * Searches all registered partitions for autorun RAID arrays
6892 * at boot time.
6895 static LIST_HEAD(all_detected_devices);
6896 struct detected_devices_node {
6897 struct list_head list;
6898 dev_t dev;
6901 void md_autodetect_dev(dev_t dev)
6903 struct detected_devices_node *node_detected_dev;
6905 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
6906 if (node_detected_dev) {
6907 node_detected_dev->dev = dev;
6908 list_add_tail(&node_detected_dev->list, &all_detected_devices);
6909 } else {
6910 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
6911 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
6916 static void autostart_arrays(int part)
6918 mdk_rdev_t *rdev;
6919 struct detected_devices_node *node_detected_dev;
6920 dev_t dev;
6921 int i_scanned, i_passed;
6923 i_scanned = 0;
6924 i_passed = 0;
6926 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
6928 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
6929 i_scanned++;
6930 node_detected_dev = list_entry(all_detected_devices.next,
6931 struct detected_devices_node, list);
6932 list_del(&node_detected_dev->list);
6933 dev = node_detected_dev->dev;
6934 kfree(node_detected_dev);
6935 rdev = md_import_device(dev,0, 90);
6936 if (IS_ERR(rdev))
6937 continue;
6939 if (test_bit(Faulty, &rdev->flags)) {
6940 MD_BUG();
6941 continue;
6943 set_bit(AutoDetected, &rdev->flags);
6944 list_add(&rdev->same_set, &pending_raid_disks);
6945 i_passed++;
6948 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
6949 i_scanned, i_passed);
6951 autorun_devices(part);
6954 #endif /* !MODULE */
6956 static __exit void md_exit(void)
6958 mddev_t *mddev;
6959 struct list_head *tmp;
6961 blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS);
6962 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
6964 unregister_blkdev(MD_MAJOR,"md");
6965 unregister_blkdev(mdp_major, "mdp");
6966 unregister_reboot_notifier(&md_notifier);
6967 unregister_sysctl_table(raid_table_header);
6968 remove_proc_entry("mdstat", NULL);
6969 for_each_mddev(mddev, tmp) {
6970 export_array(mddev);
6971 mddev->hold_active = 0;
6975 subsys_initcall(md_init);
6976 module_exit(md_exit)
6978 static int get_ro(char *buffer, struct kernel_param *kp)
6980 return sprintf(buffer, "%d", start_readonly);
6982 static int set_ro(const char *val, struct kernel_param *kp)
6984 char *e;
6985 int num = simple_strtoul(val, &e, 10);
6986 if (*val && (*e == '\0' || *e == '\n')) {
6987 start_readonly = num;
6988 return 0;
6990 return -EINVAL;
6993 module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
6994 module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
6996 module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
6998 EXPORT_SYMBOL(register_md_personality);
6999 EXPORT_SYMBOL(unregister_md_personality);
7000 EXPORT_SYMBOL(md_error);
7001 EXPORT_SYMBOL(md_done_sync);
7002 EXPORT_SYMBOL(md_write_start);
7003 EXPORT_SYMBOL(md_write_end);
7004 EXPORT_SYMBOL(md_register_thread);
7005 EXPORT_SYMBOL(md_unregister_thread);
7006 EXPORT_SYMBOL(md_wakeup_thread);
7007 EXPORT_SYMBOL(md_check_recovery);
7008 MODULE_LICENSE("GPL");
7009 MODULE_ALIAS("md");
7010 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);