Merge branch 'for-linus' of git://oss.sgi.com/xfs/xfs
[linux/fpc-iii.git] / drivers / md / md.c
blob46b3a044eadf41166a60cab6f10153fd1e75f09f
1 /*
2 md.c : Multiple Devices driver for Linux
3 Copyright (C) 1998, 1999, 2000 Ingo Molnar
5 completely rewritten, based on the MD driver code from Marc Zyngier
7 Changes:
9 - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar
10 - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com>
11 - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net>
12 - kerneld support by Boris Tobotras <boris@xtalk.msk.su>
13 - kmod support by: Cyrus Durgin
14 - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com>
15 - Devfs support by Richard Gooch <rgooch@atnf.csiro.au>
17 - lots of fixes and improvements to the RAID1/RAID5 and generic
18 RAID code (such as request based resynchronization):
20 Neil Brown <neilb@cse.unsw.edu.au>.
22 - persistent bitmap code
23 Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.
25 This program is free software; you can redistribute it and/or modify
26 it under the terms of the GNU General Public License as published by
27 the Free Software Foundation; either version 2, or (at your option)
28 any later version.
30 You should have received a copy of the GNU General Public License
31 (for example /usr/src/linux/COPYING); if not, write to the Free
32 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35 #include <linux/kthread.h>
36 #include <linux/blkdev.h>
37 #include <linux/sysctl.h>
38 #include <linux/seq_file.h>
39 #include <linux/buffer_head.h> /* for invalidate_bdev */
40 #include <linux/poll.h>
41 #include <linux/ctype.h>
42 #include <linux/string.h>
43 #include <linux/hdreg.h>
44 #include <linux/proc_fs.h>
45 #include <linux/random.h>
46 #include <linux/reboot.h>
47 #include <linux/file.h>
48 #include <linux/compat.h>
49 #include <linux/delay.h>
50 #include <linux/raid/md_p.h>
51 #include <linux/raid/md_u.h>
52 #include <linux/slab.h>
53 #include "md.h"
54 #include "bitmap.h"
56 #define DEBUG 0
57 #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);
71 #define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
74 * Default number of read corrections we'll attempt on an rdev
75 * before ejecting it from the array. We divide the read error
76 * count by 2 for every hour elapsed between read errors.
78 #define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
80 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
81 * is 1000 KB/sec, so the extra system load does not show up that much.
82 * Increase it if you want to have more _guaranteed_ speed. Note that
83 * the RAID driver will use the maximum available bandwidth if the IO
84 * subsystem is idle. There is also an 'absolute maximum' reconstruction
85 * speed limit - in case reconstruction slows down your system despite
86 * idle IO detection.
88 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
89 * or /sys/block/mdX/md/sync_speed_{min,max}
92 static int sysctl_speed_limit_min = 1000;
93 static int sysctl_speed_limit_max = 200000;
94 static inline int speed_min(mddev_t *mddev)
96 return mddev->sync_speed_min ?
97 mddev->sync_speed_min : sysctl_speed_limit_min;
100 static inline int speed_max(mddev_t *mddev)
102 return mddev->sync_speed_max ?
103 mddev->sync_speed_max : sysctl_speed_limit_max;
106 static struct ctl_table_header *raid_table_header;
108 static ctl_table raid_table[] = {
110 .procname = "speed_limit_min",
111 .data = &sysctl_speed_limit_min,
112 .maxlen = sizeof(int),
113 .mode = S_IRUGO|S_IWUSR,
114 .proc_handler = proc_dointvec,
117 .procname = "speed_limit_max",
118 .data = &sysctl_speed_limit_max,
119 .maxlen = sizeof(int),
120 .mode = S_IRUGO|S_IWUSR,
121 .proc_handler = proc_dointvec,
126 static ctl_table raid_dir_table[] = {
128 .procname = "raid",
129 .maxlen = 0,
130 .mode = S_IRUGO|S_IXUGO,
131 .child = raid_table,
136 static ctl_table raid_root_table[] = {
138 .procname = "dev",
139 .maxlen = 0,
140 .mode = 0555,
141 .child = raid_dir_table,
146 static const struct block_device_operations md_fops;
148 static int start_readonly;
151 * We have a system wide 'event count' that is incremented
152 * on any 'interesting' event, and readers of /proc/mdstat
153 * can use 'poll' or 'select' to find out when the event
154 * count increases.
156 * Events are:
157 * start array, stop array, error, add device, remove device,
158 * start build, activate spare
160 static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
161 static atomic_t md_event_count;
162 void md_new_event(mddev_t *mddev)
164 atomic_inc(&md_event_count);
165 wake_up(&md_event_waiters);
167 EXPORT_SYMBOL_GPL(md_new_event);
169 /* Alternate version that can be called from interrupts
170 * when calling sysfs_notify isn't needed.
172 static void md_new_event_inintr(mddev_t *mddev)
174 atomic_inc(&md_event_count);
175 wake_up(&md_event_waiters);
179 * Enables to iterate over all existing md arrays
180 * all_mddevs_lock protects this list.
182 static LIST_HEAD(all_mddevs);
183 static DEFINE_SPINLOCK(all_mddevs_lock);
187 * iterates through all used mddevs in the system.
188 * We take care to grab the all_mddevs_lock whenever navigating
189 * the list, and to always hold a refcount when unlocked.
190 * Any code which breaks out of this loop while own
191 * a reference to the current mddev and must mddev_put it.
193 #define for_each_mddev(mddev,tmp) \
195 for (({ spin_lock(&all_mddevs_lock); \
196 tmp = all_mddevs.next; \
197 mddev = NULL;}); \
198 ({ if (tmp != &all_mddevs) \
199 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
200 spin_unlock(&all_mddevs_lock); \
201 if (mddev) mddev_put(mddev); \
202 mddev = list_entry(tmp, mddev_t, all_mddevs); \
203 tmp != &all_mddevs;}); \
204 ({ spin_lock(&all_mddevs_lock); \
205 tmp = tmp->next;}) \
209 /* Rather than calling directly into the personality make_request function,
210 * IO requests come here first so that we can check if the device is
211 * being suspended pending a reconfiguration.
212 * We hold a refcount over the call to ->make_request. By the time that
213 * call has finished, the bio has been linked into some internal structure
214 * and so is visible to ->quiesce(), so we don't need the refcount any more.
216 static int md_make_request(struct request_queue *q, struct bio *bio)
218 const int rw = bio_data_dir(bio);
219 mddev_t *mddev = q->queuedata;
220 int rv;
221 int cpu;
223 if (mddev == NULL || mddev->pers == NULL) {
224 bio_io_error(bio);
225 return 0;
227 rcu_read_lock();
228 if (mddev->suspended || mddev->barrier) {
229 DEFINE_WAIT(__wait);
230 for (;;) {
231 prepare_to_wait(&mddev->sb_wait, &__wait,
232 TASK_UNINTERRUPTIBLE);
233 if (!mddev->suspended && !mddev->barrier)
234 break;
235 rcu_read_unlock();
236 schedule();
237 rcu_read_lock();
239 finish_wait(&mddev->sb_wait, &__wait);
241 atomic_inc(&mddev->active_io);
242 rcu_read_unlock();
244 rv = mddev->pers->make_request(mddev, bio);
246 cpu = part_stat_lock();
247 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
248 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw],
249 bio_sectors(bio));
250 part_stat_unlock();
252 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
253 wake_up(&mddev->sb_wait);
255 return rv;
258 /* mddev_suspend makes sure no new requests are submitted
259 * to the device, and that any requests that have been submitted
260 * are completely handled.
261 * Once ->stop is called and completes, the module will be completely
262 * unused.
264 static void mddev_suspend(mddev_t *mddev)
266 BUG_ON(mddev->suspended);
267 mddev->suspended = 1;
268 synchronize_rcu();
269 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
270 mddev->pers->quiesce(mddev, 1);
273 static void mddev_resume(mddev_t *mddev)
275 mddev->suspended = 0;
276 wake_up(&mddev->sb_wait);
277 mddev->pers->quiesce(mddev, 0);
280 int mddev_congested(mddev_t *mddev, int bits)
282 if (mddev->barrier)
283 return 1;
284 return mddev->suspended;
286 EXPORT_SYMBOL(mddev_congested);
289 * Generic barrier handling for md
292 #define POST_REQUEST_BARRIER ((void*)1)
294 static void md_end_barrier(struct bio *bio, int err)
296 mdk_rdev_t *rdev = bio->bi_private;
297 mddev_t *mddev = rdev->mddev;
298 if (err == -EOPNOTSUPP && mddev->barrier != POST_REQUEST_BARRIER)
299 set_bit(BIO_EOPNOTSUPP, &mddev->barrier->bi_flags);
301 rdev_dec_pending(rdev, mddev);
303 if (atomic_dec_and_test(&mddev->flush_pending)) {
304 if (mddev->barrier == POST_REQUEST_BARRIER) {
305 /* This was a post-request barrier */
306 mddev->barrier = NULL;
307 wake_up(&mddev->sb_wait);
308 } else
309 /* The pre-request barrier has finished */
310 schedule_work(&mddev->barrier_work);
312 bio_put(bio);
315 static void submit_barriers(mddev_t *mddev)
317 mdk_rdev_t *rdev;
319 rcu_read_lock();
320 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
321 if (rdev->raid_disk >= 0 &&
322 !test_bit(Faulty, &rdev->flags)) {
323 /* Take two references, one is dropped
324 * when request finishes, one after
325 * we reclaim rcu_read_lock
327 struct bio *bi;
328 atomic_inc(&rdev->nr_pending);
329 atomic_inc(&rdev->nr_pending);
330 rcu_read_unlock();
331 bi = bio_alloc(GFP_KERNEL, 0);
332 bi->bi_end_io = md_end_barrier;
333 bi->bi_private = rdev;
334 bi->bi_bdev = rdev->bdev;
335 atomic_inc(&mddev->flush_pending);
336 submit_bio(WRITE_BARRIER, bi);
337 rcu_read_lock();
338 rdev_dec_pending(rdev, mddev);
340 rcu_read_unlock();
343 static void md_submit_barrier(struct work_struct *ws)
345 mddev_t *mddev = container_of(ws, mddev_t, barrier_work);
346 struct bio *bio = mddev->barrier;
348 atomic_set(&mddev->flush_pending, 1);
350 if (test_bit(BIO_EOPNOTSUPP, &bio->bi_flags))
351 bio_endio(bio, -EOPNOTSUPP);
352 else if (bio->bi_size == 0)
353 /* an empty barrier - all done */
354 bio_endio(bio, 0);
355 else {
356 bio->bi_rw &= ~(1<<BIO_RW_BARRIER);
357 if (mddev->pers->make_request(mddev, bio))
358 generic_make_request(bio);
359 mddev->barrier = POST_REQUEST_BARRIER;
360 submit_barriers(mddev);
362 if (atomic_dec_and_test(&mddev->flush_pending)) {
363 mddev->barrier = NULL;
364 wake_up(&mddev->sb_wait);
368 void md_barrier_request(mddev_t *mddev, struct bio *bio)
370 spin_lock_irq(&mddev->write_lock);
371 wait_event_lock_irq(mddev->sb_wait,
372 !mddev->barrier,
373 mddev->write_lock, /*nothing*/);
374 mddev->barrier = bio;
375 spin_unlock_irq(&mddev->write_lock);
377 atomic_set(&mddev->flush_pending, 1);
378 INIT_WORK(&mddev->barrier_work, md_submit_barrier);
380 submit_barriers(mddev);
382 if (atomic_dec_and_test(&mddev->flush_pending))
383 schedule_work(&mddev->barrier_work);
385 EXPORT_SYMBOL(md_barrier_request);
387 static inline mddev_t *mddev_get(mddev_t *mddev)
389 atomic_inc(&mddev->active);
390 return mddev;
393 static void mddev_delayed_delete(struct work_struct *ws);
395 static void mddev_put(mddev_t *mddev)
397 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
398 return;
399 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
400 mddev->ctime == 0 && !mddev->hold_active) {
401 /* Array is not configured at all, and not held active,
402 * so destroy it */
403 list_del(&mddev->all_mddevs);
404 if (mddev->gendisk) {
405 /* we did a probe so need to clean up.
406 * Call schedule_work inside the spinlock
407 * so that flush_scheduled_work() after
408 * mddev_find will succeed in waiting for the
409 * work to be done.
411 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
412 schedule_work(&mddev->del_work);
413 } else
414 kfree(mddev);
416 spin_unlock(&all_mddevs_lock);
419 static void mddev_init(mddev_t *mddev)
421 mutex_init(&mddev->open_mutex);
422 mutex_init(&mddev->reconfig_mutex);
423 mutex_init(&mddev->bitmap_info.mutex);
424 INIT_LIST_HEAD(&mddev->disks);
425 INIT_LIST_HEAD(&mddev->all_mddevs);
426 init_timer(&mddev->safemode_timer);
427 atomic_set(&mddev->active, 1);
428 atomic_set(&mddev->openers, 0);
429 atomic_set(&mddev->active_io, 0);
430 spin_lock_init(&mddev->write_lock);
431 atomic_set(&mddev->flush_pending, 0);
432 init_waitqueue_head(&mddev->sb_wait);
433 init_waitqueue_head(&mddev->recovery_wait);
434 mddev->reshape_position = MaxSector;
435 mddev->resync_min = 0;
436 mddev->resync_max = MaxSector;
437 mddev->level = LEVEL_NONE;
440 static mddev_t * mddev_find(dev_t unit)
442 mddev_t *mddev, *new = NULL;
444 retry:
445 spin_lock(&all_mddevs_lock);
447 if (unit) {
448 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
449 if (mddev->unit == unit) {
450 mddev_get(mddev);
451 spin_unlock(&all_mddevs_lock);
452 kfree(new);
453 return mddev;
456 if (new) {
457 list_add(&new->all_mddevs, &all_mddevs);
458 spin_unlock(&all_mddevs_lock);
459 new->hold_active = UNTIL_IOCTL;
460 return new;
462 } else if (new) {
463 /* find an unused unit number */
464 static int next_minor = 512;
465 int start = next_minor;
466 int is_free = 0;
467 int dev = 0;
468 while (!is_free) {
469 dev = MKDEV(MD_MAJOR, next_minor);
470 next_minor++;
471 if (next_minor > MINORMASK)
472 next_minor = 0;
473 if (next_minor == start) {
474 /* Oh dear, all in use. */
475 spin_unlock(&all_mddevs_lock);
476 kfree(new);
477 return NULL;
480 is_free = 1;
481 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
482 if (mddev->unit == dev) {
483 is_free = 0;
484 break;
487 new->unit = dev;
488 new->md_minor = MINOR(dev);
489 new->hold_active = UNTIL_STOP;
490 list_add(&new->all_mddevs, &all_mddevs);
491 spin_unlock(&all_mddevs_lock);
492 return new;
494 spin_unlock(&all_mddevs_lock);
496 new = kzalloc(sizeof(*new), GFP_KERNEL);
497 if (!new)
498 return NULL;
500 new->unit = unit;
501 if (MAJOR(unit) == MD_MAJOR)
502 new->md_minor = MINOR(unit);
503 else
504 new->md_minor = MINOR(unit) >> MdpMinorShift;
506 mddev_init(new);
508 goto retry;
511 static inline int mddev_lock(mddev_t * mddev)
513 return mutex_lock_interruptible(&mddev->reconfig_mutex);
516 static inline int mddev_is_locked(mddev_t *mddev)
518 return mutex_is_locked(&mddev->reconfig_mutex);
521 static inline int mddev_trylock(mddev_t * mddev)
523 return mutex_trylock(&mddev->reconfig_mutex);
526 static struct attribute_group md_redundancy_group;
528 static void mddev_unlock(mddev_t * mddev)
530 if (mddev->to_remove) {
531 /* These cannot be removed under reconfig_mutex as
532 * an access to the files will try to take reconfig_mutex
533 * while holding the file unremovable, which leads to
534 * a deadlock.
535 * So hold open_mutex instead - we are allowed to take
536 * it while holding reconfig_mutex, and md_run can
537 * use it to wait for the remove to complete.
539 struct attribute_group *to_remove = mddev->to_remove;
540 mddev->to_remove = NULL;
541 mutex_lock(&mddev->open_mutex);
542 mutex_unlock(&mddev->reconfig_mutex);
544 if (to_remove != &md_redundancy_group)
545 sysfs_remove_group(&mddev->kobj, to_remove);
546 if (mddev->pers == NULL ||
547 mddev->pers->sync_request == NULL) {
548 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
549 if (mddev->sysfs_action)
550 sysfs_put(mddev->sysfs_action);
551 mddev->sysfs_action = NULL;
553 mutex_unlock(&mddev->open_mutex);
554 } else
555 mutex_unlock(&mddev->reconfig_mutex);
557 md_wakeup_thread(mddev->thread);
560 static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
562 mdk_rdev_t *rdev;
564 list_for_each_entry(rdev, &mddev->disks, same_set)
565 if (rdev->desc_nr == nr)
566 return rdev;
568 return NULL;
571 static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
573 mdk_rdev_t *rdev;
575 list_for_each_entry(rdev, &mddev->disks, same_set)
576 if (rdev->bdev->bd_dev == dev)
577 return rdev;
579 return NULL;
582 static struct mdk_personality *find_pers(int level, char *clevel)
584 struct mdk_personality *pers;
585 list_for_each_entry(pers, &pers_list, list) {
586 if (level != LEVEL_NONE && pers->level == level)
587 return pers;
588 if (strcmp(pers->name, clevel)==0)
589 return pers;
591 return NULL;
594 /* return the offset of the super block in 512byte sectors */
595 static inline sector_t calc_dev_sboffset(struct block_device *bdev)
597 sector_t num_sectors = bdev->bd_inode->i_size / 512;
598 return MD_NEW_SIZE_SECTORS(num_sectors);
601 static int alloc_disk_sb(mdk_rdev_t * rdev)
603 if (rdev->sb_page)
604 MD_BUG();
606 rdev->sb_page = alloc_page(GFP_KERNEL);
607 if (!rdev->sb_page) {
608 printk(KERN_ALERT "md: out of memory.\n");
609 return -ENOMEM;
612 return 0;
615 static void free_disk_sb(mdk_rdev_t * rdev)
617 if (rdev->sb_page) {
618 put_page(rdev->sb_page);
619 rdev->sb_loaded = 0;
620 rdev->sb_page = NULL;
621 rdev->sb_start = 0;
622 rdev->sectors = 0;
627 static void super_written(struct bio *bio, int error)
629 mdk_rdev_t *rdev = bio->bi_private;
630 mddev_t *mddev = rdev->mddev;
632 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
633 printk("md: super_written gets error=%d, uptodate=%d\n",
634 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
635 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
636 md_error(mddev, rdev);
639 if (atomic_dec_and_test(&mddev->pending_writes))
640 wake_up(&mddev->sb_wait);
641 bio_put(bio);
644 static void super_written_barrier(struct bio *bio, int error)
646 struct bio *bio2 = bio->bi_private;
647 mdk_rdev_t *rdev = bio2->bi_private;
648 mddev_t *mddev = rdev->mddev;
650 if (!test_bit(BIO_UPTODATE, &bio->bi_flags) &&
651 error == -EOPNOTSUPP) {
652 unsigned long flags;
653 /* barriers don't appear to be supported :-( */
654 set_bit(BarriersNotsupp, &rdev->flags);
655 mddev->barriers_work = 0;
656 spin_lock_irqsave(&mddev->write_lock, flags);
657 bio2->bi_next = mddev->biolist;
658 mddev->biolist = bio2;
659 spin_unlock_irqrestore(&mddev->write_lock, flags);
660 wake_up(&mddev->sb_wait);
661 bio_put(bio);
662 } else {
663 bio_put(bio2);
664 bio->bi_private = rdev;
665 super_written(bio, error);
669 void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
670 sector_t sector, int size, struct page *page)
672 /* write first size bytes of page to sector of rdev
673 * Increment mddev->pending_writes before returning
674 * and decrement it on completion, waking up sb_wait
675 * if zero is reached.
676 * If an error occurred, call md_error
678 * As we might need to resubmit the request if BIO_RW_BARRIER
679 * causes ENOTSUPP, we allocate a spare bio...
681 struct bio *bio = bio_alloc(GFP_NOIO, 1);
682 int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNCIO) | (1<<BIO_RW_UNPLUG);
684 bio->bi_bdev = rdev->bdev;
685 bio->bi_sector = sector;
686 bio_add_page(bio, page, size, 0);
687 bio->bi_private = rdev;
688 bio->bi_end_io = super_written;
689 bio->bi_rw = rw;
691 atomic_inc(&mddev->pending_writes);
692 if (!test_bit(BarriersNotsupp, &rdev->flags)) {
693 struct bio *rbio;
694 rw |= (1<<BIO_RW_BARRIER);
695 rbio = bio_clone(bio, GFP_NOIO);
696 rbio->bi_private = bio;
697 rbio->bi_end_io = super_written_barrier;
698 submit_bio(rw, rbio);
699 } else
700 submit_bio(rw, bio);
703 void md_super_wait(mddev_t *mddev)
705 /* wait for all superblock writes that were scheduled to complete.
706 * if any had to be retried (due to BARRIER problems), retry them
708 DEFINE_WAIT(wq);
709 for(;;) {
710 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
711 if (atomic_read(&mddev->pending_writes)==0)
712 break;
713 while (mddev->biolist) {
714 struct bio *bio;
715 spin_lock_irq(&mddev->write_lock);
716 bio = mddev->biolist;
717 mddev->biolist = bio->bi_next ;
718 bio->bi_next = NULL;
719 spin_unlock_irq(&mddev->write_lock);
720 submit_bio(bio->bi_rw, bio);
722 schedule();
724 finish_wait(&mddev->sb_wait, &wq);
727 static void bi_complete(struct bio *bio, int error)
729 complete((struct completion*)bio->bi_private);
732 int sync_page_io(struct block_device *bdev, sector_t sector, int size,
733 struct page *page, int rw)
735 struct bio *bio = bio_alloc(GFP_NOIO, 1);
736 struct completion event;
737 int ret;
739 rw |= (1 << BIO_RW_SYNCIO) | (1 << BIO_RW_UNPLUG);
741 bio->bi_bdev = bdev;
742 bio->bi_sector = sector;
743 bio_add_page(bio, page, size, 0);
744 init_completion(&event);
745 bio->bi_private = &event;
746 bio->bi_end_io = bi_complete;
747 submit_bio(rw, bio);
748 wait_for_completion(&event);
750 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
751 bio_put(bio);
752 return ret;
754 EXPORT_SYMBOL_GPL(sync_page_io);
756 static int read_disk_sb(mdk_rdev_t * rdev, int size)
758 char b[BDEVNAME_SIZE];
759 if (!rdev->sb_page) {
760 MD_BUG();
761 return -EINVAL;
763 if (rdev->sb_loaded)
764 return 0;
767 if (!sync_page_io(rdev->bdev, rdev->sb_start, size, rdev->sb_page, READ))
768 goto fail;
769 rdev->sb_loaded = 1;
770 return 0;
772 fail:
773 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
774 bdevname(rdev->bdev,b));
775 return -EINVAL;
778 static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
780 return sb1->set_uuid0 == sb2->set_uuid0 &&
781 sb1->set_uuid1 == sb2->set_uuid1 &&
782 sb1->set_uuid2 == sb2->set_uuid2 &&
783 sb1->set_uuid3 == sb2->set_uuid3;
786 static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
788 int ret;
789 mdp_super_t *tmp1, *tmp2;
791 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
792 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
794 if (!tmp1 || !tmp2) {
795 ret = 0;
796 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
797 goto abort;
800 *tmp1 = *sb1;
801 *tmp2 = *sb2;
804 * nr_disks is not constant
806 tmp1->nr_disks = 0;
807 tmp2->nr_disks = 0;
809 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
810 abort:
811 kfree(tmp1);
812 kfree(tmp2);
813 return ret;
817 static u32 md_csum_fold(u32 csum)
819 csum = (csum & 0xffff) + (csum >> 16);
820 return (csum & 0xffff) + (csum >> 16);
823 static unsigned int calc_sb_csum(mdp_super_t * sb)
825 u64 newcsum = 0;
826 u32 *sb32 = (u32*)sb;
827 int i;
828 unsigned int disk_csum, csum;
830 disk_csum = sb->sb_csum;
831 sb->sb_csum = 0;
833 for (i = 0; i < MD_SB_BYTES/4 ; i++)
834 newcsum += sb32[i];
835 csum = (newcsum & 0xffffffff) + (newcsum>>32);
838 #ifdef CONFIG_ALPHA
839 /* This used to use csum_partial, which was wrong for several
840 * reasons including that different results are returned on
841 * different architectures. It isn't critical that we get exactly
842 * the same return value as before (we always csum_fold before
843 * testing, and that removes any differences). However as we
844 * know that csum_partial always returned a 16bit value on
845 * alphas, do a fold to maximise conformity to previous behaviour.
847 sb->sb_csum = md_csum_fold(disk_csum);
848 #else
849 sb->sb_csum = disk_csum;
850 #endif
851 return csum;
856 * Handle superblock details.
857 * We want to be able to handle multiple superblock formats
858 * so we have a common interface to them all, and an array of
859 * different handlers.
860 * We rely on user-space to write the initial superblock, and support
861 * reading and updating of superblocks.
862 * Interface methods are:
863 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
864 * loads and validates a superblock on dev.
865 * if refdev != NULL, compare superblocks on both devices
866 * Return:
867 * 0 - dev has a superblock that is compatible with refdev
868 * 1 - dev has a superblock that is compatible and newer than refdev
869 * so dev should be used as the refdev in future
870 * -EINVAL superblock incompatible or invalid
871 * -othererror e.g. -EIO
873 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
874 * Verify that dev is acceptable into mddev.
875 * The first time, mddev->raid_disks will be 0, and data from
876 * dev should be merged in. Subsequent calls check that dev
877 * is new enough. Return 0 or -EINVAL
879 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
880 * Update the superblock for rdev with data in mddev
881 * This does not write to disc.
885 struct super_type {
886 char *name;
887 struct module *owner;
888 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev,
889 int minor_version);
890 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
891 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
892 unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev,
893 sector_t num_sectors);
897 * Check that the given mddev has no bitmap.
899 * This function is called from the run method of all personalities that do not
900 * support bitmaps. It prints an error message and returns non-zero if mddev
901 * has a bitmap. Otherwise, it returns 0.
904 int md_check_no_bitmap(mddev_t *mddev)
906 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
907 return 0;
908 printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
909 mdname(mddev), mddev->pers->name);
910 return 1;
912 EXPORT_SYMBOL(md_check_no_bitmap);
915 * load_super for 0.90.0
917 static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
919 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
920 mdp_super_t *sb;
921 int ret;
924 * Calculate the position of the superblock (512byte sectors),
925 * it's at the end of the disk.
927 * It also happens to be a multiple of 4Kb.
929 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
931 ret = read_disk_sb(rdev, MD_SB_BYTES);
932 if (ret) return ret;
934 ret = -EINVAL;
936 bdevname(rdev->bdev, b);
937 sb = (mdp_super_t*)page_address(rdev->sb_page);
939 if (sb->md_magic != MD_SB_MAGIC) {
940 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
942 goto abort;
945 if (sb->major_version != 0 ||
946 sb->minor_version < 90 ||
947 sb->minor_version > 91) {
948 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
949 sb->major_version, sb->minor_version,
951 goto abort;
954 if (sb->raid_disks <= 0)
955 goto abort;
957 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
958 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
960 goto abort;
963 rdev->preferred_minor = sb->md_minor;
964 rdev->data_offset = 0;
965 rdev->sb_size = MD_SB_BYTES;
967 if (sb->level == LEVEL_MULTIPATH)
968 rdev->desc_nr = -1;
969 else
970 rdev->desc_nr = sb->this_disk.number;
972 if (!refdev) {
973 ret = 1;
974 } else {
975 __u64 ev1, ev2;
976 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
977 if (!uuid_equal(refsb, sb)) {
978 printk(KERN_WARNING "md: %s has different UUID to %s\n",
979 b, bdevname(refdev->bdev,b2));
980 goto abort;
982 if (!sb_equal(refsb, sb)) {
983 printk(KERN_WARNING "md: %s has same UUID"
984 " but different superblock to %s\n",
985 b, bdevname(refdev->bdev, b2));
986 goto abort;
988 ev1 = md_event(sb);
989 ev2 = md_event(refsb);
990 if (ev1 > ev2)
991 ret = 1;
992 else
993 ret = 0;
995 rdev->sectors = rdev->sb_start;
997 if (rdev->sectors < sb->size * 2 && sb->level > 1)
998 /* "this cannot possibly happen" ... */
999 ret = -EINVAL;
1001 abort:
1002 return ret;
1006 * validate_super for 0.90.0
1008 static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1010 mdp_disk_t *desc;
1011 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
1012 __u64 ev1 = md_event(sb);
1014 rdev->raid_disk = -1;
1015 clear_bit(Faulty, &rdev->flags);
1016 clear_bit(In_sync, &rdev->flags);
1017 clear_bit(WriteMostly, &rdev->flags);
1018 clear_bit(BarriersNotsupp, &rdev->flags);
1020 if (mddev->raid_disks == 0) {
1021 mddev->major_version = 0;
1022 mddev->minor_version = sb->minor_version;
1023 mddev->patch_version = sb->patch_version;
1024 mddev->external = 0;
1025 mddev->chunk_sectors = sb->chunk_size >> 9;
1026 mddev->ctime = sb->ctime;
1027 mddev->utime = sb->utime;
1028 mddev->level = sb->level;
1029 mddev->clevel[0] = 0;
1030 mddev->layout = sb->layout;
1031 mddev->raid_disks = sb->raid_disks;
1032 mddev->dev_sectors = sb->size * 2;
1033 mddev->events = ev1;
1034 mddev->bitmap_info.offset = 0;
1035 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
1037 if (mddev->minor_version >= 91) {
1038 mddev->reshape_position = sb->reshape_position;
1039 mddev->delta_disks = sb->delta_disks;
1040 mddev->new_level = sb->new_level;
1041 mddev->new_layout = sb->new_layout;
1042 mddev->new_chunk_sectors = sb->new_chunk >> 9;
1043 } else {
1044 mddev->reshape_position = MaxSector;
1045 mddev->delta_disks = 0;
1046 mddev->new_level = mddev->level;
1047 mddev->new_layout = mddev->layout;
1048 mddev->new_chunk_sectors = mddev->chunk_sectors;
1051 if (sb->state & (1<<MD_SB_CLEAN))
1052 mddev->recovery_cp = MaxSector;
1053 else {
1054 if (sb->events_hi == sb->cp_events_hi &&
1055 sb->events_lo == sb->cp_events_lo) {
1056 mddev->recovery_cp = sb->recovery_cp;
1057 } else
1058 mddev->recovery_cp = 0;
1061 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1062 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1063 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1064 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1066 mddev->max_disks = MD_SB_DISKS;
1068 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
1069 mddev->bitmap_info.file == NULL)
1070 mddev->bitmap_info.offset =
1071 mddev->bitmap_info.default_offset;
1073 } else if (mddev->pers == NULL) {
1074 /* Insist on good event counter while assembling, except
1075 * for spares (which don't need an event count) */
1076 ++ev1;
1077 if (sb->disks[rdev->desc_nr].state & (
1078 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
1079 if (ev1 < mddev->events)
1080 return -EINVAL;
1081 } else if (mddev->bitmap) {
1082 /* if adding to array with a bitmap, then we can accept an
1083 * older device ... but not too old.
1085 if (ev1 < mddev->bitmap->events_cleared)
1086 return 0;
1087 } else {
1088 if (ev1 < mddev->events)
1089 /* just a hot-add of a new device, leave raid_disk at -1 */
1090 return 0;
1093 if (mddev->level != LEVEL_MULTIPATH) {
1094 desc = sb->disks + rdev->desc_nr;
1096 if (desc->state & (1<<MD_DISK_FAULTY))
1097 set_bit(Faulty, &rdev->flags);
1098 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1099 desc->raid_disk < mddev->raid_disks */) {
1100 set_bit(In_sync, &rdev->flags);
1101 rdev->raid_disk = desc->raid_disk;
1102 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1103 /* active but not in sync implies recovery up to
1104 * reshape position. We don't know exactly where
1105 * that is, so set to zero for now */
1106 if (mddev->minor_version >= 91) {
1107 rdev->recovery_offset = 0;
1108 rdev->raid_disk = desc->raid_disk;
1111 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1112 set_bit(WriteMostly, &rdev->flags);
1113 } else /* MULTIPATH are always insync */
1114 set_bit(In_sync, &rdev->flags);
1115 return 0;
1119 * sync_super for 0.90.0
1121 static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1123 mdp_super_t *sb;
1124 mdk_rdev_t *rdev2;
1125 int next_spare = mddev->raid_disks;
1128 /* make rdev->sb match mddev data..
1130 * 1/ zero out disks
1131 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1132 * 3/ any empty disks < next_spare become removed
1134 * disks[0] gets initialised to REMOVED because
1135 * we cannot be sure from other fields if it has
1136 * been initialised or not.
1138 int i;
1139 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1141 rdev->sb_size = MD_SB_BYTES;
1143 sb = (mdp_super_t*)page_address(rdev->sb_page);
1145 memset(sb, 0, sizeof(*sb));
1147 sb->md_magic = MD_SB_MAGIC;
1148 sb->major_version = mddev->major_version;
1149 sb->patch_version = mddev->patch_version;
1150 sb->gvalid_words = 0; /* ignored */
1151 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1152 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1153 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1154 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1156 sb->ctime = mddev->ctime;
1157 sb->level = mddev->level;
1158 sb->size = mddev->dev_sectors / 2;
1159 sb->raid_disks = mddev->raid_disks;
1160 sb->md_minor = mddev->md_minor;
1161 sb->not_persistent = 0;
1162 sb->utime = mddev->utime;
1163 sb->state = 0;
1164 sb->events_hi = (mddev->events>>32);
1165 sb->events_lo = (u32)mddev->events;
1167 if (mddev->reshape_position == MaxSector)
1168 sb->minor_version = 90;
1169 else {
1170 sb->minor_version = 91;
1171 sb->reshape_position = mddev->reshape_position;
1172 sb->new_level = mddev->new_level;
1173 sb->delta_disks = mddev->delta_disks;
1174 sb->new_layout = mddev->new_layout;
1175 sb->new_chunk = mddev->new_chunk_sectors << 9;
1177 mddev->minor_version = sb->minor_version;
1178 if (mddev->in_sync)
1180 sb->recovery_cp = mddev->recovery_cp;
1181 sb->cp_events_hi = (mddev->events>>32);
1182 sb->cp_events_lo = (u32)mddev->events;
1183 if (mddev->recovery_cp == MaxSector)
1184 sb->state = (1<< MD_SB_CLEAN);
1185 } else
1186 sb->recovery_cp = 0;
1188 sb->layout = mddev->layout;
1189 sb->chunk_size = mddev->chunk_sectors << 9;
1191 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
1192 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1194 sb->disks[0].state = (1<<MD_DISK_REMOVED);
1195 list_for_each_entry(rdev2, &mddev->disks, same_set) {
1196 mdp_disk_t *d;
1197 int desc_nr;
1198 int is_active = test_bit(In_sync, &rdev2->flags);
1200 if (rdev2->raid_disk >= 0 &&
1201 sb->minor_version >= 91)
1202 /* we have nowhere to store the recovery_offset,
1203 * but if it is not below the reshape_position,
1204 * we can piggy-back on that.
1206 is_active = 1;
1207 if (rdev2->raid_disk < 0 ||
1208 test_bit(Faulty, &rdev2->flags))
1209 is_active = 0;
1210 if (is_active)
1211 desc_nr = rdev2->raid_disk;
1212 else
1213 desc_nr = next_spare++;
1214 rdev2->desc_nr = desc_nr;
1215 d = &sb->disks[rdev2->desc_nr];
1216 nr_disks++;
1217 d->number = rdev2->desc_nr;
1218 d->major = MAJOR(rdev2->bdev->bd_dev);
1219 d->minor = MINOR(rdev2->bdev->bd_dev);
1220 if (is_active)
1221 d->raid_disk = rdev2->raid_disk;
1222 else
1223 d->raid_disk = rdev2->desc_nr; /* compatibility */
1224 if (test_bit(Faulty, &rdev2->flags))
1225 d->state = (1<<MD_DISK_FAULTY);
1226 else if (is_active) {
1227 d->state = (1<<MD_DISK_ACTIVE);
1228 if (test_bit(In_sync, &rdev2->flags))
1229 d->state |= (1<<MD_DISK_SYNC);
1230 active++;
1231 working++;
1232 } else {
1233 d->state = 0;
1234 spare++;
1235 working++;
1237 if (test_bit(WriteMostly, &rdev2->flags))
1238 d->state |= (1<<MD_DISK_WRITEMOSTLY);
1240 /* now set the "removed" and "faulty" bits on any missing devices */
1241 for (i=0 ; i < mddev->raid_disks ; i++) {
1242 mdp_disk_t *d = &sb->disks[i];
1243 if (d->state == 0 && d->number == 0) {
1244 d->number = i;
1245 d->raid_disk = i;
1246 d->state = (1<<MD_DISK_REMOVED);
1247 d->state |= (1<<MD_DISK_FAULTY);
1248 failed++;
1251 sb->nr_disks = nr_disks;
1252 sb->active_disks = active;
1253 sb->working_disks = working;
1254 sb->failed_disks = failed;
1255 sb->spare_disks = spare;
1257 sb->this_disk = sb->disks[rdev->desc_nr];
1258 sb->sb_csum = calc_sb_csum(sb);
1262 * rdev_size_change for 0.90.0
1264 static unsigned long long
1265 super_90_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
1267 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
1268 return 0; /* component must fit device */
1269 if (rdev->mddev->bitmap_info.offset)
1270 return 0; /* can't move bitmap */
1271 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
1272 if (!num_sectors || num_sectors > rdev->sb_start)
1273 num_sectors = rdev->sb_start;
1274 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
1275 rdev->sb_page);
1276 md_super_wait(rdev->mddev);
1277 return num_sectors / 2; /* kB for sysfs */
1282 * version 1 superblock
1285 static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
1287 __le32 disk_csum;
1288 u32 csum;
1289 unsigned long long newcsum;
1290 int size = 256 + le32_to_cpu(sb->max_dev)*2;
1291 __le32 *isuper = (__le32*)sb;
1292 int i;
1294 disk_csum = sb->sb_csum;
1295 sb->sb_csum = 0;
1296 newcsum = 0;
1297 for (i=0; size>=4; size -= 4 )
1298 newcsum += le32_to_cpu(*isuper++);
1300 if (size == 2)
1301 newcsum += le16_to_cpu(*(__le16*) isuper);
1303 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1304 sb->sb_csum = disk_csum;
1305 return cpu_to_le32(csum);
1308 static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
1310 struct mdp_superblock_1 *sb;
1311 int ret;
1312 sector_t sb_start;
1313 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1314 int bmask;
1317 * Calculate the position of the superblock in 512byte sectors.
1318 * It is always aligned to a 4K boundary and
1319 * depeding on minor_version, it can be:
1320 * 0: At least 8K, but less than 12K, from end of device
1321 * 1: At start of device
1322 * 2: 4K from start of device.
1324 switch(minor_version) {
1325 case 0:
1326 sb_start = rdev->bdev->bd_inode->i_size >> 9;
1327 sb_start -= 8*2;
1328 sb_start &= ~(sector_t)(4*2-1);
1329 break;
1330 case 1:
1331 sb_start = 0;
1332 break;
1333 case 2:
1334 sb_start = 8;
1335 break;
1336 default:
1337 return -EINVAL;
1339 rdev->sb_start = sb_start;
1341 /* superblock is rarely larger than 1K, but it can be larger,
1342 * and it is safe to read 4k, so we do that
1344 ret = read_disk_sb(rdev, 4096);
1345 if (ret) return ret;
1348 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1350 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1351 sb->major_version != cpu_to_le32(1) ||
1352 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
1353 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
1354 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
1355 return -EINVAL;
1357 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1358 printk("md: invalid superblock checksum on %s\n",
1359 bdevname(rdev->bdev,b));
1360 return -EINVAL;
1362 if (le64_to_cpu(sb->data_size) < 10) {
1363 printk("md: data_size too small on %s\n",
1364 bdevname(rdev->bdev,b));
1365 return -EINVAL;
1368 rdev->preferred_minor = 0xffff;
1369 rdev->data_offset = le64_to_cpu(sb->data_offset);
1370 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
1372 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
1373 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1374 if (rdev->sb_size & bmask)
1375 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1377 if (minor_version
1378 && rdev->data_offset < sb_start + (rdev->sb_size/512))
1379 return -EINVAL;
1381 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1382 rdev->desc_nr = -1;
1383 else
1384 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1386 if (!refdev) {
1387 ret = 1;
1388 } else {
1389 __u64 ev1, ev2;
1390 struct mdp_superblock_1 *refsb =
1391 (struct mdp_superblock_1*)page_address(refdev->sb_page);
1393 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1394 sb->level != refsb->level ||
1395 sb->layout != refsb->layout ||
1396 sb->chunksize != refsb->chunksize) {
1397 printk(KERN_WARNING "md: %s has strangely different"
1398 " superblock to %s\n",
1399 bdevname(rdev->bdev,b),
1400 bdevname(refdev->bdev,b2));
1401 return -EINVAL;
1403 ev1 = le64_to_cpu(sb->events);
1404 ev2 = le64_to_cpu(refsb->events);
1406 if (ev1 > ev2)
1407 ret = 1;
1408 else
1409 ret = 0;
1411 if (minor_version)
1412 rdev->sectors = (rdev->bdev->bd_inode->i_size >> 9) -
1413 le64_to_cpu(sb->data_offset);
1414 else
1415 rdev->sectors = rdev->sb_start;
1416 if (rdev->sectors < le64_to_cpu(sb->data_size))
1417 return -EINVAL;
1418 rdev->sectors = le64_to_cpu(sb->data_size);
1419 if (le64_to_cpu(sb->size) > rdev->sectors)
1420 return -EINVAL;
1421 return ret;
1424 static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1426 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1427 __u64 ev1 = le64_to_cpu(sb->events);
1429 rdev->raid_disk = -1;
1430 clear_bit(Faulty, &rdev->flags);
1431 clear_bit(In_sync, &rdev->flags);
1432 clear_bit(WriteMostly, &rdev->flags);
1433 clear_bit(BarriersNotsupp, &rdev->flags);
1435 if (mddev->raid_disks == 0) {
1436 mddev->major_version = 1;
1437 mddev->patch_version = 0;
1438 mddev->external = 0;
1439 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
1440 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1441 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1442 mddev->level = le32_to_cpu(sb->level);
1443 mddev->clevel[0] = 0;
1444 mddev->layout = le32_to_cpu(sb->layout);
1445 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
1446 mddev->dev_sectors = le64_to_cpu(sb->size);
1447 mddev->events = ev1;
1448 mddev->bitmap_info.offset = 0;
1449 mddev->bitmap_info.default_offset = 1024 >> 9;
1451 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1452 memcpy(mddev->uuid, sb->set_uuid, 16);
1454 mddev->max_disks = (4096-256)/2;
1456 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
1457 mddev->bitmap_info.file == NULL )
1458 mddev->bitmap_info.offset =
1459 (__s32)le32_to_cpu(sb->bitmap_offset);
1461 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1462 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1463 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1464 mddev->new_level = le32_to_cpu(sb->new_level);
1465 mddev->new_layout = le32_to_cpu(sb->new_layout);
1466 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
1467 } else {
1468 mddev->reshape_position = MaxSector;
1469 mddev->delta_disks = 0;
1470 mddev->new_level = mddev->level;
1471 mddev->new_layout = mddev->layout;
1472 mddev->new_chunk_sectors = mddev->chunk_sectors;
1475 } else if (mddev->pers == NULL) {
1476 /* Insist of good event counter while assembling, except for
1477 * spares (which don't need an event count) */
1478 ++ev1;
1479 if (rdev->desc_nr >= 0 &&
1480 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
1481 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe)
1482 if (ev1 < mddev->events)
1483 return -EINVAL;
1484 } else if (mddev->bitmap) {
1485 /* If adding to array with a bitmap, then we can accept an
1486 * older device, but not too old.
1488 if (ev1 < mddev->bitmap->events_cleared)
1489 return 0;
1490 } else {
1491 if (ev1 < mddev->events)
1492 /* just a hot-add of a new device, leave raid_disk at -1 */
1493 return 0;
1495 if (mddev->level != LEVEL_MULTIPATH) {
1496 int role;
1497 if (rdev->desc_nr < 0 ||
1498 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1499 role = 0xffff;
1500 rdev->desc_nr = -1;
1501 } else
1502 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
1503 switch(role) {
1504 case 0xffff: /* spare */
1505 break;
1506 case 0xfffe: /* faulty */
1507 set_bit(Faulty, &rdev->flags);
1508 break;
1509 default:
1510 if ((le32_to_cpu(sb->feature_map) &
1511 MD_FEATURE_RECOVERY_OFFSET))
1512 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1513 else
1514 set_bit(In_sync, &rdev->flags);
1515 rdev->raid_disk = role;
1516 break;
1518 if (sb->devflags & WriteMostly1)
1519 set_bit(WriteMostly, &rdev->flags);
1520 } else /* MULTIPATH are always insync */
1521 set_bit(In_sync, &rdev->flags);
1523 return 0;
1526 static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1528 struct mdp_superblock_1 *sb;
1529 mdk_rdev_t *rdev2;
1530 int max_dev, i;
1531 /* make rdev->sb match mddev and rdev data. */
1533 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1535 sb->feature_map = 0;
1536 sb->pad0 = 0;
1537 sb->recovery_offset = cpu_to_le64(0);
1538 memset(sb->pad1, 0, sizeof(sb->pad1));
1539 memset(sb->pad2, 0, sizeof(sb->pad2));
1540 memset(sb->pad3, 0, sizeof(sb->pad3));
1542 sb->utime = cpu_to_le64((__u64)mddev->utime);
1543 sb->events = cpu_to_le64(mddev->events);
1544 if (mddev->in_sync)
1545 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1546 else
1547 sb->resync_offset = cpu_to_le64(0);
1549 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
1551 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
1552 sb->size = cpu_to_le64(mddev->dev_sectors);
1553 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
1554 sb->level = cpu_to_le32(mddev->level);
1555 sb->layout = cpu_to_le32(mddev->layout);
1557 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1558 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
1559 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
1562 if (rdev->raid_disk >= 0 &&
1563 !test_bit(In_sync, &rdev->flags)) {
1564 sb->feature_map |=
1565 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1566 sb->recovery_offset =
1567 cpu_to_le64(rdev->recovery_offset);
1570 if (mddev->reshape_position != MaxSector) {
1571 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1572 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1573 sb->new_layout = cpu_to_le32(mddev->new_layout);
1574 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1575 sb->new_level = cpu_to_le32(mddev->new_level);
1576 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
1579 max_dev = 0;
1580 list_for_each_entry(rdev2, &mddev->disks, same_set)
1581 if (rdev2->desc_nr+1 > max_dev)
1582 max_dev = rdev2->desc_nr+1;
1584 if (max_dev > le32_to_cpu(sb->max_dev)) {
1585 int bmask;
1586 sb->max_dev = cpu_to_le32(max_dev);
1587 rdev->sb_size = max_dev * 2 + 256;
1588 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1589 if (rdev->sb_size & bmask)
1590 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1592 for (i=0; i<max_dev;i++)
1593 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1595 list_for_each_entry(rdev2, &mddev->disks, same_set) {
1596 i = rdev2->desc_nr;
1597 if (test_bit(Faulty, &rdev2->flags))
1598 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1599 else if (test_bit(In_sync, &rdev2->flags))
1600 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
1601 else if (rdev2->raid_disk >= 0)
1602 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
1603 else
1604 sb->dev_roles[i] = cpu_to_le16(0xffff);
1607 sb->sb_csum = calc_sb_1_csum(sb);
1610 static unsigned long long
1611 super_1_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
1613 struct mdp_superblock_1 *sb;
1614 sector_t max_sectors;
1615 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
1616 return 0; /* component must fit device */
1617 if (rdev->sb_start < rdev->data_offset) {
1618 /* minor versions 1 and 2; superblock before data */
1619 max_sectors = rdev->bdev->bd_inode->i_size >> 9;
1620 max_sectors -= rdev->data_offset;
1621 if (!num_sectors || num_sectors > max_sectors)
1622 num_sectors = max_sectors;
1623 } else if (rdev->mddev->bitmap_info.offset) {
1624 /* minor version 0 with bitmap we can't move */
1625 return 0;
1626 } else {
1627 /* minor version 0; superblock after data */
1628 sector_t sb_start;
1629 sb_start = (rdev->bdev->bd_inode->i_size >> 9) - 8*2;
1630 sb_start &= ~(sector_t)(4*2 - 1);
1631 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
1632 if (!num_sectors || num_sectors > max_sectors)
1633 num_sectors = max_sectors;
1634 rdev->sb_start = sb_start;
1636 sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page);
1637 sb->data_size = cpu_to_le64(num_sectors);
1638 sb->super_offset = rdev->sb_start;
1639 sb->sb_csum = calc_sb_1_csum(sb);
1640 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
1641 rdev->sb_page);
1642 md_super_wait(rdev->mddev);
1643 return num_sectors / 2; /* kB for sysfs */
1646 static struct super_type super_types[] = {
1647 [0] = {
1648 .name = "0.90.0",
1649 .owner = THIS_MODULE,
1650 .load_super = super_90_load,
1651 .validate_super = super_90_validate,
1652 .sync_super = super_90_sync,
1653 .rdev_size_change = super_90_rdev_size_change,
1655 [1] = {
1656 .name = "md-1",
1657 .owner = THIS_MODULE,
1658 .load_super = super_1_load,
1659 .validate_super = super_1_validate,
1660 .sync_super = super_1_sync,
1661 .rdev_size_change = super_1_rdev_size_change,
1665 static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1667 mdk_rdev_t *rdev, *rdev2;
1669 rcu_read_lock();
1670 rdev_for_each_rcu(rdev, mddev1)
1671 rdev_for_each_rcu(rdev2, mddev2)
1672 if (rdev->bdev->bd_contains ==
1673 rdev2->bdev->bd_contains) {
1674 rcu_read_unlock();
1675 return 1;
1677 rcu_read_unlock();
1678 return 0;
1681 static LIST_HEAD(pending_raid_disks);
1684 * Try to register data integrity profile for an mddev
1686 * This is called when an array is started and after a disk has been kicked
1687 * from the array. It only succeeds if all working and active component devices
1688 * are integrity capable with matching profiles.
1690 int md_integrity_register(mddev_t *mddev)
1692 mdk_rdev_t *rdev, *reference = NULL;
1694 if (list_empty(&mddev->disks))
1695 return 0; /* nothing to do */
1696 if (blk_get_integrity(mddev->gendisk))
1697 return 0; /* already registered */
1698 list_for_each_entry(rdev, &mddev->disks, same_set) {
1699 /* skip spares and non-functional disks */
1700 if (test_bit(Faulty, &rdev->flags))
1701 continue;
1702 if (rdev->raid_disk < 0)
1703 continue;
1705 * If at least one rdev is not integrity capable, we can not
1706 * enable data integrity for the md device.
1708 if (!bdev_get_integrity(rdev->bdev))
1709 return -EINVAL;
1710 if (!reference) {
1711 /* Use the first rdev as the reference */
1712 reference = rdev;
1713 continue;
1715 /* does this rdev's profile match the reference profile? */
1716 if (blk_integrity_compare(reference->bdev->bd_disk,
1717 rdev->bdev->bd_disk) < 0)
1718 return -EINVAL;
1721 * All component devices are integrity capable and have matching
1722 * profiles, register the common profile for the md device.
1724 if (blk_integrity_register(mddev->gendisk,
1725 bdev_get_integrity(reference->bdev)) != 0) {
1726 printk(KERN_ERR "md: failed to register integrity for %s\n",
1727 mdname(mddev));
1728 return -EINVAL;
1730 printk(KERN_NOTICE "md: data integrity on %s enabled\n",
1731 mdname(mddev));
1732 return 0;
1734 EXPORT_SYMBOL(md_integrity_register);
1736 /* Disable data integrity if non-capable/non-matching disk is being added */
1737 void md_integrity_add_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
1739 struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev);
1740 struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk);
1742 if (!bi_mddev) /* nothing to do */
1743 return;
1744 if (rdev->raid_disk < 0) /* skip spares */
1745 return;
1746 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
1747 rdev->bdev->bd_disk) >= 0)
1748 return;
1749 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
1750 blk_integrity_unregister(mddev->gendisk);
1752 EXPORT_SYMBOL(md_integrity_add_rdev);
1754 static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1756 char b[BDEVNAME_SIZE];
1757 struct kobject *ko;
1758 char *s;
1759 int err;
1761 if (rdev->mddev) {
1762 MD_BUG();
1763 return -EINVAL;
1766 /* prevent duplicates */
1767 if (find_rdev(mddev, rdev->bdev->bd_dev))
1768 return -EEXIST;
1770 /* make sure rdev->sectors exceeds mddev->dev_sectors */
1771 if (rdev->sectors && (mddev->dev_sectors == 0 ||
1772 rdev->sectors < mddev->dev_sectors)) {
1773 if (mddev->pers) {
1774 /* Cannot change size, so fail
1775 * If mddev->level <= 0, then we don't care
1776 * about aligning sizes (e.g. linear)
1778 if (mddev->level > 0)
1779 return -ENOSPC;
1780 } else
1781 mddev->dev_sectors = rdev->sectors;
1784 /* Verify rdev->desc_nr is unique.
1785 * If it is -1, assign a free number, else
1786 * check number is not in use
1788 if (rdev->desc_nr < 0) {
1789 int choice = 0;
1790 if (mddev->pers) choice = mddev->raid_disks;
1791 while (find_rdev_nr(mddev, choice))
1792 choice++;
1793 rdev->desc_nr = choice;
1794 } else {
1795 if (find_rdev_nr(mddev, rdev->desc_nr))
1796 return -EBUSY;
1798 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
1799 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
1800 mdname(mddev), mddev->max_disks);
1801 return -EBUSY;
1803 bdevname(rdev->bdev,b);
1804 while ( (s=strchr(b, '/')) != NULL)
1805 *s = '!';
1807 rdev->mddev = mddev;
1808 printk(KERN_INFO "md: bind<%s>\n", b);
1810 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
1811 goto fail;
1813 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
1814 if ((err = sysfs_create_link(&rdev->kobj, ko, "block"))) {
1815 kobject_del(&rdev->kobj);
1816 goto fail;
1818 rdev->sysfs_state = sysfs_get_dirent(rdev->kobj.sd, NULL, "state");
1820 list_add_rcu(&rdev->same_set, &mddev->disks);
1821 bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk);
1823 /* May as well allow recovery to be retried once */
1824 mddev->recovery_disabled = 0;
1826 return 0;
1828 fail:
1829 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
1830 b, mdname(mddev));
1831 return err;
1834 static void md_delayed_delete(struct work_struct *ws)
1836 mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work);
1837 kobject_del(&rdev->kobj);
1838 kobject_put(&rdev->kobj);
1841 static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1843 char b[BDEVNAME_SIZE];
1844 if (!rdev->mddev) {
1845 MD_BUG();
1846 return;
1848 bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk);
1849 list_del_rcu(&rdev->same_set);
1850 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1851 rdev->mddev = NULL;
1852 sysfs_remove_link(&rdev->kobj, "block");
1853 sysfs_put(rdev->sysfs_state);
1854 rdev->sysfs_state = NULL;
1855 /* We need to delay this, otherwise we can deadlock when
1856 * writing to 'remove' to "dev/state". We also need
1857 * to delay it due to rcu usage.
1859 synchronize_rcu();
1860 INIT_WORK(&rdev->del_work, md_delayed_delete);
1861 kobject_get(&rdev->kobj);
1862 schedule_work(&rdev->del_work);
1866 * prevent the device from being mounted, repartitioned or
1867 * otherwise reused by a RAID array (or any other kernel
1868 * subsystem), by bd_claiming the device.
1870 static int lock_rdev(mdk_rdev_t *rdev, dev_t dev, int shared)
1872 int err = 0;
1873 struct block_device *bdev;
1874 char b[BDEVNAME_SIZE];
1876 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
1877 if (IS_ERR(bdev)) {
1878 printk(KERN_ERR "md: could not open %s.\n",
1879 __bdevname(dev, b));
1880 return PTR_ERR(bdev);
1882 err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev);
1883 if (err) {
1884 printk(KERN_ERR "md: could not bd_claim %s.\n",
1885 bdevname(bdev, b));
1886 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
1887 return err;
1889 if (!shared)
1890 set_bit(AllReserved, &rdev->flags);
1891 rdev->bdev = bdev;
1892 return err;
1895 static void unlock_rdev(mdk_rdev_t *rdev)
1897 struct block_device *bdev = rdev->bdev;
1898 rdev->bdev = NULL;
1899 if (!bdev)
1900 MD_BUG();
1901 bd_release(bdev);
1902 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
1905 void md_autodetect_dev(dev_t dev);
1907 static void export_rdev(mdk_rdev_t * rdev)
1909 char b[BDEVNAME_SIZE];
1910 printk(KERN_INFO "md: export_rdev(%s)\n",
1911 bdevname(rdev->bdev,b));
1912 if (rdev->mddev)
1913 MD_BUG();
1914 free_disk_sb(rdev);
1915 #ifndef MODULE
1916 if (test_bit(AutoDetected, &rdev->flags))
1917 md_autodetect_dev(rdev->bdev->bd_dev);
1918 #endif
1919 unlock_rdev(rdev);
1920 kobject_put(&rdev->kobj);
1923 static void kick_rdev_from_array(mdk_rdev_t * rdev)
1925 unbind_rdev_from_array(rdev);
1926 export_rdev(rdev);
1929 static void export_array(mddev_t *mddev)
1931 mdk_rdev_t *rdev, *tmp;
1933 rdev_for_each(rdev, tmp, mddev) {
1934 if (!rdev->mddev) {
1935 MD_BUG();
1936 continue;
1938 kick_rdev_from_array(rdev);
1940 if (!list_empty(&mddev->disks))
1941 MD_BUG();
1942 mddev->raid_disks = 0;
1943 mddev->major_version = 0;
1946 static void print_desc(mdp_disk_t *desc)
1948 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1949 desc->major,desc->minor,desc->raid_disk,desc->state);
1952 static void print_sb_90(mdp_super_t *sb)
1954 int i;
1956 printk(KERN_INFO
1957 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1958 sb->major_version, sb->minor_version, sb->patch_version,
1959 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1960 sb->ctime);
1961 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1962 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1963 sb->md_minor, sb->layout, sb->chunk_size);
1964 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1965 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1966 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1967 sb->failed_disks, sb->spare_disks,
1968 sb->sb_csum, (unsigned long)sb->events_lo);
1970 printk(KERN_INFO);
1971 for (i = 0; i < MD_SB_DISKS; i++) {
1972 mdp_disk_t *desc;
1974 desc = sb->disks + i;
1975 if (desc->number || desc->major || desc->minor ||
1976 desc->raid_disk || (desc->state && (desc->state != 4))) {
1977 printk(" D %2d: ", i);
1978 print_desc(desc);
1981 printk(KERN_INFO "md: THIS: ");
1982 print_desc(&sb->this_disk);
1985 static void print_sb_1(struct mdp_superblock_1 *sb)
1987 __u8 *uuid;
1989 uuid = sb->set_uuid;
1990 printk(KERN_INFO
1991 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n"
1992 "md: Name: \"%s\" CT:%llu\n",
1993 le32_to_cpu(sb->major_version),
1994 le32_to_cpu(sb->feature_map),
1995 uuid,
1996 sb->set_name,
1997 (unsigned long long)le64_to_cpu(sb->ctime)
1998 & MD_SUPERBLOCK_1_TIME_SEC_MASK);
2000 uuid = sb->device_uuid;
2001 printk(KERN_INFO
2002 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
2003 " RO:%llu\n"
2004 "md: Dev:%08x UUID: %pU\n"
2005 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
2006 "md: (MaxDev:%u) \n",
2007 le32_to_cpu(sb->level),
2008 (unsigned long long)le64_to_cpu(sb->size),
2009 le32_to_cpu(sb->raid_disks),
2010 le32_to_cpu(sb->layout),
2011 le32_to_cpu(sb->chunksize),
2012 (unsigned long long)le64_to_cpu(sb->data_offset),
2013 (unsigned long long)le64_to_cpu(sb->data_size),
2014 (unsigned long long)le64_to_cpu(sb->super_offset),
2015 (unsigned long long)le64_to_cpu(sb->recovery_offset),
2016 le32_to_cpu(sb->dev_number),
2017 uuid,
2018 sb->devflags,
2019 (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK,
2020 (unsigned long long)le64_to_cpu(sb->events),
2021 (unsigned long long)le64_to_cpu(sb->resync_offset),
2022 le32_to_cpu(sb->sb_csum),
2023 le32_to_cpu(sb->max_dev)
2027 static void print_rdev(mdk_rdev_t *rdev, int major_version)
2029 char b[BDEVNAME_SIZE];
2030 printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
2031 bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors,
2032 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
2033 rdev->desc_nr);
2034 if (rdev->sb_loaded) {
2035 printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version);
2036 switch (major_version) {
2037 case 0:
2038 print_sb_90((mdp_super_t*)page_address(rdev->sb_page));
2039 break;
2040 case 1:
2041 print_sb_1((struct mdp_superblock_1 *)page_address(rdev->sb_page));
2042 break;
2044 } else
2045 printk(KERN_INFO "md: no rdev superblock!\n");
2048 static void md_print_devices(void)
2050 struct list_head *tmp;
2051 mdk_rdev_t *rdev;
2052 mddev_t *mddev;
2053 char b[BDEVNAME_SIZE];
2055 printk("\n");
2056 printk("md: **********************************\n");
2057 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
2058 printk("md: **********************************\n");
2059 for_each_mddev(mddev, tmp) {
2061 if (mddev->bitmap)
2062 bitmap_print_sb(mddev->bitmap);
2063 else
2064 printk("%s: ", mdname(mddev));
2065 list_for_each_entry(rdev, &mddev->disks, same_set)
2066 printk("<%s>", bdevname(rdev->bdev,b));
2067 printk("\n");
2069 list_for_each_entry(rdev, &mddev->disks, same_set)
2070 print_rdev(rdev, mddev->major_version);
2072 printk("md: **********************************\n");
2073 printk("\n");
2077 static void sync_sbs(mddev_t * mddev, int nospares)
2079 /* Update each superblock (in-memory image), but
2080 * if we are allowed to, skip spares which already
2081 * have the right event counter, or have one earlier
2082 * (which would mean they aren't being marked as dirty
2083 * with the rest of the array)
2085 mdk_rdev_t *rdev;
2087 /* First make sure individual recovery_offsets are correct */
2088 list_for_each_entry(rdev, &mddev->disks, same_set) {
2089 if (rdev->raid_disk >= 0 &&
2090 !test_bit(In_sync, &rdev->flags) &&
2091 mddev->curr_resync_completed > rdev->recovery_offset)
2092 rdev->recovery_offset = mddev->curr_resync_completed;
2095 list_for_each_entry(rdev, &mddev->disks, same_set) {
2096 if (rdev->sb_events == mddev->events ||
2097 (nospares &&
2098 rdev->raid_disk < 0 &&
2099 rdev->sb_events+1 == mddev->events)) {
2100 /* Don't update this superblock */
2101 rdev->sb_loaded = 2;
2102 } else {
2103 super_types[mddev->major_version].
2104 sync_super(mddev, rdev);
2105 rdev->sb_loaded = 1;
2110 static void md_update_sb(mddev_t * mddev, int force_change)
2112 mdk_rdev_t *rdev;
2113 int sync_req;
2114 int nospares = 0;
2116 mddev->utime = get_seconds();
2117 if (mddev->external)
2118 return;
2119 repeat:
2120 spin_lock_irq(&mddev->write_lock);
2122 set_bit(MD_CHANGE_PENDING, &mddev->flags);
2123 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2124 force_change = 1;
2125 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2126 /* just a clean<-> dirty transition, possibly leave spares alone,
2127 * though if events isn't the right even/odd, we will have to do
2128 * spares after all
2130 nospares = 1;
2131 if (force_change)
2132 nospares = 0;
2133 if (mddev->degraded)
2134 /* If the array is degraded, then skipping spares is both
2135 * dangerous and fairly pointless.
2136 * Dangerous because a device that was removed from the array
2137 * might have a event_count that still looks up-to-date,
2138 * so it can be re-added without a resync.
2139 * Pointless because if there are any spares to skip,
2140 * then a recovery will happen and soon that array won't
2141 * be degraded any more and the spare can go back to sleep then.
2143 nospares = 0;
2145 sync_req = mddev->in_sync;
2147 /* If this is just a dirty<->clean transition, and the array is clean
2148 * and 'events' is odd, we can roll back to the previous clean state */
2149 if (nospares
2150 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
2151 && mddev->can_decrease_events
2152 && mddev->events != 1) {
2153 mddev->events--;
2154 mddev->can_decrease_events = 0;
2155 } else {
2156 /* otherwise we have to go forward and ... */
2157 mddev->events ++;
2158 mddev->can_decrease_events = nospares;
2161 if (!mddev->events) {
2163 * oops, this 64-bit counter should never wrap.
2164 * Either we are in around ~1 trillion A.C., assuming
2165 * 1 reboot per second, or we have a bug:
2167 MD_BUG();
2168 mddev->events --;
2172 * do not write anything to disk if using
2173 * nonpersistent superblocks
2175 if (!mddev->persistent) {
2176 if (!mddev->external)
2177 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
2179 spin_unlock_irq(&mddev->write_lock);
2180 wake_up(&mddev->sb_wait);
2181 return;
2183 sync_sbs(mddev, nospares);
2184 spin_unlock_irq(&mddev->write_lock);
2186 dprintk(KERN_INFO
2187 "md: updating %s RAID superblock on device (in sync %d)\n",
2188 mdname(mddev),mddev->in_sync);
2190 bitmap_update_sb(mddev->bitmap);
2191 list_for_each_entry(rdev, &mddev->disks, same_set) {
2192 char b[BDEVNAME_SIZE];
2193 dprintk(KERN_INFO "md: ");
2194 if (rdev->sb_loaded != 1)
2195 continue; /* no noise on spare devices */
2196 if (test_bit(Faulty, &rdev->flags))
2197 dprintk("(skipping faulty ");
2199 dprintk("%s ", bdevname(rdev->bdev,b));
2200 if (!test_bit(Faulty, &rdev->flags)) {
2201 md_super_write(mddev,rdev,
2202 rdev->sb_start, rdev->sb_size,
2203 rdev->sb_page);
2204 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
2205 bdevname(rdev->bdev,b),
2206 (unsigned long long)rdev->sb_start);
2207 rdev->sb_events = mddev->events;
2209 } else
2210 dprintk(")\n");
2211 if (mddev->level == LEVEL_MULTIPATH)
2212 /* only need to write one superblock... */
2213 break;
2215 md_super_wait(mddev);
2216 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
2218 spin_lock_irq(&mddev->write_lock);
2219 if (mddev->in_sync != sync_req ||
2220 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
2221 /* have to write it out again */
2222 spin_unlock_irq(&mddev->write_lock);
2223 goto repeat;
2225 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
2226 spin_unlock_irq(&mddev->write_lock);
2227 wake_up(&mddev->sb_wait);
2228 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2229 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
2233 /* words written to sysfs files may, or may not, be \n terminated.
2234 * We want to accept with case. For this we use cmd_match.
2236 static int cmd_match(const char *cmd, const char *str)
2238 /* See if cmd, written into a sysfs file, matches
2239 * str. They must either be the same, or cmd can
2240 * have a trailing newline
2242 while (*cmd && *str && *cmd == *str) {
2243 cmd++;
2244 str++;
2246 if (*cmd == '\n')
2247 cmd++;
2248 if (*str || *cmd)
2249 return 0;
2250 return 1;
2253 struct rdev_sysfs_entry {
2254 struct attribute attr;
2255 ssize_t (*show)(mdk_rdev_t *, char *);
2256 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
2259 static ssize_t
2260 state_show(mdk_rdev_t *rdev, char *page)
2262 char *sep = "";
2263 size_t len = 0;
2265 if (test_bit(Faulty, &rdev->flags)) {
2266 len+= sprintf(page+len, "%sfaulty",sep);
2267 sep = ",";
2269 if (test_bit(In_sync, &rdev->flags)) {
2270 len += sprintf(page+len, "%sin_sync",sep);
2271 sep = ",";
2273 if (test_bit(WriteMostly, &rdev->flags)) {
2274 len += sprintf(page+len, "%swrite_mostly",sep);
2275 sep = ",";
2277 if (test_bit(Blocked, &rdev->flags)) {
2278 len += sprintf(page+len, "%sblocked", sep);
2279 sep = ",";
2281 if (!test_bit(Faulty, &rdev->flags) &&
2282 !test_bit(In_sync, &rdev->flags)) {
2283 len += sprintf(page+len, "%sspare", sep);
2284 sep = ",";
2286 return len+sprintf(page+len, "\n");
2289 static ssize_t
2290 state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2292 /* can write
2293 * faulty - simulates and error
2294 * remove - disconnects the device
2295 * writemostly - sets write_mostly
2296 * -writemostly - clears write_mostly
2297 * blocked - sets the Blocked flag
2298 * -blocked - clears the Blocked flag
2299 * insync - sets Insync providing device isn't active
2301 int err = -EINVAL;
2302 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2303 md_error(rdev->mddev, rdev);
2304 err = 0;
2305 } else if (cmd_match(buf, "remove")) {
2306 if (rdev->raid_disk >= 0)
2307 err = -EBUSY;
2308 else {
2309 mddev_t *mddev = rdev->mddev;
2310 kick_rdev_from_array(rdev);
2311 if (mddev->pers)
2312 md_update_sb(mddev, 1);
2313 md_new_event(mddev);
2314 err = 0;
2316 } else if (cmd_match(buf, "writemostly")) {
2317 set_bit(WriteMostly, &rdev->flags);
2318 err = 0;
2319 } else if (cmd_match(buf, "-writemostly")) {
2320 clear_bit(WriteMostly, &rdev->flags);
2321 err = 0;
2322 } else if (cmd_match(buf, "blocked")) {
2323 set_bit(Blocked, &rdev->flags);
2324 err = 0;
2325 } else if (cmd_match(buf, "-blocked")) {
2326 clear_bit(Blocked, &rdev->flags);
2327 wake_up(&rdev->blocked_wait);
2328 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2329 md_wakeup_thread(rdev->mddev->thread);
2331 err = 0;
2332 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2333 set_bit(In_sync, &rdev->flags);
2334 err = 0;
2336 if (!err && rdev->sysfs_state)
2337 sysfs_notify_dirent(rdev->sysfs_state);
2338 return err ? err : len;
2340 static struct rdev_sysfs_entry rdev_state =
2341 __ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
2343 static ssize_t
2344 errors_show(mdk_rdev_t *rdev, char *page)
2346 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2349 static ssize_t
2350 errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2352 char *e;
2353 unsigned long n = simple_strtoul(buf, &e, 10);
2354 if (*buf && (*e == 0 || *e == '\n')) {
2355 atomic_set(&rdev->corrected_errors, n);
2356 return len;
2358 return -EINVAL;
2360 static struct rdev_sysfs_entry rdev_errors =
2361 __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
2363 static ssize_t
2364 slot_show(mdk_rdev_t *rdev, char *page)
2366 if (rdev->raid_disk < 0)
2367 return sprintf(page, "none\n");
2368 else
2369 return sprintf(page, "%d\n", rdev->raid_disk);
2372 static ssize_t
2373 slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2375 char *e;
2376 int err;
2377 char nm[20];
2378 int slot = simple_strtoul(buf, &e, 10);
2379 if (strncmp(buf, "none", 4)==0)
2380 slot = -1;
2381 else if (e==buf || (*e && *e!= '\n'))
2382 return -EINVAL;
2383 if (rdev->mddev->pers && slot == -1) {
2384 /* Setting 'slot' on an active array requires also
2385 * updating the 'rd%d' link, and communicating
2386 * with the personality with ->hot_*_disk.
2387 * For now we only support removing
2388 * failed/spare devices. This normally happens automatically,
2389 * but not when the metadata is externally managed.
2391 if (rdev->raid_disk == -1)
2392 return -EEXIST;
2393 /* personality does all needed checks */
2394 if (rdev->mddev->pers->hot_add_disk == NULL)
2395 return -EINVAL;
2396 err = rdev->mddev->pers->
2397 hot_remove_disk(rdev->mddev, rdev->raid_disk);
2398 if (err)
2399 return err;
2400 sprintf(nm, "rd%d", rdev->raid_disk);
2401 sysfs_remove_link(&rdev->mddev->kobj, nm);
2402 rdev->raid_disk = -1;
2403 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2404 md_wakeup_thread(rdev->mddev->thread);
2405 } else if (rdev->mddev->pers) {
2406 mdk_rdev_t *rdev2;
2407 /* Activating a spare .. or possibly reactivating
2408 * if we ever get bitmaps working here.
2411 if (rdev->raid_disk != -1)
2412 return -EBUSY;
2414 if (rdev->mddev->pers->hot_add_disk == NULL)
2415 return -EINVAL;
2417 list_for_each_entry(rdev2, &rdev->mddev->disks, same_set)
2418 if (rdev2->raid_disk == slot)
2419 return -EEXIST;
2421 rdev->raid_disk = slot;
2422 if (test_bit(In_sync, &rdev->flags))
2423 rdev->saved_raid_disk = slot;
2424 else
2425 rdev->saved_raid_disk = -1;
2426 err = rdev->mddev->pers->
2427 hot_add_disk(rdev->mddev, rdev);
2428 if (err) {
2429 rdev->raid_disk = -1;
2430 return err;
2431 } else
2432 sysfs_notify_dirent(rdev->sysfs_state);
2433 sprintf(nm, "rd%d", rdev->raid_disk);
2434 if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm))
2435 printk(KERN_WARNING
2436 "md: cannot register "
2437 "%s for %s\n",
2438 nm, mdname(rdev->mddev));
2440 /* don't wakeup anyone, leave that to userspace. */
2441 } else {
2442 if (slot >= rdev->mddev->raid_disks)
2443 return -ENOSPC;
2444 rdev->raid_disk = slot;
2445 /* assume it is working */
2446 clear_bit(Faulty, &rdev->flags);
2447 clear_bit(WriteMostly, &rdev->flags);
2448 set_bit(In_sync, &rdev->flags);
2449 sysfs_notify_dirent(rdev->sysfs_state);
2451 return len;
2455 static struct rdev_sysfs_entry rdev_slot =
2456 __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
2458 static ssize_t
2459 offset_show(mdk_rdev_t *rdev, char *page)
2461 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
2464 static ssize_t
2465 offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2467 char *e;
2468 unsigned long long offset = simple_strtoull(buf, &e, 10);
2469 if (e==buf || (*e && *e != '\n'))
2470 return -EINVAL;
2471 if (rdev->mddev->pers && rdev->raid_disk >= 0)
2472 return -EBUSY;
2473 if (rdev->sectors && rdev->mddev->external)
2474 /* Must set offset before size, so overlap checks
2475 * can be sane */
2476 return -EBUSY;
2477 rdev->data_offset = offset;
2478 return len;
2481 static struct rdev_sysfs_entry rdev_offset =
2482 __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
2484 static ssize_t
2485 rdev_size_show(mdk_rdev_t *rdev, char *page)
2487 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
2490 static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2492 /* check if two start/length pairs overlap */
2493 if (s1+l1 <= s2)
2494 return 0;
2495 if (s2+l2 <= s1)
2496 return 0;
2497 return 1;
2500 static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2502 unsigned long long blocks;
2503 sector_t new;
2505 if (strict_strtoull(buf, 10, &blocks) < 0)
2506 return -EINVAL;
2508 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2509 return -EINVAL; /* sector conversion overflow */
2511 new = blocks * 2;
2512 if (new != blocks * 2)
2513 return -EINVAL; /* unsigned long long to sector_t overflow */
2515 *sectors = new;
2516 return 0;
2519 static ssize_t
2520 rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2522 mddev_t *my_mddev = rdev->mddev;
2523 sector_t oldsectors = rdev->sectors;
2524 sector_t sectors;
2526 if (strict_blocks_to_sectors(buf, &sectors) < 0)
2527 return -EINVAL;
2528 if (my_mddev->pers && rdev->raid_disk >= 0) {
2529 if (my_mddev->persistent) {
2530 sectors = super_types[my_mddev->major_version].
2531 rdev_size_change(rdev, sectors);
2532 if (!sectors)
2533 return -EBUSY;
2534 } else if (!sectors)
2535 sectors = (rdev->bdev->bd_inode->i_size >> 9) -
2536 rdev->data_offset;
2538 if (sectors < my_mddev->dev_sectors)
2539 return -EINVAL; /* component must fit device */
2541 rdev->sectors = sectors;
2542 if (sectors > oldsectors && my_mddev->external) {
2543 /* need to check that all other rdevs with the same ->bdev
2544 * do not overlap. We need to unlock the mddev to avoid
2545 * a deadlock. We have already changed rdev->sectors, and if
2546 * we have to change it back, we will have the lock again.
2548 mddev_t *mddev;
2549 int overlap = 0;
2550 struct list_head *tmp;
2552 mddev_unlock(my_mddev);
2553 for_each_mddev(mddev, tmp) {
2554 mdk_rdev_t *rdev2;
2556 mddev_lock(mddev);
2557 list_for_each_entry(rdev2, &mddev->disks, same_set)
2558 if (test_bit(AllReserved, &rdev2->flags) ||
2559 (rdev->bdev == rdev2->bdev &&
2560 rdev != rdev2 &&
2561 overlaps(rdev->data_offset, rdev->sectors,
2562 rdev2->data_offset,
2563 rdev2->sectors))) {
2564 overlap = 1;
2565 break;
2567 mddev_unlock(mddev);
2568 if (overlap) {
2569 mddev_put(mddev);
2570 break;
2573 mddev_lock(my_mddev);
2574 if (overlap) {
2575 /* Someone else could have slipped in a size
2576 * change here, but doing so is just silly.
2577 * We put oldsectors back because we *know* it is
2578 * safe, and trust userspace not to race with
2579 * itself
2581 rdev->sectors = oldsectors;
2582 return -EBUSY;
2585 return len;
2588 static struct rdev_sysfs_entry rdev_size =
2589 __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
2592 static ssize_t recovery_start_show(mdk_rdev_t *rdev, char *page)
2594 unsigned long long recovery_start = rdev->recovery_offset;
2596 if (test_bit(In_sync, &rdev->flags) ||
2597 recovery_start == MaxSector)
2598 return sprintf(page, "none\n");
2600 return sprintf(page, "%llu\n", recovery_start);
2603 static ssize_t recovery_start_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2605 unsigned long long recovery_start;
2607 if (cmd_match(buf, "none"))
2608 recovery_start = MaxSector;
2609 else if (strict_strtoull(buf, 10, &recovery_start))
2610 return -EINVAL;
2612 if (rdev->mddev->pers &&
2613 rdev->raid_disk >= 0)
2614 return -EBUSY;
2616 rdev->recovery_offset = recovery_start;
2617 if (recovery_start == MaxSector)
2618 set_bit(In_sync, &rdev->flags);
2619 else
2620 clear_bit(In_sync, &rdev->flags);
2621 return len;
2624 static struct rdev_sysfs_entry rdev_recovery_start =
2625 __ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
2627 static struct attribute *rdev_default_attrs[] = {
2628 &rdev_state.attr,
2629 &rdev_errors.attr,
2630 &rdev_slot.attr,
2631 &rdev_offset.attr,
2632 &rdev_size.attr,
2633 &rdev_recovery_start.attr,
2634 NULL,
2636 static ssize_t
2637 rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2639 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2640 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
2641 mddev_t *mddev = rdev->mddev;
2642 ssize_t rv;
2644 if (!entry->show)
2645 return -EIO;
2647 rv = mddev ? mddev_lock(mddev) : -EBUSY;
2648 if (!rv) {
2649 if (rdev->mddev == NULL)
2650 rv = -EBUSY;
2651 else
2652 rv = entry->show(rdev, page);
2653 mddev_unlock(mddev);
2655 return rv;
2658 static ssize_t
2659 rdev_attr_store(struct kobject *kobj, struct attribute *attr,
2660 const char *page, size_t length)
2662 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2663 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
2664 ssize_t rv;
2665 mddev_t *mddev = rdev->mddev;
2667 if (!entry->store)
2668 return -EIO;
2669 if (!capable(CAP_SYS_ADMIN))
2670 return -EACCES;
2671 rv = mddev ? mddev_lock(mddev): -EBUSY;
2672 if (!rv) {
2673 if (rdev->mddev == NULL)
2674 rv = -EBUSY;
2675 else
2676 rv = entry->store(rdev, page, length);
2677 mddev_unlock(mddev);
2679 return rv;
2682 static void rdev_free(struct kobject *ko)
2684 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
2685 kfree(rdev);
2687 static const struct sysfs_ops rdev_sysfs_ops = {
2688 .show = rdev_attr_show,
2689 .store = rdev_attr_store,
2691 static struct kobj_type rdev_ktype = {
2692 .release = rdev_free,
2693 .sysfs_ops = &rdev_sysfs_ops,
2694 .default_attrs = rdev_default_attrs,
2698 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2700 * mark the device faulty if:
2702 * - the device is nonexistent (zero size)
2703 * - the device has no valid superblock
2705 * a faulty rdev _never_ has rdev->sb set.
2707 static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
2709 char b[BDEVNAME_SIZE];
2710 int err;
2711 mdk_rdev_t *rdev;
2712 sector_t size;
2714 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
2715 if (!rdev) {
2716 printk(KERN_ERR "md: could not alloc mem for new device!\n");
2717 return ERR_PTR(-ENOMEM);
2720 if ((err = alloc_disk_sb(rdev)))
2721 goto abort_free;
2723 err = lock_rdev(rdev, newdev, super_format == -2);
2724 if (err)
2725 goto abort_free;
2727 kobject_init(&rdev->kobj, &rdev_ktype);
2729 rdev->desc_nr = -1;
2730 rdev->saved_raid_disk = -1;
2731 rdev->raid_disk = -1;
2732 rdev->flags = 0;
2733 rdev->data_offset = 0;
2734 rdev->sb_events = 0;
2735 rdev->last_read_error.tv_sec = 0;
2736 rdev->last_read_error.tv_nsec = 0;
2737 atomic_set(&rdev->nr_pending, 0);
2738 atomic_set(&rdev->read_errors, 0);
2739 atomic_set(&rdev->corrected_errors, 0);
2741 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2742 if (!size) {
2743 printk(KERN_WARNING
2744 "md: %s has zero or unknown size, marking faulty!\n",
2745 bdevname(rdev->bdev,b));
2746 err = -EINVAL;
2747 goto abort_free;
2750 if (super_format >= 0) {
2751 err = super_types[super_format].
2752 load_super(rdev, NULL, super_minor);
2753 if (err == -EINVAL) {
2754 printk(KERN_WARNING
2755 "md: %s does not have a valid v%d.%d "
2756 "superblock, not importing!\n",
2757 bdevname(rdev->bdev,b),
2758 super_format, super_minor);
2759 goto abort_free;
2761 if (err < 0) {
2762 printk(KERN_WARNING
2763 "md: could not read %s's sb, not importing!\n",
2764 bdevname(rdev->bdev,b));
2765 goto abort_free;
2769 INIT_LIST_HEAD(&rdev->same_set);
2770 init_waitqueue_head(&rdev->blocked_wait);
2772 return rdev;
2774 abort_free:
2775 if (rdev->sb_page) {
2776 if (rdev->bdev)
2777 unlock_rdev(rdev);
2778 free_disk_sb(rdev);
2780 kfree(rdev);
2781 return ERR_PTR(err);
2785 * Check a full RAID array for plausibility
2789 static void analyze_sbs(mddev_t * mddev)
2791 int i;
2792 mdk_rdev_t *rdev, *freshest, *tmp;
2793 char b[BDEVNAME_SIZE];
2795 freshest = NULL;
2796 rdev_for_each(rdev, tmp, mddev)
2797 switch (super_types[mddev->major_version].
2798 load_super(rdev, freshest, mddev->minor_version)) {
2799 case 1:
2800 freshest = rdev;
2801 break;
2802 case 0:
2803 break;
2804 default:
2805 printk( KERN_ERR \
2806 "md: fatal superblock inconsistency in %s"
2807 " -- removing from array\n",
2808 bdevname(rdev->bdev,b));
2809 kick_rdev_from_array(rdev);
2813 super_types[mddev->major_version].
2814 validate_super(mddev, freshest);
2816 i = 0;
2817 rdev_for_each(rdev, tmp, mddev) {
2818 if (mddev->max_disks &&
2819 (rdev->desc_nr >= mddev->max_disks ||
2820 i > mddev->max_disks)) {
2821 printk(KERN_WARNING
2822 "md: %s: %s: only %d devices permitted\n",
2823 mdname(mddev), bdevname(rdev->bdev, b),
2824 mddev->max_disks);
2825 kick_rdev_from_array(rdev);
2826 continue;
2828 if (rdev != freshest)
2829 if (super_types[mddev->major_version].
2830 validate_super(mddev, rdev)) {
2831 printk(KERN_WARNING "md: kicking non-fresh %s"
2832 " from array!\n",
2833 bdevname(rdev->bdev,b));
2834 kick_rdev_from_array(rdev);
2835 continue;
2837 if (mddev->level == LEVEL_MULTIPATH) {
2838 rdev->desc_nr = i++;
2839 rdev->raid_disk = rdev->desc_nr;
2840 set_bit(In_sync, &rdev->flags);
2841 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
2842 rdev->raid_disk = -1;
2843 clear_bit(In_sync, &rdev->flags);
2848 /* Read a fixed-point number.
2849 * Numbers in sysfs attributes should be in "standard" units where
2850 * possible, so time should be in seconds.
2851 * However we internally use a a much smaller unit such as
2852 * milliseconds or jiffies.
2853 * This function takes a decimal number with a possible fractional
2854 * component, and produces an integer which is the result of
2855 * multiplying that number by 10^'scale'.
2856 * all without any floating-point arithmetic.
2858 int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
2860 unsigned long result = 0;
2861 long decimals = -1;
2862 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
2863 if (*cp == '.')
2864 decimals = 0;
2865 else if (decimals < scale) {
2866 unsigned int value;
2867 value = *cp - '0';
2868 result = result * 10 + value;
2869 if (decimals >= 0)
2870 decimals++;
2872 cp++;
2874 if (*cp == '\n')
2875 cp++;
2876 if (*cp)
2877 return -EINVAL;
2878 if (decimals < 0)
2879 decimals = 0;
2880 while (decimals < scale) {
2881 result *= 10;
2882 decimals ++;
2884 *res = result;
2885 return 0;
2889 static void md_safemode_timeout(unsigned long data);
2891 static ssize_t
2892 safe_delay_show(mddev_t *mddev, char *page)
2894 int msec = (mddev->safemode_delay*1000)/HZ;
2895 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
2897 static ssize_t
2898 safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
2900 unsigned long msec;
2902 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
2903 return -EINVAL;
2904 if (msec == 0)
2905 mddev->safemode_delay = 0;
2906 else {
2907 unsigned long old_delay = mddev->safemode_delay;
2908 mddev->safemode_delay = (msec*HZ)/1000;
2909 if (mddev->safemode_delay == 0)
2910 mddev->safemode_delay = 1;
2911 if (mddev->safemode_delay < old_delay)
2912 md_safemode_timeout((unsigned long)mddev);
2914 return len;
2916 static struct md_sysfs_entry md_safe_delay =
2917 __ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
2919 static ssize_t
2920 level_show(mddev_t *mddev, char *page)
2922 struct mdk_personality *p = mddev->pers;
2923 if (p)
2924 return sprintf(page, "%s\n", p->name);
2925 else if (mddev->clevel[0])
2926 return sprintf(page, "%s\n", mddev->clevel);
2927 else if (mddev->level != LEVEL_NONE)
2928 return sprintf(page, "%d\n", mddev->level);
2929 else
2930 return 0;
2933 static ssize_t
2934 level_store(mddev_t *mddev, const char *buf, size_t len)
2936 char clevel[16];
2937 ssize_t rv = len;
2938 struct mdk_personality *pers;
2939 long level;
2940 void *priv;
2941 mdk_rdev_t *rdev;
2943 if (mddev->pers == NULL) {
2944 if (len == 0)
2945 return 0;
2946 if (len >= sizeof(mddev->clevel))
2947 return -ENOSPC;
2948 strncpy(mddev->clevel, buf, len);
2949 if (mddev->clevel[len-1] == '\n')
2950 len--;
2951 mddev->clevel[len] = 0;
2952 mddev->level = LEVEL_NONE;
2953 return rv;
2956 /* request to change the personality. Need to ensure:
2957 * - array is not engaged in resync/recovery/reshape
2958 * - old personality can be suspended
2959 * - new personality will access other array.
2962 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
2963 return -EBUSY;
2965 if (!mddev->pers->quiesce) {
2966 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
2967 mdname(mddev), mddev->pers->name);
2968 return -EINVAL;
2971 /* Now find the new personality */
2972 if (len == 0 || len >= sizeof(clevel))
2973 return -EINVAL;
2974 strncpy(clevel, buf, len);
2975 if (clevel[len-1] == '\n')
2976 len--;
2977 clevel[len] = 0;
2978 if (strict_strtol(clevel, 10, &level))
2979 level = LEVEL_NONE;
2981 if (request_module("md-%s", clevel) != 0)
2982 request_module("md-level-%s", clevel);
2983 spin_lock(&pers_lock);
2984 pers = find_pers(level, clevel);
2985 if (!pers || !try_module_get(pers->owner)) {
2986 spin_unlock(&pers_lock);
2987 printk(KERN_WARNING "md: personality %s not loaded\n", clevel);
2988 return -EINVAL;
2990 spin_unlock(&pers_lock);
2992 if (pers == mddev->pers) {
2993 /* Nothing to do! */
2994 module_put(pers->owner);
2995 return rv;
2997 if (!pers->takeover) {
2998 module_put(pers->owner);
2999 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
3000 mdname(mddev), clevel);
3001 return -EINVAL;
3004 /* ->takeover must set new_* and/or delta_disks
3005 * if it succeeds, and may set them when it fails.
3007 priv = pers->takeover(mddev);
3008 if (IS_ERR(priv)) {
3009 mddev->new_level = mddev->level;
3010 mddev->new_layout = mddev->layout;
3011 mddev->new_chunk_sectors = mddev->chunk_sectors;
3012 mddev->raid_disks -= mddev->delta_disks;
3013 mddev->delta_disks = 0;
3014 module_put(pers->owner);
3015 printk(KERN_WARNING "md: %s: %s would not accept array\n",
3016 mdname(mddev), clevel);
3017 return PTR_ERR(priv);
3020 /* Looks like we have a winner */
3021 mddev_suspend(mddev);
3022 mddev->pers->stop(mddev);
3024 if (mddev->pers->sync_request == NULL &&
3025 pers->sync_request != NULL) {
3026 /* need to add the md_redundancy_group */
3027 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3028 printk(KERN_WARNING
3029 "md: cannot register extra attributes for %s\n",
3030 mdname(mddev));
3031 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, NULL, "sync_action");
3033 if (mddev->pers->sync_request != NULL &&
3034 pers->sync_request == NULL) {
3035 /* need to remove the md_redundancy_group */
3036 if (mddev->to_remove == NULL)
3037 mddev->to_remove = &md_redundancy_group;
3040 if (mddev->pers->sync_request == NULL &&
3041 mddev->external) {
3042 /* We are converting from a no-redundancy array
3043 * to a redundancy array and metadata is managed
3044 * externally so we need to be sure that writes
3045 * won't block due to a need to transition
3046 * clean->dirty
3047 * until external management is started.
3049 mddev->in_sync = 0;
3050 mddev->safemode_delay = 0;
3051 mddev->safemode = 0;
3054 module_put(mddev->pers->owner);
3055 /* Invalidate devices that are now superfluous */
3056 list_for_each_entry(rdev, &mddev->disks, same_set)
3057 if (rdev->raid_disk >= mddev->raid_disks) {
3058 rdev->raid_disk = -1;
3059 clear_bit(In_sync, &rdev->flags);
3061 mddev->pers = pers;
3062 mddev->private = priv;
3063 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3064 mddev->level = mddev->new_level;
3065 mddev->layout = mddev->new_layout;
3066 mddev->chunk_sectors = mddev->new_chunk_sectors;
3067 mddev->delta_disks = 0;
3068 if (mddev->pers->sync_request == NULL) {
3069 /* this is now an array without redundancy, so
3070 * it must always be in_sync
3072 mddev->in_sync = 1;
3073 del_timer_sync(&mddev->safemode_timer);
3075 pers->run(mddev);
3076 mddev_resume(mddev);
3077 set_bit(MD_CHANGE_DEVS, &mddev->flags);
3078 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3079 md_wakeup_thread(mddev->thread);
3080 sysfs_notify(&mddev->kobj, NULL, "level");
3081 md_new_event(mddev);
3082 return rv;
3085 static struct md_sysfs_entry md_level =
3086 __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
3089 static ssize_t
3090 layout_show(mddev_t *mddev, char *page)
3092 /* just a number, not meaningful for all levels */
3093 if (mddev->reshape_position != MaxSector &&
3094 mddev->layout != mddev->new_layout)
3095 return sprintf(page, "%d (%d)\n",
3096 mddev->new_layout, mddev->layout);
3097 return sprintf(page, "%d\n", mddev->layout);
3100 static ssize_t
3101 layout_store(mddev_t *mddev, const char *buf, size_t len)
3103 char *e;
3104 unsigned long n = simple_strtoul(buf, &e, 10);
3106 if (!*buf || (*e && *e != '\n'))
3107 return -EINVAL;
3109 if (mddev->pers) {
3110 int err;
3111 if (mddev->pers->check_reshape == NULL)
3112 return -EBUSY;
3113 mddev->new_layout = n;
3114 err = mddev->pers->check_reshape(mddev);
3115 if (err) {
3116 mddev->new_layout = mddev->layout;
3117 return err;
3119 } else {
3120 mddev->new_layout = n;
3121 if (mddev->reshape_position == MaxSector)
3122 mddev->layout = n;
3124 return len;
3126 static struct md_sysfs_entry md_layout =
3127 __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
3130 static ssize_t
3131 raid_disks_show(mddev_t *mddev, char *page)
3133 if (mddev->raid_disks == 0)
3134 return 0;
3135 if (mddev->reshape_position != MaxSector &&
3136 mddev->delta_disks != 0)
3137 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3138 mddev->raid_disks - mddev->delta_disks);
3139 return sprintf(page, "%d\n", mddev->raid_disks);
3142 static int update_raid_disks(mddev_t *mddev, int raid_disks);
3144 static ssize_t
3145 raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
3147 char *e;
3148 int rv = 0;
3149 unsigned long n = simple_strtoul(buf, &e, 10);
3151 if (!*buf || (*e && *e != '\n'))
3152 return -EINVAL;
3154 if (mddev->pers)
3155 rv = update_raid_disks(mddev, n);
3156 else if (mddev->reshape_position != MaxSector) {
3157 int olddisks = mddev->raid_disks - mddev->delta_disks;
3158 mddev->delta_disks = n - olddisks;
3159 mddev->raid_disks = n;
3160 } else
3161 mddev->raid_disks = n;
3162 return rv ? rv : len;
3164 static struct md_sysfs_entry md_raid_disks =
3165 __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
3167 static ssize_t
3168 chunk_size_show(mddev_t *mddev, char *page)
3170 if (mddev->reshape_position != MaxSector &&
3171 mddev->chunk_sectors != mddev->new_chunk_sectors)
3172 return sprintf(page, "%d (%d)\n",
3173 mddev->new_chunk_sectors << 9,
3174 mddev->chunk_sectors << 9);
3175 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
3178 static ssize_t
3179 chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
3181 char *e;
3182 unsigned long n = simple_strtoul(buf, &e, 10);
3184 if (!*buf || (*e && *e != '\n'))
3185 return -EINVAL;
3187 if (mddev->pers) {
3188 int err;
3189 if (mddev->pers->check_reshape == NULL)
3190 return -EBUSY;
3191 mddev->new_chunk_sectors = n >> 9;
3192 err = mddev->pers->check_reshape(mddev);
3193 if (err) {
3194 mddev->new_chunk_sectors = mddev->chunk_sectors;
3195 return err;
3197 } else {
3198 mddev->new_chunk_sectors = n >> 9;
3199 if (mddev->reshape_position == MaxSector)
3200 mddev->chunk_sectors = n >> 9;
3202 return len;
3204 static struct md_sysfs_entry md_chunk_size =
3205 __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
3207 static ssize_t
3208 resync_start_show(mddev_t *mddev, char *page)
3210 if (mddev->recovery_cp == MaxSector)
3211 return sprintf(page, "none\n");
3212 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3215 static ssize_t
3216 resync_start_store(mddev_t *mddev, const char *buf, size_t len)
3218 char *e;
3219 unsigned long long n = simple_strtoull(buf, &e, 10);
3221 if (mddev->pers)
3222 return -EBUSY;
3223 if (cmd_match(buf, "none"))
3224 n = MaxSector;
3225 else if (!*buf || (*e && *e != '\n'))
3226 return -EINVAL;
3228 mddev->recovery_cp = n;
3229 return len;
3231 static struct md_sysfs_entry md_resync_start =
3232 __ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
3235 * The array state can be:
3237 * clear
3238 * No devices, no size, no level
3239 * Equivalent to STOP_ARRAY ioctl
3240 * inactive
3241 * May have some settings, but array is not active
3242 * all IO results in error
3243 * When written, doesn't tear down array, but just stops it
3244 * suspended (not supported yet)
3245 * All IO requests will block. The array can be reconfigured.
3246 * Writing this, if accepted, will block until array is quiescent
3247 * readonly
3248 * no resync can happen. no superblocks get written.
3249 * write requests fail
3250 * read-auto
3251 * like readonly, but behaves like 'clean' on a write request.
3253 * clean - no pending writes, but otherwise active.
3254 * When written to inactive array, starts without resync
3255 * If a write request arrives then
3256 * if metadata is known, mark 'dirty' and switch to 'active'.
3257 * if not known, block and switch to write-pending
3258 * If written to an active array that has pending writes, then fails.
3259 * active
3260 * fully active: IO and resync can be happening.
3261 * When written to inactive array, starts with resync
3263 * write-pending
3264 * clean, but writes are blocked waiting for 'active' to be written.
3266 * active-idle
3267 * like active, but no writes have been seen for a while (100msec).
3270 enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3271 write_pending, active_idle, bad_word};
3272 static char *array_states[] = {
3273 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3274 "write-pending", "active-idle", NULL };
3276 static int match_word(const char *word, char **list)
3278 int n;
3279 for (n=0; list[n]; n++)
3280 if (cmd_match(word, list[n]))
3281 break;
3282 return n;
3285 static ssize_t
3286 array_state_show(mddev_t *mddev, char *page)
3288 enum array_state st = inactive;
3290 if (mddev->pers)
3291 switch(mddev->ro) {
3292 case 1:
3293 st = readonly;
3294 break;
3295 case 2:
3296 st = read_auto;
3297 break;
3298 case 0:
3299 if (mddev->in_sync)
3300 st = clean;
3301 else if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
3302 st = write_pending;
3303 else if (mddev->safemode)
3304 st = active_idle;
3305 else
3306 st = active;
3308 else {
3309 if (list_empty(&mddev->disks) &&
3310 mddev->raid_disks == 0 &&
3311 mddev->dev_sectors == 0)
3312 st = clear;
3313 else
3314 st = inactive;
3316 return sprintf(page, "%s\n", array_states[st]);
3319 static int do_md_stop(mddev_t * mddev, int ro, int is_open);
3320 static int md_set_readonly(mddev_t * mddev, int is_open);
3321 static int do_md_run(mddev_t * mddev);
3322 static int restart_array(mddev_t *mddev);
3324 static ssize_t
3325 array_state_store(mddev_t *mddev, const char *buf, size_t len)
3327 int err = -EINVAL;
3328 enum array_state st = match_word(buf, array_states);
3329 switch(st) {
3330 case bad_word:
3331 break;
3332 case clear:
3333 /* stopping an active array */
3334 if (atomic_read(&mddev->openers) > 0)
3335 return -EBUSY;
3336 err = do_md_stop(mddev, 0, 0);
3337 break;
3338 case inactive:
3339 /* stopping an active array */
3340 if (mddev->pers) {
3341 if (atomic_read(&mddev->openers) > 0)
3342 return -EBUSY;
3343 err = do_md_stop(mddev, 2, 0);
3344 } else
3345 err = 0; /* already inactive */
3346 break;
3347 case suspended:
3348 break; /* not supported yet */
3349 case readonly:
3350 if (mddev->pers)
3351 err = md_set_readonly(mddev, 0);
3352 else {
3353 mddev->ro = 1;
3354 set_disk_ro(mddev->gendisk, 1);
3355 err = do_md_run(mddev);
3357 break;
3358 case read_auto:
3359 if (mddev->pers) {
3360 if (mddev->ro == 0)
3361 err = md_set_readonly(mddev, 0);
3362 else if (mddev->ro == 1)
3363 err = restart_array(mddev);
3364 if (err == 0) {
3365 mddev->ro = 2;
3366 set_disk_ro(mddev->gendisk, 0);
3368 } else {
3369 mddev->ro = 2;
3370 err = do_md_run(mddev);
3372 break;
3373 case clean:
3374 if (mddev->pers) {
3375 restart_array(mddev);
3376 spin_lock_irq(&mddev->write_lock);
3377 if (atomic_read(&mddev->writes_pending) == 0) {
3378 if (mddev->in_sync == 0) {
3379 mddev->in_sync = 1;
3380 if (mddev->safemode == 1)
3381 mddev->safemode = 0;
3382 if (mddev->persistent)
3383 set_bit(MD_CHANGE_CLEAN,
3384 &mddev->flags);
3386 err = 0;
3387 } else
3388 err = -EBUSY;
3389 spin_unlock_irq(&mddev->write_lock);
3390 } else
3391 err = -EINVAL;
3392 break;
3393 case active:
3394 if (mddev->pers) {
3395 restart_array(mddev);
3396 if (mddev->external)
3397 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
3398 wake_up(&mddev->sb_wait);
3399 err = 0;
3400 } else {
3401 mddev->ro = 0;
3402 set_disk_ro(mddev->gendisk, 0);
3403 err = do_md_run(mddev);
3405 break;
3406 case write_pending:
3407 case active_idle:
3408 /* these cannot be set */
3409 break;
3411 if (err)
3412 return err;
3413 else {
3414 sysfs_notify_dirent(mddev->sysfs_state);
3415 return len;
3418 static struct md_sysfs_entry md_array_state =
3419 __ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
3421 static ssize_t
3422 max_corrected_read_errors_show(mddev_t *mddev, char *page) {
3423 return sprintf(page, "%d\n",
3424 atomic_read(&mddev->max_corr_read_errors));
3427 static ssize_t
3428 max_corrected_read_errors_store(mddev_t *mddev, const char *buf, size_t len)
3430 char *e;
3431 unsigned long n = simple_strtoul(buf, &e, 10);
3433 if (*buf && (*e == 0 || *e == '\n')) {
3434 atomic_set(&mddev->max_corr_read_errors, n);
3435 return len;
3437 return -EINVAL;
3440 static struct md_sysfs_entry max_corr_read_errors =
3441 __ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
3442 max_corrected_read_errors_store);
3444 static ssize_t
3445 null_show(mddev_t *mddev, char *page)
3447 return -EINVAL;
3450 static ssize_t
3451 new_dev_store(mddev_t *mddev, const char *buf, size_t len)
3453 /* buf must be %d:%d\n? giving major and minor numbers */
3454 /* The new device is added to the array.
3455 * If the array has a persistent superblock, we read the
3456 * superblock to initialise info and check validity.
3457 * Otherwise, only checking done is that in bind_rdev_to_array,
3458 * which mainly checks size.
3460 char *e;
3461 int major = simple_strtoul(buf, &e, 10);
3462 int minor;
3463 dev_t dev;
3464 mdk_rdev_t *rdev;
3465 int err;
3467 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
3468 return -EINVAL;
3469 minor = simple_strtoul(e+1, &e, 10);
3470 if (*e && *e != '\n')
3471 return -EINVAL;
3472 dev = MKDEV(major, minor);
3473 if (major != MAJOR(dev) ||
3474 minor != MINOR(dev))
3475 return -EOVERFLOW;
3478 if (mddev->persistent) {
3479 rdev = md_import_device(dev, mddev->major_version,
3480 mddev->minor_version);
3481 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
3482 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
3483 mdk_rdev_t, same_set);
3484 err = super_types[mddev->major_version]
3485 .load_super(rdev, rdev0, mddev->minor_version);
3486 if (err < 0)
3487 goto out;
3489 } else if (mddev->external)
3490 rdev = md_import_device(dev, -2, -1);
3491 else
3492 rdev = md_import_device(dev, -1, -1);
3494 if (IS_ERR(rdev))
3495 return PTR_ERR(rdev);
3496 err = bind_rdev_to_array(rdev, mddev);
3497 out:
3498 if (err)
3499 export_rdev(rdev);
3500 return err ? err : len;
3503 static struct md_sysfs_entry md_new_device =
3504 __ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
3506 static ssize_t
3507 bitmap_store(mddev_t *mddev, const char *buf, size_t len)
3509 char *end;
3510 unsigned long chunk, end_chunk;
3512 if (!mddev->bitmap)
3513 goto out;
3514 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3515 while (*buf) {
3516 chunk = end_chunk = simple_strtoul(buf, &end, 0);
3517 if (buf == end) break;
3518 if (*end == '-') { /* range */
3519 buf = end + 1;
3520 end_chunk = simple_strtoul(buf, &end, 0);
3521 if (buf == end) break;
3523 if (*end && !isspace(*end)) break;
3524 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
3525 buf = skip_spaces(end);
3527 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
3528 out:
3529 return len;
3532 static struct md_sysfs_entry md_bitmap =
3533 __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
3535 static ssize_t
3536 size_show(mddev_t *mddev, char *page)
3538 return sprintf(page, "%llu\n",
3539 (unsigned long long)mddev->dev_sectors / 2);
3542 static int update_size(mddev_t *mddev, sector_t num_sectors);
3544 static ssize_t
3545 size_store(mddev_t *mddev, const char *buf, size_t len)
3547 /* If array is inactive, we can reduce the component size, but
3548 * not increase it (except from 0).
3549 * If array is active, we can try an on-line resize
3551 sector_t sectors;
3552 int err = strict_blocks_to_sectors(buf, &sectors);
3554 if (err < 0)
3555 return err;
3556 if (mddev->pers) {
3557 err = update_size(mddev, sectors);
3558 md_update_sb(mddev, 1);
3559 } else {
3560 if (mddev->dev_sectors == 0 ||
3561 mddev->dev_sectors > sectors)
3562 mddev->dev_sectors = sectors;
3563 else
3564 err = -ENOSPC;
3566 return err ? err : len;
3569 static struct md_sysfs_entry md_size =
3570 __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
3573 /* Metdata version.
3574 * This is one of
3575 * 'none' for arrays with no metadata (good luck...)
3576 * 'external' for arrays with externally managed metadata,
3577 * or N.M for internally known formats
3579 static ssize_t
3580 metadata_show(mddev_t *mddev, char *page)
3582 if (mddev->persistent)
3583 return sprintf(page, "%d.%d\n",
3584 mddev->major_version, mddev->minor_version);
3585 else if (mddev->external)
3586 return sprintf(page, "external:%s\n", mddev->metadata_type);
3587 else
3588 return sprintf(page, "none\n");
3591 static ssize_t
3592 metadata_store(mddev_t *mddev, const char *buf, size_t len)
3594 int major, minor;
3595 char *e;
3596 /* Changing the details of 'external' metadata is
3597 * always permitted. Otherwise there must be
3598 * no devices attached to the array.
3600 if (mddev->external && strncmp(buf, "external:", 9) == 0)
3602 else if (!list_empty(&mddev->disks))
3603 return -EBUSY;
3605 if (cmd_match(buf, "none")) {
3606 mddev->persistent = 0;
3607 mddev->external = 0;
3608 mddev->major_version = 0;
3609 mddev->minor_version = 90;
3610 return len;
3612 if (strncmp(buf, "external:", 9) == 0) {
3613 size_t namelen = len-9;
3614 if (namelen >= sizeof(mddev->metadata_type))
3615 namelen = sizeof(mddev->metadata_type)-1;
3616 strncpy(mddev->metadata_type, buf+9, namelen);
3617 mddev->metadata_type[namelen] = 0;
3618 if (namelen && mddev->metadata_type[namelen-1] == '\n')
3619 mddev->metadata_type[--namelen] = 0;
3620 mddev->persistent = 0;
3621 mddev->external = 1;
3622 mddev->major_version = 0;
3623 mddev->minor_version = 90;
3624 return len;
3626 major = simple_strtoul(buf, &e, 10);
3627 if (e==buf || *e != '.')
3628 return -EINVAL;
3629 buf = e+1;
3630 minor = simple_strtoul(buf, &e, 10);
3631 if (e==buf || (*e && *e != '\n') )
3632 return -EINVAL;
3633 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
3634 return -ENOENT;
3635 mddev->major_version = major;
3636 mddev->minor_version = minor;
3637 mddev->persistent = 1;
3638 mddev->external = 0;
3639 return len;
3642 static struct md_sysfs_entry md_metadata =
3643 __ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
3645 static ssize_t
3646 action_show(mddev_t *mddev, char *page)
3648 char *type = "idle";
3649 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
3650 type = "frozen";
3651 else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3652 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
3653 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
3654 type = "reshape";
3655 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
3656 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
3657 type = "resync";
3658 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
3659 type = "check";
3660 else
3661 type = "repair";
3662 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
3663 type = "recover";
3665 return sprintf(page, "%s\n", type);
3668 static ssize_t
3669 action_store(mddev_t *mddev, const char *page, size_t len)
3671 if (!mddev->pers || !mddev->pers->sync_request)
3672 return -EINVAL;
3674 if (cmd_match(page, "frozen"))
3675 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3676 else
3677 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3679 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
3680 if (mddev->sync_thread) {
3681 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3682 md_unregister_thread(mddev->sync_thread);
3683 mddev->sync_thread = NULL;
3684 mddev->recovery = 0;
3686 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3687 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
3688 return -EBUSY;
3689 else if (cmd_match(page, "resync"))
3690 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3691 else if (cmd_match(page, "recover")) {
3692 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
3693 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3694 } else if (cmd_match(page, "reshape")) {
3695 int err;
3696 if (mddev->pers->start_reshape == NULL)
3697 return -EINVAL;
3698 err = mddev->pers->start_reshape(mddev);
3699 if (err)
3700 return err;
3701 sysfs_notify(&mddev->kobj, NULL, "degraded");
3702 } else {
3703 if (cmd_match(page, "check"))
3704 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
3705 else if (!cmd_match(page, "repair"))
3706 return -EINVAL;
3707 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
3708 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
3710 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3711 md_wakeup_thread(mddev->thread);
3712 sysfs_notify_dirent(mddev->sysfs_action);
3713 return len;
3716 static ssize_t
3717 mismatch_cnt_show(mddev_t *mddev, char *page)
3719 return sprintf(page, "%llu\n",
3720 (unsigned long long) mddev->resync_mismatches);
3723 static struct md_sysfs_entry md_scan_mode =
3724 __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
3727 static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
3729 static ssize_t
3730 sync_min_show(mddev_t *mddev, char *page)
3732 return sprintf(page, "%d (%s)\n", speed_min(mddev),
3733 mddev->sync_speed_min ? "local": "system");
3736 static ssize_t
3737 sync_min_store(mddev_t *mddev, const char *buf, size_t len)
3739 int min;
3740 char *e;
3741 if (strncmp(buf, "system", 6)==0) {
3742 mddev->sync_speed_min = 0;
3743 return len;
3745 min = simple_strtoul(buf, &e, 10);
3746 if (buf == e || (*e && *e != '\n') || min <= 0)
3747 return -EINVAL;
3748 mddev->sync_speed_min = min;
3749 return len;
3752 static struct md_sysfs_entry md_sync_min =
3753 __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
3755 static ssize_t
3756 sync_max_show(mddev_t *mddev, char *page)
3758 return sprintf(page, "%d (%s)\n", speed_max(mddev),
3759 mddev->sync_speed_max ? "local": "system");
3762 static ssize_t
3763 sync_max_store(mddev_t *mddev, const char *buf, size_t len)
3765 int max;
3766 char *e;
3767 if (strncmp(buf, "system", 6)==0) {
3768 mddev->sync_speed_max = 0;
3769 return len;
3771 max = simple_strtoul(buf, &e, 10);
3772 if (buf == e || (*e && *e != '\n') || max <= 0)
3773 return -EINVAL;
3774 mddev->sync_speed_max = max;
3775 return len;
3778 static struct md_sysfs_entry md_sync_max =
3779 __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
3781 static ssize_t
3782 degraded_show(mddev_t *mddev, char *page)
3784 return sprintf(page, "%d\n", mddev->degraded);
3786 static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
3788 static ssize_t
3789 sync_force_parallel_show(mddev_t *mddev, char *page)
3791 return sprintf(page, "%d\n", mddev->parallel_resync);
3794 static ssize_t
3795 sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len)
3797 long n;
3799 if (strict_strtol(buf, 10, &n))
3800 return -EINVAL;
3802 if (n != 0 && n != 1)
3803 return -EINVAL;
3805 mddev->parallel_resync = n;
3807 if (mddev->sync_thread)
3808 wake_up(&resync_wait);
3810 return len;
3813 /* force parallel resync, even with shared block devices */
3814 static struct md_sysfs_entry md_sync_force_parallel =
3815 __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
3816 sync_force_parallel_show, sync_force_parallel_store);
3818 static ssize_t
3819 sync_speed_show(mddev_t *mddev, char *page)
3821 unsigned long resync, dt, db;
3822 if (mddev->curr_resync == 0)
3823 return sprintf(page, "none\n");
3824 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
3825 dt = (jiffies - mddev->resync_mark) / HZ;
3826 if (!dt) dt++;
3827 db = resync - mddev->resync_mark_cnt;
3828 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
3831 static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
3833 static ssize_t
3834 sync_completed_show(mddev_t *mddev, char *page)
3836 unsigned long max_sectors, resync;
3838 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3839 return sprintf(page, "none\n");
3841 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
3842 max_sectors = mddev->resync_max_sectors;
3843 else
3844 max_sectors = mddev->dev_sectors;
3846 resync = mddev->curr_resync_completed;
3847 return sprintf(page, "%lu / %lu\n", resync, max_sectors);
3850 static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
3852 static ssize_t
3853 min_sync_show(mddev_t *mddev, char *page)
3855 return sprintf(page, "%llu\n",
3856 (unsigned long long)mddev->resync_min);
3858 static ssize_t
3859 min_sync_store(mddev_t *mddev, const char *buf, size_t len)
3861 unsigned long long min;
3862 if (strict_strtoull(buf, 10, &min))
3863 return -EINVAL;
3864 if (min > mddev->resync_max)
3865 return -EINVAL;
3866 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3867 return -EBUSY;
3869 /* Must be a multiple of chunk_size */
3870 if (mddev->chunk_sectors) {
3871 sector_t temp = min;
3872 if (sector_div(temp, mddev->chunk_sectors))
3873 return -EINVAL;
3875 mddev->resync_min = min;
3877 return len;
3880 static struct md_sysfs_entry md_min_sync =
3881 __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
3883 static ssize_t
3884 max_sync_show(mddev_t *mddev, char *page)
3886 if (mddev->resync_max == MaxSector)
3887 return sprintf(page, "max\n");
3888 else
3889 return sprintf(page, "%llu\n",
3890 (unsigned long long)mddev->resync_max);
3892 static ssize_t
3893 max_sync_store(mddev_t *mddev, const char *buf, size_t len)
3895 if (strncmp(buf, "max", 3) == 0)
3896 mddev->resync_max = MaxSector;
3897 else {
3898 unsigned long long max;
3899 if (strict_strtoull(buf, 10, &max))
3900 return -EINVAL;
3901 if (max < mddev->resync_min)
3902 return -EINVAL;
3903 if (max < mddev->resync_max &&
3904 mddev->ro == 0 &&
3905 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3906 return -EBUSY;
3908 /* Must be a multiple of chunk_size */
3909 if (mddev->chunk_sectors) {
3910 sector_t temp = max;
3911 if (sector_div(temp, mddev->chunk_sectors))
3912 return -EINVAL;
3914 mddev->resync_max = max;
3916 wake_up(&mddev->recovery_wait);
3917 return len;
3920 static struct md_sysfs_entry md_max_sync =
3921 __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
3923 static ssize_t
3924 suspend_lo_show(mddev_t *mddev, char *page)
3926 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
3929 static ssize_t
3930 suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
3932 char *e;
3933 unsigned long long new = simple_strtoull(buf, &e, 10);
3935 if (mddev->pers == NULL ||
3936 mddev->pers->quiesce == NULL)
3937 return -EINVAL;
3938 if (buf == e || (*e && *e != '\n'))
3939 return -EINVAL;
3940 if (new >= mddev->suspend_hi ||
3941 (new > mddev->suspend_lo && new < mddev->suspend_hi)) {
3942 mddev->suspend_lo = new;
3943 mddev->pers->quiesce(mddev, 2);
3944 return len;
3945 } else
3946 return -EINVAL;
3948 static struct md_sysfs_entry md_suspend_lo =
3949 __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
3952 static ssize_t
3953 suspend_hi_show(mddev_t *mddev, char *page)
3955 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
3958 static ssize_t
3959 suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
3961 char *e;
3962 unsigned long long new = simple_strtoull(buf, &e, 10);
3964 if (mddev->pers == NULL ||
3965 mddev->pers->quiesce == NULL)
3966 return -EINVAL;
3967 if (buf == e || (*e && *e != '\n'))
3968 return -EINVAL;
3969 if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) ||
3970 (new > mddev->suspend_lo && new > mddev->suspend_hi)) {
3971 mddev->suspend_hi = new;
3972 mddev->pers->quiesce(mddev, 1);
3973 mddev->pers->quiesce(mddev, 0);
3974 return len;
3975 } else
3976 return -EINVAL;
3978 static struct md_sysfs_entry md_suspend_hi =
3979 __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
3981 static ssize_t
3982 reshape_position_show(mddev_t *mddev, char *page)
3984 if (mddev->reshape_position != MaxSector)
3985 return sprintf(page, "%llu\n",
3986 (unsigned long long)mddev->reshape_position);
3987 strcpy(page, "none\n");
3988 return 5;
3991 static ssize_t
3992 reshape_position_store(mddev_t *mddev, const char *buf, size_t len)
3994 char *e;
3995 unsigned long long new = simple_strtoull(buf, &e, 10);
3996 if (mddev->pers)
3997 return -EBUSY;
3998 if (buf == e || (*e && *e != '\n'))
3999 return -EINVAL;
4000 mddev->reshape_position = new;
4001 mddev->delta_disks = 0;
4002 mddev->new_level = mddev->level;
4003 mddev->new_layout = mddev->layout;
4004 mddev->new_chunk_sectors = mddev->chunk_sectors;
4005 return len;
4008 static struct md_sysfs_entry md_reshape_position =
4009 __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4010 reshape_position_store);
4012 static ssize_t
4013 array_size_show(mddev_t *mddev, char *page)
4015 if (mddev->external_size)
4016 return sprintf(page, "%llu\n",
4017 (unsigned long long)mddev->array_sectors/2);
4018 else
4019 return sprintf(page, "default\n");
4022 static ssize_t
4023 array_size_store(mddev_t *mddev, const char *buf, size_t len)
4025 sector_t sectors;
4027 if (strncmp(buf, "default", 7) == 0) {
4028 if (mddev->pers)
4029 sectors = mddev->pers->size(mddev, 0, 0);
4030 else
4031 sectors = mddev->array_sectors;
4033 mddev->external_size = 0;
4034 } else {
4035 if (strict_blocks_to_sectors(buf, &sectors) < 0)
4036 return -EINVAL;
4037 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
4038 return -E2BIG;
4040 mddev->external_size = 1;
4043 mddev->array_sectors = sectors;
4044 set_capacity(mddev->gendisk, mddev->array_sectors);
4045 if (mddev->pers)
4046 revalidate_disk(mddev->gendisk);
4048 return len;
4051 static struct md_sysfs_entry md_array_size =
4052 __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4053 array_size_store);
4055 static struct attribute *md_default_attrs[] = {
4056 &md_level.attr,
4057 &md_layout.attr,
4058 &md_raid_disks.attr,
4059 &md_chunk_size.attr,
4060 &md_size.attr,
4061 &md_resync_start.attr,
4062 &md_metadata.attr,
4063 &md_new_device.attr,
4064 &md_safe_delay.attr,
4065 &md_array_state.attr,
4066 &md_reshape_position.attr,
4067 &md_array_size.attr,
4068 &max_corr_read_errors.attr,
4069 NULL,
4072 static struct attribute *md_redundancy_attrs[] = {
4073 &md_scan_mode.attr,
4074 &md_mismatches.attr,
4075 &md_sync_min.attr,
4076 &md_sync_max.attr,
4077 &md_sync_speed.attr,
4078 &md_sync_force_parallel.attr,
4079 &md_sync_completed.attr,
4080 &md_min_sync.attr,
4081 &md_max_sync.attr,
4082 &md_suspend_lo.attr,
4083 &md_suspend_hi.attr,
4084 &md_bitmap.attr,
4085 &md_degraded.attr,
4086 NULL,
4088 static struct attribute_group md_redundancy_group = {
4089 .name = NULL,
4090 .attrs = md_redundancy_attrs,
4094 static ssize_t
4095 md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4097 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4098 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
4099 ssize_t rv;
4101 if (!entry->show)
4102 return -EIO;
4103 rv = mddev_lock(mddev);
4104 if (!rv) {
4105 rv = entry->show(mddev, page);
4106 mddev_unlock(mddev);
4108 return rv;
4111 static ssize_t
4112 md_attr_store(struct kobject *kobj, struct attribute *attr,
4113 const char *page, size_t length)
4115 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4116 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
4117 ssize_t rv;
4119 if (!entry->store)
4120 return -EIO;
4121 if (!capable(CAP_SYS_ADMIN))
4122 return -EACCES;
4123 rv = mddev_lock(mddev);
4124 if (mddev->hold_active == UNTIL_IOCTL)
4125 mddev->hold_active = 0;
4126 if (!rv) {
4127 rv = entry->store(mddev, page, length);
4128 mddev_unlock(mddev);
4130 return rv;
4133 static void md_free(struct kobject *ko)
4135 mddev_t *mddev = container_of(ko, mddev_t, kobj);
4137 if (mddev->sysfs_state)
4138 sysfs_put(mddev->sysfs_state);
4140 if (mddev->gendisk) {
4141 del_gendisk(mddev->gendisk);
4142 put_disk(mddev->gendisk);
4144 if (mddev->queue)
4145 blk_cleanup_queue(mddev->queue);
4147 kfree(mddev);
4150 static const struct sysfs_ops md_sysfs_ops = {
4151 .show = md_attr_show,
4152 .store = md_attr_store,
4154 static struct kobj_type md_ktype = {
4155 .release = md_free,
4156 .sysfs_ops = &md_sysfs_ops,
4157 .default_attrs = md_default_attrs,
4160 int mdp_major = 0;
4162 static void mddev_delayed_delete(struct work_struct *ws)
4164 mddev_t *mddev = container_of(ws, mddev_t, del_work);
4166 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
4167 kobject_del(&mddev->kobj);
4168 kobject_put(&mddev->kobj);
4171 static int md_alloc(dev_t dev, char *name)
4173 static DEFINE_MUTEX(disks_mutex);
4174 mddev_t *mddev = mddev_find(dev);
4175 struct gendisk *disk;
4176 int partitioned;
4177 int shift;
4178 int unit;
4179 int error;
4181 if (!mddev)
4182 return -ENODEV;
4184 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4185 shift = partitioned ? MdpMinorShift : 0;
4186 unit = MINOR(mddev->unit) >> shift;
4188 /* wait for any previous instance if this device
4189 * to be completed removed (mddev_delayed_delete).
4191 flush_scheduled_work();
4193 mutex_lock(&disks_mutex);
4194 error = -EEXIST;
4195 if (mddev->gendisk)
4196 goto abort;
4198 if (name) {
4199 /* Need to ensure that 'name' is not a duplicate.
4201 mddev_t *mddev2;
4202 spin_lock(&all_mddevs_lock);
4204 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
4205 if (mddev2->gendisk &&
4206 strcmp(mddev2->gendisk->disk_name, name) == 0) {
4207 spin_unlock(&all_mddevs_lock);
4208 goto abort;
4210 spin_unlock(&all_mddevs_lock);
4213 error = -ENOMEM;
4214 mddev->queue = blk_alloc_queue(GFP_KERNEL);
4215 if (!mddev->queue)
4216 goto abort;
4217 mddev->queue->queuedata = mddev;
4219 /* Can be unlocked because the queue is new: no concurrency */
4220 queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, mddev->queue);
4222 blk_queue_make_request(mddev->queue, md_make_request);
4224 disk = alloc_disk(1 << shift);
4225 if (!disk) {
4226 blk_cleanup_queue(mddev->queue);
4227 mddev->queue = NULL;
4228 goto abort;
4230 disk->major = MAJOR(mddev->unit);
4231 disk->first_minor = unit << shift;
4232 if (name)
4233 strcpy(disk->disk_name, name);
4234 else if (partitioned)
4235 sprintf(disk->disk_name, "md_d%d", unit);
4236 else
4237 sprintf(disk->disk_name, "md%d", unit);
4238 disk->fops = &md_fops;
4239 disk->private_data = mddev;
4240 disk->queue = mddev->queue;
4241 /* Allow extended partitions. This makes the
4242 * 'mdp' device redundant, but we can't really
4243 * remove it now.
4245 disk->flags |= GENHD_FL_EXT_DEVT;
4246 add_disk(disk);
4247 mddev->gendisk = disk;
4248 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
4249 &disk_to_dev(disk)->kobj, "%s", "md");
4250 if (error) {
4251 /* This isn't possible, but as kobject_init_and_add is marked
4252 * __must_check, we must do something with the result
4254 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
4255 disk->disk_name);
4256 error = 0;
4258 if (sysfs_create_group(&mddev->kobj, &md_bitmap_group))
4259 printk(KERN_DEBUG "pointless warning\n");
4260 abort:
4261 mutex_unlock(&disks_mutex);
4262 if (!error) {
4263 kobject_uevent(&mddev->kobj, KOBJ_ADD);
4264 mddev->sysfs_state = sysfs_get_dirent(mddev->kobj.sd, NULL, "array_state");
4266 mddev_put(mddev);
4267 return error;
4270 static struct kobject *md_probe(dev_t dev, int *part, void *data)
4272 md_alloc(dev, NULL);
4273 return NULL;
4276 static int add_named_array(const char *val, struct kernel_param *kp)
4278 /* val must be "md_*" where * is not all digits.
4279 * We allocate an array with a large free minor number, and
4280 * set the name to val. val must not already be an active name.
4282 int len = strlen(val);
4283 char buf[DISK_NAME_LEN];
4285 while (len && val[len-1] == '\n')
4286 len--;
4287 if (len >= DISK_NAME_LEN)
4288 return -E2BIG;
4289 strlcpy(buf, val, len+1);
4290 if (strncmp(buf, "md_", 3) != 0)
4291 return -EINVAL;
4292 return md_alloc(0, buf);
4295 static void md_safemode_timeout(unsigned long data)
4297 mddev_t *mddev = (mddev_t *) data;
4299 if (!atomic_read(&mddev->writes_pending)) {
4300 mddev->safemode = 1;
4301 if (mddev->external)
4302 sysfs_notify_dirent(mddev->sysfs_state);
4304 md_wakeup_thread(mddev->thread);
4307 static int start_dirty_degraded;
4309 static int md_run(mddev_t *mddev)
4311 int err;
4312 mdk_rdev_t *rdev;
4313 struct mdk_personality *pers;
4315 if (list_empty(&mddev->disks))
4316 /* cannot run an array with no devices.. */
4317 return -EINVAL;
4319 if (mddev->pers)
4320 return -EBUSY;
4322 /* These two calls synchronise us with the
4323 * sysfs_remove_group calls in mddev_unlock,
4324 * so they must have completed.
4326 mutex_lock(&mddev->open_mutex);
4327 mutex_unlock(&mddev->open_mutex);
4330 * Analyze all RAID superblock(s)
4332 if (!mddev->raid_disks) {
4333 if (!mddev->persistent)
4334 return -EINVAL;
4335 analyze_sbs(mddev);
4338 if (mddev->level != LEVEL_NONE)
4339 request_module("md-level-%d", mddev->level);
4340 else if (mddev->clevel[0])
4341 request_module("md-%s", mddev->clevel);
4344 * Drop all container device buffers, from now on
4345 * the only valid external interface is through the md
4346 * device.
4348 list_for_each_entry(rdev, &mddev->disks, same_set) {
4349 if (test_bit(Faulty, &rdev->flags))
4350 continue;
4351 sync_blockdev(rdev->bdev);
4352 invalidate_bdev(rdev->bdev);
4354 /* perform some consistency tests on the device.
4355 * We don't want the data to overlap the metadata,
4356 * Internal Bitmap issues have been handled elsewhere.
4358 if (rdev->data_offset < rdev->sb_start) {
4359 if (mddev->dev_sectors &&
4360 rdev->data_offset + mddev->dev_sectors
4361 > rdev->sb_start) {
4362 printk("md: %s: data overlaps metadata\n",
4363 mdname(mddev));
4364 return -EINVAL;
4366 } else {
4367 if (rdev->sb_start + rdev->sb_size/512
4368 > rdev->data_offset) {
4369 printk("md: %s: metadata overlaps data\n",
4370 mdname(mddev));
4371 return -EINVAL;
4374 sysfs_notify_dirent(rdev->sysfs_state);
4377 spin_lock(&pers_lock);
4378 pers = find_pers(mddev->level, mddev->clevel);
4379 if (!pers || !try_module_get(pers->owner)) {
4380 spin_unlock(&pers_lock);
4381 if (mddev->level != LEVEL_NONE)
4382 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
4383 mddev->level);
4384 else
4385 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
4386 mddev->clevel);
4387 return -EINVAL;
4389 mddev->pers = pers;
4390 spin_unlock(&pers_lock);
4391 if (mddev->level != pers->level) {
4392 mddev->level = pers->level;
4393 mddev->new_level = pers->level;
4395 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
4397 if (mddev->reshape_position != MaxSector &&
4398 pers->start_reshape == NULL) {
4399 /* This personality cannot handle reshaping... */
4400 mddev->pers = NULL;
4401 module_put(pers->owner);
4402 return -EINVAL;
4405 if (pers->sync_request) {
4406 /* Warn if this is a potentially silly
4407 * configuration.
4409 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4410 mdk_rdev_t *rdev2;
4411 int warned = 0;
4413 list_for_each_entry(rdev, &mddev->disks, same_set)
4414 list_for_each_entry(rdev2, &mddev->disks, same_set) {
4415 if (rdev < rdev2 &&
4416 rdev->bdev->bd_contains ==
4417 rdev2->bdev->bd_contains) {
4418 printk(KERN_WARNING
4419 "%s: WARNING: %s appears to be"
4420 " on the same physical disk as"
4421 " %s.\n",
4422 mdname(mddev),
4423 bdevname(rdev->bdev,b),
4424 bdevname(rdev2->bdev,b2));
4425 warned = 1;
4429 if (warned)
4430 printk(KERN_WARNING
4431 "True protection against single-disk"
4432 " failure might be compromised.\n");
4435 mddev->recovery = 0;
4436 /* may be over-ridden by personality */
4437 mddev->resync_max_sectors = mddev->dev_sectors;
4439 mddev->barriers_work = 1;
4440 mddev->ok_start_degraded = start_dirty_degraded;
4442 if (start_readonly && mddev->ro == 0)
4443 mddev->ro = 2; /* read-only, but switch on first write */
4445 err = mddev->pers->run(mddev);
4446 if (err)
4447 printk(KERN_ERR "md: pers->run() failed ...\n");
4448 else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
4449 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
4450 " but 'external_size' not in effect?\n", __func__);
4451 printk(KERN_ERR
4452 "md: invalid array_size %llu > default size %llu\n",
4453 (unsigned long long)mddev->array_sectors / 2,
4454 (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
4455 err = -EINVAL;
4456 mddev->pers->stop(mddev);
4458 if (err == 0 && mddev->pers->sync_request) {
4459 err = bitmap_create(mddev);
4460 if (err) {
4461 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
4462 mdname(mddev), err);
4463 mddev->pers->stop(mddev);
4466 if (err) {
4467 module_put(mddev->pers->owner);
4468 mddev->pers = NULL;
4469 bitmap_destroy(mddev);
4470 return err;
4472 if (mddev->pers->sync_request) {
4473 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
4474 printk(KERN_WARNING
4475 "md: cannot register extra attributes for %s\n",
4476 mdname(mddev));
4477 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, NULL, "sync_action");
4478 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
4479 mddev->ro = 0;
4481 atomic_set(&mddev->writes_pending,0);
4482 atomic_set(&mddev->max_corr_read_errors,
4483 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
4484 mddev->safemode = 0;
4485 mddev->safemode_timer.function = md_safemode_timeout;
4486 mddev->safemode_timer.data = (unsigned long) mddev;
4487 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
4488 mddev->in_sync = 1;
4490 list_for_each_entry(rdev, &mddev->disks, same_set)
4491 if (rdev->raid_disk >= 0) {
4492 char nm[20];
4493 sprintf(nm, "rd%d", rdev->raid_disk);
4494 if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
4495 printk("md: cannot register %s for %s\n",
4496 nm, mdname(mddev));
4499 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4501 if (mddev->flags)
4502 md_update_sb(mddev, 0);
4504 md_wakeup_thread(mddev->thread);
4505 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
4507 md_new_event(mddev);
4508 sysfs_notify_dirent(mddev->sysfs_state);
4509 if (mddev->sysfs_action)
4510 sysfs_notify_dirent(mddev->sysfs_action);
4511 sysfs_notify(&mddev->kobj, NULL, "degraded");
4512 return 0;
4515 static int do_md_run(mddev_t *mddev)
4517 int err;
4519 err = md_run(mddev);
4520 if (err)
4521 goto out;
4523 set_capacity(mddev->gendisk, mddev->array_sectors);
4524 revalidate_disk(mddev->gendisk);
4525 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
4526 out:
4527 return err;
4530 static int restart_array(mddev_t *mddev)
4532 struct gendisk *disk = mddev->gendisk;
4534 /* Complain if it has no devices */
4535 if (list_empty(&mddev->disks))
4536 return -ENXIO;
4537 if (!mddev->pers)
4538 return -EINVAL;
4539 if (!mddev->ro)
4540 return -EBUSY;
4541 mddev->safemode = 0;
4542 mddev->ro = 0;
4543 set_disk_ro(disk, 0);
4544 printk(KERN_INFO "md: %s switched to read-write mode.\n",
4545 mdname(mddev));
4546 /* Kick recovery or resync if necessary */
4547 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4548 md_wakeup_thread(mddev->thread);
4549 md_wakeup_thread(mddev->sync_thread);
4550 sysfs_notify_dirent(mddev->sysfs_state);
4551 return 0;
4554 /* similar to deny_write_access, but accounts for our holding a reference
4555 * to the file ourselves */
4556 static int deny_bitmap_write_access(struct file * file)
4558 struct inode *inode = file->f_mapping->host;
4560 spin_lock(&inode->i_lock);
4561 if (atomic_read(&inode->i_writecount) > 1) {
4562 spin_unlock(&inode->i_lock);
4563 return -ETXTBSY;
4565 atomic_set(&inode->i_writecount, -1);
4566 spin_unlock(&inode->i_lock);
4568 return 0;
4571 void restore_bitmap_write_access(struct file *file)
4573 struct inode *inode = file->f_mapping->host;
4575 spin_lock(&inode->i_lock);
4576 atomic_set(&inode->i_writecount, 1);
4577 spin_unlock(&inode->i_lock);
4580 static void md_clean(mddev_t *mddev)
4582 mddev->array_sectors = 0;
4583 mddev->external_size = 0;
4584 mddev->dev_sectors = 0;
4585 mddev->raid_disks = 0;
4586 mddev->recovery_cp = 0;
4587 mddev->resync_min = 0;
4588 mddev->resync_max = MaxSector;
4589 mddev->reshape_position = MaxSector;
4590 mddev->external = 0;
4591 mddev->persistent = 0;
4592 mddev->level = LEVEL_NONE;
4593 mddev->clevel[0] = 0;
4594 mddev->flags = 0;
4595 mddev->ro = 0;
4596 mddev->metadata_type[0] = 0;
4597 mddev->chunk_sectors = 0;
4598 mddev->ctime = mddev->utime = 0;
4599 mddev->layout = 0;
4600 mddev->max_disks = 0;
4601 mddev->events = 0;
4602 mddev->can_decrease_events = 0;
4603 mddev->delta_disks = 0;
4604 mddev->new_level = LEVEL_NONE;
4605 mddev->new_layout = 0;
4606 mddev->new_chunk_sectors = 0;
4607 mddev->curr_resync = 0;
4608 mddev->resync_mismatches = 0;
4609 mddev->suspend_lo = mddev->suspend_hi = 0;
4610 mddev->sync_speed_min = mddev->sync_speed_max = 0;
4611 mddev->recovery = 0;
4612 mddev->in_sync = 0;
4613 mddev->degraded = 0;
4614 mddev->barriers_work = 0;
4615 mddev->safemode = 0;
4616 mddev->bitmap_info.offset = 0;
4617 mddev->bitmap_info.default_offset = 0;
4618 mddev->bitmap_info.chunksize = 0;
4619 mddev->bitmap_info.daemon_sleep = 0;
4620 mddev->bitmap_info.max_write_behind = 0;
4623 static void md_stop_writes(mddev_t *mddev)
4625 if (mddev->sync_thread) {
4626 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4627 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4628 md_unregister_thread(mddev->sync_thread);
4629 mddev->sync_thread = NULL;
4632 del_timer_sync(&mddev->safemode_timer);
4634 bitmap_flush(mddev);
4635 md_super_wait(mddev);
4637 if (!mddev->in_sync || mddev->flags) {
4638 /* mark array as shutdown cleanly */
4639 mddev->in_sync = 1;
4640 md_update_sb(mddev, 1);
4644 static void md_stop(mddev_t *mddev)
4646 md_stop_writes(mddev);
4648 mddev->pers->stop(mddev);
4649 if (mddev->pers->sync_request && mddev->to_remove == NULL)
4650 mddev->to_remove = &md_redundancy_group;
4651 module_put(mddev->pers->owner);
4652 mddev->pers = NULL;
4653 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4656 static int md_set_readonly(mddev_t *mddev, int is_open)
4658 int err = 0;
4659 mutex_lock(&mddev->open_mutex);
4660 if (atomic_read(&mddev->openers) > is_open) {
4661 printk("md: %s still in use.\n",mdname(mddev));
4662 err = -EBUSY;
4663 goto out;
4665 if (mddev->pers) {
4666 md_stop_writes(mddev);
4668 err = -ENXIO;
4669 if (mddev->ro==1)
4670 goto out;
4671 mddev->ro = 1;
4672 set_disk_ro(mddev->gendisk, 1);
4673 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4674 sysfs_notify_dirent(mddev->sysfs_state);
4675 err = 0;
4677 out:
4678 mutex_unlock(&mddev->open_mutex);
4679 return err;
4682 /* mode:
4683 * 0 - completely stop and dis-assemble array
4684 * 2 - stop but do not disassemble array
4686 static int do_md_stop(mddev_t * mddev, int mode, int is_open)
4688 int err = 0;
4689 struct gendisk *disk = mddev->gendisk;
4690 mdk_rdev_t *rdev;
4692 mutex_lock(&mddev->open_mutex);
4693 if (atomic_read(&mddev->openers) > is_open) {
4694 printk("md: %s still in use.\n",mdname(mddev));
4695 err = -EBUSY;
4696 } else if (mddev->pers) {
4698 if (mddev->ro)
4699 set_disk_ro(disk, 0);
4701 md_stop(mddev);
4702 mddev->queue->merge_bvec_fn = NULL;
4703 mddev->queue->unplug_fn = NULL;
4704 mddev->queue->backing_dev_info.congested_fn = NULL;
4706 /* tell userspace to handle 'inactive' */
4707 sysfs_notify_dirent(mddev->sysfs_state);
4709 list_for_each_entry(rdev, &mddev->disks, same_set)
4710 if (rdev->raid_disk >= 0) {
4711 char nm[20];
4712 sprintf(nm, "rd%d", rdev->raid_disk);
4713 sysfs_remove_link(&mddev->kobj, nm);
4716 set_capacity(disk, 0);
4717 revalidate_disk(disk);
4719 if (mddev->ro)
4720 mddev->ro = 0;
4722 err = 0;
4724 mutex_unlock(&mddev->open_mutex);
4725 if (err)
4726 return err;
4728 * Free resources if final stop
4730 if (mode == 0) {
4732 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
4734 bitmap_destroy(mddev);
4735 if (mddev->bitmap_info.file) {
4736 restore_bitmap_write_access(mddev->bitmap_info.file);
4737 fput(mddev->bitmap_info.file);
4738 mddev->bitmap_info.file = NULL;
4740 mddev->bitmap_info.offset = 0;
4742 export_array(mddev);
4744 md_clean(mddev);
4745 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
4746 if (mddev->hold_active == UNTIL_STOP)
4747 mddev->hold_active = 0;
4750 err = 0;
4751 blk_integrity_unregister(disk);
4752 md_new_event(mddev);
4753 sysfs_notify_dirent(mddev->sysfs_state);
4754 return err;
4757 #ifndef MODULE
4758 static void autorun_array(mddev_t *mddev)
4760 mdk_rdev_t *rdev;
4761 int err;
4763 if (list_empty(&mddev->disks))
4764 return;
4766 printk(KERN_INFO "md: running: ");
4768 list_for_each_entry(rdev, &mddev->disks, same_set) {
4769 char b[BDEVNAME_SIZE];
4770 printk("<%s>", bdevname(rdev->bdev,b));
4772 printk("\n");
4774 err = do_md_run(mddev);
4775 if (err) {
4776 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
4777 do_md_stop(mddev, 0, 0);
4782 * lets try to run arrays based on all disks that have arrived
4783 * until now. (those are in pending_raid_disks)
4785 * the method: pick the first pending disk, collect all disks with
4786 * the same UUID, remove all from the pending list and put them into
4787 * the 'same_array' list. Then order this list based on superblock
4788 * update time (freshest comes first), kick out 'old' disks and
4789 * compare superblocks. If everything's fine then run it.
4791 * If "unit" is allocated, then bump its reference count
4793 static void autorun_devices(int part)
4795 mdk_rdev_t *rdev0, *rdev, *tmp;
4796 mddev_t *mddev;
4797 char b[BDEVNAME_SIZE];
4799 printk(KERN_INFO "md: autorun ...\n");
4800 while (!list_empty(&pending_raid_disks)) {
4801 int unit;
4802 dev_t dev;
4803 LIST_HEAD(candidates);
4804 rdev0 = list_entry(pending_raid_disks.next,
4805 mdk_rdev_t, same_set);
4807 printk(KERN_INFO "md: considering %s ...\n",
4808 bdevname(rdev0->bdev,b));
4809 INIT_LIST_HEAD(&candidates);
4810 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
4811 if (super_90_load(rdev, rdev0, 0) >= 0) {
4812 printk(KERN_INFO "md: adding %s ...\n",
4813 bdevname(rdev->bdev,b));
4814 list_move(&rdev->same_set, &candidates);
4817 * now we have a set of devices, with all of them having
4818 * mostly sane superblocks. It's time to allocate the
4819 * mddev.
4821 if (part) {
4822 dev = MKDEV(mdp_major,
4823 rdev0->preferred_minor << MdpMinorShift);
4824 unit = MINOR(dev) >> MdpMinorShift;
4825 } else {
4826 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
4827 unit = MINOR(dev);
4829 if (rdev0->preferred_minor != unit) {
4830 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
4831 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
4832 break;
4835 md_probe(dev, NULL, NULL);
4836 mddev = mddev_find(dev);
4837 if (!mddev || !mddev->gendisk) {
4838 if (mddev)
4839 mddev_put(mddev);
4840 printk(KERN_ERR
4841 "md: cannot allocate memory for md drive.\n");
4842 break;
4844 if (mddev_lock(mddev))
4845 printk(KERN_WARNING "md: %s locked, cannot run\n",
4846 mdname(mddev));
4847 else if (mddev->raid_disks || mddev->major_version
4848 || !list_empty(&mddev->disks)) {
4849 printk(KERN_WARNING
4850 "md: %s already running, cannot run %s\n",
4851 mdname(mddev), bdevname(rdev0->bdev,b));
4852 mddev_unlock(mddev);
4853 } else {
4854 printk(KERN_INFO "md: created %s\n", mdname(mddev));
4855 mddev->persistent = 1;
4856 rdev_for_each_list(rdev, tmp, &candidates) {
4857 list_del_init(&rdev->same_set);
4858 if (bind_rdev_to_array(rdev, mddev))
4859 export_rdev(rdev);
4861 autorun_array(mddev);
4862 mddev_unlock(mddev);
4864 /* on success, candidates will be empty, on error
4865 * it won't...
4867 rdev_for_each_list(rdev, tmp, &candidates) {
4868 list_del_init(&rdev->same_set);
4869 export_rdev(rdev);
4871 mddev_put(mddev);
4873 printk(KERN_INFO "md: ... autorun DONE.\n");
4875 #endif /* !MODULE */
4877 static int get_version(void __user * arg)
4879 mdu_version_t ver;
4881 ver.major = MD_MAJOR_VERSION;
4882 ver.minor = MD_MINOR_VERSION;
4883 ver.patchlevel = MD_PATCHLEVEL_VERSION;
4885 if (copy_to_user(arg, &ver, sizeof(ver)))
4886 return -EFAULT;
4888 return 0;
4891 static int get_array_info(mddev_t * mddev, void __user * arg)
4893 mdu_array_info_t info;
4894 int nr,working,insync,failed,spare;
4895 mdk_rdev_t *rdev;
4897 nr=working=insync=failed=spare=0;
4898 list_for_each_entry(rdev, &mddev->disks, same_set) {
4899 nr++;
4900 if (test_bit(Faulty, &rdev->flags))
4901 failed++;
4902 else {
4903 working++;
4904 if (test_bit(In_sync, &rdev->flags))
4905 insync++;
4906 else
4907 spare++;
4911 info.major_version = mddev->major_version;
4912 info.minor_version = mddev->minor_version;
4913 info.patch_version = MD_PATCHLEVEL_VERSION;
4914 info.ctime = mddev->ctime;
4915 info.level = mddev->level;
4916 info.size = mddev->dev_sectors / 2;
4917 if (info.size != mddev->dev_sectors / 2) /* overflow */
4918 info.size = -1;
4919 info.nr_disks = nr;
4920 info.raid_disks = mddev->raid_disks;
4921 info.md_minor = mddev->md_minor;
4922 info.not_persistent= !mddev->persistent;
4924 info.utime = mddev->utime;
4925 info.state = 0;
4926 if (mddev->in_sync)
4927 info.state = (1<<MD_SB_CLEAN);
4928 if (mddev->bitmap && mddev->bitmap_info.offset)
4929 info.state = (1<<MD_SB_BITMAP_PRESENT);
4930 info.active_disks = insync;
4931 info.working_disks = working;
4932 info.failed_disks = failed;
4933 info.spare_disks = spare;
4935 info.layout = mddev->layout;
4936 info.chunk_size = mddev->chunk_sectors << 9;
4938 if (copy_to_user(arg, &info, sizeof(info)))
4939 return -EFAULT;
4941 return 0;
4944 static int get_bitmap_file(mddev_t * mddev, void __user * arg)
4946 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
4947 char *ptr, *buf = NULL;
4948 int err = -ENOMEM;
4950 if (md_allow_write(mddev))
4951 file = kmalloc(sizeof(*file), GFP_NOIO);
4952 else
4953 file = kmalloc(sizeof(*file), GFP_KERNEL);
4955 if (!file)
4956 goto out;
4958 /* bitmap disabled, zero the first byte and copy out */
4959 if (!mddev->bitmap || !mddev->bitmap->file) {
4960 file->pathname[0] = '\0';
4961 goto copy_out;
4964 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
4965 if (!buf)
4966 goto out;
4968 ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname));
4969 if (IS_ERR(ptr))
4970 goto out;
4972 strcpy(file->pathname, ptr);
4974 copy_out:
4975 err = 0;
4976 if (copy_to_user(arg, file, sizeof(*file)))
4977 err = -EFAULT;
4978 out:
4979 kfree(buf);
4980 kfree(file);
4981 return err;
4984 static int get_disk_info(mddev_t * mddev, void __user * arg)
4986 mdu_disk_info_t info;
4987 mdk_rdev_t *rdev;
4989 if (copy_from_user(&info, arg, sizeof(info)))
4990 return -EFAULT;
4992 rdev = find_rdev_nr(mddev, info.number);
4993 if (rdev) {
4994 info.major = MAJOR(rdev->bdev->bd_dev);
4995 info.minor = MINOR(rdev->bdev->bd_dev);
4996 info.raid_disk = rdev->raid_disk;
4997 info.state = 0;
4998 if (test_bit(Faulty, &rdev->flags))
4999 info.state |= (1<<MD_DISK_FAULTY);
5000 else if (test_bit(In_sync, &rdev->flags)) {
5001 info.state |= (1<<MD_DISK_ACTIVE);
5002 info.state |= (1<<MD_DISK_SYNC);
5004 if (test_bit(WriteMostly, &rdev->flags))
5005 info.state |= (1<<MD_DISK_WRITEMOSTLY);
5006 } else {
5007 info.major = info.minor = 0;
5008 info.raid_disk = -1;
5009 info.state = (1<<MD_DISK_REMOVED);
5012 if (copy_to_user(arg, &info, sizeof(info)))
5013 return -EFAULT;
5015 return 0;
5018 static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
5020 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
5021 mdk_rdev_t *rdev;
5022 dev_t dev = MKDEV(info->major,info->minor);
5024 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
5025 return -EOVERFLOW;
5027 if (!mddev->raid_disks) {
5028 int err;
5029 /* expecting a device which has a superblock */
5030 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
5031 if (IS_ERR(rdev)) {
5032 printk(KERN_WARNING
5033 "md: md_import_device returned %ld\n",
5034 PTR_ERR(rdev));
5035 return PTR_ERR(rdev);
5037 if (!list_empty(&mddev->disks)) {
5038 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
5039 mdk_rdev_t, same_set);
5040 err = super_types[mddev->major_version]
5041 .load_super(rdev, rdev0, mddev->minor_version);
5042 if (err < 0) {
5043 printk(KERN_WARNING
5044 "md: %s has different UUID to %s\n",
5045 bdevname(rdev->bdev,b),
5046 bdevname(rdev0->bdev,b2));
5047 export_rdev(rdev);
5048 return -EINVAL;
5051 err = bind_rdev_to_array(rdev, mddev);
5052 if (err)
5053 export_rdev(rdev);
5054 return err;
5058 * add_new_disk can be used once the array is assembled
5059 * to add "hot spares". They must already have a superblock
5060 * written
5062 if (mddev->pers) {
5063 int err;
5064 if (!mddev->pers->hot_add_disk) {
5065 printk(KERN_WARNING
5066 "%s: personality does not support diskops!\n",
5067 mdname(mddev));
5068 return -EINVAL;
5070 if (mddev->persistent)
5071 rdev = md_import_device(dev, mddev->major_version,
5072 mddev->minor_version);
5073 else
5074 rdev = md_import_device(dev, -1, -1);
5075 if (IS_ERR(rdev)) {
5076 printk(KERN_WARNING
5077 "md: md_import_device returned %ld\n",
5078 PTR_ERR(rdev));
5079 return PTR_ERR(rdev);
5081 /* set save_raid_disk if appropriate */
5082 if (!mddev->persistent) {
5083 if (info->state & (1<<MD_DISK_SYNC) &&
5084 info->raid_disk < mddev->raid_disks)
5085 rdev->raid_disk = info->raid_disk;
5086 else
5087 rdev->raid_disk = -1;
5088 } else
5089 super_types[mddev->major_version].
5090 validate_super(mddev, rdev);
5091 rdev->saved_raid_disk = rdev->raid_disk;
5093 clear_bit(In_sync, &rdev->flags); /* just to be sure */
5094 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5095 set_bit(WriteMostly, &rdev->flags);
5096 else
5097 clear_bit(WriteMostly, &rdev->flags);
5099 rdev->raid_disk = -1;
5100 err = bind_rdev_to_array(rdev, mddev);
5101 if (!err && !mddev->pers->hot_remove_disk) {
5102 /* If there is hot_add_disk but no hot_remove_disk
5103 * then added disks for geometry changes,
5104 * and should be added immediately.
5106 super_types[mddev->major_version].
5107 validate_super(mddev, rdev);
5108 err = mddev->pers->hot_add_disk(mddev, rdev);
5109 if (err)
5110 unbind_rdev_from_array(rdev);
5112 if (err)
5113 export_rdev(rdev);
5114 else
5115 sysfs_notify_dirent(rdev->sysfs_state);
5117 md_update_sb(mddev, 1);
5118 if (mddev->degraded)
5119 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
5120 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5121 md_wakeup_thread(mddev->thread);
5122 return err;
5125 /* otherwise, add_new_disk is only allowed
5126 * for major_version==0 superblocks
5128 if (mddev->major_version != 0) {
5129 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
5130 mdname(mddev));
5131 return -EINVAL;
5134 if (!(info->state & (1<<MD_DISK_FAULTY))) {
5135 int err;
5136 rdev = md_import_device(dev, -1, 0);
5137 if (IS_ERR(rdev)) {
5138 printk(KERN_WARNING
5139 "md: error, md_import_device() returned %ld\n",
5140 PTR_ERR(rdev));
5141 return PTR_ERR(rdev);
5143 rdev->desc_nr = info->number;
5144 if (info->raid_disk < mddev->raid_disks)
5145 rdev->raid_disk = info->raid_disk;
5146 else
5147 rdev->raid_disk = -1;
5149 if (rdev->raid_disk < mddev->raid_disks)
5150 if (info->state & (1<<MD_DISK_SYNC))
5151 set_bit(In_sync, &rdev->flags);
5153 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5154 set_bit(WriteMostly, &rdev->flags);
5156 if (!mddev->persistent) {
5157 printk(KERN_INFO "md: nonpersistent superblock ...\n");
5158 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
5159 } else
5160 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
5161 rdev->sectors = rdev->sb_start;
5163 err = bind_rdev_to_array(rdev, mddev);
5164 if (err) {
5165 export_rdev(rdev);
5166 return err;
5170 return 0;
5173 static int hot_remove_disk(mddev_t * mddev, dev_t dev)
5175 char b[BDEVNAME_SIZE];
5176 mdk_rdev_t *rdev;
5178 rdev = find_rdev(mddev, dev);
5179 if (!rdev)
5180 return -ENXIO;
5182 if (rdev->raid_disk >= 0)
5183 goto busy;
5185 kick_rdev_from_array(rdev);
5186 md_update_sb(mddev, 1);
5187 md_new_event(mddev);
5189 return 0;
5190 busy:
5191 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
5192 bdevname(rdev->bdev,b), mdname(mddev));
5193 return -EBUSY;
5196 static int hot_add_disk(mddev_t * mddev, dev_t dev)
5198 char b[BDEVNAME_SIZE];
5199 int err;
5200 mdk_rdev_t *rdev;
5202 if (!mddev->pers)
5203 return -ENODEV;
5205 if (mddev->major_version != 0) {
5206 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
5207 " version-0 superblocks.\n",
5208 mdname(mddev));
5209 return -EINVAL;
5211 if (!mddev->pers->hot_add_disk) {
5212 printk(KERN_WARNING
5213 "%s: personality does not support diskops!\n",
5214 mdname(mddev));
5215 return -EINVAL;
5218 rdev = md_import_device(dev, -1, 0);
5219 if (IS_ERR(rdev)) {
5220 printk(KERN_WARNING
5221 "md: error, md_import_device() returned %ld\n",
5222 PTR_ERR(rdev));
5223 return -EINVAL;
5226 if (mddev->persistent)
5227 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
5228 else
5229 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
5231 rdev->sectors = rdev->sb_start;
5233 if (test_bit(Faulty, &rdev->flags)) {
5234 printk(KERN_WARNING
5235 "md: can not hot-add faulty %s disk to %s!\n",
5236 bdevname(rdev->bdev,b), mdname(mddev));
5237 err = -EINVAL;
5238 goto abort_export;
5240 clear_bit(In_sync, &rdev->flags);
5241 rdev->desc_nr = -1;
5242 rdev->saved_raid_disk = -1;
5243 err = bind_rdev_to_array(rdev, mddev);
5244 if (err)
5245 goto abort_export;
5248 * The rest should better be atomic, we can have disk failures
5249 * noticed in interrupt contexts ...
5252 rdev->raid_disk = -1;
5254 md_update_sb(mddev, 1);
5257 * Kick recovery, maybe this spare has to be added to the
5258 * array immediately.
5260 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5261 md_wakeup_thread(mddev->thread);
5262 md_new_event(mddev);
5263 return 0;
5265 abort_export:
5266 export_rdev(rdev);
5267 return err;
5270 static int set_bitmap_file(mddev_t *mddev, int fd)
5272 int err;
5274 if (mddev->pers) {
5275 if (!mddev->pers->quiesce)
5276 return -EBUSY;
5277 if (mddev->recovery || mddev->sync_thread)
5278 return -EBUSY;
5279 /* we should be able to change the bitmap.. */
5283 if (fd >= 0) {
5284 if (mddev->bitmap)
5285 return -EEXIST; /* cannot add when bitmap is present */
5286 mddev->bitmap_info.file = fget(fd);
5288 if (mddev->bitmap_info.file == NULL) {
5289 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
5290 mdname(mddev));
5291 return -EBADF;
5294 err = deny_bitmap_write_access(mddev->bitmap_info.file);
5295 if (err) {
5296 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
5297 mdname(mddev));
5298 fput(mddev->bitmap_info.file);
5299 mddev->bitmap_info.file = NULL;
5300 return err;
5302 mddev->bitmap_info.offset = 0; /* file overrides offset */
5303 } else if (mddev->bitmap == NULL)
5304 return -ENOENT; /* cannot remove what isn't there */
5305 err = 0;
5306 if (mddev->pers) {
5307 mddev->pers->quiesce(mddev, 1);
5308 if (fd >= 0)
5309 err = bitmap_create(mddev);
5310 if (fd < 0 || err) {
5311 bitmap_destroy(mddev);
5312 fd = -1; /* make sure to put the file */
5314 mddev->pers->quiesce(mddev, 0);
5316 if (fd < 0) {
5317 if (mddev->bitmap_info.file) {
5318 restore_bitmap_write_access(mddev->bitmap_info.file);
5319 fput(mddev->bitmap_info.file);
5321 mddev->bitmap_info.file = NULL;
5324 return err;
5328 * set_array_info is used two different ways
5329 * The original usage is when creating a new array.
5330 * In this usage, raid_disks is > 0 and it together with
5331 * level, size, not_persistent,layout,chunksize determine the
5332 * shape of the array.
5333 * This will always create an array with a type-0.90.0 superblock.
5334 * The newer usage is when assembling an array.
5335 * In this case raid_disks will be 0, and the major_version field is
5336 * use to determine which style super-blocks are to be found on the devices.
5337 * The minor and patch _version numbers are also kept incase the
5338 * super_block handler wishes to interpret them.
5340 static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
5343 if (info->raid_disks == 0) {
5344 /* just setting version number for superblock loading */
5345 if (info->major_version < 0 ||
5346 info->major_version >= ARRAY_SIZE(super_types) ||
5347 super_types[info->major_version].name == NULL) {
5348 /* maybe try to auto-load a module? */
5349 printk(KERN_INFO
5350 "md: superblock version %d not known\n",
5351 info->major_version);
5352 return -EINVAL;
5354 mddev->major_version = info->major_version;
5355 mddev->minor_version = info->minor_version;
5356 mddev->patch_version = info->patch_version;
5357 mddev->persistent = !info->not_persistent;
5358 /* ensure mddev_put doesn't delete this now that there
5359 * is some minimal configuration.
5361 mddev->ctime = get_seconds();
5362 return 0;
5364 mddev->major_version = MD_MAJOR_VERSION;
5365 mddev->minor_version = MD_MINOR_VERSION;
5366 mddev->patch_version = MD_PATCHLEVEL_VERSION;
5367 mddev->ctime = get_seconds();
5369 mddev->level = info->level;
5370 mddev->clevel[0] = 0;
5371 mddev->dev_sectors = 2 * (sector_t)info->size;
5372 mddev->raid_disks = info->raid_disks;
5373 /* don't set md_minor, it is determined by which /dev/md* was
5374 * openned
5376 if (info->state & (1<<MD_SB_CLEAN))
5377 mddev->recovery_cp = MaxSector;
5378 else
5379 mddev->recovery_cp = 0;
5380 mddev->persistent = ! info->not_persistent;
5381 mddev->external = 0;
5383 mddev->layout = info->layout;
5384 mddev->chunk_sectors = info->chunk_size >> 9;
5386 mddev->max_disks = MD_SB_DISKS;
5388 if (mddev->persistent)
5389 mddev->flags = 0;
5390 set_bit(MD_CHANGE_DEVS, &mddev->flags);
5392 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
5393 mddev->bitmap_info.offset = 0;
5395 mddev->reshape_position = MaxSector;
5398 * Generate a 128 bit UUID
5400 get_random_bytes(mddev->uuid, 16);
5402 mddev->new_level = mddev->level;
5403 mddev->new_chunk_sectors = mddev->chunk_sectors;
5404 mddev->new_layout = mddev->layout;
5405 mddev->delta_disks = 0;
5407 return 0;
5410 void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors)
5412 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
5414 if (mddev->external_size)
5415 return;
5417 mddev->array_sectors = array_sectors;
5419 EXPORT_SYMBOL(md_set_array_sectors);
5421 static int update_size(mddev_t *mddev, sector_t num_sectors)
5423 mdk_rdev_t *rdev;
5424 int rv;
5425 int fit = (num_sectors == 0);
5427 if (mddev->pers->resize == NULL)
5428 return -EINVAL;
5429 /* The "num_sectors" is the number of sectors of each device that
5430 * is used. This can only make sense for arrays with redundancy.
5431 * linear and raid0 always use whatever space is available. We can only
5432 * consider changing this number if no resync or reconstruction is
5433 * happening, and if the new size is acceptable. It must fit before the
5434 * sb_start or, if that is <data_offset, it must fit before the size
5435 * of each device. If num_sectors is zero, we find the largest size
5436 * that fits.
5439 if (mddev->sync_thread)
5440 return -EBUSY;
5441 if (mddev->bitmap)
5442 /* Sorry, cannot grow a bitmap yet, just remove it,
5443 * grow, and re-add.
5445 return -EBUSY;
5446 list_for_each_entry(rdev, &mddev->disks, same_set) {
5447 sector_t avail = rdev->sectors;
5449 if (fit && (num_sectors == 0 || num_sectors > avail))
5450 num_sectors = avail;
5451 if (avail < num_sectors)
5452 return -ENOSPC;
5454 rv = mddev->pers->resize(mddev, num_sectors);
5455 if (!rv)
5456 revalidate_disk(mddev->gendisk);
5457 return rv;
5460 static int update_raid_disks(mddev_t *mddev, int raid_disks)
5462 int rv;
5463 /* change the number of raid disks */
5464 if (mddev->pers->check_reshape == NULL)
5465 return -EINVAL;
5466 if (raid_disks <= 0 ||
5467 (mddev->max_disks && raid_disks >= mddev->max_disks))
5468 return -EINVAL;
5469 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
5470 return -EBUSY;
5471 mddev->delta_disks = raid_disks - mddev->raid_disks;
5473 rv = mddev->pers->check_reshape(mddev);
5474 return rv;
5479 * update_array_info is used to change the configuration of an
5480 * on-line array.
5481 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
5482 * fields in the info are checked against the array.
5483 * Any differences that cannot be handled will cause an error.
5484 * Normally, only one change can be managed at a time.
5486 static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
5488 int rv = 0;
5489 int cnt = 0;
5490 int state = 0;
5492 /* calculate expected state,ignoring low bits */
5493 if (mddev->bitmap && mddev->bitmap_info.offset)
5494 state |= (1 << MD_SB_BITMAP_PRESENT);
5496 if (mddev->major_version != info->major_version ||
5497 mddev->minor_version != info->minor_version ||
5498 /* mddev->patch_version != info->patch_version || */
5499 mddev->ctime != info->ctime ||
5500 mddev->level != info->level ||
5501 /* mddev->layout != info->layout || */
5502 !mddev->persistent != info->not_persistent||
5503 mddev->chunk_sectors != info->chunk_size >> 9 ||
5504 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
5505 ((state^info->state) & 0xfffffe00)
5507 return -EINVAL;
5508 /* Check there is only one change */
5509 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
5510 cnt++;
5511 if (mddev->raid_disks != info->raid_disks)
5512 cnt++;
5513 if (mddev->layout != info->layout)
5514 cnt++;
5515 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
5516 cnt++;
5517 if (cnt == 0)
5518 return 0;
5519 if (cnt > 1)
5520 return -EINVAL;
5522 if (mddev->layout != info->layout) {
5523 /* Change layout
5524 * we don't need to do anything at the md level, the
5525 * personality will take care of it all.
5527 if (mddev->pers->check_reshape == NULL)
5528 return -EINVAL;
5529 else {
5530 mddev->new_layout = info->layout;
5531 rv = mddev->pers->check_reshape(mddev);
5532 if (rv)
5533 mddev->new_layout = mddev->layout;
5534 return rv;
5537 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
5538 rv = update_size(mddev, (sector_t)info->size * 2);
5540 if (mddev->raid_disks != info->raid_disks)
5541 rv = update_raid_disks(mddev, info->raid_disks);
5543 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
5544 if (mddev->pers->quiesce == NULL)
5545 return -EINVAL;
5546 if (mddev->recovery || mddev->sync_thread)
5547 return -EBUSY;
5548 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
5549 /* add the bitmap */
5550 if (mddev->bitmap)
5551 return -EEXIST;
5552 if (mddev->bitmap_info.default_offset == 0)
5553 return -EINVAL;
5554 mddev->bitmap_info.offset =
5555 mddev->bitmap_info.default_offset;
5556 mddev->pers->quiesce(mddev, 1);
5557 rv = bitmap_create(mddev);
5558 if (rv)
5559 bitmap_destroy(mddev);
5560 mddev->pers->quiesce(mddev, 0);
5561 } else {
5562 /* remove the bitmap */
5563 if (!mddev->bitmap)
5564 return -ENOENT;
5565 if (mddev->bitmap->file)
5566 return -EINVAL;
5567 mddev->pers->quiesce(mddev, 1);
5568 bitmap_destroy(mddev);
5569 mddev->pers->quiesce(mddev, 0);
5570 mddev->bitmap_info.offset = 0;
5573 md_update_sb(mddev, 1);
5574 return rv;
5577 static int set_disk_faulty(mddev_t *mddev, dev_t dev)
5579 mdk_rdev_t *rdev;
5581 if (mddev->pers == NULL)
5582 return -ENODEV;
5584 rdev = find_rdev(mddev, dev);
5585 if (!rdev)
5586 return -ENODEV;
5588 md_error(mddev, rdev);
5589 return 0;
5593 * We have a problem here : there is no easy way to give a CHS
5594 * virtual geometry. We currently pretend that we have a 2 heads
5595 * 4 sectors (with a BIG number of cylinders...). This drives
5596 * dosfs just mad... ;-)
5598 static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
5600 mddev_t *mddev = bdev->bd_disk->private_data;
5602 geo->heads = 2;
5603 geo->sectors = 4;
5604 geo->cylinders = mddev->array_sectors / 8;
5605 return 0;
5608 static int md_ioctl(struct block_device *bdev, fmode_t mode,
5609 unsigned int cmd, unsigned long arg)
5611 int err = 0;
5612 void __user *argp = (void __user *)arg;
5613 mddev_t *mddev = NULL;
5614 int ro;
5616 if (!capable(CAP_SYS_ADMIN))
5617 return -EACCES;
5620 * Commands dealing with the RAID driver but not any
5621 * particular array:
5623 switch (cmd)
5625 case RAID_VERSION:
5626 err = get_version(argp);
5627 goto done;
5629 case PRINT_RAID_DEBUG:
5630 err = 0;
5631 md_print_devices();
5632 goto done;
5634 #ifndef MODULE
5635 case RAID_AUTORUN:
5636 err = 0;
5637 autostart_arrays(arg);
5638 goto done;
5639 #endif
5640 default:;
5644 * Commands creating/starting a new array:
5647 mddev = bdev->bd_disk->private_data;
5649 if (!mddev) {
5650 BUG();
5651 goto abort;
5654 err = mddev_lock(mddev);
5655 if (err) {
5656 printk(KERN_INFO
5657 "md: ioctl lock interrupted, reason %d, cmd %d\n",
5658 err, cmd);
5659 goto abort;
5662 switch (cmd)
5664 case SET_ARRAY_INFO:
5666 mdu_array_info_t info;
5667 if (!arg)
5668 memset(&info, 0, sizeof(info));
5669 else if (copy_from_user(&info, argp, sizeof(info))) {
5670 err = -EFAULT;
5671 goto abort_unlock;
5673 if (mddev->pers) {
5674 err = update_array_info(mddev, &info);
5675 if (err) {
5676 printk(KERN_WARNING "md: couldn't update"
5677 " array info. %d\n", err);
5678 goto abort_unlock;
5680 goto done_unlock;
5682 if (!list_empty(&mddev->disks)) {
5683 printk(KERN_WARNING
5684 "md: array %s already has disks!\n",
5685 mdname(mddev));
5686 err = -EBUSY;
5687 goto abort_unlock;
5689 if (mddev->raid_disks) {
5690 printk(KERN_WARNING
5691 "md: array %s already initialised!\n",
5692 mdname(mddev));
5693 err = -EBUSY;
5694 goto abort_unlock;
5696 err = set_array_info(mddev, &info);
5697 if (err) {
5698 printk(KERN_WARNING "md: couldn't set"
5699 " array info. %d\n", err);
5700 goto abort_unlock;
5703 goto done_unlock;
5705 default:;
5709 * Commands querying/configuring an existing array:
5711 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
5712 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
5713 if ((!mddev->raid_disks && !mddev->external)
5714 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
5715 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
5716 && cmd != GET_BITMAP_FILE) {
5717 err = -ENODEV;
5718 goto abort_unlock;
5722 * Commands even a read-only array can execute:
5724 switch (cmd)
5726 case GET_ARRAY_INFO:
5727 err = get_array_info(mddev, argp);
5728 goto done_unlock;
5730 case GET_BITMAP_FILE:
5731 err = get_bitmap_file(mddev, argp);
5732 goto done_unlock;
5734 case GET_DISK_INFO:
5735 err = get_disk_info(mddev, argp);
5736 goto done_unlock;
5738 case RESTART_ARRAY_RW:
5739 err = restart_array(mddev);
5740 goto done_unlock;
5742 case STOP_ARRAY:
5743 err = do_md_stop(mddev, 0, 1);
5744 goto done_unlock;
5746 case STOP_ARRAY_RO:
5747 err = md_set_readonly(mddev, 1);
5748 goto done_unlock;
5750 case BLKROSET:
5751 if (get_user(ro, (int __user *)(arg))) {
5752 err = -EFAULT;
5753 goto done_unlock;
5755 err = -EINVAL;
5757 /* if the bdev is going readonly the value of mddev->ro
5758 * does not matter, no writes are coming
5760 if (ro)
5761 goto done_unlock;
5763 /* are we are already prepared for writes? */
5764 if (mddev->ro != 1)
5765 goto done_unlock;
5767 /* transitioning to readauto need only happen for
5768 * arrays that call md_write_start
5770 if (mddev->pers) {
5771 err = restart_array(mddev);
5772 if (err == 0) {
5773 mddev->ro = 2;
5774 set_disk_ro(mddev->gendisk, 0);
5777 goto done_unlock;
5781 * The remaining ioctls are changing the state of the
5782 * superblock, so we do not allow them on read-only arrays.
5783 * However non-MD ioctls (e.g. get-size) will still come through
5784 * here and hit the 'default' below, so only disallow
5785 * 'md' ioctls, and switch to rw mode if started auto-readonly.
5787 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
5788 if (mddev->ro == 2) {
5789 mddev->ro = 0;
5790 sysfs_notify_dirent(mddev->sysfs_state);
5791 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5792 md_wakeup_thread(mddev->thread);
5793 } else {
5794 err = -EROFS;
5795 goto abort_unlock;
5799 switch (cmd)
5801 case ADD_NEW_DISK:
5803 mdu_disk_info_t info;
5804 if (copy_from_user(&info, argp, sizeof(info)))
5805 err = -EFAULT;
5806 else
5807 err = add_new_disk(mddev, &info);
5808 goto done_unlock;
5811 case HOT_REMOVE_DISK:
5812 err = hot_remove_disk(mddev, new_decode_dev(arg));
5813 goto done_unlock;
5815 case HOT_ADD_DISK:
5816 err = hot_add_disk(mddev, new_decode_dev(arg));
5817 goto done_unlock;
5819 case SET_DISK_FAULTY:
5820 err = set_disk_faulty(mddev, new_decode_dev(arg));
5821 goto done_unlock;
5823 case RUN_ARRAY:
5824 err = do_md_run(mddev);
5825 goto done_unlock;
5827 case SET_BITMAP_FILE:
5828 err = set_bitmap_file(mddev, (int)arg);
5829 goto done_unlock;
5831 default:
5832 err = -EINVAL;
5833 goto abort_unlock;
5836 done_unlock:
5837 abort_unlock:
5838 if (mddev->hold_active == UNTIL_IOCTL &&
5839 err != -EINVAL)
5840 mddev->hold_active = 0;
5841 mddev_unlock(mddev);
5843 return err;
5844 done:
5845 if (err)
5846 MD_BUG();
5847 abort:
5848 return err;
5850 #ifdef CONFIG_COMPAT
5851 static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
5852 unsigned int cmd, unsigned long arg)
5854 switch (cmd) {
5855 case HOT_REMOVE_DISK:
5856 case HOT_ADD_DISK:
5857 case SET_DISK_FAULTY:
5858 case SET_BITMAP_FILE:
5859 /* These take in integer arg, do not convert */
5860 break;
5861 default:
5862 arg = (unsigned long)compat_ptr(arg);
5863 break;
5866 return md_ioctl(bdev, mode, cmd, arg);
5868 #endif /* CONFIG_COMPAT */
5870 static int md_open(struct block_device *bdev, fmode_t mode)
5873 * Succeed if we can lock the mddev, which confirms that
5874 * it isn't being stopped right now.
5876 mddev_t *mddev = mddev_find(bdev->bd_dev);
5877 int err;
5879 if (mddev->gendisk != bdev->bd_disk) {
5880 /* we are racing with mddev_put which is discarding this
5881 * bd_disk.
5883 mddev_put(mddev);
5884 /* Wait until bdev->bd_disk is definitely gone */
5885 flush_scheduled_work();
5886 /* Then retry the open from the top */
5887 return -ERESTARTSYS;
5889 BUG_ON(mddev != bdev->bd_disk->private_data);
5891 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
5892 goto out;
5894 err = 0;
5895 atomic_inc(&mddev->openers);
5896 mutex_unlock(&mddev->open_mutex);
5898 out:
5899 return err;
5902 static int md_release(struct gendisk *disk, fmode_t mode)
5904 mddev_t *mddev = disk->private_data;
5906 BUG_ON(!mddev);
5907 atomic_dec(&mddev->openers);
5908 mddev_put(mddev);
5910 return 0;
5912 static const struct block_device_operations md_fops =
5914 .owner = THIS_MODULE,
5915 .open = md_open,
5916 .release = md_release,
5917 .ioctl = md_ioctl,
5918 #ifdef CONFIG_COMPAT
5919 .compat_ioctl = md_compat_ioctl,
5920 #endif
5921 .getgeo = md_getgeo,
5924 static int md_thread(void * arg)
5926 mdk_thread_t *thread = arg;
5929 * md_thread is a 'system-thread', it's priority should be very
5930 * high. We avoid resource deadlocks individually in each
5931 * raid personality. (RAID5 does preallocation) We also use RR and
5932 * the very same RT priority as kswapd, thus we will never get
5933 * into a priority inversion deadlock.
5935 * we definitely have to have equal or higher priority than
5936 * bdflush, otherwise bdflush will deadlock if there are too
5937 * many dirty RAID5 blocks.
5940 allow_signal(SIGKILL);
5941 while (!kthread_should_stop()) {
5943 /* We need to wait INTERRUPTIBLE so that
5944 * we don't add to the load-average.
5945 * That means we need to be sure no signals are
5946 * pending
5948 if (signal_pending(current))
5949 flush_signals(current);
5951 wait_event_interruptible_timeout
5952 (thread->wqueue,
5953 test_bit(THREAD_WAKEUP, &thread->flags)
5954 || kthread_should_stop(),
5955 thread->timeout);
5957 clear_bit(THREAD_WAKEUP, &thread->flags);
5959 thread->run(thread->mddev);
5962 return 0;
5965 void md_wakeup_thread(mdk_thread_t *thread)
5967 if (thread) {
5968 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
5969 set_bit(THREAD_WAKEUP, &thread->flags);
5970 wake_up(&thread->wqueue);
5974 mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
5975 const char *name)
5977 mdk_thread_t *thread;
5979 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
5980 if (!thread)
5981 return NULL;
5983 init_waitqueue_head(&thread->wqueue);
5985 thread->run = run;
5986 thread->mddev = mddev;
5987 thread->timeout = MAX_SCHEDULE_TIMEOUT;
5988 thread->tsk = kthread_run(md_thread, thread,
5989 "%s_%s",
5990 mdname(thread->mddev),
5991 name ?: mddev->pers->name);
5992 if (IS_ERR(thread->tsk)) {
5993 kfree(thread);
5994 return NULL;
5996 return thread;
5999 void md_unregister_thread(mdk_thread_t *thread)
6001 if (!thread)
6002 return;
6003 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
6005 kthread_stop(thread->tsk);
6006 kfree(thread);
6009 void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
6011 if (!mddev) {
6012 MD_BUG();
6013 return;
6016 if (!rdev || test_bit(Faulty, &rdev->flags))
6017 return;
6019 if (mddev->external)
6020 set_bit(Blocked, &rdev->flags);
6022 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
6023 mdname(mddev),
6024 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
6025 __builtin_return_address(0),__builtin_return_address(1),
6026 __builtin_return_address(2),__builtin_return_address(3));
6028 if (!mddev->pers)
6029 return;
6030 if (!mddev->pers->error_handler)
6031 return;
6032 mddev->pers->error_handler(mddev,rdev);
6033 if (mddev->degraded)
6034 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
6035 sysfs_notify_dirent(rdev->sysfs_state);
6036 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6037 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6038 md_wakeup_thread(mddev->thread);
6039 md_new_event_inintr(mddev);
6042 /* seq_file implementation /proc/mdstat */
6044 static void status_unused(struct seq_file *seq)
6046 int i = 0;
6047 mdk_rdev_t *rdev;
6049 seq_printf(seq, "unused devices: ");
6051 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
6052 char b[BDEVNAME_SIZE];
6053 i++;
6054 seq_printf(seq, "%s ",
6055 bdevname(rdev->bdev,b));
6057 if (!i)
6058 seq_printf(seq, "<none>");
6060 seq_printf(seq, "\n");
6064 static void status_resync(struct seq_file *seq, mddev_t * mddev)
6066 sector_t max_sectors, resync, res;
6067 unsigned long dt, db;
6068 sector_t rt;
6069 int scale;
6070 unsigned int per_milli;
6072 resync = mddev->curr_resync - atomic_read(&mddev->recovery_active);
6074 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
6075 max_sectors = mddev->resync_max_sectors;
6076 else
6077 max_sectors = mddev->dev_sectors;
6080 * Should not happen.
6082 if (!max_sectors) {
6083 MD_BUG();
6084 return;
6086 /* Pick 'scale' such that (resync>>scale)*1000 will fit
6087 * in a sector_t, and (max_sectors>>scale) will fit in a
6088 * u32, as those are the requirements for sector_div.
6089 * Thus 'scale' must be at least 10
6091 scale = 10;
6092 if (sizeof(sector_t) > sizeof(unsigned long)) {
6093 while ( max_sectors/2 > (1ULL<<(scale+32)))
6094 scale++;
6096 res = (resync>>scale)*1000;
6097 sector_div(res, (u32)((max_sectors>>scale)+1));
6099 per_milli = res;
6101 int i, x = per_milli/50, y = 20-x;
6102 seq_printf(seq, "[");
6103 for (i = 0; i < x; i++)
6104 seq_printf(seq, "=");
6105 seq_printf(seq, ">");
6106 for (i = 0; i < y; i++)
6107 seq_printf(seq, ".");
6108 seq_printf(seq, "] ");
6110 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
6111 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
6112 "reshape" :
6113 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
6114 "check" :
6115 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
6116 "resync" : "recovery"))),
6117 per_milli/10, per_milli % 10,
6118 (unsigned long long) resync/2,
6119 (unsigned long long) max_sectors/2);
6122 * dt: time from mark until now
6123 * db: blocks written from mark until now
6124 * rt: remaining time
6126 * rt is a sector_t, so could be 32bit or 64bit.
6127 * So we divide before multiply in case it is 32bit and close
6128 * to the limit.
6129 * We scale the divisor (db) by 32 to avoid loosing precision
6130 * near the end of resync when the number of remaining sectors
6131 * is close to 'db'.
6132 * We then divide rt by 32 after multiplying by db to compensate.
6133 * The '+1' avoids division by zero if db is very small.
6135 dt = ((jiffies - mddev->resync_mark) / HZ);
6136 if (!dt) dt++;
6137 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
6138 - mddev->resync_mark_cnt;
6140 rt = max_sectors - resync; /* number of remaining sectors */
6141 sector_div(rt, db/32+1);
6142 rt *= dt;
6143 rt >>= 5;
6145 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
6146 ((unsigned long)rt % 60)/6);
6148 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
6151 static void *md_seq_start(struct seq_file *seq, loff_t *pos)
6153 struct list_head *tmp;
6154 loff_t l = *pos;
6155 mddev_t *mddev;
6157 if (l >= 0x10000)
6158 return NULL;
6159 if (!l--)
6160 /* header */
6161 return (void*)1;
6163 spin_lock(&all_mddevs_lock);
6164 list_for_each(tmp,&all_mddevs)
6165 if (!l--) {
6166 mddev = list_entry(tmp, mddev_t, all_mddevs);
6167 mddev_get(mddev);
6168 spin_unlock(&all_mddevs_lock);
6169 return mddev;
6171 spin_unlock(&all_mddevs_lock);
6172 if (!l--)
6173 return (void*)2;/* tail */
6174 return NULL;
6177 static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
6179 struct list_head *tmp;
6180 mddev_t *next_mddev, *mddev = v;
6182 ++*pos;
6183 if (v == (void*)2)
6184 return NULL;
6186 spin_lock(&all_mddevs_lock);
6187 if (v == (void*)1)
6188 tmp = all_mddevs.next;
6189 else
6190 tmp = mddev->all_mddevs.next;
6191 if (tmp != &all_mddevs)
6192 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
6193 else {
6194 next_mddev = (void*)2;
6195 *pos = 0x10000;
6197 spin_unlock(&all_mddevs_lock);
6199 if (v != (void*)1)
6200 mddev_put(mddev);
6201 return next_mddev;
6205 static void md_seq_stop(struct seq_file *seq, void *v)
6207 mddev_t *mddev = v;
6209 if (mddev && v != (void*)1 && v != (void*)2)
6210 mddev_put(mddev);
6213 struct mdstat_info {
6214 int event;
6217 static int md_seq_show(struct seq_file *seq, void *v)
6219 mddev_t *mddev = v;
6220 sector_t sectors;
6221 mdk_rdev_t *rdev;
6222 struct mdstat_info *mi = seq->private;
6223 struct bitmap *bitmap;
6225 if (v == (void*)1) {
6226 struct mdk_personality *pers;
6227 seq_printf(seq, "Personalities : ");
6228 spin_lock(&pers_lock);
6229 list_for_each_entry(pers, &pers_list, list)
6230 seq_printf(seq, "[%s] ", pers->name);
6232 spin_unlock(&pers_lock);
6233 seq_printf(seq, "\n");
6234 mi->event = atomic_read(&md_event_count);
6235 return 0;
6237 if (v == (void*)2) {
6238 status_unused(seq);
6239 return 0;
6242 if (mddev_lock(mddev) < 0)
6243 return -EINTR;
6245 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
6246 seq_printf(seq, "%s : %sactive", mdname(mddev),
6247 mddev->pers ? "" : "in");
6248 if (mddev->pers) {
6249 if (mddev->ro==1)
6250 seq_printf(seq, " (read-only)");
6251 if (mddev->ro==2)
6252 seq_printf(seq, " (auto-read-only)");
6253 seq_printf(seq, " %s", mddev->pers->name);
6256 sectors = 0;
6257 list_for_each_entry(rdev, &mddev->disks, same_set) {
6258 char b[BDEVNAME_SIZE];
6259 seq_printf(seq, " %s[%d]",
6260 bdevname(rdev->bdev,b), rdev->desc_nr);
6261 if (test_bit(WriteMostly, &rdev->flags))
6262 seq_printf(seq, "(W)");
6263 if (test_bit(Faulty, &rdev->flags)) {
6264 seq_printf(seq, "(F)");
6265 continue;
6266 } else if (rdev->raid_disk < 0)
6267 seq_printf(seq, "(S)"); /* spare */
6268 sectors += rdev->sectors;
6271 if (!list_empty(&mddev->disks)) {
6272 if (mddev->pers)
6273 seq_printf(seq, "\n %llu blocks",
6274 (unsigned long long)
6275 mddev->array_sectors / 2);
6276 else
6277 seq_printf(seq, "\n %llu blocks",
6278 (unsigned long long)sectors / 2);
6280 if (mddev->persistent) {
6281 if (mddev->major_version != 0 ||
6282 mddev->minor_version != 90) {
6283 seq_printf(seq," super %d.%d",
6284 mddev->major_version,
6285 mddev->minor_version);
6287 } else if (mddev->external)
6288 seq_printf(seq, " super external:%s",
6289 mddev->metadata_type);
6290 else
6291 seq_printf(seq, " super non-persistent");
6293 if (mddev->pers) {
6294 mddev->pers->status(seq, mddev);
6295 seq_printf(seq, "\n ");
6296 if (mddev->pers->sync_request) {
6297 if (mddev->curr_resync > 2) {
6298 status_resync(seq, mddev);
6299 seq_printf(seq, "\n ");
6300 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
6301 seq_printf(seq, "\tresync=DELAYED\n ");
6302 else if (mddev->recovery_cp < MaxSector)
6303 seq_printf(seq, "\tresync=PENDING\n ");
6305 } else
6306 seq_printf(seq, "\n ");
6308 if ((bitmap = mddev->bitmap)) {
6309 unsigned long chunk_kb;
6310 unsigned long flags;
6311 spin_lock_irqsave(&bitmap->lock, flags);
6312 chunk_kb = mddev->bitmap_info.chunksize >> 10;
6313 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
6314 "%lu%s chunk",
6315 bitmap->pages - bitmap->missing_pages,
6316 bitmap->pages,
6317 (bitmap->pages - bitmap->missing_pages)
6318 << (PAGE_SHIFT - 10),
6319 chunk_kb ? chunk_kb : mddev->bitmap_info.chunksize,
6320 chunk_kb ? "KB" : "B");
6321 if (bitmap->file) {
6322 seq_printf(seq, ", file: ");
6323 seq_path(seq, &bitmap->file->f_path, " \t\n");
6326 seq_printf(seq, "\n");
6327 spin_unlock_irqrestore(&bitmap->lock, flags);
6330 seq_printf(seq, "\n");
6332 mddev_unlock(mddev);
6334 return 0;
6337 static const struct seq_operations md_seq_ops = {
6338 .start = md_seq_start,
6339 .next = md_seq_next,
6340 .stop = md_seq_stop,
6341 .show = md_seq_show,
6344 static int md_seq_open(struct inode *inode, struct file *file)
6346 int error;
6347 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
6348 if (mi == NULL)
6349 return -ENOMEM;
6351 error = seq_open(file, &md_seq_ops);
6352 if (error)
6353 kfree(mi);
6354 else {
6355 struct seq_file *p = file->private_data;
6356 p->private = mi;
6357 mi->event = atomic_read(&md_event_count);
6359 return error;
6362 static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
6364 struct seq_file *m = filp->private_data;
6365 struct mdstat_info *mi = m->private;
6366 int mask;
6368 poll_wait(filp, &md_event_waiters, wait);
6370 /* always allow read */
6371 mask = POLLIN | POLLRDNORM;
6373 if (mi->event != atomic_read(&md_event_count))
6374 mask |= POLLERR | POLLPRI;
6375 return mask;
6378 static const struct file_operations md_seq_fops = {
6379 .owner = THIS_MODULE,
6380 .open = md_seq_open,
6381 .read = seq_read,
6382 .llseek = seq_lseek,
6383 .release = seq_release_private,
6384 .poll = mdstat_poll,
6387 int register_md_personality(struct mdk_personality *p)
6389 spin_lock(&pers_lock);
6390 list_add_tail(&p->list, &pers_list);
6391 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
6392 spin_unlock(&pers_lock);
6393 return 0;
6396 int unregister_md_personality(struct mdk_personality *p)
6398 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
6399 spin_lock(&pers_lock);
6400 list_del_init(&p->list);
6401 spin_unlock(&pers_lock);
6402 return 0;
6405 static int is_mddev_idle(mddev_t *mddev, int init)
6407 mdk_rdev_t * rdev;
6408 int idle;
6409 int curr_events;
6411 idle = 1;
6412 rcu_read_lock();
6413 rdev_for_each_rcu(rdev, mddev) {
6414 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
6415 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
6416 (int)part_stat_read(&disk->part0, sectors[1]) -
6417 atomic_read(&disk->sync_io);
6418 /* sync IO will cause sync_io to increase before the disk_stats
6419 * as sync_io is counted when a request starts, and
6420 * disk_stats is counted when it completes.
6421 * So resync activity will cause curr_events to be smaller than
6422 * when there was no such activity.
6423 * non-sync IO will cause disk_stat to increase without
6424 * increasing sync_io so curr_events will (eventually)
6425 * be larger than it was before. Once it becomes
6426 * substantially larger, the test below will cause
6427 * the array to appear non-idle, and resync will slow
6428 * down.
6429 * If there is a lot of outstanding resync activity when
6430 * we set last_event to curr_events, then all that activity
6431 * completing might cause the array to appear non-idle
6432 * and resync will be slowed down even though there might
6433 * not have been non-resync activity. This will only
6434 * happen once though. 'last_events' will soon reflect
6435 * the state where there is little or no outstanding
6436 * resync requests, and further resync activity will
6437 * always make curr_events less than last_events.
6440 if (init || curr_events - rdev->last_events > 64) {
6441 rdev->last_events = curr_events;
6442 idle = 0;
6445 rcu_read_unlock();
6446 return idle;
6449 void md_done_sync(mddev_t *mddev, int blocks, int ok)
6451 /* another "blocks" (512byte) blocks have been synced */
6452 atomic_sub(blocks, &mddev->recovery_active);
6453 wake_up(&mddev->recovery_wait);
6454 if (!ok) {
6455 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6456 md_wakeup_thread(mddev->thread);
6457 // stop recovery, signal do_sync ....
6462 /* md_write_start(mddev, bi)
6463 * If we need to update some array metadata (e.g. 'active' flag
6464 * in superblock) before writing, schedule a superblock update
6465 * and wait for it to complete.
6467 void md_write_start(mddev_t *mddev, struct bio *bi)
6469 int did_change = 0;
6470 if (bio_data_dir(bi) != WRITE)
6471 return;
6473 BUG_ON(mddev->ro == 1);
6474 if (mddev->ro == 2) {
6475 /* need to switch to read/write */
6476 mddev->ro = 0;
6477 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6478 md_wakeup_thread(mddev->thread);
6479 md_wakeup_thread(mddev->sync_thread);
6480 did_change = 1;
6482 atomic_inc(&mddev->writes_pending);
6483 if (mddev->safemode == 1)
6484 mddev->safemode = 0;
6485 if (mddev->in_sync) {
6486 spin_lock_irq(&mddev->write_lock);
6487 if (mddev->in_sync) {
6488 mddev->in_sync = 0;
6489 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6490 md_wakeup_thread(mddev->thread);
6491 did_change = 1;
6493 spin_unlock_irq(&mddev->write_lock);
6495 if (did_change)
6496 sysfs_notify_dirent(mddev->sysfs_state);
6497 wait_event(mddev->sb_wait,
6498 !test_bit(MD_CHANGE_CLEAN, &mddev->flags) &&
6499 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
6502 void md_write_end(mddev_t *mddev)
6504 if (atomic_dec_and_test(&mddev->writes_pending)) {
6505 if (mddev->safemode == 2)
6506 md_wakeup_thread(mddev->thread);
6507 else if (mddev->safemode_delay)
6508 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
6512 /* md_allow_write(mddev)
6513 * Calling this ensures that the array is marked 'active' so that writes
6514 * may proceed without blocking. It is important to call this before
6515 * attempting a GFP_KERNEL allocation while holding the mddev lock.
6516 * Must be called with mddev_lock held.
6518 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
6519 * is dropped, so return -EAGAIN after notifying userspace.
6521 int md_allow_write(mddev_t *mddev)
6523 if (!mddev->pers)
6524 return 0;
6525 if (mddev->ro)
6526 return 0;
6527 if (!mddev->pers->sync_request)
6528 return 0;
6530 spin_lock_irq(&mddev->write_lock);
6531 if (mddev->in_sync) {
6532 mddev->in_sync = 0;
6533 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6534 if (mddev->safemode_delay &&
6535 mddev->safemode == 0)
6536 mddev->safemode = 1;
6537 spin_unlock_irq(&mddev->write_lock);
6538 md_update_sb(mddev, 0);
6539 sysfs_notify_dirent(mddev->sysfs_state);
6540 } else
6541 spin_unlock_irq(&mddev->write_lock);
6543 if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
6544 return -EAGAIN;
6545 else
6546 return 0;
6548 EXPORT_SYMBOL_GPL(md_allow_write);
6550 #define SYNC_MARKS 10
6551 #define SYNC_MARK_STEP (3*HZ)
6552 void md_do_sync(mddev_t *mddev)
6554 mddev_t *mddev2;
6555 unsigned int currspeed = 0,
6556 window;
6557 sector_t max_sectors,j, io_sectors;
6558 unsigned long mark[SYNC_MARKS];
6559 sector_t mark_cnt[SYNC_MARKS];
6560 int last_mark,m;
6561 struct list_head *tmp;
6562 sector_t last_check;
6563 int skipped = 0;
6564 mdk_rdev_t *rdev;
6565 char *desc;
6567 /* just incase thread restarts... */
6568 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
6569 return;
6570 if (mddev->ro) /* never try to sync a read-only array */
6571 return;
6573 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6574 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
6575 desc = "data-check";
6576 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6577 desc = "requested-resync";
6578 else
6579 desc = "resync";
6580 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
6581 desc = "reshape";
6582 else
6583 desc = "recovery";
6585 /* we overload curr_resync somewhat here.
6586 * 0 == not engaged in resync at all
6587 * 2 == checking that there is no conflict with another sync
6588 * 1 == like 2, but have yielded to allow conflicting resync to
6589 * commense
6590 * other == active in resync - this many blocks
6592 * Before starting a resync we must have set curr_resync to
6593 * 2, and then checked that every "conflicting" array has curr_resync
6594 * less than ours. When we find one that is the same or higher
6595 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
6596 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
6597 * This will mean we have to start checking from the beginning again.
6601 do {
6602 mddev->curr_resync = 2;
6604 try_again:
6605 if (kthread_should_stop())
6606 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6608 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6609 goto skip;
6610 for_each_mddev(mddev2, tmp) {
6611 if (mddev2 == mddev)
6612 continue;
6613 if (!mddev->parallel_resync
6614 && mddev2->curr_resync
6615 && match_mddev_units(mddev, mddev2)) {
6616 DEFINE_WAIT(wq);
6617 if (mddev < mddev2 && mddev->curr_resync == 2) {
6618 /* arbitrarily yield */
6619 mddev->curr_resync = 1;
6620 wake_up(&resync_wait);
6622 if (mddev > mddev2 && mddev->curr_resync == 1)
6623 /* no need to wait here, we can wait the next
6624 * time 'round when curr_resync == 2
6626 continue;
6627 /* We need to wait 'interruptible' so as not to
6628 * contribute to the load average, and not to
6629 * be caught by 'softlockup'
6631 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
6632 if (!kthread_should_stop() &&
6633 mddev2->curr_resync >= mddev->curr_resync) {
6634 printk(KERN_INFO "md: delaying %s of %s"
6635 " until %s has finished (they"
6636 " share one or more physical units)\n",
6637 desc, mdname(mddev), mdname(mddev2));
6638 mddev_put(mddev2);
6639 if (signal_pending(current))
6640 flush_signals(current);
6641 schedule();
6642 finish_wait(&resync_wait, &wq);
6643 goto try_again;
6645 finish_wait(&resync_wait, &wq);
6648 } while (mddev->curr_resync < 2);
6650 j = 0;
6651 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6652 /* resync follows the size requested by the personality,
6653 * which defaults to physical size, but can be virtual size
6655 max_sectors = mddev->resync_max_sectors;
6656 mddev->resync_mismatches = 0;
6657 /* we don't use the checkpoint if there's a bitmap */
6658 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6659 j = mddev->resync_min;
6660 else if (!mddev->bitmap)
6661 j = mddev->recovery_cp;
6663 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
6664 max_sectors = mddev->dev_sectors;
6665 else {
6666 /* recovery follows the physical size of devices */
6667 max_sectors = mddev->dev_sectors;
6668 j = MaxSector;
6669 rcu_read_lock();
6670 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
6671 if (rdev->raid_disk >= 0 &&
6672 !test_bit(Faulty, &rdev->flags) &&
6673 !test_bit(In_sync, &rdev->flags) &&
6674 rdev->recovery_offset < j)
6675 j = rdev->recovery_offset;
6676 rcu_read_unlock();
6679 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
6680 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
6681 " %d KB/sec/disk.\n", speed_min(mddev));
6682 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
6683 "(but not more than %d KB/sec) for %s.\n",
6684 speed_max(mddev), desc);
6686 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
6688 io_sectors = 0;
6689 for (m = 0; m < SYNC_MARKS; m++) {
6690 mark[m] = jiffies;
6691 mark_cnt[m] = io_sectors;
6693 last_mark = 0;
6694 mddev->resync_mark = mark[last_mark];
6695 mddev->resync_mark_cnt = mark_cnt[last_mark];
6698 * Tune reconstruction:
6700 window = 32*(PAGE_SIZE/512);
6701 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
6702 window/2,(unsigned long long) max_sectors/2);
6704 atomic_set(&mddev->recovery_active, 0);
6705 last_check = 0;
6707 if (j>2) {
6708 printk(KERN_INFO
6709 "md: resuming %s of %s from checkpoint.\n",
6710 desc, mdname(mddev));
6711 mddev->curr_resync = j;
6713 mddev->curr_resync_completed = mddev->curr_resync;
6715 while (j < max_sectors) {
6716 sector_t sectors;
6718 skipped = 0;
6720 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
6721 ((mddev->curr_resync > mddev->curr_resync_completed &&
6722 (mddev->curr_resync - mddev->curr_resync_completed)
6723 > (max_sectors >> 4)) ||
6724 (j - mddev->curr_resync_completed)*2
6725 >= mddev->resync_max - mddev->curr_resync_completed
6726 )) {
6727 /* time to update curr_resync_completed */
6728 blk_unplug(mddev->queue);
6729 wait_event(mddev->recovery_wait,
6730 atomic_read(&mddev->recovery_active) == 0);
6731 mddev->curr_resync_completed =
6732 mddev->curr_resync;
6733 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6734 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
6737 while (j >= mddev->resync_max && !kthread_should_stop()) {
6738 /* As this condition is controlled by user-space,
6739 * we can block indefinitely, so use '_interruptible'
6740 * to avoid triggering warnings.
6742 flush_signals(current); /* just in case */
6743 wait_event_interruptible(mddev->recovery_wait,
6744 mddev->resync_max > j
6745 || kthread_should_stop());
6748 if (kthread_should_stop())
6749 goto interrupted;
6751 sectors = mddev->pers->sync_request(mddev, j, &skipped,
6752 currspeed < speed_min(mddev));
6753 if (sectors == 0) {
6754 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6755 goto out;
6758 if (!skipped) { /* actual IO requested */
6759 io_sectors += sectors;
6760 atomic_add(sectors, &mddev->recovery_active);
6763 j += sectors;
6764 if (j>1) mddev->curr_resync = j;
6765 mddev->curr_mark_cnt = io_sectors;
6766 if (last_check == 0)
6767 /* this is the earliers that rebuilt will be
6768 * visible in /proc/mdstat
6770 md_new_event(mddev);
6772 if (last_check + window > io_sectors || j == max_sectors)
6773 continue;
6775 last_check = io_sectors;
6777 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6778 break;
6780 repeat:
6781 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
6782 /* step marks */
6783 int next = (last_mark+1) % SYNC_MARKS;
6785 mddev->resync_mark = mark[next];
6786 mddev->resync_mark_cnt = mark_cnt[next];
6787 mark[next] = jiffies;
6788 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
6789 last_mark = next;
6793 if (kthread_should_stop())
6794 goto interrupted;
6798 * this loop exits only if either when we are slower than
6799 * the 'hard' speed limit, or the system was IO-idle for
6800 * a jiffy.
6801 * the system might be non-idle CPU-wise, but we only care
6802 * about not overloading the IO subsystem. (things like an
6803 * e2fsck being done on the RAID array should execute fast)
6805 blk_unplug(mddev->queue);
6806 cond_resched();
6808 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
6809 /((jiffies-mddev->resync_mark)/HZ +1) +1;
6811 if (currspeed > speed_min(mddev)) {
6812 if ((currspeed > speed_max(mddev)) ||
6813 !is_mddev_idle(mddev, 0)) {
6814 msleep(500);
6815 goto repeat;
6819 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
6821 * this also signals 'finished resyncing' to md_stop
6823 out:
6824 blk_unplug(mddev->queue);
6826 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
6828 /* tell personality that we are finished */
6829 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
6831 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
6832 mddev->curr_resync > 2) {
6833 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6834 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6835 if (mddev->curr_resync >= mddev->recovery_cp) {
6836 printk(KERN_INFO
6837 "md: checkpointing %s of %s.\n",
6838 desc, mdname(mddev));
6839 mddev->recovery_cp = mddev->curr_resync;
6841 } else
6842 mddev->recovery_cp = MaxSector;
6843 } else {
6844 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6845 mddev->curr_resync = MaxSector;
6846 rcu_read_lock();
6847 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
6848 if (rdev->raid_disk >= 0 &&
6849 !test_bit(Faulty, &rdev->flags) &&
6850 !test_bit(In_sync, &rdev->flags) &&
6851 rdev->recovery_offset < mddev->curr_resync)
6852 rdev->recovery_offset = mddev->curr_resync;
6853 rcu_read_unlock();
6856 set_bit(MD_CHANGE_DEVS, &mddev->flags);
6858 skip:
6859 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6860 /* We completed so min/max setting can be forgotten if used. */
6861 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6862 mddev->resync_min = 0;
6863 mddev->resync_max = MaxSector;
6864 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6865 mddev->resync_min = mddev->curr_resync_completed;
6866 mddev->curr_resync = 0;
6867 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6868 mddev->curr_resync_completed = 0;
6869 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
6870 wake_up(&resync_wait);
6871 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
6872 md_wakeup_thread(mddev->thread);
6873 return;
6875 interrupted:
6877 * got a signal, exit.
6879 printk(KERN_INFO
6880 "md: md_do_sync() got signal ... exiting\n");
6881 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6882 goto out;
6885 EXPORT_SYMBOL_GPL(md_do_sync);
6888 static int remove_and_add_spares(mddev_t *mddev)
6890 mdk_rdev_t *rdev;
6891 int spares = 0;
6893 mddev->curr_resync_completed = 0;
6895 list_for_each_entry(rdev, &mddev->disks, same_set)
6896 if (rdev->raid_disk >= 0 &&
6897 !test_bit(Blocked, &rdev->flags) &&
6898 (test_bit(Faulty, &rdev->flags) ||
6899 ! test_bit(In_sync, &rdev->flags)) &&
6900 atomic_read(&rdev->nr_pending)==0) {
6901 if (mddev->pers->hot_remove_disk(
6902 mddev, rdev->raid_disk)==0) {
6903 char nm[20];
6904 sprintf(nm,"rd%d", rdev->raid_disk);
6905 sysfs_remove_link(&mddev->kobj, nm);
6906 rdev->raid_disk = -1;
6910 if (mddev->degraded && ! mddev->ro && !mddev->recovery_disabled) {
6911 list_for_each_entry(rdev, &mddev->disks, same_set) {
6912 if (rdev->raid_disk >= 0 &&
6913 !test_bit(In_sync, &rdev->flags) &&
6914 !test_bit(Blocked, &rdev->flags))
6915 spares++;
6916 if (rdev->raid_disk < 0
6917 && !test_bit(Faulty, &rdev->flags)) {
6918 rdev->recovery_offset = 0;
6919 if (mddev->pers->
6920 hot_add_disk(mddev, rdev) == 0) {
6921 char nm[20];
6922 sprintf(nm, "rd%d", rdev->raid_disk);
6923 if (sysfs_create_link(&mddev->kobj,
6924 &rdev->kobj, nm))
6925 printk(KERN_WARNING
6926 "md: cannot register "
6927 "%s for %s\n",
6928 nm, mdname(mddev));
6929 spares++;
6930 md_new_event(mddev);
6931 set_bit(MD_CHANGE_DEVS, &mddev->flags);
6932 } else
6933 break;
6937 return spares;
6940 * This routine is regularly called by all per-raid-array threads to
6941 * deal with generic issues like resync and super-block update.
6942 * Raid personalities that don't have a thread (linear/raid0) do not
6943 * need this as they never do any recovery or update the superblock.
6945 * It does not do any resync itself, but rather "forks" off other threads
6946 * to do that as needed.
6947 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
6948 * "->recovery" and create a thread at ->sync_thread.
6949 * When the thread finishes it sets MD_RECOVERY_DONE
6950 * and wakeups up this thread which will reap the thread and finish up.
6951 * This thread also removes any faulty devices (with nr_pending == 0).
6953 * The overall approach is:
6954 * 1/ if the superblock needs updating, update it.
6955 * 2/ If a recovery thread is running, don't do anything else.
6956 * 3/ If recovery has finished, clean up, possibly marking spares active.
6957 * 4/ If there are any faulty devices, remove them.
6958 * 5/ If array is degraded, try to add spares devices
6959 * 6/ If array has spares or is not in-sync, start a resync thread.
6961 void md_check_recovery(mddev_t *mddev)
6963 mdk_rdev_t *rdev;
6966 if (mddev->bitmap)
6967 bitmap_daemon_work(mddev);
6969 if (mddev->ro)
6970 return;
6972 if (signal_pending(current)) {
6973 if (mddev->pers->sync_request && !mddev->external) {
6974 printk(KERN_INFO "md: %s in immediate safe mode\n",
6975 mdname(mddev));
6976 mddev->safemode = 2;
6978 flush_signals(current);
6981 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
6982 return;
6983 if ( ! (
6984 (mddev->flags && !mddev->external) ||
6985 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
6986 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
6987 (mddev->external == 0 && mddev->safemode == 1) ||
6988 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
6989 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
6991 return;
6993 if (mddev_trylock(mddev)) {
6994 int spares = 0;
6996 if (mddev->ro) {
6997 /* Only thing we do on a ro array is remove
6998 * failed devices.
7000 remove_and_add_spares(mddev);
7001 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7002 goto unlock;
7005 if (!mddev->external) {
7006 int did_change = 0;
7007 spin_lock_irq(&mddev->write_lock);
7008 if (mddev->safemode &&
7009 !atomic_read(&mddev->writes_pending) &&
7010 !mddev->in_sync &&
7011 mddev->recovery_cp == MaxSector) {
7012 mddev->in_sync = 1;
7013 did_change = 1;
7014 if (mddev->persistent)
7015 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
7017 if (mddev->safemode == 1)
7018 mddev->safemode = 0;
7019 spin_unlock_irq(&mddev->write_lock);
7020 if (did_change)
7021 sysfs_notify_dirent(mddev->sysfs_state);
7024 if (mddev->flags)
7025 md_update_sb(mddev, 0);
7027 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
7028 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
7029 /* resync/recovery still happening */
7030 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7031 goto unlock;
7033 if (mddev->sync_thread) {
7034 /* resync has finished, collect result */
7035 md_unregister_thread(mddev->sync_thread);
7036 mddev->sync_thread = NULL;
7037 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
7038 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
7039 /* success...*/
7040 /* activate any spares */
7041 if (mddev->pers->spare_active(mddev))
7042 sysfs_notify(&mddev->kobj, NULL,
7043 "degraded");
7045 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7046 mddev->pers->finish_reshape)
7047 mddev->pers->finish_reshape(mddev);
7048 md_update_sb(mddev, 1);
7050 /* if array is no-longer degraded, then any saved_raid_disk
7051 * information must be scrapped
7053 if (!mddev->degraded)
7054 list_for_each_entry(rdev, &mddev->disks, same_set)
7055 rdev->saved_raid_disk = -1;
7057 mddev->recovery = 0;
7058 /* flag recovery needed just to double check */
7059 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7060 sysfs_notify_dirent(mddev->sysfs_action);
7061 md_new_event(mddev);
7062 goto unlock;
7064 /* Set RUNNING before clearing NEEDED to avoid
7065 * any transients in the value of "sync_action".
7067 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7068 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7069 /* Clear some bits that don't mean anything, but
7070 * might be left set
7072 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
7073 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
7075 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
7076 goto unlock;
7077 /* no recovery is running.
7078 * remove any failed drives, then
7079 * add spares if possible.
7080 * Spare are also removed and re-added, to allow
7081 * the personality to fail the re-add.
7084 if (mddev->reshape_position != MaxSector) {
7085 if (mddev->pers->check_reshape == NULL ||
7086 mddev->pers->check_reshape(mddev) != 0)
7087 /* Cannot proceed */
7088 goto unlock;
7089 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7090 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
7091 } else if ((spares = remove_and_add_spares(mddev))) {
7092 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7093 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
7094 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7095 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
7096 } else if (mddev->recovery_cp < MaxSector) {
7097 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7098 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
7099 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
7100 /* nothing to be done ... */
7101 goto unlock;
7103 if (mddev->pers->sync_request) {
7104 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
7105 /* We are adding a device or devices to an array
7106 * which has the bitmap stored on all devices.
7107 * So make sure all bitmap pages get written
7109 bitmap_write_all(mddev->bitmap);
7111 mddev->sync_thread = md_register_thread(md_do_sync,
7112 mddev,
7113 "resync");
7114 if (!mddev->sync_thread) {
7115 printk(KERN_ERR "%s: could not start resync"
7116 " thread...\n",
7117 mdname(mddev));
7118 /* leave the spares where they are, it shouldn't hurt */
7119 mddev->recovery = 0;
7120 } else
7121 md_wakeup_thread(mddev->sync_thread);
7122 sysfs_notify_dirent(mddev->sysfs_action);
7123 md_new_event(mddev);
7125 unlock:
7126 if (!mddev->sync_thread) {
7127 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7128 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
7129 &mddev->recovery))
7130 if (mddev->sysfs_action)
7131 sysfs_notify_dirent(mddev->sysfs_action);
7133 mddev_unlock(mddev);
7137 void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
7139 sysfs_notify_dirent(rdev->sysfs_state);
7140 wait_event_timeout(rdev->blocked_wait,
7141 !test_bit(Blocked, &rdev->flags),
7142 msecs_to_jiffies(5000));
7143 rdev_dec_pending(rdev, mddev);
7145 EXPORT_SYMBOL(md_wait_for_blocked_rdev);
7147 static int md_notify_reboot(struct notifier_block *this,
7148 unsigned long code, void *x)
7150 struct list_head *tmp;
7151 mddev_t *mddev;
7153 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
7155 printk(KERN_INFO "md: stopping all md devices.\n");
7157 for_each_mddev(mddev, tmp)
7158 if (mddev_trylock(mddev)) {
7159 /* Force a switch to readonly even array
7160 * appears to still be in use. Hence
7161 * the '100'.
7163 md_set_readonly(mddev, 100);
7164 mddev_unlock(mddev);
7167 * certain more exotic SCSI devices are known to be
7168 * volatile wrt too early system reboots. While the
7169 * right place to handle this issue is the given
7170 * driver, we do want to have a safe RAID driver ...
7172 mdelay(1000*1);
7174 return NOTIFY_DONE;
7177 static struct notifier_block md_notifier = {
7178 .notifier_call = md_notify_reboot,
7179 .next = NULL,
7180 .priority = INT_MAX, /* before any real devices */
7183 static void md_geninit(void)
7185 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
7187 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
7190 static int __init md_init(void)
7192 if (register_blkdev(MD_MAJOR, "md"))
7193 return -1;
7194 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
7195 unregister_blkdev(MD_MAJOR, "md");
7196 return -1;
7198 blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE,
7199 md_probe, NULL, NULL);
7200 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
7201 md_probe, NULL, NULL);
7203 register_reboot_notifier(&md_notifier);
7204 raid_table_header = register_sysctl_table(raid_root_table);
7206 md_geninit();
7207 return 0;
7211 #ifndef MODULE
7214 * Searches all registered partitions for autorun RAID arrays
7215 * at boot time.
7218 static LIST_HEAD(all_detected_devices);
7219 struct detected_devices_node {
7220 struct list_head list;
7221 dev_t dev;
7224 void md_autodetect_dev(dev_t dev)
7226 struct detected_devices_node *node_detected_dev;
7228 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
7229 if (node_detected_dev) {
7230 node_detected_dev->dev = dev;
7231 list_add_tail(&node_detected_dev->list, &all_detected_devices);
7232 } else {
7233 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
7234 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
7239 static void autostart_arrays(int part)
7241 mdk_rdev_t *rdev;
7242 struct detected_devices_node *node_detected_dev;
7243 dev_t dev;
7244 int i_scanned, i_passed;
7246 i_scanned = 0;
7247 i_passed = 0;
7249 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
7251 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
7252 i_scanned++;
7253 node_detected_dev = list_entry(all_detected_devices.next,
7254 struct detected_devices_node, list);
7255 list_del(&node_detected_dev->list);
7256 dev = node_detected_dev->dev;
7257 kfree(node_detected_dev);
7258 rdev = md_import_device(dev,0, 90);
7259 if (IS_ERR(rdev))
7260 continue;
7262 if (test_bit(Faulty, &rdev->flags)) {
7263 MD_BUG();
7264 continue;
7266 set_bit(AutoDetected, &rdev->flags);
7267 list_add(&rdev->same_set, &pending_raid_disks);
7268 i_passed++;
7271 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
7272 i_scanned, i_passed);
7274 autorun_devices(part);
7277 #endif /* !MODULE */
7279 static __exit void md_exit(void)
7281 mddev_t *mddev;
7282 struct list_head *tmp;
7284 blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS);
7285 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
7287 unregister_blkdev(MD_MAJOR,"md");
7288 unregister_blkdev(mdp_major, "mdp");
7289 unregister_reboot_notifier(&md_notifier);
7290 unregister_sysctl_table(raid_table_header);
7291 remove_proc_entry("mdstat", NULL);
7292 for_each_mddev(mddev, tmp) {
7293 export_array(mddev);
7294 mddev->hold_active = 0;
7298 subsys_initcall(md_init);
7299 module_exit(md_exit)
7301 static int get_ro(char *buffer, struct kernel_param *kp)
7303 return sprintf(buffer, "%d", start_readonly);
7305 static int set_ro(const char *val, struct kernel_param *kp)
7307 char *e;
7308 int num = simple_strtoul(val, &e, 10);
7309 if (*val && (*e == '\0' || *e == '\n')) {
7310 start_readonly = num;
7311 return 0;
7313 return -EINVAL;
7316 module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
7317 module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
7319 module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
7321 EXPORT_SYMBOL(register_md_personality);
7322 EXPORT_SYMBOL(unregister_md_personality);
7323 EXPORT_SYMBOL(md_error);
7324 EXPORT_SYMBOL(md_done_sync);
7325 EXPORT_SYMBOL(md_write_start);
7326 EXPORT_SYMBOL(md_write_end);
7327 EXPORT_SYMBOL(md_register_thread);
7328 EXPORT_SYMBOL(md_unregister_thread);
7329 EXPORT_SYMBOL(md_wakeup_thread);
7330 EXPORT_SYMBOL(md_check_recovery);
7331 MODULE_LICENSE("GPL");
7332 MODULE_DESCRIPTION("MD RAID framework");
7333 MODULE_ALIAS("md");
7334 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);