2 md.c : Multiple Devices driver for Linux
3 Copyright (C) 1998, 1999, 2000 Ingo Molnar
5 completely rewritten, based on the MD driver code from Marc Zyngier
9 - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar
10 - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com>
11 - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net>
12 - kerneld support by Boris Tobotras <boris@xtalk.msk.su>
13 - kmod support by: Cyrus Durgin
14 - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com>
15 - Devfs support by Richard Gooch <rgooch@atnf.csiro.au>
17 - lots of fixes and improvements to the RAID1/RAID5 and generic
18 RAID code (such as request based resynchronization):
20 Neil Brown <neilb@cse.unsw.edu.au>.
22 - persistent bitmap code
23 Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.
25 This program is free software; you can redistribute it and/or modify
26 it under the terms of the GNU General Public License as published by
27 the Free Software Foundation; either version 2, or (at your option)
30 You should have received a copy of the GNU General Public License
31 (for example /usr/src/linux/COPYING); if not, write to the Free
32 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35 #include <linux/kthread.h>
36 #include <linux/blkdev.h>
37 #include <linux/sysctl.h>
38 #include <linux/seq_file.h>
40 #include <linux/poll.h>
41 #include <linux/ctype.h>
42 #include <linux/string.h>
43 #include <linux/hdreg.h>
44 #include <linux/proc_fs.h>
45 #include <linux/random.h>
46 #include <linux/module.h>
47 #include <linux/reboot.h>
48 #include <linux/file.h>
49 #include <linux/compat.h>
50 #include <linux/delay.h>
51 #include <linux/raid/md_p.h>
52 #include <linux/raid/md_u.h>
53 #include <linux/slab.h>
58 static void autostart_arrays(int part
);
61 /* pers_list is a list of registered personalities protected
63 * pers_lock does extra service to protect accesses to
64 * mddev->thread when the mutex cannot be held.
66 static LIST_HEAD(pers_list
);
67 static DEFINE_SPINLOCK(pers_lock
);
69 static void md_print_devices(void);
71 static DECLARE_WAIT_QUEUE_HEAD(resync_wait
);
72 static struct workqueue_struct
*md_wq
;
73 static struct workqueue_struct
*md_misc_wq
;
75 static int remove_and_add_spares(struct mddev
*mddev
,
76 struct md_rdev
*this);
78 #define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
81 * Default number of read corrections we'll attempt on an rdev
82 * before ejecting it from the array. We divide the read error
83 * count by 2 for every hour elapsed between read errors.
85 #define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
87 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
88 * is 1000 KB/sec, so the extra system load does not show up that much.
89 * Increase it if you want to have more _guaranteed_ speed. Note that
90 * the RAID driver will use the maximum available bandwidth if the IO
91 * subsystem is idle. There is also an 'absolute maximum' reconstruction
92 * speed limit - in case reconstruction slows down your system despite
95 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
96 * or /sys/block/mdX/md/sync_speed_{min,max}
99 static int sysctl_speed_limit_min
= 1000;
100 static int sysctl_speed_limit_max
= 200000;
101 static inline int speed_min(struct mddev
*mddev
)
103 return mddev
->sync_speed_min
?
104 mddev
->sync_speed_min
: sysctl_speed_limit_min
;
107 static inline int speed_max(struct mddev
*mddev
)
109 return mddev
->sync_speed_max
?
110 mddev
->sync_speed_max
: sysctl_speed_limit_max
;
113 static struct ctl_table_header
*raid_table_header
;
115 static ctl_table raid_table
[] = {
117 .procname
= "speed_limit_min",
118 .data
= &sysctl_speed_limit_min
,
119 .maxlen
= sizeof(int),
120 .mode
= S_IRUGO
|S_IWUSR
,
121 .proc_handler
= proc_dointvec
,
124 .procname
= "speed_limit_max",
125 .data
= &sysctl_speed_limit_max
,
126 .maxlen
= sizeof(int),
127 .mode
= S_IRUGO
|S_IWUSR
,
128 .proc_handler
= proc_dointvec
,
133 static ctl_table raid_dir_table
[] = {
137 .mode
= S_IRUGO
|S_IXUGO
,
143 static ctl_table raid_root_table
[] = {
148 .child
= raid_dir_table
,
153 static const struct block_device_operations md_fops
;
155 static int start_readonly
;
158 * like bio_clone, but with a local bio set
161 struct bio
*bio_alloc_mddev(gfp_t gfp_mask
, int nr_iovecs
,
166 if (!mddev
|| !mddev
->bio_set
)
167 return bio_alloc(gfp_mask
, nr_iovecs
);
169 b
= bio_alloc_bioset(gfp_mask
, nr_iovecs
, mddev
->bio_set
);
174 EXPORT_SYMBOL_GPL(bio_alloc_mddev
);
176 struct bio
*bio_clone_mddev(struct bio
*bio
, gfp_t gfp_mask
,
179 if (!mddev
|| !mddev
->bio_set
)
180 return bio_clone(bio
, gfp_mask
);
182 return bio_clone_bioset(bio
, gfp_mask
, mddev
->bio_set
);
184 EXPORT_SYMBOL_GPL(bio_clone_mddev
);
186 void md_trim_bio(struct bio
*bio
, int offset
, int size
)
188 /* 'bio' is a cloned bio which we need to trim to match
189 * the given offset and size.
190 * This requires adjusting bi_sector, bi_size, and bi_io_vec
193 struct bio_vec
*bvec
;
197 if (offset
== 0 && size
== bio
->bi_size
)
200 clear_bit(BIO_SEG_VALID
, &bio
->bi_flags
);
202 bio_advance(bio
, offset
<< 9);
206 /* avoid any complications with bi_idx being non-zero*/
208 memmove(bio
->bi_io_vec
, bio
->bi_io_vec
+bio
->bi_idx
,
209 (bio
->bi_vcnt
- bio
->bi_idx
) * sizeof(struct bio_vec
));
210 bio
->bi_vcnt
-= bio
->bi_idx
;
213 /* Make sure vcnt and last bv are not too big */
214 bio_for_each_segment(bvec
, bio
, i
) {
215 if (sofar
+ bvec
->bv_len
> size
)
216 bvec
->bv_len
= size
- sofar
;
217 if (bvec
->bv_len
== 0) {
221 sofar
+= bvec
->bv_len
;
224 EXPORT_SYMBOL_GPL(md_trim_bio
);
227 * We have a system wide 'event count' that is incremented
228 * on any 'interesting' event, and readers of /proc/mdstat
229 * can use 'poll' or 'select' to find out when the event
233 * start array, stop array, error, add device, remove device,
234 * start build, activate spare
236 static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters
);
237 static atomic_t md_event_count
;
238 void md_new_event(struct mddev
*mddev
)
240 atomic_inc(&md_event_count
);
241 wake_up(&md_event_waiters
);
243 EXPORT_SYMBOL_GPL(md_new_event
);
245 /* Alternate version that can be called from interrupts
246 * when calling sysfs_notify isn't needed.
248 static void md_new_event_inintr(struct mddev
*mddev
)
250 atomic_inc(&md_event_count
);
251 wake_up(&md_event_waiters
);
255 * Enables to iterate over all existing md arrays
256 * all_mddevs_lock protects this list.
258 static LIST_HEAD(all_mddevs
);
259 static DEFINE_SPINLOCK(all_mddevs_lock
);
263 * iterates through all used mddevs in the system.
264 * We take care to grab the all_mddevs_lock whenever navigating
265 * the list, and to always hold a refcount when unlocked.
266 * Any code which breaks out of this loop while own
267 * a reference to the current mddev and must mddev_put it.
269 #define for_each_mddev(_mddev,_tmp) \
271 for (({ spin_lock(&all_mddevs_lock); \
272 _tmp = all_mddevs.next; \
274 ({ if (_tmp != &all_mddevs) \
275 mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\
276 spin_unlock(&all_mddevs_lock); \
277 if (_mddev) mddev_put(_mddev); \
278 _mddev = list_entry(_tmp, struct mddev, all_mddevs); \
279 _tmp != &all_mddevs;}); \
280 ({ spin_lock(&all_mddevs_lock); \
281 _tmp = _tmp->next;}) \
285 /* Rather than calling directly into the personality make_request function,
286 * IO requests come here first so that we can check if the device is
287 * being suspended pending a reconfiguration.
288 * We hold a refcount over the call to ->make_request. By the time that
289 * call has finished, the bio has been linked into some internal structure
290 * and so is visible to ->quiesce(), so we don't need the refcount any more.
292 static void md_make_request(struct request_queue
*q
, struct bio
*bio
)
294 const int rw
= bio_data_dir(bio
);
295 struct mddev
*mddev
= q
->queuedata
;
297 unsigned int sectors
;
299 if (mddev
== NULL
|| mddev
->pers
== NULL
304 if (mddev
->ro
== 1 && unlikely(rw
== WRITE
)) {
305 bio_endio(bio
, bio_sectors(bio
) == 0 ? 0 : -EROFS
);
308 smp_rmb(); /* Ensure implications of 'active' are visible */
310 if (mddev
->suspended
) {
313 prepare_to_wait(&mddev
->sb_wait
, &__wait
,
314 TASK_UNINTERRUPTIBLE
);
315 if (!mddev
->suspended
)
321 finish_wait(&mddev
->sb_wait
, &__wait
);
323 atomic_inc(&mddev
->active_io
);
327 * save the sectors now since our bio can
328 * go away inside make_request
330 sectors
= bio_sectors(bio
);
331 mddev
->pers
->make_request(mddev
, bio
);
333 cpu
= part_stat_lock();
334 part_stat_inc(cpu
, &mddev
->gendisk
->part0
, ios
[rw
]);
335 part_stat_add(cpu
, &mddev
->gendisk
->part0
, sectors
[rw
], sectors
);
338 if (atomic_dec_and_test(&mddev
->active_io
) && mddev
->suspended
)
339 wake_up(&mddev
->sb_wait
);
342 /* mddev_suspend makes sure no new requests are submitted
343 * to the device, and that any requests that have been submitted
344 * are completely handled.
345 * Once ->stop is called and completes, the module will be completely
348 void mddev_suspend(struct mddev
*mddev
)
350 BUG_ON(mddev
->suspended
);
351 mddev
->suspended
= 1;
353 wait_event(mddev
->sb_wait
, atomic_read(&mddev
->active_io
) == 0);
354 mddev
->pers
->quiesce(mddev
, 1);
356 del_timer_sync(&mddev
->safemode_timer
);
358 EXPORT_SYMBOL_GPL(mddev_suspend
);
360 void mddev_resume(struct mddev
*mddev
)
362 mddev
->suspended
= 0;
363 wake_up(&mddev
->sb_wait
);
364 mddev
->pers
->quiesce(mddev
, 0);
366 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
367 md_wakeup_thread(mddev
->thread
);
368 md_wakeup_thread(mddev
->sync_thread
); /* possibly kick off a reshape */
370 EXPORT_SYMBOL_GPL(mddev_resume
);
372 int mddev_congested(struct mddev
*mddev
, int bits
)
374 return mddev
->suspended
;
376 EXPORT_SYMBOL(mddev_congested
);
379 * Generic flush handling for md
382 static void md_end_flush(struct bio
*bio
, int err
)
384 struct md_rdev
*rdev
= bio
->bi_private
;
385 struct mddev
*mddev
= rdev
->mddev
;
387 rdev_dec_pending(rdev
, mddev
);
389 if (atomic_dec_and_test(&mddev
->flush_pending
)) {
390 /* The pre-request flush has finished */
391 queue_work(md_wq
, &mddev
->flush_work
);
396 static void md_submit_flush_data(struct work_struct
*ws
);
398 static void submit_flushes(struct work_struct
*ws
)
400 struct mddev
*mddev
= container_of(ws
, struct mddev
, flush_work
);
401 struct md_rdev
*rdev
;
403 INIT_WORK(&mddev
->flush_work
, md_submit_flush_data
);
404 atomic_set(&mddev
->flush_pending
, 1);
406 rdev_for_each_rcu(rdev
, mddev
)
407 if (rdev
->raid_disk
>= 0 &&
408 !test_bit(Faulty
, &rdev
->flags
)) {
409 /* Take two references, one is dropped
410 * when request finishes, one after
411 * we reclaim rcu_read_lock
414 atomic_inc(&rdev
->nr_pending
);
415 atomic_inc(&rdev
->nr_pending
);
417 bi
= bio_alloc_mddev(GFP_NOIO
, 0, mddev
);
418 bi
->bi_end_io
= md_end_flush
;
419 bi
->bi_private
= rdev
;
420 bi
->bi_bdev
= rdev
->bdev
;
421 atomic_inc(&mddev
->flush_pending
);
422 submit_bio(WRITE_FLUSH
, bi
);
424 rdev_dec_pending(rdev
, mddev
);
427 if (atomic_dec_and_test(&mddev
->flush_pending
))
428 queue_work(md_wq
, &mddev
->flush_work
);
431 static void md_submit_flush_data(struct work_struct
*ws
)
433 struct mddev
*mddev
= container_of(ws
, struct mddev
, flush_work
);
434 struct bio
*bio
= mddev
->flush_bio
;
436 if (bio
->bi_size
== 0)
437 /* an empty barrier - all done */
440 bio
->bi_rw
&= ~REQ_FLUSH
;
441 mddev
->pers
->make_request(mddev
, bio
);
444 mddev
->flush_bio
= NULL
;
445 wake_up(&mddev
->sb_wait
);
448 void md_flush_request(struct mddev
*mddev
, struct bio
*bio
)
450 spin_lock_irq(&mddev
->write_lock
);
451 wait_event_lock_irq(mddev
->sb_wait
,
454 mddev
->flush_bio
= bio
;
455 spin_unlock_irq(&mddev
->write_lock
);
457 INIT_WORK(&mddev
->flush_work
, submit_flushes
);
458 queue_work(md_wq
, &mddev
->flush_work
);
460 EXPORT_SYMBOL(md_flush_request
);
462 void md_unplug(struct blk_plug_cb
*cb
, bool from_schedule
)
464 struct mddev
*mddev
= cb
->data
;
465 md_wakeup_thread(mddev
->thread
);
468 EXPORT_SYMBOL(md_unplug
);
470 static inline struct mddev
*mddev_get(struct mddev
*mddev
)
472 atomic_inc(&mddev
->active
);
476 static void mddev_delayed_delete(struct work_struct
*ws
);
478 static void mddev_put(struct mddev
*mddev
)
480 struct bio_set
*bs
= NULL
;
482 if (!atomic_dec_and_lock(&mddev
->active
, &all_mddevs_lock
))
484 if (!mddev
->raid_disks
&& list_empty(&mddev
->disks
) &&
485 mddev
->ctime
== 0 && !mddev
->hold_active
) {
486 /* Array is not configured at all, and not held active,
488 list_del_init(&mddev
->all_mddevs
);
490 mddev
->bio_set
= NULL
;
491 if (mddev
->gendisk
) {
492 /* We did a probe so need to clean up. Call
493 * queue_work inside the spinlock so that
494 * flush_workqueue() after mddev_find will
495 * succeed in waiting for the work to be done.
497 INIT_WORK(&mddev
->del_work
, mddev_delayed_delete
);
498 queue_work(md_misc_wq
, &mddev
->del_work
);
502 spin_unlock(&all_mddevs_lock
);
507 void mddev_init(struct mddev
*mddev
)
509 mutex_init(&mddev
->open_mutex
);
510 mutex_init(&mddev
->reconfig_mutex
);
511 mutex_init(&mddev
->bitmap_info
.mutex
);
512 INIT_LIST_HEAD(&mddev
->disks
);
513 INIT_LIST_HEAD(&mddev
->all_mddevs
);
514 init_timer(&mddev
->safemode_timer
);
515 atomic_set(&mddev
->active
, 1);
516 atomic_set(&mddev
->openers
, 0);
517 atomic_set(&mddev
->active_io
, 0);
518 spin_lock_init(&mddev
->write_lock
);
519 atomic_set(&mddev
->flush_pending
, 0);
520 init_waitqueue_head(&mddev
->sb_wait
);
521 init_waitqueue_head(&mddev
->recovery_wait
);
522 mddev
->reshape_position
= MaxSector
;
523 mddev
->reshape_backwards
= 0;
524 mddev
->last_sync_action
= "none";
525 mddev
->resync_min
= 0;
526 mddev
->resync_max
= MaxSector
;
527 mddev
->level
= LEVEL_NONE
;
529 EXPORT_SYMBOL_GPL(mddev_init
);
531 static struct mddev
* mddev_find(dev_t unit
)
533 struct mddev
*mddev
, *new = NULL
;
535 if (unit
&& MAJOR(unit
) != MD_MAJOR
)
536 unit
&= ~((1<<MdpMinorShift
)-1);
539 spin_lock(&all_mddevs_lock
);
542 list_for_each_entry(mddev
, &all_mddevs
, all_mddevs
)
543 if (mddev
->unit
== unit
) {
545 spin_unlock(&all_mddevs_lock
);
551 list_add(&new->all_mddevs
, &all_mddevs
);
552 spin_unlock(&all_mddevs_lock
);
553 new->hold_active
= UNTIL_IOCTL
;
557 /* find an unused unit number */
558 static int next_minor
= 512;
559 int start
= next_minor
;
563 dev
= MKDEV(MD_MAJOR
, next_minor
);
565 if (next_minor
> MINORMASK
)
567 if (next_minor
== start
) {
568 /* Oh dear, all in use. */
569 spin_unlock(&all_mddevs_lock
);
575 list_for_each_entry(mddev
, &all_mddevs
, all_mddevs
)
576 if (mddev
->unit
== dev
) {
582 new->md_minor
= MINOR(dev
);
583 new->hold_active
= UNTIL_STOP
;
584 list_add(&new->all_mddevs
, &all_mddevs
);
585 spin_unlock(&all_mddevs_lock
);
588 spin_unlock(&all_mddevs_lock
);
590 new = kzalloc(sizeof(*new), GFP_KERNEL
);
595 if (MAJOR(unit
) == MD_MAJOR
)
596 new->md_minor
= MINOR(unit
);
598 new->md_minor
= MINOR(unit
) >> MdpMinorShift
;
605 static inline int mddev_lock(struct mddev
* mddev
)
607 return mutex_lock_interruptible(&mddev
->reconfig_mutex
);
610 static inline int mddev_is_locked(struct mddev
*mddev
)
612 return mutex_is_locked(&mddev
->reconfig_mutex
);
615 static inline int mddev_trylock(struct mddev
* mddev
)
617 return mutex_trylock(&mddev
->reconfig_mutex
);
620 static struct attribute_group md_redundancy_group
;
622 static void mddev_unlock(struct mddev
* mddev
)
624 if (mddev
->to_remove
) {
625 /* These cannot be removed under reconfig_mutex as
626 * an access to the files will try to take reconfig_mutex
627 * while holding the file unremovable, which leads to
629 * So hold set sysfs_active while the remove in happeing,
630 * and anything else which might set ->to_remove or my
631 * otherwise change the sysfs namespace will fail with
632 * -EBUSY if sysfs_active is still set.
633 * We set sysfs_active under reconfig_mutex and elsewhere
634 * test it under the same mutex to ensure its correct value
637 struct attribute_group
*to_remove
= mddev
->to_remove
;
638 mddev
->to_remove
= NULL
;
639 mddev
->sysfs_active
= 1;
640 mutex_unlock(&mddev
->reconfig_mutex
);
642 if (mddev
->kobj
.sd
) {
643 if (to_remove
!= &md_redundancy_group
)
644 sysfs_remove_group(&mddev
->kobj
, to_remove
);
645 if (mddev
->pers
== NULL
||
646 mddev
->pers
->sync_request
== NULL
) {
647 sysfs_remove_group(&mddev
->kobj
, &md_redundancy_group
);
648 if (mddev
->sysfs_action
)
649 sysfs_put(mddev
->sysfs_action
);
650 mddev
->sysfs_action
= NULL
;
653 mddev
->sysfs_active
= 0;
655 mutex_unlock(&mddev
->reconfig_mutex
);
657 /* As we've dropped the mutex we need a spinlock to
658 * make sure the thread doesn't disappear
660 spin_lock(&pers_lock
);
661 md_wakeup_thread(mddev
->thread
);
662 spin_unlock(&pers_lock
);
665 static struct md_rdev
* find_rdev_nr(struct mddev
*mddev
, int nr
)
667 struct md_rdev
*rdev
;
669 rdev_for_each(rdev
, mddev
)
670 if (rdev
->desc_nr
== nr
)
676 static struct md_rdev
*find_rdev_nr_rcu(struct mddev
*mddev
, int nr
)
678 struct md_rdev
*rdev
;
680 rdev_for_each_rcu(rdev
, mddev
)
681 if (rdev
->desc_nr
== nr
)
687 static struct md_rdev
*find_rdev(struct mddev
*mddev
, dev_t dev
)
689 struct md_rdev
*rdev
;
691 rdev_for_each(rdev
, mddev
)
692 if (rdev
->bdev
->bd_dev
== dev
)
698 static struct md_rdev
*find_rdev_rcu(struct mddev
*mddev
, dev_t dev
)
700 struct md_rdev
*rdev
;
702 rdev_for_each_rcu(rdev
, mddev
)
703 if (rdev
->bdev
->bd_dev
== dev
)
709 static struct md_personality
*find_pers(int level
, char *clevel
)
711 struct md_personality
*pers
;
712 list_for_each_entry(pers
, &pers_list
, list
) {
713 if (level
!= LEVEL_NONE
&& pers
->level
== level
)
715 if (strcmp(pers
->name
, clevel
)==0)
721 /* return the offset of the super block in 512byte sectors */
722 static inline sector_t
calc_dev_sboffset(struct md_rdev
*rdev
)
724 sector_t num_sectors
= i_size_read(rdev
->bdev
->bd_inode
) / 512;
725 return MD_NEW_SIZE_SECTORS(num_sectors
);
728 static int alloc_disk_sb(struct md_rdev
* rdev
)
733 rdev
->sb_page
= alloc_page(GFP_KERNEL
);
734 if (!rdev
->sb_page
) {
735 printk(KERN_ALERT
"md: out of memory.\n");
742 void md_rdev_clear(struct md_rdev
*rdev
)
745 put_page(rdev
->sb_page
);
747 rdev
->sb_page
= NULL
;
752 put_page(rdev
->bb_page
);
753 rdev
->bb_page
= NULL
;
755 kfree(rdev
->badblocks
.page
);
756 rdev
->badblocks
.page
= NULL
;
758 EXPORT_SYMBOL_GPL(md_rdev_clear
);
760 static void super_written(struct bio
*bio
, int error
)
762 struct md_rdev
*rdev
= bio
->bi_private
;
763 struct mddev
*mddev
= rdev
->mddev
;
765 if (error
|| !test_bit(BIO_UPTODATE
, &bio
->bi_flags
)) {
766 printk("md: super_written gets error=%d, uptodate=%d\n",
767 error
, test_bit(BIO_UPTODATE
, &bio
->bi_flags
));
768 WARN_ON(test_bit(BIO_UPTODATE
, &bio
->bi_flags
));
769 md_error(mddev
, rdev
);
772 if (atomic_dec_and_test(&mddev
->pending_writes
))
773 wake_up(&mddev
->sb_wait
);
777 void md_super_write(struct mddev
*mddev
, struct md_rdev
*rdev
,
778 sector_t sector
, int size
, struct page
*page
)
780 /* write first size bytes of page to sector of rdev
781 * Increment mddev->pending_writes before returning
782 * and decrement it on completion, waking up sb_wait
783 * if zero is reached.
784 * If an error occurred, call md_error
786 struct bio
*bio
= bio_alloc_mddev(GFP_NOIO
, 1, mddev
);
788 bio
->bi_bdev
= rdev
->meta_bdev
? rdev
->meta_bdev
: rdev
->bdev
;
789 bio
->bi_sector
= sector
;
790 bio_add_page(bio
, page
, size
, 0);
791 bio
->bi_private
= rdev
;
792 bio
->bi_end_io
= super_written
;
794 atomic_inc(&mddev
->pending_writes
);
795 submit_bio(WRITE_FLUSH_FUA
, bio
);
798 void md_super_wait(struct mddev
*mddev
)
800 /* wait for all superblock writes that were scheduled to complete */
803 prepare_to_wait(&mddev
->sb_wait
, &wq
, TASK_UNINTERRUPTIBLE
);
804 if (atomic_read(&mddev
->pending_writes
)==0)
808 finish_wait(&mddev
->sb_wait
, &wq
);
811 static void bi_complete(struct bio
*bio
, int error
)
813 complete((struct completion
*)bio
->bi_private
);
816 int sync_page_io(struct md_rdev
*rdev
, sector_t sector
, int size
,
817 struct page
*page
, int rw
, bool metadata_op
)
819 struct bio
*bio
= bio_alloc_mddev(GFP_NOIO
, 1, rdev
->mddev
);
820 struct completion event
;
825 bio
->bi_bdev
= (metadata_op
&& rdev
->meta_bdev
) ?
826 rdev
->meta_bdev
: rdev
->bdev
;
828 bio
->bi_sector
= sector
+ rdev
->sb_start
;
829 else if (rdev
->mddev
->reshape_position
!= MaxSector
&&
830 (rdev
->mddev
->reshape_backwards
==
831 (sector
>= rdev
->mddev
->reshape_position
)))
832 bio
->bi_sector
= sector
+ rdev
->new_data_offset
;
834 bio
->bi_sector
= sector
+ rdev
->data_offset
;
835 bio_add_page(bio
, page
, size
, 0);
836 init_completion(&event
);
837 bio
->bi_private
= &event
;
838 bio
->bi_end_io
= bi_complete
;
840 wait_for_completion(&event
);
842 ret
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
846 EXPORT_SYMBOL_GPL(sync_page_io
);
848 static int read_disk_sb(struct md_rdev
* rdev
, int size
)
850 char b
[BDEVNAME_SIZE
];
851 if (!rdev
->sb_page
) {
859 if (!sync_page_io(rdev
, 0, size
, rdev
->sb_page
, READ
, true))
865 printk(KERN_WARNING
"md: disabled device %s, could not read superblock.\n",
866 bdevname(rdev
->bdev
,b
));
870 static int uuid_equal(mdp_super_t
*sb1
, mdp_super_t
*sb2
)
872 return sb1
->set_uuid0
== sb2
->set_uuid0
&&
873 sb1
->set_uuid1
== sb2
->set_uuid1
&&
874 sb1
->set_uuid2
== sb2
->set_uuid2
&&
875 sb1
->set_uuid3
== sb2
->set_uuid3
;
878 static int sb_equal(mdp_super_t
*sb1
, mdp_super_t
*sb2
)
881 mdp_super_t
*tmp1
, *tmp2
;
883 tmp1
= kmalloc(sizeof(*tmp1
),GFP_KERNEL
);
884 tmp2
= kmalloc(sizeof(*tmp2
),GFP_KERNEL
);
886 if (!tmp1
|| !tmp2
) {
888 printk(KERN_INFO
"md.c sb_equal(): failed to allocate memory!\n");
896 * nr_disks is not constant
901 ret
= (memcmp(tmp1
, tmp2
, MD_SB_GENERIC_CONSTANT_WORDS
* 4) == 0);
909 static u32
md_csum_fold(u32 csum
)
911 csum
= (csum
& 0xffff) + (csum
>> 16);
912 return (csum
& 0xffff) + (csum
>> 16);
915 static unsigned int calc_sb_csum(mdp_super_t
* sb
)
918 u32
*sb32
= (u32
*)sb
;
920 unsigned int disk_csum
, csum
;
922 disk_csum
= sb
->sb_csum
;
925 for (i
= 0; i
< MD_SB_BYTES
/4 ; i
++)
927 csum
= (newcsum
& 0xffffffff) + (newcsum
>>32);
931 /* This used to use csum_partial, which was wrong for several
932 * reasons including that different results are returned on
933 * different architectures. It isn't critical that we get exactly
934 * the same return value as before (we always csum_fold before
935 * testing, and that removes any differences). However as we
936 * know that csum_partial always returned a 16bit value on
937 * alphas, do a fold to maximise conformity to previous behaviour.
939 sb
->sb_csum
= md_csum_fold(disk_csum
);
941 sb
->sb_csum
= disk_csum
;
948 * Handle superblock details.
949 * We want to be able to handle multiple superblock formats
950 * so we have a common interface to them all, and an array of
951 * different handlers.
952 * We rely on user-space to write the initial superblock, and support
953 * reading and updating of superblocks.
954 * Interface methods are:
955 * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
956 * loads and validates a superblock on dev.
957 * if refdev != NULL, compare superblocks on both devices
959 * 0 - dev has a superblock that is compatible with refdev
960 * 1 - dev has a superblock that is compatible and newer than refdev
961 * so dev should be used as the refdev in future
962 * -EINVAL superblock incompatible or invalid
963 * -othererror e.g. -EIO
965 * int validate_super(struct mddev *mddev, struct md_rdev *dev)
966 * Verify that dev is acceptable into mddev.
967 * The first time, mddev->raid_disks will be 0, and data from
968 * dev should be merged in. Subsequent calls check that dev
969 * is new enough. Return 0 or -EINVAL
971 * void sync_super(struct mddev *mddev, struct md_rdev *dev)
972 * Update the superblock for rdev with data in mddev
973 * This does not write to disc.
979 struct module
*owner
;
980 int (*load_super
)(struct md_rdev
*rdev
,
981 struct md_rdev
*refdev
,
983 int (*validate_super
)(struct mddev
*mddev
,
984 struct md_rdev
*rdev
);
985 void (*sync_super
)(struct mddev
*mddev
,
986 struct md_rdev
*rdev
);
987 unsigned long long (*rdev_size_change
)(struct md_rdev
*rdev
,
988 sector_t num_sectors
);
989 int (*allow_new_offset
)(struct md_rdev
*rdev
,
990 unsigned long long new_offset
);
994 * Check that the given mddev has no bitmap.
996 * This function is called from the run method of all personalities that do not
997 * support bitmaps. It prints an error message and returns non-zero if mddev
998 * has a bitmap. Otherwise, it returns 0.
1001 int md_check_no_bitmap(struct mddev
*mddev
)
1003 if (!mddev
->bitmap_info
.file
&& !mddev
->bitmap_info
.offset
)
1005 printk(KERN_ERR
"%s: bitmaps are not supported for %s\n",
1006 mdname(mddev
), mddev
->pers
->name
);
1009 EXPORT_SYMBOL(md_check_no_bitmap
);
1012 * load_super for 0.90.0
1014 static int super_90_load(struct md_rdev
*rdev
, struct md_rdev
*refdev
, int minor_version
)
1016 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
1021 * Calculate the position of the superblock (512byte sectors),
1022 * it's at the end of the disk.
1024 * It also happens to be a multiple of 4Kb.
1026 rdev
->sb_start
= calc_dev_sboffset(rdev
);
1028 ret
= read_disk_sb(rdev
, MD_SB_BYTES
);
1029 if (ret
) return ret
;
1033 bdevname(rdev
->bdev
, b
);
1034 sb
= page_address(rdev
->sb_page
);
1036 if (sb
->md_magic
!= MD_SB_MAGIC
) {
1037 printk(KERN_ERR
"md: invalid raid superblock magic on %s\n",
1042 if (sb
->major_version
!= 0 ||
1043 sb
->minor_version
< 90 ||
1044 sb
->minor_version
> 91) {
1045 printk(KERN_WARNING
"Bad version number %d.%d on %s\n",
1046 sb
->major_version
, sb
->minor_version
,
1051 if (sb
->raid_disks
<= 0)
1054 if (md_csum_fold(calc_sb_csum(sb
)) != md_csum_fold(sb
->sb_csum
)) {
1055 printk(KERN_WARNING
"md: invalid superblock checksum on %s\n",
1060 rdev
->preferred_minor
= sb
->md_minor
;
1061 rdev
->data_offset
= 0;
1062 rdev
->new_data_offset
= 0;
1063 rdev
->sb_size
= MD_SB_BYTES
;
1064 rdev
->badblocks
.shift
= -1;
1066 if (sb
->level
== LEVEL_MULTIPATH
)
1069 rdev
->desc_nr
= sb
->this_disk
.number
;
1075 mdp_super_t
*refsb
= page_address(refdev
->sb_page
);
1076 if (!uuid_equal(refsb
, sb
)) {
1077 printk(KERN_WARNING
"md: %s has different UUID to %s\n",
1078 b
, bdevname(refdev
->bdev
,b2
));
1081 if (!sb_equal(refsb
, sb
)) {
1082 printk(KERN_WARNING
"md: %s has same UUID"
1083 " but different superblock to %s\n",
1084 b
, bdevname(refdev
->bdev
, b2
));
1088 ev2
= md_event(refsb
);
1094 rdev
->sectors
= rdev
->sb_start
;
1095 /* Limit to 4TB as metadata cannot record more than that.
1096 * (not needed for Linear and RAID0 as metadata doesn't
1099 if (rdev
->sectors
>= (2ULL << 32) && sb
->level
>= 1)
1100 rdev
->sectors
= (2ULL << 32) - 2;
1102 if (rdev
->sectors
< ((sector_t
)sb
->size
) * 2 && sb
->level
>= 1)
1103 /* "this cannot possibly happen" ... */
1111 * validate_super for 0.90.0
1113 static int super_90_validate(struct mddev
*mddev
, struct md_rdev
*rdev
)
1116 mdp_super_t
*sb
= page_address(rdev
->sb_page
);
1117 __u64 ev1
= md_event(sb
);
1119 rdev
->raid_disk
= -1;
1120 clear_bit(Faulty
, &rdev
->flags
);
1121 clear_bit(In_sync
, &rdev
->flags
);
1122 clear_bit(WriteMostly
, &rdev
->flags
);
1124 if (mddev
->raid_disks
== 0) {
1125 mddev
->major_version
= 0;
1126 mddev
->minor_version
= sb
->minor_version
;
1127 mddev
->patch_version
= sb
->patch_version
;
1128 mddev
->external
= 0;
1129 mddev
->chunk_sectors
= sb
->chunk_size
>> 9;
1130 mddev
->ctime
= sb
->ctime
;
1131 mddev
->utime
= sb
->utime
;
1132 mddev
->level
= sb
->level
;
1133 mddev
->clevel
[0] = 0;
1134 mddev
->layout
= sb
->layout
;
1135 mddev
->raid_disks
= sb
->raid_disks
;
1136 mddev
->dev_sectors
= ((sector_t
)sb
->size
) * 2;
1137 mddev
->events
= ev1
;
1138 mddev
->bitmap_info
.offset
= 0;
1139 mddev
->bitmap_info
.space
= 0;
1140 /* bitmap can use 60 K after the 4K superblocks */
1141 mddev
->bitmap_info
.default_offset
= MD_SB_BYTES
>> 9;
1142 mddev
->bitmap_info
.default_space
= 64*2 - (MD_SB_BYTES
>> 9);
1143 mddev
->reshape_backwards
= 0;
1145 if (mddev
->minor_version
>= 91) {
1146 mddev
->reshape_position
= sb
->reshape_position
;
1147 mddev
->delta_disks
= sb
->delta_disks
;
1148 mddev
->new_level
= sb
->new_level
;
1149 mddev
->new_layout
= sb
->new_layout
;
1150 mddev
->new_chunk_sectors
= sb
->new_chunk
>> 9;
1151 if (mddev
->delta_disks
< 0)
1152 mddev
->reshape_backwards
= 1;
1154 mddev
->reshape_position
= MaxSector
;
1155 mddev
->delta_disks
= 0;
1156 mddev
->new_level
= mddev
->level
;
1157 mddev
->new_layout
= mddev
->layout
;
1158 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
1161 if (sb
->state
& (1<<MD_SB_CLEAN
))
1162 mddev
->recovery_cp
= MaxSector
;
1164 if (sb
->events_hi
== sb
->cp_events_hi
&&
1165 sb
->events_lo
== sb
->cp_events_lo
) {
1166 mddev
->recovery_cp
= sb
->recovery_cp
;
1168 mddev
->recovery_cp
= 0;
1171 memcpy(mddev
->uuid
+0, &sb
->set_uuid0
, 4);
1172 memcpy(mddev
->uuid
+4, &sb
->set_uuid1
, 4);
1173 memcpy(mddev
->uuid
+8, &sb
->set_uuid2
, 4);
1174 memcpy(mddev
->uuid
+12,&sb
->set_uuid3
, 4);
1176 mddev
->max_disks
= MD_SB_DISKS
;
1178 if (sb
->state
& (1<<MD_SB_BITMAP_PRESENT
) &&
1179 mddev
->bitmap_info
.file
== NULL
) {
1180 mddev
->bitmap_info
.offset
=
1181 mddev
->bitmap_info
.default_offset
;
1182 mddev
->bitmap_info
.space
=
1183 mddev
->bitmap_info
.space
;
1186 } else if (mddev
->pers
== NULL
) {
1187 /* Insist on good event counter while assembling, except
1188 * for spares (which don't need an event count) */
1190 if (sb
->disks
[rdev
->desc_nr
].state
& (
1191 (1<<MD_DISK_SYNC
) | (1 << MD_DISK_ACTIVE
)))
1192 if (ev1
< mddev
->events
)
1194 } else if (mddev
->bitmap
) {
1195 /* if adding to array with a bitmap, then we can accept an
1196 * older device ... but not too old.
1198 if (ev1
< mddev
->bitmap
->events_cleared
)
1201 if (ev1
< mddev
->events
)
1202 /* just a hot-add of a new device, leave raid_disk at -1 */
1206 if (mddev
->level
!= LEVEL_MULTIPATH
) {
1207 desc
= sb
->disks
+ rdev
->desc_nr
;
1209 if (desc
->state
& (1<<MD_DISK_FAULTY
))
1210 set_bit(Faulty
, &rdev
->flags
);
1211 else if (desc
->state
& (1<<MD_DISK_SYNC
) /* &&
1212 desc->raid_disk < mddev->raid_disks */) {
1213 set_bit(In_sync
, &rdev
->flags
);
1214 rdev
->raid_disk
= desc
->raid_disk
;
1215 } else if (desc
->state
& (1<<MD_DISK_ACTIVE
)) {
1216 /* active but not in sync implies recovery up to
1217 * reshape position. We don't know exactly where
1218 * that is, so set to zero for now */
1219 if (mddev
->minor_version
>= 91) {
1220 rdev
->recovery_offset
= 0;
1221 rdev
->raid_disk
= desc
->raid_disk
;
1224 if (desc
->state
& (1<<MD_DISK_WRITEMOSTLY
))
1225 set_bit(WriteMostly
, &rdev
->flags
);
1226 } else /* MULTIPATH are always insync */
1227 set_bit(In_sync
, &rdev
->flags
);
1232 * sync_super for 0.90.0
1234 static void super_90_sync(struct mddev
*mddev
, struct md_rdev
*rdev
)
1237 struct md_rdev
*rdev2
;
1238 int next_spare
= mddev
->raid_disks
;
1241 /* make rdev->sb match mddev data..
1244 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1245 * 3/ any empty disks < next_spare become removed
1247 * disks[0] gets initialised to REMOVED because
1248 * we cannot be sure from other fields if it has
1249 * been initialised or not.
1252 int active
=0, working
=0,failed
=0,spare
=0,nr_disks
=0;
1254 rdev
->sb_size
= MD_SB_BYTES
;
1256 sb
= page_address(rdev
->sb_page
);
1258 memset(sb
, 0, sizeof(*sb
));
1260 sb
->md_magic
= MD_SB_MAGIC
;
1261 sb
->major_version
= mddev
->major_version
;
1262 sb
->patch_version
= mddev
->patch_version
;
1263 sb
->gvalid_words
= 0; /* ignored */
1264 memcpy(&sb
->set_uuid0
, mddev
->uuid
+0, 4);
1265 memcpy(&sb
->set_uuid1
, mddev
->uuid
+4, 4);
1266 memcpy(&sb
->set_uuid2
, mddev
->uuid
+8, 4);
1267 memcpy(&sb
->set_uuid3
, mddev
->uuid
+12,4);
1269 sb
->ctime
= mddev
->ctime
;
1270 sb
->level
= mddev
->level
;
1271 sb
->size
= mddev
->dev_sectors
/ 2;
1272 sb
->raid_disks
= mddev
->raid_disks
;
1273 sb
->md_minor
= mddev
->md_minor
;
1274 sb
->not_persistent
= 0;
1275 sb
->utime
= mddev
->utime
;
1277 sb
->events_hi
= (mddev
->events
>>32);
1278 sb
->events_lo
= (u32
)mddev
->events
;
1280 if (mddev
->reshape_position
== MaxSector
)
1281 sb
->minor_version
= 90;
1283 sb
->minor_version
= 91;
1284 sb
->reshape_position
= mddev
->reshape_position
;
1285 sb
->new_level
= mddev
->new_level
;
1286 sb
->delta_disks
= mddev
->delta_disks
;
1287 sb
->new_layout
= mddev
->new_layout
;
1288 sb
->new_chunk
= mddev
->new_chunk_sectors
<< 9;
1290 mddev
->minor_version
= sb
->minor_version
;
1293 sb
->recovery_cp
= mddev
->recovery_cp
;
1294 sb
->cp_events_hi
= (mddev
->events
>>32);
1295 sb
->cp_events_lo
= (u32
)mddev
->events
;
1296 if (mddev
->recovery_cp
== MaxSector
)
1297 sb
->state
= (1<< MD_SB_CLEAN
);
1299 sb
->recovery_cp
= 0;
1301 sb
->layout
= mddev
->layout
;
1302 sb
->chunk_size
= mddev
->chunk_sectors
<< 9;
1304 if (mddev
->bitmap
&& mddev
->bitmap_info
.file
== NULL
)
1305 sb
->state
|= (1<<MD_SB_BITMAP_PRESENT
);
1307 sb
->disks
[0].state
= (1<<MD_DISK_REMOVED
);
1308 rdev_for_each(rdev2
, mddev
) {
1311 int is_active
= test_bit(In_sync
, &rdev2
->flags
);
1313 if (rdev2
->raid_disk
>= 0 &&
1314 sb
->minor_version
>= 91)
1315 /* we have nowhere to store the recovery_offset,
1316 * but if it is not below the reshape_position,
1317 * we can piggy-back on that.
1320 if (rdev2
->raid_disk
< 0 ||
1321 test_bit(Faulty
, &rdev2
->flags
))
1324 desc_nr
= rdev2
->raid_disk
;
1326 desc_nr
= next_spare
++;
1327 rdev2
->desc_nr
= desc_nr
;
1328 d
= &sb
->disks
[rdev2
->desc_nr
];
1330 d
->number
= rdev2
->desc_nr
;
1331 d
->major
= MAJOR(rdev2
->bdev
->bd_dev
);
1332 d
->minor
= MINOR(rdev2
->bdev
->bd_dev
);
1334 d
->raid_disk
= rdev2
->raid_disk
;
1336 d
->raid_disk
= rdev2
->desc_nr
; /* compatibility */
1337 if (test_bit(Faulty
, &rdev2
->flags
))
1338 d
->state
= (1<<MD_DISK_FAULTY
);
1339 else if (is_active
) {
1340 d
->state
= (1<<MD_DISK_ACTIVE
);
1341 if (test_bit(In_sync
, &rdev2
->flags
))
1342 d
->state
|= (1<<MD_DISK_SYNC
);
1350 if (test_bit(WriteMostly
, &rdev2
->flags
))
1351 d
->state
|= (1<<MD_DISK_WRITEMOSTLY
);
1353 /* now set the "removed" and "faulty" bits on any missing devices */
1354 for (i
=0 ; i
< mddev
->raid_disks
; i
++) {
1355 mdp_disk_t
*d
= &sb
->disks
[i
];
1356 if (d
->state
== 0 && d
->number
== 0) {
1359 d
->state
= (1<<MD_DISK_REMOVED
);
1360 d
->state
|= (1<<MD_DISK_FAULTY
);
1364 sb
->nr_disks
= nr_disks
;
1365 sb
->active_disks
= active
;
1366 sb
->working_disks
= working
;
1367 sb
->failed_disks
= failed
;
1368 sb
->spare_disks
= spare
;
1370 sb
->this_disk
= sb
->disks
[rdev
->desc_nr
];
1371 sb
->sb_csum
= calc_sb_csum(sb
);
1375 * rdev_size_change for 0.90.0
1377 static unsigned long long
1378 super_90_rdev_size_change(struct md_rdev
*rdev
, sector_t num_sectors
)
1380 if (num_sectors
&& num_sectors
< rdev
->mddev
->dev_sectors
)
1381 return 0; /* component must fit device */
1382 if (rdev
->mddev
->bitmap_info
.offset
)
1383 return 0; /* can't move bitmap */
1384 rdev
->sb_start
= calc_dev_sboffset(rdev
);
1385 if (!num_sectors
|| num_sectors
> rdev
->sb_start
)
1386 num_sectors
= rdev
->sb_start
;
1387 /* Limit to 4TB as metadata cannot record more than that.
1388 * 4TB == 2^32 KB, or 2*2^32 sectors.
1390 if (num_sectors
>= (2ULL << 32) && rdev
->mddev
->level
>= 1)
1391 num_sectors
= (2ULL << 32) - 2;
1392 md_super_write(rdev
->mddev
, rdev
, rdev
->sb_start
, rdev
->sb_size
,
1394 md_super_wait(rdev
->mddev
);
1399 super_90_allow_new_offset(struct md_rdev
*rdev
, unsigned long long new_offset
)
1401 /* non-zero offset changes not possible with v0.90 */
1402 return new_offset
== 0;
1406 * version 1 superblock
1409 static __le32
calc_sb_1_csum(struct mdp_superblock_1
* sb
)
1413 unsigned long long newcsum
;
1414 int size
= 256 + le32_to_cpu(sb
->max_dev
)*2;
1415 __le32
*isuper
= (__le32
*)sb
;
1417 disk_csum
= sb
->sb_csum
;
1420 for (; size
>= 4; size
-= 4)
1421 newcsum
+= le32_to_cpu(*isuper
++);
1424 newcsum
+= le16_to_cpu(*(__le16
*) isuper
);
1426 csum
= (newcsum
& 0xffffffff) + (newcsum
>> 32);
1427 sb
->sb_csum
= disk_csum
;
1428 return cpu_to_le32(csum
);
1431 static int md_set_badblocks(struct badblocks
*bb
, sector_t s
, int sectors
,
1433 static int super_1_load(struct md_rdev
*rdev
, struct md_rdev
*refdev
, int minor_version
)
1435 struct mdp_superblock_1
*sb
;
1439 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
1443 * Calculate the position of the superblock in 512byte sectors.
1444 * It is always aligned to a 4K boundary and
1445 * depeding on minor_version, it can be:
1446 * 0: At least 8K, but less than 12K, from end of device
1447 * 1: At start of device
1448 * 2: 4K from start of device.
1450 switch(minor_version
) {
1452 sb_start
= i_size_read(rdev
->bdev
->bd_inode
) >> 9;
1454 sb_start
&= ~(sector_t
)(4*2-1);
1465 rdev
->sb_start
= sb_start
;
1467 /* superblock is rarely larger than 1K, but it can be larger,
1468 * and it is safe to read 4k, so we do that
1470 ret
= read_disk_sb(rdev
, 4096);
1471 if (ret
) return ret
;
1474 sb
= page_address(rdev
->sb_page
);
1476 if (sb
->magic
!= cpu_to_le32(MD_SB_MAGIC
) ||
1477 sb
->major_version
!= cpu_to_le32(1) ||
1478 le32_to_cpu(sb
->max_dev
) > (4096-256)/2 ||
1479 le64_to_cpu(sb
->super_offset
) != rdev
->sb_start
||
1480 (le32_to_cpu(sb
->feature_map
) & ~MD_FEATURE_ALL
) != 0)
1483 if (calc_sb_1_csum(sb
) != sb
->sb_csum
) {
1484 printk("md: invalid superblock checksum on %s\n",
1485 bdevname(rdev
->bdev
,b
));
1488 if (le64_to_cpu(sb
->data_size
) < 10) {
1489 printk("md: data_size too small on %s\n",
1490 bdevname(rdev
->bdev
,b
));
1495 memcmp(sb
->pad3
, sb
->pad3
+1, sizeof(sb
->pad3
) - sizeof(sb
->pad3
[1])))
1496 /* Some padding is non-zero, might be a new feature */
1499 rdev
->preferred_minor
= 0xffff;
1500 rdev
->data_offset
= le64_to_cpu(sb
->data_offset
);
1501 rdev
->new_data_offset
= rdev
->data_offset
;
1502 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RESHAPE_ACTIVE
) &&
1503 (le32_to_cpu(sb
->feature_map
) & MD_FEATURE_NEW_OFFSET
))
1504 rdev
->new_data_offset
+= (s32
)le32_to_cpu(sb
->new_offset
);
1505 atomic_set(&rdev
->corrected_errors
, le32_to_cpu(sb
->cnt_corrected_read
));
1507 rdev
->sb_size
= le32_to_cpu(sb
->max_dev
) * 2 + 256;
1508 bmask
= queue_logical_block_size(rdev
->bdev
->bd_disk
->queue
)-1;
1509 if (rdev
->sb_size
& bmask
)
1510 rdev
->sb_size
= (rdev
->sb_size
| bmask
) + 1;
1513 && rdev
->data_offset
< sb_start
+ (rdev
->sb_size
/512))
1516 && rdev
->new_data_offset
< sb_start
+ (rdev
->sb_size
/512))
1519 if (sb
->level
== cpu_to_le32(LEVEL_MULTIPATH
))
1522 rdev
->desc_nr
= le32_to_cpu(sb
->dev_number
);
1524 if (!rdev
->bb_page
) {
1525 rdev
->bb_page
= alloc_page(GFP_KERNEL
);
1529 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_BAD_BLOCKS
) &&
1530 rdev
->badblocks
.count
== 0) {
1531 /* need to load the bad block list.
1532 * Currently we limit it to one page.
1538 int sectors
= le16_to_cpu(sb
->bblog_size
);
1539 if (sectors
> (PAGE_SIZE
/ 512))
1541 offset
= le32_to_cpu(sb
->bblog_offset
);
1544 bb_sector
= (long long)offset
;
1545 if (!sync_page_io(rdev
, bb_sector
, sectors
<< 9,
1546 rdev
->bb_page
, READ
, true))
1548 bbp
= (u64
*)page_address(rdev
->bb_page
);
1549 rdev
->badblocks
.shift
= sb
->bblog_shift
;
1550 for (i
= 0 ; i
< (sectors
<< (9-3)) ; i
++, bbp
++) {
1551 u64 bb
= le64_to_cpu(*bbp
);
1552 int count
= bb
& (0x3ff);
1553 u64 sector
= bb
>> 10;
1554 sector
<<= sb
->bblog_shift
;
1555 count
<<= sb
->bblog_shift
;
1558 if (md_set_badblocks(&rdev
->badblocks
,
1559 sector
, count
, 1) == 0)
1562 } else if (sb
->bblog_offset
!= 0)
1563 rdev
->badblocks
.shift
= 0;
1569 struct mdp_superblock_1
*refsb
= page_address(refdev
->sb_page
);
1571 if (memcmp(sb
->set_uuid
, refsb
->set_uuid
, 16) != 0 ||
1572 sb
->level
!= refsb
->level
||
1573 sb
->layout
!= refsb
->layout
||
1574 sb
->chunksize
!= refsb
->chunksize
) {
1575 printk(KERN_WARNING
"md: %s has strangely different"
1576 " superblock to %s\n",
1577 bdevname(rdev
->bdev
,b
),
1578 bdevname(refdev
->bdev
,b2
));
1581 ev1
= le64_to_cpu(sb
->events
);
1582 ev2
= le64_to_cpu(refsb
->events
);
1589 if (minor_version
) {
1590 sectors
= (i_size_read(rdev
->bdev
->bd_inode
) >> 9);
1591 sectors
-= rdev
->data_offset
;
1593 sectors
= rdev
->sb_start
;
1594 if (sectors
< le64_to_cpu(sb
->data_size
))
1596 rdev
->sectors
= le64_to_cpu(sb
->data_size
);
1600 static int super_1_validate(struct mddev
*mddev
, struct md_rdev
*rdev
)
1602 struct mdp_superblock_1
*sb
= page_address(rdev
->sb_page
);
1603 __u64 ev1
= le64_to_cpu(sb
->events
);
1605 rdev
->raid_disk
= -1;
1606 clear_bit(Faulty
, &rdev
->flags
);
1607 clear_bit(In_sync
, &rdev
->flags
);
1608 clear_bit(WriteMostly
, &rdev
->flags
);
1610 if (mddev
->raid_disks
== 0) {
1611 mddev
->major_version
= 1;
1612 mddev
->patch_version
= 0;
1613 mddev
->external
= 0;
1614 mddev
->chunk_sectors
= le32_to_cpu(sb
->chunksize
);
1615 mddev
->ctime
= le64_to_cpu(sb
->ctime
) & ((1ULL << 32)-1);
1616 mddev
->utime
= le64_to_cpu(sb
->utime
) & ((1ULL << 32)-1);
1617 mddev
->level
= le32_to_cpu(sb
->level
);
1618 mddev
->clevel
[0] = 0;
1619 mddev
->layout
= le32_to_cpu(sb
->layout
);
1620 mddev
->raid_disks
= le32_to_cpu(sb
->raid_disks
);
1621 mddev
->dev_sectors
= le64_to_cpu(sb
->size
);
1622 mddev
->events
= ev1
;
1623 mddev
->bitmap_info
.offset
= 0;
1624 mddev
->bitmap_info
.space
= 0;
1625 /* Default location for bitmap is 1K after superblock
1626 * using 3K - total of 4K
1628 mddev
->bitmap_info
.default_offset
= 1024 >> 9;
1629 mddev
->bitmap_info
.default_space
= (4096-1024) >> 9;
1630 mddev
->reshape_backwards
= 0;
1632 mddev
->recovery_cp
= le64_to_cpu(sb
->resync_offset
);
1633 memcpy(mddev
->uuid
, sb
->set_uuid
, 16);
1635 mddev
->max_disks
= (4096-256)/2;
1637 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_BITMAP_OFFSET
) &&
1638 mddev
->bitmap_info
.file
== NULL
) {
1639 mddev
->bitmap_info
.offset
=
1640 (__s32
)le32_to_cpu(sb
->bitmap_offset
);
1641 /* Metadata doesn't record how much space is available.
1642 * For 1.0, we assume we can use up to the superblock
1643 * if before, else to 4K beyond superblock.
1644 * For others, assume no change is possible.
1646 if (mddev
->minor_version
> 0)
1647 mddev
->bitmap_info
.space
= 0;
1648 else if (mddev
->bitmap_info
.offset
> 0)
1649 mddev
->bitmap_info
.space
=
1650 8 - mddev
->bitmap_info
.offset
;
1652 mddev
->bitmap_info
.space
=
1653 -mddev
->bitmap_info
.offset
;
1656 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RESHAPE_ACTIVE
)) {
1657 mddev
->reshape_position
= le64_to_cpu(sb
->reshape_position
);
1658 mddev
->delta_disks
= le32_to_cpu(sb
->delta_disks
);
1659 mddev
->new_level
= le32_to_cpu(sb
->new_level
);
1660 mddev
->new_layout
= le32_to_cpu(sb
->new_layout
);
1661 mddev
->new_chunk_sectors
= le32_to_cpu(sb
->new_chunk
);
1662 if (mddev
->delta_disks
< 0 ||
1663 (mddev
->delta_disks
== 0 &&
1664 (le32_to_cpu(sb
->feature_map
)
1665 & MD_FEATURE_RESHAPE_BACKWARDS
)))
1666 mddev
->reshape_backwards
= 1;
1668 mddev
->reshape_position
= MaxSector
;
1669 mddev
->delta_disks
= 0;
1670 mddev
->new_level
= mddev
->level
;
1671 mddev
->new_layout
= mddev
->layout
;
1672 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
1675 } else if (mddev
->pers
== NULL
) {
1676 /* Insist of good event counter while assembling, except for
1677 * spares (which don't need an event count) */
1679 if (rdev
->desc_nr
>= 0 &&
1680 rdev
->desc_nr
< le32_to_cpu(sb
->max_dev
) &&
1681 le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]) < 0xfffe)
1682 if (ev1
< mddev
->events
)
1684 } else if (mddev
->bitmap
) {
1685 /* If adding to array with a bitmap, then we can accept an
1686 * older device, but not too old.
1688 if (ev1
< mddev
->bitmap
->events_cleared
)
1691 if (ev1
< mddev
->events
)
1692 /* just a hot-add of a new device, leave raid_disk at -1 */
1695 if (mddev
->level
!= LEVEL_MULTIPATH
) {
1697 if (rdev
->desc_nr
< 0 ||
1698 rdev
->desc_nr
>= le32_to_cpu(sb
->max_dev
)) {
1702 role
= le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]);
1704 case 0xffff: /* spare */
1706 case 0xfffe: /* faulty */
1707 set_bit(Faulty
, &rdev
->flags
);
1710 if ((le32_to_cpu(sb
->feature_map
) &
1711 MD_FEATURE_RECOVERY_OFFSET
))
1712 rdev
->recovery_offset
= le64_to_cpu(sb
->recovery_offset
);
1714 set_bit(In_sync
, &rdev
->flags
);
1715 rdev
->raid_disk
= role
;
1718 if (sb
->devflags
& WriteMostly1
)
1719 set_bit(WriteMostly
, &rdev
->flags
);
1720 if (le32_to_cpu(sb
->feature_map
) & MD_FEATURE_REPLACEMENT
)
1721 set_bit(Replacement
, &rdev
->flags
);
1722 } else /* MULTIPATH are always insync */
1723 set_bit(In_sync
, &rdev
->flags
);
1728 static void super_1_sync(struct mddev
*mddev
, struct md_rdev
*rdev
)
1730 struct mdp_superblock_1
*sb
;
1731 struct md_rdev
*rdev2
;
1733 /* make rdev->sb match mddev and rdev data. */
1735 sb
= page_address(rdev
->sb_page
);
1737 sb
->feature_map
= 0;
1739 sb
->recovery_offset
= cpu_to_le64(0);
1740 memset(sb
->pad3
, 0, sizeof(sb
->pad3
));
1742 sb
->utime
= cpu_to_le64((__u64
)mddev
->utime
);
1743 sb
->events
= cpu_to_le64(mddev
->events
);
1745 sb
->resync_offset
= cpu_to_le64(mddev
->recovery_cp
);
1747 sb
->resync_offset
= cpu_to_le64(0);
1749 sb
->cnt_corrected_read
= cpu_to_le32(atomic_read(&rdev
->corrected_errors
));
1751 sb
->raid_disks
= cpu_to_le32(mddev
->raid_disks
);
1752 sb
->size
= cpu_to_le64(mddev
->dev_sectors
);
1753 sb
->chunksize
= cpu_to_le32(mddev
->chunk_sectors
);
1754 sb
->level
= cpu_to_le32(mddev
->level
);
1755 sb
->layout
= cpu_to_le32(mddev
->layout
);
1757 if (test_bit(WriteMostly
, &rdev
->flags
))
1758 sb
->devflags
|= WriteMostly1
;
1760 sb
->devflags
&= ~WriteMostly1
;
1761 sb
->data_offset
= cpu_to_le64(rdev
->data_offset
);
1762 sb
->data_size
= cpu_to_le64(rdev
->sectors
);
1764 if (mddev
->bitmap
&& mddev
->bitmap_info
.file
== NULL
) {
1765 sb
->bitmap_offset
= cpu_to_le32((__u32
)mddev
->bitmap_info
.offset
);
1766 sb
->feature_map
= cpu_to_le32(MD_FEATURE_BITMAP_OFFSET
);
1769 if (rdev
->raid_disk
>= 0 &&
1770 !test_bit(In_sync
, &rdev
->flags
)) {
1772 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET
);
1773 sb
->recovery_offset
=
1774 cpu_to_le64(rdev
->recovery_offset
);
1776 if (test_bit(Replacement
, &rdev
->flags
))
1778 cpu_to_le32(MD_FEATURE_REPLACEMENT
);
1780 if (mddev
->reshape_position
!= MaxSector
) {
1781 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE
);
1782 sb
->reshape_position
= cpu_to_le64(mddev
->reshape_position
);
1783 sb
->new_layout
= cpu_to_le32(mddev
->new_layout
);
1784 sb
->delta_disks
= cpu_to_le32(mddev
->delta_disks
);
1785 sb
->new_level
= cpu_to_le32(mddev
->new_level
);
1786 sb
->new_chunk
= cpu_to_le32(mddev
->new_chunk_sectors
);
1787 if (mddev
->delta_disks
== 0 &&
1788 mddev
->reshape_backwards
)
1790 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS
);
1791 if (rdev
->new_data_offset
!= rdev
->data_offset
) {
1793 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET
);
1794 sb
->new_offset
= cpu_to_le32((__u32
)(rdev
->new_data_offset
1795 - rdev
->data_offset
));
1799 if (rdev
->badblocks
.count
== 0)
1800 /* Nothing to do for bad blocks*/ ;
1801 else if (sb
->bblog_offset
== 0)
1802 /* Cannot record bad blocks on this device */
1803 md_error(mddev
, rdev
);
1805 struct badblocks
*bb
= &rdev
->badblocks
;
1806 u64
*bbp
= (u64
*)page_address(rdev
->bb_page
);
1808 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_BAD_BLOCKS
);
1813 seq
= read_seqbegin(&bb
->lock
);
1815 memset(bbp
, 0xff, PAGE_SIZE
);
1817 for (i
= 0 ; i
< bb
->count
; i
++) {
1818 u64 internal_bb
= p
[i
];
1819 u64 store_bb
= ((BB_OFFSET(internal_bb
) << 10)
1820 | BB_LEN(internal_bb
));
1821 bbp
[i
] = cpu_to_le64(store_bb
);
1824 if (read_seqretry(&bb
->lock
, seq
))
1827 bb
->sector
= (rdev
->sb_start
+
1828 (int)le32_to_cpu(sb
->bblog_offset
));
1829 bb
->size
= le16_to_cpu(sb
->bblog_size
);
1834 rdev_for_each(rdev2
, mddev
)
1835 if (rdev2
->desc_nr
+1 > max_dev
)
1836 max_dev
= rdev2
->desc_nr
+1;
1838 if (max_dev
> le32_to_cpu(sb
->max_dev
)) {
1840 sb
->max_dev
= cpu_to_le32(max_dev
);
1841 rdev
->sb_size
= max_dev
* 2 + 256;
1842 bmask
= queue_logical_block_size(rdev
->bdev
->bd_disk
->queue
)-1;
1843 if (rdev
->sb_size
& bmask
)
1844 rdev
->sb_size
= (rdev
->sb_size
| bmask
) + 1;
1846 max_dev
= le32_to_cpu(sb
->max_dev
);
1848 for (i
=0; i
<max_dev
;i
++)
1849 sb
->dev_roles
[i
] = cpu_to_le16(0xfffe);
1851 rdev_for_each(rdev2
, mddev
) {
1853 if (test_bit(Faulty
, &rdev2
->flags
))
1854 sb
->dev_roles
[i
] = cpu_to_le16(0xfffe);
1855 else if (test_bit(In_sync
, &rdev2
->flags
))
1856 sb
->dev_roles
[i
] = cpu_to_le16(rdev2
->raid_disk
);
1857 else if (rdev2
->raid_disk
>= 0)
1858 sb
->dev_roles
[i
] = cpu_to_le16(rdev2
->raid_disk
);
1860 sb
->dev_roles
[i
] = cpu_to_le16(0xffff);
1863 sb
->sb_csum
= calc_sb_1_csum(sb
);
1866 static unsigned long long
1867 super_1_rdev_size_change(struct md_rdev
*rdev
, sector_t num_sectors
)
1869 struct mdp_superblock_1
*sb
;
1870 sector_t max_sectors
;
1871 if (num_sectors
&& num_sectors
< rdev
->mddev
->dev_sectors
)
1872 return 0; /* component must fit device */
1873 if (rdev
->data_offset
!= rdev
->new_data_offset
)
1874 return 0; /* too confusing */
1875 if (rdev
->sb_start
< rdev
->data_offset
) {
1876 /* minor versions 1 and 2; superblock before data */
1877 max_sectors
= i_size_read(rdev
->bdev
->bd_inode
) >> 9;
1878 max_sectors
-= rdev
->data_offset
;
1879 if (!num_sectors
|| num_sectors
> max_sectors
)
1880 num_sectors
= max_sectors
;
1881 } else if (rdev
->mddev
->bitmap_info
.offset
) {
1882 /* minor version 0 with bitmap we can't move */
1885 /* minor version 0; superblock after data */
1887 sb_start
= (i_size_read(rdev
->bdev
->bd_inode
) >> 9) - 8*2;
1888 sb_start
&= ~(sector_t
)(4*2 - 1);
1889 max_sectors
= rdev
->sectors
+ sb_start
- rdev
->sb_start
;
1890 if (!num_sectors
|| num_sectors
> max_sectors
)
1891 num_sectors
= max_sectors
;
1892 rdev
->sb_start
= sb_start
;
1894 sb
= page_address(rdev
->sb_page
);
1895 sb
->data_size
= cpu_to_le64(num_sectors
);
1896 sb
->super_offset
= rdev
->sb_start
;
1897 sb
->sb_csum
= calc_sb_1_csum(sb
);
1898 md_super_write(rdev
->mddev
, rdev
, rdev
->sb_start
, rdev
->sb_size
,
1900 md_super_wait(rdev
->mddev
);
1906 super_1_allow_new_offset(struct md_rdev
*rdev
,
1907 unsigned long long new_offset
)
1909 /* All necessary checks on new >= old have been done */
1910 struct bitmap
*bitmap
;
1911 if (new_offset
>= rdev
->data_offset
)
1914 /* with 1.0 metadata, there is no metadata to tread on
1915 * so we can always move back */
1916 if (rdev
->mddev
->minor_version
== 0)
1919 /* otherwise we must be sure not to step on
1920 * any metadata, so stay:
1921 * 36K beyond start of superblock
1922 * beyond end of badblocks
1923 * beyond write-intent bitmap
1925 if (rdev
->sb_start
+ (32+4)*2 > new_offset
)
1927 bitmap
= rdev
->mddev
->bitmap
;
1928 if (bitmap
&& !rdev
->mddev
->bitmap_info
.file
&&
1929 rdev
->sb_start
+ rdev
->mddev
->bitmap_info
.offset
+
1930 bitmap
->storage
.file_pages
* (PAGE_SIZE
>>9) > new_offset
)
1932 if (rdev
->badblocks
.sector
+ rdev
->badblocks
.size
> new_offset
)
1938 static struct super_type super_types
[] = {
1941 .owner
= THIS_MODULE
,
1942 .load_super
= super_90_load
,
1943 .validate_super
= super_90_validate
,
1944 .sync_super
= super_90_sync
,
1945 .rdev_size_change
= super_90_rdev_size_change
,
1946 .allow_new_offset
= super_90_allow_new_offset
,
1950 .owner
= THIS_MODULE
,
1951 .load_super
= super_1_load
,
1952 .validate_super
= super_1_validate
,
1953 .sync_super
= super_1_sync
,
1954 .rdev_size_change
= super_1_rdev_size_change
,
1955 .allow_new_offset
= super_1_allow_new_offset
,
1959 static void sync_super(struct mddev
*mddev
, struct md_rdev
*rdev
)
1961 if (mddev
->sync_super
) {
1962 mddev
->sync_super(mddev
, rdev
);
1966 BUG_ON(mddev
->major_version
>= ARRAY_SIZE(super_types
));
1968 super_types
[mddev
->major_version
].sync_super(mddev
, rdev
);
1971 static int match_mddev_units(struct mddev
*mddev1
, struct mddev
*mddev2
)
1973 struct md_rdev
*rdev
, *rdev2
;
1976 rdev_for_each_rcu(rdev
, mddev1
)
1977 rdev_for_each_rcu(rdev2
, mddev2
)
1978 if (rdev
->bdev
->bd_contains
==
1979 rdev2
->bdev
->bd_contains
) {
1987 static LIST_HEAD(pending_raid_disks
);
1990 * Try to register data integrity profile for an mddev
1992 * This is called when an array is started and after a disk has been kicked
1993 * from the array. It only succeeds if all working and active component devices
1994 * are integrity capable with matching profiles.
1996 int md_integrity_register(struct mddev
*mddev
)
1998 struct md_rdev
*rdev
, *reference
= NULL
;
2000 if (list_empty(&mddev
->disks
))
2001 return 0; /* nothing to do */
2002 if (!mddev
->gendisk
|| blk_get_integrity(mddev
->gendisk
))
2003 return 0; /* shouldn't register, or already is */
2004 rdev_for_each(rdev
, mddev
) {
2005 /* skip spares and non-functional disks */
2006 if (test_bit(Faulty
, &rdev
->flags
))
2008 if (rdev
->raid_disk
< 0)
2011 /* Use the first rdev as the reference */
2015 /* does this rdev's profile match the reference profile? */
2016 if (blk_integrity_compare(reference
->bdev
->bd_disk
,
2017 rdev
->bdev
->bd_disk
) < 0)
2020 if (!reference
|| !bdev_get_integrity(reference
->bdev
))
2023 * All component devices are integrity capable and have matching
2024 * profiles, register the common profile for the md device.
2026 if (blk_integrity_register(mddev
->gendisk
,
2027 bdev_get_integrity(reference
->bdev
)) != 0) {
2028 printk(KERN_ERR
"md: failed to register integrity for %s\n",
2032 printk(KERN_NOTICE
"md: data integrity enabled on %s\n", mdname(mddev
));
2033 if (bioset_integrity_create(mddev
->bio_set
, BIO_POOL_SIZE
)) {
2034 printk(KERN_ERR
"md: failed to create integrity pool for %s\n",
2040 EXPORT_SYMBOL(md_integrity_register
);
2042 /* Disable data integrity if non-capable/non-matching disk is being added */
2043 void md_integrity_add_rdev(struct md_rdev
*rdev
, struct mddev
*mddev
)
2045 struct blk_integrity
*bi_rdev
;
2046 struct blk_integrity
*bi_mddev
;
2048 if (!mddev
->gendisk
)
2051 bi_rdev
= bdev_get_integrity(rdev
->bdev
);
2052 bi_mddev
= blk_get_integrity(mddev
->gendisk
);
2054 if (!bi_mddev
) /* nothing to do */
2056 if (rdev
->raid_disk
< 0) /* skip spares */
2058 if (bi_rdev
&& blk_integrity_compare(mddev
->gendisk
,
2059 rdev
->bdev
->bd_disk
) >= 0)
2061 printk(KERN_NOTICE
"disabling data integrity on %s\n", mdname(mddev
));
2062 blk_integrity_unregister(mddev
->gendisk
);
2064 EXPORT_SYMBOL(md_integrity_add_rdev
);
2066 static int bind_rdev_to_array(struct md_rdev
* rdev
, struct mddev
* mddev
)
2068 char b
[BDEVNAME_SIZE
];
2078 /* prevent duplicates */
2079 if (find_rdev(mddev
, rdev
->bdev
->bd_dev
))
2082 /* make sure rdev->sectors exceeds mddev->dev_sectors */
2083 if (rdev
->sectors
&& (mddev
->dev_sectors
== 0 ||
2084 rdev
->sectors
< mddev
->dev_sectors
)) {
2086 /* Cannot change size, so fail
2087 * If mddev->level <= 0, then we don't care
2088 * about aligning sizes (e.g. linear)
2090 if (mddev
->level
> 0)
2093 mddev
->dev_sectors
= rdev
->sectors
;
2096 /* Verify rdev->desc_nr is unique.
2097 * If it is -1, assign a free number, else
2098 * check number is not in use
2100 if (rdev
->desc_nr
< 0) {
2102 if (mddev
->pers
) choice
= mddev
->raid_disks
;
2103 while (find_rdev_nr(mddev
, choice
))
2105 rdev
->desc_nr
= choice
;
2107 if (find_rdev_nr(mddev
, rdev
->desc_nr
))
2110 if (mddev
->max_disks
&& rdev
->desc_nr
>= mddev
->max_disks
) {
2111 printk(KERN_WARNING
"md: %s: array is limited to %d devices\n",
2112 mdname(mddev
), mddev
->max_disks
);
2115 bdevname(rdev
->bdev
,b
);
2116 while ( (s
=strchr(b
, '/')) != NULL
)
2119 rdev
->mddev
= mddev
;
2120 printk(KERN_INFO
"md: bind<%s>\n", b
);
2122 if ((err
= kobject_add(&rdev
->kobj
, &mddev
->kobj
, "dev-%s", b
)))
2125 ko
= &part_to_dev(rdev
->bdev
->bd_part
)->kobj
;
2126 if (sysfs_create_link(&rdev
->kobj
, ko
, "block"))
2127 /* failure here is OK */;
2128 rdev
->sysfs_state
= sysfs_get_dirent_safe(rdev
->kobj
.sd
, "state");
2130 list_add_rcu(&rdev
->same_set
, &mddev
->disks
);
2131 bd_link_disk_holder(rdev
->bdev
, mddev
->gendisk
);
2133 /* May as well allow recovery to be retried once */
2134 mddev
->recovery_disabled
++;
2139 printk(KERN_WARNING
"md: failed to register dev-%s for %s\n",
2144 static void md_delayed_delete(struct work_struct
*ws
)
2146 struct md_rdev
*rdev
= container_of(ws
, struct md_rdev
, del_work
);
2147 kobject_del(&rdev
->kobj
);
2148 kobject_put(&rdev
->kobj
);
2151 static void unbind_rdev_from_array(struct md_rdev
* rdev
)
2153 char b
[BDEVNAME_SIZE
];
2158 bd_unlink_disk_holder(rdev
->bdev
, rdev
->mddev
->gendisk
);
2159 list_del_rcu(&rdev
->same_set
);
2160 printk(KERN_INFO
"md: unbind<%s>\n", bdevname(rdev
->bdev
,b
));
2162 sysfs_remove_link(&rdev
->kobj
, "block");
2163 sysfs_put(rdev
->sysfs_state
);
2164 rdev
->sysfs_state
= NULL
;
2165 rdev
->badblocks
.count
= 0;
2166 /* We need to delay this, otherwise we can deadlock when
2167 * writing to 'remove' to "dev/state". We also need
2168 * to delay it due to rcu usage.
2171 INIT_WORK(&rdev
->del_work
, md_delayed_delete
);
2172 kobject_get(&rdev
->kobj
);
2173 queue_work(md_misc_wq
, &rdev
->del_work
);
2177 * prevent the device from being mounted, repartitioned or
2178 * otherwise reused by a RAID array (or any other kernel
2179 * subsystem), by bd_claiming the device.
2181 static int lock_rdev(struct md_rdev
*rdev
, dev_t dev
, int shared
)
2184 struct block_device
*bdev
;
2185 char b
[BDEVNAME_SIZE
];
2187 bdev
= blkdev_get_by_dev(dev
, FMODE_READ
|FMODE_WRITE
|FMODE_EXCL
,
2188 shared
? (struct md_rdev
*)lock_rdev
: rdev
);
2190 printk(KERN_ERR
"md: could not open %s.\n",
2191 __bdevname(dev
, b
));
2192 return PTR_ERR(bdev
);
2198 static void unlock_rdev(struct md_rdev
*rdev
)
2200 struct block_device
*bdev
= rdev
->bdev
;
2204 blkdev_put(bdev
, FMODE_READ
|FMODE_WRITE
|FMODE_EXCL
);
2207 void md_autodetect_dev(dev_t dev
);
2209 static void export_rdev(struct md_rdev
* rdev
)
2211 char b
[BDEVNAME_SIZE
];
2212 printk(KERN_INFO
"md: export_rdev(%s)\n",
2213 bdevname(rdev
->bdev
,b
));
2216 md_rdev_clear(rdev
);
2218 if (test_bit(AutoDetected
, &rdev
->flags
))
2219 md_autodetect_dev(rdev
->bdev
->bd_dev
);
2222 kobject_put(&rdev
->kobj
);
2225 static void kick_rdev_from_array(struct md_rdev
* rdev
)
2227 unbind_rdev_from_array(rdev
);
2231 static void export_array(struct mddev
*mddev
)
2233 struct md_rdev
*rdev
, *tmp
;
2235 rdev_for_each_safe(rdev
, tmp
, mddev
) {
2240 kick_rdev_from_array(rdev
);
2242 if (!list_empty(&mddev
->disks
))
2244 mddev
->raid_disks
= 0;
2245 mddev
->major_version
= 0;
2248 static void print_desc(mdp_disk_t
*desc
)
2250 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc
->number
,
2251 desc
->major
,desc
->minor
,desc
->raid_disk
,desc
->state
);
2254 static void print_sb_90(mdp_super_t
*sb
)
2259 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
2260 sb
->major_version
, sb
->minor_version
, sb
->patch_version
,
2261 sb
->set_uuid0
, sb
->set_uuid1
, sb
->set_uuid2
, sb
->set_uuid3
,
2263 printk(KERN_INFO
"md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
2264 sb
->level
, sb
->size
, sb
->nr_disks
, sb
->raid_disks
,
2265 sb
->md_minor
, sb
->layout
, sb
->chunk_size
);
2266 printk(KERN_INFO
"md: UT:%08x ST:%d AD:%d WD:%d"
2267 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
2268 sb
->utime
, sb
->state
, sb
->active_disks
, sb
->working_disks
,
2269 sb
->failed_disks
, sb
->spare_disks
,
2270 sb
->sb_csum
, (unsigned long)sb
->events_lo
);
2273 for (i
= 0; i
< MD_SB_DISKS
; i
++) {
2276 desc
= sb
->disks
+ i
;
2277 if (desc
->number
|| desc
->major
|| desc
->minor
||
2278 desc
->raid_disk
|| (desc
->state
&& (desc
->state
!= 4))) {
2279 printk(" D %2d: ", i
);
2283 printk(KERN_INFO
"md: THIS: ");
2284 print_desc(&sb
->this_disk
);
2287 static void print_sb_1(struct mdp_superblock_1
*sb
)
2291 uuid
= sb
->set_uuid
;
2293 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n"
2294 "md: Name: \"%s\" CT:%llu\n",
2295 le32_to_cpu(sb
->major_version
),
2296 le32_to_cpu(sb
->feature_map
),
2299 (unsigned long long)le64_to_cpu(sb
->ctime
)
2300 & MD_SUPERBLOCK_1_TIME_SEC_MASK
);
2302 uuid
= sb
->device_uuid
;
2304 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
2306 "md: Dev:%08x UUID: %pU\n"
2307 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
2308 "md: (MaxDev:%u) \n",
2309 le32_to_cpu(sb
->level
),
2310 (unsigned long long)le64_to_cpu(sb
->size
),
2311 le32_to_cpu(sb
->raid_disks
),
2312 le32_to_cpu(sb
->layout
),
2313 le32_to_cpu(sb
->chunksize
),
2314 (unsigned long long)le64_to_cpu(sb
->data_offset
),
2315 (unsigned long long)le64_to_cpu(sb
->data_size
),
2316 (unsigned long long)le64_to_cpu(sb
->super_offset
),
2317 (unsigned long long)le64_to_cpu(sb
->recovery_offset
),
2318 le32_to_cpu(sb
->dev_number
),
2321 (unsigned long long)le64_to_cpu(sb
->utime
) & MD_SUPERBLOCK_1_TIME_SEC_MASK
,
2322 (unsigned long long)le64_to_cpu(sb
->events
),
2323 (unsigned long long)le64_to_cpu(sb
->resync_offset
),
2324 le32_to_cpu(sb
->sb_csum
),
2325 le32_to_cpu(sb
->max_dev
)
2329 static void print_rdev(struct md_rdev
*rdev
, int major_version
)
2331 char b
[BDEVNAME_SIZE
];
2332 printk(KERN_INFO
"md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
2333 bdevname(rdev
->bdev
, b
), (unsigned long long)rdev
->sectors
,
2334 test_bit(Faulty
, &rdev
->flags
), test_bit(In_sync
, &rdev
->flags
),
2336 if (rdev
->sb_loaded
) {
2337 printk(KERN_INFO
"md: rdev superblock (MJ:%d):\n", major_version
);
2338 switch (major_version
) {
2340 print_sb_90(page_address(rdev
->sb_page
));
2343 print_sb_1(page_address(rdev
->sb_page
));
2347 printk(KERN_INFO
"md: no rdev superblock!\n");
2350 static void md_print_devices(void)
2352 struct list_head
*tmp
;
2353 struct md_rdev
*rdev
;
2354 struct mddev
*mddev
;
2355 char b
[BDEVNAME_SIZE
];
2358 printk("md: **********************************\n");
2359 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
2360 printk("md: **********************************\n");
2361 for_each_mddev(mddev
, tmp
) {
2364 bitmap_print_sb(mddev
->bitmap
);
2366 printk("%s: ", mdname(mddev
));
2367 rdev_for_each(rdev
, mddev
)
2368 printk("<%s>", bdevname(rdev
->bdev
,b
));
2371 rdev_for_each(rdev
, mddev
)
2372 print_rdev(rdev
, mddev
->major_version
);
2374 printk("md: **********************************\n");
2379 static void sync_sbs(struct mddev
* mddev
, int nospares
)
2381 /* Update each superblock (in-memory image), but
2382 * if we are allowed to, skip spares which already
2383 * have the right event counter, or have one earlier
2384 * (which would mean they aren't being marked as dirty
2385 * with the rest of the array)
2387 struct md_rdev
*rdev
;
2388 rdev_for_each(rdev
, mddev
) {
2389 if (rdev
->sb_events
== mddev
->events
||
2391 rdev
->raid_disk
< 0 &&
2392 rdev
->sb_events
+1 == mddev
->events
)) {
2393 /* Don't update this superblock */
2394 rdev
->sb_loaded
= 2;
2396 sync_super(mddev
, rdev
);
2397 rdev
->sb_loaded
= 1;
2402 static void md_update_sb(struct mddev
* mddev
, int force_change
)
2404 struct md_rdev
*rdev
;
2407 int any_badblocks_changed
= 0;
2411 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
2415 /* First make sure individual recovery_offsets are correct */
2416 rdev_for_each(rdev
, mddev
) {
2417 if (rdev
->raid_disk
>= 0 &&
2418 mddev
->delta_disks
>= 0 &&
2419 !test_bit(In_sync
, &rdev
->flags
) &&
2420 mddev
->curr_resync_completed
> rdev
->recovery_offset
)
2421 rdev
->recovery_offset
= mddev
->curr_resync_completed
;
2424 if (!mddev
->persistent
) {
2425 clear_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
2426 clear_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
2427 if (!mddev
->external
) {
2428 clear_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
2429 rdev_for_each(rdev
, mddev
) {
2430 if (rdev
->badblocks
.changed
) {
2431 rdev
->badblocks
.changed
= 0;
2432 md_ack_all_badblocks(&rdev
->badblocks
);
2433 md_error(mddev
, rdev
);
2435 clear_bit(Blocked
, &rdev
->flags
);
2436 clear_bit(BlockedBadBlocks
, &rdev
->flags
);
2437 wake_up(&rdev
->blocked_wait
);
2440 wake_up(&mddev
->sb_wait
);
2444 spin_lock_irq(&mddev
->write_lock
);
2446 mddev
->utime
= get_seconds();
2448 if (test_and_clear_bit(MD_CHANGE_DEVS
, &mddev
->flags
))
2450 if (test_and_clear_bit(MD_CHANGE_CLEAN
, &mddev
->flags
))
2451 /* just a clean<-> dirty transition, possibly leave spares alone,
2452 * though if events isn't the right even/odd, we will have to do
2458 if (mddev
->degraded
)
2459 /* If the array is degraded, then skipping spares is both
2460 * dangerous and fairly pointless.
2461 * Dangerous because a device that was removed from the array
2462 * might have a event_count that still looks up-to-date,
2463 * so it can be re-added without a resync.
2464 * Pointless because if there are any spares to skip,
2465 * then a recovery will happen and soon that array won't
2466 * be degraded any more and the spare can go back to sleep then.
2470 sync_req
= mddev
->in_sync
;
2472 /* If this is just a dirty<->clean transition, and the array is clean
2473 * and 'events' is odd, we can roll back to the previous clean state */
2475 && (mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
)
2476 && mddev
->can_decrease_events
2477 && mddev
->events
!= 1) {
2479 mddev
->can_decrease_events
= 0;
2481 /* otherwise we have to go forward and ... */
2483 mddev
->can_decrease_events
= nospares
;
2486 if (!mddev
->events
) {
2488 * oops, this 64-bit counter should never wrap.
2489 * Either we are in around ~1 trillion A.C., assuming
2490 * 1 reboot per second, or we have a bug:
2496 rdev_for_each(rdev
, mddev
) {
2497 if (rdev
->badblocks
.changed
)
2498 any_badblocks_changed
++;
2499 if (test_bit(Faulty
, &rdev
->flags
))
2500 set_bit(FaultRecorded
, &rdev
->flags
);
2503 sync_sbs(mddev
, nospares
);
2504 spin_unlock_irq(&mddev
->write_lock
);
2506 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2507 mdname(mddev
), mddev
->in_sync
);
2509 bitmap_update_sb(mddev
->bitmap
);
2510 rdev_for_each(rdev
, mddev
) {
2511 char b
[BDEVNAME_SIZE
];
2513 if (rdev
->sb_loaded
!= 1)
2514 continue; /* no noise on spare devices */
2516 if (!test_bit(Faulty
, &rdev
->flags
) &&
2517 rdev
->saved_raid_disk
== -1) {
2518 md_super_write(mddev
,rdev
,
2519 rdev
->sb_start
, rdev
->sb_size
,
2521 pr_debug("md: (write) %s's sb offset: %llu\n",
2522 bdevname(rdev
->bdev
, b
),
2523 (unsigned long long)rdev
->sb_start
);
2524 rdev
->sb_events
= mddev
->events
;
2525 if (rdev
->badblocks
.size
) {
2526 md_super_write(mddev
, rdev
,
2527 rdev
->badblocks
.sector
,
2528 rdev
->badblocks
.size
<< 9,
2530 rdev
->badblocks
.size
= 0;
2533 } else if (test_bit(Faulty
, &rdev
->flags
))
2534 pr_debug("md: %s (skipping faulty)\n",
2535 bdevname(rdev
->bdev
, b
));
2537 pr_debug("(skipping incremental s/r ");
2539 if (mddev
->level
== LEVEL_MULTIPATH
)
2540 /* only need to write one superblock... */
2543 md_super_wait(mddev
);
2544 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
2546 spin_lock_irq(&mddev
->write_lock
);
2547 if (mddev
->in_sync
!= sync_req
||
2548 test_bit(MD_CHANGE_DEVS
, &mddev
->flags
)) {
2549 /* have to write it out again */
2550 spin_unlock_irq(&mddev
->write_lock
);
2553 clear_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
2554 spin_unlock_irq(&mddev
->write_lock
);
2555 wake_up(&mddev
->sb_wait
);
2556 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
2557 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
2559 rdev_for_each(rdev
, mddev
) {
2560 if (test_and_clear_bit(FaultRecorded
, &rdev
->flags
))
2561 clear_bit(Blocked
, &rdev
->flags
);
2563 if (any_badblocks_changed
)
2564 md_ack_all_badblocks(&rdev
->badblocks
);
2565 clear_bit(BlockedBadBlocks
, &rdev
->flags
);
2566 wake_up(&rdev
->blocked_wait
);
2570 /* words written to sysfs files may, or may not, be \n terminated.
2571 * We want to accept with case. For this we use cmd_match.
2573 static int cmd_match(const char *cmd
, const char *str
)
2575 /* See if cmd, written into a sysfs file, matches
2576 * str. They must either be the same, or cmd can
2577 * have a trailing newline
2579 while (*cmd
&& *str
&& *cmd
== *str
) {
2590 struct rdev_sysfs_entry
{
2591 struct attribute attr
;
2592 ssize_t (*show
)(struct md_rdev
*, char *);
2593 ssize_t (*store
)(struct md_rdev
*, const char *, size_t);
2597 state_show(struct md_rdev
*rdev
, char *page
)
2602 if (test_bit(Faulty
, &rdev
->flags
) ||
2603 rdev
->badblocks
.unacked_exist
) {
2604 len
+= sprintf(page
+len
, "%sfaulty",sep
);
2607 if (test_bit(In_sync
, &rdev
->flags
)) {
2608 len
+= sprintf(page
+len
, "%sin_sync",sep
);
2611 if (test_bit(WriteMostly
, &rdev
->flags
)) {
2612 len
+= sprintf(page
+len
, "%swrite_mostly",sep
);
2615 if (test_bit(Blocked
, &rdev
->flags
) ||
2616 (rdev
->badblocks
.unacked_exist
2617 && !test_bit(Faulty
, &rdev
->flags
))) {
2618 len
+= sprintf(page
+len
, "%sblocked", sep
);
2621 if (!test_bit(Faulty
, &rdev
->flags
) &&
2622 !test_bit(In_sync
, &rdev
->flags
)) {
2623 len
+= sprintf(page
+len
, "%sspare", sep
);
2626 if (test_bit(WriteErrorSeen
, &rdev
->flags
)) {
2627 len
+= sprintf(page
+len
, "%swrite_error", sep
);
2630 if (test_bit(WantReplacement
, &rdev
->flags
)) {
2631 len
+= sprintf(page
+len
, "%swant_replacement", sep
);
2634 if (test_bit(Replacement
, &rdev
->flags
)) {
2635 len
+= sprintf(page
+len
, "%sreplacement", sep
);
2639 return len
+sprintf(page
+len
, "\n");
2643 state_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
2646 * faulty - simulates an error
2647 * remove - disconnects the device
2648 * writemostly - sets write_mostly
2649 * -writemostly - clears write_mostly
2650 * blocked - sets the Blocked flags
2651 * -blocked - clears the Blocked and possibly simulates an error
2652 * insync - sets Insync providing device isn't active
2653 * write_error - sets WriteErrorSeen
2654 * -write_error - clears WriteErrorSeen
2657 if (cmd_match(buf
, "faulty") && rdev
->mddev
->pers
) {
2658 md_error(rdev
->mddev
, rdev
);
2659 if (test_bit(Faulty
, &rdev
->flags
))
2663 } else if (cmd_match(buf
, "remove")) {
2664 if (rdev
->raid_disk
>= 0)
2667 struct mddev
*mddev
= rdev
->mddev
;
2668 kick_rdev_from_array(rdev
);
2670 md_update_sb(mddev
, 1);
2671 md_new_event(mddev
);
2674 } else if (cmd_match(buf
, "writemostly")) {
2675 set_bit(WriteMostly
, &rdev
->flags
);
2677 } else if (cmd_match(buf
, "-writemostly")) {
2678 clear_bit(WriteMostly
, &rdev
->flags
);
2680 } else if (cmd_match(buf
, "blocked")) {
2681 set_bit(Blocked
, &rdev
->flags
);
2683 } else if (cmd_match(buf
, "-blocked")) {
2684 if (!test_bit(Faulty
, &rdev
->flags
) &&
2685 rdev
->badblocks
.unacked_exist
) {
2686 /* metadata handler doesn't understand badblocks,
2687 * so we need to fail the device
2689 md_error(rdev
->mddev
, rdev
);
2691 clear_bit(Blocked
, &rdev
->flags
);
2692 clear_bit(BlockedBadBlocks
, &rdev
->flags
);
2693 wake_up(&rdev
->blocked_wait
);
2694 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
2695 md_wakeup_thread(rdev
->mddev
->thread
);
2698 } else if (cmd_match(buf
, "insync") && rdev
->raid_disk
== -1) {
2699 set_bit(In_sync
, &rdev
->flags
);
2701 } else if (cmd_match(buf
, "write_error")) {
2702 set_bit(WriteErrorSeen
, &rdev
->flags
);
2704 } else if (cmd_match(buf
, "-write_error")) {
2705 clear_bit(WriteErrorSeen
, &rdev
->flags
);
2707 } else if (cmd_match(buf
, "want_replacement")) {
2708 /* Any non-spare device that is not a replacement can
2709 * become want_replacement at any time, but we then need to
2710 * check if recovery is needed.
2712 if (rdev
->raid_disk
>= 0 &&
2713 !test_bit(Replacement
, &rdev
->flags
))
2714 set_bit(WantReplacement
, &rdev
->flags
);
2715 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
2716 md_wakeup_thread(rdev
->mddev
->thread
);
2718 } else if (cmd_match(buf
, "-want_replacement")) {
2719 /* Clearing 'want_replacement' is always allowed.
2720 * Once replacements starts it is too late though.
2723 clear_bit(WantReplacement
, &rdev
->flags
);
2724 } else if (cmd_match(buf
, "replacement")) {
2725 /* Can only set a device as a replacement when array has not
2726 * yet been started. Once running, replacement is automatic
2727 * from spares, or by assigning 'slot'.
2729 if (rdev
->mddev
->pers
)
2732 set_bit(Replacement
, &rdev
->flags
);
2735 } else if (cmd_match(buf
, "-replacement")) {
2736 /* Similarly, can only clear Replacement before start */
2737 if (rdev
->mddev
->pers
)
2740 clear_bit(Replacement
, &rdev
->flags
);
2745 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
2746 return err
? err
: len
;
2748 static struct rdev_sysfs_entry rdev_state
=
2749 __ATTR(state
, S_IRUGO
|S_IWUSR
, state_show
, state_store
);
2752 errors_show(struct md_rdev
*rdev
, char *page
)
2754 return sprintf(page
, "%d\n", atomic_read(&rdev
->corrected_errors
));
2758 errors_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
2761 unsigned long n
= simple_strtoul(buf
, &e
, 10);
2762 if (*buf
&& (*e
== 0 || *e
== '\n')) {
2763 atomic_set(&rdev
->corrected_errors
, n
);
2768 static struct rdev_sysfs_entry rdev_errors
=
2769 __ATTR(errors
, S_IRUGO
|S_IWUSR
, errors_show
, errors_store
);
2772 slot_show(struct md_rdev
*rdev
, char *page
)
2774 if (rdev
->raid_disk
< 0)
2775 return sprintf(page
, "none\n");
2777 return sprintf(page
, "%d\n", rdev
->raid_disk
);
2781 slot_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
2785 int slot
= simple_strtoul(buf
, &e
, 10);
2786 if (strncmp(buf
, "none", 4)==0)
2788 else if (e
==buf
|| (*e
&& *e
!= '\n'))
2790 if (rdev
->mddev
->pers
&& slot
== -1) {
2791 /* Setting 'slot' on an active array requires also
2792 * updating the 'rd%d' link, and communicating
2793 * with the personality with ->hot_*_disk.
2794 * For now we only support removing
2795 * failed/spare devices. This normally happens automatically,
2796 * but not when the metadata is externally managed.
2798 if (rdev
->raid_disk
== -1)
2800 /* personality does all needed checks */
2801 if (rdev
->mddev
->pers
->hot_remove_disk
== NULL
)
2803 clear_bit(Blocked
, &rdev
->flags
);
2804 remove_and_add_spares(rdev
->mddev
, rdev
);
2805 if (rdev
->raid_disk
>= 0)
2807 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
2808 md_wakeup_thread(rdev
->mddev
->thread
);
2809 } else if (rdev
->mddev
->pers
) {
2810 /* Activating a spare .. or possibly reactivating
2811 * if we ever get bitmaps working here.
2814 if (rdev
->raid_disk
!= -1)
2817 if (test_bit(MD_RECOVERY_RUNNING
, &rdev
->mddev
->recovery
))
2820 if (rdev
->mddev
->pers
->hot_add_disk
== NULL
)
2823 if (slot
>= rdev
->mddev
->raid_disks
&&
2824 slot
>= rdev
->mddev
->raid_disks
+ rdev
->mddev
->delta_disks
)
2827 rdev
->raid_disk
= slot
;
2828 if (test_bit(In_sync
, &rdev
->flags
))
2829 rdev
->saved_raid_disk
= slot
;
2831 rdev
->saved_raid_disk
= -1;
2832 clear_bit(In_sync
, &rdev
->flags
);
2833 err
= rdev
->mddev
->pers
->
2834 hot_add_disk(rdev
->mddev
, rdev
);
2836 rdev
->raid_disk
= -1;
2839 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
2840 if (sysfs_link_rdev(rdev
->mddev
, rdev
))
2841 /* failure here is OK */;
2842 /* don't wakeup anyone, leave that to userspace. */
2844 if (slot
>= rdev
->mddev
->raid_disks
&&
2845 slot
>= rdev
->mddev
->raid_disks
+ rdev
->mddev
->delta_disks
)
2847 rdev
->raid_disk
= slot
;
2848 /* assume it is working */
2849 clear_bit(Faulty
, &rdev
->flags
);
2850 clear_bit(WriteMostly
, &rdev
->flags
);
2851 set_bit(In_sync
, &rdev
->flags
);
2852 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
2858 static struct rdev_sysfs_entry rdev_slot
=
2859 __ATTR(slot
, S_IRUGO
|S_IWUSR
, slot_show
, slot_store
);
2862 offset_show(struct md_rdev
*rdev
, char *page
)
2864 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->data_offset
);
2868 offset_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
2870 unsigned long long offset
;
2871 if (kstrtoull(buf
, 10, &offset
) < 0)
2873 if (rdev
->mddev
->pers
&& rdev
->raid_disk
>= 0)
2875 if (rdev
->sectors
&& rdev
->mddev
->external
)
2876 /* Must set offset before size, so overlap checks
2879 rdev
->data_offset
= offset
;
2880 rdev
->new_data_offset
= offset
;
2884 static struct rdev_sysfs_entry rdev_offset
=
2885 __ATTR(offset
, S_IRUGO
|S_IWUSR
, offset_show
, offset_store
);
2887 static ssize_t
new_offset_show(struct md_rdev
*rdev
, char *page
)
2889 return sprintf(page
, "%llu\n",
2890 (unsigned long long)rdev
->new_data_offset
);
2893 static ssize_t
new_offset_store(struct md_rdev
*rdev
,
2894 const char *buf
, size_t len
)
2896 unsigned long long new_offset
;
2897 struct mddev
*mddev
= rdev
->mddev
;
2899 if (kstrtoull(buf
, 10, &new_offset
) < 0)
2902 if (mddev
->sync_thread
)
2904 if (new_offset
== rdev
->data_offset
)
2905 /* reset is always permitted */
2907 else if (new_offset
> rdev
->data_offset
) {
2908 /* must not push array size beyond rdev_sectors */
2909 if (new_offset
- rdev
->data_offset
2910 + mddev
->dev_sectors
> rdev
->sectors
)
2913 /* Metadata worries about other space details. */
2915 /* decreasing the offset is inconsistent with a backwards
2918 if (new_offset
< rdev
->data_offset
&&
2919 mddev
->reshape_backwards
)
2921 /* Increasing offset is inconsistent with forwards
2922 * reshape. reshape_direction should be set to
2923 * 'backwards' first.
2925 if (new_offset
> rdev
->data_offset
&&
2926 !mddev
->reshape_backwards
)
2929 if (mddev
->pers
&& mddev
->persistent
&&
2930 !super_types
[mddev
->major_version
]
2931 .allow_new_offset(rdev
, new_offset
))
2933 rdev
->new_data_offset
= new_offset
;
2934 if (new_offset
> rdev
->data_offset
)
2935 mddev
->reshape_backwards
= 1;
2936 else if (new_offset
< rdev
->data_offset
)
2937 mddev
->reshape_backwards
= 0;
2941 static struct rdev_sysfs_entry rdev_new_offset
=
2942 __ATTR(new_offset
, S_IRUGO
|S_IWUSR
, new_offset_show
, new_offset_store
);
2945 rdev_size_show(struct md_rdev
*rdev
, char *page
)
2947 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->sectors
/ 2);
2950 static int overlaps(sector_t s1
, sector_t l1
, sector_t s2
, sector_t l2
)
2952 /* check if two start/length pairs overlap */
2960 static int strict_blocks_to_sectors(const char *buf
, sector_t
*sectors
)
2962 unsigned long long blocks
;
2965 if (kstrtoull(buf
, 10, &blocks
) < 0)
2968 if (blocks
& 1ULL << (8 * sizeof(blocks
) - 1))
2969 return -EINVAL
; /* sector conversion overflow */
2972 if (new != blocks
* 2)
2973 return -EINVAL
; /* unsigned long long to sector_t overflow */
2980 rdev_size_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
2982 struct mddev
*my_mddev
= rdev
->mddev
;
2983 sector_t oldsectors
= rdev
->sectors
;
2986 if (strict_blocks_to_sectors(buf
, §ors
) < 0)
2988 if (rdev
->data_offset
!= rdev
->new_data_offset
)
2989 return -EINVAL
; /* too confusing */
2990 if (my_mddev
->pers
&& rdev
->raid_disk
>= 0) {
2991 if (my_mddev
->persistent
) {
2992 sectors
= super_types
[my_mddev
->major_version
].
2993 rdev_size_change(rdev
, sectors
);
2996 } else if (!sectors
)
2997 sectors
= (i_size_read(rdev
->bdev
->bd_inode
) >> 9) -
2999 if (!my_mddev
->pers
->resize
)
3000 /* Cannot change size for RAID0 or Linear etc */
3003 if (sectors
< my_mddev
->dev_sectors
)
3004 return -EINVAL
; /* component must fit device */
3006 rdev
->sectors
= sectors
;
3007 if (sectors
> oldsectors
&& my_mddev
->external
) {
3008 /* need to check that all other rdevs with the same ->bdev
3009 * do not overlap. We need to unlock the mddev to avoid
3010 * a deadlock. We have already changed rdev->sectors, and if
3011 * we have to change it back, we will have the lock again.
3013 struct mddev
*mddev
;
3015 struct list_head
*tmp
;
3017 mddev_unlock(my_mddev
);
3018 for_each_mddev(mddev
, tmp
) {
3019 struct md_rdev
*rdev2
;
3022 rdev_for_each(rdev2
, mddev
)
3023 if (rdev
->bdev
== rdev2
->bdev
&&
3025 overlaps(rdev
->data_offset
, rdev
->sectors
,
3031 mddev_unlock(mddev
);
3037 mddev_lock(my_mddev
);
3039 /* Someone else could have slipped in a size
3040 * change here, but doing so is just silly.
3041 * We put oldsectors back because we *know* it is
3042 * safe, and trust userspace not to race with
3045 rdev
->sectors
= oldsectors
;
3052 static struct rdev_sysfs_entry rdev_size
=
3053 __ATTR(size
, S_IRUGO
|S_IWUSR
, rdev_size_show
, rdev_size_store
);
3056 static ssize_t
recovery_start_show(struct md_rdev
*rdev
, char *page
)
3058 unsigned long long recovery_start
= rdev
->recovery_offset
;
3060 if (test_bit(In_sync
, &rdev
->flags
) ||
3061 recovery_start
== MaxSector
)
3062 return sprintf(page
, "none\n");
3064 return sprintf(page
, "%llu\n", recovery_start
);
3067 static ssize_t
recovery_start_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
3069 unsigned long long recovery_start
;
3071 if (cmd_match(buf
, "none"))
3072 recovery_start
= MaxSector
;
3073 else if (kstrtoull(buf
, 10, &recovery_start
))
3076 if (rdev
->mddev
->pers
&&
3077 rdev
->raid_disk
>= 0)
3080 rdev
->recovery_offset
= recovery_start
;
3081 if (recovery_start
== MaxSector
)
3082 set_bit(In_sync
, &rdev
->flags
);
3084 clear_bit(In_sync
, &rdev
->flags
);
3088 static struct rdev_sysfs_entry rdev_recovery_start
=
3089 __ATTR(recovery_start
, S_IRUGO
|S_IWUSR
, recovery_start_show
, recovery_start_store
);
3093 badblocks_show(struct badblocks
*bb
, char *page
, int unack
);
3095 badblocks_store(struct badblocks
*bb
, const char *page
, size_t len
, int unack
);
3097 static ssize_t
bb_show(struct md_rdev
*rdev
, char *page
)
3099 return badblocks_show(&rdev
->badblocks
, page
, 0);
3101 static ssize_t
bb_store(struct md_rdev
*rdev
, const char *page
, size_t len
)
3103 int rv
= badblocks_store(&rdev
->badblocks
, page
, len
, 0);
3104 /* Maybe that ack was all we needed */
3105 if (test_and_clear_bit(BlockedBadBlocks
, &rdev
->flags
))
3106 wake_up(&rdev
->blocked_wait
);
3109 static struct rdev_sysfs_entry rdev_bad_blocks
=
3110 __ATTR(bad_blocks
, S_IRUGO
|S_IWUSR
, bb_show
, bb_store
);
3113 static ssize_t
ubb_show(struct md_rdev
*rdev
, char *page
)
3115 return badblocks_show(&rdev
->badblocks
, page
, 1);
3117 static ssize_t
ubb_store(struct md_rdev
*rdev
, const char *page
, size_t len
)
3119 return badblocks_store(&rdev
->badblocks
, page
, len
, 1);
3121 static struct rdev_sysfs_entry rdev_unack_bad_blocks
=
3122 __ATTR(unacknowledged_bad_blocks
, S_IRUGO
|S_IWUSR
, ubb_show
, ubb_store
);
3124 static struct attribute
*rdev_default_attrs
[] = {
3129 &rdev_new_offset
.attr
,
3131 &rdev_recovery_start
.attr
,
3132 &rdev_bad_blocks
.attr
,
3133 &rdev_unack_bad_blocks
.attr
,
3137 rdev_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
3139 struct rdev_sysfs_entry
*entry
= container_of(attr
, struct rdev_sysfs_entry
, attr
);
3140 struct md_rdev
*rdev
= container_of(kobj
, struct md_rdev
, kobj
);
3141 struct mddev
*mddev
= rdev
->mddev
;
3147 rv
= mddev
? mddev_lock(mddev
) : -EBUSY
;
3149 if (rdev
->mddev
== NULL
)
3152 rv
= entry
->show(rdev
, page
);
3153 mddev_unlock(mddev
);
3159 rdev_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
3160 const char *page
, size_t length
)
3162 struct rdev_sysfs_entry
*entry
= container_of(attr
, struct rdev_sysfs_entry
, attr
);
3163 struct md_rdev
*rdev
= container_of(kobj
, struct md_rdev
, kobj
);
3165 struct mddev
*mddev
= rdev
->mddev
;
3169 if (!capable(CAP_SYS_ADMIN
))
3171 rv
= mddev
? mddev_lock(mddev
): -EBUSY
;
3173 if (rdev
->mddev
== NULL
)
3176 rv
= entry
->store(rdev
, page
, length
);
3177 mddev_unlock(mddev
);
3182 static void rdev_free(struct kobject
*ko
)
3184 struct md_rdev
*rdev
= container_of(ko
, struct md_rdev
, kobj
);
3187 static const struct sysfs_ops rdev_sysfs_ops
= {
3188 .show
= rdev_attr_show
,
3189 .store
= rdev_attr_store
,
3191 static struct kobj_type rdev_ktype
= {
3192 .release
= rdev_free
,
3193 .sysfs_ops
= &rdev_sysfs_ops
,
3194 .default_attrs
= rdev_default_attrs
,
3197 int md_rdev_init(struct md_rdev
*rdev
)
3200 rdev
->saved_raid_disk
= -1;
3201 rdev
->raid_disk
= -1;
3203 rdev
->data_offset
= 0;
3204 rdev
->new_data_offset
= 0;
3205 rdev
->sb_events
= 0;
3206 rdev
->last_read_error
.tv_sec
= 0;
3207 rdev
->last_read_error
.tv_nsec
= 0;
3208 rdev
->sb_loaded
= 0;
3209 rdev
->bb_page
= NULL
;
3210 atomic_set(&rdev
->nr_pending
, 0);
3211 atomic_set(&rdev
->read_errors
, 0);
3212 atomic_set(&rdev
->corrected_errors
, 0);
3214 INIT_LIST_HEAD(&rdev
->same_set
);
3215 init_waitqueue_head(&rdev
->blocked_wait
);
3217 /* Add space to store bad block list.
3218 * This reserves the space even on arrays where it cannot
3219 * be used - I wonder if that matters
3221 rdev
->badblocks
.count
= 0;
3222 rdev
->badblocks
.shift
= -1; /* disabled until explicitly enabled */
3223 rdev
->badblocks
.page
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
3224 seqlock_init(&rdev
->badblocks
.lock
);
3225 if (rdev
->badblocks
.page
== NULL
)
3230 EXPORT_SYMBOL_GPL(md_rdev_init
);
3232 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3234 * mark the device faulty if:
3236 * - the device is nonexistent (zero size)
3237 * - the device has no valid superblock
3239 * a faulty rdev _never_ has rdev->sb set.
3241 static struct md_rdev
*md_import_device(dev_t newdev
, int super_format
, int super_minor
)
3243 char b
[BDEVNAME_SIZE
];
3245 struct md_rdev
*rdev
;
3248 rdev
= kzalloc(sizeof(*rdev
), GFP_KERNEL
);
3250 printk(KERN_ERR
"md: could not alloc mem for new device!\n");
3251 return ERR_PTR(-ENOMEM
);
3254 err
= md_rdev_init(rdev
);
3257 err
= alloc_disk_sb(rdev
);
3261 err
= lock_rdev(rdev
, newdev
, super_format
== -2);
3265 kobject_init(&rdev
->kobj
, &rdev_ktype
);
3267 size
= i_size_read(rdev
->bdev
->bd_inode
) >> BLOCK_SIZE_BITS
;
3270 "md: %s has zero or unknown size, marking faulty!\n",
3271 bdevname(rdev
->bdev
,b
));
3276 if (super_format
>= 0) {
3277 err
= super_types
[super_format
].
3278 load_super(rdev
, NULL
, super_minor
);
3279 if (err
== -EINVAL
) {
3281 "md: %s does not have a valid v%d.%d "
3282 "superblock, not importing!\n",
3283 bdevname(rdev
->bdev
,b
),
3284 super_format
, super_minor
);
3289 "md: could not read %s's sb, not importing!\n",
3290 bdevname(rdev
->bdev
,b
));
3300 md_rdev_clear(rdev
);
3302 return ERR_PTR(err
);
3306 * Check a full RAID array for plausibility
3310 static void analyze_sbs(struct mddev
* mddev
)
3313 struct md_rdev
*rdev
, *freshest
, *tmp
;
3314 char b
[BDEVNAME_SIZE
];
3317 rdev_for_each_safe(rdev
, tmp
, mddev
)
3318 switch (super_types
[mddev
->major_version
].
3319 load_super(rdev
, freshest
, mddev
->minor_version
)) {
3327 "md: fatal superblock inconsistency in %s"
3328 " -- removing from array\n",
3329 bdevname(rdev
->bdev
,b
));
3330 kick_rdev_from_array(rdev
);
3334 super_types
[mddev
->major_version
].
3335 validate_super(mddev
, freshest
);
3338 rdev_for_each_safe(rdev
, tmp
, mddev
) {
3339 if (mddev
->max_disks
&&
3340 (rdev
->desc_nr
>= mddev
->max_disks
||
3341 i
> mddev
->max_disks
)) {
3343 "md: %s: %s: only %d devices permitted\n",
3344 mdname(mddev
), bdevname(rdev
->bdev
, b
),
3346 kick_rdev_from_array(rdev
);
3349 if (rdev
!= freshest
)
3350 if (super_types
[mddev
->major_version
].
3351 validate_super(mddev
, rdev
)) {
3352 printk(KERN_WARNING
"md: kicking non-fresh %s"
3354 bdevname(rdev
->bdev
,b
));
3355 kick_rdev_from_array(rdev
);
3358 if (mddev
->level
== LEVEL_MULTIPATH
) {
3359 rdev
->desc_nr
= i
++;
3360 rdev
->raid_disk
= rdev
->desc_nr
;
3361 set_bit(In_sync
, &rdev
->flags
);
3362 } else if (rdev
->raid_disk
>= (mddev
->raid_disks
- min(0, mddev
->delta_disks
))) {
3363 rdev
->raid_disk
= -1;
3364 clear_bit(In_sync
, &rdev
->flags
);
3369 /* Read a fixed-point number.
3370 * Numbers in sysfs attributes should be in "standard" units where
3371 * possible, so time should be in seconds.
3372 * However we internally use a a much smaller unit such as
3373 * milliseconds or jiffies.
3374 * This function takes a decimal number with a possible fractional
3375 * component, and produces an integer which is the result of
3376 * multiplying that number by 10^'scale'.
3377 * all without any floating-point arithmetic.
3379 int strict_strtoul_scaled(const char *cp
, unsigned long *res
, int scale
)
3381 unsigned long result
= 0;
3383 while (isdigit(*cp
) || (*cp
== '.' && decimals
< 0)) {
3386 else if (decimals
< scale
) {
3389 result
= result
* 10 + value
;
3401 while (decimals
< scale
) {
3410 static void md_safemode_timeout(unsigned long data
);
3413 safe_delay_show(struct mddev
*mddev
, char *page
)
3415 int msec
= (mddev
->safemode_delay
*1000)/HZ
;
3416 return sprintf(page
, "%d.%03d\n", msec
/1000, msec
%1000);
3419 safe_delay_store(struct mddev
*mddev
, const char *cbuf
, size_t len
)
3423 if (strict_strtoul_scaled(cbuf
, &msec
, 3) < 0)
3426 mddev
->safemode_delay
= 0;
3428 unsigned long old_delay
= mddev
->safemode_delay
;
3429 mddev
->safemode_delay
= (msec
*HZ
)/1000;
3430 if (mddev
->safemode_delay
== 0)
3431 mddev
->safemode_delay
= 1;
3432 if (mddev
->safemode_delay
< old_delay
)
3433 md_safemode_timeout((unsigned long)mddev
);
3437 static struct md_sysfs_entry md_safe_delay
=
3438 __ATTR(safe_mode_delay
, S_IRUGO
|S_IWUSR
,safe_delay_show
, safe_delay_store
);
3441 level_show(struct mddev
*mddev
, char *page
)
3443 struct md_personality
*p
= mddev
->pers
;
3445 return sprintf(page
, "%s\n", p
->name
);
3446 else if (mddev
->clevel
[0])
3447 return sprintf(page
, "%s\n", mddev
->clevel
);
3448 else if (mddev
->level
!= LEVEL_NONE
)
3449 return sprintf(page
, "%d\n", mddev
->level
);
3455 level_store(struct mddev
*mddev
, const char *buf
, size_t len
)
3459 struct md_personality
*pers
;
3462 struct md_rdev
*rdev
;
3464 if (mddev
->pers
== NULL
) {
3467 if (len
>= sizeof(mddev
->clevel
))
3469 strncpy(mddev
->clevel
, buf
, len
);
3470 if (mddev
->clevel
[len
-1] == '\n')
3472 mddev
->clevel
[len
] = 0;
3473 mddev
->level
= LEVEL_NONE
;
3477 /* request to change the personality. Need to ensure:
3478 * - array is not engaged in resync/recovery/reshape
3479 * - old personality can be suspended
3480 * - new personality will access other array.
3483 if (mddev
->sync_thread
||
3484 mddev
->reshape_position
!= MaxSector
||
3485 mddev
->sysfs_active
)
3488 if (!mddev
->pers
->quiesce
) {
3489 printk(KERN_WARNING
"md: %s: %s does not support online personality change\n",
3490 mdname(mddev
), mddev
->pers
->name
);
3494 /* Now find the new personality */
3495 if (len
== 0 || len
>= sizeof(clevel
))
3497 strncpy(clevel
, buf
, len
);
3498 if (clevel
[len
-1] == '\n')
3501 if (kstrtol(clevel
, 10, &level
))
3504 if (request_module("md-%s", clevel
) != 0)
3505 request_module("md-level-%s", clevel
);
3506 spin_lock(&pers_lock
);
3507 pers
= find_pers(level
, clevel
);
3508 if (!pers
|| !try_module_get(pers
->owner
)) {
3509 spin_unlock(&pers_lock
);
3510 printk(KERN_WARNING
"md: personality %s not loaded\n", clevel
);
3513 spin_unlock(&pers_lock
);
3515 if (pers
== mddev
->pers
) {
3516 /* Nothing to do! */
3517 module_put(pers
->owner
);
3520 if (!pers
->takeover
) {
3521 module_put(pers
->owner
);
3522 printk(KERN_WARNING
"md: %s: %s does not support personality takeover\n",
3523 mdname(mddev
), clevel
);
3527 rdev_for_each(rdev
, mddev
)
3528 rdev
->new_raid_disk
= rdev
->raid_disk
;
3530 /* ->takeover must set new_* and/or delta_disks
3531 * if it succeeds, and may set them when it fails.
3533 priv
= pers
->takeover(mddev
);
3535 mddev
->new_level
= mddev
->level
;
3536 mddev
->new_layout
= mddev
->layout
;
3537 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
3538 mddev
->raid_disks
-= mddev
->delta_disks
;
3539 mddev
->delta_disks
= 0;
3540 mddev
->reshape_backwards
= 0;
3541 module_put(pers
->owner
);
3542 printk(KERN_WARNING
"md: %s: %s would not accept array\n",
3543 mdname(mddev
), clevel
);
3544 return PTR_ERR(priv
);
3547 /* Looks like we have a winner */
3548 mddev_suspend(mddev
);
3549 mddev
->pers
->stop(mddev
);
3551 if (mddev
->pers
->sync_request
== NULL
&&
3552 pers
->sync_request
!= NULL
) {
3553 /* need to add the md_redundancy_group */
3554 if (sysfs_create_group(&mddev
->kobj
, &md_redundancy_group
))
3556 "md: cannot register extra attributes for %s\n",
3558 mddev
->sysfs_action
= sysfs_get_dirent(mddev
->kobj
.sd
, NULL
, "sync_action");
3560 if (mddev
->pers
->sync_request
!= NULL
&&
3561 pers
->sync_request
== NULL
) {
3562 /* need to remove the md_redundancy_group */
3563 if (mddev
->to_remove
== NULL
)
3564 mddev
->to_remove
= &md_redundancy_group
;
3567 if (mddev
->pers
->sync_request
== NULL
&&
3569 /* We are converting from a no-redundancy array
3570 * to a redundancy array and metadata is managed
3571 * externally so we need to be sure that writes
3572 * won't block due to a need to transition
3574 * until external management is started.
3577 mddev
->safemode_delay
= 0;
3578 mddev
->safemode
= 0;
3581 rdev_for_each(rdev
, mddev
) {
3582 if (rdev
->raid_disk
< 0)
3584 if (rdev
->new_raid_disk
>= mddev
->raid_disks
)
3585 rdev
->new_raid_disk
= -1;
3586 if (rdev
->new_raid_disk
== rdev
->raid_disk
)
3588 sysfs_unlink_rdev(mddev
, rdev
);
3590 rdev_for_each(rdev
, mddev
) {
3591 if (rdev
->raid_disk
< 0)
3593 if (rdev
->new_raid_disk
== rdev
->raid_disk
)
3595 rdev
->raid_disk
= rdev
->new_raid_disk
;
3596 if (rdev
->raid_disk
< 0)
3597 clear_bit(In_sync
, &rdev
->flags
);
3599 if (sysfs_link_rdev(mddev
, rdev
))
3600 printk(KERN_WARNING
"md: cannot register rd%d"
3601 " for %s after level change\n",
3602 rdev
->raid_disk
, mdname(mddev
));
3606 module_put(mddev
->pers
->owner
);
3608 mddev
->private = priv
;
3609 strlcpy(mddev
->clevel
, pers
->name
, sizeof(mddev
->clevel
));
3610 mddev
->level
= mddev
->new_level
;
3611 mddev
->layout
= mddev
->new_layout
;
3612 mddev
->chunk_sectors
= mddev
->new_chunk_sectors
;
3613 mddev
->delta_disks
= 0;
3614 mddev
->reshape_backwards
= 0;
3615 mddev
->degraded
= 0;
3616 if (mddev
->pers
->sync_request
== NULL
) {
3617 /* this is now an array without redundancy, so
3618 * it must always be in_sync
3621 del_timer_sync(&mddev
->safemode_timer
);
3624 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
3625 mddev_resume(mddev
);
3626 sysfs_notify(&mddev
->kobj
, NULL
, "level");
3627 md_new_event(mddev
);
3631 static struct md_sysfs_entry md_level
=
3632 __ATTR(level
, S_IRUGO
|S_IWUSR
, level_show
, level_store
);
3636 layout_show(struct mddev
*mddev
, char *page
)
3638 /* just a number, not meaningful for all levels */
3639 if (mddev
->reshape_position
!= MaxSector
&&
3640 mddev
->layout
!= mddev
->new_layout
)
3641 return sprintf(page
, "%d (%d)\n",
3642 mddev
->new_layout
, mddev
->layout
);
3643 return sprintf(page
, "%d\n", mddev
->layout
);
3647 layout_store(struct mddev
*mddev
, const char *buf
, size_t len
)
3650 unsigned long n
= simple_strtoul(buf
, &e
, 10);
3652 if (!*buf
|| (*e
&& *e
!= '\n'))
3657 if (mddev
->pers
->check_reshape
== NULL
)
3659 mddev
->new_layout
= n
;
3660 err
= mddev
->pers
->check_reshape(mddev
);
3662 mddev
->new_layout
= mddev
->layout
;
3666 mddev
->new_layout
= n
;
3667 if (mddev
->reshape_position
== MaxSector
)
3672 static struct md_sysfs_entry md_layout
=
3673 __ATTR(layout
, S_IRUGO
|S_IWUSR
, layout_show
, layout_store
);
3677 raid_disks_show(struct mddev
*mddev
, char *page
)
3679 if (mddev
->raid_disks
== 0)
3681 if (mddev
->reshape_position
!= MaxSector
&&
3682 mddev
->delta_disks
!= 0)
3683 return sprintf(page
, "%d (%d)\n", mddev
->raid_disks
,
3684 mddev
->raid_disks
- mddev
->delta_disks
);
3685 return sprintf(page
, "%d\n", mddev
->raid_disks
);
3688 static int update_raid_disks(struct mddev
*mddev
, int raid_disks
);
3691 raid_disks_store(struct mddev
*mddev
, const char *buf
, size_t len
)
3695 unsigned long n
= simple_strtoul(buf
, &e
, 10);
3697 if (!*buf
|| (*e
&& *e
!= '\n'))
3701 rv
= update_raid_disks(mddev
, n
);
3702 else if (mddev
->reshape_position
!= MaxSector
) {
3703 struct md_rdev
*rdev
;
3704 int olddisks
= mddev
->raid_disks
- mddev
->delta_disks
;
3706 rdev_for_each(rdev
, mddev
) {
3708 rdev
->data_offset
< rdev
->new_data_offset
)
3711 rdev
->data_offset
> rdev
->new_data_offset
)
3714 mddev
->delta_disks
= n
- olddisks
;
3715 mddev
->raid_disks
= n
;
3716 mddev
->reshape_backwards
= (mddev
->delta_disks
< 0);
3718 mddev
->raid_disks
= n
;
3719 return rv
? rv
: len
;
3721 static struct md_sysfs_entry md_raid_disks
=
3722 __ATTR(raid_disks
, S_IRUGO
|S_IWUSR
, raid_disks_show
, raid_disks_store
);
3725 chunk_size_show(struct mddev
*mddev
, char *page
)
3727 if (mddev
->reshape_position
!= MaxSector
&&
3728 mddev
->chunk_sectors
!= mddev
->new_chunk_sectors
)
3729 return sprintf(page
, "%d (%d)\n",
3730 mddev
->new_chunk_sectors
<< 9,
3731 mddev
->chunk_sectors
<< 9);
3732 return sprintf(page
, "%d\n", mddev
->chunk_sectors
<< 9);
3736 chunk_size_store(struct mddev
*mddev
, const char *buf
, size_t len
)
3739 unsigned long n
= simple_strtoul(buf
, &e
, 10);
3741 if (!*buf
|| (*e
&& *e
!= '\n'))
3746 if (mddev
->pers
->check_reshape
== NULL
)
3748 mddev
->new_chunk_sectors
= n
>> 9;
3749 err
= mddev
->pers
->check_reshape(mddev
);
3751 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
3755 mddev
->new_chunk_sectors
= n
>> 9;
3756 if (mddev
->reshape_position
== MaxSector
)
3757 mddev
->chunk_sectors
= n
>> 9;
3761 static struct md_sysfs_entry md_chunk_size
=
3762 __ATTR(chunk_size
, S_IRUGO
|S_IWUSR
, chunk_size_show
, chunk_size_store
);
3765 resync_start_show(struct mddev
*mddev
, char *page
)
3767 if (mddev
->recovery_cp
== MaxSector
)
3768 return sprintf(page
, "none\n");
3769 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->recovery_cp
);
3773 resync_start_store(struct mddev
*mddev
, const char *buf
, size_t len
)
3776 unsigned long long n
= simple_strtoull(buf
, &e
, 10);
3778 if (mddev
->pers
&& !test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
))
3780 if (cmd_match(buf
, "none"))
3782 else if (!*buf
|| (*e
&& *e
!= '\n'))
3785 mddev
->recovery_cp
= n
;
3787 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
3790 static struct md_sysfs_entry md_resync_start
=
3791 __ATTR(resync_start
, S_IRUGO
|S_IWUSR
, resync_start_show
, resync_start_store
);
3794 * The array state can be:
3797 * No devices, no size, no level
3798 * Equivalent to STOP_ARRAY ioctl
3800 * May have some settings, but array is not active
3801 * all IO results in error
3802 * When written, doesn't tear down array, but just stops it
3803 * suspended (not supported yet)
3804 * All IO requests will block. The array can be reconfigured.
3805 * Writing this, if accepted, will block until array is quiescent
3807 * no resync can happen. no superblocks get written.
3808 * write requests fail
3810 * like readonly, but behaves like 'clean' on a write request.
3812 * clean - no pending writes, but otherwise active.
3813 * When written to inactive array, starts without resync
3814 * If a write request arrives then
3815 * if metadata is known, mark 'dirty' and switch to 'active'.
3816 * if not known, block and switch to write-pending
3817 * If written to an active array that has pending writes, then fails.
3819 * fully active: IO and resync can be happening.
3820 * When written to inactive array, starts with resync
3823 * clean, but writes are blocked waiting for 'active' to be written.
3826 * like active, but no writes have been seen for a while (100msec).
3829 enum array_state
{ clear
, inactive
, suspended
, readonly
, read_auto
, clean
, active
,
3830 write_pending
, active_idle
, bad_word
};
3831 static char *array_states
[] = {
3832 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3833 "write-pending", "active-idle", NULL
};
3835 static int match_word(const char *word
, char **list
)
3838 for (n
=0; list
[n
]; n
++)
3839 if (cmd_match(word
, list
[n
]))
3845 array_state_show(struct mddev
*mddev
, char *page
)
3847 enum array_state st
= inactive
;
3860 else if (test_bit(MD_CHANGE_PENDING
, &mddev
->flags
))
3862 else if (mddev
->safemode
)
3868 if (list_empty(&mddev
->disks
) &&
3869 mddev
->raid_disks
== 0 &&
3870 mddev
->dev_sectors
== 0)
3875 return sprintf(page
, "%s\n", array_states
[st
]);
3878 static int do_md_stop(struct mddev
* mddev
, int ro
, struct block_device
*bdev
);
3879 static int md_set_readonly(struct mddev
* mddev
, struct block_device
*bdev
);
3880 static int do_md_run(struct mddev
* mddev
);
3881 static int restart_array(struct mddev
*mddev
);
3884 array_state_store(struct mddev
*mddev
, const char *buf
, size_t len
)
3887 enum array_state st
= match_word(buf
, array_states
);
3892 /* stopping an active array */
3893 err
= do_md_stop(mddev
, 0, NULL
);
3896 /* stopping an active array */
3898 err
= do_md_stop(mddev
, 2, NULL
);
3900 err
= 0; /* already inactive */
3903 break; /* not supported yet */
3906 err
= md_set_readonly(mddev
, NULL
);
3909 set_disk_ro(mddev
->gendisk
, 1);
3910 err
= do_md_run(mddev
);
3916 err
= md_set_readonly(mddev
, NULL
);
3917 else if (mddev
->ro
== 1)
3918 err
= restart_array(mddev
);
3921 set_disk_ro(mddev
->gendisk
, 0);
3925 err
= do_md_run(mddev
);
3930 restart_array(mddev
);
3931 spin_lock_irq(&mddev
->write_lock
);
3932 if (atomic_read(&mddev
->writes_pending
) == 0) {
3933 if (mddev
->in_sync
== 0) {
3935 if (mddev
->safemode
== 1)
3936 mddev
->safemode
= 0;
3937 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
3942 spin_unlock_irq(&mddev
->write_lock
);
3948 restart_array(mddev
);
3949 clear_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
3950 wake_up(&mddev
->sb_wait
);
3954 set_disk_ro(mddev
->gendisk
, 0);
3955 err
= do_md_run(mddev
);
3960 /* these cannot be set */
3966 if (mddev
->hold_active
== UNTIL_IOCTL
)
3967 mddev
->hold_active
= 0;
3968 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
3972 static struct md_sysfs_entry md_array_state
=
3973 __ATTR(array_state
, S_IRUGO
|S_IWUSR
, array_state_show
, array_state_store
);
3976 max_corrected_read_errors_show(struct mddev
*mddev
, char *page
) {
3977 return sprintf(page
, "%d\n",
3978 atomic_read(&mddev
->max_corr_read_errors
));
3982 max_corrected_read_errors_store(struct mddev
*mddev
, const char *buf
, size_t len
)
3985 unsigned long n
= simple_strtoul(buf
, &e
, 10);
3987 if (*buf
&& (*e
== 0 || *e
== '\n')) {
3988 atomic_set(&mddev
->max_corr_read_errors
, n
);
3994 static struct md_sysfs_entry max_corr_read_errors
=
3995 __ATTR(max_read_errors
, S_IRUGO
|S_IWUSR
, max_corrected_read_errors_show
,
3996 max_corrected_read_errors_store
);
3999 null_show(struct mddev
*mddev
, char *page
)
4005 new_dev_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4007 /* buf must be %d:%d\n? giving major and minor numbers */
4008 /* The new device is added to the array.
4009 * If the array has a persistent superblock, we read the
4010 * superblock to initialise info and check validity.
4011 * Otherwise, only checking done is that in bind_rdev_to_array,
4012 * which mainly checks size.
4015 int major
= simple_strtoul(buf
, &e
, 10);
4018 struct md_rdev
*rdev
;
4021 if (!*buf
|| *e
!= ':' || !e
[1] || e
[1] == '\n')
4023 minor
= simple_strtoul(e
+1, &e
, 10);
4024 if (*e
&& *e
!= '\n')
4026 dev
= MKDEV(major
, minor
);
4027 if (major
!= MAJOR(dev
) ||
4028 minor
!= MINOR(dev
))
4032 if (mddev
->persistent
) {
4033 rdev
= md_import_device(dev
, mddev
->major_version
,
4034 mddev
->minor_version
);
4035 if (!IS_ERR(rdev
) && !list_empty(&mddev
->disks
)) {
4036 struct md_rdev
*rdev0
4037 = list_entry(mddev
->disks
.next
,
4038 struct md_rdev
, same_set
);
4039 err
= super_types
[mddev
->major_version
]
4040 .load_super(rdev
, rdev0
, mddev
->minor_version
);
4044 } else if (mddev
->external
)
4045 rdev
= md_import_device(dev
, -2, -1);
4047 rdev
= md_import_device(dev
, -1, -1);
4050 return PTR_ERR(rdev
);
4051 err
= bind_rdev_to_array(rdev
, mddev
);
4055 return err
? err
: len
;
4058 static struct md_sysfs_entry md_new_device
=
4059 __ATTR(new_dev
, S_IWUSR
, null_show
, new_dev_store
);
4062 bitmap_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4065 unsigned long chunk
, end_chunk
;
4069 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
4071 chunk
= end_chunk
= simple_strtoul(buf
, &end
, 0);
4072 if (buf
== end
) break;
4073 if (*end
== '-') { /* range */
4075 end_chunk
= simple_strtoul(buf
, &end
, 0);
4076 if (buf
== end
) break;
4078 if (*end
&& !isspace(*end
)) break;
4079 bitmap_dirty_bits(mddev
->bitmap
, chunk
, end_chunk
);
4080 buf
= skip_spaces(end
);
4082 bitmap_unplug(mddev
->bitmap
); /* flush the bits to disk */
4087 static struct md_sysfs_entry md_bitmap
=
4088 __ATTR(bitmap_set_bits
, S_IWUSR
, null_show
, bitmap_store
);
4091 size_show(struct mddev
*mddev
, char *page
)
4093 return sprintf(page
, "%llu\n",
4094 (unsigned long long)mddev
->dev_sectors
/ 2);
4097 static int update_size(struct mddev
*mddev
, sector_t num_sectors
);
4100 size_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4102 /* If array is inactive, we can reduce the component size, but
4103 * not increase it (except from 0).
4104 * If array is active, we can try an on-line resize
4107 int err
= strict_blocks_to_sectors(buf
, §ors
);
4112 err
= update_size(mddev
, sectors
);
4113 md_update_sb(mddev
, 1);
4115 if (mddev
->dev_sectors
== 0 ||
4116 mddev
->dev_sectors
> sectors
)
4117 mddev
->dev_sectors
= sectors
;
4121 return err
? err
: len
;
4124 static struct md_sysfs_entry md_size
=
4125 __ATTR(component_size
, S_IRUGO
|S_IWUSR
, size_show
, size_store
);
4128 /* Metadata version.
4130 * 'none' for arrays with no metadata (good luck...)
4131 * 'external' for arrays with externally managed metadata,
4132 * or N.M for internally known formats
4135 metadata_show(struct mddev
*mddev
, char *page
)
4137 if (mddev
->persistent
)
4138 return sprintf(page
, "%d.%d\n",
4139 mddev
->major_version
, mddev
->minor_version
);
4140 else if (mddev
->external
)
4141 return sprintf(page
, "external:%s\n", mddev
->metadata_type
);
4143 return sprintf(page
, "none\n");
4147 metadata_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4151 /* Changing the details of 'external' metadata is
4152 * always permitted. Otherwise there must be
4153 * no devices attached to the array.
4155 if (mddev
->external
&& strncmp(buf
, "external:", 9) == 0)
4157 else if (!list_empty(&mddev
->disks
))
4160 if (cmd_match(buf
, "none")) {
4161 mddev
->persistent
= 0;
4162 mddev
->external
= 0;
4163 mddev
->major_version
= 0;
4164 mddev
->minor_version
= 90;
4167 if (strncmp(buf
, "external:", 9) == 0) {
4168 size_t namelen
= len
-9;
4169 if (namelen
>= sizeof(mddev
->metadata_type
))
4170 namelen
= sizeof(mddev
->metadata_type
)-1;
4171 strncpy(mddev
->metadata_type
, buf
+9, namelen
);
4172 mddev
->metadata_type
[namelen
] = 0;
4173 if (namelen
&& mddev
->metadata_type
[namelen
-1] == '\n')
4174 mddev
->metadata_type
[--namelen
] = 0;
4175 mddev
->persistent
= 0;
4176 mddev
->external
= 1;
4177 mddev
->major_version
= 0;
4178 mddev
->minor_version
= 90;
4181 major
= simple_strtoul(buf
, &e
, 10);
4182 if (e
==buf
|| *e
!= '.')
4185 minor
= simple_strtoul(buf
, &e
, 10);
4186 if (e
==buf
|| (*e
&& *e
!= '\n') )
4188 if (major
>= ARRAY_SIZE(super_types
) || super_types
[major
].name
== NULL
)
4190 mddev
->major_version
= major
;
4191 mddev
->minor_version
= minor
;
4192 mddev
->persistent
= 1;
4193 mddev
->external
= 0;
4197 static struct md_sysfs_entry md_metadata
=
4198 __ATTR(metadata_version
, S_IRUGO
|S_IWUSR
, metadata_show
, metadata_store
);
4201 action_show(struct mddev
*mddev
, char *page
)
4203 char *type
= "idle";
4204 if (test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
))
4206 else if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
4207 (!mddev
->ro
&& test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))) {
4208 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
4210 else if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
4211 if (!test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
4213 else if (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
))
4217 } else if (test_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
))
4220 return sprintf(page
, "%s\n", type
);
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 md_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
);
4276 static struct md_sysfs_entry md_scan_mode
=
4277 __ATTR(sync_action
, S_IRUGO
|S_IWUSR
, action_show
, action_store
);
4280 last_sync_action_show(struct mddev
*mddev
, char *page
)
4282 return sprintf(page
, "%s\n", mddev
->last_sync_action
);
4285 static struct md_sysfs_entry md_last_scan_mode
= __ATTR_RO(last_sync_action
);
4288 mismatch_cnt_show(struct mddev
*mddev
, char *page
)
4290 return sprintf(page
, "%llu\n",
4291 (unsigned long long)
4292 atomic64_read(&mddev
->resync_mismatches
));
4295 static struct md_sysfs_entry md_mismatches
= __ATTR_RO(mismatch_cnt
);
4298 sync_min_show(struct mddev
*mddev
, char *page
)
4300 return sprintf(page
, "%d (%s)\n", speed_min(mddev
),
4301 mddev
->sync_speed_min
? "local": "system");
4305 sync_min_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4309 if (strncmp(buf
, "system", 6)==0) {
4310 mddev
->sync_speed_min
= 0;
4313 min
= simple_strtoul(buf
, &e
, 10);
4314 if (buf
== e
|| (*e
&& *e
!= '\n') || min
<= 0)
4316 mddev
->sync_speed_min
= min
;
4320 static struct md_sysfs_entry md_sync_min
=
4321 __ATTR(sync_speed_min
, S_IRUGO
|S_IWUSR
, sync_min_show
, sync_min_store
);
4324 sync_max_show(struct mddev
*mddev
, char *page
)
4326 return sprintf(page
, "%d (%s)\n", speed_max(mddev
),
4327 mddev
->sync_speed_max
? "local": "system");
4331 sync_max_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4335 if (strncmp(buf
, "system", 6)==0) {
4336 mddev
->sync_speed_max
= 0;
4339 max
= simple_strtoul(buf
, &e
, 10);
4340 if (buf
== e
|| (*e
&& *e
!= '\n') || max
<= 0)
4342 mddev
->sync_speed_max
= max
;
4346 static struct md_sysfs_entry md_sync_max
=
4347 __ATTR(sync_speed_max
, S_IRUGO
|S_IWUSR
, sync_max_show
, sync_max_store
);
4350 degraded_show(struct mddev
*mddev
, char *page
)
4352 return sprintf(page
, "%d\n", mddev
->degraded
);
4354 static struct md_sysfs_entry md_degraded
= __ATTR_RO(degraded
);
4357 sync_force_parallel_show(struct mddev
*mddev
, char *page
)
4359 return sprintf(page
, "%d\n", mddev
->parallel_resync
);
4363 sync_force_parallel_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4367 if (kstrtol(buf
, 10, &n
))
4370 if (n
!= 0 && n
!= 1)
4373 mddev
->parallel_resync
= n
;
4375 if (mddev
->sync_thread
)
4376 wake_up(&resync_wait
);
4381 /* force parallel resync, even with shared block devices */
4382 static struct md_sysfs_entry md_sync_force_parallel
=
4383 __ATTR(sync_force_parallel
, S_IRUGO
|S_IWUSR
,
4384 sync_force_parallel_show
, sync_force_parallel_store
);
4387 sync_speed_show(struct mddev
*mddev
, char *page
)
4389 unsigned long resync
, dt
, db
;
4390 if (mddev
->curr_resync
== 0)
4391 return sprintf(page
, "none\n");
4392 resync
= mddev
->curr_mark_cnt
- atomic_read(&mddev
->recovery_active
);
4393 dt
= (jiffies
- mddev
->resync_mark
) / HZ
;
4395 db
= resync
- mddev
->resync_mark_cnt
;
4396 return sprintf(page
, "%lu\n", db
/dt
/2); /* K/sec */
4399 static struct md_sysfs_entry md_sync_speed
= __ATTR_RO(sync_speed
);
4402 sync_completed_show(struct mddev
*mddev
, char *page
)
4404 unsigned long long max_sectors
, resync
;
4406 if (!test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
4407 return sprintf(page
, "none\n");
4409 if (mddev
->curr_resync
== 1 ||
4410 mddev
->curr_resync
== 2)
4411 return sprintf(page
, "delayed\n");
4413 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ||
4414 test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
4415 max_sectors
= mddev
->resync_max_sectors
;
4417 max_sectors
= mddev
->dev_sectors
;
4419 resync
= mddev
->curr_resync_completed
;
4420 return sprintf(page
, "%llu / %llu\n", resync
, max_sectors
);
4423 static struct md_sysfs_entry md_sync_completed
= __ATTR_RO(sync_completed
);
4426 min_sync_show(struct mddev
*mddev
, char *page
)
4428 return sprintf(page
, "%llu\n",
4429 (unsigned long long)mddev
->resync_min
);
4432 min_sync_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4434 unsigned long long min
;
4435 if (kstrtoull(buf
, 10, &min
))
4437 if (min
> mddev
->resync_max
)
4439 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
4442 /* Must be a multiple of chunk_size */
4443 if (mddev
->chunk_sectors
) {
4444 sector_t temp
= min
;
4445 if (sector_div(temp
, mddev
->chunk_sectors
))
4448 mddev
->resync_min
= min
;
4453 static struct md_sysfs_entry md_min_sync
=
4454 __ATTR(sync_min
, S_IRUGO
|S_IWUSR
, min_sync_show
, min_sync_store
);
4457 max_sync_show(struct mddev
*mddev
, char *page
)
4459 if (mddev
->resync_max
== MaxSector
)
4460 return sprintf(page
, "max\n");
4462 return sprintf(page
, "%llu\n",
4463 (unsigned long long)mddev
->resync_max
);
4466 max_sync_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4468 if (strncmp(buf
, "max", 3) == 0)
4469 mddev
->resync_max
= MaxSector
;
4471 unsigned long long max
;
4472 if (kstrtoull(buf
, 10, &max
))
4474 if (max
< mddev
->resync_min
)
4476 if (max
< mddev
->resync_max
&&
4478 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
4481 /* Must be a multiple of chunk_size */
4482 if (mddev
->chunk_sectors
) {
4483 sector_t temp
= max
;
4484 if (sector_div(temp
, mddev
->chunk_sectors
))
4487 mddev
->resync_max
= max
;
4489 wake_up(&mddev
->recovery_wait
);
4493 static struct md_sysfs_entry md_max_sync
=
4494 __ATTR(sync_max
, S_IRUGO
|S_IWUSR
, max_sync_show
, max_sync_store
);
4497 suspend_lo_show(struct mddev
*mddev
, char *page
)
4499 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->suspend_lo
);
4503 suspend_lo_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4506 unsigned long long new = simple_strtoull(buf
, &e
, 10);
4507 unsigned long long old
= mddev
->suspend_lo
;
4509 if (mddev
->pers
== NULL
||
4510 mddev
->pers
->quiesce
== NULL
)
4512 if (buf
== e
|| (*e
&& *e
!= '\n'))
4515 mddev
->suspend_lo
= new;
4517 /* Shrinking suspended region */
4518 mddev
->pers
->quiesce(mddev
, 2);
4520 /* Expanding suspended region - need to wait */
4521 mddev
->pers
->quiesce(mddev
, 1);
4522 mddev
->pers
->quiesce(mddev
, 0);
4526 static struct md_sysfs_entry md_suspend_lo
=
4527 __ATTR(suspend_lo
, S_IRUGO
|S_IWUSR
, suspend_lo_show
, suspend_lo_store
);
4531 suspend_hi_show(struct mddev
*mddev
, char *page
)
4533 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->suspend_hi
);
4537 suspend_hi_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4540 unsigned long long new = simple_strtoull(buf
, &e
, 10);
4541 unsigned long long old
= mddev
->suspend_hi
;
4543 if (mddev
->pers
== NULL
||
4544 mddev
->pers
->quiesce
== NULL
)
4546 if (buf
== e
|| (*e
&& *e
!= '\n'))
4549 mddev
->suspend_hi
= new;
4551 /* Shrinking suspended region */
4552 mddev
->pers
->quiesce(mddev
, 2);
4554 /* Expanding suspended region - need to wait */
4555 mddev
->pers
->quiesce(mddev
, 1);
4556 mddev
->pers
->quiesce(mddev
, 0);
4560 static struct md_sysfs_entry md_suspend_hi
=
4561 __ATTR(suspend_hi
, S_IRUGO
|S_IWUSR
, suspend_hi_show
, suspend_hi_store
);
4564 reshape_position_show(struct mddev
*mddev
, char *page
)
4566 if (mddev
->reshape_position
!= MaxSector
)
4567 return sprintf(page
, "%llu\n",
4568 (unsigned long long)mddev
->reshape_position
);
4569 strcpy(page
, "none\n");
4574 reshape_position_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4576 struct md_rdev
*rdev
;
4578 unsigned long long new = simple_strtoull(buf
, &e
, 10);
4581 if (buf
== e
|| (*e
&& *e
!= '\n'))
4583 mddev
->reshape_position
= new;
4584 mddev
->delta_disks
= 0;
4585 mddev
->reshape_backwards
= 0;
4586 mddev
->new_level
= mddev
->level
;
4587 mddev
->new_layout
= mddev
->layout
;
4588 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
4589 rdev_for_each(rdev
, mddev
)
4590 rdev
->new_data_offset
= rdev
->data_offset
;
4594 static struct md_sysfs_entry md_reshape_position
=
4595 __ATTR(reshape_position
, S_IRUGO
|S_IWUSR
, reshape_position_show
,
4596 reshape_position_store
);
4599 reshape_direction_show(struct mddev
*mddev
, char *page
)
4601 return sprintf(page
, "%s\n",
4602 mddev
->reshape_backwards
? "backwards" : "forwards");
4606 reshape_direction_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4609 if (cmd_match(buf
, "forwards"))
4611 else if (cmd_match(buf
, "backwards"))
4615 if (mddev
->reshape_backwards
== backwards
)
4618 /* check if we are allowed to change */
4619 if (mddev
->delta_disks
)
4622 if (mddev
->persistent
&&
4623 mddev
->major_version
== 0)
4626 mddev
->reshape_backwards
= backwards
;
4630 static struct md_sysfs_entry md_reshape_direction
=
4631 __ATTR(reshape_direction
, S_IRUGO
|S_IWUSR
, reshape_direction_show
,
4632 reshape_direction_store
);
4635 array_size_show(struct mddev
*mddev
, char *page
)
4637 if (mddev
->external_size
)
4638 return sprintf(page
, "%llu\n",
4639 (unsigned long long)mddev
->array_sectors
/2);
4641 return sprintf(page
, "default\n");
4645 array_size_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4649 if (strncmp(buf
, "default", 7) == 0) {
4651 sectors
= mddev
->pers
->size(mddev
, 0, 0);
4653 sectors
= mddev
->array_sectors
;
4655 mddev
->external_size
= 0;
4657 if (strict_blocks_to_sectors(buf
, §ors
) < 0)
4659 if (mddev
->pers
&& mddev
->pers
->size(mddev
, 0, 0) < sectors
)
4662 mddev
->external_size
= 1;
4665 mddev
->array_sectors
= sectors
;
4667 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
4668 revalidate_disk(mddev
->gendisk
);
4673 static struct md_sysfs_entry md_array_size
=
4674 __ATTR(array_size
, S_IRUGO
|S_IWUSR
, array_size_show
,
4677 static struct attribute
*md_default_attrs
[] = {
4680 &md_raid_disks
.attr
,
4681 &md_chunk_size
.attr
,
4683 &md_resync_start
.attr
,
4685 &md_new_device
.attr
,
4686 &md_safe_delay
.attr
,
4687 &md_array_state
.attr
,
4688 &md_reshape_position
.attr
,
4689 &md_reshape_direction
.attr
,
4690 &md_array_size
.attr
,
4691 &max_corr_read_errors
.attr
,
4695 static struct attribute
*md_redundancy_attrs
[] = {
4697 &md_last_scan_mode
.attr
,
4698 &md_mismatches
.attr
,
4701 &md_sync_speed
.attr
,
4702 &md_sync_force_parallel
.attr
,
4703 &md_sync_completed
.attr
,
4706 &md_suspend_lo
.attr
,
4707 &md_suspend_hi
.attr
,
4712 static struct attribute_group md_redundancy_group
= {
4714 .attrs
= md_redundancy_attrs
,
4719 md_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
4721 struct md_sysfs_entry
*entry
= container_of(attr
, struct md_sysfs_entry
, attr
);
4722 struct mddev
*mddev
= container_of(kobj
, struct mddev
, kobj
);
4727 spin_lock(&all_mddevs_lock
);
4728 if (list_empty(&mddev
->all_mddevs
)) {
4729 spin_unlock(&all_mddevs_lock
);
4733 spin_unlock(&all_mddevs_lock
);
4735 rv
= mddev_lock(mddev
);
4737 rv
= entry
->show(mddev
, page
);
4738 mddev_unlock(mddev
);
4745 md_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
4746 const char *page
, size_t length
)
4748 struct md_sysfs_entry
*entry
= container_of(attr
, struct md_sysfs_entry
, attr
);
4749 struct mddev
*mddev
= container_of(kobj
, struct mddev
, kobj
);
4754 if (!capable(CAP_SYS_ADMIN
))
4756 spin_lock(&all_mddevs_lock
);
4757 if (list_empty(&mddev
->all_mddevs
)) {
4758 spin_unlock(&all_mddevs_lock
);
4762 spin_unlock(&all_mddevs_lock
);
4763 if (entry
->store
== new_dev_store
)
4764 flush_workqueue(md_misc_wq
);
4765 rv
= mddev_lock(mddev
);
4767 rv
= entry
->store(mddev
, page
, length
);
4768 mddev_unlock(mddev
);
4774 static void md_free(struct kobject
*ko
)
4776 struct mddev
*mddev
= container_of(ko
, struct mddev
, kobj
);
4778 if (mddev
->sysfs_state
)
4779 sysfs_put(mddev
->sysfs_state
);
4781 if (mddev
->gendisk
) {
4782 del_gendisk(mddev
->gendisk
);
4783 put_disk(mddev
->gendisk
);
4786 blk_cleanup_queue(mddev
->queue
);
4791 static const struct sysfs_ops md_sysfs_ops
= {
4792 .show
= md_attr_show
,
4793 .store
= md_attr_store
,
4795 static struct kobj_type md_ktype
= {
4797 .sysfs_ops
= &md_sysfs_ops
,
4798 .default_attrs
= md_default_attrs
,
4803 static void mddev_delayed_delete(struct work_struct
*ws
)
4805 struct mddev
*mddev
= container_of(ws
, struct mddev
, del_work
);
4807 sysfs_remove_group(&mddev
->kobj
, &md_bitmap_group
);
4808 kobject_del(&mddev
->kobj
);
4809 kobject_put(&mddev
->kobj
);
4812 static int md_alloc(dev_t dev
, char *name
)
4814 static DEFINE_MUTEX(disks_mutex
);
4815 struct mddev
*mddev
= mddev_find(dev
);
4816 struct gendisk
*disk
;
4825 partitioned
= (MAJOR(mddev
->unit
) != MD_MAJOR
);
4826 shift
= partitioned
? MdpMinorShift
: 0;
4827 unit
= MINOR(mddev
->unit
) >> shift
;
4829 /* wait for any previous instance of this device to be
4830 * completely removed (mddev_delayed_delete).
4832 flush_workqueue(md_misc_wq
);
4834 mutex_lock(&disks_mutex
);
4840 /* Need to ensure that 'name' is not a duplicate.
4842 struct mddev
*mddev2
;
4843 spin_lock(&all_mddevs_lock
);
4845 list_for_each_entry(mddev2
, &all_mddevs
, all_mddevs
)
4846 if (mddev2
->gendisk
&&
4847 strcmp(mddev2
->gendisk
->disk_name
, name
) == 0) {
4848 spin_unlock(&all_mddevs_lock
);
4851 spin_unlock(&all_mddevs_lock
);
4855 mddev
->queue
= blk_alloc_queue(GFP_KERNEL
);
4858 mddev
->queue
->queuedata
= mddev
;
4860 blk_queue_make_request(mddev
->queue
, md_make_request
);
4861 blk_set_stacking_limits(&mddev
->queue
->limits
);
4863 disk
= alloc_disk(1 << shift
);
4865 blk_cleanup_queue(mddev
->queue
);
4866 mddev
->queue
= NULL
;
4869 disk
->major
= MAJOR(mddev
->unit
);
4870 disk
->first_minor
= unit
<< shift
;
4872 strcpy(disk
->disk_name
, name
);
4873 else if (partitioned
)
4874 sprintf(disk
->disk_name
, "md_d%d", unit
);
4876 sprintf(disk
->disk_name
, "md%d", unit
);
4877 disk
->fops
= &md_fops
;
4878 disk
->private_data
= mddev
;
4879 disk
->queue
= mddev
->queue
;
4880 blk_queue_flush(mddev
->queue
, REQ_FLUSH
| REQ_FUA
);
4881 /* Allow extended partitions. This makes the
4882 * 'mdp' device redundant, but we can't really
4885 disk
->flags
|= GENHD_FL_EXT_DEVT
;
4886 mddev
->gendisk
= disk
;
4887 /* As soon as we call add_disk(), another thread could get
4888 * through to md_open, so make sure it doesn't get too far
4890 mutex_lock(&mddev
->open_mutex
);
4893 error
= kobject_init_and_add(&mddev
->kobj
, &md_ktype
,
4894 &disk_to_dev(disk
)->kobj
, "%s", "md");
4896 /* This isn't possible, but as kobject_init_and_add is marked
4897 * __must_check, we must do something with the result
4899 printk(KERN_WARNING
"md: cannot register %s/md - name in use\n",
4903 if (mddev
->kobj
.sd
&&
4904 sysfs_create_group(&mddev
->kobj
, &md_bitmap_group
))
4905 printk(KERN_DEBUG
"pointless warning\n");
4906 mutex_unlock(&mddev
->open_mutex
);
4908 mutex_unlock(&disks_mutex
);
4909 if (!error
&& mddev
->kobj
.sd
) {
4910 kobject_uevent(&mddev
->kobj
, KOBJ_ADD
);
4911 mddev
->sysfs_state
= sysfs_get_dirent_safe(mddev
->kobj
.sd
, "array_state");
4917 static struct kobject
*md_probe(dev_t dev
, int *part
, void *data
)
4919 md_alloc(dev
, NULL
);
4923 static int add_named_array(const char *val
, struct kernel_param
*kp
)
4925 /* val must be "md_*" where * is not all digits.
4926 * We allocate an array with a large free minor number, and
4927 * set the name to val. val must not already be an active name.
4929 int len
= strlen(val
);
4930 char buf
[DISK_NAME_LEN
];
4932 while (len
&& val
[len
-1] == '\n')
4934 if (len
>= DISK_NAME_LEN
)
4936 strlcpy(buf
, val
, len
+1);
4937 if (strncmp(buf
, "md_", 3) != 0)
4939 return md_alloc(0, buf
);
4942 static void md_safemode_timeout(unsigned long data
)
4944 struct mddev
*mddev
= (struct mddev
*) data
;
4946 if (!atomic_read(&mddev
->writes_pending
)) {
4947 mddev
->safemode
= 1;
4948 if (mddev
->external
)
4949 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4951 md_wakeup_thread(mddev
->thread
);
4954 static int start_dirty_degraded
;
4956 int md_run(struct mddev
*mddev
)
4959 struct md_rdev
*rdev
;
4960 struct md_personality
*pers
;
4962 if (list_empty(&mddev
->disks
))
4963 /* cannot run an array with no devices.. */
4968 /* Cannot run until previous stop completes properly */
4969 if (mddev
->sysfs_active
)
4973 * Analyze all RAID superblock(s)
4975 if (!mddev
->raid_disks
) {
4976 if (!mddev
->persistent
)
4981 if (mddev
->level
!= LEVEL_NONE
)
4982 request_module("md-level-%d", mddev
->level
);
4983 else if (mddev
->clevel
[0])
4984 request_module("md-%s", mddev
->clevel
);
4987 * Drop all container device buffers, from now on
4988 * the only valid external interface is through the md
4991 rdev_for_each(rdev
, mddev
) {
4992 if (test_bit(Faulty
, &rdev
->flags
))
4994 sync_blockdev(rdev
->bdev
);
4995 invalidate_bdev(rdev
->bdev
);
4997 /* perform some consistency tests on the device.
4998 * We don't want the data to overlap the metadata,
4999 * Internal Bitmap issues have been handled elsewhere.
5001 if (rdev
->meta_bdev
) {
5002 /* Nothing to check */;
5003 } else if (rdev
->data_offset
< rdev
->sb_start
) {
5004 if (mddev
->dev_sectors
&&
5005 rdev
->data_offset
+ mddev
->dev_sectors
5007 printk("md: %s: data overlaps metadata\n",
5012 if (rdev
->sb_start
+ rdev
->sb_size
/512
5013 > rdev
->data_offset
) {
5014 printk("md: %s: metadata overlaps data\n",
5019 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
5022 if (mddev
->bio_set
== NULL
)
5023 mddev
->bio_set
= bioset_create(BIO_POOL_SIZE
, 0);
5025 spin_lock(&pers_lock
);
5026 pers
= find_pers(mddev
->level
, mddev
->clevel
);
5027 if (!pers
|| !try_module_get(pers
->owner
)) {
5028 spin_unlock(&pers_lock
);
5029 if (mddev
->level
!= LEVEL_NONE
)
5030 printk(KERN_WARNING
"md: personality for level %d is not loaded!\n",
5033 printk(KERN_WARNING
"md: personality for level %s is not loaded!\n",
5038 spin_unlock(&pers_lock
);
5039 if (mddev
->level
!= pers
->level
) {
5040 mddev
->level
= pers
->level
;
5041 mddev
->new_level
= pers
->level
;
5043 strlcpy(mddev
->clevel
, pers
->name
, sizeof(mddev
->clevel
));
5045 if (mddev
->reshape_position
!= MaxSector
&&
5046 pers
->start_reshape
== NULL
) {
5047 /* This personality cannot handle reshaping... */
5049 module_put(pers
->owner
);
5053 if (pers
->sync_request
) {
5054 /* Warn if this is a potentially silly
5057 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
5058 struct md_rdev
*rdev2
;
5061 rdev_for_each(rdev
, mddev
)
5062 rdev_for_each(rdev2
, mddev
) {
5064 rdev
->bdev
->bd_contains
==
5065 rdev2
->bdev
->bd_contains
) {
5067 "%s: WARNING: %s appears to be"
5068 " on the same physical disk as"
5071 bdevname(rdev
->bdev
,b
),
5072 bdevname(rdev2
->bdev
,b2
));
5079 "True protection against single-disk"
5080 " failure might be compromised.\n");
5083 mddev
->recovery
= 0;
5084 /* may be over-ridden by personality */
5085 mddev
->resync_max_sectors
= mddev
->dev_sectors
;
5087 mddev
->ok_start_degraded
= start_dirty_degraded
;
5089 if (start_readonly
&& mddev
->ro
== 0)
5090 mddev
->ro
= 2; /* read-only, but switch on first write */
5092 err
= mddev
->pers
->run(mddev
);
5094 printk(KERN_ERR
"md: pers->run() failed ...\n");
5095 else if (mddev
->pers
->size(mddev
, 0, 0) < mddev
->array_sectors
) {
5096 WARN_ONCE(!mddev
->external_size
, "%s: default size too small,"
5097 " but 'external_size' not in effect?\n", __func__
);
5099 "md: invalid array_size %llu > default size %llu\n",
5100 (unsigned long long)mddev
->array_sectors
/ 2,
5101 (unsigned long long)mddev
->pers
->size(mddev
, 0, 0) / 2);
5103 mddev
->pers
->stop(mddev
);
5105 if (err
== 0 && mddev
->pers
->sync_request
&&
5106 (mddev
->bitmap_info
.file
|| mddev
->bitmap_info
.offset
)) {
5107 err
= bitmap_create(mddev
);
5109 printk(KERN_ERR
"%s: failed to create bitmap (%d)\n",
5110 mdname(mddev
), err
);
5111 mddev
->pers
->stop(mddev
);
5115 module_put(mddev
->pers
->owner
);
5117 bitmap_destroy(mddev
);
5120 if (mddev
->pers
->sync_request
) {
5121 if (mddev
->kobj
.sd
&&
5122 sysfs_create_group(&mddev
->kobj
, &md_redundancy_group
))
5124 "md: cannot register extra attributes for %s\n",
5126 mddev
->sysfs_action
= sysfs_get_dirent_safe(mddev
->kobj
.sd
, "sync_action");
5127 } else if (mddev
->ro
== 2) /* auto-readonly not meaningful */
5130 atomic_set(&mddev
->writes_pending
,0);
5131 atomic_set(&mddev
->max_corr_read_errors
,
5132 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS
);
5133 mddev
->safemode
= 0;
5134 mddev
->safemode_timer
.function
= md_safemode_timeout
;
5135 mddev
->safemode_timer
.data
= (unsigned long) mddev
;
5136 mddev
->safemode_delay
= (200 * HZ
)/1000 +1; /* 200 msec delay */
5140 rdev_for_each(rdev
, mddev
)
5141 if (rdev
->raid_disk
>= 0)
5142 if (sysfs_link_rdev(mddev
, rdev
))
5143 /* failure here is OK */;
5145 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5148 md_update_sb(mddev
, 0);
5150 md_new_event(mddev
);
5151 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
5152 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
5153 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
5156 EXPORT_SYMBOL_GPL(md_run
);
5158 static int do_md_run(struct mddev
*mddev
)
5162 err
= md_run(mddev
);
5165 err
= bitmap_load(mddev
);
5167 bitmap_destroy(mddev
);
5171 md_wakeup_thread(mddev
->thread
);
5172 md_wakeup_thread(mddev
->sync_thread
); /* possibly kick off a reshape */
5174 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
5175 revalidate_disk(mddev
->gendisk
);
5177 kobject_uevent(&disk_to_dev(mddev
->gendisk
)->kobj
, KOBJ_CHANGE
);
5182 static int restart_array(struct mddev
*mddev
)
5184 struct gendisk
*disk
= mddev
->gendisk
;
5186 /* Complain if it has no devices */
5187 if (list_empty(&mddev
->disks
))
5193 mddev
->safemode
= 0;
5195 set_disk_ro(disk
, 0);
5196 printk(KERN_INFO
"md: %s switched to read-write mode.\n",
5198 /* Kick recovery or resync if necessary */
5199 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5200 md_wakeup_thread(mddev
->thread
);
5201 md_wakeup_thread(mddev
->sync_thread
);
5202 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
5206 /* similar to deny_write_access, but accounts for our holding a reference
5207 * to the file ourselves */
5208 static int deny_bitmap_write_access(struct file
* file
)
5210 struct inode
*inode
= file
->f_mapping
->host
;
5212 spin_lock(&inode
->i_lock
);
5213 if (atomic_read(&inode
->i_writecount
) > 1) {
5214 spin_unlock(&inode
->i_lock
);
5217 atomic_set(&inode
->i_writecount
, -1);
5218 spin_unlock(&inode
->i_lock
);
5223 void restore_bitmap_write_access(struct file
*file
)
5225 struct inode
*inode
= file
->f_mapping
->host
;
5227 spin_lock(&inode
->i_lock
);
5228 atomic_set(&inode
->i_writecount
, 1);
5229 spin_unlock(&inode
->i_lock
);
5232 static void md_clean(struct mddev
*mddev
)
5234 mddev
->array_sectors
= 0;
5235 mddev
->external_size
= 0;
5236 mddev
->dev_sectors
= 0;
5237 mddev
->raid_disks
= 0;
5238 mddev
->recovery_cp
= 0;
5239 mddev
->resync_min
= 0;
5240 mddev
->resync_max
= MaxSector
;
5241 mddev
->reshape_position
= MaxSector
;
5242 mddev
->external
= 0;
5243 mddev
->persistent
= 0;
5244 mddev
->level
= LEVEL_NONE
;
5245 mddev
->clevel
[0] = 0;
5248 mddev
->metadata_type
[0] = 0;
5249 mddev
->chunk_sectors
= 0;
5250 mddev
->ctime
= mddev
->utime
= 0;
5252 mddev
->max_disks
= 0;
5254 mddev
->can_decrease_events
= 0;
5255 mddev
->delta_disks
= 0;
5256 mddev
->reshape_backwards
= 0;
5257 mddev
->new_level
= LEVEL_NONE
;
5258 mddev
->new_layout
= 0;
5259 mddev
->new_chunk_sectors
= 0;
5260 mddev
->curr_resync
= 0;
5261 atomic64_set(&mddev
->resync_mismatches
, 0);
5262 mddev
->suspend_lo
= mddev
->suspend_hi
= 0;
5263 mddev
->sync_speed_min
= mddev
->sync_speed_max
= 0;
5264 mddev
->recovery
= 0;
5267 mddev
->degraded
= 0;
5268 mddev
->safemode
= 0;
5269 mddev
->merge_check_needed
= 0;
5270 mddev
->bitmap_info
.offset
= 0;
5271 mddev
->bitmap_info
.default_offset
= 0;
5272 mddev
->bitmap_info
.default_space
= 0;
5273 mddev
->bitmap_info
.chunksize
= 0;
5274 mddev
->bitmap_info
.daemon_sleep
= 0;
5275 mddev
->bitmap_info
.max_write_behind
= 0;
5278 static void __md_stop_writes(struct mddev
*mddev
)
5280 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
5281 if (mddev
->sync_thread
) {
5282 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
5283 md_reap_sync_thread(mddev
);
5286 del_timer_sync(&mddev
->safemode_timer
);
5288 bitmap_flush(mddev
);
5289 md_super_wait(mddev
);
5291 if (mddev
->ro
== 0 &&
5292 (!mddev
->in_sync
|| mddev
->flags
)) {
5293 /* mark array as shutdown cleanly */
5295 md_update_sb(mddev
, 1);
5299 void md_stop_writes(struct mddev
*mddev
)
5302 __md_stop_writes(mddev
);
5303 mddev_unlock(mddev
);
5305 EXPORT_SYMBOL_GPL(md_stop_writes
);
5307 static void __md_stop(struct mddev
*mddev
)
5310 mddev
->pers
->stop(mddev
);
5311 if (mddev
->pers
->sync_request
&& mddev
->to_remove
== NULL
)
5312 mddev
->to_remove
= &md_redundancy_group
;
5313 module_put(mddev
->pers
->owner
);
5315 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
5318 void md_stop(struct mddev
*mddev
)
5320 /* stop the array and free an attached data structures.
5321 * This is called from dm-raid
5324 bitmap_destroy(mddev
);
5326 bioset_free(mddev
->bio_set
);
5329 EXPORT_SYMBOL_GPL(md_stop
);
5331 static int md_set_readonly(struct mddev
*mddev
, struct block_device
*bdev
)
5334 mutex_lock(&mddev
->open_mutex
);
5335 if (atomic_read(&mddev
->openers
) > !!bdev
) {
5336 printk("md: %s still in use.\n",mdname(mddev
));
5341 sync_blockdev(bdev
);
5343 __md_stop_writes(mddev
);
5349 set_disk_ro(mddev
->gendisk
, 1);
5350 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
5351 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
5355 mutex_unlock(&mddev
->open_mutex
);
5360 * 0 - completely stop and dis-assemble array
5361 * 2 - stop but do not disassemble array
5363 static int do_md_stop(struct mddev
* mddev
, int mode
,
5364 struct block_device
*bdev
)
5366 struct gendisk
*disk
= mddev
->gendisk
;
5367 struct md_rdev
*rdev
;
5369 mutex_lock(&mddev
->open_mutex
);
5370 if (atomic_read(&mddev
->openers
) > !!bdev
||
5371 mddev
->sysfs_active
) {
5372 printk("md: %s still in use.\n",mdname(mddev
));
5373 mutex_unlock(&mddev
->open_mutex
);
5377 /* It is possible IO was issued on some other
5378 * open file which was closed before we took ->open_mutex.
5379 * As that was not the last close __blkdev_put will not
5380 * have called sync_blockdev, so we must.
5382 sync_blockdev(bdev
);
5386 set_disk_ro(disk
, 0);
5388 __md_stop_writes(mddev
);
5390 mddev
->queue
->merge_bvec_fn
= NULL
;
5391 mddev
->queue
->backing_dev_info
.congested_fn
= NULL
;
5393 /* tell userspace to handle 'inactive' */
5394 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
5396 rdev_for_each(rdev
, mddev
)
5397 if (rdev
->raid_disk
>= 0)
5398 sysfs_unlink_rdev(mddev
, rdev
);
5400 set_capacity(disk
, 0);
5401 mutex_unlock(&mddev
->open_mutex
);
5403 revalidate_disk(disk
);
5408 mutex_unlock(&mddev
->open_mutex
);
5410 * Free resources if final stop
5413 printk(KERN_INFO
"md: %s stopped.\n", mdname(mddev
));
5415 bitmap_destroy(mddev
);
5416 if (mddev
->bitmap_info
.file
) {
5417 restore_bitmap_write_access(mddev
->bitmap_info
.file
);
5418 fput(mddev
->bitmap_info
.file
);
5419 mddev
->bitmap_info
.file
= NULL
;
5421 mddev
->bitmap_info
.offset
= 0;
5423 export_array(mddev
);
5426 kobject_uevent(&disk_to_dev(mddev
->gendisk
)->kobj
, KOBJ_CHANGE
);
5427 if (mddev
->hold_active
== UNTIL_STOP
)
5428 mddev
->hold_active
= 0;
5430 blk_integrity_unregister(disk
);
5431 md_new_event(mddev
);
5432 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
5437 static void autorun_array(struct mddev
*mddev
)
5439 struct md_rdev
*rdev
;
5442 if (list_empty(&mddev
->disks
))
5445 printk(KERN_INFO
"md: running: ");
5447 rdev_for_each(rdev
, mddev
) {
5448 char b
[BDEVNAME_SIZE
];
5449 printk("<%s>", bdevname(rdev
->bdev
,b
));
5453 err
= do_md_run(mddev
);
5455 printk(KERN_WARNING
"md: do_md_run() returned %d\n", err
);
5456 do_md_stop(mddev
, 0, NULL
);
5461 * lets try to run arrays based on all disks that have arrived
5462 * until now. (those are in pending_raid_disks)
5464 * the method: pick the first pending disk, collect all disks with
5465 * the same UUID, remove all from the pending list and put them into
5466 * the 'same_array' list. Then order this list based on superblock
5467 * update time (freshest comes first), kick out 'old' disks and
5468 * compare superblocks. If everything's fine then run it.
5470 * If "unit" is allocated, then bump its reference count
5472 static void autorun_devices(int part
)
5474 struct md_rdev
*rdev0
, *rdev
, *tmp
;
5475 struct mddev
*mddev
;
5476 char b
[BDEVNAME_SIZE
];
5478 printk(KERN_INFO
"md: autorun ...\n");
5479 while (!list_empty(&pending_raid_disks
)) {
5482 LIST_HEAD(candidates
);
5483 rdev0
= list_entry(pending_raid_disks
.next
,
5484 struct md_rdev
, same_set
);
5486 printk(KERN_INFO
"md: considering %s ...\n",
5487 bdevname(rdev0
->bdev
,b
));
5488 INIT_LIST_HEAD(&candidates
);
5489 rdev_for_each_list(rdev
, tmp
, &pending_raid_disks
)
5490 if (super_90_load(rdev
, rdev0
, 0) >= 0) {
5491 printk(KERN_INFO
"md: adding %s ...\n",
5492 bdevname(rdev
->bdev
,b
));
5493 list_move(&rdev
->same_set
, &candidates
);
5496 * now we have a set of devices, with all of them having
5497 * mostly sane superblocks. It's time to allocate the
5501 dev
= MKDEV(mdp_major
,
5502 rdev0
->preferred_minor
<< MdpMinorShift
);
5503 unit
= MINOR(dev
) >> MdpMinorShift
;
5505 dev
= MKDEV(MD_MAJOR
, rdev0
->preferred_minor
);
5508 if (rdev0
->preferred_minor
!= unit
) {
5509 printk(KERN_INFO
"md: unit number in %s is bad: %d\n",
5510 bdevname(rdev0
->bdev
, b
), rdev0
->preferred_minor
);
5514 md_probe(dev
, NULL
, NULL
);
5515 mddev
= mddev_find(dev
);
5516 if (!mddev
|| !mddev
->gendisk
) {
5520 "md: cannot allocate memory for md drive.\n");
5523 if (mddev_lock(mddev
))
5524 printk(KERN_WARNING
"md: %s locked, cannot run\n",
5526 else if (mddev
->raid_disks
|| mddev
->major_version
5527 || !list_empty(&mddev
->disks
)) {
5529 "md: %s already running, cannot run %s\n",
5530 mdname(mddev
), bdevname(rdev0
->bdev
,b
));
5531 mddev_unlock(mddev
);
5533 printk(KERN_INFO
"md: created %s\n", mdname(mddev
));
5534 mddev
->persistent
= 1;
5535 rdev_for_each_list(rdev
, tmp
, &candidates
) {
5536 list_del_init(&rdev
->same_set
);
5537 if (bind_rdev_to_array(rdev
, mddev
))
5540 autorun_array(mddev
);
5541 mddev_unlock(mddev
);
5543 /* on success, candidates will be empty, on error
5546 rdev_for_each_list(rdev
, tmp
, &candidates
) {
5547 list_del_init(&rdev
->same_set
);
5552 printk(KERN_INFO
"md: ... autorun DONE.\n");
5554 #endif /* !MODULE */
5556 static int get_version(void __user
* arg
)
5560 ver
.major
= MD_MAJOR_VERSION
;
5561 ver
.minor
= MD_MINOR_VERSION
;
5562 ver
.patchlevel
= MD_PATCHLEVEL_VERSION
;
5564 if (copy_to_user(arg
, &ver
, sizeof(ver
)))
5570 static int get_array_info(struct mddev
* mddev
, void __user
* arg
)
5572 mdu_array_info_t info
;
5573 int nr
,working
,insync
,failed
,spare
;
5574 struct md_rdev
*rdev
;
5576 nr
= working
= insync
= failed
= spare
= 0;
5578 rdev_for_each_rcu(rdev
, mddev
) {
5580 if (test_bit(Faulty
, &rdev
->flags
))
5584 if (test_bit(In_sync
, &rdev
->flags
))
5592 info
.major_version
= mddev
->major_version
;
5593 info
.minor_version
= mddev
->minor_version
;
5594 info
.patch_version
= MD_PATCHLEVEL_VERSION
;
5595 info
.ctime
= mddev
->ctime
;
5596 info
.level
= mddev
->level
;
5597 info
.size
= mddev
->dev_sectors
/ 2;
5598 if (info
.size
!= mddev
->dev_sectors
/ 2) /* overflow */
5601 info
.raid_disks
= mddev
->raid_disks
;
5602 info
.md_minor
= mddev
->md_minor
;
5603 info
.not_persistent
= !mddev
->persistent
;
5605 info
.utime
= mddev
->utime
;
5608 info
.state
= (1<<MD_SB_CLEAN
);
5609 if (mddev
->bitmap
&& mddev
->bitmap_info
.offset
)
5610 info
.state
= (1<<MD_SB_BITMAP_PRESENT
);
5611 info
.active_disks
= insync
;
5612 info
.working_disks
= working
;
5613 info
.failed_disks
= failed
;
5614 info
.spare_disks
= spare
;
5616 info
.layout
= mddev
->layout
;
5617 info
.chunk_size
= mddev
->chunk_sectors
<< 9;
5619 if (copy_to_user(arg
, &info
, sizeof(info
)))
5625 static int get_bitmap_file(struct mddev
* mddev
, void __user
* arg
)
5627 mdu_bitmap_file_t
*file
= NULL
; /* too big for stack allocation */
5628 char *ptr
, *buf
= NULL
;
5631 if (md_allow_write(mddev
))
5632 file
= kmalloc(sizeof(*file
), GFP_NOIO
);
5634 file
= kmalloc(sizeof(*file
), GFP_KERNEL
);
5639 /* bitmap disabled, zero the first byte and copy out */
5640 if (!mddev
->bitmap
|| !mddev
->bitmap
->storage
.file
) {
5641 file
->pathname
[0] = '\0';
5645 buf
= kmalloc(sizeof(file
->pathname
), GFP_KERNEL
);
5649 ptr
= d_path(&mddev
->bitmap
->storage
.file
->f_path
,
5650 buf
, sizeof(file
->pathname
));
5654 strcpy(file
->pathname
, ptr
);
5658 if (copy_to_user(arg
, file
, sizeof(*file
)))
5666 static int get_disk_info(struct mddev
* mddev
, void __user
* arg
)
5668 mdu_disk_info_t info
;
5669 struct md_rdev
*rdev
;
5671 if (copy_from_user(&info
, arg
, sizeof(info
)))
5675 rdev
= find_rdev_nr_rcu(mddev
, info
.number
);
5677 info
.major
= MAJOR(rdev
->bdev
->bd_dev
);
5678 info
.minor
= MINOR(rdev
->bdev
->bd_dev
);
5679 info
.raid_disk
= rdev
->raid_disk
;
5681 if (test_bit(Faulty
, &rdev
->flags
))
5682 info
.state
|= (1<<MD_DISK_FAULTY
);
5683 else if (test_bit(In_sync
, &rdev
->flags
)) {
5684 info
.state
|= (1<<MD_DISK_ACTIVE
);
5685 info
.state
|= (1<<MD_DISK_SYNC
);
5687 if (test_bit(WriteMostly
, &rdev
->flags
))
5688 info
.state
|= (1<<MD_DISK_WRITEMOSTLY
);
5690 info
.major
= info
.minor
= 0;
5691 info
.raid_disk
= -1;
5692 info
.state
= (1<<MD_DISK_REMOVED
);
5696 if (copy_to_user(arg
, &info
, sizeof(info
)))
5702 static int add_new_disk(struct mddev
* mddev
, mdu_disk_info_t
*info
)
5704 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
5705 struct md_rdev
*rdev
;
5706 dev_t dev
= MKDEV(info
->major
,info
->minor
);
5708 if (info
->major
!= MAJOR(dev
) || info
->minor
!= MINOR(dev
))
5711 if (!mddev
->raid_disks
) {
5713 /* expecting a device which has a superblock */
5714 rdev
= md_import_device(dev
, mddev
->major_version
, mddev
->minor_version
);
5717 "md: md_import_device returned %ld\n",
5719 return PTR_ERR(rdev
);
5721 if (!list_empty(&mddev
->disks
)) {
5722 struct md_rdev
*rdev0
5723 = list_entry(mddev
->disks
.next
,
5724 struct md_rdev
, same_set
);
5725 err
= super_types
[mddev
->major_version
]
5726 .load_super(rdev
, rdev0
, mddev
->minor_version
);
5729 "md: %s has different UUID to %s\n",
5730 bdevname(rdev
->bdev
,b
),
5731 bdevname(rdev0
->bdev
,b2
));
5736 err
= bind_rdev_to_array(rdev
, mddev
);
5743 * add_new_disk can be used once the array is assembled
5744 * to add "hot spares". They must already have a superblock
5749 if (!mddev
->pers
->hot_add_disk
) {
5751 "%s: personality does not support diskops!\n",
5755 if (mddev
->persistent
)
5756 rdev
= md_import_device(dev
, mddev
->major_version
,
5757 mddev
->minor_version
);
5759 rdev
= md_import_device(dev
, -1, -1);
5762 "md: md_import_device returned %ld\n",
5764 return PTR_ERR(rdev
);
5766 /* set saved_raid_disk if appropriate */
5767 if (!mddev
->persistent
) {
5768 if (info
->state
& (1<<MD_DISK_SYNC
) &&
5769 info
->raid_disk
< mddev
->raid_disks
) {
5770 rdev
->raid_disk
= info
->raid_disk
;
5771 set_bit(In_sync
, &rdev
->flags
);
5773 rdev
->raid_disk
= -1;
5775 super_types
[mddev
->major_version
].
5776 validate_super(mddev
, rdev
);
5777 if ((info
->state
& (1<<MD_DISK_SYNC
)) &&
5778 rdev
->raid_disk
!= info
->raid_disk
) {
5779 /* This was a hot-add request, but events doesn't
5780 * match, so reject it.
5786 if (test_bit(In_sync
, &rdev
->flags
))
5787 rdev
->saved_raid_disk
= rdev
->raid_disk
;
5789 rdev
->saved_raid_disk
= -1;
5791 clear_bit(In_sync
, &rdev
->flags
); /* just to be sure */
5792 if (info
->state
& (1<<MD_DISK_WRITEMOSTLY
))
5793 set_bit(WriteMostly
, &rdev
->flags
);
5795 clear_bit(WriteMostly
, &rdev
->flags
);
5797 rdev
->raid_disk
= -1;
5798 err
= bind_rdev_to_array(rdev
, mddev
);
5799 if (!err
&& !mddev
->pers
->hot_remove_disk
) {
5800 /* If there is hot_add_disk but no hot_remove_disk
5801 * then added disks for geometry changes,
5802 * and should be added immediately.
5804 super_types
[mddev
->major_version
].
5805 validate_super(mddev
, rdev
);
5806 err
= mddev
->pers
->hot_add_disk(mddev
, rdev
);
5808 unbind_rdev_from_array(rdev
);
5813 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
5815 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
5816 if (mddev
->degraded
)
5817 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
5818 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5820 md_new_event(mddev
);
5821 md_wakeup_thread(mddev
->thread
);
5825 /* otherwise, add_new_disk is only allowed
5826 * for major_version==0 superblocks
5828 if (mddev
->major_version
!= 0) {
5829 printk(KERN_WARNING
"%s: ADD_NEW_DISK not supported\n",
5834 if (!(info
->state
& (1<<MD_DISK_FAULTY
))) {
5836 rdev
= md_import_device(dev
, -1, 0);
5839 "md: error, md_import_device() returned %ld\n",
5841 return PTR_ERR(rdev
);
5843 rdev
->desc_nr
= info
->number
;
5844 if (info
->raid_disk
< mddev
->raid_disks
)
5845 rdev
->raid_disk
= info
->raid_disk
;
5847 rdev
->raid_disk
= -1;
5849 if (rdev
->raid_disk
< mddev
->raid_disks
)
5850 if (info
->state
& (1<<MD_DISK_SYNC
))
5851 set_bit(In_sync
, &rdev
->flags
);
5853 if (info
->state
& (1<<MD_DISK_WRITEMOSTLY
))
5854 set_bit(WriteMostly
, &rdev
->flags
);
5856 if (!mddev
->persistent
) {
5857 printk(KERN_INFO
"md: nonpersistent superblock ...\n");
5858 rdev
->sb_start
= i_size_read(rdev
->bdev
->bd_inode
) / 512;
5860 rdev
->sb_start
= calc_dev_sboffset(rdev
);
5861 rdev
->sectors
= rdev
->sb_start
;
5863 err
= bind_rdev_to_array(rdev
, mddev
);
5873 static int hot_remove_disk(struct mddev
* mddev
, dev_t dev
)
5875 char b
[BDEVNAME_SIZE
];
5876 struct md_rdev
*rdev
;
5878 rdev
= find_rdev(mddev
, dev
);
5882 clear_bit(Blocked
, &rdev
->flags
);
5883 remove_and_add_spares(mddev
, rdev
);
5885 if (rdev
->raid_disk
>= 0)
5888 kick_rdev_from_array(rdev
);
5889 md_update_sb(mddev
, 1);
5890 md_new_event(mddev
);
5894 printk(KERN_WARNING
"md: cannot remove active disk %s from %s ...\n",
5895 bdevname(rdev
->bdev
,b
), mdname(mddev
));
5899 static int hot_add_disk(struct mddev
* mddev
, dev_t dev
)
5901 char b
[BDEVNAME_SIZE
];
5903 struct md_rdev
*rdev
;
5908 if (mddev
->major_version
!= 0) {
5909 printk(KERN_WARNING
"%s: HOT_ADD may only be used with"
5910 " version-0 superblocks.\n",
5914 if (!mddev
->pers
->hot_add_disk
) {
5916 "%s: personality does not support diskops!\n",
5921 rdev
= md_import_device(dev
, -1, 0);
5924 "md: error, md_import_device() returned %ld\n",
5929 if (mddev
->persistent
)
5930 rdev
->sb_start
= calc_dev_sboffset(rdev
);
5932 rdev
->sb_start
= i_size_read(rdev
->bdev
->bd_inode
) / 512;
5934 rdev
->sectors
= rdev
->sb_start
;
5936 if (test_bit(Faulty
, &rdev
->flags
)) {
5938 "md: can not hot-add faulty %s disk to %s!\n",
5939 bdevname(rdev
->bdev
,b
), mdname(mddev
));
5943 clear_bit(In_sync
, &rdev
->flags
);
5945 rdev
->saved_raid_disk
= -1;
5946 err
= bind_rdev_to_array(rdev
, mddev
);
5951 * The rest should better be atomic, we can have disk failures
5952 * noticed in interrupt contexts ...
5955 rdev
->raid_disk
= -1;
5957 md_update_sb(mddev
, 1);
5960 * Kick recovery, maybe this spare has to be added to the
5961 * array immediately.
5963 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5964 md_wakeup_thread(mddev
->thread
);
5965 md_new_event(mddev
);
5973 static int set_bitmap_file(struct mddev
*mddev
, int fd
)
5978 if (!mddev
->pers
->quiesce
)
5980 if (mddev
->recovery
|| mddev
->sync_thread
)
5982 /* we should be able to change the bitmap.. */
5988 return -EEXIST
; /* cannot add when bitmap is present */
5989 mddev
->bitmap_info
.file
= fget(fd
);
5991 if (mddev
->bitmap_info
.file
== NULL
) {
5992 printk(KERN_ERR
"%s: error: failed to get bitmap file\n",
5997 err
= deny_bitmap_write_access(mddev
->bitmap_info
.file
);
5999 printk(KERN_ERR
"%s: error: bitmap file is already in use\n",
6001 fput(mddev
->bitmap_info
.file
);
6002 mddev
->bitmap_info
.file
= NULL
;
6005 mddev
->bitmap_info
.offset
= 0; /* file overrides offset */
6006 } else if (mddev
->bitmap
== NULL
)
6007 return -ENOENT
; /* cannot remove what isn't there */
6010 mddev
->pers
->quiesce(mddev
, 1);
6012 err
= bitmap_create(mddev
);
6014 err
= bitmap_load(mddev
);
6016 if (fd
< 0 || err
) {
6017 bitmap_destroy(mddev
);
6018 fd
= -1; /* make sure to put the file */
6020 mddev
->pers
->quiesce(mddev
, 0);
6023 if (mddev
->bitmap_info
.file
) {
6024 restore_bitmap_write_access(mddev
->bitmap_info
.file
);
6025 fput(mddev
->bitmap_info
.file
);
6027 mddev
->bitmap_info
.file
= NULL
;
6034 * set_array_info is used two different ways
6035 * The original usage is when creating a new array.
6036 * In this usage, raid_disks is > 0 and it together with
6037 * level, size, not_persistent,layout,chunksize determine the
6038 * shape of the array.
6039 * This will always create an array with a type-0.90.0 superblock.
6040 * The newer usage is when assembling an array.
6041 * In this case raid_disks will be 0, and the major_version field is
6042 * use to determine which style super-blocks are to be found on the devices.
6043 * The minor and patch _version numbers are also kept incase the
6044 * super_block handler wishes to interpret them.
6046 static int set_array_info(struct mddev
* mddev
, mdu_array_info_t
*info
)
6049 if (info
->raid_disks
== 0) {
6050 /* just setting version number for superblock loading */
6051 if (info
->major_version
< 0 ||
6052 info
->major_version
>= ARRAY_SIZE(super_types
) ||
6053 super_types
[info
->major_version
].name
== NULL
) {
6054 /* maybe try to auto-load a module? */
6056 "md: superblock version %d not known\n",
6057 info
->major_version
);
6060 mddev
->major_version
= info
->major_version
;
6061 mddev
->minor_version
= info
->minor_version
;
6062 mddev
->patch_version
= info
->patch_version
;
6063 mddev
->persistent
= !info
->not_persistent
;
6064 /* ensure mddev_put doesn't delete this now that there
6065 * is some minimal configuration.
6067 mddev
->ctime
= get_seconds();
6070 mddev
->major_version
= MD_MAJOR_VERSION
;
6071 mddev
->minor_version
= MD_MINOR_VERSION
;
6072 mddev
->patch_version
= MD_PATCHLEVEL_VERSION
;
6073 mddev
->ctime
= get_seconds();
6075 mddev
->level
= info
->level
;
6076 mddev
->clevel
[0] = 0;
6077 mddev
->dev_sectors
= 2 * (sector_t
)info
->size
;
6078 mddev
->raid_disks
= info
->raid_disks
;
6079 /* don't set md_minor, it is determined by which /dev/md* was
6082 if (info
->state
& (1<<MD_SB_CLEAN
))
6083 mddev
->recovery_cp
= MaxSector
;
6085 mddev
->recovery_cp
= 0;
6086 mddev
->persistent
= ! info
->not_persistent
;
6087 mddev
->external
= 0;
6089 mddev
->layout
= info
->layout
;
6090 mddev
->chunk_sectors
= info
->chunk_size
>> 9;
6092 mddev
->max_disks
= MD_SB_DISKS
;
6094 if (mddev
->persistent
)
6096 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
6098 mddev
->bitmap_info
.default_offset
= MD_SB_BYTES
>> 9;
6099 mddev
->bitmap_info
.default_space
= 64*2 - (MD_SB_BYTES
>> 9);
6100 mddev
->bitmap_info
.offset
= 0;
6102 mddev
->reshape_position
= MaxSector
;
6105 * Generate a 128 bit UUID
6107 get_random_bytes(mddev
->uuid
, 16);
6109 mddev
->new_level
= mddev
->level
;
6110 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
6111 mddev
->new_layout
= mddev
->layout
;
6112 mddev
->delta_disks
= 0;
6113 mddev
->reshape_backwards
= 0;
6118 void md_set_array_sectors(struct mddev
*mddev
, sector_t array_sectors
)
6120 WARN(!mddev_is_locked(mddev
), "%s: unlocked mddev!\n", __func__
);
6122 if (mddev
->external_size
)
6125 mddev
->array_sectors
= array_sectors
;
6127 EXPORT_SYMBOL(md_set_array_sectors
);
6129 static int update_size(struct mddev
*mddev
, sector_t num_sectors
)
6131 struct md_rdev
*rdev
;
6133 int fit
= (num_sectors
== 0);
6135 if (mddev
->pers
->resize
== NULL
)
6137 /* The "num_sectors" is the number of sectors of each device that
6138 * is used. This can only make sense for arrays with redundancy.
6139 * linear and raid0 always use whatever space is available. We can only
6140 * consider changing this number if no resync or reconstruction is
6141 * happening, and if the new size is acceptable. It must fit before the
6142 * sb_start or, if that is <data_offset, it must fit before the size
6143 * of each device. If num_sectors is zero, we find the largest size
6146 if (mddev
->sync_thread
)
6149 rdev_for_each(rdev
, mddev
) {
6150 sector_t avail
= rdev
->sectors
;
6152 if (fit
&& (num_sectors
== 0 || num_sectors
> avail
))
6153 num_sectors
= avail
;
6154 if (avail
< num_sectors
)
6157 rv
= mddev
->pers
->resize(mddev
, num_sectors
);
6159 revalidate_disk(mddev
->gendisk
);
6163 static int update_raid_disks(struct mddev
*mddev
, int raid_disks
)
6166 struct md_rdev
*rdev
;
6167 /* change the number of raid disks */
6168 if (mddev
->pers
->check_reshape
== NULL
)
6170 if (raid_disks
<= 0 ||
6171 (mddev
->max_disks
&& raid_disks
>= mddev
->max_disks
))
6173 if (mddev
->sync_thread
|| mddev
->reshape_position
!= MaxSector
)
6176 rdev_for_each(rdev
, mddev
) {
6177 if (mddev
->raid_disks
< raid_disks
&&
6178 rdev
->data_offset
< rdev
->new_data_offset
)
6180 if (mddev
->raid_disks
> raid_disks
&&
6181 rdev
->data_offset
> rdev
->new_data_offset
)
6185 mddev
->delta_disks
= raid_disks
- mddev
->raid_disks
;
6186 if (mddev
->delta_disks
< 0)
6187 mddev
->reshape_backwards
= 1;
6188 else if (mddev
->delta_disks
> 0)
6189 mddev
->reshape_backwards
= 0;
6191 rv
= mddev
->pers
->check_reshape(mddev
);
6193 mddev
->delta_disks
= 0;
6194 mddev
->reshape_backwards
= 0;
6201 * update_array_info is used to change the configuration of an
6203 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
6204 * fields in the info are checked against the array.
6205 * Any differences that cannot be handled will cause an error.
6206 * Normally, only one change can be managed at a time.
6208 static int update_array_info(struct mddev
*mddev
, mdu_array_info_t
*info
)
6214 /* calculate expected state,ignoring low bits */
6215 if (mddev
->bitmap
&& mddev
->bitmap_info
.offset
)
6216 state
|= (1 << MD_SB_BITMAP_PRESENT
);
6218 if (mddev
->major_version
!= info
->major_version
||
6219 mddev
->minor_version
!= info
->minor_version
||
6220 /* mddev->patch_version != info->patch_version || */
6221 mddev
->ctime
!= info
->ctime
||
6222 mddev
->level
!= info
->level
||
6223 /* mddev->layout != info->layout || */
6224 !mddev
->persistent
!= info
->not_persistent
||
6225 mddev
->chunk_sectors
!= info
->chunk_size
>> 9 ||
6226 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
6227 ((state
^info
->state
) & 0xfffffe00)
6230 /* Check there is only one change */
6231 if (info
->size
>= 0 && mddev
->dev_sectors
/ 2 != info
->size
)
6233 if (mddev
->raid_disks
!= info
->raid_disks
)
6235 if (mddev
->layout
!= info
->layout
)
6237 if ((state
^ info
->state
) & (1<<MD_SB_BITMAP_PRESENT
))
6244 if (mddev
->layout
!= info
->layout
) {
6246 * we don't need to do anything at the md level, the
6247 * personality will take care of it all.
6249 if (mddev
->pers
->check_reshape
== NULL
)
6252 mddev
->new_layout
= info
->layout
;
6253 rv
= mddev
->pers
->check_reshape(mddev
);
6255 mddev
->new_layout
= mddev
->layout
;
6259 if (info
->size
>= 0 && mddev
->dev_sectors
/ 2 != info
->size
)
6260 rv
= update_size(mddev
, (sector_t
)info
->size
* 2);
6262 if (mddev
->raid_disks
!= info
->raid_disks
)
6263 rv
= update_raid_disks(mddev
, info
->raid_disks
);
6265 if ((state
^ info
->state
) & (1<<MD_SB_BITMAP_PRESENT
)) {
6266 if (mddev
->pers
->quiesce
== NULL
)
6268 if (mddev
->recovery
|| mddev
->sync_thread
)
6270 if (info
->state
& (1<<MD_SB_BITMAP_PRESENT
)) {
6271 /* add the bitmap */
6274 if (mddev
->bitmap_info
.default_offset
== 0)
6276 mddev
->bitmap_info
.offset
=
6277 mddev
->bitmap_info
.default_offset
;
6278 mddev
->bitmap_info
.space
=
6279 mddev
->bitmap_info
.default_space
;
6280 mddev
->pers
->quiesce(mddev
, 1);
6281 rv
= bitmap_create(mddev
);
6283 rv
= bitmap_load(mddev
);
6285 bitmap_destroy(mddev
);
6286 mddev
->pers
->quiesce(mddev
, 0);
6288 /* remove the bitmap */
6291 if (mddev
->bitmap
->storage
.file
)
6293 mddev
->pers
->quiesce(mddev
, 1);
6294 bitmap_destroy(mddev
);
6295 mddev
->pers
->quiesce(mddev
, 0);
6296 mddev
->bitmap_info
.offset
= 0;
6299 md_update_sb(mddev
, 1);
6303 static int set_disk_faulty(struct mddev
*mddev
, dev_t dev
)
6305 struct md_rdev
*rdev
;
6308 if (mddev
->pers
== NULL
)
6312 rdev
= find_rdev_rcu(mddev
, dev
);
6316 md_error(mddev
, rdev
);
6317 if (!test_bit(Faulty
, &rdev
->flags
))
6325 * We have a problem here : there is no easy way to give a CHS
6326 * virtual geometry. We currently pretend that we have a 2 heads
6327 * 4 sectors (with a BIG number of cylinders...). This drives
6328 * dosfs just mad... ;-)
6330 static int md_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
6332 struct mddev
*mddev
= bdev
->bd_disk
->private_data
;
6336 geo
->cylinders
= mddev
->array_sectors
/ 8;
6340 static int md_ioctl(struct block_device
*bdev
, fmode_t mode
,
6341 unsigned int cmd
, unsigned long arg
)
6344 void __user
*argp
= (void __user
*)arg
;
6345 struct mddev
*mddev
= NULL
;
6350 case GET_ARRAY_INFO
:
6354 if (!capable(CAP_SYS_ADMIN
))
6359 * Commands dealing with the RAID driver but not any
6364 err
= get_version(argp
);
6367 case PRINT_RAID_DEBUG
:
6375 autostart_arrays(arg
);
6382 * Commands creating/starting a new array:
6385 mddev
= bdev
->bd_disk
->private_data
;
6392 /* Some actions do not requires the mutex */
6394 case GET_ARRAY_INFO
:
6395 if (!mddev
->raid_disks
&& !mddev
->external
)
6398 err
= get_array_info(mddev
, argp
);
6402 if (!mddev
->raid_disks
&& !mddev
->external
)
6405 err
= get_disk_info(mddev
, argp
);
6408 case SET_DISK_FAULTY
:
6409 err
= set_disk_faulty(mddev
, new_decode_dev(arg
));
6413 if (cmd
== ADD_NEW_DISK
)
6414 /* need to ensure md_delayed_delete() has completed */
6415 flush_workqueue(md_misc_wq
);
6417 if (cmd
== HOT_REMOVE_DISK
)
6418 /* need to ensure recovery thread has run */
6419 wait_event_interruptible_timeout(mddev
->sb_wait
,
6420 !test_bit(MD_RECOVERY_NEEDED
,
6422 msecs_to_jiffies(5000));
6423 err
= mddev_lock(mddev
);
6426 "md: ioctl lock interrupted, reason %d, cmd %d\n",
6431 if (cmd
== SET_ARRAY_INFO
) {
6432 mdu_array_info_t info
;
6434 memset(&info
, 0, sizeof(info
));
6435 else if (copy_from_user(&info
, argp
, sizeof(info
))) {
6440 err
= update_array_info(mddev
, &info
);
6442 printk(KERN_WARNING
"md: couldn't update"
6443 " array info. %d\n", err
);
6448 if (!list_empty(&mddev
->disks
)) {
6450 "md: array %s already has disks!\n",
6455 if (mddev
->raid_disks
) {
6457 "md: array %s already initialised!\n",
6462 err
= set_array_info(mddev
, &info
);
6464 printk(KERN_WARNING
"md: couldn't set"
6465 " array info. %d\n", err
);
6472 * Commands querying/configuring an existing array:
6474 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
6475 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
6476 if ((!mddev
->raid_disks
&& !mddev
->external
)
6477 && cmd
!= ADD_NEW_DISK
&& cmd
!= STOP_ARRAY
6478 && cmd
!= RUN_ARRAY
&& cmd
!= SET_BITMAP_FILE
6479 && cmd
!= GET_BITMAP_FILE
) {
6485 * Commands even a read-only array can execute:
6488 case GET_BITMAP_FILE
:
6489 err
= get_bitmap_file(mddev
, argp
);
6492 case RESTART_ARRAY_RW
:
6493 err
= restart_array(mddev
);
6497 err
= do_md_stop(mddev
, 0, bdev
);
6501 err
= md_set_readonly(mddev
, bdev
);
6504 case HOT_REMOVE_DISK
:
6505 err
= hot_remove_disk(mddev
, new_decode_dev(arg
));
6509 /* We can support ADD_NEW_DISK on read-only arrays
6510 * on if we are re-adding a preexisting device.
6511 * So require mddev->pers and MD_DISK_SYNC.
6514 mdu_disk_info_t info
;
6515 if (copy_from_user(&info
, argp
, sizeof(info
)))
6517 else if (!(info
.state
& (1<<MD_DISK_SYNC
)))
6518 /* Need to clear read-only for this */
6521 err
= add_new_disk(mddev
, &info
);
6527 if (get_user(ro
, (int __user
*)(arg
))) {
6533 /* if the bdev is going readonly the value of mddev->ro
6534 * does not matter, no writes are coming
6539 /* are we are already prepared for writes? */
6543 /* transitioning to readauto need only happen for
6544 * arrays that call md_write_start
6547 err
= restart_array(mddev
);
6550 set_disk_ro(mddev
->gendisk
, 0);
6557 * The remaining ioctls are changing the state of the
6558 * superblock, so we do not allow them on read-only arrays.
6559 * However non-MD ioctls (e.g. get-size) will still come through
6560 * here and hit the 'default' below, so only disallow
6561 * 'md' ioctls, and switch to rw mode if started auto-readonly.
6563 if (_IOC_TYPE(cmd
) == MD_MAJOR
&& mddev
->ro
&& mddev
->pers
) {
6564 if (mddev
->ro
== 2) {
6566 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
6567 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6568 /* mddev_unlock will wake thread */
6569 /* If a device failed while we were read-only, we
6570 * need to make sure the metadata is updated now.
6572 if (test_bit(MD_CHANGE_DEVS
, &mddev
->flags
)) {
6573 mddev_unlock(mddev
);
6574 wait_event(mddev
->sb_wait
,
6575 !test_bit(MD_CHANGE_DEVS
, &mddev
->flags
) &&
6576 !test_bit(MD_CHANGE_PENDING
, &mddev
->flags
));
6588 mdu_disk_info_t info
;
6589 if (copy_from_user(&info
, argp
, sizeof(info
)))
6592 err
= add_new_disk(mddev
, &info
);
6597 err
= hot_add_disk(mddev
, new_decode_dev(arg
));
6601 err
= do_md_run(mddev
);
6604 case SET_BITMAP_FILE
:
6605 err
= set_bitmap_file(mddev
, (int)arg
);
6615 if (mddev
->hold_active
== UNTIL_IOCTL
&&
6617 mddev
->hold_active
= 0;
6618 mddev_unlock(mddev
);
6627 #ifdef CONFIG_COMPAT
6628 static int md_compat_ioctl(struct block_device
*bdev
, fmode_t mode
,
6629 unsigned int cmd
, unsigned long arg
)
6632 case HOT_REMOVE_DISK
:
6634 case SET_DISK_FAULTY
:
6635 case SET_BITMAP_FILE
:
6636 /* These take in integer arg, do not convert */
6639 arg
= (unsigned long)compat_ptr(arg
);
6643 return md_ioctl(bdev
, mode
, cmd
, arg
);
6645 #endif /* CONFIG_COMPAT */
6647 static int md_open(struct block_device
*bdev
, fmode_t mode
)
6650 * Succeed if we can lock the mddev, which confirms that
6651 * it isn't being stopped right now.
6653 struct mddev
*mddev
= mddev_find(bdev
->bd_dev
);
6659 if (mddev
->gendisk
!= bdev
->bd_disk
) {
6660 /* we are racing with mddev_put which is discarding this
6664 /* Wait until bdev->bd_disk is definitely gone */
6665 flush_workqueue(md_misc_wq
);
6666 /* Then retry the open from the top */
6667 return -ERESTARTSYS
;
6669 BUG_ON(mddev
!= bdev
->bd_disk
->private_data
);
6671 if ((err
= mutex_lock_interruptible(&mddev
->open_mutex
)))
6675 atomic_inc(&mddev
->openers
);
6676 mutex_unlock(&mddev
->open_mutex
);
6678 check_disk_change(bdev
);
6683 static void md_release(struct gendisk
*disk
, fmode_t mode
)
6685 struct mddev
*mddev
= disk
->private_data
;
6688 atomic_dec(&mddev
->openers
);
6692 static int md_media_changed(struct gendisk
*disk
)
6694 struct mddev
*mddev
= disk
->private_data
;
6696 return mddev
->changed
;
6699 static int md_revalidate(struct gendisk
*disk
)
6701 struct mddev
*mddev
= disk
->private_data
;
6706 static const struct block_device_operations md_fops
=
6708 .owner
= THIS_MODULE
,
6710 .release
= md_release
,
6712 #ifdef CONFIG_COMPAT
6713 .compat_ioctl
= md_compat_ioctl
,
6715 .getgeo
= md_getgeo
,
6716 .media_changed
= md_media_changed
,
6717 .revalidate_disk
= md_revalidate
,
6720 static int md_thread(void * arg
)
6722 struct md_thread
*thread
= arg
;
6725 * md_thread is a 'system-thread', it's priority should be very
6726 * high. We avoid resource deadlocks individually in each
6727 * raid personality. (RAID5 does preallocation) We also use RR and
6728 * the very same RT priority as kswapd, thus we will never get
6729 * into a priority inversion deadlock.
6731 * we definitely have to have equal or higher priority than
6732 * bdflush, otherwise bdflush will deadlock if there are too
6733 * many dirty RAID5 blocks.
6736 allow_signal(SIGKILL
);
6737 while (!kthread_should_stop()) {
6739 /* We need to wait INTERRUPTIBLE so that
6740 * we don't add to the load-average.
6741 * That means we need to be sure no signals are
6744 if (signal_pending(current
))
6745 flush_signals(current
);
6747 wait_event_interruptible_timeout
6749 test_bit(THREAD_WAKEUP
, &thread
->flags
)
6750 || kthread_should_stop(),
6753 clear_bit(THREAD_WAKEUP
, &thread
->flags
);
6754 if (!kthread_should_stop())
6755 thread
->run(thread
);
6761 void md_wakeup_thread(struct md_thread
*thread
)
6764 pr_debug("md: waking up MD thread %s.\n", thread
->tsk
->comm
);
6765 set_bit(THREAD_WAKEUP
, &thread
->flags
);
6766 wake_up(&thread
->wqueue
);
6770 struct md_thread
*md_register_thread(void (*run
) (struct md_thread
*),
6771 struct mddev
*mddev
, const char *name
)
6773 struct md_thread
*thread
;
6775 thread
= kzalloc(sizeof(struct md_thread
), GFP_KERNEL
);
6779 init_waitqueue_head(&thread
->wqueue
);
6782 thread
->mddev
= mddev
;
6783 thread
->timeout
= MAX_SCHEDULE_TIMEOUT
;
6784 thread
->tsk
= kthread_run(md_thread
, thread
,
6786 mdname(thread
->mddev
),
6788 if (IS_ERR(thread
->tsk
)) {
6795 void md_unregister_thread(struct md_thread
**threadp
)
6797 struct md_thread
*thread
= *threadp
;
6800 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread
->tsk
));
6801 /* Locking ensures that mddev_unlock does not wake_up a
6802 * non-existent thread
6804 spin_lock(&pers_lock
);
6806 spin_unlock(&pers_lock
);
6808 kthread_stop(thread
->tsk
);
6812 void md_error(struct mddev
*mddev
, struct md_rdev
*rdev
)
6819 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
))
6822 if (!mddev
->pers
|| !mddev
->pers
->error_handler
)
6824 mddev
->pers
->error_handler(mddev
,rdev
);
6825 if (mddev
->degraded
)
6826 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
6827 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
6828 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6829 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6830 md_wakeup_thread(mddev
->thread
);
6831 if (mddev
->event_work
.func
)
6832 queue_work(md_misc_wq
, &mddev
->event_work
);
6833 md_new_event_inintr(mddev
);
6836 /* seq_file implementation /proc/mdstat */
6838 static void status_unused(struct seq_file
*seq
)
6841 struct md_rdev
*rdev
;
6843 seq_printf(seq
, "unused devices: ");
6845 list_for_each_entry(rdev
, &pending_raid_disks
, same_set
) {
6846 char b
[BDEVNAME_SIZE
];
6848 seq_printf(seq
, "%s ",
6849 bdevname(rdev
->bdev
,b
));
6852 seq_printf(seq
, "<none>");
6854 seq_printf(seq
, "\n");
6858 static void status_resync(struct seq_file
*seq
, struct mddev
* mddev
)
6860 sector_t max_sectors
, resync
, res
;
6861 unsigned long dt
, db
;
6864 unsigned int per_milli
;
6866 if (mddev
->curr_resync
<= 3)
6869 resync
= mddev
->curr_resync
6870 - atomic_read(&mddev
->recovery_active
);
6872 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ||
6873 test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
6874 max_sectors
= mddev
->resync_max_sectors
;
6876 max_sectors
= mddev
->dev_sectors
;
6879 * Should not happen.
6885 /* Pick 'scale' such that (resync>>scale)*1000 will fit
6886 * in a sector_t, and (max_sectors>>scale) will fit in a
6887 * u32, as those are the requirements for sector_div.
6888 * Thus 'scale' must be at least 10
6891 if (sizeof(sector_t
) > sizeof(unsigned long)) {
6892 while ( max_sectors
/2 > (1ULL<<(scale
+32)))
6895 res
= (resync
>>scale
)*1000;
6896 sector_div(res
, (u32
)((max_sectors
>>scale
)+1));
6900 int i
, x
= per_milli
/50, y
= 20-x
;
6901 seq_printf(seq
, "[");
6902 for (i
= 0; i
< x
; i
++)
6903 seq_printf(seq
, "=");
6904 seq_printf(seq
, ">");
6905 for (i
= 0; i
< y
; i
++)
6906 seq_printf(seq
, ".");
6907 seq_printf(seq
, "] ");
6909 seq_printf(seq
, " %s =%3u.%u%% (%llu/%llu)",
6910 (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
)?
6912 (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
)?
6914 (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ?
6915 "resync" : "recovery"))),
6916 per_milli
/10, per_milli
% 10,
6917 (unsigned long long) resync
/2,
6918 (unsigned long long) max_sectors
/2);
6921 * dt: time from mark until now
6922 * db: blocks written from mark until now
6923 * rt: remaining time
6925 * rt is a sector_t, so could be 32bit or 64bit.
6926 * So we divide before multiply in case it is 32bit and close
6928 * We scale the divisor (db) by 32 to avoid losing precision
6929 * near the end of resync when the number of remaining sectors
6931 * We then divide rt by 32 after multiplying by db to compensate.
6932 * The '+1' avoids division by zero if db is very small.
6934 dt
= ((jiffies
- mddev
->resync_mark
) / HZ
);
6936 db
= (mddev
->curr_mark_cnt
- atomic_read(&mddev
->recovery_active
))
6937 - mddev
->resync_mark_cnt
;
6939 rt
= max_sectors
- resync
; /* number of remaining sectors */
6940 sector_div(rt
, db
/32+1);
6944 seq_printf(seq
, " finish=%lu.%lumin", (unsigned long)rt
/ 60,
6945 ((unsigned long)rt
% 60)/6);
6947 seq_printf(seq
, " speed=%ldK/sec", db
/2/dt
);
6950 static void *md_seq_start(struct seq_file
*seq
, loff_t
*pos
)
6952 struct list_head
*tmp
;
6954 struct mddev
*mddev
;
6962 spin_lock(&all_mddevs_lock
);
6963 list_for_each(tmp
,&all_mddevs
)
6965 mddev
= list_entry(tmp
, struct mddev
, all_mddevs
);
6967 spin_unlock(&all_mddevs_lock
);
6970 spin_unlock(&all_mddevs_lock
);
6972 return (void*)2;/* tail */
6976 static void *md_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
6978 struct list_head
*tmp
;
6979 struct mddev
*next_mddev
, *mddev
= v
;
6985 spin_lock(&all_mddevs_lock
);
6987 tmp
= all_mddevs
.next
;
6989 tmp
= mddev
->all_mddevs
.next
;
6990 if (tmp
!= &all_mddevs
)
6991 next_mddev
= mddev_get(list_entry(tmp
,struct mddev
,all_mddevs
));
6993 next_mddev
= (void*)2;
6996 spin_unlock(&all_mddevs_lock
);
7004 static void md_seq_stop(struct seq_file
*seq
, void *v
)
7006 struct mddev
*mddev
= v
;
7008 if (mddev
&& v
!= (void*)1 && v
!= (void*)2)
7012 static int md_seq_show(struct seq_file
*seq
, void *v
)
7014 struct mddev
*mddev
= v
;
7016 struct md_rdev
*rdev
;
7018 if (v
== (void*)1) {
7019 struct md_personality
*pers
;
7020 seq_printf(seq
, "Personalities : ");
7021 spin_lock(&pers_lock
);
7022 list_for_each_entry(pers
, &pers_list
, list
)
7023 seq_printf(seq
, "[%s] ", pers
->name
);
7025 spin_unlock(&pers_lock
);
7026 seq_printf(seq
, "\n");
7027 seq
->poll_event
= atomic_read(&md_event_count
);
7030 if (v
== (void*)2) {
7035 if (mddev_lock(mddev
) < 0)
7038 if (mddev
->pers
|| mddev
->raid_disks
|| !list_empty(&mddev
->disks
)) {
7039 seq_printf(seq
, "%s : %sactive", mdname(mddev
),
7040 mddev
->pers
? "" : "in");
7043 seq_printf(seq
, " (read-only)");
7045 seq_printf(seq
, " (auto-read-only)");
7046 seq_printf(seq
, " %s", mddev
->pers
->name
);
7050 rdev_for_each(rdev
, mddev
) {
7051 char b
[BDEVNAME_SIZE
];
7052 seq_printf(seq
, " %s[%d]",
7053 bdevname(rdev
->bdev
,b
), rdev
->desc_nr
);
7054 if (test_bit(WriteMostly
, &rdev
->flags
))
7055 seq_printf(seq
, "(W)");
7056 if (test_bit(Faulty
, &rdev
->flags
)) {
7057 seq_printf(seq
, "(F)");
7060 if (rdev
->raid_disk
< 0)
7061 seq_printf(seq
, "(S)"); /* spare */
7062 if (test_bit(Replacement
, &rdev
->flags
))
7063 seq_printf(seq
, "(R)");
7064 sectors
+= rdev
->sectors
;
7067 if (!list_empty(&mddev
->disks
)) {
7069 seq_printf(seq
, "\n %llu blocks",
7070 (unsigned long long)
7071 mddev
->array_sectors
/ 2);
7073 seq_printf(seq
, "\n %llu blocks",
7074 (unsigned long long)sectors
/ 2);
7076 if (mddev
->persistent
) {
7077 if (mddev
->major_version
!= 0 ||
7078 mddev
->minor_version
!= 90) {
7079 seq_printf(seq
," super %d.%d",
7080 mddev
->major_version
,
7081 mddev
->minor_version
);
7083 } else if (mddev
->external
)
7084 seq_printf(seq
, " super external:%s",
7085 mddev
->metadata_type
);
7087 seq_printf(seq
, " super non-persistent");
7090 mddev
->pers
->status(seq
, mddev
);
7091 seq_printf(seq
, "\n ");
7092 if (mddev
->pers
->sync_request
) {
7093 if (mddev
->curr_resync
> 2) {
7094 status_resync(seq
, mddev
);
7095 seq_printf(seq
, "\n ");
7096 } else if (mddev
->curr_resync
>= 1)
7097 seq_printf(seq
, "\tresync=DELAYED\n ");
7098 else if (mddev
->recovery_cp
< MaxSector
)
7099 seq_printf(seq
, "\tresync=PENDING\n ");
7102 seq_printf(seq
, "\n ");
7104 bitmap_status(seq
, mddev
->bitmap
);
7106 seq_printf(seq
, "\n");
7108 mddev_unlock(mddev
);
7113 static const struct seq_operations md_seq_ops
= {
7114 .start
= md_seq_start
,
7115 .next
= md_seq_next
,
7116 .stop
= md_seq_stop
,
7117 .show
= md_seq_show
,
7120 static int md_seq_open(struct inode
*inode
, struct file
*file
)
7122 struct seq_file
*seq
;
7125 error
= seq_open(file
, &md_seq_ops
);
7129 seq
= file
->private_data
;
7130 seq
->poll_event
= atomic_read(&md_event_count
);
7134 static unsigned int mdstat_poll(struct file
*filp
, poll_table
*wait
)
7136 struct seq_file
*seq
= filp
->private_data
;
7139 poll_wait(filp
, &md_event_waiters
, wait
);
7141 /* always allow read */
7142 mask
= POLLIN
| POLLRDNORM
;
7144 if (seq
->poll_event
!= atomic_read(&md_event_count
))
7145 mask
|= POLLERR
| POLLPRI
;
7149 static const struct file_operations md_seq_fops
= {
7150 .owner
= THIS_MODULE
,
7151 .open
= md_seq_open
,
7153 .llseek
= seq_lseek
,
7154 .release
= seq_release_private
,
7155 .poll
= mdstat_poll
,
7158 int register_md_personality(struct md_personality
*p
)
7160 spin_lock(&pers_lock
);
7161 list_add_tail(&p
->list
, &pers_list
);
7162 printk(KERN_INFO
"md: %s personality registered for level %d\n", p
->name
, p
->level
);
7163 spin_unlock(&pers_lock
);
7167 int unregister_md_personality(struct md_personality
*p
)
7169 printk(KERN_INFO
"md: %s personality unregistered\n", p
->name
);
7170 spin_lock(&pers_lock
);
7171 list_del_init(&p
->list
);
7172 spin_unlock(&pers_lock
);
7176 static int is_mddev_idle(struct mddev
*mddev
, int init
)
7178 struct md_rdev
* rdev
;
7184 rdev_for_each_rcu(rdev
, mddev
) {
7185 struct gendisk
*disk
= rdev
->bdev
->bd_contains
->bd_disk
;
7186 curr_events
= (int)part_stat_read(&disk
->part0
, sectors
[0]) +
7187 (int)part_stat_read(&disk
->part0
, sectors
[1]) -
7188 atomic_read(&disk
->sync_io
);
7189 /* sync IO will cause sync_io to increase before the disk_stats
7190 * as sync_io is counted when a request starts, and
7191 * disk_stats is counted when it completes.
7192 * So resync activity will cause curr_events to be smaller than
7193 * when there was no such activity.
7194 * non-sync IO will cause disk_stat to increase without
7195 * increasing sync_io so curr_events will (eventually)
7196 * be larger than it was before. Once it becomes
7197 * substantially larger, the test below will cause
7198 * the array to appear non-idle, and resync will slow
7200 * If there is a lot of outstanding resync activity when
7201 * we set last_event to curr_events, then all that activity
7202 * completing might cause the array to appear non-idle
7203 * and resync will be slowed down even though there might
7204 * not have been non-resync activity. This will only
7205 * happen once though. 'last_events' will soon reflect
7206 * the state where there is little or no outstanding
7207 * resync requests, and further resync activity will
7208 * always make curr_events less than last_events.
7211 if (init
|| curr_events
- rdev
->last_events
> 64) {
7212 rdev
->last_events
= curr_events
;
7220 void md_done_sync(struct mddev
*mddev
, int blocks
, int ok
)
7222 /* another "blocks" (512byte) blocks have been synced */
7223 atomic_sub(blocks
, &mddev
->recovery_active
);
7224 wake_up(&mddev
->recovery_wait
);
7226 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
7227 set_bit(MD_RECOVERY_ERROR
, &mddev
->recovery
);
7228 md_wakeup_thread(mddev
->thread
);
7229 // stop recovery, signal do_sync ....
7234 /* md_write_start(mddev, bi)
7235 * If we need to update some array metadata (e.g. 'active' flag
7236 * in superblock) before writing, schedule a superblock update
7237 * and wait for it to complete.
7239 void md_write_start(struct mddev
*mddev
, struct bio
*bi
)
7242 if (bio_data_dir(bi
) != WRITE
)
7245 BUG_ON(mddev
->ro
== 1);
7246 if (mddev
->ro
== 2) {
7247 /* need to switch to read/write */
7249 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7250 md_wakeup_thread(mddev
->thread
);
7251 md_wakeup_thread(mddev
->sync_thread
);
7254 atomic_inc(&mddev
->writes_pending
);
7255 if (mddev
->safemode
== 1)
7256 mddev
->safemode
= 0;
7257 if (mddev
->in_sync
) {
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 md_wakeup_thread(mddev
->thread
);
7266 spin_unlock_irq(&mddev
->write_lock
);
7269 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
7270 wait_event(mddev
->sb_wait
,
7271 !test_bit(MD_CHANGE_PENDING
, &mddev
->flags
));
7274 void md_write_end(struct mddev
*mddev
)
7276 if (atomic_dec_and_test(&mddev
->writes_pending
)) {
7277 if (mddev
->safemode
== 2)
7278 md_wakeup_thread(mddev
->thread
);
7279 else if (mddev
->safemode_delay
)
7280 mod_timer(&mddev
->safemode_timer
, jiffies
+ mddev
->safemode_delay
);
7284 /* md_allow_write(mddev)
7285 * Calling this ensures that the array is marked 'active' so that writes
7286 * may proceed without blocking. It is important to call this before
7287 * attempting a GFP_KERNEL allocation while holding the mddev lock.
7288 * Must be called with mddev_lock held.
7290 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
7291 * is dropped, so return -EAGAIN after notifying userspace.
7293 int md_allow_write(struct mddev
*mddev
)
7299 if (!mddev
->pers
->sync_request
)
7302 spin_lock_irq(&mddev
->write_lock
);
7303 if (mddev
->in_sync
) {
7305 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
7306 set_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
7307 if (mddev
->safemode_delay
&&
7308 mddev
->safemode
== 0)
7309 mddev
->safemode
= 1;
7310 spin_unlock_irq(&mddev
->write_lock
);
7311 md_update_sb(mddev
, 0);
7312 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
7314 spin_unlock_irq(&mddev
->write_lock
);
7316 if (test_bit(MD_CHANGE_PENDING
, &mddev
->flags
))
7321 EXPORT_SYMBOL_GPL(md_allow_write
);
7323 #define SYNC_MARKS 10
7324 #define SYNC_MARK_STEP (3*HZ)
7325 #define UPDATE_FREQUENCY (5*60*HZ)
7326 void md_do_sync(struct md_thread
*thread
)
7328 struct mddev
*mddev
= thread
->mddev
;
7329 struct mddev
*mddev2
;
7330 unsigned int currspeed
= 0,
7332 sector_t max_sectors
,j
, io_sectors
;
7333 unsigned long mark
[SYNC_MARKS
];
7334 unsigned long update_time
;
7335 sector_t mark_cnt
[SYNC_MARKS
];
7337 struct list_head
*tmp
;
7338 sector_t last_check
;
7340 struct md_rdev
*rdev
;
7341 char *desc
, *action
= NULL
;
7342 struct blk_plug plug
;
7344 /* just incase thread restarts... */
7345 if (test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
))
7347 if (mddev
->ro
) /* never try to sync a read-only array */
7350 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
7351 if (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
)) {
7352 desc
= "data-check";
7354 } else if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
)) {
7355 desc
= "requested-resync";
7359 } else if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
7364 mddev
->last_sync_action
= action
?: desc
;
7366 /* we overload curr_resync somewhat here.
7367 * 0 == not engaged in resync at all
7368 * 2 == checking that there is no conflict with another sync
7369 * 1 == like 2, but have yielded to allow conflicting resync to
7371 * other == active in resync - this many blocks
7373 * Before starting a resync we must have set curr_resync to
7374 * 2, and then checked that every "conflicting" array has curr_resync
7375 * less than ours. When we find one that is the same or higher
7376 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
7377 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
7378 * This will mean we have to start checking from the beginning again.
7383 mddev
->curr_resync
= 2;
7386 if (kthread_should_stop())
7387 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
7389 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
7391 for_each_mddev(mddev2
, tmp
) {
7392 if (mddev2
== mddev
)
7394 if (!mddev
->parallel_resync
7395 && mddev2
->curr_resync
7396 && match_mddev_units(mddev
, mddev2
)) {
7398 if (mddev
< mddev2
&& mddev
->curr_resync
== 2) {
7399 /* arbitrarily yield */
7400 mddev
->curr_resync
= 1;
7401 wake_up(&resync_wait
);
7403 if (mddev
> mddev2
&& mddev
->curr_resync
== 1)
7404 /* no need to wait here, we can wait the next
7405 * time 'round when curr_resync == 2
7408 /* We need to wait 'interruptible' so as not to
7409 * contribute to the load average, and not to
7410 * be caught by 'softlockup'
7412 prepare_to_wait(&resync_wait
, &wq
, TASK_INTERRUPTIBLE
);
7413 if (!kthread_should_stop() &&
7414 mddev2
->curr_resync
>= mddev
->curr_resync
) {
7415 printk(KERN_INFO
"md: delaying %s of %s"
7416 " until %s has finished (they"
7417 " share one or more physical units)\n",
7418 desc
, mdname(mddev
), mdname(mddev2
));
7420 if (signal_pending(current
))
7421 flush_signals(current
);
7423 finish_wait(&resync_wait
, &wq
);
7426 finish_wait(&resync_wait
, &wq
);
7429 } while (mddev
->curr_resync
< 2);
7432 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
7433 /* resync follows the size requested by the personality,
7434 * which defaults to physical size, but can be virtual size
7436 max_sectors
= mddev
->resync_max_sectors
;
7437 atomic64_set(&mddev
->resync_mismatches
, 0);
7438 /* we don't use the checkpoint if there's a bitmap */
7439 if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
7440 j
= mddev
->resync_min
;
7441 else if (!mddev
->bitmap
)
7442 j
= mddev
->recovery_cp
;
7444 } else if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
7445 max_sectors
= mddev
->resync_max_sectors
;
7447 /* recovery follows the physical size of devices */
7448 max_sectors
= mddev
->dev_sectors
;
7451 rdev_for_each_rcu(rdev
, mddev
)
7452 if (rdev
->raid_disk
>= 0 &&
7453 !test_bit(Faulty
, &rdev
->flags
) &&
7454 !test_bit(In_sync
, &rdev
->flags
) &&
7455 rdev
->recovery_offset
< j
)
7456 j
= rdev
->recovery_offset
;
7460 printk(KERN_INFO
"md: %s of RAID array %s\n", desc
, mdname(mddev
));
7461 printk(KERN_INFO
"md: minimum _guaranteed_ speed:"
7462 " %d KB/sec/disk.\n", speed_min(mddev
));
7463 printk(KERN_INFO
"md: using maximum available idle IO bandwidth "
7464 "(but not more than %d KB/sec) for %s.\n",
7465 speed_max(mddev
), desc
);
7467 is_mddev_idle(mddev
, 1); /* this initializes IO event counters */
7470 for (m
= 0; m
< SYNC_MARKS
; m
++) {
7472 mark_cnt
[m
] = io_sectors
;
7475 mddev
->resync_mark
= mark
[last_mark
];
7476 mddev
->resync_mark_cnt
= mark_cnt
[last_mark
];
7479 * Tune reconstruction:
7481 window
= 32*(PAGE_SIZE
/512);
7482 printk(KERN_INFO
"md: using %dk window, over a total of %lluk.\n",
7483 window
/2, (unsigned long long)max_sectors
/2);
7485 atomic_set(&mddev
->recovery_active
, 0);
7490 "md: resuming %s of %s from checkpoint.\n",
7491 desc
, mdname(mddev
));
7492 mddev
->curr_resync
= j
;
7494 mddev
->curr_resync
= 3; /* no longer delayed */
7495 mddev
->curr_resync_completed
= j
;
7496 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
7497 md_new_event(mddev
);
7498 update_time
= jiffies
;
7500 blk_start_plug(&plug
);
7501 while (j
< max_sectors
) {
7506 if (!test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
7507 ((mddev
->curr_resync
> mddev
->curr_resync_completed
&&
7508 (mddev
->curr_resync
- mddev
->curr_resync_completed
)
7509 > (max_sectors
>> 4)) ||
7510 time_after_eq(jiffies
, update_time
+ UPDATE_FREQUENCY
) ||
7511 (j
- mddev
->curr_resync_completed
)*2
7512 >= mddev
->resync_max
- mddev
->curr_resync_completed
7514 /* time to update curr_resync_completed */
7515 wait_event(mddev
->recovery_wait
,
7516 atomic_read(&mddev
->recovery_active
) == 0);
7517 mddev
->curr_resync_completed
= j
;
7518 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) &&
7519 j
> mddev
->recovery_cp
)
7520 mddev
->recovery_cp
= j
;
7521 update_time
= jiffies
;
7522 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
7523 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
7526 while (j
>= mddev
->resync_max
&& !kthread_should_stop()) {
7527 /* As this condition is controlled by user-space,
7528 * we can block indefinitely, so use '_interruptible'
7529 * to avoid triggering warnings.
7531 flush_signals(current
); /* just in case */
7532 wait_event_interruptible(mddev
->recovery_wait
,
7533 mddev
->resync_max
> j
7534 || kthread_should_stop());
7537 if (kthread_should_stop())
7540 sectors
= mddev
->pers
->sync_request(mddev
, j
, &skipped
,
7541 currspeed
< speed_min(mddev
));
7543 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
7547 if (!skipped
) { /* actual IO requested */
7548 io_sectors
+= sectors
;
7549 atomic_add(sectors
, &mddev
->recovery_active
);
7552 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
7557 mddev
->curr_resync
= j
;
7558 mddev
->curr_mark_cnt
= io_sectors
;
7559 if (last_check
== 0)
7560 /* this is the earliest that rebuild will be
7561 * visible in /proc/mdstat
7563 md_new_event(mddev
);
7565 if (last_check
+ window
> io_sectors
|| j
== max_sectors
)
7568 last_check
= io_sectors
;
7570 if (time_after_eq(jiffies
, mark
[last_mark
] + SYNC_MARK_STEP
)) {
7572 int next
= (last_mark
+1) % SYNC_MARKS
;
7574 mddev
->resync_mark
= mark
[next
];
7575 mddev
->resync_mark_cnt
= mark_cnt
[next
];
7576 mark
[next
] = jiffies
;
7577 mark_cnt
[next
] = io_sectors
- atomic_read(&mddev
->recovery_active
);
7582 if (kthread_should_stop())
7587 * this loop exits only if either when we are slower than
7588 * the 'hard' speed limit, or the system was IO-idle for
7590 * the system might be non-idle CPU-wise, but we only care
7591 * about not overloading the IO subsystem. (things like an
7592 * e2fsck being done on the RAID array should execute fast)
7596 currspeed
= ((unsigned long)(io_sectors
-mddev
->resync_mark_cnt
))/2
7597 /((jiffies
-mddev
->resync_mark
)/HZ
+1) +1;
7599 if (currspeed
> speed_min(mddev
)) {
7600 if ((currspeed
> speed_max(mddev
)) ||
7601 !is_mddev_idle(mddev
, 0)) {
7607 printk(KERN_INFO
"md: %s: %s done.\n",mdname(mddev
), desc
);
7609 * this also signals 'finished resyncing' to md_stop
7612 blk_finish_plug(&plug
);
7613 wait_event(mddev
->recovery_wait
, !atomic_read(&mddev
->recovery_active
));
7615 /* tell personality that we are finished */
7616 mddev
->pers
->sync_request(mddev
, max_sectors
, &skipped
, 1);
7618 if (!test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
) &&
7619 mddev
->curr_resync
> 2) {
7620 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
7621 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
7622 if (mddev
->curr_resync
>= mddev
->recovery_cp
) {
7624 "md: checkpointing %s of %s.\n",
7625 desc
, mdname(mddev
));
7626 if (test_bit(MD_RECOVERY_ERROR
,
7628 mddev
->recovery_cp
=
7629 mddev
->curr_resync_completed
;
7631 mddev
->recovery_cp
=
7635 mddev
->recovery_cp
= MaxSector
;
7637 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
7638 mddev
->curr_resync
= MaxSector
;
7640 rdev_for_each_rcu(rdev
, mddev
)
7641 if (rdev
->raid_disk
>= 0 &&
7642 mddev
->delta_disks
>= 0 &&
7643 !test_bit(Faulty
, &rdev
->flags
) &&
7644 !test_bit(In_sync
, &rdev
->flags
) &&
7645 rdev
->recovery_offset
< mddev
->curr_resync
)
7646 rdev
->recovery_offset
= mddev
->curr_resync
;
7651 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
7653 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
7654 /* We completed so min/max setting can be forgotten if used. */
7655 if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
7656 mddev
->resync_min
= 0;
7657 mddev
->resync_max
= MaxSector
;
7658 } else if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
7659 mddev
->resync_min
= mddev
->curr_resync_completed
;
7660 mddev
->curr_resync
= 0;
7661 wake_up(&resync_wait
);
7662 set_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
7663 md_wakeup_thread(mddev
->thread
);
7668 * got a signal, exit.
7671 "md: md_do_sync() got signal ... exiting\n");
7672 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
7676 EXPORT_SYMBOL_GPL(md_do_sync
);
7678 static int remove_and_add_spares(struct mddev
*mddev
,
7679 struct md_rdev
*this)
7681 struct md_rdev
*rdev
;
7685 rdev_for_each(rdev
, mddev
)
7686 if ((this == NULL
|| rdev
== this) &&
7687 rdev
->raid_disk
>= 0 &&
7688 !test_bit(Blocked
, &rdev
->flags
) &&
7689 (test_bit(Faulty
, &rdev
->flags
) ||
7690 ! test_bit(In_sync
, &rdev
->flags
)) &&
7691 atomic_read(&rdev
->nr_pending
)==0) {
7692 if (mddev
->pers
->hot_remove_disk(
7693 mddev
, rdev
) == 0) {
7694 sysfs_unlink_rdev(mddev
, rdev
);
7695 rdev
->raid_disk
= -1;
7699 if (removed
&& mddev
->kobj
.sd
)
7700 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
7705 rdev_for_each(rdev
, mddev
) {
7706 if (rdev
->raid_disk
>= 0 &&
7707 !test_bit(In_sync
, &rdev
->flags
) &&
7708 !test_bit(Faulty
, &rdev
->flags
))
7710 if (rdev
->raid_disk
>= 0)
7712 if (test_bit(Faulty
, &rdev
->flags
))
7715 rdev
->saved_raid_disk
< 0)
7718 rdev
->recovery_offset
= 0;
7719 if (rdev
->saved_raid_disk
>= 0 && mddev
->in_sync
) {
7720 spin_lock_irq(&mddev
->write_lock
);
7722 /* OK, this device, which is in_sync,
7723 * will definitely be noticed before
7724 * the next write, so recovery isn't
7727 rdev
->recovery_offset
= mddev
->recovery_cp
;
7728 spin_unlock_irq(&mddev
->write_lock
);
7730 if (mddev
->ro
&& rdev
->recovery_offset
!= MaxSector
)
7731 /* not safe to add this disk now */
7734 hot_add_disk(mddev
, rdev
) == 0) {
7735 if (sysfs_link_rdev(mddev
, rdev
))
7736 /* failure here is OK */;
7738 md_new_event(mddev
);
7739 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
7744 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
7749 * This routine is regularly called by all per-raid-array threads to
7750 * deal with generic issues like resync and super-block update.
7751 * Raid personalities that don't have a thread (linear/raid0) do not
7752 * need this as they never do any recovery or update the superblock.
7754 * It does not do any resync itself, but rather "forks" off other threads
7755 * to do that as needed.
7756 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
7757 * "->recovery" and create a thread at ->sync_thread.
7758 * When the thread finishes it sets MD_RECOVERY_DONE
7759 * and wakeups up this thread which will reap the thread and finish up.
7760 * This thread also removes any faulty devices (with nr_pending == 0).
7762 * The overall approach is:
7763 * 1/ if the superblock needs updating, update it.
7764 * 2/ If a recovery thread is running, don't do anything else.
7765 * 3/ If recovery has finished, clean up, possibly marking spares active.
7766 * 4/ If there are any faulty devices, remove them.
7767 * 5/ If array is degraded, try to add spares devices
7768 * 6/ If array has spares or is not in-sync, start a resync thread.
7770 void md_check_recovery(struct mddev
*mddev
)
7772 if (mddev
->suspended
)
7776 bitmap_daemon_work(mddev
);
7778 if (signal_pending(current
)) {
7779 if (mddev
->pers
->sync_request
&& !mddev
->external
) {
7780 printk(KERN_INFO
"md: %s in immediate safe mode\n",
7782 mddev
->safemode
= 2;
7784 flush_signals(current
);
7787 if (mddev
->ro
&& !test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))
7790 (mddev
->flags
& ~ (1<<MD_CHANGE_PENDING
)) ||
7791 test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
) ||
7792 test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
) ||
7793 (mddev
->external
== 0 && mddev
->safemode
== 1) ||
7794 (mddev
->safemode
== 2 && ! atomic_read(&mddev
->writes_pending
)
7795 && !mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
)
7799 if (mddev_trylock(mddev
)) {
7803 /* On a read-only array we can:
7804 * - remove failed devices
7805 * - add already-in_sync devices if the array itself
7807 * As we only add devices that are already in-sync,
7808 * we can activate the spares immediately.
7810 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7811 remove_and_add_spares(mddev
, NULL
);
7812 mddev
->pers
->spare_active(mddev
);
7816 if (!mddev
->external
) {
7818 spin_lock_irq(&mddev
->write_lock
);
7819 if (mddev
->safemode
&&
7820 !atomic_read(&mddev
->writes_pending
) &&
7822 mddev
->recovery_cp
== MaxSector
) {
7825 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
7827 if (mddev
->safemode
== 1)
7828 mddev
->safemode
= 0;
7829 spin_unlock_irq(&mddev
->write_lock
);
7831 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
7835 md_update_sb(mddev
, 0);
7837 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) &&
7838 !test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
)) {
7839 /* resync/recovery still happening */
7840 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7843 if (mddev
->sync_thread
) {
7844 md_reap_sync_thread(mddev
);
7847 /* Set RUNNING before clearing NEEDED to avoid
7848 * any transients in the value of "sync_action".
7850 mddev
->curr_resync_completed
= 0;
7851 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
7852 /* Clear some bits that don't mean anything, but
7855 clear_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
7856 clear_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
7858 if (!test_and_clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
) ||
7859 test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
))
7861 /* no recovery is running.
7862 * remove any failed drives, then
7863 * add spares if possible.
7864 * Spares are also removed and re-added, to allow
7865 * the personality to fail the re-add.
7868 if (mddev
->reshape_position
!= MaxSector
) {
7869 if (mddev
->pers
->check_reshape
== NULL
||
7870 mddev
->pers
->check_reshape(mddev
) != 0)
7871 /* Cannot proceed */
7873 set_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
7874 clear_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
7875 } else if ((spares
= remove_and_add_spares(mddev
, NULL
))) {
7876 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
7877 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
7878 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
7879 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
7880 } else if (mddev
->recovery_cp
< MaxSector
) {
7881 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
7882 clear_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
7883 } else if (!test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
7884 /* nothing to be done ... */
7887 if (mddev
->pers
->sync_request
) {
7889 /* We are adding a device or devices to an array
7890 * which has the bitmap stored on all devices.
7891 * So make sure all bitmap pages get written
7893 bitmap_write_all(mddev
->bitmap
);
7895 mddev
->sync_thread
= md_register_thread(md_do_sync
,
7898 if (!mddev
->sync_thread
) {
7899 printk(KERN_ERR
"%s: could not start resync"
7902 /* leave the spares where they are, it shouldn't hurt */
7903 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
7904 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
7905 clear_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
7906 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
7907 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
7909 md_wakeup_thread(mddev
->sync_thread
);
7910 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
7911 md_new_event(mddev
);
7914 wake_up(&mddev
->sb_wait
);
7916 if (!mddev
->sync_thread
) {
7917 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
7918 if (test_and_clear_bit(MD_RECOVERY_RECOVER
,
7920 if (mddev
->sysfs_action
)
7921 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
7923 mddev_unlock(mddev
);
7927 void md_reap_sync_thread(struct mddev
*mddev
)
7929 struct md_rdev
*rdev
;
7931 /* resync has finished, collect result */
7932 md_unregister_thread(&mddev
->sync_thread
);
7933 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
) &&
7934 !test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
)) {
7936 /* activate any spares */
7937 if (mddev
->pers
->spare_active(mddev
)) {
7938 sysfs_notify(&mddev
->kobj
, NULL
,
7940 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
7943 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
7944 mddev
->pers
->finish_reshape
)
7945 mddev
->pers
->finish_reshape(mddev
);
7947 /* If array is no-longer degraded, then any saved_raid_disk
7948 * information must be scrapped. Also if any device is now
7949 * In_sync we must scrape the saved_raid_disk for that device
7950 * do the superblock for an incrementally recovered device
7953 rdev_for_each(rdev
, mddev
)
7954 if (!mddev
->degraded
||
7955 test_bit(In_sync
, &rdev
->flags
))
7956 rdev
->saved_raid_disk
= -1;
7958 md_update_sb(mddev
, 1);
7959 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
7960 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
7961 clear_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
7962 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
7963 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
7964 /* flag recovery needed just to double check */
7965 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7966 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
7967 md_new_event(mddev
);
7968 if (mddev
->event_work
.func
)
7969 queue_work(md_misc_wq
, &mddev
->event_work
);
7972 void md_wait_for_blocked_rdev(struct md_rdev
*rdev
, struct mddev
*mddev
)
7974 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
7975 wait_event_timeout(rdev
->blocked_wait
,
7976 !test_bit(Blocked
, &rdev
->flags
) &&
7977 !test_bit(BlockedBadBlocks
, &rdev
->flags
),
7978 msecs_to_jiffies(5000));
7979 rdev_dec_pending(rdev
, mddev
);
7981 EXPORT_SYMBOL(md_wait_for_blocked_rdev
);
7983 void md_finish_reshape(struct mddev
*mddev
)
7985 /* called be personality module when reshape completes. */
7986 struct md_rdev
*rdev
;
7988 rdev_for_each(rdev
, mddev
) {
7989 if (rdev
->data_offset
> rdev
->new_data_offset
)
7990 rdev
->sectors
+= rdev
->data_offset
- rdev
->new_data_offset
;
7992 rdev
->sectors
-= rdev
->new_data_offset
- rdev
->data_offset
;
7993 rdev
->data_offset
= rdev
->new_data_offset
;
7996 EXPORT_SYMBOL(md_finish_reshape
);
7998 /* Bad block management.
7999 * We can record which blocks on each device are 'bad' and so just
8000 * fail those blocks, or that stripe, rather than the whole device.
8001 * Entries in the bad-block table are 64bits wide. This comprises:
8002 * Length of bad-range, in sectors: 0-511 for lengths 1-512
8003 * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes)
8004 * A 'shift' can be set so that larger blocks are tracked and
8005 * consequently larger devices can be covered.
8006 * 'Acknowledged' flag - 1 bit. - the most significant bit.
8008 * Locking of the bad-block table uses a seqlock so md_is_badblock
8009 * might need to retry if it is very unlucky.
8010 * We will sometimes want to check for bad blocks in a bi_end_io function,
8011 * so we use the write_seqlock_irq variant.
8013 * When looking for a bad block we specify a range and want to
8014 * know if any block in the range is bad. So we binary-search
8015 * to the last range that starts at-or-before the given endpoint,
8016 * (or "before the sector after the target range")
8017 * then see if it ends after the given start.
8019 * 0 if there are no known bad blocks in the range
8020 * 1 if there are known bad block which are all acknowledged
8021 * -1 if there are bad blocks which have not yet been acknowledged in metadata.
8022 * plus the start/length of the first bad section we overlap.
8024 int md_is_badblock(struct badblocks
*bb
, sector_t s
, int sectors
,
8025 sector_t
*first_bad
, int *bad_sectors
)
8031 sector_t target
= s
+ sectors
;
8034 if (bb
->shift
> 0) {
8035 /* round the start down, and the end up */
8037 target
+= (1<<bb
->shift
) - 1;
8038 target
>>= bb
->shift
;
8039 sectors
= target
- s
;
8041 /* 'target' is now the first block after the bad range */
8044 seq
= read_seqbegin(&bb
->lock
);
8049 /* Binary search between lo and hi for 'target'
8050 * i.e. for the last range that starts before 'target'
8052 /* INVARIANT: ranges before 'lo' and at-or-after 'hi'
8053 * are known not to be the last range before target.
8054 * VARIANT: hi-lo is the number of possible
8055 * ranges, and decreases until it reaches 1
8057 while (hi
- lo
> 1) {
8058 int mid
= (lo
+ hi
) / 2;
8059 sector_t a
= BB_OFFSET(p
[mid
]);
8061 /* This could still be the one, earlier ranges
8065 /* This and later ranges are definitely out. */
8068 /* 'lo' might be the last that started before target, but 'hi' isn't */
8070 /* need to check all range that end after 's' to see if
8071 * any are unacknowledged.
8074 BB_OFFSET(p
[lo
]) + BB_LEN(p
[lo
]) > s
) {
8075 if (BB_OFFSET(p
[lo
]) < target
) {
8076 /* starts before the end, and finishes after
8077 * the start, so they must overlap
8079 if (rv
!= -1 && BB_ACK(p
[lo
]))
8083 *first_bad
= BB_OFFSET(p
[lo
]);
8084 *bad_sectors
= BB_LEN(p
[lo
]);
8090 if (read_seqretry(&bb
->lock
, seq
))
8095 EXPORT_SYMBOL_GPL(md_is_badblock
);
8098 * Add a range of bad blocks to the table.
8099 * This might extend the table, or might contract it
8100 * if two adjacent ranges can be merged.
8101 * We binary-search to find the 'insertion' point, then
8102 * decide how best to handle it.
8104 static int md_set_badblocks(struct badblocks
*bb
, sector_t s
, int sectors
,
8112 /* badblocks are disabled */
8116 /* round the start down, and the end up */
8117 sector_t next
= s
+ sectors
;
8119 next
+= (1<<bb
->shift
) - 1;
8124 write_seqlock_irq(&bb
->lock
);
8129 /* Find the last range that starts at-or-before 's' */
8130 while (hi
- lo
> 1) {
8131 int mid
= (lo
+ hi
) / 2;
8132 sector_t a
= BB_OFFSET(p
[mid
]);
8138 if (hi
> lo
&& BB_OFFSET(p
[lo
]) > s
)
8142 /* we found a range that might merge with the start
8145 sector_t a
= BB_OFFSET(p
[lo
]);
8146 sector_t e
= a
+ BB_LEN(p
[lo
]);
8147 int ack
= BB_ACK(p
[lo
]);
8149 /* Yes, we can merge with a previous range */
8150 if (s
== a
&& s
+ sectors
>= e
)
8151 /* new range covers old */
8154 ack
= ack
&& acknowledged
;
8156 if (e
< s
+ sectors
)
8158 if (e
- a
<= BB_MAX_LEN
) {
8159 p
[lo
] = BB_MAKE(a
, e
-a
, ack
);
8162 /* does not all fit in one range,
8163 * make p[lo] maximal
8165 if (BB_LEN(p
[lo
]) != BB_MAX_LEN
)
8166 p
[lo
] = BB_MAKE(a
, BB_MAX_LEN
, ack
);
8172 if (sectors
&& hi
< bb
->count
) {
8173 /* 'hi' points to the first range that starts after 's'.
8174 * Maybe we can merge with the start of that range */
8175 sector_t a
= BB_OFFSET(p
[hi
]);
8176 sector_t e
= a
+ BB_LEN(p
[hi
]);
8177 int ack
= BB_ACK(p
[hi
]);
8178 if (a
<= s
+ sectors
) {
8179 /* merging is possible */
8180 if (e
<= s
+ sectors
) {
8185 ack
= ack
&& acknowledged
;
8188 if (e
- a
<= BB_MAX_LEN
) {
8189 p
[hi
] = BB_MAKE(a
, e
-a
, ack
);
8192 p
[hi
] = BB_MAKE(a
, BB_MAX_LEN
, ack
);
8200 if (sectors
== 0 && hi
< bb
->count
) {
8201 /* we might be able to combine lo and hi */
8202 /* Note: 's' is at the end of 'lo' */
8203 sector_t a
= BB_OFFSET(p
[hi
]);
8204 int lolen
= BB_LEN(p
[lo
]);
8205 int hilen
= BB_LEN(p
[hi
]);
8206 int newlen
= lolen
+ hilen
- (s
- a
);
8207 if (s
>= a
&& newlen
< BB_MAX_LEN
) {
8208 /* yes, we can combine them */
8209 int ack
= BB_ACK(p
[lo
]) && BB_ACK(p
[hi
]);
8210 p
[lo
] = BB_MAKE(BB_OFFSET(p
[lo
]), newlen
, ack
);
8211 memmove(p
+ hi
, p
+ hi
+ 1,
8212 (bb
->count
- hi
- 1) * 8);
8217 /* didn't merge (it all).
8218 * Need to add a range just before 'hi' */
8219 if (bb
->count
>= MD_MAX_BADBLOCKS
) {
8220 /* No room for more */
8224 int this_sectors
= sectors
;
8225 memmove(p
+ hi
+ 1, p
+ hi
,
8226 (bb
->count
- hi
) * 8);
8229 if (this_sectors
> BB_MAX_LEN
)
8230 this_sectors
= BB_MAX_LEN
;
8231 p
[hi
] = BB_MAKE(s
, this_sectors
, acknowledged
);
8232 sectors
-= this_sectors
;
8239 bb
->unacked_exist
= 1;
8240 write_sequnlock_irq(&bb
->lock
);
8245 int rdev_set_badblocks(struct md_rdev
*rdev
, sector_t s
, int sectors
,
8250 s
+= rdev
->new_data_offset
;
8252 s
+= rdev
->data_offset
;
8253 rv
= md_set_badblocks(&rdev
->badblocks
,
8256 /* Make sure they get written out promptly */
8257 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
8258 set_bit(MD_CHANGE_CLEAN
, &rdev
->mddev
->flags
);
8259 md_wakeup_thread(rdev
->mddev
->thread
);
8263 EXPORT_SYMBOL_GPL(rdev_set_badblocks
);
8266 * Remove a range of bad blocks from the table.
8267 * This may involve extending the table if we spilt a region,
8268 * but it must not fail. So if the table becomes full, we just
8269 * drop the remove request.
8271 static int md_clear_badblocks(struct badblocks
*bb
, sector_t s
, int sectors
)
8275 sector_t target
= s
+ sectors
;
8278 if (bb
->shift
> 0) {
8279 /* When clearing we round the start up and the end down.
8280 * This should not matter as the shift should align with
8281 * the block size and no rounding should ever be needed.
8282 * However it is better the think a block is bad when it
8283 * isn't than to think a block is not bad when it is.
8285 s
+= (1<<bb
->shift
) - 1;
8287 target
>>= bb
->shift
;
8288 sectors
= target
- s
;
8291 write_seqlock_irq(&bb
->lock
);
8296 /* Find the last range that starts before 'target' */
8297 while (hi
- lo
> 1) {
8298 int mid
= (lo
+ hi
) / 2;
8299 sector_t a
= BB_OFFSET(p
[mid
]);
8306 /* p[lo] is the last range that could overlap the
8307 * current range. Earlier ranges could also overlap,
8308 * but only this one can overlap the end of the range.
8310 if (BB_OFFSET(p
[lo
]) + BB_LEN(p
[lo
]) > target
) {
8311 /* Partial overlap, leave the tail of this range */
8312 int ack
= BB_ACK(p
[lo
]);
8313 sector_t a
= BB_OFFSET(p
[lo
]);
8314 sector_t end
= a
+ BB_LEN(p
[lo
]);
8317 /* we need to split this range */
8318 if (bb
->count
>= MD_MAX_BADBLOCKS
) {
8322 memmove(p
+lo
+1, p
+lo
, (bb
->count
- lo
) * 8);
8324 p
[lo
] = BB_MAKE(a
, s
-a
, ack
);
8327 p
[lo
] = BB_MAKE(target
, end
- target
, ack
);
8328 /* there is no longer an overlap */
8333 BB_OFFSET(p
[lo
]) + BB_LEN(p
[lo
]) > s
) {
8334 /* This range does overlap */
8335 if (BB_OFFSET(p
[lo
]) < s
) {
8336 /* Keep the early parts of this range. */
8337 int ack
= BB_ACK(p
[lo
]);
8338 sector_t start
= BB_OFFSET(p
[lo
]);
8339 p
[lo
] = BB_MAKE(start
, s
- start
, ack
);
8340 /* now low doesn't overlap, so.. */
8345 /* 'lo' is strictly before, 'hi' is strictly after,
8346 * anything between needs to be discarded
8349 memmove(p
+lo
+1, p
+hi
, (bb
->count
- hi
) * 8);
8350 bb
->count
-= (hi
- lo
- 1);
8356 write_sequnlock_irq(&bb
->lock
);
8360 int rdev_clear_badblocks(struct md_rdev
*rdev
, sector_t s
, int sectors
,
8364 s
+= rdev
->new_data_offset
;
8366 s
+= rdev
->data_offset
;
8367 return md_clear_badblocks(&rdev
->badblocks
,
8370 EXPORT_SYMBOL_GPL(rdev_clear_badblocks
);
8373 * Acknowledge all bad blocks in a list.
8374 * This only succeeds if ->changed is clear. It is used by
8375 * in-kernel metadata updates
8377 void md_ack_all_badblocks(struct badblocks
*bb
)
8379 if (bb
->page
== NULL
|| bb
->changed
)
8380 /* no point even trying */
8382 write_seqlock_irq(&bb
->lock
);
8384 if (bb
->changed
== 0 && bb
->unacked_exist
) {
8387 for (i
= 0; i
< bb
->count
; i
++) {
8388 if (!BB_ACK(p
[i
])) {
8389 sector_t start
= BB_OFFSET(p
[i
]);
8390 int len
= BB_LEN(p
[i
]);
8391 p
[i
] = BB_MAKE(start
, len
, 1);
8394 bb
->unacked_exist
= 0;
8396 write_sequnlock_irq(&bb
->lock
);
8398 EXPORT_SYMBOL_GPL(md_ack_all_badblocks
);
8400 /* sysfs access to bad-blocks list.
8401 * We present two files.
8402 * 'bad-blocks' lists sector numbers and lengths of ranges that
8403 * are recorded as bad. The list is truncated to fit within
8404 * the one-page limit of sysfs.
8405 * Writing "sector length" to this file adds an acknowledged
8407 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
8408 * been acknowledged. Writing to this file adds bad blocks
8409 * without acknowledging them. This is largely for testing.
8413 badblocks_show(struct badblocks
*bb
, char *page
, int unack
)
8424 seq
= read_seqbegin(&bb
->lock
);
8429 while (len
< PAGE_SIZE
&& i
< bb
->count
) {
8430 sector_t s
= BB_OFFSET(p
[i
]);
8431 unsigned int length
= BB_LEN(p
[i
]);
8432 int ack
= BB_ACK(p
[i
]);
8438 len
+= snprintf(page
+len
, PAGE_SIZE
-len
, "%llu %u\n",
8439 (unsigned long long)s
<< bb
->shift
,
8440 length
<< bb
->shift
);
8442 if (unack
&& len
== 0)
8443 bb
->unacked_exist
= 0;
8445 if (read_seqretry(&bb
->lock
, seq
))
8454 badblocks_store(struct badblocks
*bb
, const char *page
, size_t len
, int unack
)
8456 unsigned long long sector
;
8460 /* Allow clearing via sysfs *only* for testing/debugging.
8461 * Normally only a successful write may clear a badblock
8464 if (page
[0] == '-') {
8468 #endif /* DO_DEBUG */
8470 switch (sscanf(page
, "%llu %d%c", §or
, &length
, &newline
)) {
8472 if (newline
!= '\n')
8484 md_clear_badblocks(bb
, sector
, length
);
8487 #endif /* DO_DEBUG */
8488 if (md_set_badblocks(bb
, sector
, length
, !unack
))
8494 static int md_notify_reboot(struct notifier_block
*this,
8495 unsigned long code
, void *x
)
8497 struct list_head
*tmp
;
8498 struct mddev
*mddev
;
8501 for_each_mddev(mddev
, tmp
) {
8502 if (mddev_trylock(mddev
)) {
8504 __md_stop_writes(mddev
);
8505 mddev
->safemode
= 2;
8506 mddev_unlock(mddev
);
8511 * certain more exotic SCSI devices are known to be
8512 * volatile wrt too early system reboots. While the
8513 * right place to handle this issue is the given
8514 * driver, we do want to have a safe RAID driver ...
8522 static struct notifier_block md_notifier
= {
8523 .notifier_call
= md_notify_reboot
,
8525 .priority
= INT_MAX
, /* before any real devices */
8528 static void md_geninit(void)
8530 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t
));
8532 proc_create("mdstat", S_IRUGO
, NULL
, &md_seq_fops
);
8535 static int __init
md_init(void)
8539 md_wq
= alloc_workqueue("md", WQ_MEM_RECLAIM
, 0);
8543 md_misc_wq
= alloc_workqueue("md_misc", 0, 0);
8547 if ((ret
= register_blkdev(MD_MAJOR
, "md")) < 0)
8550 if ((ret
= register_blkdev(0, "mdp")) < 0)
8554 blk_register_region(MKDEV(MD_MAJOR
, 0), 1UL<<MINORBITS
, THIS_MODULE
,
8555 md_probe
, NULL
, NULL
);
8556 blk_register_region(MKDEV(mdp_major
, 0), 1UL<<MINORBITS
, THIS_MODULE
,
8557 md_probe
, NULL
, NULL
);
8559 register_reboot_notifier(&md_notifier
);
8560 raid_table_header
= register_sysctl_table(raid_root_table
);
8566 unregister_blkdev(MD_MAJOR
, "md");
8568 destroy_workqueue(md_misc_wq
);
8570 destroy_workqueue(md_wq
);
8578 * Searches all registered partitions for autorun RAID arrays
8582 static LIST_HEAD(all_detected_devices
);
8583 struct detected_devices_node
{
8584 struct list_head list
;
8588 void md_autodetect_dev(dev_t dev
)
8590 struct detected_devices_node
*node_detected_dev
;
8592 node_detected_dev
= kzalloc(sizeof(*node_detected_dev
), GFP_KERNEL
);
8593 if (node_detected_dev
) {
8594 node_detected_dev
->dev
= dev
;
8595 list_add_tail(&node_detected_dev
->list
, &all_detected_devices
);
8597 printk(KERN_CRIT
"md: md_autodetect_dev: kzalloc failed"
8598 ", skipping dev(%d,%d)\n", MAJOR(dev
), MINOR(dev
));
8603 static void autostart_arrays(int part
)
8605 struct md_rdev
*rdev
;
8606 struct detected_devices_node
*node_detected_dev
;
8608 int i_scanned
, i_passed
;
8613 printk(KERN_INFO
"md: Autodetecting RAID arrays.\n");
8615 while (!list_empty(&all_detected_devices
) && i_scanned
< INT_MAX
) {
8617 node_detected_dev
= list_entry(all_detected_devices
.next
,
8618 struct detected_devices_node
, list
);
8619 list_del(&node_detected_dev
->list
);
8620 dev
= node_detected_dev
->dev
;
8621 kfree(node_detected_dev
);
8622 rdev
= md_import_device(dev
,0, 90);
8626 if (test_bit(Faulty
, &rdev
->flags
)) {
8630 set_bit(AutoDetected
, &rdev
->flags
);
8631 list_add(&rdev
->same_set
, &pending_raid_disks
);
8635 printk(KERN_INFO
"md: Scanned %d and added %d devices.\n",
8636 i_scanned
, i_passed
);
8638 autorun_devices(part
);
8641 #endif /* !MODULE */
8643 static __exit
void md_exit(void)
8645 struct mddev
*mddev
;
8646 struct list_head
*tmp
;
8648 blk_unregister_region(MKDEV(MD_MAJOR
,0), 1U << MINORBITS
);
8649 blk_unregister_region(MKDEV(mdp_major
,0), 1U << MINORBITS
);
8651 unregister_blkdev(MD_MAJOR
,"md");
8652 unregister_blkdev(mdp_major
, "mdp");
8653 unregister_reboot_notifier(&md_notifier
);
8654 unregister_sysctl_table(raid_table_header
);
8655 remove_proc_entry("mdstat", NULL
);
8656 for_each_mddev(mddev
, tmp
) {
8657 export_array(mddev
);
8658 mddev
->hold_active
= 0;
8660 destroy_workqueue(md_misc_wq
);
8661 destroy_workqueue(md_wq
);
8664 subsys_initcall(md_init
);
8665 module_exit(md_exit
)
8667 static int get_ro(char *buffer
, struct kernel_param
*kp
)
8669 return sprintf(buffer
, "%d", start_readonly
);
8671 static int set_ro(const char *val
, struct kernel_param
*kp
)
8674 int num
= simple_strtoul(val
, &e
, 10);
8675 if (*val
&& (*e
== '\0' || *e
== '\n')) {
8676 start_readonly
= num
;
8682 module_param_call(start_ro
, set_ro
, get_ro
, NULL
, S_IRUSR
|S_IWUSR
);
8683 module_param(start_dirty_degraded
, int, S_IRUGO
|S_IWUSR
);
8685 module_param_call(new_array
, add_named_array
, NULL
, NULL
, S_IWUSR
);
8687 EXPORT_SYMBOL(register_md_personality
);
8688 EXPORT_SYMBOL(unregister_md_personality
);
8689 EXPORT_SYMBOL(md_error
);
8690 EXPORT_SYMBOL(md_done_sync
);
8691 EXPORT_SYMBOL(md_write_start
);
8692 EXPORT_SYMBOL(md_write_end
);
8693 EXPORT_SYMBOL(md_register_thread
);
8694 EXPORT_SYMBOL(md_unregister_thread
);
8695 EXPORT_SYMBOL(md_wakeup_thread
);
8696 EXPORT_SYMBOL(md_check_recovery
);
8697 EXPORT_SYMBOL(md_reap_sync_thread
);
8698 MODULE_LICENSE("GPL");
8699 MODULE_DESCRIPTION("MD RAID framework");
8701 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR
);