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>
39 #include <linux/mutex.h>
40 #include <linux/buffer_head.h> /* for invalidate_bdev */
41 #include <linux/poll.h>
42 #include <linux/ctype.h>
43 #include <linux/string.h>
44 #include <linux/hdreg.h>
45 #include <linux/proc_fs.h>
46 #include <linux/random.h>
47 #include <linux/module.h>
48 #include <linux/reboot.h>
49 #include <linux/file.h>
50 #include <linux/compat.h>
51 #include <linux/delay.h>
52 #include <linux/raid/md_p.h>
53 #include <linux/raid/md_u.h>
54 #include <linux/slab.h>
59 #define dprintk(x...) ((void)(DEBUG && printk(x)))
62 static void autostart_arrays(int part
);
65 static LIST_HEAD(pers_list
);
66 static DEFINE_SPINLOCK(pers_lock
);
68 static void md_print_devices(void);
70 static DECLARE_WAIT_QUEUE_HEAD(resync_wait
);
71 static struct workqueue_struct
*md_wq
;
72 static struct workqueue_struct
*md_misc_wq
;
74 #define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
77 * Default number of read corrections we'll attempt on an rdev
78 * before ejecting it from the array. We divide the read error
79 * count by 2 for every hour elapsed between read errors.
81 #define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
83 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
84 * is 1000 KB/sec, so the extra system load does not show up that much.
85 * Increase it if you want to have more _guaranteed_ speed. Note that
86 * the RAID driver will use the maximum available bandwidth if the IO
87 * subsystem is idle. There is also an 'absolute maximum' reconstruction
88 * speed limit - in case reconstruction slows down your system despite
91 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
92 * or /sys/block/mdX/md/sync_speed_{min,max}
95 static int sysctl_speed_limit_min
= 1000;
96 static int sysctl_speed_limit_max
= 200000;
97 static inline int speed_min(mddev_t
*mddev
)
99 return mddev
->sync_speed_min
?
100 mddev
->sync_speed_min
: sysctl_speed_limit_min
;
103 static inline int speed_max(mddev_t
*mddev
)
105 return mddev
->sync_speed_max
?
106 mddev
->sync_speed_max
: sysctl_speed_limit_max
;
109 static struct ctl_table_header
*raid_table_header
;
111 static ctl_table raid_table
[] = {
113 .procname
= "speed_limit_min",
114 .data
= &sysctl_speed_limit_min
,
115 .maxlen
= sizeof(int),
116 .mode
= S_IRUGO
|S_IWUSR
,
117 .proc_handler
= proc_dointvec
,
120 .procname
= "speed_limit_max",
121 .data
= &sysctl_speed_limit_max
,
122 .maxlen
= sizeof(int),
123 .mode
= S_IRUGO
|S_IWUSR
,
124 .proc_handler
= proc_dointvec
,
129 static ctl_table raid_dir_table
[] = {
133 .mode
= S_IRUGO
|S_IXUGO
,
139 static ctl_table raid_root_table
[] = {
144 .child
= raid_dir_table
,
149 static const struct block_device_operations md_fops
;
151 static int start_readonly
;
154 * like bio_clone, but with a local bio set
157 static void mddev_bio_destructor(struct bio
*bio
)
159 mddev_t
*mddev
, **mddevp
;
164 bio_free(bio
, mddev
->bio_set
);
167 struct bio
*bio_alloc_mddev(gfp_t gfp_mask
, int nr_iovecs
,
173 if (!mddev
|| !mddev
->bio_set
)
174 return bio_alloc(gfp_mask
, nr_iovecs
);
176 b
= bio_alloc_bioset(gfp_mask
, nr_iovecs
,
182 b
->bi_destructor
= mddev_bio_destructor
;
185 EXPORT_SYMBOL_GPL(bio_alloc_mddev
);
187 struct bio
*bio_clone_mddev(struct bio
*bio
, gfp_t gfp_mask
,
193 if (!mddev
|| !mddev
->bio_set
)
194 return bio_clone(bio
, gfp_mask
);
196 b
= bio_alloc_bioset(gfp_mask
, bio
->bi_max_vecs
,
202 b
->bi_destructor
= mddev_bio_destructor
;
204 if (bio_integrity(bio
)) {
207 ret
= bio_integrity_clone(b
, bio
, gfp_mask
, mddev
->bio_set
);
217 EXPORT_SYMBOL_GPL(bio_clone_mddev
);
219 void md_trim_bio(struct bio
*bio
, int offset
, int size
)
221 /* 'bio' is a cloned bio which we need to trim to match
222 * the given offset and size.
223 * This requires adjusting bi_sector, bi_size, and bi_io_vec
226 struct bio_vec
*bvec
;
230 if (offset
== 0 && size
== bio
->bi_size
)
233 bio
->bi_sector
+= offset
;
236 clear_bit(BIO_SEG_VALID
, &bio
->bi_flags
);
238 while (bio
->bi_idx
< bio
->bi_vcnt
&&
239 bio
->bi_io_vec
[bio
->bi_idx
].bv_len
<= offset
) {
240 /* remove this whole bio_vec */
241 offset
-= bio
->bi_io_vec
[bio
->bi_idx
].bv_len
;
244 if (bio
->bi_idx
< bio
->bi_vcnt
) {
245 bio
->bi_io_vec
[bio
->bi_idx
].bv_offset
+= offset
;
246 bio
->bi_io_vec
[bio
->bi_idx
].bv_len
-= offset
;
248 /* avoid any complications with bi_idx being non-zero*/
250 memmove(bio
->bi_io_vec
, bio
->bi_io_vec
+bio
->bi_idx
,
251 (bio
->bi_vcnt
- bio
->bi_idx
) * sizeof(struct bio_vec
));
252 bio
->bi_vcnt
-= bio
->bi_idx
;
255 /* Make sure vcnt and last bv are not too big */
256 bio_for_each_segment(bvec
, bio
, i
) {
257 if (sofar
+ bvec
->bv_len
> size
)
258 bvec
->bv_len
= size
- sofar
;
259 if (bvec
->bv_len
== 0) {
263 sofar
+= bvec
->bv_len
;
266 EXPORT_SYMBOL_GPL(md_trim_bio
);
269 * We have a system wide 'event count' that is incremented
270 * on any 'interesting' event, and readers of /proc/mdstat
271 * can use 'poll' or 'select' to find out when the event
275 * start array, stop array, error, add device, remove device,
276 * start build, activate spare
278 static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters
);
279 static atomic_t md_event_count
;
280 void md_new_event(mddev_t
*mddev
)
282 atomic_inc(&md_event_count
);
283 wake_up(&md_event_waiters
);
285 EXPORT_SYMBOL_GPL(md_new_event
);
287 /* Alternate version that can be called from interrupts
288 * when calling sysfs_notify isn't needed.
290 static void md_new_event_inintr(mddev_t
*mddev
)
292 atomic_inc(&md_event_count
);
293 wake_up(&md_event_waiters
);
297 * Enables to iterate over all existing md arrays
298 * all_mddevs_lock protects this list.
300 static LIST_HEAD(all_mddevs
);
301 static DEFINE_SPINLOCK(all_mddevs_lock
);
305 * iterates through all used mddevs in the system.
306 * We take care to grab the all_mddevs_lock whenever navigating
307 * the list, and to always hold a refcount when unlocked.
308 * Any code which breaks out of this loop while own
309 * a reference to the current mddev and must mddev_put it.
311 #define for_each_mddev(mddev,tmp) \
313 for (({ spin_lock(&all_mddevs_lock); \
314 tmp = all_mddevs.next; \
316 ({ if (tmp != &all_mddevs) \
317 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
318 spin_unlock(&all_mddevs_lock); \
319 if (mddev) mddev_put(mddev); \
320 mddev = list_entry(tmp, mddev_t, all_mddevs); \
321 tmp != &all_mddevs;}); \
322 ({ spin_lock(&all_mddevs_lock); \
327 /* Rather than calling directly into the personality make_request function,
328 * IO requests come here first so that we can check if the device is
329 * being suspended pending a reconfiguration.
330 * We hold a refcount over the call to ->make_request. By the time that
331 * call has finished, the bio has been linked into some internal structure
332 * and so is visible to ->quiesce(), so we don't need the refcount any more.
334 static int md_make_request(struct request_queue
*q
, struct bio
*bio
)
336 const int rw
= bio_data_dir(bio
);
337 mddev_t
*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 rv
= 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
);
383 /* mddev_suspend makes sure no new requests are submitted
384 * to the device, and that any requests that have been submitted
385 * are completely handled.
386 * Once ->stop is called and completes, the module will be completely
389 void mddev_suspend(mddev_t
*mddev
)
391 BUG_ON(mddev
->suspended
);
392 mddev
->suspended
= 1;
394 wait_event(mddev
->sb_wait
, atomic_read(&mddev
->active_io
) == 0);
395 mddev
->pers
->quiesce(mddev
, 1);
397 EXPORT_SYMBOL_GPL(mddev_suspend
);
399 void mddev_resume(mddev_t
*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(mddev_t
*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 mdk_rdev_t
*rdev
= bio
->bi_private
;
423 mddev_t
*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 mddev_t
*mddev
= container_of(ws
, mddev_t
, flush_work
);
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 mddev_t
*mddev
= container_of(ws
, mddev_t
, 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 if (mddev
->pers
->make_request(mddev
, bio
))
480 generic_make_request(bio
);
483 mddev
->flush_bio
= NULL
;
484 wake_up(&mddev
->sb_wait
);
487 void md_flush_request(mddev_t
*mddev
, struct bio
*bio
)
489 spin_lock_irq(&mddev
->write_lock
);
490 wait_event_lock_irq(mddev
->sb_wait
,
492 mddev
->write_lock
, /*nothing*/);
493 mddev
->flush_bio
= bio
;
494 spin_unlock_irq(&mddev
->write_lock
);
496 INIT_WORK(&mddev
->flush_work
, submit_flushes
);
497 queue_work(md_wq
, &mddev
->flush_work
);
499 EXPORT_SYMBOL(md_flush_request
);
501 /* Support for plugging.
502 * This mirrors the plugging support in request_queue, but does not
503 * require having a whole queue or request structures.
504 * We allocate an md_plug_cb for each md device and each thread it gets
505 * plugged on. This links tot the private plug_handle structure in the
506 * personality data where we keep a count of the number of outstanding
507 * plugs so other code can see if a plug is active.
510 struct blk_plug_cb cb
;
514 static void plugger_unplug(struct blk_plug_cb
*cb
)
516 struct md_plug_cb
*mdcb
= container_of(cb
, struct md_plug_cb
, cb
);
517 if (atomic_dec_and_test(&mdcb
->mddev
->plug_cnt
))
518 md_wakeup_thread(mdcb
->mddev
->thread
);
522 /* Check that an unplug wakeup will come shortly.
523 * If not, wakeup the md thread immediately
525 int mddev_check_plugged(mddev_t
*mddev
)
527 struct blk_plug
*plug
= current
->plug
;
528 struct md_plug_cb
*mdcb
;
533 list_for_each_entry(mdcb
, &plug
->cb_list
, cb
.list
) {
534 if (mdcb
->cb
.callback
== plugger_unplug
&&
535 mdcb
->mddev
== mddev
) {
536 /* Already on the list, move to top */
537 if (mdcb
!= list_first_entry(&plug
->cb_list
,
540 list_move(&mdcb
->cb
.list
, &plug
->cb_list
);
544 /* Not currently on the callback list */
545 mdcb
= kmalloc(sizeof(*mdcb
), GFP_ATOMIC
);
550 mdcb
->cb
.callback
= plugger_unplug
;
551 atomic_inc(&mddev
->plug_cnt
);
552 list_add(&mdcb
->cb
.list
, &plug
->cb_list
);
555 EXPORT_SYMBOL_GPL(mddev_check_plugged
);
557 static inline mddev_t
*mddev_get(mddev_t
*mddev
)
559 atomic_inc(&mddev
->active
);
563 static void mddev_delayed_delete(struct work_struct
*ws
);
565 static void mddev_put(mddev_t
*mddev
)
567 struct bio_set
*bs
= NULL
;
569 if (!atomic_dec_and_lock(&mddev
->active
, &all_mddevs_lock
))
571 if (!mddev
->raid_disks
&& list_empty(&mddev
->disks
) &&
572 mddev
->ctime
== 0 && !mddev
->hold_active
) {
573 /* Array is not configured at all, and not held active,
575 list_del(&mddev
->all_mddevs
);
577 mddev
->bio_set
= NULL
;
578 if (mddev
->gendisk
) {
579 /* We did a probe so need to clean up. Call
580 * queue_work inside the spinlock so that
581 * flush_workqueue() after mddev_find will
582 * succeed in waiting for the work to be done.
584 INIT_WORK(&mddev
->del_work
, mddev_delayed_delete
);
585 queue_work(md_misc_wq
, &mddev
->del_work
);
589 spin_unlock(&all_mddevs_lock
);
594 void mddev_init(mddev_t
*mddev
)
596 mutex_init(&mddev
->open_mutex
);
597 mutex_init(&mddev
->reconfig_mutex
);
598 mutex_init(&mddev
->bitmap_info
.mutex
);
599 INIT_LIST_HEAD(&mddev
->disks
);
600 INIT_LIST_HEAD(&mddev
->all_mddevs
);
601 init_timer(&mddev
->safemode_timer
);
602 atomic_set(&mddev
->active
, 1);
603 atomic_set(&mddev
->openers
, 0);
604 atomic_set(&mddev
->active_io
, 0);
605 atomic_set(&mddev
->plug_cnt
, 0);
606 spin_lock_init(&mddev
->write_lock
);
607 atomic_set(&mddev
->flush_pending
, 0);
608 init_waitqueue_head(&mddev
->sb_wait
);
609 init_waitqueue_head(&mddev
->recovery_wait
);
610 mddev
->reshape_position
= MaxSector
;
611 mddev
->resync_min
= 0;
612 mddev
->resync_max
= MaxSector
;
613 mddev
->level
= LEVEL_NONE
;
615 EXPORT_SYMBOL_GPL(mddev_init
);
617 static mddev_t
* mddev_find(dev_t unit
)
619 mddev_t
*mddev
, *new = NULL
;
621 if (unit
&& MAJOR(unit
) != MD_MAJOR
)
622 unit
&= ~((1<<MdpMinorShift
)-1);
625 spin_lock(&all_mddevs_lock
);
628 list_for_each_entry(mddev
, &all_mddevs
, all_mddevs
)
629 if (mddev
->unit
== unit
) {
631 spin_unlock(&all_mddevs_lock
);
637 list_add(&new->all_mddevs
, &all_mddevs
);
638 spin_unlock(&all_mddevs_lock
);
639 new->hold_active
= UNTIL_IOCTL
;
643 /* find an unused unit number */
644 static int next_minor
= 512;
645 int start
= next_minor
;
649 dev
= MKDEV(MD_MAJOR
, next_minor
);
651 if (next_minor
> MINORMASK
)
653 if (next_minor
== start
) {
654 /* Oh dear, all in use. */
655 spin_unlock(&all_mddevs_lock
);
661 list_for_each_entry(mddev
, &all_mddevs
, all_mddevs
)
662 if (mddev
->unit
== dev
) {
668 new->md_minor
= MINOR(dev
);
669 new->hold_active
= UNTIL_STOP
;
670 list_add(&new->all_mddevs
, &all_mddevs
);
671 spin_unlock(&all_mddevs_lock
);
674 spin_unlock(&all_mddevs_lock
);
676 new = kzalloc(sizeof(*new), GFP_KERNEL
);
681 if (MAJOR(unit
) == MD_MAJOR
)
682 new->md_minor
= MINOR(unit
);
684 new->md_minor
= MINOR(unit
) >> MdpMinorShift
;
691 static inline int mddev_lock(mddev_t
* mddev
)
693 return mutex_lock_interruptible(&mddev
->reconfig_mutex
);
696 static inline int mddev_is_locked(mddev_t
*mddev
)
698 return mutex_is_locked(&mddev
->reconfig_mutex
);
701 static inline int mddev_trylock(mddev_t
* mddev
)
703 return mutex_trylock(&mddev
->reconfig_mutex
);
706 static struct attribute_group md_redundancy_group
;
708 static void mddev_unlock(mddev_t
* mddev
)
710 if (mddev
->to_remove
) {
711 /* These cannot be removed under reconfig_mutex as
712 * an access to the files will try to take reconfig_mutex
713 * while holding the file unremovable, which leads to
715 * So hold set sysfs_active while the remove in happeing,
716 * and anything else which might set ->to_remove or my
717 * otherwise change the sysfs namespace will fail with
718 * -EBUSY if sysfs_active is still set.
719 * We set sysfs_active under reconfig_mutex and elsewhere
720 * test it under the same mutex to ensure its correct value
723 struct attribute_group
*to_remove
= mddev
->to_remove
;
724 mddev
->to_remove
= NULL
;
725 mddev
->sysfs_active
= 1;
726 mutex_unlock(&mddev
->reconfig_mutex
);
728 if (mddev
->kobj
.sd
) {
729 if (to_remove
!= &md_redundancy_group
)
730 sysfs_remove_group(&mddev
->kobj
, to_remove
);
731 if (mddev
->pers
== NULL
||
732 mddev
->pers
->sync_request
== NULL
) {
733 sysfs_remove_group(&mddev
->kobj
, &md_redundancy_group
);
734 if (mddev
->sysfs_action
)
735 sysfs_put(mddev
->sysfs_action
);
736 mddev
->sysfs_action
= NULL
;
739 mddev
->sysfs_active
= 0;
741 mutex_unlock(&mddev
->reconfig_mutex
);
743 md_wakeup_thread(mddev
->thread
);
746 static mdk_rdev_t
* find_rdev_nr(mddev_t
*mddev
, int nr
)
750 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
751 if (rdev
->desc_nr
== nr
)
757 static mdk_rdev_t
* find_rdev(mddev_t
* mddev
, dev_t dev
)
761 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
762 if (rdev
->bdev
->bd_dev
== dev
)
768 static struct mdk_personality
*find_pers(int level
, char *clevel
)
770 struct mdk_personality
*pers
;
771 list_for_each_entry(pers
, &pers_list
, list
) {
772 if (level
!= LEVEL_NONE
&& pers
->level
== level
)
774 if (strcmp(pers
->name
, clevel
)==0)
780 /* return the offset of the super block in 512byte sectors */
781 static inline sector_t
calc_dev_sboffset(mdk_rdev_t
*rdev
)
783 sector_t num_sectors
= i_size_read(rdev
->bdev
->bd_inode
) / 512;
784 return MD_NEW_SIZE_SECTORS(num_sectors
);
787 static int alloc_disk_sb(mdk_rdev_t
* rdev
)
792 rdev
->sb_page
= alloc_page(GFP_KERNEL
);
793 if (!rdev
->sb_page
) {
794 printk(KERN_ALERT
"md: out of memory.\n");
801 static void free_disk_sb(mdk_rdev_t
* rdev
)
804 put_page(rdev
->sb_page
);
806 rdev
->sb_page
= NULL
;
811 put_page(rdev
->bb_page
);
812 rdev
->bb_page
= NULL
;
817 static void super_written(struct bio
*bio
, int error
)
819 mdk_rdev_t
*rdev
= bio
->bi_private
;
820 mddev_t
*mddev
= rdev
->mddev
;
822 if (error
|| !test_bit(BIO_UPTODATE
, &bio
->bi_flags
)) {
823 printk("md: super_written gets error=%d, uptodate=%d\n",
824 error
, test_bit(BIO_UPTODATE
, &bio
->bi_flags
));
825 WARN_ON(test_bit(BIO_UPTODATE
, &bio
->bi_flags
));
826 md_error(mddev
, rdev
);
829 if (atomic_dec_and_test(&mddev
->pending_writes
))
830 wake_up(&mddev
->sb_wait
);
834 void md_super_write(mddev_t
*mddev
, mdk_rdev_t
*rdev
,
835 sector_t sector
, int size
, struct page
*page
)
837 /* write first size bytes of page to sector of rdev
838 * Increment mddev->pending_writes before returning
839 * and decrement it on completion, waking up sb_wait
840 * if zero is reached.
841 * If an error occurred, call md_error
843 struct bio
*bio
= bio_alloc_mddev(GFP_NOIO
, 1, mddev
);
845 bio
->bi_bdev
= rdev
->meta_bdev
? rdev
->meta_bdev
: rdev
->bdev
;
846 bio
->bi_sector
= sector
;
847 bio_add_page(bio
, page
, size
, 0);
848 bio
->bi_private
= rdev
;
849 bio
->bi_end_io
= super_written
;
851 atomic_inc(&mddev
->pending_writes
);
852 submit_bio(REQ_WRITE
| REQ_SYNC
| REQ_FLUSH
| REQ_FUA
, bio
);
855 void md_super_wait(mddev_t
*mddev
)
857 /* wait for all superblock writes that were scheduled to complete */
860 prepare_to_wait(&mddev
->sb_wait
, &wq
, TASK_UNINTERRUPTIBLE
);
861 if (atomic_read(&mddev
->pending_writes
)==0)
865 finish_wait(&mddev
->sb_wait
, &wq
);
868 static void bi_complete(struct bio
*bio
, int error
)
870 complete((struct completion
*)bio
->bi_private
);
873 int sync_page_io(mdk_rdev_t
*rdev
, sector_t sector
, int size
,
874 struct page
*page
, int rw
, bool metadata_op
)
876 struct bio
*bio
= bio_alloc_mddev(GFP_NOIO
, 1, rdev
->mddev
);
877 struct completion event
;
882 bio
->bi_bdev
= (metadata_op
&& rdev
->meta_bdev
) ?
883 rdev
->meta_bdev
: rdev
->bdev
;
885 bio
->bi_sector
= sector
+ rdev
->sb_start
;
887 bio
->bi_sector
= sector
+ rdev
->data_offset
;
888 bio_add_page(bio
, page
, size
, 0);
889 init_completion(&event
);
890 bio
->bi_private
= &event
;
891 bio
->bi_end_io
= bi_complete
;
893 wait_for_completion(&event
);
895 ret
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
899 EXPORT_SYMBOL_GPL(sync_page_io
);
901 static int read_disk_sb(mdk_rdev_t
* rdev
, int size
)
903 char b
[BDEVNAME_SIZE
];
904 if (!rdev
->sb_page
) {
912 if (!sync_page_io(rdev
, 0, size
, rdev
->sb_page
, READ
, true))
918 printk(KERN_WARNING
"md: disabled device %s, could not read superblock.\n",
919 bdevname(rdev
->bdev
,b
));
923 static int uuid_equal(mdp_super_t
*sb1
, mdp_super_t
*sb2
)
925 return sb1
->set_uuid0
== sb2
->set_uuid0
&&
926 sb1
->set_uuid1
== sb2
->set_uuid1
&&
927 sb1
->set_uuid2
== sb2
->set_uuid2
&&
928 sb1
->set_uuid3
== sb2
->set_uuid3
;
931 static int sb_equal(mdp_super_t
*sb1
, mdp_super_t
*sb2
)
934 mdp_super_t
*tmp1
, *tmp2
;
936 tmp1
= kmalloc(sizeof(*tmp1
),GFP_KERNEL
);
937 tmp2
= kmalloc(sizeof(*tmp2
),GFP_KERNEL
);
939 if (!tmp1
|| !tmp2
) {
941 printk(KERN_INFO
"md.c sb_equal(): failed to allocate memory!\n");
949 * nr_disks is not constant
954 ret
= (memcmp(tmp1
, tmp2
, MD_SB_GENERIC_CONSTANT_WORDS
* 4) == 0);
962 static u32
md_csum_fold(u32 csum
)
964 csum
= (csum
& 0xffff) + (csum
>> 16);
965 return (csum
& 0xffff) + (csum
>> 16);
968 static unsigned int calc_sb_csum(mdp_super_t
* sb
)
971 u32
*sb32
= (u32
*)sb
;
973 unsigned int disk_csum
, csum
;
975 disk_csum
= sb
->sb_csum
;
978 for (i
= 0; i
< MD_SB_BYTES
/4 ; i
++)
980 csum
= (newcsum
& 0xffffffff) + (newcsum
>>32);
984 /* This used to use csum_partial, which was wrong for several
985 * reasons including that different results are returned on
986 * different architectures. It isn't critical that we get exactly
987 * the same return value as before (we always csum_fold before
988 * testing, and that removes any differences). However as we
989 * know that csum_partial always returned a 16bit value on
990 * alphas, do a fold to maximise conformity to previous behaviour.
992 sb
->sb_csum
= md_csum_fold(disk_csum
);
994 sb
->sb_csum
= disk_csum
;
1001 * Handle superblock details.
1002 * We want to be able to handle multiple superblock formats
1003 * so we have a common interface to them all, and an array of
1004 * different handlers.
1005 * We rely on user-space to write the initial superblock, and support
1006 * reading and updating of superblocks.
1007 * Interface methods are:
1008 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
1009 * loads and validates a superblock on dev.
1010 * if refdev != NULL, compare superblocks on both devices
1012 * 0 - dev has a superblock that is compatible with refdev
1013 * 1 - dev has a superblock that is compatible and newer than refdev
1014 * so dev should be used as the refdev in future
1015 * -EINVAL superblock incompatible or invalid
1016 * -othererror e.g. -EIO
1018 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
1019 * Verify that dev is acceptable into mddev.
1020 * The first time, mddev->raid_disks will be 0, and data from
1021 * dev should be merged in. Subsequent calls check that dev
1022 * is new enough. Return 0 or -EINVAL
1024 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
1025 * Update the superblock for rdev with data in mddev
1026 * This does not write to disc.
1032 struct module
*owner
;
1033 int (*load_super
)(mdk_rdev_t
*rdev
, mdk_rdev_t
*refdev
,
1035 int (*validate_super
)(mddev_t
*mddev
, mdk_rdev_t
*rdev
);
1036 void (*sync_super
)(mddev_t
*mddev
, mdk_rdev_t
*rdev
);
1037 unsigned long long (*rdev_size_change
)(mdk_rdev_t
*rdev
,
1038 sector_t num_sectors
);
1042 * Check that the given mddev has no bitmap.
1044 * This function is called from the run method of all personalities that do not
1045 * support bitmaps. It prints an error message and returns non-zero if mddev
1046 * has a bitmap. Otherwise, it returns 0.
1049 int md_check_no_bitmap(mddev_t
*mddev
)
1051 if (!mddev
->bitmap_info
.file
&& !mddev
->bitmap_info
.offset
)
1053 printk(KERN_ERR
"%s: bitmaps are not supported for %s\n",
1054 mdname(mddev
), mddev
->pers
->name
);
1057 EXPORT_SYMBOL(md_check_no_bitmap
);
1060 * load_super for 0.90.0
1062 static int super_90_load(mdk_rdev_t
*rdev
, mdk_rdev_t
*refdev
, int minor_version
)
1064 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
1069 * Calculate the position of the superblock (512byte sectors),
1070 * it's at the end of the disk.
1072 * It also happens to be a multiple of 4Kb.
1074 rdev
->sb_start
= calc_dev_sboffset(rdev
);
1076 ret
= read_disk_sb(rdev
, MD_SB_BYTES
);
1077 if (ret
) return ret
;
1081 bdevname(rdev
->bdev
, b
);
1082 sb
= page_address(rdev
->sb_page
);
1084 if (sb
->md_magic
!= MD_SB_MAGIC
) {
1085 printk(KERN_ERR
"md: invalid raid superblock magic on %s\n",
1090 if (sb
->major_version
!= 0 ||
1091 sb
->minor_version
< 90 ||
1092 sb
->minor_version
> 91) {
1093 printk(KERN_WARNING
"Bad version number %d.%d on %s\n",
1094 sb
->major_version
, sb
->minor_version
,
1099 if (sb
->raid_disks
<= 0)
1102 if (md_csum_fold(calc_sb_csum(sb
)) != md_csum_fold(sb
->sb_csum
)) {
1103 printk(KERN_WARNING
"md: invalid superblock checksum on %s\n",
1108 rdev
->preferred_minor
= sb
->md_minor
;
1109 rdev
->data_offset
= 0;
1110 rdev
->sb_size
= MD_SB_BYTES
;
1111 rdev
->badblocks
.shift
= -1;
1113 if (sb
->level
== LEVEL_MULTIPATH
)
1116 rdev
->desc_nr
= sb
->this_disk
.number
;
1122 mdp_super_t
*refsb
= page_address(refdev
->sb_page
);
1123 if (!uuid_equal(refsb
, sb
)) {
1124 printk(KERN_WARNING
"md: %s has different UUID to %s\n",
1125 b
, bdevname(refdev
->bdev
,b2
));
1128 if (!sb_equal(refsb
, sb
)) {
1129 printk(KERN_WARNING
"md: %s has same UUID"
1130 " but different superblock to %s\n",
1131 b
, bdevname(refdev
->bdev
, b2
));
1135 ev2
= md_event(refsb
);
1141 rdev
->sectors
= rdev
->sb_start
;
1143 if (rdev
->sectors
< sb
->size
* 2 && sb
->level
> 1)
1144 /* "this cannot possibly happen" ... */
1152 * validate_super for 0.90.0
1154 static int super_90_validate(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1157 mdp_super_t
*sb
= page_address(rdev
->sb_page
);
1158 __u64 ev1
= md_event(sb
);
1160 rdev
->raid_disk
= -1;
1161 clear_bit(Faulty
, &rdev
->flags
);
1162 clear_bit(In_sync
, &rdev
->flags
);
1163 clear_bit(WriteMostly
, &rdev
->flags
);
1165 if (mddev
->raid_disks
== 0) {
1166 mddev
->major_version
= 0;
1167 mddev
->minor_version
= sb
->minor_version
;
1168 mddev
->patch_version
= sb
->patch_version
;
1169 mddev
->external
= 0;
1170 mddev
->chunk_sectors
= sb
->chunk_size
>> 9;
1171 mddev
->ctime
= sb
->ctime
;
1172 mddev
->utime
= sb
->utime
;
1173 mddev
->level
= sb
->level
;
1174 mddev
->clevel
[0] = 0;
1175 mddev
->layout
= sb
->layout
;
1176 mddev
->raid_disks
= sb
->raid_disks
;
1177 mddev
->dev_sectors
= sb
->size
* 2;
1178 mddev
->events
= ev1
;
1179 mddev
->bitmap_info
.offset
= 0;
1180 mddev
->bitmap_info
.default_offset
= MD_SB_BYTES
>> 9;
1182 if (mddev
->minor_version
>= 91) {
1183 mddev
->reshape_position
= sb
->reshape_position
;
1184 mddev
->delta_disks
= sb
->delta_disks
;
1185 mddev
->new_level
= sb
->new_level
;
1186 mddev
->new_layout
= sb
->new_layout
;
1187 mddev
->new_chunk_sectors
= sb
->new_chunk
>> 9;
1189 mddev
->reshape_position
= MaxSector
;
1190 mddev
->delta_disks
= 0;
1191 mddev
->new_level
= mddev
->level
;
1192 mddev
->new_layout
= mddev
->layout
;
1193 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
1196 if (sb
->state
& (1<<MD_SB_CLEAN
))
1197 mddev
->recovery_cp
= MaxSector
;
1199 if (sb
->events_hi
== sb
->cp_events_hi
&&
1200 sb
->events_lo
== sb
->cp_events_lo
) {
1201 mddev
->recovery_cp
= sb
->recovery_cp
;
1203 mddev
->recovery_cp
= 0;
1206 memcpy(mddev
->uuid
+0, &sb
->set_uuid0
, 4);
1207 memcpy(mddev
->uuid
+4, &sb
->set_uuid1
, 4);
1208 memcpy(mddev
->uuid
+8, &sb
->set_uuid2
, 4);
1209 memcpy(mddev
->uuid
+12,&sb
->set_uuid3
, 4);
1211 mddev
->max_disks
= MD_SB_DISKS
;
1213 if (sb
->state
& (1<<MD_SB_BITMAP_PRESENT
) &&
1214 mddev
->bitmap_info
.file
== NULL
)
1215 mddev
->bitmap_info
.offset
=
1216 mddev
->bitmap_info
.default_offset
;
1218 } else if (mddev
->pers
== NULL
) {
1219 /* Insist on good event counter while assembling, except
1220 * for spares (which don't need an event count) */
1222 if (sb
->disks
[rdev
->desc_nr
].state
& (
1223 (1<<MD_DISK_SYNC
) | (1 << MD_DISK_ACTIVE
)))
1224 if (ev1
< mddev
->events
)
1226 } else if (mddev
->bitmap
) {
1227 /* if adding to array with a bitmap, then we can accept an
1228 * older device ... but not too old.
1230 if (ev1
< mddev
->bitmap
->events_cleared
)
1233 if (ev1
< mddev
->events
)
1234 /* just a hot-add of a new device, leave raid_disk at -1 */
1238 if (mddev
->level
!= LEVEL_MULTIPATH
) {
1239 desc
= sb
->disks
+ rdev
->desc_nr
;
1241 if (desc
->state
& (1<<MD_DISK_FAULTY
))
1242 set_bit(Faulty
, &rdev
->flags
);
1243 else if (desc
->state
& (1<<MD_DISK_SYNC
) /* &&
1244 desc->raid_disk < mddev->raid_disks */) {
1245 set_bit(In_sync
, &rdev
->flags
);
1246 rdev
->raid_disk
= desc
->raid_disk
;
1247 } else if (desc
->state
& (1<<MD_DISK_ACTIVE
)) {
1248 /* active but not in sync implies recovery up to
1249 * reshape position. We don't know exactly where
1250 * that is, so set to zero for now */
1251 if (mddev
->minor_version
>= 91) {
1252 rdev
->recovery_offset
= 0;
1253 rdev
->raid_disk
= desc
->raid_disk
;
1256 if (desc
->state
& (1<<MD_DISK_WRITEMOSTLY
))
1257 set_bit(WriteMostly
, &rdev
->flags
);
1258 } else /* MULTIPATH are always insync */
1259 set_bit(In_sync
, &rdev
->flags
);
1264 * sync_super for 0.90.0
1266 static void super_90_sync(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1270 int next_spare
= mddev
->raid_disks
;
1273 /* make rdev->sb match mddev data..
1276 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1277 * 3/ any empty disks < next_spare become removed
1279 * disks[0] gets initialised to REMOVED because
1280 * we cannot be sure from other fields if it has
1281 * been initialised or not.
1284 int active
=0, working
=0,failed
=0,spare
=0,nr_disks
=0;
1286 rdev
->sb_size
= MD_SB_BYTES
;
1288 sb
= page_address(rdev
->sb_page
);
1290 memset(sb
, 0, sizeof(*sb
));
1292 sb
->md_magic
= MD_SB_MAGIC
;
1293 sb
->major_version
= mddev
->major_version
;
1294 sb
->patch_version
= mddev
->patch_version
;
1295 sb
->gvalid_words
= 0; /* ignored */
1296 memcpy(&sb
->set_uuid0
, mddev
->uuid
+0, 4);
1297 memcpy(&sb
->set_uuid1
, mddev
->uuid
+4, 4);
1298 memcpy(&sb
->set_uuid2
, mddev
->uuid
+8, 4);
1299 memcpy(&sb
->set_uuid3
, mddev
->uuid
+12,4);
1301 sb
->ctime
= mddev
->ctime
;
1302 sb
->level
= mddev
->level
;
1303 sb
->size
= mddev
->dev_sectors
/ 2;
1304 sb
->raid_disks
= mddev
->raid_disks
;
1305 sb
->md_minor
= mddev
->md_minor
;
1306 sb
->not_persistent
= 0;
1307 sb
->utime
= mddev
->utime
;
1309 sb
->events_hi
= (mddev
->events
>>32);
1310 sb
->events_lo
= (u32
)mddev
->events
;
1312 if (mddev
->reshape_position
== MaxSector
)
1313 sb
->minor_version
= 90;
1315 sb
->minor_version
= 91;
1316 sb
->reshape_position
= mddev
->reshape_position
;
1317 sb
->new_level
= mddev
->new_level
;
1318 sb
->delta_disks
= mddev
->delta_disks
;
1319 sb
->new_layout
= mddev
->new_layout
;
1320 sb
->new_chunk
= mddev
->new_chunk_sectors
<< 9;
1322 mddev
->minor_version
= sb
->minor_version
;
1325 sb
->recovery_cp
= mddev
->recovery_cp
;
1326 sb
->cp_events_hi
= (mddev
->events
>>32);
1327 sb
->cp_events_lo
= (u32
)mddev
->events
;
1328 if (mddev
->recovery_cp
== MaxSector
)
1329 sb
->state
= (1<< MD_SB_CLEAN
);
1331 sb
->recovery_cp
= 0;
1333 sb
->layout
= mddev
->layout
;
1334 sb
->chunk_size
= mddev
->chunk_sectors
<< 9;
1336 if (mddev
->bitmap
&& mddev
->bitmap_info
.file
== NULL
)
1337 sb
->state
|= (1<<MD_SB_BITMAP_PRESENT
);
1339 sb
->disks
[0].state
= (1<<MD_DISK_REMOVED
);
1340 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
) {
1343 int is_active
= test_bit(In_sync
, &rdev2
->flags
);
1345 if (rdev2
->raid_disk
>= 0 &&
1346 sb
->minor_version
>= 91)
1347 /* we have nowhere to store the recovery_offset,
1348 * but if it is not below the reshape_position,
1349 * we can piggy-back on that.
1352 if (rdev2
->raid_disk
< 0 ||
1353 test_bit(Faulty
, &rdev2
->flags
))
1356 desc_nr
= rdev2
->raid_disk
;
1358 desc_nr
= next_spare
++;
1359 rdev2
->desc_nr
= desc_nr
;
1360 d
= &sb
->disks
[rdev2
->desc_nr
];
1362 d
->number
= rdev2
->desc_nr
;
1363 d
->major
= MAJOR(rdev2
->bdev
->bd_dev
);
1364 d
->minor
= MINOR(rdev2
->bdev
->bd_dev
);
1366 d
->raid_disk
= rdev2
->raid_disk
;
1368 d
->raid_disk
= rdev2
->desc_nr
; /* compatibility */
1369 if (test_bit(Faulty
, &rdev2
->flags
))
1370 d
->state
= (1<<MD_DISK_FAULTY
);
1371 else if (is_active
) {
1372 d
->state
= (1<<MD_DISK_ACTIVE
);
1373 if (test_bit(In_sync
, &rdev2
->flags
))
1374 d
->state
|= (1<<MD_DISK_SYNC
);
1382 if (test_bit(WriteMostly
, &rdev2
->flags
))
1383 d
->state
|= (1<<MD_DISK_WRITEMOSTLY
);
1385 /* now set the "removed" and "faulty" bits on any missing devices */
1386 for (i
=0 ; i
< mddev
->raid_disks
; i
++) {
1387 mdp_disk_t
*d
= &sb
->disks
[i
];
1388 if (d
->state
== 0 && d
->number
== 0) {
1391 d
->state
= (1<<MD_DISK_REMOVED
);
1392 d
->state
|= (1<<MD_DISK_FAULTY
);
1396 sb
->nr_disks
= nr_disks
;
1397 sb
->active_disks
= active
;
1398 sb
->working_disks
= working
;
1399 sb
->failed_disks
= failed
;
1400 sb
->spare_disks
= spare
;
1402 sb
->this_disk
= sb
->disks
[rdev
->desc_nr
];
1403 sb
->sb_csum
= calc_sb_csum(sb
);
1407 * rdev_size_change for 0.90.0
1409 static unsigned long long
1410 super_90_rdev_size_change(mdk_rdev_t
*rdev
, sector_t num_sectors
)
1412 if (num_sectors
&& num_sectors
< rdev
->mddev
->dev_sectors
)
1413 return 0; /* component must fit device */
1414 if (rdev
->mddev
->bitmap_info
.offset
)
1415 return 0; /* can't move bitmap */
1416 rdev
->sb_start
= calc_dev_sboffset(rdev
);
1417 if (!num_sectors
|| num_sectors
> rdev
->sb_start
)
1418 num_sectors
= rdev
->sb_start
;
1419 md_super_write(rdev
->mddev
, rdev
, rdev
->sb_start
, rdev
->sb_size
,
1421 md_super_wait(rdev
->mddev
);
1427 * version 1 superblock
1430 static __le32
calc_sb_1_csum(struct mdp_superblock_1
* sb
)
1434 unsigned long long newcsum
;
1435 int size
= 256 + le32_to_cpu(sb
->max_dev
)*2;
1436 __le32
*isuper
= (__le32
*)sb
;
1439 disk_csum
= sb
->sb_csum
;
1442 for (i
=0; size
>=4; size
-= 4 )
1443 newcsum
+= le32_to_cpu(*isuper
++);
1446 newcsum
+= le16_to_cpu(*(__le16
*) isuper
);
1448 csum
= (newcsum
& 0xffffffff) + (newcsum
>> 32);
1449 sb
->sb_csum
= disk_csum
;
1450 return cpu_to_le32(csum
);
1453 static int md_set_badblocks(struct badblocks
*bb
, sector_t s
, int sectors
,
1455 static int super_1_load(mdk_rdev_t
*rdev
, mdk_rdev_t
*refdev
, int minor_version
)
1457 struct mdp_superblock_1
*sb
;
1460 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
1464 * Calculate the position of the superblock in 512byte sectors.
1465 * It is always aligned to a 4K boundary and
1466 * depeding on minor_version, it can be:
1467 * 0: At least 8K, but less than 12K, from end of device
1468 * 1: At start of device
1469 * 2: 4K from start of device.
1471 switch(minor_version
) {
1473 sb_start
= i_size_read(rdev
->bdev
->bd_inode
) >> 9;
1475 sb_start
&= ~(sector_t
)(4*2-1);
1486 rdev
->sb_start
= sb_start
;
1488 /* superblock is rarely larger than 1K, but it can be larger,
1489 * and it is safe to read 4k, so we do that
1491 ret
= read_disk_sb(rdev
, 4096);
1492 if (ret
) return ret
;
1495 sb
= page_address(rdev
->sb_page
);
1497 if (sb
->magic
!= cpu_to_le32(MD_SB_MAGIC
) ||
1498 sb
->major_version
!= cpu_to_le32(1) ||
1499 le32_to_cpu(sb
->max_dev
) > (4096-256)/2 ||
1500 le64_to_cpu(sb
->super_offset
) != rdev
->sb_start
||
1501 (le32_to_cpu(sb
->feature_map
) & ~MD_FEATURE_ALL
) != 0)
1504 if (calc_sb_1_csum(sb
) != sb
->sb_csum
) {
1505 printk("md: invalid superblock checksum on %s\n",
1506 bdevname(rdev
->bdev
,b
));
1509 if (le64_to_cpu(sb
->data_size
) < 10) {
1510 printk("md: data_size too small on %s\n",
1511 bdevname(rdev
->bdev
,b
));
1515 rdev
->preferred_minor
= 0xffff;
1516 rdev
->data_offset
= le64_to_cpu(sb
->data_offset
);
1517 atomic_set(&rdev
->corrected_errors
, le32_to_cpu(sb
->cnt_corrected_read
));
1519 rdev
->sb_size
= le32_to_cpu(sb
->max_dev
) * 2 + 256;
1520 bmask
= queue_logical_block_size(rdev
->bdev
->bd_disk
->queue
)-1;
1521 if (rdev
->sb_size
& bmask
)
1522 rdev
->sb_size
= (rdev
->sb_size
| bmask
) + 1;
1525 && rdev
->data_offset
< sb_start
+ (rdev
->sb_size
/512))
1528 if (sb
->level
== cpu_to_le32(LEVEL_MULTIPATH
))
1531 rdev
->desc_nr
= le32_to_cpu(sb
->dev_number
);
1533 if (!rdev
->bb_page
) {
1534 rdev
->bb_page
= alloc_page(GFP_KERNEL
);
1538 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_BAD_BLOCKS
) &&
1539 rdev
->badblocks
.count
== 0) {
1540 /* need to load the bad block list.
1541 * Currently we limit it to one page.
1547 int sectors
= le16_to_cpu(sb
->bblog_size
);
1548 if (sectors
> (PAGE_SIZE
/ 512))
1550 offset
= le32_to_cpu(sb
->bblog_offset
);
1553 bb_sector
= (long long)offset
;
1554 if (!sync_page_io(rdev
, bb_sector
, sectors
<< 9,
1555 rdev
->bb_page
, READ
, true))
1557 bbp
= (u64
*)page_address(rdev
->bb_page
);
1558 rdev
->badblocks
.shift
= sb
->bblog_shift
;
1559 for (i
= 0 ; i
< (sectors
<< (9-3)) ; i
++, bbp
++) {
1560 u64 bb
= le64_to_cpu(*bbp
);
1561 int count
= bb
& (0x3ff);
1562 u64 sector
= bb
>> 10;
1563 sector
<<= sb
->bblog_shift
;
1564 count
<<= sb
->bblog_shift
;
1567 if (md_set_badblocks(&rdev
->badblocks
,
1568 sector
, count
, 1) == 0)
1571 } else if (sb
->bblog_offset
== 0)
1572 rdev
->badblocks
.shift
= -1;
1578 struct mdp_superblock_1
*refsb
= page_address(refdev
->sb_page
);
1580 if (memcmp(sb
->set_uuid
, refsb
->set_uuid
, 16) != 0 ||
1581 sb
->level
!= refsb
->level
||
1582 sb
->layout
!= refsb
->layout
||
1583 sb
->chunksize
!= refsb
->chunksize
) {
1584 printk(KERN_WARNING
"md: %s has strangely different"
1585 " superblock to %s\n",
1586 bdevname(rdev
->bdev
,b
),
1587 bdevname(refdev
->bdev
,b2
));
1590 ev1
= le64_to_cpu(sb
->events
);
1591 ev2
= le64_to_cpu(refsb
->events
);
1599 rdev
->sectors
= (i_size_read(rdev
->bdev
->bd_inode
) >> 9) -
1600 le64_to_cpu(sb
->data_offset
);
1602 rdev
->sectors
= rdev
->sb_start
;
1603 if (rdev
->sectors
< le64_to_cpu(sb
->data_size
))
1605 rdev
->sectors
= le64_to_cpu(sb
->data_size
);
1606 if (le64_to_cpu(sb
->size
) > rdev
->sectors
)
1611 static int super_1_validate(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1613 struct mdp_superblock_1
*sb
= page_address(rdev
->sb_page
);
1614 __u64 ev1
= le64_to_cpu(sb
->events
);
1616 rdev
->raid_disk
= -1;
1617 clear_bit(Faulty
, &rdev
->flags
);
1618 clear_bit(In_sync
, &rdev
->flags
);
1619 clear_bit(WriteMostly
, &rdev
->flags
);
1621 if (mddev
->raid_disks
== 0) {
1622 mddev
->major_version
= 1;
1623 mddev
->patch_version
= 0;
1624 mddev
->external
= 0;
1625 mddev
->chunk_sectors
= le32_to_cpu(sb
->chunksize
);
1626 mddev
->ctime
= le64_to_cpu(sb
->ctime
) & ((1ULL << 32)-1);
1627 mddev
->utime
= le64_to_cpu(sb
->utime
) & ((1ULL << 32)-1);
1628 mddev
->level
= le32_to_cpu(sb
->level
);
1629 mddev
->clevel
[0] = 0;
1630 mddev
->layout
= le32_to_cpu(sb
->layout
);
1631 mddev
->raid_disks
= le32_to_cpu(sb
->raid_disks
);
1632 mddev
->dev_sectors
= le64_to_cpu(sb
->size
);
1633 mddev
->events
= ev1
;
1634 mddev
->bitmap_info
.offset
= 0;
1635 mddev
->bitmap_info
.default_offset
= 1024 >> 9;
1637 mddev
->recovery_cp
= le64_to_cpu(sb
->resync_offset
);
1638 memcpy(mddev
->uuid
, sb
->set_uuid
, 16);
1640 mddev
->max_disks
= (4096-256)/2;
1642 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_BITMAP_OFFSET
) &&
1643 mddev
->bitmap_info
.file
== NULL
)
1644 mddev
->bitmap_info
.offset
=
1645 (__s32
)le32_to_cpu(sb
->bitmap_offset
);
1647 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RESHAPE_ACTIVE
)) {
1648 mddev
->reshape_position
= le64_to_cpu(sb
->reshape_position
);
1649 mddev
->delta_disks
= le32_to_cpu(sb
->delta_disks
);
1650 mddev
->new_level
= le32_to_cpu(sb
->new_level
);
1651 mddev
->new_layout
= le32_to_cpu(sb
->new_layout
);
1652 mddev
->new_chunk_sectors
= le32_to_cpu(sb
->new_chunk
);
1654 mddev
->reshape_position
= MaxSector
;
1655 mddev
->delta_disks
= 0;
1656 mddev
->new_level
= mddev
->level
;
1657 mddev
->new_layout
= mddev
->layout
;
1658 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
1661 } else if (mddev
->pers
== NULL
) {
1662 /* Insist of good event counter while assembling, except for
1663 * spares (which don't need an event count) */
1665 if (rdev
->desc_nr
>= 0 &&
1666 rdev
->desc_nr
< le32_to_cpu(sb
->max_dev
) &&
1667 le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]) < 0xfffe)
1668 if (ev1
< mddev
->events
)
1670 } else if (mddev
->bitmap
) {
1671 /* If adding to array with a bitmap, then we can accept an
1672 * older device, but not too old.
1674 if (ev1
< mddev
->bitmap
->events_cleared
)
1677 if (ev1
< mddev
->events
)
1678 /* just a hot-add of a new device, leave raid_disk at -1 */
1681 if (mddev
->level
!= LEVEL_MULTIPATH
) {
1683 if (rdev
->desc_nr
< 0 ||
1684 rdev
->desc_nr
>= le32_to_cpu(sb
->max_dev
)) {
1688 role
= le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]);
1690 case 0xffff: /* spare */
1692 case 0xfffe: /* faulty */
1693 set_bit(Faulty
, &rdev
->flags
);
1696 if ((le32_to_cpu(sb
->feature_map
) &
1697 MD_FEATURE_RECOVERY_OFFSET
))
1698 rdev
->recovery_offset
= le64_to_cpu(sb
->recovery_offset
);
1700 set_bit(In_sync
, &rdev
->flags
);
1701 rdev
->raid_disk
= role
;
1704 if (sb
->devflags
& WriteMostly1
)
1705 set_bit(WriteMostly
, &rdev
->flags
);
1706 } else /* MULTIPATH are always insync */
1707 set_bit(In_sync
, &rdev
->flags
);
1712 static void super_1_sync(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1714 struct mdp_superblock_1
*sb
;
1717 /* make rdev->sb match mddev and rdev data. */
1719 sb
= page_address(rdev
->sb_page
);
1721 sb
->feature_map
= 0;
1723 sb
->recovery_offset
= cpu_to_le64(0);
1724 memset(sb
->pad1
, 0, sizeof(sb
->pad1
));
1725 memset(sb
->pad3
, 0, sizeof(sb
->pad3
));
1727 sb
->utime
= cpu_to_le64((__u64
)mddev
->utime
);
1728 sb
->events
= cpu_to_le64(mddev
->events
);
1730 sb
->resync_offset
= cpu_to_le64(mddev
->recovery_cp
);
1732 sb
->resync_offset
= cpu_to_le64(0);
1734 sb
->cnt_corrected_read
= cpu_to_le32(atomic_read(&rdev
->corrected_errors
));
1736 sb
->raid_disks
= cpu_to_le32(mddev
->raid_disks
);
1737 sb
->size
= cpu_to_le64(mddev
->dev_sectors
);
1738 sb
->chunksize
= cpu_to_le32(mddev
->chunk_sectors
);
1739 sb
->level
= cpu_to_le32(mddev
->level
);
1740 sb
->layout
= cpu_to_le32(mddev
->layout
);
1742 if (mddev
->bitmap
&& mddev
->bitmap_info
.file
== NULL
) {
1743 sb
->bitmap_offset
= cpu_to_le32((__u32
)mddev
->bitmap_info
.offset
);
1744 sb
->feature_map
= cpu_to_le32(MD_FEATURE_BITMAP_OFFSET
);
1747 if (rdev
->raid_disk
>= 0 &&
1748 !test_bit(In_sync
, &rdev
->flags
)) {
1750 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET
);
1751 sb
->recovery_offset
=
1752 cpu_to_le64(rdev
->recovery_offset
);
1755 if (mddev
->reshape_position
!= MaxSector
) {
1756 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE
);
1757 sb
->reshape_position
= cpu_to_le64(mddev
->reshape_position
);
1758 sb
->new_layout
= cpu_to_le32(mddev
->new_layout
);
1759 sb
->delta_disks
= cpu_to_le32(mddev
->delta_disks
);
1760 sb
->new_level
= cpu_to_le32(mddev
->new_level
);
1761 sb
->new_chunk
= cpu_to_le32(mddev
->new_chunk_sectors
);
1764 if (rdev
->badblocks
.count
== 0)
1765 /* Nothing to do for bad blocks*/ ;
1766 else if (sb
->bblog_offset
== 0)
1767 /* Cannot record bad blocks on this device */
1768 md_error(mddev
, rdev
);
1770 struct badblocks
*bb
= &rdev
->badblocks
;
1771 u64
*bbp
= (u64
*)page_address(rdev
->bb_page
);
1773 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_BAD_BLOCKS
);
1778 seq
= read_seqbegin(&bb
->lock
);
1780 memset(bbp
, 0xff, PAGE_SIZE
);
1782 for (i
= 0 ; i
< bb
->count
; i
++) {
1783 u64 internal_bb
= *p
++;
1784 u64 store_bb
= ((BB_OFFSET(internal_bb
) << 10)
1785 | BB_LEN(internal_bb
));
1786 *bbp
++ = cpu_to_le64(store_bb
);
1788 if (read_seqretry(&bb
->lock
, seq
))
1791 bb
->sector
= (rdev
->sb_start
+
1792 (int)le32_to_cpu(sb
->bblog_offset
));
1793 bb
->size
= le16_to_cpu(sb
->bblog_size
);
1799 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
)
1800 if (rdev2
->desc_nr
+1 > max_dev
)
1801 max_dev
= rdev2
->desc_nr
+1;
1803 if (max_dev
> le32_to_cpu(sb
->max_dev
)) {
1805 sb
->max_dev
= cpu_to_le32(max_dev
);
1806 rdev
->sb_size
= max_dev
* 2 + 256;
1807 bmask
= queue_logical_block_size(rdev
->bdev
->bd_disk
->queue
)-1;
1808 if (rdev
->sb_size
& bmask
)
1809 rdev
->sb_size
= (rdev
->sb_size
| bmask
) + 1;
1811 max_dev
= le32_to_cpu(sb
->max_dev
);
1813 for (i
=0; i
<max_dev
;i
++)
1814 sb
->dev_roles
[i
] = cpu_to_le16(0xfffe);
1816 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
) {
1818 if (test_bit(Faulty
, &rdev2
->flags
))
1819 sb
->dev_roles
[i
] = cpu_to_le16(0xfffe);
1820 else if (test_bit(In_sync
, &rdev2
->flags
))
1821 sb
->dev_roles
[i
] = cpu_to_le16(rdev2
->raid_disk
);
1822 else if (rdev2
->raid_disk
>= 0)
1823 sb
->dev_roles
[i
] = cpu_to_le16(rdev2
->raid_disk
);
1825 sb
->dev_roles
[i
] = cpu_to_le16(0xffff);
1828 sb
->sb_csum
= calc_sb_1_csum(sb
);
1831 static unsigned long long
1832 super_1_rdev_size_change(mdk_rdev_t
*rdev
, sector_t num_sectors
)
1834 struct mdp_superblock_1
*sb
;
1835 sector_t max_sectors
;
1836 if (num_sectors
&& num_sectors
< rdev
->mddev
->dev_sectors
)
1837 return 0; /* component must fit device */
1838 if (rdev
->sb_start
< rdev
->data_offset
) {
1839 /* minor versions 1 and 2; superblock before data */
1840 max_sectors
= i_size_read(rdev
->bdev
->bd_inode
) >> 9;
1841 max_sectors
-= rdev
->data_offset
;
1842 if (!num_sectors
|| num_sectors
> max_sectors
)
1843 num_sectors
= max_sectors
;
1844 } else if (rdev
->mddev
->bitmap_info
.offset
) {
1845 /* minor version 0 with bitmap we can't move */
1848 /* minor version 0; superblock after data */
1850 sb_start
= (i_size_read(rdev
->bdev
->bd_inode
) >> 9) - 8*2;
1851 sb_start
&= ~(sector_t
)(4*2 - 1);
1852 max_sectors
= rdev
->sectors
+ sb_start
- rdev
->sb_start
;
1853 if (!num_sectors
|| num_sectors
> max_sectors
)
1854 num_sectors
= max_sectors
;
1855 rdev
->sb_start
= sb_start
;
1857 sb
= page_address(rdev
->sb_page
);
1858 sb
->data_size
= cpu_to_le64(num_sectors
);
1859 sb
->super_offset
= rdev
->sb_start
;
1860 sb
->sb_csum
= calc_sb_1_csum(sb
);
1861 md_super_write(rdev
->mddev
, rdev
, rdev
->sb_start
, rdev
->sb_size
,
1863 md_super_wait(rdev
->mddev
);
1867 static struct super_type super_types
[] = {
1870 .owner
= THIS_MODULE
,
1871 .load_super
= super_90_load
,
1872 .validate_super
= super_90_validate
,
1873 .sync_super
= super_90_sync
,
1874 .rdev_size_change
= super_90_rdev_size_change
,
1878 .owner
= THIS_MODULE
,
1879 .load_super
= super_1_load
,
1880 .validate_super
= super_1_validate
,
1881 .sync_super
= super_1_sync
,
1882 .rdev_size_change
= super_1_rdev_size_change
,
1886 static void sync_super(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1888 if (mddev
->sync_super
) {
1889 mddev
->sync_super(mddev
, rdev
);
1893 BUG_ON(mddev
->major_version
>= ARRAY_SIZE(super_types
));
1895 super_types
[mddev
->major_version
].sync_super(mddev
, rdev
);
1898 static int match_mddev_units(mddev_t
*mddev1
, mddev_t
*mddev2
)
1900 mdk_rdev_t
*rdev
, *rdev2
;
1903 rdev_for_each_rcu(rdev
, mddev1
)
1904 rdev_for_each_rcu(rdev2
, mddev2
)
1905 if (rdev
->bdev
->bd_contains
==
1906 rdev2
->bdev
->bd_contains
) {
1914 static LIST_HEAD(pending_raid_disks
);
1917 * Try to register data integrity profile for an mddev
1919 * This is called when an array is started and after a disk has been kicked
1920 * from the array. It only succeeds if all working and active component devices
1921 * are integrity capable with matching profiles.
1923 int md_integrity_register(mddev_t
*mddev
)
1925 mdk_rdev_t
*rdev
, *reference
= NULL
;
1927 if (list_empty(&mddev
->disks
))
1928 return 0; /* nothing to do */
1929 if (!mddev
->gendisk
|| blk_get_integrity(mddev
->gendisk
))
1930 return 0; /* shouldn't register, or already is */
1931 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
1932 /* skip spares and non-functional disks */
1933 if (test_bit(Faulty
, &rdev
->flags
))
1935 if (rdev
->raid_disk
< 0)
1938 /* Use the first rdev as the reference */
1942 /* does this rdev's profile match the reference profile? */
1943 if (blk_integrity_compare(reference
->bdev
->bd_disk
,
1944 rdev
->bdev
->bd_disk
) < 0)
1947 if (!reference
|| !bdev_get_integrity(reference
->bdev
))
1950 * All component devices are integrity capable and have matching
1951 * profiles, register the common profile for the md device.
1953 if (blk_integrity_register(mddev
->gendisk
,
1954 bdev_get_integrity(reference
->bdev
)) != 0) {
1955 printk(KERN_ERR
"md: failed to register integrity for %s\n",
1959 printk(KERN_NOTICE
"md: data integrity enabled on %s\n", mdname(mddev
));
1960 if (bioset_integrity_create(mddev
->bio_set
, BIO_POOL_SIZE
)) {
1961 printk(KERN_ERR
"md: failed to create integrity pool for %s\n",
1967 EXPORT_SYMBOL(md_integrity_register
);
1969 /* Disable data integrity if non-capable/non-matching disk is being added */
1970 void md_integrity_add_rdev(mdk_rdev_t
*rdev
, mddev_t
*mddev
)
1972 struct blk_integrity
*bi_rdev
= bdev_get_integrity(rdev
->bdev
);
1973 struct blk_integrity
*bi_mddev
= blk_get_integrity(mddev
->gendisk
);
1975 if (!bi_mddev
) /* nothing to do */
1977 if (rdev
->raid_disk
< 0) /* skip spares */
1979 if (bi_rdev
&& blk_integrity_compare(mddev
->gendisk
,
1980 rdev
->bdev
->bd_disk
) >= 0)
1982 printk(KERN_NOTICE
"disabling data integrity on %s\n", mdname(mddev
));
1983 blk_integrity_unregister(mddev
->gendisk
);
1985 EXPORT_SYMBOL(md_integrity_add_rdev
);
1987 static int bind_rdev_to_array(mdk_rdev_t
* rdev
, mddev_t
* mddev
)
1989 char b
[BDEVNAME_SIZE
];
1999 /* prevent duplicates */
2000 if (find_rdev(mddev
, rdev
->bdev
->bd_dev
))
2003 /* make sure rdev->sectors exceeds mddev->dev_sectors */
2004 if (rdev
->sectors
&& (mddev
->dev_sectors
== 0 ||
2005 rdev
->sectors
< mddev
->dev_sectors
)) {
2007 /* Cannot change size, so fail
2008 * If mddev->level <= 0, then we don't care
2009 * about aligning sizes (e.g. linear)
2011 if (mddev
->level
> 0)
2014 mddev
->dev_sectors
= rdev
->sectors
;
2017 /* Verify rdev->desc_nr is unique.
2018 * If it is -1, assign a free number, else
2019 * check number is not in use
2021 if (rdev
->desc_nr
< 0) {
2023 if (mddev
->pers
) choice
= mddev
->raid_disks
;
2024 while (find_rdev_nr(mddev
, choice
))
2026 rdev
->desc_nr
= choice
;
2028 if (find_rdev_nr(mddev
, rdev
->desc_nr
))
2031 if (mddev
->max_disks
&& rdev
->desc_nr
>= mddev
->max_disks
) {
2032 printk(KERN_WARNING
"md: %s: array is limited to %d devices\n",
2033 mdname(mddev
), mddev
->max_disks
);
2036 bdevname(rdev
->bdev
,b
);
2037 while ( (s
=strchr(b
, '/')) != NULL
)
2040 rdev
->mddev
= mddev
;
2041 printk(KERN_INFO
"md: bind<%s>\n", b
);
2043 if ((err
= kobject_add(&rdev
->kobj
, &mddev
->kobj
, "dev-%s", b
)))
2046 ko
= &part_to_dev(rdev
->bdev
->bd_part
)->kobj
;
2047 if (sysfs_create_link(&rdev
->kobj
, ko
, "block"))
2048 /* failure here is OK */;
2049 rdev
->sysfs_state
= sysfs_get_dirent_safe(rdev
->kobj
.sd
, "state");
2051 list_add_rcu(&rdev
->same_set
, &mddev
->disks
);
2052 bd_link_disk_holder(rdev
->bdev
, mddev
->gendisk
);
2054 /* May as well allow recovery to be retried once */
2055 mddev
->recovery_disabled
++;
2060 printk(KERN_WARNING
"md: failed to register dev-%s for %s\n",
2065 static void md_delayed_delete(struct work_struct
*ws
)
2067 mdk_rdev_t
*rdev
= container_of(ws
, mdk_rdev_t
, del_work
);
2068 kobject_del(&rdev
->kobj
);
2069 kobject_put(&rdev
->kobj
);
2072 static void unbind_rdev_from_array(mdk_rdev_t
* rdev
)
2074 char b
[BDEVNAME_SIZE
];
2079 bd_unlink_disk_holder(rdev
->bdev
, rdev
->mddev
->gendisk
);
2080 list_del_rcu(&rdev
->same_set
);
2081 printk(KERN_INFO
"md: unbind<%s>\n", bdevname(rdev
->bdev
,b
));
2083 sysfs_remove_link(&rdev
->kobj
, "block");
2084 sysfs_put(rdev
->sysfs_state
);
2085 rdev
->sysfs_state
= NULL
;
2086 kfree(rdev
->badblocks
.page
);
2087 rdev
->badblocks
.count
= 0;
2088 rdev
->badblocks
.page
= NULL
;
2089 /* We need to delay this, otherwise we can deadlock when
2090 * writing to 'remove' to "dev/state". We also need
2091 * to delay it due to rcu usage.
2094 INIT_WORK(&rdev
->del_work
, md_delayed_delete
);
2095 kobject_get(&rdev
->kobj
);
2096 queue_work(md_misc_wq
, &rdev
->del_work
);
2100 * prevent the device from being mounted, repartitioned or
2101 * otherwise reused by a RAID array (or any other kernel
2102 * subsystem), by bd_claiming the device.
2104 static int lock_rdev(mdk_rdev_t
*rdev
, dev_t dev
, int shared
)
2107 struct block_device
*bdev
;
2108 char b
[BDEVNAME_SIZE
];
2110 bdev
= blkdev_get_by_dev(dev
, FMODE_READ
|FMODE_WRITE
|FMODE_EXCL
,
2111 shared
? (mdk_rdev_t
*)lock_rdev
: rdev
);
2113 printk(KERN_ERR
"md: could not open %s.\n",
2114 __bdevname(dev
, b
));
2115 return PTR_ERR(bdev
);
2121 static void unlock_rdev(mdk_rdev_t
*rdev
)
2123 struct block_device
*bdev
= rdev
->bdev
;
2127 blkdev_put(bdev
, FMODE_READ
|FMODE_WRITE
|FMODE_EXCL
);
2130 void md_autodetect_dev(dev_t dev
);
2132 static void export_rdev(mdk_rdev_t
* rdev
)
2134 char b
[BDEVNAME_SIZE
];
2135 printk(KERN_INFO
"md: export_rdev(%s)\n",
2136 bdevname(rdev
->bdev
,b
));
2141 if (test_bit(AutoDetected
, &rdev
->flags
))
2142 md_autodetect_dev(rdev
->bdev
->bd_dev
);
2145 kobject_put(&rdev
->kobj
);
2148 static void kick_rdev_from_array(mdk_rdev_t
* rdev
)
2150 unbind_rdev_from_array(rdev
);
2154 static void export_array(mddev_t
*mddev
)
2156 mdk_rdev_t
*rdev
, *tmp
;
2158 rdev_for_each(rdev
, tmp
, mddev
) {
2163 kick_rdev_from_array(rdev
);
2165 if (!list_empty(&mddev
->disks
))
2167 mddev
->raid_disks
= 0;
2168 mddev
->major_version
= 0;
2171 static void print_desc(mdp_disk_t
*desc
)
2173 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc
->number
,
2174 desc
->major
,desc
->minor
,desc
->raid_disk
,desc
->state
);
2177 static void print_sb_90(mdp_super_t
*sb
)
2182 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
2183 sb
->major_version
, sb
->minor_version
, sb
->patch_version
,
2184 sb
->set_uuid0
, sb
->set_uuid1
, sb
->set_uuid2
, sb
->set_uuid3
,
2186 printk(KERN_INFO
"md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
2187 sb
->level
, sb
->size
, sb
->nr_disks
, sb
->raid_disks
,
2188 sb
->md_minor
, sb
->layout
, sb
->chunk_size
);
2189 printk(KERN_INFO
"md: UT:%08x ST:%d AD:%d WD:%d"
2190 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
2191 sb
->utime
, sb
->state
, sb
->active_disks
, sb
->working_disks
,
2192 sb
->failed_disks
, sb
->spare_disks
,
2193 sb
->sb_csum
, (unsigned long)sb
->events_lo
);
2196 for (i
= 0; i
< MD_SB_DISKS
; i
++) {
2199 desc
= sb
->disks
+ i
;
2200 if (desc
->number
|| desc
->major
|| desc
->minor
||
2201 desc
->raid_disk
|| (desc
->state
&& (desc
->state
!= 4))) {
2202 printk(" D %2d: ", i
);
2206 printk(KERN_INFO
"md: THIS: ");
2207 print_desc(&sb
->this_disk
);
2210 static void print_sb_1(struct mdp_superblock_1
*sb
)
2214 uuid
= sb
->set_uuid
;
2216 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n"
2217 "md: Name: \"%s\" CT:%llu\n",
2218 le32_to_cpu(sb
->major_version
),
2219 le32_to_cpu(sb
->feature_map
),
2222 (unsigned long long)le64_to_cpu(sb
->ctime
)
2223 & MD_SUPERBLOCK_1_TIME_SEC_MASK
);
2225 uuid
= sb
->device_uuid
;
2227 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
2229 "md: Dev:%08x UUID: %pU\n"
2230 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
2231 "md: (MaxDev:%u) \n",
2232 le32_to_cpu(sb
->level
),
2233 (unsigned long long)le64_to_cpu(sb
->size
),
2234 le32_to_cpu(sb
->raid_disks
),
2235 le32_to_cpu(sb
->layout
),
2236 le32_to_cpu(sb
->chunksize
),
2237 (unsigned long long)le64_to_cpu(sb
->data_offset
),
2238 (unsigned long long)le64_to_cpu(sb
->data_size
),
2239 (unsigned long long)le64_to_cpu(sb
->super_offset
),
2240 (unsigned long long)le64_to_cpu(sb
->recovery_offset
),
2241 le32_to_cpu(sb
->dev_number
),
2244 (unsigned long long)le64_to_cpu(sb
->utime
) & MD_SUPERBLOCK_1_TIME_SEC_MASK
,
2245 (unsigned long long)le64_to_cpu(sb
->events
),
2246 (unsigned long long)le64_to_cpu(sb
->resync_offset
),
2247 le32_to_cpu(sb
->sb_csum
),
2248 le32_to_cpu(sb
->max_dev
)
2252 static void print_rdev(mdk_rdev_t
*rdev
, int major_version
)
2254 char b
[BDEVNAME_SIZE
];
2255 printk(KERN_INFO
"md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
2256 bdevname(rdev
->bdev
, b
), (unsigned long long)rdev
->sectors
,
2257 test_bit(Faulty
, &rdev
->flags
), test_bit(In_sync
, &rdev
->flags
),
2259 if (rdev
->sb_loaded
) {
2260 printk(KERN_INFO
"md: rdev superblock (MJ:%d):\n", major_version
);
2261 switch (major_version
) {
2263 print_sb_90(page_address(rdev
->sb_page
));
2266 print_sb_1(page_address(rdev
->sb_page
));
2270 printk(KERN_INFO
"md: no rdev superblock!\n");
2273 static void md_print_devices(void)
2275 struct list_head
*tmp
;
2278 char b
[BDEVNAME_SIZE
];
2281 printk("md: **********************************\n");
2282 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
2283 printk("md: **********************************\n");
2284 for_each_mddev(mddev
, tmp
) {
2287 bitmap_print_sb(mddev
->bitmap
);
2289 printk("%s: ", mdname(mddev
));
2290 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
2291 printk("<%s>", bdevname(rdev
->bdev
,b
));
2294 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
2295 print_rdev(rdev
, mddev
->major_version
);
2297 printk("md: **********************************\n");
2302 static void sync_sbs(mddev_t
* mddev
, int nospares
)
2304 /* Update each superblock (in-memory image), but
2305 * if we are allowed to, skip spares which already
2306 * have the right event counter, or have one earlier
2307 * (which would mean they aren't being marked as dirty
2308 * with the rest of the array)
2311 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
2312 if (rdev
->sb_events
== mddev
->events
||
2314 rdev
->raid_disk
< 0 &&
2315 rdev
->sb_events
+1 == mddev
->events
)) {
2316 /* Don't update this superblock */
2317 rdev
->sb_loaded
= 2;
2319 sync_super(mddev
, rdev
);
2320 rdev
->sb_loaded
= 1;
2325 static void md_update_sb(mddev_t
* mddev
, int force_change
)
2330 int any_badblocks_changed
= 0;
2333 /* First make sure individual recovery_offsets are correct */
2334 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
2335 if (rdev
->raid_disk
>= 0 &&
2336 mddev
->delta_disks
>= 0 &&
2337 !test_bit(In_sync
, &rdev
->flags
) &&
2338 mddev
->curr_resync_completed
> rdev
->recovery_offset
)
2339 rdev
->recovery_offset
= mddev
->curr_resync_completed
;
2342 if (!mddev
->persistent
) {
2343 clear_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
2344 clear_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
2345 if (!mddev
->external
) {
2346 clear_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
2347 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
2348 if (rdev
->badblocks
.changed
) {
2349 md_ack_all_badblocks(&rdev
->badblocks
);
2350 md_error(mddev
, rdev
);
2352 clear_bit(Blocked
, &rdev
->flags
);
2353 clear_bit(BlockedBadBlocks
, &rdev
->flags
);
2354 wake_up(&rdev
->blocked_wait
);
2357 wake_up(&mddev
->sb_wait
);
2361 spin_lock_irq(&mddev
->write_lock
);
2363 mddev
->utime
= get_seconds();
2365 if (test_and_clear_bit(MD_CHANGE_DEVS
, &mddev
->flags
))
2367 if (test_and_clear_bit(MD_CHANGE_CLEAN
, &mddev
->flags
))
2368 /* just a clean<-> dirty transition, possibly leave spares alone,
2369 * though if events isn't the right even/odd, we will have to do
2375 if (mddev
->degraded
)
2376 /* If the array is degraded, then skipping spares is both
2377 * dangerous and fairly pointless.
2378 * Dangerous because a device that was removed from the array
2379 * might have a event_count that still looks up-to-date,
2380 * so it can be re-added without a resync.
2381 * Pointless because if there are any spares to skip,
2382 * then a recovery will happen and soon that array won't
2383 * be degraded any more and the spare can go back to sleep then.
2387 sync_req
= mddev
->in_sync
;
2389 /* If this is just a dirty<->clean transition, and the array is clean
2390 * and 'events' is odd, we can roll back to the previous clean state */
2392 && (mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
)
2393 && mddev
->can_decrease_events
2394 && mddev
->events
!= 1) {
2396 mddev
->can_decrease_events
= 0;
2398 /* otherwise we have to go forward and ... */
2400 mddev
->can_decrease_events
= nospares
;
2403 if (!mddev
->events
) {
2405 * oops, this 64-bit counter should never wrap.
2406 * Either we are in around ~1 trillion A.C., assuming
2407 * 1 reboot per second, or we have a bug:
2413 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
2414 if (rdev
->badblocks
.changed
)
2415 any_badblocks_changed
++;
2416 if (test_bit(Faulty
, &rdev
->flags
))
2417 set_bit(FaultRecorded
, &rdev
->flags
);
2420 sync_sbs(mddev
, nospares
);
2421 spin_unlock_irq(&mddev
->write_lock
);
2424 "md: updating %s RAID superblock on device (in sync %d)\n",
2425 mdname(mddev
),mddev
->in_sync
);
2427 bitmap_update_sb(mddev
->bitmap
);
2428 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
2429 char b
[BDEVNAME_SIZE
];
2430 dprintk(KERN_INFO
"md: ");
2431 if (rdev
->sb_loaded
!= 1)
2432 continue; /* no noise on spare devices */
2433 if (test_bit(Faulty
, &rdev
->flags
))
2434 dprintk("(skipping faulty ");
2436 dprintk("%s ", bdevname(rdev
->bdev
,b
));
2437 if (!test_bit(Faulty
, &rdev
->flags
)) {
2438 md_super_write(mddev
,rdev
,
2439 rdev
->sb_start
, rdev
->sb_size
,
2441 dprintk(KERN_INFO
"(write) %s's sb offset: %llu\n",
2442 bdevname(rdev
->bdev
,b
),
2443 (unsigned long long)rdev
->sb_start
);
2444 rdev
->sb_events
= mddev
->events
;
2445 if (rdev
->badblocks
.size
) {
2446 md_super_write(mddev
, rdev
,
2447 rdev
->badblocks
.sector
,
2448 rdev
->badblocks
.size
<< 9,
2450 rdev
->badblocks
.size
= 0;
2455 if (mddev
->level
== LEVEL_MULTIPATH
)
2456 /* only need to write one superblock... */
2459 md_super_wait(mddev
);
2460 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
2462 spin_lock_irq(&mddev
->write_lock
);
2463 if (mddev
->in_sync
!= sync_req
||
2464 test_bit(MD_CHANGE_DEVS
, &mddev
->flags
)) {
2465 /* have to write it out again */
2466 spin_unlock_irq(&mddev
->write_lock
);
2469 clear_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
2470 spin_unlock_irq(&mddev
->write_lock
);
2471 wake_up(&mddev
->sb_wait
);
2472 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
2473 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
2475 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
2476 if (test_and_clear_bit(FaultRecorded
, &rdev
->flags
))
2477 clear_bit(Blocked
, &rdev
->flags
);
2479 if (any_badblocks_changed
)
2480 md_ack_all_badblocks(&rdev
->badblocks
);
2481 clear_bit(BlockedBadBlocks
, &rdev
->flags
);
2482 wake_up(&rdev
->blocked_wait
);
2486 /* words written to sysfs files may, or may not, be \n terminated.
2487 * We want to accept with case. For this we use cmd_match.
2489 static int cmd_match(const char *cmd
, const char *str
)
2491 /* See if cmd, written into a sysfs file, matches
2492 * str. They must either be the same, or cmd can
2493 * have a trailing newline
2495 while (*cmd
&& *str
&& *cmd
== *str
) {
2506 struct rdev_sysfs_entry
{
2507 struct attribute attr
;
2508 ssize_t (*show
)(mdk_rdev_t
*, char *);
2509 ssize_t (*store
)(mdk_rdev_t
*, const char *, size_t);
2513 state_show(mdk_rdev_t
*rdev
, char *page
)
2518 if (test_bit(Faulty
, &rdev
->flags
) ||
2519 rdev
->badblocks
.unacked_exist
) {
2520 len
+= sprintf(page
+len
, "%sfaulty",sep
);
2523 if (test_bit(In_sync
, &rdev
->flags
)) {
2524 len
+= sprintf(page
+len
, "%sin_sync",sep
);
2527 if (test_bit(WriteMostly
, &rdev
->flags
)) {
2528 len
+= sprintf(page
+len
, "%swrite_mostly",sep
);
2531 if (test_bit(Blocked
, &rdev
->flags
) ||
2532 rdev
->badblocks
.unacked_exist
) {
2533 len
+= sprintf(page
+len
, "%sblocked", sep
);
2536 if (!test_bit(Faulty
, &rdev
->flags
) &&
2537 !test_bit(In_sync
, &rdev
->flags
)) {
2538 len
+= sprintf(page
+len
, "%sspare", sep
);
2541 if (test_bit(WriteErrorSeen
, &rdev
->flags
)) {
2542 len
+= sprintf(page
+len
, "%swrite_error", sep
);
2545 return len
+sprintf(page
+len
, "\n");
2549 state_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2552 * faulty - simulates an error
2553 * remove - disconnects the device
2554 * writemostly - sets write_mostly
2555 * -writemostly - clears write_mostly
2556 * blocked - sets the Blocked flags
2557 * -blocked - clears the Blocked and possibly simulates an error
2558 * insync - sets Insync providing device isn't active
2559 * write_error - sets WriteErrorSeen
2560 * -write_error - clears WriteErrorSeen
2563 if (cmd_match(buf
, "faulty") && rdev
->mddev
->pers
) {
2564 md_error(rdev
->mddev
, rdev
);
2566 } else if (cmd_match(buf
, "remove")) {
2567 if (rdev
->raid_disk
>= 0)
2570 mddev_t
*mddev
= rdev
->mddev
;
2571 kick_rdev_from_array(rdev
);
2573 md_update_sb(mddev
, 1);
2574 md_new_event(mddev
);
2577 } else if (cmd_match(buf
, "writemostly")) {
2578 set_bit(WriteMostly
, &rdev
->flags
);
2580 } else if (cmd_match(buf
, "-writemostly")) {
2581 clear_bit(WriteMostly
, &rdev
->flags
);
2583 } else if (cmd_match(buf
, "blocked")) {
2584 set_bit(Blocked
, &rdev
->flags
);
2586 } else if (cmd_match(buf
, "-blocked")) {
2587 if (!test_bit(Faulty
, &rdev
->flags
) &&
2588 test_bit(BlockedBadBlocks
, &rdev
->flags
)) {
2589 /* metadata handler doesn't understand badblocks,
2590 * so we need to fail the device
2592 md_error(rdev
->mddev
, rdev
);
2594 clear_bit(Blocked
, &rdev
->flags
);
2595 clear_bit(BlockedBadBlocks
, &rdev
->flags
);
2596 wake_up(&rdev
->blocked_wait
);
2597 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
2598 md_wakeup_thread(rdev
->mddev
->thread
);
2601 } else if (cmd_match(buf
, "insync") && rdev
->raid_disk
== -1) {
2602 set_bit(In_sync
, &rdev
->flags
);
2604 } else if (cmd_match(buf
, "write_error")) {
2605 set_bit(WriteErrorSeen
, &rdev
->flags
);
2607 } else if (cmd_match(buf
, "-write_error")) {
2608 clear_bit(WriteErrorSeen
, &rdev
->flags
);
2612 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
2613 return err
? err
: len
;
2615 static struct rdev_sysfs_entry rdev_state
=
2616 __ATTR(state
, S_IRUGO
|S_IWUSR
, state_show
, state_store
);
2619 errors_show(mdk_rdev_t
*rdev
, char *page
)
2621 return sprintf(page
, "%d\n", atomic_read(&rdev
->corrected_errors
));
2625 errors_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2628 unsigned long n
= simple_strtoul(buf
, &e
, 10);
2629 if (*buf
&& (*e
== 0 || *e
== '\n')) {
2630 atomic_set(&rdev
->corrected_errors
, n
);
2635 static struct rdev_sysfs_entry rdev_errors
=
2636 __ATTR(errors
, S_IRUGO
|S_IWUSR
, errors_show
, errors_store
);
2639 slot_show(mdk_rdev_t
*rdev
, char *page
)
2641 if (rdev
->raid_disk
< 0)
2642 return sprintf(page
, "none\n");
2644 return sprintf(page
, "%d\n", rdev
->raid_disk
);
2648 slot_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2652 int slot
= simple_strtoul(buf
, &e
, 10);
2653 if (strncmp(buf
, "none", 4)==0)
2655 else if (e
==buf
|| (*e
&& *e
!= '\n'))
2657 if (rdev
->mddev
->pers
&& slot
== -1) {
2658 /* Setting 'slot' on an active array requires also
2659 * updating the 'rd%d' link, and communicating
2660 * with the personality with ->hot_*_disk.
2661 * For now we only support removing
2662 * failed/spare devices. This normally happens automatically,
2663 * but not when the metadata is externally managed.
2665 if (rdev
->raid_disk
== -1)
2667 /* personality does all needed checks */
2668 if (rdev
->mddev
->pers
->hot_remove_disk
== NULL
)
2670 err
= rdev
->mddev
->pers
->
2671 hot_remove_disk(rdev
->mddev
, rdev
->raid_disk
);
2674 sysfs_unlink_rdev(rdev
->mddev
, rdev
);
2675 rdev
->raid_disk
= -1;
2676 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
2677 md_wakeup_thread(rdev
->mddev
->thread
);
2678 } else if (rdev
->mddev
->pers
) {
2680 /* Activating a spare .. or possibly reactivating
2681 * if we ever get bitmaps working here.
2684 if (rdev
->raid_disk
!= -1)
2687 if (test_bit(MD_RECOVERY_RUNNING
, &rdev
->mddev
->recovery
))
2690 if (rdev
->mddev
->pers
->hot_add_disk
== NULL
)
2693 list_for_each_entry(rdev2
, &rdev
->mddev
->disks
, same_set
)
2694 if (rdev2
->raid_disk
== slot
)
2697 if (slot
>= rdev
->mddev
->raid_disks
&&
2698 slot
>= rdev
->mddev
->raid_disks
+ rdev
->mddev
->delta_disks
)
2701 rdev
->raid_disk
= slot
;
2702 if (test_bit(In_sync
, &rdev
->flags
))
2703 rdev
->saved_raid_disk
= slot
;
2705 rdev
->saved_raid_disk
= -1;
2706 err
= rdev
->mddev
->pers
->
2707 hot_add_disk(rdev
->mddev
, rdev
);
2709 rdev
->raid_disk
= -1;
2712 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
2713 if (sysfs_link_rdev(rdev
->mddev
, rdev
))
2714 /* failure here is OK */;
2715 /* don't wakeup anyone, leave that to userspace. */
2717 if (slot
>= rdev
->mddev
->raid_disks
&&
2718 slot
>= rdev
->mddev
->raid_disks
+ rdev
->mddev
->delta_disks
)
2720 rdev
->raid_disk
= slot
;
2721 /* assume it is working */
2722 clear_bit(Faulty
, &rdev
->flags
);
2723 clear_bit(WriteMostly
, &rdev
->flags
);
2724 set_bit(In_sync
, &rdev
->flags
);
2725 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
2731 static struct rdev_sysfs_entry rdev_slot
=
2732 __ATTR(slot
, S_IRUGO
|S_IWUSR
, slot_show
, slot_store
);
2735 offset_show(mdk_rdev_t
*rdev
, char *page
)
2737 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->data_offset
);
2741 offset_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2744 unsigned long long offset
= simple_strtoull(buf
, &e
, 10);
2745 if (e
==buf
|| (*e
&& *e
!= '\n'))
2747 if (rdev
->mddev
->pers
&& rdev
->raid_disk
>= 0)
2749 if (rdev
->sectors
&& rdev
->mddev
->external
)
2750 /* Must set offset before size, so overlap checks
2753 rdev
->data_offset
= offset
;
2757 static struct rdev_sysfs_entry rdev_offset
=
2758 __ATTR(offset
, S_IRUGO
|S_IWUSR
, offset_show
, offset_store
);
2761 rdev_size_show(mdk_rdev_t
*rdev
, char *page
)
2763 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->sectors
/ 2);
2766 static int overlaps(sector_t s1
, sector_t l1
, sector_t s2
, sector_t l2
)
2768 /* check if two start/length pairs overlap */
2776 static int strict_blocks_to_sectors(const char *buf
, sector_t
*sectors
)
2778 unsigned long long blocks
;
2781 if (strict_strtoull(buf
, 10, &blocks
) < 0)
2784 if (blocks
& 1ULL << (8 * sizeof(blocks
) - 1))
2785 return -EINVAL
; /* sector conversion overflow */
2788 if (new != blocks
* 2)
2789 return -EINVAL
; /* unsigned long long to sector_t overflow */
2796 rdev_size_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2798 mddev_t
*my_mddev
= rdev
->mddev
;
2799 sector_t oldsectors
= rdev
->sectors
;
2802 if (strict_blocks_to_sectors(buf
, §ors
) < 0)
2804 if (my_mddev
->pers
&& rdev
->raid_disk
>= 0) {
2805 if (my_mddev
->persistent
) {
2806 sectors
= super_types
[my_mddev
->major_version
].
2807 rdev_size_change(rdev
, sectors
);
2810 } else if (!sectors
)
2811 sectors
= (i_size_read(rdev
->bdev
->bd_inode
) >> 9) -
2814 if (sectors
< my_mddev
->dev_sectors
)
2815 return -EINVAL
; /* component must fit device */
2817 rdev
->sectors
= sectors
;
2818 if (sectors
> oldsectors
&& my_mddev
->external
) {
2819 /* need to check that all other rdevs with the same ->bdev
2820 * do not overlap. We need to unlock the mddev to avoid
2821 * a deadlock. We have already changed rdev->sectors, and if
2822 * we have to change it back, we will have the lock again.
2826 struct list_head
*tmp
;
2828 mddev_unlock(my_mddev
);
2829 for_each_mddev(mddev
, tmp
) {
2833 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
)
2834 if (rdev
->bdev
== rdev2
->bdev
&&
2836 overlaps(rdev
->data_offset
, rdev
->sectors
,
2842 mddev_unlock(mddev
);
2848 mddev_lock(my_mddev
);
2850 /* Someone else could have slipped in a size
2851 * change here, but doing so is just silly.
2852 * We put oldsectors back because we *know* it is
2853 * safe, and trust userspace not to race with
2856 rdev
->sectors
= oldsectors
;
2863 static struct rdev_sysfs_entry rdev_size
=
2864 __ATTR(size
, S_IRUGO
|S_IWUSR
, rdev_size_show
, rdev_size_store
);
2867 static ssize_t
recovery_start_show(mdk_rdev_t
*rdev
, char *page
)
2869 unsigned long long recovery_start
= rdev
->recovery_offset
;
2871 if (test_bit(In_sync
, &rdev
->flags
) ||
2872 recovery_start
== MaxSector
)
2873 return sprintf(page
, "none\n");
2875 return sprintf(page
, "%llu\n", recovery_start
);
2878 static ssize_t
recovery_start_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2880 unsigned long long recovery_start
;
2882 if (cmd_match(buf
, "none"))
2883 recovery_start
= MaxSector
;
2884 else if (strict_strtoull(buf
, 10, &recovery_start
))
2887 if (rdev
->mddev
->pers
&&
2888 rdev
->raid_disk
>= 0)
2891 rdev
->recovery_offset
= recovery_start
;
2892 if (recovery_start
== MaxSector
)
2893 set_bit(In_sync
, &rdev
->flags
);
2895 clear_bit(In_sync
, &rdev
->flags
);
2899 static struct rdev_sysfs_entry rdev_recovery_start
=
2900 __ATTR(recovery_start
, S_IRUGO
|S_IWUSR
, recovery_start_show
, recovery_start_store
);
2904 badblocks_show(struct badblocks
*bb
, char *page
, int unack
);
2906 badblocks_store(struct badblocks
*bb
, const char *page
, size_t len
, int unack
);
2908 static ssize_t
bb_show(mdk_rdev_t
*rdev
, char *page
)
2910 return badblocks_show(&rdev
->badblocks
, page
, 0);
2912 static ssize_t
bb_store(mdk_rdev_t
*rdev
, const char *page
, size_t len
)
2914 int rv
= badblocks_store(&rdev
->badblocks
, page
, len
, 0);
2915 /* Maybe that ack was all we needed */
2916 if (test_and_clear_bit(BlockedBadBlocks
, &rdev
->flags
))
2917 wake_up(&rdev
->blocked_wait
);
2920 static struct rdev_sysfs_entry rdev_bad_blocks
=
2921 __ATTR(bad_blocks
, S_IRUGO
|S_IWUSR
, bb_show
, bb_store
);
2924 static ssize_t
ubb_show(mdk_rdev_t
*rdev
, char *page
)
2926 return badblocks_show(&rdev
->badblocks
, page
, 1);
2928 static ssize_t
ubb_store(mdk_rdev_t
*rdev
, const char *page
, size_t len
)
2930 return badblocks_store(&rdev
->badblocks
, page
, len
, 1);
2932 static struct rdev_sysfs_entry rdev_unack_bad_blocks
=
2933 __ATTR(unacknowledged_bad_blocks
, S_IRUGO
|S_IWUSR
, ubb_show
, ubb_store
);
2935 static struct attribute
*rdev_default_attrs
[] = {
2941 &rdev_recovery_start
.attr
,
2942 &rdev_bad_blocks
.attr
,
2943 &rdev_unack_bad_blocks
.attr
,
2947 rdev_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
2949 struct rdev_sysfs_entry
*entry
= container_of(attr
, struct rdev_sysfs_entry
, attr
);
2950 mdk_rdev_t
*rdev
= container_of(kobj
, mdk_rdev_t
, kobj
);
2951 mddev_t
*mddev
= rdev
->mddev
;
2957 rv
= mddev
? mddev_lock(mddev
) : -EBUSY
;
2959 if (rdev
->mddev
== NULL
)
2962 rv
= entry
->show(rdev
, page
);
2963 mddev_unlock(mddev
);
2969 rdev_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
2970 const char *page
, size_t length
)
2972 struct rdev_sysfs_entry
*entry
= container_of(attr
, struct rdev_sysfs_entry
, attr
);
2973 mdk_rdev_t
*rdev
= container_of(kobj
, mdk_rdev_t
, kobj
);
2975 mddev_t
*mddev
= rdev
->mddev
;
2979 if (!capable(CAP_SYS_ADMIN
))
2981 rv
= mddev
? mddev_lock(mddev
): -EBUSY
;
2983 if (rdev
->mddev
== NULL
)
2986 rv
= entry
->store(rdev
, page
, length
);
2987 mddev_unlock(mddev
);
2992 static void rdev_free(struct kobject
*ko
)
2994 mdk_rdev_t
*rdev
= container_of(ko
, mdk_rdev_t
, kobj
);
2997 static const struct sysfs_ops rdev_sysfs_ops
= {
2998 .show
= rdev_attr_show
,
2999 .store
= rdev_attr_store
,
3001 static struct kobj_type rdev_ktype
= {
3002 .release
= rdev_free
,
3003 .sysfs_ops
= &rdev_sysfs_ops
,
3004 .default_attrs
= rdev_default_attrs
,
3007 int md_rdev_init(mdk_rdev_t
*rdev
)
3010 rdev
->saved_raid_disk
= -1;
3011 rdev
->raid_disk
= -1;
3013 rdev
->data_offset
= 0;
3014 rdev
->sb_events
= 0;
3015 rdev
->last_read_error
.tv_sec
= 0;
3016 rdev
->last_read_error
.tv_nsec
= 0;
3017 rdev
->sb_loaded
= 0;
3018 rdev
->bb_page
= NULL
;
3019 atomic_set(&rdev
->nr_pending
, 0);
3020 atomic_set(&rdev
->read_errors
, 0);
3021 atomic_set(&rdev
->corrected_errors
, 0);
3023 INIT_LIST_HEAD(&rdev
->same_set
);
3024 init_waitqueue_head(&rdev
->blocked_wait
);
3026 /* Add space to store bad block list.
3027 * This reserves the space even on arrays where it cannot
3028 * be used - I wonder if that matters
3030 rdev
->badblocks
.count
= 0;
3031 rdev
->badblocks
.shift
= 0;
3032 rdev
->badblocks
.page
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
3033 seqlock_init(&rdev
->badblocks
.lock
);
3034 if (rdev
->badblocks
.page
== NULL
)
3039 EXPORT_SYMBOL_GPL(md_rdev_init
);
3041 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3043 * mark the device faulty if:
3045 * - the device is nonexistent (zero size)
3046 * - the device has no valid superblock
3048 * a faulty rdev _never_ has rdev->sb set.
3050 static mdk_rdev_t
*md_import_device(dev_t newdev
, int super_format
, int super_minor
)
3052 char b
[BDEVNAME_SIZE
];
3057 rdev
= kzalloc(sizeof(*rdev
), GFP_KERNEL
);
3059 printk(KERN_ERR
"md: could not alloc mem for new device!\n");
3060 return ERR_PTR(-ENOMEM
);
3063 err
= md_rdev_init(rdev
);
3066 err
= alloc_disk_sb(rdev
);
3070 err
= lock_rdev(rdev
, newdev
, super_format
== -2);
3074 kobject_init(&rdev
->kobj
, &rdev_ktype
);
3076 size
= i_size_read(rdev
->bdev
->bd_inode
) >> BLOCK_SIZE_BITS
;
3079 "md: %s has zero or unknown size, marking faulty!\n",
3080 bdevname(rdev
->bdev
,b
));
3085 if (super_format
>= 0) {
3086 err
= super_types
[super_format
].
3087 load_super(rdev
, NULL
, super_minor
);
3088 if (err
== -EINVAL
) {
3090 "md: %s does not have a valid v%d.%d "
3091 "superblock, not importing!\n",
3092 bdevname(rdev
->bdev
,b
),
3093 super_format
, super_minor
);
3098 "md: could not read %s's sb, not importing!\n",
3099 bdevname(rdev
->bdev
,b
));
3103 if (super_format
== -1)
3104 /* hot-add for 0.90, or non-persistent: so no badblocks */
3105 rdev
->badblocks
.shift
= -1;
3113 kfree(rdev
->badblocks
.page
);
3115 return ERR_PTR(err
);
3119 * Check a full RAID array for plausibility
3123 static void analyze_sbs(mddev_t
* mddev
)
3126 mdk_rdev_t
*rdev
, *freshest
, *tmp
;
3127 char b
[BDEVNAME_SIZE
];
3130 rdev_for_each(rdev
, tmp
, mddev
)
3131 switch (super_types
[mddev
->major_version
].
3132 load_super(rdev
, freshest
, mddev
->minor_version
)) {
3140 "md: fatal superblock inconsistency in %s"
3141 " -- removing from array\n",
3142 bdevname(rdev
->bdev
,b
));
3143 kick_rdev_from_array(rdev
);
3147 super_types
[mddev
->major_version
].
3148 validate_super(mddev
, freshest
);
3151 rdev_for_each(rdev
, tmp
, mddev
) {
3152 if (mddev
->max_disks
&&
3153 (rdev
->desc_nr
>= mddev
->max_disks
||
3154 i
> mddev
->max_disks
)) {
3156 "md: %s: %s: only %d devices permitted\n",
3157 mdname(mddev
), bdevname(rdev
->bdev
, b
),
3159 kick_rdev_from_array(rdev
);
3162 if (rdev
!= freshest
)
3163 if (super_types
[mddev
->major_version
].
3164 validate_super(mddev
, rdev
)) {
3165 printk(KERN_WARNING
"md: kicking non-fresh %s"
3167 bdevname(rdev
->bdev
,b
));
3168 kick_rdev_from_array(rdev
);
3171 if (mddev
->level
== LEVEL_MULTIPATH
) {
3172 rdev
->desc_nr
= i
++;
3173 rdev
->raid_disk
= rdev
->desc_nr
;
3174 set_bit(In_sync
, &rdev
->flags
);
3175 } else if (rdev
->raid_disk
>= (mddev
->raid_disks
- min(0, mddev
->delta_disks
))) {
3176 rdev
->raid_disk
= -1;
3177 clear_bit(In_sync
, &rdev
->flags
);
3182 /* Read a fixed-point number.
3183 * Numbers in sysfs attributes should be in "standard" units where
3184 * possible, so time should be in seconds.
3185 * However we internally use a a much smaller unit such as
3186 * milliseconds or jiffies.
3187 * This function takes a decimal number with a possible fractional
3188 * component, and produces an integer which is the result of
3189 * multiplying that number by 10^'scale'.
3190 * all without any floating-point arithmetic.
3192 int strict_strtoul_scaled(const char *cp
, unsigned long *res
, int scale
)
3194 unsigned long result
= 0;
3196 while (isdigit(*cp
) || (*cp
== '.' && decimals
< 0)) {
3199 else if (decimals
< scale
) {
3202 result
= result
* 10 + value
;
3214 while (decimals
< scale
) {
3223 static void md_safemode_timeout(unsigned long data
);
3226 safe_delay_show(mddev_t
*mddev
, char *page
)
3228 int msec
= (mddev
->safemode_delay
*1000)/HZ
;
3229 return sprintf(page
, "%d.%03d\n", msec
/1000, msec
%1000);
3232 safe_delay_store(mddev_t
*mddev
, const char *cbuf
, size_t len
)
3236 if (strict_strtoul_scaled(cbuf
, &msec
, 3) < 0)
3239 mddev
->safemode_delay
= 0;
3241 unsigned long old_delay
= mddev
->safemode_delay
;
3242 mddev
->safemode_delay
= (msec
*HZ
)/1000;
3243 if (mddev
->safemode_delay
== 0)
3244 mddev
->safemode_delay
= 1;
3245 if (mddev
->safemode_delay
< old_delay
)
3246 md_safemode_timeout((unsigned long)mddev
);
3250 static struct md_sysfs_entry md_safe_delay
=
3251 __ATTR(safe_mode_delay
, S_IRUGO
|S_IWUSR
,safe_delay_show
, safe_delay_store
);
3254 level_show(mddev_t
*mddev
, char *page
)
3256 struct mdk_personality
*p
= mddev
->pers
;
3258 return sprintf(page
, "%s\n", p
->name
);
3259 else if (mddev
->clevel
[0])
3260 return sprintf(page
, "%s\n", mddev
->clevel
);
3261 else if (mddev
->level
!= LEVEL_NONE
)
3262 return sprintf(page
, "%d\n", mddev
->level
);
3268 level_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3272 struct mdk_personality
*pers
;
3277 if (mddev
->pers
== NULL
) {
3280 if (len
>= sizeof(mddev
->clevel
))
3282 strncpy(mddev
->clevel
, buf
, len
);
3283 if (mddev
->clevel
[len
-1] == '\n')
3285 mddev
->clevel
[len
] = 0;
3286 mddev
->level
= LEVEL_NONE
;
3290 /* request to change the personality. Need to ensure:
3291 * - array is not engaged in resync/recovery/reshape
3292 * - old personality can be suspended
3293 * - new personality will access other array.
3296 if (mddev
->sync_thread
||
3297 mddev
->reshape_position
!= MaxSector
||
3298 mddev
->sysfs_active
)
3301 if (!mddev
->pers
->quiesce
) {
3302 printk(KERN_WARNING
"md: %s: %s does not support online personality change\n",
3303 mdname(mddev
), mddev
->pers
->name
);
3307 /* Now find the new personality */
3308 if (len
== 0 || len
>= sizeof(clevel
))
3310 strncpy(clevel
, buf
, len
);
3311 if (clevel
[len
-1] == '\n')
3314 if (strict_strtol(clevel
, 10, &level
))
3317 if (request_module("md-%s", clevel
) != 0)
3318 request_module("md-level-%s", clevel
);
3319 spin_lock(&pers_lock
);
3320 pers
= find_pers(level
, clevel
);
3321 if (!pers
|| !try_module_get(pers
->owner
)) {
3322 spin_unlock(&pers_lock
);
3323 printk(KERN_WARNING
"md: personality %s not loaded\n", clevel
);
3326 spin_unlock(&pers_lock
);
3328 if (pers
== mddev
->pers
) {
3329 /* Nothing to do! */
3330 module_put(pers
->owner
);
3333 if (!pers
->takeover
) {
3334 module_put(pers
->owner
);
3335 printk(KERN_WARNING
"md: %s: %s does not support personality takeover\n",
3336 mdname(mddev
), clevel
);
3340 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
3341 rdev
->new_raid_disk
= rdev
->raid_disk
;
3343 /* ->takeover must set new_* and/or delta_disks
3344 * if it succeeds, and may set them when it fails.
3346 priv
= pers
->takeover(mddev
);
3348 mddev
->new_level
= mddev
->level
;
3349 mddev
->new_layout
= mddev
->layout
;
3350 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
3351 mddev
->raid_disks
-= mddev
->delta_disks
;
3352 mddev
->delta_disks
= 0;
3353 module_put(pers
->owner
);
3354 printk(KERN_WARNING
"md: %s: %s would not accept array\n",
3355 mdname(mddev
), clevel
);
3356 return PTR_ERR(priv
);
3359 /* Looks like we have a winner */
3360 mddev_suspend(mddev
);
3361 mddev
->pers
->stop(mddev
);
3363 if (mddev
->pers
->sync_request
== NULL
&&
3364 pers
->sync_request
!= NULL
) {
3365 /* need to add the md_redundancy_group */
3366 if (sysfs_create_group(&mddev
->kobj
, &md_redundancy_group
))
3368 "md: cannot register extra attributes for %s\n",
3370 mddev
->sysfs_action
= sysfs_get_dirent(mddev
->kobj
.sd
, NULL
, "sync_action");
3372 if (mddev
->pers
->sync_request
!= NULL
&&
3373 pers
->sync_request
== NULL
) {
3374 /* need to remove the md_redundancy_group */
3375 if (mddev
->to_remove
== NULL
)
3376 mddev
->to_remove
= &md_redundancy_group
;
3379 if (mddev
->pers
->sync_request
== NULL
&&
3381 /* We are converting from a no-redundancy array
3382 * to a redundancy array and metadata is managed
3383 * externally so we need to be sure that writes
3384 * won't block due to a need to transition
3386 * until external management is started.
3389 mddev
->safemode_delay
= 0;
3390 mddev
->safemode
= 0;
3393 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
3394 if (rdev
->raid_disk
< 0)
3396 if (rdev
->new_raid_disk
>= mddev
->raid_disks
)
3397 rdev
->new_raid_disk
= -1;
3398 if (rdev
->new_raid_disk
== rdev
->raid_disk
)
3400 sysfs_unlink_rdev(mddev
, rdev
);
3402 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
3403 if (rdev
->raid_disk
< 0)
3405 if (rdev
->new_raid_disk
== rdev
->raid_disk
)
3407 rdev
->raid_disk
= rdev
->new_raid_disk
;
3408 if (rdev
->raid_disk
< 0)
3409 clear_bit(In_sync
, &rdev
->flags
);
3411 if (sysfs_link_rdev(mddev
, rdev
))
3412 printk(KERN_WARNING
"md: cannot register rd%d"
3413 " for %s after level change\n",
3414 rdev
->raid_disk
, mdname(mddev
));
3418 module_put(mddev
->pers
->owner
);
3420 mddev
->private = priv
;
3421 strlcpy(mddev
->clevel
, pers
->name
, sizeof(mddev
->clevel
));
3422 mddev
->level
= mddev
->new_level
;
3423 mddev
->layout
= mddev
->new_layout
;
3424 mddev
->chunk_sectors
= mddev
->new_chunk_sectors
;
3425 mddev
->delta_disks
= 0;
3426 mddev
->degraded
= 0;
3427 if (mddev
->pers
->sync_request
== NULL
) {
3428 /* this is now an array without redundancy, so
3429 * it must always be in_sync
3432 del_timer_sync(&mddev
->safemode_timer
);
3435 mddev_resume(mddev
);
3436 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
3437 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3438 md_wakeup_thread(mddev
->thread
);
3439 sysfs_notify(&mddev
->kobj
, NULL
, "level");
3440 md_new_event(mddev
);
3444 static struct md_sysfs_entry md_level
=
3445 __ATTR(level
, S_IRUGO
|S_IWUSR
, level_show
, level_store
);
3449 layout_show(mddev_t
*mddev
, char *page
)
3451 /* just a number, not meaningful for all levels */
3452 if (mddev
->reshape_position
!= MaxSector
&&
3453 mddev
->layout
!= mddev
->new_layout
)
3454 return sprintf(page
, "%d (%d)\n",
3455 mddev
->new_layout
, mddev
->layout
);
3456 return sprintf(page
, "%d\n", mddev
->layout
);
3460 layout_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3463 unsigned long n
= simple_strtoul(buf
, &e
, 10);
3465 if (!*buf
|| (*e
&& *e
!= '\n'))
3470 if (mddev
->pers
->check_reshape
== NULL
)
3472 mddev
->new_layout
= n
;
3473 err
= mddev
->pers
->check_reshape(mddev
);
3475 mddev
->new_layout
= mddev
->layout
;
3479 mddev
->new_layout
= n
;
3480 if (mddev
->reshape_position
== MaxSector
)
3485 static struct md_sysfs_entry md_layout
=
3486 __ATTR(layout
, S_IRUGO
|S_IWUSR
, layout_show
, layout_store
);
3490 raid_disks_show(mddev_t
*mddev
, char *page
)
3492 if (mddev
->raid_disks
== 0)
3494 if (mddev
->reshape_position
!= MaxSector
&&
3495 mddev
->delta_disks
!= 0)
3496 return sprintf(page
, "%d (%d)\n", mddev
->raid_disks
,
3497 mddev
->raid_disks
- mddev
->delta_disks
);
3498 return sprintf(page
, "%d\n", mddev
->raid_disks
);
3501 static int update_raid_disks(mddev_t
*mddev
, int raid_disks
);
3504 raid_disks_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3508 unsigned long n
= simple_strtoul(buf
, &e
, 10);
3510 if (!*buf
|| (*e
&& *e
!= '\n'))
3514 rv
= update_raid_disks(mddev
, n
);
3515 else if (mddev
->reshape_position
!= MaxSector
) {
3516 int olddisks
= mddev
->raid_disks
- mddev
->delta_disks
;
3517 mddev
->delta_disks
= n
- olddisks
;
3518 mddev
->raid_disks
= n
;
3520 mddev
->raid_disks
= n
;
3521 return rv
? rv
: len
;
3523 static struct md_sysfs_entry md_raid_disks
=
3524 __ATTR(raid_disks
, S_IRUGO
|S_IWUSR
, raid_disks_show
, raid_disks_store
);
3527 chunk_size_show(mddev_t
*mddev
, char *page
)
3529 if (mddev
->reshape_position
!= MaxSector
&&
3530 mddev
->chunk_sectors
!= mddev
->new_chunk_sectors
)
3531 return sprintf(page
, "%d (%d)\n",
3532 mddev
->new_chunk_sectors
<< 9,
3533 mddev
->chunk_sectors
<< 9);
3534 return sprintf(page
, "%d\n", mddev
->chunk_sectors
<< 9);
3538 chunk_size_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3541 unsigned long n
= simple_strtoul(buf
, &e
, 10);
3543 if (!*buf
|| (*e
&& *e
!= '\n'))
3548 if (mddev
->pers
->check_reshape
== NULL
)
3550 mddev
->new_chunk_sectors
= n
>> 9;
3551 err
= mddev
->pers
->check_reshape(mddev
);
3553 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
3557 mddev
->new_chunk_sectors
= n
>> 9;
3558 if (mddev
->reshape_position
== MaxSector
)
3559 mddev
->chunk_sectors
= n
>> 9;
3563 static struct md_sysfs_entry md_chunk_size
=
3564 __ATTR(chunk_size
, S_IRUGO
|S_IWUSR
, chunk_size_show
, chunk_size_store
);
3567 resync_start_show(mddev_t
*mddev
, char *page
)
3569 if (mddev
->recovery_cp
== MaxSector
)
3570 return sprintf(page
, "none\n");
3571 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->recovery_cp
);
3575 resync_start_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3578 unsigned long long n
= simple_strtoull(buf
, &e
, 10);
3580 if (mddev
->pers
&& !test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
))
3582 if (cmd_match(buf
, "none"))
3584 else if (!*buf
|| (*e
&& *e
!= '\n'))
3587 mddev
->recovery_cp
= n
;
3590 static struct md_sysfs_entry md_resync_start
=
3591 __ATTR(resync_start
, S_IRUGO
|S_IWUSR
, resync_start_show
, resync_start_store
);
3594 * The array state can be:
3597 * No devices, no size, no level
3598 * Equivalent to STOP_ARRAY ioctl
3600 * May have some settings, but array is not active
3601 * all IO results in error
3602 * When written, doesn't tear down array, but just stops it
3603 * suspended (not supported yet)
3604 * All IO requests will block. The array can be reconfigured.
3605 * Writing this, if accepted, will block until array is quiescent
3607 * no resync can happen. no superblocks get written.
3608 * write requests fail
3610 * like readonly, but behaves like 'clean' on a write request.
3612 * clean - no pending writes, but otherwise active.
3613 * When written to inactive array, starts without resync
3614 * If a write request arrives then
3615 * if metadata is known, mark 'dirty' and switch to 'active'.
3616 * if not known, block and switch to write-pending
3617 * If written to an active array that has pending writes, then fails.
3619 * fully active: IO and resync can be happening.
3620 * When written to inactive array, starts with resync
3623 * clean, but writes are blocked waiting for 'active' to be written.
3626 * like active, but no writes have been seen for a while (100msec).
3629 enum array_state
{ clear
, inactive
, suspended
, readonly
, read_auto
, clean
, active
,
3630 write_pending
, active_idle
, bad_word
};
3631 static char *array_states
[] = {
3632 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3633 "write-pending", "active-idle", NULL
};
3635 static int match_word(const char *word
, char **list
)
3638 for (n
=0; list
[n
]; n
++)
3639 if (cmd_match(word
, list
[n
]))
3645 array_state_show(mddev_t
*mddev
, char *page
)
3647 enum array_state st
= inactive
;
3660 else if (test_bit(MD_CHANGE_PENDING
, &mddev
->flags
))
3662 else if (mddev
->safemode
)
3668 if (list_empty(&mddev
->disks
) &&
3669 mddev
->raid_disks
== 0 &&
3670 mddev
->dev_sectors
== 0)
3675 return sprintf(page
, "%s\n", array_states
[st
]);
3678 static int do_md_stop(mddev_t
* mddev
, int ro
, int is_open
);
3679 static int md_set_readonly(mddev_t
* mddev
, int is_open
);
3680 static int do_md_run(mddev_t
* mddev
);
3681 static int restart_array(mddev_t
*mddev
);
3684 array_state_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3687 enum array_state st
= match_word(buf
, array_states
);
3692 /* stopping an active array */
3693 if (atomic_read(&mddev
->openers
) > 0)
3695 err
= do_md_stop(mddev
, 0, 0);
3698 /* stopping an active array */
3700 if (atomic_read(&mddev
->openers
) > 0)
3702 err
= do_md_stop(mddev
, 2, 0);
3704 err
= 0; /* already inactive */
3707 break; /* not supported yet */
3710 err
= md_set_readonly(mddev
, 0);
3713 set_disk_ro(mddev
->gendisk
, 1);
3714 err
= do_md_run(mddev
);
3720 err
= md_set_readonly(mddev
, 0);
3721 else if (mddev
->ro
== 1)
3722 err
= restart_array(mddev
);
3725 set_disk_ro(mddev
->gendisk
, 0);
3729 err
= do_md_run(mddev
);
3734 restart_array(mddev
);
3735 spin_lock_irq(&mddev
->write_lock
);
3736 if (atomic_read(&mddev
->writes_pending
) == 0) {
3737 if (mddev
->in_sync
== 0) {
3739 if (mddev
->safemode
== 1)
3740 mddev
->safemode
= 0;
3741 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
3746 spin_unlock_irq(&mddev
->write_lock
);
3752 restart_array(mddev
);
3753 clear_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
3754 wake_up(&mddev
->sb_wait
);
3758 set_disk_ro(mddev
->gendisk
, 0);
3759 err
= do_md_run(mddev
);
3764 /* these cannot be set */
3770 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
3774 static struct md_sysfs_entry md_array_state
=
3775 __ATTR(array_state
, S_IRUGO
|S_IWUSR
, array_state_show
, array_state_store
);
3778 max_corrected_read_errors_show(mddev_t
*mddev
, char *page
) {
3779 return sprintf(page
, "%d\n",
3780 atomic_read(&mddev
->max_corr_read_errors
));
3784 max_corrected_read_errors_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3787 unsigned long n
= simple_strtoul(buf
, &e
, 10);
3789 if (*buf
&& (*e
== 0 || *e
== '\n')) {
3790 atomic_set(&mddev
->max_corr_read_errors
, n
);
3796 static struct md_sysfs_entry max_corr_read_errors
=
3797 __ATTR(max_read_errors
, S_IRUGO
|S_IWUSR
, max_corrected_read_errors_show
,
3798 max_corrected_read_errors_store
);
3801 null_show(mddev_t
*mddev
, char *page
)
3807 new_dev_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3809 /* buf must be %d:%d\n? giving major and minor numbers */
3810 /* The new device is added to the array.
3811 * If the array has a persistent superblock, we read the
3812 * superblock to initialise info and check validity.
3813 * Otherwise, only checking done is that in bind_rdev_to_array,
3814 * which mainly checks size.
3817 int major
= simple_strtoul(buf
, &e
, 10);
3823 if (!*buf
|| *e
!= ':' || !e
[1] || e
[1] == '\n')
3825 minor
= simple_strtoul(e
+1, &e
, 10);
3826 if (*e
&& *e
!= '\n')
3828 dev
= MKDEV(major
, minor
);
3829 if (major
!= MAJOR(dev
) ||
3830 minor
!= MINOR(dev
))
3834 if (mddev
->persistent
) {
3835 rdev
= md_import_device(dev
, mddev
->major_version
,
3836 mddev
->minor_version
);
3837 if (!IS_ERR(rdev
) && !list_empty(&mddev
->disks
)) {
3838 mdk_rdev_t
*rdev0
= list_entry(mddev
->disks
.next
,
3839 mdk_rdev_t
, same_set
);
3840 err
= super_types
[mddev
->major_version
]
3841 .load_super(rdev
, rdev0
, mddev
->minor_version
);
3845 } else if (mddev
->external
)
3846 rdev
= md_import_device(dev
, -2, -1);
3848 rdev
= md_import_device(dev
, -1, -1);
3851 return PTR_ERR(rdev
);
3852 err
= bind_rdev_to_array(rdev
, mddev
);
3856 return err
? err
: len
;
3859 static struct md_sysfs_entry md_new_device
=
3860 __ATTR(new_dev
, S_IWUSR
, null_show
, new_dev_store
);
3863 bitmap_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3866 unsigned long chunk
, end_chunk
;
3870 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3872 chunk
= end_chunk
= simple_strtoul(buf
, &end
, 0);
3873 if (buf
== end
) break;
3874 if (*end
== '-') { /* range */
3876 end_chunk
= simple_strtoul(buf
, &end
, 0);
3877 if (buf
== end
) break;
3879 if (*end
&& !isspace(*end
)) break;
3880 bitmap_dirty_bits(mddev
->bitmap
, chunk
, end_chunk
);
3881 buf
= skip_spaces(end
);
3883 bitmap_unplug(mddev
->bitmap
); /* flush the bits to disk */
3888 static struct md_sysfs_entry md_bitmap
=
3889 __ATTR(bitmap_set_bits
, S_IWUSR
, null_show
, bitmap_store
);
3892 size_show(mddev_t
*mddev
, char *page
)
3894 return sprintf(page
, "%llu\n",
3895 (unsigned long long)mddev
->dev_sectors
/ 2);
3898 static int update_size(mddev_t
*mddev
, sector_t num_sectors
);
3901 size_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3903 /* If array is inactive, we can reduce the component size, but
3904 * not increase it (except from 0).
3905 * If array is active, we can try an on-line resize
3908 int err
= strict_blocks_to_sectors(buf
, §ors
);
3913 err
= update_size(mddev
, sectors
);
3914 md_update_sb(mddev
, 1);
3916 if (mddev
->dev_sectors
== 0 ||
3917 mddev
->dev_sectors
> sectors
)
3918 mddev
->dev_sectors
= sectors
;
3922 return err
? err
: len
;
3925 static struct md_sysfs_entry md_size
=
3926 __ATTR(component_size
, S_IRUGO
|S_IWUSR
, size_show
, size_store
);
3931 * 'none' for arrays with no metadata (good luck...)
3932 * 'external' for arrays with externally managed metadata,
3933 * or N.M for internally known formats
3936 metadata_show(mddev_t
*mddev
, char *page
)
3938 if (mddev
->persistent
)
3939 return sprintf(page
, "%d.%d\n",
3940 mddev
->major_version
, mddev
->minor_version
);
3941 else if (mddev
->external
)
3942 return sprintf(page
, "external:%s\n", mddev
->metadata_type
);
3944 return sprintf(page
, "none\n");
3948 metadata_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3952 /* Changing the details of 'external' metadata is
3953 * always permitted. Otherwise there must be
3954 * no devices attached to the array.
3956 if (mddev
->external
&& strncmp(buf
, "external:", 9) == 0)
3958 else if (!list_empty(&mddev
->disks
))
3961 if (cmd_match(buf
, "none")) {
3962 mddev
->persistent
= 0;
3963 mddev
->external
= 0;
3964 mddev
->major_version
= 0;
3965 mddev
->minor_version
= 90;
3968 if (strncmp(buf
, "external:", 9) == 0) {
3969 size_t namelen
= len
-9;
3970 if (namelen
>= sizeof(mddev
->metadata_type
))
3971 namelen
= sizeof(mddev
->metadata_type
)-1;
3972 strncpy(mddev
->metadata_type
, buf
+9, namelen
);
3973 mddev
->metadata_type
[namelen
] = 0;
3974 if (namelen
&& mddev
->metadata_type
[namelen
-1] == '\n')
3975 mddev
->metadata_type
[--namelen
] = 0;
3976 mddev
->persistent
= 0;
3977 mddev
->external
= 1;
3978 mddev
->major_version
= 0;
3979 mddev
->minor_version
= 90;
3982 major
= simple_strtoul(buf
, &e
, 10);
3983 if (e
==buf
|| *e
!= '.')
3986 minor
= simple_strtoul(buf
, &e
, 10);
3987 if (e
==buf
|| (*e
&& *e
!= '\n') )
3989 if (major
>= ARRAY_SIZE(super_types
) || super_types
[major
].name
== NULL
)
3991 mddev
->major_version
= major
;
3992 mddev
->minor_version
= minor
;
3993 mddev
->persistent
= 1;
3994 mddev
->external
= 0;
3998 static struct md_sysfs_entry md_metadata
=
3999 __ATTR(metadata_version
, S_IRUGO
|S_IWUSR
, metadata_show
, metadata_store
);
4002 action_show(mddev_t
*mddev
, char *page
)
4004 char *type
= "idle";
4005 if (test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
))
4007 else if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
4008 (!mddev
->ro
&& test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))) {
4009 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
4011 else if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
4012 if (!test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
4014 else if (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
))
4018 } else if (test_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
))
4021 return sprintf(page
, "%s\n", type
);
4024 static void reap_sync_thread(mddev_t
*mddev
);
4027 action_store(mddev_t
*mddev
, const char *page
, size_t len
)
4029 if (!mddev
->pers
|| !mddev
->pers
->sync_request
)
4032 if (cmd_match(page
, "frozen"))
4033 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4035 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4037 if (cmd_match(page
, "idle") || cmd_match(page
, "frozen")) {
4038 if (mddev
->sync_thread
) {
4039 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
4040 reap_sync_thread(mddev
);
4042 } else if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
4043 test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))
4045 else if (cmd_match(page
, "resync"))
4046 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4047 else if (cmd_match(page
, "recover")) {
4048 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
4049 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4050 } else if (cmd_match(page
, "reshape")) {
4052 if (mddev
->pers
->start_reshape
== NULL
)
4054 err
= mddev
->pers
->start_reshape(mddev
);
4057 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
4059 if (cmd_match(page
, "check"))
4060 set_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
4061 else if (!cmd_match(page
, "repair"))
4063 set_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
4064 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
4066 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4067 md_wakeup_thread(mddev
->thread
);
4068 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
4073 mismatch_cnt_show(mddev_t
*mddev
, char *page
)
4075 return sprintf(page
, "%llu\n",
4076 (unsigned long long) mddev
->resync_mismatches
);
4079 static struct md_sysfs_entry md_scan_mode
=
4080 __ATTR(sync_action
, S_IRUGO
|S_IWUSR
, action_show
, action_store
);
4083 static struct md_sysfs_entry md_mismatches
= __ATTR_RO(mismatch_cnt
);
4086 sync_min_show(mddev_t
*mddev
, char *page
)
4088 return sprintf(page
, "%d (%s)\n", speed_min(mddev
),
4089 mddev
->sync_speed_min
? "local": "system");
4093 sync_min_store(mddev_t
*mddev
, const char *buf
, size_t len
)
4097 if (strncmp(buf
, "system", 6)==0) {
4098 mddev
->sync_speed_min
= 0;
4101 min
= simple_strtoul(buf
, &e
, 10);
4102 if (buf
== e
|| (*e
&& *e
!= '\n') || min
<= 0)
4104 mddev
->sync_speed_min
= min
;
4108 static struct md_sysfs_entry md_sync_min
=
4109 __ATTR(sync_speed_min
, S_IRUGO
|S_IWUSR
, sync_min_show
, sync_min_store
);
4112 sync_max_show(mddev_t
*mddev
, char *page
)
4114 return sprintf(page
, "%d (%s)\n", speed_max(mddev
),
4115 mddev
->sync_speed_max
? "local": "system");
4119 sync_max_store(mddev_t
*mddev
, const char *buf
, size_t len
)
4123 if (strncmp(buf
, "system", 6)==0) {
4124 mddev
->sync_speed_max
= 0;
4127 max
= simple_strtoul(buf
, &e
, 10);
4128 if (buf
== e
|| (*e
&& *e
!= '\n') || max
<= 0)
4130 mddev
->sync_speed_max
= max
;
4134 static struct md_sysfs_entry md_sync_max
=
4135 __ATTR(sync_speed_max
, S_IRUGO
|S_IWUSR
, sync_max_show
, sync_max_store
);
4138 degraded_show(mddev_t
*mddev
, char *page
)
4140 return sprintf(page
, "%d\n", mddev
->degraded
);
4142 static struct md_sysfs_entry md_degraded
= __ATTR_RO(degraded
);
4145 sync_force_parallel_show(mddev_t
*mddev
, char *page
)
4147 return sprintf(page
, "%d\n", mddev
->parallel_resync
);
4151 sync_force_parallel_store(mddev_t
*mddev
, const char *buf
, size_t len
)
4155 if (strict_strtol(buf
, 10, &n
))
4158 if (n
!= 0 && n
!= 1)
4161 mddev
->parallel_resync
= n
;
4163 if (mddev
->sync_thread
)
4164 wake_up(&resync_wait
);
4169 /* force parallel resync, even with shared block devices */
4170 static struct md_sysfs_entry md_sync_force_parallel
=
4171 __ATTR(sync_force_parallel
, S_IRUGO
|S_IWUSR
,
4172 sync_force_parallel_show
, sync_force_parallel_store
);
4175 sync_speed_show(mddev_t
*mddev
, char *page
)
4177 unsigned long resync
, dt
, db
;
4178 if (mddev
->curr_resync
== 0)
4179 return sprintf(page
, "none\n");
4180 resync
= mddev
->curr_mark_cnt
- atomic_read(&mddev
->recovery_active
);
4181 dt
= (jiffies
- mddev
->resync_mark
) / HZ
;
4183 db
= resync
- mddev
->resync_mark_cnt
;
4184 return sprintf(page
, "%lu\n", db
/dt
/2); /* K/sec */
4187 static struct md_sysfs_entry md_sync_speed
= __ATTR_RO(sync_speed
);
4190 sync_completed_show(mddev_t
*mddev
, char *page
)
4192 unsigned long long max_sectors
, resync
;
4194 if (!test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
4195 return sprintf(page
, "none\n");
4197 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
4198 max_sectors
= mddev
->resync_max_sectors
;
4200 max_sectors
= mddev
->dev_sectors
;
4202 resync
= mddev
->curr_resync_completed
;
4203 return sprintf(page
, "%llu / %llu\n", resync
, max_sectors
);
4206 static struct md_sysfs_entry md_sync_completed
= __ATTR_RO(sync_completed
);
4209 min_sync_show(mddev_t
*mddev
, char *page
)
4211 return sprintf(page
, "%llu\n",
4212 (unsigned long long)mddev
->resync_min
);
4215 min_sync_store(mddev_t
*mddev
, const char *buf
, size_t len
)
4217 unsigned long long min
;
4218 if (strict_strtoull(buf
, 10, &min
))
4220 if (min
> mddev
->resync_max
)
4222 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
4225 /* Must be a multiple of chunk_size */
4226 if (mddev
->chunk_sectors
) {
4227 sector_t temp
= min
;
4228 if (sector_div(temp
, mddev
->chunk_sectors
))
4231 mddev
->resync_min
= min
;
4236 static struct md_sysfs_entry md_min_sync
=
4237 __ATTR(sync_min
, S_IRUGO
|S_IWUSR
, min_sync_show
, min_sync_store
);
4240 max_sync_show(mddev_t
*mddev
, char *page
)
4242 if (mddev
->resync_max
== MaxSector
)
4243 return sprintf(page
, "max\n");
4245 return sprintf(page
, "%llu\n",
4246 (unsigned long long)mddev
->resync_max
);
4249 max_sync_store(mddev_t
*mddev
, const char *buf
, size_t len
)
4251 if (strncmp(buf
, "max", 3) == 0)
4252 mddev
->resync_max
= MaxSector
;
4254 unsigned long long max
;
4255 if (strict_strtoull(buf
, 10, &max
))
4257 if (max
< mddev
->resync_min
)
4259 if (max
< mddev
->resync_max
&&
4261 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
4264 /* Must be a multiple of chunk_size */
4265 if (mddev
->chunk_sectors
) {
4266 sector_t temp
= max
;
4267 if (sector_div(temp
, mddev
->chunk_sectors
))
4270 mddev
->resync_max
= max
;
4272 wake_up(&mddev
->recovery_wait
);
4276 static struct md_sysfs_entry md_max_sync
=
4277 __ATTR(sync_max
, S_IRUGO
|S_IWUSR
, max_sync_show
, max_sync_store
);
4280 suspend_lo_show(mddev_t
*mddev
, char *page
)
4282 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->suspend_lo
);
4286 suspend_lo_store(mddev_t
*mddev
, const char *buf
, size_t len
)
4289 unsigned long long new = simple_strtoull(buf
, &e
, 10);
4290 unsigned long long old
= mddev
->suspend_lo
;
4292 if (mddev
->pers
== NULL
||
4293 mddev
->pers
->quiesce
== NULL
)
4295 if (buf
== e
|| (*e
&& *e
!= '\n'))
4298 mddev
->suspend_lo
= new;
4300 /* Shrinking suspended region */
4301 mddev
->pers
->quiesce(mddev
, 2);
4303 /* Expanding suspended region - need to wait */
4304 mddev
->pers
->quiesce(mddev
, 1);
4305 mddev
->pers
->quiesce(mddev
, 0);
4309 static struct md_sysfs_entry md_suspend_lo
=
4310 __ATTR(suspend_lo
, S_IRUGO
|S_IWUSR
, suspend_lo_show
, suspend_lo_store
);
4314 suspend_hi_show(mddev_t
*mddev
, char *page
)
4316 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->suspend_hi
);
4320 suspend_hi_store(mddev_t
*mddev
, const char *buf
, size_t len
)
4323 unsigned long long new = simple_strtoull(buf
, &e
, 10);
4324 unsigned long long old
= mddev
->suspend_hi
;
4326 if (mddev
->pers
== NULL
||
4327 mddev
->pers
->quiesce
== NULL
)
4329 if (buf
== e
|| (*e
&& *e
!= '\n'))
4332 mddev
->suspend_hi
= new;
4334 /* Shrinking suspended region */
4335 mddev
->pers
->quiesce(mddev
, 2);
4337 /* Expanding suspended region - need to wait */
4338 mddev
->pers
->quiesce(mddev
, 1);
4339 mddev
->pers
->quiesce(mddev
, 0);
4343 static struct md_sysfs_entry md_suspend_hi
=
4344 __ATTR(suspend_hi
, S_IRUGO
|S_IWUSR
, suspend_hi_show
, suspend_hi_store
);
4347 reshape_position_show(mddev_t
*mddev
, char *page
)
4349 if (mddev
->reshape_position
!= MaxSector
)
4350 return sprintf(page
, "%llu\n",
4351 (unsigned long long)mddev
->reshape_position
);
4352 strcpy(page
, "none\n");
4357 reshape_position_store(mddev_t
*mddev
, const char *buf
, size_t len
)
4360 unsigned long long new = simple_strtoull(buf
, &e
, 10);
4363 if (buf
== e
|| (*e
&& *e
!= '\n'))
4365 mddev
->reshape_position
= new;
4366 mddev
->delta_disks
= 0;
4367 mddev
->new_level
= mddev
->level
;
4368 mddev
->new_layout
= mddev
->layout
;
4369 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
4373 static struct md_sysfs_entry md_reshape_position
=
4374 __ATTR(reshape_position
, S_IRUGO
|S_IWUSR
, reshape_position_show
,
4375 reshape_position_store
);
4378 array_size_show(mddev_t
*mddev
, char *page
)
4380 if (mddev
->external_size
)
4381 return sprintf(page
, "%llu\n",
4382 (unsigned long long)mddev
->array_sectors
/2);
4384 return sprintf(page
, "default\n");
4388 array_size_store(mddev_t
*mddev
, const char *buf
, size_t len
)
4392 if (strncmp(buf
, "default", 7) == 0) {
4394 sectors
= mddev
->pers
->size(mddev
, 0, 0);
4396 sectors
= mddev
->array_sectors
;
4398 mddev
->external_size
= 0;
4400 if (strict_blocks_to_sectors(buf
, §ors
) < 0)
4402 if (mddev
->pers
&& mddev
->pers
->size(mddev
, 0, 0) < sectors
)
4405 mddev
->external_size
= 1;
4408 mddev
->array_sectors
= sectors
;
4410 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
4411 revalidate_disk(mddev
->gendisk
);
4416 static struct md_sysfs_entry md_array_size
=
4417 __ATTR(array_size
, S_IRUGO
|S_IWUSR
, array_size_show
,
4420 static struct attribute
*md_default_attrs
[] = {
4423 &md_raid_disks
.attr
,
4424 &md_chunk_size
.attr
,
4426 &md_resync_start
.attr
,
4428 &md_new_device
.attr
,
4429 &md_safe_delay
.attr
,
4430 &md_array_state
.attr
,
4431 &md_reshape_position
.attr
,
4432 &md_array_size
.attr
,
4433 &max_corr_read_errors
.attr
,
4437 static struct attribute
*md_redundancy_attrs
[] = {
4439 &md_mismatches
.attr
,
4442 &md_sync_speed
.attr
,
4443 &md_sync_force_parallel
.attr
,
4444 &md_sync_completed
.attr
,
4447 &md_suspend_lo
.attr
,
4448 &md_suspend_hi
.attr
,
4453 static struct attribute_group md_redundancy_group
= {
4455 .attrs
= md_redundancy_attrs
,
4460 md_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
4462 struct md_sysfs_entry
*entry
= container_of(attr
, struct md_sysfs_entry
, attr
);
4463 mddev_t
*mddev
= container_of(kobj
, struct mddev_s
, kobj
);
4468 rv
= mddev_lock(mddev
);
4470 rv
= entry
->show(mddev
, page
);
4471 mddev_unlock(mddev
);
4477 md_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
4478 const char *page
, size_t length
)
4480 struct md_sysfs_entry
*entry
= container_of(attr
, struct md_sysfs_entry
, attr
);
4481 mddev_t
*mddev
= container_of(kobj
, struct mddev_s
, kobj
);
4486 if (!capable(CAP_SYS_ADMIN
))
4488 rv
= mddev_lock(mddev
);
4489 if (mddev
->hold_active
== UNTIL_IOCTL
)
4490 mddev
->hold_active
= 0;
4492 rv
= entry
->store(mddev
, page
, length
);
4493 mddev_unlock(mddev
);
4498 static void md_free(struct kobject
*ko
)
4500 mddev_t
*mddev
= container_of(ko
, mddev_t
, kobj
);
4502 if (mddev
->sysfs_state
)
4503 sysfs_put(mddev
->sysfs_state
);
4505 if (mddev
->gendisk
) {
4506 del_gendisk(mddev
->gendisk
);
4507 put_disk(mddev
->gendisk
);
4510 blk_cleanup_queue(mddev
->queue
);
4515 static const struct sysfs_ops md_sysfs_ops
= {
4516 .show
= md_attr_show
,
4517 .store
= md_attr_store
,
4519 static struct kobj_type md_ktype
= {
4521 .sysfs_ops
= &md_sysfs_ops
,
4522 .default_attrs
= md_default_attrs
,
4527 static void mddev_delayed_delete(struct work_struct
*ws
)
4529 mddev_t
*mddev
= container_of(ws
, mddev_t
, del_work
);
4531 sysfs_remove_group(&mddev
->kobj
, &md_bitmap_group
);
4532 kobject_del(&mddev
->kobj
);
4533 kobject_put(&mddev
->kobj
);
4536 static int md_alloc(dev_t dev
, char *name
)
4538 static DEFINE_MUTEX(disks_mutex
);
4539 mddev_t
*mddev
= mddev_find(dev
);
4540 struct gendisk
*disk
;
4549 partitioned
= (MAJOR(mddev
->unit
) != MD_MAJOR
);
4550 shift
= partitioned
? MdpMinorShift
: 0;
4551 unit
= MINOR(mddev
->unit
) >> shift
;
4553 /* wait for any previous instance of this device to be
4554 * completely removed (mddev_delayed_delete).
4556 flush_workqueue(md_misc_wq
);
4558 mutex_lock(&disks_mutex
);
4564 /* Need to ensure that 'name' is not a duplicate.
4567 spin_lock(&all_mddevs_lock
);
4569 list_for_each_entry(mddev2
, &all_mddevs
, all_mddevs
)
4570 if (mddev2
->gendisk
&&
4571 strcmp(mddev2
->gendisk
->disk_name
, name
) == 0) {
4572 spin_unlock(&all_mddevs_lock
);
4575 spin_unlock(&all_mddevs_lock
);
4579 mddev
->queue
= blk_alloc_queue(GFP_KERNEL
);
4582 mddev
->queue
->queuedata
= mddev
;
4584 blk_queue_make_request(mddev
->queue
, md_make_request
);
4586 disk
= alloc_disk(1 << shift
);
4588 blk_cleanup_queue(mddev
->queue
);
4589 mddev
->queue
= NULL
;
4592 disk
->major
= MAJOR(mddev
->unit
);
4593 disk
->first_minor
= unit
<< shift
;
4595 strcpy(disk
->disk_name
, name
);
4596 else if (partitioned
)
4597 sprintf(disk
->disk_name
, "md_d%d", unit
);
4599 sprintf(disk
->disk_name
, "md%d", unit
);
4600 disk
->fops
= &md_fops
;
4601 disk
->private_data
= mddev
;
4602 disk
->queue
= mddev
->queue
;
4603 blk_queue_flush(mddev
->queue
, REQ_FLUSH
| REQ_FUA
);
4604 /* Allow extended partitions. This makes the
4605 * 'mdp' device redundant, but we can't really
4608 disk
->flags
|= GENHD_FL_EXT_DEVT
;
4609 mddev
->gendisk
= disk
;
4610 /* As soon as we call add_disk(), another thread could get
4611 * through to md_open, so make sure it doesn't get too far
4613 mutex_lock(&mddev
->open_mutex
);
4616 error
= kobject_init_and_add(&mddev
->kobj
, &md_ktype
,
4617 &disk_to_dev(disk
)->kobj
, "%s", "md");
4619 /* This isn't possible, but as kobject_init_and_add is marked
4620 * __must_check, we must do something with the result
4622 printk(KERN_WARNING
"md: cannot register %s/md - name in use\n",
4626 if (mddev
->kobj
.sd
&&
4627 sysfs_create_group(&mddev
->kobj
, &md_bitmap_group
))
4628 printk(KERN_DEBUG
"pointless warning\n");
4629 mutex_unlock(&mddev
->open_mutex
);
4631 mutex_unlock(&disks_mutex
);
4632 if (!error
&& mddev
->kobj
.sd
) {
4633 kobject_uevent(&mddev
->kobj
, KOBJ_ADD
);
4634 mddev
->sysfs_state
= sysfs_get_dirent_safe(mddev
->kobj
.sd
, "array_state");
4640 static struct kobject
*md_probe(dev_t dev
, int *part
, void *data
)
4642 md_alloc(dev
, NULL
);
4646 static int add_named_array(const char *val
, struct kernel_param
*kp
)
4648 /* val must be "md_*" where * is not all digits.
4649 * We allocate an array with a large free minor number, and
4650 * set the name to val. val must not already be an active name.
4652 int len
= strlen(val
);
4653 char buf
[DISK_NAME_LEN
];
4655 while (len
&& val
[len
-1] == '\n')
4657 if (len
>= DISK_NAME_LEN
)
4659 strlcpy(buf
, val
, len
+1);
4660 if (strncmp(buf
, "md_", 3) != 0)
4662 return md_alloc(0, buf
);
4665 static void md_safemode_timeout(unsigned long data
)
4667 mddev_t
*mddev
= (mddev_t
*) data
;
4669 if (!atomic_read(&mddev
->writes_pending
)) {
4670 mddev
->safemode
= 1;
4671 if (mddev
->external
)
4672 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4674 md_wakeup_thread(mddev
->thread
);
4677 static int start_dirty_degraded
;
4679 int md_run(mddev_t
*mddev
)
4683 struct mdk_personality
*pers
;
4685 if (list_empty(&mddev
->disks
))
4686 /* cannot run an array with no devices.. */
4691 /* Cannot run until previous stop completes properly */
4692 if (mddev
->sysfs_active
)
4696 * Analyze all RAID superblock(s)
4698 if (!mddev
->raid_disks
) {
4699 if (!mddev
->persistent
)
4704 if (mddev
->level
!= LEVEL_NONE
)
4705 request_module("md-level-%d", mddev
->level
);
4706 else if (mddev
->clevel
[0])
4707 request_module("md-%s", mddev
->clevel
);
4710 * Drop all container device buffers, from now on
4711 * the only valid external interface is through the md
4714 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
4715 if (test_bit(Faulty
, &rdev
->flags
))
4717 sync_blockdev(rdev
->bdev
);
4718 invalidate_bdev(rdev
->bdev
);
4720 /* perform some consistency tests on the device.
4721 * We don't want the data to overlap the metadata,
4722 * Internal Bitmap issues have been handled elsewhere.
4724 if (rdev
->meta_bdev
) {
4725 /* Nothing to check */;
4726 } else if (rdev
->data_offset
< rdev
->sb_start
) {
4727 if (mddev
->dev_sectors
&&
4728 rdev
->data_offset
+ mddev
->dev_sectors
4730 printk("md: %s: data overlaps metadata\n",
4735 if (rdev
->sb_start
+ rdev
->sb_size
/512
4736 > rdev
->data_offset
) {
4737 printk("md: %s: metadata overlaps data\n",
4742 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
4745 if (mddev
->bio_set
== NULL
)
4746 mddev
->bio_set
= bioset_create(BIO_POOL_SIZE
,
4749 spin_lock(&pers_lock
);
4750 pers
= find_pers(mddev
->level
, mddev
->clevel
);
4751 if (!pers
|| !try_module_get(pers
->owner
)) {
4752 spin_unlock(&pers_lock
);
4753 if (mddev
->level
!= LEVEL_NONE
)
4754 printk(KERN_WARNING
"md: personality for level %d is not loaded!\n",
4757 printk(KERN_WARNING
"md: personality for level %s is not loaded!\n",
4762 spin_unlock(&pers_lock
);
4763 if (mddev
->level
!= pers
->level
) {
4764 mddev
->level
= pers
->level
;
4765 mddev
->new_level
= pers
->level
;
4767 strlcpy(mddev
->clevel
, pers
->name
, sizeof(mddev
->clevel
));
4769 if (mddev
->reshape_position
!= MaxSector
&&
4770 pers
->start_reshape
== NULL
) {
4771 /* This personality cannot handle reshaping... */
4773 module_put(pers
->owner
);
4777 if (pers
->sync_request
) {
4778 /* Warn if this is a potentially silly
4781 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
4785 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
4786 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
) {
4788 rdev
->bdev
->bd_contains
==
4789 rdev2
->bdev
->bd_contains
) {
4791 "%s: WARNING: %s appears to be"
4792 " on the same physical disk as"
4795 bdevname(rdev
->bdev
,b
),
4796 bdevname(rdev2
->bdev
,b2
));
4803 "True protection against single-disk"
4804 " failure might be compromised.\n");
4807 mddev
->recovery
= 0;
4808 /* may be over-ridden by personality */
4809 mddev
->resync_max_sectors
= mddev
->dev_sectors
;
4811 mddev
->ok_start_degraded
= start_dirty_degraded
;
4813 if (start_readonly
&& mddev
->ro
== 0)
4814 mddev
->ro
= 2; /* read-only, but switch on first write */
4816 err
= mddev
->pers
->run(mddev
);
4818 printk(KERN_ERR
"md: pers->run() failed ...\n");
4819 else if (mddev
->pers
->size(mddev
, 0, 0) < mddev
->array_sectors
) {
4820 WARN_ONCE(!mddev
->external_size
, "%s: default size too small,"
4821 " but 'external_size' not in effect?\n", __func__
);
4823 "md: invalid array_size %llu > default size %llu\n",
4824 (unsigned long long)mddev
->array_sectors
/ 2,
4825 (unsigned long long)mddev
->pers
->size(mddev
, 0, 0) / 2);
4827 mddev
->pers
->stop(mddev
);
4829 if (err
== 0 && mddev
->pers
->sync_request
) {
4830 err
= bitmap_create(mddev
);
4832 printk(KERN_ERR
"%s: failed to create bitmap (%d)\n",
4833 mdname(mddev
), err
);
4834 mddev
->pers
->stop(mddev
);
4838 module_put(mddev
->pers
->owner
);
4840 bitmap_destroy(mddev
);
4843 if (mddev
->pers
->sync_request
) {
4844 if (mddev
->kobj
.sd
&&
4845 sysfs_create_group(&mddev
->kobj
, &md_redundancy_group
))
4847 "md: cannot register extra attributes for %s\n",
4849 mddev
->sysfs_action
= sysfs_get_dirent_safe(mddev
->kobj
.sd
, "sync_action");
4850 } else if (mddev
->ro
== 2) /* auto-readonly not meaningful */
4853 atomic_set(&mddev
->writes_pending
,0);
4854 atomic_set(&mddev
->max_corr_read_errors
,
4855 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS
);
4856 mddev
->safemode
= 0;
4857 mddev
->safemode_timer
.function
= md_safemode_timeout
;
4858 mddev
->safemode_timer
.data
= (unsigned long) mddev
;
4859 mddev
->safemode_delay
= (200 * HZ
)/1000 +1; /* 200 msec delay */
4863 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
4864 if (rdev
->raid_disk
>= 0)
4865 if (sysfs_link_rdev(mddev
, rdev
))
4866 /* failure here is OK */;
4868 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4871 md_update_sb(mddev
, 0);
4873 md_new_event(mddev
);
4874 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4875 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
4876 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
4879 EXPORT_SYMBOL_GPL(md_run
);
4881 static int do_md_run(mddev_t
*mddev
)
4885 err
= md_run(mddev
);
4888 err
= bitmap_load(mddev
);
4890 bitmap_destroy(mddev
);
4894 md_wakeup_thread(mddev
->thread
);
4895 md_wakeup_thread(mddev
->sync_thread
); /* possibly kick off a reshape */
4897 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
4898 revalidate_disk(mddev
->gendisk
);
4900 kobject_uevent(&disk_to_dev(mddev
->gendisk
)->kobj
, KOBJ_CHANGE
);
4905 static int restart_array(mddev_t
*mddev
)
4907 struct gendisk
*disk
= mddev
->gendisk
;
4909 /* Complain if it has no devices */
4910 if (list_empty(&mddev
->disks
))
4916 mddev
->safemode
= 0;
4918 set_disk_ro(disk
, 0);
4919 printk(KERN_INFO
"md: %s switched to read-write mode.\n",
4921 /* Kick recovery or resync if necessary */
4922 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4923 md_wakeup_thread(mddev
->thread
);
4924 md_wakeup_thread(mddev
->sync_thread
);
4925 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4929 /* similar to deny_write_access, but accounts for our holding a reference
4930 * to the file ourselves */
4931 static int deny_bitmap_write_access(struct file
* file
)
4933 struct inode
*inode
= file
->f_mapping
->host
;
4935 spin_lock(&inode
->i_lock
);
4936 if (atomic_read(&inode
->i_writecount
) > 1) {
4937 spin_unlock(&inode
->i_lock
);
4940 atomic_set(&inode
->i_writecount
, -1);
4941 spin_unlock(&inode
->i_lock
);
4946 void restore_bitmap_write_access(struct file
*file
)
4948 struct inode
*inode
= file
->f_mapping
->host
;
4950 spin_lock(&inode
->i_lock
);
4951 atomic_set(&inode
->i_writecount
, 1);
4952 spin_unlock(&inode
->i_lock
);
4955 static void md_clean(mddev_t
*mddev
)
4957 mddev
->array_sectors
= 0;
4958 mddev
->external_size
= 0;
4959 mddev
->dev_sectors
= 0;
4960 mddev
->raid_disks
= 0;
4961 mddev
->recovery_cp
= 0;
4962 mddev
->resync_min
= 0;
4963 mddev
->resync_max
= MaxSector
;
4964 mddev
->reshape_position
= MaxSector
;
4965 mddev
->external
= 0;
4966 mddev
->persistent
= 0;
4967 mddev
->level
= LEVEL_NONE
;
4968 mddev
->clevel
[0] = 0;
4971 mddev
->metadata_type
[0] = 0;
4972 mddev
->chunk_sectors
= 0;
4973 mddev
->ctime
= mddev
->utime
= 0;
4975 mddev
->max_disks
= 0;
4977 mddev
->can_decrease_events
= 0;
4978 mddev
->delta_disks
= 0;
4979 mddev
->new_level
= LEVEL_NONE
;
4980 mddev
->new_layout
= 0;
4981 mddev
->new_chunk_sectors
= 0;
4982 mddev
->curr_resync
= 0;
4983 mddev
->resync_mismatches
= 0;
4984 mddev
->suspend_lo
= mddev
->suspend_hi
= 0;
4985 mddev
->sync_speed_min
= mddev
->sync_speed_max
= 0;
4986 mddev
->recovery
= 0;
4989 mddev
->degraded
= 0;
4990 mddev
->safemode
= 0;
4991 mddev
->bitmap_info
.offset
= 0;
4992 mddev
->bitmap_info
.default_offset
= 0;
4993 mddev
->bitmap_info
.chunksize
= 0;
4994 mddev
->bitmap_info
.daemon_sleep
= 0;
4995 mddev
->bitmap_info
.max_write_behind
= 0;
4998 static void __md_stop_writes(mddev_t
*mddev
)
5000 if (mddev
->sync_thread
) {
5001 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
5002 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
5003 reap_sync_thread(mddev
);
5006 del_timer_sync(&mddev
->safemode_timer
);
5008 bitmap_flush(mddev
);
5009 md_super_wait(mddev
);
5011 if (!mddev
->in_sync
|| mddev
->flags
) {
5012 /* mark array as shutdown cleanly */
5014 md_update_sb(mddev
, 1);
5018 void md_stop_writes(mddev_t
*mddev
)
5021 __md_stop_writes(mddev
);
5022 mddev_unlock(mddev
);
5024 EXPORT_SYMBOL_GPL(md_stop_writes
);
5026 void md_stop(mddev_t
*mddev
)
5029 mddev
->pers
->stop(mddev
);
5030 if (mddev
->pers
->sync_request
&& mddev
->to_remove
== NULL
)
5031 mddev
->to_remove
= &md_redundancy_group
;
5032 module_put(mddev
->pers
->owner
);
5034 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
5036 EXPORT_SYMBOL_GPL(md_stop
);
5038 static int md_set_readonly(mddev_t
*mddev
, int is_open
)
5041 mutex_lock(&mddev
->open_mutex
);
5042 if (atomic_read(&mddev
->openers
) > is_open
) {
5043 printk("md: %s still in use.\n",mdname(mddev
));
5048 __md_stop_writes(mddev
);
5054 set_disk_ro(mddev
->gendisk
, 1);
5055 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
5056 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
5060 mutex_unlock(&mddev
->open_mutex
);
5065 * 0 - completely stop and dis-assemble array
5066 * 2 - stop but do not disassemble array
5068 static int do_md_stop(mddev_t
* mddev
, int mode
, int is_open
)
5070 struct gendisk
*disk
= mddev
->gendisk
;
5073 mutex_lock(&mddev
->open_mutex
);
5074 if (atomic_read(&mddev
->openers
) > is_open
||
5075 mddev
->sysfs_active
) {
5076 printk("md: %s still in use.\n",mdname(mddev
));
5077 mutex_unlock(&mddev
->open_mutex
);
5083 set_disk_ro(disk
, 0);
5085 __md_stop_writes(mddev
);
5087 mddev
->queue
->merge_bvec_fn
= NULL
;
5088 mddev
->queue
->backing_dev_info
.congested_fn
= NULL
;
5090 /* tell userspace to handle 'inactive' */
5091 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
5093 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
5094 if (rdev
->raid_disk
>= 0)
5095 sysfs_unlink_rdev(mddev
, rdev
);
5097 set_capacity(disk
, 0);
5098 mutex_unlock(&mddev
->open_mutex
);
5100 revalidate_disk(disk
);
5105 mutex_unlock(&mddev
->open_mutex
);
5107 * Free resources if final stop
5110 printk(KERN_INFO
"md: %s stopped.\n", mdname(mddev
));
5112 bitmap_destroy(mddev
);
5113 if (mddev
->bitmap_info
.file
) {
5114 restore_bitmap_write_access(mddev
->bitmap_info
.file
);
5115 fput(mddev
->bitmap_info
.file
);
5116 mddev
->bitmap_info
.file
= NULL
;
5118 mddev
->bitmap_info
.offset
= 0;
5120 export_array(mddev
);
5123 kobject_uevent(&disk_to_dev(mddev
->gendisk
)->kobj
, KOBJ_CHANGE
);
5124 if (mddev
->hold_active
== UNTIL_STOP
)
5125 mddev
->hold_active
= 0;
5127 blk_integrity_unregister(disk
);
5128 md_new_event(mddev
);
5129 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
5134 static void autorun_array(mddev_t
*mddev
)
5139 if (list_empty(&mddev
->disks
))
5142 printk(KERN_INFO
"md: running: ");
5144 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
5145 char b
[BDEVNAME_SIZE
];
5146 printk("<%s>", bdevname(rdev
->bdev
,b
));
5150 err
= do_md_run(mddev
);
5152 printk(KERN_WARNING
"md: do_md_run() returned %d\n", err
);
5153 do_md_stop(mddev
, 0, 0);
5158 * lets try to run arrays based on all disks that have arrived
5159 * until now. (those are in pending_raid_disks)
5161 * the method: pick the first pending disk, collect all disks with
5162 * the same UUID, remove all from the pending list and put them into
5163 * the 'same_array' list. Then order this list based on superblock
5164 * update time (freshest comes first), kick out 'old' disks and
5165 * compare superblocks. If everything's fine then run it.
5167 * If "unit" is allocated, then bump its reference count
5169 static void autorun_devices(int part
)
5171 mdk_rdev_t
*rdev0
, *rdev
, *tmp
;
5173 char b
[BDEVNAME_SIZE
];
5175 printk(KERN_INFO
"md: autorun ...\n");
5176 while (!list_empty(&pending_raid_disks
)) {
5179 LIST_HEAD(candidates
);
5180 rdev0
= list_entry(pending_raid_disks
.next
,
5181 mdk_rdev_t
, same_set
);
5183 printk(KERN_INFO
"md: considering %s ...\n",
5184 bdevname(rdev0
->bdev
,b
));
5185 INIT_LIST_HEAD(&candidates
);
5186 rdev_for_each_list(rdev
, tmp
, &pending_raid_disks
)
5187 if (super_90_load(rdev
, rdev0
, 0) >= 0) {
5188 printk(KERN_INFO
"md: adding %s ...\n",
5189 bdevname(rdev
->bdev
,b
));
5190 list_move(&rdev
->same_set
, &candidates
);
5193 * now we have a set of devices, with all of them having
5194 * mostly sane superblocks. It's time to allocate the
5198 dev
= MKDEV(mdp_major
,
5199 rdev0
->preferred_minor
<< MdpMinorShift
);
5200 unit
= MINOR(dev
) >> MdpMinorShift
;
5202 dev
= MKDEV(MD_MAJOR
, rdev0
->preferred_minor
);
5205 if (rdev0
->preferred_minor
!= unit
) {
5206 printk(KERN_INFO
"md: unit number in %s is bad: %d\n",
5207 bdevname(rdev0
->bdev
, b
), rdev0
->preferred_minor
);
5211 md_probe(dev
, NULL
, NULL
);
5212 mddev
= mddev_find(dev
);
5213 if (!mddev
|| !mddev
->gendisk
) {
5217 "md: cannot allocate memory for md drive.\n");
5220 if (mddev_lock(mddev
))
5221 printk(KERN_WARNING
"md: %s locked, cannot run\n",
5223 else if (mddev
->raid_disks
|| mddev
->major_version
5224 || !list_empty(&mddev
->disks
)) {
5226 "md: %s already running, cannot run %s\n",
5227 mdname(mddev
), bdevname(rdev0
->bdev
,b
));
5228 mddev_unlock(mddev
);
5230 printk(KERN_INFO
"md: created %s\n", mdname(mddev
));
5231 mddev
->persistent
= 1;
5232 rdev_for_each_list(rdev
, tmp
, &candidates
) {
5233 list_del_init(&rdev
->same_set
);
5234 if (bind_rdev_to_array(rdev
, mddev
))
5237 autorun_array(mddev
);
5238 mddev_unlock(mddev
);
5240 /* on success, candidates will be empty, on error
5243 rdev_for_each_list(rdev
, tmp
, &candidates
) {
5244 list_del_init(&rdev
->same_set
);
5249 printk(KERN_INFO
"md: ... autorun DONE.\n");
5251 #endif /* !MODULE */
5253 static int get_version(void __user
* arg
)
5257 ver
.major
= MD_MAJOR_VERSION
;
5258 ver
.minor
= MD_MINOR_VERSION
;
5259 ver
.patchlevel
= MD_PATCHLEVEL_VERSION
;
5261 if (copy_to_user(arg
, &ver
, sizeof(ver
)))
5267 static int get_array_info(mddev_t
* mddev
, void __user
* arg
)
5269 mdu_array_info_t info
;
5270 int nr
,working
,insync
,failed
,spare
;
5273 nr
=working
=insync
=failed
=spare
=0;
5274 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
5276 if (test_bit(Faulty
, &rdev
->flags
))
5280 if (test_bit(In_sync
, &rdev
->flags
))
5287 info
.major_version
= mddev
->major_version
;
5288 info
.minor_version
= mddev
->minor_version
;
5289 info
.patch_version
= MD_PATCHLEVEL_VERSION
;
5290 info
.ctime
= mddev
->ctime
;
5291 info
.level
= mddev
->level
;
5292 info
.size
= mddev
->dev_sectors
/ 2;
5293 if (info
.size
!= mddev
->dev_sectors
/ 2) /* overflow */
5296 info
.raid_disks
= mddev
->raid_disks
;
5297 info
.md_minor
= mddev
->md_minor
;
5298 info
.not_persistent
= !mddev
->persistent
;
5300 info
.utime
= mddev
->utime
;
5303 info
.state
= (1<<MD_SB_CLEAN
);
5304 if (mddev
->bitmap
&& mddev
->bitmap_info
.offset
)
5305 info
.state
= (1<<MD_SB_BITMAP_PRESENT
);
5306 info
.active_disks
= insync
;
5307 info
.working_disks
= working
;
5308 info
.failed_disks
= failed
;
5309 info
.spare_disks
= spare
;
5311 info
.layout
= mddev
->layout
;
5312 info
.chunk_size
= mddev
->chunk_sectors
<< 9;
5314 if (copy_to_user(arg
, &info
, sizeof(info
)))
5320 static int get_bitmap_file(mddev_t
* mddev
, void __user
* arg
)
5322 mdu_bitmap_file_t
*file
= NULL
; /* too big for stack allocation */
5323 char *ptr
, *buf
= NULL
;
5326 if (md_allow_write(mddev
))
5327 file
= kmalloc(sizeof(*file
), GFP_NOIO
);
5329 file
= kmalloc(sizeof(*file
), GFP_KERNEL
);
5334 /* bitmap disabled, zero the first byte and copy out */
5335 if (!mddev
->bitmap
|| !mddev
->bitmap
->file
) {
5336 file
->pathname
[0] = '\0';
5340 buf
= kmalloc(sizeof(file
->pathname
), GFP_KERNEL
);
5344 ptr
= d_path(&mddev
->bitmap
->file
->f_path
, buf
, sizeof(file
->pathname
));
5348 strcpy(file
->pathname
, ptr
);
5352 if (copy_to_user(arg
, file
, sizeof(*file
)))
5360 static int get_disk_info(mddev_t
* mddev
, void __user
* arg
)
5362 mdu_disk_info_t info
;
5365 if (copy_from_user(&info
, arg
, sizeof(info
)))
5368 rdev
= find_rdev_nr(mddev
, info
.number
);
5370 info
.major
= MAJOR(rdev
->bdev
->bd_dev
);
5371 info
.minor
= MINOR(rdev
->bdev
->bd_dev
);
5372 info
.raid_disk
= rdev
->raid_disk
;
5374 if (test_bit(Faulty
, &rdev
->flags
))
5375 info
.state
|= (1<<MD_DISK_FAULTY
);
5376 else if (test_bit(In_sync
, &rdev
->flags
)) {
5377 info
.state
|= (1<<MD_DISK_ACTIVE
);
5378 info
.state
|= (1<<MD_DISK_SYNC
);
5380 if (test_bit(WriteMostly
, &rdev
->flags
))
5381 info
.state
|= (1<<MD_DISK_WRITEMOSTLY
);
5383 info
.major
= info
.minor
= 0;
5384 info
.raid_disk
= -1;
5385 info
.state
= (1<<MD_DISK_REMOVED
);
5388 if (copy_to_user(arg
, &info
, sizeof(info
)))
5394 static int add_new_disk(mddev_t
* mddev
, mdu_disk_info_t
*info
)
5396 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
5398 dev_t dev
= MKDEV(info
->major
,info
->minor
);
5400 if (info
->major
!= MAJOR(dev
) || info
->minor
!= MINOR(dev
))
5403 if (!mddev
->raid_disks
) {
5405 /* expecting a device which has a superblock */
5406 rdev
= md_import_device(dev
, mddev
->major_version
, mddev
->minor_version
);
5409 "md: md_import_device returned %ld\n",
5411 return PTR_ERR(rdev
);
5413 if (!list_empty(&mddev
->disks
)) {
5414 mdk_rdev_t
*rdev0
= list_entry(mddev
->disks
.next
,
5415 mdk_rdev_t
, same_set
);
5416 err
= super_types
[mddev
->major_version
]
5417 .load_super(rdev
, rdev0
, mddev
->minor_version
);
5420 "md: %s has different UUID to %s\n",
5421 bdevname(rdev
->bdev
,b
),
5422 bdevname(rdev0
->bdev
,b2
));
5427 err
= bind_rdev_to_array(rdev
, mddev
);
5434 * add_new_disk can be used once the array is assembled
5435 * to add "hot spares". They must already have a superblock
5440 if (!mddev
->pers
->hot_add_disk
) {
5442 "%s: personality does not support diskops!\n",
5446 if (mddev
->persistent
)
5447 rdev
= md_import_device(dev
, mddev
->major_version
,
5448 mddev
->minor_version
);
5450 rdev
= md_import_device(dev
, -1, -1);
5453 "md: md_import_device returned %ld\n",
5455 return PTR_ERR(rdev
);
5457 /* set saved_raid_disk if appropriate */
5458 if (!mddev
->persistent
) {
5459 if (info
->state
& (1<<MD_DISK_SYNC
) &&
5460 info
->raid_disk
< mddev
->raid_disks
) {
5461 rdev
->raid_disk
= info
->raid_disk
;
5462 set_bit(In_sync
, &rdev
->flags
);
5464 rdev
->raid_disk
= -1;
5466 super_types
[mddev
->major_version
].
5467 validate_super(mddev
, rdev
);
5468 if ((info
->state
& (1<<MD_DISK_SYNC
)) &&
5469 (!test_bit(In_sync
, &rdev
->flags
) ||
5470 rdev
->raid_disk
!= info
->raid_disk
)) {
5471 /* This was a hot-add request, but events doesn't
5472 * match, so reject it.
5478 if (test_bit(In_sync
, &rdev
->flags
))
5479 rdev
->saved_raid_disk
= rdev
->raid_disk
;
5481 rdev
->saved_raid_disk
= -1;
5483 clear_bit(In_sync
, &rdev
->flags
); /* just to be sure */
5484 if (info
->state
& (1<<MD_DISK_WRITEMOSTLY
))
5485 set_bit(WriteMostly
, &rdev
->flags
);
5487 clear_bit(WriteMostly
, &rdev
->flags
);
5489 rdev
->raid_disk
= -1;
5490 err
= bind_rdev_to_array(rdev
, mddev
);
5491 if (!err
&& !mddev
->pers
->hot_remove_disk
) {
5492 /* If there is hot_add_disk but no hot_remove_disk
5493 * then added disks for geometry changes,
5494 * and should be added immediately.
5496 super_types
[mddev
->major_version
].
5497 validate_super(mddev
, rdev
);
5498 err
= mddev
->pers
->hot_add_disk(mddev
, rdev
);
5500 unbind_rdev_from_array(rdev
);
5505 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
5507 md_update_sb(mddev
, 1);
5508 if (mddev
->degraded
)
5509 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
5510 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5512 md_new_event(mddev
);
5513 md_wakeup_thread(mddev
->thread
);
5517 /* otherwise, add_new_disk is only allowed
5518 * for major_version==0 superblocks
5520 if (mddev
->major_version
!= 0) {
5521 printk(KERN_WARNING
"%s: ADD_NEW_DISK not supported\n",
5526 if (!(info
->state
& (1<<MD_DISK_FAULTY
))) {
5528 rdev
= md_import_device(dev
, -1, 0);
5531 "md: error, md_import_device() returned %ld\n",
5533 return PTR_ERR(rdev
);
5535 rdev
->desc_nr
= info
->number
;
5536 if (info
->raid_disk
< mddev
->raid_disks
)
5537 rdev
->raid_disk
= info
->raid_disk
;
5539 rdev
->raid_disk
= -1;
5541 if (rdev
->raid_disk
< mddev
->raid_disks
)
5542 if (info
->state
& (1<<MD_DISK_SYNC
))
5543 set_bit(In_sync
, &rdev
->flags
);
5545 if (info
->state
& (1<<MD_DISK_WRITEMOSTLY
))
5546 set_bit(WriteMostly
, &rdev
->flags
);
5548 if (!mddev
->persistent
) {
5549 printk(KERN_INFO
"md: nonpersistent superblock ...\n");
5550 rdev
->sb_start
= i_size_read(rdev
->bdev
->bd_inode
) / 512;
5552 rdev
->sb_start
= calc_dev_sboffset(rdev
);
5553 rdev
->sectors
= rdev
->sb_start
;
5555 err
= bind_rdev_to_array(rdev
, mddev
);
5565 static int hot_remove_disk(mddev_t
* mddev
, dev_t dev
)
5567 char b
[BDEVNAME_SIZE
];
5570 rdev
= find_rdev(mddev
, dev
);
5574 if (rdev
->raid_disk
>= 0)
5577 kick_rdev_from_array(rdev
);
5578 md_update_sb(mddev
, 1);
5579 md_new_event(mddev
);
5583 printk(KERN_WARNING
"md: cannot remove active disk %s from %s ...\n",
5584 bdevname(rdev
->bdev
,b
), mdname(mddev
));
5588 static int hot_add_disk(mddev_t
* mddev
, dev_t dev
)
5590 char b
[BDEVNAME_SIZE
];
5597 if (mddev
->major_version
!= 0) {
5598 printk(KERN_WARNING
"%s: HOT_ADD may only be used with"
5599 " version-0 superblocks.\n",
5603 if (!mddev
->pers
->hot_add_disk
) {
5605 "%s: personality does not support diskops!\n",
5610 rdev
= md_import_device(dev
, -1, 0);
5613 "md: error, md_import_device() returned %ld\n",
5618 if (mddev
->persistent
)
5619 rdev
->sb_start
= calc_dev_sboffset(rdev
);
5621 rdev
->sb_start
= i_size_read(rdev
->bdev
->bd_inode
) / 512;
5623 rdev
->sectors
= rdev
->sb_start
;
5625 if (test_bit(Faulty
, &rdev
->flags
)) {
5627 "md: can not hot-add faulty %s disk to %s!\n",
5628 bdevname(rdev
->bdev
,b
), mdname(mddev
));
5632 clear_bit(In_sync
, &rdev
->flags
);
5634 rdev
->saved_raid_disk
= -1;
5635 err
= bind_rdev_to_array(rdev
, mddev
);
5640 * The rest should better be atomic, we can have disk failures
5641 * noticed in interrupt contexts ...
5644 rdev
->raid_disk
= -1;
5646 md_update_sb(mddev
, 1);
5649 * Kick recovery, maybe this spare has to be added to the
5650 * array immediately.
5652 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5653 md_wakeup_thread(mddev
->thread
);
5654 md_new_event(mddev
);
5662 static int set_bitmap_file(mddev_t
*mddev
, int fd
)
5667 if (!mddev
->pers
->quiesce
)
5669 if (mddev
->recovery
|| mddev
->sync_thread
)
5671 /* we should be able to change the bitmap.. */
5677 return -EEXIST
; /* cannot add when bitmap is present */
5678 mddev
->bitmap_info
.file
= fget(fd
);
5680 if (mddev
->bitmap_info
.file
== NULL
) {
5681 printk(KERN_ERR
"%s: error: failed to get bitmap file\n",
5686 err
= deny_bitmap_write_access(mddev
->bitmap_info
.file
);
5688 printk(KERN_ERR
"%s: error: bitmap file is already in use\n",
5690 fput(mddev
->bitmap_info
.file
);
5691 mddev
->bitmap_info
.file
= NULL
;
5694 mddev
->bitmap_info
.offset
= 0; /* file overrides offset */
5695 } else if (mddev
->bitmap
== NULL
)
5696 return -ENOENT
; /* cannot remove what isn't there */
5699 mddev
->pers
->quiesce(mddev
, 1);
5701 err
= bitmap_create(mddev
);
5703 err
= bitmap_load(mddev
);
5705 if (fd
< 0 || err
) {
5706 bitmap_destroy(mddev
);
5707 fd
= -1; /* make sure to put the file */
5709 mddev
->pers
->quiesce(mddev
, 0);
5712 if (mddev
->bitmap_info
.file
) {
5713 restore_bitmap_write_access(mddev
->bitmap_info
.file
);
5714 fput(mddev
->bitmap_info
.file
);
5716 mddev
->bitmap_info
.file
= NULL
;
5723 * set_array_info is used two different ways
5724 * The original usage is when creating a new array.
5725 * In this usage, raid_disks is > 0 and it together with
5726 * level, size, not_persistent,layout,chunksize determine the
5727 * shape of the array.
5728 * This will always create an array with a type-0.90.0 superblock.
5729 * The newer usage is when assembling an array.
5730 * In this case raid_disks will be 0, and the major_version field is
5731 * use to determine which style super-blocks are to be found on the devices.
5732 * The minor and patch _version numbers are also kept incase the
5733 * super_block handler wishes to interpret them.
5735 static int set_array_info(mddev_t
* mddev
, mdu_array_info_t
*info
)
5738 if (info
->raid_disks
== 0) {
5739 /* just setting version number for superblock loading */
5740 if (info
->major_version
< 0 ||
5741 info
->major_version
>= ARRAY_SIZE(super_types
) ||
5742 super_types
[info
->major_version
].name
== NULL
) {
5743 /* maybe try to auto-load a module? */
5745 "md: superblock version %d not known\n",
5746 info
->major_version
);
5749 mddev
->major_version
= info
->major_version
;
5750 mddev
->minor_version
= info
->minor_version
;
5751 mddev
->patch_version
= info
->patch_version
;
5752 mddev
->persistent
= !info
->not_persistent
;
5753 /* ensure mddev_put doesn't delete this now that there
5754 * is some minimal configuration.
5756 mddev
->ctime
= get_seconds();
5759 mddev
->major_version
= MD_MAJOR_VERSION
;
5760 mddev
->minor_version
= MD_MINOR_VERSION
;
5761 mddev
->patch_version
= MD_PATCHLEVEL_VERSION
;
5762 mddev
->ctime
= get_seconds();
5764 mddev
->level
= info
->level
;
5765 mddev
->clevel
[0] = 0;
5766 mddev
->dev_sectors
= 2 * (sector_t
)info
->size
;
5767 mddev
->raid_disks
= info
->raid_disks
;
5768 /* don't set md_minor, it is determined by which /dev/md* was
5771 if (info
->state
& (1<<MD_SB_CLEAN
))
5772 mddev
->recovery_cp
= MaxSector
;
5774 mddev
->recovery_cp
= 0;
5775 mddev
->persistent
= ! info
->not_persistent
;
5776 mddev
->external
= 0;
5778 mddev
->layout
= info
->layout
;
5779 mddev
->chunk_sectors
= info
->chunk_size
>> 9;
5781 mddev
->max_disks
= MD_SB_DISKS
;
5783 if (mddev
->persistent
)
5785 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
5787 mddev
->bitmap_info
.default_offset
= MD_SB_BYTES
>> 9;
5788 mddev
->bitmap_info
.offset
= 0;
5790 mddev
->reshape_position
= MaxSector
;
5793 * Generate a 128 bit UUID
5795 get_random_bytes(mddev
->uuid
, 16);
5797 mddev
->new_level
= mddev
->level
;
5798 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
5799 mddev
->new_layout
= mddev
->layout
;
5800 mddev
->delta_disks
= 0;
5805 void md_set_array_sectors(mddev_t
*mddev
, sector_t array_sectors
)
5807 WARN(!mddev_is_locked(mddev
), "%s: unlocked mddev!\n", __func__
);
5809 if (mddev
->external_size
)
5812 mddev
->array_sectors
= array_sectors
;
5814 EXPORT_SYMBOL(md_set_array_sectors
);
5816 static int update_size(mddev_t
*mddev
, sector_t num_sectors
)
5820 int fit
= (num_sectors
== 0);
5822 if (mddev
->pers
->resize
== NULL
)
5824 /* The "num_sectors" is the number of sectors of each device that
5825 * is used. This can only make sense for arrays with redundancy.
5826 * linear and raid0 always use whatever space is available. We can only
5827 * consider changing this number if no resync or reconstruction is
5828 * happening, and if the new size is acceptable. It must fit before the
5829 * sb_start or, if that is <data_offset, it must fit before the size
5830 * of each device. If num_sectors is zero, we find the largest size
5833 if (mddev
->sync_thread
)
5836 /* Sorry, cannot grow a bitmap yet, just remove it,
5840 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
5841 sector_t avail
= rdev
->sectors
;
5843 if (fit
&& (num_sectors
== 0 || num_sectors
> avail
))
5844 num_sectors
= avail
;
5845 if (avail
< num_sectors
)
5848 rv
= mddev
->pers
->resize(mddev
, num_sectors
);
5850 revalidate_disk(mddev
->gendisk
);
5854 static int update_raid_disks(mddev_t
*mddev
, int raid_disks
)
5857 /* change the number of raid disks */
5858 if (mddev
->pers
->check_reshape
== NULL
)
5860 if (raid_disks
<= 0 ||
5861 (mddev
->max_disks
&& raid_disks
>= mddev
->max_disks
))
5863 if (mddev
->sync_thread
|| mddev
->reshape_position
!= MaxSector
)
5865 mddev
->delta_disks
= raid_disks
- mddev
->raid_disks
;
5867 rv
= mddev
->pers
->check_reshape(mddev
);
5869 mddev
->delta_disks
= 0;
5875 * update_array_info is used to change the configuration of an
5877 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
5878 * fields in the info are checked against the array.
5879 * Any differences that cannot be handled will cause an error.
5880 * Normally, only one change can be managed at a time.
5882 static int update_array_info(mddev_t
*mddev
, mdu_array_info_t
*info
)
5888 /* calculate expected state,ignoring low bits */
5889 if (mddev
->bitmap
&& mddev
->bitmap_info
.offset
)
5890 state
|= (1 << MD_SB_BITMAP_PRESENT
);
5892 if (mddev
->major_version
!= info
->major_version
||
5893 mddev
->minor_version
!= info
->minor_version
||
5894 /* mddev->patch_version != info->patch_version || */
5895 mddev
->ctime
!= info
->ctime
||
5896 mddev
->level
!= info
->level
||
5897 /* mddev->layout != info->layout || */
5898 !mddev
->persistent
!= info
->not_persistent
||
5899 mddev
->chunk_sectors
!= info
->chunk_size
>> 9 ||
5900 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
5901 ((state
^info
->state
) & 0xfffffe00)
5904 /* Check there is only one change */
5905 if (info
->size
>= 0 && mddev
->dev_sectors
/ 2 != info
->size
)
5907 if (mddev
->raid_disks
!= info
->raid_disks
)
5909 if (mddev
->layout
!= info
->layout
)
5911 if ((state
^ info
->state
) & (1<<MD_SB_BITMAP_PRESENT
))
5918 if (mddev
->layout
!= info
->layout
) {
5920 * we don't need to do anything at the md level, the
5921 * personality will take care of it all.
5923 if (mddev
->pers
->check_reshape
== NULL
)
5926 mddev
->new_layout
= info
->layout
;
5927 rv
= mddev
->pers
->check_reshape(mddev
);
5929 mddev
->new_layout
= mddev
->layout
;
5933 if (info
->size
>= 0 && mddev
->dev_sectors
/ 2 != info
->size
)
5934 rv
= update_size(mddev
, (sector_t
)info
->size
* 2);
5936 if (mddev
->raid_disks
!= info
->raid_disks
)
5937 rv
= update_raid_disks(mddev
, info
->raid_disks
);
5939 if ((state
^ info
->state
) & (1<<MD_SB_BITMAP_PRESENT
)) {
5940 if (mddev
->pers
->quiesce
== NULL
)
5942 if (mddev
->recovery
|| mddev
->sync_thread
)
5944 if (info
->state
& (1<<MD_SB_BITMAP_PRESENT
)) {
5945 /* add the bitmap */
5948 if (mddev
->bitmap_info
.default_offset
== 0)
5950 mddev
->bitmap_info
.offset
=
5951 mddev
->bitmap_info
.default_offset
;
5952 mddev
->pers
->quiesce(mddev
, 1);
5953 rv
= bitmap_create(mddev
);
5955 rv
= bitmap_load(mddev
);
5957 bitmap_destroy(mddev
);
5958 mddev
->pers
->quiesce(mddev
, 0);
5960 /* remove the bitmap */
5963 if (mddev
->bitmap
->file
)
5965 mddev
->pers
->quiesce(mddev
, 1);
5966 bitmap_destroy(mddev
);
5967 mddev
->pers
->quiesce(mddev
, 0);
5968 mddev
->bitmap_info
.offset
= 0;
5971 md_update_sb(mddev
, 1);
5975 static int set_disk_faulty(mddev_t
*mddev
, dev_t dev
)
5979 if (mddev
->pers
== NULL
)
5982 rdev
= find_rdev(mddev
, dev
);
5986 md_error(mddev
, rdev
);
5991 * We have a problem here : there is no easy way to give a CHS
5992 * virtual geometry. We currently pretend that we have a 2 heads
5993 * 4 sectors (with a BIG number of cylinders...). This drives
5994 * dosfs just mad... ;-)
5996 static int md_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
5998 mddev_t
*mddev
= bdev
->bd_disk
->private_data
;
6002 geo
->cylinders
= mddev
->array_sectors
/ 8;
6006 static int md_ioctl(struct block_device
*bdev
, fmode_t mode
,
6007 unsigned int cmd
, unsigned long arg
)
6010 void __user
*argp
= (void __user
*)arg
;
6011 mddev_t
*mddev
= NULL
;
6014 if (!capable(CAP_SYS_ADMIN
))
6018 * Commands dealing with the RAID driver but not any
6024 err
= get_version(argp
);
6027 case PRINT_RAID_DEBUG
:
6035 autostart_arrays(arg
);
6042 * Commands creating/starting a new array:
6045 mddev
= bdev
->bd_disk
->private_data
;
6052 err
= mddev_lock(mddev
);
6055 "md: ioctl lock interrupted, reason %d, cmd %d\n",
6062 case SET_ARRAY_INFO
:
6064 mdu_array_info_t info
;
6066 memset(&info
, 0, sizeof(info
));
6067 else if (copy_from_user(&info
, argp
, sizeof(info
))) {
6072 err
= update_array_info(mddev
, &info
);
6074 printk(KERN_WARNING
"md: couldn't update"
6075 " array info. %d\n", err
);
6080 if (!list_empty(&mddev
->disks
)) {
6082 "md: array %s already has disks!\n",
6087 if (mddev
->raid_disks
) {
6089 "md: array %s already initialised!\n",
6094 err
= set_array_info(mddev
, &info
);
6096 printk(KERN_WARNING
"md: couldn't set"
6097 " array info. %d\n", err
);
6107 * Commands querying/configuring an existing array:
6109 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
6110 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
6111 if ((!mddev
->raid_disks
&& !mddev
->external
)
6112 && cmd
!= ADD_NEW_DISK
&& cmd
!= STOP_ARRAY
6113 && cmd
!= RUN_ARRAY
&& cmd
!= SET_BITMAP_FILE
6114 && cmd
!= GET_BITMAP_FILE
) {
6120 * Commands even a read-only array can execute:
6124 case GET_ARRAY_INFO
:
6125 err
= get_array_info(mddev
, argp
);
6128 case GET_BITMAP_FILE
:
6129 err
= get_bitmap_file(mddev
, argp
);
6133 err
= get_disk_info(mddev
, argp
);
6136 case RESTART_ARRAY_RW
:
6137 err
= restart_array(mddev
);
6141 err
= do_md_stop(mddev
, 0, 1);
6145 err
= md_set_readonly(mddev
, 1);
6149 if (get_user(ro
, (int __user
*)(arg
))) {
6155 /* if the bdev is going readonly the value of mddev->ro
6156 * does not matter, no writes are coming
6161 /* are we are already prepared for writes? */
6165 /* transitioning to readauto need only happen for
6166 * arrays that call md_write_start
6169 err
= restart_array(mddev
);
6172 set_disk_ro(mddev
->gendisk
, 0);
6179 * The remaining ioctls are changing the state of the
6180 * superblock, so we do not allow them on read-only arrays.
6181 * However non-MD ioctls (e.g. get-size) will still come through
6182 * here and hit the 'default' below, so only disallow
6183 * 'md' ioctls, and switch to rw mode if started auto-readonly.
6185 if (_IOC_TYPE(cmd
) == MD_MAJOR
&& mddev
->ro
&& mddev
->pers
) {
6186 if (mddev
->ro
== 2) {
6188 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
6189 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6190 md_wakeup_thread(mddev
->thread
);
6201 mdu_disk_info_t info
;
6202 if (copy_from_user(&info
, argp
, sizeof(info
)))
6205 err
= add_new_disk(mddev
, &info
);
6209 case HOT_REMOVE_DISK
:
6210 err
= hot_remove_disk(mddev
, new_decode_dev(arg
));
6214 err
= hot_add_disk(mddev
, new_decode_dev(arg
));
6217 case SET_DISK_FAULTY
:
6218 err
= set_disk_faulty(mddev
, new_decode_dev(arg
));
6222 err
= do_md_run(mddev
);
6225 case SET_BITMAP_FILE
:
6226 err
= set_bitmap_file(mddev
, (int)arg
);
6236 if (mddev
->hold_active
== UNTIL_IOCTL
&&
6238 mddev
->hold_active
= 0;
6239 mddev_unlock(mddev
);
6248 #ifdef CONFIG_COMPAT
6249 static int md_compat_ioctl(struct block_device
*bdev
, fmode_t mode
,
6250 unsigned int cmd
, unsigned long arg
)
6253 case HOT_REMOVE_DISK
:
6255 case SET_DISK_FAULTY
:
6256 case SET_BITMAP_FILE
:
6257 /* These take in integer arg, do not convert */
6260 arg
= (unsigned long)compat_ptr(arg
);
6264 return md_ioctl(bdev
, mode
, cmd
, arg
);
6266 #endif /* CONFIG_COMPAT */
6268 static int md_open(struct block_device
*bdev
, fmode_t mode
)
6271 * Succeed if we can lock the mddev, which confirms that
6272 * it isn't being stopped right now.
6274 mddev_t
*mddev
= mddev_find(bdev
->bd_dev
);
6277 if (mddev
->gendisk
!= bdev
->bd_disk
) {
6278 /* we are racing with mddev_put which is discarding this
6282 /* Wait until bdev->bd_disk is definitely gone */
6283 flush_workqueue(md_misc_wq
);
6284 /* Then retry the open from the top */
6285 return -ERESTARTSYS
;
6287 BUG_ON(mddev
!= bdev
->bd_disk
->private_data
);
6289 if ((err
= mutex_lock_interruptible(&mddev
->open_mutex
)))
6293 atomic_inc(&mddev
->openers
);
6294 mutex_unlock(&mddev
->open_mutex
);
6296 check_disk_change(bdev
);
6301 static int md_release(struct gendisk
*disk
, fmode_t mode
)
6303 mddev_t
*mddev
= disk
->private_data
;
6306 atomic_dec(&mddev
->openers
);
6312 static int md_media_changed(struct gendisk
*disk
)
6314 mddev_t
*mddev
= disk
->private_data
;
6316 return mddev
->changed
;
6319 static int md_revalidate(struct gendisk
*disk
)
6321 mddev_t
*mddev
= disk
->private_data
;
6326 static const struct block_device_operations md_fops
=
6328 .owner
= THIS_MODULE
,
6330 .release
= md_release
,
6332 #ifdef CONFIG_COMPAT
6333 .compat_ioctl
= md_compat_ioctl
,
6335 .getgeo
= md_getgeo
,
6336 .media_changed
= md_media_changed
,
6337 .revalidate_disk
= md_revalidate
,
6340 static int md_thread(void * arg
)
6342 mdk_thread_t
*thread
= arg
;
6345 * md_thread is a 'system-thread', it's priority should be very
6346 * high. We avoid resource deadlocks individually in each
6347 * raid personality. (RAID5 does preallocation) We also use RR and
6348 * the very same RT priority as kswapd, thus we will never get
6349 * into a priority inversion deadlock.
6351 * we definitely have to have equal or higher priority than
6352 * bdflush, otherwise bdflush will deadlock if there are too
6353 * many dirty RAID5 blocks.
6356 allow_signal(SIGKILL
);
6357 while (!kthread_should_stop()) {
6359 /* We need to wait INTERRUPTIBLE so that
6360 * we don't add to the load-average.
6361 * That means we need to be sure no signals are
6364 if (signal_pending(current
))
6365 flush_signals(current
);
6367 wait_event_interruptible_timeout
6369 test_bit(THREAD_WAKEUP
, &thread
->flags
)
6370 || kthread_should_stop(),
6373 clear_bit(THREAD_WAKEUP
, &thread
->flags
);
6374 if (!kthread_should_stop())
6375 thread
->run(thread
->mddev
);
6381 void md_wakeup_thread(mdk_thread_t
*thread
)
6384 dprintk("md: waking up MD thread %s.\n", thread
->tsk
->comm
);
6385 set_bit(THREAD_WAKEUP
, &thread
->flags
);
6386 wake_up(&thread
->wqueue
);
6390 mdk_thread_t
*md_register_thread(void (*run
) (mddev_t
*), mddev_t
*mddev
,
6393 mdk_thread_t
*thread
;
6395 thread
= kzalloc(sizeof(mdk_thread_t
), GFP_KERNEL
);
6399 init_waitqueue_head(&thread
->wqueue
);
6402 thread
->mddev
= mddev
;
6403 thread
->timeout
= MAX_SCHEDULE_TIMEOUT
;
6404 thread
->tsk
= kthread_run(md_thread
, thread
,
6406 mdname(thread
->mddev
),
6407 name
?: mddev
->pers
->name
);
6408 if (IS_ERR(thread
->tsk
)) {
6415 void md_unregister_thread(mdk_thread_t
*thread
)
6419 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread
->tsk
));
6421 kthread_stop(thread
->tsk
);
6425 void md_error(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
6432 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
))
6435 if (!mddev
->pers
|| !mddev
->pers
->error_handler
)
6437 mddev
->pers
->error_handler(mddev
,rdev
);
6438 if (mddev
->degraded
)
6439 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
6440 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
6441 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6442 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6443 md_wakeup_thread(mddev
->thread
);
6444 if (mddev
->event_work
.func
)
6445 queue_work(md_misc_wq
, &mddev
->event_work
);
6446 md_new_event_inintr(mddev
);
6449 /* seq_file implementation /proc/mdstat */
6451 static void status_unused(struct seq_file
*seq
)
6456 seq_printf(seq
, "unused devices: ");
6458 list_for_each_entry(rdev
, &pending_raid_disks
, same_set
) {
6459 char b
[BDEVNAME_SIZE
];
6461 seq_printf(seq
, "%s ",
6462 bdevname(rdev
->bdev
,b
));
6465 seq_printf(seq
, "<none>");
6467 seq_printf(seq
, "\n");
6471 static void status_resync(struct seq_file
*seq
, mddev_t
* mddev
)
6473 sector_t max_sectors
, resync
, res
;
6474 unsigned long dt
, db
;
6477 unsigned int per_milli
;
6479 resync
= mddev
->curr_resync
- atomic_read(&mddev
->recovery_active
);
6481 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
6482 max_sectors
= mddev
->resync_max_sectors
;
6484 max_sectors
= mddev
->dev_sectors
;
6487 * Should not happen.
6493 /* Pick 'scale' such that (resync>>scale)*1000 will fit
6494 * in a sector_t, and (max_sectors>>scale) will fit in a
6495 * u32, as those are the requirements for sector_div.
6496 * Thus 'scale' must be at least 10
6499 if (sizeof(sector_t
) > sizeof(unsigned long)) {
6500 while ( max_sectors
/2 > (1ULL<<(scale
+32)))
6503 res
= (resync
>>scale
)*1000;
6504 sector_div(res
, (u32
)((max_sectors
>>scale
)+1));
6508 int i
, x
= per_milli
/50, y
= 20-x
;
6509 seq_printf(seq
, "[");
6510 for (i
= 0; i
< x
; i
++)
6511 seq_printf(seq
, "=");
6512 seq_printf(seq
, ">");
6513 for (i
= 0; i
< y
; i
++)
6514 seq_printf(seq
, ".");
6515 seq_printf(seq
, "] ");
6517 seq_printf(seq
, " %s =%3u.%u%% (%llu/%llu)",
6518 (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
)?
6520 (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
)?
6522 (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ?
6523 "resync" : "recovery"))),
6524 per_milli
/10, per_milli
% 10,
6525 (unsigned long long) resync
/2,
6526 (unsigned long long) max_sectors
/2);
6529 * dt: time from mark until now
6530 * db: blocks written from mark until now
6531 * rt: remaining time
6533 * rt is a sector_t, so could be 32bit or 64bit.
6534 * So we divide before multiply in case it is 32bit and close
6536 * We scale the divisor (db) by 32 to avoid losing precision
6537 * near the end of resync when the number of remaining sectors
6539 * We then divide rt by 32 after multiplying by db to compensate.
6540 * The '+1' avoids division by zero if db is very small.
6542 dt
= ((jiffies
- mddev
->resync_mark
) / HZ
);
6544 db
= (mddev
->curr_mark_cnt
- atomic_read(&mddev
->recovery_active
))
6545 - mddev
->resync_mark_cnt
;
6547 rt
= max_sectors
- resync
; /* number of remaining sectors */
6548 sector_div(rt
, db
/32+1);
6552 seq_printf(seq
, " finish=%lu.%lumin", (unsigned long)rt
/ 60,
6553 ((unsigned long)rt
% 60)/6);
6555 seq_printf(seq
, " speed=%ldK/sec", db
/2/dt
);
6558 static void *md_seq_start(struct seq_file
*seq
, loff_t
*pos
)
6560 struct list_head
*tmp
;
6570 spin_lock(&all_mddevs_lock
);
6571 list_for_each(tmp
,&all_mddevs
)
6573 mddev
= list_entry(tmp
, mddev_t
, all_mddevs
);
6575 spin_unlock(&all_mddevs_lock
);
6578 spin_unlock(&all_mddevs_lock
);
6580 return (void*)2;/* tail */
6584 static void *md_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
6586 struct list_head
*tmp
;
6587 mddev_t
*next_mddev
, *mddev
= v
;
6593 spin_lock(&all_mddevs_lock
);
6595 tmp
= all_mddevs
.next
;
6597 tmp
= mddev
->all_mddevs
.next
;
6598 if (tmp
!= &all_mddevs
)
6599 next_mddev
= mddev_get(list_entry(tmp
,mddev_t
,all_mddevs
));
6601 next_mddev
= (void*)2;
6604 spin_unlock(&all_mddevs_lock
);
6612 static void md_seq_stop(struct seq_file
*seq
, void *v
)
6616 if (mddev
&& v
!= (void*)1 && v
!= (void*)2)
6620 static int md_seq_show(struct seq_file
*seq
, void *v
)
6625 struct bitmap
*bitmap
;
6627 if (v
== (void*)1) {
6628 struct mdk_personality
*pers
;
6629 seq_printf(seq
, "Personalities : ");
6630 spin_lock(&pers_lock
);
6631 list_for_each_entry(pers
, &pers_list
, list
)
6632 seq_printf(seq
, "[%s] ", pers
->name
);
6634 spin_unlock(&pers_lock
);
6635 seq_printf(seq
, "\n");
6636 seq
->poll_event
= atomic_read(&md_event_count
);
6639 if (v
== (void*)2) {
6644 if (mddev_lock(mddev
) < 0)
6647 if (mddev
->pers
|| mddev
->raid_disks
|| !list_empty(&mddev
->disks
)) {
6648 seq_printf(seq
, "%s : %sactive", mdname(mddev
),
6649 mddev
->pers
? "" : "in");
6652 seq_printf(seq
, " (read-only)");
6654 seq_printf(seq
, " (auto-read-only)");
6655 seq_printf(seq
, " %s", mddev
->pers
->name
);
6659 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
6660 char b
[BDEVNAME_SIZE
];
6661 seq_printf(seq
, " %s[%d]",
6662 bdevname(rdev
->bdev
,b
), rdev
->desc_nr
);
6663 if (test_bit(WriteMostly
, &rdev
->flags
))
6664 seq_printf(seq
, "(W)");
6665 if (test_bit(Faulty
, &rdev
->flags
)) {
6666 seq_printf(seq
, "(F)");
6668 } else if (rdev
->raid_disk
< 0)
6669 seq_printf(seq
, "(S)"); /* spare */
6670 sectors
+= rdev
->sectors
;
6673 if (!list_empty(&mddev
->disks
)) {
6675 seq_printf(seq
, "\n %llu blocks",
6676 (unsigned long long)
6677 mddev
->array_sectors
/ 2);
6679 seq_printf(seq
, "\n %llu blocks",
6680 (unsigned long long)sectors
/ 2);
6682 if (mddev
->persistent
) {
6683 if (mddev
->major_version
!= 0 ||
6684 mddev
->minor_version
!= 90) {
6685 seq_printf(seq
," super %d.%d",
6686 mddev
->major_version
,
6687 mddev
->minor_version
);
6689 } else if (mddev
->external
)
6690 seq_printf(seq
, " super external:%s",
6691 mddev
->metadata_type
);
6693 seq_printf(seq
, " super non-persistent");
6696 mddev
->pers
->status(seq
, mddev
);
6697 seq_printf(seq
, "\n ");
6698 if (mddev
->pers
->sync_request
) {
6699 if (mddev
->curr_resync
> 2) {
6700 status_resync(seq
, mddev
);
6701 seq_printf(seq
, "\n ");
6702 } else if (mddev
->curr_resync
== 1 || mddev
->curr_resync
== 2)
6703 seq_printf(seq
, "\tresync=DELAYED\n ");
6704 else if (mddev
->recovery_cp
< MaxSector
)
6705 seq_printf(seq
, "\tresync=PENDING\n ");
6708 seq_printf(seq
, "\n ");
6710 if ((bitmap
= mddev
->bitmap
)) {
6711 unsigned long chunk_kb
;
6712 unsigned long flags
;
6713 spin_lock_irqsave(&bitmap
->lock
, flags
);
6714 chunk_kb
= mddev
->bitmap_info
.chunksize
>> 10;
6715 seq_printf(seq
, "bitmap: %lu/%lu pages [%luKB], "
6717 bitmap
->pages
- bitmap
->missing_pages
,
6719 (bitmap
->pages
- bitmap
->missing_pages
)
6720 << (PAGE_SHIFT
- 10),
6721 chunk_kb
? chunk_kb
: mddev
->bitmap_info
.chunksize
,
6722 chunk_kb
? "KB" : "B");
6724 seq_printf(seq
, ", file: ");
6725 seq_path(seq
, &bitmap
->file
->f_path
, " \t\n");
6728 seq_printf(seq
, "\n");
6729 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
6732 seq_printf(seq
, "\n");
6734 mddev_unlock(mddev
);
6739 static const struct seq_operations md_seq_ops
= {
6740 .start
= md_seq_start
,
6741 .next
= md_seq_next
,
6742 .stop
= md_seq_stop
,
6743 .show
= md_seq_show
,
6746 static int md_seq_open(struct inode
*inode
, struct file
*file
)
6748 struct seq_file
*seq
;
6751 error
= seq_open(file
, &md_seq_ops
);
6755 seq
= file
->private_data
;
6756 seq
->poll_event
= atomic_read(&md_event_count
);
6760 static unsigned int mdstat_poll(struct file
*filp
, poll_table
*wait
)
6762 struct seq_file
*seq
= filp
->private_data
;
6765 poll_wait(filp
, &md_event_waiters
, wait
);
6767 /* always allow read */
6768 mask
= POLLIN
| POLLRDNORM
;
6770 if (seq
->poll_event
!= atomic_read(&md_event_count
))
6771 mask
|= POLLERR
| POLLPRI
;
6775 static const struct file_operations md_seq_fops
= {
6776 .owner
= THIS_MODULE
,
6777 .open
= md_seq_open
,
6779 .llseek
= seq_lseek
,
6780 .release
= seq_release_private
,
6781 .poll
= mdstat_poll
,
6784 int register_md_personality(struct mdk_personality
*p
)
6786 spin_lock(&pers_lock
);
6787 list_add_tail(&p
->list
, &pers_list
);
6788 printk(KERN_INFO
"md: %s personality registered for level %d\n", p
->name
, p
->level
);
6789 spin_unlock(&pers_lock
);
6793 int unregister_md_personality(struct mdk_personality
*p
)
6795 printk(KERN_INFO
"md: %s personality unregistered\n", p
->name
);
6796 spin_lock(&pers_lock
);
6797 list_del_init(&p
->list
);
6798 spin_unlock(&pers_lock
);
6802 static int is_mddev_idle(mddev_t
*mddev
, int init
)
6810 rdev_for_each_rcu(rdev
, mddev
) {
6811 struct gendisk
*disk
= rdev
->bdev
->bd_contains
->bd_disk
;
6812 curr_events
= (int)part_stat_read(&disk
->part0
, sectors
[0]) +
6813 (int)part_stat_read(&disk
->part0
, sectors
[1]) -
6814 atomic_read(&disk
->sync_io
);
6815 /* sync IO will cause sync_io to increase before the disk_stats
6816 * as sync_io is counted when a request starts, and
6817 * disk_stats is counted when it completes.
6818 * So resync activity will cause curr_events to be smaller than
6819 * when there was no such activity.
6820 * non-sync IO will cause disk_stat to increase without
6821 * increasing sync_io so curr_events will (eventually)
6822 * be larger than it was before. Once it becomes
6823 * substantially larger, the test below will cause
6824 * the array to appear non-idle, and resync will slow
6826 * If there is a lot of outstanding resync activity when
6827 * we set last_event to curr_events, then all that activity
6828 * completing might cause the array to appear non-idle
6829 * and resync will be slowed down even though there might
6830 * not have been non-resync activity. This will only
6831 * happen once though. 'last_events' will soon reflect
6832 * the state where there is little or no outstanding
6833 * resync requests, and further resync activity will
6834 * always make curr_events less than last_events.
6837 if (init
|| curr_events
- rdev
->last_events
> 64) {
6838 rdev
->last_events
= curr_events
;
6846 void md_done_sync(mddev_t
*mddev
, int blocks
, int ok
)
6848 /* another "blocks" (512byte) blocks have been synced */
6849 atomic_sub(blocks
, &mddev
->recovery_active
);
6850 wake_up(&mddev
->recovery_wait
);
6852 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6853 md_wakeup_thread(mddev
->thread
);
6854 // stop recovery, signal do_sync ....
6859 /* md_write_start(mddev, bi)
6860 * If we need to update some array metadata (e.g. 'active' flag
6861 * in superblock) before writing, schedule a superblock update
6862 * and wait for it to complete.
6864 void md_write_start(mddev_t
*mddev
, struct bio
*bi
)
6867 if (bio_data_dir(bi
) != WRITE
)
6870 BUG_ON(mddev
->ro
== 1);
6871 if (mddev
->ro
== 2) {
6872 /* need to switch to read/write */
6874 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6875 md_wakeup_thread(mddev
->thread
);
6876 md_wakeup_thread(mddev
->sync_thread
);
6879 atomic_inc(&mddev
->writes_pending
);
6880 if (mddev
->safemode
== 1)
6881 mddev
->safemode
= 0;
6882 if (mddev
->in_sync
) {
6883 spin_lock_irq(&mddev
->write_lock
);
6884 if (mddev
->in_sync
) {
6886 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
6887 set_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
6888 md_wakeup_thread(mddev
->thread
);
6891 spin_unlock_irq(&mddev
->write_lock
);
6894 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
6895 wait_event(mddev
->sb_wait
,
6896 !test_bit(MD_CHANGE_PENDING
, &mddev
->flags
));
6899 void md_write_end(mddev_t
*mddev
)
6901 if (atomic_dec_and_test(&mddev
->writes_pending
)) {
6902 if (mddev
->safemode
== 2)
6903 md_wakeup_thread(mddev
->thread
);
6904 else if (mddev
->safemode_delay
)
6905 mod_timer(&mddev
->safemode_timer
, jiffies
+ mddev
->safemode_delay
);
6909 /* md_allow_write(mddev)
6910 * Calling this ensures that the array is marked 'active' so that writes
6911 * may proceed without blocking. It is important to call this before
6912 * attempting a GFP_KERNEL allocation while holding the mddev lock.
6913 * Must be called with mddev_lock held.
6915 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
6916 * is dropped, so return -EAGAIN after notifying userspace.
6918 int md_allow_write(mddev_t
*mddev
)
6924 if (!mddev
->pers
->sync_request
)
6927 spin_lock_irq(&mddev
->write_lock
);
6928 if (mddev
->in_sync
) {
6930 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
6931 set_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
6932 if (mddev
->safemode_delay
&&
6933 mddev
->safemode
== 0)
6934 mddev
->safemode
= 1;
6935 spin_unlock_irq(&mddev
->write_lock
);
6936 md_update_sb(mddev
, 0);
6937 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
6939 spin_unlock_irq(&mddev
->write_lock
);
6941 if (test_bit(MD_CHANGE_PENDING
, &mddev
->flags
))
6946 EXPORT_SYMBOL_GPL(md_allow_write
);
6948 #define SYNC_MARKS 10
6949 #define SYNC_MARK_STEP (3*HZ)
6950 void md_do_sync(mddev_t
*mddev
)
6953 unsigned int currspeed
= 0,
6955 sector_t max_sectors
,j
, io_sectors
;
6956 unsigned long mark
[SYNC_MARKS
];
6957 sector_t mark_cnt
[SYNC_MARKS
];
6959 struct list_head
*tmp
;
6960 sector_t last_check
;
6965 /* just incase thread restarts... */
6966 if (test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
))
6968 if (mddev
->ro
) /* never try to sync a read-only array */
6971 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
6972 if (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
))
6973 desc
= "data-check";
6974 else if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
6975 desc
= "requested-resync";
6978 } else if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
6983 /* we overload curr_resync somewhat here.
6984 * 0 == not engaged in resync at all
6985 * 2 == checking that there is no conflict with another sync
6986 * 1 == like 2, but have yielded to allow conflicting resync to
6988 * other == active in resync - this many blocks
6990 * Before starting a resync we must have set curr_resync to
6991 * 2, and then checked that every "conflicting" array has curr_resync
6992 * less than ours. When we find one that is the same or higher
6993 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
6994 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
6995 * This will mean we have to start checking from the beginning again.
7000 mddev
->curr_resync
= 2;
7003 if (kthread_should_stop())
7004 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
7006 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
7008 for_each_mddev(mddev2
, tmp
) {
7009 if (mddev2
== mddev
)
7011 if (!mddev
->parallel_resync
7012 && mddev2
->curr_resync
7013 && match_mddev_units(mddev
, mddev2
)) {
7015 if (mddev
< mddev2
&& mddev
->curr_resync
== 2) {
7016 /* arbitrarily yield */
7017 mddev
->curr_resync
= 1;
7018 wake_up(&resync_wait
);
7020 if (mddev
> mddev2
&& mddev
->curr_resync
== 1)
7021 /* no need to wait here, we can wait the next
7022 * time 'round when curr_resync == 2
7025 /* We need to wait 'interruptible' so as not to
7026 * contribute to the load average, and not to
7027 * be caught by 'softlockup'
7029 prepare_to_wait(&resync_wait
, &wq
, TASK_INTERRUPTIBLE
);
7030 if (!kthread_should_stop() &&
7031 mddev2
->curr_resync
>= mddev
->curr_resync
) {
7032 printk(KERN_INFO
"md: delaying %s of %s"
7033 " until %s has finished (they"
7034 " share one or more physical units)\n",
7035 desc
, mdname(mddev
), mdname(mddev2
));
7037 if (signal_pending(current
))
7038 flush_signals(current
);
7040 finish_wait(&resync_wait
, &wq
);
7043 finish_wait(&resync_wait
, &wq
);
7046 } while (mddev
->curr_resync
< 2);
7049 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
7050 /* resync follows the size requested by the personality,
7051 * which defaults to physical size, but can be virtual size
7053 max_sectors
= mddev
->resync_max_sectors
;
7054 mddev
->resync_mismatches
= 0;
7055 /* we don't use the checkpoint if there's a bitmap */
7056 if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
7057 j
= mddev
->resync_min
;
7058 else if (!mddev
->bitmap
)
7059 j
= mddev
->recovery_cp
;
7061 } else if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
7062 max_sectors
= mddev
->dev_sectors
;
7064 /* recovery follows the physical size of devices */
7065 max_sectors
= mddev
->dev_sectors
;
7068 list_for_each_entry_rcu(rdev
, &mddev
->disks
, same_set
)
7069 if (rdev
->raid_disk
>= 0 &&
7070 !test_bit(Faulty
, &rdev
->flags
) &&
7071 !test_bit(In_sync
, &rdev
->flags
) &&
7072 rdev
->recovery_offset
< j
)
7073 j
= rdev
->recovery_offset
;
7077 printk(KERN_INFO
"md: %s of RAID array %s\n", desc
, mdname(mddev
));
7078 printk(KERN_INFO
"md: minimum _guaranteed_ speed:"
7079 " %d KB/sec/disk.\n", speed_min(mddev
));
7080 printk(KERN_INFO
"md: using maximum available idle IO bandwidth "
7081 "(but not more than %d KB/sec) for %s.\n",
7082 speed_max(mddev
), desc
);
7084 is_mddev_idle(mddev
, 1); /* this initializes IO event counters */
7087 for (m
= 0; m
< SYNC_MARKS
; m
++) {
7089 mark_cnt
[m
] = io_sectors
;
7092 mddev
->resync_mark
= mark
[last_mark
];
7093 mddev
->resync_mark_cnt
= mark_cnt
[last_mark
];
7096 * Tune reconstruction:
7098 window
= 32*(PAGE_SIZE
/512);
7099 printk(KERN_INFO
"md: using %dk window, over a total of %lluk.\n",
7100 window
/2, (unsigned long long)max_sectors
/2);
7102 atomic_set(&mddev
->recovery_active
, 0);
7107 "md: resuming %s of %s from checkpoint.\n",
7108 desc
, mdname(mddev
));
7109 mddev
->curr_resync
= j
;
7111 mddev
->curr_resync_completed
= j
;
7113 while (j
< max_sectors
) {
7118 if (!test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
7119 ((mddev
->curr_resync
> mddev
->curr_resync_completed
&&
7120 (mddev
->curr_resync
- mddev
->curr_resync_completed
)
7121 > (max_sectors
>> 4)) ||
7122 (j
- mddev
->curr_resync_completed
)*2
7123 >= mddev
->resync_max
- mddev
->curr_resync_completed
7125 /* time to update curr_resync_completed */
7126 wait_event(mddev
->recovery_wait
,
7127 atomic_read(&mddev
->recovery_active
) == 0);
7128 mddev
->curr_resync_completed
= j
;
7129 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
7130 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
7133 while (j
>= mddev
->resync_max
&& !kthread_should_stop()) {
7134 /* As this condition is controlled by user-space,
7135 * we can block indefinitely, so use '_interruptible'
7136 * to avoid triggering warnings.
7138 flush_signals(current
); /* just in case */
7139 wait_event_interruptible(mddev
->recovery_wait
,
7140 mddev
->resync_max
> j
7141 || kthread_should_stop());
7144 if (kthread_should_stop())
7147 sectors
= mddev
->pers
->sync_request(mddev
, j
, &skipped
,
7148 currspeed
< speed_min(mddev
));
7150 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
7154 if (!skipped
) { /* actual IO requested */
7155 io_sectors
+= sectors
;
7156 atomic_add(sectors
, &mddev
->recovery_active
);
7159 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
7163 if (j
>1) mddev
->curr_resync
= j
;
7164 mddev
->curr_mark_cnt
= io_sectors
;
7165 if (last_check
== 0)
7166 /* this is the earliest that rebuild will be
7167 * visible in /proc/mdstat
7169 md_new_event(mddev
);
7171 if (last_check
+ window
> io_sectors
|| j
== max_sectors
)
7174 last_check
= io_sectors
;
7176 if (time_after_eq(jiffies
, mark
[last_mark
] + SYNC_MARK_STEP
)) {
7178 int next
= (last_mark
+1) % SYNC_MARKS
;
7180 mddev
->resync_mark
= mark
[next
];
7181 mddev
->resync_mark_cnt
= mark_cnt
[next
];
7182 mark
[next
] = jiffies
;
7183 mark_cnt
[next
] = io_sectors
- atomic_read(&mddev
->recovery_active
);
7188 if (kthread_should_stop())
7193 * this loop exits only if either when we are slower than
7194 * the 'hard' speed limit, or the system was IO-idle for
7196 * the system might be non-idle CPU-wise, but we only care
7197 * about not overloading the IO subsystem. (things like an
7198 * e2fsck being done on the RAID array should execute fast)
7202 currspeed
= ((unsigned long)(io_sectors
-mddev
->resync_mark_cnt
))/2
7203 /((jiffies
-mddev
->resync_mark
)/HZ
+1) +1;
7205 if (currspeed
> speed_min(mddev
)) {
7206 if ((currspeed
> speed_max(mddev
)) ||
7207 !is_mddev_idle(mddev
, 0)) {
7213 printk(KERN_INFO
"md: %s: %s done.\n",mdname(mddev
), desc
);
7215 * this also signals 'finished resyncing' to md_stop
7218 wait_event(mddev
->recovery_wait
, !atomic_read(&mddev
->recovery_active
));
7220 /* tell personality that we are finished */
7221 mddev
->pers
->sync_request(mddev
, max_sectors
, &skipped
, 1);
7223 if (!test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
) &&
7224 mddev
->curr_resync
> 2) {
7225 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
7226 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
7227 if (mddev
->curr_resync
>= mddev
->recovery_cp
) {
7229 "md: checkpointing %s of %s.\n",
7230 desc
, mdname(mddev
));
7231 mddev
->recovery_cp
= mddev
->curr_resync
;
7234 mddev
->recovery_cp
= MaxSector
;
7236 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
7237 mddev
->curr_resync
= MaxSector
;
7239 list_for_each_entry_rcu(rdev
, &mddev
->disks
, same_set
)
7240 if (rdev
->raid_disk
>= 0 &&
7241 mddev
->delta_disks
>= 0 &&
7242 !test_bit(Faulty
, &rdev
->flags
) &&
7243 !test_bit(In_sync
, &rdev
->flags
) &&
7244 rdev
->recovery_offset
< mddev
->curr_resync
)
7245 rdev
->recovery_offset
= mddev
->curr_resync
;
7249 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
7252 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
7253 /* We completed so min/max setting can be forgotten if used. */
7254 if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
7255 mddev
->resync_min
= 0;
7256 mddev
->resync_max
= MaxSector
;
7257 } else if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
7258 mddev
->resync_min
= mddev
->curr_resync_completed
;
7259 mddev
->curr_resync
= 0;
7260 wake_up(&resync_wait
);
7261 set_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
7262 md_wakeup_thread(mddev
->thread
);
7267 * got a signal, exit.
7270 "md: md_do_sync() got signal ... exiting\n");
7271 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
7275 EXPORT_SYMBOL_GPL(md_do_sync
);
7277 static int remove_and_add_spares(mddev_t
*mddev
)
7282 mddev
->curr_resync_completed
= 0;
7284 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
7285 if (rdev
->raid_disk
>= 0 &&
7286 !test_bit(Blocked
, &rdev
->flags
) &&
7287 (test_bit(Faulty
, &rdev
->flags
) ||
7288 ! test_bit(In_sync
, &rdev
->flags
)) &&
7289 atomic_read(&rdev
->nr_pending
)==0) {
7290 if (mddev
->pers
->hot_remove_disk(
7291 mddev
, rdev
->raid_disk
)==0) {
7292 sysfs_unlink_rdev(mddev
, rdev
);
7293 rdev
->raid_disk
= -1;
7297 if (mddev
->degraded
) {
7298 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
7299 if (rdev
->raid_disk
>= 0 &&
7300 !test_bit(In_sync
, &rdev
->flags
) &&
7301 !test_bit(Faulty
, &rdev
->flags
))
7303 if (rdev
->raid_disk
< 0
7304 && !test_bit(Faulty
, &rdev
->flags
)) {
7305 rdev
->recovery_offset
= 0;
7307 hot_add_disk(mddev
, rdev
) == 0) {
7308 if (sysfs_link_rdev(mddev
, rdev
))
7309 /* failure here is OK */;
7311 md_new_event(mddev
);
7312 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
7321 static void reap_sync_thread(mddev_t
*mddev
)
7325 /* resync has finished, collect result */
7326 md_unregister_thread(mddev
->sync_thread
);
7327 mddev
->sync_thread
= NULL
;
7328 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
) &&
7329 !test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
)) {
7331 /* activate any spares */
7332 if (mddev
->pers
->spare_active(mddev
))
7333 sysfs_notify(&mddev
->kobj
, NULL
,
7336 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
7337 mddev
->pers
->finish_reshape
)
7338 mddev
->pers
->finish_reshape(mddev
);
7339 md_update_sb(mddev
, 1);
7341 /* if array is no-longer degraded, then any saved_raid_disk
7342 * information must be scrapped
7344 if (!mddev
->degraded
)
7345 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
7346 rdev
->saved_raid_disk
= -1;
7348 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
7349 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
7350 clear_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
7351 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
7352 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
7353 /* flag recovery needed just to double check */
7354 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7355 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
7356 md_new_event(mddev
);
7357 if (mddev
->event_work
.func
)
7358 queue_work(md_misc_wq
, &mddev
->event_work
);
7362 * This routine is regularly called by all per-raid-array threads to
7363 * deal with generic issues like resync and super-block update.
7364 * Raid personalities that don't have a thread (linear/raid0) do not
7365 * need this as they never do any recovery or update the superblock.
7367 * It does not do any resync itself, but rather "forks" off other threads
7368 * to do that as needed.
7369 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
7370 * "->recovery" and create a thread at ->sync_thread.
7371 * When the thread finishes it sets MD_RECOVERY_DONE
7372 * and wakeups up this thread which will reap the thread and finish up.
7373 * This thread also removes any faulty devices (with nr_pending == 0).
7375 * The overall approach is:
7376 * 1/ if the superblock needs updating, update it.
7377 * 2/ If a recovery thread is running, don't do anything else.
7378 * 3/ If recovery has finished, clean up, possibly marking spares active.
7379 * 4/ If there are any faulty devices, remove them.
7380 * 5/ If array is degraded, try to add spares devices
7381 * 6/ If array has spares or is not in-sync, start a resync thread.
7383 void md_check_recovery(mddev_t
*mddev
)
7385 if (mddev
->suspended
)
7389 bitmap_daemon_work(mddev
);
7391 if (signal_pending(current
)) {
7392 if (mddev
->pers
->sync_request
&& !mddev
->external
) {
7393 printk(KERN_INFO
"md: %s in immediate safe mode\n",
7395 mddev
->safemode
= 2;
7397 flush_signals(current
);
7400 if (mddev
->ro
&& !test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))
7403 (mddev
->flags
& ~ (1<<MD_CHANGE_PENDING
)) ||
7404 test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
) ||
7405 test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
) ||
7406 (mddev
->external
== 0 && mddev
->safemode
== 1) ||
7407 (mddev
->safemode
== 2 && ! atomic_read(&mddev
->writes_pending
)
7408 && !mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
)
7412 if (mddev_trylock(mddev
)) {
7416 /* Only thing we do on a ro array is remove
7420 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
7421 if (rdev
->raid_disk
>= 0 &&
7422 !test_bit(Blocked
, &rdev
->flags
) &&
7423 test_bit(Faulty
, &rdev
->flags
) &&
7424 atomic_read(&rdev
->nr_pending
)==0) {
7425 if (mddev
->pers
->hot_remove_disk(
7426 mddev
, rdev
->raid_disk
)==0) {
7427 sysfs_unlink_rdev(mddev
, rdev
);
7428 rdev
->raid_disk
= -1;
7431 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7435 if (!mddev
->external
) {
7437 spin_lock_irq(&mddev
->write_lock
);
7438 if (mddev
->safemode
&&
7439 !atomic_read(&mddev
->writes_pending
) &&
7441 mddev
->recovery_cp
== MaxSector
) {
7444 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
7446 if (mddev
->safemode
== 1)
7447 mddev
->safemode
= 0;
7448 spin_unlock_irq(&mddev
->write_lock
);
7450 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
7454 md_update_sb(mddev
, 0);
7456 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) &&
7457 !test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
)) {
7458 /* resync/recovery still happening */
7459 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7462 if (mddev
->sync_thread
) {
7463 reap_sync_thread(mddev
);
7466 /* Set RUNNING before clearing NEEDED to avoid
7467 * any transients in the value of "sync_action".
7469 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
7470 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7471 /* Clear some bits that don't mean anything, but
7474 clear_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
7475 clear_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
7477 if (test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
))
7479 /* no recovery is running.
7480 * remove any failed drives, then
7481 * add spares if possible.
7482 * Spare are also removed and re-added, to allow
7483 * the personality to fail the re-add.
7486 if (mddev
->reshape_position
!= MaxSector
) {
7487 if (mddev
->pers
->check_reshape
== NULL
||
7488 mddev
->pers
->check_reshape(mddev
) != 0)
7489 /* Cannot proceed */
7491 set_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
7492 clear_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
7493 } else if ((spares
= remove_and_add_spares(mddev
))) {
7494 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
7495 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
7496 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
7497 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
7498 } else if (mddev
->recovery_cp
< MaxSector
) {
7499 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
7500 clear_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
7501 } else if (!test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
7502 /* nothing to be done ... */
7505 if (mddev
->pers
->sync_request
) {
7506 if (spares
&& mddev
->bitmap
&& ! mddev
->bitmap
->file
) {
7507 /* We are adding a device or devices to an array
7508 * which has the bitmap stored on all devices.
7509 * So make sure all bitmap pages get written
7511 bitmap_write_all(mddev
->bitmap
);
7513 mddev
->sync_thread
= md_register_thread(md_do_sync
,
7516 if (!mddev
->sync_thread
) {
7517 printk(KERN_ERR
"%s: could not start resync"
7520 /* leave the spares where they are, it shouldn't hurt */
7521 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
7522 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
7523 clear_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
7524 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
7525 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
7527 md_wakeup_thread(mddev
->sync_thread
);
7528 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
7529 md_new_event(mddev
);
7532 if (!mddev
->sync_thread
) {
7533 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
7534 if (test_and_clear_bit(MD_RECOVERY_RECOVER
,
7536 if (mddev
->sysfs_action
)
7537 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
7539 mddev_unlock(mddev
);
7543 void md_wait_for_blocked_rdev(mdk_rdev_t
*rdev
, mddev_t
*mddev
)
7545 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
7546 wait_event_timeout(rdev
->blocked_wait
,
7547 !test_bit(Blocked
, &rdev
->flags
) &&
7548 !test_bit(BlockedBadBlocks
, &rdev
->flags
),
7549 msecs_to_jiffies(5000));
7550 rdev_dec_pending(rdev
, mddev
);
7552 EXPORT_SYMBOL(md_wait_for_blocked_rdev
);
7555 /* Bad block management.
7556 * We can record which blocks on each device are 'bad' and so just
7557 * fail those blocks, or that stripe, rather than the whole device.
7558 * Entries in the bad-block table are 64bits wide. This comprises:
7559 * Length of bad-range, in sectors: 0-511 for lengths 1-512
7560 * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes)
7561 * A 'shift' can be set so that larger blocks are tracked and
7562 * consequently larger devices can be covered.
7563 * 'Acknowledged' flag - 1 bit. - the most significant bit.
7565 * Locking of the bad-block table uses a seqlock so md_is_badblock
7566 * might need to retry if it is very unlucky.
7567 * We will sometimes want to check for bad blocks in a bi_end_io function,
7568 * so we use the write_seqlock_irq variant.
7570 * When looking for a bad block we specify a range and want to
7571 * know if any block in the range is bad. So we binary-search
7572 * to the last range that starts at-or-before the given endpoint,
7573 * (or "before the sector after the target range")
7574 * then see if it ends after the given start.
7576 * 0 if there are no known bad blocks in the range
7577 * 1 if there are known bad block which are all acknowledged
7578 * -1 if there are bad blocks which have not yet been acknowledged in metadata.
7579 * plus the start/length of the first bad section we overlap.
7581 int md_is_badblock(struct badblocks
*bb
, sector_t s
, int sectors
,
7582 sector_t
*first_bad
, int *bad_sectors
)
7588 sector_t target
= s
+ sectors
;
7591 if (bb
->shift
> 0) {
7592 /* round the start down, and the end up */
7594 target
+= (1<<bb
->shift
) - 1;
7595 target
>>= bb
->shift
;
7596 sectors
= target
- s
;
7598 /* 'target' is now the first block after the bad range */
7601 seq
= read_seqbegin(&bb
->lock
);
7605 /* Binary search between lo and hi for 'target'
7606 * i.e. for the last range that starts before 'target'
7608 /* INVARIANT: ranges before 'lo' and at-or-after 'hi'
7609 * are known not to be the last range before target.
7610 * VARIANT: hi-lo is the number of possible
7611 * ranges, and decreases until it reaches 1
7613 while (hi
- lo
> 1) {
7614 int mid
= (lo
+ hi
) / 2;
7615 sector_t a
= BB_OFFSET(p
[mid
]);
7617 /* This could still be the one, earlier ranges
7621 /* This and later ranges are definitely out. */
7624 /* 'lo' might be the last that started before target, but 'hi' isn't */
7626 /* need to check all range that end after 's' to see if
7627 * any are unacknowledged.
7630 BB_OFFSET(p
[lo
]) + BB_LEN(p
[lo
]) > s
) {
7631 if (BB_OFFSET(p
[lo
]) < target
) {
7632 /* starts before the end, and finishes after
7633 * the start, so they must overlap
7635 if (rv
!= -1 && BB_ACK(p
[lo
]))
7639 *first_bad
= BB_OFFSET(p
[lo
]);
7640 *bad_sectors
= BB_LEN(p
[lo
]);
7646 if (read_seqretry(&bb
->lock
, seq
))
7651 EXPORT_SYMBOL_GPL(md_is_badblock
);
7654 * Add a range of bad blocks to the table.
7655 * This might extend the table, or might contract it
7656 * if two adjacent ranges can be merged.
7657 * We binary-search to find the 'insertion' point, then
7658 * decide how best to handle it.
7660 static int md_set_badblocks(struct badblocks
*bb
, sector_t s
, int sectors
,
7668 /* badblocks are disabled */
7672 /* round the start down, and the end up */
7673 sector_t next
= s
+ sectors
;
7675 next
+= (1<<bb
->shift
) - 1;
7680 write_seqlock_irq(&bb
->lock
);
7685 /* Find the last range that starts at-or-before 's' */
7686 while (hi
- lo
> 1) {
7687 int mid
= (lo
+ hi
) / 2;
7688 sector_t a
= BB_OFFSET(p
[mid
]);
7694 if (hi
> lo
&& BB_OFFSET(p
[lo
]) > s
)
7698 /* we found a range that might merge with the start
7701 sector_t a
= BB_OFFSET(p
[lo
]);
7702 sector_t e
= a
+ BB_LEN(p
[lo
]);
7703 int ack
= BB_ACK(p
[lo
]);
7705 /* Yes, we can merge with a previous range */
7706 if (s
== a
&& s
+ sectors
>= e
)
7707 /* new range covers old */
7710 ack
= ack
&& acknowledged
;
7712 if (e
< s
+ sectors
)
7714 if (e
- a
<= BB_MAX_LEN
) {
7715 p
[lo
] = BB_MAKE(a
, e
-a
, ack
);
7718 /* does not all fit in one range,
7719 * make p[lo] maximal
7721 if (BB_LEN(p
[lo
]) != BB_MAX_LEN
)
7722 p
[lo
] = BB_MAKE(a
, BB_MAX_LEN
, ack
);
7728 if (sectors
&& hi
< bb
->count
) {
7729 /* 'hi' points to the first range that starts after 's'.
7730 * Maybe we can merge with the start of that range */
7731 sector_t a
= BB_OFFSET(p
[hi
]);
7732 sector_t e
= a
+ BB_LEN(p
[hi
]);
7733 int ack
= BB_ACK(p
[hi
]);
7734 if (a
<= s
+ sectors
) {
7735 /* merging is possible */
7736 if (e
<= s
+ sectors
) {
7741 ack
= ack
&& acknowledged
;
7744 if (e
- a
<= BB_MAX_LEN
) {
7745 p
[hi
] = BB_MAKE(a
, e
-a
, ack
);
7748 p
[hi
] = BB_MAKE(a
, BB_MAX_LEN
, ack
);
7756 if (sectors
== 0 && hi
< bb
->count
) {
7757 /* we might be able to combine lo and hi */
7758 /* Note: 's' is at the end of 'lo' */
7759 sector_t a
= BB_OFFSET(p
[hi
]);
7760 int lolen
= BB_LEN(p
[lo
]);
7761 int hilen
= BB_LEN(p
[hi
]);
7762 int newlen
= lolen
+ hilen
- (s
- a
);
7763 if (s
>= a
&& newlen
< BB_MAX_LEN
) {
7764 /* yes, we can combine them */
7765 int ack
= BB_ACK(p
[lo
]) && BB_ACK(p
[hi
]);
7766 p
[lo
] = BB_MAKE(BB_OFFSET(p
[lo
]), newlen
, ack
);
7767 memmove(p
+ hi
, p
+ hi
+ 1,
7768 (bb
->count
- hi
- 1) * 8);
7773 /* didn't merge (it all).
7774 * Need to add a range just before 'hi' */
7775 if (bb
->count
>= MD_MAX_BADBLOCKS
) {
7776 /* No room for more */
7780 int this_sectors
= sectors
;
7781 memmove(p
+ hi
+ 1, p
+ hi
,
7782 (bb
->count
- hi
) * 8);
7785 if (this_sectors
> BB_MAX_LEN
)
7786 this_sectors
= BB_MAX_LEN
;
7787 p
[hi
] = BB_MAKE(s
, this_sectors
, acknowledged
);
7788 sectors
-= this_sectors
;
7795 bb
->unacked_exist
= 1;
7796 write_sequnlock_irq(&bb
->lock
);
7801 int rdev_set_badblocks(mdk_rdev_t
*rdev
, sector_t s
, int sectors
,
7804 int rv
= md_set_badblocks(&rdev
->badblocks
,
7805 s
+ rdev
->data_offset
, sectors
, acknowledged
);
7807 /* Make sure they get written out promptly */
7808 set_bit(MD_CHANGE_CLEAN
, &rdev
->mddev
->flags
);
7809 md_wakeup_thread(rdev
->mddev
->thread
);
7813 EXPORT_SYMBOL_GPL(rdev_set_badblocks
);
7816 * Remove a range of bad blocks from the table.
7817 * This may involve extending the table if we spilt a region,
7818 * but it must not fail. So if the table becomes full, we just
7819 * drop the remove request.
7821 static int md_clear_badblocks(struct badblocks
*bb
, sector_t s
, int sectors
)
7825 sector_t target
= s
+ sectors
;
7828 if (bb
->shift
> 0) {
7829 /* When clearing we round the start up and the end down.
7830 * This should not matter as the shift should align with
7831 * the block size and no rounding should ever be needed.
7832 * However it is better the think a block is bad when it
7833 * isn't than to think a block is not bad when it is.
7835 s
+= (1<<bb
->shift
) - 1;
7837 target
>>= bb
->shift
;
7838 sectors
= target
- s
;
7841 write_seqlock_irq(&bb
->lock
);
7846 /* Find the last range that starts before 'target' */
7847 while (hi
- lo
> 1) {
7848 int mid
= (lo
+ hi
) / 2;
7849 sector_t a
= BB_OFFSET(p
[mid
]);
7856 /* p[lo] is the last range that could overlap the
7857 * current range. Earlier ranges could also overlap,
7858 * but only this one can overlap the end of the range.
7860 if (BB_OFFSET(p
[lo
]) + BB_LEN(p
[lo
]) > target
) {
7861 /* Partial overlap, leave the tail of this range */
7862 int ack
= BB_ACK(p
[lo
]);
7863 sector_t a
= BB_OFFSET(p
[lo
]);
7864 sector_t end
= a
+ BB_LEN(p
[lo
]);
7867 /* we need to split this range */
7868 if (bb
->count
>= MD_MAX_BADBLOCKS
) {
7872 memmove(p
+lo
+1, p
+lo
, (bb
->count
- lo
) * 8);
7874 p
[lo
] = BB_MAKE(a
, s
-a
, ack
);
7877 p
[lo
] = BB_MAKE(target
, end
- target
, ack
);
7878 /* there is no longer an overlap */
7883 BB_OFFSET(p
[lo
]) + BB_LEN(p
[lo
]) > s
) {
7884 /* This range does overlap */
7885 if (BB_OFFSET(p
[lo
]) < s
) {
7886 /* Keep the early parts of this range. */
7887 int ack
= BB_ACK(p
[lo
]);
7888 sector_t start
= BB_OFFSET(p
[lo
]);
7889 p
[lo
] = BB_MAKE(start
, s
- start
, ack
);
7890 /* now low doesn't overlap, so.. */
7895 /* 'lo' is strictly before, 'hi' is strictly after,
7896 * anything between needs to be discarded
7899 memmove(p
+lo
+1, p
+hi
, (bb
->count
- hi
) * 8);
7900 bb
->count
-= (hi
- lo
- 1);
7906 write_sequnlock_irq(&bb
->lock
);
7910 int rdev_clear_badblocks(mdk_rdev_t
*rdev
, sector_t s
, int sectors
)
7912 return md_clear_badblocks(&rdev
->badblocks
,
7913 s
+ rdev
->data_offset
,
7916 EXPORT_SYMBOL_GPL(rdev_clear_badblocks
);
7919 * Acknowledge all bad blocks in a list.
7920 * This only succeeds if ->changed is clear. It is used by
7921 * in-kernel metadata updates
7923 void md_ack_all_badblocks(struct badblocks
*bb
)
7925 if (bb
->page
== NULL
|| bb
->changed
)
7926 /* no point even trying */
7928 write_seqlock_irq(&bb
->lock
);
7930 if (bb
->changed
== 0) {
7933 for (i
= 0; i
< bb
->count
; i
++) {
7934 if (!BB_ACK(p
[i
])) {
7935 sector_t start
= BB_OFFSET(p
[i
]);
7936 int len
= BB_LEN(p
[i
]);
7937 p
[i
] = BB_MAKE(start
, len
, 1);
7940 bb
->unacked_exist
= 0;
7942 write_sequnlock_irq(&bb
->lock
);
7944 EXPORT_SYMBOL_GPL(md_ack_all_badblocks
);
7946 /* sysfs access to bad-blocks list.
7947 * We present two files.
7948 * 'bad-blocks' lists sector numbers and lengths of ranges that
7949 * are recorded as bad. The list is truncated to fit within
7950 * the one-page limit of sysfs.
7951 * Writing "sector length" to this file adds an acknowledged
7953 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
7954 * been acknowledged. Writing to this file adds bad blocks
7955 * without acknowledging them. This is largely for testing.
7959 badblocks_show(struct badblocks
*bb
, char *page
, int unack
)
7970 seq
= read_seqbegin(&bb
->lock
);
7975 while (len
< PAGE_SIZE
&& i
< bb
->count
) {
7976 sector_t s
= BB_OFFSET(p
[i
]);
7977 unsigned int length
= BB_LEN(p
[i
]);
7978 int ack
= BB_ACK(p
[i
]);
7984 len
+= snprintf(page
+len
, PAGE_SIZE
-len
, "%llu %u\n",
7985 (unsigned long long)s
<< bb
->shift
,
7986 length
<< bb
->shift
);
7988 if (unack
&& len
== 0)
7989 bb
->unacked_exist
= 0;
7991 if (read_seqretry(&bb
->lock
, seq
))
8000 badblocks_store(struct badblocks
*bb
, const char *page
, size_t len
, int unack
)
8002 unsigned long long sector
;
8006 /* Allow clearing via sysfs *only* for testing/debugging.
8007 * Normally only a successful write may clear a badblock
8010 if (page
[0] == '-') {
8014 #endif /* DO_DEBUG */
8016 switch (sscanf(page
, "%llu %d%c", §or
, &length
, &newline
)) {
8018 if (newline
!= '\n')
8030 md_clear_badblocks(bb
, sector
, length
);
8033 #endif /* DO_DEBUG */
8034 if (md_set_badblocks(bb
, sector
, length
, !unack
))
8040 static int md_notify_reboot(struct notifier_block
*this,
8041 unsigned long code
, void *x
)
8043 struct list_head
*tmp
;
8046 if ((code
== SYS_DOWN
) || (code
== SYS_HALT
) || (code
== SYS_POWER_OFF
)) {
8048 printk(KERN_INFO
"md: stopping all md devices.\n");
8050 for_each_mddev(mddev
, tmp
)
8051 if (mddev_trylock(mddev
)) {
8052 /* Force a switch to readonly even array
8053 * appears to still be in use. Hence
8056 md_set_readonly(mddev
, 100);
8057 mddev_unlock(mddev
);
8060 * certain more exotic SCSI devices are known to be
8061 * volatile wrt too early system reboots. While the
8062 * right place to handle this issue is the given
8063 * driver, we do want to have a safe RAID driver ...
8070 static struct notifier_block md_notifier
= {
8071 .notifier_call
= md_notify_reboot
,
8073 .priority
= INT_MAX
, /* before any real devices */
8076 static void md_geninit(void)
8078 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t
));
8080 proc_create("mdstat", S_IRUGO
, NULL
, &md_seq_fops
);
8083 static int __init
md_init(void)
8087 md_wq
= alloc_workqueue("md", WQ_MEM_RECLAIM
, 0);
8091 md_misc_wq
= alloc_workqueue("md_misc", 0, 0);
8095 if ((ret
= register_blkdev(MD_MAJOR
, "md")) < 0)
8098 if ((ret
= register_blkdev(0, "mdp")) < 0)
8102 blk_register_region(MKDEV(MD_MAJOR
, 0), 1UL<<MINORBITS
, THIS_MODULE
,
8103 md_probe
, NULL
, NULL
);
8104 blk_register_region(MKDEV(mdp_major
, 0), 1UL<<MINORBITS
, THIS_MODULE
,
8105 md_probe
, NULL
, NULL
);
8107 register_reboot_notifier(&md_notifier
);
8108 raid_table_header
= register_sysctl_table(raid_root_table
);
8114 unregister_blkdev(MD_MAJOR
, "md");
8116 destroy_workqueue(md_misc_wq
);
8118 destroy_workqueue(md_wq
);
8126 * Searches all registered partitions for autorun RAID arrays
8130 static LIST_HEAD(all_detected_devices
);
8131 struct detected_devices_node
{
8132 struct list_head list
;
8136 void md_autodetect_dev(dev_t dev
)
8138 struct detected_devices_node
*node_detected_dev
;
8140 node_detected_dev
= kzalloc(sizeof(*node_detected_dev
), GFP_KERNEL
);
8141 if (node_detected_dev
) {
8142 node_detected_dev
->dev
= dev
;
8143 list_add_tail(&node_detected_dev
->list
, &all_detected_devices
);
8145 printk(KERN_CRIT
"md: md_autodetect_dev: kzalloc failed"
8146 ", skipping dev(%d,%d)\n", MAJOR(dev
), MINOR(dev
));
8151 static void autostart_arrays(int part
)
8154 struct detected_devices_node
*node_detected_dev
;
8156 int i_scanned
, i_passed
;
8161 printk(KERN_INFO
"md: Autodetecting RAID arrays.\n");
8163 while (!list_empty(&all_detected_devices
) && i_scanned
< INT_MAX
) {
8165 node_detected_dev
= list_entry(all_detected_devices
.next
,
8166 struct detected_devices_node
, list
);
8167 list_del(&node_detected_dev
->list
);
8168 dev
= node_detected_dev
->dev
;
8169 kfree(node_detected_dev
);
8170 rdev
= md_import_device(dev
,0, 90);
8174 if (test_bit(Faulty
, &rdev
->flags
)) {
8178 set_bit(AutoDetected
, &rdev
->flags
);
8179 list_add(&rdev
->same_set
, &pending_raid_disks
);
8183 printk(KERN_INFO
"md: Scanned %d and added %d devices.\n",
8184 i_scanned
, i_passed
);
8186 autorun_devices(part
);
8189 #endif /* !MODULE */
8191 static __exit
void md_exit(void)
8194 struct list_head
*tmp
;
8196 blk_unregister_region(MKDEV(MD_MAJOR
,0), 1U << MINORBITS
);
8197 blk_unregister_region(MKDEV(mdp_major
,0), 1U << MINORBITS
);
8199 unregister_blkdev(MD_MAJOR
,"md");
8200 unregister_blkdev(mdp_major
, "mdp");
8201 unregister_reboot_notifier(&md_notifier
);
8202 unregister_sysctl_table(raid_table_header
);
8203 remove_proc_entry("mdstat", NULL
);
8204 for_each_mddev(mddev
, tmp
) {
8205 export_array(mddev
);
8206 mddev
->hold_active
= 0;
8208 destroy_workqueue(md_misc_wq
);
8209 destroy_workqueue(md_wq
);
8212 subsys_initcall(md_init
);
8213 module_exit(md_exit
)
8215 static int get_ro(char *buffer
, struct kernel_param
*kp
)
8217 return sprintf(buffer
, "%d", start_readonly
);
8219 static int set_ro(const char *val
, struct kernel_param
*kp
)
8222 int num
= simple_strtoul(val
, &e
, 10);
8223 if (*val
&& (*e
== '\0' || *e
== '\n')) {
8224 start_readonly
= num
;
8230 module_param_call(start_ro
, set_ro
, get_ro
, NULL
, S_IRUSR
|S_IWUSR
);
8231 module_param(start_dirty_degraded
, int, S_IRUGO
|S_IWUSR
);
8233 module_param_call(new_array
, add_named_array
, NULL
, NULL
, S_IWUSR
);
8235 EXPORT_SYMBOL(register_md_personality
);
8236 EXPORT_SYMBOL(unregister_md_personality
);
8237 EXPORT_SYMBOL(md_error
);
8238 EXPORT_SYMBOL(md_done_sync
);
8239 EXPORT_SYMBOL(md_write_start
);
8240 EXPORT_SYMBOL(md_write_end
);
8241 EXPORT_SYMBOL(md_register_thread
);
8242 EXPORT_SYMBOL(md_unregister_thread
);
8243 EXPORT_SYMBOL(md_wakeup_thread
);
8244 EXPORT_SYMBOL(md_check_recovery
);
8245 MODULE_LICENSE("GPL");
8246 MODULE_DESCRIPTION("MD RAID framework");
8248 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR
);