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(REQ_WRITE
| REQ_SYNC
| REQ_FLUSH
| REQ_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
;
1142 if (rdev
->sectors
< sb
->size
* 2 && sb
->level
> 1)
1143 /* "this cannot possibly happen" ... */
1151 * validate_super for 0.90.0
1153 static int super_90_validate(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1156 mdp_super_t
*sb
= page_address(rdev
->sb_page
);
1157 __u64 ev1
= md_event(sb
);
1159 rdev
->raid_disk
= -1;
1160 clear_bit(Faulty
, &rdev
->flags
);
1161 clear_bit(In_sync
, &rdev
->flags
);
1162 clear_bit(WriteMostly
, &rdev
->flags
);
1164 if (mddev
->raid_disks
== 0) {
1165 mddev
->major_version
= 0;
1166 mddev
->minor_version
= sb
->minor_version
;
1167 mddev
->patch_version
= sb
->patch_version
;
1168 mddev
->external
= 0;
1169 mddev
->chunk_sectors
= sb
->chunk_size
>> 9;
1170 mddev
->ctime
= sb
->ctime
;
1171 mddev
->utime
= sb
->utime
;
1172 mddev
->level
= sb
->level
;
1173 mddev
->clevel
[0] = 0;
1174 mddev
->layout
= sb
->layout
;
1175 mddev
->raid_disks
= sb
->raid_disks
;
1176 mddev
->dev_sectors
= sb
->size
* 2;
1177 mddev
->events
= ev1
;
1178 mddev
->bitmap_info
.offset
= 0;
1179 mddev
->bitmap_info
.default_offset
= MD_SB_BYTES
>> 9;
1181 if (mddev
->minor_version
>= 91) {
1182 mddev
->reshape_position
= sb
->reshape_position
;
1183 mddev
->delta_disks
= sb
->delta_disks
;
1184 mddev
->new_level
= sb
->new_level
;
1185 mddev
->new_layout
= sb
->new_layout
;
1186 mddev
->new_chunk_sectors
= sb
->new_chunk
>> 9;
1188 mddev
->reshape_position
= MaxSector
;
1189 mddev
->delta_disks
= 0;
1190 mddev
->new_level
= mddev
->level
;
1191 mddev
->new_layout
= mddev
->layout
;
1192 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
1195 if (sb
->state
& (1<<MD_SB_CLEAN
))
1196 mddev
->recovery_cp
= MaxSector
;
1198 if (sb
->events_hi
== sb
->cp_events_hi
&&
1199 sb
->events_lo
== sb
->cp_events_lo
) {
1200 mddev
->recovery_cp
= sb
->recovery_cp
;
1202 mddev
->recovery_cp
= 0;
1205 memcpy(mddev
->uuid
+0, &sb
->set_uuid0
, 4);
1206 memcpy(mddev
->uuid
+4, &sb
->set_uuid1
, 4);
1207 memcpy(mddev
->uuid
+8, &sb
->set_uuid2
, 4);
1208 memcpy(mddev
->uuid
+12,&sb
->set_uuid3
, 4);
1210 mddev
->max_disks
= MD_SB_DISKS
;
1212 if (sb
->state
& (1<<MD_SB_BITMAP_PRESENT
) &&
1213 mddev
->bitmap_info
.file
== NULL
)
1214 mddev
->bitmap_info
.offset
=
1215 mddev
->bitmap_info
.default_offset
;
1217 } else if (mddev
->pers
== NULL
) {
1218 /* Insist on good event counter while assembling, except
1219 * for spares (which don't need an event count) */
1221 if (sb
->disks
[rdev
->desc_nr
].state
& (
1222 (1<<MD_DISK_SYNC
) | (1 << MD_DISK_ACTIVE
)))
1223 if (ev1
< mddev
->events
)
1225 } else if (mddev
->bitmap
) {
1226 /* if adding to array with a bitmap, then we can accept an
1227 * older device ... but not too old.
1229 if (ev1
< mddev
->bitmap
->events_cleared
)
1232 if (ev1
< mddev
->events
)
1233 /* just a hot-add of a new device, leave raid_disk at -1 */
1237 if (mddev
->level
!= LEVEL_MULTIPATH
) {
1238 desc
= sb
->disks
+ rdev
->desc_nr
;
1240 if (desc
->state
& (1<<MD_DISK_FAULTY
))
1241 set_bit(Faulty
, &rdev
->flags
);
1242 else if (desc
->state
& (1<<MD_DISK_SYNC
) /* &&
1243 desc->raid_disk < mddev->raid_disks */) {
1244 set_bit(In_sync
, &rdev
->flags
);
1245 rdev
->raid_disk
= desc
->raid_disk
;
1246 } else if (desc
->state
& (1<<MD_DISK_ACTIVE
)) {
1247 /* active but not in sync implies recovery up to
1248 * reshape position. We don't know exactly where
1249 * that is, so set to zero for now */
1250 if (mddev
->minor_version
>= 91) {
1251 rdev
->recovery_offset
= 0;
1252 rdev
->raid_disk
= desc
->raid_disk
;
1255 if (desc
->state
& (1<<MD_DISK_WRITEMOSTLY
))
1256 set_bit(WriteMostly
, &rdev
->flags
);
1257 } else /* MULTIPATH are always insync */
1258 set_bit(In_sync
, &rdev
->flags
);
1263 * sync_super for 0.90.0
1265 static void super_90_sync(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1269 int next_spare
= mddev
->raid_disks
;
1272 /* make rdev->sb match mddev data..
1275 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1276 * 3/ any empty disks < next_spare become removed
1278 * disks[0] gets initialised to REMOVED because
1279 * we cannot be sure from other fields if it has
1280 * been initialised or not.
1283 int active
=0, working
=0,failed
=0,spare
=0,nr_disks
=0;
1285 rdev
->sb_size
= MD_SB_BYTES
;
1287 sb
= page_address(rdev
->sb_page
);
1289 memset(sb
, 0, sizeof(*sb
));
1291 sb
->md_magic
= MD_SB_MAGIC
;
1292 sb
->major_version
= mddev
->major_version
;
1293 sb
->patch_version
= mddev
->patch_version
;
1294 sb
->gvalid_words
= 0; /* ignored */
1295 memcpy(&sb
->set_uuid0
, mddev
->uuid
+0, 4);
1296 memcpy(&sb
->set_uuid1
, mddev
->uuid
+4, 4);
1297 memcpy(&sb
->set_uuid2
, mddev
->uuid
+8, 4);
1298 memcpy(&sb
->set_uuid3
, mddev
->uuid
+12,4);
1300 sb
->ctime
= mddev
->ctime
;
1301 sb
->level
= mddev
->level
;
1302 sb
->size
= mddev
->dev_sectors
/ 2;
1303 sb
->raid_disks
= mddev
->raid_disks
;
1304 sb
->md_minor
= mddev
->md_minor
;
1305 sb
->not_persistent
= 0;
1306 sb
->utime
= mddev
->utime
;
1308 sb
->events_hi
= (mddev
->events
>>32);
1309 sb
->events_lo
= (u32
)mddev
->events
;
1311 if (mddev
->reshape_position
== MaxSector
)
1312 sb
->minor_version
= 90;
1314 sb
->minor_version
= 91;
1315 sb
->reshape_position
= mddev
->reshape_position
;
1316 sb
->new_level
= mddev
->new_level
;
1317 sb
->delta_disks
= mddev
->delta_disks
;
1318 sb
->new_layout
= mddev
->new_layout
;
1319 sb
->new_chunk
= mddev
->new_chunk_sectors
<< 9;
1321 mddev
->minor_version
= sb
->minor_version
;
1324 sb
->recovery_cp
= mddev
->recovery_cp
;
1325 sb
->cp_events_hi
= (mddev
->events
>>32);
1326 sb
->cp_events_lo
= (u32
)mddev
->events
;
1327 if (mddev
->recovery_cp
== MaxSector
)
1328 sb
->state
= (1<< MD_SB_CLEAN
);
1330 sb
->recovery_cp
= 0;
1332 sb
->layout
= mddev
->layout
;
1333 sb
->chunk_size
= mddev
->chunk_sectors
<< 9;
1335 if (mddev
->bitmap
&& mddev
->bitmap_info
.file
== NULL
)
1336 sb
->state
|= (1<<MD_SB_BITMAP_PRESENT
);
1338 sb
->disks
[0].state
= (1<<MD_DISK_REMOVED
);
1339 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
) {
1342 int is_active
= test_bit(In_sync
, &rdev2
->flags
);
1344 if (rdev2
->raid_disk
>= 0 &&
1345 sb
->minor_version
>= 91)
1346 /* we have nowhere to store the recovery_offset,
1347 * but if it is not below the reshape_position,
1348 * we can piggy-back on that.
1351 if (rdev2
->raid_disk
< 0 ||
1352 test_bit(Faulty
, &rdev2
->flags
))
1355 desc_nr
= rdev2
->raid_disk
;
1357 desc_nr
= next_spare
++;
1358 rdev2
->desc_nr
= desc_nr
;
1359 d
= &sb
->disks
[rdev2
->desc_nr
];
1361 d
->number
= rdev2
->desc_nr
;
1362 d
->major
= MAJOR(rdev2
->bdev
->bd_dev
);
1363 d
->minor
= MINOR(rdev2
->bdev
->bd_dev
);
1365 d
->raid_disk
= rdev2
->raid_disk
;
1367 d
->raid_disk
= rdev2
->desc_nr
; /* compatibility */
1368 if (test_bit(Faulty
, &rdev2
->flags
))
1369 d
->state
= (1<<MD_DISK_FAULTY
);
1370 else if (is_active
) {
1371 d
->state
= (1<<MD_DISK_ACTIVE
);
1372 if (test_bit(In_sync
, &rdev2
->flags
))
1373 d
->state
|= (1<<MD_DISK_SYNC
);
1381 if (test_bit(WriteMostly
, &rdev2
->flags
))
1382 d
->state
|= (1<<MD_DISK_WRITEMOSTLY
);
1384 /* now set the "removed" and "faulty" bits on any missing devices */
1385 for (i
=0 ; i
< mddev
->raid_disks
; i
++) {
1386 mdp_disk_t
*d
= &sb
->disks
[i
];
1387 if (d
->state
== 0 && d
->number
== 0) {
1390 d
->state
= (1<<MD_DISK_REMOVED
);
1391 d
->state
|= (1<<MD_DISK_FAULTY
);
1395 sb
->nr_disks
= nr_disks
;
1396 sb
->active_disks
= active
;
1397 sb
->working_disks
= working
;
1398 sb
->failed_disks
= failed
;
1399 sb
->spare_disks
= spare
;
1401 sb
->this_disk
= sb
->disks
[rdev
->desc_nr
];
1402 sb
->sb_csum
= calc_sb_csum(sb
);
1406 * rdev_size_change for 0.90.0
1408 static unsigned long long
1409 super_90_rdev_size_change(mdk_rdev_t
*rdev
, sector_t num_sectors
)
1411 if (num_sectors
&& num_sectors
< rdev
->mddev
->dev_sectors
)
1412 return 0; /* component must fit device */
1413 if (rdev
->mddev
->bitmap_info
.offset
)
1414 return 0; /* can't move bitmap */
1415 rdev
->sb_start
= calc_dev_sboffset(rdev
);
1416 if (!num_sectors
|| num_sectors
> rdev
->sb_start
)
1417 num_sectors
= rdev
->sb_start
;
1418 md_super_write(rdev
->mddev
, rdev
, rdev
->sb_start
, rdev
->sb_size
,
1420 md_super_wait(rdev
->mddev
);
1426 * version 1 superblock
1429 static __le32
calc_sb_1_csum(struct mdp_superblock_1
* sb
)
1433 unsigned long long newcsum
;
1434 int size
= 256 + le32_to_cpu(sb
->max_dev
)*2;
1435 __le32
*isuper
= (__le32
*)sb
;
1438 disk_csum
= sb
->sb_csum
;
1441 for (i
=0; size
>=4; size
-= 4 )
1442 newcsum
+= le32_to_cpu(*isuper
++);
1445 newcsum
+= le16_to_cpu(*(__le16
*) isuper
);
1447 csum
= (newcsum
& 0xffffffff) + (newcsum
>> 32);
1448 sb
->sb_csum
= disk_csum
;
1449 return cpu_to_le32(csum
);
1452 static int md_set_badblocks(struct badblocks
*bb
, sector_t s
, int sectors
,
1454 static int super_1_load(mdk_rdev_t
*rdev
, mdk_rdev_t
*refdev
, int minor_version
)
1456 struct mdp_superblock_1
*sb
;
1459 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
1463 * Calculate the position of the superblock in 512byte sectors.
1464 * It is always aligned to a 4K boundary and
1465 * depeding on minor_version, it can be:
1466 * 0: At least 8K, but less than 12K, from end of device
1467 * 1: At start of device
1468 * 2: 4K from start of device.
1470 switch(minor_version
) {
1472 sb_start
= i_size_read(rdev
->bdev
->bd_inode
) >> 9;
1474 sb_start
&= ~(sector_t
)(4*2-1);
1485 rdev
->sb_start
= sb_start
;
1487 /* superblock is rarely larger than 1K, but it can be larger,
1488 * and it is safe to read 4k, so we do that
1490 ret
= read_disk_sb(rdev
, 4096);
1491 if (ret
) return ret
;
1494 sb
= page_address(rdev
->sb_page
);
1496 if (sb
->magic
!= cpu_to_le32(MD_SB_MAGIC
) ||
1497 sb
->major_version
!= cpu_to_le32(1) ||
1498 le32_to_cpu(sb
->max_dev
) > (4096-256)/2 ||
1499 le64_to_cpu(sb
->super_offset
) != rdev
->sb_start
||
1500 (le32_to_cpu(sb
->feature_map
) & ~MD_FEATURE_ALL
) != 0)
1503 if (calc_sb_1_csum(sb
) != sb
->sb_csum
) {
1504 printk("md: invalid superblock checksum on %s\n",
1505 bdevname(rdev
->bdev
,b
));
1508 if (le64_to_cpu(sb
->data_size
) < 10) {
1509 printk("md: data_size too small on %s\n",
1510 bdevname(rdev
->bdev
,b
));
1514 rdev
->preferred_minor
= 0xffff;
1515 rdev
->data_offset
= le64_to_cpu(sb
->data_offset
);
1516 atomic_set(&rdev
->corrected_errors
, le32_to_cpu(sb
->cnt_corrected_read
));
1518 rdev
->sb_size
= le32_to_cpu(sb
->max_dev
) * 2 + 256;
1519 bmask
= queue_logical_block_size(rdev
->bdev
->bd_disk
->queue
)-1;
1520 if (rdev
->sb_size
& bmask
)
1521 rdev
->sb_size
= (rdev
->sb_size
| bmask
) + 1;
1524 && rdev
->data_offset
< sb_start
+ (rdev
->sb_size
/512))
1527 if (sb
->level
== cpu_to_le32(LEVEL_MULTIPATH
))
1530 rdev
->desc_nr
= le32_to_cpu(sb
->dev_number
);
1532 if (!rdev
->bb_page
) {
1533 rdev
->bb_page
= alloc_page(GFP_KERNEL
);
1537 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_BAD_BLOCKS
) &&
1538 rdev
->badblocks
.count
== 0) {
1539 /* need to load the bad block list.
1540 * Currently we limit it to one page.
1546 int sectors
= le16_to_cpu(sb
->bblog_size
);
1547 if (sectors
> (PAGE_SIZE
/ 512))
1549 offset
= le32_to_cpu(sb
->bblog_offset
);
1552 bb_sector
= (long long)offset
;
1553 if (!sync_page_io(rdev
, bb_sector
, sectors
<< 9,
1554 rdev
->bb_page
, READ
, true))
1556 bbp
= (u64
*)page_address(rdev
->bb_page
);
1557 rdev
->badblocks
.shift
= sb
->bblog_shift
;
1558 for (i
= 0 ; i
< (sectors
<< (9-3)) ; i
++, bbp
++) {
1559 u64 bb
= le64_to_cpu(*bbp
);
1560 int count
= bb
& (0x3ff);
1561 u64 sector
= bb
>> 10;
1562 sector
<<= sb
->bblog_shift
;
1563 count
<<= sb
->bblog_shift
;
1566 if (md_set_badblocks(&rdev
->badblocks
,
1567 sector
, count
, 1) == 0)
1570 } else if (sb
->bblog_offset
== 0)
1571 rdev
->badblocks
.shift
= -1;
1577 struct mdp_superblock_1
*refsb
= page_address(refdev
->sb_page
);
1579 if (memcmp(sb
->set_uuid
, refsb
->set_uuid
, 16) != 0 ||
1580 sb
->level
!= refsb
->level
||
1581 sb
->layout
!= refsb
->layout
||
1582 sb
->chunksize
!= refsb
->chunksize
) {
1583 printk(KERN_WARNING
"md: %s has strangely different"
1584 " superblock to %s\n",
1585 bdevname(rdev
->bdev
,b
),
1586 bdevname(refdev
->bdev
,b2
));
1589 ev1
= le64_to_cpu(sb
->events
);
1590 ev2
= le64_to_cpu(refsb
->events
);
1598 rdev
->sectors
= (i_size_read(rdev
->bdev
->bd_inode
) >> 9) -
1599 le64_to_cpu(sb
->data_offset
);
1601 rdev
->sectors
= rdev
->sb_start
;
1602 if (rdev
->sectors
< le64_to_cpu(sb
->data_size
))
1604 rdev
->sectors
= le64_to_cpu(sb
->data_size
);
1605 if (le64_to_cpu(sb
->size
) > rdev
->sectors
)
1610 static int super_1_validate(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1612 struct mdp_superblock_1
*sb
= page_address(rdev
->sb_page
);
1613 __u64 ev1
= le64_to_cpu(sb
->events
);
1615 rdev
->raid_disk
= -1;
1616 clear_bit(Faulty
, &rdev
->flags
);
1617 clear_bit(In_sync
, &rdev
->flags
);
1618 clear_bit(WriteMostly
, &rdev
->flags
);
1620 if (mddev
->raid_disks
== 0) {
1621 mddev
->major_version
= 1;
1622 mddev
->patch_version
= 0;
1623 mddev
->external
= 0;
1624 mddev
->chunk_sectors
= le32_to_cpu(sb
->chunksize
);
1625 mddev
->ctime
= le64_to_cpu(sb
->ctime
) & ((1ULL << 32)-1);
1626 mddev
->utime
= le64_to_cpu(sb
->utime
) & ((1ULL << 32)-1);
1627 mddev
->level
= le32_to_cpu(sb
->level
);
1628 mddev
->clevel
[0] = 0;
1629 mddev
->layout
= le32_to_cpu(sb
->layout
);
1630 mddev
->raid_disks
= le32_to_cpu(sb
->raid_disks
);
1631 mddev
->dev_sectors
= le64_to_cpu(sb
->size
);
1632 mddev
->events
= ev1
;
1633 mddev
->bitmap_info
.offset
= 0;
1634 mddev
->bitmap_info
.default_offset
= 1024 >> 9;
1636 mddev
->recovery_cp
= le64_to_cpu(sb
->resync_offset
);
1637 memcpy(mddev
->uuid
, sb
->set_uuid
, 16);
1639 mddev
->max_disks
= (4096-256)/2;
1641 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_BITMAP_OFFSET
) &&
1642 mddev
->bitmap_info
.file
== NULL
)
1643 mddev
->bitmap_info
.offset
=
1644 (__s32
)le32_to_cpu(sb
->bitmap_offset
);
1646 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RESHAPE_ACTIVE
)) {
1647 mddev
->reshape_position
= le64_to_cpu(sb
->reshape_position
);
1648 mddev
->delta_disks
= le32_to_cpu(sb
->delta_disks
);
1649 mddev
->new_level
= le32_to_cpu(sb
->new_level
);
1650 mddev
->new_layout
= le32_to_cpu(sb
->new_layout
);
1651 mddev
->new_chunk_sectors
= le32_to_cpu(sb
->new_chunk
);
1653 mddev
->reshape_position
= MaxSector
;
1654 mddev
->delta_disks
= 0;
1655 mddev
->new_level
= mddev
->level
;
1656 mddev
->new_layout
= mddev
->layout
;
1657 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
1660 } else if (mddev
->pers
== NULL
) {
1661 /* Insist of good event counter while assembling, except for
1662 * spares (which don't need an event count) */
1664 if (rdev
->desc_nr
>= 0 &&
1665 rdev
->desc_nr
< le32_to_cpu(sb
->max_dev
) &&
1666 le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]) < 0xfffe)
1667 if (ev1
< mddev
->events
)
1669 } else if (mddev
->bitmap
) {
1670 /* If adding to array with a bitmap, then we can accept an
1671 * older device, but not too old.
1673 if (ev1
< mddev
->bitmap
->events_cleared
)
1676 if (ev1
< mddev
->events
)
1677 /* just a hot-add of a new device, leave raid_disk at -1 */
1680 if (mddev
->level
!= LEVEL_MULTIPATH
) {
1682 if (rdev
->desc_nr
< 0 ||
1683 rdev
->desc_nr
>= le32_to_cpu(sb
->max_dev
)) {
1687 role
= le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]);
1689 case 0xffff: /* spare */
1691 case 0xfffe: /* faulty */
1692 set_bit(Faulty
, &rdev
->flags
);
1695 if ((le32_to_cpu(sb
->feature_map
) &
1696 MD_FEATURE_RECOVERY_OFFSET
))
1697 rdev
->recovery_offset
= le64_to_cpu(sb
->recovery_offset
);
1699 set_bit(In_sync
, &rdev
->flags
);
1700 rdev
->raid_disk
= role
;
1703 if (sb
->devflags
& WriteMostly1
)
1704 set_bit(WriteMostly
, &rdev
->flags
);
1705 } else /* MULTIPATH are always insync */
1706 set_bit(In_sync
, &rdev
->flags
);
1711 static void super_1_sync(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1713 struct mdp_superblock_1
*sb
;
1716 /* make rdev->sb match mddev and rdev data. */
1718 sb
= page_address(rdev
->sb_page
);
1720 sb
->feature_map
= 0;
1722 sb
->recovery_offset
= cpu_to_le64(0);
1723 memset(sb
->pad1
, 0, sizeof(sb
->pad1
));
1724 memset(sb
->pad3
, 0, sizeof(sb
->pad3
));
1726 sb
->utime
= cpu_to_le64((__u64
)mddev
->utime
);
1727 sb
->events
= cpu_to_le64(mddev
->events
);
1729 sb
->resync_offset
= cpu_to_le64(mddev
->recovery_cp
);
1731 sb
->resync_offset
= cpu_to_le64(0);
1733 sb
->cnt_corrected_read
= cpu_to_le32(atomic_read(&rdev
->corrected_errors
));
1735 sb
->raid_disks
= cpu_to_le32(mddev
->raid_disks
);
1736 sb
->size
= cpu_to_le64(mddev
->dev_sectors
);
1737 sb
->chunksize
= cpu_to_le32(mddev
->chunk_sectors
);
1738 sb
->level
= cpu_to_le32(mddev
->level
);
1739 sb
->layout
= cpu_to_le32(mddev
->layout
);
1741 if (mddev
->bitmap
&& mddev
->bitmap_info
.file
== NULL
) {
1742 sb
->bitmap_offset
= cpu_to_le32((__u32
)mddev
->bitmap_info
.offset
);
1743 sb
->feature_map
= cpu_to_le32(MD_FEATURE_BITMAP_OFFSET
);
1746 if (rdev
->raid_disk
>= 0 &&
1747 !test_bit(In_sync
, &rdev
->flags
)) {
1749 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET
);
1750 sb
->recovery_offset
=
1751 cpu_to_le64(rdev
->recovery_offset
);
1754 if (mddev
->reshape_position
!= MaxSector
) {
1755 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE
);
1756 sb
->reshape_position
= cpu_to_le64(mddev
->reshape_position
);
1757 sb
->new_layout
= cpu_to_le32(mddev
->new_layout
);
1758 sb
->delta_disks
= cpu_to_le32(mddev
->delta_disks
);
1759 sb
->new_level
= cpu_to_le32(mddev
->new_level
);
1760 sb
->new_chunk
= cpu_to_le32(mddev
->new_chunk_sectors
);
1763 if (rdev
->badblocks
.count
== 0)
1764 /* Nothing to do for bad blocks*/ ;
1765 else if (sb
->bblog_offset
== 0)
1766 /* Cannot record bad blocks on this device */
1767 md_error(mddev
, rdev
);
1769 struct badblocks
*bb
= &rdev
->badblocks
;
1770 u64
*bbp
= (u64
*)page_address(rdev
->bb_page
);
1772 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_BAD_BLOCKS
);
1777 seq
= read_seqbegin(&bb
->lock
);
1779 memset(bbp
, 0xff, PAGE_SIZE
);
1781 for (i
= 0 ; i
< bb
->count
; i
++) {
1782 u64 internal_bb
= *p
++;
1783 u64 store_bb
= ((BB_OFFSET(internal_bb
) << 10)
1784 | BB_LEN(internal_bb
));
1785 *bbp
++ = cpu_to_le64(store_bb
);
1787 if (read_seqretry(&bb
->lock
, seq
))
1790 bb
->sector
= (rdev
->sb_start
+
1791 (int)le32_to_cpu(sb
->bblog_offset
));
1792 bb
->size
= le16_to_cpu(sb
->bblog_size
);
1798 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
)
1799 if (rdev2
->desc_nr
+1 > max_dev
)
1800 max_dev
= rdev2
->desc_nr
+1;
1802 if (max_dev
> le32_to_cpu(sb
->max_dev
)) {
1804 sb
->max_dev
= cpu_to_le32(max_dev
);
1805 rdev
->sb_size
= max_dev
* 2 + 256;
1806 bmask
= queue_logical_block_size(rdev
->bdev
->bd_disk
->queue
)-1;
1807 if (rdev
->sb_size
& bmask
)
1808 rdev
->sb_size
= (rdev
->sb_size
| bmask
) + 1;
1810 max_dev
= le32_to_cpu(sb
->max_dev
);
1812 for (i
=0; i
<max_dev
;i
++)
1813 sb
->dev_roles
[i
] = cpu_to_le16(0xfffe);
1815 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
) {
1817 if (test_bit(Faulty
, &rdev2
->flags
))
1818 sb
->dev_roles
[i
] = cpu_to_le16(0xfffe);
1819 else if (test_bit(In_sync
, &rdev2
->flags
))
1820 sb
->dev_roles
[i
] = cpu_to_le16(rdev2
->raid_disk
);
1821 else if (rdev2
->raid_disk
>= 0)
1822 sb
->dev_roles
[i
] = cpu_to_le16(rdev2
->raid_disk
);
1824 sb
->dev_roles
[i
] = cpu_to_le16(0xffff);
1827 sb
->sb_csum
= calc_sb_1_csum(sb
);
1830 static unsigned long long
1831 super_1_rdev_size_change(mdk_rdev_t
*rdev
, sector_t num_sectors
)
1833 struct mdp_superblock_1
*sb
;
1834 sector_t max_sectors
;
1835 if (num_sectors
&& num_sectors
< rdev
->mddev
->dev_sectors
)
1836 return 0; /* component must fit device */
1837 if (rdev
->sb_start
< rdev
->data_offset
) {
1838 /* minor versions 1 and 2; superblock before data */
1839 max_sectors
= i_size_read(rdev
->bdev
->bd_inode
) >> 9;
1840 max_sectors
-= rdev
->data_offset
;
1841 if (!num_sectors
|| num_sectors
> max_sectors
)
1842 num_sectors
= max_sectors
;
1843 } else if (rdev
->mddev
->bitmap_info
.offset
) {
1844 /* minor version 0 with bitmap we can't move */
1847 /* minor version 0; superblock after data */
1849 sb_start
= (i_size_read(rdev
->bdev
->bd_inode
) >> 9) - 8*2;
1850 sb_start
&= ~(sector_t
)(4*2 - 1);
1851 max_sectors
= rdev
->sectors
+ sb_start
- rdev
->sb_start
;
1852 if (!num_sectors
|| num_sectors
> max_sectors
)
1853 num_sectors
= max_sectors
;
1854 rdev
->sb_start
= sb_start
;
1856 sb
= page_address(rdev
->sb_page
);
1857 sb
->data_size
= cpu_to_le64(num_sectors
);
1858 sb
->super_offset
= rdev
->sb_start
;
1859 sb
->sb_csum
= calc_sb_1_csum(sb
);
1860 md_super_write(rdev
->mddev
, rdev
, rdev
->sb_start
, rdev
->sb_size
,
1862 md_super_wait(rdev
->mddev
);
1866 static struct super_type super_types
[] = {
1869 .owner
= THIS_MODULE
,
1870 .load_super
= super_90_load
,
1871 .validate_super
= super_90_validate
,
1872 .sync_super
= super_90_sync
,
1873 .rdev_size_change
= super_90_rdev_size_change
,
1877 .owner
= THIS_MODULE
,
1878 .load_super
= super_1_load
,
1879 .validate_super
= super_1_validate
,
1880 .sync_super
= super_1_sync
,
1881 .rdev_size_change
= super_1_rdev_size_change
,
1885 static void sync_super(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1887 if (mddev
->sync_super
) {
1888 mddev
->sync_super(mddev
, rdev
);
1892 BUG_ON(mddev
->major_version
>= ARRAY_SIZE(super_types
));
1894 super_types
[mddev
->major_version
].sync_super(mddev
, rdev
);
1897 static int match_mddev_units(mddev_t
*mddev1
, mddev_t
*mddev2
)
1899 mdk_rdev_t
*rdev
, *rdev2
;
1902 rdev_for_each_rcu(rdev
, mddev1
)
1903 rdev_for_each_rcu(rdev2
, mddev2
)
1904 if (rdev
->bdev
->bd_contains
==
1905 rdev2
->bdev
->bd_contains
) {
1913 static LIST_HEAD(pending_raid_disks
);
1916 * Try to register data integrity profile for an mddev
1918 * This is called when an array is started and after a disk has been kicked
1919 * from the array. It only succeeds if all working and active component devices
1920 * are integrity capable with matching profiles.
1922 int md_integrity_register(mddev_t
*mddev
)
1924 mdk_rdev_t
*rdev
, *reference
= NULL
;
1926 if (list_empty(&mddev
->disks
))
1927 return 0; /* nothing to do */
1928 if (!mddev
->gendisk
|| blk_get_integrity(mddev
->gendisk
))
1929 return 0; /* shouldn't register, or already is */
1930 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
1931 /* skip spares and non-functional disks */
1932 if (test_bit(Faulty
, &rdev
->flags
))
1934 if (rdev
->raid_disk
< 0)
1937 /* Use the first rdev as the reference */
1941 /* does this rdev's profile match the reference profile? */
1942 if (blk_integrity_compare(reference
->bdev
->bd_disk
,
1943 rdev
->bdev
->bd_disk
) < 0)
1946 if (!reference
|| !bdev_get_integrity(reference
->bdev
))
1949 * All component devices are integrity capable and have matching
1950 * profiles, register the common profile for the md device.
1952 if (blk_integrity_register(mddev
->gendisk
,
1953 bdev_get_integrity(reference
->bdev
)) != 0) {
1954 printk(KERN_ERR
"md: failed to register integrity for %s\n",
1958 printk(KERN_NOTICE
"md: data integrity enabled on %s\n", mdname(mddev
));
1959 if (bioset_integrity_create(mddev
->bio_set
, BIO_POOL_SIZE
)) {
1960 printk(KERN_ERR
"md: failed to create integrity pool for %s\n",
1966 EXPORT_SYMBOL(md_integrity_register
);
1968 /* Disable data integrity if non-capable/non-matching disk is being added */
1969 void md_integrity_add_rdev(mdk_rdev_t
*rdev
, mddev_t
*mddev
)
1971 struct blk_integrity
*bi_rdev
= bdev_get_integrity(rdev
->bdev
);
1972 struct blk_integrity
*bi_mddev
= blk_get_integrity(mddev
->gendisk
);
1974 if (!bi_mddev
) /* nothing to do */
1976 if (rdev
->raid_disk
< 0) /* skip spares */
1978 if (bi_rdev
&& blk_integrity_compare(mddev
->gendisk
,
1979 rdev
->bdev
->bd_disk
) >= 0)
1981 printk(KERN_NOTICE
"disabling data integrity on %s\n", mdname(mddev
));
1982 blk_integrity_unregister(mddev
->gendisk
);
1984 EXPORT_SYMBOL(md_integrity_add_rdev
);
1986 static int bind_rdev_to_array(mdk_rdev_t
* rdev
, mddev_t
* mddev
)
1988 char b
[BDEVNAME_SIZE
];
1998 /* prevent duplicates */
1999 if (find_rdev(mddev
, rdev
->bdev
->bd_dev
))
2002 /* make sure rdev->sectors exceeds mddev->dev_sectors */
2003 if (rdev
->sectors
&& (mddev
->dev_sectors
== 0 ||
2004 rdev
->sectors
< mddev
->dev_sectors
)) {
2006 /* Cannot change size, so fail
2007 * If mddev->level <= 0, then we don't care
2008 * about aligning sizes (e.g. linear)
2010 if (mddev
->level
> 0)
2013 mddev
->dev_sectors
= rdev
->sectors
;
2016 /* Verify rdev->desc_nr is unique.
2017 * If it is -1, assign a free number, else
2018 * check number is not in use
2020 if (rdev
->desc_nr
< 0) {
2022 if (mddev
->pers
) choice
= mddev
->raid_disks
;
2023 while (find_rdev_nr(mddev
, choice
))
2025 rdev
->desc_nr
= choice
;
2027 if (find_rdev_nr(mddev
, rdev
->desc_nr
))
2030 if (mddev
->max_disks
&& rdev
->desc_nr
>= mddev
->max_disks
) {
2031 printk(KERN_WARNING
"md: %s: array is limited to %d devices\n",
2032 mdname(mddev
), mddev
->max_disks
);
2035 bdevname(rdev
->bdev
,b
);
2036 while ( (s
=strchr(b
, '/')) != NULL
)
2039 rdev
->mddev
= mddev
;
2040 printk(KERN_INFO
"md: bind<%s>\n", b
);
2042 if ((err
= kobject_add(&rdev
->kobj
, &mddev
->kobj
, "dev-%s", b
)))
2045 ko
= &part_to_dev(rdev
->bdev
->bd_part
)->kobj
;
2046 if (sysfs_create_link(&rdev
->kobj
, ko
, "block"))
2047 /* failure here is OK */;
2048 rdev
->sysfs_state
= sysfs_get_dirent_safe(rdev
->kobj
.sd
, "state");
2050 list_add_rcu(&rdev
->same_set
, &mddev
->disks
);
2051 bd_link_disk_holder(rdev
->bdev
, mddev
->gendisk
);
2053 /* May as well allow recovery to be retried once */
2054 mddev
->recovery_disabled
++;
2059 printk(KERN_WARNING
"md: failed to register dev-%s for %s\n",
2064 static void md_delayed_delete(struct work_struct
*ws
)
2066 mdk_rdev_t
*rdev
= container_of(ws
, mdk_rdev_t
, del_work
);
2067 kobject_del(&rdev
->kobj
);
2068 kobject_put(&rdev
->kobj
);
2071 static void unbind_rdev_from_array(mdk_rdev_t
* rdev
)
2073 char b
[BDEVNAME_SIZE
];
2078 bd_unlink_disk_holder(rdev
->bdev
, rdev
->mddev
->gendisk
);
2079 list_del_rcu(&rdev
->same_set
);
2080 printk(KERN_INFO
"md: unbind<%s>\n", bdevname(rdev
->bdev
,b
));
2082 sysfs_remove_link(&rdev
->kobj
, "block");
2083 sysfs_put(rdev
->sysfs_state
);
2084 rdev
->sysfs_state
= NULL
;
2085 kfree(rdev
->badblocks
.page
);
2086 rdev
->badblocks
.count
= 0;
2087 rdev
->badblocks
.page
= NULL
;
2088 /* We need to delay this, otherwise we can deadlock when
2089 * writing to 'remove' to "dev/state". We also need
2090 * to delay it due to rcu usage.
2093 INIT_WORK(&rdev
->del_work
, md_delayed_delete
);
2094 kobject_get(&rdev
->kobj
);
2095 queue_work(md_misc_wq
, &rdev
->del_work
);
2099 * prevent the device from being mounted, repartitioned or
2100 * otherwise reused by a RAID array (or any other kernel
2101 * subsystem), by bd_claiming the device.
2103 static int lock_rdev(mdk_rdev_t
*rdev
, dev_t dev
, int shared
)
2106 struct block_device
*bdev
;
2107 char b
[BDEVNAME_SIZE
];
2109 bdev
= blkdev_get_by_dev(dev
, FMODE_READ
|FMODE_WRITE
|FMODE_EXCL
,
2110 shared
? (mdk_rdev_t
*)lock_rdev
: rdev
);
2112 printk(KERN_ERR
"md: could not open %s.\n",
2113 __bdevname(dev
, b
));
2114 return PTR_ERR(bdev
);
2120 static void unlock_rdev(mdk_rdev_t
*rdev
)
2122 struct block_device
*bdev
= rdev
->bdev
;
2126 blkdev_put(bdev
, FMODE_READ
|FMODE_WRITE
|FMODE_EXCL
);
2129 void md_autodetect_dev(dev_t dev
);
2131 static void export_rdev(mdk_rdev_t
* rdev
)
2133 char b
[BDEVNAME_SIZE
];
2134 printk(KERN_INFO
"md: export_rdev(%s)\n",
2135 bdevname(rdev
->bdev
,b
));
2140 if (test_bit(AutoDetected
, &rdev
->flags
))
2141 md_autodetect_dev(rdev
->bdev
->bd_dev
);
2144 kobject_put(&rdev
->kobj
);
2147 static void kick_rdev_from_array(mdk_rdev_t
* rdev
)
2149 unbind_rdev_from_array(rdev
);
2153 static void export_array(mddev_t
*mddev
)
2155 mdk_rdev_t
*rdev
, *tmp
;
2157 rdev_for_each(rdev
, tmp
, mddev
) {
2162 kick_rdev_from_array(rdev
);
2164 if (!list_empty(&mddev
->disks
))
2166 mddev
->raid_disks
= 0;
2167 mddev
->major_version
= 0;
2170 static void print_desc(mdp_disk_t
*desc
)
2172 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc
->number
,
2173 desc
->major
,desc
->minor
,desc
->raid_disk
,desc
->state
);
2176 static void print_sb_90(mdp_super_t
*sb
)
2181 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
2182 sb
->major_version
, sb
->minor_version
, sb
->patch_version
,
2183 sb
->set_uuid0
, sb
->set_uuid1
, sb
->set_uuid2
, sb
->set_uuid3
,
2185 printk(KERN_INFO
"md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
2186 sb
->level
, sb
->size
, sb
->nr_disks
, sb
->raid_disks
,
2187 sb
->md_minor
, sb
->layout
, sb
->chunk_size
);
2188 printk(KERN_INFO
"md: UT:%08x ST:%d AD:%d WD:%d"
2189 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
2190 sb
->utime
, sb
->state
, sb
->active_disks
, sb
->working_disks
,
2191 sb
->failed_disks
, sb
->spare_disks
,
2192 sb
->sb_csum
, (unsigned long)sb
->events_lo
);
2195 for (i
= 0; i
< MD_SB_DISKS
; i
++) {
2198 desc
= sb
->disks
+ i
;
2199 if (desc
->number
|| desc
->major
|| desc
->minor
||
2200 desc
->raid_disk
|| (desc
->state
&& (desc
->state
!= 4))) {
2201 printk(" D %2d: ", i
);
2205 printk(KERN_INFO
"md: THIS: ");
2206 print_desc(&sb
->this_disk
);
2209 static void print_sb_1(struct mdp_superblock_1
*sb
)
2213 uuid
= sb
->set_uuid
;
2215 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n"
2216 "md: Name: \"%s\" CT:%llu\n",
2217 le32_to_cpu(sb
->major_version
),
2218 le32_to_cpu(sb
->feature_map
),
2221 (unsigned long long)le64_to_cpu(sb
->ctime
)
2222 & MD_SUPERBLOCK_1_TIME_SEC_MASK
);
2224 uuid
= sb
->device_uuid
;
2226 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
2228 "md: Dev:%08x UUID: %pU\n"
2229 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
2230 "md: (MaxDev:%u) \n",
2231 le32_to_cpu(sb
->level
),
2232 (unsigned long long)le64_to_cpu(sb
->size
),
2233 le32_to_cpu(sb
->raid_disks
),
2234 le32_to_cpu(sb
->layout
),
2235 le32_to_cpu(sb
->chunksize
),
2236 (unsigned long long)le64_to_cpu(sb
->data_offset
),
2237 (unsigned long long)le64_to_cpu(sb
->data_size
),
2238 (unsigned long long)le64_to_cpu(sb
->super_offset
),
2239 (unsigned long long)le64_to_cpu(sb
->recovery_offset
),
2240 le32_to_cpu(sb
->dev_number
),
2243 (unsigned long long)le64_to_cpu(sb
->utime
) & MD_SUPERBLOCK_1_TIME_SEC_MASK
,
2244 (unsigned long long)le64_to_cpu(sb
->events
),
2245 (unsigned long long)le64_to_cpu(sb
->resync_offset
),
2246 le32_to_cpu(sb
->sb_csum
),
2247 le32_to_cpu(sb
->max_dev
)
2251 static void print_rdev(mdk_rdev_t
*rdev
, int major_version
)
2253 char b
[BDEVNAME_SIZE
];
2254 printk(KERN_INFO
"md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
2255 bdevname(rdev
->bdev
, b
), (unsigned long long)rdev
->sectors
,
2256 test_bit(Faulty
, &rdev
->flags
), test_bit(In_sync
, &rdev
->flags
),
2258 if (rdev
->sb_loaded
) {
2259 printk(KERN_INFO
"md: rdev superblock (MJ:%d):\n", major_version
);
2260 switch (major_version
) {
2262 print_sb_90(page_address(rdev
->sb_page
));
2265 print_sb_1(page_address(rdev
->sb_page
));
2269 printk(KERN_INFO
"md: no rdev superblock!\n");
2272 static void md_print_devices(void)
2274 struct list_head
*tmp
;
2277 char b
[BDEVNAME_SIZE
];
2280 printk("md: **********************************\n");
2281 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
2282 printk("md: **********************************\n");
2283 for_each_mddev(mddev
, tmp
) {
2286 bitmap_print_sb(mddev
->bitmap
);
2288 printk("%s: ", mdname(mddev
));
2289 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
2290 printk("<%s>", bdevname(rdev
->bdev
,b
));
2293 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
2294 print_rdev(rdev
, mddev
->major_version
);
2296 printk("md: **********************************\n");
2301 static void sync_sbs(mddev_t
* mddev
, int nospares
)
2303 /* Update each superblock (in-memory image), but
2304 * if we are allowed to, skip spares which already
2305 * have the right event counter, or have one earlier
2306 * (which would mean they aren't being marked as dirty
2307 * with the rest of the array)
2310 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
2311 if (rdev
->sb_events
== mddev
->events
||
2313 rdev
->raid_disk
< 0 &&
2314 rdev
->sb_events
+1 == mddev
->events
)) {
2315 /* Don't update this superblock */
2316 rdev
->sb_loaded
= 2;
2318 sync_super(mddev
, rdev
);
2319 rdev
->sb_loaded
= 1;
2324 static void md_update_sb(mddev_t
* mddev
, int force_change
)
2329 int any_badblocks_changed
= 0;
2332 /* First make sure individual recovery_offsets are correct */
2333 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
2334 if (rdev
->raid_disk
>= 0 &&
2335 mddev
->delta_disks
>= 0 &&
2336 !test_bit(In_sync
, &rdev
->flags
) &&
2337 mddev
->curr_resync_completed
> rdev
->recovery_offset
)
2338 rdev
->recovery_offset
= mddev
->curr_resync_completed
;
2341 if (!mddev
->persistent
) {
2342 clear_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
2343 clear_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
2344 if (!mddev
->external
) {
2345 clear_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
2346 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
2347 if (rdev
->badblocks
.changed
) {
2348 md_ack_all_badblocks(&rdev
->badblocks
);
2349 md_error(mddev
, rdev
);
2351 clear_bit(Blocked
, &rdev
->flags
);
2352 clear_bit(BlockedBadBlocks
, &rdev
->flags
);
2353 wake_up(&rdev
->blocked_wait
);
2356 wake_up(&mddev
->sb_wait
);
2360 spin_lock_irq(&mddev
->write_lock
);
2362 mddev
->utime
= get_seconds();
2364 if (test_and_clear_bit(MD_CHANGE_DEVS
, &mddev
->flags
))
2366 if (test_and_clear_bit(MD_CHANGE_CLEAN
, &mddev
->flags
))
2367 /* just a clean<-> dirty transition, possibly leave spares alone,
2368 * though if events isn't the right even/odd, we will have to do
2374 if (mddev
->degraded
)
2375 /* If the array is degraded, then skipping spares is both
2376 * dangerous and fairly pointless.
2377 * Dangerous because a device that was removed from the array
2378 * might have a event_count that still looks up-to-date,
2379 * so it can be re-added without a resync.
2380 * Pointless because if there are any spares to skip,
2381 * then a recovery will happen and soon that array won't
2382 * be degraded any more and the spare can go back to sleep then.
2386 sync_req
= mddev
->in_sync
;
2388 /* If this is just a dirty<->clean transition, and the array is clean
2389 * and 'events' is odd, we can roll back to the previous clean state */
2391 && (mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
)
2392 && mddev
->can_decrease_events
2393 && mddev
->events
!= 1) {
2395 mddev
->can_decrease_events
= 0;
2397 /* otherwise we have to go forward and ... */
2399 mddev
->can_decrease_events
= nospares
;
2402 if (!mddev
->events
) {
2404 * oops, this 64-bit counter should never wrap.
2405 * Either we are in around ~1 trillion A.C., assuming
2406 * 1 reboot per second, or we have a bug:
2412 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
2413 if (rdev
->badblocks
.changed
)
2414 any_badblocks_changed
++;
2415 if (test_bit(Faulty
, &rdev
->flags
))
2416 set_bit(FaultRecorded
, &rdev
->flags
);
2419 sync_sbs(mddev
, nospares
);
2420 spin_unlock_irq(&mddev
->write_lock
);
2423 "md: updating %s RAID superblock on device (in sync %d)\n",
2424 mdname(mddev
),mddev
->in_sync
);
2426 bitmap_update_sb(mddev
->bitmap
);
2427 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
2428 char b
[BDEVNAME_SIZE
];
2429 dprintk(KERN_INFO
"md: ");
2430 if (rdev
->sb_loaded
!= 1)
2431 continue; /* no noise on spare devices */
2432 if (test_bit(Faulty
, &rdev
->flags
))
2433 dprintk("(skipping faulty ");
2435 dprintk("%s ", bdevname(rdev
->bdev
,b
));
2436 if (!test_bit(Faulty
, &rdev
->flags
)) {
2437 md_super_write(mddev
,rdev
,
2438 rdev
->sb_start
, rdev
->sb_size
,
2440 dprintk(KERN_INFO
"(write) %s's sb offset: %llu\n",
2441 bdevname(rdev
->bdev
,b
),
2442 (unsigned long long)rdev
->sb_start
);
2443 rdev
->sb_events
= mddev
->events
;
2444 if (rdev
->badblocks
.size
) {
2445 md_super_write(mddev
, rdev
,
2446 rdev
->badblocks
.sector
,
2447 rdev
->badblocks
.size
<< 9,
2449 rdev
->badblocks
.size
= 0;
2454 if (mddev
->level
== LEVEL_MULTIPATH
)
2455 /* only need to write one superblock... */
2458 md_super_wait(mddev
);
2459 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
2461 spin_lock_irq(&mddev
->write_lock
);
2462 if (mddev
->in_sync
!= sync_req
||
2463 test_bit(MD_CHANGE_DEVS
, &mddev
->flags
)) {
2464 /* have to write it out again */
2465 spin_unlock_irq(&mddev
->write_lock
);
2468 clear_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
2469 spin_unlock_irq(&mddev
->write_lock
);
2470 wake_up(&mddev
->sb_wait
);
2471 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
2472 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
2474 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
2475 if (test_and_clear_bit(FaultRecorded
, &rdev
->flags
))
2476 clear_bit(Blocked
, &rdev
->flags
);
2478 if (any_badblocks_changed
)
2479 md_ack_all_badblocks(&rdev
->badblocks
);
2480 clear_bit(BlockedBadBlocks
, &rdev
->flags
);
2481 wake_up(&rdev
->blocked_wait
);
2485 /* words written to sysfs files may, or may not, be \n terminated.
2486 * We want to accept with case. For this we use cmd_match.
2488 static int cmd_match(const char *cmd
, const char *str
)
2490 /* See if cmd, written into a sysfs file, matches
2491 * str. They must either be the same, or cmd can
2492 * have a trailing newline
2494 while (*cmd
&& *str
&& *cmd
== *str
) {
2505 struct rdev_sysfs_entry
{
2506 struct attribute attr
;
2507 ssize_t (*show
)(mdk_rdev_t
*, char *);
2508 ssize_t (*store
)(mdk_rdev_t
*, const char *, size_t);
2512 state_show(mdk_rdev_t
*rdev
, char *page
)
2517 if (test_bit(Faulty
, &rdev
->flags
) ||
2518 rdev
->badblocks
.unacked_exist
) {
2519 len
+= sprintf(page
+len
, "%sfaulty",sep
);
2522 if (test_bit(In_sync
, &rdev
->flags
)) {
2523 len
+= sprintf(page
+len
, "%sin_sync",sep
);
2526 if (test_bit(WriteMostly
, &rdev
->flags
)) {
2527 len
+= sprintf(page
+len
, "%swrite_mostly",sep
);
2530 if (test_bit(Blocked
, &rdev
->flags
) ||
2531 rdev
->badblocks
.unacked_exist
) {
2532 len
+= sprintf(page
+len
, "%sblocked", sep
);
2535 if (!test_bit(Faulty
, &rdev
->flags
) &&
2536 !test_bit(In_sync
, &rdev
->flags
)) {
2537 len
+= sprintf(page
+len
, "%sspare", sep
);
2540 if (test_bit(WriteErrorSeen
, &rdev
->flags
)) {
2541 len
+= sprintf(page
+len
, "%swrite_error", sep
);
2544 return len
+sprintf(page
+len
, "\n");
2548 state_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2551 * faulty - simulates an error
2552 * remove - disconnects the device
2553 * writemostly - sets write_mostly
2554 * -writemostly - clears write_mostly
2555 * blocked - sets the Blocked flags
2556 * -blocked - clears the Blocked and possibly simulates an error
2557 * insync - sets Insync providing device isn't active
2558 * write_error - sets WriteErrorSeen
2559 * -write_error - clears WriteErrorSeen
2562 if (cmd_match(buf
, "faulty") && rdev
->mddev
->pers
) {
2563 md_error(rdev
->mddev
, rdev
);
2565 } else if (cmd_match(buf
, "remove")) {
2566 if (rdev
->raid_disk
>= 0)
2569 mddev_t
*mddev
= rdev
->mddev
;
2570 kick_rdev_from_array(rdev
);
2572 md_update_sb(mddev
, 1);
2573 md_new_event(mddev
);
2576 } else if (cmd_match(buf
, "writemostly")) {
2577 set_bit(WriteMostly
, &rdev
->flags
);
2579 } else if (cmd_match(buf
, "-writemostly")) {
2580 clear_bit(WriteMostly
, &rdev
->flags
);
2582 } else if (cmd_match(buf
, "blocked")) {
2583 set_bit(Blocked
, &rdev
->flags
);
2585 } else if (cmd_match(buf
, "-blocked")) {
2586 if (!test_bit(Faulty
, &rdev
->flags
) &&
2587 test_bit(BlockedBadBlocks
, &rdev
->flags
)) {
2588 /* metadata handler doesn't understand badblocks,
2589 * so we need to fail the device
2591 md_error(rdev
->mddev
, rdev
);
2593 clear_bit(Blocked
, &rdev
->flags
);
2594 clear_bit(BlockedBadBlocks
, &rdev
->flags
);
2595 wake_up(&rdev
->blocked_wait
);
2596 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
2597 md_wakeup_thread(rdev
->mddev
->thread
);
2600 } else if (cmd_match(buf
, "insync") && rdev
->raid_disk
== -1) {
2601 set_bit(In_sync
, &rdev
->flags
);
2603 } else if (cmd_match(buf
, "write_error")) {
2604 set_bit(WriteErrorSeen
, &rdev
->flags
);
2606 } else if (cmd_match(buf
, "-write_error")) {
2607 clear_bit(WriteErrorSeen
, &rdev
->flags
);
2611 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
2612 return err
? err
: len
;
2614 static struct rdev_sysfs_entry rdev_state
=
2615 __ATTR(state
, S_IRUGO
|S_IWUSR
, state_show
, state_store
);
2618 errors_show(mdk_rdev_t
*rdev
, char *page
)
2620 return sprintf(page
, "%d\n", atomic_read(&rdev
->corrected_errors
));
2624 errors_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2627 unsigned long n
= simple_strtoul(buf
, &e
, 10);
2628 if (*buf
&& (*e
== 0 || *e
== '\n')) {
2629 atomic_set(&rdev
->corrected_errors
, n
);
2634 static struct rdev_sysfs_entry rdev_errors
=
2635 __ATTR(errors
, S_IRUGO
|S_IWUSR
, errors_show
, errors_store
);
2638 slot_show(mdk_rdev_t
*rdev
, char *page
)
2640 if (rdev
->raid_disk
< 0)
2641 return sprintf(page
, "none\n");
2643 return sprintf(page
, "%d\n", rdev
->raid_disk
);
2647 slot_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2651 int slot
= simple_strtoul(buf
, &e
, 10);
2652 if (strncmp(buf
, "none", 4)==0)
2654 else if (e
==buf
|| (*e
&& *e
!= '\n'))
2656 if (rdev
->mddev
->pers
&& slot
== -1) {
2657 /* Setting 'slot' on an active array requires also
2658 * updating the 'rd%d' link, and communicating
2659 * with the personality with ->hot_*_disk.
2660 * For now we only support removing
2661 * failed/spare devices. This normally happens automatically,
2662 * but not when the metadata is externally managed.
2664 if (rdev
->raid_disk
== -1)
2666 /* personality does all needed checks */
2667 if (rdev
->mddev
->pers
->hot_remove_disk
== NULL
)
2669 err
= rdev
->mddev
->pers
->
2670 hot_remove_disk(rdev
->mddev
, rdev
->raid_disk
);
2673 sysfs_unlink_rdev(rdev
->mddev
, rdev
);
2674 rdev
->raid_disk
= -1;
2675 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
2676 md_wakeup_thread(rdev
->mddev
->thread
);
2677 } else if (rdev
->mddev
->pers
) {
2679 /* Activating a spare .. or possibly reactivating
2680 * if we ever get bitmaps working here.
2683 if (rdev
->raid_disk
!= -1)
2686 if (test_bit(MD_RECOVERY_RUNNING
, &rdev
->mddev
->recovery
))
2689 if (rdev
->mddev
->pers
->hot_add_disk
== NULL
)
2692 list_for_each_entry(rdev2
, &rdev
->mddev
->disks
, same_set
)
2693 if (rdev2
->raid_disk
== slot
)
2696 if (slot
>= rdev
->mddev
->raid_disks
&&
2697 slot
>= rdev
->mddev
->raid_disks
+ rdev
->mddev
->delta_disks
)
2700 rdev
->raid_disk
= slot
;
2701 if (test_bit(In_sync
, &rdev
->flags
))
2702 rdev
->saved_raid_disk
= slot
;
2704 rdev
->saved_raid_disk
= -1;
2705 err
= rdev
->mddev
->pers
->
2706 hot_add_disk(rdev
->mddev
, rdev
);
2708 rdev
->raid_disk
= -1;
2711 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
2712 if (sysfs_link_rdev(rdev
->mddev
, rdev
))
2713 /* failure here is OK */;
2714 /* don't wakeup anyone, leave that to userspace. */
2716 if (slot
>= rdev
->mddev
->raid_disks
&&
2717 slot
>= rdev
->mddev
->raid_disks
+ rdev
->mddev
->delta_disks
)
2719 rdev
->raid_disk
= slot
;
2720 /* assume it is working */
2721 clear_bit(Faulty
, &rdev
->flags
);
2722 clear_bit(WriteMostly
, &rdev
->flags
);
2723 set_bit(In_sync
, &rdev
->flags
);
2724 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
2730 static struct rdev_sysfs_entry rdev_slot
=
2731 __ATTR(slot
, S_IRUGO
|S_IWUSR
, slot_show
, slot_store
);
2734 offset_show(mdk_rdev_t
*rdev
, char *page
)
2736 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->data_offset
);
2740 offset_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2743 unsigned long long offset
= simple_strtoull(buf
, &e
, 10);
2744 if (e
==buf
|| (*e
&& *e
!= '\n'))
2746 if (rdev
->mddev
->pers
&& rdev
->raid_disk
>= 0)
2748 if (rdev
->sectors
&& rdev
->mddev
->external
)
2749 /* Must set offset before size, so overlap checks
2752 rdev
->data_offset
= offset
;
2756 static struct rdev_sysfs_entry rdev_offset
=
2757 __ATTR(offset
, S_IRUGO
|S_IWUSR
, offset_show
, offset_store
);
2760 rdev_size_show(mdk_rdev_t
*rdev
, char *page
)
2762 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->sectors
/ 2);
2765 static int overlaps(sector_t s1
, sector_t l1
, sector_t s2
, sector_t l2
)
2767 /* check if two start/length pairs overlap */
2775 static int strict_blocks_to_sectors(const char *buf
, sector_t
*sectors
)
2777 unsigned long long blocks
;
2780 if (strict_strtoull(buf
, 10, &blocks
) < 0)
2783 if (blocks
& 1ULL << (8 * sizeof(blocks
) - 1))
2784 return -EINVAL
; /* sector conversion overflow */
2787 if (new != blocks
* 2)
2788 return -EINVAL
; /* unsigned long long to sector_t overflow */
2795 rdev_size_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2797 mddev_t
*my_mddev
= rdev
->mddev
;
2798 sector_t oldsectors
= rdev
->sectors
;
2801 if (strict_blocks_to_sectors(buf
, §ors
) < 0)
2803 if (my_mddev
->pers
&& rdev
->raid_disk
>= 0) {
2804 if (my_mddev
->persistent
) {
2805 sectors
= super_types
[my_mddev
->major_version
].
2806 rdev_size_change(rdev
, sectors
);
2809 } else if (!sectors
)
2810 sectors
= (i_size_read(rdev
->bdev
->bd_inode
) >> 9) -
2813 if (sectors
< my_mddev
->dev_sectors
)
2814 return -EINVAL
; /* component must fit device */
2816 rdev
->sectors
= sectors
;
2817 if (sectors
> oldsectors
&& my_mddev
->external
) {
2818 /* need to check that all other rdevs with the same ->bdev
2819 * do not overlap. We need to unlock the mddev to avoid
2820 * a deadlock. We have already changed rdev->sectors, and if
2821 * we have to change it back, we will have the lock again.
2825 struct list_head
*tmp
;
2827 mddev_unlock(my_mddev
);
2828 for_each_mddev(mddev
, tmp
) {
2832 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
)
2833 if (rdev
->bdev
== rdev2
->bdev
&&
2835 overlaps(rdev
->data_offset
, rdev
->sectors
,
2841 mddev_unlock(mddev
);
2847 mddev_lock(my_mddev
);
2849 /* Someone else could have slipped in a size
2850 * change here, but doing so is just silly.
2851 * We put oldsectors back because we *know* it is
2852 * safe, and trust userspace not to race with
2855 rdev
->sectors
= oldsectors
;
2862 static struct rdev_sysfs_entry rdev_size
=
2863 __ATTR(size
, S_IRUGO
|S_IWUSR
, rdev_size_show
, rdev_size_store
);
2866 static ssize_t
recovery_start_show(mdk_rdev_t
*rdev
, char *page
)
2868 unsigned long long recovery_start
= rdev
->recovery_offset
;
2870 if (test_bit(In_sync
, &rdev
->flags
) ||
2871 recovery_start
== MaxSector
)
2872 return sprintf(page
, "none\n");
2874 return sprintf(page
, "%llu\n", recovery_start
);
2877 static ssize_t
recovery_start_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2879 unsigned long long recovery_start
;
2881 if (cmd_match(buf
, "none"))
2882 recovery_start
= MaxSector
;
2883 else if (strict_strtoull(buf
, 10, &recovery_start
))
2886 if (rdev
->mddev
->pers
&&
2887 rdev
->raid_disk
>= 0)
2890 rdev
->recovery_offset
= recovery_start
;
2891 if (recovery_start
== MaxSector
)
2892 set_bit(In_sync
, &rdev
->flags
);
2894 clear_bit(In_sync
, &rdev
->flags
);
2898 static struct rdev_sysfs_entry rdev_recovery_start
=
2899 __ATTR(recovery_start
, S_IRUGO
|S_IWUSR
, recovery_start_show
, recovery_start_store
);
2903 badblocks_show(struct badblocks
*bb
, char *page
, int unack
);
2905 badblocks_store(struct badblocks
*bb
, const char *page
, size_t len
, int unack
);
2907 static ssize_t
bb_show(mdk_rdev_t
*rdev
, char *page
)
2909 return badblocks_show(&rdev
->badblocks
, page
, 0);
2911 static ssize_t
bb_store(mdk_rdev_t
*rdev
, const char *page
, size_t len
)
2913 int rv
= badblocks_store(&rdev
->badblocks
, page
, len
, 0);
2914 /* Maybe that ack was all we needed */
2915 if (test_and_clear_bit(BlockedBadBlocks
, &rdev
->flags
))
2916 wake_up(&rdev
->blocked_wait
);
2919 static struct rdev_sysfs_entry rdev_bad_blocks
=
2920 __ATTR(bad_blocks
, S_IRUGO
|S_IWUSR
, bb_show
, bb_store
);
2923 static ssize_t
ubb_show(mdk_rdev_t
*rdev
, char *page
)
2925 return badblocks_show(&rdev
->badblocks
, page
, 1);
2927 static ssize_t
ubb_store(mdk_rdev_t
*rdev
, const char *page
, size_t len
)
2929 return badblocks_store(&rdev
->badblocks
, page
, len
, 1);
2931 static struct rdev_sysfs_entry rdev_unack_bad_blocks
=
2932 __ATTR(unacknowledged_bad_blocks
, S_IRUGO
|S_IWUSR
, ubb_show
, ubb_store
);
2934 static struct attribute
*rdev_default_attrs
[] = {
2940 &rdev_recovery_start
.attr
,
2941 &rdev_bad_blocks
.attr
,
2942 &rdev_unack_bad_blocks
.attr
,
2946 rdev_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
2948 struct rdev_sysfs_entry
*entry
= container_of(attr
, struct rdev_sysfs_entry
, attr
);
2949 mdk_rdev_t
*rdev
= container_of(kobj
, mdk_rdev_t
, kobj
);
2950 mddev_t
*mddev
= rdev
->mddev
;
2956 rv
= mddev
? mddev_lock(mddev
) : -EBUSY
;
2958 if (rdev
->mddev
== NULL
)
2961 rv
= entry
->show(rdev
, page
);
2962 mddev_unlock(mddev
);
2968 rdev_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
2969 const char *page
, size_t length
)
2971 struct rdev_sysfs_entry
*entry
= container_of(attr
, struct rdev_sysfs_entry
, attr
);
2972 mdk_rdev_t
*rdev
= container_of(kobj
, mdk_rdev_t
, kobj
);
2974 mddev_t
*mddev
= rdev
->mddev
;
2978 if (!capable(CAP_SYS_ADMIN
))
2980 rv
= mddev
? mddev_lock(mddev
): -EBUSY
;
2982 if (rdev
->mddev
== NULL
)
2985 rv
= entry
->store(rdev
, page
, length
);
2986 mddev_unlock(mddev
);
2991 static void rdev_free(struct kobject
*ko
)
2993 mdk_rdev_t
*rdev
= container_of(ko
, mdk_rdev_t
, kobj
);
2996 static const struct sysfs_ops rdev_sysfs_ops
= {
2997 .show
= rdev_attr_show
,
2998 .store
= rdev_attr_store
,
3000 static struct kobj_type rdev_ktype
= {
3001 .release
= rdev_free
,
3002 .sysfs_ops
= &rdev_sysfs_ops
,
3003 .default_attrs
= rdev_default_attrs
,
3006 int md_rdev_init(mdk_rdev_t
*rdev
)
3009 rdev
->saved_raid_disk
= -1;
3010 rdev
->raid_disk
= -1;
3012 rdev
->data_offset
= 0;
3013 rdev
->sb_events
= 0;
3014 rdev
->last_read_error
.tv_sec
= 0;
3015 rdev
->last_read_error
.tv_nsec
= 0;
3016 rdev
->sb_loaded
= 0;
3017 rdev
->bb_page
= NULL
;
3018 atomic_set(&rdev
->nr_pending
, 0);
3019 atomic_set(&rdev
->read_errors
, 0);
3020 atomic_set(&rdev
->corrected_errors
, 0);
3022 INIT_LIST_HEAD(&rdev
->same_set
);
3023 init_waitqueue_head(&rdev
->blocked_wait
);
3025 /* Add space to store bad block list.
3026 * This reserves the space even on arrays where it cannot
3027 * be used - I wonder if that matters
3029 rdev
->badblocks
.count
= 0;
3030 rdev
->badblocks
.shift
= 0;
3031 rdev
->badblocks
.page
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
3032 seqlock_init(&rdev
->badblocks
.lock
);
3033 if (rdev
->badblocks
.page
== NULL
)
3038 EXPORT_SYMBOL_GPL(md_rdev_init
);
3040 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3042 * mark the device faulty if:
3044 * - the device is nonexistent (zero size)
3045 * - the device has no valid superblock
3047 * a faulty rdev _never_ has rdev->sb set.
3049 static mdk_rdev_t
*md_import_device(dev_t newdev
, int super_format
, int super_minor
)
3051 char b
[BDEVNAME_SIZE
];
3056 rdev
= kzalloc(sizeof(*rdev
), GFP_KERNEL
);
3058 printk(KERN_ERR
"md: could not alloc mem for new device!\n");
3059 return ERR_PTR(-ENOMEM
);
3062 err
= md_rdev_init(rdev
);
3065 err
= alloc_disk_sb(rdev
);
3069 err
= lock_rdev(rdev
, newdev
, super_format
== -2);
3073 kobject_init(&rdev
->kobj
, &rdev_ktype
);
3075 size
= i_size_read(rdev
->bdev
->bd_inode
) >> BLOCK_SIZE_BITS
;
3078 "md: %s has zero or unknown size, marking faulty!\n",
3079 bdevname(rdev
->bdev
,b
));
3084 if (super_format
>= 0) {
3085 err
= super_types
[super_format
].
3086 load_super(rdev
, NULL
, super_minor
);
3087 if (err
== -EINVAL
) {
3089 "md: %s does not have a valid v%d.%d "
3090 "superblock, not importing!\n",
3091 bdevname(rdev
->bdev
,b
),
3092 super_format
, super_minor
);
3097 "md: could not read %s's sb, not importing!\n",
3098 bdevname(rdev
->bdev
,b
));
3102 if (super_format
== -1)
3103 /* hot-add for 0.90, or non-persistent: so no badblocks */
3104 rdev
->badblocks
.shift
= -1;
3112 kfree(rdev
->badblocks
.page
);
3114 return ERR_PTR(err
);
3118 * Check a full RAID array for plausibility
3122 static void analyze_sbs(mddev_t
* mddev
)
3125 mdk_rdev_t
*rdev
, *freshest
, *tmp
;
3126 char b
[BDEVNAME_SIZE
];
3129 rdev_for_each(rdev
, tmp
, mddev
)
3130 switch (super_types
[mddev
->major_version
].
3131 load_super(rdev
, freshest
, mddev
->minor_version
)) {
3139 "md: fatal superblock inconsistency in %s"
3140 " -- removing from array\n",
3141 bdevname(rdev
->bdev
,b
));
3142 kick_rdev_from_array(rdev
);
3146 super_types
[mddev
->major_version
].
3147 validate_super(mddev
, freshest
);
3150 rdev_for_each(rdev
, tmp
, mddev
) {
3151 if (mddev
->max_disks
&&
3152 (rdev
->desc_nr
>= mddev
->max_disks
||
3153 i
> mddev
->max_disks
)) {
3155 "md: %s: %s: only %d devices permitted\n",
3156 mdname(mddev
), bdevname(rdev
->bdev
, b
),
3158 kick_rdev_from_array(rdev
);
3161 if (rdev
!= freshest
)
3162 if (super_types
[mddev
->major_version
].
3163 validate_super(mddev
, rdev
)) {
3164 printk(KERN_WARNING
"md: kicking non-fresh %s"
3166 bdevname(rdev
->bdev
,b
));
3167 kick_rdev_from_array(rdev
);
3170 if (mddev
->level
== LEVEL_MULTIPATH
) {
3171 rdev
->desc_nr
= i
++;
3172 rdev
->raid_disk
= rdev
->desc_nr
;
3173 set_bit(In_sync
, &rdev
->flags
);
3174 } else if (rdev
->raid_disk
>= (mddev
->raid_disks
- min(0, mddev
->delta_disks
))) {
3175 rdev
->raid_disk
= -1;
3176 clear_bit(In_sync
, &rdev
->flags
);
3181 /* Read a fixed-point number.
3182 * Numbers in sysfs attributes should be in "standard" units where
3183 * possible, so time should be in seconds.
3184 * However we internally use a a much smaller unit such as
3185 * milliseconds or jiffies.
3186 * This function takes a decimal number with a possible fractional
3187 * component, and produces an integer which is the result of
3188 * multiplying that number by 10^'scale'.
3189 * all without any floating-point arithmetic.
3191 int strict_strtoul_scaled(const char *cp
, unsigned long *res
, int scale
)
3193 unsigned long result
= 0;
3195 while (isdigit(*cp
) || (*cp
== '.' && decimals
< 0)) {
3198 else if (decimals
< scale
) {
3201 result
= result
* 10 + value
;
3213 while (decimals
< scale
) {
3222 static void md_safemode_timeout(unsigned long data
);
3225 safe_delay_show(mddev_t
*mddev
, char *page
)
3227 int msec
= (mddev
->safemode_delay
*1000)/HZ
;
3228 return sprintf(page
, "%d.%03d\n", msec
/1000, msec
%1000);
3231 safe_delay_store(mddev_t
*mddev
, const char *cbuf
, size_t len
)
3235 if (strict_strtoul_scaled(cbuf
, &msec
, 3) < 0)
3238 mddev
->safemode_delay
= 0;
3240 unsigned long old_delay
= mddev
->safemode_delay
;
3241 mddev
->safemode_delay
= (msec
*HZ
)/1000;
3242 if (mddev
->safemode_delay
== 0)
3243 mddev
->safemode_delay
= 1;
3244 if (mddev
->safemode_delay
< old_delay
)
3245 md_safemode_timeout((unsigned long)mddev
);
3249 static struct md_sysfs_entry md_safe_delay
=
3250 __ATTR(safe_mode_delay
, S_IRUGO
|S_IWUSR
,safe_delay_show
, safe_delay_store
);
3253 level_show(mddev_t
*mddev
, char *page
)
3255 struct mdk_personality
*p
= mddev
->pers
;
3257 return sprintf(page
, "%s\n", p
->name
);
3258 else if (mddev
->clevel
[0])
3259 return sprintf(page
, "%s\n", mddev
->clevel
);
3260 else if (mddev
->level
!= LEVEL_NONE
)
3261 return sprintf(page
, "%d\n", mddev
->level
);
3267 level_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3271 struct mdk_personality
*pers
;
3276 if (mddev
->pers
== NULL
) {
3279 if (len
>= sizeof(mddev
->clevel
))
3281 strncpy(mddev
->clevel
, buf
, len
);
3282 if (mddev
->clevel
[len
-1] == '\n')
3284 mddev
->clevel
[len
] = 0;
3285 mddev
->level
= LEVEL_NONE
;
3289 /* request to change the personality. Need to ensure:
3290 * - array is not engaged in resync/recovery/reshape
3291 * - old personality can be suspended
3292 * - new personality will access other array.
3295 if (mddev
->sync_thread
||
3296 mddev
->reshape_position
!= MaxSector
||
3297 mddev
->sysfs_active
)
3300 if (!mddev
->pers
->quiesce
) {
3301 printk(KERN_WARNING
"md: %s: %s does not support online personality change\n",
3302 mdname(mddev
), mddev
->pers
->name
);
3306 /* Now find the new personality */
3307 if (len
== 0 || len
>= sizeof(clevel
))
3309 strncpy(clevel
, buf
, len
);
3310 if (clevel
[len
-1] == '\n')
3313 if (strict_strtol(clevel
, 10, &level
))
3316 if (request_module("md-%s", clevel
) != 0)
3317 request_module("md-level-%s", clevel
);
3318 spin_lock(&pers_lock
);
3319 pers
= find_pers(level
, clevel
);
3320 if (!pers
|| !try_module_get(pers
->owner
)) {
3321 spin_unlock(&pers_lock
);
3322 printk(KERN_WARNING
"md: personality %s not loaded\n", clevel
);
3325 spin_unlock(&pers_lock
);
3327 if (pers
== mddev
->pers
) {
3328 /* Nothing to do! */
3329 module_put(pers
->owner
);
3332 if (!pers
->takeover
) {
3333 module_put(pers
->owner
);
3334 printk(KERN_WARNING
"md: %s: %s does not support personality takeover\n",
3335 mdname(mddev
), clevel
);
3339 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
3340 rdev
->new_raid_disk
= rdev
->raid_disk
;
3342 /* ->takeover must set new_* and/or delta_disks
3343 * if it succeeds, and may set them when it fails.
3345 priv
= pers
->takeover(mddev
);
3347 mddev
->new_level
= mddev
->level
;
3348 mddev
->new_layout
= mddev
->layout
;
3349 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
3350 mddev
->raid_disks
-= mddev
->delta_disks
;
3351 mddev
->delta_disks
= 0;
3352 module_put(pers
->owner
);
3353 printk(KERN_WARNING
"md: %s: %s would not accept array\n",
3354 mdname(mddev
), clevel
);
3355 return PTR_ERR(priv
);
3358 /* Looks like we have a winner */
3359 mddev_suspend(mddev
);
3360 mddev
->pers
->stop(mddev
);
3362 if (mddev
->pers
->sync_request
== NULL
&&
3363 pers
->sync_request
!= NULL
) {
3364 /* need to add the md_redundancy_group */
3365 if (sysfs_create_group(&mddev
->kobj
, &md_redundancy_group
))
3367 "md: cannot register extra attributes for %s\n",
3369 mddev
->sysfs_action
= sysfs_get_dirent(mddev
->kobj
.sd
, NULL
, "sync_action");
3371 if (mddev
->pers
->sync_request
!= NULL
&&
3372 pers
->sync_request
== NULL
) {
3373 /* need to remove the md_redundancy_group */
3374 if (mddev
->to_remove
== NULL
)
3375 mddev
->to_remove
= &md_redundancy_group
;
3378 if (mddev
->pers
->sync_request
== NULL
&&
3380 /* We are converting from a no-redundancy array
3381 * to a redundancy array and metadata is managed
3382 * externally so we need to be sure that writes
3383 * won't block due to a need to transition
3385 * until external management is started.
3388 mddev
->safemode_delay
= 0;
3389 mddev
->safemode
= 0;
3392 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
3393 if (rdev
->raid_disk
< 0)
3395 if (rdev
->new_raid_disk
>= mddev
->raid_disks
)
3396 rdev
->new_raid_disk
= -1;
3397 if (rdev
->new_raid_disk
== rdev
->raid_disk
)
3399 sysfs_unlink_rdev(mddev
, rdev
);
3401 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
3402 if (rdev
->raid_disk
< 0)
3404 if (rdev
->new_raid_disk
== rdev
->raid_disk
)
3406 rdev
->raid_disk
= rdev
->new_raid_disk
;
3407 if (rdev
->raid_disk
< 0)
3408 clear_bit(In_sync
, &rdev
->flags
);
3410 if (sysfs_link_rdev(mddev
, rdev
))
3411 printk(KERN_WARNING
"md: cannot register rd%d"
3412 " for %s after level change\n",
3413 rdev
->raid_disk
, mdname(mddev
));
3417 module_put(mddev
->pers
->owner
);
3419 mddev
->private = priv
;
3420 strlcpy(mddev
->clevel
, pers
->name
, sizeof(mddev
->clevel
));
3421 mddev
->level
= mddev
->new_level
;
3422 mddev
->layout
= mddev
->new_layout
;
3423 mddev
->chunk_sectors
= mddev
->new_chunk_sectors
;
3424 mddev
->delta_disks
= 0;
3425 mddev
->degraded
= 0;
3426 if (mddev
->pers
->sync_request
== NULL
) {
3427 /* this is now an array without redundancy, so
3428 * it must always be in_sync
3431 del_timer_sync(&mddev
->safemode_timer
);
3434 mddev_resume(mddev
);
3435 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
3436 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3437 md_wakeup_thread(mddev
->thread
);
3438 sysfs_notify(&mddev
->kobj
, NULL
, "level");
3439 md_new_event(mddev
);
3443 static struct md_sysfs_entry md_level
=
3444 __ATTR(level
, S_IRUGO
|S_IWUSR
, level_show
, level_store
);
3448 layout_show(mddev_t
*mddev
, char *page
)
3450 /* just a number, not meaningful for all levels */
3451 if (mddev
->reshape_position
!= MaxSector
&&
3452 mddev
->layout
!= mddev
->new_layout
)
3453 return sprintf(page
, "%d (%d)\n",
3454 mddev
->new_layout
, mddev
->layout
);
3455 return sprintf(page
, "%d\n", mddev
->layout
);
3459 layout_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3462 unsigned long n
= simple_strtoul(buf
, &e
, 10);
3464 if (!*buf
|| (*e
&& *e
!= '\n'))
3469 if (mddev
->pers
->check_reshape
== NULL
)
3471 mddev
->new_layout
= n
;
3472 err
= mddev
->pers
->check_reshape(mddev
);
3474 mddev
->new_layout
= mddev
->layout
;
3478 mddev
->new_layout
= n
;
3479 if (mddev
->reshape_position
== MaxSector
)
3484 static struct md_sysfs_entry md_layout
=
3485 __ATTR(layout
, S_IRUGO
|S_IWUSR
, layout_show
, layout_store
);
3489 raid_disks_show(mddev_t
*mddev
, char *page
)
3491 if (mddev
->raid_disks
== 0)
3493 if (mddev
->reshape_position
!= MaxSector
&&
3494 mddev
->delta_disks
!= 0)
3495 return sprintf(page
, "%d (%d)\n", mddev
->raid_disks
,
3496 mddev
->raid_disks
- mddev
->delta_disks
);
3497 return sprintf(page
, "%d\n", mddev
->raid_disks
);
3500 static int update_raid_disks(mddev_t
*mddev
, int raid_disks
);
3503 raid_disks_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3507 unsigned long n
= simple_strtoul(buf
, &e
, 10);
3509 if (!*buf
|| (*e
&& *e
!= '\n'))
3513 rv
= update_raid_disks(mddev
, n
);
3514 else if (mddev
->reshape_position
!= MaxSector
) {
3515 int olddisks
= mddev
->raid_disks
- mddev
->delta_disks
;
3516 mddev
->delta_disks
= n
- olddisks
;
3517 mddev
->raid_disks
= n
;
3519 mddev
->raid_disks
= n
;
3520 return rv
? rv
: len
;
3522 static struct md_sysfs_entry md_raid_disks
=
3523 __ATTR(raid_disks
, S_IRUGO
|S_IWUSR
, raid_disks_show
, raid_disks_store
);
3526 chunk_size_show(mddev_t
*mddev
, char *page
)
3528 if (mddev
->reshape_position
!= MaxSector
&&
3529 mddev
->chunk_sectors
!= mddev
->new_chunk_sectors
)
3530 return sprintf(page
, "%d (%d)\n",
3531 mddev
->new_chunk_sectors
<< 9,
3532 mddev
->chunk_sectors
<< 9);
3533 return sprintf(page
, "%d\n", mddev
->chunk_sectors
<< 9);
3537 chunk_size_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3540 unsigned long n
= simple_strtoul(buf
, &e
, 10);
3542 if (!*buf
|| (*e
&& *e
!= '\n'))
3547 if (mddev
->pers
->check_reshape
== NULL
)
3549 mddev
->new_chunk_sectors
= n
>> 9;
3550 err
= mddev
->pers
->check_reshape(mddev
);
3552 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
3556 mddev
->new_chunk_sectors
= n
>> 9;
3557 if (mddev
->reshape_position
== MaxSector
)
3558 mddev
->chunk_sectors
= n
>> 9;
3562 static struct md_sysfs_entry md_chunk_size
=
3563 __ATTR(chunk_size
, S_IRUGO
|S_IWUSR
, chunk_size_show
, chunk_size_store
);
3566 resync_start_show(mddev_t
*mddev
, char *page
)
3568 if (mddev
->recovery_cp
== MaxSector
)
3569 return sprintf(page
, "none\n");
3570 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->recovery_cp
);
3574 resync_start_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3577 unsigned long long n
= simple_strtoull(buf
, &e
, 10);
3579 if (mddev
->pers
&& !test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
))
3581 if (cmd_match(buf
, "none"))
3583 else if (!*buf
|| (*e
&& *e
!= '\n'))
3586 mddev
->recovery_cp
= n
;
3589 static struct md_sysfs_entry md_resync_start
=
3590 __ATTR(resync_start
, S_IRUGO
|S_IWUSR
, resync_start_show
, resync_start_store
);
3593 * The array state can be:
3596 * No devices, no size, no level
3597 * Equivalent to STOP_ARRAY ioctl
3599 * May have some settings, but array is not active
3600 * all IO results in error
3601 * When written, doesn't tear down array, but just stops it
3602 * suspended (not supported yet)
3603 * All IO requests will block. The array can be reconfigured.
3604 * Writing this, if accepted, will block until array is quiescent
3606 * no resync can happen. no superblocks get written.
3607 * write requests fail
3609 * like readonly, but behaves like 'clean' on a write request.
3611 * clean - no pending writes, but otherwise active.
3612 * When written to inactive array, starts without resync
3613 * If a write request arrives then
3614 * if metadata is known, mark 'dirty' and switch to 'active'.
3615 * if not known, block and switch to write-pending
3616 * If written to an active array that has pending writes, then fails.
3618 * fully active: IO and resync can be happening.
3619 * When written to inactive array, starts with resync
3622 * clean, but writes are blocked waiting for 'active' to be written.
3625 * like active, but no writes have been seen for a while (100msec).
3628 enum array_state
{ clear
, inactive
, suspended
, readonly
, read_auto
, clean
, active
,
3629 write_pending
, active_idle
, bad_word
};
3630 static char *array_states
[] = {
3631 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3632 "write-pending", "active-idle", NULL
};
3634 static int match_word(const char *word
, char **list
)
3637 for (n
=0; list
[n
]; n
++)
3638 if (cmd_match(word
, list
[n
]))
3644 array_state_show(mddev_t
*mddev
, char *page
)
3646 enum array_state st
= inactive
;
3659 else if (test_bit(MD_CHANGE_PENDING
, &mddev
->flags
))
3661 else if (mddev
->safemode
)
3667 if (list_empty(&mddev
->disks
) &&
3668 mddev
->raid_disks
== 0 &&
3669 mddev
->dev_sectors
== 0)
3674 return sprintf(page
, "%s\n", array_states
[st
]);
3677 static int do_md_stop(mddev_t
* mddev
, int ro
, int is_open
);
3678 static int md_set_readonly(mddev_t
* mddev
, int is_open
);
3679 static int do_md_run(mddev_t
* mddev
);
3680 static int restart_array(mddev_t
*mddev
);
3683 array_state_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3686 enum array_state st
= match_word(buf
, array_states
);
3691 /* stopping an active array */
3692 if (atomic_read(&mddev
->openers
) > 0)
3694 err
= do_md_stop(mddev
, 0, 0);
3697 /* stopping an active array */
3699 if (atomic_read(&mddev
->openers
) > 0)
3701 err
= do_md_stop(mddev
, 2, 0);
3703 err
= 0; /* already inactive */
3706 break; /* not supported yet */
3709 err
= md_set_readonly(mddev
, 0);
3712 set_disk_ro(mddev
->gendisk
, 1);
3713 err
= do_md_run(mddev
);
3719 err
= md_set_readonly(mddev
, 0);
3720 else if (mddev
->ro
== 1)
3721 err
= restart_array(mddev
);
3724 set_disk_ro(mddev
->gendisk
, 0);
3728 err
= do_md_run(mddev
);
3733 restart_array(mddev
);
3734 spin_lock_irq(&mddev
->write_lock
);
3735 if (atomic_read(&mddev
->writes_pending
) == 0) {
3736 if (mddev
->in_sync
== 0) {
3738 if (mddev
->safemode
== 1)
3739 mddev
->safemode
= 0;
3740 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
3745 spin_unlock_irq(&mddev
->write_lock
);
3751 restart_array(mddev
);
3752 clear_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
3753 wake_up(&mddev
->sb_wait
);
3757 set_disk_ro(mddev
->gendisk
, 0);
3758 err
= do_md_run(mddev
);
3763 /* these cannot be set */
3769 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
3773 static struct md_sysfs_entry md_array_state
=
3774 __ATTR(array_state
, S_IRUGO
|S_IWUSR
, array_state_show
, array_state_store
);
3777 max_corrected_read_errors_show(mddev_t
*mddev
, char *page
) {
3778 return sprintf(page
, "%d\n",
3779 atomic_read(&mddev
->max_corr_read_errors
));
3783 max_corrected_read_errors_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3786 unsigned long n
= simple_strtoul(buf
, &e
, 10);
3788 if (*buf
&& (*e
== 0 || *e
== '\n')) {
3789 atomic_set(&mddev
->max_corr_read_errors
, n
);
3795 static struct md_sysfs_entry max_corr_read_errors
=
3796 __ATTR(max_read_errors
, S_IRUGO
|S_IWUSR
, max_corrected_read_errors_show
,
3797 max_corrected_read_errors_store
);
3800 null_show(mddev_t
*mddev
, char *page
)
3806 new_dev_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3808 /* buf must be %d:%d\n? giving major and minor numbers */
3809 /* The new device is added to the array.
3810 * If the array has a persistent superblock, we read the
3811 * superblock to initialise info and check validity.
3812 * Otherwise, only checking done is that in bind_rdev_to_array,
3813 * which mainly checks size.
3816 int major
= simple_strtoul(buf
, &e
, 10);
3822 if (!*buf
|| *e
!= ':' || !e
[1] || e
[1] == '\n')
3824 minor
= simple_strtoul(e
+1, &e
, 10);
3825 if (*e
&& *e
!= '\n')
3827 dev
= MKDEV(major
, minor
);
3828 if (major
!= MAJOR(dev
) ||
3829 minor
!= MINOR(dev
))
3833 if (mddev
->persistent
) {
3834 rdev
= md_import_device(dev
, mddev
->major_version
,
3835 mddev
->minor_version
);
3836 if (!IS_ERR(rdev
) && !list_empty(&mddev
->disks
)) {
3837 mdk_rdev_t
*rdev0
= list_entry(mddev
->disks
.next
,
3838 mdk_rdev_t
, same_set
);
3839 err
= super_types
[mddev
->major_version
]
3840 .load_super(rdev
, rdev0
, mddev
->minor_version
);
3844 } else if (mddev
->external
)
3845 rdev
= md_import_device(dev
, -2, -1);
3847 rdev
= md_import_device(dev
, -1, -1);
3850 return PTR_ERR(rdev
);
3851 err
= bind_rdev_to_array(rdev
, mddev
);
3855 return err
? err
: len
;
3858 static struct md_sysfs_entry md_new_device
=
3859 __ATTR(new_dev
, S_IWUSR
, null_show
, new_dev_store
);
3862 bitmap_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3865 unsigned long chunk
, end_chunk
;
3869 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3871 chunk
= end_chunk
= simple_strtoul(buf
, &end
, 0);
3872 if (buf
== end
) break;
3873 if (*end
== '-') { /* range */
3875 end_chunk
= simple_strtoul(buf
, &end
, 0);
3876 if (buf
== end
) break;
3878 if (*end
&& !isspace(*end
)) break;
3879 bitmap_dirty_bits(mddev
->bitmap
, chunk
, end_chunk
);
3880 buf
= skip_spaces(end
);
3882 bitmap_unplug(mddev
->bitmap
); /* flush the bits to disk */
3887 static struct md_sysfs_entry md_bitmap
=
3888 __ATTR(bitmap_set_bits
, S_IWUSR
, null_show
, bitmap_store
);
3891 size_show(mddev_t
*mddev
, char *page
)
3893 return sprintf(page
, "%llu\n",
3894 (unsigned long long)mddev
->dev_sectors
/ 2);
3897 static int update_size(mddev_t
*mddev
, sector_t num_sectors
);
3900 size_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3902 /* If array is inactive, we can reduce the component size, but
3903 * not increase it (except from 0).
3904 * If array is active, we can try an on-line resize
3907 int err
= strict_blocks_to_sectors(buf
, §ors
);
3912 err
= update_size(mddev
, sectors
);
3913 md_update_sb(mddev
, 1);
3915 if (mddev
->dev_sectors
== 0 ||
3916 mddev
->dev_sectors
> sectors
)
3917 mddev
->dev_sectors
= sectors
;
3921 return err
? err
: len
;
3924 static struct md_sysfs_entry md_size
=
3925 __ATTR(component_size
, S_IRUGO
|S_IWUSR
, size_show
, size_store
);
3930 * 'none' for arrays with no metadata (good luck...)
3931 * 'external' for arrays with externally managed metadata,
3932 * or N.M for internally known formats
3935 metadata_show(mddev_t
*mddev
, char *page
)
3937 if (mddev
->persistent
)
3938 return sprintf(page
, "%d.%d\n",
3939 mddev
->major_version
, mddev
->minor_version
);
3940 else if (mddev
->external
)
3941 return sprintf(page
, "external:%s\n", mddev
->metadata_type
);
3943 return sprintf(page
, "none\n");
3947 metadata_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3951 /* Changing the details of 'external' metadata is
3952 * always permitted. Otherwise there must be
3953 * no devices attached to the array.
3955 if (mddev
->external
&& strncmp(buf
, "external:", 9) == 0)
3957 else if (!list_empty(&mddev
->disks
))
3960 if (cmd_match(buf
, "none")) {
3961 mddev
->persistent
= 0;
3962 mddev
->external
= 0;
3963 mddev
->major_version
= 0;
3964 mddev
->minor_version
= 90;
3967 if (strncmp(buf
, "external:", 9) == 0) {
3968 size_t namelen
= len
-9;
3969 if (namelen
>= sizeof(mddev
->metadata_type
))
3970 namelen
= sizeof(mddev
->metadata_type
)-1;
3971 strncpy(mddev
->metadata_type
, buf
+9, namelen
);
3972 mddev
->metadata_type
[namelen
] = 0;
3973 if (namelen
&& mddev
->metadata_type
[namelen
-1] == '\n')
3974 mddev
->metadata_type
[--namelen
] = 0;
3975 mddev
->persistent
= 0;
3976 mddev
->external
= 1;
3977 mddev
->major_version
= 0;
3978 mddev
->minor_version
= 90;
3981 major
= simple_strtoul(buf
, &e
, 10);
3982 if (e
==buf
|| *e
!= '.')
3985 minor
= simple_strtoul(buf
, &e
, 10);
3986 if (e
==buf
|| (*e
&& *e
!= '\n') )
3988 if (major
>= ARRAY_SIZE(super_types
) || super_types
[major
].name
== NULL
)
3990 mddev
->major_version
= major
;
3991 mddev
->minor_version
= minor
;
3992 mddev
->persistent
= 1;
3993 mddev
->external
= 0;
3997 static struct md_sysfs_entry md_metadata
=
3998 __ATTR(metadata_version
, S_IRUGO
|S_IWUSR
, metadata_show
, metadata_store
);
4001 action_show(mddev_t
*mddev
, char *page
)
4003 char *type
= "idle";
4004 if (test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
))
4006 else if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
4007 (!mddev
->ro
&& test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))) {
4008 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
4010 else if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
4011 if (!test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
4013 else if (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
))
4017 } else if (test_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
))
4020 return sprintf(page
, "%s\n", type
);
4023 static void reap_sync_thread(mddev_t
*mddev
);
4026 action_store(mddev_t
*mddev
, const char *page
, size_t len
)
4028 if (!mddev
->pers
|| !mddev
->pers
->sync_request
)
4031 if (cmd_match(page
, "frozen"))
4032 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4034 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4036 if (cmd_match(page
, "idle") || cmd_match(page
, "frozen")) {
4037 if (mddev
->sync_thread
) {
4038 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
4039 reap_sync_thread(mddev
);
4041 } else if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
4042 test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))
4044 else if (cmd_match(page
, "resync"))
4045 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4046 else if (cmd_match(page
, "recover")) {
4047 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
4048 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4049 } else if (cmd_match(page
, "reshape")) {
4051 if (mddev
->pers
->start_reshape
== NULL
)
4053 err
= mddev
->pers
->start_reshape(mddev
);
4056 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
4058 if (cmd_match(page
, "check"))
4059 set_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
4060 else if (!cmd_match(page
, "repair"))
4062 set_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
4063 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
4065 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4066 md_wakeup_thread(mddev
->thread
);
4067 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
4072 mismatch_cnt_show(mddev_t
*mddev
, char *page
)
4074 return sprintf(page
, "%llu\n",
4075 (unsigned long long) mddev
->resync_mismatches
);
4078 static struct md_sysfs_entry md_scan_mode
=
4079 __ATTR(sync_action
, S_IRUGO
|S_IWUSR
, action_show
, action_store
);
4082 static struct md_sysfs_entry md_mismatches
= __ATTR_RO(mismatch_cnt
);
4085 sync_min_show(mddev_t
*mddev
, char *page
)
4087 return sprintf(page
, "%d (%s)\n", speed_min(mddev
),
4088 mddev
->sync_speed_min
? "local": "system");
4092 sync_min_store(mddev_t
*mddev
, const char *buf
, size_t len
)
4096 if (strncmp(buf
, "system", 6)==0) {
4097 mddev
->sync_speed_min
= 0;
4100 min
= simple_strtoul(buf
, &e
, 10);
4101 if (buf
== e
|| (*e
&& *e
!= '\n') || min
<= 0)
4103 mddev
->sync_speed_min
= min
;
4107 static struct md_sysfs_entry md_sync_min
=
4108 __ATTR(sync_speed_min
, S_IRUGO
|S_IWUSR
, sync_min_show
, sync_min_store
);
4111 sync_max_show(mddev_t
*mddev
, char *page
)
4113 return sprintf(page
, "%d (%s)\n", speed_max(mddev
),
4114 mddev
->sync_speed_max
? "local": "system");
4118 sync_max_store(mddev_t
*mddev
, const char *buf
, size_t len
)
4122 if (strncmp(buf
, "system", 6)==0) {
4123 mddev
->sync_speed_max
= 0;
4126 max
= simple_strtoul(buf
, &e
, 10);
4127 if (buf
== e
|| (*e
&& *e
!= '\n') || max
<= 0)
4129 mddev
->sync_speed_max
= max
;
4133 static struct md_sysfs_entry md_sync_max
=
4134 __ATTR(sync_speed_max
, S_IRUGO
|S_IWUSR
, sync_max_show
, sync_max_store
);
4137 degraded_show(mddev_t
*mddev
, char *page
)
4139 return sprintf(page
, "%d\n", mddev
->degraded
);
4141 static struct md_sysfs_entry md_degraded
= __ATTR_RO(degraded
);
4144 sync_force_parallel_show(mddev_t
*mddev
, char *page
)
4146 return sprintf(page
, "%d\n", mddev
->parallel_resync
);
4150 sync_force_parallel_store(mddev_t
*mddev
, const char *buf
, size_t len
)
4154 if (strict_strtol(buf
, 10, &n
))
4157 if (n
!= 0 && n
!= 1)
4160 mddev
->parallel_resync
= n
;
4162 if (mddev
->sync_thread
)
4163 wake_up(&resync_wait
);
4168 /* force parallel resync, even with shared block devices */
4169 static struct md_sysfs_entry md_sync_force_parallel
=
4170 __ATTR(sync_force_parallel
, S_IRUGO
|S_IWUSR
,
4171 sync_force_parallel_show
, sync_force_parallel_store
);
4174 sync_speed_show(mddev_t
*mddev
, char *page
)
4176 unsigned long resync
, dt
, db
;
4177 if (mddev
->curr_resync
== 0)
4178 return sprintf(page
, "none\n");
4179 resync
= mddev
->curr_mark_cnt
- atomic_read(&mddev
->recovery_active
);
4180 dt
= (jiffies
- mddev
->resync_mark
) / HZ
;
4182 db
= resync
- mddev
->resync_mark_cnt
;
4183 return sprintf(page
, "%lu\n", db
/dt
/2); /* K/sec */
4186 static struct md_sysfs_entry md_sync_speed
= __ATTR_RO(sync_speed
);
4189 sync_completed_show(mddev_t
*mddev
, char *page
)
4191 unsigned long long max_sectors
, resync
;
4193 if (!test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
4194 return sprintf(page
, "none\n");
4196 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
4197 max_sectors
= mddev
->resync_max_sectors
;
4199 max_sectors
= mddev
->dev_sectors
;
4201 resync
= mddev
->curr_resync_completed
;
4202 return sprintf(page
, "%llu / %llu\n", resync
, max_sectors
);
4205 static struct md_sysfs_entry md_sync_completed
= __ATTR_RO(sync_completed
);
4208 min_sync_show(mddev_t
*mddev
, char *page
)
4210 return sprintf(page
, "%llu\n",
4211 (unsigned long long)mddev
->resync_min
);
4214 min_sync_store(mddev_t
*mddev
, const char *buf
, size_t len
)
4216 unsigned long long min
;
4217 if (strict_strtoull(buf
, 10, &min
))
4219 if (min
> mddev
->resync_max
)
4221 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
4224 /* Must be a multiple of chunk_size */
4225 if (mddev
->chunk_sectors
) {
4226 sector_t temp
= min
;
4227 if (sector_div(temp
, mddev
->chunk_sectors
))
4230 mddev
->resync_min
= min
;
4235 static struct md_sysfs_entry md_min_sync
=
4236 __ATTR(sync_min
, S_IRUGO
|S_IWUSR
, min_sync_show
, min_sync_store
);
4239 max_sync_show(mddev_t
*mddev
, char *page
)
4241 if (mddev
->resync_max
== MaxSector
)
4242 return sprintf(page
, "max\n");
4244 return sprintf(page
, "%llu\n",
4245 (unsigned long long)mddev
->resync_max
);
4248 max_sync_store(mddev_t
*mddev
, const char *buf
, size_t len
)
4250 if (strncmp(buf
, "max", 3) == 0)
4251 mddev
->resync_max
= MaxSector
;
4253 unsigned long long max
;
4254 if (strict_strtoull(buf
, 10, &max
))
4256 if (max
< mddev
->resync_min
)
4258 if (max
< mddev
->resync_max
&&
4260 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
4263 /* Must be a multiple of chunk_size */
4264 if (mddev
->chunk_sectors
) {
4265 sector_t temp
= max
;
4266 if (sector_div(temp
, mddev
->chunk_sectors
))
4269 mddev
->resync_max
= max
;
4271 wake_up(&mddev
->recovery_wait
);
4275 static struct md_sysfs_entry md_max_sync
=
4276 __ATTR(sync_max
, S_IRUGO
|S_IWUSR
, max_sync_show
, max_sync_store
);
4279 suspend_lo_show(mddev_t
*mddev
, char *page
)
4281 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->suspend_lo
);
4285 suspend_lo_store(mddev_t
*mddev
, const char *buf
, size_t len
)
4288 unsigned long long new = simple_strtoull(buf
, &e
, 10);
4289 unsigned long long old
= mddev
->suspend_lo
;
4291 if (mddev
->pers
== NULL
||
4292 mddev
->pers
->quiesce
== NULL
)
4294 if (buf
== e
|| (*e
&& *e
!= '\n'))
4297 mddev
->suspend_lo
= new;
4299 /* Shrinking suspended region */
4300 mddev
->pers
->quiesce(mddev
, 2);
4302 /* Expanding suspended region - need to wait */
4303 mddev
->pers
->quiesce(mddev
, 1);
4304 mddev
->pers
->quiesce(mddev
, 0);
4308 static struct md_sysfs_entry md_suspend_lo
=
4309 __ATTR(suspend_lo
, S_IRUGO
|S_IWUSR
, suspend_lo_show
, suspend_lo_store
);
4313 suspend_hi_show(mddev_t
*mddev
, char *page
)
4315 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->suspend_hi
);
4319 suspend_hi_store(mddev_t
*mddev
, const char *buf
, size_t len
)
4322 unsigned long long new = simple_strtoull(buf
, &e
, 10);
4323 unsigned long long old
= mddev
->suspend_hi
;
4325 if (mddev
->pers
== NULL
||
4326 mddev
->pers
->quiesce
== NULL
)
4328 if (buf
== e
|| (*e
&& *e
!= '\n'))
4331 mddev
->suspend_hi
= new;
4333 /* Shrinking suspended region */
4334 mddev
->pers
->quiesce(mddev
, 2);
4336 /* Expanding suspended region - need to wait */
4337 mddev
->pers
->quiesce(mddev
, 1);
4338 mddev
->pers
->quiesce(mddev
, 0);
4342 static struct md_sysfs_entry md_suspend_hi
=
4343 __ATTR(suspend_hi
, S_IRUGO
|S_IWUSR
, suspend_hi_show
, suspend_hi_store
);
4346 reshape_position_show(mddev_t
*mddev
, char *page
)
4348 if (mddev
->reshape_position
!= MaxSector
)
4349 return sprintf(page
, "%llu\n",
4350 (unsigned long long)mddev
->reshape_position
);
4351 strcpy(page
, "none\n");
4356 reshape_position_store(mddev_t
*mddev
, const char *buf
, size_t len
)
4359 unsigned long long new = simple_strtoull(buf
, &e
, 10);
4362 if (buf
== e
|| (*e
&& *e
!= '\n'))
4364 mddev
->reshape_position
= new;
4365 mddev
->delta_disks
= 0;
4366 mddev
->new_level
= mddev
->level
;
4367 mddev
->new_layout
= mddev
->layout
;
4368 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
4372 static struct md_sysfs_entry md_reshape_position
=
4373 __ATTR(reshape_position
, S_IRUGO
|S_IWUSR
, reshape_position_show
,
4374 reshape_position_store
);
4377 array_size_show(mddev_t
*mddev
, char *page
)
4379 if (mddev
->external_size
)
4380 return sprintf(page
, "%llu\n",
4381 (unsigned long long)mddev
->array_sectors
/2);
4383 return sprintf(page
, "default\n");
4387 array_size_store(mddev_t
*mddev
, const char *buf
, size_t len
)
4391 if (strncmp(buf
, "default", 7) == 0) {
4393 sectors
= mddev
->pers
->size(mddev
, 0, 0);
4395 sectors
= mddev
->array_sectors
;
4397 mddev
->external_size
= 0;
4399 if (strict_blocks_to_sectors(buf
, §ors
) < 0)
4401 if (mddev
->pers
&& mddev
->pers
->size(mddev
, 0, 0) < sectors
)
4404 mddev
->external_size
= 1;
4407 mddev
->array_sectors
= sectors
;
4409 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
4410 revalidate_disk(mddev
->gendisk
);
4415 static struct md_sysfs_entry md_array_size
=
4416 __ATTR(array_size
, S_IRUGO
|S_IWUSR
, array_size_show
,
4419 static struct attribute
*md_default_attrs
[] = {
4422 &md_raid_disks
.attr
,
4423 &md_chunk_size
.attr
,
4425 &md_resync_start
.attr
,
4427 &md_new_device
.attr
,
4428 &md_safe_delay
.attr
,
4429 &md_array_state
.attr
,
4430 &md_reshape_position
.attr
,
4431 &md_array_size
.attr
,
4432 &max_corr_read_errors
.attr
,
4436 static struct attribute
*md_redundancy_attrs
[] = {
4438 &md_mismatches
.attr
,
4441 &md_sync_speed
.attr
,
4442 &md_sync_force_parallel
.attr
,
4443 &md_sync_completed
.attr
,
4446 &md_suspend_lo
.attr
,
4447 &md_suspend_hi
.attr
,
4452 static struct attribute_group md_redundancy_group
= {
4454 .attrs
= md_redundancy_attrs
,
4459 md_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
4461 struct md_sysfs_entry
*entry
= container_of(attr
, struct md_sysfs_entry
, attr
);
4462 mddev_t
*mddev
= container_of(kobj
, struct mddev_s
, kobj
);
4467 rv
= mddev_lock(mddev
);
4469 rv
= entry
->show(mddev
, page
);
4470 mddev_unlock(mddev
);
4476 md_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
4477 const char *page
, size_t length
)
4479 struct md_sysfs_entry
*entry
= container_of(attr
, struct md_sysfs_entry
, attr
);
4480 mddev_t
*mddev
= container_of(kobj
, struct mddev_s
, kobj
);
4485 if (!capable(CAP_SYS_ADMIN
))
4487 rv
= mddev_lock(mddev
);
4488 if (mddev
->hold_active
== UNTIL_IOCTL
)
4489 mddev
->hold_active
= 0;
4491 rv
= entry
->store(mddev
, page
, length
);
4492 mddev_unlock(mddev
);
4497 static void md_free(struct kobject
*ko
)
4499 mddev_t
*mddev
= container_of(ko
, mddev_t
, kobj
);
4501 if (mddev
->sysfs_state
)
4502 sysfs_put(mddev
->sysfs_state
);
4504 if (mddev
->gendisk
) {
4505 del_gendisk(mddev
->gendisk
);
4506 put_disk(mddev
->gendisk
);
4509 blk_cleanup_queue(mddev
->queue
);
4514 static const struct sysfs_ops md_sysfs_ops
= {
4515 .show
= md_attr_show
,
4516 .store
= md_attr_store
,
4518 static struct kobj_type md_ktype
= {
4520 .sysfs_ops
= &md_sysfs_ops
,
4521 .default_attrs
= md_default_attrs
,
4526 static void mddev_delayed_delete(struct work_struct
*ws
)
4528 mddev_t
*mddev
= container_of(ws
, mddev_t
, del_work
);
4530 sysfs_remove_group(&mddev
->kobj
, &md_bitmap_group
);
4531 kobject_del(&mddev
->kobj
);
4532 kobject_put(&mddev
->kobj
);
4535 static int md_alloc(dev_t dev
, char *name
)
4537 static DEFINE_MUTEX(disks_mutex
);
4538 mddev_t
*mddev
= mddev_find(dev
);
4539 struct gendisk
*disk
;
4548 partitioned
= (MAJOR(mddev
->unit
) != MD_MAJOR
);
4549 shift
= partitioned
? MdpMinorShift
: 0;
4550 unit
= MINOR(mddev
->unit
) >> shift
;
4552 /* wait for any previous instance of this device to be
4553 * completely removed (mddev_delayed_delete).
4555 flush_workqueue(md_misc_wq
);
4557 mutex_lock(&disks_mutex
);
4563 /* Need to ensure that 'name' is not a duplicate.
4566 spin_lock(&all_mddevs_lock
);
4568 list_for_each_entry(mddev2
, &all_mddevs
, all_mddevs
)
4569 if (mddev2
->gendisk
&&
4570 strcmp(mddev2
->gendisk
->disk_name
, name
) == 0) {
4571 spin_unlock(&all_mddevs_lock
);
4574 spin_unlock(&all_mddevs_lock
);
4578 mddev
->queue
= blk_alloc_queue(GFP_KERNEL
);
4581 mddev
->queue
->queuedata
= mddev
;
4583 blk_queue_make_request(mddev
->queue
, md_make_request
);
4585 disk
= alloc_disk(1 << shift
);
4587 blk_cleanup_queue(mddev
->queue
);
4588 mddev
->queue
= NULL
;
4591 disk
->major
= MAJOR(mddev
->unit
);
4592 disk
->first_minor
= unit
<< shift
;
4594 strcpy(disk
->disk_name
, name
);
4595 else if (partitioned
)
4596 sprintf(disk
->disk_name
, "md_d%d", unit
);
4598 sprintf(disk
->disk_name
, "md%d", unit
);
4599 disk
->fops
= &md_fops
;
4600 disk
->private_data
= mddev
;
4601 disk
->queue
= mddev
->queue
;
4602 blk_queue_flush(mddev
->queue
, REQ_FLUSH
| REQ_FUA
);
4603 /* Allow extended partitions. This makes the
4604 * 'mdp' device redundant, but we can't really
4607 disk
->flags
|= GENHD_FL_EXT_DEVT
;
4608 mddev
->gendisk
= disk
;
4609 /* As soon as we call add_disk(), another thread could get
4610 * through to md_open, so make sure it doesn't get too far
4612 mutex_lock(&mddev
->open_mutex
);
4615 error
= kobject_init_and_add(&mddev
->kobj
, &md_ktype
,
4616 &disk_to_dev(disk
)->kobj
, "%s", "md");
4618 /* This isn't possible, but as kobject_init_and_add is marked
4619 * __must_check, we must do something with the result
4621 printk(KERN_WARNING
"md: cannot register %s/md - name in use\n",
4625 if (mddev
->kobj
.sd
&&
4626 sysfs_create_group(&mddev
->kobj
, &md_bitmap_group
))
4627 printk(KERN_DEBUG
"pointless warning\n");
4628 mutex_unlock(&mddev
->open_mutex
);
4630 mutex_unlock(&disks_mutex
);
4631 if (!error
&& mddev
->kobj
.sd
) {
4632 kobject_uevent(&mddev
->kobj
, KOBJ_ADD
);
4633 mddev
->sysfs_state
= sysfs_get_dirent_safe(mddev
->kobj
.sd
, "array_state");
4639 static struct kobject
*md_probe(dev_t dev
, int *part
, void *data
)
4641 md_alloc(dev
, NULL
);
4645 static int add_named_array(const char *val
, struct kernel_param
*kp
)
4647 /* val must be "md_*" where * is not all digits.
4648 * We allocate an array with a large free minor number, and
4649 * set the name to val. val must not already be an active name.
4651 int len
= strlen(val
);
4652 char buf
[DISK_NAME_LEN
];
4654 while (len
&& val
[len
-1] == '\n')
4656 if (len
>= DISK_NAME_LEN
)
4658 strlcpy(buf
, val
, len
+1);
4659 if (strncmp(buf
, "md_", 3) != 0)
4661 return md_alloc(0, buf
);
4664 static void md_safemode_timeout(unsigned long data
)
4666 mddev_t
*mddev
= (mddev_t
*) data
;
4668 if (!atomic_read(&mddev
->writes_pending
)) {
4669 mddev
->safemode
= 1;
4670 if (mddev
->external
)
4671 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4673 md_wakeup_thread(mddev
->thread
);
4676 static int start_dirty_degraded
;
4678 int md_run(mddev_t
*mddev
)
4682 struct mdk_personality
*pers
;
4684 if (list_empty(&mddev
->disks
))
4685 /* cannot run an array with no devices.. */
4690 /* Cannot run until previous stop completes properly */
4691 if (mddev
->sysfs_active
)
4695 * Analyze all RAID superblock(s)
4697 if (!mddev
->raid_disks
) {
4698 if (!mddev
->persistent
)
4703 if (mddev
->level
!= LEVEL_NONE
)
4704 request_module("md-level-%d", mddev
->level
);
4705 else if (mddev
->clevel
[0])
4706 request_module("md-%s", mddev
->clevel
);
4709 * Drop all container device buffers, from now on
4710 * the only valid external interface is through the md
4713 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
4714 if (test_bit(Faulty
, &rdev
->flags
))
4716 sync_blockdev(rdev
->bdev
);
4717 invalidate_bdev(rdev
->bdev
);
4719 /* perform some consistency tests on the device.
4720 * We don't want the data to overlap the metadata,
4721 * Internal Bitmap issues have been handled elsewhere.
4723 if (rdev
->meta_bdev
) {
4724 /* Nothing to check */;
4725 } else if (rdev
->data_offset
< rdev
->sb_start
) {
4726 if (mddev
->dev_sectors
&&
4727 rdev
->data_offset
+ mddev
->dev_sectors
4729 printk("md: %s: data overlaps metadata\n",
4734 if (rdev
->sb_start
+ rdev
->sb_size
/512
4735 > rdev
->data_offset
) {
4736 printk("md: %s: metadata overlaps data\n",
4741 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
4744 if (mddev
->bio_set
== NULL
)
4745 mddev
->bio_set
= bioset_create(BIO_POOL_SIZE
,
4748 spin_lock(&pers_lock
);
4749 pers
= find_pers(mddev
->level
, mddev
->clevel
);
4750 if (!pers
|| !try_module_get(pers
->owner
)) {
4751 spin_unlock(&pers_lock
);
4752 if (mddev
->level
!= LEVEL_NONE
)
4753 printk(KERN_WARNING
"md: personality for level %d is not loaded!\n",
4756 printk(KERN_WARNING
"md: personality for level %s is not loaded!\n",
4761 spin_unlock(&pers_lock
);
4762 if (mddev
->level
!= pers
->level
) {
4763 mddev
->level
= pers
->level
;
4764 mddev
->new_level
= pers
->level
;
4766 strlcpy(mddev
->clevel
, pers
->name
, sizeof(mddev
->clevel
));
4768 if (mddev
->reshape_position
!= MaxSector
&&
4769 pers
->start_reshape
== NULL
) {
4770 /* This personality cannot handle reshaping... */
4772 module_put(pers
->owner
);
4776 if (pers
->sync_request
) {
4777 /* Warn if this is a potentially silly
4780 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
4784 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
4785 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
) {
4787 rdev
->bdev
->bd_contains
==
4788 rdev2
->bdev
->bd_contains
) {
4790 "%s: WARNING: %s appears to be"
4791 " on the same physical disk as"
4794 bdevname(rdev
->bdev
,b
),
4795 bdevname(rdev2
->bdev
,b2
));
4802 "True protection against single-disk"
4803 " failure might be compromised.\n");
4806 mddev
->recovery
= 0;
4807 /* may be over-ridden by personality */
4808 mddev
->resync_max_sectors
= mddev
->dev_sectors
;
4810 mddev
->ok_start_degraded
= start_dirty_degraded
;
4812 if (start_readonly
&& mddev
->ro
== 0)
4813 mddev
->ro
= 2; /* read-only, but switch on first write */
4815 err
= mddev
->pers
->run(mddev
);
4817 printk(KERN_ERR
"md: pers->run() failed ...\n");
4818 else if (mddev
->pers
->size(mddev
, 0, 0) < mddev
->array_sectors
) {
4819 WARN_ONCE(!mddev
->external_size
, "%s: default size too small,"
4820 " but 'external_size' not in effect?\n", __func__
);
4822 "md: invalid array_size %llu > default size %llu\n",
4823 (unsigned long long)mddev
->array_sectors
/ 2,
4824 (unsigned long long)mddev
->pers
->size(mddev
, 0, 0) / 2);
4826 mddev
->pers
->stop(mddev
);
4828 if (err
== 0 && mddev
->pers
->sync_request
) {
4829 err
= bitmap_create(mddev
);
4831 printk(KERN_ERR
"%s: failed to create bitmap (%d)\n",
4832 mdname(mddev
), err
);
4833 mddev
->pers
->stop(mddev
);
4837 module_put(mddev
->pers
->owner
);
4839 bitmap_destroy(mddev
);
4842 if (mddev
->pers
->sync_request
) {
4843 if (mddev
->kobj
.sd
&&
4844 sysfs_create_group(&mddev
->kobj
, &md_redundancy_group
))
4846 "md: cannot register extra attributes for %s\n",
4848 mddev
->sysfs_action
= sysfs_get_dirent_safe(mddev
->kobj
.sd
, "sync_action");
4849 } else if (mddev
->ro
== 2) /* auto-readonly not meaningful */
4852 atomic_set(&mddev
->writes_pending
,0);
4853 atomic_set(&mddev
->max_corr_read_errors
,
4854 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS
);
4855 mddev
->safemode
= 0;
4856 mddev
->safemode_timer
.function
= md_safemode_timeout
;
4857 mddev
->safemode_timer
.data
= (unsigned long) mddev
;
4858 mddev
->safemode_delay
= (200 * HZ
)/1000 +1; /* 200 msec delay */
4862 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
4863 if (rdev
->raid_disk
>= 0)
4864 if (sysfs_link_rdev(mddev
, rdev
))
4865 /* failure here is OK */;
4867 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4870 md_update_sb(mddev
, 0);
4872 md_new_event(mddev
);
4873 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4874 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
4875 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
4878 EXPORT_SYMBOL_GPL(md_run
);
4880 static int do_md_run(mddev_t
*mddev
)
4884 err
= md_run(mddev
);
4887 err
= bitmap_load(mddev
);
4889 bitmap_destroy(mddev
);
4893 md_wakeup_thread(mddev
->thread
);
4894 md_wakeup_thread(mddev
->sync_thread
); /* possibly kick off a reshape */
4896 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
4897 revalidate_disk(mddev
->gendisk
);
4899 kobject_uevent(&disk_to_dev(mddev
->gendisk
)->kobj
, KOBJ_CHANGE
);
4904 static int restart_array(mddev_t
*mddev
)
4906 struct gendisk
*disk
= mddev
->gendisk
;
4908 /* Complain if it has no devices */
4909 if (list_empty(&mddev
->disks
))
4915 mddev
->safemode
= 0;
4917 set_disk_ro(disk
, 0);
4918 printk(KERN_INFO
"md: %s switched to read-write mode.\n",
4920 /* Kick recovery or resync if necessary */
4921 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4922 md_wakeup_thread(mddev
->thread
);
4923 md_wakeup_thread(mddev
->sync_thread
);
4924 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4928 /* similar to deny_write_access, but accounts for our holding a reference
4929 * to the file ourselves */
4930 static int deny_bitmap_write_access(struct file
* file
)
4932 struct inode
*inode
= file
->f_mapping
->host
;
4934 spin_lock(&inode
->i_lock
);
4935 if (atomic_read(&inode
->i_writecount
) > 1) {
4936 spin_unlock(&inode
->i_lock
);
4939 atomic_set(&inode
->i_writecount
, -1);
4940 spin_unlock(&inode
->i_lock
);
4945 void restore_bitmap_write_access(struct file
*file
)
4947 struct inode
*inode
= file
->f_mapping
->host
;
4949 spin_lock(&inode
->i_lock
);
4950 atomic_set(&inode
->i_writecount
, 1);
4951 spin_unlock(&inode
->i_lock
);
4954 static void md_clean(mddev_t
*mddev
)
4956 mddev
->array_sectors
= 0;
4957 mddev
->external_size
= 0;
4958 mddev
->dev_sectors
= 0;
4959 mddev
->raid_disks
= 0;
4960 mddev
->recovery_cp
= 0;
4961 mddev
->resync_min
= 0;
4962 mddev
->resync_max
= MaxSector
;
4963 mddev
->reshape_position
= MaxSector
;
4964 mddev
->external
= 0;
4965 mddev
->persistent
= 0;
4966 mddev
->level
= LEVEL_NONE
;
4967 mddev
->clevel
[0] = 0;
4970 mddev
->metadata_type
[0] = 0;
4971 mddev
->chunk_sectors
= 0;
4972 mddev
->ctime
= mddev
->utime
= 0;
4974 mddev
->max_disks
= 0;
4976 mddev
->can_decrease_events
= 0;
4977 mddev
->delta_disks
= 0;
4978 mddev
->new_level
= LEVEL_NONE
;
4979 mddev
->new_layout
= 0;
4980 mddev
->new_chunk_sectors
= 0;
4981 mddev
->curr_resync
= 0;
4982 mddev
->resync_mismatches
= 0;
4983 mddev
->suspend_lo
= mddev
->suspend_hi
= 0;
4984 mddev
->sync_speed_min
= mddev
->sync_speed_max
= 0;
4985 mddev
->recovery
= 0;
4988 mddev
->degraded
= 0;
4989 mddev
->safemode
= 0;
4990 mddev
->bitmap_info
.offset
= 0;
4991 mddev
->bitmap_info
.default_offset
= 0;
4992 mddev
->bitmap_info
.chunksize
= 0;
4993 mddev
->bitmap_info
.daemon_sleep
= 0;
4994 mddev
->bitmap_info
.max_write_behind
= 0;
4997 static void __md_stop_writes(mddev_t
*mddev
)
4999 if (mddev
->sync_thread
) {
5000 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
5001 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
5002 reap_sync_thread(mddev
);
5005 del_timer_sync(&mddev
->safemode_timer
);
5007 bitmap_flush(mddev
);
5008 md_super_wait(mddev
);
5010 if (!mddev
->in_sync
|| mddev
->flags
) {
5011 /* mark array as shutdown cleanly */
5013 md_update_sb(mddev
, 1);
5017 void md_stop_writes(mddev_t
*mddev
)
5020 __md_stop_writes(mddev
);
5021 mddev_unlock(mddev
);
5023 EXPORT_SYMBOL_GPL(md_stop_writes
);
5025 void md_stop(mddev_t
*mddev
)
5028 mddev
->pers
->stop(mddev
);
5029 if (mddev
->pers
->sync_request
&& mddev
->to_remove
== NULL
)
5030 mddev
->to_remove
= &md_redundancy_group
;
5031 module_put(mddev
->pers
->owner
);
5033 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
5035 EXPORT_SYMBOL_GPL(md_stop
);
5037 static int md_set_readonly(mddev_t
*mddev
, int is_open
)
5040 mutex_lock(&mddev
->open_mutex
);
5041 if (atomic_read(&mddev
->openers
) > is_open
) {
5042 printk("md: %s still in use.\n",mdname(mddev
));
5047 __md_stop_writes(mddev
);
5053 set_disk_ro(mddev
->gendisk
, 1);
5054 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
5055 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
5059 mutex_unlock(&mddev
->open_mutex
);
5064 * 0 - completely stop and dis-assemble array
5065 * 2 - stop but do not disassemble array
5067 static int do_md_stop(mddev_t
* mddev
, int mode
, int is_open
)
5069 struct gendisk
*disk
= mddev
->gendisk
;
5072 mutex_lock(&mddev
->open_mutex
);
5073 if (atomic_read(&mddev
->openers
) > is_open
||
5074 mddev
->sysfs_active
) {
5075 printk("md: %s still in use.\n",mdname(mddev
));
5076 mutex_unlock(&mddev
->open_mutex
);
5082 set_disk_ro(disk
, 0);
5084 __md_stop_writes(mddev
);
5086 mddev
->queue
->merge_bvec_fn
= NULL
;
5087 mddev
->queue
->backing_dev_info
.congested_fn
= NULL
;
5089 /* tell userspace to handle 'inactive' */
5090 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
5092 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
5093 if (rdev
->raid_disk
>= 0)
5094 sysfs_unlink_rdev(mddev
, rdev
);
5096 set_capacity(disk
, 0);
5097 mutex_unlock(&mddev
->open_mutex
);
5099 revalidate_disk(disk
);
5104 mutex_unlock(&mddev
->open_mutex
);
5106 * Free resources if final stop
5109 printk(KERN_INFO
"md: %s stopped.\n", mdname(mddev
));
5111 bitmap_destroy(mddev
);
5112 if (mddev
->bitmap_info
.file
) {
5113 restore_bitmap_write_access(mddev
->bitmap_info
.file
);
5114 fput(mddev
->bitmap_info
.file
);
5115 mddev
->bitmap_info
.file
= NULL
;
5117 mddev
->bitmap_info
.offset
= 0;
5119 export_array(mddev
);
5122 kobject_uevent(&disk_to_dev(mddev
->gendisk
)->kobj
, KOBJ_CHANGE
);
5123 if (mddev
->hold_active
== UNTIL_STOP
)
5124 mddev
->hold_active
= 0;
5126 blk_integrity_unregister(disk
);
5127 md_new_event(mddev
);
5128 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
5133 static void autorun_array(mddev_t
*mddev
)
5138 if (list_empty(&mddev
->disks
))
5141 printk(KERN_INFO
"md: running: ");
5143 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
5144 char b
[BDEVNAME_SIZE
];
5145 printk("<%s>", bdevname(rdev
->bdev
,b
));
5149 err
= do_md_run(mddev
);
5151 printk(KERN_WARNING
"md: do_md_run() returned %d\n", err
);
5152 do_md_stop(mddev
, 0, 0);
5157 * lets try to run arrays based on all disks that have arrived
5158 * until now. (those are in pending_raid_disks)
5160 * the method: pick the first pending disk, collect all disks with
5161 * the same UUID, remove all from the pending list and put them into
5162 * the 'same_array' list. Then order this list based on superblock
5163 * update time (freshest comes first), kick out 'old' disks and
5164 * compare superblocks. If everything's fine then run it.
5166 * If "unit" is allocated, then bump its reference count
5168 static void autorun_devices(int part
)
5170 mdk_rdev_t
*rdev0
, *rdev
, *tmp
;
5172 char b
[BDEVNAME_SIZE
];
5174 printk(KERN_INFO
"md: autorun ...\n");
5175 while (!list_empty(&pending_raid_disks
)) {
5178 LIST_HEAD(candidates
);
5179 rdev0
= list_entry(pending_raid_disks
.next
,
5180 mdk_rdev_t
, same_set
);
5182 printk(KERN_INFO
"md: considering %s ...\n",
5183 bdevname(rdev0
->bdev
,b
));
5184 INIT_LIST_HEAD(&candidates
);
5185 rdev_for_each_list(rdev
, tmp
, &pending_raid_disks
)
5186 if (super_90_load(rdev
, rdev0
, 0) >= 0) {
5187 printk(KERN_INFO
"md: adding %s ...\n",
5188 bdevname(rdev
->bdev
,b
));
5189 list_move(&rdev
->same_set
, &candidates
);
5192 * now we have a set of devices, with all of them having
5193 * mostly sane superblocks. It's time to allocate the
5197 dev
= MKDEV(mdp_major
,
5198 rdev0
->preferred_minor
<< MdpMinorShift
);
5199 unit
= MINOR(dev
) >> MdpMinorShift
;
5201 dev
= MKDEV(MD_MAJOR
, rdev0
->preferred_minor
);
5204 if (rdev0
->preferred_minor
!= unit
) {
5205 printk(KERN_INFO
"md: unit number in %s is bad: %d\n",
5206 bdevname(rdev0
->bdev
, b
), rdev0
->preferred_minor
);
5210 md_probe(dev
, NULL
, NULL
);
5211 mddev
= mddev_find(dev
);
5212 if (!mddev
|| !mddev
->gendisk
) {
5216 "md: cannot allocate memory for md drive.\n");
5219 if (mddev_lock(mddev
))
5220 printk(KERN_WARNING
"md: %s locked, cannot run\n",
5222 else if (mddev
->raid_disks
|| mddev
->major_version
5223 || !list_empty(&mddev
->disks
)) {
5225 "md: %s already running, cannot run %s\n",
5226 mdname(mddev
), bdevname(rdev0
->bdev
,b
));
5227 mddev_unlock(mddev
);
5229 printk(KERN_INFO
"md: created %s\n", mdname(mddev
));
5230 mddev
->persistent
= 1;
5231 rdev_for_each_list(rdev
, tmp
, &candidates
) {
5232 list_del_init(&rdev
->same_set
);
5233 if (bind_rdev_to_array(rdev
, mddev
))
5236 autorun_array(mddev
);
5237 mddev_unlock(mddev
);
5239 /* on success, candidates will be empty, on error
5242 rdev_for_each_list(rdev
, tmp
, &candidates
) {
5243 list_del_init(&rdev
->same_set
);
5248 printk(KERN_INFO
"md: ... autorun DONE.\n");
5250 #endif /* !MODULE */
5252 static int get_version(void __user
* arg
)
5256 ver
.major
= MD_MAJOR_VERSION
;
5257 ver
.minor
= MD_MINOR_VERSION
;
5258 ver
.patchlevel
= MD_PATCHLEVEL_VERSION
;
5260 if (copy_to_user(arg
, &ver
, sizeof(ver
)))
5266 static int get_array_info(mddev_t
* mddev
, void __user
* arg
)
5268 mdu_array_info_t info
;
5269 int nr
,working
,insync
,failed
,spare
;
5272 nr
=working
=insync
=failed
=spare
=0;
5273 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
5275 if (test_bit(Faulty
, &rdev
->flags
))
5279 if (test_bit(In_sync
, &rdev
->flags
))
5286 info
.major_version
= mddev
->major_version
;
5287 info
.minor_version
= mddev
->minor_version
;
5288 info
.patch_version
= MD_PATCHLEVEL_VERSION
;
5289 info
.ctime
= mddev
->ctime
;
5290 info
.level
= mddev
->level
;
5291 info
.size
= mddev
->dev_sectors
/ 2;
5292 if (info
.size
!= mddev
->dev_sectors
/ 2) /* overflow */
5295 info
.raid_disks
= mddev
->raid_disks
;
5296 info
.md_minor
= mddev
->md_minor
;
5297 info
.not_persistent
= !mddev
->persistent
;
5299 info
.utime
= mddev
->utime
;
5302 info
.state
= (1<<MD_SB_CLEAN
);
5303 if (mddev
->bitmap
&& mddev
->bitmap_info
.offset
)
5304 info
.state
= (1<<MD_SB_BITMAP_PRESENT
);
5305 info
.active_disks
= insync
;
5306 info
.working_disks
= working
;
5307 info
.failed_disks
= failed
;
5308 info
.spare_disks
= spare
;
5310 info
.layout
= mddev
->layout
;
5311 info
.chunk_size
= mddev
->chunk_sectors
<< 9;
5313 if (copy_to_user(arg
, &info
, sizeof(info
)))
5319 static int get_bitmap_file(mddev_t
* mddev
, void __user
* arg
)
5321 mdu_bitmap_file_t
*file
= NULL
; /* too big for stack allocation */
5322 char *ptr
, *buf
= NULL
;
5325 if (md_allow_write(mddev
))
5326 file
= kmalloc(sizeof(*file
), GFP_NOIO
);
5328 file
= kmalloc(sizeof(*file
), GFP_KERNEL
);
5333 /* bitmap disabled, zero the first byte and copy out */
5334 if (!mddev
->bitmap
|| !mddev
->bitmap
->file
) {
5335 file
->pathname
[0] = '\0';
5339 buf
= kmalloc(sizeof(file
->pathname
), GFP_KERNEL
);
5343 ptr
= d_path(&mddev
->bitmap
->file
->f_path
, buf
, sizeof(file
->pathname
));
5347 strcpy(file
->pathname
, ptr
);
5351 if (copy_to_user(arg
, file
, sizeof(*file
)))
5359 static int get_disk_info(mddev_t
* mddev
, void __user
* arg
)
5361 mdu_disk_info_t info
;
5364 if (copy_from_user(&info
, arg
, sizeof(info
)))
5367 rdev
= find_rdev_nr(mddev
, info
.number
);
5369 info
.major
= MAJOR(rdev
->bdev
->bd_dev
);
5370 info
.minor
= MINOR(rdev
->bdev
->bd_dev
);
5371 info
.raid_disk
= rdev
->raid_disk
;
5373 if (test_bit(Faulty
, &rdev
->flags
))
5374 info
.state
|= (1<<MD_DISK_FAULTY
);
5375 else if (test_bit(In_sync
, &rdev
->flags
)) {
5376 info
.state
|= (1<<MD_DISK_ACTIVE
);
5377 info
.state
|= (1<<MD_DISK_SYNC
);
5379 if (test_bit(WriteMostly
, &rdev
->flags
))
5380 info
.state
|= (1<<MD_DISK_WRITEMOSTLY
);
5382 info
.major
= info
.minor
= 0;
5383 info
.raid_disk
= -1;
5384 info
.state
= (1<<MD_DISK_REMOVED
);
5387 if (copy_to_user(arg
, &info
, sizeof(info
)))
5393 static int add_new_disk(mddev_t
* mddev
, mdu_disk_info_t
*info
)
5395 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
5397 dev_t dev
= MKDEV(info
->major
,info
->minor
);
5399 if (info
->major
!= MAJOR(dev
) || info
->minor
!= MINOR(dev
))
5402 if (!mddev
->raid_disks
) {
5404 /* expecting a device which has a superblock */
5405 rdev
= md_import_device(dev
, mddev
->major_version
, mddev
->minor_version
);
5408 "md: md_import_device returned %ld\n",
5410 return PTR_ERR(rdev
);
5412 if (!list_empty(&mddev
->disks
)) {
5413 mdk_rdev_t
*rdev0
= list_entry(mddev
->disks
.next
,
5414 mdk_rdev_t
, same_set
);
5415 err
= super_types
[mddev
->major_version
]
5416 .load_super(rdev
, rdev0
, mddev
->minor_version
);
5419 "md: %s has different UUID to %s\n",
5420 bdevname(rdev
->bdev
,b
),
5421 bdevname(rdev0
->bdev
,b2
));
5426 err
= bind_rdev_to_array(rdev
, mddev
);
5433 * add_new_disk can be used once the array is assembled
5434 * to add "hot spares". They must already have a superblock
5439 if (!mddev
->pers
->hot_add_disk
) {
5441 "%s: personality does not support diskops!\n",
5445 if (mddev
->persistent
)
5446 rdev
= md_import_device(dev
, mddev
->major_version
,
5447 mddev
->minor_version
);
5449 rdev
= md_import_device(dev
, -1, -1);
5452 "md: md_import_device returned %ld\n",
5454 return PTR_ERR(rdev
);
5456 /* set saved_raid_disk if appropriate */
5457 if (!mddev
->persistent
) {
5458 if (info
->state
& (1<<MD_DISK_SYNC
) &&
5459 info
->raid_disk
< mddev
->raid_disks
) {
5460 rdev
->raid_disk
= info
->raid_disk
;
5461 set_bit(In_sync
, &rdev
->flags
);
5463 rdev
->raid_disk
= -1;
5465 super_types
[mddev
->major_version
].
5466 validate_super(mddev
, rdev
);
5467 if ((info
->state
& (1<<MD_DISK_SYNC
)) &&
5468 (!test_bit(In_sync
, &rdev
->flags
) ||
5469 rdev
->raid_disk
!= info
->raid_disk
)) {
5470 /* This was a hot-add request, but events doesn't
5471 * match, so reject it.
5477 if (test_bit(In_sync
, &rdev
->flags
))
5478 rdev
->saved_raid_disk
= rdev
->raid_disk
;
5480 rdev
->saved_raid_disk
= -1;
5482 clear_bit(In_sync
, &rdev
->flags
); /* just to be sure */
5483 if (info
->state
& (1<<MD_DISK_WRITEMOSTLY
))
5484 set_bit(WriteMostly
, &rdev
->flags
);
5486 clear_bit(WriteMostly
, &rdev
->flags
);
5488 rdev
->raid_disk
= -1;
5489 err
= bind_rdev_to_array(rdev
, mddev
);
5490 if (!err
&& !mddev
->pers
->hot_remove_disk
) {
5491 /* If there is hot_add_disk but no hot_remove_disk
5492 * then added disks for geometry changes,
5493 * and should be added immediately.
5495 super_types
[mddev
->major_version
].
5496 validate_super(mddev
, rdev
);
5497 err
= mddev
->pers
->hot_add_disk(mddev
, rdev
);
5499 unbind_rdev_from_array(rdev
);
5504 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
5506 md_update_sb(mddev
, 1);
5507 if (mddev
->degraded
)
5508 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
5509 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5511 md_new_event(mddev
);
5512 md_wakeup_thread(mddev
->thread
);
5516 /* otherwise, add_new_disk is only allowed
5517 * for major_version==0 superblocks
5519 if (mddev
->major_version
!= 0) {
5520 printk(KERN_WARNING
"%s: ADD_NEW_DISK not supported\n",
5525 if (!(info
->state
& (1<<MD_DISK_FAULTY
))) {
5527 rdev
= md_import_device(dev
, -1, 0);
5530 "md: error, md_import_device() returned %ld\n",
5532 return PTR_ERR(rdev
);
5534 rdev
->desc_nr
= info
->number
;
5535 if (info
->raid_disk
< mddev
->raid_disks
)
5536 rdev
->raid_disk
= info
->raid_disk
;
5538 rdev
->raid_disk
= -1;
5540 if (rdev
->raid_disk
< mddev
->raid_disks
)
5541 if (info
->state
& (1<<MD_DISK_SYNC
))
5542 set_bit(In_sync
, &rdev
->flags
);
5544 if (info
->state
& (1<<MD_DISK_WRITEMOSTLY
))
5545 set_bit(WriteMostly
, &rdev
->flags
);
5547 if (!mddev
->persistent
) {
5548 printk(KERN_INFO
"md: nonpersistent superblock ...\n");
5549 rdev
->sb_start
= i_size_read(rdev
->bdev
->bd_inode
) / 512;
5551 rdev
->sb_start
= calc_dev_sboffset(rdev
);
5552 rdev
->sectors
= rdev
->sb_start
;
5554 err
= bind_rdev_to_array(rdev
, mddev
);
5564 static int hot_remove_disk(mddev_t
* mddev
, dev_t dev
)
5566 char b
[BDEVNAME_SIZE
];
5569 rdev
= find_rdev(mddev
, dev
);
5573 if (rdev
->raid_disk
>= 0)
5576 kick_rdev_from_array(rdev
);
5577 md_update_sb(mddev
, 1);
5578 md_new_event(mddev
);
5582 printk(KERN_WARNING
"md: cannot remove active disk %s from %s ...\n",
5583 bdevname(rdev
->bdev
,b
), mdname(mddev
));
5587 static int hot_add_disk(mddev_t
* mddev
, dev_t dev
)
5589 char b
[BDEVNAME_SIZE
];
5596 if (mddev
->major_version
!= 0) {
5597 printk(KERN_WARNING
"%s: HOT_ADD may only be used with"
5598 " version-0 superblocks.\n",
5602 if (!mddev
->pers
->hot_add_disk
) {
5604 "%s: personality does not support diskops!\n",
5609 rdev
= md_import_device(dev
, -1, 0);
5612 "md: error, md_import_device() returned %ld\n",
5617 if (mddev
->persistent
)
5618 rdev
->sb_start
= calc_dev_sboffset(rdev
);
5620 rdev
->sb_start
= i_size_read(rdev
->bdev
->bd_inode
) / 512;
5622 rdev
->sectors
= rdev
->sb_start
;
5624 if (test_bit(Faulty
, &rdev
->flags
)) {
5626 "md: can not hot-add faulty %s disk to %s!\n",
5627 bdevname(rdev
->bdev
,b
), mdname(mddev
));
5631 clear_bit(In_sync
, &rdev
->flags
);
5633 rdev
->saved_raid_disk
= -1;
5634 err
= bind_rdev_to_array(rdev
, mddev
);
5639 * The rest should better be atomic, we can have disk failures
5640 * noticed in interrupt contexts ...
5643 rdev
->raid_disk
= -1;
5645 md_update_sb(mddev
, 1);
5648 * Kick recovery, maybe this spare has to be added to the
5649 * array immediately.
5651 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5652 md_wakeup_thread(mddev
->thread
);
5653 md_new_event(mddev
);
5661 static int set_bitmap_file(mddev_t
*mddev
, int fd
)
5666 if (!mddev
->pers
->quiesce
)
5668 if (mddev
->recovery
|| mddev
->sync_thread
)
5670 /* we should be able to change the bitmap.. */
5676 return -EEXIST
; /* cannot add when bitmap is present */
5677 mddev
->bitmap_info
.file
= fget(fd
);
5679 if (mddev
->bitmap_info
.file
== NULL
) {
5680 printk(KERN_ERR
"%s: error: failed to get bitmap file\n",
5685 err
= deny_bitmap_write_access(mddev
->bitmap_info
.file
);
5687 printk(KERN_ERR
"%s: error: bitmap file is already in use\n",
5689 fput(mddev
->bitmap_info
.file
);
5690 mddev
->bitmap_info
.file
= NULL
;
5693 mddev
->bitmap_info
.offset
= 0; /* file overrides offset */
5694 } else if (mddev
->bitmap
== NULL
)
5695 return -ENOENT
; /* cannot remove what isn't there */
5698 mddev
->pers
->quiesce(mddev
, 1);
5700 err
= bitmap_create(mddev
);
5702 err
= bitmap_load(mddev
);
5704 if (fd
< 0 || err
) {
5705 bitmap_destroy(mddev
);
5706 fd
= -1; /* make sure to put the file */
5708 mddev
->pers
->quiesce(mddev
, 0);
5711 if (mddev
->bitmap_info
.file
) {
5712 restore_bitmap_write_access(mddev
->bitmap_info
.file
);
5713 fput(mddev
->bitmap_info
.file
);
5715 mddev
->bitmap_info
.file
= NULL
;
5722 * set_array_info is used two different ways
5723 * The original usage is when creating a new array.
5724 * In this usage, raid_disks is > 0 and it together with
5725 * level, size, not_persistent,layout,chunksize determine the
5726 * shape of the array.
5727 * This will always create an array with a type-0.90.0 superblock.
5728 * The newer usage is when assembling an array.
5729 * In this case raid_disks will be 0, and the major_version field is
5730 * use to determine which style super-blocks are to be found on the devices.
5731 * The minor and patch _version numbers are also kept incase the
5732 * super_block handler wishes to interpret them.
5734 static int set_array_info(mddev_t
* mddev
, mdu_array_info_t
*info
)
5737 if (info
->raid_disks
== 0) {
5738 /* just setting version number for superblock loading */
5739 if (info
->major_version
< 0 ||
5740 info
->major_version
>= ARRAY_SIZE(super_types
) ||
5741 super_types
[info
->major_version
].name
== NULL
) {
5742 /* maybe try to auto-load a module? */
5744 "md: superblock version %d not known\n",
5745 info
->major_version
);
5748 mddev
->major_version
= info
->major_version
;
5749 mddev
->minor_version
= info
->minor_version
;
5750 mddev
->patch_version
= info
->patch_version
;
5751 mddev
->persistent
= !info
->not_persistent
;
5752 /* ensure mddev_put doesn't delete this now that there
5753 * is some minimal configuration.
5755 mddev
->ctime
= get_seconds();
5758 mddev
->major_version
= MD_MAJOR_VERSION
;
5759 mddev
->minor_version
= MD_MINOR_VERSION
;
5760 mddev
->patch_version
= MD_PATCHLEVEL_VERSION
;
5761 mddev
->ctime
= get_seconds();
5763 mddev
->level
= info
->level
;
5764 mddev
->clevel
[0] = 0;
5765 mddev
->dev_sectors
= 2 * (sector_t
)info
->size
;
5766 mddev
->raid_disks
= info
->raid_disks
;
5767 /* don't set md_minor, it is determined by which /dev/md* was
5770 if (info
->state
& (1<<MD_SB_CLEAN
))
5771 mddev
->recovery_cp
= MaxSector
;
5773 mddev
->recovery_cp
= 0;
5774 mddev
->persistent
= ! info
->not_persistent
;
5775 mddev
->external
= 0;
5777 mddev
->layout
= info
->layout
;
5778 mddev
->chunk_sectors
= info
->chunk_size
>> 9;
5780 mddev
->max_disks
= MD_SB_DISKS
;
5782 if (mddev
->persistent
)
5784 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
5786 mddev
->bitmap_info
.default_offset
= MD_SB_BYTES
>> 9;
5787 mddev
->bitmap_info
.offset
= 0;
5789 mddev
->reshape_position
= MaxSector
;
5792 * Generate a 128 bit UUID
5794 get_random_bytes(mddev
->uuid
, 16);
5796 mddev
->new_level
= mddev
->level
;
5797 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
5798 mddev
->new_layout
= mddev
->layout
;
5799 mddev
->delta_disks
= 0;
5804 void md_set_array_sectors(mddev_t
*mddev
, sector_t array_sectors
)
5806 WARN(!mddev_is_locked(mddev
), "%s: unlocked mddev!\n", __func__
);
5808 if (mddev
->external_size
)
5811 mddev
->array_sectors
= array_sectors
;
5813 EXPORT_SYMBOL(md_set_array_sectors
);
5815 static int update_size(mddev_t
*mddev
, sector_t num_sectors
)
5819 int fit
= (num_sectors
== 0);
5821 if (mddev
->pers
->resize
== NULL
)
5823 /* The "num_sectors" is the number of sectors of each device that
5824 * is used. This can only make sense for arrays with redundancy.
5825 * linear and raid0 always use whatever space is available. We can only
5826 * consider changing this number if no resync or reconstruction is
5827 * happening, and if the new size is acceptable. It must fit before the
5828 * sb_start or, if that is <data_offset, it must fit before the size
5829 * of each device. If num_sectors is zero, we find the largest size
5832 if (mddev
->sync_thread
)
5835 /* Sorry, cannot grow a bitmap yet, just remove it,
5839 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
5840 sector_t avail
= rdev
->sectors
;
5842 if (fit
&& (num_sectors
== 0 || num_sectors
> avail
))
5843 num_sectors
= avail
;
5844 if (avail
< num_sectors
)
5847 rv
= mddev
->pers
->resize(mddev
, num_sectors
);
5849 revalidate_disk(mddev
->gendisk
);
5853 static int update_raid_disks(mddev_t
*mddev
, int raid_disks
)
5856 /* change the number of raid disks */
5857 if (mddev
->pers
->check_reshape
== NULL
)
5859 if (raid_disks
<= 0 ||
5860 (mddev
->max_disks
&& raid_disks
>= mddev
->max_disks
))
5862 if (mddev
->sync_thread
|| mddev
->reshape_position
!= MaxSector
)
5864 mddev
->delta_disks
= raid_disks
- mddev
->raid_disks
;
5866 rv
= mddev
->pers
->check_reshape(mddev
);
5868 mddev
->delta_disks
= 0;
5874 * update_array_info is used to change the configuration of an
5876 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
5877 * fields in the info are checked against the array.
5878 * Any differences that cannot be handled will cause an error.
5879 * Normally, only one change can be managed at a time.
5881 static int update_array_info(mddev_t
*mddev
, mdu_array_info_t
*info
)
5887 /* calculate expected state,ignoring low bits */
5888 if (mddev
->bitmap
&& mddev
->bitmap_info
.offset
)
5889 state
|= (1 << MD_SB_BITMAP_PRESENT
);
5891 if (mddev
->major_version
!= info
->major_version
||
5892 mddev
->minor_version
!= info
->minor_version
||
5893 /* mddev->patch_version != info->patch_version || */
5894 mddev
->ctime
!= info
->ctime
||
5895 mddev
->level
!= info
->level
||
5896 /* mddev->layout != info->layout || */
5897 !mddev
->persistent
!= info
->not_persistent
||
5898 mddev
->chunk_sectors
!= info
->chunk_size
>> 9 ||
5899 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
5900 ((state
^info
->state
) & 0xfffffe00)
5903 /* Check there is only one change */
5904 if (info
->size
>= 0 && mddev
->dev_sectors
/ 2 != info
->size
)
5906 if (mddev
->raid_disks
!= info
->raid_disks
)
5908 if (mddev
->layout
!= info
->layout
)
5910 if ((state
^ info
->state
) & (1<<MD_SB_BITMAP_PRESENT
))
5917 if (mddev
->layout
!= info
->layout
) {
5919 * we don't need to do anything at the md level, the
5920 * personality will take care of it all.
5922 if (mddev
->pers
->check_reshape
== NULL
)
5925 mddev
->new_layout
= info
->layout
;
5926 rv
= mddev
->pers
->check_reshape(mddev
);
5928 mddev
->new_layout
= mddev
->layout
;
5932 if (info
->size
>= 0 && mddev
->dev_sectors
/ 2 != info
->size
)
5933 rv
= update_size(mddev
, (sector_t
)info
->size
* 2);
5935 if (mddev
->raid_disks
!= info
->raid_disks
)
5936 rv
= update_raid_disks(mddev
, info
->raid_disks
);
5938 if ((state
^ info
->state
) & (1<<MD_SB_BITMAP_PRESENT
)) {
5939 if (mddev
->pers
->quiesce
== NULL
)
5941 if (mddev
->recovery
|| mddev
->sync_thread
)
5943 if (info
->state
& (1<<MD_SB_BITMAP_PRESENT
)) {
5944 /* add the bitmap */
5947 if (mddev
->bitmap_info
.default_offset
== 0)
5949 mddev
->bitmap_info
.offset
=
5950 mddev
->bitmap_info
.default_offset
;
5951 mddev
->pers
->quiesce(mddev
, 1);
5952 rv
= bitmap_create(mddev
);
5954 rv
= bitmap_load(mddev
);
5956 bitmap_destroy(mddev
);
5957 mddev
->pers
->quiesce(mddev
, 0);
5959 /* remove the bitmap */
5962 if (mddev
->bitmap
->file
)
5964 mddev
->pers
->quiesce(mddev
, 1);
5965 bitmap_destroy(mddev
);
5966 mddev
->pers
->quiesce(mddev
, 0);
5967 mddev
->bitmap_info
.offset
= 0;
5970 md_update_sb(mddev
, 1);
5974 static int set_disk_faulty(mddev_t
*mddev
, dev_t dev
)
5978 if (mddev
->pers
== NULL
)
5981 rdev
= find_rdev(mddev
, dev
);
5985 md_error(mddev
, rdev
);
5990 * We have a problem here : there is no easy way to give a CHS
5991 * virtual geometry. We currently pretend that we have a 2 heads
5992 * 4 sectors (with a BIG number of cylinders...). This drives
5993 * dosfs just mad... ;-)
5995 static int md_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
5997 mddev_t
*mddev
= bdev
->bd_disk
->private_data
;
6001 geo
->cylinders
= mddev
->array_sectors
/ 8;
6005 static int md_ioctl(struct block_device
*bdev
, fmode_t mode
,
6006 unsigned int cmd
, unsigned long arg
)
6009 void __user
*argp
= (void __user
*)arg
;
6010 mddev_t
*mddev
= NULL
;
6013 if (!capable(CAP_SYS_ADMIN
))
6017 * Commands dealing with the RAID driver but not any
6023 err
= get_version(argp
);
6026 case PRINT_RAID_DEBUG
:
6034 autostart_arrays(arg
);
6041 * Commands creating/starting a new array:
6044 mddev
= bdev
->bd_disk
->private_data
;
6051 err
= mddev_lock(mddev
);
6054 "md: ioctl lock interrupted, reason %d, cmd %d\n",
6061 case SET_ARRAY_INFO
:
6063 mdu_array_info_t info
;
6065 memset(&info
, 0, sizeof(info
));
6066 else if (copy_from_user(&info
, argp
, sizeof(info
))) {
6071 err
= update_array_info(mddev
, &info
);
6073 printk(KERN_WARNING
"md: couldn't update"
6074 " array info. %d\n", err
);
6079 if (!list_empty(&mddev
->disks
)) {
6081 "md: array %s already has disks!\n",
6086 if (mddev
->raid_disks
) {
6088 "md: array %s already initialised!\n",
6093 err
= set_array_info(mddev
, &info
);
6095 printk(KERN_WARNING
"md: couldn't set"
6096 " array info. %d\n", err
);
6106 * Commands querying/configuring an existing array:
6108 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
6109 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
6110 if ((!mddev
->raid_disks
&& !mddev
->external
)
6111 && cmd
!= ADD_NEW_DISK
&& cmd
!= STOP_ARRAY
6112 && cmd
!= RUN_ARRAY
&& cmd
!= SET_BITMAP_FILE
6113 && cmd
!= GET_BITMAP_FILE
) {
6119 * Commands even a read-only array can execute:
6123 case GET_ARRAY_INFO
:
6124 err
= get_array_info(mddev
, argp
);
6127 case GET_BITMAP_FILE
:
6128 err
= get_bitmap_file(mddev
, argp
);
6132 err
= get_disk_info(mddev
, argp
);
6135 case RESTART_ARRAY_RW
:
6136 err
= restart_array(mddev
);
6140 err
= do_md_stop(mddev
, 0, 1);
6144 err
= md_set_readonly(mddev
, 1);
6148 if (get_user(ro
, (int __user
*)(arg
))) {
6154 /* if the bdev is going readonly the value of mddev->ro
6155 * does not matter, no writes are coming
6160 /* are we are already prepared for writes? */
6164 /* transitioning to readauto need only happen for
6165 * arrays that call md_write_start
6168 err
= restart_array(mddev
);
6171 set_disk_ro(mddev
->gendisk
, 0);
6178 * The remaining ioctls are changing the state of the
6179 * superblock, so we do not allow them on read-only arrays.
6180 * However non-MD ioctls (e.g. get-size) will still come through
6181 * here and hit the 'default' below, so only disallow
6182 * 'md' ioctls, and switch to rw mode if started auto-readonly.
6184 if (_IOC_TYPE(cmd
) == MD_MAJOR
&& mddev
->ro
&& mddev
->pers
) {
6185 if (mddev
->ro
== 2) {
6187 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
6188 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6189 md_wakeup_thread(mddev
->thread
);
6200 mdu_disk_info_t info
;
6201 if (copy_from_user(&info
, argp
, sizeof(info
)))
6204 err
= add_new_disk(mddev
, &info
);
6208 case HOT_REMOVE_DISK
:
6209 err
= hot_remove_disk(mddev
, new_decode_dev(arg
));
6213 err
= hot_add_disk(mddev
, new_decode_dev(arg
));
6216 case SET_DISK_FAULTY
:
6217 err
= set_disk_faulty(mddev
, new_decode_dev(arg
));
6221 err
= do_md_run(mddev
);
6224 case SET_BITMAP_FILE
:
6225 err
= set_bitmap_file(mddev
, (int)arg
);
6235 if (mddev
->hold_active
== UNTIL_IOCTL
&&
6237 mddev
->hold_active
= 0;
6238 mddev_unlock(mddev
);
6247 #ifdef CONFIG_COMPAT
6248 static int md_compat_ioctl(struct block_device
*bdev
, fmode_t mode
,
6249 unsigned int cmd
, unsigned long arg
)
6252 case HOT_REMOVE_DISK
:
6254 case SET_DISK_FAULTY
:
6255 case SET_BITMAP_FILE
:
6256 /* These take in integer arg, do not convert */
6259 arg
= (unsigned long)compat_ptr(arg
);
6263 return md_ioctl(bdev
, mode
, cmd
, arg
);
6265 #endif /* CONFIG_COMPAT */
6267 static int md_open(struct block_device
*bdev
, fmode_t mode
)
6270 * Succeed if we can lock the mddev, which confirms that
6271 * it isn't being stopped right now.
6273 mddev_t
*mddev
= mddev_find(bdev
->bd_dev
);
6276 if (mddev
->gendisk
!= bdev
->bd_disk
) {
6277 /* we are racing with mddev_put which is discarding this
6281 /* Wait until bdev->bd_disk is definitely gone */
6282 flush_workqueue(md_misc_wq
);
6283 /* Then retry the open from the top */
6284 return -ERESTARTSYS
;
6286 BUG_ON(mddev
!= bdev
->bd_disk
->private_data
);
6288 if ((err
= mutex_lock_interruptible(&mddev
->open_mutex
)))
6292 atomic_inc(&mddev
->openers
);
6293 mutex_unlock(&mddev
->open_mutex
);
6295 check_disk_change(bdev
);
6300 static int md_release(struct gendisk
*disk
, fmode_t mode
)
6302 mddev_t
*mddev
= disk
->private_data
;
6305 atomic_dec(&mddev
->openers
);
6311 static int md_media_changed(struct gendisk
*disk
)
6313 mddev_t
*mddev
= disk
->private_data
;
6315 return mddev
->changed
;
6318 static int md_revalidate(struct gendisk
*disk
)
6320 mddev_t
*mddev
= disk
->private_data
;
6325 static const struct block_device_operations md_fops
=
6327 .owner
= THIS_MODULE
,
6329 .release
= md_release
,
6331 #ifdef CONFIG_COMPAT
6332 .compat_ioctl
= md_compat_ioctl
,
6334 .getgeo
= md_getgeo
,
6335 .media_changed
= md_media_changed
,
6336 .revalidate_disk
= md_revalidate
,
6339 static int md_thread(void * arg
)
6341 mdk_thread_t
*thread
= arg
;
6344 * md_thread is a 'system-thread', it's priority should be very
6345 * high. We avoid resource deadlocks individually in each
6346 * raid personality. (RAID5 does preallocation) We also use RR and
6347 * the very same RT priority as kswapd, thus we will never get
6348 * into a priority inversion deadlock.
6350 * we definitely have to have equal or higher priority than
6351 * bdflush, otherwise bdflush will deadlock if there are too
6352 * many dirty RAID5 blocks.
6355 allow_signal(SIGKILL
);
6356 while (!kthread_should_stop()) {
6358 /* We need to wait INTERRUPTIBLE so that
6359 * we don't add to the load-average.
6360 * That means we need to be sure no signals are
6363 if (signal_pending(current
))
6364 flush_signals(current
);
6366 wait_event_interruptible_timeout
6368 test_bit(THREAD_WAKEUP
, &thread
->flags
)
6369 || kthread_should_stop(),
6372 clear_bit(THREAD_WAKEUP
, &thread
->flags
);
6373 if (!kthread_should_stop())
6374 thread
->run(thread
->mddev
);
6380 void md_wakeup_thread(mdk_thread_t
*thread
)
6383 dprintk("md: waking up MD thread %s.\n", thread
->tsk
->comm
);
6384 set_bit(THREAD_WAKEUP
, &thread
->flags
);
6385 wake_up(&thread
->wqueue
);
6389 mdk_thread_t
*md_register_thread(void (*run
) (mddev_t
*), mddev_t
*mddev
,
6392 mdk_thread_t
*thread
;
6394 thread
= kzalloc(sizeof(mdk_thread_t
), GFP_KERNEL
);
6398 init_waitqueue_head(&thread
->wqueue
);
6401 thread
->mddev
= mddev
;
6402 thread
->timeout
= MAX_SCHEDULE_TIMEOUT
;
6403 thread
->tsk
= kthread_run(md_thread
, thread
,
6405 mdname(thread
->mddev
),
6406 name
?: mddev
->pers
->name
);
6407 if (IS_ERR(thread
->tsk
)) {
6414 void md_unregister_thread(mdk_thread_t
*thread
)
6418 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread
->tsk
));
6420 kthread_stop(thread
->tsk
);
6424 void md_error(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
6431 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
))
6434 if (!mddev
->pers
|| !mddev
->pers
->error_handler
)
6436 mddev
->pers
->error_handler(mddev
,rdev
);
6437 if (mddev
->degraded
)
6438 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
6439 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
6440 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6441 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6442 md_wakeup_thread(mddev
->thread
);
6443 if (mddev
->event_work
.func
)
6444 queue_work(md_misc_wq
, &mddev
->event_work
);
6445 md_new_event_inintr(mddev
);
6448 /* seq_file implementation /proc/mdstat */
6450 static void status_unused(struct seq_file
*seq
)
6455 seq_printf(seq
, "unused devices: ");
6457 list_for_each_entry(rdev
, &pending_raid_disks
, same_set
) {
6458 char b
[BDEVNAME_SIZE
];
6460 seq_printf(seq
, "%s ",
6461 bdevname(rdev
->bdev
,b
));
6464 seq_printf(seq
, "<none>");
6466 seq_printf(seq
, "\n");
6470 static void status_resync(struct seq_file
*seq
, mddev_t
* mddev
)
6472 sector_t max_sectors
, resync
, res
;
6473 unsigned long dt
, db
;
6476 unsigned int per_milli
;
6478 resync
= mddev
->curr_resync
- atomic_read(&mddev
->recovery_active
);
6480 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
6481 max_sectors
= mddev
->resync_max_sectors
;
6483 max_sectors
= mddev
->dev_sectors
;
6486 * Should not happen.
6492 /* Pick 'scale' such that (resync>>scale)*1000 will fit
6493 * in a sector_t, and (max_sectors>>scale) will fit in a
6494 * u32, as those are the requirements for sector_div.
6495 * Thus 'scale' must be at least 10
6498 if (sizeof(sector_t
) > sizeof(unsigned long)) {
6499 while ( max_sectors
/2 > (1ULL<<(scale
+32)))
6502 res
= (resync
>>scale
)*1000;
6503 sector_div(res
, (u32
)((max_sectors
>>scale
)+1));
6507 int i
, x
= per_milli
/50, y
= 20-x
;
6508 seq_printf(seq
, "[");
6509 for (i
= 0; i
< x
; i
++)
6510 seq_printf(seq
, "=");
6511 seq_printf(seq
, ">");
6512 for (i
= 0; i
< y
; i
++)
6513 seq_printf(seq
, ".");
6514 seq_printf(seq
, "] ");
6516 seq_printf(seq
, " %s =%3u.%u%% (%llu/%llu)",
6517 (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
)?
6519 (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
)?
6521 (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ?
6522 "resync" : "recovery"))),
6523 per_milli
/10, per_milli
% 10,
6524 (unsigned long long) resync
/2,
6525 (unsigned long long) max_sectors
/2);
6528 * dt: time from mark until now
6529 * db: blocks written from mark until now
6530 * rt: remaining time
6532 * rt is a sector_t, so could be 32bit or 64bit.
6533 * So we divide before multiply in case it is 32bit and close
6535 * We scale the divisor (db) by 32 to avoid losing precision
6536 * near the end of resync when the number of remaining sectors
6538 * We then divide rt by 32 after multiplying by db to compensate.
6539 * The '+1' avoids division by zero if db is very small.
6541 dt
= ((jiffies
- mddev
->resync_mark
) / HZ
);
6543 db
= (mddev
->curr_mark_cnt
- atomic_read(&mddev
->recovery_active
))
6544 - mddev
->resync_mark_cnt
;
6546 rt
= max_sectors
- resync
; /* number of remaining sectors */
6547 sector_div(rt
, db
/32+1);
6551 seq_printf(seq
, " finish=%lu.%lumin", (unsigned long)rt
/ 60,
6552 ((unsigned long)rt
% 60)/6);
6554 seq_printf(seq
, " speed=%ldK/sec", db
/2/dt
);
6557 static void *md_seq_start(struct seq_file
*seq
, loff_t
*pos
)
6559 struct list_head
*tmp
;
6569 spin_lock(&all_mddevs_lock
);
6570 list_for_each(tmp
,&all_mddevs
)
6572 mddev
= list_entry(tmp
, mddev_t
, all_mddevs
);
6574 spin_unlock(&all_mddevs_lock
);
6577 spin_unlock(&all_mddevs_lock
);
6579 return (void*)2;/* tail */
6583 static void *md_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
6585 struct list_head
*tmp
;
6586 mddev_t
*next_mddev
, *mddev
= v
;
6592 spin_lock(&all_mddevs_lock
);
6594 tmp
= all_mddevs
.next
;
6596 tmp
= mddev
->all_mddevs
.next
;
6597 if (tmp
!= &all_mddevs
)
6598 next_mddev
= mddev_get(list_entry(tmp
,mddev_t
,all_mddevs
));
6600 next_mddev
= (void*)2;
6603 spin_unlock(&all_mddevs_lock
);
6611 static void md_seq_stop(struct seq_file
*seq
, void *v
)
6615 if (mddev
&& v
!= (void*)1 && v
!= (void*)2)
6619 static int md_seq_show(struct seq_file
*seq
, void *v
)
6624 struct bitmap
*bitmap
;
6626 if (v
== (void*)1) {
6627 struct mdk_personality
*pers
;
6628 seq_printf(seq
, "Personalities : ");
6629 spin_lock(&pers_lock
);
6630 list_for_each_entry(pers
, &pers_list
, list
)
6631 seq_printf(seq
, "[%s] ", pers
->name
);
6633 spin_unlock(&pers_lock
);
6634 seq_printf(seq
, "\n");
6635 seq
->poll_event
= atomic_read(&md_event_count
);
6638 if (v
== (void*)2) {
6643 if (mddev_lock(mddev
) < 0)
6646 if (mddev
->pers
|| mddev
->raid_disks
|| !list_empty(&mddev
->disks
)) {
6647 seq_printf(seq
, "%s : %sactive", mdname(mddev
),
6648 mddev
->pers
? "" : "in");
6651 seq_printf(seq
, " (read-only)");
6653 seq_printf(seq
, " (auto-read-only)");
6654 seq_printf(seq
, " %s", mddev
->pers
->name
);
6658 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
6659 char b
[BDEVNAME_SIZE
];
6660 seq_printf(seq
, " %s[%d]",
6661 bdevname(rdev
->bdev
,b
), rdev
->desc_nr
);
6662 if (test_bit(WriteMostly
, &rdev
->flags
))
6663 seq_printf(seq
, "(W)");
6664 if (test_bit(Faulty
, &rdev
->flags
)) {
6665 seq_printf(seq
, "(F)");
6667 } else if (rdev
->raid_disk
< 0)
6668 seq_printf(seq
, "(S)"); /* spare */
6669 sectors
+= rdev
->sectors
;
6672 if (!list_empty(&mddev
->disks
)) {
6674 seq_printf(seq
, "\n %llu blocks",
6675 (unsigned long long)
6676 mddev
->array_sectors
/ 2);
6678 seq_printf(seq
, "\n %llu blocks",
6679 (unsigned long long)sectors
/ 2);
6681 if (mddev
->persistent
) {
6682 if (mddev
->major_version
!= 0 ||
6683 mddev
->minor_version
!= 90) {
6684 seq_printf(seq
," super %d.%d",
6685 mddev
->major_version
,
6686 mddev
->minor_version
);
6688 } else if (mddev
->external
)
6689 seq_printf(seq
, " super external:%s",
6690 mddev
->metadata_type
);
6692 seq_printf(seq
, " super non-persistent");
6695 mddev
->pers
->status(seq
, mddev
);
6696 seq_printf(seq
, "\n ");
6697 if (mddev
->pers
->sync_request
) {
6698 if (mddev
->curr_resync
> 2) {
6699 status_resync(seq
, mddev
);
6700 seq_printf(seq
, "\n ");
6701 } else if (mddev
->curr_resync
== 1 || mddev
->curr_resync
== 2)
6702 seq_printf(seq
, "\tresync=DELAYED\n ");
6703 else if (mddev
->recovery_cp
< MaxSector
)
6704 seq_printf(seq
, "\tresync=PENDING\n ");
6707 seq_printf(seq
, "\n ");
6709 if ((bitmap
= mddev
->bitmap
)) {
6710 unsigned long chunk_kb
;
6711 unsigned long flags
;
6712 spin_lock_irqsave(&bitmap
->lock
, flags
);
6713 chunk_kb
= mddev
->bitmap_info
.chunksize
>> 10;
6714 seq_printf(seq
, "bitmap: %lu/%lu pages [%luKB], "
6716 bitmap
->pages
- bitmap
->missing_pages
,
6718 (bitmap
->pages
- bitmap
->missing_pages
)
6719 << (PAGE_SHIFT
- 10),
6720 chunk_kb
? chunk_kb
: mddev
->bitmap_info
.chunksize
,
6721 chunk_kb
? "KB" : "B");
6723 seq_printf(seq
, ", file: ");
6724 seq_path(seq
, &bitmap
->file
->f_path
, " \t\n");
6727 seq_printf(seq
, "\n");
6728 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
6731 seq_printf(seq
, "\n");
6733 mddev_unlock(mddev
);
6738 static const struct seq_operations md_seq_ops
= {
6739 .start
= md_seq_start
,
6740 .next
= md_seq_next
,
6741 .stop
= md_seq_stop
,
6742 .show
= md_seq_show
,
6745 static int md_seq_open(struct inode
*inode
, struct file
*file
)
6747 struct seq_file
*seq
;
6750 error
= seq_open(file
, &md_seq_ops
);
6754 seq
= file
->private_data
;
6755 seq
->poll_event
= atomic_read(&md_event_count
);
6759 static unsigned int mdstat_poll(struct file
*filp
, poll_table
*wait
)
6761 struct seq_file
*seq
= filp
->private_data
;
6764 poll_wait(filp
, &md_event_waiters
, wait
);
6766 /* always allow read */
6767 mask
= POLLIN
| POLLRDNORM
;
6769 if (seq
->poll_event
!= atomic_read(&md_event_count
))
6770 mask
|= POLLERR
| POLLPRI
;
6774 static const struct file_operations md_seq_fops
= {
6775 .owner
= THIS_MODULE
,
6776 .open
= md_seq_open
,
6778 .llseek
= seq_lseek
,
6779 .release
= seq_release_private
,
6780 .poll
= mdstat_poll
,
6783 int register_md_personality(struct mdk_personality
*p
)
6785 spin_lock(&pers_lock
);
6786 list_add_tail(&p
->list
, &pers_list
);
6787 printk(KERN_INFO
"md: %s personality registered for level %d\n", p
->name
, p
->level
);
6788 spin_unlock(&pers_lock
);
6792 int unregister_md_personality(struct mdk_personality
*p
)
6794 printk(KERN_INFO
"md: %s personality unregistered\n", p
->name
);
6795 spin_lock(&pers_lock
);
6796 list_del_init(&p
->list
);
6797 spin_unlock(&pers_lock
);
6801 static int is_mddev_idle(mddev_t
*mddev
, int init
)
6809 rdev_for_each_rcu(rdev
, mddev
) {
6810 struct gendisk
*disk
= rdev
->bdev
->bd_contains
->bd_disk
;
6811 curr_events
= (int)part_stat_read(&disk
->part0
, sectors
[0]) +
6812 (int)part_stat_read(&disk
->part0
, sectors
[1]) -
6813 atomic_read(&disk
->sync_io
);
6814 /* sync IO will cause sync_io to increase before the disk_stats
6815 * as sync_io is counted when a request starts, and
6816 * disk_stats is counted when it completes.
6817 * So resync activity will cause curr_events to be smaller than
6818 * when there was no such activity.
6819 * non-sync IO will cause disk_stat to increase without
6820 * increasing sync_io so curr_events will (eventually)
6821 * be larger than it was before. Once it becomes
6822 * substantially larger, the test below will cause
6823 * the array to appear non-idle, and resync will slow
6825 * If there is a lot of outstanding resync activity when
6826 * we set last_event to curr_events, then all that activity
6827 * completing might cause the array to appear non-idle
6828 * and resync will be slowed down even though there might
6829 * not have been non-resync activity. This will only
6830 * happen once though. 'last_events' will soon reflect
6831 * the state where there is little or no outstanding
6832 * resync requests, and further resync activity will
6833 * always make curr_events less than last_events.
6836 if (init
|| curr_events
- rdev
->last_events
> 64) {
6837 rdev
->last_events
= curr_events
;
6845 void md_done_sync(mddev_t
*mddev
, int blocks
, int ok
)
6847 /* another "blocks" (512byte) blocks have been synced */
6848 atomic_sub(blocks
, &mddev
->recovery_active
);
6849 wake_up(&mddev
->recovery_wait
);
6851 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6852 md_wakeup_thread(mddev
->thread
);
6853 // stop recovery, signal do_sync ....
6858 /* md_write_start(mddev, bi)
6859 * If we need to update some array metadata (e.g. 'active' flag
6860 * in superblock) before writing, schedule a superblock update
6861 * and wait for it to complete.
6863 void md_write_start(mddev_t
*mddev
, struct bio
*bi
)
6866 if (bio_data_dir(bi
) != WRITE
)
6869 BUG_ON(mddev
->ro
== 1);
6870 if (mddev
->ro
== 2) {
6871 /* need to switch to read/write */
6873 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6874 md_wakeup_thread(mddev
->thread
);
6875 md_wakeup_thread(mddev
->sync_thread
);
6878 atomic_inc(&mddev
->writes_pending
);
6879 if (mddev
->safemode
== 1)
6880 mddev
->safemode
= 0;
6881 if (mddev
->in_sync
) {
6882 spin_lock_irq(&mddev
->write_lock
);
6883 if (mddev
->in_sync
) {
6885 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
6886 set_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
6887 md_wakeup_thread(mddev
->thread
);
6890 spin_unlock_irq(&mddev
->write_lock
);
6893 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
6894 wait_event(mddev
->sb_wait
,
6895 !test_bit(MD_CHANGE_PENDING
, &mddev
->flags
));
6898 void md_write_end(mddev_t
*mddev
)
6900 if (atomic_dec_and_test(&mddev
->writes_pending
)) {
6901 if (mddev
->safemode
== 2)
6902 md_wakeup_thread(mddev
->thread
);
6903 else if (mddev
->safemode_delay
)
6904 mod_timer(&mddev
->safemode_timer
, jiffies
+ mddev
->safemode_delay
);
6908 /* md_allow_write(mddev)
6909 * Calling this ensures that the array is marked 'active' so that writes
6910 * may proceed without blocking. It is important to call this before
6911 * attempting a GFP_KERNEL allocation while holding the mddev lock.
6912 * Must be called with mddev_lock held.
6914 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
6915 * is dropped, so return -EAGAIN after notifying userspace.
6917 int md_allow_write(mddev_t
*mddev
)
6923 if (!mddev
->pers
->sync_request
)
6926 spin_lock_irq(&mddev
->write_lock
);
6927 if (mddev
->in_sync
) {
6929 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
6930 set_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
6931 if (mddev
->safemode_delay
&&
6932 mddev
->safemode
== 0)
6933 mddev
->safemode
= 1;
6934 spin_unlock_irq(&mddev
->write_lock
);
6935 md_update_sb(mddev
, 0);
6936 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
6938 spin_unlock_irq(&mddev
->write_lock
);
6940 if (test_bit(MD_CHANGE_PENDING
, &mddev
->flags
))
6945 EXPORT_SYMBOL_GPL(md_allow_write
);
6947 #define SYNC_MARKS 10
6948 #define SYNC_MARK_STEP (3*HZ)
6949 void md_do_sync(mddev_t
*mddev
)
6952 unsigned int currspeed
= 0,
6954 sector_t max_sectors
,j
, io_sectors
;
6955 unsigned long mark
[SYNC_MARKS
];
6956 sector_t mark_cnt
[SYNC_MARKS
];
6958 struct list_head
*tmp
;
6959 sector_t last_check
;
6964 /* just incase thread restarts... */
6965 if (test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
))
6967 if (mddev
->ro
) /* never try to sync a read-only array */
6970 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
6971 if (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
))
6972 desc
= "data-check";
6973 else if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
6974 desc
= "requested-resync";
6977 } else if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
6982 /* we overload curr_resync somewhat here.
6983 * 0 == not engaged in resync at all
6984 * 2 == checking that there is no conflict with another sync
6985 * 1 == like 2, but have yielded to allow conflicting resync to
6987 * other == active in resync - this many blocks
6989 * Before starting a resync we must have set curr_resync to
6990 * 2, and then checked that every "conflicting" array has curr_resync
6991 * less than ours. When we find one that is the same or higher
6992 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
6993 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
6994 * This will mean we have to start checking from the beginning again.
6999 mddev
->curr_resync
= 2;
7002 if (kthread_should_stop())
7003 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
7005 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
7007 for_each_mddev(mddev2
, tmp
) {
7008 if (mddev2
== mddev
)
7010 if (!mddev
->parallel_resync
7011 && mddev2
->curr_resync
7012 && match_mddev_units(mddev
, mddev2
)) {
7014 if (mddev
< mddev2
&& mddev
->curr_resync
== 2) {
7015 /* arbitrarily yield */
7016 mddev
->curr_resync
= 1;
7017 wake_up(&resync_wait
);
7019 if (mddev
> mddev2
&& mddev
->curr_resync
== 1)
7020 /* no need to wait here, we can wait the next
7021 * time 'round when curr_resync == 2
7024 /* We need to wait 'interruptible' so as not to
7025 * contribute to the load average, and not to
7026 * be caught by 'softlockup'
7028 prepare_to_wait(&resync_wait
, &wq
, TASK_INTERRUPTIBLE
);
7029 if (!kthread_should_stop() &&
7030 mddev2
->curr_resync
>= mddev
->curr_resync
) {
7031 printk(KERN_INFO
"md: delaying %s of %s"
7032 " until %s has finished (they"
7033 " share one or more physical units)\n",
7034 desc
, mdname(mddev
), mdname(mddev2
));
7036 if (signal_pending(current
))
7037 flush_signals(current
);
7039 finish_wait(&resync_wait
, &wq
);
7042 finish_wait(&resync_wait
, &wq
);
7045 } while (mddev
->curr_resync
< 2);
7048 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
7049 /* resync follows the size requested by the personality,
7050 * which defaults to physical size, but can be virtual size
7052 max_sectors
= mddev
->resync_max_sectors
;
7053 mddev
->resync_mismatches
= 0;
7054 /* we don't use the checkpoint if there's a bitmap */
7055 if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
7056 j
= mddev
->resync_min
;
7057 else if (!mddev
->bitmap
)
7058 j
= mddev
->recovery_cp
;
7060 } else if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
7061 max_sectors
= mddev
->dev_sectors
;
7063 /* recovery follows the physical size of devices */
7064 max_sectors
= mddev
->dev_sectors
;
7067 list_for_each_entry_rcu(rdev
, &mddev
->disks
, same_set
)
7068 if (rdev
->raid_disk
>= 0 &&
7069 !test_bit(Faulty
, &rdev
->flags
) &&
7070 !test_bit(In_sync
, &rdev
->flags
) &&
7071 rdev
->recovery_offset
< j
)
7072 j
= rdev
->recovery_offset
;
7076 printk(KERN_INFO
"md: %s of RAID array %s\n", desc
, mdname(mddev
));
7077 printk(KERN_INFO
"md: minimum _guaranteed_ speed:"
7078 " %d KB/sec/disk.\n", speed_min(mddev
));
7079 printk(KERN_INFO
"md: using maximum available idle IO bandwidth "
7080 "(but not more than %d KB/sec) for %s.\n",
7081 speed_max(mddev
), desc
);
7083 is_mddev_idle(mddev
, 1); /* this initializes IO event counters */
7086 for (m
= 0; m
< SYNC_MARKS
; m
++) {
7088 mark_cnt
[m
] = io_sectors
;
7091 mddev
->resync_mark
= mark
[last_mark
];
7092 mddev
->resync_mark_cnt
= mark_cnt
[last_mark
];
7095 * Tune reconstruction:
7097 window
= 32*(PAGE_SIZE
/512);
7098 printk(KERN_INFO
"md: using %dk window, over a total of %lluk.\n",
7099 window
/2, (unsigned long long)max_sectors
/2);
7101 atomic_set(&mddev
->recovery_active
, 0);
7106 "md: resuming %s of %s from checkpoint.\n",
7107 desc
, mdname(mddev
));
7108 mddev
->curr_resync
= j
;
7110 mddev
->curr_resync_completed
= j
;
7112 while (j
< max_sectors
) {
7117 if (!test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
7118 ((mddev
->curr_resync
> mddev
->curr_resync_completed
&&
7119 (mddev
->curr_resync
- mddev
->curr_resync_completed
)
7120 > (max_sectors
>> 4)) ||
7121 (j
- mddev
->curr_resync_completed
)*2
7122 >= mddev
->resync_max
- mddev
->curr_resync_completed
7124 /* time to update curr_resync_completed */
7125 wait_event(mddev
->recovery_wait
,
7126 atomic_read(&mddev
->recovery_active
) == 0);
7127 mddev
->curr_resync_completed
= j
;
7128 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
7129 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
7132 while (j
>= mddev
->resync_max
&& !kthread_should_stop()) {
7133 /* As this condition is controlled by user-space,
7134 * we can block indefinitely, so use '_interruptible'
7135 * to avoid triggering warnings.
7137 flush_signals(current
); /* just in case */
7138 wait_event_interruptible(mddev
->recovery_wait
,
7139 mddev
->resync_max
> j
7140 || kthread_should_stop());
7143 if (kthread_should_stop())
7146 sectors
= mddev
->pers
->sync_request(mddev
, j
, &skipped
,
7147 currspeed
< speed_min(mddev
));
7149 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
7153 if (!skipped
) { /* actual IO requested */
7154 io_sectors
+= sectors
;
7155 atomic_add(sectors
, &mddev
->recovery_active
);
7158 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
7162 if (j
>1) mddev
->curr_resync
= j
;
7163 mddev
->curr_mark_cnt
= io_sectors
;
7164 if (last_check
== 0)
7165 /* this is the earliest that rebuild will be
7166 * visible in /proc/mdstat
7168 md_new_event(mddev
);
7170 if (last_check
+ window
> io_sectors
|| j
== max_sectors
)
7173 last_check
= io_sectors
;
7175 if (time_after_eq(jiffies
, mark
[last_mark
] + SYNC_MARK_STEP
)) {
7177 int next
= (last_mark
+1) % SYNC_MARKS
;
7179 mddev
->resync_mark
= mark
[next
];
7180 mddev
->resync_mark_cnt
= mark_cnt
[next
];
7181 mark
[next
] = jiffies
;
7182 mark_cnt
[next
] = io_sectors
- atomic_read(&mddev
->recovery_active
);
7187 if (kthread_should_stop())
7192 * this loop exits only if either when we are slower than
7193 * the 'hard' speed limit, or the system was IO-idle for
7195 * the system might be non-idle CPU-wise, but we only care
7196 * about not overloading the IO subsystem. (things like an
7197 * e2fsck being done on the RAID array should execute fast)
7201 currspeed
= ((unsigned long)(io_sectors
-mddev
->resync_mark_cnt
))/2
7202 /((jiffies
-mddev
->resync_mark
)/HZ
+1) +1;
7204 if (currspeed
> speed_min(mddev
)) {
7205 if ((currspeed
> speed_max(mddev
)) ||
7206 !is_mddev_idle(mddev
, 0)) {
7212 printk(KERN_INFO
"md: %s: %s done.\n",mdname(mddev
), desc
);
7214 * this also signals 'finished resyncing' to md_stop
7217 wait_event(mddev
->recovery_wait
, !atomic_read(&mddev
->recovery_active
));
7219 /* tell personality that we are finished */
7220 mddev
->pers
->sync_request(mddev
, max_sectors
, &skipped
, 1);
7222 if (!test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
) &&
7223 mddev
->curr_resync
> 2) {
7224 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
7225 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
7226 if (mddev
->curr_resync
>= mddev
->recovery_cp
) {
7228 "md: checkpointing %s of %s.\n",
7229 desc
, mdname(mddev
));
7230 mddev
->recovery_cp
= mddev
->curr_resync
;
7233 mddev
->recovery_cp
= MaxSector
;
7235 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
7236 mddev
->curr_resync
= MaxSector
;
7238 list_for_each_entry_rcu(rdev
, &mddev
->disks
, same_set
)
7239 if (rdev
->raid_disk
>= 0 &&
7240 mddev
->delta_disks
>= 0 &&
7241 !test_bit(Faulty
, &rdev
->flags
) &&
7242 !test_bit(In_sync
, &rdev
->flags
) &&
7243 rdev
->recovery_offset
< mddev
->curr_resync
)
7244 rdev
->recovery_offset
= mddev
->curr_resync
;
7248 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
7251 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
7252 /* We completed so min/max setting can be forgotten if used. */
7253 if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
7254 mddev
->resync_min
= 0;
7255 mddev
->resync_max
= MaxSector
;
7256 } else if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
7257 mddev
->resync_min
= mddev
->curr_resync_completed
;
7258 mddev
->curr_resync
= 0;
7259 wake_up(&resync_wait
);
7260 set_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
7261 md_wakeup_thread(mddev
->thread
);
7266 * got a signal, exit.
7269 "md: md_do_sync() got signal ... exiting\n");
7270 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
7274 EXPORT_SYMBOL_GPL(md_do_sync
);
7276 static int remove_and_add_spares(mddev_t
*mddev
)
7281 mddev
->curr_resync_completed
= 0;
7283 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
7284 if (rdev
->raid_disk
>= 0 &&
7285 !test_bit(Blocked
, &rdev
->flags
) &&
7286 (test_bit(Faulty
, &rdev
->flags
) ||
7287 ! test_bit(In_sync
, &rdev
->flags
)) &&
7288 atomic_read(&rdev
->nr_pending
)==0) {
7289 if (mddev
->pers
->hot_remove_disk(
7290 mddev
, rdev
->raid_disk
)==0) {
7291 sysfs_unlink_rdev(mddev
, rdev
);
7292 rdev
->raid_disk
= -1;
7296 if (mddev
->degraded
) {
7297 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
7298 if (rdev
->raid_disk
>= 0 &&
7299 !test_bit(In_sync
, &rdev
->flags
) &&
7300 !test_bit(Faulty
, &rdev
->flags
))
7302 if (rdev
->raid_disk
< 0
7303 && !test_bit(Faulty
, &rdev
->flags
)) {
7304 rdev
->recovery_offset
= 0;
7306 hot_add_disk(mddev
, rdev
) == 0) {
7307 if (sysfs_link_rdev(mddev
, rdev
))
7308 /* failure here is OK */;
7310 md_new_event(mddev
);
7311 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
7320 static void reap_sync_thread(mddev_t
*mddev
)
7324 /* resync has finished, collect result */
7325 md_unregister_thread(mddev
->sync_thread
);
7326 mddev
->sync_thread
= NULL
;
7327 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
) &&
7328 !test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
)) {
7330 /* activate any spares */
7331 if (mddev
->pers
->spare_active(mddev
))
7332 sysfs_notify(&mddev
->kobj
, NULL
,
7335 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
7336 mddev
->pers
->finish_reshape
)
7337 mddev
->pers
->finish_reshape(mddev
);
7338 md_update_sb(mddev
, 1);
7340 /* if array is no-longer degraded, then any saved_raid_disk
7341 * information must be scrapped
7343 if (!mddev
->degraded
)
7344 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
7345 rdev
->saved_raid_disk
= -1;
7347 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
7348 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
7349 clear_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
7350 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
7351 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
7352 /* flag recovery needed just to double check */
7353 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7354 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
7355 md_new_event(mddev
);
7356 if (mddev
->event_work
.func
)
7357 queue_work(md_misc_wq
, &mddev
->event_work
);
7361 * This routine is regularly called by all per-raid-array threads to
7362 * deal with generic issues like resync and super-block update.
7363 * Raid personalities that don't have a thread (linear/raid0) do not
7364 * need this as they never do any recovery or update the superblock.
7366 * It does not do any resync itself, but rather "forks" off other threads
7367 * to do that as needed.
7368 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
7369 * "->recovery" and create a thread at ->sync_thread.
7370 * When the thread finishes it sets MD_RECOVERY_DONE
7371 * and wakeups up this thread which will reap the thread and finish up.
7372 * This thread also removes any faulty devices (with nr_pending == 0).
7374 * The overall approach is:
7375 * 1/ if the superblock needs updating, update it.
7376 * 2/ If a recovery thread is running, don't do anything else.
7377 * 3/ If recovery has finished, clean up, possibly marking spares active.
7378 * 4/ If there are any faulty devices, remove them.
7379 * 5/ If array is degraded, try to add spares devices
7380 * 6/ If array has spares or is not in-sync, start a resync thread.
7382 void md_check_recovery(mddev_t
*mddev
)
7384 if (mddev
->suspended
)
7388 bitmap_daemon_work(mddev
);
7390 if (signal_pending(current
)) {
7391 if (mddev
->pers
->sync_request
&& !mddev
->external
) {
7392 printk(KERN_INFO
"md: %s in immediate safe mode\n",
7394 mddev
->safemode
= 2;
7396 flush_signals(current
);
7399 if (mddev
->ro
&& !test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))
7402 (mddev
->flags
& ~ (1<<MD_CHANGE_PENDING
)) ||
7403 test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
) ||
7404 test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
) ||
7405 (mddev
->external
== 0 && mddev
->safemode
== 1) ||
7406 (mddev
->safemode
== 2 && ! atomic_read(&mddev
->writes_pending
)
7407 && !mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
)
7411 if (mddev_trylock(mddev
)) {
7415 /* Only thing we do on a ro array is remove
7419 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
7420 if (rdev
->raid_disk
>= 0 &&
7421 !test_bit(Blocked
, &rdev
->flags
) &&
7422 test_bit(Faulty
, &rdev
->flags
) &&
7423 atomic_read(&rdev
->nr_pending
)==0) {
7424 if (mddev
->pers
->hot_remove_disk(
7425 mddev
, rdev
->raid_disk
)==0) {
7426 sysfs_unlink_rdev(mddev
, rdev
);
7427 rdev
->raid_disk
= -1;
7430 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7434 if (!mddev
->external
) {
7436 spin_lock_irq(&mddev
->write_lock
);
7437 if (mddev
->safemode
&&
7438 !atomic_read(&mddev
->writes_pending
) &&
7440 mddev
->recovery_cp
== MaxSector
) {
7443 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
7445 if (mddev
->safemode
== 1)
7446 mddev
->safemode
= 0;
7447 spin_unlock_irq(&mddev
->write_lock
);
7449 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
7453 md_update_sb(mddev
, 0);
7455 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) &&
7456 !test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
)) {
7457 /* resync/recovery still happening */
7458 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7461 if (mddev
->sync_thread
) {
7462 reap_sync_thread(mddev
);
7465 /* Set RUNNING before clearing NEEDED to avoid
7466 * any transients in the value of "sync_action".
7468 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
7469 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7470 /* Clear some bits that don't mean anything, but
7473 clear_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
7474 clear_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
7476 if (test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
))
7478 /* no recovery is running.
7479 * remove any failed drives, then
7480 * add spares if possible.
7481 * Spare are also removed and re-added, to allow
7482 * the personality to fail the re-add.
7485 if (mddev
->reshape_position
!= MaxSector
) {
7486 if (mddev
->pers
->check_reshape
== NULL
||
7487 mddev
->pers
->check_reshape(mddev
) != 0)
7488 /* Cannot proceed */
7490 set_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
7491 clear_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
7492 } else if ((spares
= remove_and_add_spares(mddev
))) {
7493 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
7494 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
7495 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
7496 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
7497 } else if (mddev
->recovery_cp
< MaxSector
) {
7498 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
7499 clear_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
7500 } else if (!test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
7501 /* nothing to be done ... */
7504 if (mddev
->pers
->sync_request
) {
7505 if (spares
&& mddev
->bitmap
&& ! mddev
->bitmap
->file
) {
7506 /* We are adding a device or devices to an array
7507 * which has the bitmap stored on all devices.
7508 * So make sure all bitmap pages get written
7510 bitmap_write_all(mddev
->bitmap
);
7512 mddev
->sync_thread
= md_register_thread(md_do_sync
,
7515 if (!mddev
->sync_thread
) {
7516 printk(KERN_ERR
"%s: could not start resync"
7519 /* leave the spares where they are, it shouldn't hurt */
7520 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
7521 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
7522 clear_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
7523 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
7524 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
7526 md_wakeup_thread(mddev
->sync_thread
);
7527 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
7528 md_new_event(mddev
);
7531 if (!mddev
->sync_thread
) {
7532 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
7533 if (test_and_clear_bit(MD_RECOVERY_RECOVER
,
7535 if (mddev
->sysfs_action
)
7536 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
7538 mddev_unlock(mddev
);
7542 void md_wait_for_blocked_rdev(mdk_rdev_t
*rdev
, mddev_t
*mddev
)
7544 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
7545 wait_event_timeout(rdev
->blocked_wait
,
7546 !test_bit(Blocked
, &rdev
->flags
) &&
7547 !test_bit(BlockedBadBlocks
, &rdev
->flags
),
7548 msecs_to_jiffies(5000));
7549 rdev_dec_pending(rdev
, mddev
);
7551 EXPORT_SYMBOL(md_wait_for_blocked_rdev
);
7554 /* Bad block management.
7555 * We can record which blocks on each device are 'bad' and so just
7556 * fail those blocks, or that stripe, rather than the whole device.
7557 * Entries in the bad-block table are 64bits wide. This comprises:
7558 * Length of bad-range, in sectors: 0-511 for lengths 1-512
7559 * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes)
7560 * A 'shift' can be set so that larger blocks are tracked and
7561 * consequently larger devices can be covered.
7562 * 'Acknowledged' flag - 1 bit. - the most significant bit.
7564 * Locking of the bad-block table uses a seqlock so md_is_badblock
7565 * might need to retry if it is very unlucky.
7566 * We will sometimes want to check for bad blocks in a bi_end_io function,
7567 * so we use the write_seqlock_irq variant.
7569 * When looking for a bad block we specify a range and want to
7570 * know if any block in the range is bad. So we binary-search
7571 * to the last range that starts at-or-before the given endpoint,
7572 * (or "before the sector after the target range")
7573 * then see if it ends after the given start.
7575 * 0 if there are no known bad blocks in the range
7576 * 1 if there are known bad block which are all acknowledged
7577 * -1 if there are bad blocks which have not yet been acknowledged in metadata.
7578 * plus the start/length of the first bad section we overlap.
7580 int md_is_badblock(struct badblocks
*bb
, sector_t s
, int sectors
,
7581 sector_t
*first_bad
, int *bad_sectors
)
7587 sector_t target
= s
+ sectors
;
7590 if (bb
->shift
> 0) {
7591 /* round the start down, and the end up */
7593 target
+= (1<<bb
->shift
) - 1;
7594 target
>>= bb
->shift
;
7595 sectors
= target
- s
;
7597 /* 'target' is now the first block after the bad range */
7600 seq
= read_seqbegin(&bb
->lock
);
7604 /* Binary search between lo and hi for 'target'
7605 * i.e. for the last range that starts before 'target'
7607 /* INVARIANT: ranges before 'lo' and at-or-after 'hi'
7608 * are known not to be the last range before target.
7609 * VARIANT: hi-lo is the number of possible
7610 * ranges, and decreases until it reaches 1
7612 while (hi
- lo
> 1) {
7613 int mid
= (lo
+ hi
) / 2;
7614 sector_t a
= BB_OFFSET(p
[mid
]);
7616 /* This could still be the one, earlier ranges
7620 /* This and later ranges are definitely out. */
7623 /* 'lo' might be the last that started before target, but 'hi' isn't */
7625 /* need to check all range that end after 's' to see if
7626 * any are unacknowledged.
7629 BB_OFFSET(p
[lo
]) + BB_LEN(p
[lo
]) > s
) {
7630 if (BB_OFFSET(p
[lo
]) < target
) {
7631 /* starts before the end, and finishes after
7632 * the start, so they must overlap
7634 if (rv
!= -1 && BB_ACK(p
[lo
]))
7638 *first_bad
= BB_OFFSET(p
[lo
]);
7639 *bad_sectors
= BB_LEN(p
[lo
]);
7645 if (read_seqretry(&bb
->lock
, seq
))
7650 EXPORT_SYMBOL_GPL(md_is_badblock
);
7653 * Add a range of bad blocks to the table.
7654 * This might extend the table, or might contract it
7655 * if two adjacent ranges can be merged.
7656 * We binary-search to find the 'insertion' point, then
7657 * decide how best to handle it.
7659 static int md_set_badblocks(struct badblocks
*bb
, sector_t s
, int sectors
,
7667 /* badblocks are disabled */
7671 /* round the start down, and the end up */
7672 sector_t next
= s
+ sectors
;
7674 next
+= (1<<bb
->shift
) - 1;
7679 write_seqlock_irq(&bb
->lock
);
7684 /* Find the last range that starts at-or-before 's' */
7685 while (hi
- lo
> 1) {
7686 int mid
= (lo
+ hi
) / 2;
7687 sector_t a
= BB_OFFSET(p
[mid
]);
7693 if (hi
> lo
&& BB_OFFSET(p
[lo
]) > s
)
7697 /* we found a range that might merge with the start
7700 sector_t a
= BB_OFFSET(p
[lo
]);
7701 sector_t e
= a
+ BB_LEN(p
[lo
]);
7702 int ack
= BB_ACK(p
[lo
]);
7704 /* Yes, we can merge with a previous range */
7705 if (s
== a
&& s
+ sectors
>= e
)
7706 /* new range covers old */
7709 ack
= ack
&& acknowledged
;
7711 if (e
< s
+ sectors
)
7713 if (e
- a
<= BB_MAX_LEN
) {
7714 p
[lo
] = BB_MAKE(a
, e
-a
, ack
);
7717 /* does not all fit in one range,
7718 * make p[lo] maximal
7720 if (BB_LEN(p
[lo
]) != BB_MAX_LEN
)
7721 p
[lo
] = BB_MAKE(a
, BB_MAX_LEN
, ack
);
7727 if (sectors
&& hi
< bb
->count
) {
7728 /* 'hi' points to the first range that starts after 's'.
7729 * Maybe we can merge with the start of that range */
7730 sector_t a
= BB_OFFSET(p
[hi
]);
7731 sector_t e
= a
+ BB_LEN(p
[hi
]);
7732 int ack
= BB_ACK(p
[hi
]);
7733 if (a
<= s
+ sectors
) {
7734 /* merging is possible */
7735 if (e
<= s
+ sectors
) {
7740 ack
= ack
&& acknowledged
;
7743 if (e
- a
<= BB_MAX_LEN
) {
7744 p
[hi
] = BB_MAKE(a
, e
-a
, ack
);
7747 p
[hi
] = BB_MAKE(a
, BB_MAX_LEN
, ack
);
7755 if (sectors
== 0 && hi
< bb
->count
) {
7756 /* we might be able to combine lo and hi */
7757 /* Note: 's' is at the end of 'lo' */
7758 sector_t a
= BB_OFFSET(p
[hi
]);
7759 int lolen
= BB_LEN(p
[lo
]);
7760 int hilen
= BB_LEN(p
[hi
]);
7761 int newlen
= lolen
+ hilen
- (s
- a
);
7762 if (s
>= a
&& newlen
< BB_MAX_LEN
) {
7763 /* yes, we can combine them */
7764 int ack
= BB_ACK(p
[lo
]) && BB_ACK(p
[hi
]);
7765 p
[lo
] = BB_MAKE(BB_OFFSET(p
[lo
]), newlen
, ack
);
7766 memmove(p
+ hi
, p
+ hi
+ 1,
7767 (bb
->count
- hi
- 1) * 8);
7772 /* didn't merge (it all).
7773 * Need to add a range just before 'hi' */
7774 if (bb
->count
>= MD_MAX_BADBLOCKS
) {
7775 /* No room for more */
7779 int this_sectors
= sectors
;
7780 memmove(p
+ hi
+ 1, p
+ hi
,
7781 (bb
->count
- hi
) * 8);
7784 if (this_sectors
> BB_MAX_LEN
)
7785 this_sectors
= BB_MAX_LEN
;
7786 p
[hi
] = BB_MAKE(s
, this_sectors
, acknowledged
);
7787 sectors
-= this_sectors
;
7794 bb
->unacked_exist
= 1;
7795 write_sequnlock_irq(&bb
->lock
);
7800 int rdev_set_badblocks(mdk_rdev_t
*rdev
, sector_t s
, int sectors
,
7803 int rv
= md_set_badblocks(&rdev
->badblocks
,
7804 s
+ rdev
->data_offset
, sectors
, acknowledged
);
7806 /* Make sure they get written out promptly */
7807 set_bit(MD_CHANGE_CLEAN
, &rdev
->mddev
->flags
);
7808 md_wakeup_thread(rdev
->mddev
->thread
);
7812 EXPORT_SYMBOL_GPL(rdev_set_badblocks
);
7815 * Remove a range of bad blocks from the table.
7816 * This may involve extending the table if we spilt a region,
7817 * but it must not fail. So if the table becomes full, we just
7818 * drop the remove request.
7820 static int md_clear_badblocks(struct badblocks
*bb
, sector_t s
, int sectors
)
7824 sector_t target
= s
+ sectors
;
7827 if (bb
->shift
> 0) {
7828 /* When clearing we round the start up and the end down.
7829 * This should not matter as the shift should align with
7830 * the block size and no rounding should ever be needed.
7831 * However it is better the think a block is bad when it
7832 * isn't than to think a block is not bad when it is.
7834 s
+= (1<<bb
->shift
) - 1;
7836 target
>>= bb
->shift
;
7837 sectors
= target
- s
;
7840 write_seqlock_irq(&bb
->lock
);
7845 /* Find the last range that starts before 'target' */
7846 while (hi
- lo
> 1) {
7847 int mid
= (lo
+ hi
) / 2;
7848 sector_t a
= BB_OFFSET(p
[mid
]);
7855 /* p[lo] is the last range that could overlap the
7856 * current range. Earlier ranges could also overlap,
7857 * but only this one can overlap the end of the range.
7859 if (BB_OFFSET(p
[lo
]) + BB_LEN(p
[lo
]) > target
) {
7860 /* Partial overlap, leave the tail of this range */
7861 int ack
= BB_ACK(p
[lo
]);
7862 sector_t a
= BB_OFFSET(p
[lo
]);
7863 sector_t end
= a
+ BB_LEN(p
[lo
]);
7866 /* we need to split this range */
7867 if (bb
->count
>= MD_MAX_BADBLOCKS
) {
7871 memmove(p
+lo
+1, p
+lo
, (bb
->count
- lo
) * 8);
7873 p
[lo
] = BB_MAKE(a
, s
-a
, ack
);
7876 p
[lo
] = BB_MAKE(target
, end
- target
, ack
);
7877 /* there is no longer an overlap */
7882 BB_OFFSET(p
[lo
]) + BB_LEN(p
[lo
]) > s
) {
7883 /* This range does overlap */
7884 if (BB_OFFSET(p
[lo
]) < s
) {
7885 /* Keep the early parts of this range. */
7886 int ack
= BB_ACK(p
[lo
]);
7887 sector_t start
= BB_OFFSET(p
[lo
]);
7888 p
[lo
] = BB_MAKE(start
, s
- start
, ack
);
7889 /* now low doesn't overlap, so.. */
7894 /* 'lo' is strictly before, 'hi' is strictly after,
7895 * anything between needs to be discarded
7898 memmove(p
+lo
+1, p
+hi
, (bb
->count
- hi
) * 8);
7899 bb
->count
-= (hi
- lo
- 1);
7905 write_sequnlock_irq(&bb
->lock
);
7909 int rdev_clear_badblocks(mdk_rdev_t
*rdev
, sector_t s
, int sectors
)
7911 return md_clear_badblocks(&rdev
->badblocks
,
7912 s
+ rdev
->data_offset
,
7915 EXPORT_SYMBOL_GPL(rdev_clear_badblocks
);
7918 * Acknowledge all bad blocks in a list.
7919 * This only succeeds if ->changed is clear. It is used by
7920 * in-kernel metadata updates
7922 void md_ack_all_badblocks(struct badblocks
*bb
)
7924 if (bb
->page
== NULL
|| bb
->changed
)
7925 /* no point even trying */
7927 write_seqlock_irq(&bb
->lock
);
7929 if (bb
->changed
== 0) {
7932 for (i
= 0; i
< bb
->count
; i
++) {
7933 if (!BB_ACK(p
[i
])) {
7934 sector_t start
= BB_OFFSET(p
[i
]);
7935 int len
= BB_LEN(p
[i
]);
7936 p
[i
] = BB_MAKE(start
, len
, 1);
7939 bb
->unacked_exist
= 0;
7941 write_sequnlock_irq(&bb
->lock
);
7943 EXPORT_SYMBOL_GPL(md_ack_all_badblocks
);
7945 /* sysfs access to bad-blocks list.
7946 * We present two files.
7947 * 'bad-blocks' lists sector numbers and lengths of ranges that
7948 * are recorded as bad. The list is truncated to fit within
7949 * the one-page limit of sysfs.
7950 * Writing "sector length" to this file adds an acknowledged
7952 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
7953 * been acknowledged. Writing to this file adds bad blocks
7954 * without acknowledging them. This is largely for testing.
7958 badblocks_show(struct badblocks
*bb
, char *page
, int unack
)
7969 seq
= read_seqbegin(&bb
->lock
);
7974 while (len
< PAGE_SIZE
&& i
< bb
->count
) {
7975 sector_t s
= BB_OFFSET(p
[i
]);
7976 unsigned int length
= BB_LEN(p
[i
]);
7977 int ack
= BB_ACK(p
[i
]);
7983 len
+= snprintf(page
+len
, PAGE_SIZE
-len
, "%llu %u\n",
7984 (unsigned long long)s
<< bb
->shift
,
7985 length
<< bb
->shift
);
7987 if (unack
&& len
== 0)
7988 bb
->unacked_exist
= 0;
7990 if (read_seqretry(&bb
->lock
, seq
))
7999 badblocks_store(struct badblocks
*bb
, const char *page
, size_t len
, int unack
)
8001 unsigned long long sector
;
8005 /* Allow clearing via sysfs *only* for testing/debugging.
8006 * Normally only a successful write may clear a badblock
8009 if (page
[0] == '-') {
8013 #endif /* DO_DEBUG */
8015 switch (sscanf(page
, "%llu %d%c", §or
, &length
, &newline
)) {
8017 if (newline
!= '\n')
8029 md_clear_badblocks(bb
, sector
, length
);
8032 #endif /* DO_DEBUG */
8033 if (md_set_badblocks(bb
, sector
, length
, !unack
))
8039 static int md_notify_reboot(struct notifier_block
*this,
8040 unsigned long code
, void *x
)
8042 struct list_head
*tmp
;
8045 if ((code
== SYS_DOWN
) || (code
== SYS_HALT
) || (code
== SYS_POWER_OFF
)) {
8047 printk(KERN_INFO
"md: stopping all md devices.\n");
8049 for_each_mddev(mddev
, tmp
)
8050 if (mddev_trylock(mddev
)) {
8051 /* Force a switch to readonly even array
8052 * appears to still be in use. Hence
8055 md_set_readonly(mddev
, 100);
8056 mddev_unlock(mddev
);
8059 * certain more exotic SCSI devices are known to be
8060 * volatile wrt too early system reboots. While the
8061 * right place to handle this issue is the given
8062 * driver, we do want to have a safe RAID driver ...
8069 static struct notifier_block md_notifier
= {
8070 .notifier_call
= md_notify_reboot
,
8072 .priority
= INT_MAX
, /* before any real devices */
8075 static void md_geninit(void)
8077 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t
));
8079 proc_create("mdstat", S_IRUGO
, NULL
, &md_seq_fops
);
8082 static int __init
md_init(void)
8086 md_wq
= alloc_workqueue("md", WQ_MEM_RECLAIM
, 0);
8090 md_misc_wq
= alloc_workqueue("md_misc", 0, 0);
8094 if ((ret
= register_blkdev(MD_MAJOR
, "md")) < 0)
8097 if ((ret
= register_blkdev(0, "mdp")) < 0)
8101 blk_register_region(MKDEV(MD_MAJOR
, 0), 1UL<<MINORBITS
, THIS_MODULE
,
8102 md_probe
, NULL
, NULL
);
8103 blk_register_region(MKDEV(mdp_major
, 0), 1UL<<MINORBITS
, THIS_MODULE
,
8104 md_probe
, NULL
, NULL
);
8106 register_reboot_notifier(&md_notifier
);
8107 raid_table_header
= register_sysctl_table(raid_root_table
);
8113 unregister_blkdev(MD_MAJOR
, "md");
8115 destroy_workqueue(md_misc_wq
);
8117 destroy_workqueue(md_wq
);
8125 * Searches all registered partitions for autorun RAID arrays
8129 static LIST_HEAD(all_detected_devices
);
8130 struct detected_devices_node
{
8131 struct list_head list
;
8135 void md_autodetect_dev(dev_t dev
)
8137 struct detected_devices_node
*node_detected_dev
;
8139 node_detected_dev
= kzalloc(sizeof(*node_detected_dev
), GFP_KERNEL
);
8140 if (node_detected_dev
) {
8141 node_detected_dev
->dev
= dev
;
8142 list_add_tail(&node_detected_dev
->list
, &all_detected_devices
);
8144 printk(KERN_CRIT
"md: md_autodetect_dev: kzalloc failed"
8145 ", skipping dev(%d,%d)\n", MAJOR(dev
), MINOR(dev
));
8150 static void autostart_arrays(int part
)
8153 struct detected_devices_node
*node_detected_dev
;
8155 int i_scanned
, i_passed
;
8160 printk(KERN_INFO
"md: Autodetecting RAID arrays.\n");
8162 while (!list_empty(&all_detected_devices
) && i_scanned
< INT_MAX
) {
8164 node_detected_dev
= list_entry(all_detected_devices
.next
,
8165 struct detected_devices_node
, list
);
8166 list_del(&node_detected_dev
->list
);
8167 dev
= node_detected_dev
->dev
;
8168 kfree(node_detected_dev
);
8169 rdev
= md_import_device(dev
,0, 90);
8173 if (test_bit(Faulty
, &rdev
->flags
)) {
8177 set_bit(AutoDetected
, &rdev
->flags
);
8178 list_add(&rdev
->same_set
, &pending_raid_disks
);
8182 printk(KERN_INFO
"md: Scanned %d and added %d devices.\n",
8183 i_scanned
, i_passed
);
8185 autorun_devices(part
);
8188 #endif /* !MODULE */
8190 static __exit
void md_exit(void)
8193 struct list_head
*tmp
;
8195 blk_unregister_region(MKDEV(MD_MAJOR
,0), 1U << MINORBITS
);
8196 blk_unregister_region(MKDEV(mdp_major
,0), 1U << MINORBITS
);
8198 unregister_blkdev(MD_MAJOR
,"md");
8199 unregister_blkdev(mdp_major
, "mdp");
8200 unregister_reboot_notifier(&md_notifier
);
8201 unregister_sysctl_table(raid_table_header
);
8202 remove_proc_entry("mdstat", NULL
);
8203 for_each_mddev(mddev
, tmp
) {
8204 export_array(mddev
);
8205 mddev
->hold_active
= 0;
8207 destroy_workqueue(md_misc_wq
);
8208 destroy_workqueue(md_wq
);
8211 subsys_initcall(md_init
);
8212 module_exit(md_exit
)
8214 static int get_ro(char *buffer
, struct kernel_param
*kp
)
8216 return sprintf(buffer
, "%d", start_readonly
);
8218 static int set_ro(const char *val
, struct kernel_param
*kp
)
8221 int num
= simple_strtoul(val
, &e
, 10);
8222 if (*val
&& (*e
== '\0' || *e
== '\n')) {
8223 start_readonly
= num
;
8229 module_param_call(start_ro
, set_ro
, get_ro
, NULL
, S_IRUSR
|S_IWUSR
);
8230 module_param(start_dirty_degraded
, int, S_IRUGO
|S_IWUSR
);
8232 module_param_call(new_array
, add_named_array
, NULL
, NULL
, S_IWUSR
);
8234 EXPORT_SYMBOL(register_md_personality
);
8235 EXPORT_SYMBOL(unregister_md_personality
);
8236 EXPORT_SYMBOL(md_error
);
8237 EXPORT_SYMBOL(md_done_sync
);
8238 EXPORT_SYMBOL(md_write_start
);
8239 EXPORT_SYMBOL(md_write_end
);
8240 EXPORT_SYMBOL(md_register_thread
);
8241 EXPORT_SYMBOL(md_unregister_thread
);
8242 EXPORT_SYMBOL(md_wakeup_thread
);
8243 EXPORT_SYMBOL(md_check_recovery
);
8244 MODULE_LICENSE("GPL");
8245 MODULE_DESCRIPTION("MD RAID framework");
8247 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR
);