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>
56 #include "md-cluster.h"
59 static void autostart_arrays(int part
);
62 /* pers_list is a list of registered personalities protected
64 * pers_lock does extra service to protect accesses to
65 * mddev->thread when the mutex cannot be held.
67 static LIST_HEAD(pers_list
);
68 static DEFINE_SPINLOCK(pers_lock
);
70 struct md_cluster_operations
*md_cluster_ops
;
71 EXPORT_SYMBOL(md_cluster_ops
);
72 struct module
*md_cluster_mod
;
73 EXPORT_SYMBOL(md_cluster_mod
);
75 static DECLARE_WAIT_QUEUE_HEAD(resync_wait
);
76 static struct workqueue_struct
*md_wq
;
77 static struct workqueue_struct
*md_misc_wq
;
79 static int remove_and_add_spares(struct mddev
*mddev
,
80 struct md_rdev
*this);
81 static void mddev_detach(struct mddev
*mddev
);
84 * Default number of read corrections we'll attempt on an rdev
85 * before ejecting it from the array. We divide the read error
86 * count by 2 for every hour elapsed between read errors.
88 #define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
90 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
91 * is 1000 KB/sec, so the extra system load does not show up that much.
92 * Increase it if you want to have more _guaranteed_ speed. Note that
93 * the RAID driver will use the maximum available bandwidth if the IO
94 * subsystem is idle. There is also an 'absolute maximum' reconstruction
95 * speed limit - in case reconstruction slows down your system despite
98 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
99 * or /sys/block/mdX/md/sync_speed_{min,max}
102 static int sysctl_speed_limit_min
= 1000;
103 static int sysctl_speed_limit_max
= 200000;
104 static inline int speed_min(struct mddev
*mddev
)
106 return mddev
->sync_speed_min
?
107 mddev
->sync_speed_min
: sysctl_speed_limit_min
;
110 static inline int speed_max(struct mddev
*mddev
)
112 return mddev
->sync_speed_max
?
113 mddev
->sync_speed_max
: sysctl_speed_limit_max
;
116 static struct ctl_table_header
*raid_table_header
;
118 static struct ctl_table raid_table
[] = {
120 .procname
= "speed_limit_min",
121 .data
= &sysctl_speed_limit_min
,
122 .maxlen
= sizeof(int),
123 .mode
= S_IRUGO
|S_IWUSR
,
124 .proc_handler
= proc_dointvec
,
127 .procname
= "speed_limit_max",
128 .data
= &sysctl_speed_limit_max
,
129 .maxlen
= sizeof(int),
130 .mode
= S_IRUGO
|S_IWUSR
,
131 .proc_handler
= proc_dointvec
,
136 static struct ctl_table raid_dir_table
[] = {
140 .mode
= S_IRUGO
|S_IXUGO
,
146 static struct ctl_table raid_root_table
[] = {
151 .child
= raid_dir_table
,
156 static const struct block_device_operations md_fops
;
158 static int start_readonly
;
161 * like bio_clone, but with a local bio set
164 struct bio
*bio_alloc_mddev(gfp_t gfp_mask
, int nr_iovecs
,
169 if (!mddev
|| !mddev
->bio_set
)
170 return bio_alloc(gfp_mask
, nr_iovecs
);
172 b
= bio_alloc_bioset(gfp_mask
, nr_iovecs
, mddev
->bio_set
);
177 EXPORT_SYMBOL_GPL(bio_alloc_mddev
);
179 struct bio
*bio_clone_mddev(struct bio
*bio
, gfp_t gfp_mask
,
182 if (!mddev
|| !mddev
->bio_set
)
183 return bio_clone(bio
, gfp_mask
);
185 return bio_clone_bioset(bio
, gfp_mask
, mddev
->bio_set
);
187 EXPORT_SYMBOL_GPL(bio_clone_mddev
);
190 * We have a system wide 'event count' that is incremented
191 * on any 'interesting' event, and readers of /proc/mdstat
192 * can use 'poll' or 'select' to find out when the event
196 * start array, stop array, error, add device, remove device,
197 * start build, activate spare
199 static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters
);
200 static atomic_t md_event_count
;
201 void md_new_event(struct mddev
*mddev
)
203 atomic_inc(&md_event_count
);
204 wake_up(&md_event_waiters
);
206 EXPORT_SYMBOL_GPL(md_new_event
);
208 /* Alternate version that can be called from interrupts
209 * when calling sysfs_notify isn't needed.
211 static void md_new_event_inintr(struct mddev
*mddev
)
213 atomic_inc(&md_event_count
);
214 wake_up(&md_event_waiters
);
218 * Enables to iterate over all existing md arrays
219 * all_mddevs_lock protects this list.
221 static LIST_HEAD(all_mddevs
);
222 static DEFINE_SPINLOCK(all_mddevs_lock
);
225 * iterates through all used mddevs in the system.
226 * We take care to grab the all_mddevs_lock whenever navigating
227 * the list, and to always hold a refcount when unlocked.
228 * Any code which breaks out of this loop while own
229 * a reference to the current mddev and must mddev_put it.
231 #define for_each_mddev(_mddev,_tmp) \
233 for (({ spin_lock(&all_mddevs_lock); \
234 _tmp = all_mddevs.next; \
236 ({ if (_tmp != &all_mddevs) \
237 mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\
238 spin_unlock(&all_mddevs_lock); \
239 if (_mddev) mddev_put(_mddev); \
240 _mddev = list_entry(_tmp, struct mddev, all_mddevs); \
241 _tmp != &all_mddevs;}); \
242 ({ spin_lock(&all_mddevs_lock); \
243 _tmp = _tmp->next;}) \
246 /* Rather than calling directly into the personality make_request function,
247 * IO requests come here first so that we can check if the device is
248 * being suspended pending a reconfiguration.
249 * We hold a refcount over the call to ->make_request. By the time that
250 * call has finished, the bio has been linked into some internal structure
251 * and so is visible to ->quiesce(), so we don't need the refcount any more.
253 static blk_qc_t
md_make_request(struct request_queue
*q
, struct bio
*bio
)
255 const int rw
= bio_data_dir(bio
);
256 struct mddev
*mddev
= q
->queuedata
;
257 unsigned int sectors
;
260 blk_queue_split(q
, &bio
, q
->bio_split
);
262 if (mddev
== NULL
|| mddev
->pers
== NULL
265 return BLK_QC_T_NONE
;
267 if (mddev
->ro
== 1 && unlikely(rw
== WRITE
)) {
268 if (bio_sectors(bio
) != 0)
269 bio
->bi_error
= -EROFS
;
271 return BLK_QC_T_NONE
;
273 smp_rmb(); /* Ensure implications of 'active' are visible */
275 if (mddev
->suspended
) {
278 prepare_to_wait(&mddev
->sb_wait
, &__wait
,
279 TASK_UNINTERRUPTIBLE
);
280 if (!mddev
->suspended
)
286 finish_wait(&mddev
->sb_wait
, &__wait
);
288 atomic_inc(&mddev
->active_io
);
292 * save the sectors now since our bio can
293 * go away inside make_request
295 sectors
= bio_sectors(bio
);
296 mddev
->pers
->make_request(mddev
, bio
);
298 cpu
= part_stat_lock();
299 part_stat_inc(cpu
, &mddev
->gendisk
->part0
, ios
[rw
]);
300 part_stat_add(cpu
, &mddev
->gendisk
->part0
, sectors
[rw
], sectors
);
303 if (atomic_dec_and_test(&mddev
->active_io
) && mddev
->suspended
)
304 wake_up(&mddev
->sb_wait
);
306 return BLK_QC_T_NONE
;
309 /* mddev_suspend makes sure no new requests are submitted
310 * to the device, and that any requests that have been submitted
311 * are completely handled.
312 * Once mddev_detach() is called and completes, the module will be
315 void mddev_suspend(struct mddev
*mddev
)
317 BUG_ON(mddev
->suspended
);
318 mddev
->suspended
= 1;
320 wait_event(mddev
->sb_wait
, atomic_read(&mddev
->active_io
) == 0);
321 mddev
->pers
->quiesce(mddev
, 1);
323 del_timer_sync(&mddev
->safemode_timer
);
325 EXPORT_SYMBOL_GPL(mddev_suspend
);
327 void mddev_resume(struct mddev
*mddev
)
329 mddev
->suspended
= 0;
330 wake_up(&mddev
->sb_wait
);
331 mddev
->pers
->quiesce(mddev
, 0);
333 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
334 md_wakeup_thread(mddev
->thread
);
335 md_wakeup_thread(mddev
->sync_thread
); /* possibly kick off a reshape */
337 EXPORT_SYMBOL_GPL(mddev_resume
);
339 int mddev_congested(struct mddev
*mddev
, int bits
)
341 struct md_personality
*pers
= mddev
->pers
;
345 if (mddev
->suspended
)
347 else if (pers
&& pers
->congested
)
348 ret
= pers
->congested(mddev
, bits
);
352 EXPORT_SYMBOL_GPL(mddev_congested
);
353 static int md_congested(void *data
, int bits
)
355 struct mddev
*mddev
= data
;
356 return mddev_congested(mddev
, bits
);
360 * Generic flush handling for md
363 static void md_end_flush(struct bio
*bio
)
365 struct md_rdev
*rdev
= bio
->bi_private
;
366 struct mddev
*mddev
= rdev
->mddev
;
368 rdev_dec_pending(rdev
, mddev
);
370 if (atomic_dec_and_test(&mddev
->flush_pending
)) {
371 /* The pre-request flush has finished */
372 queue_work(md_wq
, &mddev
->flush_work
);
377 static void md_submit_flush_data(struct work_struct
*ws
);
379 static void submit_flushes(struct work_struct
*ws
)
381 struct mddev
*mddev
= container_of(ws
, struct mddev
, flush_work
);
382 struct md_rdev
*rdev
;
384 INIT_WORK(&mddev
->flush_work
, md_submit_flush_data
);
385 atomic_set(&mddev
->flush_pending
, 1);
387 rdev_for_each_rcu(rdev
, mddev
)
388 if (rdev
->raid_disk
>= 0 &&
389 !test_bit(Faulty
, &rdev
->flags
)) {
390 /* Take two references, one is dropped
391 * when request finishes, one after
392 * we reclaim rcu_read_lock
395 atomic_inc(&rdev
->nr_pending
);
396 atomic_inc(&rdev
->nr_pending
);
398 bi
= bio_alloc_mddev(GFP_NOIO
, 0, mddev
);
399 bi
->bi_end_io
= md_end_flush
;
400 bi
->bi_private
= rdev
;
401 bi
->bi_bdev
= rdev
->bdev
;
402 atomic_inc(&mddev
->flush_pending
);
403 submit_bio(WRITE_FLUSH
, bi
);
405 rdev_dec_pending(rdev
, mddev
);
408 if (atomic_dec_and_test(&mddev
->flush_pending
))
409 queue_work(md_wq
, &mddev
->flush_work
);
412 static void md_submit_flush_data(struct work_struct
*ws
)
414 struct mddev
*mddev
= container_of(ws
, struct mddev
, flush_work
);
415 struct bio
*bio
= mddev
->flush_bio
;
417 if (bio
->bi_iter
.bi_size
== 0)
418 /* an empty barrier - all done */
421 bio
->bi_rw
&= ~REQ_FLUSH
;
422 mddev
->pers
->make_request(mddev
, bio
);
425 mddev
->flush_bio
= NULL
;
426 wake_up(&mddev
->sb_wait
);
429 void md_flush_request(struct mddev
*mddev
, struct bio
*bio
)
431 spin_lock_irq(&mddev
->lock
);
432 wait_event_lock_irq(mddev
->sb_wait
,
435 mddev
->flush_bio
= bio
;
436 spin_unlock_irq(&mddev
->lock
);
438 INIT_WORK(&mddev
->flush_work
, submit_flushes
);
439 queue_work(md_wq
, &mddev
->flush_work
);
441 EXPORT_SYMBOL(md_flush_request
);
443 void md_unplug(struct blk_plug_cb
*cb
, bool from_schedule
)
445 struct mddev
*mddev
= cb
->data
;
446 md_wakeup_thread(mddev
->thread
);
449 EXPORT_SYMBOL(md_unplug
);
451 static inline struct mddev
*mddev_get(struct mddev
*mddev
)
453 atomic_inc(&mddev
->active
);
457 static void mddev_delayed_delete(struct work_struct
*ws
);
459 static void mddev_put(struct mddev
*mddev
)
461 struct bio_set
*bs
= NULL
;
463 if (!atomic_dec_and_lock(&mddev
->active
, &all_mddevs_lock
))
465 if (!mddev
->raid_disks
&& list_empty(&mddev
->disks
) &&
466 mddev
->ctime
== 0 && !mddev
->hold_active
) {
467 /* Array is not configured at all, and not held active,
469 list_del_init(&mddev
->all_mddevs
);
471 mddev
->bio_set
= NULL
;
472 if (mddev
->gendisk
) {
473 /* We did a probe so need to clean up. Call
474 * queue_work inside the spinlock so that
475 * flush_workqueue() after mddev_find will
476 * succeed in waiting for the work to be done.
478 INIT_WORK(&mddev
->del_work
, mddev_delayed_delete
);
479 queue_work(md_misc_wq
, &mddev
->del_work
);
483 spin_unlock(&all_mddevs_lock
);
488 static void md_safemode_timeout(unsigned long data
);
490 void mddev_init(struct mddev
*mddev
)
492 mutex_init(&mddev
->open_mutex
);
493 mutex_init(&mddev
->reconfig_mutex
);
494 mutex_init(&mddev
->bitmap_info
.mutex
);
495 INIT_LIST_HEAD(&mddev
->disks
);
496 INIT_LIST_HEAD(&mddev
->all_mddevs
);
497 setup_timer(&mddev
->safemode_timer
, md_safemode_timeout
,
498 (unsigned long) mddev
);
499 atomic_set(&mddev
->active
, 1);
500 atomic_set(&mddev
->openers
, 0);
501 atomic_set(&mddev
->active_io
, 0);
502 spin_lock_init(&mddev
->lock
);
503 atomic_set(&mddev
->flush_pending
, 0);
504 init_waitqueue_head(&mddev
->sb_wait
);
505 init_waitqueue_head(&mddev
->recovery_wait
);
506 mddev
->reshape_position
= MaxSector
;
507 mddev
->reshape_backwards
= 0;
508 mddev
->last_sync_action
= "none";
509 mddev
->resync_min
= 0;
510 mddev
->resync_max
= MaxSector
;
511 mddev
->level
= LEVEL_NONE
;
513 EXPORT_SYMBOL_GPL(mddev_init
);
515 static struct mddev
*mddev_find(dev_t unit
)
517 struct mddev
*mddev
, *new = NULL
;
519 if (unit
&& MAJOR(unit
) != MD_MAJOR
)
520 unit
&= ~((1<<MdpMinorShift
)-1);
523 spin_lock(&all_mddevs_lock
);
526 list_for_each_entry(mddev
, &all_mddevs
, all_mddevs
)
527 if (mddev
->unit
== unit
) {
529 spin_unlock(&all_mddevs_lock
);
535 list_add(&new->all_mddevs
, &all_mddevs
);
536 spin_unlock(&all_mddevs_lock
);
537 new->hold_active
= UNTIL_IOCTL
;
541 /* find an unused unit number */
542 static int next_minor
= 512;
543 int start
= next_minor
;
547 dev
= MKDEV(MD_MAJOR
, next_minor
);
549 if (next_minor
> MINORMASK
)
551 if (next_minor
== start
) {
552 /* Oh dear, all in use. */
553 spin_unlock(&all_mddevs_lock
);
559 list_for_each_entry(mddev
, &all_mddevs
, all_mddevs
)
560 if (mddev
->unit
== dev
) {
566 new->md_minor
= MINOR(dev
);
567 new->hold_active
= UNTIL_STOP
;
568 list_add(&new->all_mddevs
, &all_mddevs
);
569 spin_unlock(&all_mddevs_lock
);
572 spin_unlock(&all_mddevs_lock
);
574 new = kzalloc(sizeof(*new), GFP_KERNEL
);
579 if (MAJOR(unit
) == MD_MAJOR
)
580 new->md_minor
= MINOR(unit
);
582 new->md_minor
= MINOR(unit
) >> MdpMinorShift
;
589 static struct attribute_group md_redundancy_group
;
591 void mddev_unlock(struct mddev
*mddev
)
593 if (mddev
->to_remove
) {
594 /* These cannot be removed under reconfig_mutex as
595 * an access to the files will try to take reconfig_mutex
596 * while holding the file unremovable, which leads to
598 * So hold set sysfs_active while the remove in happeing,
599 * and anything else which might set ->to_remove or my
600 * otherwise change the sysfs namespace will fail with
601 * -EBUSY if sysfs_active is still set.
602 * We set sysfs_active under reconfig_mutex and elsewhere
603 * test it under the same mutex to ensure its correct value
606 struct attribute_group
*to_remove
= mddev
->to_remove
;
607 mddev
->to_remove
= NULL
;
608 mddev
->sysfs_active
= 1;
609 mutex_unlock(&mddev
->reconfig_mutex
);
611 if (mddev
->kobj
.sd
) {
612 if (to_remove
!= &md_redundancy_group
)
613 sysfs_remove_group(&mddev
->kobj
, to_remove
);
614 if (mddev
->pers
== NULL
||
615 mddev
->pers
->sync_request
== NULL
) {
616 sysfs_remove_group(&mddev
->kobj
, &md_redundancy_group
);
617 if (mddev
->sysfs_action
)
618 sysfs_put(mddev
->sysfs_action
);
619 mddev
->sysfs_action
= NULL
;
622 mddev
->sysfs_active
= 0;
624 mutex_unlock(&mddev
->reconfig_mutex
);
626 /* As we've dropped the mutex we need a spinlock to
627 * make sure the thread doesn't disappear
629 spin_lock(&pers_lock
);
630 md_wakeup_thread(mddev
->thread
);
631 spin_unlock(&pers_lock
);
633 EXPORT_SYMBOL_GPL(mddev_unlock
);
635 struct md_rdev
*md_find_rdev_nr_rcu(struct mddev
*mddev
, int nr
)
637 struct md_rdev
*rdev
;
639 rdev_for_each_rcu(rdev
, mddev
)
640 if (rdev
->desc_nr
== nr
)
645 EXPORT_SYMBOL_GPL(md_find_rdev_nr_rcu
);
647 static struct md_rdev
*find_rdev(struct mddev
*mddev
, dev_t dev
)
649 struct md_rdev
*rdev
;
651 rdev_for_each(rdev
, mddev
)
652 if (rdev
->bdev
->bd_dev
== dev
)
658 static struct md_rdev
*find_rdev_rcu(struct mddev
*mddev
, dev_t dev
)
660 struct md_rdev
*rdev
;
662 rdev_for_each_rcu(rdev
, mddev
)
663 if (rdev
->bdev
->bd_dev
== dev
)
669 static struct md_personality
*find_pers(int level
, char *clevel
)
671 struct md_personality
*pers
;
672 list_for_each_entry(pers
, &pers_list
, list
) {
673 if (level
!= LEVEL_NONE
&& pers
->level
== level
)
675 if (strcmp(pers
->name
, clevel
)==0)
681 /* return the offset of the super block in 512byte sectors */
682 static inline sector_t
calc_dev_sboffset(struct md_rdev
*rdev
)
684 sector_t num_sectors
= i_size_read(rdev
->bdev
->bd_inode
) / 512;
685 return MD_NEW_SIZE_SECTORS(num_sectors
);
688 static int alloc_disk_sb(struct md_rdev
*rdev
)
690 rdev
->sb_page
= alloc_page(GFP_KERNEL
);
691 if (!rdev
->sb_page
) {
692 printk(KERN_ALERT
"md: out of memory.\n");
699 void md_rdev_clear(struct md_rdev
*rdev
)
702 put_page(rdev
->sb_page
);
704 rdev
->sb_page
= NULL
;
709 put_page(rdev
->bb_page
);
710 rdev
->bb_page
= NULL
;
712 kfree(rdev
->badblocks
.page
);
713 rdev
->badblocks
.page
= NULL
;
715 EXPORT_SYMBOL_GPL(md_rdev_clear
);
717 static void super_written(struct bio
*bio
)
719 struct md_rdev
*rdev
= bio
->bi_private
;
720 struct mddev
*mddev
= rdev
->mddev
;
723 printk("md: super_written gets error=%d\n", bio
->bi_error
);
724 md_error(mddev
, rdev
);
727 if (atomic_dec_and_test(&mddev
->pending_writes
))
728 wake_up(&mddev
->sb_wait
);
732 void md_super_write(struct mddev
*mddev
, struct md_rdev
*rdev
,
733 sector_t sector
, int size
, struct page
*page
)
735 /* write first size bytes of page to sector of rdev
736 * Increment mddev->pending_writes before returning
737 * and decrement it on completion, waking up sb_wait
738 * if zero is reached.
739 * If an error occurred, call md_error
741 struct bio
*bio
= bio_alloc_mddev(GFP_NOIO
, 1, mddev
);
743 bio
->bi_bdev
= rdev
->meta_bdev
? rdev
->meta_bdev
: rdev
->bdev
;
744 bio
->bi_iter
.bi_sector
= sector
;
745 bio_add_page(bio
, page
, size
, 0);
746 bio
->bi_private
= rdev
;
747 bio
->bi_end_io
= super_written
;
749 atomic_inc(&mddev
->pending_writes
);
750 submit_bio(WRITE_FLUSH_FUA
, bio
);
753 void md_super_wait(struct mddev
*mddev
)
755 /* wait for all superblock writes that were scheduled to complete */
756 wait_event(mddev
->sb_wait
, atomic_read(&mddev
->pending_writes
)==0);
759 int sync_page_io(struct md_rdev
*rdev
, sector_t sector
, int size
,
760 struct page
*page
, int rw
, bool metadata_op
)
762 struct bio
*bio
= bio_alloc_mddev(GFP_NOIO
, 1, rdev
->mddev
);
765 bio
->bi_bdev
= (metadata_op
&& rdev
->meta_bdev
) ?
766 rdev
->meta_bdev
: rdev
->bdev
;
768 bio
->bi_iter
.bi_sector
= sector
+ rdev
->sb_start
;
769 else if (rdev
->mddev
->reshape_position
!= MaxSector
&&
770 (rdev
->mddev
->reshape_backwards
==
771 (sector
>= rdev
->mddev
->reshape_position
)))
772 bio
->bi_iter
.bi_sector
= sector
+ rdev
->new_data_offset
;
774 bio
->bi_iter
.bi_sector
= sector
+ rdev
->data_offset
;
775 bio_add_page(bio
, page
, size
, 0);
776 submit_bio_wait(rw
, bio
);
778 ret
= !bio
->bi_error
;
782 EXPORT_SYMBOL_GPL(sync_page_io
);
784 static int read_disk_sb(struct md_rdev
*rdev
, int size
)
786 char b
[BDEVNAME_SIZE
];
791 if (!sync_page_io(rdev
, 0, size
, rdev
->sb_page
, READ
, true))
797 printk(KERN_WARNING
"md: disabled device %s, could not read superblock.\n",
798 bdevname(rdev
->bdev
,b
));
802 static int uuid_equal(mdp_super_t
*sb1
, mdp_super_t
*sb2
)
804 return sb1
->set_uuid0
== sb2
->set_uuid0
&&
805 sb1
->set_uuid1
== sb2
->set_uuid1
&&
806 sb1
->set_uuid2
== sb2
->set_uuid2
&&
807 sb1
->set_uuid3
== sb2
->set_uuid3
;
810 static int sb_equal(mdp_super_t
*sb1
, mdp_super_t
*sb2
)
813 mdp_super_t
*tmp1
, *tmp2
;
815 tmp1
= kmalloc(sizeof(*tmp1
),GFP_KERNEL
);
816 tmp2
= kmalloc(sizeof(*tmp2
),GFP_KERNEL
);
818 if (!tmp1
|| !tmp2
) {
820 printk(KERN_INFO
"md.c sb_equal(): failed to allocate memory!\n");
828 * nr_disks is not constant
833 ret
= (memcmp(tmp1
, tmp2
, MD_SB_GENERIC_CONSTANT_WORDS
* 4) == 0);
840 static u32
md_csum_fold(u32 csum
)
842 csum
= (csum
& 0xffff) + (csum
>> 16);
843 return (csum
& 0xffff) + (csum
>> 16);
846 static unsigned int calc_sb_csum(mdp_super_t
*sb
)
849 u32
*sb32
= (u32
*)sb
;
851 unsigned int disk_csum
, csum
;
853 disk_csum
= sb
->sb_csum
;
856 for (i
= 0; i
< MD_SB_BYTES
/4 ; i
++)
858 csum
= (newcsum
& 0xffffffff) + (newcsum
>>32);
861 /* This used to use csum_partial, which was wrong for several
862 * reasons including that different results are returned on
863 * different architectures. It isn't critical that we get exactly
864 * the same return value as before (we always csum_fold before
865 * testing, and that removes any differences). However as we
866 * know that csum_partial always returned a 16bit value on
867 * alphas, do a fold to maximise conformity to previous behaviour.
869 sb
->sb_csum
= md_csum_fold(disk_csum
);
871 sb
->sb_csum
= disk_csum
;
877 * Handle superblock details.
878 * We want to be able to handle multiple superblock formats
879 * so we have a common interface to them all, and an array of
880 * different handlers.
881 * We rely on user-space to write the initial superblock, and support
882 * reading and updating of superblocks.
883 * Interface methods are:
884 * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
885 * loads and validates a superblock on dev.
886 * if refdev != NULL, compare superblocks on both devices
888 * 0 - dev has a superblock that is compatible with refdev
889 * 1 - dev has a superblock that is compatible and newer than refdev
890 * so dev should be used as the refdev in future
891 * -EINVAL superblock incompatible or invalid
892 * -othererror e.g. -EIO
894 * int validate_super(struct mddev *mddev, struct md_rdev *dev)
895 * Verify that dev is acceptable into mddev.
896 * The first time, mddev->raid_disks will be 0, and data from
897 * dev should be merged in. Subsequent calls check that dev
898 * is new enough. Return 0 or -EINVAL
900 * void sync_super(struct mddev *mddev, struct md_rdev *dev)
901 * Update the superblock for rdev with data in mddev
902 * This does not write to disc.
908 struct module
*owner
;
909 int (*load_super
)(struct md_rdev
*rdev
,
910 struct md_rdev
*refdev
,
912 int (*validate_super
)(struct mddev
*mddev
,
913 struct md_rdev
*rdev
);
914 void (*sync_super
)(struct mddev
*mddev
,
915 struct md_rdev
*rdev
);
916 unsigned long long (*rdev_size_change
)(struct md_rdev
*rdev
,
917 sector_t num_sectors
);
918 int (*allow_new_offset
)(struct md_rdev
*rdev
,
919 unsigned long long new_offset
);
923 * Check that the given mddev has no bitmap.
925 * This function is called from the run method of all personalities that do not
926 * support bitmaps. It prints an error message and returns non-zero if mddev
927 * has a bitmap. Otherwise, it returns 0.
930 int md_check_no_bitmap(struct mddev
*mddev
)
932 if (!mddev
->bitmap_info
.file
&& !mddev
->bitmap_info
.offset
)
934 printk(KERN_ERR
"%s: bitmaps are not supported for %s\n",
935 mdname(mddev
), mddev
->pers
->name
);
938 EXPORT_SYMBOL(md_check_no_bitmap
);
941 * load_super for 0.90.0
943 static int super_90_load(struct md_rdev
*rdev
, struct md_rdev
*refdev
, int minor_version
)
945 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
950 * Calculate the position of the superblock (512byte sectors),
951 * it's at the end of the disk.
953 * It also happens to be a multiple of 4Kb.
955 rdev
->sb_start
= calc_dev_sboffset(rdev
);
957 ret
= read_disk_sb(rdev
, MD_SB_BYTES
);
962 bdevname(rdev
->bdev
, b
);
963 sb
= page_address(rdev
->sb_page
);
965 if (sb
->md_magic
!= MD_SB_MAGIC
) {
966 printk(KERN_ERR
"md: invalid raid superblock magic on %s\n",
971 if (sb
->major_version
!= 0 ||
972 sb
->minor_version
< 90 ||
973 sb
->minor_version
> 91) {
974 printk(KERN_WARNING
"Bad version number %d.%d on %s\n",
975 sb
->major_version
, sb
->minor_version
,
980 if (sb
->raid_disks
<= 0)
983 if (md_csum_fold(calc_sb_csum(sb
)) != md_csum_fold(sb
->sb_csum
)) {
984 printk(KERN_WARNING
"md: invalid superblock checksum on %s\n",
989 rdev
->preferred_minor
= sb
->md_minor
;
990 rdev
->data_offset
= 0;
991 rdev
->new_data_offset
= 0;
992 rdev
->sb_size
= MD_SB_BYTES
;
993 rdev
->badblocks
.shift
= -1;
995 if (sb
->level
== LEVEL_MULTIPATH
)
998 rdev
->desc_nr
= sb
->this_disk
.number
;
1004 mdp_super_t
*refsb
= page_address(refdev
->sb_page
);
1005 if (!uuid_equal(refsb
, sb
)) {
1006 printk(KERN_WARNING
"md: %s has different UUID to %s\n",
1007 b
, bdevname(refdev
->bdev
,b2
));
1010 if (!sb_equal(refsb
, sb
)) {
1011 printk(KERN_WARNING
"md: %s has same UUID"
1012 " but different superblock to %s\n",
1013 b
, bdevname(refdev
->bdev
, b2
));
1017 ev2
= md_event(refsb
);
1023 rdev
->sectors
= rdev
->sb_start
;
1024 /* Limit to 4TB as metadata cannot record more than that.
1025 * (not needed for Linear and RAID0 as metadata doesn't
1028 if (rdev
->sectors
>= (2ULL << 32) && sb
->level
>= 1)
1029 rdev
->sectors
= (2ULL << 32) - 2;
1031 if (rdev
->sectors
< ((sector_t
)sb
->size
) * 2 && sb
->level
>= 1)
1032 /* "this cannot possibly happen" ... */
1040 * validate_super for 0.90.0
1042 static int super_90_validate(struct mddev
*mddev
, struct md_rdev
*rdev
)
1045 mdp_super_t
*sb
= page_address(rdev
->sb_page
);
1046 __u64 ev1
= md_event(sb
);
1048 rdev
->raid_disk
= -1;
1049 clear_bit(Faulty
, &rdev
->flags
);
1050 clear_bit(In_sync
, &rdev
->flags
);
1051 clear_bit(Bitmap_sync
, &rdev
->flags
);
1052 clear_bit(WriteMostly
, &rdev
->flags
);
1054 if (mddev
->raid_disks
== 0) {
1055 mddev
->major_version
= 0;
1056 mddev
->minor_version
= sb
->minor_version
;
1057 mddev
->patch_version
= sb
->patch_version
;
1058 mddev
->external
= 0;
1059 mddev
->chunk_sectors
= sb
->chunk_size
>> 9;
1060 mddev
->ctime
= sb
->ctime
;
1061 mddev
->utime
= sb
->utime
;
1062 mddev
->level
= sb
->level
;
1063 mddev
->clevel
[0] = 0;
1064 mddev
->layout
= sb
->layout
;
1065 mddev
->raid_disks
= sb
->raid_disks
;
1066 mddev
->dev_sectors
= ((sector_t
)sb
->size
) * 2;
1067 mddev
->events
= ev1
;
1068 mddev
->bitmap_info
.offset
= 0;
1069 mddev
->bitmap_info
.space
= 0;
1070 /* bitmap can use 60 K after the 4K superblocks */
1071 mddev
->bitmap_info
.default_offset
= MD_SB_BYTES
>> 9;
1072 mddev
->bitmap_info
.default_space
= 64*2 - (MD_SB_BYTES
>> 9);
1073 mddev
->reshape_backwards
= 0;
1075 if (mddev
->minor_version
>= 91) {
1076 mddev
->reshape_position
= sb
->reshape_position
;
1077 mddev
->delta_disks
= sb
->delta_disks
;
1078 mddev
->new_level
= sb
->new_level
;
1079 mddev
->new_layout
= sb
->new_layout
;
1080 mddev
->new_chunk_sectors
= sb
->new_chunk
>> 9;
1081 if (mddev
->delta_disks
< 0)
1082 mddev
->reshape_backwards
= 1;
1084 mddev
->reshape_position
= MaxSector
;
1085 mddev
->delta_disks
= 0;
1086 mddev
->new_level
= mddev
->level
;
1087 mddev
->new_layout
= mddev
->layout
;
1088 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
1091 if (sb
->state
& (1<<MD_SB_CLEAN
))
1092 mddev
->recovery_cp
= MaxSector
;
1094 if (sb
->events_hi
== sb
->cp_events_hi
&&
1095 sb
->events_lo
== sb
->cp_events_lo
) {
1096 mddev
->recovery_cp
= sb
->recovery_cp
;
1098 mddev
->recovery_cp
= 0;
1101 memcpy(mddev
->uuid
+0, &sb
->set_uuid0
, 4);
1102 memcpy(mddev
->uuid
+4, &sb
->set_uuid1
, 4);
1103 memcpy(mddev
->uuid
+8, &sb
->set_uuid2
, 4);
1104 memcpy(mddev
->uuid
+12,&sb
->set_uuid3
, 4);
1106 mddev
->max_disks
= MD_SB_DISKS
;
1108 if (sb
->state
& (1<<MD_SB_BITMAP_PRESENT
) &&
1109 mddev
->bitmap_info
.file
== NULL
) {
1110 mddev
->bitmap_info
.offset
=
1111 mddev
->bitmap_info
.default_offset
;
1112 mddev
->bitmap_info
.space
=
1113 mddev
->bitmap_info
.default_space
;
1116 } else if (mddev
->pers
== NULL
) {
1117 /* Insist on good event counter while assembling, except
1118 * for spares (which don't need an event count) */
1120 if (sb
->disks
[rdev
->desc_nr
].state
& (
1121 (1<<MD_DISK_SYNC
) | (1 << MD_DISK_ACTIVE
)))
1122 if (ev1
< mddev
->events
)
1124 } else if (mddev
->bitmap
) {
1125 /* if adding to array with a bitmap, then we can accept an
1126 * older device ... but not too old.
1128 if (ev1
< mddev
->bitmap
->events_cleared
)
1130 if (ev1
< mddev
->events
)
1131 set_bit(Bitmap_sync
, &rdev
->flags
);
1133 if (ev1
< mddev
->events
)
1134 /* just a hot-add of a new device, leave raid_disk at -1 */
1138 if (mddev
->level
!= LEVEL_MULTIPATH
) {
1139 desc
= sb
->disks
+ rdev
->desc_nr
;
1141 if (desc
->state
& (1<<MD_DISK_FAULTY
))
1142 set_bit(Faulty
, &rdev
->flags
);
1143 else if (desc
->state
& (1<<MD_DISK_SYNC
) /* &&
1144 desc->raid_disk < mddev->raid_disks */) {
1145 set_bit(In_sync
, &rdev
->flags
);
1146 rdev
->raid_disk
= desc
->raid_disk
;
1147 rdev
->saved_raid_disk
= desc
->raid_disk
;
1148 } else if (desc
->state
& (1<<MD_DISK_ACTIVE
)) {
1149 /* active but not in sync implies recovery up to
1150 * reshape position. We don't know exactly where
1151 * that is, so set to zero for now */
1152 if (mddev
->minor_version
>= 91) {
1153 rdev
->recovery_offset
= 0;
1154 rdev
->raid_disk
= desc
->raid_disk
;
1157 if (desc
->state
& (1<<MD_DISK_WRITEMOSTLY
))
1158 set_bit(WriteMostly
, &rdev
->flags
);
1159 } else /* MULTIPATH are always insync */
1160 set_bit(In_sync
, &rdev
->flags
);
1165 * sync_super for 0.90.0
1167 static void super_90_sync(struct mddev
*mddev
, struct md_rdev
*rdev
)
1170 struct md_rdev
*rdev2
;
1171 int next_spare
= mddev
->raid_disks
;
1173 /* make rdev->sb match mddev data..
1176 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1177 * 3/ any empty disks < next_spare become removed
1179 * disks[0] gets initialised to REMOVED because
1180 * we cannot be sure from other fields if it has
1181 * been initialised or not.
1184 int active
=0, working
=0,failed
=0,spare
=0,nr_disks
=0;
1186 rdev
->sb_size
= MD_SB_BYTES
;
1188 sb
= page_address(rdev
->sb_page
);
1190 memset(sb
, 0, sizeof(*sb
));
1192 sb
->md_magic
= MD_SB_MAGIC
;
1193 sb
->major_version
= mddev
->major_version
;
1194 sb
->patch_version
= mddev
->patch_version
;
1195 sb
->gvalid_words
= 0; /* ignored */
1196 memcpy(&sb
->set_uuid0
, mddev
->uuid
+0, 4);
1197 memcpy(&sb
->set_uuid1
, mddev
->uuid
+4, 4);
1198 memcpy(&sb
->set_uuid2
, mddev
->uuid
+8, 4);
1199 memcpy(&sb
->set_uuid3
, mddev
->uuid
+12,4);
1201 sb
->ctime
= mddev
->ctime
;
1202 sb
->level
= mddev
->level
;
1203 sb
->size
= mddev
->dev_sectors
/ 2;
1204 sb
->raid_disks
= mddev
->raid_disks
;
1205 sb
->md_minor
= mddev
->md_minor
;
1206 sb
->not_persistent
= 0;
1207 sb
->utime
= mddev
->utime
;
1209 sb
->events_hi
= (mddev
->events
>>32);
1210 sb
->events_lo
= (u32
)mddev
->events
;
1212 if (mddev
->reshape_position
== MaxSector
)
1213 sb
->minor_version
= 90;
1215 sb
->minor_version
= 91;
1216 sb
->reshape_position
= mddev
->reshape_position
;
1217 sb
->new_level
= mddev
->new_level
;
1218 sb
->delta_disks
= mddev
->delta_disks
;
1219 sb
->new_layout
= mddev
->new_layout
;
1220 sb
->new_chunk
= mddev
->new_chunk_sectors
<< 9;
1222 mddev
->minor_version
= sb
->minor_version
;
1225 sb
->recovery_cp
= mddev
->recovery_cp
;
1226 sb
->cp_events_hi
= (mddev
->events
>>32);
1227 sb
->cp_events_lo
= (u32
)mddev
->events
;
1228 if (mddev
->recovery_cp
== MaxSector
)
1229 sb
->state
= (1<< MD_SB_CLEAN
);
1231 sb
->recovery_cp
= 0;
1233 sb
->layout
= mddev
->layout
;
1234 sb
->chunk_size
= mddev
->chunk_sectors
<< 9;
1236 if (mddev
->bitmap
&& mddev
->bitmap_info
.file
== NULL
)
1237 sb
->state
|= (1<<MD_SB_BITMAP_PRESENT
);
1239 sb
->disks
[0].state
= (1<<MD_DISK_REMOVED
);
1240 rdev_for_each(rdev2
, mddev
) {
1243 int is_active
= test_bit(In_sync
, &rdev2
->flags
);
1245 if (rdev2
->raid_disk
>= 0 &&
1246 sb
->minor_version
>= 91)
1247 /* we have nowhere to store the recovery_offset,
1248 * but if it is not below the reshape_position,
1249 * we can piggy-back on that.
1252 if (rdev2
->raid_disk
< 0 ||
1253 test_bit(Faulty
, &rdev2
->flags
))
1256 desc_nr
= rdev2
->raid_disk
;
1258 desc_nr
= next_spare
++;
1259 rdev2
->desc_nr
= desc_nr
;
1260 d
= &sb
->disks
[rdev2
->desc_nr
];
1262 d
->number
= rdev2
->desc_nr
;
1263 d
->major
= MAJOR(rdev2
->bdev
->bd_dev
);
1264 d
->minor
= MINOR(rdev2
->bdev
->bd_dev
);
1266 d
->raid_disk
= rdev2
->raid_disk
;
1268 d
->raid_disk
= rdev2
->desc_nr
; /* compatibility */
1269 if (test_bit(Faulty
, &rdev2
->flags
))
1270 d
->state
= (1<<MD_DISK_FAULTY
);
1271 else if (is_active
) {
1272 d
->state
= (1<<MD_DISK_ACTIVE
);
1273 if (test_bit(In_sync
, &rdev2
->flags
))
1274 d
->state
|= (1<<MD_DISK_SYNC
);
1282 if (test_bit(WriteMostly
, &rdev2
->flags
))
1283 d
->state
|= (1<<MD_DISK_WRITEMOSTLY
);
1285 /* now set the "removed" and "faulty" bits on any missing devices */
1286 for (i
=0 ; i
< mddev
->raid_disks
; i
++) {
1287 mdp_disk_t
*d
= &sb
->disks
[i
];
1288 if (d
->state
== 0 && d
->number
== 0) {
1291 d
->state
= (1<<MD_DISK_REMOVED
);
1292 d
->state
|= (1<<MD_DISK_FAULTY
);
1296 sb
->nr_disks
= nr_disks
;
1297 sb
->active_disks
= active
;
1298 sb
->working_disks
= working
;
1299 sb
->failed_disks
= failed
;
1300 sb
->spare_disks
= spare
;
1302 sb
->this_disk
= sb
->disks
[rdev
->desc_nr
];
1303 sb
->sb_csum
= calc_sb_csum(sb
);
1307 * rdev_size_change for 0.90.0
1309 static unsigned long long
1310 super_90_rdev_size_change(struct md_rdev
*rdev
, sector_t num_sectors
)
1312 if (num_sectors
&& num_sectors
< rdev
->mddev
->dev_sectors
)
1313 return 0; /* component must fit device */
1314 if (rdev
->mddev
->bitmap_info
.offset
)
1315 return 0; /* can't move bitmap */
1316 rdev
->sb_start
= calc_dev_sboffset(rdev
);
1317 if (!num_sectors
|| num_sectors
> rdev
->sb_start
)
1318 num_sectors
= rdev
->sb_start
;
1319 /* Limit to 4TB as metadata cannot record more than that.
1320 * 4TB == 2^32 KB, or 2*2^32 sectors.
1322 if (num_sectors
>= (2ULL << 32) && rdev
->mddev
->level
>= 1)
1323 num_sectors
= (2ULL << 32) - 2;
1324 md_super_write(rdev
->mddev
, rdev
, rdev
->sb_start
, rdev
->sb_size
,
1326 md_super_wait(rdev
->mddev
);
1331 super_90_allow_new_offset(struct md_rdev
*rdev
, unsigned long long new_offset
)
1333 /* non-zero offset changes not possible with v0.90 */
1334 return new_offset
== 0;
1338 * version 1 superblock
1341 static __le32
calc_sb_1_csum(struct mdp_superblock_1
*sb
)
1345 unsigned long long newcsum
;
1346 int size
= 256 + le32_to_cpu(sb
->max_dev
)*2;
1347 __le32
*isuper
= (__le32
*)sb
;
1349 disk_csum
= sb
->sb_csum
;
1352 for (; size
>= 4; size
-= 4)
1353 newcsum
+= le32_to_cpu(*isuper
++);
1356 newcsum
+= le16_to_cpu(*(__le16
*) isuper
);
1358 csum
= (newcsum
& 0xffffffff) + (newcsum
>> 32);
1359 sb
->sb_csum
= disk_csum
;
1360 return cpu_to_le32(csum
);
1363 static int md_set_badblocks(struct badblocks
*bb
, sector_t s
, int sectors
,
1365 static int super_1_load(struct md_rdev
*rdev
, struct md_rdev
*refdev
, int minor_version
)
1367 struct mdp_superblock_1
*sb
;
1371 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
1375 * Calculate the position of the superblock in 512byte sectors.
1376 * It is always aligned to a 4K boundary and
1377 * depeding on minor_version, it can be:
1378 * 0: At least 8K, but less than 12K, from end of device
1379 * 1: At start of device
1380 * 2: 4K from start of device.
1382 switch(minor_version
) {
1384 sb_start
= i_size_read(rdev
->bdev
->bd_inode
) >> 9;
1386 sb_start
&= ~(sector_t
)(4*2-1);
1397 rdev
->sb_start
= sb_start
;
1399 /* superblock is rarely larger than 1K, but it can be larger,
1400 * and it is safe to read 4k, so we do that
1402 ret
= read_disk_sb(rdev
, 4096);
1403 if (ret
) return ret
;
1405 sb
= page_address(rdev
->sb_page
);
1407 if (sb
->magic
!= cpu_to_le32(MD_SB_MAGIC
) ||
1408 sb
->major_version
!= cpu_to_le32(1) ||
1409 le32_to_cpu(sb
->max_dev
) > (4096-256)/2 ||
1410 le64_to_cpu(sb
->super_offset
) != rdev
->sb_start
||
1411 (le32_to_cpu(sb
->feature_map
) & ~MD_FEATURE_ALL
) != 0)
1414 if (calc_sb_1_csum(sb
) != sb
->sb_csum
) {
1415 printk("md: invalid superblock checksum on %s\n",
1416 bdevname(rdev
->bdev
,b
));
1419 if (le64_to_cpu(sb
->data_size
) < 10) {
1420 printk("md: data_size too small on %s\n",
1421 bdevname(rdev
->bdev
,b
));
1426 memcmp(sb
->pad3
, sb
->pad3
+1, sizeof(sb
->pad3
) - sizeof(sb
->pad3
[1])))
1427 /* Some padding is non-zero, might be a new feature */
1430 rdev
->preferred_minor
= 0xffff;
1431 rdev
->data_offset
= le64_to_cpu(sb
->data_offset
);
1432 rdev
->new_data_offset
= rdev
->data_offset
;
1433 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RESHAPE_ACTIVE
) &&
1434 (le32_to_cpu(sb
->feature_map
) & MD_FEATURE_NEW_OFFSET
))
1435 rdev
->new_data_offset
+= (s32
)le32_to_cpu(sb
->new_offset
);
1436 atomic_set(&rdev
->corrected_errors
, le32_to_cpu(sb
->cnt_corrected_read
));
1438 rdev
->sb_size
= le32_to_cpu(sb
->max_dev
) * 2 + 256;
1439 bmask
= queue_logical_block_size(rdev
->bdev
->bd_disk
->queue
)-1;
1440 if (rdev
->sb_size
& bmask
)
1441 rdev
->sb_size
= (rdev
->sb_size
| bmask
) + 1;
1444 && rdev
->data_offset
< sb_start
+ (rdev
->sb_size
/512))
1447 && rdev
->new_data_offset
< sb_start
+ (rdev
->sb_size
/512))
1450 if (sb
->level
== cpu_to_le32(LEVEL_MULTIPATH
))
1453 rdev
->desc_nr
= le32_to_cpu(sb
->dev_number
);
1455 if (!rdev
->bb_page
) {
1456 rdev
->bb_page
= alloc_page(GFP_KERNEL
);
1460 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_BAD_BLOCKS
) &&
1461 rdev
->badblocks
.count
== 0) {
1462 /* need to load the bad block list.
1463 * Currently we limit it to one page.
1469 int sectors
= le16_to_cpu(sb
->bblog_size
);
1470 if (sectors
> (PAGE_SIZE
/ 512))
1472 offset
= le32_to_cpu(sb
->bblog_offset
);
1475 bb_sector
= (long long)offset
;
1476 if (!sync_page_io(rdev
, bb_sector
, sectors
<< 9,
1477 rdev
->bb_page
, READ
, true))
1479 bbp
= (u64
*)page_address(rdev
->bb_page
);
1480 rdev
->badblocks
.shift
= sb
->bblog_shift
;
1481 for (i
= 0 ; i
< (sectors
<< (9-3)) ; i
++, bbp
++) {
1482 u64 bb
= le64_to_cpu(*bbp
);
1483 int count
= bb
& (0x3ff);
1484 u64 sector
= bb
>> 10;
1485 sector
<<= sb
->bblog_shift
;
1486 count
<<= sb
->bblog_shift
;
1489 if (md_set_badblocks(&rdev
->badblocks
,
1490 sector
, count
, 1) == 0)
1493 } else if (sb
->bblog_offset
!= 0)
1494 rdev
->badblocks
.shift
= 0;
1500 struct mdp_superblock_1
*refsb
= page_address(refdev
->sb_page
);
1502 if (memcmp(sb
->set_uuid
, refsb
->set_uuid
, 16) != 0 ||
1503 sb
->level
!= refsb
->level
||
1504 sb
->layout
!= refsb
->layout
||
1505 sb
->chunksize
!= refsb
->chunksize
) {
1506 printk(KERN_WARNING
"md: %s has strangely different"
1507 " superblock to %s\n",
1508 bdevname(rdev
->bdev
,b
),
1509 bdevname(refdev
->bdev
,b2
));
1512 ev1
= le64_to_cpu(sb
->events
);
1513 ev2
= le64_to_cpu(refsb
->events
);
1520 if (minor_version
) {
1521 sectors
= (i_size_read(rdev
->bdev
->bd_inode
) >> 9);
1522 sectors
-= rdev
->data_offset
;
1524 sectors
= rdev
->sb_start
;
1525 if (sectors
< le64_to_cpu(sb
->data_size
))
1527 rdev
->sectors
= le64_to_cpu(sb
->data_size
);
1531 static int super_1_validate(struct mddev
*mddev
, struct md_rdev
*rdev
)
1533 struct mdp_superblock_1
*sb
= page_address(rdev
->sb_page
);
1534 __u64 ev1
= le64_to_cpu(sb
->events
);
1536 rdev
->raid_disk
= -1;
1537 clear_bit(Faulty
, &rdev
->flags
);
1538 clear_bit(In_sync
, &rdev
->flags
);
1539 clear_bit(Bitmap_sync
, &rdev
->flags
);
1540 clear_bit(WriteMostly
, &rdev
->flags
);
1542 if (mddev
->raid_disks
== 0) {
1543 mddev
->major_version
= 1;
1544 mddev
->patch_version
= 0;
1545 mddev
->external
= 0;
1546 mddev
->chunk_sectors
= le32_to_cpu(sb
->chunksize
);
1547 mddev
->ctime
= le64_to_cpu(sb
->ctime
) & ((1ULL << 32)-1);
1548 mddev
->utime
= le64_to_cpu(sb
->utime
) & ((1ULL << 32)-1);
1549 mddev
->level
= le32_to_cpu(sb
->level
);
1550 mddev
->clevel
[0] = 0;
1551 mddev
->layout
= le32_to_cpu(sb
->layout
);
1552 mddev
->raid_disks
= le32_to_cpu(sb
->raid_disks
);
1553 mddev
->dev_sectors
= le64_to_cpu(sb
->size
);
1554 mddev
->events
= ev1
;
1555 mddev
->bitmap_info
.offset
= 0;
1556 mddev
->bitmap_info
.space
= 0;
1557 /* Default location for bitmap is 1K after superblock
1558 * using 3K - total of 4K
1560 mddev
->bitmap_info
.default_offset
= 1024 >> 9;
1561 mddev
->bitmap_info
.default_space
= (4096-1024) >> 9;
1562 mddev
->reshape_backwards
= 0;
1564 mddev
->recovery_cp
= le64_to_cpu(sb
->resync_offset
);
1565 memcpy(mddev
->uuid
, sb
->set_uuid
, 16);
1567 mddev
->max_disks
= (4096-256)/2;
1569 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_BITMAP_OFFSET
) &&
1570 mddev
->bitmap_info
.file
== NULL
) {
1571 mddev
->bitmap_info
.offset
=
1572 (__s32
)le32_to_cpu(sb
->bitmap_offset
);
1573 /* Metadata doesn't record how much space is available.
1574 * For 1.0, we assume we can use up to the superblock
1575 * if before, else to 4K beyond superblock.
1576 * For others, assume no change is possible.
1578 if (mddev
->minor_version
> 0)
1579 mddev
->bitmap_info
.space
= 0;
1580 else if (mddev
->bitmap_info
.offset
> 0)
1581 mddev
->bitmap_info
.space
=
1582 8 - mddev
->bitmap_info
.offset
;
1584 mddev
->bitmap_info
.space
=
1585 -mddev
->bitmap_info
.offset
;
1588 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RESHAPE_ACTIVE
)) {
1589 mddev
->reshape_position
= le64_to_cpu(sb
->reshape_position
);
1590 mddev
->delta_disks
= le32_to_cpu(sb
->delta_disks
);
1591 mddev
->new_level
= le32_to_cpu(sb
->new_level
);
1592 mddev
->new_layout
= le32_to_cpu(sb
->new_layout
);
1593 mddev
->new_chunk_sectors
= le32_to_cpu(sb
->new_chunk
);
1594 if (mddev
->delta_disks
< 0 ||
1595 (mddev
->delta_disks
== 0 &&
1596 (le32_to_cpu(sb
->feature_map
)
1597 & MD_FEATURE_RESHAPE_BACKWARDS
)))
1598 mddev
->reshape_backwards
= 1;
1600 mddev
->reshape_position
= MaxSector
;
1601 mddev
->delta_disks
= 0;
1602 mddev
->new_level
= mddev
->level
;
1603 mddev
->new_layout
= mddev
->layout
;
1604 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
1607 } else if (mddev
->pers
== NULL
) {
1608 /* Insist of good event counter while assembling, except for
1609 * spares (which don't need an event count) */
1611 if (rdev
->desc_nr
>= 0 &&
1612 rdev
->desc_nr
< le32_to_cpu(sb
->max_dev
) &&
1613 (le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]) < MD_DISK_ROLE_MAX
||
1614 le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]) == MD_DISK_ROLE_JOURNAL
))
1615 if (ev1
< mddev
->events
)
1617 } else if (mddev
->bitmap
) {
1618 /* If adding to array with a bitmap, then we can accept an
1619 * older device, but not too old.
1621 if (ev1
< mddev
->bitmap
->events_cleared
)
1623 if (ev1
< mddev
->events
)
1624 set_bit(Bitmap_sync
, &rdev
->flags
);
1626 if (ev1
< mddev
->events
)
1627 /* just a hot-add of a new device, leave raid_disk at -1 */
1630 if (mddev
->level
!= LEVEL_MULTIPATH
) {
1632 if (rdev
->desc_nr
< 0 ||
1633 rdev
->desc_nr
>= le32_to_cpu(sb
->max_dev
)) {
1634 role
= MD_DISK_ROLE_SPARE
;
1637 role
= le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]);
1639 case MD_DISK_ROLE_SPARE
: /* spare */
1641 case MD_DISK_ROLE_FAULTY
: /* faulty */
1642 set_bit(Faulty
, &rdev
->flags
);
1644 case MD_DISK_ROLE_JOURNAL
: /* journal device */
1645 if (!(le32_to_cpu(sb
->feature_map
) & MD_FEATURE_JOURNAL
)) {
1646 /* journal device without journal feature */
1648 "md: journal device provided without journal feature, ignoring the device\n");
1651 set_bit(Journal
, &rdev
->flags
);
1652 rdev
->journal_tail
= le64_to_cpu(sb
->journal_tail
);
1653 if (mddev
->recovery_cp
== MaxSector
)
1654 set_bit(MD_JOURNAL_CLEAN
, &mddev
->flags
);
1655 rdev
->raid_disk
= 0;
1658 rdev
->saved_raid_disk
= role
;
1659 if ((le32_to_cpu(sb
->feature_map
) &
1660 MD_FEATURE_RECOVERY_OFFSET
)) {
1661 rdev
->recovery_offset
= le64_to_cpu(sb
->recovery_offset
);
1662 if (!(le32_to_cpu(sb
->feature_map
) &
1663 MD_FEATURE_RECOVERY_BITMAP
))
1664 rdev
->saved_raid_disk
= -1;
1666 set_bit(In_sync
, &rdev
->flags
);
1667 rdev
->raid_disk
= role
;
1670 if (sb
->devflags
& WriteMostly1
)
1671 set_bit(WriteMostly
, &rdev
->flags
);
1672 if (le32_to_cpu(sb
->feature_map
) & MD_FEATURE_REPLACEMENT
)
1673 set_bit(Replacement
, &rdev
->flags
);
1674 if (le32_to_cpu(sb
->feature_map
) & MD_FEATURE_JOURNAL
)
1675 set_bit(MD_HAS_JOURNAL
, &mddev
->flags
);
1676 } else /* MULTIPATH are always insync */
1677 set_bit(In_sync
, &rdev
->flags
);
1682 static void super_1_sync(struct mddev
*mddev
, struct md_rdev
*rdev
)
1684 struct mdp_superblock_1
*sb
;
1685 struct md_rdev
*rdev2
;
1687 /* make rdev->sb match mddev and rdev data. */
1689 sb
= page_address(rdev
->sb_page
);
1691 sb
->feature_map
= 0;
1693 sb
->recovery_offset
= cpu_to_le64(0);
1694 memset(sb
->pad3
, 0, sizeof(sb
->pad3
));
1696 sb
->utime
= cpu_to_le64((__u64
)mddev
->utime
);
1697 sb
->events
= cpu_to_le64(mddev
->events
);
1699 sb
->resync_offset
= cpu_to_le64(mddev
->recovery_cp
);
1700 else if (test_bit(MD_JOURNAL_CLEAN
, &mddev
->flags
))
1701 sb
->resync_offset
= cpu_to_le64(MaxSector
);
1703 sb
->resync_offset
= cpu_to_le64(0);
1705 sb
->cnt_corrected_read
= cpu_to_le32(atomic_read(&rdev
->corrected_errors
));
1707 sb
->raid_disks
= cpu_to_le32(mddev
->raid_disks
);
1708 sb
->size
= cpu_to_le64(mddev
->dev_sectors
);
1709 sb
->chunksize
= cpu_to_le32(mddev
->chunk_sectors
);
1710 sb
->level
= cpu_to_le32(mddev
->level
);
1711 sb
->layout
= cpu_to_le32(mddev
->layout
);
1713 if (test_bit(WriteMostly
, &rdev
->flags
))
1714 sb
->devflags
|= WriteMostly1
;
1716 sb
->devflags
&= ~WriteMostly1
;
1717 sb
->data_offset
= cpu_to_le64(rdev
->data_offset
);
1718 sb
->data_size
= cpu_to_le64(rdev
->sectors
);
1720 if (mddev
->bitmap
&& mddev
->bitmap_info
.file
== NULL
) {
1721 sb
->bitmap_offset
= cpu_to_le32((__u32
)mddev
->bitmap_info
.offset
);
1722 sb
->feature_map
= cpu_to_le32(MD_FEATURE_BITMAP_OFFSET
);
1725 if (rdev
->raid_disk
>= 0 && !test_bit(Journal
, &rdev
->flags
) &&
1726 !test_bit(In_sync
, &rdev
->flags
)) {
1728 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET
);
1729 sb
->recovery_offset
=
1730 cpu_to_le64(rdev
->recovery_offset
);
1731 if (rdev
->saved_raid_disk
>= 0 && mddev
->bitmap
)
1733 cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP
);
1735 /* Note: recovery_offset and journal_tail share space */
1736 if (test_bit(Journal
, &rdev
->flags
))
1737 sb
->journal_tail
= cpu_to_le64(rdev
->journal_tail
);
1738 if (test_bit(Replacement
, &rdev
->flags
))
1740 cpu_to_le32(MD_FEATURE_REPLACEMENT
);
1742 if (mddev
->reshape_position
!= MaxSector
) {
1743 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE
);
1744 sb
->reshape_position
= cpu_to_le64(mddev
->reshape_position
);
1745 sb
->new_layout
= cpu_to_le32(mddev
->new_layout
);
1746 sb
->delta_disks
= cpu_to_le32(mddev
->delta_disks
);
1747 sb
->new_level
= cpu_to_le32(mddev
->new_level
);
1748 sb
->new_chunk
= cpu_to_le32(mddev
->new_chunk_sectors
);
1749 if (mddev
->delta_disks
== 0 &&
1750 mddev
->reshape_backwards
)
1752 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS
);
1753 if (rdev
->new_data_offset
!= rdev
->data_offset
) {
1755 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET
);
1756 sb
->new_offset
= cpu_to_le32((__u32
)(rdev
->new_data_offset
1757 - rdev
->data_offset
));
1761 if (mddev_is_clustered(mddev
))
1762 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_CLUSTERED
);
1764 if (rdev
->badblocks
.count
== 0)
1765 /* Nothing to do for bad blocks*/ ;
1766 else if (sb
->bblog_offset
== 0)
1767 /* Cannot record bad blocks on this device */
1768 md_error(mddev
, rdev
);
1770 struct badblocks
*bb
= &rdev
->badblocks
;
1771 u64
*bbp
= (u64
*)page_address(rdev
->bb_page
);
1773 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_BAD_BLOCKS
);
1778 seq
= read_seqbegin(&bb
->lock
);
1780 memset(bbp
, 0xff, PAGE_SIZE
);
1782 for (i
= 0 ; i
< bb
->count
; i
++) {
1783 u64 internal_bb
= p
[i
];
1784 u64 store_bb
= ((BB_OFFSET(internal_bb
) << 10)
1785 | BB_LEN(internal_bb
));
1786 bbp
[i
] = cpu_to_le64(store_bb
);
1789 if (read_seqretry(&bb
->lock
, seq
))
1792 bb
->sector
= (rdev
->sb_start
+
1793 (int)le32_to_cpu(sb
->bblog_offset
));
1794 bb
->size
= le16_to_cpu(sb
->bblog_size
);
1799 rdev_for_each(rdev2
, mddev
)
1800 if (rdev2
->desc_nr
+1 > max_dev
)
1801 max_dev
= rdev2
->desc_nr
+1;
1803 if (max_dev
> le32_to_cpu(sb
->max_dev
)) {
1805 sb
->max_dev
= cpu_to_le32(max_dev
);
1806 rdev
->sb_size
= max_dev
* 2 + 256;
1807 bmask
= queue_logical_block_size(rdev
->bdev
->bd_disk
->queue
)-1;
1808 if (rdev
->sb_size
& bmask
)
1809 rdev
->sb_size
= (rdev
->sb_size
| bmask
) + 1;
1811 max_dev
= le32_to_cpu(sb
->max_dev
);
1813 for (i
=0; i
<max_dev
;i
++)
1814 sb
->dev_roles
[i
] = cpu_to_le16(MD_DISK_ROLE_FAULTY
);
1816 if (test_bit(MD_HAS_JOURNAL
, &mddev
->flags
))
1817 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_JOURNAL
);
1819 rdev_for_each(rdev2
, mddev
) {
1821 if (test_bit(Faulty
, &rdev2
->flags
))
1822 sb
->dev_roles
[i
] = cpu_to_le16(MD_DISK_ROLE_FAULTY
);
1823 else if (test_bit(In_sync
, &rdev2
->flags
))
1824 sb
->dev_roles
[i
] = cpu_to_le16(rdev2
->raid_disk
);
1825 else if (test_bit(Journal
, &rdev2
->flags
))
1826 sb
->dev_roles
[i
] = cpu_to_le16(MD_DISK_ROLE_JOURNAL
);
1827 else if (rdev2
->raid_disk
>= 0)
1828 sb
->dev_roles
[i
] = cpu_to_le16(rdev2
->raid_disk
);
1830 sb
->dev_roles
[i
] = cpu_to_le16(MD_DISK_ROLE_SPARE
);
1833 sb
->sb_csum
= calc_sb_1_csum(sb
);
1836 static unsigned long long
1837 super_1_rdev_size_change(struct md_rdev
*rdev
, sector_t num_sectors
)
1839 struct mdp_superblock_1
*sb
;
1840 sector_t max_sectors
;
1841 if (num_sectors
&& num_sectors
< rdev
->mddev
->dev_sectors
)
1842 return 0; /* component must fit device */
1843 if (rdev
->data_offset
!= rdev
->new_data_offset
)
1844 return 0; /* too confusing */
1845 if (rdev
->sb_start
< rdev
->data_offset
) {
1846 /* minor versions 1 and 2; superblock before data */
1847 max_sectors
= i_size_read(rdev
->bdev
->bd_inode
) >> 9;
1848 max_sectors
-= rdev
->data_offset
;
1849 if (!num_sectors
|| num_sectors
> max_sectors
)
1850 num_sectors
= max_sectors
;
1851 } else if (rdev
->mddev
->bitmap_info
.offset
) {
1852 /* minor version 0 with bitmap we can't move */
1855 /* minor version 0; superblock after data */
1857 sb_start
= (i_size_read(rdev
->bdev
->bd_inode
) >> 9) - 8*2;
1858 sb_start
&= ~(sector_t
)(4*2 - 1);
1859 max_sectors
= rdev
->sectors
+ sb_start
- rdev
->sb_start
;
1860 if (!num_sectors
|| num_sectors
> max_sectors
)
1861 num_sectors
= max_sectors
;
1862 rdev
->sb_start
= sb_start
;
1864 sb
= page_address(rdev
->sb_page
);
1865 sb
->data_size
= cpu_to_le64(num_sectors
);
1866 sb
->super_offset
= rdev
->sb_start
;
1867 sb
->sb_csum
= calc_sb_1_csum(sb
);
1868 md_super_write(rdev
->mddev
, rdev
, rdev
->sb_start
, rdev
->sb_size
,
1870 md_super_wait(rdev
->mddev
);
1876 super_1_allow_new_offset(struct md_rdev
*rdev
,
1877 unsigned long long new_offset
)
1879 /* All necessary checks on new >= old have been done */
1880 struct bitmap
*bitmap
;
1881 if (new_offset
>= rdev
->data_offset
)
1884 /* with 1.0 metadata, there is no metadata to tread on
1885 * so we can always move back */
1886 if (rdev
->mddev
->minor_version
== 0)
1889 /* otherwise we must be sure not to step on
1890 * any metadata, so stay:
1891 * 36K beyond start of superblock
1892 * beyond end of badblocks
1893 * beyond write-intent bitmap
1895 if (rdev
->sb_start
+ (32+4)*2 > new_offset
)
1897 bitmap
= rdev
->mddev
->bitmap
;
1898 if (bitmap
&& !rdev
->mddev
->bitmap_info
.file
&&
1899 rdev
->sb_start
+ rdev
->mddev
->bitmap_info
.offset
+
1900 bitmap
->storage
.file_pages
* (PAGE_SIZE
>>9) > new_offset
)
1902 if (rdev
->badblocks
.sector
+ rdev
->badblocks
.size
> new_offset
)
1908 static struct super_type super_types
[] = {
1911 .owner
= THIS_MODULE
,
1912 .load_super
= super_90_load
,
1913 .validate_super
= super_90_validate
,
1914 .sync_super
= super_90_sync
,
1915 .rdev_size_change
= super_90_rdev_size_change
,
1916 .allow_new_offset
= super_90_allow_new_offset
,
1920 .owner
= THIS_MODULE
,
1921 .load_super
= super_1_load
,
1922 .validate_super
= super_1_validate
,
1923 .sync_super
= super_1_sync
,
1924 .rdev_size_change
= super_1_rdev_size_change
,
1925 .allow_new_offset
= super_1_allow_new_offset
,
1929 static void sync_super(struct mddev
*mddev
, struct md_rdev
*rdev
)
1931 if (mddev
->sync_super
) {
1932 mddev
->sync_super(mddev
, rdev
);
1936 BUG_ON(mddev
->major_version
>= ARRAY_SIZE(super_types
));
1938 super_types
[mddev
->major_version
].sync_super(mddev
, rdev
);
1941 static int match_mddev_units(struct mddev
*mddev1
, struct mddev
*mddev2
)
1943 struct md_rdev
*rdev
, *rdev2
;
1946 rdev_for_each_rcu(rdev
, mddev1
) {
1947 if (test_bit(Faulty
, &rdev
->flags
) ||
1948 test_bit(Journal
, &rdev
->flags
) ||
1949 rdev
->raid_disk
== -1)
1951 rdev_for_each_rcu(rdev2
, mddev2
) {
1952 if (test_bit(Faulty
, &rdev2
->flags
) ||
1953 test_bit(Journal
, &rdev2
->flags
) ||
1954 rdev2
->raid_disk
== -1)
1956 if (rdev
->bdev
->bd_contains
==
1957 rdev2
->bdev
->bd_contains
) {
1967 static LIST_HEAD(pending_raid_disks
);
1970 * Try to register data integrity profile for an mddev
1972 * This is called when an array is started and after a disk has been kicked
1973 * from the array. It only succeeds if all working and active component devices
1974 * are integrity capable with matching profiles.
1976 int md_integrity_register(struct mddev
*mddev
)
1978 struct md_rdev
*rdev
, *reference
= NULL
;
1980 if (list_empty(&mddev
->disks
))
1981 return 0; /* nothing to do */
1982 if (!mddev
->gendisk
|| blk_get_integrity(mddev
->gendisk
))
1983 return 0; /* shouldn't register, or already is */
1984 rdev_for_each(rdev
, mddev
) {
1985 /* skip spares and non-functional disks */
1986 if (test_bit(Faulty
, &rdev
->flags
))
1988 if (rdev
->raid_disk
< 0)
1991 /* Use the first rdev as the reference */
1995 /* does this rdev's profile match the reference profile? */
1996 if (blk_integrity_compare(reference
->bdev
->bd_disk
,
1997 rdev
->bdev
->bd_disk
) < 0)
2000 if (!reference
|| !bdev_get_integrity(reference
->bdev
))
2003 * All component devices are integrity capable and have matching
2004 * profiles, register the common profile for the md device.
2006 blk_integrity_register(mddev
->gendisk
,
2007 bdev_get_integrity(reference
->bdev
));
2009 printk(KERN_NOTICE
"md: data integrity enabled on %s\n", mdname(mddev
));
2010 if (bioset_integrity_create(mddev
->bio_set
, BIO_POOL_SIZE
)) {
2011 printk(KERN_ERR
"md: failed to create integrity pool for %s\n",
2017 EXPORT_SYMBOL(md_integrity_register
);
2019 /* Disable data integrity if non-capable/non-matching disk is being added */
2020 void md_integrity_add_rdev(struct md_rdev
*rdev
, struct mddev
*mddev
)
2022 struct blk_integrity
*bi_rdev
;
2023 struct blk_integrity
*bi_mddev
;
2025 if (!mddev
->gendisk
)
2028 bi_rdev
= bdev_get_integrity(rdev
->bdev
);
2029 bi_mddev
= blk_get_integrity(mddev
->gendisk
);
2031 if (!bi_mddev
) /* nothing to do */
2033 if (rdev
->raid_disk
< 0) /* skip spares */
2035 if (bi_rdev
&& blk_integrity_compare(mddev
->gendisk
,
2036 rdev
->bdev
->bd_disk
) >= 0)
2038 WARN_ON_ONCE(!mddev
->suspended
);
2039 printk(KERN_NOTICE
"disabling data integrity on %s\n", mdname(mddev
));
2040 blk_integrity_unregister(mddev
->gendisk
);
2042 EXPORT_SYMBOL(md_integrity_add_rdev
);
2044 static int bind_rdev_to_array(struct md_rdev
*rdev
, struct mddev
*mddev
)
2046 char b
[BDEVNAME_SIZE
];
2050 /* prevent duplicates */
2051 if (find_rdev(mddev
, rdev
->bdev
->bd_dev
))
2054 /* make sure rdev->sectors exceeds mddev->dev_sectors */
2055 if (rdev
->sectors
&& (mddev
->dev_sectors
== 0 ||
2056 rdev
->sectors
< mddev
->dev_sectors
)) {
2058 /* Cannot change size, so fail
2059 * If mddev->level <= 0, then we don't care
2060 * about aligning sizes (e.g. linear)
2062 if (mddev
->level
> 0)
2065 mddev
->dev_sectors
= rdev
->sectors
;
2068 /* Verify rdev->desc_nr is unique.
2069 * If it is -1, assign a free number, else
2070 * check number is not in use
2073 if (rdev
->desc_nr
< 0) {
2076 choice
= mddev
->raid_disks
;
2077 while (md_find_rdev_nr_rcu(mddev
, choice
))
2079 rdev
->desc_nr
= choice
;
2081 if (md_find_rdev_nr_rcu(mddev
, rdev
->desc_nr
)) {
2087 if (mddev
->max_disks
&& rdev
->desc_nr
>= mddev
->max_disks
) {
2088 printk(KERN_WARNING
"md: %s: array is limited to %d devices\n",
2089 mdname(mddev
), mddev
->max_disks
);
2092 bdevname(rdev
->bdev
,b
);
2093 strreplace(b
, '/', '!');
2095 rdev
->mddev
= mddev
;
2096 printk(KERN_INFO
"md: bind<%s>\n", b
);
2098 if ((err
= kobject_add(&rdev
->kobj
, &mddev
->kobj
, "dev-%s", b
)))
2101 ko
= &part_to_dev(rdev
->bdev
->bd_part
)->kobj
;
2102 if (sysfs_create_link(&rdev
->kobj
, ko
, "block"))
2103 /* failure here is OK */;
2104 rdev
->sysfs_state
= sysfs_get_dirent_safe(rdev
->kobj
.sd
, "state");
2106 list_add_rcu(&rdev
->same_set
, &mddev
->disks
);
2107 bd_link_disk_holder(rdev
->bdev
, mddev
->gendisk
);
2109 /* May as well allow recovery to be retried once */
2110 mddev
->recovery_disabled
++;
2115 printk(KERN_WARNING
"md: failed to register dev-%s for %s\n",
2120 static void md_delayed_delete(struct work_struct
*ws
)
2122 struct md_rdev
*rdev
= container_of(ws
, struct md_rdev
, del_work
);
2123 kobject_del(&rdev
->kobj
);
2124 kobject_put(&rdev
->kobj
);
2127 static void unbind_rdev_from_array(struct md_rdev
*rdev
)
2129 char b
[BDEVNAME_SIZE
];
2131 bd_unlink_disk_holder(rdev
->bdev
, rdev
->mddev
->gendisk
);
2132 list_del_rcu(&rdev
->same_set
);
2133 printk(KERN_INFO
"md: unbind<%s>\n", bdevname(rdev
->bdev
,b
));
2135 sysfs_remove_link(&rdev
->kobj
, "block");
2136 sysfs_put(rdev
->sysfs_state
);
2137 rdev
->sysfs_state
= NULL
;
2138 rdev
->badblocks
.count
= 0;
2139 /* We need to delay this, otherwise we can deadlock when
2140 * writing to 'remove' to "dev/state". We also need
2141 * to delay it due to rcu usage.
2144 INIT_WORK(&rdev
->del_work
, md_delayed_delete
);
2145 kobject_get(&rdev
->kobj
);
2146 queue_work(md_misc_wq
, &rdev
->del_work
);
2150 * prevent the device from being mounted, repartitioned or
2151 * otherwise reused by a RAID array (or any other kernel
2152 * subsystem), by bd_claiming the device.
2154 static int lock_rdev(struct md_rdev
*rdev
, dev_t dev
, int shared
)
2157 struct block_device
*bdev
;
2158 char b
[BDEVNAME_SIZE
];
2160 bdev
= blkdev_get_by_dev(dev
, FMODE_READ
|FMODE_WRITE
|FMODE_EXCL
,
2161 shared
? (struct md_rdev
*)lock_rdev
: rdev
);
2163 printk(KERN_ERR
"md: could not open %s.\n",
2164 __bdevname(dev
, b
));
2165 return PTR_ERR(bdev
);
2171 static void unlock_rdev(struct md_rdev
*rdev
)
2173 struct block_device
*bdev
= rdev
->bdev
;
2175 blkdev_put(bdev
, FMODE_READ
|FMODE_WRITE
|FMODE_EXCL
);
2178 void md_autodetect_dev(dev_t dev
);
2180 static void export_rdev(struct md_rdev
*rdev
)
2182 char b
[BDEVNAME_SIZE
];
2184 printk(KERN_INFO
"md: export_rdev(%s)\n",
2185 bdevname(rdev
->bdev
,b
));
2186 md_rdev_clear(rdev
);
2188 if (test_bit(AutoDetected
, &rdev
->flags
))
2189 md_autodetect_dev(rdev
->bdev
->bd_dev
);
2192 kobject_put(&rdev
->kobj
);
2195 void md_kick_rdev_from_array(struct md_rdev
*rdev
)
2197 unbind_rdev_from_array(rdev
);
2200 EXPORT_SYMBOL_GPL(md_kick_rdev_from_array
);
2202 static void export_array(struct mddev
*mddev
)
2204 struct md_rdev
*rdev
;
2206 while (!list_empty(&mddev
->disks
)) {
2207 rdev
= list_first_entry(&mddev
->disks
, struct md_rdev
,
2209 md_kick_rdev_from_array(rdev
);
2211 mddev
->raid_disks
= 0;
2212 mddev
->major_version
= 0;
2215 static void sync_sbs(struct mddev
*mddev
, int nospares
)
2217 /* Update each superblock (in-memory image), but
2218 * if we are allowed to, skip spares which already
2219 * have the right event counter, or have one earlier
2220 * (which would mean they aren't being marked as dirty
2221 * with the rest of the array)
2223 struct md_rdev
*rdev
;
2224 rdev_for_each(rdev
, mddev
) {
2225 if (rdev
->sb_events
== mddev
->events
||
2227 rdev
->raid_disk
< 0 &&
2228 rdev
->sb_events
+1 == mddev
->events
)) {
2229 /* Don't update this superblock */
2230 rdev
->sb_loaded
= 2;
2232 sync_super(mddev
, rdev
);
2233 rdev
->sb_loaded
= 1;
2238 static bool does_sb_need_changing(struct mddev
*mddev
)
2240 struct md_rdev
*rdev
;
2241 struct mdp_superblock_1
*sb
;
2244 /* Find a good rdev */
2245 rdev_for_each(rdev
, mddev
)
2246 if ((rdev
->raid_disk
>= 0) && !test_bit(Faulty
, &rdev
->flags
))
2249 /* No good device found. */
2253 sb
= page_address(rdev
->sb_page
);
2254 /* Check if a device has become faulty or a spare become active */
2255 rdev_for_each(rdev
, mddev
) {
2256 role
= le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]);
2257 /* Device activated? */
2258 if (role
== 0xffff && rdev
->raid_disk
>=0 &&
2259 !test_bit(Faulty
, &rdev
->flags
))
2261 /* Device turned faulty? */
2262 if (test_bit(Faulty
, &rdev
->flags
) && (role
< 0xfffd))
2266 /* Check if any mddev parameters have changed */
2267 if ((mddev
->dev_sectors
!= le64_to_cpu(sb
->size
)) ||
2268 (mddev
->reshape_position
!= le64_to_cpu(sb
->reshape_position
)) ||
2269 (mddev
->layout
!= le64_to_cpu(sb
->layout
)) ||
2270 (mddev
->raid_disks
!= le32_to_cpu(sb
->raid_disks
)) ||
2271 (mddev
->chunk_sectors
!= le32_to_cpu(sb
->chunksize
)))
2277 void md_update_sb(struct mddev
*mddev
, int force_change
)
2279 struct md_rdev
*rdev
;
2282 int any_badblocks_changed
= 0;
2287 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
2291 if (mddev_is_clustered(mddev
)) {
2292 if (test_and_clear_bit(MD_CHANGE_DEVS
, &mddev
->flags
))
2294 ret
= md_cluster_ops
->metadata_update_start(mddev
);
2295 /* Has someone else has updated the sb */
2296 if (!does_sb_need_changing(mddev
)) {
2298 md_cluster_ops
->metadata_update_cancel(mddev
);
2299 clear_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
2304 /* First make sure individual recovery_offsets are correct */
2305 rdev_for_each(rdev
, mddev
) {
2306 if (rdev
->raid_disk
>= 0 &&
2307 mddev
->delta_disks
>= 0 &&
2308 !test_bit(Journal
, &rdev
->flags
) &&
2309 !test_bit(In_sync
, &rdev
->flags
) &&
2310 mddev
->curr_resync_completed
> rdev
->recovery_offset
)
2311 rdev
->recovery_offset
= mddev
->curr_resync_completed
;
2314 if (!mddev
->persistent
) {
2315 clear_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
2316 clear_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
2317 if (!mddev
->external
) {
2318 clear_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
2319 rdev_for_each(rdev
, mddev
) {
2320 if (rdev
->badblocks
.changed
) {
2321 rdev
->badblocks
.changed
= 0;
2322 md_ack_all_badblocks(&rdev
->badblocks
);
2323 md_error(mddev
, rdev
);
2325 clear_bit(Blocked
, &rdev
->flags
);
2326 clear_bit(BlockedBadBlocks
, &rdev
->flags
);
2327 wake_up(&rdev
->blocked_wait
);
2330 wake_up(&mddev
->sb_wait
);
2334 spin_lock(&mddev
->lock
);
2336 mddev
->utime
= get_seconds();
2338 if (test_and_clear_bit(MD_CHANGE_DEVS
, &mddev
->flags
))
2340 if (test_and_clear_bit(MD_CHANGE_CLEAN
, &mddev
->flags
))
2341 /* just a clean<-> dirty transition, possibly leave spares alone,
2342 * though if events isn't the right even/odd, we will have to do
2348 if (mddev
->degraded
)
2349 /* If the array is degraded, then skipping spares is both
2350 * dangerous and fairly pointless.
2351 * Dangerous because a device that was removed from the array
2352 * might have a event_count that still looks up-to-date,
2353 * so it can be re-added without a resync.
2354 * Pointless because if there are any spares to skip,
2355 * then a recovery will happen and soon that array won't
2356 * be degraded any more and the spare can go back to sleep then.
2360 sync_req
= mddev
->in_sync
;
2362 /* If this is just a dirty<->clean transition, and the array is clean
2363 * and 'events' is odd, we can roll back to the previous clean state */
2365 && (mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
)
2366 && mddev
->can_decrease_events
2367 && mddev
->events
!= 1) {
2369 mddev
->can_decrease_events
= 0;
2371 /* otherwise we have to go forward and ... */
2373 mddev
->can_decrease_events
= nospares
;
2377 * This 64-bit counter should never wrap.
2378 * Either we are in around ~1 trillion A.C., assuming
2379 * 1 reboot per second, or we have a bug...
2381 WARN_ON(mddev
->events
== 0);
2383 rdev_for_each(rdev
, mddev
) {
2384 if (rdev
->badblocks
.changed
)
2385 any_badblocks_changed
++;
2386 if (test_bit(Faulty
, &rdev
->flags
))
2387 set_bit(FaultRecorded
, &rdev
->flags
);
2390 sync_sbs(mddev
, nospares
);
2391 spin_unlock(&mddev
->lock
);
2393 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2394 mdname(mddev
), mddev
->in_sync
);
2396 bitmap_update_sb(mddev
->bitmap
);
2397 rdev_for_each(rdev
, mddev
) {
2398 char b
[BDEVNAME_SIZE
];
2400 if (rdev
->sb_loaded
!= 1)
2401 continue; /* no noise on spare devices */
2403 if (!test_bit(Faulty
, &rdev
->flags
)) {
2404 md_super_write(mddev
,rdev
,
2405 rdev
->sb_start
, rdev
->sb_size
,
2407 pr_debug("md: (write) %s's sb offset: %llu\n",
2408 bdevname(rdev
->bdev
, b
),
2409 (unsigned long long)rdev
->sb_start
);
2410 rdev
->sb_events
= mddev
->events
;
2411 if (rdev
->badblocks
.size
) {
2412 md_super_write(mddev
, rdev
,
2413 rdev
->badblocks
.sector
,
2414 rdev
->badblocks
.size
<< 9,
2416 rdev
->badblocks
.size
= 0;
2420 pr_debug("md: %s (skipping faulty)\n",
2421 bdevname(rdev
->bdev
, b
));
2423 if (mddev
->level
== LEVEL_MULTIPATH
)
2424 /* only need to write one superblock... */
2427 md_super_wait(mddev
);
2428 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
2430 spin_lock(&mddev
->lock
);
2431 if (mddev
->in_sync
!= sync_req
||
2432 test_bit(MD_CHANGE_DEVS
, &mddev
->flags
)) {
2433 /* have to write it out again */
2434 spin_unlock(&mddev
->lock
);
2437 clear_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
2438 spin_unlock(&mddev
->lock
);
2439 wake_up(&mddev
->sb_wait
);
2440 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
2441 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
2443 rdev_for_each(rdev
, mddev
) {
2444 if (test_and_clear_bit(FaultRecorded
, &rdev
->flags
))
2445 clear_bit(Blocked
, &rdev
->flags
);
2447 if (any_badblocks_changed
)
2448 md_ack_all_badblocks(&rdev
->badblocks
);
2449 clear_bit(BlockedBadBlocks
, &rdev
->flags
);
2450 wake_up(&rdev
->blocked_wait
);
2453 if (mddev_is_clustered(mddev
) && ret
== 0)
2454 md_cluster_ops
->metadata_update_finish(mddev
);
2456 EXPORT_SYMBOL(md_update_sb
);
2458 static int add_bound_rdev(struct md_rdev
*rdev
)
2460 struct mddev
*mddev
= rdev
->mddev
;
2463 if (!mddev
->pers
->hot_remove_disk
) {
2464 /* If there is hot_add_disk but no hot_remove_disk
2465 * then added disks for geometry changes,
2466 * and should be added immediately.
2468 super_types
[mddev
->major_version
].
2469 validate_super(mddev
, rdev
);
2470 err
= mddev
->pers
->hot_add_disk(mddev
, rdev
);
2472 unbind_rdev_from_array(rdev
);
2477 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
2479 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
2480 if (mddev
->degraded
)
2481 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
2482 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
2483 md_new_event(mddev
);
2484 md_wakeup_thread(mddev
->thread
);
2488 /* words written to sysfs files may, or may not, be \n terminated.
2489 * We want to accept with case. For this we use cmd_match.
2491 static int cmd_match(const char *cmd
, const char *str
)
2493 /* See if cmd, written into a sysfs file, matches
2494 * str. They must either be the same, or cmd can
2495 * have a trailing newline
2497 while (*cmd
&& *str
&& *cmd
== *str
) {
2508 struct rdev_sysfs_entry
{
2509 struct attribute attr
;
2510 ssize_t (*show
)(struct md_rdev
*, char *);
2511 ssize_t (*store
)(struct md_rdev
*, const char *, size_t);
2515 state_show(struct md_rdev
*rdev
, char *page
)
2519 unsigned long flags
= ACCESS_ONCE(rdev
->flags
);
2521 if (test_bit(Faulty
, &flags
) ||
2522 rdev
->badblocks
.unacked_exist
) {
2523 len
+= sprintf(page
+len
, "%sfaulty",sep
);
2526 if (test_bit(In_sync
, &flags
)) {
2527 len
+= sprintf(page
+len
, "%sin_sync",sep
);
2530 if (test_bit(Journal
, &flags
)) {
2531 len
+= sprintf(page
+len
, "%sjournal",sep
);
2534 if (test_bit(WriteMostly
, &flags
)) {
2535 len
+= sprintf(page
+len
, "%swrite_mostly",sep
);
2538 if (test_bit(Blocked
, &flags
) ||
2539 (rdev
->badblocks
.unacked_exist
2540 && !test_bit(Faulty
, &flags
))) {
2541 len
+= sprintf(page
+len
, "%sblocked", sep
);
2544 if (!test_bit(Faulty
, &flags
) &&
2545 !test_bit(Journal
, &flags
) &&
2546 !test_bit(In_sync
, &flags
)) {
2547 len
+= sprintf(page
+len
, "%sspare", sep
);
2550 if (test_bit(WriteErrorSeen
, &flags
)) {
2551 len
+= sprintf(page
+len
, "%swrite_error", sep
);
2554 if (test_bit(WantReplacement
, &flags
)) {
2555 len
+= sprintf(page
+len
, "%swant_replacement", sep
);
2558 if (test_bit(Replacement
, &flags
)) {
2559 len
+= sprintf(page
+len
, "%sreplacement", sep
);
2563 return len
+sprintf(page
+len
, "\n");
2567 state_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
2570 * faulty - simulates an error
2571 * remove - disconnects the device
2572 * writemostly - sets write_mostly
2573 * -writemostly - clears write_mostly
2574 * blocked - sets the Blocked flags
2575 * -blocked - clears the Blocked and possibly simulates an error
2576 * insync - sets Insync providing device isn't active
2577 * -insync - clear Insync for a device with a slot assigned,
2578 * so that it gets rebuilt based on bitmap
2579 * write_error - sets WriteErrorSeen
2580 * -write_error - clears WriteErrorSeen
2583 if (cmd_match(buf
, "faulty") && rdev
->mddev
->pers
) {
2584 md_error(rdev
->mddev
, rdev
);
2585 if (test_bit(Faulty
, &rdev
->flags
))
2589 } else if (cmd_match(buf
, "remove")) {
2590 if (rdev
->raid_disk
>= 0)
2593 struct mddev
*mddev
= rdev
->mddev
;
2595 if (mddev_is_clustered(mddev
))
2596 err
= md_cluster_ops
->remove_disk(mddev
, rdev
);
2599 md_kick_rdev_from_array(rdev
);
2601 md_update_sb(mddev
, 1);
2602 md_new_event(mddev
);
2605 } else if (cmd_match(buf
, "writemostly")) {
2606 set_bit(WriteMostly
, &rdev
->flags
);
2608 } else if (cmd_match(buf
, "-writemostly")) {
2609 clear_bit(WriteMostly
, &rdev
->flags
);
2611 } else if (cmd_match(buf
, "blocked")) {
2612 set_bit(Blocked
, &rdev
->flags
);
2614 } else if (cmd_match(buf
, "-blocked")) {
2615 if (!test_bit(Faulty
, &rdev
->flags
) &&
2616 rdev
->badblocks
.unacked_exist
) {
2617 /* metadata handler doesn't understand badblocks,
2618 * so we need to fail the device
2620 md_error(rdev
->mddev
, rdev
);
2622 clear_bit(Blocked
, &rdev
->flags
);
2623 clear_bit(BlockedBadBlocks
, &rdev
->flags
);
2624 wake_up(&rdev
->blocked_wait
);
2625 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
2626 md_wakeup_thread(rdev
->mddev
->thread
);
2629 } else if (cmd_match(buf
, "insync") && rdev
->raid_disk
== -1) {
2630 set_bit(In_sync
, &rdev
->flags
);
2632 } else if (cmd_match(buf
, "-insync") && rdev
->raid_disk
>= 0 &&
2633 !test_bit(Journal
, &rdev
->flags
)) {
2634 if (rdev
->mddev
->pers
== NULL
) {
2635 clear_bit(In_sync
, &rdev
->flags
);
2636 rdev
->saved_raid_disk
= rdev
->raid_disk
;
2637 rdev
->raid_disk
= -1;
2640 } else if (cmd_match(buf
, "write_error")) {
2641 set_bit(WriteErrorSeen
, &rdev
->flags
);
2643 } else if (cmd_match(buf
, "-write_error")) {
2644 clear_bit(WriteErrorSeen
, &rdev
->flags
);
2646 } else if (cmd_match(buf
, "want_replacement")) {
2647 /* Any non-spare device that is not a replacement can
2648 * become want_replacement at any time, but we then need to
2649 * check if recovery is needed.
2651 if (rdev
->raid_disk
>= 0 &&
2652 !test_bit(Journal
, &rdev
->flags
) &&
2653 !test_bit(Replacement
, &rdev
->flags
))
2654 set_bit(WantReplacement
, &rdev
->flags
);
2655 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
2656 md_wakeup_thread(rdev
->mddev
->thread
);
2658 } else if (cmd_match(buf
, "-want_replacement")) {
2659 /* Clearing 'want_replacement' is always allowed.
2660 * Once replacements starts it is too late though.
2663 clear_bit(WantReplacement
, &rdev
->flags
);
2664 } else if (cmd_match(buf
, "replacement")) {
2665 /* Can only set a device as a replacement when array has not
2666 * yet been started. Once running, replacement is automatic
2667 * from spares, or by assigning 'slot'.
2669 if (rdev
->mddev
->pers
)
2672 set_bit(Replacement
, &rdev
->flags
);
2675 } else if (cmd_match(buf
, "-replacement")) {
2676 /* Similarly, can only clear Replacement before start */
2677 if (rdev
->mddev
->pers
)
2680 clear_bit(Replacement
, &rdev
->flags
);
2683 } else if (cmd_match(buf
, "re-add")) {
2684 if (test_bit(Faulty
, &rdev
->flags
) && (rdev
->raid_disk
== -1)) {
2685 /* clear_bit is performed _after_ all the devices
2686 * have their local Faulty bit cleared. If any writes
2687 * happen in the meantime in the local node, they
2688 * will land in the local bitmap, which will be synced
2689 * by this node eventually
2691 if (!mddev_is_clustered(rdev
->mddev
) ||
2692 (err
= md_cluster_ops
->gather_bitmaps(rdev
)) == 0) {
2693 clear_bit(Faulty
, &rdev
->flags
);
2694 err
= add_bound_rdev(rdev
);
2700 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
2701 return err
? err
: len
;
2703 static struct rdev_sysfs_entry rdev_state
=
2704 __ATTR_PREALLOC(state
, S_IRUGO
|S_IWUSR
, state_show
, state_store
);
2707 errors_show(struct md_rdev
*rdev
, char *page
)
2709 return sprintf(page
, "%d\n", atomic_read(&rdev
->corrected_errors
));
2713 errors_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
2718 rv
= kstrtouint(buf
, 10, &n
);
2721 atomic_set(&rdev
->corrected_errors
, n
);
2724 static struct rdev_sysfs_entry rdev_errors
=
2725 __ATTR(errors
, S_IRUGO
|S_IWUSR
, errors_show
, errors_store
);
2728 slot_show(struct md_rdev
*rdev
, char *page
)
2730 if (test_bit(Journal
, &rdev
->flags
))
2731 return sprintf(page
, "journal\n");
2732 else if (rdev
->raid_disk
< 0)
2733 return sprintf(page
, "none\n");
2735 return sprintf(page
, "%d\n", rdev
->raid_disk
);
2739 slot_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
2744 if (test_bit(Journal
, &rdev
->flags
))
2746 if (strncmp(buf
, "none", 4)==0)
2749 err
= kstrtouint(buf
, 10, (unsigned int *)&slot
);
2753 if (rdev
->mddev
->pers
&& slot
== -1) {
2754 /* Setting 'slot' on an active array requires also
2755 * updating the 'rd%d' link, and communicating
2756 * with the personality with ->hot_*_disk.
2757 * For now we only support removing
2758 * failed/spare devices. This normally happens automatically,
2759 * but not when the metadata is externally managed.
2761 if (rdev
->raid_disk
== -1)
2763 /* personality does all needed checks */
2764 if (rdev
->mddev
->pers
->hot_remove_disk
== NULL
)
2766 clear_bit(Blocked
, &rdev
->flags
);
2767 remove_and_add_spares(rdev
->mddev
, rdev
);
2768 if (rdev
->raid_disk
>= 0)
2770 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
2771 md_wakeup_thread(rdev
->mddev
->thread
);
2772 } else if (rdev
->mddev
->pers
) {
2773 /* Activating a spare .. or possibly reactivating
2774 * if we ever get bitmaps working here.
2777 if (rdev
->raid_disk
!= -1)
2780 if (test_bit(MD_RECOVERY_RUNNING
, &rdev
->mddev
->recovery
))
2783 if (rdev
->mddev
->pers
->hot_add_disk
== NULL
)
2786 if (slot
>= rdev
->mddev
->raid_disks
&&
2787 slot
>= rdev
->mddev
->raid_disks
+ rdev
->mddev
->delta_disks
)
2790 rdev
->raid_disk
= slot
;
2791 if (test_bit(In_sync
, &rdev
->flags
))
2792 rdev
->saved_raid_disk
= slot
;
2794 rdev
->saved_raid_disk
= -1;
2795 clear_bit(In_sync
, &rdev
->flags
);
2796 clear_bit(Bitmap_sync
, &rdev
->flags
);
2797 remove_and_add_spares(rdev
->mddev
, rdev
);
2798 if (rdev
->raid_disk
== -1)
2800 /* don't wakeup anyone, leave that to userspace. */
2802 if (slot
>= rdev
->mddev
->raid_disks
&&
2803 slot
>= rdev
->mddev
->raid_disks
+ rdev
->mddev
->delta_disks
)
2805 rdev
->raid_disk
= slot
;
2806 /* assume it is working */
2807 clear_bit(Faulty
, &rdev
->flags
);
2808 clear_bit(WriteMostly
, &rdev
->flags
);
2809 set_bit(In_sync
, &rdev
->flags
);
2810 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
2815 static struct rdev_sysfs_entry rdev_slot
=
2816 __ATTR(slot
, S_IRUGO
|S_IWUSR
, slot_show
, slot_store
);
2819 offset_show(struct md_rdev
*rdev
, char *page
)
2821 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->data_offset
);
2825 offset_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
2827 unsigned long long offset
;
2828 if (kstrtoull(buf
, 10, &offset
) < 0)
2830 if (rdev
->mddev
->pers
&& rdev
->raid_disk
>= 0)
2832 if (rdev
->sectors
&& rdev
->mddev
->external
)
2833 /* Must set offset before size, so overlap checks
2836 rdev
->data_offset
= offset
;
2837 rdev
->new_data_offset
= offset
;
2841 static struct rdev_sysfs_entry rdev_offset
=
2842 __ATTR(offset
, S_IRUGO
|S_IWUSR
, offset_show
, offset_store
);
2844 static ssize_t
new_offset_show(struct md_rdev
*rdev
, char *page
)
2846 return sprintf(page
, "%llu\n",
2847 (unsigned long long)rdev
->new_data_offset
);
2850 static ssize_t
new_offset_store(struct md_rdev
*rdev
,
2851 const char *buf
, size_t len
)
2853 unsigned long long new_offset
;
2854 struct mddev
*mddev
= rdev
->mddev
;
2856 if (kstrtoull(buf
, 10, &new_offset
) < 0)
2859 if (mddev
->sync_thread
||
2860 test_bit(MD_RECOVERY_RUNNING
,&mddev
->recovery
))
2862 if (new_offset
== rdev
->data_offset
)
2863 /* reset is always permitted */
2865 else if (new_offset
> rdev
->data_offset
) {
2866 /* must not push array size beyond rdev_sectors */
2867 if (new_offset
- rdev
->data_offset
2868 + mddev
->dev_sectors
> rdev
->sectors
)
2871 /* Metadata worries about other space details. */
2873 /* decreasing the offset is inconsistent with a backwards
2876 if (new_offset
< rdev
->data_offset
&&
2877 mddev
->reshape_backwards
)
2879 /* Increasing offset is inconsistent with forwards
2880 * reshape. reshape_direction should be set to
2881 * 'backwards' first.
2883 if (new_offset
> rdev
->data_offset
&&
2884 !mddev
->reshape_backwards
)
2887 if (mddev
->pers
&& mddev
->persistent
&&
2888 !super_types
[mddev
->major_version
]
2889 .allow_new_offset(rdev
, new_offset
))
2891 rdev
->new_data_offset
= new_offset
;
2892 if (new_offset
> rdev
->data_offset
)
2893 mddev
->reshape_backwards
= 1;
2894 else if (new_offset
< rdev
->data_offset
)
2895 mddev
->reshape_backwards
= 0;
2899 static struct rdev_sysfs_entry rdev_new_offset
=
2900 __ATTR(new_offset
, S_IRUGO
|S_IWUSR
, new_offset_show
, new_offset_store
);
2903 rdev_size_show(struct md_rdev
*rdev
, char *page
)
2905 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->sectors
/ 2);
2908 static int overlaps(sector_t s1
, sector_t l1
, sector_t s2
, sector_t l2
)
2910 /* check if two start/length pairs overlap */
2918 static int strict_blocks_to_sectors(const char *buf
, sector_t
*sectors
)
2920 unsigned long long blocks
;
2923 if (kstrtoull(buf
, 10, &blocks
) < 0)
2926 if (blocks
& 1ULL << (8 * sizeof(blocks
) - 1))
2927 return -EINVAL
; /* sector conversion overflow */
2930 if (new != blocks
* 2)
2931 return -EINVAL
; /* unsigned long long to sector_t overflow */
2938 rdev_size_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
2940 struct mddev
*my_mddev
= rdev
->mddev
;
2941 sector_t oldsectors
= rdev
->sectors
;
2944 if (test_bit(Journal
, &rdev
->flags
))
2946 if (strict_blocks_to_sectors(buf
, §ors
) < 0)
2948 if (rdev
->data_offset
!= rdev
->new_data_offset
)
2949 return -EINVAL
; /* too confusing */
2950 if (my_mddev
->pers
&& rdev
->raid_disk
>= 0) {
2951 if (my_mddev
->persistent
) {
2952 sectors
= super_types
[my_mddev
->major_version
].
2953 rdev_size_change(rdev
, sectors
);
2956 } else if (!sectors
)
2957 sectors
= (i_size_read(rdev
->bdev
->bd_inode
) >> 9) -
2959 if (!my_mddev
->pers
->resize
)
2960 /* Cannot change size for RAID0 or Linear etc */
2963 if (sectors
< my_mddev
->dev_sectors
)
2964 return -EINVAL
; /* component must fit device */
2966 rdev
->sectors
= sectors
;
2967 if (sectors
> oldsectors
&& my_mddev
->external
) {
2968 /* Need to check that all other rdevs with the same
2969 * ->bdev do not overlap. 'rcu' is sufficient to walk
2970 * the rdev lists safely.
2971 * This check does not provide a hard guarantee, it
2972 * just helps avoid dangerous mistakes.
2974 struct mddev
*mddev
;
2976 struct list_head
*tmp
;
2979 for_each_mddev(mddev
, tmp
) {
2980 struct md_rdev
*rdev2
;
2982 rdev_for_each(rdev2
, mddev
)
2983 if (rdev
->bdev
== rdev2
->bdev
&&
2985 overlaps(rdev
->data_offset
, rdev
->sectors
,
2998 /* Someone else could have slipped in a size
2999 * change here, but doing so is just silly.
3000 * We put oldsectors back because we *know* it is
3001 * safe, and trust userspace not to race with
3004 rdev
->sectors
= oldsectors
;
3011 static struct rdev_sysfs_entry rdev_size
=
3012 __ATTR(size
, S_IRUGO
|S_IWUSR
, rdev_size_show
, rdev_size_store
);
3014 static ssize_t
recovery_start_show(struct md_rdev
*rdev
, char *page
)
3016 unsigned long long recovery_start
= rdev
->recovery_offset
;
3018 if (test_bit(In_sync
, &rdev
->flags
) ||
3019 recovery_start
== MaxSector
)
3020 return sprintf(page
, "none\n");
3022 return sprintf(page
, "%llu\n", recovery_start
);
3025 static ssize_t
recovery_start_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
3027 unsigned long long recovery_start
;
3029 if (cmd_match(buf
, "none"))
3030 recovery_start
= MaxSector
;
3031 else if (kstrtoull(buf
, 10, &recovery_start
))
3034 if (rdev
->mddev
->pers
&&
3035 rdev
->raid_disk
>= 0)
3038 rdev
->recovery_offset
= recovery_start
;
3039 if (recovery_start
== MaxSector
)
3040 set_bit(In_sync
, &rdev
->flags
);
3042 clear_bit(In_sync
, &rdev
->flags
);
3046 static struct rdev_sysfs_entry rdev_recovery_start
=
3047 __ATTR(recovery_start
, S_IRUGO
|S_IWUSR
, recovery_start_show
, recovery_start_store
);
3050 badblocks_show(struct badblocks
*bb
, char *page
, int unack
);
3052 badblocks_store(struct badblocks
*bb
, const char *page
, size_t len
, int unack
);
3054 static ssize_t
bb_show(struct md_rdev
*rdev
, char *page
)
3056 return badblocks_show(&rdev
->badblocks
, page
, 0);
3058 static ssize_t
bb_store(struct md_rdev
*rdev
, const char *page
, size_t len
)
3060 int rv
= badblocks_store(&rdev
->badblocks
, page
, len
, 0);
3061 /* Maybe that ack was all we needed */
3062 if (test_and_clear_bit(BlockedBadBlocks
, &rdev
->flags
))
3063 wake_up(&rdev
->blocked_wait
);
3066 static struct rdev_sysfs_entry rdev_bad_blocks
=
3067 __ATTR(bad_blocks
, S_IRUGO
|S_IWUSR
, bb_show
, bb_store
);
3069 static ssize_t
ubb_show(struct md_rdev
*rdev
, char *page
)
3071 return badblocks_show(&rdev
->badblocks
, page
, 1);
3073 static ssize_t
ubb_store(struct md_rdev
*rdev
, const char *page
, size_t len
)
3075 return badblocks_store(&rdev
->badblocks
, page
, len
, 1);
3077 static struct rdev_sysfs_entry rdev_unack_bad_blocks
=
3078 __ATTR(unacknowledged_bad_blocks
, S_IRUGO
|S_IWUSR
, ubb_show
, ubb_store
);
3080 static struct attribute
*rdev_default_attrs
[] = {
3085 &rdev_new_offset
.attr
,
3087 &rdev_recovery_start
.attr
,
3088 &rdev_bad_blocks
.attr
,
3089 &rdev_unack_bad_blocks
.attr
,
3093 rdev_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
3095 struct rdev_sysfs_entry
*entry
= container_of(attr
, struct rdev_sysfs_entry
, attr
);
3096 struct md_rdev
*rdev
= container_of(kobj
, struct md_rdev
, kobj
);
3102 return entry
->show(rdev
, page
);
3106 rdev_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
3107 const char *page
, size_t length
)
3109 struct rdev_sysfs_entry
*entry
= container_of(attr
, struct rdev_sysfs_entry
, attr
);
3110 struct md_rdev
*rdev
= container_of(kobj
, struct md_rdev
, kobj
);
3112 struct mddev
*mddev
= rdev
->mddev
;
3116 if (!capable(CAP_SYS_ADMIN
))
3118 rv
= mddev
? mddev_lock(mddev
): -EBUSY
;
3120 if (rdev
->mddev
== NULL
)
3123 rv
= entry
->store(rdev
, page
, length
);
3124 mddev_unlock(mddev
);
3129 static void rdev_free(struct kobject
*ko
)
3131 struct md_rdev
*rdev
= container_of(ko
, struct md_rdev
, kobj
);
3134 static const struct sysfs_ops rdev_sysfs_ops
= {
3135 .show
= rdev_attr_show
,
3136 .store
= rdev_attr_store
,
3138 static struct kobj_type rdev_ktype
= {
3139 .release
= rdev_free
,
3140 .sysfs_ops
= &rdev_sysfs_ops
,
3141 .default_attrs
= rdev_default_attrs
,
3144 int md_rdev_init(struct md_rdev
*rdev
)
3147 rdev
->saved_raid_disk
= -1;
3148 rdev
->raid_disk
= -1;
3150 rdev
->data_offset
= 0;
3151 rdev
->new_data_offset
= 0;
3152 rdev
->sb_events
= 0;
3153 rdev
->last_read_error
.tv_sec
= 0;
3154 rdev
->last_read_error
.tv_nsec
= 0;
3155 rdev
->sb_loaded
= 0;
3156 rdev
->bb_page
= NULL
;
3157 atomic_set(&rdev
->nr_pending
, 0);
3158 atomic_set(&rdev
->read_errors
, 0);
3159 atomic_set(&rdev
->corrected_errors
, 0);
3161 INIT_LIST_HEAD(&rdev
->same_set
);
3162 init_waitqueue_head(&rdev
->blocked_wait
);
3164 /* Add space to store bad block list.
3165 * This reserves the space even on arrays where it cannot
3166 * be used - I wonder if that matters
3168 rdev
->badblocks
.count
= 0;
3169 rdev
->badblocks
.shift
= -1; /* disabled until explicitly enabled */
3170 rdev
->badblocks
.page
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
3171 seqlock_init(&rdev
->badblocks
.lock
);
3172 if (rdev
->badblocks
.page
== NULL
)
3177 EXPORT_SYMBOL_GPL(md_rdev_init
);
3179 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3181 * mark the device faulty if:
3183 * - the device is nonexistent (zero size)
3184 * - the device has no valid superblock
3186 * a faulty rdev _never_ has rdev->sb set.
3188 static struct md_rdev
*md_import_device(dev_t newdev
, int super_format
, int super_minor
)
3190 char b
[BDEVNAME_SIZE
];
3192 struct md_rdev
*rdev
;
3195 rdev
= kzalloc(sizeof(*rdev
), GFP_KERNEL
);
3197 printk(KERN_ERR
"md: could not alloc mem for new device!\n");
3198 return ERR_PTR(-ENOMEM
);
3201 err
= md_rdev_init(rdev
);
3204 err
= alloc_disk_sb(rdev
);
3208 err
= lock_rdev(rdev
, newdev
, super_format
== -2);
3212 kobject_init(&rdev
->kobj
, &rdev_ktype
);
3214 size
= i_size_read(rdev
->bdev
->bd_inode
) >> BLOCK_SIZE_BITS
;
3217 "md: %s has zero or unknown size, marking faulty!\n",
3218 bdevname(rdev
->bdev
,b
));
3223 if (super_format
>= 0) {
3224 err
= super_types
[super_format
].
3225 load_super(rdev
, NULL
, super_minor
);
3226 if (err
== -EINVAL
) {
3228 "md: %s does not have a valid v%d.%d "
3229 "superblock, not importing!\n",
3230 bdevname(rdev
->bdev
,b
),
3231 super_format
, super_minor
);
3236 "md: could not read %s's sb, not importing!\n",
3237 bdevname(rdev
->bdev
,b
));
3247 md_rdev_clear(rdev
);
3249 return ERR_PTR(err
);
3253 * Check a full RAID array for plausibility
3256 static void analyze_sbs(struct mddev
*mddev
)
3259 struct md_rdev
*rdev
, *freshest
, *tmp
;
3260 char b
[BDEVNAME_SIZE
];
3263 rdev_for_each_safe(rdev
, tmp
, mddev
)
3264 switch (super_types
[mddev
->major_version
].
3265 load_super(rdev
, freshest
, mddev
->minor_version
)) {
3273 "md: fatal superblock inconsistency in %s"
3274 " -- removing from array\n",
3275 bdevname(rdev
->bdev
,b
));
3276 md_kick_rdev_from_array(rdev
);
3279 super_types
[mddev
->major_version
].
3280 validate_super(mddev
, freshest
);
3283 rdev_for_each_safe(rdev
, tmp
, mddev
) {
3284 if (mddev
->max_disks
&&
3285 (rdev
->desc_nr
>= mddev
->max_disks
||
3286 i
> mddev
->max_disks
)) {
3288 "md: %s: %s: only %d devices permitted\n",
3289 mdname(mddev
), bdevname(rdev
->bdev
, b
),
3291 md_kick_rdev_from_array(rdev
);
3294 if (rdev
!= freshest
) {
3295 if (super_types
[mddev
->major_version
].
3296 validate_super(mddev
, rdev
)) {
3297 printk(KERN_WARNING
"md: kicking non-fresh %s"
3299 bdevname(rdev
->bdev
,b
));
3300 md_kick_rdev_from_array(rdev
);
3304 if (mddev
->level
== LEVEL_MULTIPATH
) {
3305 rdev
->desc_nr
= i
++;
3306 rdev
->raid_disk
= rdev
->desc_nr
;
3307 set_bit(In_sync
, &rdev
->flags
);
3308 } else if (rdev
->raid_disk
>=
3309 (mddev
->raid_disks
- min(0, mddev
->delta_disks
)) &&
3310 !test_bit(Journal
, &rdev
->flags
)) {
3311 rdev
->raid_disk
= -1;
3312 clear_bit(In_sync
, &rdev
->flags
);
3317 /* Read a fixed-point number.
3318 * Numbers in sysfs attributes should be in "standard" units where
3319 * possible, so time should be in seconds.
3320 * However we internally use a a much smaller unit such as
3321 * milliseconds or jiffies.
3322 * This function takes a decimal number with a possible fractional
3323 * component, and produces an integer which is the result of
3324 * multiplying that number by 10^'scale'.
3325 * all without any floating-point arithmetic.
3327 int strict_strtoul_scaled(const char *cp
, unsigned long *res
, int scale
)
3329 unsigned long result
= 0;
3331 while (isdigit(*cp
) || (*cp
== '.' && decimals
< 0)) {
3334 else if (decimals
< scale
) {
3337 result
= result
* 10 + value
;
3349 while (decimals
< scale
) {
3358 safe_delay_show(struct mddev
*mddev
, char *page
)
3360 int msec
= (mddev
->safemode_delay
*1000)/HZ
;
3361 return sprintf(page
, "%d.%03d\n", msec
/1000, msec
%1000);
3364 safe_delay_store(struct mddev
*mddev
, const char *cbuf
, size_t len
)
3368 if (mddev_is_clustered(mddev
)) {
3369 pr_info("md: Safemode is disabled for clustered mode\n");
3373 if (strict_strtoul_scaled(cbuf
, &msec
, 3) < 0)
3376 mddev
->safemode_delay
= 0;
3378 unsigned long old_delay
= mddev
->safemode_delay
;
3379 unsigned long new_delay
= (msec
*HZ
)/1000;
3383 mddev
->safemode_delay
= new_delay
;
3384 if (new_delay
< old_delay
|| old_delay
== 0)
3385 mod_timer(&mddev
->safemode_timer
, jiffies
+1);
3389 static struct md_sysfs_entry md_safe_delay
=
3390 __ATTR(safe_mode_delay
, S_IRUGO
|S_IWUSR
,safe_delay_show
, safe_delay_store
);
3393 level_show(struct mddev
*mddev
, char *page
)
3395 struct md_personality
*p
;
3397 spin_lock(&mddev
->lock
);
3400 ret
= sprintf(page
, "%s\n", p
->name
);
3401 else if (mddev
->clevel
[0])
3402 ret
= sprintf(page
, "%s\n", mddev
->clevel
);
3403 else if (mddev
->level
!= LEVEL_NONE
)
3404 ret
= sprintf(page
, "%d\n", mddev
->level
);
3407 spin_unlock(&mddev
->lock
);
3412 level_store(struct mddev
*mddev
, const char *buf
, size_t len
)
3417 struct md_personality
*pers
, *oldpers
;
3419 void *priv
, *oldpriv
;
3420 struct md_rdev
*rdev
;
3422 if (slen
== 0 || slen
>= sizeof(clevel
))
3425 rv
= mddev_lock(mddev
);
3429 if (mddev
->pers
== NULL
) {
3430 strncpy(mddev
->clevel
, buf
, slen
);
3431 if (mddev
->clevel
[slen
-1] == '\n')
3433 mddev
->clevel
[slen
] = 0;
3434 mddev
->level
= LEVEL_NONE
;
3442 /* request to change the personality. Need to ensure:
3443 * - array is not engaged in resync/recovery/reshape
3444 * - old personality can be suspended
3445 * - new personality will access other array.
3449 if (mddev
->sync_thread
||
3450 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
3451 mddev
->reshape_position
!= MaxSector
||
3452 mddev
->sysfs_active
)
3456 if (!mddev
->pers
->quiesce
) {
3457 printk(KERN_WARNING
"md: %s: %s does not support online personality change\n",
3458 mdname(mddev
), mddev
->pers
->name
);
3462 /* Now find the new personality */
3463 strncpy(clevel
, buf
, slen
);
3464 if (clevel
[slen
-1] == '\n')
3467 if (kstrtol(clevel
, 10, &level
))
3470 if (request_module("md-%s", clevel
) != 0)
3471 request_module("md-level-%s", clevel
);
3472 spin_lock(&pers_lock
);
3473 pers
= find_pers(level
, clevel
);
3474 if (!pers
|| !try_module_get(pers
->owner
)) {
3475 spin_unlock(&pers_lock
);
3476 printk(KERN_WARNING
"md: personality %s not loaded\n", clevel
);
3480 spin_unlock(&pers_lock
);
3482 if (pers
== mddev
->pers
) {
3483 /* Nothing to do! */
3484 module_put(pers
->owner
);
3488 if (!pers
->takeover
) {
3489 module_put(pers
->owner
);
3490 printk(KERN_WARNING
"md: %s: %s does not support personality takeover\n",
3491 mdname(mddev
), clevel
);
3496 rdev_for_each(rdev
, mddev
)
3497 rdev
->new_raid_disk
= rdev
->raid_disk
;
3499 /* ->takeover must set new_* and/or delta_disks
3500 * if it succeeds, and may set them when it fails.
3502 priv
= pers
->takeover(mddev
);
3504 mddev
->new_level
= mddev
->level
;
3505 mddev
->new_layout
= mddev
->layout
;
3506 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
3507 mddev
->raid_disks
-= mddev
->delta_disks
;
3508 mddev
->delta_disks
= 0;
3509 mddev
->reshape_backwards
= 0;
3510 module_put(pers
->owner
);
3511 printk(KERN_WARNING
"md: %s: %s would not accept array\n",
3512 mdname(mddev
), clevel
);
3517 /* Looks like we have a winner */
3518 mddev_suspend(mddev
);
3519 mddev_detach(mddev
);
3521 spin_lock(&mddev
->lock
);
3522 oldpers
= mddev
->pers
;
3523 oldpriv
= mddev
->private;
3525 mddev
->private = priv
;
3526 strlcpy(mddev
->clevel
, pers
->name
, sizeof(mddev
->clevel
));
3527 mddev
->level
= mddev
->new_level
;
3528 mddev
->layout
= mddev
->new_layout
;
3529 mddev
->chunk_sectors
= mddev
->new_chunk_sectors
;
3530 mddev
->delta_disks
= 0;
3531 mddev
->reshape_backwards
= 0;
3532 mddev
->degraded
= 0;
3533 spin_unlock(&mddev
->lock
);
3535 if (oldpers
->sync_request
== NULL
&&
3537 /* We are converting from a no-redundancy array
3538 * to a redundancy array and metadata is managed
3539 * externally so we need to be sure that writes
3540 * won't block due to a need to transition
3542 * until external management is started.
3545 mddev
->safemode_delay
= 0;
3546 mddev
->safemode
= 0;
3549 oldpers
->free(mddev
, oldpriv
);
3551 if (oldpers
->sync_request
== NULL
&&
3552 pers
->sync_request
!= NULL
) {
3553 /* need to add the md_redundancy_group */
3554 if (sysfs_create_group(&mddev
->kobj
, &md_redundancy_group
))
3556 "md: cannot register extra attributes for %s\n",
3558 mddev
->sysfs_action
= sysfs_get_dirent(mddev
->kobj
.sd
, "sync_action");
3560 if (oldpers
->sync_request
!= NULL
&&
3561 pers
->sync_request
== NULL
) {
3562 /* need to remove the md_redundancy_group */
3563 if (mddev
->to_remove
== NULL
)
3564 mddev
->to_remove
= &md_redundancy_group
;
3567 rdev_for_each(rdev
, mddev
) {
3568 if (rdev
->raid_disk
< 0)
3570 if (rdev
->new_raid_disk
>= mddev
->raid_disks
)
3571 rdev
->new_raid_disk
= -1;
3572 if (rdev
->new_raid_disk
== rdev
->raid_disk
)
3574 sysfs_unlink_rdev(mddev
, rdev
);
3576 rdev_for_each(rdev
, mddev
) {
3577 if (rdev
->raid_disk
< 0)
3579 if (rdev
->new_raid_disk
== rdev
->raid_disk
)
3581 rdev
->raid_disk
= rdev
->new_raid_disk
;
3582 if (rdev
->raid_disk
< 0)
3583 clear_bit(In_sync
, &rdev
->flags
);
3585 if (sysfs_link_rdev(mddev
, rdev
))
3586 printk(KERN_WARNING
"md: cannot register rd%d"
3587 " for %s after level change\n",
3588 rdev
->raid_disk
, mdname(mddev
));
3592 if (pers
->sync_request
== NULL
) {
3593 /* this is now an array without redundancy, so
3594 * it must always be in_sync
3597 del_timer_sync(&mddev
->safemode_timer
);
3599 blk_set_stacking_limits(&mddev
->queue
->limits
);
3601 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
3602 mddev_resume(mddev
);
3604 md_update_sb(mddev
, 1);
3605 sysfs_notify(&mddev
->kobj
, NULL
, "level");
3606 md_new_event(mddev
);
3609 mddev_unlock(mddev
);
3613 static struct md_sysfs_entry md_level
=
3614 __ATTR(level
, S_IRUGO
|S_IWUSR
, level_show
, level_store
);
3617 layout_show(struct mddev
*mddev
, char *page
)
3619 /* just a number, not meaningful for all levels */
3620 if (mddev
->reshape_position
!= MaxSector
&&
3621 mddev
->layout
!= mddev
->new_layout
)
3622 return sprintf(page
, "%d (%d)\n",
3623 mddev
->new_layout
, mddev
->layout
);
3624 return sprintf(page
, "%d\n", mddev
->layout
);
3628 layout_store(struct mddev
*mddev
, const char *buf
, size_t len
)
3633 err
= kstrtouint(buf
, 10, &n
);
3636 err
= mddev_lock(mddev
);
3641 if (mddev
->pers
->check_reshape
== NULL
)
3646 mddev
->new_layout
= n
;
3647 err
= mddev
->pers
->check_reshape(mddev
);
3649 mddev
->new_layout
= mddev
->layout
;
3652 mddev
->new_layout
= n
;
3653 if (mddev
->reshape_position
== MaxSector
)
3656 mddev_unlock(mddev
);
3659 static struct md_sysfs_entry md_layout
=
3660 __ATTR(layout
, S_IRUGO
|S_IWUSR
, layout_show
, layout_store
);
3663 raid_disks_show(struct mddev
*mddev
, char *page
)
3665 if (mddev
->raid_disks
== 0)
3667 if (mddev
->reshape_position
!= MaxSector
&&
3668 mddev
->delta_disks
!= 0)
3669 return sprintf(page
, "%d (%d)\n", mddev
->raid_disks
,
3670 mddev
->raid_disks
- mddev
->delta_disks
);
3671 return sprintf(page
, "%d\n", mddev
->raid_disks
);
3674 static int update_raid_disks(struct mddev
*mddev
, int raid_disks
);
3677 raid_disks_store(struct mddev
*mddev
, const char *buf
, size_t len
)
3682 err
= kstrtouint(buf
, 10, &n
);
3686 err
= mddev_lock(mddev
);
3690 err
= update_raid_disks(mddev
, n
);
3691 else if (mddev
->reshape_position
!= MaxSector
) {
3692 struct md_rdev
*rdev
;
3693 int olddisks
= mddev
->raid_disks
- mddev
->delta_disks
;
3696 rdev_for_each(rdev
, mddev
) {
3698 rdev
->data_offset
< rdev
->new_data_offset
)
3701 rdev
->data_offset
> rdev
->new_data_offset
)
3705 mddev
->delta_disks
= n
- olddisks
;
3706 mddev
->raid_disks
= n
;
3707 mddev
->reshape_backwards
= (mddev
->delta_disks
< 0);
3709 mddev
->raid_disks
= n
;
3711 mddev_unlock(mddev
);
3712 return err
? err
: len
;
3714 static struct md_sysfs_entry md_raid_disks
=
3715 __ATTR(raid_disks
, S_IRUGO
|S_IWUSR
, raid_disks_show
, raid_disks_store
);
3718 chunk_size_show(struct mddev
*mddev
, char *page
)
3720 if (mddev
->reshape_position
!= MaxSector
&&
3721 mddev
->chunk_sectors
!= mddev
->new_chunk_sectors
)
3722 return sprintf(page
, "%d (%d)\n",
3723 mddev
->new_chunk_sectors
<< 9,
3724 mddev
->chunk_sectors
<< 9);
3725 return sprintf(page
, "%d\n", mddev
->chunk_sectors
<< 9);
3729 chunk_size_store(struct mddev
*mddev
, const char *buf
, size_t len
)
3734 err
= kstrtoul(buf
, 10, &n
);
3738 err
= mddev_lock(mddev
);
3742 if (mddev
->pers
->check_reshape
== NULL
)
3747 mddev
->new_chunk_sectors
= n
>> 9;
3748 err
= mddev
->pers
->check_reshape(mddev
);
3750 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
3753 mddev
->new_chunk_sectors
= n
>> 9;
3754 if (mddev
->reshape_position
== MaxSector
)
3755 mddev
->chunk_sectors
= n
>> 9;
3757 mddev_unlock(mddev
);
3760 static struct md_sysfs_entry md_chunk_size
=
3761 __ATTR(chunk_size
, S_IRUGO
|S_IWUSR
, chunk_size_show
, chunk_size_store
);
3764 resync_start_show(struct mddev
*mddev
, char *page
)
3766 if (mddev
->recovery_cp
== MaxSector
)
3767 return sprintf(page
, "none\n");
3768 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->recovery_cp
);
3772 resync_start_store(struct mddev
*mddev
, const char *buf
, size_t len
)
3774 unsigned long long n
;
3777 if (cmd_match(buf
, "none"))
3780 err
= kstrtoull(buf
, 10, &n
);
3783 if (n
!= (sector_t
)n
)
3787 err
= mddev_lock(mddev
);
3790 if (mddev
->pers
&& !test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
))
3794 mddev
->recovery_cp
= n
;
3796 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
3798 mddev_unlock(mddev
);
3801 static struct md_sysfs_entry md_resync_start
=
3802 __ATTR_PREALLOC(resync_start
, S_IRUGO
|S_IWUSR
,
3803 resync_start_show
, resync_start_store
);
3806 * The array state can be:
3809 * No devices, no size, no level
3810 * Equivalent to STOP_ARRAY ioctl
3812 * May have some settings, but array is not active
3813 * all IO results in error
3814 * When written, doesn't tear down array, but just stops it
3815 * suspended (not supported yet)
3816 * All IO requests will block. The array can be reconfigured.
3817 * Writing this, if accepted, will block until array is quiescent
3819 * no resync can happen. no superblocks get written.
3820 * write requests fail
3822 * like readonly, but behaves like 'clean' on a write request.
3824 * clean - no pending writes, but otherwise active.
3825 * When written to inactive array, starts without resync
3826 * If a write request arrives then
3827 * if metadata is known, mark 'dirty' and switch to 'active'.
3828 * if not known, block and switch to write-pending
3829 * If written to an active array that has pending writes, then fails.
3831 * fully active: IO and resync can be happening.
3832 * When written to inactive array, starts with resync
3835 * clean, but writes are blocked waiting for 'active' to be written.
3838 * like active, but no writes have been seen for a while (100msec).
3841 enum array_state
{ clear
, inactive
, suspended
, readonly
, read_auto
, clean
, active
,
3842 write_pending
, active_idle
, bad_word
};
3843 static char *array_states
[] = {
3844 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3845 "write-pending", "active-idle", NULL
};
3847 static int match_word(const char *word
, char **list
)
3850 for (n
=0; list
[n
]; n
++)
3851 if (cmd_match(word
, list
[n
]))
3857 array_state_show(struct mddev
*mddev
, char *page
)
3859 enum array_state st
= inactive
;
3872 else if (test_bit(MD_CHANGE_PENDING
, &mddev
->flags
))
3874 else if (mddev
->safemode
)
3880 if (list_empty(&mddev
->disks
) &&
3881 mddev
->raid_disks
== 0 &&
3882 mddev
->dev_sectors
== 0)
3887 return sprintf(page
, "%s\n", array_states
[st
]);
3890 static int do_md_stop(struct mddev
*mddev
, int ro
, struct block_device
*bdev
);
3891 static int md_set_readonly(struct mddev
*mddev
, struct block_device
*bdev
);
3892 static int do_md_run(struct mddev
*mddev
);
3893 static int restart_array(struct mddev
*mddev
);
3896 array_state_store(struct mddev
*mddev
, const char *buf
, size_t len
)
3899 enum array_state st
= match_word(buf
, array_states
);
3901 if (mddev
->pers
&& (st
== active
|| st
== clean
) && mddev
->ro
!= 1) {
3902 /* don't take reconfig_mutex when toggling between
3905 spin_lock(&mddev
->lock
);
3907 restart_array(mddev
);
3908 clear_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
3909 wake_up(&mddev
->sb_wait
);
3911 } else /* st == clean */ {
3912 restart_array(mddev
);
3913 if (atomic_read(&mddev
->writes_pending
) == 0) {
3914 if (mddev
->in_sync
== 0) {
3916 if (mddev
->safemode
== 1)
3917 mddev
->safemode
= 0;
3918 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
3924 spin_unlock(&mddev
->lock
);
3927 err
= mddev_lock(mddev
);
3935 /* stopping an active array */
3936 err
= do_md_stop(mddev
, 0, NULL
);
3939 /* stopping an active array */
3941 err
= do_md_stop(mddev
, 2, NULL
);
3943 err
= 0; /* already inactive */
3946 break; /* not supported yet */
3949 err
= md_set_readonly(mddev
, NULL
);
3952 set_disk_ro(mddev
->gendisk
, 1);
3953 err
= do_md_run(mddev
);
3959 err
= md_set_readonly(mddev
, NULL
);
3960 else if (mddev
->ro
== 1)
3961 err
= restart_array(mddev
);
3964 set_disk_ro(mddev
->gendisk
, 0);
3968 err
= do_md_run(mddev
);
3973 err
= restart_array(mddev
);
3976 spin_lock(&mddev
->lock
);
3977 if (atomic_read(&mddev
->writes_pending
) == 0) {
3978 if (mddev
->in_sync
== 0) {
3980 if (mddev
->safemode
== 1)
3981 mddev
->safemode
= 0;
3982 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
3987 spin_unlock(&mddev
->lock
);
3993 err
= restart_array(mddev
);
3996 clear_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
3997 wake_up(&mddev
->sb_wait
);
4001 set_disk_ro(mddev
->gendisk
, 0);
4002 err
= do_md_run(mddev
);
4007 /* these cannot be set */
4012 if (mddev
->hold_active
== UNTIL_IOCTL
)
4013 mddev
->hold_active
= 0;
4014 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4016 mddev_unlock(mddev
);
4019 static struct md_sysfs_entry md_array_state
=
4020 __ATTR_PREALLOC(array_state
, S_IRUGO
|S_IWUSR
, array_state_show
, array_state_store
);
4023 max_corrected_read_errors_show(struct mddev
*mddev
, char *page
) {
4024 return sprintf(page
, "%d\n",
4025 atomic_read(&mddev
->max_corr_read_errors
));
4029 max_corrected_read_errors_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4034 rv
= kstrtouint(buf
, 10, &n
);
4037 atomic_set(&mddev
->max_corr_read_errors
, n
);
4041 static struct md_sysfs_entry max_corr_read_errors
=
4042 __ATTR(max_read_errors
, S_IRUGO
|S_IWUSR
, max_corrected_read_errors_show
,
4043 max_corrected_read_errors_store
);
4046 null_show(struct mddev
*mddev
, char *page
)
4052 new_dev_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4054 /* buf must be %d:%d\n? giving major and minor numbers */
4055 /* The new device is added to the array.
4056 * If the array has a persistent superblock, we read the
4057 * superblock to initialise info and check validity.
4058 * Otherwise, only checking done is that in bind_rdev_to_array,
4059 * which mainly checks size.
4062 int major
= simple_strtoul(buf
, &e
, 10);
4065 struct md_rdev
*rdev
;
4068 if (!*buf
|| *e
!= ':' || !e
[1] || e
[1] == '\n')
4070 minor
= simple_strtoul(e
+1, &e
, 10);
4071 if (*e
&& *e
!= '\n')
4073 dev
= MKDEV(major
, minor
);
4074 if (major
!= MAJOR(dev
) ||
4075 minor
!= MINOR(dev
))
4078 flush_workqueue(md_misc_wq
);
4080 err
= mddev_lock(mddev
);
4083 if (mddev
->persistent
) {
4084 rdev
= md_import_device(dev
, mddev
->major_version
,
4085 mddev
->minor_version
);
4086 if (!IS_ERR(rdev
) && !list_empty(&mddev
->disks
)) {
4087 struct md_rdev
*rdev0
4088 = list_entry(mddev
->disks
.next
,
4089 struct md_rdev
, same_set
);
4090 err
= super_types
[mddev
->major_version
]
4091 .load_super(rdev
, rdev0
, mddev
->minor_version
);
4095 } else if (mddev
->external
)
4096 rdev
= md_import_device(dev
, -2, -1);
4098 rdev
= md_import_device(dev
, -1, -1);
4101 mddev_unlock(mddev
);
4102 return PTR_ERR(rdev
);
4104 err
= bind_rdev_to_array(rdev
, mddev
);
4108 mddev_unlock(mddev
);
4109 return err
? err
: len
;
4112 static struct md_sysfs_entry md_new_device
=
4113 __ATTR(new_dev
, S_IWUSR
, null_show
, new_dev_store
);
4116 bitmap_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4119 unsigned long chunk
, end_chunk
;
4122 err
= mddev_lock(mddev
);
4127 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
4129 chunk
= end_chunk
= simple_strtoul(buf
, &end
, 0);
4130 if (buf
== end
) break;
4131 if (*end
== '-') { /* range */
4133 end_chunk
= simple_strtoul(buf
, &end
, 0);
4134 if (buf
== end
) break;
4136 if (*end
&& !isspace(*end
)) break;
4137 bitmap_dirty_bits(mddev
->bitmap
, chunk
, end_chunk
);
4138 buf
= skip_spaces(end
);
4140 bitmap_unplug(mddev
->bitmap
); /* flush the bits to disk */
4142 mddev_unlock(mddev
);
4146 static struct md_sysfs_entry md_bitmap
=
4147 __ATTR(bitmap_set_bits
, S_IWUSR
, null_show
, bitmap_store
);
4150 size_show(struct mddev
*mddev
, char *page
)
4152 return sprintf(page
, "%llu\n",
4153 (unsigned long long)mddev
->dev_sectors
/ 2);
4156 static int update_size(struct mddev
*mddev
, sector_t num_sectors
);
4159 size_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4161 /* If array is inactive, we can reduce the component size, but
4162 * not increase it (except from 0).
4163 * If array is active, we can try an on-line resize
4166 int err
= strict_blocks_to_sectors(buf
, §ors
);
4170 err
= mddev_lock(mddev
);
4174 err
= update_size(mddev
, sectors
);
4175 md_update_sb(mddev
, 1);
4177 if (mddev
->dev_sectors
== 0 ||
4178 mddev
->dev_sectors
> sectors
)
4179 mddev
->dev_sectors
= sectors
;
4183 mddev_unlock(mddev
);
4184 return err
? err
: len
;
4187 static struct md_sysfs_entry md_size
=
4188 __ATTR(component_size
, S_IRUGO
|S_IWUSR
, size_show
, size_store
);
4190 /* Metadata version.
4192 * 'none' for arrays with no metadata (good luck...)
4193 * 'external' for arrays with externally managed metadata,
4194 * or N.M for internally known formats
4197 metadata_show(struct mddev
*mddev
, char *page
)
4199 if (mddev
->persistent
)
4200 return sprintf(page
, "%d.%d\n",
4201 mddev
->major_version
, mddev
->minor_version
);
4202 else if (mddev
->external
)
4203 return sprintf(page
, "external:%s\n", mddev
->metadata_type
);
4205 return sprintf(page
, "none\n");
4209 metadata_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4214 /* Changing the details of 'external' metadata is
4215 * always permitted. Otherwise there must be
4216 * no devices attached to the array.
4219 err
= mddev_lock(mddev
);
4223 if (mddev
->external
&& strncmp(buf
, "external:", 9) == 0)
4225 else if (!list_empty(&mddev
->disks
))
4229 if (cmd_match(buf
, "none")) {
4230 mddev
->persistent
= 0;
4231 mddev
->external
= 0;
4232 mddev
->major_version
= 0;
4233 mddev
->minor_version
= 90;
4236 if (strncmp(buf
, "external:", 9) == 0) {
4237 size_t namelen
= len
-9;
4238 if (namelen
>= sizeof(mddev
->metadata_type
))
4239 namelen
= sizeof(mddev
->metadata_type
)-1;
4240 strncpy(mddev
->metadata_type
, buf
+9, namelen
);
4241 mddev
->metadata_type
[namelen
] = 0;
4242 if (namelen
&& mddev
->metadata_type
[namelen
-1] == '\n')
4243 mddev
->metadata_type
[--namelen
] = 0;
4244 mddev
->persistent
= 0;
4245 mddev
->external
= 1;
4246 mddev
->major_version
= 0;
4247 mddev
->minor_version
= 90;
4250 major
= simple_strtoul(buf
, &e
, 10);
4252 if (e
==buf
|| *e
!= '.')
4255 minor
= simple_strtoul(buf
, &e
, 10);
4256 if (e
==buf
|| (*e
&& *e
!= '\n') )
4259 if (major
>= ARRAY_SIZE(super_types
) || super_types
[major
].name
== NULL
)
4261 mddev
->major_version
= major
;
4262 mddev
->minor_version
= minor
;
4263 mddev
->persistent
= 1;
4264 mddev
->external
= 0;
4267 mddev_unlock(mddev
);
4271 static struct md_sysfs_entry md_metadata
=
4272 __ATTR_PREALLOC(metadata_version
, S_IRUGO
|S_IWUSR
, metadata_show
, metadata_store
);
4275 action_show(struct mddev
*mddev
, char *page
)
4277 char *type
= "idle";
4278 unsigned long recovery
= mddev
->recovery
;
4279 if (test_bit(MD_RECOVERY_FROZEN
, &recovery
))
4281 else if (test_bit(MD_RECOVERY_RUNNING
, &recovery
) ||
4282 (!mddev
->ro
&& test_bit(MD_RECOVERY_NEEDED
, &recovery
))) {
4283 if (test_bit(MD_RECOVERY_RESHAPE
, &recovery
))
4285 else if (test_bit(MD_RECOVERY_SYNC
, &recovery
)) {
4286 if (!test_bit(MD_RECOVERY_REQUESTED
, &recovery
))
4288 else if (test_bit(MD_RECOVERY_CHECK
, &recovery
))
4292 } else if (test_bit(MD_RECOVERY_RECOVER
, &recovery
))
4294 else if (mddev
->reshape_position
!= MaxSector
)
4297 return sprintf(page
, "%s\n", type
);
4301 action_store(struct mddev
*mddev
, const char *page
, size_t len
)
4303 if (!mddev
->pers
|| !mddev
->pers
->sync_request
)
4307 if (cmd_match(page
, "idle") || cmd_match(page
, "frozen")) {
4308 if (cmd_match(page
, "frozen"))
4309 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4311 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4312 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) &&
4313 mddev_lock(mddev
) == 0) {
4314 flush_workqueue(md_misc_wq
);
4315 if (mddev
->sync_thread
) {
4316 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
4317 md_reap_sync_thread(mddev
);
4319 mddev_unlock(mddev
);
4321 } else if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
4322 test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))
4324 else if (cmd_match(page
, "resync"))
4325 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4326 else if (cmd_match(page
, "recover")) {
4327 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4328 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
4329 } else if (cmd_match(page
, "reshape")) {
4331 if (mddev
->pers
->start_reshape
== NULL
)
4333 err
= mddev_lock(mddev
);
4335 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4336 err
= mddev
->pers
->start_reshape(mddev
);
4337 mddev_unlock(mddev
);
4341 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
4343 if (cmd_match(page
, "check"))
4344 set_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
4345 else if (!cmd_match(page
, "repair"))
4347 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4348 set_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
4349 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
4351 if (mddev
->ro
== 2) {
4352 /* A write to sync_action is enough to justify
4353 * canceling read-auto mode
4356 md_wakeup_thread(mddev
->sync_thread
);
4358 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4359 md_wakeup_thread(mddev
->thread
);
4360 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
4364 static struct md_sysfs_entry md_scan_mode
=
4365 __ATTR_PREALLOC(sync_action
, S_IRUGO
|S_IWUSR
, action_show
, action_store
);
4368 last_sync_action_show(struct mddev
*mddev
, char *page
)
4370 return sprintf(page
, "%s\n", mddev
->last_sync_action
);
4373 static struct md_sysfs_entry md_last_scan_mode
= __ATTR_RO(last_sync_action
);
4376 mismatch_cnt_show(struct mddev
*mddev
, char *page
)
4378 return sprintf(page
, "%llu\n",
4379 (unsigned long long)
4380 atomic64_read(&mddev
->resync_mismatches
));
4383 static struct md_sysfs_entry md_mismatches
= __ATTR_RO(mismatch_cnt
);
4386 sync_min_show(struct mddev
*mddev
, char *page
)
4388 return sprintf(page
, "%d (%s)\n", speed_min(mddev
),
4389 mddev
->sync_speed_min
? "local": "system");
4393 sync_min_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4398 if (strncmp(buf
, "system", 6)==0) {
4401 rv
= kstrtouint(buf
, 10, &min
);
4407 mddev
->sync_speed_min
= min
;
4411 static struct md_sysfs_entry md_sync_min
=
4412 __ATTR(sync_speed_min
, S_IRUGO
|S_IWUSR
, sync_min_show
, sync_min_store
);
4415 sync_max_show(struct mddev
*mddev
, char *page
)
4417 return sprintf(page
, "%d (%s)\n", speed_max(mddev
),
4418 mddev
->sync_speed_max
? "local": "system");
4422 sync_max_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4427 if (strncmp(buf
, "system", 6)==0) {
4430 rv
= kstrtouint(buf
, 10, &max
);
4436 mddev
->sync_speed_max
= max
;
4440 static struct md_sysfs_entry md_sync_max
=
4441 __ATTR(sync_speed_max
, S_IRUGO
|S_IWUSR
, sync_max_show
, sync_max_store
);
4444 degraded_show(struct mddev
*mddev
, char *page
)
4446 return sprintf(page
, "%d\n", mddev
->degraded
);
4448 static struct md_sysfs_entry md_degraded
= __ATTR_RO(degraded
);
4451 sync_force_parallel_show(struct mddev
*mddev
, char *page
)
4453 return sprintf(page
, "%d\n", mddev
->parallel_resync
);
4457 sync_force_parallel_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4461 if (kstrtol(buf
, 10, &n
))
4464 if (n
!= 0 && n
!= 1)
4467 mddev
->parallel_resync
= n
;
4469 if (mddev
->sync_thread
)
4470 wake_up(&resync_wait
);
4475 /* force parallel resync, even with shared block devices */
4476 static struct md_sysfs_entry md_sync_force_parallel
=
4477 __ATTR(sync_force_parallel
, S_IRUGO
|S_IWUSR
,
4478 sync_force_parallel_show
, sync_force_parallel_store
);
4481 sync_speed_show(struct mddev
*mddev
, char *page
)
4483 unsigned long resync
, dt
, db
;
4484 if (mddev
->curr_resync
== 0)
4485 return sprintf(page
, "none\n");
4486 resync
= mddev
->curr_mark_cnt
- atomic_read(&mddev
->recovery_active
);
4487 dt
= (jiffies
- mddev
->resync_mark
) / HZ
;
4489 db
= resync
- mddev
->resync_mark_cnt
;
4490 return sprintf(page
, "%lu\n", db
/dt
/2); /* K/sec */
4493 static struct md_sysfs_entry md_sync_speed
= __ATTR_RO(sync_speed
);
4496 sync_completed_show(struct mddev
*mddev
, char *page
)
4498 unsigned long long max_sectors
, resync
;
4500 if (!test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
4501 return sprintf(page
, "none\n");
4503 if (mddev
->curr_resync
== 1 ||
4504 mddev
->curr_resync
== 2)
4505 return sprintf(page
, "delayed\n");
4507 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ||
4508 test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
4509 max_sectors
= mddev
->resync_max_sectors
;
4511 max_sectors
= mddev
->dev_sectors
;
4513 resync
= mddev
->curr_resync_completed
;
4514 return sprintf(page
, "%llu / %llu\n", resync
, max_sectors
);
4517 static struct md_sysfs_entry md_sync_completed
=
4518 __ATTR_PREALLOC(sync_completed
, S_IRUGO
, sync_completed_show
, NULL
);
4521 min_sync_show(struct mddev
*mddev
, char *page
)
4523 return sprintf(page
, "%llu\n",
4524 (unsigned long long)mddev
->resync_min
);
4527 min_sync_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4529 unsigned long long min
;
4532 if (kstrtoull(buf
, 10, &min
))
4535 spin_lock(&mddev
->lock
);
4537 if (min
> mddev
->resync_max
)
4541 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
4544 /* Round down to multiple of 4K for safety */
4545 mddev
->resync_min
= round_down(min
, 8);
4549 spin_unlock(&mddev
->lock
);
4553 static struct md_sysfs_entry md_min_sync
=
4554 __ATTR(sync_min
, S_IRUGO
|S_IWUSR
, min_sync_show
, min_sync_store
);
4557 max_sync_show(struct mddev
*mddev
, char *page
)
4559 if (mddev
->resync_max
== MaxSector
)
4560 return sprintf(page
, "max\n");
4562 return sprintf(page
, "%llu\n",
4563 (unsigned long long)mddev
->resync_max
);
4566 max_sync_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4569 spin_lock(&mddev
->lock
);
4570 if (strncmp(buf
, "max", 3) == 0)
4571 mddev
->resync_max
= MaxSector
;
4573 unsigned long long max
;
4577 if (kstrtoull(buf
, 10, &max
))
4579 if (max
< mddev
->resync_min
)
4583 if (max
< mddev
->resync_max
&&
4585 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
4588 /* Must be a multiple of chunk_size */
4589 chunk
= mddev
->chunk_sectors
;
4591 sector_t temp
= max
;
4594 if (sector_div(temp
, chunk
))
4597 mddev
->resync_max
= max
;
4599 wake_up(&mddev
->recovery_wait
);
4602 spin_unlock(&mddev
->lock
);
4606 static struct md_sysfs_entry md_max_sync
=
4607 __ATTR(sync_max
, S_IRUGO
|S_IWUSR
, max_sync_show
, max_sync_store
);
4610 suspend_lo_show(struct mddev
*mddev
, char *page
)
4612 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->suspend_lo
);
4616 suspend_lo_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4618 unsigned long long old
, new;
4621 err
= kstrtoull(buf
, 10, &new);
4624 if (new != (sector_t
)new)
4627 err
= mddev_lock(mddev
);
4631 if (mddev
->pers
== NULL
||
4632 mddev
->pers
->quiesce
== NULL
)
4634 old
= mddev
->suspend_lo
;
4635 mddev
->suspend_lo
= new;
4637 /* Shrinking suspended region */
4638 mddev
->pers
->quiesce(mddev
, 2);
4640 /* Expanding suspended region - need to wait */
4641 mddev
->pers
->quiesce(mddev
, 1);
4642 mddev
->pers
->quiesce(mddev
, 0);
4646 mddev_unlock(mddev
);
4649 static struct md_sysfs_entry md_suspend_lo
=
4650 __ATTR(suspend_lo
, S_IRUGO
|S_IWUSR
, suspend_lo_show
, suspend_lo_store
);
4653 suspend_hi_show(struct mddev
*mddev
, char *page
)
4655 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->suspend_hi
);
4659 suspend_hi_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4661 unsigned long long old
, new;
4664 err
= kstrtoull(buf
, 10, &new);
4667 if (new != (sector_t
)new)
4670 err
= mddev_lock(mddev
);
4674 if (mddev
->pers
== NULL
||
4675 mddev
->pers
->quiesce
== NULL
)
4677 old
= mddev
->suspend_hi
;
4678 mddev
->suspend_hi
= new;
4680 /* Shrinking suspended region */
4681 mddev
->pers
->quiesce(mddev
, 2);
4683 /* Expanding suspended region - need to wait */
4684 mddev
->pers
->quiesce(mddev
, 1);
4685 mddev
->pers
->quiesce(mddev
, 0);
4689 mddev_unlock(mddev
);
4692 static struct md_sysfs_entry md_suspend_hi
=
4693 __ATTR(suspend_hi
, S_IRUGO
|S_IWUSR
, suspend_hi_show
, suspend_hi_store
);
4696 reshape_position_show(struct mddev
*mddev
, char *page
)
4698 if (mddev
->reshape_position
!= MaxSector
)
4699 return sprintf(page
, "%llu\n",
4700 (unsigned long long)mddev
->reshape_position
);
4701 strcpy(page
, "none\n");
4706 reshape_position_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4708 struct md_rdev
*rdev
;
4709 unsigned long long new;
4712 err
= kstrtoull(buf
, 10, &new);
4715 if (new != (sector_t
)new)
4717 err
= mddev_lock(mddev
);
4723 mddev
->reshape_position
= new;
4724 mddev
->delta_disks
= 0;
4725 mddev
->reshape_backwards
= 0;
4726 mddev
->new_level
= mddev
->level
;
4727 mddev
->new_layout
= mddev
->layout
;
4728 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
4729 rdev_for_each(rdev
, mddev
)
4730 rdev
->new_data_offset
= rdev
->data_offset
;
4733 mddev_unlock(mddev
);
4737 static struct md_sysfs_entry md_reshape_position
=
4738 __ATTR(reshape_position
, S_IRUGO
|S_IWUSR
, reshape_position_show
,
4739 reshape_position_store
);
4742 reshape_direction_show(struct mddev
*mddev
, char *page
)
4744 return sprintf(page
, "%s\n",
4745 mddev
->reshape_backwards
? "backwards" : "forwards");
4749 reshape_direction_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4754 if (cmd_match(buf
, "forwards"))
4756 else if (cmd_match(buf
, "backwards"))
4760 if (mddev
->reshape_backwards
== backwards
)
4763 err
= mddev_lock(mddev
);
4766 /* check if we are allowed to change */
4767 if (mddev
->delta_disks
)
4769 else if (mddev
->persistent
&&
4770 mddev
->major_version
== 0)
4773 mddev
->reshape_backwards
= backwards
;
4774 mddev_unlock(mddev
);
4778 static struct md_sysfs_entry md_reshape_direction
=
4779 __ATTR(reshape_direction
, S_IRUGO
|S_IWUSR
, reshape_direction_show
,
4780 reshape_direction_store
);
4783 array_size_show(struct mddev
*mddev
, char *page
)
4785 if (mddev
->external_size
)
4786 return sprintf(page
, "%llu\n",
4787 (unsigned long long)mddev
->array_sectors
/2);
4789 return sprintf(page
, "default\n");
4793 array_size_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4798 err
= mddev_lock(mddev
);
4802 if (strncmp(buf
, "default", 7) == 0) {
4804 sectors
= mddev
->pers
->size(mddev
, 0, 0);
4806 sectors
= mddev
->array_sectors
;
4808 mddev
->external_size
= 0;
4810 if (strict_blocks_to_sectors(buf
, §ors
) < 0)
4812 else if (mddev
->pers
&& mddev
->pers
->size(mddev
, 0, 0) < sectors
)
4815 mddev
->external_size
= 1;
4819 mddev
->array_sectors
= sectors
;
4821 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
4822 revalidate_disk(mddev
->gendisk
);
4825 mddev_unlock(mddev
);
4829 static struct md_sysfs_entry md_array_size
=
4830 __ATTR(array_size
, S_IRUGO
|S_IWUSR
, array_size_show
,
4833 static struct attribute
*md_default_attrs
[] = {
4836 &md_raid_disks
.attr
,
4837 &md_chunk_size
.attr
,
4839 &md_resync_start
.attr
,
4841 &md_new_device
.attr
,
4842 &md_safe_delay
.attr
,
4843 &md_array_state
.attr
,
4844 &md_reshape_position
.attr
,
4845 &md_reshape_direction
.attr
,
4846 &md_array_size
.attr
,
4847 &max_corr_read_errors
.attr
,
4851 static struct attribute
*md_redundancy_attrs
[] = {
4853 &md_last_scan_mode
.attr
,
4854 &md_mismatches
.attr
,
4857 &md_sync_speed
.attr
,
4858 &md_sync_force_parallel
.attr
,
4859 &md_sync_completed
.attr
,
4862 &md_suspend_lo
.attr
,
4863 &md_suspend_hi
.attr
,
4868 static struct attribute_group md_redundancy_group
= {
4870 .attrs
= md_redundancy_attrs
,
4874 md_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
4876 struct md_sysfs_entry
*entry
= container_of(attr
, struct md_sysfs_entry
, attr
);
4877 struct mddev
*mddev
= container_of(kobj
, struct mddev
, kobj
);
4882 spin_lock(&all_mddevs_lock
);
4883 if (list_empty(&mddev
->all_mddevs
)) {
4884 spin_unlock(&all_mddevs_lock
);
4888 spin_unlock(&all_mddevs_lock
);
4890 rv
= entry
->show(mddev
, page
);
4896 md_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
4897 const char *page
, size_t length
)
4899 struct md_sysfs_entry
*entry
= container_of(attr
, struct md_sysfs_entry
, attr
);
4900 struct mddev
*mddev
= container_of(kobj
, struct mddev
, kobj
);
4905 if (!capable(CAP_SYS_ADMIN
))
4907 spin_lock(&all_mddevs_lock
);
4908 if (list_empty(&mddev
->all_mddevs
)) {
4909 spin_unlock(&all_mddevs_lock
);
4913 spin_unlock(&all_mddevs_lock
);
4914 rv
= entry
->store(mddev
, page
, length
);
4919 static void md_free(struct kobject
*ko
)
4921 struct mddev
*mddev
= container_of(ko
, struct mddev
, kobj
);
4923 if (mddev
->sysfs_state
)
4924 sysfs_put(mddev
->sysfs_state
);
4927 blk_cleanup_queue(mddev
->queue
);
4928 if (mddev
->gendisk
) {
4929 del_gendisk(mddev
->gendisk
);
4930 put_disk(mddev
->gendisk
);
4936 static const struct sysfs_ops md_sysfs_ops
= {
4937 .show
= md_attr_show
,
4938 .store
= md_attr_store
,
4940 static struct kobj_type md_ktype
= {
4942 .sysfs_ops
= &md_sysfs_ops
,
4943 .default_attrs
= md_default_attrs
,
4948 static void mddev_delayed_delete(struct work_struct
*ws
)
4950 struct mddev
*mddev
= container_of(ws
, struct mddev
, del_work
);
4952 sysfs_remove_group(&mddev
->kobj
, &md_bitmap_group
);
4953 kobject_del(&mddev
->kobj
);
4954 kobject_put(&mddev
->kobj
);
4957 static int md_alloc(dev_t dev
, char *name
)
4959 static DEFINE_MUTEX(disks_mutex
);
4960 struct mddev
*mddev
= mddev_find(dev
);
4961 struct gendisk
*disk
;
4970 partitioned
= (MAJOR(mddev
->unit
) != MD_MAJOR
);
4971 shift
= partitioned
? MdpMinorShift
: 0;
4972 unit
= MINOR(mddev
->unit
) >> shift
;
4974 /* wait for any previous instance of this device to be
4975 * completely removed (mddev_delayed_delete).
4977 flush_workqueue(md_misc_wq
);
4979 mutex_lock(&disks_mutex
);
4985 /* Need to ensure that 'name' is not a duplicate.
4987 struct mddev
*mddev2
;
4988 spin_lock(&all_mddevs_lock
);
4990 list_for_each_entry(mddev2
, &all_mddevs
, all_mddevs
)
4991 if (mddev2
->gendisk
&&
4992 strcmp(mddev2
->gendisk
->disk_name
, name
) == 0) {
4993 spin_unlock(&all_mddevs_lock
);
4996 spin_unlock(&all_mddevs_lock
);
5000 mddev
->queue
= blk_alloc_queue(GFP_KERNEL
);
5003 mddev
->queue
->queuedata
= mddev
;
5005 blk_queue_make_request(mddev
->queue
, md_make_request
);
5006 blk_set_stacking_limits(&mddev
->queue
->limits
);
5008 disk
= alloc_disk(1 << shift
);
5010 blk_cleanup_queue(mddev
->queue
);
5011 mddev
->queue
= NULL
;
5014 disk
->major
= MAJOR(mddev
->unit
);
5015 disk
->first_minor
= unit
<< shift
;
5017 strcpy(disk
->disk_name
, name
);
5018 else if (partitioned
)
5019 sprintf(disk
->disk_name
, "md_d%d", unit
);
5021 sprintf(disk
->disk_name
, "md%d", unit
);
5022 disk
->fops
= &md_fops
;
5023 disk
->private_data
= mddev
;
5024 disk
->queue
= mddev
->queue
;
5025 blk_queue_flush(mddev
->queue
, REQ_FLUSH
| REQ_FUA
);
5026 /* Allow extended partitions. This makes the
5027 * 'mdp' device redundant, but we can't really
5030 disk
->flags
|= GENHD_FL_EXT_DEVT
;
5031 mddev
->gendisk
= disk
;
5032 /* As soon as we call add_disk(), another thread could get
5033 * through to md_open, so make sure it doesn't get too far
5035 mutex_lock(&mddev
->open_mutex
);
5038 error
= kobject_init_and_add(&mddev
->kobj
, &md_ktype
,
5039 &disk_to_dev(disk
)->kobj
, "%s", "md");
5041 /* This isn't possible, but as kobject_init_and_add is marked
5042 * __must_check, we must do something with the result
5044 printk(KERN_WARNING
"md: cannot register %s/md - name in use\n",
5048 if (mddev
->kobj
.sd
&&
5049 sysfs_create_group(&mddev
->kobj
, &md_bitmap_group
))
5050 printk(KERN_DEBUG
"pointless warning\n");
5051 mutex_unlock(&mddev
->open_mutex
);
5053 mutex_unlock(&disks_mutex
);
5054 if (!error
&& mddev
->kobj
.sd
) {
5055 kobject_uevent(&mddev
->kobj
, KOBJ_ADD
);
5056 mddev
->sysfs_state
= sysfs_get_dirent_safe(mddev
->kobj
.sd
, "array_state");
5062 static struct kobject
*md_probe(dev_t dev
, int *part
, void *data
)
5064 md_alloc(dev
, NULL
);
5068 static int add_named_array(const char *val
, struct kernel_param
*kp
)
5070 /* val must be "md_*" where * is not all digits.
5071 * We allocate an array with a large free minor number, and
5072 * set the name to val. val must not already be an active name.
5074 int len
= strlen(val
);
5075 char buf
[DISK_NAME_LEN
];
5077 while (len
&& val
[len
-1] == '\n')
5079 if (len
>= DISK_NAME_LEN
)
5081 strlcpy(buf
, val
, len
+1);
5082 if (strncmp(buf
, "md_", 3) != 0)
5084 return md_alloc(0, buf
);
5087 static void md_safemode_timeout(unsigned long data
)
5089 struct mddev
*mddev
= (struct mddev
*) data
;
5091 if (!atomic_read(&mddev
->writes_pending
)) {
5092 mddev
->safemode
= 1;
5093 if (mddev
->external
)
5094 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
5096 md_wakeup_thread(mddev
->thread
);
5099 static int start_dirty_degraded
;
5101 int md_run(struct mddev
*mddev
)
5104 struct md_rdev
*rdev
;
5105 struct md_personality
*pers
;
5107 if (list_empty(&mddev
->disks
))
5108 /* cannot run an array with no devices.. */
5113 /* Cannot run until previous stop completes properly */
5114 if (mddev
->sysfs_active
)
5118 * Analyze all RAID superblock(s)
5120 if (!mddev
->raid_disks
) {
5121 if (!mddev
->persistent
)
5126 if (mddev
->level
!= LEVEL_NONE
)
5127 request_module("md-level-%d", mddev
->level
);
5128 else if (mddev
->clevel
[0])
5129 request_module("md-%s", mddev
->clevel
);
5132 * Drop all container device buffers, from now on
5133 * the only valid external interface is through the md
5136 rdev_for_each(rdev
, mddev
) {
5137 if (test_bit(Faulty
, &rdev
->flags
))
5139 sync_blockdev(rdev
->bdev
);
5140 invalidate_bdev(rdev
->bdev
);
5142 /* perform some consistency tests on the device.
5143 * We don't want the data to overlap the metadata,
5144 * Internal Bitmap issues have been handled elsewhere.
5146 if (rdev
->meta_bdev
) {
5147 /* Nothing to check */;
5148 } else if (rdev
->data_offset
< rdev
->sb_start
) {
5149 if (mddev
->dev_sectors
&&
5150 rdev
->data_offset
+ mddev
->dev_sectors
5152 printk("md: %s: data overlaps metadata\n",
5157 if (rdev
->sb_start
+ rdev
->sb_size
/512
5158 > rdev
->data_offset
) {
5159 printk("md: %s: metadata overlaps data\n",
5164 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
5167 if (mddev
->bio_set
== NULL
)
5168 mddev
->bio_set
= bioset_create(BIO_POOL_SIZE
, 0);
5170 spin_lock(&pers_lock
);
5171 pers
= find_pers(mddev
->level
, mddev
->clevel
);
5172 if (!pers
|| !try_module_get(pers
->owner
)) {
5173 spin_unlock(&pers_lock
);
5174 if (mddev
->level
!= LEVEL_NONE
)
5175 printk(KERN_WARNING
"md: personality for level %d is not loaded!\n",
5178 printk(KERN_WARNING
"md: personality for level %s is not loaded!\n",
5182 spin_unlock(&pers_lock
);
5183 if (mddev
->level
!= pers
->level
) {
5184 mddev
->level
= pers
->level
;
5185 mddev
->new_level
= pers
->level
;
5187 strlcpy(mddev
->clevel
, pers
->name
, sizeof(mddev
->clevel
));
5189 if (mddev
->reshape_position
!= MaxSector
&&
5190 pers
->start_reshape
== NULL
) {
5191 /* This personality cannot handle reshaping... */
5192 module_put(pers
->owner
);
5196 if (pers
->sync_request
) {
5197 /* Warn if this is a potentially silly
5200 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
5201 struct md_rdev
*rdev2
;
5204 rdev_for_each(rdev
, mddev
)
5205 rdev_for_each(rdev2
, mddev
) {
5207 rdev
->bdev
->bd_contains
==
5208 rdev2
->bdev
->bd_contains
) {
5210 "%s: WARNING: %s appears to be"
5211 " on the same physical disk as"
5214 bdevname(rdev
->bdev
,b
),
5215 bdevname(rdev2
->bdev
,b2
));
5222 "True protection against single-disk"
5223 " failure might be compromised.\n");
5226 mddev
->recovery
= 0;
5227 /* may be over-ridden by personality */
5228 mddev
->resync_max_sectors
= mddev
->dev_sectors
;
5230 mddev
->ok_start_degraded
= start_dirty_degraded
;
5232 if (start_readonly
&& mddev
->ro
== 0)
5233 mddev
->ro
= 2; /* read-only, but switch on first write */
5235 err
= pers
->run(mddev
);
5237 printk(KERN_ERR
"md: pers->run() failed ...\n");
5238 else if (pers
->size(mddev
, 0, 0) < mddev
->array_sectors
) {
5239 WARN_ONCE(!mddev
->external_size
, "%s: default size too small,"
5240 " but 'external_size' not in effect?\n", __func__
);
5242 "md: invalid array_size %llu > default size %llu\n",
5243 (unsigned long long)mddev
->array_sectors
/ 2,
5244 (unsigned long long)pers
->size(mddev
, 0, 0) / 2);
5247 if (err
== 0 && pers
->sync_request
&&
5248 (mddev
->bitmap_info
.file
|| mddev
->bitmap_info
.offset
)) {
5249 struct bitmap
*bitmap
;
5251 bitmap
= bitmap_create(mddev
, -1);
5252 if (IS_ERR(bitmap
)) {
5253 err
= PTR_ERR(bitmap
);
5254 printk(KERN_ERR
"%s: failed to create bitmap (%d)\n",
5255 mdname(mddev
), err
);
5257 mddev
->bitmap
= bitmap
;
5261 mddev_detach(mddev
);
5263 pers
->free(mddev
, mddev
->private);
5264 mddev
->private = NULL
;
5265 module_put(pers
->owner
);
5266 bitmap_destroy(mddev
);
5270 mddev
->queue
->backing_dev_info
.congested_data
= mddev
;
5271 mddev
->queue
->backing_dev_info
.congested_fn
= md_congested
;
5273 if (pers
->sync_request
) {
5274 if (mddev
->kobj
.sd
&&
5275 sysfs_create_group(&mddev
->kobj
, &md_redundancy_group
))
5277 "md: cannot register extra attributes for %s\n",
5279 mddev
->sysfs_action
= sysfs_get_dirent_safe(mddev
->kobj
.sd
, "sync_action");
5280 } else if (mddev
->ro
== 2) /* auto-readonly not meaningful */
5283 atomic_set(&mddev
->writes_pending
,0);
5284 atomic_set(&mddev
->max_corr_read_errors
,
5285 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS
);
5286 mddev
->safemode
= 0;
5287 if (mddev_is_clustered(mddev
))
5288 mddev
->safemode_delay
= 0;
5290 mddev
->safemode_delay
= (200 * HZ
)/1000 +1; /* 200 msec delay */
5293 spin_lock(&mddev
->lock
);
5296 spin_unlock(&mddev
->lock
);
5297 rdev_for_each(rdev
, mddev
)
5298 if (rdev
->raid_disk
>= 0)
5299 if (sysfs_link_rdev(mddev
, rdev
))
5300 /* failure here is OK */;
5302 if (mddev
->degraded
&& !mddev
->ro
)
5303 /* This ensures that recovering status is reported immediately
5304 * via sysfs - until a lack of spares is confirmed.
5306 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
5307 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5309 if (mddev
->flags
& MD_UPDATE_SB_FLAGS
)
5310 md_update_sb(mddev
, 0);
5312 md_new_event(mddev
);
5313 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
5314 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
5315 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
5318 EXPORT_SYMBOL_GPL(md_run
);
5320 static int do_md_run(struct mddev
*mddev
)
5324 err
= md_run(mddev
);
5327 err
= bitmap_load(mddev
);
5329 bitmap_destroy(mddev
);
5333 if (mddev_is_clustered(mddev
))
5334 md_allow_write(mddev
);
5336 md_wakeup_thread(mddev
->thread
);
5337 md_wakeup_thread(mddev
->sync_thread
); /* possibly kick off a reshape */
5339 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
5340 revalidate_disk(mddev
->gendisk
);
5342 kobject_uevent(&disk_to_dev(mddev
->gendisk
)->kobj
, KOBJ_CHANGE
);
5347 static int restart_array(struct mddev
*mddev
)
5349 struct gendisk
*disk
= mddev
->gendisk
;
5351 /* Complain if it has no devices */
5352 if (list_empty(&mddev
->disks
))
5358 if (test_bit(MD_HAS_JOURNAL
, &mddev
->flags
)) {
5359 struct md_rdev
*rdev
;
5360 bool has_journal
= false;
5363 rdev_for_each_rcu(rdev
, mddev
) {
5364 if (test_bit(Journal
, &rdev
->flags
) &&
5365 !test_bit(Faulty
, &rdev
->flags
)) {
5372 /* Don't restart rw with journal missing/faulty */
5377 mddev
->safemode
= 0;
5379 set_disk_ro(disk
, 0);
5380 printk(KERN_INFO
"md: %s switched to read-write mode.\n",
5382 /* Kick recovery or resync if necessary */
5383 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5384 md_wakeup_thread(mddev
->thread
);
5385 md_wakeup_thread(mddev
->sync_thread
);
5386 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
5390 static void md_clean(struct mddev
*mddev
)
5392 mddev
->array_sectors
= 0;
5393 mddev
->external_size
= 0;
5394 mddev
->dev_sectors
= 0;
5395 mddev
->raid_disks
= 0;
5396 mddev
->recovery_cp
= 0;
5397 mddev
->resync_min
= 0;
5398 mddev
->resync_max
= MaxSector
;
5399 mddev
->reshape_position
= MaxSector
;
5400 mddev
->external
= 0;
5401 mddev
->persistent
= 0;
5402 mddev
->level
= LEVEL_NONE
;
5403 mddev
->clevel
[0] = 0;
5406 mddev
->metadata_type
[0] = 0;
5407 mddev
->chunk_sectors
= 0;
5408 mddev
->ctime
= mddev
->utime
= 0;
5410 mddev
->max_disks
= 0;
5412 mddev
->can_decrease_events
= 0;
5413 mddev
->delta_disks
= 0;
5414 mddev
->reshape_backwards
= 0;
5415 mddev
->new_level
= LEVEL_NONE
;
5416 mddev
->new_layout
= 0;
5417 mddev
->new_chunk_sectors
= 0;
5418 mddev
->curr_resync
= 0;
5419 atomic64_set(&mddev
->resync_mismatches
, 0);
5420 mddev
->suspend_lo
= mddev
->suspend_hi
= 0;
5421 mddev
->sync_speed_min
= mddev
->sync_speed_max
= 0;
5422 mddev
->recovery
= 0;
5425 mddev
->degraded
= 0;
5426 mddev
->safemode
= 0;
5427 mddev
->private = NULL
;
5428 mddev
->bitmap_info
.offset
= 0;
5429 mddev
->bitmap_info
.default_offset
= 0;
5430 mddev
->bitmap_info
.default_space
= 0;
5431 mddev
->bitmap_info
.chunksize
= 0;
5432 mddev
->bitmap_info
.daemon_sleep
= 0;
5433 mddev
->bitmap_info
.max_write_behind
= 0;
5436 static void __md_stop_writes(struct mddev
*mddev
)
5438 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
5439 flush_workqueue(md_misc_wq
);
5440 if (mddev
->sync_thread
) {
5441 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
5442 md_reap_sync_thread(mddev
);
5445 del_timer_sync(&mddev
->safemode_timer
);
5447 bitmap_flush(mddev
);
5448 md_super_wait(mddev
);
5450 if (mddev
->ro
== 0 &&
5451 ((!mddev
->in_sync
&& !mddev_is_clustered(mddev
)) ||
5452 (mddev
->flags
& MD_UPDATE_SB_FLAGS
))) {
5453 /* mark array as shutdown cleanly */
5454 if (!mddev_is_clustered(mddev
))
5456 md_update_sb(mddev
, 1);
5460 void md_stop_writes(struct mddev
*mddev
)
5462 mddev_lock_nointr(mddev
);
5463 __md_stop_writes(mddev
);
5464 mddev_unlock(mddev
);
5466 EXPORT_SYMBOL_GPL(md_stop_writes
);
5468 static void mddev_detach(struct mddev
*mddev
)
5470 struct bitmap
*bitmap
= mddev
->bitmap
;
5471 /* wait for behind writes to complete */
5472 if (bitmap
&& atomic_read(&bitmap
->behind_writes
) > 0) {
5473 printk(KERN_INFO
"md:%s: behind writes in progress - waiting to stop.\n",
5475 /* need to kick something here to make sure I/O goes? */
5476 wait_event(bitmap
->behind_wait
,
5477 atomic_read(&bitmap
->behind_writes
) == 0);
5479 if (mddev
->pers
&& mddev
->pers
->quiesce
) {
5480 mddev
->pers
->quiesce(mddev
, 1);
5481 mddev
->pers
->quiesce(mddev
, 0);
5483 md_unregister_thread(&mddev
->thread
);
5485 blk_sync_queue(mddev
->queue
); /* the unplug fn references 'conf'*/
5488 static void __md_stop(struct mddev
*mddev
)
5490 struct md_personality
*pers
= mddev
->pers
;
5491 mddev_detach(mddev
);
5492 /* Ensure ->event_work is done */
5493 flush_workqueue(md_misc_wq
);
5494 spin_lock(&mddev
->lock
);
5497 spin_unlock(&mddev
->lock
);
5498 pers
->free(mddev
, mddev
->private);
5499 mddev
->private = NULL
;
5500 if (pers
->sync_request
&& mddev
->to_remove
== NULL
)
5501 mddev
->to_remove
= &md_redundancy_group
;
5502 module_put(pers
->owner
);
5503 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
5506 void md_stop(struct mddev
*mddev
)
5508 /* stop the array and free an attached data structures.
5509 * This is called from dm-raid
5512 bitmap_destroy(mddev
);
5514 bioset_free(mddev
->bio_set
);
5517 EXPORT_SYMBOL_GPL(md_stop
);
5519 static int md_set_readonly(struct mddev
*mddev
, struct block_device
*bdev
)
5524 if (!test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
)) {
5526 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
5527 md_wakeup_thread(mddev
->thread
);
5529 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
5530 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
5531 if (mddev
->sync_thread
)
5532 /* Thread might be blocked waiting for metadata update
5533 * which will now never happen */
5534 wake_up_process(mddev
->sync_thread
->tsk
);
5536 if (mddev
->external
&& test_bit(MD_CHANGE_PENDING
, &mddev
->flags
))
5538 mddev_unlock(mddev
);
5539 wait_event(resync_wait
, !test_bit(MD_RECOVERY_RUNNING
,
5541 wait_event(mddev
->sb_wait
,
5542 !test_bit(MD_CHANGE_PENDING
, &mddev
->flags
));
5543 mddev_lock_nointr(mddev
);
5545 mutex_lock(&mddev
->open_mutex
);
5546 if ((mddev
->pers
&& atomic_read(&mddev
->openers
) > !!bdev
) ||
5547 mddev
->sync_thread
||
5548 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
5549 (bdev
&& !test_bit(MD_STILL_CLOSED
, &mddev
->flags
))) {
5550 printk("md: %s still in use.\n",mdname(mddev
));
5552 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
5553 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5554 md_wakeup_thread(mddev
->thread
);
5560 __md_stop_writes(mddev
);
5566 set_disk_ro(mddev
->gendisk
, 1);
5567 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
5568 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5569 md_wakeup_thread(mddev
->thread
);
5570 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
5574 mutex_unlock(&mddev
->open_mutex
);
5579 * 0 - completely stop and dis-assemble array
5580 * 2 - stop but do not disassemble array
5582 static int do_md_stop(struct mddev
*mddev
, int mode
,
5583 struct block_device
*bdev
)
5585 struct gendisk
*disk
= mddev
->gendisk
;
5586 struct md_rdev
*rdev
;
5589 if (!test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
)) {
5591 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
5592 md_wakeup_thread(mddev
->thread
);
5594 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
5595 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
5596 if (mddev
->sync_thread
)
5597 /* Thread might be blocked waiting for metadata update
5598 * which will now never happen */
5599 wake_up_process(mddev
->sync_thread
->tsk
);
5601 mddev_unlock(mddev
);
5602 wait_event(resync_wait
, (mddev
->sync_thread
== NULL
&&
5603 !test_bit(MD_RECOVERY_RUNNING
,
5604 &mddev
->recovery
)));
5605 mddev_lock_nointr(mddev
);
5607 mutex_lock(&mddev
->open_mutex
);
5608 if ((mddev
->pers
&& atomic_read(&mddev
->openers
) > !!bdev
) ||
5609 mddev
->sysfs_active
||
5610 mddev
->sync_thread
||
5611 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
5612 (bdev
&& !test_bit(MD_STILL_CLOSED
, &mddev
->flags
))) {
5613 printk("md: %s still in use.\n",mdname(mddev
));
5614 mutex_unlock(&mddev
->open_mutex
);
5616 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
5617 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5618 md_wakeup_thread(mddev
->thread
);
5624 set_disk_ro(disk
, 0);
5626 __md_stop_writes(mddev
);
5628 mddev
->queue
->backing_dev_info
.congested_fn
= NULL
;
5630 /* tell userspace to handle 'inactive' */
5631 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
5633 rdev_for_each(rdev
, mddev
)
5634 if (rdev
->raid_disk
>= 0)
5635 sysfs_unlink_rdev(mddev
, rdev
);
5637 set_capacity(disk
, 0);
5638 mutex_unlock(&mddev
->open_mutex
);
5640 revalidate_disk(disk
);
5645 mutex_unlock(&mddev
->open_mutex
);
5647 * Free resources if final stop
5650 printk(KERN_INFO
"md: %s stopped.\n", mdname(mddev
));
5652 bitmap_destroy(mddev
);
5653 if (mddev
->bitmap_info
.file
) {
5654 struct file
*f
= mddev
->bitmap_info
.file
;
5655 spin_lock(&mddev
->lock
);
5656 mddev
->bitmap_info
.file
= NULL
;
5657 spin_unlock(&mddev
->lock
);
5660 mddev
->bitmap_info
.offset
= 0;
5662 export_array(mddev
);
5665 kobject_uevent(&disk_to_dev(mddev
->gendisk
)->kobj
, KOBJ_CHANGE
);
5666 if (mddev
->hold_active
== UNTIL_STOP
)
5667 mddev
->hold_active
= 0;
5669 md_new_event(mddev
);
5670 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
5675 static void autorun_array(struct mddev
*mddev
)
5677 struct md_rdev
*rdev
;
5680 if (list_empty(&mddev
->disks
))
5683 printk(KERN_INFO
"md: running: ");
5685 rdev_for_each(rdev
, mddev
) {
5686 char b
[BDEVNAME_SIZE
];
5687 printk("<%s>", bdevname(rdev
->bdev
,b
));
5691 err
= do_md_run(mddev
);
5693 printk(KERN_WARNING
"md: do_md_run() returned %d\n", err
);
5694 do_md_stop(mddev
, 0, NULL
);
5699 * lets try to run arrays based on all disks that have arrived
5700 * until now. (those are in pending_raid_disks)
5702 * the method: pick the first pending disk, collect all disks with
5703 * the same UUID, remove all from the pending list and put them into
5704 * the 'same_array' list. Then order this list based on superblock
5705 * update time (freshest comes first), kick out 'old' disks and
5706 * compare superblocks. If everything's fine then run it.
5708 * If "unit" is allocated, then bump its reference count
5710 static void autorun_devices(int part
)
5712 struct md_rdev
*rdev0
, *rdev
, *tmp
;
5713 struct mddev
*mddev
;
5714 char b
[BDEVNAME_SIZE
];
5716 printk(KERN_INFO
"md: autorun ...\n");
5717 while (!list_empty(&pending_raid_disks
)) {
5720 LIST_HEAD(candidates
);
5721 rdev0
= list_entry(pending_raid_disks
.next
,
5722 struct md_rdev
, same_set
);
5724 printk(KERN_INFO
"md: considering %s ...\n",
5725 bdevname(rdev0
->bdev
,b
));
5726 INIT_LIST_HEAD(&candidates
);
5727 rdev_for_each_list(rdev
, tmp
, &pending_raid_disks
)
5728 if (super_90_load(rdev
, rdev0
, 0) >= 0) {
5729 printk(KERN_INFO
"md: adding %s ...\n",
5730 bdevname(rdev
->bdev
,b
));
5731 list_move(&rdev
->same_set
, &candidates
);
5734 * now we have a set of devices, with all of them having
5735 * mostly sane superblocks. It's time to allocate the
5739 dev
= MKDEV(mdp_major
,
5740 rdev0
->preferred_minor
<< MdpMinorShift
);
5741 unit
= MINOR(dev
) >> MdpMinorShift
;
5743 dev
= MKDEV(MD_MAJOR
, rdev0
->preferred_minor
);
5746 if (rdev0
->preferred_minor
!= unit
) {
5747 printk(KERN_INFO
"md: unit number in %s is bad: %d\n",
5748 bdevname(rdev0
->bdev
, b
), rdev0
->preferred_minor
);
5752 md_probe(dev
, NULL
, NULL
);
5753 mddev
= mddev_find(dev
);
5754 if (!mddev
|| !mddev
->gendisk
) {
5758 "md: cannot allocate memory for md drive.\n");
5761 if (mddev_lock(mddev
))
5762 printk(KERN_WARNING
"md: %s locked, cannot run\n",
5764 else if (mddev
->raid_disks
|| mddev
->major_version
5765 || !list_empty(&mddev
->disks
)) {
5767 "md: %s already running, cannot run %s\n",
5768 mdname(mddev
), bdevname(rdev0
->bdev
,b
));
5769 mddev_unlock(mddev
);
5771 printk(KERN_INFO
"md: created %s\n", mdname(mddev
));
5772 mddev
->persistent
= 1;
5773 rdev_for_each_list(rdev
, tmp
, &candidates
) {
5774 list_del_init(&rdev
->same_set
);
5775 if (bind_rdev_to_array(rdev
, mddev
))
5778 autorun_array(mddev
);
5779 mddev_unlock(mddev
);
5781 /* on success, candidates will be empty, on error
5784 rdev_for_each_list(rdev
, tmp
, &candidates
) {
5785 list_del_init(&rdev
->same_set
);
5790 printk(KERN_INFO
"md: ... autorun DONE.\n");
5792 #endif /* !MODULE */
5794 static int get_version(void __user
*arg
)
5798 ver
.major
= MD_MAJOR_VERSION
;
5799 ver
.minor
= MD_MINOR_VERSION
;
5800 ver
.patchlevel
= MD_PATCHLEVEL_VERSION
;
5802 if (copy_to_user(arg
, &ver
, sizeof(ver
)))
5808 static int get_array_info(struct mddev
*mddev
, void __user
*arg
)
5810 mdu_array_info_t info
;
5811 int nr
,working
,insync
,failed
,spare
;
5812 struct md_rdev
*rdev
;
5814 nr
= working
= insync
= failed
= spare
= 0;
5816 rdev_for_each_rcu(rdev
, mddev
) {
5818 if (test_bit(Faulty
, &rdev
->flags
))
5822 if (test_bit(In_sync
, &rdev
->flags
))
5830 info
.major_version
= mddev
->major_version
;
5831 info
.minor_version
= mddev
->minor_version
;
5832 info
.patch_version
= MD_PATCHLEVEL_VERSION
;
5833 info
.ctime
= mddev
->ctime
;
5834 info
.level
= mddev
->level
;
5835 info
.size
= mddev
->dev_sectors
/ 2;
5836 if (info
.size
!= mddev
->dev_sectors
/ 2) /* overflow */
5839 info
.raid_disks
= mddev
->raid_disks
;
5840 info
.md_minor
= mddev
->md_minor
;
5841 info
.not_persistent
= !mddev
->persistent
;
5843 info
.utime
= mddev
->utime
;
5846 info
.state
= (1<<MD_SB_CLEAN
);
5847 if (mddev
->bitmap
&& mddev
->bitmap_info
.offset
)
5848 info
.state
|= (1<<MD_SB_BITMAP_PRESENT
);
5849 if (mddev_is_clustered(mddev
))
5850 info
.state
|= (1<<MD_SB_CLUSTERED
);
5851 info
.active_disks
= insync
;
5852 info
.working_disks
= working
;
5853 info
.failed_disks
= failed
;
5854 info
.spare_disks
= spare
;
5856 info
.layout
= mddev
->layout
;
5857 info
.chunk_size
= mddev
->chunk_sectors
<< 9;
5859 if (copy_to_user(arg
, &info
, sizeof(info
)))
5865 static int get_bitmap_file(struct mddev
*mddev
, void __user
* arg
)
5867 mdu_bitmap_file_t
*file
= NULL
; /* too big for stack allocation */
5871 file
= kzalloc(sizeof(*file
), GFP_NOIO
);
5876 spin_lock(&mddev
->lock
);
5877 /* bitmap enabled */
5878 if (mddev
->bitmap_info
.file
) {
5879 ptr
= file_path(mddev
->bitmap_info
.file
, file
->pathname
,
5880 sizeof(file
->pathname
));
5884 memmove(file
->pathname
, ptr
,
5885 sizeof(file
->pathname
)-(ptr
-file
->pathname
));
5887 spin_unlock(&mddev
->lock
);
5890 copy_to_user(arg
, file
, sizeof(*file
)))
5897 static int get_disk_info(struct mddev
*mddev
, void __user
* arg
)
5899 mdu_disk_info_t info
;
5900 struct md_rdev
*rdev
;
5902 if (copy_from_user(&info
, arg
, sizeof(info
)))
5906 rdev
= md_find_rdev_nr_rcu(mddev
, info
.number
);
5908 info
.major
= MAJOR(rdev
->bdev
->bd_dev
);
5909 info
.minor
= MINOR(rdev
->bdev
->bd_dev
);
5910 info
.raid_disk
= rdev
->raid_disk
;
5912 if (test_bit(Faulty
, &rdev
->flags
))
5913 info
.state
|= (1<<MD_DISK_FAULTY
);
5914 else if (test_bit(In_sync
, &rdev
->flags
)) {
5915 info
.state
|= (1<<MD_DISK_ACTIVE
);
5916 info
.state
|= (1<<MD_DISK_SYNC
);
5918 if (test_bit(Journal
, &rdev
->flags
))
5919 info
.state
|= (1<<MD_DISK_JOURNAL
);
5920 if (test_bit(WriteMostly
, &rdev
->flags
))
5921 info
.state
|= (1<<MD_DISK_WRITEMOSTLY
);
5923 info
.major
= info
.minor
= 0;
5924 info
.raid_disk
= -1;
5925 info
.state
= (1<<MD_DISK_REMOVED
);
5929 if (copy_to_user(arg
, &info
, sizeof(info
)))
5935 static int add_new_disk(struct mddev
*mddev
, mdu_disk_info_t
*info
)
5937 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
5938 struct md_rdev
*rdev
;
5939 dev_t dev
= MKDEV(info
->major
,info
->minor
);
5941 if (mddev_is_clustered(mddev
) &&
5942 !(info
->state
& ((1 << MD_DISK_CLUSTER_ADD
) | (1 << MD_DISK_CANDIDATE
)))) {
5943 pr_err("%s: Cannot add to clustered mddev.\n",
5948 if (info
->major
!= MAJOR(dev
) || info
->minor
!= MINOR(dev
))
5951 if (!mddev
->raid_disks
) {
5953 /* expecting a device which has a superblock */
5954 rdev
= md_import_device(dev
, mddev
->major_version
, mddev
->minor_version
);
5957 "md: md_import_device returned %ld\n",
5959 return PTR_ERR(rdev
);
5961 if (!list_empty(&mddev
->disks
)) {
5962 struct md_rdev
*rdev0
5963 = list_entry(mddev
->disks
.next
,
5964 struct md_rdev
, same_set
);
5965 err
= super_types
[mddev
->major_version
]
5966 .load_super(rdev
, rdev0
, mddev
->minor_version
);
5969 "md: %s has different UUID to %s\n",
5970 bdevname(rdev
->bdev
,b
),
5971 bdevname(rdev0
->bdev
,b2
));
5976 err
= bind_rdev_to_array(rdev
, mddev
);
5983 * add_new_disk can be used once the array is assembled
5984 * to add "hot spares". They must already have a superblock
5989 if (!mddev
->pers
->hot_add_disk
) {
5991 "%s: personality does not support diskops!\n",
5995 if (mddev
->persistent
)
5996 rdev
= md_import_device(dev
, mddev
->major_version
,
5997 mddev
->minor_version
);
5999 rdev
= md_import_device(dev
, -1, -1);
6002 "md: md_import_device returned %ld\n",
6004 return PTR_ERR(rdev
);
6006 /* set saved_raid_disk if appropriate */
6007 if (!mddev
->persistent
) {
6008 if (info
->state
& (1<<MD_DISK_SYNC
) &&
6009 info
->raid_disk
< mddev
->raid_disks
) {
6010 rdev
->raid_disk
= info
->raid_disk
;
6011 set_bit(In_sync
, &rdev
->flags
);
6012 clear_bit(Bitmap_sync
, &rdev
->flags
);
6014 rdev
->raid_disk
= -1;
6015 rdev
->saved_raid_disk
= rdev
->raid_disk
;
6017 super_types
[mddev
->major_version
].
6018 validate_super(mddev
, rdev
);
6019 if ((info
->state
& (1<<MD_DISK_SYNC
)) &&
6020 rdev
->raid_disk
!= info
->raid_disk
) {
6021 /* This was a hot-add request, but events doesn't
6022 * match, so reject it.
6028 clear_bit(In_sync
, &rdev
->flags
); /* just to be sure */
6029 if (info
->state
& (1<<MD_DISK_WRITEMOSTLY
))
6030 set_bit(WriteMostly
, &rdev
->flags
);
6032 clear_bit(WriteMostly
, &rdev
->flags
);
6034 if (info
->state
& (1<<MD_DISK_JOURNAL
))
6035 set_bit(Journal
, &rdev
->flags
);
6037 * check whether the device shows up in other nodes
6039 if (mddev_is_clustered(mddev
)) {
6040 if (info
->state
& (1 << MD_DISK_CANDIDATE
))
6041 set_bit(Candidate
, &rdev
->flags
);
6042 else if (info
->state
& (1 << MD_DISK_CLUSTER_ADD
)) {
6043 /* --add initiated by this node */
6044 err
= md_cluster_ops
->add_new_disk(mddev
, rdev
);
6052 rdev
->raid_disk
= -1;
6053 err
= bind_rdev_to_array(rdev
, mddev
);
6058 if (mddev_is_clustered(mddev
)) {
6059 if (info
->state
& (1 << MD_DISK_CANDIDATE
))
6060 md_cluster_ops
->new_disk_ack(mddev
, (err
== 0));
6063 md_cluster_ops
->add_new_disk_cancel(mddev
);
6065 err
= add_bound_rdev(rdev
);
6069 err
= add_bound_rdev(rdev
);
6074 /* otherwise, add_new_disk is only allowed
6075 * for major_version==0 superblocks
6077 if (mddev
->major_version
!= 0) {
6078 printk(KERN_WARNING
"%s: ADD_NEW_DISK not supported\n",
6083 if (!(info
->state
& (1<<MD_DISK_FAULTY
))) {
6085 rdev
= md_import_device(dev
, -1, 0);
6088 "md: error, md_import_device() returned %ld\n",
6090 return PTR_ERR(rdev
);
6092 rdev
->desc_nr
= info
->number
;
6093 if (info
->raid_disk
< mddev
->raid_disks
)
6094 rdev
->raid_disk
= info
->raid_disk
;
6096 rdev
->raid_disk
= -1;
6098 if (rdev
->raid_disk
< mddev
->raid_disks
)
6099 if (info
->state
& (1<<MD_DISK_SYNC
))
6100 set_bit(In_sync
, &rdev
->flags
);
6102 if (info
->state
& (1<<MD_DISK_WRITEMOSTLY
))
6103 set_bit(WriteMostly
, &rdev
->flags
);
6105 if (!mddev
->persistent
) {
6106 printk(KERN_INFO
"md: nonpersistent superblock ...\n");
6107 rdev
->sb_start
= i_size_read(rdev
->bdev
->bd_inode
) / 512;
6109 rdev
->sb_start
= calc_dev_sboffset(rdev
);
6110 rdev
->sectors
= rdev
->sb_start
;
6112 err
= bind_rdev_to_array(rdev
, mddev
);
6122 static int hot_remove_disk(struct mddev
*mddev
, dev_t dev
)
6124 char b
[BDEVNAME_SIZE
];
6125 struct md_rdev
*rdev
;
6128 rdev
= find_rdev(mddev
, dev
);
6132 if (mddev_is_clustered(mddev
))
6133 ret
= md_cluster_ops
->metadata_update_start(mddev
);
6135 if (rdev
->raid_disk
< 0)
6138 clear_bit(Blocked
, &rdev
->flags
);
6139 remove_and_add_spares(mddev
, rdev
);
6141 if (rdev
->raid_disk
>= 0)
6145 if (mddev_is_clustered(mddev
) && ret
== 0)
6146 md_cluster_ops
->remove_disk(mddev
, rdev
);
6148 md_kick_rdev_from_array(rdev
);
6149 md_update_sb(mddev
, 1);
6150 md_new_event(mddev
);
6154 if (mddev_is_clustered(mddev
) && ret
== 0)
6155 md_cluster_ops
->metadata_update_cancel(mddev
);
6157 printk(KERN_WARNING
"md: cannot remove active disk %s from %s ...\n",
6158 bdevname(rdev
->bdev
,b
), mdname(mddev
));
6162 static int hot_add_disk(struct mddev
*mddev
, dev_t dev
)
6164 char b
[BDEVNAME_SIZE
];
6166 struct md_rdev
*rdev
;
6171 if (mddev
->major_version
!= 0) {
6172 printk(KERN_WARNING
"%s: HOT_ADD may only be used with"
6173 " version-0 superblocks.\n",
6177 if (!mddev
->pers
->hot_add_disk
) {
6179 "%s: personality does not support diskops!\n",
6184 rdev
= md_import_device(dev
, -1, 0);
6187 "md: error, md_import_device() returned %ld\n",
6192 if (mddev
->persistent
)
6193 rdev
->sb_start
= calc_dev_sboffset(rdev
);
6195 rdev
->sb_start
= i_size_read(rdev
->bdev
->bd_inode
) / 512;
6197 rdev
->sectors
= rdev
->sb_start
;
6199 if (test_bit(Faulty
, &rdev
->flags
)) {
6201 "md: can not hot-add faulty %s disk to %s!\n",
6202 bdevname(rdev
->bdev
,b
), mdname(mddev
));
6207 clear_bit(In_sync
, &rdev
->flags
);
6209 rdev
->saved_raid_disk
= -1;
6210 err
= bind_rdev_to_array(rdev
, mddev
);
6215 * The rest should better be atomic, we can have disk failures
6216 * noticed in interrupt contexts ...
6219 rdev
->raid_disk
= -1;
6221 md_update_sb(mddev
, 1);
6223 * Kick recovery, maybe this spare has to be added to the
6224 * array immediately.
6226 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6227 md_wakeup_thread(mddev
->thread
);
6228 md_new_event(mddev
);
6236 static int set_bitmap_file(struct mddev
*mddev
, int fd
)
6241 if (!mddev
->pers
->quiesce
|| !mddev
->thread
)
6243 if (mddev
->recovery
|| mddev
->sync_thread
)
6245 /* we should be able to change the bitmap.. */
6249 struct inode
*inode
;
6252 if (mddev
->bitmap
|| mddev
->bitmap_info
.file
)
6253 return -EEXIST
; /* cannot add when bitmap is present */
6257 printk(KERN_ERR
"%s: error: failed to get bitmap file\n",
6262 inode
= f
->f_mapping
->host
;
6263 if (!S_ISREG(inode
->i_mode
)) {
6264 printk(KERN_ERR
"%s: error: bitmap file must be a regular file\n",
6267 } else if (!(f
->f_mode
& FMODE_WRITE
)) {
6268 printk(KERN_ERR
"%s: error: bitmap file must open for write\n",
6271 } else if (atomic_read(&inode
->i_writecount
) != 1) {
6272 printk(KERN_ERR
"%s: error: bitmap file is already in use\n",
6280 mddev
->bitmap_info
.file
= f
;
6281 mddev
->bitmap_info
.offset
= 0; /* file overrides offset */
6282 } else if (mddev
->bitmap
== NULL
)
6283 return -ENOENT
; /* cannot remove what isn't there */
6286 mddev
->pers
->quiesce(mddev
, 1);
6288 struct bitmap
*bitmap
;
6290 bitmap
= bitmap_create(mddev
, -1);
6291 if (!IS_ERR(bitmap
)) {
6292 mddev
->bitmap
= bitmap
;
6293 err
= bitmap_load(mddev
);
6295 err
= PTR_ERR(bitmap
);
6297 if (fd
< 0 || err
) {
6298 bitmap_destroy(mddev
);
6299 fd
= -1; /* make sure to put the file */
6301 mddev
->pers
->quiesce(mddev
, 0);
6304 struct file
*f
= mddev
->bitmap_info
.file
;
6306 spin_lock(&mddev
->lock
);
6307 mddev
->bitmap_info
.file
= NULL
;
6308 spin_unlock(&mddev
->lock
);
6317 * set_array_info is used two different ways
6318 * The original usage is when creating a new array.
6319 * In this usage, raid_disks is > 0 and it together with
6320 * level, size, not_persistent,layout,chunksize determine the
6321 * shape of the array.
6322 * This will always create an array with a type-0.90.0 superblock.
6323 * The newer usage is when assembling an array.
6324 * In this case raid_disks will be 0, and the major_version field is
6325 * use to determine which style super-blocks are to be found on the devices.
6326 * The minor and patch _version numbers are also kept incase the
6327 * super_block handler wishes to interpret them.
6329 static int set_array_info(struct mddev
*mddev
, mdu_array_info_t
*info
)
6332 if (info
->raid_disks
== 0) {
6333 /* just setting version number for superblock loading */
6334 if (info
->major_version
< 0 ||
6335 info
->major_version
>= ARRAY_SIZE(super_types
) ||
6336 super_types
[info
->major_version
].name
== NULL
) {
6337 /* maybe try to auto-load a module? */
6339 "md: superblock version %d not known\n",
6340 info
->major_version
);
6343 mddev
->major_version
= info
->major_version
;
6344 mddev
->minor_version
= info
->minor_version
;
6345 mddev
->patch_version
= info
->patch_version
;
6346 mddev
->persistent
= !info
->not_persistent
;
6347 /* ensure mddev_put doesn't delete this now that there
6348 * is some minimal configuration.
6350 mddev
->ctime
= get_seconds();
6353 mddev
->major_version
= MD_MAJOR_VERSION
;
6354 mddev
->minor_version
= MD_MINOR_VERSION
;
6355 mddev
->patch_version
= MD_PATCHLEVEL_VERSION
;
6356 mddev
->ctime
= get_seconds();
6358 mddev
->level
= info
->level
;
6359 mddev
->clevel
[0] = 0;
6360 mddev
->dev_sectors
= 2 * (sector_t
)info
->size
;
6361 mddev
->raid_disks
= info
->raid_disks
;
6362 /* don't set md_minor, it is determined by which /dev/md* was
6365 if (info
->state
& (1<<MD_SB_CLEAN
))
6366 mddev
->recovery_cp
= MaxSector
;
6368 mddev
->recovery_cp
= 0;
6369 mddev
->persistent
= ! info
->not_persistent
;
6370 mddev
->external
= 0;
6372 mddev
->layout
= info
->layout
;
6373 mddev
->chunk_sectors
= info
->chunk_size
>> 9;
6375 mddev
->max_disks
= MD_SB_DISKS
;
6377 if (mddev
->persistent
)
6379 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
6381 mddev
->bitmap_info
.default_offset
= MD_SB_BYTES
>> 9;
6382 mddev
->bitmap_info
.default_space
= 64*2 - (MD_SB_BYTES
>> 9);
6383 mddev
->bitmap_info
.offset
= 0;
6385 mddev
->reshape_position
= MaxSector
;
6388 * Generate a 128 bit UUID
6390 get_random_bytes(mddev
->uuid
, 16);
6392 mddev
->new_level
= mddev
->level
;
6393 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
6394 mddev
->new_layout
= mddev
->layout
;
6395 mddev
->delta_disks
= 0;
6396 mddev
->reshape_backwards
= 0;
6401 void md_set_array_sectors(struct mddev
*mddev
, sector_t array_sectors
)
6403 WARN(!mddev_is_locked(mddev
), "%s: unlocked mddev!\n", __func__
);
6405 if (mddev
->external_size
)
6408 mddev
->array_sectors
= array_sectors
;
6410 EXPORT_SYMBOL(md_set_array_sectors
);
6412 static int update_size(struct mddev
*mddev
, sector_t num_sectors
)
6414 struct md_rdev
*rdev
;
6416 int fit
= (num_sectors
== 0);
6418 if (mddev
->pers
->resize
== NULL
)
6420 /* The "num_sectors" is the number of sectors of each device that
6421 * is used. This can only make sense for arrays with redundancy.
6422 * linear and raid0 always use whatever space is available. We can only
6423 * consider changing this number if no resync or reconstruction is
6424 * happening, and if the new size is acceptable. It must fit before the
6425 * sb_start or, if that is <data_offset, it must fit before the size
6426 * of each device. If num_sectors is zero, we find the largest size
6429 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
6435 rdev_for_each(rdev
, mddev
) {
6436 sector_t avail
= rdev
->sectors
;
6438 if (fit
&& (num_sectors
== 0 || num_sectors
> avail
))
6439 num_sectors
= avail
;
6440 if (avail
< num_sectors
)
6443 rv
= mddev
->pers
->resize(mddev
, num_sectors
);
6445 revalidate_disk(mddev
->gendisk
);
6449 static int update_raid_disks(struct mddev
*mddev
, int raid_disks
)
6452 struct md_rdev
*rdev
;
6453 /* change the number of raid disks */
6454 if (mddev
->pers
->check_reshape
== NULL
)
6458 if (raid_disks
<= 0 ||
6459 (mddev
->max_disks
&& raid_disks
>= mddev
->max_disks
))
6461 if (mddev
->sync_thread
||
6462 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
6463 mddev
->reshape_position
!= MaxSector
)
6466 rdev_for_each(rdev
, mddev
) {
6467 if (mddev
->raid_disks
< raid_disks
&&
6468 rdev
->data_offset
< rdev
->new_data_offset
)
6470 if (mddev
->raid_disks
> raid_disks
&&
6471 rdev
->data_offset
> rdev
->new_data_offset
)
6475 mddev
->delta_disks
= raid_disks
- mddev
->raid_disks
;
6476 if (mddev
->delta_disks
< 0)
6477 mddev
->reshape_backwards
= 1;
6478 else if (mddev
->delta_disks
> 0)
6479 mddev
->reshape_backwards
= 0;
6481 rv
= mddev
->pers
->check_reshape(mddev
);
6483 mddev
->delta_disks
= 0;
6484 mddev
->reshape_backwards
= 0;
6490 * update_array_info is used to change the configuration of an
6492 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
6493 * fields in the info are checked against the array.
6494 * Any differences that cannot be handled will cause an error.
6495 * Normally, only one change can be managed at a time.
6497 static int update_array_info(struct mddev
*mddev
, mdu_array_info_t
*info
)
6503 /* calculate expected state,ignoring low bits */
6504 if (mddev
->bitmap
&& mddev
->bitmap_info
.offset
)
6505 state
|= (1 << MD_SB_BITMAP_PRESENT
);
6507 if (mddev
->major_version
!= info
->major_version
||
6508 mddev
->minor_version
!= info
->minor_version
||
6509 /* mddev->patch_version != info->patch_version || */
6510 mddev
->ctime
!= info
->ctime
||
6511 mddev
->level
!= info
->level
||
6512 /* mddev->layout != info->layout || */
6513 mddev
->persistent
!= !info
->not_persistent
||
6514 mddev
->chunk_sectors
!= info
->chunk_size
>> 9 ||
6515 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
6516 ((state
^info
->state
) & 0xfffffe00)
6519 /* Check there is only one change */
6520 if (info
->size
>= 0 && mddev
->dev_sectors
/ 2 != info
->size
)
6522 if (mddev
->raid_disks
!= info
->raid_disks
)
6524 if (mddev
->layout
!= info
->layout
)
6526 if ((state
^ info
->state
) & (1<<MD_SB_BITMAP_PRESENT
))
6533 if (mddev
->layout
!= info
->layout
) {
6535 * we don't need to do anything at the md level, the
6536 * personality will take care of it all.
6538 if (mddev
->pers
->check_reshape
== NULL
)
6541 mddev
->new_layout
= info
->layout
;
6542 rv
= mddev
->pers
->check_reshape(mddev
);
6544 mddev
->new_layout
= mddev
->layout
;
6548 if (info
->size
>= 0 && mddev
->dev_sectors
/ 2 != info
->size
)
6549 rv
= update_size(mddev
, (sector_t
)info
->size
* 2);
6551 if (mddev
->raid_disks
!= info
->raid_disks
)
6552 rv
= update_raid_disks(mddev
, info
->raid_disks
);
6554 if ((state
^ info
->state
) & (1<<MD_SB_BITMAP_PRESENT
)) {
6555 if (mddev
->pers
->quiesce
== NULL
|| mddev
->thread
== NULL
) {
6559 if (mddev
->recovery
|| mddev
->sync_thread
) {
6563 if (info
->state
& (1<<MD_SB_BITMAP_PRESENT
)) {
6564 struct bitmap
*bitmap
;
6565 /* add the bitmap */
6566 if (mddev
->bitmap
) {
6570 if (mddev
->bitmap_info
.default_offset
== 0) {
6574 mddev
->bitmap_info
.offset
=
6575 mddev
->bitmap_info
.default_offset
;
6576 mddev
->bitmap_info
.space
=
6577 mddev
->bitmap_info
.default_space
;
6578 mddev
->pers
->quiesce(mddev
, 1);
6579 bitmap
= bitmap_create(mddev
, -1);
6580 if (!IS_ERR(bitmap
)) {
6581 mddev
->bitmap
= bitmap
;
6582 rv
= bitmap_load(mddev
);
6584 rv
= PTR_ERR(bitmap
);
6586 bitmap_destroy(mddev
);
6587 mddev
->pers
->quiesce(mddev
, 0);
6589 /* remove the bitmap */
6590 if (!mddev
->bitmap
) {
6594 if (mddev
->bitmap
->storage
.file
) {
6598 mddev
->pers
->quiesce(mddev
, 1);
6599 bitmap_destroy(mddev
);
6600 mddev
->pers
->quiesce(mddev
, 0);
6601 mddev
->bitmap_info
.offset
= 0;
6604 md_update_sb(mddev
, 1);
6610 static int set_disk_faulty(struct mddev
*mddev
, dev_t dev
)
6612 struct md_rdev
*rdev
;
6615 if (mddev
->pers
== NULL
)
6619 rdev
= find_rdev_rcu(mddev
, dev
);
6623 md_error(mddev
, rdev
);
6624 if (!test_bit(Faulty
, &rdev
->flags
))
6632 * We have a problem here : there is no easy way to give a CHS
6633 * virtual geometry. We currently pretend that we have a 2 heads
6634 * 4 sectors (with a BIG number of cylinders...). This drives
6635 * dosfs just mad... ;-)
6637 static int md_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
6639 struct mddev
*mddev
= bdev
->bd_disk
->private_data
;
6643 geo
->cylinders
= mddev
->array_sectors
/ 8;
6647 static inline bool md_ioctl_valid(unsigned int cmd
)
6652 case GET_ARRAY_INFO
:
6653 case GET_BITMAP_FILE
:
6656 case HOT_REMOVE_DISK
:
6659 case RESTART_ARRAY_RW
:
6661 case SET_ARRAY_INFO
:
6662 case SET_BITMAP_FILE
:
6663 case SET_DISK_FAULTY
:
6666 case CLUSTERED_DISK_NACK
:
6673 static int md_ioctl(struct block_device
*bdev
, fmode_t mode
,
6674 unsigned int cmd
, unsigned long arg
)
6677 void __user
*argp
= (void __user
*)arg
;
6678 struct mddev
*mddev
= NULL
;
6681 if (!md_ioctl_valid(cmd
))
6686 case GET_ARRAY_INFO
:
6690 if (!capable(CAP_SYS_ADMIN
))
6695 * Commands dealing with the RAID driver but not any
6700 err
= get_version(argp
);
6706 autostart_arrays(arg
);
6713 * Commands creating/starting a new array:
6716 mddev
= bdev
->bd_disk
->private_data
;
6723 /* Some actions do not requires the mutex */
6725 case GET_ARRAY_INFO
:
6726 if (!mddev
->raid_disks
&& !mddev
->external
)
6729 err
= get_array_info(mddev
, argp
);
6733 if (!mddev
->raid_disks
&& !mddev
->external
)
6736 err
= get_disk_info(mddev
, argp
);
6739 case SET_DISK_FAULTY
:
6740 err
= set_disk_faulty(mddev
, new_decode_dev(arg
));
6743 case GET_BITMAP_FILE
:
6744 err
= get_bitmap_file(mddev
, argp
);
6749 if (cmd
== ADD_NEW_DISK
)
6750 /* need to ensure md_delayed_delete() has completed */
6751 flush_workqueue(md_misc_wq
);
6753 if (cmd
== HOT_REMOVE_DISK
)
6754 /* need to ensure recovery thread has run */
6755 wait_event_interruptible_timeout(mddev
->sb_wait
,
6756 !test_bit(MD_RECOVERY_NEEDED
,
6758 msecs_to_jiffies(5000));
6759 if (cmd
== STOP_ARRAY
|| cmd
== STOP_ARRAY_RO
) {
6760 /* Need to flush page cache, and ensure no-one else opens
6763 mutex_lock(&mddev
->open_mutex
);
6764 if (mddev
->pers
&& atomic_read(&mddev
->openers
) > 1) {
6765 mutex_unlock(&mddev
->open_mutex
);
6769 set_bit(MD_STILL_CLOSED
, &mddev
->flags
);
6770 mutex_unlock(&mddev
->open_mutex
);
6771 sync_blockdev(bdev
);
6773 err
= mddev_lock(mddev
);
6776 "md: ioctl lock interrupted, reason %d, cmd %d\n",
6781 if (cmd
== SET_ARRAY_INFO
) {
6782 mdu_array_info_t info
;
6784 memset(&info
, 0, sizeof(info
));
6785 else if (copy_from_user(&info
, argp
, sizeof(info
))) {
6790 err
= update_array_info(mddev
, &info
);
6792 printk(KERN_WARNING
"md: couldn't update"
6793 " array info. %d\n", err
);
6798 if (!list_empty(&mddev
->disks
)) {
6800 "md: array %s already has disks!\n",
6805 if (mddev
->raid_disks
) {
6807 "md: array %s already initialised!\n",
6812 err
= set_array_info(mddev
, &info
);
6814 printk(KERN_WARNING
"md: couldn't set"
6815 " array info. %d\n", err
);
6822 * Commands querying/configuring an existing array:
6824 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
6825 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
6826 if ((!mddev
->raid_disks
&& !mddev
->external
)
6827 && cmd
!= ADD_NEW_DISK
&& cmd
!= STOP_ARRAY
6828 && cmd
!= RUN_ARRAY
&& cmd
!= SET_BITMAP_FILE
6829 && cmd
!= GET_BITMAP_FILE
) {
6835 * Commands even a read-only array can execute:
6838 case RESTART_ARRAY_RW
:
6839 err
= restart_array(mddev
);
6843 err
= do_md_stop(mddev
, 0, bdev
);
6847 err
= md_set_readonly(mddev
, bdev
);
6850 case HOT_REMOVE_DISK
:
6851 err
= hot_remove_disk(mddev
, new_decode_dev(arg
));
6855 /* We can support ADD_NEW_DISK on read-only arrays
6856 * on if we are re-adding a preexisting device.
6857 * So require mddev->pers and MD_DISK_SYNC.
6860 mdu_disk_info_t info
;
6861 if (copy_from_user(&info
, argp
, sizeof(info
)))
6863 else if (!(info
.state
& (1<<MD_DISK_SYNC
)))
6864 /* Need to clear read-only for this */
6867 err
= add_new_disk(mddev
, &info
);
6873 if (get_user(ro
, (int __user
*)(arg
))) {
6879 /* if the bdev is going readonly the value of mddev->ro
6880 * does not matter, no writes are coming
6885 /* are we are already prepared for writes? */
6889 /* transitioning to readauto need only happen for
6890 * arrays that call md_write_start
6893 err
= restart_array(mddev
);
6896 set_disk_ro(mddev
->gendisk
, 0);
6903 * The remaining ioctls are changing the state of the
6904 * superblock, so we do not allow them on read-only arrays.
6906 if (mddev
->ro
&& mddev
->pers
) {
6907 if (mddev
->ro
== 2) {
6909 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
6910 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6911 /* mddev_unlock will wake thread */
6912 /* If a device failed while we were read-only, we
6913 * need to make sure the metadata is updated now.
6915 if (test_bit(MD_CHANGE_DEVS
, &mddev
->flags
)) {
6916 mddev_unlock(mddev
);
6917 wait_event(mddev
->sb_wait
,
6918 !test_bit(MD_CHANGE_DEVS
, &mddev
->flags
) &&
6919 !test_bit(MD_CHANGE_PENDING
, &mddev
->flags
));
6920 mddev_lock_nointr(mddev
);
6931 mdu_disk_info_t info
;
6932 if (copy_from_user(&info
, argp
, sizeof(info
)))
6935 err
= add_new_disk(mddev
, &info
);
6939 case CLUSTERED_DISK_NACK
:
6940 if (mddev_is_clustered(mddev
))
6941 md_cluster_ops
->new_disk_ack(mddev
, false);
6947 err
= hot_add_disk(mddev
, new_decode_dev(arg
));
6951 err
= do_md_run(mddev
);
6954 case SET_BITMAP_FILE
:
6955 err
= set_bitmap_file(mddev
, (int)arg
);
6964 if (mddev
->hold_active
== UNTIL_IOCTL
&&
6966 mddev
->hold_active
= 0;
6967 mddev_unlock(mddev
);
6971 #ifdef CONFIG_COMPAT
6972 static int md_compat_ioctl(struct block_device
*bdev
, fmode_t mode
,
6973 unsigned int cmd
, unsigned long arg
)
6976 case HOT_REMOVE_DISK
:
6978 case SET_DISK_FAULTY
:
6979 case SET_BITMAP_FILE
:
6980 /* These take in integer arg, do not convert */
6983 arg
= (unsigned long)compat_ptr(arg
);
6987 return md_ioctl(bdev
, mode
, cmd
, arg
);
6989 #endif /* CONFIG_COMPAT */
6991 static int md_open(struct block_device
*bdev
, fmode_t mode
)
6994 * Succeed if we can lock the mddev, which confirms that
6995 * it isn't being stopped right now.
6997 struct mddev
*mddev
= mddev_find(bdev
->bd_dev
);
7003 if (mddev
->gendisk
!= bdev
->bd_disk
) {
7004 /* we are racing with mddev_put which is discarding this
7008 /* Wait until bdev->bd_disk is definitely gone */
7009 flush_workqueue(md_misc_wq
);
7010 /* Then retry the open from the top */
7011 return -ERESTARTSYS
;
7013 BUG_ON(mddev
!= bdev
->bd_disk
->private_data
);
7015 if ((err
= mutex_lock_interruptible(&mddev
->open_mutex
)))
7019 atomic_inc(&mddev
->openers
);
7020 clear_bit(MD_STILL_CLOSED
, &mddev
->flags
);
7021 mutex_unlock(&mddev
->open_mutex
);
7023 check_disk_change(bdev
);
7028 static void md_release(struct gendisk
*disk
, fmode_t mode
)
7030 struct mddev
*mddev
= disk
->private_data
;
7033 atomic_dec(&mddev
->openers
);
7037 static int md_media_changed(struct gendisk
*disk
)
7039 struct mddev
*mddev
= disk
->private_data
;
7041 return mddev
->changed
;
7044 static int md_revalidate(struct gendisk
*disk
)
7046 struct mddev
*mddev
= disk
->private_data
;
7051 static const struct block_device_operations md_fops
=
7053 .owner
= THIS_MODULE
,
7055 .release
= md_release
,
7057 #ifdef CONFIG_COMPAT
7058 .compat_ioctl
= md_compat_ioctl
,
7060 .getgeo
= md_getgeo
,
7061 .media_changed
= md_media_changed
,
7062 .revalidate_disk
= md_revalidate
,
7065 static int md_thread(void *arg
)
7067 struct md_thread
*thread
= arg
;
7070 * md_thread is a 'system-thread', it's priority should be very
7071 * high. We avoid resource deadlocks individually in each
7072 * raid personality. (RAID5 does preallocation) We also use RR and
7073 * the very same RT priority as kswapd, thus we will never get
7074 * into a priority inversion deadlock.
7076 * we definitely have to have equal or higher priority than
7077 * bdflush, otherwise bdflush will deadlock if there are too
7078 * many dirty RAID5 blocks.
7081 allow_signal(SIGKILL
);
7082 while (!kthread_should_stop()) {
7084 /* We need to wait INTERRUPTIBLE so that
7085 * we don't add to the load-average.
7086 * That means we need to be sure no signals are
7089 if (signal_pending(current
))
7090 flush_signals(current
);
7092 wait_event_interruptible_timeout
7094 test_bit(THREAD_WAKEUP
, &thread
->flags
)
7095 || kthread_should_stop(),
7098 clear_bit(THREAD_WAKEUP
, &thread
->flags
);
7099 if (!kthread_should_stop())
7100 thread
->run(thread
);
7106 void md_wakeup_thread(struct md_thread
*thread
)
7109 pr_debug("md: waking up MD thread %s.\n", thread
->tsk
->comm
);
7110 set_bit(THREAD_WAKEUP
, &thread
->flags
);
7111 wake_up(&thread
->wqueue
);
7114 EXPORT_SYMBOL(md_wakeup_thread
);
7116 struct md_thread
*md_register_thread(void (*run
) (struct md_thread
*),
7117 struct mddev
*mddev
, const char *name
)
7119 struct md_thread
*thread
;
7121 thread
= kzalloc(sizeof(struct md_thread
), GFP_KERNEL
);
7125 init_waitqueue_head(&thread
->wqueue
);
7128 thread
->mddev
= mddev
;
7129 thread
->timeout
= MAX_SCHEDULE_TIMEOUT
;
7130 thread
->tsk
= kthread_run(md_thread
, thread
,
7132 mdname(thread
->mddev
),
7134 if (IS_ERR(thread
->tsk
)) {
7140 EXPORT_SYMBOL(md_register_thread
);
7142 void md_unregister_thread(struct md_thread
**threadp
)
7144 struct md_thread
*thread
= *threadp
;
7147 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread
->tsk
));
7148 /* Locking ensures that mddev_unlock does not wake_up a
7149 * non-existent thread
7151 spin_lock(&pers_lock
);
7153 spin_unlock(&pers_lock
);
7155 kthread_stop(thread
->tsk
);
7158 EXPORT_SYMBOL(md_unregister_thread
);
7160 void md_error(struct mddev
*mddev
, struct md_rdev
*rdev
)
7162 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
))
7165 if (!mddev
->pers
|| !mddev
->pers
->error_handler
)
7167 mddev
->pers
->error_handler(mddev
,rdev
);
7168 if (mddev
->degraded
)
7169 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
7170 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
7171 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
7172 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7173 md_wakeup_thread(mddev
->thread
);
7174 if (mddev
->event_work
.func
)
7175 queue_work(md_misc_wq
, &mddev
->event_work
);
7176 md_new_event_inintr(mddev
);
7178 EXPORT_SYMBOL(md_error
);
7180 /* seq_file implementation /proc/mdstat */
7182 static void status_unused(struct seq_file
*seq
)
7185 struct md_rdev
*rdev
;
7187 seq_printf(seq
, "unused devices: ");
7189 list_for_each_entry(rdev
, &pending_raid_disks
, same_set
) {
7190 char b
[BDEVNAME_SIZE
];
7192 seq_printf(seq
, "%s ",
7193 bdevname(rdev
->bdev
,b
));
7196 seq_printf(seq
, "<none>");
7198 seq_printf(seq
, "\n");
7201 static int status_resync(struct seq_file
*seq
, struct mddev
*mddev
)
7203 sector_t max_sectors
, resync
, res
;
7204 unsigned long dt
, db
;
7207 unsigned int per_milli
;
7209 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ||
7210 test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
7211 max_sectors
= mddev
->resync_max_sectors
;
7213 max_sectors
= mddev
->dev_sectors
;
7215 resync
= mddev
->curr_resync
;
7217 if (test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
))
7218 /* Still cleaning up */
7219 resync
= max_sectors
;
7221 resync
-= atomic_read(&mddev
->recovery_active
);
7224 if (mddev
->recovery_cp
< MaxSector
) {
7225 seq_printf(seq
, "\tresync=PENDING");
7231 seq_printf(seq
, "\tresync=DELAYED");
7235 WARN_ON(max_sectors
== 0);
7236 /* Pick 'scale' such that (resync>>scale)*1000 will fit
7237 * in a sector_t, and (max_sectors>>scale) will fit in a
7238 * u32, as those are the requirements for sector_div.
7239 * Thus 'scale' must be at least 10
7242 if (sizeof(sector_t
) > sizeof(unsigned long)) {
7243 while ( max_sectors
/2 > (1ULL<<(scale
+32)))
7246 res
= (resync
>>scale
)*1000;
7247 sector_div(res
, (u32
)((max_sectors
>>scale
)+1));
7251 int i
, x
= per_milli
/50, y
= 20-x
;
7252 seq_printf(seq
, "[");
7253 for (i
= 0; i
< x
; i
++)
7254 seq_printf(seq
, "=");
7255 seq_printf(seq
, ">");
7256 for (i
= 0; i
< y
; i
++)
7257 seq_printf(seq
, ".");
7258 seq_printf(seq
, "] ");
7260 seq_printf(seq
, " %s =%3u.%u%% (%llu/%llu)",
7261 (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
)?
7263 (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
)?
7265 (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ?
7266 "resync" : "recovery"))),
7267 per_milli
/10, per_milli
% 10,
7268 (unsigned long long) resync
/2,
7269 (unsigned long long) max_sectors
/2);
7272 * dt: time from mark until now
7273 * db: blocks written from mark until now
7274 * rt: remaining time
7276 * rt is a sector_t, so could be 32bit or 64bit.
7277 * So we divide before multiply in case it is 32bit and close
7279 * We scale the divisor (db) by 32 to avoid losing precision
7280 * near the end of resync when the number of remaining sectors
7282 * We then divide rt by 32 after multiplying by db to compensate.
7283 * The '+1' avoids division by zero if db is very small.
7285 dt
= ((jiffies
- mddev
->resync_mark
) / HZ
);
7287 db
= (mddev
->curr_mark_cnt
- atomic_read(&mddev
->recovery_active
))
7288 - mddev
->resync_mark_cnt
;
7290 rt
= max_sectors
- resync
; /* number of remaining sectors */
7291 sector_div(rt
, db
/32+1);
7295 seq_printf(seq
, " finish=%lu.%lumin", (unsigned long)rt
/ 60,
7296 ((unsigned long)rt
% 60)/6);
7298 seq_printf(seq
, " speed=%ldK/sec", db
/2/dt
);
7302 static void *md_seq_start(struct seq_file
*seq
, loff_t
*pos
)
7304 struct list_head
*tmp
;
7306 struct mddev
*mddev
;
7314 spin_lock(&all_mddevs_lock
);
7315 list_for_each(tmp
,&all_mddevs
)
7317 mddev
= list_entry(tmp
, struct mddev
, all_mddevs
);
7319 spin_unlock(&all_mddevs_lock
);
7322 spin_unlock(&all_mddevs_lock
);
7324 return (void*)2;/* tail */
7328 static void *md_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
7330 struct list_head
*tmp
;
7331 struct mddev
*next_mddev
, *mddev
= v
;
7337 spin_lock(&all_mddevs_lock
);
7339 tmp
= all_mddevs
.next
;
7341 tmp
= mddev
->all_mddevs
.next
;
7342 if (tmp
!= &all_mddevs
)
7343 next_mddev
= mddev_get(list_entry(tmp
,struct mddev
,all_mddevs
));
7345 next_mddev
= (void*)2;
7348 spin_unlock(&all_mddevs_lock
);
7356 static void md_seq_stop(struct seq_file
*seq
, void *v
)
7358 struct mddev
*mddev
= v
;
7360 if (mddev
&& v
!= (void*)1 && v
!= (void*)2)
7364 static int md_seq_show(struct seq_file
*seq
, void *v
)
7366 struct mddev
*mddev
= v
;
7368 struct md_rdev
*rdev
;
7370 if (v
== (void*)1) {
7371 struct md_personality
*pers
;
7372 seq_printf(seq
, "Personalities : ");
7373 spin_lock(&pers_lock
);
7374 list_for_each_entry(pers
, &pers_list
, list
)
7375 seq_printf(seq
, "[%s] ", pers
->name
);
7377 spin_unlock(&pers_lock
);
7378 seq_printf(seq
, "\n");
7379 seq
->poll_event
= atomic_read(&md_event_count
);
7382 if (v
== (void*)2) {
7387 spin_lock(&mddev
->lock
);
7388 if (mddev
->pers
|| mddev
->raid_disks
|| !list_empty(&mddev
->disks
)) {
7389 seq_printf(seq
, "%s : %sactive", mdname(mddev
),
7390 mddev
->pers
? "" : "in");
7393 seq_printf(seq
, " (read-only)");
7395 seq_printf(seq
, " (auto-read-only)");
7396 seq_printf(seq
, " %s", mddev
->pers
->name
);
7401 rdev_for_each_rcu(rdev
, mddev
) {
7402 char b
[BDEVNAME_SIZE
];
7403 seq_printf(seq
, " %s[%d]",
7404 bdevname(rdev
->bdev
,b
), rdev
->desc_nr
);
7405 if (test_bit(WriteMostly
, &rdev
->flags
))
7406 seq_printf(seq
, "(W)");
7407 if (test_bit(Journal
, &rdev
->flags
))
7408 seq_printf(seq
, "(J)");
7409 if (test_bit(Faulty
, &rdev
->flags
)) {
7410 seq_printf(seq
, "(F)");
7413 if (rdev
->raid_disk
< 0)
7414 seq_printf(seq
, "(S)"); /* spare */
7415 if (test_bit(Replacement
, &rdev
->flags
))
7416 seq_printf(seq
, "(R)");
7417 sectors
+= rdev
->sectors
;
7421 if (!list_empty(&mddev
->disks
)) {
7423 seq_printf(seq
, "\n %llu blocks",
7424 (unsigned long long)
7425 mddev
->array_sectors
/ 2);
7427 seq_printf(seq
, "\n %llu blocks",
7428 (unsigned long long)sectors
/ 2);
7430 if (mddev
->persistent
) {
7431 if (mddev
->major_version
!= 0 ||
7432 mddev
->minor_version
!= 90) {
7433 seq_printf(seq
," super %d.%d",
7434 mddev
->major_version
,
7435 mddev
->minor_version
);
7437 } else if (mddev
->external
)
7438 seq_printf(seq
, " super external:%s",
7439 mddev
->metadata_type
);
7441 seq_printf(seq
, " super non-persistent");
7444 mddev
->pers
->status(seq
, mddev
);
7445 seq_printf(seq
, "\n ");
7446 if (mddev
->pers
->sync_request
) {
7447 if (status_resync(seq
, mddev
))
7448 seq_printf(seq
, "\n ");
7451 seq_printf(seq
, "\n ");
7453 bitmap_status(seq
, mddev
->bitmap
);
7455 seq_printf(seq
, "\n");
7457 spin_unlock(&mddev
->lock
);
7462 static const struct seq_operations md_seq_ops
= {
7463 .start
= md_seq_start
,
7464 .next
= md_seq_next
,
7465 .stop
= md_seq_stop
,
7466 .show
= md_seq_show
,
7469 static int md_seq_open(struct inode
*inode
, struct file
*file
)
7471 struct seq_file
*seq
;
7474 error
= seq_open(file
, &md_seq_ops
);
7478 seq
= file
->private_data
;
7479 seq
->poll_event
= atomic_read(&md_event_count
);
7483 static int md_unloading
;
7484 static unsigned int mdstat_poll(struct file
*filp
, poll_table
*wait
)
7486 struct seq_file
*seq
= filp
->private_data
;
7490 return POLLIN
|POLLRDNORM
|POLLERR
|POLLPRI
;
7491 poll_wait(filp
, &md_event_waiters
, wait
);
7493 /* always allow read */
7494 mask
= POLLIN
| POLLRDNORM
;
7496 if (seq
->poll_event
!= atomic_read(&md_event_count
))
7497 mask
|= POLLERR
| POLLPRI
;
7501 static const struct file_operations md_seq_fops
= {
7502 .owner
= THIS_MODULE
,
7503 .open
= md_seq_open
,
7505 .llseek
= seq_lseek
,
7506 .release
= seq_release_private
,
7507 .poll
= mdstat_poll
,
7510 int register_md_personality(struct md_personality
*p
)
7512 printk(KERN_INFO
"md: %s personality registered for level %d\n",
7514 spin_lock(&pers_lock
);
7515 list_add_tail(&p
->list
, &pers_list
);
7516 spin_unlock(&pers_lock
);
7519 EXPORT_SYMBOL(register_md_personality
);
7521 int unregister_md_personality(struct md_personality
*p
)
7523 printk(KERN_INFO
"md: %s personality unregistered\n", p
->name
);
7524 spin_lock(&pers_lock
);
7525 list_del_init(&p
->list
);
7526 spin_unlock(&pers_lock
);
7529 EXPORT_SYMBOL(unregister_md_personality
);
7531 int register_md_cluster_operations(struct md_cluster_operations
*ops
,
7532 struct module
*module
)
7535 spin_lock(&pers_lock
);
7536 if (md_cluster_ops
!= NULL
)
7539 md_cluster_ops
= ops
;
7540 md_cluster_mod
= module
;
7542 spin_unlock(&pers_lock
);
7545 EXPORT_SYMBOL(register_md_cluster_operations
);
7547 int unregister_md_cluster_operations(void)
7549 spin_lock(&pers_lock
);
7550 md_cluster_ops
= NULL
;
7551 spin_unlock(&pers_lock
);
7554 EXPORT_SYMBOL(unregister_md_cluster_operations
);
7556 int md_setup_cluster(struct mddev
*mddev
, int nodes
)
7560 err
= request_module("md-cluster");
7562 pr_err("md-cluster module not found.\n");
7566 spin_lock(&pers_lock
);
7567 if (!md_cluster_ops
|| !try_module_get(md_cluster_mod
)) {
7568 spin_unlock(&pers_lock
);
7571 spin_unlock(&pers_lock
);
7573 return md_cluster_ops
->join(mddev
, nodes
);
7576 void md_cluster_stop(struct mddev
*mddev
)
7578 if (!md_cluster_ops
)
7580 md_cluster_ops
->leave(mddev
);
7581 module_put(md_cluster_mod
);
7584 static int is_mddev_idle(struct mddev
*mddev
, int init
)
7586 struct md_rdev
*rdev
;
7592 rdev_for_each_rcu(rdev
, mddev
) {
7593 struct gendisk
*disk
= rdev
->bdev
->bd_contains
->bd_disk
;
7594 curr_events
= (int)part_stat_read(&disk
->part0
, sectors
[0]) +
7595 (int)part_stat_read(&disk
->part0
, sectors
[1]) -
7596 atomic_read(&disk
->sync_io
);
7597 /* sync IO will cause sync_io to increase before the disk_stats
7598 * as sync_io is counted when a request starts, and
7599 * disk_stats is counted when it completes.
7600 * So resync activity will cause curr_events to be smaller than
7601 * when there was no such activity.
7602 * non-sync IO will cause disk_stat to increase without
7603 * increasing sync_io so curr_events will (eventually)
7604 * be larger than it was before. Once it becomes
7605 * substantially larger, the test below will cause
7606 * the array to appear non-idle, and resync will slow
7608 * If there is a lot of outstanding resync activity when
7609 * we set last_event to curr_events, then all that activity
7610 * completing might cause the array to appear non-idle
7611 * and resync will be slowed down even though there might
7612 * not have been non-resync activity. This will only
7613 * happen once though. 'last_events' will soon reflect
7614 * the state where there is little or no outstanding
7615 * resync requests, and further resync activity will
7616 * always make curr_events less than last_events.
7619 if (init
|| curr_events
- rdev
->last_events
> 64) {
7620 rdev
->last_events
= curr_events
;
7628 void md_done_sync(struct mddev
*mddev
, int blocks
, int ok
)
7630 /* another "blocks" (512byte) blocks have been synced */
7631 atomic_sub(blocks
, &mddev
->recovery_active
);
7632 wake_up(&mddev
->recovery_wait
);
7634 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
7635 set_bit(MD_RECOVERY_ERROR
, &mddev
->recovery
);
7636 md_wakeup_thread(mddev
->thread
);
7637 // stop recovery, signal do_sync ....
7640 EXPORT_SYMBOL(md_done_sync
);
7642 /* md_write_start(mddev, bi)
7643 * If we need to update some array metadata (e.g. 'active' flag
7644 * in superblock) before writing, schedule a superblock update
7645 * and wait for it to complete.
7647 void md_write_start(struct mddev
*mddev
, struct bio
*bi
)
7650 if (bio_data_dir(bi
) != WRITE
)
7653 BUG_ON(mddev
->ro
== 1);
7654 if (mddev
->ro
== 2) {
7655 /* need to switch to read/write */
7657 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7658 md_wakeup_thread(mddev
->thread
);
7659 md_wakeup_thread(mddev
->sync_thread
);
7662 atomic_inc(&mddev
->writes_pending
);
7663 if (mddev
->safemode
== 1)
7664 mddev
->safemode
= 0;
7665 if (mddev
->in_sync
) {
7666 spin_lock(&mddev
->lock
);
7667 if (mddev
->in_sync
) {
7669 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
7670 set_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
7671 md_wakeup_thread(mddev
->thread
);
7674 spin_unlock(&mddev
->lock
);
7677 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
7678 wait_event(mddev
->sb_wait
,
7679 !test_bit(MD_CHANGE_PENDING
, &mddev
->flags
));
7681 EXPORT_SYMBOL(md_write_start
);
7683 void md_write_end(struct mddev
*mddev
)
7685 if (atomic_dec_and_test(&mddev
->writes_pending
)) {
7686 if (mddev
->safemode
== 2)
7687 md_wakeup_thread(mddev
->thread
);
7688 else if (mddev
->safemode_delay
)
7689 mod_timer(&mddev
->safemode_timer
, jiffies
+ mddev
->safemode_delay
);
7692 EXPORT_SYMBOL(md_write_end
);
7694 /* md_allow_write(mddev)
7695 * Calling this ensures that the array is marked 'active' so that writes
7696 * may proceed without blocking. It is important to call this before
7697 * attempting a GFP_KERNEL allocation while holding the mddev lock.
7698 * Must be called with mddev_lock held.
7700 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
7701 * is dropped, so return -EAGAIN after notifying userspace.
7703 int md_allow_write(struct mddev
*mddev
)
7709 if (!mddev
->pers
->sync_request
)
7712 spin_lock(&mddev
->lock
);
7713 if (mddev
->in_sync
) {
7715 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
7716 set_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
7717 if (mddev
->safemode_delay
&&
7718 mddev
->safemode
== 0)
7719 mddev
->safemode
= 1;
7720 spin_unlock(&mddev
->lock
);
7721 md_update_sb(mddev
, 0);
7722 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
7724 spin_unlock(&mddev
->lock
);
7726 if (test_bit(MD_CHANGE_PENDING
, &mddev
->flags
))
7731 EXPORT_SYMBOL_GPL(md_allow_write
);
7733 #define SYNC_MARKS 10
7734 #define SYNC_MARK_STEP (3*HZ)
7735 #define UPDATE_FREQUENCY (5*60*HZ)
7736 void md_do_sync(struct md_thread
*thread
)
7738 struct mddev
*mddev
= thread
->mddev
;
7739 struct mddev
*mddev2
;
7740 unsigned int currspeed
= 0,
7742 sector_t max_sectors
,j
, io_sectors
, recovery_done
;
7743 unsigned long mark
[SYNC_MARKS
];
7744 unsigned long update_time
;
7745 sector_t mark_cnt
[SYNC_MARKS
];
7747 struct list_head
*tmp
;
7748 sector_t last_check
;
7750 struct md_rdev
*rdev
;
7751 char *desc
, *action
= NULL
;
7752 struct blk_plug plug
;
7753 bool cluster_resync_finished
= false;
7755 /* just incase thread restarts... */
7756 if (test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
))
7758 if (mddev
->ro
) {/* never try to sync a read-only array */
7759 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
7763 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
7764 if (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
)) {
7765 desc
= "data-check";
7767 } else if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
)) {
7768 desc
= "requested-resync";
7772 } else if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
7777 mddev
->last_sync_action
= action
?: desc
;
7779 /* we overload curr_resync somewhat here.
7780 * 0 == not engaged in resync at all
7781 * 2 == checking that there is no conflict with another sync
7782 * 1 == like 2, but have yielded to allow conflicting resync to
7784 * other == active in resync - this many blocks
7786 * Before starting a resync we must have set curr_resync to
7787 * 2, and then checked that every "conflicting" array has curr_resync
7788 * less than ours. When we find one that is the same or higher
7789 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
7790 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
7791 * This will mean we have to start checking from the beginning again.
7796 mddev
->curr_resync
= 2;
7799 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
7801 for_each_mddev(mddev2
, tmp
) {
7802 if (mddev2
== mddev
)
7804 if (!mddev
->parallel_resync
7805 && mddev2
->curr_resync
7806 && match_mddev_units(mddev
, mddev2
)) {
7808 if (mddev
< mddev2
&& mddev
->curr_resync
== 2) {
7809 /* arbitrarily yield */
7810 mddev
->curr_resync
= 1;
7811 wake_up(&resync_wait
);
7813 if (mddev
> mddev2
&& mddev
->curr_resync
== 1)
7814 /* no need to wait here, we can wait the next
7815 * time 'round when curr_resync == 2
7818 /* We need to wait 'interruptible' so as not to
7819 * contribute to the load average, and not to
7820 * be caught by 'softlockup'
7822 prepare_to_wait(&resync_wait
, &wq
, TASK_INTERRUPTIBLE
);
7823 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
) &&
7824 mddev2
->curr_resync
>= mddev
->curr_resync
) {
7825 printk(KERN_INFO
"md: delaying %s of %s"
7826 " until %s has finished (they"
7827 " share one or more physical units)\n",
7828 desc
, mdname(mddev
), mdname(mddev2
));
7830 if (signal_pending(current
))
7831 flush_signals(current
);
7833 finish_wait(&resync_wait
, &wq
);
7836 finish_wait(&resync_wait
, &wq
);
7839 } while (mddev
->curr_resync
< 2);
7842 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
7843 /* resync follows the size requested by the personality,
7844 * which defaults to physical size, but can be virtual size
7846 max_sectors
= mddev
->resync_max_sectors
;
7847 atomic64_set(&mddev
->resync_mismatches
, 0);
7848 /* we don't use the checkpoint if there's a bitmap */
7849 if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
7850 j
= mddev
->resync_min
;
7851 else if (!mddev
->bitmap
)
7852 j
= mddev
->recovery_cp
;
7854 } else if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
7855 max_sectors
= mddev
->resync_max_sectors
;
7857 /* recovery follows the physical size of devices */
7858 max_sectors
= mddev
->dev_sectors
;
7861 rdev_for_each_rcu(rdev
, mddev
)
7862 if (rdev
->raid_disk
>= 0 &&
7863 !test_bit(Journal
, &rdev
->flags
) &&
7864 !test_bit(Faulty
, &rdev
->flags
) &&
7865 !test_bit(In_sync
, &rdev
->flags
) &&
7866 rdev
->recovery_offset
< j
)
7867 j
= rdev
->recovery_offset
;
7870 /* If there is a bitmap, we need to make sure all
7871 * writes that started before we added a spare
7872 * complete before we start doing a recovery.
7873 * Otherwise the write might complete and (via
7874 * bitmap_endwrite) set a bit in the bitmap after the
7875 * recovery has checked that bit and skipped that
7878 if (mddev
->bitmap
) {
7879 mddev
->pers
->quiesce(mddev
, 1);
7880 mddev
->pers
->quiesce(mddev
, 0);
7884 printk(KERN_INFO
"md: %s of RAID array %s\n", desc
, mdname(mddev
));
7885 printk(KERN_INFO
"md: minimum _guaranteed_ speed:"
7886 " %d KB/sec/disk.\n", speed_min(mddev
));
7887 printk(KERN_INFO
"md: using maximum available idle IO bandwidth "
7888 "(but not more than %d KB/sec) for %s.\n",
7889 speed_max(mddev
), desc
);
7891 is_mddev_idle(mddev
, 1); /* this initializes IO event counters */
7894 for (m
= 0; m
< SYNC_MARKS
; m
++) {
7896 mark_cnt
[m
] = io_sectors
;
7899 mddev
->resync_mark
= mark
[last_mark
];
7900 mddev
->resync_mark_cnt
= mark_cnt
[last_mark
];
7903 * Tune reconstruction:
7905 window
= 32*(PAGE_SIZE
/512);
7906 printk(KERN_INFO
"md: using %dk window, over a total of %lluk.\n",
7907 window
/2, (unsigned long long)max_sectors
/2);
7909 atomic_set(&mddev
->recovery_active
, 0);
7914 "md: resuming %s of %s from checkpoint.\n",
7915 desc
, mdname(mddev
));
7916 mddev
->curr_resync
= j
;
7918 mddev
->curr_resync
= 3; /* no longer delayed */
7919 mddev
->curr_resync_completed
= j
;
7920 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
7921 md_new_event(mddev
);
7922 update_time
= jiffies
;
7924 blk_start_plug(&plug
);
7925 while (j
< max_sectors
) {
7930 if (!test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
7931 ((mddev
->curr_resync
> mddev
->curr_resync_completed
&&
7932 (mddev
->curr_resync
- mddev
->curr_resync_completed
)
7933 > (max_sectors
>> 4)) ||
7934 time_after_eq(jiffies
, update_time
+ UPDATE_FREQUENCY
) ||
7935 (j
- mddev
->curr_resync_completed
)*2
7936 >= mddev
->resync_max
- mddev
->curr_resync_completed
||
7937 mddev
->curr_resync_completed
> mddev
->resync_max
7939 /* time to update curr_resync_completed */
7940 wait_event(mddev
->recovery_wait
,
7941 atomic_read(&mddev
->recovery_active
) == 0);
7942 mddev
->curr_resync_completed
= j
;
7943 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) &&
7944 j
> mddev
->recovery_cp
)
7945 mddev
->recovery_cp
= j
;
7946 update_time
= jiffies
;
7947 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
7948 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
7951 while (j
>= mddev
->resync_max
&&
7952 !test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
7953 /* As this condition is controlled by user-space,
7954 * we can block indefinitely, so use '_interruptible'
7955 * to avoid triggering warnings.
7957 flush_signals(current
); /* just in case */
7958 wait_event_interruptible(mddev
->recovery_wait
,
7959 mddev
->resync_max
> j
7960 || test_bit(MD_RECOVERY_INTR
,
7964 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
7967 sectors
= mddev
->pers
->sync_request(mddev
, j
, &skipped
);
7969 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
7973 if (!skipped
) { /* actual IO requested */
7974 io_sectors
+= sectors
;
7975 atomic_add(sectors
, &mddev
->recovery_active
);
7978 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
7982 if (j
> max_sectors
)
7983 /* when skipping, extra large numbers can be returned. */
7986 mddev
->curr_resync
= j
;
7987 mddev
->curr_mark_cnt
= io_sectors
;
7988 if (last_check
== 0)
7989 /* this is the earliest that rebuild will be
7990 * visible in /proc/mdstat
7992 md_new_event(mddev
);
7994 if (last_check
+ window
> io_sectors
|| j
== max_sectors
)
7997 last_check
= io_sectors
;
7999 if (time_after_eq(jiffies
, mark
[last_mark
] + SYNC_MARK_STEP
)) {
8001 int next
= (last_mark
+1) % SYNC_MARKS
;
8003 mddev
->resync_mark
= mark
[next
];
8004 mddev
->resync_mark_cnt
= mark_cnt
[next
];
8005 mark
[next
] = jiffies
;
8006 mark_cnt
[next
] = io_sectors
- atomic_read(&mddev
->recovery_active
);
8010 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
8014 * this loop exits only if either when we are slower than
8015 * the 'hard' speed limit, or the system was IO-idle for
8017 * the system might be non-idle CPU-wise, but we only care
8018 * about not overloading the IO subsystem. (things like an
8019 * e2fsck being done on the RAID array should execute fast)
8023 recovery_done
= io_sectors
- atomic_read(&mddev
->recovery_active
);
8024 currspeed
= ((unsigned long)(recovery_done
- mddev
->resync_mark_cnt
))/2
8025 /((jiffies
-mddev
->resync_mark
)/HZ
+1) +1;
8027 if (currspeed
> speed_min(mddev
)) {
8028 if (currspeed
> speed_max(mddev
)) {
8032 if (!is_mddev_idle(mddev
, 0)) {
8034 * Give other IO more of a chance.
8035 * The faster the devices, the less we wait.
8037 wait_event(mddev
->recovery_wait
,
8038 !atomic_read(&mddev
->recovery_active
));
8042 printk(KERN_INFO
"md: %s: %s %s.\n",mdname(mddev
), desc
,
8043 test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)
8044 ? "interrupted" : "done");
8046 * this also signals 'finished resyncing' to md_stop
8048 blk_finish_plug(&plug
);
8049 wait_event(mddev
->recovery_wait
, !atomic_read(&mddev
->recovery_active
));
8051 if (!test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
8052 !test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
) &&
8053 mddev
->curr_resync
> 2) {
8054 mddev
->curr_resync_completed
= mddev
->curr_resync
;
8055 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
8057 /* tell personality and other nodes that we are finished */
8058 if (mddev_is_clustered(mddev
)) {
8059 md_cluster_ops
->resync_finish(mddev
);
8060 cluster_resync_finished
= true;
8062 mddev
->pers
->sync_request(mddev
, max_sectors
, &skipped
);
8064 if (!test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
) &&
8065 mddev
->curr_resync
> 2) {
8066 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
8067 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
8068 if (mddev
->curr_resync
>= mddev
->recovery_cp
) {
8070 "md: checkpointing %s of %s.\n",
8071 desc
, mdname(mddev
));
8072 if (test_bit(MD_RECOVERY_ERROR
,
8074 mddev
->recovery_cp
=
8075 mddev
->curr_resync_completed
;
8077 mddev
->recovery_cp
=
8081 mddev
->recovery_cp
= MaxSector
;
8083 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
8084 mddev
->curr_resync
= MaxSector
;
8086 rdev_for_each_rcu(rdev
, mddev
)
8087 if (rdev
->raid_disk
>= 0 &&
8088 mddev
->delta_disks
>= 0 &&
8089 !test_bit(Journal
, &rdev
->flags
) &&
8090 !test_bit(Faulty
, &rdev
->flags
) &&
8091 !test_bit(In_sync
, &rdev
->flags
) &&
8092 rdev
->recovery_offset
< mddev
->curr_resync
)
8093 rdev
->recovery_offset
= mddev
->curr_resync
;
8098 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
8100 if (mddev_is_clustered(mddev
) &&
8101 test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
) &&
8102 !cluster_resync_finished
)
8103 md_cluster_ops
->resync_finish(mddev
);
8105 spin_lock(&mddev
->lock
);
8106 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
8107 /* We completed so min/max setting can be forgotten if used. */
8108 if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
8109 mddev
->resync_min
= 0;
8110 mddev
->resync_max
= MaxSector
;
8111 } else if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
8112 mddev
->resync_min
= mddev
->curr_resync_completed
;
8113 set_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
8114 mddev
->curr_resync
= 0;
8115 spin_unlock(&mddev
->lock
);
8117 wake_up(&resync_wait
);
8118 md_wakeup_thread(mddev
->thread
);
8121 EXPORT_SYMBOL_GPL(md_do_sync
);
8123 static int remove_and_add_spares(struct mddev
*mddev
,
8124 struct md_rdev
*this)
8126 struct md_rdev
*rdev
;
8130 rdev_for_each(rdev
, mddev
)
8131 if ((this == NULL
|| rdev
== this) &&
8132 rdev
->raid_disk
>= 0 &&
8133 !test_bit(Blocked
, &rdev
->flags
) &&
8134 (test_bit(Faulty
, &rdev
->flags
) ||
8135 (!test_bit(In_sync
, &rdev
->flags
) &&
8136 !test_bit(Journal
, &rdev
->flags
))) &&
8137 atomic_read(&rdev
->nr_pending
)==0) {
8138 if (mddev
->pers
->hot_remove_disk(
8139 mddev
, rdev
) == 0) {
8140 sysfs_unlink_rdev(mddev
, rdev
);
8141 rdev
->raid_disk
= -1;
8145 if (removed
&& mddev
->kobj
.sd
)
8146 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
8148 if (this && removed
)
8151 rdev_for_each(rdev
, mddev
) {
8152 if (this && this != rdev
)
8154 if (test_bit(Candidate
, &rdev
->flags
))
8156 if (rdev
->raid_disk
>= 0 &&
8157 !test_bit(In_sync
, &rdev
->flags
) &&
8158 !test_bit(Journal
, &rdev
->flags
) &&
8159 !test_bit(Faulty
, &rdev
->flags
))
8161 if (rdev
->raid_disk
>= 0)
8163 if (test_bit(Faulty
, &rdev
->flags
))
8165 if (test_bit(Journal
, &rdev
->flags
))
8168 ! (rdev
->saved_raid_disk
>= 0 &&
8169 !test_bit(Bitmap_sync
, &rdev
->flags
)))
8172 rdev
->recovery_offset
= 0;
8174 hot_add_disk(mddev
, rdev
) == 0) {
8175 if (sysfs_link_rdev(mddev
, rdev
))
8176 /* failure here is OK */;
8178 md_new_event(mddev
);
8179 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
8184 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
8188 static void md_start_sync(struct work_struct
*ws
)
8190 struct mddev
*mddev
= container_of(ws
, struct mddev
, del_work
);
8193 if (mddev_is_clustered(mddev
)) {
8194 ret
= md_cluster_ops
->resync_start(mddev
);
8196 mddev
->sync_thread
= NULL
;
8201 mddev
->sync_thread
= md_register_thread(md_do_sync
,
8205 if (!mddev
->sync_thread
) {
8206 if (!(mddev_is_clustered(mddev
) && ret
== -EAGAIN
))
8207 printk(KERN_ERR
"%s: could not start resync"
8210 /* leave the spares where they are, it shouldn't hurt */
8211 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
8212 clear_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
8213 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
8214 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
8215 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
8216 wake_up(&resync_wait
);
8217 if (test_and_clear_bit(MD_RECOVERY_RECOVER
,
8219 if (mddev
->sysfs_action
)
8220 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
8222 md_wakeup_thread(mddev
->sync_thread
);
8223 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
8224 md_new_event(mddev
);
8228 * This routine is regularly called by all per-raid-array threads to
8229 * deal with generic issues like resync and super-block update.
8230 * Raid personalities that don't have a thread (linear/raid0) do not
8231 * need this as they never do any recovery or update the superblock.
8233 * It does not do any resync itself, but rather "forks" off other threads
8234 * to do that as needed.
8235 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
8236 * "->recovery" and create a thread at ->sync_thread.
8237 * When the thread finishes it sets MD_RECOVERY_DONE
8238 * and wakeups up this thread which will reap the thread and finish up.
8239 * This thread also removes any faulty devices (with nr_pending == 0).
8241 * The overall approach is:
8242 * 1/ if the superblock needs updating, update it.
8243 * 2/ If a recovery thread is running, don't do anything else.
8244 * 3/ If recovery has finished, clean up, possibly marking spares active.
8245 * 4/ If there are any faulty devices, remove them.
8246 * 5/ If array is degraded, try to add spares devices
8247 * 6/ If array has spares or is not in-sync, start a resync thread.
8249 void md_check_recovery(struct mddev
*mddev
)
8251 if (mddev
->suspended
)
8255 bitmap_daemon_work(mddev
);
8257 if (signal_pending(current
)) {
8258 if (mddev
->pers
->sync_request
&& !mddev
->external
) {
8259 printk(KERN_INFO
"md: %s in immediate safe mode\n",
8261 mddev
->safemode
= 2;
8263 flush_signals(current
);
8266 if (mddev
->ro
&& !test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))
8269 (mddev
->flags
& MD_UPDATE_SB_FLAGS
& ~ (1<<MD_CHANGE_PENDING
)) ||
8270 test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
) ||
8271 test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
) ||
8272 (mddev
->external
== 0 && mddev
->safemode
== 1) ||
8273 (mddev
->safemode
== 2 && ! atomic_read(&mddev
->writes_pending
)
8274 && !mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
)
8278 if (mddev_trylock(mddev
)) {
8282 struct md_rdev
*rdev
;
8283 if (!mddev
->external
&& mddev
->in_sync
)
8284 /* 'Blocked' flag not needed as failed devices
8285 * will be recorded if array switched to read/write.
8286 * Leaving it set will prevent the device
8287 * from being removed.
8289 rdev_for_each(rdev
, mddev
)
8290 clear_bit(Blocked
, &rdev
->flags
);
8291 /* On a read-only array we can:
8292 * - remove failed devices
8293 * - add already-in_sync devices if the array itself
8295 * As we only add devices that are already in-sync,
8296 * we can activate the spares immediately.
8298 remove_and_add_spares(mddev
, NULL
);
8299 /* There is no thread, but we need to call
8300 * ->spare_active and clear saved_raid_disk
8302 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
8303 md_reap_sync_thread(mddev
);
8304 clear_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
8305 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
8306 clear_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
8310 if (!mddev
->external
) {
8312 spin_lock(&mddev
->lock
);
8313 if (mddev
->safemode
&&
8314 !atomic_read(&mddev
->writes_pending
) &&
8316 mddev
->recovery_cp
== MaxSector
) {
8319 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
8321 if (mddev
->safemode
== 1)
8322 mddev
->safemode
= 0;
8323 spin_unlock(&mddev
->lock
);
8325 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
8328 if (mddev
->flags
& MD_UPDATE_SB_FLAGS
)
8329 md_update_sb(mddev
, 0);
8331 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) &&
8332 !test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
)) {
8333 /* resync/recovery still happening */
8334 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
8337 if (mddev
->sync_thread
) {
8338 md_reap_sync_thread(mddev
);
8341 /* Set RUNNING before clearing NEEDED to avoid
8342 * any transients in the value of "sync_action".
8344 mddev
->curr_resync_completed
= 0;
8345 spin_lock(&mddev
->lock
);
8346 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
8347 spin_unlock(&mddev
->lock
);
8348 /* Clear some bits that don't mean anything, but
8351 clear_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
8352 clear_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
8354 if (!test_and_clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
) ||
8355 test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
))
8357 /* no recovery is running.
8358 * remove any failed drives, then
8359 * add spares if possible.
8360 * Spares are also removed and re-added, to allow
8361 * the personality to fail the re-add.
8364 if (mddev
->reshape_position
!= MaxSector
) {
8365 if (mddev
->pers
->check_reshape
== NULL
||
8366 mddev
->pers
->check_reshape(mddev
) != 0)
8367 /* Cannot proceed */
8369 set_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
8370 clear_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
8371 } else if ((spares
= remove_and_add_spares(mddev
, NULL
))) {
8372 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
8373 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
8374 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
8375 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
8376 } else if (mddev
->recovery_cp
< MaxSector
) {
8377 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
8378 clear_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
8379 } else if (!test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
8380 /* nothing to be done ... */
8383 if (mddev
->pers
->sync_request
) {
8385 /* We are adding a device or devices to an array
8386 * which has the bitmap stored on all devices.
8387 * So make sure all bitmap pages get written
8389 bitmap_write_all(mddev
->bitmap
);
8391 INIT_WORK(&mddev
->del_work
, md_start_sync
);
8392 queue_work(md_misc_wq
, &mddev
->del_work
);
8396 if (!mddev
->sync_thread
) {
8397 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
8398 wake_up(&resync_wait
);
8399 if (test_and_clear_bit(MD_RECOVERY_RECOVER
,
8401 if (mddev
->sysfs_action
)
8402 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
8405 wake_up(&mddev
->sb_wait
);
8406 mddev_unlock(mddev
);
8409 EXPORT_SYMBOL(md_check_recovery
);
8411 void md_reap_sync_thread(struct mddev
*mddev
)
8413 struct md_rdev
*rdev
;
8415 /* resync has finished, collect result */
8416 md_unregister_thread(&mddev
->sync_thread
);
8417 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
) &&
8418 !test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
)) {
8420 /* activate any spares */
8421 if (mddev
->pers
->spare_active(mddev
)) {
8422 sysfs_notify(&mddev
->kobj
, NULL
,
8424 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
8427 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
8428 mddev
->pers
->finish_reshape
)
8429 mddev
->pers
->finish_reshape(mddev
);
8431 /* If array is no-longer degraded, then any saved_raid_disk
8432 * information must be scrapped.
8434 if (!mddev
->degraded
)
8435 rdev_for_each(rdev
, mddev
)
8436 rdev
->saved_raid_disk
= -1;
8438 md_update_sb(mddev
, 1);
8439 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
8440 clear_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
8441 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
8442 clear_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
8443 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
8444 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
8445 wake_up(&resync_wait
);
8446 /* flag recovery needed just to double check */
8447 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
8448 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
8449 md_new_event(mddev
);
8450 if (mddev
->event_work
.func
)
8451 queue_work(md_misc_wq
, &mddev
->event_work
);
8453 EXPORT_SYMBOL(md_reap_sync_thread
);
8455 void md_wait_for_blocked_rdev(struct md_rdev
*rdev
, struct mddev
*mddev
)
8457 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
8458 wait_event_timeout(rdev
->blocked_wait
,
8459 !test_bit(Blocked
, &rdev
->flags
) &&
8460 !test_bit(BlockedBadBlocks
, &rdev
->flags
),
8461 msecs_to_jiffies(5000));
8462 rdev_dec_pending(rdev
, mddev
);
8464 EXPORT_SYMBOL(md_wait_for_blocked_rdev
);
8466 void md_finish_reshape(struct mddev
*mddev
)
8468 /* called be personality module when reshape completes. */
8469 struct md_rdev
*rdev
;
8471 rdev_for_each(rdev
, mddev
) {
8472 if (rdev
->data_offset
> rdev
->new_data_offset
)
8473 rdev
->sectors
+= rdev
->data_offset
- rdev
->new_data_offset
;
8475 rdev
->sectors
-= rdev
->new_data_offset
- rdev
->data_offset
;
8476 rdev
->data_offset
= rdev
->new_data_offset
;
8479 EXPORT_SYMBOL(md_finish_reshape
);
8481 /* Bad block management.
8482 * We can record which blocks on each device are 'bad' and so just
8483 * fail those blocks, or that stripe, rather than the whole device.
8484 * Entries in the bad-block table are 64bits wide. This comprises:
8485 * Length of bad-range, in sectors: 0-511 for lengths 1-512
8486 * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes)
8487 * A 'shift' can be set so that larger blocks are tracked and
8488 * consequently larger devices can be covered.
8489 * 'Acknowledged' flag - 1 bit. - the most significant bit.
8491 * Locking of the bad-block table uses a seqlock so md_is_badblock
8492 * might need to retry if it is very unlucky.
8493 * We will sometimes want to check for bad blocks in a bi_end_io function,
8494 * so we use the write_seqlock_irq variant.
8496 * When looking for a bad block we specify a range and want to
8497 * know if any block in the range is bad. So we binary-search
8498 * to the last range that starts at-or-before the given endpoint,
8499 * (or "before the sector after the target range")
8500 * then see if it ends after the given start.
8502 * 0 if there are no known bad blocks in the range
8503 * 1 if there are known bad block which are all acknowledged
8504 * -1 if there are bad blocks which have not yet been acknowledged in metadata.
8505 * plus the start/length of the first bad section we overlap.
8507 int md_is_badblock(struct badblocks
*bb
, sector_t s
, int sectors
,
8508 sector_t
*first_bad
, int *bad_sectors
)
8514 sector_t target
= s
+ sectors
;
8517 if (bb
->shift
> 0) {
8518 /* round the start down, and the end up */
8520 target
+= (1<<bb
->shift
) - 1;
8521 target
>>= bb
->shift
;
8522 sectors
= target
- s
;
8524 /* 'target' is now the first block after the bad range */
8527 seq
= read_seqbegin(&bb
->lock
);
8532 /* Binary search between lo and hi for 'target'
8533 * i.e. for the last range that starts before 'target'
8535 /* INVARIANT: ranges before 'lo' and at-or-after 'hi'
8536 * are known not to be the last range before target.
8537 * VARIANT: hi-lo is the number of possible
8538 * ranges, and decreases until it reaches 1
8540 while (hi
- lo
> 1) {
8541 int mid
= (lo
+ hi
) / 2;
8542 sector_t a
= BB_OFFSET(p
[mid
]);
8544 /* This could still be the one, earlier ranges
8548 /* This and later ranges are definitely out. */
8551 /* 'lo' might be the last that started before target, but 'hi' isn't */
8553 /* need to check all range that end after 's' to see if
8554 * any are unacknowledged.
8557 BB_OFFSET(p
[lo
]) + BB_LEN(p
[lo
]) > s
) {
8558 if (BB_OFFSET(p
[lo
]) < target
) {
8559 /* starts before the end, and finishes after
8560 * the start, so they must overlap
8562 if (rv
!= -1 && BB_ACK(p
[lo
]))
8566 *first_bad
= BB_OFFSET(p
[lo
]);
8567 *bad_sectors
= BB_LEN(p
[lo
]);
8573 if (read_seqretry(&bb
->lock
, seq
))
8578 EXPORT_SYMBOL_GPL(md_is_badblock
);
8581 * Add a range of bad blocks to the table.
8582 * This might extend the table, or might contract it
8583 * if two adjacent ranges can be merged.
8584 * We binary-search to find the 'insertion' point, then
8585 * decide how best to handle it.
8587 static int md_set_badblocks(struct badblocks
*bb
, sector_t s
, int sectors
,
8593 unsigned long flags
;
8596 /* badblocks are disabled */
8600 /* round the start down, and the end up */
8601 sector_t next
= s
+ sectors
;
8603 next
+= (1<<bb
->shift
) - 1;
8608 write_seqlock_irqsave(&bb
->lock
, flags
);
8613 /* Find the last range that starts at-or-before 's' */
8614 while (hi
- lo
> 1) {
8615 int mid
= (lo
+ hi
) / 2;
8616 sector_t a
= BB_OFFSET(p
[mid
]);
8622 if (hi
> lo
&& BB_OFFSET(p
[lo
]) > s
)
8626 /* we found a range that might merge with the start
8629 sector_t a
= BB_OFFSET(p
[lo
]);
8630 sector_t e
= a
+ BB_LEN(p
[lo
]);
8631 int ack
= BB_ACK(p
[lo
]);
8633 /* Yes, we can merge with a previous range */
8634 if (s
== a
&& s
+ sectors
>= e
)
8635 /* new range covers old */
8638 ack
= ack
&& acknowledged
;
8640 if (e
< s
+ sectors
)
8642 if (e
- a
<= BB_MAX_LEN
) {
8643 p
[lo
] = BB_MAKE(a
, e
-a
, ack
);
8646 /* does not all fit in one range,
8647 * make p[lo] maximal
8649 if (BB_LEN(p
[lo
]) != BB_MAX_LEN
)
8650 p
[lo
] = BB_MAKE(a
, BB_MAX_LEN
, ack
);
8656 if (sectors
&& hi
< bb
->count
) {
8657 /* 'hi' points to the first range that starts after 's'.
8658 * Maybe we can merge with the start of that range */
8659 sector_t a
= BB_OFFSET(p
[hi
]);
8660 sector_t e
= a
+ BB_LEN(p
[hi
]);
8661 int ack
= BB_ACK(p
[hi
]);
8662 if (a
<= s
+ sectors
) {
8663 /* merging is possible */
8664 if (e
<= s
+ sectors
) {
8669 ack
= ack
&& acknowledged
;
8672 if (e
- a
<= BB_MAX_LEN
) {
8673 p
[hi
] = BB_MAKE(a
, e
-a
, ack
);
8676 p
[hi
] = BB_MAKE(a
, BB_MAX_LEN
, ack
);
8684 if (sectors
== 0 && hi
< bb
->count
) {
8685 /* we might be able to combine lo and hi */
8686 /* Note: 's' is at the end of 'lo' */
8687 sector_t a
= BB_OFFSET(p
[hi
]);
8688 int lolen
= BB_LEN(p
[lo
]);
8689 int hilen
= BB_LEN(p
[hi
]);
8690 int newlen
= lolen
+ hilen
- (s
- a
);
8691 if (s
>= a
&& newlen
< BB_MAX_LEN
) {
8692 /* yes, we can combine them */
8693 int ack
= BB_ACK(p
[lo
]) && BB_ACK(p
[hi
]);
8694 p
[lo
] = BB_MAKE(BB_OFFSET(p
[lo
]), newlen
, ack
);
8695 memmove(p
+ hi
, p
+ hi
+ 1,
8696 (bb
->count
- hi
- 1) * 8);
8701 /* didn't merge (it all).
8702 * Need to add a range just before 'hi' */
8703 if (bb
->count
>= MD_MAX_BADBLOCKS
) {
8704 /* No room for more */
8708 int this_sectors
= sectors
;
8709 memmove(p
+ hi
+ 1, p
+ hi
,
8710 (bb
->count
- hi
) * 8);
8713 if (this_sectors
> BB_MAX_LEN
)
8714 this_sectors
= BB_MAX_LEN
;
8715 p
[hi
] = BB_MAKE(s
, this_sectors
, acknowledged
);
8716 sectors
-= this_sectors
;
8723 bb
->unacked_exist
= 1;
8724 write_sequnlock_irqrestore(&bb
->lock
, flags
);
8729 int rdev_set_badblocks(struct md_rdev
*rdev
, sector_t s
, int sectors
,
8734 s
+= rdev
->new_data_offset
;
8736 s
+= rdev
->data_offset
;
8737 rv
= md_set_badblocks(&rdev
->badblocks
,
8740 /* Make sure they get written out promptly */
8741 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
8742 set_bit(MD_CHANGE_CLEAN
, &rdev
->mddev
->flags
);
8743 set_bit(MD_CHANGE_PENDING
, &rdev
->mddev
->flags
);
8744 md_wakeup_thread(rdev
->mddev
->thread
);
8748 EXPORT_SYMBOL_GPL(rdev_set_badblocks
);
8751 * Remove a range of bad blocks from the table.
8752 * This may involve extending the table if we spilt a region,
8753 * but it must not fail. So if the table becomes full, we just
8754 * drop the remove request.
8756 static int md_clear_badblocks(struct badblocks
*bb
, sector_t s
, int sectors
)
8760 sector_t target
= s
+ sectors
;
8763 if (bb
->shift
> 0) {
8764 /* When clearing we round the start up and the end down.
8765 * This should not matter as the shift should align with
8766 * the block size and no rounding should ever be needed.
8767 * However it is better the think a block is bad when it
8768 * isn't than to think a block is not bad when it is.
8770 s
+= (1<<bb
->shift
) - 1;
8772 target
>>= bb
->shift
;
8773 sectors
= target
- s
;
8776 write_seqlock_irq(&bb
->lock
);
8781 /* Find the last range that starts before 'target' */
8782 while (hi
- lo
> 1) {
8783 int mid
= (lo
+ hi
) / 2;
8784 sector_t a
= BB_OFFSET(p
[mid
]);
8791 /* p[lo] is the last range that could overlap the
8792 * current range. Earlier ranges could also overlap,
8793 * but only this one can overlap the end of the range.
8795 if (BB_OFFSET(p
[lo
]) + BB_LEN(p
[lo
]) > target
) {
8796 /* Partial overlap, leave the tail of this range */
8797 int ack
= BB_ACK(p
[lo
]);
8798 sector_t a
= BB_OFFSET(p
[lo
]);
8799 sector_t end
= a
+ BB_LEN(p
[lo
]);
8802 /* we need to split this range */
8803 if (bb
->count
>= MD_MAX_BADBLOCKS
) {
8807 memmove(p
+lo
+1, p
+lo
, (bb
->count
- lo
) * 8);
8809 p
[lo
] = BB_MAKE(a
, s
-a
, ack
);
8812 p
[lo
] = BB_MAKE(target
, end
- target
, ack
);
8813 /* there is no longer an overlap */
8818 BB_OFFSET(p
[lo
]) + BB_LEN(p
[lo
]) > s
) {
8819 /* This range does overlap */
8820 if (BB_OFFSET(p
[lo
]) < s
) {
8821 /* Keep the early parts of this range. */
8822 int ack
= BB_ACK(p
[lo
]);
8823 sector_t start
= BB_OFFSET(p
[lo
]);
8824 p
[lo
] = BB_MAKE(start
, s
- start
, ack
);
8825 /* now low doesn't overlap, so.. */
8830 /* 'lo' is strictly before, 'hi' is strictly after,
8831 * anything between needs to be discarded
8834 memmove(p
+lo
+1, p
+hi
, (bb
->count
- hi
) * 8);
8835 bb
->count
-= (hi
- lo
- 1);
8841 write_sequnlock_irq(&bb
->lock
);
8845 int rdev_clear_badblocks(struct md_rdev
*rdev
, sector_t s
, int sectors
,
8849 s
+= rdev
->new_data_offset
;
8851 s
+= rdev
->data_offset
;
8852 return md_clear_badblocks(&rdev
->badblocks
,
8855 EXPORT_SYMBOL_GPL(rdev_clear_badblocks
);
8858 * Acknowledge all bad blocks in a list.
8859 * This only succeeds if ->changed is clear. It is used by
8860 * in-kernel metadata updates
8862 void md_ack_all_badblocks(struct badblocks
*bb
)
8864 if (bb
->page
== NULL
|| bb
->changed
)
8865 /* no point even trying */
8867 write_seqlock_irq(&bb
->lock
);
8869 if (bb
->changed
== 0 && bb
->unacked_exist
) {
8872 for (i
= 0; i
< bb
->count
; i
++) {
8873 if (!BB_ACK(p
[i
])) {
8874 sector_t start
= BB_OFFSET(p
[i
]);
8875 int len
= BB_LEN(p
[i
]);
8876 p
[i
] = BB_MAKE(start
, len
, 1);
8879 bb
->unacked_exist
= 0;
8881 write_sequnlock_irq(&bb
->lock
);
8883 EXPORT_SYMBOL_GPL(md_ack_all_badblocks
);
8885 /* sysfs access to bad-blocks list.
8886 * We present two files.
8887 * 'bad-blocks' lists sector numbers and lengths of ranges that
8888 * are recorded as bad. The list is truncated to fit within
8889 * the one-page limit of sysfs.
8890 * Writing "sector length" to this file adds an acknowledged
8892 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
8893 * been acknowledged. Writing to this file adds bad blocks
8894 * without acknowledging them. This is largely for testing.
8898 badblocks_show(struct badblocks
*bb
, char *page
, int unack
)
8909 seq
= read_seqbegin(&bb
->lock
);
8914 while (len
< PAGE_SIZE
&& i
< bb
->count
) {
8915 sector_t s
= BB_OFFSET(p
[i
]);
8916 unsigned int length
= BB_LEN(p
[i
]);
8917 int ack
= BB_ACK(p
[i
]);
8923 len
+= snprintf(page
+len
, PAGE_SIZE
-len
, "%llu %u\n",
8924 (unsigned long long)s
<< bb
->shift
,
8925 length
<< bb
->shift
);
8927 if (unack
&& len
== 0)
8928 bb
->unacked_exist
= 0;
8930 if (read_seqretry(&bb
->lock
, seq
))
8939 badblocks_store(struct badblocks
*bb
, const char *page
, size_t len
, int unack
)
8941 unsigned long long sector
;
8945 /* Allow clearing via sysfs *only* for testing/debugging.
8946 * Normally only a successful write may clear a badblock
8949 if (page
[0] == '-') {
8953 #endif /* DO_DEBUG */
8955 switch (sscanf(page
, "%llu %d%c", §or
, &length
, &newline
)) {
8957 if (newline
!= '\n')
8969 md_clear_badblocks(bb
, sector
, length
);
8972 #endif /* DO_DEBUG */
8973 if (md_set_badblocks(bb
, sector
, length
, !unack
))
8979 static int md_notify_reboot(struct notifier_block
*this,
8980 unsigned long code
, void *x
)
8982 struct list_head
*tmp
;
8983 struct mddev
*mddev
;
8986 for_each_mddev(mddev
, tmp
) {
8987 if (mddev_trylock(mddev
)) {
8989 __md_stop_writes(mddev
);
8990 if (mddev
->persistent
)
8991 mddev
->safemode
= 2;
8992 mddev_unlock(mddev
);
8997 * certain more exotic SCSI devices are known to be
8998 * volatile wrt too early system reboots. While the
8999 * right place to handle this issue is the given
9000 * driver, we do want to have a safe RAID driver ...
9008 static struct notifier_block md_notifier
= {
9009 .notifier_call
= md_notify_reboot
,
9011 .priority
= INT_MAX
, /* before any real devices */
9014 static void md_geninit(void)
9016 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t
));
9018 proc_create("mdstat", S_IRUGO
, NULL
, &md_seq_fops
);
9021 static int __init
md_init(void)
9025 md_wq
= alloc_workqueue("md", WQ_MEM_RECLAIM
, 0);
9029 md_misc_wq
= alloc_workqueue("md_misc", 0, 0);
9033 if ((ret
= register_blkdev(MD_MAJOR
, "md")) < 0)
9036 if ((ret
= register_blkdev(0, "mdp")) < 0)
9040 blk_register_region(MKDEV(MD_MAJOR
, 0), 512, THIS_MODULE
,
9041 md_probe
, NULL
, NULL
);
9042 blk_register_region(MKDEV(mdp_major
, 0), 1UL<<MINORBITS
, THIS_MODULE
,
9043 md_probe
, NULL
, NULL
);
9045 register_reboot_notifier(&md_notifier
);
9046 raid_table_header
= register_sysctl_table(raid_root_table
);
9052 unregister_blkdev(MD_MAJOR
, "md");
9054 destroy_workqueue(md_misc_wq
);
9056 destroy_workqueue(md_wq
);
9061 static void check_sb_changes(struct mddev
*mddev
, struct md_rdev
*rdev
)
9063 struct mdp_superblock_1
*sb
= page_address(rdev
->sb_page
);
9064 struct md_rdev
*rdev2
;
9066 char b
[BDEVNAME_SIZE
];
9068 /* Check for change of roles in the active devices */
9069 rdev_for_each(rdev2
, mddev
) {
9070 if (test_bit(Faulty
, &rdev2
->flags
))
9073 /* Check if the roles changed */
9074 role
= le16_to_cpu(sb
->dev_roles
[rdev2
->desc_nr
]);
9076 if (test_bit(Candidate
, &rdev2
->flags
)) {
9077 if (role
== 0xfffe) {
9078 pr_info("md: Removing Candidate device %s because add failed\n", bdevname(rdev2
->bdev
,b
));
9079 md_kick_rdev_from_array(rdev2
);
9083 clear_bit(Candidate
, &rdev2
->flags
);
9086 if (role
!= rdev2
->raid_disk
) {
9088 if (rdev2
->raid_disk
== -1 && role
!= 0xffff) {
9089 rdev2
->saved_raid_disk
= role
;
9090 ret
= remove_and_add_spares(mddev
, rdev2
);
9091 pr_info("Activated spare: %s\n",
9092 bdevname(rdev2
->bdev
,b
));
9096 * We just want to do the minimum to mark the disk
9097 * as faulty. The recovery is performed by the
9098 * one who initiated the error.
9100 if ((role
== 0xfffe) || (role
== 0xfffd)) {
9101 md_error(mddev
, rdev2
);
9102 clear_bit(Blocked
, &rdev2
->flags
);
9107 if (mddev
->raid_disks
!= le32_to_cpu(sb
->raid_disks
))
9108 update_raid_disks(mddev
, le32_to_cpu(sb
->raid_disks
));
9110 /* Finally set the event to be up to date */
9111 mddev
->events
= le64_to_cpu(sb
->events
);
9114 static int read_rdev(struct mddev
*mddev
, struct md_rdev
*rdev
)
9117 struct page
*swapout
= rdev
->sb_page
;
9118 struct mdp_superblock_1
*sb
;
9120 /* Store the sb page of the rdev in the swapout temporary
9121 * variable in case we err in the future
9123 rdev
->sb_page
= NULL
;
9124 alloc_disk_sb(rdev
);
9125 ClearPageUptodate(rdev
->sb_page
);
9126 rdev
->sb_loaded
= 0;
9127 err
= super_types
[mddev
->major_version
].load_super(rdev
, NULL
, mddev
->minor_version
);
9130 pr_warn("%s: %d Could not reload rdev(%d) err: %d. Restoring old values\n",
9131 __func__
, __LINE__
, rdev
->desc_nr
, err
);
9132 put_page(rdev
->sb_page
);
9133 rdev
->sb_page
= swapout
;
9134 rdev
->sb_loaded
= 1;
9138 sb
= page_address(rdev
->sb_page
);
9139 /* Read the offset unconditionally, even if MD_FEATURE_RECOVERY_OFFSET
9143 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RECOVERY_OFFSET
))
9144 rdev
->recovery_offset
= le64_to_cpu(sb
->recovery_offset
);
9146 /* The other node finished recovery, call spare_active to set
9147 * device In_sync and mddev->degraded
9149 if (rdev
->recovery_offset
== MaxSector
&&
9150 !test_bit(In_sync
, &rdev
->flags
) &&
9151 mddev
->pers
->spare_active(mddev
))
9152 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
9158 void md_reload_sb(struct mddev
*mddev
, int nr
)
9160 struct md_rdev
*rdev
;
9164 rdev_for_each_rcu(rdev
, mddev
) {
9165 if (rdev
->desc_nr
== nr
)
9169 if (!rdev
|| rdev
->desc_nr
!= nr
) {
9170 pr_warn("%s: %d Could not find rdev with nr %d\n", __func__
, __LINE__
, nr
);
9174 err
= read_rdev(mddev
, rdev
);
9178 check_sb_changes(mddev
, rdev
);
9180 /* Read all rdev's to update recovery_offset */
9181 rdev_for_each_rcu(rdev
, mddev
)
9182 read_rdev(mddev
, rdev
);
9184 EXPORT_SYMBOL(md_reload_sb
);
9189 * Searches all registered partitions for autorun RAID arrays
9193 static LIST_HEAD(all_detected_devices
);
9194 struct detected_devices_node
{
9195 struct list_head list
;
9199 void md_autodetect_dev(dev_t dev
)
9201 struct detected_devices_node
*node_detected_dev
;
9203 node_detected_dev
= kzalloc(sizeof(*node_detected_dev
), GFP_KERNEL
);
9204 if (node_detected_dev
) {
9205 node_detected_dev
->dev
= dev
;
9206 list_add_tail(&node_detected_dev
->list
, &all_detected_devices
);
9208 printk(KERN_CRIT
"md: md_autodetect_dev: kzalloc failed"
9209 ", skipping dev(%d,%d)\n", MAJOR(dev
), MINOR(dev
));
9213 static void autostart_arrays(int part
)
9215 struct md_rdev
*rdev
;
9216 struct detected_devices_node
*node_detected_dev
;
9218 int i_scanned
, i_passed
;
9223 printk(KERN_INFO
"md: Autodetecting RAID arrays.\n");
9225 while (!list_empty(&all_detected_devices
) && i_scanned
< INT_MAX
) {
9227 node_detected_dev
= list_entry(all_detected_devices
.next
,
9228 struct detected_devices_node
, list
);
9229 list_del(&node_detected_dev
->list
);
9230 dev
= node_detected_dev
->dev
;
9231 kfree(node_detected_dev
);
9232 rdev
= md_import_device(dev
,0, 90);
9236 if (test_bit(Faulty
, &rdev
->flags
))
9239 set_bit(AutoDetected
, &rdev
->flags
);
9240 list_add(&rdev
->same_set
, &pending_raid_disks
);
9244 printk(KERN_INFO
"md: Scanned %d and added %d devices.\n",
9245 i_scanned
, i_passed
);
9247 autorun_devices(part
);
9250 #endif /* !MODULE */
9252 static __exit
void md_exit(void)
9254 struct mddev
*mddev
;
9255 struct list_head
*tmp
;
9258 blk_unregister_region(MKDEV(MD_MAJOR
,0), 512);
9259 blk_unregister_region(MKDEV(mdp_major
,0), 1U << MINORBITS
);
9261 unregister_blkdev(MD_MAJOR
,"md");
9262 unregister_blkdev(mdp_major
, "mdp");
9263 unregister_reboot_notifier(&md_notifier
);
9264 unregister_sysctl_table(raid_table_header
);
9266 /* We cannot unload the modules while some process is
9267 * waiting for us in select() or poll() - wake them up
9270 while (waitqueue_active(&md_event_waiters
)) {
9271 /* not safe to leave yet */
9272 wake_up(&md_event_waiters
);
9276 remove_proc_entry("mdstat", NULL
);
9278 for_each_mddev(mddev
, tmp
) {
9279 export_array(mddev
);
9280 mddev
->hold_active
= 0;
9282 destroy_workqueue(md_misc_wq
);
9283 destroy_workqueue(md_wq
);
9286 subsys_initcall(md_init
);
9287 module_exit(md_exit
)
9289 static int get_ro(char *buffer
, struct kernel_param
*kp
)
9291 return sprintf(buffer
, "%d", start_readonly
);
9293 static int set_ro(const char *val
, struct kernel_param
*kp
)
9295 return kstrtouint(val
, 10, (unsigned int *)&start_readonly
);
9298 module_param_call(start_ro
, set_ro
, get_ro
, NULL
, S_IRUSR
|S_IWUSR
);
9299 module_param(start_dirty_degraded
, int, S_IRUGO
|S_IWUSR
);
9300 module_param_call(new_array
, add_named_array
, NULL
, NULL
, S_IWUSR
);
9302 MODULE_LICENSE("GPL");
9303 MODULE_DESCRIPTION("MD RAID framework");
9305 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR
);