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 #define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
78 * Default number of read corrections we'll attempt on an rdev
79 * before ejecting it from the array. We divide the read error
80 * count by 2 for every hour elapsed between read errors.
82 #define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
84 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
85 * is 1000 KB/sec, so the extra system load does not show up that much.
86 * Increase it if you want to have more _guaranteed_ speed. Note that
87 * the RAID driver will use the maximum available bandwidth if the IO
88 * subsystem is idle. There is also an 'absolute maximum' reconstruction
89 * speed limit - in case reconstruction slows down your system despite
92 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
93 * or /sys/block/mdX/md/sync_speed_{min,max}
96 static int sysctl_speed_limit_min = 1000;
97 static int sysctl_speed_limit_max = 200000;
98 static inline int speed_min(struct mddev *mddev)
100 return mddev->sync_speed_min ?
101 mddev->sync_speed_min : sysctl_speed_limit_min;
104 static inline int speed_max(struct mddev *mddev)
106 return mddev->sync_speed_max ?
107 mddev->sync_speed_max : sysctl_speed_limit_max;
110 static struct ctl_table_header *raid_table_header;
112 static ctl_table raid_table[] = {
114 .procname = "speed_limit_min",
115 .data = &sysctl_speed_limit_min,
116 .maxlen = sizeof(int),
117 .mode = S_IRUGO|S_IWUSR,
118 .proc_handler = proc_dointvec,
121 .procname = "speed_limit_max",
122 .data = &sysctl_speed_limit_max,
123 .maxlen = sizeof(int),
124 .mode = S_IRUGO|S_IWUSR,
125 .proc_handler = proc_dointvec,
130 static ctl_table raid_dir_table[] = {
134 .mode = S_IRUGO|S_IXUGO,
140 static ctl_table raid_root_table[] = {
145 .child = raid_dir_table,
150 static const struct block_device_operations md_fops;
152 static int start_readonly;
155 * like bio_clone, but with a local bio set
158 struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs,
163 if (!mddev || !mddev->bio_set)
164 return bio_alloc(gfp_mask, nr_iovecs);
166 b = bio_alloc_bioset(gfp_mask, nr_iovecs, mddev->bio_set);
171 EXPORT_SYMBOL_GPL(bio_alloc_mddev);
173 struct bio *bio_clone_mddev(struct bio *bio, gfp_t gfp_mask,
176 if (!mddev || !mddev->bio_set)
177 return bio_clone(bio, gfp_mask);
179 return bio_clone_bioset(bio, gfp_mask, mddev->bio_set);
181 EXPORT_SYMBOL_GPL(bio_clone_mddev);
183 void md_trim_bio(struct bio *bio, int offset, int size)
185 /* 'bio' is a cloned bio which we need to trim to match
186 * the given offset and size.
187 * This requires adjusting bi_sector, bi_size, and bi_io_vec
190 struct bio_vec *bvec;
194 if (offset == 0 && size == bio->bi_size)
197 bio->bi_sector += offset;
200 clear_bit(BIO_SEG_VALID, &bio->bi_flags);
202 while (bio->bi_idx < bio->bi_vcnt &&
203 bio->bi_io_vec[bio->bi_idx].bv_len <= offset) {
204 /* remove this whole bio_vec */
205 offset -= bio->bi_io_vec[bio->bi_idx].bv_len;
208 if (bio->bi_idx < bio->bi_vcnt) {
209 bio->bi_io_vec[bio->bi_idx].bv_offset += offset;
210 bio->bi_io_vec[bio->bi_idx].bv_len -= offset;
212 /* avoid any complications with bi_idx being non-zero*/
214 memmove(bio->bi_io_vec, bio->bi_io_vec+bio->bi_idx,
215 (bio->bi_vcnt - bio->bi_idx) * sizeof(struct bio_vec));
216 bio->bi_vcnt -= bio->bi_idx;
219 /* Make sure vcnt and last bv are not too big */
220 bio_for_each_segment(bvec, bio, i) {
221 if (sofar + bvec->bv_len > size)
222 bvec->bv_len = size - sofar;
223 if (bvec->bv_len == 0) {
227 sofar += bvec->bv_len;
230 EXPORT_SYMBOL_GPL(md_trim_bio);
233 * We have a system wide 'event count' that is incremented
234 * on any 'interesting' event, and readers of /proc/mdstat
235 * can use 'poll' or 'select' to find out when the event
239 * start array, stop array, error, add device, remove device,
240 * start build, activate spare
242 static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
243 static atomic_t md_event_count;
244 void md_new_event(struct mddev *mddev)
246 atomic_inc(&md_event_count);
247 wake_up(&md_event_waiters);
249 EXPORT_SYMBOL_GPL(md_new_event);
251 /* Alternate version that can be called from interrupts
252 * when calling sysfs_notify isn't needed.
254 static void md_new_event_inintr(struct mddev *mddev)
256 atomic_inc(&md_event_count);
257 wake_up(&md_event_waiters);
261 * Enables to iterate over all existing md arrays
262 * all_mddevs_lock protects this list.
264 static LIST_HEAD(all_mddevs);
265 static DEFINE_SPINLOCK(all_mddevs_lock);
269 * iterates through all used mddevs in the system.
270 * We take care to grab the all_mddevs_lock whenever navigating
271 * the list, and to always hold a refcount when unlocked.
272 * Any code which breaks out of this loop while own
273 * a reference to the current mddev and must mddev_put it.
275 #define for_each_mddev(_mddev,_tmp) \
277 for (({ spin_lock(&all_mddevs_lock); \
278 _tmp = all_mddevs.next; \
280 ({ if (_tmp != &all_mddevs) \
281 mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\
282 spin_unlock(&all_mddevs_lock); \
283 if (_mddev) mddev_put(_mddev); \
284 _mddev = list_entry(_tmp, struct mddev, all_mddevs); \
285 _tmp != &all_mddevs;}); \
286 ({ spin_lock(&all_mddevs_lock); \
287 _tmp = _tmp->next;}) \
291 /* Rather than calling directly into the personality make_request function,
292 * IO requests come here first so that we can check if the device is
293 * being suspended pending a reconfiguration.
294 * We hold a refcount over the call to ->make_request. By the time that
295 * call has finished, the bio has been linked into some internal structure
296 * and so is visible to ->quiesce(), so we don't need the refcount any more.
298 static void md_make_request(struct request_queue *q, struct bio *bio)
300 const int rw = bio_data_dir(bio);
301 struct mddev *mddev = q->queuedata;
303 unsigned int sectors;
305 if (mddev == NULL || mddev->pers == NULL
310 smp_rmb(); /* Ensure implications of 'active' are visible */
312 if (mddev->suspended) {
315 prepare_to_wait(&mddev->sb_wait, &__wait,
316 TASK_UNINTERRUPTIBLE);
317 if (!mddev->suspended)
323 finish_wait(&mddev->sb_wait, &__wait);
325 atomic_inc(&mddev->active_io);
329 * save the sectors now since our bio can
330 * go away inside make_request
332 sectors = bio_sectors(bio);
333 mddev->pers->make_request(mddev, bio);
335 cpu = part_stat_lock();
336 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
337 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw], sectors);
340 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
341 wake_up(&mddev->sb_wait);
344 /* mddev_suspend makes sure no new requests are submitted
345 * to the device, and that any requests that have been submitted
346 * are completely handled.
347 * Once ->stop is called and completes, the module will be completely
350 void mddev_suspend(struct mddev *mddev)
352 BUG_ON(mddev->suspended);
353 mddev->suspended = 1;
355 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
356 mddev->pers->quiesce(mddev, 1);
358 del_timer_sync(&mddev->safemode_timer);
360 EXPORT_SYMBOL_GPL(mddev_suspend);
362 void mddev_resume(struct mddev *mddev)
364 mddev->suspended = 0;
365 wake_up(&mddev->sb_wait);
366 mddev->pers->quiesce(mddev, 0);
368 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
369 md_wakeup_thread(mddev->thread);
370 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
372 EXPORT_SYMBOL_GPL(mddev_resume);
374 int mddev_congested(struct mddev *mddev, int bits)
376 return mddev->suspended;
378 EXPORT_SYMBOL(mddev_congested);
381 * Generic flush handling for md
384 static void md_end_flush(struct bio *bio, int err)
386 struct md_rdev *rdev = bio->bi_private;
387 struct mddev *mddev = rdev->mddev;
389 rdev_dec_pending(rdev, mddev);
391 if (atomic_dec_and_test(&mddev->flush_pending)) {
392 /* The pre-request flush has finished */
393 queue_work(md_wq, &mddev->flush_work);
398 static void md_submit_flush_data(struct work_struct *ws);
400 static void submit_flushes(struct work_struct *ws)
402 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
403 struct md_rdev *rdev;
405 INIT_WORK(&mddev->flush_work, md_submit_flush_data);
406 atomic_set(&mddev->flush_pending, 1);
408 rdev_for_each_rcu(rdev, mddev)
409 if (rdev->raid_disk >= 0 &&
410 !test_bit(Faulty, &rdev->flags)) {
411 /* Take two references, one is dropped
412 * when request finishes, one after
413 * we reclaim rcu_read_lock
416 atomic_inc(&rdev->nr_pending);
417 atomic_inc(&rdev->nr_pending);
419 bi = bio_alloc_mddev(GFP_NOIO, 0, mddev);
420 bi->bi_end_io = md_end_flush;
421 bi->bi_private = rdev;
422 bi->bi_bdev = rdev->bdev;
423 atomic_inc(&mddev->flush_pending);
424 submit_bio(WRITE_FLUSH, bi);
426 rdev_dec_pending(rdev, mddev);
429 if (atomic_dec_and_test(&mddev->flush_pending))
430 queue_work(md_wq, &mddev->flush_work);
433 static void md_submit_flush_data(struct work_struct *ws)
435 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
436 struct bio *bio = mddev->flush_bio;
438 if (bio->bi_size == 0)
439 /* an empty barrier - all done */
442 bio->bi_rw &= ~REQ_FLUSH;
443 mddev->pers->make_request(mddev, bio);
446 mddev->flush_bio = NULL;
447 wake_up(&mddev->sb_wait);
450 void md_flush_request(struct mddev *mddev, struct bio *bio)
452 spin_lock_irq(&mddev->write_lock);
453 wait_event_lock_irq(mddev->sb_wait,
456 mddev->flush_bio = bio;
457 spin_unlock_irq(&mddev->write_lock);
459 INIT_WORK(&mddev->flush_work, submit_flushes);
460 queue_work(md_wq, &mddev->flush_work);
462 EXPORT_SYMBOL(md_flush_request);
464 void md_unplug(struct blk_plug_cb *cb, bool from_schedule)
466 struct mddev *mddev = cb->data;
467 md_wakeup_thread(mddev->thread);
470 EXPORT_SYMBOL(md_unplug);
472 static inline struct mddev *mddev_get(struct mddev *mddev)
474 atomic_inc(&mddev->active);
478 static void mddev_delayed_delete(struct work_struct *ws);
480 static void mddev_put(struct mddev *mddev)
482 struct bio_set *bs = NULL;
484 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
486 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
487 mddev->ctime == 0 && !mddev->hold_active) {
488 /* Array is not configured at all, and not held active,
490 list_del_init(&mddev->all_mddevs);
492 mddev->bio_set = NULL;
493 if (mddev->gendisk) {
494 /* We did a probe so need to clean up. Call
495 * queue_work inside the spinlock so that
496 * flush_workqueue() after mddev_find will
497 * succeed in waiting for the work to be done.
499 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
500 queue_work(md_misc_wq, &mddev->del_work);
504 spin_unlock(&all_mddevs_lock);
509 void mddev_init(struct mddev *mddev)
511 mutex_init(&mddev->open_mutex);
512 mutex_init(&mddev->reconfig_mutex);
513 mutex_init(&mddev->bitmap_info.mutex);
514 INIT_LIST_HEAD(&mddev->disks);
515 INIT_LIST_HEAD(&mddev->all_mddevs);
516 init_timer(&mddev->safemode_timer);
517 atomic_set(&mddev->active, 1);
518 atomic_set(&mddev->openers, 0);
519 atomic_set(&mddev->active_io, 0);
520 spin_lock_init(&mddev->write_lock);
521 atomic_set(&mddev->flush_pending, 0);
522 init_waitqueue_head(&mddev->sb_wait);
523 init_waitqueue_head(&mddev->recovery_wait);
524 mddev->reshape_position = MaxSector;
525 mddev->reshape_backwards = 0;
526 mddev->resync_min = 0;
527 mddev->resync_max = MaxSector;
528 mddev->level = LEVEL_NONE;
530 EXPORT_SYMBOL_GPL(mddev_init);
532 static struct mddev * mddev_find(dev_t unit)
534 struct mddev *mddev, *new = NULL;
536 if (unit && MAJOR(unit) != MD_MAJOR)
537 unit &= ~((1<<MdpMinorShift)-1);
540 spin_lock(&all_mddevs_lock);
543 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
544 if (mddev->unit == unit) {
546 spin_unlock(&all_mddevs_lock);
552 list_add(&new->all_mddevs, &all_mddevs);
553 spin_unlock(&all_mddevs_lock);
554 new->hold_active = UNTIL_IOCTL;
558 /* find an unused unit number */
559 static int next_minor = 512;
560 int start = next_minor;
564 dev = MKDEV(MD_MAJOR, next_minor);
566 if (next_minor > MINORMASK)
568 if (next_minor == start) {
569 /* Oh dear, all in use. */
570 spin_unlock(&all_mddevs_lock);
576 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
577 if (mddev->unit == dev) {
583 new->md_minor = MINOR(dev);
584 new->hold_active = UNTIL_STOP;
585 list_add(&new->all_mddevs, &all_mddevs);
586 spin_unlock(&all_mddevs_lock);
589 spin_unlock(&all_mddevs_lock);
591 new = kzalloc(sizeof(*new), GFP_KERNEL);
596 if (MAJOR(unit) == MD_MAJOR)
597 new->md_minor = MINOR(unit);
599 new->md_minor = MINOR(unit) >> MdpMinorShift;
606 static inline int mddev_lock(struct mddev * mddev)
608 return mutex_lock_interruptible(&mddev->reconfig_mutex);
611 static inline int mddev_is_locked(struct mddev *mddev)
613 return mutex_is_locked(&mddev->reconfig_mutex);
616 static inline int mddev_trylock(struct mddev * mddev)
618 return mutex_trylock(&mddev->reconfig_mutex);
621 static struct attribute_group md_redundancy_group;
623 static void mddev_unlock(struct mddev * mddev)
625 if (mddev->to_remove) {
626 /* These cannot be removed under reconfig_mutex as
627 * an access to the files will try to take reconfig_mutex
628 * while holding the file unremovable, which leads to
630 * So hold set sysfs_active while the remove in happeing,
631 * and anything else which might set ->to_remove or my
632 * otherwise change the sysfs namespace will fail with
633 * -EBUSY if sysfs_active is still set.
634 * We set sysfs_active under reconfig_mutex and elsewhere
635 * test it under the same mutex to ensure its correct value
638 struct attribute_group *to_remove = mddev->to_remove;
639 mddev->to_remove = NULL;
640 mddev->sysfs_active = 1;
641 mutex_unlock(&mddev->reconfig_mutex);
643 if (mddev->kobj.sd) {
644 if (to_remove != &md_redundancy_group)
645 sysfs_remove_group(&mddev->kobj, to_remove);
646 if (mddev->pers == NULL ||
647 mddev->pers->sync_request == NULL) {
648 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
649 if (mddev->sysfs_action)
650 sysfs_put(mddev->sysfs_action);
651 mddev->sysfs_action = NULL;
654 mddev->sysfs_active = 0;
656 mutex_unlock(&mddev->reconfig_mutex);
658 /* As we've dropped the mutex we need a spinlock to
659 * make sure the thread doesn't disappear
661 spin_lock(&pers_lock);
662 md_wakeup_thread(mddev->thread);
663 spin_unlock(&pers_lock);
666 static struct md_rdev * find_rdev_nr(struct mddev *mddev, int nr)
668 struct md_rdev *rdev;
670 rdev_for_each(rdev, mddev)
671 if (rdev->desc_nr == nr)
677 static struct md_rdev *find_rdev_nr_rcu(struct mddev *mddev, int nr)
679 struct md_rdev *rdev;
681 rdev_for_each_rcu(rdev, mddev)
682 if (rdev->desc_nr == nr)
688 static struct md_rdev *find_rdev(struct mddev *mddev, dev_t dev)
690 struct md_rdev *rdev;
692 rdev_for_each(rdev, mddev)
693 if (rdev->bdev->bd_dev == dev)
699 static struct md_rdev *find_rdev_rcu(struct mddev *mddev, dev_t dev)
701 struct md_rdev *rdev;
703 rdev_for_each_rcu(rdev, mddev)
704 if (rdev->bdev->bd_dev == dev)
710 static struct md_personality *find_pers(int level, char *clevel)
712 struct md_personality *pers;
713 list_for_each_entry(pers, &pers_list, list) {
714 if (level != LEVEL_NONE && pers->level == level)
716 if (strcmp(pers->name, clevel)==0)
722 /* return the offset of the super block in 512byte sectors */
723 static inline sector_t calc_dev_sboffset(struct md_rdev *rdev)
725 sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512;
726 return MD_NEW_SIZE_SECTORS(num_sectors);
729 static int alloc_disk_sb(struct md_rdev * rdev)
734 rdev->sb_page = alloc_page(GFP_KERNEL);
735 if (!rdev->sb_page) {
736 printk(KERN_ALERT "md: out of memory.\n");
743 void md_rdev_clear(struct md_rdev *rdev)
746 put_page(rdev->sb_page);
748 rdev->sb_page = NULL;
753 put_page(rdev->bb_page);
754 rdev->bb_page = NULL;
756 kfree(rdev->badblocks.page);
757 rdev->badblocks.page = NULL;
759 EXPORT_SYMBOL_GPL(md_rdev_clear);
761 static void super_written(struct bio *bio, int error)
763 struct md_rdev *rdev = bio->bi_private;
764 struct mddev *mddev = rdev->mddev;
766 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
767 printk("md: super_written gets error=%d, uptodate=%d\n",
768 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
769 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
770 md_error(mddev, rdev);
773 if (atomic_dec_and_test(&mddev->pending_writes))
774 wake_up(&mddev->sb_wait);
778 void md_super_write(struct mddev *mddev, struct md_rdev *rdev,
779 sector_t sector, int size, struct page *page)
781 /* write first size bytes of page to sector of rdev
782 * Increment mddev->pending_writes before returning
783 * and decrement it on completion, waking up sb_wait
784 * if zero is reached.
785 * If an error occurred, call md_error
787 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev);
789 bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev;
790 bio->bi_sector = sector;
791 bio_add_page(bio, page, size, 0);
792 bio->bi_private = rdev;
793 bio->bi_end_io = super_written;
795 atomic_inc(&mddev->pending_writes);
796 submit_bio(WRITE_FLUSH_FUA, bio);
799 void md_super_wait(struct mddev *mddev)
801 /* wait for all superblock writes that were scheduled to complete */
804 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
805 if (atomic_read(&mddev->pending_writes)==0)
809 finish_wait(&mddev->sb_wait, &wq);
812 static void bi_complete(struct bio *bio, int error)
814 complete((struct completion*)bio->bi_private);
817 int sync_page_io(struct md_rdev *rdev, sector_t sector, int size,
818 struct page *page, int rw, bool metadata_op)
820 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev);
821 struct completion event;
826 bio->bi_bdev = (metadata_op && rdev->meta_bdev) ?
827 rdev->meta_bdev : rdev->bdev;
829 bio->bi_sector = sector + rdev->sb_start;
830 else if (rdev->mddev->reshape_position != MaxSector &&
831 (rdev->mddev->reshape_backwards ==
832 (sector >= rdev->mddev->reshape_position)))
833 bio->bi_sector = sector + rdev->new_data_offset;
835 bio->bi_sector = sector + rdev->data_offset;
836 bio_add_page(bio, page, size, 0);
837 init_completion(&event);
838 bio->bi_private = &event;
839 bio->bi_end_io = bi_complete;
841 wait_for_completion(&event);
843 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
847 EXPORT_SYMBOL_GPL(sync_page_io);
849 static int read_disk_sb(struct md_rdev * rdev, int size)
851 char b[BDEVNAME_SIZE];
852 if (!rdev->sb_page) {
860 if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true))
866 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
867 bdevname(rdev->bdev,b));
871 static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
873 return sb1->set_uuid0 == sb2->set_uuid0 &&
874 sb1->set_uuid1 == sb2->set_uuid1 &&
875 sb1->set_uuid2 == sb2->set_uuid2 &&
876 sb1->set_uuid3 == sb2->set_uuid3;
879 static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
882 mdp_super_t *tmp1, *tmp2;
884 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
885 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
887 if (!tmp1 || !tmp2) {
889 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
897 * nr_disks is not constant
902 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
910 static u32 md_csum_fold(u32 csum)
912 csum = (csum & 0xffff) + (csum >> 16);
913 return (csum & 0xffff) + (csum >> 16);
916 static unsigned int calc_sb_csum(mdp_super_t * sb)
919 u32 *sb32 = (u32*)sb;
921 unsigned int disk_csum, csum;
923 disk_csum = sb->sb_csum;
926 for (i = 0; i < MD_SB_BYTES/4 ; i++)
928 csum = (newcsum & 0xffffffff) + (newcsum>>32);
932 /* This used to use csum_partial, which was wrong for several
933 * reasons including that different results are returned on
934 * different architectures. It isn't critical that we get exactly
935 * the same return value as before (we always csum_fold before
936 * testing, and that removes any differences). However as we
937 * know that csum_partial always returned a 16bit value on
938 * alphas, do a fold to maximise conformity to previous behaviour.
940 sb->sb_csum = md_csum_fold(disk_csum);
942 sb->sb_csum = disk_csum;
949 * Handle superblock details.
950 * We want to be able to handle multiple superblock formats
951 * so we have a common interface to them all, and an array of
952 * different handlers.
953 * We rely on user-space to write the initial superblock, and support
954 * reading and updating of superblocks.
955 * Interface methods are:
956 * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
957 * loads and validates a superblock on dev.
958 * if refdev != NULL, compare superblocks on both devices
960 * 0 - dev has a superblock that is compatible with refdev
961 * 1 - dev has a superblock that is compatible and newer than refdev
962 * so dev should be used as the refdev in future
963 * -EINVAL superblock incompatible or invalid
964 * -othererror e.g. -EIO
966 * int validate_super(struct mddev *mddev, struct md_rdev *dev)
967 * Verify that dev is acceptable into mddev.
968 * The first time, mddev->raid_disks will be 0, and data from
969 * dev should be merged in. Subsequent calls check that dev
970 * is new enough. Return 0 or -EINVAL
972 * void sync_super(struct mddev *mddev, struct md_rdev *dev)
973 * Update the superblock for rdev with data in mddev
974 * This does not write to disc.
980 struct module *owner;
981 int (*load_super)(struct md_rdev *rdev,
982 struct md_rdev *refdev,
984 int (*validate_super)(struct mddev *mddev,
985 struct md_rdev *rdev);
986 void (*sync_super)(struct mddev *mddev,
987 struct md_rdev *rdev);
988 unsigned long long (*rdev_size_change)(struct md_rdev *rdev,
989 sector_t num_sectors);
990 int (*allow_new_offset)(struct md_rdev *rdev,
991 unsigned long long new_offset);
995 * Check that the given mddev has no bitmap.
997 * This function is called from the run method of all personalities that do not
998 * support bitmaps. It prints an error message and returns non-zero if mddev
999 * has a bitmap. Otherwise, it returns 0.
1002 int md_check_no_bitmap(struct mddev *mddev)
1004 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
1006 printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
1007 mdname(mddev), mddev->pers->name);
1010 EXPORT_SYMBOL(md_check_no_bitmap);
1013 * load_super for 0.90.0
1015 static int super_90_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
1017 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1022 * Calculate the position of the superblock (512byte sectors),
1023 * it's at the end of the disk.
1025 * It also happens to be a multiple of 4Kb.
1027 rdev->sb_start = calc_dev_sboffset(rdev);
1029 ret = read_disk_sb(rdev, MD_SB_BYTES);
1030 if (ret) return ret;
1034 bdevname(rdev->bdev, b);
1035 sb = page_address(rdev->sb_page);
1037 if (sb->md_magic != MD_SB_MAGIC) {
1038 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
1043 if (sb->major_version != 0 ||
1044 sb->minor_version < 90 ||
1045 sb->minor_version > 91) {
1046 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
1047 sb->major_version, sb->minor_version,
1052 if (sb->raid_disks <= 0)
1055 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
1056 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
1061 rdev->preferred_minor = sb->md_minor;
1062 rdev->data_offset = 0;
1063 rdev->new_data_offset = 0;
1064 rdev->sb_size = MD_SB_BYTES;
1065 rdev->badblocks.shift = -1;
1067 if (sb->level == LEVEL_MULTIPATH)
1070 rdev->desc_nr = sb->this_disk.number;
1076 mdp_super_t *refsb = page_address(refdev->sb_page);
1077 if (!uuid_equal(refsb, sb)) {
1078 printk(KERN_WARNING "md: %s has different UUID to %s\n",
1079 b, bdevname(refdev->bdev,b2));
1082 if (!sb_equal(refsb, sb)) {
1083 printk(KERN_WARNING "md: %s has same UUID"
1084 " but different superblock to %s\n",
1085 b, bdevname(refdev->bdev, b2));
1089 ev2 = md_event(refsb);
1095 rdev->sectors = rdev->sb_start;
1096 /* Limit to 4TB as metadata cannot record more than that.
1097 * (not needed for Linear and RAID0 as metadata doesn't
1100 if (rdev->sectors >= (2ULL << 32) && sb->level >= 1)
1101 rdev->sectors = (2ULL << 32) - 2;
1103 if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1)
1104 /* "this cannot possibly happen" ... */
1112 * validate_super for 0.90.0
1114 static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev)
1117 mdp_super_t *sb = page_address(rdev->sb_page);
1118 __u64 ev1 = md_event(sb);
1120 rdev->raid_disk = -1;
1121 clear_bit(Faulty, &rdev->flags);
1122 clear_bit(In_sync, &rdev->flags);
1123 clear_bit(WriteMostly, &rdev->flags);
1125 if (mddev->raid_disks == 0) {
1126 mddev->major_version = 0;
1127 mddev->minor_version = sb->minor_version;
1128 mddev->patch_version = sb->patch_version;
1129 mddev->external = 0;
1130 mddev->chunk_sectors = sb->chunk_size >> 9;
1131 mddev->ctime = sb->ctime;
1132 mddev->utime = sb->utime;
1133 mddev->level = sb->level;
1134 mddev->clevel[0] = 0;
1135 mddev->layout = sb->layout;
1136 mddev->raid_disks = sb->raid_disks;
1137 mddev->dev_sectors = ((sector_t)sb->size) * 2;
1138 mddev->events = ev1;
1139 mddev->bitmap_info.offset = 0;
1140 mddev->bitmap_info.space = 0;
1141 /* bitmap can use 60 K after the 4K superblocks */
1142 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
1143 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
1144 mddev->reshape_backwards = 0;
1146 if (mddev->minor_version >= 91) {
1147 mddev->reshape_position = sb->reshape_position;
1148 mddev->delta_disks = sb->delta_disks;
1149 mddev->new_level = sb->new_level;
1150 mddev->new_layout = sb->new_layout;
1151 mddev->new_chunk_sectors = sb->new_chunk >> 9;
1152 if (mddev->delta_disks < 0)
1153 mddev->reshape_backwards = 1;
1155 mddev->reshape_position = MaxSector;
1156 mddev->delta_disks = 0;
1157 mddev->new_level = mddev->level;
1158 mddev->new_layout = mddev->layout;
1159 mddev->new_chunk_sectors = mddev->chunk_sectors;
1162 if (sb->state & (1<<MD_SB_CLEAN))
1163 mddev->recovery_cp = MaxSector;
1165 if (sb->events_hi == sb->cp_events_hi &&
1166 sb->events_lo == sb->cp_events_lo) {
1167 mddev->recovery_cp = sb->recovery_cp;
1169 mddev->recovery_cp = 0;
1172 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1173 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1174 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1175 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1177 mddev->max_disks = MD_SB_DISKS;
1179 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
1180 mddev->bitmap_info.file == NULL) {
1181 mddev->bitmap_info.offset =
1182 mddev->bitmap_info.default_offset;
1183 mddev->bitmap_info.space =
1184 mddev->bitmap_info.space;
1187 } else if (mddev->pers == NULL) {
1188 /* Insist on good event counter while assembling, except
1189 * for spares (which don't need an event count) */
1191 if (sb->disks[rdev->desc_nr].state & (
1192 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
1193 if (ev1 < mddev->events)
1195 } else if (mddev->bitmap) {
1196 /* if adding to array with a bitmap, then we can accept an
1197 * older device ... but not too old.
1199 if (ev1 < mddev->bitmap->events_cleared)
1202 if (ev1 < mddev->events)
1203 /* just a hot-add of a new device, leave raid_disk at -1 */
1207 if (mddev->level != LEVEL_MULTIPATH) {
1208 desc = sb->disks + rdev->desc_nr;
1210 if (desc->state & (1<<MD_DISK_FAULTY))
1211 set_bit(Faulty, &rdev->flags);
1212 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1213 desc->raid_disk < mddev->raid_disks */) {
1214 set_bit(In_sync, &rdev->flags);
1215 rdev->raid_disk = desc->raid_disk;
1216 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1217 /* active but not in sync implies recovery up to
1218 * reshape position. We don't know exactly where
1219 * that is, so set to zero for now */
1220 if (mddev->minor_version >= 91) {
1221 rdev->recovery_offset = 0;
1222 rdev->raid_disk = desc->raid_disk;
1225 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1226 set_bit(WriteMostly, &rdev->flags);
1227 } else /* MULTIPATH are always insync */
1228 set_bit(In_sync, &rdev->flags);
1233 * sync_super for 0.90.0
1235 static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev)
1238 struct md_rdev *rdev2;
1239 int next_spare = mddev->raid_disks;
1242 /* make rdev->sb match mddev data..
1245 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1246 * 3/ any empty disks < next_spare become removed
1248 * disks[0] gets initialised to REMOVED because
1249 * we cannot be sure from other fields if it has
1250 * been initialised or not.
1253 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1255 rdev->sb_size = MD_SB_BYTES;
1257 sb = page_address(rdev->sb_page);
1259 memset(sb, 0, sizeof(*sb));
1261 sb->md_magic = MD_SB_MAGIC;
1262 sb->major_version = mddev->major_version;
1263 sb->patch_version = mddev->patch_version;
1264 sb->gvalid_words = 0; /* ignored */
1265 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1266 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1267 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1268 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1270 sb->ctime = mddev->ctime;
1271 sb->level = mddev->level;
1272 sb->size = mddev->dev_sectors / 2;
1273 sb->raid_disks = mddev->raid_disks;
1274 sb->md_minor = mddev->md_minor;
1275 sb->not_persistent = 0;
1276 sb->utime = mddev->utime;
1278 sb->events_hi = (mddev->events>>32);
1279 sb->events_lo = (u32)mddev->events;
1281 if (mddev->reshape_position == MaxSector)
1282 sb->minor_version = 90;
1284 sb->minor_version = 91;
1285 sb->reshape_position = mddev->reshape_position;
1286 sb->new_level = mddev->new_level;
1287 sb->delta_disks = mddev->delta_disks;
1288 sb->new_layout = mddev->new_layout;
1289 sb->new_chunk = mddev->new_chunk_sectors << 9;
1291 mddev->minor_version = sb->minor_version;
1294 sb->recovery_cp = mddev->recovery_cp;
1295 sb->cp_events_hi = (mddev->events>>32);
1296 sb->cp_events_lo = (u32)mddev->events;
1297 if (mddev->recovery_cp == MaxSector)
1298 sb->state = (1<< MD_SB_CLEAN);
1300 sb->recovery_cp = 0;
1302 sb->layout = mddev->layout;
1303 sb->chunk_size = mddev->chunk_sectors << 9;
1305 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
1306 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1308 sb->disks[0].state = (1<<MD_DISK_REMOVED);
1309 rdev_for_each(rdev2, mddev) {
1312 int is_active = test_bit(In_sync, &rdev2->flags);
1314 if (rdev2->raid_disk >= 0 &&
1315 sb->minor_version >= 91)
1316 /* we have nowhere to store the recovery_offset,
1317 * but if it is not below the reshape_position,
1318 * we can piggy-back on that.
1321 if (rdev2->raid_disk < 0 ||
1322 test_bit(Faulty, &rdev2->flags))
1325 desc_nr = rdev2->raid_disk;
1327 desc_nr = next_spare++;
1328 rdev2->desc_nr = desc_nr;
1329 d = &sb->disks[rdev2->desc_nr];
1331 d->number = rdev2->desc_nr;
1332 d->major = MAJOR(rdev2->bdev->bd_dev);
1333 d->minor = MINOR(rdev2->bdev->bd_dev);
1335 d->raid_disk = rdev2->raid_disk;
1337 d->raid_disk = rdev2->desc_nr; /* compatibility */
1338 if (test_bit(Faulty, &rdev2->flags))
1339 d->state = (1<<MD_DISK_FAULTY);
1340 else if (is_active) {
1341 d->state = (1<<MD_DISK_ACTIVE);
1342 if (test_bit(In_sync, &rdev2->flags))
1343 d->state |= (1<<MD_DISK_SYNC);
1351 if (test_bit(WriteMostly, &rdev2->flags))
1352 d->state |= (1<<MD_DISK_WRITEMOSTLY);
1354 /* now set the "removed" and "faulty" bits on any missing devices */
1355 for (i=0 ; i < mddev->raid_disks ; i++) {
1356 mdp_disk_t *d = &sb->disks[i];
1357 if (d->state == 0 && d->number == 0) {
1360 d->state = (1<<MD_DISK_REMOVED);
1361 d->state |= (1<<MD_DISK_FAULTY);
1365 sb->nr_disks = nr_disks;
1366 sb->active_disks = active;
1367 sb->working_disks = working;
1368 sb->failed_disks = failed;
1369 sb->spare_disks = spare;
1371 sb->this_disk = sb->disks[rdev->desc_nr];
1372 sb->sb_csum = calc_sb_csum(sb);
1376 * rdev_size_change for 0.90.0
1378 static unsigned long long
1379 super_90_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
1381 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
1382 return 0; /* component must fit device */
1383 if (rdev->mddev->bitmap_info.offset)
1384 return 0; /* can't move bitmap */
1385 rdev->sb_start = calc_dev_sboffset(rdev);
1386 if (!num_sectors || num_sectors > rdev->sb_start)
1387 num_sectors = rdev->sb_start;
1388 /* Limit to 4TB as metadata cannot record more than that.
1389 * 4TB == 2^32 KB, or 2*2^32 sectors.
1391 if (num_sectors >= (2ULL << 32) && rdev->mddev->level >= 1)
1392 num_sectors = (2ULL << 32) - 2;
1393 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
1395 md_super_wait(rdev->mddev);
1400 super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset)
1402 /* non-zero offset changes not possible with v0.90 */
1403 return new_offset == 0;
1407 * version 1 superblock
1410 static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
1414 unsigned long long newcsum;
1415 int size = 256 + le32_to_cpu(sb->max_dev)*2;
1416 __le32 *isuper = (__le32*)sb;
1418 disk_csum = sb->sb_csum;
1421 for (; size >= 4; size -= 4)
1422 newcsum += le32_to_cpu(*isuper++);
1425 newcsum += le16_to_cpu(*(__le16*) isuper);
1427 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1428 sb->sb_csum = disk_csum;
1429 return cpu_to_le32(csum);
1432 static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
1434 static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
1436 struct mdp_superblock_1 *sb;
1440 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1444 * Calculate the position of the superblock in 512byte sectors.
1445 * It is always aligned to a 4K boundary and
1446 * depeding on minor_version, it can be:
1447 * 0: At least 8K, but less than 12K, from end of device
1448 * 1: At start of device
1449 * 2: 4K from start of device.
1451 switch(minor_version) {
1453 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
1455 sb_start &= ~(sector_t)(4*2-1);
1466 rdev->sb_start = sb_start;
1468 /* superblock is rarely larger than 1K, but it can be larger,
1469 * and it is safe to read 4k, so we do that
1471 ret = read_disk_sb(rdev, 4096);
1472 if (ret) return ret;
1475 sb = page_address(rdev->sb_page);
1477 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1478 sb->major_version != cpu_to_le32(1) ||
1479 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
1480 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
1481 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
1484 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1485 printk("md: invalid superblock checksum on %s\n",
1486 bdevname(rdev->bdev,b));
1489 if (le64_to_cpu(sb->data_size) < 10) {
1490 printk("md: data_size too small on %s\n",
1491 bdevname(rdev->bdev,b));
1496 memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1])))
1497 /* Some padding is non-zero, might be a new feature */
1500 rdev->preferred_minor = 0xffff;
1501 rdev->data_offset = le64_to_cpu(sb->data_offset);
1502 rdev->new_data_offset = rdev->data_offset;
1503 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) &&
1504 (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET))
1505 rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset);
1506 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
1508 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
1509 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1510 if (rdev->sb_size & bmask)
1511 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1514 && rdev->data_offset < sb_start + (rdev->sb_size/512))
1517 && rdev->new_data_offset < sb_start + (rdev->sb_size/512))
1520 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1523 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1525 if (!rdev->bb_page) {
1526 rdev->bb_page = alloc_page(GFP_KERNEL);
1530 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) &&
1531 rdev->badblocks.count == 0) {
1532 /* need to load the bad block list.
1533 * Currently we limit it to one page.
1539 int sectors = le16_to_cpu(sb->bblog_size);
1540 if (sectors > (PAGE_SIZE / 512))
1542 offset = le32_to_cpu(sb->bblog_offset);
1545 bb_sector = (long long)offset;
1546 if (!sync_page_io(rdev, bb_sector, sectors << 9,
1547 rdev->bb_page, READ, true))
1549 bbp = (u64 *)page_address(rdev->bb_page);
1550 rdev->badblocks.shift = sb->bblog_shift;
1551 for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) {
1552 u64 bb = le64_to_cpu(*bbp);
1553 int count = bb & (0x3ff);
1554 u64 sector = bb >> 10;
1555 sector <<= sb->bblog_shift;
1556 count <<= sb->bblog_shift;
1559 if (md_set_badblocks(&rdev->badblocks,
1560 sector, count, 1) == 0)
1563 } else if (sb->bblog_offset == 0)
1564 rdev->badblocks.shift = -1;
1570 struct mdp_superblock_1 *refsb = page_address(refdev->sb_page);
1572 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1573 sb->level != refsb->level ||
1574 sb->layout != refsb->layout ||
1575 sb->chunksize != refsb->chunksize) {
1576 printk(KERN_WARNING "md: %s has strangely different"
1577 " superblock to %s\n",
1578 bdevname(rdev->bdev,b),
1579 bdevname(refdev->bdev,b2));
1582 ev1 = le64_to_cpu(sb->events);
1583 ev2 = le64_to_cpu(refsb->events);
1590 if (minor_version) {
1591 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9);
1592 sectors -= rdev->data_offset;
1594 sectors = rdev->sb_start;
1595 if (sectors < le64_to_cpu(sb->data_size))
1597 rdev->sectors = le64_to_cpu(sb->data_size);
1601 static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev)
1603 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
1604 __u64 ev1 = le64_to_cpu(sb->events);
1606 rdev->raid_disk = -1;
1607 clear_bit(Faulty, &rdev->flags);
1608 clear_bit(In_sync, &rdev->flags);
1609 clear_bit(WriteMostly, &rdev->flags);
1611 if (mddev->raid_disks == 0) {
1612 mddev->major_version = 1;
1613 mddev->patch_version = 0;
1614 mddev->external = 0;
1615 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
1616 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1617 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1618 mddev->level = le32_to_cpu(sb->level);
1619 mddev->clevel[0] = 0;
1620 mddev->layout = le32_to_cpu(sb->layout);
1621 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
1622 mddev->dev_sectors = le64_to_cpu(sb->size);
1623 mddev->events = ev1;
1624 mddev->bitmap_info.offset = 0;
1625 mddev->bitmap_info.space = 0;
1626 /* Default location for bitmap is 1K after superblock
1627 * using 3K - total of 4K
1629 mddev->bitmap_info.default_offset = 1024 >> 9;
1630 mddev->bitmap_info.default_space = (4096-1024) >> 9;
1631 mddev->reshape_backwards = 0;
1633 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1634 memcpy(mddev->uuid, sb->set_uuid, 16);
1636 mddev->max_disks = (4096-256)/2;
1638 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
1639 mddev->bitmap_info.file == NULL) {
1640 mddev->bitmap_info.offset =
1641 (__s32)le32_to_cpu(sb->bitmap_offset);
1642 /* Metadata doesn't record how much space is available.
1643 * For 1.0, we assume we can use up to the superblock
1644 * if before, else to 4K beyond superblock.
1645 * For others, assume no change is possible.
1647 if (mddev->minor_version > 0)
1648 mddev->bitmap_info.space = 0;
1649 else if (mddev->bitmap_info.offset > 0)
1650 mddev->bitmap_info.space =
1651 8 - mddev->bitmap_info.offset;
1653 mddev->bitmap_info.space =
1654 -mddev->bitmap_info.offset;
1657 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1658 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1659 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1660 mddev->new_level = le32_to_cpu(sb->new_level);
1661 mddev->new_layout = le32_to_cpu(sb->new_layout);
1662 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
1663 if (mddev->delta_disks < 0 ||
1664 (mddev->delta_disks == 0 &&
1665 (le32_to_cpu(sb->feature_map)
1666 & MD_FEATURE_RESHAPE_BACKWARDS)))
1667 mddev->reshape_backwards = 1;
1669 mddev->reshape_position = MaxSector;
1670 mddev->delta_disks = 0;
1671 mddev->new_level = mddev->level;
1672 mddev->new_layout = mddev->layout;
1673 mddev->new_chunk_sectors = mddev->chunk_sectors;
1676 } else if (mddev->pers == NULL) {
1677 /* Insist of good event counter while assembling, except for
1678 * spares (which don't need an event count) */
1680 if (rdev->desc_nr >= 0 &&
1681 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
1682 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe)
1683 if (ev1 < mddev->events)
1685 } else if (mddev->bitmap) {
1686 /* If adding to array with a bitmap, then we can accept an
1687 * older device, but not too old.
1689 if (ev1 < mddev->bitmap->events_cleared)
1692 if (ev1 < mddev->events)
1693 /* just a hot-add of a new device, leave raid_disk at -1 */
1696 if (mddev->level != LEVEL_MULTIPATH) {
1698 if (rdev->desc_nr < 0 ||
1699 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1703 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
1705 case 0xffff: /* spare */
1707 case 0xfffe: /* faulty */
1708 set_bit(Faulty, &rdev->flags);
1711 if ((le32_to_cpu(sb->feature_map) &
1712 MD_FEATURE_RECOVERY_OFFSET))
1713 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1715 set_bit(In_sync, &rdev->flags);
1716 rdev->raid_disk = role;
1719 if (sb->devflags & WriteMostly1)
1720 set_bit(WriteMostly, &rdev->flags);
1721 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT)
1722 set_bit(Replacement, &rdev->flags);
1723 } else /* MULTIPATH are always insync */
1724 set_bit(In_sync, &rdev->flags);
1729 static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev)
1731 struct mdp_superblock_1 *sb;
1732 struct md_rdev *rdev2;
1734 /* make rdev->sb match mddev and rdev data. */
1736 sb = page_address(rdev->sb_page);
1738 sb->feature_map = 0;
1740 sb->recovery_offset = cpu_to_le64(0);
1741 memset(sb->pad3, 0, sizeof(sb->pad3));
1743 sb->utime = cpu_to_le64((__u64)mddev->utime);
1744 sb->events = cpu_to_le64(mddev->events);
1746 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1748 sb->resync_offset = cpu_to_le64(0);
1750 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
1752 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
1753 sb->size = cpu_to_le64(mddev->dev_sectors);
1754 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
1755 sb->level = cpu_to_le32(mddev->level);
1756 sb->layout = cpu_to_le32(mddev->layout);
1758 if (test_bit(WriteMostly, &rdev->flags))
1759 sb->devflags |= WriteMostly1;
1761 sb->devflags &= ~WriteMostly1;
1762 sb->data_offset = cpu_to_le64(rdev->data_offset);
1763 sb->data_size = cpu_to_le64(rdev->sectors);
1765 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1766 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
1767 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
1770 if (rdev->raid_disk >= 0 &&
1771 !test_bit(In_sync, &rdev->flags)) {
1773 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1774 sb->recovery_offset =
1775 cpu_to_le64(rdev->recovery_offset);
1777 if (test_bit(Replacement, &rdev->flags))
1779 cpu_to_le32(MD_FEATURE_REPLACEMENT);
1781 if (mddev->reshape_position != MaxSector) {
1782 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1783 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1784 sb->new_layout = cpu_to_le32(mddev->new_layout);
1785 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1786 sb->new_level = cpu_to_le32(mddev->new_level);
1787 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
1788 if (mddev->delta_disks == 0 &&
1789 mddev->reshape_backwards)
1791 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS);
1792 if (rdev->new_data_offset != rdev->data_offset) {
1794 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET);
1795 sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset
1796 - rdev->data_offset));
1800 if (rdev->badblocks.count == 0)
1801 /* Nothing to do for bad blocks*/ ;
1802 else if (sb->bblog_offset == 0)
1803 /* Cannot record bad blocks on this device */
1804 md_error(mddev, rdev);
1806 struct badblocks *bb = &rdev->badblocks;
1807 u64 *bbp = (u64 *)page_address(rdev->bb_page);
1809 sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
1814 seq = read_seqbegin(&bb->lock);
1816 memset(bbp, 0xff, PAGE_SIZE);
1818 for (i = 0 ; i < bb->count ; i++) {
1819 u64 internal_bb = p[i];
1820 u64 store_bb = ((BB_OFFSET(internal_bb) << 10)
1821 | BB_LEN(internal_bb));
1822 bbp[i] = cpu_to_le64(store_bb);
1825 if (read_seqretry(&bb->lock, seq))
1828 bb->sector = (rdev->sb_start +
1829 (int)le32_to_cpu(sb->bblog_offset));
1830 bb->size = le16_to_cpu(sb->bblog_size);
1835 rdev_for_each(rdev2, mddev)
1836 if (rdev2->desc_nr+1 > max_dev)
1837 max_dev = rdev2->desc_nr+1;
1839 if (max_dev > le32_to_cpu(sb->max_dev)) {
1841 sb->max_dev = cpu_to_le32(max_dev);
1842 rdev->sb_size = max_dev * 2 + 256;
1843 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1844 if (rdev->sb_size & bmask)
1845 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1847 max_dev = le32_to_cpu(sb->max_dev);
1849 for (i=0; i<max_dev;i++)
1850 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1852 rdev_for_each(rdev2, mddev) {
1854 if (test_bit(Faulty, &rdev2->flags))
1855 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1856 else if (test_bit(In_sync, &rdev2->flags))
1857 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
1858 else if (rdev2->raid_disk >= 0)
1859 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
1861 sb->dev_roles[i] = cpu_to_le16(0xffff);
1864 sb->sb_csum = calc_sb_1_csum(sb);
1867 static unsigned long long
1868 super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
1870 struct mdp_superblock_1 *sb;
1871 sector_t max_sectors;
1872 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
1873 return 0; /* component must fit device */
1874 if (rdev->data_offset != rdev->new_data_offset)
1875 return 0; /* too confusing */
1876 if (rdev->sb_start < rdev->data_offset) {
1877 /* minor versions 1 and 2; superblock before data */
1878 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
1879 max_sectors -= rdev->data_offset;
1880 if (!num_sectors || num_sectors > max_sectors)
1881 num_sectors = max_sectors;
1882 } else if (rdev->mddev->bitmap_info.offset) {
1883 /* minor version 0 with bitmap we can't move */
1886 /* minor version 0; superblock after data */
1888 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
1889 sb_start &= ~(sector_t)(4*2 - 1);
1890 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
1891 if (!num_sectors || num_sectors > max_sectors)
1892 num_sectors = max_sectors;
1893 rdev->sb_start = sb_start;
1895 sb = page_address(rdev->sb_page);
1896 sb->data_size = cpu_to_le64(num_sectors);
1897 sb->super_offset = rdev->sb_start;
1898 sb->sb_csum = calc_sb_1_csum(sb);
1899 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
1901 md_super_wait(rdev->mddev);
1907 super_1_allow_new_offset(struct md_rdev *rdev,
1908 unsigned long long new_offset)
1910 /* All necessary checks on new >= old have been done */
1911 struct bitmap *bitmap;
1912 if (new_offset >= rdev->data_offset)
1915 /* with 1.0 metadata, there is no metadata to tread on
1916 * so we can always move back */
1917 if (rdev->mddev->minor_version == 0)
1920 /* otherwise we must be sure not to step on
1921 * any metadata, so stay:
1922 * 36K beyond start of superblock
1923 * beyond end of badblocks
1924 * beyond write-intent bitmap
1926 if (rdev->sb_start + (32+4)*2 > new_offset)
1928 bitmap = rdev->mddev->bitmap;
1929 if (bitmap && !rdev->mddev->bitmap_info.file &&
1930 rdev->sb_start + rdev->mddev->bitmap_info.offset +
1931 bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset)
1933 if (rdev->badblocks.sector + rdev->badblocks.size > new_offset)
1939 static struct super_type super_types[] = {
1942 .owner = THIS_MODULE,
1943 .load_super = super_90_load,
1944 .validate_super = super_90_validate,
1945 .sync_super = super_90_sync,
1946 .rdev_size_change = super_90_rdev_size_change,
1947 .allow_new_offset = super_90_allow_new_offset,
1951 .owner = THIS_MODULE,
1952 .load_super = super_1_load,
1953 .validate_super = super_1_validate,
1954 .sync_super = super_1_sync,
1955 .rdev_size_change = super_1_rdev_size_change,
1956 .allow_new_offset = super_1_allow_new_offset,
1960 static void sync_super(struct mddev *mddev, struct md_rdev *rdev)
1962 if (mddev->sync_super) {
1963 mddev->sync_super(mddev, rdev);
1967 BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
1969 super_types[mddev->major_version].sync_super(mddev, rdev);
1972 static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2)
1974 struct md_rdev *rdev, *rdev2;
1977 rdev_for_each_rcu(rdev, mddev1)
1978 rdev_for_each_rcu(rdev2, mddev2)
1979 if (rdev->bdev->bd_contains ==
1980 rdev2->bdev->bd_contains) {
1988 static LIST_HEAD(pending_raid_disks);
1991 * Try to register data integrity profile for an mddev
1993 * This is called when an array is started and after a disk has been kicked
1994 * from the array. It only succeeds if all working and active component devices
1995 * are integrity capable with matching profiles.
1997 int md_integrity_register(struct mddev *mddev)
1999 struct md_rdev *rdev, *reference = NULL;
2001 if (list_empty(&mddev->disks))
2002 return 0; /* nothing to do */
2003 if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
2004 return 0; /* shouldn't register, or already is */
2005 rdev_for_each(rdev, mddev) {
2006 /* skip spares and non-functional disks */
2007 if (test_bit(Faulty, &rdev->flags))
2009 if (rdev->raid_disk < 0)
2012 /* Use the first rdev as the reference */
2016 /* does this rdev's profile match the reference profile? */
2017 if (blk_integrity_compare(reference->bdev->bd_disk,
2018 rdev->bdev->bd_disk) < 0)
2021 if (!reference || !bdev_get_integrity(reference->bdev))
2024 * All component devices are integrity capable and have matching
2025 * profiles, register the common profile for the md device.
2027 if (blk_integrity_register(mddev->gendisk,
2028 bdev_get_integrity(reference->bdev)) != 0) {
2029 printk(KERN_ERR "md: failed to register integrity for %s\n",
2033 printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev));
2034 if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) {
2035 printk(KERN_ERR "md: failed to create integrity pool for %s\n",
2041 EXPORT_SYMBOL(md_integrity_register);
2043 /* Disable data integrity if non-capable/non-matching disk is being added */
2044 void md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev)
2046 struct blk_integrity *bi_rdev;
2047 struct blk_integrity *bi_mddev;
2049 if (!mddev->gendisk)
2052 bi_rdev = bdev_get_integrity(rdev->bdev);
2053 bi_mddev = blk_get_integrity(mddev->gendisk);
2055 if (!bi_mddev) /* nothing to do */
2057 if (rdev->raid_disk < 0) /* skip spares */
2059 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
2060 rdev->bdev->bd_disk) >= 0)
2062 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
2063 blk_integrity_unregister(mddev->gendisk);
2065 EXPORT_SYMBOL(md_integrity_add_rdev);
2067 static int bind_rdev_to_array(struct md_rdev * rdev, struct mddev * mddev)
2069 char b[BDEVNAME_SIZE];
2079 /* prevent duplicates */
2080 if (find_rdev(mddev, rdev->bdev->bd_dev))
2083 /* make sure rdev->sectors exceeds mddev->dev_sectors */
2084 if (rdev->sectors && (mddev->dev_sectors == 0 ||
2085 rdev->sectors < mddev->dev_sectors)) {
2087 /* Cannot change size, so fail
2088 * If mddev->level <= 0, then we don't care
2089 * about aligning sizes (e.g. linear)
2091 if (mddev->level > 0)
2094 mddev->dev_sectors = rdev->sectors;
2097 /* Verify rdev->desc_nr is unique.
2098 * If it is -1, assign a free number, else
2099 * check number is not in use
2101 if (rdev->desc_nr < 0) {
2103 if (mddev->pers) choice = mddev->raid_disks;
2104 while (find_rdev_nr(mddev, choice))
2106 rdev->desc_nr = choice;
2108 if (find_rdev_nr(mddev, rdev->desc_nr))
2111 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
2112 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
2113 mdname(mddev), mddev->max_disks);
2116 bdevname(rdev->bdev,b);
2117 while ( (s=strchr(b, '/')) != NULL)
2120 rdev->mddev = mddev;
2121 printk(KERN_INFO "md: bind<%s>\n", b);
2123 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
2126 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
2127 if (sysfs_create_link(&rdev->kobj, ko, "block"))
2128 /* failure here is OK */;
2129 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
2131 list_add_rcu(&rdev->same_set, &mddev->disks);
2132 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
2134 /* May as well allow recovery to be retried once */
2135 mddev->recovery_disabled++;
2140 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
2145 static void md_delayed_delete(struct work_struct *ws)
2147 struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work);
2148 kobject_del(&rdev->kobj);
2149 kobject_put(&rdev->kobj);
2152 static void unbind_rdev_from_array(struct md_rdev * rdev)
2154 char b[BDEVNAME_SIZE];
2159 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
2160 list_del_rcu(&rdev->same_set);
2161 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
2163 sysfs_remove_link(&rdev->kobj, "block");
2164 sysfs_put(rdev->sysfs_state);
2165 rdev->sysfs_state = NULL;
2166 rdev->badblocks.count = 0;
2167 /* We need to delay this, otherwise we can deadlock when
2168 * writing to 'remove' to "dev/state". We also need
2169 * to delay it due to rcu usage.
2172 INIT_WORK(&rdev->del_work, md_delayed_delete);
2173 kobject_get(&rdev->kobj);
2174 queue_work(md_misc_wq, &rdev->del_work);
2178 * prevent the device from being mounted, repartitioned or
2179 * otherwise reused by a RAID array (or any other kernel
2180 * subsystem), by bd_claiming the device.
2182 static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared)
2185 struct block_device *bdev;
2186 char b[BDEVNAME_SIZE];
2188 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
2189 shared ? (struct md_rdev *)lock_rdev : rdev);
2191 printk(KERN_ERR "md: could not open %s.\n",
2192 __bdevname(dev, b));
2193 return PTR_ERR(bdev);
2199 static void unlock_rdev(struct md_rdev *rdev)
2201 struct block_device *bdev = rdev->bdev;
2205 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
2208 void md_autodetect_dev(dev_t dev);
2210 static void export_rdev(struct md_rdev * rdev)
2212 char b[BDEVNAME_SIZE];
2213 printk(KERN_INFO "md: export_rdev(%s)\n",
2214 bdevname(rdev->bdev,b));
2217 md_rdev_clear(rdev);
2219 if (test_bit(AutoDetected, &rdev->flags))
2220 md_autodetect_dev(rdev->bdev->bd_dev);
2223 kobject_put(&rdev->kobj);
2226 static void kick_rdev_from_array(struct md_rdev * rdev)
2228 unbind_rdev_from_array(rdev);
2232 static void export_array(struct mddev *mddev)
2234 struct md_rdev *rdev, *tmp;
2236 rdev_for_each_safe(rdev, tmp, mddev) {
2241 kick_rdev_from_array(rdev);
2243 if (!list_empty(&mddev->disks))
2245 mddev->raid_disks = 0;
2246 mddev->major_version = 0;
2249 static void print_desc(mdp_disk_t *desc)
2251 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
2252 desc->major,desc->minor,desc->raid_disk,desc->state);
2255 static void print_sb_90(mdp_super_t *sb)
2260 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
2261 sb->major_version, sb->minor_version, sb->patch_version,
2262 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
2264 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
2265 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
2266 sb->md_minor, sb->layout, sb->chunk_size);
2267 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
2268 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
2269 sb->utime, sb->state, sb->active_disks, sb->working_disks,
2270 sb->failed_disks, sb->spare_disks,
2271 sb->sb_csum, (unsigned long)sb->events_lo);
2274 for (i = 0; i < MD_SB_DISKS; i++) {
2277 desc = sb->disks + i;
2278 if (desc->number || desc->major || desc->minor ||
2279 desc->raid_disk || (desc->state && (desc->state != 4))) {
2280 printk(" D %2d: ", i);
2284 printk(KERN_INFO "md: THIS: ");
2285 print_desc(&sb->this_disk);
2288 static void print_sb_1(struct mdp_superblock_1 *sb)
2292 uuid = sb->set_uuid;
2294 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n"
2295 "md: Name: \"%s\" CT:%llu\n",
2296 le32_to_cpu(sb->major_version),
2297 le32_to_cpu(sb->feature_map),
2300 (unsigned long long)le64_to_cpu(sb->ctime)
2301 & MD_SUPERBLOCK_1_TIME_SEC_MASK);
2303 uuid = sb->device_uuid;
2305 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
2307 "md: Dev:%08x UUID: %pU\n"
2308 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
2309 "md: (MaxDev:%u) \n",
2310 le32_to_cpu(sb->level),
2311 (unsigned long long)le64_to_cpu(sb->size),
2312 le32_to_cpu(sb->raid_disks),
2313 le32_to_cpu(sb->layout),
2314 le32_to_cpu(sb->chunksize),
2315 (unsigned long long)le64_to_cpu(sb->data_offset),
2316 (unsigned long long)le64_to_cpu(sb->data_size),
2317 (unsigned long long)le64_to_cpu(sb->super_offset),
2318 (unsigned long long)le64_to_cpu(sb->recovery_offset),
2319 le32_to_cpu(sb->dev_number),
2322 (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK,
2323 (unsigned long long)le64_to_cpu(sb->events),
2324 (unsigned long long)le64_to_cpu(sb->resync_offset),
2325 le32_to_cpu(sb->sb_csum),
2326 le32_to_cpu(sb->max_dev)
2330 static void print_rdev(struct md_rdev *rdev, int major_version)
2332 char b[BDEVNAME_SIZE];
2333 printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
2334 bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors,
2335 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
2337 if (rdev->sb_loaded) {
2338 printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version);
2339 switch (major_version) {
2341 print_sb_90(page_address(rdev->sb_page));
2344 print_sb_1(page_address(rdev->sb_page));
2348 printk(KERN_INFO "md: no rdev superblock!\n");
2351 static void md_print_devices(void)
2353 struct list_head *tmp;
2354 struct md_rdev *rdev;
2355 struct mddev *mddev;
2356 char b[BDEVNAME_SIZE];
2359 printk("md: **********************************\n");
2360 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
2361 printk("md: **********************************\n");
2362 for_each_mddev(mddev, tmp) {
2365 bitmap_print_sb(mddev->bitmap);
2367 printk("%s: ", mdname(mddev));
2368 rdev_for_each(rdev, mddev)
2369 printk("<%s>", bdevname(rdev->bdev,b));
2372 rdev_for_each(rdev, mddev)
2373 print_rdev(rdev, mddev->major_version);
2375 printk("md: **********************************\n");
2380 static void sync_sbs(struct mddev * mddev, int nospares)
2382 /* Update each superblock (in-memory image), but
2383 * if we are allowed to, skip spares which already
2384 * have the right event counter, or have one earlier
2385 * (which would mean they aren't being marked as dirty
2386 * with the rest of the array)
2388 struct md_rdev *rdev;
2389 rdev_for_each(rdev, mddev) {
2390 if (rdev->sb_events == mddev->events ||
2392 rdev->raid_disk < 0 &&
2393 rdev->sb_events+1 == mddev->events)) {
2394 /* Don't update this superblock */
2395 rdev->sb_loaded = 2;
2397 sync_super(mddev, rdev);
2398 rdev->sb_loaded = 1;
2403 static void md_update_sb(struct mddev * mddev, int force_change)
2405 struct md_rdev *rdev;
2408 int any_badblocks_changed = 0;
2411 /* First make sure individual recovery_offsets are correct */
2412 rdev_for_each(rdev, mddev) {
2413 if (rdev->raid_disk >= 0 &&
2414 mddev->delta_disks >= 0 &&
2415 !test_bit(In_sync, &rdev->flags) &&
2416 mddev->curr_resync_completed > rdev->recovery_offset)
2417 rdev->recovery_offset = mddev->curr_resync_completed;
2420 if (!mddev->persistent) {
2421 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
2422 clear_bit(MD_CHANGE_DEVS, &mddev->flags);
2423 if (!mddev->external) {
2424 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
2425 rdev_for_each(rdev, mddev) {
2426 if (rdev->badblocks.changed) {
2427 rdev->badblocks.changed = 0;
2428 md_ack_all_badblocks(&rdev->badblocks);
2429 md_error(mddev, rdev);
2431 clear_bit(Blocked, &rdev->flags);
2432 clear_bit(BlockedBadBlocks, &rdev->flags);
2433 wake_up(&rdev->blocked_wait);
2436 wake_up(&mddev->sb_wait);
2440 spin_lock_irq(&mddev->write_lock);
2442 mddev->utime = get_seconds();
2444 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2446 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2447 /* just a clean<-> dirty transition, possibly leave spares alone,
2448 * though if events isn't the right even/odd, we will have to do
2454 if (mddev->degraded)
2455 /* If the array is degraded, then skipping spares is both
2456 * dangerous and fairly pointless.
2457 * Dangerous because a device that was removed from the array
2458 * might have a event_count that still looks up-to-date,
2459 * so it can be re-added without a resync.
2460 * Pointless because if there are any spares to skip,
2461 * then a recovery will happen and soon that array won't
2462 * be degraded any more and the spare can go back to sleep then.
2466 sync_req = mddev->in_sync;
2468 /* If this is just a dirty<->clean transition, and the array is clean
2469 * and 'events' is odd, we can roll back to the previous clean state */
2471 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
2472 && mddev->can_decrease_events
2473 && mddev->events != 1) {
2475 mddev->can_decrease_events = 0;
2477 /* otherwise we have to go forward and ... */
2479 mddev->can_decrease_events = nospares;
2482 if (!mddev->events) {
2484 * oops, this 64-bit counter should never wrap.
2485 * Either we are in around ~1 trillion A.C., assuming
2486 * 1 reboot per second, or we have a bug:
2492 rdev_for_each(rdev, mddev) {
2493 if (rdev->badblocks.changed)
2494 any_badblocks_changed++;
2495 if (test_bit(Faulty, &rdev->flags))
2496 set_bit(FaultRecorded, &rdev->flags);
2499 sync_sbs(mddev, nospares);
2500 spin_unlock_irq(&mddev->write_lock);
2502 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2503 mdname(mddev), mddev->in_sync);
2505 bitmap_update_sb(mddev->bitmap);
2506 rdev_for_each(rdev, mddev) {
2507 char b[BDEVNAME_SIZE];
2509 if (rdev->sb_loaded != 1)
2510 continue; /* no noise on spare devices */
2512 if (!test_bit(Faulty, &rdev->flags) &&
2513 rdev->saved_raid_disk == -1) {
2514 md_super_write(mddev,rdev,
2515 rdev->sb_start, rdev->sb_size,
2517 pr_debug("md: (write) %s's sb offset: %llu\n",
2518 bdevname(rdev->bdev, b),
2519 (unsigned long long)rdev->sb_start);
2520 rdev->sb_events = mddev->events;
2521 if (rdev->badblocks.size) {
2522 md_super_write(mddev, rdev,
2523 rdev->badblocks.sector,
2524 rdev->badblocks.size << 9,
2526 rdev->badblocks.size = 0;
2529 } else if (test_bit(Faulty, &rdev->flags))
2530 pr_debug("md: %s (skipping faulty)\n",
2531 bdevname(rdev->bdev, b));
2533 pr_debug("(skipping incremental s/r ");
2535 if (mddev->level == LEVEL_MULTIPATH)
2536 /* only need to write one superblock... */
2539 md_super_wait(mddev);
2540 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
2542 spin_lock_irq(&mddev->write_lock);
2543 if (mddev->in_sync != sync_req ||
2544 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
2545 /* have to write it out again */
2546 spin_unlock_irq(&mddev->write_lock);
2549 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
2550 spin_unlock_irq(&mddev->write_lock);
2551 wake_up(&mddev->sb_wait);
2552 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2553 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
2555 rdev_for_each(rdev, mddev) {
2556 if (test_and_clear_bit(FaultRecorded, &rdev->flags))
2557 clear_bit(Blocked, &rdev->flags);
2559 if (any_badblocks_changed)
2560 md_ack_all_badblocks(&rdev->badblocks);
2561 clear_bit(BlockedBadBlocks, &rdev->flags);
2562 wake_up(&rdev->blocked_wait);
2566 /* words written to sysfs files may, or may not, be \n terminated.
2567 * We want to accept with case. For this we use cmd_match.
2569 static int cmd_match(const char *cmd, const char *str)
2571 /* See if cmd, written into a sysfs file, matches
2572 * str. They must either be the same, or cmd can
2573 * have a trailing newline
2575 while (*cmd && *str && *cmd == *str) {
2586 struct rdev_sysfs_entry {
2587 struct attribute attr;
2588 ssize_t (*show)(struct md_rdev *, char *);
2589 ssize_t (*store)(struct md_rdev *, const char *, size_t);
2593 state_show(struct md_rdev *rdev, char *page)
2598 if (test_bit(Faulty, &rdev->flags) ||
2599 rdev->badblocks.unacked_exist) {
2600 len+= sprintf(page+len, "%sfaulty",sep);
2603 if (test_bit(In_sync, &rdev->flags)) {
2604 len += sprintf(page+len, "%sin_sync",sep);
2607 if (test_bit(WriteMostly, &rdev->flags)) {
2608 len += sprintf(page+len, "%swrite_mostly",sep);
2611 if (test_bit(Blocked, &rdev->flags) ||
2612 (rdev->badblocks.unacked_exist
2613 && !test_bit(Faulty, &rdev->flags))) {
2614 len += sprintf(page+len, "%sblocked", sep);
2617 if (!test_bit(Faulty, &rdev->flags) &&
2618 !test_bit(In_sync, &rdev->flags)) {
2619 len += sprintf(page+len, "%sspare", sep);
2622 if (test_bit(WriteErrorSeen, &rdev->flags)) {
2623 len += sprintf(page+len, "%swrite_error", sep);
2626 if (test_bit(WantReplacement, &rdev->flags)) {
2627 len += sprintf(page+len, "%swant_replacement", sep);
2630 if (test_bit(Replacement, &rdev->flags)) {
2631 len += sprintf(page+len, "%sreplacement", sep);
2635 return len+sprintf(page+len, "\n");
2639 state_store(struct md_rdev *rdev, const char *buf, size_t len)
2642 * faulty - simulates an error
2643 * remove - disconnects the device
2644 * writemostly - sets write_mostly
2645 * -writemostly - clears write_mostly
2646 * blocked - sets the Blocked flags
2647 * -blocked - clears the Blocked and possibly simulates an error
2648 * insync - sets Insync providing device isn't active
2649 * write_error - sets WriteErrorSeen
2650 * -write_error - clears WriteErrorSeen
2653 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2654 md_error(rdev->mddev, rdev);
2655 if (test_bit(Faulty, &rdev->flags))
2659 } else if (cmd_match(buf, "remove")) {
2660 if (rdev->raid_disk >= 0)
2663 struct mddev *mddev = rdev->mddev;
2664 kick_rdev_from_array(rdev);
2666 md_update_sb(mddev, 1);
2667 md_new_event(mddev);
2670 } else if (cmd_match(buf, "writemostly")) {
2671 set_bit(WriteMostly, &rdev->flags);
2673 } else if (cmd_match(buf, "-writemostly")) {
2674 clear_bit(WriteMostly, &rdev->flags);
2676 } else if (cmd_match(buf, "blocked")) {
2677 set_bit(Blocked, &rdev->flags);
2679 } else if (cmd_match(buf, "-blocked")) {
2680 if (!test_bit(Faulty, &rdev->flags) &&
2681 rdev->badblocks.unacked_exist) {
2682 /* metadata handler doesn't understand badblocks,
2683 * so we need to fail the device
2685 md_error(rdev->mddev, rdev);
2687 clear_bit(Blocked, &rdev->flags);
2688 clear_bit(BlockedBadBlocks, &rdev->flags);
2689 wake_up(&rdev->blocked_wait);
2690 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2691 md_wakeup_thread(rdev->mddev->thread);
2694 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2695 set_bit(In_sync, &rdev->flags);
2697 } else if (cmd_match(buf, "write_error")) {
2698 set_bit(WriteErrorSeen, &rdev->flags);
2700 } else if (cmd_match(buf, "-write_error")) {
2701 clear_bit(WriteErrorSeen, &rdev->flags);
2703 } else if (cmd_match(buf, "want_replacement")) {
2704 /* Any non-spare device that is not a replacement can
2705 * become want_replacement at any time, but we then need to
2706 * check if recovery is needed.
2708 if (rdev->raid_disk >= 0 &&
2709 !test_bit(Replacement, &rdev->flags))
2710 set_bit(WantReplacement, &rdev->flags);
2711 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2712 md_wakeup_thread(rdev->mddev->thread);
2714 } else if (cmd_match(buf, "-want_replacement")) {
2715 /* Clearing 'want_replacement' is always allowed.
2716 * Once replacements starts it is too late though.
2719 clear_bit(WantReplacement, &rdev->flags);
2720 } else if (cmd_match(buf, "replacement")) {
2721 /* Can only set a device as a replacement when array has not
2722 * yet been started. Once running, replacement is automatic
2723 * from spares, or by assigning 'slot'.
2725 if (rdev->mddev->pers)
2728 set_bit(Replacement, &rdev->flags);
2731 } else if (cmd_match(buf, "-replacement")) {
2732 /* Similarly, can only clear Replacement before start */
2733 if (rdev->mddev->pers)
2736 clear_bit(Replacement, &rdev->flags);
2741 sysfs_notify_dirent_safe(rdev->sysfs_state);
2742 return err ? err : len;
2744 static struct rdev_sysfs_entry rdev_state =
2745 __ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
2748 errors_show(struct md_rdev *rdev, char *page)
2750 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2754 errors_store(struct md_rdev *rdev, const char *buf, size_t len)
2757 unsigned long n = simple_strtoul(buf, &e, 10);
2758 if (*buf && (*e == 0 || *e == '\n')) {
2759 atomic_set(&rdev->corrected_errors, n);
2764 static struct rdev_sysfs_entry rdev_errors =
2765 __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
2768 slot_show(struct md_rdev *rdev, char *page)
2770 if (rdev->raid_disk < 0)
2771 return sprintf(page, "none\n");
2773 return sprintf(page, "%d\n", rdev->raid_disk);
2777 slot_store(struct md_rdev *rdev, const char *buf, size_t len)
2781 int slot = simple_strtoul(buf, &e, 10);
2782 if (strncmp(buf, "none", 4)==0)
2784 else if (e==buf || (*e && *e!= '\n'))
2786 if (rdev->mddev->pers && slot == -1) {
2787 /* Setting 'slot' on an active array requires also
2788 * updating the 'rd%d' link, and communicating
2789 * with the personality with ->hot_*_disk.
2790 * For now we only support removing
2791 * failed/spare devices. This normally happens automatically,
2792 * but not when the metadata is externally managed.
2794 if (rdev->raid_disk == -1)
2796 /* personality does all needed checks */
2797 if (rdev->mddev->pers->hot_remove_disk == NULL)
2799 err = rdev->mddev->pers->
2800 hot_remove_disk(rdev->mddev, rdev);
2803 sysfs_unlink_rdev(rdev->mddev, rdev);
2804 rdev->raid_disk = -1;
2805 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2806 md_wakeup_thread(rdev->mddev->thread);
2807 } else if (rdev->mddev->pers) {
2808 /* Activating a spare .. or possibly reactivating
2809 * if we ever get bitmaps working here.
2812 if (rdev->raid_disk != -1)
2815 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
2818 if (rdev->mddev->pers->hot_add_disk == NULL)
2821 if (slot >= rdev->mddev->raid_disks &&
2822 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2825 rdev->raid_disk = slot;
2826 if (test_bit(In_sync, &rdev->flags))
2827 rdev->saved_raid_disk = slot;
2829 rdev->saved_raid_disk = -1;
2830 clear_bit(In_sync, &rdev->flags);
2831 err = rdev->mddev->pers->
2832 hot_add_disk(rdev->mddev, rdev);
2834 rdev->raid_disk = -1;
2837 sysfs_notify_dirent_safe(rdev->sysfs_state);
2838 if (sysfs_link_rdev(rdev->mddev, rdev))
2839 /* failure here is OK */;
2840 /* don't wakeup anyone, leave that to userspace. */
2842 if (slot >= rdev->mddev->raid_disks &&
2843 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2845 rdev->raid_disk = slot;
2846 /* assume it is working */
2847 clear_bit(Faulty, &rdev->flags);
2848 clear_bit(WriteMostly, &rdev->flags);
2849 set_bit(In_sync, &rdev->flags);
2850 sysfs_notify_dirent_safe(rdev->sysfs_state);
2856 static struct rdev_sysfs_entry rdev_slot =
2857 __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
2860 offset_show(struct md_rdev *rdev, char *page)
2862 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
2866 offset_store(struct md_rdev *rdev, const char *buf, size_t len)
2868 unsigned long long offset;
2869 if (strict_strtoull(buf, 10, &offset) < 0)
2871 if (rdev->mddev->pers && rdev->raid_disk >= 0)
2873 if (rdev->sectors && rdev->mddev->external)
2874 /* Must set offset before size, so overlap checks
2877 rdev->data_offset = offset;
2878 rdev->new_data_offset = offset;
2882 static struct rdev_sysfs_entry rdev_offset =
2883 __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
2885 static ssize_t new_offset_show(struct md_rdev *rdev, char *page)
2887 return sprintf(page, "%llu\n",
2888 (unsigned long long)rdev->new_data_offset);
2891 static ssize_t new_offset_store(struct md_rdev *rdev,
2892 const char *buf, size_t len)
2894 unsigned long long new_offset;
2895 struct mddev *mddev = rdev->mddev;
2897 if (strict_strtoull(buf, 10, &new_offset) < 0)
2900 if (mddev->sync_thread)
2902 if (new_offset == rdev->data_offset)
2903 /* reset is always permitted */
2905 else if (new_offset > rdev->data_offset) {
2906 /* must not push array size beyond rdev_sectors */
2907 if (new_offset - rdev->data_offset
2908 + mddev->dev_sectors > rdev->sectors)
2911 /* Metadata worries about other space details. */
2913 /* decreasing the offset is inconsistent with a backwards
2916 if (new_offset < rdev->data_offset &&
2917 mddev->reshape_backwards)
2919 /* Increasing offset is inconsistent with forwards
2920 * reshape. reshape_direction should be set to
2921 * 'backwards' first.
2923 if (new_offset > rdev->data_offset &&
2924 !mddev->reshape_backwards)
2927 if (mddev->pers && mddev->persistent &&
2928 !super_types[mddev->major_version]
2929 .allow_new_offset(rdev, new_offset))
2931 rdev->new_data_offset = new_offset;
2932 if (new_offset > rdev->data_offset)
2933 mddev->reshape_backwards = 1;
2934 else if (new_offset < rdev->data_offset)
2935 mddev->reshape_backwards = 0;
2939 static struct rdev_sysfs_entry rdev_new_offset =
2940 __ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store);
2943 rdev_size_show(struct md_rdev *rdev, char *page)
2945 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
2948 static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2950 /* check if two start/length pairs overlap */
2958 static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2960 unsigned long long blocks;
2963 if (strict_strtoull(buf, 10, &blocks) < 0)
2966 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2967 return -EINVAL; /* sector conversion overflow */
2970 if (new != blocks * 2)
2971 return -EINVAL; /* unsigned long long to sector_t overflow */
2978 rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len)
2980 struct mddev *my_mddev = rdev->mddev;
2981 sector_t oldsectors = rdev->sectors;
2984 if (strict_blocks_to_sectors(buf, §ors) < 0)
2986 if (rdev->data_offset != rdev->new_data_offset)
2987 return -EINVAL; /* too confusing */
2988 if (my_mddev->pers && rdev->raid_disk >= 0) {
2989 if (my_mddev->persistent) {
2990 sectors = super_types[my_mddev->major_version].
2991 rdev_size_change(rdev, sectors);
2994 } else if (!sectors)
2995 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
2998 if (sectors < my_mddev->dev_sectors)
2999 return -EINVAL; /* component must fit device */
3001 rdev->sectors = sectors;
3002 if (sectors > oldsectors && my_mddev->external) {
3003 /* need to check that all other rdevs with the same ->bdev
3004 * do not overlap. We need to unlock the mddev to avoid
3005 * a deadlock. We have already changed rdev->sectors, and if
3006 * we have to change it back, we will have the lock again.
3008 struct mddev *mddev;
3010 struct list_head *tmp;
3012 mddev_unlock(my_mddev);
3013 for_each_mddev(mddev, tmp) {
3014 struct md_rdev *rdev2;
3017 rdev_for_each(rdev2, mddev)
3018 if (rdev->bdev == rdev2->bdev &&
3020 overlaps(rdev->data_offset, rdev->sectors,
3026 mddev_unlock(mddev);
3032 mddev_lock(my_mddev);
3034 /* Someone else could have slipped in a size
3035 * change here, but doing so is just silly.
3036 * We put oldsectors back because we *know* it is
3037 * safe, and trust userspace not to race with
3040 rdev->sectors = oldsectors;
3047 static struct rdev_sysfs_entry rdev_size =
3048 __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
3051 static ssize_t recovery_start_show(struct md_rdev *rdev, char *page)
3053 unsigned long long recovery_start = rdev->recovery_offset;
3055 if (test_bit(In_sync, &rdev->flags) ||
3056 recovery_start == MaxSector)
3057 return sprintf(page, "none\n");
3059 return sprintf(page, "%llu\n", recovery_start);
3062 static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len)
3064 unsigned long long recovery_start;
3066 if (cmd_match(buf, "none"))
3067 recovery_start = MaxSector;
3068 else if (strict_strtoull(buf, 10, &recovery_start))
3071 if (rdev->mddev->pers &&
3072 rdev->raid_disk >= 0)
3075 rdev->recovery_offset = recovery_start;
3076 if (recovery_start == MaxSector)
3077 set_bit(In_sync, &rdev->flags);
3079 clear_bit(In_sync, &rdev->flags);
3083 static struct rdev_sysfs_entry rdev_recovery_start =
3084 __ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
3088 badblocks_show(struct badblocks *bb, char *page, int unack);
3090 badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack);
3092 static ssize_t bb_show(struct md_rdev *rdev, char *page)
3094 return badblocks_show(&rdev->badblocks, page, 0);
3096 static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len)
3098 int rv = badblocks_store(&rdev->badblocks, page, len, 0);
3099 /* Maybe that ack was all we needed */
3100 if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags))
3101 wake_up(&rdev->blocked_wait);
3104 static struct rdev_sysfs_entry rdev_bad_blocks =
3105 __ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
3108 static ssize_t ubb_show(struct md_rdev *rdev, char *page)
3110 return badblocks_show(&rdev->badblocks, page, 1);
3112 static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len)
3114 return badblocks_store(&rdev->badblocks, page, len, 1);
3116 static struct rdev_sysfs_entry rdev_unack_bad_blocks =
3117 __ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
3119 static struct attribute *rdev_default_attrs[] = {
3124 &rdev_new_offset.attr,
3126 &rdev_recovery_start.attr,
3127 &rdev_bad_blocks.attr,
3128 &rdev_unack_bad_blocks.attr,
3132 rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3134 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
3135 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
3136 struct mddev *mddev = rdev->mddev;
3142 rv = mddev ? mddev_lock(mddev) : -EBUSY;
3144 if (rdev->mddev == NULL)
3147 rv = entry->show(rdev, page);
3148 mddev_unlock(mddev);
3154 rdev_attr_store(struct kobject *kobj, struct attribute *attr,
3155 const char *page, size_t length)
3157 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
3158 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
3160 struct mddev *mddev = rdev->mddev;
3164 if (!capable(CAP_SYS_ADMIN))
3166 rv = mddev ? mddev_lock(mddev): -EBUSY;
3168 if (rdev->mddev == NULL)
3171 rv = entry->store(rdev, page, length);
3172 mddev_unlock(mddev);
3177 static void rdev_free(struct kobject *ko)
3179 struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj);
3182 static const struct sysfs_ops rdev_sysfs_ops = {
3183 .show = rdev_attr_show,
3184 .store = rdev_attr_store,
3186 static struct kobj_type rdev_ktype = {
3187 .release = rdev_free,
3188 .sysfs_ops = &rdev_sysfs_ops,
3189 .default_attrs = rdev_default_attrs,
3192 int md_rdev_init(struct md_rdev *rdev)
3195 rdev->saved_raid_disk = -1;
3196 rdev->raid_disk = -1;
3198 rdev->data_offset = 0;
3199 rdev->new_data_offset = 0;
3200 rdev->sb_events = 0;
3201 rdev->last_read_error.tv_sec = 0;
3202 rdev->last_read_error.tv_nsec = 0;
3203 rdev->sb_loaded = 0;
3204 rdev->bb_page = NULL;
3205 atomic_set(&rdev->nr_pending, 0);
3206 atomic_set(&rdev->read_errors, 0);
3207 atomic_set(&rdev->corrected_errors, 0);
3209 INIT_LIST_HEAD(&rdev->same_set);
3210 init_waitqueue_head(&rdev->blocked_wait);
3212 /* Add space to store bad block list.
3213 * This reserves the space even on arrays where it cannot
3214 * be used - I wonder if that matters
3216 rdev->badblocks.count = 0;
3217 rdev->badblocks.shift = 0;
3218 rdev->badblocks.page = kmalloc(PAGE_SIZE, GFP_KERNEL);
3219 seqlock_init(&rdev->badblocks.lock);
3220 if (rdev->badblocks.page == NULL)
3225 EXPORT_SYMBOL_GPL(md_rdev_init);
3227 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3229 * mark the device faulty if:
3231 * - the device is nonexistent (zero size)
3232 * - the device has no valid superblock
3234 * a faulty rdev _never_ has rdev->sb set.
3236 static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor)
3238 char b[BDEVNAME_SIZE];
3240 struct md_rdev *rdev;
3243 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
3245 printk(KERN_ERR "md: could not alloc mem for new device!\n");
3246 return ERR_PTR(-ENOMEM);
3249 err = md_rdev_init(rdev);
3252 err = alloc_disk_sb(rdev);
3256 err = lock_rdev(rdev, newdev, super_format == -2);
3260 kobject_init(&rdev->kobj, &rdev_ktype);
3262 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
3265 "md: %s has zero or unknown size, marking faulty!\n",
3266 bdevname(rdev->bdev,b));
3271 if (super_format >= 0) {
3272 err = super_types[super_format].
3273 load_super(rdev, NULL, super_minor);
3274 if (err == -EINVAL) {
3276 "md: %s does not have a valid v%d.%d "
3277 "superblock, not importing!\n",
3278 bdevname(rdev->bdev,b),
3279 super_format, super_minor);
3284 "md: could not read %s's sb, not importing!\n",
3285 bdevname(rdev->bdev,b));
3289 if (super_format == -1)
3290 /* hot-add for 0.90, or non-persistent: so no badblocks */
3291 rdev->badblocks.shift = -1;
3298 md_rdev_clear(rdev);
3300 return ERR_PTR(err);
3304 * Check a full RAID array for plausibility
3308 static void analyze_sbs(struct mddev * mddev)
3311 struct md_rdev *rdev, *freshest, *tmp;
3312 char b[BDEVNAME_SIZE];
3315 rdev_for_each_safe(rdev, tmp, mddev)
3316 switch (super_types[mddev->major_version].
3317 load_super(rdev, freshest, mddev->minor_version)) {
3325 "md: fatal superblock inconsistency in %s"
3326 " -- removing from array\n",
3327 bdevname(rdev->bdev,b));
3328 kick_rdev_from_array(rdev);
3332 super_types[mddev->major_version].
3333 validate_super(mddev, freshest);
3336 rdev_for_each_safe(rdev, tmp, mddev) {
3337 if (mddev->max_disks &&
3338 (rdev->desc_nr >= mddev->max_disks ||
3339 i > mddev->max_disks)) {
3341 "md: %s: %s: only %d devices permitted\n",
3342 mdname(mddev), bdevname(rdev->bdev, b),
3344 kick_rdev_from_array(rdev);
3347 if (rdev != freshest)
3348 if (super_types[mddev->major_version].
3349 validate_super(mddev, rdev)) {
3350 printk(KERN_WARNING "md: kicking non-fresh %s"
3352 bdevname(rdev->bdev,b));
3353 kick_rdev_from_array(rdev);
3356 if (mddev->level == LEVEL_MULTIPATH) {
3357 rdev->desc_nr = i++;
3358 rdev->raid_disk = rdev->desc_nr;
3359 set_bit(In_sync, &rdev->flags);
3360 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
3361 rdev->raid_disk = -1;
3362 clear_bit(In_sync, &rdev->flags);
3367 /* Read a fixed-point number.
3368 * Numbers in sysfs attributes should be in "standard" units where
3369 * possible, so time should be in seconds.
3370 * However we internally use a a much smaller unit such as
3371 * milliseconds or jiffies.
3372 * This function takes a decimal number with a possible fractional
3373 * component, and produces an integer which is the result of
3374 * multiplying that number by 10^'scale'.
3375 * all without any floating-point arithmetic.
3377 int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3379 unsigned long result = 0;
3381 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3384 else if (decimals < scale) {
3387 result = result * 10 + value;
3399 while (decimals < scale) {
3408 static void md_safemode_timeout(unsigned long data);
3411 safe_delay_show(struct mddev *mddev, char *page)
3413 int msec = (mddev->safemode_delay*1000)/HZ;
3414 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3417 safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len)
3421 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
3424 mddev->safemode_delay = 0;
3426 unsigned long old_delay = mddev->safemode_delay;
3427 mddev->safemode_delay = (msec*HZ)/1000;
3428 if (mddev->safemode_delay == 0)
3429 mddev->safemode_delay = 1;
3430 if (mddev->safemode_delay < old_delay)
3431 md_safemode_timeout((unsigned long)mddev);
3435 static struct md_sysfs_entry md_safe_delay =
3436 __ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
3439 level_show(struct mddev *mddev, char *page)
3441 struct md_personality *p = mddev->pers;
3443 return sprintf(page, "%s\n", p->name);
3444 else if (mddev->clevel[0])
3445 return sprintf(page, "%s\n", mddev->clevel);
3446 else if (mddev->level != LEVEL_NONE)
3447 return sprintf(page, "%d\n", mddev->level);
3453 level_store(struct mddev *mddev, const char *buf, size_t len)
3457 struct md_personality *pers;
3460 struct md_rdev *rdev;
3462 if (mddev->pers == NULL) {
3465 if (len >= sizeof(mddev->clevel))
3467 strncpy(mddev->clevel, buf, len);
3468 if (mddev->clevel[len-1] == '\n')
3470 mddev->clevel[len] = 0;
3471 mddev->level = LEVEL_NONE;
3475 /* request to change the personality. Need to ensure:
3476 * - array is not engaged in resync/recovery/reshape
3477 * - old personality can be suspended
3478 * - new personality will access other array.
3481 if (mddev->sync_thread ||
3482 mddev->reshape_position != MaxSector ||
3483 mddev->sysfs_active)
3486 if (!mddev->pers->quiesce) {
3487 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
3488 mdname(mddev), mddev->pers->name);
3492 /* Now find the new personality */
3493 if (len == 0 || len >= sizeof(clevel))
3495 strncpy(clevel, buf, len);
3496 if (clevel[len-1] == '\n')
3499 if (strict_strtol(clevel, 10, &level))
3502 if (request_module("md-%s", clevel) != 0)
3503 request_module("md-level-%s", clevel);
3504 spin_lock(&pers_lock);
3505 pers = find_pers(level, clevel);
3506 if (!pers || !try_module_get(pers->owner)) {
3507 spin_unlock(&pers_lock);
3508 printk(KERN_WARNING "md: personality %s not loaded\n", clevel);
3511 spin_unlock(&pers_lock);
3513 if (pers == mddev->pers) {
3514 /* Nothing to do! */
3515 module_put(pers->owner);
3518 if (!pers->takeover) {
3519 module_put(pers->owner);
3520 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
3521 mdname(mddev), clevel);
3525 rdev_for_each(rdev, mddev)
3526 rdev->new_raid_disk = rdev->raid_disk;
3528 /* ->takeover must set new_* and/or delta_disks
3529 * if it succeeds, and may set them when it fails.
3531 priv = pers->takeover(mddev);
3533 mddev->new_level = mddev->level;
3534 mddev->new_layout = mddev->layout;
3535 mddev->new_chunk_sectors = mddev->chunk_sectors;
3536 mddev->raid_disks -= mddev->delta_disks;
3537 mddev->delta_disks = 0;
3538 mddev->reshape_backwards = 0;
3539 module_put(pers->owner);
3540 printk(KERN_WARNING "md: %s: %s would not accept array\n",
3541 mdname(mddev), clevel);
3542 return PTR_ERR(priv);
3545 /* Looks like we have a winner */
3546 mddev_suspend(mddev);
3547 mddev->pers->stop(mddev);
3549 if (mddev->pers->sync_request == NULL &&
3550 pers->sync_request != NULL) {
3551 /* need to add the md_redundancy_group */
3552 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3554 "md: cannot register extra attributes for %s\n",
3556 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, NULL, "sync_action");
3558 if (mddev->pers->sync_request != NULL &&
3559 pers->sync_request == NULL) {
3560 /* need to remove the md_redundancy_group */
3561 if (mddev->to_remove == NULL)
3562 mddev->to_remove = &md_redundancy_group;
3565 if (mddev->pers->sync_request == NULL &&
3567 /* We are converting from a no-redundancy array
3568 * to a redundancy array and metadata is managed
3569 * externally so we need to be sure that writes
3570 * won't block due to a need to transition
3572 * until external management is started.
3575 mddev->safemode_delay = 0;
3576 mddev->safemode = 0;
3579 rdev_for_each(rdev, mddev) {
3580 if (rdev->raid_disk < 0)
3582 if (rdev->new_raid_disk >= mddev->raid_disks)
3583 rdev->new_raid_disk = -1;
3584 if (rdev->new_raid_disk == rdev->raid_disk)
3586 sysfs_unlink_rdev(mddev, rdev);
3588 rdev_for_each(rdev, mddev) {
3589 if (rdev->raid_disk < 0)
3591 if (rdev->new_raid_disk == rdev->raid_disk)
3593 rdev->raid_disk = rdev->new_raid_disk;
3594 if (rdev->raid_disk < 0)
3595 clear_bit(In_sync, &rdev->flags);
3597 if (sysfs_link_rdev(mddev, rdev))
3598 printk(KERN_WARNING "md: cannot register rd%d"
3599 " for %s after level change\n",
3600 rdev->raid_disk, mdname(mddev));
3604 module_put(mddev->pers->owner);
3606 mddev->private = priv;
3607 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3608 mddev->level = mddev->new_level;
3609 mddev->layout = mddev->new_layout;
3610 mddev->chunk_sectors = mddev->new_chunk_sectors;
3611 mddev->delta_disks = 0;
3612 mddev->reshape_backwards = 0;
3613 mddev->degraded = 0;
3614 if (mddev->pers->sync_request == NULL) {
3615 /* this is now an array without redundancy, so
3616 * it must always be in_sync
3619 del_timer_sync(&mddev->safemode_timer);
3622 set_bit(MD_CHANGE_DEVS, &mddev->flags);
3623 mddev_resume(mddev);
3624 sysfs_notify(&mddev->kobj, NULL, "level");
3625 md_new_event(mddev);
3629 static struct md_sysfs_entry md_level =
3630 __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
3634 layout_show(struct mddev *mddev, char *page)
3636 /* just a number, not meaningful for all levels */
3637 if (mddev->reshape_position != MaxSector &&
3638 mddev->layout != mddev->new_layout)
3639 return sprintf(page, "%d (%d)\n",
3640 mddev->new_layout, mddev->layout);
3641 return sprintf(page, "%d\n", mddev->layout);
3645 layout_store(struct mddev *mddev, const char *buf, size_t len)
3648 unsigned long n = simple_strtoul(buf, &e, 10);
3650 if (!*buf || (*e && *e != '\n'))
3655 if (mddev->pers->check_reshape == NULL)
3657 mddev->new_layout = n;
3658 err = mddev->pers->check_reshape(mddev);
3660 mddev->new_layout = mddev->layout;
3664 mddev->new_layout = n;
3665 if (mddev->reshape_position == MaxSector)
3670 static struct md_sysfs_entry md_layout =
3671 __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
3675 raid_disks_show(struct mddev *mddev, char *page)
3677 if (mddev->raid_disks == 0)
3679 if (mddev->reshape_position != MaxSector &&
3680 mddev->delta_disks != 0)
3681 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3682 mddev->raid_disks - mddev->delta_disks);
3683 return sprintf(page, "%d\n", mddev->raid_disks);
3686 static int update_raid_disks(struct mddev *mddev, int raid_disks);
3689 raid_disks_store(struct mddev *mddev, const char *buf, size_t len)
3693 unsigned long n = simple_strtoul(buf, &e, 10);
3695 if (!*buf || (*e && *e != '\n'))
3699 rv = update_raid_disks(mddev, n);
3700 else if (mddev->reshape_position != MaxSector) {
3701 struct md_rdev *rdev;
3702 int olddisks = mddev->raid_disks - mddev->delta_disks;
3704 rdev_for_each(rdev, mddev) {
3706 rdev->data_offset < rdev->new_data_offset)
3709 rdev->data_offset > rdev->new_data_offset)
3712 mddev->delta_disks = n - olddisks;
3713 mddev->raid_disks = n;
3714 mddev->reshape_backwards = (mddev->delta_disks < 0);
3716 mddev->raid_disks = n;
3717 return rv ? rv : len;
3719 static struct md_sysfs_entry md_raid_disks =
3720 __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
3723 chunk_size_show(struct mddev *mddev, char *page)
3725 if (mddev->reshape_position != MaxSector &&
3726 mddev->chunk_sectors != mddev->new_chunk_sectors)
3727 return sprintf(page, "%d (%d)\n",
3728 mddev->new_chunk_sectors << 9,
3729 mddev->chunk_sectors << 9);
3730 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
3734 chunk_size_store(struct mddev *mddev, const char *buf, size_t len)
3737 unsigned long n = simple_strtoul(buf, &e, 10);
3739 if (!*buf || (*e && *e != '\n'))
3744 if (mddev->pers->check_reshape == NULL)
3746 mddev->new_chunk_sectors = n >> 9;
3747 err = mddev->pers->check_reshape(mddev);
3749 mddev->new_chunk_sectors = mddev->chunk_sectors;
3753 mddev->new_chunk_sectors = n >> 9;
3754 if (mddev->reshape_position == MaxSector)
3755 mddev->chunk_sectors = n >> 9;
3759 static struct md_sysfs_entry md_chunk_size =
3760 __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
3763 resync_start_show(struct mddev *mddev, char *page)
3765 if (mddev->recovery_cp == MaxSector)
3766 return sprintf(page, "none\n");
3767 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3771 resync_start_store(struct mddev *mddev, const char *buf, size_t len)
3774 unsigned long long n = simple_strtoull(buf, &e, 10);
3776 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
3778 if (cmd_match(buf, "none"))
3780 else if (!*buf || (*e && *e != '\n'))
3783 mddev->recovery_cp = n;
3785 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
3788 static struct md_sysfs_entry md_resync_start =
3789 __ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
3792 * The array state can be:
3795 * No devices, no size, no level
3796 * Equivalent to STOP_ARRAY ioctl
3798 * May have some settings, but array is not active
3799 * all IO results in error
3800 * When written, doesn't tear down array, but just stops it
3801 * suspended (not supported yet)
3802 * All IO requests will block. The array can be reconfigured.
3803 * Writing this, if accepted, will block until array is quiescent
3805 * no resync can happen. no superblocks get written.
3806 * write requests fail
3808 * like readonly, but behaves like 'clean' on a write request.
3810 * clean - no pending writes, but otherwise active.
3811 * When written to inactive array, starts without resync
3812 * If a write request arrives then
3813 * if metadata is known, mark 'dirty' and switch to 'active'.
3814 * if not known, block and switch to write-pending
3815 * If written to an active array that has pending writes, then fails.
3817 * fully active: IO and resync can be happening.
3818 * When written to inactive array, starts with resync
3821 * clean, but writes are blocked waiting for 'active' to be written.
3824 * like active, but no writes have been seen for a while (100msec).
3827 enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3828 write_pending, active_idle, bad_word};
3829 static char *array_states[] = {
3830 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3831 "write-pending", "active-idle", NULL };
3833 static int match_word(const char *word, char **list)
3836 for (n=0; list[n]; n++)
3837 if (cmd_match(word, list[n]))
3843 array_state_show(struct mddev *mddev, char *page)
3845 enum array_state st = inactive;
3858 else if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
3860 else if (mddev->safemode)
3866 if (list_empty(&mddev->disks) &&
3867 mddev->raid_disks == 0 &&
3868 mddev->dev_sectors == 0)
3873 return sprintf(page, "%s\n", array_states[st]);
3876 static int do_md_stop(struct mddev * mddev, int ro, struct block_device *bdev);
3877 static int md_set_readonly(struct mddev * mddev, struct block_device *bdev);
3878 static int do_md_run(struct mddev * mddev);
3879 static int restart_array(struct mddev *mddev);
3882 array_state_store(struct mddev *mddev, const char *buf, size_t len)
3885 enum array_state st = match_word(buf, array_states);
3890 /* stopping an active array */
3891 err = do_md_stop(mddev, 0, NULL);
3894 /* stopping an active array */
3896 err = do_md_stop(mddev, 2, NULL);
3898 err = 0; /* already inactive */
3901 break; /* not supported yet */
3904 err = md_set_readonly(mddev, NULL);
3907 set_disk_ro(mddev->gendisk, 1);
3908 err = do_md_run(mddev);
3914 err = md_set_readonly(mddev, NULL);
3915 else if (mddev->ro == 1)
3916 err = restart_array(mddev);
3919 set_disk_ro(mddev->gendisk, 0);
3923 err = do_md_run(mddev);
3928 restart_array(mddev);
3929 spin_lock_irq(&mddev->write_lock);
3930 if (atomic_read(&mddev->writes_pending) == 0) {
3931 if (mddev->in_sync == 0) {
3933 if (mddev->safemode == 1)
3934 mddev->safemode = 0;
3935 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
3940 spin_unlock_irq(&mddev->write_lock);
3946 restart_array(mddev);
3947 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
3948 wake_up(&mddev->sb_wait);
3952 set_disk_ro(mddev->gendisk, 0);
3953 err = do_md_run(mddev);
3958 /* these cannot be set */
3964 if (mddev->hold_active == UNTIL_IOCTL)
3965 mddev->hold_active = 0;
3966 sysfs_notify_dirent_safe(mddev->sysfs_state);
3970 static struct md_sysfs_entry md_array_state =
3971 __ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
3974 max_corrected_read_errors_show(struct mddev *mddev, char *page) {
3975 return sprintf(page, "%d\n",
3976 atomic_read(&mddev->max_corr_read_errors));
3980 max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len)
3983 unsigned long n = simple_strtoul(buf, &e, 10);
3985 if (*buf && (*e == 0 || *e == '\n')) {
3986 atomic_set(&mddev->max_corr_read_errors, n);
3992 static struct md_sysfs_entry max_corr_read_errors =
3993 __ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
3994 max_corrected_read_errors_store);
3997 null_show(struct mddev *mddev, char *page)
4003 new_dev_store(struct mddev *mddev, const char *buf, size_t len)
4005 /* buf must be %d:%d\n? giving major and minor numbers */
4006 /* The new device is added to the array.
4007 * If the array has a persistent superblock, we read the
4008 * superblock to initialise info and check validity.
4009 * Otherwise, only checking done is that in bind_rdev_to_array,
4010 * which mainly checks size.
4013 int major = simple_strtoul(buf, &e, 10);
4016 struct md_rdev *rdev;
4019 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
4021 minor = simple_strtoul(e+1, &e, 10);
4022 if (*e && *e != '\n')
4024 dev = MKDEV(major, minor);
4025 if (major != MAJOR(dev) ||
4026 minor != MINOR(dev))
4030 if (mddev->persistent) {
4031 rdev = md_import_device(dev, mddev->major_version,
4032 mddev->minor_version);
4033 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
4034 struct md_rdev *rdev0
4035 = list_entry(mddev->disks.next,
4036 struct md_rdev, same_set);
4037 err = super_types[mddev->major_version]
4038 .load_super(rdev, rdev0, mddev->minor_version);
4042 } else if (mddev->external)
4043 rdev = md_import_device(dev, -2, -1);
4045 rdev = md_import_device(dev, -1, -1);
4048 return PTR_ERR(rdev);
4049 err = bind_rdev_to_array(rdev, mddev);
4053 return err ? err : len;
4056 static struct md_sysfs_entry md_new_device =
4057 __ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
4060 bitmap_store(struct mddev *mddev, const char *buf, size_t len)
4063 unsigned long chunk, end_chunk;
4067 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
4069 chunk = end_chunk = simple_strtoul(buf, &end, 0);
4070 if (buf == end) break;
4071 if (*end == '-') { /* range */
4073 end_chunk = simple_strtoul(buf, &end, 0);
4074 if (buf == end) break;
4076 if (*end && !isspace(*end)) break;
4077 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
4078 buf = skip_spaces(end);
4080 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
4085 static struct md_sysfs_entry md_bitmap =
4086 __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
4089 size_show(struct mddev *mddev, char *page)
4091 return sprintf(page, "%llu\n",
4092 (unsigned long long)mddev->dev_sectors / 2);
4095 static int update_size(struct mddev *mddev, sector_t num_sectors);
4098 size_store(struct mddev *mddev, const char *buf, size_t len)
4100 /* If array is inactive, we can reduce the component size, but
4101 * not increase it (except from 0).
4102 * If array is active, we can try an on-line resize
4105 int err = strict_blocks_to_sectors(buf, §ors);
4110 err = update_size(mddev, sectors);
4111 md_update_sb(mddev, 1);
4113 if (mddev->dev_sectors == 0 ||
4114 mddev->dev_sectors > sectors)
4115 mddev->dev_sectors = sectors;
4119 return err ? err : len;
4122 static struct md_sysfs_entry md_size =
4123 __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
4126 /* Metadata version.
4128 * 'none' for arrays with no metadata (good luck...)
4129 * 'external' for arrays with externally managed metadata,
4130 * or N.M for internally known formats
4133 metadata_show(struct mddev *mddev, char *page)
4135 if (mddev->persistent)
4136 return sprintf(page, "%d.%d\n",
4137 mddev->major_version, mddev->minor_version);
4138 else if (mddev->external)
4139 return sprintf(page, "external:%s\n", mddev->metadata_type);
4141 return sprintf(page, "none\n");
4145 metadata_store(struct mddev *mddev, const char *buf, size_t len)
4149 /* Changing the details of 'external' metadata is
4150 * always permitted. Otherwise there must be
4151 * no devices attached to the array.
4153 if (mddev->external && strncmp(buf, "external:", 9) == 0)
4155 else if (!list_empty(&mddev->disks))
4158 if (cmd_match(buf, "none")) {
4159 mddev->persistent = 0;
4160 mddev->external = 0;
4161 mddev->major_version = 0;
4162 mddev->minor_version = 90;
4165 if (strncmp(buf, "external:", 9) == 0) {
4166 size_t namelen = len-9;
4167 if (namelen >= sizeof(mddev->metadata_type))
4168 namelen = sizeof(mddev->metadata_type)-1;
4169 strncpy(mddev->metadata_type, buf+9, namelen);
4170 mddev->metadata_type[namelen] = 0;
4171 if (namelen && mddev->metadata_type[namelen-1] == '\n')
4172 mddev->metadata_type[--namelen] = 0;
4173 mddev->persistent = 0;
4174 mddev->external = 1;
4175 mddev->major_version = 0;
4176 mddev->minor_version = 90;
4179 major = simple_strtoul(buf, &e, 10);
4180 if (e==buf || *e != '.')
4183 minor = simple_strtoul(buf, &e, 10);
4184 if (e==buf || (*e && *e != '\n') )
4186 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
4188 mddev->major_version = major;
4189 mddev->minor_version = minor;
4190 mddev->persistent = 1;
4191 mddev->external = 0;
4195 static struct md_sysfs_entry md_metadata =
4196 __ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
4199 action_show(struct mddev *mddev, char *page)
4201 char *type = "idle";
4202 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
4204 else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
4205 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
4206 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4208 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
4209 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
4211 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
4215 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
4218 return sprintf(page, "%s\n", type);
4221 static void reap_sync_thread(struct mddev *mddev);
4224 action_store(struct mddev *mddev, const char *page, size_t len)
4226 if (!mddev->pers || !mddev->pers->sync_request)
4229 if (cmd_match(page, "frozen"))
4230 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4232 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4234 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
4235 if (mddev->sync_thread) {
4236 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4237 reap_sync_thread(mddev);
4239 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
4240 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
4242 else if (cmd_match(page, "resync"))
4243 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4244 else if (cmd_match(page, "recover")) {
4245 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
4246 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4247 } else if (cmd_match(page, "reshape")) {
4249 if (mddev->pers->start_reshape == NULL)
4251 err = mddev->pers->start_reshape(mddev);
4254 sysfs_notify(&mddev->kobj, NULL, "degraded");
4256 if (cmd_match(page, "check"))
4257 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
4258 else if (!cmd_match(page, "repair"))
4260 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
4261 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4263 if (mddev->ro == 2) {
4264 /* A write to sync_action is enough to justify
4265 * canceling read-auto mode
4268 md_wakeup_thread(mddev->sync_thread);
4270 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4271 md_wakeup_thread(mddev->thread);
4272 sysfs_notify_dirent_safe(mddev->sysfs_action);
4277 mismatch_cnt_show(struct mddev *mddev, char *page)
4279 return sprintf(page, "%llu\n",
4280 (unsigned long long)
4281 atomic64_read(&mddev->resync_mismatches));
4284 static struct md_sysfs_entry md_scan_mode =
4285 __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
4288 static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
4291 sync_min_show(struct mddev *mddev, char *page)
4293 return sprintf(page, "%d (%s)\n", speed_min(mddev),
4294 mddev->sync_speed_min ? "local": "system");
4298 sync_min_store(struct mddev *mddev, const char *buf, size_t len)
4302 if (strncmp(buf, "system", 6)==0) {
4303 mddev->sync_speed_min = 0;
4306 min = simple_strtoul(buf, &e, 10);
4307 if (buf == e || (*e && *e != '\n') || min <= 0)
4309 mddev->sync_speed_min = min;
4313 static struct md_sysfs_entry md_sync_min =
4314 __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
4317 sync_max_show(struct mddev *mddev, char *page)
4319 return sprintf(page, "%d (%s)\n", speed_max(mddev),
4320 mddev->sync_speed_max ? "local": "system");
4324 sync_max_store(struct mddev *mddev, const char *buf, size_t len)
4328 if (strncmp(buf, "system", 6)==0) {
4329 mddev->sync_speed_max = 0;
4332 max = simple_strtoul(buf, &e, 10);
4333 if (buf == e || (*e && *e != '\n') || max <= 0)
4335 mddev->sync_speed_max = max;
4339 static struct md_sysfs_entry md_sync_max =
4340 __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
4343 degraded_show(struct mddev *mddev, char *page)
4345 return sprintf(page, "%d\n", mddev->degraded);
4347 static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
4350 sync_force_parallel_show(struct mddev *mddev, char *page)
4352 return sprintf(page, "%d\n", mddev->parallel_resync);
4356 sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len)
4360 if (strict_strtol(buf, 10, &n))
4363 if (n != 0 && n != 1)
4366 mddev->parallel_resync = n;
4368 if (mddev->sync_thread)
4369 wake_up(&resync_wait);
4374 /* force parallel resync, even with shared block devices */
4375 static struct md_sysfs_entry md_sync_force_parallel =
4376 __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
4377 sync_force_parallel_show, sync_force_parallel_store);
4380 sync_speed_show(struct mddev *mddev, char *page)
4382 unsigned long resync, dt, db;
4383 if (mddev->curr_resync == 0)
4384 return sprintf(page, "none\n");
4385 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
4386 dt = (jiffies - mddev->resync_mark) / HZ;
4388 db = resync - mddev->resync_mark_cnt;
4389 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
4392 static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
4395 sync_completed_show(struct mddev *mddev, char *page)
4397 unsigned long long max_sectors, resync;
4399 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4400 return sprintf(page, "none\n");
4402 if (mddev->curr_resync == 1 ||
4403 mddev->curr_resync == 2)
4404 return sprintf(page, "delayed\n");
4406 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
4407 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4408 max_sectors = mddev->resync_max_sectors;
4410 max_sectors = mddev->dev_sectors;
4412 resync = mddev->curr_resync_completed;
4413 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
4416 static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
4419 min_sync_show(struct mddev *mddev, char *page)
4421 return sprintf(page, "%llu\n",
4422 (unsigned long long)mddev->resync_min);
4425 min_sync_store(struct mddev *mddev, const char *buf, size_t len)
4427 unsigned long long min;
4428 if (strict_strtoull(buf, 10, &min))
4430 if (min > mddev->resync_max)
4432 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4435 /* Must be a multiple of chunk_size */
4436 if (mddev->chunk_sectors) {
4437 sector_t temp = min;
4438 if (sector_div(temp, mddev->chunk_sectors))
4441 mddev->resync_min = min;
4446 static struct md_sysfs_entry md_min_sync =
4447 __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
4450 max_sync_show(struct mddev *mddev, char *page)
4452 if (mddev->resync_max == MaxSector)
4453 return sprintf(page, "max\n");
4455 return sprintf(page, "%llu\n",
4456 (unsigned long long)mddev->resync_max);
4459 max_sync_store(struct mddev *mddev, const char *buf, size_t len)
4461 if (strncmp(buf, "max", 3) == 0)
4462 mddev->resync_max = MaxSector;
4464 unsigned long long max;
4465 if (strict_strtoull(buf, 10, &max))
4467 if (max < mddev->resync_min)
4469 if (max < mddev->resync_max &&
4471 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4474 /* Must be a multiple of chunk_size */
4475 if (mddev->chunk_sectors) {
4476 sector_t temp = max;
4477 if (sector_div(temp, mddev->chunk_sectors))
4480 mddev->resync_max = max;
4482 wake_up(&mddev->recovery_wait);
4486 static struct md_sysfs_entry md_max_sync =
4487 __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4490 suspend_lo_show(struct mddev *mddev, char *page)
4492 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4496 suspend_lo_store(struct mddev *mddev, const char *buf, size_t len)
4499 unsigned long long new = simple_strtoull(buf, &e, 10);
4500 unsigned long long old = mddev->suspend_lo;
4502 if (mddev->pers == NULL ||
4503 mddev->pers->quiesce == NULL)
4505 if (buf == e || (*e && *e != '\n'))
4508 mddev->suspend_lo = new;
4510 /* Shrinking suspended region */
4511 mddev->pers->quiesce(mddev, 2);
4513 /* Expanding suspended region - need to wait */
4514 mddev->pers->quiesce(mddev, 1);
4515 mddev->pers->quiesce(mddev, 0);
4519 static struct md_sysfs_entry md_suspend_lo =
4520 __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4524 suspend_hi_show(struct mddev *mddev, char *page)
4526 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4530 suspend_hi_store(struct mddev *mddev, const char *buf, size_t len)
4533 unsigned long long new = simple_strtoull(buf, &e, 10);
4534 unsigned long long old = mddev->suspend_hi;
4536 if (mddev->pers == NULL ||
4537 mddev->pers->quiesce == NULL)
4539 if (buf == e || (*e && *e != '\n'))
4542 mddev->suspend_hi = new;
4544 /* Shrinking suspended region */
4545 mddev->pers->quiesce(mddev, 2);
4547 /* Expanding suspended region - need to wait */
4548 mddev->pers->quiesce(mddev, 1);
4549 mddev->pers->quiesce(mddev, 0);
4553 static struct md_sysfs_entry md_suspend_hi =
4554 __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4557 reshape_position_show(struct mddev *mddev, char *page)
4559 if (mddev->reshape_position != MaxSector)
4560 return sprintf(page, "%llu\n",
4561 (unsigned long long)mddev->reshape_position);
4562 strcpy(page, "none\n");
4567 reshape_position_store(struct mddev *mddev, const char *buf, size_t len)
4569 struct md_rdev *rdev;
4571 unsigned long long new = simple_strtoull(buf, &e, 10);
4574 if (buf == e || (*e && *e != '\n'))
4576 mddev->reshape_position = new;
4577 mddev->delta_disks = 0;
4578 mddev->reshape_backwards = 0;
4579 mddev->new_level = mddev->level;
4580 mddev->new_layout = mddev->layout;
4581 mddev->new_chunk_sectors = mddev->chunk_sectors;
4582 rdev_for_each(rdev, mddev)
4583 rdev->new_data_offset = rdev->data_offset;
4587 static struct md_sysfs_entry md_reshape_position =
4588 __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4589 reshape_position_store);
4592 reshape_direction_show(struct mddev *mddev, char *page)
4594 return sprintf(page, "%s\n",
4595 mddev->reshape_backwards ? "backwards" : "forwards");
4599 reshape_direction_store(struct mddev *mddev, const char *buf, size_t len)
4602 if (cmd_match(buf, "forwards"))
4604 else if (cmd_match(buf, "backwards"))
4608 if (mddev->reshape_backwards == backwards)
4611 /* check if we are allowed to change */
4612 if (mddev->delta_disks)
4615 if (mddev->persistent &&
4616 mddev->major_version == 0)
4619 mddev->reshape_backwards = backwards;
4623 static struct md_sysfs_entry md_reshape_direction =
4624 __ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show,
4625 reshape_direction_store);
4628 array_size_show(struct mddev *mddev, char *page)
4630 if (mddev->external_size)
4631 return sprintf(page, "%llu\n",
4632 (unsigned long long)mddev->array_sectors/2);
4634 return sprintf(page, "default\n");
4638 array_size_store(struct mddev *mddev, const char *buf, size_t len)
4642 if (strncmp(buf, "default", 7) == 0) {
4644 sectors = mddev->pers->size(mddev, 0, 0);
4646 sectors = mddev->array_sectors;
4648 mddev->external_size = 0;
4650 if (strict_blocks_to_sectors(buf, §ors) < 0)
4652 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
4655 mddev->external_size = 1;
4658 mddev->array_sectors = sectors;
4660 set_capacity(mddev->gendisk, mddev->array_sectors);
4661 revalidate_disk(mddev->gendisk);
4666 static struct md_sysfs_entry md_array_size =
4667 __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4670 static struct attribute *md_default_attrs[] = {
4673 &md_raid_disks.attr,
4674 &md_chunk_size.attr,
4676 &md_resync_start.attr,
4678 &md_new_device.attr,
4679 &md_safe_delay.attr,
4680 &md_array_state.attr,
4681 &md_reshape_position.attr,
4682 &md_reshape_direction.attr,
4683 &md_array_size.attr,
4684 &max_corr_read_errors.attr,
4688 static struct attribute *md_redundancy_attrs[] = {
4690 &md_mismatches.attr,
4693 &md_sync_speed.attr,
4694 &md_sync_force_parallel.attr,
4695 &md_sync_completed.attr,
4698 &md_suspend_lo.attr,
4699 &md_suspend_hi.attr,
4704 static struct attribute_group md_redundancy_group = {
4706 .attrs = md_redundancy_attrs,
4711 md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4713 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4714 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
4719 spin_lock(&all_mddevs_lock);
4720 if (list_empty(&mddev->all_mddevs)) {
4721 spin_unlock(&all_mddevs_lock);
4725 spin_unlock(&all_mddevs_lock);
4727 rv = mddev_lock(mddev);
4729 rv = entry->show(mddev, page);
4730 mddev_unlock(mddev);
4737 md_attr_store(struct kobject *kobj, struct attribute *attr,
4738 const char *page, size_t length)
4740 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4741 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
4746 if (!capable(CAP_SYS_ADMIN))
4748 spin_lock(&all_mddevs_lock);
4749 if (list_empty(&mddev->all_mddevs)) {
4750 spin_unlock(&all_mddevs_lock);
4754 spin_unlock(&all_mddevs_lock);
4755 if (entry->store == new_dev_store)
4756 flush_workqueue(md_misc_wq);
4757 rv = mddev_lock(mddev);
4759 rv = entry->store(mddev, page, length);
4760 mddev_unlock(mddev);
4766 static void md_free(struct kobject *ko)
4768 struct mddev *mddev = container_of(ko, struct mddev, kobj);
4770 if (mddev->sysfs_state)
4771 sysfs_put(mddev->sysfs_state);
4773 if (mddev->gendisk) {
4774 del_gendisk(mddev->gendisk);
4775 put_disk(mddev->gendisk);
4778 blk_cleanup_queue(mddev->queue);
4783 static const struct sysfs_ops md_sysfs_ops = {
4784 .show = md_attr_show,
4785 .store = md_attr_store,
4787 static struct kobj_type md_ktype = {
4789 .sysfs_ops = &md_sysfs_ops,
4790 .default_attrs = md_default_attrs,
4795 static void mddev_delayed_delete(struct work_struct *ws)
4797 struct mddev *mddev = container_of(ws, struct mddev, del_work);
4799 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
4800 kobject_del(&mddev->kobj);
4801 kobject_put(&mddev->kobj);
4804 static int md_alloc(dev_t dev, char *name)
4806 static DEFINE_MUTEX(disks_mutex);
4807 struct mddev *mddev = mddev_find(dev);
4808 struct gendisk *disk;
4817 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4818 shift = partitioned ? MdpMinorShift : 0;
4819 unit = MINOR(mddev->unit) >> shift;
4821 /* wait for any previous instance of this device to be
4822 * completely removed (mddev_delayed_delete).
4824 flush_workqueue(md_misc_wq);
4826 mutex_lock(&disks_mutex);
4832 /* Need to ensure that 'name' is not a duplicate.
4834 struct mddev *mddev2;
4835 spin_lock(&all_mddevs_lock);
4837 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
4838 if (mddev2->gendisk &&
4839 strcmp(mddev2->gendisk->disk_name, name) == 0) {
4840 spin_unlock(&all_mddevs_lock);
4843 spin_unlock(&all_mddevs_lock);
4847 mddev->queue = blk_alloc_queue(GFP_KERNEL);
4850 mddev->queue->queuedata = mddev;
4852 blk_queue_make_request(mddev->queue, md_make_request);
4853 blk_set_stacking_limits(&mddev->queue->limits);
4855 disk = alloc_disk(1 << shift);
4857 blk_cleanup_queue(mddev->queue);
4858 mddev->queue = NULL;
4861 disk->major = MAJOR(mddev->unit);
4862 disk->first_minor = unit << shift;
4864 strcpy(disk->disk_name, name);
4865 else if (partitioned)
4866 sprintf(disk->disk_name, "md_d%d", unit);
4868 sprintf(disk->disk_name, "md%d", unit);
4869 disk->fops = &md_fops;
4870 disk->private_data = mddev;
4871 disk->queue = mddev->queue;
4872 blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA);
4873 /* Allow extended partitions. This makes the
4874 * 'mdp' device redundant, but we can't really
4877 disk->flags |= GENHD_FL_EXT_DEVT;
4878 mddev->gendisk = disk;
4879 /* As soon as we call add_disk(), another thread could get
4880 * through to md_open, so make sure it doesn't get too far
4882 mutex_lock(&mddev->open_mutex);
4885 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
4886 &disk_to_dev(disk)->kobj, "%s", "md");
4888 /* This isn't possible, but as kobject_init_and_add is marked
4889 * __must_check, we must do something with the result
4891 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
4895 if (mddev->kobj.sd &&
4896 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
4897 printk(KERN_DEBUG "pointless warning\n");
4898 mutex_unlock(&mddev->open_mutex);
4900 mutex_unlock(&disks_mutex);
4901 if (!error && mddev->kobj.sd) {
4902 kobject_uevent(&mddev->kobj, KOBJ_ADD);
4903 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
4909 static struct kobject *md_probe(dev_t dev, int *part, void *data)
4911 md_alloc(dev, NULL);
4915 static int add_named_array(const char *val, struct kernel_param *kp)
4917 /* val must be "md_*" where * is not all digits.
4918 * We allocate an array with a large free minor number, and
4919 * set the name to val. val must not already be an active name.
4921 int len = strlen(val);
4922 char buf[DISK_NAME_LEN];
4924 while (len && val[len-1] == '\n')
4926 if (len >= DISK_NAME_LEN)
4928 strlcpy(buf, val, len+1);
4929 if (strncmp(buf, "md_", 3) != 0)
4931 return md_alloc(0, buf);
4934 static void md_safemode_timeout(unsigned long data)
4936 struct mddev *mddev = (struct mddev *) data;
4938 if (!atomic_read(&mddev->writes_pending)) {
4939 mddev->safemode = 1;
4940 if (mddev->external)
4941 sysfs_notify_dirent_safe(mddev->sysfs_state);
4943 md_wakeup_thread(mddev->thread);
4946 static int start_dirty_degraded;
4948 int md_run(struct mddev *mddev)
4951 struct md_rdev *rdev;
4952 struct md_personality *pers;
4954 if (list_empty(&mddev->disks))
4955 /* cannot run an array with no devices.. */
4960 /* Cannot run until previous stop completes properly */
4961 if (mddev->sysfs_active)
4965 * Analyze all RAID superblock(s)
4967 if (!mddev->raid_disks) {
4968 if (!mddev->persistent)
4973 if (mddev->level != LEVEL_NONE)
4974 request_module("md-level-%d", mddev->level);
4975 else if (mddev->clevel[0])
4976 request_module("md-%s", mddev->clevel);
4979 * Drop all container device buffers, from now on
4980 * the only valid external interface is through the md
4983 rdev_for_each(rdev, mddev) {
4984 if (test_bit(Faulty, &rdev->flags))
4986 sync_blockdev(rdev->bdev);
4987 invalidate_bdev(rdev->bdev);
4989 /* perform some consistency tests on the device.
4990 * We don't want the data to overlap the metadata,
4991 * Internal Bitmap issues have been handled elsewhere.
4993 if (rdev->meta_bdev) {
4994 /* Nothing to check */;
4995 } else if (rdev->data_offset < rdev->sb_start) {
4996 if (mddev->dev_sectors &&
4997 rdev->data_offset + mddev->dev_sectors
4999 printk("md: %s: data overlaps metadata\n",
5004 if (rdev->sb_start + rdev->sb_size/512
5005 > rdev->data_offset) {
5006 printk("md: %s: metadata overlaps data\n",
5011 sysfs_notify_dirent_safe(rdev->sysfs_state);
5014 if (mddev->bio_set == NULL)
5015 mddev->bio_set = bioset_create(BIO_POOL_SIZE, 0);
5017 spin_lock(&pers_lock);
5018 pers = find_pers(mddev->level, mddev->clevel);
5019 if (!pers || !try_module_get(pers->owner)) {
5020 spin_unlock(&pers_lock);
5021 if (mddev->level != LEVEL_NONE)
5022 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
5025 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
5030 spin_unlock(&pers_lock);
5031 if (mddev->level != pers->level) {
5032 mddev->level = pers->level;
5033 mddev->new_level = pers->level;
5035 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
5037 if (mddev->reshape_position != MaxSector &&
5038 pers->start_reshape == NULL) {
5039 /* This personality cannot handle reshaping... */
5041 module_put(pers->owner);
5045 if (pers->sync_request) {
5046 /* Warn if this is a potentially silly
5049 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
5050 struct md_rdev *rdev2;
5053 rdev_for_each(rdev, mddev)
5054 rdev_for_each(rdev2, mddev) {
5056 rdev->bdev->bd_contains ==
5057 rdev2->bdev->bd_contains) {
5059 "%s: WARNING: %s appears to be"
5060 " on the same physical disk as"
5063 bdevname(rdev->bdev,b),
5064 bdevname(rdev2->bdev,b2));
5071 "True protection against single-disk"
5072 " failure might be compromised.\n");
5075 mddev->recovery = 0;
5076 /* may be over-ridden by personality */
5077 mddev->resync_max_sectors = mddev->dev_sectors;
5079 mddev->ok_start_degraded = start_dirty_degraded;
5081 if (start_readonly && mddev->ro == 0)
5082 mddev->ro = 2; /* read-only, but switch on first write */
5084 err = mddev->pers->run(mddev);
5086 printk(KERN_ERR "md: pers->run() failed ...\n");
5087 else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
5088 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
5089 " but 'external_size' not in effect?\n", __func__);
5091 "md: invalid array_size %llu > default size %llu\n",
5092 (unsigned long long)mddev->array_sectors / 2,
5093 (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
5095 mddev->pers->stop(mddev);
5097 if (err == 0 && mddev->pers->sync_request &&
5098 (mddev->bitmap_info.file || mddev->bitmap_info.offset)) {
5099 err = bitmap_create(mddev);
5101 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
5102 mdname(mddev), err);
5103 mddev->pers->stop(mddev);
5107 module_put(mddev->pers->owner);
5109 bitmap_destroy(mddev);
5112 if (mddev->pers->sync_request) {
5113 if (mddev->kobj.sd &&
5114 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
5116 "md: cannot register extra attributes for %s\n",
5118 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
5119 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
5122 atomic_set(&mddev->writes_pending,0);
5123 atomic_set(&mddev->max_corr_read_errors,
5124 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
5125 mddev->safemode = 0;
5126 mddev->safemode_timer.function = md_safemode_timeout;
5127 mddev->safemode_timer.data = (unsigned long) mddev;
5128 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
5132 rdev_for_each(rdev, mddev)
5133 if (rdev->raid_disk >= 0)
5134 if (sysfs_link_rdev(mddev, rdev))
5135 /* failure here is OK */;
5137 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5140 md_update_sb(mddev, 0);
5142 md_new_event(mddev);
5143 sysfs_notify_dirent_safe(mddev->sysfs_state);
5144 sysfs_notify_dirent_safe(mddev->sysfs_action);
5145 sysfs_notify(&mddev->kobj, NULL, "degraded");
5148 EXPORT_SYMBOL_GPL(md_run);
5150 static int do_md_run(struct mddev *mddev)
5154 err = md_run(mddev);
5157 err = bitmap_load(mddev);
5159 bitmap_destroy(mddev);
5163 md_wakeup_thread(mddev->thread);
5164 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
5166 set_capacity(mddev->gendisk, mddev->array_sectors);
5167 revalidate_disk(mddev->gendisk);
5169 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
5174 static int restart_array(struct mddev *mddev)
5176 struct gendisk *disk = mddev->gendisk;
5178 /* Complain if it has no devices */
5179 if (list_empty(&mddev->disks))
5185 mddev->safemode = 0;
5187 set_disk_ro(disk, 0);
5188 printk(KERN_INFO "md: %s switched to read-write mode.\n",
5190 /* Kick recovery or resync if necessary */
5191 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5192 md_wakeup_thread(mddev->thread);
5193 md_wakeup_thread(mddev->sync_thread);
5194 sysfs_notify_dirent_safe(mddev->sysfs_state);
5198 /* similar to deny_write_access, but accounts for our holding a reference
5199 * to the file ourselves */
5200 static int deny_bitmap_write_access(struct file * file)
5202 struct inode *inode = file->f_mapping->host;
5204 spin_lock(&inode->i_lock);
5205 if (atomic_read(&inode->i_writecount) > 1) {
5206 spin_unlock(&inode->i_lock);
5209 atomic_set(&inode->i_writecount, -1);
5210 spin_unlock(&inode->i_lock);
5215 void restore_bitmap_write_access(struct file *file)
5217 struct inode *inode = file->f_mapping->host;
5219 spin_lock(&inode->i_lock);
5220 atomic_set(&inode->i_writecount, 1);
5221 spin_unlock(&inode->i_lock);
5224 static void md_clean(struct mddev *mddev)
5226 mddev->array_sectors = 0;
5227 mddev->external_size = 0;
5228 mddev->dev_sectors = 0;
5229 mddev->raid_disks = 0;
5230 mddev->recovery_cp = 0;
5231 mddev->resync_min = 0;
5232 mddev->resync_max = MaxSector;
5233 mddev->reshape_position = MaxSector;
5234 mddev->external = 0;
5235 mddev->persistent = 0;
5236 mddev->level = LEVEL_NONE;
5237 mddev->clevel[0] = 0;
5240 mddev->metadata_type[0] = 0;
5241 mddev->chunk_sectors = 0;
5242 mddev->ctime = mddev->utime = 0;
5244 mddev->max_disks = 0;
5246 mddev->can_decrease_events = 0;
5247 mddev->delta_disks = 0;
5248 mddev->reshape_backwards = 0;
5249 mddev->new_level = LEVEL_NONE;
5250 mddev->new_layout = 0;
5251 mddev->new_chunk_sectors = 0;
5252 mddev->curr_resync = 0;
5253 atomic64_set(&mddev->resync_mismatches, 0);
5254 mddev->suspend_lo = mddev->suspend_hi = 0;
5255 mddev->sync_speed_min = mddev->sync_speed_max = 0;
5256 mddev->recovery = 0;
5259 mddev->degraded = 0;
5260 mddev->safemode = 0;
5261 mddev->merge_check_needed = 0;
5262 mddev->bitmap_info.offset = 0;
5263 mddev->bitmap_info.default_offset = 0;
5264 mddev->bitmap_info.default_space = 0;
5265 mddev->bitmap_info.chunksize = 0;
5266 mddev->bitmap_info.daemon_sleep = 0;
5267 mddev->bitmap_info.max_write_behind = 0;
5270 static void __md_stop_writes(struct mddev *mddev)
5272 if (mddev->sync_thread) {
5273 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5274 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5275 reap_sync_thread(mddev);
5278 del_timer_sync(&mddev->safemode_timer);
5280 bitmap_flush(mddev);
5281 md_super_wait(mddev);
5283 if (!mddev->in_sync || mddev->flags) {
5284 /* mark array as shutdown cleanly */
5286 md_update_sb(mddev, 1);
5290 void md_stop_writes(struct mddev *mddev)
5293 __md_stop_writes(mddev);
5294 mddev_unlock(mddev);
5296 EXPORT_SYMBOL_GPL(md_stop_writes);
5298 static void __md_stop(struct mddev *mddev)
5301 mddev->pers->stop(mddev);
5302 if (mddev->pers->sync_request && mddev->to_remove == NULL)
5303 mddev->to_remove = &md_redundancy_group;
5304 module_put(mddev->pers->owner);
5306 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5309 void md_stop(struct mddev *mddev)
5311 /* stop the array and free an attached data structures.
5312 * This is called from dm-raid
5315 bitmap_destroy(mddev);
5317 bioset_free(mddev->bio_set);
5320 EXPORT_SYMBOL_GPL(md_stop);
5322 static int md_set_readonly(struct mddev *mddev, struct block_device *bdev)
5325 mutex_lock(&mddev->open_mutex);
5326 if (atomic_read(&mddev->openers) > !!bdev) {
5327 printk("md: %s still in use.\n",mdname(mddev));
5332 sync_blockdev(bdev);
5334 __md_stop_writes(mddev);
5340 set_disk_ro(mddev->gendisk, 1);
5341 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5342 sysfs_notify_dirent_safe(mddev->sysfs_state);
5346 mutex_unlock(&mddev->open_mutex);
5351 * 0 - completely stop and dis-assemble array
5352 * 2 - stop but do not disassemble array
5354 static int do_md_stop(struct mddev * mddev, int mode,
5355 struct block_device *bdev)
5357 struct gendisk *disk = mddev->gendisk;
5358 struct md_rdev *rdev;
5360 mutex_lock(&mddev->open_mutex);
5361 if (atomic_read(&mddev->openers) > !!bdev ||
5362 mddev->sysfs_active) {
5363 printk("md: %s still in use.\n",mdname(mddev));
5364 mutex_unlock(&mddev->open_mutex);
5368 /* It is possible IO was issued on some other
5369 * open file which was closed before we took ->open_mutex.
5370 * As that was not the last close __blkdev_put will not
5371 * have called sync_blockdev, so we must.
5373 sync_blockdev(bdev);
5377 set_disk_ro(disk, 0);
5379 __md_stop_writes(mddev);
5381 mddev->queue->merge_bvec_fn = NULL;
5382 mddev->queue->backing_dev_info.congested_fn = NULL;
5384 /* tell userspace to handle 'inactive' */
5385 sysfs_notify_dirent_safe(mddev->sysfs_state);
5387 rdev_for_each(rdev, mddev)
5388 if (rdev->raid_disk >= 0)
5389 sysfs_unlink_rdev(mddev, rdev);
5391 set_capacity(disk, 0);
5392 mutex_unlock(&mddev->open_mutex);
5394 revalidate_disk(disk);
5399 mutex_unlock(&mddev->open_mutex);
5401 * Free resources if final stop
5404 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
5406 bitmap_destroy(mddev);
5407 if (mddev->bitmap_info.file) {
5408 restore_bitmap_write_access(mddev->bitmap_info.file);
5409 fput(mddev->bitmap_info.file);
5410 mddev->bitmap_info.file = NULL;
5412 mddev->bitmap_info.offset = 0;
5414 export_array(mddev);
5417 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
5418 if (mddev->hold_active == UNTIL_STOP)
5419 mddev->hold_active = 0;
5421 blk_integrity_unregister(disk);
5422 md_new_event(mddev);
5423 sysfs_notify_dirent_safe(mddev->sysfs_state);
5428 static void autorun_array(struct mddev *mddev)
5430 struct md_rdev *rdev;
5433 if (list_empty(&mddev->disks))
5436 printk(KERN_INFO "md: running: ");
5438 rdev_for_each(rdev, mddev) {
5439 char b[BDEVNAME_SIZE];
5440 printk("<%s>", bdevname(rdev->bdev,b));
5444 err = do_md_run(mddev);
5446 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
5447 do_md_stop(mddev, 0, NULL);
5452 * lets try to run arrays based on all disks that have arrived
5453 * until now. (those are in pending_raid_disks)
5455 * the method: pick the first pending disk, collect all disks with
5456 * the same UUID, remove all from the pending list and put them into
5457 * the 'same_array' list. Then order this list based on superblock
5458 * update time (freshest comes first), kick out 'old' disks and
5459 * compare superblocks. If everything's fine then run it.
5461 * If "unit" is allocated, then bump its reference count
5463 static void autorun_devices(int part)
5465 struct md_rdev *rdev0, *rdev, *tmp;
5466 struct mddev *mddev;
5467 char b[BDEVNAME_SIZE];
5469 printk(KERN_INFO "md: autorun ...\n");
5470 while (!list_empty(&pending_raid_disks)) {
5473 LIST_HEAD(candidates);
5474 rdev0 = list_entry(pending_raid_disks.next,
5475 struct md_rdev, same_set);
5477 printk(KERN_INFO "md: considering %s ...\n",
5478 bdevname(rdev0->bdev,b));
5479 INIT_LIST_HEAD(&candidates);
5480 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
5481 if (super_90_load(rdev, rdev0, 0) >= 0) {
5482 printk(KERN_INFO "md: adding %s ...\n",
5483 bdevname(rdev->bdev,b));
5484 list_move(&rdev->same_set, &candidates);
5487 * now we have a set of devices, with all of them having
5488 * mostly sane superblocks. It's time to allocate the
5492 dev = MKDEV(mdp_major,
5493 rdev0->preferred_minor << MdpMinorShift);
5494 unit = MINOR(dev) >> MdpMinorShift;
5496 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
5499 if (rdev0->preferred_minor != unit) {
5500 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
5501 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
5505 md_probe(dev, NULL, NULL);
5506 mddev = mddev_find(dev);
5507 if (!mddev || !mddev->gendisk) {
5511 "md: cannot allocate memory for md drive.\n");
5514 if (mddev_lock(mddev))
5515 printk(KERN_WARNING "md: %s locked, cannot run\n",
5517 else if (mddev->raid_disks || mddev->major_version
5518 || !list_empty(&mddev->disks)) {
5520 "md: %s already running, cannot run %s\n",
5521 mdname(mddev), bdevname(rdev0->bdev,b));
5522 mddev_unlock(mddev);
5524 printk(KERN_INFO "md: created %s\n", mdname(mddev));
5525 mddev->persistent = 1;
5526 rdev_for_each_list(rdev, tmp, &candidates) {
5527 list_del_init(&rdev->same_set);
5528 if (bind_rdev_to_array(rdev, mddev))
5531 autorun_array(mddev);
5532 mddev_unlock(mddev);
5534 /* on success, candidates will be empty, on error
5537 rdev_for_each_list(rdev, tmp, &candidates) {
5538 list_del_init(&rdev->same_set);
5543 printk(KERN_INFO "md: ... autorun DONE.\n");
5545 #endif /* !MODULE */
5547 static int get_version(void __user * arg)
5551 ver.major = MD_MAJOR_VERSION;
5552 ver.minor = MD_MINOR_VERSION;
5553 ver.patchlevel = MD_PATCHLEVEL_VERSION;
5555 if (copy_to_user(arg, &ver, sizeof(ver)))
5561 static int get_array_info(struct mddev * mddev, void __user * arg)
5563 mdu_array_info_t info;
5564 int nr,working,insync,failed,spare;
5565 struct md_rdev *rdev;
5567 nr = working = insync = failed = spare = 0;
5569 rdev_for_each_rcu(rdev, mddev) {
5571 if (test_bit(Faulty, &rdev->flags))
5575 if (test_bit(In_sync, &rdev->flags))
5583 info.major_version = mddev->major_version;
5584 info.minor_version = mddev->minor_version;
5585 info.patch_version = MD_PATCHLEVEL_VERSION;
5586 info.ctime = mddev->ctime;
5587 info.level = mddev->level;
5588 info.size = mddev->dev_sectors / 2;
5589 if (info.size != mddev->dev_sectors / 2) /* overflow */
5592 info.raid_disks = mddev->raid_disks;
5593 info.md_minor = mddev->md_minor;
5594 info.not_persistent= !mddev->persistent;
5596 info.utime = mddev->utime;
5599 info.state = (1<<MD_SB_CLEAN);
5600 if (mddev->bitmap && mddev->bitmap_info.offset)
5601 info.state = (1<<MD_SB_BITMAP_PRESENT);
5602 info.active_disks = insync;
5603 info.working_disks = working;
5604 info.failed_disks = failed;
5605 info.spare_disks = spare;
5607 info.layout = mddev->layout;
5608 info.chunk_size = mddev->chunk_sectors << 9;
5610 if (copy_to_user(arg, &info, sizeof(info)))
5616 static int get_bitmap_file(struct mddev * mddev, void __user * arg)
5618 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
5619 char *ptr, *buf = NULL;
5622 if (md_allow_write(mddev))
5623 file = kmalloc(sizeof(*file), GFP_NOIO);
5625 file = kmalloc(sizeof(*file), GFP_KERNEL);
5630 /* bitmap disabled, zero the first byte and copy out */
5631 if (!mddev->bitmap || !mddev->bitmap->storage.file) {
5632 file->pathname[0] = '\0';
5636 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
5640 ptr = d_path(&mddev->bitmap->storage.file->f_path,
5641 buf, sizeof(file->pathname));
5645 strcpy(file->pathname, ptr);
5649 if (copy_to_user(arg, file, sizeof(*file)))
5657 static int get_disk_info(struct mddev * mddev, void __user * arg)
5659 mdu_disk_info_t info;
5660 struct md_rdev *rdev;
5662 if (copy_from_user(&info, arg, sizeof(info)))
5666 rdev = find_rdev_nr_rcu(mddev, info.number);
5668 info.major = MAJOR(rdev->bdev->bd_dev);
5669 info.minor = MINOR(rdev->bdev->bd_dev);
5670 info.raid_disk = rdev->raid_disk;
5672 if (test_bit(Faulty, &rdev->flags))
5673 info.state |= (1<<MD_DISK_FAULTY);
5674 else if (test_bit(In_sync, &rdev->flags)) {
5675 info.state |= (1<<MD_DISK_ACTIVE);
5676 info.state |= (1<<MD_DISK_SYNC);
5678 if (test_bit(WriteMostly, &rdev->flags))
5679 info.state |= (1<<MD_DISK_WRITEMOSTLY);
5681 info.major = info.minor = 0;
5682 info.raid_disk = -1;
5683 info.state = (1<<MD_DISK_REMOVED);
5687 if (copy_to_user(arg, &info, sizeof(info)))
5693 static int add_new_disk(struct mddev * mddev, mdu_disk_info_t *info)
5695 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
5696 struct md_rdev *rdev;
5697 dev_t dev = MKDEV(info->major,info->minor);
5699 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
5702 if (!mddev->raid_disks) {
5704 /* expecting a device which has a superblock */
5705 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
5708 "md: md_import_device returned %ld\n",
5710 return PTR_ERR(rdev);
5712 if (!list_empty(&mddev->disks)) {
5713 struct md_rdev *rdev0
5714 = list_entry(mddev->disks.next,
5715 struct md_rdev, same_set);
5716 err = super_types[mddev->major_version]
5717 .load_super(rdev, rdev0, mddev->minor_version);
5720 "md: %s has different UUID to %s\n",
5721 bdevname(rdev->bdev,b),
5722 bdevname(rdev0->bdev,b2));
5727 err = bind_rdev_to_array(rdev, mddev);
5734 * add_new_disk can be used once the array is assembled
5735 * to add "hot spares". They must already have a superblock
5740 if (!mddev->pers->hot_add_disk) {
5742 "%s: personality does not support diskops!\n",
5746 if (mddev->persistent)
5747 rdev = md_import_device(dev, mddev->major_version,
5748 mddev->minor_version);
5750 rdev = md_import_device(dev, -1, -1);
5753 "md: md_import_device returned %ld\n",
5755 return PTR_ERR(rdev);
5757 /* set saved_raid_disk if appropriate */
5758 if (!mddev->persistent) {
5759 if (info->state & (1<<MD_DISK_SYNC) &&
5760 info->raid_disk < mddev->raid_disks) {
5761 rdev->raid_disk = info->raid_disk;
5762 set_bit(In_sync, &rdev->flags);
5764 rdev->raid_disk = -1;
5766 super_types[mddev->major_version].
5767 validate_super(mddev, rdev);
5768 if ((info->state & (1<<MD_DISK_SYNC)) &&
5769 rdev->raid_disk != info->raid_disk) {
5770 /* This was a hot-add request, but events doesn't
5771 * match, so reject it.
5777 if (test_bit(In_sync, &rdev->flags))
5778 rdev->saved_raid_disk = rdev->raid_disk;
5780 rdev->saved_raid_disk = -1;
5782 clear_bit(In_sync, &rdev->flags); /* just to be sure */
5783 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5784 set_bit(WriteMostly, &rdev->flags);
5786 clear_bit(WriteMostly, &rdev->flags);
5788 rdev->raid_disk = -1;
5789 err = bind_rdev_to_array(rdev, mddev);
5790 if (!err && !mddev->pers->hot_remove_disk) {
5791 /* If there is hot_add_disk but no hot_remove_disk
5792 * then added disks for geometry changes,
5793 * and should be added immediately.
5795 super_types[mddev->major_version].
5796 validate_super(mddev, rdev);
5797 err = mddev->pers->hot_add_disk(mddev, rdev);
5799 unbind_rdev_from_array(rdev);
5804 sysfs_notify_dirent_safe(rdev->sysfs_state);
5806 md_update_sb(mddev, 1);
5807 if (mddev->degraded)
5808 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
5809 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5811 md_new_event(mddev);
5812 md_wakeup_thread(mddev->thread);
5816 /* otherwise, add_new_disk is only allowed
5817 * for major_version==0 superblocks
5819 if (mddev->major_version != 0) {
5820 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
5825 if (!(info->state & (1<<MD_DISK_FAULTY))) {
5827 rdev = md_import_device(dev, -1, 0);
5830 "md: error, md_import_device() returned %ld\n",
5832 return PTR_ERR(rdev);
5834 rdev->desc_nr = info->number;
5835 if (info->raid_disk < mddev->raid_disks)
5836 rdev->raid_disk = info->raid_disk;
5838 rdev->raid_disk = -1;
5840 if (rdev->raid_disk < mddev->raid_disks)
5841 if (info->state & (1<<MD_DISK_SYNC))
5842 set_bit(In_sync, &rdev->flags);
5844 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5845 set_bit(WriteMostly, &rdev->flags);
5847 if (!mddev->persistent) {
5848 printk(KERN_INFO "md: nonpersistent superblock ...\n");
5849 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
5851 rdev->sb_start = calc_dev_sboffset(rdev);
5852 rdev->sectors = rdev->sb_start;
5854 err = bind_rdev_to_array(rdev, mddev);
5864 static int hot_remove_disk(struct mddev * mddev, dev_t dev)
5866 char b[BDEVNAME_SIZE];
5867 struct md_rdev *rdev;
5869 rdev = find_rdev(mddev, dev);
5873 if (rdev->raid_disk >= 0)
5876 kick_rdev_from_array(rdev);
5877 md_update_sb(mddev, 1);
5878 md_new_event(mddev);
5882 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
5883 bdevname(rdev->bdev,b), mdname(mddev));
5887 static int hot_add_disk(struct mddev * mddev, dev_t dev)
5889 char b[BDEVNAME_SIZE];
5891 struct md_rdev *rdev;
5896 if (mddev->major_version != 0) {
5897 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
5898 " version-0 superblocks.\n",
5902 if (!mddev->pers->hot_add_disk) {
5904 "%s: personality does not support diskops!\n",
5909 rdev = md_import_device(dev, -1, 0);
5912 "md: error, md_import_device() returned %ld\n",
5917 if (mddev->persistent)
5918 rdev->sb_start = calc_dev_sboffset(rdev);
5920 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
5922 rdev->sectors = rdev->sb_start;
5924 if (test_bit(Faulty, &rdev->flags)) {
5926 "md: can not hot-add faulty %s disk to %s!\n",
5927 bdevname(rdev->bdev,b), mdname(mddev));
5931 clear_bit(In_sync, &rdev->flags);
5933 rdev->saved_raid_disk = -1;
5934 err = bind_rdev_to_array(rdev, mddev);
5939 * The rest should better be atomic, we can have disk failures
5940 * noticed in interrupt contexts ...
5943 rdev->raid_disk = -1;
5945 md_update_sb(mddev, 1);
5948 * Kick recovery, maybe this spare has to be added to the
5949 * array immediately.
5951 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5952 md_wakeup_thread(mddev->thread);
5953 md_new_event(mddev);
5961 static int set_bitmap_file(struct mddev *mddev, int fd)
5966 if (!mddev->pers->quiesce)
5968 if (mddev->recovery || mddev->sync_thread)
5970 /* we should be able to change the bitmap.. */
5976 return -EEXIST; /* cannot add when bitmap is present */
5977 mddev->bitmap_info.file = fget(fd);
5979 if (mddev->bitmap_info.file == NULL) {
5980 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
5985 err = deny_bitmap_write_access(mddev->bitmap_info.file);
5987 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
5989 fput(mddev->bitmap_info.file);
5990 mddev->bitmap_info.file = NULL;
5993 mddev->bitmap_info.offset = 0; /* file overrides offset */
5994 } else if (mddev->bitmap == NULL)
5995 return -ENOENT; /* cannot remove what isn't there */
5998 mddev->pers->quiesce(mddev, 1);
6000 err = bitmap_create(mddev);
6002 err = bitmap_load(mddev);
6004 if (fd < 0 || err) {
6005 bitmap_destroy(mddev);
6006 fd = -1; /* make sure to put the file */
6008 mddev->pers->quiesce(mddev, 0);
6011 if (mddev->bitmap_info.file) {
6012 restore_bitmap_write_access(mddev->bitmap_info.file);
6013 fput(mddev->bitmap_info.file);
6015 mddev->bitmap_info.file = NULL;
6022 * set_array_info is used two different ways
6023 * The original usage is when creating a new array.
6024 * In this usage, raid_disks is > 0 and it together with
6025 * level, size, not_persistent,layout,chunksize determine the
6026 * shape of the array.
6027 * This will always create an array with a type-0.90.0 superblock.
6028 * The newer usage is when assembling an array.
6029 * In this case raid_disks will be 0, and the major_version field is
6030 * use to determine which style super-blocks are to be found on the devices.
6031 * The minor and patch _version numbers are also kept incase the
6032 * super_block handler wishes to interpret them.
6034 static int set_array_info(struct mddev * mddev, mdu_array_info_t *info)
6037 if (info->raid_disks == 0) {
6038 /* just setting version number for superblock loading */
6039 if (info->major_version < 0 ||
6040 info->major_version >= ARRAY_SIZE(super_types) ||
6041 super_types[info->major_version].name == NULL) {
6042 /* maybe try to auto-load a module? */
6044 "md: superblock version %d not known\n",
6045 info->major_version);
6048 mddev->major_version = info->major_version;
6049 mddev->minor_version = info->minor_version;
6050 mddev->patch_version = info->patch_version;
6051 mddev->persistent = !info->not_persistent;
6052 /* ensure mddev_put doesn't delete this now that there
6053 * is some minimal configuration.
6055 mddev->ctime = get_seconds();
6058 mddev->major_version = MD_MAJOR_VERSION;
6059 mddev->minor_version = MD_MINOR_VERSION;
6060 mddev->patch_version = MD_PATCHLEVEL_VERSION;
6061 mddev->ctime = get_seconds();
6063 mddev->level = info->level;
6064 mddev->clevel[0] = 0;
6065 mddev->dev_sectors = 2 * (sector_t)info->size;
6066 mddev->raid_disks = info->raid_disks;
6067 /* don't set md_minor, it is determined by which /dev/md* was
6070 if (info->state & (1<<MD_SB_CLEAN))
6071 mddev->recovery_cp = MaxSector;
6073 mddev->recovery_cp = 0;
6074 mddev->persistent = ! info->not_persistent;
6075 mddev->external = 0;
6077 mddev->layout = info->layout;
6078 mddev->chunk_sectors = info->chunk_size >> 9;
6080 mddev->max_disks = MD_SB_DISKS;
6082 if (mddev->persistent)
6084 set_bit(MD_CHANGE_DEVS, &mddev->flags);
6086 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
6087 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
6088 mddev->bitmap_info.offset = 0;
6090 mddev->reshape_position = MaxSector;
6093 * Generate a 128 bit UUID
6095 get_random_bytes(mddev->uuid, 16);
6097 mddev->new_level = mddev->level;
6098 mddev->new_chunk_sectors = mddev->chunk_sectors;
6099 mddev->new_layout = mddev->layout;
6100 mddev->delta_disks = 0;
6101 mddev->reshape_backwards = 0;
6106 void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors)
6108 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
6110 if (mddev->external_size)
6113 mddev->array_sectors = array_sectors;
6115 EXPORT_SYMBOL(md_set_array_sectors);
6117 static int update_size(struct mddev *mddev, sector_t num_sectors)
6119 struct md_rdev *rdev;
6121 int fit = (num_sectors == 0);
6123 if (mddev->pers->resize == NULL)
6125 /* The "num_sectors" is the number of sectors of each device that
6126 * is used. This can only make sense for arrays with redundancy.
6127 * linear and raid0 always use whatever space is available. We can only
6128 * consider changing this number if no resync or reconstruction is
6129 * happening, and if the new size is acceptable. It must fit before the
6130 * sb_start or, if that is <data_offset, it must fit before the size
6131 * of each device. If num_sectors is zero, we find the largest size
6134 if (mddev->sync_thread)
6137 rdev_for_each(rdev, mddev) {
6138 sector_t avail = rdev->sectors;
6140 if (fit && (num_sectors == 0 || num_sectors > avail))
6141 num_sectors = avail;
6142 if (avail < num_sectors)
6145 rv = mddev->pers->resize(mddev, num_sectors);
6147 revalidate_disk(mddev->gendisk);
6151 static int update_raid_disks(struct mddev *mddev, int raid_disks)
6154 struct md_rdev *rdev;
6155 /* change the number of raid disks */
6156 if (mddev->pers->check_reshape == NULL)
6158 if (raid_disks <= 0 ||
6159 (mddev->max_disks && raid_disks >= mddev->max_disks))
6161 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
6164 rdev_for_each(rdev, mddev) {
6165 if (mddev->raid_disks < raid_disks &&
6166 rdev->data_offset < rdev->new_data_offset)
6168 if (mddev->raid_disks > raid_disks &&
6169 rdev->data_offset > rdev->new_data_offset)
6173 mddev->delta_disks = raid_disks - mddev->raid_disks;
6174 if (mddev->delta_disks < 0)
6175 mddev->reshape_backwards = 1;
6176 else if (mddev->delta_disks > 0)
6177 mddev->reshape_backwards = 0;
6179 rv = mddev->pers->check_reshape(mddev);
6181 mddev->delta_disks = 0;
6182 mddev->reshape_backwards = 0;
6189 * update_array_info is used to change the configuration of an
6191 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
6192 * fields in the info are checked against the array.
6193 * Any differences that cannot be handled will cause an error.
6194 * Normally, only one change can be managed at a time.
6196 static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
6202 /* calculate expected state,ignoring low bits */
6203 if (mddev->bitmap && mddev->bitmap_info.offset)
6204 state |= (1 << MD_SB_BITMAP_PRESENT);
6206 if (mddev->major_version != info->major_version ||
6207 mddev->minor_version != info->minor_version ||
6208 /* mddev->patch_version != info->patch_version || */
6209 mddev->ctime != info->ctime ||
6210 mddev->level != info->level ||
6211 /* mddev->layout != info->layout || */
6212 !mddev->persistent != info->not_persistent||
6213 mddev->chunk_sectors != info->chunk_size >> 9 ||
6214 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
6215 ((state^info->state) & 0xfffffe00)
6218 /* Check there is only one change */
6219 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
6221 if (mddev->raid_disks != info->raid_disks)
6223 if (mddev->layout != info->layout)
6225 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
6232 if (mddev->layout != info->layout) {
6234 * we don't need to do anything at the md level, the
6235 * personality will take care of it all.
6237 if (mddev->pers->check_reshape == NULL)
6240 mddev->new_layout = info->layout;
6241 rv = mddev->pers->check_reshape(mddev);
6243 mddev->new_layout = mddev->layout;
6247 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
6248 rv = update_size(mddev, (sector_t)info->size * 2);
6250 if (mddev->raid_disks != info->raid_disks)
6251 rv = update_raid_disks(mddev, info->raid_disks);
6253 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
6254 if (mddev->pers->quiesce == NULL)
6256 if (mddev->recovery || mddev->sync_thread)
6258 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
6259 /* add the bitmap */
6262 if (mddev->bitmap_info.default_offset == 0)
6264 mddev->bitmap_info.offset =
6265 mddev->bitmap_info.default_offset;
6266 mddev->bitmap_info.space =
6267 mddev->bitmap_info.default_space;
6268 mddev->pers->quiesce(mddev, 1);
6269 rv = bitmap_create(mddev);
6271 rv = bitmap_load(mddev);
6273 bitmap_destroy(mddev);
6274 mddev->pers->quiesce(mddev, 0);
6276 /* remove the bitmap */
6279 if (mddev->bitmap->storage.file)
6281 mddev->pers->quiesce(mddev, 1);
6282 bitmap_destroy(mddev);
6283 mddev->pers->quiesce(mddev, 0);
6284 mddev->bitmap_info.offset = 0;
6287 md_update_sb(mddev, 1);
6291 static int set_disk_faulty(struct mddev *mddev, dev_t dev)
6293 struct md_rdev *rdev;
6296 if (mddev->pers == NULL)
6300 rdev = find_rdev_rcu(mddev, dev);
6304 md_error(mddev, rdev);
6305 if (!test_bit(Faulty, &rdev->flags))
6313 * We have a problem here : there is no easy way to give a CHS
6314 * virtual geometry. We currently pretend that we have a 2 heads
6315 * 4 sectors (with a BIG number of cylinders...). This drives
6316 * dosfs just mad... ;-)
6318 static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
6320 struct mddev *mddev = bdev->bd_disk->private_data;
6324 geo->cylinders = mddev->array_sectors / 8;
6328 static int md_ioctl(struct block_device *bdev, fmode_t mode,
6329 unsigned int cmd, unsigned long arg)
6332 void __user *argp = (void __user *)arg;
6333 struct mddev *mddev = NULL;
6338 case GET_ARRAY_INFO:
6342 if (!capable(CAP_SYS_ADMIN))
6347 * Commands dealing with the RAID driver but not any
6352 err = get_version(argp);
6355 case PRINT_RAID_DEBUG:
6363 autostart_arrays(arg);
6370 * Commands creating/starting a new array:
6373 mddev = bdev->bd_disk->private_data;
6380 /* Some actions do not requires the mutex */
6382 case GET_ARRAY_INFO:
6383 if (!mddev->raid_disks && !mddev->external)
6386 err = get_array_info(mddev, argp);
6390 if (!mddev->raid_disks && !mddev->external)
6393 err = get_disk_info(mddev, argp);
6396 case SET_DISK_FAULTY:
6397 err = set_disk_faulty(mddev, new_decode_dev(arg));
6401 if (cmd == ADD_NEW_DISK)
6402 /* need to ensure md_delayed_delete() has completed */
6403 flush_workqueue(md_misc_wq);
6405 err = mddev_lock(mddev);
6408 "md: ioctl lock interrupted, reason %d, cmd %d\n",
6413 if (cmd == SET_ARRAY_INFO) {
6414 mdu_array_info_t info;
6416 memset(&info, 0, sizeof(info));
6417 else if (copy_from_user(&info, argp, sizeof(info))) {
6422 err = update_array_info(mddev, &info);
6424 printk(KERN_WARNING "md: couldn't update"
6425 " array info. %d\n", err);
6430 if (!list_empty(&mddev->disks)) {
6432 "md: array %s already has disks!\n",
6437 if (mddev->raid_disks) {
6439 "md: array %s already initialised!\n",
6444 err = set_array_info(mddev, &info);
6446 printk(KERN_WARNING "md: couldn't set"
6447 " array info. %d\n", err);
6454 * Commands querying/configuring an existing array:
6456 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
6457 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
6458 if ((!mddev->raid_disks && !mddev->external)
6459 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
6460 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
6461 && cmd != GET_BITMAP_FILE) {
6467 * Commands even a read-only array can execute:
6470 case GET_BITMAP_FILE:
6471 err = get_bitmap_file(mddev, argp);
6474 case RESTART_ARRAY_RW:
6475 err = restart_array(mddev);
6479 err = do_md_stop(mddev, 0, bdev);
6483 err = md_set_readonly(mddev, bdev);
6487 if (get_user(ro, (int __user *)(arg))) {
6493 /* if the bdev is going readonly the value of mddev->ro
6494 * does not matter, no writes are coming
6499 /* are we are already prepared for writes? */
6503 /* transitioning to readauto need only happen for
6504 * arrays that call md_write_start
6507 err = restart_array(mddev);
6510 set_disk_ro(mddev->gendisk, 0);
6517 * The remaining ioctls are changing the state of the
6518 * superblock, so we do not allow them on read-only arrays.
6519 * However non-MD ioctls (e.g. get-size) will still come through
6520 * here and hit the 'default' below, so only disallow
6521 * 'md' ioctls, and switch to rw mode if started auto-readonly.
6523 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
6524 if (mddev->ro == 2) {
6526 sysfs_notify_dirent_safe(mddev->sysfs_state);
6527 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6528 md_wakeup_thread(mddev->thread);
6538 mdu_disk_info_t info;
6539 if (copy_from_user(&info, argp, sizeof(info)))
6542 err = add_new_disk(mddev, &info);
6546 case HOT_REMOVE_DISK:
6547 err = hot_remove_disk(mddev, new_decode_dev(arg));
6551 err = hot_add_disk(mddev, new_decode_dev(arg));
6555 err = do_md_run(mddev);
6558 case SET_BITMAP_FILE:
6559 err = set_bitmap_file(mddev, (int)arg);
6569 if (mddev->hold_active == UNTIL_IOCTL &&
6571 mddev->hold_active = 0;
6572 mddev_unlock(mddev);
6581 #ifdef CONFIG_COMPAT
6582 static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
6583 unsigned int cmd, unsigned long arg)
6586 case HOT_REMOVE_DISK:
6588 case SET_DISK_FAULTY:
6589 case SET_BITMAP_FILE:
6590 /* These take in integer arg, do not convert */
6593 arg = (unsigned long)compat_ptr(arg);
6597 return md_ioctl(bdev, mode, cmd, arg);
6599 #endif /* CONFIG_COMPAT */
6601 static int md_open(struct block_device *bdev, fmode_t mode)
6604 * Succeed if we can lock the mddev, which confirms that
6605 * it isn't being stopped right now.
6607 struct mddev *mddev = mddev_find(bdev->bd_dev);
6613 if (mddev->gendisk != bdev->bd_disk) {
6614 /* we are racing with mddev_put which is discarding this
6618 /* Wait until bdev->bd_disk is definitely gone */
6619 flush_workqueue(md_misc_wq);
6620 /* Then retry the open from the top */
6621 return -ERESTARTSYS;
6623 BUG_ON(mddev != bdev->bd_disk->private_data);
6625 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
6629 atomic_inc(&mddev->openers);
6630 mutex_unlock(&mddev->open_mutex);
6632 check_disk_change(bdev);
6637 static int md_release(struct gendisk *disk, fmode_t mode)
6639 struct mddev *mddev = disk->private_data;
6642 atomic_dec(&mddev->openers);
6648 static int md_media_changed(struct gendisk *disk)
6650 struct mddev *mddev = disk->private_data;
6652 return mddev->changed;
6655 static int md_revalidate(struct gendisk *disk)
6657 struct mddev *mddev = disk->private_data;
6662 static const struct block_device_operations md_fops =
6664 .owner = THIS_MODULE,
6666 .release = md_release,
6668 #ifdef CONFIG_COMPAT
6669 .compat_ioctl = md_compat_ioctl,
6671 .getgeo = md_getgeo,
6672 .media_changed = md_media_changed,
6673 .revalidate_disk= md_revalidate,
6676 static int md_thread(void * arg)
6678 struct md_thread *thread = arg;
6681 * md_thread is a 'system-thread', it's priority should be very
6682 * high. We avoid resource deadlocks individually in each
6683 * raid personality. (RAID5 does preallocation) We also use RR and
6684 * the very same RT priority as kswapd, thus we will never get
6685 * into a priority inversion deadlock.
6687 * we definitely have to have equal or higher priority than
6688 * bdflush, otherwise bdflush will deadlock if there are too
6689 * many dirty RAID5 blocks.
6692 allow_signal(SIGKILL);
6693 while (!kthread_should_stop()) {
6695 /* We need to wait INTERRUPTIBLE so that
6696 * we don't add to the load-average.
6697 * That means we need to be sure no signals are
6700 if (signal_pending(current))
6701 flush_signals(current);
6703 wait_event_interruptible_timeout
6705 test_bit(THREAD_WAKEUP, &thread->flags)
6706 || kthread_should_stop(),
6709 clear_bit(THREAD_WAKEUP, &thread->flags);
6710 if (!kthread_should_stop())
6711 thread->run(thread);
6717 void md_wakeup_thread(struct md_thread *thread)
6720 pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm);
6721 set_bit(THREAD_WAKEUP, &thread->flags);
6722 wake_up(&thread->wqueue);
6726 struct md_thread *md_register_thread(void (*run) (struct md_thread *),
6727 struct mddev *mddev, const char *name)
6729 struct md_thread *thread;
6731 thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL);
6735 init_waitqueue_head(&thread->wqueue);
6738 thread->mddev = mddev;
6739 thread->timeout = MAX_SCHEDULE_TIMEOUT;
6740 thread->tsk = kthread_run(md_thread, thread,
6742 mdname(thread->mddev),
6744 if (IS_ERR(thread->tsk)) {
6751 void md_unregister_thread(struct md_thread **threadp)
6753 struct md_thread *thread = *threadp;
6756 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
6757 /* Locking ensures that mddev_unlock does not wake_up a
6758 * non-existent thread
6760 spin_lock(&pers_lock);
6762 spin_unlock(&pers_lock);
6764 kthread_stop(thread->tsk);
6768 void md_error(struct mddev *mddev, struct md_rdev *rdev)
6775 if (!rdev || test_bit(Faulty, &rdev->flags))
6778 if (!mddev->pers || !mddev->pers->error_handler)
6780 mddev->pers->error_handler(mddev,rdev);
6781 if (mddev->degraded)
6782 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
6783 sysfs_notify_dirent_safe(rdev->sysfs_state);
6784 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6785 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6786 md_wakeup_thread(mddev->thread);
6787 if (mddev->event_work.func)
6788 queue_work(md_misc_wq, &mddev->event_work);
6789 md_new_event_inintr(mddev);
6792 /* seq_file implementation /proc/mdstat */
6794 static void status_unused(struct seq_file *seq)
6797 struct md_rdev *rdev;
6799 seq_printf(seq, "unused devices: ");
6801 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
6802 char b[BDEVNAME_SIZE];
6804 seq_printf(seq, "%s ",
6805 bdevname(rdev->bdev,b));
6808 seq_printf(seq, "<none>");
6810 seq_printf(seq, "\n");
6814 static void status_resync(struct seq_file *seq, struct mddev * mddev)
6816 sector_t max_sectors, resync, res;
6817 unsigned long dt, db;
6820 unsigned int per_milli;
6822 if (mddev->curr_resync <= 3)
6825 resync = mddev->curr_resync
6826 - atomic_read(&mddev->recovery_active);
6828 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
6829 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
6830 max_sectors = mddev->resync_max_sectors;
6832 max_sectors = mddev->dev_sectors;
6835 * Should not happen.
6841 /* Pick 'scale' such that (resync>>scale)*1000 will fit
6842 * in a sector_t, and (max_sectors>>scale) will fit in a
6843 * u32, as those are the requirements for sector_div.
6844 * Thus 'scale' must be at least 10
6847 if (sizeof(sector_t) > sizeof(unsigned long)) {
6848 while ( max_sectors/2 > (1ULL<<(scale+32)))
6851 res = (resync>>scale)*1000;
6852 sector_div(res, (u32)((max_sectors>>scale)+1));
6856 int i, x = per_milli/50, y = 20-x;
6857 seq_printf(seq, "[");
6858 for (i = 0; i < x; i++)
6859 seq_printf(seq, "=");
6860 seq_printf(seq, ">");
6861 for (i = 0; i < y; i++)
6862 seq_printf(seq, ".");
6863 seq_printf(seq, "] ");
6865 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
6866 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
6868 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
6870 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
6871 "resync" : "recovery"))),
6872 per_milli/10, per_milli % 10,
6873 (unsigned long long) resync/2,
6874 (unsigned long long) max_sectors/2);
6877 * dt: time from mark until now
6878 * db: blocks written from mark until now
6879 * rt: remaining time
6881 * rt is a sector_t, so could be 32bit or 64bit.
6882 * So we divide before multiply in case it is 32bit and close
6884 * We scale the divisor (db) by 32 to avoid losing precision
6885 * near the end of resync when the number of remaining sectors
6887 * We then divide rt by 32 after multiplying by db to compensate.
6888 * The '+1' avoids division by zero if db is very small.
6890 dt = ((jiffies - mddev->resync_mark) / HZ);
6892 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
6893 - mddev->resync_mark_cnt;
6895 rt = max_sectors - resync; /* number of remaining sectors */
6896 sector_div(rt, db/32+1);
6900 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
6901 ((unsigned long)rt % 60)/6);
6903 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
6906 static void *md_seq_start(struct seq_file *seq, loff_t *pos)
6908 struct list_head *tmp;
6910 struct mddev *mddev;
6918 spin_lock(&all_mddevs_lock);
6919 list_for_each(tmp,&all_mddevs)
6921 mddev = list_entry(tmp, struct mddev, all_mddevs);
6923 spin_unlock(&all_mddevs_lock);
6926 spin_unlock(&all_mddevs_lock);
6928 return (void*)2;/* tail */
6932 static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
6934 struct list_head *tmp;
6935 struct mddev *next_mddev, *mddev = v;
6941 spin_lock(&all_mddevs_lock);
6943 tmp = all_mddevs.next;
6945 tmp = mddev->all_mddevs.next;
6946 if (tmp != &all_mddevs)
6947 next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs));
6949 next_mddev = (void*)2;
6952 spin_unlock(&all_mddevs_lock);
6960 static void md_seq_stop(struct seq_file *seq, void *v)
6962 struct mddev *mddev = v;
6964 if (mddev && v != (void*)1 && v != (void*)2)
6968 static int md_seq_show(struct seq_file *seq, void *v)
6970 struct mddev *mddev = v;
6972 struct md_rdev *rdev;
6974 if (v == (void*)1) {
6975 struct md_personality *pers;
6976 seq_printf(seq, "Personalities : ");
6977 spin_lock(&pers_lock);
6978 list_for_each_entry(pers, &pers_list, list)
6979 seq_printf(seq, "[%s] ", pers->name);
6981 spin_unlock(&pers_lock);
6982 seq_printf(seq, "\n");
6983 seq->poll_event = atomic_read(&md_event_count);
6986 if (v == (void*)2) {
6991 if (mddev_lock(mddev) < 0)
6994 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
6995 seq_printf(seq, "%s : %sactive", mdname(mddev),
6996 mddev->pers ? "" : "in");
6999 seq_printf(seq, " (read-only)");
7001 seq_printf(seq, " (auto-read-only)");
7002 seq_printf(seq, " %s", mddev->pers->name);
7006 rdev_for_each(rdev, mddev) {
7007 char b[BDEVNAME_SIZE];
7008 seq_printf(seq, " %s[%d]",
7009 bdevname(rdev->bdev,b), rdev->desc_nr);
7010 if (test_bit(WriteMostly, &rdev->flags))
7011 seq_printf(seq, "(W)");
7012 if (test_bit(Faulty, &rdev->flags)) {
7013 seq_printf(seq, "(F)");
7016 if (rdev->raid_disk < 0)
7017 seq_printf(seq, "(S)"); /* spare */
7018 if (test_bit(Replacement, &rdev->flags))
7019 seq_printf(seq, "(R)");
7020 sectors += rdev->sectors;
7023 if (!list_empty(&mddev->disks)) {
7025 seq_printf(seq, "\n %llu blocks",
7026 (unsigned long long)
7027 mddev->array_sectors / 2);
7029 seq_printf(seq, "\n %llu blocks",
7030 (unsigned long long)sectors / 2);
7032 if (mddev->persistent) {
7033 if (mddev->major_version != 0 ||
7034 mddev->minor_version != 90) {
7035 seq_printf(seq," super %d.%d",
7036 mddev->major_version,
7037 mddev->minor_version);
7039 } else if (mddev->external)
7040 seq_printf(seq, " super external:%s",
7041 mddev->metadata_type);
7043 seq_printf(seq, " super non-persistent");
7046 mddev->pers->status(seq, mddev);
7047 seq_printf(seq, "\n ");
7048 if (mddev->pers->sync_request) {
7049 if (mddev->curr_resync > 2) {
7050 status_resync(seq, mddev);
7051 seq_printf(seq, "\n ");
7052 } else if (mddev->curr_resync >= 1)
7053 seq_printf(seq, "\tresync=DELAYED\n ");
7054 else if (mddev->recovery_cp < MaxSector)
7055 seq_printf(seq, "\tresync=PENDING\n ");
7058 seq_printf(seq, "\n ");
7060 bitmap_status(seq, mddev->bitmap);
7062 seq_printf(seq, "\n");
7064 mddev_unlock(mddev);
7069 static const struct seq_operations md_seq_ops = {
7070 .start = md_seq_start,
7071 .next = md_seq_next,
7072 .stop = md_seq_stop,
7073 .show = md_seq_show,
7076 static int md_seq_open(struct inode *inode, struct file *file)
7078 struct seq_file *seq;
7081 error = seq_open(file, &md_seq_ops);
7085 seq = file->private_data;
7086 seq->poll_event = atomic_read(&md_event_count);
7090 static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
7092 struct seq_file *seq = filp->private_data;
7095 poll_wait(filp, &md_event_waiters, wait);
7097 /* always allow read */
7098 mask = POLLIN | POLLRDNORM;
7100 if (seq->poll_event != atomic_read(&md_event_count))
7101 mask |= POLLERR | POLLPRI;
7105 static const struct file_operations md_seq_fops = {
7106 .owner = THIS_MODULE,
7107 .open = md_seq_open,
7109 .llseek = seq_lseek,
7110 .release = seq_release_private,
7111 .poll = mdstat_poll,
7114 int register_md_personality(struct md_personality *p)
7116 spin_lock(&pers_lock);
7117 list_add_tail(&p->list, &pers_list);
7118 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
7119 spin_unlock(&pers_lock);
7123 int unregister_md_personality(struct md_personality *p)
7125 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
7126 spin_lock(&pers_lock);
7127 list_del_init(&p->list);
7128 spin_unlock(&pers_lock);
7132 static int is_mddev_idle(struct mddev *mddev, int init)
7134 struct md_rdev * rdev;
7140 rdev_for_each_rcu(rdev, mddev) {
7141 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
7142 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
7143 (int)part_stat_read(&disk->part0, sectors[1]) -
7144 atomic_read(&disk->sync_io);
7145 /* sync IO will cause sync_io to increase before the disk_stats
7146 * as sync_io is counted when a request starts, and
7147 * disk_stats is counted when it completes.
7148 * So resync activity will cause curr_events to be smaller than
7149 * when there was no such activity.
7150 * non-sync IO will cause disk_stat to increase without
7151 * increasing sync_io so curr_events will (eventually)
7152 * be larger than it was before. Once it becomes
7153 * substantially larger, the test below will cause
7154 * the array to appear non-idle, and resync will slow
7156 * If there is a lot of outstanding resync activity when
7157 * we set last_event to curr_events, then all that activity
7158 * completing might cause the array to appear non-idle
7159 * and resync will be slowed down even though there might
7160 * not have been non-resync activity. This will only
7161 * happen once though. 'last_events' will soon reflect
7162 * the state where there is little or no outstanding
7163 * resync requests, and further resync activity will
7164 * always make curr_events less than last_events.
7167 if (init || curr_events - rdev->last_events > 64) {
7168 rdev->last_events = curr_events;
7176 void md_done_sync(struct mddev *mddev, int blocks, int ok)
7178 /* another "blocks" (512byte) blocks have been synced */
7179 atomic_sub(blocks, &mddev->recovery_active);
7180 wake_up(&mddev->recovery_wait);
7182 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7183 set_bit(MD_RECOVERY_ERROR, &mddev->recovery);
7184 md_wakeup_thread(mddev->thread);
7185 // stop recovery, signal do_sync ....
7190 /* md_write_start(mddev, bi)
7191 * If we need to update some array metadata (e.g. 'active' flag
7192 * in superblock) before writing, schedule a superblock update
7193 * and wait for it to complete.
7195 void md_write_start(struct mddev *mddev, struct bio *bi)
7198 if (bio_data_dir(bi) != WRITE)
7201 BUG_ON(mddev->ro == 1);
7202 if (mddev->ro == 2) {
7203 /* need to switch to read/write */
7205 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7206 md_wakeup_thread(mddev->thread);
7207 md_wakeup_thread(mddev->sync_thread);
7210 atomic_inc(&mddev->writes_pending);
7211 if (mddev->safemode == 1)
7212 mddev->safemode = 0;
7213 if (mddev->in_sync) {
7214 spin_lock_irq(&mddev->write_lock);
7215 if (mddev->in_sync) {
7217 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
7218 set_bit(MD_CHANGE_PENDING, &mddev->flags);
7219 md_wakeup_thread(mddev->thread);
7222 spin_unlock_irq(&mddev->write_lock);
7225 sysfs_notify_dirent_safe(mddev->sysfs_state);
7226 wait_event(mddev->sb_wait,
7227 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
7230 void md_write_end(struct mddev *mddev)
7232 if (atomic_dec_and_test(&mddev->writes_pending)) {
7233 if (mddev->safemode == 2)
7234 md_wakeup_thread(mddev->thread);
7235 else if (mddev->safemode_delay)
7236 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
7240 /* md_allow_write(mddev)
7241 * Calling this ensures that the array is marked 'active' so that writes
7242 * may proceed without blocking. It is important to call this before
7243 * attempting a GFP_KERNEL allocation while holding the mddev lock.
7244 * Must be called with mddev_lock held.
7246 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
7247 * is dropped, so return -EAGAIN after notifying userspace.
7249 int md_allow_write(struct mddev *mddev)
7255 if (!mddev->pers->sync_request)
7258 spin_lock_irq(&mddev->write_lock);
7259 if (mddev->in_sync) {
7261 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
7262 set_bit(MD_CHANGE_PENDING, &mddev->flags);
7263 if (mddev->safemode_delay &&
7264 mddev->safemode == 0)
7265 mddev->safemode = 1;
7266 spin_unlock_irq(&mddev->write_lock);
7267 md_update_sb(mddev, 0);
7268 sysfs_notify_dirent_safe(mddev->sysfs_state);
7270 spin_unlock_irq(&mddev->write_lock);
7272 if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
7277 EXPORT_SYMBOL_GPL(md_allow_write);
7279 #define SYNC_MARKS 10
7280 #define SYNC_MARK_STEP (3*HZ)
7281 #define UPDATE_FREQUENCY (5*60*HZ)
7282 void md_do_sync(struct md_thread *thread)
7284 struct mddev *mddev = thread->mddev;
7285 struct mddev *mddev2;
7286 unsigned int currspeed = 0,
7288 sector_t max_sectors,j, io_sectors;
7289 unsigned long mark[SYNC_MARKS];
7290 unsigned long update_time;
7291 sector_t mark_cnt[SYNC_MARKS];
7293 struct list_head *tmp;
7294 sector_t last_check;
7296 struct md_rdev *rdev;
7298 struct blk_plug plug;
7300 /* just incase thread restarts... */
7301 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7303 if (mddev->ro) /* never try to sync a read-only array */
7306 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
7307 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
7308 desc = "data-check";
7309 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7310 desc = "requested-resync";
7313 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
7318 /* we overload curr_resync somewhat here.
7319 * 0 == not engaged in resync at all
7320 * 2 == checking that there is no conflict with another sync
7321 * 1 == like 2, but have yielded to allow conflicting resync to
7323 * other == active in resync - this many blocks
7325 * Before starting a resync we must have set curr_resync to
7326 * 2, and then checked that every "conflicting" array has curr_resync
7327 * less than ours. When we find one that is the same or higher
7328 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
7329 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
7330 * This will mean we have to start checking from the beginning again.
7335 mddev->curr_resync = 2;
7338 if (kthread_should_stop())
7339 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7341 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7343 for_each_mddev(mddev2, tmp) {
7344 if (mddev2 == mddev)
7346 if (!mddev->parallel_resync
7347 && mddev2->curr_resync
7348 && match_mddev_units(mddev, mddev2)) {
7350 if (mddev < mddev2 && mddev->curr_resync == 2) {
7351 /* arbitrarily yield */
7352 mddev->curr_resync = 1;
7353 wake_up(&resync_wait);
7355 if (mddev > mddev2 && mddev->curr_resync == 1)
7356 /* no need to wait here, we can wait the next
7357 * time 'round when curr_resync == 2
7360 /* We need to wait 'interruptible' so as not to
7361 * contribute to the load average, and not to
7362 * be caught by 'softlockup'
7364 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
7365 if (!kthread_should_stop() &&
7366 mddev2->curr_resync >= mddev->curr_resync) {
7367 printk(KERN_INFO "md: delaying %s of %s"
7368 " until %s has finished (they"
7369 " share one or more physical units)\n",
7370 desc, mdname(mddev), mdname(mddev2));
7372 if (signal_pending(current))
7373 flush_signals(current);
7375 finish_wait(&resync_wait, &wq);
7378 finish_wait(&resync_wait, &wq);
7381 } while (mddev->curr_resync < 2);
7384 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
7385 /* resync follows the size requested by the personality,
7386 * which defaults to physical size, but can be virtual size
7388 max_sectors = mddev->resync_max_sectors;
7389 atomic64_set(&mddev->resync_mismatches, 0);
7390 /* we don't use the checkpoint if there's a bitmap */
7391 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7392 j = mddev->resync_min;
7393 else if (!mddev->bitmap)
7394 j = mddev->recovery_cp;
7396 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
7397 max_sectors = mddev->resync_max_sectors;
7399 /* recovery follows the physical size of devices */
7400 max_sectors = mddev->dev_sectors;
7403 rdev_for_each_rcu(rdev, mddev)
7404 if (rdev->raid_disk >= 0 &&
7405 !test_bit(Faulty, &rdev->flags) &&
7406 !test_bit(In_sync, &rdev->flags) &&
7407 rdev->recovery_offset < j)
7408 j = rdev->recovery_offset;
7412 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
7413 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
7414 " %d KB/sec/disk.\n", speed_min(mddev));
7415 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
7416 "(but not more than %d KB/sec) for %s.\n",
7417 speed_max(mddev), desc);
7419 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
7422 for (m = 0; m < SYNC_MARKS; m++) {
7424 mark_cnt[m] = io_sectors;
7427 mddev->resync_mark = mark[last_mark];
7428 mddev->resync_mark_cnt = mark_cnt[last_mark];
7431 * Tune reconstruction:
7433 window = 32*(PAGE_SIZE/512);
7434 printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n",
7435 window/2, (unsigned long long)max_sectors/2);
7437 atomic_set(&mddev->recovery_active, 0);
7442 "md: resuming %s of %s from checkpoint.\n",
7443 desc, mdname(mddev));
7444 mddev->curr_resync = j;
7446 mddev->curr_resync = 3; /* no longer delayed */
7447 mddev->curr_resync_completed = j;
7448 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
7449 md_new_event(mddev);
7450 update_time = jiffies;
7452 blk_start_plug(&plug);
7453 while (j < max_sectors) {
7458 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7459 ((mddev->curr_resync > mddev->curr_resync_completed &&
7460 (mddev->curr_resync - mddev->curr_resync_completed)
7461 > (max_sectors >> 4)) ||
7462 time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) ||
7463 (j - mddev->curr_resync_completed)*2
7464 >= mddev->resync_max - mddev->curr_resync_completed
7466 /* time to update curr_resync_completed */
7467 wait_event(mddev->recovery_wait,
7468 atomic_read(&mddev->recovery_active) == 0);
7469 mddev->curr_resync_completed = j;
7470 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
7471 j > mddev->recovery_cp)
7472 mddev->recovery_cp = j;
7473 update_time = jiffies;
7474 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
7475 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
7478 while (j >= mddev->resync_max && !kthread_should_stop()) {
7479 /* As this condition is controlled by user-space,
7480 * we can block indefinitely, so use '_interruptible'
7481 * to avoid triggering warnings.
7483 flush_signals(current); /* just in case */
7484 wait_event_interruptible(mddev->recovery_wait,
7485 mddev->resync_max > j
7486 || kthread_should_stop());
7489 if (kthread_should_stop())
7492 sectors = mddev->pers->sync_request(mddev, j, &skipped,
7493 currspeed < speed_min(mddev));
7495 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7499 if (!skipped) { /* actual IO requested */
7500 io_sectors += sectors;
7501 atomic_add(sectors, &mddev->recovery_active);
7504 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7509 mddev->curr_resync = j;
7510 mddev->curr_mark_cnt = io_sectors;
7511 if (last_check == 0)
7512 /* this is the earliest that rebuild will be
7513 * visible in /proc/mdstat
7515 md_new_event(mddev);
7517 if (last_check + window > io_sectors || j == max_sectors)
7520 last_check = io_sectors;
7522 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
7524 int next = (last_mark+1) % SYNC_MARKS;
7526 mddev->resync_mark = mark[next];
7527 mddev->resync_mark_cnt = mark_cnt[next];
7528 mark[next] = jiffies;
7529 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
7534 if (kthread_should_stop())
7539 * this loop exits only if either when we are slower than
7540 * the 'hard' speed limit, or the system was IO-idle for
7542 * the system might be non-idle CPU-wise, but we only care
7543 * about not overloading the IO subsystem. (things like an
7544 * e2fsck being done on the RAID array should execute fast)
7548 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
7549 /((jiffies-mddev->resync_mark)/HZ +1) +1;
7551 if (currspeed > speed_min(mddev)) {
7552 if ((currspeed > speed_max(mddev)) ||
7553 !is_mddev_idle(mddev, 0)) {
7559 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
7561 * this also signals 'finished resyncing' to md_stop
7564 blk_finish_plug(&plug);
7565 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
7567 /* tell personality that we are finished */
7568 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
7570 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
7571 mddev->curr_resync > 2) {
7572 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
7573 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7574 if (mddev->curr_resync >= mddev->recovery_cp) {
7576 "md: checkpointing %s of %s.\n",
7577 desc, mdname(mddev));
7578 if (test_bit(MD_RECOVERY_ERROR,
7580 mddev->recovery_cp =
7581 mddev->curr_resync_completed;
7583 mddev->recovery_cp =
7587 mddev->recovery_cp = MaxSector;
7589 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7590 mddev->curr_resync = MaxSector;
7592 rdev_for_each_rcu(rdev, mddev)
7593 if (rdev->raid_disk >= 0 &&
7594 mddev->delta_disks >= 0 &&
7595 !test_bit(Faulty, &rdev->flags) &&
7596 !test_bit(In_sync, &rdev->flags) &&
7597 rdev->recovery_offset < mddev->curr_resync)
7598 rdev->recovery_offset = mddev->curr_resync;
7603 set_bit(MD_CHANGE_DEVS, &mddev->flags);
7605 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7606 /* We completed so min/max setting can be forgotten if used. */
7607 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7608 mddev->resync_min = 0;
7609 mddev->resync_max = MaxSector;
7610 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7611 mddev->resync_min = mddev->curr_resync_completed;
7612 mddev->curr_resync = 0;
7613 wake_up(&resync_wait);
7614 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
7615 md_wakeup_thread(mddev->thread);
7620 * got a signal, exit.
7623 "md: md_do_sync() got signal ... exiting\n");
7624 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7628 EXPORT_SYMBOL_GPL(md_do_sync);
7630 static int remove_and_add_spares(struct mddev *mddev)
7632 struct md_rdev *rdev;
7636 rdev_for_each(rdev, mddev)
7637 if (rdev->raid_disk >= 0 &&
7638 !test_bit(Blocked, &rdev->flags) &&
7639 (test_bit(Faulty, &rdev->flags) ||
7640 ! test_bit(In_sync, &rdev->flags)) &&
7641 atomic_read(&rdev->nr_pending)==0) {
7642 if (mddev->pers->hot_remove_disk(
7643 mddev, rdev) == 0) {
7644 sysfs_unlink_rdev(mddev, rdev);
7645 rdev->raid_disk = -1;
7650 sysfs_notify(&mddev->kobj, NULL,
7654 rdev_for_each(rdev, mddev) {
7655 if (rdev->raid_disk >= 0 &&
7656 !test_bit(In_sync, &rdev->flags) &&
7657 !test_bit(Faulty, &rdev->flags))
7659 if (rdev->raid_disk < 0
7660 && !test_bit(Faulty, &rdev->flags)) {
7661 rdev->recovery_offset = 0;
7663 hot_add_disk(mddev, rdev) == 0) {
7664 if (sysfs_link_rdev(mddev, rdev))
7665 /* failure here is OK */;
7667 md_new_event(mddev);
7668 set_bit(MD_CHANGE_DEVS, &mddev->flags);
7673 set_bit(MD_CHANGE_DEVS, &mddev->flags);
7677 static void reap_sync_thread(struct mddev *mddev)
7679 struct md_rdev *rdev;
7681 /* resync has finished, collect result */
7682 md_unregister_thread(&mddev->sync_thread);
7683 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
7684 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
7686 /* activate any spares */
7687 if (mddev->pers->spare_active(mddev)) {
7688 sysfs_notify(&mddev->kobj, NULL,
7690 set_bit(MD_CHANGE_DEVS, &mddev->flags);
7693 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7694 mddev->pers->finish_reshape)
7695 mddev->pers->finish_reshape(mddev);
7697 /* If array is no-longer degraded, then any saved_raid_disk
7698 * information must be scrapped. Also if any device is now
7699 * In_sync we must scrape the saved_raid_disk for that device
7700 * do the superblock for an incrementally recovered device
7703 rdev_for_each(rdev, mddev)
7704 if (!mddev->degraded ||
7705 test_bit(In_sync, &rdev->flags))
7706 rdev->saved_raid_disk = -1;
7708 md_update_sb(mddev, 1);
7709 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7710 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7711 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7712 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7713 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
7714 /* flag recovery needed just to double check */
7715 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7716 sysfs_notify_dirent_safe(mddev->sysfs_action);
7717 md_new_event(mddev);
7718 if (mddev->event_work.func)
7719 queue_work(md_misc_wq, &mddev->event_work);
7723 * This routine is regularly called by all per-raid-array threads to
7724 * deal with generic issues like resync and super-block update.
7725 * Raid personalities that don't have a thread (linear/raid0) do not
7726 * need this as they never do any recovery or update the superblock.
7728 * It does not do any resync itself, but rather "forks" off other threads
7729 * to do that as needed.
7730 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
7731 * "->recovery" and create a thread at ->sync_thread.
7732 * When the thread finishes it sets MD_RECOVERY_DONE
7733 * and wakeups up this thread which will reap the thread and finish up.
7734 * This thread also removes any faulty devices (with nr_pending == 0).
7736 * The overall approach is:
7737 * 1/ if the superblock needs updating, update it.
7738 * 2/ If a recovery thread is running, don't do anything else.
7739 * 3/ If recovery has finished, clean up, possibly marking spares active.
7740 * 4/ If there are any faulty devices, remove them.
7741 * 5/ If array is degraded, try to add spares devices
7742 * 6/ If array has spares or is not in-sync, start a resync thread.
7744 void md_check_recovery(struct mddev *mddev)
7746 if (mddev->suspended)
7750 bitmap_daemon_work(mddev);
7752 if (signal_pending(current)) {
7753 if (mddev->pers->sync_request && !mddev->external) {
7754 printk(KERN_INFO "md: %s in immediate safe mode\n",
7756 mddev->safemode = 2;
7758 flush_signals(current);
7761 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
7764 (mddev->flags & ~ (1<<MD_CHANGE_PENDING)) ||
7765 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
7766 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
7767 (mddev->external == 0 && mddev->safemode == 1) ||
7768 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
7769 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
7773 if (mddev_trylock(mddev)) {
7777 /* Only thing we do on a ro array is remove
7780 struct md_rdev *rdev;
7781 rdev_for_each(rdev, mddev)
7782 if (rdev->raid_disk >= 0 &&
7783 !test_bit(Blocked, &rdev->flags) &&
7784 test_bit(Faulty, &rdev->flags) &&
7785 atomic_read(&rdev->nr_pending)==0) {
7786 if (mddev->pers->hot_remove_disk(
7787 mddev, rdev) == 0) {
7788 sysfs_unlink_rdev(mddev, rdev);
7789 rdev->raid_disk = -1;
7792 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7796 if (!mddev->external) {
7798 spin_lock_irq(&mddev->write_lock);
7799 if (mddev->safemode &&
7800 !atomic_read(&mddev->writes_pending) &&
7802 mddev->recovery_cp == MaxSector) {
7805 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
7807 if (mddev->safemode == 1)
7808 mddev->safemode = 0;
7809 spin_unlock_irq(&mddev->write_lock);
7811 sysfs_notify_dirent_safe(mddev->sysfs_state);
7815 md_update_sb(mddev, 0);
7817 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
7818 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
7819 /* resync/recovery still happening */
7820 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7823 if (mddev->sync_thread) {
7824 reap_sync_thread(mddev);
7827 /* Set RUNNING before clearing NEEDED to avoid
7828 * any transients in the value of "sync_action".
7830 mddev->curr_resync_completed = 0;
7831 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7832 /* Clear some bits that don't mean anything, but
7835 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
7836 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
7838 if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
7839 test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
7841 /* no recovery is running.
7842 * remove any failed drives, then
7843 * add spares if possible.
7844 * Spares are also removed and re-added, to allow
7845 * the personality to fail the re-add.
7848 if (mddev->reshape_position != MaxSector) {
7849 if (mddev->pers->check_reshape == NULL ||
7850 mddev->pers->check_reshape(mddev) != 0)
7851 /* Cannot proceed */
7853 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7854 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
7855 } else if ((spares = remove_and_add_spares(mddev))) {
7856 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7857 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
7858 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7859 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
7860 } else if (mddev->recovery_cp < MaxSector) {
7861 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7862 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
7863 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
7864 /* nothing to be done ... */
7867 if (mddev->pers->sync_request) {
7869 /* We are adding a device or devices to an array
7870 * which has the bitmap stored on all devices.
7871 * So make sure all bitmap pages get written
7873 bitmap_write_all(mddev->bitmap);
7875 mddev->sync_thread = md_register_thread(md_do_sync,
7878 if (!mddev->sync_thread) {
7879 printk(KERN_ERR "%s: could not start resync"
7882 /* leave the spares where they are, it shouldn't hurt */
7883 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7884 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7885 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7886 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7887 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
7889 md_wakeup_thread(mddev->sync_thread);
7890 sysfs_notify_dirent_safe(mddev->sysfs_action);
7891 md_new_event(mddev);
7894 if (!mddev->sync_thread) {
7895 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7896 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
7898 if (mddev->sysfs_action)
7899 sysfs_notify_dirent_safe(mddev->sysfs_action);
7901 mddev_unlock(mddev);
7905 void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev)
7907 sysfs_notify_dirent_safe(rdev->sysfs_state);
7908 wait_event_timeout(rdev->blocked_wait,
7909 !test_bit(Blocked, &rdev->flags) &&
7910 !test_bit(BlockedBadBlocks, &rdev->flags),
7911 msecs_to_jiffies(5000));
7912 rdev_dec_pending(rdev, mddev);
7914 EXPORT_SYMBOL(md_wait_for_blocked_rdev);
7916 void md_finish_reshape(struct mddev *mddev)
7918 /* called be personality module when reshape completes. */
7919 struct md_rdev *rdev;
7921 rdev_for_each(rdev, mddev) {
7922 if (rdev->data_offset > rdev->new_data_offset)
7923 rdev->sectors += rdev->data_offset - rdev->new_data_offset;
7925 rdev->sectors -= rdev->new_data_offset - rdev->data_offset;
7926 rdev->data_offset = rdev->new_data_offset;
7929 EXPORT_SYMBOL(md_finish_reshape);
7931 /* Bad block management.
7932 * We can record which blocks on each device are 'bad' and so just
7933 * fail those blocks, or that stripe, rather than the whole device.
7934 * Entries in the bad-block table are 64bits wide. This comprises:
7935 * Length of bad-range, in sectors: 0-511 for lengths 1-512
7936 * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes)
7937 * A 'shift' can be set so that larger blocks are tracked and
7938 * consequently larger devices can be covered.
7939 * 'Acknowledged' flag - 1 bit. - the most significant bit.
7941 * Locking of the bad-block table uses a seqlock so md_is_badblock
7942 * might need to retry if it is very unlucky.
7943 * We will sometimes want to check for bad blocks in a bi_end_io function,
7944 * so we use the write_seqlock_irq variant.
7946 * When looking for a bad block we specify a range and want to
7947 * know if any block in the range is bad. So we binary-search
7948 * to the last range that starts at-or-before the given endpoint,
7949 * (or "before the sector after the target range")
7950 * then see if it ends after the given start.
7952 * 0 if there are no known bad blocks in the range
7953 * 1 if there are known bad block which are all acknowledged
7954 * -1 if there are bad blocks which have not yet been acknowledged in metadata.
7955 * plus the start/length of the first bad section we overlap.
7957 int md_is_badblock(struct badblocks *bb, sector_t s, int sectors,
7958 sector_t *first_bad, int *bad_sectors)
7964 sector_t target = s + sectors;
7967 if (bb->shift > 0) {
7968 /* round the start down, and the end up */
7970 target += (1<<bb->shift) - 1;
7971 target >>= bb->shift;
7972 sectors = target - s;
7974 /* 'target' is now the first block after the bad range */
7977 seq = read_seqbegin(&bb->lock);
7982 /* Binary search between lo and hi for 'target'
7983 * i.e. for the last range that starts before 'target'
7985 /* INVARIANT: ranges before 'lo' and at-or-after 'hi'
7986 * are known not to be the last range before target.
7987 * VARIANT: hi-lo is the number of possible
7988 * ranges, and decreases until it reaches 1
7990 while (hi - lo > 1) {
7991 int mid = (lo + hi) / 2;
7992 sector_t a = BB_OFFSET(p[mid]);
7994 /* This could still be the one, earlier ranges
7998 /* This and later ranges are definitely out. */
8001 /* 'lo' might be the last that started before target, but 'hi' isn't */
8003 /* need to check all range that end after 's' to see if
8004 * any are unacknowledged.
8007 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
8008 if (BB_OFFSET(p[lo]) < target) {
8009 /* starts before the end, and finishes after
8010 * the start, so they must overlap
8012 if (rv != -1 && BB_ACK(p[lo]))
8016 *first_bad = BB_OFFSET(p[lo]);
8017 *bad_sectors = BB_LEN(p[lo]);
8023 if (read_seqretry(&bb->lock, seq))
8028 EXPORT_SYMBOL_GPL(md_is_badblock);
8031 * Add a range of bad blocks to the table.
8032 * This might extend the table, or might contract it
8033 * if two adjacent ranges can be merged.
8034 * We binary-search to find the 'insertion' point, then
8035 * decide how best to handle it.
8037 static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
8045 /* badblocks are disabled */
8049 /* round the start down, and the end up */
8050 sector_t next = s + sectors;
8052 next += (1<<bb->shift) - 1;
8057 write_seqlock_irq(&bb->lock);
8062 /* Find the last range that starts at-or-before 's' */
8063 while (hi - lo > 1) {
8064 int mid = (lo + hi) / 2;
8065 sector_t a = BB_OFFSET(p[mid]);
8071 if (hi > lo && BB_OFFSET(p[lo]) > s)
8075 /* we found a range that might merge with the start
8078 sector_t a = BB_OFFSET(p[lo]);
8079 sector_t e = a + BB_LEN(p[lo]);
8080 int ack = BB_ACK(p[lo]);
8082 /* Yes, we can merge with a previous range */
8083 if (s == a && s + sectors >= e)
8084 /* new range covers old */
8087 ack = ack && acknowledged;
8089 if (e < s + sectors)
8091 if (e - a <= BB_MAX_LEN) {
8092 p[lo] = BB_MAKE(a, e-a, ack);
8095 /* does not all fit in one range,
8096 * make p[lo] maximal
8098 if (BB_LEN(p[lo]) != BB_MAX_LEN)
8099 p[lo] = BB_MAKE(a, BB_MAX_LEN, ack);
8105 if (sectors && hi < bb->count) {
8106 /* 'hi' points to the first range that starts after 's'.
8107 * Maybe we can merge with the start of that range */
8108 sector_t a = BB_OFFSET(p[hi]);
8109 sector_t e = a + BB_LEN(p[hi]);
8110 int ack = BB_ACK(p[hi]);
8111 if (a <= s + sectors) {
8112 /* merging is possible */
8113 if (e <= s + sectors) {
8118 ack = ack && acknowledged;
8121 if (e - a <= BB_MAX_LEN) {
8122 p[hi] = BB_MAKE(a, e-a, ack);
8125 p[hi] = BB_MAKE(a, BB_MAX_LEN, ack);
8133 if (sectors == 0 && hi < bb->count) {
8134 /* we might be able to combine lo and hi */
8135 /* Note: 's' is at the end of 'lo' */
8136 sector_t a = BB_OFFSET(p[hi]);
8137 int lolen = BB_LEN(p[lo]);
8138 int hilen = BB_LEN(p[hi]);
8139 int newlen = lolen + hilen - (s - a);
8140 if (s >= a && newlen < BB_MAX_LEN) {
8141 /* yes, we can combine them */
8142 int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]);
8143 p[lo] = BB_MAKE(BB_OFFSET(p[lo]), newlen, ack);
8144 memmove(p + hi, p + hi + 1,
8145 (bb->count - hi - 1) * 8);
8150 /* didn't merge (it all).
8151 * Need to add a range just before 'hi' */
8152 if (bb->count >= MD_MAX_BADBLOCKS) {
8153 /* No room for more */
8157 int this_sectors = sectors;
8158 memmove(p + hi + 1, p + hi,
8159 (bb->count - hi) * 8);
8162 if (this_sectors > BB_MAX_LEN)
8163 this_sectors = BB_MAX_LEN;
8164 p[hi] = BB_MAKE(s, this_sectors, acknowledged);
8165 sectors -= this_sectors;
8172 bb->unacked_exist = 1;
8173 write_sequnlock_irq(&bb->lock);
8178 int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
8183 s += rdev->new_data_offset;
8185 s += rdev->data_offset;
8186 rv = md_set_badblocks(&rdev->badblocks,
8189 /* Make sure they get written out promptly */
8190 sysfs_notify_dirent_safe(rdev->sysfs_state);
8191 set_bit(MD_CHANGE_CLEAN, &rdev->mddev->flags);
8192 md_wakeup_thread(rdev->mddev->thread);
8196 EXPORT_SYMBOL_GPL(rdev_set_badblocks);
8199 * Remove a range of bad blocks from the table.
8200 * This may involve extending the table if we spilt a region,
8201 * but it must not fail. So if the table becomes full, we just
8202 * drop the remove request.
8204 static int md_clear_badblocks(struct badblocks *bb, sector_t s, int sectors)
8208 sector_t target = s + sectors;
8211 if (bb->shift > 0) {
8212 /* When clearing we round the start up and the end down.
8213 * This should not matter as the shift should align with
8214 * the block size and no rounding should ever be needed.
8215 * However it is better the think a block is bad when it
8216 * isn't than to think a block is not bad when it is.
8218 s += (1<<bb->shift) - 1;
8220 target >>= bb->shift;
8221 sectors = target - s;
8224 write_seqlock_irq(&bb->lock);
8229 /* Find the last range that starts before 'target' */
8230 while (hi - lo > 1) {
8231 int mid = (lo + hi) / 2;
8232 sector_t a = BB_OFFSET(p[mid]);
8239 /* p[lo] is the last range that could overlap the
8240 * current range. Earlier ranges could also overlap,
8241 * but only this one can overlap the end of the range.
8243 if (BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > target) {
8244 /* Partial overlap, leave the tail of this range */
8245 int ack = BB_ACK(p[lo]);
8246 sector_t a = BB_OFFSET(p[lo]);
8247 sector_t end = a + BB_LEN(p[lo]);
8250 /* we need to split this range */
8251 if (bb->count >= MD_MAX_BADBLOCKS) {
8255 memmove(p+lo+1, p+lo, (bb->count - lo) * 8);
8257 p[lo] = BB_MAKE(a, s-a, ack);
8260 p[lo] = BB_MAKE(target, end - target, ack);
8261 /* there is no longer an overlap */
8266 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
8267 /* This range does overlap */
8268 if (BB_OFFSET(p[lo]) < s) {
8269 /* Keep the early parts of this range. */
8270 int ack = BB_ACK(p[lo]);
8271 sector_t start = BB_OFFSET(p[lo]);
8272 p[lo] = BB_MAKE(start, s - start, ack);
8273 /* now low doesn't overlap, so.. */
8278 /* 'lo' is strictly before, 'hi' is strictly after,
8279 * anything between needs to be discarded
8282 memmove(p+lo+1, p+hi, (bb->count - hi) * 8);
8283 bb->count -= (hi - lo - 1);
8289 write_sequnlock_irq(&bb->lock);
8293 int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
8297 s += rdev->new_data_offset;
8299 s += rdev->data_offset;
8300 return md_clear_badblocks(&rdev->badblocks,
8303 EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
8306 * Acknowledge all bad blocks in a list.
8307 * This only succeeds if ->changed is clear. It is used by
8308 * in-kernel metadata updates
8310 void md_ack_all_badblocks(struct badblocks *bb)
8312 if (bb->page == NULL || bb->changed)
8313 /* no point even trying */
8315 write_seqlock_irq(&bb->lock);
8317 if (bb->changed == 0 && bb->unacked_exist) {
8320 for (i = 0; i < bb->count ; i++) {
8321 if (!BB_ACK(p[i])) {
8322 sector_t start = BB_OFFSET(p[i]);
8323 int len = BB_LEN(p[i]);
8324 p[i] = BB_MAKE(start, len, 1);
8327 bb->unacked_exist = 0;
8329 write_sequnlock_irq(&bb->lock);
8331 EXPORT_SYMBOL_GPL(md_ack_all_badblocks);
8333 /* sysfs access to bad-blocks list.
8334 * We present two files.
8335 * 'bad-blocks' lists sector numbers and lengths of ranges that
8336 * are recorded as bad. The list is truncated to fit within
8337 * the one-page limit of sysfs.
8338 * Writing "sector length" to this file adds an acknowledged
8340 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
8341 * been acknowledged. Writing to this file adds bad blocks
8342 * without acknowledging them. This is largely for testing.
8346 badblocks_show(struct badblocks *bb, char *page, int unack)
8357 seq = read_seqbegin(&bb->lock);
8362 while (len < PAGE_SIZE && i < bb->count) {
8363 sector_t s = BB_OFFSET(p[i]);
8364 unsigned int length = BB_LEN(p[i]);
8365 int ack = BB_ACK(p[i]);
8371 len += snprintf(page+len, PAGE_SIZE-len, "%llu %u\n",
8372 (unsigned long long)s << bb->shift,
8373 length << bb->shift);
8375 if (unack && len == 0)
8376 bb->unacked_exist = 0;
8378 if (read_seqretry(&bb->lock, seq))
8387 badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack)
8389 unsigned long long sector;
8393 /* Allow clearing via sysfs *only* for testing/debugging.
8394 * Normally only a successful write may clear a badblock
8397 if (page[0] == '-') {
8401 #endif /* DO_DEBUG */
8403 switch (sscanf(page, "%llu %d%c", §or, &length, &newline)) {
8405 if (newline != '\n')
8417 md_clear_badblocks(bb, sector, length);
8420 #endif /* DO_DEBUG */
8421 if (md_set_badblocks(bb, sector, length, !unack))
8427 static int md_notify_reboot(struct notifier_block *this,
8428 unsigned long code, void *x)
8430 struct list_head *tmp;
8431 struct mddev *mddev;
8434 for_each_mddev(mddev, tmp) {
8435 if (mddev_trylock(mddev)) {
8437 __md_stop_writes(mddev);
8438 mddev->safemode = 2;
8439 mddev_unlock(mddev);
8444 * certain more exotic SCSI devices are known to be
8445 * volatile wrt too early system reboots. While the
8446 * right place to handle this issue is the given
8447 * driver, we do want to have a safe RAID driver ...
8455 static struct notifier_block md_notifier = {
8456 .notifier_call = md_notify_reboot,
8458 .priority = INT_MAX, /* before any real devices */
8461 static void md_geninit(void)
8463 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
8465 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
8468 static int __init md_init(void)
8472 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
8476 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
8480 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
8483 if ((ret = register_blkdev(0, "mdp")) < 0)
8487 blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE,
8488 md_probe, NULL, NULL);
8489 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
8490 md_probe, NULL, NULL);
8492 register_reboot_notifier(&md_notifier);
8493 raid_table_header = register_sysctl_table(raid_root_table);
8499 unregister_blkdev(MD_MAJOR, "md");
8501 destroy_workqueue(md_misc_wq);
8503 destroy_workqueue(md_wq);
8511 * Searches all registered partitions for autorun RAID arrays
8515 static LIST_HEAD(all_detected_devices);
8516 struct detected_devices_node {
8517 struct list_head list;
8521 void md_autodetect_dev(dev_t dev)
8523 struct detected_devices_node *node_detected_dev;
8525 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
8526 if (node_detected_dev) {
8527 node_detected_dev->dev = dev;
8528 list_add_tail(&node_detected_dev->list, &all_detected_devices);
8530 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
8531 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
8536 static void autostart_arrays(int part)
8538 struct md_rdev *rdev;
8539 struct detected_devices_node *node_detected_dev;
8541 int i_scanned, i_passed;
8546 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
8548 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
8550 node_detected_dev = list_entry(all_detected_devices.next,
8551 struct detected_devices_node, list);
8552 list_del(&node_detected_dev->list);
8553 dev = node_detected_dev->dev;
8554 kfree(node_detected_dev);
8555 rdev = md_import_device(dev,0, 90);
8559 if (test_bit(Faulty, &rdev->flags)) {
8563 set_bit(AutoDetected, &rdev->flags);
8564 list_add(&rdev->same_set, &pending_raid_disks);
8568 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
8569 i_scanned, i_passed);
8571 autorun_devices(part);
8574 #endif /* !MODULE */
8576 static __exit void md_exit(void)
8578 struct mddev *mddev;
8579 struct list_head *tmp;
8581 blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS);
8582 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
8584 unregister_blkdev(MD_MAJOR,"md");
8585 unregister_blkdev(mdp_major, "mdp");
8586 unregister_reboot_notifier(&md_notifier);
8587 unregister_sysctl_table(raid_table_header);
8588 remove_proc_entry("mdstat", NULL);
8589 for_each_mddev(mddev, tmp) {
8590 export_array(mddev);
8591 mddev->hold_active = 0;
8593 destroy_workqueue(md_misc_wq);
8594 destroy_workqueue(md_wq);
8597 subsys_initcall(md_init);
8598 module_exit(md_exit)
8600 static int get_ro(char *buffer, struct kernel_param *kp)
8602 return sprintf(buffer, "%d", start_readonly);
8604 static int set_ro(const char *val, struct kernel_param *kp)
8607 int num = simple_strtoul(val, &e, 10);
8608 if (*val && (*e == '\0' || *e == '\n')) {
8609 start_readonly = num;
8615 module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
8616 module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
8618 module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
8620 EXPORT_SYMBOL(register_md_personality);
8621 EXPORT_SYMBOL(unregister_md_personality);
8622 EXPORT_SYMBOL(md_error);
8623 EXPORT_SYMBOL(md_done_sync);
8624 EXPORT_SYMBOL(md_write_start);
8625 EXPORT_SYMBOL(md_write_end);
8626 EXPORT_SYMBOL(md_register_thread);
8627 EXPORT_SYMBOL(md_unregister_thread);
8628 EXPORT_SYMBOL(md_wakeup_thread);
8629 EXPORT_SYMBOL(md_check_recovery);
8630 MODULE_LICENSE("GPL");
8631 MODULE_DESCRIPTION("MD RAID framework");
8633 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);