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
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)
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>
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/module.h>
47 #include <linux/reboot.h>
48 #include <linux/file.h>
49 #include <linux/compat.h>
50 #include <linux/delay.h>
51 #include <linux/raid/md_p.h>
52 #include <linux/raid/md_u.h>
53 #include <linux/slab.h>
58 static void autostart_arrays(int part);
61 /* pers_list is a list of registered personalities protected
63 * pers_lock does extra service to protect accesses to
64 * mddev->thread when the mutex cannot be held.
66 static LIST_HEAD(pers_list);
67 static DEFINE_SPINLOCK(pers_lock);
69 static void md_print_devices(void);
71 static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
72 static struct workqueue_struct *md_wq;
73 static struct workqueue_struct *md_misc_wq;
75 static int remove_and_add_spares(struct mddev *mddev,
76 struct md_rdev *this);
78 #define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
81 * Default number of read corrections we'll attempt on an rdev
82 * before ejecting it from the array. We divide the read error
83 * count by 2 for every hour elapsed between read errors.
85 #define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
87 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
88 * is 1000 KB/sec, so the extra system load does not show up that much.
89 * Increase it if you want to have more _guaranteed_ speed. Note that
90 * the RAID driver will use the maximum available bandwidth if the IO
91 * subsystem is idle. There is also an 'absolute maximum' reconstruction
92 * speed limit - in case reconstruction slows down your system despite
95 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
96 * or /sys/block/mdX/md/sync_speed_{min,max}
99 static int sysctl_speed_limit_min = 1000;
100 static int sysctl_speed_limit_max = 200000;
101 static inline int speed_min(struct mddev *mddev)
103 return mddev->sync_speed_min ?
104 mddev->sync_speed_min : sysctl_speed_limit_min;
107 static inline int speed_max(struct mddev *mddev)
109 return mddev->sync_speed_max ?
110 mddev->sync_speed_max : sysctl_speed_limit_max;
113 static struct ctl_table_header *raid_table_header;
115 static struct ctl_table raid_table[] = {
117 .procname = "speed_limit_min",
118 .data = &sysctl_speed_limit_min,
119 .maxlen = sizeof(int),
120 .mode = S_IRUGO|S_IWUSR,
121 .proc_handler = proc_dointvec,
124 .procname = "speed_limit_max",
125 .data = &sysctl_speed_limit_max,
126 .maxlen = sizeof(int),
127 .mode = S_IRUGO|S_IWUSR,
128 .proc_handler = proc_dointvec,
133 static struct ctl_table raid_dir_table[] = {
137 .mode = S_IRUGO|S_IXUGO,
143 static struct ctl_table raid_root_table[] = {
148 .child = raid_dir_table,
153 static const struct block_device_operations md_fops;
155 static int start_readonly;
158 * like bio_clone, but with a local bio set
161 struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs,
166 if (!mddev || !mddev->bio_set)
167 return bio_alloc(gfp_mask, nr_iovecs);
169 b = bio_alloc_bioset(gfp_mask, nr_iovecs, mddev->bio_set);
174 EXPORT_SYMBOL_GPL(bio_alloc_mddev);
176 struct bio *bio_clone_mddev(struct bio *bio, gfp_t gfp_mask,
179 if (!mddev || !mddev->bio_set)
180 return bio_clone(bio, gfp_mask);
182 return bio_clone_bioset(bio, gfp_mask, mddev->bio_set);
184 EXPORT_SYMBOL_GPL(bio_clone_mddev);
187 * We have a system wide 'event count' that is incremented
188 * on any 'interesting' event, and readers of /proc/mdstat
189 * can use 'poll' or 'select' to find out when the event
193 * start array, stop array, error, add device, remove device,
194 * start build, activate spare
196 static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
197 static atomic_t md_event_count;
198 void md_new_event(struct mddev *mddev)
200 atomic_inc(&md_event_count);
201 wake_up(&md_event_waiters);
203 EXPORT_SYMBOL_GPL(md_new_event);
205 /* Alternate version that can be called from interrupts
206 * when calling sysfs_notify isn't needed.
208 static void md_new_event_inintr(struct mddev *mddev)
210 atomic_inc(&md_event_count);
211 wake_up(&md_event_waiters);
215 * Enables to iterate over all existing md arrays
216 * all_mddevs_lock protects this list.
218 static LIST_HEAD(all_mddevs);
219 static DEFINE_SPINLOCK(all_mddevs_lock);
223 * iterates through all used mddevs in the system.
224 * We take care to grab the all_mddevs_lock whenever navigating
225 * the list, and to always hold a refcount when unlocked.
226 * Any code which breaks out of this loop while own
227 * a reference to the current mddev and must mddev_put it.
229 #define for_each_mddev(_mddev,_tmp) \
231 for (({ spin_lock(&all_mddevs_lock); \
232 _tmp = all_mddevs.next; \
234 ({ if (_tmp != &all_mddevs) \
235 mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\
236 spin_unlock(&all_mddevs_lock); \
237 if (_mddev) mddev_put(_mddev); \
238 _mddev = list_entry(_tmp, struct mddev, all_mddevs); \
239 _tmp != &all_mddevs;}); \
240 ({ spin_lock(&all_mddevs_lock); \
241 _tmp = _tmp->next;}) \
245 /* Rather than calling directly into the personality make_request function,
246 * IO requests come here first so that we can check if the device is
247 * being suspended pending a reconfiguration.
248 * We hold a refcount over the call to ->make_request. By the time that
249 * call has finished, the bio has been linked into some internal structure
250 * and so is visible to ->quiesce(), so we don't need the refcount any more.
252 static void md_make_request(struct request_queue *q, struct bio *bio)
254 const int rw = bio_data_dir(bio);
255 struct mddev *mddev = q->queuedata;
257 unsigned int sectors;
259 if (mddev == NULL || mddev->pers == NULL
264 if (mddev->ro == 1 && unlikely(rw == WRITE)) {
265 bio_endio(bio, bio_sectors(bio) == 0 ? 0 : -EROFS);
268 smp_rmb(); /* Ensure implications of 'active' are visible */
270 if (mddev->suspended) {
273 prepare_to_wait(&mddev->sb_wait, &__wait,
274 TASK_UNINTERRUPTIBLE);
275 if (!mddev->suspended)
281 finish_wait(&mddev->sb_wait, &__wait);
283 atomic_inc(&mddev->active_io);
287 * save the sectors now since our bio can
288 * go away inside make_request
290 sectors = bio_sectors(bio);
291 mddev->pers->make_request(mddev, bio);
293 cpu = part_stat_lock();
294 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
295 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw], sectors);
298 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
299 wake_up(&mddev->sb_wait);
302 /* mddev_suspend makes sure no new requests are submitted
303 * to the device, and that any requests that have been submitted
304 * are completely handled.
305 * Once ->stop is called and completes, the module will be completely
308 void mddev_suspend(struct mddev *mddev)
310 BUG_ON(mddev->suspended);
311 mddev->suspended = 1;
313 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
314 mddev->pers->quiesce(mddev, 1);
316 del_timer_sync(&mddev->safemode_timer);
318 EXPORT_SYMBOL_GPL(mddev_suspend);
320 void mddev_resume(struct mddev *mddev)
322 mddev->suspended = 0;
323 wake_up(&mddev->sb_wait);
324 mddev->pers->quiesce(mddev, 0);
326 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
327 md_wakeup_thread(mddev->thread);
328 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
330 EXPORT_SYMBOL_GPL(mddev_resume);
332 int mddev_congested(struct mddev *mddev, int bits)
334 return mddev->suspended;
336 EXPORT_SYMBOL(mddev_congested);
339 * Generic flush handling for md
342 static void md_end_flush(struct bio *bio, int err)
344 struct md_rdev *rdev = bio->bi_private;
345 struct mddev *mddev = rdev->mddev;
347 rdev_dec_pending(rdev, mddev);
349 if (atomic_dec_and_test(&mddev->flush_pending)) {
350 /* The pre-request flush has finished */
351 queue_work(md_wq, &mddev->flush_work);
356 static void md_submit_flush_data(struct work_struct *ws);
358 static void submit_flushes(struct work_struct *ws)
360 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
361 struct md_rdev *rdev;
363 INIT_WORK(&mddev->flush_work, md_submit_flush_data);
364 atomic_set(&mddev->flush_pending, 1);
366 rdev_for_each_rcu(rdev, mddev)
367 if (rdev->raid_disk >= 0 &&
368 !test_bit(Faulty, &rdev->flags)) {
369 /* Take two references, one is dropped
370 * when request finishes, one after
371 * we reclaim rcu_read_lock
374 atomic_inc(&rdev->nr_pending);
375 atomic_inc(&rdev->nr_pending);
377 bi = bio_alloc_mddev(GFP_NOIO, 0, mddev);
378 bi->bi_end_io = md_end_flush;
379 bi->bi_private = rdev;
380 bi->bi_bdev = rdev->bdev;
381 atomic_inc(&mddev->flush_pending);
382 submit_bio(WRITE_FLUSH, bi);
384 rdev_dec_pending(rdev, mddev);
387 if (atomic_dec_and_test(&mddev->flush_pending))
388 queue_work(md_wq, &mddev->flush_work);
391 static void md_submit_flush_data(struct work_struct *ws)
393 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
394 struct bio *bio = mddev->flush_bio;
396 if (bio->bi_size == 0)
397 /* an empty barrier - all done */
400 bio->bi_rw &= ~REQ_FLUSH;
401 mddev->pers->make_request(mddev, bio);
404 mddev->flush_bio = NULL;
405 wake_up(&mddev->sb_wait);
408 void md_flush_request(struct mddev *mddev, struct bio *bio)
410 spin_lock_irq(&mddev->write_lock);
411 wait_event_lock_irq(mddev->sb_wait,
414 mddev->flush_bio = bio;
415 spin_unlock_irq(&mddev->write_lock);
417 INIT_WORK(&mddev->flush_work, submit_flushes);
418 queue_work(md_wq, &mddev->flush_work);
420 EXPORT_SYMBOL(md_flush_request);
422 void md_unplug(struct blk_plug_cb *cb, bool from_schedule)
424 struct mddev *mddev = cb->data;
425 md_wakeup_thread(mddev->thread);
428 EXPORT_SYMBOL(md_unplug);
430 static inline struct mddev *mddev_get(struct mddev *mddev)
432 atomic_inc(&mddev->active);
436 static void mddev_delayed_delete(struct work_struct *ws);
438 static void mddev_put(struct mddev *mddev)
440 struct bio_set *bs = NULL;
442 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
444 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
445 mddev->ctime == 0 && !mddev->hold_active) {
446 /* Array is not configured at all, and not held active,
448 list_del_init(&mddev->all_mddevs);
450 mddev->bio_set = NULL;
451 if (mddev->gendisk) {
452 /* We did a probe so need to clean up. Call
453 * queue_work inside the spinlock so that
454 * flush_workqueue() after mddev_find will
455 * succeed in waiting for the work to be done.
457 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
458 queue_work(md_misc_wq, &mddev->del_work);
462 spin_unlock(&all_mddevs_lock);
467 void mddev_init(struct mddev *mddev)
469 mutex_init(&mddev->open_mutex);
470 mutex_init(&mddev->reconfig_mutex);
471 mutex_init(&mddev->bitmap_info.mutex);
472 INIT_LIST_HEAD(&mddev->disks);
473 INIT_LIST_HEAD(&mddev->all_mddevs);
474 init_timer(&mddev->safemode_timer);
475 atomic_set(&mddev->active, 1);
476 atomic_set(&mddev->openers, 0);
477 atomic_set(&mddev->active_io, 0);
478 spin_lock_init(&mddev->write_lock);
479 atomic_set(&mddev->flush_pending, 0);
480 init_waitqueue_head(&mddev->sb_wait);
481 init_waitqueue_head(&mddev->recovery_wait);
482 mddev->reshape_position = MaxSector;
483 mddev->reshape_backwards = 0;
484 mddev->last_sync_action = "none";
485 mddev->resync_min = 0;
486 mddev->resync_max = MaxSector;
487 mddev->level = LEVEL_NONE;
489 EXPORT_SYMBOL_GPL(mddev_init);
491 static struct mddev * mddev_find(dev_t unit)
493 struct mddev *mddev, *new = NULL;
495 if (unit && MAJOR(unit) != MD_MAJOR)
496 unit &= ~((1<<MdpMinorShift)-1);
499 spin_lock(&all_mddevs_lock);
502 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
503 if (mddev->unit == unit) {
505 spin_unlock(&all_mddevs_lock);
511 list_add(&new->all_mddevs, &all_mddevs);
512 spin_unlock(&all_mddevs_lock);
513 new->hold_active = UNTIL_IOCTL;
517 /* find an unused unit number */
518 static int next_minor = 512;
519 int start = next_minor;
523 dev = MKDEV(MD_MAJOR, next_minor);
525 if (next_minor > MINORMASK)
527 if (next_minor == start) {
528 /* Oh dear, all in use. */
529 spin_unlock(&all_mddevs_lock);
535 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
536 if (mddev->unit == dev) {
542 new->md_minor = MINOR(dev);
543 new->hold_active = UNTIL_STOP;
544 list_add(&new->all_mddevs, &all_mddevs);
545 spin_unlock(&all_mddevs_lock);
548 spin_unlock(&all_mddevs_lock);
550 new = kzalloc(sizeof(*new), GFP_KERNEL);
555 if (MAJOR(unit) == MD_MAJOR)
556 new->md_minor = MINOR(unit);
558 new->md_minor = MINOR(unit) >> MdpMinorShift;
565 static inline int __must_check mddev_lock(struct mddev * mddev)
567 return mutex_lock_interruptible(&mddev->reconfig_mutex);
570 /* Sometimes we need to take the lock in a situation where
571 * failure due to interrupts is not acceptable.
573 static inline void mddev_lock_nointr(struct mddev * mddev)
575 mutex_lock(&mddev->reconfig_mutex);
578 static inline int mddev_is_locked(struct mddev *mddev)
580 return mutex_is_locked(&mddev->reconfig_mutex);
583 static inline int mddev_trylock(struct mddev * mddev)
585 return mutex_trylock(&mddev->reconfig_mutex);
588 static struct attribute_group md_redundancy_group;
590 static void mddev_unlock(struct mddev * mddev)
592 if (mddev->to_remove) {
593 /* These cannot be removed under reconfig_mutex as
594 * an access to the files will try to take reconfig_mutex
595 * while holding the file unremovable, which leads to
597 * So hold set sysfs_active while the remove in happeing,
598 * and anything else which might set ->to_remove or my
599 * otherwise change the sysfs namespace will fail with
600 * -EBUSY if sysfs_active is still set.
601 * We set sysfs_active under reconfig_mutex and elsewhere
602 * test it under the same mutex to ensure its correct value
605 struct attribute_group *to_remove = mddev->to_remove;
606 mddev->to_remove = NULL;
607 mddev->sysfs_active = 1;
608 mutex_unlock(&mddev->reconfig_mutex);
610 if (mddev->kobj.sd) {
611 if (to_remove != &md_redundancy_group)
612 sysfs_remove_group(&mddev->kobj, to_remove);
613 if (mddev->pers == NULL ||
614 mddev->pers->sync_request == NULL) {
615 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
616 if (mddev->sysfs_action)
617 sysfs_put(mddev->sysfs_action);
618 mddev->sysfs_action = NULL;
621 mddev->sysfs_active = 0;
623 mutex_unlock(&mddev->reconfig_mutex);
625 /* As we've dropped the mutex we need a spinlock to
626 * make sure the thread doesn't disappear
628 spin_lock(&pers_lock);
629 md_wakeup_thread(mddev->thread);
630 spin_unlock(&pers_lock);
633 static struct md_rdev * find_rdev_nr(struct mddev *mddev, int nr)
635 struct md_rdev *rdev;
637 rdev_for_each(rdev, mddev)
638 if (rdev->desc_nr == nr)
644 static struct md_rdev *find_rdev_nr_rcu(struct mddev *mddev, int nr)
646 struct md_rdev *rdev;
648 rdev_for_each_rcu(rdev, mddev)
649 if (rdev->desc_nr == nr)
655 static struct md_rdev *find_rdev(struct mddev *mddev, dev_t dev)
657 struct md_rdev *rdev;
659 rdev_for_each(rdev, mddev)
660 if (rdev->bdev->bd_dev == dev)
666 static struct md_rdev *find_rdev_rcu(struct mddev *mddev, dev_t dev)
668 struct md_rdev *rdev;
670 rdev_for_each_rcu(rdev, mddev)
671 if (rdev->bdev->bd_dev == dev)
677 static struct md_personality *find_pers(int level, char *clevel)
679 struct md_personality *pers;
680 list_for_each_entry(pers, &pers_list, list) {
681 if (level != LEVEL_NONE && pers->level == level)
683 if (strcmp(pers->name, clevel)==0)
689 /* return the offset of the super block in 512byte sectors */
690 static inline sector_t calc_dev_sboffset(struct md_rdev *rdev)
692 sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512;
693 return MD_NEW_SIZE_SECTORS(num_sectors);
696 static int alloc_disk_sb(struct md_rdev * rdev)
701 rdev->sb_page = alloc_page(GFP_KERNEL);
702 if (!rdev->sb_page) {
703 printk(KERN_ALERT "md: out of memory.\n");
710 void md_rdev_clear(struct md_rdev *rdev)
713 put_page(rdev->sb_page);
715 rdev->sb_page = NULL;
720 put_page(rdev->bb_page);
721 rdev->bb_page = NULL;
723 kfree(rdev->badblocks.page);
724 rdev->badblocks.page = NULL;
726 EXPORT_SYMBOL_GPL(md_rdev_clear);
728 static void super_written(struct bio *bio, int error)
730 struct md_rdev *rdev = bio->bi_private;
731 struct mddev *mddev = rdev->mddev;
733 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
734 printk("md: super_written gets error=%d, uptodate=%d\n",
735 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
736 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
737 md_error(mddev, rdev);
740 if (atomic_dec_and_test(&mddev->pending_writes))
741 wake_up(&mddev->sb_wait);
745 void md_super_write(struct mddev *mddev, struct md_rdev *rdev,
746 sector_t sector, int size, struct page *page)
748 /* write first size bytes of page to sector of rdev
749 * Increment mddev->pending_writes before returning
750 * and decrement it on completion, waking up sb_wait
751 * if zero is reached.
752 * If an error occurred, call md_error
754 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev);
756 bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev;
757 bio->bi_sector = sector;
758 bio_add_page(bio, page, size, 0);
759 bio->bi_private = rdev;
760 bio->bi_end_io = super_written;
762 atomic_inc(&mddev->pending_writes);
763 submit_bio(WRITE_FLUSH_FUA, bio);
766 void md_super_wait(struct mddev *mddev)
768 /* wait for all superblock writes that were scheduled to complete */
771 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
772 if (atomic_read(&mddev->pending_writes)==0)
776 finish_wait(&mddev->sb_wait, &wq);
779 int sync_page_io(struct md_rdev *rdev, sector_t sector, int size,
780 struct page *page, int rw, bool metadata_op)
782 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev);
787 bio->bi_bdev = (metadata_op && rdev->meta_bdev) ?
788 rdev->meta_bdev : rdev->bdev;
790 bio->bi_sector = sector + rdev->sb_start;
791 else if (rdev->mddev->reshape_position != MaxSector &&
792 (rdev->mddev->reshape_backwards ==
793 (sector >= rdev->mddev->reshape_position)))
794 bio->bi_sector = sector + rdev->new_data_offset;
796 bio->bi_sector = sector + rdev->data_offset;
797 bio_add_page(bio, page, size, 0);
798 submit_bio_wait(rw, bio);
800 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
804 EXPORT_SYMBOL_GPL(sync_page_io);
806 static int read_disk_sb(struct md_rdev * rdev, int size)
808 char b[BDEVNAME_SIZE];
809 if (!rdev->sb_page) {
817 if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true))
823 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
824 bdevname(rdev->bdev,b));
828 static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
830 return sb1->set_uuid0 == sb2->set_uuid0 &&
831 sb1->set_uuid1 == sb2->set_uuid1 &&
832 sb1->set_uuid2 == sb2->set_uuid2 &&
833 sb1->set_uuid3 == sb2->set_uuid3;
836 static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
839 mdp_super_t *tmp1, *tmp2;
841 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
842 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
844 if (!tmp1 || !tmp2) {
846 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
854 * nr_disks is not constant
859 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
867 static u32 md_csum_fold(u32 csum)
869 csum = (csum & 0xffff) + (csum >> 16);
870 return (csum & 0xffff) + (csum >> 16);
873 static unsigned int calc_sb_csum(mdp_super_t * sb)
876 u32 *sb32 = (u32*)sb;
878 unsigned int disk_csum, csum;
880 disk_csum = sb->sb_csum;
883 for (i = 0; i < MD_SB_BYTES/4 ; i++)
885 csum = (newcsum & 0xffffffff) + (newcsum>>32);
889 /* This used to use csum_partial, which was wrong for several
890 * reasons including that different results are returned on
891 * different architectures. It isn't critical that we get exactly
892 * the same return value as before (we always csum_fold before
893 * testing, and that removes any differences). However as we
894 * know that csum_partial always returned a 16bit value on
895 * alphas, do a fold to maximise conformity to previous behaviour.
897 sb->sb_csum = md_csum_fold(disk_csum);
899 sb->sb_csum = disk_csum;
906 * Handle superblock details.
907 * We want to be able to handle multiple superblock formats
908 * so we have a common interface to them all, and an array of
909 * different handlers.
910 * We rely on user-space to write the initial superblock, and support
911 * reading and updating of superblocks.
912 * Interface methods are:
913 * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
914 * loads and validates a superblock on dev.
915 * if refdev != NULL, compare superblocks on both devices
917 * 0 - dev has a superblock that is compatible with refdev
918 * 1 - dev has a superblock that is compatible and newer than refdev
919 * so dev should be used as the refdev in future
920 * -EINVAL superblock incompatible or invalid
921 * -othererror e.g. -EIO
923 * int validate_super(struct mddev *mddev, struct md_rdev *dev)
924 * Verify that dev is acceptable into mddev.
925 * The first time, mddev->raid_disks will be 0, and data from
926 * dev should be merged in. Subsequent calls check that dev
927 * is new enough. Return 0 or -EINVAL
929 * void sync_super(struct mddev *mddev, struct md_rdev *dev)
930 * Update the superblock for rdev with data in mddev
931 * This does not write to disc.
937 struct module *owner;
938 int (*load_super)(struct md_rdev *rdev,
939 struct md_rdev *refdev,
941 int (*validate_super)(struct mddev *mddev,
942 struct md_rdev *rdev);
943 void (*sync_super)(struct mddev *mddev,
944 struct md_rdev *rdev);
945 unsigned long long (*rdev_size_change)(struct md_rdev *rdev,
946 sector_t num_sectors);
947 int (*allow_new_offset)(struct md_rdev *rdev,
948 unsigned long long new_offset);
952 * Check that the given mddev has no bitmap.
954 * This function is called from the run method of all personalities that do not
955 * support bitmaps. It prints an error message and returns non-zero if mddev
956 * has a bitmap. Otherwise, it returns 0.
959 int md_check_no_bitmap(struct mddev *mddev)
961 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
963 printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
964 mdname(mddev), mddev->pers->name);
967 EXPORT_SYMBOL(md_check_no_bitmap);
970 * load_super for 0.90.0
972 static int super_90_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
974 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
979 * Calculate the position of the superblock (512byte sectors),
980 * it's at the end of the disk.
982 * It also happens to be a multiple of 4Kb.
984 rdev->sb_start = calc_dev_sboffset(rdev);
986 ret = read_disk_sb(rdev, MD_SB_BYTES);
991 bdevname(rdev->bdev, b);
992 sb = page_address(rdev->sb_page);
994 if (sb->md_magic != MD_SB_MAGIC) {
995 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
1000 if (sb->major_version != 0 ||
1001 sb->minor_version < 90 ||
1002 sb->minor_version > 91) {
1003 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
1004 sb->major_version, sb->minor_version,
1009 if (sb->raid_disks <= 0)
1012 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
1013 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
1018 rdev->preferred_minor = sb->md_minor;
1019 rdev->data_offset = 0;
1020 rdev->new_data_offset = 0;
1021 rdev->sb_size = MD_SB_BYTES;
1022 rdev->badblocks.shift = -1;
1024 if (sb->level == LEVEL_MULTIPATH)
1027 rdev->desc_nr = sb->this_disk.number;
1033 mdp_super_t *refsb = page_address(refdev->sb_page);
1034 if (!uuid_equal(refsb, sb)) {
1035 printk(KERN_WARNING "md: %s has different UUID to %s\n",
1036 b, bdevname(refdev->bdev,b2));
1039 if (!sb_equal(refsb, sb)) {
1040 printk(KERN_WARNING "md: %s has same UUID"
1041 " but different superblock to %s\n",
1042 b, bdevname(refdev->bdev, b2));
1046 ev2 = md_event(refsb);
1052 rdev->sectors = rdev->sb_start;
1053 /* Limit to 4TB as metadata cannot record more than that.
1054 * (not needed for Linear and RAID0 as metadata doesn't
1057 if (rdev->sectors >= (2ULL << 32) && sb->level >= 1)
1058 rdev->sectors = (2ULL << 32) - 2;
1060 if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1)
1061 /* "this cannot possibly happen" ... */
1069 * validate_super for 0.90.0
1071 static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev)
1074 mdp_super_t *sb = page_address(rdev->sb_page);
1075 __u64 ev1 = md_event(sb);
1077 rdev->raid_disk = -1;
1078 clear_bit(Faulty, &rdev->flags);
1079 clear_bit(In_sync, &rdev->flags);
1080 clear_bit(Bitmap_sync, &rdev->flags);
1081 clear_bit(WriteMostly, &rdev->flags);
1083 if (mddev->raid_disks == 0) {
1084 mddev->major_version = 0;
1085 mddev->minor_version = sb->minor_version;
1086 mddev->patch_version = sb->patch_version;
1087 mddev->external = 0;
1088 mddev->chunk_sectors = sb->chunk_size >> 9;
1089 mddev->ctime = sb->ctime;
1090 mddev->utime = sb->utime;
1091 mddev->level = sb->level;
1092 mddev->clevel[0] = 0;
1093 mddev->layout = sb->layout;
1094 mddev->raid_disks = sb->raid_disks;
1095 mddev->dev_sectors = ((sector_t)sb->size) * 2;
1096 mddev->events = ev1;
1097 mddev->bitmap_info.offset = 0;
1098 mddev->bitmap_info.space = 0;
1099 /* bitmap can use 60 K after the 4K superblocks */
1100 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
1101 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
1102 mddev->reshape_backwards = 0;
1104 if (mddev->minor_version >= 91) {
1105 mddev->reshape_position = sb->reshape_position;
1106 mddev->delta_disks = sb->delta_disks;
1107 mddev->new_level = sb->new_level;
1108 mddev->new_layout = sb->new_layout;
1109 mddev->new_chunk_sectors = sb->new_chunk >> 9;
1110 if (mddev->delta_disks < 0)
1111 mddev->reshape_backwards = 1;
1113 mddev->reshape_position = MaxSector;
1114 mddev->delta_disks = 0;
1115 mddev->new_level = mddev->level;
1116 mddev->new_layout = mddev->layout;
1117 mddev->new_chunk_sectors = mddev->chunk_sectors;
1120 if (sb->state & (1<<MD_SB_CLEAN))
1121 mddev->recovery_cp = MaxSector;
1123 if (sb->events_hi == sb->cp_events_hi &&
1124 sb->events_lo == sb->cp_events_lo) {
1125 mddev->recovery_cp = sb->recovery_cp;
1127 mddev->recovery_cp = 0;
1130 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1131 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1132 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1133 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1135 mddev->max_disks = MD_SB_DISKS;
1137 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
1138 mddev->bitmap_info.file == NULL) {
1139 mddev->bitmap_info.offset =
1140 mddev->bitmap_info.default_offset;
1141 mddev->bitmap_info.space =
1142 mddev->bitmap_info.default_space;
1145 } else if (mddev->pers == NULL) {
1146 /* Insist on good event counter while assembling, except
1147 * for spares (which don't need an event count) */
1149 if (sb->disks[rdev->desc_nr].state & (
1150 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
1151 if (ev1 < mddev->events)
1153 } else if (mddev->bitmap) {
1154 /* if adding to array with a bitmap, then we can accept an
1155 * older device ... but not too old.
1157 if (ev1 < mddev->bitmap->events_cleared)
1159 if (ev1 < mddev->events)
1160 set_bit(Bitmap_sync, &rdev->flags);
1162 if (ev1 < mddev->events)
1163 /* just a hot-add of a new device, leave raid_disk at -1 */
1167 if (mddev->level != LEVEL_MULTIPATH) {
1168 desc = sb->disks + rdev->desc_nr;
1170 if (desc->state & (1<<MD_DISK_FAULTY))
1171 set_bit(Faulty, &rdev->flags);
1172 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1173 desc->raid_disk < mddev->raid_disks */) {
1174 set_bit(In_sync, &rdev->flags);
1175 rdev->raid_disk = desc->raid_disk;
1176 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1177 /* active but not in sync implies recovery up to
1178 * reshape position. We don't know exactly where
1179 * that is, so set to zero for now */
1180 if (mddev->minor_version >= 91) {
1181 rdev->recovery_offset = 0;
1182 rdev->raid_disk = desc->raid_disk;
1185 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1186 set_bit(WriteMostly, &rdev->flags);
1187 } else /* MULTIPATH are always insync */
1188 set_bit(In_sync, &rdev->flags);
1193 * sync_super for 0.90.0
1195 static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev)
1198 struct md_rdev *rdev2;
1199 int next_spare = mddev->raid_disks;
1202 /* make rdev->sb match mddev data..
1205 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1206 * 3/ any empty disks < next_spare become removed
1208 * disks[0] gets initialised to REMOVED because
1209 * we cannot be sure from other fields if it has
1210 * been initialised or not.
1213 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1215 rdev->sb_size = MD_SB_BYTES;
1217 sb = page_address(rdev->sb_page);
1219 memset(sb, 0, sizeof(*sb));
1221 sb->md_magic = MD_SB_MAGIC;
1222 sb->major_version = mddev->major_version;
1223 sb->patch_version = mddev->patch_version;
1224 sb->gvalid_words = 0; /* ignored */
1225 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1226 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1227 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1228 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1230 sb->ctime = mddev->ctime;
1231 sb->level = mddev->level;
1232 sb->size = mddev->dev_sectors / 2;
1233 sb->raid_disks = mddev->raid_disks;
1234 sb->md_minor = mddev->md_minor;
1235 sb->not_persistent = 0;
1236 sb->utime = mddev->utime;
1238 sb->events_hi = (mddev->events>>32);
1239 sb->events_lo = (u32)mddev->events;
1241 if (mddev->reshape_position == MaxSector)
1242 sb->minor_version = 90;
1244 sb->minor_version = 91;
1245 sb->reshape_position = mddev->reshape_position;
1246 sb->new_level = mddev->new_level;
1247 sb->delta_disks = mddev->delta_disks;
1248 sb->new_layout = mddev->new_layout;
1249 sb->new_chunk = mddev->new_chunk_sectors << 9;
1251 mddev->minor_version = sb->minor_version;
1254 sb->recovery_cp = mddev->recovery_cp;
1255 sb->cp_events_hi = (mddev->events>>32);
1256 sb->cp_events_lo = (u32)mddev->events;
1257 if (mddev->recovery_cp == MaxSector)
1258 sb->state = (1<< MD_SB_CLEAN);
1260 sb->recovery_cp = 0;
1262 sb->layout = mddev->layout;
1263 sb->chunk_size = mddev->chunk_sectors << 9;
1265 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
1266 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1268 sb->disks[0].state = (1<<MD_DISK_REMOVED);
1269 rdev_for_each(rdev2, mddev) {
1272 int is_active = test_bit(In_sync, &rdev2->flags);
1274 if (rdev2->raid_disk >= 0 &&
1275 sb->minor_version >= 91)
1276 /* we have nowhere to store the recovery_offset,
1277 * but if it is not below the reshape_position,
1278 * we can piggy-back on that.
1281 if (rdev2->raid_disk < 0 ||
1282 test_bit(Faulty, &rdev2->flags))
1285 desc_nr = rdev2->raid_disk;
1287 desc_nr = next_spare++;
1288 rdev2->desc_nr = desc_nr;
1289 d = &sb->disks[rdev2->desc_nr];
1291 d->number = rdev2->desc_nr;
1292 d->major = MAJOR(rdev2->bdev->bd_dev);
1293 d->minor = MINOR(rdev2->bdev->bd_dev);
1295 d->raid_disk = rdev2->raid_disk;
1297 d->raid_disk = rdev2->desc_nr; /* compatibility */
1298 if (test_bit(Faulty, &rdev2->flags))
1299 d->state = (1<<MD_DISK_FAULTY);
1300 else if (is_active) {
1301 d->state = (1<<MD_DISK_ACTIVE);
1302 if (test_bit(In_sync, &rdev2->flags))
1303 d->state |= (1<<MD_DISK_SYNC);
1311 if (test_bit(WriteMostly, &rdev2->flags))
1312 d->state |= (1<<MD_DISK_WRITEMOSTLY);
1314 /* now set the "removed" and "faulty" bits on any missing devices */
1315 for (i=0 ; i < mddev->raid_disks ; i++) {
1316 mdp_disk_t *d = &sb->disks[i];
1317 if (d->state == 0 && d->number == 0) {
1320 d->state = (1<<MD_DISK_REMOVED);
1321 d->state |= (1<<MD_DISK_FAULTY);
1325 sb->nr_disks = nr_disks;
1326 sb->active_disks = active;
1327 sb->working_disks = working;
1328 sb->failed_disks = failed;
1329 sb->spare_disks = spare;
1331 sb->this_disk = sb->disks[rdev->desc_nr];
1332 sb->sb_csum = calc_sb_csum(sb);
1336 * rdev_size_change for 0.90.0
1338 static unsigned long long
1339 super_90_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
1341 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
1342 return 0; /* component must fit device */
1343 if (rdev->mddev->bitmap_info.offset)
1344 return 0; /* can't move bitmap */
1345 rdev->sb_start = calc_dev_sboffset(rdev);
1346 if (!num_sectors || num_sectors > rdev->sb_start)
1347 num_sectors = rdev->sb_start;
1348 /* Limit to 4TB as metadata cannot record more than that.
1349 * 4TB == 2^32 KB, or 2*2^32 sectors.
1351 if (num_sectors >= (2ULL << 32) && rdev->mddev->level >= 1)
1352 num_sectors = (2ULL << 32) - 2;
1353 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
1355 md_super_wait(rdev->mddev);
1360 super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset)
1362 /* non-zero offset changes not possible with v0.90 */
1363 return new_offset == 0;
1367 * version 1 superblock
1370 static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
1374 unsigned long long newcsum;
1375 int size = 256 + le32_to_cpu(sb->max_dev)*2;
1376 __le32 *isuper = (__le32*)sb;
1378 disk_csum = sb->sb_csum;
1381 for (; size >= 4; size -= 4)
1382 newcsum += le32_to_cpu(*isuper++);
1385 newcsum += le16_to_cpu(*(__le16*) isuper);
1387 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1388 sb->sb_csum = disk_csum;
1389 return cpu_to_le32(csum);
1392 static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
1394 static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
1396 struct mdp_superblock_1 *sb;
1400 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1404 * Calculate the position of the superblock in 512byte sectors.
1405 * It is always aligned to a 4K boundary and
1406 * depeding on minor_version, it can be:
1407 * 0: At least 8K, but less than 12K, from end of device
1408 * 1: At start of device
1409 * 2: 4K from start of device.
1411 switch(minor_version) {
1413 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
1415 sb_start &= ~(sector_t)(4*2-1);
1426 rdev->sb_start = sb_start;
1428 /* superblock is rarely larger than 1K, but it can be larger,
1429 * and it is safe to read 4k, so we do that
1431 ret = read_disk_sb(rdev, 4096);
1432 if (ret) return ret;
1435 sb = page_address(rdev->sb_page);
1437 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1438 sb->major_version != cpu_to_le32(1) ||
1439 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
1440 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
1441 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
1444 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1445 printk("md: invalid superblock checksum on %s\n",
1446 bdevname(rdev->bdev,b));
1449 if (le64_to_cpu(sb->data_size) < 10) {
1450 printk("md: data_size too small on %s\n",
1451 bdevname(rdev->bdev,b));
1456 memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1])))
1457 /* Some padding is non-zero, might be a new feature */
1460 rdev->preferred_minor = 0xffff;
1461 rdev->data_offset = le64_to_cpu(sb->data_offset);
1462 rdev->new_data_offset = rdev->data_offset;
1463 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) &&
1464 (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET))
1465 rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset);
1466 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
1468 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
1469 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1470 if (rdev->sb_size & bmask)
1471 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1474 && rdev->data_offset < sb_start + (rdev->sb_size/512))
1477 && rdev->new_data_offset < sb_start + (rdev->sb_size/512))
1480 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1483 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1485 if (!rdev->bb_page) {
1486 rdev->bb_page = alloc_page(GFP_KERNEL);
1490 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) &&
1491 rdev->badblocks.count == 0) {
1492 /* need to load the bad block list.
1493 * Currently we limit it to one page.
1499 int sectors = le16_to_cpu(sb->bblog_size);
1500 if (sectors > (PAGE_SIZE / 512))
1502 offset = le32_to_cpu(sb->bblog_offset);
1505 bb_sector = (long long)offset;
1506 if (!sync_page_io(rdev, bb_sector, sectors << 9,
1507 rdev->bb_page, READ, true))
1509 bbp = (u64 *)page_address(rdev->bb_page);
1510 rdev->badblocks.shift = sb->bblog_shift;
1511 for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) {
1512 u64 bb = le64_to_cpu(*bbp);
1513 int count = bb & (0x3ff);
1514 u64 sector = bb >> 10;
1515 sector <<= sb->bblog_shift;
1516 count <<= sb->bblog_shift;
1519 if (md_set_badblocks(&rdev->badblocks,
1520 sector, count, 1) == 0)
1523 } else if (sb->bblog_offset != 0)
1524 rdev->badblocks.shift = 0;
1530 struct mdp_superblock_1 *refsb = page_address(refdev->sb_page);
1532 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1533 sb->level != refsb->level ||
1534 sb->layout != refsb->layout ||
1535 sb->chunksize != refsb->chunksize) {
1536 printk(KERN_WARNING "md: %s has strangely different"
1537 " superblock to %s\n",
1538 bdevname(rdev->bdev,b),
1539 bdevname(refdev->bdev,b2));
1542 ev1 = le64_to_cpu(sb->events);
1543 ev2 = le64_to_cpu(refsb->events);
1550 if (minor_version) {
1551 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9);
1552 sectors -= rdev->data_offset;
1554 sectors = rdev->sb_start;
1555 if (sectors < le64_to_cpu(sb->data_size))
1557 rdev->sectors = le64_to_cpu(sb->data_size);
1561 static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev)
1563 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
1564 __u64 ev1 = le64_to_cpu(sb->events);
1566 rdev->raid_disk = -1;
1567 clear_bit(Faulty, &rdev->flags);
1568 clear_bit(In_sync, &rdev->flags);
1569 clear_bit(Bitmap_sync, &rdev->flags);
1570 clear_bit(WriteMostly, &rdev->flags);
1572 if (mddev->raid_disks == 0) {
1573 mddev->major_version = 1;
1574 mddev->patch_version = 0;
1575 mddev->external = 0;
1576 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
1577 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1578 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1579 mddev->level = le32_to_cpu(sb->level);
1580 mddev->clevel[0] = 0;
1581 mddev->layout = le32_to_cpu(sb->layout);
1582 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
1583 mddev->dev_sectors = le64_to_cpu(sb->size);
1584 mddev->events = ev1;
1585 mddev->bitmap_info.offset = 0;
1586 mddev->bitmap_info.space = 0;
1587 /* Default location for bitmap is 1K after superblock
1588 * using 3K - total of 4K
1590 mddev->bitmap_info.default_offset = 1024 >> 9;
1591 mddev->bitmap_info.default_space = (4096-1024) >> 9;
1592 mddev->reshape_backwards = 0;
1594 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1595 memcpy(mddev->uuid, sb->set_uuid, 16);
1597 mddev->max_disks = (4096-256)/2;
1599 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
1600 mddev->bitmap_info.file == NULL) {
1601 mddev->bitmap_info.offset =
1602 (__s32)le32_to_cpu(sb->bitmap_offset);
1603 /* Metadata doesn't record how much space is available.
1604 * For 1.0, we assume we can use up to the superblock
1605 * if before, else to 4K beyond superblock.
1606 * For others, assume no change is possible.
1608 if (mddev->minor_version > 0)
1609 mddev->bitmap_info.space = 0;
1610 else if (mddev->bitmap_info.offset > 0)
1611 mddev->bitmap_info.space =
1612 8 - mddev->bitmap_info.offset;
1614 mddev->bitmap_info.space =
1615 -mddev->bitmap_info.offset;
1618 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1619 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1620 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1621 mddev->new_level = le32_to_cpu(sb->new_level);
1622 mddev->new_layout = le32_to_cpu(sb->new_layout);
1623 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
1624 if (mddev->delta_disks < 0 ||
1625 (mddev->delta_disks == 0 &&
1626 (le32_to_cpu(sb->feature_map)
1627 & MD_FEATURE_RESHAPE_BACKWARDS)))
1628 mddev->reshape_backwards = 1;
1630 mddev->reshape_position = MaxSector;
1631 mddev->delta_disks = 0;
1632 mddev->new_level = mddev->level;
1633 mddev->new_layout = mddev->layout;
1634 mddev->new_chunk_sectors = mddev->chunk_sectors;
1637 } else if (mddev->pers == NULL) {
1638 /* Insist of good event counter while assembling, except for
1639 * spares (which don't need an event count) */
1641 if (rdev->desc_nr >= 0 &&
1642 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
1643 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe)
1644 if (ev1 < mddev->events)
1646 } else if (mddev->bitmap) {
1647 /* If adding to array with a bitmap, then we can accept an
1648 * older device, but not too old.
1650 if (ev1 < mddev->bitmap->events_cleared)
1652 if (ev1 < mddev->events)
1653 set_bit(Bitmap_sync, &rdev->flags);
1655 if (ev1 < mddev->events)
1656 /* just a hot-add of a new device, leave raid_disk at -1 */
1659 if (mddev->level != LEVEL_MULTIPATH) {
1661 if (rdev->desc_nr < 0 ||
1662 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1666 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
1668 case 0xffff: /* spare */
1670 case 0xfffe: /* faulty */
1671 set_bit(Faulty, &rdev->flags);
1674 if ((le32_to_cpu(sb->feature_map) &
1675 MD_FEATURE_RECOVERY_OFFSET))
1676 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1678 set_bit(In_sync, &rdev->flags);
1679 rdev->raid_disk = role;
1682 if (sb->devflags & WriteMostly1)
1683 set_bit(WriteMostly, &rdev->flags);
1684 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT)
1685 set_bit(Replacement, &rdev->flags);
1686 } else /* MULTIPATH are always insync */
1687 set_bit(In_sync, &rdev->flags);
1692 static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev)
1694 struct mdp_superblock_1 *sb;
1695 struct md_rdev *rdev2;
1697 /* make rdev->sb match mddev and rdev data. */
1699 sb = page_address(rdev->sb_page);
1701 sb->feature_map = 0;
1703 sb->recovery_offset = cpu_to_le64(0);
1704 memset(sb->pad3, 0, sizeof(sb->pad3));
1706 sb->utime = cpu_to_le64((__u64)mddev->utime);
1707 sb->events = cpu_to_le64(mddev->events);
1709 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1711 sb->resync_offset = cpu_to_le64(0);
1713 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
1715 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
1716 sb->size = cpu_to_le64(mddev->dev_sectors);
1717 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
1718 sb->level = cpu_to_le32(mddev->level);
1719 sb->layout = cpu_to_le32(mddev->layout);
1721 if (test_bit(WriteMostly, &rdev->flags))
1722 sb->devflags |= WriteMostly1;
1724 sb->devflags &= ~WriteMostly1;
1725 sb->data_offset = cpu_to_le64(rdev->data_offset);
1726 sb->data_size = cpu_to_le64(rdev->sectors);
1728 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1729 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
1730 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
1733 if (rdev->raid_disk >= 0 &&
1734 !test_bit(In_sync, &rdev->flags)) {
1736 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1737 sb->recovery_offset =
1738 cpu_to_le64(rdev->recovery_offset);
1740 if (test_bit(Replacement, &rdev->flags))
1742 cpu_to_le32(MD_FEATURE_REPLACEMENT);
1744 if (mddev->reshape_position != MaxSector) {
1745 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1746 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1747 sb->new_layout = cpu_to_le32(mddev->new_layout);
1748 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1749 sb->new_level = cpu_to_le32(mddev->new_level);
1750 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
1751 if (mddev->delta_disks == 0 &&
1752 mddev->reshape_backwards)
1754 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS);
1755 if (rdev->new_data_offset != rdev->data_offset) {
1757 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET);
1758 sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset
1759 - rdev->data_offset));
1763 if (rdev->badblocks.count == 0)
1764 /* Nothing to do for bad blocks*/ ;
1765 else if (sb->bblog_offset == 0)
1766 /* Cannot record bad blocks on this device */
1767 md_error(mddev, rdev);
1769 struct badblocks *bb = &rdev->badblocks;
1770 u64 *bbp = (u64 *)page_address(rdev->bb_page);
1772 sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
1777 seq = read_seqbegin(&bb->lock);
1779 memset(bbp, 0xff, PAGE_SIZE);
1781 for (i = 0 ; i < bb->count ; i++) {
1782 u64 internal_bb = p[i];
1783 u64 store_bb = ((BB_OFFSET(internal_bb) << 10)
1784 | BB_LEN(internal_bb));
1785 bbp[i] = cpu_to_le64(store_bb);
1788 if (read_seqretry(&bb->lock, seq))
1791 bb->sector = (rdev->sb_start +
1792 (int)le32_to_cpu(sb->bblog_offset));
1793 bb->size = le16_to_cpu(sb->bblog_size);
1798 rdev_for_each(rdev2, mddev)
1799 if (rdev2->desc_nr+1 > max_dev)
1800 max_dev = rdev2->desc_nr+1;
1802 if (max_dev > le32_to_cpu(sb->max_dev)) {
1804 sb->max_dev = cpu_to_le32(max_dev);
1805 rdev->sb_size = max_dev * 2 + 256;
1806 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1807 if (rdev->sb_size & bmask)
1808 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1810 max_dev = le32_to_cpu(sb->max_dev);
1812 for (i=0; i<max_dev;i++)
1813 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1815 rdev_for_each(rdev2, mddev) {
1817 if (test_bit(Faulty, &rdev2->flags))
1818 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1819 else if (test_bit(In_sync, &rdev2->flags))
1820 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
1821 else if (rdev2->raid_disk >= 0)
1822 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
1824 sb->dev_roles[i] = cpu_to_le16(0xffff);
1827 sb->sb_csum = calc_sb_1_csum(sb);
1830 static unsigned long long
1831 super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
1833 struct mdp_superblock_1 *sb;
1834 sector_t max_sectors;
1835 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
1836 return 0; /* component must fit device */
1837 if (rdev->data_offset != rdev->new_data_offset)
1838 return 0; /* too confusing */
1839 if (rdev->sb_start < rdev->data_offset) {
1840 /* minor versions 1 and 2; superblock before data */
1841 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
1842 max_sectors -= rdev->data_offset;
1843 if (!num_sectors || num_sectors > max_sectors)
1844 num_sectors = max_sectors;
1845 } else if (rdev->mddev->bitmap_info.offset) {
1846 /* minor version 0 with bitmap we can't move */
1849 /* minor version 0; superblock after data */
1851 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
1852 sb_start &= ~(sector_t)(4*2 - 1);
1853 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
1854 if (!num_sectors || num_sectors > max_sectors)
1855 num_sectors = max_sectors;
1856 rdev->sb_start = sb_start;
1858 sb = page_address(rdev->sb_page);
1859 sb->data_size = cpu_to_le64(num_sectors);
1860 sb->super_offset = rdev->sb_start;
1861 sb->sb_csum = calc_sb_1_csum(sb);
1862 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
1864 md_super_wait(rdev->mddev);
1870 super_1_allow_new_offset(struct md_rdev *rdev,
1871 unsigned long long new_offset)
1873 /* All necessary checks on new >= old have been done */
1874 struct bitmap *bitmap;
1875 if (new_offset >= rdev->data_offset)
1878 /* with 1.0 metadata, there is no metadata to tread on
1879 * so we can always move back */
1880 if (rdev->mddev->minor_version == 0)
1883 /* otherwise we must be sure not to step on
1884 * any metadata, so stay:
1885 * 36K beyond start of superblock
1886 * beyond end of badblocks
1887 * beyond write-intent bitmap
1889 if (rdev->sb_start + (32+4)*2 > new_offset)
1891 bitmap = rdev->mddev->bitmap;
1892 if (bitmap && !rdev->mddev->bitmap_info.file &&
1893 rdev->sb_start + rdev->mddev->bitmap_info.offset +
1894 bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset)
1896 if (rdev->badblocks.sector + rdev->badblocks.size > new_offset)
1902 static struct super_type super_types[] = {
1905 .owner = THIS_MODULE,
1906 .load_super = super_90_load,
1907 .validate_super = super_90_validate,
1908 .sync_super = super_90_sync,
1909 .rdev_size_change = super_90_rdev_size_change,
1910 .allow_new_offset = super_90_allow_new_offset,
1914 .owner = THIS_MODULE,
1915 .load_super = super_1_load,
1916 .validate_super = super_1_validate,
1917 .sync_super = super_1_sync,
1918 .rdev_size_change = super_1_rdev_size_change,
1919 .allow_new_offset = super_1_allow_new_offset,
1923 static void sync_super(struct mddev *mddev, struct md_rdev *rdev)
1925 if (mddev->sync_super) {
1926 mddev->sync_super(mddev, rdev);
1930 BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
1932 super_types[mddev->major_version].sync_super(mddev, rdev);
1935 static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2)
1937 struct md_rdev *rdev, *rdev2;
1940 rdev_for_each_rcu(rdev, mddev1)
1941 rdev_for_each_rcu(rdev2, mddev2)
1942 if (rdev->bdev->bd_contains ==
1943 rdev2->bdev->bd_contains) {
1951 static LIST_HEAD(pending_raid_disks);
1954 * Try to register data integrity profile for an mddev
1956 * This is called when an array is started and after a disk has been kicked
1957 * from the array. It only succeeds if all working and active component devices
1958 * are integrity capable with matching profiles.
1960 int md_integrity_register(struct mddev *mddev)
1962 struct md_rdev *rdev, *reference = NULL;
1964 if (list_empty(&mddev->disks))
1965 return 0; /* nothing to do */
1966 if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
1967 return 0; /* shouldn't register, or already is */
1968 rdev_for_each(rdev, mddev) {
1969 /* skip spares and non-functional disks */
1970 if (test_bit(Faulty, &rdev->flags))
1972 if (rdev->raid_disk < 0)
1975 /* Use the first rdev as the reference */
1979 /* does this rdev's profile match the reference profile? */
1980 if (blk_integrity_compare(reference->bdev->bd_disk,
1981 rdev->bdev->bd_disk) < 0)
1984 if (!reference || !bdev_get_integrity(reference->bdev))
1987 * All component devices are integrity capable and have matching
1988 * profiles, register the common profile for the md device.
1990 if (blk_integrity_register(mddev->gendisk,
1991 bdev_get_integrity(reference->bdev)) != 0) {
1992 printk(KERN_ERR "md: failed to register integrity for %s\n",
1996 printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev));
1997 if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) {
1998 printk(KERN_ERR "md: failed to create integrity pool for %s\n",
2004 EXPORT_SYMBOL(md_integrity_register);
2006 /* Disable data integrity if non-capable/non-matching disk is being added */
2007 void md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev)
2009 struct blk_integrity *bi_rdev;
2010 struct blk_integrity *bi_mddev;
2012 if (!mddev->gendisk)
2015 bi_rdev = bdev_get_integrity(rdev->bdev);
2016 bi_mddev = blk_get_integrity(mddev->gendisk);
2018 if (!bi_mddev) /* nothing to do */
2020 if (rdev->raid_disk < 0) /* skip spares */
2022 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
2023 rdev->bdev->bd_disk) >= 0)
2025 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
2026 blk_integrity_unregister(mddev->gendisk);
2028 EXPORT_SYMBOL(md_integrity_add_rdev);
2030 static int bind_rdev_to_array(struct md_rdev * rdev, struct mddev * mddev)
2032 char b[BDEVNAME_SIZE];
2042 /* prevent duplicates */
2043 if (find_rdev(mddev, rdev->bdev->bd_dev))
2046 /* make sure rdev->sectors exceeds mddev->dev_sectors */
2047 if (rdev->sectors && (mddev->dev_sectors == 0 ||
2048 rdev->sectors < mddev->dev_sectors)) {
2050 /* Cannot change size, so fail
2051 * If mddev->level <= 0, then we don't care
2052 * about aligning sizes (e.g. linear)
2054 if (mddev->level > 0)
2057 mddev->dev_sectors = rdev->sectors;
2060 /* Verify rdev->desc_nr is unique.
2061 * If it is -1, assign a free number, else
2062 * check number is not in use
2064 if (rdev->desc_nr < 0) {
2066 if (mddev->pers) choice = mddev->raid_disks;
2067 while (find_rdev_nr(mddev, choice))
2069 rdev->desc_nr = choice;
2071 if (find_rdev_nr(mddev, rdev->desc_nr))
2074 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
2075 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
2076 mdname(mddev), mddev->max_disks);
2079 bdevname(rdev->bdev,b);
2080 while ( (s=strchr(b, '/')) != NULL)
2083 rdev->mddev = mddev;
2084 printk(KERN_INFO "md: bind<%s>\n", b);
2086 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
2089 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
2090 if (sysfs_create_link(&rdev->kobj, ko, "block"))
2091 /* failure here is OK */;
2092 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
2094 list_add_rcu(&rdev->same_set, &mddev->disks);
2095 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
2097 /* May as well allow recovery to be retried once */
2098 mddev->recovery_disabled++;
2103 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
2108 static void md_delayed_delete(struct work_struct *ws)
2110 struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work);
2111 kobject_del(&rdev->kobj);
2112 kobject_put(&rdev->kobj);
2115 static void unbind_rdev_from_array(struct md_rdev * rdev)
2117 char b[BDEVNAME_SIZE];
2122 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
2123 list_del_rcu(&rdev->same_set);
2124 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
2126 sysfs_remove_link(&rdev->kobj, "block");
2127 sysfs_put(rdev->sysfs_state);
2128 rdev->sysfs_state = NULL;
2129 rdev->badblocks.count = 0;
2130 /* We need to delay this, otherwise we can deadlock when
2131 * writing to 'remove' to "dev/state". We also need
2132 * to delay it due to rcu usage.
2135 INIT_WORK(&rdev->del_work, md_delayed_delete);
2136 kobject_get(&rdev->kobj);
2137 queue_work(md_misc_wq, &rdev->del_work);
2141 * prevent the device from being mounted, repartitioned or
2142 * otherwise reused by a RAID array (or any other kernel
2143 * subsystem), by bd_claiming the device.
2145 static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared)
2148 struct block_device *bdev;
2149 char b[BDEVNAME_SIZE];
2151 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
2152 shared ? (struct md_rdev *)lock_rdev : rdev);
2154 printk(KERN_ERR "md: could not open %s.\n",
2155 __bdevname(dev, b));
2156 return PTR_ERR(bdev);
2162 static void unlock_rdev(struct md_rdev *rdev)
2164 struct block_device *bdev = rdev->bdev;
2168 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
2171 void md_autodetect_dev(dev_t dev);
2173 static void export_rdev(struct md_rdev * rdev)
2175 char b[BDEVNAME_SIZE];
2176 printk(KERN_INFO "md: export_rdev(%s)\n",
2177 bdevname(rdev->bdev,b));
2180 md_rdev_clear(rdev);
2182 if (test_bit(AutoDetected, &rdev->flags))
2183 md_autodetect_dev(rdev->bdev->bd_dev);
2186 kobject_put(&rdev->kobj);
2189 static void kick_rdev_from_array(struct md_rdev * rdev)
2191 unbind_rdev_from_array(rdev);
2195 static void export_array(struct mddev *mddev)
2197 struct md_rdev *rdev, *tmp;
2199 rdev_for_each_safe(rdev, tmp, mddev) {
2204 kick_rdev_from_array(rdev);
2206 if (!list_empty(&mddev->disks))
2208 mddev->raid_disks = 0;
2209 mddev->major_version = 0;
2212 static void print_desc(mdp_disk_t *desc)
2214 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
2215 desc->major,desc->minor,desc->raid_disk,desc->state);
2218 static void print_sb_90(mdp_super_t *sb)
2223 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
2224 sb->major_version, sb->minor_version, sb->patch_version,
2225 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
2227 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
2228 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
2229 sb->md_minor, sb->layout, sb->chunk_size);
2230 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
2231 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
2232 sb->utime, sb->state, sb->active_disks, sb->working_disks,
2233 sb->failed_disks, sb->spare_disks,
2234 sb->sb_csum, (unsigned long)sb->events_lo);
2237 for (i = 0; i < MD_SB_DISKS; i++) {
2240 desc = sb->disks + i;
2241 if (desc->number || desc->major || desc->minor ||
2242 desc->raid_disk || (desc->state && (desc->state != 4))) {
2243 printk(" D %2d: ", i);
2247 printk(KERN_INFO "md: THIS: ");
2248 print_desc(&sb->this_disk);
2251 static void print_sb_1(struct mdp_superblock_1 *sb)
2255 uuid = sb->set_uuid;
2257 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n"
2258 "md: Name: \"%s\" CT:%llu\n",
2259 le32_to_cpu(sb->major_version),
2260 le32_to_cpu(sb->feature_map),
2263 (unsigned long long)le64_to_cpu(sb->ctime)
2264 & MD_SUPERBLOCK_1_TIME_SEC_MASK);
2266 uuid = sb->device_uuid;
2268 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
2270 "md: Dev:%08x UUID: %pU\n"
2271 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
2272 "md: (MaxDev:%u) \n",
2273 le32_to_cpu(sb->level),
2274 (unsigned long long)le64_to_cpu(sb->size),
2275 le32_to_cpu(sb->raid_disks),
2276 le32_to_cpu(sb->layout),
2277 le32_to_cpu(sb->chunksize),
2278 (unsigned long long)le64_to_cpu(sb->data_offset),
2279 (unsigned long long)le64_to_cpu(sb->data_size),
2280 (unsigned long long)le64_to_cpu(sb->super_offset),
2281 (unsigned long long)le64_to_cpu(sb->recovery_offset),
2282 le32_to_cpu(sb->dev_number),
2285 (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK,
2286 (unsigned long long)le64_to_cpu(sb->events),
2287 (unsigned long long)le64_to_cpu(sb->resync_offset),
2288 le32_to_cpu(sb->sb_csum),
2289 le32_to_cpu(sb->max_dev)
2293 static void print_rdev(struct md_rdev *rdev, int major_version)
2295 char b[BDEVNAME_SIZE];
2296 printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
2297 bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors,
2298 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
2300 if (rdev->sb_loaded) {
2301 printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version);
2302 switch (major_version) {
2304 print_sb_90(page_address(rdev->sb_page));
2307 print_sb_1(page_address(rdev->sb_page));
2311 printk(KERN_INFO "md: no rdev superblock!\n");
2314 static void md_print_devices(void)
2316 struct list_head *tmp;
2317 struct md_rdev *rdev;
2318 struct mddev *mddev;
2319 char b[BDEVNAME_SIZE];
2322 printk("md: **********************************\n");
2323 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
2324 printk("md: **********************************\n");
2325 for_each_mddev(mddev, tmp) {
2328 bitmap_print_sb(mddev->bitmap);
2330 printk("%s: ", mdname(mddev));
2331 rdev_for_each(rdev, mddev)
2332 printk("<%s>", bdevname(rdev->bdev,b));
2335 rdev_for_each(rdev, mddev)
2336 print_rdev(rdev, mddev->major_version);
2338 printk("md: **********************************\n");
2343 static void sync_sbs(struct mddev * mddev, int nospares)
2345 /* Update each superblock (in-memory image), but
2346 * if we are allowed to, skip spares which already
2347 * have the right event counter, or have one earlier
2348 * (which would mean they aren't being marked as dirty
2349 * with the rest of the array)
2351 struct md_rdev *rdev;
2352 rdev_for_each(rdev, mddev) {
2353 if (rdev->sb_events == mddev->events ||
2355 rdev->raid_disk < 0 &&
2356 rdev->sb_events+1 == mddev->events)) {
2357 /* Don't update this superblock */
2358 rdev->sb_loaded = 2;
2360 sync_super(mddev, rdev);
2361 rdev->sb_loaded = 1;
2366 static void md_update_sb(struct mddev * mddev, int force_change)
2368 struct md_rdev *rdev;
2371 int any_badblocks_changed = 0;
2375 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2379 /* First make sure individual recovery_offsets are correct */
2380 rdev_for_each(rdev, mddev) {
2381 if (rdev->raid_disk >= 0 &&
2382 mddev->delta_disks >= 0 &&
2383 !test_bit(In_sync, &rdev->flags) &&
2384 mddev->curr_resync_completed > rdev->recovery_offset)
2385 rdev->recovery_offset = mddev->curr_resync_completed;
2388 if (!mddev->persistent) {
2389 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
2390 clear_bit(MD_CHANGE_DEVS, &mddev->flags);
2391 if (!mddev->external) {
2392 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
2393 rdev_for_each(rdev, mddev) {
2394 if (rdev->badblocks.changed) {
2395 rdev->badblocks.changed = 0;
2396 md_ack_all_badblocks(&rdev->badblocks);
2397 md_error(mddev, rdev);
2399 clear_bit(Blocked, &rdev->flags);
2400 clear_bit(BlockedBadBlocks, &rdev->flags);
2401 wake_up(&rdev->blocked_wait);
2404 wake_up(&mddev->sb_wait);
2408 spin_lock_irq(&mddev->write_lock);
2410 mddev->utime = get_seconds();
2412 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2414 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2415 /* just a clean<-> dirty transition, possibly leave spares alone,
2416 * though if events isn't the right even/odd, we will have to do
2422 if (mddev->degraded)
2423 /* If the array is degraded, then skipping spares is both
2424 * dangerous and fairly pointless.
2425 * Dangerous because a device that was removed from the array
2426 * might have a event_count that still looks up-to-date,
2427 * so it can be re-added without a resync.
2428 * Pointless because if there are any spares to skip,
2429 * then a recovery will happen and soon that array won't
2430 * be degraded any more and the spare can go back to sleep then.
2434 sync_req = mddev->in_sync;
2436 /* If this is just a dirty<->clean transition, and the array is clean
2437 * and 'events' is odd, we can roll back to the previous clean state */
2439 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
2440 && mddev->can_decrease_events
2441 && mddev->events != 1) {
2443 mddev->can_decrease_events = 0;
2445 /* otherwise we have to go forward and ... */
2447 mddev->can_decrease_events = nospares;
2450 if (!mddev->events) {
2452 * oops, this 64-bit counter should never wrap.
2453 * Either we are in around ~1 trillion A.C., assuming
2454 * 1 reboot per second, or we have a bug:
2460 rdev_for_each(rdev, mddev) {
2461 if (rdev->badblocks.changed)
2462 any_badblocks_changed++;
2463 if (test_bit(Faulty, &rdev->flags))
2464 set_bit(FaultRecorded, &rdev->flags);
2467 sync_sbs(mddev, nospares);
2468 spin_unlock_irq(&mddev->write_lock);
2470 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2471 mdname(mddev), mddev->in_sync);
2473 bitmap_update_sb(mddev->bitmap);
2474 rdev_for_each(rdev, mddev) {
2475 char b[BDEVNAME_SIZE];
2477 if (rdev->sb_loaded != 1)
2478 continue; /* no noise on spare devices */
2480 if (!test_bit(Faulty, &rdev->flags) &&
2481 rdev->saved_raid_disk == -1) {
2482 md_super_write(mddev,rdev,
2483 rdev->sb_start, rdev->sb_size,
2485 pr_debug("md: (write) %s's sb offset: %llu\n",
2486 bdevname(rdev->bdev, b),
2487 (unsigned long long)rdev->sb_start);
2488 rdev->sb_events = mddev->events;
2489 if (rdev->badblocks.size) {
2490 md_super_write(mddev, rdev,
2491 rdev->badblocks.sector,
2492 rdev->badblocks.size << 9,
2494 rdev->badblocks.size = 0;
2497 } else if (test_bit(Faulty, &rdev->flags))
2498 pr_debug("md: %s (skipping faulty)\n",
2499 bdevname(rdev->bdev, b));
2501 pr_debug("(skipping incremental s/r ");
2503 if (mddev->level == LEVEL_MULTIPATH)
2504 /* only need to write one superblock... */
2507 md_super_wait(mddev);
2508 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
2510 spin_lock_irq(&mddev->write_lock);
2511 if (mddev->in_sync != sync_req ||
2512 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
2513 /* have to write it out again */
2514 spin_unlock_irq(&mddev->write_lock);
2517 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
2518 spin_unlock_irq(&mddev->write_lock);
2519 wake_up(&mddev->sb_wait);
2520 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2521 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
2523 rdev_for_each(rdev, mddev) {
2524 if (test_and_clear_bit(FaultRecorded, &rdev->flags))
2525 clear_bit(Blocked, &rdev->flags);
2527 if (any_badblocks_changed)
2528 md_ack_all_badblocks(&rdev->badblocks);
2529 clear_bit(BlockedBadBlocks, &rdev->flags);
2530 wake_up(&rdev->blocked_wait);
2534 /* words written to sysfs files may, or may not, be \n terminated.
2535 * We want to accept with case. For this we use cmd_match.
2537 static int cmd_match(const char *cmd, const char *str)
2539 /* See if cmd, written into a sysfs file, matches
2540 * str. They must either be the same, or cmd can
2541 * have a trailing newline
2543 while (*cmd && *str && *cmd == *str) {
2554 struct rdev_sysfs_entry {
2555 struct attribute attr;
2556 ssize_t (*show)(struct md_rdev *, char *);
2557 ssize_t (*store)(struct md_rdev *, const char *, size_t);
2561 state_show(struct md_rdev *rdev, char *page)
2566 if (test_bit(Faulty, &rdev->flags) ||
2567 rdev->badblocks.unacked_exist) {
2568 len+= sprintf(page+len, "%sfaulty",sep);
2571 if (test_bit(In_sync, &rdev->flags)) {
2572 len += sprintf(page+len, "%sin_sync",sep);
2575 if (test_bit(WriteMostly, &rdev->flags)) {
2576 len += sprintf(page+len, "%swrite_mostly",sep);
2579 if (test_bit(Blocked, &rdev->flags) ||
2580 (rdev->badblocks.unacked_exist
2581 && !test_bit(Faulty, &rdev->flags))) {
2582 len += sprintf(page+len, "%sblocked", sep);
2585 if (!test_bit(Faulty, &rdev->flags) &&
2586 !test_bit(In_sync, &rdev->flags)) {
2587 len += sprintf(page+len, "%sspare", sep);
2590 if (test_bit(WriteErrorSeen, &rdev->flags)) {
2591 len += sprintf(page+len, "%swrite_error", sep);
2594 if (test_bit(WantReplacement, &rdev->flags)) {
2595 len += sprintf(page+len, "%swant_replacement", sep);
2598 if (test_bit(Replacement, &rdev->flags)) {
2599 len += sprintf(page+len, "%sreplacement", sep);
2603 return len+sprintf(page+len, "\n");
2607 state_store(struct md_rdev *rdev, const char *buf, size_t len)
2610 * faulty - simulates an error
2611 * remove - disconnects the device
2612 * writemostly - sets write_mostly
2613 * -writemostly - clears write_mostly
2614 * blocked - sets the Blocked flags
2615 * -blocked - clears the Blocked and possibly simulates an error
2616 * insync - sets Insync providing device isn't active
2617 * write_error - sets WriteErrorSeen
2618 * -write_error - clears WriteErrorSeen
2621 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2622 md_error(rdev->mddev, rdev);
2623 if (test_bit(Faulty, &rdev->flags))
2627 } else if (cmd_match(buf, "remove")) {
2628 if (rdev->raid_disk >= 0)
2631 struct mddev *mddev = rdev->mddev;
2632 kick_rdev_from_array(rdev);
2634 md_update_sb(mddev, 1);
2635 md_new_event(mddev);
2638 } else if (cmd_match(buf, "writemostly")) {
2639 set_bit(WriteMostly, &rdev->flags);
2641 } else if (cmd_match(buf, "-writemostly")) {
2642 clear_bit(WriteMostly, &rdev->flags);
2644 } else if (cmd_match(buf, "blocked")) {
2645 set_bit(Blocked, &rdev->flags);
2647 } else if (cmd_match(buf, "-blocked")) {
2648 if (!test_bit(Faulty, &rdev->flags) &&
2649 rdev->badblocks.unacked_exist) {
2650 /* metadata handler doesn't understand badblocks,
2651 * so we need to fail the device
2653 md_error(rdev->mddev, rdev);
2655 clear_bit(Blocked, &rdev->flags);
2656 clear_bit(BlockedBadBlocks, &rdev->flags);
2657 wake_up(&rdev->blocked_wait);
2658 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2659 md_wakeup_thread(rdev->mddev->thread);
2662 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2663 set_bit(In_sync, &rdev->flags);
2665 } else if (cmd_match(buf, "write_error")) {
2666 set_bit(WriteErrorSeen, &rdev->flags);
2668 } else if (cmd_match(buf, "-write_error")) {
2669 clear_bit(WriteErrorSeen, &rdev->flags);
2671 } else if (cmd_match(buf, "want_replacement")) {
2672 /* Any non-spare device that is not a replacement can
2673 * become want_replacement at any time, but we then need to
2674 * check if recovery is needed.
2676 if (rdev->raid_disk >= 0 &&
2677 !test_bit(Replacement, &rdev->flags))
2678 set_bit(WantReplacement, &rdev->flags);
2679 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2680 md_wakeup_thread(rdev->mddev->thread);
2682 } else if (cmd_match(buf, "-want_replacement")) {
2683 /* Clearing 'want_replacement' is always allowed.
2684 * Once replacements starts it is too late though.
2687 clear_bit(WantReplacement, &rdev->flags);
2688 } else if (cmd_match(buf, "replacement")) {
2689 /* Can only set a device as a replacement when array has not
2690 * yet been started. Once running, replacement is automatic
2691 * from spares, or by assigning 'slot'.
2693 if (rdev->mddev->pers)
2696 set_bit(Replacement, &rdev->flags);
2699 } else if (cmd_match(buf, "-replacement")) {
2700 /* Similarly, can only clear Replacement before start */
2701 if (rdev->mddev->pers)
2704 clear_bit(Replacement, &rdev->flags);
2709 sysfs_notify_dirent_safe(rdev->sysfs_state);
2710 return err ? err : len;
2712 static struct rdev_sysfs_entry rdev_state =
2713 __ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
2716 errors_show(struct md_rdev *rdev, char *page)
2718 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2722 errors_store(struct md_rdev *rdev, const char *buf, size_t len)
2725 unsigned long n = simple_strtoul(buf, &e, 10);
2726 if (*buf && (*e == 0 || *e == '\n')) {
2727 atomic_set(&rdev->corrected_errors, n);
2732 static struct rdev_sysfs_entry rdev_errors =
2733 __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
2736 slot_show(struct md_rdev *rdev, char *page)
2738 if (rdev->raid_disk < 0)
2739 return sprintf(page, "none\n");
2741 return sprintf(page, "%d\n", rdev->raid_disk);
2745 slot_store(struct md_rdev *rdev, const char *buf, size_t len)
2749 int slot = simple_strtoul(buf, &e, 10);
2750 if (strncmp(buf, "none", 4)==0)
2752 else if (e==buf || (*e && *e!= '\n'))
2754 if (rdev->mddev->pers && slot == -1) {
2755 /* Setting 'slot' on an active array requires also
2756 * updating the 'rd%d' link, and communicating
2757 * with the personality with ->hot_*_disk.
2758 * For now we only support removing
2759 * failed/spare devices. This normally happens automatically,
2760 * but not when the metadata is externally managed.
2762 if (rdev->raid_disk == -1)
2764 /* personality does all needed checks */
2765 if (rdev->mddev->pers->hot_remove_disk == NULL)
2767 clear_bit(Blocked, &rdev->flags);
2768 remove_and_add_spares(rdev->mddev, rdev);
2769 if (rdev->raid_disk >= 0)
2771 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2772 md_wakeup_thread(rdev->mddev->thread);
2773 } else if (rdev->mddev->pers) {
2774 /* Activating a spare .. or possibly reactivating
2775 * if we ever get bitmaps working here.
2778 if (rdev->raid_disk != -1)
2781 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
2784 if (rdev->mddev->pers->hot_add_disk == NULL)
2787 if (slot >= rdev->mddev->raid_disks &&
2788 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2791 rdev->raid_disk = slot;
2792 if (test_bit(In_sync, &rdev->flags))
2793 rdev->saved_raid_disk = slot;
2795 rdev->saved_raid_disk = -1;
2796 clear_bit(In_sync, &rdev->flags);
2797 clear_bit(Bitmap_sync, &rdev->flags);
2798 err = rdev->mddev->pers->
2799 hot_add_disk(rdev->mddev, rdev);
2801 rdev->raid_disk = -1;
2804 sysfs_notify_dirent_safe(rdev->sysfs_state);
2805 if (sysfs_link_rdev(rdev->mddev, rdev))
2806 /* failure here is OK */;
2807 /* don't wakeup anyone, leave that to userspace. */
2809 if (slot >= rdev->mddev->raid_disks &&
2810 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2812 rdev->raid_disk = slot;
2813 /* assume it is working */
2814 clear_bit(Faulty, &rdev->flags);
2815 clear_bit(WriteMostly, &rdev->flags);
2816 set_bit(In_sync, &rdev->flags);
2817 sysfs_notify_dirent_safe(rdev->sysfs_state);
2823 static struct rdev_sysfs_entry rdev_slot =
2824 __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
2827 offset_show(struct md_rdev *rdev, char *page)
2829 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
2833 offset_store(struct md_rdev *rdev, const char *buf, size_t len)
2835 unsigned long long offset;
2836 if (kstrtoull(buf, 10, &offset) < 0)
2838 if (rdev->mddev->pers && rdev->raid_disk >= 0)
2840 if (rdev->sectors && rdev->mddev->external)
2841 /* Must set offset before size, so overlap checks
2844 rdev->data_offset = offset;
2845 rdev->new_data_offset = offset;
2849 static struct rdev_sysfs_entry rdev_offset =
2850 __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
2852 static ssize_t new_offset_show(struct md_rdev *rdev, char *page)
2854 return sprintf(page, "%llu\n",
2855 (unsigned long long)rdev->new_data_offset);
2858 static ssize_t new_offset_store(struct md_rdev *rdev,
2859 const char *buf, size_t len)
2861 unsigned long long new_offset;
2862 struct mddev *mddev = rdev->mddev;
2864 if (kstrtoull(buf, 10, &new_offset) < 0)
2867 if (mddev->sync_thread)
2869 if (new_offset == rdev->data_offset)
2870 /* reset is always permitted */
2872 else if (new_offset > rdev->data_offset) {
2873 /* must not push array size beyond rdev_sectors */
2874 if (new_offset - rdev->data_offset
2875 + mddev->dev_sectors > rdev->sectors)
2878 /* Metadata worries about other space details. */
2880 /* decreasing the offset is inconsistent with a backwards
2883 if (new_offset < rdev->data_offset &&
2884 mddev->reshape_backwards)
2886 /* Increasing offset is inconsistent with forwards
2887 * reshape. reshape_direction should be set to
2888 * 'backwards' first.
2890 if (new_offset > rdev->data_offset &&
2891 !mddev->reshape_backwards)
2894 if (mddev->pers && mddev->persistent &&
2895 !super_types[mddev->major_version]
2896 .allow_new_offset(rdev, new_offset))
2898 rdev->new_data_offset = new_offset;
2899 if (new_offset > rdev->data_offset)
2900 mddev->reshape_backwards = 1;
2901 else if (new_offset < rdev->data_offset)
2902 mddev->reshape_backwards = 0;
2906 static struct rdev_sysfs_entry rdev_new_offset =
2907 __ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store);
2910 rdev_size_show(struct md_rdev *rdev, char *page)
2912 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
2915 static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2917 /* check if two start/length pairs overlap */
2925 static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2927 unsigned long long blocks;
2930 if (kstrtoull(buf, 10, &blocks) < 0)
2933 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2934 return -EINVAL; /* sector conversion overflow */
2937 if (new != blocks * 2)
2938 return -EINVAL; /* unsigned long long to sector_t overflow */
2945 rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len)
2947 struct mddev *my_mddev = rdev->mddev;
2948 sector_t oldsectors = rdev->sectors;
2951 if (strict_blocks_to_sectors(buf, §ors) < 0)
2953 if (rdev->data_offset != rdev->new_data_offset)
2954 return -EINVAL; /* too confusing */
2955 if (my_mddev->pers && rdev->raid_disk >= 0) {
2956 if (my_mddev->persistent) {
2957 sectors = super_types[my_mddev->major_version].
2958 rdev_size_change(rdev, sectors);
2961 } else if (!sectors)
2962 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
2964 if (!my_mddev->pers->resize)
2965 /* Cannot change size for RAID0 or Linear etc */
2968 if (sectors < my_mddev->dev_sectors)
2969 return -EINVAL; /* component must fit device */
2971 rdev->sectors = sectors;
2972 if (sectors > oldsectors && my_mddev->external) {
2973 /* need to check that all other rdevs with the same ->bdev
2974 * do not overlap. We need to unlock the mddev to avoid
2975 * a deadlock. We have already changed rdev->sectors, and if
2976 * we have to change it back, we will have the lock again.
2978 struct mddev *mddev;
2980 struct list_head *tmp;
2982 mddev_unlock(my_mddev);
2983 for_each_mddev(mddev, tmp) {
2984 struct md_rdev *rdev2;
2986 mddev_lock_nointr(mddev);
2987 rdev_for_each(rdev2, mddev)
2988 if (rdev->bdev == rdev2->bdev &&
2990 overlaps(rdev->data_offset, rdev->sectors,
2996 mddev_unlock(mddev);
3002 mddev_lock_nointr(my_mddev);
3004 /* Someone else could have slipped in a size
3005 * change here, but doing so is just silly.
3006 * We put oldsectors back because we *know* it is
3007 * safe, and trust userspace not to race with
3010 rdev->sectors = oldsectors;
3017 static struct rdev_sysfs_entry rdev_size =
3018 __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
3021 static ssize_t recovery_start_show(struct md_rdev *rdev, char *page)
3023 unsigned long long recovery_start = rdev->recovery_offset;
3025 if (test_bit(In_sync, &rdev->flags) ||
3026 recovery_start == MaxSector)
3027 return sprintf(page, "none\n");
3029 return sprintf(page, "%llu\n", recovery_start);
3032 static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len)
3034 unsigned long long recovery_start;
3036 if (cmd_match(buf, "none"))
3037 recovery_start = MaxSector;
3038 else if (kstrtoull(buf, 10, &recovery_start))
3041 if (rdev->mddev->pers &&
3042 rdev->raid_disk >= 0)
3045 rdev->recovery_offset = recovery_start;
3046 if (recovery_start == MaxSector)
3047 set_bit(In_sync, &rdev->flags);
3049 clear_bit(In_sync, &rdev->flags);
3053 static struct rdev_sysfs_entry rdev_recovery_start =
3054 __ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
3058 badblocks_show(struct badblocks *bb, char *page, int unack);
3060 badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack);
3062 static ssize_t bb_show(struct md_rdev *rdev, char *page)
3064 return badblocks_show(&rdev->badblocks, page, 0);
3066 static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len)
3068 int rv = badblocks_store(&rdev->badblocks, page, len, 0);
3069 /* Maybe that ack was all we needed */
3070 if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags))
3071 wake_up(&rdev->blocked_wait);
3074 static struct rdev_sysfs_entry rdev_bad_blocks =
3075 __ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
3078 static ssize_t ubb_show(struct md_rdev *rdev, char *page)
3080 return badblocks_show(&rdev->badblocks, page, 1);
3082 static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len)
3084 return badblocks_store(&rdev->badblocks, page, len, 1);
3086 static struct rdev_sysfs_entry rdev_unack_bad_blocks =
3087 __ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
3089 static struct attribute *rdev_default_attrs[] = {
3094 &rdev_new_offset.attr,
3096 &rdev_recovery_start.attr,
3097 &rdev_bad_blocks.attr,
3098 &rdev_unack_bad_blocks.attr,
3102 rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3104 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
3105 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
3106 struct mddev *mddev = rdev->mddev;
3112 rv = mddev ? mddev_lock(mddev) : -EBUSY;
3114 if (rdev->mddev == NULL)
3117 rv = entry->show(rdev, page);
3118 mddev_unlock(mddev);
3124 rdev_attr_store(struct kobject *kobj, struct attribute *attr,
3125 const char *page, size_t length)
3127 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
3128 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
3130 struct mddev *mddev = rdev->mddev;
3134 if (!capable(CAP_SYS_ADMIN))
3136 rv = mddev ? mddev_lock(mddev): -EBUSY;
3138 if (rdev->mddev == NULL)
3141 rv = entry->store(rdev, page, length);
3142 mddev_unlock(mddev);
3147 static void rdev_free(struct kobject *ko)
3149 struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj);
3152 static const struct sysfs_ops rdev_sysfs_ops = {
3153 .show = rdev_attr_show,
3154 .store = rdev_attr_store,
3156 static struct kobj_type rdev_ktype = {
3157 .release = rdev_free,
3158 .sysfs_ops = &rdev_sysfs_ops,
3159 .default_attrs = rdev_default_attrs,
3162 int md_rdev_init(struct md_rdev *rdev)
3165 rdev->saved_raid_disk = -1;
3166 rdev->raid_disk = -1;
3168 rdev->data_offset = 0;
3169 rdev->new_data_offset = 0;
3170 rdev->sb_events = 0;
3171 rdev->last_read_error.tv_sec = 0;
3172 rdev->last_read_error.tv_nsec = 0;
3173 rdev->sb_loaded = 0;
3174 rdev->bb_page = NULL;
3175 atomic_set(&rdev->nr_pending, 0);
3176 atomic_set(&rdev->read_errors, 0);
3177 atomic_set(&rdev->corrected_errors, 0);
3179 INIT_LIST_HEAD(&rdev->same_set);
3180 init_waitqueue_head(&rdev->blocked_wait);
3182 /* Add space to store bad block list.
3183 * This reserves the space even on arrays where it cannot
3184 * be used - I wonder if that matters
3186 rdev->badblocks.count = 0;
3187 rdev->badblocks.shift = -1; /* disabled until explicitly enabled */
3188 rdev->badblocks.page = kmalloc(PAGE_SIZE, GFP_KERNEL);
3189 seqlock_init(&rdev->badblocks.lock);
3190 if (rdev->badblocks.page == NULL)
3195 EXPORT_SYMBOL_GPL(md_rdev_init);
3197 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3199 * mark the device faulty if:
3201 * - the device is nonexistent (zero size)
3202 * - the device has no valid superblock
3204 * a faulty rdev _never_ has rdev->sb set.
3206 static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor)
3208 char b[BDEVNAME_SIZE];
3210 struct md_rdev *rdev;
3213 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
3215 printk(KERN_ERR "md: could not alloc mem for new device!\n");
3216 return ERR_PTR(-ENOMEM);
3219 err = md_rdev_init(rdev);
3222 err = alloc_disk_sb(rdev);
3226 err = lock_rdev(rdev, newdev, super_format == -2);
3230 kobject_init(&rdev->kobj, &rdev_ktype);
3232 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
3235 "md: %s has zero or unknown size, marking faulty!\n",
3236 bdevname(rdev->bdev,b));
3241 if (super_format >= 0) {
3242 err = super_types[super_format].
3243 load_super(rdev, NULL, super_minor);
3244 if (err == -EINVAL) {
3246 "md: %s does not have a valid v%d.%d "
3247 "superblock, not importing!\n",
3248 bdevname(rdev->bdev,b),
3249 super_format, super_minor);
3254 "md: could not read %s's sb, not importing!\n",
3255 bdevname(rdev->bdev,b));
3265 md_rdev_clear(rdev);
3267 return ERR_PTR(err);
3271 * Check a full RAID array for plausibility
3275 static void analyze_sbs(struct mddev * mddev)
3278 struct md_rdev *rdev, *freshest, *tmp;
3279 char b[BDEVNAME_SIZE];
3282 rdev_for_each_safe(rdev, tmp, mddev)
3283 switch (super_types[mddev->major_version].
3284 load_super(rdev, freshest, mddev->minor_version)) {
3292 "md: fatal superblock inconsistency in %s"
3293 " -- removing from array\n",
3294 bdevname(rdev->bdev,b));
3295 kick_rdev_from_array(rdev);
3299 super_types[mddev->major_version].
3300 validate_super(mddev, freshest);
3303 rdev_for_each_safe(rdev, tmp, mddev) {
3304 if (mddev->max_disks &&
3305 (rdev->desc_nr >= mddev->max_disks ||
3306 i > mddev->max_disks)) {
3308 "md: %s: %s: only %d devices permitted\n",
3309 mdname(mddev), bdevname(rdev->bdev, b),
3311 kick_rdev_from_array(rdev);
3314 if (rdev != freshest)
3315 if (super_types[mddev->major_version].
3316 validate_super(mddev, rdev)) {
3317 printk(KERN_WARNING "md: kicking non-fresh %s"
3319 bdevname(rdev->bdev,b));
3320 kick_rdev_from_array(rdev);
3323 if (mddev->level == LEVEL_MULTIPATH) {
3324 rdev->desc_nr = i++;
3325 rdev->raid_disk = rdev->desc_nr;
3326 set_bit(In_sync, &rdev->flags);
3327 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
3328 rdev->raid_disk = -1;
3329 clear_bit(In_sync, &rdev->flags);
3334 /* Read a fixed-point number.
3335 * Numbers in sysfs attributes should be in "standard" units where
3336 * possible, so time should be in seconds.
3337 * However we internally use a a much smaller unit such as
3338 * milliseconds or jiffies.
3339 * This function takes a decimal number with a possible fractional
3340 * component, and produces an integer which is the result of
3341 * multiplying that number by 10^'scale'.
3342 * all without any floating-point arithmetic.
3344 int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3346 unsigned long result = 0;
3348 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3351 else if (decimals < scale) {
3354 result = result * 10 + value;
3366 while (decimals < scale) {
3375 static void md_safemode_timeout(unsigned long data);
3378 safe_delay_show(struct mddev *mddev, char *page)
3380 int msec = (mddev->safemode_delay*1000)/HZ;
3381 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3384 safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len)
3388 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
3391 mddev->safemode_delay = 0;
3393 unsigned long old_delay = mddev->safemode_delay;
3394 mddev->safemode_delay = (msec*HZ)/1000;
3395 if (mddev->safemode_delay == 0)
3396 mddev->safemode_delay = 1;
3397 if (mddev->safemode_delay < old_delay || old_delay == 0)
3398 md_safemode_timeout((unsigned long)mddev);
3402 static struct md_sysfs_entry md_safe_delay =
3403 __ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
3406 level_show(struct mddev *mddev, char *page)
3408 struct md_personality *p = mddev->pers;
3410 return sprintf(page, "%s\n", p->name);
3411 else if (mddev->clevel[0])
3412 return sprintf(page, "%s\n", mddev->clevel);
3413 else if (mddev->level != LEVEL_NONE)
3414 return sprintf(page, "%d\n", mddev->level);
3420 level_store(struct mddev *mddev, const char *buf, size_t len)
3424 struct md_personality *pers;
3427 struct md_rdev *rdev;
3429 if (mddev->pers == NULL) {
3432 if (len >= sizeof(mddev->clevel))
3434 strncpy(mddev->clevel, buf, len);
3435 if (mddev->clevel[len-1] == '\n')
3437 mddev->clevel[len] = 0;
3438 mddev->level = LEVEL_NONE;
3442 /* request to change the personality. Need to ensure:
3443 * - array is not engaged in resync/recovery/reshape
3444 * - old personality can be suspended
3445 * - new personality will access other array.
3448 if (mddev->sync_thread ||
3449 mddev->reshape_position != MaxSector ||
3450 mddev->sysfs_active)
3453 if (!mddev->pers->quiesce) {
3454 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
3455 mdname(mddev), mddev->pers->name);
3459 /* Now find the new personality */
3460 if (len == 0 || len >= sizeof(clevel))
3462 strncpy(clevel, buf, len);
3463 if (clevel[len-1] == '\n')
3466 if (kstrtol(clevel, 10, &level))
3469 if (request_module("md-%s", clevel) != 0)
3470 request_module("md-level-%s", clevel);
3471 spin_lock(&pers_lock);
3472 pers = find_pers(level, clevel);
3473 if (!pers || !try_module_get(pers->owner)) {
3474 spin_unlock(&pers_lock);
3475 printk(KERN_WARNING "md: personality %s not loaded\n", clevel);
3478 spin_unlock(&pers_lock);
3480 if (pers == mddev->pers) {
3481 /* Nothing to do! */
3482 module_put(pers->owner);
3485 if (!pers->takeover) {
3486 module_put(pers->owner);
3487 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
3488 mdname(mddev), clevel);
3492 rdev_for_each(rdev, mddev)
3493 rdev->new_raid_disk = rdev->raid_disk;
3495 /* ->takeover must set new_* and/or delta_disks
3496 * if it succeeds, and may set them when it fails.
3498 priv = pers->takeover(mddev);
3500 mddev->new_level = mddev->level;
3501 mddev->new_layout = mddev->layout;
3502 mddev->new_chunk_sectors = mddev->chunk_sectors;
3503 mddev->raid_disks -= mddev->delta_disks;
3504 mddev->delta_disks = 0;
3505 mddev->reshape_backwards = 0;
3506 module_put(pers->owner);
3507 printk(KERN_WARNING "md: %s: %s would not accept array\n",
3508 mdname(mddev), clevel);
3509 return PTR_ERR(priv);
3512 /* Looks like we have a winner */
3513 mddev_suspend(mddev);
3514 mddev->pers->stop(mddev);
3516 if (mddev->pers->sync_request == NULL &&
3517 pers->sync_request != NULL) {
3518 /* need to add the md_redundancy_group */
3519 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3521 "md: cannot register extra attributes for %s\n",
3523 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
3525 if (mddev->pers->sync_request != NULL &&
3526 pers->sync_request == NULL) {
3527 /* need to remove the md_redundancy_group */
3528 if (mddev->to_remove == NULL)
3529 mddev->to_remove = &md_redundancy_group;
3532 if (mddev->pers->sync_request == NULL &&
3534 /* We are converting from a no-redundancy array
3535 * to a redundancy array and metadata is managed
3536 * externally so we need to be sure that writes
3537 * won't block due to a need to transition
3539 * until external management is started.
3542 mddev->safemode_delay = 0;
3543 mddev->safemode = 0;
3546 rdev_for_each(rdev, mddev) {
3547 if (rdev->raid_disk < 0)
3549 if (rdev->new_raid_disk >= mddev->raid_disks)
3550 rdev->new_raid_disk = -1;
3551 if (rdev->new_raid_disk == rdev->raid_disk)
3553 sysfs_unlink_rdev(mddev, rdev);
3555 rdev_for_each(rdev, mddev) {
3556 if (rdev->raid_disk < 0)
3558 if (rdev->new_raid_disk == rdev->raid_disk)
3560 rdev->raid_disk = rdev->new_raid_disk;
3561 if (rdev->raid_disk < 0)
3562 clear_bit(In_sync, &rdev->flags);
3564 if (sysfs_link_rdev(mddev, rdev))
3565 printk(KERN_WARNING "md: cannot register rd%d"
3566 " for %s after level change\n",
3567 rdev->raid_disk, mdname(mddev));
3571 module_put(mddev->pers->owner);
3573 mddev->private = priv;
3574 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3575 mddev->level = mddev->new_level;
3576 mddev->layout = mddev->new_layout;
3577 mddev->chunk_sectors = mddev->new_chunk_sectors;
3578 mddev->delta_disks = 0;
3579 mddev->reshape_backwards = 0;
3580 mddev->degraded = 0;
3581 if (mddev->pers->sync_request == NULL) {
3582 /* this is now an array without redundancy, so
3583 * it must always be in_sync
3586 del_timer_sync(&mddev->safemode_timer);
3588 blk_set_stacking_limits(&mddev->queue->limits);
3590 set_bit(MD_CHANGE_DEVS, &mddev->flags);
3591 mddev_resume(mddev);
3592 sysfs_notify(&mddev->kobj, NULL, "level");
3593 md_new_event(mddev);
3597 static struct md_sysfs_entry md_level =
3598 __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
3602 layout_show(struct mddev *mddev, char *page)
3604 /* just a number, not meaningful for all levels */
3605 if (mddev->reshape_position != MaxSector &&
3606 mddev->layout != mddev->new_layout)
3607 return sprintf(page, "%d (%d)\n",
3608 mddev->new_layout, mddev->layout);
3609 return sprintf(page, "%d\n", mddev->layout);
3613 layout_store(struct mddev *mddev, const char *buf, size_t len)
3616 unsigned long n = simple_strtoul(buf, &e, 10);
3618 if (!*buf || (*e && *e != '\n'))
3623 if (mddev->pers->check_reshape == NULL)
3625 mddev->new_layout = n;
3626 err = mddev->pers->check_reshape(mddev);
3628 mddev->new_layout = mddev->layout;
3632 mddev->new_layout = n;
3633 if (mddev->reshape_position == MaxSector)
3638 static struct md_sysfs_entry md_layout =
3639 __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
3643 raid_disks_show(struct mddev *mddev, char *page)
3645 if (mddev->raid_disks == 0)
3647 if (mddev->reshape_position != MaxSector &&
3648 mddev->delta_disks != 0)
3649 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3650 mddev->raid_disks - mddev->delta_disks);
3651 return sprintf(page, "%d\n", mddev->raid_disks);
3654 static int update_raid_disks(struct mddev *mddev, int raid_disks);
3657 raid_disks_store(struct mddev *mddev, const char *buf, size_t len)
3661 unsigned long n = simple_strtoul(buf, &e, 10);
3663 if (!*buf || (*e && *e != '\n'))
3667 rv = update_raid_disks(mddev, n);
3668 else if (mddev->reshape_position != MaxSector) {
3669 struct md_rdev *rdev;
3670 int olddisks = mddev->raid_disks - mddev->delta_disks;
3672 rdev_for_each(rdev, mddev) {
3674 rdev->data_offset < rdev->new_data_offset)
3677 rdev->data_offset > rdev->new_data_offset)
3680 mddev->delta_disks = n - olddisks;
3681 mddev->raid_disks = n;
3682 mddev->reshape_backwards = (mddev->delta_disks < 0);
3684 mddev->raid_disks = n;
3685 return rv ? rv : len;
3687 static struct md_sysfs_entry md_raid_disks =
3688 __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
3691 chunk_size_show(struct mddev *mddev, char *page)
3693 if (mddev->reshape_position != MaxSector &&
3694 mddev->chunk_sectors != mddev->new_chunk_sectors)
3695 return sprintf(page, "%d (%d)\n",
3696 mddev->new_chunk_sectors << 9,
3697 mddev->chunk_sectors << 9);
3698 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
3702 chunk_size_store(struct mddev *mddev, const char *buf, size_t len)
3705 unsigned long n = simple_strtoul(buf, &e, 10);
3707 if (!*buf || (*e && *e != '\n'))
3712 if (mddev->pers->check_reshape == NULL)
3714 mddev->new_chunk_sectors = n >> 9;
3715 err = mddev->pers->check_reshape(mddev);
3717 mddev->new_chunk_sectors = mddev->chunk_sectors;
3721 mddev->new_chunk_sectors = n >> 9;
3722 if (mddev->reshape_position == MaxSector)
3723 mddev->chunk_sectors = n >> 9;
3727 static struct md_sysfs_entry md_chunk_size =
3728 __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
3731 resync_start_show(struct mddev *mddev, char *page)
3733 if (mddev->recovery_cp == MaxSector)
3734 return sprintf(page, "none\n");
3735 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3739 resync_start_store(struct mddev *mddev, const char *buf, size_t len)
3742 unsigned long long n = simple_strtoull(buf, &e, 10);
3744 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
3746 if (cmd_match(buf, "none"))
3748 else if (!*buf || (*e && *e != '\n'))
3751 mddev->recovery_cp = n;
3753 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
3756 static struct md_sysfs_entry md_resync_start =
3757 __ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
3760 * The array state can be:
3763 * No devices, no size, no level
3764 * Equivalent to STOP_ARRAY ioctl
3766 * May have some settings, but array is not active
3767 * all IO results in error
3768 * When written, doesn't tear down array, but just stops it
3769 * suspended (not supported yet)
3770 * All IO requests will block. The array can be reconfigured.
3771 * Writing this, if accepted, will block until array is quiescent
3773 * no resync can happen. no superblocks get written.
3774 * write requests fail
3776 * like readonly, but behaves like 'clean' on a write request.
3778 * clean - no pending writes, but otherwise active.
3779 * When written to inactive array, starts without resync
3780 * If a write request arrives then
3781 * if metadata is known, mark 'dirty' and switch to 'active'.
3782 * if not known, block and switch to write-pending
3783 * If written to an active array that has pending writes, then fails.
3785 * fully active: IO and resync can be happening.
3786 * When written to inactive array, starts with resync
3789 * clean, but writes are blocked waiting for 'active' to be written.
3792 * like active, but no writes have been seen for a while (100msec).
3795 enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3796 write_pending, active_idle, bad_word};
3797 static char *array_states[] = {
3798 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3799 "write-pending", "active-idle", NULL };
3801 static int match_word(const char *word, char **list)
3804 for (n=0; list[n]; n++)
3805 if (cmd_match(word, list[n]))
3811 array_state_show(struct mddev *mddev, char *page)
3813 enum array_state st = inactive;
3826 else if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
3828 else if (mddev->safemode)
3834 if (list_empty(&mddev->disks) &&
3835 mddev->raid_disks == 0 &&
3836 mddev->dev_sectors == 0)
3841 return sprintf(page, "%s\n", array_states[st]);
3844 static int do_md_stop(struct mddev * mddev, int ro, struct block_device *bdev);
3845 static int md_set_readonly(struct mddev * mddev, struct block_device *bdev);
3846 static int do_md_run(struct mddev * mddev);
3847 static int restart_array(struct mddev *mddev);
3850 array_state_store(struct mddev *mddev, const char *buf, size_t len)
3853 enum array_state st = match_word(buf, array_states);
3858 /* stopping an active array */
3859 err = do_md_stop(mddev, 0, NULL);
3862 /* stopping an active array */
3864 err = do_md_stop(mddev, 2, NULL);
3866 err = 0; /* already inactive */
3869 break; /* not supported yet */
3872 err = md_set_readonly(mddev, NULL);
3875 set_disk_ro(mddev->gendisk, 1);
3876 err = do_md_run(mddev);
3882 err = md_set_readonly(mddev, NULL);
3883 else if (mddev->ro == 1)
3884 err = restart_array(mddev);
3887 set_disk_ro(mddev->gendisk, 0);
3891 err = do_md_run(mddev);
3896 restart_array(mddev);
3897 spin_lock_irq(&mddev->write_lock);
3898 if (atomic_read(&mddev->writes_pending) == 0) {
3899 if (mddev->in_sync == 0) {
3901 if (mddev->safemode == 1)
3902 mddev->safemode = 0;
3903 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
3908 spin_unlock_irq(&mddev->write_lock);
3914 restart_array(mddev);
3915 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
3916 wake_up(&mddev->sb_wait);
3920 set_disk_ro(mddev->gendisk, 0);
3921 err = do_md_run(mddev);
3926 /* these cannot be set */
3932 if (mddev->hold_active == UNTIL_IOCTL)
3933 mddev->hold_active = 0;
3934 sysfs_notify_dirent_safe(mddev->sysfs_state);
3938 static struct md_sysfs_entry md_array_state =
3939 __ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
3942 max_corrected_read_errors_show(struct mddev *mddev, char *page) {
3943 return sprintf(page, "%d\n",
3944 atomic_read(&mddev->max_corr_read_errors));
3948 max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len)
3951 unsigned long n = simple_strtoul(buf, &e, 10);
3953 if (*buf && (*e == 0 || *e == '\n')) {
3954 atomic_set(&mddev->max_corr_read_errors, n);
3960 static struct md_sysfs_entry max_corr_read_errors =
3961 __ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
3962 max_corrected_read_errors_store);
3965 null_show(struct mddev *mddev, char *page)
3971 new_dev_store(struct mddev *mddev, const char *buf, size_t len)
3973 /* buf must be %d:%d\n? giving major and minor numbers */
3974 /* The new device is added to the array.
3975 * If the array has a persistent superblock, we read the
3976 * superblock to initialise info and check validity.
3977 * Otherwise, only checking done is that in bind_rdev_to_array,
3978 * which mainly checks size.
3981 int major = simple_strtoul(buf, &e, 10);
3984 struct md_rdev *rdev;
3987 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
3989 minor = simple_strtoul(e+1, &e, 10);
3990 if (*e && *e != '\n')
3992 dev = MKDEV(major, minor);
3993 if (major != MAJOR(dev) ||
3994 minor != MINOR(dev))
3998 if (mddev->persistent) {
3999 rdev = md_import_device(dev, mddev->major_version,
4000 mddev->minor_version);
4001 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
4002 struct md_rdev *rdev0
4003 = list_entry(mddev->disks.next,
4004 struct md_rdev, same_set);
4005 err = super_types[mddev->major_version]
4006 .load_super(rdev, rdev0, mddev->minor_version);
4010 } else if (mddev->external)
4011 rdev = md_import_device(dev, -2, -1);
4013 rdev = md_import_device(dev, -1, -1);
4016 return PTR_ERR(rdev);
4017 err = bind_rdev_to_array(rdev, mddev);
4021 return err ? err : len;
4024 static struct md_sysfs_entry md_new_device =
4025 __ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
4028 bitmap_store(struct mddev *mddev, const char *buf, size_t len)
4031 unsigned long chunk, end_chunk;
4035 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
4037 chunk = end_chunk = simple_strtoul(buf, &end, 0);
4038 if (buf == end) break;
4039 if (*end == '-') { /* range */
4041 end_chunk = simple_strtoul(buf, &end, 0);
4042 if (buf == end) break;
4044 if (*end && !isspace(*end)) break;
4045 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
4046 buf = skip_spaces(end);
4048 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
4053 static struct md_sysfs_entry md_bitmap =
4054 __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
4057 size_show(struct mddev *mddev, char *page)
4059 return sprintf(page, "%llu\n",
4060 (unsigned long long)mddev->dev_sectors / 2);
4063 static int update_size(struct mddev *mddev, sector_t num_sectors);
4066 size_store(struct mddev *mddev, const char *buf, size_t len)
4068 /* If array is inactive, we can reduce the component size, but
4069 * not increase it (except from 0).
4070 * If array is active, we can try an on-line resize
4073 int err = strict_blocks_to_sectors(buf, §ors);
4078 err = update_size(mddev, sectors);
4079 md_update_sb(mddev, 1);
4081 if (mddev->dev_sectors == 0 ||
4082 mddev->dev_sectors > sectors)
4083 mddev->dev_sectors = sectors;
4087 return err ? err : len;
4090 static struct md_sysfs_entry md_size =
4091 __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
4094 /* Metadata version.
4096 * 'none' for arrays with no metadata (good luck...)
4097 * 'external' for arrays with externally managed metadata,
4098 * or N.M for internally known formats
4101 metadata_show(struct mddev *mddev, char *page)
4103 if (mddev->persistent)
4104 return sprintf(page, "%d.%d\n",
4105 mddev->major_version, mddev->minor_version);
4106 else if (mddev->external)
4107 return sprintf(page, "external:%s\n", mddev->metadata_type);
4109 return sprintf(page, "none\n");
4113 metadata_store(struct mddev *mddev, const char *buf, size_t len)
4117 /* Changing the details of 'external' metadata is
4118 * always permitted. Otherwise there must be
4119 * no devices attached to the array.
4121 if (mddev->external && strncmp(buf, "external:", 9) == 0)
4123 else if (!list_empty(&mddev->disks))
4126 if (cmd_match(buf, "none")) {
4127 mddev->persistent = 0;
4128 mddev->external = 0;
4129 mddev->major_version = 0;
4130 mddev->minor_version = 90;
4133 if (strncmp(buf, "external:", 9) == 0) {
4134 size_t namelen = len-9;
4135 if (namelen >= sizeof(mddev->metadata_type))
4136 namelen = sizeof(mddev->metadata_type)-1;
4137 strncpy(mddev->metadata_type, buf+9, namelen);
4138 mddev->metadata_type[namelen] = 0;
4139 if (namelen && mddev->metadata_type[namelen-1] == '\n')
4140 mddev->metadata_type[--namelen] = 0;
4141 mddev->persistent = 0;
4142 mddev->external = 1;
4143 mddev->major_version = 0;
4144 mddev->minor_version = 90;
4147 major = simple_strtoul(buf, &e, 10);
4148 if (e==buf || *e != '.')
4151 minor = simple_strtoul(buf, &e, 10);
4152 if (e==buf || (*e && *e != '\n') )
4154 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
4156 mddev->major_version = major;
4157 mddev->minor_version = minor;
4158 mddev->persistent = 1;
4159 mddev->external = 0;
4163 static struct md_sysfs_entry md_metadata =
4164 __ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
4167 action_show(struct mddev *mddev, char *page)
4169 char *type = "idle";
4170 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
4172 else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
4173 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
4174 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4176 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
4177 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
4179 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
4183 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
4186 return sprintf(page, "%s\n", type);
4190 action_store(struct mddev *mddev, const char *page, size_t len)
4192 if (!mddev->pers || !mddev->pers->sync_request)
4195 if (cmd_match(page, "frozen"))
4196 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4198 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4200 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
4201 if (mddev->sync_thread) {
4202 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4203 md_reap_sync_thread(mddev);
4205 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
4206 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
4208 else if (cmd_match(page, "resync"))
4209 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4210 else if (cmd_match(page, "recover")) {
4211 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
4212 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4213 } else if (cmd_match(page, "reshape")) {
4215 if (mddev->pers->start_reshape == NULL)
4217 err = mddev->pers->start_reshape(mddev);
4220 sysfs_notify(&mddev->kobj, NULL, "degraded");
4222 if (cmd_match(page, "check"))
4223 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
4224 else if (!cmd_match(page, "repair"))
4226 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
4227 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4229 if (mddev->ro == 2) {
4230 /* A write to sync_action is enough to justify
4231 * canceling read-auto mode
4234 md_wakeup_thread(mddev->sync_thread);
4236 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4237 md_wakeup_thread(mddev->thread);
4238 sysfs_notify_dirent_safe(mddev->sysfs_action);
4242 static struct md_sysfs_entry md_scan_mode =
4243 __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
4246 last_sync_action_show(struct mddev *mddev, char *page)
4248 return sprintf(page, "%s\n", mddev->last_sync_action);
4251 static struct md_sysfs_entry md_last_scan_mode = __ATTR_RO(last_sync_action);
4254 mismatch_cnt_show(struct mddev *mddev, char *page)
4256 return sprintf(page, "%llu\n",
4257 (unsigned long long)
4258 atomic64_read(&mddev->resync_mismatches));
4261 static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
4264 sync_min_show(struct mddev *mddev, char *page)
4266 return sprintf(page, "%d (%s)\n", speed_min(mddev),
4267 mddev->sync_speed_min ? "local": "system");
4271 sync_min_store(struct mddev *mddev, const char *buf, size_t len)
4275 if (strncmp(buf, "system", 6)==0) {
4276 mddev->sync_speed_min = 0;
4279 min = simple_strtoul(buf, &e, 10);
4280 if (buf == e || (*e && *e != '\n') || min <= 0)
4282 mddev->sync_speed_min = min;
4286 static struct md_sysfs_entry md_sync_min =
4287 __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
4290 sync_max_show(struct mddev *mddev, char *page)
4292 return sprintf(page, "%d (%s)\n", speed_max(mddev),
4293 mddev->sync_speed_max ? "local": "system");
4297 sync_max_store(struct mddev *mddev, const char *buf, size_t len)
4301 if (strncmp(buf, "system", 6)==0) {
4302 mddev->sync_speed_max = 0;
4305 max = simple_strtoul(buf, &e, 10);
4306 if (buf == e || (*e && *e != '\n') || max <= 0)
4308 mddev->sync_speed_max = max;
4312 static struct md_sysfs_entry md_sync_max =
4313 __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
4316 degraded_show(struct mddev *mddev, char *page)
4318 return sprintf(page, "%d\n", mddev->degraded);
4320 static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
4323 sync_force_parallel_show(struct mddev *mddev, char *page)
4325 return sprintf(page, "%d\n", mddev->parallel_resync);
4329 sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len)
4333 if (kstrtol(buf, 10, &n))
4336 if (n != 0 && n != 1)
4339 mddev->parallel_resync = n;
4341 if (mddev->sync_thread)
4342 wake_up(&resync_wait);
4347 /* force parallel resync, even with shared block devices */
4348 static struct md_sysfs_entry md_sync_force_parallel =
4349 __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
4350 sync_force_parallel_show, sync_force_parallel_store);
4353 sync_speed_show(struct mddev *mddev, char *page)
4355 unsigned long resync, dt, db;
4356 if (mddev->curr_resync == 0)
4357 return sprintf(page, "none\n");
4358 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
4359 dt = (jiffies - mddev->resync_mark) / HZ;
4361 db = resync - mddev->resync_mark_cnt;
4362 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
4365 static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
4368 sync_completed_show(struct mddev *mddev, char *page)
4370 unsigned long long max_sectors, resync;
4372 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4373 return sprintf(page, "none\n");
4375 if (mddev->curr_resync == 1 ||
4376 mddev->curr_resync == 2)
4377 return sprintf(page, "delayed\n");
4379 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
4380 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4381 max_sectors = mddev->resync_max_sectors;
4383 max_sectors = mddev->dev_sectors;
4385 resync = mddev->curr_resync_completed;
4386 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
4389 static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
4392 min_sync_show(struct mddev *mddev, char *page)
4394 return sprintf(page, "%llu\n",
4395 (unsigned long long)mddev->resync_min);
4398 min_sync_store(struct mddev *mddev, const char *buf, size_t len)
4400 unsigned long long min;
4401 if (kstrtoull(buf, 10, &min))
4403 if (min > mddev->resync_max)
4405 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4408 /* Must be a multiple of chunk_size */
4409 if (mddev->chunk_sectors) {
4410 sector_t temp = min;
4411 if (sector_div(temp, mddev->chunk_sectors))
4414 mddev->resync_min = min;
4419 static struct md_sysfs_entry md_min_sync =
4420 __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
4423 max_sync_show(struct mddev *mddev, char *page)
4425 if (mddev->resync_max == MaxSector)
4426 return sprintf(page, "max\n");
4428 return sprintf(page, "%llu\n",
4429 (unsigned long long)mddev->resync_max);
4432 max_sync_store(struct mddev *mddev, const char *buf, size_t len)
4434 if (strncmp(buf, "max", 3) == 0)
4435 mddev->resync_max = MaxSector;
4437 unsigned long long max;
4438 if (kstrtoull(buf, 10, &max))
4440 if (max < mddev->resync_min)
4442 if (max < mddev->resync_max &&
4444 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4447 /* Must be a multiple of chunk_size */
4448 if (mddev->chunk_sectors) {
4449 sector_t temp = max;
4450 if (sector_div(temp, mddev->chunk_sectors))
4453 mddev->resync_max = max;
4455 wake_up(&mddev->recovery_wait);
4459 static struct md_sysfs_entry md_max_sync =
4460 __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4463 suspend_lo_show(struct mddev *mddev, char *page)
4465 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4469 suspend_lo_store(struct mddev *mddev, const char *buf, size_t len)
4472 unsigned long long new = simple_strtoull(buf, &e, 10);
4473 unsigned long long old = mddev->suspend_lo;
4475 if (mddev->pers == NULL ||
4476 mddev->pers->quiesce == NULL)
4478 if (buf == e || (*e && *e != '\n'))
4481 mddev->suspend_lo = new;
4483 /* Shrinking suspended region */
4484 mddev->pers->quiesce(mddev, 2);
4486 /* Expanding suspended region - need to wait */
4487 mddev->pers->quiesce(mddev, 1);
4488 mddev->pers->quiesce(mddev, 0);
4492 static struct md_sysfs_entry md_suspend_lo =
4493 __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4497 suspend_hi_show(struct mddev *mddev, char *page)
4499 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4503 suspend_hi_store(struct mddev *mddev, const char *buf, size_t len)
4506 unsigned long long new = simple_strtoull(buf, &e, 10);
4507 unsigned long long old = mddev->suspend_hi;
4509 if (mddev->pers == NULL ||
4510 mddev->pers->quiesce == NULL)
4512 if (buf == e || (*e && *e != '\n'))
4515 mddev->suspend_hi = new;
4517 /* Shrinking suspended region */
4518 mddev->pers->quiesce(mddev, 2);
4520 /* Expanding suspended region - need to wait */
4521 mddev->pers->quiesce(mddev, 1);
4522 mddev->pers->quiesce(mddev, 0);
4526 static struct md_sysfs_entry md_suspend_hi =
4527 __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4530 reshape_position_show(struct mddev *mddev, char *page)
4532 if (mddev->reshape_position != MaxSector)
4533 return sprintf(page, "%llu\n",
4534 (unsigned long long)mddev->reshape_position);
4535 strcpy(page, "none\n");
4540 reshape_position_store(struct mddev *mddev, const char *buf, size_t len)
4542 struct md_rdev *rdev;
4544 unsigned long long new = simple_strtoull(buf, &e, 10);
4547 if (buf == e || (*e && *e != '\n'))
4549 mddev->reshape_position = new;
4550 mddev->delta_disks = 0;
4551 mddev->reshape_backwards = 0;
4552 mddev->new_level = mddev->level;
4553 mddev->new_layout = mddev->layout;
4554 mddev->new_chunk_sectors = mddev->chunk_sectors;
4555 rdev_for_each(rdev, mddev)
4556 rdev->new_data_offset = rdev->data_offset;
4560 static struct md_sysfs_entry md_reshape_position =
4561 __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4562 reshape_position_store);
4565 reshape_direction_show(struct mddev *mddev, char *page)
4567 return sprintf(page, "%s\n",
4568 mddev->reshape_backwards ? "backwards" : "forwards");
4572 reshape_direction_store(struct mddev *mddev, const char *buf, size_t len)
4575 if (cmd_match(buf, "forwards"))
4577 else if (cmd_match(buf, "backwards"))
4581 if (mddev->reshape_backwards == backwards)
4584 /* check if we are allowed to change */
4585 if (mddev->delta_disks)
4588 if (mddev->persistent &&
4589 mddev->major_version == 0)
4592 mddev->reshape_backwards = backwards;
4596 static struct md_sysfs_entry md_reshape_direction =
4597 __ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show,
4598 reshape_direction_store);
4601 array_size_show(struct mddev *mddev, char *page)
4603 if (mddev->external_size)
4604 return sprintf(page, "%llu\n",
4605 (unsigned long long)mddev->array_sectors/2);
4607 return sprintf(page, "default\n");
4611 array_size_store(struct mddev *mddev, const char *buf, size_t len)
4615 if (strncmp(buf, "default", 7) == 0) {
4617 sectors = mddev->pers->size(mddev, 0, 0);
4619 sectors = mddev->array_sectors;
4621 mddev->external_size = 0;
4623 if (strict_blocks_to_sectors(buf, §ors) < 0)
4625 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
4628 mddev->external_size = 1;
4631 mddev->array_sectors = sectors;
4633 set_capacity(mddev->gendisk, mddev->array_sectors);
4634 revalidate_disk(mddev->gendisk);
4639 static struct md_sysfs_entry md_array_size =
4640 __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4643 static struct attribute *md_default_attrs[] = {
4646 &md_raid_disks.attr,
4647 &md_chunk_size.attr,
4649 &md_resync_start.attr,
4651 &md_new_device.attr,
4652 &md_safe_delay.attr,
4653 &md_array_state.attr,
4654 &md_reshape_position.attr,
4655 &md_reshape_direction.attr,
4656 &md_array_size.attr,
4657 &max_corr_read_errors.attr,
4661 static struct attribute *md_redundancy_attrs[] = {
4663 &md_last_scan_mode.attr,
4664 &md_mismatches.attr,
4667 &md_sync_speed.attr,
4668 &md_sync_force_parallel.attr,
4669 &md_sync_completed.attr,
4672 &md_suspend_lo.attr,
4673 &md_suspend_hi.attr,
4678 static struct attribute_group md_redundancy_group = {
4680 .attrs = md_redundancy_attrs,
4685 md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4687 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4688 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
4693 spin_lock(&all_mddevs_lock);
4694 if (list_empty(&mddev->all_mddevs)) {
4695 spin_unlock(&all_mddevs_lock);
4699 spin_unlock(&all_mddevs_lock);
4701 rv = mddev_lock(mddev);
4703 rv = entry->show(mddev, page);
4704 mddev_unlock(mddev);
4711 md_attr_store(struct kobject *kobj, struct attribute *attr,
4712 const char *page, size_t length)
4714 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4715 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
4720 if (!capable(CAP_SYS_ADMIN))
4722 spin_lock(&all_mddevs_lock);
4723 if (list_empty(&mddev->all_mddevs)) {
4724 spin_unlock(&all_mddevs_lock);
4728 spin_unlock(&all_mddevs_lock);
4729 if (entry->store == new_dev_store)
4730 flush_workqueue(md_misc_wq);
4731 rv = mddev_lock(mddev);
4733 rv = entry->store(mddev, page, length);
4734 mddev_unlock(mddev);
4740 static void md_free(struct kobject *ko)
4742 struct mddev *mddev = container_of(ko, struct mddev, kobj);
4744 if (mddev->sysfs_state)
4745 sysfs_put(mddev->sysfs_state);
4747 if (mddev->gendisk) {
4748 del_gendisk(mddev->gendisk);
4749 put_disk(mddev->gendisk);
4752 blk_cleanup_queue(mddev->queue);
4757 static const struct sysfs_ops md_sysfs_ops = {
4758 .show = md_attr_show,
4759 .store = md_attr_store,
4761 static struct kobj_type md_ktype = {
4763 .sysfs_ops = &md_sysfs_ops,
4764 .default_attrs = md_default_attrs,
4769 static void mddev_delayed_delete(struct work_struct *ws)
4771 struct mddev *mddev = container_of(ws, struct mddev, del_work);
4773 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
4774 kobject_del(&mddev->kobj);
4775 kobject_put(&mddev->kobj);
4778 static int md_alloc(dev_t dev, char *name)
4780 static DEFINE_MUTEX(disks_mutex);
4781 struct mddev *mddev = mddev_find(dev);
4782 struct gendisk *disk;
4791 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4792 shift = partitioned ? MdpMinorShift : 0;
4793 unit = MINOR(mddev->unit) >> shift;
4795 /* wait for any previous instance of this device to be
4796 * completely removed (mddev_delayed_delete).
4798 flush_workqueue(md_misc_wq);
4800 mutex_lock(&disks_mutex);
4806 /* Need to ensure that 'name' is not a duplicate.
4808 struct mddev *mddev2;
4809 spin_lock(&all_mddevs_lock);
4811 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
4812 if (mddev2->gendisk &&
4813 strcmp(mddev2->gendisk->disk_name, name) == 0) {
4814 spin_unlock(&all_mddevs_lock);
4817 spin_unlock(&all_mddevs_lock);
4821 mddev->queue = blk_alloc_queue(GFP_KERNEL);
4824 mddev->queue->queuedata = mddev;
4826 blk_queue_make_request(mddev->queue, md_make_request);
4827 blk_set_stacking_limits(&mddev->queue->limits);
4829 disk = alloc_disk(1 << shift);
4831 blk_cleanup_queue(mddev->queue);
4832 mddev->queue = NULL;
4835 disk->major = MAJOR(mddev->unit);
4836 disk->first_minor = unit << shift;
4838 strcpy(disk->disk_name, name);
4839 else if (partitioned)
4840 sprintf(disk->disk_name, "md_d%d", unit);
4842 sprintf(disk->disk_name, "md%d", unit);
4843 disk->fops = &md_fops;
4844 disk->private_data = mddev;
4845 disk->queue = mddev->queue;
4846 blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA);
4847 /* Allow extended partitions. This makes the
4848 * 'mdp' device redundant, but we can't really
4851 disk->flags |= GENHD_FL_EXT_DEVT;
4852 mddev->gendisk = disk;
4853 /* As soon as we call add_disk(), another thread could get
4854 * through to md_open, so make sure it doesn't get too far
4856 mutex_lock(&mddev->open_mutex);
4859 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
4860 &disk_to_dev(disk)->kobj, "%s", "md");
4862 /* This isn't possible, but as kobject_init_and_add is marked
4863 * __must_check, we must do something with the result
4865 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
4869 if (mddev->kobj.sd &&
4870 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
4871 printk(KERN_DEBUG "pointless warning\n");
4872 mutex_unlock(&mddev->open_mutex);
4874 mutex_unlock(&disks_mutex);
4875 if (!error && mddev->kobj.sd) {
4876 kobject_uevent(&mddev->kobj, KOBJ_ADD);
4877 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
4883 static struct kobject *md_probe(dev_t dev, int *part, void *data)
4885 md_alloc(dev, NULL);
4889 static int add_named_array(const char *val, struct kernel_param *kp)
4891 /* val must be "md_*" where * is not all digits.
4892 * We allocate an array with a large free minor number, and
4893 * set the name to val. val must not already be an active name.
4895 int len = strlen(val);
4896 char buf[DISK_NAME_LEN];
4898 while (len && val[len-1] == '\n')
4900 if (len >= DISK_NAME_LEN)
4902 strlcpy(buf, val, len+1);
4903 if (strncmp(buf, "md_", 3) != 0)
4905 return md_alloc(0, buf);
4908 static void md_safemode_timeout(unsigned long data)
4910 struct mddev *mddev = (struct mddev *) data;
4912 if (!atomic_read(&mddev->writes_pending)) {
4913 mddev->safemode = 1;
4914 if (mddev->external)
4915 sysfs_notify_dirent_safe(mddev->sysfs_state);
4917 md_wakeup_thread(mddev->thread);
4920 static int start_dirty_degraded;
4922 int md_run(struct mddev *mddev)
4925 struct md_rdev *rdev;
4926 struct md_personality *pers;
4928 if (list_empty(&mddev->disks))
4929 /* cannot run an array with no devices.. */
4934 /* Cannot run until previous stop completes properly */
4935 if (mddev->sysfs_active)
4939 * Analyze all RAID superblock(s)
4941 if (!mddev->raid_disks) {
4942 if (!mddev->persistent)
4947 if (mddev->level != LEVEL_NONE)
4948 request_module("md-level-%d", mddev->level);
4949 else if (mddev->clevel[0])
4950 request_module("md-%s", mddev->clevel);
4953 * Drop all container device buffers, from now on
4954 * the only valid external interface is through the md
4957 rdev_for_each(rdev, mddev) {
4958 if (test_bit(Faulty, &rdev->flags))
4960 sync_blockdev(rdev->bdev);
4961 invalidate_bdev(rdev->bdev);
4963 /* perform some consistency tests on the device.
4964 * We don't want the data to overlap the metadata,
4965 * Internal Bitmap issues have been handled elsewhere.
4967 if (rdev->meta_bdev) {
4968 /* Nothing to check */;
4969 } else if (rdev->data_offset < rdev->sb_start) {
4970 if (mddev->dev_sectors &&
4971 rdev->data_offset + mddev->dev_sectors
4973 printk("md: %s: data overlaps metadata\n",
4978 if (rdev->sb_start + rdev->sb_size/512
4979 > rdev->data_offset) {
4980 printk("md: %s: metadata overlaps data\n",
4985 sysfs_notify_dirent_safe(rdev->sysfs_state);
4988 if (mddev->bio_set == NULL)
4989 mddev->bio_set = bioset_create(BIO_POOL_SIZE, 0);
4991 spin_lock(&pers_lock);
4992 pers = find_pers(mddev->level, mddev->clevel);
4993 if (!pers || !try_module_get(pers->owner)) {
4994 spin_unlock(&pers_lock);
4995 if (mddev->level != LEVEL_NONE)
4996 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
4999 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
5004 spin_unlock(&pers_lock);
5005 if (mddev->level != pers->level) {
5006 mddev->level = pers->level;
5007 mddev->new_level = pers->level;
5009 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
5011 if (mddev->reshape_position != MaxSector &&
5012 pers->start_reshape == NULL) {
5013 /* This personality cannot handle reshaping... */
5015 module_put(pers->owner);
5019 if (pers->sync_request) {
5020 /* Warn if this is a potentially silly
5023 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
5024 struct md_rdev *rdev2;
5027 rdev_for_each(rdev, mddev)
5028 rdev_for_each(rdev2, mddev) {
5030 rdev->bdev->bd_contains ==
5031 rdev2->bdev->bd_contains) {
5033 "%s: WARNING: %s appears to be"
5034 " on the same physical disk as"
5037 bdevname(rdev->bdev,b),
5038 bdevname(rdev2->bdev,b2));
5045 "True protection against single-disk"
5046 " failure might be compromised.\n");
5049 mddev->recovery = 0;
5050 /* may be over-ridden by personality */
5051 mddev->resync_max_sectors = mddev->dev_sectors;
5053 mddev->ok_start_degraded = start_dirty_degraded;
5055 if (start_readonly && mddev->ro == 0)
5056 mddev->ro = 2; /* read-only, but switch on first write */
5058 err = mddev->pers->run(mddev);
5060 printk(KERN_ERR "md: pers->run() failed ...\n");
5061 else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
5062 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
5063 " but 'external_size' not in effect?\n", __func__);
5065 "md: invalid array_size %llu > default size %llu\n",
5066 (unsigned long long)mddev->array_sectors / 2,
5067 (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
5069 mddev->pers->stop(mddev);
5071 if (err == 0 && mddev->pers->sync_request &&
5072 (mddev->bitmap_info.file || mddev->bitmap_info.offset)) {
5073 err = bitmap_create(mddev);
5075 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
5076 mdname(mddev), err);
5077 mddev->pers->stop(mddev);
5081 module_put(mddev->pers->owner);
5083 bitmap_destroy(mddev);
5086 if (mddev->pers->sync_request) {
5087 if (mddev->kobj.sd &&
5088 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
5090 "md: cannot register extra attributes for %s\n",
5092 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
5093 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
5096 atomic_set(&mddev->writes_pending,0);
5097 atomic_set(&mddev->max_corr_read_errors,
5098 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
5099 mddev->safemode = 0;
5100 mddev->safemode_timer.function = md_safemode_timeout;
5101 mddev->safemode_timer.data = (unsigned long) mddev;
5102 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
5106 rdev_for_each(rdev, mddev)
5107 if (rdev->raid_disk >= 0)
5108 if (sysfs_link_rdev(mddev, rdev))
5109 /* failure here is OK */;
5111 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5113 if (mddev->flags & MD_UPDATE_SB_FLAGS)
5114 md_update_sb(mddev, 0);
5116 md_new_event(mddev);
5117 sysfs_notify_dirent_safe(mddev->sysfs_state);
5118 sysfs_notify_dirent_safe(mddev->sysfs_action);
5119 sysfs_notify(&mddev->kobj, NULL, "degraded");
5122 EXPORT_SYMBOL_GPL(md_run);
5124 static int do_md_run(struct mddev *mddev)
5128 err = md_run(mddev);
5131 err = bitmap_load(mddev);
5133 bitmap_destroy(mddev);
5137 md_wakeup_thread(mddev->thread);
5138 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
5140 set_capacity(mddev->gendisk, mddev->array_sectors);
5141 revalidate_disk(mddev->gendisk);
5143 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
5148 static int restart_array(struct mddev *mddev)
5150 struct gendisk *disk = mddev->gendisk;
5152 /* Complain if it has no devices */
5153 if (list_empty(&mddev->disks))
5159 mddev->safemode = 0;
5161 set_disk_ro(disk, 0);
5162 printk(KERN_INFO "md: %s switched to read-write mode.\n",
5164 /* Kick recovery or resync if necessary */
5165 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5166 md_wakeup_thread(mddev->thread);
5167 md_wakeup_thread(mddev->sync_thread);
5168 sysfs_notify_dirent_safe(mddev->sysfs_state);
5172 /* similar to deny_write_access, but accounts for our holding a reference
5173 * to the file ourselves */
5174 static int deny_bitmap_write_access(struct file * file)
5176 struct inode *inode = file->f_mapping->host;
5178 spin_lock(&inode->i_lock);
5179 if (atomic_read(&inode->i_writecount) > 1) {
5180 spin_unlock(&inode->i_lock);
5183 atomic_set(&inode->i_writecount, -1);
5184 spin_unlock(&inode->i_lock);
5189 void restore_bitmap_write_access(struct file *file)
5191 struct inode *inode = file->f_mapping->host;
5193 spin_lock(&inode->i_lock);
5194 atomic_set(&inode->i_writecount, 1);
5195 spin_unlock(&inode->i_lock);
5198 static void md_clean(struct mddev *mddev)
5200 mddev->array_sectors = 0;
5201 mddev->external_size = 0;
5202 mddev->dev_sectors = 0;
5203 mddev->raid_disks = 0;
5204 mddev->recovery_cp = 0;
5205 mddev->resync_min = 0;
5206 mddev->resync_max = MaxSector;
5207 mddev->reshape_position = MaxSector;
5208 mddev->external = 0;
5209 mddev->persistent = 0;
5210 mddev->level = LEVEL_NONE;
5211 mddev->clevel[0] = 0;
5214 mddev->metadata_type[0] = 0;
5215 mddev->chunk_sectors = 0;
5216 mddev->ctime = mddev->utime = 0;
5218 mddev->max_disks = 0;
5220 mddev->can_decrease_events = 0;
5221 mddev->delta_disks = 0;
5222 mddev->reshape_backwards = 0;
5223 mddev->new_level = LEVEL_NONE;
5224 mddev->new_layout = 0;
5225 mddev->new_chunk_sectors = 0;
5226 mddev->curr_resync = 0;
5227 atomic64_set(&mddev->resync_mismatches, 0);
5228 mddev->suspend_lo = mddev->suspend_hi = 0;
5229 mddev->sync_speed_min = mddev->sync_speed_max = 0;
5230 mddev->recovery = 0;
5233 mddev->degraded = 0;
5234 mddev->safemode = 0;
5235 mddev->merge_check_needed = 0;
5236 mddev->bitmap_info.offset = 0;
5237 mddev->bitmap_info.default_offset = 0;
5238 mddev->bitmap_info.default_space = 0;
5239 mddev->bitmap_info.chunksize = 0;
5240 mddev->bitmap_info.daemon_sleep = 0;
5241 mddev->bitmap_info.max_write_behind = 0;
5244 static void __md_stop_writes(struct mddev *mddev)
5246 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5247 if (mddev->sync_thread) {
5248 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5249 md_reap_sync_thread(mddev);
5252 del_timer_sync(&mddev->safemode_timer);
5254 bitmap_flush(mddev);
5255 md_super_wait(mddev);
5257 if (mddev->ro == 0 &&
5258 (!mddev->in_sync || (mddev->flags & MD_UPDATE_SB_FLAGS))) {
5259 /* mark array as shutdown cleanly */
5261 md_update_sb(mddev, 1);
5265 void md_stop_writes(struct mddev *mddev)
5267 mddev_lock_nointr(mddev);
5268 __md_stop_writes(mddev);
5269 mddev_unlock(mddev);
5271 EXPORT_SYMBOL_GPL(md_stop_writes);
5273 static void __md_stop(struct mddev *mddev)
5276 mddev->pers->stop(mddev);
5277 if (mddev->pers->sync_request && mddev->to_remove == NULL)
5278 mddev->to_remove = &md_redundancy_group;
5279 module_put(mddev->pers->owner);
5281 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5284 void md_stop(struct mddev *mddev)
5286 /* stop the array and free an attached data structures.
5287 * This is called from dm-raid
5290 bitmap_destroy(mddev);
5292 bioset_free(mddev->bio_set);
5295 EXPORT_SYMBOL_GPL(md_stop);
5297 static int md_set_readonly(struct mddev *mddev, struct block_device *bdev)
5302 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5304 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5305 md_wakeup_thread(mddev->thread);
5307 if (mddev->sync_thread) {
5308 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5309 /* Thread might be blocked waiting for metadata update
5310 * which will now never happen */
5311 wake_up_process(mddev->sync_thread->tsk);
5313 mddev_unlock(mddev);
5314 wait_event(resync_wait, mddev->sync_thread == NULL);
5315 mddev_lock_nointr(mddev);
5317 mutex_lock(&mddev->open_mutex);
5318 if (atomic_read(&mddev->openers) > !!bdev ||
5319 mddev->sync_thread ||
5320 (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) {
5321 printk("md: %s still in use.\n",mdname(mddev));
5323 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5324 md_wakeup_thread(mddev->thread);
5330 __md_stop_writes(mddev);
5336 set_disk_ro(mddev->gendisk, 1);
5337 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5338 sysfs_notify_dirent_safe(mddev->sysfs_state);
5342 mutex_unlock(&mddev->open_mutex);
5347 * 0 - completely stop and dis-assemble array
5348 * 2 - stop but do not disassemble array
5350 static int do_md_stop(struct mddev * mddev, int mode,
5351 struct block_device *bdev)
5353 struct gendisk *disk = mddev->gendisk;
5354 struct md_rdev *rdev;
5357 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5359 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5360 md_wakeup_thread(mddev->thread);
5362 if (mddev->sync_thread) {
5363 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5364 /* Thread might be blocked waiting for metadata update
5365 * which will now never happen */
5366 wake_up_process(mddev->sync_thread->tsk);
5368 mddev_unlock(mddev);
5369 wait_event(resync_wait, mddev->sync_thread == NULL);
5370 mddev_lock_nointr(mddev);
5372 mutex_lock(&mddev->open_mutex);
5373 if (atomic_read(&mddev->openers) > !!bdev ||
5374 mddev->sysfs_active ||
5375 mddev->sync_thread ||
5376 (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) {
5377 printk("md: %s still in use.\n",mdname(mddev));
5378 mutex_unlock(&mddev->open_mutex);
5380 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5381 md_wakeup_thread(mddev->thread);
5387 set_disk_ro(disk, 0);
5389 __md_stop_writes(mddev);
5391 mddev->queue->merge_bvec_fn = NULL;
5392 mddev->queue->backing_dev_info.congested_fn = NULL;
5394 /* tell userspace to handle 'inactive' */
5395 sysfs_notify_dirent_safe(mddev->sysfs_state);
5397 rdev_for_each(rdev, mddev)
5398 if (rdev->raid_disk >= 0)
5399 sysfs_unlink_rdev(mddev, rdev);
5401 set_capacity(disk, 0);
5402 mutex_unlock(&mddev->open_mutex);
5404 revalidate_disk(disk);
5409 mutex_unlock(&mddev->open_mutex);
5411 * Free resources if final stop
5414 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
5416 bitmap_destroy(mddev);
5417 if (mddev->bitmap_info.file) {
5418 restore_bitmap_write_access(mddev->bitmap_info.file);
5419 fput(mddev->bitmap_info.file);
5420 mddev->bitmap_info.file = NULL;
5422 mddev->bitmap_info.offset = 0;
5424 export_array(mddev);
5427 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
5428 if (mddev->hold_active == UNTIL_STOP)
5429 mddev->hold_active = 0;
5431 blk_integrity_unregister(disk);
5432 md_new_event(mddev);
5433 sysfs_notify_dirent_safe(mddev->sysfs_state);
5438 static void autorun_array(struct mddev *mddev)
5440 struct md_rdev *rdev;
5443 if (list_empty(&mddev->disks))
5446 printk(KERN_INFO "md: running: ");
5448 rdev_for_each(rdev, mddev) {
5449 char b[BDEVNAME_SIZE];
5450 printk("<%s>", bdevname(rdev->bdev,b));
5454 err = do_md_run(mddev);
5456 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
5457 do_md_stop(mddev, 0, NULL);
5462 * lets try to run arrays based on all disks that have arrived
5463 * until now. (those are in pending_raid_disks)
5465 * the method: pick the first pending disk, collect all disks with
5466 * the same UUID, remove all from the pending list and put them into
5467 * the 'same_array' list. Then order this list based on superblock
5468 * update time (freshest comes first), kick out 'old' disks and
5469 * compare superblocks. If everything's fine then run it.
5471 * If "unit" is allocated, then bump its reference count
5473 static void autorun_devices(int part)
5475 struct md_rdev *rdev0, *rdev, *tmp;
5476 struct mddev *mddev;
5477 char b[BDEVNAME_SIZE];
5479 printk(KERN_INFO "md: autorun ...\n");
5480 while (!list_empty(&pending_raid_disks)) {
5483 LIST_HEAD(candidates);
5484 rdev0 = list_entry(pending_raid_disks.next,
5485 struct md_rdev, same_set);
5487 printk(KERN_INFO "md: considering %s ...\n",
5488 bdevname(rdev0->bdev,b));
5489 INIT_LIST_HEAD(&candidates);
5490 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
5491 if (super_90_load(rdev, rdev0, 0) >= 0) {
5492 printk(KERN_INFO "md: adding %s ...\n",
5493 bdevname(rdev->bdev,b));
5494 list_move(&rdev->same_set, &candidates);
5497 * now we have a set of devices, with all of them having
5498 * mostly sane superblocks. It's time to allocate the
5502 dev = MKDEV(mdp_major,
5503 rdev0->preferred_minor << MdpMinorShift);
5504 unit = MINOR(dev) >> MdpMinorShift;
5506 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
5509 if (rdev0->preferred_minor != unit) {
5510 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
5511 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
5515 md_probe(dev, NULL, NULL);
5516 mddev = mddev_find(dev);
5517 if (!mddev || !mddev->gendisk) {
5521 "md: cannot allocate memory for md drive.\n");
5524 if (mddev_lock(mddev))
5525 printk(KERN_WARNING "md: %s locked, cannot run\n",
5527 else if (mddev->raid_disks || mddev->major_version
5528 || !list_empty(&mddev->disks)) {
5530 "md: %s already running, cannot run %s\n",
5531 mdname(mddev), bdevname(rdev0->bdev,b));
5532 mddev_unlock(mddev);
5534 printk(KERN_INFO "md: created %s\n", mdname(mddev));
5535 mddev->persistent = 1;
5536 rdev_for_each_list(rdev, tmp, &candidates) {
5537 list_del_init(&rdev->same_set);
5538 if (bind_rdev_to_array(rdev, mddev))
5541 autorun_array(mddev);
5542 mddev_unlock(mddev);
5544 /* on success, candidates will be empty, on error
5547 rdev_for_each_list(rdev, tmp, &candidates) {
5548 list_del_init(&rdev->same_set);
5553 printk(KERN_INFO "md: ... autorun DONE.\n");
5555 #endif /* !MODULE */
5557 static int get_version(void __user * arg)
5561 ver.major = MD_MAJOR_VERSION;
5562 ver.minor = MD_MINOR_VERSION;
5563 ver.patchlevel = MD_PATCHLEVEL_VERSION;
5565 if (copy_to_user(arg, &ver, sizeof(ver)))
5571 static int get_array_info(struct mddev * mddev, void __user * arg)
5573 mdu_array_info_t info;
5574 int nr,working,insync,failed,spare;
5575 struct md_rdev *rdev;
5577 nr = working = insync = failed = spare = 0;
5579 rdev_for_each_rcu(rdev, mddev) {
5581 if (test_bit(Faulty, &rdev->flags))
5585 if (test_bit(In_sync, &rdev->flags))
5593 info.major_version = mddev->major_version;
5594 info.minor_version = mddev->minor_version;
5595 info.patch_version = MD_PATCHLEVEL_VERSION;
5596 info.ctime = mddev->ctime;
5597 info.level = mddev->level;
5598 info.size = mddev->dev_sectors / 2;
5599 if (info.size != mddev->dev_sectors / 2) /* overflow */
5602 info.raid_disks = mddev->raid_disks;
5603 info.md_minor = mddev->md_minor;
5604 info.not_persistent= !mddev->persistent;
5606 info.utime = mddev->utime;
5609 info.state = (1<<MD_SB_CLEAN);
5610 if (mddev->bitmap && mddev->bitmap_info.offset)
5611 info.state = (1<<MD_SB_BITMAP_PRESENT);
5612 info.active_disks = insync;
5613 info.working_disks = working;
5614 info.failed_disks = failed;
5615 info.spare_disks = spare;
5617 info.layout = mddev->layout;
5618 info.chunk_size = mddev->chunk_sectors << 9;
5620 if (copy_to_user(arg, &info, sizeof(info)))
5626 static int get_bitmap_file(struct mddev * mddev, void __user * arg)
5628 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
5629 char *ptr, *buf = NULL;
5632 file = kmalloc(sizeof(*file), GFP_NOIO);
5637 /* bitmap disabled, zero the first byte and copy out */
5638 if (!mddev->bitmap || !mddev->bitmap->storage.file) {
5639 file->pathname[0] = '\0';
5643 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
5647 ptr = d_path(&mddev->bitmap->storage.file->f_path,
5648 buf, sizeof(file->pathname));
5652 strcpy(file->pathname, ptr);
5656 if (copy_to_user(arg, file, sizeof(*file)))
5664 static int get_disk_info(struct mddev * mddev, void __user * arg)
5666 mdu_disk_info_t info;
5667 struct md_rdev *rdev;
5669 if (copy_from_user(&info, arg, sizeof(info)))
5673 rdev = find_rdev_nr_rcu(mddev, info.number);
5675 info.major = MAJOR(rdev->bdev->bd_dev);
5676 info.minor = MINOR(rdev->bdev->bd_dev);
5677 info.raid_disk = rdev->raid_disk;
5679 if (test_bit(Faulty, &rdev->flags))
5680 info.state |= (1<<MD_DISK_FAULTY);
5681 else if (test_bit(In_sync, &rdev->flags)) {
5682 info.state |= (1<<MD_DISK_ACTIVE);
5683 info.state |= (1<<MD_DISK_SYNC);
5685 if (test_bit(WriteMostly, &rdev->flags))
5686 info.state |= (1<<MD_DISK_WRITEMOSTLY);
5688 info.major = info.minor = 0;
5689 info.raid_disk = -1;
5690 info.state = (1<<MD_DISK_REMOVED);
5694 if (copy_to_user(arg, &info, sizeof(info)))
5700 static int add_new_disk(struct mddev * mddev, mdu_disk_info_t *info)
5702 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
5703 struct md_rdev *rdev;
5704 dev_t dev = MKDEV(info->major,info->minor);
5706 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
5709 if (!mddev->raid_disks) {
5711 /* expecting a device which has a superblock */
5712 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
5715 "md: md_import_device returned %ld\n",
5717 return PTR_ERR(rdev);
5719 if (!list_empty(&mddev->disks)) {
5720 struct md_rdev *rdev0
5721 = list_entry(mddev->disks.next,
5722 struct md_rdev, same_set);
5723 err = super_types[mddev->major_version]
5724 .load_super(rdev, rdev0, mddev->minor_version);
5727 "md: %s has different UUID to %s\n",
5728 bdevname(rdev->bdev,b),
5729 bdevname(rdev0->bdev,b2));
5734 err = bind_rdev_to_array(rdev, mddev);
5741 * add_new_disk can be used once the array is assembled
5742 * to add "hot spares". They must already have a superblock
5747 if (!mddev->pers->hot_add_disk) {
5749 "%s: personality does not support diskops!\n",
5753 if (mddev->persistent)
5754 rdev = md_import_device(dev, mddev->major_version,
5755 mddev->minor_version);
5757 rdev = md_import_device(dev, -1, -1);
5760 "md: md_import_device returned %ld\n",
5762 return PTR_ERR(rdev);
5764 /* set saved_raid_disk if appropriate */
5765 if (!mddev->persistent) {
5766 if (info->state & (1<<MD_DISK_SYNC) &&
5767 info->raid_disk < mddev->raid_disks) {
5768 rdev->raid_disk = info->raid_disk;
5769 set_bit(In_sync, &rdev->flags);
5770 clear_bit(Bitmap_sync, &rdev->flags);
5772 rdev->raid_disk = -1;
5774 super_types[mddev->major_version].
5775 validate_super(mddev, rdev);
5776 if ((info->state & (1<<MD_DISK_SYNC)) &&
5777 rdev->raid_disk != info->raid_disk) {
5778 /* This was a hot-add request, but events doesn't
5779 * match, so reject it.
5785 if (test_bit(In_sync, &rdev->flags))
5786 rdev->saved_raid_disk = rdev->raid_disk;
5788 rdev->saved_raid_disk = -1;
5790 clear_bit(In_sync, &rdev->flags); /* just to be sure */
5791 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5792 set_bit(WriteMostly, &rdev->flags);
5794 clear_bit(WriteMostly, &rdev->flags);
5796 rdev->raid_disk = -1;
5797 err = bind_rdev_to_array(rdev, mddev);
5798 if (!err && !mddev->pers->hot_remove_disk) {
5799 /* If there is hot_add_disk but no hot_remove_disk
5800 * then added disks for geometry changes,
5801 * and should be added immediately.
5803 super_types[mddev->major_version].
5804 validate_super(mddev, rdev);
5805 err = mddev->pers->hot_add_disk(mddev, rdev);
5807 unbind_rdev_from_array(rdev);
5812 sysfs_notify_dirent_safe(rdev->sysfs_state);
5814 set_bit(MD_CHANGE_DEVS, &mddev->flags);
5815 if (mddev->degraded)
5816 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
5817 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5819 md_new_event(mddev);
5820 md_wakeup_thread(mddev->thread);
5824 /* otherwise, add_new_disk is only allowed
5825 * for major_version==0 superblocks
5827 if (mddev->major_version != 0) {
5828 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
5833 if (!(info->state & (1<<MD_DISK_FAULTY))) {
5835 rdev = md_import_device(dev, -1, 0);
5838 "md: error, md_import_device() returned %ld\n",
5840 return PTR_ERR(rdev);
5842 rdev->desc_nr = info->number;
5843 if (info->raid_disk < mddev->raid_disks)
5844 rdev->raid_disk = info->raid_disk;
5846 rdev->raid_disk = -1;
5848 if (rdev->raid_disk < mddev->raid_disks)
5849 if (info->state & (1<<MD_DISK_SYNC))
5850 set_bit(In_sync, &rdev->flags);
5852 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5853 set_bit(WriteMostly, &rdev->flags);
5855 if (!mddev->persistent) {
5856 printk(KERN_INFO "md: nonpersistent superblock ...\n");
5857 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
5859 rdev->sb_start = calc_dev_sboffset(rdev);
5860 rdev->sectors = rdev->sb_start;
5862 err = bind_rdev_to_array(rdev, mddev);
5872 static int hot_remove_disk(struct mddev * mddev, dev_t dev)
5874 char b[BDEVNAME_SIZE];
5875 struct md_rdev *rdev;
5877 rdev = find_rdev(mddev, dev);
5881 clear_bit(Blocked, &rdev->flags);
5882 remove_and_add_spares(mddev, rdev);
5884 if (rdev->raid_disk >= 0)
5887 kick_rdev_from_array(rdev);
5888 md_update_sb(mddev, 1);
5889 md_new_event(mddev);
5893 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
5894 bdevname(rdev->bdev,b), mdname(mddev));
5898 static int hot_add_disk(struct mddev * mddev, dev_t dev)
5900 char b[BDEVNAME_SIZE];
5902 struct md_rdev *rdev;
5907 if (mddev->major_version != 0) {
5908 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
5909 " version-0 superblocks.\n",
5913 if (!mddev->pers->hot_add_disk) {
5915 "%s: personality does not support diskops!\n",
5920 rdev = md_import_device(dev, -1, 0);
5923 "md: error, md_import_device() returned %ld\n",
5928 if (mddev->persistent)
5929 rdev->sb_start = calc_dev_sboffset(rdev);
5931 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
5933 rdev->sectors = rdev->sb_start;
5935 if (test_bit(Faulty, &rdev->flags)) {
5937 "md: can not hot-add faulty %s disk to %s!\n",
5938 bdevname(rdev->bdev,b), mdname(mddev));
5942 clear_bit(In_sync, &rdev->flags);
5944 rdev->saved_raid_disk = -1;
5945 err = bind_rdev_to_array(rdev, mddev);
5950 * The rest should better be atomic, we can have disk failures
5951 * noticed in interrupt contexts ...
5954 rdev->raid_disk = -1;
5956 md_update_sb(mddev, 1);
5959 * Kick recovery, maybe this spare has to be added to the
5960 * array immediately.
5962 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5963 md_wakeup_thread(mddev->thread);
5964 md_new_event(mddev);
5972 static int set_bitmap_file(struct mddev *mddev, int fd)
5977 if (!mddev->pers->quiesce)
5979 if (mddev->recovery || mddev->sync_thread)
5981 /* we should be able to change the bitmap.. */
5987 return -EEXIST; /* cannot add when bitmap is present */
5988 mddev->bitmap_info.file = fget(fd);
5990 if (mddev->bitmap_info.file == NULL) {
5991 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
5996 err = deny_bitmap_write_access(mddev->bitmap_info.file);
5998 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
6000 fput(mddev->bitmap_info.file);
6001 mddev->bitmap_info.file = NULL;
6004 mddev->bitmap_info.offset = 0; /* file overrides offset */
6005 } else if (mddev->bitmap == NULL)
6006 return -ENOENT; /* cannot remove what isn't there */
6009 mddev->pers->quiesce(mddev, 1);
6011 err = bitmap_create(mddev);
6013 err = bitmap_load(mddev);
6015 if (fd < 0 || err) {
6016 bitmap_destroy(mddev);
6017 fd = -1; /* make sure to put the file */
6019 mddev->pers->quiesce(mddev, 0);
6022 if (mddev->bitmap_info.file) {
6023 restore_bitmap_write_access(mddev->bitmap_info.file);
6024 fput(mddev->bitmap_info.file);
6026 mddev->bitmap_info.file = NULL;
6033 * set_array_info is used two different ways
6034 * The original usage is when creating a new array.
6035 * In this usage, raid_disks is > 0 and it together with
6036 * level, size, not_persistent,layout,chunksize determine the
6037 * shape of the array.
6038 * This will always create an array with a type-0.90.0 superblock.
6039 * The newer usage is when assembling an array.
6040 * In this case raid_disks will be 0, and the major_version field is
6041 * use to determine which style super-blocks are to be found on the devices.
6042 * The minor and patch _version numbers are also kept incase the
6043 * super_block handler wishes to interpret them.
6045 static int set_array_info(struct mddev * mddev, mdu_array_info_t *info)
6048 if (info->raid_disks == 0) {
6049 /* just setting version number for superblock loading */
6050 if (info->major_version < 0 ||
6051 info->major_version >= ARRAY_SIZE(super_types) ||
6052 super_types[info->major_version].name == NULL) {
6053 /* maybe try to auto-load a module? */
6055 "md: superblock version %d not known\n",
6056 info->major_version);
6059 mddev->major_version = info->major_version;
6060 mddev->minor_version = info->minor_version;
6061 mddev->patch_version = info->patch_version;
6062 mddev->persistent = !info->not_persistent;
6063 /* ensure mddev_put doesn't delete this now that there
6064 * is some minimal configuration.
6066 mddev->ctime = get_seconds();
6069 mddev->major_version = MD_MAJOR_VERSION;
6070 mddev->minor_version = MD_MINOR_VERSION;
6071 mddev->patch_version = MD_PATCHLEVEL_VERSION;
6072 mddev->ctime = get_seconds();
6074 mddev->level = info->level;
6075 mddev->clevel[0] = 0;
6076 mddev->dev_sectors = 2 * (sector_t)info->size;
6077 mddev->raid_disks = info->raid_disks;
6078 /* don't set md_minor, it is determined by which /dev/md* was
6081 if (info->state & (1<<MD_SB_CLEAN))
6082 mddev->recovery_cp = MaxSector;
6084 mddev->recovery_cp = 0;
6085 mddev->persistent = ! info->not_persistent;
6086 mddev->external = 0;
6088 mddev->layout = info->layout;
6089 mddev->chunk_sectors = info->chunk_size >> 9;
6091 mddev->max_disks = MD_SB_DISKS;
6093 if (mddev->persistent)
6095 set_bit(MD_CHANGE_DEVS, &mddev->flags);
6097 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
6098 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
6099 mddev->bitmap_info.offset = 0;
6101 mddev->reshape_position = MaxSector;
6104 * Generate a 128 bit UUID
6106 get_random_bytes(mddev->uuid, 16);
6108 mddev->new_level = mddev->level;
6109 mddev->new_chunk_sectors = mddev->chunk_sectors;
6110 mddev->new_layout = mddev->layout;
6111 mddev->delta_disks = 0;
6112 mddev->reshape_backwards = 0;
6117 void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors)
6119 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
6121 if (mddev->external_size)
6124 mddev->array_sectors = array_sectors;
6126 EXPORT_SYMBOL(md_set_array_sectors);
6128 static int update_size(struct mddev *mddev, sector_t num_sectors)
6130 struct md_rdev *rdev;
6132 int fit = (num_sectors == 0);
6134 if (mddev->pers->resize == NULL)
6136 /* The "num_sectors" is the number of sectors of each device that
6137 * is used. This can only make sense for arrays with redundancy.
6138 * linear and raid0 always use whatever space is available. We can only
6139 * consider changing this number if no resync or reconstruction is
6140 * happening, and if the new size is acceptable. It must fit before the
6141 * sb_start or, if that is <data_offset, it must fit before the size
6142 * of each device. If num_sectors is zero, we find the largest size
6145 if (mddev->sync_thread)
6148 rdev_for_each(rdev, mddev) {
6149 sector_t avail = rdev->sectors;
6151 if (fit && (num_sectors == 0 || num_sectors > avail))
6152 num_sectors = avail;
6153 if (avail < num_sectors)
6156 rv = mddev->pers->resize(mddev, num_sectors);
6158 revalidate_disk(mddev->gendisk);
6162 static int update_raid_disks(struct mddev *mddev, int raid_disks)
6165 struct md_rdev *rdev;
6166 /* change the number of raid disks */
6167 if (mddev->pers->check_reshape == NULL)
6169 if (raid_disks <= 0 ||
6170 (mddev->max_disks && raid_disks >= mddev->max_disks))
6172 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
6175 rdev_for_each(rdev, mddev) {
6176 if (mddev->raid_disks < raid_disks &&
6177 rdev->data_offset < rdev->new_data_offset)
6179 if (mddev->raid_disks > raid_disks &&
6180 rdev->data_offset > rdev->new_data_offset)
6184 mddev->delta_disks = raid_disks - mddev->raid_disks;
6185 if (mddev->delta_disks < 0)
6186 mddev->reshape_backwards = 1;
6187 else if (mddev->delta_disks > 0)
6188 mddev->reshape_backwards = 0;
6190 rv = mddev->pers->check_reshape(mddev);
6192 mddev->delta_disks = 0;
6193 mddev->reshape_backwards = 0;
6200 * update_array_info is used to change the configuration of an
6202 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
6203 * fields in the info are checked against the array.
6204 * Any differences that cannot be handled will cause an error.
6205 * Normally, only one change can be managed at a time.
6207 static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
6213 /* calculate expected state,ignoring low bits */
6214 if (mddev->bitmap && mddev->bitmap_info.offset)
6215 state |= (1 << MD_SB_BITMAP_PRESENT);
6217 if (mddev->major_version != info->major_version ||
6218 mddev->minor_version != info->minor_version ||
6219 /* mddev->patch_version != info->patch_version || */
6220 mddev->ctime != info->ctime ||
6221 mddev->level != info->level ||
6222 /* mddev->layout != info->layout || */
6223 !mddev->persistent != info->not_persistent||
6224 mddev->chunk_sectors != info->chunk_size >> 9 ||
6225 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
6226 ((state^info->state) & 0xfffffe00)
6229 /* Check there is only one change */
6230 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
6232 if (mddev->raid_disks != info->raid_disks)
6234 if (mddev->layout != info->layout)
6236 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
6243 if (mddev->layout != info->layout) {
6245 * we don't need to do anything at the md level, the
6246 * personality will take care of it all.
6248 if (mddev->pers->check_reshape == NULL)
6251 mddev->new_layout = info->layout;
6252 rv = mddev->pers->check_reshape(mddev);
6254 mddev->new_layout = mddev->layout;
6258 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
6259 rv = update_size(mddev, (sector_t)info->size * 2);
6261 if (mddev->raid_disks != info->raid_disks)
6262 rv = update_raid_disks(mddev, info->raid_disks);
6264 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
6265 if (mddev->pers->quiesce == NULL)
6267 if (mddev->recovery || mddev->sync_thread)
6269 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
6270 /* add the bitmap */
6273 if (mddev->bitmap_info.default_offset == 0)
6275 mddev->bitmap_info.offset =
6276 mddev->bitmap_info.default_offset;
6277 mddev->bitmap_info.space =
6278 mddev->bitmap_info.default_space;
6279 mddev->pers->quiesce(mddev, 1);
6280 rv = bitmap_create(mddev);
6282 rv = bitmap_load(mddev);
6284 bitmap_destroy(mddev);
6285 mddev->pers->quiesce(mddev, 0);
6287 /* remove the bitmap */
6290 if (mddev->bitmap->storage.file)
6292 mddev->pers->quiesce(mddev, 1);
6293 bitmap_destroy(mddev);
6294 mddev->pers->quiesce(mddev, 0);
6295 mddev->bitmap_info.offset = 0;
6298 md_update_sb(mddev, 1);
6302 static int set_disk_faulty(struct mddev *mddev, dev_t dev)
6304 struct md_rdev *rdev;
6307 if (mddev->pers == NULL)
6311 rdev = find_rdev_rcu(mddev, dev);
6315 md_error(mddev, rdev);
6316 if (!test_bit(Faulty, &rdev->flags))
6324 * We have a problem here : there is no easy way to give a CHS
6325 * virtual geometry. We currently pretend that we have a 2 heads
6326 * 4 sectors (with a BIG number of cylinders...). This drives
6327 * dosfs just mad... ;-)
6329 static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
6331 struct mddev *mddev = bdev->bd_disk->private_data;
6335 geo->cylinders = mddev->array_sectors / 8;
6339 static int md_ioctl(struct block_device *bdev, fmode_t mode,
6340 unsigned int cmd, unsigned long arg)
6343 void __user *argp = (void __user *)arg;
6344 struct mddev *mddev = NULL;
6349 case GET_ARRAY_INFO:
6353 if (!capable(CAP_SYS_ADMIN))
6358 * Commands dealing with the RAID driver but not any
6363 err = get_version(argp);
6366 case PRINT_RAID_DEBUG:
6374 autostart_arrays(arg);
6381 * Commands creating/starting a new array:
6384 mddev = bdev->bd_disk->private_data;
6391 /* Some actions do not requires the mutex */
6393 case GET_ARRAY_INFO:
6394 if (!mddev->raid_disks && !mddev->external)
6397 err = get_array_info(mddev, argp);
6401 if (!mddev->raid_disks && !mddev->external)
6404 err = get_disk_info(mddev, argp);
6407 case SET_DISK_FAULTY:
6408 err = set_disk_faulty(mddev, new_decode_dev(arg));
6412 if (cmd == ADD_NEW_DISK)
6413 /* need to ensure md_delayed_delete() has completed */
6414 flush_workqueue(md_misc_wq);
6416 if (cmd == HOT_REMOVE_DISK)
6417 /* need to ensure recovery thread has run */
6418 wait_event_interruptible_timeout(mddev->sb_wait,
6419 !test_bit(MD_RECOVERY_NEEDED,
6421 msecs_to_jiffies(5000));
6422 if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) {
6423 /* Need to flush page cache, and ensure no-one else opens
6426 mutex_lock(&mddev->open_mutex);
6427 if (atomic_read(&mddev->openers) > 1) {
6428 mutex_unlock(&mddev->open_mutex);
6432 set_bit(MD_STILL_CLOSED, &mddev->flags);
6433 mutex_unlock(&mddev->open_mutex);
6434 sync_blockdev(bdev);
6436 err = mddev_lock(mddev);
6439 "md: ioctl lock interrupted, reason %d, cmd %d\n",
6444 if (cmd == SET_ARRAY_INFO) {
6445 mdu_array_info_t info;
6447 memset(&info, 0, sizeof(info));
6448 else if (copy_from_user(&info, argp, sizeof(info))) {
6453 err = update_array_info(mddev, &info);
6455 printk(KERN_WARNING "md: couldn't update"
6456 " array info. %d\n", err);
6461 if (!list_empty(&mddev->disks)) {
6463 "md: array %s already has disks!\n",
6468 if (mddev->raid_disks) {
6470 "md: array %s already initialised!\n",
6475 err = set_array_info(mddev, &info);
6477 printk(KERN_WARNING "md: couldn't set"
6478 " array info. %d\n", err);
6485 * Commands querying/configuring an existing array:
6487 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
6488 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
6489 if ((!mddev->raid_disks && !mddev->external)
6490 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
6491 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
6492 && cmd != GET_BITMAP_FILE) {
6498 * Commands even a read-only array can execute:
6501 case GET_BITMAP_FILE:
6502 err = get_bitmap_file(mddev, argp);
6505 case RESTART_ARRAY_RW:
6506 err = restart_array(mddev);
6510 err = do_md_stop(mddev, 0, bdev);
6514 err = md_set_readonly(mddev, bdev);
6517 case HOT_REMOVE_DISK:
6518 err = hot_remove_disk(mddev, new_decode_dev(arg));
6522 /* We can support ADD_NEW_DISK on read-only arrays
6523 * on if we are re-adding a preexisting device.
6524 * So require mddev->pers and MD_DISK_SYNC.
6527 mdu_disk_info_t info;
6528 if (copy_from_user(&info, argp, sizeof(info)))
6530 else if (!(info.state & (1<<MD_DISK_SYNC)))
6531 /* Need to clear read-only for this */
6534 err = add_new_disk(mddev, &info);
6540 if (get_user(ro, (int __user *)(arg))) {
6546 /* if the bdev is going readonly the value of mddev->ro
6547 * does not matter, no writes are coming
6552 /* are we are already prepared for writes? */
6556 /* transitioning to readauto need only happen for
6557 * arrays that call md_write_start
6560 err = restart_array(mddev);
6563 set_disk_ro(mddev->gendisk, 0);
6570 * The remaining ioctls are changing the state of the
6571 * superblock, so we do not allow them on read-only arrays.
6572 * However non-MD ioctls (e.g. get-size) will still come through
6573 * here and hit the 'default' below, so only disallow
6574 * 'md' ioctls, and switch to rw mode if started auto-readonly.
6576 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
6577 if (mddev->ro == 2) {
6579 sysfs_notify_dirent_safe(mddev->sysfs_state);
6580 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6581 /* mddev_unlock will wake thread */
6582 /* If a device failed while we were read-only, we
6583 * need to make sure the metadata is updated now.
6585 if (test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
6586 mddev_unlock(mddev);
6587 wait_event(mddev->sb_wait,
6588 !test_bit(MD_CHANGE_DEVS, &mddev->flags) &&
6589 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
6590 mddev_lock_nointr(mddev);
6601 mdu_disk_info_t info;
6602 if (copy_from_user(&info, argp, sizeof(info)))
6605 err = add_new_disk(mddev, &info);
6610 err = hot_add_disk(mddev, new_decode_dev(arg));
6614 err = do_md_run(mddev);
6617 case SET_BITMAP_FILE:
6618 err = set_bitmap_file(mddev, (int)arg);
6628 if (mddev->hold_active == UNTIL_IOCTL &&
6630 mddev->hold_active = 0;
6631 mddev_unlock(mddev);
6640 #ifdef CONFIG_COMPAT
6641 static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
6642 unsigned int cmd, unsigned long arg)
6645 case HOT_REMOVE_DISK:
6647 case SET_DISK_FAULTY:
6648 case SET_BITMAP_FILE:
6649 /* These take in integer arg, do not convert */
6652 arg = (unsigned long)compat_ptr(arg);
6656 return md_ioctl(bdev, mode, cmd, arg);
6658 #endif /* CONFIG_COMPAT */
6660 static int md_open(struct block_device *bdev, fmode_t mode)
6663 * Succeed if we can lock the mddev, which confirms that
6664 * it isn't being stopped right now.
6666 struct mddev *mddev = mddev_find(bdev->bd_dev);
6672 if (mddev->gendisk != bdev->bd_disk) {
6673 /* we are racing with mddev_put which is discarding this
6677 /* Wait until bdev->bd_disk is definitely gone */
6678 flush_workqueue(md_misc_wq);
6679 /* Then retry the open from the top */
6680 return -ERESTARTSYS;
6682 BUG_ON(mddev != bdev->bd_disk->private_data);
6684 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
6688 atomic_inc(&mddev->openers);
6689 clear_bit(MD_STILL_CLOSED, &mddev->flags);
6690 mutex_unlock(&mddev->open_mutex);
6692 check_disk_change(bdev);
6697 static void md_release(struct gendisk *disk, fmode_t mode)
6699 struct mddev *mddev = disk->private_data;
6702 atomic_dec(&mddev->openers);
6706 static int md_media_changed(struct gendisk *disk)
6708 struct mddev *mddev = disk->private_data;
6710 return mddev->changed;
6713 static int md_revalidate(struct gendisk *disk)
6715 struct mddev *mddev = disk->private_data;
6720 static const struct block_device_operations md_fops =
6722 .owner = THIS_MODULE,
6724 .release = md_release,
6726 #ifdef CONFIG_COMPAT
6727 .compat_ioctl = md_compat_ioctl,
6729 .getgeo = md_getgeo,
6730 .media_changed = md_media_changed,
6731 .revalidate_disk= md_revalidate,
6734 static int md_thread(void * arg)
6736 struct md_thread *thread = arg;
6739 * md_thread is a 'system-thread', it's priority should be very
6740 * high. We avoid resource deadlocks individually in each
6741 * raid personality. (RAID5 does preallocation) We also use RR and
6742 * the very same RT priority as kswapd, thus we will never get
6743 * into a priority inversion deadlock.
6745 * we definitely have to have equal or higher priority than
6746 * bdflush, otherwise bdflush will deadlock if there are too
6747 * many dirty RAID5 blocks.
6750 allow_signal(SIGKILL);
6751 while (!kthread_should_stop()) {
6753 /* We need to wait INTERRUPTIBLE so that
6754 * we don't add to the load-average.
6755 * That means we need to be sure no signals are
6758 if (signal_pending(current))
6759 flush_signals(current);
6761 wait_event_interruptible_timeout
6763 test_bit(THREAD_WAKEUP, &thread->flags)
6764 || kthread_should_stop(),
6767 clear_bit(THREAD_WAKEUP, &thread->flags);
6768 if (!kthread_should_stop())
6769 thread->run(thread);
6775 void md_wakeup_thread(struct md_thread *thread)
6778 pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm);
6779 set_bit(THREAD_WAKEUP, &thread->flags);
6780 wake_up(&thread->wqueue);
6784 struct md_thread *md_register_thread(void (*run) (struct md_thread *),
6785 struct mddev *mddev, const char *name)
6787 struct md_thread *thread;
6789 thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL);
6793 init_waitqueue_head(&thread->wqueue);
6796 thread->mddev = mddev;
6797 thread->timeout = MAX_SCHEDULE_TIMEOUT;
6798 thread->tsk = kthread_run(md_thread, thread,
6800 mdname(thread->mddev),
6802 if (IS_ERR(thread->tsk)) {
6809 void md_unregister_thread(struct md_thread **threadp)
6811 struct md_thread *thread = *threadp;
6814 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
6815 /* Locking ensures that mddev_unlock does not wake_up a
6816 * non-existent thread
6818 spin_lock(&pers_lock);
6820 spin_unlock(&pers_lock);
6822 kthread_stop(thread->tsk);
6826 void md_error(struct mddev *mddev, struct md_rdev *rdev)
6833 if (!rdev || test_bit(Faulty, &rdev->flags))
6836 if (!mddev->pers || !mddev->pers->error_handler)
6838 mddev->pers->error_handler(mddev,rdev);
6839 if (mddev->degraded)
6840 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
6841 sysfs_notify_dirent_safe(rdev->sysfs_state);
6842 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6843 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6844 md_wakeup_thread(mddev->thread);
6845 if (mddev->event_work.func)
6846 queue_work(md_misc_wq, &mddev->event_work);
6847 md_new_event_inintr(mddev);
6850 /* seq_file implementation /proc/mdstat */
6852 static void status_unused(struct seq_file *seq)
6855 struct md_rdev *rdev;
6857 seq_printf(seq, "unused devices: ");
6859 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
6860 char b[BDEVNAME_SIZE];
6862 seq_printf(seq, "%s ",
6863 bdevname(rdev->bdev,b));
6866 seq_printf(seq, "<none>");
6868 seq_printf(seq, "\n");
6872 static void status_resync(struct seq_file *seq, struct mddev * mddev)
6874 sector_t max_sectors, resync, res;
6875 unsigned long dt, db;
6878 unsigned int per_milli;
6880 if (mddev->curr_resync <= 3)
6883 resync = mddev->curr_resync
6884 - atomic_read(&mddev->recovery_active);
6886 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
6887 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
6888 max_sectors = mddev->resync_max_sectors;
6890 max_sectors = mddev->dev_sectors;
6893 * Should not happen.
6899 /* Pick 'scale' such that (resync>>scale)*1000 will fit
6900 * in a sector_t, and (max_sectors>>scale) will fit in a
6901 * u32, as those are the requirements for sector_div.
6902 * Thus 'scale' must be at least 10
6905 if (sizeof(sector_t) > sizeof(unsigned long)) {
6906 while ( max_sectors/2 > (1ULL<<(scale+32)))
6909 res = (resync>>scale)*1000;
6910 sector_div(res, (u32)((max_sectors>>scale)+1));
6914 int i, x = per_milli/50, y = 20-x;
6915 seq_printf(seq, "[");
6916 for (i = 0; i < x; i++)
6917 seq_printf(seq, "=");
6918 seq_printf(seq, ">");
6919 for (i = 0; i < y; i++)
6920 seq_printf(seq, ".");
6921 seq_printf(seq, "] ");
6923 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
6924 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
6926 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
6928 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
6929 "resync" : "recovery"))),
6930 per_milli/10, per_milli % 10,
6931 (unsigned long long) resync/2,
6932 (unsigned long long) max_sectors/2);
6935 * dt: time from mark until now
6936 * db: blocks written from mark until now
6937 * rt: remaining time
6939 * rt is a sector_t, so could be 32bit or 64bit.
6940 * So we divide before multiply in case it is 32bit and close
6942 * We scale the divisor (db) by 32 to avoid losing precision
6943 * near the end of resync when the number of remaining sectors
6945 * We then divide rt by 32 after multiplying by db to compensate.
6946 * The '+1' avoids division by zero if db is very small.
6948 dt = ((jiffies - mddev->resync_mark) / HZ);
6950 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
6951 - mddev->resync_mark_cnt;
6953 rt = max_sectors - resync; /* number of remaining sectors */
6954 sector_div(rt, db/32+1);
6958 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
6959 ((unsigned long)rt % 60)/6);
6961 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
6964 static void *md_seq_start(struct seq_file *seq, loff_t *pos)
6966 struct list_head *tmp;
6968 struct mddev *mddev;
6976 spin_lock(&all_mddevs_lock);
6977 list_for_each(tmp,&all_mddevs)
6979 mddev = list_entry(tmp, struct mddev, all_mddevs);
6981 spin_unlock(&all_mddevs_lock);
6984 spin_unlock(&all_mddevs_lock);
6986 return (void*)2;/* tail */
6990 static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
6992 struct list_head *tmp;
6993 struct mddev *next_mddev, *mddev = v;
6999 spin_lock(&all_mddevs_lock);
7001 tmp = all_mddevs.next;
7003 tmp = mddev->all_mddevs.next;
7004 if (tmp != &all_mddevs)
7005 next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs));
7007 next_mddev = (void*)2;
7010 spin_unlock(&all_mddevs_lock);
7018 static void md_seq_stop(struct seq_file *seq, void *v)
7020 struct mddev *mddev = v;
7022 if (mddev && v != (void*)1 && v != (void*)2)
7026 static int md_seq_show(struct seq_file *seq, void *v)
7028 struct mddev *mddev = v;
7030 struct md_rdev *rdev;
7032 if (v == (void*)1) {
7033 struct md_personality *pers;
7034 seq_printf(seq, "Personalities : ");
7035 spin_lock(&pers_lock);
7036 list_for_each_entry(pers, &pers_list, list)
7037 seq_printf(seq, "[%s] ", pers->name);
7039 spin_unlock(&pers_lock);
7040 seq_printf(seq, "\n");
7041 seq->poll_event = atomic_read(&md_event_count);
7044 if (v == (void*)2) {
7049 if (mddev_lock(mddev) < 0)
7052 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
7053 seq_printf(seq, "%s : %sactive", mdname(mddev),
7054 mddev->pers ? "" : "in");
7057 seq_printf(seq, " (read-only)");
7059 seq_printf(seq, " (auto-read-only)");
7060 seq_printf(seq, " %s", mddev->pers->name);
7064 rdev_for_each(rdev, mddev) {
7065 char b[BDEVNAME_SIZE];
7066 seq_printf(seq, " %s[%d]",
7067 bdevname(rdev->bdev,b), rdev->desc_nr);
7068 if (test_bit(WriteMostly, &rdev->flags))
7069 seq_printf(seq, "(W)");
7070 if (test_bit(Faulty, &rdev->flags)) {
7071 seq_printf(seq, "(F)");
7074 if (rdev->raid_disk < 0)
7075 seq_printf(seq, "(S)"); /* spare */
7076 if (test_bit(Replacement, &rdev->flags))
7077 seq_printf(seq, "(R)");
7078 sectors += rdev->sectors;
7081 if (!list_empty(&mddev->disks)) {
7083 seq_printf(seq, "\n %llu blocks",
7084 (unsigned long long)
7085 mddev->array_sectors / 2);
7087 seq_printf(seq, "\n %llu blocks",
7088 (unsigned long long)sectors / 2);
7090 if (mddev->persistent) {
7091 if (mddev->major_version != 0 ||
7092 mddev->minor_version != 90) {
7093 seq_printf(seq," super %d.%d",
7094 mddev->major_version,
7095 mddev->minor_version);
7097 } else if (mddev->external)
7098 seq_printf(seq, " super external:%s",
7099 mddev->metadata_type);
7101 seq_printf(seq, " super non-persistent");
7104 mddev->pers->status(seq, mddev);
7105 seq_printf(seq, "\n ");
7106 if (mddev->pers->sync_request) {
7107 if (mddev->curr_resync > 2) {
7108 status_resync(seq, mddev);
7109 seq_printf(seq, "\n ");
7110 } else if (mddev->curr_resync >= 1)
7111 seq_printf(seq, "\tresync=DELAYED\n ");
7112 else if (mddev->recovery_cp < MaxSector)
7113 seq_printf(seq, "\tresync=PENDING\n ");
7116 seq_printf(seq, "\n ");
7118 bitmap_status(seq, mddev->bitmap);
7120 seq_printf(seq, "\n");
7122 mddev_unlock(mddev);
7127 static const struct seq_operations md_seq_ops = {
7128 .start = md_seq_start,
7129 .next = md_seq_next,
7130 .stop = md_seq_stop,
7131 .show = md_seq_show,
7134 static int md_seq_open(struct inode *inode, struct file *file)
7136 struct seq_file *seq;
7139 error = seq_open(file, &md_seq_ops);
7143 seq = file->private_data;
7144 seq->poll_event = atomic_read(&md_event_count);
7148 static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
7150 struct seq_file *seq = filp->private_data;
7153 poll_wait(filp, &md_event_waiters, wait);
7155 /* always allow read */
7156 mask = POLLIN | POLLRDNORM;
7158 if (seq->poll_event != atomic_read(&md_event_count))
7159 mask |= POLLERR | POLLPRI;
7163 static const struct file_operations md_seq_fops = {
7164 .owner = THIS_MODULE,
7165 .open = md_seq_open,
7167 .llseek = seq_lseek,
7168 .release = seq_release_private,
7169 .poll = mdstat_poll,
7172 int register_md_personality(struct md_personality *p)
7174 spin_lock(&pers_lock);
7175 list_add_tail(&p->list, &pers_list);
7176 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
7177 spin_unlock(&pers_lock);
7181 int unregister_md_personality(struct md_personality *p)
7183 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
7184 spin_lock(&pers_lock);
7185 list_del_init(&p->list);
7186 spin_unlock(&pers_lock);
7190 static int is_mddev_idle(struct mddev *mddev, int init)
7192 struct md_rdev * rdev;
7198 rdev_for_each_rcu(rdev, mddev) {
7199 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
7200 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
7201 (int)part_stat_read(&disk->part0, sectors[1]) -
7202 atomic_read(&disk->sync_io);
7203 /* sync IO will cause sync_io to increase before the disk_stats
7204 * as sync_io is counted when a request starts, and
7205 * disk_stats is counted when it completes.
7206 * So resync activity will cause curr_events to be smaller than
7207 * when there was no such activity.
7208 * non-sync IO will cause disk_stat to increase without
7209 * increasing sync_io so curr_events will (eventually)
7210 * be larger than it was before. Once it becomes
7211 * substantially larger, the test below will cause
7212 * the array to appear non-idle, and resync will slow
7214 * If there is a lot of outstanding resync activity when
7215 * we set last_event to curr_events, then all that activity
7216 * completing might cause the array to appear non-idle
7217 * and resync will be slowed down even though there might
7218 * not have been non-resync activity. This will only
7219 * happen once though. 'last_events' will soon reflect
7220 * the state where there is little or no outstanding
7221 * resync requests, and further resync activity will
7222 * always make curr_events less than last_events.
7225 if (init || curr_events - rdev->last_events > 64) {
7226 rdev->last_events = curr_events;
7234 void md_done_sync(struct mddev *mddev, int blocks, int ok)
7236 /* another "blocks" (512byte) blocks have been synced */
7237 atomic_sub(blocks, &mddev->recovery_active);
7238 wake_up(&mddev->recovery_wait);
7240 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7241 set_bit(MD_RECOVERY_ERROR, &mddev->recovery);
7242 md_wakeup_thread(mddev->thread);
7243 // stop recovery, signal do_sync ....
7248 /* md_write_start(mddev, bi)
7249 * If we need to update some array metadata (e.g. 'active' flag
7250 * in superblock) before writing, schedule a superblock update
7251 * and wait for it to complete.
7253 void md_write_start(struct mddev *mddev, struct bio *bi)
7256 if (bio_data_dir(bi) != WRITE)
7259 BUG_ON(mddev->ro == 1);
7260 if (mddev->ro == 2) {
7261 /* need to switch to read/write */
7263 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7264 md_wakeup_thread(mddev->thread);
7265 md_wakeup_thread(mddev->sync_thread);
7268 atomic_inc(&mddev->writes_pending);
7269 if (mddev->safemode == 1)
7270 mddev->safemode = 0;
7271 if (mddev->in_sync) {
7272 spin_lock_irq(&mddev->write_lock);
7273 if (mddev->in_sync) {
7275 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
7276 set_bit(MD_CHANGE_PENDING, &mddev->flags);
7277 md_wakeup_thread(mddev->thread);
7280 spin_unlock_irq(&mddev->write_lock);
7283 sysfs_notify_dirent_safe(mddev->sysfs_state);
7284 wait_event(mddev->sb_wait,
7285 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
7288 void md_write_end(struct mddev *mddev)
7290 if (atomic_dec_and_test(&mddev->writes_pending)) {
7291 if (mddev->safemode == 2)
7292 md_wakeup_thread(mddev->thread);
7293 else if (mddev->safemode_delay)
7294 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
7298 /* md_allow_write(mddev)
7299 * Calling this ensures that the array is marked 'active' so that writes
7300 * may proceed without blocking. It is important to call this before
7301 * attempting a GFP_KERNEL allocation while holding the mddev lock.
7302 * Must be called with mddev_lock held.
7304 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
7305 * is dropped, so return -EAGAIN after notifying userspace.
7307 int md_allow_write(struct mddev *mddev)
7313 if (!mddev->pers->sync_request)
7316 spin_lock_irq(&mddev->write_lock);
7317 if (mddev->in_sync) {
7319 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
7320 set_bit(MD_CHANGE_PENDING, &mddev->flags);
7321 if (mddev->safemode_delay &&
7322 mddev->safemode == 0)
7323 mddev->safemode = 1;
7324 spin_unlock_irq(&mddev->write_lock);
7325 md_update_sb(mddev, 0);
7326 sysfs_notify_dirent_safe(mddev->sysfs_state);
7328 spin_unlock_irq(&mddev->write_lock);
7330 if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
7335 EXPORT_SYMBOL_GPL(md_allow_write);
7337 #define SYNC_MARKS 10
7338 #define SYNC_MARK_STEP (3*HZ)
7339 #define UPDATE_FREQUENCY (5*60*HZ)
7340 void md_do_sync(struct md_thread *thread)
7342 struct mddev *mddev = thread->mddev;
7343 struct mddev *mddev2;
7344 unsigned int currspeed = 0,
7346 sector_t max_sectors,j, io_sectors;
7347 unsigned long mark[SYNC_MARKS];
7348 unsigned long update_time;
7349 sector_t mark_cnt[SYNC_MARKS];
7351 struct list_head *tmp;
7352 sector_t last_check;
7354 struct md_rdev *rdev;
7355 char *desc, *action = NULL;
7356 struct blk_plug plug;
7358 /* just incase thread restarts... */
7359 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7361 if (mddev->ro) /* never try to sync a read-only array */
7364 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
7365 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) {
7366 desc = "data-check";
7368 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
7369 desc = "requested-resync";
7373 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
7378 mddev->last_sync_action = action ?: desc;
7380 /* we overload curr_resync somewhat here.
7381 * 0 == not engaged in resync at all
7382 * 2 == checking that there is no conflict with another sync
7383 * 1 == like 2, but have yielded to allow conflicting resync to
7385 * other == active in resync - this many blocks
7387 * Before starting a resync we must have set curr_resync to
7388 * 2, and then checked that every "conflicting" array has curr_resync
7389 * less than ours. When we find one that is the same or higher
7390 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
7391 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
7392 * This will mean we have to start checking from the beginning again.
7397 mddev->curr_resync = 2;
7400 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7402 for_each_mddev(mddev2, tmp) {
7403 if (mddev2 == mddev)
7405 if (!mddev->parallel_resync
7406 && mddev2->curr_resync
7407 && match_mddev_units(mddev, mddev2)) {
7409 if (mddev < mddev2 && mddev->curr_resync == 2) {
7410 /* arbitrarily yield */
7411 mddev->curr_resync = 1;
7412 wake_up(&resync_wait);
7414 if (mddev > mddev2 && mddev->curr_resync == 1)
7415 /* no need to wait here, we can wait the next
7416 * time 'round when curr_resync == 2
7419 /* We need to wait 'interruptible' so as not to
7420 * contribute to the load average, and not to
7421 * be caught by 'softlockup'
7423 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
7424 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
7425 mddev2->curr_resync >= mddev->curr_resync) {
7426 printk(KERN_INFO "md: delaying %s of %s"
7427 " until %s has finished (they"
7428 " share one or more physical units)\n",
7429 desc, mdname(mddev), mdname(mddev2));
7431 if (signal_pending(current))
7432 flush_signals(current);
7434 finish_wait(&resync_wait, &wq);
7437 finish_wait(&resync_wait, &wq);
7440 } while (mddev->curr_resync < 2);
7443 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
7444 /* resync follows the size requested by the personality,
7445 * which defaults to physical size, but can be virtual size
7447 max_sectors = mddev->resync_max_sectors;
7448 atomic64_set(&mddev->resync_mismatches, 0);
7449 /* we don't use the checkpoint if there's a bitmap */
7450 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7451 j = mddev->resync_min;
7452 else if (!mddev->bitmap)
7453 j = mddev->recovery_cp;
7455 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
7456 max_sectors = mddev->resync_max_sectors;
7458 /* recovery follows the physical size of devices */
7459 max_sectors = mddev->dev_sectors;
7462 rdev_for_each_rcu(rdev, mddev)
7463 if (rdev->raid_disk >= 0 &&
7464 !test_bit(Faulty, &rdev->flags) &&
7465 !test_bit(In_sync, &rdev->flags) &&
7466 rdev->recovery_offset < j)
7467 j = rdev->recovery_offset;
7471 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
7472 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
7473 " %d KB/sec/disk.\n", speed_min(mddev));
7474 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
7475 "(but not more than %d KB/sec) for %s.\n",
7476 speed_max(mddev), desc);
7478 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
7481 for (m = 0; m < SYNC_MARKS; m++) {
7483 mark_cnt[m] = io_sectors;
7486 mddev->resync_mark = mark[last_mark];
7487 mddev->resync_mark_cnt = mark_cnt[last_mark];
7490 * Tune reconstruction:
7492 window = 32*(PAGE_SIZE/512);
7493 printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n",
7494 window/2, (unsigned long long)max_sectors/2);
7496 atomic_set(&mddev->recovery_active, 0);
7501 "md: resuming %s of %s from checkpoint.\n",
7502 desc, mdname(mddev));
7503 mddev->curr_resync = j;
7505 mddev->curr_resync = 3; /* no longer delayed */
7506 mddev->curr_resync_completed = j;
7507 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
7508 md_new_event(mddev);
7509 update_time = jiffies;
7511 blk_start_plug(&plug);
7512 while (j < max_sectors) {
7517 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7518 ((mddev->curr_resync > mddev->curr_resync_completed &&
7519 (mddev->curr_resync - mddev->curr_resync_completed)
7520 > (max_sectors >> 4)) ||
7521 time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) ||
7522 (j - mddev->curr_resync_completed)*2
7523 >= mddev->resync_max - mddev->curr_resync_completed
7525 /* time to update curr_resync_completed */
7526 wait_event(mddev->recovery_wait,
7527 atomic_read(&mddev->recovery_active) == 0);
7528 mddev->curr_resync_completed = j;
7529 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
7530 j > mddev->recovery_cp)
7531 mddev->recovery_cp = j;
7532 update_time = jiffies;
7533 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
7534 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
7537 while (j >= mddev->resync_max &&
7538 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7539 /* As this condition is controlled by user-space,
7540 * we can block indefinitely, so use '_interruptible'
7541 * to avoid triggering warnings.
7543 flush_signals(current); /* just in case */
7544 wait_event_interruptible(mddev->recovery_wait,
7545 mddev->resync_max > j
7546 || test_bit(MD_RECOVERY_INTR,
7550 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7553 sectors = mddev->pers->sync_request(mddev, j, &skipped,
7554 currspeed < speed_min(mddev));
7556 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7560 if (!skipped) { /* actual IO requested */
7561 io_sectors += sectors;
7562 atomic_add(sectors, &mddev->recovery_active);
7565 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7570 mddev->curr_resync = j;
7571 mddev->curr_mark_cnt = io_sectors;
7572 if (last_check == 0)
7573 /* this is the earliest that rebuild will be
7574 * visible in /proc/mdstat
7576 md_new_event(mddev);
7578 if (last_check + window > io_sectors || j == max_sectors)
7581 last_check = io_sectors;
7583 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
7585 int next = (last_mark+1) % SYNC_MARKS;
7587 mddev->resync_mark = mark[next];
7588 mddev->resync_mark_cnt = mark_cnt[next];
7589 mark[next] = jiffies;
7590 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
7594 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7598 * this loop exits only if either when we are slower than
7599 * the 'hard' speed limit, or the system was IO-idle for
7601 * the system might be non-idle CPU-wise, but we only care
7602 * about not overloading the IO subsystem. (things like an
7603 * e2fsck being done on the RAID array should execute fast)
7607 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
7608 /((jiffies-mddev->resync_mark)/HZ +1) +1;
7610 if (currspeed > speed_min(mddev)) {
7611 if ((currspeed > speed_max(mddev)) ||
7612 !is_mddev_idle(mddev, 0)) {
7618 printk(KERN_INFO "md: %s: %s %s.\n",mdname(mddev), desc,
7619 test_bit(MD_RECOVERY_INTR, &mddev->recovery)
7620 ? "interrupted" : "done");
7622 * this also signals 'finished resyncing' to md_stop
7624 blk_finish_plug(&plug);
7625 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
7627 /* tell personality that we are finished */
7628 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
7630 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
7631 mddev->curr_resync > 2) {
7632 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
7633 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7634 if (mddev->curr_resync >= mddev->recovery_cp) {
7636 "md: checkpointing %s of %s.\n",
7637 desc, mdname(mddev));
7638 if (test_bit(MD_RECOVERY_ERROR,
7640 mddev->recovery_cp =
7641 mddev->curr_resync_completed;
7643 mddev->recovery_cp =
7647 mddev->recovery_cp = MaxSector;
7649 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7650 mddev->curr_resync = MaxSector;
7652 rdev_for_each_rcu(rdev, mddev)
7653 if (rdev->raid_disk >= 0 &&
7654 mddev->delta_disks >= 0 &&
7655 !test_bit(Faulty, &rdev->flags) &&
7656 !test_bit(In_sync, &rdev->flags) &&
7657 rdev->recovery_offset < mddev->curr_resync)
7658 rdev->recovery_offset = mddev->curr_resync;
7663 set_bit(MD_CHANGE_DEVS, &mddev->flags);
7665 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7666 /* We completed so min/max setting can be forgotten if used. */
7667 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7668 mddev->resync_min = 0;
7669 mddev->resync_max = MaxSector;
7670 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7671 mddev->resync_min = mddev->curr_resync_completed;
7672 mddev->curr_resync = 0;
7673 wake_up(&resync_wait);
7674 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
7675 md_wakeup_thread(mddev->thread);
7678 EXPORT_SYMBOL_GPL(md_do_sync);
7680 static int remove_and_add_spares(struct mddev *mddev,
7681 struct md_rdev *this)
7683 struct md_rdev *rdev;
7687 rdev_for_each(rdev, mddev)
7688 if ((this == NULL || rdev == this) &&
7689 rdev->raid_disk >= 0 &&
7690 !test_bit(Blocked, &rdev->flags) &&
7691 (test_bit(Faulty, &rdev->flags) ||
7692 ! test_bit(In_sync, &rdev->flags)) &&
7693 atomic_read(&rdev->nr_pending)==0) {
7694 if (mddev->pers->hot_remove_disk(
7695 mddev, rdev) == 0) {
7696 sysfs_unlink_rdev(mddev, rdev);
7697 rdev->raid_disk = -1;
7701 if (removed && mddev->kobj.sd)
7702 sysfs_notify(&mddev->kobj, NULL, "degraded");
7707 rdev_for_each(rdev, mddev) {
7708 if (rdev->raid_disk >= 0 &&
7709 !test_bit(In_sync, &rdev->flags) &&
7710 !test_bit(Faulty, &rdev->flags))
7712 if (rdev->raid_disk >= 0)
7714 if (test_bit(Faulty, &rdev->flags))
7717 ! (rdev->saved_raid_disk >= 0 &&
7718 !test_bit(Bitmap_sync, &rdev->flags)))
7721 rdev->recovery_offset = 0;
7723 hot_add_disk(mddev, rdev) == 0) {
7724 if (sysfs_link_rdev(mddev, rdev))
7725 /* failure here is OK */;
7727 md_new_event(mddev);
7728 set_bit(MD_CHANGE_DEVS, &mddev->flags);
7733 set_bit(MD_CHANGE_DEVS, &mddev->flags);
7738 * This routine is regularly called by all per-raid-array threads to
7739 * deal with generic issues like resync and super-block update.
7740 * Raid personalities that don't have a thread (linear/raid0) do not
7741 * need this as they never do any recovery or update the superblock.
7743 * It does not do any resync itself, but rather "forks" off other threads
7744 * to do that as needed.
7745 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
7746 * "->recovery" and create a thread at ->sync_thread.
7747 * When the thread finishes it sets MD_RECOVERY_DONE
7748 * and wakeups up this thread which will reap the thread and finish up.
7749 * This thread also removes any faulty devices (with nr_pending == 0).
7751 * The overall approach is:
7752 * 1/ if the superblock needs updating, update it.
7753 * 2/ If a recovery thread is running, don't do anything else.
7754 * 3/ If recovery has finished, clean up, possibly marking spares active.
7755 * 4/ If there are any faulty devices, remove them.
7756 * 5/ If array is degraded, try to add spares devices
7757 * 6/ If array has spares or is not in-sync, start a resync thread.
7759 void md_check_recovery(struct mddev *mddev)
7761 if (mddev->suspended)
7765 bitmap_daemon_work(mddev);
7767 if (signal_pending(current)) {
7768 if (mddev->pers->sync_request && !mddev->external) {
7769 printk(KERN_INFO "md: %s in immediate safe mode\n",
7771 mddev->safemode = 2;
7773 flush_signals(current);
7776 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
7779 (mddev->flags & MD_UPDATE_SB_FLAGS & ~ (1<<MD_CHANGE_PENDING)) ||
7780 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
7781 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
7782 (mddev->external == 0 && mddev->safemode == 1) ||
7783 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
7784 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
7788 if (mddev_trylock(mddev)) {
7792 /* On a read-only array we can:
7793 * - remove failed devices
7794 * - add already-in_sync devices if the array itself
7796 * As we only add devices that are already in-sync,
7797 * we can activate the spares immediately.
7799 remove_and_add_spares(mddev, NULL);
7800 /* There is no thread, but we need to call
7801 * ->spare_active and clear saved_raid_disk
7803 md_reap_sync_thread(mddev);
7804 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7808 if (!mddev->external) {
7810 spin_lock_irq(&mddev->write_lock);
7811 if (mddev->safemode &&
7812 !atomic_read(&mddev->writes_pending) &&
7814 mddev->recovery_cp == MaxSector) {
7817 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
7819 if (mddev->safemode == 1)
7820 mddev->safemode = 0;
7821 spin_unlock_irq(&mddev->write_lock);
7823 sysfs_notify_dirent_safe(mddev->sysfs_state);
7826 if (mddev->flags & MD_UPDATE_SB_FLAGS)
7827 md_update_sb(mddev, 0);
7829 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
7830 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
7831 /* resync/recovery still happening */
7832 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7835 if (mddev->sync_thread) {
7836 md_reap_sync_thread(mddev);
7839 /* Set RUNNING before clearing NEEDED to avoid
7840 * any transients in the value of "sync_action".
7842 mddev->curr_resync_completed = 0;
7843 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7844 /* Clear some bits that don't mean anything, but
7847 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
7848 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
7850 if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
7851 test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
7853 /* no recovery is running.
7854 * remove any failed drives, then
7855 * add spares if possible.
7856 * Spares are also removed and re-added, to allow
7857 * the personality to fail the re-add.
7860 if (mddev->reshape_position != MaxSector) {
7861 if (mddev->pers->check_reshape == NULL ||
7862 mddev->pers->check_reshape(mddev) != 0)
7863 /* Cannot proceed */
7865 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7866 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
7867 } else if ((spares = remove_and_add_spares(mddev, NULL))) {
7868 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7869 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
7870 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7871 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
7872 } else if (mddev->recovery_cp < MaxSector) {
7873 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7874 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
7875 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
7876 /* nothing to be done ... */
7879 if (mddev->pers->sync_request) {
7881 /* We are adding a device or devices to an array
7882 * which has the bitmap stored on all devices.
7883 * So make sure all bitmap pages get written
7885 bitmap_write_all(mddev->bitmap);
7887 mddev->sync_thread = md_register_thread(md_do_sync,
7890 if (!mddev->sync_thread) {
7891 printk(KERN_ERR "%s: could not start resync"
7894 /* leave the spares where they are, it shouldn't hurt */
7895 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7896 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7897 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7898 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7899 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
7901 md_wakeup_thread(mddev->sync_thread);
7902 sysfs_notify_dirent_safe(mddev->sysfs_action);
7903 md_new_event(mddev);
7906 wake_up(&mddev->sb_wait);
7908 if (!mddev->sync_thread) {
7909 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7910 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
7912 if (mddev->sysfs_action)
7913 sysfs_notify_dirent_safe(mddev->sysfs_action);
7915 mddev_unlock(mddev);
7919 void md_reap_sync_thread(struct mddev *mddev)
7921 struct md_rdev *rdev;
7923 /* resync has finished, collect result */
7924 md_unregister_thread(&mddev->sync_thread);
7925 wake_up(&resync_wait);
7926 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
7927 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
7929 /* activate any spares */
7930 if (mddev->pers->spare_active(mddev)) {
7931 sysfs_notify(&mddev->kobj, NULL,
7933 set_bit(MD_CHANGE_DEVS, &mddev->flags);
7936 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7937 mddev->pers->finish_reshape)
7938 mddev->pers->finish_reshape(mddev);
7940 /* If array is no-longer degraded, then any saved_raid_disk
7941 * information must be scrapped. Also if any device is now
7942 * In_sync we must scrape the saved_raid_disk for that device
7943 * do the superblock for an incrementally recovered device
7946 rdev_for_each(rdev, mddev)
7947 if (!mddev->degraded ||
7948 test_bit(In_sync, &rdev->flags))
7949 rdev->saved_raid_disk = -1;
7951 md_update_sb(mddev, 1);
7952 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7953 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7954 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7955 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7956 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
7957 /* flag recovery needed just to double check */
7958 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7959 sysfs_notify_dirent_safe(mddev->sysfs_action);
7960 md_new_event(mddev);
7961 if (mddev->event_work.func)
7962 queue_work(md_misc_wq, &mddev->event_work);
7965 void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev)
7967 sysfs_notify_dirent_safe(rdev->sysfs_state);
7968 wait_event_timeout(rdev->blocked_wait,
7969 !test_bit(Blocked, &rdev->flags) &&
7970 !test_bit(BlockedBadBlocks, &rdev->flags),
7971 msecs_to_jiffies(5000));
7972 rdev_dec_pending(rdev, mddev);
7974 EXPORT_SYMBOL(md_wait_for_blocked_rdev);
7976 void md_finish_reshape(struct mddev *mddev)
7978 /* called be personality module when reshape completes. */
7979 struct md_rdev *rdev;
7981 rdev_for_each(rdev, mddev) {
7982 if (rdev->data_offset > rdev->new_data_offset)
7983 rdev->sectors += rdev->data_offset - rdev->new_data_offset;
7985 rdev->sectors -= rdev->new_data_offset - rdev->data_offset;
7986 rdev->data_offset = rdev->new_data_offset;
7989 EXPORT_SYMBOL(md_finish_reshape);
7991 /* Bad block management.
7992 * We can record which blocks on each device are 'bad' and so just
7993 * fail those blocks, or that stripe, rather than the whole device.
7994 * Entries in the bad-block table are 64bits wide. This comprises:
7995 * Length of bad-range, in sectors: 0-511 for lengths 1-512
7996 * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes)
7997 * A 'shift' can be set so that larger blocks are tracked and
7998 * consequently larger devices can be covered.
7999 * 'Acknowledged' flag - 1 bit. - the most significant bit.
8001 * Locking of the bad-block table uses a seqlock so md_is_badblock
8002 * might need to retry if it is very unlucky.
8003 * We will sometimes want to check for bad blocks in a bi_end_io function,
8004 * so we use the write_seqlock_irq variant.
8006 * When looking for a bad block we specify a range and want to
8007 * know if any block in the range is bad. So we binary-search
8008 * to the last range that starts at-or-before the given endpoint,
8009 * (or "before the sector after the target range")
8010 * then see if it ends after the given start.
8012 * 0 if there are no known bad blocks in the range
8013 * 1 if there are known bad block which are all acknowledged
8014 * -1 if there are bad blocks which have not yet been acknowledged in metadata.
8015 * plus the start/length of the first bad section we overlap.
8017 int md_is_badblock(struct badblocks *bb, sector_t s, int sectors,
8018 sector_t *first_bad, int *bad_sectors)
8024 sector_t target = s + sectors;
8027 if (bb->shift > 0) {
8028 /* round the start down, and the end up */
8030 target += (1<<bb->shift) - 1;
8031 target >>= bb->shift;
8032 sectors = target - s;
8034 /* 'target' is now the first block after the bad range */
8037 seq = read_seqbegin(&bb->lock);
8042 /* Binary search between lo and hi for 'target'
8043 * i.e. for the last range that starts before 'target'
8045 /* INVARIANT: ranges before 'lo' and at-or-after 'hi'
8046 * are known not to be the last range before target.
8047 * VARIANT: hi-lo is the number of possible
8048 * ranges, and decreases until it reaches 1
8050 while (hi - lo > 1) {
8051 int mid = (lo + hi) / 2;
8052 sector_t a = BB_OFFSET(p[mid]);
8054 /* This could still be the one, earlier ranges
8058 /* This and later ranges are definitely out. */
8061 /* 'lo' might be the last that started before target, but 'hi' isn't */
8063 /* need to check all range that end after 's' to see if
8064 * any are unacknowledged.
8067 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
8068 if (BB_OFFSET(p[lo]) < target) {
8069 /* starts before the end, and finishes after
8070 * the start, so they must overlap
8072 if (rv != -1 && BB_ACK(p[lo]))
8076 *first_bad = BB_OFFSET(p[lo]);
8077 *bad_sectors = BB_LEN(p[lo]);
8083 if (read_seqretry(&bb->lock, seq))
8088 EXPORT_SYMBOL_GPL(md_is_badblock);
8091 * Add a range of bad blocks to the table.
8092 * This might extend the table, or might contract it
8093 * if two adjacent ranges can be merged.
8094 * We binary-search to find the 'insertion' point, then
8095 * decide how best to handle it.
8097 static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
8103 unsigned long flags;
8106 /* badblocks are disabled */
8110 /* round the start down, and the end up */
8111 sector_t next = s + sectors;
8113 next += (1<<bb->shift) - 1;
8118 write_seqlock_irqsave(&bb->lock, flags);
8123 /* Find the last range that starts at-or-before 's' */
8124 while (hi - lo > 1) {
8125 int mid = (lo + hi) / 2;
8126 sector_t a = BB_OFFSET(p[mid]);
8132 if (hi > lo && BB_OFFSET(p[lo]) > s)
8136 /* we found a range that might merge with the start
8139 sector_t a = BB_OFFSET(p[lo]);
8140 sector_t e = a + BB_LEN(p[lo]);
8141 int ack = BB_ACK(p[lo]);
8143 /* Yes, we can merge with a previous range */
8144 if (s == a && s + sectors >= e)
8145 /* new range covers old */
8148 ack = ack && acknowledged;
8150 if (e < s + sectors)
8152 if (e - a <= BB_MAX_LEN) {
8153 p[lo] = BB_MAKE(a, e-a, ack);
8156 /* does not all fit in one range,
8157 * make p[lo] maximal
8159 if (BB_LEN(p[lo]) != BB_MAX_LEN)
8160 p[lo] = BB_MAKE(a, BB_MAX_LEN, ack);
8166 if (sectors && hi < bb->count) {
8167 /* 'hi' points to the first range that starts after 's'.
8168 * Maybe we can merge with the start of that range */
8169 sector_t a = BB_OFFSET(p[hi]);
8170 sector_t e = a + BB_LEN(p[hi]);
8171 int ack = BB_ACK(p[hi]);
8172 if (a <= s + sectors) {
8173 /* merging is possible */
8174 if (e <= s + sectors) {
8179 ack = ack && acknowledged;
8182 if (e - a <= BB_MAX_LEN) {
8183 p[hi] = BB_MAKE(a, e-a, ack);
8186 p[hi] = BB_MAKE(a, BB_MAX_LEN, ack);
8194 if (sectors == 0 && hi < bb->count) {
8195 /* we might be able to combine lo and hi */
8196 /* Note: 's' is at the end of 'lo' */
8197 sector_t a = BB_OFFSET(p[hi]);
8198 int lolen = BB_LEN(p[lo]);
8199 int hilen = BB_LEN(p[hi]);
8200 int newlen = lolen + hilen - (s - a);
8201 if (s >= a && newlen < BB_MAX_LEN) {
8202 /* yes, we can combine them */
8203 int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]);
8204 p[lo] = BB_MAKE(BB_OFFSET(p[lo]), newlen, ack);
8205 memmove(p + hi, p + hi + 1,
8206 (bb->count - hi - 1) * 8);
8211 /* didn't merge (it all).
8212 * Need to add a range just before 'hi' */
8213 if (bb->count >= MD_MAX_BADBLOCKS) {
8214 /* No room for more */
8218 int this_sectors = sectors;
8219 memmove(p + hi + 1, p + hi,
8220 (bb->count - hi) * 8);
8223 if (this_sectors > BB_MAX_LEN)
8224 this_sectors = BB_MAX_LEN;
8225 p[hi] = BB_MAKE(s, this_sectors, acknowledged);
8226 sectors -= this_sectors;
8233 bb->unacked_exist = 1;
8234 write_sequnlock_irqrestore(&bb->lock, flags);
8239 int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
8244 s += rdev->new_data_offset;
8246 s += rdev->data_offset;
8247 rv = md_set_badblocks(&rdev->badblocks,
8250 /* Make sure they get written out promptly */
8251 sysfs_notify_dirent_safe(rdev->sysfs_state);
8252 set_bit(MD_CHANGE_CLEAN, &rdev->mddev->flags);
8253 md_wakeup_thread(rdev->mddev->thread);
8257 EXPORT_SYMBOL_GPL(rdev_set_badblocks);
8260 * Remove a range of bad blocks from the table.
8261 * This may involve extending the table if we spilt a region,
8262 * but it must not fail. So if the table becomes full, we just
8263 * drop the remove request.
8265 static int md_clear_badblocks(struct badblocks *bb, sector_t s, int sectors)
8269 sector_t target = s + sectors;
8272 if (bb->shift > 0) {
8273 /* When clearing we round the start up and the end down.
8274 * This should not matter as the shift should align with
8275 * the block size and no rounding should ever be needed.
8276 * However it is better the think a block is bad when it
8277 * isn't than to think a block is not bad when it is.
8279 s += (1<<bb->shift) - 1;
8281 target >>= bb->shift;
8282 sectors = target - s;
8285 write_seqlock_irq(&bb->lock);
8290 /* Find the last range that starts before 'target' */
8291 while (hi - lo > 1) {
8292 int mid = (lo + hi) / 2;
8293 sector_t a = BB_OFFSET(p[mid]);
8300 /* p[lo] is the last range that could overlap the
8301 * current range. Earlier ranges could also overlap,
8302 * but only this one can overlap the end of the range.
8304 if (BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > target) {
8305 /* Partial overlap, leave the tail of this range */
8306 int ack = BB_ACK(p[lo]);
8307 sector_t a = BB_OFFSET(p[lo]);
8308 sector_t end = a + BB_LEN(p[lo]);
8311 /* we need to split this range */
8312 if (bb->count >= MD_MAX_BADBLOCKS) {
8316 memmove(p+lo+1, p+lo, (bb->count - lo) * 8);
8318 p[lo] = BB_MAKE(a, s-a, ack);
8321 p[lo] = BB_MAKE(target, end - target, ack);
8322 /* there is no longer an overlap */
8327 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
8328 /* This range does overlap */
8329 if (BB_OFFSET(p[lo]) < s) {
8330 /* Keep the early parts of this range. */
8331 int ack = BB_ACK(p[lo]);
8332 sector_t start = BB_OFFSET(p[lo]);
8333 p[lo] = BB_MAKE(start, s - start, ack);
8334 /* now low doesn't overlap, so.. */
8339 /* 'lo' is strictly before, 'hi' is strictly after,
8340 * anything between needs to be discarded
8343 memmove(p+lo+1, p+hi, (bb->count - hi) * 8);
8344 bb->count -= (hi - lo - 1);
8350 write_sequnlock_irq(&bb->lock);
8354 int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
8358 s += rdev->new_data_offset;
8360 s += rdev->data_offset;
8361 return md_clear_badblocks(&rdev->badblocks,
8364 EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
8367 * Acknowledge all bad blocks in a list.
8368 * This only succeeds if ->changed is clear. It is used by
8369 * in-kernel metadata updates
8371 void md_ack_all_badblocks(struct badblocks *bb)
8373 if (bb->page == NULL || bb->changed)
8374 /* no point even trying */
8376 write_seqlock_irq(&bb->lock);
8378 if (bb->changed == 0 && bb->unacked_exist) {
8381 for (i = 0; i < bb->count ; i++) {
8382 if (!BB_ACK(p[i])) {
8383 sector_t start = BB_OFFSET(p[i]);
8384 int len = BB_LEN(p[i]);
8385 p[i] = BB_MAKE(start, len, 1);
8388 bb->unacked_exist = 0;
8390 write_sequnlock_irq(&bb->lock);
8392 EXPORT_SYMBOL_GPL(md_ack_all_badblocks);
8394 /* sysfs access to bad-blocks list.
8395 * We present two files.
8396 * 'bad-blocks' lists sector numbers and lengths of ranges that
8397 * are recorded as bad. The list is truncated to fit within
8398 * the one-page limit of sysfs.
8399 * Writing "sector length" to this file adds an acknowledged
8401 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
8402 * been acknowledged. Writing to this file adds bad blocks
8403 * without acknowledging them. This is largely for testing.
8407 badblocks_show(struct badblocks *bb, char *page, int unack)
8418 seq = read_seqbegin(&bb->lock);
8423 while (len < PAGE_SIZE && i < bb->count) {
8424 sector_t s = BB_OFFSET(p[i]);
8425 unsigned int length = BB_LEN(p[i]);
8426 int ack = BB_ACK(p[i]);
8432 len += snprintf(page+len, PAGE_SIZE-len, "%llu %u\n",
8433 (unsigned long long)s << bb->shift,
8434 length << bb->shift);
8436 if (unack && len == 0)
8437 bb->unacked_exist = 0;
8439 if (read_seqretry(&bb->lock, seq))
8448 badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack)
8450 unsigned long long sector;
8454 /* Allow clearing via sysfs *only* for testing/debugging.
8455 * Normally only a successful write may clear a badblock
8458 if (page[0] == '-') {
8462 #endif /* DO_DEBUG */
8464 switch (sscanf(page, "%llu %d%c", §or, &length, &newline)) {
8466 if (newline != '\n')
8478 md_clear_badblocks(bb, sector, length);
8481 #endif /* DO_DEBUG */
8482 if (md_set_badblocks(bb, sector, length, !unack))
8488 static int md_notify_reboot(struct notifier_block *this,
8489 unsigned long code, void *x)
8491 struct list_head *tmp;
8492 struct mddev *mddev;
8495 for_each_mddev(mddev, tmp) {
8496 if (mddev_trylock(mddev)) {
8498 __md_stop_writes(mddev);
8499 mddev->safemode = 2;
8500 mddev_unlock(mddev);
8505 * certain more exotic SCSI devices are known to be
8506 * volatile wrt too early system reboots. While the
8507 * right place to handle this issue is the given
8508 * driver, we do want to have a safe RAID driver ...
8516 static struct notifier_block md_notifier = {
8517 .notifier_call = md_notify_reboot,
8519 .priority = INT_MAX, /* before any real devices */
8522 static void md_geninit(void)
8524 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
8526 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
8529 static int __init md_init(void)
8533 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
8537 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
8541 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
8544 if ((ret = register_blkdev(0, "mdp")) < 0)
8548 blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE,
8549 md_probe, NULL, NULL);
8550 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
8551 md_probe, NULL, NULL);
8553 register_reboot_notifier(&md_notifier);
8554 raid_table_header = register_sysctl_table(raid_root_table);
8560 unregister_blkdev(MD_MAJOR, "md");
8562 destroy_workqueue(md_misc_wq);
8564 destroy_workqueue(md_wq);
8572 * Searches all registered partitions for autorun RAID arrays
8576 static LIST_HEAD(all_detected_devices);
8577 struct detected_devices_node {
8578 struct list_head list;
8582 void md_autodetect_dev(dev_t dev)
8584 struct detected_devices_node *node_detected_dev;
8586 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
8587 if (node_detected_dev) {
8588 node_detected_dev->dev = dev;
8589 list_add_tail(&node_detected_dev->list, &all_detected_devices);
8591 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
8592 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
8597 static void autostart_arrays(int part)
8599 struct md_rdev *rdev;
8600 struct detected_devices_node *node_detected_dev;
8602 int i_scanned, i_passed;
8607 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
8609 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
8611 node_detected_dev = list_entry(all_detected_devices.next,
8612 struct detected_devices_node, list);
8613 list_del(&node_detected_dev->list);
8614 dev = node_detected_dev->dev;
8615 kfree(node_detected_dev);
8616 rdev = md_import_device(dev,0, 90);
8620 if (test_bit(Faulty, &rdev->flags)) {
8624 set_bit(AutoDetected, &rdev->flags);
8625 list_add(&rdev->same_set, &pending_raid_disks);
8629 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
8630 i_scanned, i_passed);
8632 autorun_devices(part);
8635 #endif /* !MODULE */
8637 static __exit void md_exit(void)
8639 struct mddev *mddev;
8640 struct list_head *tmp;
8642 blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS);
8643 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
8645 unregister_blkdev(MD_MAJOR,"md");
8646 unregister_blkdev(mdp_major, "mdp");
8647 unregister_reboot_notifier(&md_notifier);
8648 unregister_sysctl_table(raid_table_header);
8649 remove_proc_entry("mdstat", NULL);
8650 for_each_mddev(mddev, tmp) {
8651 export_array(mddev);
8652 mddev->hold_active = 0;
8654 destroy_workqueue(md_misc_wq);
8655 destroy_workqueue(md_wq);
8658 subsys_initcall(md_init);
8659 module_exit(md_exit)
8661 static int get_ro(char *buffer, struct kernel_param *kp)
8663 return sprintf(buffer, "%d", start_readonly);
8665 static int set_ro(const char *val, struct kernel_param *kp)
8668 int num = simple_strtoul(val, &e, 10);
8669 if (*val && (*e == '\0' || *e == '\n')) {
8670 start_readonly = num;
8676 module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
8677 module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
8679 module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
8681 EXPORT_SYMBOL(register_md_personality);
8682 EXPORT_SYMBOL(unregister_md_personality);
8683 EXPORT_SYMBOL(md_error);
8684 EXPORT_SYMBOL(md_done_sync);
8685 EXPORT_SYMBOL(md_write_start);
8686 EXPORT_SYMBOL(md_write_end);
8687 EXPORT_SYMBOL(md_register_thread);
8688 EXPORT_SYMBOL(md_unregister_thread);
8689 EXPORT_SYMBOL(md_wakeup_thread);
8690 EXPORT_SYMBOL(md_check_recovery);
8691 EXPORT_SYMBOL(md_reap_sync_thread);
8692 MODULE_LICENSE("GPL");
8693 MODULE_DESCRIPTION("MD RAID framework");
8695 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);