sfc: Don't use enums as a bitmask.
[zen-stable.git] / drivers / md / md.c
blobb12b3776c0c0c8ca46b998cac6df78845363f7ee
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/mutex.h>
40 #include <linux/buffer_head.h> /* for invalidate_bdev */
41 #include <linux/poll.h>
42 #include <linux/ctype.h>
43 #include <linux/string.h>
44 #include <linux/hdreg.h>
45 #include <linux/proc_fs.h>
46 #include <linux/random.h>
47 #include <linux/reboot.h>
48 #include <linux/file.h>
49 #include <linux/compat.h>
50 #include <linux/delay.h>
51 #include <linux/raid/md_p.h>
52 #include <linux/raid/md_u.h>
53 #include <linux/slab.h>
54 #include "md.h"
55 #include "bitmap.h"
57 #define DEBUG 0
58 #define dprintk(x...) ((void)(DEBUG && printk(x)))
60 #ifndef MODULE
61 static void autostart_arrays(int part);
62 #endif
64 static LIST_HEAD(pers_list);
65 static DEFINE_SPINLOCK(pers_lock);
67 static void md_print_devices(void);
69 static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
70 static struct workqueue_struct *md_wq;
71 static struct workqueue_struct *md_misc_wq;
73 #define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
76 * Default number of read corrections we'll attempt on an rdev
77 * before ejecting it from the array. We divide the read error
78 * count by 2 for every hour elapsed between read errors.
80 #define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
82 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
83 * is 1000 KB/sec, so the extra system load does not show up that much.
84 * Increase it if you want to have more _guaranteed_ speed. Note that
85 * the RAID driver will use the maximum available bandwidth if the IO
86 * subsystem is idle. There is also an 'absolute maximum' reconstruction
87 * speed limit - in case reconstruction slows down your system despite
88 * idle IO detection.
90 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
91 * or /sys/block/mdX/md/sync_speed_{min,max}
94 static int sysctl_speed_limit_min = 1000;
95 static int sysctl_speed_limit_max = 200000;
96 static inline int speed_min(mddev_t *mddev)
98 return mddev->sync_speed_min ?
99 mddev->sync_speed_min : sysctl_speed_limit_min;
102 static inline int speed_max(mddev_t *mddev)
104 return mddev->sync_speed_max ?
105 mddev->sync_speed_max : sysctl_speed_limit_max;
108 static struct ctl_table_header *raid_table_header;
110 static ctl_table raid_table[] = {
112 .procname = "speed_limit_min",
113 .data = &sysctl_speed_limit_min,
114 .maxlen = sizeof(int),
115 .mode = S_IRUGO|S_IWUSR,
116 .proc_handler = proc_dointvec,
119 .procname = "speed_limit_max",
120 .data = &sysctl_speed_limit_max,
121 .maxlen = sizeof(int),
122 .mode = S_IRUGO|S_IWUSR,
123 .proc_handler = proc_dointvec,
128 static ctl_table raid_dir_table[] = {
130 .procname = "raid",
131 .maxlen = 0,
132 .mode = S_IRUGO|S_IXUGO,
133 .child = raid_table,
138 static ctl_table raid_root_table[] = {
140 .procname = "dev",
141 .maxlen = 0,
142 .mode = 0555,
143 .child = raid_dir_table,
148 static const struct block_device_operations md_fops;
150 static int start_readonly;
152 /* bio_clone_mddev
153 * like bio_clone, but with a local bio set
156 static void mddev_bio_destructor(struct bio *bio)
158 mddev_t *mddev, **mddevp;
160 mddevp = (void*)bio;
161 mddev = mddevp[-1];
163 bio_free(bio, mddev->bio_set);
166 struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs,
167 mddev_t *mddev)
169 struct bio *b;
170 mddev_t **mddevp;
172 if (!mddev || !mddev->bio_set)
173 return bio_alloc(gfp_mask, nr_iovecs);
175 b = bio_alloc_bioset(gfp_mask, nr_iovecs,
176 mddev->bio_set);
177 if (!b)
178 return NULL;
179 mddevp = (void*)b;
180 mddevp[-1] = mddev;
181 b->bi_destructor = mddev_bio_destructor;
182 return b;
184 EXPORT_SYMBOL_GPL(bio_alloc_mddev);
186 struct bio *bio_clone_mddev(struct bio *bio, gfp_t gfp_mask,
187 mddev_t *mddev)
189 struct bio *b;
190 mddev_t **mddevp;
192 if (!mddev || !mddev->bio_set)
193 return bio_clone(bio, gfp_mask);
195 b = bio_alloc_bioset(gfp_mask, bio->bi_max_vecs,
196 mddev->bio_set);
197 if (!b)
198 return NULL;
199 mddevp = (void*)b;
200 mddevp[-1] = mddev;
201 b->bi_destructor = mddev_bio_destructor;
202 __bio_clone(b, bio);
203 if (bio_integrity(bio)) {
204 int ret;
206 ret = bio_integrity_clone(b, bio, gfp_mask, mddev->bio_set);
208 if (ret < 0) {
209 bio_put(b);
210 return NULL;
214 return b;
216 EXPORT_SYMBOL_GPL(bio_clone_mddev);
219 * We have a system wide 'event count' that is incremented
220 * on any 'interesting' event, and readers of /proc/mdstat
221 * can use 'poll' or 'select' to find out when the event
222 * count increases.
224 * Events are:
225 * start array, stop array, error, add device, remove device,
226 * start build, activate spare
228 static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
229 static atomic_t md_event_count;
230 void md_new_event(mddev_t *mddev)
232 atomic_inc(&md_event_count);
233 wake_up(&md_event_waiters);
235 EXPORT_SYMBOL_GPL(md_new_event);
237 /* Alternate version that can be called from interrupts
238 * when calling sysfs_notify isn't needed.
240 static void md_new_event_inintr(mddev_t *mddev)
242 atomic_inc(&md_event_count);
243 wake_up(&md_event_waiters);
247 * Enables to iterate over all existing md arrays
248 * all_mddevs_lock protects this list.
250 static LIST_HEAD(all_mddevs);
251 static DEFINE_SPINLOCK(all_mddevs_lock);
255 * iterates through all used mddevs in the system.
256 * We take care to grab the all_mddevs_lock whenever navigating
257 * the list, and to always hold a refcount when unlocked.
258 * Any code which breaks out of this loop while own
259 * a reference to the current mddev and must mddev_put it.
261 #define for_each_mddev(mddev,tmp) \
263 for (({ spin_lock(&all_mddevs_lock); \
264 tmp = all_mddevs.next; \
265 mddev = NULL;}); \
266 ({ if (tmp != &all_mddevs) \
267 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
268 spin_unlock(&all_mddevs_lock); \
269 if (mddev) mddev_put(mddev); \
270 mddev = list_entry(tmp, mddev_t, all_mddevs); \
271 tmp != &all_mddevs;}); \
272 ({ spin_lock(&all_mddevs_lock); \
273 tmp = tmp->next;}) \
277 /* Rather than calling directly into the personality make_request function,
278 * IO requests come here first so that we can check if the device is
279 * being suspended pending a reconfiguration.
280 * We hold a refcount over the call to ->make_request. By the time that
281 * call has finished, the bio has been linked into some internal structure
282 * and so is visible to ->quiesce(), so we don't need the refcount any more.
284 static int md_make_request(struct request_queue *q, struct bio *bio)
286 const int rw = bio_data_dir(bio);
287 mddev_t *mddev = q->queuedata;
288 int rv;
289 int cpu;
290 unsigned int sectors;
292 if (mddev == NULL || mddev->pers == NULL
293 || !mddev->ready) {
294 bio_io_error(bio);
295 return 0;
297 smp_rmb(); /* Ensure implications of 'active' are visible */
298 rcu_read_lock();
299 if (mddev->suspended) {
300 DEFINE_WAIT(__wait);
301 for (;;) {
302 prepare_to_wait(&mddev->sb_wait, &__wait,
303 TASK_UNINTERRUPTIBLE);
304 if (!mddev->suspended)
305 break;
306 rcu_read_unlock();
307 schedule();
308 rcu_read_lock();
310 finish_wait(&mddev->sb_wait, &__wait);
312 atomic_inc(&mddev->active_io);
313 rcu_read_unlock();
316 * save the sectors now since our bio can
317 * go away inside make_request
319 sectors = bio_sectors(bio);
320 rv = mddev->pers->make_request(mddev, bio);
322 cpu = part_stat_lock();
323 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
324 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw], sectors);
325 part_stat_unlock();
327 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
328 wake_up(&mddev->sb_wait);
330 return rv;
333 /* mddev_suspend makes sure no new requests are submitted
334 * to the device, and that any requests that have been submitted
335 * are completely handled.
336 * Once ->stop is called and completes, the module will be completely
337 * unused.
339 void mddev_suspend(mddev_t *mddev)
341 BUG_ON(mddev->suspended);
342 mddev->suspended = 1;
343 synchronize_rcu();
344 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
345 mddev->pers->quiesce(mddev, 1);
347 EXPORT_SYMBOL_GPL(mddev_suspend);
349 void mddev_resume(mddev_t *mddev)
351 mddev->suspended = 0;
352 wake_up(&mddev->sb_wait);
353 mddev->pers->quiesce(mddev, 0);
355 EXPORT_SYMBOL_GPL(mddev_resume);
357 int mddev_congested(mddev_t *mddev, int bits)
359 return mddev->suspended;
361 EXPORT_SYMBOL(mddev_congested);
364 * Generic flush handling for md
367 static void md_end_flush(struct bio *bio, int err)
369 mdk_rdev_t *rdev = bio->bi_private;
370 mddev_t *mddev = rdev->mddev;
372 rdev_dec_pending(rdev, mddev);
374 if (atomic_dec_and_test(&mddev->flush_pending)) {
375 /* The pre-request flush has finished */
376 queue_work(md_wq, &mddev->flush_work);
378 bio_put(bio);
381 static void md_submit_flush_data(struct work_struct *ws);
383 static void submit_flushes(struct work_struct *ws)
385 mddev_t *mddev = container_of(ws, mddev_t, flush_work);
386 mdk_rdev_t *rdev;
388 INIT_WORK(&mddev->flush_work, md_submit_flush_data);
389 atomic_set(&mddev->flush_pending, 1);
390 rcu_read_lock();
391 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
392 if (rdev->raid_disk >= 0 &&
393 !test_bit(Faulty, &rdev->flags)) {
394 /* Take two references, one is dropped
395 * when request finishes, one after
396 * we reclaim rcu_read_lock
398 struct bio *bi;
399 atomic_inc(&rdev->nr_pending);
400 atomic_inc(&rdev->nr_pending);
401 rcu_read_unlock();
402 bi = bio_alloc_mddev(GFP_KERNEL, 0, mddev);
403 bi->bi_end_io = md_end_flush;
404 bi->bi_private = rdev;
405 bi->bi_bdev = rdev->bdev;
406 atomic_inc(&mddev->flush_pending);
407 submit_bio(WRITE_FLUSH, bi);
408 rcu_read_lock();
409 rdev_dec_pending(rdev, mddev);
411 rcu_read_unlock();
412 if (atomic_dec_and_test(&mddev->flush_pending))
413 queue_work(md_wq, &mddev->flush_work);
416 static void md_submit_flush_data(struct work_struct *ws)
418 mddev_t *mddev = container_of(ws, mddev_t, flush_work);
419 struct bio *bio = mddev->flush_bio;
421 if (bio->bi_size == 0)
422 /* an empty barrier - all done */
423 bio_endio(bio, 0);
424 else {
425 bio->bi_rw &= ~REQ_FLUSH;
426 if (mddev->pers->make_request(mddev, bio))
427 generic_make_request(bio);
430 mddev->flush_bio = NULL;
431 wake_up(&mddev->sb_wait);
434 void md_flush_request(mddev_t *mddev, struct bio *bio)
436 spin_lock_irq(&mddev->write_lock);
437 wait_event_lock_irq(mddev->sb_wait,
438 !mddev->flush_bio,
439 mddev->write_lock, /*nothing*/);
440 mddev->flush_bio = bio;
441 spin_unlock_irq(&mddev->write_lock);
443 INIT_WORK(&mddev->flush_work, submit_flushes);
444 queue_work(md_wq, &mddev->flush_work);
446 EXPORT_SYMBOL(md_flush_request);
448 /* Support for plugging.
449 * This mirrors the plugging support in request_queue, but does not
450 * require having a whole queue
452 static void plugger_work(struct work_struct *work)
454 struct plug_handle *plug =
455 container_of(work, struct plug_handle, unplug_work);
456 plug->unplug_fn(plug);
458 static void plugger_timeout(unsigned long data)
460 struct plug_handle *plug = (void *)data;
461 kblockd_schedule_work(NULL, &plug->unplug_work);
463 void plugger_init(struct plug_handle *plug,
464 void (*unplug_fn)(struct plug_handle *))
466 plug->unplug_flag = 0;
467 plug->unplug_fn = unplug_fn;
468 init_timer(&plug->unplug_timer);
469 plug->unplug_timer.function = plugger_timeout;
470 plug->unplug_timer.data = (unsigned long)plug;
471 INIT_WORK(&plug->unplug_work, plugger_work);
473 EXPORT_SYMBOL_GPL(plugger_init);
475 void plugger_set_plug(struct plug_handle *plug)
477 if (!test_and_set_bit(PLUGGED_FLAG, &plug->unplug_flag))
478 mod_timer(&plug->unplug_timer, jiffies + msecs_to_jiffies(3)+1);
480 EXPORT_SYMBOL_GPL(plugger_set_plug);
482 int plugger_remove_plug(struct plug_handle *plug)
484 if (test_and_clear_bit(PLUGGED_FLAG, &plug->unplug_flag)) {
485 del_timer(&plug->unplug_timer);
486 return 1;
487 } else
488 return 0;
490 EXPORT_SYMBOL_GPL(plugger_remove_plug);
493 static inline mddev_t *mddev_get(mddev_t *mddev)
495 atomic_inc(&mddev->active);
496 return mddev;
499 static void mddev_delayed_delete(struct work_struct *ws);
501 static void mddev_put(mddev_t *mddev)
503 struct bio_set *bs = NULL;
505 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
506 return;
507 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
508 mddev->ctime == 0 && !mddev->hold_active) {
509 /* Array is not configured at all, and not held active,
510 * so destroy it */
511 list_del(&mddev->all_mddevs);
512 bs = mddev->bio_set;
513 mddev->bio_set = NULL;
514 if (mddev->gendisk) {
515 /* We did a probe so need to clean up. Call
516 * queue_work inside the spinlock so that
517 * flush_workqueue() after mddev_find will
518 * succeed in waiting for the work to be done.
520 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
521 queue_work(md_misc_wq, &mddev->del_work);
522 } else
523 kfree(mddev);
525 spin_unlock(&all_mddevs_lock);
526 if (bs)
527 bioset_free(bs);
530 void mddev_init(mddev_t *mddev)
532 mutex_init(&mddev->open_mutex);
533 mutex_init(&mddev->reconfig_mutex);
534 mutex_init(&mddev->bitmap_info.mutex);
535 INIT_LIST_HEAD(&mddev->disks);
536 INIT_LIST_HEAD(&mddev->all_mddevs);
537 init_timer(&mddev->safemode_timer);
538 atomic_set(&mddev->active, 1);
539 atomic_set(&mddev->openers, 0);
540 atomic_set(&mddev->active_io, 0);
541 spin_lock_init(&mddev->write_lock);
542 atomic_set(&mddev->flush_pending, 0);
543 init_waitqueue_head(&mddev->sb_wait);
544 init_waitqueue_head(&mddev->recovery_wait);
545 mddev->reshape_position = MaxSector;
546 mddev->resync_min = 0;
547 mddev->resync_max = MaxSector;
548 mddev->level = LEVEL_NONE;
550 EXPORT_SYMBOL_GPL(mddev_init);
552 static mddev_t * mddev_find(dev_t unit)
554 mddev_t *mddev, *new = NULL;
556 if (unit && MAJOR(unit) != MD_MAJOR)
557 unit &= ~((1<<MdpMinorShift)-1);
559 retry:
560 spin_lock(&all_mddevs_lock);
562 if (unit) {
563 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
564 if (mddev->unit == unit) {
565 mddev_get(mddev);
566 spin_unlock(&all_mddevs_lock);
567 kfree(new);
568 return mddev;
571 if (new) {
572 list_add(&new->all_mddevs, &all_mddevs);
573 spin_unlock(&all_mddevs_lock);
574 new->hold_active = UNTIL_IOCTL;
575 return new;
577 } else if (new) {
578 /* find an unused unit number */
579 static int next_minor = 512;
580 int start = next_minor;
581 int is_free = 0;
582 int dev = 0;
583 while (!is_free) {
584 dev = MKDEV(MD_MAJOR, next_minor);
585 next_minor++;
586 if (next_minor > MINORMASK)
587 next_minor = 0;
588 if (next_minor == start) {
589 /* Oh dear, all in use. */
590 spin_unlock(&all_mddevs_lock);
591 kfree(new);
592 return NULL;
595 is_free = 1;
596 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
597 if (mddev->unit == dev) {
598 is_free = 0;
599 break;
602 new->unit = dev;
603 new->md_minor = MINOR(dev);
604 new->hold_active = UNTIL_STOP;
605 list_add(&new->all_mddevs, &all_mddevs);
606 spin_unlock(&all_mddevs_lock);
607 return new;
609 spin_unlock(&all_mddevs_lock);
611 new = kzalloc(sizeof(*new), GFP_KERNEL);
612 if (!new)
613 return NULL;
615 new->unit = unit;
616 if (MAJOR(unit) == MD_MAJOR)
617 new->md_minor = MINOR(unit);
618 else
619 new->md_minor = MINOR(unit) >> MdpMinorShift;
621 mddev_init(new);
623 goto retry;
626 static inline int mddev_lock(mddev_t * mddev)
628 return mutex_lock_interruptible(&mddev->reconfig_mutex);
631 static inline int mddev_is_locked(mddev_t *mddev)
633 return mutex_is_locked(&mddev->reconfig_mutex);
636 static inline int mddev_trylock(mddev_t * mddev)
638 return mutex_trylock(&mddev->reconfig_mutex);
641 static struct attribute_group md_redundancy_group;
643 static void mddev_unlock(mddev_t * mddev)
645 if (mddev->to_remove) {
646 /* These cannot be removed under reconfig_mutex as
647 * an access to the files will try to take reconfig_mutex
648 * while holding the file unremovable, which leads to
649 * a deadlock.
650 * So hold set sysfs_active while the remove in happeing,
651 * and anything else which might set ->to_remove or my
652 * otherwise change the sysfs namespace will fail with
653 * -EBUSY if sysfs_active is still set.
654 * We set sysfs_active under reconfig_mutex and elsewhere
655 * test it under the same mutex to ensure its correct value
656 * is seen.
658 struct attribute_group *to_remove = mddev->to_remove;
659 mddev->to_remove = NULL;
660 mddev->sysfs_active = 1;
661 mutex_unlock(&mddev->reconfig_mutex);
663 if (mddev->kobj.sd) {
664 if (to_remove != &md_redundancy_group)
665 sysfs_remove_group(&mddev->kobj, to_remove);
666 if (mddev->pers == NULL ||
667 mddev->pers->sync_request == NULL) {
668 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
669 if (mddev->sysfs_action)
670 sysfs_put(mddev->sysfs_action);
671 mddev->sysfs_action = NULL;
674 mddev->sysfs_active = 0;
675 } else
676 mutex_unlock(&mddev->reconfig_mutex);
678 md_wakeup_thread(mddev->thread);
681 static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
683 mdk_rdev_t *rdev;
685 list_for_each_entry(rdev, &mddev->disks, same_set)
686 if (rdev->desc_nr == nr)
687 return rdev;
689 return NULL;
692 static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
694 mdk_rdev_t *rdev;
696 list_for_each_entry(rdev, &mddev->disks, same_set)
697 if (rdev->bdev->bd_dev == dev)
698 return rdev;
700 return NULL;
703 static struct mdk_personality *find_pers(int level, char *clevel)
705 struct mdk_personality *pers;
706 list_for_each_entry(pers, &pers_list, list) {
707 if (level != LEVEL_NONE && pers->level == level)
708 return pers;
709 if (strcmp(pers->name, clevel)==0)
710 return pers;
712 return NULL;
715 /* return the offset of the super block in 512byte sectors */
716 static inline sector_t calc_dev_sboffset(mdk_rdev_t *rdev)
718 sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512;
719 return MD_NEW_SIZE_SECTORS(num_sectors);
722 static int alloc_disk_sb(mdk_rdev_t * rdev)
724 if (rdev->sb_page)
725 MD_BUG();
727 rdev->sb_page = alloc_page(GFP_KERNEL);
728 if (!rdev->sb_page) {
729 printk(KERN_ALERT "md: out of memory.\n");
730 return -ENOMEM;
733 return 0;
736 static void free_disk_sb(mdk_rdev_t * rdev)
738 if (rdev->sb_page) {
739 put_page(rdev->sb_page);
740 rdev->sb_loaded = 0;
741 rdev->sb_page = NULL;
742 rdev->sb_start = 0;
743 rdev->sectors = 0;
748 static void super_written(struct bio *bio, int error)
750 mdk_rdev_t *rdev = bio->bi_private;
751 mddev_t *mddev = rdev->mddev;
753 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
754 printk("md: super_written gets error=%d, uptodate=%d\n",
755 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
756 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
757 md_error(mddev, rdev);
760 if (atomic_dec_and_test(&mddev->pending_writes))
761 wake_up(&mddev->sb_wait);
762 bio_put(bio);
765 void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
766 sector_t sector, int size, struct page *page)
768 /* write first size bytes of page to sector of rdev
769 * Increment mddev->pending_writes before returning
770 * and decrement it on completion, waking up sb_wait
771 * if zero is reached.
772 * If an error occurred, call md_error
774 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev);
776 bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev;
777 bio->bi_sector = sector;
778 bio_add_page(bio, page, size, 0);
779 bio->bi_private = rdev;
780 bio->bi_end_io = super_written;
782 atomic_inc(&mddev->pending_writes);
783 submit_bio(REQ_WRITE | REQ_SYNC | REQ_FLUSH | REQ_FUA, bio);
786 void md_super_wait(mddev_t *mddev)
788 /* wait for all superblock writes that were scheduled to complete */
789 DEFINE_WAIT(wq);
790 for(;;) {
791 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
792 if (atomic_read(&mddev->pending_writes)==0)
793 break;
794 schedule();
796 finish_wait(&mddev->sb_wait, &wq);
799 static void bi_complete(struct bio *bio, int error)
801 complete((struct completion*)bio->bi_private);
804 int sync_page_io(mdk_rdev_t *rdev, sector_t sector, int size,
805 struct page *page, int rw, bool metadata_op)
807 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev);
808 struct completion event;
809 int ret;
811 rw |= REQ_SYNC;
813 bio->bi_bdev = (metadata_op && rdev->meta_bdev) ?
814 rdev->meta_bdev : rdev->bdev;
815 if (metadata_op)
816 bio->bi_sector = sector + rdev->sb_start;
817 else
818 bio->bi_sector = sector + rdev->data_offset;
819 bio_add_page(bio, page, size, 0);
820 init_completion(&event);
821 bio->bi_private = &event;
822 bio->bi_end_io = bi_complete;
823 submit_bio(rw, bio);
824 wait_for_completion(&event);
826 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
827 bio_put(bio);
828 return ret;
830 EXPORT_SYMBOL_GPL(sync_page_io);
832 static int read_disk_sb(mdk_rdev_t * rdev, int size)
834 char b[BDEVNAME_SIZE];
835 if (!rdev->sb_page) {
836 MD_BUG();
837 return -EINVAL;
839 if (rdev->sb_loaded)
840 return 0;
843 if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true))
844 goto fail;
845 rdev->sb_loaded = 1;
846 return 0;
848 fail:
849 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
850 bdevname(rdev->bdev,b));
851 return -EINVAL;
854 static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
856 return sb1->set_uuid0 == sb2->set_uuid0 &&
857 sb1->set_uuid1 == sb2->set_uuid1 &&
858 sb1->set_uuid2 == sb2->set_uuid2 &&
859 sb1->set_uuid3 == sb2->set_uuid3;
862 static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
864 int ret;
865 mdp_super_t *tmp1, *tmp2;
867 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
868 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
870 if (!tmp1 || !tmp2) {
871 ret = 0;
872 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
873 goto abort;
876 *tmp1 = *sb1;
877 *tmp2 = *sb2;
880 * nr_disks is not constant
882 tmp1->nr_disks = 0;
883 tmp2->nr_disks = 0;
885 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
886 abort:
887 kfree(tmp1);
888 kfree(tmp2);
889 return ret;
893 static u32 md_csum_fold(u32 csum)
895 csum = (csum & 0xffff) + (csum >> 16);
896 return (csum & 0xffff) + (csum >> 16);
899 static unsigned int calc_sb_csum(mdp_super_t * sb)
901 u64 newcsum = 0;
902 u32 *sb32 = (u32*)sb;
903 int i;
904 unsigned int disk_csum, csum;
906 disk_csum = sb->sb_csum;
907 sb->sb_csum = 0;
909 for (i = 0; i < MD_SB_BYTES/4 ; i++)
910 newcsum += sb32[i];
911 csum = (newcsum & 0xffffffff) + (newcsum>>32);
914 #ifdef CONFIG_ALPHA
915 /* This used to use csum_partial, which was wrong for several
916 * reasons including that different results are returned on
917 * different architectures. It isn't critical that we get exactly
918 * the same return value as before (we always csum_fold before
919 * testing, and that removes any differences). However as we
920 * know that csum_partial always returned a 16bit value on
921 * alphas, do a fold to maximise conformity to previous behaviour.
923 sb->sb_csum = md_csum_fold(disk_csum);
924 #else
925 sb->sb_csum = disk_csum;
926 #endif
927 return csum;
932 * Handle superblock details.
933 * We want to be able to handle multiple superblock formats
934 * so we have a common interface to them all, and an array of
935 * different handlers.
936 * We rely on user-space to write the initial superblock, and support
937 * reading and updating of superblocks.
938 * Interface methods are:
939 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
940 * loads and validates a superblock on dev.
941 * if refdev != NULL, compare superblocks on both devices
942 * Return:
943 * 0 - dev has a superblock that is compatible with refdev
944 * 1 - dev has a superblock that is compatible and newer than refdev
945 * so dev should be used as the refdev in future
946 * -EINVAL superblock incompatible or invalid
947 * -othererror e.g. -EIO
949 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
950 * Verify that dev is acceptable into mddev.
951 * The first time, mddev->raid_disks will be 0, and data from
952 * dev should be merged in. Subsequent calls check that dev
953 * is new enough. Return 0 or -EINVAL
955 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
956 * Update the superblock for rdev with data in mddev
957 * This does not write to disc.
961 struct super_type {
962 char *name;
963 struct module *owner;
964 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev,
965 int minor_version);
966 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
967 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
968 unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev,
969 sector_t num_sectors);
973 * Check that the given mddev has no bitmap.
975 * This function is called from the run method of all personalities that do not
976 * support bitmaps. It prints an error message and returns non-zero if mddev
977 * has a bitmap. Otherwise, it returns 0.
980 int md_check_no_bitmap(mddev_t *mddev)
982 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
983 return 0;
984 printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
985 mdname(mddev), mddev->pers->name);
986 return 1;
988 EXPORT_SYMBOL(md_check_no_bitmap);
991 * load_super for 0.90.0
993 static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
995 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
996 mdp_super_t *sb;
997 int ret;
1000 * Calculate the position of the superblock (512byte sectors),
1001 * it's at the end of the disk.
1003 * It also happens to be a multiple of 4Kb.
1005 rdev->sb_start = calc_dev_sboffset(rdev);
1007 ret = read_disk_sb(rdev, MD_SB_BYTES);
1008 if (ret) return ret;
1010 ret = -EINVAL;
1012 bdevname(rdev->bdev, b);
1013 sb = (mdp_super_t*)page_address(rdev->sb_page);
1015 if (sb->md_magic != MD_SB_MAGIC) {
1016 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
1018 goto abort;
1021 if (sb->major_version != 0 ||
1022 sb->minor_version < 90 ||
1023 sb->minor_version > 91) {
1024 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
1025 sb->major_version, sb->minor_version,
1027 goto abort;
1030 if (sb->raid_disks <= 0)
1031 goto abort;
1033 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
1034 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
1036 goto abort;
1039 rdev->preferred_minor = sb->md_minor;
1040 rdev->data_offset = 0;
1041 rdev->sb_size = MD_SB_BYTES;
1043 if (sb->level == LEVEL_MULTIPATH)
1044 rdev->desc_nr = -1;
1045 else
1046 rdev->desc_nr = sb->this_disk.number;
1048 if (!refdev) {
1049 ret = 1;
1050 } else {
1051 __u64 ev1, ev2;
1052 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
1053 if (!uuid_equal(refsb, sb)) {
1054 printk(KERN_WARNING "md: %s has different UUID to %s\n",
1055 b, bdevname(refdev->bdev,b2));
1056 goto abort;
1058 if (!sb_equal(refsb, sb)) {
1059 printk(KERN_WARNING "md: %s has same UUID"
1060 " but different superblock to %s\n",
1061 b, bdevname(refdev->bdev, b2));
1062 goto abort;
1064 ev1 = md_event(sb);
1065 ev2 = md_event(refsb);
1066 if (ev1 > ev2)
1067 ret = 1;
1068 else
1069 ret = 0;
1071 rdev->sectors = rdev->sb_start;
1073 if (rdev->sectors < sb->size * 2 && sb->level > 1)
1074 /* "this cannot possibly happen" ... */
1075 ret = -EINVAL;
1077 abort:
1078 return ret;
1082 * validate_super for 0.90.0
1084 static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1086 mdp_disk_t *desc;
1087 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
1088 __u64 ev1 = md_event(sb);
1090 rdev->raid_disk = -1;
1091 clear_bit(Faulty, &rdev->flags);
1092 clear_bit(In_sync, &rdev->flags);
1093 clear_bit(WriteMostly, &rdev->flags);
1095 if (mddev->raid_disks == 0) {
1096 mddev->major_version = 0;
1097 mddev->minor_version = sb->minor_version;
1098 mddev->patch_version = sb->patch_version;
1099 mddev->external = 0;
1100 mddev->chunk_sectors = sb->chunk_size >> 9;
1101 mddev->ctime = sb->ctime;
1102 mddev->utime = sb->utime;
1103 mddev->level = sb->level;
1104 mddev->clevel[0] = 0;
1105 mddev->layout = sb->layout;
1106 mddev->raid_disks = sb->raid_disks;
1107 mddev->dev_sectors = sb->size * 2;
1108 mddev->events = ev1;
1109 mddev->bitmap_info.offset = 0;
1110 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
1112 if (mddev->minor_version >= 91) {
1113 mddev->reshape_position = sb->reshape_position;
1114 mddev->delta_disks = sb->delta_disks;
1115 mddev->new_level = sb->new_level;
1116 mddev->new_layout = sb->new_layout;
1117 mddev->new_chunk_sectors = sb->new_chunk >> 9;
1118 } else {
1119 mddev->reshape_position = MaxSector;
1120 mddev->delta_disks = 0;
1121 mddev->new_level = mddev->level;
1122 mddev->new_layout = mddev->layout;
1123 mddev->new_chunk_sectors = mddev->chunk_sectors;
1126 if (sb->state & (1<<MD_SB_CLEAN))
1127 mddev->recovery_cp = MaxSector;
1128 else {
1129 if (sb->events_hi == sb->cp_events_hi &&
1130 sb->events_lo == sb->cp_events_lo) {
1131 mddev->recovery_cp = sb->recovery_cp;
1132 } else
1133 mddev->recovery_cp = 0;
1136 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1137 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1138 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1139 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1141 mddev->max_disks = MD_SB_DISKS;
1143 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
1144 mddev->bitmap_info.file == NULL)
1145 mddev->bitmap_info.offset =
1146 mddev->bitmap_info.default_offset;
1148 } else if (mddev->pers == NULL) {
1149 /* Insist on good event counter while assembling, except
1150 * for spares (which don't need an event count) */
1151 ++ev1;
1152 if (sb->disks[rdev->desc_nr].state & (
1153 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
1154 if (ev1 < mddev->events)
1155 return -EINVAL;
1156 } else if (mddev->bitmap) {
1157 /* if adding to array with a bitmap, then we can accept an
1158 * older device ... but not too old.
1160 if (ev1 < mddev->bitmap->events_cleared)
1161 return 0;
1162 } else {
1163 if (ev1 < mddev->events)
1164 /* just a hot-add of a new device, leave raid_disk at -1 */
1165 return 0;
1168 if (mddev->level != LEVEL_MULTIPATH) {
1169 desc = sb->disks + rdev->desc_nr;
1171 if (desc->state & (1<<MD_DISK_FAULTY))
1172 set_bit(Faulty, &rdev->flags);
1173 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1174 desc->raid_disk < mddev->raid_disks */) {
1175 set_bit(In_sync, &rdev->flags);
1176 rdev->raid_disk = desc->raid_disk;
1177 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1178 /* active but not in sync implies recovery up to
1179 * reshape position. We don't know exactly where
1180 * that is, so set to zero for now */
1181 if (mddev->minor_version >= 91) {
1182 rdev->recovery_offset = 0;
1183 rdev->raid_disk = desc->raid_disk;
1186 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1187 set_bit(WriteMostly, &rdev->flags);
1188 } else /* MULTIPATH are always insync */
1189 set_bit(In_sync, &rdev->flags);
1190 return 0;
1194 * sync_super for 0.90.0
1196 static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1198 mdp_super_t *sb;
1199 mdk_rdev_t *rdev2;
1200 int next_spare = mddev->raid_disks;
1203 /* make rdev->sb match mddev data..
1205 * 1/ zero out disks
1206 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1207 * 3/ any empty disks < next_spare become removed
1209 * disks[0] gets initialised to REMOVED because
1210 * we cannot be sure from other fields if it has
1211 * been initialised or not.
1213 int i;
1214 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1216 rdev->sb_size = MD_SB_BYTES;
1218 sb = (mdp_super_t*)page_address(rdev->sb_page);
1220 memset(sb, 0, sizeof(*sb));
1222 sb->md_magic = MD_SB_MAGIC;
1223 sb->major_version = mddev->major_version;
1224 sb->patch_version = mddev->patch_version;
1225 sb->gvalid_words = 0; /* ignored */
1226 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1227 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1228 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1229 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1231 sb->ctime = mddev->ctime;
1232 sb->level = mddev->level;
1233 sb->size = mddev->dev_sectors / 2;
1234 sb->raid_disks = mddev->raid_disks;
1235 sb->md_minor = mddev->md_minor;
1236 sb->not_persistent = 0;
1237 sb->utime = mddev->utime;
1238 sb->state = 0;
1239 sb->events_hi = (mddev->events>>32);
1240 sb->events_lo = (u32)mddev->events;
1242 if (mddev->reshape_position == MaxSector)
1243 sb->minor_version = 90;
1244 else {
1245 sb->minor_version = 91;
1246 sb->reshape_position = mddev->reshape_position;
1247 sb->new_level = mddev->new_level;
1248 sb->delta_disks = mddev->delta_disks;
1249 sb->new_layout = mddev->new_layout;
1250 sb->new_chunk = mddev->new_chunk_sectors << 9;
1252 mddev->minor_version = sb->minor_version;
1253 if (mddev->in_sync)
1255 sb->recovery_cp = mddev->recovery_cp;
1256 sb->cp_events_hi = (mddev->events>>32);
1257 sb->cp_events_lo = (u32)mddev->events;
1258 if (mddev->recovery_cp == MaxSector)
1259 sb->state = (1<< MD_SB_CLEAN);
1260 } else
1261 sb->recovery_cp = 0;
1263 sb->layout = mddev->layout;
1264 sb->chunk_size = mddev->chunk_sectors << 9;
1266 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
1267 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1269 sb->disks[0].state = (1<<MD_DISK_REMOVED);
1270 list_for_each_entry(rdev2, &mddev->disks, same_set) {
1271 mdp_disk_t *d;
1272 int desc_nr;
1273 int is_active = test_bit(In_sync, &rdev2->flags);
1275 if (rdev2->raid_disk >= 0 &&
1276 sb->minor_version >= 91)
1277 /* we have nowhere to store the recovery_offset,
1278 * but if it is not below the reshape_position,
1279 * we can piggy-back on that.
1281 is_active = 1;
1282 if (rdev2->raid_disk < 0 ||
1283 test_bit(Faulty, &rdev2->flags))
1284 is_active = 0;
1285 if (is_active)
1286 desc_nr = rdev2->raid_disk;
1287 else
1288 desc_nr = next_spare++;
1289 rdev2->desc_nr = desc_nr;
1290 d = &sb->disks[rdev2->desc_nr];
1291 nr_disks++;
1292 d->number = rdev2->desc_nr;
1293 d->major = MAJOR(rdev2->bdev->bd_dev);
1294 d->minor = MINOR(rdev2->bdev->bd_dev);
1295 if (is_active)
1296 d->raid_disk = rdev2->raid_disk;
1297 else
1298 d->raid_disk = rdev2->desc_nr; /* compatibility */
1299 if (test_bit(Faulty, &rdev2->flags))
1300 d->state = (1<<MD_DISK_FAULTY);
1301 else if (is_active) {
1302 d->state = (1<<MD_DISK_ACTIVE);
1303 if (test_bit(In_sync, &rdev2->flags))
1304 d->state |= (1<<MD_DISK_SYNC);
1305 active++;
1306 working++;
1307 } else {
1308 d->state = 0;
1309 spare++;
1310 working++;
1312 if (test_bit(WriteMostly, &rdev2->flags))
1313 d->state |= (1<<MD_DISK_WRITEMOSTLY);
1315 /* now set the "removed" and "faulty" bits on any missing devices */
1316 for (i=0 ; i < mddev->raid_disks ; i++) {
1317 mdp_disk_t *d = &sb->disks[i];
1318 if (d->state == 0 && d->number == 0) {
1319 d->number = i;
1320 d->raid_disk = i;
1321 d->state = (1<<MD_DISK_REMOVED);
1322 d->state |= (1<<MD_DISK_FAULTY);
1323 failed++;
1326 sb->nr_disks = nr_disks;
1327 sb->active_disks = active;
1328 sb->working_disks = working;
1329 sb->failed_disks = failed;
1330 sb->spare_disks = spare;
1332 sb->this_disk = sb->disks[rdev->desc_nr];
1333 sb->sb_csum = calc_sb_csum(sb);
1337 * rdev_size_change for 0.90.0
1339 static unsigned long long
1340 super_90_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
1342 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
1343 return 0; /* component must fit device */
1344 if (rdev->mddev->bitmap_info.offset)
1345 return 0; /* can't move bitmap */
1346 rdev->sb_start = calc_dev_sboffset(rdev);
1347 if (!num_sectors || num_sectors > rdev->sb_start)
1348 num_sectors = rdev->sb_start;
1349 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
1350 rdev->sb_page);
1351 md_super_wait(rdev->mddev);
1352 return num_sectors;
1357 * version 1 superblock
1360 static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
1362 __le32 disk_csum;
1363 u32 csum;
1364 unsigned long long newcsum;
1365 int size = 256 + le32_to_cpu(sb->max_dev)*2;
1366 __le32 *isuper = (__le32*)sb;
1367 int i;
1369 disk_csum = sb->sb_csum;
1370 sb->sb_csum = 0;
1371 newcsum = 0;
1372 for (i=0; size>=4; size -= 4 )
1373 newcsum += le32_to_cpu(*isuper++);
1375 if (size == 2)
1376 newcsum += le16_to_cpu(*(__le16*) isuper);
1378 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1379 sb->sb_csum = disk_csum;
1380 return cpu_to_le32(csum);
1383 static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
1385 struct mdp_superblock_1 *sb;
1386 int ret;
1387 sector_t sb_start;
1388 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1389 int bmask;
1392 * Calculate the position of the superblock in 512byte sectors.
1393 * It is always aligned to a 4K boundary and
1394 * depeding on minor_version, it can be:
1395 * 0: At least 8K, but less than 12K, from end of device
1396 * 1: At start of device
1397 * 2: 4K from start of device.
1399 switch(minor_version) {
1400 case 0:
1401 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
1402 sb_start -= 8*2;
1403 sb_start &= ~(sector_t)(4*2-1);
1404 break;
1405 case 1:
1406 sb_start = 0;
1407 break;
1408 case 2:
1409 sb_start = 8;
1410 break;
1411 default:
1412 return -EINVAL;
1414 rdev->sb_start = sb_start;
1416 /* superblock is rarely larger than 1K, but it can be larger,
1417 * and it is safe to read 4k, so we do that
1419 ret = read_disk_sb(rdev, 4096);
1420 if (ret) return ret;
1423 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1425 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1426 sb->major_version != cpu_to_le32(1) ||
1427 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
1428 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
1429 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
1430 return -EINVAL;
1432 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1433 printk("md: invalid superblock checksum on %s\n",
1434 bdevname(rdev->bdev,b));
1435 return -EINVAL;
1437 if (le64_to_cpu(sb->data_size) < 10) {
1438 printk("md: data_size too small on %s\n",
1439 bdevname(rdev->bdev,b));
1440 return -EINVAL;
1443 rdev->preferred_minor = 0xffff;
1444 rdev->data_offset = le64_to_cpu(sb->data_offset);
1445 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
1447 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
1448 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1449 if (rdev->sb_size & bmask)
1450 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1452 if (minor_version
1453 && rdev->data_offset < sb_start + (rdev->sb_size/512))
1454 return -EINVAL;
1456 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1457 rdev->desc_nr = -1;
1458 else
1459 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1461 if (!refdev) {
1462 ret = 1;
1463 } else {
1464 __u64 ev1, ev2;
1465 struct mdp_superblock_1 *refsb =
1466 (struct mdp_superblock_1*)page_address(refdev->sb_page);
1468 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1469 sb->level != refsb->level ||
1470 sb->layout != refsb->layout ||
1471 sb->chunksize != refsb->chunksize) {
1472 printk(KERN_WARNING "md: %s has strangely different"
1473 " superblock to %s\n",
1474 bdevname(rdev->bdev,b),
1475 bdevname(refdev->bdev,b2));
1476 return -EINVAL;
1478 ev1 = le64_to_cpu(sb->events);
1479 ev2 = le64_to_cpu(refsb->events);
1481 if (ev1 > ev2)
1482 ret = 1;
1483 else
1484 ret = 0;
1486 if (minor_version)
1487 rdev->sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
1488 le64_to_cpu(sb->data_offset);
1489 else
1490 rdev->sectors = rdev->sb_start;
1491 if (rdev->sectors < le64_to_cpu(sb->data_size))
1492 return -EINVAL;
1493 rdev->sectors = le64_to_cpu(sb->data_size);
1494 if (le64_to_cpu(sb->size) > rdev->sectors)
1495 return -EINVAL;
1496 return ret;
1499 static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1501 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1502 __u64 ev1 = le64_to_cpu(sb->events);
1504 rdev->raid_disk = -1;
1505 clear_bit(Faulty, &rdev->flags);
1506 clear_bit(In_sync, &rdev->flags);
1507 clear_bit(WriteMostly, &rdev->flags);
1509 if (mddev->raid_disks == 0) {
1510 mddev->major_version = 1;
1511 mddev->patch_version = 0;
1512 mddev->external = 0;
1513 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
1514 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1515 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1516 mddev->level = le32_to_cpu(sb->level);
1517 mddev->clevel[0] = 0;
1518 mddev->layout = le32_to_cpu(sb->layout);
1519 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
1520 mddev->dev_sectors = le64_to_cpu(sb->size);
1521 mddev->events = ev1;
1522 mddev->bitmap_info.offset = 0;
1523 mddev->bitmap_info.default_offset = 1024 >> 9;
1525 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1526 memcpy(mddev->uuid, sb->set_uuid, 16);
1528 mddev->max_disks = (4096-256)/2;
1530 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
1531 mddev->bitmap_info.file == NULL )
1532 mddev->bitmap_info.offset =
1533 (__s32)le32_to_cpu(sb->bitmap_offset);
1535 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1536 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1537 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1538 mddev->new_level = le32_to_cpu(sb->new_level);
1539 mddev->new_layout = le32_to_cpu(sb->new_layout);
1540 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
1541 } else {
1542 mddev->reshape_position = MaxSector;
1543 mddev->delta_disks = 0;
1544 mddev->new_level = mddev->level;
1545 mddev->new_layout = mddev->layout;
1546 mddev->new_chunk_sectors = mddev->chunk_sectors;
1549 } else if (mddev->pers == NULL) {
1550 /* Insist of good event counter while assembling, except for
1551 * spares (which don't need an event count) */
1552 ++ev1;
1553 if (rdev->desc_nr >= 0 &&
1554 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
1555 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe)
1556 if (ev1 < mddev->events)
1557 return -EINVAL;
1558 } else if (mddev->bitmap) {
1559 /* If adding to array with a bitmap, then we can accept an
1560 * older device, but not too old.
1562 if (ev1 < mddev->bitmap->events_cleared)
1563 return 0;
1564 } else {
1565 if (ev1 < mddev->events)
1566 /* just a hot-add of a new device, leave raid_disk at -1 */
1567 return 0;
1569 if (mddev->level != LEVEL_MULTIPATH) {
1570 int role;
1571 if (rdev->desc_nr < 0 ||
1572 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1573 role = 0xffff;
1574 rdev->desc_nr = -1;
1575 } else
1576 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
1577 switch(role) {
1578 case 0xffff: /* spare */
1579 break;
1580 case 0xfffe: /* faulty */
1581 set_bit(Faulty, &rdev->flags);
1582 break;
1583 default:
1584 if ((le32_to_cpu(sb->feature_map) &
1585 MD_FEATURE_RECOVERY_OFFSET))
1586 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1587 else
1588 set_bit(In_sync, &rdev->flags);
1589 rdev->raid_disk = role;
1590 break;
1592 if (sb->devflags & WriteMostly1)
1593 set_bit(WriteMostly, &rdev->flags);
1594 } else /* MULTIPATH are always insync */
1595 set_bit(In_sync, &rdev->flags);
1597 return 0;
1600 static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1602 struct mdp_superblock_1 *sb;
1603 mdk_rdev_t *rdev2;
1604 int max_dev, i;
1605 /* make rdev->sb match mddev and rdev data. */
1607 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1609 sb->feature_map = 0;
1610 sb->pad0 = 0;
1611 sb->recovery_offset = cpu_to_le64(0);
1612 memset(sb->pad1, 0, sizeof(sb->pad1));
1613 memset(sb->pad2, 0, sizeof(sb->pad2));
1614 memset(sb->pad3, 0, sizeof(sb->pad3));
1616 sb->utime = cpu_to_le64((__u64)mddev->utime);
1617 sb->events = cpu_to_le64(mddev->events);
1618 if (mddev->in_sync)
1619 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1620 else
1621 sb->resync_offset = cpu_to_le64(0);
1623 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
1625 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
1626 sb->size = cpu_to_le64(mddev->dev_sectors);
1627 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
1628 sb->level = cpu_to_le32(mddev->level);
1629 sb->layout = cpu_to_le32(mddev->layout);
1631 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1632 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
1633 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
1636 if (rdev->raid_disk >= 0 &&
1637 !test_bit(In_sync, &rdev->flags)) {
1638 sb->feature_map |=
1639 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1640 sb->recovery_offset =
1641 cpu_to_le64(rdev->recovery_offset);
1644 if (mddev->reshape_position != MaxSector) {
1645 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1646 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1647 sb->new_layout = cpu_to_le32(mddev->new_layout);
1648 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1649 sb->new_level = cpu_to_le32(mddev->new_level);
1650 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
1653 max_dev = 0;
1654 list_for_each_entry(rdev2, &mddev->disks, same_set)
1655 if (rdev2->desc_nr+1 > max_dev)
1656 max_dev = rdev2->desc_nr+1;
1658 if (max_dev > le32_to_cpu(sb->max_dev)) {
1659 int bmask;
1660 sb->max_dev = cpu_to_le32(max_dev);
1661 rdev->sb_size = max_dev * 2 + 256;
1662 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1663 if (rdev->sb_size & bmask)
1664 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1665 } else
1666 max_dev = le32_to_cpu(sb->max_dev);
1668 for (i=0; i<max_dev;i++)
1669 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1671 list_for_each_entry(rdev2, &mddev->disks, same_set) {
1672 i = rdev2->desc_nr;
1673 if (test_bit(Faulty, &rdev2->flags))
1674 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1675 else if (test_bit(In_sync, &rdev2->flags))
1676 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
1677 else if (rdev2->raid_disk >= 0)
1678 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
1679 else
1680 sb->dev_roles[i] = cpu_to_le16(0xffff);
1683 sb->sb_csum = calc_sb_1_csum(sb);
1686 static unsigned long long
1687 super_1_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
1689 struct mdp_superblock_1 *sb;
1690 sector_t max_sectors;
1691 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
1692 return 0; /* component must fit device */
1693 if (rdev->sb_start < rdev->data_offset) {
1694 /* minor versions 1 and 2; superblock before data */
1695 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
1696 max_sectors -= rdev->data_offset;
1697 if (!num_sectors || num_sectors > max_sectors)
1698 num_sectors = max_sectors;
1699 } else if (rdev->mddev->bitmap_info.offset) {
1700 /* minor version 0 with bitmap we can't move */
1701 return 0;
1702 } else {
1703 /* minor version 0; superblock after data */
1704 sector_t sb_start;
1705 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
1706 sb_start &= ~(sector_t)(4*2 - 1);
1707 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
1708 if (!num_sectors || num_sectors > max_sectors)
1709 num_sectors = max_sectors;
1710 rdev->sb_start = sb_start;
1712 sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page);
1713 sb->data_size = cpu_to_le64(num_sectors);
1714 sb->super_offset = rdev->sb_start;
1715 sb->sb_csum = calc_sb_1_csum(sb);
1716 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
1717 rdev->sb_page);
1718 md_super_wait(rdev->mddev);
1719 return num_sectors;
1722 static struct super_type super_types[] = {
1723 [0] = {
1724 .name = "0.90.0",
1725 .owner = THIS_MODULE,
1726 .load_super = super_90_load,
1727 .validate_super = super_90_validate,
1728 .sync_super = super_90_sync,
1729 .rdev_size_change = super_90_rdev_size_change,
1731 [1] = {
1732 .name = "md-1",
1733 .owner = THIS_MODULE,
1734 .load_super = super_1_load,
1735 .validate_super = super_1_validate,
1736 .sync_super = super_1_sync,
1737 .rdev_size_change = super_1_rdev_size_change,
1741 static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1743 mdk_rdev_t *rdev, *rdev2;
1745 rcu_read_lock();
1746 rdev_for_each_rcu(rdev, mddev1)
1747 rdev_for_each_rcu(rdev2, mddev2)
1748 if (rdev->bdev->bd_contains ==
1749 rdev2->bdev->bd_contains) {
1750 rcu_read_unlock();
1751 return 1;
1753 rcu_read_unlock();
1754 return 0;
1757 static LIST_HEAD(pending_raid_disks);
1760 * Try to register data integrity profile for an mddev
1762 * This is called when an array is started and after a disk has been kicked
1763 * from the array. It only succeeds if all working and active component devices
1764 * are integrity capable with matching profiles.
1766 int md_integrity_register(mddev_t *mddev)
1768 mdk_rdev_t *rdev, *reference = NULL;
1770 if (list_empty(&mddev->disks))
1771 return 0; /* nothing to do */
1772 if (blk_get_integrity(mddev->gendisk))
1773 return 0; /* already registered */
1774 list_for_each_entry(rdev, &mddev->disks, same_set) {
1775 /* skip spares and non-functional disks */
1776 if (test_bit(Faulty, &rdev->flags))
1777 continue;
1778 if (rdev->raid_disk < 0)
1779 continue;
1780 if (!reference) {
1781 /* Use the first rdev as the reference */
1782 reference = rdev;
1783 continue;
1785 /* does this rdev's profile match the reference profile? */
1786 if (blk_integrity_compare(reference->bdev->bd_disk,
1787 rdev->bdev->bd_disk) < 0)
1788 return -EINVAL;
1790 if (!reference || !bdev_get_integrity(reference->bdev))
1791 return 0;
1793 * All component devices are integrity capable and have matching
1794 * profiles, register the common profile for the md device.
1796 if (blk_integrity_register(mddev->gendisk,
1797 bdev_get_integrity(reference->bdev)) != 0) {
1798 printk(KERN_ERR "md: failed to register integrity for %s\n",
1799 mdname(mddev));
1800 return -EINVAL;
1802 printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev));
1803 if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) {
1804 printk(KERN_ERR "md: failed to create integrity pool for %s\n",
1805 mdname(mddev));
1806 return -EINVAL;
1808 return 0;
1810 EXPORT_SYMBOL(md_integrity_register);
1812 /* Disable data integrity if non-capable/non-matching disk is being added */
1813 void md_integrity_add_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
1815 struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev);
1816 struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk);
1818 if (!bi_mddev) /* nothing to do */
1819 return;
1820 if (rdev->raid_disk < 0) /* skip spares */
1821 return;
1822 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
1823 rdev->bdev->bd_disk) >= 0)
1824 return;
1825 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
1826 blk_integrity_unregister(mddev->gendisk);
1828 EXPORT_SYMBOL(md_integrity_add_rdev);
1830 static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1832 char b[BDEVNAME_SIZE];
1833 struct kobject *ko;
1834 char *s;
1835 int err;
1837 if (rdev->mddev) {
1838 MD_BUG();
1839 return -EINVAL;
1842 /* prevent duplicates */
1843 if (find_rdev(mddev, rdev->bdev->bd_dev))
1844 return -EEXIST;
1846 /* make sure rdev->sectors exceeds mddev->dev_sectors */
1847 if (rdev->sectors && (mddev->dev_sectors == 0 ||
1848 rdev->sectors < mddev->dev_sectors)) {
1849 if (mddev->pers) {
1850 /* Cannot change size, so fail
1851 * If mddev->level <= 0, then we don't care
1852 * about aligning sizes (e.g. linear)
1854 if (mddev->level > 0)
1855 return -ENOSPC;
1856 } else
1857 mddev->dev_sectors = rdev->sectors;
1860 /* Verify rdev->desc_nr is unique.
1861 * If it is -1, assign a free number, else
1862 * check number is not in use
1864 if (rdev->desc_nr < 0) {
1865 int choice = 0;
1866 if (mddev->pers) choice = mddev->raid_disks;
1867 while (find_rdev_nr(mddev, choice))
1868 choice++;
1869 rdev->desc_nr = choice;
1870 } else {
1871 if (find_rdev_nr(mddev, rdev->desc_nr))
1872 return -EBUSY;
1874 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
1875 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
1876 mdname(mddev), mddev->max_disks);
1877 return -EBUSY;
1879 bdevname(rdev->bdev,b);
1880 while ( (s=strchr(b, '/')) != NULL)
1881 *s = '!';
1883 rdev->mddev = mddev;
1884 printk(KERN_INFO "md: bind<%s>\n", b);
1886 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
1887 goto fail;
1889 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
1890 if (sysfs_create_link(&rdev->kobj, ko, "block"))
1891 /* failure here is OK */;
1892 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
1894 list_add_rcu(&rdev->same_set, &mddev->disks);
1895 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
1897 /* May as well allow recovery to be retried once */
1898 mddev->recovery_disabled = 0;
1900 return 0;
1902 fail:
1903 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
1904 b, mdname(mddev));
1905 return err;
1908 static void md_delayed_delete(struct work_struct *ws)
1910 mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work);
1911 kobject_del(&rdev->kobj);
1912 kobject_put(&rdev->kobj);
1915 static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1917 char b[BDEVNAME_SIZE];
1918 if (!rdev->mddev) {
1919 MD_BUG();
1920 return;
1922 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
1923 list_del_rcu(&rdev->same_set);
1924 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1925 rdev->mddev = NULL;
1926 sysfs_remove_link(&rdev->kobj, "block");
1927 sysfs_put(rdev->sysfs_state);
1928 rdev->sysfs_state = NULL;
1929 /* We need to delay this, otherwise we can deadlock when
1930 * writing to 'remove' to "dev/state". We also need
1931 * to delay it due to rcu usage.
1933 synchronize_rcu();
1934 INIT_WORK(&rdev->del_work, md_delayed_delete);
1935 kobject_get(&rdev->kobj);
1936 queue_work(md_misc_wq, &rdev->del_work);
1940 * prevent the device from being mounted, repartitioned or
1941 * otherwise reused by a RAID array (or any other kernel
1942 * subsystem), by bd_claiming the device.
1944 static int lock_rdev(mdk_rdev_t *rdev, dev_t dev, int shared)
1946 int err = 0;
1947 struct block_device *bdev;
1948 char b[BDEVNAME_SIZE];
1950 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
1951 shared ? (mdk_rdev_t *)lock_rdev : rdev);
1952 if (IS_ERR(bdev)) {
1953 printk(KERN_ERR "md: could not open %s.\n",
1954 __bdevname(dev, b));
1955 return PTR_ERR(bdev);
1957 rdev->bdev = bdev;
1958 return err;
1961 static void unlock_rdev(mdk_rdev_t *rdev)
1963 struct block_device *bdev = rdev->bdev;
1964 rdev->bdev = NULL;
1965 if (!bdev)
1966 MD_BUG();
1967 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
1970 void md_autodetect_dev(dev_t dev);
1972 static void export_rdev(mdk_rdev_t * rdev)
1974 char b[BDEVNAME_SIZE];
1975 printk(KERN_INFO "md: export_rdev(%s)\n",
1976 bdevname(rdev->bdev,b));
1977 if (rdev->mddev)
1978 MD_BUG();
1979 free_disk_sb(rdev);
1980 #ifndef MODULE
1981 if (test_bit(AutoDetected, &rdev->flags))
1982 md_autodetect_dev(rdev->bdev->bd_dev);
1983 #endif
1984 unlock_rdev(rdev);
1985 kobject_put(&rdev->kobj);
1988 static void kick_rdev_from_array(mdk_rdev_t * rdev)
1990 unbind_rdev_from_array(rdev);
1991 export_rdev(rdev);
1994 static void export_array(mddev_t *mddev)
1996 mdk_rdev_t *rdev, *tmp;
1998 rdev_for_each(rdev, tmp, mddev) {
1999 if (!rdev->mddev) {
2000 MD_BUG();
2001 continue;
2003 kick_rdev_from_array(rdev);
2005 if (!list_empty(&mddev->disks))
2006 MD_BUG();
2007 mddev->raid_disks = 0;
2008 mddev->major_version = 0;
2011 static void print_desc(mdp_disk_t *desc)
2013 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
2014 desc->major,desc->minor,desc->raid_disk,desc->state);
2017 static void print_sb_90(mdp_super_t *sb)
2019 int i;
2021 printk(KERN_INFO
2022 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
2023 sb->major_version, sb->minor_version, sb->patch_version,
2024 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
2025 sb->ctime);
2026 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
2027 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
2028 sb->md_minor, sb->layout, sb->chunk_size);
2029 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
2030 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
2031 sb->utime, sb->state, sb->active_disks, sb->working_disks,
2032 sb->failed_disks, sb->spare_disks,
2033 sb->sb_csum, (unsigned long)sb->events_lo);
2035 printk(KERN_INFO);
2036 for (i = 0; i < MD_SB_DISKS; i++) {
2037 mdp_disk_t *desc;
2039 desc = sb->disks + i;
2040 if (desc->number || desc->major || desc->minor ||
2041 desc->raid_disk || (desc->state && (desc->state != 4))) {
2042 printk(" D %2d: ", i);
2043 print_desc(desc);
2046 printk(KERN_INFO "md: THIS: ");
2047 print_desc(&sb->this_disk);
2050 static void print_sb_1(struct mdp_superblock_1 *sb)
2052 __u8 *uuid;
2054 uuid = sb->set_uuid;
2055 printk(KERN_INFO
2056 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n"
2057 "md: Name: \"%s\" CT:%llu\n",
2058 le32_to_cpu(sb->major_version),
2059 le32_to_cpu(sb->feature_map),
2060 uuid,
2061 sb->set_name,
2062 (unsigned long long)le64_to_cpu(sb->ctime)
2063 & MD_SUPERBLOCK_1_TIME_SEC_MASK);
2065 uuid = sb->device_uuid;
2066 printk(KERN_INFO
2067 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
2068 " RO:%llu\n"
2069 "md: Dev:%08x UUID: %pU\n"
2070 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
2071 "md: (MaxDev:%u) \n",
2072 le32_to_cpu(sb->level),
2073 (unsigned long long)le64_to_cpu(sb->size),
2074 le32_to_cpu(sb->raid_disks),
2075 le32_to_cpu(sb->layout),
2076 le32_to_cpu(sb->chunksize),
2077 (unsigned long long)le64_to_cpu(sb->data_offset),
2078 (unsigned long long)le64_to_cpu(sb->data_size),
2079 (unsigned long long)le64_to_cpu(sb->super_offset),
2080 (unsigned long long)le64_to_cpu(sb->recovery_offset),
2081 le32_to_cpu(sb->dev_number),
2082 uuid,
2083 sb->devflags,
2084 (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK,
2085 (unsigned long long)le64_to_cpu(sb->events),
2086 (unsigned long long)le64_to_cpu(sb->resync_offset),
2087 le32_to_cpu(sb->sb_csum),
2088 le32_to_cpu(sb->max_dev)
2092 static void print_rdev(mdk_rdev_t *rdev, int major_version)
2094 char b[BDEVNAME_SIZE];
2095 printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
2096 bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors,
2097 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
2098 rdev->desc_nr);
2099 if (rdev->sb_loaded) {
2100 printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version);
2101 switch (major_version) {
2102 case 0:
2103 print_sb_90((mdp_super_t*)page_address(rdev->sb_page));
2104 break;
2105 case 1:
2106 print_sb_1((struct mdp_superblock_1 *)page_address(rdev->sb_page));
2107 break;
2109 } else
2110 printk(KERN_INFO "md: no rdev superblock!\n");
2113 static void md_print_devices(void)
2115 struct list_head *tmp;
2116 mdk_rdev_t *rdev;
2117 mddev_t *mddev;
2118 char b[BDEVNAME_SIZE];
2120 printk("\n");
2121 printk("md: **********************************\n");
2122 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
2123 printk("md: **********************************\n");
2124 for_each_mddev(mddev, tmp) {
2126 if (mddev->bitmap)
2127 bitmap_print_sb(mddev->bitmap);
2128 else
2129 printk("%s: ", mdname(mddev));
2130 list_for_each_entry(rdev, &mddev->disks, same_set)
2131 printk("<%s>", bdevname(rdev->bdev,b));
2132 printk("\n");
2134 list_for_each_entry(rdev, &mddev->disks, same_set)
2135 print_rdev(rdev, mddev->major_version);
2137 printk("md: **********************************\n");
2138 printk("\n");
2142 static void sync_sbs(mddev_t * mddev, int nospares)
2144 /* Update each superblock (in-memory image), but
2145 * if we are allowed to, skip spares which already
2146 * have the right event counter, or have one earlier
2147 * (which would mean they aren't being marked as dirty
2148 * with the rest of the array)
2150 mdk_rdev_t *rdev;
2151 list_for_each_entry(rdev, &mddev->disks, same_set) {
2152 if (rdev->sb_events == mddev->events ||
2153 (nospares &&
2154 rdev->raid_disk < 0 &&
2155 rdev->sb_events+1 == mddev->events)) {
2156 /* Don't update this superblock */
2157 rdev->sb_loaded = 2;
2158 } else {
2159 super_types[mddev->major_version].
2160 sync_super(mddev, rdev);
2161 rdev->sb_loaded = 1;
2166 static void md_update_sb(mddev_t * mddev, int force_change)
2168 mdk_rdev_t *rdev;
2169 int sync_req;
2170 int nospares = 0;
2172 repeat:
2173 /* First make sure individual recovery_offsets are correct */
2174 list_for_each_entry(rdev, &mddev->disks, same_set) {
2175 if (rdev->raid_disk >= 0 &&
2176 mddev->delta_disks >= 0 &&
2177 !test_bit(In_sync, &rdev->flags) &&
2178 mddev->curr_resync_completed > rdev->recovery_offset)
2179 rdev->recovery_offset = mddev->curr_resync_completed;
2182 if (!mddev->persistent) {
2183 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
2184 clear_bit(MD_CHANGE_DEVS, &mddev->flags);
2185 if (!mddev->external)
2186 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
2187 wake_up(&mddev->sb_wait);
2188 return;
2191 spin_lock_irq(&mddev->write_lock);
2193 mddev->utime = get_seconds();
2195 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2196 force_change = 1;
2197 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2198 /* just a clean<-> dirty transition, possibly leave spares alone,
2199 * though if events isn't the right even/odd, we will have to do
2200 * spares after all
2202 nospares = 1;
2203 if (force_change)
2204 nospares = 0;
2205 if (mddev->degraded)
2206 /* If the array is degraded, then skipping spares is both
2207 * dangerous and fairly pointless.
2208 * Dangerous because a device that was removed from the array
2209 * might have a event_count that still looks up-to-date,
2210 * so it can be re-added without a resync.
2211 * Pointless because if there are any spares to skip,
2212 * then a recovery will happen and soon that array won't
2213 * be degraded any more and the spare can go back to sleep then.
2215 nospares = 0;
2217 sync_req = mddev->in_sync;
2219 /* If this is just a dirty<->clean transition, and the array is clean
2220 * and 'events' is odd, we can roll back to the previous clean state */
2221 if (nospares
2222 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
2223 && mddev->can_decrease_events
2224 && mddev->events != 1) {
2225 mddev->events--;
2226 mddev->can_decrease_events = 0;
2227 } else {
2228 /* otherwise we have to go forward and ... */
2229 mddev->events ++;
2230 mddev->can_decrease_events = nospares;
2233 if (!mddev->events) {
2235 * oops, this 64-bit counter should never wrap.
2236 * Either we are in around ~1 trillion A.C., assuming
2237 * 1 reboot per second, or we have a bug:
2239 MD_BUG();
2240 mddev->events --;
2242 sync_sbs(mddev, nospares);
2243 spin_unlock_irq(&mddev->write_lock);
2245 dprintk(KERN_INFO
2246 "md: updating %s RAID superblock on device (in sync %d)\n",
2247 mdname(mddev),mddev->in_sync);
2249 bitmap_update_sb(mddev->bitmap);
2250 list_for_each_entry(rdev, &mddev->disks, same_set) {
2251 char b[BDEVNAME_SIZE];
2252 dprintk(KERN_INFO "md: ");
2253 if (rdev->sb_loaded != 1)
2254 continue; /* no noise on spare devices */
2255 if (test_bit(Faulty, &rdev->flags))
2256 dprintk("(skipping faulty ");
2258 dprintk("%s ", bdevname(rdev->bdev,b));
2259 if (!test_bit(Faulty, &rdev->flags)) {
2260 md_super_write(mddev,rdev,
2261 rdev->sb_start, rdev->sb_size,
2262 rdev->sb_page);
2263 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
2264 bdevname(rdev->bdev,b),
2265 (unsigned long long)rdev->sb_start);
2266 rdev->sb_events = mddev->events;
2268 } else
2269 dprintk(")\n");
2270 if (mddev->level == LEVEL_MULTIPATH)
2271 /* only need to write one superblock... */
2272 break;
2274 md_super_wait(mddev);
2275 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
2277 spin_lock_irq(&mddev->write_lock);
2278 if (mddev->in_sync != sync_req ||
2279 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
2280 /* have to write it out again */
2281 spin_unlock_irq(&mddev->write_lock);
2282 goto repeat;
2284 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
2285 spin_unlock_irq(&mddev->write_lock);
2286 wake_up(&mddev->sb_wait);
2287 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2288 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
2292 /* words written to sysfs files may, or may not, be \n terminated.
2293 * We want to accept with case. For this we use cmd_match.
2295 static int cmd_match(const char *cmd, const char *str)
2297 /* See if cmd, written into a sysfs file, matches
2298 * str. They must either be the same, or cmd can
2299 * have a trailing newline
2301 while (*cmd && *str && *cmd == *str) {
2302 cmd++;
2303 str++;
2305 if (*cmd == '\n')
2306 cmd++;
2307 if (*str || *cmd)
2308 return 0;
2309 return 1;
2312 struct rdev_sysfs_entry {
2313 struct attribute attr;
2314 ssize_t (*show)(mdk_rdev_t *, char *);
2315 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
2318 static ssize_t
2319 state_show(mdk_rdev_t *rdev, char *page)
2321 char *sep = "";
2322 size_t len = 0;
2324 if (test_bit(Faulty, &rdev->flags)) {
2325 len+= sprintf(page+len, "%sfaulty",sep);
2326 sep = ",";
2328 if (test_bit(In_sync, &rdev->flags)) {
2329 len += sprintf(page+len, "%sin_sync",sep);
2330 sep = ",";
2332 if (test_bit(WriteMostly, &rdev->flags)) {
2333 len += sprintf(page+len, "%swrite_mostly",sep);
2334 sep = ",";
2336 if (test_bit(Blocked, &rdev->flags)) {
2337 len += sprintf(page+len, "%sblocked", sep);
2338 sep = ",";
2340 if (!test_bit(Faulty, &rdev->flags) &&
2341 !test_bit(In_sync, &rdev->flags)) {
2342 len += sprintf(page+len, "%sspare", sep);
2343 sep = ",";
2345 return len+sprintf(page+len, "\n");
2348 static ssize_t
2349 state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2351 /* can write
2352 * faulty - simulates and error
2353 * remove - disconnects the device
2354 * writemostly - sets write_mostly
2355 * -writemostly - clears write_mostly
2356 * blocked - sets the Blocked flag
2357 * -blocked - clears the Blocked flag
2358 * insync - sets Insync providing device isn't active
2360 int err = -EINVAL;
2361 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2362 md_error(rdev->mddev, rdev);
2363 err = 0;
2364 } else if (cmd_match(buf, "remove")) {
2365 if (rdev->raid_disk >= 0)
2366 err = -EBUSY;
2367 else {
2368 mddev_t *mddev = rdev->mddev;
2369 kick_rdev_from_array(rdev);
2370 if (mddev->pers)
2371 md_update_sb(mddev, 1);
2372 md_new_event(mddev);
2373 err = 0;
2375 } else if (cmd_match(buf, "writemostly")) {
2376 set_bit(WriteMostly, &rdev->flags);
2377 err = 0;
2378 } else if (cmd_match(buf, "-writemostly")) {
2379 clear_bit(WriteMostly, &rdev->flags);
2380 err = 0;
2381 } else if (cmd_match(buf, "blocked")) {
2382 set_bit(Blocked, &rdev->flags);
2383 err = 0;
2384 } else if (cmd_match(buf, "-blocked")) {
2385 clear_bit(Blocked, &rdev->flags);
2386 wake_up(&rdev->blocked_wait);
2387 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2388 md_wakeup_thread(rdev->mddev->thread);
2390 err = 0;
2391 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2392 set_bit(In_sync, &rdev->flags);
2393 err = 0;
2395 if (!err)
2396 sysfs_notify_dirent_safe(rdev->sysfs_state);
2397 return err ? err : len;
2399 static struct rdev_sysfs_entry rdev_state =
2400 __ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
2402 static ssize_t
2403 errors_show(mdk_rdev_t *rdev, char *page)
2405 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2408 static ssize_t
2409 errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2411 char *e;
2412 unsigned long n = simple_strtoul(buf, &e, 10);
2413 if (*buf && (*e == 0 || *e == '\n')) {
2414 atomic_set(&rdev->corrected_errors, n);
2415 return len;
2417 return -EINVAL;
2419 static struct rdev_sysfs_entry rdev_errors =
2420 __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
2422 static ssize_t
2423 slot_show(mdk_rdev_t *rdev, char *page)
2425 if (rdev->raid_disk < 0)
2426 return sprintf(page, "none\n");
2427 else
2428 return sprintf(page, "%d\n", rdev->raid_disk);
2431 static ssize_t
2432 slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2434 char *e;
2435 int err;
2436 char nm[20];
2437 int slot = simple_strtoul(buf, &e, 10);
2438 if (strncmp(buf, "none", 4)==0)
2439 slot = -1;
2440 else if (e==buf || (*e && *e!= '\n'))
2441 return -EINVAL;
2442 if (rdev->mddev->pers && slot == -1) {
2443 /* Setting 'slot' on an active array requires also
2444 * updating the 'rd%d' link, and communicating
2445 * with the personality with ->hot_*_disk.
2446 * For now we only support removing
2447 * failed/spare devices. This normally happens automatically,
2448 * but not when the metadata is externally managed.
2450 if (rdev->raid_disk == -1)
2451 return -EEXIST;
2452 /* personality does all needed checks */
2453 if (rdev->mddev->pers->hot_add_disk == NULL)
2454 return -EINVAL;
2455 err = rdev->mddev->pers->
2456 hot_remove_disk(rdev->mddev, rdev->raid_disk);
2457 if (err)
2458 return err;
2459 sprintf(nm, "rd%d", rdev->raid_disk);
2460 sysfs_remove_link(&rdev->mddev->kobj, nm);
2461 rdev->raid_disk = -1;
2462 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2463 md_wakeup_thread(rdev->mddev->thread);
2464 } else if (rdev->mddev->pers) {
2465 mdk_rdev_t *rdev2;
2466 /* Activating a spare .. or possibly reactivating
2467 * if we ever get bitmaps working here.
2470 if (rdev->raid_disk != -1)
2471 return -EBUSY;
2473 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
2474 return -EBUSY;
2476 if (rdev->mddev->pers->hot_add_disk == NULL)
2477 return -EINVAL;
2479 list_for_each_entry(rdev2, &rdev->mddev->disks, same_set)
2480 if (rdev2->raid_disk == slot)
2481 return -EEXIST;
2483 if (slot >= rdev->mddev->raid_disks &&
2484 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2485 return -ENOSPC;
2487 rdev->raid_disk = slot;
2488 if (test_bit(In_sync, &rdev->flags))
2489 rdev->saved_raid_disk = slot;
2490 else
2491 rdev->saved_raid_disk = -1;
2492 err = rdev->mddev->pers->
2493 hot_add_disk(rdev->mddev, rdev);
2494 if (err) {
2495 rdev->raid_disk = -1;
2496 return err;
2497 } else
2498 sysfs_notify_dirent_safe(rdev->sysfs_state);
2499 sprintf(nm, "rd%d", rdev->raid_disk);
2500 if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm))
2501 /* failure here is OK */;
2502 /* don't wakeup anyone, leave that to userspace. */
2503 } else {
2504 if (slot >= rdev->mddev->raid_disks &&
2505 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2506 return -ENOSPC;
2507 rdev->raid_disk = slot;
2508 /* assume it is working */
2509 clear_bit(Faulty, &rdev->flags);
2510 clear_bit(WriteMostly, &rdev->flags);
2511 set_bit(In_sync, &rdev->flags);
2512 sysfs_notify_dirent_safe(rdev->sysfs_state);
2514 return len;
2518 static struct rdev_sysfs_entry rdev_slot =
2519 __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
2521 static ssize_t
2522 offset_show(mdk_rdev_t *rdev, char *page)
2524 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
2527 static ssize_t
2528 offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2530 char *e;
2531 unsigned long long offset = simple_strtoull(buf, &e, 10);
2532 if (e==buf || (*e && *e != '\n'))
2533 return -EINVAL;
2534 if (rdev->mddev->pers && rdev->raid_disk >= 0)
2535 return -EBUSY;
2536 if (rdev->sectors && rdev->mddev->external)
2537 /* Must set offset before size, so overlap checks
2538 * can be sane */
2539 return -EBUSY;
2540 rdev->data_offset = offset;
2541 return len;
2544 static struct rdev_sysfs_entry rdev_offset =
2545 __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
2547 static ssize_t
2548 rdev_size_show(mdk_rdev_t *rdev, char *page)
2550 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
2553 static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2555 /* check if two start/length pairs overlap */
2556 if (s1+l1 <= s2)
2557 return 0;
2558 if (s2+l2 <= s1)
2559 return 0;
2560 return 1;
2563 static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2565 unsigned long long blocks;
2566 sector_t new;
2568 if (strict_strtoull(buf, 10, &blocks) < 0)
2569 return -EINVAL;
2571 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2572 return -EINVAL; /* sector conversion overflow */
2574 new = blocks * 2;
2575 if (new != blocks * 2)
2576 return -EINVAL; /* unsigned long long to sector_t overflow */
2578 *sectors = new;
2579 return 0;
2582 static ssize_t
2583 rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2585 mddev_t *my_mddev = rdev->mddev;
2586 sector_t oldsectors = rdev->sectors;
2587 sector_t sectors;
2589 if (strict_blocks_to_sectors(buf, &sectors) < 0)
2590 return -EINVAL;
2591 if (my_mddev->pers && rdev->raid_disk >= 0) {
2592 if (my_mddev->persistent) {
2593 sectors = super_types[my_mddev->major_version].
2594 rdev_size_change(rdev, sectors);
2595 if (!sectors)
2596 return -EBUSY;
2597 } else if (!sectors)
2598 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
2599 rdev->data_offset;
2601 if (sectors < my_mddev->dev_sectors)
2602 return -EINVAL; /* component must fit device */
2604 rdev->sectors = sectors;
2605 if (sectors > oldsectors && my_mddev->external) {
2606 /* need to check that all other rdevs with the same ->bdev
2607 * do not overlap. We need to unlock the mddev to avoid
2608 * a deadlock. We have already changed rdev->sectors, and if
2609 * we have to change it back, we will have the lock again.
2611 mddev_t *mddev;
2612 int overlap = 0;
2613 struct list_head *tmp;
2615 mddev_unlock(my_mddev);
2616 for_each_mddev(mddev, tmp) {
2617 mdk_rdev_t *rdev2;
2619 mddev_lock(mddev);
2620 list_for_each_entry(rdev2, &mddev->disks, same_set)
2621 if (rdev->bdev == rdev2->bdev &&
2622 rdev != rdev2 &&
2623 overlaps(rdev->data_offset, rdev->sectors,
2624 rdev2->data_offset,
2625 rdev2->sectors)) {
2626 overlap = 1;
2627 break;
2629 mddev_unlock(mddev);
2630 if (overlap) {
2631 mddev_put(mddev);
2632 break;
2635 mddev_lock(my_mddev);
2636 if (overlap) {
2637 /* Someone else could have slipped in a size
2638 * change here, but doing so is just silly.
2639 * We put oldsectors back because we *know* it is
2640 * safe, and trust userspace not to race with
2641 * itself
2643 rdev->sectors = oldsectors;
2644 return -EBUSY;
2647 return len;
2650 static struct rdev_sysfs_entry rdev_size =
2651 __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
2654 static ssize_t recovery_start_show(mdk_rdev_t *rdev, char *page)
2656 unsigned long long recovery_start = rdev->recovery_offset;
2658 if (test_bit(In_sync, &rdev->flags) ||
2659 recovery_start == MaxSector)
2660 return sprintf(page, "none\n");
2662 return sprintf(page, "%llu\n", recovery_start);
2665 static ssize_t recovery_start_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2667 unsigned long long recovery_start;
2669 if (cmd_match(buf, "none"))
2670 recovery_start = MaxSector;
2671 else if (strict_strtoull(buf, 10, &recovery_start))
2672 return -EINVAL;
2674 if (rdev->mddev->pers &&
2675 rdev->raid_disk >= 0)
2676 return -EBUSY;
2678 rdev->recovery_offset = recovery_start;
2679 if (recovery_start == MaxSector)
2680 set_bit(In_sync, &rdev->flags);
2681 else
2682 clear_bit(In_sync, &rdev->flags);
2683 return len;
2686 static struct rdev_sysfs_entry rdev_recovery_start =
2687 __ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
2689 static struct attribute *rdev_default_attrs[] = {
2690 &rdev_state.attr,
2691 &rdev_errors.attr,
2692 &rdev_slot.attr,
2693 &rdev_offset.attr,
2694 &rdev_size.attr,
2695 &rdev_recovery_start.attr,
2696 NULL,
2698 static ssize_t
2699 rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2701 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2702 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
2703 mddev_t *mddev = rdev->mddev;
2704 ssize_t rv;
2706 if (!entry->show)
2707 return -EIO;
2709 rv = mddev ? mddev_lock(mddev) : -EBUSY;
2710 if (!rv) {
2711 if (rdev->mddev == NULL)
2712 rv = -EBUSY;
2713 else
2714 rv = entry->show(rdev, page);
2715 mddev_unlock(mddev);
2717 return rv;
2720 static ssize_t
2721 rdev_attr_store(struct kobject *kobj, struct attribute *attr,
2722 const char *page, size_t length)
2724 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2725 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
2726 ssize_t rv;
2727 mddev_t *mddev = rdev->mddev;
2729 if (!entry->store)
2730 return -EIO;
2731 if (!capable(CAP_SYS_ADMIN))
2732 return -EACCES;
2733 rv = mddev ? mddev_lock(mddev): -EBUSY;
2734 if (!rv) {
2735 if (rdev->mddev == NULL)
2736 rv = -EBUSY;
2737 else
2738 rv = entry->store(rdev, page, length);
2739 mddev_unlock(mddev);
2741 return rv;
2744 static void rdev_free(struct kobject *ko)
2746 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
2747 kfree(rdev);
2749 static const struct sysfs_ops rdev_sysfs_ops = {
2750 .show = rdev_attr_show,
2751 .store = rdev_attr_store,
2753 static struct kobj_type rdev_ktype = {
2754 .release = rdev_free,
2755 .sysfs_ops = &rdev_sysfs_ops,
2756 .default_attrs = rdev_default_attrs,
2759 void md_rdev_init(mdk_rdev_t *rdev)
2761 rdev->desc_nr = -1;
2762 rdev->saved_raid_disk = -1;
2763 rdev->raid_disk = -1;
2764 rdev->flags = 0;
2765 rdev->data_offset = 0;
2766 rdev->sb_events = 0;
2767 rdev->last_read_error.tv_sec = 0;
2768 rdev->last_read_error.tv_nsec = 0;
2769 atomic_set(&rdev->nr_pending, 0);
2770 atomic_set(&rdev->read_errors, 0);
2771 atomic_set(&rdev->corrected_errors, 0);
2773 INIT_LIST_HEAD(&rdev->same_set);
2774 init_waitqueue_head(&rdev->blocked_wait);
2776 EXPORT_SYMBOL_GPL(md_rdev_init);
2778 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2780 * mark the device faulty if:
2782 * - the device is nonexistent (zero size)
2783 * - the device has no valid superblock
2785 * a faulty rdev _never_ has rdev->sb set.
2787 static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
2789 char b[BDEVNAME_SIZE];
2790 int err;
2791 mdk_rdev_t *rdev;
2792 sector_t size;
2794 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
2795 if (!rdev) {
2796 printk(KERN_ERR "md: could not alloc mem for new device!\n");
2797 return ERR_PTR(-ENOMEM);
2800 md_rdev_init(rdev);
2801 if ((err = alloc_disk_sb(rdev)))
2802 goto abort_free;
2804 err = lock_rdev(rdev, newdev, super_format == -2);
2805 if (err)
2806 goto abort_free;
2808 kobject_init(&rdev->kobj, &rdev_ktype);
2810 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
2811 if (!size) {
2812 printk(KERN_WARNING
2813 "md: %s has zero or unknown size, marking faulty!\n",
2814 bdevname(rdev->bdev,b));
2815 err = -EINVAL;
2816 goto abort_free;
2819 if (super_format >= 0) {
2820 err = super_types[super_format].
2821 load_super(rdev, NULL, super_minor);
2822 if (err == -EINVAL) {
2823 printk(KERN_WARNING
2824 "md: %s does not have a valid v%d.%d "
2825 "superblock, not importing!\n",
2826 bdevname(rdev->bdev,b),
2827 super_format, super_minor);
2828 goto abort_free;
2830 if (err < 0) {
2831 printk(KERN_WARNING
2832 "md: could not read %s's sb, not importing!\n",
2833 bdevname(rdev->bdev,b));
2834 goto abort_free;
2838 return rdev;
2840 abort_free:
2841 if (rdev->sb_page) {
2842 if (rdev->bdev)
2843 unlock_rdev(rdev);
2844 free_disk_sb(rdev);
2846 kfree(rdev);
2847 return ERR_PTR(err);
2851 * Check a full RAID array for plausibility
2855 static void analyze_sbs(mddev_t * mddev)
2857 int i;
2858 mdk_rdev_t *rdev, *freshest, *tmp;
2859 char b[BDEVNAME_SIZE];
2861 freshest = NULL;
2862 rdev_for_each(rdev, tmp, mddev)
2863 switch (super_types[mddev->major_version].
2864 load_super(rdev, freshest, mddev->minor_version)) {
2865 case 1:
2866 freshest = rdev;
2867 break;
2868 case 0:
2869 break;
2870 default:
2871 printk( KERN_ERR \
2872 "md: fatal superblock inconsistency in %s"
2873 " -- removing from array\n",
2874 bdevname(rdev->bdev,b));
2875 kick_rdev_from_array(rdev);
2879 super_types[mddev->major_version].
2880 validate_super(mddev, freshest);
2882 i = 0;
2883 rdev_for_each(rdev, tmp, mddev) {
2884 if (mddev->max_disks &&
2885 (rdev->desc_nr >= mddev->max_disks ||
2886 i > mddev->max_disks)) {
2887 printk(KERN_WARNING
2888 "md: %s: %s: only %d devices permitted\n",
2889 mdname(mddev), bdevname(rdev->bdev, b),
2890 mddev->max_disks);
2891 kick_rdev_from_array(rdev);
2892 continue;
2894 if (rdev != freshest)
2895 if (super_types[mddev->major_version].
2896 validate_super(mddev, rdev)) {
2897 printk(KERN_WARNING "md: kicking non-fresh %s"
2898 " from array!\n",
2899 bdevname(rdev->bdev,b));
2900 kick_rdev_from_array(rdev);
2901 continue;
2903 if (mddev->level == LEVEL_MULTIPATH) {
2904 rdev->desc_nr = i++;
2905 rdev->raid_disk = rdev->desc_nr;
2906 set_bit(In_sync, &rdev->flags);
2907 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
2908 rdev->raid_disk = -1;
2909 clear_bit(In_sync, &rdev->flags);
2914 /* Read a fixed-point number.
2915 * Numbers in sysfs attributes should be in "standard" units where
2916 * possible, so time should be in seconds.
2917 * However we internally use a a much smaller unit such as
2918 * milliseconds or jiffies.
2919 * This function takes a decimal number with a possible fractional
2920 * component, and produces an integer which is the result of
2921 * multiplying that number by 10^'scale'.
2922 * all without any floating-point arithmetic.
2924 int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
2926 unsigned long result = 0;
2927 long decimals = -1;
2928 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
2929 if (*cp == '.')
2930 decimals = 0;
2931 else if (decimals < scale) {
2932 unsigned int value;
2933 value = *cp - '0';
2934 result = result * 10 + value;
2935 if (decimals >= 0)
2936 decimals++;
2938 cp++;
2940 if (*cp == '\n')
2941 cp++;
2942 if (*cp)
2943 return -EINVAL;
2944 if (decimals < 0)
2945 decimals = 0;
2946 while (decimals < scale) {
2947 result *= 10;
2948 decimals ++;
2950 *res = result;
2951 return 0;
2955 static void md_safemode_timeout(unsigned long data);
2957 static ssize_t
2958 safe_delay_show(mddev_t *mddev, char *page)
2960 int msec = (mddev->safemode_delay*1000)/HZ;
2961 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
2963 static ssize_t
2964 safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
2966 unsigned long msec;
2968 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
2969 return -EINVAL;
2970 if (msec == 0)
2971 mddev->safemode_delay = 0;
2972 else {
2973 unsigned long old_delay = mddev->safemode_delay;
2974 mddev->safemode_delay = (msec*HZ)/1000;
2975 if (mddev->safemode_delay == 0)
2976 mddev->safemode_delay = 1;
2977 if (mddev->safemode_delay < old_delay)
2978 md_safemode_timeout((unsigned long)mddev);
2980 return len;
2982 static struct md_sysfs_entry md_safe_delay =
2983 __ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
2985 static ssize_t
2986 level_show(mddev_t *mddev, char *page)
2988 struct mdk_personality *p = mddev->pers;
2989 if (p)
2990 return sprintf(page, "%s\n", p->name);
2991 else if (mddev->clevel[0])
2992 return sprintf(page, "%s\n", mddev->clevel);
2993 else if (mddev->level != LEVEL_NONE)
2994 return sprintf(page, "%d\n", mddev->level);
2995 else
2996 return 0;
2999 static ssize_t
3000 level_store(mddev_t *mddev, const char *buf, size_t len)
3002 char clevel[16];
3003 ssize_t rv = len;
3004 struct mdk_personality *pers;
3005 long level;
3006 void *priv;
3007 mdk_rdev_t *rdev;
3009 if (mddev->pers == NULL) {
3010 if (len == 0)
3011 return 0;
3012 if (len >= sizeof(mddev->clevel))
3013 return -ENOSPC;
3014 strncpy(mddev->clevel, buf, len);
3015 if (mddev->clevel[len-1] == '\n')
3016 len--;
3017 mddev->clevel[len] = 0;
3018 mddev->level = LEVEL_NONE;
3019 return rv;
3022 /* request to change the personality. Need to ensure:
3023 * - array is not engaged in resync/recovery/reshape
3024 * - old personality can be suspended
3025 * - new personality will access other array.
3028 if (mddev->sync_thread ||
3029 mddev->reshape_position != MaxSector ||
3030 mddev->sysfs_active)
3031 return -EBUSY;
3033 if (!mddev->pers->quiesce) {
3034 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
3035 mdname(mddev), mddev->pers->name);
3036 return -EINVAL;
3039 /* Now find the new personality */
3040 if (len == 0 || len >= sizeof(clevel))
3041 return -EINVAL;
3042 strncpy(clevel, buf, len);
3043 if (clevel[len-1] == '\n')
3044 len--;
3045 clevel[len] = 0;
3046 if (strict_strtol(clevel, 10, &level))
3047 level = LEVEL_NONE;
3049 if (request_module("md-%s", clevel) != 0)
3050 request_module("md-level-%s", clevel);
3051 spin_lock(&pers_lock);
3052 pers = find_pers(level, clevel);
3053 if (!pers || !try_module_get(pers->owner)) {
3054 spin_unlock(&pers_lock);
3055 printk(KERN_WARNING "md: personality %s not loaded\n", clevel);
3056 return -EINVAL;
3058 spin_unlock(&pers_lock);
3060 if (pers == mddev->pers) {
3061 /* Nothing to do! */
3062 module_put(pers->owner);
3063 return rv;
3065 if (!pers->takeover) {
3066 module_put(pers->owner);
3067 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
3068 mdname(mddev), clevel);
3069 return -EINVAL;
3072 list_for_each_entry(rdev, &mddev->disks, same_set)
3073 rdev->new_raid_disk = rdev->raid_disk;
3075 /* ->takeover must set new_* and/or delta_disks
3076 * if it succeeds, and may set them when it fails.
3078 priv = pers->takeover(mddev);
3079 if (IS_ERR(priv)) {
3080 mddev->new_level = mddev->level;
3081 mddev->new_layout = mddev->layout;
3082 mddev->new_chunk_sectors = mddev->chunk_sectors;
3083 mddev->raid_disks -= mddev->delta_disks;
3084 mddev->delta_disks = 0;
3085 module_put(pers->owner);
3086 printk(KERN_WARNING "md: %s: %s would not accept array\n",
3087 mdname(mddev), clevel);
3088 return PTR_ERR(priv);
3091 /* Looks like we have a winner */
3092 mddev_suspend(mddev);
3093 mddev->pers->stop(mddev);
3095 if (mddev->pers->sync_request == NULL &&
3096 pers->sync_request != NULL) {
3097 /* need to add the md_redundancy_group */
3098 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3099 printk(KERN_WARNING
3100 "md: cannot register extra attributes for %s\n",
3101 mdname(mddev));
3102 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, NULL, "sync_action");
3104 if (mddev->pers->sync_request != NULL &&
3105 pers->sync_request == NULL) {
3106 /* need to remove the md_redundancy_group */
3107 if (mddev->to_remove == NULL)
3108 mddev->to_remove = &md_redundancy_group;
3111 if (mddev->pers->sync_request == NULL &&
3112 mddev->external) {
3113 /* We are converting from a no-redundancy array
3114 * to a redundancy array and metadata is managed
3115 * externally so we need to be sure that writes
3116 * won't block due to a need to transition
3117 * clean->dirty
3118 * until external management is started.
3120 mddev->in_sync = 0;
3121 mddev->safemode_delay = 0;
3122 mddev->safemode = 0;
3125 list_for_each_entry(rdev, &mddev->disks, same_set) {
3126 char nm[20];
3127 if (rdev->raid_disk < 0)
3128 continue;
3129 if (rdev->new_raid_disk >= mddev->raid_disks)
3130 rdev->new_raid_disk = -1;
3131 if (rdev->new_raid_disk == rdev->raid_disk)
3132 continue;
3133 sprintf(nm, "rd%d", rdev->raid_disk);
3134 sysfs_remove_link(&mddev->kobj, nm);
3136 list_for_each_entry(rdev, &mddev->disks, same_set) {
3137 if (rdev->raid_disk < 0)
3138 continue;
3139 if (rdev->new_raid_disk == rdev->raid_disk)
3140 continue;
3141 rdev->raid_disk = rdev->new_raid_disk;
3142 if (rdev->raid_disk < 0)
3143 clear_bit(In_sync, &rdev->flags);
3144 else {
3145 char nm[20];
3146 sprintf(nm, "rd%d", rdev->raid_disk);
3147 if(sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
3148 printk("md: cannot register %s for %s after level change\n",
3149 nm, mdname(mddev));
3153 module_put(mddev->pers->owner);
3154 mddev->pers = pers;
3155 mddev->private = priv;
3156 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3157 mddev->level = mddev->new_level;
3158 mddev->layout = mddev->new_layout;
3159 mddev->chunk_sectors = mddev->new_chunk_sectors;
3160 mddev->delta_disks = 0;
3161 if (mddev->pers->sync_request == NULL) {
3162 /* this is now an array without redundancy, so
3163 * it must always be in_sync
3165 mddev->in_sync = 1;
3166 del_timer_sync(&mddev->safemode_timer);
3168 pers->run(mddev);
3169 mddev_resume(mddev);
3170 set_bit(MD_CHANGE_DEVS, &mddev->flags);
3171 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3172 md_wakeup_thread(mddev->thread);
3173 sysfs_notify(&mddev->kobj, NULL, "level");
3174 md_new_event(mddev);
3175 return rv;
3178 static struct md_sysfs_entry md_level =
3179 __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
3182 static ssize_t
3183 layout_show(mddev_t *mddev, char *page)
3185 /* just a number, not meaningful for all levels */
3186 if (mddev->reshape_position != MaxSector &&
3187 mddev->layout != mddev->new_layout)
3188 return sprintf(page, "%d (%d)\n",
3189 mddev->new_layout, mddev->layout);
3190 return sprintf(page, "%d\n", mddev->layout);
3193 static ssize_t
3194 layout_store(mddev_t *mddev, const char *buf, size_t len)
3196 char *e;
3197 unsigned long n = simple_strtoul(buf, &e, 10);
3199 if (!*buf || (*e && *e != '\n'))
3200 return -EINVAL;
3202 if (mddev->pers) {
3203 int err;
3204 if (mddev->pers->check_reshape == NULL)
3205 return -EBUSY;
3206 mddev->new_layout = n;
3207 err = mddev->pers->check_reshape(mddev);
3208 if (err) {
3209 mddev->new_layout = mddev->layout;
3210 return err;
3212 } else {
3213 mddev->new_layout = n;
3214 if (mddev->reshape_position == MaxSector)
3215 mddev->layout = n;
3217 return len;
3219 static struct md_sysfs_entry md_layout =
3220 __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
3223 static ssize_t
3224 raid_disks_show(mddev_t *mddev, char *page)
3226 if (mddev->raid_disks == 0)
3227 return 0;
3228 if (mddev->reshape_position != MaxSector &&
3229 mddev->delta_disks != 0)
3230 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3231 mddev->raid_disks - mddev->delta_disks);
3232 return sprintf(page, "%d\n", mddev->raid_disks);
3235 static int update_raid_disks(mddev_t *mddev, int raid_disks);
3237 static ssize_t
3238 raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
3240 char *e;
3241 int rv = 0;
3242 unsigned long n = simple_strtoul(buf, &e, 10);
3244 if (!*buf || (*e && *e != '\n'))
3245 return -EINVAL;
3247 if (mddev->pers)
3248 rv = update_raid_disks(mddev, n);
3249 else if (mddev->reshape_position != MaxSector) {
3250 int olddisks = mddev->raid_disks - mddev->delta_disks;
3251 mddev->delta_disks = n - olddisks;
3252 mddev->raid_disks = n;
3253 } else
3254 mddev->raid_disks = n;
3255 return rv ? rv : len;
3257 static struct md_sysfs_entry md_raid_disks =
3258 __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
3260 static ssize_t
3261 chunk_size_show(mddev_t *mddev, char *page)
3263 if (mddev->reshape_position != MaxSector &&
3264 mddev->chunk_sectors != mddev->new_chunk_sectors)
3265 return sprintf(page, "%d (%d)\n",
3266 mddev->new_chunk_sectors << 9,
3267 mddev->chunk_sectors << 9);
3268 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
3271 static ssize_t
3272 chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
3274 char *e;
3275 unsigned long n = simple_strtoul(buf, &e, 10);
3277 if (!*buf || (*e && *e != '\n'))
3278 return -EINVAL;
3280 if (mddev->pers) {
3281 int err;
3282 if (mddev->pers->check_reshape == NULL)
3283 return -EBUSY;
3284 mddev->new_chunk_sectors = n >> 9;
3285 err = mddev->pers->check_reshape(mddev);
3286 if (err) {
3287 mddev->new_chunk_sectors = mddev->chunk_sectors;
3288 return err;
3290 } else {
3291 mddev->new_chunk_sectors = n >> 9;
3292 if (mddev->reshape_position == MaxSector)
3293 mddev->chunk_sectors = n >> 9;
3295 return len;
3297 static struct md_sysfs_entry md_chunk_size =
3298 __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
3300 static ssize_t
3301 resync_start_show(mddev_t *mddev, char *page)
3303 if (mddev->recovery_cp == MaxSector)
3304 return sprintf(page, "none\n");
3305 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3308 static ssize_t
3309 resync_start_store(mddev_t *mddev, const char *buf, size_t len)
3311 char *e;
3312 unsigned long long n = simple_strtoull(buf, &e, 10);
3314 if (mddev->pers)
3315 return -EBUSY;
3316 if (cmd_match(buf, "none"))
3317 n = MaxSector;
3318 else if (!*buf || (*e && *e != '\n'))
3319 return -EINVAL;
3321 mddev->recovery_cp = n;
3322 return len;
3324 static struct md_sysfs_entry md_resync_start =
3325 __ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
3328 * The array state can be:
3330 * clear
3331 * No devices, no size, no level
3332 * Equivalent to STOP_ARRAY ioctl
3333 * inactive
3334 * May have some settings, but array is not active
3335 * all IO results in error
3336 * When written, doesn't tear down array, but just stops it
3337 * suspended (not supported yet)
3338 * All IO requests will block. The array can be reconfigured.
3339 * Writing this, if accepted, will block until array is quiescent
3340 * readonly
3341 * no resync can happen. no superblocks get written.
3342 * write requests fail
3343 * read-auto
3344 * like readonly, but behaves like 'clean' on a write request.
3346 * clean - no pending writes, but otherwise active.
3347 * When written to inactive array, starts without resync
3348 * If a write request arrives then
3349 * if metadata is known, mark 'dirty' and switch to 'active'.
3350 * if not known, block and switch to write-pending
3351 * If written to an active array that has pending writes, then fails.
3352 * active
3353 * fully active: IO and resync can be happening.
3354 * When written to inactive array, starts with resync
3356 * write-pending
3357 * clean, but writes are blocked waiting for 'active' to be written.
3359 * active-idle
3360 * like active, but no writes have been seen for a while (100msec).
3363 enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3364 write_pending, active_idle, bad_word};
3365 static char *array_states[] = {
3366 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3367 "write-pending", "active-idle", NULL };
3369 static int match_word(const char *word, char **list)
3371 int n;
3372 for (n=0; list[n]; n++)
3373 if (cmd_match(word, list[n]))
3374 break;
3375 return n;
3378 static ssize_t
3379 array_state_show(mddev_t *mddev, char *page)
3381 enum array_state st = inactive;
3383 if (mddev->pers)
3384 switch(mddev->ro) {
3385 case 1:
3386 st = readonly;
3387 break;
3388 case 2:
3389 st = read_auto;
3390 break;
3391 case 0:
3392 if (mddev->in_sync)
3393 st = clean;
3394 else if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
3395 st = write_pending;
3396 else if (mddev->safemode)
3397 st = active_idle;
3398 else
3399 st = active;
3401 else {
3402 if (list_empty(&mddev->disks) &&
3403 mddev->raid_disks == 0 &&
3404 mddev->dev_sectors == 0)
3405 st = clear;
3406 else
3407 st = inactive;
3409 return sprintf(page, "%s\n", array_states[st]);
3412 static int do_md_stop(mddev_t * mddev, int ro, int is_open);
3413 static int md_set_readonly(mddev_t * mddev, int is_open);
3414 static int do_md_run(mddev_t * mddev);
3415 static int restart_array(mddev_t *mddev);
3417 static ssize_t
3418 array_state_store(mddev_t *mddev, const char *buf, size_t len)
3420 int err = -EINVAL;
3421 enum array_state st = match_word(buf, array_states);
3422 switch(st) {
3423 case bad_word:
3424 break;
3425 case clear:
3426 /* stopping an active array */
3427 if (atomic_read(&mddev->openers) > 0)
3428 return -EBUSY;
3429 err = do_md_stop(mddev, 0, 0);
3430 break;
3431 case inactive:
3432 /* stopping an active array */
3433 if (mddev->pers) {
3434 if (atomic_read(&mddev->openers) > 0)
3435 return -EBUSY;
3436 err = do_md_stop(mddev, 2, 0);
3437 } else
3438 err = 0; /* already inactive */
3439 break;
3440 case suspended:
3441 break; /* not supported yet */
3442 case readonly:
3443 if (mddev->pers)
3444 err = md_set_readonly(mddev, 0);
3445 else {
3446 mddev->ro = 1;
3447 set_disk_ro(mddev->gendisk, 1);
3448 err = do_md_run(mddev);
3450 break;
3451 case read_auto:
3452 if (mddev->pers) {
3453 if (mddev->ro == 0)
3454 err = md_set_readonly(mddev, 0);
3455 else if (mddev->ro == 1)
3456 err = restart_array(mddev);
3457 if (err == 0) {
3458 mddev->ro = 2;
3459 set_disk_ro(mddev->gendisk, 0);
3461 } else {
3462 mddev->ro = 2;
3463 err = do_md_run(mddev);
3465 break;
3466 case clean:
3467 if (mddev->pers) {
3468 restart_array(mddev);
3469 spin_lock_irq(&mddev->write_lock);
3470 if (atomic_read(&mddev->writes_pending) == 0) {
3471 if (mddev->in_sync == 0) {
3472 mddev->in_sync = 1;
3473 if (mddev->safemode == 1)
3474 mddev->safemode = 0;
3475 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
3477 err = 0;
3478 } else
3479 err = -EBUSY;
3480 spin_unlock_irq(&mddev->write_lock);
3481 } else
3482 err = -EINVAL;
3483 break;
3484 case active:
3485 if (mddev->pers) {
3486 restart_array(mddev);
3487 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
3488 wake_up(&mddev->sb_wait);
3489 err = 0;
3490 } else {
3491 mddev->ro = 0;
3492 set_disk_ro(mddev->gendisk, 0);
3493 err = do_md_run(mddev);
3495 break;
3496 case write_pending:
3497 case active_idle:
3498 /* these cannot be set */
3499 break;
3501 if (err)
3502 return err;
3503 else {
3504 sysfs_notify_dirent_safe(mddev->sysfs_state);
3505 return len;
3508 static struct md_sysfs_entry md_array_state =
3509 __ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
3511 static ssize_t
3512 max_corrected_read_errors_show(mddev_t *mddev, char *page) {
3513 return sprintf(page, "%d\n",
3514 atomic_read(&mddev->max_corr_read_errors));
3517 static ssize_t
3518 max_corrected_read_errors_store(mddev_t *mddev, const char *buf, size_t len)
3520 char *e;
3521 unsigned long n = simple_strtoul(buf, &e, 10);
3523 if (*buf && (*e == 0 || *e == '\n')) {
3524 atomic_set(&mddev->max_corr_read_errors, n);
3525 return len;
3527 return -EINVAL;
3530 static struct md_sysfs_entry max_corr_read_errors =
3531 __ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
3532 max_corrected_read_errors_store);
3534 static ssize_t
3535 null_show(mddev_t *mddev, char *page)
3537 return -EINVAL;
3540 static ssize_t
3541 new_dev_store(mddev_t *mddev, const char *buf, size_t len)
3543 /* buf must be %d:%d\n? giving major and minor numbers */
3544 /* The new device is added to the array.
3545 * If the array has a persistent superblock, we read the
3546 * superblock to initialise info and check validity.
3547 * Otherwise, only checking done is that in bind_rdev_to_array,
3548 * which mainly checks size.
3550 char *e;
3551 int major = simple_strtoul(buf, &e, 10);
3552 int minor;
3553 dev_t dev;
3554 mdk_rdev_t *rdev;
3555 int err;
3557 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
3558 return -EINVAL;
3559 minor = simple_strtoul(e+1, &e, 10);
3560 if (*e && *e != '\n')
3561 return -EINVAL;
3562 dev = MKDEV(major, minor);
3563 if (major != MAJOR(dev) ||
3564 minor != MINOR(dev))
3565 return -EOVERFLOW;
3568 if (mddev->persistent) {
3569 rdev = md_import_device(dev, mddev->major_version,
3570 mddev->minor_version);
3571 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
3572 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
3573 mdk_rdev_t, same_set);
3574 err = super_types[mddev->major_version]
3575 .load_super(rdev, rdev0, mddev->minor_version);
3576 if (err < 0)
3577 goto out;
3579 } else if (mddev->external)
3580 rdev = md_import_device(dev, -2, -1);
3581 else
3582 rdev = md_import_device(dev, -1, -1);
3584 if (IS_ERR(rdev))
3585 return PTR_ERR(rdev);
3586 err = bind_rdev_to_array(rdev, mddev);
3587 out:
3588 if (err)
3589 export_rdev(rdev);
3590 return err ? err : len;
3593 static struct md_sysfs_entry md_new_device =
3594 __ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
3596 static ssize_t
3597 bitmap_store(mddev_t *mddev, const char *buf, size_t len)
3599 char *end;
3600 unsigned long chunk, end_chunk;
3602 if (!mddev->bitmap)
3603 goto out;
3604 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3605 while (*buf) {
3606 chunk = end_chunk = simple_strtoul(buf, &end, 0);
3607 if (buf == end) break;
3608 if (*end == '-') { /* range */
3609 buf = end + 1;
3610 end_chunk = simple_strtoul(buf, &end, 0);
3611 if (buf == end) break;
3613 if (*end && !isspace(*end)) break;
3614 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
3615 buf = skip_spaces(end);
3617 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
3618 out:
3619 return len;
3622 static struct md_sysfs_entry md_bitmap =
3623 __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
3625 static ssize_t
3626 size_show(mddev_t *mddev, char *page)
3628 return sprintf(page, "%llu\n",
3629 (unsigned long long)mddev->dev_sectors / 2);
3632 static int update_size(mddev_t *mddev, sector_t num_sectors);
3634 static ssize_t
3635 size_store(mddev_t *mddev, const char *buf, size_t len)
3637 /* If array is inactive, we can reduce the component size, but
3638 * not increase it (except from 0).
3639 * If array is active, we can try an on-line resize
3641 sector_t sectors;
3642 int err = strict_blocks_to_sectors(buf, &sectors);
3644 if (err < 0)
3645 return err;
3646 if (mddev->pers) {
3647 err = update_size(mddev, sectors);
3648 md_update_sb(mddev, 1);
3649 } else {
3650 if (mddev->dev_sectors == 0 ||
3651 mddev->dev_sectors > sectors)
3652 mddev->dev_sectors = sectors;
3653 else
3654 err = -ENOSPC;
3656 return err ? err : len;
3659 static struct md_sysfs_entry md_size =
3660 __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
3663 /* Metdata version.
3664 * This is one of
3665 * 'none' for arrays with no metadata (good luck...)
3666 * 'external' for arrays with externally managed metadata,
3667 * or N.M for internally known formats
3669 static ssize_t
3670 metadata_show(mddev_t *mddev, char *page)
3672 if (mddev->persistent)
3673 return sprintf(page, "%d.%d\n",
3674 mddev->major_version, mddev->minor_version);
3675 else if (mddev->external)
3676 return sprintf(page, "external:%s\n", mddev->metadata_type);
3677 else
3678 return sprintf(page, "none\n");
3681 static ssize_t
3682 metadata_store(mddev_t *mddev, const char *buf, size_t len)
3684 int major, minor;
3685 char *e;
3686 /* Changing the details of 'external' metadata is
3687 * always permitted. Otherwise there must be
3688 * no devices attached to the array.
3690 if (mddev->external && strncmp(buf, "external:", 9) == 0)
3692 else if (!list_empty(&mddev->disks))
3693 return -EBUSY;
3695 if (cmd_match(buf, "none")) {
3696 mddev->persistent = 0;
3697 mddev->external = 0;
3698 mddev->major_version = 0;
3699 mddev->minor_version = 90;
3700 return len;
3702 if (strncmp(buf, "external:", 9) == 0) {
3703 size_t namelen = len-9;
3704 if (namelen >= sizeof(mddev->metadata_type))
3705 namelen = sizeof(mddev->metadata_type)-1;
3706 strncpy(mddev->metadata_type, buf+9, namelen);
3707 mddev->metadata_type[namelen] = 0;
3708 if (namelen && mddev->metadata_type[namelen-1] == '\n')
3709 mddev->metadata_type[--namelen] = 0;
3710 mddev->persistent = 0;
3711 mddev->external = 1;
3712 mddev->major_version = 0;
3713 mddev->minor_version = 90;
3714 return len;
3716 major = simple_strtoul(buf, &e, 10);
3717 if (e==buf || *e != '.')
3718 return -EINVAL;
3719 buf = e+1;
3720 minor = simple_strtoul(buf, &e, 10);
3721 if (e==buf || (*e && *e != '\n') )
3722 return -EINVAL;
3723 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
3724 return -ENOENT;
3725 mddev->major_version = major;
3726 mddev->minor_version = minor;
3727 mddev->persistent = 1;
3728 mddev->external = 0;
3729 return len;
3732 static struct md_sysfs_entry md_metadata =
3733 __ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
3735 static ssize_t
3736 action_show(mddev_t *mddev, char *page)
3738 char *type = "idle";
3739 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
3740 type = "frozen";
3741 else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3742 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
3743 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
3744 type = "reshape";
3745 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
3746 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
3747 type = "resync";
3748 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
3749 type = "check";
3750 else
3751 type = "repair";
3752 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
3753 type = "recover";
3755 return sprintf(page, "%s\n", type);
3758 static void reap_sync_thread(mddev_t *mddev);
3760 static ssize_t
3761 action_store(mddev_t *mddev, const char *page, size_t len)
3763 if (!mddev->pers || !mddev->pers->sync_request)
3764 return -EINVAL;
3766 if (cmd_match(page, "frozen"))
3767 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3768 else
3769 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3771 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
3772 if (mddev->sync_thread) {
3773 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3774 reap_sync_thread(mddev);
3776 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3777 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
3778 return -EBUSY;
3779 else if (cmd_match(page, "resync"))
3780 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3781 else if (cmd_match(page, "recover")) {
3782 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
3783 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3784 } else if (cmd_match(page, "reshape")) {
3785 int err;
3786 if (mddev->pers->start_reshape == NULL)
3787 return -EINVAL;
3788 err = mddev->pers->start_reshape(mddev);
3789 if (err)
3790 return err;
3791 sysfs_notify(&mddev->kobj, NULL, "degraded");
3792 } else {
3793 if (cmd_match(page, "check"))
3794 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
3795 else if (!cmd_match(page, "repair"))
3796 return -EINVAL;
3797 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
3798 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
3800 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3801 md_wakeup_thread(mddev->thread);
3802 sysfs_notify_dirent_safe(mddev->sysfs_action);
3803 return len;
3806 static ssize_t
3807 mismatch_cnt_show(mddev_t *mddev, char *page)
3809 return sprintf(page, "%llu\n",
3810 (unsigned long long) mddev->resync_mismatches);
3813 static struct md_sysfs_entry md_scan_mode =
3814 __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
3817 static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
3819 static ssize_t
3820 sync_min_show(mddev_t *mddev, char *page)
3822 return sprintf(page, "%d (%s)\n", speed_min(mddev),
3823 mddev->sync_speed_min ? "local": "system");
3826 static ssize_t
3827 sync_min_store(mddev_t *mddev, const char *buf, size_t len)
3829 int min;
3830 char *e;
3831 if (strncmp(buf, "system", 6)==0) {
3832 mddev->sync_speed_min = 0;
3833 return len;
3835 min = simple_strtoul(buf, &e, 10);
3836 if (buf == e || (*e && *e != '\n') || min <= 0)
3837 return -EINVAL;
3838 mddev->sync_speed_min = min;
3839 return len;
3842 static struct md_sysfs_entry md_sync_min =
3843 __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
3845 static ssize_t
3846 sync_max_show(mddev_t *mddev, char *page)
3848 return sprintf(page, "%d (%s)\n", speed_max(mddev),
3849 mddev->sync_speed_max ? "local": "system");
3852 static ssize_t
3853 sync_max_store(mddev_t *mddev, const char *buf, size_t len)
3855 int max;
3856 char *e;
3857 if (strncmp(buf, "system", 6)==0) {
3858 mddev->sync_speed_max = 0;
3859 return len;
3861 max = simple_strtoul(buf, &e, 10);
3862 if (buf == e || (*e && *e != '\n') || max <= 0)
3863 return -EINVAL;
3864 mddev->sync_speed_max = max;
3865 return len;
3868 static struct md_sysfs_entry md_sync_max =
3869 __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
3871 static ssize_t
3872 degraded_show(mddev_t *mddev, char *page)
3874 return sprintf(page, "%d\n", mddev->degraded);
3876 static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
3878 static ssize_t
3879 sync_force_parallel_show(mddev_t *mddev, char *page)
3881 return sprintf(page, "%d\n", mddev->parallel_resync);
3884 static ssize_t
3885 sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len)
3887 long n;
3889 if (strict_strtol(buf, 10, &n))
3890 return -EINVAL;
3892 if (n != 0 && n != 1)
3893 return -EINVAL;
3895 mddev->parallel_resync = n;
3897 if (mddev->sync_thread)
3898 wake_up(&resync_wait);
3900 return len;
3903 /* force parallel resync, even with shared block devices */
3904 static struct md_sysfs_entry md_sync_force_parallel =
3905 __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
3906 sync_force_parallel_show, sync_force_parallel_store);
3908 static ssize_t
3909 sync_speed_show(mddev_t *mddev, char *page)
3911 unsigned long resync, dt, db;
3912 if (mddev->curr_resync == 0)
3913 return sprintf(page, "none\n");
3914 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
3915 dt = (jiffies - mddev->resync_mark) / HZ;
3916 if (!dt) dt++;
3917 db = resync - mddev->resync_mark_cnt;
3918 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
3921 static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
3923 static ssize_t
3924 sync_completed_show(mddev_t *mddev, char *page)
3926 unsigned long long max_sectors, resync;
3928 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3929 return sprintf(page, "none\n");
3931 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
3932 max_sectors = mddev->resync_max_sectors;
3933 else
3934 max_sectors = mddev->dev_sectors;
3936 resync = mddev->curr_resync_completed;
3937 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
3940 static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
3942 static ssize_t
3943 min_sync_show(mddev_t *mddev, char *page)
3945 return sprintf(page, "%llu\n",
3946 (unsigned long long)mddev->resync_min);
3948 static ssize_t
3949 min_sync_store(mddev_t *mddev, const char *buf, size_t len)
3951 unsigned long long min;
3952 if (strict_strtoull(buf, 10, &min))
3953 return -EINVAL;
3954 if (min > mddev->resync_max)
3955 return -EINVAL;
3956 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3957 return -EBUSY;
3959 /* Must be a multiple of chunk_size */
3960 if (mddev->chunk_sectors) {
3961 sector_t temp = min;
3962 if (sector_div(temp, mddev->chunk_sectors))
3963 return -EINVAL;
3965 mddev->resync_min = min;
3967 return len;
3970 static struct md_sysfs_entry md_min_sync =
3971 __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
3973 static ssize_t
3974 max_sync_show(mddev_t *mddev, char *page)
3976 if (mddev->resync_max == MaxSector)
3977 return sprintf(page, "max\n");
3978 else
3979 return sprintf(page, "%llu\n",
3980 (unsigned long long)mddev->resync_max);
3982 static ssize_t
3983 max_sync_store(mddev_t *mddev, const char *buf, size_t len)
3985 if (strncmp(buf, "max", 3) == 0)
3986 mddev->resync_max = MaxSector;
3987 else {
3988 unsigned long long max;
3989 if (strict_strtoull(buf, 10, &max))
3990 return -EINVAL;
3991 if (max < mddev->resync_min)
3992 return -EINVAL;
3993 if (max < mddev->resync_max &&
3994 mddev->ro == 0 &&
3995 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3996 return -EBUSY;
3998 /* Must be a multiple of chunk_size */
3999 if (mddev->chunk_sectors) {
4000 sector_t temp = max;
4001 if (sector_div(temp, mddev->chunk_sectors))
4002 return -EINVAL;
4004 mddev->resync_max = max;
4006 wake_up(&mddev->recovery_wait);
4007 return len;
4010 static struct md_sysfs_entry md_max_sync =
4011 __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4013 static ssize_t
4014 suspend_lo_show(mddev_t *mddev, char *page)
4016 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4019 static ssize_t
4020 suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
4022 char *e;
4023 unsigned long long new = simple_strtoull(buf, &e, 10);
4024 unsigned long long old = mddev->suspend_lo;
4026 if (mddev->pers == NULL ||
4027 mddev->pers->quiesce == NULL)
4028 return -EINVAL;
4029 if (buf == e || (*e && *e != '\n'))
4030 return -EINVAL;
4032 mddev->suspend_lo = new;
4033 if (new >= old)
4034 /* Shrinking suspended region */
4035 mddev->pers->quiesce(mddev, 2);
4036 else {
4037 /* Expanding suspended region - need to wait */
4038 mddev->pers->quiesce(mddev, 1);
4039 mddev->pers->quiesce(mddev, 0);
4041 return len;
4043 static struct md_sysfs_entry md_suspend_lo =
4044 __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4047 static ssize_t
4048 suspend_hi_show(mddev_t *mddev, char *page)
4050 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4053 static ssize_t
4054 suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
4056 char *e;
4057 unsigned long long new = simple_strtoull(buf, &e, 10);
4058 unsigned long long old = mddev->suspend_hi;
4060 if (mddev->pers == NULL ||
4061 mddev->pers->quiesce == NULL)
4062 return -EINVAL;
4063 if (buf == e || (*e && *e != '\n'))
4064 return -EINVAL;
4066 mddev->suspend_hi = new;
4067 if (new <= old)
4068 /* Shrinking suspended region */
4069 mddev->pers->quiesce(mddev, 2);
4070 else {
4071 /* Expanding suspended region - need to wait */
4072 mddev->pers->quiesce(mddev, 1);
4073 mddev->pers->quiesce(mddev, 0);
4075 return len;
4077 static struct md_sysfs_entry md_suspend_hi =
4078 __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4080 static ssize_t
4081 reshape_position_show(mddev_t *mddev, char *page)
4083 if (mddev->reshape_position != MaxSector)
4084 return sprintf(page, "%llu\n",
4085 (unsigned long long)mddev->reshape_position);
4086 strcpy(page, "none\n");
4087 return 5;
4090 static ssize_t
4091 reshape_position_store(mddev_t *mddev, const char *buf, size_t len)
4093 char *e;
4094 unsigned long long new = simple_strtoull(buf, &e, 10);
4095 if (mddev->pers)
4096 return -EBUSY;
4097 if (buf == e || (*e && *e != '\n'))
4098 return -EINVAL;
4099 mddev->reshape_position = new;
4100 mddev->delta_disks = 0;
4101 mddev->new_level = mddev->level;
4102 mddev->new_layout = mddev->layout;
4103 mddev->new_chunk_sectors = mddev->chunk_sectors;
4104 return len;
4107 static struct md_sysfs_entry md_reshape_position =
4108 __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4109 reshape_position_store);
4111 static ssize_t
4112 array_size_show(mddev_t *mddev, char *page)
4114 if (mddev->external_size)
4115 return sprintf(page, "%llu\n",
4116 (unsigned long long)mddev->array_sectors/2);
4117 else
4118 return sprintf(page, "default\n");
4121 static ssize_t
4122 array_size_store(mddev_t *mddev, const char *buf, size_t len)
4124 sector_t sectors;
4126 if (strncmp(buf, "default", 7) == 0) {
4127 if (mddev->pers)
4128 sectors = mddev->pers->size(mddev, 0, 0);
4129 else
4130 sectors = mddev->array_sectors;
4132 mddev->external_size = 0;
4133 } else {
4134 if (strict_blocks_to_sectors(buf, &sectors) < 0)
4135 return -EINVAL;
4136 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
4137 return -E2BIG;
4139 mddev->external_size = 1;
4142 mddev->array_sectors = sectors;
4143 if (mddev->pers) {
4144 set_capacity(mddev->gendisk, mddev->array_sectors);
4145 revalidate_disk(mddev->gendisk);
4147 return len;
4150 static struct md_sysfs_entry md_array_size =
4151 __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4152 array_size_store);
4154 static struct attribute *md_default_attrs[] = {
4155 &md_level.attr,
4156 &md_layout.attr,
4157 &md_raid_disks.attr,
4158 &md_chunk_size.attr,
4159 &md_size.attr,
4160 &md_resync_start.attr,
4161 &md_metadata.attr,
4162 &md_new_device.attr,
4163 &md_safe_delay.attr,
4164 &md_array_state.attr,
4165 &md_reshape_position.attr,
4166 &md_array_size.attr,
4167 &max_corr_read_errors.attr,
4168 NULL,
4171 static struct attribute *md_redundancy_attrs[] = {
4172 &md_scan_mode.attr,
4173 &md_mismatches.attr,
4174 &md_sync_min.attr,
4175 &md_sync_max.attr,
4176 &md_sync_speed.attr,
4177 &md_sync_force_parallel.attr,
4178 &md_sync_completed.attr,
4179 &md_min_sync.attr,
4180 &md_max_sync.attr,
4181 &md_suspend_lo.attr,
4182 &md_suspend_hi.attr,
4183 &md_bitmap.attr,
4184 &md_degraded.attr,
4185 NULL,
4187 static struct attribute_group md_redundancy_group = {
4188 .name = NULL,
4189 .attrs = md_redundancy_attrs,
4193 static ssize_t
4194 md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4196 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4197 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
4198 ssize_t rv;
4200 if (!entry->show)
4201 return -EIO;
4202 rv = mddev_lock(mddev);
4203 if (!rv) {
4204 rv = entry->show(mddev, page);
4205 mddev_unlock(mddev);
4207 return rv;
4210 static ssize_t
4211 md_attr_store(struct kobject *kobj, struct attribute *attr,
4212 const char *page, size_t length)
4214 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4215 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
4216 ssize_t rv;
4218 if (!entry->store)
4219 return -EIO;
4220 if (!capable(CAP_SYS_ADMIN))
4221 return -EACCES;
4222 rv = mddev_lock(mddev);
4223 if (mddev->hold_active == UNTIL_IOCTL)
4224 mddev->hold_active = 0;
4225 if (!rv) {
4226 rv = entry->store(mddev, page, length);
4227 mddev_unlock(mddev);
4229 return rv;
4232 static void md_free(struct kobject *ko)
4234 mddev_t *mddev = container_of(ko, mddev_t, kobj);
4236 if (mddev->sysfs_state)
4237 sysfs_put(mddev->sysfs_state);
4239 if (mddev->gendisk) {
4240 del_gendisk(mddev->gendisk);
4241 put_disk(mddev->gendisk);
4243 if (mddev->queue)
4244 blk_cleanup_queue(mddev->queue);
4246 kfree(mddev);
4249 static const struct sysfs_ops md_sysfs_ops = {
4250 .show = md_attr_show,
4251 .store = md_attr_store,
4253 static struct kobj_type md_ktype = {
4254 .release = md_free,
4255 .sysfs_ops = &md_sysfs_ops,
4256 .default_attrs = md_default_attrs,
4259 int mdp_major = 0;
4261 static void mddev_delayed_delete(struct work_struct *ws)
4263 mddev_t *mddev = container_of(ws, mddev_t, del_work);
4265 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
4266 kobject_del(&mddev->kobj);
4267 kobject_put(&mddev->kobj);
4270 static int md_alloc(dev_t dev, char *name)
4272 static DEFINE_MUTEX(disks_mutex);
4273 mddev_t *mddev = mddev_find(dev);
4274 struct gendisk *disk;
4275 int partitioned;
4276 int shift;
4277 int unit;
4278 int error;
4280 if (!mddev)
4281 return -ENODEV;
4283 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4284 shift = partitioned ? MdpMinorShift : 0;
4285 unit = MINOR(mddev->unit) >> shift;
4287 /* wait for any previous instance of this device to be
4288 * completely removed (mddev_delayed_delete).
4290 flush_workqueue(md_misc_wq);
4292 mutex_lock(&disks_mutex);
4293 error = -EEXIST;
4294 if (mddev->gendisk)
4295 goto abort;
4297 if (name) {
4298 /* Need to ensure that 'name' is not a duplicate.
4300 mddev_t *mddev2;
4301 spin_lock(&all_mddevs_lock);
4303 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
4304 if (mddev2->gendisk &&
4305 strcmp(mddev2->gendisk->disk_name, name) == 0) {
4306 spin_unlock(&all_mddevs_lock);
4307 goto abort;
4309 spin_unlock(&all_mddevs_lock);
4312 error = -ENOMEM;
4313 mddev->queue = blk_alloc_queue(GFP_KERNEL);
4314 if (!mddev->queue)
4315 goto abort;
4316 mddev->queue->queuedata = mddev;
4318 blk_queue_make_request(mddev->queue, md_make_request);
4320 disk = alloc_disk(1 << shift);
4321 if (!disk) {
4322 blk_cleanup_queue(mddev->queue);
4323 mddev->queue = NULL;
4324 goto abort;
4326 disk->major = MAJOR(mddev->unit);
4327 disk->first_minor = unit << shift;
4328 if (name)
4329 strcpy(disk->disk_name, name);
4330 else if (partitioned)
4331 sprintf(disk->disk_name, "md_d%d", unit);
4332 else
4333 sprintf(disk->disk_name, "md%d", unit);
4334 disk->fops = &md_fops;
4335 disk->private_data = mddev;
4336 disk->queue = mddev->queue;
4337 /* Allow extended partitions. This makes the
4338 * 'mdp' device redundant, but we can't really
4339 * remove it now.
4341 disk->flags |= GENHD_FL_EXT_DEVT;
4342 add_disk(disk);
4343 mddev->gendisk = disk;
4344 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
4345 &disk_to_dev(disk)->kobj, "%s", "md");
4346 if (error) {
4347 /* This isn't possible, but as kobject_init_and_add is marked
4348 * __must_check, we must do something with the result
4350 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
4351 disk->disk_name);
4352 error = 0;
4354 if (mddev->kobj.sd &&
4355 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
4356 printk(KERN_DEBUG "pointless warning\n");
4358 blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA);
4359 abort:
4360 mutex_unlock(&disks_mutex);
4361 if (!error && mddev->kobj.sd) {
4362 kobject_uevent(&mddev->kobj, KOBJ_ADD);
4363 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
4365 mddev_put(mddev);
4366 return error;
4369 static struct kobject *md_probe(dev_t dev, int *part, void *data)
4371 md_alloc(dev, NULL);
4372 return NULL;
4375 static int add_named_array(const char *val, struct kernel_param *kp)
4377 /* val must be "md_*" where * is not all digits.
4378 * We allocate an array with a large free minor number, and
4379 * set the name to val. val must not already be an active name.
4381 int len = strlen(val);
4382 char buf[DISK_NAME_LEN];
4384 while (len && val[len-1] == '\n')
4385 len--;
4386 if (len >= DISK_NAME_LEN)
4387 return -E2BIG;
4388 strlcpy(buf, val, len+1);
4389 if (strncmp(buf, "md_", 3) != 0)
4390 return -EINVAL;
4391 return md_alloc(0, buf);
4394 static void md_safemode_timeout(unsigned long data)
4396 mddev_t *mddev = (mddev_t *) data;
4398 if (!atomic_read(&mddev->writes_pending)) {
4399 mddev->safemode = 1;
4400 if (mddev->external)
4401 sysfs_notify_dirent_safe(mddev->sysfs_state);
4403 md_wakeup_thread(mddev->thread);
4406 static int start_dirty_degraded;
4408 int md_run(mddev_t *mddev)
4410 int err;
4411 mdk_rdev_t *rdev;
4412 struct mdk_personality *pers;
4414 if (list_empty(&mddev->disks))
4415 /* cannot run an array with no devices.. */
4416 return -EINVAL;
4418 if (mddev->pers)
4419 return -EBUSY;
4420 /* Cannot run until previous stop completes properly */
4421 if (mddev->sysfs_active)
4422 return -EBUSY;
4425 * Analyze all RAID superblock(s)
4427 if (!mddev->raid_disks) {
4428 if (!mddev->persistent)
4429 return -EINVAL;
4430 analyze_sbs(mddev);
4433 if (mddev->level != LEVEL_NONE)
4434 request_module("md-level-%d", mddev->level);
4435 else if (mddev->clevel[0])
4436 request_module("md-%s", mddev->clevel);
4439 * Drop all container device buffers, from now on
4440 * the only valid external interface is through the md
4441 * device.
4443 list_for_each_entry(rdev, &mddev->disks, same_set) {
4444 if (test_bit(Faulty, &rdev->flags))
4445 continue;
4446 sync_blockdev(rdev->bdev);
4447 invalidate_bdev(rdev->bdev);
4449 /* perform some consistency tests on the device.
4450 * We don't want the data to overlap the metadata,
4451 * Internal Bitmap issues have been handled elsewhere.
4453 if (rdev->meta_bdev) {
4454 /* Nothing to check */;
4455 } else if (rdev->data_offset < rdev->sb_start) {
4456 if (mddev->dev_sectors &&
4457 rdev->data_offset + mddev->dev_sectors
4458 > rdev->sb_start) {
4459 printk("md: %s: data overlaps metadata\n",
4460 mdname(mddev));
4461 return -EINVAL;
4463 } else {
4464 if (rdev->sb_start + rdev->sb_size/512
4465 > rdev->data_offset) {
4466 printk("md: %s: metadata overlaps data\n",
4467 mdname(mddev));
4468 return -EINVAL;
4471 sysfs_notify_dirent_safe(rdev->sysfs_state);
4474 if (mddev->bio_set == NULL)
4475 mddev->bio_set = bioset_create(BIO_POOL_SIZE, sizeof(mddev));
4477 spin_lock(&pers_lock);
4478 pers = find_pers(mddev->level, mddev->clevel);
4479 if (!pers || !try_module_get(pers->owner)) {
4480 spin_unlock(&pers_lock);
4481 if (mddev->level != LEVEL_NONE)
4482 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
4483 mddev->level);
4484 else
4485 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
4486 mddev->clevel);
4487 return -EINVAL;
4489 mddev->pers = pers;
4490 spin_unlock(&pers_lock);
4491 if (mddev->level != pers->level) {
4492 mddev->level = pers->level;
4493 mddev->new_level = pers->level;
4495 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
4497 if (mddev->reshape_position != MaxSector &&
4498 pers->start_reshape == NULL) {
4499 /* This personality cannot handle reshaping... */
4500 mddev->pers = NULL;
4501 module_put(pers->owner);
4502 return -EINVAL;
4505 if (pers->sync_request) {
4506 /* Warn if this is a potentially silly
4507 * configuration.
4509 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4510 mdk_rdev_t *rdev2;
4511 int warned = 0;
4513 list_for_each_entry(rdev, &mddev->disks, same_set)
4514 list_for_each_entry(rdev2, &mddev->disks, same_set) {
4515 if (rdev < rdev2 &&
4516 rdev->bdev->bd_contains ==
4517 rdev2->bdev->bd_contains) {
4518 printk(KERN_WARNING
4519 "%s: WARNING: %s appears to be"
4520 " on the same physical disk as"
4521 " %s.\n",
4522 mdname(mddev),
4523 bdevname(rdev->bdev,b),
4524 bdevname(rdev2->bdev,b2));
4525 warned = 1;
4529 if (warned)
4530 printk(KERN_WARNING
4531 "True protection against single-disk"
4532 " failure might be compromised.\n");
4535 mddev->recovery = 0;
4536 /* may be over-ridden by personality */
4537 mddev->resync_max_sectors = mddev->dev_sectors;
4539 mddev->ok_start_degraded = start_dirty_degraded;
4541 if (start_readonly && mddev->ro == 0)
4542 mddev->ro = 2; /* read-only, but switch on first write */
4544 err = mddev->pers->run(mddev);
4545 if (err)
4546 printk(KERN_ERR "md: pers->run() failed ...\n");
4547 else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
4548 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
4549 " but 'external_size' not in effect?\n", __func__);
4550 printk(KERN_ERR
4551 "md: invalid array_size %llu > default size %llu\n",
4552 (unsigned long long)mddev->array_sectors / 2,
4553 (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
4554 err = -EINVAL;
4555 mddev->pers->stop(mddev);
4557 if (err == 0 && mddev->pers->sync_request) {
4558 err = bitmap_create(mddev);
4559 if (err) {
4560 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
4561 mdname(mddev), err);
4562 mddev->pers->stop(mddev);
4565 if (err) {
4566 module_put(mddev->pers->owner);
4567 mddev->pers = NULL;
4568 bitmap_destroy(mddev);
4569 return err;
4571 if (mddev->pers->sync_request) {
4572 if (mddev->kobj.sd &&
4573 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
4574 printk(KERN_WARNING
4575 "md: cannot register extra attributes for %s\n",
4576 mdname(mddev));
4577 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
4578 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
4579 mddev->ro = 0;
4581 atomic_set(&mddev->writes_pending,0);
4582 atomic_set(&mddev->max_corr_read_errors,
4583 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
4584 mddev->safemode = 0;
4585 mddev->safemode_timer.function = md_safemode_timeout;
4586 mddev->safemode_timer.data = (unsigned long) mddev;
4587 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
4588 mddev->in_sync = 1;
4589 smp_wmb();
4590 mddev->ready = 1;
4591 list_for_each_entry(rdev, &mddev->disks, same_set)
4592 if (rdev->raid_disk >= 0) {
4593 char nm[20];
4594 sprintf(nm, "rd%d", rdev->raid_disk);
4595 if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
4596 /* failure here is OK */;
4599 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4601 if (mddev->flags)
4602 md_update_sb(mddev, 0);
4604 md_wakeup_thread(mddev->thread);
4605 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
4607 md_new_event(mddev);
4608 sysfs_notify_dirent_safe(mddev->sysfs_state);
4609 sysfs_notify_dirent_safe(mddev->sysfs_action);
4610 sysfs_notify(&mddev->kobj, NULL, "degraded");
4611 return 0;
4613 EXPORT_SYMBOL_GPL(md_run);
4615 static int do_md_run(mddev_t *mddev)
4617 int err;
4619 err = md_run(mddev);
4620 if (err)
4621 goto out;
4622 err = bitmap_load(mddev);
4623 if (err) {
4624 bitmap_destroy(mddev);
4625 goto out;
4627 set_capacity(mddev->gendisk, mddev->array_sectors);
4628 revalidate_disk(mddev->gendisk);
4629 mddev->changed = 1;
4630 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
4631 out:
4632 return err;
4635 static int restart_array(mddev_t *mddev)
4637 struct gendisk *disk = mddev->gendisk;
4639 /* Complain if it has no devices */
4640 if (list_empty(&mddev->disks))
4641 return -ENXIO;
4642 if (!mddev->pers)
4643 return -EINVAL;
4644 if (!mddev->ro)
4645 return -EBUSY;
4646 mddev->safemode = 0;
4647 mddev->ro = 0;
4648 set_disk_ro(disk, 0);
4649 printk(KERN_INFO "md: %s switched to read-write mode.\n",
4650 mdname(mddev));
4651 /* Kick recovery or resync if necessary */
4652 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4653 md_wakeup_thread(mddev->thread);
4654 md_wakeup_thread(mddev->sync_thread);
4655 sysfs_notify_dirent_safe(mddev->sysfs_state);
4656 return 0;
4659 /* similar to deny_write_access, but accounts for our holding a reference
4660 * to the file ourselves */
4661 static int deny_bitmap_write_access(struct file * file)
4663 struct inode *inode = file->f_mapping->host;
4665 spin_lock(&inode->i_lock);
4666 if (atomic_read(&inode->i_writecount) > 1) {
4667 spin_unlock(&inode->i_lock);
4668 return -ETXTBSY;
4670 atomic_set(&inode->i_writecount, -1);
4671 spin_unlock(&inode->i_lock);
4673 return 0;
4676 void restore_bitmap_write_access(struct file *file)
4678 struct inode *inode = file->f_mapping->host;
4680 spin_lock(&inode->i_lock);
4681 atomic_set(&inode->i_writecount, 1);
4682 spin_unlock(&inode->i_lock);
4685 static void md_clean(mddev_t *mddev)
4687 mddev->array_sectors = 0;
4688 mddev->external_size = 0;
4689 mddev->dev_sectors = 0;
4690 mddev->raid_disks = 0;
4691 mddev->recovery_cp = 0;
4692 mddev->resync_min = 0;
4693 mddev->resync_max = MaxSector;
4694 mddev->reshape_position = MaxSector;
4695 mddev->external = 0;
4696 mddev->persistent = 0;
4697 mddev->level = LEVEL_NONE;
4698 mddev->clevel[0] = 0;
4699 mddev->flags = 0;
4700 mddev->ro = 0;
4701 mddev->metadata_type[0] = 0;
4702 mddev->chunk_sectors = 0;
4703 mddev->ctime = mddev->utime = 0;
4704 mddev->layout = 0;
4705 mddev->max_disks = 0;
4706 mddev->events = 0;
4707 mddev->can_decrease_events = 0;
4708 mddev->delta_disks = 0;
4709 mddev->new_level = LEVEL_NONE;
4710 mddev->new_layout = 0;
4711 mddev->new_chunk_sectors = 0;
4712 mddev->curr_resync = 0;
4713 mddev->resync_mismatches = 0;
4714 mddev->suspend_lo = mddev->suspend_hi = 0;
4715 mddev->sync_speed_min = mddev->sync_speed_max = 0;
4716 mddev->recovery = 0;
4717 mddev->in_sync = 0;
4718 mddev->changed = 0;
4719 mddev->degraded = 0;
4720 mddev->safemode = 0;
4721 mddev->bitmap_info.offset = 0;
4722 mddev->bitmap_info.default_offset = 0;
4723 mddev->bitmap_info.chunksize = 0;
4724 mddev->bitmap_info.daemon_sleep = 0;
4725 mddev->bitmap_info.max_write_behind = 0;
4726 mddev->plug = NULL;
4729 static void __md_stop_writes(mddev_t *mddev)
4731 if (mddev->sync_thread) {
4732 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4733 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4734 reap_sync_thread(mddev);
4737 del_timer_sync(&mddev->safemode_timer);
4739 bitmap_flush(mddev);
4740 md_super_wait(mddev);
4742 if (!mddev->in_sync || mddev->flags) {
4743 /* mark array as shutdown cleanly */
4744 mddev->in_sync = 1;
4745 md_update_sb(mddev, 1);
4749 void md_stop_writes(mddev_t *mddev)
4751 mddev_lock(mddev);
4752 __md_stop_writes(mddev);
4753 mddev_unlock(mddev);
4755 EXPORT_SYMBOL_GPL(md_stop_writes);
4757 void md_stop(mddev_t *mddev)
4759 mddev->ready = 0;
4760 mddev->pers->stop(mddev);
4761 if (mddev->pers->sync_request && mddev->to_remove == NULL)
4762 mddev->to_remove = &md_redundancy_group;
4763 module_put(mddev->pers->owner);
4764 mddev->pers = NULL;
4765 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4767 EXPORT_SYMBOL_GPL(md_stop);
4769 static int md_set_readonly(mddev_t *mddev, int is_open)
4771 int err = 0;
4772 mutex_lock(&mddev->open_mutex);
4773 if (atomic_read(&mddev->openers) > is_open) {
4774 printk("md: %s still in use.\n",mdname(mddev));
4775 err = -EBUSY;
4776 goto out;
4778 if (mddev->pers) {
4779 __md_stop_writes(mddev);
4781 err = -ENXIO;
4782 if (mddev->ro==1)
4783 goto out;
4784 mddev->ro = 1;
4785 set_disk_ro(mddev->gendisk, 1);
4786 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4787 sysfs_notify_dirent_safe(mddev->sysfs_state);
4788 err = 0;
4790 out:
4791 mutex_unlock(&mddev->open_mutex);
4792 return err;
4795 /* mode:
4796 * 0 - completely stop and dis-assemble array
4797 * 2 - stop but do not disassemble array
4799 static int do_md_stop(mddev_t * mddev, int mode, int is_open)
4801 struct gendisk *disk = mddev->gendisk;
4802 mdk_rdev_t *rdev;
4804 mutex_lock(&mddev->open_mutex);
4805 if (atomic_read(&mddev->openers) > is_open ||
4806 mddev->sysfs_active) {
4807 printk("md: %s still in use.\n",mdname(mddev));
4808 mutex_unlock(&mddev->open_mutex);
4809 return -EBUSY;
4812 if (mddev->pers) {
4813 if (mddev->ro)
4814 set_disk_ro(disk, 0);
4816 __md_stop_writes(mddev);
4817 md_stop(mddev);
4818 mddev->queue->merge_bvec_fn = NULL;
4819 mddev->queue->backing_dev_info.congested_fn = NULL;
4821 /* tell userspace to handle 'inactive' */
4822 sysfs_notify_dirent_safe(mddev->sysfs_state);
4824 list_for_each_entry(rdev, &mddev->disks, same_set)
4825 if (rdev->raid_disk >= 0) {
4826 char nm[20];
4827 sprintf(nm, "rd%d", rdev->raid_disk);
4828 sysfs_remove_link(&mddev->kobj, nm);
4831 set_capacity(disk, 0);
4832 mutex_unlock(&mddev->open_mutex);
4833 mddev->changed = 1;
4834 revalidate_disk(disk);
4836 if (mddev->ro)
4837 mddev->ro = 0;
4838 } else
4839 mutex_unlock(&mddev->open_mutex);
4841 * Free resources if final stop
4843 if (mode == 0) {
4844 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
4846 bitmap_destroy(mddev);
4847 if (mddev->bitmap_info.file) {
4848 restore_bitmap_write_access(mddev->bitmap_info.file);
4849 fput(mddev->bitmap_info.file);
4850 mddev->bitmap_info.file = NULL;
4852 mddev->bitmap_info.offset = 0;
4854 export_array(mddev);
4856 md_clean(mddev);
4857 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
4858 if (mddev->hold_active == UNTIL_STOP)
4859 mddev->hold_active = 0;
4861 blk_integrity_unregister(disk);
4862 md_new_event(mddev);
4863 sysfs_notify_dirent_safe(mddev->sysfs_state);
4864 return 0;
4867 #ifndef MODULE
4868 static void autorun_array(mddev_t *mddev)
4870 mdk_rdev_t *rdev;
4871 int err;
4873 if (list_empty(&mddev->disks))
4874 return;
4876 printk(KERN_INFO "md: running: ");
4878 list_for_each_entry(rdev, &mddev->disks, same_set) {
4879 char b[BDEVNAME_SIZE];
4880 printk("<%s>", bdevname(rdev->bdev,b));
4882 printk("\n");
4884 err = do_md_run(mddev);
4885 if (err) {
4886 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
4887 do_md_stop(mddev, 0, 0);
4892 * lets try to run arrays based on all disks that have arrived
4893 * until now. (those are in pending_raid_disks)
4895 * the method: pick the first pending disk, collect all disks with
4896 * the same UUID, remove all from the pending list and put them into
4897 * the 'same_array' list. Then order this list based on superblock
4898 * update time (freshest comes first), kick out 'old' disks and
4899 * compare superblocks. If everything's fine then run it.
4901 * If "unit" is allocated, then bump its reference count
4903 static void autorun_devices(int part)
4905 mdk_rdev_t *rdev0, *rdev, *tmp;
4906 mddev_t *mddev;
4907 char b[BDEVNAME_SIZE];
4909 printk(KERN_INFO "md: autorun ...\n");
4910 while (!list_empty(&pending_raid_disks)) {
4911 int unit;
4912 dev_t dev;
4913 LIST_HEAD(candidates);
4914 rdev0 = list_entry(pending_raid_disks.next,
4915 mdk_rdev_t, same_set);
4917 printk(KERN_INFO "md: considering %s ...\n",
4918 bdevname(rdev0->bdev,b));
4919 INIT_LIST_HEAD(&candidates);
4920 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
4921 if (super_90_load(rdev, rdev0, 0) >= 0) {
4922 printk(KERN_INFO "md: adding %s ...\n",
4923 bdevname(rdev->bdev,b));
4924 list_move(&rdev->same_set, &candidates);
4927 * now we have a set of devices, with all of them having
4928 * mostly sane superblocks. It's time to allocate the
4929 * mddev.
4931 if (part) {
4932 dev = MKDEV(mdp_major,
4933 rdev0->preferred_minor << MdpMinorShift);
4934 unit = MINOR(dev) >> MdpMinorShift;
4935 } else {
4936 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
4937 unit = MINOR(dev);
4939 if (rdev0->preferred_minor != unit) {
4940 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
4941 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
4942 break;
4945 md_probe(dev, NULL, NULL);
4946 mddev = mddev_find(dev);
4947 if (!mddev || !mddev->gendisk) {
4948 if (mddev)
4949 mddev_put(mddev);
4950 printk(KERN_ERR
4951 "md: cannot allocate memory for md drive.\n");
4952 break;
4954 if (mddev_lock(mddev))
4955 printk(KERN_WARNING "md: %s locked, cannot run\n",
4956 mdname(mddev));
4957 else if (mddev->raid_disks || mddev->major_version
4958 || !list_empty(&mddev->disks)) {
4959 printk(KERN_WARNING
4960 "md: %s already running, cannot run %s\n",
4961 mdname(mddev), bdevname(rdev0->bdev,b));
4962 mddev_unlock(mddev);
4963 } else {
4964 printk(KERN_INFO "md: created %s\n", mdname(mddev));
4965 mddev->persistent = 1;
4966 rdev_for_each_list(rdev, tmp, &candidates) {
4967 list_del_init(&rdev->same_set);
4968 if (bind_rdev_to_array(rdev, mddev))
4969 export_rdev(rdev);
4971 autorun_array(mddev);
4972 mddev_unlock(mddev);
4974 /* on success, candidates will be empty, on error
4975 * it won't...
4977 rdev_for_each_list(rdev, tmp, &candidates) {
4978 list_del_init(&rdev->same_set);
4979 export_rdev(rdev);
4981 mddev_put(mddev);
4983 printk(KERN_INFO "md: ... autorun DONE.\n");
4985 #endif /* !MODULE */
4987 static int get_version(void __user * arg)
4989 mdu_version_t ver;
4991 ver.major = MD_MAJOR_VERSION;
4992 ver.minor = MD_MINOR_VERSION;
4993 ver.patchlevel = MD_PATCHLEVEL_VERSION;
4995 if (copy_to_user(arg, &ver, sizeof(ver)))
4996 return -EFAULT;
4998 return 0;
5001 static int get_array_info(mddev_t * mddev, void __user * arg)
5003 mdu_array_info_t info;
5004 int nr,working,insync,failed,spare;
5005 mdk_rdev_t *rdev;
5007 nr=working=insync=failed=spare=0;
5008 list_for_each_entry(rdev, &mddev->disks, same_set) {
5009 nr++;
5010 if (test_bit(Faulty, &rdev->flags))
5011 failed++;
5012 else {
5013 working++;
5014 if (test_bit(In_sync, &rdev->flags))
5015 insync++;
5016 else
5017 spare++;
5021 info.major_version = mddev->major_version;
5022 info.minor_version = mddev->minor_version;
5023 info.patch_version = MD_PATCHLEVEL_VERSION;
5024 info.ctime = mddev->ctime;
5025 info.level = mddev->level;
5026 info.size = mddev->dev_sectors / 2;
5027 if (info.size != mddev->dev_sectors / 2) /* overflow */
5028 info.size = -1;
5029 info.nr_disks = nr;
5030 info.raid_disks = mddev->raid_disks;
5031 info.md_minor = mddev->md_minor;
5032 info.not_persistent= !mddev->persistent;
5034 info.utime = mddev->utime;
5035 info.state = 0;
5036 if (mddev->in_sync)
5037 info.state = (1<<MD_SB_CLEAN);
5038 if (mddev->bitmap && mddev->bitmap_info.offset)
5039 info.state = (1<<MD_SB_BITMAP_PRESENT);
5040 info.active_disks = insync;
5041 info.working_disks = working;
5042 info.failed_disks = failed;
5043 info.spare_disks = spare;
5045 info.layout = mddev->layout;
5046 info.chunk_size = mddev->chunk_sectors << 9;
5048 if (copy_to_user(arg, &info, sizeof(info)))
5049 return -EFAULT;
5051 return 0;
5054 static int get_bitmap_file(mddev_t * mddev, void __user * arg)
5056 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
5057 char *ptr, *buf = NULL;
5058 int err = -ENOMEM;
5060 if (md_allow_write(mddev))
5061 file = kmalloc(sizeof(*file), GFP_NOIO);
5062 else
5063 file = kmalloc(sizeof(*file), GFP_KERNEL);
5065 if (!file)
5066 goto out;
5068 /* bitmap disabled, zero the first byte and copy out */
5069 if (!mddev->bitmap || !mddev->bitmap->file) {
5070 file->pathname[0] = '\0';
5071 goto copy_out;
5074 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
5075 if (!buf)
5076 goto out;
5078 ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname));
5079 if (IS_ERR(ptr))
5080 goto out;
5082 strcpy(file->pathname, ptr);
5084 copy_out:
5085 err = 0;
5086 if (copy_to_user(arg, file, sizeof(*file)))
5087 err = -EFAULT;
5088 out:
5089 kfree(buf);
5090 kfree(file);
5091 return err;
5094 static int get_disk_info(mddev_t * mddev, void __user * arg)
5096 mdu_disk_info_t info;
5097 mdk_rdev_t *rdev;
5099 if (copy_from_user(&info, arg, sizeof(info)))
5100 return -EFAULT;
5102 rdev = find_rdev_nr(mddev, info.number);
5103 if (rdev) {
5104 info.major = MAJOR(rdev->bdev->bd_dev);
5105 info.minor = MINOR(rdev->bdev->bd_dev);
5106 info.raid_disk = rdev->raid_disk;
5107 info.state = 0;
5108 if (test_bit(Faulty, &rdev->flags))
5109 info.state |= (1<<MD_DISK_FAULTY);
5110 else if (test_bit(In_sync, &rdev->flags)) {
5111 info.state |= (1<<MD_DISK_ACTIVE);
5112 info.state |= (1<<MD_DISK_SYNC);
5114 if (test_bit(WriteMostly, &rdev->flags))
5115 info.state |= (1<<MD_DISK_WRITEMOSTLY);
5116 } else {
5117 info.major = info.minor = 0;
5118 info.raid_disk = -1;
5119 info.state = (1<<MD_DISK_REMOVED);
5122 if (copy_to_user(arg, &info, sizeof(info)))
5123 return -EFAULT;
5125 return 0;
5128 static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
5130 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
5131 mdk_rdev_t *rdev;
5132 dev_t dev = MKDEV(info->major,info->minor);
5134 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
5135 return -EOVERFLOW;
5137 if (!mddev->raid_disks) {
5138 int err;
5139 /* expecting a device which has a superblock */
5140 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
5141 if (IS_ERR(rdev)) {
5142 printk(KERN_WARNING
5143 "md: md_import_device returned %ld\n",
5144 PTR_ERR(rdev));
5145 return PTR_ERR(rdev);
5147 if (!list_empty(&mddev->disks)) {
5148 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
5149 mdk_rdev_t, same_set);
5150 err = super_types[mddev->major_version]
5151 .load_super(rdev, rdev0, mddev->minor_version);
5152 if (err < 0) {
5153 printk(KERN_WARNING
5154 "md: %s has different UUID to %s\n",
5155 bdevname(rdev->bdev,b),
5156 bdevname(rdev0->bdev,b2));
5157 export_rdev(rdev);
5158 return -EINVAL;
5161 err = bind_rdev_to_array(rdev, mddev);
5162 if (err)
5163 export_rdev(rdev);
5164 return err;
5168 * add_new_disk can be used once the array is assembled
5169 * to add "hot spares". They must already have a superblock
5170 * written
5172 if (mddev->pers) {
5173 int err;
5174 if (!mddev->pers->hot_add_disk) {
5175 printk(KERN_WARNING
5176 "%s: personality does not support diskops!\n",
5177 mdname(mddev));
5178 return -EINVAL;
5180 if (mddev->persistent)
5181 rdev = md_import_device(dev, mddev->major_version,
5182 mddev->minor_version);
5183 else
5184 rdev = md_import_device(dev, -1, -1);
5185 if (IS_ERR(rdev)) {
5186 printk(KERN_WARNING
5187 "md: md_import_device returned %ld\n",
5188 PTR_ERR(rdev));
5189 return PTR_ERR(rdev);
5191 /* set saved_raid_disk if appropriate */
5192 if (!mddev->persistent) {
5193 if (info->state & (1<<MD_DISK_SYNC) &&
5194 info->raid_disk < mddev->raid_disks) {
5195 rdev->raid_disk = info->raid_disk;
5196 set_bit(In_sync, &rdev->flags);
5197 } else
5198 rdev->raid_disk = -1;
5199 } else
5200 super_types[mddev->major_version].
5201 validate_super(mddev, rdev);
5202 if (test_bit(In_sync, &rdev->flags))
5203 rdev->saved_raid_disk = rdev->raid_disk;
5204 else
5205 rdev->saved_raid_disk = -1;
5207 clear_bit(In_sync, &rdev->flags); /* just to be sure */
5208 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5209 set_bit(WriteMostly, &rdev->flags);
5210 else
5211 clear_bit(WriteMostly, &rdev->flags);
5213 rdev->raid_disk = -1;
5214 err = bind_rdev_to_array(rdev, mddev);
5215 if (!err && !mddev->pers->hot_remove_disk) {
5216 /* If there is hot_add_disk but no hot_remove_disk
5217 * then added disks for geometry changes,
5218 * and should be added immediately.
5220 super_types[mddev->major_version].
5221 validate_super(mddev, rdev);
5222 err = mddev->pers->hot_add_disk(mddev, rdev);
5223 if (err)
5224 unbind_rdev_from_array(rdev);
5226 if (err)
5227 export_rdev(rdev);
5228 else
5229 sysfs_notify_dirent_safe(rdev->sysfs_state);
5231 md_update_sb(mddev, 1);
5232 if (mddev->degraded)
5233 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
5234 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5235 md_wakeup_thread(mddev->thread);
5236 return err;
5239 /* otherwise, add_new_disk is only allowed
5240 * for major_version==0 superblocks
5242 if (mddev->major_version != 0) {
5243 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
5244 mdname(mddev));
5245 return -EINVAL;
5248 if (!(info->state & (1<<MD_DISK_FAULTY))) {
5249 int err;
5250 rdev = md_import_device(dev, -1, 0);
5251 if (IS_ERR(rdev)) {
5252 printk(KERN_WARNING
5253 "md: error, md_import_device() returned %ld\n",
5254 PTR_ERR(rdev));
5255 return PTR_ERR(rdev);
5257 rdev->desc_nr = info->number;
5258 if (info->raid_disk < mddev->raid_disks)
5259 rdev->raid_disk = info->raid_disk;
5260 else
5261 rdev->raid_disk = -1;
5263 if (rdev->raid_disk < mddev->raid_disks)
5264 if (info->state & (1<<MD_DISK_SYNC))
5265 set_bit(In_sync, &rdev->flags);
5267 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5268 set_bit(WriteMostly, &rdev->flags);
5270 if (!mddev->persistent) {
5271 printk(KERN_INFO "md: nonpersistent superblock ...\n");
5272 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
5273 } else
5274 rdev->sb_start = calc_dev_sboffset(rdev);
5275 rdev->sectors = rdev->sb_start;
5277 err = bind_rdev_to_array(rdev, mddev);
5278 if (err) {
5279 export_rdev(rdev);
5280 return err;
5284 return 0;
5287 static int hot_remove_disk(mddev_t * mddev, dev_t dev)
5289 char b[BDEVNAME_SIZE];
5290 mdk_rdev_t *rdev;
5292 rdev = find_rdev(mddev, dev);
5293 if (!rdev)
5294 return -ENXIO;
5296 if (rdev->raid_disk >= 0)
5297 goto busy;
5299 kick_rdev_from_array(rdev);
5300 md_update_sb(mddev, 1);
5301 md_new_event(mddev);
5303 return 0;
5304 busy:
5305 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
5306 bdevname(rdev->bdev,b), mdname(mddev));
5307 return -EBUSY;
5310 static int hot_add_disk(mddev_t * mddev, dev_t dev)
5312 char b[BDEVNAME_SIZE];
5313 int err;
5314 mdk_rdev_t *rdev;
5316 if (!mddev->pers)
5317 return -ENODEV;
5319 if (mddev->major_version != 0) {
5320 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
5321 " version-0 superblocks.\n",
5322 mdname(mddev));
5323 return -EINVAL;
5325 if (!mddev->pers->hot_add_disk) {
5326 printk(KERN_WARNING
5327 "%s: personality does not support diskops!\n",
5328 mdname(mddev));
5329 return -EINVAL;
5332 rdev = md_import_device(dev, -1, 0);
5333 if (IS_ERR(rdev)) {
5334 printk(KERN_WARNING
5335 "md: error, md_import_device() returned %ld\n",
5336 PTR_ERR(rdev));
5337 return -EINVAL;
5340 if (mddev->persistent)
5341 rdev->sb_start = calc_dev_sboffset(rdev);
5342 else
5343 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
5345 rdev->sectors = rdev->sb_start;
5347 if (test_bit(Faulty, &rdev->flags)) {
5348 printk(KERN_WARNING
5349 "md: can not hot-add faulty %s disk to %s!\n",
5350 bdevname(rdev->bdev,b), mdname(mddev));
5351 err = -EINVAL;
5352 goto abort_export;
5354 clear_bit(In_sync, &rdev->flags);
5355 rdev->desc_nr = -1;
5356 rdev->saved_raid_disk = -1;
5357 err = bind_rdev_to_array(rdev, mddev);
5358 if (err)
5359 goto abort_export;
5362 * The rest should better be atomic, we can have disk failures
5363 * noticed in interrupt contexts ...
5366 rdev->raid_disk = -1;
5368 md_update_sb(mddev, 1);
5371 * Kick recovery, maybe this spare has to be added to the
5372 * array immediately.
5374 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5375 md_wakeup_thread(mddev->thread);
5376 md_new_event(mddev);
5377 return 0;
5379 abort_export:
5380 export_rdev(rdev);
5381 return err;
5384 static int set_bitmap_file(mddev_t *mddev, int fd)
5386 int err;
5388 if (mddev->pers) {
5389 if (!mddev->pers->quiesce)
5390 return -EBUSY;
5391 if (mddev->recovery || mddev->sync_thread)
5392 return -EBUSY;
5393 /* we should be able to change the bitmap.. */
5397 if (fd >= 0) {
5398 if (mddev->bitmap)
5399 return -EEXIST; /* cannot add when bitmap is present */
5400 mddev->bitmap_info.file = fget(fd);
5402 if (mddev->bitmap_info.file == NULL) {
5403 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
5404 mdname(mddev));
5405 return -EBADF;
5408 err = deny_bitmap_write_access(mddev->bitmap_info.file);
5409 if (err) {
5410 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
5411 mdname(mddev));
5412 fput(mddev->bitmap_info.file);
5413 mddev->bitmap_info.file = NULL;
5414 return err;
5416 mddev->bitmap_info.offset = 0; /* file overrides offset */
5417 } else if (mddev->bitmap == NULL)
5418 return -ENOENT; /* cannot remove what isn't there */
5419 err = 0;
5420 if (mddev->pers) {
5421 mddev->pers->quiesce(mddev, 1);
5422 if (fd >= 0) {
5423 err = bitmap_create(mddev);
5424 if (!err)
5425 err = bitmap_load(mddev);
5427 if (fd < 0 || err) {
5428 bitmap_destroy(mddev);
5429 fd = -1; /* make sure to put the file */
5431 mddev->pers->quiesce(mddev, 0);
5433 if (fd < 0) {
5434 if (mddev->bitmap_info.file) {
5435 restore_bitmap_write_access(mddev->bitmap_info.file);
5436 fput(mddev->bitmap_info.file);
5438 mddev->bitmap_info.file = NULL;
5441 return err;
5445 * set_array_info is used two different ways
5446 * The original usage is when creating a new array.
5447 * In this usage, raid_disks is > 0 and it together with
5448 * level, size, not_persistent,layout,chunksize determine the
5449 * shape of the array.
5450 * This will always create an array with a type-0.90.0 superblock.
5451 * The newer usage is when assembling an array.
5452 * In this case raid_disks will be 0, and the major_version field is
5453 * use to determine which style super-blocks are to be found on the devices.
5454 * The minor and patch _version numbers are also kept incase the
5455 * super_block handler wishes to interpret them.
5457 static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
5460 if (info->raid_disks == 0) {
5461 /* just setting version number for superblock loading */
5462 if (info->major_version < 0 ||
5463 info->major_version >= ARRAY_SIZE(super_types) ||
5464 super_types[info->major_version].name == NULL) {
5465 /* maybe try to auto-load a module? */
5466 printk(KERN_INFO
5467 "md: superblock version %d not known\n",
5468 info->major_version);
5469 return -EINVAL;
5471 mddev->major_version = info->major_version;
5472 mddev->minor_version = info->minor_version;
5473 mddev->patch_version = info->patch_version;
5474 mddev->persistent = !info->not_persistent;
5475 /* ensure mddev_put doesn't delete this now that there
5476 * is some minimal configuration.
5478 mddev->ctime = get_seconds();
5479 return 0;
5481 mddev->major_version = MD_MAJOR_VERSION;
5482 mddev->minor_version = MD_MINOR_VERSION;
5483 mddev->patch_version = MD_PATCHLEVEL_VERSION;
5484 mddev->ctime = get_seconds();
5486 mddev->level = info->level;
5487 mddev->clevel[0] = 0;
5488 mddev->dev_sectors = 2 * (sector_t)info->size;
5489 mddev->raid_disks = info->raid_disks;
5490 /* don't set md_minor, it is determined by which /dev/md* was
5491 * openned
5493 if (info->state & (1<<MD_SB_CLEAN))
5494 mddev->recovery_cp = MaxSector;
5495 else
5496 mddev->recovery_cp = 0;
5497 mddev->persistent = ! info->not_persistent;
5498 mddev->external = 0;
5500 mddev->layout = info->layout;
5501 mddev->chunk_sectors = info->chunk_size >> 9;
5503 mddev->max_disks = MD_SB_DISKS;
5505 if (mddev->persistent)
5506 mddev->flags = 0;
5507 set_bit(MD_CHANGE_DEVS, &mddev->flags);
5509 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
5510 mddev->bitmap_info.offset = 0;
5512 mddev->reshape_position = MaxSector;
5515 * Generate a 128 bit UUID
5517 get_random_bytes(mddev->uuid, 16);
5519 mddev->new_level = mddev->level;
5520 mddev->new_chunk_sectors = mddev->chunk_sectors;
5521 mddev->new_layout = mddev->layout;
5522 mddev->delta_disks = 0;
5524 return 0;
5527 void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors)
5529 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
5531 if (mddev->external_size)
5532 return;
5534 mddev->array_sectors = array_sectors;
5536 EXPORT_SYMBOL(md_set_array_sectors);
5538 static int update_size(mddev_t *mddev, sector_t num_sectors)
5540 mdk_rdev_t *rdev;
5541 int rv;
5542 int fit = (num_sectors == 0);
5544 if (mddev->pers->resize == NULL)
5545 return -EINVAL;
5546 /* The "num_sectors" is the number of sectors of each device that
5547 * is used. This can only make sense for arrays with redundancy.
5548 * linear and raid0 always use whatever space is available. We can only
5549 * consider changing this number if no resync or reconstruction is
5550 * happening, and if the new size is acceptable. It must fit before the
5551 * sb_start or, if that is <data_offset, it must fit before the size
5552 * of each device. If num_sectors is zero, we find the largest size
5553 * that fits.
5555 if (mddev->sync_thread)
5556 return -EBUSY;
5557 if (mddev->bitmap)
5558 /* Sorry, cannot grow a bitmap yet, just remove it,
5559 * grow, and re-add.
5561 return -EBUSY;
5562 list_for_each_entry(rdev, &mddev->disks, same_set) {
5563 sector_t avail = rdev->sectors;
5565 if (fit && (num_sectors == 0 || num_sectors > avail))
5566 num_sectors = avail;
5567 if (avail < num_sectors)
5568 return -ENOSPC;
5570 rv = mddev->pers->resize(mddev, num_sectors);
5571 if (!rv)
5572 revalidate_disk(mddev->gendisk);
5573 return rv;
5576 static int update_raid_disks(mddev_t *mddev, int raid_disks)
5578 int rv;
5579 /* change the number of raid disks */
5580 if (mddev->pers->check_reshape == NULL)
5581 return -EINVAL;
5582 if (raid_disks <= 0 ||
5583 (mddev->max_disks && raid_disks >= mddev->max_disks))
5584 return -EINVAL;
5585 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
5586 return -EBUSY;
5587 mddev->delta_disks = raid_disks - mddev->raid_disks;
5589 rv = mddev->pers->check_reshape(mddev);
5590 if (rv < 0)
5591 mddev->delta_disks = 0;
5592 return rv;
5597 * update_array_info is used to change the configuration of an
5598 * on-line array.
5599 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
5600 * fields in the info are checked against the array.
5601 * Any differences that cannot be handled will cause an error.
5602 * Normally, only one change can be managed at a time.
5604 static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
5606 int rv = 0;
5607 int cnt = 0;
5608 int state = 0;
5610 /* calculate expected state,ignoring low bits */
5611 if (mddev->bitmap && mddev->bitmap_info.offset)
5612 state |= (1 << MD_SB_BITMAP_PRESENT);
5614 if (mddev->major_version != info->major_version ||
5615 mddev->minor_version != info->minor_version ||
5616 /* mddev->patch_version != info->patch_version || */
5617 mddev->ctime != info->ctime ||
5618 mddev->level != info->level ||
5619 /* mddev->layout != info->layout || */
5620 !mddev->persistent != info->not_persistent||
5621 mddev->chunk_sectors != info->chunk_size >> 9 ||
5622 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
5623 ((state^info->state) & 0xfffffe00)
5625 return -EINVAL;
5626 /* Check there is only one change */
5627 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
5628 cnt++;
5629 if (mddev->raid_disks != info->raid_disks)
5630 cnt++;
5631 if (mddev->layout != info->layout)
5632 cnt++;
5633 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
5634 cnt++;
5635 if (cnt == 0)
5636 return 0;
5637 if (cnt > 1)
5638 return -EINVAL;
5640 if (mddev->layout != info->layout) {
5641 /* Change layout
5642 * we don't need to do anything at the md level, the
5643 * personality will take care of it all.
5645 if (mddev->pers->check_reshape == NULL)
5646 return -EINVAL;
5647 else {
5648 mddev->new_layout = info->layout;
5649 rv = mddev->pers->check_reshape(mddev);
5650 if (rv)
5651 mddev->new_layout = mddev->layout;
5652 return rv;
5655 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
5656 rv = update_size(mddev, (sector_t)info->size * 2);
5658 if (mddev->raid_disks != info->raid_disks)
5659 rv = update_raid_disks(mddev, info->raid_disks);
5661 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
5662 if (mddev->pers->quiesce == NULL)
5663 return -EINVAL;
5664 if (mddev->recovery || mddev->sync_thread)
5665 return -EBUSY;
5666 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
5667 /* add the bitmap */
5668 if (mddev->bitmap)
5669 return -EEXIST;
5670 if (mddev->bitmap_info.default_offset == 0)
5671 return -EINVAL;
5672 mddev->bitmap_info.offset =
5673 mddev->bitmap_info.default_offset;
5674 mddev->pers->quiesce(mddev, 1);
5675 rv = bitmap_create(mddev);
5676 if (!rv)
5677 rv = bitmap_load(mddev);
5678 if (rv)
5679 bitmap_destroy(mddev);
5680 mddev->pers->quiesce(mddev, 0);
5681 } else {
5682 /* remove the bitmap */
5683 if (!mddev->bitmap)
5684 return -ENOENT;
5685 if (mddev->bitmap->file)
5686 return -EINVAL;
5687 mddev->pers->quiesce(mddev, 1);
5688 bitmap_destroy(mddev);
5689 mddev->pers->quiesce(mddev, 0);
5690 mddev->bitmap_info.offset = 0;
5693 md_update_sb(mddev, 1);
5694 return rv;
5697 static int set_disk_faulty(mddev_t *mddev, dev_t dev)
5699 mdk_rdev_t *rdev;
5701 if (mddev->pers == NULL)
5702 return -ENODEV;
5704 rdev = find_rdev(mddev, dev);
5705 if (!rdev)
5706 return -ENODEV;
5708 md_error(mddev, rdev);
5709 return 0;
5713 * We have a problem here : there is no easy way to give a CHS
5714 * virtual geometry. We currently pretend that we have a 2 heads
5715 * 4 sectors (with a BIG number of cylinders...). This drives
5716 * dosfs just mad... ;-)
5718 static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
5720 mddev_t *mddev = bdev->bd_disk->private_data;
5722 geo->heads = 2;
5723 geo->sectors = 4;
5724 geo->cylinders = mddev->array_sectors / 8;
5725 return 0;
5728 static int md_ioctl(struct block_device *bdev, fmode_t mode,
5729 unsigned int cmd, unsigned long arg)
5731 int err = 0;
5732 void __user *argp = (void __user *)arg;
5733 mddev_t *mddev = NULL;
5734 int ro;
5736 if (!capable(CAP_SYS_ADMIN))
5737 return -EACCES;
5740 * Commands dealing with the RAID driver but not any
5741 * particular array:
5743 switch (cmd)
5745 case RAID_VERSION:
5746 err = get_version(argp);
5747 goto done;
5749 case PRINT_RAID_DEBUG:
5750 err = 0;
5751 md_print_devices();
5752 goto done;
5754 #ifndef MODULE
5755 case RAID_AUTORUN:
5756 err = 0;
5757 autostart_arrays(arg);
5758 goto done;
5759 #endif
5760 default:;
5764 * Commands creating/starting a new array:
5767 mddev = bdev->bd_disk->private_data;
5769 if (!mddev) {
5770 BUG();
5771 goto abort;
5774 err = mddev_lock(mddev);
5775 if (err) {
5776 printk(KERN_INFO
5777 "md: ioctl lock interrupted, reason %d, cmd %d\n",
5778 err, cmd);
5779 goto abort;
5782 switch (cmd)
5784 case SET_ARRAY_INFO:
5786 mdu_array_info_t info;
5787 if (!arg)
5788 memset(&info, 0, sizeof(info));
5789 else if (copy_from_user(&info, argp, sizeof(info))) {
5790 err = -EFAULT;
5791 goto abort_unlock;
5793 if (mddev->pers) {
5794 err = update_array_info(mddev, &info);
5795 if (err) {
5796 printk(KERN_WARNING "md: couldn't update"
5797 " array info. %d\n", err);
5798 goto abort_unlock;
5800 goto done_unlock;
5802 if (!list_empty(&mddev->disks)) {
5803 printk(KERN_WARNING
5804 "md: array %s already has disks!\n",
5805 mdname(mddev));
5806 err = -EBUSY;
5807 goto abort_unlock;
5809 if (mddev->raid_disks) {
5810 printk(KERN_WARNING
5811 "md: array %s already initialised!\n",
5812 mdname(mddev));
5813 err = -EBUSY;
5814 goto abort_unlock;
5816 err = set_array_info(mddev, &info);
5817 if (err) {
5818 printk(KERN_WARNING "md: couldn't set"
5819 " array info. %d\n", err);
5820 goto abort_unlock;
5823 goto done_unlock;
5825 default:;
5829 * Commands querying/configuring an existing array:
5831 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
5832 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
5833 if ((!mddev->raid_disks && !mddev->external)
5834 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
5835 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
5836 && cmd != GET_BITMAP_FILE) {
5837 err = -ENODEV;
5838 goto abort_unlock;
5842 * Commands even a read-only array can execute:
5844 switch (cmd)
5846 case GET_ARRAY_INFO:
5847 err = get_array_info(mddev, argp);
5848 goto done_unlock;
5850 case GET_BITMAP_FILE:
5851 err = get_bitmap_file(mddev, argp);
5852 goto done_unlock;
5854 case GET_DISK_INFO:
5855 err = get_disk_info(mddev, argp);
5856 goto done_unlock;
5858 case RESTART_ARRAY_RW:
5859 err = restart_array(mddev);
5860 goto done_unlock;
5862 case STOP_ARRAY:
5863 err = do_md_stop(mddev, 0, 1);
5864 goto done_unlock;
5866 case STOP_ARRAY_RO:
5867 err = md_set_readonly(mddev, 1);
5868 goto done_unlock;
5870 case BLKROSET:
5871 if (get_user(ro, (int __user *)(arg))) {
5872 err = -EFAULT;
5873 goto done_unlock;
5875 err = -EINVAL;
5877 /* if the bdev is going readonly the value of mddev->ro
5878 * does not matter, no writes are coming
5880 if (ro)
5881 goto done_unlock;
5883 /* are we are already prepared for writes? */
5884 if (mddev->ro != 1)
5885 goto done_unlock;
5887 /* transitioning to readauto need only happen for
5888 * arrays that call md_write_start
5890 if (mddev->pers) {
5891 err = restart_array(mddev);
5892 if (err == 0) {
5893 mddev->ro = 2;
5894 set_disk_ro(mddev->gendisk, 0);
5897 goto done_unlock;
5901 * The remaining ioctls are changing the state of the
5902 * superblock, so we do not allow them on read-only arrays.
5903 * However non-MD ioctls (e.g. get-size) will still come through
5904 * here and hit the 'default' below, so only disallow
5905 * 'md' ioctls, and switch to rw mode if started auto-readonly.
5907 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
5908 if (mddev->ro == 2) {
5909 mddev->ro = 0;
5910 sysfs_notify_dirent_safe(mddev->sysfs_state);
5911 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5912 md_wakeup_thread(mddev->thread);
5913 } else {
5914 err = -EROFS;
5915 goto abort_unlock;
5919 switch (cmd)
5921 case ADD_NEW_DISK:
5923 mdu_disk_info_t info;
5924 if (copy_from_user(&info, argp, sizeof(info)))
5925 err = -EFAULT;
5926 else
5927 err = add_new_disk(mddev, &info);
5928 goto done_unlock;
5931 case HOT_REMOVE_DISK:
5932 err = hot_remove_disk(mddev, new_decode_dev(arg));
5933 goto done_unlock;
5935 case HOT_ADD_DISK:
5936 err = hot_add_disk(mddev, new_decode_dev(arg));
5937 goto done_unlock;
5939 case SET_DISK_FAULTY:
5940 err = set_disk_faulty(mddev, new_decode_dev(arg));
5941 goto done_unlock;
5943 case RUN_ARRAY:
5944 err = do_md_run(mddev);
5945 goto done_unlock;
5947 case SET_BITMAP_FILE:
5948 err = set_bitmap_file(mddev, (int)arg);
5949 goto done_unlock;
5951 default:
5952 err = -EINVAL;
5953 goto abort_unlock;
5956 done_unlock:
5957 abort_unlock:
5958 if (mddev->hold_active == UNTIL_IOCTL &&
5959 err != -EINVAL)
5960 mddev->hold_active = 0;
5961 mddev_unlock(mddev);
5963 return err;
5964 done:
5965 if (err)
5966 MD_BUG();
5967 abort:
5968 return err;
5970 #ifdef CONFIG_COMPAT
5971 static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
5972 unsigned int cmd, unsigned long arg)
5974 switch (cmd) {
5975 case HOT_REMOVE_DISK:
5976 case HOT_ADD_DISK:
5977 case SET_DISK_FAULTY:
5978 case SET_BITMAP_FILE:
5979 /* These take in integer arg, do not convert */
5980 break;
5981 default:
5982 arg = (unsigned long)compat_ptr(arg);
5983 break;
5986 return md_ioctl(bdev, mode, cmd, arg);
5988 #endif /* CONFIG_COMPAT */
5990 static int md_open(struct block_device *bdev, fmode_t mode)
5993 * Succeed if we can lock the mddev, which confirms that
5994 * it isn't being stopped right now.
5996 mddev_t *mddev = mddev_find(bdev->bd_dev);
5997 int err;
5999 if (mddev->gendisk != bdev->bd_disk) {
6000 /* we are racing with mddev_put which is discarding this
6001 * bd_disk.
6003 mddev_put(mddev);
6004 /* Wait until bdev->bd_disk is definitely gone */
6005 flush_workqueue(md_misc_wq);
6006 /* Then retry the open from the top */
6007 return -ERESTARTSYS;
6009 BUG_ON(mddev != bdev->bd_disk->private_data);
6011 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
6012 goto out;
6014 err = 0;
6015 atomic_inc(&mddev->openers);
6016 mutex_unlock(&mddev->open_mutex);
6018 check_disk_change(bdev);
6019 out:
6020 return err;
6023 static int md_release(struct gendisk *disk, fmode_t mode)
6025 mddev_t *mddev = disk->private_data;
6027 BUG_ON(!mddev);
6028 atomic_dec(&mddev->openers);
6029 mddev_put(mddev);
6031 return 0;
6034 static int md_media_changed(struct gendisk *disk)
6036 mddev_t *mddev = disk->private_data;
6038 return mddev->changed;
6041 static int md_revalidate(struct gendisk *disk)
6043 mddev_t *mddev = disk->private_data;
6045 mddev->changed = 0;
6046 return 0;
6048 static const struct block_device_operations md_fops =
6050 .owner = THIS_MODULE,
6051 .open = md_open,
6052 .release = md_release,
6053 .ioctl = md_ioctl,
6054 #ifdef CONFIG_COMPAT
6055 .compat_ioctl = md_compat_ioctl,
6056 #endif
6057 .getgeo = md_getgeo,
6058 .media_changed = md_media_changed,
6059 .revalidate_disk= md_revalidate,
6062 static int md_thread(void * arg)
6064 mdk_thread_t *thread = arg;
6067 * md_thread is a 'system-thread', it's priority should be very
6068 * high. We avoid resource deadlocks individually in each
6069 * raid personality. (RAID5 does preallocation) We also use RR and
6070 * the very same RT priority as kswapd, thus we will never get
6071 * into a priority inversion deadlock.
6073 * we definitely have to have equal or higher priority than
6074 * bdflush, otherwise bdflush will deadlock if there are too
6075 * many dirty RAID5 blocks.
6078 allow_signal(SIGKILL);
6079 while (!kthread_should_stop()) {
6081 /* We need to wait INTERRUPTIBLE so that
6082 * we don't add to the load-average.
6083 * That means we need to be sure no signals are
6084 * pending
6086 if (signal_pending(current))
6087 flush_signals(current);
6089 wait_event_interruptible_timeout
6090 (thread->wqueue,
6091 test_bit(THREAD_WAKEUP, &thread->flags)
6092 || kthread_should_stop(),
6093 thread->timeout);
6095 clear_bit(THREAD_WAKEUP, &thread->flags);
6096 if (!kthread_should_stop())
6097 thread->run(thread->mddev);
6100 return 0;
6103 void md_wakeup_thread(mdk_thread_t *thread)
6105 if (thread) {
6106 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
6107 set_bit(THREAD_WAKEUP, &thread->flags);
6108 wake_up(&thread->wqueue);
6112 mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
6113 const char *name)
6115 mdk_thread_t *thread;
6117 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
6118 if (!thread)
6119 return NULL;
6121 init_waitqueue_head(&thread->wqueue);
6123 thread->run = run;
6124 thread->mddev = mddev;
6125 thread->timeout = MAX_SCHEDULE_TIMEOUT;
6126 thread->tsk = kthread_run(md_thread, thread,
6127 "%s_%s",
6128 mdname(thread->mddev),
6129 name ?: mddev->pers->name);
6130 if (IS_ERR(thread->tsk)) {
6131 kfree(thread);
6132 return NULL;
6134 return thread;
6137 void md_unregister_thread(mdk_thread_t *thread)
6139 if (!thread)
6140 return;
6141 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
6143 kthread_stop(thread->tsk);
6144 kfree(thread);
6147 void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
6149 if (!mddev) {
6150 MD_BUG();
6151 return;
6154 if (!rdev || test_bit(Faulty, &rdev->flags))
6155 return;
6157 if (mddev->external)
6158 set_bit(Blocked, &rdev->flags);
6160 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
6161 mdname(mddev),
6162 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
6163 __builtin_return_address(0),__builtin_return_address(1),
6164 __builtin_return_address(2),__builtin_return_address(3));
6166 if (!mddev->pers)
6167 return;
6168 if (!mddev->pers->error_handler)
6169 return;
6170 mddev->pers->error_handler(mddev,rdev);
6171 if (mddev->degraded)
6172 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
6173 sysfs_notify_dirent_safe(rdev->sysfs_state);
6174 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6175 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6176 md_wakeup_thread(mddev->thread);
6177 if (mddev->event_work.func)
6178 queue_work(md_misc_wq, &mddev->event_work);
6179 md_new_event_inintr(mddev);
6182 /* seq_file implementation /proc/mdstat */
6184 static void status_unused(struct seq_file *seq)
6186 int i = 0;
6187 mdk_rdev_t *rdev;
6189 seq_printf(seq, "unused devices: ");
6191 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
6192 char b[BDEVNAME_SIZE];
6193 i++;
6194 seq_printf(seq, "%s ",
6195 bdevname(rdev->bdev,b));
6197 if (!i)
6198 seq_printf(seq, "<none>");
6200 seq_printf(seq, "\n");
6204 static void status_resync(struct seq_file *seq, mddev_t * mddev)
6206 sector_t max_sectors, resync, res;
6207 unsigned long dt, db;
6208 sector_t rt;
6209 int scale;
6210 unsigned int per_milli;
6212 resync = mddev->curr_resync - atomic_read(&mddev->recovery_active);
6214 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
6215 max_sectors = mddev->resync_max_sectors;
6216 else
6217 max_sectors = mddev->dev_sectors;
6220 * Should not happen.
6222 if (!max_sectors) {
6223 MD_BUG();
6224 return;
6226 /* Pick 'scale' such that (resync>>scale)*1000 will fit
6227 * in a sector_t, and (max_sectors>>scale) will fit in a
6228 * u32, as those are the requirements for sector_div.
6229 * Thus 'scale' must be at least 10
6231 scale = 10;
6232 if (sizeof(sector_t) > sizeof(unsigned long)) {
6233 while ( max_sectors/2 > (1ULL<<(scale+32)))
6234 scale++;
6236 res = (resync>>scale)*1000;
6237 sector_div(res, (u32)((max_sectors>>scale)+1));
6239 per_milli = res;
6241 int i, x = per_milli/50, y = 20-x;
6242 seq_printf(seq, "[");
6243 for (i = 0; i < x; i++)
6244 seq_printf(seq, "=");
6245 seq_printf(seq, ">");
6246 for (i = 0; i < y; i++)
6247 seq_printf(seq, ".");
6248 seq_printf(seq, "] ");
6250 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
6251 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
6252 "reshape" :
6253 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
6254 "check" :
6255 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
6256 "resync" : "recovery"))),
6257 per_milli/10, per_milli % 10,
6258 (unsigned long long) resync/2,
6259 (unsigned long long) max_sectors/2);
6262 * dt: time from mark until now
6263 * db: blocks written from mark until now
6264 * rt: remaining time
6266 * rt is a sector_t, so could be 32bit or 64bit.
6267 * So we divide before multiply in case it is 32bit and close
6268 * to the limit.
6269 * We scale the divisor (db) by 32 to avoid losing precision
6270 * near the end of resync when the number of remaining sectors
6271 * is close to 'db'.
6272 * We then divide rt by 32 after multiplying by db to compensate.
6273 * The '+1' avoids division by zero if db is very small.
6275 dt = ((jiffies - mddev->resync_mark) / HZ);
6276 if (!dt) dt++;
6277 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
6278 - mddev->resync_mark_cnt;
6280 rt = max_sectors - resync; /* number of remaining sectors */
6281 sector_div(rt, db/32+1);
6282 rt *= dt;
6283 rt >>= 5;
6285 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
6286 ((unsigned long)rt % 60)/6);
6288 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
6291 static void *md_seq_start(struct seq_file *seq, loff_t *pos)
6293 struct list_head *tmp;
6294 loff_t l = *pos;
6295 mddev_t *mddev;
6297 if (l >= 0x10000)
6298 return NULL;
6299 if (!l--)
6300 /* header */
6301 return (void*)1;
6303 spin_lock(&all_mddevs_lock);
6304 list_for_each(tmp,&all_mddevs)
6305 if (!l--) {
6306 mddev = list_entry(tmp, mddev_t, all_mddevs);
6307 mddev_get(mddev);
6308 spin_unlock(&all_mddevs_lock);
6309 return mddev;
6311 spin_unlock(&all_mddevs_lock);
6312 if (!l--)
6313 return (void*)2;/* tail */
6314 return NULL;
6317 static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
6319 struct list_head *tmp;
6320 mddev_t *next_mddev, *mddev = v;
6322 ++*pos;
6323 if (v == (void*)2)
6324 return NULL;
6326 spin_lock(&all_mddevs_lock);
6327 if (v == (void*)1)
6328 tmp = all_mddevs.next;
6329 else
6330 tmp = mddev->all_mddevs.next;
6331 if (tmp != &all_mddevs)
6332 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
6333 else {
6334 next_mddev = (void*)2;
6335 *pos = 0x10000;
6337 spin_unlock(&all_mddevs_lock);
6339 if (v != (void*)1)
6340 mddev_put(mddev);
6341 return next_mddev;
6345 static void md_seq_stop(struct seq_file *seq, void *v)
6347 mddev_t *mddev = v;
6349 if (mddev && v != (void*)1 && v != (void*)2)
6350 mddev_put(mddev);
6353 struct mdstat_info {
6354 int event;
6357 static int md_seq_show(struct seq_file *seq, void *v)
6359 mddev_t *mddev = v;
6360 sector_t sectors;
6361 mdk_rdev_t *rdev;
6362 struct mdstat_info *mi = seq->private;
6363 struct bitmap *bitmap;
6365 if (v == (void*)1) {
6366 struct mdk_personality *pers;
6367 seq_printf(seq, "Personalities : ");
6368 spin_lock(&pers_lock);
6369 list_for_each_entry(pers, &pers_list, list)
6370 seq_printf(seq, "[%s] ", pers->name);
6372 spin_unlock(&pers_lock);
6373 seq_printf(seq, "\n");
6374 mi->event = atomic_read(&md_event_count);
6375 return 0;
6377 if (v == (void*)2) {
6378 status_unused(seq);
6379 return 0;
6382 if (mddev_lock(mddev) < 0)
6383 return -EINTR;
6385 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
6386 seq_printf(seq, "%s : %sactive", mdname(mddev),
6387 mddev->pers ? "" : "in");
6388 if (mddev->pers) {
6389 if (mddev->ro==1)
6390 seq_printf(seq, " (read-only)");
6391 if (mddev->ro==2)
6392 seq_printf(seq, " (auto-read-only)");
6393 seq_printf(seq, " %s", mddev->pers->name);
6396 sectors = 0;
6397 list_for_each_entry(rdev, &mddev->disks, same_set) {
6398 char b[BDEVNAME_SIZE];
6399 seq_printf(seq, " %s[%d]",
6400 bdevname(rdev->bdev,b), rdev->desc_nr);
6401 if (test_bit(WriteMostly, &rdev->flags))
6402 seq_printf(seq, "(W)");
6403 if (test_bit(Faulty, &rdev->flags)) {
6404 seq_printf(seq, "(F)");
6405 continue;
6406 } else if (rdev->raid_disk < 0)
6407 seq_printf(seq, "(S)"); /* spare */
6408 sectors += rdev->sectors;
6411 if (!list_empty(&mddev->disks)) {
6412 if (mddev->pers)
6413 seq_printf(seq, "\n %llu blocks",
6414 (unsigned long long)
6415 mddev->array_sectors / 2);
6416 else
6417 seq_printf(seq, "\n %llu blocks",
6418 (unsigned long long)sectors / 2);
6420 if (mddev->persistent) {
6421 if (mddev->major_version != 0 ||
6422 mddev->minor_version != 90) {
6423 seq_printf(seq," super %d.%d",
6424 mddev->major_version,
6425 mddev->minor_version);
6427 } else if (mddev->external)
6428 seq_printf(seq, " super external:%s",
6429 mddev->metadata_type);
6430 else
6431 seq_printf(seq, " super non-persistent");
6433 if (mddev->pers) {
6434 mddev->pers->status(seq, mddev);
6435 seq_printf(seq, "\n ");
6436 if (mddev->pers->sync_request) {
6437 if (mddev->curr_resync > 2) {
6438 status_resync(seq, mddev);
6439 seq_printf(seq, "\n ");
6440 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
6441 seq_printf(seq, "\tresync=DELAYED\n ");
6442 else if (mddev->recovery_cp < MaxSector)
6443 seq_printf(seq, "\tresync=PENDING\n ");
6445 } else
6446 seq_printf(seq, "\n ");
6448 if ((bitmap = mddev->bitmap)) {
6449 unsigned long chunk_kb;
6450 unsigned long flags;
6451 spin_lock_irqsave(&bitmap->lock, flags);
6452 chunk_kb = mddev->bitmap_info.chunksize >> 10;
6453 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
6454 "%lu%s chunk",
6455 bitmap->pages - bitmap->missing_pages,
6456 bitmap->pages,
6457 (bitmap->pages - bitmap->missing_pages)
6458 << (PAGE_SHIFT - 10),
6459 chunk_kb ? chunk_kb : mddev->bitmap_info.chunksize,
6460 chunk_kb ? "KB" : "B");
6461 if (bitmap->file) {
6462 seq_printf(seq, ", file: ");
6463 seq_path(seq, &bitmap->file->f_path, " \t\n");
6466 seq_printf(seq, "\n");
6467 spin_unlock_irqrestore(&bitmap->lock, flags);
6470 seq_printf(seq, "\n");
6472 mddev_unlock(mddev);
6474 return 0;
6477 static const struct seq_operations md_seq_ops = {
6478 .start = md_seq_start,
6479 .next = md_seq_next,
6480 .stop = md_seq_stop,
6481 .show = md_seq_show,
6484 static int md_seq_open(struct inode *inode, struct file *file)
6486 int error;
6487 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
6488 if (mi == NULL)
6489 return -ENOMEM;
6491 error = seq_open(file, &md_seq_ops);
6492 if (error)
6493 kfree(mi);
6494 else {
6495 struct seq_file *p = file->private_data;
6496 p->private = mi;
6497 mi->event = atomic_read(&md_event_count);
6499 return error;
6502 static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
6504 struct seq_file *m = filp->private_data;
6505 struct mdstat_info *mi = m->private;
6506 int mask;
6508 poll_wait(filp, &md_event_waiters, wait);
6510 /* always allow read */
6511 mask = POLLIN | POLLRDNORM;
6513 if (mi->event != atomic_read(&md_event_count))
6514 mask |= POLLERR | POLLPRI;
6515 return mask;
6518 static const struct file_operations md_seq_fops = {
6519 .owner = THIS_MODULE,
6520 .open = md_seq_open,
6521 .read = seq_read,
6522 .llseek = seq_lseek,
6523 .release = seq_release_private,
6524 .poll = mdstat_poll,
6527 int register_md_personality(struct mdk_personality *p)
6529 spin_lock(&pers_lock);
6530 list_add_tail(&p->list, &pers_list);
6531 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
6532 spin_unlock(&pers_lock);
6533 return 0;
6536 int unregister_md_personality(struct mdk_personality *p)
6538 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
6539 spin_lock(&pers_lock);
6540 list_del_init(&p->list);
6541 spin_unlock(&pers_lock);
6542 return 0;
6545 static int is_mddev_idle(mddev_t *mddev, int init)
6547 mdk_rdev_t * rdev;
6548 int idle;
6549 int curr_events;
6551 idle = 1;
6552 rcu_read_lock();
6553 rdev_for_each_rcu(rdev, mddev) {
6554 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
6555 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
6556 (int)part_stat_read(&disk->part0, sectors[1]) -
6557 atomic_read(&disk->sync_io);
6558 /* sync IO will cause sync_io to increase before the disk_stats
6559 * as sync_io is counted when a request starts, and
6560 * disk_stats is counted when it completes.
6561 * So resync activity will cause curr_events to be smaller than
6562 * when there was no such activity.
6563 * non-sync IO will cause disk_stat to increase without
6564 * increasing sync_io so curr_events will (eventually)
6565 * be larger than it was before. Once it becomes
6566 * substantially larger, the test below will cause
6567 * the array to appear non-idle, and resync will slow
6568 * down.
6569 * If there is a lot of outstanding resync activity when
6570 * we set last_event to curr_events, then all that activity
6571 * completing might cause the array to appear non-idle
6572 * and resync will be slowed down even though there might
6573 * not have been non-resync activity. This will only
6574 * happen once though. 'last_events' will soon reflect
6575 * the state where there is little or no outstanding
6576 * resync requests, and further resync activity will
6577 * always make curr_events less than last_events.
6580 if (init || curr_events - rdev->last_events > 64) {
6581 rdev->last_events = curr_events;
6582 idle = 0;
6585 rcu_read_unlock();
6586 return idle;
6589 void md_done_sync(mddev_t *mddev, int blocks, int ok)
6591 /* another "blocks" (512byte) blocks have been synced */
6592 atomic_sub(blocks, &mddev->recovery_active);
6593 wake_up(&mddev->recovery_wait);
6594 if (!ok) {
6595 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6596 md_wakeup_thread(mddev->thread);
6597 // stop recovery, signal do_sync ....
6602 /* md_write_start(mddev, bi)
6603 * If we need to update some array metadata (e.g. 'active' flag
6604 * in superblock) before writing, schedule a superblock update
6605 * and wait for it to complete.
6607 void md_write_start(mddev_t *mddev, struct bio *bi)
6609 int did_change = 0;
6610 if (bio_data_dir(bi) != WRITE)
6611 return;
6613 BUG_ON(mddev->ro == 1);
6614 if (mddev->ro == 2) {
6615 /* need to switch to read/write */
6616 mddev->ro = 0;
6617 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6618 md_wakeup_thread(mddev->thread);
6619 md_wakeup_thread(mddev->sync_thread);
6620 did_change = 1;
6622 atomic_inc(&mddev->writes_pending);
6623 if (mddev->safemode == 1)
6624 mddev->safemode = 0;
6625 if (mddev->in_sync) {
6626 spin_lock_irq(&mddev->write_lock);
6627 if (mddev->in_sync) {
6628 mddev->in_sync = 0;
6629 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6630 set_bit(MD_CHANGE_PENDING, &mddev->flags);
6631 md_wakeup_thread(mddev->thread);
6632 did_change = 1;
6634 spin_unlock_irq(&mddev->write_lock);
6636 if (did_change)
6637 sysfs_notify_dirent_safe(mddev->sysfs_state);
6638 wait_event(mddev->sb_wait,
6639 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
6642 void md_write_end(mddev_t *mddev)
6644 if (atomic_dec_and_test(&mddev->writes_pending)) {
6645 if (mddev->safemode == 2)
6646 md_wakeup_thread(mddev->thread);
6647 else if (mddev->safemode_delay)
6648 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
6652 /* md_allow_write(mddev)
6653 * Calling this ensures that the array is marked 'active' so that writes
6654 * may proceed without blocking. It is important to call this before
6655 * attempting a GFP_KERNEL allocation while holding the mddev lock.
6656 * Must be called with mddev_lock held.
6658 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
6659 * is dropped, so return -EAGAIN after notifying userspace.
6661 int md_allow_write(mddev_t *mddev)
6663 if (!mddev->pers)
6664 return 0;
6665 if (mddev->ro)
6666 return 0;
6667 if (!mddev->pers->sync_request)
6668 return 0;
6670 spin_lock_irq(&mddev->write_lock);
6671 if (mddev->in_sync) {
6672 mddev->in_sync = 0;
6673 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6674 set_bit(MD_CHANGE_PENDING, &mddev->flags);
6675 if (mddev->safemode_delay &&
6676 mddev->safemode == 0)
6677 mddev->safemode = 1;
6678 spin_unlock_irq(&mddev->write_lock);
6679 md_update_sb(mddev, 0);
6680 sysfs_notify_dirent_safe(mddev->sysfs_state);
6681 } else
6682 spin_unlock_irq(&mddev->write_lock);
6684 if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
6685 return -EAGAIN;
6686 else
6687 return 0;
6689 EXPORT_SYMBOL_GPL(md_allow_write);
6691 void md_unplug(mddev_t *mddev)
6693 if (mddev->plug)
6694 mddev->plug->unplug_fn(mddev->plug);
6697 #define SYNC_MARKS 10
6698 #define SYNC_MARK_STEP (3*HZ)
6699 void md_do_sync(mddev_t *mddev)
6701 mddev_t *mddev2;
6702 unsigned int currspeed = 0,
6703 window;
6704 sector_t max_sectors,j, io_sectors;
6705 unsigned long mark[SYNC_MARKS];
6706 sector_t mark_cnt[SYNC_MARKS];
6707 int last_mark,m;
6708 struct list_head *tmp;
6709 sector_t last_check;
6710 int skipped = 0;
6711 mdk_rdev_t *rdev;
6712 char *desc;
6714 /* just incase thread restarts... */
6715 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
6716 return;
6717 if (mddev->ro) /* never try to sync a read-only array */
6718 return;
6720 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6721 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
6722 desc = "data-check";
6723 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6724 desc = "requested-resync";
6725 else
6726 desc = "resync";
6727 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
6728 desc = "reshape";
6729 else
6730 desc = "recovery";
6732 /* we overload curr_resync somewhat here.
6733 * 0 == not engaged in resync at all
6734 * 2 == checking that there is no conflict with another sync
6735 * 1 == like 2, but have yielded to allow conflicting resync to
6736 * commense
6737 * other == active in resync - this many blocks
6739 * Before starting a resync we must have set curr_resync to
6740 * 2, and then checked that every "conflicting" array has curr_resync
6741 * less than ours. When we find one that is the same or higher
6742 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
6743 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
6744 * This will mean we have to start checking from the beginning again.
6748 do {
6749 mddev->curr_resync = 2;
6751 try_again:
6752 if (kthread_should_stop())
6753 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6755 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6756 goto skip;
6757 for_each_mddev(mddev2, tmp) {
6758 if (mddev2 == mddev)
6759 continue;
6760 if (!mddev->parallel_resync
6761 && mddev2->curr_resync
6762 && match_mddev_units(mddev, mddev2)) {
6763 DEFINE_WAIT(wq);
6764 if (mddev < mddev2 && mddev->curr_resync == 2) {
6765 /* arbitrarily yield */
6766 mddev->curr_resync = 1;
6767 wake_up(&resync_wait);
6769 if (mddev > mddev2 && mddev->curr_resync == 1)
6770 /* no need to wait here, we can wait the next
6771 * time 'round when curr_resync == 2
6773 continue;
6774 /* We need to wait 'interruptible' so as not to
6775 * contribute to the load average, and not to
6776 * be caught by 'softlockup'
6778 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
6779 if (!kthread_should_stop() &&
6780 mddev2->curr_resync >= mddev->curr_resync) {
6781 printk(KERN_INFO "md: delaying %s of %s"
6782 " until %s has finished (they"
6783 " share one or more physical units)\n",
6784 desc, mdname(mddev), mdname(mddev2));
6785 mddev_put(mddev2);
6786 if (signal_pending(current))
6787 flush_signals(current);
6788 schedule();
6789 finish_wait(&resync_wait, &wq);
6790 goto try_again;
6792 finish_wait(&resync_wait, &wq);
6795 } while (mddev->curr_resync < 2);
6797 j = 0;
6798 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6799 /* resync follows the size requested by the personality,
6800 * which defaults to physical size, but can be virtual size
6802 max_sectors = mddev->resync_max_sectors;
6803 mddev->resync_mismatches = 0;
6804 /* we don't use the checkpoint if there's a bitmap */
6805 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6806 j = mddev->resync_min;
6807 else if (!mddev->bitmap)
6808 j = mddev->recovery_cp;
6810 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
6811 max_sectors = mddev->dev_sectors;
6812 else {
6813 /* recovery follows the physical size of devices */
6814 max_sectors = mddev->dev_sectors;
6815 j = MaxSector;
6816 rcu_read_lock();
6817 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
6818 if (rdev->raid_disk >= 0 &&
6819 !test_bit(Faulty, &rdev->flags) &&
6820 !test_bit(In_sync, &rdev->flags) &&
6821 rdev->recovery_offset < j)
6822 j = rdev->recovery_offset;
6823 rcu_read_unlock();
6826 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
6827 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
6828 " %d KB/sec/disk.\n", speed_min(mddev));
6829 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
6830 "(but not more than %d KB/sec) for %s.\n",
6831 speed_max(mddev), desc);
6833 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
6835 io_sectors = 0;
6836 for (m = 0; m < SYNC_MARKS; m++) {
6837 mark[m] = jiffies;
6838 mark_cnt[m] = io_sectors;
6840 last_mark = 0;
6841 mddev->resync_mark = mark[last_mark];
6842 mddev->resync_mark_cnt = mark_cnt[last_mark];
6845 * Tune reconstruction:
6847 window = 32*(PAGE_SIZE/512);
6848 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
6849 window/2,(unsigned long long) max_sectors/2);
6851 atomic_set(&mddev->recovery_active, 0);
6852 last_check = 0;
6854 if (j>2) {
6855 printk(KERN_INFO
6856 "md: resuming %s of %s from checkpoint.\n",
6857 desc, mdname(mddev));
6858 mddev->curr_resync = j;
6860 mddev->curr_resync_completed = j;
6862 while (j < max_sectors) {
6863 sector_t sectors;
6865 skipped = 0;
6867 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
6868 ((mddev->curr_resync > mddev->curr_resync_completed &&
6869 (mddev->curr_resync - mddev->curr_resync_completed)
6870 > (max_sectors >> 4)) ||
6871 (j - mddev->curr_resync_completed)*2
6872 >= mddev->resync_max - mddev->curr_resync_completed
6873 )) {
6874 /* time to update curr_resync_completed */
6875 wait_event(mddev->recovery_wait,
6876 atomic_read(&mddev->recovery_active) == 0);
6877 mddev->curr_resync_completed = j;
6878 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6879 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
6882 while (j >= mddev->resync_max && !kthread_should_stop()) {
6883 /* As this condition is controlled by user-space,
6884 * we can block indefinitely, so use '_interruptible'
6885 * to avoid triggering warnings.
6887 flush_signals(current); /* just in case */
6888 wait_event_interruptible(mddev->recovery_wait,
6889 mddev->resync_max > j
6890 || kthread_should_stop());
6893 if (kthread_should_stop())
6894 goto interrupted;
6896 sectors = mddev->pers->sync_request(mddev, j, &skipped,
6897 currspeed < speed_min(mddev));
6898 if (sectors == 0) {
6899 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6900 goto out;
6903 if (!skipped) { /* actual IO requested */
6904 io_sectors += sectors;
6905 atomic_add(sectors, &mddev->recovery_active);
6908 j += sectors;
6909 if (j>1) mddev->curr_resync = j;
6910 mddev->curr_mark_cnt = io_sectors;
6911 if (last_check == 0)
6912 /* this is the earliers that rebuilt will be
6913 * visible in /proc/mdstat
6915 md_new_event(mddev);
6917 if (last_check + window > io_sectors || j == max_sectors)
6918 continue;
6920 last_check = io_sectors;
6922 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6923 break;
6925 repeat:
6926 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
6927 /* step marks */
6928 int next = (last_mark+1) % SYNC_MARKS;
6930 mddev->resync_mark = mark[next];
6931 mddev->resync_mark_cnt = mark_cnt[next];
6932 mark[next] = jiffies;
6933 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
6934 last_mark = next;
6938 if (kthread_should_stop())
6939 goto interrupted;
6943 * this loop exits only if either when we are slower than
6944 * the 'hard' speed limit, or the system was IO-idle for
6945 * a jiffy.
6946 * the system might be non-idle CPU-wise, but we only care
6947 * about not overloading the IO subsystem. (things like an
6948 * e2fsck being done on the RAID array should execute fast)
6950 cond_resched();
6952 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
6953 /((jiffies-mddev->resync_mark)/HZ +1) +1;
6955 if (currspeed > speed_min(mddev)) {
6956 if ((currspeed > speed_max(mddev)) ||
6957 !is_mddev_idle(mddev, 0)) {
6958 msleep(500);
6959 goto repeat;
6963 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
6965 * this also signals 'finished resyncing' to md_stop
6967 out:
6968 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
6970 /* tell personality that we are finished */
6971 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
6973 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
6974 mddev->curr_resync > 2) {
6975 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6976 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6977 if (mddev->curr_resync >= mddev->recovery_cp) {
6978 printk(KERN_INFO
6979 "md: checkpointing %s of %s.\n",
6980 desc, mdname(mddev));
6981 mddev->recovery_cp = mddev->curr_resync;
6983 } else
6984 mddev->recovery_cp = MaxSector;
6985 } else {
6986 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6987 mddev->curr_resync = MaxSector;
6988 rcu_read_lock();
6989 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
6990 if (rdev->raid_disk >= 0 &&
6991 mddev->delta_disks >= 0 &&
6992 !test_bit(Faulty, &rdev->flags) &&
6993 !test_bit(In_sync, &rdev->flags) &&
6994 rdev->recovery_offset < mddev->curr_resync)
6995 rdev->recovery_offset = mddev->curr_resync;
6996 rcu_read_unlock();
6999 set_bit(MD_CHANGE_DEVS, &mddev->flags);
7001 skip:
7002 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7003 /* We completed so min/max setting can be forgotten if used. */
7004 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7005 mddev->resync_min = 0;
7006 mddev->resync_max = MaxSector;
7007 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7008 mddev->resync_min = mddev->curr_resync_completed;
7009 mddev->curr_resync = 0;
7010 wake_up(&resync_wait);
7011 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
7012 md_wakeup_thread(mddev->thread);
7013 return;
7015 interrupted:
7017 * got a signal, exit.
7019 printk(KERN_INFO
7020 "md: md_do_sync() got signal ... exiting\n");
7021 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7022 goto out;
7025 EXPORT_SYMBOL_GPL(md_do_sync);
7028 static int remove_and_add_spares(mddev_t *mddev)
7030 mdk_rdev_t *rdev;
7031 int spares = 0;
7033 mddev->curr_resync_completed = 0;
7035 list_for_each_entry(rdev, &mddev->disks, same_set)
7036 if (rdev->raid_disk >= 0 &&
7037 !test_bit(Blocked, &rdev->flags) &&
7038 (test_bit(Faulty, &rdev->flags) ||
7039 ! test_bit(In_sync, &rdev->flags)) &&
7040 atomic_read(&rdev->nr_pending)==0) {
7041 if (mddev->pers->hot_remove_disk(
7042 mddev, rdev->raid_disk)==0) {
7043 char nm[20];
7044 sprintf(nm,"rd%d", rdev->raid_disk);
7045 sysfs_remove_link(&mddev->kobj, nm);
7046 rdev->raid_disk = -1;
7050 if (mddev->degraded && !mddev->recovery_disabled) {
7051 list_for_each_entry(rdev, &mddev->disks, same_set) {
7052 if (rdev->raid_disk >= 0 &&
7053 !test_bit(In_sync, &rdev->flags) &&
7054 !test_bit(Blocked, &rdev->flags))
7055 spares++;
7056 if (rdev->raid_disk < 0
7057 && !test_bit(Faulty, &rdev->flags)) {
7058 rdev->recovery_offset = 0;
7059 if (mddev->pers->
7060 hot_add_disk(mddev, rdev) == 0) {
7061 char nm[20];
7062 sprintf(nm, "rd%d", rdev->raid_disk);
7063 if (sysfs_create_link(&mddev->kobj,
7064 &rdev->kobj, nm))
7065 /* failure here is OK */;
7066 spares++;
7067 md_new_event(mddev);
7068 set_bit(MD_CHANGE_DEVS, &mddev->flags);
7069 } else
7070 break;
7074 return spares;
7077 static void reap_sync_thread(mddev_t *mddev)
7079 mdk_rdev_t *rdev;
7081 /* resync has finished, collect result */
7082 md_unregister_thread(mddev->sync_thread);
7083 mddev->sync_thread = NULL;
7084 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
7085 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
7086 /* success...*/
7087 /* activate any spares */
7088 if (mddev->pers->spare_active(mddev))
7089 sysfs_notify(&mddev->kobj, NULL,
7090 "degraded");
7092 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7093 mddev->pers->finish_reshape)
7094 mddev->pers->finish_reshape(mddev);
7095 md_update_sb(mddev, 1);
7097 /* if array is no-longer degraded, then any saved_raid_disk
7098 * information must be scrapped
7100 if (!mddev->degraded)
7101 list_for_each_entry(rdev, &mddev->disks, same_set)
7102 rdev->saved_raid_disk = -1;
7104 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7105 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7106 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7107 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7108 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
7109 /* flag recovery needed just to double check */
7110 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7111 sysfs_notify_dirent_safe(mddev->sysfs_action);
7112 md_new_event(mddev);
7116 * This routine is regularly called by all per-raid-array threads to
7117 * deal with generic issues like resync and super-block update.
7118 * Raid personalities that don't have a thread (linear/raid0) do not
7119 * need this as they never do any recovery or update the superblock.
7121 * It does not do any resync itself, but rather "forks" off other threads
7122 * to do that as needed.
7123 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
7124 * "->recovery" and create a thread at ->sync_thread.
7125 * When the thread finishes it sets MD_RECOVERY_DONE
7126 * and wakeups up this thread which will reap the thread and finish up.
7127 * This thread also removes any faulty devices (with nr_pending == 0).
7129 * The overall approach is:
7130 * 1/ if the superblock needs updating, update it.
7131 * 2/ If a recovery thread is running, don't do anything else.
7132 * 3/ If recovery has finished, clean up, possibly marking spares active.
7133 * 4/ If there are any faulty devices, remove them.
7134 * 5/ If array is degraded, try to add spares devices
7135 * 6/ If array has spares or is not in-sync, start a resync thread.
7137 void md_check_recovery(mddev_t *mddev)
7139 if (mddev->bitmap)
7140 bitmap_daemon_work(mddev);
7142 if (mddev->ro)
7143 return;
7145 if (signal_pending(current)) {
7146 if (mddev->pers->sync_request && !mddev->external) {
7147 printk(KERN_INFO "md: %s in immediate safe mode\n",
7148 mdname(mddev));
7149 mddev->safemode = 2;
7151 flush_signals(current);
7154 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
7155 return;
7156 if ( ! (
7157 (mddev->flags & ~ (1<<MD_CHANGE_PENDING)) ||
7158 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
7159 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
7160 (mddev->external == 0 && mddev->safemode == 1) ||
7161 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
7162 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
7164 return;
7166 if (mddev_trylock(mddev)) {
7167 int spares = 0;
7169 if (mddev->ro) {
7170 /* Only thing we do on a ro array is remove
7171 * failed devices.
7173 mdk_rdev_t *rdev;
7174 list_for_each_entry(rdev, &mddev->disks, same_set)
7175 if (rdev->raid_disk >= 0 &&
7176 !test_bit(Blocked, &rdev->flags) &&
7177 test_bit(Faulty, &rdev->flags) &&
7178 atomic_read(&rdev->nr_pending)==0) {
7179 if (mddev->pers->hot_remove_disk(
7180 mddev, rdev->raid_disk)==0) {
7181 char nm[20];
7182 sprintf(nm,"rd%d", rdev->raid_disk);
7183 sysfs_remove_link(&mddev->kobj, nm);
7184 rdev->raid_disk = -1;
7187 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7188 goto unlock;
7191 if (!mddev->external) {
7192 int did_change = 0;
7193 spin_lock_irq(&mddev->write_lock);
7194 if (mddev->safemode &&
7195 !atomic_read(&mddev->writes_pending) &&
7196 !mddev->in_sync &&
7197 mddev->recovery_cp == MaxSector) {
7198 mddev->in_sync = 1;
7199 did_change = 1;
7200 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
7202 if (mddev->safemode == 1)
7203 mddev->safemode = 0;
7204 spin_unlock_irq(&mddev->write_lock);
7205 if (did_change)
7206 sysfs_notify_dirent_safe(mddev->sysfs_state);
7209 if (mddev->flags)
7210 md_update_sb(mddev, 0);
7212 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
7213 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
7214 /* resync/recovery still happening */
7215 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7216 goto unlock;
7218 if (mddev->sync_thread) {
7219 reap_sync_thread(mddev);
7220 goto unlock;
7222 /* Set RUNNING before clearing NEEDED to avoid
7223 * any transients in the value of "sync_action".
7225 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7226 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7227 /* Clear some bits that don't mean anything, but
7228 * might be left set
7230 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
7231 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
7233 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
7234 goto unlock;
7235 /* no recovery is running.
7236 * remove any failed drives, then
7237 * add spares if possible.
7238 * Spare are also removed and re-added, to allow
7239 * the personality to fail the re-add.
7242 if (mddev->reshape_position != MaxSector) {
7243 if (mddev->pers->check_reshape == NULL ||
7244 mddev->pers->check_reshape(mddev) != 0)
7245 /* Cannot proceed */
7246 goto unlock;
7247 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7248 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
7249 } else if ((spares = remove_and_add_spares(mddev))) {
7250 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7251 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
7252 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7253 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
7254 } else if (mddev->recovery_cp < MaxSector) {
7255 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7256 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
7257 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
7258 /* nothing to be done ... */
7259 goto unlock;
7261 if (mddev->pers->sync_request) {
7262 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
7263 /* We are adding a device or devices to an array
7264 * which has the bitmap stored on all devices.
7265 * So make sure all bitmap pages get written
7267 bitmap_write_all(mddev->bitmap);
7269 mddev->sync_thread = md_register_thread(md_do_sync,
7270 mddev,
7271 "resync");
7272 if (!mddev->sync_thread) {
7273 printk(KERN_ERR "%s: could not start resync"
7274 " thread...\n",
7275 mdname(mddev));
7276 /* leave the spares where they are, it shouldn't hurt */
7277 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7278 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7279 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7280 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7281 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
7282 } else
7283 md_wakeup_thread(mddev->sync_thread);
7284 sysfs_notify_dirent_safe(mddev->sysfs_action);
7285 md_new_event(mddev);
7287 unlock:
7288 if (!mddev->sync_thread) {
7289 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7290 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
7291 &mddev->recovery))
7292 if (mddev->sysfs_action)
7293 sysfs_notify_dirent_safe(mddev->sysfs_action);
7295 mddev_unlock(mddev);
7299 void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
7301 sysfs_notify_dirent_safe(rdev->sysfs_state);
7302 wait_event_timeout(rdev->blocked_wait,
7303 !test_bit(Blocked, &rdev->flags),
7304 msecs_to_jiffies(5000));
7305 rdev_dec_pending(rdev, mddev);
7307 EXPORT_SYMBOL(md_wait_for_blocked_rdev);
7309 static int md_notify_reboot(struct notifier_block *this,
7310 unsigned long code, void *x)
7312 struct list_head *tmp;
7313 mddev_t *mddev;
7315 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
7317 printk(KERN_INFO "md: stopping all md devices.\n");
7319 for_each_mddev(mddev, tmp)
7320 if (mddev_trylock(mddev)) {
7321 /* Force a switch to readonly even array
7322 * appears to still be in use. Hence
7323 * the '100'.
7325 md_set_readonly(mddev, 100);
7326 mddev_unlock(mddev);
7329 * certain more exotic SCSI devices are known to be
7330 * volatile wrt too early system reboots. While the
7331 * right place to handle this issue is the given
7332 * driver, we do want to have a safe RAID driver ...
7334 mdelay(1000*1);
7336 return NOTIFY_DONE;
7339 static struct notifier_block md_notifier = {
7340 .notifier_call = md_notify_reboot,
7341 .next = NULL,
7342 .priority = INT_MAX, /* before any real devices */
7345 static void md_geninit(void)
7347 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
7349 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
7352 static int __init md_init(void)
7354 int ret = -ENOMEM;
7356 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
7357 if (!md_wq)
7358 goto err_wq;
7360 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
7361 if (!md_misc_wq)
7362 goto err_misc_wq;
7364 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
7365 goto err_md;
7367 if ((ret = register_blkdev(0, "mdp")) < 0)
7368 goto err_mdp;
7369 mdp_major = ret;
7371 blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE,
7372 md_probe, NULL, NULL);
7373 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
7374 md_probe, NULL, NULL);
7376 register_reboot_notifier(&md_notifier);
7377 raid_table_header = register_sysctl_table(raid_root_table);
7379 md_geninit();
7380 return 0;
7382 err_mdp:
7383 unregister_blkdev(MD_MAJOR, "md");
7384 err_md:
7385 destroy_workqueue(md_misc_wq);
7386 err_misc_wq:
7387 destroy_workqueue(md_wq);
7388 err_wq:
7389 return ret;
7392 #ifndef MODULE
7395 * Searches all registered partitions for autorun RAID arrays
7396 * at boot time.
7399 static LIST_HEAD(all_detected_devices);
7400 struct detected_devices_node {
7401 struct list_head list;
7402 dev_t dev;
7405 void md_autodetect_dev(dev_t dev)
7407 struct detected_devices_node *node_detected_dev;
7409 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
7410 if (node_detected_dev) {
7411 node_detected_dev->dev = dev;
7412 list_add_tail(&node_detected_dev->list, &all_detected_devices);
7413 } else {
7414 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
7415 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
7420 static void autostart_arrays(int part)
7422 mdk_rdev_t *rdev;
7423 struct detected_devices_node *node_detected_dev;
7424 dev_t dev;
7425 int i_scanned, i_passed;
7427 i_scanned = 0;
7428 i_passed = 0;
7430 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
7432 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
7433 i_scanned++;
7434 node_detected_dev = list_entry(all_detected_devices.next,
7435 struct detected_devices_node, list);
7436 list_del(&node_detected_dev->list);
7437 dev = node_detected_dev->dev;
7438 kfree(node_detected_dev);
7439 rdev = md_import_device(dev,0, 90);
7440 if (IS_ERR(rdev))
7441 continue;
7443 if (test_bit(Faulty, &rdev->flags)) {
7444 MD_BUG();
7445 continue;
7447 set_bit(AutoDetected, &rdev->flags);
7448 list_add(&rdev->same_set, &pending_raid_disks);
7449 i_passed++;
7452 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
7453 i_scanned, i_passed);
7455 autorun_devices(part);
7458 #endif /* !MODULE */
7460 static __exit void md_exit(void)
7462 mddev_t *mddev;
7463 struct list_head *tmp;
7465 blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS);
7466 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
7468 unregister_blkdev(MD_MAJOR,"md");
7469 unregister_blkdev(mdp_major, "mdp");
7470 unregister_reboot_notifier(&md_notifier);
7471 unregister_sysctl_table(raid_table_header);
7472 remove_proc_entry("mdstat", NULL);
7473 for_each_mddev(mddev, tmp) {
7474 export_array(mddev);
7475 mddev->hold_active = 0;
7477 destroy_workqueue(md_misc_wq);
7478 destroy_workqueue(md_wq);
7481 subsys_initcall(md_init);
7482 module_exit(md_exit)
7484 static int get_ro(char *buffer, struct kernel_param *kp)
7486 return sprintf(buffer, "%d", start_readonly);
7488 static int set_ro(const char *val, struct kernel_param *kp)
7490 char *e;
7491 int num = simple_strtoul(val, &e, 10);
7492 if (*val && (*e == '\0' || *e == '\n')) {
7493 start_readonly = num;
7494 return 0;
7496 return -EINVAL;
7499 module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
7500 module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
7502 module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
7504 EXPORT_SYMBOL(register_md_personality);
7505 EXPORT_SYMBOL(unregister_md_personality);
7506 EXPORT_SYMBOL(md_error);
7507 EXPORT_SYMBOL(md_done_sync);
7508 EXPORT_SYMBOL(md_write_start);
7509 EXPORT_SYMBOL(md_write_end);
7510 EXPORT_SYMBOL(md_register_thread);
7511 EXPORT_SYMBOL(md_unregister_thread);
7512 EXPORT_SYMBOL(md_wakeup_thread);
7513 EXPORT_SYMBOL(md_check_recovery);
7514 MODULE_LICENSE("GPL");
7515 MODULE_DESCRIPTION("MD RAID framework");
7516 MODULE_ALIAS("md");
7517 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);