Merge branch 'sched-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / md / md.c
blob4332fc2f25d4a2260b575f66229514bad10a2fa4
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 md_wakeup_thread(mddev->thread);
356 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
358 EXPORT_SYMBOL_GPL(mddev_resume);
360 int mddev_congested(mddev_t *mddev, int bits)
362 return mddev->suspended;
364 EXPORT_SYMBOL(mddev_congested);
367 * Generic flush handling for md
370 static void md_end_flush(struct bio *bio, int err)
372 mdk_rdev_t *rdev = bio->bi_private;
373 mddev_t *mddev = rdev->mddev;
375 rdev_dec_pending(rdev, mddev);
377 if (atomic_dec_and_test(&mddev->flush_pending)) {
378 /* The pre-request flush has finished */
379 queue_work(md_wq, &mddev->flush_work);
381 bio_put(bio);
384 static void md_submit_flush_data(struct work_struct *ws);
386 static void submit_flushes(struct work_struct *ws)
388 mddev_t *mddev = container_of(ws, mddev_t, flush_work);
389 mdk_rdev_t *rdev;
391 INIT_WORK(&mddev->flush_work, md_submit_flush_data);
392 atomic_set(&mddev->flush_pending, 1);
393 rcu_read_lock();
394 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
395 if (rdev->raid_disk >= 0 &&
396 !test_bit(Faulty, &rdev->flags)) {
397 /* Take two references, one is dropped
398 * when request finishes, one after
399 * we reclaim rcu_read_lock
401 struct bio *bi;
402 atomic_inc(&rdev->nr_pending);
403 atomic_inc(&rdev->nr_pending);
404 rcu_read_unlock();
405 bi = bio_alloc_mddev(GFP_KERNEL, 0, mddev);
406 bi->bi_end_io = md_end_flush;
407 bi->bi_private = rdev;
408 bi->bi_bdev = rdev->bdev;
409 atomic_inc(&mddev->flush_pending);
410 submit_bio(WRITE_FLUSH, bi);
411 rcu_read_lock();
412 rdev_dec_pending(rdev, mddev);
414 rcu_read_unlock();
415 if (atomic_dec_and_test(&mddev->flush_pending))
416 queue_work(md_wq, &mddev->flush_work);
419 static void md_submit_flush_data(struct work_struct *ws)
421 mddev_t *mddev = container_of(ws, mddev_t, flush_work);
422 struct bio *bio = mddev->flush_bio;
424 if (bio->bi_size == 0)
425 /* an empty barrier - all done */
426 bio_endio(bio, 0);
427 else {
428 bio->bi_rw &= ~REQ_FLUSH;
429 if (mddev->pers->make_request(mddev, bio))
430 generic_make_request(bio);
433 mddev->flush_bio = NULL;
434 wake_up(&mddev->sb_wait);
437 void md_flush_request(mddev_t *mddev, struct bio *bio)
439 spin_lock_irq(&mddev->write_lock);
440 wait_event_lock_irq(mddev->sb_wait,
441 !mddev->flush_bio,
442 mddev->write_lock, /*nothing*/);
443 mddev->flush_bio = bio;
444 spin_unlock_irq(&mddev->write_lock);
446 INIT_WORK(&mddev->flush_work, submit_flushes);
447 queue_work(md_wq, &mddev->flush_work);
449 EXPORT_SYMBOL(md_flush_request);
451 /* Support for plugging.
452 * This mirrors the plugging support in request_queue, but does not
453 * require having a whole queue or request structures.
454 * We allocate an md_plug_cb for each md device and each thread it gets
455 * plugged on. This links tot the private plug_handle structure in the
456 * personality data where we keep a count of the number of outstanding
457 * plugs so other code can see if a plug is active.
459 struct md_plug_cb {
460 struct blk_plug_cb cb;
461 mddev_t *mddev;
464 static void plugger_unplug(struct blk_plug_cb *cb)
466 struct md_plug_cb *mdcb = container_of(cb, struct md_plug_cb, cb);
467 if (atomic_dec_and_test(&mdcb->mddev->plug_cnt))
468 md_wakeup_thread(mdcb->mddev->thread);
469 kfree(mdcb);
472 /* Check that an unplug wakeup will come shortly.
473 * If not, wakeup the md thread immediately
475 int mddev_check_plugged(mddev_t *mddev)
477 struct blk_plug *plug = current->plug;
478 struct md_plug_cb *mdcb;
480 if (!plug)
481 return 0;
483 list_for_each_entry(mdcb, &plug->cb_list, cb.list) {
484 if (mdcb->cb.callback == plugger_unplug &&
485 mdcb->mddev == mddev) {
486 /* Already on the list, move to top */
487 if (mdcb != list_first_entry(&plug->cb_list,
488 struct md_plug_cb,
489 cb.list))
490 list_move(&mdcb->cb.list, &plug->cb_list);
491 return 1;
494 /* Not currently on the callback list */
495 mdcb = kmalloc(sizeof(*mdcb), GFP_ATOMIC);
496 if (!mdcb)
497 return 0;
499 mdcb->mddev = mddev;
500 mdcb->cb.callback = plugger_unplug;
501 atomic_inc(&mddev->plug_cnt);
502 list_add(&mdcb->cb.list, &plug->cb_list);
503 return 1;
505 EXPORT_SYMBOL_GPL(mddev_check_plugged);
507 static inline mddev_t *mddev_get(mddev_t *mddev)
509 atomic_inc(&mddev->active);
510 return mddev;
513 static void mddev_delayed_delete(struct work_struct *ws);
515 static void mddev_put(mddev_t *mddev)
517 struct bio_set *bs = NULL;
519 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
520 return;
521 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
522 mddev->ctime == 0 && !mddev->hold_active) {
523 /* Array is not configured at all, and not held active,
524 * so destroy it */
525 list_del(&mddev->all_mddevs);
526 bs = mddev->bio_set;
527 mddev->bio_set = NULL;
528 if (mddev->gendisk) {
529 /* We did a probe so need to clean up. Call
530 * queue_work inside the spinlock so that
531 * flush_workqueue() after mddev_find will
532 * succeed in waiting for the work to be done.
534 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
535 queue_work(md_misc_wq, &mddev->del_work);
536 } else
537 kfree(mddev);
539 spin_unlock(&all_mddevs_lock);
540 if (bs)
541 bioset_free(bs);
544 void mddev_init(mddev_t *mddev)
546 mutex_init(&mddev->open_mutex);
547 mutex_init(&mddev->reconfig_mutex);
548 mutex_init(&mddev->bitmap_info.mutex);
549 INIT_LIST_HEAD(&mddev->disks);
550 INIT_LIST_HEAD(&mddev->all_mddevs);
551 init_timer(&mddev->safemode_timer);
552 atomic_set(&mddev->active, 1);
553 atomic_set(&mddev->openers, 0);
554 atomic_set(&mddev->active_io, 0);
555 atomic_set(&mddev->plug_cnt, 0);
556 spin_lock_init(&mddev->write_lock);
557 atomic_set(&mddev->flush_pending, 0);
558 init_waitqueue_head(&mddev->sb_wait);
559 init_waitqueue_head(&mddev->recovery_wait);
560 mddev->reshape_position = MaxSector;
561 mddev->resync_min = 0;
562 mddev->resync_max = MaxSector;
563 mddev->level = LEVEL_NONE;
565 EXPORT_SYMBOL_GPL(mddev_init);
567 static mddev_t * mddev_find(dev_t unit)
569 mddev_t *mddev, *new = NULL;
571 if (unit && MAJOR(unit) != MD_MAJOR)
572 unit &= ~((1<<MdpMinorShift)-1);
574 retry:
575 spin_lock(&all_mddevs_lock);
577 if (unit) {
578 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
579 if (mddev->unit == unit) {
580 mddev_get(mddev);
581 spin_unlock(&all_mddevs_lock);
582 kfree(new);
583 return mddev;
586 if (new) {
587 list_add(&new->all_mddevs, &all_mddevs);
588 spin_unlock(&all_mddevs_lock);
589 new->hold_active = UNTIL_IOCTL;
590 return new;
592 } else if (new) {
593 /* find an unused unit number */
594 static int next_minor = 512;
595 int start = next_minor;
596 int is_free = 0;
597 int dev = 0;
598 while (!is_free) {
599 dev = MKDEV(MD_MAJOR, next_minor);
600 next_minor++;
601 if (next_minor > MINORMASK)
602 next_minor = 0;
603 if (next_minor == start) {
604 /* Oh dear, all in use. */
605 spin_unlock(&all_mddevs_lock);
606 kfree(new);
607 return NULL;
610 is_free = 1;
611 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
612 if (mddev->unit == dev) {
613 is_free = 0;
614 break;
617 new->unit = dev;
618 new->md_minor = MINOR(dev);
619 new->hold_active = UNTIL_STOP;
620 list_add(&new->all_mddevs, &all_mddevs);
621 spin_unlock(&all_mddevs_lock);
622 return new;
624 spin_unlock(&all_mddevs_lock);
626 new = kzalloc(sizeof(*new), GFP_KERNEL);
627 if (!new)
628 return NULL;
630 new->unit = unit;
631 if (MAJOR(unit) == MD_MAJOR)
632 new->md_minor = MINOR(unit);
633 else
634 new->md_minor = MINOR(unit) >> MdpMinorShift;
636 mddev_init(new);
638 goto retry;
641 static inline int mddev_lock(mddev_t * mddev)
643 return mutex_lock_interruptible(&mddev->reconfig_mutex);
646 static inline int mddev_is_locked(mddev_t *mddev)
648 return mutex_is_locked(&mddev->reconfig_mutex);
651 static inline int mddev_trylock(mddev_t * mddev)
653 return mutex_trylock(&mddev->reconfig_mutex);
656 static struct attribute_group md_redundancy_group;
658 static void mddev_unlock(mddev_t * mddev)
660 if (mddev->to_remove) {
661 /* These cannot be removed under reconfig_mutex as
662 * an access to the files will try to take reconfig_mutex
663 * while holding the file unremovable, which leads to
664 * a deadlock.
665 * So hold set sysfs_active while the remove in happeing,
666 * and anything else which might set ->to_remove or my
667 * otherwise change the sysfs namespace will fail with
668 * -EBUSY if sysfs_active is still set.
669 * We set sysfs_active under reconfig_mutex and elsewhere
670 * test it under the same mutex to ensure its correct value
671 * is seen.
673 struct attribute_group *to_remove = mddev->to_remove;
674 mddev->to_remove = NULL;
675 mddev->sysfs_active = 1;
676 mutex_unlock(&mddev->reconfig_mutex);
678 if (mddev->kobj.sd) {
679 if (to_remove != &md_redundancy_group)
680 sysfs_remove_group(&mddev->kobj, to_remove);
681 if (mddev->pers == NULL ||
682 mddev->pers->sync_request == NULL) {
683 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
684 if (mddev->sysfs_action)
685 sysfs_put(mddev->sysfs_action);
686 mddev->sysfs_action = NULL;
689 mddev->sysfs_active = 0;
690 } else
691 mutex_unlock(&mddev->reconfig_mutex);
693 md_wakeup_thread(mddev->thread);
696 static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
698 mdk_rdev_t *rdev;
700 list_for_each_entry(rdev, &mddev->disks, same_set)
701 if (rdev->desc_nr == nr)
702 return rdev;
704 return NULL;
707 static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
709 mdk_rdev_t *rdev;
711 list_for_each_entry(rdev, &mddev->disks, same_set)
712 if (rdev->bdev->bd_dev == dev)
713 return rdev;
715 return NULL;
718 static struct mdk_personality *find_pers(int level, char *clevel)
720 struct mdk_personality *pers;
721 list_for_each_entry(pers, &pers_list, list) {
722 if (level != LEVEL_NONE && pers->level == level)
723 return pers;
724 if (strcmp(pers->name, clevel)==0)
725 return pers;
727 return NULL;
730 /* return the offset of the super block in 512byte sectors */
731 static inline sector_t calc_dev_sboffset(mdk_rdev_t *rdev)
733 sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512;
734 return MD_NEW_SIZE_SECTORS(num_sectors);
737 static int alloc_disk_sb(mdk_rdev_t * rdev)
739 if (rdev->sb_page)
740 MD_BUG();
742 rdev->sb_page = alloc_page(GFP_KERNEL);
743 if (!rdev->sb_page) {
744 printk(KERN_ALERT "md: out of memory.\n");
745 return -ENOMEM;
748 return 0;
751 static void free_disk_sb(mdk_rdev_t * rdev)
753 if (rdev->sb_page) {
754 put_page(rdev->sb_page);
755 rdev->sb_loaded = 0;
756 rdev->sb_page = NULL;
757 rdev->sb_start = 0;
758 rdev->sectors = 0;
763 static void super_written(struct bio *bio, int error)
765 mdk_rdev_t *rdev = bio->bi_private;
766 mddev_t *mddev = rdev->mddev;
768 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
769 printk("md: super_written gets error=%d, uptodate=%d\n",
770 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
771 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
772 md_error(mddev, rdev);
775 if (atomic_dec_and_test(&mddev->pending_writes))
776 wake_up(&mddev->sb_wait);
777 bio_put(bio);
780 void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
781 sector_t sector, int size, struct page *page)
783 /* write first size bytes of page to sector of rdev
784 * Increment mddev->pending_writes before returning
785 * and decrement it on completion, waking up sb_wait
786 * if zero is reached.
787 * If an error occurred, call md_error
789 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev);
791 bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev;
792 bio->bi_sector = sector;
793 bio_add_page(bio, page, size, 0);
794 bio->bi_private = rdev;
795 bio->bi_end_io = super_written;
797 atomic_inc(&mddev->pending_writes);
798 submit_bio(REQ_WRITE | REQ_SYNC | REQ_FLUSH | REQ_FUA, bio);
801 void md_super_wait(mddev_t *mddev)
803 /* wait for all superblock writes that were scheduled to complete */
804 DEFINE_WAIT(wq);
805 for(;;) {
806 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
807 if (atomic_read(&mddev->pending_writes)==0)
808 break;
809 schedule();
811 finish_wait(&mddev->sb_wait, &wq);
814 static void bi_complete(struct bio *bio, int error)
816 complete((struct completion*)bio->bi_private);
819 int sync_page_io(mdk_rdev_t *rdev, sector_t sector, int size,
820 struct page *page, int rw, bool metadata_op)
822 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev);
823 struct completion event;
824 int ret;
826 rw |= REQ_SYNC;
828 bio->bi_bdev = (metadata_op && rdev->meta_bdev) ?
829 rdev->meta_bdev : rdev->bdev;
830 if (metadata_op)
831 bio->bi_sector = sector + rdev->sb_start;
832 else
833 bio->bi_sector = sector + rdev->data_offset;
834 bio_add_page(bio, page, size, 0);
835 init_completion(&event);
836 bio->bi_private = &event;
837 bio->bi_end_io = bi_complete;
838 submit_bio(rw, bio);
839 wait_for_completion(&event);
841 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
842 bio_put(bio);
843 return ret;
845 EXPORT_SYMBOL_GPL(sync_page_io);
847 static int read_disk_sb(mdk_rdev_t * rdev, int size)
849 char b[BDEVNAME_SIZE];
850 if (!rdev->sb_page) {
851 MD_BUG();
852 return -EINVAL;
854 if (rdev->sb_loaded)
855 return 0;
858 if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true))
859 goto fail;
860 rdev->sb_loaded = 1;
861 return 0;
863 fail:
864 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
865 bdevname(rdev->bdev,b));
866 return -EINVAL;
869 static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
871 return sb1->set_uuid0 == sb2->set_uuid0 &&
872 sb1->set_uuid1 == sb2->set_uuid1 &&
873 sb1->set_uuid2 == sb2->set_uuid2 &&
874 sb1->set_uuid3 == sb2->set_uuid3;
877 static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
879 int ret;
880 mdp_super_t *tmp1, *tmp2;
882 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
883 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
885 if (!tmp1 || !tmp2) {
886 ret = 0;
887 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
888 goto abort;
891 *tmp1 = *sb1;
892 *tmp2 = *sb2;
895 * nr_disks is not constant
897 tmp1->nr_disks = 0;
898 tmp2->nr_disks = 0;
900 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
901 abort:
902 kfree(tmp1);
903 kfree(tmp2);
904 return ret;
908 static u32 md_csum_fold(u32 csum)
910 csum = (csum & 0xffff) + (csum >> 16);
911 return (csum & 0xffff) + (csum >> 16);
914 static unsigned int calc_sb_csum(mdp_super_t * sb)
916 u64 newcsum = 0;
917 u32 *sb32 = (u32*)sb;
918 int i;
919 unsigned int disk_csum, csum;
921 disk_csum = sb->sb_csum;
922 sb->sb_csum = 0;
924 for (i = 0; i < MD_SB_BYTES/4 ; i++)
925 newcsum += sb32[i];
926 csum = (newcsum & 0xffffffff) + (newcsum>>32);
929 #ifdef CONFIG_ALPHA
930 /* This used to use csum_partial, which was wrong for several
931 * reasons including that different results are returned on
932 * different architectures. It isn't critical that we get exactly
933 * the same return value as before (we always csum_fold before
934 * testing, and that removes any differences). However as we
935 * know that csum_partial always returned a 16bit value on
936 * alphas, do a fold to maximise conformity to previous behaviour.
938 sb->sb_csum = md_csum_fold(disk_csum);
939 #else
940 sb->sb_csum = disk_csum;
941 #endif
942 return csum;
947 * Handle superblock details.
948 * We want to be able to handle multiple superblock formats
949 * so we have a common interface to them all, and an array of
950 * different handlers.
951 * We rely on user-space to write the initial superblock, and support
952 * reading and updating of superblocks.
953 * Interface methods are:
954 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
955 * loads and validates a superblock on dev.
956 * if refdev != NULL, compare superblocks on both devices
957 * Return:
958 * 0 - dev has a superblock that is compatible with refdev
959 * 1 - dev has a superblock that is compatible and newer than refdev
960 * so dev should be used as the refdev in future
961 * -EINVAL superblock incompatible or invalid
962 * -othererror e.g. -EIO
964 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
965 * Verify that dev is acceptable into mddev.
966 * The first time, mddev->raid_disks will be 0, and data from
967 * dev should be merged in. Subsequent calls check that dev
968 * is new enough. Return 0 or -EINVAL
970 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
971 * Update the superblock for rdev with data in mddev
972 * This does not write to disc.
976 struct super_type {
977 char *name;
978 struct module *owner;
979 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev,
980 int minor_version);
981 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
982 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
983 unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev,
984 sector_t num_sectors);
988 * Check that the given mddev has no bitmap.
990 * This function is called from the run method of all personalities that do not
991 * support bitmaps. It prints an error message and returns non-zero if mddev
992 * has a bitmap. Otherwise, it returns 0.
995 int md_check_no_bitmap(mddev_t *mddev)
997 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
998 return 0;
999 printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
1000 mdname(mddev), mddev->pers->name);
1001 return 1;
1003 EXPORT_SYMBOL(md_check_no_bitmap);
1006 * load_super for 0.90.0
1008 static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
1010 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1011 mdp_super_t *sb;
1012 int ret;
1015 * Calculate the position of the superblock (512byte sectors),
1016 * it's at the end of the disk.
1018 * It also happens to be a multiple of 4Kb.
1020 rdev->sb_start = calc_dev_sboffset(rdev);
1022 ret = read_disk_sb(rdev, MD_SB_BYTES);
1023 if (ret) return ret;
1025 ret = -EINVAL;
1027 bdevname(rdev->bdev, b);
1028 sb = (mdp_super_t*)page_address(rdev->sb_page);
1030 if (sb->md_magic != MD_SB_MAGIC) {
1031 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
1033 goto abort;
1036 if (sb->major_version != 0 ||
1037 sb->minor_version < 90 ||
1038 sb->minor_version > 91) {
1039 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
1040 sb->major_version, sb->minor_version,
1042 goto abort;
1045 if (sb->raid_disks <= 0)
1046 goto abort;
1048 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
1049 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
1051 goto abort;
1054 rdev->preferred_minor = sb->md_minor;
1055 rdev->data_offset = 0;
1056 rdev->sb_size = MD_SB_BYTES;
1058 if (sb->level == LEVEL_MULTIPATH)
1059 rdev->desc_nr = -1;
1060 else
1061 rdev->desc_nr = sb->this_disk.number;
1063 if (!refdev) {
1064 ret = 1;
1065 } else {
1066 __u64 ev1, ev2;
1067 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
1068 if (!uuid_equal(refsb, sb)) {
1069 printk(KERN_WARNING "md: %s has different UUID to %s\n",
1070 b, bdevname(refdev->bdev,b2));
1071 goto abort;
1073 if (!sb_equal(refsb, sb)) {
1074 printk(KERN_WARNING "md: %s has same UUID"
1075 " but different superblock to %s\n",
1076 b, bdevname(refdev->bdev, b2));
1077 goto abort;
1079 ev1 = md_event(sb);
1080 ev2 = md_event(refsb);
1081 if (ev1 > ev2)
1082 ret = 1;
1083 else
1084 ret = 0;
1086 rdev->sectors = rdev->sb_start;
1088 if (rdev->sectors < sb->size * 2 && sb->level > 1)
1089 /* "this cannot possibly happen" ... */
1090 ret = -EINVAL;
1092 abort:
1093 return ret;
1097 * validate_super for 0.90.0
1099 static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1101 mdp_disk_t *desc;
1102 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
1103 __u64 ev1 = md_event(sb);
1105 rdev->raid_disk = -1;
1106 clear_bit(Faulty, &rdev->flags);
1107 clear_bit(In_sync, &rdev->flags);
1108 clear_bit(WriteMostly, &rdev->flags);
1110 if (mddev->raid_disks == 0) {
1111 mddev->major_version = 0;
1112 mddev->minor_version = sb->minor_version;
1113 mddev->patch_version = sb->patch_version;
1114 mddev->external = 0;
1115 mddev->chunk_sectors = sb->chunk_size >> 9;
1116 mddev->ctime = sb->ctime;
1117 mddev->utime = sb->utime;
1118 mddev->level = sb->level;
1119 mddev->clevel[0] = 0;
1120 mddev->layout = sb->layout;
1121 mddev->raid_disks = sb->raid_disks;
1122 mddev->dev_sectors = sb->size * 2;
1123 mddev->events = ev1;
1124 mddev->bitmap_info.offset = 0;
1125 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
1127 if (mddev->minor_version >= 91) {
1128 mddev->reshape_position = sb->reshape_position;
1129 mddev->delta_disks = sb->delta_disks;
1130 mddev->new_level = sb->new_level;
1131 mddev->new_layout = sb->new_layout;
1132 mddev->new_chunk_sectors = sb->new_chunk >> 9;
1133 } else {
1134 mddev->reshape_position = MaxSector;
1135 mddev->delta_disks = 0;
1136 mddev->new_level = mddev->level;
1137 mddev->new_layout = mddev->layout;
1138 mddev->new_chunk_sectors = mddev->chunk_sectors;
1141 if (sb->state & (1<<MD_SB_CLEAN))
1142 mddev->recovery_cp = MaxSector;
1143 else {
1144 if (sb->events_hi == sb->cp_events_hi &&
1145 sb->events_lo == sb->cp_events_lo) {
1146 mddev->recovery_cp = sb->recovery_cp;
1147 } else
1148 mddev->recovery_cp = 0;
1151 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1152 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1153 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1154 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1156 mddev->max_disks = MD_SB_DISKS;
1158 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
1159 mddev->bitmap_info.file == NULL)
1160 mddev->bitmap_info.offset =
1161 mddev->bitmap_info.default_offset;
1163 } else if (mddev->pers == NULL) {
1164 /* Insist on good event counter while assembling, except
1165 * for spares (which don't need an event count) */
1166 ++ev1;
1167 if (sb->disks[rdev->desc_nr].state & (
1168 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
1169 if (ev1 < mddev->events)
1170 return -EINVAL;
1171 } else if (mddev->bitmap) {
1172 /* if adding to array with a bitmap, then we can accept an
1173 * older device ... but not too old.
1175 if (ev1 < mddev->bitmap->events_cleared)
1176 return 0;
1177 } else {
1178 if (ev1 < mddev->events)
1179 /* just a hot-add of a new device, leave raid_disk at -1 */
1180 return 0;
1183 if (mddev->level != LEVEL_MULTIPATH) {
1184 desc = sb->disks + rdev->desc_nr;
1186 if (desc->state & (1<<MD_DISK_FAULTY))
1187 set_bit(Faulty, &rdev->flags);
1188 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1189 desc->raid_disk < mddev->raid_disks */) {
1190 set_bit(In_sync, &rdev->flags);
1191 rdev->raid_disk = desc->raid_disk;
1192 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1193 /* active but not in sync implies recovery up to
1194 * reshape position. We don't know exactly where
1195 * that is, so set to zero for now */
1196 if (mddev->minor_version >= 91) {
1197 rdev->recovery_offset = 0;
1198 rdev->raid_disk = desc->raid_disk;
1201 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1202 set_bit(WriteMostly, &rdev->flags);
1203 } else /* MULTIPATH are always insync */
1204 set_bit(In_sync, &rdev->flags);
1205 return 0;
1209 * sync_super for 0.90.0
1211 static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1213 mdp_super_t *sb;
1214 mdk_rdev_t *rdev2;
1215 int next_spare = mddev->raid_disks;
1218 /* make rdev->sb match mddev data..
1220 * 1/ zero out disks
1221 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1222 * 3/ any empty disks < next_spare become removed
1224 * disks[0] gets initialised to REMOVED because
1225 * we cannot be sure from other fields if it has
1226 * been initialised or not.
1228 int i;
1229 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1231 rdev->sb_size = MD_SB_BYTES;
1233 sb = (mdp_super_t*)page_address(rdev->sb_page);
1235 memset(sb, 0, sizeof(*sb));
1237 sb->md_magic = MD_SB_MAGIC;
1238 sb->major_version = mddev->major_version;
1239 sb->patch_version = mddev->patch_version;
1240 sb->gvalid_words = 0; /* ignored */
1241 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1242 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1243 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1244 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1246 sb->ctime = mddev->ctime;
1247 sb->level = mddev->level;
1248 sb->size = mddev->dev_sectors / 2;
1249 sb->raid_disks = mddev->raid_disks;
1250 sb->md_minor = mddev->md_minor;
1251 sb->not_persistent = 0;
1252 sb->utime = mddev->utime;
1253 sb->state = 0;
1254 sb->events_hi = (mddev->events>>32);
1255 sb->events_lo = (u32)mddev->events;
1257 if (mddev->reshape_position == MaxSector)
1258 sb->minor_version = 90;
1259 else {
1260 sb->minor_version = 91;
1261 sb->reshape_position = mddev->reshape_position;
1262 sb->new_level = mddev->new_level;
1263 sb->delta_disks = mddev->delta_disks;
1264 sb->new_layout = mddev->new_layout;
1265 sb->new_chunk = mddev->new_chunk_sectors << 9;
1267 mddev->minor_version = sb->minor_version;
1268 if (mddev->in_sync)
1270 sb->recovery_cp = mddev->recovery_cp;
1271 sb->cp_events_hi = (mddev->events>>32);
1272 sb->cp_events_lo = (u32)mddev->events;
1273 if (mddev->recovery_cp == MaxSector)
1274 sb->state = (1<< MD_SB_CLEAN);
1275 } else
1276 sb->recovery_cp = 0;
1278 sb->layout = mddev->layout;
1279 sb->chunk_size = mddev->chunk_sectors << 9;
1281 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
1282 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1284 sb->disks[0].state = (1<<MD_DISK_REMOVED);
1285 list_for_each_entry(rdev2, &mddev->disks, same_set) {
1286 mdp_disk_t *d;
1287 int desc_nr;
1288 int is_active = test_bit(In_sync, &rdev2->flags);
1290 if (rdev2->raid_disk >= 0 &&
1291 sb->minor_version >= 91)
1292 /* we have nowhere to store the recovery_offset,
1293 * but if it is not below the reshape_position,
1294 * we can piggy-back on that.
1296 is_active = 1;
1297 if (rdev2->raid_disk < 0 ||
1298 test_bit(Faulty, &rdev2->flags))
1299 is_active = 0;
1300 if (is_active)
1301 desc_nr = rdev2->raid_disk;
1302 else
1303 desc_nr = next_spare++;
1304 rdev2->desc_nr = desc_nr;
1305 d = &sb->disks[rdev2->desc_nr];
1306 nr_disks++;
1307 d->number = rdev2->desc_nr;
1308 d->major = MAJOR(rdev2->bdev->bd_dev);
1309 d->minor = MINOR(rdev2->bdev->bd_dev);
1310 if (is_active)
1311 d->raid_disk = rdev2->raid_disk;
1312 else
1313 d->raid_disk = rdev2->desc_nr; /* compatibility */
1314 if (test_bit(Faulty, &rdev2->flags))
1315 d->state = (1<<MD_DISK_FAULTY);
1316 else if (is_active) {
1317 d->state = (1<<MD_DISK_ACTIVE);
1318 if (test_bit(In_sync, &rdev2->flags))
1319 d->state |= (1<<MD_DISK_SYNC);
1320 active++;
1321 working++;
1322 } else {
1323 d->state = 0;
1324 spare++;
1325 working++;
1327 if (test_bit(WriteMostly, &rdev2->flags))
1328 d->state |= (1<<MD_DISK_WRITEMOSTLY);
1330 /* now set the "removed" and "faulty" bits on any missing devices */
1331 for (i=0 ; i < mddev->raid_disks ; i++) {
1332 mdp_disk_t *d = &sb->disks[i];
1333 if (d->state == 0 && d->number == 0) {
1334 d->number = i;
1335 d->raid_disk = i;
1336 d->state = (1<<MD_DISK_REMOVED);
1337 d->state |= (1<<MD_DISK_FAULTY);
1338 failed++;
1341 sb->nr_disks = nr_disks;
1342 sb->active_disks = active;
1343 sb->working_disks = working;
1344 sb->failed_disks = failed;
1345 sb->spare_disks = spare;
1347 sb->this_disk = sb->disks[rdev->desc_nr];
1348 sb->sb_csum = calc_sb_csum(sb);
1352 * rdev_size_change for 0.90.0
1354 static unsigned long long
1355 super_90_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
1357 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
1358 return 0; /* component must fit device */
1359 if (rdev->mddev->bitmap_info.offset)
1360 return 0; /* can't move bitmap */
1361 rdev->sb_start = calc_dev_sboffset(rdev);
1362 if (!num_sectors || num_sectors > rdev->sb_start)
1363 num_sectors = rdev->sb_start;
1364 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
1365 rdev->sb_page);
1366 md_super_wait(rdev->mddev);
1367 return num_sectors;
1372 * version 1 superblock
1375 static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
1377 __le32 disk_csum;
1378 u32 csum;
1379 unsigned long long newcsum;
1380 int size = 256 + le32_to_cpu(sb->max_dev)*2;
1381 __le32 *isuper = (__le32*)sb;
1382 int i;
1384 disk_csum = sb->sb_csum;
1385 sb->sb_csum = 0;
1386 newcsum = 0;
1387 for (i=0; size>=4; size -= 4 )
1388 newcsum += le32_to_cpu(*isuper++);
1390 if (size == 2)
1391 newcsum += le16_to_cpu(*(__le16*) isuper);
1393 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1394 sb->sb_csum = disk_csum;
1395 return cpu_to_le32(csum);
1398 static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
1400 struct mdp_superblock_1 *sb;
1401 int ret;
1402 sector_t sb_start;
1403 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1404 int bmask;
1407 * Calculate the position of the superblock in 512byte sectors.
1408 * It is always aligned to a 4K boundary and
1409 * depeding on minor_version, it can be:
1410 * 0: At least 8K, but less than 12K, from end of device
1411 * 1: At start of device
1412 * 2: 4K from start of device.
1414 switch(minor_version) {
1415 case 0:
1416 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
1417 sb_start -= 8*2;
1418 sb_start &= ~(sector_t)(4*2-1);
1419 break;
1420 case 1:
1421 sb_start = 0;
1422 break;
1423 case 2:
1424 sb_start = 8;
1425 break;
1426 default:
1427 return -EINVAL;
1429 rdev->sb_start = sb_start;
1431 /* superblock is rarely larger than 1K, but it can be larger,
1432 * and it is safe to read 4k, so we do that
1434 ret = read_disk_sb(rdev, 4096);
1435 if (ret) return ret;
1438 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1440 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1441 sb->major_version != cpu_to_le32(1) ||
1442 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
1443 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
1444 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
1445 return -EINVAL;
1447 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1448 printk("md: invalid superblock checksum on %s\n",
1449 bdevname(rdev->bdev,b));
1450 return -EINVAL;
1452 if (le64_to_cpu(sb->data_size) < 10) {
1453 printk("md: data_size too small on %s\n",
1454 bdevname(rdev->bdev,b));
1455 return -EINVAL;
1458 rdev->preferred_minor = 0xffff;
1459 rdev->data_offset = le64_to_cpu(sb->data_offset);
1460 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
1462 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
1463 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1464 if (rdev->sb_size & bmask)
1465 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1467 if (minor_version
1468 && rdev->data_offset < sb_start + (rdev->sb_size/512))
1469 return -EINVAL;
1471 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1472 rdev->desc_nr = -1;
1473 else
1474 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1476 if (!refdev) {
1477 ret = 1;
1478 } else {
1479 __u64 ev1, ev2;
1480 struct mdp_superblock_1 *refsb =
1481 (struct mdp_superblock_1*)page_address(refdev->sb_page);
1483 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1484 sb->level != refsb->level ||
1485 sb->layout != refsb->layout ||
1486 sb->chunksize != refsb->chunksize) {
1487 printk(KERN_WARNING "md: %s has strangely different"
1488 " superblock to %s\n",
1489 bdevname(rdev->bdev,b),
1490 bdevname(refdev->bdev,b2));
1491 return -EINVAL;
1493 ev1 = le64_to_cpu(sb->events);
1494 ev2 = le64_to_cpu(refsb->events);
1496 if (ev1 > ev2)
1497 ret = 1;
1498 else
1499 ret = 0;
1501 if (minor_version)
1502 rdev->sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
1503 le64_to_cpu(sb->data_offset);
1504 else
1505 rdev->sectors = rdev->sb_start;
1506 if (rdev->sectors < le64_to_cpu(sb->data_size))
1507 return -EINVAL;
1508 rdev->sectors = le64_to_cpu(sb->data_size);
1509 if (le64_to_cpu(sb->size) > rdev->sectors)
1510 return -EINVAL;
1511 return ret;
1514 static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1516 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1517 __u64 ev1 = le64_to_cpu(sb->events);
1519 rdev->raid_disk = -1;
1520 clear_bit(Faulty, &rdev->flags);
1521 clear_bit(In_sync, &rdev->flags);
1522 clear_bit(WriteMostly, &rdev->flags);
1524 if (mddev->raid_disks == 0) {
1525 mddev->major_version = 1;
1526 mddev->patch_version = 0;
1527 mddev->external = 0;
1528 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
1529 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1530 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1531 mddev->level = le32_to_cpu(sb->level);
1532 mddev->clevel[0] = 0;
1533 mddev->layout = le32_to_cpu(sb->layout);
1534 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
1535 mddev->dev_sectors = le64_to_cpu(sb->size);
1536 mddev->events = ev1;
1537 mddev->bitmap_info.offset = 0;
1538 mddev->bitmap_info.default_offset = 1024 >> 9;
1540 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1541 memcpy(mddev->uuid, sb->set_uuid, 16);
1543 mddev->max_disks = (4096-256)/2;
1545 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
1546 mddev->bitmap_info.file == NULL )
1547 mddev->bitmap_info.offset =
1548 (__s32)le32_to_cpu(sb->bitmap_offset);
1550 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1551 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1552 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1553 mddev->new_level = le32_to_cpu(sb->new_level);
1554 mddev->new_layout = le32_to_cpu(sb->new_layout);
1555 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
1556 } else {
1557 mddev->reshape_position = MaxSector;
1558 mddev->delta_disks = 0;
1559 mddev->new_level = mddev->level;
1560 mddev->new_layout = mddev->layout;
1561 mddev->new_chunk_sectors = mddev->chunk_sectors;
1564 } else if (mddev->pers == NULL) {
1565 /* Insist of good event counter while assembling, except for
1566 * spares (which don't need an event count) */
1567 ++ev1;
1568 if (rdev->desc_nr >= 0 &&
1569 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
1570 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe)
1571 if (ev1 < mddev->events)
1572 return -EINVAL;
1573 } else if (mddev->bitmap) {
1574 /* If adding to array with a bitmap, then we can accept an
1575 * older device, but not too old.
1577 if (ev1 < mddev->bitmap->events_cleared)
1578 return 0;
1579 } else {
1580 if (ev1 < mddev->events)
1581 /* just a hot-add of a new device, leave raid_disk at -1 */
1582 return 0;
1584 if (mddev->level != LEVEL_MULTIPATH) {
1585 int role;
1586 if (rdev->desc_nr < 0 ||
1587 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1588 role = 0xffff;
1589 rdev->desc_nr = -1;
1590 } else
1591 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
1592 switch(role) {
1593 case 0xffff: /* spare */
1594 break;
1595 case 0xfffe: /* faulty */
1596 set_bit(Faulty, &rdev->flags);
1597 break;
1598 default:
1599 if ((le32_to_cpu(sb->feature_map) &
1600 MD_FEATURE_RECOVERY_OFFSET))
1601 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1602 else
1603 set_bit(In_sync, &rdev->flags);
1604 rdev->raid_disk = role;
1605 break;
1607 if (sb->devflags & WriteMostly1)
1608 set_bit(WriteMostly, &rdev->flags);
1609 } else /* MULTIPATH are always insync */
1610 set_bit(In_sync, &rdev->flags);
1612 return 0;
1615 static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1617 struct mdp_superblock_1 *sb;
1618 mdk_rdev_t *rdev2;
1619 int max_dev, i;
1620 /* make rdev->sb match mddev and rdev data. */
1622 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1624 sb->feature_map = 0;
1625 sb->pad0 = 0;
1626 sb->recovery_offset = cpu_to_le64(0);
1627 memset(sb->pad1, 0, sizeof(sb->pad1));
1628 memset(sb->pad2, 0, sizeof(sb->pad2));
1629 memset(sb->pad3, 0, sizeof(sb->pad3));
1631 sb->utime = cpu_to_le64((__u64)mddev->utime);
1632 sb->events = cpu_to_le64(mddev->events);
1633 if (mddev->in_sync)
1634 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1635 else
1636 sb->resync_offset = cpu_to_le64(0);
1638 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
1640 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
1641 sb->size = cpu_to_le64(mddev->dev_sectors);
1642 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
1643 sb->level = cpu_to_le32(mddev->level);
1644 sb->layout = cpu_to_le32(mddev->layout);
1646 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1647 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
1648 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
1651 if (rdev->raid_disk >= 0 &&
1652 !test_bit(In_sync, &rdev->flags)) {
1653 sb->feature_map |=
1654 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1655 sb->recovery_offset =
1656 cpu_to_le64(rdev->recovery_offset);
1659 if (mddev->reshape_position != MaxSector) {
1660 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1661 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1662 sb->new_layout = cpu_to_le32(mddev->new_layout);
1663 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1664 sb->new_level = cpu_to_le32(mddev->new_level);
1665 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
1668 max_dev = 0;
1669 list_for_each_entry(rdev2, &mddev->disks, same_set)
1670 if (rdev2->desc_nr+1 > max_dev)
1671 max_dev = rdev2->desc_nr+1;
1673 if (max_dev > le32_to_cpu(sb->max_dev)) {
1674 int bmask;
1675 sb->max_dev = cpu_to_le32(max_dev);
1676 rdev->sb_size = max_dev * 2 + 256;
1677 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1678 if (rdev->sb_size & bmask)
1679 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1680 } else
1681 max_dev = le32_to_cpu(sb->max_dev);
1683 for (i=0; i<max_dev;i++)
1684 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1686 list_for_each_entry(rdev2, &mddev->disks, same_set) {
1687 i = rdev2->desc_nr;
1688 if (test_bit(Faulty, &rdev2->flags))
1689 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1690 else if (test_bit(In_sync, &rdev2->flags))
1691 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
1692 else if (rdev2->raid_disk >= 0)
1693 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
1694 else
1695 sb->dev_roles[i] = cpu_to_le16(0xffff);
1698 sb->sb_csum = calc_sb_1_csum(sb);
1701 static unsigned long long
1702 super_1_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
1704 struct mdp_superblock_1 *sb;
1705 sector_t max_sectors;
1706 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
1707 return 0; /* component must fit device */
1708 if (rdev->sb_start < rdev->data_offset) {
1709 /* minor versions 1 and 2; superblock before data */
1710 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
1711 max_sectors -= rdev->data_offset;
1712 if (!num_sectors || num_sectors > max_sectors)
1713 num_sectors = max_sectors;
1714 } else if (rdev->mddev->bitmap_info.offset) {
1715 /* minor version 0 with bitmap we can't move */
1716 return 0;
1717 } else {
1718 /* minor version 0; superblock after data */
1719 sector_t sb_start;
1720 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
1721 sb_start &= ~(sector_t)(4*2 - 1);
1722 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
1723 if (!num_sectors || num_sectors > max_sectors)
1724 num_sectors = max_sectors;
1725 rdev->sb_start = sb_start;
1727 sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page);
1728 sb->data_size = cpu_to_le64(num_sectors);
1729 sb->super_offset = rdev->sb_start;
1730 sb->sb_csum = calc_sb_1_csum(sb);
1731 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
1732 rdev->sb_page);
1733 md_super_wait(rdev->mddev);
1734 return num_sectors;
1737 static struct super_type super_types[] = {
1738 [0] = {
1739 .name = "0.90.0",
1740 .owner = THIS_MODULE,
1741 .load_super = super_90_load,
1742 .validate_super = super_90_validate,
1743 .sync_super = super_90_sync,
1744 .rdev_size_change = super_90_rdev_size_change,
1746 [1] = {
1747 .name = "md-1",
1748 .owner = THIS_MODULE,
1749 .load_super = super_1_load,
1750 .validate_super = super_1_validate,
1751 .sync_super = super_1_sync,
1752 .rdev_size_change = super_1_rdev_size_change,
1756 static void sync_super(mddev_t *mddev, mdk_rdev_t *rdev)
1758 if (mddev->sync_super) {
1759 mddev->sync_super(mddev, rdev);
1760 return;
1763 BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
1765 super_types[mddev->major_version].sync_super(mddev, rdev);
1768 static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1770 mdk_rdev_t *rdev, *rdev2;
1772 rcu_read_lock();
1773 rdev_for_each_rcu(rdev, mddev1)
1774 rdev_for_each_rcu(rdev2, mddev2)
1775 if (rdev->bdev->bd_contains ==
1776 rdev2->bdev->bd_contains) {
1777 rcu_read_unlock();
1778 return 1;
1780 rcu_read_unlock();
1781 return 0;
1784 static LIST_HEAD(pending_raid_disks);
1787 * Try to register data integrity profile for an mddev
1789 * This is called when an array is started and after a disk has been kicked
1790 * from the array. It only succeeds if all working and active component devices
1791 * are integrity capable with matching profiles.
1793 int md_integrity_register(mddev_t *mddev)
1795 mdk_rdev_t *rdev, *reference = NULL;
1797 if (list_empty(&mddev->disks))
1798 return 0; /* nothing to do */
1799 if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
1800 return 0; /* shouldn't register, or already is */
1801 list_for_each_entry(rdev, &mddev->disks, same_set) {
1802 /* skip spares and non-functional disks */
1803 if (test_bit(Faulty, &rdev->flags))
1804 continue;
1805 if (rdev->raid_disk < 0)
1806 continue;
1807 if (!reference) {
1808 /* Use the first rdev as the reference */
1809 reference = rdev;
1810 continue;
1812 /* does this rdev's profile match the reference profile? */
1813 if (blk_integrity_compare(reference->bdev->bd_disk,
1814 rdev->bdev->bd_disk) < 0)
1815 return -EINVAL;
1817 if (!reference || !bdev_get_integrity(reference->bdev))
1818 return 0;
1820 * All component devices are integrity capable and have matching
1821 * profiles, register the common profile for the md device.
1823 if (blk_integrity_register(mddev->gendisk,
1824 bdev_get_integrity(reference->bdev)) != 0) {
1825 printk(KERN_ERR "md: failed to register integrity for %s\n",
1826 mdname(mddev));
1827 return -EINVAL;
1829 printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev));
1830 if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) {
1831 printk(KERN_ERR "md: failed to create integrity pool for %s\n",
1832 mdname(mddev));
1833 return -EINVAL;
1835 return 0;
1837 EXPORT_SYMBOL(md_integrity_register);
1839 /* Disable data integrity if non-capable/non-matching disk is being added */
1840 void md_integrity_add_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
1842 struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev);
1843 struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk);
1845 if (!bi_mddev) /* nothing to do */
1846 return;
1847 if (rdev->raid_disk < 0) /* skip spares */
1848 return;
1849 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
1850 rdev->bdev->bd_disk) >= 0)
1851 return;
1852 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
1853 blk_integrity_unregister(mddev->gendisk);
1855 EXPORT_SYMBOL(md_integrity_add_rdev);
1857 static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1859 char b[BDEVNAME_SIZE];
1860 struct kobject *ko;
1861 char *s;
1862 int err;
1864 if (rdev->mddev) {
1865 MD_BUG();
1866 return -EINVAL;
1869 /* prevent duplicates */
1870 if (find_rdev(mddev, rdev->bdev->bd_dev))
1871 return -EEXIST;
1873 /* make sure rdev->sectors exceeds mddev->dev_sectors */
1874 if (rdev->sectors && (mddev->dev_sectors == 0 ||
1875 rdev->sectors < mddev->dev_sectors)) {
1876 if (mddev->pers) {
1877 /* Cannot change size, so fail
1878 * If mddev->level <= 0, then we don't care
1879 * about aligning sizes (e.g. linear)
1881 if (mddev->level > 0)
1882 return -ENOSPC;
1883 } else
1884 mddev->dev_sectors = rdev->sectors;
1887 /* Verify rdev->desc_nr is unique.
1888 * If it is -1, assign a free number, else
1889 * check number is not in use
1891 if (rdev->desc_nr < 0) {
1892 int choice = 0;
1893 if (mddev->pers) choice = mddev->raid_disks;
1894 while (find_rdev_nr(mddev, choice))
1895 choice++;
1896 rdev->desc_nr = choice;
1897 } else {
1898 if (find_rdev_nr(mddev, rdev->desc_nr))
1899 return -EBUSY;
1901 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
1902 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
1903 mdname(mddev), mddev->max_disks);
1904 return -EBUSY;
1906 bdevname(rdev->bdev,b);
1907 while ( (s=strchr(b, '/')) != NULL)
1908 *s = '!';
1910 rdev->mddev = mddev;
1911 printk(KERN_INFO "md: bind<%s>\n", b);
1913 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
1914 goto fail;
1916 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
1917 if (sysfs_create_link(&rdev->kobj, ko, "block"))
1918 /* failure here is OK */;
1919 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
1921 list_add_rcu(&rdev->same_set, &mddev->disks);
1922 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
1924 /* May as well allow recovery to be retried once */
1925 mddev->recovery_disabled = 0;
1927 return 0;
1929 fail:
1930 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
1931 b, mdname(mddev));
1932 return err;
1935 static void md_delayed_delete(struct work_struct *ws)
1937 mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work);
1938 kobject_del(&rdev->kobj);
1939 kobject_put(&rdev->kobj);
1942 static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1944 char b[BDEVNAME_SIZE];
1945 if (!rdev->mddev) {
1946 MD_BUG();
1947 return;
1949 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
1950 list_del_rcu(&rdev->same_set);
1951 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1952 rdev->mddev = NULL;
1953 sysfs_remove_link(&rdev->kobj, "block");
1954 sysfs_put(rdev->sysfs_state);
1955 rdev->sysfs_state = NULL;
1956 /* We need to delay this, otherwise we can deadlock when
1957 * writing to 'remove' to "dev/state". We also need
1958 * to delay it due to rcu usage.
1960 synchronize_rcu();
1961 INIT_WORK(&rdev->del_work, md_delayed_delete);
1962 kobject_get(&rdev->kobj);
1963 queue_work(md_misc_wq, &rdev->del_work);
1967 * prevent the device from being mounted, repartitioned or
1968 * otherwise reused by a RAID array (or any other kernel
1969 * subsystem), by bd_claiming the device.
1971 static int lock_rdev(mdk_rdev_t *rdev, dev_t dev, int shared)
1973 int err = 0;
1974 struct block_device *bdev;
1975 char b[BDEVNAME_SIZE];
1977 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
1978 shared ? (mdk_rdev_t *)lock_rdev : rdev);
1979 if (IS_ERR(bdev)) {
1980 printk(KERN_ERR "md: could not open %s.\n",
1981 __bdevname(dev, b));
1982 return PTR_ERR(bdev);
1984 rdev->bdev = bdev;
1985 return err;
1988 static void unlock_rdev(mdk_rdev_t *rdev)
1990 struct block_device *bdev = rdev->bdev;
1991 rdev->bdev = NULL;
1992 if (!bdev)
1993 MD_BUG();
1994 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
1997 void md_autodetect_dev(dev_t dev);
1999 static void export_rdev(mdk_rdev_t * rdev)
2001 char b[BDEVNAME_SIZE];
2002 printk(KERN_INFO "md: export_rdev(%s)\n",
2003 bdevname(rdev->bdev,b));
2004 if (rdev->mddev)
2005 MD_BUG();
2006 free_disk_sb(rdev);
2007 #ifndef MODULE
2008 if (test_bit(AutoDetected, &rdev->flags))
2009 md_autodetect_dev(rdev->bdev->bd_dev);
2010 #endif
2011 unlock_rdev(rdev);
2012 kobject_put(&rdev->kobj);
2015 static void kick_rdev_from_array(mdk_rdev_t * rdev)
2017 unbind_rdev_from_array(rdev);
2018 export_rdev(rdev);
2021 static void export_array(mddev_t *mddev)
2023 mdk_rdev_t *rdev, *tmp;
2025 rdev_for_each(rdev, tmp, mddev) {
2026 if (!rdev->mddev) {
2027 MD_BUG();
2028 continue;
2030 kick_rdev_from_array(rdev);
2032 if (!list_empty(&mddev->disks))
2033 MD_BUG();
2034 mddev->raid_disks = 0;
2035 mddev->major_version = 0;
2038 static void print_desc(mdp_disk_t *desc)
2040 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
2041 desc->major,desc->minor,desc->raid_disk,desc->state);
2044 static void print_sb_90(mdp_super_t *sb)
2046 int i;
2048 printk(KERN_INFO
2049 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
2050 sb->major_version, sb->minor_version, sb->patch_version,
2051 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
2052 sb->ctime);
2053 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
2054 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
2055 sb->md_minor, sb->layout, sb->chunk_size);
2056 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
2057 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
2058 sb->utime, sb->state, sb->active_disks, sb->working_disks,
2059 sb->failed_disks, sb->spare_disks,
2060 sb->sb_csum, (unsigned long)sb->events_lo);
2062 printk(KERN_INFO);
2063 for (i = 0; i < MD_SB_DISKS; i++) {
2064 mdp_disk_t *desc;
2066 desc = sb->disks + i;
2067 if (desc->number || desc->major || desc->minor ||
2068 desc->raid_disk || (desc->state && (desc->state != 4))) {
2069 printk(" D %2d: ", i);
2070 print_desc(desc);
2073 printk(KERN_INFO "md: THIS: ");
2074 print_desc(&sb->this_disk);
2077 static void print_sb_1(struct mdp_superblock_1 *sb)
2079 __u8 *uuid;
2081 uuid = sb->set_uuid;
2082 printk(KERN_INFO
2083 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n"
2084 "md: Name: \"%s\" CT:%llu\n",
2085 le32_to_cpu(sb->major_version),
2086 le32_to_cpu(sb->feature_map),
2087 uuid,
2088 sb->set_name,
2089 (unsigned long long)le64_to_cpu(sb->ctime)
2090 & MD_SUPERBLOCK_1_TIME_SEC_MASK);
2092 uuid = sb->device_uuid;
2093 printk(KERN_INFO
2094 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
2095 " RO:%llu\n"
2096 "md: Dev:%08x UUID: %pU\n"
2097 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
2098 "md: (MaxDev:%u) \n",
2099 le32_to_cpu(sb->level),
2100 (unsigned long long)le64_to_cpu(sb->size),
2101 le32_to_cpu(sb->raid_disks),
2102 le32_to_cpu(sb->layout),
2103 le32_to_cpu(sb->chunksize),
2104 (unsigned long long)le64_to_cpu(sb->data_offset),
2105 (unsigned long long)le64_to_cpu(sb->data_size),
2106 (unsigned long long)le64_to_cpu(sb->super_offset),
2107 (unsigned long long)le64_to_cpu(sb->recovery_offset),
2108 le32_to_cpu(sb->dev_number),
2109 uuid,
2110 sb->devflags,
2111 (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK,
2112 (unsigned long long)le64_to_cpu(sb->events),
2113 (unsigned long long)le64_to_cpu(sb->resync_offset),
2114 le32_to_cpu(sb->sb_csum),
2115 le32_to_cpu(sb->max_dev)
2119 static void print_rdev(mdk_rdev_t *rdev, int major_version)
2121 char b[BDEVNAME_SIZE];
2122 printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
2123 bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors,
2124 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
2125 rdev->desc_nr);
2126 if (rdev->sb_loaded) {
2127 printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version);
2128 switch (major_version) {
2129 case 0:
2130 print_sb_90((mdp_super_t*)page_address(rdev->sb_page));
2131 break;
2132 case 1:
2133 print_sb_1((struct mdp_superblock_1 *)page_address(rdev->sb_page));
2134 break;
2136 } else
2137 printk(KERN_INFO "md: no rdev superblock!\n");
2140 static void md_print_devices(void)
2142 struct list_head *tmp;
2143 mdk_rdev_t *rdev;
2144 mddev_t *mddev;
2145 char b[BDEVNAME_SIZE];
2147 printk("\n");
2148 printk("md: **********************************\n");
2149 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
2150 printk("md: **********************************\n");
2151 for_each_mddev(mddev, tmp) {
2153 if (mddev->bitmap)
2154 bitmap_print_sb(mddev->bitmap);
2155 else
2156 printk("%s: ", mdname(mddev));
2157 list_for_each_entry(rdev, &mddev->disks, same_set)
2158 printk("<%s>", bdevname(rdev->bdev,b));
2159 printk("\n");
2161 list_for_each_entry(rdev, &mddev->disks, same_set)
2162 print_rdev(rdev, mddev->major_version);
2164 printk("md: **********************************\n");
2165 printk("\n");
2169 static void sync_sbs(mddev_t * mddev, int nospares)
2171 /* Update each superblock (in-memory image), but
2172 * if we are allowed to, skip spares which already
2173 * have the right event counter, or have one earlier
2174 * (which would mean they aren't being marked as dirty
2175 * with the rest of the array)
2177 mdk_rdev_t *rdev;
2178 list_for_each_entry(rdev, &mddev->disks, same_set) {
2179 if (rdev->sb_events == mddev->events ||
2180 (nospares &&
2181 rdev->raid_disk < 0 &&
2182 rdev->sb_events+1 == mddev->events)) {
2183 /* Don't update this superblock */
2184 rdev->sb_loaded = 2;
2185 } else {
2186 sync_super(mddev, rdev);
2187 rdev->sb_loaded = 1;
2192 static void md_update_sb(mddev_t * mddev, int force_change)
2194 mdk_rdev_t *rdev;
2195 int sync_req;
2196 int nospares = 0;
2198 repeat:
2199 /* First make sure individual recovery_offsets are correct */
2200 list_for_each_entry(rdev, &mddev->disks, same_set) {
2201 if (rdev->raid_disk >= 0 &&
2202 mddev->delta_disks >= 0 &&
2203 !test_bit(In_sync, &rdev->flags) &&
2204 mddev->curr_resync_completed > rdev->recovery_offset)
2205 rdev->recovery_offset = mddev->curr_resync_completed;
2208 if (!mddev->persistent) {
2209 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
2210 clear_bit(MD_CHANGE_DEVS, &mddev->flags);
2211 if (!mddev->external)
2212 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
2213 wake_up(&mddev->sb_wait);
2214 return;
2217 spin_lock_irq(&mddev->write_lock);
2219 mddev->utime = get_seconds();
2221 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2222 force_change = 1;
2223 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2224 /* just a clean<-> dirty transition, possibly leave spares alone,
2225 * though if events isn't the right even/odd, we will have to do
2226 * spares after all
2228 nospares = 1;
2229 if (force_change)
2230 nospares = 0;
2231 if (mddev->degraded)
2232 /* If the array is degraded, then skipping spares is both
2233 * dangerous and fairly pointless.
2234 * Dangerous because a device that was removed from the array
2235 * might have a event_count that still looks up-to-date,
2236 * so it can be re-added without a resync.
2237 * Pointless because if there are any spares to skip,
2238 * then a recovery will happen and soon that array won't
2239 * be degraded any more and the spare can go back to sleep then.
2241 nospares = 0;
2243 sync_req = mddev->in_sync;
2245 /* If this is just a dirty<->clean transition, and the array is clean
2246 * and 'events' is odd, we can roll back to the previous clean state */
2247 if (nospares
2248 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
2249 && mddev->can_decrease_events
2250 && mddev->events != 1) {
2251 mddev->events--;
2252 mddev->can_decrease_events = 0;
2253 } else {
2254 /* otherwise we have to go forward and ... */
2255 mddev->events ++;
2256 mddev->can_decrease_events = nospares;
2259 if (!mddev->events) {
2261 * oops, this 64-bit counter should never wrap.
2262 * Either we are in around ~1 trillion A.C., assuming
2263 * 1 reboot per second, or we have a bug:
2265 MD_BUG();
2266 mddev->events --;
2268 sync_sbs(mddev, nospares);
2269 spin_unlock_irq(&mddev->write_lock);
2271 dprintk(KERN_INFO
2272 "md: updating %s RAID superblock on device (in sync %d)\n",
2273 mdname(mddev),mddev->in_sync);
2275 bitmap_update_sb(mddev->bitmap);
2276 list_for_each_entry(rdev, &mddev->disks, same_set) {
2277 char b[BDEVNAME_SIZE];
2278 dprintk(KERN_INFO "md: ");
2279 if (rdev->sb_loaded != 1)
2280 continue; /* no noise on spare devices */
2281 if (test_bit(Faulty, &rdev->flags))
2282 dprintk("(skipping faulty ");
2284 dprintk("%s ", bdevname(rdev->bdev,b));
2285 if (!test_bit(Faulty, &rdev->flags)) {
2286 md_super_write(mddev,rdev,
2287 rdev->sb_start, rdev->sb_size,
2288 rdev->sb_page);
2289 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
2290 bdevname(rdev->bdev,b),
2291 (unsigned long long)rdev->sb_start);
2292 rdev->sb_events = mddev->events;
2294 } else
2295 dprintk(")\n");
2296 if (mddev->level == LEVEL_MULTIPATH)
2297 /* only need to write one superblock... */
2298 break;
2300 md_super_wait(mddev);
2301 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
2303 spin_lock_irq(&mddev->write_lock);
2304 if (mddev->in_sync != sync_req ||
2305 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
2306 /* have to write it out again */
2307 spin_unlock_irq(&mddev->write_lock);
2308 goto repeat;
2310 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
2311 spin_unlock_irq(&mddev->write_lock);
2312 wake_up(&mddev->sb_wait);
2313 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2314 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
2318 /* words written to sysfs files may, or may not, be \n terminated.
2319 * We want to accept with case. For this we use cmd_match.
2321 static int cmd_match(const char *cmd, const char *str)
2323 /* See if cmd, written into a sysfs file, matches
2324 * str. They must either be the same, or cmd can
2325 * have a trailing newline
2327 while (*cmd && *str && *cmd == *str) {
2328 cmd++;
2329 str++;
2331 if (*cmd == '\n')
2332 cmd++;
2333 if (*str || *cmd)
2334 return 0;
2335 return 1;
2338 struct rdev_sysfs_entry {
2339 struct attribute attr;
2340 ssize_t (*show)(mdk_rdev_t *, char *);
2341 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
2344 static ssize_t
2345 state_show(mdk_rdev_t *rdev, char *page)
2347 char *sep = "";
2348 size_t len = 0;
2350 if (test_bit(Faulty, &rdev->flags)) {
2351 len+= sprintf(page+len, "%sfaulty",sep);
2352 sep = ",";
2354 if (test_bit(In_sync, &rdev->flags)) {
2355 len += sprintf(page+len, "%sin_sync",sep);
2356 sep = ",";
2358 if (test_bit(WriteMostly, &rdev->flags)) {
2359 len += sprintf(page+len, "%swrite_mostly",sep);
2360 sep = ",";
2362 if (test_bit(Blocked, &rdev->flags)) {
2363 len += sprintf(page+len, "%sblocked", sep);
2364 sep = ",";
2366 if (!test_bit(Faulty, &rdev->flags) &&
2367 !test_bit(In_sync, &rdev->flags)) {
2368 len += sprintf(page+len, "%sspare", sep);
2369 sep = ",";
2371 return len+sprintf(page+len, "\n");
2374 static ssize_t
2375 state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2377 /* can write
2378 * faulty - simulates and error
2379 * remove - disconnects the device
2380 * writemostly - sets write_mostly
2381 * -writemostly - clears write_mostly
2382 * blocked - sets the Blocked flag
2383 * -blocked - clears the Blocked flag
2384 * insync - sets Insync providing device isn't active
2386 int err = -EINVAL;
2387 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2388 md_error(rdev->mddev, rdev);
2389 err = 0;
2390 } else if (cmd_match(buf, "remove")) {
2391 if (rdev->raid_disk >= 0)
2392 err = -EBUSY;
2393 else {
2394 mddev_t *mddev = rdev->mddev;
2395 kick_rdev_from_array(rdev);
2396 if (mddev->pers)
2397 md_update_sb(mddev, 1);
2398 md_new_event(mddev);
2399 err = 0;
2401 } else if (cmd_match(buf, "writemostly")) {
2402 set_bit(WriteMostly, &rdev->flags);
2403 err = 0;
2404 } else if (cmd_match(buf, "-writemostly")) {
2405 clear_bit(WriteMostly, &rdev->flags);
2406 err = 0;
2407 } else if (cmd_match(buf, "blocked")) {
2408 set_bit(Blocked, &rdev->flags);
2409 err = 0;
2410 } else if (cmd_match(buf, "-blocked")) {
2411 clear_bit(Blocked, &rdev->flags);
2412 wake_up(&rdev->blocked_wait);
2413 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2414 md_wakeup_thread(rdev->mddev->thread);
2416 err = 0;
2417 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2418 set_bit(In_sync, &rdev->flags);
2419 err = 0;
2421 if (!err)
2422 sysfs_notify_dirent_safe(rdev->sysfs_state);
2423 return err ? err : len;
2425 static struct rdev_sysfs_entry rdev_state =
2426 __ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
2428 static ssize_t
2429 errors_show(mdk_rdev_t *rdev, char *page)
2431 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2434 static ssize_t
2435 errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2437 char *e;
2438 unsigned long n = simple_strtoul(buf, &e, 10);
2439 if (*buf && (*e == 0 || *e == '\n')) {
2440 atomic_set(&rdev->corrected_errors, n);
2441 return len;
2443 return -EINVAL;
2445 static struct rdev_sysfs_entry rdev_errors =
2446 __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
2448 static ssize_t
2449 slot_show(mdk_rdev_t *rdev, char *page)
2451 if (rdev->raid_disk < 0)
2452 return sprintf(page, "none\n");
2453 else
2454 return sprintf(page, "%d\n", rdev->raid_disk);
2457 static ssize_t
2458 slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2460 char *e;
2461 int err;
2462 char nm[20];
2463 int slot = simple_strtoul(buf, &e, 10);
2464 if (strncmp(buf, "none", 4)==0)
2465 slot = -1;
2466 else if (e==buf || (*e && *e!= '\n'))
2467 return -EINVAL;
2468 if (rdev->mddev->pers && slot == -1) {
2469 /* Setting 'slot' on an active array requires also
2470 * updating the 'rd%d' link, and communicating
2471 * with the personality with ->hot_*_disk.
2472 * For now we only support removing
2473 * failed/spare devices. This normally happens automatically,
2474 * but not when the metadata is externally managed.
2476 if (rdev->raid_disk == -1)
2477 return -EEXIST;
2478 /* personality does all needed checks */
2479 if (rdev->mddev->pers->hot_remove_disk == NULL)
2480 return -EINVAL;
2481 err = rdev->mddev->pers->
2482 hot_remove_disk(rdev->mddev, rdev->raid_disk);
2483 if (err)
2484 return err;
2485 sprintf(nm, "rd%d", rdev->raid_disk);
2486 sysfs_remove_link(&rdev->mddev->kobj, nm);
2487 rdev->raid_disk = -1;
2488 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2489 md_wakeup_thread(rdev->mddev->thread);
2490 } else if (rdev->mddev->pers) {
2491 mdk_rdev_t *rdev2;
2492 /* Activating a spare .. or possibly reactivating
2493 * if we ever get bitmaps working here.
2496 if (rdev->raid_disk != -1)
2497 return -EBUSY;
2499 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
2500 return -EBUSY;
2502 if (rdev->mddev->pers->hot_add_disk == NULL)
2503 return -EINVAL;
2505 list_for_each_entry(rdev2, &rdev->mddev->disks, same_set)
2506 if (rdev2->raid_disk == slot)
2507 return -EEXIST;
2509 if (slot >= rdev->mddev->raid_disks &&
2510 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2511 return -ENOSPC;
2513 rdev->raid_disk = slot;
2514 if (test_bit(In_sync, &rdev->flags))
2515 rdev->saved_raid_disk = slot;
2516 else
2517 rdev->saved_raid_disk = -1;
2518 err = rdev->mddev->pers->
2519 hot_add_disk(rdev->mddev, rdev);
2520 if (err) {
2521 rdev->raid_disk = -1;
2522 return err;
2523 } else
2524 sysfs_notify_dirent_safe(rdev->sysfs_state);
2525 sprintf(nm, "rd%d", rdev->raid_disk);
2526 if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm))
2527 /* failure here is OK */;
2528 /* don't wakeup anyone, leave that to userspace. */
2529 } else {
2530 if (slot >= rdev->mddev->raid_disks &&
2531 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2532 return -ENOSPC;
2533 rdev->raid_disk = slot;
2534 /* assume it is working */
2535 clear_bit(Faulty, &rdev->flags);
2536 clear_bit(WriteMostly, &rdev->flags);
2537 set_bit(In_sync, &rdev->flags);
2538 sysfs_notify_dirent_safe(rdev->sysfs_state);
2540 return len;
2544 static struct rdev_sysfs_entry rdev_slot =
2545 __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
2547 static ssize_t
2548 offset_show(mdk_rdev_t *rdev, char *page)
2550 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
2553 static ssize_t
2554 offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2556 char *e;
2557 unsigned long long offset = simple_strtoull(buf, &e, 10);
2558 if (e==buf || (*e && *e != '\n'))
2559 return -EINVAL;
2560 if (rdev->mddev->pers && rdev->raid_disk >= 0)
2561 return -EBUSY;
2562 if (rdev->sectors && rdev->mddev->external)
2563 /* Must set offset before size, so overlap checks
2564 * can be sane */
2565 return -EBUSY;
2566 rdev->data_offset = offset;
2567 return len;
2570 static struct rdev_sysfs_entry rdev_offset =
2571 __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
2573 static ssize_t
2574 rdev_size_show(mdk_rdev_t *rdev, char *page)
2576 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
2579 static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2581 /* check if two start/length pairs overlap */
2582 if (s1+l1 <= s2)
2583 return 0;
2584 if (s2+l2 <= s1)
2585 return 0;
2586 return 1;
2589 static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2591 unsigned long long blocks;
2592 sector_t new;
2594 if (strict_strtoull(buf, 10, &blocks) < 0)
2595 return -EINVAL;
2597 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2598 return -EINVAL; /* sector conversion overflow */
2600 new = blocks * 2;
2601 if (new != blocks * 2)
2602 return -EINVAL; /* unsigned long long to sector_t overflow */
2604 *sectors = new;
2605 return 0;
2608 static ssize_t
2609 rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2611 mddev_t *my_mddev = rdev->mddev;
2612 sector_t oldsectors = rdev->sectors;
2613 sector_t sectors;
2615 if (strict_blocks_to_sectors(buf, &sectors) < 0)
2616 return -EINVAL;
2617 if (my_mddev->pers && rdev->raid_disk >= 0) {
2618 if (my_mddev->persistent) {
2619 sectors = super_types[my_mddev->major_version].
2620 rdev_size_change(rdev, sectors);
2621 if (!sectors)
2622 return -EBUSY;
2623 } else if (!sectors)
2624 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
2625 rdev->data_offset;
2627 if (sectors < my_mddev->dev_sectors)
2628 return -EINVAL; /* component must fit device */
2630 rdev->sectors = sectors;
2631 if (sectors > oldsectors && my_mddev->external) {
2632 /* need to check that all other rdevs with the same ->bdev
2633 * do not overlap. We need to unlock the mddev to avoid
2634 * a deadlock. We have already changed rdev->sectors, and if
2635 * we have to change it back, we will have the lock again.
2637 mddev_t *mddev;
2638 int overlap = 0;
2639 struct list_head *tmp;
2641 mddev_unlock(my_mddev);
2642 for_each_mddev(mddev, tmp) {
2643 mdk_rdev_t *rdev2;
2645 mddev_lock(mddev);
2646 list_for_each_entry(rdev2, &mddev->disks, same_set)
2647 if (rdev->bdev == rdev2->bdev &&
2648 rdev != rdev2 &&
2649 overlaps(rdev->data_offset, rdev->sectors,
2650 rdev2->data_offset,
2651 rdev2->sectors)) {
2652 overlap = 1;
2653 break;
2655 mddev_unlock(mddev);
2656 if (overlap) {
2657 mddev_put(mddev);
2658 break;
2661 mddev_lock(my_mddev);
2662 if (overlap) {
2663 /* Someone else could have slipped in a size
2664 * change here, but doing so is just silly.
2665 * We put oldsectors back because we *know* it is
2666 * safe, and trust userspace not to race with
2667 * itself
2669 rdev->sectors = oldsectors;
2670 return -EBUSY;
2673 return len;
2676 static struct rdev_sysfs_entry rdev_size =
2677 __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
2680 static ssize_t recovery_start_show(mdk_rdev_t *rdev, char *page)
2682 unsigned long long recovery_start = rdev->recovery_offset;
2684 if (test_bit(In_sync, &rdev->flags) ||
2685 recovery_start == MaxSector)
2686 return sprintf(page, "none\n");
2688 return sprintf(page, "%llu\n", recovery_start);
2691 static ssize_t recovery_start_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2693 unsigned long long recovery_start;
2695 if (cmd_match(buf, "none"))
2696 recovery_start = MaxSector;
2697 else if (strict_strtoull(buf, 10, &recovery_start))
2698 return -EINVAL;
2700 if (rdev->mddev->pers &&
2701 rdev->raid_disk >= 0)
2702 return -EBUSY;
2704 rdev->recovery_offset = recovery_start;
2705 if (recovery_start == MaxSector)
2706 set_bit(In_sync, &rdev->flags);
2707 else
2708 clear_bit(In_sync, &rdev->flags);
2709 return len;
2712 static struct rdev_sysfs_entry rdev_recovery_start =
2713 __ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
2715 static struct attribute *rdev_default_attrs[] = {
2716 &rdev_state.attr,
2717 &rdev_errors.attr,
2718 &rdev_slot.attr,
2719 &rdev_offset.attr,
2720 &rdev_size.attr,
2721 &rdev_recovery_start.attr,
2722 NULL,
2724 static ssize_t
2725 rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2727 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2728 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
2729 mddev_t *mddev = rdev->mddev;
2730 ssize_t rv;
2732 if (!entry->show)
2733 return -EIO;
2735 rv = mddev ? mddev_lock(mddev) : -EBUSY;
2736 if (!rv) {
2737 if (rdev->mddev == NULL)
2738 rv = -EBUSY;
2739 else
2740 rv = entry->show(rdev, page);
2741 mddev_unlock(mddev);
2743 return rv;
2746 static ssize_t
2747 rdev_attr_store(struct kobject *kobj, struct attribute *attr,
2748 const char *page, size_t length)
2750 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2751 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
2752 ssize_t rv;
2753 mddev_t *mddev = rdev->mddev;
2755 if (!entry->store)
2756 return -EIO;
2757 if (!capable(CAP_SYS_ADMIN))
2758 return -EACCES;
2759 rv = mddev ? mddev_lock(mddev): -EBUSY;
2760 if (!rv) {
2761 if (rdev->mddev == NULL)
2762 rv = -EBUSY;
2763 else
2764 rv = entry->store(rdev, page, length);
2765 mddev_unlock(mddev);
2767 return rv;
2770 static void rdev_free(struct kobject *ko)
2772 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
2773 kfree(rdev);
2775 static const struct sysfs_ops rdev_sysfs_ops = {
2776 .show = rdev_attr_show,
2777 .store = rdev_attr_store,
2779 static struct kobj_type rdev_ktype = {
2780 .release = rdev_free,
2781 .sysfs_ops = &rdev_sysfs_ops,
2782 .default_attrs = rdev_default_attrs,
2785 void md_rdev_init(mdk_rdev_t *rdev)
2787 rdev->desc_nr = -1;
2788 rdev->saved_raid_disk = -1;
2789 rdev->raid_disk = -1;
2790 rdev->flags = 0;
2791 rdev->data_offset = 0;
2792 rdev->sb_events = 0;
2793 rdev->last_read_error.tv_sec = 0;
2794 rdev->last_read_error.tv_nsec = 0;
2795 atomic_set(&rdev->nr_pending, 0);
2796 atomic_set(&rdev->read_errors, 0);
2797 atomic_set(&rdev->corrected_errors, 0);
2799 INIT_LIST_HEAD(&rdev->same_set);
2800 init_waitqueue_head(&rdev->blocked_wait);
2802 EXPORT_SYMBOL_GPL(md_rdev_init);
2804 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2806 * mark the device faulty if:
2808 * - the device is nonexistent (zero size)
2809 * - the device has no valid superblock
2811 * a faulty rdev _never_ has rdev->sb set.
2813 static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
2815 char b[BDEVNAME_SIZE];
2816 int err;
2817 mdk_rdev_t *rdev;
2818 sector_t size;
2820 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
2821 if (!rdev) {
2822 printk(KERN_ERR "md: could not alloc mem for new device!\n");
2823 return ERR_PTR(-ENOMEM);
2826 md_rdev_init(rdev);
2827 if ((err = alloc_disk_sb(rdev)))
2828 goto abort_free;
2830 err = lock_rdev(rdev, newdev, super_format == -2);
2831 if (err)
2832 goto abort_free;
2834 kobject_init(&rdev->kobj, &rdev_ktype);
2836 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
2837 if (!size) {
2838 printk(KERN_WARNING
2839 "md: %s has zero or unknown size, marking faulty!\n",
2840 bdevname(rdev->bdev,b));
2841 err = -EINVAL;
2842 goto abort_free;
2845 if (super_format >= 0) {
2846 err = super_types[super_format].
2847 load_super(rdev, NULL, super_minor);
2848 if (err == -EINVAL) {
2849 printk(KERN_WARNING
2850 "md: %s does not have a valid v%d.%d "
2851 "superblock, not importing!\n",
2852 bdevname(rdev->bdev,b),
2853 super_format, super_minor);
2854 goto abort_free;
2856 if (err < 0) {
2857 printk(KERN_WARNING
2858 "md: could not read %s's sb, not importing!\n",
2859 bdevname(rdev->bdev,b));
2860 goto abort_free;
2864 return rdev;
2866 abort_free:
2867 if (rdev->sb_page) {
2868 if (rdev->bdev)
2869 unlock_rdev(rdev);
2870 free_disk_sb(rdev);
2872 kfree(rdev);
2873 return ERR_PTR(err);
2877 * Check a full RAID array for plausibility
2881 static void analyze_sbs(mddev_t * mddev)
2883 int i;
2884 mdk_rdev_t *rdev, *freshest, *tmp;
2885 char b[BDEVNAME_SIZE];
2887 freshest = NULL;
2888 rdev_for_each(rdev, tmp, mddev)
2889 switch (super_types[mddev->major_version].
2890 load_super(rdev, freshest, mddev->minor_version)) {
2891 case 1:
2892 freshest = rdev;
2893 break;
2894 case 0:
2895 break;
2896 default:
2897 printk( KERN_ERR \
2898 "md: fatal superblock inconsistency in %s"
2899 " -- removing from array\n",
2900 bdevname(rdev->bdev,b));
2901 kick_rdev_from_array(rdev);
2905 super_types[mddev->major_version].
2906 validate_super(mddev, freshest);
2908 i = 0;
2909 rdev_for_each(rdev, tmp, mddev) {
2910 if (mddev->max_disks &&
2911 (rdev->desc_nr >= mddev->max_disks ||
2912 i > mddev->max_disks)) {
2913 printk(KERN_WARNING
2914 "md: %s: %s: only %d devices permitted\n",
2915 mdname(mddev), bdevname(rdev->bdev, b),
2916 mddev->max_disks);
2917 kick_rdev_from_array(rdev);
2918 continue;
2920 if (rdev != freshest)
2921 if (super_types[mddev->major_version].
2922 validate_super(mddev, rdev)) {
2923 printk(KERN_WARNING "md: kicking non-fresh %s"
2924 " from array!\n",
2925 bdevname(rdev->bdev,b));
2926 kick_rdev_from_array(rdev);
2927 continue;
2929 if (mddev->level == LEVEL_MULTIPATH) {
2930 rdev->desc_nr = i++;
2931 rdev->raid_disk = rdev->desc_nr;
2932 set_bit(In_sync, &rdev->flags);
2933 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
2934 rdev->raid_disk = -1;
2935 clear_bit(In_sync, &rdev->flags);
2940 /* Read a fixed-point number.
2941 * Numbers in sysfs attributes should be in "standard" units where
2942 * possible, so time should be in seconds.
2943 * However we internally use a a much smaller unit such as
2944 * milliseconds or jiffies.
2945 * This function takes a decimal number with a possible fractional
2946 * component, and produces an integer which is the result of
2947 * multiplying that number by 10^'scale'.
2948 * all without any floating-point arithmetic.
2950 int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
2952 unsigned long result = 0;
2953 long decimals = -1;
2954 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
2955 if (*cp == '.')
2956 decimals = 0;
2957 else if (decimals < scale) {
2958 unsigned int value;
2959 value = *cp - '0';
2960 result = result * 10 + value;
2961 if (decimals >= 0)
2962 decimals++;
2964 cp++;
2966 if (*cp == '\n')
2967 cp++;
2968 if (*cp)
2969 return -EINVAL;
2970 if (decimals < 0)
2971 decimals = 0;
2972 while (decimals < scale) {
2973 result *= 10;
2974 decimals ++;
2976 *res = result;
2977 return 0;
2981 static void md_safemode_timeout(unsigned long data);
2983 static ssize_t
2984 safe_delay_show(mddev_t *mddev, char *page)
2986 int msec = (mddev->safemode_delay*1000)/HZ;
2987 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
2989 static ssize_t
2990 safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
2992 unsigned long msec;
2994 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
2995 return -EINVAL;
2996 if (msec == 0)
2997 mddev->safemode_delay = 0;
2998 else {
2999 unsigned long old_delay = mddev->safemode_delay;
3000 mddev->safemode_delay = (msec*HZ)/1000;
3001 if (mddev->safemode_delay == 0)
3002 mddev->safemode_delay = 1;
3003 if (mddev->safemode_delay < old_delay)
3004 md_safemode_timeout((unsigned long)mddev);
3006 return len;
3008 static struct md_sysfs_entry md_safe_delay =
3009 __ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
3011 static ssize_t
3012 level_show(mddev_t *mddev, char *page)
3014 struct mdk_personality *p = mddev->pers;
3015 if (p)
3016 return sprintf(page, "%s\n", p->name);
3017 else if (mddev->clevel[0])
3018 return sprintf(page, "%s\n", mddev->clevel);
3019 else if (mddev->level != LEVEL_NONE)
3020 return sprintf(page, "%d\n", mddev->level);
3021 else
3022 return 0;
3025 static ssize_t
3026 level_store(mddev_t *mddev, const char *buf, size_t len)
3028 char clevel[16];
3029 ssize_t rv = len;
3030 struct mdk_personality *pers;
3031 long level;
3032 void *priv;
3033 mdk_rdev_t *rdev;
3035 if (mddev->pers == NULL) {
3036 if (len == 0)
3037 return 0;
3038 if (len >= sizeof(mddev->clevel))
3039 return -ENOSPC;
3040 strncpy(mddev->clevel, buf, len);
3041 if (mddev->clevel[len-1] == '\n')
3042 len--;
3043 mddev->clevel[len] = 0;
3044 mddev->level = LEVEL_NONE;
3045 return rv;
3048 /* request to change the personality. Need to ensure:
3049 * - array is not engaged in resync/recovery/reshape
3050 * - old personality can be suspended
3051 * - new personality will access other array.
3054 if (mddev->sync_thread ||
3055 mddev->reshape_position != MaxSector ||
3056 mddev->sysfs_active)
3057 return -EBUSY;
3059 if (!mddev->pers->quiesce) {
3060 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
3061 mdname(mddev), mddev->pers->name);
3062 return -EINVAL;
3065 /* Now find the new personality */
3066 if (len == 0 || len >= sizeof(clevel))
3067 return -EINVAL;
3068 strncpy(clevel, buf, len);
3069 if (clevel[len-1] == '\n')
3070 len--;
3071 clevel[len] = 0;
3072 if (strict_strtol(clevel, 10, &level))
3073 level = LEVEL_NONE;
3075 if (request_module("md-%s", clevel) != 0)
3076 request_module("md-level-%s", clevel);
3077 spin_lock(&pers_lock);
3078 pers = find_pers(level, clevel);
3079 if (!pers || !try_module_get(pers->owner)) {
3080 spin_unlock(&pers_lock);
3081 printk(KERN_WARNING "md: personality %s not loaded\n", clevel);
3082 return -EINVAL;
3084 spin_unlock(&pers_lock);
3086 if (pers == mddev->pers) {
3087 /* Nothing to do! */
3088 module_put(pers->owner);
3089 return rv;
3091 if (!pers->takeover) {
3092 module_put(pers->owner);
3093 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
3094 mdname(mddev), clevel);
3095 return -EINVAL;
3098 list_for_each_entry(rdev, &mddev->disks, same_set)
3099 rdev->new_raid_disk = rdev->raid_disk;
3101 /* ->takeover must set new_* and/or delta_disks
3102 * if it succeeds, and may set them when it fails.
3104 priv = pers->takeover(mddev);
3105 if (IS_ERR(priv)) {
3106 mddev->new_level = mddev->level;
3107 mddev->new_layout = mddev->layout;
3108 mddev->new_chunk_sectors = mddev->chunk_sectors;
3109 mddev->raid_disks -= mddev->delta_disks;
3110 mddev->delta_disks = 0;
3111 module_put(pers->owner);
3112 printk(KERN_WARNING "md: %s: %s would not accept array\n",
3113 mdname(mddev), clevel);
3114 return PTR_ERR(priv);
3117 /* Looks like we have a winner */
3118 mddev_suspend(mddev);
3119 mddev->pers->stop(mddev);
3121 if (mddev->pers->sync_request == NULL &&
3122 pers->sync_request != NULL) {
3123 /* need to add the md_redundancy_group */
3124 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3125 printk(KERN_WARNING
3126 "md: cannot register extra attributes for %s\n",
3127 mdname(mddev));
3128 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, NULL, "sync_action");
3130 if (mddev->pers->sync_request != NULL &&
3131 pers->sync_request == NULL) {
3132 /* need to remove the md_redundancy_group */
3133 if (mddev->to_remove == NULL)
3134 mddev->to_remove = &md_redundancy_group;
3137 if (mddev->pers->sync_request == NULL &&
3138 mddev->external) {
3139 /* We are converting from a no-redundancy array
3140 * to a redundancy array and metadata is managed
3141 * externally so we need to be sure that writes
3142 * won't block due to a need to transition
3143 * clean->dirty
3144 * until external management is started.
3146 mddev->in_sync = 0;
3147 mddev->safemode_delay = 0;
3148 mddev->safemode = 0;
3151 list_for_each_entry(rdev, &mddev->disks, same_set) {
3152 char nm[20];
3153 if (rdev->raid_disk < 0)
3154 continue;
3155 if (rdev->new_raid_disk >= mddev->raid_disks)
3156 rdev->new_raid_disk = -1;
3157 if (rdev->new_raid_disk == rdev->raid_disk)
3158 continue;
3159 sprintf(nm, "rd%d", rdev->raid_disk);
3160 sysfs_remove_link(&mddev->kobj, nm);
3162 list_for_each_entry(rdev, &mddev->disks, same_set) {
3163 if (rdev->raid_disk < 0)
3164 continue;
3165 if (rdev->new_raid_disk == rdev->raid_disk)
3166 continue;
3167 rdev->raid_disk = rdev->new_raid_disk;
3168 if (rdev->raid_disk < 0)
3169 clear_bit(In_sync, &rdev->flags);
3170 else {
3171 char nm[20];
3172 sprintf(nm, "rd%d", rdev->raid_disk);
3173 if(sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
3174 printk("md: cannot register %s for %s after level change\n",
3175 nm, mdname(mddev));
3179 module_put(mddev->pers->owner);
3180 mddev->pers = pers;
3181 mddev->private = priv;
3182 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3183 mddev->level = mddev->new_level;
3184 mddev->layout = mddev->new_layout;
3185 mddev->chunk_sectors = mddev->new_chunk_sectors;
3186 mddev->delta_disks = 0;
3187 mddev->degraded = 0;
3188 if (mddev->pers->sync_request == NULL) {
3189 /* this is now an array without redundancy, so
3190 * it must always be in_sync
3192 mddev->in_sync = 1;
3193 del_timer_sync(&mddev->safemode_timer);
3195 pers->run(mddev);
3196 mddev_resume(mddev);
3197 set_bit(MD_CHANGE_DEVS, &mddev->flags);
3198 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3199 md_wakeup_thread(mddev->thread);
3200 sysfs_notify(&mddev->kobj, NULL, "level");
3201 md_new_event(mddev);
3202 return rv;
3205 static struct md_sysfs_entry md_level =
3206 __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
3209 static ssize_t
3210 layout_show(mddev_t *mddev, char *page)
3212 /* just a number, not meaningful for all levels */
3213 if (mddev->reshape_position != MaxSector &&
3214 mddev->layout != mddev->new_layout)
3215 return sprintf(page, "%d (%d)\n",
3216 mddev->new_layout, mddev->layout);
3217 return sprintf(page, "%d\n", mddev->layout);
3220 static ssize_t
3221 layout_store(mddev_t *mddev, const char *buf, size_t len)
3223 char *e;
3224 unsigned long n = simple_strtoul(buf, &e, 10);
3226 if (!*buf || (*e && *e != '\n'))
3227 return -EINVAL;
3229 if (mddev->pers) {
3230 int err;
3231 if (mddev->pers->check_reshape == NULL)
3232 return -EBUSY;
3233 mddev->new_layout = n;
3234 err = mddev->pers->check_reshape(mddev);
3235 if (err) {
3236 mddev->new_layout = mddev->layout;
3237 return err;
3239 } else {
3240 mddev->new_layout = n;
3241 if (mddev->reshape_position == MaxSector)
3242 mddev->layout = n;
3244 return len;
3246 static struct md_sysfs_entry md_layout =
3247 __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
3250 static ssize_t
3251 raid_disks_show(mddev_t *mddev, char *page)
3253 if (mddev->raid_disks == 0)
3254 return 0;
3255 if (mddev->reshape_position != MaxSector &&
3256 mddev->delta_disks != 0)
3257 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3258 mddev->raid_disks - mddev->delta_disks);
3259 return sprintf(page, "%d\n", mddev->raid_disks);
3262 static int update_raid_disks(mddev_t *mddev, int raid_disks);
3264 static ssize_t
3265 raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
3267 char *e;
3268 int rv = 0;
3269 unsigned long n = simple_strtoul(buf, &e, 10);
3271 if (!*buf || (*e && *e != '\n'))
3272 return -EINVAL;
3274 if (mddev->pers)
3275 rv = update_raid_disks(mddev, n);
3276 else if (mddev->reshape_position != MaxSector) {
3277 int olddisks = mddev->raid_disks - mddev->delta_disks;
3278 mddev->delta_disks = n - olddisks;
3279 mddev->raid_disks = n;
3280 } else
3281 mddev->raid_disks = n;
3282 return rv ? rv : len;
3284 static struct md_sysfs_entry md_raid_disks =
3285 __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
3287 static ssize_t
3288 chunk_size_show(mddev_t *mddev, char *page)
3290 if (mddev->reshape_position != MaxSector &&
3291 mddev->chunk_sectors != mddev->new_chunk_sectors)
3292 return sprintf(page, "%d (%d)\n",
3293 mddev->new_chunk_sectors << 9,
3294 mddev->chunk_sectors << 9);
3295 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
3298 static ssize_t
3299 chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
3301 char *e;
3302 unsigned long n = simple_strtoul(buf, &e, 10);
3304 if (!*buf || (*e && *e != '\n'))
3305 return -EINVAL;
3307 if (mddev->pers) {
3308 int err;
3309 if (mddev->pers->check_reshape == NULL)
3310 return -EBUSY;
3311 mddev->new_chunk_sectors = n >> 9;
3312 err = mddev->pers->check_reshape(mddev);
3313 if (err) {
3314 mddev->new_chunk_sectors = mddev->chunk_sectors;
3315 return err;
3317 } else {
3318 mddev->new_chunk_sectors = n >> 9;
3319 if (mddev->reshape_position == MaxSector)
3320 mddev->chunk_sectors = n >> 9;
3322 return len;
3324 static struct md_sysfs_entry md_chunk_size =
3325 __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
3327 static ssize_t
3328 resync_start_show(mddev_t *mddev, char *page)
3330 if (mddev->recovery_cp == MaxSector)
3331 return sprintf(page, "none\n");
3332 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3335 static ssize_t
3336 resync_start_store(mddev_t *mddev, const char *buf, size_t len)
3338 char *e;
3339 unsigned long long n = simple_strtoull(buf, &e, 10);
3341 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
3342 return -EBUSY;
3343 if (cmd_match(buf, "none"))
3344 n = MaxSector;
3345 else if (!*buf || (*e && *e != '\n'))
3346 return -EINVAL;
3348 mddev->recovery_cp = n;
3349 return len;
3351 static struct md_sysfs_entry md_resync_start =
3352 __ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
3355 * The array state can be:
3357 * clear
3358 * No devices, no size, no level
3359 * Equivalent to STOP_ARRAY ioctl
3360 * inactive
3361 * May have some settings, but array is not active
3362 * all IO results in error
3363 * When written, doesn't tear down array, but just stops it
3364 * suspended (not supported yet)
3365 * All IO requests will block. The array can be reconfigured.
3366 * Writing this, if accepted, will block until array is quiescent
3367 * readonly
3368 * no resync can happen. no superblocks get written.
3369 * write requests fail
3370 * read-auto
3371 * like readonly, but behaves like 'clean' on a write request.
3373 * clean - no pending writes, but otherwise active.
3374 * When written to inactive array, starts without resync
3375 * If a write request arrives then
3376 * if metadata is known, mark 'dirty' and switch to 'active'.
3377 * if not known, block and switch to write-pending
3378 * If written to an active array that has pending writes, then fails.
3379 * active
3380 * fully active: IO and resync can be happening.
3381 * When written to inactive array, starts with resync
3383 * write-pending
3384 * clean, but writes are blocked waiting for 'active' to be written.
3386 * active-idle
3387 * like active, but no writes have been seen for a while (100msec).
3390 enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3391 write_pending, active_idle, bad_word};
3392 static char *array_states[] = {
3393 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3394 "write-pending", "active-idle", NULL };
3396 static int match_word(const char *word, char **list)
3398 int n;
3399 for (n=0; list[n]; n++)
3400 if (cmd_match(word, list[n]))
3401 break;
3402 return n;
3405 static ssize_t
3406 array_state_show(mddev_t *mddev, char *page)
3408 enum array_state st = inactive;
3410 if (mddev->pers)
3411 switch(mddev->ro) {
3412 case 1:
3413 st = readonly;
3414 break;
3415 case 2:
3416 st = read_auto;
3417 break;
3418 case 0:
3419 if (mddev->in_sync)
3420 st = clean;
3421 else if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
3422 st = write_pending;
3423 else if (mddev->safemode)
3424 st = active_idle;
3425 else
3426 st = active;
3428 else {
3429 if (list_empty(&mddev->disks) &&
3430 mddev->raid_disks == 0 &&
3431 mddev->dev_sectors == 0)
3432 st = clear;
3433 else
3434 st = inactive;
3436 return sprintf(page, "%s\n", array_states[st]);
3439 static int do_md_stop(mddev_t * mddev, int ro, int is_open);
3440 static int md_set_readonly(mddev_t * mddev, int is_open);
3441 static int do_md_run(mddev_t * mddev);
3442 static int restart_array(mddev_t *mddev);
3444 static ssize_t
3445 array_state_store(mddev_t *mddev, const char *buf, size_t len)
3447 int err = -EINVAL;
3448 enum array_state st = match_word(buf, array_states);
3449 switch(st) {
3450 case bad_word:
3451 break;
3452 case clear:
3453 /* stopping an active array */
3454 if (atomic_read(&mddev->openers) > 0)
3455 return -EBUSY;
3456 err = do_md_stop(mddev, 0, 0);
3457 break;
3458 case inactive:
3459 /* stopping an active array */
3460 if (mddev->pers) {
3461 if (atomic_read(&mddev->openers) > 0)
3462 return -EBUSY;
3463 err = do_md_stop(mddev, 2, 0);
3464 } else
3465 err = 0; /* already inactive */
3466 break;
3467 case suspended:
3468 break; /* not supported yet */
3469 case readonly:
3470 if (mddev->pers)
3471 err = md_set_readonly(mddev, 0);
3472 else {
3473 mddev->ro = 1;
3474 set_disk_ro(mddev->gendisk, 1);
3475 err = do_md_run(mddev);
3477 break;
3478 case read_auto:
3479 if (mddev->pers) {
3480 if (mddev->ro == 0)
3481 err = md_set_readonly(mddev, 0);
3482 else if (mddev->ro == 1)
3483 err = restart_array(mddev);
3484 if (err == 0) {
3485 mddev->ro = 2;
3486 set_disk_ro(mddev->gendisk, 0);
3488 } else {
3489 mddev->ro = 2;
3490 err = do_md_run(mddev);
3492 break;
3493 case clean:
3494 if (mddev->pers) {
3495 restart_array(mddev);
3496 spin_lock_irq(&mddev->write_lock);
3497 if (atomic_read(&mddev->writes_pending) == 0) {
3498 if (mddev->in_sync == 0) {
3499 mddev->in_sync = 1;
3500 if (mddev->safemode == 1)
3501 mddev->safemode = 0;
3502 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
3504 err = 0;
3505 } else
3506 err = -EBUSY;
3507 spin_unlock_irq(&mddev->write_lock);
3508 } else
3509 err = -EINVAL;
3510 break;
3511 case active:
3512 if (mddev->pers) {
3513 restart_array(mddev);
3514 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
3515 wake_up(&mddev->sb_wait);
3516 err = 0;
3517 } else {
3518 mddev->ro = 0;
3519 set_disk_ro(mddev->gendisk, 0);
3520 err = do_md_run(mddev);
3522 break;
3523 case write_pending:
3524 case active_idle:
3525 /* these cannot be set */
3526 break;
3528 if (err)
3529 return err;
3530 else {
3531 sysfs_notify_dirent_safe(mddev->sysfs_state);
3532 return len;
3535 static struct md_sysfs_entry md_array_state =
3536 __ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
3538 static ssize_t
3539 max_corrected_read_errors_show(mddev_t *mddev, char *page) {
3540 return sprintf(page, "%d\n",
3541 atomic_read(&mddev->max_corr_read_errors));
3544 static ssize_t
3545 max_corrected_read_errors_store(mddev_t *mddev, const char *buf, size_t len)
3547 char *e;
3548 unsigned long n = simple_strtoul(buf, &e, 10);
3550 if (*buf && (*e == 0 || *e == '\n')) {
3551 atomic_set(&mddev->max_corr_read_errors, n);
3552 return len;
3554 return -EINVAL;
3557 static struct md_sysfs_entry max_corr_read_errors =
3558 __ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
3559 max_corrected_read_errors_store);
3561 static ssize_t
3562 null_show(mddev_t *mddev, char *page)
3564 return -EINVAL;
3567 static ssize_t
3568 new_dev_store(mddev_t *mddev, const char *buf, size_t len)
3570 /* buf must be %d:%d\n? giving major and minor numbers */
3571 /* The new device is added to the array.
3572 * If the array has a persistent superblock, we read the
3573 * superblock to initialise info and check validity.
3574 * Otherwise, only checking done is that in bind_rdev_to_array,
3575 * which mainly checks size.
3577 char *e;
3578 int major = simple_strtoul(buf, &e, 10);
3579 int minor;
3580 dev_t dev;
3581 mdk_rdev_t *rdev;
3582 int err;
3584 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
3585 return -EINVAL;
3586 minor = simple_strtoul(e+1, &e, 10);
3587 if (*e && *e != '\n')
3588 return -EINVAL;
3589 dev = MKDEV(major, minor);
3590 if (major != MAJOR(dev) ||
3591 minor != MINOR(dev))
3592 return -EOVERFLOW;
3595 if (mddev->persistent) {
3596 rdev = md_import_device(dev, mddev->major_version,
3597 mddev->minor_version);
3598 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
3599 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
3600 mdk_rdev_t, same_set);
3601 err = super_types[mddev->major_version]
3602 .load_super(rdev, rdev0, mddev->minor_version);
3603 if (err < 0)
3604 goto out;
3606 } else if (mddev->external)
3607 rdev = md_import_device(dev, -2, -1);
3608 else
3609 rdev = md_import_device(dev, -1, -1);
3611 if (IS_ERR(rdev))
3612 return PTR_ERR(rdev);
3613 err = bind_rdev_to_array(rdev, mddev);
3614 out:
3615 if (err)
3616 export_rdev(rdev);
3617 return err ? err : len;
3620 static struct md_sysfs_entry md_new_device =
3621 __ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
3623 static ssize_t
3624 bitmap_store(mddev_t *mddev, const char *buf, size_t len)
3626 char *end;
3627 unsigned long chunk, end_chunk;
3629 if (!mddev->bitmap)
3630 goto out;
3631 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3632 while (*buf) {
3633 chunk = end_chunk = simple_strtoul(buf, &end, 0);
3634 if (buf == end) break;
3635 if (*end == '-') { /* range */
3636 buf = end + 1;
3637 end_chunk = simple_strtoul(buf, &end, 0);
3638 if (buf == end) break;
3640 if (*end && !isspace(*end)) break;
3641 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
3642 buf = skip_spaces(end);
3644 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
3645 out:
3646 return len;
3649 static struct md_sysfs_entry md_bitmap =
3650 __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
3652 static ssize_t
3653 size_show(mddev_t *mddev, char *page)
3655 return sprintf(page, "%llu\n",
3656 (unsigned long long)mddev->dev_sectors / 2);
3659 static int update_size(mddev_t *mddev, sector_t num_sectors);
3661 static ssize_t
3662 size_store(mddev_t *mddev, const char *buf, size_t len)
3664 /* If array is inactive, we can reduce the component size, but
3665 * not increase it (except from 0).
3666 * If array is active, we can try an on-line resize
3668 sector_t sectors;
3669 int err = strict_blocks_to_sectors(buf, &sectors);
3671 if (err < 0)
3672 return err;
3673 if (mddev->pers) {
3674 err = update_size(mddev, sectors);
3675 md_update_sb(mddev, 1);
3676 } else {
3677 if (mddev->dev_sectors == 0 ||
3678 mddev->dev_sectors > sectors)
3679 mddev->dev_sectors = sectors;
3680 else
3681 err = -ENOSPC;
3683 return err ? err : len;
3686 static struct md_sysfs_entry md_size =
3687 __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
3690 /* Metdata version.
3691 * This is one of
3692 * 'none' for arrays with no metadata (good luck...)
3693 * 'external' for arrays with externally managed metadata,
3694 * or N.M for internally known formats
3696 static ssize_t
3697 metadata_show(mddev_t *mddev, char *page)
3699 if (mddev->persistent)
3700 return sprintf(page, "%d.%d\n",
3701 mddev->major_version, mddev->minor_version);
3702 else if (mddev->external)
3703 return sprintf(page, "external:%s\n", mddev->metadata_type);
3704 else
3705 return sprintf(page, "none\n");
3708 static ssize_t
3709 metadata_store(mddev_t *mddev, const char *buf, size_t len)
3711 int major, minor;
3712 char *e;
3713 /* Changing the details of 'external' metadata is
3714 * always permitted. Otherwise there must be
3715 * no devices attached to the array.
3717 if (mddev->external && strncmp(buf, "external:", 9) == 0)
3719 else if (!list_empty(&mddev->disks))
3720 return -EBUSY;
3722 if (cmd_match(buf, "none")) {
3723 mddev->persistent = 0;
3724 mddev->external = 0;
3725 mddev->major_version = 0;
3726 mddev->minor_version = 90;
3727 return len;
3729 if (strncmp(buf, "external:", 9) == 0) {
3730 size_t namelen = len-9;
3731 if (namelen >= sizeof(mddev->metadata_type))
3732 namelen = sizeof(mddev->metadata_type)-1;
3733 strncpy(mddev->metadata_type, buf+9, namelen);
3734 mddev->metadata_type[namelen] = 0;
3735 if (namelen && mddev->metadata_type[namelen-1] == '\n')
3736 mddev->metadata_type[--namelen] = 0;
3737 mddev->persistent = 0;
3738 mddev->external = 1;
3739 mddev->major_version = 0;
3740 mddev->minor_version = 90;
3741 return len;
3743 major = simple_strtoul(buf, &e, 10);
3744 if (e==buf || *e != '.')
3745 return -EINVAL;
3746 buf = e+1;
3747 minor = simple_strtoul(buf, &e, 10);
3748 if (e==buf || (*e && *e != '\n') )
3749 return -EINVAL;
3750 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
3751 return -ENOENT;
3752 mddev->major_version = major;
3753 mddev->minor_version = minor;
3754 mddev->persistent = 1;
3755 mddev->external = 0;
3756 return len;
3759 static struct md_sysfs_entry md_metadata =
3760 __ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
3762 static ssize_t
3763 action_show(mddev_t *mddev, char *page)
3765 char *type = "idle";
3766 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
3767 type = "frozen";
3768 else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3769 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
3770 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
3771 type = "reshape";
3772 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
3773 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
3774 type = "resync";
3775 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
3776 type = "check";
3777 else
3778 type = "repair";
3779 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
3780 type = "recover";
3782 return sprintf(page, "%s\n", type);
3785 static void reap_sync_thread(mddev_t *mddev);
3787 static ssize_t
3788 action_store(mddev_t *mddev, const char *page, size_t len)
3790 if (!mddev->pers || !mddev->pers->sync_request)
3791 return -EINVAL;
3793 if (cmd_match(page, "frozen"))
3794 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3795 else
3796 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3798 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
3799 if (mddev->sync_thread) {
3800 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3801 reap_sync_thread(mddev);
3803 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3804 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
3805 return -EBUSY;
3806 else if (cmd_match(page, "resync"))
3807 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3808 else if (cmd_match(page, "recover")) {
3809 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
3810 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3811 } else if (cmd_match(page, "reshape")) {
3812 int err;
3813 if (mddev->pers->start_reshape == NULL)
3814 return -EINVAL;
3815 err = mddev->pers->start_reshape(mddev);
3816 if (err)
3817 return err;
3818 sysfs_notify(&mddev->kobj, NULL, "degraded");
3819 } else {
3820 if (cmd_match(page, "check"))
3821 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
3822 else if (!cmd_match(page, "repair"))
3823 return -EINVAL;
3824 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
3825 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
3827 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3828 md_wakeup_thread(mddev->thread);
3829 sysfs_notify_dirent_safe(mddev->sysfs_action);
3830 return len;
3833 static ssize_t
3834 mismatch_cnt_show(mddev_t *mddev, char *page)
3836 return sprintf(page, "%llu\n",
3837 (unsigned long long) mddev->resync_mismatches);
3840 static struct md_sysfs_entry md_scan_mode =
3841 __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
3844 static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
3846 static ssize_t
3847 sync_min_show(mddev_t *mddev, char *page)
3849 return sprintf(page, "%d (%s)\n", speed_min(mddev),
3850 mddev->sync_speed_min ? "local": "system");
3853 static ssize_t
3854 sync_min_store(mddev_t *mddev, const char *buf, size_t len)
3856 int min;
3857 char *e;
3858 if (strncmp(buf, "system", 6)==0) {
3859 mddev->sync_speed_min = 0;
3860 return len;
3862 min = simple_strtoul(buf, &e, 10);
3863 if (buf == e || (*e && *e != '\n') || min <= 0)
3864 return -EINVAL;
3865 mddev->sync_speed_min = min;
3866 return len;
3869 static struct md_sysfs_entry md_sync_min =
3870 __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
3872 static ssize_t
3873 sync_max_show(mddev_t *mddev, char *page)
3875 return sprintf(page, "%d (%s)\n", speed_max(mddev),
3876 mddev->sync_speed_max ? "local": "system");
3879 static ssize_t
3880 sync_max_store(mddev_t *mddev, const char *buf, size_t len)
3882 int max;
3883 char *e;
3884 if (strncmp(buf, "system", 6)==0) {
3885 mddev->sync_speed_max = 0;
3886 return len;
3888 max = simple_strtoul(buf, &e, 10);
3889 if (buf == e || (*e && *e != '\n') || max <= 0)
3890 return -EINVAL;
3891 mddev->sync_speed_max = max;
3892 return len;
3895 static struct md_sysfs_entry md_sync_max =
3896 __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
3898 static ssize_t
3899 degraded_show(mddev_t *mddev, char *page)
3901 return sprintf(page, "%d\n", mddev->degraded);
3903 static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
3905 static ssize_t
3906 sync_force_parallel_show(mddev_t *mddev, char *page)
3908 return sprintf(page, "%d\n", mddev->parallel_resync);
3911 static ssize_t
3912 sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len)
3914 long n;
3916 if (strict_strtol(buf, 10, &n))
3917 return -EINVAL;
3919 if (n != 0 && n != 1)
3920 return -EINVAL;
3922 mddev->parallel_resync = n;
3924 if (mddev->sync_thread)
3925 wake_up(&resync_wait);
3927 return len;
3930 /* force parallel resync, even with shared block devices */
3931 static struct md_sysfs_entry md_sync_force_parallel =
3932 __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
3933 sync_force_parallel_show, sync_force_parallel_store);
3935 static ssize_t
3936 sync_speed_show(mddev_t *mddev, char *page)
3938 unsigned long resync, dt, db;
3939 if (mddev->curr_resync == 0)
3940 return sprintf(page, "none\n");
3941 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
3942 dt = (jiffies - mddev->resync_mark) / HZ;
3943 if (!dt) dt++;
3944 db = resync - mddev->resync_mark_cnt;
3945 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
3948 static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
3950 static ssize_t
3951 sync_completed_show(mddev_t *mddev, char *page)
3953 unsigned long long max_sectors, resync;
3955 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3956 return sprintf(page, "none\n");
3958 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
3959 max_sectors = mddev->resync_max_sectors;
3960 else
3961 max_sectors = mddev->dev_sectors;
3963 resync = mddev->curr_resync_completed;
3964 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
3967 static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
3969 static ssize_t
3970 min_sync_show(mddev_t *mddev, char *page)
3972 return sprintf(page, "%llu\n",
3973 (unsigned long long)mddev->resync_min);
3975 static ssize_t
3976 min_sync_store(mddev_t *mddev, const char *buf, size_t len)
3978 unsigned long long min;
3979 if (strict_strtoull(buf, 10, &min))
3980 return -EINVAL;
3981 if (min > mddev->resync_max)
3982 return -EINVAL;
3983 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3984 return -EBUSY;
3986 /* Must be a multiple of chunk_size */
3987 if (mddev->chunk_sectors) {
3988 sector_t temp = min;
3989 if (sector_div(temp, mddev->chunk_sectors))
3990 return -EINVAL;
3992 mddev->resync_min = min;
3994 return len;
3997 static struct md_sysfs_entry md_min_sync =
3998 __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
4000 static ssize_t
4001 max_sync_show(mddev_t *mddev, char *page)
4003 if (mddev->resync_max == MaxSector)
4004 return sprintf(page, "max\n");
4005 else
4006 return sprintf(page, "%llu\n",
4007 (unsigned long long)mddev->resync_max);
4009 static ssize_t
4010 max_sync_store(mddev_t *mddev, const char *buf, size_t len)
4012 if (strncmp(buf, "max", 3) == 0)
4013 mddev->resync_max = MaxSector;
4014 else {
4015 unsigned long long max;
4016 if (strict_strtoull(buf, 10, &max))
4017 return -EINVAL;
4018 if (max < mddev->resync_min)
4019 return -EINVAL;
4020 if (max < mddev->resync_max &&
4021 mddev->ro == 0 &&
4022 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4023 return -EBUSY;
4025 /* Must be a multiple of chunk_size */
4026 if (mddev->chunk_sectors) {
4027 sector_t temp = max;
4028 if (sector_div(temp, mddev->chunk_sectors))
4029 return -EINVAL;
4031 mddev->resync_max = max;
4033 wake_up(&mddev->recovery_wait);
4034 return len;
4037 static struct md_sysfs_entry md_max_sync =
4038 __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4040 static ssize_t
4041 suspend_lo_show(mddev_t *mddev, char *page)
4043 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4046 static ssize_t
4047 suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
4049 char *e;
4050 unsigned long long new = simple_strtoull(buf, &e, 10);
4051 unsigned long long old = mddev->suspend_lo;
4053 if (mddev->pers == NULL ||
4054 mddev->pers->quiesce == NULL)
4055 return -EINVAL;
4056 if (buf == e || (*e && *e != '\n'))
4057 return -EINVAL;
4059 mddev->suspend_lo = new;
4060 if (new >= old)
4061 /* Shrinking suspended region */
4062 mddev->pers->quiesce(mddev, 2);
4063 else {
4064 /* Expanding suspended region - need to wait */
4065 mddev->pers->quiesce(mddev, 1);
4066 mddev->pers->quiesce(mddev, 0);
4068 return len;
4070 static struct md_sysfs_entry md_suspend_lo =
4071 __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4074 static ssize_t
4075 suspend_hi_show(mddev_t *mddev, char *page)
4077 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4080 static ssize_t
4081 suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
4083 char *e;
4084 unsigned long long new = simple_strtoull(buf, &e, 10);
4085 unsigned long long old = mddev->suspend_hi;
4087 if (mddev->pers == NULL ||
4088 mddev->pers->quiesce == NULL)
4089 return -EINVAL;
4090 if (buf == e || (*e && *e != '\n'))
4091 return -EINVAL;
4093 mddev->suspend_hi = new;
4094 if (new <= old)
4095 /* Shrinking suspended region */
4096 mddev->pers->quiesce(mddev, 2);
4097 else {
4098 /* Expanding suspended region - need to wait */
4099 mddev->pers->quiesce(mddev, 1);
4100 mddev->pers->quiesce(mddev, 0);
4102 return len;
4104 static struct md_sysfs_entry md_suspend_hi =
4105 __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4107 static ssize_t
4108 reshape_position_show(mddev_t *mddev, char *page)
4110 if (mddev->reshape_position != MaxSector)
4111 return sprintf(page, "%llu\n",
4112 (unsigned long long)mddev->reshape_position);
4113 strcpy(page, "none\n");
4114 return 5;
4117 static ssize_t
4118 reshape_position_store(mddev_t *mddev, const char *buf, size_t len)
4120 char *e;
4121 unsigned long long new = simple_strtoull(buf, &e, 10);
4122 if (mddev->pers)
4123 return -EBUSY;
4124 if (buf == e || (*e && *e != '\n'))
4125 return -EINVAL;
4126 mddev->reshape_position = new;
4127 mddev->delta_disks = 0;
4128 mddev->new_level = mddev->level;
4129 mddev->new_layout = mddev->layout;
4130 mddev->new_chunk_sectors = mddev->chunk_sectors;
4131 return len;
4134 static struct md_sysfs_entry md_reshape_position =
4135 __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4136 reshape_position_store);
4138 static ssize_t
4139 array_size_show(mddev_t *mddev, char *page)
4141 if (mddev->external_size)
4142 return sprintf(page, "%llu\n",
4143 (unsigned long long)mddev->array_sectors/2);
4144 else
4145 return sprintf(page, "default\n");
4148 static ssize_t
4149 array_size_store(mddev_t *mddev, const char *buf, size_t len)
4151 sector_t sectors;
4153 if (strncmp(buf, "default", 7) == 0) {
4154 if (mddev->pers)
4155 sectors = mddev->pers->size(mddev, 0, 0);
4156 else
4157 sectors = mddev->array_sectors;
4159 mddev->external_size = 0;
4160 } else {
4161 if (strict_blocks_to_sectors(buf, &sectors) < 0)
4162 return -EINVAL;
4163 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
4164 return -E2BIG;
4166 mddev->external_size = 1;
4169 mddev->array_sectors = sectors;
4170 if (mddev->pers) {
4171 set_capacity(mddev->gendisk, mddev->array_sectors);
4172 revalidate_disk(mddev->gendisk);
4174 return len;
4177 static struct md_sysfs_entry md_array_size =
4178 __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4179 array_size_store);
4181 static struct attribute *md_default_attrs[] = {
4182 &md_level.attr,
4183 &md_layout.attr,
4184 &md_raid_disks.attr,
4185 &md_chunk_size.attr,
4186 &md_size.attr,
4187 &md_resync_start.attr,
4188 &md_metadata.attr,
4189 &md_new_device.attr,
4190 &md_safe_delay.attr,
4191 &md_array_state.attr,
4192 &md_reshape_position.attr,
4193 &md_array_size.attr,
4194 &max_corr_read_errors.attr,
4195 NULL,
4198 static struct attribute *md_redundancy_attrs[] = {
4199 &md_scan_mode.attr,
4200 &md_mismatches.attr,
4201 &md_sync_min.attr,
4202 &md_sync_max.attr,
4203 &md_sync_speed.attr,
4204 &md_sync_force_parallel.attr,
4205 &md_sync_completed.attr,
4206 &md_min_sync.attr,
4207 &md_max_sync.attr,
4208 &md_suspend_lo.attr,
4209 &md_suspend_hi.attr,
4210 &md_bitmap.attr,
4211 &md_degraded.attr,
4212 NULL,
4214 static struct attribute_group md_redundancy_group = {
4215 .name = NULL,
4216 .attrs = md_redundancy_attrs,
4220 static ssize_t
4221 md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4223 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4224 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
4225 ssize_t rv;
4227 if (!entry->show)
4228 return -EIO;
4229 rv = mddev_lock(mddev);
4230 if (!rv) {
4231 rv = entry->show(mddev, page);
4232 mddev_unlock(mddev);
4234 return rv;
4237 static ssize_t
4238 md_attr_store(struct kobject *kobj, struct attribute *attr,
4239 const char *page, size_t length)
4241 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4242 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
4243 ssize_t rv;
4245 if (!entry->store)
4246 return -EIO;
4247 if (!capable(CAP_SYS_ADMIN))
4248 return -EACCES;
4249 rv = mddev_lock(mddev);
4250 if (mddev->hold_active == UNTIL_IOCTL)
4251 mddev->hold_active = 0;
4252 if (!rv) {
4253 rv = entry->store(mddev, page, length);
4254 mddev_unlock(mddev);
4256 return rv;
4259 static void md_free(struct kobject *ko)
4261 mddev_t *mddev = container_of(ko, mddev_t, kobj);
4263 if (mddev->sysfs_state)
4264 sysfs_put(mddev->sysfs_state);
4266 if (mddev->gendisk) {
4267 del_gendisk(mddev->gendisk);
4268 put_disk(mddev->gendisk);
4270 if (mddev->queue)
4271 blk_cleanup_queue(mddev->queue);
4273 kfree(mddev);
4276 static const struct sysfs_ops md_sysfs_ops = {
4277 .show = md_attr_show,
4278 .store = md_attr_store,
4280 static struct kobj_type md_ktype = {
4281 .release = md_free,
4282 .sysfs_ops = &md_sysfs_ops,
4283 .default_attrs = md_default_attrs,
4286 int mdp_major = 0;
4288 static void mddev_delayed_delete(struct work_struct *ws)
4290 mddev_t *mddev = container_of(ws, mddev_t, del_work);
4292 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
4293 kobject_del(&mddev->kobj);
4294 kobject_put(&mddev->kobj);
4297 static int md_alloc(dev_t dev, char *name)
4299 static DEFINE_MUTEX(disks_mutex);
4300 mddev_t *mddev = mddev_find(dev);
4301 struct gendisk *disk;
4302 int partitioned;
4303 int shift;
4304 int unit;
4305 int error;
4307 if (!mddev)
4308 return -ENODEV;
4310 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4311 shift = partitioned ? MdpMinorShift : 0;
4312 unit = MINOR(mddev->unit) >> shift;
4314 /* wait for any previous instance of this device to be
4315 * completely removed (mddev_delayed_delete).
4317 flush_workqueue(md_misc_wq);
4319 mutex_lock(&disks_mutex);
4320 error = -EEXIST;
4321 if (mddev->gendisk)
4322 goto abort;
4324 if (name) {
4325 /* Need to ensure that 'name' is not a duplicate.
4327 mddev_t *mddev2;
4328 spin_lock(&all_mddevs_lock);
4330 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
4331 if (mddev2->gendisk &&
4332 strcmp(mddev2->gendisk->disk_name, name) == 0) {
4333 spin_unlock(&all_mddevs_lock);
4334 goto abort;
4336 spin_unlock(&all_mddevs_lock);
4339 error = -ENOMEM;
4340 mddev->queue = blk_alloc_queue(GFP_KERNEL);
4341 if (!mddev->queue)
4342 goto abort;
4343 mddev->queue->queuedata = mddev;
4345 blk_queue_make_request(mddev->queue, md_make_request);
4347 disk = alloc_disk(1 << shift);
4348 if (!disk) {
4349 blk_cleanup_queue(mddev->queue);
4350 mddev->queue = NULL;
4351 goto abort;
4353 disk->major = MAJOR(mddev->unit);
4354 disk->first_minor = unit << shift;
4355 if (name)
4356 strcpy(disk->disk_name, name);
4357 else if (partitioned)
4358 sprintf(disk->disk_name, "md_d%d", unit);
4359 else
4360 sprintf(disk->disk_name, "md%d", unit);
4361 disk->fops = &md_fops;
4362 disk->private_data = mddev;
4363 disk->queue = mddev->queue;
4364 blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA);
4365 /* Allow extended partitions. This makes the
4366 * 'mdp' device redundant, but we can't really
4367 * remove it now.
4369 disk->flags |= GENHD_FL_EXT_DEVT;
4370 mddev->gendisk = disk;
4371 /* As soon as we call add_disk(), another thread could get
4372 * through to md_open, so make sure it doesn't get too far
4374 mutex_lock(&mddev->open_mutex);
4375 add_disk(disk);
4377 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
4378 &disk_to_dev(disk)->kobj, "%s", "md");
4379 if (error) {
4380 /* This isn't possible, but as kobject_init_and_add is marked
4381 * __must_check, we must do something with the result
4383 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
4384 disk->disk_name);
4385 error = 0;
4387 if (mddev->kobj.sd &&
4388 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
4389 printk(KERN_DEBUG "pointless warning\n");
4390 mutex_unlock(&mddev->open_mutex);
4391 abort:
4392 mutex_unlock(&disks_mutex);
4393 if (!error && mddev->kobj.sd) {
4394 kobject_uevent(&mddev->kobj, KOBJ_ADD);
4395 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
4397 mddev_put(mddev);
4398 return error;
4401 static struct kobject *md_probe(dev_t dev, int *part, void *data)
4403 md_alloc(dev, NULL);
4404 return NULL;
4407 static int add_named_array(const char *val, struct kernel_param *kp)
4409 /* val must be "md_*" where * is not all digits.
4410 * We allocate an array with a large free minor number, and
4411 * set the name to val. val must not already be an active name.
4413 int len = strlen(val);
4414 char buf[DISK_NAME_LEN];
4416 while (len && val[len-1] == '\n')
4417 len--;
4418 if (len >= DISK_NAME_LEN)
4419 return -E2BIG;
4420 strlcpy(buf, val, len+1);
4421 if (strncmp(buf, "md_", 3) != 0)
4422 return -EINVAL;
4423 return md_alloc(0, buf);
4426 static void md_safemode_timeout(unsigned long data)
4428 mddev_t *mddev = (mddev_t *) data;
4430 if (!atomic_read(&mddev->writes_pending)) {
4431 mddev->safemode = 1;
4432 if (mddev->external)
4433 sysfs_notify_dirent_safe(mddev->sysfs_state);
4435 md_wakeup_thread(mddev->thread);
4438 static int start_dirty_degraded;
4440 int md_run(mddev_t *mddev)
4442 int err;
4443 mdk_rdev_t *rdev;
4444 struct mdk_personality *pers;
4446 if (list_empty(&mddev->disks))
4447 /* cannot run an array with no devices.. */
4448 return -EINVAL;
4450 if (mddev->pers)
4451 return -EBUSY;
4452 /* Cannot run until previous stop completes properly */
4453 if (mddev->sysfs_active)
4454 return -EBUSY;
4457 * Analyze all RAID superblock(s)
4459 if (!mddev->raid_disks) {
4460 if (!mddev->persistent)
4461 return -EINVAL;
4462 analyze_sbs(mddev);
4465 if (mddev->level != LEVEL_NONE)
4466 request_module("md-level-%d", mddev->level);
4467 else if (mddev->clevel[0])
4468 request_module("md-%s", mddev->clevel);
4471 * Drop all container device buffers, from now on
4472 * the only valid external interface is through the md
4473 * device.
4475 list_for_each_entry(rdev, &mddev->disks, same_set) {
4476 if (test_bit(Faulty, &rdev->flags))
4477 continue;
4478 sync_blockdev(rdev->bdev);
4479 invalidate_bdev(rdev->bdev);
4481 /* perform some consistency tests on the device.
4482 * We don't want the data to overlap the metadata,
4483 * Internal Bitmap issues have been handled elsewhere.
4485 if (rdev->meta_bdev) {
4486 /* Nothing to check */;
4487 } else if (rdev->data_offset < rdev->sb_start) {
4488 if (mddev->dev_sectors &&
4489 rdev->data_offset + mddev->dev_sectors
4490 > rdev->sb_start) {
4491 printk("md: %s: data overlaps metadata\n",
4492 mdname(mddev));
4493 return -EINVAL;
4495 } else {
4496 if (rdev->sb_start + rdev->sb_size/512
4497 > rdev->data_offset) {
4498 printk("md: %s: metadata overlaps data\n",
4499 mdname(mddev));
4500 return -EINVAL;
4503 sysfs_notify_dirent_safe(rdev->sysfs_state);
4506 if (mddev->bio_set == NULL)
4507 mddev->bio_set = bioset_create(BIO_POOL_SIZE, sizeof(mddev));
4509 spin_lock(&pers_lock);
4510 pers = find_pers(mddev->level, mddev->clevel);
4511 if (!pers || !try_module_get(pers->owner)) {
4512 spin_unlock(&pers_lock);
4513 if (mddev->level != LEVEL_NONE)
4514 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
4515 mddev->level);
4516 else
4517 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
4518 mddev->clevel);
4519 return -EINVAL;
4521 mddev->pers = pers;
4522 spin_unlock(&pers_lock);
4523 if (mddev->level != pers->level) {
4524 mddev->level = pers->level;
4525 mddev->new_level = pers->level;
4527 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
4529 if (mddev->reshape_position != MaxSector &&
4530 pers->start_reshape == NULL) {
4531 /* This personality cannot handle reshaping... */
4532 mddev->pers = NULL;
4533 module_put(pers->owner);
4534 return -EINVAL;
4537 if (pers->sync_request) {
4538 /* Warn if this is a potentially silly
4539 * configuration.
4541 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4542 mdk_rdev_t *rdev2;
4543 int warned = 0;
4545 list_for_each_entry(rdev, &mddev->disks, same_set)
4546 list_for_each_entry(rdev2, &mddev->disks, same_set) {
4547 if (rdev < rdev2 &&
4548 rdev->bdev->bd_contains ==
4549 rdev2->bdev->bd_contains) {
4550 printk(KERN_WARNING
4551 "%s: WARNING: %s appears to be"
4552 " on the same physical disk as"
4553 " %s.\n",
4554 mdname(mddev),
4555 bdevname(rdev->bdev,b),
4556 bdevname(rdev2->bdev,b2));
4557 warned = 1;
4561 if (warned)
4562 printk(KERN_WARNING
4563 "True protection against single-disk"
4564 " failure might be compromised.\n");
4567 mddev->recovery = 0;
4568 /* may be over-ridden by personality */
4569 mddev->resync_max_sectors = mddev->dev_sectors;
4571 mddev->ok_start_degraded = start_dirty_degraded;
4573 if (start_readonly && mddev->ro == 0)
4574 mddev->ro = 2; /* read-only, but switch on first write */
4576 err = mddev->pers->run(mddev);
4577 if (err)
4578 printk(KERN_ERR "md: pers->run() failed ...\n");
4579 else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
4580 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
4581 " but 'external_size' not in effect?\n", __func__);
4582 printk(KERN_ERR
4583 "md: invalid array_size %llu > default size %llu\n",
4584 (unsigned long long)mddev->array_sectors / 2,
4585 (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
4586 err = -EINVAL;
4587 mddev->pers->stop(mddev);
4589 if (err == 0 && mddev->pers->sync_request) {
4590 err = bitmap_create(mddev);
4591 if (err) {
4592 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
4593 mdname(mddev), err);
4594 mddev->pers->stop(mddev);
4597 if (err) {
4598 module_put(mddev->pers->owner);
4599 mddev->pers = NULL;
4600 bitmap_destroy(mddev);
4601 return err;
4603 if (mddev->pers->sync_request) {
4604 if (mddev->kobj.sd &&
4605 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
4606 printk(KERN_WARNING
4607 "md: cannot register extra attributes for %s\n",
4608 mdname(mddev));
4609 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
4610 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
4611 mddev->ro = 0;
4613 atomic_set(&mddev->writes_pending,0);
4614 atomic_set(&mddev->max_corr_read_errors,
4615 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
4616 mddev->safemode = 0;
4617 mddev->safemode_timer.function = md_safemode_timeout;
4618 mddev->safemode_timer.data = (unsigned long) mddev;
4619 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
4620 mddev->in_sync = 1;
4621 smp_wmb();
4622 mddev->ready = 1;
4623 list_for_each_entry(rdev, &mddev->disks, same_set)
4624 if (rdev->raid_disk >= 0) {
4625 char nm[20];
4626 sprintf(nm, "rd%d", rdev->raid_disk);
4627 if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
4628 /* failure here is OK */;
4631 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4633 if (mddev->flags)
4634 md_update_sb(mddev, 0);
4636 md_new_event(mddev);
4637 sysfs_notify_dirent_safe(mddev->sysfs_state);
4638 sysfs_notify_dirent_safe(mddev->sysfs_action);
4639 sysfs_notify(&mddev->kobj, NULL, "degraded");
4640 return 0;
4642 EXPORT_SYMBOL_GPL(md_run);
4644 static int do_md_run(mddev_t *mddev)
4646 int err;
4648 err = md_run(mddev);
4649 if (err)
4650 goto out;
4651 err = bitmap_load(mddev);
4652 if (err) {
4653 bitmap_destroy(mddev);
4654 goto out;
4657 md_wakeup_thread(mddev->thread);
4658 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
4660 set_capacity(mddev->gendisk, mddev->array_sectors);
4661 revalidate_disk(mddev->gendisk);
4662 mddev->changed = 1;
4663 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
4664 out:
4665 return err;
4668 static int restart_array(mddev_t *mddev)
4670 struct gendisk *disk = mddev->gendisk;
4672 /* Complain if it has no devices */
4673 if (list_empty(&mddev->disks))
4674 return -ENXIO;
4675 if (!mddev->pers)
4676 return -EINVAL;
4677 if (!mddev->ro)
4678 return -EBUSY;
4679 mddev->safemode = 0;
4680 mddev->ro = 0;
4681 set_disk_ro(disk, 0);
4682 printk(KERN_INFO "md: %s switched to read-write mode.\n",
4683 mdname(mddev));
4684 /* Kick recovery or resync if necessary */
4685 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4686 md_wakeup_thread(mddev->thread);
4687 md_wakeup_thread(mddev->sync_thread);
4688 sysfs_notify_dirent_safe(mddev->sysfs_state);
4689 return 0;
4692 /* similar to deny_write_access, but accounts for our holding a reference
4693 * to the file ourselves */
4694 static int deny_bitmap_write_access(struct file * file)
4696 struct inode *inode = file->f_mapping->host;
4698 spin_lock(&inode->i_lock);
4699 if (atomic_read(&inode->i_writecount) > 1) {
4700 spin_unlock(&inode->i_lock);
4701 return -ETXTBSY;
4703 atomic_set(&inode->i_writecount, -1);
4704 spin_unlock(&inode->i_lock);
4706 return 0;
4709 void restore_bitmap_write_access(struct file *file)
4711 struct inode *inode = file->f_mapping->host;
4713 spin_lock(&inode->i_lock);
4714 atomic_set(&inode->i_writecount, 1);
4715 spin_unlock(&inode->i_lock);
4718 static void md_clean(mddev_t *mddev)
4720 mddev->array_sectors = 0;
4721 mddev->external_size = 0;
4722 mddev->dev_sectors = 0;
4723 mddev->raid_disks = 0;
4724 mddev->recovery_cp = 0;
4725 mddev->resync_min = 0;
4726 mddev->resync_max = MaxSector;
4727 mddev->reshape_position = MaxSector;
4728 mddev->external = 0;
4729 mddev->persistent = 0;
4730 mddev->level = LEVEL_NONE;
4731 mddev->clevel[0] = 0;
4732 mddev->flags = 0;
4733 mddev->ro = 0;
4734 mddev->metadata_type[0] = 0;
4735 mddev->chunk_sectors = 0;
4736 mddev->ctime = mddev->utime = 0;
4737 mddev->layout = 0;
4738 mddev->max_disks = 0;
4739 mddev->events = 0;
4740 mddev->can_decrease_events = 0;
4741 mddev->delta_disks = 0;
4742 mddev->new_level = LEVEL_NONE;
4743 mddev->new_layout = 0;
4744 mddev->new_chunk_sectors = 0;
4745 mddev->curr_resync = 0;
4746 mddev->resync_mismatches = 0;
4747 mddev->suspend_lo = mddev->suspend_hi = 0;
4748 mddev->sync_speed_min = mddev->sync_speed_max = 0;
4749 mddev->recovery = 0;
4750 mddev->in_sync = 0;
4751 mddev->changed = 0;
4752 mddev->degraded = 0;
4753 mddev->safemode = 0;
4754 mddev->bitmap_info.offset = 0;
4755 mddev->bitmap_info.default_offset = 0;
4756 mddev->bitmap_info.chunksize = 0;
4757 mddev->bitmap_info.daemon_sleep = 0;
4758 mddev->bitmap_info.max_write_behind = 0;
4761 static void __md_stop_writes(mddev_t *mddev)
4763 if (mddev->sync_thread) {
4764 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4765 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4766 reap_sync_thread(mddev);
4769 del_timer_sync(&mddev->safemode_timer);
4771 bitmap_flush(mddev);
4772 md_super_wait(mddev);
4774 if (!mddev->in_sync || mddev->flags) {
4775 /* mark array as shutdown cleanly */
4776 mddev->in_sync = 1;
4777 md_update_sb(mddev, 1);
4781 void md_stop_writes(mddev_t *mddev)
4783 mddev_lock(mddev);
4784 __md_stop_writes(mddev);
4785 mddev_unlock(mddev);
4787 EXPORT_SYMBOL_GPL(md_stop_writes);
4789 void md_stop(mddev_t *mddev)
4791 mddev->ready = 0;
4792 mddev->pers->stop(mddev);
4793 if (mddev->pers->sync_request && mddev->to_remove == NULL)
4794 mddev->to_remove = &md_redundancy_group;
4795 module_put(mddev->pers->owner);
4796 mddev->pers = NULL;
4797 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4799 EXPORT_SYMBOL_GPL(md_stop);
4801 static int md_set_readonly(mddev_t *mddev, int is_open)
4803 int err = 0;
4804 mutex_lock(&mddev->open_mutex);
4805 if (atomic_read(&mddev->openers) > is_open) {
4806 printk("md: %s still in use.\n",mdname(mddev));
4807 err = -EBUSY;
4808 goto out;
4810 if (mddev->pers) {
4811 __md_stop_writes(mddev);
4813 err = -ENXIO;
4814 if (mddev->ro==1)
4815 goto out;
4816 mddev->ro = 1;
4817 set_disk_ro(mddev->gendisk, 1);
4818 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4819 sysfs_notify_dirent_safe(mddev->sysfs_state);
4820 err = 0;
4822 out:
4823 mutex_unlock(&mddev->open_mutex);
4824 return err;
4827 /* mode:
4828 * 0 - completely stop and dis-assemble array
4829 * 2 - stop but do not disassemble array
4831 static int do_md_stop(mddev_t * mddev, int mode, int is_open)
4833 struct gendisk *disk = mddev->gendisk;
4834 mdk_rdev_t *rdev;
4836 mutex_lock(&mddev->open_mutex);
4837 if (atomic_read(&mddev->openers) > is_open ||
4838 mddev->sysfs_active) {
4839 printk("md: %s still in use.\n",mdname(mddev));
4840 mutex_unlock(&mddev->open_mutex);
4841 return -EBUSY;
4844 if (mddev->pers) {
4845 if (mddev->ro)
4846 set_disk_ro(disk, 0);
4848 __md_stop_writes(mddev);
4849 md_stop(mddev);
4850 mddev->queue->merge_bvec_fn = NULL;
4851 mddev->queue->backing_dev_info.congested_fn = NULL;
4853 /* tell userspace to handle 'inactive' */
4854 sysfs_notify_dirent_safe(mddev->sysfs_state);
4856 list_for_each_entry(rdev, &mddev->disks, same_set)
4857 if (rdev->raid_disk >= 0) {
4858 char nm[20];
4859 sprintf(nm, "rd%d", rdev->raid_disk);
4860 sysfs_remove_link(&mddev->kobj, nm);
4863 set_capacity(disk, 0);
4864 mutex_unlock(&mddev->open_mutex);
4865 mddev->changed = 1;
4866 revalidate_disk(disk);
4868 if (mddev->ro)
4869 mddev->ro = 0;
4870 } else
4871 mutex_unlock(&mddev->open_mutex);
4873 * Free resources if final stop
4875 if (mode == 0) {
4876 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
4878 bitmap_destroy(mddev);
4879 if (mddev->bitmap_info.file) {
4880 restore_bitmap_write_access(mddev->bitmap_info.file);
4881 fput(mddev->bitmap_info.file);
4882 mddev->bitmap_info.file = NULL;
4884 mddev->bitmap_info.offset = 0;
4886 export_array(mddev);
4888 md_clean(mddev);
4889 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
4890 if (mddev->hold_active == UNTIL_STOP)
4891 mddev->hold_active = 0;
4893 blk_integrity_unregister(disk);
4894 md_new_event(mddev);
4895 sysfs_notify_dirent_safe(mddev->sysfs_state);
4896 return 0;
4899 #ifndef MODULE
4900 static void autorun_array(mddev_t *mddev)
4902 mdk_rdev_t *rdev;
4903 int err;
4905 if (list_empty(&mddev->disks))
4906 return;
4908 printk(KERN_INFO "md: running: ");
4910 list_for_each_entry(rdev, &mddev->disks, same_set) {
4911 char b[BDEVNAME_SIZE];
4912 printk("<%s>", bdevname(rdev->bdev,b));
4914 printk("\n");
4916 err = do_md_run(mddev);
4917 if (err) {
4918 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
4919 do_md_stop(mddev, 0, 0);
4924 * lets try to run arrays based on all disks that have arrived
4925 * until now. (those are in pending_raid_disks)
4927 * the method: pick the first pending disk, collect all disks with
4928 * the same UUID, remove all from the pending list and put them into
4929 * the 'same_array' list. Then order this list based on superblock
4930 * update time (freshest comes first), kick out 'old' disks and
4931 * compare superblocks. If everything's fine then run it.
4933 * If "unit" is allocated, then bump its reference count
4935 static void autorun_devices(int part)
4937 mdk_rdev_t *rdev0, *rdev, *tmp;
4938 mddev_t *mddev;
4939 char b[BDEVNAME_SIZE];
4941 printk(KERN_INFO "md: autorun ...\n");
4942 while (!list_empty(&pending_raid_disks)) {
4943 int unit;
4944 dev_t dev;
4945 LIST_HEAD(candidates);
4946 rdev0 = list_entry(pending_raid_disks.next,
4947 mdk_rdev_t, same_set);
4949 printk(KERN_INFO "md: considering %s ...\n",
4950 bdevname(rdev0->bdev,b));
4951 INIT_LIST_HEAD(&candidates);
4952 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
4953 if (super_90_load(rdev, rdev0, 0) >= 0) {
4954 printk(KERN_INFO "md: adding %s ...\n",
4955 bdevname(rdev->bdev,b));
4956 list_move(&rdev->same_set, &candidates);
4959 * now we have a set of devices, with all of them having
4960 * mostly sane superblocks. It's time to allocate the
4961 * mddev.
4963 if (part) {
4964 dev = MKDEV(mdp_major,
4965 rdev0->preferred_minor << MdpMinorShift);
4966 unit = MINOR(dev) >> MdpMinorShift;
4967 } else {
4968 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
4969 unit = MINOR(dev);
4971 if (rdev0->preferred_minor != unit) {
4972 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
4973 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
4974 break;
4977 md_probe(dev, NULL, NULL);
4978 mddev = mddev_find(dev);
4979 if (!mddev || !mddev->gendisk) {
4980 if (mddev)
4981 mddev_put(mddev);
4982 printk(KERN_ERR
4983 "md: cannot allocate memory for md drive.\n");
4984 break;
4986 if (mddev_lock(mddev))
4987 printk(KERN_WARNING "md: %s locked, cannot run\n",
4988 mdname(mddev));
4989 else if (mddev->raid_disks || mddev->major_version
4990 || !list_empty(&mddev->disks)) {
4991 printk(KERN_WARNING
4992 "md: %s already running, cannot run %s\n",
4993 mdname(mddev), bdevname(rdev0->bdev,b));
4994 mddev_unlock(mddev);
4995 } else {
4996 printk(KERN_INFO "md: created %s\n", mdname(mddev));
4997 mddev->persistent = 1;
4998 rdev_for_each_list(rdev, tmp, &candidates) {
4999 list_del_init(&rdev->same_set);
5000 if (bind_rdev_to_array(rdev, mddev))
5001 export_rdev(rdev);
5003 autorun_array(mddev);
5004 mddev_unlock(mddev);
5006 /* on success, candidates will be empty, on error
5007 * it won't...
5009 rdev_for_each_list(rdev, tmp, &candidates) {
5010 list_del_init(&rdev->same_set);
5011 export_rdev(rdev);
5013 mddev_put(mddev);
5015 printk(KERN_INFO "md: ... autorun DONE.\n");
5017 #endif /* !MODULE */
5019 static int get_version(void __user * arg)
5021 mdu_version_t ver;
5023 ver.major = MD_MAJOR_VERSION;
5024 ver.minor = MD_MINOR_VERSION;
5025 ver.patchlevel = MD_PATCHLEVEL_VERSION;
5027 if (copy_to_user(arg, &ver, sizeof(ver)))
5028 return -EFAULT;
5030 return 0;
5033 static int get_array_info(mddev_t * mddev, void __user * arg)
5035 mdu_array_info_t info;
5036 int nr,working,insync,failed,spare;
5037 mdk_rdev_t *rdev;
5039 nr=working=insync=failed=spare=0;
5040 list_for_each_entry(rdev, &mddev->disks, same_set) {
5041 nr++;
5042 if (test_bit(Faulty, &rdev->flags))
5043 failed++;
5044 else {
5045 working++;
5046 if (test_bit(In_sync, &rdev->flags))
5047 insync++;
5048 else
5049 spare++;
5053 info.major_version = mddev->major_version;
5054 info.minor_version = mddev->minor_version;
5055 info.patch_version = MD_PATCHLEVEL_VERSION;
5056 info.ctime = mddev->ctime;
5057 info.level = mddev->level;
5058 info.size = mddev->dev_sectors / 2;
5059 if (info.size != mddev->dev_sectors / 2) /* overflow */
5060 info.size = -1;
5061 info.nr_disks = nr;
5062 info.raid_disks = mddev->raid_disks;
5063 info.md_minor = mddev->md_minor;
5064 info.not_persistent= !mddev->persistent;
5066 info.utime = mddev->utime;
5067 info.state = 0;
5068 if (mddev->in_sync)
5069 info.state = (1<<MD_SB_CLEAN);
5070 if (mddev->bitmap && mddev->bitmap_info.offset)
5071 info.state = (1<<MD_SB_BITMAP_PRESENT);
5072 info.active_disks = insync;
5073 info.working_disks = working;
5074 info.failed_disks = failed;
5075 info.spare_disks = spare;
5077 info.layout = mddev->layout;
5078 info.chunk_size = mddev->chunk_sectors << 9;
5080 if (copy_to_user(arg, &info, sizeof(info)))
5081 return -EFAULT;
5083 return 0;
5086 static int get_bitmap_file(mddev_t * mddev, void __user * arg)
5088 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
5089 char *ptr, *buf = NULL;
5090 int err = -ENOMEM;
5092 if (md_allow_write(mddev))
5093 file = kmalloc(sizeof(*file), GFP_NOIO);
5094 else
5095 file = kmalloc(sizeof(*file), GFP_KERNEL);
5097 if (!file)
5098 goto out;
5100 /* bitmap disabled, zero the first byte and copy out */
5101 if (!mddev->bitmap || !mddev->bitmap->file) {
5102 file->pathname[0] = '\0';
5103 goto copy_out;
5106 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
5107 if (!buf)
5108 goto out;
5110 ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname));
5111 if (IS_ERR(ptr))
5112 goto out;
5114 strcpy(file->pathname, ptr);
5116 copy_out:
5117 err = 0;
5118 if (copy_to_user(arg, file, sizeof(*file)))
5119 err = -EFAULT;
5120 out:
5121 kfree(buf);
5122 kfree(file);
5123 return err;
5126 static int get_disk_info(mddev_t * mddev, void __user * arg)
5128 mdu_disk_info_t info;
5129 mdk_rdev_t *rdev;
5131 if (copy_from_user(&info, arg, sizeof(info)))
5132 return -EFAULT;
5134 rdev = find_rdev_nr(mddev, info.number);
5135 if (rdev) {
5136 info.major = MAJOR(rdev->bdev->bd_dev);
5137 info.minor = MINOR(rdev->bdev->bd_dev);
5138 info.raid_disk = rdev->raid_disk;
5139 info.state = 0;
5140 if (test_bit(Faulty, &rdev->flags))
5141 info.state |= (1<<MD_DISK_FAULTY);
5142 else if (test_bit(In_sync, &rdev->flags)) {
5143 info.state |= (1<<MD_DISK_ACTIVE);
5144 info.state |= (1<<MD_DISK_SYNC);
5146 if (test_bit(WriteMostly, &rdev->flags))
5147 info.state |= (1<<MD_DISK_WRITEMOSTLY);
5148 } else {
5149 info.major = info.minor = 0;
5150 info.raid_disk = -1;
5151 info.state = (1<<MD_DISK_REMOVED);
5154 if (copy_to_user(arg, &info, sizeof(info)))
5155 return -EFAULT;
5157 return 0;
5160 static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
5162 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
5163 mdk_rdev_t *rdev;
5164 dev_t dev = MKDEV(info->major,info->minor);
5166 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
5167 return -EOVERFLOW;
5169 if (!mddev->raid_disks) {
5170 int err;
5171 /* expecting a device which has a superblock */
5172 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
5173 if (IS_ERR(rdev)) {
5174 printk(KERN_WARNING
5175 "md: md_import_device returned %ld\n",
5176 PTR_ERR(rdev));
5177 return PTR_ERR(rdev);
5179 if (!list_empty(&mddev->disks)) {
5180 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
5181 mdk_rdev_t, same_set);
5182 err = super_types[mddev->major_version]
5183 .load_super(rdev, rdev0, mddev->minor_version);
5184 if (err < 0) {
5185 printk(KERN_WARNING
5186 "md: %s has different UUID to %s\n",
5187 bdevname(rdev->bdev,b),
5188 bdevname(rdev0->bdev,b2));
5189 export_rdev(rdev);
5190 return -EINVAL;
5193 err = bind_rdev_to_array(rdev, mddev);
5194 if (err)
5195 export_rdev(rdev);
5196 return err;
5200 * add_new_disk can be used once the array is assembled
5201 * to add "hot spares". They must already have a superblock
5202 * written
5204 if (mddev->pers) {
5205 int err;
5206 if (!mddev->pers->hot_add_disk) {
5207 printk(KERN_WARNING
5208 "%s: personality does not support diskops!\n",
5209 mdname(mddev));
5210 return -EINVAL;
5212 if (mddev->persistent)
5213 rdev = md_import_device(dev, mddev->major_version,
5214 mddev->minor_version);
5215 else
5216 rdev = md_import_device(dev, -1, -1);
5217 if (IS_ERR(rdev)) {
5218 printk(KERN_WARNING
5219 "md: md_import_device returned %ld\n",
5220 PTR_ERR(rdev));
5221 return PTR_ERR(rdev);
5223 /* set saved_raid_disk if appropriate */
5224 if (!mddev->persistent) {
5225 if (info->state & (1<<MD_DISK_SYNC) &&
5226 info->raid_disk < mddev->raid_disks) {
5227 rdev->raid_disk = info->raid_disk;
5228 set_bit(In_sync, &rdev->flags);
5229 } else
5230 rdev->raid_disk = -1;
5231 } else
5232 super_types[mddev->major_version].
5233 validate_super(mddev, rdev);
5234 if ((info->state & (1<<MD_DISK_SYNC)) &&
5235 (!test_bit(In_sync, &rdev->flags) ||
5236 rdev->raid_disk != info->raid_disk)) {
5237 /* This was a hot-add request, but events doesn't
5238 * match, so reject it.
5240 export_rdev(rdev);
5241 return -EINVAL;
5244 if (test_bit(In_sync, &rdev->flags))
5245 rdev->saved_raid_disk = rdev->raid_disk;
5246 else
5247 rdev->saved_raid_disk = -1;
5249 clear_bit(In_sync, &rdev->flags); /* just to be sure */
5250 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5251 set_bit(WriteMostly, &rdev->flags);
5252 else
5253 clear_bit(WriteMostly, &rdev->flags);
5255 rdev->raid_disk = -1;
5256 err = bind_rdev_to_array(rdev, mddev);
5257 if (!err && !mddev->pers->hot_remove_disk) {
5258 /* If there is hot_add_disk but no hot_remove_disk
5259 * then added disks for geometry changes,
5260 * and should be added immediately.
5262 super_types[mddev->major_version].
5263 validate_super(mddev, rdev);
5264 err = mddev->pers->hot_add_disk(mddev, rdev);
5265 if (err)
5266 unbind_rdev_from_array(rdev);
5268 if (err)
5269 export_rdev(rdev);
5270 else
5271 sysfs_notify_dirent_safe(rdev->sysfs_state);
5273 md_update_sb(mddev, 1);
5274 if (mddev->degraded)
5275 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
5276 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5277 if (!err)
5278 md_new_event(mddev);
5279 md_wakeup_thread(mddev->thread);
5280 return err;
5283 /* otherwise, add_new_disk is only allowed
5284 * for major_version==0 superblocks
5286 if (mddev->major_version != 0) {
5287 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
5288 mdname(mddev));
5289 return -EINVAL;
5292 if (!(info->state & (1<<MD_DISK_FAULTY))) {
5293 int err;
5294 rdev = md_import_device(dev, -1, 0);
5295 if (IS_ERR(rdev)) {
5296 printk(KERN_WARNING
5297 "md: error, md_import_device() returned %ld\n",
5298 PTR_ERR(rdev));
5299 return PTR_ERR(rdev);
5301 rdev->desc_nr = info->number;
5302 if (info->raid_disk < mddev->raid_disks)
5303 rdev->raid_disk = info->raid_disk;
5304 else
5305 rdev->raid_disk = -1;
5307 if (rdev->raid_disk < mddev->raid_disks)
5308 if (info->state & (1<<MD_DISK_SYNC))
5309 set_bit(In_sync, &rdev->flags);
5311 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5312 set_bit(WriteMostly, &rdev->flags);
5314 if (!mddev->persistent) {
5315 printk(KERN_INFO "md: nonpersistent superblock ...\n");
5316 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
5317 } else
5318 rdev->sb_start = calc_dev_sboffset(rdev);
5319 rdev->sectors = rdev->sb_start;
5321 err = bind_rdev_to_array(rdev, mddev);
5322 if (err) {
5323 export_rdev(rdev);
5324 return err;
5328 return 0;
5331 static int hot_remove_disk(mddev_t * mddev, dev_t dev)
5333 char b[BDEVNAME_SIZE];
5334 mdk_rdev_t *rdev;
5336 rdev = find_rdev(mddev, dev);
5337 if (!rdev)
5338 return -ENXIO;
5340 if (rdev->raid_disk >= 0)
5341 goto busy;
5343 kick_rdev_from_array(rdev);
5344 md_update_sb(mddev, 1);
5345 md_new_event(mddev);
5347 return 0;
5348 busy:
5349 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
5350 bdevname(rdev->bdev,b), mdname(mddev));
5351 return -EBUSY;
5354 static int hot_add_disk(mddev_t * mddev, dev_t dev)
5356 char b[BDEVNAME_SIZE];
5357 int err;
5358 mdk_rdev_t *rdev;
5360 if (!mddev->pers)
5361 return -ENODEV;
5363 if (mddev->major_version != 0) {
5364 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
5365 " version-0 superblocks.\n",
5366 mdname(mddev));
5367 return -EINVAL;
5369 if (!mddev->pers->hot_add_disk) {
5370 printk(KERN_WARNING
5371 "%s: personality does not support diskops!\n",
5372 mdname(mddev));
5373 return -EINVAL;
5376 rdev = md_import_device(dev, -1, 0);
5377 if (IS_ERR(rdev)) {
5378 printk(KERN_WARNING
5379 "md: error, md_import_device() returned %ld\n",
5380 PTR_ERR(rdev));
5381 return -EINVAL;
5384 if (mddev->persistent)
5385 rdev->sb_start = calc_dev_sboffset(rdev);
5386 else
5387 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
5389 rdev->sectors = rdev->sb_start;
5391 if (test_bit(Faulty, &rdev->flags)) {
5392 printk(KERN_WARNING
5393 "md: can not hot-add faulty %s disk to %s!\n",
5394 bdevname(rdev->bdev,b), mdname(mddev));
5395 err = -EINVAL;
5396 goto abort_export;
5398 clear_bit(In_sync, &rdev->flags);
5399 rdev->desc_nr = -1;
5400 rdev->saved_raid_disk = -1;
5401 err = bind_rdev_to_array(rdev, mddev);
5402 if (err)
5403 goto abort_export;
5406 * The rest should better be atomic, we can have disk failures
5407 * noticed in interrupt contexts ...
5410 rdev->raid_disk = -1;
5412 md_update_sb(mddev, 1);
5415 * Kick recovery, maybe this spare has to be added to the
5416 * array immediately.
5418 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5419 md_wakeup_thread(mddev->thread);
5420 md_new_event(mddev);
5421 return 0;
5423 abort_export:
5424 export_rdev(rdev);
5425 return err;
5428 static int set_bitmap_file(mddev_t *mddev, int fd)
5430 int err;
5432 if (mddev->pers) {
5433 if (!mddev->pers->quiesce)
5434 return -EBUSY;
5435 if (mddev->recovery || mddev->sync_thread)
5436 return -EBUSY;
5437 /* we should be able to change the bitmap.. */
5441 if (fd >= 0) {
5442 if (mddev->bitmap)
5443 return -EEXIST; /* cannot add when bitmap is present */
5444 mddev->bitmap_info.file = fget(fd);
5446 if (mddev->bitmap_info.file == NULL) {
5447 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
5448 mdname(mddev));
5449 return -EBADF;
5452 err = deny_bitmap_write_access(mddev->bitmap_info.file);
5453 if (err) {
5454 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
5455 mdname(mddev));
5456 fput(mddev->bitmap_info.file);
5457 mddev->bitmap_info.file = NULL;
5458 return err;
5460 mddev->bitmap_info.offset = 0; /* file overrides offset */
5461 } else if (mddev->bitmap == NULL)
5462 return -ENOENT; /* cannot remove what isn't there */
5463 err = 0;
5464 if (mddev->pers) {
5465 mddev->pers->quiesce(mddev, 1);
5466 if (fd >= 0) {
5467 err = bitmap_create(mddev);
5468 if (!err)
5469 err = bitmap_load(mddev);
5471 if (fd < 0 || err) {
5472 bitmap_destroy(mddev);
5473 fd = -1; /* make sure to put the file */
5475 mddev->pers->quiesce(mddev, 0);
5477 if (fd < 0) {
5478 if (mddev->bitmap_info.file) {
5479 restore_bitmap_write_access(mddev->bitmap_info.file);
5480 fput(mddev->bitmap_info.file);
5482 mddev->bitmap_info.file = NULL;
5485 return err;
5489 * set_array_info is used two different ways
5490 * The original usage is when creating a new array.
5491 * In this usage, raid_disks is > 0 and it together with
5492 * level, size, not_persistent,layout,chunksize determine the
5493 * shape of the array.
5494 * This will always create an array with a type-0.90.0 superblock.
5495 * The newer usage is when assembling an array.
5496 * In this case raid_disks will be 0, and the major_version field is
5497 * use to determine which style super-blocks are to be found on the devices.
5498 * The minor and patch _version numbers are also kept incase the
5499 * super_block handler wishes to interpret them.
5501 static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
5504 if (info->raid_disks == 0) {
5505 /* just setting version number for superblock loading */
5506 if (info->major_version < 0 ||
5507 info->major_version >= ARRAY_SIZE(super_types) ||
5508 super_types[info->major_version].name == NULL) {
5509 /* maybe try to auto-load a module? */
5510 printk(KERN_INFO
5511 "md: superblock version %d not known\n",
5512 info->major_version);
5513 return -EINVAL;
5515 mddev->major_version = info->major_version;
5516 mddev->minor_version = info->minor_version;
5517 mddev->patch_version = info->patch_version;
5518 mddev->persistent = !info->not_persistent;
5519 /* ensure mddev_put doesn't delete this now that there
5520 * is some minimal configuration.
5522 mddev->ctime = get_seconds();
5523 return 0;
5525 mddev->major_version = MD_MAJOR_VERSION;
5526 mddev->minor_version = MD_MINOR_VERSION;
5527 mddev->patch_version = MD_PATCHLEVEL_VERSION;
5528 mddev->ctime = get_seconds();
5530 mddev->level = info->level;
5531 mddev->clevel[0] = 0;
5532 mddev->dev_sectors = 2 * (sector_t)info->size;
5533 mddev->raid_disks = info->raid_disks;
5534 /* don't set md_minor, it is determined by which /dev/md* was
5535 * openned
5537 if (info->state & (1<<MD_SB_CLEAN))
5538 mddev->recovery_cp = MaxSector;
5539 else
5540 mddev->recovery_cp = 0;
5541 mddev->persistent = ! info->not_persistent;
5542 mddev->external = 0;
5544 mddev->layout = info->layout;
5545 mddev->chunk_sectors = info->chunk_size >> 9;
5547 mddev->max_disks = MD_SB_DISKS;
5549 if (mddev->persistent)
5550 mddev->flags = 0;
5551 set_bit(MD_CHANGE_DEVS, &mddev->flags);
5553 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
5554 mddev->bitmap_info.offset = 0;
5556 mddev->reshape_position = MaxSector;
5559 * Generate a 128 bit UUID
5561 get_random_bytes(mddev->uuid, 16);
5563 mddev->new_level = mddev->level;
5564 mddev->new_chunk_sectors = mddev->chunk_sectors;
5565 mddev->new_layout = mddev->layout;
5566 mddev->delta_disks = 0;
5568 return 0;
5571 void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors)
5573 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
5575 if (mddev->external_size)
5576 return;
5578 mddev->array_sectors = array_sectors;
5580 EXPORT_SYMBOL(md_set_array_sectors);
5582 static int update_size(mddev_t *mddev, sector_t num_sectors)
5584 mdk_rdev_t *rdev;
5585 int rv;
5586 int fit = (num_sectors == 0);
5588 if (mddev->pers->resize == NULL)
5589 return -EINVAL;
5590 /* The "num_sectors" is the number of sectors of each device that
5591 * is used. This can only make sense for arrays with redundancy.
5592 * linear and raid0 always use whatever space is available. We can only
5593 * consider changing this number if no resync or reconstruction is
5594 * happening, and if the new size is acceptable. It must fit before the
5595 * sb_start or, if that is <data_offset, it must fit before the size
5596 * of each device. If num_sectors is zero, we find the largest size
5597 * that fits.
5599 if (mddev->sync_thread)
5600 return -EBUSY;
5601 if (mddev->bitmap)
5602 /* Sorry, cannot grow a bitmap yet, just remove it,
5603 * grow, and re-add.
5605 return -EBUSY;
5606 list_for_each_entry(rdev, &mddev->disks, same_set) {
5607 sector_t avail = rdev->sectors;
5609 if (fit && (num_sectors == 0 || num_sectors > avail))
5610 num_sectors = avail;
5611 if (avail < num_sectors)
5612 return -ENOSPC;
5614 rv = mddev->pers->resize(mddev, num_sectors);
5615 if (!rv)
5616 revalidate_disk(mddev->gendisk);
5617 return rv;
5620 static int update_raid_disks(mddev_t *mddev, int raid_disks)
5622 int rv;
5623 /* change the number of raid disks */
5624 if (mddev->pers->check_reshape == NULL)
5625 return -EINVAL;
5626 if (raid_disks <= 0 ||
5627 (mddev->max_disks && raid_disks >= mddev->max_disks))
5628 return -EINVAL;
5629 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
5630 return -EBUSY;
5631 mddev->delta_disks = raid_disks - mddev->raid_disks;
5633 rv = mddev->pers->check_reshape(mddev);
5634 if (rv < 0)
5635 mddev->delta_disks = 0;
5636 return rv;
5641 * update_array_info is used to change the configuration of an
5642 * on-line array.
5643 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
5644 * fields in the info are checked against the array.
5645 * Any differences that cannot be handled will cause an error.
5646 * Normally, only one change can be managed at a time.
5648 static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
5650 int rv = 0;
5651 int cnt = 0;
5652 int state = 0;
5654 /* calculate expected state,ignoring low bits */
5655 if (mddev->bitmap && mddev->bitmap_info.offset)
5656 state |= (1 << MD_SB_BITMAP_PRESENT);
5658 if (mddev->major_version != info->major_version ||
5659 mddev->minor_version != info->minor_version ||
5660 /* mddev->patch_version != info->patch_version || */
5661 mddev->ctime != info->ctime ||
5662 mddev->level != info->level ||
5663 /* mddev->layout != info->layout || */
5664 !mddev->persistent != info->not_persistent||
5665 mddev->chunk_sectors != info->chunk_size >> 9 ||
5666 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
5667 ((state^info->state) & 0xfffffe00)
5669 return -EINVAL;
5670 /* Check there is only one change */
5671 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
5672 cnt++;
5673 if (mddev->raid_disks != info->raid_disks)
5674 cnt++;
5675 if (mddev->layout != info->layout)
5676 cnt++;
5677 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
5678 cnt++;
5679 if (cnt == 0)
5680 return 0;
5681 if (cnt > 1)
5682 return -EINVAL;
5684 if (mddev->layout != info->layout) {
5685 /* Change layout
5686 * we don't need to do anything at the md level, the
5687 * personality will take care of it all.
5689 if (mddev->pers->check_reshape == NULL)
5690 return -EINVAL;
5691 else {
5692 mddev->new_layout = info->layout;
5693 rv = mddev->pers->check_reshape(mddev);
5694 if (rv)
5695 mddev->new_layout = mddev->layout;
5696 return rv;
5699 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
5700 rv = update_size(mddev, (sector_t)info->size * 2);
5702 if (mddev->raid_disks != info->raid_disks)
5703 rv = update_raid_disks(mddev, info->raid_disks);
5705 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
5706 if (mddev->pers->quiesce == NULL)
5707 return -EINVAL;
5708 if (mddev->recovery || mddev->sync_thread)
5709 return -EBUSY;
5710 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
5711 /* add the bitmap */
5712 if (mddev->bitmap)
5713 return -EEXIST;
5714 if (mddev->bitmap_info.default_offset == 0)
5715 return -EINVAL;
5716 mddev->bitmap_info.offset =
5717 mddev->bitmap_info.default_offset;
5718 mddev->pers->quiesce(mddev, 1);
5719 rv = bitmap_create(mddev);
5720 if (!rv)
5721 rv = bitmap_load(mddev);
5722 if (rv)
5723 bitmap_destroy(mddev);
5724 mddev->pers->quiesce(mddev, 0);
5725 } else {
5726 /* remove the bitmap */
5727 if (!mddev->bitmap)
5728 return -ENOENT;
5729 if (mddev->bitmap->file)
5730 return -EINVAL;
5731 mddev->pers->quiesce(mddev, 1);
5732 bitmap_destroy(mddev);
5733 mddev->pers->quiesce(mddev, 0);
5734 mddev->bitmap_info.offset = 0;
5737 md_update_sb(mddev, 1);
5738 return rv;
5741 static int set_disk_faulty(mddev_t *mddev, dev_t dev)
5743 mdk_rdev_t *rdev;
5745 if (mddev->pers == NULL)
5746 return -ENODEV;
5748 rdev = find_rdev(mddev, dev);
5749 if (!rdev)
5750 return -ENODEV;
5752 md_error(mddev, rdev);
5753 return 0;
5757 * We have a problem here : there is no easy way to give a CHS
5758 * virtual geometry. We currently pretend that we have a 2 heads
5759 * 4 sectors (with a BIG number of cylinders...). This drives
5760 * dosfs just mad... ;-)
5762 static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
5764 mddev_t *mddev = bdev->bd_disk->private_data;
5766 geo->heads = 2;
5767 geo->sectors = 4;
5768 geo->cylinders = mddev->array_sectors / 8;
5769 return 0;
5772 static int md_ioctl(struct block_device *bdev, fmode_t mode,
5773 unsigned int cmd, unsigned long arg)
5775 int err = 0;
5776 void __user *argp = (void __user *)arg;
5777 mddev_t *mddev = NULL;
5778 int ro;
5780 if (!capable(CAP_SYS_ADMIN))
5781 return -EACCES;
5784 * Commands dealing with the RAID driver but not any
5785 * particular array:
5787 switch (cmd)
5789 case RAID_VERSION:
5790 err = get_version(argp);
5791 goto done;
5793 case PRINT_RAID_DEBUG:
5794 err = 0;
5795 md_print_devices();
5796 goto done;
5798 #ifndef MODULE
5799 case RAID_AUTORUN:
5800 err = 0;
5801 autostart_arrays(arg);
5802 goto done;
5803 #endif
5804 default:;
5808 * Commands creating/starting a new array:
5811 mddev = bdev->bd_disk->private_data;
5813 if (!mddev) {
5814 BUG();
5815 goto abort;
5818 err = mddev_lock(mddev);
5819 if (err) {
5820 printk(KERN_INFO
5821 "md: ioctl lock interrupted, reason %d, cmd %d\n",
5822 err, cmd);
5823 goto abort;
5826 switch (cmd)
5828 case SET_ARRAY_INFO:
5830 mdu_array_info_t info;
5831 if (!arg)
5832 memset(&info, 0, sizeof(info));
5833 else if (copy_from_user(&info, argp, sizeof(info))) {
5834 err = -EFAULT;
5835 goto abort_unlock;
5837 if (mddev->pers) {
5838 err = update_array_info(mddev, &info);
5839 if (err) {
5840 printk(KERN_WARNING "md: couldn't update"
5841 " array info. %d\n", err);
5842 goto abort_unlock;
5844 goto done_unlock;
5846 if (!list_empty(&mddev->disks)) {
5847 printk(KERN_WARNING
5848 "md: array %s already has disks!\n",
5849 mdname(mddev));
5850 err = -EBUSY;
5851 goto abort_unlock;
5853 if (mddev->raid_disks) {
5854 printk(KERN_WARNING
5855 "md: array %s already initialised!\n",
5856 mdname(mddev));
5857 err = -EBUSY;
5858 goto abort_unlock;
5860 err = set_array_info(mddev, &info);
5861 if (err) {
5862 printk(KERN_WARNING "md: couldn't set"
5863 " array info. %d\n", err);
5864 goto abort_unlock;
5867 goto done_unlock;
5869 default:;
5873 * Commands querying/configuring an existing array:
5875 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
5876 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
5877 if ((!mddev->raid_disks && !mddev->external)
5878 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
5879 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
5880 && cmd != GET_BITMAP_FILE) {
5881 err = -ENODEV;
5882 goto abort_unlock;
5886 * Commands even a read-only array can execute:
5888 switch (cmd)
5890 case GET_ARRAY_INFO:
5891 err = get_array_info(mddev, argp);
5892 goto done_unlock;
5894 case GET_BITMAP_FILE:
5895 err = get_bitmap_file(mddev, argp);
5896 goto done_unlock;
5898 case GET_DISK_INFO:
5899 err = get_disk_info(mddev, argp);
5900 goto done_unlock;
5902 case RESTART_ARRAY_RW:
5903 err = restart_array(mddev);
5904 goto done_unlock;
5906 case STOP_ARRAY:
5907 err = do_md_stop(mddev, 0, 1);
5908 goto done_unlock;
5910 case STOP_ARRAY_RO:
5911 err = md_set_readonly(mddev, 1);
5912 goto done_unlock;
5914 case BLKROSET:
5915 if (get_user(ro, (int __user *)(arg))) {
5916 err = -EFAULT;
5917 goto done_unlock;
5919 err = -EINVAL;
5921 /* if the bdev is going readonly the value of mddev->ro
5922 * does not matter, no writes are coming
5924 if (ro)
5925 goto done_unlock;
5927 /* are we are already prepared for writes? */
5928 if (mddev->ro != 1)
5929 goto done_unlock;
5931 /* transitioning to readauto need only happen for
5932 * arrays that call md_write_start
5934 if (mddev->pers) {
5935 err = restart_array(mddev);
5936 if (err == 0) {
5937 mddev->ro = 2;
5938 set_disk_ro(mddev->gendisk, 0);
5941 goto done_unlock;
5945 * The remaining ioctls are changing the state of the
5946 * superblock, so we do not allow them on read-only arrays.
5947 * However non-MD ioctls (e.g. get-size) will still come through
5948 * here and hit the 'default' below, so only disallow
5949 * 'md' ioctls, and switch to rw mode if started auto-readonly.
5951 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
5952 if (mddev->ro == 2) {
5953 mddev->ro = 0;
5954 sysfs_notify_dirent_safe(mddev->sysfs_state);
5955 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5956 md_wakeup_thread(mddev->thread);
5957 } else {
5958 err = -EROFS;
5959 goto abort_unlock;
5963 switch (cmd)
5965 case ADD_NEW_DISK:
5967 mdu_disk_info_t info;
5968 if (copy_from_user(&info, argp, sizeof(info)))
5969 err = -EFAULT;
5970 else
5971 err = add_new_disk(mddev, &info);
5972 goto done_unlock;
5975 case HOT_REMOVE_DISK:
5976 err = hot_remove_disk(mddev, new_decode_dev(arg));
5977 goto done_unlock;
5979 case HOT_ADD_DISK:
5980 err = hot_add_disk(mddev, new_decode_dev(arg));
5981 goto done_unlock;
5983 case SET_DISK_FAULTY:
5984 err = set_disk_faulty(mddev, new_decode_dev(arg));
5985 goto done_unlock;
5987 case RUN_ARRAY:
5988 err = do_md_run(mddev);
5989 goto done_unlock;
5991 case SET_BITMAP_FILE:
5992 err = set_bitmap_file(mddev, (int)arg);
5993 goto done_unlock;
5995 default:
5996 err = -EINVAL;
5997 goto abort_unlock;
6000 done_unlock:
6001 abort_unlock:
6002 if (mddev->hold_active == UNTIL_IOCTL &&
6003 err != -EINVAL)
6004 mddev->hold_active = 0;
6005 mddev_unlock(mddev);
6007 return err;
6008 done:
6009 if (err)
6010 MD_BUG();
6011 abort:
6012 return err;
6014 #ifdef CONFIG_COMPAT
6015 static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
6016 unsigned int cmd, unsigned long arg)
6018 switch (cmd) {
6019 case HOT_REMOVE_DISK:
6020 case HOT_ADD_DISK:
6021 case SET_DISK_FAULTY:
6022 case SET_BITMAP_FILE:
6023 /* These take in integer arg, do not convert */
6024 break;
6025 default:
6026 arg = (unsigned long)compat_ptr(arg);
6027 break;
6030 return md_ioctl(bdev, mode, cmd, arg);
6032 #endif /* CONFIG_COMPAT */
6034 static int md_open(struct block_device *bdev, fmode_t mode)
6037 * Succeed if we can lock the mddev, which confirms that
6038 * it isn't being stopped right now.
6040 mddev_t *mddev = mddev_find(bdev->bd_dev);
6041 int err;
6043 if (mddev->gendisk != bdev->bd_disk) {
6044 /* we are racing with mddev_put which is discarding this
6045 * bd_disk.
6047 mddev_put(mddev);
6048 /* Wait until bdev->bd_disk is definitely gone */
6049 flush_workqueue(md_misc_wq);
6050 /* Then retry the open from the top */
6051 return -ERESTARTSYS;
6053 BUG_ON(mddev != bdev->bd_disk->private_data);
6055 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
6056 goto out;
6058 err = 0;
6059 atomic_inc(&mddev->openers);
6060 mutex_unlock(&mddev->open_mutex);
6062 check_disk_change(bdev);
6063 out:
6064 return err;
6067 static int md_release(struct gendisk *disk, fmode_t mode)
6069 mddev_t *mddev = disk->private_data;
6071 BUG_ON(!mddev);
6072 atomic_dec(&mddev->openers);
6073 mddev_put(mddev);
6075 return 0;
6078 static int md_media_changed(struct gendisk *disk)
6080 mddev_t *mddev = disk->private_data;
6082 return mddev->changed;
6085 static int md_revalidate(struct gendisk *disk)
6087 mddev_t *mddev = disk->private_data;
6089 mddev->changed = 0;
6090 return 0;
6092 static const struct block_device_operations md_fops =
6094 .owner = THIS_MODULE,
6095 .open = md_open,
6096 .release = md_release,
6097 .ioctl = md_ioctl,
6098 #ifdef CONFIG_COMPAT
6099 .compat_ioctl = md_compat_ioctl,
6100 #endif
6101 .getgeo = md_getgeo,
6102 .media_changed = md_media_changed,
6103 .revalidate_disk= md_revalidate,
6106 static int md_thread(void * arg)
6108 mdk_thread_t *thread = arg;
6111 * md_thread is a 'system-thread', it's priority should be very
6112 * high. We avoid resource deadlocks individually in each
6113 * raid personality. (RAID5 does preallocation) We also use RR and
6114 * the very same RT priority as kswapd, thus we will never get
6115 * into a priority inversion deadlock.
6117 * we definitely have to have equal or higher priority than
6118 * bdflush, otherwise bdflush will deadlock if there are too
6119 * many dirty RAID5 blocks.
6122 allow_signal(SIGKILL);
6123 while (!kthread_should_stop()) {
6125 /* We need to wait INTERRUPTIBLE so that
6126 * we don't add to the load-average.
6127 * That means we need to be sure no signals are
6128 * pending
6130 if (signal_pending(current))
6131 flush_signals(current);
6133 wait_event_interruptible_timeout
6134 (thread->wqueue,
6135 test_bit(THREAD_WAKEUP, &thread->flags)
6136 || kthread_should_stop(),
6137 thread->timeout);
6139 clear_bit(THREAD_WAKEUP, &thread->flags);
6140 if (!kthread_should_stop())
6141 thread->run(thread->mddev);
6144 return 0;
6147 void md_wakeup_thread(mdk_thread_t *thread)
6149 if (thread) {
6150 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
6151 set_bit(THREAD_WAKEUP, &thread->flags);
6152 wake_up(&thread->wqueue);
6156 mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
6157 const char *name)
6159 mdk_thread_t *thread;
6161 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
6162 if (!thread)
6163 return NULL;
6165 init_waitqueue_head(&thread->wqueue);
6167 thread->run = run;
6168 thread->mddev = mddev;
6169 thread->timeout = MAX_SCHEDULE_TIMEOUT;
6170 thread->tsk = kthread_run(md_thread, thread,
6171 "%s_%s",
6172 mdname(thread->mddev),
6173 name ?: mddev->pers->name);
6174 if (IS_ERR(thread->tsk)) {
6175 kfree(thread);
6176 return NULL;
6178 return thread;
6181 void md_unregister_thread(mdk_thread_t *thread)
6183 if (!thread)
6184 return;
6185 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
6187 kthread_stop(thread->tsk);
6188 kfree(thread);
6191 void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
6193 if (!mddev) {
6194 MD_BUG();
6195 return;
6198 if (!rdev || test_bit(Faulty, &rdev->flags))
6199 return;
6201 if (mddev->external)
6202 set_bit(Blocked, &rdev->flags);
6204 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
6205 mdname(mddev),
6206 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
6207 __builtin_return_address(0),__builtin_return_address(1),
6208 __builtin_return_address(2),__builtin_return_address(3));
6210 if (!mddev->pers)
6211 return;
6212 if (!mddev->pers->error_handler)
6213 return;
6214 mddev->pers->error_handler(mddev,rdev);
6215 if (mddev->degraded)
6216 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
6217 sysfs_notify_dirent_safe(rdev->sysfs_state);
6218 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6219 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6220 md_wakeup_thread(mddev->thread);
6221 if (mddev->event_work.func)
6222 queue_work(md_misc_wq, &mddev->event_work);
6223 md_new_event_inintr(mddev);
6226 /* seq_file implementation /proc/mdstat */
6228 static void status_unused(struct seq_file *seq)
6230 int i = 0;
6231 mdk_rdev_t *rdev;
6233 seq_printf(seq, "unused devices: ");
6235 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
6236 char b[BDEVNAME_SIZE];
6237 i++;
6238 seq_printf(seq, "%s ",
6239 bdevname(rdev->bdev,b));
6241 if (!i)
6242 seq_printf(seq, "<none>");
6244 seq_printf(seq, "\n");
6248 static void status_resync(struct seq_file *seq, mddev_t * mddev)
6250 sector_t max_sectors, resync, res;
6251 unsigned long dt, db;
6252 sector_t rt;
6253 int scale;
6254 unsigned int per_milli;
6256 resync = mddev->curr_resync - atomic_read(&mddev->recovery_active);
6258 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
6259 max_sectors = mddev->resync_max_sectors;
6260 else
6261 max_sectors = mddev->dev_sectors;
6264 * Should not happen.
6266 if (!max_sectors) {
6267 MD_BUG();
6268 return;
6270 /* Pick 'scale' such that (resync>>scale)*1000 will fit
6271 * in a sector_t, and (max_sectors>>scale) will fit in a
6272 * u32, as those are the requirements for sector_div.
6273 * Thus 'scale' must be at least 10
6275 scale = 10;
6276 if (sizeof(sector_t) > sizeof(unsigned long)) {
6277 while ( max_sectors/2 > (1ULL<<(scale+32)))
6278 scale++;
6280 res = (resync>>scale)*1000;
6281 sector_div(res, (u32)((max_sectors>>scale)+1));
6283 per_milli = res;
6285 int i, x = per_milli/50, y = 20-x;
6286 seq_printf(seq, "[");
6287 for (i = 0; i < x; i++)
6288 seq_printf(seq, "=");
6289 seq_printf(seq, ">");
6290 for (i = 0; i < y; i++)
6291 seq_printf(seq, ".");
6292 seq_printf(seq, "] ");
6294 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
6295 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
6296 "reshape" :
6297 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
6298 "check" :
6299 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
6300 "resync" : "recovery"))),
6301 per_milli/10, per_milli % 10,
6302 (unsigned long long) resync/2,
6303 (unsigned long long) max_sectors/2);
6306 * dt: time from mark until now
6307 * db: blocks written from mark until now
6308 * rt: remaining time
6310 * rt is a sector_t, so could be 32bit or 64bit.
6311 * So we divide before multiply in case it is 32bit and close
6312 * to the limit.
6313 * We scale the divisor (db) by 32 to avoid losing precision
6314 * near the end of resync when the number of remaining sectors
6315 * is close to 'db'.
6316 * We then divide rt by 32 after multiplying by db to compensate.
6317 * The '+1' avoids division by zero if db is very small.
6319 dt = ((jiffies - mddev->resync_mark) / HZ);
6320 if (!dt) dt++;
6321 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
6322 - mddev->resync_mark_cnt;
6324 rt = max_sectors - resync; /* number of remaining sectors */
6325 sector_div(rt, db/32+1);
6326 rt *= dt;
6327 rt >>= 5;
6329 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
6330 ((unsigned long)rt % 60)/6);
6332 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
6335 static void *md_seq_start(struct seq_file *seq, loff_t *pos)
6337 struct list_head *tmp;
6338 loff_t l = *pos;
6339 mddev_t *mddev;
6341 if (l >= 0x10000)
6342 return NULL;
6343 if (!l--)
6344 /* header */
6345 return (void*)1;
6347 spin_lock(&all_mddevs_lock);
6348 list_for_each(tmp,&all_mddevs)
6349 if (!l--) {
6350 mddev = list_entry(tmp, mddev_t, all_mddevs);
6351 mddev_get(mddev);
6352 spin_unlock(&all_mddevs_lock);
6353 return mddev;
6355 spin_unlock(&all_mddevs_lock);
6356 if (!l--)
6357 return (void*)2;/* tail */
6358 return NULL;
6361 static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
6363 struct list_head *tmp;
6364 mddev_t *next_mddev, *mddev = v;
6366 ++*pos;
6367 if (v == (void*)2)
6368 return NULL;
6370 spin_lock(&all_mddevs_lock);
6371 if (v == (void*)1)
6372 tmp = all_mddevs.next;
6373 else
6374 tmp = mddev->all_mddevs.next;
6375 if (tmp != &all_mddevs)
6376 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
6377 else {
6378 next_mddev = (void*)2;
6379 *pos = 0x10000;
6381 spin_unlock(&all_mddevs_lock);
6383 if (v != (void*)1)
6384 mddev_put(mddev);
6385 return next_mddev;
6389 static void md_seq_stop(struct seq_file *seq, void *v)
6391 mddev_t *mddev = v;
6393 if (mddev && v != (void*)1 && v != (void*)2)
6394 mddev_put(mddev);
6397 struct mdstat_info {
6398 int event;
6401 static int md_seq_show(struct seq_file *seq, void *v)
6403 mddev_t *mddev = v;
6404 sector_t sectors;
6405 mdk_rdev_t *rdev;
6406 struct mdstat_info *mi = seq->private;
6407 struct bitmap *bitmap;
6409 if (v == (void*)1) {
6410 struct mdk_personality *pers;
6411 seq_printf(seq, "Personalities : ");
6412 spin_lock(&pers_lock);
6413 list_for_each_entry(pers, &pers_list, list)
6414 seq_printf(seq, "[%s] ", pers->name);
6416 spin_unlock(&pers_lock);
6417 seq_printf(seq, "\n");
6418 mi->event = atomic_read(&md_event_count);
6419 return 0;
6421 if (v == (void*)2) {
6422 status_unused(seq);
6423 return 0;
6426 if (mddev_lock(mddev) < 0)
6427 return -EINTR;
6429 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
6430 seq_printf(seq, "%s : %sactive", mdname(mddev),
6431 mddev->pers ? "" : "in");
6432 if (mddev->pers) {
6433 if (mddev->ro==1)
6434 seq_printf(seq, " (read-only)");
6435 if (mddev->ro==2)
6436 seq_printf(seq, " (auto-read-only)");
6437 seq_printf(seq, " %s", mddev->pers->name);
6440 sectors = 0;
6441 list_for_each_entry(rdev, &mddev->disks, same_set) {
6442 char b[BDEVNAME_SIZE];
6443 seq_printf(seq, " %s[%d]",
6444 bdevname(rdev->bdev,b), rdev->desc_nr);
6445 if (test_bit(WriteMostly, &rdev->flags))
6446 seq_printf(seq, "(W)");
6447 if (test_bit(Faulty, &rdev->flags)) {
6448 seq_printf(seq, "(F)");
6449 continue;
6450 } else if (rdev->raid_disk < 0)
6451 seq_printf(seq, "(S)"); /* spare */
6452 sectors += rdev->sectors;
6455 if (!list_empty(&mddev->disks)) {
6456 if (mddev->pers)
6457 seq_printf(seq, "\n %llu blocks",
6458 (unsigned long long)
6459 mddev->array_sectors / 2);
6460 else
6461 seq_printf(seq, "\n %llu blocks",
6462 (unsigned long long)sectors / 2);
6464 if (mddev->persistent) {
6465 if (mddev->major_version != 0 ||
6466 mddev->minor_version != 90) {
6467 seq_printf(seq," super %d.%d",
6468 mddev->major_version,
6469 mddev->minor_version);
6471 } else if (mddev->external)
6472 seq_printf(seq, " super external:%s",
6473 mddev->metadata_type);
6474 else
6475 seq_printf(seq, " super non-persistent");
6477 if (mddev->pers) {
6478 mddev->pers->status(seq, mddev);
6479 seq_printf(seq, "\n ");
6480 if (mddev->pers->sync_request) {
6481 if (mddev->curr_resync > 2) {
6482 status_resync(seq, mddev);
6483 seq_printf(seq, "\n ");
6484 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
6485 seq_printf(seq, "\tresync=DELAYED\n ");
6486 else if (mddev->recovery_cp < MaxSector)
6487 seq_printf(seq, "\tresync=PENDING\n ");
6489 } else
6490 seq_printf(seq, "\n ");
6492 if ((bitmap = mddev->bitmap)) {
6493 unsigned long chunk_kb;
6494 unsigned long flags;
6495 spin_lock_irqsave(&bitmap->lock, flags);
6496 chunk_kb = mddev->bitmap_info.chunksize >> 10;
6497 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
6498 "%lu%s chunk",
6499 bitmap->pages - bitmap->missing_pages,
6500 bitmap->pages,
6501 (bitmap->pages - bitmap->missing_pages)
6502 << (PAGE_SHIFT - 10),
6503 chunk_kb ? chunk_kb : mddev->bitmap_info.chunksize,
6504 chunk_kb ? "KB" : "B");
6505 if (bitmap->file) {
6506 seq_printf(seq, ", file: ");
6507 seq_path(seq, &bitmap->file->f_path, " \t\n");
6510 seq_printf(seq, "\n");
6511 spin_unlock_irqrestore(&bitmap->lock, flags);
6514 seq_printf(seq, "\n");
6516 mddev_unlock(mddev);
6518 return 0;
6521 static const struct seq_operations md_seq_ops = {
6522 .start = md_seq_start,
6523 .next = md_seq_next,
6524 .stop = md_seq_stop,
6525 .show = md_seq_show,
6528 static int md_seq_open(struct inode *inode, struct file *file)
6530 int error;
6531 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
6532 if (mi == NULL)
6533 return -ENOMEM;
6535 error = seq_open(file, &md_seq_ops);
6536 if (error)
6537 kfree(mi);
6538 else {
6539 struct seq_file *p = file->private_data;
6540 p->private = mi;
6541 mi->event = atomic_read(&md_event_count);
6543 return error;
6546 static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
6548 struct seq_file *m = filp->private_data;
6549 struct mdstat_info *mi = m->private;
6550 int mask;
6552 poll_wait(filp, &md_event_waiters, wait);
6554 /* always allow read */
6555 mask = POLLIN | POLLRDNORM;
6557 if (mi->event != atomic_read(&md_event_count))
6558 mask |= POLLERR | POLLPRI;
6559 return mask;
6562 static const struct file_operations md_seq_fops = {
6563 .owner = THIS_MODULE,
6564 .open = md_seq_open,
6565 .read = seq_read,
6566 .llseek = seq_lseek,
6567 .release = seq_release_private,
6568 .poll = mdstat_poll,
6571 int register_md_personality(struct mdk_personality *p)
6573 spin_lock(&pers_lock);
6574 list_add_tail(&p->list, &pers_list);
6575 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
6576 spin_unlock(&pers_lock);
6577 return 0;
6580 int unregister_md_personality(struct mdk_personality *p)
6582 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
6583 spin_lock(&pers_lock);
6584 list_del_init(&p->list);
6585 spin_unlock(&pers_lock);
6586 return 0;
6589 static int is_mddev_idle(mddev_t *mddev, int init)
6591 mdk_rdev_t * rdev;
6592 int idle;
6593 int curr_events;
6595 idle = 1;
6596 rcu_read_lock();
6597 rdev_for_each_rcu(rdev, mddev) {
6598 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
6599 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
6600 (int)part_stat_read(&disk->part0, sectors[1]) -
6601 atomic_read(&disk->sync_io);
6602 /* sync IO will cause sync_io to increase before the disk_stats
6603 * as sync_io is counted when a request starts, and
6604 * disk_stats is counted when it completes.
6605 * So resync activity will cause curr_events to be smaller than
6606 * when there was no such activity.
6607 * non-sync IO will cause disk_stat to increase without
6608 * increasing sync_io so curr_events will (eventually)
6609 * be larger than it was before. Once it becomes
6610 * substantially larger, the test below will cause
6611 * the array to appear non-idle, and resync will slow
6612 * down.
6613 * If there is a lot of outstanding resync activity when
6614 * we set last_event to curr_events, then all that activity
6615 * completing might cause the array to appear non-idle
6616 * and resync will be slowed down even though there might
6617 * not have been non-resync activity. This will only
6618 * happen once though. 'last_events' will soon reflect
6619 * the state where there is little or no outstanding
6620 * resync requests, and further resync activity will
6621 * always make curr_events less than last_events.
6624 if (init || curr_events - rdev->last_events > 64) {
6625 rdev->last_events = curr_events;
6626 idle = 0;
6629 rcu_read_unlock();
6630 return idle;
6633 void md_done_sync(mddev_t *mddev, int blocks, int ok)
6635 /* another "blocks" (512byte) blocks have been synced */
6636 atomic_sub(blocks, &mddev->recovery_active);
6637 wake_up(&mddev->recovery_wait);
6638 if (!ok) {
6639 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6640 md_wakeup_thread(mddev->thread);
6641 // stop recovery, signal do_sync ....
6646 /* md_write_start(mddev, bi)
6647 * If we need to update some array metadata (e.g. 'active' flag
6648 * in superblock) before writing, schedule a superblock update
6649 * and wait for it to complete.
6651 void md_write_start(mddev_t *mddev, struct bio *bi)
6653 int did_change = 0;
6654 if (bio_data_dir(bi) != WRITE)
6655 return;
6657 BUG_ON(mddev->ro == 1);
6658 if (mddev->ro == 2) {
6659 /* need to switch to read/write */
6660 mddev->ro = 0;
6661 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6662 md_wakeup_thread(mddev->thread);
6663 md_wakeup_thread(mddev->sync_thread);
6664 did_change = 1;
6666 atomic_inc(&mddev->writes_pending);
6667 if (mddev->safemode == 1)
6668 mddev->safemode = 0;
6669 if (mddev->in_sync) {
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 md_wakeup_thread(mddev->thread);
6676 did_change = 1;
6678 spin_unlock_irq(&mddev->write_lock);
6680 if (did_change)
6681 sysfs_notify_dirent_safe(mddev->sysfs_state);
6682 wait_event(mddev->sb_wait,
6683 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
6686 void md_write_end(mddev_t *mddev)
6688 if (atomic_dec_and_test(&mddev->writes_pending)) {
6689 if (mddev->safemode == 2)
6690 md_wakeup_thread(mddev->thread);
6691 else if (mddev->safemode_delay)
6692 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
6696 /* md_allow_write(mddev)
6697 * Calling this ensures that the array is marked 'active' so that writes
6698 * may proceed without blocking. It is important to call this before
6699 * attempting a GFP_KERNEL allocation while holding the mddev lock.
6700 * Must be called with mddev_lock held.
6702 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
6703 * is dropped, so return -EAGAIN after notifying userspace.
6705 int md_allow_write(mddev_t *mddev)
6707 if (!mddev->pers)
6708 return 0;
6709 if (mddev->ro)
6710 return 0;
6711 if (!mddev->pers->sync_request)
6712 return 0;
6714 spin_lock_irq(&mddev->write_lock);
6715 if (mddev->in_sync) {
6716 mddev->in_sync = 0;
6717 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6718 set_bit(MD_CHANGE_PENDING, &mddev->flags);
6719 if (mddev->safemode_delay &&
6720 mddev->safemode == 0)
6721 mddev->safemode = 1;
6722 spin_unlock_irq(&mddev->write_lock);
6723 md_update_sb(mddev, 0);
6724 sysfs_notify_dirent_safe(mddev->sysfs_state);
6725 } else
6726 spin_unlock_irq(&mddev->write_lock);
6728 if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
6729 return -EAGAIN;
6730 else
6731 return 0;
6733 EXPORT_SYMBOL_GPL(md_allow_write);
6735 #define SYNC_MARKS 10
6736 #define SYNC_MARK_STEP (3*HZ)
6737 void md_do_sync(mddev_t *mddev)
6739 mddev_t *mddev2;
6740 unsigned int currspeed = 0,
6741 window;
6742 sector_t max_sectors,j, io_sectors;
6743 unsigned long mark[SYNC_MARKS];
6744 sector_t mark_cnt[SYNC_MARKS];
6745 int last_mark,m;
6746 struct list_head *tmp;
6747 sector_t last_check;
6748 int skipped = 0;
6749 mdk_rdev_t *rdev;
6750 char *desc;
6752 /* just incase thread restarts... */
6753 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
6754 return;
6755 if (mddev->ro) /* never try to sync a read-only array */
6756 return;
6758 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6759 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
6760 desc = "data-check";
6761 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6762 desc = "requested-resync";
6763 else
6764 desc = "resync";
6765 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
6766 desc = "reshape";
6767 else
6768 desc = "recovery";
6770 /* we overload curr_resync somewhat here.
6771 * 0 == not engaged in resync at all
6772 * 2 == checking that there is no conflict with another sync
6773 * 1 == like 2, but have yielded to allow conflicting resync to
6774 * commense
6775 * other == active in resync - this many blocks
6777 * Before starting a resync we must have set curr_resync to
6778 * 2, and then checked that every "conflicting" array has curr_resync
6779 * less than ours. When we find one that is the same or higher
6780 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
6781 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
6782 * This will mean we have to start checking from the beginning again.
6786 do {
6787 mddev->curr_resync = 2;
6789 try_again:
6790 if (kthread_should_stop())
6791 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6793 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6794 goto skip;
6795 for_each_mddev(mddev2, tmp) {
6796 if (mddev2 == mddev)
6797 continue;
6798 if (!mddev->parallel_resync
6799 && mddev2->curr_resync
6800 && match_mddev_units(mddev, mddev2)) {
6801 DEFINE_WAIT(wq);
6802 if (mddev < mddev2 && mddev->curr_resync == 2) {
6803 /* arbitrarily yield */
6804 mddev->curr_resync = 1;
6805 wake_up(&resync_wait);
6807 if (mddev > mddev2 && mddev->curr_resync == 1)
6808 /* no need to wait here, we can wait the next
6809 * time 'round when curr_resync == 2
6811 continue;
6812 /* We need to wait 'interruptible' so as not to
6813 * contribute to the load average, and not to
6814 * be caught by 'softlockup'
6816 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
6817 if (!kthread_should_stop() &&
6818 mddev2->curr_resync >= mddev->curr_resync) {
6819 printk(KERN_INFO "md: delaying %s of %s"
6820 " until %s has finished (they"
6821 " share one or more physical units)\n",
6822 desc, mdname(mddev), mdname(mddev2));
6823 mddev_put(mddev2);
6824 if (signal_pending(current))
6825 flush_signals(current);
6826 schedule();
6827 finish_wait(&resync_wait, &wq);
6828 goto try_again;
6830 finish_wait(&resync_wait, &wq);
6833 } while (mddev->curr_resync < 2);
6835 j = 0;
6836 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6837 /* resync follows the size requested by the personality,
6838 * which defaults to physical size, but can be virtual size
6840 max_sectors = mddev->resync_max_sectors;
6841 mddev->resync_mismatches = 0;
6842 /* we don't use the checkpoint if there's a bitmap */
6843 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6844 j = mddev->resync_min;
6845 else if (!mddev->bitmap)
6846 j = mddev->recovery_cp;
6848 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
6849 max_sectors = mddev->dev_sectors;
6850 else {
6851 /* recovery follows the physical size of devices */
6852 max_sectors = mddev->dev_sectors;
6853 j = MaxSector;
6854 rcu_read_lock();
6855 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
6856 if (rdev->raid_disk >= 0 &&
6857 !test_bit(Faulty, &rdev->flags) &&
6858 !test_bit(In_sync, &rdev->flags) &&
6859 rdev->recovery_offset < j)
6860 j = rdev->recovery_offset;
6861 rcu_read_unlock();
6864 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
6865 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
6866 " %d KB/sec/disk.\n", speed_min(mddev));
6867 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
6868 "(but not more than %d KB/sec) for %s.\n",
6869 speed_max(mddev), desc);
6871 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
6873 io_sectors = 0;
6874 for (m = 0; m < SYNC_MARKS; m++) {
6875 mark[m] = jiffies;
6876 mark_cnt[m] = io_sectors;
6878 last_mark = 0;
6879 mddev->resync_mark = mark[last_mark];
6880 mddev->resync_mark_cnt = mark_cnt[last_mark];
6883 * Tune reconstruction:
6885 window = 32*(PAGE_SIZE/512);
6886 printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n",
6887 window/2, (unsigned long long)max_sectors/2);
6889 atomic_set(&mddev->recovery_active, 0);
6890 last_check = 0;
6892 if (j>2) {
6893 printk(KERN_INFO
6894 "md: resuming %s of %s from checkpoint.\n",
6895 desc, mdname(mddev));
6896 mddev->curr_resync = j;
6898 mddev->curr_resync_completed = j;
6900 while (j < max_sectors) {
6901 sector_t sectors;
6903 skipped = 0;
6905 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
6906 ((mddev->curr_resync > mddev->curr_resync_completed &&
6907 (mddev->curr_resync - mddev->curr_resync_completed)
6908 > (max_sectors >> 4)) ||
6909 (j - mddev->curr_resync_completed)*2
6910 >= mddev->resync_max - mddev->curr_resync_completed
6911 )) {
6912 /* time to update curr_resync_completed */
6913 wait_event(mddev->recovery_wait,
6914 atomic_read(&mddev->recovery_active) == 0);
6915 mddev->curr_resync_completed = j;
6916 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6917 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
6920 while (j >= mddev->resync_max && !kthread_should_stop()) {
6921 /* As this condition is controlled by user-space,
6922 * we can block indefinitely, so use '_interruptible'
6923 * to avoid triggering warnings.
6925 flush_signals(current); /* just in case */
6926 wait_event_interruptible(mddev->recovery_wait,
6927 mddev->resync_max > j
6928 || kthread_should_stop());
6931 if (kthread_should_stop())
6932 goto interrupted;
6934 sectors = mddev->pers->sync_request(mddev, j, &skipped,
6935 currspeed < speed_min(mddev));
6936 if (sectors == 0) {
6937 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6938 goto out;
6941 if (!skipped) { /* actual IO requested */
6942 io_sectors += sectors;
6943 atomic_add(sectors, &mddev->recovery_active);
6946 j += sectors;
6947 if (j>1) mddev->curr_resync = j;
6948 mddev->curr_mark_cnt = io_sectors;
6949 if (last_check == 0)
6950 /* this is the earliers that rebuilt will be
6951 * visible in /proc/mdstat
6953 md_new_event(mddev);
6955 if (last_check + window > io_sectors || j == max_sectors)
6956 continue;
6958 last_check = io_sectors;
6960 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6961 break;
6963 repeat:
6964 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
6965 /* step marks */
6966 int next = (last_mark+1) % SYNC_MARKS;
6968 mddev->resync_mark = mark[next];
6969 mddev->resync_mark_cnt = mark_cnt[next];
6970 mark[next] = jiffies;
6971 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
6972 last_mark = next;
6976 if (kthread_should_stop())
6977 goto interrupted;
6981 * this loop exits only if either when we are slower than
6982 * the 'hard' speed limit, or the system was IO-idle for
6983 * a jiffy.
6984 * the system might be non-idle CPU-wise, but we only care
6985 * about not overloading the IO subsystem. (things like an
6986 * e2fsck being done on the RAID array should execute fast)
6988 cond_resched();
6990 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
6991 /((jiffies-mddev->resync_mark)/HZ +1) +1;
6993 if (currspeed > speed_min(mddev)) {
6994 if ((currspeed > speed_max(mddev)) ||
6995 !is_mddev_idle(mddev, 0)) {
6996 msleep(500);
6997 goto repeat;
7001 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
7003 * this also signals 'finished resyncing' to md_stop
7005 out:
7006 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
7008 /* tell personality that we are finished */
7009 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
7011 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
7012 mddev->curr_resync > 2) {
7013 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
7014 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7015 if (mddev->curr_resync >= mddev->recovery_cp) {
7016 printk(KERN_INFO
7017 "md: checkpointing %s of %s.\n",
7018 desc, mdname(mddev));
7019 mddev->recovery_cp = mddev->curr_resync;
7021 } else
7022 mddev->recovery_cp = MaxSector;
7023 } else {
7024 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7025 mddev->curr_resync = MaxSector;
7026 rcu_read_lock();
7027 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
7028 if (rdev->raid_disk >= 0 &&
7029 mddev->delta_disks >= 0 &&
7030 !test_bit(Faulty, &rdev->flags) &&
7031 !test_bit(In_sync, &rdev->flags) &&
7032 rdev->recovery_offset < mddev->curr_resync)
7033 rdev->recovery_offset = mddev->curr_resync;
7034 rcu_read_unlock();
7037 set_bit(MD_CHANGE_DEVS, &mddev->flags);
7039 skip:
7040 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7041 /* We completed so min/max setting can be forgotten if used. */
7042 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7043 mddev->resync_min = 0;
7044 mddev->resync_max = MaxSector;
7045 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7046 mddev->resync_min = mddev->curr_resync_completed;
7047 mddev->curr_resync = 0;
7048 wake_up(&resync_wait);
7049 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
7050 md_wakeup_thread(mddev->thread);
7051 return;
7053 interrupted:
7055 * got a signal, exit.
7057 printk(KERN_INFO
7058 "md: md_do_sync() got signal ... exiting\n");
7059 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7060 goto out;
7063 EXPORT_SYMBOL_GPL(md_do_sync);
7065 static int remove_and_add_spares(mddev_t *mddev)
7067 mdk_rdev_t *rdev;
7068 int spares = 0;
7070 mddev->curr_resync_completed = 0;
7072 list_for_each_entry(rdev, &mddev->disks, same_set)
7073 if (rdev->raid_disk >= 0 &&
7074 !test_bit(Blocked, &rdev->flags) &&
7075 (test_bit(Faulty, &rdev->flags) ||
7076 ! test_bit(In_sync, &rdev->flags)) &&
7077 atomic_read(&rdev->nr_pending)==0) {
7078 if (mddev->pers->hot_remove_disk(
7079 mddev, rdev->raid_disk)==0) {
7080 char nm[20];
7081 sprintf(nm,"rd%d", rdev->raid_disk);
7082 sysfs_remove_link(&mddev->kobj, nm);
7083 rdev->raid_disk = -1;
7087 if (mddev->degraded && !mddev->recovery_disabled) {
7088 list_for_each_entry(rdev, &mddev->disks, same_set) {
7089 if (rdev->raid_disk >= 0 &&
7090 !test_bit(In_sync, &rdev->flags) &&
7091 !test_bit(Blocked, &rdev->flags))
7092 spares++;
7093 if (rdev->raid_disk < 0
7094 && !test_bit(Faulty, &rdev->flags)) {
7095 rdev->recovery_offset = 0;
7096 if (mddev->pers->
7097 hot_add_disk(mddev, rdev) == 0) {
7098 char nm[20];
7099 sprintf(nm, "rd%d", rdev->raid_disk);
7100 if (sysfs_create_link(&mddev->kobj,
7101 &rdev->kobj, nm))
7102 /* failure here is OK */;
7103 spares++;
7104 md_new_event(mddev);
7105 set_bit(MD_CHANGE_DEVS, &mddev->flags);
7106 } else
7107 break;
7111 return spares;
7114 static void reap_sync_thread(mddev_t *mddev)
7116 mdk_rdev_t *rdev;
7118 /* resync has finished, collect result */
7119 md_unregister_thread(mddev->sync_thread);
7120 mddev->sync_thread = NULL;
7121 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
7122 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
7123 /* success...*/
7124 /* activate any spares */
7125 if (mddev->pers->spare_active(mddev))
7126 sysfs_notify(&mddev->kobj, NULL,
7127 "degraded");
7129 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7130 mddev->pers->finish_reshape)
7131 mddev->pers->finish_reshape(mddev);
7132 md_update_sb(mddev, 1);
7134 /* if array is no-longer degraded, then any saved_raid_disk
7135 * information must be scrapped
7137 if (!mddev->degraded)
7138 list_for_each_entry(rdev, &mddev->disks, same_set)
7139 rdev->saved_raid_disk = -1;
7141 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7142 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7143 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7144 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7145 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
7146 /* flag recovery needed just to double check */
7147 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7148 sysfs_notify_dirent_safe(mddev->sysfs_action);
7149 md_new_event(mddev);
7153 * This routine is regularly called by all per-raid-array threads to
7154 * deal with generic issues like resync and super-block update.
7155 * Raid personalities that don't have a thread (linear/raid0) do not
7156 * need this as they never do any recovery or update the superblock.
7158 * It does not do any resync itself, but rather "forks" off other threads
7159 * to do that as needed.
7160 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
7161 * "->recovery" and create a thread at ->sync_thread.
7162 * When the thread finishes it sets MD_RECOVERY_DONE
7163 * and wakeups up this thread which will reap the thread and finish up.
7164 * This thread also removes any faulty devices (with nr_pending == 0).
7166 * The overall approach is:
7167 * 1/ if the superblock needs updating, update it.
7168 * 2/ If a recovery thread is running, don't do anything else.
7169 * 3/ If recovery has finished, clean up, possibly marking spares active.
7170 * 4/ If there are any faulty devices, remove them.
7171 * 5/ If array is degraded, try to add spares devices
7172 * 6/ If array has spares or is not in-sync, start a resync thread.
7174 void md_check_recovery(mddev_t *mddev)
7176 if (mddev->suspended)
7177 return;
7179 if (mddev->bitmap)
7180 bitmap_daemon_work(mddev);
7182 if (mddev->ro)
7183 return;
7185 if (signal_pending(current)) {
7186 if (mddev->pers->sync_request && !mddev->external) {
7187 printk(KERN_INFO "md: %s in immediate safe mode\n",
7188 mdname(mddev));
7189 mddev->safemode = 2;
7191 flush_signals(current);
7194 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
7195 return;
7196 if ( ! (
7197 (mddev->flags & ~ (1<<MD_CHANGE_PENDING)) ||
7198 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
7199 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
7200 (mddev->external == 0 && mddev->safemode == 1) ||
7201 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
7202 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
7204 return;
7206 if (mddev_trylock(mddev)) {
7207 int spares = 0;
7209 if (mddev->ro) {
7210 /* Only thing we do on a ro array is remove
7211 * failed devices.
7213 mdk_rdev_t *rdev;
7214 list_for_each_entry(rdev, &mddev->disks, same_set)
7215 if (rdev->raid_disk >= 0 &&
7216 !test_bit(Blocked, &rdev->flags) &&
7217 test_bit(Faulty, &rdev->flags) &&
7218 atomic_read(&rdev->nr_pending)==0) {
7219 if (mddev->pers->hot_remove_disk(
7220 mddev, rdev->raid_disk)==0) {
7221 char nm[20];
7222 sprintf(nm,"rd%d", rdev->raid_disk);
7223 sysfs_remove_link(&mddev->kobj, nm);
7224 rdev->raid_disk = -1;
7227 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7228 goto unlock;
7231 if (!mddev->external) {
7232 int did_change = 0;
7233 spin_lock_irq(&mddev->write_lock);
7234 if (mddev->safemode &&
7235 !atomic_read(&mddev->writes_pending) &&
7236 !mddev->in_sync &&
7237 mddev->recovery_cp == MaxSector) {
7238 mddev->in_sync = 1;
7239 did_change = 1;
7240 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
7242 if (mddev->safemode == 1)
7243 mddev->safemode = 0;
7244 spin_unlock_irq(&mddev->write_lock);
7245 if (did_change)
7246 sysfs_notify_dirent_safe(mddev->sysfs_state);
7249 if (mddev->flags)
7250 md_update_sb(mddev, 0);
7252 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
7253 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
7254 /* resync/recovery still happening */
7255 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7256 goto unlock;
7258 if (mddev->sync_thread) {
7259 reap_sync_thread(mddev);
7260 goto unlock;
7262 /* Set RUNNING before clearing NEEDED to avoid
7263 * any transients in the value of "sync_action".
7265 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7266 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7267 /* Clear some bits that don't mean anything, but
7268 * might be left set
7270 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
7271 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
7273 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
7274 goto unlock;
7275 /* no recovery is running.
7276 * remove any failed drives, then
7277 * add spares if possible.
7278 * Spare are also removed and re-added, to allow
7279 * the personality to fail the re-add.
7282 if (mddev->reshape_position != MaxSector) {
7283 if (mddev->pers->check_reshape == NULL ||
7284 mddev->pers->check_reshape(mddev) != 0)
7285 /* Cannot proceed */
7286 goto unlock;
7287 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7288 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
7289 } else if ((spares = remove_and_add_spares(mddev))) {
7290 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7291 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
7292 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7293 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
7294 } else if (mddev->recovery_cp < MaxSector) {
7295 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7296 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
7297 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
7298 /* nothing to be done ... */
7299 goto unlock;
7301 if (mddev->pers->sync_request) {
7302 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
7303 /* We are adding a device or devices to an array
7304 * which has the bitmap stored on all devices.
7305 * So make sure all bitmap pages get written
7307 bitmap_write_all(mddev->bitmap);
7309 mddev->sync_thread = md_register_thread(md_do_sync,
7310 mddev,
7311 "resync");
7312 if (!mddev->sync_thread) {
7313 printk(KERN_ERR "%s: could not start resync"
7314 " thread...\n",
7315 mdname(mddev));
7316 /* leave the spares where they are, it shouldn't hurt */
7317 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7318 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7319 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7320 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7321 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
7322 } else
7323 md_wakeup_thread(mddev->sync_thread);
7324 sysfs_notify_dirent_safe(mddev->sysfs_action);
7325 md_new_event(mddev);
7327 unlock:
7328 if (!mddev->sync_thread) {
7329 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7330 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
7331 &mddev->recovery))
7332 if (mddev->sysfs_action)
7333 sysfs_notify_dirent_safe(mddev->sysfs_action);
7335 mddev_unlock(mddev);
7339 void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
7341 sysfs_notify_dirent_safe(rdev->sysfs_state);
7342 wait_event_timeout(rdev->blocked_wait,
7343 !test_bit(Blocked, &rdev->flags),
7344 msecs_to_jiffies(5000));
7345 rdev_dec_pending(rdev, mddev);
7347 EXPORT_SYMBOL(md_wait_for_blocked_rdev);
7349 static int md_notify_reboot(struct notifier_block *this,
7350 unsigned long code, void *x)
7352 struct list_head *tmp;
7353 mddev_t *mddev;
7355 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
7357 printk(KERN_INFO "md: stopping all md devices.\n");
7359 for_each_mddev(mddev, tmp)
7360 if (mddev_trylock(mddev)) {
7361 /* Force a switch to readonly even array
7362 * appears to still be in use. Hence
7363 * the '100'.
7365 md_set_readonly(mddev, 100);
7366 mddev_unlock(mddev);
7369 * certain more exotic SCSI devices are known to be
7370 * volatile wrt too early system reboots. While the
7371 * right place to handle this issue is the given
7372 * driver, we do want to have a safe RAID driver ...
7374 mdelay(1000*1);
7376 return NOTIFY_DONE;
7379 static struct notifier_block md_notifier = {
7380 .notifier_call = md_notify_reboot,
7381 .next = NULL,
7382 .priority = INT_MAX, /* before any real devices */
7385 static void md_geninit(void)
7387 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
7389 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
7392 static int __init md_init(void)
7394 int ret = -ENOMEM;
7396 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
7397 if (!md_wq)
7398 goto err_wq;
7400 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
7401 if (!md_misc_wq)
7402 goto err_misc_wq;
7404 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
7405 goto err_md;
7407 if ((ret = register_blkdev(0, "mdp")) < 0)
7408 goto err_mdp;
7409 mdp_major = ret;
7411 blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE,
7412 md_probe, NULL, NULL);
7413 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
7414 md_probe, NULL, NULL);
7416 register_reboot_notifier(&md_notifier);
7417 raid_table_header = register_sysctl_table(raid_root_table);
7419 md_geninit();
7420 return 0;
7422 err_mdp:
7423 unregister_blkdev(MD_MAJOR, "md");
7424 err_md:
7425 destroy_workqueue(md_misc_wq);
7426 err_misc_wq:
7427 destroy_workqueue(md_wq);
7428 err_wq:
7429 return ret;
7432 #ifndef MODULE
7435 * Searches all registered partitions for autorun RAID arrays
7436 * at boot time.
7439 static LIST_HEAD(all_detected_devices);
7440 struct detected_devices_node {
7441 struct list_head list;
7442 dev_t dev;
7445 void md_autodetect_dev(dev_t dev)
7447 struct detected_devices_node *node_detected_dev;
7449 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
7450 if (node_detected_dev) {
7451 node_detected_dev->dev = dev;
7452 list_add_tail(&node_detected_dev->list, &all_detected_devices);
7453 } else {
7454 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
7455 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
7460 static void autostart_arrays(int part)
7462 mdk_rdev_t *rdev;
7463 struct detected_devices_node *node_detected_dev;
7464 dev_t dev;
7465 int i_scanned, i_passed;
7467 i_scanned = 0;
7468 i_passed = 0;
7470 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
7472 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
7473 i_scanned++;
7474 node_detected_dev = list_entry(all_detected_devices.next,
7475 struct detected_devices_node, list);
7476 list_del(&node_detected_dev->list);
7477 dev = node_detected_dev->dev;
7478 kfree(node_detected_dev);
7479 rdev = md_import_device(dev,0, 90);
7480 if (IS_ERR(rdev))
7481 continue;
7483 if (test_bit(Faulty, &rdev->flags)) {
7484 MD_BUG();
7485 continue;
7487 set_bit(AutoDetected, &rdev->flags);
7488 list_add(&rdev->same_set, &pending_raid_disks);
7489 i_passed++;
7492 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
7493 i_scanned, i_passed);
7495 autorun_devices(part);
7498 #endif /* !MODULE */
7500 static __exit void md_exit(void)
7502 mddev_t *mddev;
7503 struct list_head *tmp;
7505 blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS);
7506 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
7508 unregister_blkdev(MD_MAJOR,"md");
7509 unregister_blkdev(mdp_major, "mdp");
7510 unregister_reboot_notifier(&md_notifier);
7511 unregister_sysctl_table(raid_table_header);
7512 remove_proc_entry("mdstat", NULL);
7513 for_each_mddev(mddev, tmp) {
7514 export_array(mddev);
7515 mddev->hold_active = 0;
7517 destroy_workqueue(md_misc_wq);
7518 destroy_workqueue(md_wq);
7521 subsys_initcall(md_init);
7522 module_exit(md_exit)
7524 static int get_ro(char *buffer, struct kernel_param *kp)
7526 return sprintf(buffer, "%d", start_readonly);
7528 static int set_ro(const char *val, struct kernel_param *kp)
7530 char *e;
7531 int num = simple_strtoul(val, &e, 10);
7532 if (*val && (*e == '\0' || *e == '\n')) {
7533 start_readonly = num;
7534 return 0;
7536 return -EINVAL;
7539 module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
7540 module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
7542 module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
7544 EXPORT_SYMBOL(register_md_personality);
7545 EXPORT_SYMBOL(unregister_md_personality);
7546 EXPORT_SYMBOL(md_error);
7547 EXPORT_SYMBOL(md_done_sync);
7548 EXPORT_SYMBOL(md_write_start);
7549 EXPORT_SYMBOL(md_write_end);
7550 EXPORT_SYMBOL(md_register_thread);
7551 EXPORT_SYMBOL(md_unregister_thread);
7552 EXPORT_SYMBOL(md_wakeup_thread);
7553 EXPORT_SYMBOL(md_check_recovery);
7554 MODULE_LICENSE("GPL");
7555 MODULE_DESCRIPTION("MD RAID framework");
7556 MODULE_ALIAS("md");
7557 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);