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 md_wakeup_thread(mddev
->thread
);
406 md_wakeup_thread(mddev
->sync_thread
); /* possibly kick off a reshape */
408 EXPORT_SYMBOL_GPL(mddev_resume
);
410 int mddev_congested(struct mddev
*mddev
, int bits
)
412 return mddev
->suspended
;
414 EXPORT_SYMBOL(mddev_congested
);
417 * Generic flush handling for md
420 static void md_end_flush(struct bio
*bio
, int err
)
422 struct md_rdev
*rdev
= bio
->bi_private
;
423 struct mddev
*mddev
= rdev
->mddev
;
425 rdev_dec_pending(rdev
, mddev
);
427 if (atomic_dec_and_test(&mddev
->flush_pending
)) {
428 /* The pre-request flush has finished */
429 queue_work(md_wq
, &mddev
->flush_work
);
434 static void md_submit_flush_data(struct work_struct
*ws
);
436 static void submit_flushes(struct work_struct
*ws
)
438 struct mddev
*mddev
= container_of(ws
, struct mddev
, flush_work
);
439 struct md_rdev
*rdev
;
441 INIT_WORK(&mddev
->flush_work
, md_submit_flush_data
);
442 atomic_set(&mddev
->flush_pending
, 1);
444 list_for_each_entry_rcu(rdev
, &mddev
->disks
, same_set
)
445 if (rdev
->raid_disk
>= 0 &&
446 !test_bit(Faulty
, &rdev
->flags
)) {
447 /* Take two references, one is dropped
448 * when request finishes, one after
449 * we reclaim rcu_read_lock
452 atomic_inc(&rdev
->nr_pending
);
453 atomic_inc(&rdev
->nr_pending
);
455 bi
= bio_alloc_mddev(GFP_KERNEL
, 0, mddev
);
456 bi
->bi_end_io
= md_end_flush
;
457 bi
->bi_private
= rdev
;
458 bi
->bi_bdev
= rdev
->bdev
;
459 atomic_inc(&mddev
->flush_pending
);
460 submit_bio(WRITE_FLUSH
, bi
);
462 rdev_dec_pending(rdev
, mddev
);
465 if (atomic_dec_and_test(&mddev
->flush_pending
))
466 queue_work(md_wq
, &mddev
->flush_work
);
469 static void md_submit_flush_data(struct work_struct
*ws
)
471 struct mddev
*mddev
= container_of(ws
, struct mddev
, flush_work
);
472 struct bio
*bio
= mddev
->flush_bio
;
474 if (bio
->bi_size
== 0)
475 /* an empty barrier - all done */
478 bio
->bi_rw
&= ~REQ_FLUSH
;
479 mddev
->pers
->make_request(mddev
, bio
);
482 mddev
->flush_bio
= NULL
;
483 wake_up(&mddev
->sb_wait
);
486 void md_flush_request(struct mddev
*mddev
, struct bio
*bio
)
488 spin_lock_irq(&mddev
->write_lock
);
489 wait_event_lock_irq(mddev
->sb_wait
,
491 mddev
->write_lock
, /*nothing*/);
492 mddev
->flush_bio
= bio
;
493 spin_unlock_irq(&mddev
->write_lock
);
495 INIT_WORK(&mddev
->flush_work
, submit_flushes
);
496 queue_work(md_wq
, &mddev
->flush_work
);
498 EXPORT_SYMBOL(md_flush_request
);
500 /* Support for plugging.
501 * This mirrors the plugging support in request_queue, but does not
502 * require having a whole queue or request structures.
503 * We allocate an md_plug_cb for each md device and each thread it gets
504 * plugged on. This links tot the private plug_handle structure in the
505 * personality data where we keep a count of the number of outstanding
506 * plugs so other code can see if a plug is active.
509 struct blk_plug_cb cb
;
513 static void plugger_unplug(struct blk_plug_cb
*cb
)
515 struct md_plug_cb
*mdcb
= container_of(cb
, struct md_plug_cb
, cb
);
516 if (atomic_dec_and_test(&mdcb
->mddev
->plug_cnt
))
517 md_wakeup_thread(mdcb
->mddev
->thread
);
521 /* Check that an unplug wakeup will come shortly.
522 * If not, wakeup the md thread immediately
524 int mddev_check_plugged(struct mddev
*mddev
)
526 struct blk_plug
*plug
= current
->plug
;
527 struct md_plug_cb
*mdcb
;
532 list_for_each_entry(mdcb
, &plug
->cb_list
, cb
.list
) {
533 if (mdcb
->cb
.callback
== plugger_unplug
&&
534 mdcb
->mddev
== mddev
) {
535 /* Already on the list, move to top */
536 if (mdcb
!= list_first_entry(&plug
->cb_list
,
539 list_move(&mdcb
->cb
.list
, &plug
->cb_list
);
543 /* Not currently on the callback list */
544 mdcb
= kmalloc(sizeof(*mdcb
), GFP_ATOMIC
);
549 mdcb
->cb
.callback
= plugger_unplug
;
550 atomic_inc(&mddev
->plug_cnt
);
551 list_add(&mdcb
->cb
.list
, &plug
->cb_list
);
554 EXPORT_SYMBOL_GPL(mddev_check_plugged
);
556 static inline struct mddev
*mddev_get(struct mddev
*mddev
)
558 atomic_inc(&mddev
->active
);
562 static void mddev_delayed_delete(struct work_struct
*ws
);
564 static void mddev_put(struct mddev
*mddev
)
566 struct bio_set
*bs
= NULL
;
568 if (!atomic_dec_and_lock(&mddev
->active
, &all_mddevs_lock
))
570 if (!mddev
->raid_disks
&& list_empty(&mddev
->disks
) &&
571 mddev
->ctime
== 0 && !mddev
->hold_active
) {
572 /* Array is not configured at all, and not held active,
574 list_del_init(&mddev
->all_mddevs
);
576 mddev
->bio_set
= NULL
;
577 if (mddev
->gendisk
) {
578 /* We did a probe so need to clean up. Call
579 * queue_work inside the spinlock so that
580 * flush_workqueue() after mddev_find will
581 * succeed in waiting for the work to be done.
583 INIT_WORK(&mddev
->del_work
, mddev_delayed_delete
);
584 queue_work(md_misc_wq
, &mddev
->del_work
);
588 spin_unlock(&all_mddevs_lock
);
593 void mddev_init(struct mddev
*mddev
)
595 mutex_init(&mddev
->open_mutex
);
596 mutex_init(&mddev
->reconfig_mutex
);
597 mutex_init(&mddev
->bitmap_info
.mutex
);
598 INIT_LIST_HEAD(&mddev
->disks
);
599 INIT_LIST_HEAD(&mddev
->all_mddevs
);
600 init_timer(&mddev
->safemode_timer
);
601 atomic_set(&mddev
->active
, 1);
602 atomic_set(&mddev
->openers
, 0);
603 atomic_set(&mddev
->active_io
, 0);
604 atomic_set(&mddev
->plug_cnt
, 0);
605 spin_lock_init(&mddev
->write_lock
);
606 atomic_set(&mddev
->flush_pending
, 0);
607 init_waitqueue_head(&mddev
->sb_wait
);
608 init_waitqueue_head(&mddev
->recovery_wait
);
609 mddev
->reshape_position
= MaxSector
;
610 mddev
->resync_min
= 0;
611 mddev
->resync_max
= MaxSector
;
612 mddev
->level
= LEVEL_NONE
;
614 EXPORT_SYMBOL_GPL(mddev_init
);
616 static struct mddev
* mddev_find(dev_t unit
)
618 struct mddev
*mddev
, *new = NULL
;
620 if (unit
&& MAJOR(unit
) != MD_MAJOR
)
621 unit
&= ~((1<<MdpMinorShift
)-1);
624 spin_lock(&all_mddevs_lock
);
627 list_for_each_entry(mddev
, &all_mddevs
, all_mddevs
)
628 if (mddev
->unit
== unit
) {
630 spin_unlock(&all_mddevs_lock
);
636 list_add(&new->all_mddevs
, &all_mddevs
);
637 spin_unlock(&all_mddevs_lock
);
638 new->hold_active
= UNTIL_IOCTL
;
642 /* find an unused unit number */
643 static int next_minor
= 512;
644 int start
= next_minor
;
648 dev
= MKDEV(MD_MAJOR
, next_minor
);
650 if (next_minor
> MINORMASK
)
652 if (next_minor
== start
) {
653 /* Oh dear, all in use. */
654 spin_unlock(&all_mddevs_lock
);
660 list_for_each_entry(mddev
, &all_mddevs
, all_mddevs
)
661 if (mddev
->unit
== dev
) {
667 new->md_minor
= MINOR(dev
);
668 new->hold_active
= UNTIL_STOP
;
669 list_add(&new->all_mddevs
, &all_mddevs
);
670 spin_unlock(&all_mddevs_lock
);
673 spin_unlock(&all_mddevs_lock
);
675 new = kzalloc(sizeof(*new), GFP_KERNEL
);
680 if (MAJOR(unit
) == MD_MAJOR
)
681 new->md_minor
= MINOR(unit
);
683 new->md_minor
= MINOR(unit
) >> MdpMinorShift
;
690 static inline int mddev_lock(struct mddev
* mddev
)
692 return mutex_lock_interruptible(&mddev
->reconfig_mutex
);
695 static inline int mddev_is_locked(struct mddev
*mddev
)
697 return mutex_is_locked(&mddev
->reconfig_mutex
);
700 static inline int mddev_trylock(struct mddev
* mddev
)
702 return mutex_trylock(&mddev
->reconfig_mutex
);
705 static struct attribute_group md_redundancy_group
;
707 static void mddev_unlock(struct mddev
* mddev
)
709 if (mddev
->to_remove
) {
710 /* These cannot be removed under reconfig_mutex as
711 * an access to the files will try to take reconfig_mutex
712 * while holding the file unremovable, which leads to
714 * So hold set sysfs_active while the remove in happeing,
715 * and anything else which might set ->to_remove or my
716 * otherwise change the sysfs namespace will fail with
717 * -EBUSY if sysfs_active is still set.
718 * We set sysfs_active under reconfig_mutex and elsewhere
719 * test it under the same mutex to ensure its correct value
722 struct attribute_group
*to_remove
= mddev
->to_remove
;
723 mddev
->to_remove
= NULL
;
724 mddev
->sysfs_active
= 1;
725 mutex_unlock(&mddev
->reconfig_mutex
);
727 if (mddev
->kobj
.sd
) {
728 if (to_remove
!= &md_redundancy_group
)
729 sysfs_remove_group(&mddev
->kobj
, to_remove
);
730 if (mddev
->pers
== NULL
||
731 mddev
->pers
->sync_request
== NULL
) {
732 sysfs_remove_group(&mddev
->kobj
, &md_redundancy_group
);
733 if (mddev
->sysfs_action
)
734 sysfs_put(mddev
->sysfs_action
);
735 mddev
->sysfs_action
= NULL
;
738 mddev
->sysfs_active
= 0;
740 mutex_unlock(&mddev
->reconfig_mutex
);
742 /* As we've dropped the mutex we need a spinlock to
743 * make sure the thread doesn't disappear
745 spin_lock(&pers_lock
);
746 md_wakeup_thread(mddev
->thread
);
747 spin_unlock(&pers_lock
);
750 static struct md_rdev
* find_rdev_nr(struct mddev
*mddev
, int nr
)
752 struct md_rdev
*rdev
;
754 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
755 if (rdev
->desc_nr
== nr
)
761 static struct md_rdev
* find_rdev(struct mddev
* mddev
, dev_t dev
)
763 struct md_rdev
*rdev
;
765 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
766 if (rdev
->bdev
->bd_dev
== dev
)
772 static struct md_personality
*find_pers(int level
, char *clevel
)
774 struct md_personality
*pers
;
775 list_for_each_entry(pers
, &pers_list
, list
) {
776 if (level
!= LEVEL_NONE
&& pers
->level
== level
)
778 if (strcmp(pers
->name
, clevel
)==0)
784 /* return the offset of the super block in 512byte sectors */
785 static inline sector_t
calc_dev_sboffset(struct md_rdev
*rdev
)
787 sector_t num_sectors
= i_size_read(rdev
->bdev
->bd_inode
) / 512;
788 return MD_NEW_SIZE_SECTORS(num_sectors
);
791 static int alloc_disk_sb(struct md_rdev
* rdev
)
796 rdev
->sb_page
= alloc_page(GFP_KERNEL
);
797 if (!rdev
->sb_page
) {
798 printk(KERN_ALERT
"md: out of memory.\n");
805 static void free_disk_sb(struct md_rdev
* rdev
)
808 put_page(rdev
->sb_page
);
810 rdev
->sb_page
= NULL
;
815 put_page(rdev
->bb_page
);
816 rdev
->bb_page
= NULL
;
821 static void super_written(struct bio
*bio
, int error
)
823 struct md_rdev
*rdev
= bio
->bi_private
;
824 struct mddev
*mddev
= rdev
->mddev
;
826 if (error
|| !test_bit(BIO_UPTODATE
, &bio
->bi_flags
)) {
827 printk("md: super_written gets error=%d, uptodate=%d\n",
828 error
, test_bit(BIO_UPTODATE
, &bio
->bi_flags
));
829 WARN_ON(test_bit(BIO_UPTODATE
, &bio
->bi_flags
));
830 md_error(mddev
, rdev
);
833 if (atomic_dec_and_test(&mddev
->pending_writes
))
834 wake_up(&mddev
->sb_wait
);
838 void md_super_write(struct mddev
*mddev
, struct md_rdev
*rdev
,
839 sector_t sector
, int size
, struct page
*page
)
841 /* write first size bytes of page to sector of rdev
842 * Increment mddev->pending_writes before returning
843 * and decrement it on completion, waking up sb_wait
844 * if zero is reached.
845 * If an error occurred, call md_error
847 struct bio
*bio
= bio_alloc_mddev(GFP_NOIO
, 1, mddev
);
849 bio
->bi_bdev
= rdev
->meta_bdev
? rdev
->meta_bdev
: rdev
->bdev
;
850 bio
->bi_sector
= sector
;
851 bio_add_page(bio
, page
, size
, 0);
852 bio
->bi_private
= rdev
;
853 bio
->bi_end_io
= super_written
;
855 atomic_inc(&mddev
->pending_writes
);
856 submit_bio(WRITE_FLUSH_FUA
, bio
);
859 void md_super_wait(struct mddev
*mddev
)
861 /* wait for all superblock writes that were scheduled to complete */
864 prepare_to_wait(&mddev
->sb_wait
, &wq
, TASK_UNINTERRUPTIBLE
);
865 if (atomic_read(&mddev
->pending_writes
)==0)
869 finish_wait(&mddev
->sb_wait
, &wq
);
872 static void bi_complete(struct bio
*bio
, int error
)
874 complete((struct completion
*)bio
->bi_private
);
877 int sync_page_io(struct md_rdev
*rdev
, sector_t sector
, int size
,
878 struct page
*page
, int rw
, bool metadata_op
)
880 struct bio
*bio
= bio_alloc_mddev(GFP_NOIO
, 1, rdev
->mddev
);
881 struct completion event
;
886 bio
->bi_bdev
= (metadata_op
&& rdev
->meta_bdev
) ?
887 rdev
->meta_bdev
: rdev
->bdev
;
889 bio
->bi_sector
= sector
+ rdev
->sb_start
;
891 bio
->bi_sector
= sector
+ rdev
->data_offset
;
892 bio_add_page(bio
, page
, size
, 0);
893 init_completion(&event
);
894 bio
->bi_private
= &event
;
895 bio
->bi_end_io
= bi_complete
;
897 wait_for_completion(&event
);
899 ret
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
903 EXPORT_SYMBOL_GPL(sync_page_io
);
905 static int read_disk_sb(struct md_rdev
* rdev
, int size
)
907 char b
[BDEVNAME_SIZE
];
908 if (!rdev
->sb_page
) {
916 if (!sync_page_io(rdev
, 0, size
, rdev
->sb_page
, READ
, true))
922 printk(KERN_WARNING
"md: disabled device %s, could not read superblock.\n",
923 bdevname(rdev
->bdev
,b
));
927 static int uuid_equal(mdp_super_t
*sb1
, mdp_super_t
*sb2
)
929 return sb1
->set_uuid0
== sb2
->set_uuid0
&&
930 sb1
->set_uuid1
== sb2
->set_uuid1
&&
931 sb1
->set_uuid2
== sb2
->set_uuid2
&&
932 sb1
->set_uuid3
== sb2
->set_uuid3
;
935 static int sb_equal(mdp_super_t
*sb1
, mdp_super_t
*sb2
)
938 mdp_super_t
*tmp1
, *tmp2
;
940 tmp1
= kmalloc(sizeof(*tmp1
),GFP_KERNEL
);
941 tmp2
= kmalloc(sizeof(*tmp2
),GFP_KERNEL
);
943 if (!tmp1
|| !tmp2
) {
945 printk(KERN_INFO
"md.c sb_equal(): failed to allocate memory!\n");
953 * nr_disks is not constant
958 ret
= (memcmp(tmp1
, tmp2
, MD_SB_GENERIC_CONSTANT_WORDS
* 4) == 0);
966 static u32
md_csum_fold(u32 csum
)
968 csum
= (csum
& 0xffff) + (csum
>> 16);
969 return (csum
& 0xffff) + (csum
>> 16);
972 static unsigned int calc_sb_csum(mdp_super_t
* sb
)
975 u32
*sb32
= (u32
*)sb
;
977 unsigned int disk_csum
, csum
;
979 disk_csum
= sb
->sb_csum
;
982 for (i
= 0; i
< MD_SB_BYTES
/4 ; i
++)
984 csum
= (newcsum
& 0xffffffff) + (newcsum
>>32);
988 /* This used to use csum_partial, which was wrong for several
989 * reasons including that different results are returned on
990 * different architectures. It isn't critical that we get exactly
991 * the same return value as before (we always csum_fold before
992 * testing, and that removes any differences). However as we
993 * know that csum_partial always returned a 16bit value on
994 * alphas, do a fold to maximise conformity to previous behaviour.
996 sb
->sb_csum
= md_csum_fold(disk_csum
);
998 sb
->sb_csum
= disk_csum
;
1005 * Handle superblock details.
1006 * We want to be able to handle multiple superblock formats
1007 * so we have a common interface to them all, and an array of
1008 * different handlers.
1009 * We rely on user-space to write the initial superblock, and support
1010 * reading and updating of superblocks.
1011 * Interface methods are:
1012 * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
1013 * loads and validates a superblock on dev.
1014 * if refdev != NULL, compare superblocks on both devices
1016 * 0 - dev has a superblock that is compatible with refdev
1017 * 1 - dev has a superblock that is compatible and newer than refdev
1018 * so dev should be used as the refdev in future
1019 * -EINVAL superblock incompatible or invalid
1020 * -othererror e.g. -EIO
1022 * int validate_super(struct mddev *mddev, struct md_rdev *dev)
1023 * Verify that dev is acceptable into mddev.
1024 * The first time, mddev->raid_disks will be 0, and data from
1025 * dev should be merged in. Subsequent calls check that dev
1026 * is new enough. Return 0 or -EINVAL
1028 * void sync_super(struct mddev *mddev, struct md_rdev *dev)
1029 * Update the superblock for rdev with data in mddev
1030 * This does not write to disc.
1036 struct module
*owner
;
1037 int (*load_super
)(struct md_rdev
*rdev
, struct md_rdev
*refdev
,
1039 int (*validate_super
)(struct mddev
*mddev
, struct md_rdev
*rdev
);
1040 void (*sync_super
)(struct mddev
*mddev
, struct md_rdev
*rdev
);
1041 unsigned long long (*rdev_size_change
)(struct md_rdev
*rdev
,
1042 sector_t num_sectors
);
1046 * Check that the given mddev has no bitmap.
1048 * This function is called from the run method of all personalities that do not
1049 * support bitmaps. It prints an error message and returns non-zero if mddev
1050 * has a bitmap. Otherwise, it returns 0.
1053 int md_check_no_bitmap(struct mddev
*mddev
)
1055 if (!mddev
->bitmap_info
.file
&& !mddev
->bitmap_info
.offset
)
1057 printk(KERN_ERR
"%s: bitmaps are not supported for %s\n",
1058 mdname(mddev
), mddev
->pers
->name
);
1061 EXPORT_SYMBOL(md_check_no_bitmap
);
1064 * load_super for 0.90.0
1066 static int super_90_load(struct md_rdev
*rdev
, struct md_rdev
*refdev
, int minor_version
)
1068 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
1073 * Calculate the position of the superblock (512byte sectors),
1074 * it's at the end of the disk.
1076 * It also happens to be a multiple of 4Kb.
1078 rdev
->sb_start
= calc_dev_sboffset(rdev
);
1080 ret
= read_disk_sb(rdev
, MD_SB_BYTES
);
1081 if (ret
) return ret
;
1085 bdevname(rdev
->bdev
, b
);
1086 sb
= page_address(rdev
->sb_page
);
1088 if (sb
->md_magic
!= MD_SB_MAGIC
) {
1089 printk(KERN_ERR
"md: invalid raid superblock magic on %s\n",
1094 if (sb
->major_version
!= 0 ||
1095 sb
->minor_version
< 90 ||
1096 sb
->minor_version
> 91) {
1097 printk(KERN_WARNING
"Bad version number %d.%d on %s\n",
1098 sb
->major_version
, sb
->minor_version
,
1103 if (sb
->raid_disks
<= 0)
1106 if (md_csum_fold(calc_sb_csum(sb
)) != md_csum_fold(sb
->sb_csum
)) {
1107 printk(KERN_WARNING
"md: invalid superblock checksum on %s\n",
1112 rdev
->preferred_minor
= sb
->md_minor
;
1113 rdev
->data_offset
= 0;
1114 rdev
->sb_size
= MD_SB_BYTES
;
1115 rdev
->badblocks
.shift
= -1;
1117 if (sb
->level
== LEVEL_MULTIPATH
)
1120 rdev
->desc_nr
= sb
->this_disk
.number
;
1126 mdp_super_t
*refsb
= page_address(refdev
->sb_page
);
1127 if (!uuid_equal(refsb
, sb
)) {
1128 printk(KERN_WARNING
"md: %s has different UUID to %s\n",
1129 b
, bdevname(refdev
->bdev
,b2
));
1132 if (!sb_equal(refsb
, sb
)) {
1133 printk(KERN_WARNING
"md: %s has same UUID"
1134 " but different superblock to %s\n",
1135 b
, bdevname(refdev
->bdev
, b2
));
1139 ev2
= md_event(refsb
);
1145 rdev
->sectors
= rdev
->sb_start
;
1146 /* Limit to 4TB as metadata cannot record more than that */
1147 if (rdev
->sectors
>= (2ULL << 32))
1148 rdev
->sectors
= (2ULL << 32) - 2;
1150 if (rdev
->sectors
< ((sector_t
)sb
->size
) * 2 && sb
->level
>= 1)
1151 /* "this cannot possibly happen" ... */
1159 * validate_super for 0.90.0
1161 static int super_90_validate(struct mddev
*mddev
, struct md_rdev
*rdev
)
1164 mdp_super_t
*sb
= page_address(rdev
->sb_page
);
1165 __u64 ev1
= md_event(sb
);
1167 rdev
->raid_disk
= -1;
1168 clear_bit(Faulty
, &rdev
->flags
);
1169 clear_bit(In_sync
, &rdev
->flags
);
1170 clear_bit(WriteMostly
, &rdev
->flags
);
1172 if (mddev
->raid_disks
== 0) {
1173 mddev
->major_version
= 0;
1174 mddev
->minor_version
= sb
->minor_version
;
1175 mddev
->patch_version
= sb
->patch_version
;
1176 mddev
->external
= 0;
1177 mddev
->chunk_sectors
= sb
->chunk_size
>> 9;
1178 mddev
->ctime
= sb
->ctime
;
1179 mddev
->utime
= sb
->utime
;
1180 mddev
->level
= sb
->level
;
1181 mddev
->clevel
[0] = 0;
1182 mddev
->layout
= sb
->layout
;
1183 mddev
->raid_disks
= sb
->raid_disks
;
1184 mddev
->dev_sectors
= ((sector_t
)sb
->size
) * 2;
1185 mddev
->events
= ev1
;
1186 mddev
->bitmap_info
.offset
= 0;
1187 mddev
->bitmap_info
.default_offset
= MD_SB_BYTES
>> 9;
1189 if (mddev
->minor_version
>= 91) {
1190 mddev
->reshape_position
= sb
->reshape_position
;
1191 mddev
->delta_disks
= sb
->delta_disks
;
1192 mddev
->new_level
= sb
->new_level
;
1193 mddev
->new_layout
= sb
->new_layout
;
1194 mddev
->new_chunk_sectors
= sb
->new_chunk
>> 9;
1196 mddev
->reshape_position
= MaxSector
;
1197 mddev
->delta_disks
= 0;
1198 mddev
->new_level
= mddev
->level
;
1199 mddev
->new_layout
= mddev
->layout
;
1200 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
1203 if (sb
->state
& (1<<MD_SB_CLEAN
))
1204 mddev
->recovery_cp
= MaxSector
;
1206 if (sb
->events_hi
== sb
->cp_events_hi
&&
1207 sb
->events_lo
== sb
->cp_events_lo
) {
1208 mddev
->recovery_cp
= sb
->recovery_cp
;
1210 mddev
->recovery_cp
= 0;
1213 memcpy(mddev
->uuid
+0, &sb
->set_uuid0
, 4);
1214 memcpy(mddev
->uuid
+4, &sb
->set_uuid1
, 4);
1215 memcpy(mddev
->uuid
+8, &sb
->set_uuid2
, 4);
1216 memcpy(mddev
->uuid
+12,&sb
->set_uuid3
, 4);
1218 mddev
->max_disks
= MD_SB_DISKS
;
1220 if (sb
->state
& (1<<MD_SB_BITMAP_PRESENT
) &&
1221 mddev
->bitmap_info
.file
== NULL
)
1222 mddev
->bitmap_info
.offset
=
1223 mddev
->bitmap_info
.default_offset
;
1225 } else if (mddev
->pers
== NULL
) {
1226 /* Insist on good event counter while assembling, except
1227 * for spares (which don't need an event count) */
1229 if (sb
->disks
[rdev
->desc_nr
].state
& (
1230 (1<<MD_DISK_SYNC
) | (1 << MD_DISK_ACTIVE
)))
1231 if (ev1
< mddev
->events
)
1233 } else if (mddev
->bitmap
) {
1234 /* if adding to array with a bitmap, then we can accept an
1235 * older device ... but not too old.
1237 if (ev1
< mddev
->bitmap
->events_cleared
)
1240 if (ev1
< mddev
->events
)
1241 /* just a hot-add of a new device, leave raid_disk at -1 */
1245 if (mddev
->level
!= LEVEL_MULTIPATH
) {
1246 desc
= sb
->disks
+ rdev
->desc_nr
;
1248 if (desc
->state
& (1<<MD_DISK_FAULTY
))
1249 set_bit(Faulty
, &rdev
->flags
);
1250 else if (desc
->state
& (1<<MD_DISK_SYNC
) /* &&
1251 desc->raid_disk < mddev->raid_disks */) {
1252 set_bit(In_sync
, &rdev
->flags
);
1253 rdev
->raid_disk
= desc
->raid_disk
;
1254 } else if (desc
->state
& (1<<MD_DISK_ACTIVE
)) {
1255 /* active but not in sync implies recovery up to
1256 * reshape position. We don't know exactly where
1257 * that is, so set to zero for now */
1258 if (mddev
->minor_version
>= 91) {
1259 rdev
->recovery_offset
= 0;
1260 rdev
->raid_disk
= desc
->raid_disk
;
1263 if (desc
->state
& (1<<MD_DISK_WRITEMOSTLY
))
1264 set_bit(WriteMostly
, &rdev
->flags
);
1265 } else /* MULTIPATH are always insync */
1266 set_bit(In_sync
, &rdev
->flags
);
1271 * sync_super for 0.90.0
1273 static void super_90_sync(struct mddev
*mddev
, struct md_rdev
*rdev
)
1276 struct md_rdev
*rdev2
;
1277 int next_spare
= mddev
->raid_disks
;
1280 /* make rdev->sb match mddev data..
1283 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1284 * 3/ any empty disks < next_spare become removed
1286 * disks[0] gets initialised to REMOVED because
1287 * we cannot be sure from other fields if it has
1288 * been initialised or not.
1291 int active
=0, working
=0,failed
=0,spare
=0,nr_disks
=0;
1293 rdev
->sb_size
= MD_SB_BYTES
;
1295 sb
= page_address(rdev
->sb_page
);
1297 memset(sb
, 0, sizeof(*sb
));
1299 sb
->md_magic
= MD_SB_MAGIC
;
1300 sb
->major_version
= mddev
->major_version
;
1301 sb
->patch_version
= mddev
->patch_version
;
1302 sb
->gvalid_words
= 0; /* ignored */
1303 memcpy(&sb
->set_uuid0
, mddev
->uuid
+0, 4);
1304 memcpy(&sb
->set_uuid1
, mddev
->uuid
+4, 4);
1305 memcpy(&sb
->set_uuid2
, mddev
->uuid
+8, 4);
1306 memcpy(&sb
->set_uuid3
, mddev
->uuid
+12,4);
1308 sb
->ctime
= mddev
->ctime
;
1309 sb
->level
= mddev
->level
;
1310 sb
->size
= mddev
->dev_sectors
/ 2;
1311 sb
->raid_disks
= mddev
->raid_disks
;
1312 sb
->md_minor
= mddev
->md_minor
;
1313 sb
->not_persistent
= 0;
1314 sb
->utime
= mddev
->utime
;
1316 sb
->events_hi
= (mddev
->events
>>32);
1317 sb
->events_lo
= (u32
)mddev
->events
;
1319 if (mddev
->reshape_position
== MaxSector
)
1320 sb
->minor_version
= 90;
1322 sb
->minor_version
= 91;
1323 sb
->reshape_position
= mddev
->reshape_position
;
1324 sb
->new_level
= mddev
->new_level
;
1325 sb
->delta_disks
= mddev
->delta_disks
;
1326 sb
->new_layout
= mddev
->new_layout
;
1327 sb
->new_chunk
= mddev
->new_chunk_sectors
<< 9;
1329 mddev
->minor_version
= sb
->minor_version
;
1332 sb
->recovery_cp
= mddev
->recovery_cp
;
1333 sb
->cp_events_hi
= (mddev
->events
>>32);
1334 sb
->cp_events_lo
= (u32
)mddev
->events
;
1335 if (mddev
->recovery_cp
== MaxSector
)
1336 sb
->state
= (1<< MD_SB_CLEAN
);
1338 sb
->recovery_cp
= 0;
1340 sb
->layout
= mddev
->layout
;
1341 sb
->chunk_size
= mddev
->chunk_sectors
<< 9;
1343 if (mddev
->bitmap
&& mddev
->bitmap_info
.file
== NULL
)
1344 sb
->state
|= (1<<MD_SB_BITMAP_PRESENT
);
1346 sb
->disks
[0].state
= (1<<MD_DISK_REMOVED
);
1347 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
) {
1350 int is_active
= test_bit(In_sync
, &rdev2
->flags
);
1352 if (rdev2
->raid_disk
>= 0 &&
1353 sb
->minor_version
>= 91)
1354 /* we have nowhere to store the recovery_offset,
1355 * but if it is not below the reshape_position,
1356 * we can piggy-back on that.
1359 if (rdev2
->raid_disk
< 0 ||
1360 test_bit(Faulty
, &rdev2
->flags
))
1363 desc_nr
= rdev2
->raid_disk
;
1365 desc_nr
= next_spare
++;
1366 rdev2
->desc_nr
= desc_nr
;
1367 d
= &sb
->disks
[rdev2
->desc_nr
];
1369 d
->number
= rdev2
->desc_nr
;
1370 d
->major
= MAJOR(rdev2
->bdev
->bd_dev
);
1371 d
->minor
= MINOR(rdev2
->bdev
->bd_dev
);
1373 d
->raid_disk
= rdev2
->raid_disk
;
1375 d
->raid_disk
= rdev2
->desc_nr
; /* compatibility */
1376 if (test_bit(Faulty
, &rdev2
->flags
))
1377 d
->state
= (1<<MD_DISK_FAULTY
);
1378 else if (is_active
) {
1379 d
->state
= (1<<MD_DISK_ACTIVE
);
1380 if (test_bit(In_sync
, &rdev2
->flags
))
1381 d
->state
|= (1<<MD_DISK_SYNC
);
1389 if (test_bit(WriteMostly
, &rdev2
->flags
))
1390 d
->state
|= (1<<MD_DISK_WRITEMOSTLY
);
1392 /* now set the "removed" and "faulty" bits on any missing devices */
1393 for (i
=0 ; i
< mddev
->raid_disks
; i
++) {
1394 mdp_disk_t
*d
= &sb
->disks
[i
];
1395 if (d
->state
== 0 && d
->number
== 0) {
1398 d
->state
= (1<<MD_DISK_REMOVED
);
1399 d
->state
|= (1<<MD_DISK_FAULTY
);
1403 sb
->nr_disks
= nr_disks
;
1404 sb
->active_disks
= active
;
1405 sb
->working_disks
= working
;
1406 sb
->failed_disks
= failed
;
1407 sb
->spare_disks
= spare
;
1409 sb
->this_disk
= sb
->disks
[rdev
->desc_nr
];
1410 sb
->sb_csum
= calc_sb_csum(sb
);
1414 * rdev_size_change for 0.90.0
1416 static unsigned long long
1417 super_90_rdev_size_change(struct md_rdev
*rdev
, sector_t num_sectors
)
1419 if (num_sectors
&& num_sectors
< rdev
->mddev
->dev_sectors
)
1420 return 0; /* component must fit device */
1421 if (rdev
->mddev
->bitmap_info
.offset
)
1422 return 0; /* can't move bitmap */
1423 rdev
->sb_start
= calc_dev_sboffset(rdev
);
1424 if (!num_sectors
|| num_sectors
> rdev
->sb_start
)
1425 num_sectors
= rdev
->sb_start
;
1426 /* Limit to 4TB as metadata cannot record more than that.
1427 * 4TB == 2^32 KB, or 2*2^32 sectors.
1429 if (num_sectors
>= (2ULL << 32))
1430 num_sectors
= (2ULL << 32) - 2;
1431 md_super_write(rdev
->mddev
, rdev
, rdev
->sb_start
, rdev
->sb_size
,
1433 md_super_wait(rdev
->mddev
);
1439 * version 1 superblock
1442 static __le32
calc_sb_1_csum(struct mdp_superblock_1
* sb
)
1446 unsigned long long newcsum
;
1447 int size
= 256 + le32_to_cpu(sb
->max_dev
)*2;
1448 __le32
*isuper
= (__le32
*)sb
;
1451 disk_csum
= sb
->sb_csum
;
1454 for (i
=0; size
>=4; size
-= 4 )
1455 newcsum
+= le32_to_cpu(*isuper
++);
1458 newcsum
+= le16_to_cpu(*(__le16
*) isuper
);
1460 csum
= (newcsum
& 0xffffffff) + (newcsum
>> 32);
1461 sb
->sb_csum
= disk_csum
;
1462 return cpu_to_le32(csum
);
1465 static int md_set_badblocks(struct badblocks
*bb
, sector_t s
, int sectors
,
1467 static int super_1_load(struct md_rdev
*rdev
, struct md_rdev
*refdev
, int minor_version
)
1469 struct mdp_superblock_1
*sb
;
1472 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
1476 * Calculate the position of the superblock in 512byte sectors.
1477 * It is always aligned to a 4K boundary and
1478 * depeding on minor_version, it can be:
1479 * 0: At least 8K, but less than 12K, from end of device
1480 * 1: At start of device
1481 * 2: 4K from start of device.
1483 switch(minor_version
) {
1485 sb_start
= i_size_read(rdev
->bdev
->bd_inode
) >> 9;
1487 sb_start
&= ~(sector_t
)(4*2-1);
1498 rdev
->sb_start
= sb_start
;
1500 /* superblock is rarely larger than 1K, but it can be larger,
1501 * and it is safe to read 4k, so we do that
1503 ret
= read_disk_sb(rdev
, 4096);
1504 if (ret
) return ret
;
1507 sb
= page_address(rdev
->sb_page
);
1509 if (sb
->magic
!= cpu_to_le32(MD_SB_MAGIC
) ||
1510 sb
->major_version
!= cpu_to_le32(1) ||
1511 le32_to_cpu(sb
->max_dev
) > (4096-256)/2 ||
1512 le64_to_cpu(sb
->super_offset
) != rdev
->sb_start
||
1513 (le32_to_cpu(sb
->feature_map
) & ~MD_FEATURE_ALL
) != 0)
1516 if (calc_sb_1_csum(sb
) != sb
->sb_csum
) {
1517 printk("md: invalid superblock checksum on %s\n",
1518 bdevname(rdev
->bdev
,b
));
1521 if (le64_to_cpu(sb
->data_size
) < 10) {
1522 printk("md: data_size too small on %s\n",
1523 bdevname(rdev
->bdev
,b
));
1527 rdev
->preferred_minor
= 0xffff;
1528 rdev
->data_offset
= le64_to_cpu(sb
->data_offset
);
1529 atomic_set(&rdev
->corrected_errors
, le32_to_cpu(sb
->cnt_corrected_read
));
1531 rdev
->sb_size
= le32_to_cpu(sb
->max_dev
) * 2 + 256;
1532 bmask
= queue_logical_block_size(rdev
->bdev
->bd_disk
->queue
)-1;
1533 if (rdev
->sb_size
& bmask
)
1534 rdev
->sb_size
= (rdev
->sb_size
| bmask
) + 1;
1537 && rdev
->data_offset
< sb_start
+ (rdev
->sb_size
/512))
1540 if (sb
->level
== cpu_to_le32(LEVEL_MULTIPATH
))
1543 rdev
->desc_nr
= le32_to_cpu(sb
->dev_number
);
1545 if (!rdev
->bb_page
) {
1546 rdev
->bb_page
= alloc_page(GFP_KERNEL
);
1550 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_BAD_BLOCKS
) &&
1551 rdev
->badblocks
.count
== 0) {
1552 /* need to load the bad block list.
1553 * Currently we limit it to one page.
1559 int sectors
= le16_to_cpu(sb
->bblog_size
);
1560 if (sectors
> (PAGE_SIZE
/ 512))
1562 offset
= le32_to_cpu(sb
->bblog_offset
);
1565 bb_sector
= (long long)offset
;
1566 if (!sync_page_io(rdev
, bb_sector
, sectors
<< 9,
1567 rdev
->bb_page
, READ
, true))
1569 bbp
= (u64
*)page_address(rdev
->bb_page
);
1570 rdev
->badblocks
.shift
= sb
->bblog_shift
;
1571 for (i
= 0 ; i
< (sectors
<< (9-3)) ; i
++, bbp
++) {
1572 u64 bb
= le64_to_cpu(*bbp
);
1573 int count
= bb
& (0x3ff);
1574 u64 sector
= bb
>> 10;
1575 sector
<<= sb
->bblog_shift
;
1576 count
<<= sb
->bblog_shift
;
1579 if (md_set_badblocks(&rdev
->badblocks
,
1580 sector
, count
, 1) == 0)
1583 } else if (sb
->bblog_offset
== 0)
1584 rdev
->badblocks
.shift
= -1;
1590 struct mdp_superblock_1
*refsb
= page_address(refdev
->sb_page
);
1592 if (memcmp(sb
->set_uuid
, refsb
->set_uuid
, 16) != 0 ||
1593 sb
->level
!= refsb
->level
||
1594 sb
->layout
!= refsb
->layout
||
1595 sb
->chunksize
!= refsb
->chunksize
) {
1596 printk(KERN_WARNING
"md: %s has strangely different"
1597 " superblock to %s\n",
1598 bdevname(rdev
->bdev
,b
),
1599 bdevname(refdev
->bdev
,b2
));
1602 ev1
= le64_to_cpu(sb
->events
);
1603 ev2
= le64_to_cpu(refsb
->events
);
1611 rdev
->sectors
= (i_size_read(rdev
->bdev
->bd_inode
) >> 9) -
1612 le64_to_cpu(sb
->data_offset
);
1614 rdev
->sectors
= rdev
->sb_start
;
1615 if (rdev
->sectors
< le64_to_cpu(sb
->data_size
))
1617 rdev
->sectors
= le64_to_cpu(sb
->data_size
);
1618 if (le64_to_cpu(sb
->size
) > rdev
->sectors
)
1623 static int super_1_validate(struct mddev
*mddev
, struct md_rdev
*rdev
)
1625 struct mdp_superblock_1
*sb
= page_address(rdev
->sb_page
);
1626 __u64 ev1
= le64_to_cpu(sb
->events
);
1628 rdev
->raid_disk
= -1;
1629 clear_bit(Faulty
, &rdev
->flags
);
1630 clear_bit(In_sync
, &rdev
->flags
);
1631 clear_bit(WriteMostly
, &rdev
->flags
);
1633 if (mddev
->raid_disks
== 0) {
1634 mddev
->major_version
= 1;
1635 mddev
->patch_version
= 0;
1636 mddev
->external
= 0;
1637 mddev
->chunk_sectors
= le32_to_cpu(sb
->chunksize
);
1638 mddev
->ctime
= le64_to_cpu(sb
->ctime
) & ((1ULL << 32)-1);
1639 mddev
->utime
= le64_to_cpu(sb
->utime
) & ((1ULL << 32)-1);
1640 mddev
->level
= le32_to_cpu(sb
->level
);
1641 mddev
->clevel
[0] = 0;
1642 mddev
->layout
= le32_to_cpu(sb
->layout
);
1643 mddev
->raid_disks
= le32_to_cpu(sb
->raid_disks
);
1644 mddev
->dev_sectors
= le64_to_cpu(sb
->size
);
1645 mddev
->events
= ev1
;
1646 mddev
->bitmap_info
.offset
= 0;
1647 mddev
->bitmap_info
.default_offset
= 1024 >> 9;
1649 mddev
->recovery_cp
= le64_to_cpu(sb
->resync_offset
);
1650 memcpy(mddev
->uuid
, sb
->set_uuid
, 16);
1652 mddev
->max_disks
= (4096-256)/2;
1654 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_BITMAP_OFFSET
) &&
1655 mddev
->bitmap_info
.file
== NULL
)
1656 mddev
->bitmap_info
.offset
=
1657 (__s32
)le32_to_cpu(sb
->bitmap_offset
);
1659 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RESHAPE_ACTIVE
)) {
1660 mddev
->reshape_position
= le64_to_cpu(sb
->reshape_position
);
1661 mddev
->delta_disks
= le32_to_cpu(sb
->delta_disks
);
1662 mddev
->new_level
= le32_to_cpu(sb
->new_level
);
1663 mddev
->new_layout
= le32_to_cpu(sb
->new_layout
);
1664 mddev
->new_chunk_sectors
= le32_to_cpu(sb
->new_chunk
);
1666 mddev
->reshape_position
= MaxSector
;
1667 mddev
->delta_disks
= 0;
1668 mddev
->new_level
= mddev
->level
;
1669 mddev
->new_layout
= mddev
->layout
;
1670 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
1673 } else if (mddev
->pers
== NULL
) {
1674 /* Insist of good event counter while assembling, except for
1675 * spares (which don't need an event count) */
1677 if (rdev
->desc_nr
>= 0 &&
1678 rdev
->desc_nr
< le32_to_cpu(sb
->max_dev
) &&
1679 le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]) < 0xfffe)
1680 if (ev1
< mddev
->events
)
1682 } else if (mddev
->bitmap
) {
1683 /* If adding to array with a bitmap, then we can accept an
1684 * older device, but not too old.
1686 if (ev1
< mddev
->bitmap
->events_cleared
)
1689 if (ev1
< mddev
->events
)
1690 /* just a hot-add of a new device, leave raid_disk at -1 */
1693 if (mddev
->level
!= LEVEL_MULTIPATH
) {
1695 if (rdev
->desc_nr
< 0 ||
1696 rdev
->desc_nr
>= le32_to_cpu(sb
->max_dev
)) {
1700 role
= le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]);
1702 case 0xffff: /* spare */
1704 case 0xfffe: /* faulty */
1705 set_bit(Faulty
, &rdev
->flags
);
1708 if ((le32_to_cpu(sb
->feature_map
) &
1709 MD_FEATURE_RECOVERY_OFFSET
))
1710 rdev
->recovery_offset
= le64_to_cpu(sb
->recovery_offset
);
1712 set_bit(In_sync
, &rdev
->flags
);
1713 rdev
->raid_disk
= role
;
1716 if (sb
->devflags
& WriteMostly1
)
1717 set_bit(WriteMostly
, &rdev
->flags
);
1718 if (le32_to_cpu(sb
->feature_map
) & MD_FEATURE_REPLACEMENT
)
1719 set_bit(Replacement
, &rdev
->flags
);
1720 } else /* MULTIPATH are always insync */
1721 set_bit(In_sync
, &rdev
->flags
);
1726 static void super_1_sync(struct mddev
*mddev
, struct md_rdev
*rdev
)
1728 struct mdp_superblock_1
*sb
;
1729 struct md_rdev
*rdev2
;
1731 /* make rdev->sb match mddev and rdev data. */
1733 sb
= page_address(rdev
->sb_page
);
1735 sb
->feature_map
= 0;
1737 sb
->recovery_offset
= cpu_to_le64(0);
1738 memset(sb
->pad1
, 0, sizeof(sb
->pad1
));
1739 memset(sb
->pad3
, 0, sizeof(sb
->pad3
));
1741 sb
->utime
= cpu_to_le64((__u64
)mddev
->utime
);
1742 sb
->events
= cpu_to_le64(mddev
->events
);
1744 sb
->resync_offset
= cpu_to_le64(mddev
->recovery_cp
);
1746 sb
->resync_offset
= cpu_to_le64(0);
1748 sb
->cnt_corrected_read
= cpu_to_le32(atomic_read(&rdev
->corrected_errors
));
1750 sb
->raid_disks
= cpu_to_le32(mddev
->raid_disks
);
1751 sb
->size
= cpu_to_le64(mddev
->dev_sectors
);
1752 sb
->chunksize
= cpu_to_le32(mddev
->chunk_sectors
);
1753 sb
->level
= cpu_to_le32(mddev
->level
);
1754 sb
->layout
= cpu_to_le32(mddev
->layout
);
1756 if (test_bit(WriteMostly
, &rdev
->flags
))
1757 sb
->devflags
|= WriteMostly1
;
1759 sb
->devflags
&= ~WriteMostly1
;
1761 if (mddev
->bitmap
&& mddev
->bitmap_info
.file
== NULL
) {
1762 sb
->bitmap_offset
= cpu_to_le32((__u32
)mddev
->bitmap_info
.offset
);
1763 sb
->feature_map
= cpu_to_le32(MD_FEATURE_BITMAP_OFFSET
);
1766 if (rdev
->raid_disk
>= 0 &&
1767 !test_bit(In_sync
, &rdev
->flags
)) {
1769 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET
);
1770 sb
->recovery_offset
=
1771 cpu_to_le64(rdev
->recovery_offset
);
1773 if (test_bit(Replacement
, &rdev
->flags
))
1775 cpu_to_le32(MD_FEATURE_REPLACEMENT
);
1777 if (mddev
->reshape_position
!= MaxSector
) {
1778 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE
);
1779 sb
->reshape_position
= cpu_to_le64(mddev
->reshape_position
);
1780 sb
->new_layout
= cpu_to_le32(mddev
->new_layout
);
1781 sb
->delta_disks
= cpu_to_le32(mddev
->delta_disks
);
1782 sb
->new_level
= cpu_to_le32(mddev
->new_level
);
1783 sb
->new_chunk
= cpu_to_le32(mddev
->new_chunk_sectors
);
1786 if (rdev
->badblocks
.count
== 0)
1787 /* Nothing to do for bad blocks*/ ;
1788 else if (sb
->bblog_offset
== 0)
1789 /* Cannot record bad blocks on this device */
1790 md_error(mddev
, rdev
);
1792 struct badblocks
*bb
= &rdev
->badblocks
;
1793 u64
*bbp
= (u64
*)page_address(rdev
->bb_page
);
1795 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_BAD_BLOCKS
);
1800 seq
= read_seqbegin(&bb
->lock
);
1802 memset(bbp
, 0xff, PAGE_SIZE
);
1804 for (i
= 0 ; i
< bb
->count
; i
++) {
1805 u64 internal_bb
= *p
++;
1806 u64 store_bb
= ((BB_OFFSET(internal_bb
) << 10)
1807 | BB_LEN(internal_bb
));
1808 *bbp
++ = cpu_to_le64(store_bb
);
1811 if (read_seqretry(&bb
->lock
, seq
))
1814 bb
->sector
= (rdev
->sb_start
+
1815 (int)le32_to_cpu(sb
->bblog_offset
));
1816 bb
->size
= le16_to_cpu(sb
->bblog_size
);
1821 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
)
1822 if (rdev2
->desc_nr
+1 > max_dev
)
1823 max_dev
= rdev2
->desc_nr
+1;
1825 if (max_dev
> le32_to_cpu(sb
->max_dev
)) {
1827 sb
->max_dev
= cpu_to_le32(max_dev
);
1828 rdev
->sb_size
= max_dev
* 2 + 256;
1829 bmask
= queue_logical_block_size(rdev
->bdev
->bd_disk
->queue
)-1;
1830 if (rdev
->sb_size
& bmask
)
1831 rdev
->sb_size
= (rdev
->sb_size
| bmask
) + 1;
1833 max_dev
= le32_to_cpu(sb
->max_dev
);
1835 for (i
=0; i
<max_dev
;i
++)
1836 sb
->dev_roles
[i
] = cpu_to_le16(0xfffe);
1838 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
) {
1840 if (test_bit(Faulty
, &rdev2
->flags
))
1841 sb
->dev_roles
[i
] = cpu_to_le16(0xfffe);
1842 else if (test_bit(In_sync
, &rdev2
->flags
))
1843 sb
->dev_roles
[i
] = cpu_to_le16(rdev2
->raid_disk
);
1844 else if (rdev2
->raid_disk
>= 0)
1845 sb
->dev_roles
[i
] = cpu_to_le16(rdev2
->raid_disk
);
1847 sb
->dev_roles
[i
] = cpu_to_le16(0xffff);
1850 sb
->sb_csum
= calc_sb_1_csum(sb
);
1853 static unsigned long long
1854 super_1_rdev_size_change(struct md_rdev
*rdev
, sector_t num_sectors
)
1856 struct mdp_superblock_1
*sb
;
1857 sector_t max_sectors
;
1858 if (num_sectors
&& num_sectors
< rdev
->mddev
->dev_sectors
)
1859 return 0; /* component must fit device */
1860 if (rdev
->sb_start
< rdev
->data_offset
) {
1861 /* minor versions 1 and 2; superblock before data */
1862 max_sectors
= i_size_read(rdev
->bdev
->bd_inode
) >> 9;
1863 max_sectors
-= rdev
->data_offset
;
1864 if (!num_sectors
|| num_sectors
> max_sectors
)
1865 num_sectors
= max_sectors
;
1866 } else if (rdev
->mddev
->bitmap_info
.offset
) {
1867 /* minor version 0 with bitmap we can't move */
1870 /* minor version 0; superblock after data */
1872 sb_start
= (i_size_read(rdev
->bdev
->bd_inode
) >> 9) - 8*2;
1873 sb_start
&= ~(sector_t
)(4*2 - 1);
1874 max_sectors
= rdev
->sectors
+ sb_start
- rdev
->sb_start
;
1875 if (!num_sectors
|| num_sectors
> max_sectors
)
1876 num_sectors
= max_sectors
;
1877 rdev
->sb_start
= sb_start
;
1879 sb
= page_address(rdev
->sb_page
);
1880 sb
->data_size
= cpu_to_le64(num_sectors
);
1881 sb
->super_offset
= rdev
->sb_start
;
1882 sb
->sb_csum
= calc_sb_1_csum(sb
);
1883 md_super_write(rdev
->mddev
, rdev
, rdev
->sb_start
, rdev
->sb_size
,
1885 md_super_wait(rdev
->mddev
);
1889 static struct super_type super_types
[] = {
1892 .owner
= THIS_MODULE
,
1893 .load_super
= super_90_load
,
1894 .validate_super
= super_90_validate
,
1895 .sync_super
= super_90_sync
,
1896 .rdev_size_change
= super_90_rdev_size_change
,
1900 .owner
= THIS_MODULE
,
1901 .load_super
= super_1_load
,
1902 .validate_super
= super_1_validate
,
1903 .sync_super
= super_1_sync
,
1904 .rdev_size_change
= super_1_rdev_size_change
,
1908 static void sync_super(struct mddev
*mddev
, struct md_rdev
*rdev
)
1910 if (mddev
->sync_super
) {
1911 mddev
->sync_super(mddev
, rdev
);
1915 BUG_ON(mddev
->major_version
>= ARRAY_SIZE(super_types
));
1917 super_types
[mddev
->major_version
].sync_super(mddev
, rdev
);
1920 static int match_mddev_units(struct mddev
*mddev1
, struct mddev
*mddev2
)
1922 struct md_rdev
*rdev
, *rdev2
;
1925 rdev_for_each_rcu(rdev
, mddev1
)
1926 rdev_for_each_rcu(rdev2
, mddev2
)
1927 if (rdev
->bdev
->bd_contains
==
1928 rdev2
->bdev
->bd_contains
) {
1936 static LIST_HEAD(pending_raid_disks
);
1939 * Try to register data integrity profile for an mddev
1941 * This is called when an array is started and after a disk has been kicked
1942 * from the array. It only succeeds if all working and active component devices
1943 * are integrity capable with matching profiles.
1945 int md_integrity_register(struct mddev
*mddev
)
1947 struct md_rdev
*rdev
, *reference
= NULL
;
1949 if (list_empty(&mddev
->disks
))
1950 return 0; /* nothing to do */
1951 if (!mddev
->gendisk
|| blk_get_integrity(mddev
->gendisk
))
1952 return 0; /* shouldn't register, or already is */
1953 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
1954 /* skip spares and non-functional disks */
1955 if (test_bit(Faulty
, &rdev
->flags
))
1957 if (rdev
->raid_disk
< 0)
1960 /* Use the first rdev as the reference */
1964 /* does this rdev's profile match the reference profile? */
1965 if (blk_integrity_compare(reference
->bdev
->bd_disk
,
1966 rdev
->bdev
->bd_disk
) < 0)
1969 if (!reference
|| !bdev_get_integrity(reference
->bdev
))
1972 * All component devices are integrity capable and have matching
1973 * profiles, register the common profile for the md device.
1975 if (blk_integrity_register(mddev
->gendisk
,
1976 bdev_get_integrity(reference
->bdev
)) != 0) {
1977 printk(KERN_ERR
"md: failed to register integrity for %s\n",
1981 printk(KERN_NOTICE
"md: data integrity enabled on %s\n", mdname(mddev
));
1982 if (bioset_integrity_create(mddev
->bio_set
, BIO_POOL_SIZE
)) {
1983 printk(KERN_ERR
"md: failed to create integrity pool for %s\n",
1989 EXPORT_SYMBOL(md_integrity_register
);
1991 /* Disable data integrity if non-capable/non-matching disk is being added */
1992 void md_integrity_add_rdev(struct md_rdev
*rdev
, struct mddev
*mddev
)
1994 struct blk_integrity
*bi_rdev
= bdev_get_integrity(rdev
->bdev
);
1995 struct blk_integrity
*bi_mddev
= blk_get_integrity(mddev
->gendisk
);
1997 if (!bi_mddev
) /* nothing to do */
1999 if (rdev
->raid_disk
< 0) /* skip spares */
2001 if (bi_rdev
&& blk_integrity_compare(mddev
->gendisk
,
2002 rdev
->bdev
->bd_disk
) >= 0)
2004 printk(KERN_NOTICE
"disabling data integrity on %s\n", mdname(mddev
));
2005 blk_integrity_unregister(mddev
->gendisk
);
2007 EXPORT_SYMBOL(md_integrity_add_rdev
);
2009 static int bind_rdev_to_array(struct md_rdev
* rdev
, struct mddev
* mddev
)
2011 char b
[BDEVNAME_SIZE
];
2021 /* prevent duplicates */
2022 if (find_rdev(mddev
, rdev
->bdev
->bd_dev
))
2025 /* make sure rdev->sectors exceeds mddev->dev_sectors */
2026 if (rdev
->sectors
&& (mddev
->dev_sectors
== 0 ||
2027 rdev
->sectors
< mddev
->dev_sectors
)) {
2029 /* Cannot change size, so fail
2030 * If mddev->level <= 0, then we don't care
2031 * about aligning sizes (e.g. linear)
2033 if (mddev
->level
> 0)
2036 mddev
->dev_sectors
= rdev
->sectors
;
2039 /* Verify rdev->desc_nr is unique.
2040 * If it is -1, assign a free number, else
2041 * check number is not in use
2043 if (rdev
->desc_nr
< 0) {
2045 if (mddev
->pers
) choice
= mddev
->raid_disks
;
2046 while (find_rdev_nr(mddev
, choice
))
2048 rdev
->desc_nr
= choice
;
2050 if (find_rdev_nr(mddev
, rdev
->desc_nr
))
2053 if (mddev
->max_disks
&& rdev
->desc_nr
>= mddev
->max_disks
) {
2054 printk(KERN_WARNING
"md: %s: array is limited to %d devices\n",
2055 mdname(mddev
), mddev
->max_disks
);
2058 bdevname(rdev
->bdev
,b
);
2059 while ( (s
=strchr(b
, '/')) != NULL
)
2062 rdev
->mddev
= mddev
;
2063 printk(KERN_INFO
"md: bind<%s>\n", b
);
2065 if ((err
= kobject_add(&rdev
->kobj
, &mddev
->kobj
, "dev-%s", b
)))
2068 ko
= &part_to_dev(rdev
->bdev
->bd_part
)->kobj
;
2069 if (sysfs_create_link(&rdev
->kobj
, ko
, "block"))
2070 /* failure here is OK */;
2071 rdev
->sysfs_state
= sysfs_get_dirent_safe(rdev
->kobj
.sd
, "state");
2073 list_add_rcu(&rdev
->same_set
, &mddev
->disks
);
2074 bd_link_disk_holder(rdev
->bdev
, mddev
->gendisk
);
2076 /* May as well allow recovery to be retried once */
2077 mddev
->recovery_disabled
++;
2082 printk(KERN_WARNING
"md: failed to register dev-%s for %s\n",
2087 static void md_delayed_delete(struct work_struct
*ws
)
2089 struct md_rdev
*rdev
= container_of(ws
, struct md_rdev
, del_work
);
2090 kobject_del(&rdev
->kobj
);
2091 kobject_put(&rdev
->kobj
);
2094 static void unbind_rdev_from_array(struct md_rdev
* rdev
)
2096 char b
[BDEVNAME_SIZE
];
2101 bd_unlink_disk_holder(rdev
->bdev
, rdev
->mddev
->gendisk
);
2102 list_del_rcu(&rdev
->same_set
);
2103 printk(KERN_INFO
"md: unbind<%s>\n", bdevname(rdev
->bdev
,b
));
2105 sysfs_remove_link(&rdev
->kobj
, "block");
2106 sysfs_put(rdev
->sysfs_state
);
2107 rdev
->sysfs_state
= NULL
;
2108 kfree(rdev
->badblocks
.page
);
2109 rdev
->badblocks
.count
= 0;
2110 rdev
->badblocks
.page
= NULL
;
2111 /* We need to delay this, otherwise we can deadlock when
2112 * writing to 'remove' to "dev/state". We also need
2113 * to delay it due to rcu usage.
2116 INIT_WORK(&rdev
->del_work
, md_delayed_delete
);
2117 kobject_get(&rdev
->kobj
);
2118 queue_work(md_misc_wq
, &rdev
->del_work
);
2122 * prevent the device from being mounted, repartitioned or
2123 * otherwise reused by a RAID array (or any other kernel
2124 * subsystem), by bd_claiming the device.
2126 static int lock_rdev(struct md_rdev
*rdev
, dev_t dev
, int shared
)
2129 struct block_device
*bdev
;
2130 char b
[BDEVNAME_SIZE
];
2132 bdev
= blkdev_get_by_dev(dev
, FMODE_READ
|FMODE_WRITE
|FMODE_EXCL
,
2133 shared
? (struct md_rdev
*)lock_rdev
: rdev
);
2135 printk(KERN_ERR
"md: could not open %s.\n",
2136 __bdevname(dev
, b
));
2137 return PTR_ERR(bdev
);
2143 static void unlock_rdev(struct md_rdev
*rdev
)
2145 struct block_device
*bdev
= rdev
->bdev
;
2149 blkdev_put(bdev
, FMODE_READ
|FMODE_WRITE
|FMODE_EXCL
);
2152 void md_autodetect_dev(dev_t dev
);
2154 static void export_rdev(struct md_rdev
* rdev
)
2156 char b
[BDEVNAME_SIZE
];
2157 printk(KERN_INFO
"md: export_rdev(%s)\n",
2158 bdevname(rdev
->bdev
,b
));
2163 if (test_bit(AutoDetected
, &rdev
->flags
))
2164 md_autodetect_dev(rdev
->bdev
->bd_dev
);
2167 kobject_put(&rdev
->kobj
);
2170 static void kick_rdev_from_array(struct md_rdev
* rdev
)
2172 unbind_rdev_from_array(rdev
);
2176 static void export_array(struct mddev
*mddev
)
2178 struct md_rdev
*rdev
, *tmp
;
2180 rdev_for_each(rdev
, tmp
, mddev
) {
2185 kick_rdev_from_array(rdev
);
2187 if (!list_empty(&mddev
->disks
))
2189 mddev
->raid_disks
= 0;
2190 mddev
->major_version
= 0;
2193 static void print_desc(mdp_disk_t
*desc
)
2195 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc
->number
,
2196 desc
->major
,desc
->minor
,desc
->raid_disk
,desc
->state
);
2199 static void print_sb_90(mdp_super_t
*sb
)
2204 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
2205 sb
->major_version
, sb
->minor_version
, sb
->patch_version
,
2206 sb
->set_uuid0
, sb
->set_uuid1
, sb
->set_uuid2
, sb
->set_uuid3
,
2208 printk(KERN_INFO
"md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
2209 sb
->level
, sb
->size
, sb
->nr_disks
, sb
->raid_disks
,
2210 sb
->md_minor
, sb
->layout
, sb
->chunk_size
);
2211 printk(KERN_INFO
"md: UT:%08x ST:%d AD:%d WD:%d"
2212 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
2213 sb
->utime
, sb
->state
, sb
->active_disks
, sb
->working_disks
,
2214 sb
->failed_disks
, sb
->spare_disks
,
2215 sb
->sb_csum
, (unsigned long)sb
->events_lo
);
2218 for (i
= 0; i
< MD_SB_DISKS
; i
++) {
2221 desc
= sb
->disks
+ i
;
2222 if (desc
->number
|| desc
->major
|| desc
->minor
||
2223 desc
->raid_disk
|| (desc
->state
&& (desc
->state
!= 4))) {
2224 printk(" D %2d: ", i
);
2228 printk(KERN_INFO
"md: THIS: ");
2229 print_desc(&sb
->this_disk
);
2232 static void print_sb_1(struct mdp_superblock_1
*sb
)
2236 uuid
= sb
->set_uuid
;
2238 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n"
2239 "md: Name: \"%s\" CT:%llu\n",
2240 le32_to_cpu(sb
->major_version
),
2241 le32_to_cpu(sb
->feature_map
),
2244 (unsigned long long)le64_to_cpu(sb
->ctime
)
2245 & MD_SUPERBLOCK_1_TIME_SEC_MASK
);
2247 uuid
= sb
->device_uuid
;
2249 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
2251 "md: Dev:%08x UUID: %pU\n"
2252 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
2253 "md: (MaxDev:%u) \n",
2254 le32_to_cpu(sb
->level
),
2255 (unsigned long long)le64_to_cpu(sb
->size
),
2256 le32_to_cpu(sb
->raid_disks
),
2257 le32_to_cpu(sb
->layout
),
2258 le32_to_cpu(sb
->chunksize
),
2259 (unsigned long long)le64_to_cpu(sb
->data_offset
),
2260 (unsigned long long)le64_to_cpu(sb
->data_size
),
2261 (unsigned long long)le64_to_cpu(sb
->super_offset
),
2262 (unsigned long long)le64_to_cpu(sb
->recovery_offset
),
2263 le32_to_cpu(sb
->dev_number
),
2266 (unsigned long long)le64_to_cpu(sb
->utime
) & MD_SUPERBLOCK_1_TIME_SEC_MASK
,
2267 (unsigned long long)le64_to_cpu(sb
->events
),
2268 (unsigned long long)le64_to_cpu(sb
->resync_offset
),
2269 le32_to_cpu(sb
->sb_csum
),
2270 le32_to_cpu(sb
->max_dev
)
2274 static void print_rdev(struct md_rdev
*rdev
, int major_version
)
2276 char b
[BDEVNAME_SIZE
];
2277 printk(KERN_INFO
"md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
2278 bdevname(rdev
->bdev
, b
), (unsigned long long)rdev
->sectors
,
2279 test_bit(Faulty
, &rdev
->flags
), test_bit(In_sync
, &rdev
->flags
),
2281 if (rdev
->sb_loaded
) {
2282 printk(KERN_INFO
"md: rdev superblock (MJ:%d):\n", major_version
);
2283 switch (major_version
) {
2285 print_sb_90(page_address(rdev
->sb_page
));
2288 print_sb_1(page_address(rdev
->sb_page
));
2292 printk(KERN_INFO
"md: no rdev superblock!\n");
2295 static void md_print_devices(void)
2297 struct list_head
*tmp
;
2298 struct md_rdev
*rdev
;
2299 struct mddev
*mddev
;
2300 char b
[BDEVNAME_SIZE
];
2303 printk("md: **********************************\n");
2304 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
2305 printk("md: **********************************\n");
2306 for_each_mddev(mddev
, tmp
) {
2309 bitmap_print_sb(mddev
->bitmap
);
2311 printk("%s: ", mdname(mddev
));
2312 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
2313 printk("<%s>", bdevname(rdev
->bdev
,b
));
2316 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
2317 print_rdev(rdev
, mddev
->major_version
);
2319 printk("md: **********************************\n");
2324 static void sync_sbs(struct mddev
* mddev
, int nospares
)
2326 /* Update each superblock (in-memory image), but
2327 * if we are allowed to, skip spares which already
2328 * have the right event counter, or have one earlier
2329 * (which would mean they aren't being marked as dirty
2330 * with the rest of the array)
2332 struct md_rdev
*rdev
;
2333 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
2334 if (rdev
->sb_events
== mddev
->events
||
2336 rdev
->raid_disk
< 0 &&
2337 rdev
->sb_events
+1 == mddev
->events
)) {
2338 /* Don't update this superblock */
2339 rdev
->sb_loaded
= 2;
2341 sync_super(mddev
, rdev
);
2342 rdev
->sb_loaded
= 1;
2347 static void md_update_sb(struct mddev
* mddev
, int force_change
)
2349 struct md_rdev
*rdev
;
2352 int any_badblocks_changed
= 0;
2355 /* First make sure individual recovery_offsets are correct */
2356 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
2357 if (rdev
->raid_disk
>= 0 &&
2358 mddev
->delta_disks
>= 0 &&
2359 !test_bit(In_sync
, &rdev
->flags
) &&
2360 mddev
->curr_resync_completed
> rdev
->recovery_offset
)
2361 rdev
->recovery_offset
= mddev
->curr_resync_completed
;
2364 if (!mddev
->persistent
) {
2365 clear_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
2366 clear_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
2367 if (!mddev
->external
) {
2368 clear_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
2369 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
2370 if (rdev
->badblocks
.changed
) {
2371 rdev
->badblocks
.changed
= 0;
2372 md_ack_all_badblocks(&rdev
->badblocks
);
2373 md_error(mddev
, rdev
);
2375 clear_bit(Blocked
, &rdev
->flags
);
2376 clear_bit(BlockedBadBlocks
, &rdev
->flags
);
2377 wake_up(&rdev
->blocked_wait
);
2380 wake_up(&mddev
->sb_wait
);
2384 spin_lock_irq(&mddev
->write_lock
);
2386 mddev
->utime
= get_seconds();
2388 if (test_and_clear_bit(MD_CHANGE_DEVS
, &mddev
->flags
))
2390 if (test_and_clear_bit(MD_CHANGE_CLEAN
, &mddev
->flags
))
2391 /* just a clean<-> dirty transition, possibly leave spares alone,
2392 * though if events isn't the right even/odd, we will have to do
2398 if (mddev
->degraded
)
2399 /* If the array is degraded, then skipping spares is both
2400 * dangerous and fairly pointless.
2401 * Dangerous because a device that was removed from the array
2402 * might have a event_count that still looks up-to-date,
2403 * so it can be re-added without a resync.
2404 * Pointless because if there are any spares to skip,
2405 * then a recovery will happen and soon that array won't
2406 * be degraded any more and the spare can go back to sleep then.
2410 sync_req
= mddev
->in_sync
;
2412 /* If this is just a dirty<->clean transition, and the array is clean
2413 * and 'events' is odd, we can roll back to the previous clean state */
2415 && (mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
)
2416 && mddev
->can_decrease_events
2417 && mddev
->events
!= 1) {
2419 mddev
->can_decrease_events
= 0;
2421 /* otherwise we have to go forward and ... */
2423 mddev
->can_decrease_events
= nospares
;
2426 if (!mddev
->events
) {
2428 * oops, this 64-bit counter should never wrap.
2429 * Either we are in around ~1 trillion A.C., assuming
2430 * 1 reboot per second, or we have a bug:
2436 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
2437 if (rdev
->badblocks
.changed
)
2438 any_badblocks_changed
++;
2439 if (test_bit(Faulty
, &rdev
->flags
))
2440 set_bit(FaultRecorded
, &rdev
->flags
);
2443 sync_sbs(mddev
, nospares
);
2444 spin_unlock_irq(&mddev
->write_lock
);
2446 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2447 mdname(mddev
), mddev
->in_sync
);
2449 bitmap_update_sb(mddev
->bitmap
);
2450 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
2451 char b
[BDEVNAME_SIZE
];
2453 if (rdev
->sb_loaded
!= 1)
2454 continue; /* no noise on spare devices */
2456 if (!test_bit(Faulty
, &rdev
->flags
) &&
2457 rdev
->saved_raid_disk
== -1) {
2458 md_super_write(mddev
,rdev
,
2459 rdev
->sb_start
, rdev
->sb_size
,
2461 pr_debug("md: (write) %s's sb offset: %llu\n",
2462 bdevname(rdev
->bdev
, b
),
2463 (unsigned long long)rdev
->sb_start
);
2464 rdev
->sb_events
= mddev
->events
;
2465 if (rdev
->badblocks
.size
) {
2466 md_super_write(mddev
, rdev
,
2467 rdev
->badblocks
.sector
,
2468 rdev
->badblocks
.size
<< 9,
2470 rdev
->badblocks
.size
= 0;
2473 } else if (test_bit(Faulty
, &rdev
->flags
))
2474 pr_debug("md: %s (skipping faulty)\n",
2475 bdevname(rdev
->bdev
, b
));
2477 pr_debug("(skipping incremental s/r ");
2479 if (mddev
->level
== LEVEL_MULTIPATH
)
2480 /* only need to write one superblock... */
2483 md_super_wait(mddev
);
2484 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
2486 spin_lock_irq(&mddev
->write_lock
);
2487 if (mddev
->in_sync
!= sync_req
||
2488 test_bit(MD_CHANGE_DEVS
, &mddev
->flags
)) {
2489 /* have to write it out again */
2490 spin_unlock_irq(&mddev
->write_lock
);
2493 clear_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
2494 spin_unlock_irq(&mddev
->write_lock
);
2495 wake_up(&mddev
->sb_wait
);
2496 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
2497 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
2499 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
2500 if (test_and_clear_bit(FaultRecorded
, &rdev
->flags
))
2501 clear_bit(Blocked
, &rdev
->flags
);
2503 if (any_badblocks_changed
)
2504 md_ack_all_badblocks(&rdev
->badblocks
);
2505 clear_bit(BlockedBadBlocks
, &rdev
->flags
);
2506 wake_up(&rdev
->blocked_wait
);
2510 /* words written to sysfs files may, or may not, be \n terminated.
2511 * We want to accept with case. For this we use cmd_match.
2513 static int cmd_match(const char *cmd
, const char *str
)
2515 /* See if cmd, written into a sysfs file, matches
2516 * str. They must either be the same, or cmd can
2517 * have a trailing newline
2519 while (*cmd
&& *str
&& *cmd
== *str
) {
2530 struct rdev_sysfs_entry
{
2531 struct attribute attr
;
2532 ssize_t (*show
)(struct md_rdev
*, char *);
2533 ssize_t (*store
)(struct md_rdev
*, const char *, size_t);
2537 state_show(struct md_rdev
*rdev
, char *page
)
2542 if (test_bit(Faulty
, &rdev
->flags
) ||
2543 rdev
->badblocks
.unacked_exist
) {
2544 len
+= sprintf(page
+len
, "%sfaulty",sep
);
2547 if (test_bit(In_sync
, &rdev
->flags
)) {
2548 len
+= sprintf(page
+len
, "%sin_sync",sep
);
2551 if (test_bit(WriteMostly
, &rdev
->flags
)) {
2552 len
+= sprintf(page
+len
, "%swrite_mostly",sep
);
2555 if (test_bit(Blocked
, &rdev
->flags
) ||
2556 (rdev
->badblocks
.unacked_exist
2557 && !test_bit(Faulty
, &rdev
->flags
))) {
2558 len
+= sprintf(page
+len
, "%sblocked", sep
);
2561 if (!test_bit(Faulty
, &rdev
->flags
) &&
2562 !test_bit(In_sync
, &rdev
->flags
)) {
2563 len
+= sprintf(page
+len
, "%sspare", sep
);
2566 if (test_bit(WriteErrorSeen
, &rdev
->flags
)) {
2567 len
+= sprintf(page
+len
, "%swrite_error", sep
);
2570 if (test_bit(WantReplacement
, &rdev
->flags
)) {
2571 len
+= sprintf(page
+len
, "%swant_replacement", sep
);
2574 if (test_bit(Replacement
, &rdev
->flags
)) {
2575 len
+= sprintf(page
+len
, "%sreplacement", sep
);
2579 return len
+sprintf(page
+len
, "\n");
2583 state_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
2586 * faulty - simulates an error
2587 * remove - disconnects the device
2588 * writemostly - sets write_mostly
2589 * -writemostly - clears write_mostly
2590 * blocked - sets the Blocked flags
2591 * -blocked - clears the Blocked and possibly simulates an error
2592 * insync - sets Insync providing device isn't active
2593 * write_error - sets WriteErrorSeen
2594 * -write_error - clears WriteErrorSeen
2597 if (cmd_match(buf
, "faulty") && rdev
->mddev
->pers
) {
2598 md_error(rdev
->mddev
, rdev
);
2599 if (test_bit(Faulty
, &rdev
->flags
))
2603 } else if (cmd_match(buf
, "remove")) {
2604 if (rdev
->raid_disk
>= 0)
2607 struct mddev
*mddev
= rdev
->mddev
;
2608 kick_rdev_from_array(rdev
);
2610 md_update_sb(mddev
, 1);
2611 md_new_event(mddev
);
2614 } else if (cmd_match(buf
, "writemostly")) {
2615 set_bit(WriteMostly
, &rdev
->flags
);
2617 } else if (cmd_match(buf
, "-writemostly")) {
2618 clear_bit(WriteMostly
, &rdev
->flags
);
2620 } else if (cmd_match(buf
, "blocked")) {
2621 set_bit(Blocked
, &rdev
->flags
);
2623 } else if (cmd_match(buf
, "-blocked")) {
2624 if (!test_bit(Faulty
, &rdev
->flags
) &&
2625 rdev
->badblocks
.unacked_exist
) {
2626 /* metadata handler doesn't understand badblocks,
2627 * so we need to fail the device
2629 md_error(rdev
->mddev
, rdev
);
2631 clear_bit(Blocked
, &rdev
->flags
);
2632 clear_bit(BlockedBadBlocks
, &rdev
->flags
);
2633 wake_up(&rdev
->blocked_wait
);
2634 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
2635 md_wakeup_thread(rdev
->mddev
->thread
);
2638 } else if (cmd_match(buf
, "insync") && rdev
->raid_disk
== -1) {
2639 set_bit(In_sync
, &rdev
->flags
);
2641 } else if (cmd_match(buf
, "write_error")) {
2642 set_bit(WriteErrorSeen
, &rdev
->flags
);
2644 } else if (cmd_match(buf
, "-write_error")) {
2645 clear_bit(WriteErrorSeen
, &rdev
->flags
);
2647 } else if (cmd_match(buf
, "want_replacement")) {
2648 /* Any non-spare device that is not a replacement can
2649 * become want_replacement at any time, but we then need to
2650 * check if recovery is needed.
2652 if (rdev
->raid_disk
>= 0 &&
2653 !test_bit(Replacement
, &rdev
->flags
))
2654 set_bit(WantReplacement
, &rdev
->flags
);
2655 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
2656 md_wakeup_thread(rdev
->mddev
->thread
);
2658 } else if (cmd_match(buf
, "-want_replacement")) {
2659 /* Clearing 'want_replacement' is always allowed.
2660 * Once replacements starts it is too late though.
2663 clear_bit(WantReplacement
, &rdev
->flags
);
2664 } else if (cmd_match(buf
, "replacement")) {
2665 /* Can only set a device as a replacement when array has not
2666 * yet been started. Once running, replacement is automatic
2667 * from spares, or by assigning 'slot'.
2669 if (rdev
->mddev
->pers
)
2672 set_bit(Replacement
, &rdev
->flags
);
2675 } else if (cmd_match(buf
, "-replacement")) {
2676 /* Similarly, can only clear Replacement before start */
2677 if (rdev
->mddev
->pers
)
2680 clear_bit(Replacement
, &rdev
->flags
);
2685 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
2686 return err
? err
: len
;
2688 static struct rdev_sysfs_entry rdev_state
=
2689 __ATTR(state
, S_IRUGO
|S_IWUSR
, state_show
, state_store
);
2692 errors_show(struct md_rdev
*rdev
, char *page
)
2694 return sprintf(page
, "%d\n", atomic_read(&rdev
->corrected_errors
));
2698 errors_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
2701 unsigned long n
= simple_strtoul(buf
, &e
, 10);
2702 if (*buf
&& (*e
== 0 || *e
== '\n')) {
2703 atomic_set(&rdev
->corrected_errors
, n
);
2708 static struct rdev_sysfs_entry rdev_errors
=
2709 __ATTR(errors
, S_IRUGO
|S_IWUSR
, errors_show
, errors_store
);
2712 slot_show(struct md_rdev
*rdev
, char *page
)
2714 if (rdev
->raid_disk
< 0)
2715 return sprintf(page
, "none\n");
2717 return sprintf(page
, "%d\n", rdev
->raid_disk
);
2721 slot_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
2725 int slot
= simple_strtoul(buf
, &e
, 10);
2726 if (strncmp(buf
, "none", 4)==0)
2728 else if (e
==buf
|| (*e
&& *e
!= '\n'))
2730 if (rdev
->mddev
->pers
&& slot
== -1) {
2731 /* Setting 'slot' on an active array requires also
2732 * updating the 'rd%d' link, and communicating
2733 * with the personality with ->hot_*_disk.
2734 * For now we only support removing
2735 * failed/spare devices. This normally happens automatically,
2736 * but not when the metadata is externally managed.
2738 if (rdev
->raid_disk
== -1)
2740 /* personality does all needed checks */
2741 if (rdev
->mddev
->pers
->hot_remove_disk
== NULL
)
2743 err
= rdev
->mddev
->pers
->
2744 hot_remove_disk(rdev
->mddev
, rdev
);
2747 sysfs_unlink_rdev(rdev
->mddev
, rdev
);
2748 rdev
->raid_disk
= -1;
2749 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
2750 md_wakeup_thread(rdev
->mddev
->thread
);
2751 } else if (rdev
->mddev
->pers
) {
2752 /* Activating a spare .. or possibly reactivating
2753 * if we ever get bitmaps working here.
2756 if (rdev
->raid_disk
!= -1)
2759 if (test_bit(MD_RECOVERY_RUNNING
, &rdev
->mddev
->recovery
))
2762 if (rdev
->mddev
->pers
->hot_add_disk
== NULL
)
2765 if (slot
>= rdev
->mddev
->raid_disks
&&
2766 slot
>= rdev
->mddev
->raid_disks
+ rdev
->mddev
->delta_disks
)
2769 rdev
->raid_disk
= slot
;
2770 if (test_bit(In_sync
, &rdev
->flags
))
2771 rdev
->saved_raid_disk
= slot
;
2773 rdev
->saved_raid_disk
= -1;
2774 clear_bit(In_sync
, &rdev
->flags
);
2775 err
= rdev
->mddev
->pers
->
2776 hot_add_disk(rdev
->mddev
, rdev
);
2778 rdev
->raid_disk
= -1;
2781 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
2782 if (sysfs_link_rdev(rdev
->mddev
, rdev
))
2783 /* failure here is OK */;
2784 /* don't wakeup anyone, leave that to userspace. */
2786 if (slot
>= rdev
->mddev
->raid_disks
&&
2787 slot
>= rdev
->mddev
->raid_disks
+ rdev
->mddev
->delta_disks
)
2789 rdev
->raid_disk
= slot
;
2790 /* assume it is working */
2791 clear_bit(Faulty
, &rdev
->flags
);
2792 clear_bit(WriteMostly
, &rdev
->flags
);
2793 set_bit(In_sync
, &rdev
->flags
);
2794 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
2800 static struct rdev_sysfs_entry rdev_slot
=
2801 __ATTR(slot
, S_IRUGO
|S_IWUSR
, slot_show
, slot_store
);
2804 offset_show(struct md_rdev
*rdev
, char *page
)
2806 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->data_offset
);
2810 offset_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
2813 unsigned long long offset
= simple_strtoull(buf
, &e
, 10);
2814 if (e
==buf
|| (*e
&& *e
!= '\n'))
2816 if (rdev
->mddev
->pers
&& rdev
->raid_disk
>= 0)
2818 if (rdev
->sectors
&& rdev
->mddev
->external
)
2819 /* Must set offset before size, so overlap checks
2822 rdev
->data_offset
= offset
;
2826 static struct rdev_sysfs_entry rdev_offset
=
2827 __ATTR(offset
, S_IRUGO
|S_IWUSR
, offset_show
, offset_store
);
2830 rdev_size_show(struct md_rdev
*rdev
, char *page
)
2832 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->sectors
/ 2);
2835 static int overlaps(sector_t s1
, sector_t l1
, sector_t s2
, sector_t l2
)
2837 /* check if two start/length pairs overlap */
2845 static int strict_blocks_to_sectors(const char *buf
, sector_t
*sectors
)
2847 unsigned long long blocks
;
2850 if (strict_strtoull(buf
, 10, &blocks
) < 0)
2853 if (blocks
& 1ULL << (8 * sizeof(blocks
) - 1))
2854 return -EINVAL
; /* sector conversion overflow */
2857 if (new != blocks
* 2)
2858 return -EINVAL
; /* unsigned long long to sector_t overflow */
2865 rdev_size_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
2867 struct mddev
*my_mddev
= rdev
->mddev
;
2868 sector_t oldsectors
= rdev
->sectors
;
2871 if (strict_blocks_to_sectors(buf
, §ors
) < 0)
2873 if (my_mddev
->pers
&& rdev
->raid_disk
>= 0) {
2874 if (my_mddev
->persistent
) {
2875 sectors
= super_types
[my_mddev
->major_version
].
2876 rdev_size_change(rdev
, sectors
);
2879 } else if (!sectors
)
2880 sectors
= (i_size_read(rdev
->bdev
->bd_inode
) >> 9) -
2883 if (sectors
< my_mddev
->dev_sectors
)
2884 return -EINVAL
; /* component must fit device */
2886 rdev
->sectors
= sectors
;
2887 if (sectors
> oldsectors
&& my_mddev
->external
) {
2888 /* need to check that all other rdevs with the same ->bdev
2889 * do not overlap. We need to unlock the mddev to avoid
2890 * a deadlock. We have already changed rdev->sectors, and if
2891 * we have to change it back, we will have the lock again.
2893 struct mddev
*mddev
;
2895 struct list_head
*tmp
;
2897 mddev_unlock(my_mddev
);
2898 for_each_mddev(mddev
, tmp
) {
2899 struct md_rdev
*rdev2
;
2902 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
)
2903 if (rdev
->bdev
== rdev2
->bdev
&&
2905 overlaps(rdev
->data_offset
, rdev
->sectors
,
2911 mddev_unlock(mddev
);
2917 mddev_lock(my_mddev
);
2919 /* Someone else could have slipped in a size
2920 * change here, but doing so is just silly.
2921 * We put oldsectors back because we *know* it is
2922 * safe, and trust userspace not to race with
2925 rdev
->sectors
= oldsectors
;
2932 static struct rdev_sysfs_entry rdev_size
=
2933 __ATTR(size
, S_IRUGO
|S_IWUSR
, rdev_size_show
, rdev_size_store
);
2936 static ssize_t
recovery_start_show(struct md_rdev
*rdev
, char *page
)
2938 unsigned long long recovery_start
= rdev
->recovery_offset
;
2940 if (test_bit(In_sync
, &rdev
->flags
) ||
2941 recovery_start
== MaxSector
)
2942 return sprintf(page
, "none\n");
2944 return sprintf(page
, "%llu\n", recovery_start
);
2947 static ssize_t
recovery_start_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
2949 unsigned long long recovery_start
;
2951 if (cmd_match(buf
, "none"))
2952 recovery_start
= MaxSector
;
2953 else if (strict_strtoull(buf
, 10, &recovery_start
))
2956 if (rdev
->mddev
->pers
&&
2957 rdev
->raid_disk
>= 0)
2960 rdev
->recovery_offset
= recovery_start
;
2961 if (recovery_start
== MaxSector
)
2962 set_bit(In_sync
, &rdev
->flags
);
2964 clear_bit(In_sync
, &rdev
->flags
);
2968 static struct rdev_sysfs_entry rdev_recovery_start
=
2969 __ATTR(recovery_start
, S_IRUGO
|S_IWUSR
, recovery_start_show
, recovery_start_store
);
2973 badblocks_show(struct badblocks
*bb
, char *page
, int unack
);
2975 badblocks_store(struct badblocks
*bb
, const char *page
, size_t len
, int unack
);
2977 static ssize_t
bb_show(struct md_rdev
*rdev
, char *page
)
2979 return badblocks_show(&rdev
->badblocks
, page
, 0);
2981 static ssize_t
bb_store(struct md_rdev
*rdev
, const char *page
, size_t len
)
2983 int rv
= badblocks_store(&rdev
->badblocks
, page
, len
, 0);
2984 /* Maybe that ack was all we needed */
2985 if (test_and_clear_bit(BlockedBadBlocks
, &rdev
->flags
))
2986 wake_up(&rdev
->blocked_wait
);
2989 static struct rdev_sysfs_entry rdev_bad_blocks
=
2990 __ATTR(bad_blocks
, S_IRUGO
|S_IWUSR
, bb_show
, bb_store
);
2993 static ssize_t
ubb_show(struct md_rdev
*rdev
, char *page
)
2995 return badblocks_show(&rdev
->badblocks
, page
, 1);
2997 static ssize_t
ubb_store(struct md_rdev
*rdev
, const char *page
, size_t len
)
2999 return badblocks_store(&rdev
->badblocks
, page
, len
, 1);
3001 static struct rdev_sysfs_entry rdev_unack_bad_blocks
=
3002 __ATTR(unacknowledged_bad_blocks
, S_IRUGO
|S_IWUSR
, ubb_show
, ubb_store
);
3004 static struct attribute
*rdev_default_attrs
[] = {
3010 &rdev_recovery_start
.attr
,
3011 &rdev_bad_blocks
.attr
,
3012 &rdev_unack_bad_blocks
.attr
,
3016 rdev_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
3018 struct rdev_sysfs_entry
*entry
= container_of(attr
, struct rdev_sysfs_entry
, attr
);
3019 struct md_rdev
*rdev
= container_of(kobj
, struct md_rdev
, kobj
);
3020 struct mddev
*mddev
= rdev
->mddev
;
3026 rv
= mddev
? mddev_lock(mddev
) : -EBUSY
;
3028 if (rdev
->mddev
== NULL
)
3031 rv
= entry
->show(rdev
, page
);
3032 mddev_unlock(mddev
);
3038 rdev_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
3039 const char *page
, size_t length
)
3041 struct rdev_sysfs_entry
*entry
= container_of(attr
, struct rdev_sysfs_entry
, attr
);
3042 struct md_rdev
*rdev
= container_of(kobj
, struct md_rdev
, kobj
);
3044 struct mddev
*mddev
= rdev
->mddev
;
3048 if (!capable(CAP_SYS_ADMIN
))
3050 rv
= mddev
? mddev_lock(mddev
): -EBUSY
;
3052 if (rdev
->mddev
== NULL
)
3055 rv
= entry
->store(rdev
, page
, length
);
3056 mddev_unlock(mddev
);
3061 static void rdev_free(struct kobject
*ko
)
3063 struct md_rdev
*rdev
= container_of(ko
, struct md_rdev
, kobj
);
3066 static const struct sysfs_ops rdev_sysfs_ops
= {
3067 .show
= rdev_attr_show
,
3068 .store
= rdev_attr_store
,
3070 static struct kobj_type rdev_ktype
= {
3071 .release
= rdev_free
,
3072 .sysfs_ops
= &rdev_sysfs_ops
,
3073 .default_attrs
= rdev_default_attrs
,
3076 int md_rdev_init(struct md_rdev
*rdev
)
3079 rdev
->saved_raid_disk
= -1;
3080 rdev
->raid_disk
= -1;
3082 rdev
->data_offset
= 0;
3083 rdev
->sb_events
= 0;
3084 rdev
->last_read_error
.tv_sec
= 0;
3085 rdev
->last_read_error
.tv_nsec
= 0;
3086 rdev
->sb_loaded
= 0;
3087 rdev
->bb_page
= NULL
;
3088 atomic_set(&rdev
->nr_pending
, 0);
3089 atomic_set(&rdev
->read_errors
, 0);
3090 atomic_set(&rdev
->corrected_errors
, 0);
3092 INIT_LIST_HEAD(&rdev
->same_set
);
3093 init_waitqueue_head(&rdev
->blocked_wait
);
3095 /* Add space to store bad block list.
3096 * This reserves the space even on arrays where it cannot
3097 * be used - I wonder if that matters
3099 rdev
->badblocks
.count
= 0;
3100 rdev
->badblocks
.shift
= 0;
3101 rdev
->badblocks
.page
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
3102 seqlock_init(&rdev
->badblocks
.lock
);
3103 if (rdev
->badblocks
.page
== NULL
)
3108 EXPORT_SYMBOL_GPL(md_rdev_init
);
3110 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3112 * mark the device faulty if:
3114 * - the device is nonexistent (zero size)
3115 * - the device has no valid superblock
3117 * a faulty rdev _never_ has rdev->sb set.
3119 static struct md_rdev
*md_import_device(dev_t newdev
, int super_format
, int super_minor
)
3121 char b
[BDEVNAME_SIZE
];
3123 struct md_rdev
*rdev
;
3126 rdev
= kzalloc(sizeof(*rdev
), GFP_KERNEL
);
3128 printk(KERN_ERR
"md: could not alloc mem for new device!\n");
3129 return ERR_PTR(-ENOMEM
);
3132 err
= md_rdev_init(rdev
);
3135 err
= alloc_disk_sb(rdev
);
3139 err
= lock_rdev(rdev
, newdev
, super_format
== -2);
3143 kobject_init(&rdev
->kobj
, &rdev_ktype
);
3145 size
= i_size_read(rdev
->bdev
->bd_inode
) >> BLOCK_SIZE_BITS
;
3148 "md: %s has zero or unknown size, marking faulty!\n",
3149 bdevname(rdev
->bdev
,b
));
3154 if (super_format
>= 0) {
3155 err
= super_types
[super_format
].
3156 load_super(rdev
, NULL
, super_minor
);
3157 if (err
== -EINVAL
) {
3159 "md: %s does not have a valid v%d.%d "
3160 "superblock, not importing!\n",
3161 bdevname(rdev
->bdev
,b
),
3162 super_format
, super_minor
);
3167 "md: could not read %s's sb, not importing!\n",
3168 bdevname(rdev
->bdev
,b
));
3172 if (super_format
== -1)
3173 /* hot-add for 0.90, or non-persistent: so no badblocks */
3174 rdev
->badblocks
.shift
= -1;
3182 kfree(rdev
->badblocks
.page
);
3184 return ERR_PTR(err
);
3188 * Check a full RAID array for plausibility
3192 static void analyze_sbs(struct mddev
* mddev
)
3195 struct md_rdev
*rdev
, *freshest
, *tmp
;
3196 char b
[BDEVNAME_SIZE
];
3199 rdev_for_each(rdev
, tmp
, mddev
)
3200 switch (super_types
[mddev
->major_version
].
3201 load_super(rdev
, freshest
, mddev
->minor_version
)) {
3209 "md: fatal superblock inconsistency in %s"
3210 " -- removing from array\n",
3211 bdevname(rdev
->bdev
,b
));
3212 kick_rdev_from_array(rdev
);
3216 super_types
[mddev
->major_version
].
3217 validate_super(mddev
, freshest
);
3220 rdev_for_each(rdev
, tmp
, mddev
) {
3221 if (mddev
->max_disks
&&
3222 (rdev
->desc_nr
>= mddev
->max_disks
||
3223 i
> mddev
->max_disks
)) {
3225 "md: %s: %s: only %d devices permitted\n",
3226 mdname(mddev
), bdevname(rdev
->bdev
, b
),
3228 kick_rdev_from_array(rdev
);
3231 if (rdev
!= freshest
)
3232 if (super_types
[mddev
->major_version
].
3233 validate_super(mddev
, rdev
)) {
3234 printk(KERN_WARNING
"md: kicking non-fresh %s"
3236 bdevname(rdev
->bdev
,b
));
3237 kick_rdev_from_array(rdev
);
3240 if (mddev
->level
== LEVEL_MULTIPATH
) {
3241 rdev
->desc_nr
= i
++;
3242 rdev
->raid_disk
= rdev
->desc_nr
;
3243 set_bit(In_sync
, &rdev
->flags
);
3244 } else if (rdev
->raid_disk
>= (mddev
->raid_disks
- min(0, mddev
->delta_disks
))) {
3245 rdev
->raid_disk
= -1;
3246 clear_bit(In_sync
, &rdev
->flags
);
3251 /* Read a fixed-point number.
3252 * Numbers in sysfs attributes should be in "standard" units where
3253 * possible, so time should be in seconds.
3254 * However we internally use a a much smaller unit such as
3255 * milliseconds or jiffies.
3256 * This function takes a decimal number with a possible fractional
3257 * component, and produces an integer which is the result of
3258 * multiplying that number by 10^'scale'.
3259 * all without any floating-point arithmetic.
3261 int strict_strtoul_scaled(const char *cp
, unsigned long *res
, int scale
)
3263 unsigned long result
= 0;
3265 while (isdigit(*cp
) || (*cp
== '.' && decimals
< 0)) {
3268 else if (decimals
< scale
) {
3271 result
= result
* 10 + value
;
3283 while (decimals
< scale
) {
3292 static void md_safemode_timeout(unsigned long data
);
3295 safe_delay_show(struct mddev
*mddev
, char *page
)
3297 int msec
= (mddev
->safemode_delay
*1000)/HZ
;
3298 return sprintf(page
, "%d.%03d\n", msec
/1000, msec
%1000);
3301 safe_delay_store(struct mddev
*mddev
, const char *cbuf
, size_t len
)
3305 if (strict_strtoul_scaled(cbuf
, &msec
, 3) < 0)
3308 mddev
->safemode_delay
= 0;
3310 unsigned long old_delay
= mddev
->safemode_delay
;
3311 mddev
->safemode_delay
= (msec
*HZ
)/1000;
3312 if (mddev
->safemode_delay
== 0)
3313 mddev
->safemode_delay
= 1;
3314 if (mddev
->safemode_delay
< old_delay
)
3315 md_safemode_timeout((unsigned long)mddev
);
3319 static struct md_sysfs_entry md_safe_delay
=
3320 __ATTR(safe_mode_delay
, S_IRUGO
|S_IWUSR
,safe_delay_show
, safe_delay_store
);
3323 level_show(struct mddev
*mddev
, char *page
)
3325 struct md_personality
*p
= mddev
->pers
;
3327 return sprintf(page
, "%s\n", p
->name
);
3328 else if (mddev
->clevel
[0])
3329 return sprintf(page
, "%s\n", mddev
->clevel
);
3330 else if (mddev
->level
!= LEVEL_NONE
)
3331 return sprintf(page
, "%d\n", mddev
->level
);
3337 level_store(struct mddev
*mddev
, const char *buf
, size_t len
)
3341 struct md_personality
*pers
;
3344 struct md_rdev
*rdev
;
3346 if (mddev
->pers
== NULL
) {
3349 if (len
>= sizeof(mddev
->clevel
))
3351 strncpy(mddev
->clevel
, buf
, len
);
3352 if (mddev
->clevel
[len
-1] == '\n')
3354 mddev
->clevel
[len
] = 0;
3355 mddev
->level
= LEVEL_NONE
;
3359 /* request to change the personality. Need to ensure:
3360 * - array is not engaged in resync/recovery/reshape
3361 * - old personality can be suspended
3362 * - new personality will access other array.
3365 if (mddev
->sync_thread
||
3366 mddev
->reshape_position
!= MaxSector
||
3367 mddev
->sysfs_active
)
3370 if (!mddev
->pers
->quiesce
) {
3371 printk(KERN_WARNING
"md: %s: %s does not support online personality change\n",
3372 mdname(mddev
), mddev
->pers
->name
);
3376 /* Now find the new personality */
3377 if (len
== 0 || len
>= sizeof(clevel
))
3379 strncpy(clevel
, buf
, len
);
3380 if (clevel
[len
-1] == '\n')
3383 if (strict_strtol(clevel
, 10, &level
))
3386 if (request_module("md-%s", clevel
) != 0)
3387 request_module("md-level-%s", clevel
);
3388 spin_lock(&pers_lock
);
3389 pers
= find_pers(level
, clevel
);
3390 if (!pers
|| !try_module_get(pers
->owner
)) {
3391 spin_unlock(&pers_lock
);
3392 printk(KERN_WARNING
"md: personality %s not loaded\n", clevel
);
3395 spin_unlock(&pers_lock
);
3397 if (pers
== mddev
->pers
) {
3398 /* Nothing to do! */
3399 module_put(pers
->owner
);
3402 if (!pers
->takeover
) {
3403 module_put(pers
->owner
);
3404 printk(KERN_WARNING
"md: %s: %s does not support personality takeover\n",
3405 mdname(mddev
), clevel
);
3409 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
3410 rdev
->new_raid_disk
= rdev
->raid_disk
;
3412 /* ->takeover must set new_* and/or delta_disks
3413 * if it succeeds, and may set them when it fails.
3415 priv
= pers
->takeover(mddev
);
3417 mddev
->new_level
= mddev
->level
;
3418 mddev
->new_layout
= mddev
->layout
;
3419 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
3420 mddev
->raid_disks
-= mddev
->delta_disks
;
3421 mddev
->delta_disks
= 0;
3422 module_put(pers
->owner
);
3423 printk(KERN_WARNING
"md: %s: %s would not accept array\n",
3424 mdname(mddev
), clevel
);
3425 return PTR_ERR(priv
);
3428 /* Looks like we have a winner */
3429 mddev_suspend(mddev
);
3430 mddev
->pers
->stop(mddev
);
3432 if (mddev
->pers
->sync_request
== NULL
&&
3433 pers
->sync_request
!= NULL
) {
3434 /* need to add the md_redundancy_group */
3435 if (sysfs_create_group(&mddev
->kobj
, &md_redundancy_group
))
3437 "md: cannot register extra attributes for %s\n",
3439 mddev
->sysfs_action
= sysfs_get_dirent(mddev
->kobj
.sd
, NULL
, "sync_action");
3441 if (mddev
->pers
->sync_request
!= NULL
&&
3442 pers
->sync_request
== NULL
) {
3443 /* need to remove the md_redundancy_group */
3444 if (mddev
->to_remove
== NULL
)
3445 mddev
->to_remove
= &md_redundancy_group
;
3448 if (mddev
->pers
->sync_request
== NULL
&&
3450 /* We are converting from a no-redundancy array
3451 * to a redundancy array and metadata is managed
3452 * externally so we need to be sure that writes
3453 * won't block due to a need to transition
3455 * until external management is started.
3458 mddev
->safemode_delay
= 0;
3459 mddev
->safemode
= 0;
3462 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
3463 if (rdev
->raid_disk
< 0)
3465 if (rdev
->new_raid_disk
>= mddev
->raid_disks
)
3466 rdev
->new_raid_disk
= -1;
3467 if (rdev
->new_raid_disk
== rdev
->raid_disk
)
3469 sysfs_unlink_rdev(mddev
, rdev
);
3471 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
3472 if (rdev
->raid_disk
< 0)
3474 if (rdev
->new_raid_disk
== rdev
->raid_disk
)
3476 rdev
->raid_disk
= rdev
->new_raid_disk
;
3477 if (rdev
->raid_disk
< 0)
3478 clear_bit(In_sync
, &rdev
->flags
);
3480 if (sysfs_link_rdev(mddev
, rdev
))
3481 printk(KERN_WARNING
"md: cannot register rd%d"
3482 " for %s after level change\n",
3483 rdev
->raid_disk
, mdname(mddev
));
3487 module_put(mddev
->pers
->owner
);
3489 mddev
->private = priv
;
3490 strlcpy(mddev
->clevel
, pers
->name
, sizeof(mddev
->clevel
));
3491 mddev
->level
= mddev
->new_level
;
3492 mddev
->layout
= mddev
->new_layout
;
3493 mddev
->chunk_sectors
= mddev
->new_chunk_sectors
;
3494 mddev
->delta_disks
= 0;
3495 mddev
->degraded
= 0;
3496 if (mddev
->pers
->sync_request
== NULL
) {
3497 /* this is now an array without redundancy, so
3498 * it must always be in_sync
3501 del_timer_sync(&mddev
->safemode_timer
);
3504 mddev_resume(mddev
);
3505 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
3506 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3507 md_wakeup_thread(mddev
->thread
);
3508 sysfs_notify(&mddev
->kobj
, NULL
, "level");
3509 md_new_event(mddev
);
3513 static struct md_sysfs_entry md_level
=
3514 __ATTR(level
, S_IRUGO
|S_IWUSR
, level_show
, level_store
);
3518 layout_show(struct mddev
*mddev
, char *page
)
3520 /* just a number, not meaningful for all levels */
3521 if (mddev
->reshape_position
!= MaxSector
&&
3522 mddev
->layout
!= mddev
->new_layout
)
3523 return sprintf(page
, "%d (%d)\n",
3524 mddev
->new_layout
, mddev
->layout
);
3525 return sprintf(page
, "%d\n", mddev
->layout
);
3529 layout_store(struct mddev
*mddev
, const char *buf
, size_t len
)
3532 unsigned long n
= simple_strtoul(buf
, &e
, 10);
3534 if (!*buf
|| (*e
&& *e
!= '\n'))
3539 if (mddev
->pers
->check_reshape
== NULL
)
3541 mddev
->new_layout
= n
;
3542 err
= mddev
->pers
->check_reshape(mddev
);
3544 mddev
->new_layout
= mddev
->layout
;
3548 mddev
->new_layout
= n
;
3549 if (mddev
->reshape_position
== MaxSector
)
3554 static struct md_sysfs_entry md_layout
=
3555 __ATTR(layout
, S_IRUGO
|S_IWUSR
, layout_show
, layout_store
);
3559 raid_disks_show(struct mddev
*mddev
, char *page
)
3561 if (mddev
->raid_disks
== 0)
3563 if (mddev
->reshape_position
!= MaxSector
&&
3564 mddev
->delta_disks
!= 0)
3565 return sprintf(page
, "%d (%d)\n", mddev
->raid_disks
,
3566 mddev
->raid_disks
- mddev
->delta_disks
);
3567 return sprintf(page
, "%d\n", mddev
->raid_disks
);
3570 static int update_raid_disks(struct mddev
*mddev
, int raid_disks
);
3573 raid_disks_store(struct mddev
*mddev
, const char *buf
, size_t len
)
3577 unsigned long n
= simple_strtoul(buf
, &e
, 10);
3579 if (!*buf
|| (*e
&& *e
!= '\n'))
3583 rv
= update_raid_disks(mddev
, n
);
3584 else if (mddev
->reshape_position
!= MaxSector
) {
3585 int olddisks
= mddev
->raid_disks
- mddev
->delta_disks
;
3586 mddev
->delta_disks
= n
- olddisks
;
3587 mddev
->raid_disks
= n
;
3589 mddev
->raid_disks
= n
;
3590 return rv
? rv
: len
;
3592 static struct md_sysfs_entry md_raid_disks
=
3593 __ATTR(raid_disks
, S_IRUGO
|S_IWUSR
, raid_disks_show
, raid_disks_store
);
3596 chunk_size_show(struct mddev
*mddev
, char *page
)
3598 if (mddev
->reshape_position
!= MaxSector
&&
3599 mddev
->chunk_sectors
!= mddev
->new_chunk_sectors
)
3600 return sprintf(page
, "%d (%d)\n",
3601 mddev
->new_chunk_sectors
<< 9,
3602 mddev
->chunk_sectors
<< 9);
3603 return sprintf(page
, "%d\n", mddev
->chunk_sectors
<< 9);
3607 chunk_size_store(struct mddev
*mddev
, const char *buf
, size_t len
)
3610 unsigned long n
= simple_strtoul(buf
, &e
, 10);
3612 if (!*buf
|| (*e
&& *e
!= '\n'))
3617 if (mddev
->pers
->check_reshape
== NULL
)
3619 mddev
->new_chunk_sectors
= n
>> 9;
3620 err
= mddev
->pers
->check_reshape(mddev
);
3622 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
3626 mddev
->new_chunk_sectors
= n
>> 9;
3627 if (mddev
->reshape_position
== MaxSector
)
3628 mddev
->chunk_sectors
= n
>> 9;
3632 static struct md_sysfs_entry md_chunk_size
=
3633 __ATTR(chunk_size
, S_IRUGO
|S_IWUSR
, chunk_size_show
, chunk_size_store
);
3636 resync_start_show(struct mddev
*mddev
, char *page
)
3638 if (mddev
->recovery_cp
== MaxSector
)
3639 return sprintf(page
, "none\n");
3640 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->recovery_cp
);
3644 resync_start_store(struct mddev
*mddev
, const char *buf
, size_t len
)
3647 unsigned long long n
= simple_strtoull(buf
, &e
, 10);
3649 if (mddev
->pers
&& !test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
))
3651 if (cmd_match(buf
, "none"))
3653 else if (!*buf
|| (*e
&& *e
!= '\n'))
3656 mddev
->recovery_cp
= n
;
3659 static struct md_sysfs_entry md_resync_start
=
3660 __ATTR(resync_start
, S_IRUGO
|S_IWUSR
, resync_start_show
, resync_start_store
);
3663 * The array state can be:
3666 * No devices, no size, no level
3667 * Equivalent to STOP_ARRAY ioctl
3669 * May have some settings, but array is not active
3670 * all IO results in error
3671 * When written, doesn't tear down array, but just stops it
3672 * suspended (not supported yet)
3673 * All IO requests will block. The array can be reconfigured.
3674 * Writing this, if accepted, will block until array is quiescent
3676 * no resync can happen. no superblocks get written.
3677 * write requests fail
3679 * like readonly, but behaves like 'clean' on a write request.
3681 * clean - no pending writes, but otherwise active.
3682 * When written to inactive array, starts without resync
3683 * If a write request arrives then
3684 * if metadata is known, mark 'dirty' and switch to 'active'.
3685 * if not known, block and switch to write-pending
3686 * If written to an active array that has pending writes, then fails.
3688 * fully active: IO and resync can be happening.
3689 * When written to inactive array, starts with resync
3692 * clean, but writes are blocked waiting for 'active' to be written.
3695 * like active, but no writes have been seen for a while (100msec).
3698 enum array_state
{ clear
, inactive
, suspended
, readonly
, read_auto
, clean
, active
,
3699 write_pending
, active_idle
, bad_word
};
3700 static char *array_states
[] = {
3701 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3702 "write-pending", "active-idle", NULL
};
3704 static int match_word(const char *word
, char **list
)
3707 for (n
=0; list
[n
]; n
++)
3708 if (cmd_match(word
, list
[n
]))
3714 array_state_show(struct mddev
*mddev
, char *page
)
3716 enum array_state st
= inactive
;
3729 else if (test_bit(MD_CHANGE_PENDING
, &mddev
->flags
))
3731 else if (mddev
->safemode
)
3737 if (list_empty(&mddev
->disks
) &&
3738 mddev
->raid_disks
== 0 &&
3739 mddev
->dev_sectors
== 0)
3744 return sprintf(page
, "%s\n", array_states
[st
]);
3747 static int do_md_stop(struct mddev
* mddev
, int ro
, int is_open
);
3748 static int md_set_readonly(struct mddev
* mddev
, int is_open
);
3749 static int do_md_run(struct mddev
* mddev
);
3750 static int restart_array(struct mddev
*mddev
);
3753 array_state_store(struct mddev
*mddev
, const char *buf
, size_t len
)
3756 enum array_state st
= match_word(buf
, array_states
);
3761 /* stopping an active array */
3762 if (atomic_read(&mddev
->openers
) > 0)
3764 err
= do_md_stop(mddev
, 0, 0);
3767 /* stopping an active array */
3769 if (atomic_read(&mddev
->openers
) > 0)
3771 err
= do_md_stop(mddev
, 2, 0);
3773 err
= 0; /* already inactive */
3776 break; /* not supported yet */
3779 err
= md_set_readonly(mddev
, 0);
3782 set_disk_ro(mddev
->gendisk
, 1);
3783 err
= do_md_run(mddev
);
3789 err
= md_set_readonly(mddev
, 0);
3790 else if (mddev
->ro
== 1)
3791 err
= restart_array(mddev
);
3794 set_disk_ro(mddev
->gendisk
, 0);
3798 err
= do_md_run(mddev
);
3803 restart_array(mddev
);
3804 spin_lock_irq(&mddev
->write_lock
);
3805 if (atomic_read(&mddev
->writes_pending
) == 0) {
3806 if (mddev
->in_sync
== 0) {
3808 if (mddev
->safemode
== 1)
3809 mddev
->safemode
= 0;
3810 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
3815 spin_unlock_irq(&mddev
->write_lock
);
3821 restart_array(mddev
);
3822 clear_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
3823 wake_up(&mddev
->sb_wait
);
3827 set_disk_ro(mddev
->gendisk
, 0);
3828 err
= do_md_run(mddev
);
3833 /* these cannot be set */
3839 if (mddev
->hold_active
== UNTIL_IOCTL
)
3840 mddev
->hold_active
= 0;
3841 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
3845 static struct md_sysfs_entry md_array_state
=
3846 __ATTR(array_state
, S_IRUGO
|S_IWUSR
, array_state_show
, array_state_store
);
3849 max_corrected_read_errors_show(struct mddev
*mddev
, char *page
) {
3850 return sprintf(page
, "%d\n",
3851 atomic_read(&mddev
->max_corr_read_errors
));
3855 max_corrected_read_errors_store(struct mddev
*mddev
, const char *buf
, size_t len
)
3858 unsigned long n
= simple_strtoul(buf
, &e
, 10);
3860 if (*buf
&& (*e
== 0 || *e
== '\n')) {
3861 atomic_set(&mddev
->max_corr_read_errors
, n
);
3867 static struct md_sysfs_entry max_corr_read_errors
=
3868 __ATTR(max_read_errors
, S_IRUGO
|S_IWUSR
, max_corrected_read_errors_show
,
3869 max_corrected_read_errors_store
);
3872 null_show(struct mddev
*mddev
, char *page
)
3878 new_dev_store(struct mddev
*mddev
, const char *buf
, size_t len
)
3880 /* buf must be %d:%d\n? giving major and minor numbers */
3881 /* The new device is added to the array.
3882 * If the array has a persistent superblock, we read the
3883 * superblock to initialise info and check validity.
3884 * Otherwise, only checking done is that in bind_rdev_to_array,
3885 * which mainly checks size.
3888 int major
= simple_strtoul(buf
, &e
, 10);
3891 struct md_rdev
*rdev
;
3894 if (!*buf
|| *e
!= ':' || !e
[1] || e
[1] == '\n')
3896 minor
= simple_strtoul(e
+1, &e
, 10);
3897 if (*e
&& *e
!= '\n')
3899 dev
= MKDEV(major
, minor
);
3900 if (major
!= MAJOR(dev
) ||
3901 minor
!= MINOR(dev
))
3905 if (mddev
->persistent
) {
3906 rdev
= md_import_device(dev
, mddev
->major_version
,
3907 mddev
->minor_version
);
3908 if (!IS_ERR(rdev
) && !list_empty(&mddev
->disks
)) {
3909 struct md_rdev
*rdev0
3910 = list_entry(mddev
->disks
.next
,
3911 struct md_rdev
, same_set
);
3912 err
= super_types
[mddev
->major_version
]
3913 .load_super(rdev
, rdev0
, mddev
->minor_version
);
3917 } else if (mddev
->external
)
3918 rdev
= md_import_device(dev
, -2, -1);
3920 rdev
= md_import_device(dev
, -1, -1);
3923 return PTR_ERR(rdev
);
3924 err
= bind_rdev_to_array(rdev
, mddev
);
3928 return err
? err
: len
;
3931 static struct md_sysfs_entry md_new_device
=
3932 __ATTR(new_dev
, S_IWUSR
, null_show
, new_dev_store
);
3935 bitmap_store(struct mddev
*mddev
, const char *buf
, size_t len
)
3938 unsigned long chunk
, end_chunk
;
3942 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3944 chunk
= end_chunk
= simple_strtoul(buf
, &end
, 0);
3945 if (buf
== end
) break;
3946 if (*end
== '-') { /* range */
3948 end_chunk
= simple_strtoul(buf
, &end
, 0);
3949 if (buf
== end
) break;
3951 if (*end
&& !isspace(*end
)) break;
3952 bitmap_dirty_bits(mddev
->bitmap
, chunk
, end_chunk
);
3953 buf
= skip_spaces(end
);
3955 bitmap_unplug(mddev
->bitmap
); /* flush the bits to disk */
3960 static struct md_sysfs_entry md_bitmap
=
3961 __ATTR(bitmap_set_bits
, S_IWUSR
, null_show
, bitmap_store
);
3964 size_show(struct mddev
*mddev
, char *page
)
3966 return sprintf(page
, "%llu\n",
3967 (unsigned long long)mddev
->dev_sectors
/ 2);
3970 static int update_size(struct mddev
*mddev
, sector_t num_sectors
);
3973 size_store(struct mddev
*mddev
, const char *buf
, size_t len
)
3975 /* If array is inactive, we can reduce the component size, but
3976 * not increase it (except from 0).
3977 * If array is active, we can try an on-line resize
3980 int err
= strict_blocks_to_sectors(buf
, §ors
);
3985 err
= update_size(mddev
, sectors
);
3986 md_update_sb(mddev
, 1);
3988 if (mddev
->dev_sectors
== 0 ||
3989 mddev
->dev_sectors
> sectors
)
3990 mddev
->dev_sectors
= sectors
;
3994 return err
? err
: len
;
3997 static struct md_sysfs_entry md_size
=
3998 __ATTR(component_size
, S_IRUGO
|S_IWUSR
, size_show
, size_store
);
4003 * 'none' for arrays with no metadata (good luck...)
4004 * 'external' for arrays with externally managed metadata,
4005 * or N.M for internally known formats
4008 metadata_show(struct mddev
*mddev
, char *page
)
4010 if (mddev
->persistent
)
4011 return sprintf(page
, "%d.%d\n",
4012 mddev
->major_version
, mddev
->minor_version
);
4013 else if (mddev
->external
)
4014 return sprintf(page
, "external:%s\n", mddev
->metadata_type
);
4016 return sprintf(page
, "none\n");
4020 metadata_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4024 /* Changing the details of 'external' metadata is
4025 * always permitted. Otherwise there must be
4026 * no devices attached to the array.
4028 if (mddev
->external
&& strncmp(buf
, "external:", 9) == 0)
4030 else if (!list_empty(&mddev
->disks
))
4033 if (cmd_match(buf
, "none")) {
4034 mddev
->persistent
= 0;
4035 mddev
->external
= 0;
4036 mddev
->major_version
= 0;
4037 mddev
->minor_version
= 90;
4040 if (strncmp(buf
, "external:", 9) == 0) {
4041 size_t namelen
= len
-9;
4042 if (namelen
>= sizeof(mddev
->metadata_type
))
4043 namelen
= sizeof(mddev
->metadata_type
)-1;
4044 strncpy(mddev
->metadata_type
, buf
+9, namelen
);
4045 mddev
->metadata_type
[namelen
] = 0;
4046 if (namelen
&& mddev
->metadata_type
[namelen
-1] == '\n')
4047 mddev
->metadata_type
[--namelen
] = 0;
4048 mddev
->persistent
= 0;
4049 mddev
->external
= 1;
4050 mddev
->major_version
= 0;
4051 mddev
->minor_version
= 90;
4054 major
= simple_strtoul(buf
, &e
, 10);
4055 if (e
==buf
|| *e
!= '.')
4058 minor
= simple_strtoul(buf
, &e
, 10);
4059 if (e
==buf
|| (*e
&& *e
!= '\n') )
4061 if (major
>= ARRAY_SIZE(super_types
) || super_types
[major
].name
== NULL
)
4063 mddev
->major_version
= major
;
4064 mddev
->minor_version
= minor
;
4065 mddev
->persistent
= 1;
4066 mddev
->external
= 0;
4070 static struct md_sysfs_entry md_metadata
=
4071 __ATTR(metadata_version
, S_IRUGO
|S_IWUSR
, metadata_show
, metadata_store
);
4074 action_show(struct mddev
*mddev
, char *page
)
4076 char *type
= "idle";
4077 if (test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
))
4079 else if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
4080 (!mddev
->ro
&& test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))) {
4081 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
4083 else if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
4084 if (!test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
4086 else if (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
))
4090 } else if (test_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
))
4093 return sprintf(page
, "%s\n", type
);
4096 static void reap_sync_thread(struct mddev
*mddev
);
4099 action_store(struct mddev
*mddev
, const char *page
, size_t len
)
4101 if (!mddev
->pers
|| !mddev
->pers
->sync_request
)
4104 if (cmd_match(page
, "frozen"))
4105 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4107 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4109 if (cmd_match(page
, "idle") || cmd_match(page
, "frozen")) {
4110 if (mddev
->sync_thread
) {
4111 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
4112 reap_sync_thread(mddev
);
4114 } else if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
4115 test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))
4117 else if (cmd_match(page
, "resync"))
4118 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4119 else if (cmd_match(page
, "recover")) {
4120 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
4121 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4122 } else if (cmd_match(page
, "reshape")) {
4124 if (mddev
->pers
->start_reshape
== NULL
)
4126 err
= mddev
->pers
->start_reshape(mddev
);
4129 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
4131 if (cmd_match(page
, "check"))
4132 set_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
4133 else if (!cmd_match(page
, "repair"))
4135 set_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
4136 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
4138 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4139 md_wakeup_thread(mddev
->thread
);
4140 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
4145 mismatch_cnt_show(struct mddev
*mddev
, char *page
)
4147 return sprintf(page
, "%llu\n",
4148 (unsigned long long) mddev
->resync_mismatches
);
4151 static struct md_sysfs_entry md_scan_mode
=
4152 __ATTR(sync_action
, S_IRUGO
|S_IWUSR
, action_show
, action_store
);
4155 static struct md_sysfs_entry md_mismatches
= __ATTR_RO(mismatch_cnt
);
4158 sync_min_show(struct mddev
*mddev
, char *page
)
4160 return sprintf(page
, "%d (%s)\n", speed_min(mddev
),
4161 mddev
->sync_speed_min
? "local": "system");
4165 sync_min_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4169 if (strncmp(buf
, "system", 6)==0) {
4170 mddev
->sync_speed_min
= 0;
4173 min
= simple_strtoul(buf
, &e
, 10);
4174 if (buf
== e
|| (*e
&& *e
!= '\n') || min
<= 0)
4176 mddev
->sync_speed_min
= min
;
4180 static struct md_sysfs_entry md_sync_min
=
4181 __ATTR(sync_speed_min
, S_IRUGO
|S_IWUSR
, sync_min_show
, sync_min_store
);
4184 sync_max_show(struct mddev
*mddev
, char *page
)
4186 return sprintf(page
, "%d (%s)\n", speed_max(mddev
),
4187 mddev
->sync_speed_max
? "local": "system");
4191 sync_max_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4195 if (strncmp(buf
, "system", 6)==0) {
4196 mddev
->sync_speed_max
= 0;
4199 max
= simple_strtoul(buf
, &e
, 10);
4200 if (buf
== e
|| (*e
&& *e
!= '\n') || max
<= 0)
4202 mddev
->sync_speed_max
= max
;
4206 static struct md_sysfs_entry md_sync_max
=
4207 __ATTR(sync_speed_max
, S_IRUGO
|S_IWUSR
, sync_max_show
, sync_max_store
);
4210 degraded_show(struct mddev
*mddev
, char *page
)
4212 return sprintf(page
, "%d\n", mddev
->degraded
);
4214 static struct md_sysfs_entry md_degraded
= __ATTR_RO(degraded
);
4217 sync_force_parallel_show(struct mddev
*mddev
, char *page
)
4219 return sprintf(page
, "%d\n", mddev
->parallel_resync
);
4223 sync_force_parallel_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4227 if (strict_strtol(buf
, 10, &n
))
4230 if (n
!= 0 && n
!= 1)
4233 mddev
->parallel_resync
= n
;
4235 if (mddev
->sync_thread
)
4236 wake_up(&resync_wait
);
4241 /* force parallel resync, even with shared block devices */
4242 static struct md_sysfs_entry md_sync_force_parallel
=
4243 __ATTR(sync_force_parallel
, S_IRUGO
|S_IWUSR
,
4244 sync_force_parallel_show
, sync_force_parallel_store
);
4247 sync_speed_show(struct mddev
*mddev
, char *page
)
4249 unsigned long resync
, dt
, db
;
4250 if (mddev
->curr_resync
== 0)
4251 return sprintf(page
, "none\n");
4252 resync
= mddev
->curr_mark_cnt
- atomic_read(&mddev
->recovery_active
);
4253 dt
= (jiffies
- mddev
->resync_mark
) / HZ
;
4255 db
= resync
- mddev
->resync_mark_cnt
;
4256 return sprintf(page
, "%lu\n", db
/dt
/2); /* K/sec */
4259 static struct md_sysfs_entry md_sync_speed
= __ATTR_RO(sync_speed
);
4262 sync_completed_show(struct mddev
*mddev
, char *page
)
4264 unsigned long long max_sectors
, resync
;
4266 if (!test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
4267 return sprintf(page
, "none\n");
4269 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
4270 max_sectors
= mddev
->resync_max_sectors
;
4272 max_sectors
= mddev
->dev_sectors
;
4274 resync
= mddev
->curr_resync_completed
;
4275 return sprintf(page
, "%llu / %llu\n", resync
, max_sectors
);
4278 static struct md_sysfs_entry md_sync_completed
= __ATTR_RO(sync_completed
);
4281 min_sync_show(struct mddev
*mddev
, char *page
)
4283 return sprintf(page
, "%llu\n",
4284 (unsigned long long)mddev
->resync_min
);
4287 min_sync_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4289 unsigned long long min
;
4290 if (strict_strtoull(buf
, 10, &min
))
4292 if (min
> mddev
->resync_max
)
4294 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
4297 /* Must be a multiple of chunk_size */
4298 if (mddev
->chunk_sectors
) {
4299 sector_t temp
= min
;
4300 if (sector_div(temp
, mddev
->chunk_sectors
))
4303 mddev
->resync_min
= min
;
4308 static struct md_sysfs_entry md_min_sync
=
4309 __ATTR(sync_min
, S_IRUGO
|S_IWUSR
, min_sync_show
, min_sync_store
);
4312 max_sync_show(struct mddev
*mddev
, char *page
)
4314 if (mddev
->resync_max
== MaxSector
)
4315 return sprintf(page
, "max\n");
4317 return sprintf(page
, "%llu\n",
4318 (unsigned long long)mddev
->resync_max
);
4321 max_sync_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4323 if (strncmp(buf
, "max", 3) == 0)
4324 mddev
->resync_max
= MaxSector
;
4326 unsigned long long max
;
4327 if (strict_strtoull(buf
, 10, &max
))
4329 if (max
< mddev
->resync_min
)
4331 if (max
< mddev
->resync_max
&&
4333 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
4336 /* Must be a multiple of chunk_size */
4337 if (mddev
->chunk_sectors
) {
4338 sector_t temp
= max
;
4339 if (sector_div(temp
, mddev
->chunk_sectors
))
4342 mddev
->resync_max
= max
;
4344 wake_up(&mddev
->recovery_wait
);
4348 static struct md_sysfs_entry md_max_sync
=
4349 __ATTR(sync_max
, S_IRUGO
|S_IWUSR
, max_sync_show
, max_sync_store
);
4352 suspend_lo_show(struct mddev
*mddev
, char *page
)
4354 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->suspend_lo
);
4358 suspend_lo_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4361 unsigned long long new = simple_strtoull(buf
, &e
, 10);
4362 unsigned long long old
= mddev
->suspend_lo
;
4364 if (mddev
->pers
== NULL
||
4365 mddev
->pers
->quiesce
== NULL
)
4367 if (buf
== e
|| (*e
&& *e
!= '\n'))
4370 mddev
->suspend_lo
= new;
4372 /* Shrinking suspended region */
4373 mddev
->pers
->quiesce(mddev
, 2);
4375 /* Expanding suspended region - need to wait */
4376 mddev
->pers
->quiesce(mddev
, 1);
4377 mddev
->pers
->quiesce(mddev
, 0);
4381 static struct md_sysfs_entry md_suspend_lo
=
4382 __ATTR(suspend_lo
, S_IRUGO
|S_IWUSR
, suspend_lo_show
, suspend_lo_store
);
4386 suspend_hi_show(struct mddev
*mddev
, char *page
)
4388 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->suspend_hi
);
4392 suspend_hi_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4395 unsigned long long new = simple_strtoull(buf
, &e
, 10);
4396 unsigned long long old
= mddev
->suspend_hi
;
4398 if (mddev
->pers
== NULL
||
4399 mddev
->pers
->quiesce
== NULL
)
4401 if (buf
== e
|| (*e
&& *e
!= '\n'))
4404 mddev
->suspend_hi
= new;
4406 /* Shrinking suspended region */
4407 mddev
->pers
->quiesce(mddev
, 2);
4409 /* Expanding suspended region - need to wait */
4410 mddev
->pers
->quiesce(mddev
, 1);
4411 mddev
->pers
->quiesce(mddev
, 0);
4415 static struct md_sysfs_entry md_suspend_hi
=
4416 __ATTR(suspend_hi
, S_IRUGO
|S_IWUSR
, suspend_hi_show
, suspend_hi_store
);
4419 reshape_position_show(struct mddev
*mddev
, char *page
)
4421 if (mddev
->reshape_position
!= MaxSector
)
4422 return sprintf(page
, "%llu\n",
4423 (unsigned long long)mddev
->reshape_position
);
4424 strcpy(page
, "none\n");
4429 reshape_position_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4432 unsigned long long new = simple_strtoull(buf
, &e
, 10);
4435 if (buf
== e
|| (*e
&& *e
!= '\n'))
4437 mddev
->reshape_position
= new;
4438 mddev
->delta_disks
= 0;
4439 mddev
->new_level
= mddev
->level
;
4440 mddev
->new_layout
= mddev
->layout
;
4441 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
4445 static struct md_sysfs_entry md_reshape_position
=
4446 __ATTR(reshape_position
, S_IRUGO
|S_IWUSR
, reshape_position_show
,
4447 reshape_position_store
);
4450 array_size_show(struct mddev
*mddev
, char *page
)
4452 if (mddev
->external_size
)
4453 return sprintf(page
, "%llu\n",
4454 (unsigned long long)mddev
->array_sectors
/2);
4456 return sprintf(page
, "default\n");
4460 array_size_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4464 if (strncmp(buf
, "default", 7) == 0) {
4466 sectors
= mddev
->pers
->size(mddev
, 0, 0);
4468 sectors
= mddev
->array_sectors
;
4470 mddev
->external_size
= 0;
4472 if (strict_blocks_to_sectors(buf
, §ors
) < 0)
4474 if (mddev
->pers
&& mddev
->pers
->size(mddev
, 0, 0) < sectors
)
4477 mddev
->external_size
= 1;
4480 mddev
->array_sectors
= sectors
;
4482 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
4483 revalidate_disk(mddev
->gendisk
);
4488 static struct md_sysfs_entry md_array_size
=
4489 __ATTR(array_size
, S_IRUGO
|S_IWUSR
, array_size_show
,
4492 static struct attribute
*md_default_attrs
[] = {
4495 &md_raid_disks
.attr
,
4496 &md_chunk_size
.attr
,
4498 &md_resync_start
.attr
,
4500 &md_new_device
.attr
,
4501 &md_safe_delay
.attr
,
4502 &md_array_state
.attr
,
4503 &md_reshape_position
.attr
,
4504 &md_array_size
.attr
,
4505 &max_corr_read_errors
.attr
,
4509 static struct attribute
*md_redundancy_attrs
[] = {
4511 &md_mismatches
.attr
,
4514 &md_sync_speed
.attr
,
4515 &md_sync_force_parallel
.attr
,
4516 &md_sync_completed
.attr
,
4519 &md_suspend_lo
.attr
,
4520 &md_suspend_hi
.attr
,
4525 static struct attribute_group md_redundancy_group
= {
4527 .attrs
= md_redundancy_attrs
,
4532 md_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
4534 struct md_sysfs_entry
*entry
= container_of(attr
, struct md_sysfs_entry
, attr
);
4535 struct mddev
*mddev
= container_of(kobj
, struct mddev
, kobj
);
4540 spin_lock(&all_mddevs_lock
);
4541 if (list_empty(&mddev
->all_mddevs
)) {
4542 spin_unlock(&all_mddevs_lock
);
4546 spin_unlock(&all_mddevs_lock
);
4548 rv
= mddev_lock(mddev
);
4550 rv
= entry
->show(mddev
, page
);
4551 mddev_unlock(mddev
);
4558 md_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
4559 const char *page
, size_t length
)
4561 struct md_sysfs_entry
*entry
= container_of(attr
, struct md_sysfs_entry
, attr
);
4562 struct mddev
*mddev
= container_of(kobj
, struct mddev
, kobj
);
4567 if (!capable(CAP_SYS_ADMIN
))
4569 spin_lock(&all_mddevs_lock
);
4570 if (list_empty(&mddev
->all_mddevs
)) {
4571 spin_unlock(&all_mddevs_lock
);
4575 spin_unlock(&all_mddevs_lock
);
4576 rv
= mddev_lock(mddev
);
4578 rv
= entry
->store(mddev
, page
, length
);
4579 mddev_unlock(mddev
);
4585 static void md_free(struct kobject
*ko
)
4587 struct mddev
*mddev
= container_of(ko
, struct mddev
, kobj
);
4589 if (mddev
->sysfs_state
)
4590 sysfs_put(mddev
->sysfs_state
);
4592 if (mddev
->gendisk
) {
4593 del_gendisk(mddev
->gendisk
);
4594 put_disk(mddev
->gendisk
);
4597 blk_cleanup_queue(mddev
->queue
);
4602 static const struct sysfs_ops md_sysfs_ops
= {
4603 .show
= md_attr_show
,
4604 .store
= md_attr_store
,
4606 static struct kobj_type md_ktype
= {
4608 .sysfs_ops
= &md_sysfs_ops
,
4609 .default_attrs
= md_default_attrs
,
4614 static void mddev_delayed_delete(struct work_struct
*ws
)
4616 struct mddev
*mddev
= container_of(ws
, struct mddev
, del_work
);
4618 sysfs_remove_group(&mddev
->kobj
, &md_bitmap_group
);
4619 kobject_del(&mddev
->kobj
);
4620 kobject_put(&mddev
->kobj
);
4623 static int md_alloc(dev_t dev
, char *name
)
4625 static DEFINE_MUTEX(disks_mutex
);
4626 struct mddev
*mddev
= mddev_find(dev
);
4627 struct gendisk
*disk
;
4636 partitioned
= (MAJOR(mddev
->unit
) != MD_MAJOR
);
4637 shift
= partitioned
? MdpMinorShift
: 0;
4638 unit
= MINOR(mddev
->unit
) >> shift
;
4640 /* wait for any previous instance of this device to be
4641 * completely removed (mddev_delayed_delete).
4643 flush_workqueue(md_misc_wq
);
4645 mutex_lock(&disks_mutex
);
4651 /* Need to ensure that 'name' is not a duplicate.
4653 struct mddev
*mddev2
;
4654 spin_lock(&all_mddevs_lock
);
4656 list_for_each_entry(mddev2
, &all_mddevs
, all_mddevs
)
4657 if (mddev2
->gendisk
&&
4658 strcmp(mddev2
->gendisk
->disk_name
, name
) == 0) {
4659 spin_unlock(&all_mddevs_lock
);
4662 spin_unlock(&all_mddevs_lock
);
4666 mddev
->queue
= blk_alloc_queue(GFP_KERNEL
);
4669 mddev
->queue
->queuedata
= mddev
;
4671 blk_queue_make_request(mddev
->queue
, md_make_request
);
4672 blk_set_stacking_limits(&mddev
->queue
->limits
);
4674 disk
= alloc_disk(1 << shift
);
4676 blk_cleanup_queue(mddev
->queue
);
4677 mddev
->queue
= NULL
;
4680 disk
->major
= MAJOR(mddev
->unit
);
4681 disk
->first_minor
= unit
<< shift
;
4683 strcpy(disk
->disk_name
, name
);
4684 else if (partitioned
)
4685 sprintf(disk
->disk_name
, "md_d%d", unit
);
4687 sprintf(disk
->disk_name
, "md%d", unit
);
4688 disk
->fops
= &md_fops
;
4689 disk
->private_data
= mddev
;
4690 disk
->queue
= mddev
->queue
;
4691 blk_queue_flush(mddev
->queue
, REQ_FLUSH
| REQ_FUA
);
4692 /* Allow extended partitions. This makes the
4693 * 'mdp' device redundant, but we can't really
4696 disk
->flags
|= GENHD_FL_EXT_DEVT
;
4697 mddev
->gendisk
= disk
;
4698 /* As soon as we call add_disk(), another thread could get
4699 * through to md_open, so make sure it doesn't get too far
4701 mutex_lock(&mddev
->open_mutex
);
4704 error
= kobject_init_and_add(&mddev
->kobj
, &md_ktype
,
4705 &disk_to_dev(disk
)->kobj
, "%s", "md");
4707 /* This isn't possible, but as kobject_init_and_add is marked
4708 * __must_check, we must do something with the result
4710 printk(KERN_WARNING
"md: cannot register %s/md - name in use\n",
4714 if (mddev
->kobj
.sd
&&
4715 sysfs_create_group(&mddev
->kobj
, &md_bitmap_group
))
4716 printk(KERN_DEBUG
"pointless warning\n");
4717 mutex_unlock(&mddev
->open_mutex
);
4719 mutex_unlock(&disks_mutex
);
4720 if (!error
&& mddev
->kobj
.sd
) {
4721 kobject_uevent(&mddev
->kobj
, KOBJ_ADD
);
4722 mddev
->sysfs_state
= sysfs_get_dirent_safe(mddev
->kobj
.sd
, "array_state");
4728 static struct kobject
*md_probe(dev_t dev
, int *part
, void *data
)
4730 md_alloc(dev
, NULL
);
4734 static int add_named_array(const char *val
, struct kernel_param
*kp
)
4736 /* val must be "md_*" where * is not all digits.
4737 * We allocate an array with a large free minor number, and
4738 * set the name to val. val must not already be an active name.
4740 int len
= strlen(val
);
4741 char buf
[DISK_NAME_LEN
];
4743 while (len
&& val
[len
-1] == '\n')
4745 if (len
>= DISK_NAME_LEN
)
4747 strlcpy(buf
, val
, len
+1);
4748 if (strncmp(buf
, "md_", 3) != 0)
4750 return md_alloc(0, buf
);
4753 static void md_safemode_timeout(unsigned long data
)
4755 struct mddev
*mddev
= (struct mddev
*) data
;
4757 if (!atomic_read(&mddev
->writes_pending
)) {
4758 mddev
->safemode
= 1;
4759 if (mddev
->external
)
4760 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4762 md_wakeup_thread(mddev
->thread
);
4765 static int start_dirty_degraded
;
4767 int md_run(struct mddev
*mddev
)
4770 struct md_rdev
*rdev
;
4771 struct md_personality
*pers
;
4773 if (list_empty(&mddev
->disks
))
4774 /* cannot run an array with no devices.. */
4779 /* Cannot run until previous stop completes properly */
4780 if (mddev
->sysfs_active
)
4784 * Analyze all RAID superblock(s)
4786 if (!mddev
->raid_disks
) {
4787 if (!mddev
->persistent
)
4792 if (mddev
->level
!= LEVEL_NONE
)
4793 request_module("md-level-%d", mddev
->level
);
4794 else if (mddev
->clevel
[0])
4795 request_module("md-%s", mddev
->clevel
);
4798 * Drop all container device buffers, from now on
4799 * the only valid external interface is through the md
4802 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
4803 if (test_bit(Faulty
, &rdev
->flags
))
4805 sync_blockdev(rdev
->bdev
);
4806 invalidate_bdev(rdev
->bdev
);
4808 /* perform some consistency tests on the device.
4809 * We don't want the data to overlap the metadata,
4810 * Internal Bitmap issues have been handled elsewhere.
4812 if (rdev
->meta_bdev
) {
4813 /* Nothing to check */;
4814 } else if (rdev
->data_offset
< rdev
->sb_start
) {
4815 if (mddev
->dev_sectors
&&
4816 rdev
->data_offset
+ mddev
->dev_sectors
4818 printk("md: %s: data overlaps metadata\n",
4823 if (rdev
->sb_start
+ rdev
->sb_size
/512
4824 > rdev
->data_offset
) {
4825 printk("md: %s: metadata overlaps data\n",
4830 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
4833 if (mddev
->bio_set
== NULL
)
4834 mddev
->bio_set
= bioset_create(BIO_POOL_SIZE
,
4835 sizeof(struct mddev
*));
4837 spin_lock(&pers_lock
);
4838 pers
= find_pers(mddev
->level
, mddev
->clevel
);
4839 if (!pers
|| !try_module_get(pers
->owner
)) {
4840 spin_unlock(&pers_lock
);
4841 if (mddev
->level
!= LEVEL_NONE
)
4842 printk(KERN_WARNING
"md: personality for level %d is not loaded!\n",
4845 printk(KERN_WARNING
"md: personality for level %s is not loaded!\n",
4850 spin_unlock(&pers_lock
);
4851 if (mddev
->level
!= pers
->level
) {
4852 mddev
->level
= pers
->level
;
4853 mddev
->new_level
= pers
->level
;
4855 strlcpy(mddev
->clevel
, pers
->name
, sizeof(mddev
->clevel
));
4857 if (mddev
->reshape_position
!= MaxSector
&&
4858 pers
->start_reshape
== NULL
) {
4859 /* This personality cannot handle reshaping... */
4861 module_put(pers
->owner
);
4865 if (pers
->sync_request
) {
4866 /* Warn if this is a potentially silly
4869 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
4870 struct md_rdev
*rdev2
;
4873 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
4874 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
) {
4876 rdev
->bdev
->bd_contains
==
4877 rdev2
->bdev
->bd_contains
) {
4879 "%s: WARNING: %s appears to be"
4880 " on the same physical disk as"
4883 bdevname(rdev
->bdev
,b
),
4884 bdevname(rdev2
->bdev
,b2
));
4891 "True protection against single-disk"
4892 " failure might be compromised.\n");
4895 mddev
->recovery
= 0;
4896 /* may be over-ridden by personality */
4897 mddev
->resync_max_sectors
= mddev
->dev_sectors
;
4899 mddev
->ok_start_degraded
= start_dirty_degraded
;
4901 if (start_readonly
&& mddev
->ro
== 0)
4902 mddev
->ro
= 2; /* read-only, but switch on first write */
4904 err
= mddev
->pers
->run(mddev
);
4906 printk(KERN_ERR
"md: pers->run() failed ...\n");
4907 else if (mddev
->pers
->size(mddev
, 0, 0) < mddev
->array_sectors
) {
4908 WARN_ONCE(!mddev
->external_size
, "%s: default size too small,"
4909 " but 'external_size' not in effect?\n", __func__
);
4911 "md: invalid array_size %llu > default size %llu\n",
4912 (unsigned long long)mddev
->array_sectors
/ 2,
4913 (unsigned long long)mddev
->pers
->size(mddev
, 0, 0) / 2);
4915 mddev
->pers
->stop(mddev
);
4917 if (err
== 0 && mddev
->pers
->sync_request
) {
4918 err
= bitmap_create(mddev
);
4920 printk(KERN_ERR
"%s: failed to create bitmap (%d)\n",
4921 mdname(mddev
), err
);
4922 mddev
->pers
->stop(mddev
);
4926 module_put(mddev
->pers
->owner
);
4928 bitmap_destroy(mddev
);
4931 if (mddev
->pers
->sync_request
) {
4932 if (mddev
->kobj
.sd
&&
4933 sysfs_create_group(&mddev
->kobj
, &md_redundancy_group
))
4935 "md: cannot register extra attributes for %s\n",
4937 mddev
->sysfs_action
= sysfs_get_dirent_safe(mddev
->kobj
.sd
, "sync_action");
4938 } else if (mddev
->ro
== 2) /* auto-readonly not meaningful */
4941 atomic_set(&mddev
->writes_pending
,0);
4942 atomic_set(&mddev
->max_corr_read_errors
,
4943 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS
);
4944 mddev
->safemode
= 0;
4945 mddev
->safemode_timer
.function
= md_safemode_timeout
;
4946 mddev
->safemode_timer
.data
= (unsigned long) mddev
;
4947 mddev
->safemode_delay
= (200 * HZ
)/1000 +1; /* 200 msec delay */
4951 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
4952 if (rdev
->raid_disk
>= 0)
4953 if (sysfs_link_rdev(mddev
, rdev
))
4954 /* failure here is OK */;
4956 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4959 md_update_sb(mddev
, 0);
4961 md_new_event(mddev
);
4962 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4963 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
4964 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
4967 EXPORT_SYMBOL_GPL(md_run
);
4969 static int do_md_run(struct mddev
*mddev
)
4973 err
= md_run(mddev
);
4976 err
= bitmap_load(mddev
);
4978 bitmap_destroy(mddev
);
4982 md_wakeup_thread(mddev
->thread
);
4983 md_wakeup_thread(mddev
->sync_thread
); /* possibly kick off a reshape */
4985 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
4986 revalidate_disk(mddev
->gendisk
);
4988 kobject_uevent(&disk_to_dev(mddev
->gendisk
)->kobj
, KOBJ_CHANGE
);
4993 static int restart_array(struct mddev
*mddev
)
4995 struct gendisk
*disk
= mddev
->gendisk
;
4997 /* Complain if it has no devices */
4998 if (list_empty(&mddev
->disks
))
5004 mddev
->safemode
= 0;
5006 set_disk_ro(disk
, 0);
5007 printk(KERN_INFO
"md: %s switched to read-write mode.\n",
5009 /* Kick recovery or resync if necessary */
5010 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5011 md_wakeup_thread(mddev
->thread
);
5012 md_wakeup_thread(mddev
->sync_thread
);
5013 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
5017 /* similar to deny_write_access, but accounts for our holding a reference
5018 * to the file ourselves */
5019 static int deny_bitmap_write_access(struct file
* file
)
5021 struct inode
*inode
= file
->f_mapping
->host
;
5023 spin_lock(&inode
->i_lock
);
5024 if (atomic_read(&inode
->i_writecount
) > 1) {
5025 spin_unlock(&inode
->i_lock
);
5028 atomic_set(&inode
->i_writecount
, -1);
5029 spin_unlock(&inode
->i_lock
);
5034 void restore_bitmap_write_access(struct file
*file
)
5036 struct inode
*inode
= file
->f_mapping
->host
;
5038 spin_lock(&inode
->i_lock
);
5039 atomic_set(&inode
->i_writecount
, 1);
5040 spin_unlock(&inode
->i_lock
);
5043 static void md_clean(struct mddev
*mddev
)
5045 mddev
->array_sectors
= 0;
5046 mddev
->external_size
= 0;
5047 mddev
->dev_sectors
= 0;
5048 mddev
->raid_disks
= 0;
5049 mddev
->recovery_cp
= 0;
5050 mddev
->resync_min
= 0;
5051 mddev
->resync_max
= MaxSector
;
5052 mddev
->reshape_position
= MaxSector
;
5053 mddev
->external
= 0;
5054 mddev
->persistent
= 0;
5055 mddev
->level
= LEVEL_NONE
;
5056 mddev
->clevel
[0] = 0;
5059 mddev
->metadata_type
[0] = 0;
5060 mddev
->chunk_sectors
= 0;
5061 mddev
->ctime
= mddev
->utime
= 0;
5063 mddev
->max_disks
= 0;
5065 mddev
->can_decrease_events
= 0;
5066 mddev
->delta_disks
= 0;
5067 mddev
->new_level
= LEVEL_NONE
;
5068 mddev
->new_layout
= 0;
5069 mddev
->new_chunk_sectors
= 0;
5070 mddev
->curr_resync
= 0;
5071 mddev
->resync_mismatches
= 0;
5072 mddev
->suspend_lo
= mddev
->suspend_hi
= 0;
5073 mddev
->sync_speed_min
= mddev
->sync_speed_max
= 0;
5074 mddev
->recovery
= 0;
5077 mddev
->degraded
= 0;
5078 mddev
->safemode
= 0;
5079 mddev
->bitmap_info
.offset
= 0;
5080 mddev
->bitmap_info
.default_offset
= 0;
5081 mddev
->bitmap_info
.chunksize
= 0;
5082 mddev
->bitmap_info
.daemon_sleep
= 0;
5083 mddev
->bitmap_info
.max_write_behind
= 0;
5086 static void __md_stop_writes(struct mddev
*mddev
)
5088 if (mddev
->sync_thread
) {
5089 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
5090 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
5091 reap_sync_thread(mddev
);
5094 del_timer_sync(&mddev
->safemode_timer
);
5096 bitmap_flush(mddev
);
5097 md_super_wait(mddev
);
5099 if (!mddev
->in_sync
|| mddev
->flags
) {
5100 /* mark array as shutdown cleanly */
5102 md_update_sb(mddev
, 1);
5106 void md_stop_writes(struct mddev
*mddev
)
5109 __md_stop_writes(mddev
);
5110 mddev_unlock(mddev
);
5112 EXPORT_SYMBOL_GPL(md_stop_writes
);
5114 void md_stop(struct mddev
*mddev
)
5117 mddev
->pers
->stop(mddev
);
5118 if (mddev
->pers
->sync_request
&& mddev
->to_remove
== NULL
)
5119 mddev
->to_remove
= &md_redundancy_group
;
5120 module_put(mddev
->pers
->owner
);
5122 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
5124 EXPORT_SYMBOL_GPL(md_stop
);
5126 static int md_set_readonly(struct mddev
*mddev
, int is_open
)
5129 mutex_lock(&mddev
->open_mutex
);
5130 if (atomic_read(&mddev
->openers
) > is_open
) {
5131 printk("md: %s still in use.\n",mdname(mddev
));
5136 __md_stop_writes(mddev
);
5142 set_disk_ro(mddev
->gendisk
, 1);
5143 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
5144 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
5148 mutex_unlock(&mddev
->open_mutex
);
5153 * 0 - completely stop and dis-assemble array
5154 * 2 - stop but do not disassemble array
5156 static int do_md_stop(struct mddev
* mddev
, int mode
, int is_open
)
5158 struct gendisk
*disk
= mddev
->gendisk
;
5159 struct md_rdev
*rdev
;
5161 mutex_lock(&mddev
->open_mutex
);
5162 if (atomic_read(&mddev
->openers
) > is_open
||
5163 mddev
->sysfs_active
) {
5164 printk("md: %s still in use.\n",mdname(mddev
));
5165 mutex_unlock(&mddev
->open_mutex
);
5171 set_disk_ro(disk
, 0);
5173 __md_stop_writes(mddev
);
5175 mddev
->queue
->merge_bvec_fn
= NULL
;
5176 mddev
->queue
->backing_dev_info
.congested_fn
= NULL
;
5178 /* tell userspace to handle 'inactive' */
5179 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
5181 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
5182 if (rdev
->raid_disk
>= 0)
5183 sysfs_unlink_rdev(mddev
, rdev
);
5185 set_capacity(disk
, 0);
5186 mutex_unlock(&mddev
->open_mutex
);
5188 revalidate_disk(disk
);
5193 mutex_unlock(&mddev
->open_mutex
);
5195 * Free resources if final stop
5198 printk(KERN_INFO
"md: %s stopped.\n", mdname(mddev
));
5200 bitmap_destroy(mddev
);
5201 if (mddev
->bitmap_info
.file
) {
5202 restore_bitmap_write_access(mddev
->bitmap_info
.file
);
5203 fput(mddev
->bitmap_info
.file
);
5204 mddev
->bitmap_info
.file
= NULL
;
5206 mddev
->bitmap_info
.offset
= 0;
5208 export_array(mddev
);
5211 kobject_uevent(&disk_to_dev(mddev
->gendisk
)->kobj
, KOBJ_CHANGE
);
5212 if (mddev
->hold_active
== UNTIL_STOP
)
5213 mddev
->hold_active
= 0;
5215 blk_integrity_unregister(disk
);
5216 md_new_event(mddev
);
5217 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
5222 static void autorun_array(struct mddev
*mddev
)
5224 struct md_rdev
*rdev
;
5227 if (list_empty(&mddev
->disks
))
5230 printk(KERN_INFO
"md: running: ");
5232 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
5233 char b
[BDEVNAME_SIZE
];
5234 printk("<%s>", bdevname(rdev
->bdev
,b
));
5238 err
= do_md_run(mddev
);
5240 printk(KERN_WARNING
"md: do_md_run() returned %d\n", err
);
5241 do_md_stop(mddev
, 0, 0);
5246 * lets try to run arrays based on all disks that have arrived
5247 * until now. (those are in pending_raid_disks)
5249 * the method: pick the first pending disk, collect all disks with
5250 * the same UUID, remove all from the pending list and put them into
5251 * the 'same_array' list. Then order this list based on superblock
5252 * update time (freshest comes first), kick out 'old' disks and
5253 * compare superblocks. If everything's fine then run it.
5255 * If "unit" is allocated, then bump its reference count
5257 static void autorun_devices(int part
)
5259 struct md_rdev
*rdev0
, *rdev
, *tmp
;
5260 struct mddev
*mddev
;
5261 char b
[BDEVNAME_SIZE
];
5263 printk(KERN_INFO
"md: autorun ...\n");
5264 while (!list_empty(&pending_raid_disks
)) {
5267 LIST_HEAD(candidates
);
5268 rdev0
= list_entry(pending_raid_disks
.next
,
5269 struct md_rdev
, same_set
);
5271 printk(KERN_INFO
"md: considering %s ...\n",
5272 bdevname(rdev0
->bdev
,b
));
5273 INIT_LIST_HEAD(&candidates
);
5274 rdev_for_each_list(rdev
, tmp
, &pending_raid_disks
)
5275 if (super_90_load(rdev
, rdev0
, 0) >= 0) {
5276 printk(KERN_INFO
"md: adding %s ...\n",
5277 bdevname(rdev
->bdev
,b
));
5278 list_move(&rdev
->same_set
, &candidates
);
5281 * now we have a set of devices, with all of them having
5282 * mostly sane superblocks. It's time to allocate the
5286 dev
= MKDEV(mdp_major
,
5287 rdev0
->preferred_minor
<< MdpMinorShift
);
5288 unit
= MINOR(dev
) >> MdpMinorShift
;
5290 dev
= MKDEV(MD_MAJOR
, rdev0
->preferred_minor
);
5293 if (rdev0
->preferred_minor
!= unit
) {
5294 printk(KERN_INFO
"md: unit number in %s is bad: %d\n",
5295 bdevname(rdev0
->bdev
, b
), rdev0
->preferred_minor
);
5299 md_probe(dev
, NULL
, NULL
);
5300 mddev
= mddev_find(dev
);
5301 if (!mddev
|| !mddev
->gendisk
) {
5305 "md: cannot allocate memory for md drive.\n");
5308 if (mddev_lock(mddev
))
5309 printk(KERN_WARNING
"md: %s locked, cannot run\n",
5311 else if (mddev
->raid_disks
|| mddev
->major_version
5312 || !list_empty(&mddev
->disks
)) {
5314 "md: %s already running, cannot run %s\n",
5315 mdname(mddev
), bdevname(rdev0
->bdev
,b
));
5316 mddev_unlock(mddev
);
5318 printk(KERN_INFO
"md: created %s\n", mdname(mddev
));
5319 mddev
->persistent
= 1;
5320 rdev_for_each_list(rdev
, tmp
, &candidates
) {
5321 list_del_init(&rdev
->same_set
);
5322 if (bind_rdev_to_array(rdev
, mddev
))
5325 autorun_array(mddev
);
5326 mddev_unlock(mddev
);
5328 /* on success, candidates will be empty, on error
5331 rdev_for_each_list(rdev
, tmp
, &candidates
) {
5332 list_del_init(&rdev
->same_set
);
5337 printk(KERN_INFO
"md: ... autorun DONE.\n");
5339 #endif /* !MODULE */
5341 static int get_version(void __user
* arg
)
5345 ver
.major
= MD_MAJOR_VERSION
;
5346 ver
.minor
= MD_MINOR_VERSION
;
5347 ver
.patchlevel
= MD_PATCHLEVEL_VERSION
;
5349 if (copy_to_user(arg
, &ver
, sizeof(ver
)))
5355 static int get_array_info(struct mddev
* mddev
, void __user
* arg
)
5357 mdu_array_info_t info
;
5358 int nr
,working
,insync
,failed
,spare
;
5359 struct md_rdev
*rdev
;
5361 nr
=working
=insync
=failed
=spare
=0;
5362 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
5364 if (test_bit(Faulty
, &rdev
->flags
))
5368 if (test_bit(In_sync
, &rdev
->flags
))
5375 info
.major_version
= mddev
->major_version
;
5376 info
.minor_version
= mddev
->minor_version
;
5377 info
.patch_version
= MD_PATCHLEVEL_VERSION
;
5378 info
.ctime
= mddev
->ctime
;
5379 info
.level
= mddev
->level
;
5380 info
.size
= mddev
->dev_sectors
/ 2;
5381 if (info
.size
!= mddev
->dev_sectors
/ 2) /* overflow */
5384 info
.raid_disks
= mddev
->raid_disks
;
5385 info
.md_minor
= mddev
->md_minor
;
5386 info
.not_persistent
= !mddev
->persistent
;
5388 info
.utime
= mddev
->utime
;
5391 info
.state
= (1<<MD_SB_CLEAN
);
5392 if (mddev
->bitmap
&& mddev
->bitmap_info
.offset
)
5393 info
.state
= (1<<MD_SB_BITMAP_PRESENT
);
5394 info
.active_disks
= insync
;
5395 info
.working_disks
= working
;
5396 info
.failed_disks
= failed
;
5397 info
.spare_disks
= spare
;
5399 info
.layout
= mddev
->layout
;
5400 info
.chunk_size
= mddev
->chunk_sectors
<< 9;
5402 if (copy_to_user(arg
, &info
, sizeof(info
)))
5408 static int get_bitmap_file(struct mddev
* mddev
, void __user
* arg
)
5410 mdu_bitmap_file_t
*file
= NULL
; /* too big for stack allocation */
5411 char *ptr
, *buf
= NULL
;
5414 if (md_allow_write(mddev
))
5415 file
= kmalloc(sizeof(*file
), GFP_NOIO
);
5417 file
= kmalloc(sizeof(*file
), GFP_KERNEL
);
5422 /* bitmap disabled, zero the first byte and copy out */
5423 if (!mddev
->bitmap
|| !mddev
->bitmap
->file
) {
5424 file
->pathname
[0] = '\0';
5428 buf
= kmalloc(sizeof(file
->pathname
), GFP_KERNEL
);
5432 ptr
= d_path(&mddev
->bitmap
->file
->f_path
, buf
, sizeof(file
->pathname
));
5436 strcpy(file
->pathname
, ptr
);
5440 if (copy_to_user(arg
, file
, sizeof(*file
)))
5448 static int get_disk_info(struct mddev
* mddev
, void __user
* arg
)
5450 mdu_disk_info_t info
;
5451 struct md_rdev
*rdev
;
5453 if (copy_from_user(&info
, arg
, sizeof(info
)))
5456 rdev
= find_rdev_nr(mddev
, info
.number
);
5458 info
.major
= MAJOR(rdev
->bdev
->bd_dev
);
5459 info
.minor
= MINOR(rdev
->bdev
->bd_dev
);
5460 info
.raid_disk
= rdev
->raid_disk
;
5462 if (test_bit(Faulty
, &rdev
->flags
))
5463 info
.state
|= (1<<MD_DISK_FAULTY
);
5464 else if (test_bit(In_sync
, &rdev
->flags
)) {
5465 info
.state
|= (1<<MD_DISK_ACTIVE
);
5466 info
.state
|= (1<<MD_DISK_SYNC
);
5468 if (test_bit(WriteMostly
, &rdev
->flags
))
5469 info
.state
|= (1<<MD_DISK_WRITEMOSTLY
);
5471 info
.major
= info
.minor
= 0;
5472 info
.raid_disk
= -1;
5473 info
.state
= (1<<MD_DISK_REMOVED
);
5476 if (copy_to_user(arg
, &info
, sizeof(info
)))
5482 static int add_new_disk(struct mddev
* mddev
, mdu_disk_info_t
*info
)
5484 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
5485 struct md_rdev
*rdev
;
5486 dev_t dev
= MKDEV(info
->major
,info
->minor
);
5488 if (info
->major
!= MAJOR(dev
) || info
->minor
!= MINOR(dev
))
5491 if (!mddev
->raid_disks
) {
5493 /* expecting a device which has a superblock */
5494 rdev
= md_import_device(dev
, mddev
->major_version
, mddev
->minor_version
);
5497 "md: md_import_device returned %ld\n",
5499 return PTR_ERR(rdev
);
5501 if (!list_empty(&mddev
->disks
)) {
5502 struct md_rdev
*rdev0
5503 = list_entry(mddev
->disks
.next
,
5504 struct md_rdev
, same_set
);
5505 err
= super_types
[mddev
->major_version
]
5506 .load_super(rdev
, rdev0
, mddev
->minor_version
);
5509 "md: %s has different UUID to %s\n",
5510 bdevname(rdev
->bdev
,b
),
5511 bdevname(rdev0
->bdev
,b2
));
5516 err
= bind_rdev_to_array(rdev
, mddev
);
5523 * add_new_disk can be used once the array is assembled
5524 * to add "hot spares". They must already have a superblock
5529 if (!mddev
->pers
->hot_add_disk
) {
5531 "%s: personality does not support diskops!\n",
5535 if (mddev
->persistent
)
5536 rdev
= md_import_device(dev
, mddev
->major_version
,
5537 mddev
->minor_version
);
5539 rdev
= md_import_device(dev
, -1, -1);
5542 "md: md_import_device returned %ld\n",
5544 return PTR_ERR(rdev
);
5546 /* set saved_raid_disk if appropriate */
5547 if (!mddev
->persistent
) {
5548 if (info
->state
& (1<<MD_DISK_SYNC
) &&
5549 info
->raid_disk
< mddev
->raid_disks
) {
5550 rdev
->raid_disk
= info
->raid_disk
;
5551 set_bit(In_sync
, &rdev
->flags
);
5553 rdev
->raid_disk
= -1;
5555 super_types
[mddev
->major_version
].
5556 validate_super(mddev
, rdev
);
5557 if ((info
->state
& (1<<MD_DISK_SYNC
)) &&
5558 (!test_bit(In_sync
, &rdev
->flags
) ||
5559 rdev
->raid_disk
!= info
->raid_disk
)) {
5560 /* This was a hot-add request, but events doesn't
5561 * match, so reject it.
5567 if (test_bit(In_sync
, &rdev
->flags
))
5568 rdev
->saved_raid_disk
= rdev
->raid_disk
;
5570 rdev
->saved_raid_disk
= -1;
5572 clear_bit(In_sync
, &rdev
->flags
); /* just to be sure */
5573 if (info
->state
& (1<<MD_DISK_WRITEMOSTLY
))
5574 set_bit(WriteMostly
, &rdev
->flags
);
5576 clear_bit(WriteMostly
, &rdev
->flags
);
5578 rdev
->raid_disk
= -1;
5579 err
= bind_rdev_to_array(rdev
, mddev
);
5580 if (!err
&& !mddev
->pers
->hot_remove_disk
) {
5581 /* If there is hot_add_disk but no hot_remove_disk
5582 * then added disks for geometry changes,
5583 * and should be added immediately.
5585 super_types
[mddev
->major_version
].
5586 validate_super(mddev
, rdev
);
5587 err
= mddev
->pers
->hot_add_disk(mddev
, rdev
);
5589 unbind_rdev_from_array(rdev
);
5594 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
5596 md_update_sb(mddev
, 1);
5597 if (mddev
->degraded
)
5598 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
5599 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5601 md_new_event(mddev
);
5602 md_wakeup_thread(mddev
->thread
);
5606 /* otherwise, add_new_disk is only allowed
5607 * for major_version==0 superblocks
5609 if (mddev
->major_version
!= 0) {
5610 printk(KERN_WARNING
"%s: ADD_NEW_DISK not supported\n",
5615 if (!(info
->state
& (1<<MD_DISK_FAULTY
))) {
5617 rdev
= md_import_device(dev
, -1, 0);
5620 "md: error, md_import_device() returned %ld\n",
5622 return PTR_ERR(rdev
);
5624 rdev
->desc_nr
= info
->number
;
5625 if (info
->raid_disk
< mddev
->raid_disks
)
5626 rdev
->raid_disk
= info
->raid_disk
;
5628 rdev
->raid_disk
= -1;
5630 if (rdev
->raid_disk
< mddev
->raid_disks
)
5631 if (info
->state
& (1<<MD_DISK_SYNC
))
5632 set_bit(In_sync
, &rdev
->flags
);
5634 if (info
->state
& (1<<MD_DISK_WRITEMOSTLY
))
5635 set_bit(WriteMostly
, &rdev
->flags
);
5637 if (!mddev
->persistent
) {
5638 printk(KERN_INFO
"md: nonpersistent superblock ...\n");
5639 rdev
->sb_start
= i_size_read(rdev
->bdev
->bd_inode
) / 512;
5641 rdev
->sb_start
= calc_dev_sboffset(rdev
);
5642 rdev
->sectors
= rdev
->sb_start
;
5644 err
= bind_rdev_to_array(rdev
, mddev
);
5654 static int hot_remove_disk(struct mddev
* mddev
, dev_t dev
)
5656 char b
[BDEVNAME_SIZE
];
5657 struct md_rdev
*rdev
;
5659 rdev
= find_rdev(mddev
, dev
);
5663 if (rdev
->raid_disk
>= 0)
5666 kick_rdev_from_array(rdev
);
5667 md_update_sb(mddev
, 1);
5668 md_new_event(mddev
);
5672 printk(KERN_WARNING
"md: cannot remove active disk %s from %s ...\n",
5673 bdevname(rdev
->bdev
,b
), mdname(mddev
));
5677 static int hot_add_disk(struct mddev
* mddev
, dev_t dev
)
5679 char b
[BDEVNAME_SIZE
];
5681 struct md_rdev
*rdev
;
5686 if (mddev
->major_version
!= 0) {
5687 printk(KERN_WARNING
"%s: HOT_ADD may only be used with"
5688 " version-0 superblocks.\n",
5692 if (!mddev
->pers
->hot_add_disk
) {
5694 "%s: personality does not support diskops!\n",
5699 rdev
= md_import_device(dev
, -1, 0);
5702 "md: error, md_import_device() returned %ld\n",
5707 if (mddev
->persistent
)
5708 rdev
->sb_start
= calc_dev_sboffset(rdev
);
5710 rdev
->sb_start
= i_size_read(rdev
->bdev
->bd_inode
) / 512;
5712 rdev
->sectors
= rdev
->sb_start
;
5714 if (test_bit(Faulty
, &rdev
->flags
)) {
5716 "md: can not hot-add faulty %s disk to %s!\n",
5717 bdevname(rdev
->bdev
,b
), mdname(mddev
));
5721 clear_bit(In_sync
, &rdev
->flags
);
5723 rdev
->saved_raid_disk
= -1;
5724 err
= bind_rdev_to_array(rdev
, mddev
);
5729 * The rest should better be atomic, we can have disk failures
5730 * noticed in interrupt contexts ...
5733 rdev
->raid_disk
= -1;
5735 md_update_sb(mddev
, 1);
5738 * Kick recovery, maybe this spare has to be added to the
5739 * array immediately.
5741 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5742 md_wakeup_thread(mddev
->thread
);
5743 md_new_event(mddev
);
5751 static int set_bitmap_file(struct mddev
*mddev
, int fd
)
5756 if (!mddev
->pers
->quiesce
)
5758 if (mddev
->recovery
|| mddev
->sync_thread
)
5760 /* we should be able to change the bitmap.. */
5766 return -EEXIST
; /* cannot add when bitmap is present */
5767 mddev
->bitmap_info
.file
= fget(fd
);
5769 if (mddev
->bitmap_info
.file
== NULL
) {
5770 printk(KERN_ERR
"%s: error: failed to get bitmap file\n",
5775 err
= deny_bitmap_write_access(mddev
->bitmap_info
.file
);
5777 printk(KERN_ERR
"%s: error: bitmap file is already in use\n",
5779 fput(mddev
->bitmap_info
.file
);
5780 mddev
->bitmap_info
.file
= NULL
;
5783 mddev
->bitmap_info
.offset
= 0; /* file overrides offset */
5784 } else if (mddev
->bitmap
== NULL
)
5785 return -ENOENT
; /* cannot remove what isn't there */
5788 mddev
->pers
->quiesce(mddev
, 1);
5790 err
= bitmap_create(mddev
);
5792 err
= bitmap_load(mddev
);
5794 if (fd
< 0 || err
) {
5795 bitmap_destroy(mddev
);
5796 fd
= -1; /* make sure to put the file */
5798 mddev
->pers
->quiesce(mddev
, 0);
5801 if (mddev
->bitmap_info
.file
) {
5802 restore_bitmap_write_access(mddev
->bitmap_info
.file
);
5803 fput(mddev
->bitmap_info
.file
);
5805 mddev
->bitmap_info
.file
= NULL
;
5812 * set_array_info is used two different ways
5813 * The original usage is when creating a new array.
5814 * In this usage, raid_disks is > 0 and it together with
5815 * level, size, not_persistent,layout,chunksize determine the
5816 * shape of the array.
5817 * This will always create an array with a type-0.90.0 superblock.
5818 * The newer usage is when assembling an array.
5819 * In this case raid_disks will be 0, and the major_version field is
5820 * use to determine which style super-blocks are to be found on the devices.
5821 * The minor and patch _version numbers are also kept incase the
5822 * super_block handler wishes to interpret them.
5824 static int set_array_info(struct mddev
* mddev
, mdu_array_info_t
*info
)
5827 if (info
->raid_disks
== 0) {
5828 /* just setting version number for superblock loading */
5829 if (info
->major_version
< 0 ||
5830 info
->major_version
>= ARRAY_SIZE(super_types
) ||
5831 super_types
[info
->major_version
].name
== NULL
) {
5832 /* maybe try to auto-load a module? */
5834 "md: superblock version %d not known\n",
5835 info
->major_version
);
5838 mddev
->major_version
= info
->major_version
;
5839 mddev
->minor_version
= info
->minor_version
;
5840 mddev
->patch_version
= info
->patch_version
;
5841 mddev
->persistent
= !info
->not_persistent
;
5842 /* ensure mddev_put doesn't delete this now that there
5843 * is some minimal configuration.
5845 mddev
->ctime
= get_seconds();
5848 mddev
->major_version
= MD_MAJOR_VERSION
;
5849 mddev
->minor_version
= MD_MINOR_VERSION
;
5850 mddev
->patch_version
= MD_PATCHLEVEL_VERSION
;
5851 mddev
->ctime
= get_seconds();
5853 mddev
->level
= info
->level
;
5854 mddev
->clevel
[0] = 0;
5855 mddev
->dev_sectors
= 2 * (sector_t
)info
->size
;
5856 mddev
->raid_disks
= info
->raid_disks
;
5857 /* don't set md_minor, it is determined by which /dev/md* was
5860 if (info
->state
& (1<<MD_SB_CLEAN
))
5861 mddev
->recovery_cp
= MaxSector
;
5863 mddev
->recovery_cp
= 0;
5864 mddev
->persistent
= ! info
->not_persistent
;
5865 mddev
->external
= 0;
5867 mddev
->layout
= info
->layout
;
5868 mddev
->chunk_sectors
= info
->chunk_size
>> 9;
5870 mddev
->max_disks
= MD_SB_DISKS
;
5872 if (mddev
->persistent
)
5874 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
5876 mddev
->bitmap_info
.default_offset
= MD_SB_BYTES
>> 9;
5877 mddev
->bitmap_info
.offset
= 0;
5879 mddev
->reshape_position
= MaxSector
;
5882 * Generate a 128 bit UUID
5884 get_random_bytes(mddev
->uuid
, 16);
5886 mddev
->new_level
= mddev
->level
;
5887 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
5888 mddev
->new_layout
= mddev
->layout
;
5889 mddev
->delta_disks
= 0;
5894 void md_set_array_sectors(struct mddev
*mddev
, sector_t array_sectors
)
5896 WARN(!mddev_is_locked(mddev
), "%s: unlocked mddev!\n", __func__
);
5898 if (mddev
->external_size
)
5901 mddev
->array_sectors
= array_sectors
;
5903 EXPORT_SYMBOL(md_set_array_sectors
);
5905 static int update_size(struct mddev
*mddev
, sector_t num_sectors
)
5907 struct md_rdev
*rdev
;
5909 int fit
= (num_sectors
== 0);
5911 if (mddev
->pers
->resize
== NULL
)
5913 /* The "num_sectors" is the number of sectors of each device that
5914 * is used. This can only make sense for arrays with redundancy.
5915 * linear and raid0 always use whatever space is available. We can only
5916 * consider changing this number if no resync or reconstruction is
5917 * happening, and if the new size is acceptable. It must fit before the
5918 * sb_start or, if that is <data_offset, it must fit before the size
5919 * of each device. If num_sectors is zero, we find the largest size
5922 if (mddev
->sync_thread
)
5925 /* Sorry, cannot grow a bitmap yet, just remove it,
5929 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
5930 sector_t avail
= rdev
->sectors
;
5932 if (fit
&& (num_sectors
== 0 || num_sectors
> avail
))
5933 num_sectors
= avail
;
5934 if (avail
< num_sectors
)
5937 rv
= mddev
->pers
->resize(mddev
, num_sectors
);
5939 revalidate_disk(mddev
->gendisk
);
5943 static int update_raid_disks(struct mddev
*mddev
, int raid_disks
)
5946 /* change the number of raid disks */
5947 if (mddev
->pers
->check_reshape
== NULL
)
5949 if (raid_disks
<= 0 ||
5950 (mddev
->max_disks
&& raid_disks
>= mddev
->max_disks
))
5952 if (mddev
->sync_thread
|| mddev
->reshape_position
!= MaxSector
)
5954 mddev
->delta_disks
= raid_disks
- mddev
->raid_disks
;
5956 rv
= mddev
->pers
->check_reshape(mddev
);
5958 mddev
->delta_disks
= 0;
5964 * update_array_info is used to change the configuration of an
5966 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
5967 * fields in the info are checked against the array.
5968 * Any differences that cannot be handled will cause an error.
5969 * Normally, only one change can be managed at a time.
5971 static int update_array_info(struct mddev
*mddev
, mdu_array_info_t
*info
)
5977 /* calculate expected state,ignoring low bits */
5978 if (mddev
->bitmap
&& mddev
->bitmap_info
.offset
)
5979 state
|= (1 << MD_SB_BITMAP_PRESENT
);
5981 if (mddev
->major_version
!= info
->major_version
||
5982 mddev
->minor_version
!= info
->minor_version
||
5983 /* mddev->patch_version != info->patch_version || */
5984 mddev
->ctime
!= info
->ctime
||
5985 mddev
->level
!= info
->level
||
5986 /* mddev->layout != info->layout || */
5987 !mddev
->persistent
!= info
->not_persistent
||
5988 mddev
->chunk_sectors
!= info
->chunk_size
>> 9 ||
5989 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
5990 ((state
^info
->state
) & 0xfffffe00)
5993 /* Check there is only one change */
5994 if (info
->size
>= 0 && mddev
->dev_sectors
/ 2 != info
->size
)
5996 if (mddev
->raid_disks
!= info
->raid_disks
)
5998 if (mddev
->layout
!= info
->layout
)
6000 if ((state
^ info
->state
) & (1<<MD_SB_BITMAP_PRESENT
))
6007 if (mddev
->layout
!= info
->layout
) {
6009 * we don't need to do anything at the md level, the
6010 * personality will take care of it all.
6012 if (mddev
->pers
->check_reshape
== NULL
)
6015 mddev
->new_layout
= info
->layout
;
6016 rv
= mddev
->pers
->check_reshape(mddev
);
6018 mddev
->new_layout
= mddev
->layout
;
6022 if (info
->size
>= 0 && mddev
->dev_sectors
/ 2 != info
->size
)
6023 rv
= update_size(mddev
, (sector_t
)info
->size
* 2);
6025 if (mddev
->raid_disks
!= info
->raid_disks
)
6026 rv
= update_raid_disks(mddev
, info
->raid_disks
);
6028 if ((state
^ info
->state
) & (1<<MD_SB_BITMAP_PRESENT
)) {
6029 if (mddev
->pers
->quiesce
== NULL
)
6031 if (mddev
->recovery
|| mddev
->sync_thread
)
6033 if (info
->state
& (1<<MD_SB_BITMAP_PRESENT
)) {
6034 /* add the bitmap */
6037 if (mddev
->bitmap_info
.default_offset
== 0)
6039 mddev
->bitmap_info
.offset
=
6040 mddev
->bitmap_info
.default_offset
;
6041 mddev
->pers
->quiesce(mddev
, 1);
6042 rv
= bitmap_create(mddev
);
6044 rv
= bitmap_load(mddev
);
6046 bitmap_destroy(mddev
);
6047 mddev
->pers
->quiesce(mddev
, 0);
6049 /* remove the bitmap */
6052 if (mddev
->bitmap
->file
)
6054 mddev
->pers
->quiesce(mddev
, 1);
6055 bitmap_destroy(mddev
);
6056 mddev
->pers
->quiesce(mddev
, 0);
6057 mddev
->bitmap_info
.offset
= 0;
6060 md_update_sb(mddev
, 1);
6064 static int set_disk_faulty(struct mddev
*mddev
, dev_t dev
)
6066 struct md_rdev
*rdev
;
6068 if (mddev
->pers
== NULL
)
6071 rdev
= find_rdev(mddev
, dev
);
6075 md_error(mddev
, rdev
);
6076 if (!test_bit(Faulty
, &rdev
->flags
))
6082 * We have a problem here : there is no easy way to give a CHS
6083 * virtual geometry. We currently pretend that we have a 2 heads
6084 * 4 sectors (with a BIG number of cylinders...). This drives
6085 * dosfs just mad... ;-)
6087 static int md_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
6089 struct mddev
*mddev
= bdev
->bd_disk
->private_data
;
6093 geo
->cylinders
= mddev
->array_sectors
/ 8;
6097 static int md_ioctl(struct block_device
*bdev
, fmode_t mode
,
6098 unsigned int cmd
, unsigned long arg
)
6101 void __user
*argp
= (void __user
*)arg
;
6102 struct mddev
*mddev
= NULL
;
6107 case GET_ARRAY_INFO
:
6111 if (!capable(CAP_SYS_ADMIN
))
6116 * Commands dealing with the RAID driver but not any
6122 err
= get_version(argp
);
6125 case PRINT_RAID_DEBUG
:
6133 autostart_arrays(arg
);
6140 * Commands creating/starting a new array:
6143 mddev
= bdev
->bd_disk
->private_data
;
6150 err
= mddev_lock(mddev
);
6153 "md: ioctl lock interrupted, reason %d, cmd %d\n",
6160 case SET_ARRAY_INFO
:
6162 mdu_array_info_t info
;
6164 memset(&info
, 0, sizeof(info
));
6165 else if (copy_from_user(&info
, argp
, sizeof(info
))) {
6170 err
= update_array_info(mddev
, &info
);
6172 printk(KERN_WARNING
"md: couldn't update"
6173 " array info. %d\n", err
);
6178 if (!list_empty(&mddev
->disks
)) {
6180 "md: array %s already has disks!\n",
6185 if (mddev
->raid_disks
) {
6187 "md: array %s already initialised!\n",
6192 err
= set_array_info(mddev
, &info
);
6194 printk(KERN_WARNING
"md: couldn't set"
6195 " array info. %d\n", err
);
6205 * Commands querying/configuring an existing array:
6207 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
6208 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
6209 if ((!mddev
->raid_disks
&& !mddev
->external
)
6210 && cmd
!= ADD_NEW_DISK
&& cmd
!= STOP_ARRAY
6211 && cmd
!= RUN_ARRAY
&& cmd
!= SET_BITMAP_FILE
6212 && cmd
!= GET_BITMAP_FILE
) {
6218 * Commands even a read-only array can execute:
6222 case GET_ARRAY_INFO
:
6223 err
= get_array_info(mddev
, argp
);
6226 case GET_BITMAP_FILE
:
6227 err
= get_bitmap_file(mddev
, argp
);
6231 err
= get_disk_info(mddev
, argp
);
6234 case RESTART_ARRAY_RW
:
6235 err
= restart_array(mddev
);
6239 err
= do_md_stop(mddev
, 0, 1);
6243 err
= md_set_readonly(mddev
, 1);
6247 if (get_user(ro
, (int __user
*)(arg
))) {
6253 /* if the bdev is going readonly the value of mddev->ro
6254 * does not matter, no writes are coming
6259 /* are we are already prepared for writes? */
6263 /* transitioning to readauto need only happen for
6264 * arrays that call md_write_start
6267 err
= restart_array(mddev
);
6270 set_disk_ro(mddev
->gendisk
, 0);
6277 * The remaining ioctls are changing the state of the
6278 * superblock, so we do not allow them on read-only arrays.
6279 * However non-MD ioctls (e.g. get-size) will still come through
6280 * here and hit the 'default' below, so only disallow
6281 * 'md' ioctls, and switch to rw mode if started auto-readonly.
6283 if (_IOC_TYPE(cmd
) == MD_MAJOR
&& mddev
->ro
&& mddev
->pers
) {
6284 if (mddev
->ro
== 2) {
6286 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
6287 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6288 md_wakeup_thread(mddev
->thread
);
6299 mdu_disk_info_t info
;
6300 if (copy_from_user(&info
, argp
, sizeof(info
)))
6303 err
= add_new_disk(mddev
, &info
);
6307 case HOT_REMOVE_DISK
:
6308 err
= hot_remove_disk(mddev
, new_decode_dev(arg
));
6312 err
= hot_add_disk(mddev
, new_decode_dev(arg
));
6315 case SET_DISK_FAULTY
:
6316 err
= set_disk_faulty(mddev
, new_decode_dev(arg
));
6320 err
= do_md_run(mddev
);
6323 case SET_BITMAP_FILE
:
6324 err
= set_bitmap_file(mddev
, (int)arg
);
6334 if (mddev
->hold_active
== UNTIL_IOCTL
&&
6336 mddev
->hold_active
= 0;
6337 mddev_unlock(mddev
);
6346 #ifdef CONFIG_COMPAT
6347 static int md_compat_ioctl(struct block_device
*bdev
, fmode_t mode
,
6348 unsigned int cmd
, unsigned long arg
)
6351 case HOT_REMOVE_DISK
:
6353 case SET_DISK_FAULTY
:
6354 case SET_BITMAP_FILE
:
6355 /* These take in integer arg, do not convert */
6358 arg
= (unsigned long)compat_ptr(arg
);
6362 return md_ioctl(bdev
, mode
, cmd
, arg
);
6364 #endif /* CONFIG_COMPAT */
6366 static int md_open(struct block_device
*bdev
, fmode_t mode
)
6369 * Succeed if we can lock the mddev, which confirms that
6370 * it isn't being stopped right now.
6372 struct mddev
*mddev
= mddev_find(bdev
->bd_dev
);
6375 if (mddev
->gendisk
!= bdev
->bd_disk
) {
6376 /* we are racing with mddev_put which is discarding this
6380 /* Wait until bdev->bd_disk is definitely gone */
6381 flush_workqueue(md_misc_wq
);
6382 /* Then retry the open from the top */
6383 return -ERESTARTSYS
;
6385 BUG_ON(mddev
!= bdev
->bd_disk
->private_data
);
6387 if ((err
= mutex_lock_interruptible(&mddev
->open_mutex
)))
6391 atomic_inc(&mddev
->openers
);
6392 mutex_unlock(&mddev
->open_mutex
);
6394 check_disk_change(bdev
);
6399 static int md_release(struct gendisk
*disk
, fmode_t mode
)
6401 struct mddev
*mddev
= disk
->private_data
;
6404 atomic_dec(&mddev
->openers
);
6410 static int md_media_changed(struct gendisk
*disk
)
6412 struct mddev
*mddev
= disk
->private_data
;
6414 return mddev
->changed
;
6417 static int md_revalidate(struct gendisk
*disk
)
6419 struct mddev
*mddev
= disk
->private_data
;
6424 static const struct block_device_operations md_fops
=
6426 .owner
= THIS_MODULE
,
6428 .release
= md_release
,
6430 #ifdef CONFIG_COMPAT
6431 .compat_ioctl
= md_compat_ioctl
,
6433 .getgeo
= md_getgeo
,
6434 .media_changed
= md_media_changed
,
6435 .revalidate_disk
= md_revalidate
,
6438 static int md_thread(void * arg
)
6440 struct md_thread
*thread
= arg
;
6443 * md_thread is a 'system-thread', it's priority should be very
6444 * high. We avoid resource deadlocks individually in each
6445 * raid personality. (RAID5 does preallocation) We also use RR and
6446 * the very same RT priority as kswapd, thus we will never get
6447 * into a priority inversion deadlock.
6449 * we definitely have to have equal or higher priority than
6450 * bdflush, otherwise bdflush will deadlock if there are too
6451 * many dirty RAID5 blocks.
6454 allow_signal(SIGKILL
);
6455 while (!kthread_should_stop()) {
6457 /* We need to wait INTERRUPTIBLE so that
6458 * we don't add to the load-average.
6459 * That means we need to be sure no signals are
6462 if (signal_pending(current
))
6463 flush_signals(current
);
6465 wait_event_interruptible_timeout
6467 test_bit(THREAD_WAKEUP
, &thread
->flags
)
6468 || kthread_should_stop(),
6471 clear_bit(THREAD_WAKEUP
, &thread
->flags
);
6472 if (!kthread_should_stop())
6473 thread
->run(thread
->mddev
);
6479 void md_wakeup_thread(struct md_thread
*thread
)
6482 pr_debug("md: waking up MD thread %s.\n", thread
->tsk
->comm
);
6483 set_bit(THREAD_WAKEUP
, &thread
->flags
);
6484 wake_up(&thread
->wqueue
);
6488 struct md_thread
*md_register_thread(void (*run
) (struct mddev
*), struct mddev
*mddev
,
6491 struct md_thread
*thread
;
6493 thread
= kzalloc(sizeof(struct md_thread
), GFP_KERNEL
);
6497 init_waitqueue_head(&thread
->wqueue
);
6500 thread
->mddev
= mddev
;
6501 thread
->timeout
= MAX_SCHEDULE_TIMEOUT
;
6502 thread
->tsk
= kthread_run(md_thread
, thread
,
6504 mdname(thread
->mddev
),
6505 name
?: mddev
->pers
->name
);
6506 if (IS_ERR(thread
->tsk
)) {
6513 void md_unregister_thread(struct md_thread
**threadp
)
6515 struct md_thread
*thread
= *threadp
;
6518 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread
->tsk
));
6519 /* Locking ensures that mddev_unlock does not wake_up a
6520 * non-existent thread
6522 spin_lock(&pers_lock
);
6524 spin_unlock(&pers_lock
);
6526 kthread_stop(thread
->tsk
);
6530 void md_error(struct mddev
*mddev
, struct md_rdev
*rdev
)
6537 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
))
6540 if (!mddev
->pers
|| !mddev
->pers
->error_handler
)
6542 mddev
->pers
->error_handler(mddev
,rdev
);
6543 if (mddev
->degraded
)
6544 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
6545 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
6546 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6547 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6548 md_wakeup_thread(mddev
->thread
);
6549 if (mddev
->event_work
.func
)
6550 queue_work(md_misc_wq
, &mddev
->event_work
);
6551 md_new_event_inintr(mddev
);
6554 /* seq_file implementation /proc/mdstat */
6556 static void status_unused(struct seq_file
*seq
)
6559 struct md_rdev
*rdev
;
6561 seq_printf(seq
, "unused devices: ");
6563 list_for_each_entry(rdev
, &pending_raid_disks
, same_set
) {
6564 char b
[BDEVNAME_SIZE
];
6566 seq_printf(seq
, "%s ",
6567 bdevname(rdev
->bdev
,b
));
6570 seq_printf(seq
, "<none>");
6572 seq_printf(seq
, "\n");
6576 static void status_resync(struct seq_file
*seq
, struct mddev
* mddev
)
6578 sector_t max_sectors
, resync
, res
;
6579 unsigned long dt
, db
;
6582 unsigned int per_milli
;
6584 resync
= mddev
->curr_resync
- atomic_read(&mddev
->recovery_active
);
6586 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
6587 max_sectors
= mddev
->resync_max_sectors
;
6589 max_sectors
= mddev
->dev_sectors
;
6592 * Should not happen.
6598 /* Pick 'scale' such that (resync>>scale)*1000 will fit
6599 * in a sector_t, and (max_sectors>>scale) will fit in a
6600 * u32, as those are the requirements for sector_div.
6601 * Thus 'scale' must be at least 10
6604 if (sizeof(sector_t
) > sizeof(unsigned long)) {
6605 while ( max_sectors
/2 > (1ULL<<(scale
+32)))
6608 res
= (resync
>>scale
)*1000;
6609 sector_div(res
, (u32
)((max_sectors
>>scale
)+1));
6613 int i
, x
= per_milli
/50, y
= 20-x
;
6614 seq_printf(seq
, "[");
6615 for (i
= 0; i
< x
; i
++)
6616 seq_printf(seq
, "=");
6617 seq_printf(seq
, ">");
6618 for (i
= 0; i
< y
; i
++)
6619 seq_printf(seq
, ".");
6620 seq_printf(seq
, "] ");
6622 seq_printf(seq
, " %s =%3u.%u%% (%llu/%llu)",
6623 (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
)?
6625 (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
)?
6627 (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ?
6628 "resync" : "recovery"))),
6629 per_milli
/10, per_milli
% 10,
6630 (unsigned long long) resync
/2,
6631 (unsigned long long) max_sectors
/2);
6634 * dt: time from mark until now
6635 * db: blocks written from mark until now
6636 * rt: remaining time
6638 * rt is a sector_t, so could be 32bit or 64bit.
6639 * So we divide before multiply in case it is 32bit and close
6641 * We scale the divisor (db) by 32 to avoid losing precision
6642 * near the end of resync when the number of remaining sectors
6644 * We then divide rt by 32 after multiplying by db to compensate.
6645 * The '+1' avoids division by zero if db is very small.
6647 dt
= ((jiffies
- mddev
->resync_mark
) / HZ
);
6649 db
= (mddev
->curr_mark_cnt
- atomic_read(&mddev
->recovery_active
))
6650 - mddev
->resync_mark_cnt
;
6652 rt
= max_sectors
- resync
; /* number of remaining sectors */
6653 sector_div(rt
, db
/32+1);
6657 seq_printf(seq
, " finish=%lu.%lumin", (unsigned long)rt
/ 60,
6658 ((unsigned long)rt
% 60)/6);
6660 seq_printf(seq
, " speed=%ldK/sec", db
/2/dt
);
6663 static void *md_seq_start(struct seq_file
*seq
, loff_t
*pos
)
6665 struct list_head
*tmp
;
6667 struct mddev
*mddev
;
6675 spin_lock(&all_mddevs_lock
);
6676 list_for_each(tmp
,&all_mddevs
)
6678 mddev
= list_entry(tmp
, struct mddev
, all_mddevs
);
6680 spin_unlock(&all_mddevs_lock
);
6683 spin_unlock(&all_mddevs_lock
);
6685 return (void*)2;/* tail */
6689 static void *md_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
6691 struct list_head
*tmp
;
6692 struct mddev
*next_mddev
, *mddev
= v
;
6698 spin_lock(&all_mddevs_lock
);
6700 tmp
= all_mddevs
.next
;
6702 tmp
= mddev
->all_mddevs
.next
;
6703 if (tmp
!= &all_mddevs
)
6704 next_mddev
= mddev_get(list_entry(tmp
,struct mddev
,all_mddevs
));
6706 next_mddev
= (void*)2;
6709 spin_unlock(&all_mddevs_lock
);
6717 static void md_seq_stop(struct seq_file
*seq
, void *v
)
6719 struct mddev
*mddev
= v
;
6721 if (mddev
&& v
!= (void*)1 && v
!= (void*)2)
6725 static int md_seq_show(struct seq_file
*seq
, void *v
)
6727 struct mddev
*mddev
= v
;
6729 struct md_rdev
*rdev
;
6730 struct bitmap
*bitmap
;
6732 if (v
== (void*)1) {
6733 struct md_personality
*pers
;
6734 seq_printf(seq
, "Personalities : ");
6735 spin_lock(&pers_lock
);
6736 list_for_each_entry(pers
, &pers_list
, list
)
6737 seq_printf(seq
, "[%s] ", pers
->name
);
6739 spin_unlock(&pers_lock
);
6740 seq_printf(seq
, "\n");
6741 seq
->poll_event
= atomic_read(&md_event_count
);
6744 if (v
== (void*)2) {
6749 if (mddev_lock(mddev
) < 0)
6752 if (mddev
->pers
|| mddev
->raid_disks
|| !list_empty(&mddev
->disks
)) {
6753 seq_printf(seq
, "%s : %sactive", mdname(mddev
),
6754 mddev
->pers
? "" : "in");
6757 seq_printf(seq
, " (read-only)");
6759 seq_printf(seq
, " (auto-read-only)");
6760 seq_printf(seq
, " %s", mddev
->pers
->name
);
6764 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
6765 char b
[BDEVNAME_SIZE
];
6766 seq_printf(seq
, " %s[%d]",
6767 bdevname(rdev
->bdev
,b
), rdev
->desc_nr
);
6768 if (test_bit(WriteMostly
, &rdev
->flags
))
6769 seq_printf(seq
, "(W)");
6770 if (test_bit(Faulty
, &rdev
->flags
)) {
6771 seq_printf(seq
, "(F)");
6774 if (rdev
->raid_disk
< 0)
6775 seq_printf(seq
, "(S)"); /* spare */
6776 if (test_bit(Replacement
, &rdev
->flags
))
6777 seq_printf(seq
, "(R)");
6778 sectors
+= rdev
->sectors
;
6781 if (!list_empty(&mddev
->disks
)) {
6783 seq_printf(seq
, "\n %llu blocks",
6784 (unsigned long long)
6785 mddev
->array_sectors
/ 2);
6787 seq_printf(seq
, "\n %llu blocks",
6788 (unsigned long long)sectors
/ 2);
6790 if (mddev
->persistent
) {
6791 if (mddev
->major_version
!= 0 ||
6792 mddev
->minor_version
!= 90) {
6793 seq_printf(seq
," super %d.%d",
6794 mddev
->major_version
,
6795 mddev
->minor_version
);
6797 } else if (mddev
->external
)
6798 seq_printf(seq
, " super external:%s",
6799 mddev
->metadata_type
);
6801 seq_printf(seq
, " super non-persistent");
6804 mddev
->pers
->status(seq
, mddev
);
6805 seq_printf(seq
, "\n ");
6806 if (mddev
->pers
->sync_request
) {
6807 if (mddev
->curr_resync
> 2) {
6808 status_resync(seq
, mddev
);
6809 seq_printf(seq
, "\n ");
6810 } else if (mddev
->curr_resync
== 1 || mddev
->curr_resync
== 2)
6811 seq_printf(seq
, "\tresync=DELAYED\n ");
6812 else if (mddev
->recovery_cp
< MaxSector
)
6813 seq_printf(seq
, "\tresync=PENDING\n ");
6816 seq_printf(seq
, "\n ");
6818 if ((bitmap
= mddev
->bitmap
)) {
6819 unsigned long chunk_kb
;
6820 unsigned long flags
;
6821 spin_lock_irqsave(&bitmap
->lock
, flags
);
6822 chunk_kb
= mddev
->bitmap_info
.chunksize
>> 10;
6823 seq_printf(seq
, "bitmap: %lu/%lu pages [%luKB], "
6825 bitmap
->pages
- bitmap
->missing_pages
,
6827 (bitmap
->pages
- bitmap
->missing_pages
)
6828 << (PAGE_SHIFT
- 10),
6829 chunk_kb
? chunk_kb
: mddev
->bitmap_info
.chunksize
,
6830 chunk_kb
? "KB" : "B");
6832 seq_printf(seq
, ", file: ");
6833 seq_path(seq
, &bitmap
->file
->f_path
, " \t\n");
6836 seq_printf(seq
, "\n");
6837 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
6840 seq_printf(seq
, "\n");
6842 mddev_unlock(mddev
);
6847 static const struct seq_operations md_seq_ops
= {
6848 .start
= md_seq_start
,
6849 .next
= md_seq_next
,
6850 .stop
= md_seq_stop
,
6851 .show
= md_seq_show
,
6854 static int md_seq_open(struct inode
*inode
, struct file
*file
)
6856 struct seq_file
*seq
;
6859 error
= seq_open(file
, &md_seq_ops
);
6863 seq
= file
->private_data
;
6864 seq
->poll_event
= atomic_read(&md_event_count
);
6868 static unsigned int mdstat_poll(struct file
*filp
, poll_table
*wait
)
6870 struct seq_file
*seq
= filp
->private_data
;
6873 poll_wait(filp
, &md_event_waiters
, wait
);
6875 /* always allow read */
6876 mask
= POLLIN
| POLLRDNORM
;
6878 if (seq
->poll_event
!= atomic_read(&md_event_count
))
6879 mask
|= POLLERR
| POLLPRI
;
6883 static const struct file_operations md_seq_fops
= {
6884 .owner
= THIS_MODULE
,
6885 .open
= md_seq_open
,
6887 .llseek
= seq_lseek
,
6888 .release
= seq_release_private
,
6889 .poll
= mdstat_poll
,
6892 int register_md_personality(struct md_personality
*p
)
6894 spin_lock(&pers_lock
);
6895 list_add_tail(&p
->list
, &pers_list
);
6896 printk(KERN_INFO
"md: %s personality registered for level %d\n", p
->name
, p
->level
);
6897 spin_unlock(&pers_lock
);
6901 int unregister_md_personality(struct md_personality
*p
)
6903 printk(KERN_INFO
"md: %s personality unregistered\n", p
->name
);
6904 spin_lock(&pers_lock
);
6905 list_del_init(&p
->list
);
6906 spin_unlock(&pers_lock
);
6910 static int is_mddev_idle(struct mddev
*mddev
, int init
)
6912 struct md_rdev
* rdev
;
6918 rdev_for_each_rcu(rdev
, mddev
) {
6919 struct gendisk
*disk
= rdev
->bdev
->bd_contains
->bd_disk
;
6920 curr_events
= (int)part_stat_read(&disk
->part0
, sectors
[0]) +
6921 (int)part_stat_read(&disk
->part0
, sectors
[1]) -
6922 atomic_read(&disk
->sync_io
);
6923 /* sync IO will cause sync_io to increase before the disk_stats
6924 * as sync_io is counted when a request starts, and
6925 * disk_stats is counted when it completes.
6926 * So resync activity will cause curr_events to be smaller than
6927 * when there was no such activity.
6928 * non-sync IO will cause disk_stat to increase without
6929 * increasing sync_io so curr_events will (eventually)
6930 * be larger than it was before. Once it becomes
6931 * substantially larger, the test below will cause
6932 * the array to appear non-idle, and resync will slow
6934 * If there is a lot of outstanding resync activity when
6935 * we set last_event to curr_events, then all that activity
6936 * completing might cause the array to appear non-idle
6937 * and resync will be slowed down even though there might
6938 * not have been non-resync activity. This will only
6939 * happen once though. 'last_events' will soon reflect
6940 * the state where there is little or no outstanding
6941 * resync requests, and further resync activity will
6942 * always make curr_events less than last_events.
6945 if (init
|| curr_events
- rdev
->last_events
> 64) {
6946 rdev
->last_events
= curr_events
;
6954 void md_done_sync(struct mddev
*mddev
, int blocks
, int ok
)
6956 /* another "blocks" (512byte) blocks have been synced */
6957 atomic_sub(blocks
, &mddev
->recovery_active
);
6958 wake_up(&mddev
->recovery_wait
);
6960 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6961 md_wakeup_thread(mddev
->thread
);
6962 // stop recovery, signal do_sync ....
6967 /* md_write_start(mddev, bi)
6968 * If we need to update some array metadata (e.g. 'active' flag
6969 * in superblock) before writing, schedule a superblock update
6970 * and wait for it to complete.
6972 void md_write_start(struct mddev
*mddev
, struct bio
*bi
)
6975 if (bio_data_dir(bi
) != WRITE
)
6978 BUG_ON(mddev
->ro
== 1);
6979 if (mddev
->ro
== 2) {
6980 /* need to switch to read/write */
6982 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6983 md_wakeup_thread(mddev
->thread
);
6984 md_wakeup_thread(mddev
->sync_thread
);
6987 atomic_inc(&mddev
->writes_pending
);
6988 if (mddev
->safemode
== 1)
6989 mddev
->safemode
= 0;
6990 if (mddev
->in_sync
) {
6991 spin_lock_irq(&mddev
->write_lock
);
6992 if (mddev
->in_sync
) {
6994 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
6995 set_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
6996 md_wakeup_thread(mddev
->thread
);
6999 spin_unlock_irq(&mddev
->write_lock
);
7002 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
7003 wait_event(mddev
->sb_wait
,
7004 !test_bit(MD_CHANGE_PENDING
, &mddev
->flags
));
7007 void md_write_end(struct mddev
*mddev
)
7009 if (atomic_dec_and_test(&mddev
->writes_pending
)) {
7010 if (mddev
->safemode
== 2)
7011 md_wakeup_thread(mddev
->thread
);
7012 else if (mddev
->safemode_delay
)
7013 mod_timer(&mddev
->safemode_timer
, jiffies
+ mddev
->safemode_delay
);
7017 /* md_allow_write(mddev)
7018 * Calling this ensures that the array is marked 'active' so that writes
7019 * may proceed without blocking. It is important to call this before
7020 * attempting a GFP_KERNEL allocation while holding the mddev lock.
7021 * Must be called with mddev_lock held.
7023 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
7024 * is dropped, so return -EAGAIN after notifying userspace.
7026 int md_allow_write(struct mddev
*mddev
)
7032 if (!mddev
->pers
->sync_request
)
7035 spin_lock_irq(&mddev
->write_lock
);
7036 if (mddev
->in_sync
) {
7038 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
7039 set_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
7040 if (mddev
->safemode_delay
&&
7041 mddev
->safemode
== 0)
7042 mddev
->safemode
= 1;
7043 spin_unlock_irq(&mddev
->write_lock
);
7044 md_update_sb(mddev
, 0);
7045 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
7047 spin_unlock_irq(&mddev
->write_lock
);
7049 if (test_bit(MD_CHANGE_PENDING
, &mddev
->flags
))
7054 EXPORT_SYMBOL_GPL(md_allow_write
);
7056 #define SYNC_MARKS 10
7057 #define SYNC_MARK_STEP (3*HZ)
7058 void md_do_sync(struct mddev
*mddev
)
7060 struct mddev
*mddev2
;
7061 unsigned int currspeed
= 0,
7063 sector_t max_sectors
,j
, io_sectors
;
7064 unsigned long mark
[SYNC_MARKS
];
7065 sector_t mark_cnt
[SYNC_MARKS
];
7067 struct list_head
*tmp
;
7068 sector_t last_check
;
7070 struct md_rdev
*rdev
;
7073 /* just incase thread restarts... */
7074 if (test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
))
7076 if (mddev
->ro
) /* never try to sync a read-only array */
7079 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
7080 if (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
))
7081 desc
= "data-check";
7082 else if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
7083 desc
= "requested-resync";
7086 } else if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
7091 /* we overload curr_resync somewhat here.
7092 * 0 == not engaged in resync at all
7093 * 2 == checking that there is no conflict with another sync
7094 * 1 == like 2, but have yielded to allow conflicting resync to
7096 * other == active in resync - this many blocks
7098 * Before starting a resync we must have set curr_resync to
7099 * 2, and then checked that every "conflicting" array has curr_resync
7100 * less than ours. When we find one that is the same or higher
7101 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
7102 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
7103 * This will mean we have to start checking from the beginning again.
7108 mddev
->curr_resync
= 2;
7111 if (kthread_should_stop())
7112 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
7114 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
7116 for_each_mddev(mddev2
, tmp
) {
7117 if (mddev2
== mddev
)
7119 if (!mddev
->parallel_resync
7120 && mddev2
->curr_resync
7121 && match_mddev_units(mddev
, mddev2
)) {
7123 if (mddev
< mddev2
&& mddev
->curr_resync
== 2) {
7124 /* arbitrarily yield */
7125 mddev
->curr_resync
= 1;
7126 wake_up(&resync_wait
);
7128 if (mddev
> mddev2
&& mddev
->curr_resync
== 1)
7129 /* no need to wait here, we can wait the next
7130 * time 'round when curr_resync == 2
7133 /* We need to wait 'interruptible' so as not to
7134 * contribute to the load average, and not to
7135 * be caught by 'softlockup'
7137 prepare_to_wait(&resync_wait
, &wq
, TASK_INTERRUPTIBLE
);
7138 if (!kthread_should_stop() &&
7139 mddev2
->curr_resync
>= mddev
->curr_resync
) {
7140 printk(KERN_INFO
"md: delaying %s of %s"
7141 " until %s has finished (they"
7142 " share one or more physical units)\n",
7143 desc
, mdname(mddev
), mdname(mddev2
));
7145 if (signal_pending(current
))
7146 flush_signals(current
);
7148 finish_wait(&resync_wait
, &wq
);
7151 finish_wait(&resync_wait
, &wq
);
7154 } while (mddev
->curr_resync
< 2);
7157 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
7158 /* resync follows the size requested by the personality,
7159 * which defaults to physical size, but can be virtual size
7161 max_sectors
= mddev
->resync_max_sectors
;
7162 mddev
->resync_mismatches
= 0;
7163 /* we don't use the checkpoint if there's a bitmap */
7164 if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
7165 j
= mddev
->resync_min
;
7166 else if (!mddev
->bitmap
)
7167 j
= mddev
->recovery_cp
;
7169 } else if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
7170 max_sectors
= mddev
->dev_sectors
;
7172 /* recovery follows the physical size of devices */
7173 max_sectors
= mddev
->dev_sectors
;
7176 list_for_each_entry_rcu(rdev
, &mddev
->disks
, same_set
)
7177 if (rdev
->raid_disk
>= 0 &&
7178 !test_bit(Faulty
, &rdev
->flags
) &&
7179 !test_bit(In_sync
, &rdev
->flags
) &&
7180 rdev
->recovery_offset
< j
)
7181 j
= rdev
->recovery_offset
;
7185 printk(KERN_INFO
"md: %s of RAID array %s\n", desc
, mdname(mddev
));
7186 printk(KERN_INFO
"md: minimum _guaranteed_ speed:"
7187 " %d KB/sec/disk.\n", speed_min(mddev
));
7188 printk(KERN_INFO
"md: using maximum available idle IO bandwidth "
7189 "(but not more than %d KB/sec) for %s.\n",
7190 speed_max(mddev
), desc
);
7192 is_mddev_idle(mddev
, 1); /* this initializes IO event counters */
7195 for (m
= 0; m
< SYNC_MARKS
; m
++) {
7197 mark_cnt
[m
] = io_sectors
;
7200 mddev
->resync_mark
= mark
[last_mark
];
7201 mddev
->resync_mark_cnt
= mark_cnt
[last_mark
];
7204 * Tune reconstruction:
7206 window
= 32*(PAGE_SIZE
/512);
7207 printk(KERN_INFO
"md: using %dk window, over a total of %lluk.\n",
7208 window
/2, (unsigned long long)max_sectors
/2);
7210 atomic_set(&mddev
->recovery_active
, 0);
7215 "md: resuming %s of %s from checkpoint.\n",
7216 desc
, mdname(mddev
));
7217 mddev
->curr_resync
= j
;
7219 mddev
->curr_resync_completed
= j
;
7221 while (j
< max_sectors
) {
7226 if (!test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
7227 ((mddev
->curr_resync
> mddev
->curr_resync_completed
&&
7228 (mddev
->curr_resync
- mddev
->curr_resync_completed
)
7229 > (max_sectors
>> 4)) ||
7230 (j
- mddev
->curr_resync_completed
)*2
7231 >= mddev
->resync_max
- mddev
->curr_resync_completed
7233 /* time to update curr_resync_completed */
7234 wait_event(mddev
->recovery_wait
,
7235 atomic_read(&mddev
->recovery_active
) == 0);
7236 mddev
->curr_resync_completed
= j
;
7237 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
7238 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
7241 while (j
>= mddev
->resync_max
&& !kthread_should_stop()) {
7242 /* As this condition is controlled by user-space,
7243 * we can block indefinitely, so use '_interruptible'
7244 * to avoid triggering warnings.
7246 flush_signals(current
); /* just in case */
7247 wait_event_interruptible(mddev
->recovery_wait
,
7248 mddev
->resync_max
> j
7249 || kthread_should_stop());
7252 if (kthread_should_stop())
7255 sectors
= mddev
->pers
->sync_request(mddev
, j
, &skipped
,
7256 currspeed
< speed_min(mddev
));
7258 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
7262 if (!skipped
) { /* actual IO requested */
7263 io_sectors
+= sectors
;
7264 atomic_add(sectors
, &mddev
->recovery_active
);
7267 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
7271 if (j
>1) mddev
->curr_resync
= j
;
7272 mddev
->curr_mark_cnt
= io_sectors
;
7273 if (last_check
== 0)
7274 /* this is the earliest that rebuild will be
7275 * visible in /proc/mdstat
7277 md_new_event(mddev
);
7279 if (last_check
+ window
> io_sectors
|| j
== max_sectors
)
7282 last_check
= io_sectors
;
7284 if (time_after_eq(jiffies
, mark
[last_mark
] + SYNC_MARK_STEP
)) {
7286 int next
= (last_mark
+1) % SYNC_MARKS
;
7288 mddev
->resync_mark
= mark
[next
];
7289 mddev
->resync_mark_cnt
= mark_cnt
[next
];
7290 mark
[next
] = jiffies
;
7291 mark_cnt
[next
] = io_sectors
- atomic_read(&mddev
->recovery_active
);
7296 if (kthread_should_stop())
7301 * this loop exits only if either when we are slower than
7302 * the 'hard' speed limit, or the system was IO-idle for
7304 * the system might be non-idle CPU-wise, but we only care
7305 * about not overloading the IO subsystem. (things like an
7306 * e2fsck being done on the RAID array should execute fast)
7310 currspeed
= ((unsigned long)(io_sectors
-mddev
->resync_mark_cnt
))/2
7311 /((jiffies
-mddev
->resync_mark
)/HZ
+1) +1;
7313 if (currspeed
> speed_min(mddev
)) {
7314 if ((currspeed
> speed_max(mddev
)) ||
7315 !is_mddev_idle(mddev
, 0)) {
7321 printk(KERN_INFO
"md: %s: %s done.\n",mdname(mddev
), desc
);
7323 * this also signals 'finished resyncing' to md_stop
7326 wait_event(mddev
->recovery_wait
, !atomic_read(&mddev
->recovery_active
));
7328 /* tell personality that we are finished */
7329 mddev
->pers
->sync_request(mddev
, max_sectors
, &skipped
, 1);
7331 if (!test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
) &&
7332 mddev
->curr_resync
> 2) {
7333 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
7334 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
7335 if (mddev
->curr_resync
>= mddev
->recovery_cp
) {
7337 "md: checkpointing %s of %s.\n",
7338 desc
, mdname(mddev
));
7339 mddev
->recovery_cp
=
7340 mddev
->curr_resync_completed
;
7343 mddev
->recovery_cp
= MaxSector
;
7345 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
7346 mddev
->curr_resync
= MaxSector
;
7348 list_for_each_entry_rcu(rdev
, &mddev
->disks
, same_set
)
7349 if (rdev
->raid_disk
>= 0 &&
7350 mddev
->delta_disks
>= 0 &&
7351 !test_bit(Faulty
, &rdev
->flags
) &&
7352 !test_bit(In_sync
, &rdev
->flags
) &&
7353 rdev
->recovery_offset
< mddev
->curr_resync
)
7354 rdev
->recovery_offset
= mddev
->curr_resync
;
7359 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
7361 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
7362 /* We completed so min/max setting can be forgotten if used. */
7363 if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
7364 mddev
->resync_min
= 0;
7365 mddev
->resync_max
= MaxSector
;
7366 } else if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
7367 mddev
->resync_min
= mddev
->curr_resync_completed
;
7368 mddev
->curr_resync
= 0;
7369 wake_up(&resync_wait
);
7370 set_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
7371 md_wakeup_thread(mddev
->thread
);
7376 * got a signal, exit.
7379 "md: md_do_sync() got signal ... exiting\n");
7380 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
7384 EXPORT_SYMBOL_GPL(md_do_sync
);
7386 static int remove_and_add_spares(struct mddev
*mddev
)
7388 struct md_rdev
*rdev
;
7392 mddev
->curr_resync_completed
= 0;
7394 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
7395 if (rdev
->raid_disk
>= 0 &&
7396 !test_bit(Blocked
, &rdev
->flags
) &&
7397 (test_bit(Faulty
, &rdev
->flags
) ||
7398 ! test_bit(In_sync
, &rdev
->flags
)) &&
7399 atomic_read(&rdev
->nr_pending
)==0) {
7400 if (mddev
->pers
->hot_remove_disk(
7401 mddev
, rdev
) == 0) {
7402 sysfs_unlink_rdev(mddev
, rdev
);
7403 rdev
->raid_disk
= -1;
7408 sysfs_notify(&mddev
->kobj
, NULL
,
7412 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
7413 if (rdev
->raid_disk
>= 0 &&
7414 !test_bit(In_sync
, &rdev
->flags
) &&
7415 !test_bit(Faulty
, &rdev
->flags
))
7417 if (rdev
->raid_disk
< 0
7418 && !test_bit(Faulty
, &rdev
->flags
)) {
7419 rdev
->recovery_offset
= 0;
7421 hot_add_disk(mddev
, rdev
) == 0) {
7422 if (sysfs_link_rdev(mddev
, rdev
))
7423 /* failure here is OK */;
7425 md_new_event(mddev
);
7426 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
7433 static void reap_sync_thread(struct mddev
*mddev
)
7435 struct md_rdev
*rdev
;
7437 /* resync has finished, collect result */
7438 md_unregister_thread(&mddev
->sync_thread
);
7439 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
) &&
7440 !test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
)) {
7442 /* activate any spares */
7443 if (mddev
->pers
->spare_active(mddev
))
7444 sysfs_notify(&mddev
->kobj
, NULL
,
7447 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
7448 mddev
->pers
->finish_reshape
)
7449 mddev
->pers
->finish_reshape(mddev
);
7451 /* If array is no-longer degraded, then any saved_raid_disk
7452 * information must be scrapped. Also if any device is now
7453 * In_sync we must scrape the saved_raid_disk for that device
7454 * do the superblock for an incrementally recovered device
7457 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
7458 if (!mddev
->degraded
||
7459 test_bit(In_sync
, &rdev
->flags
))
7460 rdev
->saved_raid_disk
= -1;
7462 md_update_sb(mddev
, 1);
7463 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
7464 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
7465 clear_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
7466 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
7467 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
7468 /* flag recovery needed just to double check */
7469 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7470 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
7471 md_new_event(mddev
);
7472 if (mddev
->event_work
.func
)
7473 queue_work(md_misc_wq
, &mddev
->event_work
);
7477 * This routine is regularly called by all per-raid-array threads to
7478 * deal with generic issues like resync and super-block update.
7479 * Raid personalities that don't have a thread (linear/raid0) do not
7480 * need this as they never do any recovery or update the superblock.
7482 * It does not do any resync itself, but rather "forks" off other threads
7483 * to do that as needed.
7484 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
7485 * "->recovery" and create a thread at ->sync_thread.
7486 * When the thread finishes it sets MD_RECOVERY_DONE
7487 * and wakeups up this thread which will reap the thread and finish up.
7488 * This thread also removes any faulty devices (with nr_pending == 0).
7490 * The overall approach is:
7491 * 1/ if the superblock needs updating, update it.
7492 * 2/ If a recovery thread is running, don't do anything else.
7493 * 3/ If recovery has finished, clean up, possibly marking spares active.
7494 * 4/ If there are any faulty devices, remove them.
7495 * 5/ If array is degraded, try to add spares devices
7496 * 6/ If array has spares or is not in-sync, start a resync thread.
7498 void md_check_recovery(struct mddev
*mddev
)
7500 if (mddev
->suspended
)
7504 bitmap_daemon_work(mddev
);
7506 if (signal_pending(current
)) {
7507 if (mddev
->pers
->sync_request
&& !mddev
->external
) {
7508 printk(KERN_INFO
"md: %s in immediate safe mode\n",
7510 mddev
->safemode
= 2;
7512 flush_signals(current
);
7515 if (mddev
->ro
&& !test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))
7518 (mddev
->flags
& ~ (1<<MD_CHANGE_PENDING
)) ||
7519 test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
) ||
7520 test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
) ||
7521 (mddev
->external
== 0 && mddev
->safemode
== 1) ||
7522 (mddev
->safemode
== 2 && ! atomic_read(&mddev
->writes_pending
)
7523 && !mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
)
7527 if (mddev_trylock(mddev
)) {
7531 /* Only thing we do on a ro array is remove
7534 struct md_rdev
*rdev
;
7535 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
7536 if (rdev
->raid_disk
>= 0 &&
7537 !test_bit(Blocked
, &rdev
->flags
) &&
7538 test_bit(Faulty
, &rdev
->flags
) &&
7539 atomic_read(&rdev
->nr_pending
)==0) {
7540 if (mddev
->pers
->hot_remove_disk(
7541 mddev
, rdev
) == 0) {
7542 sysfs_unlink_rdev(mddev
, rdev
);
7543 rdev
->raid_disk
= -1;
7546 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7550 if (!mddev
->external
) {
7552 spin_lock_irq(&mddev
->write_lock
);
7553 if (mddev
->safemode
&&
7554 !atomic_read(&mddev
->writes_pending
) &&
7556 mddev
->recovery_cp
== MaxSector
) {
7559 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
7561 if (mddev
->safemode
== 1)
7562 mddev
->safemode
= 0;
7563 spin_unlock_irq(&mddev
->write_lock
);
7565 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
7569 md_update_sb(mddev
, 0);
7571 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) &&
7572 !test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
)) {
7573 /* resync/recovery still happening */
7574 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7577 if (mddev
->sync_thread
) {
7578 reap_sync_thread(mddev
);
7581 /* Set RUNNING before clearing NEEDED to avoid
7582 * any transients in the value of "sync_action".
7584 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
7585 /* Clear some bits that don't mean anything, but
7588 clear_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
7589 clear_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
7591 if (!test_and_clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
) ||
7592 test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
))
7594 /* no recovery is running.
7595 * remove any failed drives, then
7596 * add spares if possible.
7597 * Spare are also removed and re-added, to allow
7598 * the personality to fail the re-add.
7601 if (mddev
->reshape_position
!= MaxSector
) {
7602 if (mddev
->pers
->check_reshape
== NULL
||
7603 mddev
->pers
->check_reshape(mddev
) != 0)
7604 /* Cannot proceed */
7606 set_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
7607 clear_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
7608 } else if ((spares
= remove_and_add_spares(mddev
))) {
7609 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
7610 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
7611 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
7612 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
7613 } else if (mddev
->recovery_cp
< MaxSector
) {
7614 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
7615 clear_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
7616 } else if (!test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
7617 /* nothing to be done ... */
7620 if (mddev
->pers
->sync_request
) {
7621 if (spares
&& mddev
->bitmap
&& ! mddev
->bitmap
->file
) {
7622 /* We are adding a device or devices to an array
7623 * which has the bitmap stored on all devices.
7624 * So make sure all bitmap pages get written
7626 bitmap_write_all(mddev
->bitmap
);
7628 mddev
->sync_thread
= md_register_thread(md_do_sync
,
7631 if (!mddev
->sync_thread
) {
7632 printk(KERN_ERR
"%s: could not start resync"
7635 /* leave the spares where they are, it shouldn't hurt */
7636 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
7637 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
7638 clear_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
7639 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
7640 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
7642 md_wakeup_thread(mddev
->sync_thread
);
7643 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
7644 md_new_event(mddev
);
7647 if (!mddev
->sync_thread
) {
7648 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
7649 if (test_and_clear_bit(MD_RECOVERY_RECOVER
,
7651 if (mddev
->sysfs_action
)
7652 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
7654 mddev_unlock(mddev
);
7658 void md_wait_for_blocked_rdev(struct md_rdev
*rdev
, struct mddev
*mddev
)
7660 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
7661 wait_event_timeout(rdev
->blocked_wait
,
7662 !test_bit(Blocked
, &rdev
->flags
) &&
7663 !test_bit(BlockedBadBlocks
, &rdev
->flags
),
7664 msecs_to_jiffies(5000));
7665 rdev_dec_pending(rdev
, mddev
);
7667 EXPORT_SYMBOL(md_wait_for_blocked_rdev
);
7670 /* Bad block management.
7671 * We can record which blocks on each device are 'bad' and so just
7672 * fail those blocks, or that stripe, rather than the whole device.
7673 * Entries in the bad-block table are 64bits wide. This comprises:
7674 * Length of bad-range, in sectors: 0-511 for lengths 1-512
7675 * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes)
7676 * A 'shift' can be set so that larger blocks are tracked and
7677 * consequently larger devices can be covered.
7678 * 'Acknowledged' flag - 1 bit. - the most significant bit.
7680 * Locking of the bad-block table uses a seqlock so md_is_badblock
7681 * might need to retry if it is very unlucky.
7682 * We will sometimes want to check for bad blocks in a bi_end_io function,
7683 * so we use the write_seqlock_irq variant.
7685 * When looking for a bad block we specify a range and want to
7686 * know if any block in the range is bad. So we binary-search
7687 * to the last range that starts at-or-before the given endpoint,
7688 * (or "before the sector after the target range")
7689 * then see if it ends after the given start.
7691 * 0 if there are no known bad blocks in the range
7692 * 1 if there are known bad block which are all acknowledged
7693 * -1 if there are bad blocks which have not yet been acknowledged in metadata.
7694 * plus the start/length of the first bad section we overlap.
7696 int md_is_badblock(struct badblocks
*bb
, sector_t s
, int sectors
,
7697 sector_t
*first_bad
, int *bad_sectors
)
7703 sector_t target
= s
+ sectors
;
7706 if (bb
->shift
> 0) {
7707 /* round the start down, and the end up */
7709 target
+= (1<<bb
->shift
) - 1;
7710 target
>>= bb
->shift
;
7711 sectors
= target
- s
;
7713 /* 'target' is now the first block after the bad range */
7716 seq
= read_seqbegin(&bb
->lock
);
7720 /* Binary search between lo and hi for 'target'
7721 * i.e. for the last range that starts before 'target'
7723 /* INVARIANT: ranges before 'lo' and at-or-after 'hi'
7724 * are known not to be the last range before target.
7725 * VARIANT: hi-lo is the number of possible
7726 * ranges, and decreases until it reaches 1
7728 while (hi
- lo
> 1) {
7729 int mid
= (lo
+ hi
) / 2;
7730 sector_t a
= BB_OFFSET(p
[mid
]);
7732 /* This could still be the one, earlier ranges
7736 /* This and later ranges are definitely out. */
7739 /* 'lo' might be the last that started before target, but 'hi' isn't */
7741 /* need to check all range that end after 's' to see if
7742 * any are unacknowledged.
7745 BB_OFFSET(p
[lo
]) + BB_LEN(p
[lo
]) > s
) {
7746 if (BB_OFFSET(p
[lo
]) < target
) {
7747 /* starts before the end, and finishes after
7748 * the start, so they must overlap
7750 if (rv
!= -1 && BB_ACK(p
[lo
]))
7754 *first_bad
= BB_OFFSET(p
[lo
]);
7755 *bad_sectors
= BB_LEN(p
[lo
]);
7761 if (read_seqretry(&bb
->lock
, seq
))
7766 EXPORT_SYMBOL_GPL(md_is_badblock
);
7769 * Add a range of bad blocks to the table.
7770 * This might extend the table, or might contract it
7771 * if two adjacent ranges can be merged.
7772 * We binary-search to find the 'insertion' point, then
7773 * decide how best to handle it.
7775 static int md_set_badblocks(struct badblocks
*bb
, sector_t s
, int sectors
,
7783 /* badblocks are disabled */
7787 /* round the start down, and the end up */
7788 sector_t next
= s
+ sectors
;
7790 next
+= (1<<bb
->shift
) - 1;
7795 write_seqlock_irq(&bb
->lock
);
7800 /* Find the last range that starts at-or-before 's' */
7801 while (hi
- lo
> 1) {
7802 int mid
= (lo
+ hi
) / 2;
7803 sector_t a
= BB_OFFSET(p
[mid
]);
7809 if (hi
> lo
&& BB_OFFSET(p
[lo
]) > s
)
7813 /* we found a range that might merge with the start
7816 sector_t a
= BB_OFFSET(p
[lo
]);
7817 sector_t e
= a
+ BB_LEN(p
[lo
]);
7818 int ack
= BB_ACK(p
[lo
]);
7820 /* Yes, we can merge with a previous range */
7821 if (s
== a
&& s
+ sectors
>= e
)
7822 /* new range covers old */
7825 ack
= ack
&& acknowledged
;
7827 if (e
< s
+ sectors
)
7829 if (e
- a
<= BB_MAX_LEN
) {
7830 p
[lo
] = BB_MAKE(a
, e
-a
, ack
);
7833 /* does not all fit in one range,
7834 * make p[lo] maximal
7836 if (BB_LEN(p
[lo
]) != BB_MAX_LEN
)
7837 p
[lo
] = BB_MAKE(a
, BB_MAX_LEN
, ack
);
7843 if (sectors
&& hi
< bb
->count
) {
7844 /* 'hi' points to the first range that starts after 's'.
7845 * Maybe we can merge with the start of that range */
7846 sector_t a
= BB_OFFSET(p
[hi
]);
7847 sector_t e
= a
+ BB_LEN(p
[hi
]);
7848 int ack
= BB_ACK(p
[hi
]);
7849 if (a
<= s
+ sectors
) {
7850 /* merging is possible */
7851 if (e
<= s
+ sectors
) {
7856 ack
= ack
&& acknowledged
;
7859 if (e
- a
<= BB_MAX_LEN
) {
7860 p
[hi
] = BB_MAKE(a
, e
-a
, ack
);
7863 p
[hi
] = BB_MAKE(a
, BB_MAX_LEN
, ack
);
7871 if (sectors
== 0 && hi
< bb
->count
) {
7872 /* we might be able to combine lo and hi */
7873 /* Note: 's' is at the end of 'lo' */
7874 sector_t a
= BB_OFFSET(p
[hi
]);
7875 int lolen
= BB_LEN(p
[lo
]);
7876 int hilen
= BB_LEN(p
[hi
]);
7877 int newlen
= lolen
+ hilen
- (s
- a
);
7878 if (s
>= a
&& newlen
< BB_MAX_LEN
) {
7879 /* yes, we can combine them */
7880 int ack
= BB_ACK(p
[lo
]) && BB_ACK(p
[hi
]);
7881 p
[lo
] = BB_MAKE(BB_OFFSET(p
[lo
]), newlen
, ack
);
7882 memmove(p
+ hi
, p
+ hi
+ 1,
7883 (bb
->count
- hi
- 1) * 8);
7888 /* didn't merge (it all).
7889 * Need to add a range just before 'hi' */
7890 if (bb
->count
>= MD_MAX_BADBLOCKS
) {
7891 /* No room for more */
7895 int this_sectors
= sectors
;
7896 memmove(p
+ hi
+ 1, p
+ hi
,
7897 (bb
->count
- hi
) * 8);
7900 if (this_sectors
> BB_MAX_LEN
)
7901 this_sectors
= BB_MAX_LEN
;
7902 p
[hi
] = BB_MAKE(s
, this_sectors
, acknowledged
);
7903 sectors
-= this_sectors
;
7910 bb
->unacked_exist
= 1;
7911 write_sequnlock_irq(&bb
->lock
);
7916 int rdev_set_badblocks(struct md_rdev
*rdev
, sector_t s
, int sectors
,
7919 int rv
= md_set_badblocks(&rdev
->badblocks
,
7920 s
+ rdev
->data_offset
, sectors
, acknowledged
);
7922 /* Make sure they get written out promptly */
7923 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
7924 set_bit(MD_CHANGE_CLEAN
, &rdev
->mddev
->flags
);
7925 md_wakeup_thread(rdev
->mddev
->thread
);
7929 EXPORT_SYMBOL_GPL(rdev_set_badblocks
);
7932 * Remove a range of bad blocks from the table.
7933 * This may involve extending the table if we spilt a region,
7934 * but it must not fail. So if the table becomes full, we just
7935 * drop the remove request.
7937 static int md_clear_badblocks(struct badblocks
*bb
, sector_t s
, int sectors
)
7941 sector_t target
= s
+ sectors
;
7944 if (bb
->shift
> 0) {
7945 /* When clearing we round the start up and the end down.
7946 * This should not matter as the shift should align with
7947 * the block size and no rounding should ever be needed.
7948 * However it is better the think a block is bad when it
7949 * isn't than to think a block is not bad when it is.
7951 s
+= (1<<bb
->shift
) - 1;
7953 target
>>= bb
->shift
;
7954 sectors
= target
- s
;
7957 write_seqlock_irq(&bb
->lock
);
7962 /* Find the last range that starts before 'target' */
7963 while (hi
- lo
> 1) {
7964 int mid
= (lo
+ hi
) / 2;
7965 sector_t a
= BB_OFFSET(p
[mid
]);
7972 /* p[lo] is the last range that could overlap the
7973 * current range. Earlier ranges could also overlap,
7974 * but only this one can overlap the end of the range.
7976 if (BB_OFFSET(p
[lo
]) + BB_LEN(p
[lo
]) > target
) {
7977 /* Partial overlap, leave the tail of this range */
7978 int ack
= BB_ACK(p
[lo
]);
7979 sector_t a
= BB_OFFSET(p
[lo
]);
7980 sector_t end
= a
+ BB_LEN(p
[lo
]);
7983 /* we need to split this range */
7984 if (bb
->count
>= MD_MAX_BADBLOCKS
) {
7988 memmove(p
+lo
+1, p
+lo
, (bb
->count
- lo
) * 8);
7990 p
[lo
] = BB_MAKE(a
, s
-a
, ack
);
7993 p
[lo
] = BB_MAKE(target
, end
- target
, ack
);
7994 /* there is no longer an overlap */
7999 BB_OFFSET(p
[lo
]) + BB_LEN(p
[lo
]) > s
) {
8000 /* This range does overlap */
8001 if (BB_OFFSET(p
[lo
]) < s
) {
8002 /* Keep the early parts of this range. */
8003 int ack
= BB_ACK(p
[lo
]);
8004 sector_t start
= BB_OFFSET(p
[lo
]);
8005 p
[lo
] = BB_MAKE(start
, s
- start
, ack
);
8006 /* now low doesn't overlap, so.. */
8011 /* 'lo' is strictly before, 'hi' is strictly after,
8012 * anything between needs to be discarded
8015 memmove(p
+lo
+1, p
+hi
, (bb
->count
- hi
) * 8);
8016 bb
->count
-= (hi
- lo
- 1);
8022 write_sequnlock_irq(&bb
->lock
);
8026 int rdev_clear_badblocks(struct md_rdev
*rdev
, sector_t s
, int sectors
)
8028 return md_clear_badblocks(&rdev
->badblocks
,
8029 s
+ rdev
->data_offset
,
8032 EXPORT_SYMBOL_GPL(rdev_clear_badblocks
);
8035 * Acknowledge all bad blocks in a list.
8036 * This only succeeds if ->changed is clear. It is used by
8037 * in-kernel metadata updates
8039 void md_ack_all_badblocks(struct badblocks
*bb
)
8041 if (bb
->page
== NULL
|| bb
->changed
)
8042 /* no point even trying */
8044 write_seqlock_irq(&bb
->lock
);
8046 if (bb
->changed
== 0) {
8049 for (i
= 0; i
< bb
->count
; i
++) {
8050 if (!BB_ACK(p
[i
])) {
8051 sector_t start
= BB_OFFSET(p
[i
]);
8052 int len
= BB_LEN(p
[i
]);
8053 p
[i
] = BB_MAKE(start
, len
, 1);
8056 bb
->unacked_exist
= 0;
8058 write_sequnlock_irq(&bb
->lock
);
8060 EXPORT_SYMBOL_GPL(md_ack_all_badblocks
);
8062 /* sysfs access to bad-blocks list.
8063 * We present two files.
8064 * 'bad-blocks' lists sector numbers and lengths of ranges that
8065 * are recorded as bad. The list is truncated to fit within
8066 * the one-page limit of sysfs.
8067 * Writing "sector length" to this file adds an acknowledged
8069 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
8070 * been acknowledged. Writing to this file adds bad blocks
8071 * without acknowledging them. This is largely for testing.
8075 badblocks_show(struct badblocks
*bb
, char *page
, int unack
)
8086 seq
= read_seqbegin(&bb
->lock
);
8091 while (len
< PAGE_SIZE
&& i
< bb
->count
) {
8092 sector_t s
= BB_OFFSET(p
[i
]);
8093 unsigned int length
= BB_LEN(p
[i
]);
8094 int ack
= BB_ACK(p
[i
]);
8100 len
+= snprintf(page
+len
, PAGE_SIZE
-len
, "%llu %u\n",
8101 (unsigned long long)s
<< bb
->shift
,
8102 length
<< bb
->shift
);
8104 if (unack
&& len
== 0)
8105 bb
->unacked_exist
= 0;
8107 if (read_seqretry(&bb
->lock
, seq
))
8116 badblocks_store(struct badblocks
*bb
, const char *page
, size_t len
, int unack
)
8118 unsigned long long sector
;
8122 /* Allow clearing via sysfs *only* for testing/debugging.
8123 * Normally only a successful write may clear a badblock
8126 if (page
[0] == '-') {
8130 #endif /* DO_DEBUG */
8132 switch (sscanf(page
, "%llu %d%c", §or
, &length
, &newline
)) {
8134 if (newline
!= '\n')
8146 md_clear_badblocks(bb
, sector
, length
);
8149 #endif /* DO_DEBUG */
8150 if (md_set_badblocks(bb
, sector
, length
, !unack
))
8156 static int md_notify_reboot(struct notifier_block
*this,
8157 unsigned long code
, void *x
)
8159 struct list_head
*tmp
;
8160 struct mddev
*mddev
;
8163 for_each_mddev(mddev
, tmp
) {
8164 if (mddev_trylock(mddev
)) {
8166 __md_stop_writes(mddev
);
8167 mddev
->safemode
= 2;
8168 mddev_unlock(mddev
);
8173 * certain more exotic SCSI devices are known to be
8174 * volatile wrt too early system reboots. While the
8175 * right place to handle this issue is the given
8176 * driver, we do want to have a safe RAID driver ...
8184 static struct notifier_block md_notifier
= {
8185 .notifier_call
= md_notify_reboot
,
8187 .priority
= INT_MAX
, /* before any real devices */
8190 static void md_geninit(void)
8192 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t
));
8194 proc_create("mdstat", S_IRUGO
, NULL
, &md_seq_fops
);
8197 static int __init
md_init(void)
8201 md_wq
= alloc_workqueue("md", WQ_MEM_RECLAIM
, 0);
8205 md_misc_wq
= alloc_workqueue("md_misc", 0, 0);
8209 if ((ret
= register_blkdev(MD_MAJOR
, "md")) < 0)
8212 if ((ret
= register_blkdev(0, "mdp")) < 0)
8216 blk_register_region(MKDEV(MD_MAJOR
, 0), 1UL<<MINORBITS
, THIS_MODULE
,
8217 md_probe
, NULL
, NULL
);
8218 blk_register_region(MKDEV(mdp_major
, 0), 1UL<<MINORBITS
, THIS_MODULE
,
8219 md_probe
, NULL
, NULL
);
8221 register_reboot_notifier(&md_notifier
);
8222 raid_table_header
= register_sysctl_table(raid_root_table
);
8228 unregister_blkdev(MD_MAJOR
, "md");
8230 destroy_workqueue(md_misc_wq
);
8232 destroy_workqueue(md_wq
);
8240 * Searches all registered partitions for autorun RAID arrays
8244 static LIST_HEAD(all_detected_devices
);
8245 struct detected_devices_node
{
8246 struct list_head list
;
8250 void md_autodetect_dev(dev_t dev
)
8252 struct detected_devices_node
*node_detected_dev
;
8254 node_detected_dev
= kzalloc(sizeof(*node_detected_dev
), GFP_KERNEL
);
8255 if (node_detected_dev
) {
8256 node_detected_dev
->dev
= dev
;
8257 list_add_tail(&node_detected_dev
->list
, &all_detected_devices
);
8259 printk(KERN_CRIT
"md: md_autodetect_dev: kzalloc failed"
8260 ", skipping dev(%d,%d)\n", MAJOR(dev
), MINOR(dev
));
8265 static void autostart_arrays(int part
)
8267 struct md_rdev
*rdev
;
8268 struct detected_devices_node
*node_detected_dev
;
8270 int i_scanned
, i_passed
;
8275 printk(KERN_INFO
"md: Autodetecting RAID arrays.\n");
8277 while (!list_empty(&all_detected_devices
) && i_scanned
< INT_MAX
) {
8279 node_detected_dev
= list_entry(all_detected_devices
.next
,
8280 struct detected_devices_node
, list
);
8281 list_del(&node_detected_dev
->list
);
8282 dev
= node_detected_dev
->dev
;
8283 kfree(node_detected_dev
);
8284 rdev
= md_import_device(dev
,0, 90);
8288 if (test_bit(Faulty
, &rdev
->flags
)) {
8292 set_bit(AutoDetected
, &rdev
->flags
);
8293 list_add(&rdev
->same_set
, &pending_raid_disks
);
8297 printk(KERN_INFO
"md: Scanned %d and added %d devices.\n",
8298 i_scanned
, i_passed
);
8300 autorun_devices(part
);
8303 #endif /* !MODULE */
8305 static __exit
void md_exit(void)
8307 struct mddev
*mddev
;
8308 struct list_head
*tmp
;
8310 blk_unregister_region(MKDEV(MD_MAJOR
,0), 1U << MINORBITS
);
8311 blk_unregister_region(MKDEV(mdp_major
,0), 1U << MINORBITS
);
8313 unregister_blkdev(MD_MAJOR
,"md");
8314 unregister_blkdev(mdp_major
, "mdp");
8315 unregister_reboot_notifier(&md_notifier
);
8316 unregister_sysctl_table(raid_table_header
);
8317 remove_proc_entry("mdstat", NULL
);
8318 for_each_mddev(mddev
, tmp
) {
8319 export_array(mddev
);
8320 mddev
->hold_active
= 0;
8322 destroy_workqueue(md_misc_wq
);
8323 destroy_workqueue(md_wq
);
8326 subsys_initcall(md_init
);
8327 module_exit(md_exit
)
8329 static int get_ro(char *buffer
, struct kernel_param
*kp
)
8331 return sprintf(buffer
, "%d", start_readonly
);
8333 static int set_ro(const char *val
, struct kernel_param
*kp
)
8336 int num
= simple_strtoul(val
, &e
, 10);
8337 if (*val
&& (*e
== '\0' || *e
== '\n')) {
8338 start_readonly
= num
;
8344 module_param_call(start_ro
, set_ro
, get_ro
, NULL
, S_IRUSR
|S_IWUSR
);
8345 module_param(start_dirty_degraded
, int, S_IRUGO
|S_IWUSR
);
8347 module_param_call(new_array
, add_named_array
, NULL
, NULL
, S_IWUSR
);
8349 EXPORT_SYMBOL(register_md_personality
);
8350 EXPORT_SYMBOL(unregister_md_personality
);
8351 EXPORT_SYMBOL(md_error
);
8352 EXPORT_SYMBOL(md_done_sync
);
8353 EXPORT_SYMBOL(md_write_start
);
8354 EXPORT_SYMBOL(md_write_end
);
8355 EXPORT_SYMBOL(md_register_thread
);
8356 EXPORT_SYMBOL(md_unregister_thread
);
8357 EXPORT_SYMBOL(md_wakeup_thread
);
8358 EXPORT_SYMBOL(md_check_recovery
);
8359 MODULE_LICENSE("GPL");
8360 MODULE_DESCRIPTION("MD RAID framework");
8362 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR
);