2 md.c : Multiple Devices driver for Linux
3 Copyright (C) 1998, 1999, 2000 Ingo Molnar
5 completely rewritten, based on the MD driver code from Marc Zyngier
9 - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar
10 - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com>
11 - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net>
12 - kerneld support by Boris Tobotras <boris@xtalk.msk.su>
13 - kmod support by: Cyrus Durgin
14 - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com>
15 - Devfs support by Richard Gooch <rgooch@atnf.csiro.au>
17 - lots of fixes and improvements to the RAID1/RAID5 and generic
18 RAID code (such as request based resynchronization):
20 Neil Brown <neilb@cse.unsw.edu.au>.
22 - persistent bitmap code
23 Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.
25 This program is free software; you can redistribute it and/or modify
26 it under the terms of the GNU General Public License as published by
27 the Free Software Foundation; either version 2, or (at your option)
30 You should have received a copy of the GNU General Public License
31 (for example /usr/src/linux/COPYING); if not, write to the Free
32 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35 #include <linux/kthread.h>
36 #include <linux/blkdev.h>
37 #include <linux/sysctl.h>
38 #include <linux/seq_file.h>
40 #include <linux/poll.h>
41 #include <linux/ctype.h>
42 #include <linux/string.h>
43 #include <linux/hdreg.h>
44 #include <linux/proc_fs.h>
45 #include <linux/random.h>
46 #include <linux/module.h>
47 #include <linux/reboot.h>
48 #include <linux/file.h>
49 #include <linux/compat.h>
50 #include <linux/delay.h>
51 #include <linux/raid/md_p.h>
52 #include <linux/raid/md_u.h>
53 #include <linux/slab.h>
58 static void autostart_arrays(int part
);
61 /* pers_list is a list of registered personalities protected
63 * pers_lock does extra service to protect accesses to
64 * mddev->thread when the mutex cannot be held.
66 static LIST_HEAD(pers_list
);
67 static DEFINE_SPINLOCK(pers_lock
);
69 static void md_print_devices(void);
71 static DECLARE_WAIT_QUEUE_HEAD(resync_wait
);
72 static struct workqueue_struct
*md_wq
;
73 static struct workqueue_struct
*md_misc_wq
;
75 #define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
78 * Default number of read corrections we'll attempt on an rdev
79 * before ejecting it from the array. We divide the read error
80 * count by 2 for every hour elapsed between read errors.
82 #define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
84 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
85 * is 1000 KB/sec, so the extra system load does not show up that much.
86 * Increase it if you want to have more _guaranteed_ speed. Note that
87 * the RAID driver will use the maximum available bandwidth if the IO
88 * subsystem is idle. There is also an 'absolute maximum' reconstruction
89 * speed limit - in case reconstruction slows down your system despite
92 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
93 * or /sys/block/mdX/md/sync_speed_{min,max}
96 static int sysctl_speed_limit_min
= 1000;
97 static int sysctl_speed_limit_max
= 200000;
98 static inline int speed_min(struct mddev
*mddev
)
100 return mddev
->sync_speed_min
?
101 mddev
->sync_speed_min
: sysctl_speed_limit_min
;
104 static inline int speed_max(struct mddev
*mddev
)
106 return mddev
->sync_speed_max
?
107 mddev
->sync_speed_max
: sysctl_speed_limit_max
;
110 static struct ctl_table_header
*raid_table_header
;
112 static ctl_table raid_table
[] = {
114 .procname
= "speed_limit_min",
115 .data
= &sysctl_speed_limit_min
,
116 .maxlen
= sizeof(int),
117 .mode
= S_IRUGO
|S_IWUSR
,
118 .proc_handler
= proc_dointvec
,
121 .procname
= "speed_limit_max",
122 .data
= &sysctl_speed_limit_max
,
123 .maxlen
= sizeof(int),
124 .mode
= S_IRUGO
|S_IWUSR
,
125 .proc_handler
= proc_dointvec
,
130 static ctl_table raid_dir_table
[] = {
134 .mode
= S_IRUGO
|S_IXUGO
,
140 static ctl_table raid_root_table
[] = {
145 .child
= raid_dir_table
,
150 static const struct block_device_operations md_fops
;
152 static int start_readonly
;
155 * like bio_clone, but with a local bio set
158 static void mddev_bio_destructor(struct bio
*bio
)
160 struct mddev
*mddev
, **mddevp
;
165 bio_free(bio
, mddev
->bio_set
);
168 struct bio
*bio_alloc_mddev(gfp_t gfp_mask
, int nr_iovecs
,
172 struct mddev
**mddevp
;
174 if (!mddev
|| !mddev
->bio_set
)
175 return bio_alloc(gfp_mask
, nr_iovecs
);
177 b
= bio_alloc_bioset(gfp_mask
, nr_iovecs
,
183 b
->bi_destructor
= mddev_bio_destructor
;
186 EXPORT_SYMBOL_GPL(bio_alloc_mddev
);
188 struct bio
*bio_clone_mddev(struct bio
*bio
, gfp_t gfp_mask
,
192 struct mddev
**mddevp
;
194 if (!mddev
|| !mddev
->bio_set
)
195 return bio_clone(bio
, gfp_mask
);
197 b
= bio_alloc_bioset(gfp_mask
, bio
->bi_max_vecs
,
203 b
->bi_destructor
= mddev_bio_destructor
;
205 if (bio_integrity(bio
)) {
208 ret
= bio_integrity_clone(b
, bio
, gfp_mask
, mddev
->bio_set
);
218 EXPORT_SYMBOL_GPL(bio_clone_mddev
);
220 void md_trim_bio(struct bio
*bio
, int offset
, int size
)
222 /* 'bio' is a cloned bio which we need to trim to match
223 * the given offset and size.
224 * This requires adjusting bi_sector, bi_size, and bi_io_vec
227 struct bio_vec
*bvec
;
231 if (offset
== 0 && size
== bio
->bi_size
)
234 bio
->bi_sector
+= offset
;
237 clear_bit(BIO_SEG_VALID
, &bio
->bi_flags
);
239 while (bio
->bi_idx
< bio
->bi_vcnt
&&
240 bio
->bi_io_vec
[bio
->bi_idx
].bv_len
<= offset
) {
241 /* remove this whole bio_vec */
242 offset
-= bio
->bi_io_vec
[bio
->bi_idx
].bv_len
;
245 if (bio
->bi_idx
< bio
->bi_vcnt
) {
246 bio
->bi_io_vec
[bio
->bi_idx
].bv_offset
+= offset
;
247 bio
->bi_io_vec
[bio
->bi_idx
].bv_len
-= offset
;
249 /* avoid any complications with bi_idx being non-zero*/
251 memmove(bio
->bi_io_vec
, bio
->bi_io_vec
+bio
->bi_idx
,
252 (bio
->bi_vcnt
- bio
->bi_idx
) * sizeof(struct bio_vec
));
253 bio
->bi_vcnt
-= bio
->bi_idx
;
256 /* Make sure vcnt and last bv are not too big */
257 bio_for_each_segment(bvec
, bio
, i
) {
258 if (sofar
+ bvec
->bv_len
> size
)
259 bvec
->bv_len
= size
- sofar
;
260 if (bvec
->bv_len
== 0) {
264 sofar
+= bvec
->bv_len
;
267 EXPORT_SYMBOL_GPL(md_trim_bio
);
270 * We have a system wide 'event count' that is incremented
271 * on any 'interesting' event, and readers of /proc/mdstat
272 * can use 'poll' or 'select' to find out when the event
276 * start array, stop array, error, add device, remove device,
277 * start build, activate spare
279 static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters
);
280 static atomic_t md_event_count
;
281 void md_new_event(struct mddev
*mddev
)
283 atomic_inc(&md_event_count
);
284 wake_up(&md_event_waiters
);
286 EXPORT_SYMBOL_GPL(md_new_event
);
288 /* Alternate version that can be called from interrupts
289 * when calling sysfs_notify isn't needed.
291 static void md_new_event_inintr(struct mddev
*mddev
)
293 atomic_inc(&md_event_count
);
294 wake_up(&md_event_waiters
);
298 * Enables to iterate over all existing md arrays
299 * all_mddevs_lock protects this list.
301 static LIST_HEAD(all_mddevs
);
302 static DEFINE_SPINLOCK(all_mddevs_lock
);
306 * iterates through all used mddevs in the system.
307 * We take care to grab the all_mddevs_lock whenever navigating
308 * the list, and to always hold a refcount when unlocked.
309 * Any code which breaks out of this loop while own
310 * a reference to the current mddev and must mddev_put it.
312 #define for_each_mddev(_mddev,_tmp) \
314 for (({ spin_lock(&all_mddevs_lock); \
315 _tmp = all_mddevs.next; \
317 ({ if (_tmp != &all_mddevs) \
318 mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\
319 spin_unlock(&all_mddevs_lock); \
320 if (_mddev) mddev_put(_mddev); \
321 _mddev = list_entry(_tmp, struct mddev, all_mddevs); \
322 _tmp != &all_mddevs;}); \
323 ({ spin_lock(&all_mddevs_lock); \
324 _tmp = _tmp->next;}) \
328 /* Rather than calling directly into the personality make_request function,
329 * IO requests come here first so that we can check if the device is
330 * being suspended pending a reconfiguration.
331 * We hold a refcount over the call to ->make_request. By the time that
332 * call has finished, the bio has been linked into some internal structure
333 * and so is visible to ->quiesce(), so we don't need the refcount any more.
335 static void md_make_request(struct request_queue
*q
, struct bio
*bio
)
337 const int rw
= bio_data_dir(bio
);
338 struct mddev
*mddev
= q
->queuedata
;
340 unsigned int sectors
;
342 if (mddev
== NULL
|| mddev
->pers
== NULL
347 smp_rmb(); /* Ensure implications of 'active' are visible */
349 if (mddev
->suspended
) {
352 prepare_to_wait(&mddev
->sb_wait
, &__wait
,
353 TASK_UNINTERRUPTIBLE
);
354 if (!mddev
->suspended
)
360 finish_wait(&mddev
->sb_wait
, &__wait
);
362 atomic_inc(&mddev
->active_io
);
366 * save the sectors now since our bio can
367 * go away inside make_request
369 sectors
= bio_sectors(bio
);
370 mddev
->pers
->make_request(mddev
, bio
);
372 cpu
= part_stat_lock();
373 part_stat_inc(cpu
, &mddev
->gendisk
->part0
, ios
[rw
]);
374 part_stat_add(cpu
, &mddev
->gendisk
->part0
, sectors
[rw
], sectors
);
377 if (atomic_dec_and_test(&mddev
->active_io
) && mddev
->suspended
)
378 wake_up(&mddev
->sb_wait
);
381 /* mddev_suspend makes sure no new requests are submitted
382 * to the device, and that any requests that have been submitted
383 * are completely handled.
384 * Once ->stop is called and completes, the module will be completely
387 void mddev_suspend(struct mddev
*mddev
)
389 BUG_ON(mddev
->suspended
);
390 mddev
->suspended
= 1;
392 wait_event(mddev
->sb_wait
, atomic_read(&mddev
->active_io
) == 0);
393 mddev
->pers
->quiesce(mddev
, 1);
395 del_timer_sync(&mddev
->safemode_timer
);
397 EXPORT_SYMBOL_GPL(mddev_suspend
);
399 void mddev_resume(struct mddev
*mddev
)
401 mddev
->suspended
= 0;
402 wake_up(&mddev
->sb_wait
);
403 mddev
->pers
->quiesce(mddev
, 0);
405 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
406 md_wakeup_thread(mddev
->thread
);
407 md_wakeup_thread(mddev
->sync_thread
); /* possibly kick off a reshape */
409 EXPORT_SYMBOL_GPL(mddev_resume
);
411 int mddev_congested(struct mddev
*mddev
, int bits
)
413 return mddev
->suspended
;
415 EXPORT_SYMBOL(mddev_congested
);
418 * Generic flush handling for md
421 static void md_end_flush(struct bio
*bio
, int err
)
423 struct md_rdev
*rdev
= bio
->bi_private
;
424 struct mddev
*mddev
= rdev
->mddev
;
426 rdev_dec_pending(rdev
, mddev
);
428 if (atomic_dec_and_test(&mddev
->flush_pending
)) {
429 /* The pre-request flush has finished */
430 queue_work(md_wq
, &mddev
->flush_work
);
435 static void md_submit_flush_data(struct work_struct
*ws
);
437 static void submit_flushes(struct work_struct
*ws
)
439 struct mddev
*mddev
= container_of(ws
, struct mddev
, flush_work
);
440 struct md_rdev
*rdev
;
442 INIT_WORK(&mddev
->flush_work
, md_submit_flush_data
);
443 atomic_set(&mddev
->flush_pending
, 1);
445 rdev_for_each_rcu(rdev
, mddev
)
446 if (rdev
->raid_disk
>= 0 &&
447 !test_bit(Faulty
, &rdev
->flags
)) {
448 /* Take two references, one is dropped
449 * when request finishes, one after
450 * we reclaim rcu_read_lock
453 atomic_inc(&rdev
->nr_pending
);
454 atomic_inc(&rdev
->nr_pending
);
456 bi
= bio_alloc_mddev(GFP_NOIO
, 0, mddev
);
457 bi
->bi_end_io
= md_end_flush
;
458 bi
->bi_private
= rdev
;
459 bi
->bi_bdev
= rdev
->bdev
;
460 atomic_inc(&mddev
->flush_pending
);
461 submit_bio(WRITE_FLUSH
, bi
);
463 rdev_dec_pending(rdev
, mddev
);
466 if (atomic_dec_and_test(&mddev
->flush_pending
))
467 queue_work(md_wq
, &mddev
->flush_work
);
470 static void md_submit_flush_data(struct work_struct
*ws
)
472 struct mddev
*mddev
= container_of(ws
, struct mddev
, flush_work
);
473 struct bio
*bio
= mddev
->flush_bio
;
475 if (bio
->bi_size
== 0)
476 /* an empty barrier - all done */
479 bio
->bi_rw
&= ~REQ_FLUSH
;
480 mddev
->pers
->make_request(mddev
, bio
);
483 mddev
->flush_bio
= NULL
;
484 wake_up(&mddev
->sb_wait
);
487 void md_flush_request(struct mddev
*mddev
, struct bio
*bio
)
489 spin_lock_irq(&mddev
->write_lock
);
490 wait_event_lock_irq(mddev
->sb_wait
,
492 mddev
->write_lock
, /*nothing*/);
493 mddev
->flush_bio
= bio
;
494 spin_unlock_irq(&mddev
->write_lock
);
496 INIT_WORK(&mddev
->flush_work
, submit_flushes
);
497 queue_work(md_wq
, &mddev
->flush_work
);
499 EXPORT_SYMBOL(md_flush_request
);
501 void md_unplug(struct blk_plug_cb
*cb
)
503 struct mddev
*mddev
= cb
->data
;
504 md_wakeup_thread(mddev
->thread
);
507 EXPORT_SYMBOL(md_unplug
);
509 static inline struct mddev
*mddev_get(struct mddev
*mddev
)
511 atomic_inc(&mddev
->active
);
515 static void mddev_delayed_delete(struct work_struct
*ws
);
517 static void mddev_put(struct mddev
*mddev
)
519 struct bio_set
*bs
= NULL
;
521 if (!atomic_dec_and_lock(&mddev
->active
, &all_mddevs_lock
))
523 if (!mddev
->raid_disks
&& list_empty(&mddev
->disks
) &&
524 mddev
->ctime
== 0 && !mddev
->hold_active
) {
525 /* Array is not configured at all, and not held active,
527 list_del_init(&mddev
->all_mddevs
);
529 mddev
->bio_set
= NULL
;
530 if (mddev
->gendisk
) {
531 /* We did a probe so need to clean up. Call
532 * queue_work inside the spinlock so that
533 * flush_workqueue() after mddev_find will
534 * succeed in waiting for the work to be done.
536 INIT_WORK(&mddev
->del_work
, mddev_delayed_delete
);
537 queue_work(md_misc_wq
, &mddev
->del_work
);
541 spin_unlock(&all_mddevs_lock
);
546 void mddev_init(struct mddev
*mddev
)
548 mutex_init(&mddev
->open_mutex
);
549 mutex_init(&mddev
->reconfig_mutex
);
550 mutex_init(&mddev
->bitmap_info
.mutex
);
551 INIT_LIST_HEAD(&mddev
->disks
);
552 INIT_LIST_HEAD(&mddev
->all_mddevs
);
553 init_timer(&mddev
->safemode_timer
);
554 atomic_set(&mddev
->active
, 1);
555 atomic_set(&mddev
->openers
, 0);
556 atomic_set(&mddev
->active_io
, 0);
557 spin_lock_init(&mddev
->write_lock
);
558 atomic_set(&mddev
->flush_pending
, 0);
559 init_waitqueue_head(&mddev
->sb_wait
);
560 init_waitqueue_head(&mddev
->recovery_wait
);
561 mddev
->reshape_position
= MaxSector
;
562 mddev
->reshape_backwards
= 0;
563 mddev
->resync_min
= 0;
564 mddev
->resync_max
= MaxSector
;
565 mddev
->level
= LEVEL_NONE
;
567 EXPORT_SYMBOL_GPL(mddev_init
);
569 static struct mddev
* mddev_find(dev_t unit
)
571 struct mddev
*mddev
, *new = NULL
;
573 if (unit
&& MAJOR(unit
) != MD_MAJOR
)
574 unit
&= ~((1<<MdpMinorShift
)-1);
577 spin_lock(&all_mddevs_lock
);
580 list_for_each_entry(mddev
, &all_mddevs
, all_mddevs
)
581 if (mddev
->unit
== unit
) {
583 spin_unlock(&all_mddevs_lock
);
589 list_add(&new->all_mddevs
, &all_mddevs
);
590 spin_unlock(&all_mddevs_lock
);
591 new->hold_active
= UNTIL_IOCTL
;
595 /* find an unused unit number */
596 static int next_minor
= 512;
597 int start
= next_minor
;
601 dev
= MKDEV(MD_MAJOR
, next_minor
);
603 if (next_minor
> MINORMASK
)
605 if (next_minor
== start
) {
606 /* Oh dear, all in use. */
607 spin_unlock(&all_mddevs_lock
);
613 list_for_each_entry(mddev
, &all_mddevs
, all_mddevs
)
614 if (mddev
->unit
== dev
) {
620 new->md_minor
= MINOR(dev
);
621 new->hold_active
= UNTIL_STOP
;
622 list_add(&new->all_mddevs
, &all_mddevs
);
623 spin_unlock(&all_mddevs_lock
);
626 spin_unlock(&all_mddevs_lock
);
628 new = kzalloc(sizeof(*new), GFP_KERNEL
);
633 if (MAJOR(unit
) == MD_MAJOR
)
634 new->md_minor
= MINOR(unit
);
636 new->md_minor
= MINOR(unit
) >> MdpMinorShift
;
643 static inline int mddev_lock(struct mddev
* mddev
)
645 return mutex_lock_interruptible(&mddev
->reconfig_mutex
);
648 static inline int mddev_is_locked(struct mddev
*mddev
)
650 return mutex_is_locked(&mddev
->reconfig_mutex
);
653 static inline int mddev_trylock(struct mddev
* mddev
)
655 return mutex_trylock(&mddev
->reconfig_mutex
);
658 static struct attribute_group md_redundancy_group
;
660 static void mddev_unlock(struct mddev
* mddev
)
662 if (mddev
->to_remove
) {
663 /* These cannot be removed under reconfig_mutex as
664 * an access to the files will try to take reconfig_mutex
665 * while holding the file unremovable, which leads to
667 * So hold set sysfs_active while the remove in happeing,
668 * and anything else which might set ->to_remove or my
669 * otherwise change the sysfs namespace will fail with
670 * -EBUSY if sysfs_active is still set.
671 * We set sysfs_active under reconfig_mutex and elsewhere
672 * test it under the same mutex to ensure its correct value
675 struct attribute_group
*to_remove
= mddev
->to_remove
;
676 mddev
->to_remove
= NULL
;
677 mddev
->sysfs_active
= 1;
678 mutex_unlock(&mddev
->reconfig_mutex
);
680 if (mddev
->kobj
.sd
) {
681 if (to_remove
!= &md_redundancy_group
)
682 sysfs_remove_group(&mddev
->kobj
, to_remove
);
683 if (mddev
->pers
== NULL
||
684 mddev
->pers
->sync_request
== NULL
) {
685 sysfs_remove_group(&mddev
->kobj
, &md_redundancy_group
);
686 if (mddev
->sysfs_action
)
687 sysfs_put(mddev
->sysfs_action
);
688 mddev
->sysfs_action
= NULL
;
691 mddev
->sysfs_active
= 0;
693 mutex_unlock(&mddev
->reconfig_mutex
);
695 /* As we've dropped the mutex we need a spinlock to
696 * make sure the thread doesn't disappear
698 spin_lock(&pers_lock
);
699 md_wakeup_thread(mddev
->thread
);
700 spin_unlock(&pers_lock
);
703 static struct md_rdev
* find_rdev_nr(struct mddev
*mddev
, int nr
)
705 struct md_rdev
*rdev
;
707 rdev_for_each(rdev
, mddev
)
708 if (rdev
->desc_nr
== nr
)
714 static struct md_rdev
* find_rdev(struct mddev
* mddev
, dev_t dev
)
716 struct md_rdev
*rdev
;
718 rdev_for_each(rdev
, mddev
)
719 if (rdev
->bdev
->bd_dev
== dev
)
725 static struct md_personality
*find_pers(int level
, char *clevel
)
727 struct md_personality
*pers
;
728 list_for_each_entry(pers
, &pers_list
, list
) {
729 if (level
!= LEVEL_NONE
&& pers
->level
== level
)
731 if (strcmp(pers
->name
, clevel
)==0)
737 /* return the offset of the super block in 512byte sectors */
738 static inline sector_t
calc_dev_sboffset(struct md_rdev
*rdev
)
740 sector_t num_sectors
= i_size_read(rdev
->bdev
->bd_inode
) / 512;
741 return MD_NEW_SIZE_SECTORS(num_sectors
);
744 static int alloc_disk_sb(struct md_rdev
* rdev
)
749 rdev
->sb_page
= alloc_page(GFP_KERNEL
);
750 if (!rdev
->sb_page
) {
751 printk(KERN_ALERT
"md: out of memory.\n");
758 void md_rdev_clear(struct md_rdev
*rdev
)
761 put_page(rdev
->sb_page
);
763 rdev
->sb_page
= NULL
;
768 put_page(rdev
->bb_page
);
769 rdev
->bb_page
= NULL
;
771 kfree(rdev
->badblocks
.page
);
772 rdev
->badblocks
.page
= NULL
;
774 EXPORT_SYMBOL_GPL(md_rdev_clear
);
776 static void super_written(struct bio
*bio
, int error
)
778 struct md_rdev
*rdev
= bio
->bi_private
;
779 struct mddev
*mddev
= rdev
->mddev
;
781 if (error
|| !test_bit(BIO_UPTODATE
, &bio
->bi_flags
)) {
782 printk("md: super_written gets error=%d, uptodate=%d\n",
783 error
, test_bit(BIO_UPTODATE
, &bio
->bi_flags
));
784 WARN_ON(test_bit(BIO_UPTODATE
, &bio
->bi_flags
));
785 md_error(mddev
, rdev
);
788 if (atomic_dec_and_test(&mddev
->pending_writes
))
789 wake_up(&mddev
->sb_wait
);
793 void md_super_write(struct mddev
*mddev
, struct md_rdev
*rdev
,
794 sector_t sector
, int size
, struct page
*page
)
796 /* write first size bytes of page to sector of rdev
797 * Increment mddev->pending_writes before returning
798 * and decrement it on completion, waking up sb_wait
799 * if zero is reached.
800 * If an error occurred, call md_error
802 struct bio
*bio
= bio_alloc_mddev(GFP_NOIO
, 1, mddev
);
804 bio
->bi_bdev
= rdev
->meta_bdev
? rdev
->meta_bdev
: rdev
->bdev
;
805 bio
->bi_sector
= sector
;
806 bio_add_page(bio
, page
, size
, 0);
807 bio
->bi_private
= rdev
;
808 bio
->bi_end_io
= super_written
;
810 atomic_inc(&mddev
->pending_writes
);
811 submit_bio(WRITE_FLUSH_FUA
, bio
);
814 void md_super_wait(struct mddev
*mddev
)
816 /* wait for all superblock writes that were scheduled to complete */
819 prepare_to_wait(&mddev
->sb_wait
, &wq
, TASK_UNINTERRUPTIBLE
);
820 if (atomic_read(&mddev
->pending_writes
)==0)
824 finish_wait(&mddev
->sb_wait
, &wq
);
827 static void bi_complete(struct bio
*bio
, int error
)
829 complete((struct completion
*)bio
->bi_private
);
832 int sync_page_io(struct md_rdev
*rdev
, sector_t sector
, int size
,
833 struct page
*page
, int rw
, bool metadata_op
)
835 struct bio
*bio
= bio_alloc_mddev(GFP_NOIO
, 1, rdev
->mddev
);
836 struct completion event
;
841 bio
->bi_bdev
= (metadata_op
&& rdev
->meta_bdev
) ?
842 rdev
->meta_bdev
: rdev
->bdev
;
844 bio
->bi_sector
= sector
+ rdev
->sb_start
;
845 else if (rdev
->mddev
->reshape_position
!= MaxSector
&&
846 (rdev
->mddev
->reshape_backwards
==
847 (sector
>= rdev
->mddev
->reshape_position
)))
848 bio
->bi_sector
= sector
+ rdev
->new_data_offset
;
850 bio
->bi_sector
= sector
+ rdev
->data_offset
;
851 bio_add_page(bio
, page
, size
, 0);
852 init_completion(&event
);
853 bio
->bi_private
= &event
;
854 bio
->bi_end_io
= bi_complete
;
856 wait_for_completion(&event
);
858 ret
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
862 EXPORT_SYMBOL_GPL(sync_page_io
);
864 static int read_disk_sb(struct md_rdev
* rdev
, int size
)
866 char b
[BDEVNAME_SIZE
];
867 if (!rdev
->sb_page
) {
875 if (!sync_page_io(rdev
, 0, size
, rdev
->sb_page
, READ
, true))
881 printk(KERN_WARNING
"md: disabled device %s, could not read superblock.\n",
882 bdevname(rdev
->bdev
,b
));
886 static int uuid_equal(mdp_super_t
*sb1
, mdp_super_t
*sb2
)
888 return sb1
->set_uuid0
== sb2
->set_uuid0
&&
889 sb1
->set_uuid1
== sb2
->set_uuid1
&&
890 sb1
->set_uuid2
== sb2
->set_uuid2
&&
891 sb1
->set_uuid3
== sb2
->set_uuid3
;
894 static int sb_equal(mdp_super_t
*sb1
, mdp_super_t
*sb2
)
897 mdp_super_t
*tmp1
, *tmp2
;
899 tmp1
= kmalloc(sizeof(*tmp1
),GFP_KERNEL
);
900 tmp2
= kmalloc(sizeof(*tmp2
),GFP_KERNEL
);
902 if (!tmp1
|| !tmp2
) {
904 printk(KERN_INFO
"md.c sb_equal(): failed to allocate memory!\n");
912 * nr_disks is not constant
917 ret
= (memcmp(tmp1
, tmp2
, MD_SB_GENERIC_CONSTANT_WORDS
* 4) == 0);
925 static u32
md_csum_fold(u32 csum
)
927 csum
= (csum
& 0xffff) + (csum
>> 16);
928 return (csum
& 0xffff) + (csum
>> 16);
931 static unsigned int calc_sb_csum(mdp_super_t
* sb
)
934 u32
*sb32
= (u32
*)sb
;
936 unsigned int disk_csum
, csum
;
938 disk_csum
= sb
->sb_csum
;
941 for (i
= 0; i
< MD_SB_BYTES
/4 ; i
++)
943 csum
= (newcsum
& 0xffffffff) + (newcsum
>>32);
947 /* This used to use csum_partial, which was wrong for several
948 * reasons including that different results are returned on
949 * different architectures. It isn't critical that we get exactly
950 * the same return value as before (we always csum_fold before
951 * testing, and that removes any differences). However as we
952 * know that csum_partial always returned a 16bit value on
953 * alphas, do a fold to maximise conformity to previous behaviour.
955 sb
->sb_csum
= md_csum_fold(disk_csum
);
957 sb
->sb_csum
= disk_csum
;
964 * Handle superblock details.
965 * We want to be able to handle multiple superblock formats
966 * so we have a common interface to them all, and an array of
967 * different handlers.
968 * We rely on user-space to write the initial superblock, and support
969 * reading and updating of superblocks.
970 * Interface methods are:
971 * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
972 * loads and validates a superblock on dev.
973 * if refdev != NULL, compare superblocks on both devices
975 * 0 - dev has a superblock that is compatible with refdev
976 * 1 - dev has a superblock that is compatible and newer than refdev
977 * so dev should be used as the refdev in future
978 * -EINVAL superblock incompatible or invalid
979 * -othererror e.g. -EIO
981 * int validate_super(struct mddev *mddev, struct md_rdev *dev)
982 * Verify that dev is acceptable into mddev.
983 * The first time, mddev->raid_disks will be 0, and data from
984 * dev should be merged in. Subsequent calls check that dev
985 * is new enough. Return 0 or -EINVAL
987 * void sync_super(struct mddev *mddev, struct md_rdev *dev)
988 * Update the superblock for rdev with data in mddev
989 * This does not write to disc.
995 struct module
*owner
;
996 int (*load_super
)(struct md_rdev
*rdev
,
997 struct md_rdev
*refdev
,
999 int (*validate_super
)(struct mddev
*mddev
,
1000 struct md_rdev
*rdev
);
1001 void (*sync_super
)(struct mddev
*mddev
,
1002 struct md_rdev
*rdev
);
1003 unsigned long long (*rdev_size_change
)(struct md_rdev
*rdev
,
1004 sector_t num_sectors
);
1005 int (*allow_new_offset
)(struct md_rdev
*rdev
,
1006 unsigned long long new_offset
);
1010 * Check that the given mddev has no bitmap.
1012 * This function is called from the run method of all personalities that do not
1013 * support bitmaps. It prints an error message and returns non-zero if mddev
1014 * has a bitmap. Otherwise, it returns 0.
1017 int md_check_no_bitmap(struct mddev
*mddev
)
1019 if (!mddev
->bitmap_info
.file
&& !mddev
->bitmap_info
.offset
)
1021 printk(KERN_ERR
"%s: bitmaps are not supported for %s\n",
1022 mdname(mddev
), mddev
->pers
->name
);
1025 EXPORT_SYMBOL(md_check_no_bitmap
);
1028 * load_super for 0.90.0
1030 static int super_90_load(struct md_rdev
*rdev
, struct md_rdev
*refdev
, int minor_version
)
1032 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
1037 * Calculate the position of the superblock (512byte sectors),
1038 * it's at the end of the disk.
1040 * It also happens to be a multiple of 4Kb.
1042 rdev
->sb_start
= calc_dev_sboffset(rdev
);
1044 ret
= read_disk_sb(rdev
, MD_SB_BYTES
);
1045 if (ret
) return ret
;
1049 bdevname(rdev
->bdev
, b
);
1050 sb
= page_address(rdev
->sb_page
);
1052 if (sb
->md_magic
!= MD_SB_MAGIC
) {
1053 printk(KERN_ERR
"md: invalid raid superblock magic on %s\n",
1058 if (sb
->major_version
!= 0 ||
1059 sb
->minor_version
< 90 ||
1060 sb
->minor_version
> 91) {
1061 printk(KERN_WARNING
"Bad version number %d.%d on %s\n",
1062 sb
->major_version
, sb
->minor_version
,
1067 if (sb
->raid_disks
<= 0)
1070 if (md_csum_fold(calc_sb_csum(sb
)) != md_csum_fold(sb
->sb_csum
)) {
1071 printk(KERN_WARNING
"md: invalid superblock checksum on %s\n",
1076 rdev
->preferred_minor
= sb
->md_minor
;
1077 rdev
->data_offset
= 0;
1078 rdev
->new_data_offset
= 0;
1079 rdev
->sb_size
= MD_SB_BYTES
;
1080 rdev
->badblocks
.shift
= -1;
1082 if (sb
->level
== LEVEL_MULTIPATH
)
1085 rdev
->desc_nr
= sb
->this_disk
.number
;
1091 mdp_super_t
*refsb
= page_address(refdev
->sb_page
);
1092 if (!uuid_equal(refsb
, sb
)) {
1093 printk(KERN_WARNING
"md: %s has different UUID to %s\n",
1094 b
, bdevname(refdev
->bdev
,b2
));
1097 if (!sb_equal(refsb
, sb
)) {
1098 printk(KERN_WARNING
"md: %s has same UUID"
1099 " but different superblock to %s\n",
1100 b
, bdevname(refdev
->bdev
, b2
));
1104 ev2
= md_event(refsb
);
1110 rdev
->sectors
= rdev
->sb_start
;
1111 /* Limit to 4TB as metadata cannot record more than that */
1112 if (rdev
->sectors
>= (2ULL << 32))
1113 rdev
->sectors
= (2ULL << 32) - 2;
1115 if (rdev
->sectors
< ((sector_t
)sb
->size
) * 2 && sb
->level
>= 1)
1116 /* "this cannot possibly happen" ... */
1124 * validate_super for 0.90.0
1126 static int super_90_validate(struct mddev
*mddev
, struct md_rdev
*rdev
)
1129 mdp_super_t
*sb
= page_address(rdev
->sb_page
);
1130 __u64 ev1
= md_event(sb
);
1132 rdev
->raid_disk
= -1;
1133 clear_bit(Faulty
, &rdev
->flags
);
1134 clear_bit(In_sync
, &rdev
->flags
);
1135 clear_bit(WriteMostly
, &rdev
->flags
);
1137 if (mddev
->raid_disks
== 0) {
1138 mddev
->major_version
= 0;
1139 mddev
->minor_version
= sb
->minor_version
;
1140 mddev
->patch_version
= sb
->patch_version
;
1141 mddev
->external
= 0;
1142 mddev
->chunk_sectors
= sb
->chunk_size
>> 9;
1143 mddev
->ctime
= sb
->ctime
;
1144 mddev
->utime
= sb
->utime
;
1145 mddev
->level
= sb
->level
;
1146 mddev
->clevel
[0] = 0;
1147 mddev
->layout
= sb
->layout
;
1148 mddev
->raid_disks
= sb
->raid_disks
;
1149 mddev
->dev_sectors
= ((sector_t
)sb
->size
) * 2;
1150 mddev
->events
= ev1
;
1151 mddev
->bitmap_info
.offset
= 0;
1152 mddev
->bitmap_info
.space
= 0;
1153 /* bitmap can use 60 K after the 4K superblocks */
1154 mddev
->bitmap_info
.default_offset
= MD_SB_BYTES
>> 9;
1155 mddev
->bitmap_info
.default_space
= 64*2 - (MD_SB_BYTES
>> 9);
1156 mddev
->reshape_backwards
= 0;
1158 if (mddev
->minor_version
>= 91) {
1159 mddev
->reshape_position
= sb
->reshape_position
;
1160 mddev
->delta_disks
= sb
->delta_disks
;
1161 mddev
->new_level
= sb
->new_level
;
1162 mddev
->new_layout
= sb
->new_layout
;
1163 mddev
->new_chunk_sectors
= sb
->new_chunk
>> 9;
1164 if (mddev
->delta_disks
< 0)
1165 mddev
->reshape_backwards
= 1;
1167 mddev
->reshape_position
= MaxSector
;
1168 mddev
->delta_disks
= 0;
1169 mddev
->new_level
= mddev
->level
;
1170 mddev
->new_layout
= mddev
->layout
;
1171 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
1174 if (sb
->state
& (1<<MD_SB_CLEAN
))
1175 mddev
->recovery_cp
= MaxSector
;
1177 if (sb
->events_hi
== sb
->cp_events_hi
&&
1178 sb
->events_lo
== sb
->cp_events_lo
) {
1179 mddev
->recovery_cp
= sb
->recovery_cp
;
1181 mddev
->recovery_cp
= 0;
1184 memcpy(mddev
->uuid
+0, &sb
->set_uuid0
, 4);
1185 memcpy(mddev
->uuid
+4, &sb
->set_uuid1
, 4);
1186 memcpy(mddev
->uuid
+8, &sb
->set_uuid2
, 4);
1187 memcpy(mddev
->uuid
+12,&sb
->set_uuid3
, 4);
1189 mddev
->max_disks
= MD_SB_DISKS
;
1191 if (sb
->state
& (1<<MD_SB_BITMAP_PRESENT
) &&
1192 mddev
->bitmap_info
.file
== NULL
) {
1193 mddev
->bitmap_info
.offset
=
1194 mddev
->bitmap_info
.default_offset
;
1195 mddev
->bitmap_info
.space
=
1196 mddev
->bitmap_info
.space
;
1199 } else if (mddev
->pers
== NULL
) {
1200 /* Insist on good event counter while assembling, except
1201 * for spares (which don't need an event count) */
1203 if (sb
->disks
[rdev
->desc_nr
].state
& (
1204 (1<<MD_DISK_SYNC
) | (1 << MD_DISK_ACTIVE
)))
1205 if (ev1
< mddev
->events
)
1207 } else if (mddev
->bitmap
) {
1208 /* if adding to array with a bitmap, then we can accept an
1209 * older device ... but not too old.
1211 if (ev1
< mddev
->bitmap
->events_cleared
)
1214 if (ev1
< mddev
->events
)
1215 /* just a hot-add of a new device, leave raid_disk at -1 */
1219 if (mddev
->level
!= LEVEL_MULTIPATH
) {
1220 desc
= sb
->disks
+ rdev
->desc_nr
;
1222 if (desc
->state
& (1<<MD_DISK_FAULTY
))
1223 set_bit(Faulty
, &rdev
->flags
);
1224 else if (desc
->state
& (1<<MD_DISK_SYNC
) /* &&
1225 desc->raid_disk < mddev->raid_disks */) {
1226 set_bit(In_sync
, &rdev
->flags
);
1227 rdev
->raid_disk
= desc
->raid_disk
;
1228 } else if (desc
->state
& (1<<MD_DISK_ACTIVE
)) {
1229 /* active but not in sync implies recovery up to
1230 * reshape position. We don't know exactly where
1231 * that is, so set to zero for now */
1232 if (mddev
->minor_version
>= 91) {
1233 rdev
->recovery_offset
= 0;
1234 rdev
->raid_disk
= desc
->raid_disk
;
1237 if (desc
->state
& (1<<MD_DISK_WRITEMOSTLY
))
1238 set_bit(WriteMostly
, &rdev
->flags
);
1239 } else /* MULTIPATH are always insync */
1240 set_bit(In_sync
, &rdev
->flags
);
1245 * sync_super for 0.90.0
1247 static void super_90_sync(struct mddev
*mddev
, struct md_rdev
*rdev
)
1250 struct md_rdev
*rdev2
;
1251 int next_spare
= mddev
->raid_disks
;
1254 /* make rdev->sb match mddev data..
1257 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1258 * 3/ any empty disks < next_spare become removed
1260 * disks[0] gets initialised to REMOVED because
1261 * we cannot be sure from other fields if it has
1262 * been initialised or not.
1265 int active
=0, working
=0,failed
=0,spare
=0,nr_disks
=0;
1267 rdev
->sb_size
= MD_SB_BYTES
;
1269 sb
= page_address(rdev
->sb_page
);
1271 memset(sb
, 0, sizeof(*sb
));
1273 sb
->md_magic
= MD_SB_MAGIC
;
1274 sb
->major_version
= mddev
->major_version
;
1275 sb
->patch_version
= mddev
->patch_version
;
1276 sb
->gvalid_words
= 0; /* ignored */
1277 memcpy(&sb
->set_uuid0
, mddev
->uuid
+0, 4);
1278 memcpy(&sb
->set_uuid1
, mddev
->uuid
+4, 4);
1279 memcpy(&sb
->set_uuid2
, mddev
->uuid
+8, 4);
1280 memcpy(&sb
->set_uuid3
, mddev
->uuid
+12,4);
1282 sb
->ctime
= mddev
->ctime
;
1283 sb
->level
= mddev
->level
;
1284 sb
->size
= mddev
->dev_sectors
/ 2;
1285 sb
->raid_disks
= mddev
->raid_disks
;
1286 sb
->md_minor
= mddev
->md_minor
;
1287 sb
->not_persistent
= 0;
1288 sb
->utime
= mddev
->utime
;
1290 sb
->events_hi
= (mddev
->events
>>32);
1291 sb
->events_lo
= (u32
)mddev
->events
;
1293 if (mddev
->reshape_position
== MaxSector
)
1294 sb
->minor_version
= 90;
1296 sb
->minor_version
= 91;
1297 sb
->reshape_position
= mddev
->reshape_position
;
1298 sb
->new_level
= mddev
->new_level
;
1299 sb
->delta_disks
= mddev
->delta_disks
;
1300 sb
->new_layout
= mddev
->new_layout
;
1301 sb
->new_chunk
= mddev
->new_chunk_sectors
<< 9;
1303 mddev
->minor_version
= sb
->minor_version
;
1306 sb
->recovery_cp
= mddev
->recovery_cp
;
1307 sb
->cp_events_hi
= (mddev
->events
>>32);
1308 sb
->cp_events_lo
= (u32
)mddev
->events
;
1309 if (mddev
->recovery_cp
== MaxSector
)
1310 sb
->state
= (1<< MD_SB_CLEAN
);
1312 sb
->recovery_cp
= 0;
1314 sb
->layout
= mddev
->layout
;
1315 sb
->chunk_size
= mddev
->chunk_sectors
<< 9;
1317 if (mddev
->bitmap
&& mddev
->bitmap_info
.file
== NULL
)
1318 sb
->state
|= (1<<MD_SB_BITMAP_PRESENT
);
1320 sb
->disks
[0].state
= (1<<MD_DISK_REMOVED
);
1321 rdev_for_each(rdev2
, mddev
) {
1324 int is_active
= test_bit(In_sync
, &rdev2
->flags
);
1326 if (rdev2
->raid_disk
>= 0 &&
1327 sb
->minor_version
>= 91)
1328 /* we have nowhere to store the recovery_offset,
1329 * but if it is not below the reshape_position,
1330 * we can piggy-back on that.
1333 if (rdev2
->raid_disk
< 0 ||
1334 test_bit(Faulty
, &rdev2
->flags
))
1337 desc_nr
= rdev2
->raid_disk
;
1339 desc_nr
= next_spare
++;
1340 rdev2
->desc_nr
= desc_nr
;
1341 d
= &sb
->disks
[rdev2
->desc_nr
];
1343 d
->number
= rdev2
->desc_nr
;
1344 d
->major
= MAJOR(rdev2
->bdev
->bd_dev
);
1345 d
->minor
= MINOR(rdev2
->bdev
->bd_dev
);
1347 d
->raid_disk
= rdev2
->raid_disk
;
1349 d
->raid_disk
= rdev2
->desc_nr
; /* compatibility */
1350 if (test_bit(Faulty
, &rdev2
->flags
))
1351 d
->state
= (1<<MD_DISK_FAULTY
);
1352 else if (is_active
) {
1353 d
->state
= (1<<MD_DISK_ACTIVE
);
1354 if (test_bit(In_sync
, &rdev2
->flags
))
1355 d
->state
|= (1<<MD_DISK_SYNC
);
1363 if (test_bit(WriteMostly
, &rdev2
->flags
))
1364 d
->state
|= (1<<MD_DISK_WRITEMOSTLY
);
1366 /* now set the "removed" and "faulty" bits on any missing devices */
1367 for (i
=0 ; i
< mddev
->raid_disks
; i
++) {
1368 mdp_disk_t
*d
= &sb
->disks
[i
];
1369 if (d
->state
== 0 && d
->number
== 0) {
1372 d
->state
= (1<<MD_DISK_REMOVED
);
1373 d
->state
|= (1<<MD_DISK_FAULTY
);
1377 sb
->nr_disks
= nr_disks
;
1378 sb
->active_disks
= active
;
1379 sb
->working_disks
= working
;
1380 sb
->failed_disks
= failed
;
1381 sb
->spare_disks
= spare
;
1383 sb
->this_disk
= sb
->disks
[rdev
->desc_nr
];
1384 sb
->sb_csum
= calc_sb_csum(sb
);
1388 * rdev_size_change for 0.90.0
1390 static unsigned long long
1391 super_90_rdev_size_change(struct md_rdev
*rdev
, sector_t num_sectors
)
1393 if (num_sectors
&& num_sectors
< rdev
->mddev
->dev_sectors
)
1394 return 0; /* component must fit device */
1395 if (rdev
->mddev
->bitmap_info
.offset
)
1396 return 0; /* can't move bitmap */
1397 rdev
->sb_start
= calc_dev_sboffset(rdev
);
1398 if (!num_sectors
|| num_sectors
> rdev
->sb_start
)
1399 num_sectors
= rdev
->sb_start
;
1400 /* Limit to 4TB as metadata cannot record more than that.
1401 * 4TB == 2^32 KB, or 2*2^32 sectors.
1403 if (num_sectors
>= (2ULL << 32))
1404 num_sectors
= (2ULL << 32) - 2;
1405 md_super_write(rdev
->mddev
, rdev
, rdev
->sb_start
, rdev
->sb_size
,
1407 md_super_wait(rdev
->mddev
);
1412 super_90_allow_new_offset(struct md_rdev
*rdev
, unsigned long long new_offset
)
1414 /* non-zero offset changes not possible with v0.90 */
1415 return new_offset
== 0;
1419 * version 1 superblock
1422 static __le32
calc_sb_1_csum(struct mdp_superblock_1
* sb
)
1426 unsigned long long newcsum
;
1427 int size
= 256 + le32_to_cpu(sb
->max_dev
)*2;
1428 __le32
*isuper
= (__le32
*)sb
;
1431 disk_csum
= sb
->sb_csum
;
1434 for (i
=0; size
>=4; size
-= 4 )
1435 newcsum
+= le32_to_cpu(*isuper
++);
1438 newcsum
+= le16_to_cpu(*(__le16
*) isuper
);
1440 csum
= (newcsum
& 0xffffffff) + (newcsum
>> 32);
1441 sb
->sb_csum
= disk_csum
;
1442 return cpu_to_le32(csum
);
1445 static int md_set_badblocks(struct badblocks
*bb
, sector_t s
, int sectors
,
1447 static int super_1_load(struct md_rdev
*rdev
, struct md_rdev
*refdev
, int minor_version
)
1449 struct mdp_superblock_1
*sb
;
1453 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
1457 * Calculate the position of the superblock in 512byte sectors.
1458 * It is always aligned to a 4K boundary and
1459 * depeding on minor_version, it can be:
1460 * 0: At least 8K, but less than 12K, from end of device
1461 * 1: At start of device
1462 * 2: 4K from start of device.
1464 switch(minor_version
) {
1466 sb_start
= i_size_read(rdev
->bdev
->bd_inode
) >> 9;
1468 sb_start
&= ~(sector_t
)(4*2-1);
1479 rdev
->sb_start
= sb_start
;
1481 /* superblock is rarely larger than 1K, but it can be larger,
1482 * and it is safe to read 4k, so we do that
1484 ret
= read_disk_sb(rdev
, 4096);
1485 if (ret
) return ret
;
1488 sb
= page_address(rdev
->sb_page
);
1490 if (sb
->magic
!= cpu_to_le32(MD_SB_MAGIC
) ||
1491 sb
->major_version
!= cpu_to_le32(1) ||
1492 le32_to_cpu(sb
->max_dev
) > (4096-256)/2 ||
1493 le64_to_cpu(sb
->super_offset
) != rdev
->sb_start
||
1494 (le32_to_cpu(sb
->feature_map
) & ~MD_FEATURE_ALL
) != 0)
1497 if (calc_sb_1_csum(sb
) != sb
->sb_csum
) {
1498 printk("md: invalid superblock checksum on %s\n",
1499 bdevname(rdev
->bdev
,b
));
1502 if (le64_to_cpu(sb
->data_size
) < 10) {
1503 printk("md: data_size too small on %s\n",
1504 bdevname(rdev
->bdev
,b
));
1509 memcmp(sb
->pad3
, sb
->pad3
+1, sizeof(sb
->pad3
) - sizeof(sb
->pad3
[1])))
1510 /* Some padding is non-zero, might be a new feature */
1513 rdev
->preferred_minor
= 0xffff;
1514 rdev
->data_offset
= le64_to_cpu(sb
->data_offset
);
1515 rdev
->new_data_offset
= rdev
->data_offset
;
1516 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RESHAPE_ACTIVE
) &&
1517 (le32_to_cpu(sb
->feature_map
) & MD_FEATURE_NEW_OFFSET
))
1518 rdev
->new_data_offset
+= (s32
)le32_to_cpu(sb
->new_offset
);
1519 atomic_set(&rdev
->corrected_errors
, le32_to_cpu(sb
->cnt_corrected_read
));
1521 rdev
->sb_size
= le32_to_cpu(sb
->max_dev
) * 2 + 256;
1522 bmask
= queue_logical_block_size(rdev
->bdev
->bd_disk
->queue
)-1;
1523 if (rdev
->sb_size
& bmask
)
1524 rdev
->sb_size
= (rdev
->sb_size
| bmask
) + 1;
1527 && rdev
->data_offset
< sb_start
+ (rdev
->sb_size
/512))
1530 && rdev
->new_data_offset
< sb_start
+ (rdev
->sb_size
/512))
1533 if (sb
->level
== cpu_to_le32(LEVEL_MULTIPATH
))
1536 rdev
->desc_nr
= le32_to_cpu(sb
->dev_number
);
1538 if (!rdev
->bb_page
) {
1539 rdev
->bb_page
= alloc_page(GFP_KERNEL
);
1543 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_BAD_BLOCKS
) &&
1544 rdev
->badblocks
.count
== 0) {
1545 /* need to load the bad block list.
1546 * Currently we limit it to one page.
1552 int sectors
= le16_to_cpu(sb
->bblog_size
);
1553 if (sectors
> (PAGE_SIZE
/ 512))
1555 offset
= le32_to_cpu(sb
->bblog_offset
);
1558 bb_sector
= (long long)offset
;
1559 if (!sync_page_io(rdev
, bb_sector
, sectors
<< 9,
1560 rdev
->bb_page
, READ
, true))
1562 bbp
= (u64
*)page_address(rdev
->bb_page
);
1563 rdev
->badblocks
.shift
= sb
->bblog_shift
;
1564 for (i
= 0 ; i
< (sectors
<< (9-3)) ; i
++, bbp
++) {
1565 u64 bb
= le64_to_cpu(*bbp
);
1566 int count
= bb
& (0x3ff);
1567 u64 sector
= bb
>> 10;
1568 sector
<<= sb
->bblog_shift
;
1569 count
<<= sb
->bblog_shift
;
1572 if (md_set_badblocks(&rdev
->badblocks
,
1573 sector
, count
, 1) == 0)
1576 } else if (sb
->bblog_offset
== 0)
1577 rdev
->badblocks
.shift
= -1;
1583 struct mdp_superblock_1
*refsb
= page_address(refdev
->sb_page
);
1585 if (memcmp(sb
->set_uuid
, refsb
->set_uuid
, 16) != 0 ||
1586 sb
->level
!= refsb
->level
||
1587 sb
->layout
!= refsb
->layout
||
1588 sb
->chunksize
!= refsb
->chunksize
) {
1589 printk(KERN_WARNING
"md: %s has strangely different"
1590 " superblock to %s\n",
1591 bdevname(rdev
->bdev
,b
),
1592 bdevname(refdev
->bdev
,b2
));
1595 ev1
= le64_to_cpu(sb
->events
);
1596 ev2
= le64_to_cpu(refsb
->events
);
1603 if (minor_version
) {
1604 sectors
= (i_size_read(rdev
->bdev
->bd_inode
) >> 9);
1605 sectors
-= rdev
->data_offset
;
1607 sectors
= rdev
->sb_start
;
1608 if (sectors
< le64_to_cpu(sb
->data_size
))
1610 rdev
->sectors
= le64_to_cpu(sb
->data_size
);
1614 static int super_1_validate(struct mddev
*mddev
, struct md_rdev
*rdev
)
1616 struct mdp_superblock_1
*sb
= page_address(rdev
->sb_page
);
1617 __u64 ev1
= le64_to_cpu(sb
->events
);
1619 rdev
->raid_disk
= -1;
1620 clear_bit(Faulty
, &rdev
->flags
);
1621 clear_bit(In_sync
, &rdev
->flags
);
1622 clear_bit(WriteMostly
, &rdev
->flags
);
1624 if (mddev
->raid_disks
== 0) {
1625 mddev
->major_version
= 1;
1626 mddev
->patch_version
= 0;
1627 mddev
->external
= 0;
1628 mddev
->chunk_sectors
= le32_to_cpu(sb
->chunksize
);
1629 mddev
->ctime
= le64_to_cpu(sb
->ctime
) & ((1ULL << 32)-1);
1630 mddev
->utime
= le64_to_cpu(sb
->utime
) & ((1ULL << 32)-1);
1631 mddev
->level
= le32_to_cpu(sb
->level
);
1632 mddev
->clevel
[0] = 0;
1633 mddev
->layout
= le32_to_cpu(sb
->layout
);
1634 mddev
->raid_disks
= le32_to_cpu(sb
->raid_disks
);
1635 mddev
->dev_sectors
= le64_to_cpu(sb
->size
);
1636 mddev
->events
= ev1
;
1637 mddev
->bitmap_info
.offset
= 0;
1638 mddev
->bitmap_info
.space
= 0;
1639 /* Default location for bitmap is 1K after superblock
1640 * using 3K - total of 4K
1642 mddev
->bitmap_info
.default_offset
= 1024 >> 9;
1643 mddev
->bitmap_info
.default_space
= (4096-1024) >> 9;
1644 mddev
->reshape_backwards
= 0;
1646 mddev
->recovery_cp
= le64_to_cpu(sb
->resync_offset
);
1647 memcpy(mddev
->uuid
, sb
->set_uuid
, 16);
1649 mddev
->max_disks
= (4096-256)/2;
1651 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_BITMAP_OFFSET
) &&
1652 mddev
->bitmap_info
.file
== NULL
) {
1653 mddev
->bitmap_info
.offset
=
1654 (__s32
)le32_to_cpu(sb
->bitmap_offset
);
1655 /* Metadata doesn't record how much space is available.
1656 * For 1.0, we assume we can use up to the superblock
1657 * if before, else to 4K beyond superblock.
1658 * For others, assume no change is possible.
1660 if (mddev
->minor_version
> 0)
1661 mddev
->bitmap_info
.space
= 0;
1662 else if (mddev
->bitmap_info
.offset
> 0)
1663 mddev
->bitmap_info
.space
=
1664 8 - mddev
->bitmap_info
.offset
;
1666 mddev
->bitmap_info
.space
=
1667 -mddev
->bitmap_info
.offset
;
1670 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RESHAPE_ACTIVE
)) {
1671 mddev
->reshape_position
= le64_to_cpu(sb
->reshape_position
);
1672 mddev
->delta_disks
= le32_to_cpu(sb
->delta_disks
);
1673 mddev
->new_level
= le32_to_cpu(sb
->new_level
);
1674 mddev
->new_layout
= le32_to_cpu(sb
->new_layout
);
1675 mddev
->new_chunk_sectors
= le32_to_cpu(sb
->new_chunk
);
1676 if (mddev
->delta_disks
< 0 ||
1677 (mddev
->delta_disks
== 0 &&
1678 (le32_to_cpu(sb
->feature_map
)
1679 & MD_FEATURE_RESHAPE_BACKWARDS
)))
1680 mddev
->reshape_backwards
= 1;
1682 mddev
->reshape_position
= MaxSector
;
1683 mddev
->delta_disks
= 0;
1684 mddev
->new_level
= mddev
->level
;
1685 mddev
->new_layout
= mddev
->layout
;
1686 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
1689 } else if (mddev
->pers
== NULL
) {
1690 /* Insist of good event counter while assembling, except for
1691 * spares (which don't need an event count) */
1693 if (rdev
->desc_nr
>= 0 &&
1694 rdev
->desc_nr
< le32_to_cpu(sb
->max_dev
) &&
1695 le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]) < 0xfffe)
1696 if (ev1
< mddev
->events
)
1698 } else if (mddev
->bitmap
) {
1699 /* If adding to array with a bitmap, then we can accept an
1700 * older device, but not too old.
1702 if (ev1
< mddev
->bitmap
->events_cleared
)
1705 if (ev1
< mddev
->events
)
1706 /* just a hot-add of a new device, leave raid_disk at -1 */
1709 if (mddev
->level
!= LEVEL_MULTIPATH
) {
1711 if (rdev
->desc_nr
< 0 ||
1712 rdev
->desc_nr
>= le32_to_cpu(sb
->max_dev
)) {
1716 role
= le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]);
1718 case 0xffff: /* spare */
1720 case 0xfffe: /* faulty */
1721 set_bit(Faulty
, &rdev
->flags
);
1724 if ((le32_to_cpu(sb
->feature_map
) &
1725 MD_FEATURE_RECOVERY_OFFSET
))
1726 rdev
->recovery_offset
= le64_to_cpu(sb
->recovery_offset
);
1728 set_bit(In_sync
, &rdev
->flags
);
1729 rdev
->raid_disk
= role
;
1732 if (sb
->devflags
& WriteMostly1
)
1733 set_bit(WriteMostly
, &rdev
->flags
);
1734 if (le32_to_cpu(sb
->feature_map
) & MD_FEATURE_REPLACEMENT
)
1735 set_bit(Replacement
, &rdev
->flags
);
1736 } else /* MULTIPATH are always insync */
1737 set_bit(In_sync
, &rdev
->flags
);
1742 static void super_1_sync(struct mddev
*mddev
, struct md_rdev
*rdev
)
1744 struct mdp_superblock_1
*sb
;
1745 struct md_rdev
*rdev2
;
1747 /* make rdev->sb match mddev and rdev data. */
1749 sb
= page_address(rdev
->sb_page
);
1751 sb
->feature_map
= 0;
1753 sb
->recovery_offset
= cpu_to_le64(0);
1754 memset(sb
->pad3
, 0, sizeof(sb
->pad3
));
1756 sb
->utime
= cpu_to_le64((__u64
)mddev
->utime
);
1757 sb
->events
= cpu_to_le64(mddev
->events
);
1759 sb
->resync_offset
= cpu_to_le64(mddev
->recovery_cp
);
1761 sb
->resync_offset
= cpu_to_le64(0);
1763 sb
->cnt_corrected_read
= cpu_to_le32(atomic_read(&rdev
->corrected_errors
));
1765 sb
->raid_disks
= cpu_to_le32(mddev
->raid_disks
);
1766 sb
->size
= cpu_to_le64(mddev
->dev_sectors
);
1767 sb
->chunksize
= cpu_to_le32(mddev
->chunk_sectors
);
1768 sb
->level
= cpu_to_le32(mddev
->level
);
1769 sb
->layout
= cpu_to_le32(mddev
->layout
);
1771 if (test_bit(WriteMostly
, &rdev
->flags
))
1772 sb
->devflags
|= WriteMostly1
;
1774 sb
->devflags
&= ~WriteMostly1
;
1775 sb
->data_offset
= cpu_to_le64(rdev
->data_offset
);
1776 sb
->data_size
= cpu_to_le64(rdev
->sectors
);
1778 if (mddev
->bitmap
&& mddev
->bitmap_info
.file
== NULL
) {
1779 sb
->bitmap_offset
= cpu_to_le32((__u32
)mddev
->bitmap_info
.offset
);
1780 sb
->feature_map
= cpu_to_le32(MD_FEATURE_BITMAP_OFFSET
);
1783 if (rdev
->raid_disk
>= 0 &&
1784 !test_bit(In_sync
, &rdev
->flags
)) {
1786 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET
);
1787 sb
->recovery_offset
=
1788 cpu_to_le64(rdev
->recovery_offset
);
1790 if (test_bit(Replacement
, &rdev
->flags
))
1792 cpu_to_le32(MD_FEATURE_REPLACEMENT
);
1794 if (mddev
->reshape_position
!= MaxSector
) {
1795 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE
);
1796 sb
->reshape_position
= cpu_to_le64(mddev
->reshape_position
);
1797 sb
->new_layout
= cpu_to_le32(mddev
->new_layout
);
1798 sb
->delta_disks
= cpu_to_le32(mddev
->delta_disks
);
1799 sb
->new_level
= cpu_to_le32(mddev
->new_level
);
1800 sb
->new_chunk
= cpu_to_le32(mddev
->new_chunk_sectors
);
1801 if (mddev
->delta_disks
== 0 &&
1802 mddev
->reshape_backwards
)
1804 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS
);
1805 if (rdev
->new_data_offset
!= rdev
->data_offset
) {
1807 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET
);
1808 sb
->new_offset
= cpu_to_le32((__u32
)(rdev
->new_data_offset
1809 - rdev
->data_offset
));
1813 if (rdev
->badblocks
.count
== 0)
1814 /* Nothing to do for bad blocks*/ ;
1815 else if (sb
->bblog_offset
== 0)
1816 /* Cannot record bad blocks on this device */
1817 md_error(mddev
, rdev
);
1819 struct badblocks
*bb
= &rdev
->badblocks
;
1820 u64
*bbp
= (u64
*)page_address(rdev
->bb_page
);
1822 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_BAD_BLOCKS
);
1827 seq
= read_seqbegin(&bb
->lock
);
1829 memset(bbp
, 0xff, PAGE_SIZE
);
1831 for (i
= 0 ; i
< bb
->count
; i
++) {
1832 u64 internal_bb
= *p
++;
1833 u64 store_bb
= ((BB_OFFSET(internal_bb
) << 10)
1834 | BB_LEN(internal_bb
));
1835 *bbp
++ = cpu_to_le64(store_bb
);
1838 if (read_seqretry(&bb
->lock
, seq
))
1841 bb
->sector
= (rdev
->sb_start
+
1842 (int)le32_to_cpu(sb
->bblog_offset
));
1843 bb
->size
= le16_to_cpu(sb
->bblog_size
);
1848 rdev_for_each(rdev2
, mddev
)
1849 if (rdev2
->desc_nr
+1 > max_dev
)
1850 max_dev
= rdev2
->desc_nr
+1;
1852 if (max_dev
> le32_to_cpu(sb
->max_dev
)) {
1854 sb
->max_dev
= cpu_to_le32(max_dev
);
1855 rdev
->sb_size
= max_dev
* 2 + 256;
1856 bmask
= queue_logical_block_size(rdev
->bdev
->bd_disk
->queue
)-1;
1857 if (rdev
->sb_size
& bmask
)
1858 rdev
->sb_size
= (rdev
->sb_size
| bmask
) + 1;
1860 max_dev
= le32_to_cpu(sb
->max_dev
);
1862 for (i
=0; i
<max_dev
;i
++)
1863 sb
->dev_roles
[i
] = cpu_to_le16(0xfffe);
1865 rdev_for_each(rdev2
, mddev
) {
1867 if (test_bit(Faulty
, &rdev2
->flags
))
1868 sb
->dev_roles
[i
] = cpu_to_le16(0xfffe);
1869 else if (test_bit(In_sync
, &rdev2
->flags
))
1870 sb
->dev_roles
[i
] = cpu_to_le16(rdev2
->raid_disk
);
1871 else if (rdev2
->raid_disk
>= 0)
1872 sb
->dev_roles
[i
] = cpu_to_le16(rdev2
->raid_disk
);
1874 sb
->dev_roles
[i
] = cpu_to_le16(0xffff);
1877 sb
->sb_csum
= calc_sb_1_csum(sb
);
1880 static unsigned long long
1881 super_1_rdev_size_change(struct md_rdev
*rdev
, sector_t num_sectors
)
1883 struct mdp_superblock_1
*sb
;
1884 sector_t max_sectors
;
1885 if (num_sectors
&& num_sectors
< rdev
->mddev
->dev_sectors
)
1886 return 0; /* component must fit device */
1887 if (rdev
->data_offset
!= rdev
->new_data_offset
)
1888 return 0; /* too confusing */
1889 if (rdev
->sb_start
< rdev
->data_offset
) {
1890 /* minor versions 1 and 2; superblock before data */
1891 max_sectors
= i_size_read(rdev
->bdev
->bd_inode
) >> 9;
1892 max_sectors
-= rdev
->data_offset
;
1893 if (!num_sectors
|| num_sectors
> max_sectors
)
1894 num_sectors
= max_sectors
;
1895 } else if (rdev
->mddev
->bitmap_info
.offset
) {
1896 /* minor version 0 with bitmap we can't move */
1899 /* minor version 0; superblock after data */
1901 sb_start
= (i_size_read(rdev
->bdev
->bd_inode
) >> 9) - 8*2;
1902 sb_start
&= ~(sector_t
)(4*2 - 1);
1903 max_sectors
= rdev
->sectors
+ sb_start
- rdev
->sb_start
;
1904 if (!num_sectors
|| num_sectors
> max_sectors
)
1905 num_sectors
= max_sectors
;
1906 rdev
->sb_start
= sb_start
;
1908 sb
= page_address(rdev
->sb_page
);
1909 sb
->data_size
= cpu_to_le64(num_sectors
);
1910 sb
->super_offset
= rdev
->sb_start
;
1911 sb
->sb_csum
= calc_sb_1_csum(sb
);
1912 md_super_write(rdev
->mddev
, rdev
, rdev
->sb_start
, rdev
->sb_size
,
1914 md_super_wait(rdev
->mddev
);
1920 super_1_allow_new_offset(struct md_rdev
*rdev
,
1921 unsigned long long new_offset
)
1923 /* All necessary checks on new >= old have been done */
1924 struct bitmap
*bitmap
;
1925 if (new_offset
>= rdev
->data_offset
)
1928 /* with 1.0 metadata, there is no metadata to tread on
1929 * so we can always move back */
1930 if (rdev
->mddev
->minor_version
== 0)
1933 /* otherwise we must be sure not to step on
1934 * any metadata, so stay:
1935 * 36K beyond start of superblock
1936 * beyond end of badblocks
1937 * beyond write-intent bitmap
1939 if (rdev
->sb_start
+ (32+4)*2 > new_offset
)
1941 bitmap
= rdev
->mddev
->bitmap
;
1942 if (bitmap
&& !rdev
->mddev
->bitmap_info
.file
&&
1943 rdev
->sb_start
+ rdev
->mddev
->bitmap_info
.offset
+
1944 bitmap
->storage
.file_pages
* (PAGE_SIZE
>>9) > new_offset
)
1946 if (rdev
->badblocks
.sector
+ rdev
->badblocks
.size
> new_offset
)
1952 static struct super_type super_types
[] = {
1955 .owner
= THIS_MODULE
,
1956 .load_super
= super_90_load
,
1957 .validate_super
= super_90_validate
,
1958 .sync_super
= super_90_sync
,
1959 .rdev_size_change
= super_90_rdev_size_change
,
1960 .allow_new_offset
= super_90_allow_new_offset
,
1964 .owner
= THIS_MODULE
,
1965 .load_super
= super_1_load
,
1966 .validate_super
= super_1_validate
,
1967 .sync_super
= super_1_sync
,
1968 .rdev_size_change
= super_1_rdev_size_change
,
1969 .allow_new_offset
= super_1_allow_new_offset
,
1973 static void sync_super(struct mddev
*mddev
, struct md_rdev
*rdev
)
1975 if (mddev
->sync_super
) {
1976 mddev
->sync_super(mddev
, rdev
);
1980 BUG_ON(mddev
->major_version
>= ARRAY_SIZE(super_types
));
1982 super_types
[mddev
->major_version
].sync_super(mddev
, rdev
);
1985 static int match_mddev_units(struct mddev
*mddev1
, struct mddev
*mddev2
)
1987 struct md_rdev
*rdev
, *rdev2
;
1990 rdev_for_each_rcu(rdev
, mddev1
)
1991 rdev_for_each_rcu(rdev2
, mddev2
)
1992 if (rdev
->bdev
->bd_contains
==
1993 rdev2
->bdev
->bd_contains
) {
2001 static LIST_HEAD(pending_raid_disks
);
2004 * Try to register data integrity profile for an mddev
2006 * This is called when an array is started and after a disk has been kicked
2007 * from the array. It only succeeds if all working and active component devices
2008 * are integrity capable with matching profiles.
2010 int md_integrity_register(struct mddev
*mddev
)
2012 struct md_rdev
*rdev
, *reference
= NULL
;
2014 if (list_empty(&mddev
->disks
))
2015 return 0; /* nothing to do */
2016 if (!mddev
->gendisk
|| blk_get_integrity(mddev
->gendisk
))
2017 return 0; /* shouldn't register, or already is */
2018 rdev_for_each(rdev
, mddev
) {
2019 /* skip spares and non-functional disks */
2020 if (test_bit(Faulty
, &rdev
->flags
))
2022 if (rdev
->raid_disk
< 0)
2025 /* Use the first rdev as the reference */
2029 /* does this rdev's profile match the reference profile? */
2030 if (blk_integrity_compare(reference
->bdev
->bd_disk
,
2031 rdev
->bdev
->bd_disk
) < 0)
2034 if (!reference
|| !bdev_get_integrity(reference
->bdev
))
2037 * All component devices are integrity capable and have matching
2038 * profiles, register the common profile for the md device.
2040 if (blk_integrity_register(mddev
->gendisk
,
2041 bdev_get_integrity(reference
->bdev
)) != 0) {
2042 printk(KERN_ERR
"md: failed to register integrity for %s\n",
2046 printk(KERN_NOTICE
"md: data integrity enabled on %s\n", mdname(mddev
));
2047 if (bioset_integrity_create(mddev
->bio_set
, BIO_POOL_SIZE
)) {
2048 printk(KERN_ERR
"md: failed to create integrity pool for %s\n",
2054 EXPORT_SYMBOL(md_integrity_register
);
2056 /* Disable data integrity if non-capable/non-matching disk is being added */
2057 void md_integrity_add_rdev(struct md_rdev
*rdev
, struct mddev
*mddev
)
2059 struct blk_integrity
*bi_rdev
= bdev_get_integrity(rdev
->bdev
);
2060 struct blk_integrity
*bi_mddev
= blk_get_integrity(mddev
->gendisk
);
2062 if (!bi_mddev
) /* nothing to do */
2064 if (rdev
->raid_disk
< 0) /* skip spares */
2066 if (bi_rdev
&& blk_integrity_compare(mddev
->gendisk
,
2067 rdev
->bdev
->bd_disk
) >= 0)
2069 printk(KERN_NOTICE
"disabling data integrity on %s\n", mdname(mddev
));
2070 blk_integrity_unregister(mddev
->gendisk
);
2072 EXPORT_SYMBOL(md_integrity_add_rdev
);
2074 static int bind_rdev_to_array(struct md_rdev
* rdev
, struct mddev
* mddev
)
2076 char b
[BDEVNAME_SIZE
];
2086 /* prevent duplicates */
2087 if (find_rdev(mddev
, rdev
->bdev
->bd_dev
))
2090 /* make sure rdev->sectors exceeds mddev->dev_sectors */
2091 if (rdev
->sectors
&& (mddev
->dev_sectors
== 0 ||
2092 rdev
->sectors
< mddev
->dev_sectors
)) {
2094 /* Cannot change size, so fail
2095 * If mddev->level <= 0, then we don't care
2096 * about aligning sizes (e.g. linear)
2098 if (mddev
->level
> 0)
2101 mddev
->dev_sectors
= rdev
->sectors
;
2104 /* Verify rdev->desc_nr is unique.
2105 * If it is -1, assign a free number, else
2106 * check number is not in use
2108 if (rdev
->desc_nr
< 0) {
2110 if (mddev
->pers
) choice
= mddev
->raid_disks
;
2111 while (find_rdev_nr(mddev
, choice
))
2113 rdev
->desc_nr
= choice
;
2115 if (find_rdev_nr(mddev
, rdev
->desc_nr
))
2118 if (mddev
->max_disks
&& rdev
->desc_nr
>= mddev
->max_disks
) {
2119 printk(KERN_WARNING
"md: %s: array is limited to %d devices\n",
2120 mdname(mddev
), mddev
->max_disks
);
2123 bdevname(rdev
->bdev
,b
);
2124 while ( (s
=strchr(b
, '/')) != NULL
)
2127 rdev
->mddev
= mddev
;
2128 printk(KERN_INFO
"md: bind<%s>\n", b
);
2130 if ((err
= kobject_add(&rdev
->kobj
, &mddev
->kobj
, "dev-%s", b
)))
2133 ko
= &part_to_dev(rdev
->bdev
->bd_part
)->kobj
;
2134 if (sysfs_create_link(&rdev
->kobj
, ko
, "block"))
2135 /* failure here is OK */;
2136 rdev
->sysfs_state
= sysfs_get_dirent_safe(rdev
->kobj
.sd
, "state");
2138 list_add_rcu(&rdev
->same_set
, &mddev
->disks
);
2139 bd_link_disk_holder(rdev
->bdev
, mddev
->gendisk
);
2141 /* May as well allow recovery to be retried once */
2142 mddev
->recovery_disabled
++;
2147 printk(KERN_WARNING
"md: failed to register dev-%s for %s\n",
2152 static void md_delayed_delete(struct work_struct
*ws
)
2154 struct md_rdev
*rdev
= container_of(ws
, struct md_rdev
, del_work
);
2155 kobject_del(&rdev
->kobj
);
2156 kobject_put(&rdev
->kobj
);
2159 static void unbind_rdev_from_array(struct md_rdev
* rdev
)
2161 char b
[BDEVNAME_SIZE
];
2166 bd_unlink_disk_holder(rdev
->bdev
, rdev
->mddev
->gendisk
);
2167 list_del_rcu(&rdev
->same_set
);
2168 printk(KERN_INFO
"md: unbind<%s>\n", bdevname(rdev
->bdev
,b
));
2170 sysfs_remove_link(&rdev
->kobj
, "block");
2171 sysfs_put(rdev
->sysfs_state
);
2172 rdev
->sysfs_state
= NULL
;
2173 rdev
->badblocks
.count
= 0;
2174 /* We need to delay this, otherwise we can deadlock when
2175 * writing to 'remove' to "dev/state". We also need
2176 * to delay it due to rcu usage.
2179 INIT_WORK(&rdev
->del_work
, md_delayed_delete
);
2180 kobject_get(&rdev
->kobj
);
2181 queue_work(md_misc_wq
, &rdev
->del_work
);
2185 * prevent the device from being mounted, repartitioned or
2186 * otherwise reused by a RAID array (or any other kernel
2187 * subsystem), by bd_claiming the device.
2189 static int lock_rdev(struct md_rdev
*rdev
, dev_t dev
, int shared
)
2192 struct block_device
*bdev
;
2193 char b
[BDEVNAME_SIZE
];
2195 bdev
= blkdev_get_by_dev(dev
, FMODE_READ
|FMODE_WRITE
|FMODE_EXCL
,
2196 shared
? (struct md_rdev
*)lock_rdev
: rdev
);
2198 printk(KERN_ERR
"md: could not open %s.\n",
2199 __bdevname(dev
, b
));
2200 return PTR_ERR(bdev
);
2206 static void unlock_rdev(struct md_rdev
*rdev
)
2208 struct block_device
*bdev
= rdev
->bdev
;
2212 blkdev_put(bdev
, FMODE_READ
|FMODE_WRITE
|FMODE_EXCL
);
2215 void md_autodetect_dev(dev_t dev
);
2217 static void export_rdev(struct md_rdev
* rdev
)
2219 char b
[BDEVNAME_SIZE
];
2220 printk(KERN_INFO
"md: export_rdev(%s)\n",
2221 bdevname(rdev
->bdev
,b
));
2224 md_rdev_clear(rdev
);
2226 if (test_bit(AutoDetected
, &rdev
->flags
))
2227 md_autodetect_dev(rdev
->bdev
->bd_dev
);
2230 kobject_put(&rdev
->kobj
);
2233 static void kick_rdev_from_array(struct md_rdev
* rdev
)
2235 unbind_rdev_from_array(rdev
);
2239 static void export_array(struct mddev
*mddev
)
2241 struct md_rdev
*rdev
, *tmp
;
2243 rdev_for_each_safe(rdev
, tmp
, mddev
) {
2248 kick_rdev_from_array(rdev
);
2250 if (!list_empty(&mddev
->disks
))
2252 mddev
->raid_disks
= 0;
2253 mddev
->major_version
= 0;
2256 static void print_desc(mdp_disk_t
*desc
)
2258 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc
->number
,
2259 desc
->major
,desc
->minor
,desc
->raid_disk
,desc
->state
);
2262 static void print_sb_90(mdp_super_t
*sb
)
2267 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
2268 sb
->major_version
, sb
->minor_version
, sb
->patch_version
,
2269 sb
->set_uuid0
, sb
->set_uuid1
, sb
->set_uuid2
, sb
->set_uuid3
,
2271 printk(KERN_INFO
"md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
2272 sb
->level
, sb
->size
, sb
->nr_disks
, sb
->raid_disks
,
2273 sb
->md_minor
, sb
->layout
, sb
->chunk_size
);
2274 printk(KERN_INFO
"md: UT:%08x ST:%d AD:%d WD:%d"
2275 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
2276 sb
->utime
, sb
->state
, sb
->active_disks
, sb
->working_disks
,
2277 sb
->failed_disks
, sb
->spare_disks
,
2278 sb
->sb_csum
, (unsigned long)sb
->events_lo
);
2281 for (i
= 0; i
< MD_SB_DISKS
; i
++) {
2284 desc
= sb
->disks
+ i
;
2285 if (desc
->number
|| desc
->major
|| desc
->minor
||
2286 desc
->raid_disk
|| (desc
->state
&& (desc
->state
!= 4))) {
2287 printk(" D %2d: ", i
);
2291 printk(KERN_INFO
"md: THIS: ");
2292 print_desc(&sb
->this_disk
);
2295 static void print_sb_1(struct mdp_superblock_1
*sb
)
2299 uuid
= sb
->set_uuid
;
2301 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n"
2302 "md: Name: \"%s\" CT:%llu\n",
2303 le32_to_cpu(sb
->major_version
),
2304 le32_to_cpu(sb
->feature_map
),
2307 (unsigned long long)le64_to_cpu(sb
->ctime
)
2308 & MD_SUPERBLOCK_1_TIME_SEC_MASK
);
2310 uuid
= sb
->device_uuid
;
2312 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
2314 "md: Dev:%08x UUID: %pU\n"
2315 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
2316 "md: (MaxDev:%u) \n",
2317 le32_to_cpu(sb
->level
),
2318 (unsigned long long)le64_to_cpu(sb
->size
),
2319 le32_to_cpu(sb
->raid_disks
),
2320 le32_to_cpu(sb
->layout
),
2321 le32_to_cpu(sb
->chunksize
),
2322 (unsigned long long)le64_to_cpu(sb
->data_offset
),
2323 (unsigned long long)le64_to_cpu(sb
->data_size
),
2324 (unsigned long long)le64_to_cpu(sb
->super_offset
),
2325 (unsigned long long)le64_to_cpu(sb
->recovery_offset
),
2326 le32_to_cpu(sb
->dev_number
),
2329 (unsigned long long)le64_to_cpu(sb
->utime
) & MD_SUPERBLOCK_1_TIME_SEC_MASK
,
2330 (unsigned long long)le64_to_cpu(sb
->events
),
2331 (unsigned long long)le64_to_cpu(sb
->resync_offset
),
2332 le32_to_cpu(sb
->sb_csum
),
2333 le32_to_cpu(sb
->max_dev
)
2337 static void print_rdev(struct md_rdev
*rdev
, int major_version
)
2339 char b
[BDEVNAME_SIZE
];
2340 printk(KERN_INFO
"md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
2341 bdevname(rdev
->bdev
, b
), (unsigned long long)rdev
->sectors
,
2342 test_bit(Faulty
, &rdev
->flags
), test_bit(In_sync
, &rdev
->flags
),
2344 if (rdev
->sb_loaded
) {
2345 printk(KERN_INFO
"md: rdev superblock (MJ:%d):\n", major_version
);
2346 switch (major_version
) {
2348 print_sb_90(page_address(rdev
->sb_page
));
2351 print_sb_1(page_address(rdev
->sb_page
));
2355 printk(KERN_INFO
"md: no rdev superblock!\n");
2358 static void md_print_devices(void)
2360 struct list_head
*tmp
;
2361 struct md_rdev
*rdev
;
2362 struct mddev
*mddev
;
2363 char b
[BDEVNAME_SIZE
];
2366 printk("md: **********************************\n");
2367 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
2368 printk("md: **********************************\n");
2369 for_each_mddev(mddev
, tmp
) {
2372 bitmap_print_sb(mddev
->bitmap
);
2374 printk("%s: ", mdname(mddev
));
2375 rdev_for_each(rdev
, mddev
)
2376 printk("<%s>", bdevname(rdev
->bdev
,b
));
2379 rdev_for_each(rdev
, mddev
)
2380 print_rdev(rdev
, mddev
->major_version
);
2382 printk("md: **********************************\n");
2387 static void sync_sbs(struct mddev
* mddev
, int nospares
)
2389 /* Update each superblock (in-memory image), but
2390 * if we are allowed to, skip spares which already
2391 * have the right event counter, or have one earlier
2392 * (which would mean they aren't being marked as dirty
2393 * with the rest of the array)
2395 struct md_rdev
*rdev
;
2396 rdev_for_each(rdev
, mddev
) {
2397 if (rdev
->sb_events
== mddev
->events
||
2399 rdev
->raid_disk
< 0 &&
2400 rdev
->sb_events
+1 == mddev
->events
)) {
2401 /* Don't update this superblock */
2402 rdev
->sb_loaded
= 2;
2404 sync_super(mddev
, rdev
);
2405 rdev
->sb_loaded
= 1;
2410 static void md_update_sb(struct mddev
* mddev
, int force_change
)
2412 struct md_rdev
*rdev
;
2415 int any_badblocks_changed
= 0;
2418 /* First make sure individual recovery_offsets are correct */
2419 rdev_for_each(rdev
, mddev
) {
2420 if (rdev
->raid_disk
>= 0 &&
2421 mddev
->delta_disks
>= 0 &&
2422 !test_bit(In_sync
, &rdev
->flags
) &&
2423 mddev
->curr_resync_completed
> rdev
->recovery_offset
)
2424 rdev
->recovery_offset
= mddev
->curr_resync_completed
;
2427 if (!mddev
->persistent
) {
2428 clear_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
2429 clear_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
2430 if (!mddev
->external
) {
2431 clear_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
2432 rdev_for_each(rdev
, mddev
) {
2433 if (rdev
->badblocks
.changed
) {
2434 rdev
->badblocks
.changed
= 0;
2435 md_ack_all_badblocks(&rdev
->badblocks
);
2436 md_error(mddev
, rdev
);
2438 clear_bit(Blocked
, &rdev
->flags
);
2439 clear_bit(BlockedBadBlocks
, &rdev
->flags
);
2440 wake_up(&rdev
->blocked_wait
);
2443 wake_up(&mddev
->sb_wait
);
2447 spin_lock_irq(&mddev
->write_lock
);
2449 mddev
->utime
= get_seconds();
2451 if (test_and_clear_bit(MD_CHANGE_DEVS
, &mddev
->flags
))
2453 if (test_and_clear_bit(MD_CHANGE_CLEAN
, &mddev
->flags
))
2454 /* just a clean<-> dirty transition, possibly leave spares alone,
2455 * though if events isn't the right even/odd, we will have to do
2461 if (mddev
->degraded
)
2462 /* If the array is degraded, then skipping spares is both
2463 * dangerous and fairly pointless.
2464 * Dangerous because a device that was removed from the array
2465 * might have a event_count that still looks up-to-date,
2466 * so it can be re-added without a resync.
2467 * Pointless because if there are any spares to skip,
2468 * then a recovery will happen and soon that array won't
2469 * be degraded any more and the spare can go back to sleep then.
2473 sync_req
= mddev
->in_sync
;
2475 /* If this is just a dirty<->clean transition, and the array is clean
2476 * and 'events' is odd, we can roll back to the previous clean state */
2478 && (mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
)
2479 && mddev
->can_decrease_events
2480 && mddev
->events
!= 1) {
2482 mddev
->can_decrease_events
= 0;
2484 /* otherwise we have to go forward and ... */
2486 mddev
->can_decrease_events
= nospares
;
2489 if (!mddev
->events
) {
2491 * oops, this 64-bit counter should never wrap.
2492 * Either we are in around ~1 trillion A.C., assuming
2493 * 1 reboot per second, or we have a bug:
2499 rdev_for_each(rdev
, mddev
) {
2500 if (rdev
->badblocks
.changed
)
2501 any_badblocks_changed
++;
2502 if (test_bit(Faulty
, &rdev
->flags
))
2503 set_bit(FaultRecorded
, &rdev
->flags
);
2506 sync_sbs(mddev
, nospares
);
2507 spin_unlock_irq(&mddev
->write_lock
);
2509 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2510 mdname(mddev
), mddev
->in_sync
);
2512 bitmap_update_sb(mddev
->bitmap
);
2513 rdev_for_each(rdev
, mddev
) {
2514 char b
[BDEVNAME_SIZE
];
2516 if (rdev
->sb_loaded
!= 1)
2517 continue; /* no noise on spare devices */
2519 if (!test_bit(Faulty
, &rdev
->flags
) &&
2520 rdev
->saved_raid_disk
== -1) {
2521 md_super_write(mddev
,rdev
,
2522 rdev
->sb_start
, rdev
->sb_size
,
2524 pr_debug("md: (write) %s's sb offset: %llu\n",
2525 bdevname(rdev
->bdev
, b
),
2526 (unsigned long long)rdev
->sb_start
);
2527 rdev
->sb_events
= mddev
->events
;
2528 if (rdev
->badblocks
.size
) {
2529 md_super_write(mddev
, rdev
,
2530 rdev
->badblocks
.sector
,
2531 rdev
->badblocks
.size
<< 9,
2533 rdev
->badblocks
.size
= 0;
2536 } else if (test_bit(Faulty
, &rdev
->flags
))
2537 pr_debug("md: %s (skipping faulty)\n",
2538 bdevname(rdev
->bdev
, b
));
2540 pr_debug("(skipping incremental s/r ");
2542 if (mddev
->level
== LEVEL_MULTIPATH
)
2543 /* only need to write one superblock... */
2546 md_super_wait(mddev
);
2547 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
2549 spin_lock_irq(&mddev
->write_lock
);
2550 if (mddev
->in_sync
!= sync_req
||
2551 test_bit(MD_CHANGE_DEVS
, &mddev
->flags
)) {
2552 /* have to write it out again */
2553 spin_unlock_irq(&mddev
->write_lock
);
2556 clear_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
2557 spin_unlock_irq(&mddev
->write_lock
);
2558 wake_up(&mddev
->sb_wait
);
2559 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
2560 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
2562 rdev_for_each(rdev
, mddev
) {
2563 if (test_and_clear_bit(FaultRecorded
, &rdev
->flags
))
2564 clear_bit(Blocked
, &rdev
->flags
);
2566 if (any_badblocks_changed
)
2567 md_ack_all_badblocks(&rdev
->badblocks
);
2568 clear_bit(BlockedBadBlocks
, &rdev
->flags
);
2569 wake_up(&rdev
->blocked_wait
);
2573 /* words written to sysfs files may, or may not, be \n terminated.
2574 * We want to accept with case. For this we use cmd_match.
2576 static int cmd_match(const char *cmd
, const char *str
)
2578 /* See if cmd, written into a sysfs file, matches
2579 * str. They must either be the same, or cmd can
2580 * have a trailing newline
2582 while (*cmd
&& *str
&& *cmd
== *str
) {
2593 struct rdev_sysfs_entry
{
2594 struct attribute attr
;
2595 ssize_t (*show
)(struct md_rdev
*, char *);
2596 ssize_t (*store
)(struct md_rdev
*, const char *, size_t);
2600 state_show(struct md_rdev
*rdev
, char *page
)
2605 if (test_bit(Faulty
, &rdev
->flags
) ||
2606 rdev
->badblocks
.unacked_exist
) {
2607 len
+= sprintf(page
+len
, "%sfaulty",sep
);
2610 if (test_bit(In_sync
, &rdev
->flags
)) {
2611 len
+= sprintf(page
+len
, "%sin_sync",sep
);
2614 if (test_bit(WriteMostly
, &rdev
->flags
)) {
2615 len
+= sprintf(page
+len
, "%swrite_mostly",sep
);
2618 if (test_bit(Blocked
, &rdev
->flags
) ||
2619 (rdev
->badblocks
.unacked_exist
2620 && !test_bit(Faulty
, &rdev
->flags
))) {
2621 len
+= sprintf(page
+len
, "%sblocked", sep
);
2624 if (!test_bit(Faulty
, &rdev
->flags
) &&
2625 !test_bit(In_sync
, &rdev
->flags
)) {
2626 len
+= sprintf(page
+len
, "%sspare", sep
);
2629 if (test_bit(WriteErrorSeen
, &rdev
->flags
)) {
2630 len
+= sprintf(page
+len
, "%swrite_error", sep
);
2633 if (test_bit(WantReplacement
, &rdev
->flags
)) {
2634 len
+= sprintf(page
+len
, "%swant_replacement", sep
);
2637 if (test_bit(Replacement
, &rdev
->flags
)) {
2638 len
+= sprintf(page
+len
, "%sreplacement", sep
);
2642 return len
+sprintf(page
+len
, "\n");
2646 state_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
2649 * faulty - simulates an error
2650 * remove - disconnects the device
2651 * writemostly - sets write_mostly
2652 * -writemostly - clears write_mostly
2653 * blocked - sets the Blocked flags
2654 * -blocked - clears the Blocked and possibly simulates an error
2655 * insync - sets Insync providing device isn't active
2656 * write_error - sets WriteErrorSeen
2657 * -write_error - clears WriteErrorSeen
2660 if (cmd_match(buf
, "faulty") && rdev
->mddev
->pers
) {
2661 md_error(rdev
->mddev
, rdev
);
2662 if (test_bit(Faulty
, &rdev
->flags
))
2666 } else if (cmd_match(buf
, "remove")) {
2667 if (rdev
->raid_disk
>= 0)
2670 struct mddev
*mddev
= rdev
->mddev
;
2671 kick_rdev_from_array(rdev
);
2673 md_update_sb(mddev
, 1);
2674 md_new_event(mddev
);
2677 } else if (cmd_match(buf
, "writemostly")) {
2678 set_bit(WriteMostly
, &rdev
->flags
);
2680 } else if (cmd_match(buf
, "-writemostly")) {
2681 clear_bit(WriteMostly
, &rdev
->flags
);
2683 } else if (cmd_match(buf
, "blocked")) {
2684 set_bit(Blocked
, &rdev
->flags
);
2686 } else if (cmd_match(buf
, "-blocked")) {
2687 if (!test_bit(Faulty
, &rdev
->flags
) &&
2688 rdev
->badblocks
.unacked_exist
) {
2689 /* metadata handler doesn't understand badblocks,
2690 * so we need to fail the device
2692 md_error(rdev
->mddev
, rdev
);
2694 clear_bit(Blocked
, &rdev
->flags
);
2695 clear_bit(BlockedBadBlocks
, &rdev
->flags
);
2696 wake_up(&rdev
->blocked_wait
);
2697 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
2698 md_wakeup_thread(rdev
->mddev
->thread
);
2701 } else if (cmd_match(buf
, "insync") && rdev
->raid_disk
== -1) {
2702 set_bit(In_sync
, &rdev
->flags
);
2704 } else if (cmd_match(buf
, "write_error")) {
2705 set_bit(WriteErrorSeen
, &rdev
->flags
);
2707 } else if (cmd_match(buf
, "-write_error")) {
2708 clear_bit(WriteErrorSeen
, &rdev
->flags
);
2710 } else if (cmd_match(buf
, "want_replacement")) {
2711 /* Any non-spare device that is not a replacement can
2712 * become want_replacement at any time, but we then need to
2713 * check if recovery is needed.
2715 if (rdev
->raid_disk
>= 0 &&
2716 !test_bit(Replacement
, &rdev
->flags
))
2717 set_bit(WantReplacement
, &rdev
->flags
);
2718 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
2719 md_wakeup_thread(rdev
->mddev
->thread
);
2721 } else if (cmd_match(buf
, "-want_replacement")) {
2722 /* Clearing 'want_replacement' is always allowed.
2723 * Once replacements starts it is too late though.
2726 clear_bit(WantReplacement
, &rdev
->flags
);
2727 } else if (cmd_match(buf
, "replacement")) {
2728 /* Can only set a device as a replacement when array has not
2729 * yet been started. Once running, replacement is automatic
2730 * from spares, or by assigning 'slot'.
2732 if (rdev
->mddev
->pers
)
2735 set_bit(Replacement
, &rdev
->flags
);
2738 } else if (cmd_match(buf
, "-replacement")) {
2739 /* Similarly, can only clear Replacement before start */
2740 if (rdev
->mddev
->pers
)
2743 clear_bit(Replacement
, &rdev
->flags
);
2748 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
2749 return err
? err
: len
;
2751 static struct rdev_sysfs_entry rdev_state
=
2752 __ATTR(state
, S_IRUGO
|S_IWUSR
, state_show
, state_store
);
2755 errors_show(struct md_rdev
*rdev
, char *page
)
2757 return sprintf(page
, "%d\n", atomic_read(&rdev
->corrected_errors
));
2761 errors_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
2764 unsigned long n
= simple_strtoul(buf
, &e
, 10);
2765 if (*buf
&& (*e
== 0 || *e
== '\n')) {
2766 atomic_set(&rdev
->corrected_errors
, n
);
2771 static struct rdev_sysfs_entry rdev_errors
=
2772 __ATTR(errors
, S_IRUGO
|S_IWUSR
, errors_show
, errors_store
);
2775 slot_show(struct md_rdev
*rdev
, char *page
)
2777 if (rdev
->raid_disk
< 0)
2778 return sprintf(page
, "none\n");
2780 return sprintf(page
, "%d\n", rdev
->raid_disk
);
2784 slot_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
2788 int slot
= simple_strtoul(buf
, &e
, 10);
2789 if (strncmp(buf
, "none", 4)==0)
2791 else if (e
==buf
|| (*e
&& *e
!= '\n'))
2793 if (rdev
->mddev
->pers
&& slot
== -1) {
2794 /* Setting 'slot' on an active array requires also
2795 * updating the 'rd%d' link, and communicating
2796 * with the personality with ->hot_*_disk.
2797 * For now we only support removing
2798 * failed/spare devices. This normally happens automatically,
2799 * but not when the metadata is externally managed.
2801 if (rdev
->raid_disk
== -1)
2803 /* personality does all needed checks */
2804 if (rdev
->mddev
->pers
->hot_remove_disk
== NULL
)
2806 err
= rdev
->mddev
->pers
->
2807 hot_remove_disk(rdev
->mddev
, rdev
);
2810 sysfs_unlink_rdev(rdev
->mddev
, rdev
);
2811 rdev
->raid_disk
= -1;
2812 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
2813 md_wakeup_thread(rdev
->mddev
->thread
);
2814 } else if (rdev
->mddev
->pers
) {
2815 /* Activating a spare .. or possibly reactivating
2816 * if we ever get bitmaps working here.
2819 if (rdev
->raid_disk
!= -1)
2822 if (test_bit(MD_RECOVERY_RUNNING
, &rdev
->mddev
->recovery
))
2825 if (rdev
->mddev
->pers
->hot_add_disk
== NULL
)
2828 if (slot
>= rdev
->mddev
->raid_disks
&&
2829 slot
>= rdev
->mddev
->raid_disks
+ rdev
->mddev
->delta_disks
)
2832 rdev
->raid_disk
= slot
;
2833 if (test_bit(In_sync
, &rdev
->flags
))
2834 rdev
->saved_raid_disk
= slot
;
2836 rdev
->saved_raid_disk
= -1;
2837 clear_bit(In_sync
, &rdev
->flags
);
2838 err
= rdev
->mddev
->pers
->
2839 hot_add_disk(rdev
->mddev
, rdev
);
2841 rdev
->raid_disk
= -1;
2844 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
2845 if (sysfs_link_rdev(rdev
->mddev
, rdev
))
2846 /* failure here is OK */;
2847 /* don't wakeup anyone, leave that to userspace. */
2849 if (slot
>= rdev
->mddev
->raid_disks
&&
2850 slot
>= rdev
->mddev
->raid_disks
+ rdev
->mddev
->delta_disks
)
2852 rdev
->raid_disk
= slot
;
2853 /* assume it is working */
2854 clear_bit(Faulty
, &rdev
->flags
);
2855 clear_bit(WriteMostly
, &rdev
->flags
);
2856 set_bit(In_sync
, &rdev
->flags
);
2857 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
2863 static struct rdev_sysfs_entry rdev_slot
=
2864 __ATTR(slot
, S_IRUGO
|S_IWUSR
, slot_show
, slot_store
);
2867 offset_show(struct md_rdev
*rdev
, char *page
)
2869 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->data_offset
);
2873 offset_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
2875 unsigned long long offset
;
2876 if (strict_strtoull(buf
, 10, &offset
) < 0)
2878 if (rdev
->mddev
->pers
&& rdev
->raid_disk
>= 0)
2880 if (rdev
->sectors
&& rdev
->mddev
->external
)
2881 /* Must set offset before size, so overlap checks
2884 rdev
->data_offset
= offset
;
2885 rdev
->new_data_offset
= offset
;
2889 static struct rdev_sysfs_entry rdev_offset
=
2890 __ATTR(offset
, S_IRUGO
|S_IWUSR
, offset_show
, offset_store
);
2892 static ssize_t
new_offset_show(struct md_rdev
*rdev
, char *page
)
2894 return sprintf(page
, "%llu\n",
2895 (unsigned long long)rdev
->new_data_offset
);
2898 static ssize_t
new_offset_store(struct md_rdev
*rdev
,
2899 const char *buf
, size_t len
)
2901 unsigned long long new_offset
;
2902 struct mddev
*mddev
= rdev
->mddev
;
2904 if (strict_strtoull(buf
, 10, &new_offset
) < 0)
2907 if (mddev
->sync_thread
)
2909 if (new_offset
== rdev
->data_offset
)
2910 /* reset is always permitted */
2912 else if (new_offset
> rdev
->data_offset
) {
2913 /* must not push array size beyond rdev_sectors */
2914 if (new_offset
- rdev
->data_offset
2915 + mddev
->dev_sectors
> rdev
->sectors
)
2918 /* Metadata worries about other space details. */
2920 /* decreasing the offset is inconsistent with a backwards
2923 if (new_offset
< rdev
->data_offset
&&
2924 mddev
->reshape_backwards
)
2926 /* Increasing offset is inconsistent with forwards
2927 * reshape. reshape_direction should be set to
2928 * 'backwards' first.
2930 if (new_offset
> rdev
->data_offset
&&
2931 !mddev
->reshape_backwards
)
2934 if (mddev
->pers
&& mddev
->persistent
&&
2935 !super_types
[mddev
->major_version
]
2936 .allow_new_offset(rdev
, new_offset
))
2938 rdev
->new_data_offset
= new_offset
;
2939 if (new_offset
> rdev
->data_offset
)
2940 mddev
->reshape_backwards
= 1;
2941 else if (new_offset
< rdev
->data_offset
)
2942 mddev
->reshape_backwards
= 0;
2946 static struct rdev_sysfs_entry rdev_new_offset
=
2947 __ATTR(new_offset
, S_IRUGO
|S_IWUSR
, new_offset_show
, new_offset_store
);
2950 rdev_size_show(struct md_rdev
*rdev
, char *page
)
2952 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->sectors
/ 2);
2955 static int overlaps(sector_t s1
, sector_t l1
, sector_t s2
, sector_t l2
)
2957 /* check if two start/length pairs overlap */
2965 static int strict_blocks_to_sectors(const char *buf
, sector_t
*sectors
)
2967 unsigned long long blocks
;
2970 if (strict_strtoull(buf
, 10, &blocks
) < 0)
2973 if (blocks
& 1ULL << (8 * sizeof(blocks
) - 1))
2974 return -EINVAL
; /* sector conversion overflow */
2977 if (new != blocks
* 2)
2978 return -EINVAL
; /* unsigned long long to sector_t overflow */
2985 rdev_size_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
2987 struct mddev
*my_mddev
= rdev
->mddev
;
2988 sector_t oldsectors
= rdev
->sectors
;
2991 if (strict_blocks_to_sectors(buf
, §ors
) < 0)
2993 if (rdev
->data_offset
!= rdev
->new_data_offset
)
2994 return -EINVAL
; /* too confusing */
2995 if (my_mddev
->pers
&& rdev
->raid_disk
>= 0) {
2996 if (my_mddev
->persistent
) {
2997 sectors
= super_types
[my_mddev
->major_version
].
2998 rdev_size_change(rdev
, sectors
);
3001 } else if (!sectors
)
3002 sectors
= (i_size_read(rdev
->bdev
->bd_inode
) >> 9) -
3005 if (sectors
< my_mddev
->dev_sectors
)
3006 return -EINVAL
; /* component must fit device */
3008 rdev
->sectors
= sectors
;
3009 if (sectors
> oldsectors
&& my_mddev
->external
) {
3010 /* need to check that all other rdevs with the same ->bdev
3011 * do not overlap. We need to unlock the mddev to avoid
3012 * a deadlock. We have already changed rdev->sectors, and if
3013 * we have to change it back, we will have the lock again.
3015 struct mddev
*mddev
;
3017 struct list_head
*tmp
;
3019 mddev_unlock(my_mddev
);
3020 for_each_mddev(mddev
, tmp
) {
3021 struct md_rdev
*rdev2
;
3024 rdev_for_each(rdev2
, mddev
)
3025 if (rdev
->bdev
== rdev2
->bdev
&&
3027 overlaps(rdev
->data_offset
, rdev
->sectors
,
3033 mddev_unlock(mddev
);
3039 mddev_lock(my_mddev
);
3041 /* Someone else could have slipped in a size
3042 * change here, but doing so is just silly.
3043 * We put oldsectors back because we *know* it is
3044 * safe, and trust userspace not to race with
3047 rdev
->sectors
= oldsectors
;
3054 static struct rdev_sysfs_entry rdev_size
=
3055 __ATTR(size
, S_IRUGO
|S_IWUSR
, rdev_size_show
, rdev_size_store
);
3058 static ssize_t
recovery_start_show(struct md_rdev
*rdev
, char *page
)
3060 unsigned long long recovery_start
= rdev
->recovery_offset
;
3062 if (test_bit(In_sync
, &rdev
->flags
) ||
3063 recovery_start
== MaxSector
)
3064 return sprintf(page
, "none\n");
3066 return sprintf(page
, "%llu\n", recovery_start
);
3069 static ssize_t
recovery_start_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
3071 unsigned long long recovery_start
;
3073 if (cmd_match(buf
, "none"))
3074 recovery_start
= MaxSector
;
3075 else if (strict_strtoull(buf
, 10, &recovery_start
))
3078 if (rdev
->mddev
->pers
&&
3079 rdev
->raid_disk
>= 0)
3082 rdev
->recovery_offset
= recovery_start
;
3083 if (recovery_start
== MaxSector
)
3084 set_bit(In_sync
, &rdev
->flags
);
3086 clear_bit(In_sync
, &rdev
->flags
);
3090 static struct rdev_sysfs_entry rdev_recovery_start
=
3091 __ATTR(recovery_start
, S_IRUGO
|S_IWUSR
, recovery_start_show
, recovery_start_store
);
3095 badblocks_show(struct badblocks
*bb
, char *page
, int unack
);
3097 badblocks_store(struct badblocks
*bb
, const char *page
, size_t len
, int unack
);
3099 static ssize_t
bb_show(struct md_rdev
*rdev
, char *page
)
3101 return badblocks_show(&rdev
->badblocks
, page
, 0);
3103 static ssize_t
bb_store(struct md_rdev
*rdev
, const char *page
, size_t len
)
3105 int rv
= badblocks_store(&rdev
->badblocks
, page
, len
, 0);
3106 /* Maybe that ack was all we needed */
3107 if (test_and_clear_bit(BlockedBadBlocks
, &rdev
->flags
))
3108 wake_up(&rdev
->blocked_wait
);
3111 static struct rdev_sysfs_entry rdev_bad_blocks
=
3112 __ATTR(bad_blocks
, S_IRUGO
|S_IWUSR
, bb_show
, bb_store
);
3115 static ssize_t
ubb_show(struct md_rdev
*rdev
, char *page
)
3117 return badblocks_show(&rdev
->badblocks
, page
, 1);
3119 static ssize_t
ubb_store(struct md_rdev
*rdev
, const char *page
, size_t len
)
3121 return badblocks_store(&rdev
->badblocks
, page
, len
, 1);
3123 static struct rdev_sysfs_entry rdev_unack_bad_blocks
=
3124 __ATTR(unacknowledged_bad_blocks
, S_IRUGO
|S_IWUSR
, ubb_show
, ubb_store
);
3126 static struct attribute
*rdev_default_attrs
[] = {
3131 &rdev_new_offset
.attr
,
3133 &rdev_recovery_start
.attr
,
3134 &rdev_bad_blocks
.attr
,
3135 &rdev_unack_bad_blocks
.attr
,
3139 rdev_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
3141 struct rdev_sysfs_entry
*entry
= container_of(attr
, struct rdev_sysfs_entry
, attr
);
3142 struct md_rdev
*rdev
= container_of(kobj
, struct md_rdev
, kobj
);
3143 struct mddev
*mddev
= rdev
->mddev
;
3149 rv
= mddev
? mddev_lock(mddev
) : -EBUSY
;
3151 if (rdev
->mddev
== NULL
)
3154 rv
= entry
->show(rdev
, page
);
3155 mddev_unlock(mddev
);
3161 rdev_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
3162 const char *page
, size_t length
)
3164 struct rdev_sysfs_entry
*entry
= container_of(attr
, struct rdev_sysfs_entry
, attr
);
3165 struct md_rdev
*rdev
= container_of(kobj
, struct md_rdev
, kobj
);
3167 struct mddev
*mddev
= rdev
->mddev
;
3171 if (!capable(CAP_SYS_ADMIN
))
3173 rv
= mddev
? mddev_lock(mddev
): -EBUSY
;
3175 if (rdev
->mddev
== NULL
)
3178 rv
= entry
->store(rdev
, page
, length
);
3179 mddev_unlock(mddev
);
3184 static void rdev_free(struct kobject
*ko
)
3186 struct md_rdev
*rdev
= container_of(ko
, struct md_rdev
, kobj
);
3189 static const struct sysfs_ops rdev_sysfs_ops
= {
3190 .show
= rdev_attr_show
,
3191 .store
= rdev_attr_store
,
3193 static struct kobj_type rdev_ktype
= {
3194 .release
= rdev_free
,
3195 .sysfs_ops
= &rdev_sysfs_ops
,
3196 .default_attrs
= rdev_default_attrs
,
3199 int md_rdev_init(struct md_rdev
*rdev
)
3202 rdev
->saved_raid_disk
= -1;
3203 rdev
->raid_disk
= -1;
3205 rdev
->data_offset
= 0;
3206 rdev
->new_data_offset
= 0;
3207 rdev
->sb_events
= 0;
3208 rdev
->last_read_error
.tv_sec
= 0;
3209 rdev
->last_read_error
.tv_nsec
= 0;
3210 rdev
->sb_loaded
= 0;
3211 rdev
->bb_page
= NULL
;
3212 atomic_set(&rdev
->nr_pending
, 0);
3213 atomic_set(&rdev
->read_errors
, 0);
3214 atomic_set(&rdev
->corrected_errors
, 0);
3216 INIT_LIST_HEAD(&rdev
->same_set
);
3217 init_waitqueue_head(&rdev
->blocked_wait
);
3219 /* Add space to store bad block list.
3220 * This reserves the space even on arrays where it cannot
3221 * be used - I wonder if that matters
3223 rdev
->badblocks
.count
= 0;
3224 rdev
->badblocks
.shift
= 0;
3225 rdev
->badblocks
.page
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
3226 seqlock_init(&rdev
->badblocks
.lock
);
3227 if (rdev
->badblocks
.page
== NULL
)
3232 EXPORT_SYMBOL_GPL(md_rdev_init
);
3234 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3236 * mark the device faulty if:
3238 * - the device is nonexistent (zero size)
3239 * - the device has no valid superblock
3241 * a faulty rdev _never_ has rdev->sb set.
3243 static struct md_rdev
*md_import_device(dev_t newdev
, int super_format
, int super_minor
)
3245 char b
[BDEVNAME_SIZE
];
3247 struct md_rdev
*rdev
;
3250 rdev
= kzalloc(sizeof(*rdev
), GFP_KERNEL
);
3252 printk(KERN_ERR
"md: could not alloc mem for new device!\n");
3253 return ERR_PTR(-ENOMEM
);
3256 err
= md_rdev_init(rdev
);
3259 err
= alloc_disk_sb(rdev
);
3263 err
= lock_rdev(rdev
, newdev
, super_format
== -2);
3267 kobject_init(&rdev
->kobj
, &rdev_ktype
);
3269 size
= i_size_read(rdev
->bdev
->bd_inode
) >> BLOCK_SIZE_BITS
;
3272 "md: %s has zero or unknown size, marking faulty!\n",
3273 bdevname(rdev
->bdev
,b
));
3278 if (super_format
>= 0) {
3279 err
= super_types
[super_format
].
3280 load_super(rdev
, NULL
, super_minor
);
3281 if (err
== -EINVAL
) {
3283 "md: %s does not have a valid v%d.%d "
3284 "superblock, not importing!\n",
3285 bdevname(rdev
->bdev
,b
),
3286 super_format
, super_minor
);
3291 "md: could not read %s's sb, not importing!\n",
3292 bdevname(rdev
->bdev
,b
));
3296 if (super_format
== -1)
3297 /* hot-add for 0.90, or non-persistent: so no badblocks */
3298 rdev
->badblocks
.shift
= -1;
3305 md_rdev_clear(rdev
);
3307 return ERR_PTR(err
);
3311 * Check a full RAID array for plausibility
3315 static void analyze_sbs(struct mddev
* mddev
)
3318 struct md_rdev
*rdev
, *freshest
, *tmp
;
3319 char b
[BDEVNAME_SIZE
];
3322 rdev_for_each_safe(rdev
, tmp
, mddev
)
3323 switch (super_types
[mddev
->major_version
].
3324 load_super(rdev
, freshest
, mddev
->minor_version
)) {
3332 "md: fatal superblock inconsistency in %s"
3333 " -- removing from array\n",
3334 bdevname(rdev
->bdev
,b
));
3335 kick_rdev_from_array(rdev
);
3339 super_types
[mddev
->major_version
].
3340 validate_super(mddev
, freshest
);
3343 rdev_for_each_safe(rdev
, tmp
, mddev
) {
3344 if (mddev
->max_disks
&&
3345 (rdev
->desc_nr
>= mddev
->max_disks
||
3346 i
> mddev
->max_disks
)) {
3348 "md: %s: %s: only %d devices permitted\n",
3349 mdname(mddev
), bdevname(rdev
->bdev
, b
),
3351 kick_rdev_from_array(rdev
);
3354 if (rdev
!= freshest
)
3355 if (super_types
[mddev
->major_version
].
3356 validate_super(mddev
, rdev
)) {
3357 printk(KERN_WARNING
"md: kicking non-fresh %s"
3359 bdevname(rdev
->bdev
,b
));
3360 kick_rdev_from_array(rdev
);
3363 if (mddev
->level
== LEVEL_MULTIPATH
) {
3364 rdev
->desc_nr
= i
++;
3365 rdev
->raid_disk
= rdev
->desc_nr
;
3366 set_bit(In_sync
, &rdev
->flags
);
3367 } else if (rdev
->raid_disk
>= (mddev
->raid_disks
- min(0, mddev
->delta_disks
))) {
3368 rdev
->raid_disk
= -1;
3369 clear_bit(In_sync
, &rdev
->flags
);
3374 /* Read a fixed-point number.
3375 * Numbers in sysfs attributes should be in "standard" units where
3376 * possible, so time should be in seconds.
3377 * However we internally use a a much smaller unit such as
3378 * milliseconds or jiffies.
3379 * This function takes a decimal number with a possible fractional
3380 * component, and produces an integer which is the result of
3381 * multiplying that number by 10^'scale'.
3382 * all without any floating-point arithmetic.
3384 int strict_strtoul_scaled(const char *cp
, unsigned long *res
, int scale
)
3386 unsigned long result
= 0;
3388 while (isdigit(*cp
) || (*cp
== '.' && decimals
< 0)) {
3391 else if (decimals
< scale
) {
3394 result
= result
* 10 + value
;
3406 while (decimals
< scale
) {
3415 static void md_safemode_timeout(unsigned long data
);
3418 safe_delay_show(struct mddev
*mddev
, char *page
)
3420 int msec
= (mddev
->safemode_delay
*1000)/HZ
;
3421 return sprintf(page
, "%d.%03d\n", msec
/1000, msec
%1000);
3424 safe_delay_store(struct mddev
*mddev
, const char *cbuf
, size_t len
)
3428 if (strict_strtoul_scaled(cbuf
, &msec
, 3) < 0)
3431 mddev
->safemode_delay
= 0;
3433 unsigned long old_delay
= mddev
->safemode_delay
;
3434 mddev
->safemode_delay
= (msec
*HZ
)/1000;
3435 if (mddev
->safemode_delay
== 0)
3436 mddev
->safemode_delay
= 1;
3437 if (mddev
->safemode_delay
< old_delay
)
3438 md_safemode_timeout((unsigned long)mddev
);
3442 static struct md_sysfs_entry md_safe_delay
=
3443 __ATTR(safe_mode_delay
, S_IRUGO
|S_IWUSR
,safe_delay_show
, safe_delay_store
);
3446 level_show(struct mddev
*mddev
, char *page
)
3448 struct md_personality
*p
= mddev
->pers
;
3450 return sprintf(page
, "%s\n", p
->name
);
3451 else if (mddev
->clevel
[0])
3452 return sprintf(page
, "%s\n", mddev
->clevel
);
3453 else if (mddev
->level
!= LEVEL_NONE
)
3454 return sprintf(page
, "%d\n", mddev
->level
);
3460 level_store(struct mddev
*mddev
, const char *buf
, size_t len
)
3464 struct md_personality
*pers
;
3467 struct md_rdev
*rdev
;
3469 if (mddev
->pers
== NULL
) {
3472 if (len
>= sizeof(mddev
->clevel
))
3474 strncpy(mddev
->clevel
, buf
, len
);
3475 if (mddev
->clevel
[len
-1] == '\n')
3477 mddev
->clevel
[len
] = 0;
3478 mddev
->level
= LEVEL_NONE
;
3482 /* request to change the personality. Need to ensure:
3483 * - array is not engaged in resync/recovery/reshape
3484 * - old personality can be suspended
3485 * - new personality will access other array.
3488 if (mddev
->sync_thread
||
3489 mddev
->reshape_position
!= MaxSector
||
3490 mddev
->sysfs_active
)
3493 if (!mddev
->pers
->quiesce
) {
3494 printk(KERN_WARNING
"md: %s: %s does not support online personality change\n",
3495 mdname(mddev
), mddev
->pers
->name
);
3499 /* Now find the new personality */
3500 if (len
== 0 || len
>= sizeof(clevel
))
3502 strncpy(clevel
, buf
, len
);
3503 if (clevel
[len
-1] == '\n')
3506 if (strict_strtol(clevel
, 10, &level
))
3509 if (request_module("md-%s", clevel
) != 0)
3510 request_module("md-level-%s", clevel
);
3511 spin_lock(&pers_lock
);
3512 pers
= find_pers(level
, clevel
);
3513 if (!pers
|| !try_module_get(pers
->owner
)) {
3514 spin_unlock(&pers_lock
);
3515 printk(KERN_WARNING
"md: personality %s not loaded\n", clevel
);
3518 spin_unlock(&pers_lock
);
3520 if (pers
== mddev
->pers
) {
3521 /* Nothing to do! */
3522 module_put(pers
->owner
);
3525 if (!pers
->takeover
) {
3526 module_put(pers
->owner
);
3527 printk(KERN_WARNING
"md: %s: %s does not support personality takeover\n",
3528 mdname(mddev
), clevel
);
3532 rdev_for_each(rdev
, mddev
)
3533 rdev
->new_raid_disk
= rdev
->raid_disk
;
3535 /* ->takeover must set new_* and/or delta_disks
3536 * if it succeeds, and may set them when it fails.
3538 priv
= pers
->takeover(mddev
);
3540 mddev
->new_level
= mddev
->level
;
3541 mddev
->new_layout
= mddev
->layout
;
3542 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
3543 mddev
->raid_disks
-= mddev
->delta_disks
;
3544 mddev
->delta_disks
= 0;
3545 mddev
->reshape_backwards
= 0;
3546 module_put(pers
->owner
);
3547 printk(KERN_WARNING
"md: %s: %s would not accept array\n",
3548 mdname(mddev
), clevel
);
3549 return PTR_ERR(priv
);
3552 /* Looks like we have a winner */
3553 mddev_suspend(mddev
);
3554 mddev
->pers
->stop(mddev
);
3556 if (mddev
->pers
->sync_request
== NULL
&&
3557 pers
->sync_request
!= NULL
) {
3558 /* need to add the md_redundancy_group */
3559 if (sysfs_create_group(&mddev
->kobj
, &md_redundancy_group
))
3561 "md: cannot register extra attributes for %s\n",
3563 mddev
->sysfs_action
= sysfs_get_dirent(mddev
->kobj
.sd
, NULL
, "sync_action");
3565 if (mddev
->pers
->sync_request
!= NULL
&&
3566 pers
->sync_request
== NULL
) {
3567 /* need to remove the md_redundancy_group */
3568 if (mddev
->to_remove
== NULL
)
3569 mddev
->to_remove
= &md_redundancy_group
;
3572 if (mddev
->pers
->sync_request
== NULL
&&
3574 /* We are converting from a no-redundancy array
3575 * to a redundancy array and metadata is managed
3576 * externally so we need to be sure that writes
3577 * won't block due to a need to transition
3579 * until external management is started.
3582 mddev
->safemode_delay
= 0;
3583 mddev
->safemode
= 0;
3586 rdev_for_each(rdev
, mddev
) {
3587 if (rdev
->raid_disk
< 0)
3589 if (rdev
->new_raid_disk
>= mddev
->raid_disks
)
3590 rdev
->new_raid_disk
= -1;
3591 if (rdev
->new_raid_disk
== rdev
->raid_disk
)
3593 sysfs_unlink_rdev(mddev
, rdev
);
3595 rdev_for_each(rdev
, mddev
) {
3596 if (rdev
->raid_disk
< 0)
3598 if (rdev
->new_raid_disk
== rdev
->raid_disk
)
3600 rdev
->raid_disk
= rdev
->new_raid_disk
;
3601 if (rdev
->raid_disk
< 0)
3602 clear_bit(In_sync
, &rdev
->flags
);
3604 if (sysfs_link_rdev(mddev
, rdev
))
3605 printk(KERN_WARNING
"md: cannot register rd%d"
3606 " for %s after level change\n",
3607 rdev
->raid_disk
, mdname(mddev
));
3611 module_put(mddev
->pers
->owner
);
3613 mddev
->private = priv
;
3614 strlcpy(mddev
->clevel
, pers
->name
, sizeof(mddev
->clevel
));
3615 mddev
->level
= mddev
->new_level
;
3616 mddev
->layout
= mddev
->new_layout
;
3617 mddev
->chunk_sectors
= mddev
->new_chunk_sectors
;
3618 mddev
->delta_disks
= 0;
3619 mddev
->reshape_backwards
= 0;
3620 mddev
->degraded
= 0;
3621 if (mddev
->pers
->sync_request
== NULL
) {
3622 /* this is now an array without redundancy, so
3623 * it must always be in_sync
3626 del_timer_sync(&mddev
->safemode_timer
);
3629 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
3630 mddev_resume(mddev
);
3631 sysfs_notify(&mddev
->kobj
, NULL
, "level");
3632 md_new_event(mddev
);
3636 static struct md_sysfs_entry md_level
=
3637 __ATTR(level
, S_IRUGO
|S_IWUSR
, level_show
, level_store
);
3641 layout_show(struct mddev
*mddev
, char *page
)
3643 /* just a number, not meaningful for all levels */
3644 if (mddev
->reshape_position
!= MaxSector
&&
3645 mddev
->layout
!= mddev
->new_layout
)
3646 return sprintf(page
, "%d (%d)\n",
3647 mddev
->new_layout
, mddev
->layout
);
3648 return sprintf(page
, "%d\n", mddev
->layout
);
3652 layout_store(struct mddev
*mddev
, const char *buf
, size_t len
)
3655 unsigned long n
= simple_strtoul(buf
, &e
, 10);
3657 if (!*buf
|| (*e
&& *e
!= '\n'))
3662 if (mddev
->pers
->check_reshape
== NULL
)
3664 mddev
->new_layout
= n
;
3665 err
= mddev
->pers
->check_reshape(mddev
);
3667 mddev
->new_layout
= mddev
->layout
;
3671 mddev
->new_layout
= n
;
3672 if (mddev
->reshape_position
== MaxSector
)
3677 static struct md_sysfs_entry md_layout
=
3678 __ATTR(layout
, S_IRUGO
|S_IWUSR
, layout_show
, layout_store
);
3682 raid_disks_show(struct mddev
*mddev
, char *page
)
3684 if (mddev
->raid_disks
== 0)
3686 if (mddev
->reshape_position
!= MaxSector
&&
3687 mddev
->delta_disks
!= 0)
3688 return sprintf(page
, "%d (%d)\n", mddev
->raid_disks
,
3689 mddev
->raid_disks
- mddev
->delta_disks
);
3690 return sprintf(page
, "%d\n", mddev
->raid_disks
);
3693 static int update_raid_disks(struct mddev
*mddev
, int raid_disks
);
3696 raid_disks_store(struct mddev
*mddev
, const char *buf
, size_t len
)
3700 unsigned long n
= simple_strtoul(buf
, &e
, 10);
3702 if (!*buf
|| (*e
&& *e
!= '\n'))
3706 rv
= update_raid_disks(mddev
, n
);
3707 else if (mddev
->reshape_position
!= MaxSector
) {
3708 struct md_rdev
*rdev
;
3709 int olddisks
= mddev
->raid_disks
- mddev
->delta_disks
;
3711 rdev_for_each(rdev
, mddev
) {
3713 rdev
->data_offset
< rdev
->new_data_offset
)
3716 rdev
->data_offset
> rdev
->new_data_offset
)
3719 mddev
->delta_disks
= n
- olddisks
;
3720 mddev
->raid_disks
= n
;
3721 mddev
->reshape_backwards
= (mddev
->delta_disks
< 0);
3723 mddev
->raid_disks
= n
;
3724 return rv
? rv
: len
;
3726 static struct md_sysfs_entry md_raid_disks
=
3727 __ATTR(raid_disks
, S_IRUGO
|S_IWUSR
, raid_disks_show
, raid_disks_store
);
3730 chunk_size_show(struct mddev
*mddev
, char *page
)
3732 if (mddev
->reshape_position
!= MaxSector
&&
3733 mddev
->chunk_sectors
!= mddev
->new_chunk_sectors
)
3734 return sprintf(page
, "%d (%d)\n",
3735 mddev
->new_chunk_sectors
<< 9,
3736 mddev
->chunk_sectors
<< 9);
3737 return sprintf(page
, "%d\n", mddev
->chunk_sectors
<< 9);
3741 chunk_size_store(struct mddev
*mddev
, const char *buf
, size_t len
)
3744 unsigned long n
= simple_strtoul(buf
, &e
, 10);
3746 if (!*buf
|| (*e
&& *e
!= '\n'))
3751 if (mddev
->pers
->check_reshape
== NULL
)
3753 mddev
->new_chunk_sectors
= n
>> 9;
3754 err
= mddev
->pers
->check_reshape(mddev
);
3756 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
3760 mddev
->new_chunk_sectors
= n
>> 9;
3761 if (mddev
->reshape_position
== MaxSector
)
3762 mddev
->chunk_sectors
= n
>> 9;
3766 static struct md_sysfs_entry md_chunk_size
=
3767 __ATTR(chunk_size
, S_IRUGO
|S_IWUSR
, chunk_size_show
, chunk_size_store
);
3770 resync_start_show(struct mddev
*mddev
, char *page
)
3772 if (mddev
->recovery_cp
== MaxSector
)
3773 return sprintf(page
, "none\n");
3774 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->recovery_cp
);
3778 resync_start_store(struct mddev
*mddev
, const char *buf
, size_t len
)
3781 unsigned long long n
= simple_strtoull(buf
, &e
, 10);
3783 if (mddev
->pers
&& !test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
))
3785 if (cmd_match(buf
, "none"))
3787 else if (!*buf
|| (*e
&& *e
!= '\n'))
3790 mddev
->recovery_cp
= n
;
3793 static struct md_sysfs_entry md_resync_start
=
3794 __ATTR(resync_start
, S_IRUGO
|S_IWUSR
, resync_start_show
, resync_start_store
);
3797 * The array state can be:
3800 * No devices, no size, no level
3801 * Equivalent to STOP_ARRAY ioctl
3803 * May have some settings, but array is not active
3804 * all IO results in error
3805 * When written, doesn't tear down array, but just stops it
3806 * suspended (not supported yet)
3807 * All IO requests will block. The array can be reconfigured.
3808 * Writing this, if accepted, will block until array is quiescent
3810 * no resync can happen. no superblocks get written.
3811 * write requests fail
3813 * like readonly, but behaves like 'clean' on a write request.
3815 * clean - no pending writes, but otherwise active.
3816 * When written to inactive array, starts without resync
3817 * If a write request arrives then
3818 * if metadata is known, mark 'dirty' and switch to 'active'.
3819 * if not known, block and switch to write-pending
3820 * If written to an active array that has pending writes, then fails.
3822 * fully active: IO and resync can be happening.
3823 * When written to inactive array, starts with resync
3826 * clean, but writes are blocked waiting for 'active' to be written.
3829 * like active, but no writes have been seen for a while (100msec).
3832 enum array_state
{ clear
, inactive
, suspended
, readonly
, read_auto
, clean
, active
,
3833 write_pending
, active_idle
, bad_word
};
3834 static char *array_states
[] = {
3835 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3836 "write-pending", "active-idle", NULL
};
3838 static int match_word(const char *word
, char **list
)
3841 for (n
=0; list
[n
]; n
++)
3842 if (cmd_match(word
, list
[n
]))
3848 array_state_show(struct mddev
*mddev
, char *page
)
3850 enum array_state st
= inactive
;
3863 else if (test_bit(MD_CHANGE_PENDING
, &mddev
->flags
))
3865 else if (mddev
->safemode
)
3871 if (list_empty(&mddev
->disks
) &&
3872 mddev
->raid_disks
== 0 &&
3873 mddev
->dev_sectors
== 0)
3878 return sprintf(page
, "%s\n", array_states
[st
]);
3881 static int do_md_stop(struct mddev
* mddev
, int ro
, struct block_device
*bdev
);
3882 static int md_set_readonly(struct mddev
* mddev
, struct block_device
*bdev
);
3883 static int do_md_run(struct mddev
* mddev
);
3884 static int restart_array(struct mddev
*mddev
);
3887 array_state_store(struct mddev
*mddev
, const char *buf
, size_t len
)
3890 enum array_state st
= match_word(buf
, array_states
);
3895 /* stopping an active array */
3896 if (atomic_read(&mddev
->openers
) > 0)
3898 err
= do_md_stop(mddev
, 0, NULL
);
3901 /* stopping an active array */
3903 if (atomic_read(&mddev
->openers
) > 0)
3905 err
= do_md_stop(mddev
, 2, NULL
);
3907 err
= 0; /* already inactive */
3910 break; /* not supported yet */
3913 err
= md_set_readonly(mddev
, NULL
);
3916 set_disk_ro(mddev
->gendisk
, 1);
3917 err
= do_md_run(mddev
);
3923 err
= md_set_readonly(mddev
, NULL
);
3924 else if (mddev
->ro
== 1)
3925 err
= restart_array(mddev
);
3928 set_disk_ro(mddev
->gendisk
, 0);
3932 err
= do_md_run(mddev
);
3937 restart_array(mddev
);
3938 spin_lock_irq(&mddev
->write_lock
);
3939 if (atomic_read(&mddev
->writes_pending
) == 0) {
3940 if (mddev
->in_sync
== 0) {
3942 if (mddev
->safemode
== 1)
3943 mddev
->safemode
= 0;
3944 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
3949 spin_unlock_irq(&mddev
->write_lock
);
3955 restart_array(mddev
);
3956 clear_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
3957 wake_up(&mddev
->sb_wait
);
3961 set_disk_ro(mddev
->gendisk
, 0);
3962 err
= do_md_run(mddev
);
3967 /* these cannot be set */
3973 if (mddev
->hold_active
== UNTIL_IOCTL
)
3974 mddev
->hold_active
= 0;
3975 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
3979 static struct md_sysfs_entry md_array_state
=
3980 __ATTR(array_state
, S_IRUGO
|S_IWUSR
, array_state_show
, array_state_store
);
3983 max_corrected_read_errors_show(struct mddev
*mddev
, char *page
) {
3984 return sprintf(page
, "%d\n",
3985 atomic_read(&mddev
->max_corr_read_errors
));
3989 max_corrected_read_errors_store(struct mddev
*mddev
, const char *buf
, size_t len
)
3992 unsigned long n
= simple_strtoul(buf
, &e
, 10);
3994 if (*buf
&& (*e
== 0 || *e
== '\n')) {
3995 atomic_set(&mddev
->max_corr_read_errors
, n
);
4001 static struct md_sysfs_entry max_corr_read_errors
=
4002 __ATTR(max_read_errors
, S_IRUGO
|S_IWUSR
, max_corrected_read_errors_show
,
4003 max_corrected_read_errors_store
);
4006 null_show(struct mddev
*mddev
, char *page
)
4012 new_dev_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4014 /* buf must be %d:%d\n? giving major and minor numbers */
4015 /* The new device is added to the array.
4016 * If the array has a persistent superblock, we read the
4017 * superblock to initialise info and check validity.
4018 * Otherwise, only checking done is that in bind_rdev_to_array,
4019 * which mainly checks size.
4022 int major
= simple_strtoul(buf
, &e
, 10);
4025 struct md_rdev
*rdev
;
4028 if (!*buf
|| *e
!= ':' || !e
[1] || e
[1] == '\n')
4030 minor
= simple_strtoul(e
+1, &e
, 10);
4031 if (*e
&& *e
!= '\n')
4033 dev
= MKDEV(major
, minor
);
4034 if (major
!= MAJOR(dev
) ||
4035 minor
!= MINOR(dev
))
4039 if (mddev
->persistent
) {
4040 rdev
= md_import_device(dev
, mddev
->major_version
,
4041 mddev
->minor_version
);
4042 if (!IS_ERR(rdev
) && !list_empty(&mddev
->disks
)) {
4043 struct md_rdev
*rdev0
4044 = list_entry(mddev
->disks
.next
,
4045 struct md_rdev
, same_set
);
4046 err
= super_types
[mddev
->major_version
]
4047 .load_super(rdev
, rdev0
, mddev
->minor_version
);
4051 } else if (mddev
->external
)
4052 rdev
= md_import_device(dev
, -2, -1);
4054 rdev
= md_import_device(dev
, -1, -1);
4057 return PTR_ERR(rdev
);
4058 err
= bind_rdev_to_array(rdev
, mddev
);
4062 return err
? err
: len
;
4065 static struct md_sysfs_entry md_new_device
=
4066 __ATTR(new_dev
, S_IWUSR
, null_show
, new_dev_store
);
4069 bitmap_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4072 unsigned long chunk
, end_chunk
;
4076 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
4078 chunk
= end_chunk
= simple_strtoul(buf
, &end
, 0);
4079 if (buf
== end
) break;
4080 if (*end
== '-') { /* range */
4082 end_chunk
= simple_strtoul(buf
, &end
, 0);
4083 if (buf
== end
) break;
4085 if (*end
&& !isspace(*end
)) break;
4086 bitmap_dirty_bits(mddev
->bitmap
, chunk
, end_chunk
);
4087 buf
= skip_spaces(end
);
4089 bitmap_unplug(mddev
->bitmap
); /* flush the bits to disk */
4094 static struct md_sysfs_entry md_bitmap
=
4095 __ATTR(bitmap_set_bits
, S_IWUSR
, null_show
, bitmap_store
);
4098 size_show(struct mddev
*mddev
, char *page
)
4100 return sprintf(page
, "%llu\n",
4101 (unsigned long long)mddev
->dev_sectors
/ 2);
4104 static int update_size(struct mddev
*mddev
, sector_t num_sectors
);
4107 size_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4109 /* If array is inactive, we can reduce the component size, but
4110 * not increase it (except from 0).
4111 * If array is active, we can try an on-line resize
4114 int err
= strict_blocks_to_sectors(buf
, §ors
);
4119 err
= update_size(mddev
, sectors
);
4120 md_update_sb(mddev
, 1);
4122 if (mddev
->dev_sectors
== 0 ||
4123 mddev
->dev_sectors
> sectors
)
4124 mddev
->dev_sectors
= sectors
;
4128 return err
? err
: len
;
4131 static struct md_sysfs_entry md_size
=
4132 __ATTR(component_size
, S_IRUGO
|S_IWUSR
, size_show
, size_store
);
4137 * 'none' for arrays with no metadata (good luck...)
4138 * 'external' for arrays with externally managed metadata,
4139 * or N.M for internally known formats
4142 metadata_show(struct mddev
*mddev
, char *page
)
4144 if (mddev
->persistent
)
4145 return sprintf(page
, "%d.%d\n",
4146 mddev
->major_version
, mddev
->minor_version
);
4147 else if (mddev
->external
)
4148 return sprintf(page
, "external:%s\n", mddev
->metadata_type
);
4150 return sprintf(page
, "none\n");
4154 metadata_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4158 /* Changing the details of 'external' metadata is
4159 * always permitted. Otherwise there must be
4160 * no devices attached to the array.
4162 if (mddev
->external
&& strncmp(buf
, "external:", 9) == 0)
4164 else if (!list_empty(&mddev
->disks
))
4167 if (cmd_match(buf
, "none")) {
4168 mddev
->persistent
= 0;
4169 mddev
->external
= 0;
4170 mddev
->major_version
= 0;
4171 mddev
->minor_version
= 90;
4174 if (strncmp(buf
, "external:", 9) == 0) {
4175 size_t namelen
= len
-9;
4176 if (namelen
>= sizeof(mddev
->metadata_type
))
4177 namelen
= sizeof(mddev
->metadata_type
)-1;
4178 strncpy(mddev
->metadata_type
, buf
+9, namelen
);
4179 mddev
->metadata_type
[namelen
] = 0;
4180 if (namelen
&& mddev
->metadata_type
[namelen
-1] == '\n')
4181 mddev
->metadata_type
[--namelen
] = 0;
4182 mddev
->persistent
= 0;
4183 mddev
->external
= 1;
4184 mddev
->major_version
= 0;
4185 mddev
->minor_version
= 90;
4188 major
= simple_strtoul(buf
, &e
, 10);
4189 if (e
==buf
|| *e
!= '.')
4192 minor
= simple_strtoul(buf
, &e
, 10);
4193 if (e
==buf
|| (*e
&& *e
!= '\n') )
4195 if (major
>= ARRAY_SIZE(super_types
) || super_types
[major
].name
== NULL
)
4197 mddev
->major_version
= major
;
4198 mddev
->minor_version
= minor
;
4199 mddev
->persistent
= 1;
4200 mddev
->external
= 0;
4204 static struct md_sysfs_entry md_metadata
=
4205 __ATTR(metadata_version
, S_IRUGO
|S_IWUSR
, metadata_show
, metadata_store
);
4208 action_show(struct mddev
*mddev
, char *page
)
4210 char *type
= "idle";
4211 if (test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
))
4213 else if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
4214 (!mddev
->ro
&& test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))) {
4215 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
4217 else if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
4218 if (!test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
4220 else if (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
))
4224 } else if (test_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
))
4227 return sprintf(page
, "%s\n", type
);
4230 static void reap_sync_thread(struct mddev
*mddev
);
4233 action_store(struct mddev
*mddev
, const char *page
, size_t len
)
4235 if (!mddev
->pers
|| !mddev
->pers
->sync_request
)
4238 if (cmd_match(page
, "frozen"))
4239 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4241 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4243 if (cmd_match(page
, "idle") || cmd_match(page
, "frozen")) {
4244 if (mddev
->sync_thread
) {
4245 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
4246 reap_sync_thread(mddev
);
4248 } else if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
4249 test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))
4251 else if (cmd_match(page
, "resync"))
4252 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4253 else if (cmd_match(page
, "recover")) {
4254 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
4255 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4256 } else if (cmd_match(page
, "reshape")) {
4258 if (mddev
->pers
->start_reshape
== NULL
)
4260 err
= mddev
->pers
->start_reshape(mddev
);
4263 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
4265 if (cmd_match(page
, "check"))
4266 set_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
4267 else if (!cmd_match(page
, "repair"))
4269 set_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
4270 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
4272 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4273 md_wakeup_thread(mddev
->thread
);
4274 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
4279 mismatch_cnt_show(struct mddev
*mddev
, char *page
)
4281 return sprintf(page
, "%llu\n",
4282 (unsigned long long) mddev
->resync_mismatches
);
4285 static struct md_sysfs_entry md_scan_mode
=
4286 __ATTR(sync_action
, S_IRUGO
|S_IWUSR
, action_show
, action_store
);
4289 static struct md_sysfs_entry md_mismatches
= __ATTR_RO(mismatch_cnt
);
4292 sync_min_show(struct mddev
*mddev
, char *page
)
4294 return sprintf(page
, "%d (%s)\n", speed_min(mddev
),
4295 mddev
->sync_speed_min
? "local": "system");
4299 sync_min_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4303 if (strncmp(buf
, "system", 6)==0) {
4304 mddev
->sync_speed_min
= 0;
4307 min
= simple_strtoul(buf
, &e
, 10);
4308 if (buf
== e
|| (*e
&& *e
!= '\n') || min
<= 0)
4310 mddev
->sync_speed_min
= min
;
4314 static struct md_sysfs_entry md_sync_min
=
4315 __ATTR(sync_speed_min
, S_IRUGO
|S_IWUSR
, sync_min_show
, sync_min_store
);
4318 sync_max_show(struct mddev
*mddev
, char *page
)
4320 return sprintf(page
, "%d (%s)\n", speed_max(mddev
),
4321 mddev
->sync_speed_max
? "local": "system");
4325 sync_max_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4329 if (strncmp(buf
, "system", 6)==0) {
4330 mddev
->sync_speed_max
= 0;
4333 max
= simple_strtoul(buf
, &e
, 10);
4334 if (buf
== e
|| (*e
&& *e
!= '\n') || max
<= 0)
4336 mddev
->sync_speed_max
= max
;
4340 static struct md_sysfs_entry md_sync_max
=
4341 __ATTR(sync_speed_max
, S_IRUGO
|S_IWUSR
, sync_max_show
, sync_max_store
);
4344 degraded_show(struct mddev
*mddev
, char *page
)
4346 return sprintf(page
, "%d\n", mddev
->degraded
);
4348 static struct md_sysfs_entry md_degraded
= __ATTR_RO(degraded
);
4351 sync_force_parallel_show(struct mddev
*mddev
, char *page
)
4353 return sprintf(page
, "%d\n", mddev
->parallel_resync
);
4357 sync_force_parallel_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4361 if (strict_strtol(buf
, 10, &n
))
4364 if (n
!= 0 && n
!= 1)
4367 mddev
->parallel_resync
= n
;
4369 if (mddev
->sync_thread
)
4370 wake_up(&resync_wait
);
4375 /* force parallel resync, even with shared block devices */
4376 static struct md_sysfs_entry md_sync_force_parallel
=
4377 __ATTR(sync_force_parallel
, S_IRUGO
|S_IWUSR
,
4378 sync_force_parallel_show
, sync_force_parallel_store
);
4381 sync_speed_show(struct mddev
*mddev
, char *page
)
4383 unsigned long resync
, dt
, db
;
4384 if (mddev
->curr_resync
== 0)
4385 return sprintf(page
, "none\n");
4386 resync
= mddev
->curr_mark_cnt
- atomic_read(&mddev
->recovery_active
);
4387 dt
= (jiffies
- mddev
->resync_mark
) / HZ
;
4389 db
= resync
- mddev
->resync_mark_cnt
;
4390 return sprintf(page
, "%lu\n", db
/dt
/2); /* K/sec */
4393 static struct md_sysfs_entry md_sync_speed
= __ATTR_RO(sync_speed
);
4396 sync_completed_show(struct mddev
*mddev
, char *page
)
4398 unsigned long long max_sectors
, resync
;
4400 if (!test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
4401 return sprintf(page
, "none\n");
4403 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ||
4404 test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
4405 max_sectors
= mddev
->resync_max_sectors
;
4407 max_sectors
= mddev
->dev_sectors
;
4409 resync
= mddev
->curr_resync_completed
;
4410 return sprintf(page
, "%llu / %llu\n", resync
, max_sectors
);
4413 static struct md_sysfs_entry md_sync_completed
= __ATTR_RO(sync_completed
);
4416 min_sync_show(struct mddev
*mddev
, char *page
)
4418 return sprintf(page
, "%llu\n",
4419 (unsigned long long)mddev
->resync_min
);
4422 min_sync_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4424 unsigned long long min
;
4425 if (strict_strtoull(buf
, 10, &min
))
4427 if (min
> mddev
->resync_max
)
4429 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
4432 /* Must be a multiple of chunk_size */
4433 if (mddev
->chunk_sectors
) {
4434 sector_t temp
= min
;
4435 if (sector_div(temp
, mddev
->chunk_sectors
))
4438 mddev
->resync_min
= min
;
4443 static struct md_sysfs_entry md_min_sync
=
4444 __ATTR(sync_min
, S_IRUGO
|S_IWUSR
, min_sync_show
, min_sync_store
);
4447 max_sync_show(struct mddev
*mddev
, char *page
)
4449 if (mddev
->resync_max
== MaxSector
)
4450 return sprintf(page
, "max\n");
4452 return sprintf(page
, "%llu\n",
4453 (unsigned long long)mddev
->resync_max
);
4456 max_sync_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4458 if (strncmp(buf
, "max", 3) == 0)
4459 mddev
->resync_max
= MaxSector
;
4461 unsigned long long max
;
4462 if (strict_strtoull(buf
, 10, &max
))
4464 if (max
< mddev
->resync_min
)
4466 if (max
< mddev
->resync_max
&&
4468 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
4471 /* Must be a multiple of chunk_size */
4472 if (mddev
->chunk_sectors
) {
4473 sector_t temp
= max
;
4474 if (sector_div(temp
, mddev
->chunk_sectors
))
4477 mddev
->resync_max
= max
;
4479 wake_up(&mddev
->recovery_wait
);
4483 static struct md_sysfs_entry md_max_sync
=
4484 __ATTR(sync_max
, S_IRUGO
|S_IWUSR
, max_sync_show
, max_sync_store
);
4487 suspend_lo_show(struct mddev
*mddev
, char *page
)
4489 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->suspend_lo
);
4493 suspend_lo_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4496 unsigned long long new = simple_strtoull(buf
, &e
, 10);
4497 unsigned long long old
= mddev
->suspend_lo
;
4499 if (mddev
->pers
== NULL
||
4500 mddev
->pers
->quiesce
== NULL
)
4502 if (buf
== e
|| (*e
&& *e
!= '\n'))
4505 mddev
->suspend_lo
= new;
4507 /* Shrinking suspended region */
4508 mddev
->pers
->quiesce(mddev
, 2);
4510 /* Expanding suspended region - need to wait */
4511 mddev
->pers
->quiesce(mddev
, 1);
4512 mddev
->pers
->quiesce(mddev
, 0);
4516 static struct md_sysfs_entry md_suspend_lo
=
4517 __ATTR(suspend_lo
, S_IRUGO
|S_IWUSR
, suspend_lo_show
, suspend_lo_store
);
4521 suspend_hi_show(struct mddev
*mddev
, char *page
)
4523 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->suspend_hi
);
4527 suspend_hi_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4530 unsigned long long new = simple_strtoull(buf
, &e
, 10);
4531 unsigned long long old
= mddev
->suspend_hi
;
4533 if (mddev
->pers
== NULL
||
4534 mddev
->pers
->quiesce
== NULL
)
4536 if (buf
== e
|| (*e
&& *e
!= '\n'))
4539 mddev
->suspend_hi
= new;
4541 /* Shrinking suspended region */
4542 mddev
->pers
->quiesce(mddev
, 2);
4544 /* Expanding suspended region - need to wait */
4545 mddev
->pers
->quiesce(mddev
, 1);
4546 mddev
->pers
->quiesce(mddev
, 0);
4550 static struct md_sysfs_entry md_suspend_hi
=
4551 __ATTR(suspend_hi
, S_IRUGO
|S_IWUSR
, suspend_hi_show
, suspend_hi_store
);
4554 reshape_position_show(struct mddev
*mddev
, char *page
)
4556 if (mddev
->reshape_position
!= MaxSector
)
4557 return sprintf(page
, "%llu\n",
4558 (unsigned long long)mddev
->reshape_position
);
4559 strcpy(page
, "none\n");
4564 reshape_position_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4566 struct md_rdev
*rdev
;
4568 unsigned long long new = simple_strtoull(buf
, &e
, 10);
4571 if (buf
== e
|| (*e
&& *e
!= '\n'))
4573 mddev
->reshape_position
= new;
4574 mddev
->delta_disks
= 0;
4575 mddev
->reshape_backwards
= 0;
4576 mddev
->new_level
= mddev
->level
;
4577 mddev
->new_layout
= mddev
->layout
;
4578 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
4579 rdev_for_each(rdev
, mddev
)
4580 rdev
->new_data_offset
= rdev
->data_offset
;
4584 static struct md_sysfs_entry md_reshape_position
=
4585 __ATTR(reshape_position
, S_IRUGO
|S_IWUSR
, reshape_position_show
,
4586 reshape_position_store
);
4589 reshape_direction_show(struct mddev
*mddev
, char *page
)
4591 return sprintf(page
, "%s\n",
4592 mddev
->reshape_backwards
? "backwards" : "forwards");
4596 reshape_direction_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4599 if (cmd_match(buf
, "forwards"))
4601 else if (cmd_match(buf
, "backwards"))
4605 if (mddev
->reshape_backwards
== backwards
)
4608 /* check if we are allowed to change */
4609 if (mddev
->delta_disks
)
4612 if (mddev
->persistent
&&
4613 mddev
->major_version
== 0)
4616 mddev
->reshape_backwards
= backwards
;
4620 static struct md_sysfs_entry md_reshape_direction
=
4621 __ATTR(reshape_direction
, S_IRUGO
|S_IWUSR
, reshape_direction_show
,
4622 reshape_direction_store
);
4625 array_size_show(struct mddev
*mddev
, char *page
)
4627 if (mddev
->external_size
)
4628 return sprintf(page
, "%llu\n",
4629 (unsigned long long)mddev
->array_sectors
/2);
4631 return sprintf(page
, "default\n");
4635 array_size_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4639 if (strncmp(buf
, "default", 7) == 0) {
4641 sectors
= mddev
->pers
->size(mddev
, 0, 0);
4643 sectors
= mddev
->array_sectors
;
4645 mddev
->external_size
= 0;
4647 if (strict_blocks_to_sectors(buf
, §ors
) < 0)
4649 if (mddev
->pers
&& mddev
->pers
->size(mddev
, 0, 0) < sectors
)
4652 mddev
->external_size
= 1;
4655 mddev
->array_sectors
= sectors
;
4657 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
4658 revalidate_disk(mddev
->gendisk
);
4663 static struct md_sysfs_entry md_array_size
=
4664 __ATTR(array_size
, S_IRUGO
|S_IWUSR
, array_size_show
,
4667 static struct attribute
*md_default_attrs
[] = {
4670 &md_raid_disks
.attr
,
4671 &md_chunk_size
.attr
,
4673 &md_resync_start
.attr
,
4675 &md_new_device
.attr
,
4676 &md_safe_delay
.attr
,
4677 &md_array_state
.attr
,
4678 &md_reshape_position
.attr
,
4679 &md_reshape_direction
.attr
,
4680 &md_array_size
.attr
,
4681 &max_corr_read_errors
.attr
,
4685 static struct attribute
*md_redundancy_attrs
[] = {
4687 &md_mismatches
.attr
,
4690 &md_sync_speed
.attr
,
4691 &md_sync_force_parallel
.attr
,
4692 &md_sync_completed
.attr
,
4695 &md_suspend_lo
.attr
,
4696 &md_suspend_hi
.attr
,
4701 static struct attribute_group md_redundancy_group
= {
4703 .attrs
= md_redundancy_attrs
,
4708 md_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
4710 struct md_sysfs_entry
*entry
= container_of(attr
, struct md_sysfs_entry
, attr
);
4711 struct mddev
*mddev
= container_of(kobj
, struct mddev
, kobj
);
4716 spin_lock(&all_mddevs_lock
);
4717 if (list_empty(&mddev
->all_mddevs
)) {
4718 spin_unlock(&all_mddevs_lock
);
4722 spin_unlock(&all_mddevs_lock
);
4724 rv
= mddev_lock(mddev
);
4726 rv
= entry
->show(mddev
, page
);
4727 mddev_unlock(mddev
);
4734 md_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
4735 const char *page
, size_t length
)
4737 struct md_sysfs_entry
*entry
= container_of(attr
, struct md_sysfs_entry
, attr
);
4738 struct mddev
*mddev
= container_of(kobj
, struct mddev
, kobj
);
4743 if (!capable(CAP_SYS_ADMIN
))
4745 spin_lock(&all_mddevs_lock
);
4746 if (list_empty(&mddev
->all_mddevs
)) {
4747 spin_unlock(&all_mddevs_lock
);
4751 spin_unlock(&all_mddevs_lock
);
4752 rv
= mddev_lock(mddev
);
4754 rv
= entry
->store(mddev
, page
, length
);
4755 mddev_unlock(mddev
);
4761 static void md_free(struct kobject
*ko
)
4763 struct mddev
*mddev
= container_of(ko
, struct mddev
, kobj
);
4765 if (mddev
->sysfs_state
)
4766 sysfs_put(mddev
->sysfs_state
);
4768 if (mddev
->gendisk
) {
4769 del_gendisk(mddev
->gendisk
);
4770 put_disk(mddev
->gendisk
);
4773 blk_cleanup_queue(mddev
->queue
);
4778 static const struct sysfs_ops md_sysfs_ops
= {
4779 .show
= md_attr_show
,
4780 .store
= md_attr_store
,
4782 static struct kobj_type md_ktype
= {
4784 .sysfs_ops
= &md_sysfs_ops
,
4785 .default_attrs
= md_default_attrs
,
4790 static void mddev_delayed_delete(struct work_struct
*ws
)
4792 struct mddev
*mddev
= container_of(ws
, struct mddev
, del_work
);
4794 sysfs_remove_group(&mddev
->kobj
, &md_bitmap_group
);
4795 kobject_del(&mddev
->kobj
);
4796 kobject_put(&mddev
->kobj
);
4799 static int md_alloc(dev_t dev
, char *name
)
4801 static DEFINE_MUTEX(disks_mutex
);
4802 struct mddev
*mddev
= mddev_find(dev
);
4803 struct gendisk
*disk
;
4812 partitioned
= (MAJOR(mddev
->unit
) != MD_MAJOR
);
4813 shift
= partitioned
? MdpMinorShift
: 0;
4814 unit
= MINOR(mddev
->unit
) >> shift
;
4816 /* wait for any previous instance of this device to be
4817 * completely removed (mddev_delayed_delete).
4819 flush_workqueue(md_misc_wq
);
4821 mutex_lock(&disks_mutex
);
4827 /* Need to ensure that 'name' is not a duplicate.
4829 struct mddev
*mddev2
;
4830 spin_lock(&all_mddevs_lock
);
4832 list_for_each_entry(mddev2
, &all_mddevs
, all_mddevs
)
4833 if (mddev2
->gendisk
&&
4834 strcmp(mddev2
->gendisk
->disk_name
, name
) == 0) {
4835 spin_unlock(&all_mddevs_lock
);
4838 spin_unlock(&all_mddevs_lock
);
4842 mddev
->queue
= blk_alloc_queue(GFP_KERNEL
);
4845 mddev
->queue
->queuedata
= mddev
;
4847 blk_queue_make_request(mddev
->queue
, md_make_request
);
4848 blk_set_stacking_limits(&mddev
->queue
->limits
);
4850 disk
= alloc_disk(1 << shift
);
4852 blk_cleanup_queue(mddev
->queue
);
4853 mddev
->queue
= NULL
;
4856 disk
->major
= MAJOR(mddev
->unit
);
4857 disk
->first_minor
= unit
<< shift
;
4859 strcpy(disk
->disk_name
, name
);
4860 else if (partitioned
)
4861 sprintf(disk
->disk_name
, "md_d%d", unit
);
4863 sprintf(disk
->disk_name
, "md%d", unit
);
4864 disk
->fops
= &md_fops
;
4865 disk
->private_data
= mddev
;
4866 disk
->queue
= mddev
->queue
;
4867 blk_queue_flush(mddev
->queue
, REQ_FLUSH
| REQ_FUA
);
4868 /* Allow extended partitions. This makes the
4869 * 'mdp' device redundant, but we can't really
4872 disk
->flags
|= GENHD_FL_EXT_DEVT
;
4873 mddev
->gendisk
= disk
;
4874 /* As soon as we call add_disk(), another thread could get
4875 * through to md_open, so make sure it doesn't get too far
4877 mutex_lock(&mddev
->open_mutex
);
4880 error
= kobject_init_and_add(&mddev
->kobj
, &md_ktype
,
4881 &disk_to_dev(disk
)->kobj
, "%s", "md");
4883 /* This isn't possible, but as kobject_init_and_add is marked
4884 * __must_check, we must do something with the result
4886 printk(KERN_WARNING
"md: cannot register %s/md - name in use\n",
4890 if (mddev
->kobj
.sd
&&
4891 sysfs_create_group(&mddev
->kobj
, &md_bitmap_group
))
4892 printk(KERN_DEBUG
"pointless warning\n");
4893 mutex_unlock(&mddev
->open_mutex
);
4895 mutex_unlock(&disks_mutex
);
4896 if (!error
&& mddev
->kobj
.sd
) {
4897 kobject_uevent(&mddev
->kobj
, KOBJ_ADD
);
4898 mddev
->sysfs_state
= sysfs_get_dirent_safe(mddev
->kobj
.sd
, "array_state");
4904 static struct kobject
*md_probe(dev_t dev
, int *part
, void *data
)
4906 md_alloc(dev
, NULL
);
4910 static int add_named_array(const char *val
, struct kernel_param
*kp
)
4912 /* val must be "md_*" where * is not all digits.
4913 * We allocate an array with a large free minor number, and
4914 * set the name to val. val must not already be an active name.
4916 int len
= strlen(val
);
4917 char buf
[DISK_NAME_LEN
];
4919 while (len
&& val
[len
-1] == '\n')
4921 if (len
>= DISK_NAME_LEN
)
4923 strlcpy(buf
, val
, len
+1);
4924 if (strncmp(buf
, "md_", 3) != 0)
4926 return md_alloc(0, buf
);
4929 static void md_safemode_timeout(unsigned long data
)
4931 struct mddev
*mddev
= (struct mddev
*) data
;
4933 if (!atomic_read(&mddev
->writes_pending
)) {
4934 mddev
->safemode
= 1;
4935 if (mddev
->external
)
4936 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4938 md_wakeup_thread(mddev
->thread
);
4941 static int start_dirty_degraded
;
4943 int md_run(struct mddev
*mddev
)
4946 struct md_rdev
*rdev
;
4947 struct md_personality
*pers
;
4949 if (list_empty(&mddev
->disks
))
4950 /* cannot run an array with no devices.. */
4955 /* Cannot run until previous stop completes properly */
4956 if (mddev
->sysfs_active
)
4960 * Analyze all RAID superblock(s)
4962 if (!mddev
->raid_disks
) {
4963 if (!mddev
->persistent
)
4968 if (mddev
->level
!= LEVEL_NONE
)
4969 request_module("md-level-%d", mddev
->level
);
4970 else if (mddev
->clevel
[0])
4971 request_module("md-%s", mddev
->clevel
);
4974 * Drop all container device buffers, from now on
4975 * the only valid external interface is through the md
4978 rdev_for_each(rdev
, mddev
) {
4979 if (test_bit(Faulty
, &rdev
->flags
))
4981 sync_blockdev(rdev
->bdev
);
4982 invalidate_bdev(rdev
->bdev
);
4984 /* perform some consistency tests on the device.
4985 * We don't want the data to overlap the metadata,
4986 * Internal Bitmap issues have been handled elsewhere.
4988 if (rdev
->meta_bdev
) {
4989 /* Nothing to check */;
4990 } else if (rdev
->data_offset
< rdev
->sb_start
) {
4991 if (mddev
->dev_sectors
&&
4992 rdev
->data_offset
+ mddev
->dev_sectors
4994 printk("md: %s: data overlaps metadata\n",
4999 if (rdev
->sb_start
+ rdev
->sb_size
/512
5000 > rdev
->data_offset
) {
5001 printk("md: %s: metadata overlaps data\n",
5006 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
5009 if (mddev
->bio_set
== NULL
)
5010 mddev
->bio_set
= bioset_create(BIO_POOL_SIZE
,
5011 sizeof(struct mddev
*));
5013 spin_lock(&pers_lock
);
5014 pers
= find_pers(mddev
->level
, mddev
->clevel
);
5015 if (!pers
|| !try_module_get(pers
->owner
)) {
5016 spin_unlock(&pers_lock
);
5017 if (mddev
->level
!= LEVEL_NONE
)
5018 printk(KERN_WARNING
"md: personality for level %d is not loaded!\n",
5021 printk(KERN_WARNING
"md: personality for level %s is not loaded!\n",
5026 spin_unlock(&pers_lock
);
5027 if (mddev
->level
!= pers
->level
) {
5028 mddev
->level
= pers
->level
;
5029 mddev
->new_level
= pers
->level
;
5031 strlcpy(mddev
->clevel
, pers
->name
, sizeof(mddev
->clevel
));
5033 if (mddev
->reshape_position
!= MaxSector
&&
5034 pers
->start_reshape
== NULL
) {
5035 /* This personality cannot handle reshaping... */
5037 module_put(pers
->owner
);
5041 if (pers
->sync_request
) {
5042 /* Warn if this is a potentially silly
5045 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
5046 struct md_rdev
*rdev2
;
5049 rdev_for_each(rdev
, mddev
)
5050 rdev_for_each(rdev2
, mddev
) {
5052 rdev
->bdev
->bd_contains
==
5053 rdev2
->bdev
->bd_contains
) {
5055 "%s: WARNING: %s appears to be"
5056 " on the same physical disk as"
5059 bdevname(rdev
->bdev
,b
),
5060 bdevname(rdev2
->bdev
,b2
));
5067 "True protection against single-disk"
5068 " failure might be compromised.\n");
5071 mddev
->recovery
= 0;
5072 /* may be over-ridden by personality */
5073 mddev
->resync_max_sectors
= mddev
->dev_sectors
;
5075 mddev
->ok_start_degraded
= start_dirty_degraded
;
5077 if (start_readonly
&& mddev
->ro
== 0)
5078 mddev
->ro
= 2; /* read-only, but switch on first write */
5080 err
= mddev
->pers
->run(mddev
);
5082 printk(KERN_ERR
"md: pers->run() failed ...\n");
5083 else if (mddev
->pers
->size(mddev
, 0, 0) < mddev
->array_sectors
) {
5084 WARN_ONCE(!mddev
->external_size
, "%s: default size too small,"
5085 " but 'external_size' not in effect?\n", __func__
);
5087 "md: invalid array_size %llu > default size %llu\n",
5088 (unsigned long long)mddev
->array_sectors
/ 2,
5089 (unsigned long long)mddev
->pers
->size(mddev
, 0, 0) / 2);
5091 mddev
->pers
->stop(mddev
);
5093 if (err
== 0 && mddev
->pers
->sync_request
&&
5094 (mddev
->bitmap_info
.file
|| mddev
->bitmap_info
.offset
)) {
5095 err
= bitmap_create(mddev
);
5097 printk(KERN_ERR
"%s: failed to create bitmap (%d)\n",
5098 mdname(mddev
), err
);
5099 mddev
->pers
->stop(mddev
);
5103 module_put(mddev
->pers
->owner
);
5105 bitmap_destroy(mddev
);
5108 if (mddev
->pers
->sync_request
) {
5109 if (mddev
->kobj
.sd
&&
5110 sysfs_create_group(&mddev
->kobj
, &md_redundancy_group
))
5112 "md: cannot register extra attributes for %s\n",
5114 mddev
->sysfs_action
= sysfs_get_dirent_safe(mddev
->kobj
.sd
, "sync_action");
5115 } else if (mddev
->ro
== 2) /* auto-readonly not meaningful */
5118 atomic_set(&mddev
->writes_pending
,0);
5119 atomic_set(&mddev
->max_corr_read_errors
,
5120 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS
);
5121 mddev
->safemode
= 0;
5122 mddev
->safemode_timer
.function
= md_safemode_timeout
;
5123 mddev
->safemode_timer
.data
= (unsigned long) mddev
;
5124 mddev
->safemode_delay
= (200 * HZ
)/1000 +1; /* 200 msec delay */
5128 rdev_for_each(rdev
, mddev
)
5129 if (rdev
->raid_disk
>= 0)
5130 if (sysfs_link_rdev(mddev
, rdev
))
5131 /* failure here is OK */;
5133 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5136 md_update_sb(mddev
, 0);
5138 md_new_event(mddev
);
5139 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
5140 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
5141 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
5144 EXPORT_SYMBOL_GPL(md_run
);
5146 static int do_md_run(struct mddev
*mddev
)
5150 err
= md_run(mddev
);
5153 err
= bitmap_load(mddev
);
5155 bitmap_destroy(mddev
);
5159 md_wakeup_thread(mddev
->thread
);
5160 md_wakeup_thread(mddev
->sync_thread
); /* possibly kick off a reshape */
5162 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
5163 revalidate_disk(mddev
->gendisk
);
5165 kobject_uevent(&disk_to_dev(mddev
->gendisk
)->kobj
, KOBJ_CHANGE
);
5170 static int restart_array(struct mddev
*mddev
)
5172 struct gendisk
*disk
= mddev
->gendisk
;
5174 /* Complain if it has no devices */
5175 if (list_empty(&mddev
->disks
))
5181 mddev
->safemode
= 0;
5183 set_disk_ro(disk
, 0);
5184 printk(KERN_INFO
"md: %s switched to read-write mode.\n",
5186 /* Kick recovery or resync if necessary */
5187 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5188 md_wakeup_thread(mddev
->thread
);
5189 md_wakeup_thread(mddev
->sync_thread
);
5190 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
5194 /* similar to deny_write_access, but accounts for our holding a reference
5195 * to the file ourselves */
5196 static int deny_bitmap_write_access(struct file
* file
)
5198 struct inode
*inode
= file
->f_mapping
->host
;
5200 spin_lock(&inode
->i_lock
);
5201 if (atomic_read(&inode
->i_writecount
) > 1) {
5202 spin_unlock(&inode
->i_lock
);
5205 atomic_set(&inode
->i_writecount
, -1);
5206 spin_unlock(&inode
->i_lock
);
5211 void restore_bitmap_write_access(struct file
*file
)
5213 struct inode
*inode
= file
->f_mapping
->host
;
5215 spin_lock(&inode
->i_lock
);
5216 atomic_set(&inode
->i_writecount
, 1);
5217 spin_unlock(&inode
->i_lock
);
5220 static void md_clean(struct mddev
*mddev
)
5222 mddev
->array_sectors
= 0;
5223 mddev
->external_size
= 0;
5224 mddev
->dev_sectors
= 0;
5225 mddev
->raid_disks
= 0;
5226 mddev
->recovery_cp
= 0;
5227 mddev
->resync_min
= 0;
5228 mddev
->resync_max
= MaxSector
;
5229 mddev
->reshape_position
= MaxSector
;
5230 mddev
->external
= 0;
5231 mddev
->persistent
= 0;
5232 mddev
->level
= LEVEL_NONE
;
5233 mddev
->clevel
[0] = 0;
5236 mddev
->metadata_type
[0] = 0;
5237 mddev
->chunk_sectors
= 0;
5238 mddev
->ctime
= mddev
->utime
= 0;
5240 mddev
->max_disks
= 0;
5242 mddev
->can_decrease_events
= 0;
5243 mddev
->delta_disks
= 0;
5244 mddev
->reshape_backwards
= 0;
5245 mddev
->new_level
= LEVEL_NONE
;
5246 mddev
->new_layout
= 0;
5247 mddev
->new_chunk_sectors
= 0;
5248 mddev
->curr_resync
= 0;
5249 mddev
->resync_mismatches
= 0;
5250 mddev
->suspend_lo
= mddev
->suspend_hi
= 0;
5251 mddev
->sync_speed_min
= mddev
->sync_speed_max
= 0;
5252 mddev
->recovery
= 0;
5255 mddev
->degraded
= 0;
5256 mddev
->safemode
= 0;
5257 mddev
->merge_check_needed
= 0;
5258 mddev
->bitmap_info
.offset
= 0;
5259 mddev
->bitmap_info
.default_offset
= 0;
5260 mddev
->bitmap_info
.default_space
= 0;
5261 mddev
->bitmap_info
.chunksize
= 0;
5262 mddev
->bitmap_info
.daemon_sleep
= 0;
5263 mddev
->bitmap_info
.max_write_behind
= 0;
5266 static void __md_stop_writes(struct mddev
*mddev
)
5268 if (mddev
->sync_thread
) {
5269 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
5270 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
5271 reap_sync_thread(mddev
);
5274 del_timer_sync(&mddev
->safemode_timer
);
5276 bitmap_flush(mddev
);
5277 md_super_wait(mddev
);
5279 if (!mddev
->in_sync
|| mddev
->flags
) {
5280 /* mark array as shutdown cleanly */
5282 md_update_sb(mddev
, 1);
5286 void md_stop_writes(struct mddev
*mddev
)
5289 __md_stop_writes(mddev
);
5290 mddev_unlock(mddev
);
5292 EXPORT_SYMBOL_GPL(md_stop_writes
);
5294 void md_stop(struct mddev
*mddev
)
5297 mddev
->pers
->stop(mddev
);
5298 if (mddev
->pers
->sync_request
&& mddev
->to_remove
== NULL
)
5299 mddev
->to_remove
= &md_redundancy_group
;
5300 module_put(mddev
->pers
->owner
);
5302 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
5304 EXPORT_SYMBOL_GPL(md_stop
);
5306 static int md_set_readonly(struct mddev
*mddev
, struct block_device
*bdev
)
5309 mutex_lock(&mddev
->open_mutex
);
5310 if (atomic_read(&mddev
->openers
) > !!bdev
) {
5311 printk("md: %s still in use.\n",mdname(mddev
));
5316 sync_blockdev(bdev
);
5318 __md_stop_writes(mddev
);
5324 set_disk_ro(mddev
->gendisk
, 1);
5325 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
5326 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
5330 mutex_unlock(&mddev
->open_mutex
);
5335 * 0 - completely stop and dis-assemble array
5336 * 2 - stop but do not disassemble array
5338 static int do_md_stop(struct mddev
* mddev
, int mode
,
5339 struct block_device
*bdev
)
5341 struct gendisk
*disk
= mddev
->gendisk
;
5342 struct md_rdev
*rdev
;
5344 mutex_lock(&mddev
->open_mutex
);
5345 if (atomic_read(&mddev
->openers
) > !!bdev
||
5346 mddev
->sysfs_active
) {
5347 printk("md: %s still in use.\n",mdname(mddev
));
5348 mutex_unlock(&mddev
->open_mutex
);
5352 /* It is possible IO was issued on some other
5353 * open file which was closed before we took ->open_mutex.
5354 * As that was not the last close __blkdev_put will not
5355 * have called sync_blockdev, so we must.
5357 sync_blockdev(bdev
);
5361 set_disk_ro(disk
, 0);
5363 __md_stop_writes(mddev
);
5365 mddev
->queue
->merge_bvec_fn
= NULL
;
5366 mddev
->queue
->backing_dev_info
.congested_fn
= NULL
;
5368 /* tell userspace to handle 'inactive' */
5369 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
5371 rdev_for_each(rdev
, mddev
)
5372 if (rdev
->raid_disk
>= 0)
5373 sysfs_unlink_rdev(mddev
, rdev
);
5375 set_capacity(disk
, 0);
5376 mutex_unlock(&mddev
->open_mutex
);
5378 revalidate_disk(disk
);
5383 mutex_unlock(&mddev
->open_mutex
);
5385 * Free resources if final stop
5388 printk(KERN_INFO
"md: %s stopped.\n", mdname(mddev
));
5390 bitmap_destroy(mddev
);
5391 if (mddev
->bitmap_info
.file
) {
5392 restore_bitmap_write_access(mddev
->bitmap_info
.file
);
5393 fput(mddev
->bitmap_info
.file
);
5394 mddev
->bitmap_info
.file
= NULL
;
5396 mddev
->bitmap_info
.offset
= 0;
5398 export_array(mddev
);
5401 kobject_uevent(&disk_to_dev(mddev
->gendisk
)->kobj
, KOBJ_CHANGE
);
5402 if (mddev
->hold_active
== UNTIL_STOP
)
5403 mddev
->hold_active
= 0;
5405 blk_integrity_unregister(disk
);
5406 md_new_event(mddev
);
5407 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
5412 static void autorun_array(struct mddev
*mddev
)
5414 struct md_rdev
*rdev
;
5417 if (list_empty(&mddev
->disks
))
5420 printk(KERN_INFO
"md: running: ");
5422 rdev_for_each(rdev
, mddev
) {
5423 char b
[BDEVNAME_SIZE
];
5424 printk("<%s>", bdevname(rdev
->bdev
,b
));
5428 err
= do_md_run(mddev
);
5430 printk(KERN_WARNING
"md: do_md_run() returned %d\n", err
);
5431 do_md_stop(mddev
, 0, NULL
);
5436 * lets try to run arrays based on all disks that have arrived
5437 * until now. (those are in pending_raid_disks)
5439 * the method: pick the first pending disk, collect all disks with
5440 * the same UUID, remove all from the pending list and put them into
5441 * the 'same_array' list. Then order this list based on superblock
5442 * update time (freshest comes first), kick out 'old' disks and
5443 * compare superblocks. If everything's fine then run it.
5445 * If "unit" is allocated, then bump its reference count
5447 static void autorun_devices(int part
)
5449 struct md_rdev
*rdev0
, *rdev
, *tmp
;
5450 struct mddev
*mddev
;
5451 char b
[BDEVNAME_SIZE
];
5453 printk(KERN_INFO
"md: autorun ...\n");
5454 while (!list_empty(&pending_raid_disks
)) {
5457 LIST_HEAD(candidates
);
5458 rdev0
= list_entry(pending_raid_disks
.next
,
5459 struct md_rdev
, same_set
);
5461 printk(KERN_INFO
"md: considering %s ...\n",
5462 bdevname(rdev0
->bdev
,b
));
5463 INIT_LIST_HEAD(&candidates
);
5464 rdev_for_each_list(rdev
, tmp
, &pending_raid_disks
)
5465 if (super_90_load(rdev
, rdev0
, 0) >= 0) {
5466 printk(KERN_INFO
"md: adding %s ...\n",
5467 bdevname(rdev
->bdev
,b
));
5468 list_move(&rdev
->same_set
, &candidates
);
5471 * now we have a set of devices, with all of them having
5472 * mostly sane superblocks. It's time to allocate the
5476 dev
= MKDEV(mdp_major
,
5477 rdev0
->preferred_minor
<< MdpMinorShift
);
5478 unit
= MINOR(dev
) >> MdpMinorShift
;
5480 dev
= MKDEV(MD_MAJOR
, rdev0
->preferred_minor
);
5483 if (rdev0
->preferred_minor
!= unit
) {
5484 printk(KERN_INFO
"md: unit number in %s is bad: %d\n",
5485 bdevname(rdev0
->bdev
, b
), rdev0
->preferred_minor
);
5489 md_probe(dev
, NULL
, NULL
);
5490 mddev
= mddev_find(dev
);
5491 if (!mddev
|| !mddev
->gendisk
) {
5495 "md: cannot allocate memory for md drive.\n");
5498 if (mddev_lock(mddev
))
5499 printk(KERN_WARNING
"md: %s locked, cannot run\n",
5501 else if (mddev
->raid_disks
|| mddev
->major_version
5502 || !list_empty(&mddev
->disks
)) {
5504 "md: %s already running, cannot run %s\n",
5505 mdname(mddev
), bdevname(rdev0
->bdev
,b
));
5506 mddev_unlock(mddev
);
5508 printk(KERN_INFO
"md: created %s\n", mdname(mddev
));
5509 mddev
->persistent
= 1;
5510 rdev_for_each_list(rdev
, tmp
, &candidates
) {
5511 list_del_init(&rdev
->same_set
);
5512 if (bind_rdev_to_array(rdev
, mddev
))
5515 autorun_array(mddev
);
5516 mddev_unlock(mddev
);
5518 /* on success, candidates will be empty, on error
5521 rdev_for_each_list(rdev
, tmp
, &candidates
) {
5522 list_del_init(&rdev
->same_set
);
5527 printk(KERN_INFO
"md: ... autorun DONE.\n");
5529 #endif /* !MODULE */
5531 static int get_version(void __user
* arg
)
5535 ver
.major
= MD_MAJOR_VERSION
;
5536 ver
.minor
= MD_MINOR_VERSION
;
5537 ver
.patchlevel
= MD_PATCHLEVEL_VERSION
;
5539 if (copy_to_user(arg
, &ver
, sizeof(ver
)))
5545 static int get_array_info(struct mddev
* mddev
, void __user
* arg
)
5547 mdu_array_info_t info
;
5548 int nr
,working
,insync
,failed
,spare
;
5549 struct md_rdev
*rdev
;
5551 nr
=working
=insync
=failed
=spare
=0;
5552 rdev_for_each(rdev
, mddev
) {
5554 if (test_bit(Faulty
, &rdev
->flags
))
5558 if (test_bit(In_sync
, &rdev
->flags
))
5565 info
.major_version
= mddev
->major_version
;
5566 info
.minor_version
= mddev
->minor_version
;
5567 info
.patch_version
= MD_PATCHLEVEL_VERSION
;
5568 info
.ctime
= mddev
->ctime
;
5569 info
.level
= mddev
->level
;
5570 info
.size
= mddev
->dev_sectors
/ 2;
5571 if (info
.size
!= mddev
->dev_sectors
/ 2) /* overflow */
5574 info
.raid_disks
= mddev
->raid_disks
;
5575 info
.md_minor
= mddev
->md_minor
;
5576 info
.not_persistent
= !mddev
->persistent
;
5578 info
.utime
= mddev
->utime
;
5581 info
.state
= (1<<MD_SB_CLEAN
);
5582 if (mddev
->bitmap
&& mddev
->bitmap_info
.offset
)
5583 info
.state
= (1<<MD_SB_BITMAP_PRESENT
);
5584 info
.active_disks
= insync
;
5585 info
.working_disks
= working
;
5586 info
.failed_disks
= failed
;
5587 info
.spare_disks
= spare
;
5589 info
.layout
= mddev
->layout
;
5590 info
.chunk_size
= mddev
->chunk_sectors
<< 9;
5592 if (copy_to_user(arg
, &info
, sizeof(info
)))
5598 static int get_bitmap_file(struct mddev
* mddev
, void __user
* arg
)
5600 mdu_bitmap_file_t
*file
= NULL
; /* too big for stack allocation */
5601 char *ptr
, *buf
= NULL
;
5604 if (md_allow_write(mddev
))
5605 file
= kmalloc(sizeof(*file
), GFP_NOIO
);
5607 file
= kmalloc(sizeof(*file
), GFP_KERNEL
);
5612 /* bitmap disabled, zero the first byte and copy out */
5613 if (!mddev
->bitmap
|| !mddev
->bitmap
->storage
.file
) {
5614 file
->pathname
[0] = '\0';
5618 buf
= kmalloc(sizeof(file
->pathname
), GFP_KERNEL
);
5622 ptr
= d_path(&mddev
->bitmap
->storage
.file
->f_path
,
5623 buf
, sizeof(file
->pathname
));
5627 strcpy(file
->pathname
, ptr
);
5631 if (copy_to_user(arg
, file
, sizeof(*file
)))
5639 static int get_disk_info(struct mddev
* mddev
, void __user
* arg
)
5641 mdu_disk_info_t info
;
5642 struct md_rdev
*rdev
;
5644 if (copy_from_user(&info
, arg
, sizeof(info
)))
5647 rdev
= find_rdev_nr(mddev
, info
.number
);
5649 info
.major
= MAJOR(rdev
->bdev
->bd_dev
);
5650 info
.minor
= MINOR(rdev
->bdev
->bd_dev
);
5651 info
.raid_disk
= rdev
->raid_disk
;
5653 if (test_bit(Faulty
, &rdev
->flags
))
5654 info
.state
|= (1<<MD_DISK_FAULTY
);
5655 else if (test_bit(In_sync
, &rdev
->flags
)) {
5656 info
.state
|= (1<<MD_DISK_ACTIVE
);
5657 info
.state
|= (1<<MD_DISK_SYNC
);
5659 if (test_bit(WriteMostly
, &rdev
->flags
))
5660 info
.state
|= (1<<MD_DISK_WRITEMOSTLY
);
5662 info
.major
= info
.minor
= 0;
5663 info
.raid_disk
= -1;
5664 info
.state
= (1<<MD_DISK_REMOVED
);
5667 if (copy_to_user(arg
, &info
, sizeof(info
)))
5673 static int add_new_disk(struct mddev
* mddev
, mdu_disk_info_t
*info
)
5675 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
5676 struct md_rdev
*rdev
;
5677 dev_t dev
= MKDEV(info
->major
,info
->minor
);
5679 if (info
->major
!= MAJOR(dev
) || info
->minor
!= MINOR(dev
))
5682 if (!mddev
->raid_disks
) {
5684 /* expecting a device which has a superblock */
5685 rdev
= md_import_device(dev
, mddev
->major_version
, mddev
->minor_version
);
5688 "md: md_import_device returned %ld\n",
5690 return PTR_ERR(rdev
);
5692 if (!list_empty(&mddev
->disks
)) {
5693 struct md_rdev
*rdev0
5694 = list_entry(mddev
->disks
.next
,
5695 struct md_rdev
, same_set
);
5696 err
= super_types
[mddev
->major_version
]
5697 .load_super(rdev
, rdev0
, mddev
->minor_version
);
5700 "md: %s has different UUID to %s\n",
5701 bdevname(rdev
->bdev
,b
),
5702 bdevname(rdev0
->bdev
,b2
));
5707 err
= bind_rdev_to_array(rdev
, mddev
);
5714 * add_new_disk can be used once the array is assembled
5715 * to add "hot spares". They must already have a superblock
5720 if (!mddev
->pers
->hot_add_disk
) {
5722 "%s: personality does not support diskops!\n",
5726 if (mddev
->persistent
)
5727 rdev
= md_import_device(dev
, mddev
->major_version
,
5728 mddev
->minor_version
);
5730 rdev
= md_import_device(dev
, -1, -1);
5733 "md: md_import_device returned %ld\n",
5735 return PTR_ERR(rdev
);
5737 /* set saved_raid_disk if appropriate */
5738 if (!mddev
->persistent
) {
5739 if (info
->state
& (1<<MD_DISK_SYNC
) &&
5740 info
->raid_disk
< mddev
->raid_disks
) {
5741 rdev
->raid_disk
= info
->raid_disk
;
5742 set_bit(In_sync
, &rdev
->flags
);
5744 rdev
->raid_disk
= -1;
5746 super_types
[mddev
->major_version
].
5747 validate_super(mddev
, rdev
);
5748 if ((info
->state
& (1<<MD_DISK_SYNC
)) &&
5749 rdev
->raid_disk
!= info
->raid_disk
) {
5750 /* This was a hot-add request, but events doesn't
5751 * match, so reject it.
5757 if (test_bit(In_sync
, &rdev
->flags
))
5758 rdev
->saved_raid_disk
= rdev
->raid_disk
;
5760 rdev
->saved_raid_disk
= -1;
5762 clear_bit(In_sync
, &rdev
->flags
); /* just to be sure */
5763 if (info
->state
& (1<<MD_DISK_WRITEMOSTLY
))
5764 set_bit(WriteMostly
, &rdev
->flags
);
5766 clear_bit(WriteMostly
, &rdev
->flags
);
5768 rdev
->raid_disk
= -1;
5769 err
= bind_rdev_to_array(rdev
, mddev
);
5770 if (!err
&& !mddev
->pers
->hot_remove_disk
) {
5771 /* If there is hot_add_disk but no hot_remove_disk
5772 * then added disks for geometry changes,
5773 * and should be added immediately.
5775 super_types
[mddev
->major_version
].
5776 validate_super(mddev
, rdev
);
5777 err
= mddev
->pers
->hot_add_disk(mddev
, rdev
);
5779 unbind_rdev_from_array(rdev
);
5784 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
5786 md_update_sb(mddev
, 1);
5787 if (mddev
->degraded
)
5788 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
5789 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5791 md_new_event(mddev
);
5792 md_wakeup_thread(mddev
->thread
);
5796 /* otherwise, add_new_disk is only allowed
5797 * for major_version==0 superblocks
5799 if (mddev
->major_version
!= 0) {
5800 printk(KERN_WARNING
"%s: ADD_NEW_DISK not supported\n",
5805 if (!(info
->state
& (1<<MD_DISK_FAULTY
))) {
5807 rdev
= md_import_device(dev
, -1, 0);
5810 "md: error, md_import_device() returned %ld\n",
5812 return PTR_ERR(rdev
);
5814 rdev
->desc_nr
= info
->number
;
5815 if (info
->raid_disk
< mddev
->raid_disks
)
5816 rdev
->raid_disk
= info
->raid_disk
;
5818 rdev
->raid_disk
= -1;
5820 if (rdev
->raid_disk
< mddev
->raid_disks
)
5821 if (info
->state
& (1<<MD_DISK_SYNC
))
5822 set_bit(In_sync
, &rdev
->flags
);
5824 if (info
->state
& (1<<MD_DISK_WRITEMOSTLY
))
5825 set_bit(WriteMostly
, &rdev
->flags
);
5827 if (!mddev
->persistent
) {
5828 printk(KERN_INFO
"md: nonpersistent superblock ...\n");
5829 rdev
->sb_start
= i_size_read(rdev
->bdev
->bd_inode
) / 512;
5831 rdev
->sb_start
= calc_dev_sboffset(rdev
);
5832 rdev
->sectors
= rdev
->sb_start
;
5834 err
= bind_rdev_to_array(rdev
, mddev
);
5844 static int hot_remove_disk(struct mddev
* mddev
, dev_t dev
)
5846 char b
[BDEVNAME_SIZE
];
5847 struct md_rdev
*rdev
;
5849 rdev
= find_rdev(mddev
, dev
);
5853 if (rdev
->raid_disk
>= 0)
5856 kick_rdev_from_array(rdev
);
5857 md_update_sb(mddev
, 1);
5858 md_new_event(mddev
);
5862 printk(KERN_WARNING
"md: cannot remove active disk %s from %s ...\n",
5863 bdevname(rdev
->bdev
,b
), mdname(mddev
));
5867 static int hot_add_disk(struct mddev
* mddev
, dev_t dev
)
5869 char b
[BDEVNAME_SIZE
];
5871 struct md_rdev
*rdev
;
5876 if (mddev
->major_version
!= 0) {
5877 printk(KERN_WARNING
"%s: HOT_ADD may only be used with"
5878 " version-0 superblocks.\n",
5882 if (!mddev
->pers
->hot_add_disk
) {
5884 "%s: personality does not support diskops!\n",
5889 rdev
= md_import_device(dev
, -1, 0);
5892 "md: error, md_import_device() returned %ld\n",
5897 if (mddev
->persistent
)
5898 rdev
->sb_start
= calc_dev_sboffset(rdev
);
5900 rdev
->sb_start
= i_size_read(rdev
->bdev
->bd_inode
) / 512;
5902 rdev
->sectors
= rdev
->sb_start
;
5904 if (test_bit(Faulty
, &rdev
->flags
)) {
5906 "md: can not hot-add faulty %s disk to %s!\n",
5907 bdevname(rdev
->bdev
,b
), mdname(mddev
));
5911 clear_bit(In_sync
, &rdev
->flags
);
5913 rdev
->saved_raid_disk
= -1;
5914 err
= bind_rdev_to_array(rdev
, mddev
);
5919 * The rest should better be atomic, we can have disk failures
5920 * noticed in interrupt contexts ...
5923 rdev
->raid_disk
= -1;
5925 md_update_sb(mddev
, 1);
5928 * Kick recovery, maybe this spare has to be added to the
5929 * array immediately.
5931 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5932 md_wakeup_thread(mddev
->thread
);
5933 md_new_event(mddev
);
5941 static int set_bitmap_file(struct mddev
*mddev
, int fd
)
5946 if (!mddev
->pers
->quiesce
)
5948 if (mddev
->recovery
|| mddev
->sync_thread
)
5950 /* we should be able to change the bitmap.. */
5956 return -EEXIST
; /* cannot add when bitmap is present */
5957 mddev
->bitmap_info
.file
= fget(fd
);
5959 if (mddev
->bitmap_info
.file
== NULL
) {
5960 printk(KERN_ERR
"%s: error: failed to get bitmap file\n",
5965 err
= deny_bitmap_write_access(mddev
->bitmap_info
.file
);
5967 printk(KERN_ERR
"%s: error: bitmap file is already in use\n",
5969 fput(mddev
->bitmap_info
.file
);
5970 mddev
->bitmap_info
.file
= NULL
;
5973 mddev
->bitmap_info
.offset
= 0; /* file overrides offset */
5974 } else if (mddev
->bitmap
== NULL
)
5975 return -ENOENT
; /* cannot remove what isn't there */
5978 mddev
->pers
->quiesce(mddev
, 1);
5980 err
= bitmap_create(mddev
);
5982 err
= bitmap_load(mddev
);
5984 if (fd
< 0 || err
) {
5985 bitmap_destroy(mddev
);
5986 fd
= -1; /* make sure to put the file */
5988 mddev
->pers
->quiesce(mddev
, 0);
5991 if (mddev
->bitmap_info
.file
) {
5992 restore_bitmap_write_access(mddev
->bitmap_info
.file
);
5993 fput(mddev
->bitmap_info
.file
);
5995 mddev
->bitmap_info
.file
= NULL
;
6002 * set_array_info is used two different ways
6003 * The original usage is when creating a new array.
6004 * In this usage, raid_disks is > 0 and it together with
6005 * level, size, not_persistent,layout,chunksize determine the
6006 * shape of the array.
6007 * This will always create an array with a type-0.90.0 superblock.
6008 * The newer usage is when assembling an array.
6009 * In this case raid_disks will be 0, and the major_version field is
6010 * use to determine which style super-blocks are to be found on the devices.
6011 * The minor and patch _version numbers are also kept incase the
6012 * super_block handler wishes to interpret them.
6014 static int set_array_info(struct mddev
* mddev
, mdu_array_info_t
*info
)
6017 if (info
->raid_disks
== 0) {
6018 /* just setting version number for superblock loading */
6019 if (info
->major_version
< 0 ||
6020 info
->major_version
>= ARRAY_SIZE(super_types
) ||
6021 super_types
[info
->major_version
].name
== NULL
) {
6022 /* maybe try to auto-load a module? */
6024 "md: superblock version %d not known\n",
6025 info
->major_version
);
6028 mddev
->major_version
= info
->major_version
;
6029 mddev
->minor_version
= info
->minor_version
;
6030 mddev
->patch_version
= info
->patch_version
;
6031 mddev
->persistent
= !info
->not_persistent
;
6032 /* ensure mddev_put doesn't delete this now that there
6033 * is some minimal configuration.
6035 mddev
->ctime
= get_seconds();
6038 mddev
->major_version
= MD_MAJOR_VERSION
;
6039 mddev
->minor_version
= MD_MINOR_VERSION
;
6040 mddev
->patch_version
= MD_PATCHLEVEL_VERSION
;
6041 mddev
->ctime
= get_seconds();
6043 mddev
->level
= info
->level
;
6044 mddev
->clevel
[0] = 0;
6045 mddev
->dev_sectors
= 2 * (sector_t
)info
->size
;
6046 mddev
->raid_disks
= info
->raid_disks
;
6047 /* don't set md_minor, it is determined by which /dev/md* was
6050 if (info
->state
& (1<<MD_SB_CLEAN
))
6051 mddev
->recovery_cp
= MaxSector
;
6053 mddev
->recovery_cp
= 0;
6054 mddev
->persistent
= ! info
->not_persistent
;
6055 mddev
->external
= 0;
6057 mddev
->layout
= info
->layout
;
6058 mddev
->chunk_sectors
= info
->chunk_size
>> 9;
6060 mddev
->max_disks
= MD_SB_DISKS
;
6062 if (mddev
->persistent
)
6064 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
6066 mddev
->bitmap_info
.default_offset
= MD_SB_BYTES
>> 9;
6067 mddev
->bitmap_info
.default_space
= 64*2 - (MD_SB_BYTES
>> 9);
6068 mddev
->bitmap_info
.offset
= 0;
6070 mddev
->reshape_position
= MaxSector
;
6073 * Generate a 128 bit UUID
6075 get_random_bytes(mddev
->uuid
, 16);
6077 mddev
->new_level
= mddev
->level
;
6078 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
6079 mddev
->new_layout
= mddev
->layout
;
6080 mddev
->delta_disks
= 0;
6081 mddev
->reshape_backwards
= 0;
6086 void md_set_array_sectors(struct mddev
*mddev
, sector_t array_sectors
)
6088 WARN(!mddev_is_locked(mddev
), "%s: unlocked mddev!\n", __func__
);
6090 if (mddev
->external_size
)
6093 mddev
->array_sectors
= array_sectors
;
6095 EXPORT_SYMBOL(md_set_array_sectors
);
6097 static int update_size(struct mddev
*mddev
, sector_t num_sectors
)
6099 struct md_rdev
*rdev
;
6101 int fit
= (num_sectors
== 0);
6103 if (mddev
->pers
->resize
== NULL
)
6105 /* The "num_sectors" is the number of sectors of each device that
6106 * is used. This can only make sense for arrays with redundancy.
6107 * linear and raid0 always use whatever space is available. We can only
6108 * consider changing this number if no resync or reconstruction is
6109 * happening, and if the new size is acceptable. It must fit before the
6110 * sb_start or, if that is <data_offset, it must fit before the size
6111 * of each device. If num_sectors is zero, we find the largest size
6114 if (mddev
->sync_thread
)
6117 rdev_for_each(rdev
, mddev
) {
6118 sector_t avail
= rdev
->sectors
;
6120 if (fit
&& (num_sectors
== 0 || num_sectors
> avail
))
6121 num_sectors
= avail
;
6122 if (avail
< num_sectors
)
6125 rv
= mddev
->pers
->resize(mddev
, num_sectors
);
6127 revalidate_disk(mddev
->gendisk
);
6131 static int update_raid_disks(struct mddev
*mddev
, int raid_disks
)
6134 struct md_rdev
*rdev
;
6135 /* change the number of raid disks */
6136 if (mddev
->pers
->check_reshape
== NULL
)
6138 if (raid_disks
<= 0 ||
6139 (mddev
->max_disks
&& raid_disks
>= mddev
->max_disks
))
6141 if (mddev
->sync_thread
|| mddev
->reshape_position
!= MaxSector
)
6144 rdev_for_each(rdev
, mddev
) {
6145 if (mddev
->raid_disks
< raid_disks
&&
6146 rdev
->data_offset
< rdev
->new_data_offset
)
6148 if (mddev
->raid_disks
> raid_disks
&&
6149 rdev
->data_offset
> rdev
->new_data_offset
)
6153 mddev
->delta_disks
= raid_disks
- mddev
->raid_disks
;
6154 if (mddev
->delta_disks
< 0)
6155 mddev
->reshape_backwards
= 1;
6156 else if (mddev
->delta_disks
> 0)
6157 mddev
->reshape_backwards
= 0;
6159 rv
= mddev
->pers
->check_reshape(mddev
);
6161 mddev
->delta_disks
= 0;
6162 mddev
->reshape_backwards
= 0;
6169 * update_array_info is used to change the configuration of an
6171 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
6172 * fields in the info are checked against the array.
6173 * Any differences that cannot be handled will cause an error.
6174 * Normally, only one change can be managed at a time.
6176 static int update_array_info(struct mddev
*mddev
, mdu_array_info_t
*info
)
6182 /* calculate expected state,ignoring low bits */
6183 if (mddev
->bitmap
&& mddev
->bitmap_info
.offset
)
6184 state
|= (1 << MD_SB_BITMAP_PRESENT
);
6186 if (mddev
->major_version
!= info
->major_version
||
6187 mddev
->minor_version
!= info
->minor_version
||
6188 /* mddev->patch_version != info->patch_version || */
6189 mddev
->ctime
!= info
->ctime
||
6190 mddev
->level
!= info
->level
||
6191 /* mddev->layout != info->layout || */
6192 !mddev
->persistent
!= info
->not_persistent
||
6193 mddev
->chunk_sectors
!= info
->chunk_size
>> 9 ||
6194 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
6195 ((state
^info
->state
) & 0xfffffe00)
6198 /* Check there is only one change */
6199 if (info
->size
>= 0 && mddev
->dev_sectors
/ 2 != info
->size
)
6201 if (mddev
->raid_disks
!= info
->raid_disks
)
6203 if (mddev
->layout
!= info
->layout
)
6205 if ((state
^ info
->state
) & (1<<MD_SB_BITMAP_PRESENT
))
6212 if (mddev
->layout
!= info
->layout
) {
6214 * we don't need to do anything at the md level, the
6215 * personality will take care of it all.
6217 if (mddev
->pers
->check_reshape
== NULL
)
6220 mddev
->new_layout
= info
->layout
;
6221 rv
= mddev
->pers
->check_reshape(mddev
);
6223 mddev
->new_layout
= mddev
->layout
;
6227 if (info
->size
>= 0 && mddev
->dev_sectors
/ 2 != info
->size
)
6228 rv
= update_size(mddev
, (sector_t
)info
->size
* 2);
6230 if (mddev
->raid_disks
!= info
->raid_disks
)
6231 rv
= update_raid_disks(mddev
, info
->raid_disks
);
6233 if ((state
^ info
->state
) & (1<<MD_SB_BITMAP_PRESENT
)) {
6234 if (mddev
->pers
->quiesce
== NULL
)
6236 if (mddev
->recovery
|| mddev
->sync_thread
)
6238 if (info
->state
& (1<<MD_SB_BITMAP_PRESENT
)) {
6239 /* add the bitmap */
6242 if (mddev
->bitmap_info
.default_offset
== 0)
6244 mddev
->bitmap_info
.offset
=
6245 mddev
->bitmap_info
.default_offset
;
6246 mddev
->bitmap_info
.space
=
6247 mddev
->bitmap_info
.default_space
;
6248 mddev
->pers
->quiesce(mddev
, 1);
6249 rv
= bitmap_create(mddev
);
6251 rv
= bitmap_load(mddev
);
6253 bitmap_destroy(mddev
);
6254 mddev
->pers
->quiesce(mddev
, 0);
6256 /* remove the bitmap */
6259 if (mddev
->bitmap
->storage
.file
)
6261 mddev
->pers
->quiesce(mddev
, 1);
6262 bitmap_destroy(mddev
);
6263 mddev
->pers
->quiesce(mddev
, 0);
6264 mddev
->bitmap_info
.offset
= 0;
6267 md_update_sb(mddev
, 1);
6271 static int set_disk_faulty(struct mddev
*mddev
, dev_t dev
)
6273 struct md_rdev
*rdev
;
6275 if (mddev
->pers
== NULL
)
6278 rdev
= find_rdev(mddev
, dev
);
6282 md_error(mddev
, rdev
);
6283 if (!test_bit(Faulty
, &rdev
->flags
))
6289 * We have a problem here : there is no easy way to give a CHS
6290 * virtual geometry. We currently pretend that we have a 2 heads
6291 * 4 sectors (with a BIG number of cylinders...). This drives
6292 * dosfs just mad... ;-)
6294 static int md_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
6296 struct mddev
*mddev
= bdev
->bd_disk
->private_data
;
6300 geo
->cylinders
= mddev
->array_sectors
/ 8;
6304 static int md_ioctl(struct block_device
*bdev
, fmode_t mode
,
6305 unsigned int cmd
, unsigned long arg
)
6308 void __user
*argp
= (void __user
*)arg
;
6309 struct mddev
*mddev
= NULL
;
6314 case GET_ARRAY_INFO
:
6318 if (!capable(CAP_SYS_ADMIN
))
6323 * Commands dealing with the RAID driver but not any
6329 err
= get_version(argp
);
6332 case PRINT_RAID_DEBUG
:
6340 autostart_arrays(arg
);
6347 * Commands creating/starting a new array:
6350 mddev
= bdev
->bd_disk
->private_data
;
6357 err
= mddev_lock(mddev
);
6360 "md: ioctl lock interrupted, reason %d, cmd %d\n",
6367 case SET_ARRAY_INFO
:
6369 mdu_array_info_t info
;
6371 memset(&info
, 0, sizeof(info
));
6372 else if (copy_from_user(&info
, argp
, sizeof(info
))) {
6377 err
= update_array_info(mddev
, &info
);
6379 printk(KERN_WARNING
"md: couldn't update"
6380 " array info. %d\n", err
);
6385 if (!list_empty(&mddev
->disks
)) {
6387 "md: array %s already has disks!\n",
6392 if (mddev
->raid_disks
) {
6394 "md: array %s already initialised!\n",
6399 err
= set_array_info(mddev
, &info
);
6401 printk(KERN_WARNING
"md: couldn't set"
6402 " array info. %d\n", err
);
6412 * Commands querying/configuring an existing array:
6414 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
6415 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
6416 if ((!mddev
->raid_disks
&& !mddev
->external
)
6417 && cmd
!= ADD_NEW_DISK
&& cmd
!= STOP_ARRAY
6418 && cmd
!= RUN_ARRAY
&& cmd
!= SET_BITMAP_FILE
6419 && cmd
!= GET_BITMAP_FILE
) {
6425 * Commands even a read-only array can execute:
6429 case GET_ARRAY_INFO
:
6430 err
= get_array_info(mddev
, argp
);
6433 case GET_BITMAP_FILE
:
6434 err
= get_bitmap_file(mddev
, argp
);
6438 err
= get_disk_info(mddev
, argp
);
6441 case RESTART_ARRAY_RW
:
6442 err
= restart_array(mddev
);
6446 err
= do_md_stop(mddev
, 0, bdev
);
6450 err
= md_set_readonly(mddev
, bdev
);
6454 if (get_user(ro
, (int __user
*)(arg
))) {
6460 /* if the bdev is going readonly the value of mddev->ro
6461 * does not matter, no writes are coming
6466 /* are we are already prepared for writes? */
6470 /* transitioning to readauto need only happen for
6471 * arrays that call md_write_start
6474 err
= restart_array(mddev
);
6477 set_disk_ro(mddev
->gendisk
, 0);
6484 * The remaining ioctls are changing the state of the
6485 * superblock, so we do not allow them on read-only arrays.
6486 * However non-MD ioctls (e.g. get-size) will still come through
6487 * here and hit the 'default' below, so only disallow
6488 * 'md' ioctls, and switch to rw mode if started auto-readonly.
6490 if (_IOC_TYPE(cmd
) == MD_MAJOR
&& mddev
->ro
&& mddev
->pers
) {
6491 if (mddev
->ro
== 2) {
6493 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
6494 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6495 md_wakeup_thread(mddev
->thread
);
6506 mdu_disk_info_t info
;
6507 if (copy_from_user(&info
, argp
, sizeof(info
)))
6510 err
= add_new_disk(mddev
, &info
);
6514 case HOT_REMOVE_DISK
:
6515 err
= hot_remove_disk(mddev
, new_decode_dev(arg
));
6519 err
= hot_add_disk(mddev
, new_decode_dev(arg
));
6522 case SET_DISK_FAULTY
:
6523 err
= set_disk_faulty(mddev
, new_decode_dev(arg
));
6527 err
= do_md_run(mddev
);
6530 case SET_BITMAP_FILE
:
6531 err
= set_bitmap_file(mddev
, (int)arg
);
6541 if (mddev
->hold_active
== UNTIL_IOCTL
&&
6543 mddev
->hold_active
= 0;
6544 mddev_unlock(mddev
);
6553 #ifdef CONFIG_COMPAT
6554 static int md_compat_ioctl(struct block_device
*bdev
, fmode_t mode
,
6555 unsigned int cmd
, unsigned long arg
)
6558 case HOT_REMOVE_DISK
:
6560 case SET_DISK_FAULTY
:
6561 case SET_BITMAP_FILE
:
6562 /* These take in integer arg, do not convert */
6565 arg
= (unsigned long)compat_ptr(arg
);
6569 return md_ioctl(bdev
, mode
, cmd
, arg
);
6571 #endif /* CONFIG_COMPAT */
6573 static int md_open(struct block_device
*bdev
, fmode_t mode
)
6576 * Succeed if we can lock the mddev, which confirms that
6577 * it isn't being stopped right now.
6579 struct mddev
*mddev
= mddev_find(bdev
->bd_dev
);
6585 if (mddev
->gendisk
!= bdev
->bd_disk
) {
6586 /* we are racing with mddev_put which is discarding this
6590 /* Wait until bdev->bd_disk is definitely gone */
6591 flush_workqueue(md_misc_wq
);
6592 /* Then retry the open from the top */
6593 return -ERESTARTSYS
;
6595 BUG_ON(mddev
!= bdev
->bd_disk
->private_data
);
6597 if ((err
= mutex_lock_interruptible(&mddev
->open_mutex
)))
6601 atomic_inc(&mddev
->openers
);
6602 mutex_unlock(&mddev
->open_mutex
);
6604 check_disk_change(bdev
);
6609 static int md_release(struct gendisk
*disk
, fmode_t mode
)
6611 struct mddev
*mddev
= disk
->private_data
;
6614 atomic_dec(&mddev
->openers
);
6620 static int md_media_changed(struct gendisk
*disk
)
6622 struct mddev
*mddev
= disk
->private_data
;
6624 return mddev
->changed
;
6627 static int md_revalidate(struct gendisk
*disk
)
6629 struct mddev
*mddev
= disk
->private_data
;
6634 static const struct block_device_operations md_fops
=
6636 .owner
= THIS_MODULE
,
6638 .release
= md_release
,
6640 #ifdef CONFIG_COMPAT
6641 .compat_ioctl
= md_compat_ioctl
,
6643 .getgeo
= md_getgeo
,
6644 .media_changed
= md_media_changed
,
6645 .revalidate_disk
= md_revalidate
,
6648 static int md_thread(void * arg
)
6650 struct md_thread
*thread
= arg
;
6653 * md_thread is a 'system-thread', it's priority should be very
6654 * high. We avoid resource deadlocks individually in each
6655 * raid personality. (RAID5 does preallocation) We also use RR and
6656 * the very same RT priority as kswapd, thus we will never get
6657 * into a priority inversion deadlock.
6659 * we definitely have to have equal or higher priority than
6660 * bdflush, otherwise bdflush will deadlock if there are too
6661 * many dirty RAID5 blocks.
6664 allow_signal(SIGKILL
);
6665 while (!kthread_should_stop()) {
6667 /* We need to wait INTERRUPTIBLE so that
6668 * we don't add to the load-average.
6669 * That means we need to be sure no signals are
6672 if (signal_pending(current
))
6673 flush_signals(current
);
6675 wait_event_interruptible_timeout
6677 test_bit(THREAD_WAKEUP
, &thread
->flags
)
6678 || kthread_should_stop(),
6681 clear_bit(THREAD_WAKEUP
, &thread
->flags
);
6682 if (!kthread_should_stop())
6683 thread
->run(thread
->mddev
);
6689 void md_wakeup_thread(struct md_thread
*thread
)
6692 pr_debug("md: waking up MD thread %s.\n", thread
->tsk
->comm
);
6693 set_bit(THREAD_WAKEUP
, &thread
->flags
);
6694 wake_up(&thread
->wqueue
);
6698 struct md_thread
*md_register_thread(void (*run
) (struct mddev
*), struct mddev
*mddev
,
6701 struct md_thread
*thread
;
6703 thread
= kzalloc(sizeof(struct md_thread
), GFP_KERNEL
);
6707 init_waitqueue_head(&thread
->wqueue
);
6710 thread
->mddev
= mddev
;
6711 thread
->timeout
= MAX_SCHEDULE_TIMEOUT
;
6712 thread
->tsk
= kthread_run(md_thread
, thread
,
6714 mdname(thread
->mddev
),
6716 if (IS_ERR(thread
->tsk
)) {
6723 void md_unregister_thread(struct md_thread
**threadp
)
6725 struct md_thread
*thread
= *threadp
;
6728 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread
->tsk
));
6729 /* Locking ensures that mddev_unlock does not wake_up a
6730 * non-existent thread
6732 spin_lock(&pers_lock
);
6734 spin_unlock(&pers_lock
);
6736 kthread_stop(thread
->tsk
);
6740 void md_error(struct mddev
*mddev
, struct md_rdev
*rdev
)
6747 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
))
6750 if (!mddev
->pers
|| !mddev
->pers
->error_handler
)
6752 mddev
->pers
->error_handler(mddev
,rdev
);
6753 if (mddev
->degraded
)
6754 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
6755 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
6756 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6757 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6758 md_wakeup_thread(mddev
->thread
);
6759 if (mddev
->event_work
.func
)
6760 queue_work(md_misc_wq
, &mddev
->event_work
);
6761 md_new_event_inintr(mddev
);
6764 /* seq_file implementation /proc/mdstat */
6766 static void status_unused(struct seq_file
*seq
)
6769 struct md_rdev
*rdev
;
6771 seq_printf(seq
, "unused devices: ");
6773 list_for_each_entry(rdev
, &pending_raid_disks
, same_set
) {
6774 char b
[BDEVNAME_SIZE
];
6776 seq_printf(seq
, "%s ",
6777 bdevname(rdev
->bdev
,b
));
6780 seq_printf(seq
, "<none>");
6782 seq_printf(seq
, "\n");
6786 static void status_resync(struct seq_file
*seq
, struct mddev
* mddev
)
6788 sector_t max_sectors
, resync
, res
;
6789 unsigned long dt
, db
;
6792 unsigned int per_milli
;
6794 resync
= mddev
->curr_resync
- atomic_read(&mddev
->recovery_active
);
6796 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ||
6797 test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
6798 max_sectors
= mddev
->resync_max_sectors
;
6800 max_sectors
= mddev
->dev_sectors
;
6803 * Should not happen.
6809 /* Pick 'scale' such that (resync>>scale)*1000 will fit
6810 * in a sector_t, and (max_sectors>>scale) will fit in a
6811 * u32, as those are the requirements for sector_div.
6812 * Thus 'scale' must be at least 10
6815 if (sizeof(sector_t
) > sizeof(unsigned long)) {
6816 while ( max_sectors
/2 > (1ULL<<(scale
+32)))
6819 res
= (resync
>>scale
)*1000;
6820 sector_div(res
, (u32
)((max_sectors
>>scale
)+1));
6824 int i
, x
= per_milli
/50, y
= 20-x
;
6825 seq_printf(seq
, "[");
6826 for (i
= 0; i
< x
; i
++)
6827 seq_printf(seq
, "=");
6828 seq_printf(seq
, ">");
6829 for (i
= 0; i
< y
; i
++)
6830 seq_printf(seq
, ".");
6831 seq_printf(seq
, "] ");
6833 seq_printf(seq
, " %s =%3u.%u%% (%llu/%llu)",
6834 (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
)?
6836 (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
)?
6838 (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ?
6839 "resync" : "recovery"))),
6840 per_milli
/10, per_milli
% 10,
6841 (unsigned long long) resync
/2,
6842 (unsigned long long) max_sectors
/2);
6845 * dt: time from mark until now
6846 * db: blocks written from mark until now
6847 * rt: remaining time
6849 * rt is a sector_t, so could be 32bit or 64bit.
6850 * So we divide before multiply in case it is 32bit and close
6852 * We scale the divisor (db) by 32 to avoid losing precision
6853 * near the end of resync when the number of remaining sectors
6855 * We then divide rt by 32 after multiplying by db to compensate.
6856 * The '+1' avoids division by zero if db is very small.
6858 dt
= ((jiffies
- mddev
->resync_mark
) / HZ
);
6860 db
= (mddev
->curr_mark_cnt
- atomic_read(&mddev
->recovery_active
))
6861 - mddev
->resync_mark_cnt
;
6863 rt
= max_sectors
- resync
; /* number of remaining sectors */
6864 sector_div(rt
, db
/32+1);
6868 seq_printf(seq
, " finish=%lu.%lumin", (unsigned long)rt
/ 60,
6869 ((unsigned long)rt
% 60)/6);
6871 seq_printf(seq
, " speed=%ldK/sec", db
/2/dt
);
6874 static void *md_seq_start(struct seq_file
*seq
, loff_t
*pos
)
6876 struct list_head
*tmp
;
6878 struct mddev
*mddev
;
6886 spin_lock(&all_mddevs_lock
);
6887 list_for_each(tmp
,&all_mddevs
)
6889 mddev
= list_entry(tmp
, struct mddev
, all_mddevs
);
6891 spin_unlock(&all_mddevs_lock
);
6894 spin_unlock(&all_mddevs_lock
);
6896 return (void*)2;/* tail */
6900 static void *md_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
6902 struct list_head
*tmp
;
6903 struct mddev
*next_mddev
, *mddev
= v
;
6909 spin_lock(&all_mddevs_lock
);
6911 tmp
= all_mddevs
.next
;
6913 tmp
= mddev
->all_mddevs
.next
;
6914 if (tmp
!= &all_mddevs
)
6915 next_mddev
= mddev_get(list_entry(tmp
,struct mddev
,all_mddevs
));
6917 next_mddev
= (void*)2;
6920 spin_unlock(&all_mddevs_lock
);
6928 static void md_seq_stop(struct seq_file
*seq
, void *v
)
6930 struct mddev
*mddev
= v
;
6932 if (mddev
&& v
!= (void*)1 && v
!= (void*)2)
6936 static int md_seq_show(struct seq_file
*seq
, void *v
)
6938 struct mddev
*mddev
= v
;
6940 struct md_rdev
*rdev
;
6942 if (v
== (void*)1) {
6943 struct md_personality
*pers
;
6944 seq_printf(seq
, "Personalities : ");
6945 spin_lock(&pers_lock
);
6946 list_for_each_entry(pers
, &pers_list
, list
)
6947 seq_printf(seq
, "[%s] ", pers
->name
);
6949 spin_unlock(&pers_lock
);
6950 seq_printf(seq
, "\n");
6951 seq
->poll_event
= atomic_read(&md_event_count
);
6954 if (v
== (void*)2) {
6959 if (mddev_lock(mddev
) < 0)
6962 if (mddev
->pers
|| mddev
->raid_disks
|| !list_empty(&mddev
->disks
)) {
6963 seq_printf(seq
, "%s : %sactive", mdname(mddev
),
6964 mddev
->pers
? "" : "in");
6967 seq_printf(seq
, " (read-only)");
6969 seq_printf(seq
, " (auto-read-only)");
6970 seq_printf(seq
, " %s", mddev
->pers
->name
);
6974 rdev_for_each(rdev
, mddev
) {
6975 char b
[BDEVNAME_SIZE
];
6976 seq_printf(seq
, " %s[%d]",
6977 bdevname(rdev
->bdev
,b
), rdev
->desc_nr
);
6978 if (test_bit(WriteMostly
, &rdev
->flags
))
6979 seq_printf(seq
, "(W)");
6980 if (test_bit(Faulty
, &rdev
->flags
)) {
6981 seq_printf(seq
, "(F)");
6984 if (rdev
->raid_disk
< 0)
6985 seq_printf(seq
, "(S)"); /* spare */
6986 if (test_bit(Replacement
, &rdev
->flags
))
6987 seq_printf(seq
, "(R)");
6988 sectors
+= rdev
->sectors
;
6991 if (!list_empty(&mddev
->disks
)) {
6993 seq_printf(seq
, "\n %llu blocks",
6994 (unsigned long long)
6995 mddev
->array_sectors
/ 2);
6997 seq_printf(seq
, "\n %llu blocks",
6998 (unsigned long long)sectors
/ 2);
7000 if (mddev
->persistent
) {
7001 if (mddev
->major_version
!= 0 ||
7002 mddev
->minor_version
!= 90) {
7003 seq_printf(seq
," super %d.%d",
7004 mddev
->major_version
,
7005 mddev
->minor_version
);
7007 } else if (mddev
->external
)
7008 seq_printf(seq
, " super external:%s",
7009 mddev
->metadata_type
);
7011 seq_printf(seq
, " super non-persistent");
7014 mddev
->pers
->status(seq
, mddev
);
7015 seq_printf(seq
, "\n ");
7016 if (mddev
->pers
->sync_request
) {
7017 if (mddev
->curr_resync
> 2) {
7018 status_resync(seq
, mddev
);
7019 seq_printf(seq
, "\n ");
7020 } else if (mddev
->curr_resync
== 1 || mddev
->curr_resync
== 2)
7021 seq_printf(seq
, "\tresync=DELAYED\n ");
7022 else if (mddev
->recovery_cp
< MaxSector
)
7023 seq_printf(seq
, "\tresync=PENDING\n ");
7026 seq_printf(seq
, "\n ");
7028 bitmap_status(seq
, mddev
->bitmap
);
7030 seq_printf(seq
, "\n");
7032 mddev_unlock(mddev
);
7037 static const struct seq_operations md_seq_ops
= {
7038 .start
= md_seq_start
,
7039 .next
= md_seq_next
,
7040 .stop
= md_seq_stop
,
7041 .show
= md_seq_show
,
7044 static int md_seq_open(struct inode
*inode
, struct file
*file
)
7046 struct seq_file
*seq
;
7049 error
= seq_open(file
, &md_seq_ops
);
7053 seq
= file
->private_data
;
7054 seq
->poll_event
= atomic_read(&md_event_count
);
7058 static unsigned int mdstat_poll(struct file
*filp
, poll_table
*wait
)
7060 struct seq_file
*seq
= filp
->private_data
;
7063 poll_wait(filp
, &md_event_waiters
, wait
);
7065 /* always allow read */
7066 mask
= POLLIN
| POLLRDNORM
;
7068 if (seq
->poll_event
!= atomic_read(&md_event_count
))
7069 mask
|= POLLERR
| POLLPRI
;
7073 static const struct file_operations md_seq_fops
= {
7074 .owner
= THIS_MODULE
,
7075 .open
= md_seq_open
,
7077 .llseek
= seq_lseek
,
7078 .release
= seq_release_private
,
7079 .poll
= mdstat_poll
,
7082 int register_md_personality(struct md_personality
*p
)
7084 spin_lock(&pers_lock
);
7085 list_add_tail(&p
->list
, &pers_list
);
7086 printk(KERN_INFO
"md: %s personality registered for level %d\n", p
->name
, p
->level
);
7087 spin_unlock(&pers_lock
);
7091 int unregister_md_personality(struct md_personality
*p
)
7093 printk(KERN_INFO
"md: %s personality unregistered\n", p
->name
);
7094 spin_lock(&pers_lock
);
7095 list_del_init(&p
->list
);
7096 spin_unlock(&pers_lock
);
7100 static int is_mddev_idle(struct mddev
*mddev
, int init
)
7102 struct md_rdev
* rdev
;
7108 rdev_for_each_rcu(rdev
, mddev
) {
7109 struct gendisk
*disk
= rdev
->bdev
->bd_contains
->bd_disk
;
7110 curr_events
= (int)part_stat_read(&disk
->part0
, sectors
[0]) +
7111 (int)part_stat_read(&disk
->part0
, sectors
[1]) -
7112 atomic_read(&disk
->sync_io
);
7113 /* sync IO will cause sync_io to increase before the disk_stats
7114 * as sync_io is counted when a request starts, and
7115 * disk_stats is counted when it completes.
7116 * So resync activity will cause curr_events to be smaller than
7117 * when there was no such activity.
7118 * non-sync IO will cause disk_stat to increase without
7119 * increasing sync_io so curr_events will (eventually)
7120 * be larger than it was before. Once it becomes
7121 * substantially larger, the test below will cause
7122 * the array to appear non-idle, and resync will slow
7124 * If there is a lot of outstanding resync activity when
7125 * we set last_event to curr_events, then all that activity
7126 * completing might cause the array to appear non-idle
7127 * and resync will be slowed down even though there might
7128 * not have been non-resync activity. This will only
7129 * happen once though. 'last_events' will soon reflect
7130 * the state where there is little or no outstanding
7131 * resync requests, and further resync activity will
7132 * always make curr_events less than last_events.
7135 if (init
|| curr_events
- rdev
->last_events
> 64) {
7136 rdev
->last_events
= curr_events
;
7144 void md_done_sync(struct mddev
*mddev
, int blocks
, int ok
)
7146 /* another "blocks" (512byte) blocks have been synced */
7147 atomic_sub(blocks
, &mddev
->recovery_active
);
7148 wake_up(&mddev
->recovery_wait
);
7150 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
7151 md_wakeup_thread(mddev
->thread
);
7152 // stop recovery, signal do_sync ....
7157 /* md_write_start(mddev, bi)
7158 * If we need to update some array metadata (e.g. 'active' flag
7159 * in superblock) before writing, schedule a superblock update
7160 * and wait for it to complete.
7162 void md_write_start(struct mddev
*mddev
, struct bio
*bi
)
7165 if (bio_data_dir(bi
) != WRITE
)
7168 BUG_ON(mddev
->ro
== 1);
7169 if (mddev
->ro
== 2) {
7170 /* need to switch to read/write */
7172 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7173 md_wakeup_thread(mddev
->thread
);
7174 md_wakeup_thread(mddev
->sync_thread
);
7177 atomic_inc(&mddev
->writes_pending
);
7178 if (mddev
->safemode
== 1)
7179 mddev
->safemode
= 0;
7180 if (mddev
->in_sync
) {
7181 spin_lock_irq(&mddev
->write_lock
);
7182 if (mddev
->in_sync
) {
7184 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
7185 set_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
7186 md_wakeup_thread(mddev
->thread
);
7189 spin_unlock_irq(&mddev
->write_lock
);
7192 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
7193 wait_event(mddev
->sb_wait
,
7194 !test_bit(MD_CHANGE_PENDING
, &mddev
->flags
));
7197 void md_write_end(struct mddev
*mddev
)
7199 if (atomic_dec_and_test(&mddev
->writes_pending
)) {
7200 if (mddev
->safemode
== 2)
7201 md_wakeup_thread(mddev
->thread
);
7202 else if (mddev
->safemode_delay
)
7203 mod_timer(&mddev
->safemode_timer
, jiffies
+ mddev
->safemode_delay
);
7207 /* md_allow_write(mddev)
7208 * Calling this ensures that the array is marked 'active' so that writes
7209 * may proceed without blocking. It is important to call this before
7210 * attempting a GFP_KERNEL allocation while holding the mddev lock.
7211 * Must be called with mddev_lock held.
7213 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
7214 * is dropped, so return -EAGAIN after notifying userspace.
7216 int md_allow_write(struct mddev
*mddev
)
7222 if (!mddev
->pers
->sync_request
)
7225 spin_lock_irq(&mddev
->write_lock
);
7226 if (mddev
->in_sync
) {
7228 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
7229 set_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
7230 if (mddev
->safemode_delay
&&
7231 mddev
->safemode
== 0)
7232 mddev
->safemode
= 1;
7233 spin_unlock_irq(&mddev
->write_lock
);
7234 md_update_sb(mddev
, 0);
7235 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
7237 spin_unlock_irq(&mddev
->write_lock
);
7239 if (test_bit(MD_CHANGE_PENDING
, &mddev
->flags
))
7244 EXPORT_SYMBOL_GPL(md_allow_write
);
7246 #define SYNC_MARKS 10
7247 #define SYNC_MARK_STEP (3*HZ)
7248 void md_do_sync(struct mddev
*mddev
)
7250 struct mddev
*mddev2
;
7251 unsigned int currspeed
= 0,
7253 sector_t max_sectors
,j
, io_sectors
;
7254 unsigned long mark
[SYNC_MARKS
];
7255 sector_t mark_cnt
[SYNC_MARKS
];
7257 struct list_head
*tmp
;
7258 sector_t last_check
;
7260 struct md_rdev
*rdev
;
7262 struct blk_plug plug
;
7264 /* just incase thread restarts... */
7265 if (test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
))
7267 if (mddev
->ro
) /* never try to sync a read-only array */
7270 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
7271 if (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
))
7272 desc
= "data-check";
7273 else if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
7274 desc
= "requested-resync";
7277 } else if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
7282 /* we overload curr_resync somewhat here.
7283 * 0 == not engaged in resync at all
7284 * 2 == checking that there is no conflict with another sync
7285 * 1 == like 2, but have yielded to allow conflicting resync to
7287 * other == active in resync - this many blocks
7289 * Before starting a resync we must have set curr_resync to
7290 * 2, and then checked that every "conflicting" array has curr_resync
7291 * less than ours. When we find one that is the same or higher
7292 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
7293 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
7294 * This will mean we have to start checking from the beginning again.
7299 mddev
->curr_resync
= 2;
7302 if (kthread_should_stop())
7303 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
7305 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
7307 for_each_mddev(mddev2
, tmp
) {
7308 if (mddev2
== mddev
)
7310 if (!mddev
->parallel_resync
7311 && mddev2
->curr_resync
7312 && match_mddev_units(mddev
, mddev2
)) {
7314 if (mddev
< mddev2
&& mddev
->curr_resync
== 2) {
7315 /* arbitrarily yield */
7316 mddev
->curr_resync
= 1;
7317 wake_up(&resync_wait
);
7319 if (mddev
> mddev2
&& mddev
->curr_resync
== 1)
7320 /* no need to wait here, we can wait the next
7321 * time 'round when curr_resync == 2
7324 /* We need to wait 'interruptible' so as not to
7325 * contribute to the load average, and not to
7326 * be caught by 'softlockup'
7328 prepare_to_wait(&resync_wait
, &wq
, TASK_INTERRUPTIBLE
);
7329 if (!kthread_should_stop() &&
7330 mddev2
->curr_resync
>= mddev
->curr_resync
) {
7331 printk(KERN_INFO
"md: delaying %s of %s"
7332 " until %s has finished (they"
7333 " share one or more physical units)\n",
7334 desc
, mdname(mddev
), mdname(mddev2
));
7336 if (signal_pending(current
))
7337 flush_signals(current
);
7339 finish_wait(&resync_wait
, &wq
);
7342 finish_wait(&resync_wait
, &wq
);
7345 } while (mddev
->curr_resync
< 2);
7348 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
7349 /* resync follows the size requested by the personality,
7350 * which defaults to physical size, but can be virtual size
7352 max_sectors
= mddev
->resync_max_sectors
;
7353 mddev
->resync_mismatches
= 0;
7354 /* we don't use the checkpoint if there's a bitmap */
7355 if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
7356 j
= mddev
->resync_min
;
7357 else if (!mddev
->bitmap
)
7358 j
= mddev
->recovery_cp
;
7360 } else if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
7361 max_sectors
= mddev
->resync_max_sectors
;
7363 /* recovery follows the physical size of devices */
7364 max_sectors
= mddev
->dev_sectors
;
7367 rdev_for_each_rcu(rdev
, mddev
)
7368 if (rdev
->raid_disk
>= 0 &&
7369 !test_bit(Faulty
, &rdev
->flags
) &&
7370 !test_bit(In_sync
, &rdev
->flags
) &&
7371 rdev
->recovery_offset
< j
)
7372 j
= rdev
->recovery_offset
;
7376 printk(KERN_INFO
"md: %s of RAID array %s\n", desc
, mdname(mddev
));
7377 printk(KERN_INFO
"md: minimum _guaranteed_ speed:"
7378 " %d KB/sec/disk.\n", speed_min(mddev
));
7379 printk(KERN_INFO
"md: using maximum available idle IO bandwidth "
7380 "(but not more than %d KB/sec) for %s.\n",
7381 speed_max(mddev
), desc
);
7383 is_mddev_idle(mddev
, 1); /* this initializes IO event counters */
7386 for (m
= 0; m
< SYNC_MARKS
; m
++) {
7388 mark_cnt
[m
] = io_sectors
;
7391 mddev
->resync_mark
= mark
[last_mark
];
7392 mddev
->resync_mark_cnt
= mark_cnt
[last_mark
];
7395 * Tune reconstruction:
7397 window
= 32*(PAGE_SIZE
/512);
7398 printk(KERN_INFO
"md: using %dk window, over a total of %lluk.\n",
7399 window
/2, (unsigned long long)max_sectors
/2);
7401 atomic_set(&mddev
->recovery_active
, 0);
7406 "md: resuming %s of %s from checkpoint.\n",
7407 desc
, mdname(mddev
));
7408 mddev
->curr_resync
= j
;
7410 mddev
->curr_resync_completed
= j
;
7412 blk_start_plug(&plug
);
7413 while (j
< max_sectors
) {
7418 if (!test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
7419 ((mddev
->curr_resync
> mddev
->curr_resync_completed
&&
7420 (mddev
->curr_resync
- mddev
->curr_resync_completed
)
7421 > (max_sectors
>> 4)) ||
7422 (j
- mddev
->curr_resync_completed
)*2
7423 >= mddev
->resync_max
- mddev
->curr_resync_completed
7425 /* time to update curr_resync_completed */
7426 wait_event(mddev
->recovery_wait
,
7427 atomic_read(&mddev
->recovery_active
) == 0);
7428 mddev
->curr_resync_completed
= j
;
7429 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
7430 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
7433 while (j
>= mddev
->resync_max
&& !kthread_should_stop()) {
7434 /* As this condition is controlled by user-space,
7435 * we can block indefinitely, so use '_interruptible'
7436 * to avoid triggering warnings.
7438 flush_signals(current
); /* just in case */
7439 wait_event_interruptible(mddev
->recovery_wait
,
7440 mddev
->resync_max
> j
7441 || kthread_should_stop());
7444 if (kthread_should_stop())
7447 sectors
= mddev
->pers
->sync_request(mddev
, j
, &skipped
,
7448 currspeed
< speed_min(mddev
));
7450 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
7454 if (!skipped
) { /* actual IO requested */
7455 io_sectors
+= sectors
;
7456 atomic_add(sectors
, &mddev
->recovery_active
);
7459 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
7463 if (j
>1) mddev
->curr_resync
= j
;
7464 mddev
->curr_mark_cnt
= io_sectors
;
7465 if (last_check
== 0)
7466 /* this is the earliest that rebuild will be
7467 * visible in /proc/mdstat
7469 md_new_event(mddev
);
7471 if (last_check
+ window
> io_sectors
|| j
== max_sectors
)
7474 last_check
= io_sectors
;
7476 if (time_after_eq(jiffies
, mark
[last_mark
] + SYNC_MARK_STEP
)) {
7478 int next
= (last_mark
+1) % SYNC_MARKS
;
7480 mddev
->resync_mark
= mark
[next
];
7481 mddev
->resync_mark_cnt
= mark_cnt
[next
];
7482 mark
[next
] = jiffies
;
7483 mark_cnt
[next
] = io_sectors
- atomic_read(&mddev
->recovery_active
);
7488 if (kthread_should_stop())
7493 * this loop exits only if either when we are slower than
7494 * the 'hard' speed limit, or the system was IO-idle for
7496 * the system might be non-idle CPU-wise, but we only care
7497 * about not overloading the IO subsystem. (things like an
7498 * e2fsck being done on the RAID array should execute fast)
7502 currspeed
= ((unsigned long)(io_sectors
-mddev
->resync_mark_cnt
))/2
7503 /((jiffies
-mddev
->resync_mark
)/HZ
+1) +1;
7505 if (currspeed
> speed_min(mddev
)) {
7506 if ((currspeed
> speed_max(mddev
)) ||
7507 !is_mddev_idle(mddev
, 0)) {
7513 printk(KERN_INFO
"md: %s: %s done.\n",mdname(mddev
), desc
);
7515 * this also signals 'finished resyncing' to md_stop
7518 blk_finish_plug(&plug
);
7519 wait_event(mddev
->recovery_wait
, !atomic_read(&mddev
->recovery_active
));
7521 /* tell personality that we are finished */
7522 mddev
->pers
->sync_request(mddev
, max_sectors
, &skipped
, 1);
7524 if (!test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
) &&
7525 mddev
->curr_resync
> 2) {
7526 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
7527 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
7528 if (mddev
->curr_resync
>= mddev
->recovery_cp
) {
7530 "md: checkpointing %s of %s.\n",
7531 desc
, mdname(mddev
));
7532 mddev
->recovery_cp
=
7533 mddev
->curr_resync_completed
;
7536 mddev
->recovery_cp
= MaxSector
;
7538 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
7539 mddev
->curr_resync
= MaxSector
;
7541 rdev_for_each_rcu(rdev
, mddev
)
7542 if (rdev
->raid_disk
>= 0 &&
7543 mddev
->delta_disks
>= 0 &&
7544 !test_bit(Faulty
, &rdev
->flags
) &&
7545 !test_bit(In_sync
, &rdev
->flags
) &&
7546 rdev
->recovery_offset
< mddev
->curr_resync
)
7547 rdev
->recovery_offset
= mddev
->curr_resync
;
7552 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
7554 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
7555 /* We completed so min/max setting can be forgotten if used. */
7556 if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
7557 mddev
->resync_min
= 0;
7558 mddev
->resync_max
= MaxSector
;
7559 } else if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
7560 mddev
->resync_min
= mddev
->curr_resync_completed
;
7561 mddev
->curr_resync
= 0;
7562 wake_up(&resync_wait
);
7563 set_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
7564 md_wakeup_thread(mddev
->thread
);
7569 * got a signal, exit.
7572 "md: md_do_sync() got signal ... exiting\n");
7573 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
7577 EXPORT_SYMBOL_GPL(md_do_sync
);
7579 static int remove_and_add_spares(struct mddev
*mddev
)
7581 struct md_rdev
*rdev
;
7585 mddev
->curr_resync_completed
= 0;
7587 rdev_for_each(rdev
, mddev
)
7588 if (rdev
->raid_disk
>= 0 &&
7589 !test_bit(Blocked
, &rdev
->flags
) &&
7590 (test_bit(Faulty
, &rdev
->flags
) ||
7591 ! test_bit(In_sync
, &rdev
->flags
)) &&
7592 atomic_read(&rdev
->nr_pending
)==0) {
7593 if (mddev
->pers
->hot_remove_disk(
7594 mddev
, rdev
) == 0) {
7595 sysfs_unlink_rdev(mddev
, rdev
);
7596 rdev
->raid_disk
= -1;
7601 sysfs_notify(&mddev
->kobj
, NULL
,
7605 rdev_for_each(rdev
, mddev
) {
7606 if (rdev
->raid_disk
>= 0 &&
7607 !test_bit(In_sync
, &rdev
->flags
) &&
7608 !test_bit(Faulty
, &rdev
->flags
))
7610 if (rdev
->raid_disk
< 0
7611 && !test_bit(Faulty
, &rdev
->flags
)) {
7612 rdev
->recovery_offset
= 0;
7614 hot_add_disk(mddev
, rdev
) == 0) {
7615 if (sysfs_link_rdev(mddev
, rdev
))
7616 /* failure here is OK */;
7618 md_new_event(mddev
);
7619 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
7626 static void reap_sync_thread(struct mddev
*mddev
)
7628 struct md_rdev
*rdev
;
7630 /* resync has finished, collect result */
7631 md_unregister_thread(&mddev
->sync_thread
);
7632 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
) &&
7633 !test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
)) {
7635 /* activate any spares */
7636 if (mddev
->pers
->spare_active(mddev
))
7637 sysfs_notify(&mddev
->kobj
, NULL
,
7640 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
7641 mddev
->pers
->finish_reshape
)
7642 mddev
->pers
->finish_reshape(mddev
);
7644 /* If array is no-longer degraded, then any saved_raid_disk
7645 * information must be scrapped. Also if any device is now
7646 * In_sync we must scrape the saved_raid_disk for that device
7647 * do the superblock for an incrementally recovered device
7650 rdev_for_each(rdev
, mddev
)
7651 if (!mddev
->degraded
||
7652 test_bit(In_sync
, &rdev
->flags
))
7653 rdev
->saved_raid_disk
= -1;
7655 md_update_sb(mddev
, 1);
7656 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
7657 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
7658 clear_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
7659 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
7660 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
7661 /* flag recovery needed just to double check */
7662 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7663 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
7664 md_new_event(mddev
);
7665 if (mddev
->event_work
.func
)
7666 queue_work(md_misc_wq
, &mddev
->event_work
);
7670 * This routine is regularly called by all per-raid-array threads to
7671 * deal with generic issues like resync and super-block update.
7672 * Raid personalities that don't have a thread (linear/raid0) do not
7673 * need this as they never do any recovery or update the superblock.
7675 * It does not do any resync itself, but rather "forks" off other threads
7676 * to do that as needed.
7677 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
7678 * "->recovery" and create a thread at ->sync_thread.
7679 * When the thread finishes it sets MD_RECOVERY_DONE
7680 * and wakeups up this thread which will reap the thread and finish up.
7681 * This thread also removes any faulty devices (with nr_pending == 0).
7683 * The overall approach is:
7684 * 1/ if the superblock needs updating, update it.
7685 * 2/ If a recovery thread is running, don't do anything else.
7686 * 3/ If recovery has finished, clean up, possibly marking spares active.
7687 * 4/ If there are any faulty devices, remove them.
7688 * 5/ If array is degraded, try to add spares devices
7689 * 6/ If array has spares or is not in-sync, start a resync thread.
7691 void md_check_recovery(struct mddev
*mddev
)
7693 if (mddev
->suspended
)
7697 bitmap_daemon_work(mddev
);
7699 if (signal_pending(current
)) {
7700 if (mddev
->pers
->sync_request
&& !mddev
->external
) {
7701 printk(KERN_INFO
"md: %s in immediate safe mode\n",
7703 mddev
->safemode
= 2;
7705 flush_signals(current
);
7708 if (mddev
->ro
&& !test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))
7711 (mddev
->flags
& ~ (1<<MD_CHANGE_PENDING
)) ||
7712 test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
) ||
7713 test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
) ||
7714 (mddev
->external
== 0 && mddev
->safemode
== 1) ||
7715 (mddev
->safemode
== 2 && ! atomic_read(&mddev
->writes_pending
)
7716 && !mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
)
7720 if (mddev_trylock(mddev
)) {
7724 /* Only thing we do on a ro array is remove
7727 struct md_rdev
*rdev
;
7728 rdev_for_each(rdev
, mddev
)
7729 if (rdev
->raid_disk
>= 0 &&
7730 !test_bit(Blocked
, &rdev
->flags
) &&
7731 test_bit(Faulty
, &rdev
->flags
) &&
7732 atomic_read(&rdev
->nr_pending
)==0) {
7733 if (mddev
->pers
->hot_remove_disk(
7734 mddev
, rdev
) == 0) {
7735 sysfs_unlink_rdev(mddev
, rdev
);
7736 rdev
->raid_disk
= -1;
7739 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7743 if (!mddev
->external
) {
7745 spin_lock_irq(&mddev
->write_lock
);
7746 if (mddev
->safemode
&&
7747 !atomic_read(&mddev
->writes_pending
) &&
7749 mddev
->recovery_cp
== MaxSector
) {
7752 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
7754 if (mddev
->safemode
== 1)
7755 mddev
->safemode
= 0;
7756 spin_unlock_irq(&mddev
->write_lock
);
7758 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
7762 md_update_sb(mddev
, 0);
7764 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) &&
7765 !test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
)) {
7766 /* resync/recovery still happening */
7767 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7770 if (mddev
->sync_thread
) {
7771 reap_sync_thread(mddev
);
7774 /* Set RUNNING before clearing NEEDED to avoid
7775 * any transients in the value of "sync_action".
7777 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
7778 /* Clear some bits that don't mean anything, but
7781 clear_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
7782 clear_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
7784 if (!test_and_clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
) ||
7785 test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
))
7787 /* no recovery is running.
7788 * remove any failed drives, then
7789 * add spares if possible.
7790 * Spare are also removed and re-added, to allow
7791 * the personality to fail the re-add.
7794 if (mddev
->reshape_position
!= MaxSector
) {
7795 if (mddev
->pers
->check_reshape
== NULL
||
7796 mddev
->pers
->check_reshape(mddev
) != 0)
7797 /* Cannot proceed */
7799 set_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
7800 clear_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
7801 } else if ((spares
= remove_and_add_spares(mddev
))) {
7802 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
7803 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
7804 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
7805 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
7806 } else if (mddev
->recovery_cp
< MaxSector
) {
7807 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
7808 clear_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
7809 } else if (!test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
7810 /* nothing to be done ... */
7813 if (mddev
->pers
->sync_request
) {
7815 /* We are adding a device or devices to an array
7816 * which has the bitmap stored on all devices.
7817 * So make sure all bitmap pages get written
7819 bitmap_write_all(mddev
->bitmap
);
7821 mddev
->sync_thread
= md_register_thread(md_do_sync
,
7824 if (!mddev
->sync_thread
) {
7825 printk(KERN_ERR
"%s: could not start resync"
7828 /* leave the spares where they are, it shouldn't hurt */
7829 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
7830 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
7831 clear_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
7832 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
7833 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
7835 md_wakeup_thread(mddev
->sync_thread
);
7836 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
7837 md_new_event(mddev
);
7840 if (!mddev
->sync_thread
) {
7841 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
7842 if (test_and_clear_bit(MD_RECOVERY_RECOVER
,
7844 if (mddev
->sysfs_action
)
7845 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
7847 mddev_unlock(mddev
);
7851 void md_wait_for_blocked_rdev(struct md_rdev
*rdev
, struct mddev
*mddev
)
7853 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
7854 wait_event_timeout(rdev
->blocked_wait
,
7855 !test_bit(Blocked
, &rdev
->flags
) &&
7856 !test_bit(BlockedBadBlocks
, &rdev
->flags
),
7857 msecs_to_jiffies(5000));
7858 rdev_dec_pending(rdev
, mddev
);
7860 EXPORT_SYMBOL(md_wait_for_blocked_rdev
);
7862 void md_finish_reshape(struct mddev
*mddev
)
7864 /* called be personality module when reshape completes. */
7865 struct md_rdev
*rdev
;
7867 rdev_for_each(rdev
, mddev
) {
7868 if (rdev
->data_offset
> rdev
->new_data_offset
)
7869 rdev
->sectors
+= rdev
->data_offset
- rdev
->new_data_offset
;
7871 rdev
->sectors
-= rdev
->new_data_offset
- rdev
->data_offset
;
7872 rdev
->data_offset
= rdev
->new_data_offset
;
7875 EXPORT_SYMBOL(md_finish_reshape
);
7877 /* Bad block management.
7878 * We can record which blocks on each device are 'bad' and so just
7879 * fail those blocks, or that stripe, rather than the whole device.
7880 * Entries in the bad-block table are 64bits wide. This comprises:
7881 * Length of bad-range, in sectors: 0-511 for lengths 1-512
7882 * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes)
7883 * A 'shift' can be set so that larger blocks are tracked and
7884 * consequently larger devices can be covered.
7885 * 'Acknowledged' flag - 1 bit. - the most significant bit.
7887 * Locking of the bad-block table uses a seqlock so md_is_badblock
7888 * might need to retry if it is very unlucky.
7889 * We will sometimes want to check for bad blocks in a bi_end_io function,
7890 * so we use the write_seqlock_irq variant.
7892 * When looking for a bad block we specify a range and want to
7893 * know if any block in the range is bad. So we binary-search
7894 * to the last range that starts at-or-before the given endpoint,
7895 * (or "before the sector after the target range")
7896 * then see if it ends after the given start.
7898 * 0 if there are no known bad blocks in the range
7899 * 1 if there are known bad block which are all acknowledged
7900 * -1 if there are bad blocks which have not yet been acknowledged in metadata.
7901 * plus the start/length of the first bad section we overlap.
7903 int md_is_badblock(struct badblocks
*bb
, sector_t s
, int sectors
,
7904 sector_t
*first_bad
, int *bad_sectors
)
7910 sector_t target
= s
+ sectors
;
7913 if (bb
->shift
> 0) {
7914 /* round the start down, and the end up */
7916 target
+= (1<<bb
->shift
) - 1;
7917 target
>>= bb
->shift
;
7918 sectors
= target
- s
;
7920 /* 'target' is now the first block after the bad range */
7923 seq
= read_seqbegin(&bb
->lock
);
7927 /* Binary search between lo and hi for 'target'
7928 * i.e. for the last range that starts before 'target'
7930 /* INVARIANT: ranges before 'lo' and at-or-after 'hi'
7931 * are known not to be the last range before target.
7932 * VARIANT: hi-lo is the number of possible
7933 * ranges, and decreases until it reaches 1
7935 while (hi
- lo
> 1) {
7936 int mid
= (lo
+ hi
) / 2;
7937 sector_t a
= BB_OFFSET(p
[mid
]);
7939 /* This could still be the one, earlier ranges
7943 /* This and later ranges are definitely out. */
7946 /* 'lo' might be the last that started before target, but 'hi' isn't */
7948 /* need to check all range that end after 's' to see if
7949 * any are unacknowledged.
7952 BB_OFFSET(p
[lo
]) + BB_LEN(p
[lo
]) > s
) {
7953 if (BB_OFFSET(p
[lo
]) < target
) {
7954 /* starts before the end, and finishes after
7955 * the start, so they must overlap
7957 if (rv
!= -1 && BB_ACK(p
[lo
]))
7961 *first_bad
= BB_OFFSET(p
[lo
]);
7962 *bad_sectors
= BB_LEN(p
[lo
]);
7968 if (read_seqretry(&bb
->lock
, seq
))
7973 EXPORT_SYMBOL_GPL(md_is_badblock
);
7976 * Add a range of bad blocks to the table.
7977 * This might extend the table, or might contract it
7978 * if two adjacent ranges can be merged.
7979 * We binary-search to find the 'insertion' point, then
7980 * decide how best to handle it.
7982 static int md_set_badblocks(struct badblocks
*bb
, sector_t s
, int sectors
,
7990 /* badblocks are disabled */
7994 /* round the start down, and the end up */
7995 sector_t next
= s
+ sectors
;
7997 next
+= (1<<bb
->shift
) - 1;
8002 write_seqlock_irq(&bb
->lock
);
8007 /* Find the last range that starts at-or-before 's' */
8008 while (hi
- lo
> 1) {
8009 int mid
= (lo
+ hi
) / 2;
8010 sector_t a
= BB_OFFSET(p
[mid
]);
8016 if (hi
> lo
&& BB_OFFSET(p
[lo
]) > s
)
8020 /* we found a range that might merge with the start
8023 sector_t a
= BB_OFFSET(p
[lo
]);
8024 sector_t e
= a
+ BB_LEN(p
[lo
]);
8025 int ack
= BB_ACK(p
[lo
]);
8027 /* Yes, we can merge with a previous range */
8028 if (s
== a
&& s
+ sectors
>= e
)
8029 /* new range covers old */
8032 ack
= ack
&& acknowledged
;
8034 if (e
< s
+ sectors
)
8036 if (e
- a
<= BB_MAX_LEN
) {
8037 p
[lo
] = BB_MAKE(a
, e
-a
, ack
);
8040 /* does not all fit in one range,
8041 * make p[lo] maximal
8043 if (BB_LEN(p
[lo
]) != BB_MAX_LEN
)
8044 p
[lo
] = BB_MAKE(a
, BB_MAX_LEN
, ack
);
8050 if (sectors
&& hi
< bb
->count
) {
8051 /* 'hi' points to the first range that starts after 's'.
8052 * Maybe we can merge with the start of that range */
8053 sector_t a
= BB_OFFSET(p
[hi
]);
8054 sector_t e
= a
+ BB_LEN(p
[hi
]);
8055 int ack
= BB_ACK(p
[hi
]);
8056 if (a
<= s
+ sectors
) {
8057 /* merging is possible */
8058 if (e
<= s
+ sectors
) {
8063 ack
= ack
&& acknowledged
;
8066 if (e
- a
<= BB_MAX_LEN
) {
8067 p
[hi
] = BB_MAKE(a
, e
-a
, ack
);
8070 p
[hi
] = BB_MAKE(a
, BB_MAX_LEN
, ack
);
8078 if (sectors
== 0 && hi
< bb
->count
) {
8079 /* we might be able to combine lo and hi */
8080 /* Note: 's' is at the end of 'lo' */
8081 sector_t a
= BB_OFFSET(p
[hi
]);
8082 int lolen
= BB_LEN(p
[lo
]);
8083 int hilen
= BB_LEN(p
[hi
]);
8084 int newlen
= lolen
+ hilen
- (s
- a
);
8085 if (s
>= a
&& newlen
< BB_MAX_LEN
) {
8086 /* yes, we can combine them */
8087 int ack
= BB_ACK(p
[lo
]) && BB_ACK(p
[hi
]);
8088 p
[lo
] = BB_MAKE(BB_OFFSET(p
[lo
]), newlen
, ack
);
8089 memmove(p
+ hi
, p
+ hi
+ 1,
8090 (bb
->count
- hi
- 1) * 8);
8095 /* didn't merge (it all).
8096 * Need to add a range just before 'hi' */
8097 if (bb
->count
>= MD_MAX_BADBLOCKS
) {
8098 /* No room for more */
8102 int this_sectors
= sectors
;
8103 memmove(p
+ hi
+ 1, p
+ hi
,
8104 (bb
->count
- hi
) * 8);
8107 if (this_sectors
> BB_MAX_LEN
)
8108 this_sectors
= BB_MAX_LEN
;
8109 p
[hi
] = BB_MAKE(s
, this_sectors
, acknowledged
);
8110 sectors
-= this_sectors
;
8117 bb
->unacked_exist
= 1;
8118 write_sequnlock_irq(&bb
->lock
);
8123 int rdev_set_badblocks(struct md_rdev
*rdev
, sector_t s
, int sectors
,
8128 s
+= rdev
->new_data_offset
;
8130 s
+= rdev
->data_offset
;
8131 rv
= md_set_badblocks(&rdev
->badblocks
,
8134 /* Make sure they get written out promptly */
8135 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
8136 set_bit(MD_CHANGE_CLEAN
, &rdev
->mddev
->flags
);
8137 md_wakeup_thread(rdev
->mddev
->thread
);
8141 EXPORT_SYMBOL_GPL(rdev_set_badblocks
);
8144 * Remove a range of bad blocks from the table.
8145 * This may involve extending the table if we spilt a region,
8146 * but it must not fail. So if the table becomes full, we just
8147 * drop the remove request.
8149 static int md_clear_badblocks(struct badblocks
*bb
, sector_t s
, int sectors
)
8153 sector_t target
= s
+ sectors
;
8156 if (bb
->shift
> 0) {
8157 /* When clearing we round the start up and the end down.
8158 * This should not matter as the shift should align with
8159 * the block size and no rounding should ever be needed.
8160 * However it is better the think a block is bad when it
8161 * isn't than to think a block is not bad when it is.
8163 s
+= (1<<bb
->shift
) - 1;
8165 target
>>= bb
->shift
;
8166 sectors
= target
- s
;
8169 write_seqlock_irq(&bb
->lock
);
8174 /* Find the last range that starts before 'target' */
8175 while (hi
- lo
> 1) {
8176 int mid
= (lo
+ hi
) / 2;
8177 sector_t a
= BB_OFFSET(p
[mid
]);
8184 /* p[lo] is the last range that could overlap the
8185 * current range. Earlier ranges could also overlap,
8186 * but only this one can overlap the end of the range.
8188 if (BB_OFFSET(p
[lo
]) + BB_LEN(p
[lo
]) > target
) {
8189 /* Partial overlap, leave the tail of this range */
8190 int ack
= BB_ACK(p
[lo
]);
8191 sector_t a
= BB_OFFSET(p
[lo
]);
8192 sector_t end
= a
+ BB_LEN(p
[lo
]);
8195 /* we need to split this range */
8196 if (bb
->count
>= MD_MAX_BADBLOCKS
) {
8200 memmove(p
+lo
+1, p
+lo
, (bb
->count
- lo
) * 8);
8202 p
[lo
] = BB_MAKE(a
, s
-a
, ack
);
8205 p
[lo
] = BB_MAKE(target
, end
- target
, ack
);
8206 /* there is no longer an overlap */
8211 BB_OFFSET(p
[lo
]) + BB_LEN(p
[lo
]) > s
) {
8212 /* This range does overlap */
8213 if (BB_OFFSET(p
[lo
]) < s
) {
8214 /* Keep the early parts of this range. */
8215 int ack
= BB_ACK(p
[lo
]);
8216 sector_t start
= BB_OFFSET(p
[lo
]);
8217 p
[lo
] = BB_MAKE(start
, s
- start
, ack
);
8218 /* now low doesn't overlap, so.. */
8223 /* 'lo' is strictly before, 'hi' is strictly after,
8224 * anything between needs to be discarded
8227 memmove(p
+lo
+1, p
+hi
, (bb
->count
- hi
) * 8);
8228 bb
->count
-= (hi
- lo
- 1);
8234 write_sequnlock_irq(&bb
->lock
);
8238 int rdev_clear_badblocks(struct md_rdev
*rdev
, sector_t s
, int sectors
,
8242 s
+= rdev
->new_data_offset
;
8244 s
+= rdev
->data_offset
;
8245 return md_clear_badblocks(&rdev
->badblocks
,
8248 EXPORT_SYMBOL_GPL(rdev_clear_badblocks
);
8251 * Acknowledge all bad blocks in a list.
8252 * This only succeeds if ->changed is clear. It is used by
8253 * in-kernel metadata updates
8255 void md_ack_all_badblocks(struct badblocks
*bb
)
8257 if (bb
->page
== NULL
|| bb
->changed
)
8258 /* no point even trying */
8260 write_seqlock_irq(&bb
->lock
);
8262 if (bb
->changed
== 0 && bb
->unacked_exist
) {
8265 for (i
= 0; i
< bb
->count
; i
++) {
8266 if (!BB_ACK(p
[i
])) {
8267 sector_t start
= BB_OFFSET(p
[i
]);
8268 int len
= BB_LEN(p
[i
]);
8269 p
[i
] = BB_MAKE(start
, len
, 1);
8272 bb
->unacked_exist
= 0;
8274 write_sequnlock_irq(&bb
->lock
);
8276 EXPORT_SYMBOL_GPL(md_ack_all_badblocks
);
8278 /* sysfs access to bad-blocks list.
8279 * We present two files.
8280 * 'bad-blocks' lists sector numbers and lengths of ranges that
8281 * are recorded as bad. The list is truncated to fit within
8282 * the one-page limit of sysfs.
8283 * Writing "sector length" to this file adds an acknowledged
8285 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
8286 * been acknowledged. Writing to this file adds bad blocks
8287 * without acknowledging them. This is largely for testing.
8291 badblocks_show(struct badblocks
*bb
, char *page
, int unack
)
8302 seq
= read_seqbegin(&bb
->lock
);
8307 while (len
< PAGE_SIZE
&& i
< bb
->count
) {
8308 sector_t s
= BB_OFFSET(p
[i
]);
8309 unsigned int length
= BB_LEN(p
[i
]);
8310 int ack
= BB_ACK(p
[i
]);
8316 len
+= snprintf(page
+len
, PAGE_SIZE
-len
, "%llu %u\n",
8317 (unsigned long long)s
<< bb
->shift
,
8318 length
<< bb
->shift
);
8320 if (unack
&& len
== 0)
8321 bb
->unacked_exist
= 0;
8323 if (read_seqretry(&bb
->lock
, seq
))
8332 badblocks_store(struct badblocks
*bb
, const char *page
, size_t len
, int unack
)
8334 unsigned long long sector
;
8338 /* Allow clearing via sysfs *only* for testing/debugging.
8339 * Normally only a successful write may clear a badblock
8342 if (page
[0] == '-') {
8346 #endif /* DO_DEBUG */
8348 switch (sscanf(page
, "%llu %d%c", §or
, &length
, &newline
)) {
8350 if (newline
!= '\n')
8362 md_clear_badblocks(bb
, sector
, length
);
8365 #endif /* DO_DEBUG */
8366 if (md_set_badblocks(bb
, sector
, length
, !unack
))
8372 static int md_notify_reboot(struct notifier_block
*this,
8373 unsigned long code
, void *x
)
8375 struct list_head
*tmp
;
8376 struct mddev
*mddev
;
8379 for_each_mddev(mddev
, tmp
) {
8380 if (mddev_trylock(mddev
)) {
8382 __md_stop_writes(mddev
);
8383 mddev
->safemode
= 2;
8384 mddev_unlock(mddev
);
8389 * certain more exotic SCSI devices are known to be
8390 * volatile wrt too early system reboots. While the
8391 * right place to handle this issue is the given
8392 * driver, we do want to have a safe RAID driver ...
8400 static struct notifier_block md_notifier
= {
8401 .notifier_call
= md_notify_reboot
,
8403 .priority
= INT_MAX
, /* before any real devices */
8406 static void md_geninit(void)
8408 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t
));
8410 proc_create("mdstat", S_IRUGO
, NULL
, &md_seq_fops
);
8413 static int __init
md_init(void)
8417 md_wq
= alloc_workqueue("md", WQ_MEM_RECLAIM
, 0);
8421 md_misc_wq
= alloc_workqueue("md_misc", 0, 0);
8425 if ((ret
= register_blkdev(MD_MAJOR
, "md")) < 0)
8428 if ((ret
= register_blkdev(0, "mdp")) < 0)
8432 blk_register_region(MKDEV(MD_MAJOR
, 0), 1UL<<MINORBITS
, THIS_MODULE
,
8433 md_probe
, NULL
, NULL
);
8434 blk_register_region(MKDEV(mdp_major
, 0), 1UL<<MINORBITS
, THIS_MODULE
,
8435 md_probe
, NULL
, NULL
);
8437 register_reboot_notifier(&md_notifier
);
8438 raid_table_header
= register_sysctl_table(raid_root_table
);
8444 unregister_blkdev(MD_MAJOR
, "md");
8446 destroy_workqueue(md_misc_wq
);
8448 destroy_workqueue(md_wq
);
8456 * Searches all registered partitions for autorun RAID arrays
8460 static LIST_HEAD(all_detected_devices
);
8461 struct detected_devices_node
{
8462 struct list_head list
;
8466 void md_autodetect_dev(dev_t dev
)
8468 struct detected_devices_node
*node_detected_dev
;
8470 node_detected_dev
= kzalloc(sizeof(*node_detected_dev
), GFP_KERNEL
);
8471 if (node_detected_dev
) {
8472 node_detected_dev
->dev
= dev
;
8473 list_add_tail(&node_detected_dev
->list
, &all_detected_devices
);
8475 printk(KERN_CRIT
"md: md_autodetect_dev: kzalloc failed"
8476 ", skipping dev(%d,%d)\n", MAJOR(dev
), MINOR(dev
));
8481 static void autostart_arrays(int part
)
8483 struct md_rdev
*rdev
;
8484 struct detected_devices_node
*node_detected_dev
;
8486 int i_scanned
, i_passed
;
8491 printk(KERN_INFO
"md: Autodetecting RAID arrays.\n");
8493 while (!list_empty(&all_detected_devices
) && i_scanned
< INT_MAX
) {
8495 node_detected_dev
= list_entry(all_detected_devices
.next
,
8496 struct detected_devices_node
, list
);
8497 list_del(&node_detected_dev
->list
);
8498 dev
= node_detected_dev
->dev
;
8499 kfree(node_detected_dev
);
8500 rdev
= md_import_device(dev
,0, 90);
8504 if (test_bit(Faulty
, &rdev
->flags
)) {
8508 set_bit(AutoDetected
, &rdev
->flags
);
8509 list_add(&rdev
->same_set
, &pending_raid_disks
);
8513 printk(KERN_INFO
"md: Scanned %d and added %d devices.\n",
8514 i_scanned
, i_passed
);
8516 autorun_devices(part
);
8519 #endif /* !MODULE */
8521 static __exit
void md_exit(void)
8523 struct mddev
*mddev
;
8524 struct list_head
*tmp
;
8526 blk_unregister_region(MKDEV(MD_MAJOR
,0), 1U << MINORBITS
);
8527 blk_unregister_region(MKDEV(mdp_major
,0), 1U << MINORBITS
);
8529 unregister_blkdev(MD_MAJOR
,"md");
8530 unregister_blkdev(mdp_major
, "mdp");
8531 unregister_reboot_notifier(&md_notifier
);
8532 unregister_sysctl_table(raid_table_header
);
8533 remove_proc_entry("mdstat", NULL
);
8534 for_each_mddev(mddev
, tmp
) {
8535 export_array(mddev
);
8536 mddev
->hold_active
= 0;
8538 destroy_workqueue(md_misc_wq
);
8539 destroy_workqueue(md_wq
);
8542 subsys_initcall(md_init
);
8543 module_exit(md_exit
)
8545 static int get_ro(char *buffer
, struct kernel_param
*kp
)
8547 return sprintf(buffer
, "%d", start_readonly
);
8549 static int set_ro(const char *val
, struct kernel_param
*kp
)
8552 int num
= simple_strtoul(val
, &e
, 10);
8553 if (*val
&& (*e
== '\0' || *e
== '\n')) {
8554 start_readonly
= num
;
8560 module_param_call(start_ro
, set_ro
, get_ro
, NULL
, S_IRUSR
|S_IWUSR
);
8561 module_param(start_dirty_degraded
, int, S_IRUGO
|S_IWUSR
);
8563 module_param_call(new_array
, add_named_array
, NULL
, NULL
, S_IWUSR
);
8565 EXPORT_SYMBOL(register_md_personality
);
8566 EXPORT_SYMBOL(unregister_md_personality
);
8567 EXPORT_SYMBOL(md_error
);
8568 EXPORT_SYMBOL(md_done_sync
);
8569 EXPORT_SYMBOL(md_write_start
);
8570 EXPORT_SYMBOL(md_write_end
);
8571 EXPORT_SYMBOL(md_register_thread
);
8572 EXPORT_SYMBOL(md_unregister_thread
);
8573 EXPORT_SYMBOL(md_wakeup_thread
);
8574 EXPORT_SYMBOL(md_check_recovery
);
8575 MODULE_LICENSE("GPL");
8576 MODULE_DESCRIPTION("MD RAID framework");
8578 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR
);