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 /* bio could be mergeable after passing to underlayer */
297 bio
->bi_rw
&= ~REQ_NOMERGE
;
298 mddev
->pers
->make_request(mddev
, bio
);
300 cpu
= part_stat_lock();
301 part_stat_inc(cpu
, &mddev
->gendisk
->part0
, ios
[rw
]);
302 part_stat_add(cpu
, &mddev
->gendisk
->part0
, sectors
[rw
], sectors
);
305 if (atomic_dec_and_test(&mddev
->active_io
) && mddev
->suspended
)
306 wake_up(&mddev
->sb_wait
);
308 return BLK_QC_T_NONE
;
311 /* mddev_suspend makes sure no new requests are submitted
312 * to the device, and that any requests that have been submitted
313 * are completely handled.
314 * Once mddev_detach() is called and completes, the module will be
317 void mddev_suspend(struct mddev
*mddev
)
319 if (mddev
->suspended
++)
322 wait_event(mddev
->sb_wait
, atomic_read(&mddev
->active_io
) == 0);
323 mddev
->pers
->quiesce(mddev
, 1);
325 del_timer_sync(&mddev
->safemode_timer
);
327 EXPORT_SYMBOL_GPL(mddev_suspend
);
329 void mddev_resume(struct mddev
*mddev
)
331 if (--mddev
->suspended
)
333 wake_up(&mddev
->sb_wait
);
334 mddev
->pers
->quiesce(mddev
, 0);
336 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
337 md_wakeup_thread(mddev
->thread
);
338 md_wakeup_thread(mddev
->sync_thread
); /* possibly kick off a reshape */
340 EXPORT_SYMBOL_GPL(mddev_resume
);
342 int mddev_congested(struct mddev
*mddev
, int bits
)
344 struct md_personality
*pers
= mddev
->pers
;
348 if (mddev
->suspended
)
350 else if (pers
&& pers
->congested
)
351 ret
= pers
->congested(mddev
, bits
);
355 EXPORT_SYMBOL_GPL(mddev_congested
);
356 static int md_congested(void *data
, int bits
)
358 struct mddev
*mddev
= data
;
359 return mddev_congested(mddev
, bits
);
363 * Generic flush handling for md
366 static void md_end_flush(struct bio
*bio
)
368 struct md_rdev
*rdev
= bio
->bi_private
;
369 struct mddev
*mddev
= rdev
->mddev
;
371 rdev_dec_pending(rdev
, mddev
);
373 if (atomic_dec_and_test(&mddev
->flush_pending
)) {
374 /* The pre-request flush has finished */
375 queue_work(md_wq
, &mddev
->flush_work
);
380 static void md_submit_flush_data(struct work_struct
*ws
);
382 static void submit_flushes(struct work_struct
*ws
)
384 struct mddev
*mddev
= container_of(ws
, struct mddev
, flush_work
);
385 struct md_rdev
*rdev
;
387 INIT_WORK(&mddev
->flush_work
, md_submit_flush_data
);
388 atomic_set(&mddev
->flush_pending
, 1);
390 rdev_for_each_rcu(rdev
, mddev
)
391 if (rdev
->raid_disk
>= 0 &&
392 !test_bit(Faulty
, &rdev
->flags
)) {
393 /* Take two references, one is dropped
394 * when request finishes, one after
395 * we reclaim rcu_read_lock
398 atomic_inc(&rdev
->nr_pending
);
399 atomic_inc(&rdev
->nr_pending
);
401 bi
= bio_alloc_mddev(GFP_NOIO
, 0, mddev
);
402 bi
->bi_end_io
= md_end_flush
;
403 bi
->bi_private
= rdev
;
404 bi
->bi_bdev
= rdev
->bdev
;
405 atomic_inc(&mddev
->flush_pending
);
406 submit_bio(WRITE_FLUSH
, bi
);
408 rdev_dec_pending(rdev
, mddev
);
411 if (atomic_dec_and_test(&mddev
->flush_pending
))
412 queue_work(md_wq
, &mddev
->flush_work
);
415 static void md_submit_flush_data(struct work_struct
*ws
)
417 struct mddev
*mddev
= container_of(ws
, struct mddev
, flush_work
);
418 struct bio
*bio
= mddev
->flush_bio
;
420 if (bio
->bi_iter
.bi_size
== 0)
421 /* an empty barrier - all done */
424 bio
->bi_rw
&= ~REQ_FLUSH
;
425 mddev
->pers
->make_request(mddev
, bio
);
428 mddev
->flush_bio
= NULL
;
429 wake_up(&mddev
->sb_wait
);
432 void md_flush_request(struct mddev
*mddev
, struct bio
*bio
)
434 spin_lock_irq(&mddev
->lock
);
435 wait_event_lock_irq(mddev
->sb_wait
,
438 mddev
->flush_bio
= bio
;
439 spin_unlock_irq(&mddev
->lock
);
441 INIT_WORK(&mddev
->flush_work
, submit_flushes
);
442 queue_work(md_wq
, &mddev
->flush_work
);
444 EXPORT_SYMBOL(md_flush_request
);
446 void md_unplug(struct blk_plug_cb
*cb
, bool from_schedule
)
448 struct mddev
*mddev
= cb
->data
;
449 md_wakeup_thread(mddev
->thread
);
452 EXPORT_SYMBOL(md_unplug
);
454 static inline struct mddev
*mddev_get(struct mddev
*mddev
)
456 atomic_inc(&mddev
->active
);
460 static void mddev_delayed_delete(struct work_struct
*ws
);
462 static void mddev_put(struct mddev
*mddev
)
464 struct bio_set
*bs
= NULL
;
466 if (!atomic_dec_and_lock(&mddev
->active
, &all_mddevs_lock
))
468 if (!mddev
->raid_disks
&& list_empty(&mddev
->disks
) &&
469 mddev
->ctime
== 0 && !mddev
->hold_active
) {
470 /* Array is not configured at all, and not held active,
472 list_del_init(&mddev
->all_mddevs
);
474 mddev
->bio_set
= NULL
;
475 if (mddev
->gendisk
) {
476 /* We did a probe so need to clean up. Call
477 * queue_work inside the spinlock so that
478 * flush_workqueue() after mddev_find will
479 * succeed in waiting for the work to be done.
481 INIT_WORK(&mddev
->del_work
, mddev_delayed_delete
);
482 queue_work(md_misc_wq
, &mddev
->del_work
);
486 spin_unlock(&all_mddevs_lock
);
491 static void md_safemode_timeout(unsigned long data
);
493 void mddev_init(struct mddev
*mddev
)
495 mutex_init(&mddev
->open_mutex
);
496 mutex_init(&mddev
->reconfig_mutex
);
497 mutex_init(&mddev
->bitmap_info
.mutex
);
498 INIT_LIST_HEAD(&mddev
->disks
);
499 INIT_LIST_HEAD(&mddev
->all_mddevs
);
500 setup_timer(&mddev
->safemode_timer
, md_safemode_timeout
,
501 (unsigned long) mddev
);
502 atomic_set(&mddev
->active
, 1);
503 atomic_set(&mddev
->openers
, 0);
504 atomic_set(&mddev
->active_io
, 0);
505 spin_lock_init(&mddev
->lock
);
506 atomic_set(&mddev
->flush_pending
, 0);
507 init_waitqueue_head(&mddev
->sb_wait
);
508 init_waitqueue_head(&mddev
->recovery_wait
);
509 mddev
->reshape_position
= MaxSector
;
510 mddev
->reshape_backwards
= 0;
511 mddev
->last_sync_action
= "none";
512 mddev
->resync_min
= 0;
513 mddev
->resync_max
= MaxSector
;
514 mddev
->level
= LEVEL_NONE
;
516 EXPORT_SYMBOL_GPL(mddev_init
);
518 static struct mddev
*mddev_find(dev_t unit
)
520 struct mddev
*mddev
, *new = NULL
;
522 if (unit
&& MAJOR(unit
) != MD_MAJOR
)
523 unit
&= ~((1<<MdpMinorShift
)-1);
526 spin_lock(&all_mddevs_lock
);
529 list_for_each_entry(mddev
, &all_mddevs
, all_mddevs
)
530 if (mddev
->unit
== unit
) {
532 spin_unlock(&all_mddevs_lock
);
538 list_add(&new->all_mddevs
, &all_mddevs
);
539 spin_unlock(&all_mddevs_lock
);
540 new->hold_active
= UNTIL_IOCTL
;
544 /* find an unused unit number */
545 static int next_minor
= 512;
546 int start
= next_minor
;
550 dev
= MKDEV(MD_MAJOR
, next_minor
);
552 if (next_minor
> MINORMASK
)
554 if (next_minor
== start
) {
555 /* Oh dear, all in use. */
556 spin_unlock(&all_mddevs_lock
);
562 list_for_each_entry(mddev
, &all_mddevs
, all_mddevs
)
563 if (mddev
->unit
== dev
) {
569 new->md_minor
= MINOR(dev
);
570 new->hold_active
= UNTIL_STOP
;
571 list_add(&new->all_mddevs
, &all_mddevs
);
572 spin_unlock(&all_mddevs_lock
);
575 spin_unlock(&all_mddevs_lock
);
577 new = kzalloc(sizeof(*new), GFP_KERNEL
);
582 if (MAJOR(unit
) == MD_MAJOR
)
583 new->md_minor
= MINOR(unit
);
585 new->md_minor
= MINOR(unit
) >> MdpMinorShift
;
592 static struct attribute_group md_redundancy_group
;
594 void mddev_unlock(struct mddev
*mddev
)
596 if (mddev
->to_remove
) {
597 /* These cannot be removed under reconfig_mutex as
598 * an access to the files will try to take reconfig_mutex
599 * while holding the file unremovable, which leads to
601 * So hold set sysfs_active while the remove in happeing,
602 * and anything else which might set ->to_remove or my
603 * otherwise change the sysfs namespace will fail with
604 * -EBUSY if sysfs_active is still set.
605 * We set sysfs_active under reconfig_mutex and elsewhere
606 * test it under the same mutex to ensure its correct value
609 struct attribute_group
*to_remove
= mddev
->to_remove
;
610 mddev
->to_remove
= NULL
;
611 mddev
->sysfs_active
= 1;
612 mutex_unlock(&mddev
->reconfig_mutex
);
614 if (mddev
->kobj
.sd
) {
615 if (to_remove
!= &md_redundancy_group
)
616 sysfs_remove_group(&mddev
->kobj
, to_remove
);
617 if (mddev
->pers
== NULL
||
618 mddev
->pers
->sync_request
== NULL
) {
619 sysfs_remove_group(&mddev
->kobj
, &md_redundancy_group
);
620 if (mddev
->sysfs_action
)
621 sysfs_put(mddev
->sysfs_action
);
622 mddev
->sysfs_action
= NULL
;
625 mddev
->sysfs_active
= 0;
627 mutex_unlock(&mddev
->reconfig_mutex
);
629 /* As we've dropped the mutex we need a spinlock to
630 * make sure the thread doesn't disappear
632 spin_lock(&pers_lock
);
633 md_wakeup_thread(mddev
->thread
);
634 spin_unlock(&pers_lock
);
636 EXPORT_SYMBOL_GPL(mddev_unlock
);
638 struct md_rdev
*md_find_rdev_nr_rcu(struct mddev
*mddev
, int nr
)
640 struct md_rdev
*rdev
;
642 rdev_for_each_rcu(rdev
, mddev
)
643 if (rdev
->desc_nr
== nr
)
648 EXPORT_SYMBOL_GPL(md_find_rdev_nr_rcu
);
650 static struct md_rdev
*find_rdev(struct mddev
*mddev
, dev_t dev
)
652 struct md_rdev
*rdev
;
654 rdev_for_each(rdev
, mddev
)
655 if (rdev
->bdev
->bd_dev
== dev
)
661 static struct md_rdev
*find_rdev_rcu(struct mddev
*mddev
, dev_t dev
)
663 struct md_rdev
*rdev
;
665 rdev_for_each_rcu(rdev
, mddev
)
666 if (rdev
->bdev
->bd_dev
== dev
)
672 static struct md_personality
*find_pers(int level
, char *clevel
)
674 struct md_personality
*pers
;
675 list_for_each_entry(pers
, &pers_list
, list
) {
676 if (level
!= LEVEL_NONE
&& pers
->level
== level
)
678 if (strcmp(pers
->name
, clevel
)==0)
684 /* return the offset of the super block in 512byte sectors */
685 static inline sector_t
calc_dev_sboffset(struct md_rdev
*rdev
)
687 sector_t num_sectors
= i_size_read(rdev
->bdev
->bd_inode
) / 512;
688 return MD_NEW_SIZE_SECTORS(num_sectors
);
691 static int alloc_disk_sb(struct md_rdev
*rdev
)
693 rdev
->sb_page
= alloc_page(GFP_KERNEL
);
694 if (!rdev
->sb_page
) {
695 printk(KERN_ALERT
"md: out of memory.\n");
702 void md_rdev_clear(struct md_rdev
*rdev
)
705 put_page(rdev
->sb_page
);
707 rdev
->sb_page
= NULL
;
712 put_page(rdev
->bb_page
);
713 rdev
->bb_page
= NULL
;
715 kfree(rdev
->badblocks
.page
);
716 rdev
->badblocks
.page
= NULL
;
718 EXPORT_SYMBOL_GPL(md_rdev_clear
);
720 static void super_written(struct bio
*bio
)
722 struct md_rdev
*rdev
= bio
->bi_private
;
723 struct mddev
*mddev
= rdev
->mddev
;
726 printk("md: super_written gets error=%d\n", bio
->bi_error
);
727 md_error(mddev
, rdev
);
730 if (atomic_dec_and_test(&mddev
->pending_writes
))
731 wake_up(&mddev
->sb_wait
);
735 void md_super_write(struct mddev
*mddev
, struct md_rdev
*rdev
,
736 sector_t sector
, int size
, struct page
*page
)
738 /* write first size bytes of page to sector of rdev
739 * Increment mddev->pending_writes before returning
740 * and decrement it on completion, waking up sb_wait
741 * if zero is reached.
742 * If an error occurred, call md_error
744 struct bio
*bio
= bio_alloc_mddev(GFP_NOIO
, 1, mddev
);
746 bio
->bi_bdev
= rdev
->meta_bdev
? rdev
->meta_bdev
: rdev
->bdev
;
747 bio
->bi_iter
.bi_sector
= sector
;
748 bio_add_page(bio
, page
, size
, 0);
749 bio
->bi_private
= rdev
;
750 bio
->bi_end_io
= super_written
;
752 atomic_inc(&mddev
->pending_writes
);
753 submit_bio(WRITE_FLUSH_FUA
, bio
);
756 void md_super_wait(struct mddev
*mddev
)
758 /* wait for all superblock writes that were scheduled to complete */
759 wait_event(mddev
->sb_wait
, atomic_read(&mddev
->pending_writes
)==0);
762 int sync_page_io(struct md_rdev
*rdev
, sector_t sector
, int size
,
763 struct page
*page
, int rw
, bool metadata_op
)
765 struct bio
*bio
= bio_alloc_mddev(GFP_NOIO
, 1, rdev
->mddev
);
768 bio
->bi_bdev
= (metadata_op
&& rdev
->meta_bdev
) ?
769 rdev
->meta_bdev
: rdev
->bdev
;
771 bio
->bi_iter
.bi_sector
= sector
+ rdev
->sb_start
;
772 else if (rdev
->mddev
->reshape_position
!= MaxSector
&&
773 (rdev
->mddev
->reshape_backwards
==
774 (sector
>= rdev
->mddev
->reshape_position
)))
775 bio
->bi_iter
.bi_sector
= sector
+ rdev
->new_data_offset
;
777 bio
->bi_iter
.bi_sector
= sector
+ rdev
->data_offset
;
778 bio_add_page(bio
, page
, size
, 0);
779 submit_bio_wait(rw
, bio
);
781 ret
= !bio
->bi_error
;
785 EXPORT_SYMBOL_GPL(sync_page_io
);
787 static int read_disk_sb(struct md_rdev
*rdev
, int size
)
789 char b
[BDEVNAME_SIZE
];
794 if (!sync_page_io(rdev
, 0, size
, rdev
->sb_page
, READ
, true))
800 printk(KERN_WARNING
"md: disabled device %s, could not read superblock.\n",
801 bdevname(rdev
->bdev
,b
));
805 static int uuid_equal(mdp_super_t
*sb1
, mdp_super_t
*sb2
)
807 return sb1
->set_uuid0
== sb2
->set_uuid0
&&
808 sb1
->set_uuid1
== sb2
->set_uuid1
&&
809 sb1
->set_uuid2
== sb2
->set_uuid2
&&
810 sb1
->set_uuid3
== sb2
->set_uuid3
;
813 static int sb_equal(mdp_super_t
*sb1
, mdp_super_t
*sb2
)
816 mdp_super_t
*tmp1
, *tmp2
;
818 tmp1
= kmalloc(sizeof(*tmp1
),GFP_KERNEL
);
819 tmp2
= kmalloc(sizeof(*tmp2
),GFP_KERNEL
);
821 if (!tmp1
|| !tmp2
) {
823 printk(KERN_INFO
"md.c sb_equal(): failed to allocate memory!\n");
831 * nr_disks is not constant
836 ret
= (memcmp(tmp1
, tmp2
, MD_SB_GENERIC_CONSTANT_WORDS
* 4) == 0);
843 static u32
md_csum_fold(u32 csum
)
845 csum
= (csum
& 0xffff) + (csum
>> 16);
846 return (csum
& 0xffff) + (csum
>> 16);
849 static unsigned int calc_sb_csum(mdp_super_t
*sb
)
852 u32
*sb32
= (u32
*)sb
;
854 unsigned int disk_csum
, csum
;
856 disk_csum
= sb
->sb_csum
;
859 for (i
= 0; i
< MD_SB_BYTES
/4 ; i
++)
861 csum
= (newcsum
& 0xffffffff) + (newcsum
>>32);
864 /* This used to use csum_partial, which was wrong for several
865 * reasons including that different results are returned on
866 * different architectures. It isn't critical that we get exactly
867 * the same return value as before (we always csum_fold before
868 * testing, and that removes any differences). However as we
869 * know that csum_partial always returned a 16bit value on
870 * alphas, do a fold to maximise conformity to previous behaviour.
872 sb
->sb_csum
= md_csum_fold(disk_csum
);
874 sb
->sb_csum
= disk_csum
;
880 * Handle superblock details.
881 * We want to be able to handle multiple superblock formats
882 * so we have a common interface to them all, and an array of
883 * different handlers.
884 * We rely on user-space to write the initial superblock, and support
885 * reading and updating of superblocks.
886 * Interface methods are:
887 * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
888 * loads and validates a superblock on dev.
889 * if refdev != NULL, compare superblocks on both devices
891 * 0 - dev has a superblock that is compatible with refdev
892 * 1 - dev has a superblock that is compatible and newer than refdev
893 * so dev should be used as the refdev in future
894 * -EINVAL superblock incompatible or invalid
895 * -othererror e.g. -EIO
897 * int validate_super(struct mddev *mddev, struct md_rdev *dev)
898 * Verify that dev is acceptable into mddev.
899 * The first time, mddev->raid_disks will be 0, and data from
900 * dev should be merged in. Subsequent calls check that dev
901 * is new enough. Return 0 or -EINVAL
903 * void sync_super(struct mddev *mddev, struct md_rdev *dev)
904 * Update the superblock for rdev with data in mddev
905 * This does not write to disc.
911 struct module
*owner
;
912 int (*load_super
)(struct md_rdev
*rdev
,
913 struct md_rdev
*refdev
,
915 int (*validate_super
)(struct mddev
*mddev
,
916 struct md_rdev
*rdev
);
917 void (*sync_super
)(struct mddev
*mddev
,
918 struct md_rdev
*rdev
);
919 unsigned long long (*rdev_size_change
)(struct md_rdev
*rdev
,
920 sector_t num_sectors
);
921 int (*allow_new_offset
)(struct md_rdev
*rdev
,
922 unsigned long long new_offset
);
926 * Check that the given mddev has no bitmap.
928 * This function is called from the run method of all personalities that do not
929 * support bitmaps. It prints an error message and returns non-zero if mddev
930 * has a bitmap. Otherwise, it returns 0.
933 int md_check_no_bitmap(struct mddev
*mddev
)
935 if (!mddev
->bitmap_info
.file
&& !mddev
->bitmap_info
.offset
)
937 printk(KERN_ERR
"%s: bitmaps are not supported for %s\n",
938 mdname(mddev
), mddev
->pers
->name
);
941 EXPORT_SYMBOL(md_check_no_bitmap
);
944 * load_super for 0.90.0
946 static int super_90_load(struct md_rdev
*rdev
, struct md_rdev
*refdev
, int minor_version
)
948 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
953 * Calculate the position of the superblock (512byte sectors),
954 * it's at the end of the disk.
956 * It also happens to be a multiple of 4Kb.
958 rdev
->sb_start
= calc_dev_sboffset(rdev
);
960 ret
= read_disk_sb(rdev
, MD_SB_BYTES
);
965 bdevname(rdev
->bdev
, b
);
966 sb
= page_address(rdev
->sb_page
);
968 if (sb
->md_magic
!= MD_SB_MAGIC
) {
969 printk(KERN_ERR
"md: invalid raid superblock magic on %s\n",
974 if (sb
->major_version
!= 0 ||
975 sb
->minor_version
< 90 ||
976 sb
->minor_version
> 91) {
977 printk(KERN_WARNING
"Bad version number %d.%d on %s\n",
978 sb
->major_version
, sb
->minor_version
,
983 if (sb
->raid_disks
<= 0)
986 if (md_csum_fold(calc_sb_csum(sb
)) != md_csum_fold(sb
->sb_csum
)) {
987 printk(KERN_WARNING
"md: invalid superblock checksum on %s\n",
992 rdev
->preferred_minor
= sb
->md_minor
;
993 rdev
->data_offset
= 0;
994 rdev
->new_data_offset
= 0;
995 rdev
->sb_size
= MD_SB_BYTES
;
996 rdev
->badblocks
.shift
= -1;
998 if (sb
->level
== LEVEL_MULTIPATH
)
1001 rdev
->desc_nr
= sb
->this_disk
.number
;
1007 mdp_super_t
*refsb
= page_address(refdev
->sb_page
);
1008 if (!uuid_equal(refsb
, sb
)) {
1009 printk(KERN_WARNING
"md: %s has different UUID to %s\n",
1010 b
, bdevname(refdev
->bdev
,b2
));
1013 if (!sb_equal(refsb
, sb
)) {
1014 printk(KERN_WARNING
"md: %s has same UUID"
1015 " but different superblock to %s\n",
1016 b
, bdevname(refdev
->bdev
, b2
));
1020 ev2
= md_event(refsb
);
1026 rdev
->sectors
= rdev
->sb_start
;
1027 /* Limit to 4TB as metadata cannot record more than that.
1028 * (not needed for Linear and RAID0 as metadata doesn't
1031 if (IS_ENABLED(CONFIG_LBDAF
) && (u64
)rdev
->sectors
>= (2ULL << 32) &&
1033 rdev
->sectors
= (sector_t
)(2ULL << 32) - 2;
1035 if (rdev
->sectors
< ((sector_t
)sb
->size
) * 2 && sb
->level
>= 1)
1036 /* "this cannot possibly happen" ... */
1044 * validate_super for 0.90.0
1046 static int super_90_validate(struct mddev
*mddev
, struct md_rdev
*rdev
)
1049 mdp_super_t
*sb
= page_address(rdev
->sb_page
);
1050 __u64 ev1
= md_event(sb
);
1052 rdev
->raid_disk
= -1;
1053 clear_bit(Faulty
, &rdev
->flags
);
1054 clear_bit(In_sync
, &rdev
->flags
);
1055 clear_bit(Bitmap_sync
, &rdev
->flags
);
1056 clear_bit(WriteMostly
, &rdev
->flags
);
1058 if (mddev
->raid_disks
== 0) {
1059 mddev
->major_version
= 0;
1060 mddev
->minor_version
= sb
->minor_version
;
1061 mddev
->patch_version
= sb
->patch_version
;
1062 mddev
->external
= 0;
1063 mddev
->chunk_sectors
= sb
->chunk_size
>> 9;
1064 mddev
->ctime
= sb
->ctime
;
1065 mddev
->utime
= sb
->utime
;
1066 mddev
->level
= sb
->level
;
1067 mddev
->clevel
[0] = 0;
1068 mddev
->layout
= sb
->layout
;
1069 mddev
->raid_disks
= sb
->raid_disks
;
1070 mddev
->dev_sectors
= ((sector_t
)sb
->size
) * 2;
1071 mddev
->events
= ev1
;
1072 mddev
->bitmap_info
.offset
= 0;
1073 mddev
->bitmap_info
.space
= 0;
1074 /* bitmap can use 60 K after the 4K superblocks */
1075 mddev
->bitmap_info
.default_offset
= MD_SB_BYTES
>> 9;
1076 mddev
->bitmap_info
.default_space
= 64*2 - (MD_SB_BYTES
>> 9);
1077 mddev
->reshape_backwards
= 0;
1079 if (mddev
->minor_version
>= 91) {
1080 mddev
->reshape_position
= sb
->reshape_position
;
1081 mddev
->delta_disks
= sb
->delta_disks
;
1082 mddev
->new_level
= sb
->new_level
;
1083 mddev
->new_layout
= sb
->new_layout
;
1084 mddev
->new_chunk_sectors
= sb
->new_chunk
>> 9;
1085 if (mddev
->delta_disks
< 0)
1086 mddev
->reshape_backwards
= 1;
1088 mddev
->reshape_position
= MaxSector
;
1089 mddev
->delta_disks
= 0;
1090 mddev
->new_level
= mddev
->level
;
1091 mddev
->new_layout
= mddev
->layout
;
1092 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
1095 if (sb
->state
& (1<<MD_SB_CLEAN
))
1096 mddev
->recovery_cp
= MaxSector
;
1098 if (sb
->events_hi
== sb
->cp_events_hi
&&
1099 sb
->events_lo
== sb
->cp_events_lo
) {
1100 mddev
->recovery_cp
= sb
->recovery_cp
;
1102 mddev
->recovery_cp
= 0;
1105 memcpy(mddev
->uuid
+0, &sb
->set_uuid0
, 4);
1106 memcpy(mddev
->uuid
+4, &sb
->set_uuid1
, 4);
1107 memcpy(mddev
->uuid
+8, &sb
->set_uuid2
, 4);
1108 memcpy(mddev
->uuid
+12,&sb
->set_uuid3
, 4);
1110 mddev
->max_disks
= MD_SB_DISKS
;
1112 if (sb
->state
& (1<<MD_SB_BITMAP_PRESENT
) &&
1113 mddev
->bitmap_info
.file
== NULL
) {
1114 mddev
->bitmap_info
.offset
=
1115 mddev
->bitmap_info
.default_offset
;
1116 mddev
->bitmap_info
.space
=
1117 mddev
->bitmap_info
.default_space
;
1120 } else if (mddev
->pers
== NULL
) {
1121 /* Insist on good event counter while assembling, except
1122 * for spares (which don't need an event count) */
1124 if (sb
->disks
[rdev
->desc_nr
].state
& (
1125 (1<<MD_DISK_SYNC
) | (1 << MD_DISK_ACTIVE
)))
1126 if (ev1
< mddev
->events
)
1128 } else if (mddev
->bitmap
) {
1129 /* if adding to array with a bitmap, then we can accept an
1130 * older device ... but not too old.
1132 if (ev1
< mddev
->bitmap
->events_cleared
)
1134 if (ev1
< mddev
->events
)
1135 set_bit(Bitmap_sync
, &rdev
->flags
);
1137 if (ev1
< mddev
->events
)
1138 /* just a hot-add of a new device, leave raid_disk at -1 */
1142 if (mddev
->level
!= LEVEL_MULTIPATH
) {
1143 desc
= sb
->disks
+ rdev
->desc_nr
;
1145 if (desc
->state
& (1<<MD_DISK_FAULTY
))
1146 set_bit(Faulty
, &rdev
->flags
);
1147 else if (desc
->state
& (1<<MD_DISK_SYNC
) /* &&
1148 desc->raid_disk < mddev->raid_disks */) {
1149 set_bit(In_sync
, &rdev
->flags
);
1150 rdev
->raid_disk
= desc
->raid_disk
;
1151 rdev
->saved_raid_disk
= desc
->raid_disk
;
1152 } else if (desc
->state
& (1<<MD_DISK_ACTIVE
)) {
1153 /* active but not in sync implies recovery up to
1154 * reshape position. We don't know exactly where
1155 * that is, so set to zero for now */
1156 if (mddev
->minor_version
>= 91) {
1157 rdev
->recovery_offset
= 0;
1158 rdev
->raid_disk
= desc
->raid_disk
;
1161 if (desc
->state
& (1<<MD_DISK_WRITEMOSTLY
))
1162 set_bit(WriteMostly
, &rdev
->flags
);
1163 } else /* MULTIPATH are always insync */
1164 set_bit(In_sync
, &rdev
->flags
);
1169 * sync_super for 0.90.0
1171 static void super_90_sync(struct mddev
*mddev
, struct md_rdev
*rdev
)
1174 struct md_rdev
*rdev2
;
1175 int next_spare
= mddev
->raid_disks
;
1177 /* make rdev->sb match mddev data..
1180 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1181 * 3/ any empty disks < next_spare become removed
1183 * disks[0] gets initialised to REMOVED because
1184 * we cannot be sure from other fields if it has
1185 * been initialised or not.
1188 int active
=0, working
=0,failed
=0,spare
=0,nr_disks
=0;
1190 rdev
->sb_size
= MD_SB_BYTES
;
1192 sb
= page_address(rdev
->sb_page
);
1194 memset(sb
, 0, sizeof(*sb
));
1196 sb
->md_magic
= MD_SB_MAGIC
;
1197 sb
->major_version
= mddev
->major_version
;
1198 sb
->patch_version
= mddev
->patch_version
;
1199 sb
->gvalid_words
= 0; /* ignored */
1200 memcpy(&sb
->set_uuid0
, mddev
->uuid
+0, 4);
1201 memcpy(&sb
->set_uuid1
, mddev
->uuid
+4, 4);
1202 memcpy(&sb
->set_uuid2
, mddev
->uuid
+8, 4);
1203 memcpy(&sb
->set_uuid3
, mddev
->uuid
+12,4);
1205 sb
->ctime
= mddev
->ctime
;
1206 sb
->level
= mddev
->level
;
1207 sb
->size
= mddev
->dev_sectors
/ 2;
1208 sb
->raid_disks
= mddev
->raid_disks
;
1209 sb
->md_minor
= mddev
->md_minor
;
1210 sb
->not_persistent
= 0;
1211 sb
->utime
= mddev
->utime
;
1213 sb
->events_hi
= (mddev
->events
>>32);
1214 sb
->events_lo
= (u32
)mddev
->events
;
1216 if (mddev
->reshape_position
== MaxSector
)
1217 sb
->minor_version
= 90;
1219 sb
->minor_version
= 91;
1220 sb
->reshape_position
= mddev
->reshape_position
;
1221 sb
->new_level
= mddev
->new_level
;
1222 sb
->delta_disks
= mddev
->delta_disks
;
1223 sb
->new_layout
= mddev
->new_layout
;
1224 sb
->new_chunk
= mddev
->new_chunk_sectors
<< 9;
1226 mddev
->minor_version
= sb
->minor_version
;
1229 sb
->recovery_cp
= mddev
->recovery_cp
;
1230 sb
->cp_events_hi
= (mddev
->events
>>32);
1231 sb
->cp_events_lo
= (u32
)mddev
->events
;
1232 if (mddev
->recovery_cp
== MaxSector
)
1233 sb
->state
= (1<< MD_SB_CLEAN
);
1235 sb
->recovery_cp
= 0;
1237 sb
->layout
= mddev
->layout
;
1238 sb
->chunk_size
= mddev
->chunk_sectors
<< 9;
1240 if (mddev
->bitmap
&& mddev
->bitmap_info
.file
== NULL
)
1241 sb
->state
|= (1<<MD_SB_BITMAP_PRESENT
);
1243 sb
->disks
[0].state
= (1<<MD_DISK_REMOVED
);
1244 rdev_for_each(rdev2
, mddev
) {
1247 int is_active
= test_bit(In_sync
, &rdev2
->flags
);
1249 if (rdev2
->raid_disk
>= 0 &&
1250 sb
->minor_version
>= 91)
1251 /* we have nowhere to store the recovery_offset,
1252 * but if it is not below the reshape_position,
1253 * we can piggy-back on that.
1256 if (rdev2
->raid_disk
< 0 ||
1257 test_bit(Faulty
, &rdev2
->flags
))
1260 desc_nr
= rdev2
->raid_disk
;
1262 desc_nr
= next_spare
++;
1263 rdev2
->desc_nr
= desc_nr
;
1264 d
= &sb
->disks
[rdev2
->desc_nr
];
1266 d
->number
= rdev2
->desc_nr
;
1267 d
->major
= MAJOR(rdev2
->bdev
->bd_dev
);
1268 d
->minor
= MINOR(rdev2
->bdev
->bd_dev
);
1270 d
->raid_disk
= rdev2
->raid_disk
;
1272 d
->raid_disk
= rdev2
->desc_nr
; /* compatibility */
1273 if (test_bit(Faulty
, &rdev2
->flags
))
1274 d
->state
= (1<<MD_DISK_FAULTY
);
1275 else if (is_active
) {
1276 d
->state
= (1<<MD_DISK_ACTIVE
);
1277 if (test_bit(In_sync
, &rdev2
->flags
))
1278 d
->state
|= (1<<MD_DISK_SYNC
);
1286 if (test_bit(WriteMostly
, &rdev2
->flags
))
1287 d
->state
|= (1<<MD_DISK_WRITEMOSTLY
);
1289 /* now set the "removed" and "faulty" bits on any missing devices */
1290 for (i
=0 ; i
< mddev
->raid_disks
; i
++) {
1291 mdp_disk_t
*d
= &sb
->disks
[i
];
1292 if (d
->state
== 0 && d
->number
== 0) {
1295 d
->state
= (1<<MD_DISK_REMOVED
);
1296 d
->state
|= (1<<MD_DISK_FAULTY
);
1300 sb
->nr_disks
= nr_disks
;
1301 sb
->active_disks
= active
;
1302 sb
->working_disks
= working
;
1303 sb
->failed_disks
= failed
;
1304 sb
->spare_disks
= spare
;
1306 sb
->this_disk
= sb
->disks
[rdev
->desc_nr
];
1307 sb
->sb_csum
= calc_sb_csum(sb
);
1311 * rdev_size_change for 0.90.0
1313 static unsigned long long
1314 super_90_rdev_size_change(struct md_rdev
*rdev
, sector_t num_sectors
)
1316 if (num_sectors
&& num_sectors
< rdev
->mddev
->dev_sectors
)
1317 return 0; /* component must fit device */
1318 if (rdev
->mddev
->bitmap_info
.offset
)
1319 return 0; /* can't move bitmap */
1320 rdev
->sb_start
= calc_dev_sboffset(rdev
);
1321 if (!num_sectors
|| num_sectors
> rdev
->sb_start
)
1322 num_sectors
= rdev
->sb_start
;
1323 /* Limit to 4TB as metadata cannot record more than that.
1324 * 4TB == 2^32 KB, or 2*2^32 sectors.
1326 if (IS_ENABLED(CONFIG_LBDAF
) && (u64
)num_sectors
>= (2ULL << 32) &&
1327 rdev
->mddev
->level
>= 1)
1328 num_sectors
= (sector_t
)(2ULL << 32) - 2;
1329 md_super_write(rdev
->mddev
, rdev
, rdev
->sb_start
, rdev
->sb_size
,
1331 md_super_wait(rdev
->mddev
);
1336 super_90_allow_new_offset(struct md_rdev
*rdev
, unsigned long long new_offset
)
1338 /* non-zero offset changes not possible with v0.90 */
1339 return new_offset
== 0;
1343 * version 1 superblock
1346 static __le32
calc_sb_1_csum(struct mdp_superblock_1
*sb
)
1350 unsigned long long newcsum
;
1351 int size
= 256 + le32_to_cpu(sb
->max_dev
)*2;
1352 __le32
*isuper
= (__le32
*)sb
;
1354 disk_csum
= sb
->sb_csum
;
1357 for (; size
>= 4; size
-= 4)
1358 newcsum
+= le32_to_cpu(*isuper
++);
1361 newcsum
+= le16_to_cpu(*(__le16
*) isuper
);
1363 csum
= (newcsum
& 0xffffffff) + (newcsum
>> 32);
1364 sb
->sb_csum
= disk_csum
;
1365 return cpu_to_le32(csum
);
1368 static int md_set_badblocks(struct badblocks
*bb
, sector_t s
, int sectors
,
1370 static int super_1_load(struct md_rdev
*rdev
, struct md_rdev
*refdev
, int minor_version
)
1372 struct mdp_superblock_1
*sb
;
1376 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
1380 * Calculate the position of the superblock in 512byte sectors.
1381 * It is always aligned to a 4K boundary and
1382 * depeding on minor_version, it can be:
1383 * 0: At least 8K, but less than 12K, from end of device
1384 * 1: At start of device
1385 * 2: 4K from start of device.
1387 switch(minor_version
) {
1389 sb_start
= i_size_read(rdev
->bdev
->bd_inode
) >> 9;
1391 sb_start
&= ~(sector_t
)(4*2-1);
1402 rdev
->sb_start
= sb_start
;
1404 /* superblock is rarely larger than 1K, but it can be larger,
1405 * and it is safe to read 4k, so we do that
1407 ret
= read_disk_sb(rdev
, 4096);
1408 if (ret
) return ret
;
1410 sb
= page_address(rdev
->sb_page
);
1412 if (sb
->magic
!= cpu_to_le32(MD_SB_MAGIC
) ||
1413 sb
->major_version
!= cpu_to_le32(1) ||
1414 le32_to_cpu(sb
->max_dev
) > (4096-256)/2 ||
1415 le64_to_cpu(sb
->super_offset
) != rdev
->sb_start
||
1416 (le32_to_cpu(sb
->feature_map
) & ~MD_FEATURE_ALL
) != 0)
1419 if (calc_sb_1_csum(sb
) != sb
->sb_csum
) {
1420 printk("md: invalid superblock checksum on %s\n",
1421 bdevname(rdev
->bdev
,b
));
1424 if (le64_to_cpu(sb
->data_size
) < 10) {
1425 printk("md: data_size too small on %s\n",
1426 bdevname(rdev
->bdev
,b
));
1431 memcmp(sb
->pad3
, sb
->pad3
+1, sizeof(sb
->pad3
) - sizeof(sb
->pad3
[1])))
1432 /* Some padding is non-zero, might be a new feature */
1435 rdev
->preferred_minor
= 0xffff;
1436 rdev
->data_offset
= le64_to_cpu(sb
->data_offset
);
1437 rdev
->new_data_offset
= rdev
->data_offset
;
1438 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RESHAPE_ACTIVE
) &&
1439 (le32_to_cpu(sb
->feature_map
) & MD_FEATURE_NEW_OFFSET
))
1440 rdev
->new_data_offset
+= (s32
)le32_to_cpu(sb
->new_offset
);
1441 atomic_set(&rdev
->corrected_errors
, le32_to_cpu(sb
->cnt_corrected_read
));
1443 rdev
->sb_size
= le32_to_cpu(sb
->max_dev
) * 2 + 256;
1444 bmask
= queue_logical_block_size(rdev
->bdev
->bd_disk
->queue
)-1;
1445 if (rdev
->sb_size
& bmask
)
1446 rdev
->sb_size
= (rdev
->sb_size
| bmask
) + 1;
1449 && rdev
->data_offset
< sb_start
+ (rdev
->sb_size
/512))
1452 && rdev
->new_data_offset
< sb_start
+ (rdev
->sb_size
/512))
1455 if (sb
->level
== cpu_to_le32(LEVEL_MULTIPATH
))
1458 rdev
->desc_nr
= le32_to_cpu(sb
->dev_number
);
1460 if (!rdev
->bb_page
) {
1461 rdev
->bb_page
= alloc_page(GFP_KERNEL
);
1465 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_BAD_BLOCKS
) &&
1466 rdev
->badblocks
.count
== 0) {
1467 /* need to load the bad block list.
1468 * Currently we limit it to one page.
1474 int sectors
= le16_to_cpu(sb
->bblog_size
);
1475 if (sectors
> (PAGE_SIZE
/ 512))
1477 offset
= le32_to_cpu(sb
->bblog_offset
);
1480 bb_sector
= (long long)offset
;
1481 if (!sync_page_io(rdev
, bb_sector
, sectors
<< 9,
1482 rdev
->bb_page
, READ
, true))
1484 bbp
= (u64
*)page_address(rdev
->bb_page
);
1485 rdev
->badblocks
.shift
= sb
->bblog_shift
;
1486 for (i
= 0 ; i
< (sectors
<< (9-3)) ; i
++, bbp
++) {
1487 u64 bb
= le64_to_cpu(*bbp
);
1488 int count
= bb
& (0x3ff);
1489 u64 sector
= bb
>> 10;
1490 sector
<<= sb
->bblog_shift
;
1491 count
<<= sb
->bblog_shift
;
1494 if (md_set_badblocks(&rdev
->badblocks
,
1495 sector
, count
, 1) == 0)
1498 } else if (sb
->bblog_offset
!= 0)
1499 rdev
->badblocks
.shift
= 0;
1505 struct mdp_superblock_1
*refsb
= page_address(refdev
->sb_page
);
1507 if (memcmp(sb
->set_uuid
, refsb
->set_uuid
, 16) != 0 ||
1508 sb
->level
!= refsb
->level
||
1509 sb
->layout
!= refsb
->layout
||
1510 sb
->chunksize
!= refsb
->chunksize
) {
1511 printk(KERN_WARNING
"md: %s has strangely different"
1512 " superblock to %s\n",
1513 bdevname(rdev
->bdev
,b
),
1514 bdevname(refdev
->bdev
,b2
));
1517 ev1
= le64_to_cpu(sb
->events
);
1518 ev2
= le64_to_cpu(refsb
->events
);
1525 if (minor_version
) {
1526 sectors
= (i_size_read(rdev
->bdev
->bd_inode
) >> 9);
1527 sectors
-= rdev
->data_offset
;
1529 sectors
= rdev
->sb_start
;
1530 if (sectors
< le64_to_cpu(sb
->data_size
))
1532 rdev
->sectors
= le64_to_cpu(sb
->data_size
);
1536 static int super_1_validate(struct mddev
*mddev
, struct md_rdev
*rdev
)
1538 struct mdp_superblock_1
*sb
= page_address(rdev
->sb_page
);
1539 __u64 ev1
= le64_to_cpu(sb
->events
);
1541 rdev
->raid_disk
= -1;
1542 clear_bit(Faulty
, &rdev
->flags
);
1543 clear_bit(In_sync
, &rdev
->flags
);
1544 clear_bit(Bitmap_sync
, &rdev
->flags
);
1545 clear_bit(WriteMostly
, &rdev
->flags
);
1547 if (mddev
->raid_disks
== 0) {
1548 mddev
->major_version
= 1;
1549 mddev
->patch_version
= 0;
1550 mddev
->external
= 0;
1551 mddev
->chunk_sectors
= le32_to_cpu(sb
->chunksize
);
1552 mddev
->ctime
= le64_to_cpu(sb
->ctime
) & ((1ULL << 32)-1);
1553 mddev
->utime
= le64_to_cpu(sb
->utime
) & ((1ULL << 32)-1);
1554 mddev
->level
= le32_to_cpu(sb
->level
);
1555 mddev
->clevel
[0] = 0;
1556 mddev
->layout
= le32_to_cpu(sb
->layout
);
1557 mddev
->raid_disks
= le32_to_cpu(sb
->raid_disks
);
1558 mddev
->dev_sectors
= le64_to_cpu(sb
->size
);
1559 mddev
->events
= ev1
;
1560 mddev
->bitmap_info
.offset
= 0;
1561 mddev
->bitmap_info
.space
= 0;
1562 /* Default location for bitmap is 1K after superblock
1563 * using 3K - total of 4K
1565 mddev
->bitmap_info
.default_offset
= 1024 >> 9;
1566 mddev
->bitmap_info
.default_space
= (4096-1024) >> 9;
1567 mddev
->reshape_backwards
= 0;
1569 mddev
->recovery_cp
= le64_to_cpu(sb
->resync_offset
);
1570 memcpy(mddev
->uuid
, sb
->set_uuid
, 16);
1572 mddev
->max_disks
= (4096-256)/2;
1574 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_BITMAP_OFFSET
) &&
1575 mddev
->bitmap_info
.file
== NULL
) {
1576 mddev
->bitmap_info
.offset
=
1577 (__s32
)le32_to_cpu(sb
->bitmap_offset
);
1578 /* Metadata doesn't record how much space is available.
1579 * For 1.0, we assume we can use up to the superblock
1580 * if before, else to 4K beyond superblock.
1581 * For others, assume no change is possible.
1583 if (mddev
->minor_version
> 0)
1584 mddev
->bitmap_info
.space
= 0;
1585 else if (mddev
->bitmap_info
.offset
> 0)
1586 mddev
->bitmap_info
.space
=
1587 8 - mddev
->bitmap_info
.offset
;
1589 mddev
->bitmap_info
.space
=
1590 -mddev
->bitmap_info
.offset
;
1593 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RESHAPE_ACTIVE
)) {
1594 mddev
->reshape_position
= le64_to_cpu(sb
->reshape_position
);
1595 mddev
->delta_disks
= le32_to_cpu(sb
->delta_disks
);
1596 mddev
->new_level
= le32_to_cpu(sb
->new_level
);
1597 mddev
->new_layout
= le32_to_cpu(sb
->new_layout
);
1598 mddev
->new_chunk_sectors
= le32_to_cpu(sb
->new_chunk
);
1599 if (mddev
->delta_disks
< 0 ||
1600 (mddev
->delta_disks
== 0 &&
1601 (le32_to_cpu(sb
->feature_map
)
1602 & MD_FEATURE_RESHAPE_BACKWARDS
)))
1603 mddev
->reshape_backwards
= 1;
1605 mddev
->reshape_position
= MaxSector
;
1606 mddev
->delta_disks
= 0;
1607 mddev
->new_level
= mddev
->level
;
1608 mddev
->new_layout
= mddev
->layout
;
1609 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
1612 } else if (mddev
->pers
== NULL
) {
1613 /* Insist of good event counter while assembling, except for
1614 * spares (which don't need an event count) */
1616 if (rdev
->desc_nr
>= 0 &&
1617 rdev
->desc_nr
< le32_to_cpu(sb
->max_dev
) &&
1618 (le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]) < MD_DISK_ROLE_MAX
||
1619 le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]) == MD_DISK_ROLE_JOURNAL
))
1620 if (ev1
< mddev
->events
)
1622 } else if (mddev
->bitmap
) {
1623 /* If adding to array with a bitmap, then we can accept an
1624 * older device, but not too old.
1626 if (ev1
< mddev
->bitmap
->events_cleared
)
1628 if (ev1
< mddev
->events
)
1629 set_bit(Bitmap_sync
, &rdev
->flags
);
1631 if (ev1
< mddev
->events
)
1632 /* just a hot-add of a new device, leave raid_disk at -1 */
1635 if (mddev
->level
!= LEVEL_MULTIPATH
) {
1637 if (rdev
->desc_nr
< 0 ||
1638 rdev
->desc_nr
>= le32_to_cpu(sb
->max_dev
)) {
1639 role
= MD_DISK_ROLE_SPARE
;
1642 role
= le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]);
1644 case MD_DISK_ROLE_SPARE
: /* spare */
1646 case MD_DISK_ROLE_FAULTY
: /* faulty */
1647 set_bit(Faulty
, &rdev
->flags
);
1649 case MD_DISK_ROLE_JOURNAL
: /* journal device */
1650 if (!(le32_to_cpu(sb
->feature_map
) & MD_FEATURE_JOURNAL
)) {
1651 /* journal device without journal feature */
1653 "md: journal device provided without journal feature, ignoring the device\n");
1656 set_bit(Journal
, &rdev
->flags
);
1657 rdev
->journal_tail
= le64_to_cpu(sb
->journal_tail
);
1658 if (mddev
->recovery_cp
== MaxSector
)
1659 set_bit(MD_JOURNAL_CLEAN
, &mddev
->flags
);
1660 rdev
->raid_disk
= 0;
1663 rdev
->saved_raid_disk
= role
;
1664 if ((le32_to_cpu(sb
->feature_map
) &
1665 MD_FEATURE_RECOVERY_OFFSET
)) {
1666 rdev
->recovery_offset
= le64_to_cpu(sb
->recovery_offset
);
1667 if (!(le32_to_cpu(sb
->feature_map
) &
1668 MD_FEATURE_RECOVERY_BITMAP
))
1669 rdev
->saved_raid_disk
= -1;
1671 set_bit(In_sync
, &rdev
->flags
);
1672 rdev
->raid_disk
= role
;
1675 if (sb
->devflags
& WriteMostly1
)
1676 set_bit(WriteMostly
, &rdev
->flags
);
1677 if (le32_to_cpu(sb
->feature_map
) & MD_FEATURE_REPLACEMENT
)
1678 set_bit(Replacement
, &rdev
->flags
);
1679 if (le32_to_cpu(sb
->feature_map
) & MD_FEATURE_JOURNAL
)
1680 set_bit(MD_HAS_JOURNAL
, &mddev
->flags
);
1681 } else /* MULTIPATH are always insync */
1682 set_bit(In_sync
, &rdev
->flags
);
1687 static void super_1_sync(struct mddev
*mddev
, struct md_rdev
*rdev
)
1689 struct mdp_superblock_1
*sb
;
1690 struct md_rdev
*rdev2
;
1692 /* make rdev->sb match mddev and rdev data. */
1694 sb
= page_address(rdev
->sb_page
);
1696 sb
->feature_map
= 0;
1698 sb
->recovery_offset
= cpu_to_le64(0);
1699 memset(sb
->pad3
, 0, sizeof(sb
->pad3
));
1701 sb
->utime
= cpu_to_le64((__u64
)mddev
->utime
);
1702 sb
->events
= cpu_to_le64(mddev
->events
);
1704 sb
->resync_offset
= cpu_to_le64(mddev
->recovery_cp
);
1705 else if (test_bit(MD_JOURNAL_CLEAN
, &mddev
->flags
))
1706 sb
->resync_offset
= cpu_to_le64(MaxSector
);
1708 sb
->resync_offset
= cpu_to_le64(0);
1710 sb
->cnt_corrected_read
= cpu_to_le32(atomic_read(&rdev
->corrected_errors
));
1712 sb
->raid_disks
= cpu_to_le32(mddev
->raid_disks
);
1713 sb
->size
= cpu_to_le64(mddev
->dev_sectors
);
1714 sb
->chunksize
= cpu_to_le32(mddev
->chunk_sectors
);
1715 sb
->level
= cpu_to_le32(mddev
->level
);
1716 sb
->layout
= cpu_to_le32(mddev
->layout
);
1718 if (test_bit(WriteMostly
, &rdev
->flags
))
1719 sb
->devflags
|= WriteMostly1
;
1721 sb
->devflags
&= ~WriteMostly1
;
1722 sb
->data_offset
= cpu_to_le64(rdev
->data_offset
);
1723 sb
->data_size
= cpu_to_le64(rdev
->sectors
);
1725 if (mddev
->bitmap
&& mddev
->bitmap_info
.file
== NULL
) {
1726 sb
->bitmap_offset
= cpu_to_le32((__u32
)mddev
->bitmap_info
.offset
);
1727 sb
->feature_map
= cpu_to_le32(MD_FEATURE_BITMAP_OFFSET
);
1730 if (rdev
->raid_disk
>= 0 && !test_bit(Journal
, &rdev
->flags
) &&
1731 !test_bit(In_sync
, &rdev
->flags
)) {
1733 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET
);
1734 sb
->recovery_offset
=
1735 cpu_to_le64(rdev
->recovery_offset
);
1736 if (rdev
->saved_raid_disk
>= 0 && mddev
->bitmap
)
1738 cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP
);
1740 /* Note: recovery_offset and journal_tail share space */
1741 if (test_bit(Journal
, &rdev
->flags
))
1742 sb
->journal_tail
= cpu_to_le64(rdev
->journal_tail
);
1743 if (test_bit(Replacement
, &rdev
->flags
))
1745 cpu_to_le32(MD_FEATURE_REPLACEMENT
);
1747 if (mddev
->reshape_position
!= MaxSector
) {
1748 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE
);
1749 sb
->reshape_position
= cpu_to_le64(mddev
->reshape_position
);
1750 sb
->new_layout
= cpu_to_le32(mddev
->new_layout
);
1751 sb
->delta_disks
= cpu_to_le32(mddev
->delta_disks
);
1752 sb
->new_level
= cpu_to_le32(mddev
->new_level
);
1753 sb
->new_chunk
= cpu_to_le32(mddev
->new_chunk_sectors
);
1754 if (mddev
->delta_disks
== 0 &&
1755 mddev
->reshape_backwards
)
1757 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS
);
1758 if (rdev
->new_data_offset
!= rdev
->data_offset
) {
1760 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET
);
1761 sb
->new_offset
= cpu_to_le32((__u32
)(rdev
->new_data_offset
1762 - rdev
->data_offset
));
1766 if (mddev_is_clustered(mddev
))
1767 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_CLUSTERED
);
1769 if (rdev
->badblocks
.count
== 0)
1770 /* Nothing to do for bad blocks*/ ;
1771 else if (sb
->bblog_offset
== 0)
1772 /* Cannot record bad blocks on this device */
1773 md_error(mddev
, rdev
);
1775 struct badblocks
*bb
= &rdev
->badblocks
;
1776 u64
*bbp
= (u64
*)page_address(rdev
->bb_page
);
1778 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_BAD_BLOCKS
);
1783 seq
= read_seqbegin(&bb
->lock
);
1785 memset(bbp
, 0xff, PAGE_SIZE
);
1787 for (i
= 0 ; i
< bb
->count
; i
++) {
1788 u64 internal_bb
= p
[i
];
1789 u64 store_bb
= ((BB_OFFSET(internal_bb
) << 10)
1790 | BB_LEN(internal_bb
));
1791 bbp
[i
] = cpu_to_le64(store_bb
);
1794 if (read_seqretry(&bb
->lock
, seq
))
1797 bb
->sector
= (rdev
->sb_start
+
1798 (int)le32_to_cpu(sb
->bblog_offset
));
1799 bb
->size
= le16_to_cpu(sb
->bblog_size
);
1804 rdev_for_each(rdev2
, mddev
)
1805 if (rdev2
->desc_nr
+1 > max_dev
)
1806 max_dev
= rdev2
->desc_nr
+1;
1808 if (max_dev
> le32_to_cpu(sb
->max_dev
)) {
1810 sb
->max_dev
= cpu_to_le32(max_dev
);
1811 rdev
->sb_size
= max_dev
* 2 + 256;
1812 bmask
= queue_logical_block_size(rdev
->bdev
->bd_disk
->queue
)-1;
1813 if (rdev
->sb_size
& bmask
)
1814 rdev
->sb_size
= (rdev
->sb_size
| bmask
) + 1;
1816 max_dev
= le32_to_cpu(sb
->max_dev
);
1818 for (i
=0; i
<max_dev
;i
++)
1819 sb
->dev_roles
[i
] = cpu_to_le16(MD_DISK_ROLE_FAULTY
);
1821 if (test_bit(MD_HAS_JOURNAL
, &mddev
->flags
))
1822 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_JOURNAL
);
1824 rdev_for_each(rdev2
, mddev
) {
1826 if (test_bit(Faulty
, &rdev2
->flags
))
1827 sb
->dev_roles
[i
] = cpu_to_le16(MD_DISK_ROLE_FAULTY
);
1828 else if (test_bit(In_sync
, &rdev2
->flags
))
1829 sb
->dev_roles
[i
] = cpu_to_le16(rdev2
->raid_disk
);
1830 else if (test_bit(Journal
, &rdev2
->flags
))
1831 sb
->dev_roles
[i
] = cpu_to_le16(MD_DISK_ROLE_JOURNAL
);
1832 else if (rdev2
->raid_disk
>= 0)
1833 sb
->dev_roles
[i
] = cpu_to_le16(rdev2
->raid_disk
);
1835 sb
->dev_roles
[i
] = cpu_to_le16(MD_DISK_ROLE_SPARE
);
1838 sb
->sb_csum
= calc_sb_1_csum(sb
);
1841 static unsigned long long
1842 super_1_rdev_size_change(struct md_rdev
*rdev
, sector_t num_sectors
)
1844 struct mdp_superblock_1
*sb
;
1845 sector_t max_sectors
;
1846 if (num_sectors
&& num_sectors
< rdev
->mddev
->dev_sectors
)
1847 return 0; /* component must fit device */
1848 if (rdev
->data_offset
!= rdev
->new_data_offset
)
1849 return 0; /* too confusing */
1850 if (rdev
->sb_start
< rdev
->data_offset
) {
1851 /* minor versions 1 and 2; superblock before data */
1852 max_sectors
= i_size_read(rdev
->bdev
->bd_inode
) >> 9;
1853 max_sectors
-= rdev
->data_offset
;
1854 if (!num_sectors
|| num_sectors
> max_sectors
)
1855 num_sectors
= max_sectors
;
1856 } else if (rdev
->mddev
->bitmap_info
.offset
) {
1857 /* minor version 0 with bitmap we can't move */
1860 /* minor version 0; superblock after data */
1862 sb_start
= (i_size_read(rdev
->bdev
->bd_inode
) >> 9) - 8*2;
1863 sb_start
&= ~(sector_t
)(4*2 - 1);
1864 max_sectors
= rdev
->sectors
+ sb_start
- rdev
->sb_start
;
1865 if (!num_sectors
|| num_sectors
> max_sectors
)
1866 num_sectors
= max_sectors
;
1867 rdev
->sb_start
= sb_start
;
1869 sb
= page_address(rdev
->sb_page
);
1870 sb
->data_size
= cpu_to_le64(num_sectors
);
1871 sb
->super_offset
= cpu_to_le64(rdev
->sb_start
);
1872 sb
->sb_csum
= calc_sb_1_csum(sb
);
1873 md_super_write(rdev
->mddev
, rdev
, rdev
->sb_start
, rdev
->sb_size
,
1875 md_super_wait(rdev
->mddev
);
1881 super_1_allow_new_offset(struct md_rdev
*rdev
,
1882 unsigned long long new_offset
)
1884 /* All necessary checks on new >= old have been done */
1885 struct bitmap
*bitmap
;
1886 if (new_offset
>= rdev
->data_offset
)
1889 /* with 1.0 metadata, there is no metadata to tread on
1890 * so we can always move back */
1891 if (rdev
->mddev
->minor_version
== 0)
1894 /* otherwise we must be sure not to step on
1895 * any metadata, so stay:
1896 * 36K beyond start of superblock
1897 * beyond end of badblocks
1898 * beyond write-intent bitmap
1900 if (rdev
->sb_start
+ (32+4)*2 > new_offset
)
1902 bitmap
= rdev
->mddev
->bitmap
;
1903 if (bitmap
&& !rdev
->mddev
->bitmap_info
.file
&&
1904 rdev
->sb_start
+ rdev
->mddev
->bitmap_info
.offset
+
1905 bitmap
->storage
.file_pages
* (PAGE_SIZE
>>9) > new_offset
)
1907 if (rdev
->badblocks
.sector
+ rdev
->badblocks
.size
> new_offset
)
1913 static struct super_type super_types
[] = {
1916 .owner
= THIS_MODULE
,
1917 .load_super
= super_90_load
,
1918 .validate_super
= super_90_validate
,
1919 .sync_super
= super_90_sync
,
1920 .rdev_size_change
= super_90_rdev_size_change
,
1921 .allow_new_offset
= super_90_allow_new_offset
,
1925 .owner
= THIS_MODULE
,
1926 .load_super
= super_1_load
,
1927 .validate_super
= super_1_validate
,
1928 .sync_super
= super_1_sync
,
1929 .rdev_size_change
= super_1_rdev_size_change
,
1930 .allow_new_offset
= super_1_allow_new_offset
,
1934 static void sync_super(struct mddev
*mddev
, struct md_rdev
*rdev
)
1936 if (mddev
->sync_super
) {
1937 mddev
->sync_super(mddev
, rdev
);
1941 BUG_ON(mddev
->major_version
>= ARRAY_SIZE(super_types
));
1943 super_types
[mddev
->major_version
].sync_super(mddev
, rdev
);
1946 static int match_mddev_units(struct mddev
*mddev1
, struct mddev
*mddev2
)
1948 struct md_rdev
*rdev
, *rdev2
;
1951 rdev_for_each_rcu(rdev
, mddev1
) {
1952 if (test_bit(Faulty
, &rdev
->flags
) ||
1953 test_bit(Journal
, &rdev
->flags
) ||
1954 rdev
->raid_disk
== -1)
1956 rdev_for_each_rcu(rdev2
, mddev2
) {
1957 if (test_bit(Faulty
, &rdev2
->flags
) ||
1958 test_bit(Journal
, &rdev2
->flags
) ||
1959 rdev2
->raid_disk
== -1)
1961 if (rdev
->bdev
->bd_contains
==
1962 rdev2
->bdev
->bd_contains
) {
1972 static LIST_HEAD(pending_raid_disks
);
1975 * Try to register data integrity profile for an mddev
1977 * This is called when an array is started and after a disk has been kicked
1978 * from the array. It only succeeds if all working and active component devices
1979 * are integrity capable with matching profiles.
1981 int md_integrity_register(struct mddev
*mddev
)
1983 struct md_rdev
*rdev
, *reference
= NULL
;
1985 if (list_empty(&mddev
->disks
))
1986 return 0; /* nothing to do */
1987 if (!mddev
->gendisk
|| blk_get_integrity(mddev
->gendisk
))
1988 return 0; /* shouldn't register, or already is */
1989 rdev_for_each(rdev
, mddev
) {
1990 /* skip spares and non-functional disks */
1991 if (test_bit(Faulty
, &rdev
->flags
))
1993 if (rdev
->raid_disk
< 0)
1996 /* Use the first rdev as the reference */
2000 /* does this rdev's profile match the reference profile? */
2001 if (blk_integrity_compare(reference
->bdev
->bd_disk
,
2002 rdev
->bdev
->bd_disk
) < 0)
2005 if (!reference
|| !bdev_get_integrity(reference
->bdev
))
2008 * All component devices are integrity capable and have matching
2009 * profiles, register the common profile for the md device.
2011 blk_integrity_register(mddev
->gendisk
,
2012 bdev_get_integrity(reference
->bdev
));
2014 printk(KERN_NOTICE
"md: data integrity enabled on %s\n", mdname(mddev
));
2015 if (bioset_integrity_create(mddev
->bio_set
, BIO_POOL_SIZE
)) {
2016 printk(KERN_ERR
"md: failed to create integrity pool for %s\n",
2022 EXPORT_SYMBOL(md_integrity_register
);
2025 * Attempt to add an rdev, but only if it is consistent with the current
2028 int md_integrity_add_rdev(struct md_rdev
*rdev
, struct mddev
*mddev
)
2030 struct blk_integrity
*bi_rdev
;
2031 struct blk_integrity
*bi_mddev
;
2032 char name
[BDEVNAME_SIZE
];
2034 if (!mddev
->gendisk
)
2037 bi_rdev
= bdev_get_integrity(rdev
->bdev
);
2038 bi_mddev
= blk_get_integrity(mddev
->gendisk
);
2040 if (!bi_mddev
) /* nothing to do */
2043 if (blk_integrity_compare(mddev
->gendisk
, rdev
->bdev
->bd_disk
) != 0) {
2044 printk(KERN_NOTICE
"%s: incompatible integrity profile for %s\n",
2045 mdname(mddev
), bdevname(rdev
->bdev
, name
));
2051 EXPORT_SYMBOL(md_integrity_add_rdev
);
2053 static int bind_rdev_to_array(struct md_rdev
*rdev
, struct mddev
*mddev
)
2055 char b
[BDEVNAME_SIZE
];
2059 /* prevent duplicates */
2060 if (find_rdev(mddev
, rdev
->bdev
->bd_dev
))
2063 /* make sure rdev->sectors exceeds mddev->dev_sectors */
2064 if (rdev
->sectors
&& (mddev
->dev_sectors
== 0 ||
2065 rdev
->sectors
< mddev
->dev_sectors
)) {
2067 /* Cannot change size, so fail
2068 * If mddev->level <= 0, then we don't care
2069 * about aligning sizes (e.g. linear)
2071 if (mddev
->level
> 0)
2074 mddev
->dev_sectors
= rdev
->sectors
;
2077 /* Verify rdev->desc_nr is unique.
2078 * If it is -1, assign a free number, else
2079 * check number is not in use
2082 if (rdev
->desc_nr
< 0) {
2085 choice
= mddev
->raid_disks
;
2086 while (md_find_rdev_nr_rcu(mddev
, choice
))
2088 rdev
->desc_nr
= choice
;
2090 if (md_find_rdev_nr_rcu(mddev
, rdev
->desc_nr
)) {
2096 if (mddev
->max_disks
&& rdev
->desc_nr
>= mddev
->max_disks
) {
2097 printk(KERN_WARNING
"md: %s: array is limited to %d devices\n",
2098 mdname(mddev
), mddev
->max_disks
);
2101 bdevname(rdev
->bdev
,b
);
2102 strreplace(b
, '/', '!');
2104 rdev
->mddev
= mddev
;
2105 printk(KERN_INFO
"md: bind<%s>\n", b
);
2107 if ((err
= kobject_add(&rdev
->kobj
, &mddev
->kobj
, "dev-%s", b
)))
2110 ko
= &part_to_dev(rdev
->bdev
->bd_part
)->kobj
;
2111 if (sysfs_create_link(&rdev
->kobj
, ko
, "block"))
2112 /* failure here is OK */;
2113 rdev
->sysfs_state
= sysfs_get_dirent_safe(rdev
->kobj
.sd
, "state");
2115 list_add_rcu(&rdev
->same_set
, &mddev
->disks
);
2116 bd_link_disk_holder(rdev
->bdev
, mddev
->gendisk
);
2118 /* May as well allow recovery to be retried once */
2119 mddev
->recovery_disabled
++;
2124 printk(KERN_WARNING
"md: failed to register dev-%s for %s\n",
2129 static void md_delayed_delete(struct work_struct
*ws
)
2131 struct md_rdev
*rdev
= container_of(ws
, struct md_rdev
, del_work
);
2132 kobject_del(&rdev
->kobj
);
2133 kobject_put(&rdev
->kobj
);
2136 static void unbind_rdev_from_array(struct md_rdev
*rdev
)
2138 char b
[BDEVNAME_SIZE
];
2140 bd_unlink_disk_holder(rdev
->bdev
, rdev
->mddev
->gendisk
);
2141 list_del_rcu(&rdev
->same_set
);
2142 printk(KERN_INFO
"md: unbind<%s>\n", bdevname(rdev
->bdev
,b
));
2144 sysfs_remove_link(&rdev
->kobj
, "block");
2145 sysfs_put(rdev
->sysfs_state
);
2146 rdev
->sysfs_state
= NULL
;
2147 rdev
->badblocks
.count
= 0;
2148 /* We need to delay this, otherwise we can deadlock when
2149 * writing to 'remove' to "dev/state". We also need
2150 * to delay it due to rcu usage.
2153 INIT_WORK(&rdev
->del_work
, md_delayed_delete
);
2154 kobject_get(&rdev
->kobj
);
2155 queue_work(md_misc_wq
, &rdev
->del_work
);
2159 * prevent the device from being mounted, repartitioned or
2160 * otherwise reused by a RAID array (or any other kernel
2161 * subsystem), by bd_claiming the device.
2163 static int lock_rdev(struct md_rdev
*rdev
, dev_t dev
, int shared
)
2166 struct block_device
*bdev
;
2167 char b
[BDEVNAME_SIZE
];
2169 bdev
= blkdev_get_by_dev(dev
, FMODE_READ
|FMODE_WRITE
|FMODE_EXCL
,
2170 shared
? (struct md_rdev
*)lock_rdev
: rdev
);
2172 printk(KERN_ERR
"md: could not open %s.\n",
2173 __bdevname(dev
, b
));
2174 return PTR_ERR(bdev
);
2180 static void unlock_rdev(struct md_rdev
*rdev
)
2182 struct block_device
*bdev
= rdev
->bdev
;
2184 blkdev_put(bdev
, FMODE_READ
|FMODE_WRITE
|FMODE_EXCL
);
2187 void md_autodetect_dev(dev_t dev
);
2189 static void export_rdev(struct md_rdev
*rdev
)
2191 char b
[BDEVNAME_SIZE
];
2193 printk(KERN_INFO
"md: export_rdev(%s)\n",
2194 bdevname(rdev
->bdev
,b
));
2195 md_rdev_clear(rdev
);
2197 if (test_bit(AutoDetected
, &rdev
->flags
))
2198 md_autodetect_dev(rdev
->bdev
->bd_dev
);
2201 kobject_put(&rdev
->kobj
);
2204 void md_kick_rdev_from_array(struct md_rdev
*rdev
)
2206 unbind_rdev_from_array(rdev
);
2209 EXPORT_SYMBOL_GPL(md_kick_rdev_from_array
);
2211 static void export_array(struct mddev
*mddev
)
2213 struct md_rdev
*rdev
;
2215 while (!list_empty(&mddev
->disks
)) {
2216 rdev
= list_first_entry(&mddev
->disks
, struct md_rdev
,
2218 md_kick_rdev_from_array(rdev
);
2220 mddev
->raid_disks
= 0;
2221 mddev
->major_version
= 0;
2224 static void sync_sbs(struct mddev
*mddev
, int nospares
)
2226 /* Update each superblock (in-memory image), but
2227 * if we are allowed to, skip spares which already
2228 * have the right event counter, or have one earlier
2229 * (which would mean they aren't being marked as dirty
2230 * with the rest of the array)
2232 struct md_rdev
*rdev
;
2233 rdev_for_each(rdev
, mddev
) {
2234 if (rdev
->sb_events
== mddev
->events
||
2236 rdev
->raid_disk
< 0 &&
2237 rdev
->sb_events
+1 == mddev
->events
)) {
2238 /* Don't update this superblock */
2239 rdev
->sb_loaded
= 2;
2241 sync_super(mddev
, rdev
);
2242 rdev
->sb_loaded
= 1;
2247 static bool does_sb_need_changing(struct mddev
*mddev
)
2249 struct md_rdev
*rdev
;
2250 struct mdp_superblock_1
*sb
;
2253 /* Find a good rdev */
2254 rdev_for_each(rdev
, mddev
)
2255 if ((rdev
->raid_disk
>= 0) && !test_bit(Faulty
, &rdev
->flags
))
2258 /* No good device found. */
2262 sb
= page_address(rdev
->sb_page
);
2263 /* Check if a device has become faulty or a spare become active */
2264 rdev_for_each(rdev
, mddev
) {
2265 role
= le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]);
2266 /* Device activated? */
2267 if (role
== 0xffff && rdev
->raid_disk
>=0 &&
2268 !test_bit(Faulty
, &rdev
->flags
))
2270 /* Device turned faulty? */
2271 if (test_bit(Faulty
, &rdev
->flags
) && (role
< 0xfffd))
2275 /* Check if any mddev parameters have changed */
2276 if ((mddev
->dev_sectors
!= le64_to_cpu(sb
->size
)) ||
2277 (mddev
->reshape_position
!= le64_to_cpu(sb
->reshape_position
)) ||
2278 (mddev
->layout
!= le32_to_cpu(sb
->layout
)) ||
2279 (mddev
->raid_disks
!= le32_to_cpu(sb
->raid_disks
)) ||
2280 (mddev
->chunk_sectors
!= le32_to_cpu(sb
->chunksize
)))
2286 void md_update_sb(struct mddev
*mddev
, int force_change
)
2288 struct md_rdev
*rdev
;
2291 int any_badblocks_changed
= 0;
2296 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
2300 if (mddev_is_clustered(mddev
)) {
2301 if (test_and_clear_bit(MD_CHANGE_DEVS
, &mddev
->flags
))
2303 ret
= md_cluster_ops
->metadata_update_start(mddev
);
2304 /* Has someone else has updated the sb */
2305 if (!does_sb_need_changing(mddev
)) {
2307 md_cluster_ops
->metadata_update_cancel(mddev
);
2308 clear_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
2313 /* First make sure individual recovery_offsets are correct */
2314 rdev_for_each(rdev
, mddev
) {
2315 if (rdev
->raid_disk
>= 0 &&
2316 mddev
->delta_disks
>= 0 &&
2317 !test_bit(Journal
, &rdev
->flags
) &&
2318 !test_bit(In_sync
, &rdev
->flags
) &&
2319 mddev
->curr_resync_completed
> rdev
->recovery_offset
)
2320 rdev
->recovery_offset
= mddev
->curr_resync_completed
;
2323 if (!mddev
->persistent
) {
2324 clear_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
2325 clear_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
2326 if (!mddev
->external
) {
2327 clear_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
2328 rdev_for_each(rdev
, mddev
) {
2329 if (rdev
->badblocks
.changed
) {
2330 rdev
->badblocks
.changed
= 0;
2331 md_ack_all_badblocks(&rdev
->badblocks
);
2332 md_error(mddev
, rdev
);
2334 clear_bit(Blocked
, &rdev
->flags
);
2335 clear_bit(BlockedBadBlocks
, &rdev
->flags
);
2336 wake_up(&rdev
->blocked_wait
);
2339 wake_up(&mddev
->sb_wait
);
2343 spin_lock(&mddev
->lock
);
2345 mddev
->utime
= get_seconds();
2347 if (test_and_clear_bit(MD_CHANGE_DEVS
, &mddev
->flags
))
2349 if (test_and_clear_bit(MD_CHANGE_CLEAN
, &mddev
->flags
))
2350 /* just a clean<-> dirty transition, possibly leave spares alone,
2351 * though if events isn't the right even/odd, we will have to do
2357 if (mddev
->degraded
)
2358 /* If the array is degraded, then skipping spares is both
2359 * dangerous and fairly pointless.
2360 * Dangerous because a device that was removed from the array
2361 * might have a event_count that still looks up-to-date,
2362 * so it can be re-added without a resync.
2363 * Pointless because if there are any spares to skip,
2364 * then a recovery will happen and soon that array won't
2365 * be degraded any more and the spare can go back to sleep then.
2369 sync_req
= mddev
->in_sync
;
2371 /* If this is just a dirty<->clean transition, and the array is clean
2372 * and 'events' is odd, we can roll back to the previous clean state */
2374 && (mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
)
2375 && mddev
->can_decrease_events
2376 && mddev
->events
!= 1) {
2378 mddev
->can_decrease_events
= 0;
2380 /* otherwise we have to go forward and ... */
2382 mddev
->can_decrease_events
= nospares
;
2386 * This 64-bit counter should never wrap.
2387 * Either we are in around ~1 trillion A.C., assuming
2388 * 1 reboot per second, or we have a bug...
2390 WARN_ON(mddev
->events
== 0);
2392 rdev_for_each(rdev
, mddev
) {
2393 if (rdev
->badblocks
.changed
)
2394 any_badblocks_changed
++;
2395 if (test_bit(Faulty
, &rdev
->flags
))
2396 set_bit(FaultRecorded
, &rdev
->flags
);
2399 sync_sbs(mddev
, nospares
);
2400 spin_unlock(&mddev
->lock
);
2402 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2403 mdname(mddev
), mddev
->in_sync
);
2405 bitmap_update_sb(mddev
->bitmap
);
2406 rdev_for_each(rdev
, mddev
) {
2407 char b
[BDEVNAME_SIZE
];
2409 if (rdev
->sb_loaded
!= 1)
2410 continue; /* no noise on spare devices */
2412 if (!test_bit(Faulty
, &rdev
->flags
)) {
2413 md_super_write(mddev
,rdev
,
2414 rdev
->sb_start
, rdev
->sb_size
,
2416 pr_debug("md: (write) %s's sb offset: %llu\n",
2417 bdevname(rdev
->bdev
, b
),
2418 (unsigned long long)rdev
->sb_start
);
2419 rdev
->sb_events
= mddev
->events
;
2420 if (rdev
->badblocks
.size
) {
2421 md_super_write(mddev
, rdev
,
2422 rdev
->badblocks
.sector
,
2423 rdev
->badblocks
.size
<< 9,
2425 rdev
->badblocks
.size
= 0;
2429 pr_debug("md: %s (skipping faulty)\n",
2430 bdevname(rdev
->bdev
, b
));
2432 if (mddev
->level
== LEVEL_MULTIPATH
)
2433 /* only need to write one superblock... */
2436 md_super_wait(mddev
);
2437 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
2439 spin_lock(&mddev
->lock
);
2440 if (mddev
->in_sync
!= sync_req
||
2441 test_bit(MD_CHANGE_DEVS
, &mddev
->flags
)) {
2442 /* have to write it out again */
2443 spin_unlock(&mddev
->lock
);
2446 clear_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
2447 spin_unlock(&mddev
->lock
);
2448 wake_up(&mddev
->sb_wait
);
2449 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
2450 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
2452 rdev_for_each(rdev
, mddev
) {
2453 if (test_and_clear_bit(FaultRecorded
, &rdev
->flags
))
2454 clear_bit(Blocked
, &rdev
->flags
);
2456 if (any_badblocks_changed
)
2457 md_ack_all_badblocks(&rdev
->badblocks
);
2458 clear_bit(BlockedBadBlocks
, &rdev
->flags
);
2459 wake_up(&rdev
->blocked_wait
);
2462 if (mddev_is_clustered(mddev
) && ret
== 0)
2463 md_cluster_ops
->metadata_update_finish(mddev
);
2465 EXPORT_SYMBOL(md_update_sb
);
2467 static int add_bound_rdev(struct md_rdev
*rdev
)
2469 struct mddev
*mddev
= rdev
->mddev
;
2472 if (!mddev
->pers
->hot_remove_disk
) {
2473 /* If there is hot_add_disk but no hot_remove_disk
2474 * then added disks for geometry changes,
2475 * and should be added immediately.
2477 super_types
[mddev
->major_version
].
2478 validate_super(mddev
, rdev
);
2479 err
= mddev
->pers
->hot_add_disk(mddev
, rdev
);
2481 unbind_rdev_from_array(rdev
);
2486 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
2488 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
2489 if (mddev
->degraded
)
2490 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
2491 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
2492 md_new_event(mddev
);
2493 md_wakeup_thread(mddev
->thread
);
2497 /* words written to sysfs files may, or may not, be \n terminated.
2498 * We want to accept with case. For this we use cmd_match.
2500 static int cmd_match(const char *cmd
, const char *str
)
2502 /* See if cmd, written into a sysfs file, matches
2503 * str. They must either be the same, or cmd can
2504 * have a trailing newline
2506 while (*cmd
&& *str
&& *cmd
== *str
) {
2517 struct rdev_sysfs_entry
{
2518 struct attribute attr
;
2519 ssize_t (*show
)(struct md_rdev
*, char *);
2520 ssize_t (*store
)(struct md_rdev
*, const char *, size_t);
2524 state_show(struct md_rdev
*rdev
, char *page
)
2528 unsigned long flags
= ACCESS_ONCE(rdev
->flags
);
2530 if (test_bit(Faulty
, &flags
) ||
2531 rdev
->badblocks
.unacked_exist
) {
2532 len
+= sprintf(page
+len
, "%sfaulty",sep
);
2535 if (test_bit(In_sync
, &flags
)) {
2536 len
+= sprintf(page
+len
, "%sin_sync",sep
);
2539 if (test_bit(Journal
, &flags
)) {
2540 len
+= sprintf(page
+len
, "%sjournal",sep
);
2543 if (test_bit(WriteMostly
, &flags
)) {
2544 len
+= sprintf(page
+len
, "%swrite_mostly",sep
);
2547 if (test_bit(Blocked
, &flags
) ||
2548 (rdev
->badblocks
.unacked_exist
2549 && !test_bit(Faulty
, &flags
))) {
2550 len
+= sprintf(page
+len
, "%sblocked", sep
);
2553 if (!test_bit(Faulty
, &flags
) &&
2554 !test_bit(Journal
, &flags
) &&
2555 !test_bit(In_sync
, &flags
)) {
2556 len
+= sprintf(page
+len
, "%sspare", sep
);
2559 if (test_bit(WriteErrorSeen
, &flags
)) {
2560 len
+= sprintf(page
+len
, "%swrite_error", sep
);
2563 if (test_bit(WantReplacement
, &flags
)) {
2564 len
+= sprintf(page
+len
, "%swant_replacement", sep
);
2567 if (test_bit(Replacement
, &flags
)) {
2568 len
+= sprintf(page
+len
, "%sreplacement", sep
);
2572 return len
+sprintf(page
+len
, "\n");
2576 state_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
2579 * faulty - simulates an error
2580 * remove - disconnects the device
2581 * writemostly - sets write_mostly
2582 * -writemostly - clears write_mostly
2583 * blocked - sets the Blocked flags
2584 * -blocked - clears the Blocked and possibly simulates an error
2585 * insync - sets Insync providing device isn't active
2586 * -insync - clear Insync for a device with a slot assigned,
2587 * so that it gets rebuilt based on bitmap
2588 * write_error - sets WriteErrorSeen
2589 * -write_error - clears WriteErrorSeen
2592 if (cmd_match(buf
, "faulty") && rdev
->mddev
->pers
) {
2593 md_error(rdev
->mddev
, rdev
);
2594 if (test_bit(Faulty
, &rdev
->flags
))
2598 } else if (cmd_match(buf
, "remove")) {
2599 if (rdev
->raid_disk
>= 0)
2602 struct mddev
*mddev
= rdev
->mddev
;
2604 if (mddev_is_clustered(mddev
))
2605 err
= md_cluster_ops
->remove_disk(mddev
, rdev
);
2608 md_kick_rdev_from_array(rdev
);
2610 md_update_sb(mddev
, 1);
2611 md_new_event(mddev
);
2614 } else if (cmd_match(buf
, "writemostly")) {
2615 set_bit(WriteMostly
, &rdev
->flags
);
2617 } else if (cmd_match(buf
, "-writemostly")) {
2618 clear_bit(WriteMostly
, &rdev
->flags
);
2620 } else if (cmd_match(buf
, "blocked")) {
2621 set_bit(Blocked
, &rdev
->flags
);
2623 } else if (cmd_match(buf
, "-blocked")) {
2624 if (!test_bit(Faulty
, &rdev
->flags
) &&
2625 rdev
->badblocks
.unacked_exist
) {
2626 /* metadata handler doesn't understand badblocks,
2627 * so we need to fail the device
2629 md_error(rdev
->mddev
, rdev
);
2631 clear_bit(Blocked
, &rdev
->flags
);
2632 clear_bit(BlockedBadBlocks
, &rdev
->flags
);
2633 wake_up(&rdev
->blocked_wait
);
2634 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
2635 md_wakeup_thread(rdev
->mddev
->thread
);
2638 } else if (cmd_match(buf
, "insync") && rdev
->raid_disk
== -1) {
2639 set_bit(In_sync
, &rdev
->flags
);
2641 } else if (cmd_match(buf
, "-insync") && rdev
->raid_disk
>= 0 &&
2642 !test_bit(Journal
, &rdev
->flags
)) {
2643 if (rdev
->mddev
->pers
== NULL
) {
2644 clear_bit(In_sync
, &rdev
->flags
);
2645 rdev
->saved_raid_disk
= rdev
->raid_disk
;
2646 rdev
->raid_disk
= -1;
2649 } else if (cmd_match(buf
, "write_error")) {
2650 set_bit(WriteErrorSeen
, &rdev
->flags
);
2652 } else if (cmd_match(buf
, "-write_error")) {
2653 clear_bit(WriteErrorSeen
, &rdev
->flags
);
2655 } else if (cmd_match(buf
, "want_replacement")) {
2656 /* Any non-spare device that is not a replacement can
2657 * become want_replacement at any time, but we then need to
2658 * check if recovery is needed.
2660 if (rdev
->raid_disk
>= 0 &&
2661 !test_bit(Journal
, &rdev
->flags
) &&
2662 !test_bit(Replacement
, &rdev
->flags
))
2663 set_bit(WantReplacement
, &rdev
->flags
);
2664 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
2665 md_wakeup_thread(rdev
->mddev
->thread
);
2667 } else if (cmd_match(buf
, "-want_replacement")) {
2668 /* Clearing 'want_replacement' is always allowed.
2669 * Once replacements starts it is too late though.
2672 clear_bit(WantReplacement
, &rdev
->flags
);
2673 } else if (cmd_match(buf
, "replacement")) {
2674 /* Can only set a device as a replacement when array has not
2675 * yet been started. Once running, replacement is automatic
2676 * from spares, or by assigning 'slot'.
2678 if (rdev
->mddev
->pers
)
2681 set_bit(Replacement
, &rdev
->flags
);
2684 } else if (cmd_match(buf
, "-replacement")) {
2685 /* Similarly, can only clear Replacement before start */
2686 if (rdev
->mddev
->pers
)
2689 clear_bit(Replacement
, &rdev
->flags
);
2692 } else if (cmd_match(buf
, "re-add")) {
2693 if (test_bit(Faulty
, &rdev
->flags
) && (rdev
->raid_disk
== -1) &&
2694 rdev
->saved_raid_disk
>= 0) {
2695 /* clear_bit is performed _after_ all the devices
2696 * have their local Faulty bit cleared. If any writes
2697 * happen in the meantime in the local node, they
2698 * will land in the local bitmap, which will be synced
2699 * by this node eventually
2701 if (!mddev_is_clustered(rdev
->mddev
) ||
2702 (err
= md_cluster_ops
->gather_bitmaps(rdev
)) == 0) {
2703 clear_bit(Faulty
, &rdev
->flags
);
2704 err
= add_bound_rdev(rdev
);
2710 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
2711 return err
? err
: len
;
2713 static struct rdev_sysfs_entry rdev_state
=
2714 __ATTR_PREALLOC(state
, S_IRUGO
|S_IWUSR
, state_show
, state_store
);
2717 errors_show(struct md_rdev
*rdev
, char *page
)
2719 return sprintf(page
, "%d\n", atomic_read(&rdev
->corrected_errors
));
2723 errors_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
2728 rv
= kstrtouint(buf
, 10, &n
);
2731 atomic_set(&rdev
->corrected_errors
, n
);
2734 static struct rdev_sysfs_entry rdev_errors
=
2735 __ATTR(errors
, S_IRUGO
|S_IWUSR
, errors_show
, errors_store
);
2738 slot_show(struct md_rdev
*rdev
, char *page
)
2740 if (test_bit(Journal
, &rdev
->flags
))
2741 return sprintf(page
, "journal\n");
2742 else if (rdev
->raid_disk
< 0)
2743 return sprintf(page
, "none\n");
2745 return sprintf(page
, "%d\n", rdev
->raid_disk
);
2749 slot_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
2754 if (test_bit(Journal
, &rdev
->flags
))
2756 if (strncmp(buf
, "none", 4)==0)
2759 err
= kstrtouint(buf
, 10, (unsigned int *)&slot
);
2763 if (rdev
->mddev
->pers
&& slot
== -1) {
2764 /* Setting 'slot' on an active array requires also
2765 * updating the 'rd%d' link, and communicating
2766 * with the personality with ->hot_*_disk.
2767 * For now we only support removing
2768 * failed/spare devices. This normally happens automatically,
2769 * but not when the metadata is externally managed.
2771 if (rdev
->raid_disk
== -1)
2773 /* personality does all needed checks */
2774 if (rdev
->mddev
->pers
->hot_remove_disk
== NULL
)
2776 clear_bit(Blocked
, &rdev
->flags
);
2777 remove_and_add_spares(rdev
->mddev
, rdev
);
2778 if (rdev
->raid_disk
>= 0)
2780 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
2781 md_wakeup_thread(rdev
->mddev
->thread
);
2782 } else if (rdev
->mddev
->pers
) {
2783 /* Activating a spare .. or possibly reactivating
2784 * if we ever get bitmaps working here.
2788 if (rdev
->raid_disk
!= -1)
2791 if (test_bit(MD_RECOVERY_RUNNING
, &rdev
->mddev
->recovery
))
2794 if (rdev
->mddev
->pers
->hot_add_disk
== NULL
)
2797 if (slot
>= rdev
->mddev
->raid_disks
&&
2798 slot
>= rdev
->mddev
->raid_disks
+ rdev
->mddev
->delta_disks
)
2801 rdev
->raid_disk
= slot
;
2802 if (test_bit(In_sync
, &rdev
->flags
))
2803 rdev
->saved_raid_disk
= slot
;
2805 rdev
->saved_raid_disk
= -1;
2806 clear_bit(In_sync
, &rdev
->flags
);
2807 clear_bit(Bitmap_sync
, &rdev
->flags
);
2808 err
= rdev
->mddev
->pers
->
2809 hot_add_disk(rdev
->mddev
, rdev
);
2811 rdev
->raid_disk
= -1;
2814 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
2815 if (sysfs_link_rdev(rdev
->mddev
, rdev
))
2816 /* failure here is OK */;
2817 /* don't wakeup anyone, leave that to userspace. */
2819 if (slot
>= rdev
->mddev
->raid_disks
&&
2820 slot
>= rdev
->mddev
->raid_disks
+ rdev
->mddev
->delta_disks
)
2822 rdev
->raid_disk
= slot
;
2823 /* assume it is working */
2824 clear_bit(Faulty
, &rdev
->flags
);
2825 clear_bit(WriteMostly
, &rdev
->flags
);
2826 set_bit(In_sync
, &rdev
->flags
);
2827 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
2832 static struct rdev_sysfs_entry rdev_slot
=
2833 __ATTR(slot
, S_IRUGO
|S_IWUSR
, slot_show
, slot_store
);
2836 offset_show(struct md_rdev
*rdev
, char *page
)
2838 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->data_offset
);
2842 offset_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
2844 unsigned long long offset
;
2845 if (kstrtoull(buf
, 10, &offset
) < 0)
2847 if (rdev
->mddev
->pers
&& rdev
->raid_disk
>= 0)
2849 if (rdev
->sectors
&& rdev
->mddev
->external
)
2850 /* Must set offset before size, so overlap checks
2853 rdev
->data_offset
= offset
;
2854 rdev
->new_data_offset
= offset
;
2858 static struct rdev_sysfs_entry rdev_offset
=
2859 __ATTR(offset
, S_IRUGO
|S_IWUSR
, offset_show
, offset_store
);
2861 static ssize_t
new_offset_show(struct md_rdev
*rdev
, char *page
)
2863 return sprintf(page
, "%llu\n",
2864 (unsigned long long)rdev
->new_data_offset
);
2867 static ssize_t
new_offset_store(struct md_rdev
*rdev
,
2868 const char *buf
, size_t len
)
2870 unsigned long long new_offset
;
2871 struct mddev
*mddev
= rdev
->mddev
;
2873 if (kstrtoull(buf
, 10, &new_offset
) < 0)
2876 if (mddev
->sync_thread
||
2877 test_bit(MD_RECOVERY_RUNNING
,&mddev
->recovery
))
2879 if (new_offset
== rdev
->data_offset
)
2880 /* reset is always permitted */
2882 else if (new_offset
> rdev
->data_offset
) {
2883 /* must not push array size beyond rdev_sectors */
2884 if (new_offset
- rdev
->data_offset
2885 + mddev
->dev_sectors
> rdev
->sectors
)
2888 /* Metadata worries about other space details. */
2890 /* decreasing the offset is inconsistent with a backwards
2893 if (new_offset
< rdev
->data_offset
&&
2894 mddev
->reshape_backwards
)
2896 /* Increasing offset is inconsistent with forwards
2897 * reshape. reshape_direction should be set to
2898 * 'backwards' first.
2900 if (new_offset
> rdev
->data_offset
&&
2901 !mddev
->reshape_backwards
)
2904 if (mddev
->pers
&& mddev
->persistent
&&
2905 !super_types
[mddev
->major_version
]
2906 .allow_new_offset(rdev
, new_offset
))
2908 rdev
->new_data_offset
= new_offset
;
2909 if (new_offset
> rdev
->data_offset
)
2910 mddev
->reshape_backwards
= 1;
2911 else if (new_offset
< rdev
->data_offset
)
2912 mddev
->reshape_backwards
= 0;
2916 static struct rdev_sysfs_entry rdev_new_offset
=
2917 __ATTR(new_offset
, S_IRUGO
|S_IWUSR
, new_offset_show
, new_offset_store
);
2920 rdev_size_show(struct md_rdev
*rdev
, char *page
)
2922 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->sectors
/ 2);
2925 static int overlaps(sector_t s1
, sector_t l1
, sector_t s2
, sector_t l2
)
2927 /* check if two start/length pairs overlap */
2935 static int strict_blocks_to_sectors(const char *buf
, sector_t
*sectors
)
2937 unsigned long long blocks
;
2940 if (kstrtoull(buf
, 10, &blocks
) < 0)
2943 if (blocks
& 1ULL << (8 * sizeof(blocks
) - 1))
2944 return -EINVAL
; /* sector conversion overflow */
2947 if (new != blocks
* 2)
2948 return -EINVAL
; /* unsigned long long to sector_t overflow */
2955 rdev_size_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
2957 struct mddev
*my_mddev
= rdev
->mddev
;
2958 sector_t oldsectors
= rdev
->sectors
;
2961 if (test_bit(Journal
, &rdev
->flags
))
2963 if (strict_blocks_to_sectors(buf
, §ors
) < 0)
2965 if (rdev
->data_offset
!= rdev
->new_data_offset
)
2966 return -EINVAL
; /* too confusing */
2967 if (my_mddev
->pers
&& rdev
->raid_disk
>= 0) {
2968 if (my_mddev
->persistent
) {
2969 sectors
= super_types
[my_mddev
->major_version
].
2970 rdev_size_change(rdev
, sectors
);
2973 } else if (!sectors
)
2974 sectors
= (i_size_read(rdev
->bdev
->bd_inode
) >> 9) -
2976 if (!my_mddev
->pers
->resize
)
2977 /* Cannot change size for RAID0 or Linear etc */
2980 if (sectors
< my_mddev
->dev_sectors
)
2981 return -EINVAL
; /* component must fit device */
2983 rdev
->sectors
= sectors
;
2984 if (sectors
> oldsectors
&& my_mddev
->external
) {
2985 /* Need to check that all other rdevs with the same
2986 * ->bdev do not overlap. 'rcu' is sufficient to walk
2987 * the rdev lists safely.
2988 * This check does not provide a hard guarantee, it
2989 * just helps avoid dangerous mistakes.
2991 struct mddev
*mddev
;
2993 struct list_head
*tmp
;
2996 for_each_mddev(mddev
, tmp
) {
2997 struct md_rdev
*rdev2
;
2999 rdev_for_each(rdev2
, mddev
)
3000 if (rdev
->bdev
== rdev2
->bdev
&&
3002 overlaps(rdev
->data_offset
, rdev
->sectors
,
3015 /* Someone else could have slipped in a size
3016 * change here, but doing so is just silly.
3017 * We put oldsectors back because we *know* it is
3018 * safe, and trust userspace not to race with
3021 rdev
->sectors
= oldsectors
;
3028 static struct rdev_sysfs_entry rdev_size
=
3029 __ATTR(size
, S_IRUGO
|S_IWUSR
, rdev_size_show
, rdev_size_store
);
3031 static ssize_t
recovery_start_show(struct md_rdev
*rdev
, char *page
)
3033 unsigned long long recovery_start
= rdev
->recovery_offset
;
3035 if (test_bit(In_sync
, &rdev
->flags
) ||
3036 recovery_start
== MaxSector
)
3037 return sprintf(page
, "none\n");
3039 return sprintf(page
, "%llu\n", recovery_start
);
3042 static ssize_t
recovery_start_store(struct md_rdev
*rdev
, const char *buf
, size_t len
)
3044 unsigned long long recovery_start
;
3046 if (cmd_match(buf
, "none"))
3047 recovery_start
= MaxSector
;
3048 else if (kstrtoull(buf
, 10, &recovery_start
))
3051 if (rdev
->mddev
->pers
&&
3052 rdev
->raid_disk
>= 0)
3055 rdev
->recovery_offset
= recovery_start
;
3056 if (recovery_start
== MaxSector
)
3057 set_bit(In_sync
, &rdev
->flags
);
3059 clear_bit(In_sync
, &rdev
->flags
);
3063 static struct rdev_sysfs_entry rdev_recovery_start
=
3064 __ATTR(recovery_start
, S_IRUGO
|S_IWUSR
, recovery_start_show
, recovery_start_store
);
3067 badblocks_show(struct badblocks
*bb
, char *page
, int unack
);
3069 badblocks_store(struct badblocks
*bb
, const char *page
, size_t len
, int unack
);
3071 static ssize_t
bb_show(struct md_rdev
*rdev
, char *page
)
3073 return badblocks_show(&rdev
->badblocks
, page
, 0);
3075 static ssize_t
bb_store(struct md_rdev
*rdev
, const char *page
, size_t len
)
3077 int rv
= badblocks_store(&rdev
->badblocks
, page
, len
, 0);
3078 /* Maybe that ack was all we needed */
3079 if (test_and_clear_bit(BlockedBadBlocks
, &rdev
->flags
))
3080 wake_up(&rdev
->blocked_wait
);
3083 static struct rdev_sysfs_entry rdev_bad_blocks
=
3084 __ATTR(bad_blocks
, S_IRUGO
|S_IWUSR
, bb_show
, bb_store
);
3086 static ssize_t
ubb_show(struct md_rdev
*rdev
, char *page
)
3088 return badblocks_show(&rdev
->badblocks
, page
, 1);
3090 static ssize_t
ubb_store(struct md_rdev
*rdev
, const char *page
, size_t len
)
3092 return badblocks_store(&rdev
->badblocks
, page
, len
, 1);
3094 static struct rdev_sysfs_entry rdev_unack_bad_blocks
=
3095 __ATTR(unacknowledged_bad_blocks
, S_IRUGO
|S_IWUSR
, ubb_show
, ubb_store
);
3097 static struct attribute
*rdev_default_attrs
[] = {
3102 &rdev_new_offset
.attr
,
3104 &rdev_recovery_start
.attr
,
3105 &rdev_bad_blocks
.attr
,
3106 &rdev_unack_bad_blocks
.attr
,
3110 rdev_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
3112 struct rdev_sysfs_entry
*entry
= container_of(attr
, struct rdev_sysfs_entry
, attr
);
3113 struct md_rdev
*rdev
= container_of(kobj
, struct md_rdev
, kobj
);
3119 return entry
->show(rdev
, page
);
3123 rdev_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
3124 const char *page
, size_t length
)
3126 struct rdev_sysfs_entry
*entry
= container_of(attr
, struct rdev_sysfs_entry
, attr
);
3127 struct md_rdev
*rdev
= container_of(kobj
, struct md_rdev
, kobj
);
3129 struct mddev
*mddev
= rdev
->mddev
;
3133 if (!capable(CAP_SYS_ADMIN
))
3135 rv
= mddev
? mddev_lock(mddev
): -EBUSY
;
3137 if (rdev
->mddev
== NULL
)
3140 rv
= entry
->store(rdev
, page
, length
);
3141 mddev_unlock(mddev
);
3146 static void rdev_free(struct kobject
*ko
)
3148 struct md_rdev
*rdev
= container_of(ko
, struct md_rdev
, kobj
);
3151 static const struct sysfs_ops rdev_sysfs_ops
= {
3152 .show
= rdev_attr_show
,
3153 .store
= rdev_attr_store
,
3155 static struct kobj_type rdev_ktype
= {
3156 .release
= rdev_free
,
3157 .sysfs_ops
= &rdev_sysfs_ops
,
3158 .default_attrs
= rdev_default_attrs
,
3161 int md_rdev_init(struct md_rdev
*rdev
)
3164 rdev
->saved_raid_disk
= -1;
3165 rdev
->raid_disk
= -1;
3167 rdev
->data_offset
= 0;
3168 rdev
->new_data_offset
= 0;
3169 rdev
->sb_events
= 0;
3170 rdev
->last_read_error
.tv_sec
= 0;
3171 rdev
->last_read_error
.tv_nsec
= 0;
3172 rdev
->sb_loaded
= 0;
3173 rdev
->bb_page
= NULL
;
3174 atomic_set(&rdev
->nr_pending
, 0);
3175 atomic_set(&rdev
->read_errors
, 0);
3176 atomic_set(&rdev
->corrected_errors
, 0);
3178 INIT_LIST_HEAD(&rdev
->same_set
);
3179 init_waitqueue_head(&rdev
->blocked_wait
);
3181 /* Add space to store bad block list.
3182 * This reserves the space even on arrays where it cannot
3183 * be used - I wonder if that matters
3185 rdev
->badblocks
.count
= 0;
3186 rdev
->badblocks
.shift
= -1; /* disabled until explicitly enabled */
3187 rdev
->badblocks
.page
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
3188 seqlock_init(&rdev
->badblocks
.lock
);
3189 if (rdev
->badblocks
.page
== NULL
)
3194 EXPORT_SYMBOL_GPL(md_rdev_init
);
3196 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3198 * mark the device faulty if:
3200 * - the device is nonexistent (zero size)
3201 * - the device has no valid superblock
3203 * a faulty rdev _never_ has rdev->sb set.
3205 static struct md_rdev
*md_import_device(dev_t newdev
, int super_format
, int super_minor
)
3207 char b
[BDEVNAME_SIZE
];
3209 struct md_rdev
*rdev
;
3212 rdev
= kzalloc(sizeof(*rdev
), GFP_KERNEL
);
3214 printk(KERN_ERR
"md: could not alloc mem for new device!\n");
3215 return ERR_PTR(-ENOMEM
);
3218 err
= md_rdev_init(rdev
);
3221 err
= alloc_disk_sb(rdev
);
3225 err
= lock_rdev(rdev
, newdev
, super_format
== -2);
3229 kobject_init(&rdev
->kobj
, &rdev_ktype
);
3231 size
= i_size_read(rdev
->bdev
->bd_inode
) >> BLOCK_SIZE_BITS
;
3234 "md: %s has zero or unknown size, marking faulty!\n",
3235 bdevname(rdev
->bdev
,b
));
3240 if (super_format
>= 0) {
3241 err
= super_types
[super_format
].
3242 load_super(rdev
, NULL
, super_minor
);
3243 if (err
== -EINVAL
) {
3245 "md: %s does not have a valid v%d.%d "
3246 "superblock, not importing!\n",
3247 bdevname(rdev
->bdev
,b
),
3248 super_format
, super_minor
);
3253 "md: could not read %s's sb, not importing!\n",
3254 bdevname(rdev
->bdev
,b
));
3264 md_rdev_clear(rdev
);
3266 return ERR_PTR(err
);
3270 * Check a full RAID array for plausibility
3273 static void analyze_sbs(struct mddev
*mddev
)
3276 struct md_rdev
*rdev
, *freshest
, *tmp
;
3277 char b
[BDEVNAME_SIZE
];
3280 rdev_for_each_safe(rdev
, tmp
, mddev
)
3281 switch (super_types
[mddev
->major_version
].
3282 load_super(rdev
, freshest
, mddev
->minor_version
)) {
3290 "md: fatal superblock inconsistency in %s"
3291 " -- removing from array\n",
3292 bdevname(rdev
->bdev
,b
));
3293 md_kick_rdev_from_array(rdev
);
3296 super_types
[mddev
->major_version
].
3297 validate_super(mddev
, freshest
);
3300 rdev_for_each_safe(rdev
, tmp
, mddev
) {
3301 if (mddev
->max_disks
&&
3302 (rdev
->desc_nr
>= mddev
->max_disks
||
3303 i
> mddev
->max_disks
)) {
3305 "md: %s: %s: only %d devices permitted\n",
3306 mdname(mddev
), bdevname(rdev
->bdev
, b
),
3308 md_kick_rdev_from_array(rdev
);
3311 if (rdev
!= freshest
) {
3312 if (super_types
[mddev
->major_version
].
3313 validate_super(mddev
, rdev
)) {
3314 printk(KERN_WARNING
"md: kicking non-fresh %s"
3316 bdevname(rdev
->bdev
,b
));
3317 md_kick_rdev_from_array(rdev
);
3321 if (mddev
->level
== LEVEL_MULTIPATH
) {
3322 rdev
->desc_nr
= i
++;
3323 rdev
->raid_disk
= rdev
->desc_nr
;
3324 set_bit(In_sync
, &rdev
->flags
);
3325 } else if (rdev
->raid_disk
>=
3326 (mddev
->raid_disks
- min(0, mddev
->delta_disks
)) &&
3327 !test_bit(Journal
, &rdev
->flags
)) {
3328 rdev
->raid_disk
= -1;
3329 clear_bit(In_sync
, &rdev
->flags
);
3334 /* Read a fixed-point number.
3335 * Numbers in sysfs attributes should be in "standard" units where
3336 * possible, so time should be in seconds.
3337 * However we internally use a a much smaller unit such as
3338 * milliseconds or jiffies.
3339 * This function takes a decimal number with a possible fractional
3340 * component, and produces an integer which is the result of
3341 * multiplying that number by 10^'scale'.
3342 * all without any floating-point arithmetic.
3344 int strict_strtoul_scaled(const char *cp
, unsigned long *res
, int scale
)
3346 unsigned long result
= 0;
3348 while (isdigit(*cp
) || (*cp
== '.' && decimals
< 0)) {
3351 else if (decimals
< scale
) {
3354 result
= result
* 10 + value
;
3366 while (decimals
< scale
) {
3375 safe_delay_show(struct mddev
*mddev
, char *page
)
3377 int msec
= (mddev
->safemode_delay
*1000)/HZ
;
3378 return sprintf(page
, "%d.%03d\n", msec
/1000, msec
%1000);
3381 safe_delay_store(struct mddev
*mddev
, const char *cbuf
, size_t len
)
3385 if (mddev_is_clustered(mddev
)) {
3386 pr_info("md: Safemode is disabled for clustered mode\n");
3390 if (strict_strtoul_scaled(cbuf
, &msec
, 3) < 0)
3393 mddev
->safemode_delay
= 0;
3395 unsigned long old_delay
= mddev
->safemode_delay
;
3396 unsigned long new_delay
= (msec
*HZ
)/1000;
3400 mddev
->safemode_delay
= new_delay
;
3401 if (new_delay
< old_delay
|| old_delay
== 0)
3402 mod_timer(&mddev
->safemode_timer
, jiffies
+1);
3406 static struct md_sysfs_entry md_safe_delay
=
3407 __ATTR(safe_mode_delay
, S_IRUGO
|S_IWUSR
,safe_delay_show
, safe_delay_store
);
3410 level_show(struct mddev
*mddev
, char *page
)
3412 struct md_personality
*p
;
3414 spin_lock(&mddev
->lock
);
3417 ret
= sprintf(page
, "%s\n", p
->name
);
3418 else if (mddev
->clevel
[0])
3419 ret
= sprintf(page
, "%s\n", mddev
->clevel
);
3420 else if (mddev
->level
!= LEVEL_NONE
)
3421 ret
= sprintf(page
, "%d\n", mddev
->level
);
3424 spin_unlock(&mddev
->lock
);
3429 level_store(struct mddev
*mddev
, const char *buf
, size_t len
)
3434 struct md_personality
*pers
, *oldpers
;
3436 void *priv
, *oldpriv
;
3437 struct md_rdev
*rdev
;
3439 if (slen
== 0 || slen
>= sizeof(clevel
))
3442 rv
= mddev_lock(mddev
);
3446 if (mddev
->pers
== NULL
) {
3447 strncpy(mddev
->clevel
, buf
, slen
);
3448 if (mddev
->clevel
[slen
-1] == '\n')
3450 mddev
->clevel
[slen
] = 0;
3451 mddev
->level
= LEVEL_NONE
;
3459 /* request to change the personality. Need to ensure:
3460 * - array is not engaged in resync/recovery/reshape
3461 * - old personality can be suspended
3462 * - new personality will access other array.
3466 if (mddev
->sync_thread
||
3467 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
3468 mddev
->reshape_position
!= MaxSector
||
3469 mddev
->sysfs_active
)
3473 if (!mddev
->pers
->quiesce
) {
3474 printk(KERN_WARNING
"md: %s: %s does not support online personality change\n",
3475 mdname(mddev
), mddev
->pers
->name
);
3479 /* Now find the new personality */
3480 strncpy(clevel
, buf
, slen
);
3481 if (clevel
[slen
-1] == '\n')
3484 if (kstrtol(clevel
, 10, &level
))
3487 if (request_module("md-%s", clevel
) != 0)
3488 request_module("md-level-%s", clevel
);
3489 spin_lock(&pers_lock
);
3490 pers
= find_pers(level
, clevel
);
3491 if (!pers
|| !try_module_get(pers
->owner
)) {
3492 spin_unlock(&pers_lock
);
3493 printk(KERN_WARNING
"md: personality %s not loaded\n", clevel
);
3497 spin_unlock(&pers_lock
);
3499 if (pers
== mddev
->pers
) {
3500 /* Nothing to do! */
3501 module_put(pers
->owner
);
3505 if (!pers
->takeover
) {
3506 module_put(pers
->owner
);
3507 printk(KERN_WARNING
"md: %s: %s does not support personality takeover\n",
3508 mdname(mddev
), clevel
);
3513 rdev_for_each(rdev
, mddev
)
3514 rdev
->new_raid_disk
= rdev
->raid_disk
;
3516 /* ->takeover must set new_* and/or delta_disks
3517 * if it succeeds, and may set them when it fails.
3519 priv
= pers
->takeover(mddev
);
3521 mddev
->new_level
= mddev
->level
;
3522 mddev
->new_layout
= mddev
->layout
;
3523 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
3524 mddev
->raid_disks
-= mddev
->delta_disks
;
3525 mddev
->delta_disks
= 0;
3526 mddev
->reshape_backwards
= 0;
3527 module_put(pers
->owner
);
3528 printk(KERN_WARNING
"md: %s: %s would not accept array\n",
3529 mdname(mddev
), clevel
);
3534 /* Looks like we have a winner */
3535 mddev_suspend(mddev
);
3536 mddev_detach(mddev
);
3538 spin_lock(&mddev
->lock
);
3539 oldpers
= mddev
->pers
;
3540 oldpriv
= mddev
->private;
3542 mddev
->private = priv
;
3543 strlcpy(mddev
->clevel
, pers
->name
, sizeof(mddev
->clevel
));
3544 mddev
->level
= mddev
->new_level
;
3545 mddev
->layout
= mddev
->new_layout
;
3546 mddev
->chunk_sectors
= mddev
->new_chunk_sectors
;
3547 mddev
->delta_disks
= 0;
3548 mddev
->reshape_backwards
= 0;
3549 mddev
->degraded
= 0;
3550 spin_unlock(&mddev
->lock
);
3552 if (oldpers
->sync_request
== NULL
&&
3554 /* We are converting from a no-redundancy array
3555 * to a redundancy array and metadata is managed
3556 * externally so we need to be sure that writes
3557 * won't block due to a need to transition
3559 * until external management is started.
3562 mddev
->safemode_delay
= 0;
3563 mddev
->safemode
= 0;
3566 oldpers
->free(mddev
, oldpriv
);
3568 if (oldpers
->sync_request
== NULL
&&
3569 pers
->sync_request
!= NULL
) {
3570 /* need to add the md_redundancy_group */
3571 if (sysfs_create_group(&mddev
->kobj
, &md_redundancy_group
))
3573 "md: cannot register extra attributes for %s\n",
3575 mddev
->sysfs_action
= sysfs_get_dirent(mddev
->kobj
.sd
, "sync_action");
3577 if (oldpers
->sync_request
!= NULL
&&
3578 pers
->sync_request
== NULL
) {
3579 /* need to remove the md_redundancy_group */
3580 if (mddev
->to_remove
== NULL
)
3581 mddev
->to_remove
= &md_redundancy_group
;
3584 rdev_for_each(rdev
, mddev
) {
3585 if (rdev
->raid_disk
< 0)
3587 if (rdev
->new_raid_disk
>= mddev
->raid_disks
)
3588 rdev
->new_raid_disk
= -1;
3589 if (rdev
->new_raid_disk
== rdev
->raid_disk
)
3591 sysfs_unlink_rdev(mddev
, rdev
);
3593 rdev_for_each(rdev
, mddev
) {
3594 if (rdev
->raid_disk
< 0)
3596 if (rdev
->new_raid_disk
== rdev
->raid_disk
)
3598 rdev
->raid_disk
= rdev
->new_raid_disk
;
3599 if (rdev
->raid_disk
< 0)
3600 clear_bit(In_sync
, &rdev
->flags
);
3602 if (sysfs_link_rdev(mddev
, rdev
))
3603 printk(KERN_WARNING
"md: cannot register rd%d"
3604 " for %s after level change\n",
3605 rdev
->raid_disk
, mdname(mddev
));
3609 if (pers
->sync_request
== NULL
) {
3610 /* this is now an array without redundancy, so
3611 * it must always be in_sync
3614 del_timer_sync(&mddev
->safemode_timer
);
3616 blk_set_stacking_limits(&mddev
->queue
->limits
);
3618 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
3619 mddev_resume(mddev
);
3621 md_update_sb(mddev
, 1);
3622 sysfs_notify(&mddev
->kobj
, NULL
, "level");
3623 md_new_event(mddev
);
3626 mddev_unlock(mddev
);
3630 static struct md_sysfs_entry md_level
=
3631 __ATTR(level
, S_IRUGO
|S_IWUSR
, level_show
, level_store
);
3634 layout_show(struct mddev
*mddev
, char *page
)
3636 /* just a number, not meaningful for all levels */
3637 if (mddev
->reshape_position
!= MaxSector
&&
3638 mddev
->layout
!= mddev
->new_layout
)
3639 return sprintf(page
, "%d (%d)\n",
3640 mddev
->new_layout
, mddev
->layout
);
3641 return sprintf(page
, "%d\n", mddev
->layout
);
3645 layout_store(struct mddev
*mddev
, const char *buf
, size_t len
)
3650 err
= kstrtouint(buf
, 10, &n
);
3653 err
= mddev_lock(mddev
);
3658 if (mddev
->pers
->check_reshape
== NULL
)
3663 mddev
->new_layout
= n
;
3664 err
= mddev
->pers
->check_reshape(mddev
);
3666 mddev
->new_layout
= mddev
->layout
;
3669 mddev
->new_layout
= n
;
3670 if (mddev
->reshape_position
== MaxSector
)
3673 mddev_unlock(mddev
);
3676 static struct md_sysfs_entry md_layout
=
3677 __ATTR(layout
, S_IRUGO
|S_IWUSR
, layout_show
, layout_store
);
3680 raid_disks_show(struct mddev
*mddev
, char *page
)
3682 if (mddev
->raid_disks
== 0)
3684 if (mddev
->reshape_position
!= MaxSector
&&
3685 mddev
->delta_disks
!= 0)
3686 return sprintf(page
, "%d (%d)\n", mddev
->raid_disks
,
3687 mddev
->raid_disks
- mddev
->delta_disks
);
3688 return sprintf(page
, "%d\n", mddev
->raid_disks
);
3691 static int update_raid_disks(struct mddev
*mddev
, int raid_disks
);
3694 raid_disks_store(struct mddev
*mddev
, const char *buf
, size_t len
)
3699 err
= kstrtouint(buf
, 10, &n
);
3703 err
= mddev_lock(mddev
);
3707 err
= update_raid_disks(mddev
, n
);
3708 else if (mddev
->reshape_position
!= MaxSector
) {
3709 struct md_rdev
*rdev
;
3710 int olddisks
= mddev
->raid_disks
- mddev
->delta_disks
;
3713 rdev_for_each(rdev
, mddev
) {
3715 rdev
->data_offset
< rdev
->new_data_offset
)
3718 rdev
->data_offset
> rdev
->new_data_offset
)
3722 mddev
->delta_disks
= n
- olddisks
;
3723 mddev
->raid_disks
= n
;
3724 mddev
->reshape_backwards
= (mddev
->delta_disks
< 0);
3726 mddev
->raid_disks
= n
;
3728 mddev_unlock(mddev
);
3729 return err
? err
: len
;
3731 static struct md_sysfs_entry md_raid_disks
=
3732 __ATTR(raid_disks
, S_IRUGO
|S_IWUSR
, raid_disks_show
, raid_disks_store
);
3735 chunk_size_show(struct mddev
*mddev
, char *page
)
3737 if (mddev
->reshape_position
!= MaxSector
&&
3738 mddev
->chunk_sectors
!= mddev
->new_chunk_sectors
)
3739 return sprintf(page
, "%d (%d)\n",
3740 mddev
->new_chunk_sectors
<< 9,
3741 mddev
->chunk_sectors
<< 9);
3742 return sprintf(page
, "%d\n", mddev
->chunk_sectors
<< 9);
3746 chunk_size_store(struct mddev
*mddev
, const char *buf
, size_t len
)
3751 err
= kstrtoul(buf
, 10, &n
);
3755 err
= mddev_lock(mddev
);
3759 if (mddev
->pers
->check_reshape
== NULL
)
3764 mddev
->new_chunk_sectors
= n
>> 9;
3765 err
= mddev
->pers
->check_reshape(mddev
);
3767 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
3770 mddev
->new_chunk_sectors
= n
>> 9;
3771 if (mddev
->reshape_position
== MaxSector
)
3772 mddev
->chunk_sectors
= n
>> 9;
3774 mddev_unlock(mddev
);
3777 static struct md_sysfs_entry md_chunk_size
=
3778 __ATTR(chunk_size
, S_IRUGO
|S_IWUSR
, chunk_size_show
, chunk_size_store
);
3781 resync_start_show(struct mddev
*mddev
, char *page
)
3783 if (mddev
->recovery_cp
== MaxSector
)
3784 return sprintf(page
, "none\n");
3785 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->recovery_cp
);
3789 resync_start_store(struct mddev
*mddev
, const char *buf
, size_t len
)
3791 unsigned long long n
;
3794 if (cmd_match(buf
, "none"))
3797 err
= kstrtoull(buf
, 10, &n
);
3800 if (n
!= (sector_t
)n
)
3804 err
= mddev_lock(mddev
);
3807 if (mddev
->pers
&& !test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
))
3811 mddev
->recovery_cp
= n
;
3813 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
3815 mddev_unlock(mddev
);
3818 static struct md_sysfs_entry md_resync_start
=
3819 __ATTR_PREALLOC(resync_start
, S_IRUGO
|S_IWUSR
,
3820 resync_start_show
, resync_start_store
);
3823 * The array state can be:
3826 * No devices, no size, no level
3827 * Equivalent to STOP_ARRAY ioctl
3829 * May have some settings, but array is not active
3830 * all IO results in error
3831 * When written, doesn't tear down array, but just stops it
3832 * suspended (not supported yet)
3833 * All IO requests will block. The array can be reconfigured.
3834 * Writing this, if accepted, will block until array is quiescent
3836 * no resync can happen. no superblocks get written.
3837 * write requests fail
3839 * like readonly, but behaves like 'clean' on a write request.
3841 * clean - no pending writes, but otherwise active.
3842 * When written to inactive array, starts without resync
3843 * If a write request arrives then
3844 * if metadata is known, mark 'dirty' and switch to 'active'.
3845 * if not known, block and switch to write-pending
3846 * If written to an active array that has pending writes, then fails.
3848 * fully active: IO and resync can be happening.
3849 * When written to inactive array, starts with resync
3852 * clean, but writes are blocked waiting for 'active' to be written.
3855 * like active, but no writes have been seen for a while (100msec).
3858 enum array_state
{ clear
, inactive
, suspended
, readonly
, read_auto
, clean
, active
,
3859 write_pending
, active_idle
, bad_word
};
3860 static char *array_states
[] = {
3861 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3862 "write-pending", "active-idle", NULL
};
3864 static int match_word(const char *word
, char **list
)
3867 for (n
=0; list
[n
]; n
++)
3868 if (cmd_match(word
, list
[n
]))
3874 array_state_show(struct mddev
*mddev
, char *page
)
3876 enum array_state st
= inactive
;
3889 else if (test_bit(MD_CHANGE_PENDING
, &mddev
->flags
))
3891 else if (mddev
->safemode
)
3897 if (list_empty(&mddev
->disks
) &&
3898 mddev
->raid_disks
== 0 &&
3899 mddev
->dev_sectors
== 0)
3904 return sprintf(page
, "%s\n", array_states
[st
]);
3907 static int do_md_stop(struct mddev
*mddev
, int ro
, struct block_device
*bdev
);
3908 static int md_set_readonly(struct mddev
*mddev
, struct block_device
*bdev
);
3909 static int do_md_run(struct mddev
*mddev
);
3910 static int restart_array(struct mddev
*mddev
);
3913 array_state_store(struct mddev
*mddev
, const char *buf
, size_t len
)
3916 enum array_state st
= match_word(buf
, array_states
);
3918 if (mddev
->pers
&& (st
== active
|| st
== clean
) && mddev
->ro
!= 1) {
3919 /* don't take reconfig_mutex when toggling between
3922 spin_lock(&mddev
->lock
);
3924 restart_array(mddev
);
3925 clear_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
3926 wake_up(&mddev
->sb_wait
);
3928 } else /* st == clean */ {
3929 restart_array(mddev
);
3930 if (atomic_read(&mddev
->writes_pending
) == 0) {
3931 if (mddev
->in_sync
== 0) {
3933 if (mddev
->safemode
== 1)
3934 mddev
->safemode
= 0;
3935 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
3941 spin_unlock(&mddev
->lock
);
3944 err
= mddev_lock(mddev
);
3952 /* stopping an active array */
3953 err
= do_md_stop(mddev
, 0, NULL
);
3956 /* stopping an active array */
3958 err
= do_md_stop(mddev
, 2, NULL
);
3960 err
= 0; /* already inactive */
3963 break; /* not supported yet */
3966 err
= md_set_readonly(mddev
, NULL
);
3969 set_disk_ro(mddev
->gendisk
, 1);
3970 err
= do_md_run(mddev
);
3976 err
= md_set_readonly(mddev
, NULL
);
3977 else if (mddev
->ro
== 1)
3978 err
= restart_array(mddev
);
3981 set_disk_ro(mddev
->gendisk
, 0);
3985 err
= do_md_run(mddev
);
3990 err
= restart_array(mddev
);
3993 spin_lock(&mddev
->lock
);
3994 if (atomic_read(&mddev
->writes_pending
) == 0) {
3995 if (mddev
->in_sync
== 0) {
3997 if (mddev
->safemode
== 1)
3998 mddev
->safemode
= 0;
3999 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
4004 spin_unlock(&mddev
->lock
);
4010 err
= restart_array(mddev
);
4013 clear_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
4014 wake_up(&mddev
->sb_wait
);
4018 set_disk_ro(mddev
->gendisk
, 0);
4019 err
= do_md_run(mddev
);
4024 /* these cannot be set */
4029 if (mddev
->hold_active
== UNTIL_IOCTL
)
4030 mddev
->hold_active
= 0;
4031 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4033 mddev_unlock(mddev
);
4036 static struct md_sysfs_entry md_array_state
=
4037 __ATTR_PREALLOC(array_state
, S_IRUGO
|S_IWUSR
, array_state_show
, array_state_store
);
4040 max_corrected_read_errors_show(struct mddev
*mddev
, char *page
) {
4041 return sprintf(page
, "%d\n",
4042 atomic_read(&mddev
->max_corr_read_errors
));
4046 max_corrected_read_errors_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4051 rv
= kstrtouint(buf
, 10, &n
);
4054 atomic_set(&mddev
->max_corr_read_errors
, n
);
4058 static struct md_sysfs_entry max_corr_read_errors
=
4059 __ATTR(max_read_errors
, S_IRUGO
|S_IWUSR
, max_corrected_read_errors_show
,
4060 max_corrected_read_errors_store
);
4063 null_show(struct mddev
*mddev
, char *page
)
4069 new_dev_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4071 /* buf must be %d:%d\n? giving major and minor numbers */
4072 /* The new device is added to the array.
4073 * If the array has a persistent superblock, we read the
4074 * superblock to initialise info and check validity.
4075 * Otherwise, only checking done is that in bind_rdev_to_array,
4076 * which mainly checks size.
4079 int major
= simple_strtoul(buf
, &e
, 10);
4082 struct md_rdev
*rdev
;
4085 if (!*buf
|| *e
!= ':' || !e
[1] || e
[1] == '\n')
4087 minor
= simple_strtoul(e
+1, &e
, 10);
4088 if (*e
&& *e
!= '\n')
4090 dev
= MKDEV(major
, minor
);
4091 if (major
!= MAJOR(dev
) ||
4092 minor
!= MINOR(dev
))
4095 flush_workqueue(md_misc_wq
);
4097 err
= mddev_lock(mddev
);
4100 if (mddev
->persistent
) {
4101 rdev
= md_import_device(dev
, mddev
->major_version
,
4102 mddev
->minor_version
);
4103 if (!IS_ERR(rdev
) && !list_empty(&mddev
->disks
)) {
4104 struct md_rdev
*rdev0
4105 = list_entry(mddev
->disks
.next
,
4106 struct md_rdev
, same_set
);
4107 err
= super_types
[mddev
->major_version
]
4108 .load_super(rdev
, rdev0
, mddev
->minor_version
);
4112 } else if (mddev
->external
)
4113 rdev
= md_import_device(dev
, -2, -1);
4115 rdev
= md_import_device(dev
, -1, -1);
4118 mddev_unlock(mddev
);
4119 return PTR_ERR(rdev
);
4121 err
= bind_rdev_to_array(rdev
, mddev
);
4125 mddev_unlock(mddev
);
4126 return err
? err
: len
;
4129 static struct md_sysfs_entry md_new_device
=
4130 __ATTR(new_dev
, S_IWUSR
, null_show
, new_dev_store
);
4133 bitmap_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4136 unsigned long chunk
, end_chunk
;
4139 err
= mddev_lock(mddev
);
4144 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
4146 chunk
= end_chunk
= simple_strtoul(buf
, &end
, 0);
4147 if (buf
== end
) break;
4148 if (*end
== '-') { /* range */
4150 end_chunk
= simple_strtoul(buf
, &end
, 0);
4151 if (buf
== end
) break;
4153 if (*end
&& !isspace(*end
)) break;
4154 bitmap_dirty_bits(mddev
->bitmap
, chunk
, end_chunk
);
4155 buf
= skip_spaces(end
);
4157 bitmap_unplug(mddev
->bitmap
); /* flush the bits to disk */
4159 mddev_unlock(mddev
);
4163 static struct md_sysfs_entry md_bitmap
=
4164 __ATTR(bitmap_set_bits
, S_IWUSR
, null_show
, bitmap_store
);
4167 size_show(struct mddev
*mddev
, char *page
)
4169 return sprintf(page
, "%llu\n",
4170 (unsigned long long)mddev
->dev_sectors
/ 2);
4173 static int update_size(struct mddev
*mddev
, sector_t num_sectors
);
4176 size_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4178 /* If array is inactive, we can reduce the component size, but
4179 * not increase it (except from 0).
4180 * If array is active, we can try an on-line resize
4183 int err
= strict_blocks_to_sectors(buf
, §ors
);
4187 err
= mddev_lock(mddev
);
4191 err
= update_size(mddev
, sectors
);
4192 md_update_sb(mddev
, 1);
4194 if (mddev
->dev_sectors
== 0 ||
4195 mddev
->dev_sectors
> sectors
)
4196 mddev
->dev_sectors
= sectors
;
4200 mddev_unlock(mddev
);
4201 return err
? err
: len
;
4204 static struct md_sysfs_entry md_size
=
4205 __ATTR(component_size
, S_IRUGO
|S_IWUSR
, size_show
, size_store
);
4207 /* Metadata version.
4209 * 'none' for arrays with no metadata (good luck...)
4210 * 'external' for arrays with externally managed metadata,
4211 * or N.M for internally known formats
4214 metadata_show(struct mddev
*mddev
, char *page
)
4216 if (mddev
->persistent
)
4217 return sprintf(page
, "%d.%d\n",
4218 mddev
->major_version
, mddev
->minor_version
);
4219 else if (mddev
->external
)
4220 return sprintf(page
, "external:%s\n", mddev
->metadata_type
);
4222 return sprintf(page
, "none\n");
4226 metadata_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4231 /* Changing the details of 'external' metadata is
4232 * always permitted. Otherwise there must be
4233 * no devices attached to the array.
4236 err
= mddev_lock(mddev
);
4240 if (mddev
->external
&& strncmp(buf
, "external:", 9) == 0)
4242 else if (!list_empty(&mddev
->disks
))
4246 if (cmd_match(buf
, "none")) {
4247 mddev
->persistent
= 0;
4248 mddev
->external
= 0;
4249 mddev
->major_version
= 0;
4250 mddev
->minor_version
= 90;
4253 if (strncmp(buf
, "external:", 9) == 0) {
4254 size_t namelen
= len
-9;
4255 if (namelen
>= sizeof(mddev
->metadata_type
))
4256 namelen
= sizeof(mddev
->metadata_type
)-1;
4257 strncpy(mddev
->metadata_type
, buf
+9, namelen
);
4258 mddev
->metadata_type
[namelen
] = 0;
4259 if (namelen
&& mddev
->metadata_type
[namelen
-1] == '\n')
4260 mddev
->metadata_type
[--namelen
] = 0;
4261 mddev
->persistent
= 0;
4262 mddev
->external
= 1;
4263 mddev
->major_version
= 0;
4264 mddev
->minor_version
= 90;
4267 major
= simple_strtoul(buf
, &e
, 10);
4269 if (e
==buf
|| *e
!= '.')
4272 minor
= simple_strtoul(buf
, &e
, 10);
4273 if (e
==buf
|| (*e
&& *e
!= '\n') )
4276 if (major
>= ARRAY_SIZE(super_types
) || super_types
[major
].name
== NULL
)
4278 mddev
->major_version
= major
;
4279 mddev
->minor_version
= minor
;
4280 mddev
->persistent
= 1;
4281 mddev
->external
= 0;
4284 mddev_unlock(mddev
);
4288 static struct md_sysfs_entry md_metadata
=
4289 __ATTR_PREALLOC(metadata_version
, S_IRUGO
|S_IWUSR
, metadata_show
, metadata_store
);
4292 action_show(struct mddev
*mddev
, char *page
)
4294 char *type
= "idle";
4295 unsigned long recovery
= mddev
->recovery
;
4296 if (test_bit(MD_RECOVERY_FROZEN
, &recovery
))
4298 else if (test_bit(MD_RECOVERY_RUNNING
, &recovery
) ||
4299 (!mddev
->ro
&& test_bit(MD_RECOVERY_NEEDED
, &recovery
))) {
4300 if (test_bit(MD_RECOVERY_RESHAPE
, &recovery
))
4302 else if (test_bit(MD_RECOVERY_SYNC
, &recovery
)) {
4303 if (!test_bit(MD_RECOVERY_REQUESTED
, &recovery
))
4305 else if (test_bit(MD_RECOVERY_CHECK
, &recovery
))
4309 } else if (test_bit(MD_RECOVERY_RECOVER
, &recovery
))
4311 else if (mddev
->reshape_position
!= MaxSector
)
4314 return sprintf(page
, "%s\n", type
);
4318 action_store(struct mddev
*mddev
, const char *page
, size_t len
)
4320 if (!mddev
->pers
|| !mddev
->pers
->sync_request
)
4324 if (cmd_match(page
, "idle") || cmd_match(page
, "frozen")) {
4325 if (cmd_match(page
, "frozen"))
4326 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4328 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4329 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) &&
4330 mddev_lock(mddev
) == 0) {
4331 flush_workqueue(md_misc_wq
);
4332 if (mddev
->sync_thread
) {
4333 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
4334 md_reap_sync_thread(mddev
);
4336 mddev_unlock(mddev
);
4338 } else if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
4340 else if (cmd_match(page
, "resync"))
4341 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4342 else if (cmd_match(page
, "recover")) {
4343 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4344 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
4345 } else if (cmd_match(page
, "reshape")) {
4347 if (mddev
->pers
->start_reshape
== NULL
)
4349 err
= mddev_lock(mddev
);
4351 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
4354 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4355 err
= mddev
->pers
->start_reshape(mddev
);
4357 mddev_unlock(mddev
);
4361 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
4363 if (cmd_match(page
, "check"))
4364 set_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
4365 else if (!cmd_match(page
, "repair"))
4367 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4368 set_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
4369 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
4371 if (mddev
->ro
== 2) {
4372 /* A write to sync_action is enough to justify
4373 * canceling read-auto mode
4376 md_wakeup_thread(mddev
->sync_thread
);
4378 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4379 md_wakeup_thread(mddev
->thread
);
4380 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
4384 static struct md_sysfs_entry md_scan_mode
=
4385 __ATTR_PREALLOC(sync_action
, S_IRUGO
|S_IWUSR
, action_show
, action_store
);
4388 last_sync_action_show(struct mddev
*mddev
, char *page
)
4390 return sprintf(page
, "%s\n", mddev
->last_sync_action
);
4393 static struct md_sysfs_entry md_last_scan_mode
= __ATTR_RO(last_sync_action
);
4396 mismatch_cnt_show(struct mddev
*mddev
, char *page
)
4398 return sprintf(page
, "%llu\n",
4399 (unsigned long long)
4400 atomic64_read(&mddev
->resync_mismatches
));
4403 static struct md_sysfs_entry md_mismatches
= __ATTR_RO(mismatch_cnt
);
4406 sync_min_show(struct mddev
*mddev
, char *page
)
4408 return sprintf(page
, "%d (%s)\n", speed_min(mddev
),
4409 mddev
->sync_speed_min
? "local": "system");
4413 sync_min_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4418 if (strncmp(buf
, "system", 6)==0) {
4421 rv
= kstrtouint(buf
, 10, &min
);
4427 mddev
->sync_speed_min
= min
;
4431 static struct md_sysfs_entry md_sync_min
=
4432 __ATTR(sync_speed_min
, S_IRUGO
|S_IWUSR
, sync_min_show
, sync_min_store
);
4435 sync_max_show(struct mddev
*mddev
, char *page
)
4437 return sprintf(page
, "%d (%s)\n", speed_max(mddev
),
4438 mddev
->sync_speed_max
? "local": "system");
4442 sync_max_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4447 if (strncmp(buf
, "system", 6)==0) {
4450 rv
= kstrtouint(buf
, 10, &max
);
4456 mddev
->sync_speed_max
= max
;
4460 static struct md_sysfs_entry md_sync_max
=
4461 __ATTR(sync_speed_max
, S_IRUGO
|S_IWUSR
, sync_max_show
, sync_max_store
);
4464 degraded_show(struct mddev
*mddev
, char *page
)
4466 return sprintf(page
, "%d\n", mddev
->degraded
);
4468 static struct md_sysfs_entry md_degraded
= __ATTR_RO(degraded
);
4471 sync_force_parallel_show(struct mddev
*mddev
, char *page
)
4473 return sprintf(page
, "%d\n", mddev
->parallel_resync
);
4477 sync_force_parallel_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4481 if (kstrtol(buf
, 10, &n
))
4484 if (n
!= 0 && n
!= 1)
4487 mddev
->parallel_resync
= n
;
4489 if (mddev
->sync_thread
)
4490 wake_up(&resync_wait
);
4495 /* force parallel resync, even with shared block devices */
4496 static struct md_sysfs_entry md_sync_force_parallel
=
4497 __ATTR(sync_force_parallel
, S_IRUGO
|S_IWUSR
,
4498 sync_force_parallel_show
, sync_force_parallel_store
);
4501 sync_speed_show(struct mddev
*mddev
, char *page
)
4503 unsigned long resync
, dt
, db
;
4504 if (mddev
->curr_resync
== 0)
4505 return sprintf(page
, "none\n");
4506 resync
= mddev
->curr_mark_cnt
- atomic_read(&mddev
->recovery_active
);
4507 dt
= (jiffies
- mddev
->resync_mark
) / HZ
;
4509 db
= resync
- mddev
->resync_mark_cnt
;
4510 return sprintf(page
, "%lu\n", db
/dt
/2); /* K/sec */
4513 static struct md_sysfs_entry md_sync_speed
= __ATTR_RO(sync_speed
);
4516 sync_completed_show(struct mddev
*mddev
, char *page
)
4518 unsigned long long max_sectors
, resync
;
4520 if (!test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
4521 return sprintf(page
, "none\n");
4523 if (mddev
->curr_resync
== 1 ||
4524 mddev
->curr_resync
== 2)
4525 return sprintf(page
, "delayed\n");
4527 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ||
4528 test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
4529 max_sectors
= mddev
->resync_max_sectors
;
4531 max_sectors
= mddev
->dev_sectors
;
4533 resync
= mddev
->curr_resync_completed
;
4534 return sprintf(page
, "%llu / %llu\n", resync
, max_sectors
);
4537 static struct md_sysfs_entry md_sync_completed
=
4538 __ATTR_PREALLOC(sync_completed
, S_IRUGO
, sync_completed_show
, NULL
);
4541 min_sync_show(struct mddev
*mddev
, char *page
)
4543 return sprintf(page
, "%llu\n",
4544 (unsigned long long)mddev
->resync_min
);
4547 min_sync_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4549 unsigned long long min
;
4552 if (kstrtoull(buf
, 10, &min
))
4555 spin_lock(&mddev
->lock
);
4557 if (min
> mddev
->resync_max
)
4561 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
4564 /* Round down to multiple of 4K for safety */
4565 mddev
->resync_min
= round_down(min
, 8);
4569 spin_unlock(&mddev
->lock
);
4573 static struct md_sysfs_entry md_min_sync
=
4574 __ATTR(sync_min
, S_IRUGO
|S_IWUSR
, min_sync_show
, min_sync_store
);
4577 max_sync_show(struct mddev
*mddev
, char *page
)
4579 if (mddev
->resync_max
== MaxSector
)
4580 return sprintf(page
, "max\n");
4582 return sprintf(page
, "%llu\n",
4583 (unsigned long long)mddev
->resync_max
);
4586 max_sync_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4589 spin_lock(&mddev
->lock
);
4590 if (strncmp(buf
, "max", 3) == 0)
4591 mddev
->resync_max
= MaxSector
;
4593 unsigned long long max
;
4597 if (kstrtoull(buf
, 10, &max
))
4599 if (max
< mddev
->resync_min
)
4603 if (max
< mddev
->resync_max
&&
4605 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
4608 /* Must be a multiple of chunk_size */
4609 chunk
= mddev
->chunk_sectors
;
4611 sector_t temp
= max
;
4614 if (sector_div(temp
, chunk
))
4617 mddev
->resync_max
= max
;
4619 wake_up(&mddev
->recovery_wait
);
4622 spin_unlock(&mddev
->lock
);
4626 static struct md_sysfs_entry md_max_sync
=
4627 __ATTR(sync_max
, S_IRUGO
|S_IWUSR
, max_sync_show
, max_sync_store
);
4630 suspend_lo_show(struct mddev
*mddev
, char *page
)
4632 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->suspend_lo
);
4636 suspend_lo_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4638 unsigned long long old
, new;
4641 err
= kstrtoull(buf
, 10, &new);
4644 if (new != (sector_t
)new)
4647 err
= mddev_lock(mddev
);
4651 if (mddev
->pers
== NULL
||
4652 mddev
->pers
->quiesce
== NULL
)
4654 old
= mddev
->suspend_lo
;
4655 mddev
->suspend_lo
= new;
4657 /* Shrinking suspended region */
4658 mddev
->pers
->quiesce(mddev
, 2);
4660 /* Expanding suspended region - need to wait */
4661 mddev
->pers
->quiesce(mddev
, 1);
4662 mddev
->pers
->quiesce(mddev
, 0);
4666 mddev_unlock(mddev
);
4669 static struct md_sysfs_entry md_suspend_lo
=
4670 __ATTR(suspend_lo
, S_IRUGO
|S_IWUSR
, suspend_lo_show
, suspend_lo_store
);
4673 suspend_hi_show(struct mddev
*mddev
, char *page
)
4675 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->suspend_hi
);
4679 suspend_hi_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4681 unsigned long long old
, new;
4684 err
= kstrtoull(buf
, 10, &new);
4687 if (new != (sector_t
)new)
4690 err
= mddev_lock(mddev
);
4694 if (mddev
->pers
== NULL
||
4695 mddev
->pers
->quiesce
== NULL
)
4697 old
= mddev
->suspend_hi
;
4698 mddev
->suspend_hi
= new;
4700 /* Shrinking suspended region */
4701 mddev
->pers
->quiesce(mddev
, 2);
4703 /* Expanding suspended region - need to wait */
4704 mddev
->pers
->quiesce(mddev
, 1);
4705 mddev
->pers
->quiesce(mddev
, 0);
4709 mddev_unlock(mddev
);
4712 static struct md_sysfs_entry md_suspend_hi
=
4713 __ATTR(suspend_hi
, S_IRUGO
|S_IWUSR
, suspend_hi_show
, suspend_hi_store
);
4716 reshape_position_show(struct mddev
*mddev
, char *page
)
4718 if (mddev
->reshape_position
!= MaxSector
)
4719 return sprintf(page
, "%llu\n",
4720 (unsigned long long)mddev
->reshape_position
);
4721 strcpy(page
, "none\n");
4726 reshape_position_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4728 struct md_rdev
*rdev
;
4729 unsigned long long new;
4732 err
= kstrtoull(buf
, 10, &new);
4735 if (new != (sector_t
)new)
4737 err
= mddev_lock(mddev
);
4743 mddev
->reshape_position
= new;
4744 mddev
->delta_disks
= 0;
4745 mddev
->reshape_backwards
= 0;
4746 mddev
->new_level
= mddev
->level
;
4747 mddev
->new_layout
= mddev
->layout
;
4748 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
4749 rdev_for_each(rdev
, mddev
)
4750 rdev
->new_data_offset
= rdev
->data_offset
;
4753 mddev_unlock(mddev
);
4757 static struct md_sysfs_entry md_reshape_position
=
4758 __ATTR(reshape_position
, S_IRUGO
|S_IWUSR
, reshape_position_show
,
4759 reshape_position_store
);
4762 reshape_direction_show(struct mddev
*mddev
, char *page
)
4764 return sprintf(page
, "%s\n",
4765 mddev
->reshape_backwards
? "backwards" : "forwards");
4769 reshape_direction_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4774 if (cmd_match(buf
, "forwards"))
4776 else if (cmd_match(buf
, "backwards"))
4780 if (mddev
->reshape_backwards
== backwards
)
4783 err
= mddev_lock(mddev
);
4786 /* check if we are allowed to change */
4787 if (mddev
->delta_disks
)
4789 else if (mddev
->persistent
&&
4790 mddev
->major_version
== 0)
4793 mddev
->reshape_backwards
= backwards
;
4794 mddev_unlock(mddev
);
4798 static struct md_sysfs_entry md_reshape_direction
=
4799 __ATTR(reshape_direction
, S_IRUGO
|S_IWUSR
, reshape_direction_show
,
4800 reshape_direction_store
);
4803 array_size_show(struct mddev
*mddev
, char *page
)
4805 if (mddev
->external_size
)
4806 return sprintf(page
, "%llu\n",
4807 (unsigned long long)mddev
->array_sectors
/2);
4809 return sprintf(page
, "default\n");
4813 array_size_store(struct mddev
*mddev
, const char *buf
, size_t len
)
4818 err
= mddev_lock(mddev
);
4822 if (strncmp(buf
, "default", 7) == 0) {
4824 sectors
= mddev
->pers
->size(mddev
, 0, 0);
4826 sectors
= mddev
->array_sectors
;
4828 mddev
->external_size
= 0;
4830 if (strict_blocks_to_sectors(buf
, §ors
) < 0)
4832 else if (mddev
->pers
&& mddev
->pers
->size(mddev
, 0, 0) < sectors
)
4835 mddev
->external_size
= 1;
4839 mddev
->array_sectors
= sectors
;
4841 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
4842 revalidate_disk(mddev
->gendisk
);
4845 mddev_unlock(mddev
);
4849 static struct md_sysfs_entry md_array_size
=
4850 __ATTR(array_size
, S_IRUGO
|S_IWUSR
, array_size_show
,
4853 static struct attribute
*md_default_attrs
[] = {
4856 &md_raid_disks
.attr
,
4857 &md_chunk_size
.attr
,
4859 &md_resync_start
.attr
,
4861 &md_new_device
.attr
,
4862 &md_safe_delay
.attr
,
4863 &md_array_state
.attr
,
4864 &md_reshape_position
.attr
,
4865 &md_reshape_direction
.attr
,
4866 &md_array_size
.attr
,
4867 &max_corr_read_errors
.attr
,
4871 static struct attribute
*md_redundancy_attrs
[] = {
4873 &md_last_scan_mode
.attr
,
4874 &md_mismatches
.attr
,
4877 &md_sync_speed
.attr
,
4878 &md_sync_force_parallel
.attr
,
4879 &md_sync_completed
.attr
,
4882 &md_suspend_lo
.attr
,
4883 &md_suspend_hi
.attr
,
4888 static struct attribute_group md_redundancy_group
= {
4890 .attrs
= md_redundancy_attrs
,
4894 md_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
4896 struct md_sysfs_entry
*entry
= container_of(attr
, struct md_sysfs_entry
, attr
);
4897 struct mddev
*mddev
= container_of(kobj
, struct mddev
, kobj
);
4902 spin_lock(&all_mddevs_lock
);
4903 if (list_empty(&mddev
->all_mddevs
)) {
4904 spin_unlock(&all_mddevs_lock
);
4908 spin_unlock(&all_mddevs_lock
);
4910 rv
= entry
->show(mddev
, page
);
4916 md_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
4917 const char *page
, size_t length
)
4919 struct md_sysfs_entry
*entry
= container_of(attr
, struct md_sysfs_entry
, attr
);
4920 struct mddev
*mddev
= container_of(kobj
, struct mddev
, kobj
);
4925 if (!capable(CAP_SYS_ADMIN
))
4927 spin_lock(&all_mddevs_lock
);
4928 if (list_empty(&mddev
->all_mddevs
)) {
4929 spin_unlock(&all_mddevs_lock
);
4933 spin_unlock(&all_mddevs_lock
);
4934 rv
= entry
->store(mddev
, page
, length
);
4939 static void md_free(struct kobject
*ko
)
4941 struct mddev
*mddev
= container_of(ko
, struct mddev
, kobj
);
4943 if (mddev
->sysfs_state
)
4944 sysfs_put(mddev
->sysfs_state
);
4947 blk_cleanup_queue(mddev
->queue
);
4948 if (mddev
->gendisk
) {
4949 del_gendisk(mddev
->gendisk
);
4950 put_disk(mddev
->gendisk
);
4956 static const struct sysfs_ops md_sysfs_ops
= {
4957 .show
= md_attr_show
,
4958 .store
= md_attr_store
,
4960 static struct kobj_type md_ktype
= {
4962 .sysfs_ops
= &md_sysfs_ops
,
4963 .default_attrs
= md_default_attrs
,
4968 static void mddev_delayed_delete(struct work_struct
*ws
)
4970 struct mddev
*mddev
= container_of(ws
, struct mddev
, del_work
);
4972 sysfs_remove_group(&mddev
->kobj
, &md_bitmap_group
);
4973 kobject_del(&mddev
->kobj
);
4974 kobject_put(&mddev
->kobj
);
4977 static int md_alloc(dev_t dev
, char *name
)
4979 static DEFINE_MUTEX(disks_mutex
);
4980 struct mddev
*mddev
= mddev_find(dev
);
4981 struct gendisk
*disk
;
4990 partitioned
= (MAJOR(mddev
->unit
) != MD_MAJOR
);
4991 shift
= partitioned
? MdpMinorShift
: 0;
4992 unit
= MINOR(mddev
->unit
) >> shift
;
4994 /* wait for any previous instance of this device to be
4995 * completely removed (mddev_delayed_delete).
4997 flush_workqueue(md_misc_wq
);
4999 mutex_lock(&disks_mutex
);
5005 /* Need to ensure that 'name' is not a duplicate.
5007 struct mddev
*mddev2
;
5008 spin_lock(&all_mddevs_lock
);
5010 list_for_each_entry(mddev2
, &all_mddevs
, all_mddevs
)
5011 if (mddev2
->gendisk
&&
5012 strcmp(mddev2
->gendisk
->disk_name
, name
) == 0) {
5013 spin_unlock(&all_mddevs_lock
);
5016 spin_unlock(&all_mddevs_lock
);
5020 mddev
->queue
= blk_alloc_queue(GFP_KERNEL
);
5023 mddev
->queue
->queuedata
= mddev
;
5025 blk_queue_make_request(mddev
->queue
, md_make_request
);
5026 blk_set_stacking_limits(&mddev
->queue
->limits
);
5028 disk
= alloc_disk(1 << shift
);
5030 blk_cleanup_queue(mddev
->queue
);
5031 mddev
->queue
= NULL
;
5034 disk
->major
= MAJOR(mddev
->unit
);
5035 disk
->first_minor
= unit
<< shift
;
5037 strcpy(disk
->disk_name
, name
);
5038 else if (partitioned
)
5039 sprintf(disk
->disk_name
, "md_d%d", unit
);
5041 sprintf(disk
->disk_name
, "md%d", unit
);
5042 disk
->fops
= &md_fops
;
5043 disk
->private_data
= mddev
;
5044 disk
->queue
= mddev
->queue
;
5045 blk_queue_flush(mddev
->queue
, REQ_FLUSH
| REQ_FUA
);
5046 /* Allow extended partitions. This makes the
5047 * 'mdp' device redundant, but we can't really
5050 disk
->flags
|= GENHD_FL_EXT_DEVT
;
5051 mddev
->gendisk
= disk
;
5052 /* As soon as we call add_disk(), another thread could get
5053 * through to md_open, so make sure it doesn't get too far
5055 mutex_lock(&mddev
->open_mutex
);
5058 error
= kobject_init_and_add(&mddev
->kobj
, &md_ktype
,
5059 &disk_to_dev(disk
)->kobj
, "%s", "md");
5061 /* This isn't possible, but as kobject_init_and_add is marked
5062 * __must_check, we must do something with the result
5064 printk(KERN_WARNING
"md: cannot register %s/md - name in use\n",
5068 if (mddev
->kobj
.sd
&&
5069 sysfs_create_group(&mddev
->kobj
, &md_bitmap_group
))
5070 printk(KERN_DEBUG
"pointless warning\n");
5071 mutex_unlock(&mddev
->open_mutex
);
5073 mutex_unlock(&disks_mutex
);
5074 if (!error
&& mddev
->kobj
.sd
) {
5075 kobject_uevent(&mddev
->kobj
, KOBJ_ADD
);
5076 mddev
->sysfs_state
= sysfs_get_dirent_safe(mddev
->kobj
.sd
, "array_state");
5082 static struct kobject
*md_probe(dev_t dev
, int *part
, void *data
)
5084 md_alloc(dev
, NULL
);
5088 static int add_named_array(const char *val
, struct kernel_param
*kp
)
5090 /* val must be "md_*" where * is not all digits.
5091 * We allocate an array with a large free minor number, and
5092 * set the name to val. val must not already be an active name.
5094 int len
= strlen(val
);
5095 char buf
[DISK_NAME_LEN
];
5097 while (len
&& val
[len
-1] == '\n')
5099 if (len
>= DISK_NAME_LEN
)
5101 strlcpy(buf
, val
, len
+1);
5102 if (strncmp(buf
, "md_", 3) != 0)
5104 return md_alloc(0, buf
);
5107 static void md_safemode_timeout(unsigned long data
)
5109 struct mddev
*mddev
= (struct mddev
*) data
;
5111 if (!atomic_read(&mddev
->writes_pending
)) {
5112 mddev
->safemode
= 1;
5113 if (mddev
->external
)
5114 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
5116 md_wakeup_thread(mddev
->thread
);
5119 static int start_dirty_degraded
;
5121 int md_run(struct mddev
*mddev
)
5124 struct md_rdev
*rdev
;
5125 struct md_personality
*pers
;
5127 if (list_empty(&mddev
->disks
))
5128 /* cannot run an array with no devices.. */
5133 /* Cannot run until previous stop completes properly */
5134 if (mddev
->sysfs_active
)
5138 * Analyze all RAID superblock(s)
5140 if (!mddev
->raid_disks
) {
5141 if (!mddev
->persistent
)
5146 if (mddev
->level
!= LEVEL_NONE
)
5147 request_module("md-level-%d", mddev
->level
);
5148 else if (mddev
->clevel
[0])
5149 request_module("md-%s", mddev
->clevel
);
5152 * Drop all container device buffers, from now on
5153 * the only valid external interface is through the md
5156 rdev_for_each(rdev
, mddev
) {
5157 if (test_bit(Faulty
, &rdev
->flags
))
5159 sync_blockdev(rdev
->bdev
);
5160 invalidate_bdev(rdev
->bdev
);
5162 /* perform some consistency tests on the device.
5163 * We don't want the data to overlap the metadata,
5164 * Internal Bitmap issues have been handled elsewhere.
5166 if (rdev
->meta_bdev
) {
5167 /* Nothing to check */;
5168 } else if (rdev
->data_offset
< rdev
->sb_start
) {
5169 if (mddev
->dev_sectors
&&
5170 rdev
->data_offset
+ mddev
->dev_sectors
5172 printk("md: %s: data overlaps metadata\n",
5177 if (rdev
->sb_start
+ rdev
->sb_size
/512
5178 > rdev
->data_offset
) {
5179 printk("md: %s: metadata overlaps data\n",
5184 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
5187 if (mddev
->bio_set
== NULL
)
5188 mddev
->bio_set
= bioset_create(BIO_POOL_SIZE
, 0);
5190 spin_lock(&pers_lock
);
5191 pers
= find_pers(mddev
->level
, mddev
->clevel
);
5192 if (!pers
|| !try_module_get(pers
->owner
)) {
5193 spin_unlock(&pers_lock
);
5194 if (mddev
->level
!= LEVEL_NONE
)
5195 printk(KERN_WARNING
"md: personality for level %d is not loaded!\n",
5198 printk(KERN_WARNING
"md: personality for level %s is not loaded!\n",
5202 spin_unlock(&pers_lock
);
5203 if (mddev
->level
!= pers
->level
) {
5204 mddev
->level
= pers
->level
;
5205 mddev
->new_level
= pers
->level
;
5207 strlcpy(mddev
->clevel
, pers
->name
, sizeof(mddev
->clevel
));
5209 if (mddev
->reshape_position
!= MaxSector
&&
5210 pers
->start_reshape
== NULL
) {
5211 /* This personality cannot handle reshaping... */
5212 module_put(pers
->owner
);
5216 if (pers
->sync_request
) {
5217 /* Warn if this is a potentially silly
5220 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
5221 struct md_rdev
*rdev2
;
5224 rdev_for_each(rdev
, mddev
)
5225 rdev_for_each(rdev2
, mddev
) {
5227 rdev
->bdev
->bd_contains
==
5228 rdev2
->bdev
->bd_contains
) {
5230 "%s: WARNING: %s appears to be"
5231 " on the same physical disk as"
5234 bdevname(rdev
->bdev
,b
),
5235 bdevname(rdev2
->bdev
,b2
));
5242 "True protection against single-disk"
5243 " failure might be compromised.\n");
5246 mddev
->recovery
= 0;
5247 /* may be over-ridden by personality */
5248 mddev
->resync_max_sectors
= mddev
->dev_sectors
;
5250 mddev
->ok_start_degraded
= start_dirty_degraded
;
5252 if (start_readonly
&& mddev
->ro
== 0)
5253 mddev
->ro
= 2; /* read-only, but switch on first write */
5255 err
= pers
->run(mddev
);
5257 printk(KERN_ERR
"md: pers->run() failed ...\n");
5258 else if (pers
->size(mddev
, 0, 0) < mddev
->array_sectors
) {
5259 WARN_ONCE(!mddev
->external_size
, "%s: default size too small,"
5260 " but 'external_size' not in effect?\n", __func__
);
5262 "md: invalid array_size %llu > default size %llu\n",
5263 (unsigned long long)mddev
->array_sectors
/ 2,
5264 (unsigned long long)pers
->size(mddev
, 0, 0) / 2);
5267 if (err
== 0 && pers
->sync_request
&&
5268 (mddev
->bitmap_info
.file
|| mddev
->bitmap_info
.offset
)) {
5269 struct bitmap
*bitmap
;
5271 bitmap
= bitmap_create(mddev
, -1);
5272 if (IS_ERR(bitmap
)) {
5273 err
= PTR_ERR(bitmap
);
5274 printk(KERN_ERR
"%s: failed to create bitmap (%d)\n",
5275 mdname(mddev
), err
);
5277 mddev
->bitmap
= bitmap
;
5281 mddev_detach(mddev
);
5283 pers
->free(mddev
, mddev
->private);
5284 mddev
->private = NULL
;
5285 module_put(pers
->owner
);
5286 bitmap_destroy(mddev
);
5290 mddev
->queue
->backing_dev_info
.congested_data
= mddev
;
5291 mddev
->queue
->backing_dev_info
.congested_fn
= md_congested
;
5293 if (pers
->sync_request
) {
5294 if (mddev
->kobj
.sd
&&
5295 sysfs_create_group(&mddev
->kobj
, &md_redundancy_group
))
5297 "md: cannot register extra attributes for %s\n",
5299 mddev
->sysfs_action
= sysfs_get_dirent_safe(mddev
->kobj
.sd
, "sync_action");
5300 } else if (mddev
->ro
== 2) /* auto-readonly not meaningful */
5303 atomic_set(&mddev
->writes_pending
,0);
5304 atomic_set(&mddev
->max_corr_read_errors
,
5305 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS
);
5306 mddev
->safemode
= 0;
5307 if (mddev_is_clustered(mddev
))
5308 mddev
->safemode_delay
= 0;
5310 mddev
->safemode_delay
= (200 * HZ
)/1000 +1; /* 200 msec delay */
5313 spin_lock(&mddev
->lock
);
5316 spin_unlock(&mddev
->lock
);
5317 rdev_for_each(rdev
, mddev
)
5318 if (rdev
->raid_disk
>= 0)
5319 if (sysfs_link_rdev(mddev
, rdev
))
5320 /* failure here is OK */;
5322 if (mddev
->degraded
&& !mddev
->ro
)
5323 /* This ensures that recovering status is reported immediately
5324 * via sysfs - until a lack of spares is confirmed.
5326 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
5327 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5329 if (mddev
->flags
& MD_UPDATE_SB_FLAGS
)
5330 md_update_sb(mddev
, 0);
5332 md_new_event(mddev
);
5333 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
5334 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
5335 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
5338 EXPORT_SYMBOL_GPL(md_run
);
5340 static int do_md_run(struct mddev
*mddev
)
5344 err
= md_run(mddev
);
5347 err
= bitmap_load(mddev
);
5349 bitmap_destroy(mddev
);
5353 if (mddev_is_clustered(mddev
))
5354 md_allow_write(mddev
);
5356 md_wakeup_thread(mddev
->thread
);
5357 md_wakeup_thread(mddev
->sync_thread
); /* possibly kick off a reshape */
5359 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
5360 revalidate_disk(mddev
->gendisk
);
5362 kobject_uevent(&disk_to_dev(mddev
->gendisk
)->kobj
, KOBJ_CHANGE
);
5367 static int restart_array(struct mddev
*mddev
)
5369 struct gendisk
*disk
= mddev
->gendisk
;
5371 /* Complain if it has no devices */
5372 if (list_empty(&mddev
->disks
))
5378 if (test_bit(MD_HAS_JOURNAL
, &mddev
->flags
)) {
5379 struct md_rdev
*rdev
;
5380 bool has_journal
= false;
5383 rdev_for_each_rcu(rdev
, mddev
) {
5384 if (test_bit(Journal
, &rdev
->flags
) &&
5385 !test_bit(Faulty
, &rdev
->flags
)) {
5392 /* Don't restart rw with journal missing/faulty */
5397 mddev
->safemode
= 0;
5399 set_disk_ro(disk
, 0);
5400 printk(KERN_INFO
"md: %s switched to read-write mode.\n",
5402 /* Kick recovery or resync if necessary */
5403 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5404 md_wakeup_thread(mddev
->thread
);
5405 md_wakeup_thread(mddev
->sync_thread
);
5406 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
5410 static void md_clean(struct mddev
*mddev
)
5412 mddev
->array_sectors
= 0;
5413 mddev
->external_size
= 0;
5414 mddev
->dev_sectors
= 0;
5415 mddev
->raid_disks
= 0;
5416 mddev
->recovery_cp
= 0;
5417 mddev
->resync_min
= 0;
5418 mddev
->resync_max
= MaxSector
;
5419 mddev
->reshape_position
= MaxSector
;
5420 mddev
->external
= 0;
5421 mddev
->persistent
= 0;
5422 mddev
->level
= LEVEL_NONE
;
5423 mddev
->clevel
[0] = 0;
5426 mddev
->metadata_type
[0] = 0;
5427 mddev
->chunk_sectors
= 0;
5428 mddev
->ctime
= mddev
->utime
= 0;
5430 mddev
->max_disks
= 0;
5432 mddev
->can_decrease_events
= 0;
5433 mddev
->delta_disks
= 0;
5434 mddev
->reshape_backwards
= 0;
5435 mddev
->new_level
= LEVEL_NONE
;
5436 mddev
->new_layout
= 0;
5437 mddev
->new_chunk_sectors
= 0;
5438 mddev
->curr_resync
= 0;
5439 atomic64_set(&mddev
->resync_mismatches
, 0);
5440 mddev
->suspend_lo
= mddev
->suspend_hi
= 0;
5441 mddev
->sync_speed_min
= mddev
->sync_speed_max
= 0;
5442 mddev
->recovery
= 0;
5445 mddev
->degraded
= 0;
5446 mddev
->safemode
= 0;
5447 mddev
->private = NULL
;
5448 mddev
->bitmap_info
.offset
= 0;
5449 mddev
->bitmap_info
.default_offset
= 0;
5450 mddev
->bitmap_info
.default_space
= 0;
5451 mddev
->bitmap_info
.chunksize
= 0;
5452 mddev
->bitmap_info
.daemon_sleep
= 0;
5453 mddev
->bitmap_info
.max_write_behind
= 0;
5456 static void __md_stop_writes(struct mddev
*mddev
)
5458 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
5459 flush_workqueue(md_misc_wq
);
5460 if (mddev
->sync_thread
) {
5461 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
5462 md_reap_sync_thread(mddev
);
5465 del_timer_sync(&mddev
->safemode_timer
);
5467 bitmap_flush(mddev
);
5468 md_super_wait(mddev
);
5470 if (mddev
->ro
== 0 &&
5471 ((!mddev
->in_sync
&& !mddev_is_clustered(mddev
)) ||
5472 (mddev
->flags
& MD_UPDATE_SB_FLAGS
))) {
5473 /* mark array as shutdown cleanly */
5474 if (!mddev_is_clustered(mddev
))
5476 md_update_sb(mddev
, 1);
5480 void md_stop_writes(struct mddev
*mddev
)
5482 mddev_lock_nointr(mddev
);
5483 __md_stop_writes(mddev
);
5484 mddev_unlock(mddev
);
5486 EXPORT_SYMBOL_GPL(md_stop_writes
);
5488 static void mddev_detach(struct mddev
*mddev
)
5490 struct bitmap
*bitmap
= mddev
->bitmap
;
5491 /* wait for behind writes to complete */
5492 if (bitmap
&& atomic_read(&bitmap
->behind_writes
) > 0) {
5493 printk(KERN_INFO
"md:%s: behind writes in progress - waiting to stop.\n",
5495 /* need to kick something here to make sure I/O goes? */
5496 wait_event(bitmap
->behind_wait
,
5497 atomic_read(&bitmap
->behind_writes
) == 0);
5499 if (mddev
->pers
&& mddev
->pers
->quiesce
) {
5500 mddev
->pers
->quiesce(mddev
, 1);
5501 mddev
->pers
->quiesce(mddev
, 0);
5503 md_unregister_thread(&mddev
->thread
);
5505 blk_sync_queue(mddev
->queue
); /* the unplug fn references 'conf'*/
5508 static void __md_stop(struct mddev
*mddev
)
5510 struct md_personality
*pers
= mddev
->pers
;
5511 mddev_detach(mddev
);
5512 /* Ensure ->event_work is done */
5513 flush_workqueue(md_misc_wq
);
5514 spin_lock(&mddev
->lock
);
5517 spin_unlock(&mddev
->lock
);
5518 pers
->free(mddev
, mddev
->private);
5519 mddev
->private = NULL
;
5520 if (pers
->sync_request
&& mddev
->to_remove
== NULL
)
5521 mddev
->to_remove
= &md_redundancy_group
;
5522 module_put(pers
->owner
);
5523 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
5526 void md_stop(struct mddev
*mddev
)
5528 /* stop the array and free an attached data structures.
5529 * This is called from dm-raid
5532 bitmap_destroy(mddev
);
5534 bioset_free(mddev
->bio_set
);
5537 EXPORT_SYMBOL_GPL(md_stop
);
5539 static int md_set_readonly(struct mddev
*mddev
, struct block_device
*bdev
)
5544 if (!test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
)) {
5546 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
5547 md_wakeup_thread(mddev
->thread
);
5549 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
5550 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
5551 if (mddev
->sync_thread
)
5552 /* Thread might be blocked waiting for metadata update
5553 * which will now never happen */
5554 wake_up_process(mddev
->sync_thread
->tsk
);
5556 if (mddev
->external
&& test_bit(MD_CHANGE_PENDING
, &mddev
->flags
))
5558 mddev_unlock(mddev
);
5559 wait_event(resync_wait
, !test_bit(MD_RECOVERY_RUNNING
,
5561 wait_event(mddev
->sb_wait
,
5562 !test_bit(MD_CHANGE_PENDING
, &mddev
->flags
));
5563 mddev_lock_nointr(mddev
);
5565 mutex_lock(&mddev
->open_mutex
);
5566 if ((mddev
->pers
&& atomic_read(&mddev
->openers
) > !!bdev
) ||
5567 mddev
->sync_thread
||
5568 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
5569 (bdev
&& !test_bit(MD_STILL_CLOSED
, &mddev
->flags
))) {
5570 printk("md: %s still in use.\n",mdname(mddev
));
5572 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
5573 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5574 md_wakeup_thread(mddev
->thread
);
5580 __md_stop_writes(mddev
);
5586 set_disk_ro(mddev
->gendisk
, 1);
5587 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
5588 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5589 md_wakeup_thread(mddev
->thread
);
5590 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
5594 mutex_unlock(&mddev
->open_mutex
);
5599 * 0 - completely stop and dis-assemble array
5600 * 2 - stop but do not disassemble array
5602 static int do_md_stop(struct mddev
*mddev
, int mode
,
5603 struct block_device
*bdev
)
5605 struct gendisk
*disk
= mddev
->gendisk
;
5606 struct md_rdev
*rdev
;
5609 if (!test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
)) {
5611 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
5612 md_wakeup_thread(mddev
->thread
);
5614 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
5615 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
5616 if (mddev
->sync_thread
)
5617 /* Thread might be blocked waiting for metadata update
5618 * which will now never happen */
5619 wake_up_process(mddev
->sync_thread
->tsk
);
5621 mddev_unlock(mddev
);
5622 wait_event(resync_wait
, (mddev
->sync_thread
== NULL
&&
5623 !test_bit(MD_RECOVERY_RUNNING
,
5624 &mddev
->recovery
)));
5625 mddev_lock_nointr(mddev
);
5627 mutex_lock(&mddev
->open_mutex
);
5628 if ((mddev
->pers
&& atomic_read(&mddev
->openers
) > !!bdev
) ||
5629 mddev
->sysfs_active
||
5630 mddev
->sync_thread
||
5631 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
5632 (bdev
&& !test_bit(MD_STILL_CLOSED
, &mddev
->flags
))) {
5633 printk("md: %s still in use.\n",mdname(mddev
));
5634 mutex_unlock(&mddev
->open_mutex
);
5636 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
5637 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5638 md_wakeup_thread(mddev
->thread
);
5644 set_disk_ro(disk
, 0);
5646 __md_stop_writes(mddev
);
5648 mddev
->queue
->backing_dev_info
.congested_fn
= NULL
;
5650 /* tell userspace to handle 'inactive' */
5651 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
5653 rdev_for_each(rdev
, mddev
)
5654 if (rdev
->raid_disk
>= 0)
5655 sysfs_unlink_rdev(mddev
, rdev
);
5657 set_capacity(disk
, 0);
5658 mutex_unlock(&mddev
->open_mutex
);
5660 revalidate_disk(disk
);
5665 mutex_unlock(&mddev
->open_mutex
);
5667 * Free resources if final stop
5670 printk(KERN_INFO
"md: %s stopped.\n", mdname(mddev
));
5672 bitmap_destroy(mddev
);
5673 if (mddev
->bitmap_info
.file
) {
5674 struct file
*f
= mddev
->bitmap_info
.file
;
5675 spin_lock(&mddev
->lock
);
5676 mddev
->bitmap_info
.file
= NULL
;
5677 spin_unlock(&mddev
->lock
);
5680 mddev
->bitmap_info
.offset
= 0;
5682 export_array(mddev
);
5685 kobject_uevent(&disk_to_dev(mddev
->gendisk
)->kobj
, KOBJ_CHANGE
);
5686 if (mddev
->hold_active
== UNTIL_STOP
)
5687 mddev
->hold_active
= 0;
5689 md_new_event(mddev
);
5690 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
5695 static void autorun_array(struct mddev
*mddev
)
5697 struct md_rdev
*rdev
;
5700 if (list_empty(&mddev
->disks
))
5703 printk(KERN_INFO
"md: running: ");
5705 rdev_for_each(rdev
, mddev
) {
5706 char b
[BDEVNAME_SIZE
];
5707 printk("<%s>", bdevname(rdev
->bdev
,b
));
5711 err
= do_md_run(mddev
);
5713 printk(KERN_WARNING
"md: do_md_run() returned %d\n", err
);
5714 do_md_stop(mddev
, 0, NULL
);
5719 * lets try to run arrays based on all disks that have arrived
5720 * until now. (those are in pending_raid_disks)
5722 * the method: pick the first pending disk, collect all disks with
5723 * the same UUID, remove all from the pending list and put them into
5724 * the 'same_array' list. Then order this list based on superblock
5725 * update time (freshest comes first), kick out 'old' disks and
5726 * compare superblocks. If everything's fine then run it.
5728 * If "unit" is allocated, then bump its reference count
5730 static void autorun_devices(int part
)
5732 struct md_rdev
*rdev0
, *rdev
, *tmp
;
5733 struct mddev
*mddev
;
5734 char b
[BDEVNAME_SIZE
];
5736 printk(KERN_INFO
"md: autorun ...\n");
5737 while (!list_empty(&pending_raid_disks
)) {
5740 LIST_HEAD(candidates
);
5741 rdev0
= list_entry(pending_raid_disks
.next
,
5742 struct md_rdev
, same_set
);
5744 printk(KERN_INFO
"md: considering %s ...\n",
5745 bdevname(rdev0
->bdev
,b
));
5746 INIT_LIST_HEAD(&candidates
);
5747 rdev_for_each_list(rdev
, tmp
, &pending_raid_disks
)
5748 if (super_90_load(rdev
, rdev0
, 0) >= 0) {
5749 printk(KERN_INFO
"md: adding %s ...\n",
5750 bdevname(rdev
->bdev
,b
));
5751 list_move(&rdev
->same_set
, &candidates
);
5754 * now we have a set of devices, with all of them having
5755 * mostly sane superblocks. It's time to allocate the
5759 dev
= MKDEV(mdp_major
,
5760 rdev0
->preferred_minor
<< MdpMinorShift
);
5761 unit
= MINOR(dev
) >> MdpMinorShift
;
5763 dev
= MKDEV(MD_MAJOR
, rdev0
->preferred_minor
);
5766 if (rdev0
->preferred_minor
!= unit
) {
5767 printk(KERN_INFO
"md: unit number in %s is bad: %d\n",
5768 bdevname(rdev0
->bdev
, b
), rdev0
->preferred_minor
);
5772 md_probe(dev
, NULL
, NULL
);
5773 mddev
= mddev_find(dev
);
5774 if (!mddev
|| !mddev
->gendisk
) {
5778 "md: cannot allocate memory for md drive.\n");
5781 if (mddev_lock(mddev
))
5782 printk(KERN_WARNING
"md: %s locked, cannot run\n",
5784 else if (mddev
->raid_disks
|| mddev
->major_version
5785 || !list_empty(&mddev
->disks
)) {
5787 "md: %s already running, cannot run %s\n",
5788 mdname(mddev
), bdevname(rdev0
->bdev
,b
));
5789 mddev_unlock(mddev
);
5791 printk(KERN_INFO
"md: created %s\n", mdname(mddev
));
5792 mddev
->persistent
= 1;
5793 rdev_for_each_list(rdev
, tmp
, &candidates
) {
5794 list_del_init(&rdev
->same_set
);
5795 if (bind_rdev_to_array(rdev
, mddev
))
5798 autorun_array(mddev
);
5799 mddev_unlock(mddev
);
5801 /* on success, candidates will be empty, on error
5804 rdev_for_each_list(rdev
, tmp
, &candidates
) {
5805 list_del_init(&rdev
->same_set
);
5810 printk(KERN_INFO
"md: ... autorun DONE.\n");
5812 #endif /* !MODULE */
5814 static int get_version(void __user
*arg
)
5818 ver
.major
= MD_MAJOR_VERSION
;
5819 ver
.minor
= MD_MINOR_VERSION
;
5820 ver
.patchlevel
= MD_PATCHLEVEL_VERSION
;
5822 if (copy_to_user(arg
, &ver
, sizeof(ver
)))
5828 static int get_array_info(struct mddev
*mddev
, void __user
*arg
)
5830 mdu_array_info_t info
;
5831 int nr
,working
,insync
,failed
,spare
;
5832 struct md_rdev
*rdev
;
5834 nr
= working
= insync
= failed
= spare
= 0;
5836 rdev_for_each_rcu(rdev
, mddev
) {
5838 if (test_bit(Faulty
, &rdev
->flags
))
5842 if (test_bit(In_sync
, &rdev
->flags
))
5850 info
.major_version
= mddev
->major_version
;
5851 info
.minor_version
= mddev
->minor_version
;
5852 info
.patch_version
= MD_PATCHLEVEL_VERSION
;
5853 info
.ctime
= mddev
->ctime
;
5854 info
.level
= mddev
->level
;
5855 info
.size
= mddev
->dev_sectors
/ 2;
5856 if (info
.size
!= mddev
->dev_sectors
/ 2) /* overflow */
5859 info
.raid_disks
= mddev
->raid_disks
;
5860 info
.md_minor
= mddev
->md_minor
;
5861 info
.not_persistent
= !mddev
->persistent
;
5863 info
.utime
= mddev
->utime
;
5866 info
.state
= (1<<MD_SB_CLEAN
);
5867 if (mddev
->bitmap
&& mddev
->bitmap_info
.offset
)
5868 info
.state
|= (1<<MD_SB_BITMAP_PRESENT
);
5869 if (mddev_is_clustered(mddev
))
5870 info
.state
|= (1<<MD_SB_CLUSTERED
);
5871 info
.active_disks
= insync
;
5872 info
.working_disks
= working
;
5873 info
.failed_disks
= failed
;
5874 info
.spare_disks
= spare
;
5876 info
.layout
= mddev
->layout
;
5877 info
.chunk_size
= mddev
->chunk_sectors
<< 9;
5879 if (copy_to_user(arg
, &info
, sizeof(info
)))
5885 static int get_bitmap_file(struct mddev
*mddev
, void __user
* arg
)
5887 mdu_bitmap_file_t
*file
= NULL
; /* too big for stack allocation */
5891 file
= kzalloc(sizeof(*file
), GFP_NOIO
);
5896 spin_lock(&mddev
->lock
);
5897 /* bitmap enabled */
5898 if (mddev
->bitmap_info
.file
) {
5899 ptr
= file_path(mddev
->bitmap_info
.file
, file
->pathname
,
5900 sizeof(file
->pathname
));
5904 memmove(file
->pathname
, ptr
,
5905 sizeof(file
->pathname
)-(ptr
-file
->pathname
));
5907 spin_unlock(&mddev
->lock
);
5910 copy_to_user(arg
, file
, sizeof(*file
)))
5917 static int get_disk_info(struct mddev
*mddev
, void __user
* arg
)
5919 mdu_disk_info_t info
;
5920 struct md_rdev
*rdev
;
5922 if (copy_from_user(&info
, arg
, sizeof(info
)))
5926 rdev
= md_find_rdev_nr_rcu(mddev
, info
.number
);
5928 info
.major
= MAJOR(rdev
->bdev
->bd_dev
);
5929 info
.minor
= MINOR(rdev
->bdev
->bd_dev
);
5930 info
.raid_disk
= rdev
->raid_disk
;
5932 if (test_bit(Faulty
, &rdev
->flags
))
5933 info
.state
|= (1<<MD_DISK_FAULTY
);
5934 else if (test_bit(In_sync
, &rdev
->flags
)) {
5935 info
.state
|= (1<<MD_DISK_ACTIVE
);
5936 info
.state
|= (1<<MD_DISK_SYNC
);
5938 if (test_bit(Journal
, &rdev
->flags
))
5939 info
.state
|= (1<<MD_DISK_JOURNAL
);
5940 if (test_bit(WriteMostly
, &rdev
->flags
))
5941 info
.state
|= (1<<MD_DISK_WRITEMOSTLY
);
5943 info
.major
= info
.minor
= 0;
5944 info
.raid_disk
= -1;
5945 info
.state
= (1<<MD_DISK_REMOVED
);
5949 if (copy_to_user(arg
, &info
, sizeof(info
)))
5955 static int add_new_disk(struct mddev
*mddev
, mdu_disk_info_t
*info
)
5957 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
5958 struct md_rdev
*rdev
;
5959 dev_t dev
= MKDEV(info
->major
,info
->minor
);
5961 if (mddev_is_clustered(mddev
) &&
5962 !(info
->state
& ((1 << MD_DISK_CLUSTER_ADD
) | (1 << MD_DISK_CANDIDATE
)))) {
5963 pr_err("%s: Cannot add to clustered mddev.\n",
5968 if (info
->major
!= MAJOR(dev
) || info
->minor
!= MINOR(dev
))
5971 if (!mddev
->raid_disks
) {
5973 /* expecting a device which has a superblock */
5974 rdev
= md_import_device(dev
, mddev
->major_version
, mddev
->minor_version
);
5977 "md: md_import_device returned %ld\n",
5979 return PTR_ERR(rdev
);
5981 if (!list_empty(&mddev
->disks
)) {
5982 struct md_rdev
*rdev0
5983 = list_entry(mddev
->disks
.next
,
5984 struct md_rdev
, same_set
);
5985 err
= super_types
[mddev
->major_version
]
5986 .load_super(rdev
, rdev0
, mddev
->minor_version
);
5989 "md: %s has different UUID to %s\n",
5990 bdevname(rdev
->bdev
,b
),
5991 bdevname(rdev0
->bdev
,b2
));
5996 err
= bind_rdev_to_array(rdev
, mddev
);
6003 * add_new_disk can be used once the array is assembled
6004 * to add "hot spares". They must already have a superblock
6009 if (!mddev
->pers
->hot_add_disk
) {
6011 "%s: personality does not support diskops!\n",
6015 if (mddev
->persistent
)
6016 rdev
= md_import_device(dev
, mddev
->major_version
,
6017 mddev
->minor_version
);
6019 rdev
= md_import_device(dev
, -1, -1);
6022 "md: md_import_device returned %ld\n",
6024 return PTR_ERR(rdev
);
6026 /* set saved_raid_disk if appropriate */
6027 if (!mddev
->persistent
) {
6028 if (info
->state
& (1<<MD_DISK_SYNC
) &&
6029 info
->raid_disk
< mddev
->raid_disks
) {
6030 rdev
->raid_disk
= info
->raid_disk
;
6031 set_bit(In_sync
, &rdev
->flags
);
6032 clear_bit(Bitmap_sync
, &rdev
->flags
);
6034 rdev
->raid_disk
= -1;
6035 rdev
->saved_raid_disk
= rdev
->raid_disk
;
6037 super_types
[mddev
->major_version
].
6038 validate_super(mddev
, rdev
);
6039 if ((info
->state
& (1<<MD_DISK_SYNC
)) &&
6040 rdev
->raid_disk
!= info
->raid_disk
) {
6041 /* This was a hot-add request, but events doesn't
6042 * match, so reject it.
6048 clear_bit(In_sync
, &rdev
->flags
); /* just to be sure */
6049 if (info
->state
& (1<<MD_DISK_WRITEMOSTLY
))
6050 set_bit(WriteMostly
, &rdev
->flags
);
6052 clear_bit(WriteMostly
, &rdev
->flags
);
6054 if (info
->state
& (1<<MD_DISK_JOURNAL
))
6055 set_bit(Journal
, &rdev
->flags
);
6057 * check whether the device shows up in other nodes
6059 if (mddev_is_clustered(mddev
)) {
6060 if (info
->state
& (1 << MD_DISK_CANDIDATE
))
6061 set_bit(Candidate
, &rdev
->flags
);
6062 else if (info
->state
& (1 << MD_DISK_CLUSTER_ADD
)) {
6063 /* --add initiated by this node */
6064 err
= md_cluster_ops
->add_new_disk(mddev
, rdev
);
6072 rdev
->raid_disk
= -1;
6073 err
= bind_rdev_to_array(rdev
, mddev
);
6078 if (mddev_is_clustered(mddev
)) {
6079 if (info
->state
& (1 << MD_DISK_CANDIDATE
))
6080 md_cluster_ops
->new_disk_ack(mddev
, (err
== 0));
6083 md_cluster_ops
->add_new_disk_cancel(mddev
);
6085 err
= add_bound_rdev(rdev
);
6089 err
= add_bound_rdev(rdev
);
6094 /* otherwise, add_new_disk is only allowed
6095 * for major_version==0 superblocks
6097 if (mddev
->major_version
!= 0) {
6098 printk(KERN_WARNING
"%s: ADD_NEW_DISK not supported\n",
6103 if (!(info
->state
& (1<<MD_DISK_FAULTY
))) {
6105 rdev
= md_import_device(dev
, -1, 0);
6108 "md: error, md_import_device() returned %ld\n",
6110 return PTR_ERR(rdev
);
6112 rdev
->desc_nr
= info
->number
;
6113 if (info
->raid_disk
< mddev
->raid_disks
)
6114 rdev
->raid_disk
= info
->raid_disk
;
6116 rdev
->raid_disk
= -1;
6118 if (rdev
->raid_disk
< mddev
->raid_disks
)
6119 if (info
->state
& (1<<MD_DISK_SYNC
))
6120 set_bit(In_sync
, &rdev
->flags
);
6122 if (info
->state
& (1<<MD_DISK_WRITEMOSTLY
))
6123 set_bit(WriteMostly
, &rdev
->flags
);
6125 if (!mddev
->persistent
) {
6126 printk(KERN_INFO
"md: nonpersistent superblock ...\n");
6127 rdev
->sb_start
= i_size_read(rdev
->bdev
->bd_inode
) / 512;
6129 rdev
->sb_start
= calc_dev_sboffset(rdev
);
6130 rdev
->sectors
= rdev
->sb_start
;
6132 err
= bind_rdev_to_array(rdev
, mddev
);
6142 static int hot_remove_disk(struct mddev
*mddev
, dev_t dev
)
6144 char b
[BDEVNAME_SIZE
];
6145 struct md_rdev
*rdev
;
6151 rdev
= find_rdev(mddev
, dev
);
6155 if (mddev_is_clustered(mddev
))
6156 ret
= md_cluster_ops
->metadata_update_start(mddev
);
6158 if (rdev
->raid_disk
< 0)
6161 clear_bit(Blocked
, &rdev
->flags
);
6162 remove_and_add_spares(mddev
, rdev
);
6164 if (rdev
->raid_disk
>= 0)
6168 if (mddev_is_clustered(mddev
) && ret
== 0)
6169 md_cluster_ops
->remove_disk(mddev
, rdev
);
6171 md_kick_rdev_from_array(rdev
);
6172 md_update_sb(mddev
, 1);
6173 md_new_event(mddev
);
6177 if (mddev_is_clustered(mddev
) && ret
== 0)
6178 md_cluster_ops
->metadata_update_cancel(mddev
);
6180 printk(KERN_WARNING
"md: cannot remove active disk %s from %s ...\n",
6181 bdevname(rdev
->bdev
,b
), mdname(mddev
));
6185 static int hot_add_disk(struct mddev
*mddev
, dev_t dev
)
6187 char b
[BDEVNAME_SIZE
];
6189 struct md_rdev
*rdev
;
6194 if (mddev
->major_version
!= 0) {
6195 printk(KERN_WARNING
"%s: HOT_ADD may only be used with"
6196 " version-0 superblocks.\n",
6200 if (!mddev
->pers
->hot_add_disk
) {
6202 "%s: personality does not support diskops!\n",
6207 rdev
= md_import_device(dev
, -1, 0);
6210 "md: error, md_import_device() returned %ld\n",
6215 if (mddev
->persistent
)
6216 rdev
->sb_start
= calc_dev_sboffset(rdev
);
6218 rdev
->sb_start
= i_size_read(rdev
->bdev
->bd_inode
) / 512;
6220 rdev
->sectors
= rdev
->sb_start
;
6222 if (test_bit(Faulty
, &rdev
->flags
)) {
6224 "md: can not hot-add faulty %s disk to %s!\n",
6225 bdevname(rdev
->bdev
,b
), mdname(mddev
));
6230 clear_bit(In_sync
, &rdev
->flags
);
6232 rdev
->saved_raid_disk
= -1;
6233 err
= bind_rdev_to_array(rdev
, mddev
);
6238 * The rest should better be atomic, we can have disk failures
6239 * noticed in interrupt contexts ...
6242 rdev
->raid_disk
= -1;
6244 md_update_sb(mddev
, 1);
6246 * Kick recovery, maybe this spare has to be added to the
6247 * array immediately.
6249 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6250 md_wakeup_thread(mddev
->thread
);
6251 md_new_event(mddev
);
6259 static int set_bitmap_file(struct mddev
*mddev
, int fd
)
6264 if (!mddev
->pers
->quiesce
|| !mddev
->thread
)
6266 if (mddev
->recovery
|| mddev
->sync_thread
)
6268 /* we should be able to change the bitmap.. */
6272 struct inode
*inode
;
6275 if (mddev
->bitmap
|| mddev
->bitmap_info
.file
)
6276 return -EEXIST
; /* cannot add when bitmap is present */
6280 printk(KERN_ERR
"%s: error: failed to get bitmap file\n",
6285 inode
= f
->f_mapping
->host
;
6286 if (!S_ISREG(inode
->i_mode
)) {
6287 printk(KERN_ERR
"%s: error: bitmap file must be a regular file\n",
6290 } else if (!(f
->f_mode
& FMODE_WRITE
)) {
6291 printk(KERN_ERR
"%s: error: bitmap file must open for write\n",
6294 } else if (atomic_read(&inode
->i_writecount
) != 1) {
6295 printk(KERN_ERR
"%s: error: bitmap file is already in use\n",
6303 mddev
->bitmap_info
.file
= f
;
6304 mddev
->bitmap_info
.offset
= 0; /* file overrides offset */
6305 } else if (mddev
->bitmap
== NULL
)
6306 return -ENOENT
; /* cannot remove what isn't there */
6309 mddev
->pers
->quiesce(mddev
, 1);
6311 struct bitmap
*bitmap
;
6313 bitmap
= bitmap_create(mddev
, -1);
6314 if (!IS_ERR(bitmap
)) {
6315 mddev
->bitmap
= bitmap
;
6316 err
= bitmap_load(mddev
);
6318 err
= PTR_ERR(bitmap
);
6320 if (fd
< 0 || err
) {
6321 bitmap_destroy(mddev
);
6322 fd
= -1; /* make sure to put the file */
6324 mddev
->pers
->quiesce(mddev
, 0);
6327 struct file
*f
= mddev
->bitmap_info
.file
;
6329 spin_lock(&mddev
->lock
);
6330 mddev
->bitmap_info
.file
= NULL
;
6331 spin_unlock(&mddev
->lock
);
6340 * set_array_info is used two different ways
6341 * The original usage is when creating a new array.
6342 * In this usage, raid_disks is > 0 and it together with
6343 * level, size, not_persistent,layout,chunksize determine the
6344 * shape of the array.
6345 * This will always create an array with a type-0.90.0 superblock.
6346 * The newer usage is when assembling an array.
6347 * In this case raid_disks will be 0, and the major_version field is
6348 * use to determine which style super-blocks are to be found on the devices.
6349 * The minor and patch _version numbers are also kept incase the
6350 * super_block handler wishes to interpret them.
6352 static int set_array_info(struct mddev
*mddev
, mdu_array_info_t
*info
)
6355 if (info
->raid_disks
== 0) {
6356 /* just setting version number for superblock loading */
6357 if (info
->major_version
< 0 ||
6358 info
->major_version
>= ARRAY_SIZE(super_types
) ||
6359 super_types
[info
->major_version
].name
== NULL
) {
6360 /* maybe try to auto-load a module? */
6362 "md: superblock version %d not known\n",
6363 info
->major_version
);
6366 mddev
->major_version
= info
->major_version
;
6367 mddev
->minor_version
= info
->minor_version
;
6368 mddev
->patch_version
= info
->patch_version
;
6369 mddev
->persistent
= !info
->not_persistent
;
6370 /* ensure mddev_put doesn't delete this now that there
6371 * is some minimal configuration.
6373 mddev
->ctime
= get_seconds();
6376 mddev
->major_version
= MD_MAJOR_VERSION
;
6377 mddev
->minor_version
= MD_MINOR_VERSION
;
6378 mddev
->patch_version
= MD_PATCHLEVEL_VERSION
;
6379 mddev
->ctime
= get_seconds();
6381 mddev
->level
= info
->level
;
6382 mddev
->clevel
[0] = 0;
6383 mddev
->dev_sectors
= 2 * (sector_t
)info
->size
;
6384 mddev
->raid_disks
= info
->raid_disks
;
6385 /* don't set md_minor, it is determined by which /dev/md* was
6388 if (info
->state
& (1<<MD_SB_CLEAN
))
6389 mddev
->recovery_cp
= MaxSector
;
6391 mddev
->recovery_cp
= 0;
6392 mddev
->persistent
= ! info
->not_persistent
;
6393 mddev
->external
= 0;
6395 mddev
->layout
= info
->layout
;
6396 mddev
->chunk_sectors
= info
->chunk_size
>> 9;
6398 mddev
->max_disks
= MD_SB_DISKS
;
6400 if (mddev
->persistent
)
6402 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
6404 mddev
->bitmap_info
.default_offset
= MD_SB_BYTES
>> 9;
6405 mddev
->bitmap_info
.default_space
= 64*2 - (MD_SB_BYTES
>> 9);
6406 mddev
->bitmap_info
.offset
= 0;
6408 mddev
->reshape_position
= MaxSector
;
6411 * Generate a 128 bit UUID
6413 get_random_bytes(mddev
->uuid
, 16);
6415 mddev
->new_level
= mddev
->level
;
6416 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
6417 mddev
->new_layout
= mddev
->layout
;
6418 mddev
->delta_disks
= 0;
6419 mddev
->reshape_backwards
= 0;
6424 void md_set_array_sectors(struct mddev
*mddev
, sector_t array_sectors
)
6426 WARN(!mddev_is_locked(mddev
), "%s: unlocked mddev!\n", __func__
);
6428 if (mddev
->external_size
)
6431 mddev
->array_sectors
= array_sectors
;
6433 EXPORT_SYMBOL(md_set_array_sectors
);
6435 static int update_size(struct mddev
*mddev
, sector_t num_sectors
)
6437 struct md_rdev
*rdev
;
6439 int fit
= (num_sectors
== 0);
6441 if (mddev
->pers
->resize
== NULL
)
6443 /* The "num_sectors" is the number of sectors of each device that
6444 * is used. This can only make sense for arrays with redundancy.
6445 * linear and raid0 always use whatever space is available. We can only
6446 * consider changing this number if no resync or reconstruction is
6447 * happening, and if the new size is acceptable. It must fit before the
6448 * sb_start or, if that is <data_offset, it must fit before the size
6449 * of each device. If num_sectors is zero, we find the largest size
6452 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
6458 rdev_for_each(rdev
, mddev
) {
6459 sector_t avail
= rdev
->sectors
;
6461 if (fit
&& (num_sectors
== 0 || num_sectors
> avail
))
6462 num_sectors
= avail
;
6463 if (avail
< num_sectors
)
6466 rv
= mddev
->pers
->resize(mddev
, num_sectors
);
6468 revalidate_disk(mddev
->gendisk
);
6472 static int update_raid_disks(struct mddev
*mddev
, int raid_disks
)
6475 struct md_rdev
*rdev
;
6476 /* change the number of raid disks */
6477 if (mddev
->pers
->check_reshape
== NULL
)
6481 if (raid_disks
<= 0 ||
6482 (mddev
->max_disks
&& raid_disks
>= mddev
->max_disks
))
6484 if (mddev
->sync_thread
||
6485 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
6486 mddev
->reshape_position
!= MaxSector
)
6489 rdev_for_each(rdev
, mddev
) {
6490 if (mddev
->raid_disks
< raid_disks
&&
6491 rdev
->data_offset
< rdev
->new_data_offset
)
6493 if (mddev
->raid_disks
> raid_disks
&&
6494 rdev
->data_offset
> rdev
->new_data_offset
)
6498 mddev
->delta_disks
= raid_disks
- mddev
->raid_disks
;
6499 if (mddev
->delta_disks
< 0)
6500 mddev
->reshape_backwards
= 1;
6501 else if (mddev
->delta_disks
> 0)
6502 mddev
->reshape_backwards
= 0;
6504 rv
= mddev
->pers
->check_reshape(mddev
);
6506 mddev
->delta_disks
= 0;
6507 mddev
->reshape_backwards
= 0;
6513 * update_array_info is used to change the configuration of an
6515 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
6516 * fields in the info are checked against the array.
6517 * Any differences that cannot be handled will cause an error.
6518 * Normally, only one change can be managed at a time.
6520 static int update_array_info(struct mddev
*mddev
, mdu_array_info_t
*info
)
6526 /* calculate expected state,ignoring low bits */
6527 if (mddev
->bitmap
&& mddev
->bitmap_info
.offset
)
6528 state
|= (1 << MD_SB_BITMAP_PRESENT
);
6530 if (mddev
->major_version
!= info
->major_version
||
6531 mddev
->minor_version
!= info
->minor_version
||
6532 /* mddev->patch_version != info->patch_version || */
6533 mddev
->ctime
!= info
->ctime
||
6534 mddev
->level
!= info
->level
||
6535 /* mddev->layout != info->layout || */
6536 mddev
->persistent
!= !info
->not_persistent
||
6537 mddev
->chunk_sectors
!= info
->chunk_size
>> 9 ||
6538 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
6539 ((state
^info
->state
) & 0xfffffe00)
6542 /* Check there is only one change */
6543 if (info
->size
>= 0 && mddev
->dev_sectors
/ 2 != info
->size
)
6545 if (mddev
->raid_disks
!= info
->raid_disks
)
6547 if (mddev
->layout
!= info
->layout
)
6549 if ((state
^ info
->state
) & (1<<MD_SB_BITMAP_PRESENT
))
6556 if (mddev
->layout
!= info
->layout
) {
6558 * we don't need to do anything at the md level, the
6559 * personality will take care of it all.
6561 if (mddev
->pers
->check_reshape
== NULL
)
6564 mddev
->new_layout
= info
->layout
;
6565 rv
= mddev
->pers
->check_reshape(mddev
);
6567 mddev
->new_layout
= mddev
->layout
;
6571 if (info
->size
>= 0 && mddev
->dev_sectors
/ 2 != info
->size
)
6572 rv
= update_size(mddev
, (sector_t
)info
->size
* 2);
6574 if (mddev
->raid_disks
!= info
->raid_disks
)
6575 rv
= update_raid_disks(mddev
, info
->raid_disks
);
6577 if ((state
^ info
->state
) & (1<<MD_SB_BITMAP_PRESENT
)) {
6578 if (mddev
->pers
->quiesce
== NULL
|| mddev
->thread
== NULL
) {
6582 if (mddev
->recovery
|| mddev
->sync_thread
) {
6586 if (info
->state
& (1<<MD_SB_BITMAP_PRESENT
)) {
6587 struct bitmap
*bitmap
;
6588 /* add the bitmap */
6589 if (mddev
->bitmap
) {
6593 if (mddev
->bitmap_info
.default_offset
== 0) {
6597 mddev
->bitmap_info
.offset
=
6598 mddev
->bitmap_info
.default_offset
;
6599 mddev
->bitmap_info
.space
=
6600 mddev
->bitmap_info
.default_space
;
6601 mddev
->pers
->quiesce(mddev
, 1);
6602 bitmap
= bitmap_create(mddev
, -1);
6603 if (!IS_ERR(bitmap
)) {
6604 mddev
->bitmap
= bitmap
;
6605 rv
= bitmap_load(mddev
);
6607 rv
= PTR_ERR(bitmap
);
6609 bitmap_destroy(mddev
);
6610 mddev
->pers
->quiesce(mddev
, 0);
6612 /* remove the bitmap */
6613 if (!mddev
->bitmap
) {
6617 if (mddev
->bitmap
->storage
.file
) {
6621 mddev
->pers
->quiesce(mddev
, 1);
6622 bitmap_destroy(mddev
);
6623 mddev
->pers
->quiesce(mddev
, 0);
6624 mddev
->bitmap_info
.offset
= 0;
6627 md_update_sb(mddev
, 1);
6633 static int set_disk_faulty(struct mddev
*mddev
, dev_t dev
)
6635 struct md_rdev
*rdev
;
6638 if (mddev
->pers
== NULL
)
6642 rdev
= find_rdev_rcu(mddev
, dev
);
6646 md_error(mddev
, rdev
);
6647 if (!test_bit(Faulty
, &rdev
->flags
))
6655 * We have a problem here : there is no easy way to give a CHS
6656 * virtual geometry. We currently pretend that we have a 2 heads
6657 * 4 sectors (with a BIG number of cylinders...). This drives
6658 * dosfs just mad... ;-)
6660 static int md_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
6662 struct mddev
*mddev
= bdev
->bd_disk
->private_data
;
6666 geo
->cylinders
= mddev
->array_sectors
/ 8;
6670 static inline bool md_ioctl_valid(unsigned int cmd
)
6675 case GET_ARRAY_INFO
:
6676 case GET_BITMAP_FILE
:
6679 case HOT_REMOVE_DISK
:
6682 case RESTART_ARRAY_RW
:
6684 case SET_ARRAY_INFO
:
6685 case SET_BITMAP_FILE
:
6686 case SET_DISK_FAULTY
:
6689 case CLUSTERED_DISK_NACK
:
6696 static int md_ioctl(struct block_device
*bdev
, fmode_t mode
,
6697 unsigned int cmd
, unsigned long arg
)
6700 void __user
*argp
= (void __user
*)arg
;
6701 struct mddev
*mddev
= NULL
;
6704 if (!md_ioctl_valid(cmd
))
6709 case GET_ARRAY_INFO
:
6713 if (!capable(CAP_SYS_ADMIN
))
6718 * Commands dealing with the RAID driver but not any
6723 err
= get_version(argp
);
6729 autostart_arrays(arg
);
6736 * Commands creating/starting a new array:
6739 mddev
= bdev
->bd_disk
->private_data
;
6746 /* Some actions do not requires the mutex */
6748 case GET_ARRAY_INFO
:
6749 if (!mddev
->raid_disks
&& !mddev
->external
)
6752 err
= get_array_info(mddev
, argp
);
6756 if (!mddev
->raid_disks
&& !mddev
->external
)
6759 err
= get_disk_info(mddev
, argp
);
6762 case SET_DISK_FAULTY
:
6763 err
= set_disk_faulty(mddev
, new_decode_dev(arg
));
6766 case GET_BITMAP_FILE
:
6767 err
= get_bitmap_file(mddev
, argp
);
6772 if (cmd
== ADD_NEW_DISK
)
6773 /* need to ensure md_delayed_delete() has completed */
6774 flush_workqueue(md_misc_wq
);
6776 if (cmd
== HOT_REMOVE_DISK
)
6777 /* need to ensure recovery thread has run */
6778 wait_event_interruptible_timeout(mddev
->sb_wait
,
6779 !test_bit(MD_RECOVERY_NEEDED
,
6781 msecs_to_jiffies(5000));
6782 if (cmd
== STOP_ARRAY
|| cmd
== STOP_ARRAY_RO
) {
6783 /* Need to flush page cache, and ensure no-one else opens
6786 mutex_lock(&mddev
->open_mutex
);
6787 if (mddev
->pers
&& atomic_read(&mddev
->openers
) > 1) {
6788 mutex_unlock(&mddev
->open_mutex
);
6792 set_bit(MD_STILL_CLOSED
, &mddev
->flags
);
6793 mutex_unlock(&mddev
->open_mutex
);
6794 sync_blockdev(bdev
);
6796 err
= mddev_lock(mddev
);
6799 "md: ioctl lock interrupted, reason %d, cmd %d\n",
6804 if (cmd
== SET_ARRAY_INFO
) {
6805 mdu_array_info_t info
;
6807 memset(&info
, 0, sizeof(info
));
6808 else if (copy_from_user(&info
, argp
, sizeof(info
))) {
6813 err
= update_array_info(mddev
, &info
);
6815 printk(KERN_WARNING
"md: couldn't update"
6816 " array info. %d\n", err
);
6821 if (!list_empty(&mddev
->disks
)) {
6823 "md: array %s already has disks!\n",
6828 if (mddev
->raid_disks
) {
6830 "md: array %s already initialised!\n",
6835 err
= set_array_info(mddev
, &info
);
6837 printk(KERN_WARNING
"md: couldn't set"
6838 " array info. %d\n", err
);
6845 * Commands querying/configuring an existing array:
6847 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
6848 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
6849 if ((!mddev
->raid_disks
&& !mddev
->external
)
6850 && cmd
!= ADD_NEW_DISK
&& cmd
!= STOP_ARRAY
6851 && cmd
!= RUN_ARRAY
&& cmd
!= SET_BITMAP_FILE
6852 && cmd
!= GET_BITMAP_FILE
) {
6858 * Commands even a read-only array can execute:
6861 case RESTART_ARRAY_RW
:
6862 err
= restart_array(mddev
);
6866 err
= do_md_stop(mddev
, 0, bdev
);
6870 err
= md_set_readonly(mddev
, bdev
);
6873 case HOT_REMOVE_DISK
:
6874 err
= hot_remove_disk(mddev
, new_decode_dev(arg
));
6878 /* We can support ADD_NEW_DISK on read-only arrays
6879 * on if we are re-adding a preexisting device.
6880 * So require mddev->pers and MD_DISK_SYNC.
6883 mdu_disk_info_t info
;
6884 if (copy_from_user(&info
, argp
, sizeof(info
)))
6886 else if (!(info
.state
& (1<<MD_DISK_SYNC
)))
6887 /* Need to clear read-only for this */
6890 err
= add_new_disk(mddev
, &info
);
6896 if (get_user(ro
, (int __user
*)(arg
))) {
6902 /* if the bdev is going readonly the value of mddev->ro
6903 * does not matter, no writes are coming
6908 /* are we are already prepared for writes? */
6912 /* transitioning to readauto need only happen for
6913 * arrays that call md_write_start
6916 err
= restart_array(mddev
);
6919 set_disk_ro(mddev
->gendisk
, 0);
6926 * The remaining ioctls are changing the state of the
6927 * superblock, so we do not allow them on read-only arrays.
6929 if (mddev
->ro
&& mddev
->pers
) {
6930 if (mddev
->ro
== 2) {
6932 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
6933 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6934 /* mddev_unlock will wake thread */
6935 /* If a device failed while we were read-only, we
6936 * need to make sure the metadata is updated now.
6938 if (test_bit(MD_CHANGE_DEVS
, &mddev
->flags
)) {
6939 mddev_unlock(mddev
);
6940 wait_event(mddev
->sb_wait
,
6941 !test_bit(MD_CHANGE_DEVS
, &mddev
->flags
) &&
6942 !test_bit(MD_CHANGE_PENDING
, &mddev
->flags
));
6943 mddev_lock_nointr(mddev
);
6954 mdu_disk_info_t info
;
6955 if (copy_from_user(&info
, argp
, sizeof(info
)))
6958 err
= add_new_disk(mddev
, &info
);
6962 case CLUSTERED_DISK_NACK
:
6963 if (mddev_is_clustered(mddev
))
6964 md_cluster_ops
->new_disk_ack(mddev
, false);
6970 err
= hot_add_disk(mddev
, new_decode_dev(arg
));
6974 err
= do_md_run(mddev
);
6977 case SET_BITMAP_FILE
:
6978 err
= set_bitmap_file(mddev
, (int)arg
);
6987 if (mddev
->hold_active
== UNTIL_IOCTL
&&
6989 mddev
->hold_active
= 0;
6990 mddev_unlock(mddev
);
6994 #ifdef CONFIG_COMPAT
6995 static int md_compat_ioctl(struct block_device
*bdev
, fmode_t mode
,
6996 unsigned int cmd
, unsigned long arg
)
6999 case HOT_REMOVE_DISK
:
7001 case SET_DISK_FAULTY
:
7002 case SET_BITMAP_FILE
:
7003 /* These take in integer arg, do not convert */
7006 arg
= (unsigned long)compat_ptr(arg
);
7010 return md_ioctl(bdev
, mode
, cmd
, arg
);
7012 #endif /* CONFIG_COMPAT */
7014 static int md_open(struct block_device
*bdev
, fmode_t mode
)
7017 * Succeed if we can lock the mddev, which confirms that
7018 * it isn't being stopped right now.
7020 struct mddev
*mddev
= mddev_find(bdev
->bd_dev
);
7026 if (mddev
->gendisk
!= bdev
->bd_disk
) {
7027 /* we are racing with mddev_put which is discarding this
7031 /* Wait until bdev->bd_disk is definitely gone */
7032 flush_workqueue(md_misc_wq
);
7033 /* Then retry the open from the top */
7034 return -ERESTARTSYS
;
7036 BUG_ON(mddev
!= bdev
->bd_disk
->private_data
);
7038 if ((err
= mutex_lock_interruptible(&mddev
->open_mutex
)))
7042 atomic_inc(&mddev
->openers
);
7043 clear_bit(MD_STILL_CLOSED
, &mddev
->flags
);
7044 mutex_unlock(&mddev
->open_mutex
);
7046 check_disk_change(bdev
);
7051 static void md_release(struct gendisk
*disk
, fmode_t mode
)
7053 struct mddev
*mddev
= disk
->private_data
;
7056 atomic_dec(&mddev
->openers
);
7060 static int md_media_changed(struct gendisk
*disk
)
7062 struct mddev
*mddev
= disk
->private_data
;
7064 return mddev
->changed
;
7067 static int md_revalidate(struct gendisk
*disk
)
7069 struct mddev
*mddev
= disk
->private_data
;
7074 static const struct block_device_operations md_fops
=
7076 .owner
= THIS_MODULE
,
7078 .release
= md_release
,
7080 #ifdef CONFIG_COMPAT
7081 .compat_ioctl
= md_compat_ioctl
,
7083 .getgeo
= md_getgeo
,
7084 .media_changed
= md_media_changed
,
7085 .revalidate_disk
= md_revalidate
,
7088 static int md_thread(void *arg
)
7090 struct md_thread
*thread
= arg
;
7093 * md_thread is a 'system-thread', it's priority should be very
7094 * high. We avoid resource deadlocks individually in each
7095 * raid personality. (RAID5 does preallocation) We also use RR and
7096 * the very same RT priority as kswapd, thus we will never get
7097 * into a priority inversion deadlock.
7099 * we definitely have to have equal or higher priority than
7100 * bdflush, otherwise bdflush will deadlock if there are too
7101 * many dirty RAID5 blocks.
7104 allow_signal(SIGKILL
);
7105 while (!kthread_should_stop()) {
7107 /* We need to wait INTERRUPTIBLE so that
7108 * we don't add to the load-average.
7109 * That means we need to be sure no signals are
7112 if (signal_pending(current
))
7113 flush_signals(current
);
7115 wait_event_interruptible_timeout
7117 test_bit(THREAD_WAKEUP
, &thread
->flags
)
7118 || kthread_should_stop(),
7121 clear_bit(THREAD_WAKEUP
, &thread
->flags
);
7122 if (!kthread_should_stop())
7123 thread
->run(thread
);
7129 void md_wakeup_thread(struct md_thread
*thread
)
7132 pr_debug("md: waking up MD thread %s.\n", thread
->tsk
->comm
);
7133 set_bit(THREAD_WAKEUP
, &thread
->flags
);
7134 wake_up(&thread
->wqueue
);
7137 EXPORT_SYMBOL(md_wakeup_thread
);
7139 struct md_thread
*md_register_thread(void (*run
) (struct md_thread
*),
7140 struct mddev
*mddev
, const char *name
)
7142 struct md_thread
*thread
;
7144 thread
= kzalloc(sizeof(struct md_thread
), GFP_KERNEL
);
7148 init_waitqueue_head(&thread
->wqueue
);
7151 thread
->mddev
= mddev
;
7152 thread
->timeout
= MAX_SCHEDULE_TIMEOUT
;
7153 thread
->tsk
= kthread_run(md_thread
, thread
,
7155 mdname(thread
->mddev
),
7157 if (IS_ERR(thread
->tsk
)) {
7163 EXPORT_SYMBOL(md_register_thread
);
7165 void md_unregister_thread(struct md_thread
**threadp
)
7167 struct md_thread
*thread
= *threadp
;
7170 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread
->tsk
));
7171 /* Locking ensures that mddev_unlock does not wake_up a
7172 * non-existent thread
7174 spin_lock(&pers_lock
);
7176 spin_unlock(&pers_lock
);
7178 kthread_stop(thread
->tsk
);
7181 EXPORT_SYMBOL(md_unregister_thread
);
7183 void md_error(struct mddev
*mddev
, struct md_rdev
*rdev
)
7185 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
))
7188 if (!mddev
->pers
|| !mddev
->pers
->error_handler
)
7190 mddev
->pers
->error_handler(mddev
,rdev
);
7191 if (mddev
->degraded
)
7192 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
7193 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
7194 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
7195 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7196 md_wakeup_thread(mddev
->thread
);
7197 if (mddev
->event_work
.func
)
7198 queue_work(md_misc_wq
, &mddev
->event_work
);
7199 md_new_event_inintr(mddev
);
7201 EXPORT_SYMBOL(md_error
);
7203 /* seq_file implementation /proc/mdstat */
7205 static void status_unused(struct seq_file
*seq
)
7208 struct md_rdev
*rdev
;
7210 seq_printf(seq
, "unused devices: ");
7212 list_for_each_entry(rdev
, &pending_raid_disks
, same_set
) {
7213 char b
[BDEVNAME_SIZE
];
7215 seq_printf(seq
, "%s ",
7216 bdevname(rdev
->bdev
,b
));
7219 seq_printf(seq
, "<none>");
7221 seq_printf(seq
, "\n");
7224 static int status_resync(struct seq_file
*seq
, struct mddev
*mddev
)
7226 sector_t max_sectors
, resync
, res
;
7227 unsigned long dt
, db
;
7230 unsigned int per_milli
;
7232 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ||
7233 test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
7234 max_sectors
= mddev
->resync_max_sectors
;
7236 max_sectors
= mddev
->dev_sectors
;
7238 resync
= mddev
->curr_resync
;
7240 if (test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
))
7241 /* Still cleaning up */
7242 resync
= max_sectors
;
7244 resync
-= atomic_read(&mddev
->recovery_active
);
7247 if (mddev
->recovery_cp
< MaxSector
) {
7248 seq_printf(seq
, "\tresync=PENDING");
7254 seq_printf(seq
, "\tresync=DELAYED");
7258 WARN_ON(max_sectors
== 0);
7259 /* Pick 'scale' such that (resync>>scale)*1000 will fit
7260 * in a sector_t, and (max_sectors>>scale) will fit in a
7261 * u32, as those are the requirements for sector_div.
7262 * Thus 'scale' must be at least 10
7265 if (sizeof(sector_t
) > sizeof(unsigned long)) {
7266 while ( max_sectors
/2 > (1ULL<<(scale
+32)))
7269 res
= (resync
>>scale
)*1000;
7270 sector_div(res
, (u32
)((max_sectors
>>scale
)+1));
7274 int i
, x
= per_milli
/50, y
= 20-x
;
7275 seq_printf(seq
, "[");
7276 for (i
= 0; i
< x
; i
++)
7277 seq_printf(seq
, "=");
7278 seq_printf(seq
, ">");
7279 for (i
= 0; i
< y
; i
++)
7280 seq_printf(seq
, ".");
7281 seq_printf(seq
, "] ");
7283 seq_printf(seq
, " %s =%3u.%u%% (%llu/%llu)",
7284 (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
)?
7286 (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
)?
7288 (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ?
7289 "resync" : "recovery"))),
7290 per_milli
/10, per_milli
% 10,
7291 (unsigned long long) resync
/2,
7292 (unsigned long long) max_sectors
/2);
7295 * dt: time from mark until now
7296 * db: blocks written from mark until now
7297 * rt: remaining time
7299 * rt is a sector_t, so could be 32bit or 64bit.
7300 * So we divide before multiply in case it is 32bit and close
7302 * We scale the divisor (db) by 32 to avoid losing precision
7303 * near the end of resync when the number of remaining sectors
7305 * We then divide rt by 32 after multiplying by db to compensate.
7306 * The '+1' avoids division by zero if db is very small.
7308 dt
= ((jiffies
- mddev
->resync_mark
) / HZ
);
7310 db
= (mddev
->curr_mark_cnt
- atomic_read(&mddev
->recovery_active
))
7311 - mddev
->resync_mark_cnt
;
7313 rt
= max_sectors
- resync
; /* number of remaining sectors */
7314 sector_div(rt
, db
/32+1);
7318 seq_printf(seq
, " finish=%lu.%lumin", (unsigned long)rt
/ 60,
7319 ((unsigned long)rt
% 60)/6);
7321 seq_printf(seq
, " speed=%ldK/sec", db
/2/dt
);
7325 static void *md_seq_start(struct seq_file
*seq
, loff_t
*pos
)
7327 struct list_head
*tmp
;
7329 struct mddev
*mddev
;
7337 spin_lock(&all_mddevs_lock
);
7338 list_for_each(tmp
,&all_mddevs
)
7340 mddev
= list_entry(tmp
, struct mddev
, all_mddevs
);
7342 spin_unlock(&all_mddevs_lock
);
7345 spin_unlock(&all_mddevs_lock
);
7347 return (void*)2;/* tail */
7351 static void *md_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
7353 struct list_head
*tmp
;
7354 struct mddev
*next_mddev
, *mddev
= v
;
7360 spin_lock(&all_mddevs_lock
);
7362 tmp
= all_mddevs
.next
;
7364 tmp
= mddev
->all_mddevs
.next
;
7365 if (tmp
!= &all_mddevs
)
7366 next_mddev
= mddev_get(list_entry(tmp
,struct mddev
,all_mddevs
));
7368 next_mddev
= (void*)2;
7371 spin_unlock(&all_mddevs_lock
);
7379 static void md_seq_stop(struct seq_file
*seq
, void *v
)
7381 struct mddev
*mddev
= v
;
7383 if (mddev
&& v
!= (void*)1 && v
!= (void*)2)
7387 static int md_seq_show(struct seq_file
*seq
, void *v
)
7389 struct mddev
*mddev
= v
;
7391 struct md_rdev
*rdev
;
7393 if (v
== (void*)1) {
7394 struct md_personality
*pers
;
7395 seq_printf(seq
, "Personalities : ");
7396 spin_lock(&pers_lock
);
7397 list_for_each_entry(pers
, &pers_list
, list
)
7398 seq_printf(seq
, "[%s] ", pers
->name
);
7400 spin_unlock(&pers_lock
);
7401 seq_printf(seq
, "\n");
7402 seq
->poll_event
= atomic_read(&md_event_count
);
7405 if (v
== (void*)2) {
7410 spin_lock(&mddev
->lock
);
7411 if (mddev
->pers
|| mddev
->raid_disks
|| !list_empty(&mddev
->disks
)) {
7412 seq_printf(seq
, "%s : %sactive", mdname(mddev
),
7413 mddev
->pers
? "" : "in");
7416 seq_printf(seq
, " (read-only)");
7418 seq_printf(seq
, " (auto-read-only)");
7419 seq_printf(seq
, " %s", mddev
->pers
->name
);
7424 rdev_for_each_rcu(rdev
, mddev
) {
7425 char b
[BDEVNAME_SIZE
];
7426 seq_printf(seq
, " %s[%d]",
7427 bdevname(rdev
->bdev
,b
), rdev
->desc_nr
);
7428 if (test_bit(WriteMostly
, &rdev
->flags
))
7429 seq_printf(seq
, "(W)");
7430 if (test_bit(Journal
, &rdev
->flags
))
7431 seq_printf(seq
, "(J)");
7432 if (test_bit(Faulty
, &rdev
->flags
)) {
7433 seq_printf(seq
, "(F)");
7436 if (rdev
->raid_disk
< 0)
7437 seq_printf(seq
, "(S)"); /* spare */
7438 if (test_bit(Replacement
, &rdev
->flags
))
7439 seq_printf(seq
, "(R)");
7440 sectors
+= rdev
->sectors
;
7444 if (!list_empty(&mddev
->disks
)) {
7446 seq_printf(seq
, "\n %llu blocks",
7447 (unsigned long long)
7448 mddev
->array_sectors
/ 2);
7450 seq_printf(seq
, "\n %llu blocks",
7451 (unsigned long long)sectors
/ 2);
7453 if (mddev
->persistent
) {
7454 if (mddev
->major_version
!= 0 ||
7455 mddev
->minor_version
!= 90) {
7456 seq_printf(seq
," super %d.%d",
7457 mddev
->major_version
,
7458 mddev
->minor_version
);
7460 } else if (mddev
->external
)
7461 seq_printf(seq
, " super external:%s",
7462 mddev
->metadata_type
);
7464 seq_printf(seq
, " super non-persistent");
7467 mddev
->pers
->status(seq
, mddev
);
7468 seq_printf(seq
, "\n ");
7469 if (mddev
->pers
->sync_request
) {
7470 if (status_resync(seq
, mddev
))
7471 seq_printf(seq
, "\n ");
7474 seq_printf(seq
, "\n ");
7476 bitmap_status(seq
, mddev
->bitmap
);
7478 seq_printf(seq
, "\n");
7480 spin_unlock(&mddev
->lock
);
7485 static const struct seq_operations md_seq_ops
= {
7486 .start
= md_seq_start
,
7487 .next
= md_seq_next
,
7488 .stop
= md_seq_stop
,
7489 .show
= md_seq_show
,
7492 static int md_seq_open(struct inode
*inode
, struct file
*file
)
7494 struct seq_file
*seq
;
7497 error
= seq_open(file
, &md_seq_ops
);
7501 seq
= file
->private_data
;
7502 seq
->poll_event
= atomic_read(&md_event_count
);
7506 static int md_unloading
;
7507 static unsigned int mdstat_poll(struct file
*filp
, poll_table
*wait
)
7509 struct seq_file
*seq
= filp
->private_data
;
7513 return POLLIN
|POLLRDNORM
|POLLERR
|POLLPRI
;
7514 poll_wait(filp
, &md_event_waiters
, wait
);
7516 /* always allow read */
7517 mask
= POLLIN
| POLLRDNORM
;
7519 if (seq
->poll_event
!= atomic_read(&md_event_count
))
7520 mask
|= POLLERR
| POLLPRI
;
7524 static const struct file_operations md_seq_fops
= {
7525 .owner
= THIS_MODULE
,
7526 .open
= md_seq_open
,
7528 .llseek
= seq_lseek
,
7529 .release
= seq_release_private
,
7530 .poll
= mdstat_poll
,
7533 int register_md_personality(struct md_personality
*p
)
7535 printk(KERN_INFO
"md: %s personality registered for level %d\n",
7537 spin_lock(&pers_lock
);
7538 list_add_tail(&p
->list
, &pers_list
);
7539 spin_unlock(&pers_lock
);
7542 EXPORT_SYMBOL(register_md_personality
);
7544 int unregister_md_personality(struct md_personality
*p
)
7546 printk(KERN_INFO
"md: %s personality unregistered\n", p
->name
);
7547 spin_lock(&pers_lock
);
7548 list_del_init(&p
->list
);
7549 spin_unlock(&pers_lock
);
7552 EXPORT_SYMBOL(unregister_md_personality
);
7554 int register_md_cluster_operations(struct md_cluster_operations
*ops
,
7555 struct module
*module
)
7558 spin_lock(&pers_lock
);
7559 if (md_cluster_ops
!= NULL
)
7562 md_cluster_ops
= ops
;
7563 md_cluster_mod
= module
;
7565 spin_unlock(&pers_lock
);
7568 EXPORT_SYMBOL(register_md_cluster_operations
);
7570 int unregister_md_cluster_operations(void)
7572 spin_lock(&pers_lock
);
7573 md_cluster_ops
= NULL
;
7574 spin_unlock(&pers_lock
);
7577 EXPORT_SYMBOL(unregister_md_cluster_operations
);
7579 int md_setup_cluster(struct mddev
*mddev
, int nodes
)
7581 if (!md_cluster_ops
)
7582 request_module("md-cluster");
7583 spin_lock(&pers_lock
);
7584 /* ensure module won't be unloaded */
7585 if (!md_cluster_ops
|| !try_module_get(md_cluster_mod
)) {
7586 pr_err("can't find md-cluster module or get it's reference.\n");
7587 spin_unlock(&pers_lock
);
7590 spin_unlock(&pers_lock
);
7592 return md_cluster_ops
->join(mddev
, nodes
);
7595 void md_cluster_stop(struct mddev
*mddev
)
7597 if (!md_cluster_ops
)
7599 md_cluster_ops
->leave(mddev
);
7600 module_put(md_cluster_mod
);
7603 static int is_mddev_idle(struct mddev
*mddev
, int init
)
7605 struct md_rdev
*rdev
;
7611 rdev_for_each_rcu(rdev
, mddev
) {
7612 struct gendisk
*disk
= rdev
->bdev
->bd_contains
->bd_disk
;
7613 curr_events
= (int)part_stat_read(&disk
->part0
, sectors
[0]) +
7614 (int)part_stat_read(&disk
->part0
, sectors
[1]) -
7615 atomic_read(&disk
->sync_io
);
7616 /* sync IO will cause sync_io to increase before the disk_stats
7617 * as sync_io is counted when a request starts, and
7618 * disk_stats is counted when it completes.
7619 * So resync activity will cause curr_events to be smaller than
7620 * when there was no such activity.
7621 * non-sync IO will cause disk_stat to increase without
7622 * increasing sync_io so curr_events will (eventually)
7623 * be larger than it was before. Once it becomes
7624 * substantially larger, the test below will cause
7625 * the array to appear non-idle, and resync will slow
7627 * If there is a lot of outstanding resync activity when
7628 * we set last_event to curr_events, then all that activity
7629 * completing might cause the array to appear non-idle
7630 * and resync will be slowed down even though there might
7631 * not have been non-resync activity. This will only
7632 * happen once though. 'last_events' will soon reflect
7633 * the state where there is little or no outstanding
7634 * resync requests, and further resync activity will
7635 * always make curr_events less than last_events.
7638 if (init
|| curr_events
- rdev
->last_events
> 64) {
7639 rdev
->last_events
= curr_events
;
7647 void md_done_sync(struct mddev
*mddev
, int blocks
, int ok
)
7649 /* another "blocks" (512byte) blocks have been synced */
7650 atomic_sub(blocks
, &mddev
->recovery_active
);
7651 wake_up(&mddev
->recovery_wait
);
7653 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
7654 set_bit(MD_RECOVERY_ERROR
, &mddev
->recovery
);
7655 md_wakeup_thread(mddev
->thread
);
7656 // stop recovery, signal do_sync ....
7659 EXPORT_SYMBOL(md_done_sync
);
7661 /* md_write_start(mddev, bi)
7662 * If we need to update some array metadata (e.g. 'active' flag
7663 * in superblock) before writing, schedule a superblock update
7664 * and wait for it to complete.
7666 void md_write_start(struct mddev
*mddev
, struct bio
*bi
)
7669 if (bio_data_dir(bi
) != WRITE
)
7672 BUG_ON(mddev
->ro
== 1);
7673 if (mddev
->ro
== 2) {
7674 /* need to switch to read/write */
7676 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7677 md_wakeup_thread(mddev
->thread
);
7678 md_wakeup_thread(mddev
->sync_thread
);
7681 atomic_inc(&mddev
->writes_pending
);
7682 if (mddev
->safemode
== 1)
7683 mddev
->safemode
= 0;
7684 if (mddev
->in_sync
) {
7685 spin_lock(&mddev
->lock
);
7686 if (mddev
->in_sync
) {
7688 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
7689 set_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
7690 md_wakeup_thread(mddev
->thread
);
7693 spin_unlock(&mddev
->lock
);
7696 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
7697 wait_event(mddev
->sb_wait
,
7698 !test_bit(MD_CHANGE_PENDING
, &mddev
->flags
));
7700 EXPORT_SYMBOL(md_write_start
);
7702 void md_write_end(struct mddev
*mddev
)
7704 if (atomic_dec_and_test(&mddev
->writes_pending
)) {
7705 if (mddev
->safemode
== 2)
7706 md_wakeup_thread(mddev
->thread
);
7707 else if (mddev
->safemode_delay
)
7708 mod_timer(&mddev
->safemode_timer
, jiffies
+ mddev
->safemode_delay
);
7711 EXPORT_SYMBOL(md_write_end
);
7713 /* md_allow_write(mddev)
7714 * Calling this ensures that the array is marked 'active' so that writes
7715 * may proceed without blocking. It is important to call this before
7716 * attempting a GFP_KERNEL allocation while holding the mddev lock.
7717 * Must be called with mddev_lock held.
7719 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
7720 * is dropped, so return -EAGAIN after notifying userspace.
7722 int md_allow_write(struct mddev
*mddev
)
7728 if (!mddev
->pers
->sync_request
)
7731 spin_lock(&mddev
->lock
);
7732 if (mddev
->in_sync
) {
7734 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
7735 set_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
7736 if (mddev
->safemode_delay
&&
7737 mddev
->safemode
== 0)
7738 mddev
->safemode
= 1;
7739 spin_unlock(&mddev
->lock
);
7740 md_update_sb(mddev
, 0);
7741 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
7743 spin_unlock(&mddev
->lock
);
7745 if (test_bit(MD_CHANGE_PENDING
, &mddev
->flags
))
7750 EXPORT_SYMBOL_GPL(md_allow_write
);
7752 #define SYNC_MARKS 10
7753 #define SYNC_MARK_STEP (3*HZ)
7754 #define UPDATE_FREQUENCY (5*60*HZ)
7755 void md_do_sync(struct md_thread
*thread
)
7757 struct mddev
*mddev
= thread
->mddev
;
7758 struct mddev
*mddev2
;
7759 unsigned int currspeed
= 0,
7761 sector_t max_sectors
,j
, io_sectors
, recovery_done
;
7762 unsigned long mark
[SYNC_MARKS
];
7763 unsigned long update_time
;
7764 sector_t mark_cnt
[SYNC_MARKS
];
7766 struct list_head
*tmp
;
7767 sector_t last_check
;
7769 struct md_rdev
*rdev
;
7770 char *desc
, *action
= NULL
;
7771 struct blk_plug plug
;
7772 bool cluster_resync_finished
= false;
7774 /* just incase thread restarts... */
7775 if (test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
))
7777 if (mddev
->ro
) {/* never try to sync a read-only array */
7778 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
7782 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
7783 if (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
)) {
7784 desc
= "data-check";
7786 } else if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
)) {
7787 desc
= "requested-resync";
7791 } else if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
7796 mddev
->last_sync_action
= action
?: desc
;
7798 /* we overload curr_resync somewhat here.
7799 * 0 == not engaged in resync at all
7800 * 2 == checking that there is no conflict with another sync
7801 * 1 == like 2, but have yielded to allow conflicting resync to
7803 * other == active in resync - this many blocks
7805 * Before starting a resync we must have set curr_resync to
7806 * 2, and then checked that every "conflicting" array has curr_resync
7807 * less than ours. When we find one that is the same or higher
7808 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
7809 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
7810 * This will mean we have to start checking from the beginning again.
7815 mddev
->curr_resync
= 2;
7818 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
7820 for_each_mddev(mddev2
, tmp
) {
7821 if (mddev2
== mddev
)
7823 if (!mddev
->parallel_resync
7824 && mddev2
->curr_resync
7825 && match_mddev_units(mddev
, mddev2
)) {
7827 if (mddev
< mddev2
&& mddev
->curr_resync
== 2) {
7828 /* arbitrarily yield */
7829 mddev
->curr_resync
= 1;
7830 wake_up(&resync_wait
);
7832 if (mddev
> mddev2
&& mddev
->curr_resync
== 1)
7833 /* no need to wait here, we can wait the next
7834 * time 'round when curr_resync == 2
7837 /* We need to wait 'interruptible' so as not to
7838 * contribute to the load average, and not to
7839 * be caught by 'softlockup'
7841 prepare_to_wait(&resync_wait
, &wq
, TASK_INTERRUPTIBLE
);
7842 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
) &&
7843 mddev2
->curr_resync
>= mddev
->curr_resync
) {
7844 printk(KERN_INFO
"md: delaying %s of %s"
7845 " until %s has finished (they"
7846 " share one or more physical units)\n",
7847 desc
, mdname(mddev
), mdname(mddev2
));
7849 if (signal_pending(current
))
7850 flush_signals(current
);
7852 finish_wait(&resync_wait
, &wq
);
7855 finish_wait(&resync_wait
, &wq
);
7858 } while (mddev
->curr_resync
< 2);
7861 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
7862 /* resync follows the size requested by the personality,
7863 * which defaults to physical size, but can be virtual size
7865 max_sectors
= mddev
->resync_max_sectors
;
7866 atomic64_set(&mddev
->resync_mismatches
, 0);
7867 /* we don't use the checkpoint if there's a bitmap */
7868 if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
7869 j
= mddev
->resync_min
;
7870 else if (!mddev
->bitmap
)
7871 j
= mddev
->recovery_cp
;
7873 } else if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
7874 max_sectors
= mddev
->resync_max_sectors
;
7876 /* recovery follows the physical size of devices */
7877 max_sectors
= mddev
->dev_sectors
;
7880 rdev_for_each_rcu(rdev
, mddev
)
7881 if (rdev
->raid_disk
>= 0 &&
7882 !test_bit(Journal
, &rdev
->flags
) &&
7883 !test_bit(Faulty
, &rdev
->flags
) &&
7884 !test_bit(In_sync
, &rdev
->flags
) &&
7885 rdev
->recovery_offset
< j
)
7886 j
= rdev
->recovery_offset
;
7889 /* If there is a bitmap, we need to make sure all
7890 * writes that started before we added a spare
7891 * complete before we start doing a recovery.
7892 * Otherwise the write might complete and (via
7893 * bitmap_endwrite) set a bit in the bitmap after the
7894 * recovery has checked that bit and skipped that
7897 if (mddev
->bitmap
) {
7898 mddev
->pers
->quiesce(mddev
, 1);
7899 mddev
->pers
->quiesce(mddev
, 0);
7903 printk(KERN_INFO
"md: %s of RAID array %s\n", desc
, mdname(mddev
));
7904 printk(KERN_INFO
"md: minimum _guaranteed_ speed:"
7905 " %d KB/sec/disk.\n", speed_min(mddev
));
7906 printk(KERN_INFO
"md: using maximum available idle IO bandwidth "
7907 "(but not more than %d KB/sec) for %s.\n",
7908 speed_max(mddev
), desc
);
7910 is_mddev_idle(mddev
, 1); /* this initializes IO event counters */
7913 for (m
= 0; m
< SYNC_MARKS
; m
++) {
7915 mark_cnt
[m
] = io_sectors
;
7918 mddev
->resync_mark
= mark
[last_mark
];
7919 mddev
->resync_mark_cnt
= mark_cnt
[last_mark
];
7922 * Tune reconstruction:
7924 window
= 32*(PAGE_SIZE
/512);
7925 printk(KERN_INFO
"md: using %dk window, over a total of %lluk.\n",
7926 window
/2, (unsigned long long)max_sectors
/2);
7928 atomic_set(&mddev
->recovery_active
, 0);
7933 "md: resuming %s of %s from checkpoint.\n",
7934 desc
, mdname(mddev
));
7935 mddev
->curr_resync
= j
;
7937 mddev
->curr_resync
= 3; /* no longer delayed */
7938 mddev
->curr_resync_completed
= j
;
7939 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
7940 md_new_event(mddev
);
7941 update_time
= jiffies
;
7943 blk_start_plug(&plug
);
7944 while (j
< max_sectors
) {
7949 if (!test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
7950 ((mddev
->curr_resync
> mddev
->curr_resync_completed
&&
7951 (mddev
->curr_resync
- mddev
->curr_resync_completed
)
7952 > (max_sectors
>> 4)) ||
7953 time_after_eq(jiffies
, update_time
+ UPDATE_FREQUENCY
) ||
7954 (j
- mddev
->curr_resync_completed
)*2
7955 >= mddev
->resync_max
- mddev
->curr_resync_completed
||
7956 mddev
->curr_resync_completed
> mddev
->resync_max
7958 /* time to update curr_resync_completed */
7959 wait_event(mddev
->recovery_wait
,
7960 atomic_read(&mddev
->recovery_active
) == 0);
7961 mddev
->curr_resync_completed
= j
;
7962 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) &&
7963 j
> mddev
->recovery_cp
)
7964 mddev
->recovery_cp
= j
;
7965 update_time
= jiffies
;
7966 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
7967 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
7970 while (j
>= mddev
->resync_max
&&
7971 !test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
7972 /* As this condition is controlled by user-space,
7973 * we can block indefinitely, so use '_interruptible'
7974 * to avoid triggering warnings.
7976 flush_signals(current
); /* just in case */
7977 wait_event_interruptible(mddev
->recovery_wait
,
7978 mddev
->resync_max
> j
7979 || test_bit(MD_RECOVERY_INTR
,
7983 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
7986 sectors
= mddev
->pers
->sync_request(mddev
, j
, &skipped
);
7988 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
7992 if (!skipped
) { /* actual IO requested */
7993 io_sectors
+= sectors
;
7994 atomic_add(sectors
, &mddev
->recovery_active
);
7997 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
8001 if (j
> max_sectors
)
8002 /* when skipping, extra large numbers can be returned. */
8005 mddev
->curr_resync
= j
;
8006 mddev
->curr_mark_cnt
= io_sectors
;
8007 if (last_check
== 0)
8008 /* this is the earliest that rebuild will be
8009 * visible in /proc/mdstat
8011 md_new_event(mddev
);
8013 if (last_check
+ window
> io_sectors
|| j
== max_sectors
)
8016 last_check
= io_sectors
;
8018 if (time_after_eq(jiffies
, mark
[last_mark
] + SYNC_MARK_STEP
)) {
8020 int next
= (last_mark
+1) % SYNC_MARKS
;
8022 mddev
->resync_mark
= mark
[next
];
8023 mddev
->resync_mark_cnt
= mark_cnt
[next
];
8024 mark
[next
] = jiffies
;
8025 mark_cnt
[next
] = io_sectors
- atomic_read(&mddev
->recovery_active
);
8029 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
8033 * this loop exits only if either when we are slower than
8034 * the 'hard' speed limit, or the system was IO-idle for
8036 * the system might be non-idle CPU-wise, but we only care
8037 * about not overloading the IO subsystem. (things like an
8038 * e2fsck being done on the RAID array should execute fast)
8042 recovery_done
= io_sectors
- atomic_read(&mddev
->recovery_active
);
8043 currspeed
= ((unsigned long)(recovery_done
- mddev
->resync_mark_cnt
))/2
8044 /((jiffies
-mddev
->resync_mark
)/HZ
+1) +1;
8046 if (currspeed
> speed_min(mddev
)) {
8047 if (currspeed
> speed_max(mddev
)) {
8051 if (!is_mddev_idle(mddev
, 0)) {
8053 * Give other IO more of a chance.
8054 * The faster the devices, the less we wait.
8056 wait_event(mddev
->recovery_wait
,
8057 !atomic_read(&mddev
->recovery_active
));
8061 printk(KERN_INFO
"md: %s: %s %s.\n",mdname(mddev
), desc
,
8062 test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)
8063 ? "interrupted" : "done");
8065 * this also signals 'finished resyncing' to md_stop
8067 blk_finish_plug(&plug
);
8068 wait_event(mddev
->recovery_wait
, !atomic_read(&mddev
->recovery_active
));
8070 if (!test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
8071 !test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
) &&
8072 mddev
->curr_resync
> 2) {
8073 mddev
->curr_resync_completed
= mddev
->curr_resync
;
8074 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
8076 /* tell personality and other nodes that we are finished */
8077 if (mddev_is_clustered(mddev
)) {
8078 md_cluster_ops
->resync_finish(mddev
);
8079 cluster_resync_finished
= true;
8081 mddev
->pers
->sync_request(mddev
, max_sectors
, &skipped
);
8083 if (!test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
) &&
8084 mddev
->curr_resync
> 2) {
8085 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
8086 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
8087 if (mddev
->curr_resync
>= mddev
->recovery_cp
) {
8089 "md: checkpointing %s of %s.\n",
8090 desc
, mdname(mddev
));
8091 if (test_bit(MD_RECOVERY_ERROR
,
8093 mddev
->recovery_cp
=
8094 mddev
->curr_resync_completed
;
8096 mddev
->recovery_cp
=
8100 mddev
->recovery_cp
= MaxSector
;
8102 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
8103 mddev
->curr_resync
= MaxSector
;
8105 rdev_for_each_rcu(rdev
, mddev
)
8106 if (rdev
->raid_disk
>= 0 &&
8107 mddev
->delta_disks
>= 0 &&
8108 !test_bit(Journal
, &rdev
->flags
) &&
8109 !test_bit(Faulty
, &rdev
->flags
) &&
8110 !test_bit(In_sync
, &rdev
->flags
) &&
8111 rdev
->recovery_offset
< mddev
->curr_resync
)
8112 rdev
->recovery_offset
= mddev
->curr_resync
;
8117 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
8119 if (mddev_is_clustered(mddev
) &&
8120 test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
) &&
8121 !cluster_resync_finished
)
8122 md_cluster_ops
->resync_finish(mddev
);
8124 spin_lock(&mddev
->lock
);
8125 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
8126 /* We completed so min/max setting can be forgotten if used. */
8127 if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
8128 mddev
->resync_min
= 0;
8129 mddev
->resync_max
= MaxSector
;
8130 } else if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
8131 mddev
->resync_min
= mddev
->curr_resync_completed
;
8132 set_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
8133 mddev
->curr_resync
= 0;
8134 spin_unlock(&mddev
->lock
);
8136 wake_up(&resync_wait
);
8137 md_wakeup_thread(mddev
->thread
);
8140 EXPORT_SYMBOL_GPL(md_do_sync
);
8142 static int remove_and_add_spares(struct mddev
*mddev
,
8143 struct md_rdev
*this)
8145 struct md_rdev
*rdev
;
8149 rdev_for_each(rdev
, mddev
)
8150 if ((this == NULL
|| rdev
== this) &&
8151 rdev
->raid_disk
>= 0 &&
8152 !test_bit(Blocked
, &rdev
->flags
) &&
8153 (test_bit(Faulty
, &rdev
->flags
) ||
8154 (!test_bit(In_sync
, &rdev
->flags
) &&
8155 !test_bit(Journal
, &rdev
->flags
))) &&
8156 atomic_read(&rdev
->nr_pending
)==0) {
8157 if (mddev
->pers
->hot_remove_disk(
8158 mddev
, rdev
) == 0) {
8159 sysfs_unlink_rdev(mddev
, rdev
);
8160 rdev
->saved_raid_disk
= rdev
->raid_disk
;
8161 rdev
->raid_disk
= -1;
8165 if (removed
&& mddev
->kobj
.sd
)
8166 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
8168 if (this && removed
)
8171 rdev_for_each(rdev
, mddev
) {
8172 if (this && this != rdev
)
8174 if (test_bit(Candidate
, &rdev
->flags
))
8176 if (rdev
->raid_disk
>= 0 &&
8177 !test_bit(In_sync
, &rdev
->flags
) &&
8178 !test_bit(Journal
, &rdev
->flags
) &&
8179 !test_bit(Faulty
, &rdev
->flags
))
8181 if (rdev
->raid_disk
>= 0)
8183 if (test_bit(Faulty
, &rdev
->flags
))
8185 if (test_bit(Journal
, &rdev
->flags
))
8188 ! (rdev
->saved_raid_disk
>= 0 &&
8189 !test_bit(Bitmap_sync
, &rdev
->flags
)))
8192 rdev
->recovery_offset
= 0;
8194 hot_add_disk(mddev
, rdev
) == 0) {
8195 if (sysfs_link_rdev(mddev
, rdev
))
8196 /* failure here is OK */;
8198 md_new_event(mddev
);
8199 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
8204 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
8208 static void md_start_sync(struct work_struct
*ws
)
8210 struct mddev
*mddev
= container_of(ws
, struct mddev
, del_work
);
8213 if (mddev_is_clustered(mddev
)) {
8214 ret
= md_cluster_ops
->resync_start(mddev
);
8216 mddev
->sync_thread
= NULL
;
8221 mddev
->sync_thread
= md_register_thread(md_do_sync
,
8225 if (!mddev
->sync_thread
) {
8226 if (!(mddev_is_clustered(mddev
) && ret
== -EAGAIN
))
8227 printk(KERN_ERR
"%s: could not start resync"
8230 /* leave the spares where they are, it shouldn't hurt */
8231 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
8232 clear_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
8233 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
8234 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
8235 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
8236 wake_up(&resync_wait
);
8237 if (test_and_clear_bit(MD_RECOVERY_RECOVER
,
8239 if (mddev
->sysfs_action
)
8240 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
8242 md_wakeup_thread(mddev
->sync_thread
);
8243 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
8244 md_new_event(mddev
);
8248 * This routine is regularly called by all per-raid-array threads to
8249 * deal with generic issues like resync and super-block update.
8250 * Raid personalities that don't have a thread (linear/raid0) do not
8251 * need this as they never do any recovery or update the superblock.
8253 * It does not do any resync itself, but rather "forks" off other threads
8254 * to do that as needed.
8255 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
8256 * "->recovery" and create a thread at ->sync_thread.
8257 * When the thread finishes it sets MD_RECOVERY_DONE
8258 * and wakeups up this thread which will reap the thread and finish up.
8259 * This thread also removes any faulty devices (with nr_pending == 0).
8261 * The overall approach is:
8262 * 1/ if the superblock needs updating, update it.
8263 * 2/ If a recovery thread is running, don't do anything else.
8264 * 3/ If recovery has finished, clean up, possibly marking spares active.
8265 * 4/ If there are any faulty devices, remove them.
8266 * 5/ If array is degraded, try to add spares devices
8267 * 6/ If array has spares or is not in-sync, start a resync thread.
8269 void md_check_recovery(struct mddev
*mddev
)
8271 if (mddev
->suspended
)
8275 bitmap_daemon_work(mddev
);
8277 if (signal_pending(current
)) {
8278 if (mddev
->pers
->sync_request
&& !mddev
->external
) {
8279 printk(KERN_INFO
"md: %s in immediate safe mode\n",
8281 mddev
->safemode
= 2;
8283 flush_signals(current
);
8286 if (mddev
->ro
&& !test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))
8289 (mddev
->flags
& MD_UPDATE_SB_FLAGS
& ~ (1<<MD_CHANGE_PENDING
)) ||
8290 test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
) ||
8291 test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
) ||
8292 (mddev
->external
== 0 && mddev
->safemode
== 1) ||
8293 (mddev
->safemode
== 2 && ! atomic_read(&mddev
->writes_pending
)
8294 && !mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
)
8298 if (mddev_trylock(mddev
)) {
8302 struct md_rdev
*rdev
;
8303 if (!mddev
->external
&& mddev
->in_sync
)
8304 /* 'Blocked' flag not needed as failed devices
8305 * will be recorded if array switched to read/write.
8306 * Leaving it set will prevent the device
8307 * from being removed.
8309 rdev_for_each(rdev
, mddev
)
8310 clear_bit(Blocked
, &rdev
->flags
);
8311 /* On a read-only array we can:
8312 * - remove failed devices
8313 * - add already-in_sync devices if the array itself
8315 * As we only add devices that are already in-sync,
8316 * we can activate the spares immediately.
8318 remove_and_add_spares(mddev
, NULL
);
8319 /* There is no thread, but we need to call
8320 * ->spare_active and clear saved_raid_disk
8322 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
8323 md_reap_sync_thread(mddev
);
8324 clear_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
8325 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
8326 clear_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
8330 if (!mddev
->external
) {
8332 spin_lock(&mddev
->lock
);
8333 if (mddev
->safemode
&&
8334 !atomic_read(&mddev
->writes_pending
) &&
8336 mddev
->recovery_cp
== MaxSector
) {
8339 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
8341 if (mddev
->safemode
== 1)
8342 mddev
->safemode
= 0;
8343 spin_unlock(&mddev
->lock
);
8345 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
8348 if (mddev
->flags
& MD_UPDATE_SB_FLAGS
)
8349 md_update_sb(mddev
, 0);
8351 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) &&
8352 !test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
)) {
8353 /* resync/recovery still happening */
8354 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
8357 if (mddev
->sync_thread
) {
8358 md_reap_sync_thread(mddev
);
8361 /* Set RUNNING before clearing NEEDED to avoid
8362 * any transients in the value of "sync_action".
8364 mddev
->curr_resync_completed
= 0;
8365 spin_lock(&mddev
->lock
);
8366 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
8367 spin_unlock(&mddev
->lock
);
8368 /* Clear some bits that don't mean anything, but
8371 clear_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
8372 clear_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
8374 if (!test_and_clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
) ||
8375 test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
))
8377 /* no recovery is running.
8378 * remove any failed drives, then
8379 * add spares if possible.
8380 * Spares are also removed and re-added, to allow
8381 * the personality to fail the re-add.
8384 if (mddev
->reshape_position
!= MaxSector
) {
8385 if (mddev
->pers
->check_reshape
== NULL
||
8386 mddev
->pers
->check_reshape(mddev
) != 0)
8387 /* Cannot proceed */
8389 set_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
8390 clear_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
8391 } else if ((spares
= remove_and_add_spares(mddev
, NULL
))) {
8392 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
8393 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
8394 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
8395 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
8396 } else if (mddev
->recovery_cp
< MaxSector
) {
8397 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
8398 clear_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
8399 } else if (!test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
8400 /* nothing to be done ... */
8403 if (mddev
->pers
->sync_request
) {
8405 /* We are adding a device or devices to an array
8406 * which has the bitmap stored on all devices.
8407 * So make sure all bitmap pages get written
8409 bitmap_write_all(mddev
->bitmap
);
8411 INIT_WORK(&mddev
->del_work
, md_start_sync
);
8412 queue_work(md_misc_wq
, &mddev
->del_work
);
8416 if (!mddev
->sync_thread
) {
8417 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
8418 wake_up(&resync_wait
);
8419 if (test_and_clear_bit(MD_RECOVERY_RECOVER
,
8421 if (mddev
->sysfs_action
)
8422 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
8425 wake_up(&mddev
->sb_wait
);
8426 mddev_unlock(mddev
);
8429 EXPORT_SYMBOL(md_check_recovery
);
8431 void md_reap_sync_thread(struct mddev
*mddev
)
8433 struct md_rdev
*rdev
;
8435 /* resync has finished, collect result */
8436 md_unregister_thread(&mddev
->sync_thread
);
8437 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
) &&
8438 !test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
)) {
8440 /* activate any spares */
8441 if (mddev
->pers
->spare_active(mddev
)) {
8442 sysfs_notify(&mddev
->kobj
, NULL
,
8444 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
8447 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
8448 mddev
->pers
->finish_reshape
)
8449 mddev
->pers
->finish_reshape(mddev
);
8451 /* If array is no-longer degraded, then any saved_raid_disk
8452 * information must be scrapped.
8454 if (!mddev
->degraded
)
8455 rdev_for_each(rdev
, mddev
)
8456 rdev
->saved_raid_disk
= -1;
8458 md_update_sb(mddev
, 1);
8459 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
8460 clear_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
8461 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
8462 clear_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
8463 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
8464 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
8465 wake_up(&resync_wait
);
8466 /* flag recovery needed just to double check */
8467 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
8468 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
8469 md_new_event(mddev
);
8470 if (mddev
->event_work
.func
)
8471 queue_work(md_misc_wq
, &mddev
->event_work
);
8473 EXPORT_SYMBOL(md_reap_sync_thread
);
8475 void md_wait_for_blocked_rdev(struct md_rdev
*rdev
, struct mddev
*mddev
)
8477 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
8478 wait_event_timeout(rdev
->blocked_wait
,
8479 !test_bit(Blocked
, &rdev
->flags
) &&
8480 !test_bit(BlockedBadBlocks
, &rdev
->flags
),
8481 msecs_to_jiffies(5000));
8482 rdev_dec_pending(rdev
, mddev
);
8484 EXPORT_SYMBOL(md_wait_for_blocked_rdev
);
8486 void md_finish_reshape(struct mddev
*mddev
)
8488 /* called be personality module when reshape completes. */
8489 struct md_rdev
*rdev
;
8491 rdev_for_each(rdev
, mddev
) {
8492 if (rdev
->data_offset
> rdev
->new_data_offset
)
8493 rdev
->sectors
+= rdev
->data_offset
- rdev
->new_data_offset
;
8495 rdev
->sectors
-= rdev
->new_data_offset
- rdev
->data_offset
;
8496 rdev
->data_offset
= rdev
->new_data_offset
;
8499 EXPORT_SYMBOL(md_finish_reshape
);
8501 /* Bad block management.
8502 * We can record which blocks on each device are 'bad' and so just
8503 * fail those blocks, or that stripe, rather than the whole device.
8504 * Entries in the bad-block table are 64bits wide. This comprises:
8505 * Length of bad-range, in sectors: 0-511 for lengths 1-512
8506 * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes)
8507 * A 'shift' can be set so that larger blocks are tracked and
8508 * consequently larger devices can be covered.
8509 * 'Acknowledged' flag - 1 bit. - the most significant bit.
8511 * Locking of the bad-block table uses a seqlock so md_is_badblock
8512 * might need to retry if it is very unlucky.
8513 * We will sometimes want to check for bad blocks in a bi_end_io function,
8514 * so we use the write_seqlock_irq variant.
8516 * When looking for a bad block we specify a range and want to
8517 * know if any block in the range is bad. So we binary-search
8518 * to the last range that starts at-or-before the given endpoint,
8519 * (or "before the sector after the target range")
8520 * then see if it ends after the given start.
8522 * 0 if there are no known bad blocks in the range
8523 * 1 if there are known bad block which are all acknowledged
8524 * -1 if there are bad blocks which have not yet been acknowledged in metadata.
8525 * plus the start/length of the first bad section we overlap.
8527 int md_is_badblock(struct badblocks
*bb
, sector_t s
, int sectors
,
8528 sector_t
*first_bad
, int *bad_sectors
)
8534 sector_t target
= s
+ sectors
;
8537 if (bb
->shift
> 0) {
8538 /* round the start down, and the end up */
8540 target
+= (1<<bb
->shift
) - 1;
8541 target
>>= bb
->shift
;
8542 sectors
= target
- s
;
8544 /* 'target' is now the first block after the bad range */
8547 seq
= read_seqbegin(&bb
->lock
);
8552 /* Binary search between lo and hi for 'target'
8553 * i.e. for the last range that starts before 'target'
8555 /* INVARIANT: ranges before 'lo' and at-or-after 'hi'
8556 * are known not to be the last range before target.
8557 * VARIANT: hi-lo is the number of possible
8558 * ranges, and decreases until it reaches 1
8560 while (hi
- lo
> 1) {
8561 int mid
= (lo
+ hi
) / 2;
8562 sector_t a
= BB_OFFSET(p
[mid
]);
8564 /* This could still be the one, earlier ranges
8568 /* This and later ranges are definitely out. */
8571 /* 'lo' might be the last that started before target, but 'hi' isn't */
8573 /* need to check all range that end after 's' to see if
8574 * any are unacknowledged.
8577 BB_OFFSET(p
[lo
]) + BB_LEN(p
[lo
]) > s
) {
8578 if (BB_OFFSET(p
[lo
]) < target
) {
8579 /* starts before the end, and finishes after
8580 * the start, so they must overlap
8582 if (rv
!= -1 && BB_ACK(p
[lo
]))
8586 *first_bad
= BB_OFFSET(p
[lo
]);
8587 *bad_sectors
= BB_LEN(p
[lo
]);
8593 if (read_seqretry(&bb
->lock
, seq
))
8598 EXPORT_SYMBOL_GPL(md_is_badblock
);
8601 * Add a range of bad blocks to the table.
8602 * This might extend the table, or might contract it
8603 * if two adjacent ranges can be merged.
8604 * We binary-search to find the 'insertion' point, then
8605 * decide how best to handle it.
8607 static int md_set_badblocks(struct badblocks
*bb
, sector_t s
, int sectors
,
8613 unsigned long flags
;
8616 /* badblocks are disabled */
8620 /* round the start down, and the end up */
8621 sector_t next
= s
+ sectors
;
8623 next
+= (1<<bb
->shift
) - 1;
8628 write_seqlock_irqsave(&bb
->lock
, flags
);
8633 /* Find the last range that starts at-or-before 's' */
8634 while (hi
- lo
> 1) {
8635 int mid
= (lo
+ hi
) / 2;
8636 sector_t a
= BB_OFFSET(p
[mid
]);
8642 if (hi
> lo
&& BB_OFFSET(p
[lo
]) > s
)
8646 /* we found a range that might merge with the start
8649 sector_t a
= BB_OFFSET(p
[lo
]);
8650 sector_t e
= a
+ BB_LEN(p
[lo
]);
8651 int ack
= BB_ACK(p
[lo
]);
8653 /* Yes, we can merge with a previous range */
8654 if (s
== a
&& s
+ sectors
>= e
)
8655 /* new range covers old */
8658 ack
= ack
&& acknowledged
;
8660 if (e
< s
+ sectors
)
8662 if (e
- a
<= BB_MAX_LEN
) {
8663 p
[lo
] = BB_MAKE(a
, e
-a
, ack
);
8666 /* does not all fit in one range,
8667 * make p[lo] maximal
8669 if (BB_LEN(p
[lo
]) != BB_MAX_LEN
)
8670 p
[lo
] = BB_MAKE(a
, BB_MAX_LEN
, ack
);
8676 if (sectors
&& hi
< bb
->count
) {
8677 /* 'hi' points to the first range that starts after 's'.
8678 * Maybe we can merge with the start of that range */
8679 sector_t a
= BB_OFFSET(p
[hi
]);
8680 sector_t e
= a
+ BB_LEN(p
[hi
]);
8681 int ack
= BB_ACK(p
[hi
]);
8682 if (a
<= s
+ sectors
) {
8683 /* merging is possible */
8684 if (e
<= s
+ sectors
) {
8689 ack
= ack
&& acknowledged
;
8692 if (e
- a
<= BB_MAX_LEN
) {
8693 p
[hi
] = BB_MAKE(a
, e
-a
, ack
);
8696 p
[hi
] = BB_MAKE(a
, BB_MAX_LEN
, ack
);
8704 if (sectors
== 0 && hi
< bb
->count
) {
8705 /* we might be able to combine lo and hi */
8706 /* Note: 's' is at the end of 'lo' */
8707 sector_t a
= BB_OFFSET(p
[hi
]);
8708 int lolen
= BB_LEN(p
[lo
]);
8709 int hilen
= BB_LEN(p
[hi
]);
8710 int newlen
= lolen
+ hilen
- (s
- a
);
8711 if (s
>= a
&& newlen
< BB_MAX_LEN
) {
8712 /* yes, we can combine them */
8713 int ack
= BB_ACK(p
[lo
]) && BB_ACK(p
[hi
]);
8714 p
[lo
] = BB_MAKE(BB_OFFSET(p
[lo
]), newlen
, ack
);
8715 memmove(p
+ hi
, p
+ hi
+ 1,
8716 (bb
->count
- hi
- 1) * 8);
8721 /* didn't merge (it all).
8722 * Need to add a range just before 'hi' */
8723 if (bb
->count
>= MD_MAX_BADBLOCKS
) {
8724 /* No room for more */
8728 int this_sectors
= sectors
;
8729 memmove(p
+ hi
+ 1, p
+ hi
,
8730 (bb
->count
- hi
) * 8);
8733 if (this_sectors
> BB_MAX_LEN
)
8734 this_sectors
= BB_MAX_LEN
;
8735 p
[hi
] = BB_MAKE(s
, this_sectors
, acknowledged
);
8736 sectors
-= this_sectors
;
8743 bb
->unacked_exist
= 1;
8744 write_sequnlock_irqrestore(&bb
->lock
, flags
);
8749 int rdev_set_badblocks(struct md_rdev
*rdev
, sector_t s
, int sectors
,
8754 s
+= rdev
->new_data_offset
;
8756 s
+= rdev
->data_offset
;
8757 rv
= md_set_badblocks(&rdev
->badblocks
,
8760 /* Make sure they get written out promptly */
8761 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
8762 set_bit(MD_CHANGE_CLEAN
, &rdev
->mddev
->flags
);
8763 set_bit(MD_CHANGE_PENDING
, &rdev
->mddev
->flags
);
8764 md_wakeup_thread(rdev
->mddev
->thread
);
8768 EXPORT_SYMBOL_GPL(rdev_set_badblocks
);
8771 * Remove a range of bad blocks from the table.
8772 * This may involve extending the table if we spilt a region,
8773 * but it must not fail. So if the table becomes full, we just
8774 * drop the remove request.
8776 static int md_clear_badblocks(struct badblocks
*bb
, sector_t s
, int sectors
)
8780 sector_t target
= s
+ sectors
;
8783 if (bb
->shift
> 0) {
8784 /* When clearing we round the start up and the end down.
8785 * This should not matter as the shift should align with
8786 * the block size and no rounding should ever be needed.
8787 * However it is better the think a block is bad when it
8788 * isn't than to think a block is not bad when it is.
8790 s
+= (1<<bb
->shift
) - 1;
8792 target
>>= bb
->shift
;
8793 sectors
= target
- s
;
8796 write_seqlock_irq(&bb
->lock
);
8801 /* Find the last range that starts before 'target' */
8802 while (hi
- lo
> 1) {
8803 int mid
= (lo
+ hi
) / 2;
8804 sector_t a
= BB_OFFSET(p
[mid
]);
8811 /* p[lo] is the last range that could overlap the
8812 * current range. Earlier ranges could also overlap,
8813 * but only this one can overlap the end of the range.
8815 if (BB_OFFSET(p
[lo
]) + BB_LEN(p
[lo
]) > target
) {
8816 /* Partial overlap, leave the tail of this range */
8817 int ack
= BB_ACK(p
[lo
]);
8818 sector_t a
= BB_OFFSET(p
[lo
]);
8819 sector_t end
= a
+ BB_LEN(p
[lo
]);
8822 /* we need to split this range */
8823 if (bb
->count
>= MD_MAX_BADBLOCKS
) {
8827 memmove(p
+lo
+1, p
+lo
, (bb
->count
- lo
) * 8);
8829 p
[lo
] = BB_MAKE(a
, s
-a
, ack
);
8832 p
[lo
] = BB_MAKE(target
, end
- target
, ack
);
8833 /* there is no longer an overlap */
8838 BB_OFFSET(p
[lo
]) + BB_LEN(p
[lo
]) > s
) {
8839 /* This range does overlap */
8840 if (BB_OFFSET(p
[lo
]) < s
) {
8841 /* Keep the early parts of this range. */
8842 int ack
= BB_ACK(p
[lo
]);
8843 sector_t start
= BB_OFFSET(p
[lo
]);
8844 p
[lo
] = BB_MAKE(start
, s
- start
, ack
);
8845 /* now low doesn't overlap, so.. */
8850 /* 'lo' is strictly before, 'hi' is strictly after,
8851 * anything between needs to be discarded
8854 memmove(p
+lo
+1, p
+hi
, (bb
->count
- hi
) * 8);
8855 bb
->count
-= (hi
- lo
- 1);
8861 write_sequnlock_irq(&bb
->lock
);
8865 int rdev_clear_badblocks(struct md_rdev
*rdev
, sector_t s
, int sectors
,
8869 s
+= rdev
->new_data_offset
;
8871 s
+= rdev
->data_offset
;
8872 return md_clear_badblocks(&rdev
->badblocks
,
8875 EXPORT_SYMBOL_GPL(rdev_clear_badblocks
);
8878 * Acknowledge all bad blocks in a list.
8879 * This only succeeds if ->changed is clear. It is used by
8880 * in-kernel metadata updates
8882 void md_ack_all_badblocks(struct badblocks
*bb
)
8884 if (bb
->page
== NULL
|| bb
->changed
)
8885 /* no point even trying */
8887 write_seqlock_irq(&bb
->lock
);
8889 if (bb
->changed
== 0 && bb
->unacked_exist
) {
8892 for (i
= 0; i
< bb
->count
; i
++) {
8893 if (!BB_ACK(p
[i
])) {
8894 sector_t start
= BB_OFFSET(p
[i
]);
8895 int len
= BB_LEN(p
[i
]);
8896 p
[i
] = BB_MAKE(start
, len
, 1);
8899 bb
->unacked_exist
= 0;
8901 write_sequnlock_irq(&bb
->lock
);
8903 EXPORT_SYMBOL_GPL(md_ack_all_badblocks
);
8905 /* sysfs access to bad-blocks list.
8906 * We present two files.
8907 * 'bad-blocks' lists sector numbers and lengths of ranges that
8908 * are recorded as bad. The list is truncated to fit within
8909 * the one-page limit of sysfs.
8910 * Writing "sector length" to this file adds an acknowledged
8912 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
8913 * been acknowledged. Writing to this file adds bad blocks
8914 * without acknowledging them. This is largely for testing.
8918 badblocks_show(struct badblocks
*bb
, char *page
, int unack
)
8929 seq
= read_seqbegin(&bb
->lock
);
8934 while (len
< PAGE_SIZE
&& i
< bb
->count
) {
8935 sector_t s
= BB_OFFSET(p
[i
]);
8936 unsigned int length
= BB_LEN(p
[i
]);
8937 int ack
= BB_ACK(p
[i
]);
8943 len
+= snprintf(page
+len
, PAGE_SIZE
-len
, "%llu %u\n",
8944 (unsigned long long)s
<< bb
->shift
,
8945 length
<< bb
->shift
);
8947 if (unack
&& len
== 0)
8948 bb
->unacked_exist
= 0;
8950 if (read_seqretry(&bb
->lock
, seq
))
8959 badblocks_store(struct badblocks
*bb
, const char *page
, size_t len
, int unack
)
8961 unsigned long long sector
;
8965 /* Allow clearing via sysfs *only* for testing/debugging.
8966 * Normally only a successful write may clear a badblock
8969 if (page
[0] == '-') {
8973 #endif /* DO_DEBUG */
8975 switch (sscanf(page
, "%llu %d%c", §or
, &length
, &newline
)) {
8977 if (newline
!= '\n')
8989 md_clear_badblocks(bb
, sector
, length
);
8992 #endif /* DO_DEBUG */
8993 if (md_set_badblocks(bb
, sector
, length
, !unack
))
8999 static int md_notify_reboot(struct notifier_block
*this,
9000 unsigned long code
, void *x
)
9002 struct list_head
*tmp
;
9003 struct mddev
*mddev
;
9006 for_each_mddev(mddev
, tmp
) {
9007 if (mddev_trylock(mddev
)) {
9009 __md_stop_writes(mddev
);
9010 if (mddev
->persistent
)
9011 mddev
->safemode
= 2;
9012 mddev_unlock(mddev
);
9017 * certain more exotic SCSI devices are known to be
9018 * volatile wrt too early system reboots. While the
9019 * right place to handle this issue is the given
9020 * driver, we do want to have a safe RAID driver ...
9028 static struct notifier_block md_notifier
= {
9029 .notifier_call
= md_notify_reboot
,
9031 .priority
= INT_MAX
, /* before any real devices */
9034 static void md_geninit(void)
9036 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t
));
9038 proc_create("mdstat", S_IRUGO
, NULL
, &md_seq_fops
);
9041 static int __init
md_init(void)
9045 md_wq
= alloc_workqueue("md", WQ_MEM_RECLAIM
, 0);
9049 md_misc_wq
= alloc_workqueue("md_misc", 0, 0);
9053 if ((ret
= register_blkdev(MD_MAJOR
, "md")) < 0)
9056 if ((ret
= register_blkdev(0, "mdp")) < 0)
9060 blk_register_region(MKDEV(MD_MAJOR
, 0), 512, THIS_MODULE
,
9061 md_probe
, NULL
, NULL
);
9062 blk_register_region(MKDEV(mdp_major
, 0), 1UL<<MINORBITS
, THIS_MODULE
,
9063 md_probe
, NULL
, NULL
);
9065 register_reboot_notifier(&md_notifier
);
9066 raid_table_header
= register_sysctl_table(raid_root_table
);
9072 unregister_blkdev(MD_MAJOR
, "md");
9074 destroy_workqueue(md_misc_wq
);
9076 destroy_workqueue(md_wq
);
9081 static void check_sb_changes(struct mddev
*mddev
, struct md_rdev
*rdev
)
9083 struct mdp_superblock_1
*sb
= page_address(rdev
->sb_page
);
9084 struct md_rdev
*rdev2
;
9086 char b
[BDEVNAME_SIZE
];
9088 /* Check for change of roles in the active devices */
9089 rdev_for_each(rdev2
, mddev
) {
9090 if (test_bit(Faulty
, &rdev2
->flags
))
9093 /* Check if the roles changed */
9094 role
= le16_to_cpu(sb
->dev_roles
[rdev2
->desc_nr
]);
9096 if (test_bit(Candidate
, &rdev2
->flags
)) {
9097 if (role
== 0xfffe) {
9098 pr_info("md: Removing Candidate device %s because add failed\n", bdevname(rdev2
->bdev
,b
));
9099 md_kick_rdev_from_array(rdev2
);
9103 clear_bit(Candidate
, &rdev2
->flags
);
9106 if (role
!= rdev2
->raid_disk
) {
9108 if (rdev2
->raid_disk
== -1 && role
!= 0xffff) {
9109 rdev2
->saved_raid_disk
= role
;
9110 ret
= remove_and_add_spares(mddev
, rdev2
);
9111 pr_info("Activated spare: %s\n",
9112 bdevname(rdev2
->bdev
,b
));
9116 * We just want to do the minimum to mark the disk
9117 * as faulty. The recovery is performed by the
9118 * one who initiated the error.
9120 if ((role
== 0xfffe) || (role
== 0xfffd)) {
9121 md_error(mddev
, rdev2
);
9122 clear_bit(Blocked
, &rdev2
->flags
);
9127 if (mddev
->raid_disks
!= le32_to_cpu(sb
->raid_disks
))
9128 update_raid_disks(mddev
, le32_to_cpu(sb
->raid_disks
));
9130 /* Finally set the event to be up to date */
9131 mddev
->events
= le64_to_cpu(sb
->events
);
9134 static int read_rdev(struct mddev
*mddev
, struct md_rdev
*rdev
)
9137 struct page
*swapout
= rdev
->sb_page
;
9138 struct mdp_superblock_1
*sb
;
9140 /* Store the sb page of the rdev in the swapout temporary
9141 * variable in case we err in the future
9143 rdev
->sb_page
= NULL
;
9144 alloc_disk_sb(rdev
);
9145 ClearPageUptodate(rdev
->sb_page
);
9146 rdev
->sb_loaded
= 0;
9147 err
= super_types
[mddev
->major_version
].load_super(rdev
, NULL
, mddev
->minor_version
);
9150 pr_warn("%s: %d Could not reload rdev(%d) err: %d. Restoring old values\n",
9151 __func__
, __LINE__
, rdev
->desc_nr
, err
);
9152 put_page(rdev
->sb_page
);
9153 rdev
->sb_page
= swapout
;
9154 rdev
->sb_loaded
= 1;
9158 sb
= page_address(rdev
->sb_page
);
9159 /* Read the offset unconditionally, even if MD_FEATURE_RECOVERY_OFFSET
9163 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RECOVERY_OFFSET
))
9164 rdev
->recovery_offset
= le64_to_cpu(sb
->recovery_offset
);
9166 /* The other node finished recovery, call spare_active to set
9167 * device In_sync and mddev->degraded
9169 if (rdev
->recovery_offset
== MaxSector
&&
9170 !test_bit(In_sync
, &rdev
->flags
) &&
9171 mddev
->pers
->spare_active(mddev
))
9172 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
9178 void md_reload_sb(struct mddev
*mddev
, int nr
)
9180 struct md_rdev
*rdev
;
9184 rdev_for_each_rcu(rdev
, mddev
) {
9185 if (rdev
->desc_nr
== nr
)
9189 if (!rdev
|| rdev
->desc_nr
!= nr
) {
9190 pr_warn("%s: %d Could not find rdev with nr %d\n", __func__
, __LINE__
, nr
);
9194 err
= read_rdev(mddev
, rdev
);
9198 check_sb_changes(mddev
, rdev
);
9200 /* Read all rdev's to update recovery_offset */
9201 rdev_for_each_rcu(rdev
, mddev
)
9202 read_rdev(mddev
, rdev
);
9204 EXPORT_SYMBOL(md_reload_sb
);
9209 * Searches all registered partitions for autorun RAID arrays
9213 static LIST_HEAD(all_detected_devices
);
9214 struct detected_devices_node
{
9215 struct list_head list
;
9219 void md_autodetect_dev(dev_t dev
)
9221 struct detected_devices_node
*node_detected_dev
;
9223 node_detected_dev
= kzalloc(sizeof(*node_detected_dev
), GFP_KERNEL
);
9224 if (node_detected_dev
) {
9225 node_detected_dev
->dev
= dev
;
9226 list_add_tail(&node_detected_dev
->list
, &all_detected_devices
);
9228 printk(KERN_CRIT
"md: md_autodetect_dev: kzalloc failed"
9229 ", skipping dev(%d,%d)\n", MAJOR(dev
), MINOR(dev
));
9233 static void autostart_arrays(int part
)
9235 struct md_rdev
*rdev
;
9236 struct detected_devices_node
*node_detected_dev
;
9238 int i_scanned
, i_passed
;
9243 printk(KERN_INFO
"md: Autodetecting RAID arrays.\n");
9245 while (!list_empty(&all_detected_devices
) && i_scanned
< INT_MAX
) {
9247 node_detected_dev
= list_entry(all_detected_devices
.next
,
9248 struct detected_devices_node
, list
);
9249 list_del(&node_detected_dev
->list
);
9250 dev
= node_detected_dev
->dev
;
9251 kfree(node_detected_dev
);
9252 rdev
= md_import_device(dev
,0, 90);
9256 if (test_bit(Faulty
, &rdev
->flags
))
9259 set_bit(AutoDetected
, &rdev
->flags
);
9260 list_add(&rdev
->same_set
, &pending_raid_disks
);
9264 printk(KERN_INFO
"md: Scanned %d and added %d devices.\n",
9265 i_scanned
, i_passed
);
9267 autorun_devices(part
);
9270 #endif /* !MODULE */
9272 static __exit
void md_exit(void)
9274 struct mddev
*mddev
;
9275 struct list_head
*tmp
;
9278 blk_unregister_region(MKDEV(MD_MAJOR
,0), 512);
9279 blk_unregister_region(MKDEV(mdp_major
,0), 1U << MINORBITS
);
9281 unregister_blkdev(MD_MAJOR
,"md");
9282 unregister_blkdev(mdp_major
, "mdp");
9283 unregister_reboot_notifier(&md_notifier
);
9284 unregister_sysctl_table(raid_table_header
);
9286 /* We cannot unload the modules while some process is
9287 * waiting for us in select() or poll() - wake them up
9290 while (waitqueue_active(&md_event_waiters
)) {
9291 /* not safe to leave yet */
9292 wake_up(&md_event_waiters
);
9296 remove_proc_entry("mdstat", NULL
);
9298 for_each_mddev(mddev
, tmp
) {
9299 export_array(mddev
);
9300 mddev
->hold_active
= 0;
9302 destroy_workqueue(md_misc_wq
);
9303 destroy_workqueue(md_wq
);
9306 subsys_initcall(md_init
);
9307 module_exit(md_exit
)
9309 static int get_ro(char *buffer
, struct kernel_param
*kp
)
9311 return sprintf(buffer
, "%d", start_readonly
);
9313 static int set_ro(const char *val
, struct kernel_param
*kp
)
9315 return kstrtouint(val
, 10, (unsigned int *)&start_readonly
);
9318 module_param_call(start_ro
, set_ro
, get_ro
, NULL
, S_IRUSR
|S_IWUSR
);
9319 module_param(start_dirty_degraded
, int, S_IRUGO
|S_IWUSR
);
9320 module_param_call(new_array
, add_named_array
, NULL
, NULL
, S_IWUSR
);
9322 MODULE_LICENSE("GPL");
9323 MODULE_DESCRIPTION("MD RAID framework");
9325 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR
);