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/reboot.h>
48 #include <linux/file.h>
49 #include <linux/compat.h>
50 #include <linux/delay.h>
51 #include <linux/raid/md_p.h>
52 #include <linux/raid/md_u.h>
53 #include <linux/slab.h>
58 #define dprintk(x...) ((void)(DEBUG && printk(x)))
61 static void autostart_arrays(int part
);
64 static LIST_HEAD(pers_list
);
65 static DEFINE_SPINLOCK(pers_lock
);
67 static void md_print_devices(void);
69 static DECLARE_WAIT_QUEUE_HEAD(resync_wait
);
70 static struct workqueue_struct
*md_wq
;
71 static struct workqueue_struct
*md_misc_wq
;
73 #define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
76 * Default number of read corrections we'll attempt on an rdev
77 * before ejecting it from the array. We divide the read error
78 * count by 2 for every hour elapsed between read errors.
80 #define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
82 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
83 * is 1000 KB/sec, so the extra system load does not show up that much.
84 * Increase it if you want to have more _guaranteed_ speed. Note that
85 * the RAID driver will use the maximum available bandwidth if the IO
86 * subsystem is idle. There is also an 'absolute maximum' reconstruction
87 * speed limit - in case reconstruction slows down your system despite
90 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
91 * or /sys/block/mdX/md/sync_speed_{min,max}
94 static int sysctl_speed_limit_min
= 1000;
95 static int sysctl_speed_limit_max
= 200000;
96 static inline int speed_min(mddev_t
*mddev
)
98 return mddev
->sync_speed_min
?
99 mddev
->sync_speed_min
: sysctl_speed_limit_min
;
102 static inline int speed_max(mddev_t
*mddev
)
104 return mddev
->sync_speed_max
?
105 mddev
->sync_speed_max
: sysctl_speed_limit_max
;
108 static struct ctl_table_header
*raid_table_header
;
110 static ctl_table raid_table
[] = {
112 .procname
= "speed_limit_min",
113 .data
= &sysctl_speed_limit_min
,
114 .maxlen
= sizeof(int),
115 .mode
= S_IRUGO
|S_IWUSR
,
116 .proc_handler
= proc_dointvec
,
119 .procname
= "speed_limit_max",
120 .data
= &sysctl_speed_limit_max
,
121 .maxlen
= sizeof(int),
122 .mode
= S_IRUGO
|S_IWUSR
,
123 .proc_handler
= proc_dointvec
,
128 static ctl_table raid_dir_table
[] = {
132 .mode
= S_IRUGO
|S_IXUGO
,
138 static ctl_table raid_root_table
[] = {
143 .child
= raid_dir_table
,
148 static const struct block_device_operations md_fops
;
150 static int start_readonly
;
153 * like bio_clone, but with a local bio set
156 static void mddev_bio_destructor(struct bio
*bio
)
158 mddev_t
*mddev
, **mddevp
;
163 bio_free(bio
, mddev
->bio_set
);
166 struct bio
*bio_alloc_mddev(gfp_t gfp_mask
, int nr_iovecs
,
172 if (!mddev
|| !mddev
->bio_set
)
173 return bio_alloc(gfp_mask
, nr_iovecs
);
175 b
= bio_alloc_bioset(gfp_mask
, nr_iovecs
,
181 b
->bi_destructor
= mddev_bio_destructor
;
184 EXPORT_SYMBOL_GPL(bio_alloc_mddev
);
186 struct bio
*bio_clone_mddev(struct bio
*bio
, gfp_t gfp_mask
,
192 if (!mddev
|| !mddev
->bio_set
)
193 return bio_clone(bio
, gfp_mask
);
195 b
= bio_alloc_bioset(gfp_mask
, bio
->bi_max_vecs
,
201 b
->bi_destructor
= mddev_bio_destructor
;
203 if (bio_integrity(bio
)) {
206 ret
= bio_integrity_clone(b
, bio
, gfp_mask
, mddev
->bio_set
);
216 EXPORT_SYMBOL_GPL(bio_clone_mddev
);
218 void md_trim_bio(struct bio
*bio
, int offset
, int size
)
220 /* 'bio' is a cloned bio which we need to trim to match
221 * the given offset and size.
222 * This requires adjusting bi_sector, bi_size, and bi_io_vec
225 struct bio_vec
*bvec
;
229 if (offset
== 0 && size
== bio
->bi_size
)
232 bio
->bi_sector
+= offset
;
235 clear_bit(BIO_SEG_VALID
, &bio
->bi_flags
);
237 while (bio
->bi_idx
< bio
->bi_vcnt
&&
238 bio
->bi_io_vec
[bio
->bi_idx
].bv_len
<= offset
) {
239 /* remove this whole bio_vec */
240 offset
-= bio
->bi_io_vec
[bio
->bi_idx
].bv_len
;
243 if (bio
->bi_idx
< bio
->bi_vcnt
) {
244 bio
->bi_io_vec
[bio
->bi_idx
].bv_offset
+= offset
;
245 bio
->bi_io_vec
[bio
->bi_idx
].bv_len
-= offset
;
247 /* avoid any complications with bi_idx being non-zero*/
249 memmove(bio
->bi_io_vec
, bio
->bi_io_vec
+bio
->bi_idx
,
250 (bio
->bi_vcnt
- bio
->bi_idx
) * sizeof(struct bio_vec
));
251 bio
->bi_vcnt
-= bio
->bi_idx
;
254 /* Make sure vcnt and last bv are not too big */
255 bio_for_each_segment(bvec
, bio
, i
) {
256 if (sofar
+ bvec
->bv_len
> size
)
257 bvec
->bv_len
= size
- sofar
;
258 if (bvec
->bv_len
== 0) {
262 sofar
+= bvec
->bv_len
;
265 EXPORT_SYMBOL_GPL(md_trim_bio
);
268 * We have a system wide 'event count' that is incremented
269 * on any 'interesting' event, and readers of /proc/mdstat
270 * can use 'poll' or 'select' to find out when the event
274 * start array, stop array, error, add device, remove device,
275 * start build, activate spare
277 static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters
);
278 static atomic_t md_event_count
;
279 void md_new_event(mddev_t
*mddev
)
281 atomic_inc(&md_event_count
);
282 wake_up(&md_event_waiters
);
284 EXPORT_SYMBOL_GPL(md_new_event
);
286 /* Alternate version that can be called from interrupts
287 * when calling sysfs_notify isn't needed.
289 static void md_new_event_inintr(mddev_t
*mddev
)
291 atomic_inc(&md_event_count
);
292 wake_up(&md_event_waiters
);
296 * Enables to iterate over all existing md arrays
297 * all_mddevs_lock protects this list.
299 static LIST_HEAD(all_mddevs
);
300 static DEFINE_SPINLOCK(all_mddevs_lock
);
304 * iterates through all used mddevs in the system.
305 * We take care to grab the all_mddevs_lock whenever navigating
306 * the list, and to always hold a refcount when unlocked.
307 * Any code which breaks out of this loop while own
308 * a reference to the current mddev and must mddev_put it.
310 #define for_each_mddev(mddev,tmp) \
312 for (({ spin_lock(&all_mddevs_lock); \
313 tmp = all_mddevs.next; \
315 ({ if (tmp != &all_mddevs) \
316 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
317 spin_unlock(&all_mddevs_lock); \
318 if (mddev) mddev_put(mddev); \
319 mddev = list_entry(tmp, mddev_t, all_mddevs); \
320 tmp != &all_mddevs;}); \
321 ({ spin_lock(&all_mddevs_lock); \
326 /* Rather than calling directly into the personality make_request function,
327 * IO requests come here first so that we can check if the device is
328 * being suspended pending a reconfiguration.
329 * We hold a refcount over the call to ->make_request. By the time that
330 * call has finished, the bio has been linked into some internal structure
331 * and so is visible to ->quiesce(), so we don't need the refcount any more.
333 static int md_make_request(struct request_queue
*q
, struct bio
*bio
)
335 const int rw
= bio_data_dir(bio
);
336 mddev_t
*mddev
= q
->queuedata
;
339 unsigned int sectors
;
341 if (mddev
== NULL
|| mddev
->pers
== NULL
346 smp_rmb(); /* Ensure implications of 'active' are visible */
348 if (mddev
->suspended
) {
351 prepare_to_wait(&mddev
->sb_wait
, &__wait
,
352 TASK_UNINTERRUPTIBLE
);
353 if (!mddev
->suspended
)
359 finish_wait(&mddev
->sb_wait
, &__wait
);
361 atomic_inc(&mddev
->active_io
);
365 * save the sectors now since our bio can
366 * go away inside make_request
368 sectors
= bio_sectors(bio
);
369 rv
= mddev
->pers
->make_request(mddev
, bio
);
371 cpu
= part_stat_lock();
372 part_stat_inc(cpu
, &mddev
->gendisk
->part0
, ios
[rw
]);
373 part_stat_add(cpu
, &mddev
->gendisk
->part0
, sectors
[rw
], sectors
);
376 if (atomic_dec_and_test(&mddev
->active_io
) && mddev
->suspended
)
377 wake_up(&mddev
->sb_wait
);
382 /* mddev_suspend makes sure no new requests are submitted
383 * to the device, and that any requests that have been submitted
384 * are completely handled.
385 * Once ->stop is called and completes, the module will be completely
388 void mddev_suspend(mddev_t
*mddev
)
390 BUG_ON(mddev
->suspended
);
391 mddev
->suspended
= 1;
393 wait_event(mddev
->sb_wait
, atomic_read(&mddev
->active_io
) == 0);
394 mddev
->pers
->quiesce(mddev
, 1);
396 EXPORT_SYMBOL_GPL(mddev_suspend
);
398 void mddev_resume(mddev_t
*mddev
)
400 mddev
->suspended
= 0;
401 wake_up(&mddev
->sb_wait
);
402 mddev
->pers
->quiesce(mddev
, 0);
404 md_wakeup_thread(mddev
->thread
);
405 md_wakeup_thread(mddev
->sync_thread
); /* possibly kick off a reshape */
407 EXPORT_SYMBOL_GPL(mddev_resume
);
409 int mddev_congested(mddev_t
*mddev
, int bits
)
411 return mddev
->suspended
;
413 EXPORT_SYMBOL(mddev_congested
);
416 * Generic flush handling for md
419 static void md_end_flush(struct bio
*bio
, int err
)
421 mdk_rdev_t
*rdev
= bio
->bi_private
;
422 mddev_t
*mddev
= rdev
->mddev
;
424 rdev_dec_pending(rdev
, mddev
);
426 if (atomic_dec_and_test(&mddev
->flush_pending
)) {
427 /* The pre-request flush has finished */
428 queue_work(md_wq
, &mddev
->flush_work
);
433 static void md_submit_flush_data(struct work_struct
*ws
);
435 static void submit_flushes(struct work_struct
*ws
)
437 mddev_t
*mddev
= container_of(ws
, mddev_t
, flush_work
);
440 INIT_WORK(&mddev
->flush_work
, md_submit_flush_data
);
441 atomic_set(&mddev
->flush_pending
, 1);
443 list_for_each_entry_rcu(rdev
, &mddev
->disks
, same_set
)
444 if (rdev
->raid_disk
>= 0 &&
445 !test_bit(Faulty
, &rdev
->flags
)) {
446 /* Take two references, one is dropped
447 * when request finishes, one after
448 * we reclaim rcu_read_lock
451 atomic_inc(&rdev
->nr_pending
);
452 atomic_inc(&rdev
->nr_pending
);
454 bi
= bio_alloc_mddev(GFP_KERNEL
, 0, mddev
);
455 bi
->bi_end_io
= md_end_flush
;
456 bi
->bi_private
= rdev
;
457 bi
->bi_bdev
= rdev
->bdev
;
458 atomic_inc(&mddev
->flush_pending
);
459 submit_bio(WRITE_FLUSH
, bi
);
461 rdev_dec_pending(rdev
, mddev
);
464 if (atomic_dec_and_test(&mddev
->flush_pending
))
465 queue_work(md_wq
, &mddev
->flush_work
);
468 static void md_submit_flush_data(struct work_struct
*ws
)
470 mddev_t
*mddev
= container_of(ws
, mddev_t
, flush_work
);
471 struct bio
*bio
= mddev
->flush_bio
;
473 if (bio
->bi_size
== 0)
474 /* an empty barrier - all done */
477 bio
->bi_rw
&= ~REQ_FLUSH
;
478 if (mddev
->pers
->make_request(mddev
, bio
))
479 generic_make_request(bio
);
482 mddev
->flush_bio
= NULL
;
483 wake_up(&mddev
->sb_wait
);
486 void md_flush_request(mddev_t
*mddev
, struct bio
*bio
)
488 spin_lock_irq(&mddev
->write_lock
);
489 wait_event_lock_irq(mddev
->sb_wait
,
491 mddev
->write_lock
, /*nothing*/);
492 mddev
->flush_bio
= bio
;
493 spin_unlock_irq(&mddev
->write_lock
);
495 INIT_WORK(&mddev
->flush_work
, submit_flushes
);
496 queue_work(md_wq
, &mddev
->flush_work
);
498 EXPORT_SYMBOL(md_flush_request
);
500 /* Support for plugging.
501 * This mirrors the plugging support in request_queue, but does not
502 * require having a whole queue or request structures.
503 * We allocate an md_plug_cb for each md device and each thread it gets
504 * plugged on. This links tot the private plug_handle structure in the
505 * personality data where we keep a count of the number of outstanding
506 * plugs so other code can see if a plug is active.
509 struct blk_plug_cb cb
;
513 static void plugger_unplug(struct blk_plug_cb
*cb
)
515 struct md_plug_cb
*mdcb
= container_of(cb
, struct md_plug_cb
, cb
);
516 if (atomic_dec_and_test(&mdcb
->mddev
->plug_cnt
))
517 md_wakeup_thread(mdcb
->mddev
->thread
);
521 /* Check that an unplug wakeup will come shortly.
522 * If not, wakeup the md thread immediately
524 int mddev_check_plugged(mddev_t
*mddev
)
526 struct blk_plug
*plug
= current
->plug
;
527 struct md_plug_cb
*mdcb
;
532 list_for_each_entry(mdcb
, &plug
->cb_list
, cb
.list
) {
533 if (mdcb
->cb
.callback
== plugger_unplug
&&
534 mdcb
->mddev
== mddev
) {
535 /* Already on the list, move to top */
536 if (mdcb
!= list_first_entry(&plug
->cb_list
,
539 list_move(&mdcb
->cb
.list
, &plug
->cb_list
);
543 /* Not currently on the callback list */
544 mdcb
= kmalloc(sizeof(*mdcb
), GFP_ATOMIC
);
549 mdcb
->cb
.callback
= plugger_unplug
;
550 atomic_inc(&mddev
->plug_cnt
);
551 list_add(&mdcb
->cb
.list
, &plug
->cb_list
);
554 EXPORT_SYMBOL_GPL(mddev_check_plugged
);
556 static inline mddev_t
*mddev_get(mddev_t
*mddev
)
558 atomic_inc(&mddev
->active
);
562 static void mddev_delayed_delete(struct work_struct
*ws
);
564 static void mddev_put(mddev_t
*mddev
)
566 struct bio_set
*bs
= NULL
;
568 if (!atomic_dec_and_lock(&mddev
->active
, &all_mddevs_lock
))
570 if (!mddev
->raid_disks
&& list_empty(&mddev
->disks
) &&
571 mddev
->ctime
== 0 && !mddev
->hold_active
) {
572 /* Array is not configured at all, and not held active,
574 list_del(&mddev
->all_mddevs
);
576 mddev
->bio_set
= NULL
;
577 if (mddev
->gendisk
) {
578 /* We did a probe so need to clean up. Call
579 * queue_work inside the spinlock so that
580 * flush_workqueue() after mddev_find will
581 * succeed in waiting for the work to be done.
583 INIT_WORK(&mddev
->del_work
, mddev_delayed_delete
);
584 queue_work(md_misc_wq
, &mddev
->del_work
);
588 spin_unlock(&all_mddevs_lock
);
593 void mddev_init(mddev_t
*mddev
)
595 mutex_init(&mddev
->open_mutex
);
596 mutex_init(&mddev
->reconfig_mutex
);
597 mutex_init(&mddev
->bitmap_info
.mutex
);
598 INIT_LIST_HEAD(&mddev
->disks
);
599 INIT_LIST_HEAD(&mddev
->all_mddevs
);
600 init_timer(&mddev
->safemode_timer
);
601 atomic_set(&mddev
->active
, 1);
602 atomic_set(&mddev
->openers
, 0);
603 atomic_set(&mddev
->active_io
, 0);
604 atomic_set(&mddev
->plug_cnt
, 0);
605 spin_lock_init(&mddev
->write_lock
);
606 atomic_set(&mddev
->flush_pending
, 0);
607 init_waitqueue_head(&mddev
->sb_wait
);
608 init_waitqueue_head(&mddev
->recovery_wait
);
609 mddev
->reshape_position
= MaxSector
;
610 mddev
->resync_min
= 0;
611 mddev
->resync_max
= MaxSector
;
612 mddev
->level
= LEVEL_NONE
;
614 EXPORT_SYMBOL_GPL(mddev_init
);
616 static mddev_t
* mddev_find(dev_t unit
)
618 mddev_t
*mddev
, *new = NULL
;
620 if (unit
&& MAJOR(unit
) != MD_MAJOR
)
621 unit
&= ~((1<<MdpMinorShift
)-1);
624 spin_lock(&all_mddevs_lock
);
627 list_for_each_entry(mddev
, &all_mddevs
, all_mddevs
)
628 if (mddev
->unit
== unit
) {
630 spin_unlock(&all_mddevs_lock
);
636 list_add(&new->all_mddevs
, &all_mddevs
);
637 spin_unlock(&all_mddevs_lock
);
638 new->hold_active
= UNTIL_IOCTL
;
642 /* find an unused unit number */
643 static int next_minor
= 512;
644 int start
= next_minor
;
648 dev
= MKDEV(MD_MAJOR
, next_minor
);
650 if (next_minor
> MINORMASK
)
652 if (next_minor
== start
) {
653 /* Oh dear, all in use. */
654 spin_unlock(&all_mddevs_lock
);
660 list_for_each_entry(mddev
, &all_mddevs
, all_mddevs
)
661 if (mddev
->unit
== dev
) {
667 new->md_minor
= MINOR(dev
);
668 new->hold_active
= UNTIL_STOP
;
669 list_add(&new->all_mddevs
, &all_mddevs
);
670 spin_unlock(&all_mddevs_lock
);
673 spin_unlock(&all_mddevs_lock
);
675 new = kzalloc(sizeof(*new), GFP_KERNEL
);
680 if (MAJOR(unit
) == MD_MAJOR
)
681 new->md_minor
= MINOR(unit
);
683 new->md_minor
= MINOR(unit
) >> MdpMinorShift
;
690 static inline int mddev_lock(mddev_t
* mddev
)
692 return mutex_lock_interruptible(&mddev
->reconfig_mutex
);
695 static inline int mddev_is_locked(mddev_t
*mddev
)
697 return mutex_is_locked(&mddev
->reconfig_mutex
);
700 static inline int mddev_trylock(mddev_t
* mddev
)
702 return mutex_trylock(&mddev
->reconfig_mutex
);
705 static struct attribute_group md_redundancy_group
;
707 static void mddev_unlock(mddev_t
* mddev
)
709 if (mddev
->to_remove
) {
710 /* These cannot be removed under reconfig_mutex as
711 * an access to the files will try to take reconfig_mutex
712 * while holding the file unremovable, which leads to
714 * So hold set sysfs_active while the remove in happeing,
715 * and anything else which might set ->to_remove or my
716 * otherwise change the sysfs namespace will fail with
717 * -EBUSY if sysfs_active is still set.
718 * We set sysfs_active under reconfig_mutex and elsewhere
719 * test it under the same mutex to ensure its correct value
722 struct attribute_group
*to_remove
= mddev
->to_remove
;
723 mddev
->to_remove
= NULL
;
724 mddev
->sysfs_active
= 1;
725 mutex_unlock(&mddev
->reconfig_mutex
);
727 if (mddev
->kobj
.sd
) {
728 if (to_remove
!= &md_redundancy_group
)
729 sysfs_remove_group(&mddev
->kobj
, to_remove
);
730 if (mddev
->pers
== NULL
||
731 mddev
->pers
->sync_request
== NULL
) {
732 sysfs_remove_group(&mddev
->kobj
, &md_redundancy_group
);
733 if (mddev
->sysfs_action
)
734 sysfs_put(mddev
->sysfs_action
);
735 mddev
->sysfs_action
= NULL
;
738 mddev
->sysfs_active
= 0;
740 mutex_unlock(&mddev
->reconfig_mutex
);
742 md_wakeup_thread(mddev
->thread
);
745 static mdk_rdev_t
* find_rdev_nr(mddev_t
*mddev
, int nr
)
749 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
750 if (rdev
->desc_nr
== nr
)
756 static mdk_rdev_t
* find_rdev(mddev_t
* mddev
, dev_t dev
)
760 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
761 if (rdev
->bdev
->bd_dev
== dev
)
767 static struct mdk_personality
*find_pers(int level
, char *clevel
)
769 struct mdk_personality
*pers
;
770 list_for_each_entry(pers
, &pers_list
, list
) {
771 if (level
!= LEVEL_NONE
&& pers
->level
== level
)
773 if (strcmp(pers
->name
, clevel
)==0)
779 /* return the offset of the super block in 512byte sectors */
780 static inline sector_t
calc_dev_sboffset(mdk_rdev_t
*rdev
)
782 sector_t num_sectors
= i_size_read(rdev
->bdev
->bd_inode
) / 512;
783 return MD_NEW_SIZE_SECTORS(num_sectors
);
786 static int alloc_disk_sb(mdk_rdev_t
* rdev
)
791 rdev
->sb_page
= alloc_page(GFP_KERNEL
);
792 if (!rdev
->sb_page
) {
793 printk(KERN_ALERT
"md: out of memory.\n");
800 static void free_disk_sb(mdk_rdev_t
* rdev
)
803 put_page(rdev
->sb_page
);
805 rdev
->sb_page
= NULL
;
810 put_page(rdev
->bb_page
);
811 rdev
->bb_page
= NULL
;
816 static void super_written(struct bio
*bio
, int error
)
818 mdk_rdev_t
*rdev
= bio
->bi_private
;
819 mddev_t
*mddev
= rdev
->mddev
;
821 if (error
|| !test_bit(BIO_UPTODATE
, &bio
->bi_flags
)) {
822 printk("md: super_written gets error=%d, uptodate=%d\n",
823 error
, test_bit(BIO_UPTODATE
, &bio
->bi_flags
));
824 WARN_ON(test_bit(BIO_UPTODATE
, &bio
->bi_flags
));
825 md_error(mddev
, rdev
);
828 if (atomic_dec_and_test(&mddev
->pending_writes
))
829 wake_up(&mddev
->sb_wait
);
833 void md_super_write(mddev_t
*mddev
, mdk_rdev_t
*rdev
,
834 sector_t sector
, int size
, struct page
*page
)
836 /* write first size bytes of page to sector of rdev
837 * Increment mddev->pending_writes before returning
838 * and decrement it on completion, waking up sb_wait
839 * if zero is reached.
840 * If an error occurred, call md_error
842 struct bio
*bio
= bio_alloc_mddev(GFP_NOIO
, 1, mddev
);
844 bio
->bi_bdev
= rdev
->meta_bdev
? rdev
->meta_bdev
: rdev
->bdev
;
845 bio
->bi_sector
= sector
;
846 bio_add_page(bio
, page
, size
, 0);
847 bio
->bi_private
= rdev
;
848 bio
->bi_end_io
= super_written
;
850 atomic_inc(&mddev
->pending_writes
);
851 submit_bio(WRITE_FLUSH_FUA
, bio
);
854 void md_super_wait(mddev_t
*mddev
)
856 /* wait for all superblock writes that were scheduled to complete */
859 prepare_to_wait(&mddev
->sb_wait
, &wq
, TASK_UNINTERRUPTIBLE
);
860 if (atomic_read(&mddev
->pending_writes
)==0)
864 finish_wait(&mddev
->sb_wait
, &wq
);
867 static void bi_complete(struct bio
*bio
, int error
)
869 complete((struct completion
*)bio
->bi_private
);
872 int sync_page_io(mdk_rdev_t
*rdev
, sector_t sector
, int size
,
873 struct page
*page
, int rw
, bool metadata_op
)
875 struct bio
*bio
= bio_alloc_mddev(GFP_NOIO
, 1, rdev
->mddev
);
876 struct completion event
;
881 bio
->bi_bdev
= (metadata_op
&& rdev
->meta_bdev
) ?
882 rdev
->meta_bdev
: rdev
->bdev
;
884 bio
->bi_sector
= sector
+ rdev
->sb_start
;
886 bio
->bi_sector
= sector
+ rdev
->data_offset
;
887 bio_add_page(bio
, page
, size
, 0);
888 init_completion(&event
);
889 bio
->bi_private
= &event
;
890 bio
->bi_end_io
= bi_complete
;
892 wait_for_completion(&event
);
894 ret
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
898 EXPORT_SYMBOL_GPL(sync_page_io
);
900 static int read_disk_sb(mdk_rdev_t
* rdev
, int size
)
902 char b
[BDEVNAME_SIZE
];
903 if (!rdev
->sb_page
) {
911 if (!sync_page_io(rdev
, 0, size
, rdev
->sb_page
, READ
, true))
917 printk(KERN_WARNING
"md: disabled device %s, could not read superblock.\n",
918 bdevname(rdev
->bdev
,b
));
922 static int uuid_equal(mdp_super_t
*sb1
, mdp_super_t
*sb2
)
924 return sb1
->set_uuid0
== sb2
->set_uuid0
&&
925 sb1
->set_uuid1
== sb2
->set_uuid1
&&
926 sb1
->set_uuid2
== sb2
->set_uuid2
&&
927 sb1
->set_uuid3
== sb2
->set_uuid3
;
930 static int sb_equal(mdp_super_t
*sb1
, mdp_super_t
*sb2
)
933 mdp_super_t
*tmp1
, *tmp2
;
935 tmp1
= kmalloc(sizeof(*tmp1
),GFP_KERNEL
);
936 tmp2
= kmalloc(sizeof(*tmp2
),GFP_KERNEL
);
938 if (!tmp1
|| !tmp2
) {
940 printk(KERN_INFO
"md.c sb_equal(): failed to allocate memory!\n");
948 * nr_disks is not constant
953 ret
= (memcmp(tmp1
, tmp2
, MD_SB_GENERIC_CONSTANT_WORDS
* 4) == 0);
961 static u32
md_csum_fold(u32 csum
)
963 csum
= (csum
& 0xffff) + (csum
>> 16);
964 return (csum
& 0xffff) + (csum
>> 16);
967 static unsigned int calc_sb_csum(mdp_super_t
* sb
)
970 u32
*sb32
= (u32
*)sb
;
972 unsigned int disk_csum
, csum
;
974 disk_csum
= sb
->sb_csum
;
977 for (i
= 0; i
< MD_SB_BYTES
/4 ; i
++)
979 csum
= (newcsum
& 0xffffffff) + (newcsum
>>32);
983 /* This used to use csum_partial, which was wrong for several
984 * reasons including that different results are returned on
985 * different architectures. It isn't critical that we get exactly
986 * the same return value as before (we always csum_fold before
987 * testing, and that removes any differences). However as we
988 * know that csum_partial always returned a 16bit value on
989 * alphas, do a fold to maximise conformity to previous behaviour.
991 sb
->sb_csum
= md_csum_fold(disk_csum
);
993 sb
->sb_csum
= disk_csum
;
1000 * Handle superblock details.
1001 * We want to be able to handle multiple superblock formats
1002 * so we have a common interface to them all, and an array of
1003 * different handlers.
1004 * We rely on user-space to write the initial superblock, and support
1005 * reading and updating of superblocks.
1006 * Interface methods are:
1007 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
1008 * loads and validates a superblock on dev.
1009 * if refdev != NULL, compare superblocks on both devices
1011 * 0 - dev has a superblock that is compatible with refdev
1012 * 1 - dev has a superblock that is compatible and newer than refdev
1013 * so dev should be used as the refdev in future
1014 * -EINVAL superblock incompatible or invalid
1015 * -othererror e.g. -EIO
1017 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
1018 * Verify that dev is acceptable into mddev.
1019 * The first time, mddev->raid_disks will be 0, and data from
1020 * dev should be merged in. Subsequent calls check that dev
1021 * is new enough. Return 0 or -EINVAL
1023 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
1024 * Update the superblock for rdev with data in mddev
1025 * This does not write to disc.
1031 struct module
*owner
;
1032 int (*load_super
)(mdk_rdev_t
*rdev
, mdk_rdev_t
*refdev
,
1034 int (*validate_super
)(mddev_t
*mddev
, mdk_rdev_t
*rdev
);
1035 void (*sync_super
)(mddev_t
*mddev
, mdk_rdev_t
*rdev
);
1036 unsigned long long (*rdev_size_change
)(mdk_rdev_t
*rdev
,
1037 sector_t num_sectors
);
1041 * Check that the given mddev has no bitmap.
1043 * This function is called from the run method of all personalities that do not
1044 * support bitmaps. It prints an error message and returns non-zero if mddev
1045 * has a bitmap. Otherwise, it returns 0.
1048 int md_check_no_bitmap(mddev_t
*mddev
)
1050 if (!mddev
->bitmap_info
.file
&& !mddev
->bitmap_info
.offset
)
1052 printk(KERN_ERR
"%s: bitmaps are not supported for %s\n",
1053 mdname(mddev
), mddev
->pers
->name
);
1056 EXPORT_SYMBOL(md_check_no_bitmap
);
1059 * load_super for 0.90.0
1061 static int super_90_load(mdk_rdev_t
*rdev
, mdk_rdev_t
*refdev
, int minor_version
)
1063 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
1068 * Calculate the position of the superblock (512byte sectors),
1069 * it's at the end of the disk.
1071 * It also happens to be a multiple of 4Kb.
1073 rdev
->sb_start
= calc_dev_sboffset(rdev
);
1075 ret
= read_disk_sb(rdev
, MD_SB_BYTES
);
1076 if (ret
) return ret
;
1080 bdevname(rdev
->bdev
, b
);
1081 sb
= page_address(rdev
->sb_page
);
1083 if (sb
->md_magic
!= MD_SB_MAGIC
) {
1084 printk(KERN_ERR
"md: invalid raid superblock magic on %s\n",
1089 if (sb
->major_version
!= 0 ||
1090 sb
->minor_version
< 90 ||
1091 sb
->minor_version
> 91) {
1092 printk(KERN_WARNING
"Bad version number %d.%d on %s\n",
1093 sb
->major_version
, sb
->minor_version
,
1098 if (sb
->raid_disks
<= 0)
1101 if (md_csum_fold(calc_sb_csum(sb
)) != md_csum_fold(sb
->sb_csum
)) {
1102 printk(KERN_WARNING
"md: invalid superblock checksum on %s\n",
1107 rdev
->preferred_minor
= sb
->md_minor
;
1108 rdev
->data_offset
= 0;
1109 rdev
->sb_size
= MD_SB_BYTES
;
1110 rdev
->badblocks
.shift
= -1;
1112 if (sb
->level
== LEVEL_MULTIPATH
)
1115 rdev
->desc_nr
= sb
->this_disk
.number
;
1121 mdp_super_t
*refsb
= page_address(refdev
->sb_page
);
1122 if (!uuid_equal(refsb
, sb
)) {
1123 printk(KERN_WARNING
"md: %s has different UUID to %s\n",
1124 b
, bdevname(refdev
->bdev
,b2
));
1127 if (!sb_equal(refsb
, sb
)) {
1128 printk(KERN_WARNING
"md: %s has same UUID"
1129 " but different superblock to %s\n",
1130 b
, bdevname(refdev
->bdev
, b2
));
1134 ev2
= md_event(refsb
);
1140 rdev
->sectors
= rdev
->sb_start
;
1141 /* Limit to 4TB as metadata cannot record more than that */
1142 if (rdev
->sectors
>= (2ULL << 32))
1143 rdev
->sectors
= (2ULL << 32) - 2;
1145 if (rdev
->sectors
< ((sector_t
)sb
->size
) * 2 && sb
->level
>= 1)
1146 /* "this cannot possibly happen" ... */
1154 * validate_super for 0.90.0
1156 static int super_90_validate(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1159 mdp_super_t
*sb
= page_address(rdev
->sb_page
);
1160 __u64 ev1
= md_event(sb
);
1162 rdev
->raid_disk
= -1;
1163 clear_bit(Faulty
, &rdev
->flags
);
1164 clear_bit(In_sync
, &rdev
->flags
);
1165 clear_bit(WriteMostly
, &rdev
->flags
);
1167 if (mddev
->raid_disks
== 0) {
1168 mddev
->major_version
= 0;
1169 mddev
->minor_version
= sb
->minor_version
;
1170 mddev
->patch_version
= sb
->patch_version
;
1171 mddev
->external
= 0;
1172 mddev
->chunk_sectors
= sb
->chunk_size
>> 9;
1173 mddev
->ctime
= sb
->ctime
;
1174 mddev
->utime
= sb
->utime
;
1175 mddev
->level
= sb
->level
;
1176 mddev
->clevel
[0] = 0;
1177 mddev
->layout
= sb
->layout
;
1178 mddev
->raid_disks
= sb
->raid_disks
;
1179 mddev
->dev_sectors
= ((sector_t
)sb
->size
) * 2;
1180 mddev
->events
= ev1
;
1181 mddev
->bitmap_info
.offset
= 0;
1182 mddev
->bitmap_info
.default_offset
= MD_SB_BYTES
>> 9;
1184 if (mddev
->minor_version
>= 91) {
1185 mddev
->reshape_position
= sb
->reshape_position
;
1186 mddev
->delta_disks
= sb
->delta_disks
;
1187 mddev
->new_level
= sb
->new_level
;
1188 mddev
->new_layout
= sb
->new_layout
;
1189 mddev
->new_chunk_sectors
= sb
->new_chunk
>> 9;
1191 mddev
->reshape_position
= MaxSector
;
1192 mddev
->delta_disks
= 0;
1193 mddev
->new_level
= mddev
->level
;
1194 mddev
->new_layout
= mddev
->layout
;
1195 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
1198 if (sb
->state
& (1<<MD_SB_CLEAN
))
1199 mddev
->recovery_cp
= MaxSector
;
1201 if (sb
->events_hi
== sb
->cp_events_hi
&&
1202 sb
->events_lo
== sb
->cp_events_lo
) {
1203 mddev
->recovery_cp
= sb
->recovery_cp
;
1205 mddev
->recovery_cp
= 0;
1208 memcpy(mddev
->uuid
+0, &sb
->set_uuid0
, 4);
1209 memcpy(mddev
->uuid
+4, &sb
->set_uuid1
, 4);
1210 memcpy(mddev
->uuid
+8, &sb
->set_uuid2
, 4);
1211 memcpy(mddev
->uuid
+12,&sb
->set_uuid3
, 4);
1213 mddev
->max_disks
= MD_SB_DISKS
;
1215 if (sb
->state
& (1<<MD_SB_BITMAP_PRESENT
) &&
1216 mddev
->bitmap_info
.file
== NULL
)
1217 mddev
->bitmap_info
.offset
=
1218 mddev
->bitmap_info
.default_offset
;
1220 } else if (mddev
->pers
== NULL
) {
1221 /* Insist on good event counter while assembling, except
1222 * for spares (which don't need an event count) */
1224 if (sb
->disks
[rdev
->desc_nr
].state
& (
1225 (1<<MD_DISK_SYNC
) | (1 << MD_DISK_ACTIVE
)))
1226 if (ev1
< mddev
->events
)
1228 } else if (mddev
->bitmap
) {
1229 /* if adding to array with a bitmap, then we can accept an
1230 * older device ... but not too old.
1232 if (ev1
< mddev
->bitmap
->events_cleared
)
1235 if (ev1
< mddev
->events
)
1236 /* just a hot-add of a new device, leave raid_disk at -1 */
1240 if (mddev
->level
!= LEVEL_MULTIPATH
) {
1241 desc
= sb
->disks
+ rdev
->desc_nr
;
1243 if (desc
->state
& (1<<MD_DISK_FAULTY
))
1244 set_bit(Faulty
, &rdev
->flags
);
1245 else if (desc
->state
& (1<<MD_DISK_SYNC
) /* &&
1246 desc->raid_disk < mddev->raid_disks */) {
1247 set_bit(In_sync
, &rdev
->flags
);
1248 rdev
->raid_disk
= desc
->raid_disk
;
1249 } else if (desc
->state
& (1<<MD_DISK_ACTIVE
)) {
1250 /* active but not in sync implies recovery up to
1251 * reshape position. We don't know exactly where
1252 * that is, so set to zero for now */
1253 if (mddev
->minor_version
>= 91) {
1254 rdev
->recovery_offset
= 0;
1255 rdev
->raid_disk
= desc
->raid_disk
;
1258 if (desc
->state
& (1<<MD_DISK_WRITEMOSTLY
))
1259 set_bit(WriteMostly
, &rdev
->flags
);
1260 } else /* MULTIPATH are always insync */
1261 set_bit(In_sync
, &rdev
->flags
);
1266 * sync_super for 0.90.0
1268 static void super_90_sync(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1272 int next_spare
= mddev
->raid_disks
;
1275 /* make rdev->sb match mddev data..
1278 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1279 * 3/ any empty disks < next_spare become removed
1281 * disks[0] gets initialised to REMOVED because
1282 * we cannot be sure from other fields if it has
1283 * been initialised or not.
1286 int active
=0, working
=0,failed
=0,spare
=0,nr_disks
=0;
1288 rdev
->sb_size
= MD_SB_BYTES
;
1290 sb
= page_address(rdev
->sb_page
);
1292 memset(sb
, 0, sizeof(*sb
));
1294 sb
->md_magic
= MD_SB_MAGIC
;
1295 sb
->major_version
= mddev
->major_version
;
1296 sb
->patch_version
= mddev
->patch_version
;
1297 sb
->gvalid_words
= 0; /* ignored */
1298 memcpy(&sb
->set_uuid0
, mddev
->uuid
+0, 4);
1299 memcpy(&sb
->set_uuid1
, mddev
->uuid
+4, 4);
1300 memcpy(&sb
->set_uuid2
, mddev
->uuid
+8, 4);
1301 memcpy(&sb
->set_uuid3
, mddev
->uuid
+12,4);
1303 sb
->ctime
= mddev
->ctime
;
1304 sb
->level
= mddev
->level
;
1305 sb
->size
= mddev
->dev_sectors
/ 2;
1306 sb
->raid_disks
= mddev
->raid_disks
;
1307 sb
->md_minor
= mddev
->md_minor
;
1308 sb
->not_persistent
= 0;
1309 sb
->utime
= mddev
->utime
;
1311 sb
->events_hi
= (mddev
->events
>>32);
1312 sb
->events_lo
= (u32
)mddev
->events
;
1314 if (mddev
->reshape_position
== MaxSector
)
1315 sb
->minor_version
= 90;
1317 sb
->minor_version
= 91;
1318 sb
->reshape_position
= mddev
->reshape_position
;
1319 sb
->new_level
= mddev
->new_level
;
1320 sb
->delta_disks
= mddev
->delta_disks
;
1321 sb
->new_layout
= mddev
->new_layout
;
1322 sb
->new_chunk
= mddev
->new_chunk_sectors
<< 9;
1324 mddev
->minor_version
= sb
->minor_version
;
1327 sb
->recovery_cp
= mddev
->recovery_cp
;
1328 sb
->cp_events_hi
= (mddev
->events
>>32);
1329 sb
->cp_events_lo
= (u32
)mddev
->events
;
1330 if (mddev
->recovery_cp
== MaxSector
)
1331 sb
->state
= (1<< MD_SB_CLEAN
);
1333 sb
->recovery_cp
= 0;
1335 sb
->layout
= mddev
->layout
;
1336 sb
->chunk_size
= mddev
->chunk_sectors
<< 9;
1338 if (mddev
->bitmap
&& mddev
->bitmap_info
.file
== NULL
)
1339 sb
->state
|= (1<<MD_SB_BITMAP_PRESENT
);
1341 sb
->disks
[0].state
= (1<<MD_DISK_REMOVED
);
1342 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
) {
1345 int is_active
= test_bit(In_sync
, &rdev2
->flags
);
1347 if (rdev2
->raid_disk
>= 0 &&
1348 sb
->minor_version
>= 91)
1349 /* we have nowhere to store the recovery_offset,
1350 * but if it is not below the reshape_position,
1351 * we can piggy-back on that.
1354 if (rdev2
->raid_disk
< 0 ||
1355 test_bit(Faulty
, &rdev2
->flags
))
1358 desc_nr
= rdev2
->raid_disk
;
1360 desc_nr
= next_spare
++;
1361 rdev2
->desc_nr
= desc_nr
;
1362 d
= &sb
->disks
[rdev2
->desc_nr
];
1364 d
->number
= rdev2
->desc_nr
;
1365 d
->major
= MAJOR(rdev2
->bdev
->bd_dev
);
1366 d
->minor
= MINOR(rdev2
->bdev
->bd_dev
);
1368 d
->raid_disk
= rdev2
->raid_disk
;
1370 d
->raid_disk
= rdev2
->desc_nr
; /* compatibility */
1371 if (test_bit(Faulty
, &rdev2
->flags
))
1372 d
->state
= (1<<MD_DISK_FAULTY
);
1373 else if (is_active
) {
1374 d
->state
= (1<<MD_DISK_ACTIVE
);
1375 if (test_bit(In_sync
, &rdev2
->flags
))
1376 d
->state
|= (1<<MD_DISK_SYNC
);
1384 if (test_bit(WriteMostly
, &rdev2
->flags
))
1385 d
->state
|= (1<<MD_DISK_WRITEMOSTLY
);
1387 /* now set the "removed" and "faulty" bits on any missing devices */
1388 for (i
=0 ; i
< mddev
->raid_disks
; i
++) {
1389 mdp_disk_t
*d
= &sb
->disks
[i
];
1390 if (d
->state
== 0 && d
->number
== 0) {
1393 d
->state
= (1<<MD_DISK_REMOVED
);
1394 d
->state
|= (1<<MD_DISK_FAULTY
);
1398 sb
->nr_disks
= nr_disks
;
1399 sb
->active_disks
= active
;
1400 sb
->working_disks
= working
;
1401 sb
->failed_disks
= failed
;
1402 sb
->spare_disks
= spare
;
1404 sb
->this_disk
= sb
->disks
[rdev
->desc_nr
];
1405 sb
->sb_csum
= calc_sb_csum(sb
);
1409 * rdev_size_change for 0.90.0
1411 static unsigned long long
1412 super_90_rdev_size_change(mdk_rdev_t
*rdev
, sector_t num_sectors
)
1414 if (num_sectors
&& num_sectors
< rdev
->mddev
->dev_sectors
)
1415 return 0; /* component must fit device */
1416 if (rdev
->mddev
->bitmap_info
.offset
)
1417 return 0; /* can't move bitmap */
1418 rdev
->sb_start
= calc_dev_sboffset(rdev
);
1419 if (!num_sectors
|| num_sectors
> rdev
->sb_start
)
1420 num_sectors
= rdev
->sb_start
;
1421 /* Limit to 4TB as metadata cannot record more than that.
1422 * 4TB == 2^32 KB, or 2*2^32 sectors.
1424 if (num_sectors
>= (2ULL << 32))
1425 num_sectors
= (2ULL << 32) - 2;
1426 md_super_write(rdev
->mddev
, rdev
, rdev
->sb_start
, rdev
->sb_size
,
1428 md_super_wait(rdev
->mddev
);
1434 * version 1 superblock
1437 static __le32
calc_sb_1_csum(struct mdp_superblock_1
* sb
)
1441 unsigned long long newcsum
;
1442 int size
= 256 + le32_to_cpu(sb
->max_dev
)*2;
1443 __le32
*isuper
= (__le32
*)sb
;
1446 disk_csum
= sb
->sb_csum
;
1449 for (i
=0; size
>=4; size
-= 4 )
1450 newcsum
+= le32_to_cpu(*isuper
++);
1453 newcsum
+= le16_to_cpu(*(__le16
*) isuper
);
1455 csum
= (newcsum
& 0xffffffff) + (newcsum
>> 32);
1456 sb
->sb_csum
= disk_csum
;
1457 return cpu_to_le32(csum
);
1460 static int md_set_badblocks(struct badblocks
*bb
, sector_t s
, int sectors
,
1462 static int super_1_load(mdk_rdev_t
*rdev
, mdk_rdev_t
*refdev
, int minor_version
)
1464 struct mdp_superblock_1
*sb
;
1467 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
1471 * Calculate the position of the superblock in 512byte sectors.
1472 * It is always aligned to a 4K boundary and
1473 * depeding on minor_version, it can be:
1474 * 0: At least 8K, but less than 12K, from end of device
1475 * 1: At start of device
1476 * 2: 4K from start of device.
1478 switch(minor_version
) {
1480 sb_start
= i_size_read(rdev
->bdev
->bd_inode
) >> 9;
1482 sb_start
&= ~(sector_t
)(4*2-1);
1493 rdev
->sb_start
= sb_start
;
1495 /* superblock is rarely larger than 1K, but it can be larger,
1496 * and it is safe to read 4k, so we do that
1498 ret
= read_disk_sb(rdev
, 4096);
1499 if (ret
) return ret
;
1502 sb
= page_address(rdev
->sb_page
);
1504 if (sb
->magic
!= cpu_to_le32(MD_SB_MAGIC
) ||
1505 sb
->major_version
!= cpu_to_le32(1) ||
1506 le32_to_cpu(sb
->max_dev
) > (4096-256)/2 ||
1507 le64_to_cpu(sb
->super_offset
) != rdev
->sb_start
||
1508 (le32_to_cpu(sb
->feature_map
) & ~MD_FEATURE_ALL
) != 0)
1511 if (calc_sb_1_csum(sb
) != sb
->sb_csum
) {
1512 printk("md: invalid superblock checksum on %s\n",
1513 bdevname(rdev
->bdev
,b
));
1516 if (le64_to_cpu(sb
->data_size
) < 10) {
1517 printk("md: data_size too small on %s\n",
1518 bdevname(rdev
->bdev
,b
));
1522 rdev
->preferred_minor
= 0xffff;
1523 rdev
->data_offset
= le64_to_cpu(sb
->data_offset
);
1524 atomic_set(&rdev
->corrected_errors
, le32_to_cpu(sb
->cnt_corrected_read
));
1526 rdev
->sb_size
= le32_to_cpu(sb
->max_dev
) * 2 + 256;
1527 bmask
= queue_logical_block_size(rdev
->bdev
->bd_disk
->queue
)-1;
1528 if (rdev
->sb_size
& bmask
)
1529 rdev
->sb_size
= (rdev
->sb_size
| bmask
) + 1;
1532 && rdev
->data_offset
< sb_start
+ (rdev
->sb_size
/512))
1535 if (sb
->level
== cpu_to_le32(LEVEL_MULTIPATH
))
1538 rdev
->desc_nr
= le32_to_cpu(sb
->dev_number
);
1540 if (!rdev
->bb_page
) {
1541 rdev
->bb_page
= alloc_page(GFP_KERNEL
);
1545 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_BAD_BLOCKS
) &&
1546 rdev
->badblocks
.count
== 0) {
1547 /* need to load the bad block list.
1548 * Currently we limit it to one page.
1554 int sectors
= le16_to_cpu(sb
->bblog_size
);
1555 if (sectors
> (PAGE_SIZE
/ 512))
1557 offset
= le32_to_cpu(sb
->bblog_offset
);
1560 bb_sector
= (long long)offset
;
1561 if (!sync_page_io(rdev
, bb_sector
, sectors
<< 9,
1562 rdev
->bb_page
, READ
, true))
1564 bbp
= (u64
*)page_address(rdev
->bb_page
);
1565 rdev
->badblocks
.shift
= sb
->bblog_shift
;
1566 for (i
= 0 ; i
< (sectors
<< (9-3)) ; i
++, bbp
++) {
1567 u64 bb
= le64_to_cpu(*bbp
);
1568 int count
= bb
& (0x3ff);
1569 u64 sector
= bb
>> 10;
1570 sector
<<= sb
->bblog_shift
;
1571 count
<<= sb
->bblog_shift
;
1574 if (md_set_badblocks(&rdev
->badblocks
,
1575 sector
, count
, 1) == 0)
1578 } else if (sb
->bblog_offset
== 0)
1579 rdev
->badblocks
.shift
= -1;
1585 struct mdp_superblock_1
*refsb
= page_address(refdev
->sb_page
);
1587 if (memcmp(sb
->set_uuid
, refsb
->set_uuid
, 16) != 0 ||
1588 sb
->level
!= refsb
->level
||
1589 sb
->layout
!= refsb
->layout
||
1590 sb
->chunksize
!= refsb
->chunksize
) {
1591 printk(KERN_WARNING
"md: %s has strangely different"
1592 " superblock to %s\n",
1593 bdevname(rdev
->bdev
,b
),
1594 bdevname(refdev
->bdev
,b2
));
1597 ev1
= le64_to_cpu(sb
->events
);
1598 ev2
= le64_to_cpu(refsb
->events
);
1606 rdev
->sectors
= (i_size_read(rdev
->bdev
->bd_inode
) >> 9) -
1607 le64_to_cpu(sb
->data_offset
);
1609 rdev
->sectors
= rdev
->sb_start
;
1610 if (rdev
->sectors
< le64_to_cpu(sb
->data_size
))
1612 rdev
->sectors
= le64_to_cpu(sb
->data_size
);
1613 if (le64_to_cpu(sb
->size
) > rdev
->sectors
)
1618 static int super_1_validate(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1620 struct mdp_superblock_1
*sb
= page_address(rdev
->sb_page
);
1621 __u64 ev1
= le64_to_cpu(sb
->events
);
1623 rdev
->raid_disk
= -1;
1624 clear_bit(Faulty
, &rdev
->flags
);
1625 clear_bit(In_sync
, &rdev
->flags
);
1626 clear_bit(WriteMostly
, &rdev
->flags
);
1628 if (mddev
->raid_disks
== 0) {
1629 mddev
->major_version
= 1;
1630 mddev
->patch_version
= 0;
1631 mddev
->external
= 0;
1632 mddev
->chunk_sectors
= le32_to_cpu(sb
->chunksize
);
1633 mddev
->ctime
= le64_to_cpu(sb
->ctime
) & ((1ULL << 32)-1);
1634 mddev
->utime
= le64_to_cpu(sb
->utime
) & ((1ULL << 32)-1);
1635 mddev
->level
= le32_to_cpu(sb
->level
);
1636 mddev
->clevel
[0] = 0;
1637 mddev
->layout
= le32_to_cpu(sb
->layout
);
1638 mddev
->raid_disks
= le32_to_cpu(sb
->raid_disks
);
1639 mddev
->dev_sectors
= le64_to_cpu(sb
->size
);
1640 mddev
->events
= ev1
;
1641 mddev
->bitmap_info
.offset
= 0;
1642 mddev
->bitmap_info
.default_offset
= 1024 >> 9;
1644 mddev
->recovery_cp
= le64_to_cpu(sb
->resync_offset
);
1645 memcpy(mddev
->uuid
, sb
->set_uuid
, 16);
1647 mddev
->max_disks
= (4096-256)/2;
1649 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_BITMAP_OFFSET
) &&
1650 mddev
->bitmap_info
.file
== NULL
)
1651 mddev
->bitmap_info
.offset
=
1652 (__s32
)le32_to_cpu(sb
->bitmap_offset
);
1654 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RESHAPE_ACTIVE
)) {
1655 mddev
->reshape_position
= le64_to_cpu(sb
->reshape_position
);
1656 mddev
->delta_disks
= le32_to_cpu(sb
->delta_disks
);
1657 mddev
->new_level
= le32_to_cpu(sb
->new_level
);
1658 mddev
->new_layout
= le32_to_cpu(sb
->new_layout
);
1659 mddev
->new_chunk_sectors
= le32_to_cpu(sb
->new_chunk
);
1661 mddev
->reshape_position
= MaxSector
;
1662 mddev
->delta_disks
= 0;
1663 mddev
->new_level
= mddev
->level
;
1664 mddev
->new_layout
= mddev
->layout
;
1665 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
1668 } else if (mddev
->pers
== NULL
) {
1669 /* Insist of good event counter while assembling, except for
1670 * spares (which don't need an event count) */
1672 if (rdev
->desc_nr
>= 0 &&
1673 rdev
->desc_nr
< le32_to_cpu(sb
->max_dev
) &&
1674 le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]) < 0xfffe)
1675 if (ev1
< mddev
->events
)
1677 } else if (mddev
->bitmap
) {
1678 /* If adding to array with a bitmap, then we can accept an
1679 * older device, but not too old.
1681 if (ev1
< mddev
->bitmap
->events_cleared
)
1684 if (ev1
< mddev
->events
)
1685 /* just a hot-add of a new device, leave raid_disk at -1 */
1688 if (mddev
->level
!= LEVEL_MULTIPATH
) {
1690 if (rdev
->desc_nr
< 0 ||
1691 rdev
->desc_nr
>= le32_to_cpu(sb
->max_dev
)) {
1695 role
= le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]);
1697 case 0xffff: /* spare */
1699 case 0xfffe: /* faulty */
1700 set_bit(Faulty
, &rdev
->flags
);
1703 if ((le32_to_cpu(sb
->feature_map
) &
1704 MD_FEATURE_RECOVERY_OFFSET
))
1705 rdev
->recovery_offset
= le64_to_cpu(sb
->recovery_offset
);
1707 set_bit(In_sync
, &rdev
->flags
);
1708 rdev
->raid_disk
= role
;
1711 if (sb
->devflags
& WriteMostly1
)
1712 set_bit(WriteMostly
, &rdev
->flags
);
1713 } else /* MULTIPATH are always insync */
1714 set_bit(In_sync
, &rdev
->flags
);
1719 static void super_1_sync(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1721 struct mdp_superblock_1
*sb
;
1724 /* make rdev->sb match mddev and rdev data. */
1726 sb
= page_address(rdev
->sb_page
);
1728 sb
->feature_map
= 0;
1730 sb
->recovery_offset
= cpu_to_le64(0);
1731 memset(sb
->pad1
, 0, sizeof(sb
->pad1
));
1732 memset(sb
->pad3
, 0, sizeof(sb
->pad3
));
1734 sb
->utime
= cpu_to_le64((__u64
)mddev
->utime
);
1735 sb
->events
= cpu_to_le64(mddev
->events
);
1737 sb
->resync_offset
= cpu_to_le64(mddev
->recovery_cp
);
1739 sb
->resync_offset
= cpu_to_le64(0);
1741 sb
->cnt_corrected_read
= cpu_to_le32(atomic_read(&rdev
->corrected_errors
));
1743 sb
->raid_disks
= cpu_to_le32(mddev
->raid_disks
);
1744 sb
->size
= cpu_to_le64(mddev
->dev_sectors
);
1745 sb
->chunksize
= cpu_to_le32(mddev
->chunk_sectors
);
1746 sb
->level
= cpu_to_le32(mddev
->level
);
1747 sb
->layout
= cpu_to_le32(mddev
->layout
);
1749 if (test_bit(WriteMostly
, &rdev
->flags
))
1750 sb
->devflags
|= WriteMostly1
;
1752 sb
->devflags
&= ~WriteMostly1
;
1754 if (mddev
->bitmap
&& mddev
->bitmap_info
.file
== NULL
) {
1755 sb
->bitmap_offset
= cpu_to_le32((__u32
)mddev
->bitmap_info
.offset
);
1756 sb
->feature_map
= cpu_to_le32(MD_FEATURE_BITMAP_OFFSET
);
1759 if (rdev
->raid_disk
>= 0 &&
1760 !test_bit(In_sync
, &rdev
->flags
)) {
1762 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET
);
1763 sb
->recovery_offset
=
1764 cpu_to_le64(rdev
->recovery_offset
);
1767 if (mddev
->reshape_position
!= MaxSector
) {
1768 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE
);
1769 sb
->reshape_position
= cpu_to_le64(mddev
->reshape_position
);
1770 sb
->new_layout
= cpu_to_le32(mddev
->new_layout
);
1771 sb
->delta_disks
= cpu_to_le32(mddev
->delta_disks
);
1772 sb
->new_level
= cpu_to_le32(mddev
->new_level
);
1773 sb
->new_chunk
= cpu_to_le32(mddev
->new_chunk_sectors
);
1776 if (rdev
->badblocks
.count
== 0)
1777 /* Nothing to do for bad blocks*/ ;
1778 else if (sb
->bblog_offset
== 0)
1779 /* Cannot record bad blocks on this device */
1780 md_error(mddev
, rdev
);
1782 struct badblocks
*bb
= &rdev
->badblocks
;
1783 u64
*bbp
= (u64
*)page_address(rdev
->bb_page
);
1785 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_BAD_BLOCKS
);
1790 seq
= read_seqbegin(&bb
->lock
);
1792 memset(bbp
, 0xff, PAGE_SIZE
);
1794 for (i
= 0 ; i
< bb
->count
; i
++) {
1795 u64 internal_bb
= *p
++;
1796 u64 store_bb
= ((BB_OFFSET(internal_bb
) << 10)
1797 | BB_LEN(internal_bb
));
1798 *bbp
++ = cpu_to_le64(store_bb
);
1800 if (read_seqretry(&bb
->lock
, seq
))
1803 bb
->sector
= (rdev
->sb_start
+
1804 (int)le32_to_cpu(sb
->bblog_offset
));
1805 bb
->size
= le16_to_cpu(sb
->bblog_size
);
1811 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
)
1812 if (rdev2
->desc_nr
+1 > max_dev
)
1813 max_dev
= rdev2
->desc_nr
+1;
1815 if (max_dev
> le32_to_cpu(sb
->max_dev
)) {
1817 sb
->max_dev
= cpu_to_le32(max_dev
);
1818 rdev
->sb_size
= max_dev
* 2 + 256;
1819 bmask
= queue_logical_block_size(rdev
->bdev
->bd_disk
->queue
)-1;
1820 if (rdev
->sb_size
& bmask
)
1821 rdev
->sb_size
= (rdev
->sb_size
| bmask
) + 1;
1823 max_dev
= le32_to_cpu(sb
->max_dev
);
1825 for (i
=0; i
<max_dev
;i
++)
1826 sb
->dev_roles
[i
] = cpu_to_le16(0xfffe);
1828 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
) {
1830 if (test_bit(Faulty
, &rdev2
->flags
))
1831 sb
->dev_roles
[i
] = cpu_to_le16(0xfffe);
1832 else if (test_bit(In_sync
, &rdev2
->flags
))
1833 sb
->dev_roles
[i
] = cpu_to_le16(rdev2
->raid_disk
);
1834 else if (rdev2
->raid_disk
>= 0)
1835 sb
->dev_roles
[i
] = cpu_to_le16(rdev2
->raid_disk
);
1837 sb
->dev_roles
[i
] = cpu_to_le16(0xffff);
1840 sb
->sb_csum
= calc_sb_1_csum(sb
);
1843 static unsigned long long
1844 super_1_rdev_size_change(mdk_rdev_t
*rdev
, sector_t num_sectors
)
1846 struct mdp_superblock_1
*sb
;
1847 sector_t max_sectors
;
1848 if (num_sectors
&& num_sectors
< rdev
->mddev
->dev_sectors
)
1849 return 0; /* component must fit device */
1850 if (rdev
->sb_start
< rdev
->data_offset
) {
1851 /* minor versions 1 and 2; superblock before data */
1852 max_sectors
= i_size_read(rdev
->bdev
->bd_inode
) >> 9;
1853 max_sectors
-= rdev
->data_offset
;
1854 if (!num_sectors
|| num_sectors
> max_sectors
)
1855 num_sectors
= max_sectors
;
1856 } else if (rdev
->mddev
->bitmap_info
.offset
) {
1857 /* minor version 0 with bitmap we can't move */
1860 /* minor version 0; superblock after data */
1862 sb_start
= (i_size_read(rdev
->bdev
->bd_inode
) >> 9) - 8*2;
1863 sb_start
&= ~(sector_t
)(4*2 - 1);
1864 max_sectors
= rdev
->sectors
+ sb_start
- rdev
->sb_start
;
1865 if (!num_sectors
|| num_sectors
> max_sectors
)
1866 num_sectors
= max_sectors
;
1867 rdev
->sb_start
= sb_start
;
1869 sb
= page_address(rdev
->sb_page
);
1870 sb
->data_size
= cpu_to_le64(num_sectors
);
1871 sb
->super_offset
= rdev
->sb_start
;
1872 sb
->sb_csum
= calc_sb_1_csum(sb
);
1873 md_super_write(rdev
->mddev
, rdev
, rdev
->sb_start
, rdev
->sb_size
,
1875 md_super_wait(rdev
->mddev
);
1879 static struct super_type super_types
[] = {
1882 .owner
= THIS_MODULE
,
1883 .load_super
= super_90_load
,
1884 .validate_super
= super_90_validate
,
1885 .sync_super
= super_90_sync
,
1886 .rdev_size_change
= super_90_rdev_size_change
,
1890 .owner
= THIS_MODULE
,
1891 .load_super
= super_1_load
,
1892 .validate_super
= super_1_validate
,
1893 .sync_super
= super_1_sync
,
1894 .rdev_size_change
= super_1_rdev_size_change
,
1898 static void sync_super(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1900 if (mddev
->sync_super
) {
1901 mddev
->sync_super(mddev
, rdev
);
1905 BUG_ON(mddev
->major_version
>= ARRAY_SIZE(super_types
));
1907 super_types
[mddev
->major_version
].sync_super(mddev
, rdev
);
1910 static int match_mddev_units(mddev_t
*mddev1
, mddev_t
*mddev2
)
1912 mdk_rdev_t
*rdev
, *rdev2
;
1915 rdev_for_each_rcu(rdev
, mddev1
)
1916 rdev_for_each_rcu(rdev2
, mddev2
)
1917 if (rdev
->bdev
->bd_contains
==
1918 rdev2
->bdev
->bd_contains
) {
1926 static LIST_HEAD(pending_raid_disks
);
1929 * Try to register data integrity profile for an mddev
1931 * This is called when an array is started and after a disk has been kicked
1932 * from the array. It only succeeds if all working and active component devices
1933 * are integrity capable with matching profiles.
1935 int md_integrity_register(mddev_t
*mddev
)
1937 mdk_rdev_t
*rdev
, *reference
= NULL
;
1939 if (list_empty(&mddev
->disks
))
1940 return 0; /* nothing to do */
1941 if (!mddev
->gendisk
|| blk_get_integrity(mddev
->gendisk
))
1942 return 0; /* shouldn't register, or already is */
1943 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
1944 /* skip spares and non-functional disks */
1945 if (test_bit(Faulty
, &rdev
->flags
))
1947 if (rdev
->raid_disk
< 0)
1950 /* Use the first rdev as the reference */
1954 /* does this rdev's profile match the reference profile? */
1955 if (blk_integrity_compare(reference
->bdev
->bd_disk
,
1956 rdev
->bdev
->bd_disk
) < 0)
1959 if (!reference
|| !bdev_get_integrity(reference
->bdev
))
1962 * All component devices are integrity capable and have matching
1963 * profiles, register the common profile for the md device.
1965 if (blk_integrity_register(mddev
->gendisk
,
1966 bdev_get_integrity(reference
->bdev
)) != 0) {
1967 printk(KERN_ERR
"md: failed to register integrity for %s\n",
1971 printk(KERN_NOTICE
"md: data integrity enabled on %s\n", mdname(mddev
));
1972 if (bioset_integrity_create(mddev
->bio_set
, BIO_POOL_SIZE
)) {
1973 printk(KERN_ERR
"md: failed to create integrity pool for %s\n",
1979 EXPORT_SYMBOL(md_integrity_register
);
1981 /* Disable data integrity if non-capable/non-matching disk is being added */
1982 void md_integrity_add_rdev(mdk_rdev_t
*rdev
, mddev_t
*mddev
)
1984 struct blk_integrity
*bi_rdev
= bdev_get_integrity(rdev
->bdev
);
1985 struct blk_integrity
*bi_mddev
= blk_get_integrity(mddev
->gendisk
);
1987 if (!bi_mddev
) /* nothing to do */
1989 if (rdev
->raid_disk
< 0) /* skip spares */
1991 if (bi_rdev
&& blk_integrity_compare(mddev
->gendisk
,
1992 rdev
->bdev
->bd_disk
) >= 0)
1994 printk(KERN_NOTICE
"disabling data integrity on %s\n", mdname(mddev
));
1995 blk_integrity_unregister(mddev
->gendisk
);
1997 EXPORT_SYMBOL(md_integrity_add_rdev
);
1999 static int bind_rdev_to_array(mdk_rdev_t
* rdev
, mddev_t
* mddev
)
2001 char b
[BDEVNAME_SIZE
];
2011 /* prevent duplicates */
2012 if (find_rdev(mddev
, rdev
->bdev
->bd_dev
))
2015 /* make sure rdev->sectors exceeds mddev->dev_sectors */
2016 if (rdev
->sectors
&& (mddev
->dev_sectors
== 0 ||
2017 rdev
->sectors
< mddev
->dev_sectors
)) {
2019 /* Cannot change size, so fail
2020 * If mddev->level <= 0, then we don't care
2021 * about aligning sizes (e.g. linear)
2023 if (mddev
->level
> 0)
2026 mddev
->dev_sectors
= rdev
->sectors
;
2029 /* Verify rdev->desc_nr is unique.
2030 * If it is -1, assign a free number, else
2031 * check number is not in use
2033 if (rdev
->desc_nr
< 0) {
2035 if (mddev
->pers
) choice
= mddev
->raid_disks
;
2036 while (find_rdev_nr(mddev
, choice
))
2038 rdev
->desc_nr
= choice
;
2040 if (find_rdev_nr(mddev
, rdev
->desc_nr
))
2043 if (mddev
->max_disks
&& rdev
->desc_nr
>= mddev
->max_disks
) {
2044 printk(KERN_WARNING
"md: %s: array is limited to %d devices\n",
2045 mdname(mddev
), mddev
->max_disks
);
2048 bdevname(rdev
->bdev
,b
);
2049 while ( (s
=strchr(b
, '/')) != NULL
)
2052 rdev
->mddev
= mddev
;
2053 printk(KERN_INFO
"md: bind<%s>\n", b
);
2055 if ((err
= kobject_add(&rdev
->kobj
, &mddev
->kobj
, "dev-%s", b
)))
2058 ko
= &part_to_dev(rdev
->bdev
->bd_part
)->kobj
;
2059 if (sysfs_create_link(&rdev
->kobj
, ko
, "block"))
2060 /* failure here is OK */;
2061 rdev
->sysfs_state
= sysfs_get_dirent_safe(rdev
->kobj
.sd
, "state");
2063 list_add_rcu(&rdev
->same_set
, &mddev
->disks
);
2064 bd_link_disk_holder(rdev
->bdev
, mddev
->gendisk
);
2066 /* May as well allow recovery to be retried once */
2067 mddev
->recovery_disabled
++;
2072 printk(KERN_WARNING
"md: failed to register dev-%s for %s\n",
2077 static void md_delayed_delete(struct work_struct
*ws
)
2079 mdk_rdev_t
*rdev
= container_of(ws
, mdk_rdev_t
, del_work
);
2080 kobject_del(&rdev
->kobj
);
2081 kobject_put(&rdev
->kobj
);
2084 static void unbind_rdev_from_array(mdk_rdev_t
* rdev
)
2086 char b
[BDEVNAME_SIZE
];
2091 bd_unlink_disk_holder(rdev
->bdev
, rdev
->mddev
->gendisk
);
2092 list_del_rcu(&rdev
->same_set
);
2093 printk(KERN_INFO
"md: unbind<%s>\n", bdevname(rdev
->bdev
,b
));
2095 sysfs_remove_link(&rdev
->kobj
, "block");
2096 sysfs_put(rdev
->sysfs_state
);
2097 rdev
->sysfs_state
= NULL
;
2098 kfree(rdev
->badblocks
.page
);
2099 rdev
->badblocks
.count
= 0;
2100 rdev
->badblocks
.page
= NULL
;
2101 /* We need to delay this, otherwise we can deadlock when
2102 * writing to 'remove' to "dev/state". We also need
2103 * to delay it due to rcu usage.
2106 INIT_WORK(&rdev
->del_work
, md_delayed_delete
);
2107 kobject_get(&rdev
->kobj
);
2108 queue_work(md_misc_wq
, &rdev
->del_work
);
2112 * prevent the device from being mounted, repartitioned or
2113 * otherwise reused by a RAID array (or any other kernel
2114 * subsystem), by bd_claiming the device.
2116 static int lock_rdev(mdk_rdev_t
*rdev
, dev_t dev
, int shared
)
2119 struct block_device
*bdev
;
2120 char b
[BDEVNAME_SIZE
];
2122 bdev
= blkdev_get_by_dev(dev
, FMODE_READ
|FMODE_WRITE
|FMODE_EXCL
,
2123 shared
? (mdk_rdev_t
*)lock_rdev
: rdev
);
2125 printk(KERN_ERR
"md: could not open %s.\n",
2126 __bdevname(dev
, b
));
2127 return PTR_ERR(bdev
);
2133 static void unlock_rdev(mdk_rdev_t
*rdev
)
2135 struct block_device
*bdev
= rdev
->bdev
;
2139 blkdev_put(bdev
, FMODE_READ
|FMODE_WRITE
|FMODE_EXCL
);
2142 void md_autodetect_dev(dev_t dev
);
2144 static void export_rdev(mdk_rdev_t
* rdev
)
2146 char b
[BDEVNAME_SIZE
];
2147 printk(KERN_INFO
"md: export_rdev(%s)\n",
2148 bdevname(rdev
->bdev
,b
));
2153 if (test_bit(AutoDetected
, &rdev
->flags
))
2154 md_autodetect_dev(rdev
->bdev
->bd_dev
);
2157 kobject_put(&rdev
->kobj
);
2160 static void kick_rdev_from_array(mdk_rdev_t
* rdev
)
2162 unbind_rdev_from_array(rdev
);
2166 static void export_array(mddev_t
*mddev
)
2168 mdk_rdev_t
*rdev
, *tmp
;
2170 rdev_for_each(rdev
, tmp
, mddev
) {
2175 kick_rdev_from_array(rdev
);
2177 if (!list_empty(&mddev
->disks
))
2179 mddev
->raid_disks
= 0;
2180 mddev
->major_version
= 0;
2183 static void print_desc(mdp_disk_t
*desc
)
2185 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc
->number
,
2186 desc
->major
,desc
->minor
,desc
->raid_disk
,desc
->state
);
2189 static void print_sb_90(mdp_super_t
*sb
)
2194 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
2195 sb
->major_version
, sb
->minor_version
, sb
->patch_version
,
2196 sb
->set_uuid0
, sb
->set_uuid1
, sb
->set_uuid2
, sb
->set_uuid3
,
2198 printk(KERN_INFO
"md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
2199 sb
->level
, sb
->size
, sb
->nr_disks
, sb
->raid_disks
,
2200 sb
->md_minor
, sb
->layout
, sb
->chunk_size
);
2201 printk(KERN_INFO
"md: UT:%08x ST:%d AD:%d WD:%d"
2202 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
2203 sb
->utime
, sb
->state
, sb
->active_disks
, sb
->working_disks
,
2204 sb
->failed_disks
, sb
->spare_disks
,
2205 sb
->sb_csum
, (unsigned long)sb
->events_lo
);
2208 for (i
= 0; i
< MD_SB_DISKS
; i
++) {
2211 desc
= sb
->disks
+ i
;
2212 if (desc
->number
|| desc
->major
|| desc
->minor
||
2213 desc
->raid_disk
|| (desc
->state
&& (desc
->state
!= 4))) {
2214 printk(" D %2d: ", i
);
2218 printk(KERN_INFO
"md: THIS: ");
2219 print_desc(&sb
->this_disk
);
2222 static void print_sb_1(struct mdp_superblock_1
*sb
)
2226 uuid
= sb
->set_uuid
;
2228 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n"
2229 "md: Name: \"%s\" CT:%llu\n",
2230 le32_to_cpu(sb
->major_version
),
2231 le32_to_cpu(sb
->feature_map
),
2234 (unsigned long long)le64_to_cpu(sb
->ctime
)
2235 & MD_SUPERBLOCK_1_TIME_SEC_MASK
);
2237 uuid
= sb
->device_uuid
;
2239 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
2241 "md: Dev:%08x UUID: %pU\n"
2242 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
2243 "md: (MaxDev:%u) \n",
2244 le32_to_cpu(sb
->level
),
2245 (unsigned long long)le64_to_cpu(sb
->size
),
2246 le32_to_cpu(sb
->raid_disks
),
2247 le32_to_cpu(sb
->layout
),
2248 le32_to_cpu(sb
->chunksize
),
2249 (unsigned long long)le64_to_cpu(sb
->data_offset
),
2250 (unsigned long long)le64_to_cpu(sb
->data_size
),
2251 (unsigned long long)le64_to_cpu(sb
->super_offset
),
2252 (unsigned long long)le64_to_cpu(sb
->recovery_offset
),
2253 le32_to_cpu(sb
->dev_number
),
2256 (unsigned long long)le64_to_cpu(sb
->utime
) & MD_SUPERBLOCK_1_TIME_SEC_MASK
,
2257 (unsigned long long)le64_to_cpu(sb
->events
),
2258 (unsigned long long)le64_to_cpu(sb
->resync_offset
),
2259 le32_to_cpu(sb
->sb_csum
),
2260 le32_to_cpu(sb
->max_dev
)
2264 static void print_rdev(mdk_rdev_t
*rdev
, int major_version
)
2266 char b
[BDEVNAME_SIZE
];
2267 printk(KERN_INFO
"md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
2268 bdevname(rdev
->bdev
, b
), (unsigned long long)rdev
->sectors
,
2269 test_bit(Faulty
, &rdev
->flags
), test_bit(In_sync
, &rdev
->flags
),
2271 if (rdev
->sb_loaded
) {
2272 printk(KERN_INFO
"md: rdev superblock (MJ:%d):\n", major_version
);
2273 switch (major_version
) {
2275 print_sb_90(page_address(rdev
->sb_page
));
2278 print_sb_1(page_address(rdev
->sb_page
));
2282 printk(KERN_INFO
"md: no rdev superblock!\n");
2285 static void md_print_devices(void)
2287 struct list_head
*tmp
;
2290 char b
[BDEVNAME_SIZE
];
2293 printk("md: **********************************\n");
2294 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
2295 printk("md: **********************************\n");
2296 for_each_mddev(mddev
, tmp
) {
2299 bitmap_print_sb(mddev
->bitmap
);
2301 printk("%s: ", mdname(mddev
));
2302 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
2303 printk("<%s>", bdevname(rdev
->bdev
,b
));
2306 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
2307 print_rdev(rdev
, mddev
->major_version
);
2309 printk("md: **********************************\n");
2314 static void sync_sbs(mddev_t
* mddev
, int nospares
)
2316 /* Update each superblock (in-memory image), but
2317 * if we are allowed to, skip spares which already
2318 * have the right event counter, or have one earlier
2319 * (which would mean they aren't being marked as dirty
2320 * with the rest of the array)
2323 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
2324 if (rdev
->sb_events
== mddev
->events
||
2326 rdev
->raid_disk
< 0 &&
2327 rdev
->sb_events
+1 == mddev
->events
)) {
2328 /* Don't update this superblock */
2329 rdev
->sb_loaded
= 2;
2331 sync_super(mddev
, rdev
);
2332 rdev
->sb_loaded
= 1;
2337 static void md_update_sb(mddev_t
* mddev
, int force_change
)
2342 int any_badblocks_changed
= 0;
2345 /* First make sure individual recovery_offsets are correct */
2346 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
2347 if (rdev
->raid_disk
>= 0 &&
2348 mddev
->delta_disks
>= 0 &&
2349 !test_bit(In_sync
, &rdev
->flags
) &&
2350 mddev
->curr_resync_completed
> rdev
->recovery_offset
)
2351 rdev
->recovery_offset
= mddev
->curr_resync_completed
;
2354 if (!mddev
->persistent
) {
2355 clear_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
2356 clear_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
2357 if (!mddev
->external
) {
2358 clear_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
2359 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
2360 if (rdev
->badblocks
.changed
) {
2361 md_ack_all_badblocks(&rdev
->badblocks
);
2362 md_error(mddev
, rdev
);
2364 clear_bit(Blocked
, &rdev
->flags
);
2365 clear_bit(BlockedBadBlocks
, &rdev
->flags
);
2366 wake_up(&rdev
->blocked_wait
);
2369 wake_up(&mddev
->sb_wait
);
2373 spin_lock_irq(&mddev
->write_lock
);
2375 mddev
->utime
= get_seconds();
2377 if (test_and_clear_bit(MD_CHANGE_DEVS
, &mddev
->flags
))
2379 if (test_and_clear_bit(MD_CHANGE_CLEAN
, &mddev
->flags
))
2380 /* just a clean<-> dirty transition, possibly leave spares alone,
2381 * though if events isn't the right even/odd, we will have to do
2387 if (mddev
->degraded
)
2388 /* If the array is degraded, then skipping spares is both
2389 * dangerous and fairly pointless.
2390 * Dangerous because a device that was removed from the array
2391 * might have a event_count that still looks up-to-date,
2392 * so it can be re-added without a resync.
2393 * Pointless because if there are any spares to skip,
2394 * then a recovery will happen and soon that array won't
2395 * be degraded any more and the spare can go back to sleep then.
2399 sync_req
= mddev
->in_sync
;
2401 /* If this is just a dirty<->clean transition, and the array is clean
2402 * and 'events' is odd, we can roll back to the previous clean state */
2404 && (mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
)
2405 && mddev
->can_decrease_events
2406 && mddev
->events
!= 1) {
2408 mddev
->can_decrease_events
= 0;
2410 /* otherwise we have to go forward and ... */
2412 mddev
->can_decrease_events
= nospares
;
2415 if (!mddev
->events
) {
2417 * oops, this 64-bit counter should never wrap.
2418 * Either we are in around ~1 trillion A.C., assuming
2419 * 1 reboot per second, or we have a bug:
2425 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
2426 if (rdev
->badblocks
.changed
)
2427 any_badblocks_changed
++;
2428 if (test_bit(Faulty
, &rdev
->flags
))
2429 set_bit(FaultRecorded
, &rdev
->flags
);
2432 sync_sbs(mddev
, nospares
);
2433 spin_unlock_irq(&mddev
->write_lock
);
2436 "md: updating %s RAID superblock on device (in sync %d)\n",
2437 mdname(mddev
),mddev
->in_sync
);
2439 bitmap_update_sb(mddev
->bitmap
);
2440 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
2441 char b
[BDEVNAME_SIZE
];
2442 dprintk(KERN_INFO
"md: ");
2443 if (rdev
->sb_loaded
!= 1)
2444 continue; /* no noise on spare devices */
2445 if (test_bit(Faulty
, &rdev
->flags
))
2446 dprintk("(skipping faulty ");
2448 dprintk("%s ", bdevname(rdev
->bdev
,b
));
2449 if (!test_bit(Faulty
, &rdev
->flags
)) {
2450 md_super_write(mddev
,rdev
,
2451 rdev
->sb_start
, rdev
->sb_size
,
2453 dprintk(KERN_INFO
"(write) %s's sb offset: %llu\n",
2454 bdevname(rdev
->bdev
,b
),
2455 (unsigned long long)rdev
->sb_start
);
2456 rdev
->sb_events
= mddev
->events
;
2457 if (rdev
->badblocks
.size
) {
2458 md_super_write(mddev
, rdev
,
2459 rdev
->badblocks
.sector
,
2460 rdev
->badblocks
.size
<< 9,
2462 rdev
->badblocks
.size
= 0;
2467 if (mddev
->level
== LEVEL_MULTIPATH
)
2468 /* only need to write one superblock... */
2471 md_super_wait(mddev
);
2472 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
2474 spin_lock_irq(&mddev
->write_lock
);
2475 if (mddev
->in_sync
!= sync_req
||
2476 test_bit(MD_CHANGE_DEVS
, &mddev
->flags
)) {
2477 /* have to write it out again */
2478 spin_unlock_irq(&mddev
->write_lock
);
2481 clear_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
2482 spin_unlock_irq(&mddev
->write_lock
);
2483 wake_up(&mddev
->sb_wait
);
2484 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
2485 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
2487 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
2488 if (test_and_clear_bit(FaultRecorded
, &rdev
->flags
))
2489 clear_bit(Blocked
, &rdev
->flags
);
2491 if (any_badblocks_changed
)
2492 md_ack_all_badblocks(&rdev
->badblocks
);
2493 clear_bit(BlockedBadBlocks
, &rdev
->flags
);
2494 wake_up(&rdev
->blocked_wait
);
2498 /* words written to sysfs files may, or may not, be \n terminated.
2499 * We want to accept with case. For this we use cmd_match.
2501 static int cmd_match(const char *cmd
, const char *str
)
2503 /* See if cmd, written into a sysfs file, matches
2504 * str. They must either be the same, or cmd can
2505 * have a trailing newline
2507 while (*cmd
&& *str
&& *cmd
== *str
) {
2518 struct rdev_sysfs_entry
{
2519 struct attribute attr
;
2520 ssize_t (*show
)(mdk_rdev_t
*, char *);
2521 ssize_t (*store
)(mdk_rdev_t
*, const char *, size_t);
2525 state_show(mdk_rdev_t
*rdev
, char *page
)
2530 if (test_bit(Faulty
, &rdev
->flags
) ||
2531 rdev
->badblocks
.unacked_exist
) {
2532 len
+= sprintf(page
+len
, "%sfaulty",sep
);
2535 if (test_bit(In_sync
, &rdev
->flags
)) {
2536 len
+= sprintf(page
+len
, "%sin_sync",sep
);
2539 if (test_bit(WriteMostly
, &rdev
->flags
)) {
2540 len
+= sprintf(page
+len
, "%swrite_mostly",sep
);
2543 if (test_bit(Blocked
, &rdev
->flags
) ||
2544 rdev
->badblocks
.unacked_exist
) {
2545 len
+= sprintf(page
+len
, "%sblocked", sep
);
2548 if (!test_bit(Faulty
, &rdev
->flags
) &&
2549 !test_bit(In_sync
, &rdev
->flags
)) {
2550 len
+= sprintf(page
+len
, "%sspare", sep
);
2553 if (test_bit(WriteErrorSeen
, &rdev
->flags
)) {
2554 len
+= sprintf(page
+len
, "%swrite_error", sep
);
2557 return len
+sprintf(page
+len
, "\n");
2561 state_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2564 * faulty - simulates an error
2565 * remove - disconnects the device
2566 * writemostly - sets write_mostly
2567 * -writemostly - clears write_mostly
2568 * blocked - sets the Blocked flags
2569 * -blocked - clears the Blocked and possibly simulates an error
2570 * insync - sets Insync providing device isn't active
2571 * write_error - sets WriteErrorSeen
2572 * -write_error - clears WriteErrorSeen
2575 if (cmd_match(buf
, "faulty") && rdev
->mddev
->pers
) {
2576 md_error(rdev
->mddev
, rdev
);
2577 if (test_bit(Faulty
, &rdev
->flags
))
2581 } else if (cmd_match(buf
, "remove")) {
2582 if (rdev
->raid_disk
>= 0)
2585 mddev_t
*mddev
= rdev
->mddev
;
2586 kick_rdev_from_array(rdev
);
2588 md_update_sb(mddev
, 1);
2589 md_new_event(mddev
);
2592 } else if (cmd_match(buf
, "writemostly")) {
2593 set_bit(WriteMostly
, &rdev
->flags
);
2595 } else if (cmd_match(buf
, "-writemostly")) {
2596 clear_bit(WriteMostly
, &rdev
->flags
);
2598 } else if (cmd_match(buf
, "blocked")) {
2599 set_bit(Blocked
, &rdev
->flags
);
2601 } else if (cmd_match(buf
, "-blocked")) {
2602 if (!test_bit(Faulty
, &rdev
->flags
) &&
2603 rdev
->badblocks
.unacked_exist
) {
2604 /* metadata handler doesn't understand badblocks,
2605 * so we need to fail the device
2607 md_error(rdev
->mddev
, rdev
);
2609 clear_bit(Blocked
, &rdev
->flags
);
2610 clear_bit(BlockedBadBlocks
, &rdev
->flags
);
2611 wake_up(&rdev
->blocked_wait
);
2612 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
2613 md_wakeup_thread(rdev
->mddev
->thread
);
2616 } else if (cmd_match(buf
, "insync") && rdev
->raid_disk
== -1) {
2617 set_bit(In_sync
, &rdev
->flags
);
2619 } else if (cmd_match(buf
, "write_error")) {
2620 set_bit(WriteErrorSeen
, &rdev
->flags
);
2622 } else if (cmd_match(buf
, "-write_error")) {
2623 clear_bit(WriteErrorSeen
, &rdev
->flags
);
2627 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
2628 return err
? err
: len
;
2630 static struct rdev_sysfs_entry rdev_state
=
2631 __ATTR(state
, S_IRUGO
|S_IWUSR
, state_show
, state_store
);
2634 errors_show(mdk_rdev_t
*rdev
, char *page
)
2636 return sprintf(page
, "%d\n", atomic_read(&rdev
->corrected_errors
));
2640 errors_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2643 unsigned long n
= simple_strtoul(buf
, &e
, 10);
2644 if (*buf
&& (*e
== 0 || *e
== '\n')) {
2645 atomic_set(&rdev
->corrected_errors
, n
);
2650 static struct rdev_sysfs_entry rdev_errors
=
2651 __ATTR(errors
, S_IRUGO
|S_IWUSR
, errors_show
, errors_store
);
2654 slot_show(mdk_rdev_t
*rdev
, char *page
)
2656 if (rdev
->raid_disk
< 0)
2657 return sprintf(page
, "none\n");
2659 return sprintf(page
, "%d\n", rdev
->raid_disk
);
2663 slot_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2667 int slot
= simple_strtoul(buf
, &e
, 10);
2668 if (strncmp(buf
, "none", 4)==0)
2670 else if (e
==buf
|| (*e
&& *e
!= '\n'))
2672 if (rdev
->mddev
->pers
&& slot
== -1) {
2673 /* Setting 'slot' on an active array requires also
2674 * updating the 'rd%d' link, and communicating
2675 * with the personality with ->hot_*_disk.
2676 * For now we only support removing
2677 * failed/spare devices. This normally happens automatically,
2678 * but not when the metadata is externally managed.
2680 if (rdev
->raid_disk
== -1)
2682 /* personality does all needed checks */
2683 if (rdev
->mddev
->pers
->hot_remove_disk
== NULL
)
2685 err
= rdev
->mddev
->pers
->
2686 hot_remove_disk(rdev
->mddev
, rdev
->raid_disk
);
2689 sysfs_unlink_rdev(rdev
->mddev
, rdev
);
2690 rdev
->raid_disk
= -1;
2691 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
2692 md_wakeup_thread(rdev
->mddev
->thread
);
2693 } else if (rdev
->mddev
->pers
) {
2695 /* Activating a spare .. or possibly reactivating
2696 * if we ever get bitmaps working here.
2699 if (rdev
->raid_disk
!= -1)
2702 if (test_bit(MD_RECOVERY_RUNNING
, &rdev
->mddev
->recovery
))
2705 if (rdev
->mddev
->pers
->hot_add_disk
== NULL
)
2708 list_for_each_entry(rdev2
, &rdev
->mddev
->disks
, same_set
)
2709 if (rdev2
->raid_disk
== slot
)
2712 if (slot
>= rdev
->mddev
->raid_disks
&&
2713 slot
>= rdev
->mddev
->raid_disks
+ rdev
->mddev
->delta_disks
)
2716 rdev
->raid_disk
= slot
;
2717 if (test_bit(In_sync
, &rdev
->flags
))
2718 rdev
->saved_raid_disk
= slot
;
2720 rdev
->saved_raid_disk
= -1;
2721 err
= rdev
->mddev
->pers
->
2722 hot_add_disk(rdev
->mddev
, rdev
);
2724 rdev
->raid_disk
= -1;
2727 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
2728 if (sysfs_link_rdev(rdev
->mddev
, rdev
))
2729 /* failure here is OK */;
2730 /* don't wakeup anyone, leave that to userspace. */
2732 if (slot
>= rdev
->mddev
->raid_disks
&&
2733 slot
>= rdev
->mddev
->raid_disks
+ rdev
->mddev
->delta_disks
)
2735 rdev
->raid_disk
= slot
;
2736 /* assume it is working */
2737 clear_bit(Faulty
, &rdev
->flags
);
2738 clear_bit(WriteMostly
, &rdev
->flags
);
2739 set_bit(In_sync
, &rdev
->flags
);
2740 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
2746 static struct rdev_sysfs_entry rdev_slot
=
2747 __ATTR(slot
, S_IRUGO
|S_IWUSR
, slot_show
, slot_store
);
2750 offset_show(mdk_rdev_t
*rdev
, char *page
)
2752 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->data_offset
);
2756 offset_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2759 unsigned long long offset
= simple_strtoull(buf
, &e
, 10);
2760 if (e
==buf
|| (*e
&& *e
!= '\n'))
2762 if (rdev
->mddev
->pers
&& rdev
->raid_disk
>= 0)
2764 if (rdev
->sectors
&& rdev
->mddev
->external
)
2765 /* Must set offset before size, so overlap checks
2768 rdev
->data_offset
= offset
;
2772 static struct rdev_sysfs_entry rdev_offset
=
2773 __ATTR(offset
, S_IRUGO
|S_IWUSR
, offset_show
, offset_store
);
2776 rdev_size_show(mdk_rdev_t
*rdev
, char *page
)
2778 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->sectors
/ 2);
2781 static int overlaps(sector_t s1
, sector_t l1
, sector_t s2
, sector_t l2
)
2783 /* check if two start/length pairs overlap */
2791 static int strict_blocks_to_sectors(const char *buf
, sector_t
*sectors
)
2793 unsigned long long blocks
;
2796 if (strict_strtoull(buf
, 10, &blocks
) < 0)
2799 if (blocks
& 1ULL << (8 * sizeof(blocks
) - 1))
2800 return -EINVAL
; /* sector conversion overflow */
2803 if (new != blocks
* 2)
2804 return -EINVAL
; /* unsigned long long to sector_t overflow */
2811 rdev_size_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2813 mddev_t
*my_mddev
= rdev
->mddev
;
2814 sector_t oldsectors
= rdev
->sectors
;
2817 if (strict_blocks_to_sectors(buf
, §ors
) < 0)
2819 if (my_mddev
->pers
&& rdev
->raid_disk
>= 0) {
2820 if (my_mddev
->persistent
) {
2821 sectors
= super_types
[my_mddev
->major_version
].
2822 rdev_size_change(rdev
, sectors
);
2825 } else if (!sectors
)
2826 sectors
= (i_size_read(rdev
->bdev
->bd_inode
) >> 9) -
2829 if (sectors
< my_mddev
->dev_sectors
)
2830 return -EINVAL
; /* component must fit device */
2832 rdev
->sectors
= sectors
;
2833 if (sectors
> oldsectors
&& my_mddev
->external
) {
2834 /* need to check that all other rdevs with the same ->bdev
2835 * do not overlap. We need to unlock the mddev to avoid
2836 * a deadlock. We have already changed rdev->sectors, and if
2837 * we have to change it back, we will have the lock again.
2841 struct list_head
*tmp
;
2843 mddev_unlock(my_mddev
);
2844 for_each_mddev(mddev
, tmp
) {
2848 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
)
2849 if (rdev
->bdev
== rdev2
->bdev
&&
2851 overlaps(rdev
->data_offset
, rdev
->sectors
,
2857 mddev_unlock(mddev
);
2863 mddev_lock(my_mddev
);
2865 /* Someone else could have slipped in a size
2866 * change here, but doing so is just silly.
2867 * We put oldsectors back because we *know* it is
2868 * safe, and trust userspace not to race with
2871 rdev
->sectors
= oldsectors
;
2878 static struct rdev_sysfs_entry rdev_size
=
2879 __ATTR(size
, S_IRUGO
|S_IWUSR
, rdev_size_show
, rdev_size_store
);
2882 static ssize_t
recovery_start_show(mdk_rdev_t
*rdev
, char *page
)
2884 unsigned long long recovery_start
= rdev
->recovery_offset
;
2886 if (test_bit(In_sync
, &rdev
->flags
) ||
2887 recovery_start
== MaxSector
)
2888 return sprintf(page
, "none\n");
2890 return sprintf(page
, "%llu\n", recovery_start
);
2893 static ssize_t
recovery_start_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2895 unsigned long long recovery_start
;
2897 if (cmd_match(buf
, "none"))
2898 recovery_start
= MaxSector
;
2899 else if (strict_strtoull(buf
, 10, &recovery_start
))
2902 if (rdev
->mddev
->pers
&&
2903 rdev
->raid_disk
>= 0)
2906 rdev
->recovery_offset
= recovery_start
;
2907 if (recovery_start
== MaxSector
)
2908 set_bit(In_sync
, &rdev
->flags
);
2910 clear_bit(In_sync
, &rdev
->flags
);
2914 static struct rdev_sysfs_entry rdev_recovery_start
=
2915 __ATTR(recovery_start
, S_IRUGO
|S_IWUSR
, recovery_start_show
, recovery_start_store
);
2919 badblocks_show(struct badblocks
*bb
, char *page
, int unack
);
2921 badblocks_store(struct badblocks
*bb
, const char *page
, size_t len
, int unack
);
2923 static ssize_t
bb_show(mdk_rdev_t
*rdev
, char *page
)
2925 return badblocks_show(&rdev
->badblocks
, page
, 0);
2927 static ssize_t
bb_store(mdk_rdev_t
*rdev
, const char *page
, size_t len
)
2929 int rv
= badblocks_store(&rdev
->badblocks
, page
, len
, 0);
2930 /* Maybe that ack was all we needed */
2931 if (test_and_clear_bit(BlockedBadBlocks
, &rdev
->flags
))
2932 wake_up(&rdev
->blocked_wait
);
2935 static struct rdev_sysfs_entry rdev_bad_blocks
=
2936 __ATTR(bad_blocks
, S_IRUGO
|S_IWUSR
, bb_show
, bb_store
);
2939 static ssize_t
ubb_show(mdk_rdev_t
*rdev
, char *page
)
2941 return badblocks_show(&rdev
->badblocks
, page
, 1);
2943 static ssize_t
ubb_store(mdk_rdev_t
*rdev
, const char *page
, size_t len
)
2945 return badblocks_store(&rdev
->badblocks
, page
, len
, 1);
2947 static struct rdev_sysfs_entry rdev_unack_bad_blocks
=
2948 __ATTR(unacknowledged_bad_blocks
, S_IRUGO
|S_IWUSR
, ubb_show
, ubb_store
);
2950 static struct attribute
*rdev_default_attrs
[] = {
2956 &rdev_recovery_start
.attr
,
2957 &rdev_bad_blocks
.attr
,
2958 &rdev_unack_bad_blocks
.attr
,
2962 rdev_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
2964 struct rdev_sysfs_entry
*entry
= container_of(attr
, struct rdev_sysfs_entry
, attr
);
2965 mdk_rdev_t
*rdev
= container_of(kobj
, mdk_rdev_t
, kobj
);
2966 mddev_t
*mddev
= rdev
->mddev
;
2972 rv
= mddev
? mddev_lock(mddev
) : -EBUSY
;
2974 if (rdev
->mddev
== NULL
)
2977 rv
= entry
->show(rdev
, page
);
2978 mddev_unlock(mddev
);
2984 rdev_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
2985 const char *page
, size_t length
)
2987 struct rdev_sysfs_entry
*entry
= container_of(attr
, struct rdev_sysfs_entry
, attr
);
2988 mdk_rdev_t
*rdev
= container_of(kobj
, mdk_rdev_t
, kobj
);
2990 mddev_t
*mddev
= rdev
->mddev
;
2994 if (!capable(CAP_SYS_ADMIN
))
2996 rv
= mddev
? mddev_lock(mddev
): -EBUSY
;
2998 if (rdev
->mddev
== NULL
)
3001 rv
= entry
->store(rdev
, page
, length
);
3002 mddev_unlock(mddev
);
3007 static void rdev_free(struct kobject
*ko
)
3009 mdk_rdev_t
*rdev
= container_of(ko
, mdk_rdev_t
, kobj
);
3012 static const struct sysfs_ops rdev_sysfs_ops
= {
3013 .show
= rdev_attr_show
,
3014 .store
= rdev_attr_store
,
3016 static struct kobj_type rdev_ktype
= {
3017 .release
= rdev_free
,
3018 .sysfs_ops
= &rdev_sysfs_ops
,
3019 .default_attrs
= rdev_default_attrs
,
3022 int md_rdev_init(mdk_rdev_t
*rdev
)
3025 rdev
->saved_raid_disk
= -1;
3026 rdev
->raid_disk
= -1;
3028 rdev
->data_offset
= 0;
3029 rdev
->sb_events
= 0;
3030 rdev
->last_read_error
.tv_sec
= 0;
3031 rdev
->last_read_error
.tv_nsec
= 0;
3032 rdev
->sb_loaded
= 0;
3033 rdev
->bb_page
= NULL
;
3034 atomic_set(&rdev
->nr_pending
, 0);
3035 atomic_set(&rdev
->read_errors
, 0);
3036 atomic_set(&rdev
->corrected_errors
, 0);
3038 INIT_LIST_HEAD(&rdev
->same_set
);
3039 init_waitqueue_head(&rdev
->blocked_wait
);
3041 /* Add space to store bad block list.
3042 * This reserves the space even on arrays where it cannot
3043 * be used - I wonder if that matters
3045 rdev
->badblocks
.count
= 0;
3046 rdev
->badblocks
.shift
= 0;
3047 rdev
->badblocks
.page
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
3048 seqlock_init(&rdev
->badblocks
.lock
);
3049 if (rdev
->badblocks
.page
== NULL
)
3054 EXPORT_SYMBOL_GPL(md_rdev_init
);
3056 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3058 * mark the device faulty if:
3060 * - the device is nonexistent (zero size)
3061 * - the device has no valid superblock
3063 * a faulty rdev _never_ has rdev->sb set.
3065 static mdk_rdev_t
*md_import_device(dev_t newdev
, int super_format
, int super_minor
)
3067 char b
[BDEVNAME_SIZE
];
3072 rdev
= kzalloc(sizeof(*rdev
), GFP_KERNEL
);
3074 printk(KERN_ERR
"md: could not alloc mem for new device!\n");
3075 return ERR_PTR(-ENOMEM
);
3078 err
= md_rdev_init(rdev
);
3081 err
= alloc_disk_sb(rdev
);
3085 err
= lock_rdev(rdev
, newdev
, super_format
== -2);
3089 kobject_init(&rdev
->kobj
, &rdev_ktype
);
3091 size
= i_size_read(rdev
->bdev
->bd_inode
) >> BLOCK_SIZE_BITS
;
3094 "md: %s has zero or unknown size, marking faulty!\n",
3095 bdevname(rdev
->bdev
,b
));
3100 if (super_format
>= 0) {
3101 err
= super_types
[super_format
].
3102 load_super(rdev
, NULL
, super_minor
);
3103 if (err
== -EINVAL
) {
3105 "md: %s does not have a valid v%d.%d "
3106 "superblock, not importing!\n",
3107 bdevname(rdev
->bdev
,b
),
3108 super_format
, super_minor
);
3113 "md: could not read %s's sb, not importing!\n",
3114 bdevname(rdev
->bdev
,b
));
3118 if (super_format
== -1)
3119 /* hot-add for 0.90, or non-persistent: so no badblocks */
3120 rdev
->badblocks
.shift
= -1;
3128 kfree(rdev
->badblocks
.page
);
3130 return ERR_PTR(err
);
3134 * Check a full RAID array for plausibility
3138 static void analyze_sbs(mddev_t
* mddev
)
3141 mdk_rdev_t
*rdev
, *freshest
, *tmp
;
3142 char b
[BDEVNAME_SIZE
];
3145 rdev_for_each(rdev
, tmp
, mddev
)
3146 switch (super_types
[mddev
->major_version
].
3147 load_super(rdev
, freshest
, mddev
->minor_version
)) {
3155 "md: fatal superblock inconsistency in %s"
3156 " -- removing from array\n",
3157 bdevname(rdev
->bdev
,b
));
3158 kick_rdev_from_array(rdev
);
3162 super_types
[mddev
->major_version
].
3163 validate_super(mddev
, freshest
);
3166 rdev_for_each(rdev
, tmp
, mddev
) {
3167 if (mddev
->max_disks
&&
3168 (rdev
->desc_nr
>= mddev
->max_disks
||
3169 i
> mddev
->max_disks
)) {
3171 "md: %s: %s: only %d devices permitted\n",
3172 mdname(mddev
), bdevname(rdev
->bdev
, b
),
3174 kick_rdev_from_array(rdev
);
3177 if (rdev
!= freshest
)
3178 if (super_types
[mddev
->major_version
].
3179 validate_super(mddev
, rdev
)) {
3180 printk(KERN_WARNING
"md: kicking non-fresh %s"
3182 bdevname(rdev
->bdev
,b
));
3183 kick_rdev_from_array(rdev
);
3186 if (mddev
->level
== LEVEL_MULTIPATH
) {
3187 rdev
->desc_nr
= i
++;
3188 rdev
->raid_disk
= rdev
->desc_nr
;
3189 set_bit(In_sync
, &rdev
->flags
);
3190 } else if (rdev
->raid_disk
>= (mddev
->raid_disks
- min(0, mddev
->delta_disks
))) {
3191 rdev
->raid_disk
= -1;
3192 clear_bit(In_sync
, &rdev
->flags
);
3197 /* Read a fixed-point number.
3198 * Numbers in sysfs attributes should be in "standard" units where
3199 * possible, so time should be in seconds.
3200 * However we internally use a a much smaller unit such as
3201 * milliseconds or jiffies.
3202 * This function takes a decimal number with a possible fractional
3203 * component, and produces an integer which is the result of
3204 * multiplying that number by 10^'scale'.
3205 * all without any floating-point arithmetic.
3207 int strict_strtoul_scaled(const char *cp
, unsigned long *res
, int scale
)
3209 unsigned long result
= 0;
3211 while (isdigit(*cp
) || (*cp
== '.' && decimals
< 0)) {
3214 else if (decimals
< scale
) {
3217 result
= result
* 10 + value
;
3229 while (decimals
< scale
) {
3238 static void md_safemode_timeout(unsigned long data
);
3241 safe_delay_show(mddev_t
*mddev
, char *page
)
3243 int msec
= (mddev
->safemode_delay
*1000)/HZ
;
3244 return sprintf(page
, "%d.%03d\n", msec
/1000, msec
%1000);
3247 safe_delay_store(mddev_t
*mddev
, const char *cbuf
, size_t len
)
3251 if (strict_strtoul_scaled(cbuf
, &msec
, 3) < 0)
3254 mddev
->safemode_delay
= 0;
3256 unsigned long old_delay
= mddev
->safemode_delay
;
3257 mddev
->safemode_delay
= (msec
*HZ
)/1000;
3258 if (mddev
->safemode_delay
== 0)
3259 mddev
->safemode_delay
= 1;
3260 if (mddev
->safemode_delay
< old_delay
)
3261 md_safemode_timeout((unsigned long)mddev
);
3265 static struct md_sysfs_entry md_safe_delay
=
3266 __ATTR(safe_mode_delay
, S_IRUGO
|S_IWUSR
,safe_delay_show
, safe_delay_store
);
3269 level_show(mddev_t
*mddev
, char *page
)
3271 struct mdk_personality
*p
= mddev
->pers
;
3273 return sprintf(page
, "%s\n", p
->name
);
3274 else if (mddev
->clevel
[0])
3275 return sprintf(page
, "%s\n", mddev
->clevel
);
3276 else if (mddev
->level
!= LEVEL_NONE
)
3277 return sprintf(page
, "%d\n", mddev
->level
);
3283 level_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3287 struct mdk_personality
*pers
;
3292 if (mddev
->pers
== NULL
) {
3295 if (len
>= sizeof(mddev
->clevel
))
3297 strncpy(mddev
->clevel
, buf
, len
);
3298 if (mddev
->clevel
[len
-1] == '\n')
3300 mddev
->clevel
[len
] = 0;
3301 mddev
->level
= LEVEL_NONE
;
3305 /* request to change the personality. Need to ensure:
3306 * - array is not engaged in resync/recovery/reshape
3307 * - old personality can be suspended
3308 * - new personality will access other array.
3311 if (mddev
->sync_thread
||
3312 mddev
->reshape_position
!= MaxSector
||
3313 mddev
->sysfs_active
)
3316 if (!mddev
->pers
->quiesce
) {
3317 printk(KERN_WARNING
"md: %s: %s does not support online personality change\n",
3318 mdname(mddev
), mddev
->pers
->name
);
3322 /* Now find the new personality */
3323 if (len
== 0 || len
>= sizeof(clevel
))
3325 strncpy(clevel
, buf
, len
);
3326 if (clevel
[len
-1] == '\n')
3329 if (strict_strtol(clevel
, 10, &level
))
3332 if (request_module("md-%s", clevel
) != 0)
3333 request_module("md-level-%s", clevel
);
3334 spin_lock(&pers_lock
);
3335 pers
= find_pers(level
, clevel
);
3336 if (!pers
|| !try_module_get(pers
->owner
)) {
3337 spin_unlock(&pers_lock
);
3338 printk(KERN_WARNING
"md: personality %s not loaded\n", clevel
);
3341 spin_unlock(&pers_lock
);
3343 if (pers
== mddev
->pers
) {
3344 /* Nothing to do! */
3345 module_put(pers
->owner
);
3348 if (!pers
->takeover
) {
3349 module_put(pers
->owner
);
3350 printk(KERN_WARNING
"md: %s: %s does not support personality takeover\n",
3351 mdname(mddev
), clevel
);
3355 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
3356 rdev
->new_raid_disk
= rdev
->raid_disk
;
3358 /* ->takeover must set new_* and/or delta_disks
3359 * if it succeeds, and may set them when it fails.
3361 priv
= pers
->takeover(mddev
);
3363 mddev
->new_level
= mddev
->level
;
3364 mddev
->new_layout
= mddev
->layout
;
3365 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
3366 mddev
->raid_disks
-= mddev
->delta_disks
;
3367 mddev
->delta_disks
= 0;
3368 module_put(pers
->owner
);
3369 printk(KERN_WARNING
"md: %s: %s would not accept array\n",
3370 mdname(mddev
), clevel
);
3371 return PTR_ERR(priv
);
3374 /* Looks like we have a winner */
3375 mddev_suspend(mddev
);
3376 mddev
->pers
->stop(mddev
);
3378 if (mddev
->pers
->sync_request
== NULL
&&
3379 pers
->sync_request
!= NULL
) {
3380 /* need to add the md_redundancy_group */
3381 if (sysfs_create_group(&mddev
->kobj
, &md_redundancy_group
))
3383 "md: cannot register extra attributes for %s\n",
3385 mddev
->sysfs_action
= sysfs_get_dirent(mddev
->kobj
.sd
, NULL
, "sync_action");
3387 if (mddev
->pers
->sync_request
!= NULL
&&
3388 pers
->sync_request
== NULL
) {
3389 /* need to remove the md_redundancy_group */
3390 if (mddev
->to_remove
== NULL
)
3391 mddev
->to_remove
= &md_redundancy_group
;
3394 if (mddev
->pers
->sync_request
== NULL
&&
3396 /* We are converting from a no-redundancy array
3397 * to a redundancy array and metadata is managed
3398 * externally so we need to be sure that writes
3399 * won't block due to a need to transition
3401 * until external management is started.
3404 mddev
->safemode_delay
= 0;
3405 mddev
->safemode
= 0;
3408 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
3409 if (rdev
->raid_disk
< 0)
3411 if (rdev
->new_raid_disk
>= mddev
->raid_disks
)
3412 rdev
->new_raid_disk
= -1;
3413 if (rdev
->new_raid_disk
== rdev
->raid_disk
)
3415 sysfs_unlink_rdev(mddev
, rdev
);
3417 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
3418 if (rdev
->raid_disk
< 0)
3420 if (rdev
->new_raid_disk
== rdev
->raid_disk
)
3422 rdev
->raid_disk
= rdev
->new_raid_disk
;
3423 if (rdev
->raid_disk
< 0)
3424 clear_bit(In_sync
, &rdev
->flags
);
3426 if (sysfs_link_rdev(mddev
, rdev
))
3427 printk(KERN_WARNING
"md: cannot register rd%d"
3428 " for %s after level change\n",
3429 rdev
->raid_disk
, mdname(mddev
));
3433 module_put(mddev
->pers
->owner
);
3435 mddev
->private = priv
;
3436 strlcpy(mddev
->clevel
, pers
->name
, sizeof(mddev
->clevel
));
3437 mddev
->level
= mddev
->new_level
;
3438 mddev
->layout
= mddev
->new_layout
;
3439 mddev
->chunk_sectors
= mddev
->new_chunk_sectors
;
3440 mddev
->delta_disks
= 0;
3441 mddev
->degraded
= 0;
3442 if (mddev
->pers
->sync_request
== NULL
) {
3443 /* this is now an array without redundancy, so
3444 * it must always be in_sync
3447 del_timer_sync(&mddev
->safemode_timer
);
3450 mddev_resume(mddev
);
3451 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
3452 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3453 md_wakeup_thread(mddev
->thread
);
3454 sysfs_notify(&mddev
->kobj
, NULL
, "level");
3455 md_new_event(mddev
);
3459 static struct md_sysfs_entry md_level
=
3460 __ATTR(level
, S_IRUGO
|S_IWUSR
, level_show
, level_store
);
3464 layout_show(mddev_t
*mddev
, char *page
)
3466 /* just a number, not meaningful for all levels */
3467 if (mddev
->reshape_position
!= MaxSector
&&
3468 mddev
->layout
!= mddev
->new_layout
)
3469 return sprintf(page
, "%d (%d)\n",
3470 mddev
->new_layout
, mddev
->layout
);
3471 return sprintf(page
, "%d\n", mddev
->layout
);
3475 layout_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3478 unsigned long n
= simple_strtoul(buf
, &e
, 10);
3480 if (!*buf
|| (*e
&& *e
!= '\n'))
3485 if (mddev
->pers
->check_reshape
== NULL
)
3487 mddev
->new_layout
= n
;
3488 err
= mddev
->pers
->check_reshape(mddev
);
3490 mddev
->new_layout
= mddev
->layout
;
3494 mddev
->new_layout
= n
;
3495 if (mddev
->reshape_position
== MaxSector
)
3500 static struct md_sysfs_entry md_layout
=
3501 __ATTR(layout
, S_IRUGO
|S_IWUSR
, layout_show
, layout_store
);
3505 raid_disks_show(mddev_t
*mddev
, char *page
)
3507 if (mddev
->raid_disks
== 0)
3509 if (mddev
->reshape_position
!= MaxSector
&&
3510 mddev
->delta_disks
!= 0)
3511 return sprintf(page
, "%d (%d)\n", mddev
->raid_disks
,
3512 mddev
->raid_disks
- mddev
->delta_disks
);
3513 return sprintf(page
, "%d\n", mddev
->raid_disks
);
3516 static int update_raid_disks(mddev_t
*mddev
, int raid_disks
);
3519 raid_disks_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3523 unsigned long n
= simple_strtoul(buf
, &e
, 10);
3525 if (!*buf
|| (*e
&& *e
!= '\n'))
3529 rv
= update_raid_disks(mddev
, n
);
3530 else if (mddev
->reshape_position
!= MaxSector
) {
3531 int olddisks
= mddev
->raid_disks
- mddev
->delta_disks
;
3532 mddev
->delta_disks
= n
- olddisks
;
3533 mddev
->raid_disks
= n
;
3535 mddev
->raid_disks
= n
;
3536 return rv
? rv
: len
;
3538 static struct md_sysfs_entry md_raid_disks
=
3539 __ATTR(raid_disks
, S_IRUGO
|S_IWUSR
, raid_disks_show
, raid_disks_store
);
3542 chunk_size_show(mddev_t
*mddev
, char *page
)
3544 if (mddev
->reshape_position
!= MaxSector
&&
3545 mddev
->chunk_sectors
!= mddev
->new_chunk_sectors
)
3546 return sprintf(page
, "%d (%d)\n",
3547 mddev
->new_chunk_sectors
<< 9,
3548 mddev
->chunk_sectors
<< 9);
3549 return sprintf(page
, "%d\n", mddev
->chunk_sectors
<< 9);
3553 chunk_size_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3556 unsigned long n
= simple_strtoul(buf
, &e
, 10);
3558 if (!*buf
|| (*e
&& *e
!= '\n'))
3563 if (mddev
->pers
->check_reshape
== NULL
)
3565 mddev
->new_chunk_sectors
= n
>> 9;
3566 err
= mddev
->pers
->check_reshape(mddev
);
3568 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
3572 mddev
->new_chunk_sectors
= n
>> 9;
3573 if (mddev
->reshape_position
== MaxSector
)
3574 mddev
->chunk_sectors
= n
>> 9;
3578 static struct md_sysfs_entry md_chunk_size
=
3579 __ATTR(chunk_size
, S_IRUGO
|S_IWUSR
, chunk_size_show
, chunk_size_store
);
3582 resync_start_show(mddev_t
*mddev
, char *page
)
3584 if (mddev
->recovery_cp
== MaxSector
)
3585 return sprintf(page
, "none\n");
3586 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->recovery_cp
);
3590 resync_start_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3593 unsigned long long n
= simple_strtoull(buf
, &e
, 10);
3595 if (mddev
->pers
&& !test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
))
3597 if (cmd_match(buf
, "none"))
3599 else if (!*buf
|| (*e
&& *e
!= '\n'))
3602 mddev
->recovery_cp
= n
;
3605 static struct md_sysfs_entry md_resync_start
=
3606 __ATTR(resync_start
, S_IRUGO
|S_IWUSR
, resync_start_show
, resync_start_store
);
3609 * The array state can be:
3612 * No devices, no size, no level
3613 * Equivalent to STOP_ARRAY ioctl
3615 * May have some settings, but array is not active
3616 * all IO results in error
3617 * When written, doesn't tear down array, but just stops it
3618 * suspended (not supported yet)
3619 * All IO requests will block. The array can be reconfigured.
3620 * Writing this, if accepted, will block until array is quiescent
3622 * no resync can happen. no superblocks get written.
3623 * write requests fail
3625 * like readonly, but behaves like 'clean' on a write request.
3627 * clean - no pending writes, but otherwise active.
3628 * When written to inactive array, starts without resync
3629 * If a write request arrives then
3630 * if metadata is known, mark 'dirty' and switch to 'active'.
3631 * if not known, block and switch to write-pending
3632 * If written to an active array that has pending writes, then fails.
3634 * fully active: IO and resync can be happening.
3635 * When written to inactive array, starts with resync
3638 * clean, but writes are blocked waiting for 'active' to be written.
3641 * like active, but no writes have been seen for a while (100msec).
3644 enum array_state
{ clear
, inactive
, suspended
, readonly
, read_auto
, clean
, active
,
3645 write_pending
, active_idle
, bad_word
};
3646 static char *array_states
[] = {
3647 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3648 "write-pending", "active-idle", NULL
};
3650 static int match_word(const char *word
, char **list
)
3653 for (n
=0; list
[n
]; n
++)
3654 if (cmd_match(word
, list
[n
]))
3660 array_state_show(mddev_t
*mddev
, char *page
)
3662 enum array_state st
= inactive
;
3675 else if (test_bit(MD_CHANGE_PENDING
, &mddev
->flags
))
3677 else if (mddev
->safemode
)
3683 if (list_empty(&mddev
->disks
) &&
3684 mddev
->raid_disks
== 0 &&
3685 mddev
->dev_sectors
== 0)
3690 return sprintf(page
, "%s\n", array_states
[st
]);
3693 static int do_md_stop(mddev_t
* mddev
, int ro
, int is_open
);
3694 static int md_set_readonly(mddev_t
* mddev
, int is_open
);
3695 static int do_md_run(mddev_t
* mddev
);
3696 static int restart_array(mddev_t
*mddev
);
3699 array_state_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3702 enum array_state st
= match_word(buf
, array_states
);
3707 /* stopping an active array */
3708 if (atomic_read(&mddev
->openers
) > 0)
3710 err
= do_md_stop(mddev
, 0, 0);
3713 /* stopping an active array */
3715 if (atomic_read(&mddev
->openers
) > 0)
3717 err
= do_md_stop(mddev
, 2, 0);
3719 err
= 0; /* already inactive */
3722 break; /* not supported yet */
3725 err
= md_set_readonly(mddev
, 0);
3728 set_disk_ro(mddev
->gendisk
, 1);
3729 err
= do_md_run(mddev
);
3735 err
= md_set_readonly(mddev
, 0);
3736 else if (mddev
->ro
== 1)
3737 err
= restart_array(mddev
);
3740 set_disk_ro(mddev
->gendisk
, 0);
3744 err
= do_md_run(mddev
);
3749 restart_array(mddev
);
3750 spin_lock_irq(&mddev
->write_lock
);
3751 if (atomic_read(&mddev
->writes_pending
) == 0) {
3752 if (mddev
->in_sync
== 0) {
3754 if (mddev
->safemode
== 1)
3755 mddev
->safemode
= 0;
3756 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
3761 spin_unlock_irq(&mddev
->write_lock
);
3767 restart_array(mddev
);
3768 clear_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
3769 wake_up(&mddev
->sb_wait
);
3773 set_disk_ro(mddev
->gendisk
, 0);
3774 err
= do_md_run(mddev
);
3779 /* these cannot be set */
3785 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
3789 static struct md_sysfs_entry md_array_state
=
3790 __ATTR(array_state
, S_IRUGO
|S_IWUSR
, array_state_show
, array_state_store
);
3793 max_corrected_read_errors_show(mddev_t
*mddev
, char *page
) {
3794 return sprintf(page
, "%d\n",
3795 atomic_read(&mddev
->max_corr_read_errors
));
3799 max_corrected_read_errors_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3802 unsigned long n
= simple_strtoul(buf
, &e
, 10);
3804 if (*buf
&& (*e
== 0 || *e
== '\n')) {
3805 atomic_set(&mddev
->max_corr_read_errors
, n
);
3811 static struct md_sysfs_entry max_corr_read_errors
=
3812 __ATTR(max_read_errors
, S_IRUGO
|S_IWUSR
, max_corrected_read_errors_show
,
3813 max_corrected_read_errors_store
);
3816 null_show(mddev_t
*mddev
, char *page
)
3822 new_dev_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3824 /* buf must be %d:%d\n? giving major and minor numbers */
3825 /* The new device is added to the array.
3826 * If the array has a persistent superblock, we read the
3827 * superblock to initialise info and check validity.
3828 * Otherwise, only checking done is that in bind_rdev_to_array,
3829 * which mainly checks size.
3832 int major
= simple_strtoul(buf
, &e
, 10);
3838 if (!*buf
|| *e
!= ':' || !e
[1] || e
[1] == '\n')
3840 minor
= simple_strtoul(e
+1, &e
, 10);
3841 if (*e
&& *e
!= '\n')
3843 dev
= MKDEV(major
, minor
);
3844 if (major
!= MAJOR(dev
) ||
3845 minor
!= MINOR(dev
))
3849 if (mddev
->persistent
) {
3850 rdev
= md_import_device(dev
, mddev
->major_version
,
3851 mddev
->minor_version
);
3852 if (!IS_ERR(rdev
) && !list_empty(&mddev
->disks
)) {
3853 mdk_rdev_t
*rdev0
= list_entry(mddev
->disks
.next
,
3854 mdk_rdev_t
, same_set
);
3855 err
= super_types
[mddev
->major_version
]
3856 .load_super(rdev
, rdev0
, mddev
->minor_version
);
3860 } else if (mddev
->external
)
3861 rdev
= md_import_device(dev
, -2, -1);
3863 rdev
= md_import_device(dev
, -1, -1);
3866 return PTR_ERR(rdev
);
3867 err
= bind_rdev_to_array(rdev
, mddev
);
3871 return err
? err
: len
;
3874 static struct md_sysfs_entry md_new_device
=
3875 __ATTR(new_dev
, S_IWUSR
, null_show
, new_dev_store
);
3878 bitmap_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3881 unsigned long chunk
, end_chunk
;
3885 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3887 chunk
= end_chunk
= simple_strtoul(buf
, &end
, 0);
3888 if (buf
== end
) break;
3889 if (*end
== '-') { /* range */
3891 end_chunk
= simple_strtoul(buf
, &end
, 0);
3892 if (buf
== end
) break;
3894 if (*end
&& !isspace(*end
)) break;
3895 bitmap_dirty_bits(mddev
->bitmap
, chunk
, end_chunk
);
3896 buf
= skip_spaces(end
);
3898 bitmap_unplug(mddev
->bitmap
); /* flush the bits to disk */
3903 static struct md_sysfs_entry md_bitmap
=
3904 __ATTR(bitmap_set_bits
, S_IWUSR
, null_show
, bitmap_store
);
3907 size_show(mddev_t
*mddev
, char *page
)
3909 return sprintf(page
, "%llu\n",
3910 (unsigned long long)mddev
->dev_sectors
/ 2);
3913 static int update_size(mddev_t
*mddev
, sector_t num_sectors
);
3916 size_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3918 /* If array is inactive, we can reduce the component size, but
3919 * not increase it (except from 0).
3920 * If array is active, we can try an on-line resize
3923 int err
= strict_blocks_to_sectors(buf
, §ors
);
3928 err
= update_size(mddev
, sectors
);
3929 md_update_sb(mddev
, 1);
3931 if (mddev
->dev_sectors
== 0 ||
3932 mddev
->dev_sectors
> sectors
)
3933 mddev
->dev_sectors
= sectors
;
3937 return err
? err
: len
;
3940 static struct md_sysfs_entry md_size
=
3941 __ATTR(component_size
, S_IRUGO
|S_IWUSR
, size_show
, size_store
);
3946 * 'none' for arrays with no metadata (good luck...)
3947 * 'external' for arrays with externally managed metadata,
3948 * or N.M for internally known formats
3951 metadata_show(mddev_t
*mddev
, char *page
)
3953 if (mddev
->persistent
)
3954 return sprintf(page
, "%d.%d\n",
3955 mddev
->major_version
, mddev
->minor_version
);
3956 else if (mddev
->external
)
3957 return sprintf(page
, "external:%s\n", mddev
->metadata_type
);
3959 return sprintf(page
, "none\n");
3963 metadata_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3967 /* Changing the details of 'external' metadata is
3968 * always permitted. Otherwise there must be
3969 * no devices attached to the array.
3971 if (mddev
->external
&& strncmp(buf
, "external:", 9) == 0)
3973 else if (!list_empty(&mddev
->disks
))
3976 if (cmd_match(buf
, "none")) {
3977 mddev
->persistent
= 0;
3978 mddev
->external
= 0;
3979 mddev
->major_version
= 0;
3980 mddev
->minor_version
= 90;
3983 if (strncmp(buf
, "external:", 9) == 0) {
3984 size_t namelen
= len
-9;
3985 if (namelen
>= sizeof(mddev
->metadata_type
))
3986 namelen
= sizeof(mddev
->metadata_type
)-1;
3987 strncpy(mddev
->metadata_type
, buf
+9, namelen
);
3988 mddev
->metadata_type
[namelen
] = 0;
3989 if (namelen
&& mddev
->metadata_type
[namelen
-1] == '\n')
3990 mddev
->metadata_type
[--namelen
] = 0;
3991 mddev
->persistent
= 0;
3992 mddev
->external
= 1;
3993 mddev
->major_version
= 0;
3994 mddev
->minor_version
= 90;
3997 major
= simple_strtoul(buf
, &e
, 10);
3998 if (e
==buf
|| *e
!= '.')
4001 minor
= simple_strtoul(buf
, &e
, 10);
4002 if (e
==buf
|| (*e
&& *e
!= '\n') )
4004 if (major
>= ARRAY_SIZE(super_types
) || super_types
[major
].name
== NULL
)
4006 mddev
->major_version
= major
;
4007 mddev
->minor_version
= minor
;
4008 mddev
->persistent
= 1;
4009 mddev
->external
= 0;
4013 static struct md_sysfs_entry md_metadata
=
4014 __ATTR(metadata_version
, S_IRUGO
|S_IWUSR
, metadata_show
, metadata_store
);
4017 action_show(mddev_t
*mddev
, char *page
)
4019 char *type
= "idle";
4020 if (test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
))
4022 else if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
4023 (!mddev
->ro
&& test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))) {
4024 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
4026 else if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
4027 if (!test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
4029 else if (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
))
4033 } else if (test_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
))
4036 return sprintf(page
, "%s\n", type
);
4039 static void reap_sync_thread(mddev_t
*mddev
);
4042 action_store(mddev_t
*mddev
, const char *page
, size_t len
)
4044 if (!mddev
->pers
|| !mddev
->pers
->sync_request
)
4047 if (cmd_match(page
, "frozen"))
4048 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4050 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4052 if (cmd_match(page
, "idle") || cmd_match(page
, "frozen")) {
4053 if (mddev
->sync_thread
) {
4054 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
4055 reap_sync_thread(mddev
);
4057 } else if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
4058 test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))
4060 else if (cmd_match(page
, "resync"))
4061 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4062 else if (cmd_match(page
, "recover")) {
4063 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
4064 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4065 } else if (cmd_match(page
, "reshape")) {
4067 if (mddev
->pers
->start_reshape
== NULL
)
4069 err
= mddev
->pers
->start_reshape(mddev
);
4072 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
4074 if (cmd_match(page
, "check"))
4075 set_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
4076 else if (!cmd_match(page
, "repair"))
4078 set_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
4079 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
4081 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4082 md_wakeup_thread(mddev
->thread
);
4083 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
4088 mismatch_cnt_show(mddev_t
*mddev
, char *page
)
4090 return sprintf(page
, "%llu\n",
4091 (unsigned long long) mddev
->resync_mismatches
);
4094 static struct md_sysfs_entry md_scan_mode
=
4095 __ATTR(sync_action
, S_IRUGO
|S_IWUSR
, action_show
, action_store
);
4098 static struct md_sysfs_entry md_mismatches
= __ATTR_RO(mismatch_cnt
);
4101 sync_min_show(mddev_t
*mddev
, char *page
)
4103 return sprintf(page
, "%d (%s)\n", speed_min(mddev
),
4104 mddev
->sync_speed_min
? "local": "system");
4108 sync_min_store(mddev_t
*mddev
, const char *buf
, size_t len
)
4112 if (strncmp(buf
, "system", 6)==0) {
4113 mddev
->sync_speed_min
= 0;
4116 min
= simple_strtoul(buf
, &e
, 10);
4117 if (buf
== e
|| (*e
&& *e
!= '\n') || min
<= 0)
4119 mddev
->sync_speed_min
= min
;
4123 static struct md_sysfs_entry md_sync_min
=
4124 __ATTR(sync_speed_min
, S_IRUGO
|S_IWUSR
, sync_min_show
, sync_min_store
);
4127 sync_max_show(mddev_t
*mddev
, char *page
)
4129 return sprintf(page
, "%d (%s)\n", speed_max(mddev
),
4130 mddev
->sync_speed_max
? "local": "system");
4134 sync_max_store(mddev_t
*mddev
, const char *buf
, size_t len
)
4138 if (strncmp(buf
, "system", 6)==0) {
4139 mddev
->sync_speed_max
= 0;
4142 max
= simple_strtoul(buf
, &e
, 10);
4143 if (buf
== e
|| (*e
&& *e
!= '\n') || max
<= 0)
4145 mddev
->sync_speed_max
= max
;
4149 static struct md_sysfs_entry md_sync_max
=
4150 __ATTR(sync_speed_max
, S_IRUGO
|S_IWUSR
, sync_max_show
, sync_max_store
);
4153 degraded_show(mddev_t
*mddev
, char *page
)
4155 return sprintf(page
, "%d\n", mddev
->degraded
);
4157 static struct md_sysfs_entry md_degraded
= __ATTR_RO(degraded
);
4160 sync_force_parallel_show(mddev_t
*mddev
, char *page
)
4162 return sprintf(page
, "%d\n", mddev
->parallel_resync
);
4166 sync_force_parallel_store(mddev_t
*mddev
, const char *buf
, size_t len
)
4170 if (strict_strtol(buf
, 10, &n
))
4173 if (n
!= 0 && n
!= 1)
4176 mddev
->parallel_resync
= n
;
4178 if (mddev
->sync_thread
)
4179 wake_up(&resync_wait
);
4184 /* force parallel resync, even with shared block devices */
4185 static struct md_sysfs_entry md_sync_force_parallel
=
4186 __ATTR(sync_force_parallel
, S_IRUGO
|S_IWUSR
,
4187 sync_force_parallel_show
, sync_force_parallel_store
);
4190 sync_speed_show(mddev_t
*mddev
, char *page
)
4192 unsigned long resync
, dt
, db
;
4193 if (mddev
->curr_resync
== 0)
4194 return sprintf(page
, "none\n");
4195 resync
= mddev
->curr_mark_cnt
- atomic_read(&mddev
->recovery_active
);
4196 dt
= (jiffies
- mddev
->resync_mark
) / HZ
;
4198 db
= resync
- mddev
->resync_mark_cnt
;
4199 return sprintf(page
, "%lu\n", db
/dt
/2); /* K/sec */
4202 static struct md_sysfs_entry md_sync_speed
= __ATTR_RO(sync_speed
);
4205 sync_completed_show(mddev_t
*mddev
, char *page
)
4207 unsigned long long max_sectors
, resync
;
4209 if (!test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
4210 return sprintf(page
, "none\n");
4212 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
4213 max_sectors
= mddev
->resync_max_sectors
;
4215 max_sectors
= mddev
->dev_sectors
;
4217 resync
= mddev
->curr_resync_completed
;
4218 return sprintf(page
, "%llu / %llu\n", resync
, max_sectors
);
4221 static struct md_sysfs_entry md_sync_completed
= __ATTR_RO(sync_completed
);
4224 min_sync_show(mddev_t
*mddev
, char *page
)
4226 return sprintf(page
, "%llu\n",
4227 (unsigned long long)mddev
->resync_min
);
4230 min_sync_store(mddev_t
*mddev
, const char *buf
, size_t len
)
4232 unsigned long long min
;
4233 if (strict_strtoull(buf
, 10, &min
))
4235 if (min
> mddev
->resync_max
)
4237 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
4240 /* Must be a multiple of chunk_size */
4241 if (mddev
->chunk_sectors
) {
4242 sector_t temp
= min
;
4243 if (sector_div(temp
, mddev
->chunk_sectors
))
4246 mddev
->resync_min
= min
;
4251 static struct md_sysfs_entry md_min_sync
=
4252 __ATTR(sync_min
, S_IRUGO
|S_IWUSR
, min_sync_show
, min_sync_store
);
4255 max_sync_show(mddev_t
*mddev
, char *page
)
4257 if (mddev
->resync_max
== MaxSector
)
4258 return sprintf(page
, "max\n");
4260 return sprintf(page
, "%llu\n",
4261 (unsigned long long)mddev
->resync_max
);
4264 max_sync_store(mddev_t
*mddev
, const char *buf
, size_t len
)
4266 if (strncmp(buf
, "max", 3) == 0)
4267 mddev
->resync_max
= MaxSector
;
4269 unsigned long long max
;
4270 if (strict_strtoull(buf
, 10, &max
))
4272 if (max
< mddev
->resync_min
)
4274 if (max
< mddev
->resync_max
&&
4276 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
4279 /* Must be a multiple of chunk_size */
4280 if (mddev
->chunk_sectors
) {
4281 sector_t temp
= max
;
4282 if (sector_div(temp
, mddev
->chunk_sectors
))
4285 mddev
->resync_max
= max
;
4287 wake_up(&mddev
->recovery_wait
);
4291 static struct md_sysfs_entry md_max_sync
=
4292 __ATTR(sync_max
, S_IRUGO
|S_IWUSR
, max_sync_show
, max_sync_store
);
4295 suspend_lo_show(mddev_t
*mddev
, char *page
)
4297 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->suspend_lo
);
4301 suspend_lo_store(mddev_t
*mddev
, const char *buf
, size_t len
)
4304 unsigned long long new = simple_strtoull(buf
, &e
, 10);
4305 unsigned long long old
= mddev
->suspend_lo
;
4307 if (mddev
->pers
== NULL
||
4308 mddev
->pers
->quiesce
== NULL
)
4310 if (buf
== e
|| (*e
&& *e
!= '\n'))
4313 mddev
->suspend_lo
= new;
4315 /* Shrinking suspended region */
4316 mddev
->pers
->quiesce(mddev
, 2);
4318 /* Expanding suspended region - need to wait */
4319 mddev
->pers
->quiesce(mddev
, 1);
4320 mddev
->pers
->quiesce(mddev
, 0);
4324 static struct md_sysfs_entry md_suspend_lo
=
4325 __ATTR(suspend_lo
, S_IRUGO
|S_IWUSR
, suspend_lo_show
, suspend_lo_store
);
4329 suspend_hi_show(mddev_t
*mddev
, char *page
)
4331 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->suspend_hi
);
4335 suspend_hi_store(mddev_t
*mddev
, const char *buf
, size_t len
)
4338 unsigned long long new = simple_strtoull(buf
, &e
, 10);
4339 unsigned long long old
= mddev
->suspend_hi
;
4341 if (mddev
->pers
== NULL
||
4342 mddev
->pers
->quiesce
== NULL
)
4344 if (buf
== e
|| (*e
&& *e
!= '\n'))
4347 mddev
->suspend_hi
= new;
4349 /* Shrinking suspended region */
4350 mddev
->pers
->quiesce(mddev
, 2);
4352 /* Expanding suspended region - need to wait */
4353 mddev
->pers
->quiesce(mddev
, 1);
4354 mddev
->pers
->quiesce(mddev
, 0);
4358 static struct md_sysfs_entry md_suspend_hi
=
4359 __ATTR(suspend_hi
, S_IRUGO
|S_IWUSR
, suspend_hi_show
, suspend_hi_store
);
4362 reshape_position_show(mddev_t
*mddev
, char *page
)
4364 if (mddev
->reshape_position
!= MaxSector
)
4365 return sprintf(page
, "%llu\n",
4366 (unsigned long long)mddev
->reshape_position
);
4367 strcpy(page
, "none\n");
4372 reshape_position_store(mddev_t
*mddev
, const char *buf
, size_t len
)
4375 unsigned long long new = simple_strtoull(buf
, &e
, 10);
4378 if (buf
== e
|| (*e
&& *e
!= '\n'))
4380 mddev
->reshape_position
= new;
4381 mddev
->delta_disks
= 0;
4382 mddev
->new_level
= mddev
->level
;
4383 mddev
->new_layout
= mddev
->layout
;
4384 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
4388 static struct md_sysfs_entry md_reshape_position
=
4389 __ATTR(reshape_position
, S_IRUGO
|S_IWUSR
, reshape_position_show
,
4390 reshape_position_store
);
4393 array_size_show(mddev_t
*mddev
, char *page
)
4395 if (mddev
->external_size
)
4396 return sprintf(page
, "%llu\n",
4397 (unsigned long long)mddev
->array_sectors
/2);
4399 return sprintf(page
, "default\n");
4403 array_size_store(mddev_t
*mddev
, const char *buf
, size_t len
)
4407 if (strncmp(buf
, "default", 7) == 0) {
4409 sectors
= mddev
->pers
->size(mddev
, 0, 0);
4411 sectors
= mddev
->array_sectors
;
4413 mddev
->external_size
= 0;
4415 if (strict_blocks_to_sectors(buf
, §ors
) < 0)
4417 if (mddev
->pers
&& mddev
->pers
->size(mddev
, 0, 0) < sectors
)
4420 mddev
->external_size
= 1;
4423 mddev
->array_sectors
= sectors
;
4425 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
4426 revalidate_disk(mddev
->gendisk
);
4431 static struct md_sysfs_entry md_array_size
=
4432 __ATTR(array_size
, S_IRUGO
|S_IWUSR
, array_size_show
,
4435 static struct attribute
*md_default_attrs
[] = {
4438 &md_raid_disks
.attr
,
4439 &md_chunk_size
.attr
,
4441 &md_resync_start
.attr
,
4443 &md_new_device
.attr
,
4444 &md_safe_delay
.attr
,
4445 &md_array_state
.attr
,
4446 &md_reshape_position
.attr
,
4447 &md_array_size
.attr
,
4448 &max_corr_read_errors
.attr
,
4452 static struct attribute
*md_redundancy_attrs
[] = {
4454 &md_mismatches
.attr
,
4457 &md_sync_speed
.attr
,
4458 &md_sync_force_parallel
.attr
,
4459 &md_sync_completed
.attr
,
4462 &md_suspend_lo
.attr
,
4463 &md_suspend_hi
.attr
,
4468 static struct attribute_group md_redundancy_group
= {
4470 .attrs
= md_redundancy_attrs
,
4475 md_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
4477 struct md_sysfs_entry
*entry
= container_of(attr
, struct md_sysfs_entry
, attr
);
4478 mddev_t
*mddev
= container_of(kobj
, struct mddev_s
, kobj
);
4483 rv
= mddev_lock(mddev
);
4485 rv
= entry
->show(mddev
, page
);
4486 mddev_unlock(mddev
);
4492 md_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
4493 const char *page
, size_t length
)
4495 struct md_sysfs_entry
*entry
= container_of(attr
, struct md_sysfs_entry
, attr
);
4496 mddev_t
*mddev
= container_of(kobj
, struct mddev_s
, kobj
);
4501 if (!capable(CAP_SYS_ADMIN
))
4503 rv
= mddev_lock(mddev
);
4504 if (mddev
->hold_active
== UNTIL_IOCTL
)
4505 mddev
->hold_active
= 0;
4507 rv
= entry
->store(mddev
, page
, length
);
4508 mddev_unlock(mddev
);
4513 static void md_free(struct kobject
*ko
)
4515 mddev_t
*mddev
= container_of(ko
, mddev_t
, kobj
);
4517 if (mddev
->sysfs_state
)
4518 sysfs_put(mddev
->sysfs_state
);
4520 if (mddev
->gendisk
) {
4521 del_gendisk(mddev
->gendisk
);
4522 put_disk(mddev
->gendisk
);
4525 blk_cleanup_queue(mddev
->queue
);
4530 static const struct sysfs_ops md_sysfs_ops
= {
4531 .show
= md_attr_show
,
4532 .store
= md_attr_store
,
4534 static struct kobj_type md_ktype
= {
4536 .sysfs_ops
= &md_sysfs_ops
,
4537 .default_attrs
= md_default_attrs
,
4542 static void mddev_delayed_delete(struct work_struct
*ws
)
4544 mddev_t
*mddev
= container_of(ws
, mddev_t
, del_work
);
4546 sysfs_remove_group(&mddev
->kobj
, &md_bitmap_group
);
4547 kobject_del(&mddev
->kobj
);
4548 kobject_put(&mddev
->kobj
);
4551 static int md_alloc(dev_t dev
, char *name
)
4553 static DEFINE_MUTEX(disks_mutex
);
4554 mddev_t
*mddev
= mddev_find(dev
);
4555 struct gendisk
*disk
;
4564 partitioned
= (MAJOR(mddev
->unit
) != MD_MAJOR
);
4565 shift
= partitioned
? MdpMinorShift
: 0;
4566 unit
= MINOR(mddev
->unit
) >> shift
;
4568 /* wait for any previous instance of this device to be
4569 * completely removed (mddev_delayed_delete).
4571 flush_workqueue(md_misc_wq
);
4573 mutex_lock(&disks_mutex
);
4579 /* Need to ensure that 'name' is not a duplicate.
4582 spin_lock(&all_mddevs_lock
);
4584 list_for_each_entry(mddev2
, &all_mddevs
, all_mddevs
)
4585 if (mddev2
->gendisk
&&
4586 strcmp(mddev2
->gendisk
->disk_name
, name
) == 0) {
4587 spin_unlock(&all_mddevs_lock
);
4590 spin_unlock(&all_mddevs_lock
);
4594 mddev
->queue
= blk_alloc_queue(GFP_KERNEL
);
4597 mddev
->queue
->queuedata
= mddev
;
4599 blk_queue_make_request(mddev
->queue
, md_make_request
);
4601 disk
= alloc_disk(1 << shift
);
4603 blk_cleanup_queue(mddev
->queue
);
4604 mddev
->queue
= NULL
;
4607 disk
->major
= MAJOR(mddev
->unit
);
4608 disk
->first_minor
= unit
<< shift
;
4610 strcpy(disk
->disk_name
, name
);
4611 else if (partitioned
)
4612 sprintf(disk
->disk_name
, "md_d%d", unit
);
4614 sprintf(disk
->disk_name
, "md%d", unit
);
4615 disk
->fops
= &md_fops
;
4616 disk
->private_data
= mddev
;
4617 disk
->queue
= mddev
->queue
;
4618 blk_queue_flush(mddev
->queue
, REQ_FLUSH
| REQ_FUA
);
4619 /* Allow extended partitions. This makes the
4620 * 'mdp' device redundant, but we can't really
4623 disk
->flags
|= GENHD_FL_EXT_DEVT
;
4624 mddev
->gendisk
= disk
;
4625 /* As soon as we call add_disk(), another thread could get
4626 * through to md_open, so make sure it doesn't get too far
4628 mutex_lock(&mddev
->open_mutex
);
4631 error
= kobject_init_and_add(&mddev
->kobj
, &md_ktype
,
4632 &disk_to_dev(disk
)->kobj
, "%s", "md");
4634 /* This isn't possible, but as kobject_init_and_add is marked
4635 * __must_check, we must do something with the result
4637 printk(KERN_WARNING
"md: cannot register %s/md - name in use\n",
4641 if (mddev
->kobj
.sd
&&
4642 sysfs_create_group(&mddev
->kobj
, &md_bitmap_group
))
4643 printk(KERN_DEBUG
"pointless warning\n");
4644 mutex_unlock(&mddev
->open_mutex
);
4646 mutex_unlock(&disks_mutex
);
4647 if (!error
&& mddev
->kobj
.sd
) {
4648 kobject_uevent(&mddev
->kobj
, KOBJ_ADD
);
4649 mddev
->sysfs_state
= sysfs_get_dirent_safe(mddev
->kobj
.sd
, "array_state");
4655 static struct kobject
*md_probe(dev_t dev
, int *part
, void *data
)
4657 md_alloc(dev
, NULL
);
4661 static int add_named_array(const char *val
, struct kernel_param
*kp
)
4663 /* val must be "md_*" where * is not all digits.
4664 * We allocate an array with a large free minor number, and
4665 * set the name to val. val must not already be an active name.
4667 int len
= strlen(val
);
4668 char buf
[DISK_NAME_LEN
];
4670 while (len
&& val
[len
-1] == '\n')
4672 if (len
>= DISK_NAME_LEN
)
4674 strlcpy(buf
, val
, len
+1);
4675 if (strncmp(buf
, "md_", 3) != 0)
4677 return md_alloc(0, buf
);
4680 static void md_safemode_timeout(unsigned long data
)
4682 mddev_t
*mddev
= (mddev_t
*) data
;
4684 if (!atomic_read(&mddev
->writes_pending
)) {
4685 mddev
->safemode
= 1;
4686 if (mddev
->external
)
4687 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4689 md_wakeup_thread(mddev
->thread
);
4692 static int start_dirty_degraded
;
4694 int md_run(mddev_t
*mddev
)
4698 struct mdk_personality
*pers
;
4700 if (list_empty(&mddev
->disks
))
4701 /* cannot run an array with no devices.. */
4706 /* Cannot run until previous stop completes properly */
4707 if (mddev
->sysfs_active
)
4711 * Analyze all RAID superblock(s)
4713 if (!mddev
->raid_disks
) {
4714 if (!mddev
->persistent
)
4719 if (mddev
->level
!= LEVEL_NONE
)
4720 request_module("md-level-%d", mddev
->level
);
4721 else if (mddev
->clevel
[0])
4722 request_module("md-%s", mddev
->clevel
);
4725 * Drop all container device buffers, from now on
4726 * the only valid external interface is through the md
4729 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
4730 if (test_bit(Faulty
, &rdev
->flags
))
4732 sync_blockdev(rdev
->bdev
);
4733 invalidate_bdev(rdev
->bdev
);
4735 /* perform some consistency tests on the device.
4736 * We don't want the data to overlap the metadata,
4737 * Internal Bitmap issues have been handled elsewhere.
4739 if (rdev
->meta_bdev
) {
4740 /* Nothing to check */;
4741 } else if (rdev
->data_offset
< rdev
->sb_start
) {
4742 if (mddev
->dev_sectors
&&
4743 rdev
->data_offset
+ mddev
->dev_sectors
4745 printk("md: %s: data overlaps metadata\n",
4750 if (rdev
->sb_start
+ rdev
->sb_size
/512
4751 > rdev
->data_offset
) {
4752 printk("md: %s: metadata overlaps data\n",
4757 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
4760 if (mddev
->bio_set
== NULL
)
4761 mddev
->bio_set
= bioset_create(BIO_POOL_SIZE
,
4764 spin_lock(&pers_lock
);
4765 pers
= find_pers(mddev
->level
, mddev
->clevel
);
4766 if (!pers
|| !try_module_get(pers
->owner
)) {
4767 spin_unlock(&pers_lock
);
4768 if (mddev
->level
!= LEVEL_NONE
)
4769 printk(KERN_WARNING
"md: personality for level %d is not loaded!\n",
4772 printk(KERN_WARNING
"md: personality for level %s is not loaded!\n",
4777 spin_unlock(&pers_lock
);
4778 if (mddev
->level
!= pers
->level
) {
4779 mddev
->level
= pers
->level
;
4780 mddev
->new_level
= pers
->level
;
4782 strlcpy(mddev
->clevel
, pers
->name
, sizeof(mddev
->clevel
));
4784 if (mddev
->reshape_position
!= MaxSector
&&
4785 pers
->start_reshape
== NULL
) {
4786 /* This personality cannot handle reshaping... */
4788 module_put(pers
->owner
);
4792 if (pers
->sync_request
) {
4793 /* Warn if this is a potentially silly
4796 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
4800 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
4801 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
) {
4803 rdev
->bdev
->bd_contains
==
4804 rdev2
->bdev
->bd_contains
) {
4806 "%s: WARNING: %s appears to be"
4807 " on the same physical disk as"
4810 bdevname(rdev
->bdev
,b
),
4811 bdevname(rdev2
->bdev
,b2
));
4818 "True protection against single-disk"
4819 " failure might be compromised.\n");
4822 mddev
->recovery
= 0;
4823 /* may be over-ridden by personality */
4824 mddev
->resync_max_sectors
= mddev
->dev_sectors
;
4826 mddev
->ok_start_degraded
= start_dirty_degraded
;
4828 if (start_readonly
&& mddev
->ro
== 0)
4829 mddev
->ro
= 2; /* read-only, but switch on first write */
4831 err
= mddev
->pers
->run(mddev
);
4833 printk(KERN_ERR
"md: pers->run() failed ...\n");
4834 else if (mddev
->pers
->size(mddev
, 0, 0) < mddev
->array_sectors
) {
4835 WARN_ONCE(!mddev
->external_size
, "%s: default size too small,"
4836 " but 'external_size' not in effect?\n", __func__
);
4838 "md: invalid array_size %llu > default size %llu\n",
4839 (unsigned long long)mddev
->array_sectors
/ 2,
4840 (unsigned long long)mddev
->pers
->size(mddev
, 0, 0) / 2);
4842 mddev
->pers
->stop(mddev
);
4844 if (err
== 0 && mddev
->pers
->sync_request
) {
4845 err
= bitmap_create(mddev
);
4847 printk(KERN_ERR
"%s: failed to create bitmap (%d)\n",
4848 mdname(mddev
), err
);
4849 mddev
->pers
->stop(mddev
);
4853 module_put(mddev
->pers
->owner
);
4855 bitmap_destroy(mddev
);
4858 if (mddev
->pers
->sync_request
) {
4859 if (mddev
->kobj
.sd
&&
4860 sysfs_create_group(&mddev
->kobj
, &md_redundancy_group
))
4862 "md: cannot register extra attributes for %s\n",
4864 mddev
->sysfs_action
= sysfs_get_dirent_safe(mddev
->kobj
.sd
, "sync_action");
4865 } else if (mddev
->ro
== 2) /* auto-readonly not meaningful */
4868 atomic_set(&mddev
->writes_pending
,0);
4869 atomic_set(&mddev
->max_corr_read_errors
,
4870 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS
);
4871 mddev
->safemode
= 0;
4872 mddev
->safemode_timer
.function
= md_safemode_timeout
;
4873 mddev
->safemode_timer
.data
= (unsigned long) mddev
;
4874 mddev
->safemode_delay
= (200 * HZ
)/1000 +1; /* 200 msec delay */
4878 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
4879 if (rdev
->raid_disk
>= 0)
4880 if (sysfs_link_rdev(mddev
, rdev
))
4881 /* failure here is OK */;
4883 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4886 md_update_sb(mddev
, 0);
4888 md_new_event(mddev
);
4889 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4890 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
4891 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
4894 EXPORT_SYMBOL_GPL(md_run
);
4896 static int do_md_run(mddev_t
*mddev
)
4900 err
= md_run(mddev
);
4903 err
= bitmap_load(mddev
);
4905 bitmap_destroy(mddev
);
4909 md_wakeup_thread(mddev
->thread
);
4910 md_wakeup_thread(mddev
->sync_thread
); /* possibly kick off a reshape */
4912 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
4913 revalidate_disk(mddev
->gendisk
);
4915 kobject_uevent(&disk_to_dev(mddev
->gendisk
)->kobj
, KOBJ_CHANGE
);
4920 static int restart_array(mddev_t
*mddev
)
4922 struct gendisk
*disk
= mddev
->gendisk
;
4924 /* Complain if it has no devices */
4925 if (list_empty(&mddev
->disks
))
4931 mddev
->safemode
= 0;
4933 set_disk_ro(disk
, 0);
4934 printk(KERN_INFO
"md: %s switched to read-write mode.\n",
4936 /* Kick recovery or resync if necessary */
4937 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4938 md_wakeup_thread(mddev
->thread
);
4939 md_wakeup_thread(mddev
->sync_thread
);
4940 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4944 /* similar to deny_write_access, but accounts for our holding a reference
4945 * to the file ourselves */
4946 static int deny_bitmap_write_access(struct file
* file
)
4948 struct inode
*inode
= file
->f_mapping
->host
;
4950 spin_lock(&inode
->i_lock
);
4951 if (atomic_read(&inode
->i_writecount
) > 1) {
4952 spin_unlock(&inode
->i_lock
);
4955 atomic_set(&inode
->i_writecount
, -1);
4956 spin_unlock(&inode
->i_lock
);
4961 void restore_bitmap_write_access(struct file
*file
)
4963 struct inode
*inode
= file
->f_mapping
->host
;
4965 spin_lock(&inode
->i_lock
);
4966 atomic_set(&inode
->i_writecount
, 1);
4967 spin_unlock(&inode
->i_lock
);
4970 static void md_clean(mddev_t
*mddev
)
4972 mddev
->array_sectors
= 0;
4973 mddev
->external_size
= 0;
4974 mddev
->dev_sectors
= 0;
4975 mddev
->raid_disks
= 0;
4976 mddev
->recovery_cp
= 0;
4977 mddev
->resync_min
= 0;
4978 mddev
->resync_max
= MaxSector
;
4979 mddev
->reshape_position
= MaxSector
;
4980 mddev
->external
= 0;
4981 mddev
->persistent
= 0;
4982 mddev
->level
= LEVEL_NONE
;
4983 mddev
->clevel
[0] = 0;
4986 mddev
->metadata_type
[0] = 0;
4987 mddev
->chunk_sectors
= 0;
4988 mddev
->ctime
= mddev
->utime
= 0;
4990 mddev
->max_disks
= 0;
4992 mddev
->can_decrease_events
= 0;
4993 mddev
->delta_disks
= 0;
4994 mddev
->new_level
= LEVEL_NONE
;
4995 mddev
->new_layout
= 0;
4996 mddev
->new_chunk_sectors
= 0;
4997 mddev
->curr_resync
= 0;
4998 mddev
->resync_mismatches
= 0;
4999 mddev
->suspend_lo
= mddev
->suspend_hi
= 0;
5000 mddev
->sync_speed_min
= mddev
->sync_speed_max
= 0;
5001 mddev
->recovery
= 0;
5004 mddev
->degraded
= 0;
5005 mddev
->safemode
= 0;
5006 mddev
->bitmap_info
.offset
= 0;
5007 mddev
->bitmap_info
.default_offset
= 0;
5008 mddev
->bitmap_info
.chunksize
= 0;
5009 mddev
->bitmap_info
.daemon_sleep
= 0;
5010 mddev
->bitmap_info
.max_write_behind
= 0;
5013 static void __md_stop_writes(mddev_t
*mddev
)
5015 if (mddev
->sync_thread
) {
5016 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
5017 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
5018 reap_sync_thread(mddev
);
5021 del_timer_sync(&mddev
->safemode_timer
);
5023 bitmap_flush(mddev
);
5024 md_super_wait(mddev
);
5026 if (!mddev
->in_sync
|| mddev
->flags
) {
5027 /* mark array as shutdown cleanly */
5029 md_update_sb(mddev
, 1);
5033 void md_stop_writes(mddev_t
*mddev
)
5036 __md_stop_writes(mddev
);
5037 mddev_unlock(mddev
);
5039 EXPORT_SYMBOL_GPL(md_stop_writes
);
5041 void md_stop(mddev_t
*mddev
)
5044 mddev
->pers
->stop(mddev
);
5045 if (mddev
->pers
->sync_request
&& mddev
->to_remove
== NULL
)
5046 mddev
->to_remove
= &md_redundancy_group
;
5047 module_put(mddev
->pers
->owner
);
5049 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
5051 EXPORT_SYMBOL_GPL(md_stop
);
5053 static int md_set_readonly(mddev_t
*mddev
, int is_open
)
5056 mutex_lock(&mddev
->open_mutex
);
5057 if (atomic_read(&mddev
->openers
) > is_open
) {
5058 printk("md: %s still in use.\n",mdname(mddev
));
5063 __md_stop_writes(mddev
);
5069 set_disk_ro(mddev
->gendisk
, 1);
5070 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
5071 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
5075 mutex_unlock(&mddev
->open_mutex
);
5080 * 0 - completely stop and dis-assemble array
5081 * 2 - stop but do not disassemble array
5083 static int do_md_stop(mddev_t
* mddev
, int mode
, int is_open
)
5085 struct gendisk
*disk
= mddev
->gendisk
;
5088 mutex_lock(&mddev
->open_mutex
);
5089 if (atomic_read(&mddev
->openers
) > is_open
||
5090 mddev
->sysfs_active
) {
5091 printk("md: %s still in use.\n",mdname(mddev
));
5092 mutex_unlock(&mddev
->open_mutex
);
5098 set_disk_ro(disk
, 0);
5100 __md_stop_writes(mddev
);
5102 mddev
->queue
->merge_bvec_fn
= NULL
;
5103 mddev
->queue
->backing_dev_info
.congested_fn
= NULL
;
5105 /* tell userspace to handle 'inactive' */
5106 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
5108 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
5109 if (rdev
->raid_disk
>= 0)
5110 sysfs_unlink_rdev(mddev
, rdev
);
5112 set_capacity(disk
, 0);
5113 mutex_unlock(&mddev
->open_mutex
);
5115 revalidate_disk(disk
);
5120 mutex_unlock(&mddev
->open_mutex
);
5122 * Free resources if final stop
5125 printk(KERN_INFO
"md: %s stopped.\n", mdname(mddev
));
5127 bitmap_destroy(mddev
);
5128 if (mddev
->bitmap_info
.file
) {
5129 restore_bitmap_write_access(mddev
->bitmap_info
.file
);
5130 fput(mddev
->bitmap_info
.file
);
5131 mddev
->bitmap_info
.file
= NULL
;
5133 mddev
->bitmap_info
.offset
= 0;
5135 export_array(mddev
);
5138 kobject_uevent(&disk_to_dev(mddev
->gendisk
)->kobj
, KOBJ_CHANGE
);
5139 if (mddev
->hold_active
== UNTIL_STOP
)
5140 mddev
->hold_active
= 0;
5142 blk_integrity_unregister(disk
);
5143 md_new_event(mddev
);
5144 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
5149 static void autorun_array(mddev_t
*mddev
)
5154 if (list_empty(&mddev
->disks
))
5157 printk(KERN_INFO
"md: running: ");
5159 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
5160 char b
[BDEVNAME_SIZE
];
5161 printk("<%s>", bdevname(rdev
->bdev
,b
));
5165 err
= do_md_run(mddev
);
5167 printk(KERN_WARNING
"md: do_md_run() returned %d\n", err
);
5168 do_md_stop(mddev
, 0, 0);
5173 * lets try to run arrays based on all disks that have arrived
5174 * until now. (those are in pending_raid_disks)
5176 * the method: pick the first pending disk, collect all disks with
5177 * the same UUID, remove all from the pending list and put them into
5178 * the 'same_array' list. Then order this list based on superblock
5179 * update time (freshest comes first), kick out 'old' disks and
5180 * compare superblocks. If everything's fine then run it.
5182 * If "unit" is allocated, then bump its reference count
5184 static void autorun_devices(int part
)
5186 mdk_rdev_t
*rdev0
, *rdev
, *tmp
;
5188 char b
[BDEVNAME_SIZE
];
5190 printk(KERN_INFO
"md: autorun ...\n");
5191 while (!list_empty(&pending_raid_disks
)) {
5194 LIST_HEAD(candidates
);
5195 rdev0
= list_entry(pending_raid_disks
.next
,
5196 mdk_rdev_t
, same_set
);
5198 printk(KERN_INFO
"md: considering %s ...\n",
5199 bdevname(rdev0
->bdev
,b
));
5200 INIT_LIST_HEAD(&candidates
);
5201 rdev_for_each_list(rdev
, tmp
, &pending_raid_disks
)
5202 if (super_90_load(rdev
, rdev0
, 0) >= 0) {
5203 printk(KERN_INFO
"md: adding %s ...\n",
5204 bdevname(rdev
->bdev
,b
));
5205 list_move(&rdev
->same_set
, &candidates
);
5208 * now we have a set of devices, with all of them having
5209 * mostly sane superblocks. It's time to allocate the
5213 dev
= MKDEV(mdp_major
,
5214 rdev0
->preferred_minor
<< MdpMinorShift
);
5215 unit
= MINOR(dev
) >> MdpMinorShift
;
5217 dev
= MKDEV(MD_MAJOR
, rdev0
->preferred_minor
);
5220 if (rdev0
->preferred_minor
!= unit
) {
5221 printk(KERN_INFO
"md: unit number in %s is bad: %d\n",
5222 bdevname(rdev0
->bdev
, b
), rdev0
->preferred_minor
);
5226 md_probe(dev
, NULL
, NULL
);
5227 mddev
= mddev_find(dev
);
5228 if (!mddev
|| !mddev
->gendisk
) {
5232 "md: cannot allocate memory for md drive.\n");
5235 if (mddev_lock(mddev
))
5236 printk(KERN_WARNING
"md: %s locked, cannot run\n",
5238 else if (mddev
->raid_disks
|| mddev
->major_version
5239 || !list_empty(&mddev
->disks
)) {
5241 "md: %s already running, cannot run %s\n",
5242 mdname(mddev
), bdevname(rdev0
->bdev
,b
));
5243 mddev_unlock(mddev
);
5245 printk(KERN_INFO
"md: created %s\n", mdname(mddev
));
5246 mddev
->persistent
= 1;
5247 rdev_for_each_list(rdev
, tmp
, &candidates
) {
5248 list_del_init(&rdev
->same_set
);
5249 if (bind_rdev_to_array(rdev
, mddev
))
5252 autorun_array(mddev
);
5253 mddev_unlock(mddev
);
5255 /* on success, candidates will be empty, on error
5258 rdev_for_each_list(rdev
, tmp
, &candidates
) {
5259 list_del_init(&rdev
->same_set
);
5264 printk(KERN_INFO
"md: ... autorun DONE.\n");
5266 #endif /* !MODULE */
5268 static int get_version(void __user
* arg
)
5272 ver
.major
= MD_MAJOR_VERSION
;
5273 ver
.minor
= MD_MINOR_VERSION
;
5274 ver
.patchlevel
= MD_PATCHLEVEL_VERSION
;
5276 if (copy_to_user(arg
, &ver
, sizeof(ver
)))
5282 static int get_array_info(mddev_t
* mddev
, void __user
* arg
)
5284 mdu_array_info_t info
;
5285 int nr
,working
,insync
,failed
,spare
;
5288 nr
=working
=insync
=failed
=spare
=0;
5289 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
5291 if (test_bit(Faulty
, &rdev
->flags
))
5295 if (test_bit(In_sync
, &rdev
->flags
))
5302 info
.major_version
= mddev
->major_version
;
5303 info
.minor_version
= mddev
->minor_version
;
5304 info
.patch_version
= MD_PATCHLEVEL_VERSION
;
5305 info
.ctime
= mddev
->ctime
;
5306 info
.level
= mddev
->level
;
5307 info
.size
= mddev
->dev_sectors
/ 2;
5308 if (info
.size
!= mddev
->dev_sectors
/ 2) /* overflow */
5311 info
.raid_disks
= mddev
->raid_disks
;
5312 info
.md_minor
= mddev
->md_minor
;
5313 info
.not_persistent
= !mddev
->persistent
;
5315 info
.utime
= mddev
->utime
;
5318 info
.state
= (1<<MD_SB_CLEAN
);
5319 if (mddev
->bitmap
&& mddev
->bitmap_info
.offset
)
5320 info
.state
= (1<<MD_SB_BITMAP_PRESENT
);
5321 info
.active_disks
= insync
;
5322 info
.working_disks
= working
;
5323 info
.failed_disks
= failed
;
5324 info
.spare_disks
= spare
;
5326 info
.layout
= mddev
->layout
;
5327 info
.chunk_size
= mddev
->chunk_sectors
<< 9;
5329 if (copy_to_user(arg
, &info
, sizeof(info
)))
5335 static int get_bitmap_file(mddev_t
* mddev
, void __user
* arg
)
5337 mdu_bitmap_file_t
*file
= NULL
; /* too big for stack allocation */
5338 char *ptr
, *buf
= NULL
;
5341 if (md_allow_write(mddev
))
5342 file
= kmalloc(sizeof(*file
), GFP_NOIO
);
5344 file
= kmalloc(sizeof(*file
), GFP_KERNEL
);
5349 /* bitmap disabled, zero the first byte and copy out */
5350 if (!mddev
->bitmap
|| !mddev
->bitmap
->file
) {
5351 file
->pathname
[0] = '\0';
5355 buf
= kmalloc(sizeof(file
->pathname
), GFP_KERNEL
);
5359 ptr
= d_path(&mddev
->bitmap
->file
->f_path
, buf
, sizeof(file
->pathname
));
5363 strcpy(file
->pathname
, ptr
);
5367 if (copy_to_user(arg
, file
, sizeof(*file
)))
5375 static int get_disk_info(mddev_t
* mddev
, void __user
* arg
)
5377 mdu_disk_info_t info
;
5380 if (copy_from_user(&info
, arg
, sizeof(info
)))
5383 rdev
= find_rdev_nr(mddev
, info
.number
);
5385 info
.major
= MAJOR(rdev
->bdev
->bd_dev
);
5386 info
.minor
= MINOR(rdev
->bdev
->bd_dev
);
5387 info
.raid_disk
= rdev
->raid_disk
;
5389 if (test_bit(Faulty
, &rdev
->flags
))
5390 info
.state
|= (1<<MD_DISK_FAULTY
);
5391 else if (test_bit(In_sync
, &rdev
->flags
)) {
5392 info
.state
|= (1<<MD_DISK_ACTIVE
);
5393 info
.state
|= (1<<MD_DISK_SYNC
);
5395 if (test_bit(WriteMostly
, &rdev
->flags
))
5396 info
.state
|= (1<<MD_DISK_WRITEMOSTLY
);
5398 info
.major
= info
.minor
= 0;
5399 info
.raid_disk
= -1;
5400 info
.state
= (1<<MD_DISK_REMOVED
);
5403 if (copy_to_user(arg
, &info
, sizeof(info
)))
5409 static int add_new_disk(mddev_t
* mddev
, mdu_disk_info_t
*info
)
5411 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
5413 dev_t dev
= MKDEV(info
->major
,info
->minor
);
5415 if (info
->major
!= MAJOR(dev
) || info
->minor
!= MINOR(dev
))
5418 if (!mddev
->raid_disks
) {
5420 /* expecting a device which has a superblock */
5421 rdev
= md_import_device(dev
, mddev
->major_version
, mddev
->minor_version
);
5424 "md: md_import_device returned %ld\n",
5426 return PTR_ERR(rdev
);
5428 if (!list_empty(&mddev
->disks
)) {
5429 mdk_rdev_t
*rdev0
= list_entry(mddev
->disks
.next
,
5430 mdk_rdev_t
, same_set
);
5431 err
= super_types
[mddev
->major_version
]
5432 .load_super(rdev
, rdev0
, mddev
->minor_version
);
5435 "md: %s has different UUID to %s\n",
5436 bdevname(rdev
->bdev
,b
),
5437 bdevname(rdev0
->bdev
,b2
));
5442 err
= bind_rdev_to_array(rdev
, mddev
);
5449 * add_new_disk can be used once the array is assembled
5450 * to add "hot spares". They must already have a superblock
5455 if (!mddev
->pers
->hot_add_disk
) {
5457 "%s: personality does not support diskops!\n",
5461 if (mddev
->persistent
)
5462 rdev
= md_import_device(dev
, mddev
->major_version
,
5463 mddev
->minor_version
);
5465 rdev
= md_import_device(dev
, -1, -1);
5468 "md: md_import_device returned %ld\n",
5470 return PTR_ERR(rdev
);
5472 /* set saved_raid_disk if appropriate */
5473 if (!mddev
->persistent
) {
5474 if (info
->state
& (1<<MD_DISK_SYNC
) &&
5475 info
->raid_disk
< mddev
->raid_disks
) {
5476 rdev
->raid_disk
= info
->raid_disk
;
5477 set_bit(In_sync
, &rdev
->flags
);
5479 rdev
->raid_disk
= -1;
5481 super_types
[mddev
->major_version
].
5482 validate_super(mddev
, rdev
);
5483 if ((info
->state
& (1<<MD_DISK_SYNC
)) &&
5484 (!test_bit(In_sync
, &rdev
->flags
) ||
5485 rdev
->raid_disk
!= info
->raid_disk
)) {
5486 /* This was a hot-add request, but events doesn't
5487 * match, so reject it.
5493 if (test_bit(In_sync
, &rdev
->flags
))
5494 rdev
->saved_raid_disk
= rdev
->raid_disk
;
5496 rdev
->saved_raid_disk
= -1;
5498 clear_bit(In_sync
, &rdev
->flags
); /* just to be sure */
5499 if (info
->state
& (1<<MD_DISK_WRITEMOSTLY
))
5500 set_bit(WriteMostly
, &rdev
->flags
);
5502 clear_bit(WriteMostly
, &rdev
->flags
);
5504 rdev
->raid_disk
= -1;
5505 err
= bind_rdev_to_array(rdev
, mddev
);
5506 if (!err
&& !mddev
->pers
->hot_remove_disk
) {
5507 /* If there is hot_add_disk but no hot_remove_disk
5508 * then added disks for geometry changes,
5509 * and should be added immediately.
5511 super_types
[mddev
->major_version
].
5512 validate_super(mddev
, rdev
);
5513 err
= mddev
->pers
->hot_add_disk(mddev
, rdev
);
5515 unbind_rdev_from_array(rdev
);
5520 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
5522 md_update_sb(mddev
, 1);
5523 if (mddev
->degraded
)
5524 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
5525 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5527 md_new_event(mddev
);
5528 md_wakeup_thread(mddev
->thread
);
5532 /* otherwise, add_new_disk is only allowed
5533 * for major_version==0 superblocks
5535 if (mddev
->major_version
!= 0) {
5536 printk(KERN_WARNING
"%s: ADD_NEW_DISK not supported\n",
5541 if (!(info
->state
& (1<<MD_DISK_FAULTY
))) {
5543 rdev
= md_import_device(dev
, -1, 0);
5546 "md: error, md_import_device() returned %ld\n",
5548 return PTR_ERR(rdev
);
5550 rdev
->desc_nr
= info
->number
;
5551 if (info
->raid_disk
< mddev
->raid_disks
)
5552 rdev
->raid_disk
= info
->raid_disk
;
5554 rdev
->raid_disk
= -1;
5556 if (rdev
->raid_disk
< mddev
->raid_disks
)
5557 if (info
->state
& (1<<MD_DISK_SYNC
))
5558 set_bit(In_sync
, &rdev
->flags
);
5560 if (info
->state
& (1<<MD_DISK_WRITEMOSTLY
))
5561 set_bit(WriteMostly
, &rdev
->flags
);
5563 if (!mddev
->persistent
) {
5564 printk(KERN_INFO
"md: nonpersistent superblock ...\n");
5565 rdev
->sb_start
= i_size_read(rdev
->bdev
->bd_inode
) / 512;
5567 rdev
->sb_start
= calc_dev_sboffset(rdev
);
5568 rdev
->sectors
= rdev
->sb_start
;
5570 err
= bind_rdev_to_array(rdev
, mddev
);
5580 static int hot_remove_disk(mddev_t
* mddev
, dev_t dev
)
5582 char b
[BDEVNAME_SIZE
];
5585 rdev
= find_rdev(mddev
, dev
);
5589 if (rdev
->raid_disk
>= 0)
5592 kick_rdev_from_array(rdev
);
5593 md_update_sb(mddev
, 1);
5594 md_new_event(mddev
);
5598 printk(KERN_WARNING
"md: cannot remove active disk %s from %s ...\n",
5599 bdevname(rdev
->bdev
,b
), mdname(mddev
));
5603 static int hot_add_disk(mddev_t
* mddev
, dev_t dev
)
5605 char b
[BDEVNAME_SIZE
];
5612 if (mddev
->major_version
!= 0) {
5613 printk(KERN_WARNING
"%s: HOT_ADD may only be used with"
5614 " version-0 superblocks.\n",
5618 if (!mddev
->pers
->hot_add_disk
) {
5620 "%s: personality does not support diskops!\n",
5625 rdev
= md_import_device(dev
, -1, 0);
5628 "md: error, md_import_device() returned %ld\n",
5633 if (mddev
->persistent
)
5634 rdev
->sb_start
= calc_dev_sboffset(rdev
);
5636 rdev
->sb_start
= i_size_read(rdev
->bdev
->bd_inode
) / 512;
5638 rdev
->sectors
= rdev
->sb_start
;
5640 if (test_bit(Faulty
, &rdev
->flags
)) {
5642 "md: can not hot-add faulty %s disk to %s!\n",
5643 bdevname(rdev
->bdev
,b
), mdname(mddev
));
5647 clear_bit(In_sync
, &rdev
->flags
);
5649 rdev
->saved_raid_disk
= -1;
5650 err
= bind_rdev_to_array(rdev
, mddev
);
5655 * The rest should better be atomic, we can have disk failures
5656 * noticed in interrupt contexts ...
5659 rdev
->raid_disk
= -1;
5661 md_update_sb(mddev
, 1);
5664 * Kick recovery, maybe this spare has to be added to the
5665 * array immediately.
5667 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5668 md_wakeup_thread(mddev
->thread
);
5669 md_new_event(mddev
);
5677 static int set_bitmap_file(mddev_t
*mddev
, int fd
)
5682 if (!mddev
->pers
->quiesce
)
5684 if (mddev
->recovery
|| mddev
->sync_thread
)
5686 /* we should be able to change the bitmap.. */
5692 return -EEXIST
; /* cannot add when bitmap is present */
5693 mddev
->bitmap_info
.file
= fget(fd
);
5695 if (mddev
->bitmap_info
.file
== NULL
) {
5696 printk(KERN_ERR
"%s: error: failed to get bitmap file\n",
5701 err
= deny_bitmap_write_access(mddev
->bitmap_info
.file
);
5703 printk(KERN_ERR
"%s: error: bitmap file is already in use\n",
5705 fput(mddev
->bitmap_info
.file
);
5706 mddev
->bitmap_info
.file
= NULL
;
5709 mddev
->bitmap_info
.offset
= 0; /* file overrides offset */
5710 } else if (mddev
->bitmap
== NULL
)
5711 return -ENOENT
; /* cannot remove what isn't there */
5714 mddev
->pers
->quiesce(mddev
, 1);
5716 err
= bitmap_create(mddev
);
5718 err
= bitmap_load(mddev
);
5720 if (fd
< 0 || err
) {
5721 bitmap_destroy(mddev
);
5722 fd
= -1; /* make sure to put the file */
5724 mddev
->pers
->quiesce(mddev
, 0);
5727 if (mddev
->bitmap_info
.file
) {
5728 restore_bitmap_write_access(mddev
->bitmap_info
.file
);
5729 fput(mddev
->bitmap_info
.file
);
5731 mddev
->bitmap_info
.file
= NULL
;
5738 * set_array_info is used two different ways
5739 * The original usage is when creating a new array.
5740 * In this usage, raid_disks is > 0 and it together with
5741 * level, size, not_persistent,layout,chunksize determine the
5742 * shape of the array.
5743 * This will always create an array with a type-0.90.0 superblock.
5744 * The newer usage is when assembling an array.
5745 * In this case raid_disks will be 0, and the major_version field is
5746 * use to determine which style super-blocks are to be found on the devices.
5747 * The minor and patch _version numbers are also kept incase the
5748 * super_block handler wishes to interpret them.
5750 static int set_array_info(mddev_t
* mddev
, mdu_array_info_t
*info
)
5753 if (info
->raid_disks
== 0) {
5754 /* just setting version number for superblock loading */
5755 if (info
->major_version
< 0 ||
5756 info
->major_version
>= ARRAY_SIZE(super_types
) ||
5757 super_types
[info
->major_version
].name
== NULL
) {
5758 /* maybe try to auto-load a module? */
5760 "md: superblock version %d not known\n",
5761 info
->major_version
);
5764 mddev
->major_version
= info
->major_version
;
5765 mddev
->minor_version
= info
->minor_version
;
5766 mddev
->patch_version
= info
->patch_version
;
5767 mddev
->persistent
= !info
->not_persistent
;
5768 /* ensure mddev_put doesn't delete this now that there
5769 * is some minimal configuration.
5771 mddev
->ctime
= get_seconds();
5774 mddev
->major_version
= MD_MAJOR_VERSION
;
5775 mddev
->minor_version
= MD_MINOR_VERSION
;
5776 mddev
->patch_version
= MD_PATCHLEVEL_VERSION
;
5777 mddev
->ctime
= get_seconds();
5779 mddev
->level
= info
->level
;
5780 mddev
->clevel
[0] = 0;
5781 mddev
->dev_sectors
= 2 * (sector_t
)info
->size
;
5782 mddev
->raid_disks
= info
->raid_disks
;
5783 /* don't set md_minor, it is determined by which /dev/md* was
5786 if (info
->state
& (1<<MD_SB_CLEAN
))
5787 mddev
->recovery_cp
= MaxSector
;
5789 mddev
->recovery_cp
= 0;
5790 mddev
->persistent
= ! info
->not_persistent
;
5791 mddev
->external
= 0;
5793 mddev
->layout
= info
->layout
;
5794 mddev
->chunk_sectors
= info
->chunk_size
>> 9;
5796 mddev
->max_disks
= MD_SB_DISKS
;
5798 if (mddev
->persistent
)
5800 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
5802 mddev
->bitmap_info
.default_offset
= MD_SB_BYTES
>> 9;
5803 mddev
->bitmap_info
.offset
= 0;
5805 mddev
->reshape_position
= MaxSector
;
5808 * Generate a 128 bit UUID
5810 get_random_bytes(mddev
->uuid
, 16);
5812 mddev
->new_level
= mddev
->level
;
5813 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
5814 mddev
->new_layout
= mddev
->layout
;
5815 mddev
->delta_disks
= 0;
5820 void md_set_array_sectors(mddev_t
*mddev
, sector_t array_sectors
)
5822 WARN(!mddev_is_locked(mddev
), "%s: unlocked mddev!\n", __func__
);
5824 if (mddev
->external_size
)
5827 mddev
->array_sectors
= array_sectors
;
5829 EXPORT_SYMBOL(md_set_array_sectors
);
5831 static int update_size(mddev_t
*mddev
, sector_t num_sectors
)
5835 int fit
= (num_sectors
== 0);
5837 if (mddev
->pers
->resize
== NULL
)
5839 /* The "num_sectors" is the number of sectors of each device that
5840 * is used. This can only make sense for arrays with redundancy.
5841 * linear and raid0 always use whatever space is available. We can only
5842 * consider changing this number if no resync or reconstruction is
5843 * happening, and if the new size is acceptable. It must fit before the
5844 * sb_start or, if that is <data_offset, it must fit before the size
5845 * of each device. If num_sectors is zero, we find the largest size
5848 if (mddev
->sync_thread
)
5851 /* Sorry, cannot grow a bitmap yet, just remove it,
5855 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
5856 sector_t avail
= rdev
->sectors
;
5858 if (fit
&& (num_sectors
== 0 || num_sectors
> avail
))
5859 num_sectors
= avail
;
5860 if (avail
< num_sectors
)
5863 rv
= mddev
->pers
->resize(mddev
, num_sectors
);
5865 revalidate_disk(mddev
->gendisk
);
5869 static int update_raid_disks(mddev_t
*mddev
, int raid_disks
)
5872 /* change the number of raid disks */
5873 if (mddev
->pers
->check_reshape
== NULL
)
5875 if (raid_disks
<= 0 ||
5876 (mddev
->max_disks
&& raid_disks
>= mddev
->max_disks
))
5878 if (mddev
->sync_thread
|| mddev
->reshape_position
!= MaxSector
)
5880 mddev
->delta_disks
= raid_disks
- mddev
->raid_disks
;
5882 rv
= mddev
->pers
->check_reshape(mddev
);
5884 mddev
->delta_disks
= 0;
5890 * update_array_info is used to change the configuration of an
5892 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
5893 * fields in the info are checked against the array.
5894 * Any differences that cannot be handled will cause an error.
5895 * Normally, only one change can be managed at a time.
5897 static int update_array_info(mddev_t
*mddev
, mdu_array_info_t
*info
)
5903 /* calculate expected state,ignoring low bits */
5904 if (mddev
->bitmap
&& mddev
->bitmap_info
.offset
)
5905 state
|= (1 << MD_SB_BITMAP_PRESENT
);
5907 if (mddev
->major_version
!= info
->major_version
||
5908 mddev
->minor_version
!= info
->minor_version
||
5909 /* mddev->patch_version != info->patch_version || */
5910 mddev
->ctime
!= info
->ctime
||
5911 mddev
->level
!= info
->level
||
5912 /* mddev->layout != info->layout || */
5913 !mddev
->persistent
!= info
->not_persistent
||
5914 mddev
->chunk_sectors
!= info
->chunk_size
>> 9 ||
5915 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
5916 ((state
^info
->state
) & 0xfffffe00)
5919 /* Check there is only one change */
5920 if (info
->size
>= 0 && mddev
->dev_sectors
/ 2 != info
->size
)
5922 if (mddev
->raid_disks
!= info
->raid_disks
)
5924 if (mddev
->layout
!= info
->layout
)
5926 if ((state
^ info
->state
) & (1<<MD_SB_BITMAP_PRESENT
))
5933 if (mddev
->layout
!= info
->layout
) {
5935 * we don't need to do anything at the md level, the
5936 * personality will take care of it all.
5938 if (mddev
->pers
->check_reshape
== NULL
)
5941 mddev
->new_layout
= info
->layout
;
5942 rv
= mddev
->pers
->check_reshape(mddev
);
5944 mddev
->new_layout
= mddev
->layout
;
5948 if (info
->size
>= 0 && mddev
->dev_sectors
/ 2 != info
->size
)
5949 rv
= update_size(mddev
, (sector_t
)info
->size
* 2);
5951 if (mddev
->raid_disks
!= info
->raid_disks
)
5952 rv
= update_raid_disks(mddev
, info
->raid_disks
);
5954 if ((state
^ info
->state
) & (1<<MD_SB_BITMAP_PRESENT
)) {
5955 if (mddev
->pers
->quiesce
== NULL
)
5957 if (mddev
->recovery
|| mddev
->sync_thread
)
5959 if (info
->state
& (1<<MD_SB_BITMAP_PRESENT
)) {
5960 /* add the bitmap */
5963 if (mddev
->bitmap_info
.default_offset
== 0)
5965 mddev
->bitmap_info
.offset
=
5966 mddev
->bitmap_info
.default_offset
;
5967 mddev
->pers
->quiesce(mddev
, 1);
5968 rv
= bitmap_create(mddev
);
5970 rv
= bitmap_load(mddev
);
5972 bitmap_destroy(mddev
);
5973 mddev
->pers
->quiesce(mddev
, 0);
5975 /* remove the bitmap */
5978 if (mddev
->bitmap
->file
)
5980 mddev
->pers
->quiesce(mddev
, 1);
5981 bitmap_destroy(mddev
);
5982 mddev
->pers
->quiesce(mddev
, 0);
5983 mddev
->bitmap_info
.offset
= 0;
5986 md_update_sb(mddev
, 1);
5990 static int set_disk_faulty(mddev_t
*mddev
, dev_t dev
)
5994 if (mddev
->pers
== NULL
)
5997 rdev
= find_rdev(mddev
, dev
);
6001 md_error(mddev
, rdev
);
6002 if (!test_bit(Faulty
, &rdev
->flags
))
6008 * We have a problem here : there is no easy way to give a CHS
6009 * virtual geometry. We currently pretend that we have a 2 heads
6010 * 4 sectors (with a BIG number of cylinders...). This drives
6011 * dosfs just mad... ;-)
6013 static int md_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
6015 mddev_t
*mddev
= bdev
->bd_disk
->private_data
;
6019 geo
->cylinders
= mddev
->array_sectors
/ 8;
6023 static int md_ioctl(struct block_device
*bdev
, fmode_t mode
,
6024 unsigned int cmd
, unsigned long arg
)
6027 void __user
*argp
= (void __user
*)arg
;
6028 mddev_t
*mddev
= NULL
;
6031 if (!capable(CAP_SYS_ADMIN
))
6035 * Commands dealing with the RAID driver but not any
6041 err
= get_version(argp
);
6044 case PRINT_RAID_DEBUG
:
6052 autostart_arrays(arg
);
6059 * Commands creating/starting a new array:
6062 mddev
= bdev
->bd_disk
->private_data
;
6069 err
= mddev_lock(mddev
);
6072 "md: ioctl lock interrupted, reason %d, cmd %d\n",
6079 case SET_ARRAY_INFO
:
6081 mdu_array_info_t info
;
6083 memset(&info
, 0, sizeof(info
));
6084 else if (copy_from_user(&info
, argp
, sizeof(info
))) {
6089 err
= update_array_info(mddev
, &info
);
6091 printk(KERN_WARNING
"md: couldn't update"
6092 " array info. %d\n", err
);
6097 if (!list_empty(&mddev
->disks
)) {
6099 "md: array %s already has disks!\n",
6104 if (mddev
->raid_disks
) {
6106 "md: array %s already initialised!\n",
6111 err
= set_array_info(mddev
, &info
);
6113 printk(KERN_WARNING
"md: couldn't set"
6114 " array info. %d\n", err
);
6124 * Commands querying/configuring an existing array:
6126 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
6127 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
6128 if ((!mddev
->raid_disks
&& !mddev
->external
)
6129 && cmd
!= ADD_NEW_DISK
&& cmd
!= STOP_ARRAY
6130 && cmd
!= RUN_ARRAY
&& cmd
!= SET_BITMAP_FILE
6131 && cmd
!= GET_BITMAP_FILE
) {
6137 * Commands even a read-only array can execute:
6141 case GET_ARRAY_INFO
:
6142 err
= get_array_info(mddev
, argp
);
6145 case GET_BITMAP_FILE
:
6146 err
= get_bitmap_file(mddev
, argp
);
6150 err
= get_disk_info(mddev
, argp
);
6153 case RESTART_ARRAY_RW
:
6154 err
= restart_array(mddev
);
6158 err
= do_md_stop(mddev
, 0, 1);
6162 err
= md_set_readonly(mddev
, 1);
6166 if (get_user(ro
, (int __user
*)(arg
))) {
6172 /* if the bdev is going readonly the value of mddev->ro
6173 * does not matter, no writes are coming
6178 /* are we are already prepared for writes? */
6182 /* transitioning to readauto need only happen for
6183 * arrays that call md_write_start
6186 err
= restart_array(mddev
);
6189 set_disk_ro(mddev
->gendisk
, 0);
6196 * The remaining ioctls are changing the state of the
6197 * superblock, so we do not allow them on read-only arrays.
6198 * However non-MD ioctls (e.g. get-size) will still come through
6199 * here and hit the 'default' below, so only disallow
6200 * 'md' ioctls, and switch to rw mode if started auto-readonly.
6202 if (_IOC_TYPE(cmd
) == MD_MAJOR
&& mddev
->ro
&& mddev
->pers
) {
6203 if (mddev
->ro
== 2) {
6205 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
6206 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6207 md_wakeup_thread(mddev
->thread
);
6218 mdu_disk_info_t info
;
6219 if (copy_from_user(&info
, argp
, sizeof(info
)))
6222 err
= add_new_disk(mddev
, &info
);
6226 case HOT_REMOVE_DISK
:
6227 err
= hot_remove_disk(mddev
, new_decode_dev(arg
));
6231 err
= hot_add_disk(mddev
, new_decode_dev(arg
));
6234 case SET_DISK_FAULTY
:
6235 err
= set_disk_faulty(mddev
, new_decode_dev(arg
));
6239 err
= do_md_run(mddev
);
6242 case SET_BITMAP_FILE
:
6243 err
= set_bitmap_file(mddev
, (int)arg
);
6253 if (mddev
->hold_active
== UNTIL_IOCTL
&&
6255 mddev
->hold_active
= 0;
6256 mddev_unlock(mddev
);
6265 #ifdef CONFIG_COMPAT
6266 static int md_compat_ioctl(struct block_device
*bdev
, fmode_t mode
,
6267 unsigned int cmd
, unsigned long arg
)
6270 case HOT_REMOVE_DISK
:
6272 case SET_DISK_FAULTY
:
6273 case SET_BITMAP_FILE
:
6274 /* These take in integer arg, do not convert */
6277 arg
= (unsigned long)compat_ptr(arg
);
6281 return md_ioctl(bdev
, mode
, cmd
, arg
);
6283 #endif /* CONFIG_COMPAT */
6285 static int md_open(struct block_device
*bdev
, fmode_t mode
)
6288 * Succeed if we can lock the mddev, which confirms that
6289 * it isn't being stopped right now.
6291 mddev_t
*mddev
= mddev_find(bdev
->bd_dev
);
6294 if (mddev
->gendisk
!= bdev
->bd_disk
) {
6295 /* we are racing with mddev_put which is discarding this
6299 /* Wait until bdev->bd_disk is definitely gone */
6300 flush_workqueue(md_misc_wq
);
6301 /* Then retry the open from the top */
6302 return -ERESTARTSYS
;
6304 BUG_ON(mddev
!= bdev
->bd_disk
->private_data
);
6306 if ((err
= mutex_lock_interruptible(&mddev
->open_mutex
)))
6310 atomic_inc(&mddev
->openers
);
6311 mutex_unlock(&mddev
->open_mutex
);
6313 check_disk_change(bdev
);
6318 static int md_release(struct gendisk
*disk
, fmode_t mode
)
6320 mddev_t
*mddev
= disk
->private_data
;
6323 atomic_dec(&mddev
->openers
);
6329 static int md_media_changed(struct gendisk
*disk
)
6331 mddev_t
*mddev
= disk
->private_data
;
6333 return mddev
->changed
;
6336 static int md_revalidate(struct gendisk
*disk
)
6338 mddev_t
*mddev
= disk
->private_data
;
6343 static const struct block_device_operations md_fops
=
6345 .owner
= THIS_MODULE
,
6347 .release
= md_release
,
6349 #ifdef CONFIG_COMPAT
6350 .compat_ioctl
= md_compat_ioctl
,
6352 .getgeo
= md_getgeo
,
6353 .media_changed
= md_media_changed
,
6354 .revalidate_disk
= md_revalidate
,
6357 static int md_thread(void * arg
)
6359 mdk_thread_t
*thread
= arg
;
6362 * md_thread is a 'system-thread', it's priority should be very
6363 * high. We avoid resource deadlocks individually in each
6364 * raid personality. (RAID5 does preallocation) We also use RR and
6365 * the very same RT priority as kswapd, thus we will never get
6366 * into a priority inversion deadlock.
6368 * we definitely have to have equal or higher priority than
6369 * bdflush, otherwise bdflush will deadlock if there are too
6370 * many dirty RAID5 blocks.
6373 allow_signal(SIGKILL
);
6374 while (!kthread_should_stop()) {
6376 /* We need to wait INTERRUPTIBLE so that
6377 * we don't add to the load-average.
6378 * That means we need to be sure no signals are
6381 if (signal_pending(current
))
6382 flush_signals(current
);
6384 wait_event_interruptible_timeout
6386 test_bit(THREAD_WAKEUP
, &thread
->flags
)
6387 || kthread_should_stop(),
6390 clear_bit(THREAD_WAKEUP
, &thread
->flags
);
6391 if (!kthread_should_stop())
6392 thread
->run(thread
->mddev
);
6398 void md_wakeup_thread(mdk_thread_t
*thread
)
6401 dprintk("md: waking up MD thread %s.\n", thread
->tsk
->comm
);
6402 set_bit(THREAD_WAKEUP
, &thread
->flags
);
6403 wake_up(&thread
->wqueue
);
6407 mdk_thread_t
*md_register_thread(void (*run
) (mddev_t
*), mddev_t
*mddev
,
6410 mdk_thread_t
*thread
;
6412 thread
= kzalloc(sizeof(mdk_thread_t
), GFP_KERNEL
);
6416 init_waitqueue_head(&thread
->wqueue
);
6419 thread
->mddev
= mddev
;
6420 thread
->timeout
= MAX_SCHEDULE_TIMEOUT
;
6421 thread
->tsk
= kthread_run(md_thread
, thread
,
6423 mdname(thread
->mddev
),
6424 name
?: mddev
->pers
->name
);
6425 if (IS_ERR(thread
->tsk
)) {
6432 void md_unregister_thread(mdk_thread_t
*thread
)
6436 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread
->tsk
));
6438 kthread_stop(thread
->tsk
);
6442 void md_error(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
6449 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
))
6452 if (!mddev
->pers
|| !mddev
->pers
->error_handler
)
6454 mddev
->pers
->error_handler(mddev
,rdev
);
6455 if (mddev
->degraded
)
6456 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
6457 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
6458 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6459 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6460 md_wakeup_thread(mddev
->thread
);
6461 if (mddev
->event_work
.func
)
6462 queue_work(md_misc_wq
, &mddev
->event_work
);
6463 md_new_event_inintr(mddev
);
6466 /* seq_file implementation /proc/mdstat */
6468 static void status_unused(struct seq_file
*seq
)
6473 seq_printf(seq
, "unused devices: ");
6475 list_for_each_entry(rdev
, &pending_raid_disks
, same_set
) {
6476 char b
[BDEVNAME_SIZE
];
6478 seq_printf(seq
, "%s ",
6479 bdevname(rdev
->bdev
,b
));
6482 seq_printf(seq
, "<none>");
6484 seq_printf(seq
, "\n");
6488 static void status_resync(struct seq_file
*seq
, mddev_t
* mddev
)
6490 sector_t max_sectors
, resync
, res
;
6491 unsigned long dt
, db
;
6494 unsigned int per_milli
;
6496 resync
= mddev
->curr_resync
- atomic_read(&mddev
->recovery_active
);
6498 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
6499 max_sectors
= mddev
->resync_max_sectors
;
6501 max_sectors
= mddev
->dev_sectors
;
6504 * Should not happen.
6510 /* Pick 'scale' such that (resync>>scale)*1000 will fit
6511 * in a sector_t, and (max_sectors>>scale) will fit in a
6512 * u32, as those are the requirements for sector_div.
6513 * Thus 'scale' must be at least 10
6516 if (sizeof(sector_t
) > sizeof(unsigned long)) {
6517 while ( max_sectors
/2 > (1ULL<<(scale
+32)))
6520 res
= (resync
>>scale
)*1000;
6521 sector_div(res
, (u32
)((max_sectors
>>scale
)+1));
6525 int i
, x
= per_milli
/50, y
= 20-x
;
6526 seq_printf(seq
, "[");
6527 for (i
= 0; i
< x
; i
++)
6528 seq_printf(seq
, "=");
6529 seq_printf(seq
, ">");
6530 for (i
= 0; i
< y
; i
++)
6531 seq_printf(seq
, ".");
6532 seq_printf(seq
, "] ");
6534 seq_printf(seq
, " %s =%3u.%u%% (%llu/%llu)",
6535 (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
)?
6537 (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
)?
6539 (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ?
6540 "resync" : "recovery"))),
6541 per_milli
/10, per_milli
% 10,
6542 (unsigned long long) resync
/2,
6543 (unsigned long long) max_sectors
/2);
6546 * dt: time from mark until now
6547 * db: blocks written from mark until now
6548 * rt: remaining time
6550 * rt is a sector_t, so could be 32bit or 64bit.
6551 * So we divide before multiply in case it is 32bit and close
6553 * We scale the divisor (db) by 32 to avoid losing precision
6554 * near the end of resync when the number of remaining sectors
6556 * We then divide rt by 32 after multiplying by db to compensate.
6557 * The '+1' avoids division by zero if db is very small.
6559 dt
= ((jiffies
- mddev
->resync_mark
) / HZ
);
6561 db
= (mddev
->curr_mark_cnt
- atomic_read(&mddev
->recovery_active
))
6562 - mddev
->resync_mark_cnt
;
6564 rt
= max_sectors
- resync
; /* number of remaining sectors */
6565 sector_div(rt
, db
/32+1);
6569 seq_printf(seq
, " finish=%lu.%lumin", (unsigned long)rt
/ 60,
6570 ((unsigned long)rt
% 60)/6);
6572 seq_printf(seq
, " speed=%ldK/sec", db
/2/dt
);
6575 static void *md_seq_start(struct seq_file
*seq
, loff_t
*pos
)
6577 struct list_head
*tmp
;
6587 spin_lock(&all_mddevs_lock
);
6588 list_for_each(tmp
,&all_mddevs
)
6590 mddev
= list_entry(tmp
, mddev_t
, all_mddevs
);
6592 spin_unlock(&all_mddevs_lock
);
6595 spin_unlock(&all_mddevs_lock
);
6597 return (void*)2;/* tail */
6601 static void *md_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
6603 struct list_head
*tmp
;
6604 mddev_t
*next_mddev
, *mddev
= v
;
6610 spin_lock(&all_mddevs_lock
);
6612 tmp
= all_mddevs
.next
;
6614 tmp
= mddev
->all_mddevs
.next
;
6615 if (tmp
!= &all_mddevs
)
6616 next_mddev
= mddev_get(list_entry(tmp
,mddev_t
,all_mddevs
));
6618 next_mddev
= (void*)2;
6621 spin_unlock(&all_mddevs_lock
);
6629 static void md_seq_stop(struct seq_file
*seq
, void *v
)
6633 if (mddev
&& v
!= (void*)1 && v
!= (void*)2)
6637 static int md_seq_show(struct seq_file
*seq
, void *v
)
6642 struct bitmap
*bitmap
;
6644 if (v
== (void*)1) {
6645 struct mdk_personality
*pers
;
6646 seq_printf(seq
, "Personalities : ");
6647 spin_lock(&pers_lock
);
6648 list_for_each_entry(pers
, &pers_list
, list
)
6649 seq_printf(seq
, "[%s] ", pers
->name
);
6651 spin_unlock(&pers_lock
);
6652 seq_printf(seq
, "\n");
6653 seq
->poll_event
= atomic_read(&md_event_count
);
6656 if (v
== (void*)2) {
6661 if (mddev_lock(mddev
) < 0)
6664 if (mddev
->pers
|| mddev
->raid_disks
|| !list_empty(&mddev
->disks
)) {
6665 seq_printf(seq
, "%s : %sactive", mdname(mddev
),
6666 mddev
->pers
? "" : "in");
6669 seq_printf(seq
, " (read-only)");
6671 seq_printf(seq
, " (auto-read-only)");
6672 seq_printf(seq
, " %s", mddev
->pers
->name
);
6676 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
6677 char b
[BDEVNAME_SIZE
];
6678 seq_printf(seq
, " %s[%d]",
6679 bdevname(rdev
->bdev
,b
), rdev
->desc_nr
);
6680 if (test_bit(WriteMostly
, &rdev
->flags
))
6681 seq_printf(seq
, "(W)");
6682 if (test_bit(Faulty
, &rdev
->flags
)) {
6683 seq_printf(seq
, "(F)");
6685 } else if (rdev
->raid_disk
< 0)
6686 seq_printf(seq
, "(S)"); /* spare */
6687 sectors
+= rdev
->sectors
;
6690 if (!list_empty(&mddev
->disks
)) {
6692 seq_printf(seq
, "\n %llu blocks",
6693 (unsigned long long)
6694 mddev
->array_sectors
/ 2);
6696 seq_printf(seq
, "\n %llu blocks",
6697 (unsigned long long)sectors
/ 2);
6699 if (mddev
->persistent
) {
6700 if (mddev
->major_version
!= 0 ||
6701 mddev
->minor_version
!= 90) {
6702 seq_printf(seq
," super %d.%d",
6703 mddev
->major_version
,
6704 mddev
->minor_version
);
6706 } else if (mddev
->external
)
6707 seq_printf(seq
, " super external:%s",
6708 mddev
->metadata_type
);
6710 seq_printf(seq
, " super non-persistent");
6713 mddev
->pers
->status(seq
, mddev
);
6714 seq_printf(seq
, "\n ");
6715 if (mddev
->pers
->sync_request
) {
6716 if (mddev
->curr_resync
> 2) {
6717 status_resync(seq
, mddev
);
6718 seq_printf(seq
, "\n ");
6719 } else if (mddev
->curr_resync
== 1 || mddev
->curr_resync
== 2)
6720 seq_printf(seq
, "\tresync=DELAYED\n ");
6721 else if (mddev
->recovery_cp
< MaxSector
)
6722 seq_printf(seq
, "\tresync=PENDING\n ");
6725 seq_printf(seq
, "\n ");
6727 if ((bitmap
= mddev
->bitmap
)) {
6728 unsigned long chunk_kb
;
6729 unsigned long flags
;
6730 spin_lock_irqsave(&bitmap
->lock
, flags
);
6731 chunk_kb
= mddev
->bitmap_info
.chunksize
>> 10;
6732 seq_printf(seq
, "bitmap: %lu/%lu pages [%luKB], "
6734 bitmap
->pages
- bitmap
->missing_pages
,
6736 (bitmap
->pages
- bitmap
->missing_pages
)
6737 << (PAGE_SHIFT
- 10),
6738 chunk_kb
? chunk_kb
: mddev
->bitmap_info
.chunksize
,
6739 chunk_kb
? "KB" : "B");
6741 seq_printf(seq
, ", file: ");
6742 seq_path(seq
, &bitmap
->file
->f_path
, " \t\n");
6745 seq_printf(seq
, "\n");
6746 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
6749 seq_printf(seq
, "\n");
6751 mddev_unlock(mddev
);
6756 static const struct seq_operations md_seq_ops
= {
6757 .start
= md_seq_start
,
6758 .next
= md_seq_next
,
6759 .stop
= md_seq_stop
,
6760 .show
= md_seq_show
,
6763 static int md_seq_open(struct inode
*inode
, struct file
*file
)
6765 struct seq_file
*seq
;
6768 error
= seq_open(file
, &md_seq_ops
);
6772 seq
= file
->private_data
;
6773 seq
->poll_event
= atomic_read(&md_event_count
);
6777 static unsigned int mdstat_poll(struct file
*filp
, poll_table
*wait
)
6779 struct seq_file
*seq
= filp
->private_data
;
6782 poll_wait(filp
, &md_event_waiters
, wait
);
6784 /* always allow read */
6785 mask
= POLLIN
| POLLRDNORM
;
6787 if (seq
->poll_event
!= atomic_read(&md_event_count
))
6788 mask
|= POLLERR
| POLLPRI
;
6792 static const struct file_operations md_seq_fops
= {
6793 .owner
= THIS_MODULE
,
6794 .open
= md_seq_open
,
6796 .llseek
= seq_lseek
,
6797 .release
= seq_release_private
,
6798 .poll
= mdstat_poll
,
6801 int register_md_personality(struct mdk_personality
*p
)
6803 spin_lock(&pers_lock
);
6804 list_add_tail(&p
->list
, &pers_list
);
6805 printk(KERN_INFO
"md: %s personality registered for level %d\n", p
->name
, p
->level
);
6806 spin_unlock(&pers_lock
);
6810 int unregister_md_personality(struct mdk_personality
*p
)
6812 printk(KERN_INFO
"md: %s personality unregistered\n", p
->name
);
6813 spin_lock(&pers_lock
);
6814 list_del_init(&p
->list
);
6815 spin_unlock(&pers_lock
);
6819 static int is_mddev_idle(mddev_t
*mddev
, int init
)
6827 rdev_for_each_rcu(rdev
, mddev
) {
6828 struct gendisk
*disk
= rdev
->bdev
->bd_contains
->bd_disk
;
6829 curr_events
= (int)part_stat_read(&disk
->part0
, sectors
[0]) +
6830 (int)part_stat_read(&disk
->part0
, sectors
[1]) -
6831 atomic_read(&disk
->sync_io
);
6832 /* sync IO will cause sync_io to increase before the disk_stats
6833 * as sync_io is counted when a request starts, and
6834 * disk_stats is counted when it completes.
6835 * So resync activity will cause curr_events to be smaller than
6836 * when there was no such activity.
6837 * non-sync IO will cause disk_stat to increase without
6838 * increasing sync_io so curr_events will (eventually)
6839 * be larger than it was before. Once it becomes
6840 * substantially larger, the test below will cause
6841 * the array to appear non-idle, and resync will slow
6843 * If there is a lot of outstanding resync activity when
6844 * we set last_event to curr_events, then all that activity
6845 * completing might cause the array to appear non-idle
6846 * and resync will be slowed down even though there might
6847 * not have been non-resync activity. This will only
6848 * happen once though. 'last_events' will soon reflect
6849 * the state where there is little or no outstanding
6850 * resync requests, and further resync activity will
6851 * always make curr_events less than last_events.
6854 if (init
|| curr_events
- rdev
->last_events
> 64) {
6855 rdev
->last_events
= curr_events
;
6863 void md_done_sync(mddev_t
*mddev
, int blocks
, int ok
)
6865 /* another "blocks" (512byte) blocks have been synced */
6866 atomic_sub(blocks
, &mddev
->recovery_active
);
6867 wake_up(&mddev
->recovery_wait
);
6869 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6870 md_wakeup_thread(mddev
->thread
);
6871 // stop recovery, signal do_sync ....
6876 /* md_write_start(mddev, bi)
6877 * If we need to update some array metadata (e.g. 'active' flag
6878 * in superblock) before writing, schedule a superblock update
6879 * and wait for it to complete.
6881 void md_write_start(mddev_t
*mddev
, struct bio
*bi
)
6884 if (bio_data_dir(bi
) != WRITE
)
6887 BUG_ON(mddev
->ro
== 1);
6888 if (mddev
->ro
== 2) {
6889 /* need to switch to read/write */
6891 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6892 md_wakeup_thread(mddev
->thread
);
6893 md_wakeup_thread(mddev
->sync_thread
);
6896 atomic_inc(&mddev
->writes_pending
);
6897 if (mddev
->safemode
== 1)
6898 mddev
->safemode
= 0;
6899 if (mddev
->in_sync
) {
6900 spin_lock_irq(&mddev
->write_lock
);
6901 if (mddev
->in_sync
) {
6903 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
6904 set_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
6905 md_wakeup_thread(mddev
->thread
);
6908 spin_unlock_irq(&mddev
->write_lock
);
6911 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
6912 wait_event(mddev
->sb_wait
,
6913 !test_bit(MD_CHANGE_PENDING
, &mddev
->flags
));
6916 void md_write_end(mddev_t
*mddev
)
6918 if (atomic_dec_and_test(&mddev
->writes_pending
)) {
6919 if (mddev
->safemode
== 2)
6920 md_wakeup_thread(mddev
->thread
);
6921 else if (mddev
->safemode_delay
)
6922 mod_timer(&mddev
->safemode_timer
, jiffies
+ mddev
->safemode_delay
);
6926 /* md_allow_write(mddev)
6927 * Calling this ensures that the array is marked 'active' so that writes
6928 * may proceed without blocking. It is important to call this before
6929 * attempting a GFP_KERNEL allocation while holding the mddev lock.
6930 * Must be called with mddev_lock held.
6932 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
6933 * is dropped, so return -EAGAIN after notifying userspace.
6935 int md_allow_write(mddev_t
*mddev
)
6941 if (!mddev
->pers
->sync_request
)
6944 spin_lock_irq(&mddev
->write_lock
);
6945 if (mddev
->in_sync
) {
6947 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
6948 set_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
6949 if (mddev
->safemode_delay
&&
6950 mddev
->safemode
== 0)
6951 mddev
->safemode
= 1;
6952 spin_unlock_irq(&mddev
->write_lock
);
6953 md_update_sb(mddev
, 0);
6954 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
6956 spin_unlock_irq(&mddev
->write_lock
);
6958 if (test_bit(MD_CHANGE_PENDING
, &mddev
->flags
))
6963 EXPORT_SYMBOL_GPL(md_allow_write
);
6965 #define SYNC_MARKS 10
6966 #define SYNC_MARK_STEP (3*HZ)
6967 void md_do_sync(mddev_t
*mddev
)
6970 unsigned int currspeed
= 0,
6972 sector_t max_sectors
,j
, io_sectors
;
6973 unsigned long mark
[SYNC_MARKS
];
6974 sector_t mark_cnt
[SYNC_MARKS
];
6976 struct list_head
*tmp
;
6977 sector_t last_check
;
6982 /* just incase thread restarts... */
6983 if (test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
))
6985 if (mddev
->ro
) /* never try to sync a read-only array */
6988 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
6989 if (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
))
6990 desc
= "data-check";
6991 else if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
6992 desc
= "requested-resync";
6995 } else if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
7000 /* we overload curr_resync somewhat here.
7001 * 0 == not engaged in resync at all
7002 * 2 == checking that there is no conflict with another sync
7003 * 1 == like 2, but have yielded to allow conflicting resync to
7005 * other == active in resync - this many blocks
7007 * Before starting a resync we must have set curr_resync to
7008 * 2, and then checked that every "conflicting" array has curr_resync
7009 * less than ours. When we find one that is the same or higher
7010 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
7011 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
7012 * This will mean we have to start checking from the beginning again.
7017 mddev
->curr_resync
= 2;
7020 if (kthread_should_stop())
7021 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
7023 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
7025 for_each_mddev(mddev2
, tmp
) {
7026 if (mddev2
== mddev
)
7028 if (!mddev
->parallel_resync
7029 && mddev2
->curr_resync
7030 && match_mddev_units(mddev
, mddev2
)) {
7032 if (mddev
< mddev2
&& mddev
->curr_resync
== 2) {
7033 /* arbitrarily yield */
7034 mddev
->curr_resync
= 1;
7035 wake_up(&resync_wait
);
7037 if (mddev
> mddev2
&& mddev
->curr_resync
== 1)
7038 /* no need to wait here, we can wait the next
7039 * time 'round when curr_resync == 2
7042 /* We need to wait 'interruptible' so as not to
7043 * contribute to the load average, and not to
7044 * be caught by 'softlockup'
7046 prepare_to_wait(&resync_wait
, &wq
, TASK_INTERRUPTIBLE
);
7047 if (!kthread_should_stop() &&
7048 mddev2
->curr_resync
>= mddev
->curr_resync
) {
7049 printk(KERN_INFO
"md: delaying %s of %s"
7050 " until %s has finished (they"
7051 " share one or more physical units)\n",
7052 desc
, mdname(mddev
), mdname(mddev2
));
7054 if (signal_pending(current
))
7055 flush_signals(current
);
7057 finish_wait(&resync_wait
, &wq
);
7060 finish_wait(&resync_wait
, &wq
);
7063 } while (mddev
->curr_resync
< 2);
7066 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
7067 /* resync follows the size requested by the personality,
7068 * which defaults to physical size, but can be virtual size
7070 max_sectors
= mddev
->resync_max_sectors
;
7071 mddev
->resync_mismatches
= 0;
7072 /* we don't use the checkpoint if there's a bitmap */
7073 if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
7074 j
= mddev
->resync_min
;
7075 else if (!mddev
->bitmap
)
7076 j
= mddev
->recovery_cp
;
7078 } else if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
7079 max_sectors
= mddev
->dev_sectors
;
7081 /* recovery follows the physical size of devices */
7082 max_sectors
= mddev
->dev_sectors
;
7085 list_for_each_entry_rcu(rdev
, &mddev
->disks
, same_set
)
7086 if (rdev
->raid_disk
>= 0 &&
7087 !test_bit(Faulty
, &rdev
->flags
) &&
7088 !test_bit(In_sync
, &rdev
->flags
) &&
7089 rdev
->recovery_offset
< j
)
7090 j
= rdev
->recovery_offset
;
7094 printk(KERN_INFO
"md: %s of RAID array %s\n", desc
, mdname(mddev
));
7095 printk(KERN_INFO
"md: minimum _guaranteed_ speed:"
7096 " %d KB/sec/disk.\n", speed_min(mddev
));
7097 printk(KERN_INFO
"md: using maximum available idle IO bandwidth "
7098 "(but not more than %d KB/sec) for %s.\n",
7099 speed_max(mddev
), desc
);
7101 is_mddev_idle(mddev
, 1); /* this initializes IO event counters */
7104 for (m
= 0; m
< SYNC_MARKS
; m
++) {
7106 mark_cnt
[m
] = io_sectors
;
7109 mddev
->resync_mark
= mark
[last_mark
];
7110 mddev
->resync_mark_cnt
= mark_cnt
[last_mark
];
7113 * Tune reconstruction:
7115 window
= 32*(PAGE_SIZE
/512);
7116 printk(KERN_INFO
"md: using %dk window, over a total of %lluk.\n",
7117 window
/2, (unsigned long long)max_sectors
/2);
7119 atomic_set(&mddev
->recovery_active
, 0);
7124 "md: resuming %s of %s from checkpoint.\n",
7125 desc
, mdname(mddev
));
7126 mddev
->curr_resync
= j
;
7128 mddev
->curr_resync_completed
= j
;
7130 while (j
< max_sectors
) {
7135 if (!test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
7136 ((mddev
->curr_resync
> mddev
->curr_resync_completed
&&
7137 (mddev
->curr_resync
- mddev
->curr_resync_completed
)
7138 > (max_sectors
>> 4)) ||
7139 (j
- mddev
->curr_resync_completed
)*2
7140 >= mddev
->resync_max
- mddev
->curr_resync_completed
7142 /* time to update curr_resync_completed */
7143 wait_event(mddev
->recovery_wait
,
7144 atomic_read(&mddev
->recovery_active
) == 0);
7145 mddev
->curr_resync_completed
= j
;
7146 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
7147 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
7150 while (j
>= mddev
->resync_max
&& !kthread_should_stop()) {
7151 /* As this condition is controlled by user-space,
7152 * we can block indefinitely, so use '_interruptible'
7153 * to avoid triggering warnings.
7155 flush_signals(current
); /* just in case */
7156 wait_event_interruptible(mddev
->recovery_wait
,
7157 mddev
->resync_max
> j
7158 || kthread_should_stop());
7161 if (kthread_should_stop())
7164 sectors
= mddev
->pers
->sync_request(mddev
, j
, &skipped
,
7165 currspeed
< speed_min(mddev
));
7167 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
7171 if (!skipped
) { /* actual IO requested */
7172 io_sectors
+= sectors
;
7173 atomic_add(sectors
, &mddev
->recovery_active
);
7176 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
7180 if (j
>1) mddev
->curr_resync
= j
;
7181 mddev
->curr_mark_cnt
= io_sectors
;
7182 if (last_check
== 0)
7183 /* this is the earliest that rebuild will be
7184 * visible in /proc/mdstat
7186 md_new_event(mddev
);
7188 if (last_check
+ window
> io_sectors
|| j
== max_sectors
)
7191 last_check
= io_sectors
;
7193 if (time_after_eq(jiffies
, mark
[last_mark
] + SYNC_MARK_STEP
)) {
7195 int next
= (last_mark
+1) % SYNC_MARKS
;
7197 mddev
->resync_mark
= mark
[next
];
7198 mddev
->resync_mark_cnt
= mark_cnt
[next
];
7199 mark
[next
] = jiffies
;
7200 mark_cnt
[next
] = io_sectors
- atomic_read(&mddev
->recovery_active
);
7205 if (kthread_should_stop())
7210 * this loop exits only if either when we are slower than
7211 * the 'hard' speed limit, or the system was IO-idle for
7213 * the system might be non-idle CPU-wise, but we only care
7214 * about not overloading the IO subsystem. (things like an
7215 * e2fsck being done on the RAID array should execute fast)
7219 currspeed
= ((unsigned long)(io_sectors
-mddev
->resync_mark_cnt
))/2
7220 /((jiffies
-mddev
->resync_mark
)/HZ
+1) +1;
7222 if (currspeed
> speed_min(mddev
)) {
7223 if ((currspeed
> speed_max(mddev
)) ||
7224 !is_mddev_idle(mddev
, 0)) {
7230 printk(KERN_INFO
"md: %s: %s done.\n",mdname(mddev
), desc
);
7232 * this also signals 'finished resyncing' to md_stop
7235 wait_event(mddev
->recovery_wait
, !atomic_read(&mddev
->recovery_active
));
7237 /* tell personality that we are finished */
7238 mddev
->pers
->sync_request(mddev
, max_sectors
, &skipped
, 1);
7240 if (!test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
) &&
7241 mddev
->curr_resync
> 2) {
7242 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
7243 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
7244 if (mddev
->curr_resync
>= mddev
->recovery_cp
) {
7246 "md: checkpointing %s of %s.\n",
7247 desc
, mdname(mddev
));
7248 mddev
->recovery_cp
= mddev
->curr_resync
;
7251 mddev
->recovery_cp
= MaxSector
;
7253 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
7254 mddev
->curr_resync
= MaxSector
;
7256 list_for_each_entry_rcu(rdev
, &mddev
->disks
, same_set
)
7257 if (rdev
->raid_disk
>= 0 &&
7258 mddev
->delta_disks
>= 0 &&
7259 !test_bit(Faulty
, &rdev
->flags
) &&
7260 !test_bit(In_sync
, &rdev
->flags
) &&
7261 rdev
->recovery_offset
< mddev
->curr_resync
)
7262 rdev
->recovery_offset
= mddev
->curr_resync
;
7266 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
7269 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
7270 /* We completed so min/max setting can be forgotten if used. */
7271 if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
7272 mddev
->resync_min
= 0;
7273 mddev
->resync_max
= MaxSector
;
7274 } else if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
7275 mddev
->resync_min
= mddev
->curr_resync_completed
;
7276 mddev
->curr_resync
= 0;
7277 wake_up(&resync_wait
);
7278 set_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
7279 md_wakeup_thread(mddev
->thread
);
7284 * got a signal, exit.
7287 "md: md_do_sync() got signal ... exiting\n");
7288 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
7292 EXPORT_SYMBOL_GPL(md_do_sync
);
7294 static int remove_and_add_spares(mddev_t
*mddev
)
7299 mddev
->curr_resync_completed
= 0;
7301 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
7302 if (rdev
->raid_disk
>= 0 &&
7303 !test_bit(Blocked
, &rdev
->flags
) &&
7304 (test_bit(Faulty
, &rdev
->flags
) ||
7305 ! test_bit(In_sync
, &rdev
->flags
)) &&
7306 atomic_read(&rdev
->nr_pending
)==0) {
7307 if (mddev
->pers
->hot_remove_disk(
7308 mddev
, rdev
->raid_disk
)==0) {
7309 sysfs_unlink_rdev(mddev
, rdev
);
7310 rdev
->raid_disk
= -1;
7314 if (mddev
->degraded
) {
7315 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
7316 if (rdev
->raid_disk
>= 0 &&
7317 !test_bit(In_sync
, &rdev
->flags
) &&
7318 !test_bit(Faulty
, &rdev
->flags
))
7320 if (rdev
->raid_disk
< 0
7321 && !test_bit(Faulty
, &rdev
->flags
)) {
7322 rdev
->recovery_offset
= 0;
7324 hot_add_disk(mddev
, rdev
) == 0) {
7325 if (sysfs_link_rdev(mddev
, rdev
))
7326 /* failure here is OK */;
7328 md_new_event(mddev
);
7329 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
7338 static void reap_sync_thread(mddev_t
*mddev
)
7342 /* resync has finished, collect result */
7343 md_unregister_thread(mddev
->sync_thread
);
7344 mddev
->sync_thread
= NULL
;
7345 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
) &&
7346 !test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
)) {
7348 /* activate any spares */
7349 if (mddev
->pers
->spare_active(mddev
))
7350 sysfs_notify(&mddev
->kobj
, NULL
,
7353 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
7354 mddev
->pers
->finish_reshape
)
7355 mddev
->pers
->finish_reshape(mddev
);
7356 md_update_sb(mddev
, 1);
7358 /* if array is no-longer degraded, then any saved_raid_disk
7359 * information must be scrapped
7361 if (!mddev
->degraded
)
7362 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
7363 rdev
->saved_raid_disk
= -1;
7365 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
7366 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
7367 clear_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
7368 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
7369 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
7370 /* flag recovery needed just to double check */
7371 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7372 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
7373 md_new_event(mddev
);
7374 if (mddev
->event_work
.func
)
7375 queue_work(md_misc_wq
, &mddev
->event_work
);
7379 * This routine is regularly called by all per-raid-array threads to
7380 * deal with generic issues like resync and super-block update.
7381 * Raid personalities that don't have a thread (linear/raid0) do not
7382 * need this as they never do any recovery or update the superblock.
7384 * It does not do any resync itself, but rather "forks" off other threads
7385 * to do that as needed.
7386 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
7387 * "->recovery" and create a thread at ->sync_thread.
7388 * When the thread finishes it sets MD_RECOVERY_DONE
7389 * and wakeups up this thread which will reap the thread and finish up.
7390 * This thread also removes any faulty devices (with nr_pending == 0).
7392 * The overall approach is:
7393 * 1/ if the superblock needs updating, update it.
7394 * 2/ If a recovery thread is running, don't do anything else.
7395 * 3/ If recovery has finished, clean up, possibly marking spares active.
7396 * 4/ If there are any faulty devices, remove them.
7397 * 5/ If array is degraded, try to add spares devices
7398 * 6/ If array has spares or is not in-sync, start a resync thread.
7400 void md_check_recovery(mddev_t
*mddev
)
7402 if (mddev
->suspended
)
7406 bitmap_daemon_work(mddev
);
7408 if (signal_pending(current
)) {
7409 if (mddev
->pers
->sync_request
&& !mddev
->external
) {
7410 printk(KERN_INFO
"md: %s in immediate safe mode\n",
7412 mddev
->safemode
= 2;
7414 flush_signals(current
);
7417 if (mddev
->ro
&& !test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))
7420 (mddev
->flags
& ~ (1<<MD_CHANGE_PENDING
)) ||
7421 test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
) ||
7422 test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
) ||
7423 (mddev
->external
== 0 && mddev
->safemode
== 1) ||
7424 (mddev
->safemode
== 2 && ! atomic_read(&mddev
->writes_pending
)
7425 && !mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
)
7429 if (mddev_trylock(mddev
)) {
7433 /* Only thing we do on a ro array is remove
7437 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
7438 if (rdev
->raid_disk
>= 0 &&
7439 !test_bit(Blocked
, &rdev
->flags
) &&
7440 test_bit(Faulty
, &rdev
->flags
) &&
7441 atomic_read(&rdev
->nr_pending
)==0) {
7442 if (mddev
->pers
->hot_remove_disk(
7443 mddev
, rdev
->raid_disk
)==0) {
7444 sysfs_unlink_rdev(mddev
, rdev
);
7445 rdev
->raid_disk
= -1;
7448 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7452 if (!mddev
->external
) {
7454 spin_lock_irq(&mddev
->write_lock
);
7455 if (mddev
->safemode
&&
7456 !atomic_read(&mddev
->writes_pending
) &&
7458 mddev
->recovery_cp
== MaxSector
) {
7461 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
7463 if (mddev
->safemode
== 1)
7464 mddev
->safemode
= 0;
7465 spin_unlock_irq(&mddev
->write_lock
);
7467 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
7471 md_update_sb(mddev
, 0);
7473 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) &&
7474 !test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
)) {
7475 /* resync/recovery still happening */
7476 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7479 if (mddev
->sync_thread
) {
7480 reap_sync_thread(mddev
);
7483 /* Set RUNNING before clearing NEEDED to avoid
7484 * any transients in the value of "sync_action".
7486 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
7487 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7488 /* Clear some bits that don't mean anything, but
7491 clear_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
7492 clear_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
7494 if (test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
))
7496 /* no recovery is running.
7497 * remove any failed drives, then
7498 * add spares if possible.
7499 * Spare are also removed and re-added, to allow
7500 * the personality to fail the re-add.
7503 if (mddev
->reshape_position
!= MaxSector
) {
7504 if (mddev
->pers
->check_reshape
== NULL
||
7505 mddev
->pers
->check_reshape(mddev
) != 0)
7506 /* Cannot proceed */
7508 set_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
7509 clear_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
7510 } else if ((spares
= remove_and_add_spares(mddev
))) {
7511 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
7512 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
7513 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
7514 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
7515 } else if (mddev
->recovery_cp
< MaxSector
) {
7516 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
7517 clear_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
7518 } else if (!test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
7519 /* nothing to be done ... */
7522 if (mddev
->pers
->sync_request
) {
7523 if (spares
&& mddev
->bitmap
&& ! mddev
->bitmap
->file
) {
7524 /* We are adding a device or devices to an array
7525 * which has the bitmap stored on all devices.
7526 * So make sure all bitmap pages get written
7528 bitmap_write_all(mddev
->bitmap
);
7530 mddev
->sync_thread
= md_register_thread(md_do_sync
,
7533 if (!mddev
->sync_thread
) {
7534 printk(KERN_ERR
"%s: could not start resync"
7537 /* leave the spares where they are, it shouldn't hurt */
7538 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
7539 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
7540 clear_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
7541 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
7542 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
7544 md_wakeup_thread(mddev
->sync_thread
);
7545 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
7546 md_new_event(mddev
);
7549 if (!mddev
->sync_thread
) {
7550 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
7551 if (test_and_clear_bit(MD_RECOVERY_RECOVER
,
7553 if (mddev
->sysfs_action
)
7554 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
7556 mddev_unlock(mddev
);
7560 void md_wait_for_blocked_rdev(mdk_rdev_t
*rdev
, mddev_t
*mddev
)
7562 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
7563 wait_event_timeout(rdev
->blocked_wait
,
7564 !test_bit(Blocked
, &rdev
->flags
) &&
7565 !test_bit(BlockedBadBlocks
, &rdev
->flags
),
7566 msecs_to_jiffies(5000));
7567 rdev_dec_pending(rdev
, mddev
);
7569 EXPORT_SYMBOL(md_wait_for_blocked_rdev
);
7572 /* Bad block management.
7573 * We can record which blocks on each device are 'bad' and so just
7574 * fail those blocks, or that stripe, rather than the whole device.
7575 * Entries in the bad-block table are 64bits wide. This comprises:
7576 * Length of bad-range, in sectors: 0-511 for lengths 1-512
7577 * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes)
7578 * A 'shift' can be set so that larger blocks are tracked and
7579 * consequently larger devices can be covered.
7580 * 'Acknowledged' flag - 1 bit. - the most significant bit.
7582 * Locking of the bad-block table uses a seqlock so md_is_badblock
7583 * might need to retry if it is very unlucky.
7584 * We will sometimes want to check for bad blocks in a bi_end_io function,
7585 * so we use the write_seqlock_irq variant.
7587 * When looking for a bad block we specify a range and want to
7588 * know if any block in the range is bad. So we binary-search
7589 * to the last range that starts at-or-before the given endpoint,
7590 * (or "before the sector after the target range")
7591 * then see if it ends after the given start.
7593 * 0 if there are no known bad blocks in the range
7594 * 1 if there are known bad block which are all acknowledged
7595 * -1 if there are bad blocks which have not yet been acknowledged in metadata.
7596 * plus the start/length of the first bad section we overlap.
7598 int md_is_badblock(struct badblocks
*bb
, sector_t s
, int sectors
,
7599 sector_t
*first_bad
, int *bad_sectors
)
7605 sector_t target
= s
+ sectors
;
7608 if (bb
->shift
> 0) {
7609 /* round the start down, and the end up */
7611 target
+= (1<<bb
->shift
) - 1;
7612 target
>>= bb
->shift
;
7613 sectors
= target
- s
;
7615 /* 'target' is now the first block after the bad range */
7618 seq
= read_seqbegin(&bb
->lock
);
7622 /* Binary search between lo and hi for 'target'
7623 * i.e. for the last range that starts before 'target'
7625 /* INVARIANT: ranges before 'lo' and at-or-after 'hi'
7626 * are known not to be the last range before target.
7627 * VARIANT: hi-lo is the number of possible
7628 * ranges, and decreases until it reaches 1
7630 while (hi
- lo
> 1) {
7631 int mid
= (lo
+ hi
) / 2;
7632 sector_t a
= BB_OFFSET(p
[mid
]);
7634 /* This could still be the one, earlier ranges
7638 /* This and later ranges are definitely out. */
7641 /* 'lo' might be the last that started before target, but 'hi' isn't */
7643 /* need to check all range that end after 's' to see if
7644 * any are unacknowledged.
7647 BB_OFFSET(p
[lo
]) + BB_LEN(p
[lo
]) > s
) {
7648 if (BB_OFFSET(p
[lo
]) < target
) {
7649 /* starts before the end, and finishes after
7650 * the start, so they must overlap
7652 if (rv
!= -1 && BB_ACK(p
[lo
]))
7656 *first_bad
= BB_OFFSET(p
[lo
]);
7657 *bad_sectors
= BB_LEN(p
[lo
]);
7663 if (read_seqretry(&bb
->lock
, seq
))
7668 EXPORT_SYMBOL_GPL(md_is_badblock
);
7671 * Add a range of bad blocks to the table.
7672 * This might extend the table, or might contract it
7673 * if two adjacent ranges can be merged.
7674 * We binary-search to find the 'insertion' point, then
7675 * decide how best to handle it.
7677 static int md_set_badblocks(struct badblocks
*bb
, sector_t s
, int sectors
,
7685 /* badblocks are disabled */
7689 /* round the start down, and the end up */
7690 sector_t next
= s
+ sectors
;
7692 next
+= (1<<bb
->shift
) - 1;
7697 write_seqlock_irq(&bb
->lock
);
7702 /* Find the last range that starts at-or-before 's' */
7703 while (hi
- lo
> 1) {
7704 int mid
= (lo
+ hi
) / 2;
7705 sector_t a
= BB_OFFSET(p
[mid
]);
7711 if (hi
> lo
&& BB_OFFSET(p
[lo
]) > s
)
7715 /* we found a range that might merge with the start
7718 sector_t a
= BB_OFFSET(p
[lo
]);
7719 sector_t e
= a
+ BB_LEN(p
[lo
]);
7720 int ack
= BB_ACK(p
[lo
]);
7722 /* Yes, we can merge with a previous range */
7723 if (s
== a
&& s
+ sectors
>= e
)
7724 /* new range covers old */
7727 ack
= ack
&& acknowledged
;
7729 if (e
< s
+ sectors
)
7731 if (e
- a
<= BB_MAX_LEN
) {
7732 p
[lo
] = BB_MAKE(a
, e
-a
, ack
);
7735 /* does not all fit in one range,
7736 * make p[lo] maximal
7738 if (BB_LEN(p
[lo
]) != BB_MAX_LEN
)
7739 p
[lo
] = BB_MAKE(a
, BB_MAX_LEN
, ack
);
7745 if (sectors
&& hi
< bb
->count
) {
7746 /* 'hi' points to the first range that starts after 's'.
7747 * Maybe we can merge with the start of that range */
7748 sector_t a
= BB_OFFSET(p
[hi
]);
7749 sector_t e
= a
+ BB_LEN(p
[hi
]);
7750 int ack
= BB_ACK(p
[hi
]);
7751 if (a
<= s
+ sectors
) {
7752 /* merging is possible */
7753 if (e
<= s
+ sectors
) {
7758 ack
= ack
&& acknowledged
;
7761 if (e
- a
<= BB_MAX_LEN
) {
7762 p
[hi
] = BB_MAKE(a
, e
-a
, ack
);
7765 p
[hi
] = BB_MAKE(a
, BB_MAX_LEN
, ack
);
7773 if (sectors
== 0 && hi
< bb
->count
) {
7774 /* we might be able to combine lo and hi */
7775 /* Note: 's' is at the end of 'lo' */
7776 sector_t a
= BB_OFFSET(p
[hi
]);
7777 int lolen
= BB_LEN(p
[lo
]);
7778 int hilen
= BB_LEN(p
[hi
]);
7779 int newlen
= lolen
+ hilen
- (s
- a
);
7780 if (s
>= a
&& newlen
< BB_MAX_LEN
) {
7781 /* yes, we can combine them */
7782 int ack
= BB_ACK(p
[lo
]) && BB_ACK(p
[hi
]);
7783 p
[lo
] = BB_MAKE(BB_OFFSET(p
[lo
]), newlen
, ack
);
7784 memmove(p
+ hi
, p
+ hi
+ 1,
7785 (bb
->count
- hi
- 1) * 8);
7790 /* didn't merge (it all).
7791 * Need to add a range just before 'hi' */
7792 if (bb
->count
>= MD_MAX_BADBLOCKS
) {
7793 /* No room for more */
7797 int this_sectors
= sectors
;
7798 memmove(p
+ hi
+ 1, p
+ hi
,
7799 (bb
->count
- hi
) * 8);
7802 if (this_sectors
> BB_MAX_LEN
)
7803 this_sectors
= BB_MAX_LEN
;
7804 p
[hi
] = BB_MAKE(s
, this_sectors
, acknowledged
);
7805 sectors
-= this_sectors
;
7812 bb
->unacked_exist
= 1;
7813 write_sequnlock_irq(&bb
->lock
);
7818 int rdev_set_badblocks(mdk_rdev_t
*rdev
, sector_t s
, int sectors
,
7821 int rv
= md_set_badblocks(&rdev
->badblocks
,
7822 s
+ rdev
->data_offset
, sectors
, acknowledged
);
7824 /* Make sure they get written out promptly */
7825 set_bit(MD_CHANGE_CLEAN
, &rdev
->mddev
->flags
);
7826 md_wakeup_thread(rdev
->mddev
->thread
);
7830 EXPORT_SYMBOL_GPL(rdev_set_badblocks
);
7833 * Remove a range of bad blocks from the table.
7834 * This may involve extending the table if we spilt a region,
7835 * but it must not fail. So if the table becomes full, we just
7836 * drop the remove request.
7838 static int md_clear_badblocks(struct badblocks
*bb
, sector_t s
, int sectors
)
7842 sector_t target
= s
+ sectors
;
7845 if (bb
->shift
> 0) {
7846 /* When clearing we round the start up and the end down.
7847 * This should not matter as the shift should align with
7848 * the block size and no rounding should ever be needed.
7849 * However it is better the think a block is bad when it
7850 * isn't than to think a block is not bad when it is.
7852 s
+= (1<<bb
->shift
) - 1;
7854 target
>>= bb
->shift
;
7855 sectors
= target
- s
;
7858 write_seqlock_irq(&bb
->lock
);
7863 /* Find the last range that starts before 'target' */
7864 while (hi
- lo
> 1) {
7865 int mid
= (lo
+ hi
) / 2;
7866 sector_t a
= BB_OFFSET(p
[mid
]);
7873 /* p[lo] is the last range that could overlap the
7874 * current range. Earlier ranges could also overlap,
7875 * but only this one can overlap the end of the range.
7877 if (BB_OFFSET(p
[lo
]) + BB_LEN(p
[lo
]) > target
) {
7878 /* Partial overlap, leave the tail of this range */
7879 int ack
= BB_ACK(p
[lo
]);
7880 sector_t a
= BB_OFFSET(p
[lo
]);
7881 sector_t end
= a
+ BB_LEN(p
[lo
]);
7884 /* we need to split this range */
7885 if (bb
->count
>= MD_MAX_BADBLOCKS
) {
7889 memmove(p
+lo
+1, p
+lo
, (bb
->count
- lo
) * 8);
7891 p
[lo
] = BB_MAKE(a
, s
-a
, ack
);
7894 p
[lo
] = BB_MAKE(target
, end
- target
, ack
);
7895 /* there is no longer an overlap */
7900 BB_OFFSET(p
[lo
]) + BB_LEN(p
[lo
]) > s
) {
7901 /* This range does overlap */
7902 if (BB_OFFSET(p
[lo
]) < s
) {
7903 /* Keep the early parts of this range. */
7904 int ack
= BB_ACK(p
[lo
]);
7905 sector_t start
= BB_OFFSET(p
[lo
]);
7906 p
[lo
] = BB_MAKE(start
, s
- start
, ack
);
7907 /* now low doesn't overlap, so.. */
7912 /* 'lo' is strictly before, 'hi' is strictly after,
7913 * anything between needs to be discarded
7916 memmove(p
+lo
+1, p
+hi
, (bb
->count
- hi
) * 8);
7917 bb
->count
-= (hi
- lo
- 1);
7923 write_sequnlock_irq(&bb
->lock
);
7927 int rdev_clear_badblocks(mdk_rdev_t
*rdev
, sector_t s
, int sectors
)
7929 return md_clear_badblocks(&rdev
->badblocks
,
7930 s
+ rdev
->data_offset
,
7933 EXPORT_SYMBOL_GPL(rdev_clear_badblocks
);
7936 * Acknowledge all bad blocks in a list.
7937 * This only succeeds if ->changed is clear. It is used by
7938 * in-kernel metadata updates
7940 void md_ack_all_badblocks(struct badblocks
*bb
)
7942 if (bb
->page
== NULL
|| bb
->changed
)
7943 /* no point even trying */
7945 write_seqlock_irq(&bb
->lock
);
7947 if (bb
->changed
== 0) {
7950 for (i
= 0; i
< bb
->count
; i
++) {
7951 if (!BB_ACK(p
[i
])) {
7952 sector_t start
= BB_OFFSET(p
[i
]);
7953 int len
= BB_LEN(p
[i
]);
7954 p
[i
] = BB_MAKE(start
, len
, 1);
7957 bb
->unacked_exist
= 0;
7959 write_sequnlock_irq(&bb
->lock
);
7961 EXPORT_SYMBOL_GPL(md_ack_all_badblocks
);
7963 /* sysfs access to bad-blocks list.
7964 * We present two files.
7965 * 'bad-blocks' lists sector numbers and lengths of ranges that
7966 * are recorded as bad. The list is truncated to fit within
7967 * the one-page limit of sysfs.
7968 * Writing "sector length" to this file adds an acknowledged
7970 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
7971 * been acknowledged. Writing to this file adds bad blocks
7972 * without acknowledging them. This is largely for testing.
7976 badblocks_show(struct badblocks
*bb
, char *page
, int unack
)
7987 seq
= read_seqbegin(&bb
->lock
);
7992 while (len
< PAGE_SIZE
&& i
< bb
->count
) {
7993 sector_t s
= BB_OFFSET(p
[i
]);
7994 unsigned int length
= BB_LEN(p
[i
]);
7995 int ack
= BB_ACK(p
[i
]);
8001 len
+= snprintf(page
+len
, PAGE_SIZE
-len
, "%llu %u\n",
8002 (unsigned long long)s
<< bb
->shift
,
8003 length
<< bb
->shift
);
8005 if (unack
&& len
== 0)
8006 bb
->unacked_exist
= 0;
8008 if (read_seqretry(&bb
->lock
, seq
))
8017 badblocks_store(struct badblocks
*bb
, const char *page
, size_t len
, int unack
)
8019 unsigned long long sector
;
8023 /* Allow clearing via sysfs *only* for testing/debugging.
8024 * Normally only a successful write may clear a badblock
8027 if (page
[0] == '-') {
8031 #endif /* DO_DEBUG */
8033 switch (sscanf(page
, "%llu %d%c", §or
, &length
, &newline
)) {
8035 if (newline
!= '\n')
8047 md_clear_badblocks(bb
, sector
, length
);
8050 #endif /* DO_DEBUG */
8051 if (md_set_badblocks(bb
, sector
, length
, !unack
))
8057 static int md_notify_reboot(struct notifier_block
*this,
8058 unsigned long code
, void *x
)
8060 struct list_head
*tmp
;
8063 if ((code
== SYS_DOWN
) || (code
== SYS_HALT
) || (code
== SYS_POWER_OFF
)) {
8065 printk(KERN_INFO
"md: stopping all md devices.\n");
8067 for_each_mddev(mddev
, tmp
)
8068 if (mddev_trylock(mddev
)) {
8069 /* Force a switch to readonly even array
8070 * appears to still be in use. Hence
8073 md_set_readonly(mddev
, 100);
8074 mddev_unlock(mddev
);
8077 * certain more exotic SCSI devices are known to be
8078 * volatile wrt too early system reboots. While the
8079 * right place to handle this issue is the given
8080 * driver, we do want to have a safe RAID driver ...
8087 static struct notifier_block md_notifier
= {
8088 .notifier_call
= md_notify_reboot
,
8090 .priority
= INT_MAX
, /* before any real devices */
8093 static void md_geninit(void)
8095 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t
));
8097 proc_create("mdstat", S_IRUGO
, NULL
, &md_seq_fops
);
8100 static int __init
md_init(void)
8104 md_wq
= alloc_workqueue("md", WQ_MEM_RECLAIM
, 0);
8108 md_misc_wq
= alloc_workqueue("md_misc", 0, 0);
8112 if ((ret
= register_blkdev(MD_MAJOR
, "md")) < 0)
8115 if ((ret
= register_blkdev(0, "mdp")) < 0)
8119 blk_register_region(MKDEV(MD_MAJOR
, 0), 1UL<<MINORBITS
, THIS_MODULE
,
8120 md_probe
, NULL
, NULL
);
8121 blk_register_region(MKDEV(mdp_major
, 0), 1UL<<MINORBITS
, THIS_MODULE
,
8122 md_probe
, NULL
, NULL
);
8124 register_reboot_notifier(&md_notifier
);
8125 raid_table_header
= register_sysctl_table(raid_root_table
);
8131 unregister_blkdev(MD_MAJOR
, "md");
8133 destroy_workqueue(md_misc_wq
);
8135 destroy_workqueue(md_wq
);
8143 * Searches all registered partitions for autorun RAID arrays
8147 static LIST_HEAD(all_detected_devices
);
8148 struct detected_devices_node
{
8149 struct list_head list
;
8153 void md_autodetect_dev(dev_t dev
)
8155 struct detected_devices_node
*node_detected_dev
;
8157 node_detected_dev
= kzalloc(sizeof(*node_detected_dev
), GFP_KERNEL
);
8158 if (node_detected_dev
) {
8159 node_detected_dev
->dev
= dev
;
8160 list_add_tail(&node_detected_dev
->list
, &all_detected_devices
);
8162 printk(KERN_CRIT
"md: md_autodetect_dev: kzalloc failed"
8163 ", skipping dev(%d,%d)\n", MAJOR(dev
), MINOR(dev
));
8168 static void autostart_arrays(int part
)
8171 struct detected_devices_node
*node_detected_dev
;
8173 int i_scanned
, i_passed
;
8178 printk(KERN_INFO
"md: Autodetecting RAID arrays.\n");
8180 while (!list_empty(&all_detected_devices
) && i_scanned
< INT_MAX
) {
8182 node_detected_dev
= list_entry(all_detected_devices
.next
,
8183 struct detected_devices_node
, list
);
8184 list_del(&node_detected_dev
->list
);
8185 dev
= node_detected_dev
->dev
;
8186 kfree(node_detected_dev
);
8187 rdev
= md_import_device(dev
,0, 90);
8191 if (test_bit(Faulty
, &rdev
->flags
)) {
8195 set_bit(AutoDetected
, &rdev
->flags
);
8196 list_add(&rdev
->same_set
, &pending_raid_disks
);
8200 printk(KERN_INFO
"md: Scanned %d and added %d devices.\n",
8201 i_scanned
, i_passed
);
8203 autorun_devices(part
);
8206 #endif /* !MODULE */
8208 static __exit
void md_exit(void)
8211 struct list_head
*tmp
;
8213 blk_unregister_region(MKDEV(MD_MAJOR
,0), 1U << MINORBITS
);
8214 blk_unregister_region(MKDEV(mdp_major
,0), 1U << MINORBITS
);
8216 unregister_blkdev(MD_MAJOR
,"md");
8217 unregister_blkdev(mdp_major
, "mdp");
8218 unregister_reboot_notifier(&md_notifier
);
8219 unregister_sysctl_table(raid_table_header
);
8220 remove_proc_entry("mdstat", NULL
);
8221 for_each_mddev(mddev
, tmp
) {
8222 export_array(mddev
);
8223 mddev
->hold_active
= 0;
8225 destroy_workqueue(md_misc_wq
);
8226 destroy_workqueue(md_wq
);
8229 subsys_initcall(md_init
);
8230 module_exit(md_exit
)
8232 static int get_ro(char *buffer
, struct kernel_param
*kp
)
8234 return sprintf(buffer
, "%d", start_readonly
);
8236 static int set_ro(const char *val
, struct kernel_param
*kp
)
8239 int num
= simple_strtoul(val
, &e
, 10);
8240 if (*val
&& (*e
== '\0' || *e
== '\n')) {
8241 start_readonly
= num
;
8247 module_param_call(start_ro
, set_ro
, get_ro
, NULL
, S_IRUSR
|S_IWUSR
);
8248 module_param(start_dirty_degraded
, int, S_IRUGO
|S_IWUSR
);
8250 module_param_call(new_array
, add_named_array
, NULL
, NULL
, S_IWUSR
);
8252 EXPORT_SYMBOL(register_md_personality
);
8253 EXPORT_SYMBOL(unregister_md_personality
);
8254 EXPORT_SYMBOL(md_error
);
8255 EXPORT_SYMBOL(md_done_sync
);
8256 EXPORT_SYMBOL(md_write_start
);
8257 EXPORT_SYMBOL(md_write_end
);
8258 EXPORT_SYMBOL(md_register_thread
);
8259 EXPORT_SYMBOL(md_unregister_thread
);
8260 EXPORT_SYMBOL(md_wakeup_thread
);
8261 EXPORT_SYMBOL(md_check_recovery
);
8262 MODULE_LICENSE("GPL");
8263 MODULE_DESCRIPTION("MD RAID framework");
8265 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR
);