2 md.c : Multiple Devices driver for Linux
3 Copyright (C) 1998, 1999, 2000 Ingo Molnar
5 completely rewritten, based on the MD driver code from Marc Zyngier
9 - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar
10 - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com>
11 - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net>
12 - kerneld support by Boris Tobotras <boris@xtalk.msk.su>
13 - kmod support by: Cyrus Durgin
14 - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com>
15 - Devfs support by Richard Gooch <rgooch@atnf.csiro.au>
17 - lots of fixes and improvements to the RAID1/RAID5 and generic
18 RAID code (such as request based resynchronization):
20 Neil Brown <neilb@cse.unsw.edu.au>.
22 - persistent bitmap code
23 Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.
25 This program is free software; you can redistribute it and/or modify
26 it under the terms of the GNU General Public License as published by
27 the Free Software Foundation; either version 2, or (at your option)
30 You should have received a copy of the GNU General Public License
31 (for example /usr/src/linux/COPYING); if not, write to the Free
32 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35 #include <linux/kthread.h>
36 #include <linux/blkdev.h>
37 #include <linux/sysctl.h>
38 #include <linux/seq_file.h>
40 #include <linux/poll.h>
41 #include <linux/ctype.h>
42 #include <linux/string.h>
43 #include <linux/hdreg.h>
44 #include <linux/proc_fs.h>
45 #include <linux/random.h>
46 #include <linux/module.h>
47 #include <linux/reboot.h>
48 #include <linux/file.h>
49 #include <linux/compat.h>
50 #include <linux/delay.h>
51 #include <linux/raid/md_p.h>
52 #include <linux/raid/md_u.h>
53 #include <linux/slab.h>
58 static void autostart_arrays(int part
);
61 /* pers_list is a list of registered personalities protected
63 * pers_lock does extra service to protect accesses to
64 * mddev->thread when the mutex cannot be held.
66 static LIST_HEAD(pers_list
);
67 static DEFINE_SPINLOCK(pers_lock
);
69 static void md_print_devices(void);
71 static DECLARE_WAIT_QUEUE_HEAD(resync_wait
);
72 static struct workqueue_struct
*md_wq
;
73 static struct workqueue_struct
*md_misc_wq
;
75 #define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
78 * Default number of read corrections we'll attempt on an rdev
79 * before ejecting it from the array. We divide the read error
80 * count by 2 for every hour elapsed between read errors.
82 #define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
84 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
85 * is 1000 KB/sec, so the extra system load does not show up that much.
86 * Increase it if you want to have more _guaranteed_ speed. Note that
87 * the RAID driver will use the maximum available bandwidth if the IO
88 * subsystem is idle. There is also an 'absolute maximum' reconstruction
89 * speed limit - in case reconstruction slows down your system despite
92 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
93 * or /sys/block/mdX/md/sync_speed_{min,max}
96 static int sysctl_speed_limit_min
= 1000;
97 static int sysctl_speed_limit_max
= 200000;
98 static inline int speed_min(struct mddev
*mddev
)
100 return mddev
->sync_speed_min
?
101 mddev
->sync_speed_min
: sysctl_speed_limit_min
;
104 static inline int speed_max(struct mddev
*mddev
)
106 return mddev
->sync_speed_max
?
107 mddev
->sync_speed_max
: sysctl_speed_limit_max
;
110 static struct ctl_table_header
*raid_table_header
;
112 static ctl_table raid_table
[] = {
114 .procname
= "speed_limit_min",
115 .data
= &sysctl_speed_limit_min
,
116 .maxlen
= sizeof(int),
117 .mode
= S_IRUGO
|S_IWUSR
,
118 .proc_handler
= proc_dointvec
,
121 .procname
= "speed_limit_max",
122 .data
= &sysctl_speed_limit_max
,
123 .maxlen
= sizeof(int),
124 .mode
= S_IRUGO
|S_IWUSR
,
125 .proc_handler
= proc_dointvec
,
130 static ctl_table raid_dir_table
[] = {
134 .mode
= S_IRUGO
|S_IXUGO
,
140 static ctl_table raid_root_table
[] = {
145 .child
= raid_dir_table
,
150 static const struct block_device_operations md_fops
;
152 static int start_readonly
;
155 * like bio_clone, but with a local bio set
158 static void mddev_bio_destructor(struct bio
*bio
)
160 struct mddev
*mddev
, **mddevp
;
165 bio_free(bio
, mddev
->bio_set
);
168 struct bio
*bio_alloc_mddev(gfp_t gfp_mask
, int nr_iovecs
,
172 struct mddev
**mddevp
;
174 if (!mddev
|| !mddev
->bio_set
)
175 return bio_alloc(gfp_mask
, nr_iovecs
);
177 b
= bio_alloc_bioset(gfp_mask
, nr_iovecs
,
183 b
->bi_destructor
= mddev_bio_destructor
;
186 EXPORT_SYMBOL_GPL(bio_alloc_mddev
);
188 struct bio
*bio_clone_mddev(struct bio
*bio
, gfp_t gfp_mask
,
192 struct mddev
**mddevp
;
194 if (!mddev
|| !mddev
->bio_set
)
195 return bio_clone(bio
, gfp_mask
);
197 b
= bio_alloc_bioset(gfp_mask
, bio
->bi_max_vecs
,
203 b
->bi_destructor
= mddev_bio_destructor
;
205 if (bio_integrity(bio
)) {
208 ret
= bio_integrity_clone(b
, bio
, gfp_mask
, mddev
->bio_set
);
218 EXPORT_SYMBOL_GPL(bio_clone_mddev
);
220 void md_trim_bio(struct bio
*bio
, int offset
, int size
)
222 /* 'bio' is a cloned bio which we need to trim to match
223 * the given offset and size.
224 * This requires adjusting bi_sector, bi_size, and bi_io_vec
227 struct bio_vec
*bvec
;
231 if (offset
== 0 && size
== bio
->bi_size
)
234 bio
->bi_sector
+= offset
;
237 clear_bit(BIO_SEG_VALID
, &bio
->bi_flags
);
239 while (bio
->bi_idx
< bio
->bi_vcnt
&&
240 bio
->bi_io_vec
[bio
->bi_idx
].bv_len
<= offset
) {
241 /* remove this whole bio_vec */
242 offset
-= bio
->bi_io_vec
[bio
->bi_idx
].bv_len
;
245 if (bio
->bi_idx
< bio
->bi_vcnt
) {
246 bio
->bi_io_vec
[bio
->bi_idx
].bv_offset
+= offset
;
247 bio
->bi_io_vec
[bio
->bi_idx
].bv_len
-= offset
;
249 /* avoid any complications with bi_idx being non-zero*/
251 memmove(bio
->bi_io_vec
, bio
->bi_io_vec
+bio
->bi_idx
,
252 (bio
->bi_vcnt
- bio
->bi_idx
) * sizeof(struct bio_vec
));
253 bio
->bi_vcnt
-= bio
->bi_idx
;
256 /* Make sure vcnt and last bv are not too big */
257 bio_for_each_segment(bvec
, bio
, i
) {
258 if (sofar
+ bvec
->bv_len
> size
)
259 bvec
->bv_len
= size
- sofar
;
260 if (bvec
->bv_len
== 0) {
264 sofar
+= bvec
->bv_len
;
267 EXPORT_SYMBOL_GPL(md_trim_bio
);
270 * We have a system wide 'event count' that is incremented
271 * on any 'interesting' event, and readers of /proc/mdstat
272 * can use 'poll' or 'select' to find out when the event
276 * start array, stop array, error, add device, remove device,
277 * start build, activate spare
279 static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters
);
280 static atomic_t md_event_count
;
281 void md_new_event(struct mddev
*mddev
)
283 atomic_inc(&md_event_count
);
284 wake_up(&md_event_waiters
);
286 EXPORT_SYMBOL_GPL(md_new_event
);
288 /* Alternate version that can be called from interrupts
289 * when calling sysfs_notify isn't needed.
291 static void md_new_event_inintr(struct mddev
*mddev
)
293 atomic_inc(&md_event_count
);
294 wake_up(&md_event_waiters
);
298 * Enables to iterate over all existing md arrays
299 * all_mddevs_lock protects this list.
301 static LIST_HEAD(all_mddevs
);
302 static DEFINE_SPINLOCK(all_mddevs_lock
);
306 * iterates through all used mddevs in the system.
307 * We take care to grab the all_mddevs_lock whenever navigating
308 * the list, and to always hold a refcount when unlocked.
309 * Any code which breaks out of this loop while own
310 * a reference to the current mddev and must mddev_put it.
312 #define for_each_mddev(_mddev,_tmp) \
314 for (({ spin_lock(&all_mddevs_lock); \
315 _tmp = all_mddevs.next; \
317 ({ if (_tmp != &all_mddevs) \
318 mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\
319 spin_unlock(&all_mddevs_lock); \
320 if (_mddev) mddev_put(_mddev); \
321 _mddev = list_entry(_tmp, struct mddev, all_mddevs); \
322 _tmp != &all_mddevs;}); \
323 ({ spin_lock(&all_mddevs_lock); \
324 _tmp = _tmp->next;}) \
328 /* Rather than calling directly into the personality make_request function,
329 * IO requests come here first so that we can check if the device is
330 * being suspended pending a reconfiguration.
331 * We hold a refcount over the call to ->make_request. By the time that
332 * call has finished, the bio has been linked into some internal structure
333 * and so is visible to ->quiesce(), so we don't need the refcount any more.
335 static void md_make_request(struct request_queue
*q
, struct bio
*bio
)
337 const int rw
= bio_data_dir(bio
);
338 struct mddev
*mddev
= q
->queuedata
;
340 unsigned int sectors
;
342 if (mddev
== NULL
|| mddev
->pers
== NULL
347 smp_rmb(); /* Ensure implications of 'active' are visible */
349 if (mddev
->suspended
) {
352 prepare_to_wait(&mddev
->sb_wait
, &__wait
,
353 TASK_UNINTERRUPTIBLE
);
354 if (!mddev
->suspended
)
360 finish_wait(&mddev
->sb_wait
, &__wait
);
362 atomic_inc(&mddev
->active_io
);
366 * save the sectors now since our bio can
367 * go away inside make_request
369 sectors
= bio_sectors(bio
);
370 mddev
->pers
->make_request(mddev
, bio
);
372 cpu
= part_stat_lock();
373 part_stat_inc(cpu
, &mddev
->gendisk
->part0
, ios
[rw
]);
374 part_stat_add(cpu
, &mddev
->gendisk
->part0
, sectors
[rw
], sectors
);
377 if (atomic_dec_and_test(&mddev
->active_io
) && mddev
->suspended
)
378 wake_up(&mddev
->sb_wait
);
381 /* mddev_suspend makes sure no new requests are submitted
382 * to the device, and that any requests that have been submitted
383 * are completely handled.
384 * Once ->stop is called and completes, the module will be completely
387 void mddev_suspend(struct mddev
*mddev
)
389 BUG_ON(mddev
->suspended
);
390 mddev
->suspended
= 1;
392 wait_event(mddev
->sb_wait
, atomic_read(&mddev
->active_io
) == 0);
393 mddev
->pers
->quiesce(mddev
, 1);
395 EXPORT_SYMBOL_GPL(mddev_suspend
);
397 void mddev_resume(struct mddev
*mddev
)
399 mddev
->suspended
= 0;
400 wake_up(&mddev
->sb_wait
);
401 mddev
->pers
->quiesce(mddev
, 0);
403 md_wakeup_thread(mddev
->thread
);
404 md_wakeup_thread(mddev
->sync_thread
); /* possibly kick off a reshape */
406 EXPORT_SYMBOL_GPL(mddev_resume
);
408 int mddev_congested(struct mddev
*mddev
, int bits
)
410 return mddev
->suspended
;
412 EXPORT_SYMBOL(mddev_congested
);
415 * Generic flush handling for md
418 static void md_end_flush(struct bio
*bio
, int err
)
420 struct md_rdev
*rdev
= bio
->bi_private
;
421 struct mddev
*mddev
= rdev
->mddev
;
423 rdev_dec_pending(rdev
, mddev
);
425 if (atomic_dec_and_test(&mddev
->flush_pending
)) {
426 /* The pre-request flush has finished */
427 queue_work(md_wq
, &mddev
->flush_work
);
432 static void md_submit_flush_data(struct work_struct
*ws
);
434 static void submit_flushes(struct work_struct
*ws
)
436 struct mddev
*mddev
= container_of(ws
, struct mddev
, flush_work
);
437 struct md_rdev
*rdev
;
439 INIT_WORK(&mddev
->flush_work
, md_submit_flush_data
);
440 atomic_set(&mddev
->flush_pending
, 1);
442 list_for_each_entry_rcu(rdev
, &mddev
->disks
, same_set
)
443 if (rdev
->raid_disk
>= 0 &&
444 !test_bit(Faulty
, &rdev
->flags
)) {
445 /* Take two references, one is dropped
446 * when request finishes, one after
447 * we reclaim rcu_read_lock
450 atomic_inc(&rdev
->nr_pending
);
451 atomic_inc(&rdev
->nr_pending
);
453 bi
= bio_alloc_mddev(GFP_KERNEL
, 0, mddev
);
454 bi
->bi_end_io
= md_end_flush
;
455 bi
->bi_private
= rdev
;
456 bi
->bi_bdev
= rdev
->bdev
;
457 atomic_inc(&mddev
->flush_pending
);
458 submit_bio(WRITE_FLUSH
, bi
);
460 rdev_dec_pending(rdev
, mddev
);
463 if (atomic_dec_and_test(&mddev
->flush_pending
))
464 queue_work(md_wq
, &mddev
->flush_work
);
467 static void md_submit_flush_data(struct work_struct
*ws
)
469 struct mddev
*mddev
= container_of(ws
, struct mddev
, flush_work
);
470 struct bio
*bio
= mddev
->flush_bio
;
472 if (bio
->bi_size
== 0)
473 /* an empty barrier - all done */
476 bio
->bi_rw
&= ~REQ_FLUSH
;
477 mddev
->pers
->make_request(mddev
, bio
);
480 mddev
->flush_bio
= NULL
;
481 wake_up(&mddev
->sb_wait
);
484 void md_flush_request(struct mddev
*mddev
, struct bio
*bio
)
486 spin_lock_irq(&mddev
->write_lock
);
487 wait_event_lock_irq(mddev
->sb_wait
,
489 mddev
->write_lock
, /*nothing*/);
490 mddev
->flush_bio
= bio
;
491 spin_unlock_irq(&mddev
->write_lock
);
493 INIT_WORK(&mddev
->flush_work
, submit_flushes
);
494 queue_work(md_wq
, &mddev
->flush_work
);
496 EXPORT_SYMBOL(md_flush_request
);
498 /* Support for plugging.
499 * This mirrors the plugging support in request_queue, but does not
500 * require having a whole queue or request structures.
501 * We allocate an md_plug_cb for each md device and each thread it gets
502 * plugged on. This links tot the private plug_handle structure in the
503 * personality data where we keep a count of the number of outstanding
504 * plugs so other code can see if a plug is active.
507 struct blk_plug_cb cb
;
511 static void plugger_unplug(struct blk_plug_cb
*cb
)
513 struct md_plug_cb
*mdcb
= container_of(cb
, struct md_plug_cb
, cb
);
514 if (atomic_dec_and_test(&mdcb
->mddev
->plug_cnt
))
515 md_wakeup_thread(mdcb
->mddev
->thread
);
519 /* Check that an unplug wakeup will come shortly.
520 * If not, wakeup the md thread immediately
522 int mddev_check_plugged(struct mddev
*mddev
)
524 struct blk_plug
*plug
= current
->plug
;
525 struct md_plug_cb
*mdcb
;
530 list_for_each_entry(mdcb
, &plug
->cb_list
, cb
.list
) {
531 if (mdcb
->cb
.callback
== plugger_unplug
&&
532 mdcb
->mddev
== mddev
) {
533 /* Already on the list, move to top */
534 if (mdcb
!= list_first_entry(&plug
->cb_list
,
537 list_move(&mdcb
->cb
.list
, &plug
->cb_list
);
541 /* Not currently on the callback list */
542 mdcb
= kmalloc(sizeof(*mdcb
), GFP_ATOMIC
);
547 mdcb
->cb
.callback
= plugger_unplug
;
548 atomic_inc(&mddev
->plug_cnt
);
549 list_add(&mdcb
->cb
.list
, &plug
->cb_list
);
552 EXPORT_SYMBOL_GPL(mddev_check_plugged
);
554 static inline struct mddev
*mddev_get(struct mddev
*mddev
)
556 atomic_inc(&mddev
->active
);
560 static void mddev_delayed_delete(struct work_struct
*ws
);
562 static void mddev_put(struct mddev
*mddev
)
564 struct bio_set
*bs
= NULL
;
566 if (!atomic_dec_and_lock(&mddev
->active
, &all_mddevs_lock
))
568 if (!mddev
->raid_disks
&& list_empty(&mddev
->disks
) &&
569 mddev
->ctime
== 0 && !mddev
->hold_active
) {
570 /* Array is not configured at all, and not held active,
572 list_del_init(&mddev
->all_mddevs
);
574 mddev
->bio_set
= NULL
;
575 if (mddev
->gendisk
) {
576 /* We did a probe so need to clean up. Call
577 * queue_work inside the spinlock so that
578 * flush_workqueue() after mddev_find will
579 * succeed in waiting for the work to be done.
581 INIT_WORK(&mddev
->del_work
, mddev_delayed_delete
);
582 queue_work(md_misc_wq
, &mddev
->del_work
);
586 spin_unlock(&all_mddevs_lock
);
591 void mddev_init(struct mddev
*mddev
)
593 mutex_init(&mddev
->open_mutex
);
594 mutex_init(&mddev
->reconfig_mutex
);
595 mutex_init(&mddev
->bitmap_info
.mutex
);
596 INIT_LIST_HEAD(&mddev
->disks
);
597 INIT_LIST_HEAD(&mddev
->all_mddevs
);
598 init_timer(&mddev
->safemode_timer
);
599 atomic_set(&mddev
->active
, 1);
600 atomic_set(&mddev
->openers
, 0);
601 atomic_set(&mddev
->active_io
, 0);
602 atomic_set(&mddev
->plug_cnt
, 0);
603 spin_lock_init(&mddev
->write_lock
);
604 atomic_set(&mddev
->flush_pending
, 0);
605 init_waitqueue_head(&mddev
->sb_wait
);
606 init_waitqueue_head(&mddev
->recovery_wait
);
607 mddev
->reshape_position
= MaxSector
;
608 mddev
->resync_min
= 0;
609 mddev
->resync_max
= MaxSector
;
610 mddev
->level
= LEVEL_NONE
;
612 EXPORT_SYMBOL_GPL(mddev_init
);
614 static struct mddev
* mddev_find(dev_t unit
)
616 struct mddev
*mddev
, *new = NULL
;
618 if (unit
&& MAJOR(unit
) != MD_MAJOR
)
619 unit
&= ~((1<<MdpMinorShift
)-1);
622 spin_lock(&all_mddevs_lock
);
625 list_for_each_entry(mddev
, &all_mddevs
, all_mddevs
)
626 if (mddev
->unit
== unit
) {
628 spin_unlock(&all_mddevs_lock
);
634 list_add(&new->all_mddevs
, &all_mddevs
);
635 spin_unlock(&all_mddevs_lock
);
636 new->hold_active
= UNTIL_IOCTL
;
640 /* find an unused unit number */
641 static int next_minor
= 512;
642 int start
= next_minor
;
646 dev
= MKDEV(MD_MAJOR
, next_minor
);
648 if (next_minor
> MINORMASK
)
650 if (next_minor
== start
) {
651 /* Oh dear, all in use. */
652 spin_unlock(&all_mddevs_lock
);
658 list_for_each_entry(mddev
, &all_mddevs
, all_mddevs
)
659 if (mddev
->unit
== dev
) {
665 new->md_minor
= MINOR(dev
);
666 new->hold_active
= UNTIL_STOP
;
667 list_add(&new->all_mddevs
, &all_mddevs
);
668 spin_unlock(&all_mddevs_lock
);
671 spin_unlock(&all_mddevs_lock
);
673 new = kzalloc(sizeof(*new), GFP_KERNEL
);
678 if (MAJOR(unit
) == MD_MAJOR
)
679 new->md_minor
= MINOR(unit
);
681 new->md_minor
= MINOR(unit
) >> MdpMinorShift
;
688 static inline int mddev_lock(struct mddev
* mddev
)
690 return mutex_lock_interruptible(&mddev
->reconfig_mutex
);
693 static inline int mddev_is_locked(struct mddev
*mddev
)
695 return mutex_is_locked(&mddev
->reconfig_mutex
);
698 static inline int mddev_trylock(struct mddev
* mddev
)
700 return mutex_trylock(&mddev
->reconfig_mutex
);
703 static struct attribute_group md_redundancy_group
;
705 static void mddev_unlock(struct mddev
* mddev
)
707 if (mddev
->to_remove
) {
708 /* These cannot be removed under reconfig_mutex as
709 * an access to the files will try to take reconfig_mutex
710 * while holding the file unremovable, which leads to
712 * So hold set sysfs_active while the remove in happeing,
713 * and anything else which might set ->to_remove or my
714 * otherwise change the sysfs namespace will fail with
715 * -EBUSY if sysfs_active is still set.
716 * We set sysfs_active under reconfig_mutex and elsewhere
717 * test it under the same mutex to ensure its correct value
720 struct attribute_group
*to_remove
= mddev
->to_remove
;
721 mddev
->to_remove
= NULL
;
722 mddev
->sysfs_active
= 1;
723 mutex_unlock(&mddev
->reconfig_mutex
);
725 if (mddev
->kobj
.sd
) {
726 if (to_remove
!= &md_redundancy_group
)
727 sysfs_remove_group(&mddev
->kobj
, to_remove
);
728 if (mddev
->pers
== NULL
||
729 mddev
->pers
->sync_request
== NULL
) {
730 sysfs_remove_group(&mddev
->kobj
, &md_redundancy_group
);
731 if (mddev
->sysfs_action
)
732 sysfs_put(mddev
->sysfs_action
);
733 mddev
->sysfs_action
= NULL
;
736 mddev
->sysfs_active
= 0;
738 mutex_unlock(&mddev
->reconfig_mutex
);
740 /* As we've dropped the mutex we need a spinlock to
741 * make sure the thread doesn't disappear
743 spin_lock(&pers_lock
);
744 md_wakeup_thread(mddev
->thread
);
745 spin_unlock(&pers_lock
);
748 static struct md_rdev
* find_rdev_nr(struct mddev
*mddev
, int nr
)
750 struct md_rdev
*rdev
;
752 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
753 if (rdev
->desc_nr
== nr
)
759 static struct md_rdev
* find_rdev(struct mddev
* mddev
, dev_t dev
)
761 struct md_rdev
*rdev
;
763 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
764 if (rdev
->bdev
->bd_dev
== dev
)
770 static struct md_personality
*find_pers(int level
, char *clevel
)
772 struct md_personality
*pers
;
773 list_for_each_entry(pers
, &pers_list
, list
) {
774 if (level
!= LEVEL_NONE
&& pers
->level
== level
)
776 if (strcmp(pers
->name
, clevel
)==0)
782 /* return the offset of the super block in 512byte sectors */
783 static inline sector_t
calc_dev_sboffset(struct md_rdev
*rdev
)
785 sector_t num_sectors
= i_size_read(rdev
->bdev
->bd_inode
) / 512;
786 return MD_NEW_SIZE_SECTORS(num_sectors
);
789 static int alloc_disk_sb(struct md_rdev
* rdev
)
794 rdev
->sb_page
= alloc_page(GFP_KERNEL
);
795 if (!rdev
->sb_page
) {
796 printk(KERN_ALERT
"md: out of memory.\n");
803 static void free_disk_sb(struct md_rdev
* rdev
)
806 put_page(rdev
->sb_page
);
808 rdev
->sb_page
= NULL
;
813 put_page(rdev
->bb_page
);
814 rdev
->bb_page
= NULL
;
819 static void super_written(struct bio
*bio
, int error
)
821 struct md_rdev
*rdev
= bio
->bi_private
;
822 struct mddev
*mddev
= rdev
->mddev
;
824 if (error
|| !test_bit(BIO_UPTODATE
, &bio
->bi_flags
)) {
825 printk("md: super_written gets error=%d, uptodate=%d\n",
826 error
, test_bit(BIO_UPTODATE
, &bio
->bi_flags
));
827 WARN_ON(test_bit(BIO_UPTODATE
, &bio
->bi_flags
));
828 md_error(mddev
, rdev
);
831 if (atomic_dec_and_test(&mddev
->pending_writes
))
832 wake_up(&mddev
->sb_wait
);
836 void md_super_write(struct mddev
*mddev
, struct md_rdev
*rdev
,
837 sector_t sector
, int size
, struct page
*page
)
839 /* write first size bytes of page to sector of rdev
840 * Increment mddev->pending_writes before returning
841 * and decrement it on completion, waking up sb_wait
842 * if zero is reached.
843 * If an error occurred, call md_error
845 struct bio
*bio
= bio_alloc_mddev(GFP_NOIO
, 1, mddev
);
847 bio
->bi_bdev
= rdev
->meta_bdev
? rdev
->meta_bdev
: rdev
->bdev
;
848 bio
->bi_sector
= sector
;
849 bio_add_page(bio
, page
, size
, 0);
850 bio
->bi_private
= rdev
;
851 bio
->bi_end_io
= super_written
;
853 atomic_inc(&mddev
->pending_writes
);
854 submit_bio(WRITE_FLUSH_FUA
, bio
);
857 void md_super_wait(struct mddev
*mddev
)
859 /* wait for all superblock writes that were scheduled to complete */
862 prepare_to_wait(&mddev
->sb_wait
, &wq
, TASK_UNINTERRUPTIBLE
);
863 if (atomic_read(&mddev
->pending_writes
)==0)
867 finish_wait(&mddev
->sb_wait
, &wq
);
870 static void bi_complete(struct bio
*bio
, int error
)
872 complete((struct completion
*)bio
->bi_private
);
875 int sync_page_io(struct md_rdev
*rdev
, sector_t sector
, int size
,
876 struct page
*page
, int rw
, bool metadata_op
)
878 struct bio
*bio
= bio_alloc_mddev(GFP_NOIO
, 1, rdev
->mddev
);
879 struct completion event
;
884 bio
->bi_bdev
= (metadata_op
&& rdev
->meta_bdev
) ?
885 rdev
->meta_bdev
: rdev
->bdev
;
887 bio
->bi_sector
= sector
+ rdev
->sb_start
;
889 bio
->bi_sector
= sector
+ rdev
->data_offset
;
890 bio_add_page(bio
, page
, size
, 0);
891 init_completion(&event
);
892 bio
->bi_private
= &event
;
893 bio
->bi_end_io
= bi_complete
;
895 wait_for_completion(&event
);
897 ret
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
901 EXPORT_SYMBOL_GPL(sync_page_io
);
903 static int read_disk_sb(struct md_rdev
* rdev
, int size
)
905 char b
[BDEVNAME_SIZE
];
906 if (!rdev
->sb_page
) {
914 if (!sync_page_io(rdev
, 0, size
, rdev
->sb_page
, READ
, true))
920 printk(KERN_WARNING
"md: disabled device %s, could not read superblock.\n",
921 bdevname(rdev
->bdev
,b
));
925 static int uuid_equal(mdp_super_t
*sb1
, mdp_super_t
*sb2
)
927 return sb1
->set_uuid0
== sb2
->set_uuid0
&&
928 sb1
->set_uuid1
== sb2
->set_uuid1
&&
929 sb1
->set_uuid2
== sb2
->set_uuid2
&&
930 sb1
->set_uuid3
== sb2
->set_uuid3
;
933 static int sb_equal(mdp_super_t
*sb1
, mdp_super_t
*sb2
)
936 mdp_super_t
*tmp1
, *tmp2
;
938 tmp1
= kmalloc(sizeof(*tmp1
),GFP_KERNEL
);
939 tmp2
= kmalloc(sizeof(*tmp2
),GFP_KERNEL
);
941 if (!tmp1
|| !tmp2
) {
943 printk(KERN_INFO
"md.c sb_equal(): failed to allocate memory!\n");
951 * nr_disks is not constant
956 ret
= (memcmp(tmp1
, tmp2
, MD_SB_GENERIC_CONSTANT_WORDS
* 4) == 0);
964 static u32
md_csum_fold(u32 csum
)
966 csum
= (csum
& 0xffff) + (csum
>> 16);
967 return (csum
& 0xffff) + (csum
>> 16);
970 static unsigned int calc_sb_csum(mdp_super_t
* sb
)
973 u32
*sb32
= (u32
*)sb
;
975 unsigned int disk_csum
, csum
;
977 disk_csum
= sb
->sb_csum
;
980 for (i
= 0; i
< MD_SB_BYTES
/4 ; i
++)
982 csum
= (newcsum
& 0xffffffff) + (newcsum
>>32);
986 /* This used to use csum_partial, which was wrong for several
987 * reasons including that different results are returned on
988 * different architectures. It isn't critical that we get exactly
989 * the same return value as before (we always csum_fold before
990 * testing, and that removes any differences). However as we
991 * know that csum_partial always returned a 16bit value on
992 * alphas, do a fold to maximise conformity to previous behaviour.
994 sb
->sb_csum
= md_csum_fold(disk_csum
);
996 sb
->sb_csum
= disk_csum
;
1003 * Handle superblock details.
1004 * We want to be able to handle multiple superblock formats
1005 * so we have a common interface to them all, and an array of
1006 * different handlers.
1007 * We rely on user-space to write the initial superblock, and support
1008 * reading and updating of superblocks.
1009 * Interface methods are:
1010 * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
1011 * loads and validates a superblock on dev.
1012 * if refdev != NULL, compare superblocks on both devices
1014 * 0 - dev has a superblock that is compatible with refdev
1015 * 1 - dev has a superblock that is compatible and newer than refdev
1016 * so dev should be used as the refdev in future
1017 * -EINVAL superblock incompatible or invalid
1018 * -othererror e.g. -EIO
1020 * int validate_super(struct mddev *mddev, struct md_rdev *dev)
1021 * Verify that dev is acceptable into mddev.
1022 * The first time, mddev->raid_disks will be 0, and data from
1023 * dev should be merged in. Subsequent calls check that dev
1024 * is new enough. Return 0 or -EINVAL
1026 * void sync_super(struct mddev *mddev, struct md_rdev *dev)
1027 * Update the superblock for rdev with data in mddev
1028 * This does not write to disc.
1034 struct module
*owner
;
1035 int (*load_super
)(struct md_rdev
*rdev
, struct md_rdev
*refdev
,
1037 int (*validate_super
)(struct mddev
*mddev
, struct md_rdev
*rdev
);
1038 void (*sync_super
)(struct mddev
*mddev
, struct md_rdev
*rdev
);
1039 unsigned long long (*rdev_size_change
)(struct md_rdev
*rdev
,
1040 sector_t num_sectors
);
1044 * Check that the given mddev has no bitmap.
1046 * This function is called from the run method of all personalities that do not
1047 * support bitmaps. It prints an error message and returns non-zero if mddev
1048 * has a bitmap. Otherwise, it returns 0.
1051 int md_check_no_bitmap(struct mddev
*mddev
)
1053 if (!mddev
->bitmap_info
.file
&& !mddev
->bitmap_info
.offset
)
1055 printk(KERN_ERR
"%s: bitmaps are not supported for %s\n",
1056 mdname(mddev
), mddev
->pers
->name
);
1059 EXPORT_SYMBOL(md_check_no_bitmap
);
1062 * load_super for 0.90.0
1064 static int super_90_load(struct md_rdev
*rdev
, struct md_rdev
*refdev
, int minor_version
)
1066 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
1071 * Calculate the position of the superblock (512byte sectors),
1072 * it's at the end of the disk.
1074 * It also happens to be a multiple of 4Kb.
1076 rdev
->sb_start
= calc_dev_sboffset(rdev
);
1078 ret
= read_disk_sb(rdev
, MD_SB_BYTES
);
1079 if (ret
) return ret
;
1083 bdevname(rdev
->bdev
, b
);
1084 sb
= page_address(rdev
->sb_page
);
1086 if (sb
->md_magic
!= MD_SB_MAGIC
) {
1087 printk(KERN_ERR
"md: invalid raid superblock magic on %s\n",
1092 if (sb
->major_version
!= 0 ||
1093 sb
->minor_version
< 90 ||
1094 sb
->minor_version
> 91) {
1095 printk(KERN_WARNING
"Bad version number %d.%d on %s\n",
1096 sb
->major_version
, sb
->minor_version
,
1101 if (sb
->raid_disks
<= 0)
1104 if (md_csum_fold(calc_sb_csum(sb
)) != md_csum_fold(sb
->sb_csum
)) {
1105 printk(KERN_WARNING
"md: invalid superblock checksum on %s\n",
1110 rdev
->preferred_minor
= sb
->md_minor
;
1111 rdev
->data_offset
= 0;
1112 rdev
->sb_size
= MD_SB_BYTES
;
1113 rdev
->badblocks
.shift
= -1;
1115 if (sb
->level
== LEVEL_MULTIPATH
)
1118 rdev
->desc_nr
= sb
->this_disk
.number
;
1124 mdp_super_t
*refsb
= page_address(refdev
->sb_page
);
1125 if (!uuid_equal(refsb
, sb
)) {
1126 printk(KERN_WARNING
"md: %s has different UUID to %s\n",
1127 b
, bdevname(refdev
->bdev
,b2
));
1130 if (!sb_equal(refsb
, sb
)) {
1131 printk(KERN_WARNING
"md: %s has same UUID"
1132 " but different superblock to %s\n",
1133 b
, bdevname(refdev
->bdev
, b2
));
1137 ev2
= md_event(refsb
);
1143 rdev
->sectors
= rdev
->sb_start
;
1144 /* Limit to 4TB as metadata cannot record more than that */
1145 if (rdev
->sectors
>= (2ULL << 32))
1146 rdev
->sectors
= (2ULL << 32) - 2;
1148 if (rdev
->sectors
< ((sector_t
)sb
->size
) * 2 && sb
->level
>= 1)
1149 /* "this cannot possibly happen" ... */
1157 * validate_super for 0.90.0
1159 static int super_90_validate(struct mddev
*mddev
, struct md_rdev
*rdev
)
1162 mdp_super_t
*sb
= page_address(rdev
->sb_page
);
1163 __u64 ev1
= md_event(sb
);
1165 rdev
->raid_disk
= -1;
1166 clear_bit(Faulty
, &rdev
->flags
);
1167 clear_bit(In_sync
, &rdev
->flags
);
1168 clear_bit(WriteMostly
, &rdev
->flags
);
1170 if (mddev
->raid_disks
== 0) {
1171 mddev
->major_version
= 0;
1172 mddev
->minor_version
= sb
->minor_version
;
1173 mddev
->patch_version
= sb
->patch_version
;
1174 mddev
->external
= 0;
1175 mddev
->chunk_sectors
= sb
->chunk_size
>> 9;
1176 mddev
->ctime
= sb
->ctime
;
1177 mddev
->utime
= sb
->utime
;
1178 mddev
->level
= sb
->level
;
1179 mddev
->clevel
[0] = 0;
1180 mddev
->layout
= sb
->layout
;
1181 mddev
->raid_disks
= sb
->raid_disks
;
1182 mddev
->dev_sectors
= ((sector_t
)sb
->size
) * 2;
1183 mddev
->events
= ev1
;
1184 mddev
->bitmap_info
.offset
= 0;
1185 mddev
->bitmap_info
.default_offset
= MD_SB_BYTES
>> 9;
1187 if (mddev
->minor_version
>= 91) {
1188 mddev
->reshape_position
= sb
->reshape_position
;
1189 mddev
->delta_disks
= sb
->delta_disks
;
1190 mddev
->new_level
= sb
->new_level
;
1191 mddev
->new_layout
= sb
->new_layout
;
1192 mddev
->new_chunk_sectors
= sb
->new_chunk
>> 9;
1194 mddev
->reshape_position
= MaxSector
;
1195 mddev
->delta_disks
= 0;
1196 mddev
->new_level
= mddev
->level
;
1197 mddev
->new_layout
= mddev
->layout
;
1198 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
1201 if (sb
->state
& (1<<MD_SB_CLEAN
))
1202 mddev
->recovery_cp
= MaxSector
;
1204 if (sb
->events_hi
== sb
->cp_events_hi
&&
1205 sb
->events_lo
== sb
->cp_events_lo
) {
1206 mddev
->recovery_cp
= sb
->recovery_cp
;
1208 mddev
->recovery_cp
= 0;
1211 memcpy(mddev
->uuid
+0, &sb
->set_uuid0
, 4);
1212 memcpy(mddev
->uuid
+4, &sb
->set_uuid1
, 4);
1213 memcpy(mddev
->uuid
+8, &sb
->set_uuid2
, 4);
1214 memcpy(mddev
->uuid
+12,&sb
->set_uuid3
, 4);
1216 mddev
->max_disks
= MD_SB_DISKS
;
1218 if (sb
->state
& (1<<MD_SB_BITMAP_PRESENT
) &&
1219 mddev
->bitmap_info
.file
== NULL
)
1220 mddev
->bitmap_info
.offset
=
1221 mddev
->bitmap_info
.default_offset
;
1223 } else if (mddev
->pers
== NULL
) {
1224 /* Insist on good event counter while assembling, except
1225 * for spares (which don't need an event count) */
1227 if (sb
->disks
[rdev
->desc_nr
].state
& (
1228 (1<<MD_DISK_SYNC
) | (1 << MD_DISK_ACTIVE
)))
1229 if (ev1
< mddev
->events
)
1231 } else if (mddev
->bitmap
) {
1232 /* if adding to array with a bitmap, then we can accept an
1233 * older device ... but not too old.
1235 if (ev1
< mddev
->bitmap
->events_cleared
)
1238 if (ev1
< mddev
->events
)
1239 /* just a hot-add of a new device, leave raid_disk at -1 */
1243 if (mddev
->level
!= LEVEL_MULTIPATH
) {
1244 desc
= sb
->disks
+ rdev
->desc_nr
;
1246 if (desc
->state
& (1<<MD_DISK_FAULTY
))
1247 set_bit(Faulty
, &rdev
->flags
);
1248 else if (desc
->state
& (1<<MD_DISK_SYNC
) /* &&
1249 desc->raid_disk < mddev->raid_disks */) {
1250 set_bit(In_sync
, &rdev
->flags
);
1251 rdev
->raid_disk
= desc
->raid_disk
;
1252 } else if (desc
->state
& (1<<MD_DISK_ACTIVE
)) {
1253 /* active but not in sync implies recovery up to
1254 * reshape position. We don't know exactly where
1255 * that is, so set to zero for now */
1256 if (mddev
->minor_version
>= 91) {
1257 rdev
->recovery_offset
= 0;
1258 rdev
->raid_disk
= desc
->raid_disk
;
1261 if (desc
->state
& (1<<MD_DISK_WRITEMOSTLY
))
1262 set_bit(WriteMostly
, &rdev
->flags
);
1263 } else /* MULTIPATH are always insync */
1264 set_bit(In_sync
, &rdev
->flags
);
1269 * sync_super for 0.90.0
1271 static void super_90_sync(struct mddev
*mddev
, struct md_rdev
*rdev
)
1274 struct md_rdev
*rdev2
;
1275 int next_spare
= mddev
->raid_disks
;
1278 /* make rdev->sb match mddev data..
1281 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1282 * 3/ any empty disks < next_spare become removed
1284 * disks[0] gets initialised to REMOVED because
1285 * we cannot be sure from other fields if it has
1286 * been initialised or not.
1289 int active
=0, working
=0,failed
=0,spare
=0,nr_disks
=0;
1291 rdev
->sb_size
= MD_SB_BYTES
;
1293 sb
= page_address(rdev
->sb_page
);
1295 memset(sb
, 0, sizeof(*sb
));
1297 sb
->md_magic
= MD_SB_MAGIC
;
1298 sb
->major_version
= mddev
->major_version
;
1299 sb
->patch_version
= mddev
->patch_version
;
1300 sb
->gvalid_words
= 0; /* ignored */
1301 memcpy(&sb
->set_uuid0
, mddev
->uuid
+0, 4);
1302 memcpy(&sb
->set_uuid1
, mddev
->uuid
+4, 4);
1303 memcpy(&sb
->set_uuid2
, mddev
->uuid
+8, 4);
1304 memcpy(&sb
->set_uuid3
, mddev
->uuid
+12,4);
1306 sb
->ctime
= mddev
->ctime
;
1307 sb
->level
= mddev
->level
;
1308 sb
->size
= mddev
->dev_sectors
/ 2;
1309 sb
->raid_disks
= mddev
->raid_disks
;
1310 sb
->md_minor
= mddev
->md_minor
;
1311 sb
->not_persistent
= 0;
1312 sb
->utime
= mddev
->utime
;
1314 sb
->events_hi
= (mddev
->events
>>32);
1315 sb
->events_lo
= (u32
)mddev
->events
;
1317 if (mddev
->reshape_position
== MaxSector
)
1318 sb
->minor_version
= 90;
1320 sb
->minor_version
= 91;
1321 sb
->reshape_position
= mddev
->reshape_position
;
1322 sb
->new_level
= mddev
->new_level
;
1323 sb
->delta_disks
= mddev
->delta_disks
;
1324 sb
->new_layout
= mddev
->new_layout
;
1325 sb
->new_chunk
= mddev
->new_chunk_sectors
<< 9;
1327 mddev
->minor_version
= sb
->minor_version
;
1330 sb
->recovery_cp
= mddev
->recovery_cp
;
1331 sb
->cp_events_hi
= (mddev
->events
>>32);
1332 sb
->cp_events_lo
= (u32
)mddev
->events
;
1333 if (mddev
->recovery_cp
== MaxSector
)
1334 sb
->state
= (1<< MD_SB_CLEAN
);
1336 sb
->recovery_cp
= 0;
1338 sb
->layout
= mddev
->layout
;
1339 sb
->chunk_size
= mddev
->chunk_sectors
<< 9;
1341 if (mddev
->bitmap
&& mddev
->bitmap_info
.file
== NULL
)
1342 sb
->state
|= (1<<MD_SB_BITMAP_PRESENT
);
1344 sb
->disks
[0].state
= (1<<MD_DISK_REMOVED
);
1345 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
) {
1348 int is_active
= test_bit(In_sync
, &rdev2
->flags
);
1350 if (rdev2
->raid_disk
>= 0 &&
1351 sb
->minor_version
>= 91)
1352 /* we have nowhere to store the recovery_offset,
1353 * but if it is not below the reshape_position,
1354 * we can piggy-back on that.
1357 if (rdev2
->raid_disk
< 0 ||
1358 test_bit(Faulty
, &rdev2
->flags
))
1361 desc_nr
= rdev2
->raid_disk
;
1363 desc_nr
= next_spare
++;
1364 rdev2
->desc_nr
= desc_nr
;
1365 d
= &sb
->disks
[rdev2
->desc_nr
];
1367 d
->number
= rdev2
->desc_nr
;
1368 d
->major
= MAJOR(rdev2
->bdev
->bd_dev
);
1369 d
->minor
= MINOR(rdev2
->bdev
->bd_dev
);
1371 d
->raid_disk
= rdev2
->raid_disk
;
1373 d
->raid_disk
= rdev2
->desc_nr
; /* compatibility */
1374 if (test_bit(Faulty
, &rdev2
->flags
))
1375 d
->state
= (1<<MD_DISK_FAULTY
);
1376 else if (is_active
) {
1377 d
->state
= (1<<MD_DISK_ACTIVE
);
1378 if (test_bit(In_sync
, &rdev2
->flags
))
1379 d
->state
|= (1<<MD_DISK_SYNC
);
1387 if (test_bit(WriteMostly
, &rdev2
->flags
))
1388 d
->state
|= (1<<MD_DISK_WRITEMOSTLY
);
1390 /* now set the "removed" and "faulty" bits on any missing devices */
1391 for (i
=0 ; i
< mddev
->raid_disks
; i
++) {
1392 mdp_disk_t
*d
= &sb
->disks
[i
];
1393 if (d
->state
== 0 && d
->number
== 0) {
1396 d
->state
= (1<<MD_DISK_REMOVED
);
1397 d
->state
|= (1<<MD_DISK_FAULTY
);
1401 sb
->nr_disks
= nr_disks
;
1402 sb
->active_disks
= active
;
1403 sb
->working_disks
= working
;
1404 sb
->failed_disks
= failed
;
1405 sb
->spare_disks
= spare
;
1407 sb
->this_disk
= sb
->disks
[rdev
->desc_nr
];
1408 sb
->sb_csum
= calc_sb_csum(sb
);
1412 * rdev_size_change for 0.90.0
1414 static unsigned long long
1415 super_90_rdev_size_change(struct md_rdev
*rdev
, sector_t num_sectors
)
1417 if (num_sectors
&& num_sectors
< rdev
->mddev
->dev_sectors
)
1418 return 0; /* component must fit device */
1419 if (rdev
->mddev
->bitmap_info
.offset
)
1420 return 0; /* can't move bitmap */
1421 rdev
->sb_start
= calc_dev_sboffset(rdev
);
1422 if (!num_sectors
|| num_sectors
> rdev
->sb_start
)
1423 num_sectors
= rdev
->sb_start
;
1424 /* Limit to 4TB as metadata cannot record more than that.
1425 * 4TB == 2^32 KB, or 2*2^32 sectors.
1427 if (num_sectors
>= (2ULL << 32))
1428 num_sectors
= (2ULL << 32) - 2;
1429 md_super_write(rdev
->mddev
, rdev
, rdev
->sb_start
, rdev
->sb_size
,
1431 md_super_wait(rdev
->mddev
);
1437 * version 1 superblock
1440 static __le32
calc_sb_1_csum(struct mdp_superblock_1
* sb
)
1444 unsigned long long newcsum
;
1445 int size
= 256 + le32_to_cpu(sb
->max_dev
)*2;
1446 __le32
*isuper
= (__le32
*)sb
;
1449 disk_csum
= sb
->sb_csum
;
1452 for (i
=0; size
>=4; size
-= 4 )
1453 newcsum
+= le32_to_cpu(*isuper
++);
1456 newcsum
+= le16_to_cpu(*(__le16
*) isuper
);
1458 csum
= (newcsum
& 0xffffffff) + (newcsum
>> 32);
1459 sb
->sb_csum
= disk_csum
;
1460 return cpu_to_le32(csum
);
1463 static int md_set_badblocks(struct badblocks
*bb
, sector_t s
, int sectors
,
1465 static int super_1_load(struct md_rdev
*rdev
, struct md_rdev
*refdev
, int minor_version
)
1467 struct mdp_superblock_1
*sb
;
1470 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
1474 * Calculate the position of the superblock in 512byte sectors.
1475 * It is always aligned to a 4K boundary and
1476 * depeding on minor_version, it can be:
1477 * 0: At least 8K, but less than 12K, from end of device
1478 * 1: At start of device
1479 * 2: 4K from start of device.
1481 switch(minor_version
) {
1483 sb_start
= i_size_read(rdev
->bdev
->bd_inode
) >> 9;
1485 sb_start
&= ~(sector_t
)(4*2-1);
1496 rdev
->sb_start
= sb_start
;
1498 /* superblock is rarely larger than 1K, but it can be larger,
1499 * and it is safe to read 4k, so we do that
1501 ret
= read_disk_sb(rdev
, 4096);
1502 if (ret
) return ret
;
1505 sb
= page_address(rdev
->sb_page
);
1507 if (sb
->magic
!= cpu_to_le32(MD_SB_MAGIC
) ||
1508 sb
->major_version
!= cpu_to_le32(1) ||
1509 le32_to_cpu(sb
->max_dev
) > (4096-256)/2 ||
1510 le64_to_cpu(sb
->super_offset
) != rdev
->sb_start
||
1511 (le32_to_cpu(sb
->feature_map
) & ~MD_FEATURE_ALL
) != 0)
1514 if (calc_sb_1_csum(sb
) != sb
->sb_csum
) {
1515 printk("md: invalid superblock checksum on %s\n",
1516 bdevname(rdev
->bdev
,b
));
1519 if (le64_to_cpu(sb
->data_size
) < 10) {
1520 printk("md: data_size too small on %s\n",
1521 bdevname(rdev
->bdev
,b
));
1525 rdev
->preferred_minor
= 0xffff;
1526 rdev
->data_offset
= le64_to_cpu(sb
->data_offset
);
1527 atomic_set(&rdev
->corrected_errors
, le32_to_cpu(sb
->cnt_corrected_read
));
1529 rdev
->sb_size
= le32_to_cpu(sb
->max_dev
) * 2 + 256;
1530 bmask
= queue_logical_block_size(rdev
->bdev
->bd_disk
->queue
)-1;
1531 if (rdev
->sb_size
& bmask
)
1532 rdev
->sb_size
= (rdev
->sb_size
| bmask
) + 1;
1535 && rdev
->data_offset
< sb_start
+ (rdev
->sb_size
/512))
1538 if (sb
->level
== cpu_to_le32(LEVEL_MULTIPATH
))
1541 rdev
->desc_nr
= le32_to_cpu(sb
->dev_number
);
1543 if (!rdev
->bb_page
) {
1544 rdev
->bb_page
= alloc_page(GFP_KERNEL
);
1548 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_BAD_BLOCKS
) &&
1549 rdev
->badblocks
.count
== 0) {
1550 /* need to load the bad block list.
1551 * Currently we limit it to one page.
1557 int sectors
= le16_to_cpu(sb
->bblog_size
);
1558 if (sectors
> (PAGE_SIZE
/ 512))
1560 offset
= le32_to_cpu(sb
->bblog_offset
);
1563 bb_sector
= (long long)offset
;
1564 if (!sync_page_io(rdev
, bb_sector
, sectors
<< 9,
1565 rdev
->bb_page
, READ
, true))
1567 bbp
= (u64
*)page_address(rdev
->bb_page
);
1568 rdev
->badblocks
.shift
= sb
->bblog_shift
;
1569 for (i
= 0 ; i
< (sectors
<< (9-3)) ; i
++, bbp
++) {
1570 u64 bb
= le64_to_cpu(*bbp
);
1571 int count
= bb
& (0x3ff);
1572 u64 sector
= bb
>> 10;
1573 sector
<<= sb
->bblog_shift
;
1574 count
<<= sb
->bblog_shift
;
1577 if (md_set_badblocks(&rdev
->badblocks
,
1578 sector
, count
, 1) == 0)
1581 } else if (sb
->bblog_offset
== 0)
1582 rdev
->badblocks
.shift
= -1;
1588 struct mdp_superblock_1
*refsb
= page_address(refdev
->sb_page
);
1590 if (memcmp(sb
->set_uuid
, refsb
->set_uuid
, 16) != 0 ||
1591 sb
->level
!= refsb
->level
||
1592 sb
->layout
!= refsb
->layout
||
1593 sb
->chunksize
!= refsb
->chunksize
) {
1594 printk(KERN_WARNING
"md: %s has strangely different"
1595 " superblock to %s\n",
1596 bdevname(rdev
->bdev
,b
),
1597 bdevname(refdev
->bdev
,b2
));
1600 ev1
= le64_to_cpu(sb
->events
);
1601 ev2
= le64_to_cpu(refsb
->events
);
1609 rdev
->sectors
= (i_size_read(rdev
->bdev
->bd_inode
) >> 9) -
1610 le64_to_cpu(sb
->data_offset
);
1612 rdev
->sectors
= rdev
->sb_start
;
1613 if (rdev
->sectors
< le64_to_cpu(sb
->data_size
))
1615 rdev
->sectors
= le64_to_cpu(sb
->data_size
);
1616 if (le64_to_cpu(sb
->size
) > rdev
->sectors
)
1621 static int super_1_validate(struct mddev
*mddev
, struct md_rdev
*rdev
)
1623 struct mdp_superblock_1
*sb
= page_address(rdev
->sb_page
);
1624 __u64 ev1
= le64_to_cpu(sb
->events
);
1626 rdev
->raid_disk
= -1;
1627 clear_bit(Faulty
, &rdev
->flags
);
1628 clear_bit(In_sync
, &rdev
->flags
);
1629 clear_bit(WriteMostly
, &rdev
->flags
);
1631 if (mddev
->raid_disks
== 0) {
1632 mddev
->major_version
= 1;
1633 mddev
->patch_version
= 0;
1634 mddev
->external
= 0;
1635 mddev
->chunk_sectors
= le32_to_cpu(sb
->chunksize
);
1636 mddev
->ctime
= le64_to_cpu(sb
->ctime
) & ((1ULL << 32)-1);
1637 mddev
->utime
= le64_to_cpu(sb
->utime
) & ((1ULL << 32)-1);
1638 mddev
->level
= le32_to_cpu(sb
->level
);
1639 mddev
->clevel
[0] = 0;
1640 mddev
->layout
= le32_to_cpu(sb
->layout
);
1641 mddev
->raid_disks
= le32_to_cpu(sb
->raid_disks
);
1642 mddev
->dev_sectors
= le64_to_cpu(sb
->size
);
1643 mddev
->events
= ev1
;
1644 mddev
->bitmap_info
.offset
= 0;
1645 mddev
->bitmap_info
.default_offset
= 1024 >> 9;
1647 mddev
->recovery_cp
= le64_to_cpu(sb
->resync_offset
);
1648 memcpy(mddev
->uuid
, sb
->set_uuid
, 16);
1650 mddev
->max_disks
= (4096-256)/2;
1652 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_BITMAP_OFFSET
) &&
1653 mddev
->bitmap_info
.file
== NULL
)
1654 mddev
->bitmap_info
.offset
=
1655 (__s32
)le32_to_cpu(sb
->bitmap_offset
);
1657 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RESHAPE_ACTIVE
)) {
1658 mddev
->reshape_position
= le64_to_cpu(sb
->reshape_position
);
1659 mddev
->delta_disks
= le32_to_cpu(sb
->delta_disks
);
1660 mddev
->new_level
= le32_to_cpu(sb
->new_level
);
1661 mddev
->new_layout
= le32_to_cpu(sb
->new_layout
);
1662 mddev
->new_chunk_sectors
= le32_to_cpu(sb
->new_chunk
);
1664 mddev
->reshape_position
= MaxSector
;
1665 mddev
->delta_disks
= 0;
1666 mddev
->new_level
= mddev
->level
;
1667 mddev
->new_layout
= mddev
->layout
;
1668 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
1671 } else if (mddev
->pers
== NULL
) {
1672 /* Insist of good event counter while assembling, except for
1673 * spares (which don't need an event count) */
1675 if (rdev
->desc_nr
>= 0 &&
1676 rdev
->desc_nr
< le32_to_cpu(sb
->max_dev
) &&
1677 le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]) < 0xfffe)
1678 if (ev1
< mddev
->events
)
1680 } else if (mddev
->bitmap
) {
1681 /* If adding to array with a bitmap, then we can accept an
1682 * older device, but not too old.
1684 if (ev1
< mddev
->bitmap
->events_cleared
)
1687 if (ev1
< mddev
->events
)
1688 /* just a hot-add of a new device, leave raid_disk at -1 */
1691 if (mddev
->level
!= LEVEL_MULTIPATH
) {
1693 if (rdev
->desc_nr
< 0 ||
1694 rdev
->desc_nr
>= le32_to_cpu(sb
->max_dev
)) {
1698 role
= le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]);
1700 case 0xffff: /* spare */
1702 case 0xfffe: /* faulty */
1703 set_bit(Faulty
, &rdev
->flags
);
1706 if ((le32_to_cpu(sb
->feature_map
) &
1707 MD_FEATURE_RECOVERY_OFFSET
))
1708 rdev
->recovery_offset
= le64_to_cpu(sb
->recovery_offset
);
1710 set_bit(In_sync
, &rdev
->flags
);
1711 rdev
->raid_disk
= role
;
1714 if (sb
->devflags
& WriteMostly1
)
1715 set_bit(WriteMostly
, &rdev
->flags
);
1716 if (le32_to_cpu(sb
->feature_map
) & MD_FEATURE_REPLACEMENT
)
1717 set_bit(Replacement
, &rdev
->flags
);
1718 } else /* MULTIPATH are always insync */
1719 set_bit(In_sync
, &rdev
->flags
);
1724 static void super_1_sync(struct mddev
*mddev
, struct md_rdev
*rdev
)
1726 struct mdp_superblock_1
*sb
;
1727 struct md_rdev
*rdev2
;
1729 /* make rdev->sb match mddev and rdev data. */
1731 sb
= page_address(rdev
->sb_page
);
1733 sb
->feature_map
= 0;
1735 sb
->recovery_offset
= cpu_to_le64(0);
1736 memset(sb
->pad1
, 0, sizeof(sb
->pad1
));
1737 memset(sb
->pad3
, 0, sizeof(sb
->pad3
));
1739 sb
->utime
= cpu_to_le64((__u64
)mddev
->utime
);
1740 sb
->events
= cpu_to_le64(mddev
->events
);
1742 sb
->resync_offset
= cpu_to_le64(mddev
->recovery_cp
);
1744 sb
->resync_offset
= cpu_to_le64(0);
1746 sb
->cnt_corrected_read
= cpu_to_le32(atomic_read(&rdev
->corrected_errors
));
1748 sb
->raid_disks
= cpu_to_le32(mddev
->raid_disks
);
1749 sb
->size
= cpu_to_le64(mddev
->dev_sectors
);
1750 sb
->chunksize
= cpu_to_le32(mddev
->chunk_sectors
);
1751 sb
->level
= cpu_to_le32(mddev
->level
);
1752 sb
->layout
= cpu_to_le32(mddev
->layout
);
1754 if (test_bit(WriteMostly
, &rdev
->flags
))
1755 sb
->devflags
|= WriteMostly1
;
1757 sb
->devflags
&= ~WriteMostly1
;
1759 if (mddev
->bitmap
&& mddev
->bitmap_info
.file
== NULL
) {
1760 sb
->bitmap_offset
= cpu_to_le32((__u32
)mddev
->bitmap_info
.offset
);
1761 sb
->feature_map
= cpu_to_le32(MD_FEATURE_BITMAP_OFFSET
);
1764 if (rdev
->raid_disk
>= 0 &&
1765 !test_bit(In_sync
, &rdev
->flags
)) {
1767 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET
);
1768 sb
->recovery_offset
=
1769 cpu_to_le64(rdev
->recovery_offset
);
1771 if (test_bit(Replacement
, &rdev
->flags
))
1773 cpu_to_le32(MD_FEATURE_REPLACEMENT
);
1775 if (mddev
->reshape_position
!= MaxSector
) {
1776 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE
);
1777 sb
->reshape_position
= cpu_to_le64(mddev
->reshape_position
);
1778 sb
->new_layout
= cpu_to_le32(mddev
->new_layout
);
1779 sb
->delta_disks
= cpu_to_le32(mddev
->delta_disks
);
1780 sb
->new_level
= cpu_to_le32(mddev
->new_level
);
1781 sb
->new_chunk
= cpu_to_le32(mddev
->new_chunk_sectors
);
1784 if (rdev
->badblocks
.count
== 0)
1785 /* Nothing to do for bad blocks*/ ;
1786 else if (sb
->bblog_offset
== 0)
1787 /* Cannot record bad blocks on this device */
1788 md_error(mddev
, rdev
);
1790 struct badblocks
*bb
= &rdev
->badblocks
;
1791 u64
*bbp
= (u64
*)page_address(rdev
->bb_page
);
1793 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_BAD_BLOCKS
);
1798 seq
= read_seqbegin(&bb
->lock
);
1800 memset(bbp
, 0xff, PAGE_SIZE
);
1802 for (i
= 0 ; i
< bb
->count
; i
++) {
1803 u64 internal_bb
= *p
++;
1804 u64 store_bb
= ((BB_OFFSET(internal_bb
) << 10)
1805 | BB_LEN(internal_bb
));
1806 *bbp
++ = cpu_to_le64(store_bb
);
1809 if (read_seqretry(&bb
->lock
, seq
))
1812 bb
->sector
= (rdev
->sb_start
+
1813 (int)le32_to_cpu(sb
->bblog_offset
));
1814 bb
->size
= le16_to_cpu(sb
->bblog_size
);
1819 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
)
1820 if (rdev2
->desc_nr
+1 > max_dev
)
1821 max_dev
= rdev2
->desc_nr
+1;
1823 if (max_dev
> le32_to_cpu(sb
->max_dev
)) {
1825 sb
->max_dev
= cpu_to_le32(max_dev
);
1826 rdev
->sb_size
= max_dev
* 2 + 256;
1827 bmask
= queue_logical_block_size(rdev
->bdev
->bd_disk
->queue
)-1;
1828 if (rdev
->sb_size
& bmask
)
1829 rdev
->sb_size
= (rdev
->sb_size
| bmask
) + 1;
1831 max_dev
= le32_to_cpu(sb
->max_dev
);
1833 for (i
=0; i
<max_dev
;i
++)
1834 sb
->dev_roles
[i
] = cpu_to_le16(0xfffe);
1836 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
) {
1838 if (test_bit(Faulty
, &rdev2
->flags
))
1839 sb
->dev_roles
[i
] = cpu_to_le16(0xfffe);
1840 else if (test_bit(In_sync
, &rdev2
->flags
))
1841 sb
->dev_roles
[i
] = cpu_to_le16(rdev2
->raid_disk
);
1842 else if (rdev2
->raid_disk
>= 0)
1843 sb
->dev_roles
[i
] = cpu_to_le16(rdev2
->raid_disk
);
1845 sb
->dev_roles
[i
] = cpu_to_le16(0xffff);
1848 sb
->sb_csum
= calc_sb_1_csum(sb
);
1851 static unsigned long long
1852 super_1_rdev_size_change(struct md_rdev
*rdev
, sector_t num_sectors
)
1854 struct mdp_superblock_1
*sb
;
1855 sector_t max_sectors
;
1856 if (num_sectors
&& num_sectors
< rdev
->mddev
->dev_sectors
)
1857 return 0; /* component must fit device */
1858 if (rdev
->sb_start
< rdev
->data_offset
) {
1859 /* minor versions 1 and 2; superblock before data */
1860 max_sectors
= i_size_read(rdev
->bdev
->bd_inode
) >> 9;
1861 max_sectors
-= rdev
->data_offset
;
1862 if (!num_sectors
|| num_sectors
> max_sectors
)
1863 num_sectors
= max_sectors
;
1864 } else if (rdev
->mddev
->bitmap_info
.offset
) {
1865 /* minor version 0 with bitmap we can't move */
1868 /* minor version 0; superblock after data */
1870 sb_start
= (i_size_read(rdev
->bdev
->bd_inode
) >> 9) - 8*2;
1871 sb_start
&= ~(sector_t
)(4*2 - 1);
1872 max_sectors
= rdev
->sectors
+ sb_start
- rdev
->sb_start
;
1873 if (!num_sectors
|| num_sectors
> max_sectors
)
1874 num_sectors
= max_sectors
;
1875 rdev
->sb_start
= sb_start
;
1877 sb
= page_address(rdev
->sb_page
);
1878 sb
->data_size
= cpu_to_le64(num_sectors
);
1879 sb
->super_offset
= rdev
->sb_start
;
1880 sb
->sb_csum
= calc_sb_1_csum(sb
);
1881 md_super_write(rdev
->mddev
, rdev
, rdev
->sb_start
, rdev
->sb_size
,
1883 md_super_wait(rdev
->mddev
);
1887 static struct super_type super_types
[] = {
1890 .owner
= THIS_MODULE
,
1891 .load_super
= super_90_load
,
1892 .validate_super
= super_90_validate
,
1893 .sync_super
= super_90_sync
,
1894 .rdev_size_change
= super_90_rdev_size_change
,
1898 .owner
= THIS_MODULE
,
1899 .load_super
= super_1_load
,
1900 .validate_super
= super_1_validate
,
1901 .sync_super
= super_1_sync
,
1902 .rdev_size_change
= super_1_rdev_size_change
,
1906 static void sync_super(struct mddev
*mddev
, struct md_rdev
*rdev
)
1908 if (mddev
->sync_super
) {
1909 mddev
->sync_super(mddev
, rdev
);
1913 BUG_ON(mddev
->major_version
>= ARRAY_SIZE(super_types
));
1915 super_types
[mddev
->major_version
].sync_super(mddev
, rdev
);
1918 static int match_mddev_units(struct mddev
*mddev1
, struct mddev
*mddev2
)
1920 struct md_rdev
*rdev
, *rdev2
;
1923 rdev_for_each_rcu(rdev
, mddev1
)
1924 rdev_for_each_rcu(rdev2
, mddev2
)
1925 if (rdev
->bdev
->bd_contains
==
1926 rdev2
->bdev
->bd_contains
) {
1934 static LIST_HEAD(pending_raid_disks
);
1937 * Try to register data integrity profile for an mddev
1939 * This is called when an array is started and after a disk has been kicked
1940 * from the array. It only succeeds if all working and active component devices
1941 * are integrity capable with matching profiles.
1943 int md_integrity_register(struct mddev
*mddev
)
1945 struct md_rdev
*rdev
, *reference
= NULL
;
1947 if (list_empty(&mddev
->disks
))
1948 return 0; /* nothing to do */
1949 if (!mddev
->gendisk
|| blk_get_integrity(mddev
->gendisk
))
1950 return 0; /* shouldn't register, or already is */
1951 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
1952 /* skip spares and non-functional disks */
1953 if (test_bit(Faulty
, &rdev
->flags
))
1955 if (rdev
->raid_disk
< 0)
1958 /* Use the first rdev as the reference */
1962 /* does this rdev's profile match the reference profile? */
1963 if (blk_integrity_compare(reference
->bdev
->bd_disk
,
1964 rdev
->bdev
->bd_disk
) < 0)
1967 if (!reference
|| !bdev_get_integrity(reference
->bdev
))
1970 * All component devices are integrity capable and have matching
1971 * profiles, register the common profile for the md device.
1973 if (blk_integrity_register(mddev
->gendisk
,
1974 bdev_get_integrity(reference
->bdev
)) != 0) {
1975 printk(KERN_ERR
"md: failed to register integrity for %s\n",
1979 printk(KERN_NOTICE
"md: data integrity enabled on %s\n", mdname(mddev
));
1980 if (bioset_integrity_create(mddev
->bio_set
, BIO_POOL_SIZE
)) {
1981 printk(KERN_ERR
"md: failed to create integrity pool for %s\n",
1987 EXPORT_SYMBOL(md_integrity_register
);
1989 /* Disable data integrity if non-capable/non-matching disk is being added */
1990 void md_integrity_add_rdev(struct md_rdev
*rdev
, struct mddev
*mddev
)
1992 struct blk_integrity
*bi_rdev
= bdev_get_integrity(rdev
->bdev
);
1993 struct blk_integrity
*bi_mddev
= blk_get_integrity(mddev
->gendisk
);
1995 if (!bi_mddev
) /* nothing to do */
1997 if (rdev
->raid_disk
< 0) /* skip spares */
1999 if (bi_rdev
&& blk_integrity_compare(mddev
->gendisk
,
2000 rdev
->bdev
->bd_disk
) >= 0)
2002 printk(KERN_NOTICE
"disabling data integrity on %s\n", mdname(mddev
));
2003 blk_integrity_unregister(mddev
->gendisk
);
2005 EXPORT_SYMBOL(md_integrity_add_rdev
);
2007 static int bind_rdev_to_array(struct md_rdev
* rdev
, struct mddev
* mddev
)
2009 char b
[BDEVNAME_SIZE
];
2019 /* prevent duplicates */
2020 if (find_rdev(mddev
, rdev
->bdev
->bd_dev
))
2023 /* make sure rdev->sectors exceeds mddev->dev_sectors */
2024 if (rdev
->sectors
&& (mddev
->dev_sectors
== 0 ||
2025 rdev
->sectors
< mddev
->dev_sectors
)) {
2027 /* Cannot change size, so fail
2028 * If mddev->level <= 0, then we don't care
2029 * about aligning sizes (e.g. linear)
2031 if (mddev
->level
> 0)
2034 mddev
->dev_sectors
= rdev
->sectors
;
2037 /* Verify rdev->desc_nr is unique.
2038 * If it is -1, assign a free number, else
2039 * check number is not in use
2041 if (rdev
->desc_nr
< 0) {
2043 if (mddev
->pers
) choice
= mddev
->raid_disks
;
2044 while (find_rdev_nr(mddev
, choice
))
2046 rdev
->desc_nr
= choice
;
2048 if (find_rdev_nr(mddev
, rdev
->desc_nr
))
2051 if (mddev
->max_disks
&& rdev
->desc_nr
>= mddev
->max_disks
) {
2052 printk(KERN_WARNING
"md: %s: array is limited to %d devices\n",
2053 mdname(mddev
), mddev
->max_disks
);
2056 bdevname(rdev
->bdev
,b
);
2057 while ( (s
=strchr(b
, '/')) != NULL
)
2060 rdev
->mddev
= mddev
;
2061 printk(KERN_INFO
"md: bind<%s>\n", b
);
2063 if ((err
= kobject_add(&rdev
->kobj
, &mddev
->kobj
, "dev-%s", b
)))
2066 ko
= &part_to_dev(rdev
->bdev
->bd_part
)->kobj
;
2067 if (sysfs_create_link(&rdev
->kobj
, ko
, "block"))
2068 /* failure here is OK */;
2069 rdev
->sysfs_state
= sysfs_get_dirent_safe(rdev
->kobj
.sd
, "state");
2071 list_add_rcu(&rdev
->same_set
, &mddev
->disks
);
2072 bd_link_disk_holder(rdev
->bdev
, mddev
->gendisk
);
2074 /* May as well allow recovery to be retried once */
2075 mddev
->recovery_disabled
++;
2080 printk(KERN_WARNING
"md: failed to register dev-%s for %s\n",
2085 static void md_delayed_delete(struct work_struct
*ws
)
2087 struct md_rdev
*rdev
= container_of(ws
, struct md_rdev
, del_work
);
2088 kobject_del(&rdev
->kobj
);
2089 kobject_put(&rdev
->kobj
);
2092 static void unbind_rdev_from_array(struct md_rdev
* rdev
)
2094 char b
[BDEVNAME_SIZE
];
2099 bd_unlink_disk_holder(rdev
->bdev
, rdev
->mddev
->gendisk
);
2100 list_del_rcu(&rdev
->same_set
);
2101 printk(KERN_INFO
"md: unbind<%s>\n", bdevname(rdev
->bdev
,b
));
2103 sysfs_remove_link(&rdev
->kobj
, "block");
2104 sysfs_put(rdev
->sysfs_state
);
2105 rdev
->sysfs_state
= NULL
;
2106 kfree(rdev
->badblocks
.page
);
2107 rdev
->badblocks
.count
= 0;
2108 rdev
->badblocks
.page
= NULL
;
2109 /* We need to delay this, otherwise we can deadlock when
2110 * writing to 'remove' to "dev/state". We also need
2111 * to delay it due to rcu usage.
2114 INIT_WORK(&rdev
->del_work
, md_delayed_delete
);
2115 kobject_get(&rdev
->kobj
);
2116 queue_work(md_misc_wq
, &rdev
->del_work
);
2120 * prevent the device from being mounted, repartitioned or
2121 * otherwise reused by a RAID array (or any other kernel
2122 * subsystem), by bd_claiming the device.
2124 static int lock_rdev(struct md_rdev
*rdev
, dev_t dev
, int shared
)
2127 struct block_device
*bdev
;
2128 char b
[BDEVNAME_SIZE
];
2130 bdev
= blkdev_get_by_dev(dev
, FMODE_READ
|FMODE_WRITE
|FMODE_EXCL
,
2131 shared
? (struct md_rdev
*)lock_rdev
: rdev
);
2133 printk(KERN_ERR
"md: could not open %s.\n",
2134 __bdevname(dev
, b
));
2135 return PTR_ERR(bdev
);
2141 static void unlock_rdev(struct md_rdev
*rdev
)
2143 struct block_device
*bdev
= rdev
->bdev
;
2147 blkdev_put(bdev
, FMODE_READ
|FMODE_WRITE
|FMODE_EXCL
);
2150 void md_autodetect_dev(dev_t dev
);
2152 static void export_rdev(struct md_rdev
* rdev
)
2154 char b
[BDEVNAME_SIZE
];
2155 printk(KERN_INFO
"md: export_rdev(%s)\n",
2156 bdevname(rdev
->bdev
,b
));
2161 if (test_bit(AutoDetected
, &rdev
->flags
))
2162 md_autodetect_dev(rdev
->bdev
->bd_dev
);
2165 kobject_put(&rdev
->kobj
);
2168 static void kick_rdev_from_array(struct md_rdev
* rdev
)
2170 unbind_rdev_from_array(rdev
);
2174 static void export_array(struct mddev
*mddev
)
2176 struct md_rdev
*rdev
, *tmp
;
2178 rdev_for_each(rdev
, tmp
, mddev
) {
2183 kick_rdev_from_array(rdev
);
2185 if (!list_empty(&mddev
->disks
))
2187 mddev
->raid_disks
= 0;
2188 mddev
->major_version
= 0;
2191 static void print_desc(mdp_disk_t
*desc
)
2193 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc
->number
,
2194 desc
->major
,desc
->minor
,desc
->raid_disk
,desc
->state
);
2197 static void print_sb_90(mdp_super_t
*sb
)
2202 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
2203 sb
->major_version
, sb
->minor_version
, sb
->patch_version
,
2204 sb
->set_uuid0
, sb
->set_uuid1
, sb
->set_uuid2
, sb
->set_uuid3
,
2206 printk(KERN_INFO
"md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
2207 sb
->level
, sb
->size
, sb
->nr_disks
, sb
->raid_disks
,
2208 sb
->md_minor
, sb
->layout
, sb
->chunk_size
);
2209 printk(KERN_INFO
"md: UT:%08x ST:%d AD:%d WD:%d"
2210 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
2211 sb
->utime
, sb
->state
, sb
->active_disks
, sb
->working_disks
,
2212 sb
->failed_disks
, sb
->spare_disks
,
2213 sb
->sb_csum
, (unsigned long)sb
->events_lo
);
2216 for (i
= 0; i
< MD_SB_DISKS
; i
++) {
2219 desc
= sb
->disks
+ i
;
2220 if (desc
->number
|| desc
->major
|| desc
->minor
||
2221 desc
->raid_disk
|| (desc
->state
&& (desc
->state
!= 4))) {
2222 printk(" D %2d: ", i
);
2226 printk(KERN_INFO
"md: THIS: ");
2227 print_desc(&sb
->this_disk
);
2230 static void print_sb_1(struct mdp_superblock_1
*sb
)
2234 uuid
= sb
->set_uuid
;
2236 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n"
2237 "md: Name: \"%s\" CT:%llu\n",
2238 le32_to_cpu(sb
->major_version
),
2239 le32_to_cpu(sb
->feature_map
),
2242 (unsigned long long)le64_to_cpu(sb
->ctime
)
2243 & MD_SUPERBLOCK_1_TIME_SEC_MASK
);
2245 uuid
= sb
->device_uuid
;
2247 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
2249 "md: Dev:%08x UUID: %pU\n"
2250 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
2251 "md: (MaxDev:%u) \n",
2252 le32_to_cpu(sb
->level
),
2253 (unsigned long long)le64_to_cpu(sb
->size
),
2254 le32_to_cpu(sb
->raid_disks
),
2255 le32_to_cpu(sb
->layout
),
2256 le32_to_cpu(sb
->chunksize
),
2257 (unsigned long long)le64_to_cpu(sb
->data_offset
),
2258 (unsigned long long)le64_to_cpu(sb
->data_size
),
2259 (unsigned long long)le64_to_cpu(sb
->super_offset
),
2260 (unsigned long long)le64_to_cpu(sb
->recovery_offset
),
2261 le32_to_cpu(sb
->dev_number
),
2264 (unsigned long long)le64_to_cpu(sb
->utime
) & MD_SUPERBLOCK_1_TIME_SEC_MASK
,
2265 (unsigned long long)le64_to_cpu(sb
->events
),
2266 (unsigned long long)le64_to_cpu(sb
->resync_offset
),
2267 le32_to_cpu(sb
->sb_csum
),
2268 le32_to_cpu(sb
->max_dev
)
2272 static void print_rdev(struct md_rdev
*rdev
, int major_version
)
2274 char b
[BDEVNAME_SIZE
];
2275 printk(KERN_INFO
"md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
2276 bdevname(rdev
->bdev
, b
), (unsigned long long)rdev
->sectors
,
2277 test_bit(Faulty
, &rdev
->flags
), test_bit(In_sync
, &rdev
->flags
),
2279 if (rdev
->sb_loaded
) {
2280 printk(KERN_INFO
"md: rdev superblock (MJ:%d):\n", major_version
);
2281 switch (major_version
) {
2283 print_sb_90(page_address(rdev
->sb_page
));
2286 print_sb_1(page_address(rdev
->sb_page
));
2290 printk(KERN_INFO
"md: no rdev superblock!\n");
2293 static void md_print_devices(void)
2295 struct list_head
*tmp
;
2296 struct md_rdev
*rdev
;
2297 struct mddev
*mddev
;
2298 char b
[BDEVNAME_SIZE
];
2301 printk("md: **********************************\n");
2302 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
2303 printk("md: **********************************\n");
2304 for_each_mddev(mddev
, tmp
) {
2307 bitmap_print_sb(mddev
->bitmap
);
2309 printk("%s: ", mdname(mddev
));
2310 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
2311 printk("<%s>", bdevname(rdev
->bdev
,b
));
2314 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
2315 print_rdev(rdev
, mddev
->major_version
);
2317 printk("md: **********************************\n");
2322 static void sync_sbs(struct mddev
* mddev
, int nospares
)
2324 /* Update each superblock (in-memory image), but
2325 * if we are allowed to, skip spares which already
2326 * have the right event counter, or have one earlier
2327 * (which would mean they aren't being marked as dirty
2328 * with the rest of the array)
2330 struct md_rdev
*rdev
;
2331 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
2332 if (rdev
->sb_events
== mddev
->events
||
2334 rdev
->raid_disk
< 0 &&
2335 rdev
->sb_events
+1 == mddev
->events
)) {
2336 /* Don't update this superblock */
2337 rdev
->sb_loaded
= 2;
2339 sync_super(mddev
, rdev
);
2340 rdev
->sb_loaded
= 1;
2345 static void md_update_sb(struct mddev
* mddev
, int force_change
)
2347 struct md_rdev
*rdev
;
2350 int any_badblocks_changed
= 0;
2353 /* First make sure individual recovery_offsets are correct */
2354 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
2355 if (rdev
->raid_disk
>= 0 &&
2356 mddev
->delta_disks
>= 0 &&
2357 !test_bit(In_sync
, &rdev
->flags
) &&
2358 mddev
->curr_resync_completed
> rdev
->recovery_offset
)
2359 rdev
->recovery_offset
= mddev
->curr_resync_completed
;
2362 if (!mddev
->persistent
) {
2363 clear_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
2364 clear_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
2365 if (!mddev
->external
) {
2366 clear_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
2367 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
2368 if (rdev
->badblocks
.changed
) {
2369 rdev
->badblocks
.changed
= 0;
2370 md_ack_all_badblocks(&rdev
->badblocks
);
2371 md_error(mddev
, rdev
);
2373 clear_bit(Blocked
, &rdev
->flags
);
2374 clear_bit(BlockedBadBlocks
, &rdev
->flags
);
2375 wake_up(&rdev
->blocked_wait
);
2378 wake_up(&mddev
->sb_wait
);
2382 spin_lock_irq(&mddev
->write_lock
);
2384 mddev
->utime
= get_seconds();
2386 if (test_and_clear_bit(MD_CHANGE_DEVS
, &mddev
->flags
))
2388 if (test_and_clear_bit(MD_CHANGE_CLEAN
, &mddev
->flags
))
2389 /* just a clean<-> dirty transition, possibly leave spares alone,
2390 * though if events isn't the right even/odd, we will have to do
2396 if (mddev
->degraded
)
2397 /* If the array is degraded, then skipping spares is both
2398 * dangerous and fairly pointless.
2399 * Dangerous because a device that was removed from the array
2400 * might have a event_count that still looks up-to-date,
2401 * so it can be re-added without a resync.
2402 * Pointless because if there are any spares to skip,
2403 * then a recovery will happen and soon that array won't
2404 * be degraded any more and the spare can go back to sleep then.
2408 sync_req
= mddev
->in_sync
;
2410 /* If this is just a dirty<->clean transition, and the array is clean
2411 * and 'events' is odd, we can roll back to the previous clean state */
2413 && (mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
)
2414 && mddev
->can_decrease_events
2415 && mddev
->events
!= 1) {
2417 mddev
->can_decrease_events
= 0;
2419 /* otherwise we have to go forward and ... */
2421 mddev
->can_decrease_events
= nospares
;
2424 if (!mddev
->events
) {
2426 * oops, this 64-bit counter should never wrap.
2427 * Either we are in around ~1 trillion A.C., assuming
2428 * 1 reboot per second, or we have a bug:
2434 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
2435 if (rdev
->badblocks
.changed
)
2436 any_badblocks_changed
++;
2437 if (test_bit(Faulty
, &rdev
->flags
))
2438 set_bit(FaultRecorded
, &rdev
->flags
);
2441 sync_sbs(mddev
, nospares
);
2442 spin_unlock_irq(&mddev
->write_lock
);
2444 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2445 mdname(mddev
), mddev
->in_sync
);
2447 bitmap_update_sb(mddev
->bitmap
);
2448 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
2449 char b
[BDEVNAME_SIZE
];
2451 if (rdev
->sb_loaded
!= 1)
2452 continue; /* no noise on spare devices */
2454 if (!test_bit(Faulty
, &rdev
->flags
) &&
2455 rdev
->saved_raid_disk
== -1) {
2456 md_super_write(mddev
,rdev
,
2457 rdev
->sb_start
, rdev
->sb_size
,
2459 pr_debug("md: (write) %s's sb offset: %llu\n",
2460 bdevname(rdev
->bdev
, b
),
2461 (unsigned long long)rdev
->sb_start
);
2462 rdev
->sb_events
= mddev
->events
;
2463 if (rdev
->badblocks
.size
) {
2464 md_super_write(mddev
, rdev
,
2465 rdev
->badblocks
.sector
,
2466 rdev
->badblocks
.size
<< 9,
2468 rdev
->badblocks
.size
= 0;
2471 } else if (test_bit(Faulty
, &rdev
->flags
))
2472 pr_debug("md: %s (skipping faulty)\n",
2473 bdevname(rdev
->bdev
, b
));
2475 pr_debug("(skipping incremental s/r ");
2477 if (mddev
->level
== LEVEL_MULTIPATH
)
2478 /* only need to write one superblock... */
2481 md_super_wait(mddev
);
2482 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
2484 spin_lock_irq(&mddev
->write_lock
);
2485 if (mddev
->in_sync
!= sync_req
||
2486 test_bit(MD_CHANGE_DEVS
, &mddev
->flags
)) {
2487 /* have to write it out again */
2488 spin_unlock_irq(&mddev
->write_lock
);
2491 clear_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
2492 spin_unlock_irq(&mddev
->write_lock
);
2493 wake_up(&mddev
->sb_wait
);
2494 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
2495 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
2497 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
2498 if (test_and_clear_bit(FaultRecorded
, &rdev
->flags
))
2499 clear_bit(Blocked
, &rdev
->flags
);
2501 if (any_badblocks_changed
)
2502 md_ack_all_badblocks(&rdev
->badblocks
);
2503 clear_bit(BlockedBadBlocks
, &rdev
->flags
);
2504 wake_up(&rdev
->blocked_wait
);
2508 /* words written to sysfs files may, or may not, be \n terminated.
2509 * We want to accept with case. For this we use cmd_match.
2511 static int cmd_match(const char *cmd
, const char *str
)
2513 /* See if cmd, written into a sysfs file, matches
2514 * str. They must either be the same, or cmd can
2515 * have a trailing newline
2517 while (*cmd
&& *str
&& *cmd
== *str
) {
2528 struct rdev_sysfs_entry
{
2529 struct attribute attr
;
2530 ssize_t (*show
)(struct md_rdev
*, char *);
2531 ssize_t (*store
)(struct md_rdev
*, const char *, size_t);
2535 state_show(struct md_rdev
*rdev
, char *page
)
2540 if (test_bit(Faulty
, &rdev
->flags
) ||
2541 rdev
->badblocks
.unacked_exist
) {
2542 len
+= sprintf(page
+len
, "%sfaulty",sep
);
2545 if (test_bit(In_sync
, &rdev
->flags
)) {
2546 len
+= sprintf(page
+len
, "%sin_sync",sep
);
2549 if (test_bit(WriteMostly
, &rdev
->flags
)) {
2550 len
+= sprintf(page
+len
, "%swrite_mostly",sep
);
2553 if (test_bit(Blocked
, &rdev
->flags
) ||
2554 (rdev
->badblocks
.unacked_exist
2555 && !test_bit(Faulty
, &rdev
->flags
))) {
2556 len
+= sprintf(page
+len
, "%sblocked", sep
);
2559 if (!test_bit(Faulty
, &rdev
->flags
) &&
2560 !test_bit(In_sync
, &rdev
->flags
)) {
2561 len
+= sprintf(page
+len
, "%sspare", sep
);
2564 if (test_bit(WriteErrorSeen
, &rdev
->flags
)) {
2565 len
+= sprintf(page
+len
, "%swrite_error", sep
);
2568 if (test_bit(WantReplacement
, &rdev
->flags
)) {
2569 len
+= sprintf(page
+len
, "%swant_replacement", sep
);
2572 if (test_bit(Replacement
, &rdev
->flags
)) {
2573 len
+= sprintf(page
+len
, "%sreplacement", sep
);
2577 return len
+sprintf(page
+len
, "\n");
2581 state_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
2584 * faulty - simulates an error
2585 * remove - disconnects the device
2586 * writemostly - sets write_mostly
2587 * -writemostly - clears write_mostly
2588 * blocked - sets the Blocked flags
2589 * -blocked - clears the Blocked and possibly simulates an error
2590 * insync - sets Insync providing device isn't active
2591 * write_error - sets WriteErrorSeen
2592 * -write_error - clears WriteErrorSeen
2595 if (cmd_match(buf
, "faulty") && rdev
->mddev
->pers
) {
2596 md_error(rdev
->mddev
, rdev
);
2597 if (test_bit(Faulty
, &rdev
->flags
))
2601 } else if (cmd_match(buf
, "remove")) {
2602 if (rdev
->raid_disk
>= 0)
2605 struct mddev
*mddev
= rdev
->mddev
;
2606 kick_rdev_from_array(rdev
);
2608 md_update_sb(mddev
, 1);
2609 md_new_event(mddev
);
2612 } else if (cmd_match(buf
, "writemostly")) {
2613 set_bit(WriteMostly
, &rdev
->flags
);
2615 } else if (cmd_match(buf
, "-writemostly")) {
2616 clear_bit(WriteMostly
, &rdev
->flags
);
2618 } else if (cmd_match(buf
, "blocked")) {
2619 set_bit(Blocked
, &rdev
->flags
);
2621 } else if (cmd_match(buf
, "-blocked")) {
2622 if (!test_bit(Faulty
, &rdev
->flags
) &&
2623 rdev
->badblocks
.unacked_exist
) {
2624 /* metadata handler doesn't understand badblocks,
2625 * so we need to fail the device
2627 md_error(rdev
->mddev
, rdev
);
2629 clear_bit(Blocked
, &rdev
->flags
);
2630 clear_bit(BlockedBadBlocks
, &rdev
->flags
);
2631 wake_up(&rdev
->blocked_wait
);
2632 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
2633 md_wakeup_thread(rdev
->mddev
->thread
);
2636 } else if (cmd_match(buf
, "insync") && rdev
->raid_disk
== -1) {
2637 set_bit(In_sync
, &rdev
->flags
);
2639 } else if (cmd_match(buf
, "write_error")) {
2640 set_bit(WriteErrorSeen
, &rdev
->flags
);
2642 } else if (cmd_match(buf
, "-write_error")) {
2643 clear_bit(WriteErrorSeen
, &rdev
->flags
);
2645 } else if (cmd_match(buf
, "want_replacement")) {
2646 /* Any non-spare device that is not a replacement can
2647 * become want_replacement at any time, but we then need to
2648 * check if recovery is needed.
2650 if (rdev
->raid_disk
>= 0 &&
2651 !test_bit(Replacement
, &rdev
->flags
))
2652 set_bit(WantReplacement
, &rdev
->flags
);
2653 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
2654 md_wakeup_thread(rdev
->mddev
->thread
);
2656 } else if (cmd_match(buf
, "-want_replacement")) {
2657 /* Clearing 'want_replacement' is always allowed.
2658 * Once replacements starts it is too late though.
2661 clear_bit(WantReplacement
, &rdev
->flags
);
2662 } else if (cmd_match(buf
, "replacement")) {
2663 /* Can only set a device as a replacement when array has not
2664 * yet been started. Once running, replacement is automatic
2665 * from spares, or by assigning 'slot'.
2667 if (rdev
->mddev
->pers
)
2670 set_bit(Replacement
, &rdev
->flags
);
2673 } else if (cmd_match(buf
, "-replacement")) {
2674 /* Similarly, can only clear Replacement before start */
2675 if (rdev
->mddev
->pers
)
2678 clear_bit(Replacement
, &rdev
->flags
);
2683 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
2684 return err
? err
: len
;
2686 static struct rdev_sysfs_entry rdev_state
=
2687 __ATTR(state
, S_IRUGO
|S_IWUSR
, state_show
, state_store
);
2690 errors_show(struct md_rdev
*rdev
, char *page
)
2692 return sprintf(page
, "%d\n", atomic_read(&rdev
->corrected_errors
));
2696 errors_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
2699 unsigned long n
= simple_strtoul(buf
, &e
, 10);
2700 if (*buf
&& (*e
== 0 || *e
== '\n')) {
2701 atomic_set(&rdev
->corrected_errors
, n
);
2706 static struct rdev_sysfs_entry rdev_errors
=
2707 __ATTR(errors
, S_IRUGO
|S_IWUSR
, errors_show
, errors_store
);
2710 slot_show(struct md_rdev
*rdev
, char *page
)
2712 if (rdev
->raid_disk
< 0)
2713 return sprintf(page
, "none\n");
2715 return sprintf(page
, "%d\n", rdev
->raid_disk
);
2719 slot_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
2723 int slot
= simple_strtoul(buf
, &e
, 10);
2724 if (strncmp(buf
, "none", 4)==0)
2726 else if (e
==buf
|| (*e
&& *e
!= '\n'))
2728 if (rdev
->mddev
->pers
&& slot
== -1) {
2729 /* Setting 'slot' on an active array requires also
2730 * updating the 'rd%d' link, and communicating
2731 * with the personality with ->hot_*_disk.
2732 * For now we only support removing
2733 * failed/spare devices. This normally happens automatically,
2734 * but not when the metadata is externally managed.
2736 if (rdev
->raid_disk
== -1)
2738 /* personality does all needed checks */
2739 if (rdev
->mddev
->pers
->hot_remove_disk
== NULL
)
2741 err
= rdev
->mddev
->pers
->
2742 hot_remove_disk(rdev
->mddev
, rdev
);
2745 sysfs_unlink_rdev(rdev
->mddev
, rdev
);
2746 rdev
->raid_disk
= -1;
2747 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
2748 md_wakeup_thread(rdev
->mddev
->thread
);
2749 } else if (rdev
->mddev
->pers
) {
2750 /* Activating a spare .. or possibly reactivating
2751 * if we ever get bitmaps working here.
2754 if (rdev
->raid_disk
!= -1)
2757 if (test_bit(MD_RECOVERY_RUNNING
, &rdev
->mddev
->recovery
))
2760 if (rdev
->mddev
->pers
->hot_add_disk
== NULL
)
2763 if (slot
>= rdev
->mddev
->raid_disks
&&
2764 slot
>= rdev
->mddev
->raid_disks
+ rdev
->mddev
->delta_disks
)
2767 rdev
->raid_disk
= slot
;
2768 if (test_bit(In_sync
, &rdev
->flags
))
2769 rdev
->saved_raid_disk
= slot
;
2771 rdev
->saved_raid_disk
= -1;
2772 clear_bit(In_sync
, &rdev
->flags
);
2773 err
= rdev
->mddev
->pers
->
2774 hot_add_disk(rdev
->mddev
, rdev
);
2776 rdev
->raid_disk
= -1;
2779 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
2780 if (sysfs_link_rdev(rdev
->mddev
, rdev
))
2781 /* failure here is OK */;
2782 /* don't wakeup anyone, leave that to userspace. */
2784 if (slot
>= rdev
->mddev
->raid_disks
&&
2785 slot
>= rdev
->mddev
->raid_disks
+ rdev
->mddev
->delta_disks
)
2787 rdev
->raid_disk
= slot
;
2788 /* assume it is working */
2789 clear_bit(Faulty
, &rdev
->flags
);
2790 clear_bit(WriteMostly
, &rdev
->flags
);
2791 set_bit(In_sync
, &rdev
->flags
);
2792 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
2798 static struct rdev_sysfs_entry rdev_slot
=
2799 __ATTR(slot
, S_IRUGO
|S_IWUSR
, slot_show
, slot_store
);
2802 offset_show(struct md_rdev
*rdev
, char *page
)
2804 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->data_offset
);
2808 offset_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
2811 unsigned long long offset
= simple_strtoull(buf
, &e
, 10);
2812 if (e
==buf
|| (*e
&& *e
!= '\n'))
2814 if (rdev
->mddev
->pers
&& rdev
->raid_disk
>= 0)
2816 if (rdev
->sectors
&& rdev
->mddev
->external
)
2817 /* Must set offset before size, so overlap checks
2820 rdev
->data_offset
= offset
;
2824 static struct rdev_sysfs_entry rdev_offset
=
2825 __ATTR(offset
, S_IRUGO
|S_IWUSR
, offset_show
, offset_store
);
2828 rdev_size_show(struct md_rdev
*rdev
, char *page
)
2830 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->sectors
/ 2);
2833 static int overlaps(sector_t s1
, sector_t l1
, sector_t s2
, sector_t l2
)
2835 /* check if two start/length pairs overlap */
2843 static int strict_blocks_to_sectors(const char *buf
, sector_t
*sectors
)
2845 unsigned long long blocks
;
2848 if (strict_strtoull(buf
, 10, &blocks
) < 0)
2851 if (blocks
& 1ULL << (8 * sizeof(blocks
) - 1))
2852 return -EINVAL
; /* sector conversion overflow */
2855 if (new != blocks
* 2)
2856 return -EINVAL
; /* unsigned long long to sector_t overflow */
2863 rdev_size_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
2865 struct mddev
*my_mddev
= rdev
->mddev
;
2866 sector_t oldsectors
= rdev
->sectors
;
2869 if (strict_blocks_to_sectors(buf
, §ors
) < 0)
2871 if (my_mddev
->pers
&& rdev
->raid_disk
>= 0) {
2872 if (my_mddev
->persistent
) {
2873 sectors
= super_types
[my_mddev
->major_version
].
2874 rdev_size_change(rdev
, sectors
);
2877 } else if (!sectors
)
2878 sectors
= (i_size_read(rdev
->bdev
->bd_inode
) >> 9) -
2881 if (sectors
< my_mddev
->dev_sectors
)
2882 return -EINVAL
; /* component must fit device */
2884 rdev
->sectors
= sectors
;
2885 if (sectors
> oldsectors
&& my_mddev
->external
) {
2886 /* need to check that all other rdevs with the same ->bdev
2887 * do not overlap. We need to unlock the mddev to avoid
2888 * a deadlock. We have already changed rdev->sectors, and if
2889 * we have to change it back, we will have the lock again.
2891 struct mddev
*mddev
;
2893 struct list_head
*tmp
;
2895 mddev_unlock(my_mddev
);
2896 for_each_mddev(mddev
, tmp
) {
2897 struct md_rdev
*rdev2
;
2900 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
)
2901 if (rdev
->bdev
== rdev2
->bdev
&&
2903 overlaps(rdev
->data_offset
, rdev
->sectors
,
2909 mddev_unlock(mddev
);
2915 mddev_lock(my_mddev
);
2917 /* Someone else could have slipped in a size
2918 * change here, but doing so is just silly.
2919 * We put oldsectors back because we *know* it is
2920 * safe, and trust userspace not to race with
2923 rdev
->sectors
= oldsectors
;
2930 static struct rdev_sysfs_entry rdev_size
=
2931 __ATTR(size
, S_IRUGO
|S_IWUSR
, rdev_size_show
, rdev_size_store
);
2934 static ssize_t
recovery_start_show(struct md_rdev
*rdev
, char *page
)
2936 unsigned long long recovery_start
= rdev
->recovery_offset
;
2938 if (test_bit(In_sync
, &rdev
->flags
) ||
2939 recovery_start
== MaxSector
)
2940 return sprintf(page
, "none\n");
2942 return sprintf(page
, "%llu\n", recovery_start
);
2945 static ssize_t
recovery_start_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
2947 unsigned long long recovery_start
;
2949 if (cmd_match(buf
, "none"))
2950 recovery_start
= MaxSector
;
2951 else if (strict_strtoull(buf
, 10, &recovery_start
))
2954 if (rdev
->mddev
->pers
&&
2955 rdev
->raid_disk
>= 0)
2958 rdev
->recovery_offset
= recovery_start
;
2959 if (recovery_start
== MaxSector
)
2960 set_bit(In_sync
, &rdev
->flags
);
2962 clear_bit(In_sync
, &rdev
->flags
);
2966 static struct rdev_sysfs_entry rdev_recovery_start
=
2967 __ATTR(recovery_start
, S_IRUGO
|S_IWUSR
, recovery_start_show
, recovery_start_store
);
2971 badblocks_show(struct badblocks
*bb
, char *page
, int unack
);
2973 badblocks_store(struct badblocks
*bb
, const char *page
, size_t len
, int unack
);
2975 static ssize_t
bb_show(struct md_rdev
*rdev
, char *page
)
2977 return badblocks_show(&rdev
->badblocks
, page
, 0);
2979 static ssize_t
bb_store(struct md_rdev
*rdev
, const char *page
, size_t len
)
2981 int rv
= badblocks_store(&rdev
->badblocks
, page
, len
, 0);
2982 /* Maybe that ack was all we needed */
2983 if (test_and_clear_bit(BlockedBadBlocks
, &rdev
->flags
))
2984 wake_up(&rdev
->blocked_wait
);
2987 static struct rdev_sysfs_entry rdev_bad_blocks
=
2988 __ATTR(bad_blocks
, S_IRUGO
|S_IWUSR
, bb_show
, bb_store
);
2991 static ssize_t
ubb_show(struct md_rdev
*rdev
, char *page
)
2993 return badblocks_show(&rdev
->badblocks
, page
, 1);
2995 static ssize_t
ubb_store(struct md_rdev
*rdev
, const char *page
, size_t len
)
2997 return badblocks_store(&rdev
->badblocks
, page
, len
, 1);
2999 static struct rdev_sysfs_entry rdev_unack_bad_blocks
=
3000 __ATTR(unacknowledged_bad_blocks
, S_IRUGO
|S_IWUSR
, ubb_show
, ubb_store
);
3002 static struct attribute
*rdev_default_attrs
[] = {
3008 &rdev_recovery_start
.attr
,
3009 &rdev_bad_blocks
.attr
,
3010 &rdev_unack_bad_blocks
.attr
,
3014 rdev_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
3016 struct rdev_sysfs_entry
*entry
= container_of(attr
, struct rdev_sysfs_entry
, attr
);
3017 struct md_rdev
*rdev
= container_of(kobj
, struct md_rdev
, kobj
);
3018 struct mddev
*mddev
= rdev
->mddev
;
3024 rv
= mddev
? mddev_lock(mddev
) : -EBUSY
;
3026 if (rdev
->mddev
== NULL
)
3029 rv
= entry
->show(rdev
, page
);
3030 mddev_unlock(mddev
);
3036 rdev_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
3037 const char *page
, size_t length
)
3039 struct rdev_sysfs_entry
*entry
= container_of(attr
, struct rdev_sysfs_entry
, attr
);
3040 struct md_rdev
*rdev
= container_of(kobj
, struct md_rdev
, kobj
);
3042 struct mddev
*mddev
= rdev
->mddev
;
3046 if (!capable(CAP_SYS_ADMIN
))
3048 rv
= mddev
? mddev_lock(mddev
): -EBUSY
;
3050 if (rdev
->mddev
== NULL
)
3053 rv
= entry
->store(rdev
, page
, length
);
3054 mddev_unlock(mddev
);
3059 static void rdev_free(struct kobject
*ko
)
3061 struct md_rdev
*rdev
= container_of(ko
, struct md_rdev
, kobj
);
3064 static const struct sysfs_ops rdev_sysfs_ops
= {
3065 .show
= rdev_attr_show
,
3066 .store
= rdev_attr_store
,
3068 static struct kobj_type rdev_ktype
= {
3069 .release
= rdev_free
,
3070 .sysfs_ops
= &rdev_sysfs_ops
,
3071 .default_attrs
= rdev_default_attrs
,
3074 int md_rdev_init(struct md_rdev
*rdev
)
3077 rdev
->saved_raid_disk
= -1;
3078 rdev
->raid_disk
= -1;
3080 rdev
->data_offset
= 0;
3081 rdev
->sb_events
= 0;
3082 rdev
->last_read_error
.tv_sec
= 0;
3083 rdev
->last_read_error
.tv_nsec
= 0;
3084 rdev
->sb_loaded
= 0;
3085 rdev
->bb_page
= NULL
;
3086 atomic_set(&rdev
->nr_pending
, 0);
3087 atomic_set(&rdev
->read_errors
, 0);
3088 atomic_set(&rdev
->corrected_errors
, 0);
3090 INIT_LIST_HEAD(&rdev
->same_set
);
3091 init_waitqueue_head(&rdev
->blocked_wait
);
3093 /* Add space to store bad block list.
3094 * This reserves the space even on arrays where it cannot
3095 * be used - I wonder if that matters
3097 rdev
->badblocks
.count
= 0;
3098 rdev
->badblocks
.shift
= 0;
3099 rdev
->badblocks
.page
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
3100 seqlock_init(&rdev
->badblocks
.lock
);
3101 if (rdev
->badblocks
.page
== NULL
)
3106 EXPORT_SYMBOL_GPL(md_rdev_init
);
3108 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3110 * mark the device faulty if:
3112 * - the device is nonexistent (zero size)
3113 * - the device has no valid superblock
3115 * a faulty rdev _never_ has rdev->sb set.
3117 static struct md_rdev
*md_import_device(dev_t newdev
, int super_format
, int super_minor
)
3119 char b
[BDEVNAME_SIZE
];
3121 struct md_rdev
*rdev
;
3124 rdev
= kzalloc(sizeof(*rdev
), GFP_KERNEL
);
3126 printk(KERN_ERR
"md: could not alloc mem for new device!\n");
3127 return ERR_PTR(-ENOMEM
);
3130 err
= md_rdev_init(rdev
);
3133 err
= alloc_disk_sb(rdev
);
3137 err
= lock_rdev(rdev
, newdev
, super_format
== -2);
3141 kobject_init(&rdev
->kobj
, &rdev_ktype
);
3143 size
= i_size_read(rdev
->bdev
->bd_inode
) >> BLOCK_SIZE_BITS
;
3146 "md: %s has zero or unknown size, marking faulty!\n",
3147 bdevname(rdev
->bdev
,b
));
3152 if (super_format
>= 0) {
3153 err
= super_types
[super_format
].
3154 load_super(rdev
, NULL
, super_minor
);
3155 if (err
== -EINVAL
) {
3157 "md: %s does not have a valid v%d.%d "
3158 "superblock, not importing!\n",
3159 bdevname(rdev
->bdev
,b
),
3160 super_format
, super_minor
);
3165 "md: could not read %s's sb, not importing!\n",
3166 bdevname(rdev
->bdev
,b
));
3170 if (super_format
== -1)
3171 /* hot-add for 0.90, or non-persistent: so no badblocks */
3172 rdev
->badblocks
.shift
= -1;
3180 kfree(rdev
->badblocks
.page
);
3182 return ERR_PTR(err
);
3186 * Check a full RAID array for plausibility
3190 static void analyze_sbs(struct mddev
* mddev
)
3193 struct md_rdev
*rdev
, *freshest
, *tmp
;
3194 char b
[BDEVNAME_SIZE
];
3197 rdev_for_each(rdev
, tmp
, mddev
)
3198 switch (super_types
[mddev
->major_version
].
3199 load_super(rdev
, freshest
, mddev
->minor_version
)) {
3207 "md: fatal superblock inconsistency in %s"
3208 " -- removing from array\n",
3209 bdevname(rdev
->bdev
,b
));
3210 kick_rdev_from_array(rdev
);
3214 super_types
[mddev
->major_version
].
3215 validate_super(mddev
, freshest
);
3218 rdev_for_each(rdev
, tmp
, mddev
) {
3219 if (mddev
->max_disks
&&
3220 (rdev
->desc_nr
>= mddev
->max_disks
||
3221 i
> mddev
->max_disks
)) {
3223 "md: %s: %s: only %d devices permitted\n",
3224 mdname(mddev
), bdevname(rdev
->bdev
, b
),
3226 kick_rdev_from_array(rdev
);
3229 if (rdev
!= freshest
)
3230 if (super_types
[mddev
->major_version
].
3231 validate_super(mddev
, rdev
)) {
3232 printk(KERN_WARNING
"md: kicking non-fresh %s"
3234 bdevname(rdev
->bdev
,b
));
3235 kick_rdev_from_array(rdev
);
3238 if (mddev
->level
== LEVEL_MULTIPATH
) {
3239 rdev
->desc_nr
= i
++;
3240 rdev
->raid_disk
= rdev
->desc_nr
;
3241 set_bit(In_sync
, &rdev
->flags
);
3242 } else if (rdev
->raid_disk
>= (mddev
->raid_disks
- min(0, mddev
->delta_disks
))) {
3243 rdev
->raid_disk
= -1;
3244 clear_bit(In_sync
, &rdev
->flags
);
3249 /* Read a fixed-point number.
3250 * Numbers in sysfs attributes should be in "standard" units where
3251 * possible, so time should be in seconds.
3252 * However we internally use a a much smaller unit such as
3253 * milliseconds or jiffies.
3254 * This function takes a decimal number with a possible fractional
3255 * component, and produces an integer which is the result of
3256 * multiplying that number by 10^'scale'.
3257 * all without any floating-point arithmetic.
3259 int strict_strtoul_scaled(const char *cp
, unsigned long *res
, int scale
)
3261 unsigned long result
= 0;
3263 while (isdigit(*cp
) || (*cp
== '.' && decimals
< 0)) {
3266 else if (decimals
< scale
) {
3269 result
= result
* 10 + value
;
3281 while (decimals
< scale
) {
3290 static void md_safemode_timeout(unsigned long data
);
3293 safe_delay_show(struct mddev
*mddev
, char *page
)
3295 int msec
= (mddev
->safemode_delay
*1000)/HZ
;
3296 return sprintf(page
, "%d.%03d\n", msec
/1000, msec
%1000);
3299 safe_delay_store(struct mddev
*mddev
, const char *cbuf
, size_t len
)
3303 if (strict_strtoul_scaled(cbuf
, &msec
, 3) < 0)
3306 mddev
->safemode_delay
= 0;
3308 unsigned long old_delay
= mddev
->safemode_delay
;
3309 mddev
->safemode_delay
= (msec
*HZ
)/1000;
3310 if (mddev
->safemode_delay
== 0)
3311 mddev
->safemode_delay
= 1;
3312 if (mddev
->safemode_delay
< old_delay
)
3313 md_safemode_timeout((unsigned long)mddev
);
3317 static struct md_sysfs_entry md_safe_delay
=
3318 __ATTR(safe_mode_delay
, S_IRUGO
|S_IWUSR
,safe_delay_show
, safe_delay_store
);
3321 level_show(struct mddev
*mddev
, char *page
)
3323 struct md_personality
*p
= mddev
->pers
;
3325 return sprintf(page
, "%s\n", p
->name
);
3326 else if (mddev
->clevel
[0])
3327 return sprintf(page
, "%s\n", mddev
->clevel
);
3328 else if (mddev
->level
!= LEVEL_NONE
)
3329 return sprintf(page
, "%d\n", mddev
->level
);
3335 level_store(struct mddev
*mddev
, const char *buf
, size_t len
)
3339 struct md_personality
*pers
;
3342 struct md_rdev
*rdev
;
3344 if (mddev
->pers
== NULL
) {
3347 if (len
>= sizeof(mddev
->clevel
))
3349 strncpy(mddev
->clevel
, buf
, len
);
3350 if (mddev
->clevel
[len
-1] == '\n')
3352 mddev
->clevel
[len
] = 0;
3353 mddev
->level
= LEVEL_NONE
;
3357 /* request to change the personality. Need to ensure:
3358 * - array is not engaged in resync/recovery/reshape
3359 * - old personality can be suspended
3360 * - new personality will access other array.
3363 if (mddev
->sync_thread
||
3364 mddev
->reshape_position
!= MaxSector
||
3365 mddev
->sysfs_active
)
3368 if (!mddev
->pers
->quiesce
) {
3369 printk(KERN_WARNING
"md: %s: %s does not support online personality change\n",
3370 mdname(mddev
), mddev
->pers
->name
);
3374 /* Now find the new personality */
3375 if (len
== 0 || len
>= sizeof(clevel
))
3377 strncpy(clevel
, buf
, len
);
3378 if (clevel
[len
-1] == '\n')
3381 if (strict_strtol(clevel
, 10, &level
))
3384 if (request_module("md-%s", clevel
) != 0)
3385 request_module("md-level-%s", clevel
);
3386 spin_lock(&pers_lock
);
3387 pers
= find_pers(level
, clevel
);
3388 if (!pers
|| !try_module_get(pers
->owner
)) {
3389 spin_unlock(&pers_lock
);
3390 printk(KERN_WARNING
"md: personality %s not loaded\n", clevel
);
3393 spin_unlock(&pers_lock
);
3395 if (pers
== mddev
->pers
) {
3396 /* Nothing to do! */
3397 module_put(pers
->owner
);
3400 if (!pers
->takeover
) {
3401 module_put(pers
->owner
);
3402 printk(KERN_WARNING
"md: %s: %s does not support personality takeover\n",
3403 mdname(mddev
), clevel
);
3407 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
3408 rdev
->new_raid_disk
= rdev
->raid_disk
;
3410 /* ->takeover must set new_* and/or delta_disks
3411 * if it succeeds, and may set them when it fails.
3413 priv
= pers
->takeover(mddev
);
3415 mddev
->new_level
= mddev
->level
;
3416 mddev
->new_layout
= mddev
->layout
;
3417 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
3418 mddev
->raid_disks
-= mddev
->delta_disks
;
3419 mddev
->delta_disks
= 0;
3420 module_put(pers
->owner
);
3421 printk(KERN_WARNING
"md: %s: %s would not accept array\n",
3422 mdname(mddev
), clevel
);
3423 return PTR_ERR(priv
);
3426 /* Looks like we have a winner */
3427 mddev_suspend(mddev
);
3428 mddev
->pers
->stop(mddev
);
3430 if (mddev
->pers
->sync_request
== NULL
&&
3431 pers
->sync_request
!= NULL
) {
3432 /* need to add the md_redundancy_group */
3433 if (sysfs_create_group(&mddev
->kobj
, &md_redundancy_group
))
3435 "md: cannot register extra attributes for %s\n",
3437 mddev
->sysfs_action
= sysfs_get_dirent(mddev
->kobj
.sd
, NULL
, "sync_action");
3439 if (mddev
->pers
->sync_request
!= NULL
&&
3440 pers
->sync_request
== NULL
) {
3441 /* need to remove the md_redundancy_group */
3442 if (mddev
->to_remove
== NULL
)
3443 mddev
->to_remove
= &md_redundancy_group
;
3446 if (mddev
->pers
->sync_request
== NULL
&&
3448 /* We are converting from a no-redundancy array
3449 * to a redundancy array and metadata is managed
3450 * externally so we need to be sure that writes
3451 * won't block due to a need to transition
3453 * until external management is started.
3456 mddev
->safemode_delay
= 0;
3457 mddev
->safemode
= 0;
3460 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
3461 if (rdev
->raid_disk
< 0)
3463 if (rdev
->new_raid_disk
>= mddev
->raid_disks
)
3464 rdev
->new_raid_disk
= -1;
3465 if (rdev
->new_raid_disk
== rdev
->raid_disk
)
3467 sysfs_unlink_rdev(mddev
, rdev
);
3469 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
3470 if (rdev
->raid_disk
< 0)
3472 if (rdev
->new_raid_disk
== rdev
->raid_disk
)
3474 rdev
->raid_disk
= rdev
->new_raid_disk
;
3475 if (rdev
->raid_disk
< 0)
3476 clear_bit(In_sync
, &rdev
->flags
);
3478 if (sysfs_link_rdev(mddev
, rdev
))
3479 printk(KERN_WARNING
"md: cannot register rd%d"
3480 " for %s after level change\n",
3481 rdev
->raid_disk
, mdname(mddev
));
3485 module_put(mddev
->pers
->owner
);
3487 mddev
->private = priv
;
3488 strlcpy(mddev
->clevel
, pers
->name
, sizeof(mddev
->clevel
));
3489 mddev
->level
= mddev
->new_level
;
3490 mddev
->layout
= mddev
->new_layout
;
3491 mddev
->chunk_sectors
= mddev
->new_chunk_sectors
;
3492 mddev
->delta_disks
= 0;
3493 mddev
->degraded
= 0;
3494 if (mddev
->pers
->sync_request
== NULL
) {
3495 /* this is now an array without redundancy, so
3496 * it must always be in_sync
3499 del_timer_sync(&mddev
->safemode_timer
);
3502 mddev_resume(mddev
);
3503 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
3504 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3505 md_wakeup_thread(mddev
->thread
);
3506 sysfs_notify(&mddev
->kobj
, NULL
, "level");
3507 md_new_event(mddev
);
3511 static struct md_sysfs_entry md_level
=
3512 __ATTR(level
, S_IRUGO
|S_IWUSR
, level_show
, level_store
);
3516 layout_show(struct mddev
*mddev
, char *page
)
3518 /* just a number, not meaningful for all levels */
3519 if (mddev
->reshape_position
!= MaxSector
&&
3520 mddev
->layout
!= mddev
->new_layout
)
3521 return sprintf(page
, "%d (%d)\n",
3522 mddev
->new_layout
, mddev
->layout
);
3523 return sprintf(page
, "%d\n", mddev
->layout
);
3527 layout_store(struct mddev
*mddev
, const char *buf
, size_t len
)
3530 unsigned long n
= simple_strtoul(buf
, &e
, 10);
3532 if (!*buf
|| (*e
&& *e
!= '\n'))
3537 if (mddev
->pers
->check_reshape
== NULL
)
3539 mddev
->new_layout
= n
;
3540 err
= mddev
->pers
->check_reshape(mddev
);
3542 mddev
->new_layout
= mddev
->layout
;
3546 mddev
->new_layout
= n
;
3547 if (mddev
->reshape_position
== MaxSector
)
3552 static struct md_sysfs_entry md_layout
=
3553 __ATTR(layout
, S_IRUGO
|S_IWUSR
, layout_show
, layout_store
);
3557 raid_disks_show(struct mddev
*mddev
, char *page
)
3559 if (mddev
->raid_disks
== 0)
3561 if (mddev
->reshape_position
!= MaxSector
&&
3562 mddev
->delta_disks
!= 0)
3563 return sprintf(page
, "%d (%d)\n", mddev
->raid_disks
,
3564 mddev
->raid_disks
- mddev
->delta_disks
);
3565 return sprintf(page
, "%d\n", mddev
->raid_disks
);
3568 static int update_raid_disks(struct mddev
*mddev
, int raid_disks
);
3571 raid_disks_store(struct mddev
*mddev
, const char *buf
, size_t len
)
3575 unsigned long n
= simple_strtoul(buf
, &e
, 10);
3577 if (!*buf
|| (*e
&& *e
!= '\n'))
3581 rv
= update_raid_disks(mddev
, n
);
3582 else if (mddev
->reshape_position
!= MaxSector
) {
3583 int olddisks
= mddev
->raid_disks
- mddev
->delta_disks
;
3584 mddev
->delta_disks
= n
- olddisks
;
3585 mddev
->raid_disks
= n
;
3587 mddev
->raid_disks
= n
;
3588 return rv
? rv
: len
;
3590 static struct md_sysfs_entry md_raid_disks
=
3591 __ATTR(raid_disks
, S_IRUGO
|S_IWUSR
, raid_disks_show
, raid_disks_store
);
3594 chunk_size_show(struct mddev
*mddev
, char *page
)
3596 if (mddev
->reshape_position
!= MaxSector
&&
3597 mddev
->chunk_sectors
!= mddev
->new_chunk_sectors
)
3598 return sprintf(page
, "%d (%d)\n",
3599 mddev
->new_chunk_sectors
<< 9,
3600 mddev
->chunk_sectors
<< 9);
3601 return sprintf(page
, "%d\n", mddev
->chunk_sectors
<< 9);
3605 chunk_size_store(struct mddev
*mddev
, const char *buf
, size_t len
)
3608 unsigned long n
= simple_strtoul(buf
, &e
, 10);
3610 if (!*buf
|| (*e
&& *e
!= '\n'))
3615 if (mddev
->pers
->check_reshape
== NULL
)
3617 mddev
->new_chunk_sectors
= n
>> 9;
3618 err
= mddev
->pers
->check_reshape(mddev
);
3620 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
3624 mddev
->new_chunk_sectors
= n
>> 9;
3625 if (mddev
->reshape_position
== MaxSector
)
3626 mddev
->chunk_sectors
= n
>> 9;
3630 static struct md_sysfs_entry md_chunk_size
=
3631 __ATTR(chunk_size
, S_IRUGO
|S_IWUSR
, chunk_size_show
, chunk_size_store
);
3634 resync_start_show(struct mddev
*mddev
, char *page
)
3636 if (mddev
->recovery_cp
== MaxSector
)
3637 return sprintf(page
, "none\n");
3638 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->recovery_cp
);
3642 resync_start_store(struct mddev
*mddev
, const char *buf
, size_t len
)
3645 unsigned long long n
= simple_strtoull(buf
, &e
, 10);
3647 if (mddev
->pers
&& !test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
))
3649 if (cmd_match(buf
, "none"))
3651 else if (!*buf
|| (*e
&& *e
!= '\n'))
3654 mddev
->recovery_cp
= n
;
3657 static struct md_sysfs_entry md_resync_start
=
3658 __ATTR(resync_start
, S_IRUGO
|S_IWUSR
, resync_start_show
, resync_start_store
);
3661 * The array state can be:
3664 * No devices, no size, no level
3665 * Equivalent to STOP_ARRAY ioctl
3667 * May have some settings, but array is not active
3668 * all IO results in error
3669 * When written, doesn't tear down array, but just stops it
3670 * suspended (not supported yet)
3671 * All IO requests will block. The array can be reconfigured.
3672 * Writing this, if accepted, will block until array is quiescent
3674 * no resync can happen. no superblocks get written.
3675 * write requests fail
3677 * like readonly, but behaves like 'clean' on a write request.
3679 * clean - no pending writes, but otherwise active.
3680 * When written to inactive array, starts without resync
3681 * If a write request arrives then
3682 * if metadata is known, mark 'dirty' and switch to 'active'.
3683 * if not known, block and switch to write-pending
3684 * If written to an active array that has pending writes, then fails.
3686 * fully active: IO and resync can be happening.
3687 * When written to inactive array, starts with resync
3690 * clean, but writes are blocked waiting for 'active' to be written.
3693 * like active, but no writes have been seen for a while (100msec).
3696 enum array_state
{ clear
, inactive
, suspended
, readonly
, read_auto
, clean
, active
,
3697 write_pending
, active_idle
, bad_word
};
3698 static char *array_states
[] = {
3699 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3700 "write-pending", "active-idle", NULL
};
3702 static int match_word(const char *word
, char **list
)
3705 for (n
=0; list
[n
]; n
++)
3706 if (cmd_match(word
, list
[n
]))
3712 array_state_show(struct mddev
*mddev
, char *page
)
3714 enum array_state st
= inactive
;
3727 else if (test_bit(MD_CHANGE_PENDING
, &mddev
->flags
))
3729 else if (mddev
->safemode
)
3735 if (list_empty(&mddev
->disks
) &&
3736 mddev
->raid_disks
== 0 &&
3737 mddev
->dev_sectors
== 0)
3742 return sprintf(page
, "%s\n", array_states
[st
]);
3745 static int do_md_stop(struct mddev
* mddev
, int ro
, int is_open
);
3746 static int md_set_readonly(struct mddev
* mddev
, int is_open
);
3747 static int do_md_run(struct mddev
* mddev
);
3748 static int restart_array(struct mddev
*mddev
);
3751 array_state_store(struct mddev
*mddev
, const char *buf
, size_t len
)
3754 enum array_state st
= match_word(buf
, array_states
);
3759 /* stopping an active array */
3760 if (atomic_read(&mddev
->openers
) > 0)
3762 err
= do_md_stop(mddev
, 0, 0);
3765 /* stopping an active array */
3767 if (atomic_read(&mddev
->openers
) > 0)
3769 err
= do_md_stop(mddev
, 2, 0);
3771 err
= 0; /* already inactive */
3774 break; /* not supported yet */
3777 err
= md_set_readonly(mddev
, 0);
3780 set_disk_ro(mddev
->gendisk
, 1);
3781 err
= do_md_run(mddev
);
3787 err
= md_set_readonly(mddev
, 0);
3788 else if (mddev
->ro
== 1)
3789 err
= restart_array(mddev
);
3792 set_disk_ro(mddev
->gendisk
, 0);
3796 err
= do_md_run(mddev
);
3801 restart_array(mddev
);
3802 spin_lock_irq(&mddev
->write_lock
);
3803 if (atomic_read(&mddev
->writes_pending
) == 0) {
3804 if (mddev
->in_sync
== 0) {
3806 if (mddev
->safemode
== 1)
3807 mddev
->safemode
= 0;
3808 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
3813 spin_unlock_irq(&mddev
->write_lock
);
3819 restart_array(mddev
);
3820 clear_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
3821 wake_up(&mddev
->sb_wait
);
3825 set_disk_ro(mddev
->gendisk
, 0);
3826 err
= do_md_run(mddev
);
3831 /* these cannot be set */
3837 if (mddev
->hold_active
== UNTIL_IOCTL
)
3838 mddev
->hold_active
= 0;
3839 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
3843 static struct md_sysfs_entry md_array_state
=
3844 __ATTR(array_state
, S_IRUGO
|S_IWUSR
, array_state_show
, array_state_store
);
3847 max_corrected_read_errors_show(struct mddev
*mddev
, char *page
) {
3848 return sprintf(page
, "%d\n",
3849 atomic_read(&mddev
->max_corr_read_errors
));
3853 max_corrected_read_errors_store(struct mddev
*mddev
, const char *buf
, size_t len
)
3856 unsigned long n
= simple_strtoul(buf
, &e
, 10);
3858 if (*buf
&& (*e
== 0 || *e
== '\n')) {
3859 atomic_set(&mddev
->max_corr_read_errors
, n
);
3865 static struct md_sysfs_entry max_corr_read_errors
=
3866 __ATTR(max_read_errors
, S_IRUGO
|S_IWUSR
, max_corrected_read_errors_show
,
3867 max_corrected_read_errors_store
);
3870 null_show(struct mddev
*mddev
, char *page
)
3876 new_dev_store(struct mddev
*mddev
, const char *buf
, size_t len
)
3878 /* buf must be %d:%d\n? giving major and minor numbers */
3879 /* The new device is added to the array.
3880 * If the array has a persistent superblock, we read the
3881 * superblock to initialise info and check validity.
3882 * Otherwise, only checking done is that in bind_rdev_to_array,
3883 * which mainly checks size.
3886 int major
= simple_strtoul(buf
, &e
, 10);
3889 struct md_rdev
*rdev
;
3892 if (!*buf
|| *e
!= ':' || !e
[1] || e
[1] == '\n')
3894 minor
= simple_strtoul(e
+1, &e
, 10);
3895 if (*e
&& *e
!= '\n')
3897 dev
= MKDEV(major
, minor
);
3898 if (major
!= MAJOR(dev
) ||
3899 minor
!= MINOR(dev
))
3903 if (mddev
->persistent
) {
3904 rdev
= md_import_device(dev
, mddev
->major_version
,
3905 mddev
->minor_version
);
3906 if (!IS_ERR(rdev
) && !list_empty(&mddev
->disks
)) {
3907 struct md_rdev
*rdev0
3908 = list_entry(mddev
->disks
.next
,
3909 struct md_rdev
, same_set
);
3910 err
= super_types
[mddev
->major_version
]
3911 .load_super(rdev
, rdev0
, mddev
->minor_version
);
3915 } else if (mddev
->external
)
3916 rdev
= md_import_device(dev
, -2, -1);
3918 rdev
= md_import_device(dev
, -1, -1);
3921 return PTR_ERR(rdev
);
3922 err
= bind_rdev_to_array(rdev
, mddev
);
3926 return err
? err
: len
;
3929 static struct md_sysfs_entry md_new_device
=
3930 __ATTR(new_dev
, S_IWUSR
, null_show
, new_dev_store
);
3933 bitmap_store(struct mddev
*mddev
, const char *buf
, size_t len
)
3936 unsigned long chunk
, end_chunk
;
3940 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3942 chunk
= end_chunk
= simple_strtoul(buf
, &end
, 0);
3943 if (buf
== end
) break;
3944 if (*end
== '-') { /* range */
3946 end_chunk
= simple_strtoul(buf
, &end
, 0);
3947 if (buf
== end
) break;
3949 if (*end
&& !isspace(*end
)) break;
3950 bitmap_dirty_bits(mddev
->bitmap
, chunk
, end_chunk
);
3951 buf
= skip_spaces(end
);
3953 bitmap_unplug(mddev
->bitmap
); /* flush the bits to disk */
3958 static struct md_sysfs_entry md_bitmap
=
3959 __ATTR(bitmap_set_bits
, S_IWUSR
, null_show
, bitmap_store
);
3962 size_show(struct mddev
*mddev
, char *page
)
3964 return sprintf(page
, "%llu\n",
3965 (unsigned long long)mddev
->dev_sectors
/ 2);
3968 static int update_size(struct mddev
*mddev
, sector_t num_sectors
);
3971 size_store(struct mddev
*mddev
, const char *buf
, size_t len
)
3973 /* If array is inactive, we can reduce the component size, but
3974 * not increase it (except from 0).
3975 * If array is active, we can try an on-line resize
3978 int err
= strict_blocks_to_sectors(buf
, §ors
);
3983 err
= update_size(mddev
, sectors
);
3984 md_update_sb(mddev
, 1);
3986 if (mddev
->dev_sectors
== 0 ||
3987 mddev
->dev_sectors
> sectors
)
3988 mddev
->dev_sectors
= sectors
;
3992 return err
? err
: len
;
3995 static struct md_sysfs_entry md_size
=
3996 __ATTR(component_size
, S_IRUGO
|S_IWUSR
, size_show
, size_store
);
4001 * 'none' for arrays with no metadata (good luck...)
4002 * 'external' for arrays with externally managed metadata,
4003 * or N.M for internally known formats
4006 metadata_show(struct mddev
*mddev
, char *page
)
4008 if (mddev
->persistent
)
4009 return sprintf(page
, "%d.%d\n",
4010 mddev
->major_version
, mddev
->minor_version
);
4011 else if (mddev
->external
)
4012 return sprintf(page
, "external:%s\n", mddev
->metadata_type
);
4014 return sprintf(page
, "none\n");
4018 metadata_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4022 /* Changing the details of 'external' metadata is
4023 * always permitted. Otherwise there must be
4024 * no devices attached to the array.
4026 if (mddev
->external
&& strncmp(buf
, "external:", 9) == 0)
4028 else if (!list_empty(&mddev
->disks
))
4031 if (cmd_match(buf
, "none")) {
4032 mddev
->persistent
= 0;
4033 mddev
->external
= 0;
4034 mddev
->major_version
= 0;
4035 mddev
->minor_version
= 90;
4038 if (strncmp(buf
, "external:", 9) == 0) {
4039 size_t namelen
= len
-9;
4040 if (namelen
>= sizeof(mddev
->metadata_type
))
4041 namelen
= sizeof(mddev
->metadata_type
)-1;
4042 strncpy(mddev
->metadata_type
, buf
+9, namelen
);
4043 mddev
->metadata_type
[namelen
] = 0;
4044 if (namelen
&& mddev
->metadata_type
[namelen
-1] == '\n')
4045 mddev
->metadata_type
[--namelen
] = 0;
4046 mddev
->persistent
= 0;
4047 mddev
->external
= 1;
4048 mddev
->major_version
= 0;
4049 mddev
->minor_version
= 90;
4052 major
= simple_strtoul(buf
, &e
, 10);
4053 if (e
==buf
|| *e
!= '.')
4056 minor
= simple_strtoul(buf
, &e
, 10);
4057 if (e
==buf
|| (*e
&& *e
!= '\n') )
4059 if (major
>= ARRAY_SIZE(super_types
) || super_types
[major
].name
== NULL
)
4061 mddev
->major_version
= major
;
4062 mddev
->minor_version
= minor
;
4063 mddev
->persistent
= 1;
4064 mddev
->external
= 0;
4068 static struct md_sysfs_entry md_metadata
=
4069 __ATTR(metadata_version
, S_IRUGO
|S_IWUSR
, metadata_show
, metadata_store
);
4072 action_show(struct mddev
*mddev
, char *page
)
4074 char *type
= "idle";
4075 if (test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
))
4077 else if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
4078 (!mddev
->ro
&& test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))) {
4079 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
4081 else if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
4082 if (!test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
4084 else if (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
))
4088 } else if (test_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
))
4091 return sprintf(page
, "%s\n", type
);
4094 static void reap_sync_thread(struct mddev
*mddev
);
4097 action_store(struct mddev
*mddev
, const char *page
, size_t len
)
4099 if (!mddev
->pers
|| !mddev
->pers
->sync_request
)
4102 if (cmd_match(page
, "frozen"))
4103 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4105 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4107 if (cmd_match(page
, "idle") || cmd_match(page
, "frozen")) {
4108 if (mddev
->sync_thread
) {
4109 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
4110 reap_sync_thread(mddev
);
4112 } else if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
4113 test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))
4115 else if (cmd_match(page
, "resync"))
4116 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4117 else if (cmd_match(page
, "recover")) {
4118 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
4119 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4120 } else if (cmd_match(page
, "reshape")) {
4122 if (mddev
->pers
->start_reshape
== NULL
)
4124 err
= mddev
->pers
->start_reshape(mddev
);
4127 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
4129 if (cmd_match(page
, "check"))
4130 set_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
4131 else if (!cmd_match(page
, "repair"))
4133 set_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
4134 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
4136 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4137 md_wakeup_thread(mddev
->thread
);
4138 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
4143 mismatch_cnt_show(struct mddev
*mddev
, char *page
)
4145 return sprintf(page
, "%llu\n",
4146 (unsigned long long) mddev
->resync_mismatches
);
4149 static struct md_sysfs_entry md_scan_mode
=
4150 __ATTR(sync_action
, S_IRUGO
|S_IWUSR
, action_show
, action_store
);
4153 static struct md_sysfs_entry md_mismatches
= __ATTR_RO(mismatch_cnt
);
4156 sync_min_show(struct mddev
*mddev
, char *page
)
4158 return sprintf(page
, "%d (%s)\n", speed_min(mddev
),
4159 mddev
->sync_speed_min
? "local": "system");
4163 sync_min_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4167 if (strncmp(buf
, "system", 6)==0) {
4168 mddev
->sync_speed_min
= 0;
4171 min
= simple_strtoul(buf
, &e
, 10);
4172 if (buf
== e
|| (*e
&& *e
!= '\n') || min
<= 0)
4174 mddev
->sync_speed_min
= min
;
4178 static struct md_sysfs_entry md_sync_min
=
4179 __ATTR(sync_speed_min
, S_IRUGO
|S_IWUSR
, sync_min_show
, sync_min_store
);
4182 sync_max_show(struct mddev
*mddev
, char *page
)
4184 return sprintf(page
, "%d (%s)\n", speed_max(mddev
),
4185 mddev
->sync_speed_max
? "local": "system");
4189 sync_max_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4193 if (strncmp(buf
, "system", 6)==0) {
4194 mddev
->sync_speed_max
= 0;
4197 max
= simple_strtoul(buf
, &e
, 10);
4198 if (buf
== e
|| (*e
&& *e
!= '\n') || max
<= 0)
4200 mddev
->sync_speed_max
= max
;
4204 static struct md_sysfs_entry md_sync_max
=
4205 __ATTR(sync_speed_max
, S_IRUGO
|S_IWUSR
, sync_max_show
, sync_max_store
);
4208 degraded_show(struct mddev
*mddev
, char *page
)
4210 return sprintf(page
, "%d\n", mddev
->degraded
);
4212 static struct md_sysfs_entry md_degraded
= __ATTR_RO(degraded
);
4215 sync_force_parallel_show(struct mddev
*mddev
, char *page
)
4217 return sprintf(page
, "%d\n", mddev
->parallel_resync
);
4221 sync_force_parallel_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4225 if (strict_strtol(buf
, 10, &n
))
4228 if (n
!= 0 && n
!= 1)
4231 mddev
->parallel_resync
= n
;
4233 if (mddev
->sync_thread
)
4234 wake_up(&resync_wait
);
4239 /* force parallel resync, even with shared block devices */
4240 static struct md_sysfs_entry md_sync_force_parallel
=
4241 __ATTR(sync_force_parallel
, S_IRUGO
|S_IWUSR
,
4242 sync_force_parallel_show
, sync_force_parallel_store
);
4245 sync_speed_show(struct mddev
*mddev
, char *page
)
4247 unsigned long resync
, dt
, db
;
4248 if (mddev
->curr_resync
== 0)
4249 return sprintf(page
, "none\n");
4250 resync
= mddev
->curr_mark_cnt
- atomic_read(&mddev
->recovery_active
);
4251 dt
= (jiffies
- mddev
->resync_mark
) / HZ
;
4253 db
= resync
- mddev
->resync_mark_cnt
;
4254 return sprintf(page
, "%lu\n", db
/dt
/2); /* K/sec */
4257 static struct md_sysfs_entry md_sync_speed
= __ATTR_RO(sync_speed
);
4260 sync_completed_show(struct mddev
*mddev
, char *page
)
4262 unsigned long long max_sectors
, resync
;
4264 if (!test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
4265 return sprintf(page
, "none\n");
4267 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
4268 max_sectors
= mddev
->resync_max_sectors
;
4270 max_sectors
= mddev
->dev_sectors
;
4272 resync
= mddev
->curr_resync_completed
;
4273 return sprintf(page
, "%llu / %llu\n", resync
, max_sectors
);
4276 static struct md_sysfs_entry md_sync_completed
= __ATTR_RO(sync_completed
);
4279 min_sync_show(struct mddev
*mddev
, char *page
)
4281 return sprintf(page
, "%llu\n",
4282 (unsigned long long)mddev
->resync_min
);
4285 min_sync_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4287 unsigned long long min
;
4288 if (strict_strtoull(buf
, 10, &min
))
4290 if (min
> mddev
->resync_max
)
4292 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
4295 /* Must be a multiple of chunk_size */
4296 if (mddev
->chunk_sectors
) {
4297 sector_t temp
= min
;
4298 if (sector_div(temp
, mddev
->chunk_sectors
))
4301 mddev
->resync_min
= min
;
4306 static struct md_sysfs_entry md_min_sync
=
4307 __ATTR(sync_min
, S_IRUGO
|S_IWUSR
, min_sync_show
, min_sync_store
);
4310 max_sync_show(struct mddev
*mddev
, char *page
)
4312 if (mddev
->resync_max
== MaxSector
)
4313 return sprintf(page
, "max\n");
4315 return sprintf(page
, "%llu\n",
4316 (unsigned long long)mddev
->resync_max
);
4319 max_sync_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4321 if (strncmp(buf
, "max", 3) == 0)
4322 mddev
->resync_max
= MaxSector
;
4324 unsigned long long max
;
4325 if (strict_strtoull(buf
, 10, &max
))
4327 if (max
< mddev
->resync_min
)
4329 if (max
< mddev
->resync_max
&&
4331 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
4334 /* Must be a multiple of chunk_size */
4335 if (mddev
->chunk_sectors
) {
4336 sector_t temp
= max
;
4337 if (sector_div(temp
, mddev
->chunk_sectors
))
4340 mddev
->resync_max
= max
;
4342 wake_up(&mddev
->recovery_wait
);
4346 static struct md_sysfs_entry md_max_sync
=
4347 __ATTR(sync_max
, S_IRUGO
|S_IWUSR
, max_sync_show
, max_sync_store
);
4350 suspend_lo_show(struct mddev
*mddev
, char *page
)
4352 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->suspend_lo
);
4356 suspend_lo_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4359 unsigned long long new = simple_strtoull(buf
, &e
, 10);
4360 unsigned long long old
= mddev
->suspend_lo
;
4362 if (mddev
->pers
== NULL
||
4363 mddev
->pers
->quiesce
== NULL
)
4365 if (buf
== e
|| (*e
&& *e
!= '\n'))
4368 mddev
->suspend_lo
= new;
4370 /* Shrinking suspended region */
4371 mddev
->pers
->quiesce(mddev
, 2);
4373 /* Expanding suspended region - need to wait */
4374 mddev
->pers
->quiesce(mddev
, 1);
4375 mddev
->pers
->quiesce(mddev
, 0);
4379 static struct md_sysfs_entry md_suspend_lo
=
4380 __ATTR(suspend_lo
, S_IRUGO
|S_IWUSR
, suspend_lo_show
, suspend_lo_store
);
4384 suspend_hi_show(struct mddev
*mddev
, char *page
)
4386 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->suspend_hi
);
4390 suspend_hi_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4393 unsigned long long new = simple_strtoull(buf
, &e
, 10);
4394 unsigned long long old
= mddev
->suspend_hi
;
4396 if (mddev
->pers
== NULL
||
4397 mddev
->pers
->quiesce
== NULL
)
4399 if (buf
== e
|| (*e
&& *e
!= '\n'))
4402 mddev
->suspend_hi
= new;
4404 /* Shrinking suspended region */
4405 mddev
->pers
->quiesce(mddev
, 2);
4407 /* Expanding suspended region - need to wait */
4408 mddev
->pers
->quiesce(mddev
, 1);
4409 mddev
->pers
->quiesce(mddev
, 0);
4413 static struct md_sysfs_entry md_suspend_hi
=
4414 __ATTR(suspend_hi
, S_IRUGO
|S_IWUSR
, suspend_hi_show
, suspend_hi_store
);
4417 reshape_position_show(struct mddev
*mddev
, char *page
)
4419 if (mddev
->reshape_position
!= MaxSector
)
4420 return sprintf(page
, "%llu\n",
4421 (unsigned long long)mddev
->reshape_position
);
4422 strcpy(page
, "none\n");
4427 reshape_position_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4430 unsigned long long new = simple_strtoull(buf
, &e
, 10);
4433 if (buf
== e
|| (*e
&& *e
!= '\n'))
4435 mddev
->reshape_position
= new;
4436 mddev
->delta_disks
= 0;
4437 mddev
->new_level
= mddev
->level
;
4438 mddev
->new_layout
= mddev
->layout
;
4439 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
4443 static struct md_sysfs_entry md_reshape_position
=
4444 __ATTR(reshape_position
, S_IRUGO
|S_IWUSR
, reshape_position_show
,
4445 reshape_position_store
);
4448 array_size_show(struct mddev
*mddev
, char *page
)
4450 if (mddev
->external_size
)
4451 return sprintf(page
, "%llu\n",
4452 (unsigned long long)mddev
->array_sectors
/2);
4454 return sprintf(page
, "default\n");
4458 array_size_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4462 if (strncmp(buf
, "default", 7) == 0) {
4464 sectors
= mddev
->pers
->size(mddev
, 0, 0);
4466 sectors
= mddev
->array_sectors
;
4468 mddev
->external_size
= 0;
4470 if (strict_blocks_to_sectors(buf
, §ors
) < 0)
4472 if (mddev
->pers
&& mddev
->pers
->size(mddev
, 0, 0) < sectors
)
4475 mddev
->external_size
= 1;
4478 mddev
->array_sectors
= sectors
;
4480 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
4481 revalidate_disk(mddev
->gendisk
);
4486 static struct md_sysfs_entry md_array_size
=
4487 __ATTR(array_size
, S_IRUGO
|S_IWUSR
, array_size_show
,
4490 static struct attribute
*md_default_attrs
[] = {
4493 &md_raid_disks
.attr
,
4494 &md_chunk_size
.attr
,
4496 &md_resync_start
.attr
,
4498 &md_new_device
.attr
,
4499 &md_safe_delay
.attr
,
4500 &md_array_state
.attr
,
4501 &md_reshape_position
.attr
,
4502 &md_array_size
.attr
,
4503 &max_corr_read_errors
.attr
,
4507 static struct attribute
*md_redundancy_attrs
[] = {
4509 &md_mismatches
.attr
,
4512 &md_sync_speed
.attr
,
4513 &md_sync_force_parallel
.attr
,
4514 &md_sync_completed
.attr
,
4517 &md_suspend_lo
.attr
,
4518 &md_suspend_hi
.attr
,
4523 static struct attribute_group md_redundancy_group
= {
4525 .attrs
= md_redundancy_attrs
,
4530 md_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
4532 struct md_sysfs_entry
*entry
= container_of(attr
, struct md_sysfs_entry
, attr
);
4533 struct mddev
*mddev
= container_of(kobj
, struct mddev
, kobj
);
4538 spin_lock(&all_mddevs_lock
);
4539 if (list_empty(&mddev
->all_mddevs
)) {
4540 spin_unlock(&all_mddevs_lock
);
4544 spin_unlock(&all_mddevs_lock
);
4546 rv
= mddev_lock(mddev
);
4548 rv
= entry
->show(mddev
, page
);
4549 mddev_unlock(mddev
);
4556 md_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
4557 const char *page
, size_t length
)
4559 struct md_sysfs_entry
*entry
= container_of(attr
, struct md_sysfs_entry
, attr
);
4560 struct mddev
*mddev
= container_of(kobj
, struct mddev
, kobj
);
4565 if (!capable(CAP_SYS_ADMIN
))
4567 spin_lock(&all_mddevs_lock
);
4568 if (list_empty(&mddev
->all_mddevs
)) {
4569 spin_unlock(&all_mddevs_lock
);
4573 spin_unlock(&all_mddevs_lock
);
4574 rv
= mddev_lock(mddev
);
4576 rv
= entry
->store(mddev
, page
, length
);
4577 mddev_unlock(mddev
);
4583 static void md_free(struct kobject
*ko
)
4585 struct mddev
*mddev
= container_of(ko
, struct mddev
, kobj
);
4587 if (mddev
->sysfs_state
)
4588 sysfs_put(mddev
->sysfs_state
);
4590 if (mddev
->gendisk
) {
4591 del_gendisk(mddev
->gendisk
);
4592 put_disk(mddev
->gendisk
);
4595 blk_cleanup_queue(mddev
->queue
);
4600 static const struct sysfs_ops md_sysfs_ops
= {
4601 .show
= md_attr_show
,
4602 .store
= md_attr_store
,
4604 static struct kobj_type md_ktype
= {
4606 .sysfs_ops
= &md_sysfs_ops
,
4607 .default_attrs
= md_default_attrs
,
4612 static void mddev_delayed_delete(struct work_struct
*ws
)
4614 struct mddev
*mddev
= container_of(ws
, struct mddev
, del_work
);
4616 sysfs_remove_group(&mddev
->kobj
, &md_bitmap_group
);
4617 kobject_del(&mddev
->kobj
);
4618 kobject_put(&mddev
->kobj
);
4621 static int md_alloc(dev_t dev
, char *name
)
4623 static DEFINE_MUTEX(disks_mutex
);
4624 struct mddev
*mddev
= mddev_find(dev
);
4625 struct gendisk
*disk
;
4634 partitioned
= (MAJOR(mddev
->unit
) != MD_MAJOR
);
4635 shift
= partitioned
? MdpMinorShift
: 0;
4636 unit
= MINOR(mddev
->unit
) >> shift
;
4638 /* wait for any previous instance of this device to be
4639 * completely removed (mddev_delayed_delete).
4641 flush_workqueue(md_misc_wq
);
4643 mutex_lock(&disks_mutex
);
4649 /* Need to ensure that 'name' is not a duplicate.
4651 struct mddev
*mddev2
;
4652 spin_lock(&all_mddevs_lock
);
4654 list_for_each_entry(mddev2
, &all_mddevs
, all_mddevs
)
4655 if (mddev2
->gendisk
&&
4656 strcmp(mddev2
->gendisk
->disk_name
, name
) == 0) {
4657 spin_unlock(&all_mddevs_lock
);
4660 spin_unlock(&all_mddevs_lock
);
4664 mddev
->queue
= blk_alloc_queue(GFP_KERNEL
);
4667 mddev
->queue
->queuedata
= mddev
;
4669 blk_queue_make_request(mddev
->queue
, md_make_request
);
4670 blk_set_stacking_limits(&mddev
->queue
->limits
);
4672 disk
= alloc_disk(1 << shift
);
4674 blk_cleanup_queue(mddev
->queue
);
4675 mddev
->queue
= NULL
;
4678 disk
->major
= MAJOR(mddev
->unit
);
4679 disk
->first_minor
= unit
<< shift
;
4681 strcpy(disk
->disk_name
, name
);
4682 else if (partitioned
)
4683 sprintf(disk
->disk_name
, "md_d%d", unit
);
4685 sprintf(disk
->disk_name
, "md%d", unit
);
4686 disk
->fops
= &md_fops
;
4687 disk
->private_data
= mddev
;
4688 disk
->queue
= mddev
->queue
;
4689 blk_queue_flush(mddev
->queue
, REQ_FLUSH
| REQ_FUA
);
4690 /* Allow extended partitions. This makes the
4691 * 'mdp' device redundant, but we can't really
4694 disk
->flags
|= GENHD_FL_EXT_DEVT
;
4695 mddev
->gendisk
= disk
;
4696 /* As soon as we call add_disk(), another thread could get
4697 * through to md_open, so make sure it doesn't get too far
4699 mutex_lock(&mddev
->open_mutex
);
4702 error
= kobject_init_and_add(&mddev
->kobj
, &md_ktype
,
4703 &disk_to_dev(disk
)->kobj
, "%s", "md");
4705 /* This isn't possible, but as kobject_init_and_add is marked
4706 * __must_check, we must do something with the result
4708 printk(KERN_WARNING
"md: cannot register %s/md - name in use\n",
4712 if (mddev
->kobj
.sd
&&
4713 sysfs_create_group(&mddev
->kobj
, &md_bitmap_group
))
4714 printk(KERN_DEBUG
"pointless warning\n");
4715 mutex_unlock(&mddev
->open_mutex
);
4717 mutex_unlock(&disks_mutex
);
4718 if (!error
&& mddev
->kobj
.sd
) {
4719 kobject_uevent(&mddev
->kobj
, KOBJ_ADD
);
4720 mddev
->sysfs_state
= sysfs_get_dirent_safe(mddev
->kobj
.sd
, "array_state");
4726 static struct kobject
*md_probe(dev_t dev
, int *part
, void *data
)
4728 md_alloc(dev
, NULL
);
4732 static int add_named_array(const char *val
, struct kernel_param
*kp
)
4734 /* val must be "md_*" where * is not all digits.
4735 * We allocate an array with a large free minor number, and
4736 * set the name to val. val must not already be an active name.
4738 int len
= strlen(val
);
4739 char buf
[DISK_NAME_LEN
];
4741 while (len
&& val
[len
-1] == '\n')
4743 if (len
>= DISK_NAME_LEN
)
4745 strlcpy(buf
, val
, len
+1);
4746 if (strncmp(buf
, "md_", 3) != 0)
4748 return md_alloc(0, buf
);
4751 static void md_safemode_timeout(unsigned long data
)
4753 struct mddev
*mddev
= (struct mddev
*) data
;
4755 if (!atomic_read(&mddev
->writes_pending
)) {
4756 mddev
->safemode
= 1;
4757 if (mddev
->external
)
4758 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4760 md_wakeup_thread(mddev
->thread
);
4763 static int start_dirty_degraded
;
4765 int md_run(struct mddev
*mddev
)
4768 struct md_rdev
*rdev
;
4769 struct md_personality
*pers
;
4771 if (list_empty(&mddev
->disks
))
4772 /* cannot run an array with no devices.. */
4777 /* Cannot run until previous stop completes properly */
4778 if (mddev
->sysfs_active
)
4782 * Analyze all RAID superblock(s)
4784 if (!mddev
->raid_disks
) {
4785 if (!mddev
->persistent
)
4790 if (mddev
->level
!= LEVEL_NONE
)
4791 request_module("md-level-%d", mddev
->level
);
4792 else if (mddev
->clevel
[0])
4793 request_module("md-%s", mddev
->clevel
);
4796 * Drop all container device buffers, from now on
4797 * the only valid external interface is through the md
4800 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
4801 if (test_bit(Faulty
, &rdev
->flags
))
4803 sync_blockdev(rdev
->bdev
);
4804 invalidate_bdev(rdev
->bdev
);
4806 /* perform some consistency tests on the device.
4807 * We don't want the data to overlap the metadata,
4808 * Internal Bitmap issues have been handled elsewhere.
4810 if (rdev
->meta_bdev
) {
4811 /* Nothing to check */;
4812 } else if (rdev
->data_offset
< rdev
->sb_start
) {
4813 if (mddev
->dev_sectors
&&
4814 rdev
->data_offset
+ mddev
->dev_sectors
4816 printk("md: %s: data overlaps metadata\n",
4821 if (rdev
->sb_start
+ rdev
->sb_size
/512
4822 > rdev
->data_offset
) {
4823 printk("md: %s: metadata overlaps data\n",
4828 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
4831 if (mddev
->bio_set
== NULL
)
4832 mddev
->bio_set
= bioset_create(BIO_POOL_SIZE
,
4833 sizeof(struct mddev
*));
4835 spin_lock(&pers_lock
);
4836 pers
= find_pers(mddev
->level
, mddev
->clevel
);
4837 if (!pers
|| !try_module_get(pers
->owner
)) {
4838 spin_unlock(&pers_lock
);
4839 if (mddev
->level
!= LEVEL_NONE
)
4840 printk(KERN_WARNING
"md: personality for level %d is not loaded!\n",
4843 printk(KERN_WARNING
"md: personality for level %s is not loaded!\n",
4848 spin_unlock(&pers_lock
);
4849 if (mddev
->level
!= pers
->level
) {
4850 mddev
->level
= pers
->level
;
4851 mddev
->new_level
= pers
->level
;
4853 strlcpy(mddev
->clevel
, pers
->name
, sizeof(mddev
->clevel
));
4855 if (mddev
->reshape_position
!= MaxSector
&&
4856 pers
->start_reshape
== NULL
) {
4857 /* This personality cannot handle reshaping... */
4859 module_put(pers
->owner
);
4863 if (pers
->sync_request
) {
4864 /* Warn if this is a potentially silly
4867 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
4868 struct md_rdev
*rdev2
;
4871 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
4872 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
) {
4874 rdev
->bdev
->bd_contains
==
4875 rdev2
->bdev
->bd_contains
) {
4877 "%s: WARNING: %s appears to be"
4878 " on the same physical disk as"
4881 bdevname(rdev
->bdev
,b
),
4882 bdevname(rdev2
->bdev
,b2
));
4889 "True protection against single-disk"
4890 " failure might be compromised.\n");
4893 mddev
->recovery
= 0;
4894 /* may be over-ridden by personality */
4895 mddev
->resync_max_sectors
= mddev
->dev_sectors
;
4897 mddev
->ok_start_degraded
= start_dirty_degraded
;
4899 if (start_readonly
&& mddev
->ro
== 0)
4900 mddev
->ro
= 2; /* read-only, but switch on first write */
4902 err
= mddev
->pers
->run(mddev
);
4904 printk(KERN_ERR
"md: pers->run() failed ...\n");
4905 else if (mddev
->pers
->size(mddev
, 0, 0) < mddev
->array_sectors
) {
4906 WARN_ONCE(!mddev
->external_size
, "%s: default size too small,"
4907 " but 'external_size' not in effect?\n", __func__
);
4909 "md: invalid array_size %llu > default size %llu\n",
4910 (unsigned long long)mddev
->array_sectors
/ 2,
4911 (unsigned long long)mddev
->pers
->size(mddev
, 0, 0) / 2);
4913 mddev
->pers
->stop(mddev
);
4915 if (err
== 0 && mddev
->pers
->sync_request
) {
4916 err
= bitmap_create(mddev
);
4918 printk(KERN_ERR
"%s: failed to create bitmap (%d)\n",
4919 mdname(mddev
), err
);
4920 mddev
->pers
->stop(mddev
);
4924 module_put(mddev
->pers
->owner
);
4926 bitmap_destroy(mddev
);
4929 if (mddev
->pers
->sync_request
) {
4930 if (mddev
->kobj
.sd
&&
4931 sysfs_create_group(&mddev
->kobj
, &md_redundancy_group
))
4933 "md: cannot register extra attributes for %s\n",
4935 mddev
->sysfs_action
= sysfs_get_dirent_safe(mddev
->kobj
.sd
, "sync_action");
4936 } else if (mddev
->ro
== 2) /* auto-readonly not meaningful */
4939 atomic_set(&mddev
->writes_pending
,0);
4940 atomic_set(&mddev
->max_corr_read_errors
,
4941 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS
);
4942 mddev
->safemode
= 0;
4943 mddev
->safemode_timer
.function
= md_safemode_timeout
;
4944 mddev
->safemode_timer
.data
= (unsigned long) mddev
;
4945 mddev
->safemode_delay
= (200 * HZ
)/1000 +1; /* 200 msec delay */
4949 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
4950 if (rdev
->raid_disk
>= 0)
4951 if (sysfs_link_rdev(mddev
, rdev
))
4952 /* failure here is OK */;
4954 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4957 md_update_sb(mddev
, 0);
4959 md_new_event(mddev
);
4960 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4961 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
4962 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
4965 EXPORT_SYMBOL_GPL(md_run
);
4967 static int do_md_run(struct mddev
*mddev
)
4971 err
= md_run(mddev
);
4974 err
= bitmap_load(mddev
);
4976 bitmap_destroy(mddev
);
4980 md_wakeup_thread(mddev
->thread
);
4981 md_wakeup_thread(mddev
->sync_thread
); /* possibly kick off a reshape */
4983 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
4984 revalidate_disk(mddev
->gendisk
);
4986 kobject_uevent(&disk_to_dev(mddev
->gendisk
)->kobj
, KOBJ_CHANGE
);
4991 static int restart_array(struct mddev
*mddev
)
4993 struct gendisk
*disk
= mddev
->gendisk
;
4995 /* Complain if it has no devices */
4996 if (list_empty(&mddev
->disks
))
5002 mddev
->safemode
= 0;
5004 set_disk_ro(disk
, 0);
5005 printk(KERN_INFO
"md: %s switched to read-write mode.\n",
5007 /* Kick recovery or resync if necessary */
5008 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5009 md_wakeup_thread(mddev
->thread
);
5010 md_wakeup_thread(mddev
->sync_thread
);
5011 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
5015 /* similar to deny_write_access, but accounts for our holding a reference
5016 * to the file ourselves */
5017 static int deny_bitmap_write_access(struct file
* file
)
5019 struct inode
*inode
= file
->f_mapping
->host
;
5021 spin_lock(&inode
->i_lock
);
5022 if (atomic_read(&inode
->i_writecount
) > 1) {
5023 spin_unlock(&inode
->i_lock
);
5026 atomic_set(&inode
->i_writecount
, -1);
5027 spin_unlock(&inode
->i_lock
);
5032 void restore_bitmap_write_access(struct file
*file
)
5034 struct inode
*inode
= file
->f_mapping
->host
;
5036 spin_lock(&inode
->i_lock
);
5037 atomic_set(&inode
->i_writecount
, 1);
5038 spin_unlock(&inode
->i_lock
);
5041 static void md_clean(struct mddev
*mddev
)
5043 mddev
->array_sectors
= 0;
5044 mddev
->external_size
= 0;
5045 mddev
->dev_sectors
= 0;
5046 mddev
->raid_disks
= 0;
5047 mddev
->recovery_cp
= 0;
5048 mddev
->resync_min
= 0;
5049 mddev
->resync_max
= MaxSector
;
5050 mddev
->reshape_position
= MaxSector
;
5051 mddev
->external
= 0;
5052 mddev
->persistent
= 0;
5053 mddev
->level
= LEVEL_NONE
;
5054 mddev
->clevel
[0] = 0;
5057 mddev
->metadata_type
[0] = 0;
5058 mddev
->chunk_sectors
= 0;
5059 mddev
->ctime
= mddev
->utime
= 0;
5061 mddev
->max_disks
= 0;
5063 mddev
->can_decrease_events
= 0;
5064 mddev
->delta_disks
= 0;
5065 mddev
->new_level
= LEVEL_NONE
;
5066 mddev
->new_layout
= 0;
5067 mddev
->new_chunk_sectors
= 0;
5068 mddev
->curr_resync
= 0;
5069 mddev
->resync_mismatches
= 0;
5070 mddev
->suspend_lo
= mddev
->suspend_hi
= 0;
5071 mddev
->sync_speed_min
= mddev
->sync_speed_max
= 0;
5072 mddev
->recovery
= 0;
5075 mddev
->degraded
= 0;
5076 mddev
->safemode
= 0;
5077 mddev
->bitmap_info
.offset
= 0;
5078 mddev
->bitmap_info
.default_offset
= 0;
5079 mddev
->bitmap_info
.chunksize
= 0;
5080 mddev
->bitmap_info
.daemon_sleep
= 0;
5081 mddev
->bitmap_info
.max_write_behind
= 0;
5084 static void __md_stop_writes(struct mddev
*mddev
)
5086 if (mddev
->sync_thread
) {
5087 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
5088 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
5089 reap_sync_thread(mddev
);
5092 del_timer_sync(&mddev
->safemode_timer
);
5094 bitmap_flush(mddev
);
5095 md_super_wait(mddev
);
5097 if (!mddev
->in_sync
|| mddev
->flags
) {
5098 /* mark array as shutdown cleanly */
5100 md_update_sb(mddev
, 1);
5104 void md_stop_writes(struct mddev
*mddev
)
5107 __md_stop_writes(mddev
);
5108 mddev_unlock(mddev
);
5110 EXPORT_SYMBOL_GPL(md_stop_writes
);
5112 void md_stop(struct mddev
*mddev
)
5115 mddev
->pers
->stop(mddev
);
5116 if (mddev
->pers
->sync_request
&& mddev
->to_remove
== NULL
)
5117 mddev
->to_remove
= &md_redundancy_group
;
5118 module_put(mddev
->pers
->owner
);
5120 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
5122 EXPORT_SYMBOL_GPL(md_stop
);
5124 static int md_set_readonly(struct mddev
*mddev
, int is_open
)
5127 mutex_lock(&mddev
->open_mutex
);
5128 if (atomic_read(&mddev
->openers
) > is_open
) {
5129 printk("md: %s still in use.\n",mdname(mddev
));
5134 __md_stop_writes(mddev
);
5140 set_disk_ro(mddev
->gendisk
, 1);
5141 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
5142 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
5146 mutex_unlock(&mddev
->open_mutex
);
5151 * 0 - completely stop and dis-assemble array
5152 * 2 - stop but do not disassemble array
5154 static int do_md_stop(struct mddev
* mddev
, int mode
, int is_open
)
5156 struct gendisk
*disk
= mddev
->gendisk
;
5157 struct md_rdev
*rdev
;
5159 mutex_lock(&mddev
->open_mutex
);
5160 if (atomic_read(&mddev
->openers
) > is_open
||
5161 mddev
->sysfs_active
) {
5162 printk("md: %s still in use.\n",mdname(mddev
));
5163 mutex_unlock(&mddev
->open_mutex
);
5169 set_disk_ro(disk
, 0);
5171 __md_stop_writes(mddev
);
5173 mddev
->queue
->merge_bvec_fn
= NULL
;
5174 mddev
->queue
->backing_dev_info
.congested_fn
= NULL
;
5176 /* tell userspace to handle 'inactive' */
5177 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
5179 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
5180 if (rdev
->raid_disk
>= 0)
5181 sysfs_unlink_rdev(mddev
, rdev
);
5183 set_capacity(disk
, 0);
5184 mutex_unlock(&mddev
->open_mutex
);
5186 revalidate_disk(disk
);
5191 mutex_unlock(&mddev
->open_mutex
);
5193 * Free resources if final stop
5196 printk(KERN_INFO
"md: %s stopped.\n", mdname(mddev
));
5198 bitmap_destroy(mddev
);
5199 if (mddev
->bitmap_info
.file
) {
5200 restore_bitmap_write_access(mddev
->bitmap_info
.file
);
5201 fput(mddev
->bitmap_info
.file
);
5202 mddev
->bitmap_info
.file
= NULL
;
5204 mddev
->bitmap_info
.offset
= 0;
5206 export_array(mddev
);
5209 kobject_uevent(&disk_to_dev(mddev
->gendisk
)->kobj
, KOBJ_CHANGE
);
5210 if (mddev
->hold_active
== UNTIL_STOP
)
5211 mddev
->hold_active
= 0;
5213 blk_integrity_unregister(disk
);
5214 md_new_event(mddev
);
5215 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
5220 static void autorun_array(struct mddev
*mddev
)
5222 struct md_rdev
*rdev
;
5225 if (list_empty(&mddev
->disks
))
5228 printk(KERN_INFO
"md: running: ");
5230 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
5231 char b
[BDEVNAME_SIZE
];
5232 printk("<%s>", bdevname(rdev
->bdev
,b
));
5236 err
= do_md_run(mddev
);
5238 printk(KERN_WARNING
"md: do_md_run() returned %d\n", err
);
5239 do_md_stop(mddev
, 0, 0);
5244 * lets try to run arrays based on all disks that have arrived
5245 * until now. (those are in pending_raid_disks)
5247 * the method: pick the first pending disk, collect all disks with
5248 * the same UUID, remove all from the pending list and put them into
5249 * the 'same_array' list. Then order this list based on superblock
5250 * update time (freshest comes first), kick out 'old' disks and
5251 * compare superblocks. If everything's fine then run it.
5253 * If "unit" is allocated, then bump its reference count
5255 static void autorun_devices(int part
)
5257 struct md_rdev
*rdev0
, *rdev
, *tmp
;
5258 struct mddev
*mddev
;
5259 char b
[BDEVNAME_SIZE
];
5261 printk(KERN_INFO
"md: autorun ...\n");
5262 while (!list_empty(&pending_raid_disks
)) {
5265 LIST_HEAD(candidates
);
5266 rdev0
= list_entry(pending_raid_disks
.next
,
5267 struct md_rdev
, same_set
);
5269 printk(KERN_INFO
"md: considering %s ...\n",
5270 bdevname(rdev0
->bdev
,b
));
5271 INIT_LIST_HEAD(&candidates
);
5272 rdev_for_each_list(rdev
, tmp
, &pending_raid_disks
)
5273 if (super_90_load(rdev
, rdev0
, 0) >= 0) {
5274 printk(KERN_INFO
"md: adding %s ...\n",
5275 bdevname(rdev
->bdev
,b
));
5276 list_move(&rdev
->same_set
, &candidates
);
5279 * now we have a set of devices, with all of them having
5280 * mostly sane superblocks. It's time to allocate the
5284 dev
= MKDEV(mdp_major
,
5285 rdev0
->preferred_minor
<< MdpMinorShift
);
5286 unit
= MINOR(dev
) >> MdpMinorShift
;
5288 dev
= MKDEV(MD_MAJOR
, rdev0
->preferred_minor
);
5291 if (rdev0
->preferred_minor
!= unit
) {
5292 printk(KERN_INFO
"md: unit number in %s is bad: %d\n",
5293 bdevname(rdev0
->bdev
, b
), rdev0
->preferred_minor
);
5297 md_probe(dev
, NULL
, NULL
);
5298 mddev
= mddev_find(dev
);
5299 if (!mddev
|| !mddev
->gendisk
) {
5303 "md: cannot allocate memory for md drive.\n");
5306 if (mddev_lock(mddev
))
5307 printk(KERN_WARNING
"md: %s locked, cannot run\n",
5309 else if (mddev
->raid_disks
|| mddev
->major_version
5310 || !list_empty(&mddev
->disks
)) {
5312 "md: %s already running, cannot run %s\n",
5313 mdname(mddev
), bdevname(rdev0
->bdev
,b
));
5314 mddev_unlock(mddev
);
5316 printk(KERN_INFO
"md: created %s\n", mdname(mddev
));
5317 mddev
->persistent
= 1;
5318 rdev_for_each_list(rdev
, tmp
, &candidates
) {
5319 list_del_init(&rdev
->same_set
);
5320 if (bind_rdev_to_array(rdev
, mddev
))
5323 autorun_array(mddev
);
5324 mddev_unlock(mddev
);
5326 /* on success, candidates will be empty, on error
5329 rdev_for_each_list(rdev
, tmp
, &candidates
) {
5330 list_del_init(&rdev
->same_set
);
5335 printk(KERN_INFO
"md: ... autorun DONE.\n");
5337 #endif /* !MODULE */
5339 static int get_version(void __user
* arg
)
5343 ver
.major
= MD_MAJOR_VERSION
;
5344 ver
.minor
= MD_MINOR_VERSION
;
5345 ver
.patchlevel
= MD_PATCHLEVEL_VERSION
;
5347 if (copy_to_user(arg
, &ver
, sizeof(ver
)))
5353 static int get_array_info(struct mddev
* mddev
, void __user
* arg
)
5355 mdu_array_info_t info
;
5356 int nr
,working
,insync
,failed
,spare
;
5357 struct md_rdev
*rdev
;
5359 nr
=working
=insync
=failed
=spare
=0;
5360 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
5362 if (test_bit(Faulty
, &rdev
->flags
))
5366 if (test_bit(In_sync
, &rdev
->flags
))
5373 info
.major_version
= mddev
->major_version
;
5374 info
.minor_version
= mddev
->minor_version
;
5375 info
.patch_version
= MD_PATCHLEVEL_VERSION
;
5376 info
.ctime
= mddev
->ctime
;
5377 info
.level
= mddev
->level
;
5378 info
.size
= mddev
->dev_sectors
/ 2;
5379 if (info
.size
!= mddev
->dev_sectors
/ 2) /* overflow */
5382 info
.raid_disks
= mddev
->raid_disks
;
5383 info
.md_minor
= mddev
->md_minor
;
5384 info
.not_persistent
= !mddev
->persistent
;
5386 info
.utime
= mddev
->utime
;
5389 info
.state
= (1<<MD_SB_CLEAN
);
5390 if (mddev
->bitmap
&& mddev
->bitmap_info
.offset
)
5391 info
.state
= (1<<MD_SB_BITMAP_PRESENT
);
5392 info
.active_disks
= insync
;
5393 info
.working_disks
= working
;
5394 info
.failed_disks
= failed
;
5395 info
.spare_disks
= spare
;
5397 info
.layout
= mddev
->layout
;
5398 info
.chunk_size
= mddev
->chunk_sectors
<< 9;
5400 if (copy_to_user(arg
, &info
, sizeof(info
)))
5406 static int get_bitmap_file(struct mddev
* mddev
, void __user
* arg
)
5408 mdu_bitmap_file_t
*file
= NULL
; /* too big for stack allocation */
5409 char *ptr
, *buf
= NULL
;
5412 if (md_allow_write(mddev
))
5413 file
= kmalloc(sizeof(*file
), GFP_NOIO
);
5415 file
= kmalloc(sizeof(*file
), GFP_KERNEL
);
5420 /* bitmap disabled, zero the first byte and copy out */
5421 if (!mddev
->bitmap
|| !mddev
->bitmap
->file
) {
5422 file
->pathname
[0] = '\0';
5426 buf
= kmalloc(sizeof(file
->pathname
), GFP_KERNEL
);
5430 ptr
= d_path(&mddev
->bitmap
->file
->f_path
, buf
, sizeof(file
->pathname
));
5434 strcpy(file
->pathname
, ptr
);
5438 if (copy_to_user(arg
, file
, sizeof(*file
)))
5446 static int get_disk_info(struct mddev
* mddev
, void __user
* arg
)
5448 mdu_disk_info_t info
;
5449 struct md_rdev
*rdev
;
5451 if (copy_from_user(&info
, arg
, sizeof(info
)))
5454 rdev
= find_rdev_nr(mddev
, info
.number
);
5456 info
.major
= MAJOR(rdev
->bdev
->bd_dev
);
5457 info
.minor
= MINOR(rdev
->bdev
->bd_dev
);
5458 info
.raid_disk
= rdev
->raid_disk
;
5460 if (test_bit(Faulty
, &rdev
->flags
))
5461 info
.state
|= (1<<MD_DISK_FAULTY
);
5462 else if (test_bit(In_sync
, &rdev
->flags
)) {
5463 info
.state
|= (1<<MD_DISK_ACTIVE
);
5464 info
.state
|= (1<<MD_DISK_SYNC
);
5466 if (test_bit(WriteMostly
, &rdev
->flags
))
5467 info
.state
|= (1<<MD_DISK_WRITEMOSTLY
);
5469 info
.major
= info
.minor
= 0;
5470 info
.raid_disk
= -1;
5471 info
.state
= (1<<MD_DISK_REMOVED
);
5474 if (copy_to_user(arg
, &info
, sizeof(info
)))
5480 static int add_new_disk(struct mddev
* mddev
, mdu_disk_info_t
*info
)
5482 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
5483 struct md_rdev
*rdev
;
5484 dev_t dev
= MKDEV(info
->major
,info
->minor
);
5486 if (info
->major
!= MAJOR(dev
) || info
->minor
!= MINOR(dev
))
5489 if (!mddev
->raid_disks
) {
5491 /* expecting a device which has a superblock */
5492 rdev
= md_import_device(dev
, mddev
->major_version
, mddev
->minor_version
);
5495 "md: md_import_device returned %ld\n",
5497 return PTR_ERR(rdev
);
5499 if (!list_empty(&mddev
->disks
)) {
5500 struct md_rdev
*rdev0
5501 = list_entry(mddev
->disks
.next
,
5502 struct md_rdev
, same_set
);
5503 err
= super_types
[mddev
->major_version
]
5504 .load_super(rdev
, rdev0
, mddev
->minor_version
);
5507 "md: %s has different UUID to %s\n",
5508 bdevname(rdev
->bdev
,b
),
5509 bdevname(rdev0
->bdev
,b2
));
5514 err
= bind_rdev_to_array(rdev
, mddev
);
5521 * add_new_disk can be used once the array is assembled
5522 * to add "hot spares". They must already have a superblock
5527 if (!mddev
->pers
->hot_add_disk
) {
5529 "%s: personality does not support diskops!\n",
5533 if (mddev
->persistent
)
5534 rdev
= md_import_device(dev
, mddev
->major_version
,
5535 mddev
->minor_version
);
5537 rdev
= md_import_device(dev
, -1, -1);
5540 "md: md_import_device returned %ld\n",
5542 return PTR_ERR(rdev
);
5544 /* set saved_raid_disk if appropriate */
5545 if (!mddev
->persistent
) {
5546 if (info
->state
& (1<<MD_DISK_SYNC
) &&
5547 info
->raid_disk
< mddev
->raid_disks
) {
5548 rdev
->raid_disk
= info
->raid_disk
;
5549 set_bit(In_sync
, &rdev
->flags
);
5551 rdev
->raid_disk
= -1;
5553 super_types
[mddev
->major_version
].
5554 validate_super(mddev
, rdev
);
5555 if ((info
->state
& (1<<MD_DISK_SYNC
)) &&
5556 (!test_bit(In_sync
, &rdev
->flags
) ||
5557 rdev
->raid_disk
!= info
->raid_disk
)) {
5558 /* This was a hot-add request, but events doesn't
5559 * match, so reject it.
5565 if (test_bit(In_sync
, &rdev
->flags
))
5566 rdev
->saved_raid_disk
= rdev
->raid_disk
;
5568 rdev
->saved_raid_disk
= -1;
5570 clear_bit(In_sync
, &rdev
->flags
); /* just to be sure */
5571 if (info
->state
& (1<<MD_DISK_WRITEMOSTLY
))
5572 set_bit(WriteMostly
, &rdev
->flags
);
5574 clear_bit(WriteMostly
, &rdev
->flags
);
5576 rdev
->raid_disk
= -1;
5577 err
= bind_rdev_to_array(rdev
, mddev
);
5578 if (!err
&& !mddev
->pers
->hot_remove_disk
) {
5579 /* If there is hot_add_disk but no hot_remove_disk
5580 * then added disks for geometry changes,
5581 * and should be added immediately.
5583 super_types
[mddev
->major_version
].
5584 validate_super(mddev
, rdev
);
5585 err
= mddev
->pers
->hot_add_disk(mddev
, rdev
);
5587 unbind_rdev_from_array(rdev
);
5592 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
5594 md_update_sb(mddev
, 1);
5595 if (mddev
->degraded
)
5596 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
5597 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5599 md_new_event(mddev
);
5600 md_wakeup_thread(mddev
->thread
);
5604 /* otherwise, add_new_disk is only allowed
5605 * for major_version==0 superblocks
5607 if (mddev
->major_version
!= 0) {
5608 printk(KERN_WARNING
"%s: ADD_NEW_DISK not supported\n",
5613 if (!(info
->state
& (1<<MD_DISK_FAULTY
))) {
5615 rdev
= md_import_device(dev
, -1, 0);
5618 "md: error, md_import_device() returned %ld\n",
5620 return PTR_ERR(rdev
);
5622 rdev
->desc_nr
= info
->number
;
5623 if (info
->raid_disk
< mddev
->raid_disks
)
5624 rdev
->raid_disk
= info
->raid_disk
;
5626 rdev
->raid_disk
= -1;
5628 if (rdev
->raid_disk
< mddev
->raid_disks
)
5629 if (info
->state
& (1<<MD_DISK_SYNC
))
5630 set_bit(In_sync
, &rdev
->flags
);
5632 if (info
->state
& (1<<MD_DISK_WRITEMOSTLY
))
5633 set_bit(WriteMostly
, &rdev
->flags
);
5635 if (!mddev
->persistent
) {
5636 printk(KERN_INFO
"md: nonpersistent superblock ...\n");
5637 rdev
->sb_start
= i_size_read(rdev
->bdev
->bd_inode
) / 512;
5639 rdev
->sb_start
= calc_dev_sboffset(rdev
);
5640 rdev
->sectors
= rdev
->sb_start
;
5642 err
= bind_rdev_to_array(rdev
, mddev
);
5652 static int hot_remove_disk(struct mddev
* mddev
, dev_t dev
)
5654 char b
[BDEVNAME_SIZE
];
5655 struct md_rdev
*rdev
;
5657 rdev
= find_rdev(mddev
, dev
);
5661 if (rdev
->raid_disk
>= 0)
5664 kick_rdev_from_array(rdev
);
5665 md_update_sb(mddev
, 1);
5666 md_new_event(mddev
);
5670 printk(KERN_WARNING
"md: cannot remove active disk %s from %s ...\n",
5671 bdevname(rdev
->bdev
,b
), mdname(mddev
));
5675 static int hot_add_disk(struct mddev
* mddev
, dev_t dev
)
5677 char b
[BDEVNAME_SIZE
];
5679 struct md_rdev
*rdev
;
5684 if (mddev
->major_version
!= 0) {
5685 printk(KERN_WARNING
"%s: HOT_ADD may only be used with"
5686 " version-0 superblocks.\n",
5690 if (!mddev
->pers
->hot_add_disk
) {
5692 "%s: personality does not support diskops!\n",
5697 rdev
= md_import_device(dev
, -1, 0);
5700 "md: error, md_import_device() returned %ld\n",
5705 if (mddev
->persistent
)
5706 rdev
->sb_start
= calc_dev_sboffset(rdev
);
5708 rdev
->sb_start
= i_size_read(rdev
->bdev
->bd_inode
) / 512;
5710 rdev
->sectors
= rdev
->sb_start
;
5712 if (test_bit(Faulty
, &rdev
->flags
)) {
5714 "md: can not hot-add faulty %s disk to %s!\n",
5715 bdevname(rdev
->bdev
,b
), mdname(mddev
));
5719 clear_bit(In_sync
, &rdev
->flags
);
5721 rdev
->saved_raid_disk
= -1;
5722 err
= bind_rdev_to_array(rdev
, mddev
);
5727 * The rest should better be atomic, we can have disk failures
5728 * noticed in interrupt contexts ...
5731 rdev
->raid_disk
= -1;
5733 md_update_sb(mddev
, 1);
5736 * Kick recovery, maybe this spare has to be added to the
5737 * array immediately.
5739 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5740 md_wakeup_thread(mddev
->thread
);
5741 md_new_event(mddev
);
5749 static int set_bitmap_file(struct mddev
*mddev
, int fd
)
5754 if (!mddev
->pers
->quiesce
)
5756 if (mddev
->recovery
|| mddev
->sync_thread
)
5758 /* we should be able to change the bitmap.. */
5764 return -EEXIST
; /* cannot add when bitmap is present */
5765 mddev
->bitmap_info
.file
= fget(fd
);
5767 if (mddev
->bitmap_info
.file
== NULL
) {
5768 printk(KERN_ERR
"%s: error: failed to get bitmap file\n",
5773 err
= deny_bitmap_write_access(mddev
->bitmap_info
.file
);
5775 printk(KERN_ERR
"%s: error: bitmap file is already in use\n",
5777 fput(mddev
->bitmap_info
.file
);
5778 mddev
->bitmap_info
.file
= NULL
;
5781 mddev
->bitmap_info
.offset
= 0; /* file overrides offset */
5782 } else if (mddev
->bitmap
== NULL
)
5783 return -ENOENT
; /* cannot remove what isn't there */
5786 mddev
->pers
->quiesce(mddev
, 1);
5788 err
= bitmap_create(mddev
);
5790 err
= bitmap_load(mddev
);
5792 if (fd
< 0 || err
) {
5793 bitmap_destroy(mddev
);
5794 fd
= -1; /* make sure to put the file */
5796 mddev
->pers
->quiesce(mddev
, 0);
5799 if (mddev
->bitmap_info
.file
) {
5800 restore_bitmap_write_access(mddev
->bitmap_info
.file
);
5801 fput(mddev
->bitmap_info
.file
);
5803 mddev
->bitmap_info
.file
= NULL
;
5810 * set_array_info is used two different ways
5811 * The original usage is when creating a new array.
5812 * In this usage, raid_disks is > 0 and it together with
5813 * level, size, not_persistent,layout,chunksize determine the
5814 * shape of the array.
5815 * This will always create an array with a type-0.90.0 superblock.
5816 * The newer usage is when assembling an array.
5817 * In this case raid_disks will be 0, and the major_version field is
5818 * use to determine which style super-blocks are to be found on the devices.
5819 * The minor and patch _version numbers are also kept incase the
5820 * super_block handler wishes to interpret them.
5822 static int set_array_info(struct mddev
* mddev
, mdu_array_info_t
*info
)
5825 if (info
->raid_disks
== 0) {
5826 /* just setting version number for superblock loading */
5827 if (info
->major_version
< 0 ||
5828 info
->major_version
>= ARRAY_SIZE(super_types
) ||
5829 super_types
[info
->major_version
].name
== NULL
) {
5830 /* maybe try to auto-load a module? */
5832 "md: superblock version %d not known\n",
5833 info
->major_version
);
5836 mddev
->major_version
= info
->major_version
;
5837 mddev
->minor_version
= info
->minor_version
;
5838 mddev
->patch_version
= info
->patch_version
;
5839 mddev
->persistent
= !info
->not_persistent
;
5840 /* ensure mddev_put doesn't delete this now that there
5841 * is some minimal configuration.
5843 mddev
->ctime
= get_seconds();
5846 mddev
->major_version
= MD_MAJOR_VERSION
;
5847 mddev
->minor_version
= MD_MINOR_VERSION
;
5848 mddev
->patch_version
= MD_PATCHLEVEL_VERSION
;
5849 mddev
->ctime
= get_seconds();
5851 mddev
->level
= info
->level
;
5852 mddev
->clevel
[0] = 0;
5853 mddev
->dev_sectors
= 2 * (sector_t
)info
->size
;
5854 mddev
->raid_disks
= info
->raid_disks
;
5855 /* don't set md_minor, it is determined by which /dev/md* was
5858 if (info
->state
& (1<<MD_SB_CLEAN
))
5859 mddev
->recovery_cp
= MaxSector
;
5861 mddev
->recovery_cp
= 0;
5862 mddev
->persistent
= ! info
->not_persistent
;
5863 mddev
->external
= 0;
5865 mddev
->layout
= info
->layout
;
5866 mddev
->chunk_sectors
= info
->chunk_size
>> 9;
5868 mddev
->max_disks
= MD_SB_DISKS
;
5870 if (mddev
->persistent
)
5872 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
5874 mddev
->bitmap_info
.default_offset
= MD_SB_BYTES
>> 9;
5875 mddev
->bitmap_info
.offset
= 0;
5877 mddev
->reshape_position
= MaxSector
;
5880 * Generate a 128 bit UUID
5882 get_random_bytes(mddev
->uuid
, 16);
5884 mddev
->new_level
= mddev
->level
;
5885 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
5886 mddev
->new_layout
= mddev
->layout
;
5887 mddev
->delta_disks
= 0;
5892 void md_set_array_sectors(struct mddev
*mddev
, sector_t array_sectors
)
5894 WARN(!mddev_is_locked(mddev
), "%s: unlocked mddev!\n", __func__
);
5896 if (mddev
->external_size
)
5899 mddev
->array_sectors
= array_sectors
;
5901 EXPORT_SYMBOL(md_set_array_sectors
);
5903 static int update_size(struct mddev
*mddev
, sector_t num_sectors
)
5905 struct md_rdev
*rdev
;
5907 int fit
= (num_sectors
== 0);
5909 if (mddev
->pers
->resize
== NULL
)
5911 /* The "num_sectors" is the number of sectors of each device that
5912 * is used. This can only make sense for arrays with redundancy.
5913 * linear and raid0 always use whatever space is available. We can only
5914 * consider changing this number if no resync or reconstruction is
5915 * happening, and if the new size is acceptable. It must fit before the
5916 * sb_start or, if that is <data_offset, it must fit before the size
5917 * of each device. If num_sectors is zero, we find the largest size
5920 if (mddev
->sync_thread
)
5923 /* Sorry, cannot grow a bitmap yet, just remove it,
5927 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
5928 sector_t avail
= rdev
->sectors
;
5930 if (fit
&& (num_sectors
== 0 || num_sectors
> avail
))
5931 num_sectors
= avail
;
5932 if (avail
< num_sectors
)
5935 rv
= mddev
->pers
->resize(mddev
, num_sectors
);
5937 revalidate_disk(mddev
->gendisk
);
5941 static int update_raid_disks(struct mddev
*mddev
, int raid_disks
)
5944 /* change the number of raid disks */
5945 if (mddev
->pers
->check_reshape
== NULL
)
5947 if (raid_disks
<= 0 ||
5948 (mddev
->max_disks
&& raid_disks
>= mddev
->max_disks
))
5950 if (mddev
->sync_thread
|| mddev
->reshape_position
!= MaxSector
)
5952 mddev
->delta_disks
= raid_disks
- mddev
->raid_disks
;
5954 rv
= mddev
->pers
->check_reshape(mddev
);
5956 mddev
->delta_disks
= 0;
5962 * update_array_info is used to change the configuration of an
5964 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
5965 * fields in the info are checked against the array.
5966 * Any differences that cannot be handled will cause an error.
5967 * Normally, only one change can be managed at a time.
5969 static int update_array_info(struct mddev
*mddev
, mdu_array_info_t
*info
)
5975 /* calculate expected state,ignoring low bits */
5976 if (mddev
->bitmap
&& mddev
->bitmap_info
.offset
)
5977 state
|= (1 << MD_SB_BITMAP_PRESENT
);
5979 if (mddev
->major_version
!= info
->major_version
||
5980 mddev
->minor_version
!= info
->minor_version
||
5981 /* mddev->patch_version != info->patch_version || */
5982 mddev
->ctime
!= info
->ctime
||
5983 mddev
->level
!= info
->level
||
5984 /* mddev->layout != info->layout || */
5985 !mddev
->persistent
!= info
->not_persistent
||
5986 mddev
->chunk_sectors
!= info
->chunk_size
>> 9 ||
5987 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
5988 ((state
^info
->state
) & 0xfffffe00)
5991 /* Check there is only one change */
5992 if (info
->size
>= 0 && mddev
->dev_sectors
/ 2 != info
->size
)
5994 if (mddev
->raid_disks
!= info
->raid_disks
)
5996 if (mddev
->layout
!= info
->layout
)
5998 if ((state
^ info
->state
) & (1<<MD_SB_BITMAP_PRESENT
))
6005 if (mddev
->layout
!= info
->layout
) {
6007 * we don't need to do anything at the md level, the
6008 * personality will take care of it all.
6010 if (mddev
->pers
->check_reshape
== NULL
)
6013 mddev
->new_layout
= info
->layout
;
6014 rv
= mddev
->pers
->check_reshape(mddev
);
6016 mddev
->new_layout
= mddev
->layout
;
6020 if (info
->size
>= 0 && mddev
->dev_sectors
/ 2 != info
->size
)
6021 rv
= update_size(mddev
, (sector_t
)info
->size
* 2);
6023 if (mddev
->raid_disks
!= info
->raid_disks
)
6024 rv
= update_raid_disks(mddev
, info
->raid_disks
);
6026 if ((state
^ info
->state
) & (1<<MD_SB_BITMAP_PRESENT
)) {
6027 if (mddev
->pers
->quiesce
== NULL
)
6029 if (mddev
->recovery
|| mddev
->sync_thread
)
6031 if (info
->state
& (1<<MD_SB_BITMAP_PRESENT
)) {
6032 /* add the bitmap */
6035 if (mddev
->bitmap_info
.default_offset
== 0)
6037 mddev
->bitmap_info
.offset
=
6038 mddev
->bitmap_info
.default_offset
;
6039 mddev
->pers
->quiesce(mddev
, 1);
6040 rv
= bitmap_create(mddev
);
6042 rv
= bitmap_load(mddev
);
6044 bitmap_destroy(mddev
);
6045 mddev
->pers
->quiesce(mddev
, 0);
6047 /* remove the bitmap */
6050 if (mddev
->bitmap
->file
)
6052 mddev
->pers
->quiesce(mddev
, 1);
6053 bitmap_destroy(mddev
);
6054 mddev
->pers
->quiesce(mddev
, 0);
6055 mddev
->bitmap_info
.offset
= 0;
6058 md_update_sb(mddev
, 1);
6062 static int set_disk_faulty(struct mddev
*mddev
, dev_t dev
)
6064 struct md_rdev
*rdev
;
6066 if (mddev
->pers
== NULL
)
6069 rdev
= find_rdev(mddev
, dev
);
6073 md_error(mddev
, rdev
);
6074 if (!test_bit(Faulty
, &rdev
->flags
))
6080 * We have a problem here : there is no easy way to give a CHS
6081 * virtual geometry. We currently pretend that we have a 2 heads
6082 * 4 sectors (with a BIG number of cylinders...). This drives
6083 * dosfs just mad... ;-)
6085 static int md_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
6087 struct mddev
*mddev
= bdev
->bd_disk
->private_data
;
6091 geo
->cylinders
= mddev
->array_sectors
/ 8;
6095 static int md_ioctl(struct block_device
*bdev
, fmode_t mode
,
6096 unsigned int cmd
, unsigned long arg
)
6099 void __user
*argp
= (void __user
*)arg
;
6100 struct mddev
*mddev
= NULL
;
6105 case GET_ARRAY_INFO
:
6109 if (!capable(CAP_SYS_ADMIN
))
6114 * Commands dealing with the RAID driver but not any
6120 err
= get_version(argp
);
6123 case PRINT_RAID_DEBUG
:
6131 autostart_arrays(arg
);
6138 * Commands creating/starting a new array:
6141 mddev
= bdev
->bd_disk
->private_data
;
6148 err
= mddev_lock(mddev
);
6151 "md: ioctl lock interrupted, reason %d, cmd %d\n",
6158 case SET_ARRAY_INFO
:
6160 mdu_array_info_t info
;
6162 memset(&info
, 0, sizeof(info
));
6163 else if (copy_from_user(&info
, argp
, sizeof(info
))) {
6168 err
= update_array_info(mddev
, &info
);
6170 printk(KERN_WARNING
"md: couldn't update"
6171 " array info. %d\n", err
);
6176 if (!list_empty(&mddev
->disks
)) {
6178 "md: array %s already has disks!\n",
6183 if (mddev
->raid_disks
) {
6185 "md: array %s already initialised!\n",
6190 err
= set_array_info(mddev
, &info
);
6192 printk(KERN_WARNING
"md: couldn't set"
6193 " array info. %d\n", err
);
6203 * Commands querying/configuring an existing array:
6205 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
6206 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
6207 if ((!mddev
->raid_disks
&& !mddev
->external
)
6208 && cmd
!= ADD_NEW_DISK
&& cmd
!= STOP_ARRAY
6209 && cmd
!= RUN_ARRAY
&& cmd
!= SET_BITMAP_FILE
6210 && cmd
!= GET_BITMAP_FILE
) {
6216 * Commands even a read-only array can execute:
6220 case GET_ARRAY_INFO
:
6221 err
= get_array_info(mddev
, argp
);
6224 case GET_BITMAP_FILE
:
6225 err
= get_bitmap_file(mddev
, argp
);
6229 err
= get_disk_info(mddev
, argp
);
6232 case RESTART_ARRAY_RW
:
6233 err
= restart_array(mddev
);
6237 err
= do_md_stop(mddev
, 0, 1);
6241 err
= md_set_readonly(mddev
, 1);
6245 if (get_user(ro
, (int __user
*)(arg
))) {
6251 /* if the bdev is going readonly the value of mddev->ro
6252 * does not matter, no writes are coming
6257 /* are we are already prepared for writes? */
6261 /* transitioning to readauto need only happen for
6262 * arrays that call md_write_start
6265 err
= restart_array(mddev
);
6268 set_disk_ro(mddev
->gendisk
, 0);
6275 * The remaining ioctls are changing the state of the
6276 * superblock, so we do not allow them on read-only arrays.
6277 * However non-MD ioctls (e.g. get-size) will still come through
6278 * here and hit the 'default' below, so only disallow
6279 * 'md' ioctls, and switch to rw mode if started auto-readonly.
6281 if (_IOC_TYPE(cmd
) == MD_MAJOR
&& mddev
->ro
&& mddev
->pers
) {
6282 if (mddev
->ro
== 2) {
6284 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
6285 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6286 md_wakeup_thread(mddev
->thread
);
6297 mdu_disk_info_t info
;
6298 if (copy_from_user(&info
, argp
, sizeof(info
)))
6301 err
= add_new_disk(mddev
, &info
);
6305 case HOT_REMOVE_DISK
:
6306 err
= hot_remove_disk(mddev
, new_decode_dev(arg
));
6310 err
= hot_add_disk(mddev
, new_decode_dev(arg
));
6313 case SET_DISK_FAULTY
:
6314 err
= set_disk_faulty(mddev
, new_decode_dev(arg
));
6318 err
= do_md_run(mddev
);
6321 case SET_BITMAP_FILE
:
6322 err
= set_bitmap_file(mddev
, (int)arg
);
6332 if (mddev
->hold_active
== UNTIL_IOCTL
&&
6334 mddev
->hold_active
= 0;
6335 mddev_unlock(mddev
);
6344 #ifdef CONFIG_COMPAT
6345 static int md_compat_ioctl(struct block_device
*bdev
, fmode_t mode
,
6346 unsigned int cmd
, unsigned long arg
)
6349 case HOT_REMOVE_DISK
:
6351 case SET_DISK_FAULTY
:
6352 case SET_BITMAP_FILE
:
6353 /* These take in integer arg, do not convert */
6356 arg
= (unsigned long)compat_ptr(arg
);
6360 return md_ioctl(bdev
, mode
, cmd
, arg
);
6362 #endif /* CONFIG_COMPAT */
6364 static int md_open(struct block_device
*bdev
, fmode_t mode
)
6367 * Succeed if we can lock the mddev, which confirms that
6368 * it isn't being stopped right now.
6370 struct mddev
*mddev
= mddev_find(bdev
->bd_dev
);
6373 if (mddev
->gendisk
!= bdev
->bd_disk
) {
6374 /* we are racing with mddev_put which is discarding this
6378 /* Wait until bdev->bd_disk is definitely gone */
6379 flush_workqueue(md_misc_wq
);
6380 /* Then retry the open from the top */
6381 return -ERESTARTSYS
;
6383 BUG_ON(mddev
!= bdev
->bd_disk
->private_data
);
6385 if ((err
= mutex_lock_interruptible(&mddev
->open_mutex
)))
6389 atomic_inc(&mddev
->openers
);
6390 mutex_unlock(&mddev
->open_mutex
);
6392 check_disk_change(bdev
);
6397 static int md_release(struct gendisk
*disk
, fmode_t mode
)
6399 struct mddev
*mddev
= disk
->private_data
;
6402 atomic_dec(&mddev
->openers
);
6408 static int md_media_changed(struct gendisk
*disk
)
6410 struct mddev
*mddev
= disk
->private_data
;
6412 return mddev
->changed
;
6415 static int md_revalidate(struct gendisk
*disk
)
6417 struct mddev
*mddev
= disk
->private_data
;
6422 static const struct block_device_operations md_fops
=
6424 .owner
= THIS_MODULE
,
6426 .release
= md_release
,
6428 #ifdef CONFIG_COMPAT
6429 .compat_ioctl
= md_compat_ioctl
,
6431 .getgeo
= md_getgeo
,
6432 .media_changed
= md_media_changed
,
6433 .revalidate_disk
= md_revalidate
,
6436 static int md_thread(void * arg
)
6438 struct md_thread
*thread
= arg
;
6441 * md_thread is a 'system-thread', it's priority should be very
6442 * high. We avoid resource deadlocks individually in each
6443 * raid personality. (RAID5 does preallocation) We also use RR and
6444 * the very same RT priority as kswapd, thus we will never get
6445 * into a priority inversion deadlock.
6447 * we definitely have to have equal or higher priority than
6448 * bdflush, otherwise bdflush will deadlock if there are too
6449 * many dirty RAID5 blocks.
6452 allow_signal(SIGKILL
);
6453 while (!kthread_should_stop()) {
6455 /* We need to wait INTERRUPTIBLE so that
6456 * we don't add to the load-average.
6457 * That means we need to be sure no signals are
6460 if (signal_pending(current
))
6461 flush_signals(current
);
6463 wait_event_interruptible_timeout
6465 test_bit(THREAD_WAKEUP
, &thread
->flags
)
6466 || kthread_should_stop(),
6469 clear_bit(THREAD_WAKEUP
, &thread
->flags
);
6470 if (!kthread_should_stop())
6471 thread
->run(thread
->mddev
);
6477 void md_wakeup_thread(struct md_thread
*thread
)
6480 pr_debug("md: waking up MD thread %s.\n", thread
->tsk
->comm
);
6481 set_bit(THREAD_WAKEUP
, &thread
->flags
);
6482 wake_up(&thread
->wqueue
);
6486 struct md_thread
*md_register_thread(void (*run
) (struct mddev
*), struct mddev
*mddev
,
6489 struct md_thread
*thread
;
6491 thread
= kzalloc(sizeof(struct md_thread
), GFP_KERNEL
);
6495 init_waitqueue_head(&thread
->wqueue
);
6498 thread
->mddev
= mddev
;
6499 thread
->timeout
= MAX_SCHEDULE_TIMEOUT
;
6500 thread
->tsk
= kthread_run(md_thread
, thread
,
6502 mdname(thread
->mddev
),
6503 name
?: mddev
->pers
->name
);
6504 if (IS_ERR(thread
->tsk
)) {
6511 void md_unregister_thread(struct md_thread
**threadp
)
6513 struct md_thread
*thread
= *threadp
;
6516 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread
->tsk
));
6517 /* Locking ensures that mddev_unlock does not wake_up a
6518 * non-existent thread
6520 spin_lock(&pers_lock
);
6522 spin_unlock(&pers_lock
);
6524 kthread_stop(thread
->tsk
);
6528 void md_error(struct mddev
*mddev
, struct md_rdev
*rdev
)
6535 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
))
6538 if (!mddev
->pers
|| !mddev
->pers
->error_handler
)
6540 mddev
->pers
->error_handler(mddev
,rdev
);
6541 if (mddev
->degraded
)
6542 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
6543 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
6544 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6545 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6546 md_wakeup_thread(mddev
->thread
);
6547 if (mddev
->event_work
.func
)
6548 queue_work(md_misc_wq
, &mddev
->event_work
);
6549 md_new_event_inintr(mddev
);
6552 /* seq_file implementation /proc/mdstat */
6554 static void status_unused(struct seq_file
*seq
)
6557 struct md_rdev
*rdev
;
6559 seq_printf(seq
, "unused devices: ");
6561 list_for_each_entry(rdev
, &pending_raid_disks
, same_set
) {
6562 char b
[BDEVNAME_SIZE
];
6564 seq_printf(seq
, "%s ",
6565 bdevname(rdev
->bdev
,b
));
6568 seq_printf(seq
, "<none>");
6570 seq_printf(seq
, "\n");
6574 static void status_resync(struct seq_file
*seq
, struct mddev
* mddev
)
6576 sector_t max_sectors
, resync
, res
;
6577 unsigned long dt
, db
;
6580 unsigned int per_milli
;
6582 resync
= mddev
->curr_resync
- atomic_read(&mddev
->recovery_active
);
6584 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
6585 max_sectors
= mddev
->resync_max_sectors
;
6587 max_sectors
= mddev
->dev_sectors
;
6590 * Should not happen.
6596 /* Pick 'scale' such that (resync>>scale)*1000 will fit
6597 * in a sector_t, and (max_sectors>>scale) will fit in a
6598 * u32, as those are the requirements for sector_div.
6599 * Thus 'scale' must be at least 10
6602 if (sizeof(sector_t
) > sizeof(unsigned long)) {
6603 while ( max_sectors
/2 > (1ULL<<(scale
+32)))
6606 res
= (resync
>>scale
)*1000;
6607 sector_div(res
, (u32
)((max_sectors
>>scale
)+1));
6611 int i
, x
= per_milli
/50, y
= 20-x
;
6612 seq_printf(seq
, "[");
6613 for (i
= 0; i
< x
; i
++)
6614 seq_printf(seq
, "=");
6615 seq_printf(seq
, ">");
6616 for (i
= 0; i
< y
; i
++)
6617 seq_printf(seq
, ".");
6618 seq_printf(seq
, "] ");
6620 seq_printf(seq
, " %s =%3u.%u%% (%llu/%llu)",
6621 (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
)?
6623 (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
)?
6625 (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ?
6626 "resync" : "recovery"))),
6627 per_milli
/10, per_milli
% 10,
6628 (unsigned long long) resync
/2,
6629 (unsigned long long) max_sectors
/2);
6632 * dt: time from mark until now
6633 * db: blocks written from mark until now
6634 * rt: remaining time
6636 * rt is a sector_t, so could be 32bit or 64bit.
6637 * So we divide before multiply in case it is 32bit and close
6639 * We scale the divisor (db) by 32 to avoid losing precision
6640 * near the end of resync when the number of remaining sectors
6642 * We then divide rt by 32 after multiplying by db to compensate.
6643 * The '+1' avoids division by zero if db is very small.
6645 dt
= ((jiffies
- mddev
->resync_mark
) / HZ
);
6647 db
= (mddev
->curr_mark_cnt
- atomic_read(&mddev
->recovery_active
))
6648 - mddev
->resync_mark_cnt
;
6650 rt
= max_sectors
- resync
; /* number of remaining sectors */
6651 sector_div(rt
, db
/32+1);
6655 seq_printf(seq
, " finish=%lu.%lumin", (unsigned long)rt
/ 60,
6656 ((unsigned long)rt
% 60)/6);
6658 seq_printf(seq
, " speed=%ldK/sec", db
/2/dt
);
6661 static void *md_seq_start(struct seq_file
*seq
, loff_t
*pos
)
6663 struct list_head
*tmp
;
6665 struct mddev
*mddev
;
6673 spin_lock(&all_mddevs_lock
);
6674 list_for_each(tmp
,&all_mddevs
)
6676 mddev
= list_entry(tmp
, struct mddev
, all_mddevs
);
6678 spin_unlock(&all_mddevs_lock
);
6681 spin_unlock(&all_mddevs_lock
);
6683 return (void*)2;/* tail */
6687 static void *md_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
6689 struct list_head
*tmp
;
6690 struct mddev
*next_mddev
, *mddev
= v
;
6696 spin_lock(&all_mddevs_lock
);
6698 tmp
= all_mddevs
.next
;
6700 tmp
= mddev
->all_mddevs
.next
;
6701 if (tmp
!= &all_mddevs
)
6702 next_mddev
= mddev_get(list_entry(tmp
,struct mddev
,all_mddevs
));
6704 next_mddev
= (void*)2;
6707 spin_unlock(&all_mddevs_lock
);
6715 static void md_seq_stop(struct seq_file
*seq
, void *v
)
6717 struct mddev
*mddev
= v
;
6719 if (mddev
&& v
!= (void*)1 && v
!= (void*)2)
6723 static int md_seq_show(struct seq_file
*seq
, void *v
)
6725 struct mddev
*mddev
= v
;
6727 struct md_rdev
*rdev
;
6728 struct bitmap
*bitmap
;
6730 if (v
== (void*)1) {
6731 struct md_personality
*pers
;
6732 seq_printf(seq
, "Personalities : ");
6733 spin_lock(&pers_lock
);
6734 list_for_each_entry(pers
, &pers_list
, list
)
6735 seq_printf(seq
, "[%s] ", pers
->name
);
6737 spin_unlock(&pers_lock
);
6738 seq_printf(seq
, "\n");
6739 seq
->poll_event
= atomic_read(&md_event_count
);
6742 if (v
== (void*)2) {
6747 if (mddev_lock(mddev
) < 0)
6750 if (mddev
->pers
|| mddev
->raid_disks
|| !list_empty(&mddev
->disks
)) {
6751 seq_printf(seq
, "%s : %sactive", mdname(mddev
),
6752 mddev
->pers
? "" : "in");
6755 seq_printf(seq
, " (read-only)");
6757 seq_printf(seq
, " (auto-read-only)");
6758 seq_printf(seq
, " %s", mddev
->pers
->name
);
6762 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
6763 char b
[BDEVNAME_SIZE
];
6764 seq_printf(seq
, " %s[%d]",
6765 bdevname(rdev
->bdev
,b
), rdev
->desc_nr
);
6766 if (test_bit(WriteMostly
, &rdev
->flags
))
6767 seq_printf(seq
, "(W)");
6768 if (test_bit(Faulty
, &rdev
->flags
)) {
6769 seq_printf(seq
, "(F)");
6772 if (rdev
->raid_disk
< 0)
6773 seq_printf(seq
, "(S)"); /* spare */
6774 if (test_bit(Replacement
, &rdev
->flags
))
6775 seq_printf(seq
, "(R)");
6776 sectors
+= rdev
->sectors
;
6779 if (!list_empty(&mddev
->disks
)) {
6781 seq_printf(seq
, "\n %llu blocks",
6782 (unsigned long long)
6783 mddev
->array_sectors
/ 2);
6785 seq_printf(seq
, "\n %llu blocks",
6786 (unsigned long long)sectors
/ 2);
6788 if (mddev
->persistent
) {
6789 if (mddev
->major_version
!= 0 ||
6790 mddev
->minor_version
!= 90) {
6791 seq_printf(seq
," super %d.%d",
6792 mddev
->major_version
,
6793 mddev
->minor_version
);
6795 } else if (mddev
->external
)
6796 seq_printf(seq
, " super external:%s",
6797 mddev
->metadata_type
);
6799 seq_printf(seq
, " super non-persistent");
6802 mddev
->pers
->status(seq
, mddev
);
6803 seq_printf(seq
, "\n ");
6804 if (mddev
->pers
->sync_request
) {
6805 if (mddev
->curr_resync
> 2) {
6806 status_resync(seq
, mddev
);
6807 seq_printf(seq
, "\n ");
6808 } else if (mddev
->curr_resync
== 1 || mddev
->curr_resync
== 2)
6809 seq_printf(seq
, "\tresync=DELAYED\n ");
6810 else if (mddev
->recovery_cp
< MaxSector
)
6811 seq_printf(seq
, "\tresync=PENDING\n ");
6814 seq_printf(seq
, "\n ");
6816 if ((bitmap
= mddev
->bitmap
)) {
6817 unsigned long chunk_kb
;
6818 unsigned long flags
;
6819 spin_lock_irqsave(&bitmap
->lock
, flags
);
6820 chunk_kb
= mddev
->bitmap_info
.chunksize
>> 10;
6821 seq_printf(seq
, "bitmap: %lu/%lu pages [%luKB], "
6823 bitmap
->pages
- bitmap
->missing_pages
,
6825 (bitmap
->pages
- bitmap
->missing_pages
)
6826 << (PAGE_SHIFT
- 10),
6827 chunk_kb
? chunk_kb
: mddev
->bitmap_info
.chunksize
,
6828 chunk_kb
? "KB" : "B");
6830 seq_printf(seq
, ", file: ");
6831 seq_path(seq
, &bitmap
->file
->f_path
, " \t\n");
6834 seq_printf(seq
, "\n");
6835 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
6838 seq_printf(seq
, "\n");
6840 mddev_unlock(mddev
);
6845 static const struct seq_operations md_seq_ops
= {
6846 .start
= md_seq_start
,
6847 .next
= md_seq_next
,
6848 .stop
= md_seq_stop
,
6849 .show
= md_seq_show
,
6852 static int md_seq_open(struct inode
*inode
, struct file
*file
)
6854 struct seq_file
*seq
;
6857 error
= seq_open(file
, &md_seq_ops
);
6861 seq
= file
->private_data
;
6862 seq
->poll_event
= atomic_read(&md_event_count
);
6866 static unsigned int mdstat_poll(struct file
*filp
, poll_table
*wait
)
6868 struct seq_file
*seq
= filp
->private_data
;
6871 poll_wait(filp
, &md_event_waiters
, wait
);
6873 /* always allow read */
6874 mask
= POLLIN
| POLLRDNORM
;
6876 if (seq
->poll_event
!= atomic_read(&md_event_count
))
6877 mask
|= POLLERR
| POLLPRI
;
6881 static const struct file_operations md_seq_fops
= {
6882 .owner
= THIS_MODULE
,
6883 .open
= md_seq_open
,
6885 .llseek
= seq_lseek
,
6886 .release
= seq_release_private
,
6887 .poll
= mdstat_poll
,
6890 int register_md_personality(struct md_personality
*p
)
6892 spin_lock(&pers_lock
);
6893 list_add_tail(&p
->list
, &pers_list
);
6894 printk(KERN_INFO
"md: %s personality registered for level %d\n", p
->name
, p
->level
);
6895 spin_unlock(&pers_lock
);
6899 int unregister_md_personality(struct md_personality
*p
)
6901 printk(KERN_INFO
"md: %s personality unregistered\n", p
->name
);
6902 spin_lock(&pers_lock
);
6903 list_del_init(&p
->list
);
6904 spin_unlock(&pers_lock
);
6908 static int is_mddev_idle(struct mddev
*mddev
, int init
)
6910 struct md_rdev
* rdev
;
6916 rdev_for_each_rcu(rdev
, mddev
) {
6917 struct gendisk
*disk
= rdev
->bdev
->bd_contains
->bd_disk
;
6918 curr_events
= (int)part_stat_read(&disk
->part0
, sectors
[0]) +
6919 (int)part_stat_read(&disk
->part0
, sectors
[1]) -
6920 atomic_read(&disk
->sync_io
);
6921 /* sync IO will cause sync_io to increase before the disk_stats
6922 * as sync_io is counted when a request starts, and
6923 * disk_stats is counted when it completes.
6924 * So resync activity will cause curr_events to be smaller than
6925 * when there was no such activity.
6926 * non-sync IO will cause disk_stat to increase without
6927 * increasing sync_io so curr_events will (eventually)
6928 * be larger than it was before. Once it becomes
6929 * substantially larger, the test below will cause
6930 * the array to appear non-idle, and resync will slow
6932 * If there is a lot of outstanding resync activity when
6933 * we set last_event to curr_events, then all that activity
6934 * completing might cause the array to appear non-idle
6935 * and resync will be slowed down even though there might
6936 * not have been non-resync activity. This will only
6937 * happen once though. 'last_events' will soon reflect
6938 * the state where there is little or no outstanding
6939 * resync requests, and further resync activity will
6940 * always make curr_events less than last_events.
6943 if (init
|| curr_events
- rdev
->last_events
> 64) {
6944 rdev
->last_events
= curr_events
;
6952 void md_done_sync(struct mddev
*mddev
, int blocks
, int ok
)
6954 /* another "blocks" (512byte) blocks have been synced */
6955 atomic_sub(blocks
, &mddev
->recovery_active
);
6956 wake_up(&mddev
->recovery_wait
);
6958 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6959 md_wakeup_thread(mddev
->thread
);
6960 // stop recovery, signal do_sync ....
6965 /* md_write_start(mddev, bi)
6966 * If we need to update some array metadata (e.g. 'active' flag
6967 * in superblock) before writing, schedule a superblock update
6968 * and wait for it to complete.
6970 void md_write_start(struct mddev
*mddev
, struct bio
*bi
)
6973 if (bio_data_dir(bi
) != WRITE
)
6976 BUG_ON(mddev
->ro
== 1);
6977 if (mddev
->ro
== 2) {
6978 /* need to switch to read/write */
6980 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6981 md_wakeup_thread(mddev
->thread
);
6982 md_wakeup_thread(mddev
->sync_thread
);
6985 atomic_inc(&mddev
->writes_pending
);
6986 if (mddev
->safemode
== 1)
6987 mddev
->safemode
= 0;
6988 if (mddev
->in_sync
) {
6989 spin_lock_irq(&mddev
->write_lock
);
6990 if (mddev
->in_sync
) {
6992 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
6993 set_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
6994 md_wakeup_thread(mddev
->thread
);
6997 spin_unlock_irq(&mddev
->write_lock
);
7000 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
7001 wait_event(mddev
->sb_wait
,
7002 !test_bit(MD_CHANGE_PENDING
, &mddev
->flags
));
7005 void md_write_end(struct mddev
*mddev
)
7007 if (atomic_dec_and_test(&mddev
->writes_pending
)) {
7008 if (mddev
->safemode
== 2)
7009 md_wakeup_thread(mddev
->thread
);
7010 else if (mddev
->safemode_delay
)
7011 mod_timer(&mddev
->safemode_timer
, jiffies
+ mddev
->safemode_delay
);
7015 /* md_allow_write(mddev)
7016 * Calling this ensures that the array is marked 'active' so that writes
7017 * may proceed without blocking. It is important to call this before
7018 * attempting a GFP_KERNEL allocation while holding the mddev lock.
7019 * Must be called with mddev_lock held.
7021 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
7022 * is dropped, so return -EAGAIN after notifying userspace.
7024 int md_allow_write(struct mddev
*mddev
)
7030 if (!mddev
->pers
->sync_request
)
7033 spin_lock_irq(&mddev
->write_lock
);
7034 if (mddev
->in_sync
) {
7036 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
7037 set_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
7038 if (mddev
->safemode_delay
&&
7039 mddev
->safemode
== 0)
7040 mddev
->safemode
= 1;
7041 spin_unlock_irq(&mddev
->write_lock
);
7042 md_update_sb(mddev
, 0);
7043 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
7045 spin_unlock_irq(&mddev
->write_lock
);
7047 if (test_bit(MD_CHANGE_PENDING
, &mddev
->flags
))
7052 EXPORT_SYMBOL_GPL(md_allow_write
);
7054 #define SYNC_MARKS 10
7055 #define SYNC_MARK_STEP (3*HZ)
7056 void md_do_sync(struct mddev
*mddev
)
7058 struct mddev
*mddev2
;
7059 unsigned int currspeed
= 0,
7061 sector_t max_sectors
,j
, io_sectors
;
7062 unsigned long mark
[SYNC_MARKS
];
7063 sector_t mark_cnt
[SYNC_MARKS
];
7065 struct list_head
*tmp
;
7066 sector_t last_check
;
7068 struct md_rdev
*rdev
;
7071 /* just incase thread restarts... */
7072 if (test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
))
7074 if (mddev
->ro
) /* never try to sync a read-only array */
7077 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
7078 if (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
))
7079 desc
= "data-check";
7080 else if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
7081 desc
= "requested-resync";
7084 } else if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
7089 /* we overload curr_resync somewhat here.
7090 * 0 == not engaged in resync at all
7091 * 2 == checking that there is no conflict with another sync
7092 * 1 == like 2, but have yielded to allow conflicting resync to
7094 * other == active in resync - this many blocks
7096 * Before starting a resync we must have set curr_resync to
7097 * 2, and then checked that every "conflicting" array has curr_resync
7098 * less than ours. When we find one that is the same or higher
7099 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
7100 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
7101 * This will mean we have to start checking from the beginning again.
7106 mddev
->curr_resync
= 2;
7109 if (kthread_should_stop())
7110 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
7112 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
7114 for_each_mddev(mddev2
, tmp
) {
7115 if (mddev2
== mddev
)
7117 if (!mddev
->parallel_resync
7118 && mddev2
->curr_resync
7119 && match_mddev_units(mddev
, mddev2
)) {
7121 if (mddev
< mddev2
&& mddev
->curr_resync
== 2) {
7122 /* arbitrarily yield */
7123 mddev
->curr_resync
= 1;
7124 wake_up(&resync_wait
);
7126 if (mddev
> mddev2
&& mddev
->curr_resync
== 1)
7127 /* no need to wait here, we can wait the next
7128 * time 'round when curr_resync == 2
7131 /* We need to wait 'interruptible' so as not to
7132 * contribute to the load average, and not to
7133 * be caught by 'softlockup'
7135 prepare_to_wait(&resync_wait
, &wq
, TASK_INTERRUPTIBLE
);
7136 if (!kthread_should_stop() &&
7137 mddev2
->curr_resync
>= mddev
->curr_resync
) {
7138 printk(KERN_INFO
"md: delaying %s of %s"
7139 " until %s has finished (they"
7140 " share one or more physical units)\n",
7141 desc
, mdname(mddev
), mdname(mddev2
));
7143 if (signal_pending(current
))
7144 flush_signals(current
);
7146 finish_wait(&resync_wait
, &wq
);
7149 finish_wait(&resync_wait
, &wq
);
7152 } while (mddev
->curr_resync
< 2);
7155 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
7156 /* resync follows the size requested by the personality,
7157 * which defaults to physical size, but can be virtual size
7159 max_sectors
= mddev
->resync_max_sectors
;
7160 mddev
->resync_mismatches
= 0;
7161 /* we don't use the checkpoint if there's a bitmap */
7162 if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
7163 j
= mddev
->resync_min
;
7164 else if (!mddev
->bitmap
)
7165 j
= mddev
->recovery_cp
;
7167 } else if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
7168 max_sectors
= mddev
->dev_sectors
;
7170 /* recovery follows the physical size of devices */
7171 max_sectors
= mddev
->dev_sectors
;
7174 list_for_each_entry_rcu(rdev
, &mddev
->disks
, same_set
)
7175 if (rdev
->raid_disk
>= 0 &&
7176 !test_bit(Faulty
, &rdev
->flags
) &&
7177 !test_bit(In_sync
, &rdev
->flags
) &&
7178 rdev
->recovery_offset
< j
)
7179 j
= rdev
->recovery_offset
;
7183 printk(KERN_INFO
"md: %s of RAID array %s\n", desc
, mdname(mddev
));
7184 printk(KERN_INFO
"md: minimum _guaranteed_ speed:"
7185 " %d KB/sec/disk.\n", speed_min(mddev
));
7186 printk(KERN_INFO
"md: using maximum available idle IO bandwidth "
7187 "(but not more than %d KB/sec) for %s.\n",
7188 speed_max(mddev
), desc
);
7190 is_mddev_idle(mddev
, 1); /* this initializes IO event counters */
7193 for (m
= 0; m
< SYNC_MARKS
; m
++) {
7195 mark_cnt
[m
] = io_sectors
;
7198 mddev
->resync_mark
= mark
[last_mark
];
7199 mddev
->resync_mark_cnt
= mark_cnt
[last_mark
];
7202 * Tune reconstruction:
7204 window
= 32*(PAGE_SIZE
/512);
7205 printk(KERN_INFO
"md: using %dk window, over a total of %lluk.\n",
7206 window
/2, (unsigned long long)max_sectors
/2);
7208 atomic_set(&mddev
->recovery_active
, 0);
7213 "md: resuming %s of %s from checkpoint.\n",
7214 desc
, mdname(mddev
));
7215 mddev
->curr_resync
= j
;
7217 mddev
->curr_resync_completed
= j
;
7219 while (j
< max_sectors
) {
7224 if (!test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
7225 ((mddev
->curr_resync
> mddev
->curr_resync_completed
&&
7226 (mddev
->curr_resync
- mddev
->curr_resync_completed
)
7227 > (max_sectors
>> 4)) ||
7228 (j
- mddev
->curr_resync_completed
)*2
7229 >= mddev
->resync_max
- mddev
->curr_resync_completed
7231 /* time to update curr_resync_completed */
7232 wait_event(mddev
->recovery_wait
,
7233 atomic_read(&mddev
->recovery_active
) == 0);
7234 mddev
->curr_resync_completed
= j
;
7235 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
7236 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
7239 while (j
>= mddev
->resync_max
&& !kthread_should_stop()) {
7240 /* As this condition is controlled by user-space,
7241 * we can block indefinitely, so use '_interruptible'
7242 * to avoid triggering warnings.
7244 flush_signals(current
); /* just in case */
7245 wait_event_interruptible(mddev
->recovery_wait
,
7246 mddev
->resync_max
> j
7247 || kthread_should_stop());
7250 if (kthread_should_stop())
7253 sectors
= mddev
->pers
->sync_request(mddev
, j
, &skipped
,
7254 currspeed
< speed_min(mddev
));
7256 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
7260 if (!skipped
) { /* actual IO requested */
7261 io_sectors
+= sectors
;
7262 atomic_add(sectors
, &mddev
->recovery_active
);
7265 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
7269 if (j
>1) mddev
->curr_resync
= j
;
7270 mddev
->curr_mark_cnt
= io_sectors
;
7271 if (last_check
== 0)
7272 /* this is the earliest that rebuild will be
7273 * visible in /proc/mdstat
7275 md_new_event(mddev
);
7277 if (last_check
+ window
> io_sectors
|| j
== max_sectors
)
7280 last_check
= io_sectors
;
7282 if (time_after_eq(jiffies
, mark
[last_mark
] + SYNC_MARK_STEP
)) {
7284 int next
= (last_mark
+1) % SYNC_MARKS
;
7286 mddev
->resync_mark
= mark
[next
];
7287 mddev
->resync_mark_cnt
= mark_cnt
[next
];
7288 mark
[next
] = jiffies
;
7289 mark_cnt
[next
] = io_sectors
- atomic_read(&mddev
->recovery_active
);
7294 if (kthread_should_stop())
7299 * this loop exits only if either when we are slower than
7300 * the 'hard' speed limit, or the system was IO-idle for
7302 * the system might be non-idle CPU-wise, but we only care
7303 * about not overloading the IO subsystem. (things like an
7304 * e2fsck being done on the RAID array should execute fast)
7308 currspeed
= ((unsigned long)(io_sectors
-mddev
->resync_mark_cnt
))/2
7309 /((jiffies
-mddev
->resync_mark
)/HZ
+1) +1;
7311 if (currspeed
> speed_min(mddev
)) {
7312 if ((currspeed
> speed_max(mddev
)) ||
7313 !is_mddev_idle(mddev
, 0)) {
7319 printk(KERN_INFO
"md: %s: %s done.\n",mdname(mddev
), desc
);
7321 * this also signals 'finished resyncing' to md_stop
7324 wait_event(mddev
->recovery_wait
, !atomic_read(&mddev
->recovery_active
));
7326 /* tell personality that we are finished */
7327 mddev
->pers
->sync_request(mddev
, max_sectors
, &skipped
, 1);
7329 if (!test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
) &&
7330 mddev
->curr_resync
> 2) {
7331 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
7332 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
7333 if (mddev
->curr_resync
>= mddev
->recovery_cp
) {
7335 "md: checkpointing %s of %s.\n",
7336 desc
, mdname(mddev
));
7337 mddev
->recovery_cp
=
7338 mddev
->curr_resync_completed
;
7341 mddev
->recovery_cp
= MaxSector
;
7343 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
7344 mddev
->curr_resync
= MaxSector
;
7346 list_for_each_entry_rcu(rdev
, &mddev
->disks
, same_set
)
7347 if (rdev
->raid_disk
>= 0 &&
7348 mddev
->delta_disks
>= 0 &&
7349 !test_bit(Faulty
, &rdev
->flags
) &&
7350 !test_bit(In_sync
, &rdev
->flags
) &&
7351 rdev
->recovery_offset
< mddev
->curr_resync
)
7352 rdev
->recovery_offset
= mddev
->curr_resync
;
7357 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
7359 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
7360 /* We completed so min/max setting can be forgotten if used. */
7361 if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
7362 mddev
->resync_min
= 0;
7363 mddev
->resync_max
= MaxSector
;
7364 } else if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
7365 mddev
->resync_min
= mddev
->curr_resync_completed
;
7366 mddev
->curr_resync
= 0;
7367 wake_up(&resync_wait
);
7368 set_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
7369 md_wakeup_thread(mddev
->thread
);
7374 * got a signal, exit.
7377 "md: md_do_sync() got signal ... exiting\n");
7378 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
7382 EXPORT_SYMBOL_GPL(md_do_sync
);
7384 static int remove_and_add_spares(struct mddev
*mddev
)
7386 struct md_rdev
*rdev
;
7390 mddev
->curr_resync_completed
= 0;
7392 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
7393 if (rdev
->raid_disk
>= 0 &&
7394 !test_bit(Blocked
, &rdev
->flags
) &&
7395 (test_bit(Faulty
, &rdev
->flags
) ||
7396 ! test_bit(In_sync
, &rdev
->flags
)) &&
7397 atomic_read(&rdev
->nr_pending
)==0) {
7398 if (mddev
->pers
->hot_remove_disk(
7399 mddev
, rdev
) == 0) {
7400 sysfs_unlink_rdev(mddev
, rdev
);
7401 rdev
->raid_disk
= -1;
7406 sysfs_notify(&mddev
->kobj
, NULL
,
7410 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
7411 if (rdev
->raid_disk
>= 0 &&
7412 !test_bit(In_sync
, &rdev
->flags
) &&
7413 !test_bit(Faulty
, &rdev
->flags
))
7415 if (rdev
->raid_disk
< 0
7416 && !test_bit(Faulty
, &rdev
->flags
)) {
7417 rdev
->recovery_offset
= 0;
7419 hot_add_disk(mddev
, rdev
) == 0) {
7420 if (sysfs_link_rdev(mddev
, rdev
))
7421 /* failure here is OK */;
7423 md_new_event(mddev
);
7424 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
7431 static void reap_sync_thread(struct mddev
*mddev
)
7433 struct md_rdev
*rdev
;
7435 /* resync has finished, collect result */
7436 md_unregister_thread(&mddev
->sync_thread
);
7437 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
) &&
7438 !test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
)) {
7440 /* activate any spares */
7441 if (mddev
->pers
->spare_active(mddev
))
7442 sysfs_notify(&mddev
->kobj
, NULL
,
7445 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
7446 mddev
->pers
->finish_reshape
)
7447 mddev
->pers
->finish_reshape(mddev
);
7449 /* If array is no-longer degraded, then any saved_raid_disk
7450 * information must be scrapped. Also if any device is now
7451 * In_sync we must scrape the saved_raid_disk for that device
7452 * do the superblock for an incrementally recovered device
7455 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
7456 if (!mddev
->degraded
||
7457 test_bit(In_sync
, &rdev
->flags
))
7458 rdev
->saved_raid_disk
= -1;
7460 md_update_sb(mddev
, 1);
7461 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
7462 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
7463 clear_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
7464 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
7465 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
7466 /* flag recovery needed just to double check */
7467 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7468 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
7469 md_new_event(mddev
);
7470 if (mddev
->event_work
.func
)
7471 queue_work(md_misc_wq
, &mddev
->event_work
);
7475 * This routine is regularly called by all per-raid-array threads to
7476 * deal with generic issues like resync and super-block update.
7477 * Raid personalities that don't have a thread (linear/raid0) do not
7478 * need this as they never do any recovery or update the superblock.
7480 * It does not do any resync itself, but rather "forks" off other threads
7481 * to do that as needed.
7482 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
7483 * "->recovery" and create a thread at ->sync_thread.
7484 * When the thread finishes it sets MD_RECOVERY_DONE
7485 * and wakeups up this thread which will reap the thread and finish up.
7486 * This thread also removes any faulty devices (with nr_pending == 0).
7488 * The overall approach is:
7489 * 1/ if the superblock needs updating, update it.
7490 * 2/ If a recovery thread is running, don't do anything else.
7491 * 3/ If recovery has finished, clean up, possibly marking spares active.
7492 * 4/ If there are any faulty devices, remove them.
7493 * 5/ If array is degraded, try to add spares devices
7494 * 6/ If array has spares or is not in-sync, start a resync thread.
7496 void md_check_recovery(struct mddev
*mddev
)
7498 if (mddev
->suspended
)
7502 bitmap_daemon_work(mddev
);
7504 if (signal_pending(current
)) {
7505 if (mddev
->pers
->sync_request
&& !mddev
->external
) {
7506 printk(KERN_INFO
"md: %s in immediate safe mode\n",
7508 mddev
->safemode
= 2;
7510 flush_signals(current
);
7513 if (mddev
->ro
&& !test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))
7516 (mddev
->flags
& ~ (1<<MD_CHANGE_PENDING
)) ||
7517 test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
) ||
7518 test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
) ||
7519 (mddev
->external
== 0 && mddev
->safemode
== 1) ||
7520 (mddev
->safemode
== 2 && ! atomic_read(&mddev
->writes_pending
)
7521 && !mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
)
7525 if (mddev_trylock(mddev
)) {
7529 /* Only thing we do on a ro array is remove
7532 struct md_rdev
*rdev
;
7533 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
7534 if (rdev
->raid_disk
>= 0 &&
7535 !test_bit(Blocked
, &rdev
->flags
) &&
7536 test_bit(Faulty
, &rdev
->flags
) &&
7537 atomic_read(&rdev
->nr_pending
)==0) {
7538 if (mddev
->pers
->hot_remove_disk(
7539 mddev
, rdev
) == 0) {
7540 sysfs_unlink_rdev(mddev
, rdev
);
7541 rdev
->raid_disk
= -1;
7544 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7548 if (!mddev
->external
) {
7550 spin_lock_irq(&mddev
->write_lock
);
7551 if (mddev
->safemode
&&
7552 !atomic_read(&mddev
->writes_pending
) &&
7554 mddev
->recovery_cp
== MaxSector
) {
7557 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
7559 if (mddev
->safemode
== 1)
7560 mddev
->safemode
= 0;
7561 spin_unlock_irq(&mddev
->write_lock
);
7563 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
7567 md_update_sb(mddev
, 0);
7569 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) &&
7570 !test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
)) {
7571 /* resync/recovery still happening */
7572 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7575 if (mddev
->sync_thread
) {
7576 reap_sync_thread(mddev
);
7579 /* Set RUNNING before clearing NEEDED to avoid
7580 * any transients in the value of "sync_action".
7582 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
7583 /* Clear some bits that don't mean anything, but
7586 clear_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
7587 clear_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
7589 if (!test_and_clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
) ||
7590 test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
))
7592 /* no recovery is running.
7593 * remove any failed drives, then
7594 * add spares if possible.
7595 * Spare are also removed and re-added, to allow
7596 * the personality to fail the re-add.
7599 if (mddev
->reshape_position
!= MaxSector
) {
7600 if (mddev
->pers
->check_reshape
== NULL
||
7601 mddev
->pers
->check_reshape(mddev
) != 0)
7602 /* Cannot proceed */
7604 set_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
7605 clear_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
7606 } else if ((spares
= remove_and_add_spares(mddev
))) {
7607 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
7608 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
7609 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
7610 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
7611 } else if (mddev
->recovery_cp
< MaxSector
) {
7612 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
7613 clear_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
7614 } else if (!test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
7615 /* nothing to be done ... */
7618 if (mddev
->pers
->sync_request
) {
7619 if (spares
&& mddev
->bitmap
&& ! mddev
->bitmap
->file
) {
7620 /* We are adding a device or devices to an array
7621 * which has the bitmap stored on all devices.
7622 * So make sure all bitmap pages get written
7624 bitmap_write_all(mddev
->bitmap
);
7626 mddev
->sync_thread
= md_register_thread(md_do_sync
,
7629 if (!mddev
->sync_thread
) {
7630 printk(KERN_ERR
"%s: could not start resync"
7633 /* leave the spares where they are, it shouldn't hurt */
7634 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
7635 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
7636 clear_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
7637 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
7638 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
7640 md_wakeup_thread(mddev
->sync_thread
);
7641 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
7642 md_new_event(mddev
);
7645 if (!mddev
->sync_thread
) {
7646 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
7647 if (test_and_clear_bit(MD_RECOVERY_RECOVER
,
7649 if (mddev
->sysfs_action
)
7650 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
7652 mddev_unlock(mddev
);
7656 void md_wait_for_blocked_rdev(struct md_rdev
*rdev
, struct mddev
*mddev
)
7658 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
7659 wait_event_timeout(rdev
->blocked_wait
,
7660 !test_bit(Blocked
, &rdev
->flags
) &&
7661 !test_bit(BlockedBadBlocks
, &rdev
->flags
),
7662 msecs_to_jiffies(5000));
7663 rdev_dec_pending(rdev
, mddev
);
7665 EXPORT_SYMBOL(md_wait_for_blocked_rdev
);
7668 /* Bad block management.
7669 * We can record which blocks on each device are 'bad' and so just
7670 * fail those blocks, or that stripe, rather than the whole device.
7671 * Entries in the bad-block table are 64bits wide. This comprises:
7672 * Length of bad-range, in sectors: 0-511 for lengths 1-512
7673 * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes)
7674 * A 'shift' can be set so that larger blocks are tracked and
7675 * consequently larger devices can be covered.
7676 * 'Acknowledged' flag - 1 bit. - the most significant bit.
7678 * Locking of the bad-block table uses a seqlock so md_is_badblock
7679 * might need to retry if it is very unlucky.
7680 * We will sometimes want to check for bad blocks in a bi_end_io function,
7681 * so we use the write_seqlock_irq variant.
7683 * When looking for a bad block we specify a range and want to
7684 * know if any block in the range is bad. So we binary-search
7685 * to the last range that starts at-or-before the given endpoint,
7686 * (or "before the sector after the target range")
7687 * then see if it ends after the given start.
7689 * 0 if there are no known bad blocks in the range
7690 * 1 if there are known bad block which are all acknowledged
7691 * -1 if there are bad blocks which have not yet been acknowledged in metadata.
7692 * plus the start/length of the first bad section we overlap.
7694 int md_is_badblock(struct badblocks
*bb
, sector_t s
, int sectors
,
7695 sector_t
*first_bad
, int *bad_sectors
)
7701 sector_t target
= s
+ sectors
;
7704 if (bb
->shift
> 0) {
7705 /* round the start down, and the end up */
7707 target
+= (1<<bb
->shift
) - 1;
7708 target
>>= bb
->shift
;
7709 sectors
= target
- s
;
7711 /* 'target' is now the first block after the bad range */
7714 seq
= read_seqbegin(&bb
->lock
);
7718 /* Binary search between lo and hi for 'target'
7719 * i.e. for the last range that starts before 'target'
7721 /* INVARIANT: ranges before 'lo' and at-or-after 'hi'
7722 * are known not to be the last range before target.
7723 * VARIANT: hi-lo is the number of possible
7724 * ranges, and decreases until it reaches 1
7726 while (hi
- lo
> 1) {
7727 int mid
= (lo
+ hi
) / 2;
7728 sector_t a
= BB_OFFSET(p
[mid
]);
7730 /* This could still be the one, earlier ranges
7734 /* This and later ranges are definitely out. */
7737 /* 'lo' might be the last that started before target, but 'hi' isn't */
7739 /* need to check all range that end after 's' to see if
7740 * any are unacknowledged.
7743 BB_OFFSET(p
[lo
]) + BB_LEN(p
[lo
]) > s
) {
7744 if (BB_OFFSET(p
[lo
]) < target
) {
7745 /* starts before the end, and finishes after
7746 * the start, so they must overlap
7748 if (rv
!= -1 && BB_ACK(p
[lo
]))
7752 *first_bad
= BB_OFFSET(p
[lo
]);
7753 *bad_sectors
= BB_LEN(p
[lo
]);
7759 if (read_seqretry(&bb
->lock
, seq
))
7764 EXPORT_SYMBOL_GPL(md_is_badblock
);
7767 * Add a range of bad blocks to the table.
7768 * This might extend the table, or might contract it
7769 * if two adjacent ranges can be merged.
7770 * We binary-search to find the 'insertion' point, then
7771 * decide how best to handle it.
7773 static int md_set_badblocks(struct badblocks
*bb
, sector_t s
, int sectors
,
7781 /* badblocks are disabled */
7785 /* round the start down, and the end up */
7786 sector_t next
= s
+ sectors
;
7788 next
+= (1<<bb
->shift
) - 1;
7793 write_seqlock_irq(&bb
->lock
);
7798 /* Find the last range that starts at-or-before 's' */
7799 while (hi
- lo
> 1) {
7800 int mid
= (lo
+ hi
) / 2;
7801 sector_t a
= BB_OFFSET(p
[mid
]);
7807 if (hi
> lo
&& BB_OFFSET(p
[lo
]) > s
)
7811 /* we found a range that might merge with the start
7814 sector_t a
= BB_OFFSET(p
[lo
]);
7815 sector_t e
= a
+ BB_LEN(p
[lo
]);
7816 int ack
= BB_ACK(p
[lo
]);
7818 /* Yes, we can merge with a previous range */
7819 if (s
== a
&& s
+ sectors
>= e
)
7820 /* new range covers old */
7823 ack
= ack
&& acknowledged
;
7825 if (e
< s
+ sectors
)
7827 if (e
- a
<= BB_MAX_LEN
) {
7828 p
[lo
] = BB_MAKE(a
, e
-a
, ack
);
7831 /* does not all fit in one range,
7832 * make p[lo] maximal
7834 if (BB_LEN(p
[lo
]) != BB_MAX_LEN
)
7835 p
[lo
] = BB_MAKE(a
, BB_MAX_LEN
, ack
);
7841 if (sectors
&& hi
< bb
->count
) {
7842 /* 'hi' points to the first range that starts after 's'.
7843 * Maybe we can merge with the start of that range */
7844 sector_t a
= BB_OFFSET(p
[hi
]);
7845 sector_t e
= a
+ BB_LEN(p
[hi
]);
7846 int ack
= BB_ACK(p
[hi
]);
7847 if (a
<= s
+ sectors
) {
7848 /* merging is possible */
7849 if (e
<= s
+ sectors
) {
7854 ack
= ack
&& acknowledged
;
7857 if (e
- a
<= BB_MAX_LEN
) {
7858 p
[hi
] = BB_MAKE(a
, e
-a
, ack
);
7861 p
[hi
] = BB_MAKE(a
, BB_MAX_LEN
, ack
);
7869 if (sectors
== 0 && hi
< bb
->count
) {
7870 /* we might be able to combine lo and hi */
7871 /* Note: 's' is at the end of 'lo' */
7872 sector_t a
= BB_OFFSET(p
[hi
]);
7873 int lolen
= BB_LEN(p
[lo
]);
7874 int hilen
= BB_LEN(p
[hi
]);
7875 int newlen
= lolen
+ hilen
- (s
- a
);
7876 if (s
>= a
&& newlen
< BB_MAX_LEN
) {
7877 /* yes, we can combine them */
7878 int ack
= BB_ACK(p
[lo
]) && BB_ACK(p
[hi
]);
7879 p
[lo
] = BB_MAKE(BB_OFFSET(p
[lo
]), newlen
, ack
);
7880 memmove(p
+ hi
, p
+ hi
+ 1,
7881 (bb
->count
- hi
- 1) * 8);
7886 /* didn't merge (it all).
7887 * Need to add a range just before 'hi' */
7888 if (bb
->count
>= MD_MAX_BADBLOCKS
) {
7889 /* No room for more */
7893 int this_sectors
= sectors
;
7894 memmove(p
+ hi
+ 1, p
+ hi
,
7895 (bb
->count
- hi
) * 8);
7898 if (this_sectors
> BB_MAX_LEN
)
7899 this_sectors
= BB_MAX_LEN
;
7900 p
[hi
] = BB_MAKE(s
, this_sectors
, acknowledged
);
7901 sectors
-= this_sectors
;
7908 bb
->unacked_exist
= 1;
7909 write_sequnlock_irq(&bb
->lock
);
7914 int rdev_set_badblocks(struct md_rdev
*rdev
, sector_t s
, int sectors
,
7917 int rv
= md_set_badblocks(&rdev
->badblocks
,
7918 s
+ rdev
->data_offset
, sectors
, acknowledged
);
7920 /* Make sure they get written out promptly */
7921 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
7922 set_bit(MD_CHANGE_CLEAN
, &rdev
->mddev
->flags
);
7923 md_wakeup_thread(rdev
->mddev
->thread
);
7927 EXPORT_SYMBOL_GPL(rdev_set_badblocks
);
7930 * Remove a range of bad blocks from the table.
7931 * This may involve extending the table if we spilt a region,
7932 * but it must not fail. So if the table becomes full, we just
7933 * drop the remove request.
7935 static int md_clear_badblocks(struct badblocks
*bb
, sector_t s
, int sectors
)
7939 sector_t target
= s
+ sectors
;
7942 if (bb
->shift
> 0) {
7943 /* When clearing we round the start up and the end down.
7944 * This should not matter as the shift should align with
7945 * the block size and no rounding should ever be needed.
7946 * However it is better the think a block is bad when it
7947 * isn't than to think a block is not bad when it is.
7949 s
+= (1<<bb
->shift
) - 1;
7951 target
>>= bb
->shift
;
7952 sectors
= target
- s
;
7955 write_seqlock_irq(&bb
->lock
);
7960 /* Find the last range that starts before 'target' */
7961 while (hi
- lo
> 1) {
7962 int mid
= (lo
+ hi
) / 2;
7963 sector_t a
= BB_OFFSET(p
[mid
]);
7970 /* p[lo] is the last range that could overlap the
7971 * current range. Earlier ranges could also overlap,
7972 * but only this one can overlap the end of the range.
7974 if (BB_OFFSET(p
[lo
]) + BB_LEN(p
[lo
]) > target
) {
7975 /* Partial overlap, leave the tail of this range */
7976 int ack
= BB_ACK(p
[lo
]);
7977 sector_t a
= BB_OFFSET(p
[lo
]);
7978 sector_t end
= a
+ BB_LEN(p
[lo
]);
7981 /* we need to split this range */
7982 if (bb
->count
>= MD_MAX_BADBLOCKS
) {
7986 memmove(p
+lo
+1, p
+lo
, (bb
->count
- lo
) * 8);
7988 p
[lo
] = BB_MAKE(a
, s
-a
, ack
);
7991 p
[lo
] = BB_MAKE(target
, end
- target
, ack
);
7992 /* there is no longer an overlap */
7997 BB_OFFSET(p
[lo
]) + BB_LEN(p
[lo
]) > s
) {
7998 /* This range does overlap */
7999 if (BB_OFFSET(p
[lo
]) < s
) {
8000 /* Keep the early parts of this range. */
8001 int ack
= BB_ACK(p
[lo
]);
8002 sector_t start
= BB_OFFSET(p
[lo
]);
8003 p
[lo
] = BB_MAKE(start
, s
- start
, ack
);
8004 /* now low doesn't overlap, so.. */
8009 /* 'lo' is strictly before, 'hi' is strictly after,
8010 * anything between needs to be discarded
8013 memmove(p
+lo
+1, p
+hi
, (bb
->count
- hi
) * 8);
8014 bb
->count
-= (hi
- lo
- 1);
8020 write_sequnlock_irq(&bb
->lock
);
8024 int rdev_clear_badblocks(struct md_rdev
*rdev
, sector_t s
, int sectors
)
8026 return md_clear_badblocks(&rdev
->badblocks
,
8027 s
+ rdev
->data_offset
,
8030 EXPORT_SYMBOL_GPL(rdev_clear_badblocks
);
8033 * Acknowledge all bad blocks in a list.
8034 * This only succeeds if ->changed is clear. It is used by
8035 * in-kernel metadata updates
8037 void md_ack_all_badblocks(struct badblocks
*bb
)
8039 if (bb
->page
== NULL
|| bb
->changed
)
8040 /* no point even trying */
8042 write_seqlock_irq(&bb
->lock
);
8044 if (bb
->changed
== 0) {
8047 for (i
= 0; i
< bb
->count
; i
++) {
8048 if (!BB_ACK(p
[i
])) {
8049 sector_t start
= BB_OFFSET(p
[i
]);
8050 int len
= BB_LEN(p
[i
]);
8051 p
[i
] = BB_MAKE(start
, len
, 1);
8054 bb
->unacked_exist
= 0;
8056 write_sequnlock_irq(&bb
->lock
);
8058 EXPORT_SYMBOL_GPL(md_ack_all_badblocks
);
8060 /* sysfs access to bad-blocks list.
8061 * We present two files.
8062 * 'bad-blocks' lists sector numbers and lengths of ranges that
8063 * are recorded as bad. The list is truncated to fit within
8064 * the one-page limit of sysfs.
8065 * Writing "sector length" to this file adds an acknowledged
8067 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
8068 * been acknowledged. Writing to this file adds bad blocks
8069 * without acknowledging them. This is largely for testing.
8073 badblocks_show(struct badblocks
*bb
, char *page
, int unack
)
8084 seq
= read_seqbegin(&bb
->lock
);
8089 while (len
< PAGE_SIZE
&& i
< bb
->count
) {
8090 sector_t s
= BB_OFFSET(p
[i
]);
8091 unsigned int length
= BB_LEN(p
[i
]);
8092 int ack
= BB_ACK(p
[i
]);
8098 len
+= snprintf(page
+len
, PAGE_SIZE
-len
, "%llu %u\n",
8099 (unsigned long long)s
<< bb
->shift
,
8100 length
<< bb
->shift
);
8102 if (unack
&& len
== 0)
8103 bb
->unacked_exist
= 0;
8105 if (read_seqretry(&bb
->lock
, seq
))
8114 badblocks_store(struct badblocks
*bb
, const char *page
, size_t len
, int unack
)
8116 unsigned long long sector
;
8120 /* Allow clearing via sysfs *only* for testing/debugging.
8121 * Normally only a successful write may clear a badblock
8124 if (page
[0] == '-') {
8128 #endif /* DO_DEBUG */
8130 switch (sscanf(page
, "%llu %d%c", §or
, &length
, &newline
)) {
8132 if (newline
!= '\n')
8144 md_clear_badblocks(bb
, sector
, length
);
8147 #endif /* DO_DEBUG */
8148 if (md_set_badblocks(bb
, sector
, length
, !unack
))
8154 static int md_notify_reboot(struct notifier_block
*this,
8155 unsigned long code
, void *x
)
8157 struct list_head
*tmp
;
8158 struct mddev
*mddev
;
8161 for_each_mddev(mddev
, tmp
) {
8162 if (mddev_trylock(mddev
)) {
8164 __md_stop_writes(mddev
);
8165 mddev
->safemode
= 2;
8166 mddev_unlock(mddev
);
8171 * certain more exotic SCSI devices are known to be
8172 * volatile wrt too early system reboots. While the
8173 * right place to handle this issue is the given
8174 * driver, we do want to have a safe RAID driver ...
8182 static struct notifier_block md_notifier
= {
8183 .notifier_call
= md_notify_reboot
,
8185 .priority
= INT_MAX
, /* before any real devices */
8188 static void md_geninit(void)
8190 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t
));
8192 proc_create("mdstat", S_IRUGO
, NULL
, &md_seq_fops
);
8195 static int __init
md_init(void)
8199 md_wq
= alloc_workqueue("md", WQ_MEM_RECLAIM
, 0);
8203 md_misc_wq
= alloc_workqueue("md_misc", 0, 0);
8207 if ((ret
= register_blkdev(MD_MAJOR
, "md")) < 0)
8210 if ((ret
= register_blkdev(0, "mdp")) < 0)
8214 blk_register_region(MKDEV(MD_MAJOR
, 0), 1UL<<MINORBITS
, THIS_MODULE
,
8215 md_probe
, NULL
, NULL
);
8216 blk_register_region(MKDEV(mdp_major
, 0), 1UL<<MINORBITS
, THIS_MODULE
,
8217 md_probe
, NULL
, NULL
);
8219 register_reboot_notifier(&md_notifier
);
8220 raid_table_header
= register_sysctl_table(raid_root_table
);
8226 unregister_blkdev(MD_MAJOR
, "md");
8228 destroy_workqueue(md_misc_wq
);
8230 destroy_workqueue(md_wq
);
8238 * Searches all registered partitions for autorun RAID arrays
8242 static LIST_HEAD(all_detected_devices
);
8243 struct detected_devices_node
{
8244 struct list_head list
;
8248 void md_autodetect_dev(dev_t dev
)
8250 struct detected_devices_node
*node_detected_dev
;
8252 node_detected_dev
= kzalloc(sizeof(*node_detected_dev
), GFP_KERNEL
);
8253 if (node_detected_dev
) {
8254 node_detected_dev
->dev
= dev
;
8255 list_add_tail(&node_detected_dev
->list
, &all_detected_devices
);
8257 printk(KERN_CRIT
"md: md_autodetect_dev: kzalloc failed"
8258 ", skipping dev(%d,%d)\n", MAJOR(dev
), MINOR(dev
));
8263 static void autostart_arrays(int part
)
8265 struct md_rdev
*rdev
;
8266 struct detected_devices_node
*node_detected_dev
;
8268 int i_scanned
, i_passed
;
8273 printk(KERN_INFO
"md: Autodetecting RAID arrays.\n");
8275 while (!list_empty(&all_detected_devices
) && i_scanned
< INT_MAX
) {
8277 node_detected_dev
= list_entry(all_detected_devices
.next
,
8278 struct detected_devices_node
, list
);
8279 list_del(&node_detected_dev
->list
);
8280 dev
= node_detected_dev
->dev
;
8281 kfree(node_detected_dev
);
8282 rdev
= md_import_device(dev
,0, 90);
8286 if (test_bit(Faulty
, &rdev
->flags
)) {
8290 set_bit(AutoDetected
, &rdev
->flags
);
8291 list_add(&rdev
->same_set
, &pending_raid_disks
);
8295 printk(KERN_INFO
"md: Scanned %d and added %d devices.\n",
8296 i_scanned
, i_passed
);
8298 autorun_devices(part
);
8301 #endif /* !MODULE */
8303 static __exit
void md_exit(void)
8305 struct mddev
*mddev
;
8306 struct list_head
*tmp
;
8308 blk_unregister_region(MKDEV(MD_MAJOR
,0), 1U << MINORBITS
);
8309 blk_unregister_region(MKDEV(mdp_major
,0), 1U << MINORBITS
);
8311 unregister_blkdev(MD_MAJOR
,"md");
8312 unregister_blkdev(mdp_major
, "mdp");
8313 unregister_reboot_notifier(&md_notifier
);
8314 unregister_sysctl_table(raid_table_header
);
8315 remove_proc_entry("mdstat", NULL
);
8316 for_each_mddev(mddev
, tmp
) {
8317 export_array(mddev
);
8318 mddev
->hold_active
= 0;
8320 destroy_workqueue(md_misc_wq
);
8321 destroy_workqueue(md_wq
);
8324 subsys_initcall(md_init
);
8325 module_exit(md_exit
)
8327 static int get_ro(char *buffer
, struct kernel_param
*kp
)
8329 return sprintf(buffer
, "%d", start_readonly
);
8331 static int set_ro(const char *val
, struct kernel_param
*kp
)
8334 int num
= simple_strtoul(val
, &e
, 10);
8335 if (*val
&& (*e
== '\0' || *e
== '\n')) {
8336 start_readonly
= num
;
8342 module_param_call(start_ro
, set_ro
, get_ro
, NULL
, S_IRUSR
|S_IWUSR
);
8343 module_param(start_dirty_degraded
, int, S_IRUGO
|S_IWUSR
);
8345 module_param_call(new_array
, add_named_array
, NULL
, NULL
, S_IWUSR
);
8347 EXPORT_SYMBOL(register_md_personality
);
8348 EXPORT_SYMBOL(unregister_md_personality
);
8349 EXPORT_SYMBOL(md_error
);
8350 EXPORT_SYMBOL(md_done_sync
);
8351 EXPORT_SYMBOL(md_write_start
);
8352 EXPORT_SYMBOL(md_write_end
);
8353 EXPORT_SYMBOL(md_register_thread
);
8354 EXPORT_SYMBOL(md_unregister_thread
);
8355 EXPORT_SYMBOL(md_wakeup_thread
);
8356 EXPORT_SYMBOL(md_check_recovery
);
8357 MODULE_LICENSE("GPL");
8358 MODULE_DESCRIPTION("MD RAID framework");
8360 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR
);