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 if (mddev
->ro
== 1 && unlikely(rw
== WRITE
)) {
348 bio_endio(bio
, bio_sectors(bio
) == 0 ? 0 : -EROFS
);
351 smp_rmb(); /* Ensure implications of 'active' are visible */
353 if (mddev
->suspended
) {
356 prepare_to_wait(&mddev
->sb_wait
, &__wait
,
357 TASK_UNINTERRUPTIBLE
);
358 if (!mddev
->suspended
)
364 finish_wait(&mddev
->sb_wait
, &__wait
);
366 atomic_inc(&mddev
->active_io
);
370 * save the sectors now since our bio can
371 * go away inside make_request
373 sectors
= bio_sectors(bio
);
374 mddev
->pers
->make_request(mddev
, bio
);
376 cpu
= part_stat_lock();
377 part_stat_inc(cpu
, &mddev
->gendisk
->part0
, ios
[rw
]);
378 part_stat_add(cpu
, &mddev
->gendisk
->part0
, sectors
[rw
], sectors
);
381 if (atomic_dec_and_test(&mddev
->active_io
) && mddev
->suspended
)
382 wake_up(&mddev
->sb_wait
);
385 /* mddev_suspend makes sure no new requests are submitted
386 * to the device, and that any requests that have been submitted
387 * are completely handled.
388 * Once ->stop is called and completes, the module will be completely
391 void mddev_suspend(struct mddev
*mddev
)
393 BUG_ON(mddev
->suspended
);
394 mddev
->suspended
= 1;
396 wait_event(mddev
->sb_wait
, atomic_read(&mddev
->active_io
) == 0);
397 mddev
->pers
->quiesce(mddev
, 1);
399 del_timer_sync(&mddev
->safemode_timer
);
401 EXPORT_SYMBOL_GPL(mddev_suspend
);
403 void mddev_resume(struct mddev
*mddev
)
405 mddev
->suspended
= 0;
406 wake_up(&mddev
->sb_wait
);
407 mddev
->pers
->quiesce(mddev
, 0);
409 md_wakeup_thread(mddev
->thread
);
410 md_wakeup_thread(mddev
->sync_thread
); /* possibly kick off a reshape */
412 EXPORT_SYMBOL_GPL(mddev_resume
);
414 int mddev_congested(struct mddev
*mddev
, int bits
)
416 return mddev
->suspended
;
418 EXPORT_SYMBOL(mddev_congested
);
421 * Generic flush handling for md
424 static void md_end_flush(struct bio
*bio
, int err
)
426 struct md_rdev
*rdev
= bio
->bi_private
;
427 struct mddev
*mddev
= rdev
->mddev
;
429 rdev_dec_pending(rdev
, mddev
);
431 if (atomic_dec_and_test(&mddev
->flush_pending
)) {
432 /* The pre-request flush has finished */
433 queue_work(md_wq
, &mddev
->flush_work
);
438 static void md_submit_flush_data(struct work_struct
*ws
);
440 static void submit_flushes(struct work_struct
*ws
)
442 struct mddev
*mddev
= container_of(ws
, struct mddev
, flush_work
);
443 struct md_rdev
*rdev
;
445 INIT_WORK(&mddev
->flush_work
, md_submit_flush_data
);
446 atomic_set(&mddev
->flush_pending
, 1);
448 rdev_for_each_rcu(rdev
, mddev
)
449 if (rdev
->raid_disk
>= 0 &&
450 !test_bit(Faulty
, &rdev
->flags
)) {
451 /* Take two references, one is dropped
452 * when request finishes, one after
453 * we reclaim rcu_read_lock
456 atomic_inc(&rdev
->nr_pending
);
457 atomic_inc(&rdev
->nr_pending
);
459 bi
= bio_alloc_mddev(GFP_NOIO
, 0, mddev
);
460 bi
->bi_end_io
= md_end_flush
;
461 bi
->bi_private
= rdev
;
462 bi
->bi_bdev
= rdev
->bdev
;
463 atomic_inc(&mddev
->flush_pending
);
464 submit_bio(WRITE_FLUSH
, bi
);
466 rdev_dec_pending(rdev
, mddev
);
469 if (atomic_dec_and_test(&mddev
->flush_pending
))
470 queue_work(md_wq
, &mddev
->flush_work
);
473 static void md_submit_flush_data(struct work_struct
*ws
)
475 struct mddev
*mddev
= container_of(ws
, struct mddev
, flush_work
);
476 struct bio
*bio
= mddev
->flush_bio
;
478 if (bio
->bi_size
== 0)
479 /* an empty barrier - all done */
482 bio
->bi_rw
&= ~REQ_FLUSH
;
483 mddev
->pers
->make_request(mddev
, bio
);
486 mddev
->flush_bio
= NULL
;
487 wake_up(&mddev
->sb_wait
);
490 void md_flush_request(struct mddev
*mddev
, struct bio
*bio
)
492 spin_lock_irq(&mddev
->write_lock
);
493 wait_event_lock_irq(mddev
->sb_wait
,
495 mddev
->write_lock
, /*nothing*/);
496 mddev
->flush_bio
= bio
;
497 spin_unlock_irq(&mddev
->write_lock
);
499 INIT_WORK(&mddev
->flush_work
, submit_flushes
);
500 queue_work(md_wq
, &mddev
->flush_work
);
502 EXPORT_SYMBOL(md_flush_request
);
504 /* Support for plugging.
505 * This mirrors the plugging support in request_queue, but does not
506 * require having a whole queue or request structures.
507 * We allocate an md_plug_cb for each md device and each thread it gets
508 * plugged on. This links tot the private plug_handle structure in the
509 * personality data where we keep a count of the number of outstanding
510 * plugs so other code can see if a plug is active.
513 struct blk_plug_cb cb
;
517 static void plugger_unplug(struct blk_plug_cb
*cb
)
519 struct md_plug_cb
*mdcb
= container_of(cb
, struct md_plug_cb
, cb
);
520 if (atomic_dec_and_test(&mdcb
->mddev
->plug_cnt
))
521 md_wakeup_thread(mdcb
->mddev
->thread
);
525 /* Check that an unplug wakeup will come shortly.
526 * If not, wakeup the md thread immediately
528 int mddev_check_plugged(struct mddev
*mddev
)
530 struct blk_plug
*plug
= current
->plug
;
531 struct md_plug_cb
*mdcb
;
536 list_for_each_entry(mdcb
, &plug
->cb_list
, cb
.list
) {
537 if (mdcb
->cb
.callback
== plugger_unplug
&&
538 mdcb
->mddev
== mddev
) {
539 /* Already on the list, move to top */
540 if (mdcb
!= list_first_entry(&plug
->cb_list
,
543 list_move(&mdcb
->cb
.list
, &plug
->cb_list
);
547 /* Not currently on the callback list */
548 mdcb
= kmalloc(sizeof(*mdcb
), GFP_ATOMIC
);
553 mdcb
->cb
.callback
= plugger_unplug
;
554 atomic_inc(&mddev
->plug_cnt
);
555 list_add(&mdcb
->cb
.list
, &plug
->cb_list
);
558 EXPORT_SYMBOL_GPL(mddev_check_plugged
);
560 static inline struct mddev
*mddev_get(struct mddev
*mddev
)
562 atomic_inc(&mddev
->active
);
566 static void mddev_delayed_delete(struct work_struct
*ws
);
568 static void mddev_put(struct mddev
*mddev
)
570 struct bio_set
*bs
= NULL
;
572 if (!atomic_dec_and_lock(&mddev
->active
, &all_mddevs_lock
))
574 if (!mddev
->raid_disks
&& list_empty(&mddev
->disks
) &&
575 mddev
->ctime
== 0 && !mddev
->hold_active
) {
576 /* Array is not configured at all, and not held active,
578 list_del_init(&mddev
->all_mddevs
);
580 mddev
->bio_set
= NULL
;
581 if (mddev
->gendisk
) {
582 /* We did a probe so need to clean up. Call
583 * queue_work inside the spinlock so that
584 * flush_workqueue() after mddev_find will
585 * succeed in waiting for the work to be done.
587 INIT_WORK(&mddev
->del_work
, mddev_delayed_delete
);
588 queue_work(md_misc_wq
, &mddev
->del_work
);
592 spin_unlock(&all_mddevs_lock
);
597 void mddev_init(struct mddev
*mddev
)
599 mutex_init(&mddev
->open_mutex
);
600 mutex_init(&mddev
->reconfig_mutex
);
601 mutex_init(&mddev
->bitmap_info
.mutex
);
602 INIT_LIST_HEAD(&mddev
->disks
);
603 INIT_LIST_HEAD(&mddev
->all_mddevs
);
604 init_timer(&mddev
->safemode_timer
);
605 atomic_set(&mddev
->active
, 1);
606 atomic_set(&mddev
->openers
, 0);
607 atomic_set(&mddev
->active_io
, 0);
608 atomic_set(&mddev
->plug_cnt
, 0);
609 spin_lock_init(&mddev
->write_lock
);
610 atomic_set(&mddev
->flush_pending
, 0);
611 init_waitqueue_head(&mddev
->sb_wait
);
612 init_waitqueue_head(&mddev
->recovery_wait
);
613 mddev
->reshape_position
= MaxSector
;
614 mddev
->resync_min
= 0;
615 mddev
->resync_max
= MaxSector
;
616 mddev
->level
= LEVEL_NONE
;
618 EXPORT_SYMBOL_GPL(mddev_init
);
620 static struct mddev
* mddev_find(dev_t unit
)
622 struct mddev
*mddev
, *new = NULL
;
624 if (unit
&& MAJOR(unit
) != MD_MAJOR
)
625 unit
&= ~((1<<MdpMinorShift
)-1);
628 spin_lock(&all_mddevs_lock
);
631 list_for_each_entry(mddev
, &all_mddevs
, all_mddevs
)
632 if (mddev
->unit
== unit
) {
634 spin_unlock(&all_mddevs_lock
);
640 list_add(&new->all_mddevs
, &all_mddevs
);
641 spin_unlock(&all_mddevs_lock
);
642 new->hold_active
= UNTIL_IOCTL
;
646 /* find an unused unit number */
647 static int next_minor
= 512;
648 int start
= next_minor
;
652 dev
= MKDEV(MD_MAJOR
, next_minor
);
654 if (next_minor
> MINORMASK
)
656 if (next_minor
== start
) {
657 /* Oh dear, all in use. */
658 spin_unlock(&all_mddevs_lock
);
664 list_for_each_entry(mddev
, &all_mddevs
, all_mddevs
)
665 if (mddev
->unit
== dev
) {
671 new->md_minor
= MINOR(dev
);
672 new->hold_active
= UNTIL_STOP
;
673 list_add(&new->all_mddevs
, &all_mddevs
);
674 spin_unlock(&all_mddevs_lock
);
677 spin_unlock(&all_mddevs_lock
);
679 new = kzalloc(sizeof(*new), GFP_KERNEL
);
684 if (MAJOR(unit
) == MD_MAJOR
)
685 new->md_minor
= MINOR(unit
);
687 new->md_minor
= MINOR(unit
) >> MdpMinorShift
;
694 static inline int mddev_lock(struct mddev
* mddev
)
696 return mutex_lock_interruptible(&mddev
->reconfig_mutex
);
699 static inline int mddev_is_locked(struct mddev
*mddev
)
701 return mutex_is_locked(&mddev
->reconfig_mutex
);
704 static inline int mddev_trylock(struct mddev
* mddev
)
706 return mutex_trylock(&mddev
->reconfig_mutex
);
709 static struct attribute_group md_redundancy_group
;
711 static void mddev_unlock(struct mddev
* mddev
)
713 if (mddev
->to_remove
) {
714 /* These cannot be removed under reconfig_mutex as
715 * an access to the files will try to take reconfig_mutex
716 * while holding the file unremovable, which leads to
718 * So hold set sysfs_active while the remove in happeing,
719 * and anything else which might set ->to_remove or my
720 * otherwise change the sysfs namespace will fail with
721 * -EBUSY if sysfs_active is still set.
722 * We set sysfs_active under reconfig_mutex and elsewhere
723 * test it under the same mutex to ensure its correct value
726 struct attribute_group
*to_remove
= mddev
->to_remove
;
727 mddev
->to_remove
= NULL
;
728 mddev
->sysfs_active
= 1;
729 mutex_unlock(&mddev
->reconfig_mutex
);
731 if (mddev
->kobj
.sd
) {
732 if (to_remove
!= &md_redundancy_group
)
733 sysfs_remove_group(&mddev
->kobj
, to_remove
);
734 if (mddev
->pers
== NULL
||
735 mddev
->pers
->sync_request
== NULL
) {
736 sysfs_remove_group(&mddev
->kobj
, &md_redundancy_group
);
737 if (mddev
->sysfs_action
)
738 sysfs_put(mddev
->sysfs_action
);
739 mddev
->sysfs_action
= NULL
;
742 mddev
->sysfs_active
= 0;
744 mutex_unlock(&mddev
->reconfig_mutex
);
746 /* As we've dropped the mutex we need a spinlock to
747 * make sure the thread doesn't disappear
749 spin_lock(&pers_lock
);
750 md_wakeup_thread(mddev
->thread
);
751 spin_unlock(&pers_lock
);
754 static struct md_rdev
* find_rdev_nr(struct mddev
*mddev
, int nr
)
756 struct md_rdev
*rdev
;
758 rdev_for_each(rdev
, mddev
)
759 if (rdev
->desc_nr
== nr
)
765 static struct md_rdev
* find_rdev(struct mddev
* mddev
, dev_t dev
)
767 struct md_rdev
*rdev
;
769 rdev_for_each(rdev
, mddev
)
770 if (rdev
->bdev
->bd_dev
== dev
)
776 static struct md_personality
*find_pers(int level
, char *clevel
)
778 struct md_personality
*pers
;
779 list_for_each_entry(pers
, &pers_list
, list
) {
780 if (level
!= LEVEL_NONE
&& pers
->level
== level
)
782 if (strcmp(pers
->name
, clevel
)==0)
788 /* return the offset of the super block in 512byte sectors */
789 static inline sector_t
calc_dev_sboffset(struct md_rdev
*rdev
)
791 sector_t num_sectors
= i_size_read(rdev
->bdev
->bd_inode
) / 512;
792 return MD_NEW_SIZE_SECTORS(num_sectors
);
795 static int alloc_disk_sb(struct md_rdev
* rdev
)
800 rdev
->sb_page
= alloc_page(GFP_KERNEL
);
801 if (!rdev
->sb_page
) {
802 printk(KERN_ALERT
"md: out of memory.\n");
809 static void free_disk_sb(struct md_rdev
* rdev
)
812 put_page(rdev
->sb_page
);
814 rdev
->sb_page
= NULL
;
819 put_page(rdev
->bb_page
);
820 rdev
->bb_page
= NULL
;
825 static void super_written(struct bio
*bio
, int error
)
827 struct md_rdev
*rdev
= bio
->bi_private
;
828 struct mddev
*mddev
= rdev
->mddev
;
830 if (error
|| !test_bit(BIO_UPTODATE
, &bio
->bi_flags
)) {
831 printk("md: super_written gets error=%d, uptodate=%d\n",
832 error
, test_bit(BIO_UPTODATE
, &bio
->bi_flags
));
833 WARN_ON(test_bit(BIO_UPTODATE
, &bio
->bi_flags
));
834 md_error(mddev
, rdev
);
837 if (atomic_dec_and_test(&mddev
->pending_writes
))
838 wake_up(&mddev
->sb_wait
);
842 void md_super_write(struct mddev
*mddev
, struct md_rdev
*rdev
,
843 sector_t sector
, int size
, struct page
*page
)
845 /* write first size bytes of page to sector of rdev
846 * Increment mddev->pending_writes before returning
847 * and decrement it on completion, waking up sb_wait
848 * if zero is reached.
849 * If an error occurred, call md_error
851 struct bio
*bio
= bio_alloc_mddev(GFP_NOIO
, 1, mddev
);
853 bio
->bi_bdev
= rdev
->meta_bdev
? rdev
->meta_bdev
: rdev
->bdev
;
854 bio
->bi_sector
= sector
;
855 bio_add_page(bio
, page
, size
, 0);
856 bio
->bi_private
= rdev
;
857 bio
->bi_end_io
= super_written
;
859 atomic_inc(&mddev
->pending_writes
);
860 submit_bio(WRITE_FLUSH_FUA
, bio
);
863 void md_super_wait(struct mddev
*mddev
)
865 /* wait for all superblock writes that were scheduled to complete */
868 prepare_to_wait(&mddev
->sb_wait
, &wq
, TASK_UNINTERRUPTIBLE
);
869 if (atomic_read(&mddev
->pending_writes
)==0)
873 finish_wait(&mddev
->sb_wait
, &wq
);
876 static void bi_complete(struct bio
*bio
, int error
)
878 complete((struct completion
*)bio
->bi_private
);
881 int sync_page_io(struct md_rdev
*rdev
, sector_t sector
, int size
,
882 struct page
*page
, int rw
, bool metadata_op
)
884 struct bio
*bio
= bio_alloc_mddev(GFP_NOIO
, 1, rdev
->mddev
);
885 struct completion event
;
890 bio
->bi_bdev
= (metadata_op
&& rdev
->meta_bdev
) ?
891 rdev
->meta_bdev
: rdev
->bdev
;
893 bio
->bi_sector
= sector
+ rdev
->sb_start
;
895 bio
->bi_sector
= sector
+ rdev
->data_offset
;
896 bio_add_page(bio
, page
, size
, 0);
897 init_completion(&event
);
898 bio
->bi_private
= &event
;
899 bio
->bi_end_io
= bi_complete
;
901 wait_for_completion(&event
);
903 ret
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
907 EXPORT_SYMBOL_GPL(sync_page_io
);
909 static int read_disk_sb(struct md_rdev
* rdev
, int size
)
911 char b
[BDEVNAME_SIZE
];
912 if (!rdev
->sb_page
) {
920 if (!sync_page_io(rdev
, 0, size
, rdev
->sb_page
, READ
, true))
926 printk(KERN_WARNING
"md: disabled device %s, could not read superblock.\n",
927 bdevname(rdev
->bdev
,b
));
931 static int uuid_equal(mdp_super_t
*sb1
, mdp_super_t
*sb2
)
933 return sb1
->set_uuid0
== sb2
->set_uuid0
&&
934 sb1
->set_uuid1
== sb2
->set_uuid1
&&
935 sb1
->set_uuid2
== sb2
->set_uuid2
&&
936 sb1
->set_uuid3
== sb2
->set_uuid3
;
939 static int sb_equal(mdp_super_t
*sb1
, mdp_super_t
*sb2
)
942 mdp_super_t
*tmp1
, *tmp2
;
944 tmp1
= kmalloc(sizeof(*tmp1
),GFP_KERNEL
);
945 tmp2
= kmalloc(sizeof(*tmp2
),GFP_KERNEL
);
947 if (!tmp1
|| !tmp2
) {
949 printk(KERN_INFO
"md.c sb_equal(): failed to allocate memory!\n");
957 * nr_disks is not constant
962 ret
= (memcmp(tmp1
, tmp2
, MD_SB_GENERIC_CONSTANT_WORDS
* 4) == 0);
970 static u32
md_csum_fold(u32 csum
)
972 csum
= (csum
& 0xffff) + (csum
>> 16);
973 return (csum
& 0xffff) + (csum
>> 16);
976 static unsigned int calc_sb_csum(mdp_super_t
* sb
)
979 u32
*sb32
= (u32
*)sb
;
981 unsigned int disk_csum
, csum
;
983 disk_csum
= sb
->sb_csum
;
986 for (i
= 0; i
< MD_SB_BYTES
/4 ; i
++)
988 csum
= (newcsum
& 0xffffffff) + (newcsum
>>32);
992 /* This used to use csum_partial, which was wrong for several
993 * reasons including that different results are returned on
994 * different architectures. It isn't critical that we get exactly
995 * the same return value as before (we always csum_fold before
996 * testing, and that removes any differences). However as we
997 * know that csum_partial always returned a 16bit value on
998 * alphas, do a fold to maximise conformity to previous behaviour.
1000 sb
->sb_csum
= md_csum_fold(disk_csum
);
1002 sb
->sb_csum
= disk_csum
;
1009 * Handle superblock details.
1010 * We want to be able to handle multiple superblock formats
1011 * so we have a common interface to them all, and an array of
1012 * different handlers.
1013 * We rely on user-space to write the initial superblock, and support
1014 * reading and updating of superblocks.
1015 * Interface methods are:
1016 * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
1017 * loads and validates a superblock on dev.
1018 * if refdev != NULL, compare superblocks on both devices
1020 * 0 - dev has a superblock that is compatible with refdev
1021 * 1 - dev has a superblock that is compatible and newer than refdev
1022 * so dev should be used as the refdev in future
1023 * -EINVAL superblock incompatible or invalid
1024 * -othererror e.g. -EIO
1026 * int validate_super(struct mddev *mddev, struct md_rdev *dev)
1027 * Verify that dev is acceptable into mddev.
1028 * The first time, mddev->raid_disks will be 0, and data from
1029 * dev should be merged in. Subsequent calls check that dev
1030 * is new enough. Return 0 or -EINVAL
1032 * void sync_super(struct mddev *mddev, struct md_rdev *dev)
1033 * Update the superblock for rdev with data in mddev
1034 * This does not write to disc.
1040 struct module
*owner
;
1041 int (*load_super
)(struct md_rdev
*rdev
, struct md_rdev
*refdev
,
1043 int (*validate_super
)(struct mddev
*mddev
, struct md_rdev
*rdev
);
1044 void (*sync_super
)(struct mddev
*mddev
, struct md_rdev
*rdev
);
1045 unsigned long long (*rdev_size_change
)(struct md_rdev
*rdev
,
1046 sector_t num_sectors
);
1050 * Check that the given mddev has no bitmap.
1052 * This function is called from the run method of all personalities that do not
1053 * support bitmaps. It prints an error message and returns non-zero if mddev
1054 * has a bitmap. Otherwise, it returns 0.
1057 int md_check_no_bitmap(struct mddev
*mddev
)
1059 if (!mddev
->bitmap_info
.file
&& !mddev
->bitmap_info
.offset
)
1061 printk(KERN_ERR
"%s: bitmaps are not supported for %s\n",
1062 mdname(mddev
), mddev
->pers
->name
);
1065 EXPORT_SYMBOL(md_check_no_bitmap
);
1068 * load_super for 0.90.0
1070 static int super_90_load(struct md_rdev
*rdev
, struct md_rdev
*refdev
, int minor_version
)
1072 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
1077 * Calculate the position of the superblock (512byte sectors),
1078 * it's at the end of the disk.
1080 * It also happens to be a multiple of 4Kb.
1082 rdev
->sb_start
= calc_dev_sboffset(rdev
);
1084 ret
= read_disk_sb(rdev
, MD_SB_BYTES
);
1085 if (ret
) return ret
;
1089 bdevname(rdev
->bdev
, b
);
1090 sb
= page_address(rdev
->sb_page
);
1092 if (sb
->md_magic
!= MD_SB_MAGIC
) {
1093 printk(KERN_ERR
"md: invalid raid superblock magic on %s\n",
1098 if (sb
->major_version
!= 0 ||
1099 sb
->minor_version
< 90 ||
1100 sb
->minor_version
> 91) {
1101 printk(KERN_WARNING
"Bad version number %d.%d on %s\n",
1102 sb
->major_version
, sb
->minor_version
,
1107 if (sb
->raid_disks
<= 0)
1110 if (md_csum_fold(calc_sb_csum(sb
)) != md_csum_fold(sb
->sb_csum
)) {
1111 printk(KERN_WARNING
"md: invalid superblock checksum on %s\n",
1116 rdev
->preferred_minor
= sb
->md_minor
;
1117 rdev
->data_offset
= 0;
1118 rdev
->sb_size
= MD_SB_BYTES
;
1119 rdev
->badblocks
.shift
= -1;
1121 if (sb
->level
== LEVEL_MULTIPATH
)
1124 rdev
->desc_nr
= sb
->this_disk
.number
;
1130 mdp_super_t
*refsb
= page_address(refdev
->sb_page
);
1131 if (!uuid_equal(refsb
, sb
)) {
1132 printk(KERN_WARNING
"md: %s has different UUID to %s\n",
1133 b
, bdevname(refdev
->bdev
,b2
));
1136 if (!sb_equal(refsb
, sb
)) {
1137 printk(KERN_WARNING
"md: %s has same UUID"
1138 " but different superblock to %s\n",
1139 b
, bdevname(refdev
->bdev
, b2
));
1143 ev2
= md_event(refsb
);
1149 rdev
->sectors
= rdev
->sb_start
;
1150 /* Limit to 4TB as metadata cannot record more than that.
1151 * (not needed for Linear and RAID0 as metadata doesn't
1154 if (rdev
->sectors
>= (2ULL << 32) && sb
->level
>= 1)
1155 rdev
->sectors
= (2ULL << 32) - 2;
1157 if (rdev
->sectors
< ((sector_t
)sb
->size
) * 2 && sb
->level
>= 1)
1158 /* "this cannot possibly happen" ... */
1166 * validate_super for 0.90.0
1168 static int super_90_validate(struct mddev
*mddev
, struct md_rdev
*rdev
)
1171 mdp_super_t
*sb
= page_address(rdev
->sb_page
);
1172 __u64 ev1
= md_event(sb
);
1174 rdev
->raid_disk
= -1;
1175 clear_bit(Faulty
, &rdev
->flags
);
1176 clear_bit(In_sync
, &rdev
->flags
);
1177 clear_bit(WriteMostly
, &rdev
->flags
);
1179 if (mddev
->raid_disks
== 0) {
1180 mddev
->major_version
= 0;
1181 mddev
->minor_version
= sb
->minor_version
;
1182 mddev
->patch_version
= sb
->patch_version
;
1183 mddev
->external
= 0;
1184 mddev
->chunk_sectors
= sb
->chunk_size
>> 9;
1185 mddev
->ctime
= sb
->ctime
;
1186 mddev
->utime
= sb
->utime
;
1187 mddev
->level
= sb
->level
;
1188 mddev
->clevel
[0] = 0;
1189 mddev
->layout
= sb
->layout
;
1190 mddev
->raid_disks
= sb
->raid_disks
;
1191 mddev
->dev_sectors
= ((sector_t
)sb
->size
) * 2;
1192 mddev
->events
= ev1
;
1193 mddev
->bitmap_info
.offset
= 0;
1194 mddev
->bitmap_info
.default_offset
= MD_SB_BYTES
>> 9;
1196 if (mddev
->minor_version
>= 91) {
1197 mddev
->reshape_position
= sb
->reshape_position
;
1198 mddev
->delta_disks
= sb
->delta_disks
;
1199 mddev
->new_level
= sb
->new_level
;
1200 mddev
->new_layout
= sb
->new_layout
;
1201 mddev
->new_chunk_sectors
= sb
->new_chunk
>> 9;
1203 mddev
->reshape_position
= MaxSector
;
1204 mddev
->delta_disks
= 0;
1205 mddev
->new_level
= mddev
->level
;
1206 mddev
->new_layout
= mddev
->layout
;
1207 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
1210 if (sb
->state
& (1<<MD_SB_CLEAN
))
1211 mddev
->recovery_cp
= MaxSector
;
1213 if (sb
->events_hi
== sb
->cp_events_hi
&&
1214 sb
->events_lo
== sb
->cp_events_lo
) {
1215 mddev
->recovery_cp
= sb
->recovery_cp
;
1217 mddev
->recovery_cp
= 0;
1220 memcpy(mddev
->uuid
+0, &sb
->set_uuid0
, 4);
1221 memcpy(mddev
->uuid
+4, &sb
->set_uuid1
, 4);
1222 memcpy(mddev
->uuid
+8, &sb
->set_uuid2
, 4);
1223 memcpy(mddev
->uuid
+12,&sb
->set_uuid3
, 4);
1225 mddev
->max_disks
= MD_SB_DISKS
;
1227 if (sb
->state
& (1<<MD_SB_BITMAP_PRESENT
) &&
1228 mddev
->bitmap_info
.file
== NULL
)
1229 mddev
->bitmap_info
.offset
=
1230 mddev
->bitmap_info
.default_offset
;
1232 } else if (mddev
->pers
== NULL
) {
1233 /* Insist on good event counter while assembling, except
1234 * for spares (which don't need an event count) */
1236 if (sb
->disks
[rdev
->desc_nr
].state
& (
1237 (1<<MD_DISK_SYNC
) | (1 << MD_DISK_ACTIVE
)))
1238 if (ev1
< mddev
->events
)
1240 } else if (mddev
->bitmap
) {
1241 /* if adding to array with a bitmap, then we can accept an
1242 * older device ... but not too old.
1244 if (ev1
< mddev
->bitmap
->events_cleared
)
1247 if (ev1
< mddev
->events
)
1248 /* just a hot-add of a new device, leave raid_disk at -1 */
1252 if (mddev
->level
!= LEVEL_MULTIPATH
) {
1253 desc
= sb
->disks
+ rdev
->desc_nr
;
1255 if (desc
->state
& (1<<MD_DISK_FAULTY
))
1256 set_bit(Faulty
, &rdev
->flags
);
1257 else if (desc
->state
& (1<<MD_DISK_SYNC
) /* &&
1258 desc->raid_disk < mddev->raid_disks */) {
1259 set_bit(In_sync
, &rdev
->flags
);
1260 rdev
->raid_disk
= desc
->raid_disk
;
1261 } else if (desc
->state
& (1<<MD_DISK_ACTIVE
)) {
1262 /* active but not in sync implies recovery up to
1263 * reshape position. We don't know exactly where
1264 * that is, so set to zero for now */
1265 if (mddev
->minor_version
>= 91) {
1266 rdev
->recovery_offset
= 0;
1267 rdev
->raid_disk
= desc
->raid_disk
;
1270 if (desc
->state
& (1<<MD_DISK_WRITEMOSTLY
))
1271 set_bit(WriteMostly
, &rdev
->flags
);
1272 } else /* MULTIPATH are always insync */
1273 set_bit(In_sync
, &rdev
->flags
);
1278 * sync_super for 0.90.0
1280 static void super_90_sync(struct mddev
*mddev
, struct md_rdev
*rdev
)
1283 struct md_rdev
*rdev2
;
1284 int next_spare
= mddev
->raid_disks
;
1287 /* make rdev->sb match mddev data..
1290 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1291 * 3/ any empty disks < next_spare become removed
1293 * disks[0] gets initialised to REMOVED because
1294 * we cannot be sure from other fields if it has
1295 * been initialised or not.
1298 int active
=0, working
=0,failed
=0,spare
=0,nr_disks
=0;
1300 rdev
->sb_size
= MD_SB_BYTES
;
1302 sb
= page_address(rdev
->sb_page
);
1304 memset(sb
, 0, sizeof(*sb
));
1306 sb
->md_magic
= MD_SB_MAGIC
;
1307 sb
->major_version
= mddev
->major_version
;
1308 sb
->patch_version
= mddev
->patch_version
;
1309 sb
->gvalid_words
= 0; /* ignored */
1310 memcpy(&sb
->set_uuid0
, mddev
->uuid
+0, 4);
1311 memcpy(&sb
->set_uuid1
, mddev
->uuid
+4, 4);
1312 memcpy(&sb
->set_uuid2
, mddev
->uuid
+8, 4);
1313 memcpy(&sb
->set_uuid3
, mddev
->uuid
+12,4);
1315 sb
->ctime
= mddev
->ctime
;
1316 sb
->level
= mddev
->level
;
1317 sb
->size
= mddev
->dev_sectors
/ 2;
1318 sb
->raid_disks
= mddev
->raid_disks
;
1319 sb
->md_minor
= mddev
->md_minor
;
1320 sb
->not_persistent
= 0;
1321 sb
->utime
= mddev
->utime
;
1323 sb
->events_hi
= (mddev
->events
>>32);
1324 sb
->events_lo
= (u32
)mddev
->events
;
1326 if (mddev
->reshape_position
== MaxSector
)
1327 sb
->minor_version
= 90;
1329 sb
->minor_version
= 91;
1330 sb
->reshape_position
= mddev
->reshape_position
;
1331 sb
->new_level
= mddev
->new_level
;
1332 sb
->delta_disks
= mddev
->delta_disks
;
1333 sb
->new_layout
= mddev
->new_layout
;
1334 sb
->new_chunk
= mddev
->new_chunk_sectors
<< 9;
1336 mddev
->minor_version
= sb
->minor_version
;
1339 sb
->recovery_cp
= mddev
->recovery_cp
;
1340 sb
->cp_events_hi
= (mddev
->events
>>32);
1341 sb
->cp_events_lo
= (u32
)mddev
->events
;
1342 if (mddev
->recovery_cp
== MaxSector
)
1343 sb
->state
= (1<< MD_SB_CLEAN
);
1345 sb
->recovery_cp
= 0;
1347 sb
->layout
= mddev
->layout
;
1348 sb
->chunk_size
= mddev
->chunk_sectors
<< 9;
1350 if (mddev
->bitmap
&& mddev
->bitmap_info
.file
== NULL
)
1351 sb
->state
|= (1<<MD_SB_BITMAP_PRESENT
);
1353 sb
->disks
[0].state
= (1<<MD_DISK_REMOVED
);
1354 rdev_for_each(rdev2
, mddev
) {
1357 int is_active
= test_bit(In_sync
, &rdev2
->flags
);
1359 if (rdev2
->raid_disk
>= 0 &&
1360 sb
->minor_version
>= 91)
1361 /* we have nowhere to store the recovery_offset,
1362 * but if it is not below the reshape_position,
1363 * we can piggy-back on that.
1366 if (rdev2
->raid_disk
< 0 ||
1367 test_bit(Faulty
, &rdev2
->flags
))
1370 desc_nr
= rdev2
->raid_disk
;
1372 desc_nr
= next_spare
++;
1373 rdev2
->desc_nr
= desc_nr
;
1374 d
= &sb
->disks
[rdev2
->desc_nr
];
1376 d
->number
= rdev2
->desc_nr
;
1377 d
->major
= MAJOR(rdev2
->bdev
->bd_dev
);
1378 d
->minor
= MINOR(rdev2
->bdev
->bd_dev
);
1380 d
->raid_disk
= rdev2
->raid_disk
;
1382 d
->raid_disk
= rdev2
->desc_nr
; /* compatibility */
1383 if (test_bit(Faulty
, &rdev2
->flags
))
1384 d
->state
= (1<<MD_DISK_FAULTY
);
1385 else if (is_active
) {
1386 d
->state
= (1<<MD_DISK_ACTIVE
);
1387 if (test_bit(In_sync
, &rdev2
->flags
))
1388 d
->state
|= (1<<MD_DISK_SYNC
);
1396 if (test_bit(WriteMostly
, &rdev2
->flags
))
1397 d
->state
|= (1<<MD_DISK_WRITEMOSTLY
);
1399 /* now set the "removed" and "faulty" bits on any missing devices */
1400 for (i
=0 ; i
< mddev
->raid_disks
; i
++) {
1401 mdp_disk_t
*d
= &sb
->disks
[i
];
1402 if (d
->state
== 0 && d
->number
== 0) {
1405 d
->state
= (1<<MD_DISK_REMOVED
);
1406 d
->state
|= (1<<MD_DISK_FAULTY
);
1410 sb
->nr_disks
= nr_disks
;
1411 sb
->active_disks
= active
;
1412 sb
->working_disks
= working
;
1413 sb
->failed_disks
= failed
;
1414 sb
->spare_disks
= spare
;
1416 sb
->this_disk
= sb
->disks
[rdev
->desc_nr
];
1417 sb
->sb_csum
= calc_sb_csum(sb
);
1421 * rdev_size_change for 0.90.0
1423 static unsigned long long
1424 super_90_rdev_size_change(struct md_rdev
*rdev
, sector_t num_sectors
)
1426 if (num_sectors
&& num_sectors
< rdev
->mddev
->dev_sectors
)
1427 return 0; /* component must fit device */
1428 if (rdev
->mddev
->bitmap_info
.offset
)
1429 return 0; /* can't move bitmap */
1430 rdev
->sb_start
= calc_dev_sboffset(rdev
);
1431 if (!num_sectors
|| num_sectors
> rdev
->sb_start
)
1432 num_sectors
= rdev
->sb_start
;
1433 /* Limit to 4TB as metadata cannot record more than that.
1434 * 4TB == 2^32 KB, or 2*2^32 sectors.
1436 if (num_sectors
>= (2ULL << 32) && rdev
->mddev
->level
>= 1)
1437 num_sectors
= (2ULL << 32) - 2;
1438 md_super_write(rdev
->mddev
, rdev
, rdev
->sb_start
, rdev
->sb_size
,
1440 md_super_wait(rdev
->mddev
);
1446 * version 1 superblock
1449 static __le32
calc_sb_1_csum(struct mdp_superblock_1
* sb
)
1453 unsigned long long newcsum
;
1454 int size
= 256 + le32_to_cpu(sb
->max_dev
)*2;
1455 __le32
*isuper
= (__le32
*)sb
;
1458 disk_csum
= sb
->sb_csum
;
1461 for (i
=0; size
>=4; size
-= 4 )
1462 newcsum
+= le32_to_cpu(*isuper
++);
1465 newcsum
+= le16_to_cpu(*(__le16
*) isuper
);
1467 csum
= (newcsum
& 0xffffffff) + (newcsum
>> 32);
1468 sb
->sb_csum
= disk_csum
;
1469 return cpu_to_le32(csum
);
1472 static int md_set_badblocks(struct badblocks
*bb
, sector_t s
, int sectors
,
1474 static int super_1_load(struct md_rdev
*rdev
, struct md_rdev
*refdev
, int minor_version
)
1476 struct mdp_superblock_1
*sb
;
1479 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
1483 * Calculate the position of the superblock in 512byte sectors.
1484 * It is always aligned to a 4K boundary and
1485 * depeding on minor_version, it can be:
1486 * 0: At least 8K, but less than 12K, from end of device
1487 * 1: At start of device
1488 * 2: 4K from start of device.
1490 switch(minor_version
) {
1492 sb_start
= i_size_read(rdev
->bdev
->bd_inode
) >> 9;
1494 sb_start
&= ~(sector_t
)(4*2-1);
1505 rdev
->sb_start
= sb_start
;
1507 /* superblock is rarely larger than 1K, but it can be larger,
1508 * and it is safe to read 4k, so we do that
1510 ret
= read_disk_sb(rdev
, 4096);
1511 if (ret
) return ret
;
1514 sb
= page_address(rdev
->sb_page
);
1516 if (sb
->magic
!= cpu_to_le32(MD_SB_MAGIC
) ||
1517 sb
->major_version
!= cpu_to_le32(1) ||
1518 le32_to_cpu(sb
->max_dev
) > (4096-256)/2 ||
1519 le64_to_cpu(sb
->super_offset
) != rdev
->sb_start
||
1520 (le32_to_cpu(sb
->feature_map
) & ~MD_FEATURE_ALL
) != 0)
1523 if (calc_sb_1_csum(sb
) != sb
->sb_csum
) {
1524 printk("md: invalid superblock checksum on %s\n",
1525 bdevname(rdev
->bdev
,b
));
1528 if (le64_to_cpu(sb
->data_size
) < 10) {
1529 printk("md: data_size too small on %s\n",
1530 bdevname(rdev
->bdev
,b
));
1534 rdev
->preferred_minor
= 0xffff;
1535 rdev
->data_offset
= le64_to_cpu(sb
->data_offset
);
1536 atomic_set(&rdev
->corrected_errors
, le32_to_cpu(sb
->cnt_corrected_read
));
1538 rdev
->sb_size
= le32_to_cpu(sb
->max_dev
) * 2 + 256;
1539 bmask
= queue_logical_block_size(rdev
->bdev
->bd_disk
->queue
)-1;
1540 if (rdev
->sb_size
& bmask
)
1541 rdev
->sb_size
= (rdev
->sb_size
| bmask
) + 1;
1544 && rdev
->data_offset
< sb_start
+ (rdev
->sb_size
/512))
1547 if (sb
->level
== cpu_to_le32(LEVEL_MULTIPATH
))
1550 rdev
->desc_nr
= le32_to_cpu(sb
->dev_number
);
1552 if (!rdev
->bb_page
) {
1553 rdev
->bb_page
= alloc_page(GFP_KERNEL
);
1557 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_BAD_BLOCKS
) &&
1558 rdev
->badblocks
.count
== 0) {
1559 /* need to load the bad block list.
1560 * Currently we limit it to one page.
1566 int sectors
= le16_to_cpu(sb
->bblog_size
);
1567 if (sectors
> (PAGE_SIZE
/ 512))
1569 offset
= le32_to_cpu(sb
->bblog_offset
);
1572 bb_sector
= (long long)offset
;
1573 if (!sync_page_io(rdev
, bb_sector
, sectors
<< 9,
1574 rdev
->bb_page
, READ
, true))
1576 bbp
= (u64
*)page_address(rdev
->bb_page
);
1577 rdev
->badblocks
.shift
= sb
->bblog_shift
;
1578 for (i
= 0 ; i
< (sectors
<< (9-3)) ; i
++, bbp
++) {
1579 u64 bb
= le64_to_cpu(*bbp
);
1580 int count
= bb
& (0x3ff);
1581 u64 sector
= bb
>> 10;
1582 sector
<<= sb
->bblog_shift
;
1583 count
<<= sb
->bblog_shift
;
1586 if (md_set_badblocks(&rdev
->badblocks
,
1587 sector
, count
, 1) == 0)
1590 } else if (sb
->bblog_offset
!= 0)
1591 rdev
->badblocks
.shift
= 0;
1597 struct mdp_superblock_1
*refsb
= page_address(refdev
->sb_page
);
1599 if (memcmp(sb
->set_uuid
, refsb
->set_uuid
, 16) != 0 ||
1600 sb
->level
!= refsb
->level
||
1601 sb
->layout
!= refsb
->layout
||
1602 sb
->chunksize
!= refsb
->chunksize
) {
1603 printk(KERN_WARNING
"md: %s has strangely different"
1604 " superblock to %s\n",
1605 bdevname(rdev
->bdev
,b
),
1606 bdevname(refdev
->bdev
,b2
));
1609 ev1
= le64_to_cpu(sb
->events
);
1610 ev2
= le64_to_cpu(refsb
->events
);
1618 rdev
->sectors
= (i_size_read(rdev
->bdev
->bd_inode
) >> 9) -
1619 le64_to_cpu(sb
->data_offset
);
1621 rdev
->sectors
= rdev
->sb_start
;
1622 if (rdev
->sectors
< le64_to_cpu(sb
->data_size
))
1624 rdev
->sectors
= le64_to_cpu(sb
->data_size
);
1625 if (le64_to_cpu(sb
->size
) > rdev
->sectors
)
1630 static int super_1_validate(struct mddev
*mddev
, struct md_rdev
*rdev
)
1632 struct mdp_superblock_1
*sb
= page_address(rdev
->sb_page
);
1633 __u64 ev1
= le64_to_cpu(sb
->events
);
1635 rdev
->raid_disk
= -1;
1636 clear_bit(Faulty
, &rdev
->flags
);
1637 clear_bit(In_sync
, &rdev
->flags
);
1638 clear_bit(WriteMostly
, &rdev
->flags
);
1640 if (mddev
->raid_disks
== 0) {
1641 mddev
->major_version
= 1;
1642 mddev
->patch_version
= 0;
1643 mddev
->external
= 0;
1644 mddev
->chunk_sectors
= le32_to_cpu(sb
->chunksize
);
1645 mddev
->ctime
= le64_to_cpu(sb
->ctime
) & ((1ULL << 32)-1);
1646 mddev
->utime
= le64_to_cpu(sb
->utime
) & ((1ULL << 32)-1);
1647 mddev
->level
= le32_to_cpu(sb
->level
);
1648 mddev
->clevel
[0] = 0;
1649 mddev
->layout
= le32_to_cpu(sb
->layout
);
1650 mddev
->raid_disks
= le32_to_cpu(sb
->raid_disks
);
1651 mddev
->dev_sectors
= le64_to_cpu(sb
->size
);
1652 mddev
->events
= ev1
;
1653 mddev
->bitmap_info
.offset
= 0;
1654 mddev
->bitmap_info
.default_offset
= 1024 >> 9;
1656 mddev
->recovery_cp
= le64_to_cpu(sb
->resync_offset
);
1657 memcpy(mddev
->uuid
, sb
->set_uuid
, 16);
1659 mddev
->max_disks
= (4096-256)/2;
1661 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_BITMAP_OFFSET
) &&
1662 mddev
->bitmap_info
.file
== NULL
)
1663 mddev
->bitmap_info
.offset
=
1664 (__s32
)le32_to_cpu(sb
->bitmap_offset
);
1666 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RESHAPE_ACTIVE
)) {
1667 mddev
->reshape_position
= le64_to_cpu(sb
->reshape_position
);
1668 mddev
->delta_disks
= le32_to_cpu(sb
->delta_disks
);
1669 mddev
->new_level
= le32_to_cpu(sb
->new_level
);
1670 mddev
->new_layout
= le32_to_cpu(sb
->new_layout
);
1671 mddev
->new_chunk_sectors
= le32_to_cpu(sb
->new_chunk
);
1673 mddev
->reshape_position
= MaxSector
;
1674 mddev
->delta_disks
= 0;
1675 mddev
->new_level
= mddev
->level
;
1676 mddev
->new_layout
= mddev
->layout
;
1677 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
1680 } else if (mddev
->pers
== NULL
) {
1681 /* Insist of good event counter while assembling, except for
1682 * spares (which don't need an event count) */
1684 if (rdev
->desc_nr
>= 0 &&
1685 rdev
->desc_nr
< le32_to_cpu(sb
->max_dev
) &&
1686 le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]) < 0xfffe)
1687 if (ev1
< mddev
->events
)
1689 } else if (mddev
->bitmap
) {
1690 /* If adding to array with a bitmap, then we can accept an
1691 * older device, but not too old.
1693 if (ev1
< mddev
->bitmap
->events_cleared
)
1696 if (ev1
< mddev
->events
)
1697 /* just a hot-add of a new device, leave raid_disk at -1 */
1700 if (mddev
->level
!= LEVEL_MULTIPATH
) {
1702 if (rdev
->desc_nr
< 0 ||
1703 rdev
->desc_nr
>= le32_to_cpu(sb
->max_dev
)) {
1707 role
= le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]);
1709 case 0xffff: /* spare */
1711 case 0xfffe: /* faulty */
1712 set_bit(Faulty
, &rdev
->flags
);
1715 if ((le32_to_cpu(sb
->feature_map
) &
1716 MD_FEATURE_RECOVERY_OFFSET
))
1717 rdev
->recovery_offset
= le64_to_cpu(sb
->recovery_offset
);
1719 set_bit(In_sync
, &rdev
->flags
);
1720 rdev
->raid_disk
= role
;
1723 if (sb
->devflags
& WriteMostly1
)
1724 set_bit(WriteMostly
, &rdev
->flags
);
1725 if (le32_to_cpu(sb
->feature_map
) & MD_FEATURE_REPLACEMENT
)
1726 set_bit(Replacement
, &rdev
->flags
);
1727 } else /* MULTIPATH are always insync */
1728 set_bit(In_sync
, &rdev
->flags
);
1733 static void super_1_sync(struct mddev
*mddev
, struct md_rdev
*rdev
)
1735 struct mdp_superblock_1
*sb
;
1736 struct md_rdev
*rdev2
;
1738 /* make rdev->sb match mddev and rdev data. */
1740 sb
= page_address(rdev
->sb_page
);
1742 sb
->feature_map
= 0;
1744 sb
->recovery_offset
= cpu_to_le64(0);
1745 memset(sb
->pad1
, 0, sizeof(sb
->pad1
));
1746 memset(sb
->pad3
, 0, sizeof(sb
->pad3
));
1748 sb
->utime
= cpu_to_le64((__u64
)mddev
->utime
);
1749 sb
->events
= cpu_to_le64(mddev
->events
);
1751 sb
->resync_offset
= cpu_to_le64(mddev
->recovery_cp
);
1753 sb
->resync_offset
= cpu_to_le64(0);
1755 sb
->cnt_corrected_read
= cpu_to_le32(atomic_read(&rdev
->corrected_errors
));
1757 sb
->raid_disks
= cpu_to_le32(mddev
->raid_disks
);
1758 sb
->size
= cpu_to_le64(mddev
->dev_sectors
);
1759 sb
->chunksize
= cpu_to_le32(mddev
->chunk_sectors
);
1760 sb
->level
= cpu_to_le32(mddev
->level
);
1761 sb
->layout
= cpu_to_le32(mddev
->layout
);
1763 if (test_bit(WriteMostly
, &rdev
->flags
))
1764 sb
->devflags
|= WriteMostly1
;
1766 sb
->devflags
&= ~WriteMostly1
;
1768 if (mddev
->bitmap
&& mddev
->bitmap_info
.file
== NULL
) {
1769 sb
->bitmap_offset
= cpu_to_le32((__u32
)mddev
->bitmap_info
.offset
);
1770 sb
->feature_map
= cpu_to_le32(MD_FEATURE_BITMAP_OFFSET
);
1773 if (rdev
->raid_disk
>= 0 &&
1774 !test_bit(In_sync
, &rdev
->flags
)) {
1776 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET
);
1777 sb
->recovery_offset
=
1778 cpu_to_le64(rdev
->recovery_offset
);
1780 if (test_bit(Replacement
, &rdev
->flags
))
1782 cpu_to_le32(MD_FEATURE_REPLACEMENT
);
1784 if (mddev
->reshape_position
!= MaxSector
) {
1785 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE
);
1786 sb
->reshape_position
= cpu_to_le64(mddev
->reshape_position
);
1787 sb
->new_layout
= cpu_to_le32(mddev
->new_layout
);
1788 sb
->delta_disks
= cpu_to_le32(mddev
->delta_disks
);
1789 sb
->new_level
= cpu_to_le32(mddev
->new_level
);
1790 sb
->new_chunk
= cpu_to_le32(mddev
->new_chunk_sectors
);
1793 if (rdev
->badblocks
.count
== 0)
1794 /* Nothing to do for bad blocks*/ ;
1795 else if (sb
->bblog_offset
== 0)
1796 /* Cannot record bad blocks on this device */
1797 md_error(mddev
, rdev
);
1799 struct badblocks
*bb
= &rdev
->badblocks
;
1800 u64
*bbp
= (u64
*)page_address(rdev
->bb_page
);
1802 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_BAD_BLOCKS
);
1807 seq
= read_seqbegin(&bb
->lock
);
1809 memset(bbp
, 0xff, PAGE_SIZE
);
1811 for (i
= 0 ; i
< bb
->count
; i
++) {
1812 u64 internal_bb
= p
[i
];
1813 u64 store_bb
= ((BB_OFFSET(internal_bb
) << 10)
1814 | BB_LEN(internal_bb
));
1815 bbp
[i
] = cpu_to_le64(store_bb
);
1818 if (read_seqretry(&bb
->lock
, seq
))
1821 bb
->sector
= (rdev
->sb_start
+
1822 (int)le32_to_cpu(sb
->bblog_offset
));
1823 bb
->size
= le16_to_cpu(sb
->bblog_size
);
1828 rdev_for_each(rdev2
, mddev
)
1829 if (rdev2
->desc_nr
+1 > max_dev
)
1830 max_dev
= rdev2
->desc_nr
+1;
1832 if (max_dev
> le32_to_cpu(sb
->max_dev
)) {
1834 sb
->max_dev
= cpu_to_le32(max_dev
);
1835 rdev
->sb_size
= max_dev
* 2 + 256;
1836 bmask
= queue_logical_block_size(rdev
->bdev
->bd_disk
->queue
)-1;
1837 if (rdev
->sb_size
& bmask
)
1838 rdev
->sb_size
= (rdev
->sb_size
| bmask
) + 1;
1840 max_dev
= le32_to_cpu(sb
->max_dev
);
1842 for (i
=0; i
<max_dev
;i
++)
1843 sb
->dev_roles
[i
] = cpu_to_le16(0xfffe);
1845 rdev_for_each(rdev2
, mddev
) {
1847 if (test_bit(Faulty
, &rdev2
->flags
))
1848 sb
->dev_roles
[i
] = cpu_to_le16(0xfffe);
1849 else if (test_bit(In_sync
, &rdev2
->flags
))
1850 sb
->dev_roles
[i
] = cpu_to_le16(rdev2
->raid_disk
);
1851 else if (rdev2
->raid_disk
>= 0)
1852 sb
->dev_roles
[i
] = cpu_to_le16(rdev2
->raid_disk
);
1854 sb
->dev_roles
[i
] = cpu_to_le16(0xffff);
1857 sb
->sb_csum
= calc_sb_1_csum(sb
);
1860 static unsigned long long
1861 super_1_rdev_size_change(struct md_rdev
*rdev
, sector_t num_sectors
)
1863 struct mdp_superblock_1
*sb
;
1864 sector_t max_sectors
;
1865 if (num_sectors
&& num_sectors
< rdev
->mddev
->dev_sectors
)
1866 return 0; /* component must fit device */
1867 if (rdev
->sb_start
< rdev
->data_offset
) {
1868 /* minor versions 1 and 2; superblock before data */
1869 max_sectors
= i_size_read(rdev
->bdev
->bd_inode
) >> 9;
1870 max_sectors
-= rdev
->data_offset
;
1871 if (!num_sectors
|| num_sectors
> max_sectors
)
1872 num_sectors
= max_sectors
;
1873 } else if (rdev
->mddev
->bitmap_info
.offset
) {
1874 /* minor version 0 with bitmap we can't move */
1877 /* minor version 0; superblock after data */
1879 sb_start
= (i_size_read(rdev
->bdev
->bd_inode
) >> 9) - 8*2;
1880 sb_start
&= ~(sector_t
)(4*2 - 1);
1881 max_sectors
= rdev
->sectors
+ sb_start
- rdev
->sb_start
;
1882 if (!num_sectors
|| num_sectors
> max_sectors
)
1883 num_sectors
= max_sectors
;
1884 rdev
->sb_start
= sb_start
;
1886 sb
= page_address(rdev
->sb_page
);
1887 sb
->data_size
= cpu_to_le64(num_sectors
);
1888 sb
->super_offset
= rdev
->sb_start
;
1889 sb
->sb_csum
= calc_sb_1_csum(sb
);
1890 md_super_write(rdev
->mddev
, rdev
, rdev
->sb_start
, rdev
->sb_size
,
1892 md_super_wait(rdev
->mddev
);
1896 static struct super_type super_types
[] = {
1899 .owner
= THIS_MODULE
,
1900 .load_super
= super_90_load
,
1901 .validate_super
= super_90_validate
,
1902 .sync_super
= super_90_sync
,
1903 .rdev_size_change
= super_90_rdev_size_change
,
1907 .owner
= THIS_MODULE
,
1908 .load_super
= super_1_load
,
1909 .validate_super
= super_1_validate
,
1910 .sync_super
= super_1_sync
,
1911 .rdev_size_change
= super_1_rdev_size_change
,
1915 static void sync_super(struct mddev
*mddev
, struct md_rdev
*rdev
)
1917 if (mddev
->sync_super
) {
1918 mddev
->sync_super(mddev
, rdev
);
1922 BUG_ON(mddev
->major_version
>= ARRAY_SIZE(super_types
));
1924 super_types
[mddev
->major_version
].sync_super(mddev
, rdev
);
1927 static int match_mddev_units(struct mddev
*mddev1
, struct mddev
*mddev2
)
1929 struct md_rdev
*rdev
, *rdev2
;
1932 rdev_for_each_rcu(rdev
, mddev1
)
1933 rdev_for_each_rcu(rdev2
, mddev2
)
1934 if (rdev
->bdev
->bd_contains
==
1935 rdev2
->bdev
->bd_contains
) {
1943 static LIST_HEAD(pending_raid_disks
);
1946 * Try to register data integrity profile for an mddev
1948 * This is called when an array is started and after a disk has been kicked
1949 * from the array. It only succeeds if all working and active component devices
1950 * are integrity capable with matching profiles.
1952 int md_integrity_register(struct mddev
*mddev
)
1954 struct md_rdev
*rdev
, *reference
= NULL
;
1956 if (list_empty(&mddev
->disks
))
1957 return 0; /* nothing to do */
1958 if (!mddev
->gendisk
|| blk_get_integrity(mddev
->gendisk
))
1959 return 0; /* shouldn't register, or already is */
1960 rdev_for_each(rdev
, mddev
) {
1961 /* skip spares and non-functional disks */
1962 if (test_bit(Faulty
, &rdev
->flags
))
1964 if (rdev
->raid_disk
< 0)
1967 /* Use the first rdev as the reference */
1971 /* does this rdev's profile match the reference profile? */
1972 if (blk_integrity_compare(reference
->bdev
->bd_disk
,
1973 rdev
->bdev
->bd_disk
) < 0)
1976 if (!reference
|| !bdev_get_integrity(reference
->bdev
))
1979 * All component devices are integrity capable and have matching
1980 * profiles, register the common profile for the md device.
1982 if (blk_integrity_register(mddev
->gendisk
,
1983 bdev_get_integrity(reference
->bdev
)) != 0) {
1984 printk(KERN_ERR
"md: failed to register integrity for %s\n",
1988 printk(KERN_NOTICE
"md: data integrity enabled on %s\n", mdname(mddev
));
1989 if (bioset_integrity_create(mddev
->bio_set
, BIO_POOL_SIZE
)) {
1990 printk(KERN_ERR
"md: failed to create integrity pool for %s\n",
1996 EXPORT_SYMBOL(md_integrity_register
);
1998 /* Disable data integrity if non-capable/non-matching disk is being added */
1999 void md_integrity_add_rdev(struct md_rdev
*rdev
, struct mddev
*mddev
)
2001 struct blk_integrity
*bi_rdev
= bdev_get_integrity(rdev
->bdev
);
2002 struct blk_integrity
*bi_mddev
= blk_get_integrity(mddev
->gendisk
);
2004 if (!bi_mddev
) /* nothing to do */
2006 if (rdev
->raid_disk
< 0) /* skip spares */
2008 if (bi_rdev
&& blk_integrity_compare(mddev
->gendisk
,
2009 rdev
->bdev
->bd_disk
) >= 0)
2011 printk(KERN_NOTICE
"disabling data integrity on %s\n", mdname(mddev
));
2012 blk_integrity_unregister(mddev
->gendisk
);
2014 EXPORT_SYMBOL(md_integrity_add_rdev
);
2016 static int bind_rdev_to_array(struct md_rdev
* rdev
, struct mddev
* mddev
)
2018 char b
[BDEVNAME_SIZE
];
2028 /* prevent duplicates */
2029 if (find_rdev(mddev
, rdev
->bdev
->bd_dev
))
2032 /* make sure rdev->sectors exceeds mddev->dev_sectors */
2033 if (rdev
->sectors
&& (mddev
->dev_sectors
== 0 ||
2034 rdev
->sectors
< mddev
->dev_sectors
)) {
2036 /* Cannot change size, so fail
2037 * If mddev->level <= 0, then we don't care
2038 * about aligning sizes (e.g. linear)
2040 if (mddev
->level
> 0)
2043 mddev
->dev_sectors
= rdev
->sectors
;
2046 /* Verify rdev->desc_nr is unique.
2047 * If it is -1, assign a free number, else
2048 * check number is not in use
2050 if (rdev
->desc_nr
< 0) {
2052 if (mddev
->pers
) choice
= mddev
->raid_disks
;
2053 while (find_rdev_nr(mddev
, choice
))
2055 rdev
->desc_nr
= choice
;
2057 if (find_rdev_nr(mddev
, rdev
->desc_nr
))
2060 if (mddev
->max_disks
&& rdev
->desc_nr
>= mddev
->max_disks
) {
2061 printk(KERN_WARNING
"md: %s: array is limited to %d devices\n",
2062 mdname(mddev
), mddev
->max_disks
);
2065 bdevname(rdev
->bdev
,b
);
2066 while ( (s
=strchr(b
, '/')) != NULL
)
2069 rdev
->mddev
= mddev
;
2070 printk(KERN_INFO
"md: bind<%s>\n", b
);
2072 if ((err
= kobject_add(&rdev
->kobj
, &mddev
->kobj
, "dev-%s", b
)))
2075 ko
= &part_to_dev(rdev
->bdev
->bd_part
)->kobj
;
2076 if (sysfs_create_link(&rdev
->kobj
, ko
, "block"))
2077 /* failure here is OK */;
2078 rdev
->sysfs_state
= sysfs_get_dirent_safe(rdev
->kobj
.sd
, "state");
2080 list_add_rcu(&rdev
->same_set
, &mddev
->disks
);
2081 bd_link_disk_holder(rdev
->bdev
, mddev
->gendisk
);
2083 /* May as well allow recovery to be retried once */
2084 mddev
->recovery_disabled
++;
2089 printk(KERN_WARNING
"md: failed to register dev-%s for %s\n",
2094 static void md_delayed_delete(struct work_struct
*ws
)
2096 struct md_rdev
*rdev
= container_of(ws
, struct md_rdev
, del_work
);
2097 kobject_del(&rdev
->kobj
);
2098 kobject_put(&rdev
->kobj
);
2101 static void unbind_rdev_from_array(struct md_rdev
* rdev
)
2103 char b
[BDEVNAME_SIZE
];
2108 bd_unlink_disk_holder(rdev
->bdev
, rdev
->mddev
->gendisk
);
2109 list_del_rcu(&rdev
->same_set
);
2110 printk(KERN_INFO
"md: unbind<%s>\n", bdevname(rdev
->bdev
,b
));
2112 sysfs_remove_link(&rdev
->kobj
, "block");
2113 sysfs_put(rdev
->sysfs_state
);
2114 rdev
->sysfs_state
= NULL
;
2115 kfree(rdev
->badblocks
.page
);
2116 rdev
->badblocks
.count
= 0;
2117 rdev
->badblocks
.page
= NULL
;
2118 /* We need to delay this, otherwise we can deadlock when
2119 * writing to 'remove' to "dev/state". We also need
2120 * to delay it due to rcu usage.
2123 INIT_WORK(&rdev
->del_work
, md_delayed_delete
);
2124 kobject_get(&rdev
->kobj
);
2125 queue_work(md_misc_wq
, &rdev
->del_work
);
2129 * prevent the device from being mounted, repartitioned or
2130 * otherwise reused by a RAID array (or any other kernel
2131 * subsystem), by bd_claiming the device.
2133 static int lock_rdev(struct md_rdev
*rdev
, dev_t dev
, int shared
)
2136 struct block_device
*bdev
;
2137 char b
[BDEVNAME_SIZE
];
2139 bdev
= blkdev_get_by_dev(dev
, FMODE_READ
|FMODE_WRITE
|FMODE_EXCL
,
2140 shared
? (struct md_rdev
*)lock_rdev
: rdev
);
2142 printk(KERN_ERR
"md: could not open %s.\n",
2143 __bdevname(dev
, b
));
2144 return PTR_ERR(bdev
);
2150 static void unlock_rdev(struct md_rdev
*rdev
)
2152 struct block_device
*bdev
= rdev
->bdev
;
2156 blkdev_put(bdev
, FMODE_READ
|FMODE_WRITE
|FMODE_EXCL
);
2159 void md_autodetect_dev(dev_t dev
);
2161 static void export_rdev(struct md_rdev
* rdev
)
2163 char b
[BDEVNAME_SIZE
];
2164 printk(KERN_INFO
"md: export_rdev(%s)\n",
2165 bdevname(rdev
->bdev
,b
));
2170 if (test_bit(AutoDetected
, &rdev
->flags
))
2171 md_autodetect_dev(rdev
->bdev
->bd_dev
);
2174 kobject_put(&rdev
->kobj
);
2177 static void kick_rdev_from_array(struct md_rdev
* rdev
)
2179 unbind_rdev_from_array(rdev
);
2183 static void export_array(struct mddev
*mddev
)
2185 struct md_rdev
*rdev
, *tmp
;
2187 rdev_for_each_safe(rdev
, tmp
, mddev
) {
2192 kick_rdev_from_array(rdev
);
2194 if (!list_empty(&mddev
->disks
))
2196 mddev
->raid_disks
= 0;
2197 mddev
->major_version
= 0;
2200 static void print_desc(mdp_disk_t
*desc
)
2202 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc
->number
,
2203 desc
->major
,desc
->minor
,desc
->raid_disk
,desc
->state
);
2206 static void print_sb_90(mdp_super_t
*sb
)
2211 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
2212 sb
->major_version
, sb
->minor_version
, sb
->patch_version
,
2213 sb
->set_uuid0
, sb
->set_uuid1
, sb
->set_uuid2
, sb
->set_uuid3
,
2215 printk(KERN_INFO
"md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
2216 sb
->level
, sb
->size
, sb
->nr_disks
, sb
->raid_disks
,
2217 sb
->md_minor
, sb
->layout
, sb
->chunk_size
);
2218 printk(KERN_INFO
"md: UT:%08x ST:%d AD:%d WD:%d"
2219 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
2220 sb
->utime
, sb
->state
, sb
->active_disks
, sb
->working_disks
,
2221 sb
->failed_disks
, sb
->spare_disks
,
2222 sb
->sb_csum
, (unsigned long)sb
->events_lo
);
2225 for (i
= 0; i
< MD_SB_DISKS
; i
++) {
2228 desc
= sb
->disks
+ i
;
2229 if (desc
->number
|| desc
->major
|| desc
->minor
||
2230 desc
->raid_disk
|| (desc
->state
&& (desc
->state
!= 4))) {
2231 printk(" D %2d: ", i
);
2235 printk(KERN_INFO
"md: THIS: ");
2236 print_desc(&sb
->this_disk
);
2239 static void print_sb_1(struct mdp_superblock_1
*sb
)
2243 uuid
= sb
->set_uuid
;
2245 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n"
2246 "md: Name: \"%s\" CT:%llu\n",
2247 le32_to_cpu(sb
->major_version
),
2248 le32_to_cpu(sb
->feature_map
),
2251 (unsigned long long)le64_to_cpu(sb
->ctime
)
2252 & MD_SUPERBLOCK_1_TIME_SEC_MASK
);
2254 uuid
= sb
->device_uuid
;
2256 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
2258 "md: Dev:%08x UUID: %pU\n"
2259 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
2260 "md: (MaxDev:%u) \n",
2261 le32_to_cpu(sb
->level
),
2262 (unsigned long long)le64_to_cpu(sb
->size
),
2263 le32_to_cpu(sb
->raid_disks
),
2264 le32_to_cpu(sb
->layout
),
2265 le32_to_cpu(sb
->chunksize
),
2266 (unsigned long long)le64_to_cpu(sb
->data_offset
),
2267 (unsigned long long)le64_to_cpu(sb
->data_size
),
2268 (unsigned long long)le64_to_cpu(sb
->super_offset
),
2269 (unsigned long long)le64_to_cpu(sb
->recovery_offset
),
2270 le32_to_cpu(sb
->dev_number
),
2273 (unsigned long long)le64_to_cpu(sb
->utime
) & MD_SUPERBLOCK_1_TIME_SEC_MASK
,
2274 (unsigned long long)le64_to_cpu(sb
->events
),
2275 (unsigned long long)le64_to_cpu(sb
->resync_offset
),
2276 le32_to_cpu(sb
->sb_csum
),
2277 le32_to_cpu(sb
->max_dev
)
2281 static void print_rdev(struct md_rdev
*rdev
, int major_version
)
2283 char b
[BDEVNAME_SIZE
];
2284 printk(KERN_INFO
"md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
2285 bdevname(rdev
->bdev
, b
), (unsigned long long)rdev
->sectors
,
2286 test_bit(Faulty
, &rdev
->flags
), test_bit(In_sync
, &rdev
->flags
),
2288 if (rdev
->sb_loaded
) {
2289 printk(KERN_INFO
"md: rdev superblock (MJ:%d):\n", major_version
);
2290 switch (major_version
) {
2292 print_sb_90(page_address(rdev
->sb_page
));
2295 print_sb_1(page_address(rdev
->sb_page
));
2299 printk(KERN_INFO
"md: no rdev superblock!\n");
2302 static void md_print_devices(void)
2304 struct list_head
*tmp
;
2305 struct md_rdev
*rdev
;
2306 struct mddev
*mddev
;
2307 char b
[BDEVNAME_SIZE
];
2310 printk("md: **********************************\n");
2311 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
2312 printk("md: **********************************\n");
2313 for_each_mddev(mddev
, tmp
) {
2316 bitmap_print_sb(mddev
->bitmap
);
2318 printk("%s: ", mdname(mddev
));
2319 rdev_for_each(rdev
, mddev
)
2320 printk("<%s>", bdevname(rdev
->bdev
,b
));
2323 rdev_for_each(rdev
, mddev
)
2324 print_rdev(rdev
, mddev
->major_version
);
2326 printk("md: **********************************\n");
2331 static void sync_sbs(struct mddev
* mddev
, int nospares
)
2333 /* Update each superblock (in-memory image), but
2334 * if we are allowed to, skip spares which already
2335 * have the right event counter, or have one earlier
2336 * (which would mean they aren't being marked as dirty
2337 * with the rest of the array)
2339 struct md_rdev
*rdev
;
2340 rdev_for_each(rdev
, mddev
) {
2341 if (rdev
->sb_events
== mddev
->events
||
2343 rdev
->raid_disk
< 0 &&
2344 rdev
->sb_events
+1 == mddev
->events
)) {
2345 /* Don't update this superblock */
2346 rdev
->sb_loaded
= 2;
2348 sync_super(mddev
, rdev
);
2349 rdev
->sb_loaded
= 1;
2354 static void md_update_sb(struct mddev
* mddev
, int force_change
)
2356 struct md_rdev
*rdev
;
2359 int any_badblocks_changed
= 0;
2362 /* First make sure individual recovery_offsets are correct */
2363 rdev_for_each(rdev
, mddev
) {
2364 if (rdev
->raid_disk
>= 0 &&
2365 mddev
->delta_disks
>= 0 &&
2366 !test_bit(In_sync
, &rdev
->flags
) &&
2367 mddev
->curr_resync_completed
> rdev
->recovery_offset
)
2368 rdev
->recovery_offset
= mddev
->curr_resync_completed
;
2371 if (!mddev
->persistent
) {
2372 clear_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
2373 clear_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
2374 if (!mddev
->external
) {
2375 clear_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
2376 rdev_for_each(rdev
, mddev
) {
2377 if (rdev
->badblocks
.changed
) {
2378 rdev
->badblocks
.changed
= 0;
2379 md_ack_all_badblocks(&rdev
->badblocks
);
2380 md_error(mddev
, rdev
);
2382 clear_bit(Blocked
, &rdev
->flags
);
2383 clear_bit(BlockedBadBlocks
, &rdev
->flags
);
2384 wake_up(&rdev
->blocked_wait
);
2387 wake_up(&mddev
->sb_wait
);
2391 spin_lock_irq(&mddev
->write_lock
);
2393 mddev
->utime
= get_seconds();
2395 if (test_and_clear_bit(MD_CHANGE_DEVS
, &mddev
->flags
))
2397 if (test_and_clear_bit(MD_CHANGE_CLEAN
, &mddev
->flags
))
2398 /* just a clean<-> dirty transition, possibly leave spares alone,
2399 * though if events isn't the right even/odd, we will have to do
2405 if (mddev
->degraded
)
2406 /* If the array is degraded, then skipping spares is both
2407 * dangerous and fairly pointless.
2408 * Dangerous because a device that was removed from the array
2409 * might have a event_count that still looks up-to-date,
2410 * so it can be re-added without a resync.
2411 * Pointless because if there are any spares to skip,
2412 * then a recovery will happen and soon that array won't
2413 * be degraded any more and the spare can go back to sleep then.
2417 sync_req
= mddev
->in_sync
;
2419 /* If this is just a dirty<->clean transition, and the array is clean
2420 * and 'events' is odd, we can roll back to the previous clean state */
2422 && (mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
)
2423 && mddev
->can_decrease_events
2424 && mddev
->events
!= 1) {
2426 mddev
->can_decrease_events
= 0;
2428 /* otherwise we have to go forward and ... */
2430 mddev
->can_decrease_events
= nospares
;
2433 if (!mddev
->events
) {
2435 * oops, this 64-bit counter should never wrap.
2436 * Either we are in around ~1 trillion A.C., assuming
2437 * 1 reboot per second, or we have a bug:
2443 rdev_for_each(rdev
, mddev
) {
2444 if (rdev
->badblocks
.changed
)
2445 any_badblocks_changed
++;
2446 if (test_bit(Faulty
, &rdev
->flags
))
2447 set_bit(FaultRecorded
, &rdev
->flags
);
2450 sync_sbs(mddev
, nospares
);
2451 spin_unlock_irq(&mddev
->write_lock
);
2453 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2454 mdname(mddev
), mddev
->in_sync
);
2456 bitmap_update_sb(mddev
->bitmap
);
2457 rdev_for_each(rdev
, mddev
) {
2458 char b
[BDEVNAME_SIZE
];
2460 if (rdev
->sb_loaded
!= 1)
2461 continue; /* no noise on spare devices */
2463 if (!test_bit(Faulty
, &rdev
->flags
) &&
2464 rdev
->saved_raid_disk
== -1) {
2465 md_super_write(mddev
,rdev
,
2466 rdev
->sb_start
, rdev
->sb_size
,
2468 pr_debug("md: (write) %s's sb offset: %llu\n",
2469 bdevname(rdev
->bdev
, b
),
2470 (unsigned long long)rdev
->sb_start
);
2471 rdev
->sb_events
= mddev
->events
;
2472 if (rdev
->badblocks
.size
) {
2473 md_super_write(mddev
, rdev
,
2474 rdev
->badblocks
.sector
,
2475 rdev
->badblocks
.size
<< 9,
2477 rdev
->badblocks
.size
= 0;
2480 } else if (test_bit(Faulty
, &rdev
->flags
))
2481 pr_debug("md: %s (skipping faulty)\n",
2482 bdevname(rdev
->bdev
, b
));
2484 pr_debug("(skipping incremental s/r ");
2486 if (mddev
->level
== LEVEL_MULTIPATH
)
2487 /* only need to write one superblock... */
2490 md_super_wait(mddev
);
2491 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
2493 spin_lock_irq(&mddev
->write_lock
);
2494 if (mddev
->in_sync
!= sync_req
||
2495 test_bit(MD_CHANGE_DEVS
, &mddev
->flags
)) {
2496 /* have to write it out again */
2497 spin_unlock_irq(&mddev
->write_lock
);
2500 clear_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
2501 spin_unlock_irq(&mddev
->write_lock
);
2502 wake_up(&mddev
->sb_wait
);
2503 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
2504 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
2506 rdev_for_each(rdev
, mddev
) {
2507 if (test_and_clear_bit(FaultRecorded
, &rdev
->flags
))
2508 clear_bit(Blocked
, &rdev
->flags
);
2510 if (any_badblocks_changed
)
2511 md_ack_all_badblocks(&rdev
->badblocks
);
2512 clear_bit(BlockedBadBlocks
, &rdev
->flags
);
2513 wake_up(&rdev
->blocked_wait
);
2517 /* words written to sysfs files may, or may not, be \n terminated.
2518 * We want to accept with case. For this we use cmd_match.
2520 static int cmd_match(const char *cmd
, const char *str
)
2522 /* See if cmd, written into a sysfs file, matches
2523 * str. They must either be the same, or cmd can
2524 * have a trailing newline
2526 while (*cmd
&& *str
&& *cmd
== *str
) {
2537 struct rdev_sysfs_entry
{
2538 struct attribute attr
;
2539 ssize_t (*show
)(struct md_rdev
*, char *);
2540 ssize_t (*store
)(struct md_rdev
*, const char *, size_t);
2544 state_show(struct md_rdev
*rdev
, char *page
)
2549 if (test_bit(Faulty
, &rdev
->flags
) ||
2550 rdev
->badblocks
.unacked_exist
) {
2551 len
+= sprintf(page
+len
, "%sfaulty",sep
);
2554 if (test_bit(In_sync
, &rdev
->flags
)) {
2555 len
+= sprintf(page
+len
, "%sin_sync",sep
);
2558 if (test_bit(WriteMostly
, &rdev
->flags
)) {
2559 len
+= sprintf(page
+len
, "%swrite_mostly",sep
);
2562 if (test_bit(Blocked
, &rdev
->flags
) ||
2563 (rdev
->badblocks
.unacked_exist
2564 && !test_bit(Faulty
, &rdev
->flags
))) {
2565 len
+= sprintf(page
+len
, "%sblocked", sep
);
2568 if (!test_bit(Faulty
, &rdev
->flags
) &&
2569 !test_bit(In_sync
, &rdev
->flags
)) {
2570 len
+= sprintf(page
+len
, "%sspare", sep
);
2573 if (test_bit(WriteErrorSeen
, &rdev
->flags
)) {
2574 len
+= sprintf(page
+len
, "%swrite_error", sep
);
2577 if (test_bit(WantReplacement
, &rdev
->flags
)) {
2578 len
+= sprintf(page
+len
, "%swant_replacement", sep
);
2581 if (test_bit(Replacement
, &rdev
->flags
)) {
2582 len
+= sprintf(page
+len
, "%sreplacement", sep
);
2586 return len
+sprintf(page
+len
, "\n");
2590 state_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
2593 * faulty - simulates an error
2594 * remove - disconnects the device
2595 * writemostly - sets write_mostly
2596 * -writemostly - clears write_mostly
2597 * blocked - sets the Blocked flags
2598 * -blocked - clears the Blocked and possibly simulates an error
2599 * insync - sets Insync providing device isn't active
2600 * write_error - sets WriteErrorSeen
2601 * -write_error - clears WriteErrorSeen
2604 if (cmd_match(buf
, "faulty") && rdev
->mddev
->pers
) {
2605 md_error(rdev
->mddev
, rdev
);
2606 if (test_bit(Faulty
, &rdev
->flags
))
2610 } else if (cmd_match(buf
, "remove")) {
2611 if (rdev
->raid_disk
>= 0)
2614 struct mddev
*mddev
= rdev
->mddev
;
2615 kick_rdev_from_array(rdev
);
2617 md_update_sb(mddev
, 1);
2618 md_new_event(mddev
);
2621 } else if (cmd_match(buf
, "writemostly")) {
2622 set_bit(WriteMostly
, &rdev
->flags
);
2624 } else if (cmd_match(buf
, "-writemostly")) {
2625 clear_bit(WriteMostly
, &rdev
->flags
);
2627 } else if (cmd_match(buf
, "blocked")) {
2628 set_bit(Blocked
, &rdev
->flags
);
2630 } else if (cmd_match(buf
, "-blocked")) {
2631 if (!test_bit(Faulty
, &rdev
->flags
) &&
2632 rdev
->badblocks
.unacked_exist
) {
2633 /* metadata handler doesn't understand badblocks,
2634 * so we need to fail the device
2636 md_error(rdev
->mddev
, rdev
);
2638 clear_bit(Blocked
, &rdev
->flags
);
2639 clear_bit(BlockedBadBlocks
, &rdev
->flags
);
2640 wake_up(&rdev
->blocked_wait
);
2641 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
2642 md_wakeup_thread(rdev
->mddev
->thread
);
2645 } else if (cmd_match(buf
, "insync") && rdev
->raid_disk
== -1) {
2646 set_bit(In_sync
, &rdev
->flags
);
2648 } else if (cmd_match(buf
, "write_error")) {
2649 set_bit(WriteErrorSeen
, &rdev
->flags
);
2651 } else if (cmd_match(buf
, "-write_error")) {
2652 clear_bit(WriteErrorSeen
, &rdev
->flags
);
2654 } else if (cmd_match(buf
, "want_replacement")) {
2655 /* Any non-spare device that is not a replacement can
2656 * become want_replacement at any time, but we then need to
2657 * check if recovery is needed.
2659 if (rdev
->raid_disk
>= 0 &&
2660 !test_bit(Replacement
, &rdev
->flags
))
2661 set_bit(WantReplacement
, &rdev
->flags
);
2662 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
2663 md_wakeup_thread(rdev
->mddev
->thread
);
2665 } else if (cmd_match(buf
, "-want_replacement")) {
2666 /* Clearing 'want_replacement' is always allowed.
2667 * Once replacements starts it is too late though.
2670 clear_bit(WantReplacement
, &rdev
->flags
);
2671 } else if (cmd_match(buf
, "replacement")) {
2672 /* Can only set a device as a replacement when array has not
2673 * yet been started. Once running, replacement is automatic
2674 * from spares, or by assigning 'slot'.
2676 if (rdev
->mddev
->pers
)
2679 set_bit(Replacement
, &rdev
->flags
);
2682 } else if (cmd_match(buf
, "-replacement")) {
2683 /* Similarly, can only clear Replacement before start */
2684 if (rdev
->mddev
->pers
)
2687 clear_bit(Replacement
, &rdev
->flags
);
2692 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
2693 return err
? err
: len
;
2695 static struct rdev_sysfs_entry rdev_state
=
2696 __ATTR(state
, S_IRUGO
|S_IWUSR
, state_show
, state_store
);
2699 errors_show(struct md_rdev
*rdev
, char *page
)
2701 return sprintf(page
, "%d\n", atomic_read(&rdev
->corrected_errors
));
2705 errors_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
2708 unsigned long n
= simple_strtoul(buf
, &e
, 10);
2709 if (*buf
&& (*e
== 0 || *e
== '\n')) {
2710 atomic_set(&rdev
->corrected_errors
, n
);
2715 static struct rdev_sysfs_entry rdev_errors
=
2716 __ATTR(errors
, S_IRUGO
|S_IWUSR
, errors_show
, errors_store
);
2719 slot_show(struct md_rdev
*rdev
, char *page
)
2721 if (rdev
->raid_disk
< 0)
2722 return sprintf(page
, "none\n");
2724 return sprintf(page
, "%d\n", rdev
->raid_disk
);
2728 slot_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
2732 int slot
= simple_strtoul(buf
, &e
, 10);
2733 if (strncmp(buf
, "none", 4)==0)
2735 else if (e
==buf
|| (*e
&& *e
!= '\n'))
2737 if (rdev
->mddev
->pers
&& slot
== -1) {
2738 /* Setting 'slot' on an active array requires also
2739 * updating the 'rd%d' link, and communicating
2740 * with the personality with ->hot_*_disk.
2741 * For now we only support removing
2742 * failed/spare devices. This normally happens automatically,
2743 * but not when the metadata is externally managed.
2745 if (rdev
->raid_disk
== -1)
2747 /* personality does all needed checks */
2748 if (rdev
->mddev
->pers
->hot_remove_disk
== NULL
)
2750 err
= rdev
->mddev
->pers
->
2751 hot_remove_disk(rdev
->mddev
, rdev
);
2754 sysfs_unlink_rdev(rdev
->mddev
, rdev
);
2755 rdev
->raid_disk
= -1;
2756 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
2757 md_wakeup_thread(rdev
->mddev
->thread
);
2758 } else if (rdev
->mddev
->pers
) {
2759 /* Activating a spare .. or possibly reactivating
2760 * if we ever get bitmaps working here.
2763 if (rdev
->raid_disk
!= -1)
2766 if (test_bit(MD_RECOVERY_RUNNING
, &rdev
->mddev
->recovery
))
2769 if (rdev
->mddev
->pers
->hot_add_disk
== NULL
)
2772 if (slot
>= rdev
->mddev
->raid_disks
&&
2773 slot
>= rdev
->mddev
->raid_disks
+ rdev
->mddev
->delta_disks
)
2776 rdev
->raid_disk
= slot
;
2777 if (test_bit(In_sync
, &rdev
->flags
))
2778 rdev
->saved_raid_disk
= slot
;
2780 rdev
->saved_raid_disk
= -1;
2781 clear_bit(In_sync
, &rdev
->flags
);
2782 err
= rdev
->mddev
->pers
->
2783 hot_add_disk(rdev
->mddev
, rdev
);
2785 rdev
->raid_disk
= -1;
2788 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
2789 if (sysfs_link_rdev(rdev
->mddev
, rdev
))
2790 /* failure here is OK */;
2791 /* don't wakeup anyone, leave that to userspace. */
2793 if (slot
>= rdev
->mddev
->raid_disks
&&
2794 slot
>= rdev
->mddev
->raid_disks
+ rdev
->mddev
->delta_disks
)
2796 rdev
->raid_disk
= slot
;
2797 /* assume it is working */
2798 clear_bit(Faulty
, &rdev
->flags
);
2799 clear_bit(WriteMostly
, &rdev
->flags
);
2800 set_bit(In_sync
, &rdev
->flags
);
2801 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
2807 static struct rdev_sysfs_entry rdev_slot
=
2808 __ATTR(slot
, S_IRUGO
|S_IWUSR
, slot_show
, slot_store
);
2811 offset_show(struct md_rdev
*rdev
, char *page
)
2813 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->data_offset
);
2817 offset_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
2820 unsigned long long offset
= simple_strtoull(buf
, &e
, 10);
2821 if (e
==buf
|| (*e
&& *e
!= '\n'))
2823 if (rdev
->mddev
->pers
&& rdev
->raid_disk
>= 0)
2825 if (rdev
->sectors
&& rdev
->mddev
->external
)
2826 /* Must set offset before size, so overlap checks
2829 rdev
->data_offset
= offset
;
2833 static struct rdev_sysfs_entry rdev_offset
=
2834 __ATTR(offset
, S_IRUGO
|S_IWUSR
, offset_show
, offset_store
);
2837 rdev_size_show(struct md_rdev
*rdev
, char *page
)
2839 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->sectors
/ 2);
2842 static int overlaps(sector_t s1
, sector_t l1
, sector_t s2
, sector_t l2
)
2844 /* check if two start/length pairs overlap */
2852 static int strict_blocks_to_sectors(const char *buf
, sector_t
*sectors
)
2854 unsigned long long blocks
;
2857 if (strict_strtoull(buf
, 10, &blocks
) < 0)
2860 if (blocks
& 1ULL << (8 * sizeof(blocks
) - 1))
2861 return -EINVAL
; /* sector conversion overflow */
2864 if (new != blocks
* 2)
2865 return -EINVAL
; /* unsigned long long to sector_t overflow */
2872 rdev_size_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
2874 struct mddev
*my_mddev
= rdev
->mddev
;
2875 sector_t oldsectors
= rdev
->sectors
;
2878 if (strict_blocks_to_sectors(buf
, §ors
) < 0)
2880 if (my_mddev
->pers
&& rdev
->raid_disk
>= 0) {
2881 if (my_mddev
->persistent
) {
2882 sectors
= super_types
[my_mddev
->major_version
].
2883 rdev_size_change(rdev
, sectors
);
2886 } else if (!sectors
)
2887 sectors
= (i_size_read(rdev
->bdev
->bd_inode
) >> 9) -
2889 if (!my_mddev
->pers
->resize
)
2890 /* Cannot change size for RAID0 or Linear etc */
2893 if (sectors
< my_mddev
->dev_sectors
)
2894 return -EINVAL
; /* component must fit device */
2896 rdev
->sectors
= sectors
;
2897 if (sectors
> oldsectors
&& my_mddev
->external
) {
2898 /* need to check that all other rdevs with the same ->bdev
2899 * do not overlap. We need to unlock the mddev to avoid
2900 * a deadlock. We have already changed rdev->sectors, and if
2901 * we have to change it back, we will have the lock again.
2903 struct mddev
*mddev
;
2905 struct list_head
*tmp
;
2907 mddev_unlock(my_mddev
);
2908 for_each_mddev(mddev
, tmp
) {
2909 struct md_rdev
*rdev2
;
2912 rdev_for_each(rdev2
, mddev
)
2913 if (rdev
->bdev
== rdev2
->bdev
&&
2915 overlaps(rdev
->data_offset
, rdev
->sectors
,
2921 mddev_unlock(mddev
);
2927 mddev_lock(my_mddev
);
2929 /* Someone else could have slipped in a size
2930 * change here, but doing so is just silly.
2931 * We put oldsectors back because we *know* it is
2932 * safe, and trust userspace not to race with
2935 rdev
->sectors
= oldsectors
;
2942 static struct rdev_sysfs_entry rdev_size
=
2943 __ATTR(size
, S_IRUGO
|S_IWUSR
, rdev_size_show
, rdev_size_store
);
2946 static ssize_t
recovery_start_show(struct md_rdev
*rdev
, char *page
)
2948 unsigned long long recovery_start
= rdev
->recovery_offset
;
2950 if (test_bit(In_sync
, &rdev
->flags
) ||
2951 recovery_start
== MaxSector
)
2952 return sprintf(page
, "none\n");
2954 return sprintf(page
, "%llu\n", recovery_start
);
2957 static ssize_t
recovery_start_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
2959 unsigned long long recovery_start
;
2961 if (cmd_match(buf
, "none"))
2962 recovery_start
= MaxSector
;
2963 else if (strict_strtoull(buf
, 10, &recovery_start
))
2966 if (rdev
->mddev
->pers
&&
2967 rdev
->raid_disk
>= 0)
2970 rdev
->recovery_offset
= recovery_start
;
2971 if (recovery_start
== MaxSector
)
2972 set_bit(In_sync
, &rdev
->flags
);
2974 clear_bit(In_sync
, &rdev
->flags
);
2978 static struct rdev_sysfs_entry rdev_recovery_start
=
2979 __ATTR(recovery_start
, S_IRUGO
|S_IWUSR
, recovery_start_show
, recovery_start_store
);
2983 badblocks_show(struct badblocks
*bb
, char *page
, int unack
);
2985 badblocks_store(struct badblocks
*bb
, const char *page
, size_t len
, int unack
);
2987 static ssize_t
bb_show(struct md_rdev
*rdev
, char *page
)
2989 return badblocks_show(&rdev
->badblocks
, page
, 0);
2991 static ssize_t
bb_store(struct md_rdev
*rdev
, const char *page
, size_t len
)
2993 int rv
= badblocks_store(&rdev
->badblocks
, page
, len
, 0);
2994 /* Maybe that ack was all we needed */
2995 if (test_and_clear_bit(BlockedBadBlocks
, &rdev
->flags
))
2996 wake_up(&rdev
->blocked_wait
);
2999 static struct rdev_sysfs_entry rdev_bad_blocks
=
3000 __ATTR(bad_blocks
, S_IRUGO
|S_IWUSR
, bb_show
, bb_store
);
3003 static ssize_t
ubb_show(struct md_rdev
*rdev
, char *page
)
3005 return badblocks_show(&rdev
->badblocks
, page
, 1);
3007 static ssize_t
ubb_store(struct md_rdev
*rdev
, const char *page
, size_t len
)
3009 return badblocks_store(&rdev
->badblocks
, page
, len
, 1);
3011 static struct rdev_sysfs_entry rdev_unack_bad_blocks
=
3012 __ATTR(unacknowledged_bad_blocks
, S_IRUGO
|S_IWUSR
, ubb_show
, ubb_store
);
3014 static struct attribute
*rdev_default_attrs
[] = {
3020 &rdev_recovery_start
.attr
,
3021 &rdev_bad_blocks
.attr
,
3022 &rdev_unack_bad_blocks
.attr
,
3026 rdev_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
3028 struct rdev_sysfs_entry
*entry
= container_of(attr
, struct rdev_sysfs_entry
, attr
);
3029 struct md_rdev
*rdev
= container_of(kobj
, struct md_rdev
, kobj
);
3030 struct mddev
*mddev
= rdev
->mddev
;
3036 rv
= mddev
? mddev_lock(mddev
) : -EBUSY
;
3038 if (rdev
->mddev
== NULL
)
3041 rv
= entry
->show(rdev
, page
);
3042 mddev_unlock(mddev
);
3048 rdev_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
3049 const char *page
, size_t length
)
3051 struct rdev_sysfs_entry
*entry
= container_of(attr
, struct rdev_sysfs_entry
, attr
);
3052 struct md_rdev
*rdev
= container_of(kobj
, struct md_rdev
, kobj
);
3054 struct mddev
*mddev
= rdev
->mddev
;
3058 if (!capable(CAP_SYS_ADMIN
))
3060 rv
= mddev
? mddev_lock(mddev
): -EBUSY
;
3062 if (rdev
->mddev
== NULL
)
3065 rv
= entry
->store(rdev
, page
, length
);
3066 mddev_unlock(mddev
);
3071 static void rdev_free(struct kobject
*ko
)
3073 struct md_rdev
*rdev
= container_of(ko
, struct md_rdev
, kobj
);
3076 static const struct sysfs_ops rdev_sysfs_ops
= {
3077 .show
= rdev_attr_show
,
3078 .store
= rdev_attr_store
,
3080 static struct kobj_type rdev_ktype
= {
3081 .release
= rdev_free
,
3082 .sysfs_ops
= &rdev_sysfs_ops
,
3083 .default_attrs
= rdev_default_attrs
,
3086 int md_rdev_init(struct md_rdev
*rdev
)
3089 rdev
->saved_raid_disk
= -1;
3090 rdev
->raid_disk
= -1;
3092 rdev
->data_offset
= 0;
3093 rdev
->sb_events
= 0;
3094 rdev
->last_read_error
.tv_sec
= 0;
3095 rdev
->last_read_error
.tv_nsec
= 0;
3096 rdev
->sb_loaded
= 0;
3097 rdev
->bb_page
= NULL
;
3098 atomic_set(&rdev
->nr_pending
, 0);
3099 atomic_set(&rdev
->read_errors
, 0);
3100 atomic_set(&rdev
->corrected_errors
, 0);
3102 INIT_LIST_HEAD(&rdev
->same_set
);
3103 init_waitqueue_head(&rdev
->blocked_wait
);
3105 /* Add space to store bad block list.
3106 * This reserves the space even on arrays where it cannot
3107 * be used - I wonder if that matters
3109 rdev
->badblocks
.count
= 0;
3110 rdev
->badblocks
.shift
= -1; /* disabled until explicitly enabled */
3111 rdev
->badblocks
.page
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
3112 seqlock_init(&rdev
->badblocks
.lock
);
3113 if (rdev
->badblocks
.page
== NULL
)
3118 EXPORT_SYMBOL_GPL(md_rdev_init
);
3120 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3122 * mark the device faulty if:
3124 * - the device is nonexistent (zero size)
3125 * - the device has no valid superblock
3127 * a faulty rdev _never_ has rdev->sb set.
3129 static struct md_rdev
*md_import_device(dev_t newdev
, int super_format
, int super_minor
)
3131 char b
[BDEVNAME_SIZE
];
3133 struct md_rdev
*rdev
;
3136 rdev
= kzalloc(sizeof(*rdev
), GFP_KERNEL
);
3138 printk(KERN_ERR
"md: could not alloc mem for new device!\n");
3139 return ERR_PTR(-ENOMEM
);
3142 err
= md_rdev_init(rdev
);
3145 err
= alloc_disk_sb(rdev
);
3149 err
= lock_rdev(rdev
, newdev
, super_format
== -2);
3153 kobject_init(&rdev
->kobj
, &rdev_ktype
);
3155 size
= i_size_read(rdev
->bdev
->bd_inode
) >> BLOCK_SIZE_BITS
;
3158 "md: %s has zero or unknown size, marking faulty!\n",
3159 bdevname(rdev
->bdev
,b
));
3164 if (super_format
>= 0) {
3165 err
= super_types
[super_format
].
3166 load_super(rdev
, NULL
, super_minor
);
3167 if (err
== -EINVAL
) {
3169 "md: %s does not have a valid v%d.%d "
3170 "superblock, not importing!\n",
3171 bdevname(rdev
->bdev
,b
),
3172 super_format
, super_minor
);
3177 "md: could not read %s's sb, not importing!\n",
3178 bdevname(rdev
->bdev
,b
));
3189 kfree(rdev
->badblocks
.page
);
3191 return ERR_PTR(err
);
3195 * Check a full RAID array for plausibility
3199 static void analyze_sbs(struct mddev
* mddev
)
3202 struct md_rdev
*rdev
, *freshest
, *tmp
;
3203 char b
[BDEVNAME_SIZE
];
3206 rdev_for_each_safe(rdev
, tmp
, mddev
)
3207 switch (super_types
[mddev
->major_version
].
3208 load_super(rdev
, freshest
, mddev
->minor_version
)) {
3216 "md: fatal superblock inconsistency in %s"
3217 " -- removing from array\n",
3218 bdevname(rdev
->bdev
,b
));
3219 kick_rdev_from_array(rdev
);
3223 super_types
[mddev
->major_version
].
3224 validate_super(mddev
, freshest
);
3227 rdev_for_each_safe(rdev
, tmp
, mddev
) {
3228 if (mddev
->max_disks
&&
3229 (rdev
->desc_nr
>= mddev
->max_disks
||
3230 i
> mddev
->max_disks
)) {
3232 "md: %s: %s: only %d devices permitted\n",
3233 mdname(mddev
), bdevname(rdev
->bdev
, b
),
3235 kick_rdev_from_array(rdev
);
3238 if (rdev
!= freshest
)
3239 if (super_types
[mddev
->major_version
].
3240 validate_super(mddev
, rdev
)) {
3241 printk(KERN_WARNING
"md: kicking non-fresh %s"
3243 bdevname(rdev
->bdev
,b
));
3244 kick_rdev_from_array(rdev
);
3247 if (mddev
->level
== LEVEL_MULTIPATH
) {
3248 rdev
->desc_nr
= i
++;
3249 rdev
->raid_disk
= rdev
->desc_nr
;
3250 set_bit(In_sync
, &rdev
->flags
);
3251 } else if (rdev
->raid_disk
>= (mddev
->raid_disks
- min(0, mddev
->delta_disks
))) {
3252 rdev
->raid_disk
= -1;
3253 clear_bit(In_sync
, &rdev
->flags
);
3258 /* Read a fixed-point number.
3259 * Numbers in sysfs attributes should be in "standard" units where
3260 * possible, so time should be in seconds.
3261 * However we internally use a a much smaller unit such as
3262 * milliseconds or jiffies.
3263 * This function takes a decimal number with a possible fractional
3264 * component, and produces an integer which is the result of
3265 * multiplying that number by 10^'scale'.
3266 * all without any floating-point arithmetic.
3268 int strict_strtoul_scaled(const char *cp
, unsigned long *res
, int scale
)
3270 unsigned long result
= 0;
3272 while (isdigit(*cp
) || (*cp
== '.' && decimals
< 0)) {
3275 else if (decimals
< scale
) {
3278 result
= result
* 10 + value
;
3290 while (decimals
< scale
) {
3299 static void md_safemode_timeout(unsigned long data
);
3302 safe_delay_show(struct mddev
*mddev
, char *page
)
3304 int msec
= (mddev
->safemode_delay
*1000)/HZ
;
3305 return sprintf(page
, "%d.%03d\n", msec
/1000, msec
%1000);
3308 safe_delay_store(struct mddev
*mddev
, const char *cbuf
, size_t len
)
3312 if (strict_strtoul_scaled(cbuf
, &msec
, 3) < 0)
3315 mddev
->safemode_delay
= 0;
3317 unsigned long old_delay
= mddev
->safemode_delay
;
3318 mddev
->safemode_delay
= (msec
*HZ
)/1000;
3319 if (mddev
->safemode_delay
== 0)
3320 mddev
->safemode_delay
= 1;
3321 if (mddev
->safemode_delay
< old_delay
)
3322 md_safemode_timeout((unsigned long)mddev
);
3326 static struct md_sysfs_entry md_safe_delay
=
3327 __ATTR(safe_mode_delay
, S_IRUGO
|S_IWUSR
,safe_delay_show
, safe_delay_store
);
3330 level_show(struct mddev
*mddev
, char *page
)
3332 struct md_personality
*p
= mddev
->pers
;
3334 return sprintf(page
, "%s\n", p
->name
);
3335 else if (mddev
->clevel
[0])
3336 return sprintf(page
, "%s\n", mddev
->clevel
);
3337 else if (mddev
->level
!= LEVEL_NONE
)
3338 return sprintf(page
, "%d\n", mddev
->level
);
3344 level_store(struct mddev
*mddev
, const char *buf
, size_t len
)
3348 struct md_personality
*pers
;
3351 struct md_rdev
*rdev
;
3353 if (mddev
->pers
== NULL
) {
3356 if (len
>= sizeof(mddev
->clevel
))
3358 strncpy(mddev
->clevel
, buf
, len
);
3359 if (mddev
->clevel
[len
-1] == '\n')
3361 mddev
->clevel
[len
] = 0;
3362 mddev
->level
= LEVEL_NONE
;
3366 /* request to change the personality. Need to ensure:
3367 * - array is not engaged in resync/recovery/reshape
3368 * - old personality can be suspended
3369 * - new personality will access other array.
3372 if (mddev
->sync_thread
||
3373 mddev
->reshape_position
!= MaxSector
||
3374 mddev
->sysfs_active
)
3377 if (!mddev
->pers
->quiesce
) {
3378 printk(KERN_WARNING
"md: %s: %s does not support online personality change\n",
3379 mdname(mddev
), mddev
->pers
->name
);
3383 /* Now find the new personality */
3384 if (len
== 0 || len
>= sizeof(clevel
))
3386 strncpy(clevel
, buf
, len
);
3387 if (clevel
[len
-1] == '\n')
3390 if (strict_strtol(clevel
, 10, &level
))
3393 if (request_module("md-%s", clevel
) != 0)
3394 request_module("md-level-%s", clevel
);
3395 spin_lock(&pers_lock
);
3396 pers
= find_pers(level
, clevel
);
3397 if (!pers
|| !try_module_get(pers
->owner
)) {
3398 spin_unlock(&pers_lock
);
3399 printk(KERN_WARNING
"md: personality %s not loaded\n", clevel
);
3402 spin_unlock(&pers_lock
);
3404 if (pers
== mddev
->pers
) {
3405 /* Nothing to do! */
3406 module_put(pers
->owner
);
3409 if (!pers
->takeover
) {
3410 module_put(pers
->owner
);
3411 printk(KERN_WARNING
"md: %s: %s does not support personality takeover\n",
3412 mdname(mddev
), clevel
);
3416 rdev_for_each(rdev
, mddev
)
3417 rdev
->new_raid_disk
= rdev
->raid_disk
;
3419 /* ->takeover must set new_* and/or delta_disks
3420 * if it succeeds, and may set them when it fails.
3422 priv
= pers
->takeover(mddev
);
3424 mddev
->new_level
= mddev
->level
;
3425 mddev
->new_layout
= mddev
->layout
;
3426 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
3427 mddev
->raid_disks
-= mddev
->delta_disks
;
3428 mddev
->delta_disks
= 0;
3429 module_put(pers
->owner
);
3430 printk(KERN_WARNING
"md: %s: %s would not accept array\n",
3431 mdname(mddev
), clevel
);
3432 return PTR_ERR(priv
);
3435 /* Looks like we have a winner */
3436 mddev_suspend(mddev
);
3437 mddev
->pers
->stop(mddev
);
3439 if (mddev
->pers
->sync_request
== NULL
&&
3440 pers
->sync_request
!= NULL
) {
3441 /* need to add the md_redundancy_group */
3442 if (sysfs_create_group(&mddev
->kobj
, &md_redundancy_group
))
3444 "md: cannot register extra attributes for %s\n",
3446 mddev
->sysfs_action
= sysfs_get_dirent(mddev
->kobj
.sd
, NULL
, "sync_action");
3448 if (mddev
->pers
->sync_request
!= NULL
&&
3449 pers
->sync_request
== NULL
) {
3450 /* need to remove the md_redundancy_group */
3451 if (mddev
->to_remove
== NULL
)
3452 mddev
->to_remove
= &md_redundancy_group
;
3455 if (mddev
->pers
->sync_request
== NULL
&&
3457 /* We are converting from a no-redundancy array
3458 * to a redundancy array and metadata is managed
3459 * externally so we need to be sure that writes
3460 * won't block due to a need to transition
3462 * until external management is started.
3465 mddev
->safemode_delay
= 0;
3466 mddev
->safemode
= 0;
3469 rdev_for_each(rdev
, mddev
) {
3470 if (rdev
->raid_disk
< 0)
3472 if (rdev
->new_raid_disk
>= mddev
->raid_disks
)
3473 rdev
->new_raid_disk
= -1;
3474 if (rdev
->new_raid_disk
== rdev
->raid_disk
)
3476 sysfs_unlink_rdev(mddev
, rdev
);
3478 rdev_for_each(rdev
, mddev
) {
3479 if (rdev
->raid_disk
< 0)
3481 if (rdev
->new_raid_disk
== rdev
->raid_disk
)
3483 rdev
->raid_disk
= rdev
->new_raid_disk
;
3484 if (rdev
->raid_disk
< 0)
3485 clear_bit(In_sync
, &rdev
->flags
);
3487 if (sysfs_link_rdev(mddev
, rdev
))
3488 printk(KERN_WARNING
"md: cannot register rd%d"
3489 " for %s after level change\n",
3490 rdev
->raid_disk
, mdname(mddev
));
3494 module_put(mddev
->pers
->owner
);
3496 mddev
->private = priv
;
3497 strlcpy(mddev
->clevel
, pers
->name
, sizeof(mddev
->clevel
));
3498 mddev
->level
= mddev
->new_level
;
3499 mddev
->layout
= mddev
->new_layout
;
3500 mddev
->chunk_sectors
= mddev
->new_chunk_sectors
;
3501 mddev
->delta_disks
= 0;
3502 mddev
->degraded
= 0;
3503 if (mddev
->pers
->sync_request
== NULL
) {
3504 /* this is now an array without redundancy, so
3505 * it must always be in_sync
3508 del_timer_sync(&mddev
->safemode_timer
);
3510 blk_set_stacking_limits(&mddev
->queue
->limits
);
3512 mddev_resume(mddev
);
3513 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
3514 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3515 md_wakeup_thread(mddev
->thread
);
3516 sysfs_notify(&mddev
->kobj
, NULL
, "level");
3517 md_new_event(mddev
);
3521 static struct md_sysfs_entry md_level
=
3522 __ATTR(level
, S_IRUGO
|S_IWUSR
, level_show
, level_store
);
3526 layout_show(struct mddev
*mddev
, char *page
)
3528 /* just a number, not meaningful for all levels */
3529 if (mddev
->reshape_position
!= MaxSector
&&
3530 mddev
->layout
!= mddev
->new_layout
)
3531 return sprintf(page
, "%d (%d)\n",
3532 mddev
->new_layout
, mddev
->layout
);
3533 return sprintf(page
, "%d\n", mddev
->layout
);
3537 layout_store(struct mddev
*mddev
, const char *buf
, size_t len
)
3540 unsigned long n
= simple_strtoul(buf
, &e
, 10);
3542 if (!*buf
|| (*e
&& *e
!= '\n'))
3547 if (mddev
->pers
->check_reshape
== NULL
)
3549 mddev
->new_layout
= n
;
3550 err
= mddev
->pers
->check_reshape(mddev
);
3552 mddev
->new_layout
= mddev
->layout
;
3556 mddev
->new_layout
= n
;
3557 if (mddev
->reshape_position
== MaxSector
)
3562 static struct md_sysfs_entry md_layout
=
3563 __ATTR(layout
, S_IRUGO
|S_IWUSR
, layout_show
, layout_store
);
3567 raid_disks_show(struct mddev
*mddev
, char *page
)
3569 if (mddev
->raid_disks
== 0)
3571 if (mddev
->reshape_position
!= MaxSector
&&
3572 mddev
->delta_disks
!= 0)
3573 return sprintf(page
, "%d (%d)\n", mddev
->raid_disks
,
3574 mddev
->raid_disks
- mddev
->delta_disks
);
3575 return sprintf(page
, "%d\n", mddev
->raid_disks
);
3578 static int update_raid_disks(struct mddev
*mddev
, int raid_disks
);
3581 raid_disks_store(struct mddev
*mddev
, const char *buf
, size_t len
)
3585 unsigned long n
= simple_strtoul(buf
, &e
, 10);
3587 if (!*buf
|| (*e
&& *e
!= '\n'))
3591 rv
= update_raid_disks(mddev
, n
);
3592 else if (mddev
->reshape_position
!= MaxSector
) {
3593 int olddisks
= mddev
->raid_disks
- mddev
->delta_disks
;
3594 mddev
->delta_disks
= n
- olddisks
;
3595 mddev
->raid_disks
= n
;
3597 mddev
->raid_disks
= n
;
3598 return rv
? rv
: len
;
3600 static struct md_sysfs_entry md_raid_disks
=
3601 __ATTR(raid_disks
, S_IRUGO
|S_IWUSR
, raid_disks_show
, raid_disks_store
);
3604 chunk_size_show(struct mddev
*mddev
, char *page
)
3606 if (mddev
->reshape_position
!= MaxSector
&&
3607 mddev
->chunk_sectors
!= mddev
->new_chunk_sectors
)
3608 return sprintf(page
, "%d (%d)\n",
3609 mddev
->new_chunk_sectors
<< 9,
3610 mddev
->chunk_sectors
<< 9);
3611 return sprintf(page
, "%d\n", mddev
->chunk_sectors
<< 9);
3615 chunk_size_store(struct mddev
*mddev
, const char *buf
, size_t len
)
3618 unsigned long n
= simple_strtoul(buf
, &e
, 10);
3620 if (!*buf
|| (*e
&& *e
!= '\n'))
3625 if (mddev
->pers
->check_reshape
== NULL
)
3627 mddev
->new_chunk_sectors
= n
>> 9;
3628 err
= mddev
->pers
->check_reshape(mddev
);
3630 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
3634 mddev
->new_chunk_sectors
= n
>> 9;
3635 if (mddev
->reshape_position
== MaxSector
)
3636 mddev
->chunk_sectors
= n
>> 9;
3640 static struct md_sysfs_entry md_chunk_size
=
3641 __ATTR(chunk_size
, S_IRUGO
|S_IWUSR
, chunk_size_show
, chunk_size_store
);
3644 resync_start_show(struct mddev
*mddev
, char *page
)
3646 if (mddev
->recovery_cp
== MaxSector
)
3647 return sprintf(page
, "none\n");
3648 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->recovery_cp
);
3652 resync_start_store(struct mddev
*mddev
, const char *buf
, size_t len
)
3655 unsigned long long n
= simple_strtoull(buf
, &e
, 10);
3657 if (mddev
->pers
&& !test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
))
3659 if (cmd_match(buf
, "none"))
3661 else if (!*buf
|| (*e
&& *e
!= '\n'))
3664 mddev
->recovery_cp
= n
;
3667 static struct md_sysfs_entry md_resync_start
=
3668 __ATTR(resync_start
, S_IRUGO
|S_IWUSR
, resync_start_show
, resync_start_store
);
3671 * The array state can be:
3674 * No devices, no size, no level
3675 * Equivalent to STOP_ARRAY ioctl
3677 * May have some settings, but array is not active
3678 * all IO results in error
3679 * When written, doesn't tear down array, but just stops it
3680 * suspended (not supported yet)
3681 * All IO requests will block. The array can be reconfigured.
3682 * Writing this, if accepted, will block until array is quiescent
3684 * no resync can happen. no superblocks get written.
3685 * write requests fail
3687 * like readonly, but behaves like 'clean' on a write request.
3689 * clean - no pending writes, but otherwise active.
3690 * When written to inactive array, starts without resync
3691 * If a write request arrives then
3692 * if metadata is known, mark 'dirty' and switch to 'active'.
3693 * if not known, block and switch to write-pending
3694 * If written to an active array that has pending writes, then fails.
3696 * fully active: IO and resync can be happening.
3697 * When written to inactive array, starts with resync
3700 * clean, but writes are blocked waiting for 'active' to be written.
3703 * like active, but no writes have been seen for a while (100msec).
3706 enum array_state
{ clear
, inactive
, suspended
, readonly
, read_auto
, clean
, active
,
3707 write_pending
, active_idle
, bad_word
};
3708 static char *array_states
[] = {
3709 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3710 "write-pending", "active-idle", NULL
};
3712 static int match_word(const char *word
, char **list
)
3715 for (n
=0; list
[n
]; n
++)
3716 if (cmd_match(word
, list
[n
]))
3722 array_state_show(struct mddev
*mddev
, char *page
)
3724 enum array_state st
= inactive
;
3737 else if (test_bit(MD_CHANGE_PENDING
, &mddev
->flags
))
3739 else if (mddev
->safemode
)
3745 if (list_empty(&mddev
->disks
) &&
3746 mddev
->raid_disks
== 0 &&
3747 mddev
->dev_sectors
== 0)
3752 return sprintf(page
, "%s\n", array_states
[st
]);
3755 static int do_md_stop(struct mddev
* mddev
, int ro
, struct block_device
*bdev
);
3756 static int md_set_readonly(struct mddev
* mddev
, struct block_device
*bdev
);
3757 static int do_md_run(struct mddev
* mddev
);
3758 static int restart_array(struct mddev
*mddev
);
3761 array_state_store(struct mddev
*mddev
, const char *buf
, size_t len
)
3764 enum array_state st
= match_word(buf
, array_states
);
3769 /* stopping an active array */
3770 if (atomic_read(&mddev
->openers
) > 0)
3772 err
= do_md_stop(mddev
, 0, NULL
);
3775 /* stopping an active array */
3777 if (atomic_read(&mddev
->openers
) > 0)
3779 err
= do_md_stop(mddev
, 2, NULL
);
3781 err
= 0; /* already inactive */
3784 break; /* not supported yet */
3787 err
= md_set_readonly(mddev
, NULL
);
3790 set_disk_ro(mddev
->gendisk
, 1);
3791 err
= do_md_run(mddev
);
3797 err
= md_set_readonly(mddev
, NULL
);
3798 else if (mddev
->ro
== 1)
3799 err
= restart_array(mddev
);
3802 set_disk_ro(mddev
->gendisk
, 0);
3806 err
= do_md_run(mddev
);
3811 restart_array(mddev
);
3812 spin_lock_irq(&mddev
->write_lock
);
3813 if (atomic_read(&mddev
->writes_pending
) == 0) {
3814 if (mddev
->in_sync
== 0) {
3816 if (mddev
->safemode
== 1)
3817 mddev
->safemode
= 0;
3818 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
3823 spin_unlock_irq(&mddev
->write_lock
);
3829 restart_array(mddev
);
3830 clear_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
3831 wake_up(&mddev
->sb_wait
);
3835 set_disk_ro(mddev
->gendisk
, 0);
3836 err
= do_md_run(mddev
);
3841 /* these cannot be set */
3847 if (mddev
->hold_active
== UNTIL_IOCTL
)
3848 mddev
->hold_active
= 0;
3849 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
3853 static struct md_sysfs_entry md_array_state
=
3854 __ATTR(array_state
, S_IRUGO
|S_IWUSR
, array_state_show
, array_state_store
);
3857 max_corrected_read_errors_show(struct mddev
*mddev
, char *page
) {
3858 return sprintf(page
, "%d\n",
3859 atomic_read(&mddev
->max_corr_read_errors
));
3863 max_corrected_read_errors_store(struct mddev
*mddev
, const char *buf
, size_t len
)
3866 unsigned long n
= simple_strtoul(buf
, &e
, 10);
3868 if (*buf
&& (*e
== 0 || *e
== '\n')) {
3869 atomic_set(&mddev
->max_corr_read_errors
, n
);
3875 static struct md_sysfs_entry max_corr_read_errors
=
3876 __ATTR(max_read_errors
, S_IRUGO
|S_IWUSR
, max_corrected_read_errors_show
,
3877 max_corrected_read_errors_store
);
3880 null_show(struct mddev
*mddev
, char *page
)
3886 new_dev_store(struct mddev
*mddev
, const char *buf
, size_t len
)
3888 /* buf must be %d:%d\n? giving major and minor numbers */
3889 /* The new device is added to the array.
3890 * If the array has a persistent superblock, we read the
3891 * superblock to initialise info and check validity.
3892 * Otherwise, only checking done is that in bind_rdev_to_array,
3893 * which mainly checks size.
3896 int major
= simple_strtoul(buf
, &e
, 10);
3899 struct md_rdev
*rdev
;
3902 if (!*buf
|| *e
!= ':' || !e
[1] || e
[1] == '\n')
3904 minor
= simple_strtoul(e
+1, &e
, 10);
3905 if (*e
&& *e
!= '\n')
3907 dev
= MKDEV(major
, minor
);
3908 if (major
!= MAJOR(dev
) ||
3909 minor
!= MINOR(dev
))
3913 if (mddev
->persistent
) {
3914 rdev
= md_import_device(dev
, mddev
->major_version
,
3915 mddev
->minor_version
);
3916 if (!IS_ERR(rdev
) && !list_empty(&mddev
->disks
)) {
3917 struct md_rdev
*rdev0
3918 = list_entry(mddev
->disks
.next
,
3919 struct md_rdev
, same_set
);
3920 err
= super_types
[mddev
->major_version
]
3921 .load_super(rdev
, rdev0
, mddev
->minor_version
);
3925 } else if (mddev
->external
)
3926 rdev
= md_import_device(dev
, -2, -1);
3928 rdev
= md_import_device(dev
, -1, -1);
3931 return PTR_ERR(rdev
);
3932 err
= bind_rdev_to_array(rdev
, mddev
);
3936 return err
? err
: len
;
3939 static struct md_sysfs_entry md_new_device
=
3940 __ATTR(new_dev
, S_IWUSR
, null_show
, new_dev_store
);
3943 bitmap_store(struct mddev
*mddev
, const char *buf
, size_t len
)
3946 unsigned long chunk
, end_chunk
;
3950 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3952 chunk
= end_chunk
= simple_strtoul(buf
, &end
, 0);
3953 if (buf
== end
) break;
3954 if (*end
== '-') { /* range */
3956 end_chunk
= simple_strtoul(buf
, &end
, 0);
3957 if (buf
== end
) break;
3959 if (*end
&& !isspace(*end
)) break;
3960 bitmap_dirty_bits(mddev
->bitmap
, chunk
, end_chunk
);
3961 buf
= skip_spaces(end
);
3963 bitmap_unplug(mddev
->bitmap
); /* flush the bits to disk */
3968 static struct md_sysfs_entry md_bitmap
=
3969 __ATTR(bitmap_set_bits
, S_IWUSR
, null_show
, bitmap_store
);
3972 size_show(struct mddev
*mddev
, char *page
)
3974 return sprintf(page
, "%llu\n",
3975 (unsigned long long)mddev
->dev_sectors
/ 2);
3978 static int update_size(struct mddev
*mddev
, sector_t num_sectors
);
3981 size_store(struct mddev
*mddev
, const char *buf
, size_t len
)
3983 /* If array is inactive, we can reduce the component size, but
3984 * not increase it (except from 0).
3985 * If array is active, we can try an on-line resize
3988 int err
= strict_blocks_to_sectors(buf
, §ors
);
3993 err
= update_size(mddev
, sectors
);
3994 md_update_sb(mddev
, 1);
3996 if (mddev
->dev_sectors
== 0 ||
3997 mddev
->dev_sectors
> sectors
)
3998 mddev
->dev_sectors
= sectors
;
4002 return err
? err
: len
;
4005 static struct md_sysfs_entry md_size
=
4006 __ATTR(component_size
, S_IRUGO
|S_IWUSR
, size_show
, size_store
);
4011 * 'none' for arrays with no metadata (good luck...)
4012 * 'external' for arrays with externally managed metadata,
4013 * or N.M for internally known formats
4016 metadata_show(struct mddev
*mddev
, char *page
)
4018 if (mddev
->persistent
)
4019 return sprintf(page
, "%d.%d\n",
4020 mddev
->major_version
, mddev
->minor_version
);
4021 else if (mddev
->external
)
4022 return sprintf(page
, "external:%s\n", mddev
->metadata_type
);
4024 return sprintf(page
, "none\n");
4028 metadata_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4032 /* Changing the details of 'external' metadata is
4033 * always permitted. Otherwise there must be
4034 * no devices attached to the array.
4036 if (mddev
->external
&& strncmp(buf
, "external:", 9) == 0)
4038 else if (!list_empty(&mddev
->disks
))
4041 if (cmd_match(buf
, "none")) {
4042 mddev
->persistent
= 0;
4043 mddev
->external
= 0;
4044 mddev
->major_version
= 0;
4045 mddev
->minor_version
= 90;
4048 if (strncmp(buf
, "external:", 9) == 0) {
4049 size_t namelen
= len
-9;
4050 if (namelen
>= sizeof(mddev
->metadata_type
))
4051 namelen
= sizeof(mddev
->metadata_type
)-1;
4052 strncpy(mddev
->metadata_type
, buf
+9, namelen
);
4053 mddev
->metadata_type
[namelen
] = 0;
4054 if (namelen
&& mddev
->metadata_type
[namelen
-1] == '\n')
4055 mddev
->metadata_type
[--namelen
] = 0;
4056 mddev
->persistent
= 0;
4057 mddev
->external
= 1;
4058 mddev
->major_version
= 0;
4059 mddev
->minor_version
= 90;
4062 major
= simple_strtoul(buf
, &e
, 10);
4063 if (e
==buf
|| *e
!= '.')
4066 minor
= simple_strtoul(buf
, &e
, 10);
4067 if (e
==buf
|| (*e
&& *e
!= '\n') )
4069 if (major
>= ARRAY_SIZE(super_types
) || super_types
[major
].name
== NULL
)
4071 mddev
->major_version
= major
;
4072 mddev
->minor_version
= minor
;
4073 mddev
->persistent
= 1;
4074 mddev
->external
= 0;
4078 static struct md_sysfs_entry md_metadata
=
4079 __ATTR(metadata_version
, S_IRUGO
|S_IWUSR
, metadata_show
, metadata_store
);
4082 action_show(struct mddev
*mddev
, char *page
)
4084 char *type
= "idle";
4085 if (test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
))
4087 else if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
4088 (!mddev
->ro
&& test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))) {
4089 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
4091 else if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
4092 if (!test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
4094 else if (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
))
4098 } else if (test_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
))
4101 return sprintf(page
, "%s\n", type
);
4104 static void reap_sync_thread(struct mddev
*mddev
);
4107 action_store(struct mddev
*mddev
, const char *page
, size_t len
)
4109 if (!mddev
->pers
|| !mddev
->pers
->sync_request
)
4112 if (cmd_match(page
, "frozen"))
4113 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4115 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4117 if (cmd_match(page
, "idle") || cmd_match(page
, "frozen")) {
4118 if (mddev
->sync_thread
) {
4119 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
4120 reap_sync_thread(mddev
);
4122 } else if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
4123 test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))
4125 else if (cmd_match(page
, "resync"))
4126 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4127 else if (cmd_match(page
, "recover")) {
4128 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
4129 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4130 } else if (cmd_match(page
, "reshape")) {
4132 if (mddev
->pers
->start_reshape
== NULL
)
4134 err
= mddev
->pers
->start_reshape(mddev
);
4137 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
4139 if (cmd_match(page
, "check"))
4140 set_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
4141 else if (!cmd_match(page
, "repair"))
4143 set_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
4144 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
4146 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4147 md_wakeup_thread(mddev
->thread
);
4148 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
4153 mismatch_cnt_show(struct mddev
*mddev
, char *page
)
4155 return sprintf(page
, "%llu\n",
4156 (unsigned long long) mddev
->resync_mismatches
);
4159 static struct md_sysfs_entry md_scan_mode
=
4160 __ATTR(sync_action
, S_IRUGO
|S_IWUSR
, action_show
, action_store
);
4163 static struct md_sysfs_entry md_mismatches
= __ATTR_RO(mismatch_cnt
);
4166 sync_min_show(struct mddev
*mddev
, char *page
)
4168 return sprintf(page
, "%d (%s)\n", speed_min(mddev
),
4169 mddev
->sync_speed_min
? "local": "system");
4173 sync_min_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4177 if (strncmp(buf
, "system", 6)==0) {
4178 mddev
->sync_speed_min
= 0;
4181 min
= simple_strtoul(buf
, &e
, 10);
4182 if (buf
== e
|| (*e
&& *e
!= '\n') || min
<= 0)
4184 mddev
->sync_speed_min
= min
;
4188 static struct md_sysfs_entry md_sync_min
=
4189 __ATTR(sync_speed_min
, S_IRUGO
|S_IWUSR
, sync_min_show
, sync_min_store
);
4192 sync_max_show(struct mddev
*mddev
, char *page
)
4194 return sprintf(page
, "%d (%s)\n", speed_max(mddev
),
4195 mddev
->sync_speed_max
? "local": "system");
4199 sync_max_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4203 if (strncmp(buf
, "system", 6)==0) {
4204 mddev
->sync_speed_max
= 0;
4207 max
= simple_strtoul(buf
, &e
, 10);
4208 if (buf
== e
|| (*e
&& *e
!= '\n') || max
<= 0)
4210 mddev
->sync_speed_max
= max
;
4214 static struct md_sysfs_entry md_sync_max
=
4215 __ATTR(sync_speed_max
, S_IRUGO
|S_IWUSR
, sync_max_show
, sync_max_store
);
4218 degraded_show(struct mddev
*mddev
, char *page
)
4220 return sprintf(page
, "%d\n", mddev
->degraded
);
4222 static struct md_sysfs_entry md_degraded
= __ATTR_RO(degraded
);
4225 sync_force_parallel_show(struct mddev
*mddev
, char *page
)
4227 return sprintf(page
, "%d\n", mddev
->parallel_resync
);
4231 sync_force_parallel_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4235 if (strict_strtol(buf
, 10, &n
))
4238 if (n
!= 0 && n
!= 1)
4241 mddev
->parallel_resync
= n
;
4243 if (mddev
->sync_thread
)
4244 wake_up(&resync_wait
);
4249 /* force parallel resync, even with shared block devices */
4250 static struct md_sysfs_entry md_sync_force_parallel
=
4251 __ATTR(sync_force_parallel
, S_IRUGO
|S_IWUSR
,
4252 sync_force_parallel_show
, sync_force_parallel_store
);
4255 sync_speed_show(struct mddev
*mddev
, char *page
)
4257 unsigned long resync
, dt
, db
;
4258 if (mddev
->curr_resync
== 0)
4259 return sprintf(page
, "none\n");
4260 resync
= mddev
->curr_mark_cnt
- atomic_read(&mddev
->recovery_active
);
4261 dt
= (jiffies
- mddev
->resync_mark
) / HZ
;
4263 db
= resync
- mddev
->resync_mark_cnt
;
4264 return sprintf(page
, "%lu\n", db
/dt
/2); /* K/sec */
4267 static struct md_sysfs_entry md_sync_speed
= __ATTR_RO(sync_speed
);
4270 sync_completed_show(struct mddev
*mddev
, char *page
)
4272 unsigned long long max_sectors
, resync
;
4274 if (!test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
4275 return sprintf(page
, "none\n");
4277 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
4278 max_sectors
= mddev
->resync_max_sectors
;
4280 max_sectors
= mddev
->dev_sectors
;
4282 resync
= mddev
->curr_resync_completed
;
4283 return sprintf(page
, "%llu / %llu\n", resync
, max_sectors
);
4286 static struct md_sysfs_entry md_sync_completed
= __ATTR_RO(sync_completed
);
4289 min_sync_show(struct mddev
*mddev
, char *page
)
4291 return sprintf(page
, "%llu\n",
4292 (unsigned long long)mddev
->resync_min
);
4295 min_sync_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4297 unsigned long long min
;
4298 if (strict_strtoull(buf
, 10, &min
))
4300 if (min
> mddev
->resync_max
)
4302 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
4305 /* Must be a multiple of chunk_size */
4306 if (mddev
->chunk_sectors
) {
4307 sector_t temp
= min
;
4308 if (sector_div(temp
, mddev
->chunk_sectors
))
4311 mddev
->resync_min
= min
;
4316 static struct md_sysfs_entry md_min_sync
=
4317 __ATTR(sync_min
, S_IRUGO
|S_IWUSR
, min_sync_show
, min_sync_store
);
4320 max_sync_show(struct mddev
*mddev
, char *page
)
4322 if (mddev
->resync_max
== MaxSector
)
4323 return sprintf(page
, "max\n");
4325 return sprintf(page
, "%llu\n",
4326 (unsigned long long)mddev
->resync_max
);
4329 max_sync_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4331 if (strncmp(buf
, "max", 3) == 0)
4332 mddev
->resync_max
= MaxSector
;
4334 unsigned long long max
;
4335 if (strict_strtoull(buf
, 10, &max
))
4337 if (max
< mddev
->resync_min
)
4339 if (max
< mddev
->resync_max
&&
4341 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
4344 /* Must be a multiple of chunk_size */
4345 if (mddev
->chunk_sectors
) {
4346 sector_t temp
= max
;
4347 if (sector_div(temp
, mddev
->chunk_sectors
))
4350 mddev
->resync_max
= max
;
4352 wake_up(&mddev
->recovery_wait
);
4356 static struct md_sysfs_entry md_max_sync
=
4357 __ATTR(sync_max
, S_IRUGO
|S_IWUSR
, max_sync_show
, max_sync_store
);
4360 suspend_lo_show(struct mddev
*mddev
, char *page
)
4362 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->suspend_lo
);
4366 suspend_lo_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4369 unsigned long long new = simple_strtoull(buf
, &e
, 10);
4370 unsigned long long old
= mddev
->suspend_lo
;
4372 if (mddev
->pers
== NULL
||
4373 mddev
->pers
->quiesce
== NULL
)
4375 if (buf
== e
|| (*e
&& *e
!= '\n'))
4378 mddev
->suspend_lo
= new;
4380 /* Shrinking suspended region */
4381 mddev
->pers
->quiesce(mddev
, 2);
4383 /* Expanding suspended region - need to wait */
4384 mddev
->pers
->quiesce(mddev
, 1);
4385 mddev
->pers
->quiesce(mddev
, 0);
4389 static struct md_sysfs_entry md_suspend_lo
=
4390 __ATTR(suspend_lo
, S_IRUGO
|S_IWUSR
, suspend_lo_show
, suspend_lo_store
);
4394 suspend_hi_show(struct mddev
*mddev
, char *page
)
4396 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->suspend_hi
);
4400 suspend_hi_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4403 unsigned long long new = simple_strtoull(buf
, &e
, 10);
4404 unsigned long long old
= mddev
->suspend_hi
;
4406 if (mddev
->pers
== NULL
||
4407 mddev
->pers
->quiesce
== NULL
)
4409 if (buf
== e
|| (*e
&& *e
!= '\n'))
4412 mddev
->suspend_hi
= new;
4414 /* Shrinking suspended region */
4415 mddev
->pers
->quiesce(mddev
, 2);
4417 /* Expanding suspended region - need to wait */
4418 mddev
->pers
->quiesce(mddev
, 1);
4419 mddev
->pers
->quiesce(mddev
, 0);
4423 static struct md_sysfs_entry md_suspend_hi
=
4424 __ATTR(suspend_hi
, S_IRUGO
|S_IWUSR
, suspend_hi_show
, suspend_hi_store
);
4427 reshape_position_show(struct mddev
*mddev
, char *page
)
4429 if (mddev
->reshape_position
!= MaxSector
)
4430 return sprintf(page
, "%llu\n",
4431 (unsigned long long)mddev
->reshape_position
);
4432 strcpy(page
, "none\n");
4437 reshape_position_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4440 unsigned long long new = simple_strtoull(buf
, &e
, 10);
4443 if (buf
== e
|| (*e
&& *e
!= '\n'))
4445 mddev
->reshape_position
= new;
4446 mddev
->delta_disks
= 0;
4447 mddev
->new_level
= mddev
->level
;
4448 mddev
->new_layout
= mddev
->layout
;
4449 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
4453 static struct md_sysfs_entry md_reshape_position
=
4454 __ATTR(reshape_position
, S_IRUGO
|S_IWUSR
, reshape_position_show
,
4455 reshape_position_store
);
4458 array_size_show(struct mddev
*mddev
, char *page
)
4460 if (mddev
->external_size
)
4461 return sprintf(page
, "%llu\n",
4462 (unsigned long long)mddev
->array_sectors
/2);
4464 return sprintf(page
, "default\n");
4468 array_size_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4472 if (strncmp(buf
, "default", 7) == 0) {
4474 sectors
= mddev
->pers
->size(mddev
, 0, 0);
4476 sectors
= mddev
->array_sectors
;
4478 mddev
->external_size
= 0;
4480 if (strict_blocks_to_sectors(buf
, §ors
) < 0)
4482 if (mddev
->pers
&& mddev
->pers
->size(mddev
, 0, 0) < sectors
)
4485 mddev
->external_size
= 1;
4488 mddev
->array_sectors
= sectors
;
4490 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
4491 revalidate_disk(mddev
->gendisk
);
4496 static struct md_sysfs_entry md_array_size
=
4497 __ATTR(array_size
, S_IRUGO
|S_IWUSR
, array_size_show
,
4500 static struct attribute
*md_default_attrs
[] = {
4503 &md_raid_disks
.attr
,
4504 &md_chunk_size
.attr
,
4506 &md_resync_start
.attr
,
4508 &md_new_device
.attr
,
4509 &md_safe_delay
.attr
,
4510 &md_array_state
.attr
,
4511 &md_reshape_position
.attr
,
4512 &md_array_size
.attr
,
4513 &max_corr_read_errors
.attr
,
4517 static struct attribute
*md_redundancy_attrs
[] = {
4519 &md_mismatches
.attr
,
4522 &md_sync_speed
.attr
,
4523 &md_sync_force_parallel
.attr
,
4524 &md_sync_completed
.attr
,
4527 &md_suspend_lo
.attr
,
4528 &md_suspend_hi
.attr
,
4533 static struct attribute_group md_redundancy_group
= {
4535 .attrs
= md_redundancy_attrs
,
4540 md_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
4542 struct md_sysfs_entry
*entry
= container_of(attr
, struct md_sysfs_entry
, attr
);
4543 struct mddev
*mddev
= container_of(kobj
, struct mddev
, kobj
);
4548 spin_lock(&all_mddevs_lock
);
4549 if (list_empty(&mddev
->all_mddevs
)) {
4550 spin_unlock(&all_mddevs_lock
);
4554 spin_unlock(&all_mddevs_lock
);
4556 rv
= mddev_lock(mddev
);
4558 rv
= entry
->show(mddev
, page
);
4559 mddev_unlock(mddev
);
4566 md_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
4567 const char *page
, size_t length
)
4569 struct md_sysfs_entry
*entry
= container_of(attr
, struct md_sysfs_entry
, attr
);
4570 struct mddev
*mddev
= container_of(kobj
, struct mddev
, kobj
);
4575 if (!capable(CAP_SYS_ADMIN
))
4577 spin_lock(&all_mddevs_lock
);
4578 if (list_empty(&mddev
->all_mddevs
)) {
4579 spin_unlock(&all_mddevs_lock
);
4583 spin_unlock(&all_mddevs_lock
);
4584 rv
= mddev_lock(mddev
);
4586 rv
= entry
->store(mddev
, page
, length
);
4587 mddev_unlock(mddev
);
4593 static void md_free(struct kobject
*ko
)
4595 struct mddev
*mddev
= container_of(ko
, struct mddev
, kobj
);
4597 if (mddev
->sysfs_state
)
4598 sysfs_put(mddev
->sysfs_state
);
4600 if (mddev
->gendisk
) {
4601 del_gendisk(mddev
->gendisk
);
4602 put_disk(mddev
->gendisk
);
4605 blk_cleanup_queue(mddev
->queue
);
4610 static const struct sysfs_ops md_sysfs_ops
= {
4611 .show
= md_attr_show
,
4612 .store
= md_attr_store
,
4614 static struct kobj_type md_ktype
= {
4616 .sysfs_ops
= &md_sysfs_ops
,
4617 .default_attrs
= md_default_attrs
,
4622 static void mddev_delayed_delete(struct work_struct
*ws
)
4624 struct mddev
*mddev
= container_of(ws
, struct mddev
, del_work
);
4626 sysfs_remove_group(&mddev
->kobj
, &md_bitmap_group
);
4627 kobject_del(&mddev
->kobj
);
4628 kobject_put(&mddev
->kobj
);
4631 static int md_alloc(dev_t dev
, char *name
)
4633 static DEFINE_MUTEX(disks_mutex
);
4634 struct mddev
*mddev
= mddev_find(dev
);
4635 struct gendisk
*disk
;
4644 partitioned
= (MAJOR(mddev
->unit
) != MD_MAJOR
);
4645 shift
= partitioned
? MdpMinorShift
: 0;
4646 unit
= MINOR(mddev
->unit
) >> shift
;
4648 /* wait for any previous instance of this device to be
4649 * completely removed (mddev_delayed_delete).
4651 flush_workqueue(md_misc_wq
);
4653 mutex_lock(&disks_mutex
);
4659 /* Need to ensure that 'name' is not a duplicate.
4661 struct mddev
*mddev2
;
4662 spin_lock(&all_mddevs_lock
);
4664 list_for_each_entry(mddev2
, &all_mddevs
, all_mddevs
)
4665 if (mddev2
->gendisk
&&
4666 strcmp(mddev2
->gendisk
->disk_name
, name
) == 0) {
4667 spin_unlock(&all_mddevs_lock
);
4670 spin_unlock(&all_mddevs_lock
);
4674 mddev
->queue
= blk_alloc_queue(GFP_KERNEL
);
4677 mddev
->queue
->queuedata
= mddev
;
4679 blk_queue_make_request(mddev
->queue
, md_make_request
);
4680 blk_set_stacking_limits(&mddev
->queue
->limits
);
4682 disk
= alloc_disk(1 << shift
);
4684 blk_cleanup_queue(mddev
->queue
);
4685 mddev
->queue
= NULL
;
4688 disk
->major
= MAJOR(mddev
->unit
);
4689 disk
->first_minor
= unit
<< shift
;
4691 strcpy(disk
->disk_name
, name
);
4692 else if (partitioned
)
4693 sprintf(disk
->disk_name
, "md_d%d", unit
);
4695 sprintf(disk
->disk_name
, "md%d", unit
);
4696 disk
->fops
= &md_fops
;
4697 disk
->private_data
= mddev
;
4698 disk
->queue
= mddev
->queue
;
4699 blk_queue_flush(mddev
->queue
, REQ_FLUSH
| REQ_FUA
);
4700 /* Allow extended partitions. This makes the
4701 * 'mdp' device redundant, but we can't really
4704 disk
->flags
|= GENHD_FL_EXT_DEVT
;
4705 mddev
->gendisk
= disk
;
4706 /* As soon as we call add_disk(), another thread could get
4707 * through to md_open, so make sure it doesn't get too far
4709 mutex_lock(&mddev
->open_mutex
);
4712 error
= kobject_init_and_add(&mddev
->kobj
, &md_ktype
,
4713 &disk_to_dev(disk
)->kobj
, "%s", "md");
4715 /* This isn't possible, but as kobject_init_and_add is marked
4716 * __must_check, we must do something with the result
4718 printk(KERN_WARNING
"md: cannot register %s/md - name in use\n",
4722 if (mddev
->kobj
.sd
&&
4723 sysfs_create_group(&mddev
->kobj
, &md_bitmap_group
))
4724 printk(KERN_DEBUG
"pointless warning\n");
4725 mutex_unlock(&mddev
->open_mutex
);
4727 mutex_unlock(&disks_mutex
);
4728 if (!error
&& mddev
->kobj
.sd
) {
4729 kobject_uevent(&mddev
->kobj
, KOBJ_ADD
);
4730 mddev
->sysfs_state
= sysfs_get_dirent_safe(mddev
->kobj
.sd
, "array_state");
4736 static struct kobject
*md_probe(dev_t dev
, int *part
, void *data
)
4738 md_alloc(dev
, NULL
);
4742 static int add_named_array(const char *val
, struct kernel_param
*kp
)
4744 /* val must be "md_*" where * is not all digits.
4745 * We allocate an array with a large free minor number, and
4746 * set the name to val. val must not already be an active name.
4748 int len
= strlen(val
);
4749 char buf
[DISK_NAME_LEN
];
4751 while (len
&& val
[len
-1] == '\n')
4753 if (len
>= DISK_NAME_LEN
)
4755 strlcpy(buf
, val
, len
+1);
4756 if (strncmp(buf
, "md_", 3) != 0)
4758 return md_alloc(0, buf
);
4761 static void md_safemode_timeout(unsigned long data
)
4763 struct mddev
*mddev
= (struct mddev
*) data
;
4765 if (!atomic_read(&mddev
->writes_pending
)) {
4766 mddev
->safemode
= 1;
4767 if (mddev
->external
)
4768 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4770 md_wakeup_thread(mddev
->thread
);
4773 static int start_dirty_degraded
;
4775 int md_run(struct mddev
*mddev
)
4778 struct md_rdev
*rdev
;
4779 struct md_personality
*pers
;
4781 if (list_empty(&mddev
->disks
))
4782 /* cannot run an array with no devices.. */
4787 /* Cannot run until previous stop completes properly */
4788 if (mddev
->sysfs_active
)
4792 * Analyze all RAID superblock(s)
4794 if (!mddev
->raid_disks
) {
4795 if (!mddev
->persistent
)
4800 if (mddev
->level
!= LEVEL_NONE
)
4801 request_module("md-level-%d", mddev
->level
);
4802 else if (mddev
->clevel
[0])
4803 request_module("md-%s", mddev
->clevel
);
4806 * Drop all container device buffers, from now on
4807 * the only valid external interface is through the md
4810 rdev_for_each(rdev
, mddev
) {
4811 if (test_bit(Faulty
, &rdev
->flags
))
4813 sync_blockdev(rdev
->bdev
);
4814 invalidate_bdev(rdev
->bdev
);
4816 /* perform some consistency tests on the device.
4817 * We don't want the data to overlap the metadata,
4818 * Internal Bitmap issues have been handled elsewhere.
4820 if (rdev
->meta_bdev
) {
4821 /* Nothing to check */;
4822 } else if (rdev
->data_offset
< rdev
->sb_start
) {
4823 if (mddev
->dev_sectors
&&
4824 rdev
->data_offset
+ mddev
->dev_sectors
4826 printk("md: %s: data overlaps metadata\n",
4831 if (rdev
->sb_start
+ rdev
->sb_size
/512
4832 > rdev
->data_offset
) {
4833 printk("md: %s: metadata overlaps data\n",
4838 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
4841 if (mddev
->bio_set
== NULL
)
4842 mddev
->bio_set
= bioset_create(BIO_POOL_SIZE
,
4843 sizeof(struct mddev
*));
4845 spin_lock(&pers_lock
);
4846 pers
= find_pers(mddev
->level
, mddev
->clevel
);
4847 if (!pers
|| !try_module_get(pers
->owner
)) {
4848 spin_unlock(&pers_lock
);
4849 if (mddev
->level
!= LEVEL_NONE
)
4850 printk(KERN_WARNING
"md: personality for level %d is not loaded!\n",
4853 printk(KERN_WARNING
"md: personality for level %s is not loaded!\n",
4858 spin_unlock(&pers_lock
);
4859 if (mddev
->level
!= pers
->level
) {
4860 mddev
->level
= pers
->level
;
4861 mddev
->new_level
= pers
->level
;
4863 strlcpy(mddev
->clevel
, pers
->name
, sizeof(mddev
->clevel
));
4865 if (mddev
->reshape_position
!= MaxSector
&&
4866 pers
->start_reshape
== NULL
) {
4867 /* This personality cannot handle reshaping... */
4869 module_put(pers
->owner
);
4873 if (pers
->sync_request
) {
4874 /* Warn if this is a potentially silly
4877 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
4878 struct md_rdev
*rdev2
;
4881 rdev_for_each(rdev
, mddev
)
4882 rdev_for_each(rdev2
, mddev
) {
4884 rdev
->bdev
->bd_contains
==
4885 rdev2
->bdev
->bd_contains
) {
4887 "%s: WARNING: %s appears to be"
4888 " on the same physical disk as"
4891 bdevname(rdev
->bdev
,b
),
4892 bdevname(rdev2
->bdev
,b2
));
4899 "True protection against single-disk"
4900 " failure might be compromised.\n");
4903 mddev
->recovery
= 0;
4904 /* may be over-ridden by personality */
4905 mddev
->resync_max_sectors
= mddev
->dev_sectors
;
4907 mddev
->ok_start_degraded
= start_dirty_degraded
;
4909 if (start_readonly
&& mddev
->ro
== 0)
4910 mddev
->ro
= 2; /* read-only, but switch on first write */
4912 err
= mddev
->pers
->run(mddev
);
4914 printk(KERN_ERR
"md: pers->run() failed ...\n");
4915 else if (mddev
->pers
->size(mddev
, 0, 0) < mddev
->array_sectors
) {
4916 WARN_ONCE(!mddev
->external_size
, "%s: default size too small,"
4917 " but 'external_size' not in effect?\n", __func__
);
4919 "md: invalid array_size %llu > default size %llu\n",
4920 (unsigned long long)mddev
->array_sectors
/ 2,
4921 (unsigned long long)mddev
->pers
->size(mddev
, 0, 0) / 2);
4923 mddev
->pers
->stop(mddev
);
4925 if (err
== 0 && mddev
->pers
->sync_request
) {
4926 err
= bitmap_create(mddev
);
4928 printk(KERN_ERR
"%s: failed to create bitmap (%d)\n",
4929 mdname(mddev
), err
);
4930 mddev
->pers
->stop(mddev
);
4934 module_put(mddev
->pers
->owner
);
4936 bitmap_destroy(mddev
);
4939 if (mddev
->pers
->sync_request
) {
4940 if (mddev
->kobj
.sd
&&
4941 sysfs_create_group(&mddev
->kobj
, &md_redundancy_group
))
4943 "md: cannot register extra attributes for %s\n",
4945 mddev
->sysfs_action
= sysfs_get_dirent_safe(mddev
->kobj
.sd
, "sync_action");
4946 } else if (mddev
->ro
== 2) /* auto-readonly not meaningful */
4949 atomic_set(&mddev
->writes_pending
,0);
4950 atomic_set(&mddev
->max_corr_read_errors
,
4951 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS
);
4952 mddev
->safemode
= 0;
4953 mddev
->safemode_timer
.function
= md_safemode_timeout
;
4954 mddev
->safemode_timer
.data
= (unsigned long) mddev
;
4955 mddev
->safemode_delay
= (200 * HZ
)/1000 +1; /* 200 msec delay */
4959 rdev_for_each(rdev
, mddev
)
4960 if (rdev
->raid_disk
>= 0)
4961 if (sysfs_link_rdev(mddev
, rdev
))
4962 /* failure here is OK */;
4964 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4967 md_update_sb(mddev
, 0);
4969 md_new_event(mddev
);
4970 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4971 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
4972 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
4975 EXPORT_SYMBOL_GPL(md_run
);
4977 static int do_md_run(struct mddev
*mddev
)
4981 err
= md_run(mddev
);
4984 err
= bitmap_load(mddev
);
4986 bitmap_destroy(mddev
);
4990 md_wakeup_thread(mddev
->thread
);
4991 md_wakeup_thread(mddev
->sync_thread
); /* possibly kick off a reshape */
4993 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
4994 revalidate_disk(mddev
->gendisk
);
4996 kobject_uevent(&disk_to_dev(mddev
->gendisk
)->kobj
, KOBJ_CHANGE
);
5001 static int restart_array(struct mddev
*mddev
)
5003 struct gendisk
*disk
= mddev
->gendisk
;
5005 /* Complain if it has no devices */
5006 if (list_empty(&mddev
->disks
))
5012 mddev
->safemode
= 0;
5014 set_disk_ro(disk
, 0);
5015 printk(KERN_INFO
"md: %s switched to read-write mode.\n",
5017 /* Kick recovery or resync if necessary */
5018 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5019 md_wakeup_thread(mddev
->thread
);
5020 md_wakeup_thread(mddev
->sync_thread
);
5021 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
5025 /* similar to deny_write_access, but accounts for our holding a reference
5026 * to the file ourselves */
5027 static int deny_bitmap_write_access(struct file
* file
)
5029 struct inode
*inode
= file
->f_mapping
->host
;
5031 spin_lock(&inode
->i_lock
);
5032 if (atomic_read(&inode
->i_writecount
) > 1) {
5033 spin_unlock(&inode
->i_lock
);
5036 atomic_set(&inode
->i_writecount
, -1);
5037 spin_unlock(&inode
->i_lock
);
5042 void restore_bitmap_write_access(struct file
*file
)
5044 struct inode
*inode
= file
->f_mapping
->host
;
5046 spin_lock(&inode
->i_lock
);
5047 atomic_set(&inode
->i_writecount
, 1);
5048 spin_unlock(&inode
->i_lock
);
5051 static void md_clean(struct mddev
*mddev
)
5053 mddev
->array_sectors
= 0;
5054 mddev
->external_size
= 0;
5055 mddev
->dev_sectors
= 0;
5056 mddev
->raid_disks
= 0;
5057 mddev
->recovery_cp
= 0;
5058 mddev
->resync_min
= 0;
5059 mddev
->resync_max
= MaxSector
;
5060 mddev
->reshape_position
= MaxSector
;
5061 mddev
->external
= 0;
5062 mddev
->persistent
= 0;
5063 mddev
->level
= LEVEL_NONE
;
5064 mddev
->clevel
[0] = 0;
5067 mddev
->metadata_type
[0] = 0;
5068 mddev
->chunk_sectors
= 0;
5069 mddev
->ctime
= mddev
->utime
= 0;
5071 mddev
->max_disks
= 0;
5073 mddev
->can_decrease_events
= 0;
5074 mddev
->delta_disks
= 0;
5075 mddev
->new_level
= LEVEL_NONE
;
5076 mddev
->new_layout
= 0;
5077 mddev
->new_chunk_sectors
= 0;
5078 mddev
->curr_resync
= 0;
5079 mddev
->resync_mismatches
= 0;
5080 mddev
->suspend_lo
= mddev
->suspend_hi
= 0;
5081 mddev
->sync_speed_min
= mddev
->sync_speed_max
= 0;
5082 mddev
->recovery
= 0;
5085 mddev
->degraded
= 0;
5086 mddev
->safemode
= 0;
5087 mddev
->merge_check_needed
= 0;
5088 mddev
->bitmap_info
.offset
= 0;
5089 mddev
->bitmap_info
.default_offset
= 0;
5090 mddev
->bitmap_info
.chunksize
= 0;
5091 mddev
->bitmap_info
.daemon_sleep
= 0;
5092 mddev
->bitmap_info
.max_write_behind
= 0;
5095 static void __md_stop_writes(struct mddev
*mddev
)
5097 if (mddev
->sync_thread
) {
5098 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
5099 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
5100 reap_sync_thread(mddev
);
5103 del_timer_sync(&mddev
->safemode_timer
);
5105 bitmap_flush(mddev
);
5106 md_super_wait(mddev
);
5108 if (!mddev
->in_sync
|| mddev
->flags
) {
5109 /* mark array as shutdown cleanly */
5111 md_update_sb(mddev
, 1);
5115 void md_stop_writes(struct mddev
*mddev
)
5118 __md_stop_writes(mddev
);
5119 mddev_unlock(mddev
);
5121 EXPORT_SYMBOL_GPL(md_stop_writes
);
5123 void md_stop(struct mddev
*mddev
)
5126 mddev
->pers
->stop(mddev
);
5127 if (mddev
->pers
->sync_request
&& mddev
->to_remove
== NULL
)
5128 mddev
->to_remove
= &md_redundancy_group
;
5129 module_put(mddev
->pers
->owner
);
5131 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
5133 EXPORT_SYMBOL_GPL(md_stop
);
5135 static int md_set_readonly(struct mddev
*mddev
, struct block_device
*bdev
)
5138 mutex_lock(&mddev
->open_mutex
);
5139 if (atomic_read(&mddev
->openers
) > !!bdev
) {
5140 printk("md: %s still in use.\n",mdname(mddev
));
5145 sync_blockdev(bdev
);
5147 __md_stop_writes(mddev
);
5153 set_disk_ro(mddev
->gendisk
, 1);
5154 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
5155 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
5159 mutex_unlock(&mddev
->open_mutex
);
5164 * 0 - completely stop and dis-assemble array
5165 * 2 - stop but do not disassemble array
5167 static int do_md_stop(struct mddev
* mddev
, int mode
,
5168 struct block_device
*bdev
)
5170 struct gendisk
*disk
= mddev
->gendisk
;
5171 struct md_rdev
*rdev
;
5173 mutex_lock(&mddev
->open_mutex
);
5174 if (atomic_read(&mddev
->openers
) > !!bdev
||
5175 mddev
->sysfs_active
) {
5176 printk("md: %s still in use.\n",mdname(mddev
));
5177 mutex_unlock(&mddev
->open_mutex
);
5181 /* It is possible IO was issued on some other
5182 * open file which was closed before we took ->open_mutex.
5183 * As that was not the last close __blkdev_put will not
5184 * have called sync_blockdev, so we must.
5186 sync_blockdev(bdev
);
5190 set_disk_ro(disk
, 0);
5192 __md_stop_writes(mddev
);
5194 mddev
->queue
->merge_bvec_fn
= NULL
;
5195 mddev
->queue
->backing_dev_info
.congested_fn
= NULL
;
5197 /* tell userspace to handle 'inactive' */
5198 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
5200 rdev_for_each(rdev
, mddev
)
5201 if (rdev
->raid_disk
>= 0)
5202 sysfs_unlink_rdev(mddev
, rdev
);
5204 set_capacity(disk
, 0);
5205 mutex_unlock(&mddev
->open_mutex
);
5207 revalidate_disk(disk
);
5212 mutex_unlock(&mddev
->open_mutex
);
5214 * Free resources if final stop
5217 printk(KERN_INFO
"md: %s stopped.\n", mdname(mddev
));
5219 bitmap_destroy(mddev
);
5220 if (mddev
->bitmap_info
.file
) {
5221 restore_bitmap_write_access(mddev
->bitmap_info
.file
);
5222 fput(mddev
->bitmap_info
.file
);
5223 mddev
->bitmap_info
.file
= NULL
;
5225 mddev
->bitmap_info
.offset
= 0;
5227 export_array(mddev
);
5230 kobject_uevent(&disk_to_dev(mddev
->gendisk
)->kobj
, KOBJ_CHANGE
);
5231 if (mddev
->hold_active
== UNTIL_STOP
)
5232 mddev
->hold_active
= 0;
5234 blk_integrity_unregister(disk
);
5235 md_new_event(mddev
);
5236 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
5241 static void autorun_array(struct mddev
*mddev
)
5243 struct md_rdev
*rdev
;
5246 if (list_empty(&mddev
->disks
))
5249 printk(KERN_INFO
"md: running: ");
5251 rdev_for_each(rdev
, mddev
) {
5252 char b
[BDEVNAME_SIZE
];
5253 printk("<%s>", bdevname(rdev
->bdev
,b
));
5257 err
= do_md_run(mddev
);
5259 printk(KERN_WARNING
"md: do_md_run() returned %d\n", err
);
5260 do_md_stop(mddev
, 0, NULL
);
5265 * lets try to run arrays based on all disks that have arrived
5266 * until now. (those are in pending_raid_disks)
5268 * the method: pick the first pending disk, collect all disks with
5269 * the same UUID, remove all from the pending list and put them into
5270 * the 'same_array' list. Then order this list based on superblock
5271 * update time (freshest comes first), kick out 'old' disks and
5272 * compare superblocks. If everything's fine then run it.
5274 * If "unit" is allocated, then bump its reference count
5276 static void autorun_devices(int part
)
5278 struct md_rdev
*rdev0
, *rdev
, *tmp
;
5279 struct mddev
*mddev
;
5280 char b
[BDEVNAME_SIZE
];
5282 printk(KERN_INFO
"md: autorun ...\n");
5283 while (!list_empty(&pending_raid_disks
)) {
5286 LIST_HEAD(candidates
);
5287 rdev0
= list_entry(pending_raid_disks
.next
,
5288 struct md_rdev
, same_set
);
5290 printk(KERN_INFO
"md: considering %s ...\n",
5291 bdevname(rdev0
->bdev
,b
));
5292 INIT_LIST_HEAD(&candidates
);
5293 rdev_for_each_list(rdev
, tmp
, &pending_raid_disks
)
5294 if (super_90_load(rdev
, rdev0
, 0) >= 0) {
5295 printk(KERN_INFO
"md: adding %s ...\n",
5296 bdevname(rdev
->bdev
,b
));
5297 list_move(&rdev
->same_set
, &candidates
);
5300 * now we have a set of devices, with all of them having
5301 * mostly sane superblocks. It's time to allocate the
5305 dev
= MKDEV(mdp_major
,
5306 rdev0
->preferred_minor
<< MdpMinorShift
);
5307 unit
= MINOR(dev
) >> MdpMinorShift
;
5309 dev
= MKDEV(MD_MAJOR
, rdev0
->preferred_minor
);
5312 if (rdev0
->preferred_minor
!= unit
) {
5313 printk(KERN_INFO
"md: unit number in %s is bad: %d\n",
5314 bdevname(rdev0
->bdev
, b
), rdev0
->preferred_minor
);
5318 md_probe(dev
, NULL
, NULL
);
5319 mddev
= mddev_find(dev
);
5320 if (!mddev
|| !mddev
->gendisk
) {
5324 "md: cannot allocate memory for md drive.\n");
5327 if (mddev_lock(mddev
))
5328 printk(KERN_WARNING
"md: %s locked, cannot run\n",
5330 else if (mddev
->raid_disks
|| mddev
->major_version
5331 || !list_empty(&mddev
->disks
)) {
5333 "md: %s already running, cannot run %s\n",
5334 mdname(mddev
), bdevname(rdev0
->bdev
,b
));
5335 mddev_unlock(mddev
);
5337 printk(KERN_INFO
"md: created %s\n", mdname(mddev
));
5338 mddev
->persistent
= 1;
5339 rdev_for_each_list(rdev
, tmp
, &candidates
) {
5340 list_del_init(&rdev
->same_set
);
5341 if (bind_rdev_to_array(rdev
, mddev
))
5344 autorun_array(mddev
);
5345 mddev_unlock(mddev
);
5347 /* on success, candidates will be empty, on error
5350 rdev_for_each_list(rdev
, tmp
, &candidates
) {
5351 list_del_init(&rdev
->same_set
);
5356 printk(KERN_INFO
"md: ... autorun DONE.\n");
5358 #endif /* !MODULE */
5360 static int get_version(void __user
* arg
)
5364 ver
.major
= MD_MAJOR_VERSION
;
5365 ver
.minor
= MD_MINOR_VERSION
;
5366 ver
.patchlevel
= MD_PATCHLEVEL_VERSION
;
5368 if (copy_to_user(arg
, &ver
, sizeof(ver
)))
5374 static int get_array_info(struct mddev
* mddev
, void __user
* arg
)
5376 mdu_array_info_t info
;
5377 int nr
,working
,insync
,failed
,spare
;
5378 struct md_rdev
*rdev
;
5380 nr
=working
=insync
=failed
=spare
=0;
5381 rdev_for_each(rdev
, mddev
) {
5383 if (test_bit(Faulty
, &rdev
->flags
))
5387 if (test_bit(In_sync
, &rdev
->flags
))
5394 info
.major_version
= mddev
->major_version
;
5395 info
.minor_version
= mddev
->minor_version
;
5396 info
.patch_version
= MD_PATCHLEVEL_VERSION
;
5397 info
.ctime
= mddev
->ctime
;
5398 info
.level
= mddev
->level
;
5399 info
.size
= mddev
->dev_sectors
/ 2;
5400 if (info
.size
!= mddev
->dev_sectors
/ 2) /* overflow */
5403 info
.raid_disks
= mddev
->raid_disks
;
5404 info
.md_minor
= mddev
->md_minor
;
5405 info
.not_persistent
= !mddev
->persistent
;
5407 info
.utime
= mddev
->utime
;
5410 info
.state
= (1<<MD_SB_CLEAN
);
5411 if (mddev
->bitmap
&& mddev
->bitmap_info
.offset
)
5412 info
.state
= (1<<MD_SB_BITMAP_PRESENT
);
5413 info
.active_disks
= insync
;
5414 info
.working_disks
= working
;
5415 info
.failed_disks
= failed
;
5416 info
.spare_disks
= spare
;
5418 info
.layout
= mddev
->layout
;
5419 info
.chunk_size
= mddev
->chunk_sectors
<< 9;
5421 if (copy_to_user(arg
, &info
, sizeof(info
)))
5427 static int get_bitmap_file(struct mddev
* mddev
, void __user
* arg
)
5429 mdu_bitmap_file_t
*file
= NULL
; /* too big for stack allocation */
5430 char *ptr
, *buf
= NULL
;
5433 if (md_allow_write(mddev
))
5434 file
= kmalloc(sizeof(*file
), GFP_NOIO
);
5436 file
= kmalloc(sizeof(*file
), GFP_KERNEL
);
5441 /* bitmap disabled, zero the first byte and copy out */
5442 if (!mddev
->bitmap
|| !mddev
->bitmap
->file
) {
5443 file
->pathname
[0] = '\0';
5447 buf
= kmalloc(sizeof(file
->pathname
), GFP_KERNEL
);
5451 ptr
= d_path(&mddev
->bitmap
->file
->f_path
, buf
, sizeof(file
->pathname
));
5455 strcpy(file
->pathname
, ptr
);
5459 if (copy_to_user(arg
, file
, sizeof(*file
)))
5467 static int get_disk_info(struct mddev
* mddev
, void __user
* arg
)
5469 mdu_disk_info_t info
;
5470 struct md_rdev
*rdev
;
5472 if (copy_from_user(&info
, arg
, sizeof(info
)))
5475 rdev
= find_rdev_nr(mddev
, info
.number
);
5477 info
.major
= MAJOR(rdev
->bdev
->bd_dev
);
5478 info
.minor
= MINOR(rdev
->bdev
->bd_dev
);
5479 info
.raid_disk
= rdev
->raid_disk
;
5481 if (test_bit(Faulty
, &rdev
->flags
))
5482 info
.state
|= (1<<MD_DISK_FAULTY
);
5483 else if (test_bit(In_sync
, &rdev
->flags
)) {
5484 info
.state
|= (1<<MD_DISK_ACTIVE
);
5485 info
.state
|= (1<<MD_DISK_SYNC
);
5487 if (test_bit(WriteMostly
, &rdev
->flags
))
5488 info
.state
|= (1<<MD_DISK_WRITEMOSTLY
);
5490 info
.major
= info
.minor
= 0;
5491 info
.raid_disk
= -1;
5492 info
.state
= (1<<MD_DISK_REMOVED
);
5495 if (copy_to_user(arg
, &info
, sizeof(info
)))
5501 static int add_new_disk(struct mddev
* mddev
, mdu_disk_info_t
*info
)
5503 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
5504 struct md_rdev
*rdev
;
5505 dev_t dev
= MKDEV(info
->major
,info
->minor
);
5507 if (info
->major
!= MAJOR(dev
) || info
->minor
!= MINOR(dev
))
5510 if (!mddev
->raid_disks
) {
5512 /* expecting a device which has a superblock */
5513 rdev
= md_import_device(dev
, mddev
->major_version
, mddev
->minor_version
);
5516 "md: md_import_device returned %ld\n",
5518 return PTR_ERR(rdev
);
5520 if (!list_empty(&mddev
->disks
)) {
5521 struct md_rdev
*rdev0
5522 = list_entry(mddev
->disks
.next
,
5523 struct md_rdev
, same_set
);
5524 err
= super_types
[mddev
->major_version
]
5525 .load_super(rdev
, rdev0
, mddev
->minor_version
);
5528 "md: %s has different UUID to %s\n",
5529 bdevname(rdev
->bdev
,b
),
5530 bdevname(rdev0
->bdev
,b2
));
5535 err
= bind_rdev_to_array(rdev
, mddev
);
5542 * add_new_disk can be used once the array is assembled
5543 * to add "hot spares". They must already have a superblock
5548 if (!mddev
->pers
->hot_add_disk
) {
5550 "%s: personality does not support diskops!\n",
5554 if (mddev
->persistent
)
5555 rdev
= md_import_device(dev
, mddev
->major_version
,
5556 mddev
->minor_version
);
5558 rdev
= md_import_device(dev
, -1, -1);
5561 "md: md_import_device returned %ld\n",
5563 return PTR_ERR(rdev
);
5565 /* set saved_raid_disk if appropriate */
5566 if (!mddev
->persistent
) {
5567 if (info
->state
& (1<<MD_DISK_SYNC
) &&
5568 info
->raid_disk
< mddev
->raid_disks
) {
5569 rdev
->raid_disk
= info
->raid_disk
;
5570 set_bit(In_sync
, &rdev
->flags
);
5572 rdev
->raid_disk
= -1;
5574 super_types
[mddev
->major_version
].
5575 validate_super(mddev
, rdev
);
5576 if ((info
->state
& (1<<MD_DISK_SYNC
)) &&
5577 (!test_bit(In_sync
, &rdev
->flags
) ||
5578 rdev
->raid_disk
!= info
->raid_disk
)) {
5579 /* This was a hot-add request, but events doesn't
5580 * match, so reject it.
5586 if (test_bit(In_sync
, &rdev
->flags
))
5587 rdev
->saved_raid_disk
= rdev
->raid_disk
;
5589 rdev
->saved_raid_disk
= -1;
5591 clear_bit(In_sync
, &rdev
->flags
); /* just to be sure */
5592 if (info
->state
& (1<<MD_DISK_WRITEMOSTLY
))
5593 set_bit(WriteMostly
, &rdev
->flags
);
5595 clear_bit(WriteMostly
, &rdev
->flags
);
5597 rdev
->raid_disk
= -1;
5598 err
= bind_rdev_to_array(rdev
, mddev
);
5599 if (!err
&& !mddev
->pers
->hot_remove_disk
) {
5600 /* If there is hot_add_disk but no hot_remove_disk
5601 * then added disks for geometry changes,
5602 * and should be added immediately.
5604 super_types
[mddev
->major_version
].
5605 validate_super(mddev
, rdev
);
5606 err
= mddev
->pers
->hot_add_disk(mddev
, rdev
);
5608 unbind_rdev_from_array(rdev
);
5613 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
5615 md_update_sb(mddev
, 1);
5616 if (mddev
->degraded
)
5617 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
5618 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5620 md_new_event(mddev
);
5621 md_wakeup_thread(mddev
->thread
);
5625 /* otherwise, add_new_disk is only allowed
5626 * for major_version==0 superblocks
5628 if (mddev
->major_version
!= 0) {
5629 printk(KERN_WARNING
"%s: ADD_NEW_DISK not supported\n",
5634 if (!(info
->state
& (1<<MD_DISK_FAULTY
))) {
5636 rdev
= md_import_device(dev
, -1, 0);
5639 "md: error, md_import_device() returned %ld\n",
5641 return PTR_ERR(rdev
);
5643 rdev
->desc_nr
= info
->number
;
5644 if (info
->raid_disk
< mddev
->raid_disks
)
5645 rdev
->raid_disk
= info
->raid_disk
;
5647 rdev
->raid_disk
= -1;
5649 if (rdev
->raid_disk
< mddev
->raid_disks
)
5650 if (info
->state
& (1<<MD_DISK_SYNC
))
5651 set_bit(In_sync
, &rdev
->flags
);
5653 if (info
->state
& (1<<MD_DISK_WRITEMOSTLY
))
5654 set_bit(WriteMostly
, &rdev
->flags
);
5656 if (!mddev
->persistent
) {
5657 printk(KERN_INFO
"md: nonpersistent superblock ...\n");
5658 rdev
->sb_start
= i_size_read(rdev
->bdev
->bd_inode
) / 512;
5660 rdev
->sb_start
= calc_dev_sboffset(rdev
);
5661 rdev
->sectors
= rdev
->sb_start
;
5663 err
= bind_rdev_to_array(rdev
, mddev
);
5673 static int hot_remove_disk(struct mddev
* mddev
, dev_t dev
)
5675 char b
[BDEVNAME_SIZE
];
5676 struct md_rdev
*rdev
;
5678 rdev
= find_rdev(mddev
, dev
);
5682 if (rdev
->raid_disk
>= 0)
5685 kick_rdev_from_array(rdev
);
5686 md_update_sb(mddev
, 1);
5687 md_new_event(mddev
);
5691 printk(KERN_WARNING
"md: cannot remove active disk %s from %s ...\n",
5692 bdevname(rdev
->bdev
,b
), mdname(mddev
));
5696 static int hot_add_disk(struct mddev
* mddev
, dev_t dev
)
5698 char b
[BDEVNAME_SIZE
];
5700 struct md_rdev
*rdev
;
5705 if (mddev
->major_version
!= 0) {
5706 printk(KERN_WARNING
"%s: HOT_ADD may only be used with"
5707 " version-0 superblocks.\n",
5711 if (!mddev
->pers
->hot_add_disk
) {
5713 "%s: personality does not support diskops!\n",
5718 rdev
= md_import_device(dev
, -1, 0);
5721 "md: error, md_import_device() returned %ld\n",
5726 if (mddev
->persistent
)
5727 rdev
->sb_start
= calc_dev_sboffset(rdev
);
5729 rdev
->sb_start
= i_size_read(rdev
->bdev
->bd_inode
) / 512;
5731 rdev
->sectors
= rdev
->sb_start
;
5733 if (test_bit(Faulty
, &rdev
->flags
)) {
5735 "md: can not hot-add faulty %s disk to %s!\n",
5736 bdevname(rdev
->bdev
,b
), mdname(mddev
));
5740 clear_bit(In_sync
, &rdev
->flags
);
5742 rdev
->saved_raid_disk
= -1;
5743 err
= bind_rdev_to_array(rdev
, mddev
);
5748 * The rest should better be atomic, we can have disk failures
5749 * noticed in interrupt contexts ...
5752 rdev
->raid_disk
= -1;
5754 md_update_sb(mddev
, 1);
5757 * Kick recovery, maybe this spare has to be added to the
5758 * array immediately.
5760 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5761 md_wakeup_thread(mddev
->thread
);
5762 md_new_event(mddev
);
5770 static int set_bitmap_file(struct mddev
*mddev
, int fd
)
5775 if (!mddev
->pers
->quiesce
)
5777 if (mddev
->recovery
|| mddev
->sync_thread
)
5779 /* we should be able to change the bitmap.. */
5785 return -EEXIST
; /* cannot add when bitmap is present */
5786 mddev
->bitmap_info
.file
= fget(fd
);
5788 if (mddev
->bitmap_info
.file
== NULL
) {
5789 printk(KERN_ERR
"%s: error: failed to get bitmap file\n",
5794 err
= deny_bitmap_write_access(mddev
->bitmap_info
.file
);
5796 printk(KERN_ERR
"%s: error: bitmap file is already in use\n",
5798 fput(mddev
->bitmap_info
.file
);
5799 mddev
->bitmap_info
.file
= NULL
;
5802 mddev
->bitmap_info
.offset
= 0; /* file overrides offset */
5803 } else if (mddev
->bitmap
== NULL
)
5804 return -ENOENT
; /* cannot remove what isn't there */
5807 mddev
->pers
->quiesce(mddev
, 1);
5809 err
= bitmap_create(mddev
);
5811 err
= bitmap_load(mddev
);
5813 if (fd
< 0 || err
) {
5814 bitmap_destroy(mddev
);
5815 fd
= -1; /* make sure to put the file */
5817 mddev
->pers
->quiesce(mddev
, 0);
5820 if (mddev
->bitmap_info
.file
) {
5821 restore_bitmap_write_access(mddev
->bitmap_info
.file
);
5822 fput(mddev
->bitmap_info
.file
);
5824 mddev
->bitmap_info
.file
= NULL
;
5831 * set_array_info is used two different ways
5832 * The original usage is when creating a new array.
5833 * In this usage, raid_disks is > 0 and it together with
5834 * level, size, not_persistent,layout,chunksize determine the
5835 * shape of the array.
5836 * This will always create an array with a type-0.90.0 superblock.
5837 * The newer usage is when assembling an array.
5838 * In this case raid_disks will be 0, and the major_version field is
5839 * use to determine which style super-blocks are to be found on the devices.
5840 * The minor and patch _version numbers are also kept incase the
5841 * super_block handler wishes to interpret them.
5843 static int set_array_info(struct mddev
* mddev
, mdu_array_info_t
*info
)
5846 if (info
->raid_disks
== 0) {
5847 /* just setting version number for superblock loading */
5848 if (info
->major_version
< 0 ||
5849 info
->major_version
>= ARRAY_SIZE(super_types
) ||
5850 super_types
[info
->major_version
].name
== NULL
) {
5851 /* maybe try to auto-load a module? */
5853 "md: superblock version %d not known\n",
5854 info
->major_version
);
5857 mddev
->major_version
= info
->major_version
;
5858 mddev
->minor_version
= info
->minor_version
;
5859 mddev
->patch_version
= info
->patch_version
;
5860 mddev
->persistent
= !info
->not_persistent
;
5861 /* ensure mddev_put doesn't delete this now that there
5862 * is some minimal configuration.
5864 mddev
->ctime
= get_seconds();
5867 mddev
->major_version
= MD_MAJOR_VERSION
;
5868 mddev
->minor_version
= MD_MINOR_VERSION
;
5869 mddev
->patch_version
= MD_PATCHLEVEL_VERSION
;
5870 mddev
->ctime
= get_seconds();
5872 mddev
->level
= info
->level
;
5873 mddev
->clevel
[0] = 0;
5874 mddev
->dev_sectors
= 2 * (sector_t
)info
->size
;
5875 mddev
->raid_disks
= info
->raid_disks
;
5876 /* don't set md_minor, it is determined by which /dev/md* was
5879 if (info
->state
& (1<<MD_SB_CLEAN
))
5880 mddev
->recovery_cp
= MaxSector
;
5882 mddev
->recovery_cp
= 0;
5883 mddev
->persistent
= ! info
->not_persistent
;
5884 mddev
->external
= 0;
5886 mddev
->layout
= info
->layout
;
5887 mddev
->chunk_sectors
= info
->chunk_size
>> 9;
5889 mddev
->max_disks
= MD_SB_DISKS
;
5891 if (mddev
->persistent
)
5893 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
5895 mddev
->bitmap_info
.default_offset
= MD_SB_BYTES
>> 9;
5896 mddev
->bitmap_info
.offset
= 0;
5898 mddev
->reshape_position
= MaxSector
;
5901 * Generate a 128 bit UUID
5903 get_random_bytes(mddev
->uuid
, 16);
5905 mddev
->new_level
= mddev
->level
;
5906 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
5907 mddev
->new_layout
= mddev
->layout
;
5908 mddev
->delta_disks
= 0;
5913 void md_set_array_sectors(struct mddev
*mddev
, sector_t array_sectors
)
5915 WARN(!mddev_is_locked(mddev
), "%s: unlocked mddev!\n", __func__
);
5917 if (mddev
->external_size
)
5920 mddev
->array_sectors
= array_sectors
;
5922 EXPORT_SYMBOL(md_set_array_sectors
);
5924 static int update_size(struct mddev
*mddev
, sector_t num_sectors
)
5926 struct md_rdev
*rdev
;
5928 int fit
= (num_sectors
== 0);
5930 if (mddev
->pers
->resize
== NULL
)
5932 /* The "num_sectors" is the number of sectors of each device that
5933 * is used. This can only make sense for arrays with redundancy.
5934 * linear and raid0 always use whatever space is available. We can only
5935 * consider changing this number if no resync or reconstruction is
5936 * happening, and if the new size is acceptable. It must fit before the
5937 * sb_start or, if that is <data_offset, it must fit before the size
5938 * of each device. If num_sectors is zero, we find the largest size
5941 if (mddev
->sync_thread
)
5944 /* Sorry, cannot grow a bitmap yet, just remove it,
5948 rdev_for_each(rdev
, mddev
) {
5949 sector_t avail
= rdev
->sectors
;
5951 if (fit
&& (num_sectors
== 0 || num_sectors
> avail
))
5952 num_sectors
= avail
;
5953 if (avail
< num_sectors
)
5956 rv
= mddev
->pers
->resize(mddev
, num_sectors
);
5958 revalidate_disk(mddev
->gendisk
);
5962 static int update_raid_disks(struct mddev
*mddev
, int raid_disks
)
5965 /* change the number of raid disks */
5966 if (mddev
->pers
->check_reshape
== NULL
)
5968 if (raid_disks
<= 0 ||
5969 (mddev
->max_disks
&& raid_disks
>= mddev
->max_disks
))
5971 if (mddev
->sync_thread
|| mddev
->reshape_position
!= MaxSector
)
5973 mddev
->delta_disks
= raid_disks
- mddev
->raid_disks
;
5975 rv
= mddev
->pers
->check_reshape(mddev
);
5977 mddev
->delta_disks
= 0;
5983 * update_array_info is used to change the configuration of an
5985 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
5986 * fields in the info are checked against the array.
5987 * Any differences that cannot be handled will cause an error.
5988 * Normally, only one change can be managed at a time.
5990 static int update_array_info(struct mddev
*mddev
, mdu_array_info_t
*info
)
5996 /* calculate expected state,ignoring low bits */
5997 if (mddev
->bitmap
&& mddev
->bitmap_info
.offset
)
5998 state
|= (1 << MD_SB_BITMAP_PRESENT
);
6000 if (mddev
->major_version
!= info
->major_version
||
6001 mddev
->minor_version
!= info
->minor_version
||
6002 /* mddev->patch_version != info->patch_version || */
6003 mddev
->ctime
!= info
->ctime
||
6004 mddev
->level
!= info
->level
||
6005 /* mddev->layout != info->layout || */
6006 !mddev
->persistent
!= info
->not_persistent
||
6007 mddev
->chunk_sectors
!= info
->chunk_size
>> 9 ||
6008 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
6009 ((state
^info
->state
) & 0xfffffe00)
6012 /* Check there is only one change */
6013 if (info
->size
>= 0 && mddev
->dev_sectors
/ 2 != info
->size
)
6015 if (mddev
->raid_disks
!= info
->raid_disks
)
6017 if (mddev
->layout
!= info
->layout
)
6019 if ((state
^ info
->state
) & (1<<MD_SB_BITMAP_PRESENT
))
6026 if (mddev
->layout
!= info
->layout
) {
6028 * we don't need to do anything at the md level, the
6029 * personality will take care of it all.
6031 if (mddev
->pers
->check_reshape
== NULL
)
6034 mddev
->new_layout
= info
->layout
;
6035 rv
= mddev
->pers
->check_reshape(mddev
);
6037 mddev
->new_layout
= mddev
->layout
;
6041 if (info
->size
>= 0 && mddev
->dev_sectors
/ 2 != info
->size
)
6042 rv
= update_size(mddev
, (sector_t
)info
->size
* 2);
6044 if (mddev
->raid_disks
!= info
->raid_disks
)
6045 rv
= update_raid_disks(mddev
, info
->raid_disks
);
6047 if ((state
^ info
->state
) & (1<<MD_SB_BITMAP_PRESENT
)) {
6048 if (mddev
->pers
->quiesce
== NULL
)
6050 if (mddev
->recovery
|| mddev
->sync_thread
)
6052 if (info
->state
& (1<<MD_SB_BITMAP_PRESENT
)) {
6053 /* add the bitmap */
6056 if (mddev
->bitmap_info
.default_offset
== 0)
6058 mddev
->bitmap_info
.offset
=
6059 mddev
->bitmap_info
.default_offset
;
6060 mddev
->pers
->quiesce(mddev
, 1);
6061 rv
= bitmap_create(mddev
);
6063 rv
= bitmap_load(mddev
);
6065 bitmap_destroy(mddev
);
6066 mddev
->pers
->quiesce(mddev
, 0);
6068 /* remove the bitmap */
6071 if (mddev
->bitmap
->file
)
6073 mddev
->pers
->quiesce(mddev
, 1);
6074 bitmap_destroy(mddev
);
6075 mddev
->pers
->quiesce(mddev
, 0);
6076 mddev
->bitmap_info
.offset
= 0;
6079 md_update_sb(mddev
, 1);
6083 static int set_disk_faulty(struct mddev
*mddev
, dev_t dev
)
6085 struct md_rdev
*rdev
;
6087 if (mddev
->pers
== NULL
)
6090 rdev
= find_rdev(mddev
, dev
);
6094 md_error(mddev
, rdev
);
6095 if (!test_bit(Faulty
, &rdev
->flags
))
6101 * We have a problem here : there is no easy way to give a CHS
6102 * virtual geometry. We currently pretend that we have a 2 heads
6103 * 4 sectors (with a BIG number of cylinders...). This drives
6104 * dosfs just mad... ;-)
6106 static int md_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
6108 struct mddev
*mddev
= bdev
->bd_disk
->private_data
;
6112 geo
->cylinders
= mddev
->array_sectors
/ 8;
6116 static int md_ioctl(struct block_device
*bdev
, fmode_t mode
,
6117 unsigned int cmd
, unsigned long arg
)
6120 void __user
*argp
= (void __user
*)arg
;
6121 struct mddev
*mddev
= NULL
;
6126 case GET_ARRAY_INFO
:
6130 if (!capable(CAP_SYS_ADMIN
))
6135 * Commands dealing with the RAID driver but not any
6141 err
= get_version(argp
);
6144 case PRINT_RAID_DEBUG
:
6152 autostart_arrays(arg
);
6159 * Commands creating/starting a new array:
6162 mddev
= bdev
->bd_disk
->private_data
;
6169 err
= mddev_lock(mddev
);
6172 "md: ioctl lock interrupted, reason %d, cmd %d\n",
6179 case SET_ARRAY_INFO
:
6181 mdu_array_info_t info
;
6183 memset(&info
, 0, sizeof(info
));
6184 else if (copy_from_user(&info
, argp
, sizeof(info
))) {
6189 err
= update_array_info(mddev
, &info
);
6191 printk(KERN_WARNING
"md: couldn't update"
6192 " array info. %d\n", err
);
6197 if (!list_empty(&mddev
->disks
)) {
6199 "md: array %s already has disks!\n",
6204 if (mddev
->raid_disks
) {
6206 "md: array %s already initialised!\n",
6211 err
= set_array_info(mddev
, &info
);
6213 printk(KERN_WARNING
"md: couldn't set"
6214 " array info. %d\n", err
);
6224 * Commands querying/configuring an existing array:
6226 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
6227 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
6228 if ((!mddev
->raid_disks
&& !mddev
->external
)
6229 && cmd
!= ADD_NEW_DISK
&& cmd
!= STOP_ARRAY
6230 && cmd
!= RUN_ARRAY
&& cmd
!= SET_BITMAP_FILE
6231 && cmd
!= GET_BITMAP_FILE
) {
6237 * Commands even a read-only array can execute:
6241 case GET_ARRAY_INFO
:
6242 err
= get_array_info(mddev
, argp
);
6245 case GET_BITMAP_FILE
:
6246 err
= get_bitmap_file(mddev
, argp
);
6250 err
= get_disk_info(mddev
, argp
);
6253 case RESTART_ARRAY_RW
:
6254 err
= restart_array(mddev
);
6258 err
= do_md_stop(mddev
, 0, bdev
);
6262 err
= md_set_readonly(mddev
, bdev
);
6266 if (get_user(ro
, (int __user
*)(arg
))) {
6272 /* if the bdev is going readonly the value of mddev->ro
6273 * does not matter, no writes are coming
6278 /* are we are already prepared for writes? */
6282 /* transitioning to readauto need only happen for
6283 * arrays that call md_write_start
6286 err
= restart_array(mddev
);
6289 set_disk_ro(mddev
->gendisk
, 0);
6296 * The remaining ioctls are changing the state of the
6297 * superblock, so we do not allow them on read-only arrays.
6298 * However non-MD ioctls (e.g. get-size) will still come through
6299 * here and hit the 'default' below, so only disallow
6300 * 'md' ioctls, and switch to rw mode if started auto-readonly.
6302 if (_IOC_TYPE(cmd
) == MD_MAJOR
&& mddev
->ro
&& mddev
->pers
) {
6303 if (mddev
->ro
== 2) {
6305 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
6306 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6307 md_wakeup_thread(mddev
->thread
);
6318 mdu_disk_info_t info
;
6319 if (copy_from_user(&info
, argp
, sizeof(info
)))
6322 err
= add_new_disk(mddev
, &info
);
6326 case HOT_REMOVE_DISK
:
6327 err
= hot_remove_disk(mddev
, new_decode_dev(arg
));
6331 err
= hot_add_disk(mddev
, new_decode_dev(arg
));
6334 case SET_DISK_FAULTY
:
6335 err
= set_disk_faulty(mddev
, new_decode_dev(arg
));
6339 err
= do_md_run(mddev
);
6342 case SET_BITMAP_FILE
:
6343 err
= set_bitmap_file(mddev
, (int)arg
);
6353 if (mddev
->hold_active
== UNTIL_IOCTL
&&
6355 mddev
->hold_active
= 0;
6356 mddev_unlock(mddev
);
6365 #ifdef CONFIG_COMPAT
6366 static int md_compat_ioctl(struct block_device
*bdev
, fmode_t mode
,
6367 unsigned int cmd
, unsigned long arg
)
6370 case HOT_REMOVE_DISK
:
6372 case SET_DISK_FAULTY
:
6373 case SET_BITMAP_FILE
:
6374 /* These take in integer arg, do not convert */
6377 arg
= (unsigned long)compat_ptr(arg
);
6381 return md_ioctl(bdev
, mode
, cmd
, arg
);
6383 #endif /* CONFIG_COMPAT */
6385 static int md_open(struct block_device
*bdev
, fmode_t mode
)
6388 * Succeed if we can lock the mddev, which confirms that
6389 * it isn't being stopped right now.
6391 struct mddev
*mddev
= mddev_find(bdev
->bd_dev
);
6394 if (mddev
->gendisk
!= bdev
->bd_disk
) {
6395 /* we are racing with mddev_put which is discarding this
6399 /* Wait until bdev->bd_disk is definitely gone */
6400 flush_workqueue(md_misc_wq
);
6401 /* Then retry the open from the top */
6402 return -ERESTARTSYS
;
6404 BUG_ON(mddev
!= bdev
->bd_disk
->private_data
);
6406 if ((err
= mutex_lock_interruptible(&mddev
->open_mutex
)))
6410 atomic_inc(&mddev
->openers
);
6411 mutex_unlock(&mddev
->open_mutex
);
6413 check_disk_change(bdev
);
6418 static int md_release(struct gendisk
*disk
, fmode_t mode
)
6420 struct mddev
*mddev
= disk
->private_data
;
6423 atomic_dec(&mddev
->openers
);
6429 static int md_media_changed(struct gendisk
*disk
)
6431 struct mddev
*mddev
= disk
->private_data
;
6433 return mddev
->changed
;
6436 static int md_revalidate(struct gendisk
*disk
)
6438 struct mddev
*mddev
= disk
->private_data
;
6443 static const struct block_device_operations md_fops
=
6445 .owner
= THIS_MODULE
,
6447 .release
= md_release
,
6449 #ifdef CONFIG_COMPAT
6450 .compat_ioctl
= md_compat_ioctl
,
6452 .getgeo
= md_getgeo
,
6453 .media_changed
= md_media_changed
,
6454 .revalidate_disk
= md_revalidate
,
6457 static int md_thread(void * arg
)
6459 struct md_thread
*thread
= arg
;
6462 * md_thread is a 'system-thread', it's priority should be very
6463 * high. We avoid resource deadlocks individually in each
6464 * raid personality. (RAID5 does preallocation) We also use RR and
6465 * the very same RT priority as kswapd, thus we will never get
6466 * into a priority inversion deadlock.
6468 * we definitely have to have equal or higher priority than
6469 * bdflush, otherwise bdflush will deadlock if there are too
6470 * many dirty RAID5 blocks.
6473 allow_signal(SIGKILL
);
6474 while (!kthread_should_stop()) {
6476 /* We need to wait INTERRUPTIBLE so that
6477 * we don't add to the load-average.
6478 * That means we need to be sure no signals are
6481 if (signal_pending(current
))
6482 flush_signals(current
);
6484 wait_event_interruptible_timeout
6486 test_bit(THREAD_WAKEUP
, &thread
->flags
)
6487 || kthread_should_stop(),
6490 clear_bit(THREAD_WAKEUP
, &thread
->flags
);
6491 if (!kthread_should_stop())
6492 thread
->run(thread
->mddev
);
6498 void md_wakeup_thread(struct md_thread
*thread
)
6501 pr_debug("md: waking up MD thread %s.\n", thread
->tsk
->comm
);
6502 set_bit(THREAD_WAKEUP
, &thread
->flags
);
6503 wake_up(&thread
->wqueue
);
6507 struct md_thread
*md_register_thread(void (*run
) (struct mddev
*), struct mddev
*mddev
,
6510 struct md_thread
*thread
;
6512 thread
= kzalloc(sizeof(struct md_thread
), GFP_KERNEL
);
6516 init_waitqueue_head(&thread
->wqueue
);
6519 thread
->mddev
= mddev
;
6520 thread
->timeout
= MAX_SCHEDULE_TIMEOUT
;
6521 thread
->tsk
= kthread_run(md_thread
, thread
,
6523 mdname(thread
->mddev
),
6524 name
?: mddev
->pers
->name
);
6525 if (IS_ERR(thread
->tsk
)) {
6532 void md_unregister_thread(struct md_thread
**threadp
)
6534 struct md_thread
*thread
= *threadp
;
6537 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread
->tsk
));
6538 /* Locking ensures that mddev_unlock does not wake_up a
6539 * non-existent thread
6541 spin_lock(&pers_lock
);
6543 spin_unlock(&pers_lock
);
6545 kthread_stop(thread
->tsk
);
6549 void md_error(struct mddev
*mddev
, struct md_rdev
*rdev
)
6556 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
))
6559 if (!mddev
->pers
|| !mddev
->pers
->error_handler
)
6561 mddev
->pers
->error_handler(mddev
,rdev
);
6562 if (mddev
->degraded
)
6563 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
6564 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
6565 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6566 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6567 md_wakeup_thread(mddev
->thread
);
6568 if (mddev
->event_work
.func
)
6569 queue_work(md_misc_wq
, &mddev
->event_work
);
6570 md_new_event_inintr(mddev
);
6573 /* seq_file implementation /proc/mdstat */
6575 static void status_unused(struct seq_file
*seq
)
6578 struct md_rdev
*rdev
;
6580 seq_printf(seq
, "unused devices: ");
6582 list_for_each_entry(rdev
, &pending_raid_disks
, same_set
) {
6583 char b
[BDEVNAME_SIZE
];
6585 seq_printf(seq
, "%s ",
6586 bdevname(rdev
->bdev
,b
));
6589 seq_printf(seq
, "<none>");
6591 seq_printf(seq
, "\n");
6595 static void status_resync(struct seq_file
*seq
, struct mddev
* mddev
)
6597 sector_t max_sectors
, resync
, res
;
6598 unsigned long dt
, db
;
6601 unsigned int per_milli
;
6603 resync
= mddev
->curr_resync
- atomic_read(&mddev
->recovery_active
);
6605 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
6606 max_sectors
= mddev
->resync_max_sectors
;
6608 max_sectors
= mddev
->dev_sectors
;
6611 * Should not happen.
6617 /* Pick 'scale' such that (resync>>scale)*1000 will fit
6618 * in a sector_t, and (max_sectors>>scale) will fit in a
6619 * u32, as those are the requirements for sector_div.
6620 * Thus 'scale' must be at least 10
6623 if (sizeof(sector_t
) > sizeof(unsigned long)) {
6624 while ( max_sectors
/2 > (1ULL<<(scale
+32)))
6627 res
= (resync
>>scale
)*1000;
6628 sector_div(res
, (u32
)((max_sectors
>>scale
)+1));
6632 int i
, x
= per_milli
/50, y
= 20-x
;
6633 seq_printf(seq
, "[");
6634 for (i
= 0; i
< x
; i
++)
6635 seq_printf(seq
, "=");
6636 seq_printf(seq
, ">");
6637 for (i
= 0; i
< y
; i
++)
6638 seq_printf(seq
, ".");
6639 seq_printf(seq
, "] ");
6641 seq_printf(seq
, " %s =%3u.%u%% (%llu/%llu)",
6642 (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
)?
6644 (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
)?
6646 (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ?
6647 "resync" : "recovery"))),
6648 per_milli
/10, per_milli
% 10,
6649 (unsigned long long) resync
/2,
6650 (unsigned long long) max_sectors
/2);
6653 * dt: time from mark until now
6654 * db: blocks written from mark until now
6655 * rt: remaining time
6657 * rt is a sector_t, so could be 32bit or 64bit.
6658 * So we divide before multiply in case it is 32bit and close
6660 * We scale the divisor (db) by 32 to avoid losing precision
6661 * near the end of resync when the number of remaining sectors
6663 * We then divide rt by 32 after multiplying by db to compensate.
6664 * The '+1' avoids division by zero if db is very small.
6666 dt
= ((jiffies
- mddev
->resync_mark
) / HZ
);
6668 db
= (mddev
->curr_mark_cnt
- atomic_read(&mddev
->recovery_active
))
6669 - mddev
->resync_mark_cnt
;
6671 rt
= max_sectors
- resync
; /* number of remaining sectors */
6672 sector_div(rt
, db
/32+1);
6676 seq_printf(seq
, " finish=%lu.%lumin", (unsigned long)rt
/ 60,
6677 ((unsigned long)rt
% 60)/6);
6679 seq_printf(seq
, " speed=%ldK/sec", db
/2/dt
);
6682 static void *md_seq_start(struct seq_file
*seq
, loff_t
*pos
)
6684 struct list_head
*tmp
;
6686 struct mddev
*mddev
;
6694 spin_lock(&all_mddevs_lock
);
6695 list_for_each(tmp
,&all_mddevs
)
6697 mddev
= list_entry(tmp
, struct mddev
, all_mddevs
);
6699 spin_unlock(&all_mddevs_lock
);
6702 spin_unlock(&all_mddevs_lock
);
6704 return (void*)2;/* tail */
6708 static void *md_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
6710 struct list_head
*tmp
;
6711 struct mddev
*next_mddev
, *mddev
= v
;
6717 spin_lock(&all_mddevs_lock
);
6719 tmp
= all_mddevs
.next
;
6721 tmp
= mddev
->all_mddevs
.next
;
6722 if (tmp
!= &all_mddevs
)
6723 next_mddev
= mddev_get(list_entry(tmp
,struct mddev
,all_mddevs
));
6725 next_mddev
= (void*)2;
6728 spin_unlock(&all_mddevs_lock
);
6736 static void md_seq_stop(struct seq_file
*seq
, void *v
)
6738 struct mddev
*mddev
= v
;
6740 if (mddev
&& v
!= (void*)1 && v
!= (void*)2)
6744 static int md_seq_show(struct seq_file
*seq
, void *v
)
6746 struct mddev
*mddev
= v
;
6748 struct md_rdev
*rdev
;
6750 if (v
== (void*)1) {
6751 struct md_personality
*pers
;
6752 seq_printf(seq
, "Personalities : ");
6753 spin_lock(&pers_lock
);
6754 list_for_each_entry(pers
, &pers_list
, list
)
6755 seq_printf(seq
, "[%s] ", pers
->name
);
6757 spin_unlock(&pers_lock
);
6758 seq_printf(seq
, "\n");
6759 seq
->poll_event
= atomic_read(&md_event_count
);
6762 if (v
== (void*)2) {
6767 if (mddev_lock(mddev
) < 0)
6770 if (mddev
->pers
|| mddev
->raid_disks
|| !list_empty(&mddev
->disks
)) {
6771 seq_printf(seq
, "%s : %sactive", mdname(mddev
),
6772 mddev
->pers
? "" : "in");
6775 seq_printf(seq
, " (read-only)");
6777 seq_printf(seq
, " (auto-read-only)");
6778 seq_printf(seq
, " %s", mddev
->pers
->name
);
6782 rdev_for_each(rdev
, mddev
) {
6783 char b
[BDEVNAME_SIZE
];
6784 seq_printf(seq
, " %s[%d]",
6785 bdevname(rdev
->bdev
,b
), rdev
->desc_nr
);
6786 if (test_bit(WriteMostly
, &rdev
->flags
))
6787 seq_printf(seq
, "(W)");
6788 if (test_bit(Faulty
, &rdev
->flags
)) {
6789 seq_printf(seq
, "(F)");
6792 if (rdev
->raid_disk
< 0)
6793 seq_printf(seq
, "(S)"); /* spare */
6794 if (test_bit(Replacement
, &rdev
->flags
))
6795 seq_printf(seq
, "(R)");
6796 sectors
+= rdev
->sectors
;
6799 if (!list_empty(&mddev
->disks
)) {
6801 seq_printf(seq
, "\n %llu blocks",
6802 (unsigned long long)
6803 mddev
->array_sectors
/ 2);
6805 seq_printf(seq
, "\n %llu blocks",
6806 (unsigned long long)sectors
/ 2);
6808 if (mddev
->persistent
) {
6809 if (mddev
->major_version
!= 0 ||
6810 mddev
->minor_version
!= 90) {
6811 seq_printf(seq
," super %d.%d",
6812 mddev
->major_version
,
6813 mddev
->minor_version
);
6815 } else if (mddev
->external
)
6816 seq_printf(seq
, " super external:%s",
6817 mddev
->metadata_type
);
6819 seq_printf(seq
, " super non-persistent");
6822 mddev
->pers
->status(seq
, mddev
);
6823 seq_printf(seq
, "\n ");
6824 if (mddev
->pers
->sync_request
) {
6825 if (mddev
->curr_resync
> 2) {
6826 status_resync(seq
, mddev
);
6827 seq_printf(seq
, "\n ");
6828 } else if (mddev
->curr_resync
== 1 || mddev
->curr_resync
== 2)
6829 seq_printf(seq
, "\tresync=DELAYED\n ");
6830 else if (mddev
->recovery_cp
< MaxSector
)
6831 seq_printf(seq
, "\tresync=PENDING\n ");
6834 seq_printf(seq
, "\n ");
6836 bitmap_status(seq
, mddev
->bitmap
);
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 */
7075 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
7079 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
7080 if (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
))
7081 desc
= "data-check";
7082 else if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
7083 desc
= "requested-resync";
7086 } else if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
7091 /* we overload curr_resync somewhat here.
7092 * 0 == not engaged in resync at all
7093 * 2 == checking that there is no conflict with another sync
7094 * 1 == like 2, but have yielded to allow conflicting resync to
7096 * other == active in resync - this many blocks
7098 * Before starting a resync we must have set curr_resync to
7099 * 2, and then checked that every "conflicting" array has curr_resync
7100 * less than ours. When we find one that is the same or higher
7101 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
7102 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
7103 * This will mean we have to start checking from the beginning again.
7108 mddev
->curr_resync
= 2;
7111 if (kthread_should_stop())
7112 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
7114 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
7116 for_each_mddev(mddev2
, tmp
) {
7117 if (mddev2
== mddev
)
7119 if (!mddev
->parallel_resync
7120 && mddev2
->curr_resync
7121 && match_mddev_units(mddev
, mddev2
)) {
7123 if (mddev
< mddev2
&& mddev
->curr_resync
== 2) {
7124 /* arbitrarily yield */
7125 mddev
->curr_resync
= 1;
7126 wake_up(&resync_wait
);
7128 if (mddev
> mddev2
&& mddev
->curr_resync
== 1)
7129 /* no need to wait here, we can wait the next
7130 * time 'round when curr_resync == 2
7133 /* We need to wait 'interruptible' so as not to
7134 * contribute to the load average, and not to
7135 * be caught by 'softlockup'
7137 prepare_to_wait(&resync_wait
, &wq
, TASK_INTERRUPTIBLE
);
7138 if (!kthread_should_stop() &&
7139 mddev2
->curr_resync
>= mddev
->curr_resync
) {
7140 printk(KERN_INFO
"md: delaying %s of %s"
7141 " until %s has finished (they"
7142 " share one or more physical units)\n",
7143 desc
, mdname(mddev
), mdname(mddev2
));
7145 if (signal_pending(current
))
7146 flush_signals(current
);
7148 finish_wait(&resync_wait
, &wq
);
7151 finish_wait(&resync_wait
, &wq
);
7154 } while (mddev
->curr_resync
< 2);
7157 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
7158 /* resync follows the size requested by the personality,
7159 * which defaults to physical size, but can be virtual size
7161 max_sectors
= mddev
->resync_max_sectors
;
7162 mddev
->resync_mismatches
= 0;
7163 /* we don't use the checkpoint if there's a bitmap */
7164 if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
7165 j
= mddev
->resync_min
;
7166 else if (!mddev
->bitmap
)
7167 j
= mddev
->recovery_cp
;
7169 } else if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
7170 max_sectors
= mddev
->dev_sectors
;
7172 /* recovery follows the physical size of devices */
7173 max_sectors
= mddev
->dev_sectors
;
7176 rdev_for_each_rcu(rdev
, mddev
)
7177 if (rdev
->raid_disk
>= 0 &&
7178 !test_bit(Faulty
, &rdev
->flags
) &&
7179 !test_bit(In_sync
, &rdev
->flags
) &&
7180 rdev
->recovery_offset
< j
)
7181 j
= rdev
->recovery_offset
;
7184 /* If there is a bitmap, we need to make sure all
7185 * writes that started before we added a spare
7186 * complete before we start doing a recovery.
7187 * Otherwise the write might complete and (via
7188 * bitmap_endwrite) set a bit in the bitmap after the
7189 * recovery has checked that bit and skipped that
7192 if (mddev
->bitmap
) {
7193 mddev
->pers
->quiesce(mddev
, 1);
7194 mddev
->pers
->quiesce(mddev
, 0);
7198 printk(KERN_INFO
"md: %s of RAID array %s\n", desc
, mdname(mddev
));
7199 printk(KERN_INFO
"md: minimum _guaranteed_ speed:"
7200 " %d KB/sec/disk.\n", speed_min(mddev
));
7201 printk(KERN_INFO
"md: using maximum available idle IO bandwidth "
7202 "(but not more than %d KB/sec) for %s.\n",
7203 speed_max(mddev
), desc
);
7205 is_mddev_idle(mddev
, 1); /* this initializes IO event counters */
7208 for (m
= 0; m
< SYNC_MARKS
; m
++) {
7210 mark_cnt
[m
] = io_sectors
;
7213 mddev
->resync_mark
= mark
[last_mark
];
7214 mddev
->resync_mark_cnt
= mark_cnt
[last_mark
];
7217 * Tune reconstruction:
7219 window
= 32*(PAGE_SIZE
/512);
7220 printk(KERN_INFO
"md: using %dk window, over a total of %lluk.\n",
7221 window
/2, (unsigned long long)max_sectors
/2);
7223 atomic_set(&mddev
->recovery_active
, 0);
7228 "md: resuming %s of %s from checkpoint.\n",
7229 desc
, mdname(mddev
));
7230 mddev
->curr_resync
= j
;
7232 mddev
->curr_resync_completed
= j
;
7234 while (j
< max_sectors
) {
7239 if (!test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
7240 ((mddev
->curr_resync
> mddev
->curr_resync_completed
&&
7241 (mddev
->curr_resync
- mddev
->curr_resync_completed
)
7242 > (max_sectors
>> 4)) ||
7243 (j
- mddev
->curr_resync_completed
)*2
7244 >= mddev
->resync_max
- mddev
->curr_resync_completed
7246 /* time to update curr_resync_completed */
7247 wait_event(mddev
->recovery_wait
,
7248 atomic_read(&mddev
->recovery_active
) == 0);
7249 mddev
->curr_resync_completed
= j
;
7250 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
7251 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
7254 while (j
>= mddev
->resync_max
&& !kthread_should_stop()) {
7255 /* As this condition is controlled by user-space,
7256 * we can block indefinitely, so use '_interruptible'
7257 * to avoid triggering warnings.
7259 flush_signals(current
); /* just in case */
7260 wait_event_interruptible(mddev
->recovery_wait
,
7261 mddev
->resync_max
> j
7262 || kthread_should_stop());
7265 if (kthread_should_stop())
7268 sectors
= mddev
->pers
->sync_request(mddev
, j
, &skipped
,
7269 currspeed
< speed_min(mddev
));
7271 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
7275 if (!skipped
) { /* actual IO requested */
7276 io_sectors
+= sectors
;
7277 atomic_add(sectors
, &mddev
->recovery_active
);
7280 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
7284 if (j
>1) mddev
->curr_resync
= j
;
7285 mddev
->curr_mark_cnt
= io_sectors
;
7286 if (last_check
== 0)
7287 /* this is the earliest that rebuild will be
7288 * visible in /proc/mdstat
7290 md_new_event(mddev
);
7292 if (last_check
+ window
> io_sectors
|| j
== max_sectors
)
7295 last_check
= io_sectors
;
7297 if (time_after_eq(jiffies
, mark
[last_mark
] + SYNC_MARK_STEP
)) {
7299 int next
= (last_mark
+1) % SYNC_MARKS
;
7301 mddev
->resync_mark
= mark
[next
];
7302 mddev
->resync_mark_cnt
= mark_cnt
[next
];
7303 mark
[next
] = jiffies
;
7304 mark_cnt
[next
] = io_sectors
- atomic_read(&mddev
->recovery_active
);
7309 if (kthread_should_stop())
7314 * this loop exits only if either when we are slower than
7315 * the 'hard' speed limit, or the system was IO-idle for
7317 * the system might be non-idle CPU-wise, but we only care
7318 * about not overloading the IO subsystem. (things like an
7319 * e2fsck being done on the RAID array should execute fast)
7323 currspeed
= ((unsigned long)(io_sectors
-mddev
->resync_mark_cnt
))/2
7324 /((jiffies
-mddev
->resync_mark
)/HZ
+1) +1;
7326 if (currspeed
> speed_min(mddev
)) {
7327 if ((currspeed
> speed_max(mddev
)) ||
7328 !is_mddev_idle(mddev
, 0)) {
7334 printk(KERN_INFO
"md: %s: %s done.\n",mdname(mddev
), desc
);
7336 * this also signals 'finished resyncing' to md_stop
7339 wait_event(mddev
->recovery_wait
, !atomic_read(&mddev
->recovery_active
));
7341 /* tell personality that we are finished */
7342 mddev
->pers
->sync_request(mddev
, max_sectors
, &skipped
, 1);
7344 if (!test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
) &&
7345 mddev
->curr_resync
> 2) {
7346 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
7347 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
7348 if (mddev
->curr_resync
>= mddev
->recovery_cp
) {
7350 "md: checkpointing %s of %s.\n",
7351 desc
, mdname(mddev
));
7352 mddev
->recovery_cp
=
7353 mddev
->curr_resync_completed
;
7356 mddev
->recovery_cp
= MaxSector
;
7358 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
7359 mddev
->curr_resync
= MaxSector
;
7361 rdev_for_each_rcu(rdev
, mddev
)
7362 if (rdev
->raid_disk
>= 0 &&
7363 mddev
->delta_disks
>= 0 &&
7364 !test_bit(Faulty
, &rdev
->flags
) &&
7365 !test_bit(In_sync
, &rdev
->flags
) &&
7366 rdev
->recovery_offset
< mddev
->curr_resync
)
7367 rdev
->recovery_offset
= mddev
->curr_resync
;
7372 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
7374 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
7375 /* We completed so min/max setting can be forgotten if used. */
7376 if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
7377 mddev
->resync_min
= 0;
7378 mddev
->resync_max
= MaxSector
;
7379 } else if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
7380 mddev
->resync_min
= mddev
->curr_resync_completed
;
7381 mddev
->curr_resync
= 0;
7382 wake_up(&resync_wait
);
7383 set_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
7384 md_wakeup_thread(mddev
->thread
);
7389 * got a signal, exit.
7392 "md: md_do_sync() got signal ... exiting\n");
7393 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
7397 EXPORT_SYMBOL_GPL(md_do_sync
);
7399 static int remove_and_add_spares(struct mddev
*mddev
)
7401 struct md_rdev
*rdev
;
7405 mddev
->curr_resync_completed
= 0;
7407 rdev_for_each(rdev
, mddev
)
7408 if (rdev
->raid_disk
>= 0 &&
7409 !test_bit(Blocked
, &rdev
->flags
) &&
7410 (test_bit(Faulty
, &rdev
->flags
) ||
7411 ! test_bit(In_sync
, &rdev
->flags
)) &&
7412 atomic_read(&rdev
->nr_pending
)==0) {
7413 if (mddev
->pers
->hot_remove_disk(
7414 mddev
, rdev
) == 0) {
7415 sysfs_unlink_rdev(mddev
, rdev
);
7416 rdev
->raid_disk
= -1;
7421 sysfs_notify(&mddev
->kobj
, NULL
,
7425 rdev_for_each(rdev
, mddev
) {
7426 if (rdev
->raid_disk
>= 0 &&
7427 !test_bit(In_sync
, &rdev
->flags
) &&
7428 !test_bit(Faulty
, &rdev
->flags
))
7430 if (rdev
->raid_disk
< 0
7431 && !test_bit(Faulty
, &rdev
->flags
)) {
7432 rdev
->recovery_offset
= 0;
7434 hot_add_disk(mddev
, rdev
) == 0) {
7435 if (sysfs_link_rdev(mddev
, rdev
))
7436 /* failure here is OK */;
7438 md_new_event(mddev
);
7439 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
7444 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
7448 static void reap_sync_thread(struct mddev
*mddev
)
7450 struct md_rdev
*rdev
;
7452 /* resync has finished, collect result */
7453 md_unregister_thread(&mddev
->sync_thread
);
7454 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
) &&
7455 !test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
)) {
7457 /* activate any spares */
7458 if (mddev
->pers
->spare_active(mddev
)) {
7459 sysfs_notify(&mddev
->kobj
, NULL
,
7461 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
7464 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
7465 mddev
->pers
->finish_reshape
)
7466 mddev
->pers
->finish_reshape(mddev
);
7468 /* If array is no-longer degraded, then any saved_raid_disk
7469 * information must be scrapped. Also if any device is now
7470 * In_sync we must scrape the saved_raid_disk for that device
7471 * do the superblock for an incrementally recovered device
7474 rdev_for_each(rdev
, mddev
)
7475 if (!mddev
->degraded
||
7476 test_bit(In_sync
, &rdev
->flags
))
7477 rdev
->saved_raid_disk
= -1;
7479 md_update_sb(mddev
, 1);
7480 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
7481 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
7482 clear_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
7483 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
7484 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
7485 /* flag recovery needed just to double check */
7486 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7487 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
7488 md_new_event(mddev
);
7489 if (mddev
->event_work
.func
)
7490 queue_work(md_misc_wq
, &mddev
->event_work
);
7494 * This routine is regularly called by all per-raid-array threads to
7495 * deal with generic issues like resync and super-block update.
7496 * Raid personalities that don't have a thread (linear/raid0) do not
7497 * need this as they never do any recovery or update the superblock.
7499 * It does not do any resync itself, but rather "forks" off other threads
7500 * to do that as needed.
7501 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
7502 * "->recovery" and create a thread at ->sync_thread.
7503 * When the thread finishes it sets MD_RECOVERY_DONE
7504 * and wakeups up this thread which will reap the thread and finish up.
7505 * This thread also removes any faulty devices (with nr_pending == 0).
7507 * The overall approach is:
7508 * 1/ if the superblock needs updating, update it.
7509 * 2/ If a recovery thread is running, don't do anything else.
7510 * 3/ If recovery has finished, clean up, possibly marking spares active.
7511 * 4/ If there are any faulty devices, remove them.
7512 * 5/ If array is degraded, try to add spares devices
7513 * 6/ If array has spares or is not in-sync, start a resync thread.
7515 void md_check_recovery(struct mddev
*mddev
)
7517 if (mddev
->suspended
)
7521 bitmap_daemon_work(mddev
);
7523 if (signal_pending(current
)) {
7524 if (mddev
->pers
->sync_request
&& !mddev
->external
) {
7525 printk(KERN_INFO
"md: %s in immediate safe mode\n",
7527 mddev
->safemode
= 2;
7529 flush_signals(current
);
7532 if (mddev
->ro
&& !test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))
7535 (mddev
->flags
& ~ (1<<MD_CHANGE_PENDING
)) ||
7536 test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
) ||
7537 test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
) ||
7538 (mddev
->external
== 0 && mddev
->safemode
== 1) ||
7539 (mddev
->safemode
== 2 && ! atomic_read(&mddev
->writes_pending
)
7540 && !mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
)
7544 if (mddev_trylock(mddev
)) {
7548 /* Only thing we do on a ro array is remove
7551 struct md_rdev
*rdev
;
7552 rdev_for_each(rdev
, mddev
)
7553 if (rdev
->raid_disk
>= 0 &&
7554 !test_bit(Blocked
, &rdev
->flags
) &&
7555 test_bit(Faulty
, &rdev
->flags
) &&
7556 atomic_read(&rdev
->nr_pending
)==0) {
7557 if (mddev
->pers
->hot_remove_disk(
7558 mddev
, rdev
) == 0) {
7559 sysfs_unlink_rdev(mddev
, rdev
);
7560 rdev
->raid_disk
= -1;
7563 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7567 if (!mddev
->external
) {
7569 spin_lock_irq(&mddev
->write_lock
);
7570 if (mddev
->safemode
&&
7571 !atomic_read(&mddev
->writes_pending
) &&
7573 mddev
->recovery_cp
== MaxSector
) {
7576 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
7578 if (mddev
->safemode
== 1)
7579 mddev
->safemode
= 0;
7580 spin_unlock_irq(&mddev
->write_lock
);
7582 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
7586 md_update_sb(mddev
, 0);
7588 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) &&
7589 !test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
)) {
7590 /* resync/recovery still happening */
7591 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7594 if (mddev
->sync_thread
) {
7595 reap_sync_thread(mddev
);
7598 /* Set RUNNING before clearing NEEDED to avoid
7599 * any transients in the value of "sync_action".
7601 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
7602 /* Clear some bits that don't mean anything, but
7605 clear_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
7606 clear_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
7608 if (!test_and_clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
) ||
7609 test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
))
7611 /* no recovery is running.
7612 * remove any failed drives, then
7613 * add spares if possible.
7614 * Spare are also removed and re-added, to allow
7615 * the personality to fail the re-add.
7618 if (mddev
->reshape_position
!= MaxSector
) {
7619 if (mddev
->pers
->check_reshape
== NULL
||
7620 mddev
->pers
->check_reshape(mddev
) != 0)
7621 /* Cannot proceed */
7623 set_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
7624 clear_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
7625 } else if ((spares
= remove_and_add_spares(mddev
))) {
7626 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
7627 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
7628 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
7629 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
7630 } else if (mddev
->recovery_cp
< MaxSector
) {
7631 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
7632 clear_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
7633 } else if (!test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
7634 /* nothing to be done ... */
7637 if (mddev
->pers
->sync_request
) {
7638 if (spares
&& mddev
->bitmap
&& ! mddev
->bitmap
->file
) {
7639 /* We are adding a device or devices to an array
7640 * which has the bitmap stored on all devices.
7641 * So make sure all bitmap pages get written
7643 bitmap_write_all(mddev
->bitmap
);
7645 mddev
->sync_thread
= md_register_thread(md_do_sync
,
7648 if (!mddev
->sync_thread
) {
7649 printk(KERN_ERR
"%s: could not start resync"
7652 /* leave the spares where they are, it shouldn't hurt */
7653 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
7654 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
7655 clear_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
7656 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
7657 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
7659 md_wakeup_thread(mddev
->sync_thread
);
7660 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
7661 md_new_event(mddev
);
7664 if (!mddev
->sync_thread
) {
7665 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
7666 if (test_and_clear_bit(MD_RECOVERY_RECOVER
,
7668 if (mddev
->sysfs_action
)
7669 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
7671 mddev_unlock(mddev
);
7675 void md_wait_for_blocked_rdev(struct md_rdev
*rdev
, struct mddev
*mddev
)
7677 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
7678 wait_event_timeout(rdev
->blocked_wait
,
7679 !test_bit(Blocked
, &rdev
->flags
) &&
7680 !test_bit(BlockedBadBlocks
, &rdev
->flags
),
7681 msecs_to_jiffies(5000));
7682 rdev_dec_pending(rdev
, mddev
);
7684 EXPORT_SYMBOL(md_wait_for_blocked_rdev
);
7687 /* Bad block management.
7688 * We can record which blocks on each device are 'bad' and so just
7689 * fail those blocks, or that stripe, rather than the whole device.
7690 * Entries in the bad-block table are 64bits wide. This comprises:
7691 * Length of bad-range, in sectors: 0-511 for lengths 1-512
7692 * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes)
7693 * A 'shift' can be set so that larger blocks are tracked and
7694 * consequently larger devices can be covered.
7695 * 'Acknowledged' flag - 1 bit. - the most significant bit.
7697 * Locking of the bad-block table uses a seqlock so md_is_badblock
7698 * might need to retry if it is very unlucky.
7699 * We will sometimes want to check for bad blocks in a bi_end_io function,
7700 * so we use the write_seqlock_irq variant.
7702 * When looking for a bad block we specify a range and want to
7703 * know if any block in the range is bad. So we binary-search
7704 * to the last range that starts at-or-before the given endpoint,
7705 * (or "before the sector after the target range")
7706 * then see if it ends after the given start.
7708 * 0 if there are no known bad blocks in the range
7709 * 1 if there are known bad block which are all acknowledged
7710 * -1 if there are bad blocks which have not yet been acknowledged in metadata.
7711 * plus the start/length of the first bad section we overlap.
7713 int md_is_badblock(struct badblocks
*bb
, sector_t s
, int sectors
,
7714 sector_t
*first_bad
, int *bad_sectors
)
7720 sector_t target
= s
+ sectors
;
7723 if (bb
->shift
> 0) {
7724 /* round the start down, and the end up */
7726 target
+= (1<<bb
->shift
) - 1;
7727 target
>>= bb
->shift
;
7728 sectors
= target
- s
;
7730 /* 'target' is now the first block after the bad range */
7733 seq
= read_seqbegin(&bb
->lock
);
7738 /* Binary search between lo and hi for 'target'
7739 * i.e. for the last range that starts before 'target'
7741 /* INVARIANT: ranges before 'lo' and at-or-after 'hi'
7742 * are known not to be the last range before target.
7743 * VARIANT: hi-lo is the number of possible
7744 * ranges, and decreases until it reaches 1
7746 while (hi
- lo
> 1) {
7747 int mid
= (lo
+ hi
) / 2;
7748 sector_t a
= BB_OFFSET(p
[mid
]);
7750 /* This could still be the one, earlier ranges
7754 /* This and later ranges are definitely out. */
7757 /* 'lo' might be the last that started before target, but 'hi' isn't */
7759 /* need to check all range that end after 's' to see if
7760 * any are unacknowledged.
7763 BB_OFFSET(p
[lo
]) + BB_LEN(p
[lo
]) > s
) {
7764 if (BB_OFFSET(p
[lo
]) < target
) {
7765 /* starts before the end, and finishes after
7766 * the start, so they must overlap
7768 if (rv
!= -1 && BB_ACK(p
[lo
]))
7772 *first_bad
= BB_OFFSET(p
[lo
]);
7773 *bad_sectors
= BB_LEN(p
[lo
]);
7779 if (read_seqretry(&bb
->lock
, seq
))
7784 EXPORT_SYMBOL_GPL(md_is_badblock
);
7787 * Add a range of bad blocks to the table.
7788 * This might extend the table, or might contract it
7789 * if two adjacent ranges can be merged.
7790 * We binary-search to find the 'insertion' point, then
7791 * decide how best to handle it.
7793 static int md_set_badblocks(struct badblocks
*bb
, sector_t s
, int sectors
,
7801 /* badblocks are disabled */
7805 /* round the start down, and the end up */
7806 sector_t next
= s
+ sectors
;
7808 next
+= (1<<bb
->shift
) - 1;
7813 write_seqlock_irq(&bb
->lock
);
7818 /* Find the last range that starts at-or-before 's' */
7819 while (hi
- lo
> 1) {
7820 int mid
= (lo
+ hi
) / 2;
7821 sector_t a
= BB_OFFSET(p
[mid
]);
7827 if (hi
> lo
&& BB_OFFSET(p
[lo
]) > s
)
7831 /* we found a range that might merge with the start
7834 sector_t a
= BB_OFFSET(p
[lo
]);
7835 sector_t e
= a
+ BB_LEN(p
[lo
]);
7836 int ack
= BB_ACK(p
[lo
]);
7838 /* Yes, we can merge with a previous range */
7839 if (s
== a
&& s
+ sectors
>= e
)
7840 /* new range covers old */
7843 ack
= ack
&& acknowledged
;
7845 if (e
< s
+ sectors
)
7847 if (e
- a
<= BB_MAX_LEN
) {
7848 p
[lo
] = BB_MAKE(a
, e
-a
, ack
);
7851 /* does not all fit in one range,
7852 * make p[lo] maximal
7854 if (BB_LEN(p
[lo
]) != BB_MAX_LEN
)
7855 p
[lo
] = BB_MAKE(a
, BB_MAX_LEN
, ack
);
7861 if (sectors
&& hi
< bb
->count
) {
7862 /* 'hi' points to the first range that starts after 's'.
7863 * Maybe we can merge with the start of that range */
7864 sector_t a
= BB_OFFSET(p
[hi
]);
7865 sector_t e
= a
+ BB_LEN(p
[hi
]);
7866 int ack
= BB_ACK(p
[hi
]);
7867 if (a
<= s
+ sectors
) {
7868 /* merging is possible */
7869 if (e
<= s
+ sectors
) {
7874 ack
= ack
&& acknowledged
;
7877 if (e
- a
<= BB_MAX_LEN
) {
7878 p
[hi
] = BB_MAKE(a
, e
-a
, ack
);
7881 p
[hi
] = BB_MAKE(a
, BB_MAX_LEN
, ack
);
7889 if (sectors
== 0 && hi
< bb
->count
) {
7890 /* we might be able to combine lo and hi */
7891 /* Note: 's' is at the end of 'lo' */
7892 sector_t a
= BB_OFFSET(p
[hi
]);
7893 int lolen
= BB_LEN(p
[lo
]);
7894 int hilen
= BB_LEN(p
[hi
]);
7895 int newlen
= lolen
+ hilen
- (s
- a
);
7896 if (s
>= a
&& newlen
< BB_MAX_LEN
) {
7897 /* yes, we can combine them */
7898 int ack
= BB_ACK(p
[lo
]) && BB_ACK(p
[hi
]);
7899 p
[lo
] = BB_MAKE(BB_OFFSET(p
[lo
]), newlen
, ack
);
7900 memmove(p
+ hi
, p
+ hi
+ 1,
7901 (bb
->count
- hi
- 1) * 8);
7906 /* didn't merge (it all).
7907 * Need to add a range just before 'hi' */
7908 if (bb
->count
>= MD_MAX_BADBLOCKS
) {
7909 /* No room for more */
7913 int this_sectors
= sectors
;
7914 memmove(p
+ hi
+ 1, p
+ hi
,
7915 (bb
->count
- hi
) * 8);
7918 if (this_sectors
> BB_MAX_LEN
)
7919 this_sectors
= BB_MAX_LEN
;
7920 p
[hi
] = BB_MAKE(s
, this_sectors
, acknowledged
);
7921 sectors
-= this_sectors
;
7928 bb
->unacked_exist
= 1;
7929 write_sequnlock_irq(&bb
->lock
);
7934 int rdev_set_badblocks(struct md_rdev
*rdev
, sector_t s
, int sectors
,
7937 int rv
= md_set_badblocks(&rdev
->badblocks
,
7938 s
+ rdev
->data_offset
, sectors
, acknowledged
);
7940 /* Make sure they get written out promptly */
7941 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
7942 set_bit(MD_CHANGE_CLEAN
, &rdev
->mddev
->flags
);
7943 md_wakeup_thread(rdev
->mddev
->thread
);
7947 EXPORT_SYMBOL_GPL(rdev_set_badblocks
);
7950 * Remove a range of bad blocks from the table.
7951 * This may involve extending the table if we spilt a region,
7952 * but it must not fail. So if the table becomes full, we just
7953 * drop the remove request.
7955 static int md_clear_badblocks(struct badblocks
*bb
, sector_t s
, int sectors
)
7959 sector_t target
= s
+ sectors
;
7962 if (bb
->shift
> 0) {
7963 /* When clearing we round the start up and the end down.
7964 * This should not matter as the shift should align with
7965 * the block size and no rounding should ever be needed.
7966 * However it is better the think a block is bad when it
7967 * isn't than to think a block is not bad when it is.
7969 s
+= (1<<bb
->shift
) - 1;
7971 target
>>= bb
->shift
;
7972 sectors
= target
- s
;
7975 write_seqlock_irq(&bb
->lock
);
7980 /* Find the last range that starts before 'target' */
7981 while (hi
- lo
> 1) {
7982 int mid
= (lo
+ hi
) / 2;
7983 sector_t a
= BB_OFFSET(p
[mid
]);
7990 /* p[lo] is the last range that could overlap the
7991 * current range. Earlier ranges could also overlap,
7992 * but only this one can overlap the end of the range.
7994 if (BB_OFFSET(p
[lo
]) + BB_LEN(p
[lo
]) > target
) {
7995 /* Partial overlap, leave the tail of this range */
7996 int ack
= BB_ACK(p
[lo
]);
7997 sector_t a
= BB_OFFSET(p
[lo
]);
7998 sector_t end
= a
+ BB_LEN(p
[lo
]);
8001 /* we need to split this range */
8002 if (bb
->count
>= MD_MAX_BADBLOCKS
) {
8006 memmove(p
+lo
+1, p
+lo
, (bb
->count
- lo
) * 8);
8008 p
[lo
] = BB_MAKE(a
, s
-a
, ack
);
8011 p
[lo
] = BB_MAKE(target
, end
- target
, ack
);
8012 /* there is no longer an overlap */
8017 BB_OFFSET(p
[lo
]) + BB_LEN(p
[lo
]) > s
) {
8018 /* This range does overlap */
8019 if (BB_OFFSET(p
[lo
]) < s
) {
8020 /* Keep the early parts of this range. */
8021 int ack
= BB_ACK(p
[lo
]);
8022 sector_t start
= BB_OFFSET(p
[lo
]);
8023 p
[lo
] = BB_MAKE(start
, s
- start
, ack
);
8024 /* now low doesn't overlap, so.. */
8029 /* 'lo' is strictly before, 'hi' is strictly after,
8030 * anything between needs to be discarded
8033 memmove(p
+lo
+1, p
+hi
, (bb
->count
- hi
) * 8);
8034 bb
->count
-= (hi
- lo
- 1);
8040 write_sequnlock_irq(&bb
->lock
);
8044 int rdev_clear_badblocks(struct md_rdev
*rdev
, sector_t s
, int sectors
)
8046 return md_clear_badblocks(&rdev
->badblocks
,
8047 s
+ rdev
->data_offset
,
8050 EXPORT_SYMBOL_GPL(rdev_clear_badblocks
);
8053 * Acknowledge all bad blocks in a list.
8054 * This only succeeds if ->changed is clear. It is used by
8055 * in-kernel metadata updates
8057 void md_ack_all_badblocks(struct badblocks
*bb
)
8059 if (bb
->page
== NULL
|| bb
->changed
)
8060 /* no point even trying */
8062 write_seqlock_irq(&bb
->lock
);
8064 if (bb
->changed
== 0 && bb
->unacked_exist
) {
8067 for (i
= 0; i
< bb
->count
; i
++) {
8068 if (!BB_ACK(p
[i
])) {
8069 sector_t start
= BB_OFFSET(p
[i
]);
8070 int len
= BB_LEN(p
[i
]);
8071 p
[i
] = BB_MAKE(start
, len
, 1);
8074 bb
->unacked_exist
= 0;
8076 write_sequnlock_irq(&bb
->lock
);
8078 EXPORT_SYMBOL_GPL(md_ack_all_badblocks
);
8080 /* sysfs access to bad-blocks list.
8081 * We present two files.
8082 * 'bad-blocks' lists sector numbers and lengths of ranges that
8083 * are recorded as bad. The list is truncated to fit within
8084 * the one-page limit of sysfs.
8085 * Writing "sector length" to this file adds an acknowledged
8087 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
8088 * been acknowledged. Writing to this file adds bad blocks
8089 * without acknowledging them. This is largely for testing.
8093 badblocks_show(struct badblocks
*bb
, char *page
, int unack
)
8104 seq
= read_seqbegin(&bb
->lock
);
8109 while (len
< PAGE_SIZE
&& i
< bb
->count
) {
8110 sector_t s
= BB_OFFSET(p
[i
]);
8111 unsigned int length
= BB_LEN(p
[i
]);
8112 int ack
= BB_ACK(p
[i
]);
8118 len
+= snprintf(page
+len
, PAGE_SIZE
-len
, "%llu %u\n",
8119 (unsigned long long)s
<< bb
->shift
,
8120 length
<< bb
->shift
);
8122 if (unack
&& len
== 0)
8123 bb
->unacked_exist
= 0;
8125 if (read_seqretry(&bb
->lock
, seq
))
8134 badblocks_store(struct badblocks
*bb
, const char *page
, size_t len
, int unack
)
8136 unsigned long long sector
;
8140 /* Allow clearing via sysfs *only* for testing/debugging.
8141 * Normally only a successful write may clear a badblock
8144 if (page
[0] == '-') {
8148 #endif /* DO_DEBUG */
8150 switch (sscanf(page
, "%llu %d%c", §or
, &length
, &newline
)) {
8152 if (newline
!= '\n')
8164 md_clear_badblocks(bb
, sector
, length
);
8167 #endif /* DO_DEBUG */
8168 if (md_set_badblocks(bb
, sector
, length
, !unack
))
8174 static int md_notify_reboot(struct notifier_block
*this,
8175 unsigned long code
, void *x
)
8177 struct list_head
*tmp
;
8178 struct mddev
*mddev
;
8181 for_each_mddev(mddev
, tmp
) {
8182 if (mddev_trylock(mddev
)) {
8184 __md_stop_writes(mddev
);
8185 if (mddev
->persistent
)
8186 mddev
->safemode
= 2;
8187 mddev_unlock(mddev
);
8192 * certain more exotic SCSI devices are known to be
8193 * volatile wrt too early system reboots. While the
8194 * right place to handle this issue is the given
8195 * driver, we do want to have a safe RAID driver ...
8203 static struct notifier_block md_notifier
= {
8204 .notifier_call
= md_notify_reboot
,
8206 .priority
= INT_MAX
, /* before any real devices */
8209 static void md_geninit(void)
8211 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t
));
8213 proc_create("mdstat", S_IRUGO
, NULL
, &md_seq_fops
);
8216 static int __init
md_init(void)
8220 md_wq
= alloc_workqueue("md", WQ_MEM_RECLAIM
, 0);
8224 md_misc_wq
= alloc_workqueue("md_misc", 0, 0);
8228 if ((ret
= register_blkdev(MD_MAJOR
, "md")) < 0)
8231 if ((ret
= register_blkdev(0, "mdp")) < 0)
8235 blk_register_region(MKDEV(MD_MAJOR
, 0), 1UL<<MINORBITS
, THIS_MODULE
,
8236 md_probe
, NULL
, NULL
);
8237 blk_register_region(MKDEV(mdp_major
, 0), 1UL<<MINORBITS
, THIS_MODULE
,
8238 md_probe
, NULL
, NULL
);
8240 register_reboot_notifier(&md_notifier
);
8241 raid_table_header
= register_sysctl_table(raid_root_table
);
8247 unregister_blkdev(MD_MAJOR
, "md");
8249 destroy_workqueue(md_misc_wq
);
8251 destroy_workqueue(md_wq
);
8259 * Searches all registered partitions for autorun RAID arrays
8263 static LIST_HEAD(all_detected_devices
);
8264 struct detected_devices_node
{
8265 struct list_head list
;
8269 void md_autodetect_dev(dev_t dev
)
8271 struct detected_devices_node
*node_detected_dev
;
8273 node_detected_dev
= kzalloc(sizeof(*node_detected_dev
), GFP_KERNEL
);
8274 if (node_detected_dev
) {
8275 node_detected_dev
->dev
= dev
;
8276 list_add_tail(&node_detected_dev
->list
, &all_detected_devices
);
8278 printk(KERN_CRIT
"md: md_autodetect_dev: kzalloc failed"
8279 ", skipping dev(%d,%d)\n", MAJOR(dev
), MINOR(dev
));
8284 static void autostart_arrays(int part
)
8286 struct md_rdev
*rdev
;
8287 struct detected_devices_node
*node_detected_dev
;
8289 int i_scanned
, i_passed
;
8294 printk(KERN_INFO
"md: Autodetecting RAID arrays.\n");
8296 while (!list_empty(&all_detected_devices
) && i_scanned
< INT_MAX
) {
8298 node_detected_dev
= list_entry(all_detected_devices
.next
,
8299 struct detected_devices_node
, list
);
8300 list_del(&node_detected_dev
->list
);
8301 dev
= node_detected_dev
->dev
;
8302 kfree(node_detected_dev
);
8303 rdev
= md_import_device(dev
,0, 90);
8307 if (test_bit(Faulty
, &rdev
->flags
)) {
8311 set_bit(AutoDetected
, &rdev
->flags
);
8312 list_add(&rdev
->same_set
, &pending_raid_disks
);
8316 printk(KERN_INFO
"md: Scanned %d and added %d devices.\n",
8317 i_scanned
, i_passed
);
8319 autorun_devices(part
);
8322 #endif /* !MODULE */
8324 static __exit
void md_exit(void)
8326 struct mddev
*mddev
;
8327 struct list_head
*tmp
;
8329 blk_unregister_region(MKDEV(MD_MAJOR
,0), 1U << MINORBITS
);
8330 blk_unregister_region(MKDEV(mdp_major
,0), 1U << MINORBITS
);
8332 unregister_blkdev(MD_MAJOR
,"md");
8333 unregister_blkdev(mdp_major
, "mdp");
8334 unregister_reboot_notifier(&md_notifier
);
8335 unregister_sysctl_table(raid_table_header
);
8336 remove_proc_entry("mdstat", NULL
);
8337 for_each_mddev(mddev
, tmp
) {
8338 export_array(mddev
);
8339 mddev
->hold_active
= 0;
8341 destroy_workqueue(md_misc_wq
);
8342 destroy_workqueue(md_wq
);
8345 subsys_initcall(md_init
);
8346 module_exit(md_exit
)
8348 static int get_ro(char *buffer
, struct kernel_param
*kp
)
8350 return sprintf(buffer
, "%d", start_readonly
);
8352 static int set_ro(const char *val
, struct kernel_param
*kp
)
8355 int num
= simple_strtoul(val
, &e
, 10);
8356 if (*val
&& (*e
== '\0' || *e
== '\n')) {
8357 start_readonly
= num
;
8363 module_param_call(start_ro
, set_ro
, get_ro
, NULL
, S_IRUSR
|S_IWUSR
);
8364 module_param(start_dirty_degraded
, int, S_IRUGO
|S_IWUSR
);
8366 module_param_call(new_array
, add_named_array
, NULL
, NULL
, S_IWUSR
);
8368 EXPORT_SYMBOL(register_md_personality
);
8369 EXPORT_SYMBOL(unregister_md_personality
);
8370 EXPORT_SYMBOL(md_error
);
8371 EXPORT_SYMBOL(md_done_sync
);
8372 EXPORT_SYMBOL(md_write_start
);
8373 EXPORT_SYMBOL(md_write_end
);
8374 EXPORT_SYMBOL(md_register_thread
);
8375 EXPORT_SYMBOL(md_unregister_thread
);
8376 EXPORT_SYMBOL(md_wakeup_thread
);
8377 EXPORT_SYMBOL(md_check_recovery
);
8378 MODULE_LICENSE("GPL");
8379 MODULE_DESCRIPTION("MD RAID framework");
8381 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR
);