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 rdev->saved_raid_disk = desc->raid_disk;
1177 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1178 /* active but not in sync implies recovery up to
1179 * reshape position. We don't know exactly where
1180 * that is, so set to zero for now */
1181 if (mddev->minor_version >= 91) {
1182 rdev->recovery_offset = 0;
1183 rdev->raid_disk = desc->raid_disk;
1186 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1187 set_bit(WriteMostly, &rdev->flags);
1188 } else /* MULTIPATH are always insync */
1189 set_bit(In_sync, &rdev->flags);
1194 * sync_super for 0.90.0
1196 static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev)
1199 struct md_rdev *rdev2;
1200 int next_spare = mddev->raid_disks;
1203 /* make rdev->sb match mddev data..
1206 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1207 * 3/ any empty disks < next_spare become removed
1209 * disks[0] gets initialised to REMOVED because
1210 * we cannot be sure from other fields if it has
1211 * been initialised or not.
1214 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1216 rdev->sb_size = MD_SB_BYTES;
1218 sb = page_address(rdev->sb_page);
1220 memset(sb, 0, sizeof(*sb));
1222 sb->md_magic = MD_SB_MAGIC;
1223 sb->major_version = mddev->major_version;
1224 sb->patch_version = mddev->patch_version;
1225 sb->gvalid_words = 0; /* ignored */
1226 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1227 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1228 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1229 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1231 sb->ctime = mddev->ctime;
1232 sb->level = mddev->level;
1233 sb->size = mddev->dev_sectors / 2;
1234 sb->raid_disks = mddev->raid_disks;
1235 sb->md_minor = mddev->md_minor;
1236 sb->not_persistent = 0;
1237 sb->utime = mddev->utime;
1239 sb->events_hi = (mddev->events>>32);
1240 sb->events_lo = (u32)mddev->events;
1242 if (mddev->reshape_position == MaxSector)
1243 sb->minor_version = 90;
1245 sb->minor_version = 91;
1246 sb->reshape_position = mddev->reshape_position;
1247 sb->new_level = mddev->new_level;
1248 sb->delta_disks = mddev->delta_disks;
1249 sb->new_layout = mddev->new_layout;
1250 sb->new_chunk = mddev->new_chunk_sectors << 9;
1252 mddev->minor_version = sb->minor_version;
1255 sb->recovery_cp = mddev->recovery_cp;
1256 sb->cp_events_hi = (mddev->events>>32);
1257 sb->cp_events_lo = (u32)mddev->events;
1258 if (mddev->recovery_cp == MaxSector)
1259 sb->state = (1<< MD_SB_CLEAN);
1261 sb->recovery_cp = 0;
1263 sb->layout = mddev->layout;
1264 sb->chunk_size = mddev->chunk_sectors << 9;
1266 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
1267 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1269 sb->disks[0].state = (1<<MD_DISK_REMOVED);
1270 rdev_for_each(rdev2, mddev) {
1273 int is_active = test_bit(In_sync, &rdev2->flags);
1275 if (rdev2->raid_disk >= 0 &&
1276 sb->minor_version >= 91)
1277 /* we have nowhere to store the recovery_offset,
1278 * but if it is not below the reshape_position,
1279 * we can piggy-back on that.
1282 if (rdev2->raid_disk < 0 ||
1283 test_bit(Faulty, &rdev2->flags))
1286 desc_nr = rdev2->raid_disk;
1288 desc_nr = next_spare++;
1289 rdev2->desc_nr = desc_nr;
1290 d = &sb->disks[rdev2->desc_nr];
1292 d->number = rdev2->desc_nr;
1293 d->major = MAJOR(rdev2->bdev->bd_dev);
1294 d->minor = MINOR(rdev2->bdev->bd_dev);
1296 d->raid_disk = rdev2->raid_disk;
1298 d->raid_disk = rdev2->desc_nr; /* compatibility */
1299 if (test_bit(Faulty, &rdev2->flags))
1300 d->state = (1<<MD_DISK_FAULTY);
1301 else if (is_active) {
1302 d->state = (1<<MD_DISK_ACTIVE);
1303 if (test_bit(In_sync, &rdev2->flags))
1304 d->state |= (1<<MD_DISK_SYNC);
1312 if (test_bit(WriteMostly, &rdev2->flags))
1313 d->state |= (1<<MD_DISK_WRITEMOSTLY);
1315 /* now set the "removed" and "faulty" bits on any missing devices */
1316 for (i=0 ; i < mddev->raid_disks ; i++) {
1317 mdp_disk_t *d = &sb->disks[i];
1318 if (d->state == 0 && d->number == 0) {
1321 d->state = (1<<MD_DISK_REMOVED);
1322 d->state |= (1<<MD_DISK_FAULTY);
1326 sb->nr_disks = nr_disks;
1327 sb->active_disks = active;
1328 sb->working_disks = working;
1329 sb->failed_disks = failed;
1330 sb->spare_disks = spare;
1332 sb->this_disk = sb->disks[rdev->desc_nr];
1333 sb->sb_csum = calc_sb_csum(sb);
1337 * rdev_size_change for 0.90.0
1339 static unsigned long long
1340 super_90_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
1342 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
1343 return 0; /* component must fit device */
1344 if (rdev->mddev->bitmap_info.offset)
1345 return 0; /* can't move bitmap */
1346 rdev->sb_start = calc_dev_sboffset(rdev);
1347 if (!num_sectors || num_sectors > rdev->sb_start)
1348 num_sectors = rdev->sb_start;
1349 /* Limit to 4TB as metadata cannot record more than that.
1350 * 4TB == 2^32 KB, or 2*2^32 sectors.
1352 if (num_sectors >= (2ULL << 32) && rdev->mddev->level >= 1)
1353 num_sectors = (2ULL << 32) - 2;
1354 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
1356 md_super_wait(rdev->mddev);
1361 super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset)
1363 /* non-zero offset changes not possible with v0.90 */
1364 return new_offset == 0;
1368 * version 1 superblock
1371 static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
1375 unsigned long long newcsum;
1376 int size = 256 + le32_to_cpu(sb->max_dev)*2;
1377 __le32 *isuper = (__le32*)sb;
1379 disk_csum = sb->sb_csum;
1382 for (; size >= 4; size -= 4)
1383 newcsum += le32_to_cpu(*isuper++);
1386 newcsum += le16_to_cpu(*(__le16*) isuper);
1388 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1389 sb->sb_csum = disk_csum;
1390 return cpu_to_le32(csum);
1393 static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
1395 static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
1397 struct mdp_superblock_1 *sb;
1401 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1405 * Calculate the position of the superblock in 512byte sectors.
1406 * It is always aligned to a 4K boundary and
1407 * depeding on minor_version, it can be:
1408 * 0: At least 8K, but less than 12K, from end of device
1409 * 1: At start of device
1410 * 2: 4K from start of device.
1412 switch(minor_version) {
1414 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
1416 sb_start &= ~(sector_t)(4*2-1);
1427 rdev->sb_start = sb_start;
1429 /* superblock is rarely larger than 1K, but it can be larger,
1430 * and it is safe to read 4k, so we do that
1432 ret = read_disk_sb(rdev, 4096);
1433 if (ret) return ret;
1436 sb = page_address(rdev->sb_page);
1438 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1439 sb->major_version != cpu_to_le32(1) ||
1440 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
1441 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
1442 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
1445 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1446 printk("md: invalid superblock checksum on %s\n",
1447 bdevname(rdev->bdev,b));
1450 if (le64_to_cpu(sb->data_size) < 10) {
1451 printk("md: data_size too small on %s\n",
1452 bdevname(rdev->bdev,b));
1457 memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1])))
1458 /* Some padding is non-zero, might be a new feature */
1461 rdev->preferred_minor = 0xffff;
1462 rdev->data_offset = le64_to_cpu(sb->data_offset);
1463 rdev->new_data_offset = rdev->data_offset;
1464 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) &&
1465 (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET))
1466 rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset);
1467 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
1469 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
1470 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1471 if (rdev->sb_size & bmask)
1472 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1475 && rdev->data_offset < sb_start + (rdev->sb_size/512))
1478 && rdev->new_data_offset < sb_start + (rdev->sb_size/512))
1481 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1484 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1486 if (!rdev->bb_page) {
1487 rdev->bb_page = alloc_page(GFP_KERNEL);
1491 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) &&
1492 rdev->badblocks.count == 0) {
1493 /* need to load the bad block list.
1494 * Currently we limit it to one page.
1500 int sectors = le16_to_cpu(sb->bblog_size);
1501 if (sectors > (PAGE_SIZE / 512))
1503 offset = le32_to_cpu(sb->bblog_offset);
1506 bb_sector = (long long)offset;
1507 if (!sync_page_io(rdev, bb_sector, sectors << 9,
1508 rdev->bb_page, READ, true))
1510 bbp = (u64 *)page_address(rdev->bb_page);
1511 rdev->badblocks.shift = sb->bblog_shift;
1512 for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) {
1513 u64 bb = le64_to_cpu(*bbp);
1514 int count = bb & (0x3ff);
1515 u64 sector = bb >> 10;
1516 sector <<= sb->bblog_shift;
1517 count <<= sb->bblog_shift;
1520 if (md_set_badblocks(&rdev->badblocks,
1521 sector, count, 1) == 0)
1524 } else if (sb->bblog_offset != 0)
1525 rdev->badblocks.shift = 0;
1531 struct mdp_superblock_1 *refsb = page_address(refdev->sb_page);
1533 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1534 sb->level != refsb->level ||
1535 sb->layout != refsb->layout ||
1536 sb->chunksize != refsb->chunksize) {
1537 printk(KERN_WARNING "md: %s has strangely different"
1538 " superblock to %s\n",
1539 bdevname(rdev->bdev,b),
1540 bdevname(refdev->bdev,b2));
1543 ev1 = le64_to_cpu(sb->events);
1544 ev2 = le64_to_cpu(refsb->events);
1551 if (minor_version) {
1552 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9);
1553 sectors -= rdev->data_offset;
1555 sectors = rdev->sb_start;
1556 if (sectors < le64_to_cpu(sb->data_size))
1558 rdev->sectors = le64_to_cpu(sb->data_size);
1562 static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev)
1564 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
1565 __u64 ev1 = le64_to_cpu(sb->events);
1567 rdev->raid_disk = -1;
1568 clear_bit(Faulty, &rdev->flags);
1569 clear_bit(In_sync, &rdev->flags);
1570 clear_bit(Bitmap_sync, &rdev->flags);
1571 clear_bit(WriteMostly, &rdev->flags);
1573 if (mddev->raid_disks == 0) {
1574 mddev->major_version = 1;
1575 mddev->patch_version = 0;
1576 mddev->external = 0;
1577 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
1578 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1579 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1580 mddev->level = le32_to_cpu(sb->level);
1581 mddev->clevel[0] = 0;
1582 mddev->layout = le32_to_cpu(sb->layout);
1583 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
1584 mddev->dev_sectors = le64_to_cpu(sb->size);
1585 mddev->events = ev1;
1586 mddev->bitmap_info.offset = 0;
1587 mddev->bitmap_info.space = 0;
1588 /* Default location for bitmap is 1K after superblock
1589 * using 3K - total of 4K
1591 mddev->bitmap_info.default_offset = 1024 >> 9;
1592 mddev->bitmap_info.default_space = (4096-1024) >> 9;
1593 mddev->reshape_backwards = 0;
1595 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1596 memcpy(mddev->uuid, sb->set_uuid, 16);
1598 mddev->max_disks = (4096-256)/2;
1600 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
1601 mddev->bitmap_info.file == NULL) {
1602 mddev->bitmap_info.offset =
1603 (__s32)le32_to_cpu(sb->bitmap_offset);
1604 /* Metadata doesn't record how much space is available.
1605 * For 1.0, we assume we can use up to the superblock
1606 * if before, else to 4K beyond superblock.
1607 * For others, assume no change is possible.
1609 if (mddev->minor_version > 0)
1610 mddev->bitmap_info.space = 0;
1611 else if (mddev->bitmap_info.offset > 0)
1612 mddev->bitmap_info.space =
1613 8 - mddev->bitmap_info.offset;
1615 mddev->bitmap_info.space =
1616 -mddev->bitmap_info.offset;
1619 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1620 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1621 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1622 mddev->new_level = le32_to_cpu(sb->new_level);
1623 mddev->new_layout = le32_to_cpu(sb->new_layout);
1624 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
1625 if (mddev->delta_disks < 0 ||
1626 (mddev->delta_disks == 0 &&
1627 (le32_to_cpu(sb->feature_map)
1628 & MD_FEATURE_RESHAPE_BACKWARDS)))
1629 mddev->reshape_backwards = 1;
1631 mddev->reshape_position = MaxSector;
1632 mddev->delta_disks = 0;
1633 mddev->new_level = mddev->level;
1634 mddev->new_layout = mddev->layout;
1635 mddev->new_chunk_sectors = mddev->chunk_sectors;
1638 } else if (mddev->pers == NULL) {
1639 /* Insist of good event counter while assembling, except for
1640 * spares (which don't need an event count) */
1642 if (rdev->desc_nr >= 0 &&
1643 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
1644 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe)
1645 if (ev1 < mddev->events)
1647 } else if (mddev->bitmap) {
1648 /* If adding to array with a bitmap, then we can accept an
1649 * older device, but not too old.
1651 if (ev1 < mddev->bitmap->events_cleared)
1653 if (ev1 < mddev->events)
1654 set_bit(Bitmap_sync, &rdev->flags);
1656 if (ev1 < mddev->events)
1657 /* just a hot-add of a new device, leave raid_disk at -1 */
1660 if (mddev->level != LEVEL_MULTIPATH) {
1662 if (rdev->desc_nr < 0 ||
1663 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1667 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
1669 case 0xffff: /* spare */
1671 case 0xfffe: /* faulty */
1672 set_bit(Faulty, &rdev->flags);
1675 rdev->saved_raid_disk = role;
1676 if ((le32_to_cpu(sb->feature_map) &
1677 MD_FEATURE_RECOVERY_OFFSET)) {
1678 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1679 if (!(le32_to_cpu(sb->feature_map) &
1680 MD_FEATURE_RECOVERY_BITMAP))
1681 rdev->saved_raid_disk = -1;
1683 set_bit(In_sync, &rdev->flags);
1684 rdev->raid_disk = role;
1687 if (sb->devflags & WriteMostly1)
1688 set_bit(WriteMostly, &rdev->flags);
1689 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT)
1690 set_bit(Replacement, &rdev->flags);
1691 } else /* MULTIPATH are always insync */
1692 set_bit(In_sync, &rdev->flags);
1697 static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev)
1699 struct mdp_superblock_1 *sb;
1700 struct md_rdev *rdev2;
1702 /* make rdev->sb match mddev and rdev data. */
1704 sb = page_address(rdev->sb_page);
1706 sb->feature_map = 0;
1708 sb->recovery_offset = cpu_to_le64(0);
1709 memset(sb->pad3, 0, sizeof(sb->pad3));
1711 sb->utime = cpu_to_le64((__u64)mddev->utime);
1712 sb->events = cpu_to_le64(mddev->events);
1714 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1716 sb->resync_offset = cpu_to_le64(0);
1718 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
1720 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
1721 sb->size = cpu_to_le64(mddev->dev_sectors);
1722 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
1723 sb->level = cpu_to_le32(mddev->level);
1724 sb->layout = cpu_to_le32(mddev->layout);
1726 if (test_bit(WriteMostly, &rdev->flags))
1727 sb->devflags |= WriteMostly1;
1729 sb->devflags &= ~WriteMostly1;
1730 sb->data_offset = cpu_to_le64(rdev->data_offset);
1731 sb->data_size = cpu_to_le64(rdev->sectors);
1733 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1734 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
1735 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
1738 if (rdev->raid_disk >= 0 &&
1739 !test_bit(In_sync, &rdev->flags)) {
1741 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1742 sb->recovery_offset =
1743 cpu_to_le64(rdev->recovery_offset);
1744 if (rdev->saved_raid_disk >= 0 && mddev->bitmap)
1746 cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP);
1748 if (test_bit(Replacement, &rdev->flags))
1750 cpu_to_le32(MD_FEATURE_REPLACEMENT);
1752 if (mddev->reshape_position != MaxSector) {
1753 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1754 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1755 sb->new_layout = cpu_to_le32(mddev->new_layout);
1756 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1757 sb->new_level = cpu_to_le32(mddev->new_level);
1758 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
1759 if (mddev->delta_disks == 0 &&
1760 mddev->reshape_backwards)
1762 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS);
1763 if (rdev->new_data_offset != rdev->data_offset) {
1765 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET);
1766 sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset
1767 - rdev->data_offset));
1771 if (rdev->badblocks.count == 0)
1772 /* Nothing to do for bad blocks*/ ;
1773 else if (sb->bblog_offset == 0)
1774 /* Cannot record bad blocks on this device */
1775 md_error(mddev, rdev);
1777 struct badblocks *bb = &rdev->badblocks;
1778 u64 *bbp = (u64 *)page_address(rdev->bb_page);
1780 sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
1785 seq = read_seqbegin(&bb->lock);
1787 memset(bbp, 0xff, PAGE_SIZE);
1789 for (i = 0 ; i < bb->count ; i++) {
1790 u64 internal_bb = p[i];
1791 u64 store_bb = ((BB_OFFSET(internal_bb) << 10)
1792 | BB_LEN(internal_bb));
1793 bbp[i] = cpu_to_le64(store_bb);
1796 if (read_seqretry(&bb->lock, seq))
1799 bb->sector = (rdev->sb_start +
1800 (int)le32_to_cpu(sb->bblog_offset));
1801 bb->size = le16_to_cpu(sb->bblog_size);
1806 rdev_for_each(rdev2, mddev)
1807 if (rdev2->desc_nr+1 > max_dev)
1808 max_dev = rdev2->desc_nr+1;
1810 if (max_dev > le32_to_cpu(sb->max_dev)) {
1812 sb->max_dev = cpu_to_le32(max_dev);
1813 rdev->sb_size = max_dev * 2 + 256;
1814 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1815 if (rdev->sb_size & bmask)
1816 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1818 max_dev = le32_to_cpu(sb->max_dev);
1820 for (i=0; i<max_dev;i++)
1821 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1823 rdev_for_each(rdev2, mddev) {
1825 if (test_bit(Faulty, &rdev2->flags))
1826 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1827 else if (test_bit(In_sync, &rdev2->flags))
1828 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
1829 else if (rdev2->raid_disk >= 0)
1830 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
1832 sb->dev_roles[i] = cpu_to_le16(0xffff);
1835 sb->sb_csum = calc_sb_1_csum(sb);
1838 static unsigned long long
1839 super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
1841 struct mdp_superblock_1 *sb;
1842 sector_t max_sectors;
1843 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
1844 return 0; /* component must fit device */
1845 if (rdev->data_offset != rdev->new_data_offset)
1846 return 0; /* too confusing */
1847 if (rdev->sb_start < rdev->data_offset) {
1848 /* minor versions 1 and 2; superblock before data */
1849 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
1850 max_sectors -= rdev->data_offset;
1851 if (!num_sectors || num_sectors > max_sectors)
1852 num_sectors = max_sectors;
1853 } else if (rdev->mddev->bitmap_info.offset) {
1854 /* minor version 0 with bitmap we can't move */
1857 /* minor version 0; superblock after data */
1859 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
1860 sb_start &= ~(sector_t)(4*2 - 1);
1861 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
1862 if (!num_sectors || num_sectors > max_sectors)
1863 num_sectors = max_sectors;
1864 rdev->sb_start = sb_start;
1866 sb = page_address(rdev->sb_page);
1867 sb->data_size = cpu_to_le64(num_sectors);
1868 sb->super_offset = rdev->sb_start;
1869 sb->sb_csum = calc_sb_1_csum(sb);
1870 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
1872 md_super_wait(rdev->mddev);
1878 super_1_allow_new_offset(struct md_rdev *rdev,
1879 unsigned long long new_offset)
1881 /* All necessary checks on new >= old have been done */
1882 struct bitmap *bitmap;
1883 if (new_offset >= rdev->data_offset)
1886 /* with 1.0 metadata, there is no metadata to tread on
1887 * so we can always move back */
1888 if (rdev->mddev->minor_version == 0)
1891 /* otherwise we must be sure not to step on
1892 * any metadata, so stay:
1893 * 36K beyond start of superblock
1894 * beyond end of badblocks
1895 * beyond write-intent bitmap
1897 if (rdev->sb_start + (32+4)*2 > new_offset)
1899 bitmap = rdev->mddev->bitmap;
1900 if (bitmap && !rdev->mddev->bitmap_info.file &&
1901 rdev->sb_start + rdev->mddev->bitmap_info.offset +
1902 bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset)
1904 if (rdev->badblocks.sector + rdev->badblocks.size > new_offset)
1910 static struct super_type super_types[] = {
1913 .owner = THIS_MODULE,
1914 .load_super = super_90_load,
1915 .validate_super = super_90_validate,
1916 .sync_super = super_90_sync,
1917 .rdev_size_change = super_90_rdev_size_change,
1918 .allow_new_offset = super_90_allow_new_offset,
1922 .owner = THIS_MODULE,
1923 .load_super = super_1_load,
1924 .validate_super = super_1_validate,
1925 .sync_super = super_1_sync,
1926 .rdev_size_change = super_1_rdev_size_change,
1927 .allow_new_offset = super_1_allow_new_offset,
1931 static void sync_super(struct mddev *mddev, struct md_rdev *rdev)
1933 if (mddev->sync_super) {
1934 mddev->sync_super(mddev, rdev);
1938 BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
1940 super_types[mddev->major_version].sync_super(mddev, rdev);
1943 static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2)
1945 struct md_rdev *rdev, *rdev2;
1948 rdev_for_each_rcu(rdev, mddev1)
1949 rdev_for_each_rcu(rdev2, mddev2)
1950 if (rdev->bdev->bd_contains ==
1951 rdev2->bdev->bd_contains) {
1959 static LIST_HEAD(pending_raid_disks);
1962 * Try to register data integrity profile for an mddev
1964 * This is called when an array is started and after a disk has been kicked
1965 * from the array. It only succeeds if all working and active component devices
1966 * are integrity capable with matching profiles.
1968 int md_integrity_register(struct mddev *mddev)
1970 struct md_rdev *rdev, *reference = NULL;
1972 if (list_empty(&mddev->disks))
1973 return 0; /* nothing to do */
1974 if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
1975 return 0; /* shouldn't register, or already is */
1976 rdev_for_each(rdev, mddev) {
1977 /* skip spares and non-functional disks */
1978 if (test_bit(Faulty, &rdev->flags))
1980 if (rdev->raid_disk < 0)
1983 /* Use the first rdev as the reference */
1987 /* does this rdev's profile match the reference profile? */
1988 if (blk_integrity_compare(reference->bdev->bd_disk,
1989 rdev->bdev->bd_disk) < 0)
1992 if (!reference || !bdev_get_integrity(reference->bdev))
1995 * All component devices are integrity capable and have matching
1996 * profiles, register the common profile for the md device.
1998 if (blk_integrity_register(mddev->gendisk,
1999 bdev_get_integrity(reference->bdev)) != 0) {
2000 printk(KERN_ERR "md: failed to register integrity for %s\n",
2004 printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev));
2005 if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) {
2006 printk(KERN_ERR "md: failed to create integrity pool for %s\n",
2012 EXPORT_SYMBOL(md_integrity_register);
2014 /* Disable data integrity if non-capable/non-matching disk is being added */
2015 void md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev)
2017 struct blk_integrity *bi_rdev;
2018 struct blk_integrity *bi_mddev;
2020 if (!mddev->gendisk)
2023 bi_rdev = bdev_get_integrity(rdev->bdev);
2024 bi_mddev = blk_get_integrity(mddev->gendisk);
2026 if (!bi_mddev) /* nothing to do */
2028 if (rdev->raid_disk < 0) /* skip spares */
2030 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
2031 rdev->bdev->bd_disk) >= 0)
2033 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
2034 blk_integrity_unregister(mddev->gendisk);
2036 EXPORT_SYMBOL(md_integrity_add_rdev);
2038 static int bind_rdev_to_array(struct md_rdev * rdev, struct mddev * mddev)
2040 char b[BDEVNAME_SIZE];
2050 /* prevent duplicates */
2051 if (find_rdev(mddev, rdev->bdev->bd_dev))
2054 /* make sure rdev->sectors exceeds mddev->dev_sectors */
2055 if (rdev->sectors && (mddev->dev_sectors == 0 ||
2056 rdev->sectors < mddev->dev_sectors)) {
2058 /* Cannot change size, so fail
2059 * If mddev->level <= 0, then we don't care
2060 * about aligning sizes (e.g. linear)
2062 if (mddev->level > 0)
2065 mddev->dev_sectors = rdev->sectors;
2068 /* Verify rdev->desc_nr is unique.
2069 * If it is -1, assign a free number, else
2070 * check number is not in use
2072 if (rdev->desc_nr < 0) {
2074 if (mddev->pers) choice = mddev->raid_disks;
2075 while (find_rdev_nr(mddev, choice))
2077 rdev->desc_nr = choice;
2079 if (find_rdev_nr(mddev, rdev->desc_nr))
2082 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
2083 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
2084 mdname(mddev), mddev->max_disks);
2087 bdevname(rdev->bdev,b);
2088 while ( (s=strchr(b, '/')) != NULL)
2091 rdev->mddev = mddev;
2092 printk(KERN_INFO "md: bind<%s>\n", b);
2094 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
2097 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
2098 if (sysfs_create_link(&rdev->kobj, ko, "block"))
2099 /* failure here is OK */;
2100 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
2102 list_add_rcu(&rdev->same_set, &mddev->disks);
2103 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
2105 /* May as well allow recovery to be retried once */
2106 mddev->recovery_disabled++;
2111 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
2116 static void md_delayed_delete(struct work_struct *ws)
2118 struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work);
2119 kobject_del(&rdev->kobj);
2120 kobject_put(&rdev->kobj);
2123 static void unbind_rdev_from_array(struct md_rdev * rdev)
2125 char b[BDEVNAME_SIZE];
2130 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
2131 list_del_rcu(&rdev->same_set);
2132 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
2134 sysfs_remove_link(&rdev->kobj, "block");
2135 sysfs_put(rdev->sysfs_state);
2136 rdev->sysfs_state = NULL;
2137 rdev->badblocks.count = 0;
2138 /* We need to delay this, otherwise we can deadlock when
2139 * writing to 'remove' to "dev/state". We also need
2140 * to delay it due to rcu usage.
2143 INIT_WORK(&rdev->del_work, md_delayed_delete);
2144 kobject_get(&rdev->kobj);
2145 queue_work(md_misc_wq, &rdev->del_work);
2149 * prevent the device from being mounted, repartitioned or
2150 * otherwise reused by a RAID array (or any other kernel
2151 * subsystem), by bd_claiming the device.
2153 static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared)
2156 struct block_device *bdev;
2157 char b[BDEVNAME_SIZE];
2159 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
2160 shared ? (struct md_rdev *)lock_rdev : rdev);
2162 printk(KERN_ERR "md: could not open %s.\n",
2163 __bdevname(dev, b));
2164 return PTR_ERR(bdev);
2170 static void unlock_rdev(struct md_rdev *rdev)
2172 struct block_device *bdev = rdev->bdev;
2176 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
2179 void md_autodetect_dev(dev_t dev);
2181 static void export_rdev(struct md_rdev * rdev)
2183 char b[BDEVNAME_SIZE];
2184 printk(KERN_INFO "md: export_rdev(%s)\n",
2185 bdevname(rdev->bdev,b));
2188 md_rdev_clear(rdev);
2190 if (test_bit(AutoDetected, &rdev->flags))
2191 md_autodetect_dev(rdev->bdev->bd_dev);
2194 kobject_put(&rdev->kobj);
2197 static void kick_rdev_from_array(struct md_rdev * rdev)
2199 unbind_rdev_from_array(rdev);
2203 static void export_array(struct mddev *mddev)
2205 struct md_rdev *rdev, *tmp;
2207 rdev_for_each_safe(rdev, tmp, mddev) {
2212 kick_rdev_from_array(rdev);
2214 if (!list_empty(&mddev->disks))
2216 mddev->raid_disks = 0;
2217 mddev->major_version = 0;
2220 static void print_desc(mdp_disk_t *desc)
2222 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
2223 desc->major,desc->minor,desc->raid_disk,desc->state);
2226 static void print_sb_90(mdp_super_t *sb)
2231 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
2232 sb->major_version, sb->minor_version, sb->patch_version,
2233 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
2235 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
2236 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
2237 sb->md_minor, sb->layout, sb->chunk_size);
2238 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
2239 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
2240 sb->utime, sb->state, sb->active_disks, sb->working_disks,
2241 sb->failed_disks, sb->spare_disks,
2242 sb->sb_csum, (unsigned long)sb->events_lo);
2245 for (i = 0; i < MD_SB_DISKS; i++) {
2248 desc = sb->disks + i;
2249 if (desc->number || desc->major || desc->minor ||
2250 desc->raid_disk || (desc->state && (desc->state != 4))) {
2251 printk(" D %2d: ", i);
2255 printk(KERN_INFO "md: THIS: ");
2256 print_desc(&sb->this_disk);
2259 static void print_sb_1(struct mdp_superblock_1 *sb)
2263 uuid = sb->set_uuid;
2265 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n"
2266 "md: Name: \"%s\" CT:%llu\n",
2267 le32_to_cpu(sb->major_version),
2268 le32_to_cpu(sb->feature_map),
2271 (unsigned long long)le64_to_cpu(sb->ctime)
2272 & MD_SUPERBLOCK_1_TIME_SEC_MASK);
2274 uuid = sb->device_uuid;
2276 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
2278 "md: Dev:%08x UUID: %pU\n"
2279 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
2280 "md: (MaxDev:%u) \n",
2281 le32_to_cpu(sb->level),
2282 (unsigned long long)le64_to_cpu(sb->size),
2283 le32_to_cpu(sb->raid_disks),
2284 le32_to_cpu(sb->layout),
2285 le32_to_cpu(sb->chunksize),
2286 (unsigned long long)le64_to_cpu(sb->data_offset),
2287 (unsigned long long)le64_to_cpu(sb->data_size),
2288 (unsigned long long)le64_to_cpu(sb->super_offset),
2289 (unsigned long long)le64_to_cpu(sb->recovery_offset),
2290 le32_to_cpu(sb->dev_number),
2293 (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK,
2294 (unsigned long long)le64_to_cpu(sb->events),
2295 (unsigned long long)le64_to_cpu(sb->resync_offset),
2296 le32_to_cpu(sb->sb_csum),
2297 le32_to_cpu(sb->max_dev)
2301 static void print_rdev(struct md_rdev *rdev, int major_version)
2303 char b[BDEVNAME_SIZE];
2304 printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
2305 bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors,
2306 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
2308 if (rdev->sb_loaded) {
2309 printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version);
2310 switch (major_version) {
2312 print_sb_90(page_address(rdev->sb_page));
2315 print_sb_1(page_address(rdev->sb_page));
2319 printk(KERN_INFO "md: no rdev superblock!\n");
2322 static void md_print_devices(void)
2324 struct list_head *tmp;
2325 struct md_rdev *rdev;
2326 struct mddev *mddev;
2327 char b[BDEVNAME_SIZE];
2330 printk("md: **********************************\n");
2331 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
2332 printk("md: **********************************\n");
2333 for_each_mddev(mddev, tmp) {
2336 bitmap_print_sb(mddev->bitmap);
2338 printk("%s: ", mdname(mddev));
2339 rdev_for_each(rdev, mddev)
2340 printk("<%s>", bdevname(rdev->bdev,b));
2343 rdev_for_each(rdev, mddev)
2344 print_rdev(rdev, mddev->major_version);
2346 printk("md: **********************************\n");
2351 static void sync_sbs(struct mddev * mddev, int nospares)
2353 /* Update each superblock (in-memory image), but
2354 * if we are allowed to, skip spares which already
2355 * have the right event counter, or have one earlier
2356 * (which would mean they aren't being marked as dirty
2357 * with the rest of the array)
2359 struct md_rdev *rdev;
2360 rdev_for_each(rdev, mddev) {
2361 if (rdev->sb_events == mddev->events ||
2363 rdev->raid_disk < 0 &&
2364 rdev->sb_events+1 == mddev->events)) {
2365 /* Don't update this superblock */
2366 rdev->sb_loaded = 2;
2368 sync_super(mddev, rdev);
2369 rdev->sb_loaded = 1;
2374 static void md_update_sb(struct mddev * mddev, int force_change)
2376 struct md_rdev *rdev;
2379 int any_badblocks_changed = 0;
2383 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2387 /* First make sure individual recovery_offsets are correct */
2388 rdev_for_each(rdev, mddev) {
2389 if (rdev->raid_disk >= 0 &&
2390 mddev->delta_disks >= 0 &&
2391 !test_bit(In_sync, &rdev->flags) &&
2392 mddev->curr_resync_completed > rdev->recovery_offset)
2393 rdev->recovery_offset = mddev->curr_resync_completed;
2396 if (!mddev->persistent) {
2397 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
2398 clear_bit(MD_CHANGE_DEVS, &mddev->flags);
2399 if (!mddev->external) {
2400 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
2401 rdev_for_each(rdev, mddev) {
2402 if (rdev->badblocks.changed) {
2403 rdev->badblocks.changed = 0;
2404 md_ack_all_badblocks(&rdev->badblocks);
2405 md_error(mddev, rdev);
2407 clear_bit(Blocked, &rdev->flags);
2408 clear_bit(BlockedBadBlocks, &rdev->flags);
2409 wake_up(&rdev->blocked_wait);
2412 wake_up(&mddev->sb_wait);
2416 spin_lock_irq(&mddev->write_lock);
2418 mddev->utime = get_seconds();
2420 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2422 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2423 /* just a clean<-> dirty transition, possibly leave spares alone,
2424 * though if events isn't the right even/odd, we will have to do
2430 if (mddev->degraded)
2431 /* If the array is degraded, then skipping spares is both
2432 * dangerous and fairly pointless.
2433 * Dangerous because a device that was removed from the array
2434 * might have a event_count that still looks up-to-date,
2435 * so it can be re-added without a resync.
2436 * Pointless because if there are any spares to skip,
2437 * then a recovery will happen and soon that array won't
2438 * be degraded any more and the spare can go back to sleep then.
2442 sync_req = mddev->in_sync;
2444 /* If this is just a dirty<->clean transition, and the array is clean
2445 * and 'events' is odd, we can roll back to the previous clean state */
2447 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
2448 && mddev->can_decrease_events
2449 && mddev->events != 1) {
2451 mddev->can_decrease_events = 0;
2453 /* otherwise we have to go forward and ... */
2455 mddev->can_decrease_events = nospares;
2458 if (!mddev->events) {
2460 * oops, this 64-bit counter should never wrap.
2461 * Either we are in around ~1 trillion A.C., assuming
2462 * 1 reboot per second, or we have a bug:
2468 rdev_for_each(rdev, mddev) {
2469 if (rdev->badblocks.changed)
2470 any_badblocks_changed++;
2471 if (test_bit(Faulty, &rdev->flags))
2472 set_bit(FaultRecorded, &rdev->flags);
2475 sync_sbs(mddev, nospares);
2476 spin_unlock_irq(&mddev->write_lock);
2478 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2479 mdname(mddev), mddev->in_sync);
2481 bitmap_update_sb(mddev->bitmap);
2482 rdev_for_each(rdev, mddev) {
2483 char b[BDEVNAME_SIZE];
2485 if (rdev->sb_loaded != 1)
2486 continue; /* no noise on spare devices */
2488 if (!test_bit(Faulty, &rdev->flags)) {
2489 md_super_write(mddev,rdev,
2490 rdev->sb_start, rdev->sb_size,
2492 pr_debug("md: (write) %s's sb offset: %llu\n",
2493 bdevname(rdev->bdev, b),
2494 (unsigned long long)rdev->sb_start);
2495 rdev->sb_events = mddev->events;
2496 if (rdev->badblocks.size) {
2497 md_super_write(mddev, rdev,
2498 rdev->badblocks.sector,
2499 rdev->badblocks.size << 9,
2501 rdev->badblocks.size = 0;
2505 pr_debug("md: %s (skipping faulty)\n",
2506 bdevname(rdev->bdev, b));
2508 if (mddev->level == LEVEL_MULTIPATH)
2509 /* only need to write one superblock... */
2512 md_super_wait(mddev);
2513 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
2515 spin_lock_irq(&mddev->write_lock);
2516 if (mddev->in_sync != sync_req ||
2517 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
2518 /* have to write it out again */
2519 spin_unlock_irq(&mddev->write_lock);
2522 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
2523 spin_unlock_irq(&mddev->write_lock);
2524 wake_up(&mddev->sb_wait);
2525 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2526 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
2528 rdev_for_each(rdev, mddev) {
2529 if (test_and_clear_bit(FaultRecorded, &rdev->flags))
2530 clear_bit(Blocked, &rdev->flags);
2532 if (any_badblocks_changed)
2533 md_ack_all_badblocks(&rdev->badblocks);
2534 clear_bit(BlockedBadBlocks, &rdev->flags);
2535 wake_up(&rdev->blocked_wait);
2539 /* words written to sysfs files may, or may not, be \n terminated.
2540 * We want to accept with case. For this we use cmd_match.
2542 static int cmd_match(const char *cmd, const char *str)
2544 /* See if cmd, written into a sysfs file, matches
2545 * str. They must either be the same, or cmd can
2546 * have a trailing newline
2548 while (*cmd && *str && *cmd == *str) {
2559 struct rdev_sysfs_entry {
2560 struct attribute attr;
2561 ssize_t (*show)(struct md_rdev *, char *);
2562 ssize_t (*store)(struct md_rdev *, const char *, size_t);
2566 state_show(struct md_rdev *rdev, char *page)
2571 if (test_bit(Faulty, &rdev->flags) ||
2572 rdev->badblocks.unacked_exist) {
2573 len+= sprintf(page+len, "%sfaulty",sep);
2576 if (test_bit(In_sync, &rdev->flags)) {
2577 len += sprintf(page+len, "%sin_sync",sep);
2580 if (test_bit(WriteMostly, &rdev->flags)) {
2581 len += sprintf(page+len, "%swrite_mostly",sep);
2584 if (test_bit(Blocked, &rdev->flags) ||
2585 (rdev->badblocks.unacked_exist
2586 && !test_bit(Faulty, &rdev->flags))) {
2587 len += sprintf(page+len, "%sblocked", sep);
2590 if (!test_bit(Faulty, &rdev->flags) &&
2591 !test_bit(In_sync, &rdev->flags)) {
2592 len += sprintf(page+len, "%sspare", sep);
2595 if (test_bit(WriteErrorSeen, &rdev->flags)) {
2596 len += sprintf(page+len, "%swrite_error", sep);
2599 if (test_bit(WantReplacement, &rdev->flags)) {
2600 len += sprintf(page+len, "%swant_replacement", sep);
2603 if (test_bit(Replacement, &rdev->flags)) {
2604 len += sprintf(page+len, "%sreplacement", sep);
2608 return len+sprintf(page+len, "\n");
2612 state_store(struct md_rdev *rdev, const char *buf, size_t len)
2615 * faulty - simulates an error
2616 * remove - disconnects the device
2617 * writemostly - sets write_mostly
2618 * -writemostly - clears write_mostly
2619 * blocked - sets the Blocked flags
2620 * -blocked - clears the Blocked and possibly simulates an error
2621 * insync - sets Insync providing device isn't active
2622 * -insync - clear Insync for a device with a slot assigned,
2623 * so that it gets rebuilt based on bitmap
2624 * write_error - sets WriteErrorSeen
2625 * -write_error - clears WriteErrorSeen
2628 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2629 md_error(rdev->mddev, rdev);
2630 if (test_bit(Faulty, &rdev->flags))
2634 } else if (cmd_match(buf, "remove")) {
2635 if (rdev->raid_disk >= 0)
2638 struct mddev *mddev = rdev->mddev;
2639 kick_rdev_from_array(rdev);
2641 md_update_sb(mddev, 1);
2642 md_new_event(mddev);
2645 } else if (cmd_match(buf, "writemostly")) {
2646 set_bit(WriteMostly, &rdev->flags);
2648 } else if (cmd_match(buf, "-writemostly")) {
2649 clear_bit(WriteMostly, &rdev->flags);
2651 } else if (cmd_match(buf, "blocked")) {
2652 set_bit(Blocked, &rdev->flags);
2654 } else if (cmd_match(buf, "-blocked")) {
2655 if (!test_bit(Faulty, &rdev->flags) &&
2656 rdev->badblocks.unacked_exist) {
2657 /* metadata handler doesn't understand badblocks,
2658 * so we need to fail the device
2660 md_error(rdev->mddev, rdev);
2662 clear_bit(Blocked, &rdev->flags);
2663 clear_bit(BlockedBadBlocks, &rdev->flags);
2664 wake_up(&rdev->blocked_wait);
2665 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2666 md_wakeup_thread(rdev->mddev->thread);
2669 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2670 set_bit(In_sync, &rdev->flags);
2672 } else if (cmd_match(buf, "-insync") && rdev->raid_disk >= 0) {
2673 clear_bit(In_sync, &rdev->flags);
2674 rdev->saved_raid_disk = rdev->raid_disk;
2675 rdev->raid_disk = -1;
2677 } else if (cmd_match(buf, "write_error")) {
2678 set_bit(WriteErrorSeen, &rdev->flags);
2680 } else if (cmd_match(buf, "-write_error")) {
2681 clear_bit(WriteErrorSeen, &rdev->flags);
2683 } else if (cmd_match(buf, "want_replacement")) {
2684 /* Any non-spare device that is not a replacement can
2685 * become want_replacement at any time, but we then need to
2686 * check if recovery is needed.
2688 if (rdev->raid_disk >= 0 &&
2689 !test_bit(Replacement, &rdev->flags))
2690 set_bit(WantReplacement, &rdev->flags);
2691 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2692 md_wakeup_thread(rdev->mddev->thread);
2694 } else if (cmd_match(buf, "-want_replacement")) {
2695 /* Clearing 'want_replacement' is always allowed.
2696 * Once replacements starts it is too late though.
2699 clear_bit(WantReplacement, &rdev->flags);
2700 } else if (cmd_match(buf, "replacement")) {
2701 /* Can only set a device as a replacement when array has not
2702 * yet been started. Once running, replacement is automatic
2703 * from spares, or by assigning 'slot'.
2705 if (rdev->mddev->pers)
2708 set_bit(Replacement, &rdev->flags);
2711 } else if (cmd_match(buf, "-replacement")) {
2712 /* Similarly, can only clear Replacement before start */
2713 if (rdev->mddev->pers)
2716 clear_bit(Replacement, &rdev->flags);
2721 sysfs_notify_dirent_safe(rdev->sysfs_state);
2722 return err ? err : len;
2724 static struct rdev_sysfs_entry rdev_state =
2725 __ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
2728 errors_show(struct md_rdev *rdev, char *page)
2730 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2734 errors_store(struct md_rdev *rdev, const char *buf, size_t len)
2737 unsigned long n = simple_strtoul(buf, &e, 10);
2738 if (*buf && (*e == 0 || *e == '\n')) {
2739 atomic_set(&rdev->corrected_errors, n);
2744 static struct rdev_sysfs_entry rdev_errors =
2745 __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
2748 slot_show(struct md_rdev *rdev, char *page)
2750 if (rdev->raid_disk < 0)
2751 return sprintf(page, "none\n");
2753 return sprintf(page, "%d\n", rdev->raid_disk);
2757 slot_store(struct md_rdev *rdev, const char *buf, size_t len)
2761 int slot = simple_strtoul(buf, &e, 10);
2762 if (strncmp(buf, "none", 4)==0)
2764 else if (e==buf || (*e && *e!= '\n'))
2766 if (rdev->mddev->pers && slot == -1) {
2767 /* Setting 'slot' on an active array requires also
2768 * updating the 'rd%d' link, and communicating
2769 * with the personality with ->hot_*_disk.
2770 * For now we only support removing
2771 * failed/spare devices. This normally happens automatically,
2772 * but not when the metadata is externally managed.
2774 if (rdev->raid_disk == -1)
2776 /* personality does all needed checks */
2777 if (rdev->mddev->pers->hot_remove_disk == NULL)
2779 clear_bit(Blocked, &rdev->flags);
2780 remove_and_add_spares(rdev->mddev, rdev);
2781 if (rdev->raid_disk >= 0)
2783 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2784 md_wakeup_thread(rdev->mddev->thread);
2785 } else if (rdev->mddev->pers) {
2786 /* Activating a spare .. or possibly reactivating
2787 * if we ever get bitmaps working here.
2790 if (rdev->raid_disk != -1)
2793 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
2796 if (rdev->mddev->pers->hot_add_disk == NULL)
2799 if (slot >= rdev->mddev->raid_disks &&
2800 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2803 rdev->raid_disk = slot;
2804 if (test_bit(In_sync, &rdev->flags))
2805 rdev->saved_raid_disk = slot;
2807 rdev->saved_raid_disk = -1;
2808 clear_bit(In_sync, &rdev->flags);
2809 clear_bit(Bitmap_sync, &rdev->flags);
2810 err = rdev->mddev->pers->
2811 hot_add_disk(rdev->mddev, rdev);
2813 rdev->raid_disk = -1;
2816 sysfs_notify_dirent_safe(rdev->sysfs_state);
2817 if (sysfs_link_rdev(rdev->mddev, rdev))
2818 /* failure here is OK */;
2819 /* don't wakeup anyone, leave that to userspace. */
2821 if (slot >= rdev->mddev->raid_disks &&
2822 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2824 rdev->raid_disk = slot;
2825 /* assume it is working */
2826 clear_bit(Faulty, &rdev->flags);
2827 clear_bit(WriteMostly, &rdev->flags);
2828 set_bit(In_sync, &rdev->flags);
2829 sysfs_notify_dirent_safe(rdev->sysfs_state);
2835 static struct rdev_sysfs_entry rdev_slot =
2836 __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
2839 offset_show(struct md_rdev *rdev, char *page)
2841 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
2845 offset_store(struct md_rdev *rdev, const char *buf, size_t len)
2847 unsigned long long offset;
2848 if (kstrtoull(buf, 10, &offset) < 0)
2850 if (rdev->mddev->pers && rdev->raid_disk >= 0)
2852 if (rdev->sectors && rdev->mddev->external)
2853 /* Must set offset before size, so overlap checks
2856 rdev->data_offset = offset;
2857 rdev->new_data_offset = offset;
2861 static struct rdev_sysfs_entry rdev_offset =
2862 __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
2864 static ssize_t new_offset_show(struct md_rdev *rdev, char *page)
2866 return sprintf(page, "%llu\n",
2867 (unsigned long long)rdev->new_data_offset);
2870 static ssize_t new_offset_store(struct md_rdev *rdev,
2871 const char *buf, size_t len)
2873 unsigned long long new_offset;
2874 struct mddev *mddev = rdev->mddev;
2876 if (kstrtoull(buf, 10, &new_offset) < 0)
2879 if (mddev->sync_thread)
2881 if (new_offset == rdev->data_offset)
2882 /* reset is always permitted */
2884 else if (new_offset > rdev->data_offset) {
2885 /* must not push array size beyond rdev_sectors */
2886 if (new_offset - rdev->data_offset
2887 + mddev->dev_sectors > rdev->sectors)
2890 /* Metadata worries about other space details. */
2892 /* decreasing the offset is inconsistent with a backwards
2895 if (new_offset < rdev->data_offset &&
2896 mddev->reshape_backwards)
2898 /* Increasing offset is inconsistent with forwards
2899 * reshape. reshape_direction should be set to
2900 * 'backwards' first.
2902 if (new_offset > rdev->data_offset &&
2903 !mddev->reshape_backwards)
2906 if (mddev->pers && mddev->persistent &&
2907 !super_types[mddev->major_version]
2908 .allow_new_offset(rdev, new_offset))
2910 rdev->new_data_offset = new_offset;
2911 if (new_offset > rdev->data_offset)
2912 mddev->reshape_backwards = 1;
2913 else if (new_offset < rdev->data_offset)
2914 mddev->reshape_backwards = 0;
2918 static struct rdev_sysfs_entry rdev_new_offset =
2919 __ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store);
2922 rdev_size_show(struct md_rdev *rdev, char *page)
2924 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
2927 static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2929 /* check if two start/length pairs overlap */
2937 static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2939 unsigned long long blocks;
2942 if (kstrtoull(buf, 10, &blocks) < 0)
2945 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2946 return -EINVAL; /* sector conversion overflow */
2949 if (new != blocks * 2)
2950 return -EINVAL; /* unsigned long long to sector_t overflow */
2957 rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len)
2959 struct mddev *my_mddev = rdev->mddev;
2960 sector_t oldsectors = rdev->sectors;
2963 if (strict_blocks_to_sectors(buf, §ors) < 0)
2965 if (rdev->data_offset != rdev->new_data_offset)
2966 return -EINVAL; /* too confusing */
2967 if (my_mddev->pers && rdev->raid_disk >= 0) {
2968 if (my_mddev->persistent) {
2969 sectors = super_types[my_mddev->major_version].
2970 rdev_size_change(rdev, sectors);
2973 } else if (!sectors)
2974 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
2976 if (!my_mddev->pers->resize)
2977 /* Cannot change size for RAID0 or Linear etc */
2980 if (sectors < my_mddev->dev_sectors)
2981 return -EINVAL; /* component must fit device */
2983 rdev->sectors = sectors;
2984 if (sectors > oldsectors && my_mddev->external) {
2985 /* need to check that all other rdevs with the same ->bdev
2986 * do not overlap. We need to unlock the mddev to avoid
2987 * a deadlock. We have already changed rdev->sectors, and if
2988 * we have to change it back, we will have the lock again.
2990 struct mddev *mddev;
2992 struct list_head *tmp;
2994 mddev_unlock(my_mddev);
2995 for_each_mddev(mddev, tmp) {
2996 struct md_rdev *rdev2;
2998 mddev_lock_nointr(mddev);
2999 rdev_for_each(rdev2, mddev)
3000 if (rdev->bdev == rdev2->bdev &&
3002 overlaps(rdev->data_offset, rdev->sectors,
3008 mddev_unlock(mddev);
3014 mddev_lock_nointr(my_mddev);
3016 /* Someone else could have slipped in a size
3017 * change here, but doing so is just silly.
3018 * We put oldsectors back because we *know* it is
3019 * safe, and trust userspace not to race with
3022 rdev->sectors = oldsectors;
3029 static struct rdev_sysfs_entry rdev_size =
3030 __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
3033 static ssize_t recovery_start_show(struct md_rdev *rdev, char *page)
3035 unsigned long long recovery_start = rdev->recovery_offset;
3037 if (test_bit(In_sync, &rdev->flags) ||
3038 recovery_start == MaxSector)
3039 return sprintf(page, "none\n");
3041 return sprintf(page, "%llu\n", recovery_start);
3044 static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len)
3046 unsigned long long recovery_start;
3048 if (cmd_match(buf, "none"))
3049 recovery_start = MaxSector;
3050 else if (kstrtoull(buf, 10, &recovery_start))
3053 if (rdev->mddev->pers &&
3054 rdev->raid_disk >= 0)
3057 rdev->recovery_offset = recovery_start;
3058 if (recovery_start == MaxSector)
3059 set_bit(In_sync, &rdev->flags);
3061 clear_bit(In_sync, &rdev->flags);
3065 static struct rdev_sysfs_entry rdev_recovery_start =
3066 __ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
3070 badblocks_show(struct badblocks *bb, char *page, int unack);
3072 badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack);
3074 static ssize_t bb_show(struct md_rdev *rdev, char *page)
3076 return badblocks_show(&rdev->badblocks, page, 0);
3078 static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len)
3080 int rv = badblocks_store(&rdev->badblocks, page, len, 0);
3081 /* Maybe that ack was all we needed */
3082 if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags))
3083 wake_up(&rdev->blocked_wait);
3086 static struct rdev_sysfs_entry rdev_bad_blocks =
3087 __ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
3090 static ssize_t ubb_show(struct md_rdev *rdev, char *page)
3092 return badblocks_show(&rdev->badblocks, page, 1);
3094 static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len)
3096 return badblocks_store(&rdev->badblocks, page, len, 1);
3098 static struct rdev_sysfs_entry rdev_unack_bad_blocks =
3099 __ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
3101 static struct attribute *rdev_default_attrs[] = {
3106 &rdev_new_offset.attr,
3108 &rdev_recovery_start.attr,
3109 &rdev_bad_blocks.attr,
3110 &rdev_unack_bad_blocks.attr,
3114 rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3116 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
3117 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
3118 struct mddev *mddev = rdev->mddev;
3124 rv = mddev ? mddev_lock(mddev) : -EBUSY;
3126 if (rdev->mddev == NULL)
3129 rv = entry->show(rdev, page);
3130 mddev_unlock(mddev);
3136 rdev_attr_store(struct kobject *kobj, struct attribute *attr,
3137 const char *page, size_t length)
3139 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
3140 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
3142 struct mddev *mddev = rdev->mddev;
3146 if (!capable(CAP_SYS_ADMIN))
3148 rv = mddev ? mddev_lock(mddev): -EBUSY;
3150 if (rdev->mddev == NULL)
3153 rv = entry->store(rdev, page, length);
3154 mddev_unlock(mddev);
3159 static void rdev_free(struct kobject *ko)
3161 struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj);
3164 static const struct sysfs_ops rdev_sysfs_ops = {
3165 .show = rdev_attr_show,
3166 .store = rdev_attr_store,
3168 static struct kobj_type rdev_ktype = {
3169 .release = rdev_free,
3170 .sysfs_ops = &rdev_sysfs_ops,
3171 .default_attrs = rdev_default_attrs,
3174 int md_rdev_init(struct md_rdev *rdev)
3177 rdev->saved_raid_disk = -1;
3178 rdev->raid_disk = -1;
3180 rdev->data_offset = 0;
3181 rdev->new_data_offset = 0;
3182 rdev->sb_events = 0;
3183 rdev->last_read_error.tv_sec = 0;
3184 rdev->last_read_error.tv_nsec = 0;
3185 rdev->sb_loaded = 0;
3186 rdev->bb_page = NULL;
3187 atomic_set(&rdev->nr_pending, 0);
3188 atomic_set(&rdev->read_errors, 0);
3189 atomic_set(&rdev->corrected_errors, 0);
3191 INIT_LIST_HEAD(&rdev->same_set);
3192 init_waitqueue_head(&rdev->blocked_wait);
3194 /* Add space to store bad block list.
3195 * This reserves the space even on arrays where it cannot
3196 * be used - I wonder if that matters
3198 rdev->badblocks.count = 0;
3199 rdev->badblocks.shift = -1; /* disabled until explicitly enabled */
3200 rdev->badblocks.page = kmalloc(PAGE_SIZE, GFP_KERNEL);
3201 seqlock_init(&rdev->badblocks.lock);
3202 if (rdev->badblocks.page == NULL)
3207 EXPORT_SYMBOL_GPL(md_rdev_init);
3209 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3211 * mark the device faulty if:
3213 * - the device is nonexistent (zero size)
3214 * - the device has no valid superblock
3216 * a faulty rdev _never_ has rdev->sb set.
3218 static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor)
3220 char b[BDEVNAME_SIZE];
3222 struct md_rdev *rdev;
3225 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
3227 printk(KERN_ERR "md: could not alloc mem for new device!\n");
3228 return ERR_PTR(-ENOMEM);
3231 err = md_rdev_init(rdev);
3234 err = alloc_disk_sb(rdev);
3238 err = lock_rdev(rdev, newdev, super_format == -2);
3242 kobject_init(&rdev->kobj, &rdev_ktype);
3244 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
3247 "md: %s has zero or unknown size, marking faulty!\n",
3248 bdevname(rdev->bdev,b));
3253 if (super_format >= 0) {
3254 err = super_types[super_format].
3255 load_super(rdev, NULL, super_minor);
3256 if (err == -EINVAL) {
3258 "md: %s does not have a valid v%d.%d "
3259 "superblock, not importing!\n",
3260 bdevname(rdev->bdev,b),
3261 super_format, super_minor);
3266 "md: could not read %s's sb, not importing!\n",
3267 bdevname(rdev->bdev,b));
3277 md_rdev_clear(rdev);
3279 return ERR_PTR(err);
3283 * Check a full RAID array for plausibility
3287 static void analyze_sbs(struct mddev * mddev)
3290 struct md_rdev *rdev, *freshest, *tmp;
3291 char b[BDEVNAME_SIZE];
3294 rdev_for_each_safe(rdev, tmp, mddev)
3295 switch (super_types[mddev->major_version].
3296 load_super(rdev, freshest, mddev->minor_version)) {
3304 "md: fatal superblock inconsistency in %s"
3305 " -- removing from array\n",
3306 bdevname(rdev->bdev,b));
3307 kick_rdev_from_array(rdev);
3311 super_types[mddev->major_version].
3312 validate_super(mddev, freshest);
3315 rdev_for_each_safe(rdev, tmp, mddev) {
3316 if (mddev->max_disks &&
3317 (rdev->desc_nr >= mddev->max_disks ||
3318 i > mddev->max_disks)) {
3320 "md: %s: %s: only %d devices permitted\n",
3321 mdname(mddev), bdevname(rdev->bdev, b),
3323 kick_rdev_from_array(rdev);
3326 if (rdev != freshest)
3327 if (super_types[mddev->major_version].
3328 validate_super(mddev, rdev)) {
3329 printk(KERN_WARNING "md: kicking non-fresh %s"
3331 bdevname(rdev->bdev,b));
3332 kick_rdev_from_array(rdev);
3335 if (mddev->level == LEVEL_MULTIPATH) {
3336 rdev->desc_nr = i++;
3337 rdev->raid_disk = rdev->desc_nr;
3338 set_bit(In_sync, &rdev->flags);
3339 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
3340 rdev->raid_disk = -1;
3341 clear_bit(In_sync, &rdev->flags);
3346 /* Read a fixed-point number.
3347 * Numbers in sysfs attributes should be in "standard" units where
3348 * possible, so time should be in seconds.
3349 * However we internally use a a much smaller unit such as
3350 * milliseconds or jiffies.
3351 * This function takes a decimal number with a possible fractional
3352 * component, and produces an integer which is the result of
3353 * multiplying that number by 10^'scale'.
3354 * all without any floating-point arithmetic.
3356 int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3358 unsigned long result = 0;
3360 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3363 else if (decimals < scale) {
3366 result = result * 10 + value;
3378 while (decimals < scale) {
3387 static void md_safemode_timeout(unsigned long data);
3390 safe_delay_show(struct mddev *mddev, char *page)
3392 int msec = (mddev->safemode_delay*1000)/HZ;
3393 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3396 safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len)
3400 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
3403 mddev->safemode_delay = 0;
3405 unsigned long old_delay = mddev->safemode_delay;
3406 mddev->safemode_delay = (msec*HZ)/1000;
3407 if (mddev->safemode_delay == 0)
3408 mddev->safemode_delay = 1;
3409 if (mddev->safemode_delay < old_delay || old_delay == 0)
3410 md_safemode_timeout((unsigned long)mddev);
3414 static struct md_sysfs_entry md_safe_delay =
3415 __ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
3418 level_show(struct mddev *mddev, char *page)
3420 struct md_personality *p = mddev->pers;
3422 return sprintf(page, "%s\n", p->name);
3423 else if (mddev->clevel[0])
3424 return sprintf(page, "%s\n", mddev->clevel);
3425 else if (mddev->level != LEVEL_NONE)
3426 return sprintf(page, "%d\n", mddev->level);
3432 level_store(struct mddev *mddev, const char *buf, size_t len)
3436 struct md_personality *pers;
3439 struct md_rdev *rdev;
3441 if (mddev->pers == NULL) {
3444 if (len >= sizeof(mddev->clevel))
3446 strncpy(mddev->clevel, buf, len);
3447 if (mddev->clevel[len-1] == '\n')
3449 mddev->clevel[len] = 0;
3450 mddev->level = LEVEL_NONE;
3454 /* request to change the personality. Need to ensure:
3455 * - array is not engaged in resync/recovery/reshape
3456 * - old personality can be suspended
3457 * - new personality will access other array.
3460 if (mddev->sync_thread ||
3461 mddev->reshape_position != MaxSector ||
3462 mddev->sysfs_active)
3465 if (!mddev->pers->quiesce) {
3466 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
3467 mdname(mddev), mddev->pers->name);
3471 /* Now find the new personality */
3472 if (len == 0 || len >= sizeof(clevel))
3474 strncpy(clevel, buf, len);
3475 if (clevel[len-1] == '\n')
3478 if (kstrtol(clevel, 10, &level))
3481 if (request_module("md-%s", clevel) != 0)
3482 request_module("md-level-%s", clevel);
3483 spin_lock(&pers_lock);
3484 pers = find_pers(level, clevel);
3485 if (!pers || !try_module_get(pers->owner)) {
3486 spin_unlock(&pers_lock);
3487 printk(KERN_WARNING "md: personality %s not loaded\n", clevel);
3490 spin_unlock(&pers_lock);
3492 if (pers == mddev->pers) {
3493 /* Nothing to do! */
3494 module_put(pers->owner);
3497 if (!pers->takeover) {
3498 module_put(pers->owner);
3499 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
3500 mdname(mddev), clevel);
3504 rdev_for_each(rdev, mddev)
3505 rdev->new_raid_disk = rdev->raid_disk;
3507 /* ->takeover must set new_* and/or delta_disks
3508 * if it succeeds, and may set them when it fails.
3510 priv = pers->takeover(mddev);
3512 mddev->new_level = mddev->level;
3513 mddev->new_layout = mddev->layout;
3514 mddev->new_chunk_sectors = mddev->chunk_sectors;
3515 mddev->raid_disks -= mddev->delta_disks;
3516 mddev->delta_disks = 0;
3517 mddev->reshape_backwards = 0;
3518 module_put(pers->owner);
3519 printk(KERN_WARNING "md: %s: %s would not accept array\n",
3520 mdname(mddev), clevel);
3521 return PTR_ERR(priv);
3524 /* Looks like we have a winner */
3525 mddev_suspend(mddev);
3526 mddev->pers->stop(mddev);
3528 if (mddev->pers->sync_request == NULL &&
3529 pers->sync_request != NULL) {
3530 /* need to add the md_redundancy_group */
3531 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3533 "md: cannot register extra attributes for %s\n",
3535 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
3537 if (mddev->pers->sync_request != NULL &&
3538 pers->sync_request == NULL) {
3539 /* need to remove the md_redundancy_group */
3540 if (mddev->to_remove == NULL)
3541 mddev->to_remove = &md_redundancy_group;
3544 if (mddev->pers->sync_request == NULL &&
3546 /* We are converting from a no-redundancy array
3547 * to a redundancy array and metadata is managed
3548 * externally so we need to be sure that writes
3549 * won't block due to a need to transition
3551 * until external management is started.
3554 mddev->safemode_delay = 0;
3555 mddev->safemode = 0;
3558 rdev_for_each(rdev, mddev) {
3559 if (rdev->raid_disk < 0)
3561 if (rdev->new_raid_disk >= mddev->raid_disks)
3562 rdev->new_raid_disk = -1;
3563 if (rdev->new_raid_disk == rdev->raid_disk)
3565 sysfs_unlink_rdev(mddev, rdev);
3567 rdev_for_each(rdev, mddev) {
3568 if (rdev->raid_disk < 0)
3570 if (rdev->new_raid_disk == rdev->raid_disk)
3572 rdev->raid_disk = rdev->new_raid_disk;
3573 if (rdev->raid_disk < 0)
3574 clear_bit(In_sync, &rdev->flags);
3576 if (sysfs_link_rdev(mddev, rdev))
3577 printk(KERN_WARNING "md: cannot register rd%d"
3578 " for %s after level change\n",
3579 rdev->raid_disk, mdname(mddev));
3583 module_put(mddev->pers->owner);
3585 mddev->private = priv;
3586 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3587 mddev->level = mddev->new_level;
3588 mddev->layout = mddev->new_layout;
3589 mddev->chunk_sectors = mddev->new_chunk_sectors;
3590 mddev->delta_disks = 0;
3591 mddev->reshape_backwards = 0;
3592 mddev->degraded = 0;
3593 if (mddev->pers->sync_request == NULL) {
3594 /* this is now an array without redundancy, so
3595 * it must always be in_sync
3598 del_timer_sync(&mddev->safemode_timer);
3600 blk_set_stacking_limits(&mddev->queue->limits);
3602 set_bit(MD_CHANGE_DEVS, &mddev->flags);
3603 mddev_resume(mddev);
3605 md_update_sb(mddev, 1);
3606 sysfs_notify(&mddev->kobj, NULL, "level");
3607 md_new_event(mddev);
3611 static struct md_sysfs_entry md_level =
3612 __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
3616 layout_show(struct mddev *mddev, char *page)
3618 /* just a number, not meaningful for all levels */
3619 if (mddev->reshape_position != MaxSector &&
3620 mddev->layout != mddev->new_layout)
3621 return sprintf(page, "%d (%d)\n",
3622 mddev->new_layout, mddev->layout);
3623 return sprintf(page, "%d\n", mddev->layout);
3627 layout_store(struct mddev *mddev, const char *buf, size_t len)
3630 unsigned long n = simple_strtoul(buf, &e, 10);
3632 if (!*buf || (*e && *e != '\n'))
3637 if (mddev->pers->check_reshape == NULL)
3639 mddev->new_layout = n;
3640 err = mddev->pers->check_reshape(mddev);
3642 mddev->new_layout = mddev->layout;
3646 mddev->new_layout = n;
3647 if (mddev->reshape_position == MaxSector)
3652 static struct md_sysfs_entry md_layout =
3653 __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
3657 raid_disks_show(struct mddev *mddev, char *page)
3659 if (mddev->raid_disks == 0)
3661 if (mddev->reshape_position != MaxSector &&
3662 mddev->delta_disks != 0)
3663 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3664 mddev->raid_disks - mddev->delta_disks);
3665 return sprintf(page, "%d\n", mddev->raid_disks);
3668 static int update_raid_disks(struct mddev *mddev, int raid_disks);
3671 raid_disks_store(struct mddev *mddev, const char *buf, size_t len)
3675 unsigned long n = simple_strtoul(buf, &e, 10);
3677 if (!*buf || (*e && *e != '\n'))
3681 rv = update_raid_disks(mddev, n);
3682 else if (mddev->reshape_position != MaxSector) {
3683 struct md_rdev *rdev;
3684 int olddisks = mddev->raid_disks - mddev->delta_disks;
3686 rdev_for_each(rdev, mddev) {
3688 rdev->data_offset < rdev->new_data_offset)
3691 rdev->data_offset > rdev->new_data_offset)
3694 mddev->delta_disks = n - olddisks;
3695 mddev->raid_disks = n;
3696 mddev->reshape_backwards = (mddev->delta_disks < 0);
3698 mddev->raid_disks = n;
3699 return rv ? rv : len;
3701 static struct md_sysfs_entry md_raid_disks =
3702 __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
3705 chunk_size_show(struct mddev *mddev, char *page)
3707 if (mddev->reshape_position != MaxSector &&
3708 mddev->chunk_sectors != mddev->new_chunk_sectors)
3709 return sprintf(page, "%d (%d)\n",
3710 mddev->new_chunk_sectors << 9,
3711 mddev->chunk_sectors << 9);
3712 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
3716 chunk_size_store(struct mddev *mddev, const char *buf, size_t len)
3719 unsigned long n = simple_strtoul(buf, &e, 10);
3721 if (!*buf || (*e && *e != '\n'))
3726 if (mddev->pers->check_reshape == NULL)
3728 mddev->new_chunk_sectors = n >> 9;
3729 err = mddev->pers->check_reshape(mddev);
3731 mddev->new_chunk_sectors = mddev->chunk_sectors;
3735 mddev->new_chunk_sectors = n >> 9;
3736 if (mddev->reshape_position == MaxSector)
3737 mddev->chunk_sectors = n >> 9;
3741 static struct md_sysfs_entry md_chunk_size =
3742 __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
3745 resync_start_show(struct mddev *mddev, char *page)
3747 if (mddev->recovery_cp == MaxSector)
3748 return sprintf(page, "none\n");
3749 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3753 resync_start_store(struct mddev *mddev, const char *buf, size_t len)
3756 unsigned long long n = simple_strtoull(buf, &e, 10);
3758 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
3760 if (cmd_match(buf, "none"))
3762 else if (!*buf || (*e && *e != '\n'))
3765 mddev->recovery_cp = n;
3767 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
3770 static struct md_sysfs_entry md_resync_start =
3771 __ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
3774 * The array state can be:
3777 * No devices, no size, no level
3778 * Equivalent to STOP_ARRAY ioctl
3780 * May have some settings, but array is not active
3781 * all IO results in error
3782 * When written, doesn't tear down array, but just stops it
3783 * suspended (not supported yet)
3784 * All IO requests will block. The array can be reconfigured.
3785 * Writing this, if accepted, will block until array is quiescent
3787 * no resync can happen. no superblocks get written.
3788 * write requests fail
3790 * like readonly, but behaves like 'clean' on a write request.
3792 * clean - no pending writes, but otherwise active.
3793 * When written to inactive array, starts without resync
3794 * If a write request arrives then
3795 * if metadata is known, mark 'dirty' and switch to 'active'.
3796 * if not known, block and switch to write-pending
3797 * If written to an active array that has pending writes, then fails.
3799 * fully active: IO and resync can be happening.
3800 * When written to inactive array, starts with resync
3803 * clean, but writes are blocked waiting for 'active' to be written.
3806 * like active, but no writes have been seen for a while (100msec).
3809 enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3810 write_pending, active_idle, bad_word};
3811 static char *array_states[] = {
3812 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3813 "write-pending", "active-idle", NULL };
3815 static int match_word(const char *word, char **list)
3818 for (n=0; list[n]; n++)
3819 if (cmd_match(word, list[n]))
3825 array_state_show(struct mddev *mddev, char *page)
3827 enum array_state st = inactive;
3840 else if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
3842 else if (mddev->safemode)
3848 if (list_empty(&mddev->disks) &&
3849 mddev->raid_disks == 0 &&
3850 mddev->dev_sectors == 0)
3855 return sprintf(page, "%s\n", array_states[st]);
3858 static int do_md_stop(struct mddev * mddev, int ro, struct block_device *bdev);
3859 static int md_set_readonly(struct mddev * mddev, struct block_device *bdev);
3860 static int do_md_run(struct mddev * mddev);
3861 static int restart_array(struct mddev *mddev);
3864 array_state_store(struct mddev *mddev, const char *buf, size_t len)
3867 enum array_state st = match_word(buf, array_states);
3872 /* stopping an active array */
3873 err = do_md_stop(mddev, 0, NULL);
3876 /* stopping an active array */
3878 err = do_md_stop(mddev, 2, NULL);
3880 err = 0; /* already inactive */
3883 break; /* not supported yet */
3886 err = md_set_readonly(mddev, NULL);
3889 set_disk_ro(mddev->gendisk, 1);
3890 err = do_md_run(mddev);
3896 err = md_set_readonly(mddev, NULL);
3897 else if (mddev->ro == 1)
3898 err = restart_array(mddev);
3901 set_disk_ro(mddev->gendisk, 0);
3905 err = do_md_run(mddev);
3910 restart_array(mddev);
3911 spin_lock_irq(&mddev->write_lock);
3912 if (atomic_read(&mddev->writes_pending) == 0) {
3913 if (mddev->in_sync == 0) {
3915 if (mddev->safemode == 1)
3916 mddev->safemode = 0;
3917 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
3922 spin_unlock_irq(&mddev->write_lock);
3928 restart_array(mddev);
3929 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
3930 wake_up(&mddev->sb_wait);
3934 set_disk_ro(mddev->gendisk, 0);
3935 err = do_md_run(mddev);
3940 /* these cannot be set */
3946 if (mddev->hold_active == UNTIL_IOCTL)
3947 mddev->hold_active = 0;
3948 sysfs_notify_dirent_safe(mddev->sysfs_state);
3952 static struct md_sysfs_entry md_array_state =
3953 __ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
3956 max_corrected_read_errors_show(struct mddev *mddev, char *page) {
3957 return sprintf(page, "%d\n",
3958 atomic_read(&mddev->max_corr_read_errors));
3962 max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len)
3965 unsigned long n = simple_strtoul(buf, &e, 10);
3967 if (*buf && (*e == 0 || *e == '\n')) {
3968 atomic_set(&mddev->max_corr_read_errors, n);
3974 static struct md_sysfs_entry max_corr_read_errors =
3975 __ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
3976 max_corrected_read_errors_store);
3979 null_show(struct mddev *mddev, char *page)
3985 new_dev_store(struct mddev *mddev, const char *buf, size_t len)
3987 /* buf must be %d:%d\n? giving major and minor numbers */
3988 /* The new device is added to the array.
3989 * If the array has a persistent superblock, we read the
3990 * superblock to initialise info and check validity.
3991 * Otherwise, only checking done is that in bind_rdev_to_array,
3992 * which mainly checks size.
3995 int major = simple_strtoul(buf, &e, 10);
3998 struct md_rdev *rdev;
4001 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
4003 minor = simple_strtoul(e+1, &e, 10);
4004 if (*e && *e != '\n')
4006 dev = MKDEV(major, minor);
4007 if (major != MAJOR(dev) ||
4008 minor != MINOR(dev))
4012 if (mddev->persistent) {
4013 rdev = md_import_device(dev, mddev->major_version,
4014 mddev->minor_version);
4015 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
4016 struct md_rdev *rdev0
4017 = list_entry(mddev->disks.next,
4018 struct md_rdev, same_set);
4019 err = super_types[mddev->major_version]
4020 .load_super(rdev, rdev0, mddev->minor_version);
4024 } else if (mddev->external)
4025 rdev = md_import_device(dev, -2, -1);
4027 rdev = md_import_device(dev, -1, -1);
4030 return PTR_ERR(rdev);
4031 err = bind_rdev_to_array(rdev, mddev);
4035 return err ? err : len;
4038 static struct md_sysfs_entry md_new_device =
4039 __ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
4042 bitmap_store(struct mddev *mddev, const char *buf, size_t len)
4045 unsigned long chunk, end_chunk;
4049 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
4051 chunk = end_chunk = simple_strtoul(buf, &end, 0);
4052 if (buf == end) break;
4053 if (*end == '-') { /* range */
4055 end_chunk = simple_strtoul(buf, &end, 0);
4056 if (buf == end) break;
4058 if (*end && !isspace(*end)) break;
4059 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
4060 buf = skip_spaces(end);
4062 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
4067 static struct md_sysfs_entry md_bitmap =
4068 __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
4071 size_show(struct mddev *mddev, char *page)
4073 return sprintf(page, "%llu\n",
4074 (unsigned long long)mddev->dev_sectors / 2);
4077 static int update_size(struct mddev *mddev, sector_t num_sectors);
4080 size_store(struct mddev *mddev, const char *buf, size_t len)
4082 /* If array is inactive, we can reduce the component size, but
4083 * not increase it (except from 0).
4084 * If array is active, we can try an on-line resize
4087 int err = strict_blocks_to_sectors(buf, §ors);
4092 err = update_size(mddev, sectors);
4093 md_update_sb(mddev, 1);
4095 if (mddev->dev_sectors == 0 ||
4096 mddev->dev_sectors > sectors)
4097 mddev->dev_sectors = sectors;
4101 return err ? err : len;
4104 static struct md_sysfs_entry md_size =
4105 __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
4108 /* Metadata version.
4110 * 'none' for arrays with no metadata (good luck...)
4111 * 'external' for arrays with externally managed metadata,
4112 * or N.M for internally known formats
4115 metadata_show(struct mddev *mddev, char *page)
4117 if (mddev->persistent)
4118 return sprintf(page, "%d.%d\n",
4119 mddev->major_version, mddev->minor_version);
4120 else if (mddev->external)
4121 return sprintf(page, "external:%s\n", mddev->metadata_type);
4123 return sprintf(page, "none\n");
4127 metadata_store(struct mddev *mddev, const char *buf, size_t len)
4131 /* Changing the details of 'external' metadata is
4132 * always permitted. Otherwise there must be
4133 * no devices attached to the array.
4135 if (mddev->external && strncmp(buf, "external:", 9) == 0)
4137 else if (!list_empty(&mddev->disks))
4140 if (cmd_match(buf, "none")) {
4141 mddev->persistent = 0;
4142 mddev->external = 0;
4143 mddev->major_version = 0;
4144 mddev->minor_version = 90;
4147 if (strncmp(buf, "external:", 9) == 0) {
4148 size_t namelen = len-9;
4149 if (namelen >= sizeof(mddev->metadata_type))
4150 namelen = sizeof(mddev->metadata_type)-1;
4151 strncpy(mddev->metadata_type, buf+9, namelen);
4152 mddev->metadata_type[namelen] = 0;
4153 if (namelen && mddev->metadata_type[namelen-1] == '\n')
4154 mddev->metadata_type[--namelen] = 0;
4155 mddev->persistent = 0;
4156 mddev->external = 1;
4157 mddev->major_version = 0;
4158 mddev->minor_version = 90;
4161 major = simple_strtoul(buf, &e, 10);
4162 if (e==buf || *e != '.')
4165 minor = simple_strtoul(buf, &e, 10);
4166 if (e==buf || (*e && *e != '\n') )
4168 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
4170 mddev->major_version = major;
4171 mddev->minor_version = minor;
4172 mddev->persistent = 1;
4173 mddev->external = 0;
4177 static struct md_sysfs_entry md_metadata =
4178 __ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
4181 action_show(struct mddev *mddev, char *page)
4183 char *type = "idle";
4184 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
4186 else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
4187 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
4188 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4190 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
4191 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
4193 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
4197 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
4200 return sprintf(page, "%s\n", type);
4204 action_store(struct mddev *mddev, const char *page, size_t len)
4206 if (!mddev->pers || !mddev->pers->sync_request)
4209 if (cmd_match(page, "frozen"))
4210 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4212 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4214 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
4215 if (mddev->sync_thread) {
4216 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4217 md_reap_sync_thread(mddev);
4219 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
4220 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
4222 else if (cmd_match(page, "resync"))
4223 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4224 else if (cmd_match(page, "recover")) {
4225 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
4226 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4227 } else if (cmd_match(page, "reshape")) {
4229 if (mddev->pers->start_reshape == NULL)
4231 err = mddev->pers->start_reshape(mddev);
4234 sysfs_notify(&mddev->kobj, NULL, "degraded");
4236 if (cmd_match(page, "check"))
4237 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
4238 else if (!cmd_match(page, "repair"))
4240 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
4241 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4243 if (mddev->ro == 2) {
4244 /* A write to sync_action is enough to justify
4245 * canceling read-auto mode
4248 md_wakeup_thread(mddev->sync_thread);
4250 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4251 md_wakeup_thread(mddev->thread);
4252 sysfs_notify_dirent_safe(mddev->sysfs_action);
4256 static struct md_sysfs_entry md_scan_mode =
4257 __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
4260 last_sync_action_show(struct mddev *mddev, char *page)
4262 return sprintf(page, "%s\n", mddev->last_sync_action);
4265 static struct md_sysfs_entry md_last_scan_mode = __ATTR_RO(last_sync_action);
4268 mismatch_cnt_show(struct mddev *mddev, char *page)
4270 return sprintf(page, "%llu\n",
4271 (unsigned long long)
4272 atomic64_read(&mddev->resync_mismatches));
4275 static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
4278 sync_min_show(struct mddev *mddev, char *page)
4280 return sprintf(page, "%d (%s)\n", speed_min(mddev),
4281 mddev->sync_speed_min ? "local": "system");
4285 sync_min_store(struct mddev *mddev, const char *buf, size_t len)
4289 if (strncmp(buf, "system", 6)==0) {
4290 mddev->sync_speed_min = 0;
4293 min = simple_strtoul(buf, &e, 10);
4294 if (buf == e || (*e && *e != '\n') || min <= 0)
4296 mddev->sync_speed_min = min;
4300 static struct md_sysfs_entry md_sync_min =
4301 __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
4304 sync_max_show(struct mddev *mddev, char *page)
4306 return sprintf(page, "%d (%s)\n", speed_max(mddev),
4307 mddev->sync_speed_max ? "local": "system");
4311 sync_max_store(struct mddev *mddev, const char *buf, size_t len)
4315 if (strncmp(buf, "system", 6)==0) {
4316 mddev->sync_speed_max = 0;
4319 max = simple_strtoul(buf, &e, 10);
4320 if (buf == e || (*e && *e != '\n') || max <= 0)
4322 mddev->sync_speed_max = max;
4326 static struct md_sysfs_entry md_sync_max =
4327 __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
4330 degraded_show(struct mddev *mddev, char *page)
4332 return sprintf(page, "%d\n", mddev->degraded);
4334 static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
4337 sync_force_parallel_show(struct mddev *mddev, char *page)
4339 return sprintf(page, "%d\n", mddev->parallel_resync);
4343 sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len)
4347 if (kstrtol(buf, 10, &n))
4350 if (n != 0 && n != 1)
4353 mddev->parallel_resync = n;
4355 if (mddev->sync_thread)
4356 wake_up(&resync_wait);
4361 /* force parallel resync, even with shared block devices */
4362 static struct md_sysfs_entry md_sync_force_parallel =
4363 __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
4364 sync_force_parallel_show, sync_force_parallel_store);
4367 sync_speed_show(struct mddev *mddev, char *page)
4369 unsigned long resync, dt, db;
4370 if (mddev->curr_resync == 0)
4371 return sprintf(page, "none\n");
4372 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
4373 dt = (jiffies - mddev->resync_mark) / HZ;
4375 db = resync - mddev->resync_mark_cnt;
4376 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
4379 static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
4382 sync_completed_show(struct mddev *mddev, char *page)
4384 unsigned long long max_sectors, resync;
4386 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4387 return sprintf(page, "none\n");
4389 if (mddev->curr_resync == 1 ||
4390 mddev->curr_resync == 2)
4391 return sprintf(page, "delayed\n");
4393 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
4394 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4395 max_sectors = mddev->resync_max_sectors;
4397 max_sectors = mddev->dev_sectors;
4399 resync = mddev->curr_resync_completed;
4400 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
4403 static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
4406 min_sync_show(struct mddev *mddev, char *page)
4408 return sprintf(page, "%llu\n",
4409 (unsigned long long)mddev->resync_min);
4412 min_sync_store(struct mddev *mddev, const char *buf, size_t len)
4414 unsigned long long min;
4415 if (kstrtoull(buf, 10, &min))
4417 if (min > mddev->resync_max)
4419 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4422 /* Must be a multiple of chunk_size */
4423 if (mddev->chunk_sectors) {
4424 sector_t temp = min;
4425 if (sector_div(temp, mddev->chunk_sectors))
4428 mddev->resync_min = min;
4433 static struct md_sysfs_entry md_min_sync =
4434 __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
4437 max_sync_show(struct mddev *mddev, char *page)
4439 if (mddev->resync_max == MaxSector)
4440 return sprintf(page, "max\n");
4442 return sprintf(page, "%llu\n",
4443 (unsigned long long)mddev->resync_max);
4446 max_sync_store(struct mddev *mddev, const char *buf, size_t len)
4448 if (strncmp(buf, "max", 3) == 0)
4449 mddev->resync_max = MaxSector;
4451 unsigned long long max;
4452 if (kstrtoull(buf, 10, &max))
4454 if (max < mddev->resync_min)
4456 if (max < mddev->resync_max &&
4458 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4461 /* Must be a multiple of chunk_size */
4462 if (mddev->chunk_sectors) {
4463 sector_t temp = max;
4464 if (sector_div(temp, mddev->chunk_sectors))
4467 mddev->resync_max = max;
4469 wake_up(&mddev->recovery_wait);
4473 static struct md_sysfs_entry md_max_sync =
4474 __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4477 suspend_lo_show(struct mddev *mddev, char *page)
4479 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4483 suspend_lo_store(struct mddev *mddev, const char *buf, size_t len)
4486 unsigned long long new = simple_strtoull(buf, &e, 10);
4487 unsigned long long old = mddev->suspend_lo;
4489 if (mddev->pers == NULL ||
4490 mddev->pers->quiesce == NULL)
4492 if (buf == e || (*e && *e != '\n'))
4495 mddev->suspend_lo = new;
4497 /* Shrinking suspended region */
4498 mddev->pers->quiesce(mddev, 2);
4500 /* Expanding suspended region - need to wait */
4501 mddev->pers->quiesce(mddev, 1);
4502 mddev->pers->quiesce(mddev, 0);
4506 static struct md_sysfs_entry md_suspend_lo =
4507 __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4511 suspend_hi_show(struct mddev *mddev, char *page)
4513 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4517 suspend_hi_store(struct mddev *mddev, const char *buf, size_t len)
4520 unsigned long long new = simple_strtoull(buf, &e, 10);
4521 unsigned long long old = mddev->suspend_hi;
4523 if (mddev->pers == NULL ||
4524 mddev->pers->quiesce == NULL)
4526 if (buf == e || (*e && *e != '\n'))
4529 mddev->suspend_hi = new;
4531 /* Shrinking suspended region */
4532 mddev->pers->quiesce(mddev, 2);
4534 /* Expanding suspended region - need to wait */
4535 mddev->pers->quiesce(mddev, 1);
4536 mddev->pers->quiesce(mddev, 0);
4540 static struct md_sysfs_entry md_suspend_hi =
4541 __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4544 reshape_position_show(struct mddev *mddev, char *page)
4546 if (mddev->reshape_position != MaxSector)
4547 return sprintf(page, "%llu\n",
4548 (unsigned long long)mddev->reshape_position);
4549 strcpy(page, "none\n");
4554 reshape_position_store(struct mddev *mddev, const char *buf, size_t len)
4556 struct md_rdev *rdev;
4558 unsigned long long new = simple_strtoull(buf, &e, 10);
4561 if (buf == e || (*e && *e != '\n'))
4563 mddev->reshape_position = new;
4564 mddev->delta_disks = 0;
4565 mddev->reshape_backwards = 0;
4566 mddev->new_level = mddev->level;
4567 mddev->new_layout = mddev->layout;
4568 mddev->new_chunk_sectors = mddev->chunk_sectors;
4569 rdev_for_each(rdev, mddev)
4570 rdev->new_data_offset = rdev->data_offset;
4574 static struct md_sysfs_entry md_reshape_position =
4575 __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4576 reshape_position_store);
4579 reshape_direction_show(struct mddev *mddev, char *page)
4581 return sprintf(page, "%s\n",
4582 mddev->reshape_backwards ? "backwards" : "forwards");
4586 reshape_direction_store(struct mddev *mddev, const char *buf, size_t len)
4589 if (cmd_match(buf, "forwards"))
4591 else if (cmd_match(buf, "backwards"))
4595 if (mddev->reshape_backwards == backwards)
4598 /* check if we are allowed to change */
4599 if (mddev->delta_disks)
4602 if (mddev->persistent &&
4603 mddev->major_version == 0)
4606 mddev->reshape_backwards = backwards;
4610 static struct md_sysfs_entry md_reshape_direction =
4611 __ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show,
4612 reshape_direction_store);
4615 array_size_show(struct mddev *mddev, char *page)
4617 if (mddev->external_size)
4618 return sprintf(page, "%llu\n",
4619 (unsigned long long)mddev->array_sectors/2);
4621 return sprintf(page, "default\n");
4625 array_size_store(struct mddev *mddev, const char *buf, size_t len)
4629 if (strncmp(buf, "default", 7) == 0) {
4631 sectors = mddev->pers->size(mddev, 0, 0);
4633 sectors = mddev->array_sectors;
4635 mddev->external_size = 0;
4637 if (strict_blocks_to_sectors(buf, §ors) < 0)
4639 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
4642 mddev->external_size = 1;
4645 mddev->array_sectors = sectors;
4647 set_capacity(mddev->gendisk, mddev->array_sectors);
4648 revalidate_disk(mddev->gendisk);
4653 static struct md_sysfs_entry md_array_size =
4654 __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4657 static struct attribute *md_default_attrs[] = {
4660 &md_raid_disks.attr,
4661 &md_chunk_size.attr,
4663 &md_resync_start.attr,
4665 &md_new_device.attr,
4666 &md_safe_delay.attr,
4667 &md_array_state.attr,
4668 &md_reshape_position.attr,
4669 &md_reshape_direction.attr,
4670 &md_array_size.attr,
4671 &max_corr_read_errors.attr,
4675 static struct attribute *md_redundancy_attrs[] = {
4677 &md_last_scan_mode.attr,
4678 &md_mismatches.attr,
4681 &md_sync_speed.attr,
4682 &md_sync_force_parallel.attr,
4683 &md_sync_completed.attr,
4686 &md_suspend_lo.attr,
4687 &md_suspend_hi.attr,
4692 static struct attribute_group md_redundancy_group = {
4694 .attrs = md_redundancy_attrs,
4699 md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4701 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4702 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
4707 spin_lock(&all_mddevs_lock);
4708 if (list_empty(&mddev->all_mddevs)) {
4709 spin_unlock(&all_mddevs_lock);
4713 spin_unlock(&all_mddevs_lock);
4715 rv = mddev_lock(mddev);
4717 rv = entry->show(mddev, page);
4718 mddev_unlock(mddev);
4725 md_attr_store(struct kobject *kobj, struct attribute *attr,
4726 const char *page, size_t length)
4728 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4729 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
4734 if (!capable(CAP_SYS_ADMIN))
4736 spin_lock(&all_mddevs_lock);
4737 if (list_empty(&mddev->all_mddevs)) {
4738 spin_unlock(&all_mddevs_lock);
4742 spin_unlock(&all_mddevs_lock);
4743 if (entry->store == new_dev_store)
4744 flush_workqueue(md_misc_wq);
4745 rv = mddev_lock(mddev);
4747 rv = entry->store(mddev, page, length);
4748 mddev_unlock(mddev);
4754 static void md_free(struct kobject *ko)
4756 struct mddev *mddev = container_of(ko, struct mddev, kobj);
4758 if (mddev->sysfs_state)
4759 sysfs_put(mddev->sysfs_state);
4761 if (mddev->gendisk) {
4762 del_gendisk(mddev->gendisk);
4763 put_disk(mddev->gendisk);
4766 blk_cleanup_queue(mddev->queue);
4771 static const struct sysfs_ops md_sysfs_ops = {
4772 .show = md_attr_show,
4773 .store = md_attr_store,
4775 static struct kobj_type md_ktype = {
4777 .sysfs_ops = &md_sysfs_ops,
4778 .default_attrs = md_default_attrs,
4783 static void mddev_delayed_delete(struct work_struct *ws)
4785 struct mddev *mddev = container_of(ws, struct mddev, del_work);
4787 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
4788 kobject_del(&mddev->kobj);
4789 kobject_put(&mddev->kobj);
4792 static int md_alloc(dev_t dev, char *name)
4794 static DEFINE_MUTEX(disks_mutex);
4795 struct mddev *mddev = mddev_find(dev);
4796 struct gendisk *disk;
4805 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4806 shift = partitioned ? MdpMinorShift : 0;
4807 unit = MINOR(mddev->unit) >> shift;
4809 /* wait for any previous instance of this device to be
4810 * completely removed (mddev_delayed_delete).
4812 flush_workqueue(md_misc_wq);
4814 mutex_lock(&disks_mutex);
4820 /* Need to ensure that 'name' is not a duplicate.
4822 struct mddev *mddev2;
4823 spin_lock(&all_mddevs_lock);
4825 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
4826 if (mddev2->gendisk &&
4827 strcmp(mddev2->gendisk->disk_name, name) == 0) {
4828 spin_unlock(&all_mddevs_lock);
4831 spin_unlock(&all_mddevs_lock);
4835 mddev->queue = blk_alloc_queue(GFP_KERNEL);
4838 mddev->queue->queuedata = mddev;
4840 blk_queue_make_request(mddev->queue, md_make_request);
4841 blk_set_stacking_limits(&mddev->queue->limits);
4843 disk = alloc_disk(1 << shift);
4845 blk_cleanup_queue(mddev->queue);
4846 mddev->queue = NULL;
4849 disk->major = MAJOR(mddev->unit);
4850 disk->first_minor = unit << shift;
4852 strcpy(disk->disk_name, name);
4853 else if (partitioned)
4854 sprintf(disk->disk_name, "md_d%d", unit);
4856 sprintf(disk->disk_name, "md%d", unit);
4857 disk->fops = &md_fops;
4858 disk->private_data = mddev;
4859 disk->queue = mddev->queue;
4860 blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA);
4861 /* Allow extended partitions. This makes the
4862 * 'mdp' device redundant, but we can't really
4865 disk->flags |= GENHD_FL_EXT_DEVT;
4866 mddev->gendisk = disk;
4867 /* As soon as we call add_disk(), another thread could get
4868 * through to md_open, so make sure it doesn't get too far
4870 mutex_lock(&mddev->open_mutex);
4873 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
4874 &disk_to_dev(disk)->kobj, "%s", "md");
4876 /* This isn't possible, but as kobject_init_and_add is marked
4877 * __must_check, we must do something with the result
4879 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
4883 if (mddev->kobj.sd &&
4884 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
4885 printk(KERN_DEBUG "pointless warning\n");
4886 mutex_unlock(&mddev->open_mutex);
4888 mutex_unlock(&disks_mutex);
4889 if (!error && mddev->kobj.sd) {
4890 kobject_uevent(&mddev->kobj, KOBJ_ADD);
4891 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
4897 static struct kobject *md_probe(dev_t dev, int *part, void *data)
4899 md_alloc(dev, NULL);
4903 static int add_named_array(const char *val, struct kernel_param *kp)
4905 /* val must be "md_*" where * is not all digits.
4906 * We allocate an array with a large free minor number, and
4907 * set the name to val. val must not already be an active name.
4909 int len = strlen(val);
4910 char buf[DISK_NAME_LEN];
4912 while (len && val[len-1] == '\n')
4914 if (len >= DISK_NAME_LEN)
4916 strlcpy(buf, val, len+1);
4917 if (strncmp(buf, "md_", 3) != 0)
4919 return md_alloc(0, buf);
4922 static void md_safemode_timeout(unsigned long data)
4924 struct mddev *mddev = (struct mddev *) data;
4926 if (!atomic_read(&mddev->writes_pending)) {
4927 mddev->safemode = 1;
4928 if (mddev->external)
4929 sysfs_notify_dirent_safe(mddev->sysfs_state);
4931 md_wakeup_thread(mddev->thread);
4934 static int start_dirty_degraded;
4936 int md_run(struct mddev *mddev)
4939 struct md_rdev *rdev;
4940 struct md_personality *pers;
4942 if (list_empty(&mddev->disks))
4943 /* cannot run an array with no devices.. */
4948 /* Cannot run until previous stop completes properly */
4949 if (mddev->sysfs_active)
4953 * Analyze all RAID superblock(s)
4955 if (!mddev->raid_disks) {
4956 if (!mddev->persistent)
4961 if (mddev->level != LEVEL_NONE)
4962 request_module("md-level-%d", mddev->level);
4963 else if (mddev->clevel[0])
4964 request_module("md-%s", mddev->clevel);
4967 * Drop all container device buffers, from now on
4968 * the only valid external interface is through the md
4971 rdev_for_each(rdev, mddev) {
4972 if (test_bit(Faulty, &rdev->flags))
4974 sync_blockdev(rdev->bdev);
4975 invalidate_bdev(rdev->bdev);
4977 /* perform some consistency tests on the device.
4978 * We don't want the data to overlap the metadata,
4979 * Internal Bitmap issues have been handled elsewhere.
4981 if (rdev->meta_bdev) {
4982 /* Nothing to check */;
4983 } else if (rdev->data_offset < rdev->sb_start) {
4984 if (mddev->dev_sectors &&
4985 rdev->data_offset + mddev->dev_sectors
4987 printk("md: %s: data overlaps metadata\n",
4992 if (rdev->sb_start + rdev->sb_size/512
4993 > rdev->data_offset) {
4994 printk("md: %s: metadata overlaps data\n",
4999 sysfs_notify_dirent_safe(rdev->sysfs_state);
5002 if (mddev->bio_set == NULL)
5003 mddev->bio_set = bioset_create(BIO_POOL_SIZE, 0);
5005 spin_lock(&pers_lock);
5006 pers = find_pers(mddev->level, mddev->clevel);
5007 if (!pers || !try_module_get(pers->owner)) {
5008 spin_unlock(&pers_lock);
5009 if (mddev->level != LEVEL_NONE)
5010 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
5013 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
5018 spin_unlock(&pers_lock);
5019 if (mddev->level != pers->level) {
5020 mddev->level = pers->level;
5021 mddev->new_level = pers->level;
5023 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
5025 if (mddev->reshape_position != MaxSector &&
5026 pers->start_reshape == NULL) {
5027 /* This personality cannot handle reshaping... */
5029 module_put(pers->owner);
5033 if (pers->sync_request) {
5034 /* Warn if this is a potentially silly
5037 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
5038 struct md_rdev *rdev2;
5041 rdev_for_each(rdev, mddev)
5042 rdev_for_each(rdev2, mddev) {
5044 rdev->bdev->bd_contains ==
5045 rdev2->bdev->bd_contains) {
5047 "%s: WARNING: %s appears to be"
5048 " on the same physical disk as"
5051 bdevname(rdev->bdev,b),
5052 bdevname(rdev2->bdev,b2));
5059 "True protection against single-disk"
5060 " failure might be compromised.\n");
5063 mddev->recovery = 0;
5064 /* may be over-ridden by personality */
5065 mddev->resync_max_sectors = mddev->dev_sectors;
5067 mddev->ok_start_degraded = start_dirty_degraded;
5069 if (start_readonly && mddev->ro == 0)
5070 mddev->ro = 2; /* read-only, but switch on first write */
5072 err = mddev->pers->run(mddev);
5074 printk(KERN_ERR "md: pers->run() failed ...\n");
5075 else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
5076 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
5077 " but 'external_size' not in effect?\n", __func__);
5079 "md: invalid array_size %llu > default size %llu\n",
5080 (unsigned long long)mddev->array_sectors / 2,
5081 (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
5083 mddev->pers->stop(mddev);
5085 if (err == 0 && mddev->pers->sync_request &&
5086 (mddev->bitmap_info.file || mddev->bitmap_info.offset)) {
5087 err = bitmap_create(mddev);
5089 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
5090 mdname(mddev), err);
5091 mddev->pers->stop(mddev);
5095 module_put(mddev->pers->owner);
5097 bitmap_destroy(mddev);
5100 if (mddev->pers->sync_request) {
5101 if (mddev->kobj.sd &&
5102 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
5104 "md: cannot register extra attributes for %s\n",
5106 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
5107 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
5110 atomic_set(&mddev->writes_pending,0);
5111 atomic_set(&mddev->max_corr_read_errors,
5112 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
5113 mddev->safemode = 0;
5114 mddev->safemode_timer.function = md_safemode_timeout;
5115 mddev->safemode_timer.data = (unsigned long) mddev;
5116 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
5120 rdev_for_each(rdev, mddev)
5121 if (rdev->raid_disk >= 0)
5122 if (sysfs_link_rdev(mddev, rdev))
5123 /* failure here is OK */;
5125 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5127 if (mddev->flags & MD_UPDATE_SB_FLAGS)
5128 md_update_sb(mddev, 0);
5130 md_new_event(mddev);
5131 sysfs_notify_dirent_safe(mddev->sysfs_state);
5132 sysfs_notify_dirent_safe(mddev->sysfs_action);
5133 sysfs_notify(&mddev->kobj, NULL, "degraded");
5136 EXPORT_SYMBOL_GPL(md_run);
5138 static int do_md_run(struct mddev *mddev)
5142 err = md_run(mddev);
5145 err = bitmap_load(mddev);
5147 bitmap_destroy(mddev);
5151 md_wakeup_thread(mddev->thread);
5152 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
5154 set_capacity(mddev->gendisk, mddev->array_sectors);
5155 revalidate_disk(mddev->gendisk);
5157 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
5162 static int restart_array(struct mddev *mddev)
5164 struct gendisk *disk = mddev->gendisk;
5166 /* Complain if it has no devices */
5167 if (list_empty(&mddev->disks))
5173 mddev->safemode = 0;
5175 set_disk_ro(disk, 0);
5176 printk(KERN_INFO "md: %s switched to read-write mode.\n",
5178 /* Kick recovery or resync if necessary */
5179 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5180 md_wakeup_thread(mddev->thread);
5181 md_wakeup_thread(mddev->sync_thread);
5182 sysfs_notify_dirent_safe(mddev->sysfs_state);
5186 /* similar to deny_write_access, but accounts for our holding a reference
5187 * to the file ourselves */
5188 static int deny_bitmap_write_access(struct file * file)
5190 struct inode *inode = file->f_mapping->host;
5192 spin_lock(&inode->i_lock);
5193 if (atomic_read(&inode->i_writecount) > 1) {
5194 spin_unlock(&inode->i_lock);
5197 atomic_set(&inode->i_writecount, -1);
5198 spin_unlock(&inode->i_lock);
5203 void restore_bitmap_write_access(struct file *file)
5205 struct inode *inode = file->f_mapping->host;
5207 spin_lock(&inode->i_lock);
5208 atomic_set(&inode->i_writecount, 1);
5209 spin_unlock(&inode->i_lock);
5212 static void md_clean(struct mddev *mddev)
5214 mddev->array_sectors = 0;
5215 mddev->external_size = 0;
5216 mddev->dev_sectors = 0;
5217 mddev->raid_disks = 0;
5218 mddev->recovery_cp = 0;
5219 mddev->resync_min = 0;
5220 mddev->resync_max = MaxSector;
5221 mddev->reshape_position = MaxSector;
5222 mddev->external = 0;
5223 mddev->persistent = 0;
5224 mddev->level = LEVEL_NONE;
5225 mddev->clevel[0] = 0;
5228 mddev->metadata_type[0] = 0;
5229 mddev->chunk_sectors = 0;
5230 mddev->ctime = mddev->utime = 0;
5232 mddev->max_disks = 0;
5234 mddev->can_decrease_events = 0;
5235 mddev->delta_disks = 0;
5236 mddev->reshape_backwards = 0;
5237 mddev->new_level = LEVEL_NONE;
5238 mddev->new_layout = 0;
5239 mddev->new_chunk_sectors = 0;
5240 mddev->curr_resync = 0;
5241 atomic64_set(&mddev->resync_mismatches, 0);
5242 mddev->suspend_lo = mddev->suspend_hi = 0;
5243 mddev->sync_speed_min = mddev->sync_speed_max = 0;
5244 mddev->recovery = 0;
5247 mddev->degraded = 0;
5248 mddev->safemode = 0;
5249 mddev->merge_check_needed = 0;
5250 mddev->bitmap_info.offset = 0;
5251 mddev->bitmap_info.default_offset = 0;
5252 mddev->bitmap_info.default_space = 0;
5253 mddev->bitmap_info.chunksize = 0;
5254 mddev->bitmap_info.daemon_sleep = 0;
5255 mddev->bitmap_info.max_write_behind = 0;
5258 static void __md_stop_writes(struct mddev *mddev)
5260 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5261 if (mddev->sync_thread) {
5262 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5263 md_reap_sync_thread(mddev);
5266 del_timer_sync(&mddev->safemode_timer);
5268 bitmap_flush(mddev);
5269 md_super_wait(mddev);
5271 if (mddev->ro == 0 &&
5272 (!mddev->in_sync || (mddev->flags & MD_UPDATE_SB_FLAGS))) {
5273 /* mark array as shutdown cleanly */
5275 md_update_sb(mddev, 1);
5279 void md_stop_writes(struct mddev *mddev)
5281 mddev_lock_nointr(mddev);
5282 __md_stop_writes(mddev);
5283 mddev_unlock(mddev);
5285 EXPORT_SYMBOL_GPL(md_stop_writes);
5287 static void __md_stop(struct mddev *mddev)
5290 mddev->pers->stop(mddev);
5291 if (mddev->pers->sync_request && mddev->to_remove == NULL)
5292 mddev->to_remove = &md_redundancy_group;
5293 module_put(mddev->pers->owner);
5295 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5298 void md_stop(struct mddev *mddev)
5300 /* stop the array and free an attached data structures.
5301 * This is called from dm-raid
5304 bitmap_destroy(mddev);
5306 bioset_free(mddev->bio_set);
5309 EXPORT_SYMBOL_GPL(md_stop);
5311 static int md_set_readonly(struct mddev *mddev, struct block_device *bdev)
5316 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5318 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5319 md_wakeup_thread(mddev->thread);
5321 if (mddev->sync_thread) {
5322 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5323 /* Thread might be blocked waiting for metadata update
5324 * which will now never happen */
5325 wake_up_process(mddev->sync_thread->tsk);
5327 mddev_unlock(mddev);
5328 wait_event(resync_wait, mddev->sync_thread == NULL);
5329 mddev_lock_nointr(mddev);
5331 mutex_lock(&mddev->open_mutex);
5332 if (atomic_read(&mddev->openers) > !!bdev ||
5333 mddev->sync_thread ||
5334 (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) {
5335 printk("md: %s still in use.\n",mdname(mddev));
5337 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5338 md_wakeup_thread(mddev->thread);
5344 __md_stop_writes(mddev);
5350 set_disk_ro(mddev->gendisk, 1);
5351 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5352 sysfs_notify_dirent_safe(mddev->sysfs_state);
5356 mutex_unlock(&mddev->open_mutex);
5361 * 0 - completely stop and dis-assemble array
5362 * 2 - stop but do not disassemble array
5364 static int do_md_stop(struct mddev * mddev, int mode,
5365 struct block_device *bdev)
5367 struct gendisk *disk = mddev->gendisk;
5368 struct md_rdev *rdev;
5371 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5373 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5374 md_wakeup_thread(mddev->thread);
5376 if (mddev->sync_thread) {
5377 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5378 /* Thread might be blocked waiting for metadata update
5379 * which will now never happen */
5380 wake_up_process(mddev->sync_thread->tsk);
5382 mddev_unlock(mddev);
5383 wait_event(resync_wait, mddev->sync_thread == NULL);
5384 mddev_lock_nointr(mddev);
5386 mutex_lock(&mddev->open_mutex);
5387 if (atomic_read(&mddev->openers) > !!bdev ||
5388 mddev->sysfs_active ||
5389 mddev->sync_thread ||
5390 (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) {
5391 printk("md: %s still in use.\n",mdname(mddev));
5392 mutex_unlock(&mddev->open_mutex);
5394 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5395 md_wakeup_thread(mddev->thread);
5401 set_disk_ro(disk, 0);
5403 __md_stop_writes(mddev);
5405 mddev->queue->merge_bvec_fn = NULL;
5406 mddev->queue->backing_dev_info.congested_fn = NULL;
5408 /* tell userspace to handle 'inactive' */
5409 sysfs_notify_dirent_safe(mddev->sysfs_state);
5411 rdev_for_each(rdev, mddev)
5412 if (rdev->raid_disk >= 0)
5413 sysfs_unlink_rdev(mddev, rdev);
5415 set_capacity(disk, 0);
5416 mutex_unlock(&mddev->open_mutex);
5418 revalidate_disk(disk);
5423 mutex_unlock(&mddev->open_mutex);
5425 * Free resources if final stop
5428 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
5430 bitmap_destroy(mddev);
5431 if (mddev->bitmap_info.file) {
5432 restore_bitmap_write_access(mddev->bitmap_info.file);
5433 fput(mddev->bitmap_info.file);
5434 mddev->bitmap_info.file = NULL;
5436 mddev->bitmap_info.offset = 0;
5438 export_array(mddev);
5441 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
5442 if (mddev->hold_active == UNTIL_STOP)
5443 mddev->hold_active = 0;
5445 blk_integrity_unregister(disk);
5446 md_new_event(mddev);
5447 sysfs_notify_dirent_safe(mddev->sysfs_state);
5452 static void autorun_array(struct mddev *mddev)
5454 struct md_rdev *rdev;
5457 if (list_empty(&mddev->disks))
5460 printk(KERN_INFO "md: running: ");
5462 rdev_for_each(rdev, mddev) {
5463 char b[BDEVNAME_SIZE];
5464 printk("<%s>", bdevname(rdev->bdev,b));
5468 err = do_md_run(mddev);
5470 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
5471 do_md_stop(mddev, 0, NULL);
5476 * lets try to run arrays based on all disks that have arrived
5477 * until now. (those are in pending_raid_disks)
5479 * the method: pick the first pending disk, collect all disks with
5480 * the same UUID, remove all from the pending list and put them into
5481 * the 'same_array' list. Then order this list based on superblock
5482 * update time (freshest comes first), kick out 'old' disks and
5483 * compare superblocks. If everything's fine then run it.
5485 * If "unit" is allocated, then bump its reference count
5487 static void autorun_devices(int part)
5489 struct md_rdev *rdev0, *rdev, *tmp;
5490 struct mddev *mddev;
5491 char b[BDEVNAME_SIZE];
5493 printk(KERN_INFO "md: autorun ...\n");
5494 while (!list_empty(&pending_raid_disks)) {
5497 LIST_HEAD(candidates);
5498 rdev0 = list_entry(pending_raid_disks.next,
5499 struct md_rdev, same_set);
5501 printk(KERN_INFO "md: considering %s ...\n",
5502 bdevname(rdev0->bdev,b));
5503 INIT_LIST_HEAD(&candidates);
5504 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
5505 if (super_90_load(rdev, rdev0, 0) >= 0) {
5506 printk(KERN_INFO "md: adding %s ...\n",
5507 bdevname(rdev->bdev,b));
5508 list_move(&rdev->same_set, &candidates);
5511 * now we have a set of devices, with all of them having
5512 * mostly sane superblocks. It's time to allocate the
5516 dev = MKDEV(mdp_major,
5517 rdev0->preferred_minor << MdpMinorShift);
5518 unit = MINOR(dev) >> MdpMinorShift;
5520 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
5523 if (rdev0->preferred_minor != unit) {
5524 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
5525 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
5529 md_probe(dev, NULL, NULL);
5530 mddev = mddev_find(dev);
5531 if (!mddev || !mddev->gendisk) {
5535 "md: cannot allocate memory for md drive.\n");
5538 if (mddev_lock(mddev))
5539 printk(KERN_WARNING "md: %s locked, cannot run\n",
5541 else if (mddev->raid_disks || mddev->major_version
5542 || !list_empty(&mddev->disks)) {
5544 "md: %s already running, cannot run %s\n",
5545 mdname(mddev), bdevname(rdev0->bdev,b));
5546 mddev_unlock(mddev);
5548 printk(KERN_INFO "md: created %s\n", mdname(mddev));
5549 mddev->persistent = 1;
5550 rdev_for_each_list(rdev, tmp, &candidates) {
5551 list_del_init(&rdev->same_set);
5552 if (bind_rdev_to_array(rdev, mddev))
5555 autorun_array(mddev);
5556 mddev_unlock(mddev);
5558 /* on success, candidates will be empty, on error
5561 rdev_for_each_list(rdev, tmp, &candidates) {
5562 list_del_init(&rdev->same_set);
5567 printk(KERN_INFO "md: ... autorun DONE.\n");
5569 #endif /* !MODULE */
5571 static int get_version(void __user * arg)
5575 ver.major = MD_MAJOR_VERSION;
5576 ver.minor = MD_MINOR_VERSION;
5577 ver.patchlevel = MD_PATCHLEVEL_VERSION;
5579 if (copy_to_user(arg, &ver, sizeof(ver)))
5585 static int get_array_info(struct mddev * mddev, void __user * arg)
5587 mdu_array_info_t info;
5588 int nr,working,insync,failed,spare;
5589 struct md_rdev *rdev;
5591 nr = working = insync = failed = spare = 0;
5593 rdev_for_each_rcu(rdev, mddev) {
5595 if (test_bit(Faulty, &rdev->flags))
5599 if (test_bit(In_sync, &rdev->flags))
5607 info.major_version = mddev->major_version;
5608 info.minor_version = mddev->minor_version;
5609 info.patch_version = MD_PATCHLEVEL_VERSION;
5610 info.ctime = mddev->ctime;
5611 info.level = mddev->level;
5612 info.size = mddev->dev_sectors / 2;
5613 if (info.size != mddev->dev_sectors / 2) /* overflow */
5616 info.raid_disks = mddev->raid_disks;
5617 info.md_minor = mddev->md_minor;
5618 info.not_persistent= !mddev->persistent;
5620 info.utime = mddev->utime;
5623 info.state = (1<<MD_SB_CLEAN);
5624 if (mddev->bitmap && mddev->bitmap_info.offset)
5625 info.state = (1<<MD_SB_BITMAP_PRESENT);
5626 info.active_disks = insync;
5627 info.working_disks = working;
5628 info.failed_disks = failed;
5629 info.spare_disks = spare;
5631 info.layout = mddev->layout;
5632 info.chunk_size = mddev->chunk_sectors << 9;
5634 if (copy_to_user(arg, &info, sizeof(info)))
5640 static int get_bitmap_file(struct mddev * mddev, void __user * arg)
5642 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
5643 char *ptr, *buf = NULL;
5646 file = kmalloc(sizeof(*file), GFP_NOIO);
5651 /* bitmap disabled, zero the first byte and copy out */
5652 if (!mddev->bitmap || !mddev->bitmap->storage.file) {
5653 file->pathname[0] = '\0';
5657 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
5661 ptr = d_path(&mddev->bitmap->storage.file->f_path,
5662 buf, sizeof(file->pathname));
5666 strcpy(file->pathname, ptr);
5670 if (copy_to_user(arg, file, sizeof(*file)))
5678 static int get_disk_info(struct mddev * mddev, void __user * arg)
5680 mdu_disk_info_t info;
5681 struct md_rdev *rdev;
5683 if (copy_from_user(&info, arg, sizeof(info)))
5687 rdev = find_rdev_nr_rcu(mddev, info.number);
5689 info.major = MAJOR(rdev->bdev->bd_dev);
5690 info.minor = MINOR(rdev->bdev->bd_dev);
5691 info.raid_disk = rdev->raid_disk;
5693 if (test_bit(Faulty, &rdev->flags))
5694 info.state |= (1<<MD_DISK_FAULTY);
5695 else if (test_bit(In_sync, &rdev->flags)) {
5696 info.state |= (1<<MD_DISK_ACTIVE);
5697 info.state |= (1<<MD_DISK_SYNC);
5699 if (test_bit(WriteMostly, &rdev->flags))
5700 info.state |= (1<<MD_DISK_WRITEMOSTLY);
5702 info.major = info.minor = 0;
5703 info.raid_disk = -1;
5704 info.state = (1<<MD_DISK_REMOVED);
5708 if (copy_to_user(arg, &info, sizeof(info)))
5714 static int add_new_disk(struct mddev * mddev, mdu_disk_info_t *info)
5716 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
5717 struct md_rdev *rdev;
5718 dev_t dev = MKDEV(info->major,info->minor);
5720 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
5723 if (!mddev->raid_disks) {
5725 /* expecting a device which has a superblock */
5726 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
5729 "md: md_import_device returned %ld\n",
5731 return PTR_ERR(rdev);
5733 if (!list_empty(&mddev->disks)) {
5734 struct md_rdev *rdev0
5735 = list_entry(mddev->disks.next,
5736 struct md_rdev, same_set);
5737 err = super_types[mddev->major_version]
5738 .load_super(rdev, rdev0, mddev->minor_version);
5741 "md: %s has different UUID to %s\n",
5742 bdevname(rdev->bdev,b),
5743 bdevname(rdev0->bdev,b2));
5748 err = bind_rdev_to_array(rdev, mddev);
5755 * add_new_disk can be used once the array is assembled
5756 * to add "hot spares". They must already have a superblock
5761 if (!mddev->pers->hot_add_disk) {
5763 "%s: personality does not support diskops!\n",
5767 if (mddev->persistent)
5768 rdev = md_import_device(dev, mddev->major_version,
5769 mddev->minor_version);
5771 rdev = md_import_device(dev, -1, -1);
5774 "md: md_import_device returned %ld\n",
5776 return PTR_ERR(rdev);
5778 /* set saved_raid_disk if appropriate */
5779 if (!mddev->persistent) {
5780 if (info->state & (1<<MD_DISK_SYNC) &&
5781 info->raid_disk < mddev->raid_disks) {
5782 rdev->raid_disk = info->raid_disk;
5783 set_bit(In_sync, &rdev->flags);
5784 clear_bit(Bitmap_sync, &rdev->flags);
5786 rdev->raid_disk = -1;
5787 rdev->saved_raid_disk = rdev->raid_disk;
5789 super_types[mddev->major_version].
5790 validate_super(mddev, rdev);
5791 if ((info->state & (1<<MD_DISK_SYNC)) &&
5792 rdev->raid_disk != info->raid_disk) {
5793 /* This was a hot-add request, but events doesn't
5794 * match, so reject it.
5800 clear_bit(In_sync, &rdev->flags); /* just to be sure */
5801 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5802 set_bit(WriteMostly, &rdev->flags);
5804 clear_bit(WriteMostly, &rdev->flags);
5806 rdev->raid_disk = -1;
5807 err = bind_rdev_to_array(rdev, mddev);
5808 if (!err && !mddev->pers->hot_remove_disk) {
5809 /* If there is hot_add_disk but no hot_remove_disk
5810 * then added disks for geometry changes,
5811 * and should be added immediately.
5813 super_types[mddev->major_version].
5814 validate_super(mddev, rdev);
5815 err = mddev->pers->hot_add_disk(mddev, rdev);
5817 unbind_rdev_from_array(rdev);
5822 sysfs_notify_dirent_safe(rdev->sysfs_state);
5824 set_bit(MD_CHANGE_DEVS, &mddev->flags);
5825 if (mddev->degraded)
5826 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
5827 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5829 md_new_event(mddev);
5830 md_wakeup_thread(mddev->thread);
5834 /* otherwise, add_new_disk is only allowed
5835 * for major_version==0 superblocks
5837 if (mddev->major_version != 0) {
5838 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
5843 if (!(info->state & (1<<MD_DISK_FAULTY))) {
5845 rdev = md_import_device(dev, -1, 0);
5848 "md: error, md_import_device() returned %ld\n",
5850 return PTR_ERR(rdev);
5852 rdev->desc_nr = info->number;
5853 if (info->raid_disk < mddev->raid_disks)
5854 rdev->raid_disk = info->raid_disk;
5856 rdev->raid_disk = -1;
5858 if (rdev->raid_disk < mddev->raid_disks)
5859 if (info->state & (1<<MD_DISK_SYNC))
5860 set_bit(In_sync, &rdev->flags);
5862 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5863 set_bit(WriteMostly, &rdev->flags);
5865 if (!mddev->persistent) {
5866 printk(KERN_INFO "md: nonpersistent superblock ...\n");
5867 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
5869 rdev->sb_start = calc_dev_sboffset(rdev);
5870 rdev->sectors = rdev->sb_start;
5872 err = bind_rdev_to_array(rdev, mddev);
5882 static int hot_remove_disk(struct mddev * mddev, dev_t dev)
5884 char b[BDEVNAME_SIZE];
5885 struct md_rdev *rdev;
5887 rdev = find_rdev(mddev, dev);
5891 clear_bit(Blocked, &rdev->flags);
5892 remove_and_add_spares(mddev, rdev);
5894 if (rdev->raid_disk >= 0)
5897 kick_rdev_from_array(rdev);
5898 md_update_sb(mddev, 1);
5899 md_new_event(mddev);
5903 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
5904 bdevname(rdev->bdev,b), mdname(mddev));
5908 static int hot_add_disk(struct mddev * mddev, dev_t dev)
5910 char b[BDEVNAME_SIZE];
5912 struct md_rdev *rdev;
5917 if (mddev->major_version != 0) {
5918 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
5919 " version-0 superblocks.\n",
5923 if (!mddev->pers->hot_add_disk) {
5925 "%s: personality does not support diskops!\n",
5930 rdev = md_import_device(dev, -1, 0);
5933 "md: error, md_import_device() returned %ld\n",
5938 if (mddev->persistent)
5939 rdev->sb_start = calc_dev_sboffset(rdev);
5941 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
5943 rdev->sectors = rdev->sb_start;
5945 if (test_bit(Faulty, &rdev->flags)) {
5947 "md: can not hot-add faulty %s disk to %s!\n",
5948 bdevname(rdev->bdev,b), mdname(mddev));
5952 clear_bit(In_sync, &rdev->flags);
5954 rdev->saved_raid_disk = -1;
5955 err = bind_rdev_to_array(rdev, mddev);
5960 * The rest should better be atomic, we can have disk failures
5961 * noticed in interrupt contexts ...
5964 rdev->raid_disk = -1;
5966 md_update_sb(mddev, 1);
5969 * Kick recovery, maybe this spare has to be added to the
5970 * array immediately.
5972 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5973 md_wakeup_thread(mddev->thread);
5974 md_new_event(mddev);
5982 static int set_bitmap_file(struct mddev *mddev, int fd)
5987 if (!mddev->pers->quiesce)
5989 if (mddev->recovery || mddev->sync_thread)
5991 /* we should be able to change the bitmap.. */
5997 return -EEXIST; /* cannot add when bitmap is present */
5998 mddev->bitmap_info.file = fget(fd);
6000 if (mddev->bitmap_info.file == NULL) {
6001 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
6006 err = deny_bitmap_write_access(mddev->bitmap_info.file);
6008 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
6010 fput(mddev->bitmap_info.file);
6011 mddev->bitmap_info.file = NULL;
6014 mddev->bitmap_info.offset = 0; /* file overrides offset */
6015 } else if (mddev->bitmap == NULL)
6016 return -ENOENT; /* cannot remove what isn't there */
6019 mddev->pers->quiesce(mddev, 1);
6021 err = bitmap_create(mddev);
6023 err = bitmap_load(mddev);
6025 if (fd < 0 || err) {
6026 bitmap_destroy(mddev);
6027 fd = -1; /* make sure to put the file */
6029 mddev->pers->quiesce(mddev, 0);
6032 if (mddev->bitmap_info.file) {
6033 restore_bitmap_write_access(mddev->bitmap_info.file);
6034 fput(mddev->bitmap_info.file);
6036 mddev->bitmap_info.file = NULL;
6043 * set_array_info is used two different ways
6044 * The original usage is when creating a new array.
6045 * In this usage, raid_disks is > 0 and it together with
6046 * level, size, not_persistent,layout,chunksize determine the
6047 * shape of the array.
6048 * This will always create an array with a type-0.90.0 superblock.
6049 * The newer usage is when assembling an array.
6050 * In this case raid_disks will be 0, and the major_version field is
6051 * use to determine which style super-blocks are to be found on the devices.
6052 * The minor and patch _version numbers are also kept incase the
6053 * super_block handler wishes to interpret them.
6055 static int set_array_info(struct mddev * mddev, mdu_array_info_t *info)
6058 if (info->raid_disks == 0) {
6059 /* just setting version number for superblock loading */
6060 if (info->major_version < 0 ||
6061 info->major_version >= ARRAY_SIZE(super_types) ||
6062 super_types[info->major_version].name == NULL) {
6063 /* maybe try to auto-load a module? */
6065 "md: superblock version %d not known\n",
6066 info->major_version);
6069 mddev->major_version = info->major_version;
6070 mddev->minor_version = info->minor_version;
6071 mddev->patch_version = info->patch_version;
6072 mddev->persistent = !info->not_persistent;
6073 /* ensure mddev_put doesn't delete this now that there
6074 * is some minimal configuration.
6076 mddev->ctime = get_seconds();
6079 mddev->major_version = MD_MAJOR_VERSION;
6080 mddev->minor_version = MD_MINOR_VERSION;
6081 mddev->patch_version = MD_PATCHLEVEL_VERSION;
6082 mddev->ctime = get_seconds();
6084 mddev->level = info->level;
6085 mddev->clevel[0] = 0;
6086 mddev->dev_sectors = 2 * (sector_t)info->size;
6087 mddev->raid_disks = info->raid_disks;
6088 /* don't set md_minor, it is determined by which /dev/md* was
6091 if (info->state & (1<<MD_SB_CLEAN))
6092 mddev->recovery_cp = MaxSector;
6094 mddev->recovery_cp = 0;
6095 mddev->persistent = ! info->not_persistent;
6096 mddev->external = 0;
6098 mddev->layout = info->layout;
6099 mddev->chunk_sectors = info->chunk_size >> 9;
6101 mddev->max_disks = MD_SB_DISKS;
6103 if (mddev->persistent)
6105 set_bit(MD_CHANGE_DEVS, &mddev->flags);
6107 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
6108 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
6109 mddev->bitmap_info.offset = 0;
6111 mddev->reshape_position = MaxSector;
6114 * Generate a 128 bit UUID
6116 get_random_bytes(mddev->uuid, 16);
6118 mddev->new_level = mddev->level;
6119 mddev->new_chunk_sectors = mddev->chunk_sectors;
6120 mddev->new_layout = mddev->layout;
6121 mddev->delta_disks = 0;
6122 mddev->reshape_backwards = 0;
6127 void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors)
6129 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
6131 if (mddev->external_size)
6134 mddev->array_sectors = array_sectors;
6136 EXPORT_SYMBOL(md_set_array_sectors);
6138 static int update_size(struct mddev *mddev, sector_t num_sectors)
6140 struct md_rdev *rdev;
6142 int fit = (num_sectors == 0);
6144 if (mddev->pers->resize == NULL)
6146 /* The "num_sectors" is the number of sectors of each device that
6147 * is used. This can only make sense for arrays with redundancy.
6148 * linear and raid0 always use whatever space is available. We can only
6149 * consider changing this number if no resync or reconstruction is
6150 * happening, and if the new size is acceptable. It must fit before the
6151 * sb_start or, if that is <data_offset, it must fit before the size
6152 * of each device. If num_sectors is zero, we find the largest size
6155 if (mddev->sync_thread)
6158 rdev_for_each(rdev, mddev) {
6159 sector_t avail = rdev->sectors;
6161 if (fit && (num_sectors == 0 || num_sectors > avail))
6162 num_sectors = avail;
6163 if (avail < num_sectors)
6166 rv = mddev->pers->resize(mddev, num_sectors);
6168 revalidate_disk(mddev->gendisk);
6172 static int update_raid_disks(struct mddev *mddev, int raid_disks)
6175 struct md_rdev *rdev;
6176 /* change the number of raid disks */
6177 if (mddev->pers->check_reshape == NULL)
6179 if (raid_disks <= 0 ||
6180 (mddev->max_disks && raid_disks >= mddev->max_disks))
6182 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
6185 rdev_for_each(rdev, mddev) {
6186 if (mddev->raid_disks < raid_disks &&
6187 rdev->data_offset < rdev->new_data_offset)
6189 if (mddev->raid_disks > raid_disks &&
6190 rdev->data_offset > rdev->new_data_offset)
6194 mddev->delta_disks = raid_disks - mddev->raid_disks;
6195 if (mddev->delta_disks < 0)
6196 mddev->reshape_backwards = 1;
6197 else if (mddev->delta_disks > 0)
6198 mddev->reshape_backwards = 0;
6200 rv = mddev->pers->check_reshape(mddev);
6202 mddev->delta_disks = 0;
6203 mddev->reshape_backwards = 0;
6210 * update_array_info is used to change the configuration of an
6212 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
6213 * fields in the info are checked against the array.
6214 * Any differences that cannot be handled will cause an error.
6215 * Normally, only one change can be managed at a time.
6217 static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
6223 /* calculate expected state,ignoring low bits */
6224 if (mddev->bitmap && mddev->bitmap_info.offset)
6225 state |= (1 << MD_SB_BITMAP_PRESENT);
6227 if (mddev->major_version != info->major_version ||
6228 mddev->minor_version != info->minor_version ||
6229 /* mddev->patch_version != info->patch_version || */
6230 mddev->ctime != info->ctime ||
6231 mddev->level != info->level ||
6232 /* mddev->layout != info->layout || */
6233 !mddev->persistent != info->not_persistent||
6234 mddev->chunk_sectors != info->chunk_size >> 9 ||
6235 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
6236 ((state^info->state) & 0xfffffe00)
6239 /* Check there is only one change */
6240 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
6242 if (mddev->raid_disks != info->raid_disks)
6244 if (mddev->layout != info->layout)
6246 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
6253 if (mddev->layout != info->layout) {
6255 * we don't need to do anything at the md level, the
6256 * personality will take care of it all.
6258 if (mddev->pers->check_reshape == NULL)
6261 mddev->new_layout = info->layout;
6262 rv = mddev->pers->check_reshape(mddev);
6264 mddev->new_layout = mddev->layout;
6268 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
6269 rv = update_size(mddev, (sector_t)info->size * 2);
6271 if (mddev->raid_disks != info->raid_disks)
6272 rv = update_raid_disks(mddev, info->raid_disks);
6274 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
6275 if (mddev->pers->quiesce == NULL)
6277 if (mddev->recovery || mddev->sync_thread)
6279 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
6280 /* add the bitmap */
6283 if (mddev->bitmap_info.default_offset == 0)
6285 mddev->bitmap_info.offset =
6286 mddev->bitmap_info.default_offset;
6287 mddev->bitmap_info.space =
6288 mddev->bitmap_info.default_space;
6289 mddev->pers->quiesce(mddev, 1);
6290 rv = bitmap_create(mddev);
6292 rv = bitmap_load(mddev);
6294 bitmap_destroy(mddev);
6295 mddev->pers->quiesce(mddev, 0);
6297 /* remove the bitmap */
6300 if (mddev->bitmap->storage.file)
6302 mddev->pers->quiesce(mddev, 1);
6303 bitmap_destroy(mddev);
6304 mddev->pers->quiesce(mddev, 0);
6305 mddev->bitmap_info.offset = 0;
6308 md_update_sb(mddev, 1);
6312 static int set_disk_faulty(struct mddev *mddev, dev_t dev)
6314 struct md_rdev *rdev;
6317 if (mddev->pers == NULL)
6321 rdev = find_rdev_rcu(mddev, dev);
6325 md_error(mddev, rdev);
6326 if (!test_bit(Faulty, &rdev->flags))
6334 * We have a problem here : there is no easy way to give a CHS
6335 * virtual geometry. We currently pretend that we have a 2 heads
6336 * 4 sectors (with a BIG number of cylinders...). This drives
6337 * dosfs just mad... ;-)
6339 static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
6341 struct mddev *mddev = bdev->bd_disk->private_data;
6345 geo->cylinders = mddev->array_sectors / 8;
6349 static inline bool md_ioctl_valid(unsigned int cmd)
6354 case GET_ARRAY_INFO:
6355 case GET_BITMAP_FILE:
6358 case HOT_REMOVE_DISK:
6359 case PRINT_RAID_DEBUG:
6362 case RESTART_ARRAY_RW:
6364 case SET_ARRAY_INFO:
6365 case SET_BITMAP_FILE:
6366 case SET_DISK_FAULTY:
6375 static int md_ioctl(struct block_device *bdev, fmode_t mode,
6376 unsigned int cmd, unsigned long arg)
6379 void __user *argp = (void __user *)arg;
6380 struct mddev *mddev = NULL;
6383 if (!md_ioctl_valid(cmd))
6388 case GET_ARRAY_INFO:
6392 if (!capable(CAP_SYS_ADMIN))
6397 * Commands dealing with the RAID driver but not any
6402 err = get_version(argp);
6405 case PRINT_RAID_DEBUG:
6413 autostart_arrays(arg);
6420 * Commands creating/starting a new array:
6423 mddev = bdev->bd_disk->private_data;
6430 /* Some actions do not requires the mutex */
6432 case GET_ARRAY_INFO:
6433 if (!mddev->raid_disks && !mddev->external)
6436 err = get_array_info(mddev, argp);
6440 if (!mddev->raid_disks && !mddev->external)
6443 err = get_disk_info(mddev, argp);
6446 case SET_DISK_FAULTY:
6447 err = set_disk_faulty(mddev, new_decode_dev(arg));
6451 if (cmd == ADD_NEW_DISK)
6452 /* need to ensure md_delayed_delete() has completed */
6453 flush_workqueue(md_misc_wq);
6455 if (cmd == HOT_REMOVE_DISK)
6456 /* need to ensure recovery thread has run */
6457 wait_event_interruptible_timeout(mddev->sb_wait,
6458 !test_bit(MD_RECOVERY_NEEDED,
6460 msecs_to_jiffies(5000));
6461 if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) {
6462 /* Need to flush page cache, and ensure no-one else opens
6465 mutex_lock(&mddev->open_mutex);
6466 if (atomic_read(&mddev->openers) > 1) {
6467 mutex_unlock(&mddev->open_mutex);
6471 set_bit(MD_STILL_CLOSED, &mddev->flags);
6472 mutex_unlock(&mddev->open_mutex);
6473 sync_blockdev(bdev);
6475 err = mddev_lock(mddev);
6478 "md: ioctl lock interrupted, reason %d, cmd %d\n",
6483 if (cmd == SET_ARRAY_INFO) {
6484 mdu_array_info_t info;
6486 memset(&info, 0, sizeof(info));
6487 else if (copy_from_user(&info, argp, sizeof(info))) {
6492 err = update_array_info(mddev, &info);
6494 printk(KERN_WARNING "md: couldn't update"
6495 " array info. %d\n", err);
6500 if (!list_empty(&mddev->disks)) {
6502 "md: array %s already has disks!\n",
6507 if (mddev->raid_disks) {
6509 "md: array %s already initialised!\n",
6514 err = set_array_info(mddev, &info);
6516 printk(KERN_WARNING "md: couldn't set"
6517 " array info. %d\n", err);
6524 * Commands querying/configuring an existing array:
6526 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
6527 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
6528 if ((!mddev->raid_disks && !mddev->external)
6529 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
6530 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
6531 && cmd != GET_BITMAP_FILE) {
6537 * Commands even a read-only array can execute:
6540 case GET_BITMAP_FILE:
6541 err = get_bitmap_file(mddev, argp);
6544 case RESTART_ARRAY_RW:
6545 err = restart_array(mddev);
6549 err = do_md_stop(mddev, 0, bdev);
6553 err = md_set_readonly(mddev, bdev);
6556 case HOT_REMOVE_DISK:
6557 err = hot_remove_disk(mddev, new_decode_dev(arg));
6561 /* We can support ADD_NEW_DISK on read-only arrays
6562 * on if we are re-adding a preexisting device.
6563 * So require mddev->pers and MD_DISK_SYNC.
6566 mdu_disk_info_t info;
6567 if (copy_from_user(&info, argp, sizeof(info)))
6569 else if (!(info.state & (1<<MD_DISK_SYNC)))
6570 /* Need to clear read-only for this */
6573 err = add_new_disk(mddev, &info);
6579 if (get_user(ro, (int __user *)(arg))) {
6585 /* if the bdev is going readonly the value of mddev->ro
6586 * does not matter, no writes are coming
6591 /* are we are already prepared for writes? */
6595 /* transitioning to readauto need only happen for
6596 * arrays that call md_write_start
6599 err = restart_array(mddev);
6602 set_disk_ro(mddev->gendisk, 0);
6609 * The remaining ioctls are changing the state of the
6610 * superblock, so we do not allow them on read-only arrays.
6611 * However non-MD ioctls (e.g. get-size) will still come through
6612 * here and hit the 'default' below, so only disallow
6613 * 'md' ioctls, and switch to rw mode if started auto-readonly.
6615 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
6616 if (mddev->ro == 2) {
6618 sysfs_notify_dirent_safe(mddev->sysfs_state);
6619 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6620 /* mddev_unlock will wake thread */
6621 /* If a device failed while we were read-only, we
6622 * need to make sure the metadata is updated now.
6624 if (test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
6625 mddev_unlock(mddev);
6626 wait_event(mddev->sb_wait,
6627 !test_bit(MD_CHANGE_DEVS, &mddev->flags) &&
6628 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
6629 mddev_lock_nointr(mddev);
6640 mdu_disk_info_t info;
6641 if (copy_from_user(&info, argp, sizeof(info)))
6644 err = add_new_disk(mddev, &info);
6649 err = hot_add_disk(mddev, new_decode_dev(arg));
6653 err = do_md_run(mddev);
6656 case SET_BITMAP_FILE:
6657 err = set_bitmap_file(mddev, (int)arg);
6667 if (mddev->hold_active == UNTIL_IOCTL &&
6669 mddev->hold_active = 0;
6670 mddev_unlock(mddev);
6679 #ifdef CONFIG_COMPAT
6680 static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
6681 unsigned int cmd, unsigned long arg)
6684 case HOT_REMOVE_DISK:
6686 case SET_DISK_FAULTY:
6687 case SET_BITMAP_FILE:
6688 /* These take in integer arg, do not convert */
6691 arg = (unsigned long)compat_ptr(arg);
6695 return md_ioctl(bdev, mode, cmd, arg);
6697 #endif /* CONFIG_COMPAT */
6699 static int md_open(struct block_device *bdev, fmode_t mode)
6702 * Succeed if we can lock the mddev, which confirms that
6703 * it isn't being stopped right now.
6705 struct mddev *mddev = mddev_find(bdev->bd_dev);
6711 if (mddev->gendisk != bdev->bd_disk) {
6712 /* we are racing with mddev_put which is discarding this
6716 /* Wait until bdev->bd_disk is definitely gone */
6717 flush_workqueue(md_misc_wq);
6718 /* Then retry the open from the top */
6719 return -ERESTARTSYS;
6721 BUG_ON(mddev != bdev->bd_disk->private_data);
6723 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
6727 atomic_inc(&mddev->openers);
6728 clear_bit(MD_STILL_CLOSED, &mddev->flags);
6729 mutex_unlock(&mddev->open_mutex);
6731 check_disk_change(bdev);
6736 static void md_release(struct gendisk *disk, fmode_t mode)
6738 struct mddev *mddev = disk->private_data;
6741 atomic_dec(&mddev->openers);
6745 static int md_media_changed(struct gendisk *disk)
6747 struct mddev *mddev = disk->private_data;
6749 return mddev->changed;
6752 static int md_revalidate(struct gendisk *disk)
6754 struct mddev *mddev = disk->private_data;
6759 static const struct block_device_operations md_fops =
6761 .owner = THIS_MODULE,
6763 .release = md_release,
6765 #ifdef CONFIG_COMPAT
6766 .compat_ioctl = md_compat_ioctl,
6768 .getgeo = md_getgeo,
6769 .media_changed = md_media_changed,
6770 .revalidate_disk= md_revalidate,
6773 static int md_thread(void * arg)
6775 struct md_thread *thread = arg;
6778 * md_thread is a 'system-thread', it's priority should be very
6779 * high. We avoid resource deadlocks individually in each
6780 * raid personality. (RAID5 does preallocation) We also use RR and
6781 * the very same RT priority as kswapd, thus we will never get
6782 * into a priority inversion deadlock.
6784 * we definitely have to have equal or higher priority than
6785 * bdflush, otherwise bdflush will deadlock if there are too
6786 * many dirty RAID5 blocks.
6789 allow_signal(SIGKILL);
6790 while (!kthread_should_stop()) {
6792 /* We need to wait INTERRUPTIBLE so that
6793 * we don't add to the load-average.
6794 * That means we need to be sure no signals are
6797 if (signal_pending(current))
6798 flush_signals(current);
6800 wait_event_interruptible_timeout
6802 test_bit(THREAD_WAKEUP, &thread->flags)
6803 || kthread_should_stop(),
6806 clear_bit(THREAD_WAKEUP, &thread->flags);
6807 if (!kthread_should_stop())
6808 thread->run(thread);
6814 void md_wakeup_thread(struct md_thread *thread)
6817 pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm);
6818 set_bit(THREAD_WAKEUP, &thread->flags);
6819 wake_up(&thread->wqueue);
6823 struct md_thread *md_register_thread(void (*run) (struct md_thread *),
6824 struct mddev *mddev, const char *name)
6826 struct md_thread *thread;
6828 thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL);
6832 init_waitqueue_head(&thread->wqueue);
6835 thread->mddev = mddev;
6836 thread->timeout = MAX_SCHEDULE_TIMEOUT;
6837 thread->tsk = kthread_run(md_thread, thread,
6839 mdname(thread->mddev),
6841 if (IS_ERR(thread->tsk)) {
6848 void md_unregister_thread(struct md_thread **threadp)
6850 struct md_thread *thread = *threadp;
6853 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
6854 /* Locking ensures that mddev_unlock does not wake_up a
6855 * non-existent thread
6857 spin_lock(&pers_lock);
6859 spin_unlock(&pers_lock);
6861 kthread_stop(thread->tsk);
6865 void md_error(struct mddev *mddev, struct md_rdev *rdev)
6872 if (!rdev || test_bit(Faulty, &rdev->flags))
6875 if (!mddev->pers || !mddev->pers->error_handler)
6877 mddev->pers->error_handler(mddev,rdev);
6878 if (mddev->degraded)
6879 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
6880 sysfs_notify_dirent_safe(rdev->sysfs_state);
6881 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6882 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6883 md_wakeup_thread(mddev->thread);
6884 if (mddev->event_work.func)
6885 queue_work(md_misc_wq, &mddev->event_work);
6886 md_new_event_inintr(mddev);
6889 /* seq_file implementation /proc/mdstat */
6891 static void status_unused(struct seq_file *seq)
6894 struct md_rdev *rdev;
6896 seq_printf(seq, "unused devices: ");
6898 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
6899 char b[BDEVNAME_SIZE];
6901 seq_printf(seq, "%s ",
6902 bdevname(rdev->bdev,b));
6905 seq_printf(seq, "<none>");
6907 seq_printf(seq, "\n");
6911 static void status_resync(struct seq_file *seq, struct mddev * mddev)
6913 sector_t max_sectors, resync, res;
6914 unsigned long dt, db;
6917 unsigned int per_milli;
6919 if (mddev->curr_resync <= 3)
6922 resync = mddev->curr_resync
6923 - atomic_read(&mddev->recovery_active);
6925 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
6926 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
6927 max_sectors = mddev->resync_max_sectors;
6929 max_sectors = mddev->dev_sectors;
6932 * Should not happen.
6938 /* Pick 'scale' such that (resync>>scale)*1000 will fit
6939 * in a sector_t, and (max_sectors>>scale) will fit in a
6940 * u32, as those are the requirements for sector_div.
6941 * Thus 'scale' must be at least 10
6944 if (sizeof(sector_t) > sizeof(unsigned long)) {
6945 while ( max_sectors/2 > (1ULL<<(scale+32)))
6948 res = (resync>>scale)*1000;
6949 sector_div(res, (u32)((max_sectors>>scale)+1));
6953 int i, x = per_milli/50, y = 20-x;
6954 seq_printf(seq, "[");
6955 for (i = 0; i < x; i++)
6956 seq_printf(seq, "=");
6957 seq_printf(seq, ">");
6958 for (i = 0; i < y; i++)
6959 seq_printf(seq, ".");
6960 seq_printf(seq, "] ");
6962 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
6963 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
6965 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
6967 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
6968 "resync" : "recovery"))),
6969 per_milli/10, per_milli % 10,
6970 (unsigned long long) resync/2,
6971 (unsigned long long) max_sectors/2);
6974 * dt: time from mark until now
6975 * db: blocks written from mark until now
6976 * rt: remaining time
6978 * rt is a sector_t, so could be 32bit or 64bit.
6979 * So we divide before multiply in case it is 32bit and close
6981 * We scale the divisor (db) by 32 to avoid losing precision
6982 * near the end of resync when the number of remaining sectors
6984 * We then divide rt by 32 after multiplying by db to compensate.
6985 * The '+1' avoids division by zero if db is very small.
6987 dt = ((jiffies - mddev->resync_mark) / HZ);
6989 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
6990 - mddev->resync_mark_cnt;
6992 rt = max_sectors - resync; /* number of remaining sectors */
6993 sector_div(rt, db/32+1);
6997 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
6998 ((unsigned long)rt % 60)/6);
7000 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
7003 static void *md_seq_start(struct seq_file *seq, loff_t *pos)
7005 struct list_head *tmp;
7007 struct mddev *mddev;
7015 spin_lock(&all_mddevs_lock);
7016 list_for_each(tmp,&all_mddevs)
7018 mddev = list_entry(tmp, struct mddev, all_mddevs);
7020 spin_unlock(&all_mddevs_lock);
7023 spin_unlock(&all_mddevs_lock);
7025 return (void*)2;/* tail */
7029 static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
7031 struct list_head *tmp;
7032 struct mddev *next_mddev, *mddev = v;
7038 spin_lock(&all_mddevs_lock);
7040 tmp = all_mddevs.next;
7042 tmp = mddev->all_mddevs.next;
7043 if (tmp != &all_mddevs)
7044 next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs));
7046 next_mddev = (void*)2;
7049 spin_unlock(&all_mddevs_lock);
7057 static void md_seq_stop(struct seq_file *seq, void *v)
7059 struct mddev *mddev = v;
7061 if (mddev && v != (void*)1 && v != (void*)2)
7065 static int md_seq_show(struct seq_file *seq, void *v)
7067 struct mddev *mddev = v;
7069 struct md_rdev *rdev;
7071 if (v == (void*)1) {
7072 struct md_personality *pers;
7073 seq_printf(seq, "Personalities : ");
7074 spin_lock(&pers_lock);
7075 list_for_each_entry(pers, &pers_list, list)
7076 seq_printf(seq, "[%s] ", pers->name);
7078 spin_unlock(&pers_lock);
7079 seq_printf(seq, "\n");
7080 seq->poll_event = atomic_read(&md_event_count);
7083 if (v == (void*)2) {
7088 if (mddev_lock(mddev) < 0)
7091 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
7092 seq_printf(seq, "%s : %sactive", mdname(mddev),
7093 mddev->pers ? "" : "in");
7096 seq_printf(seq, " (read-only)");
7098 seq_printf(seq, " (auto-read-only)");
7099 seq_printf(seq, " %s", mddev->pers->name);
7103 rdev_for_each(rdev, mddev) {
7104 char b[BDEVNAME_SIZE];
7105 seq_printf(seq, " %s[%d]",
7106 bdevname(rdev->bdev,b), rdev->desc_nr);
7107 if (test_bit(WriteMostly, &rdev->flags))
7108 seq_printf(seq, "(W)");
7109 if (test_bit(Faulty, &rdev->flags)) {
7110 seq_printf(seq, "(F)");
7113 if (rdev->raid_disk < 0)
7114 seq_printf(seq, "(S)"); /* spare */
7115 if (test_bit(Replacement, &rdev->flags))
7116 seq_printf(seq, "(R)");
7117 sectors += rdev->sectors;
7120 if (!list_empty(&mddev->disks)) {
7122 seq_printf(seq, "\n %llu blocks",
7123 (unsigned long long)
7124 mddev->array_sectors / 2);
7126 seq_printf(seq, "\n %llu blocks",
7127 (unsigned long long)sectors / 2);
7129 if (mddev->persistent) {
7130 if (mddev->major_version != 0 ||
7131 mddev->minor_version != 90) {
7132 seq_printf(seq," super %d.%d",
7133 mddev->major_version,
7134 mddev->minor_version);
7136 } else if (mddev->external)
7137 seq_printf(seq, " super external:%s",
7138 mddev->metadata_type);
7140 seq_printf(seq, " super non-persistent");
7143 mddev->pers->status(seq, mddev);
7144 seq_printf(seq, "\n ");
7145 if (mddev->pers->sync_request) {
7146 if (mddev->curr_resync > 2) {
7147 status_resync(seq, mddev);
7148 seq_printf(seq, "\n ");
7149 } else if (mddev->curr_resync >= 1)
7150 seq_printf(seq, "\tresync=DELAYED\n ");
7151 else if (mddev->recovery_cp < MaxSector)
7152 seq_printf(seq, "\tresync=PENDING\n ");
7155 seq_printf(seq, "\n ");
7157 bitmap_status(seq, mddev->bitmap);
7159 seq_printf(seq, "\n");
7161 mddev_unlock(mddev);
7166 static const struct seq_operations md_seq_ops = {
7167 .start = md_seq_start,
7168 .next = md_seq_next,
7169 .stop = md_seq_stop,
7170 .show = md_seq_show,
7173 static int md_seq_open(struct inode *inode, struct file *file)
7175 struct seq_file *seq;
7178 error = seq_open(file, &md_seq_ops);
7182 seq = file->private_data;
7183 seq->poll_event = atomic_read(&md_event_count);
7187 static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
7189 struct seq_file *seq = filp->private_data;
7192 poll_wait(filp, &md_event_waiters, wait);
7194 /* always allow read */
7195 mask = POLLIN | POLLRDNORM;
7197 if (seq->poll_event != atomic_read(&md_event_count))
7198 mask |= POLLERR | POLLPRI;
7202 static const struct file_operations md_seq_fops = {
7203 .owner = THIS_MODULE,
7204 .open = md_seq_open,
7206 .llseek = seq_lseek,
7207 .release = seq_release_private,
7208 .poll = mdstat_poll,
7211 int register_md_personality(struct md_personality *p)
7213 spin_lock(&pers_lock);
7214 list_add_tail(&p->list, &pers_list);
7215 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
7216 spin_unlock(&pers_lock);
7220 int unregister_md_personality(struct md_personality *p)
7222 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
7223 spin_lock(&pers_lock);
7224 list_del_init(&p->list);
7225 spin_unlock(&pers_lock);
7229 static int is_mddev_idle(struct mddev *mddev, int init)
7231 struct md_rdev * rdev;
7237 rdev_for_each_rcu(rdev, mddev) {
7238 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
7239 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
7240 (int)part_stat_read(&disk->part0, sectors[1]) -
7241 atomic_read(&disk->sync_io);
7242 /* sync IO will cause sync_io to increase before the disk_stats
7243 * as sync_io is counted when a request starts, and
7244 * disk_stats is counted when it completes.
7245 * So resync activity will cause curr_events to be smaller than
7246 * when there was no such activity.
7247 * non-sync IO will cause disk_stat to increase without
7248 * increasing sync_io so curr_events will (eventually)
7249 * be larger than it was before. Once it becomes
7250 * substantially larger, the test below will cause
7251 * the array to appear non-idle, and resync will slow
7253 * If there is a lot of outstanding resync activity when
7254 * we set last_event to curr_events, then all that activity
7255 * completing might cause the array to appear non-idle
7256 * and resync will be slowed down even though there might
7257 * not have been non-resync activity. This will only
7258 * happen once though. 'last_events' will soon reflect
7259 * the state where there is little or no outstanding
7260 * resync requests, and further resync activity will
7261 * always make curr_events less than last_events.
7264 if (init || curr_events - rdev->last_events > 64) {
7265 rdev->last_events = curr_events;
7273 void md_done_sync(struct mddev *mddev, int blocks, int ok)
7275 /* another "blocks" (512byte) blocks have been synced */
7276 atomic_sub(blocks, &mddev->recovery_active);
7277 wake_up(&mddev->recovery_wait);
7279 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7280 set_bit(MD_RECOVERY_ERROR, &mddev->recovery);
7281 md_wakeup_thread(mddev->thread);
7282 // stop recovery, signal do_sync ....
7287 /* md_write_start(mddev, bi)
7288 * If we need to update some array metadata (e.g. 'active' flag
7289 * in superblock) before writing, schedule a superblock update
7290 * and wait for it to complete.
7292 void md_write_start(struct mddev *mddev, struct bio *bi)
7295 if (bio_data_dir(bi) != WRITE)
7298 BUG_ON(mddev->ro == 1);
7299 if (mddev->ro == 2) {
7300 /* need to switch to read/write */
7302 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7303 md_wakeup_thread(mddev->thread);
7304 md_wakeup_thread(mddev->sync_thread);
7307 atomic_inc(&mddev->writes_pending);
7308 if (mddev->safemode == 1)
7309 mddev->safemode = 0;
7310 if (mddev->in_sync) {
7311 spin_lock_irq(&mddev->write_lock);
7312 if (mddev->in_sync) {
7314 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
7315 set_bit(MD_CHANGE_PENDING, &mddev->flags);
7316 md_wakeup_thread(mddev->thread);
7319 spin_unlock_irq(&mddev->write_lock);
7322 sysfs_notify_dirent_safe(mddev->sysfs_state);
7323 wait_event(mddev->sb_wait,
7324 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
7327 void md_write_end(struct mddev *mddev)
7329 if (atomic_dec_and_test(&mddev->writes_pending)) {
7330 if (mddev->safemode == 2)
7331 md_wakeup_thread(mddev->thread);
7332 else if (mddev->safemode_delay)
7333 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
7337 /* md_allow_write(mddev)
7338 * Calling this ensures that the array is marked 'active' so that writes
7339 * may proceed without blocking. It is important to call this before
7340 * attempting a GFP_KERNEL allocation while holding the mddev lock.
7341 * Must be called with mddev_lock held.
7343 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
7344 * is dropped, so return -EAGAIN after notifying userspace.
7346 int md_allow_write(struct mddev *mddev)
7352 if (!mddev->pers->sync_request)
7355 spin_lock_irq(&mddev->write_lock);
7356 if (mddev->in_sync) {
7358 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
7359 set_bit(MD_CHANGE_PENDING, &mddev->flags);
7360 if (mddev->safemode_delay &&
7361 mddev->safemode == 0)
7362 mddev->safemode = 1;
7363 spin_unlock_irq(&mddev->write_lock);
7364 md_update_sb(mddev, 0);
7365 sysfs_notify_dirent_safe(mddev->sysfs_state);
7367 spin_unlock_irq(&mddev->write_lock);
7369 if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
7374 EXPORT_SYMBOL_GPL(md_allow_write);
7376 #define SYNC_MARKS 10
7377 #define SYNC_MARK_STEP (3*HZ)
7378 #define UPDATE_FREQUENCY (5*60*HZ)
7379 void md_do_sync(struct md_thread *thread)
7381 struct mddev *mddev = thread->mddev;
7382 struct mddev *mddev2;
7383 unsigned int currspeed = 0,
7385 sector_t max_sectors,j, io_sectors;
7386 unsigned long mark[SYNC_MARKS];
7387 unsigned long update_time;
7388 sector_t mark_cnt[SYNC_MARKS];
7390 struct list_head *tmp;
7391 sector_t last_check;
7393 struct md_rdev *rdev;
7394 char *desc, *action = NULL;
7395 struct blk_plug plug;
7397 /* just incase thread restarts... */
7398 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7400 if (mddev->ro) /* never try to sync a read-only array */
7403 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
7404 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) {
7405 desc = "data-check";
7407 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
7408 desc = "requested-resync";
7412 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
7417 mddev->last_sync_action = action ?: desc;
7419 /* we overload curr_resync somewhat here.
7420 * 0 == not engaged in resync at all
7421 * 2 == checking that there is no conflict with another sync
7422 * 1 == like 2, but have yielded to allow conflicting resync to
7424 * other == active in resync - this many blocks
7426 * Before starting a resync we must have set curr_resync to
7427 * 2, and then checked that every "conflicting" array has curr_resync
7428 * less than ours. When we find one that is the same or higher
7429 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
7430 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
7431 * This will mean we have to start checking from the beginning again.
7436 mddev->curr_resync = 2;
7439 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7441 for_each_mddev(mddev2, tmp) {
7442 if (mddev2 == mddev)
7444 if (!mddev->parallel_resync
7445 && mddev2->curr_resync
7446 && match_mddev_units(mddev, mddev2)) {
7448 if (mddev < mddev2 && mddev->curr_resync == 2) {
7449 /* arbitrarily yield */
7450 mddev->curr_resync = 1;
7451 wake_up(&resync_wait);
7453 if (mddev > mddev2 && mddev->curr_resync == 1)
7454 /* no need to wait here, we can wait the next
7455 * time 'round when curr_resync == 2
7458 /* We need to wait 'interruptible' so as not to
7459 * contribute to the load average, and not to
7460 * be caught by 'softlockup'
7462 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
7463 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
7464 mddev2->curr_resync >= mddev->curr_resync) {
7465 printk(KERN_INFO "md: delaying %s of %s"
7466 " until %s has finished (they"
7467 " share one or more physical units)\n",
7468 desc, mdname(mddev), mdname(mddev2));
7470 if (signal_pending(current))
7471 flush_signals(current);
7473 finish_wait(&resync_wait, &wq);
7476 finish_wait(&resync_wait, &wq);
7479 } while (mddev->curr_resync < 2);
7482 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
7483 /* resync follows the size requested by the personality,
7484 * which defaults to physical size, but can be virtual size
7486 max_sectors = mddev->resync_max_sectors;
7487 atomic64_set(&mddev->resync_mismatches, 0);
7488 /* we don't use the checkpoint if there's a bitmap */
7489 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7490 j = mddev->resync_min;
7491 else if (!mddev->bitmap)
7492 j = mddev->recovery_cp;
7494 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
7495 max_sectors = mddev->resync_max_sectors;
7497 /* recovery follows the physical size of devices */
7498 max_sectors = mddev->dev_sectors;
7501 rdev_for_each_rcu(rdev, mddev)
7502 if (rdev->raid_disk >= 0 &&
7503 !test_bit(Faulty, &rdev->flags) &&
7504 !test_bit(In_sync, &rdev->flags) &&
7505 rdev->recovery_offset < j)
7506 j = rdev->recovery_offset;
7510 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
7511 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
7512 " %d KB/sec/disk.\n", speed_min(mddev));
7513 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
7514 "(but not more than %d KB/sec) for %s.\n",
7515 speed_max(mddev), desc);
7517 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
7520 for (m = 0; m < SYNC_MARKS; m++) {
7522 mark_cnt[m] = io_sectors;
7525 mddev->resync_mark = mark[last_mark];
7526 mddev->resync_mark_cnt = mark_cnt[last_mark];
7529 * Tune reconstruction:
7531 window = 32*(PAGE_SIZE/512);
7532 printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n",
7533 window/2, (unsigned long long)max_sectors/2);
7535 atomic_set(&mddev->recovery_active, 0);
7540 "md: resuming %s of %s from checkpoint.\n",
7541 desc, mdname(mddev));
7542 mddev->curr_resync = j;
7544 mddev->curr_resync = 3; /* no longer delayed */
7545 mddev->curr_resync_completed = j;
7546 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
7547 md_new_event(mddev);
7548 update_time = jiffies;
7550 blk_start_plug(&plug);
7551 while (j < max_sectors) {
7556 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7557 ((mddev->curr_resync > mddev->curr_resync_completed &&
7558 (mddev->curr_resync - mddev->curr_resync_completed)
7559 > (max_sectors >> 4)) ||
7560 time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) ||
7561 (j - mddev->curr_resync_completed)*2
7562 >= mddev->resync_max - mddev->curr_resync_completed
7564 /* time to update curr_resync_completed */
7565 wait_event(mddev->recovery_wait,
7566 atomic_read(&mddev->recovery_active) == 0);
7567 mddev->curr_resync_completed = j;
7568 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
7569 j > mddev->recovery_cp)
7570 mddev->recovery_cp = j;
7571 update_time = jiffies;
7572 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
7573 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
7576 while (j >= mddev->resync_max &&
7577 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7578 /* As this condition is controlled by user-space,
7579 * we can block indefinitely, so use '_interruptible'
7580 * to avoid triggering warnings.
7582 flush_signals(current); /* just in case */
7583 wait_event_interruptible(mddev->recovery_wait,
7584 mddev->resync_max > j
7585 || test_bit(MD_RECOVERY_INTR,
7589 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7592 sectors = mddev->pers->sync_request(mddev, j, &skipped,
7593 currspeed < speed_min(mddev));
7595 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7599 if (!skipped) { /* actual IO requested */
7600 io_sectors += sectors;
7601 atomic_add(sectors, &mddev->recovery_active);
7604 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7609 mddev->curr_resync = j;
7610 mddev->curr_mark_cnt = io_sectors;
7611 if (last_check == 0)
7612 /* this is the earliest that rebuild will be
7613 * visible in /proc/mdstat
7615 md_new_event(mddev);
7617 if (last_check + window > io_sectors || j == max_sectors)
7620 last_check = io_sectors;
7622 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
7624 int next = (last_mark+1) % SYNC_MARKS;
7626 mddev->resync_mark = mark[next];
7627 mddev->resync_mark_cnt = mark_cnt[next];
7628 mark[next] = jiffies;
7629 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
7633 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7637 * this loop exits only if either when we are slower than
7638 * the 'hard' speed limit, or the system was IO-idle for
7640 * the system might be non-idle CPU-wise, but we only care
7641 * about not overloading the IO subsystem. (things like an
7642 * e2fsck being done on the RAID array should execute fast)
7646 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
7647 /((jiffies-mddev->resync_mark)/HZ +1) +1;
7649 if (currspeed > speed_min(mddev)) {
7650 if ((currspeed > speed_max(mddev)) ||
7651 !is_mddev_idle(mddev, 0)) {
7657 printk(KERN_INFO "md: %s: %s %s.\n",mdname(mddev), desc,
7658 test_bit(MD_RECOVERY_INTR, &mddev->recovery)
7659 ? "interrupted" : "done");
7661 * this also signals 'finished resyncing' to md_stop
7663 blk_finish_plug(&plug);
7664 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
7666 /* tell personality that we are finished */
7667 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
7669 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
7670 mddev->curr_resync > 2) {
7671 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
7672 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7673 if (mddev->curr_resync >= mddev->recovery_cp) {
7675 "md: checkpointing %s of %s.\n",
7676 desc, mdname(mddev));
7677 if (test_bit(MD_RECOVERY_ERROR,
7679 mddev->recovery_cp =
7680 mddev->curr_resync_completed;
7682 mddev->recovery_cp =
7686 mddev->recovery_cp = MaxSector;
7688 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7689 mddev->curr_resync = MaxSector;
7691 rdev_for_each_rcu(rdev, mddev)
7692 if (rdev->raid_disk >= 0 &&
7693 mddev->delta_disks >= 0 &&
7694 !test_bit(Faulty, &rdev->flags) &&
7695 !test_bit(In_sync, &rdev->flags) &&
7696 rdev->recovery_offset < mddev->curr_resync)
7697 rdev->recovery_offset = mddev->curr_resync;
7702 set_bit(MD_CHANGE_DEVS, &mddev->flags);
7704 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7705 /* We completed so min/max setting can be forgotten if used. */
7706 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7707 mddev->resync_min = 0;
7708 mddev->resync_max = MaxSector;
7709 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7710 mddev->resync_min = mddev->curr_resync_completed;
7711 mddev->curr_resync = 0;
7712 wake_up(&resync_wait);
7713 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
7714 md_wakeup_thread(mddev->thread);
7717 EXPORT_SYMBOL_GPL(md_do_sync);
7719 static int remove_and_add_spares(struct mddev *mddev,
7720 struct md_rdev *this)
7722 struct md_rdev *rdev;
7726 rdev_for_each(rdev, mddev)
7727 if ((this == NULL || rdev == this) &&
7728 rdev->raid_disk >= 0 &&
7729 !test_bit(Blocked, &rdev->flags) &&
7730 (test_bit(Faulty, &rdev->flags) ||
7731 ! test_bit(In_sync, &rdev->flags)) &&
7732 atomic_read(&rdev->nr_pending)==0) {
7733 if (mddev->pers->hot_remove_disk(
7734 mddev, rdev) == 0) {
7735 sysfs_unlink_rdev(mddev, rdev);
7736 rdev->raid_disk = -1;
7740 if (removed && mddev->kobj.sd)
7741 sysfs_notify(&mddev->kobj, NULL, "degraded");
7746 rdev_for_each(rdev, mddev) {
7747 if (rdev->raid_disk >= 0 &&
7748 !test_bit(In_sync, &rdev->flags) &&
7749 !test_bit(Faulty, &rdev->flags))
7751 if (rdev->raid_disk >= 0)
7753 if (test_bit(Faulty, &rdev->flags))
7756 ! (rdev->saved_raid_disk >= 0 &&
7757 !test_bit(Bitmap_sync, &rdev->flags)))
7760 if (rdev->saved_raid_disk < 0)
7761 rdev->recovery_offset = 0;
7763 hot_add_disk(mddev, rdev) == 0) {
7764 if (sysfs_link_rdev(mddev, rdev))
7765 /* failure here is OK */;
7767 md_new_event(mddev);
7768 set_bit(MD_CHANGE_DEVS, &mddev->flags);
7773 set_bit(MD_CHANGE_DEVS, &mddev->flags);
7778 * This routine is regularly called by all per-raid-array threads to
7779 * deal with generic issues like resync and super-block update.
7780 * Raid personalities that don't have a thread (linear/raid0) do not
7781 * need this as they never do any recovery or update the superblock.
7783 * It does not do any resync itself, but rather "forks" off other threads
7784 * to do that as needed.
7785 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
7786 * "->recovery" and create a thread at ->sync_thread.
7787 * When the thread finishes it sets MD_RECOVERY_DONE
7788 * and wakeups up this thread which will reap the thread and finish up.
7789 * This thread also removes any faulty devices (with nr_pending == 0).
7791 * The overall approach is:
7792 * 1/ if the superblock needs updating, update it.
7793 * 2/ If a recovery thread is running, don't do anything else.
7794 * 3/ If recovery has finished, clean up, possibly marking spares active.
7795 * 4/ If there are any faulty devices, remove them.
7796 * 5/ If array is degraded, try to add spares devices
7797 * 6/ If array has spares or is not in-sync, start a resync thread.
7799 void md_check_recovery(struct mddev *mddev)
7801 if (mddev->suspended)
7805 bitmap_daemon_work(mddev);
7807 if (signal_pending(current)) {
7808 if (mddev->pers->sync_request && !mddev->external) {
7809 printk(KERN_INFO "md: %s in immediate safe mode\n",
7811 mddev->safemode = 2;
7813 flush_signals(current);
7816 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
7819 (mddev->flags & MD_UPDATE_SB_FLAGS & ~ (1<<MD_CHANGE_PENDING)) ||
7820 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
7821 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
7822 (mddev->external == 0 && mddev->safemode == 1) ||
7823 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
7824 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
7828 if (mddev_trylock(mddev)) {
7832 /* On a read-only array we can:
7833 * - remove failed devices
7834 * - add already-in_sync devices if the array itself
7836 * As we only add devices that are already in-sync,
7837 * we can activate the spares immediately.
7839 remove_and_add_spares(mddev, NULL);
7840 /* There is no thread, but we need to call
7841 * ->spare_active and clear saved_raid_disk
7843 md_reap_sync_thread(mddev);
7844 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7848 if (!mddev->external) {
7850 spin_lock_irq(&mddev->write_lock);
7851 if (mddev->safemode &&
7852 !atomic_read(&mddev->writes_pending) &&
7854 mddev->recovery_cp == MaxSector) {
7857 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
7859 if (mddev->safemode == 1)
7860 mddev->safemode = 0;
7861 spin_unlock_irq(&mddev->write_lock);
7863 sysfs_notify_dirent_safe(mddev->sysfs_state);
7866 if (mddev->flags & MD_UPDATE_SB_FLAGS)
7867 md_update_sb(mddev, 0);
7869 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
7870 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
7871 /* resync/recovery still happening */
7872 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7875 if (mddev->sync_thread) {
7876 md_reap_sync_thread(mddev);
7879 /* Set RUNNING before clearing NEEDED to avoid
7880 * any transients in the value of "sync_action".
7882 mddev->curr_resync_completed = 0;
7883 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7884 /* Clear some bits that don't mean anything, but
7887 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
7888 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
7890 if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
7891 test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
7893 /* no recovery is running.
7894 * remove any failed drives, then
7895 * add spares if possible.
7896 * Spares are also removed and re-added, to allow
7897 * the personality to fail the re-add.
7900 if (mddev->reshape_position != MaxSector) {
7901 if (mddev->pers->check_reshape == NULL ||
7902 mddev->pers->check_reshape(mddev) != 0)
7903 /* Cannot proceed */
7905 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7906 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
7907 } else if ((spares = remove_and_add_spares(mddev, NULL))) {
7908 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7909 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
7910 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7911 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
7912 } else if (mddev->recovery_cp < MaxSector) {
7913 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7914 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
7915 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
7916 /* nothing to be done ... */
7919 if (mddev->pers->sync_request) {
7921 /* We are adding a device or devices to an array
7922 * which has the bitmap stored on all devices.
7923 * So make sure all bitmap pages get written
7925 bitmap_write_all(mddev->bitmap);
7927 mddev->sync_thread = md_register_thread(md_do_sync,
7930 if (!mddev->sync_thread) {
7931 printk(KERN_ERR "%s: could not start resync"
7934 /* leave the spares where they are, it shouldn't hurt */
7935 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7936 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7937 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7938 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7939 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
7941 md_wakeup_thread(mddev->sync_thread);
7942 sysfs_notify_dirent_safe(mddev->sysfs_action);
7943 md_new_event(mddev);
7946 wake_up(&mddev->sb_wait);
7948 if (!mddev->sync_thread) {
7949 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7950 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
7952 if (mddev->sysfs_action)
7953 sysfs_notify_dirent_safe(mddev->sysfs_action);
7955 mddev_unlock(mddev);
7959 void md_reap_sync_thread(struct mddev *mddev)
7961 struct md_rdev *rdev;
7963 /* resync has finished, collect result */
7964 md_unregister_thread(&mddev->sync_thread);
7965 wake_up(&resync_wait);
7966 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
7967 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
7969 /* activate any spares */
7970 if (mddev->pers->spare_active(mddev)) {
7971 sysfs_notify(&mddev->kobj, NULL,
7973 set_bit(MD_CHANGE_DEVS, &mddev->flags);
7976 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7977 mddev->pers->finish_reshape)
7978 mddev->pers->finish_reshape(mddev);
7980 /* If array is no-longer degraded, then any saved_raid_disk
7981 * information must be scrapped.
7983 if (!mddev->degraded)
7984 rdev_for_each(rdev, mddev)
7985 rdev->saved_raid_disk = -1;
7987 md_update_sb(mddev, 1);
7988 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7989 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7990 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7991 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7992 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
7993 /* flag recovery needed just to double check */
7994 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7995 sysfs_notify_dirent_safe(mddev->sysfs_action);
7996 md_new_event(mddev);
7997 if (mddev->event_work.func)
7998 queue_work(md_misc_wq, &mddev->event_work);
8001 void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev)
8003 sysfs_notify_dirent_safe(rdev->sysfs_state);
8004 wait_event_timeout(rdev->blocked_wait,
8005 !test_bit(Blocked, &rdev->flags) &&
8006 !test_bit(BlockedBadBlocks, &rdev->flags),
8007 msecs_to_jiffies(5000));
8008 rdev_dec_pending(rdev, mddev);
8010 EXPORT_SYMBOL(md_wait_for_blocked_rdev);
8012 void md_finish_reshape(struct mddev *mddev)
8014 /* called be personality module when reshape completes. */
8015 struct md_rdev *rdev;
8017 rdev_for_each(rdev, mddev) {
8018 if (rdev->data_offset > rdev->new_data_offset)
8019 rdev->sectors += rdev->data_offset - rdev->new_data_offset;
8021 rdev->sectors -= rdev->new_data_offset - rdev->data_offset;
8022 rdev->data_offset = rdev->new_data_offset;
8025 EXPORT_SYMBOL(md_finish_reshape);
8027 /* Bad block management.
8028 * We can record which blocks on each device are 'bad' and so just
8029 * fail those blocks, or that stripe, rather than the whole device.
8030 * Entries in the bad-block table are 64bits wide. This comprises:
8031 * Length of bad-range, in sectors: 0-511 for lengths 1-512
8032 * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes)
8033 * A 'shift' can be set so that larger blocks are tracked and
8034 * consequently larger devices can be covered.
8035 * 'Acknowledged' flag - 1 bit. - the most significant bit.
8037 * Locking of the bad-block table uses a seqlock so md_is_badblock
8038 * might need to retry if it is very unlucky.
8039 * We will sometimes want to check for bad blocks in a bi_end_io function,
8040 * so we use the write_seqlock_irq variant.
8042 * When looking for a bad block we specify a range and want to
8043 * know if any block in the range is bad. So we binary-search
8044 * to the last range that starts at-or-before the given endpoint,
8045 * (or "before the sector after the target range")
8046 * then see if it ends after the given start.
8048 * 0 if there are no known bad blocks in the range
8049 * 1 if there are known bad block which are all acknowledged
8050 * -1 if there are bad blocks which have not yet been acknowledged in metadata.
8051 * plus the start/length of the first bad section we overlap.
8053 int md_is_badblock(struct badblocks *bb, sector_t s, int sectors,
8054 sector_t *first_bad, int *bad_sectors)
8060 sector_t target = s + sectors;
8063 if (bb->shift > 0) {
8064 /* round the start down, and the end up */
8066 target += (1<<bb->shift) - 1;
8067 target >>= bb->shift;
8068 sectors = target - s;
8070 /* 'target' is now the first block after the bad range */
8073 seq = read_seqbegin(&bb->lock);
8078 /* Binary search between lo and hi for 'target'
8079 * i.e. for the last range that starts before 'target'
8081 /* INVARIANT: ranges before 'lo' and at-or-after 'hi'
8082 * are known not to be the last range before target.
8083 * VARIANT: hi-lo is the number of possible
8084 * ranges, and decreases until it reaches 1
8086 while (hi - lo > 1) {
8087 int mid = (lo + hi) / 2;
8088 sector_t a = BB_OFFSET(p[mid]);
8090 /* This could still be the one, earlier ranges
8094 /* This and later ranges are definitely out. */
8097 /* 'lo' might be the last that started before target, but 'hi' isn't */
8099 /* need to check all range that end after 's' to see if
8100 * any are unacknowledged.
8103 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
8104 if (BB_OFFSET(p[lo]) < target) {
8105 /* starts before the end, and finishes after
8106 * the start, so they must overlap
8108 if (rv != -1 && BB_ACK(p[lo]))
8112 *first_bad = BB_OFFSET(p[lo]);
8113 *bad_sectors = BB_LEN(p[lo]);
8119 if (read_seqretry(&bb->lock, seq))
8124 EXPORT_SYMBOL_GPL(md_is_badblock);
8127 * Add a range of bad blocks to the table.
8128 * This might extend the table, or might contract it
8129 * if two adjacent ranges can be merged.
8130 * We binary-search to find the 'insertion' point, then
8131 * decide how best to handle it.
8133 static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
8139 unsigned long flags;
8142 /* badblocks are disabled */
8146 /* round the start down, and the end up */
8147 sector_t next = s + sectors;
8149 next += (1<<bb->shift) - 1;
8154 write_seqlock_irqsave(&bb->lock, flags);
8159 /* Find the last range that starts at-or-before 's' */
8160 while (hi - lo > 1) {
8161 int mid = (lo + hi) / 2;
8162 sector_t a = BB_OFFSET(p[mid]);
8168 if (hi > lo && BB_OFFSET(p[lo]) > s)
8172 /* we found a range that might merge with the start
8175 sector_t a = BB_OFFSET(p[lo]);
8176 sector_t e = a + BB_LEN(p[lo]);
8177 int ack = BB_ACK(p[lo]);
8179 /* Yes, we can merge with a previous range */
8180 if (s == a && s + sectors >= e)
8181 /* new range covers old */
8184 ack = ack && acknowledged;
8186 if (e < s + sectors)
8188 if (e - a <= BB_MAX_LEN) {
8189 p[lo] = BB_MAKE(a, e-a, ack);
8192 /* does not all fit in one range,
8193 * make p[lo] maximal
8195 if (BB_LEN(p[lo]) != BB_MAX_LEN)
8196 p[lo] = BB_MAKE(a, BB_MAX_LEN, ack);
8202 if (sectors && hi < bb->count) {
8203 /* 'hi' points to the first range that starts after 's'.
8204 * Maybe we can merge with the start of that range */
8205 sector_t a = BB_OFFSET(p[hi]);
8206 sector_t e = a + BB_LEN(p[hi]);
8207 int ack = BB_ACK(p[hi]);
8208 if (a <= s + sectors) {
8209 /* merging is possible */
8210 if (e <= s + sectors) {
8215 ack = ack && acknowledged;
8218 if (e - a <= BB_MAX_LEN) {
8219 p[hi] = BB_MAKE(a, e-a, ack);
8222 p[hi] = BB_MAKE(a, BB_MAX_LEN, ack);
8230 if (sectors == 0 && hi < bb->count) {
8231 /* we might be able to combine lo and hi */
8232 /* Note: 's' is at the end of 'lo' */
8233 sector_t a = BB_OFFSET(p[hi]);
8234 int lolen = BB_LEN(p[lo]);
8235 int hilen = BB_LEN(p[hi]);
8236 int newlen = lolen + hilen - (s - a);
8237 if (s >= a && newlen < BB_MAX_LEN) {
8238 /* yes, we can combine them */
8239 int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]);
8240 p[lo] = BB_MAKE(BB_OFFSET(p[lo]), newlen, ack);
8241 memmove(p + hi, p + hi + 1,
8242 (bb->count - hi - 1) * 8);
8247 /* didn't merge (it all).
8248 * Need to add a range just before 'hi' */
8249 if (bb->count >= MD_MAX_BADBLOCKS) {
8250 /* No room for more */
8254 int this_sectors = sectors;
8255 memmove(p + hi + 1, p + hi,
8256 (bb->count - hi) * 8);
8259 if (this_sectors > BB_MAX_LEN)
8260 this_sectors = BB_MAX_LEN;
8261 p[hi] = BB_MAKE(s, this_sectors, acknowledged);
8262 sectors -= this_sectors;
8269 bb->unacked_exist = 1;
8270 write_sequnlock_irqrestore(&bb->lock, flags);
8275 int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
8280 s += rdev->new_data_offset;
8282 s += rdev->data_offset;
8283 rv = md_set_badblocks(&rdev->badblocks,
8286 /* Make sure they get written out promptly */
8287 sysfs_notify_dirent_safe(rdev->sysfs_state);
8288 set_bit(MD_CHANGE_CLEAN, &rdev->mddev->flags);
8289 md_wakeup_thread(rdev->mddev->thread);
8293 EXPORT_SYMBOL_GPL(rdev_set_badblocks);
8296 * Remove a range of bad blocks from the table.
8297 * This may involve extending the table if we spilt a region,
8298 * but it must not fail. So if the table becomes full, we just
8299 * drop the remove request.
8301 static int md_clear_badblocks(struct badblocks *bb, sector_t s, int sectors)
8305 sector_t target = s + sectors;
8308 if (bb->shift > 0) {
8309 /* When clearing we round the start up and the end down.
8310 * This should not matter as the shift should align with
8311 * the block size and no rounding should ever be needed.
8312 * However it is better the think a block is bad when it
8313 * isn't than to think a block is not bad when it is.
8315 s += (1<<bb->shift) - 1;
8317 target >>= bb->shift;
8318 sectors = target - s;
8321 write_seqlock_irq(&bb->lock);
8326 /* Find the last range that starts before 'target' */
8327 while (hi - lo > 1) {
8328 int mid = (lo + hi) / 2;
8329 sector_t a = BB_OFFSET(p[mid]);
8336 /* p[lo] is the last range that could overlap the
8337 * current range. Earlier ranges could also overlap,
8338 * but only this one can overlap the end of the range.
8340 if (BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > target) {
8341 /* Partial overlap, leave the tail of this range */
8342 int ack = BB_ACK(p[lo]);
8343 sector_t a = BB_OFFSET(p[lo]);
8344 sector_t end = a + BB_LEN(p[lo]);
8347 /* we need to split this range */
8348 if (bb->count >= MD_MAX_BADBLOCKS) {
8352 memmove(p+lo+1, p+lo, (bb->count - lo) * 8);
8354 p[lo] = BB_MAKE(a, s-a, ack);
8357 p[lo] = BB_MAKE(target, end - target, ack);
8358 /* there is no longer an overlap */
8363 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
8364 /* This range does overlap */
8365 if (BB_OFFSET(p[lo]) < s) {
8366 /* Keep the early parts of this range. */
8367 int ack = BB_ACK(p[lo]);
8368 sector_t start = BB_OFFSET(p[lo]);
8369 p[lo] = BB_MAKE(start, s - start, ack);
8370 /* now low doesn't overlap, so.. */
8375 /* 'lo' is strictly before, 'hi' is strictly after,
8376 * anything between needs to be discarded
8379 memmove(p+lo+1, p+hi, (bb->count - hi) * 8);
8380 bb->count -= (hi - lo - 1);
8386 write_sequnlock_irq(&bb->lock);
8390 int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
8394 s += rdev->new_data_offset;
8396 s += rdev->data_offset;
8397 return md_clear_badblocks(&rdev->badblocks,
8400 EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
8403 * Acknowledge all bad blocks in a list.
8404 * This only succeeds if ->changed is clear. It is used by
8405 * in-kernel metadata updates
8407 void md_ack_all_badblocks(struct badblocks *bb)
8409 if (bb->page == NULL || bb->changed)
8410 /* no point even trying */
8412 write_seqlock_irq(&bb->lock);
8414 if (bb->changed == 0 && bb->unacked_exist) {
8417 for (i = 0; i < bb->count ; i++) {
8418 if (!BB_ACK(p[i])) {
8419 sector_t start = BB_OFFSET(p[i]);
8420 int len = BB_LEN(p[i]);
8421 p[i] = BB_MAKE(start, len, 1);
8424 bb->unacked_exist = 0;
8426 write_sequnlock_irq(&bb->lock);
8428 EXPORT_SYMBOL_GPL(md_ack_all_badblocks);
8430 /* sysfs access to bad-blocks list.
8431 * We present two files.
8432 * 'bad-blocks' lists sector numbers and lengths of ranges that
8433 * are recorded as bad. The list is truncated to fit within
8434 * the one-page limit of sysfs.
8435 * Writing "sector length" to this file adds an acknowledged
8437 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
8438 * been acknowledged. Writing to this file adds bad blocks
8439 * without acknowledging them. This is largely for testing.
8443 badblocks_show(struct badblocks *bb, char *page, int unack)
8454 seq = read_seqbegin(&bb->lock);
8459 while (len < PAGE_SIZE && i < bb->count) {
8460 sector_t s = BB_OFFSET(p[i]);
8461 unsigned int length = BB_LEN(p[i]);
8462 int ack = BB_ACK(p[i]);
8468 len += snprintf(page+len, PAGE_SIZE-len, "%llu %u\n",
8469 (unsigned long long)s << bb->shift,
8470 length << bb->shift);
8472 if (unack && len == 0)
8473 bb->unacked_exist = 0;
8475 if (read_seqretry(&bb->lock, seq))
8484 badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack)
8486 unsigned long long sector;
8490 /* Allow clearing via sysfs *only* for testing/debugging.
8491 * Normally only a successful write may clear a badblock
8494 if (page[0] == '-') {
8498 #endif /* DO_DEBUG */
8500 switch (sscanf(page, "%llu %d%c", §or, &length, &newline)) {
8502 if (newline != '\n')
8514 md_clear_badblocks(bb, sector, length);
8517 #endif /* DO_DEBUG */
8518 if (md_set_badblocks(bb, sector, length, !unack))
8524 static int md_notify_reboot(struct notifier_block *this,
8525 unsigned long code, void *x)
8527 struct list_head *tmp;
8528 struct mddev *mddev;
8531 for_each_mddev(mddev, tmp) {
8532 if (mddev_trylock(mddev)) {
8534 __md_stop_writes(mddev);
8535 mddev->safemode = 2;
8536 mddev_unlock(mddev);
8541 * certain more exotic SCSI devices are known to be
8542 * volatile wrt too early system reboots. While the
8543 * right place to handle this issue is the given
8544 * driver, we do want to have a safe RAID driver ...
8552 static struct notifier_block md_notifier = {
8553 .notifier_call = md_notify_reboot,
8555 .priority = INT_MAX, /* before any real devices */
8558 static void md_geninit(void)
8560 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
8562 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
8565 static int __init md_init(void)
8569 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
8573 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
8577 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
8580 if ((ret = register_blkdev(0, "mdp")) < 0)
8584 blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE,
8585 md_probe, NULL, NULL);
8586 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
8587 md_probe, NULL, NULL);
8589 register_reboot_notifier(&md_notifier);
8590 raid_table_header = register_sysctl_table(raid_root_table);
8596 unregister_blkdev(MD_MAJOR, "md");
8598 destroy_workqueue(md_misc_wq);
8600 destroy_workqueue(md_wq);
8608 * Searches all registered partitions for autorun RAID arrays
8612 static LIST_HEAD(all_detected_devices);
8613 struct detected_devices_node {
8614 struct list_head list;
8618 void md_autodetect_dev(dev_t dev)
8620 struct detected_devices_node *node_detected_dev;
8622 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
8623 if (node_detected_dev) {
8624 node_detected_dev->dev = dev;
8625 list_add_tail(&node_detected_dev->list, &all_detected_devices);
8627 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
8628 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
8633 static void autostart_arrays(int part)
8635 struct md_rdev *rdev;
8636 struct detected_devices_node *node_detected_dev;
8638 int i_scanned, i_passed;
8643 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
8645 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
8647 node_detected_dev = list_entry(all_detected_devices.next,
8648 struct detected_devices_node, list);
8649 list_del(&node_detected_dev->list);
8650 dev = node_detected_dev->dev;
8651 kfree(node_detected_dev);
8652 rdev = md_import_device(dev,0, 90);
8656 if (test_bit(Faulty, &rdev->flags)) {
8660 set_bit(AutoDetected, &rdev->flags);
8661 list_add(&rdev->same_set, &pending_raid_disks);
8665 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
8666 i_scanned, i_passed);
8668 autorun_devices(part);
8671 #endif /* !MODULE */
8673 static __exit void md_exit(void)
8675 struct mddev *mddev;
8676 struct list_head *tmp;
8678 blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS);
8679 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
8681 unregister_blkdev(MD_MAJOR,"md");
8682 unregister_blkdev(mdp_major, "mdp");
8683 unregister_reboot_notifier(&md_notifier);
8684 unregister_sysctl_table(raid_table_header);
8685 remove_proc_entry("mdstat", NULL);
8686 for_each_mddev(mddev, tmp) {
8687 export_array(mddev);
8688 mddev->hold_active = 0;
8690 destroy_workqueue(md_misc_wq);
8691 destroy_workqueue(md_wq);
8694 subsys_initcall(md_init);
8695 module_exit(md_exit)
8697 static int get_ro(char *buffer, struct kernel_param *kp)
8699 return sprintf(buffer, "%d", start_readonly);
8701 static int set_ro(const char *val, struct kernel_param *kp)
8704 int num = simple_strtoul(val, &e, 10);
8705 if (*val && (*e == '\0' || *e == '\n')) {
8706 start_readonly = num;
8712 module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
8713 module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
8715 module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
8717 EXPORT_SYMBOL(register_md_personality);
8718 EXPORT_SYMBOL(unregister_md_personality);
8719 EXPORT_SYMBOL(md_error);
8720 EXPORT_SYMBOL(md_done_sync);
8721 EXPORT_SYMBOL(md_write_start);
8722 EXPORT_SYMBOL(md_write_end);
8723 EXPORT_SYMBOL(md_register_thread);
8724 EXPORT_SYMBOL(md_unregister_thread);
8725 EXPORT_SYMBOL(md_wakeup_thread);
8726 EXPORT_SYMBOL(md_check_recovery);
8727 EXPORT_SYMBOL(md_reap_sync_thread);
8728 MODULE_LICENSE("GPL");
8729 MODULE_DESCRIPTION("MD RAID framework");
8731 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);