2 md.c : Multiple Devices driver for Linux
3 Copyright (C) 1998, 1999, 2000 Ingo Molnar
5 completely rewritten, based on the MD driver code from Marc Zyngier
9 - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar
10 - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com>
11 - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net>
12 - kerneld support by Boris Tobotras <boris@xtalk.msk.su>
13 - kmod support by: Cyrus Durgin
14 - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com>
15 - Devfs support by Richard Gooch <rgooch@atnf.csiro.au>
17 - lots of fixes and improvements to the RAID1/RAID5 and generic
18 RAID code (such as request based resynchronization):
20 Neil Brown <neilb@cse.unsw.edu.au>.
22 - persistent bitmap code
23 Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.
25 This program is free software; you can redistribute it and/or modify
26 it under the terms of the GNU General Public License as published by
27 the Free Software Foundation; either version 2, or (at your option)
30 You should have received a copy of the GNU General Public License
31 (for example /usr/src/linux/COPYING); if not, write to the Free
32 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35 #include <linux/kthread.h>
36 #include <linux/blkdev.h>
37 #include <linux/sysctl.h>
38 #include <linux/seq_file.h>
39 #include <linux/mutex.h>
40 #include <linux/buffer_head.h> /* for invalidate_bdev */
41 #include <linux/poll.h>
42 #include <linux/ctype.h>
43 #include <linux/string.h>
44 #include <linux/hdreg.h>
45 #include <linux/proc_fs.h>
46 #include <linux/random.h>
47 #include <linux/reboot.h>
48 #include <linux/file.h>
49 #include <linux/compat.h>
50 #include <linux/delay.h>
51 #include <linux/raid/md_p.h>
52 #include <linux/raid/md_u.h>
53 #include <linux/slab.h>
58 #define dprintk(x...) ((void)(DEBUG && printk(x)))
60 static DEFINE_MUTEX(md_mutex
);
63 static void autostart_arrays(int part
);
66 static LIST_HEAD(pers_list
);
67 static DEFINE_SPINLOCK(pers_lock
);
69 static void md_print_devices(void);
71 static DECLARE_WAIT_QUEUE_HEAD(resync_wait
);
73 #define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
76 * Default number of read corrections we'll attempt on an rdev
77 * before ejecting it from the array. We divide the read error
78 * count by 2 for every hour elapsed between read errors.
80 #define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
82 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
83 * is 1000 KB/sec, so the extra system load does not show up that much.
84 * Increase it if you want to have more _guaranteed_ speed. Note that
85 * the RAID driver will use the maximum available bandwidth if the IO
86 * subsystem is idle. There is also an 'absolute maximum' reconstruction
87 * speed limit - in case reconstruction slows down your system despite
90 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
91 * or /sys/block/mdX/md/sync_speed_{min,max}
94 static int sysctl_speed_limit_min
= 1000;
95 static int sysctl_speed_limit_max
= 200000;
96 static inline int speed_min(mddev_t
*mddev
)
98 return mddev
->sync_speed_min
?
99 mddev
->sync_speed_min
: sysctl_speed_limit_min
;
102 static inline int speed_max(mddev_t
*mddev
)
104 return mddev
->sync_speed_max
?
105 mddev
->sync_speed_max
: sysctl_speed_limit_max
;
108 static struct ctl_table_header
*raid_table_header
;
110 static ctl_table raid_table
[] = {
112 .procname
= "speed_limit_min",
113 .data
= &sysctl_speed_limit_min
,
114 .maxlen
= sizeof(int),
115 .mode
= S_IRUGO
|S_IWUSR
,
116 .proc_handler
= proc_dointvec
,
119 .procname
= "speed_limit_max",
120 .data
= &sysctl_speed_limit_max
,
121 .maxlen
= sizeof(int),
122 .mode
= S_IRUGO
|S_IWUSR
,
123 .proc_handler
= proc_dointvec
,
128 static ctl_table raid_dir_table
[] = {
132 .mode
= S_IRUGO
|S_IXUGO
,
138 static ctl_table raid_root_table
[] = {
143 .child
= raid_dir_table
,
148 static const struct block_device_operations md_fops
;
150 static int start_readonly
;
153 * We have a system wide 'event count' that is incremented
154 * on any 'interesting' event, and readers of /proc/mdstat
155 * can use 'poll' or 'select' to find out when the event
159 * start array, stop array, error, add device, remove device,
160 * start build, activate spare
162 static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters
);
163 static atomic_t md_event_count
;
164 void md_new_event(mddev_t
*mddev
)
166 atomic_inc(&md_event_count
);
167 wake_up(&md_event_waiters
);
169 EXPORT_SYMBOL_GPL(md_new_event
);
171 /* Alternate version that can be called from interrupts
172 * when calling sysfs_notify isn't needed.
174 static void md_new_event_inintr(mddev_t
*mddev
)
176 atomic_inc(&md_event_count
);
177 wake_up(&md_event_waiters
);
181 * Enables to iterate over all existing md arrays
182 * all_mddevs_lock protects this list.
184 static LIST_HEAD(all_mddevs
);
185 static DEFINE_SPINLOCK(all_mddevs_lock
);
189 * iterates through all used mddevs in the system.
190 * We take care to grab the all_mddevs_lock whenever navigating
191 * the list, and to always hold a refcount when unlocked.
192 * Any code which breaks out of this loop while own
193 * a reference to the current mddev and must mddev_put it.
195 #define for_each_mddev(mddev,tmp) \
197 for (({ spin_lock(&all_mddevs_lock); \
198 tmp = all_mddevs.next; \
200 ({ if (tmp != &all_mddevs) \
201 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
202 spin_unlock(&all_mddevs_lock); \
203 if (mddev) mddev_put(mddev); \
204 mddev = list_entry(tmp, mddev_t, all_mddevs); \
205 tmp != &all_mddevs;}); \
206 ({ spin_lock(&all_mddevs_lock); \
211 /* Rather than calling directly into the personality make_request function,
212 * IO requests come here first so that we can check if the device is
213 * being suspended pending a reconfiguration.
214 * We hold a refcount over the call to ->make_request. By the time that
215 * call has finished, the bio has been linked into some internal structure
216 * and so is visible to ->quiesce(), so we don't need the refcount any more.
218 static int md_make_request(struct request_queue
*q
, struct bio
*bio
)
220 const int rw
= bio_data_dir(bio
);
221 mddev_t
*mddev
= q
->queuedata
;
225 if (mddev
== NULL
|| mddev
->pers
== NULL
) {
230 if (mddev
->suspended
) {
233 prepare_to_wait(&mddev
->sb_wait
, &__wait
,
234 TASK_UNINTERRUPTIBLE
);
235 if (!mddev
->suspended
)
241 finish_wait(&mddev
->sb_wait
, &__wait
);
243 atomic_inc(&mddev
->active_io
);
246 rv
= mddev
->pers
->make_request(mddev
, bio
);
248 cpu
= part_stat_lock();
249 part_stat_inc(cpu
, &mddev
->gendisk
->part0
, ios
[rw
]);
250 part_stat_add(cpu
, &mddev
->gendisk
->part0
, sectors
[rw
],
254 if (atomic_dec_and_test(&mddev
->active_io
) && mddev
->suspended
)
255 wake_up(&mddev
->sb_wait
);
260 /* mddev_suspend makes sure no new requests are submitted
261 * to the device, and that any requests that have been submitted
262 * are completely handled.
263 * Once ->stop is called and completes, the module will be completely
266 void mddev_suspend(mddev_t
*mddev
)
268 BUG_ON(mddev
->suspended
);
269 mddev
->suspended
= 1;
271 wait_event(mddev
->sb_wait
, atomic_read(&mddev
->active_io
) == 0);
272 mddev
->pers
->quiesce(mddev
, 1);
274 EXPORT_SYMBOL_GPL(mddev_suspend
);
276 void mddev_resume(mddev_t
*mddev
)
278 mddev
->suspended
= 0;
279 wake_up(&mddev
->sb_wait
);
280 mddev
->pers
->quiesce(mddev
, 0);
282 EXPORT_SYMBOL_GPL(mddev_resume
);
284 int mddev_congested(mddev_t
*mddev
, int bits
)
286 return mddev
->suspended
;
288 EXPORT_SYMBOL(mddev_congested
);
291 * Generic flush handling for md
294 static void md_end_flush(struct bio
*bio
, int err
)
296 mdk_rdev_t
*rdev
= bio
->bi_private
;
297 mddev_t
*mddev
= rdev
->mddev
;
299 rdev_dec_pending(rdev
, mddev
);
301 if (atomic_dec_and_test(&mddev
->flush_pending
)) {
302 /* The pre-request flush has finished */
303 schedule_work(&mddev
->flush_work
);
308 static void submit_flushes(mddev_t
*mddev
)
313 list_for_each_entry_rcu(rdev
, &mddev
->disks
, same_set
)
314 if (rdev
->raid_disk
>= 0 &&
315 !test_bit(Faulty
, &rdev
->flags
)) {
316 /* Take two references, one is dropped
317 * when request finishes, one after
318 * we reclaim rcu_read_lock
321 atomic_inc(&rdev
->nr_pending
);
322 atomic_inc(&rdev
->nr_pending
);
324 bi
= bio_alloc(GFP_KERNEL
, 0);
325 bi
->bi_end_io
= md_end_flush
;
326 bi
->bi_private
= rdev
;
327 bi
->bi_bdev
= rdev
->bdev
;
328 atomic_inc(&mddev
->flush_pending
);
329 submit_bio(WRITE_FLUSH
, bi
);
331 rdev_dec_pending(rdev
, mddev
);
336 static void md_submit_flush_data(struct work_struct
*ws
)
338 mddev_t
*mddev
= container_of(ws
, mddev_t
, flush_work
);
339 struct bio
*bio
= mddev
->flush_bio
;
341 atomic_set(&mddev
->flush_pending
, 1);
343 if (bio
->bi_size
== 0)
344 /* an empty barrier - all done */
347 bio
->bi_rw
&= ~REQ_FLUSH
;
348 if (mddev
->pers
->make_request(mddev
, bio
))
349 generic_make_request(bio
);
351 if (atomic_dec_and_test(&mddev
->flush_pending
)) {
352 mddev
->flush_bio
= NULL
;
353 wake_up(&mddev
->sb_wait
);
357 void md_flush_request(mddev_t
*mddev
, struct bio
*bio
)
359 spin_lock_irq(&mddev
->write_lock
);
360 wait_event_lock_irq(mddev
->sb_wait
,
362 mddev
->write_lock
, /*nothing*/);
363 mddev
->flush_bio
= bio
;
364 spin_unlock_irq(&mddev
->write_lock
);
366 atomic_set(&mddev
->flush_pending
, 1);
367 INIT_WORK(&mddev
->flush_work
, md_submit_flush_data
);
369 submit_flushes(mddev
);
371 if (atomic_dec_and_test(&mddev
->flush_pending
))
372 schedule_work(&mddev
->flush_work
);
374 EXPORT_SYMBOL(md_flush_request
);
376 /* Support for plugging.
377 * This mirrors the plugging support in request_queue, but does not
378 * require having a whole queue
380 static void plugger_work(struct work_struct
*work
)
382 struct plug_handle
*plug
=
383 container_of(work
, struct plug_handle
, unplug_work
);
384 plug
->unplug_fn(plug
);
386 static void plugger_timeout(unsigned long data
)
388 struct plug_handle
*plug
= (void *)data
;
389 kblockd_schedule_work(NULL
, &plug
->unplug_work
);
391 void plugger_init(struct plug_handle
*plug
,
392 void (*unplug_fn
)(struct plug_handle
*))
394 plug
->unplug_flag
= 0;
395 plug
->unplug_fn
= unplug_fn
;
396 init_timer(&plug
->unplug_timer
);
397 plug
->unplug_timer
.function
= plugger_timeout
;
398 plug
->unplug_timer
.data
= (unsigned long)plug
;
399 INIT_WORK(&plug
->unplug_work
, plugger_work
);
401 EXPORT_SYMBOL_GPL(plugger_init
);
403 void plugger_set_plug(struct plug_handle
*plug
)
405 if (!test_and_set_bit(PLUGGED_FLAG
, &plug
->unplug_flag
))
406 mod_timer(&plug
->unplug_timer
, jiffies
+ msecs_to_jiffies(3)+1);
408 EXPORT_SYMBOL_GPL(plugger_set_plug
);
410 int plugger_remove_plug(struct plug_handle
*plug
)
412 if (test_and_clear_bit(PLUGGED_FLAG
, &plug
->unplug_flag
)) {
413 del_timer(&plug
->unplug_timer
);
418 EXPORT_SYMBOL_GPL(plugger_remove_plug
);
421 static inline mddev_t
*mddev_get(mddev_t
*mddev
)
423 atomic_inc(&mddev
->active
);
427 static void mddev_delayed_delete(struct work_struct
*ws
);
429 static void mddev_put(mddev_t
*mddev
)
431 if (!atomic_dec_and_lock(&mddev
->active
, &all_mddevs_lock
))
433 if (!mddev
->raid_disks
&& list_empty(&mddev
->disks
) &&
434 mddev
->ctime
== 0 && !mddev
->hold_active
) {
435 /* Array is not configured at all, and not held active,
437 list_del(&mddev
->all_mddevs
);
438 if (mddev
->gendisk
) {
439 /* we did a probe so need to clean up.
440 * Call schedule_work inside the spinlock
441 * so that flush_scheduled_work() after
442 * mddev_find will succeed in waiting for the
445 INIT_WORK(&mddev
->del_work
, mddev_delayed_delete
);
446 schedule_work(&mddev
->del_work
);
450 spin_unlock(&all_mddevs_lock
);
453 void mddev_init(mddev_t
*mddev
)
455 mutex_init(&mddev
->open_mutex
);
456 mutex_init(&mddev
->reconfig_mutex
);
457 mutex_init(&mddev
->bitmap_info
.mutex
);
458 INIT_LIST_HEAD(&mddev
->disks
);
459 INIT_LIST_HEAD(&mddev
->all_mddevs
);
460 init_timer(&mddev
->safemode_timer
);
461 atomic_set(&mddev
->active
, 1);
462 atomic_set(&mddev
->openers
, 0);
463 atomic_set(&mddev
->active_io
, 0);
464 spin_lock_init(&mddev
->write_lock
);
465 atomic_set(&mddev
->flush_pending
, 0);
466 init_waitqueue_head(&mddev
->sb_wait
);
467 init_waitqueue_head(&mddev
->recovery_wait
);
468 mddev
->reshape_position
= MaxSector
;
469 mddev
->resync_min
= 0;
470 mddev
->resync_max
= MaxSector
;
471 mddev
->level
= LEVEL_NONE
;
473 EXPORT_SYMBOL_GPL(mddev_init
);
475 static mddev_t
* mddev_find(dev_t unit
)
477 mddev_t
*mddev
, *new = NULL
;
480 spin_lock(&all_mddevs_lock
);
483 list_for_each_entry(mddev
, &all_mddevs
, all_mddevs
)
484 if (mddev
->unit
== unit
) {
486 spin_unlock(&all_mddevs_lock
);
492 list_add(&new->all_mddevs
, &all_mddevs
);
493 spin_unlock(&all_mddevs_lock
);
494 new->hold_active
= UNTIL_IOCTL
;
498 /* find an unused unit number */
499 static int next_minor
= 512;
500 int start
= next_minor
;
504 dev
= MKDEV(MD_MAJOR
, next_minor
);
506 if (next_minor
> MINORMASK
)
508 if (next_minor
== start
) {
509 /* Oh dear, all in use. */
510 spin_unlock(&all_mddevs_lock
);
516 list_for_each_entry(mddev
, &all_mddevs
, all_mddevs
)
517 if (mddev
->unit
== dev
) {
523 new->md_minor
= MINOR(dev
);
524 new->hold_active
= UNTIL_STOP
;
525 list_add(&new->all_mddevs
, &all_mddevs
);
526 spin_unlock(&all_mddevs_lock
);
529 spin_unlock(&all_mddevs_lock
);
531 new = kzalloc(sizeof(*new), GFP_KERNEL
);
536 if (MAJOR(unit
) == MD_MAJOR
)
537 new->md_minor
= MINOR(unit
);
539 new->md_minor
= MINOR(unit
) >> MdpMinorShift
;
546 static inline int mddev_lock(mddev_t
* mddev
)
548 return mutex_lock_interruptible(&mddev
->reconfig_mutex
);
551 static inline int mddev_is_locked(mddev_t
*mddev
)
553 return mutex_is_locked(&mddev
->reconfig_mutex
);
556 static inline int mddev_trylock(mddev_t
* mddev
)
558 return mutex_trylock(&mddev
->reconfig_mutex
);
561 static struct attribute_group md_redundancy_group
;
563 static void mddev_unlock(mddev_t
* mddev
)
565 if (mddev
->to_remove
) {
566 /* These cannot be removed under reconfig_mutex as
567 * an access to the files will try to take reconfig_mutex
568 * while holding the file unremovable, which leads to
570 * So hold set sysfs_active while the remove in happeing,
571 * and anything else which might set ->to_remove or my
572 * otherwise change the sysfs namespace will fail with
573 * -EBUSY if sysfs_active is still set.
574 * We set sysfs_active under reconfig_mutex and elsewhere
575 * test it under the same mutex to ensure its correct value
578 struct attribute_group
*to_remove
= mddev
->to_remove
;
579 mddev
->to_remove
= NULL
;
580 mddev
->sysfs_active
= 1;
581 mutex_unlock(&mddev
->reconfig_mutex
);
583 if (mddev
->kobj
.sd
) {
584 if (to_remove
!= &md_redundancy_group
)
585 sysfs_remove_group(&mddev
->kobj
, to_remove
);
586 if (mddev
->pers
== NULL
||
587 mddev
->pers
->sync_request
== NULL
) {
588 sysfs_remove_group(&mddev
->kobj
, &md_redundancy_group
);
589 if (mddev
->sysfs_action
)
590 sysfs_put(mddev
->sysfs_action
);
591 mddev
->sysfs_action
= NULL
;
594 mddev
->sysfs_active
= 0;
596 mutex_unlock(&mddev
->reconfig_mutex
);
598 md_wakeup_thread(mddev
->thread
);
601 static mdk_rdev_t
* find_rdev_nr(mddev_t
*mddev
, int nr
)
605 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
606 if (rdev
->desc_nr
== nr
)
612 static mdk_rdev_t
* find_rdev(mddev_t
* mddev
, dev_t dev
)
616 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
617 if (rdev
->bdev
->bd_dev
== dev
)
623 static struct mdk_personality
*find_pers(int level
, char *clevel
)
625 struct mdk_personality
*pers
;
626 list_for_each_entry(pers
, &pers_list
, list
) {
627 if (level
!= LEVEL_NONE
&& pers
->level
== level
)
629 if (strcmp(pers
->name
, clevel
)==0)
635 /* return the offset of the super block in 512byte sectors */
636 static inline sector_t
calc_dev_sboffset(struct block_device
*bdev
)
638 sector_t num_sectors
= bdev
->bd_inode
->i_size
/ 512;
639 return MD_NEW_SIZE_SECTORS(num_sectors
);
642 static int alloc_disk_sb(mdk_rdev_t
* rdev
)
647 rdev
->sb_page
= alloc_page(GFP_KERNEL
);
648 if (!rdev
->sb_page
) {
649 printk(KERN_ALERT
"md: out of memory.\n");
656 static void free_disk_sb(mdk_rdev_t
* rdev
)
659 put_page(rdev
->sb_page
);
661 rdev
->sb_page
= NULL
;
668 static void super_written(struct bio
*bio
, int error
)
670 mdk_rdev_t
*rdev
= bio
->bi_private
;
671 mddev_t
*mddev
= rdev
->mddev
;
673 if (error
|| !test_bit(BIO_UPTODATE
, &bio
->bi_flags
)) {
674 printk("md: super_written gets error=%d, uptodate=%d\n",
675 error
, test_bit(BIO_UPTODATE
, &bio
->bi_flags
));
676 WARN_ON(test_bit(BIO_UPTODATE
, &bio
->bi_flags
));
677 md_error(mddev
, rdev
);
680 if (atomic_dec_and_test(&mddev
->pending_writes
))
681 wake_up(&mddev
->sb_wait
);
685 void md_super_write(mddev_t
*mddev
, mdk_rdev_t
*rdev
,
686 sector_t sector
, int size
, struct page
*page
)
688 /* write first size bytes of page to sector of rdev
689 * Increment mddev->pending_writes before returning
690 * and decrement it on completion, waking up sb_wait
691 * if zero is reached.
692 * If an error occurred, call md_error
694 struct bio
*bio
= bio_alloc(GFP_NOIO
, 1);
696 bio
->bi_bdev
= rdev
->bdev
;
697 bio
->bi_sector
= sector
;
698 bio_add_page(bio
, page
, size
, 0);
699 bio
->bi_private
= rdev
;
700 bio
->bi_end_io
= super_written
;
702 atomic_inc(&mddev
->pending_writes
);
703 submit_bio(REQ_WRITE
| REQ_SYNC
| REQ_UNPLUG
| REQ_FLUSH
| REQ_FUA
,
707 void md_super_wait(mddev_t
*mddev
)
709 /* wait for all superblock writes that were scheduled to complete */
712 prepare_to_wait(&mddev
->sb_wait
, &wq
, TASK_UNINTERRUPTIBLE
);
713 if (atomic_read(&mddev
->pending_writes
)==0)
717 finish_wait(&mddev
->sb_wait
, &wq
);
720 static void bi_complete(struct bio
*bio
, int error
)
722 complete((struct completion
*)bio
->bi_private
);
725 int sync_page_io(struct block_device
*bdev
, sector_t sector
, int size
,
726 struct page
*page
, int rw
)
728 struct bio
*bio
= bio_alloc(GFP_NOIO
, 1);
729 struct completion event
;
732 rw
|= REQ_SYNC
| REQ_UNPLUG
;
735 bio
->bi_sector
= sector
;
736 bio_add_page(bio
, page
, size
, 0);
737 init_completion(&event
);
738 bio
->bi_private
= &event
;
739 bio
->bi_end_io
= bi_complete
;
741 wait_for_completion(&event
);
743 ret
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
747 EXPORT_SYMBOL_GPL(sync_page_io
);
749 static int read_disk_sb(mdk_rdev_t
* rdev
, int size
)
751 char b
[BDEVNAME_SIZE
];
752 if (!rdev
->sb_page
) {
760 if (!sync_page_io(rdev
->bdev
, rdev
->sb_start
, size
, rdev
->sb_page
, READ
))
766 printk(KERN_WARNING
"md: disabled device %s, could not read superblock.\n",
767 bdevname(rdev
->bdev
,b
));
771 static int uuid_equal(mdp_super_t
*sb1
, mdp_super_t
*sb2
)
773 return sb1
->set_uuid0
== sb2
->set_uuid0
&&
774 sb1
->set_uuid1
== sb2
->set_uuid1
&&
775 sb1
->set_uuid2
== sb2
->set_uuid2
&&
776 sb1
->set_uuid3
== sb2
->set_uuid3
;
779 static int sb_equal(mdp_super_t
*sb1
, mdp_super_t
*sb2
)
782 mdp_super_t
*tmp1
, *tmp2
;
784 tmp1
= kmalloc(sizeof(*tmp1
),GFP_KERNEL
);
785 tmp2
= kmalloc(sizeof(*tmp2
),GFP_KERNEL
);
787 if (!tmp1
|| !tmp2
) {
789 printk(KERN_INFO
"md.c sb_equal(): failed to allocate memory!\n");
797 * nr_disks is not constant
802 ret
= (memcmp(tmp1
, tmp2
, MD_SB_GENERIC_CONSTANT_WORDS
* 4) == 0);
810 static u32
md_csum_fold(u32 csum
)
812 csum
= (csum
& 0xffff) + (csum
>> 16);
813 return (csum
& 0xffff) + (csum
>> 16);
816 static unsigned int calc_sb_csum(mdp_super_t
* sb
)
819 u32
*sb32
= (u32
*)sb
;
821 unsigned int disk_csum
, csum
;
823 disk_csum
= sb
->sb_csum
;
826 for (i
= 0; i
< MD_SB_BYTES
/4 ; i
++)
828 csum
= (newcsum
& 0xffffffff) + (newcsum
>>32);
832 /* This used to use csum_partial, which was wrong for several
833 * reasons including that different results are returned on
834 * different architectures. It isn't critical that we get exactly
835 * the same return value as before (we always csum_fold before
836 * testing, and that removes any differences). However as we
837 * know that csum_partial always returned a 16bit value on
838 * alphas, do a fold to maximise conformity to previous behaviour.
840 sb
->sb_csum
= md_csum_fold(disk_csum
);
842 sb
->sb_csum
= disk_csum
;
849 * Handle superblock details.
850 * We want to be able to handle multiple superblock formats
851 * so we have a common interface to them all, and an array of
852 * different handlers.
853 * We rely on user-space to write the initial superblock, and support
854 * reading and updating of superblocks.
855 * Interface methods are:
856 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
857 * loads and validates a superblock on dev.
858 * if refdev != NULL, compare superblocks on both devices
860 * 0 - dev has a superblock that is compatible with refdev
861 * 1 - dev has a superblock that is compatible and newer than refdev
862 * so dev should be used as the refdev in future
863 * -EINVAL superblock incompatible or invalid
864 * -othererror e.g. -EIO
866 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
867 * Verify that dev is acceptable into mddev.
868 * The first time, mddev->raid_disks will be 0, and data from
869 * dev should be merged in. Subsequent calls check that dev
870 * is new enough. Return 0 or -EINVAL
872 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
873 * Update the superblock for rdev with data in mddev
874 * This does not write to disc.
880 struct module
*owner
;
881 int (*load_super
)(mdk_rdev_t
*rdev
, mdk_rdev_t
*refdev
,
883 int (*validate_super
)(mddev_t
*mddev
, mdk_rdev_t
*rdev
);
884 void (*sync_super
)(mddev_t
*mddev
, mdk_rdev_t
*rdev
);
885 unsigned long long (*rdev_size_change
)(mdk_rdev_t
*rdev
,
886 sector_t num_sectors
);
890 * Check that the given mddev has no bitmap.
892 * This function is called from the run method of all personalities that do not
893 * support bitmaps. It prints an error message and returns non-zero if mddev
894 * has a bitmap. Otherwise, it returns 0.
897 int md_check_no_bitmap(mddev_t
*mddev
)
899 if (!mddev
->bitmap_info
.file
&& !mddev
->bitmap_info
.offset
)
901 printk(KERN_ERR
"%s: bitmaps are not supported for %s\n",
902 mdname(mddev
), mddev
->pers
->name
);
905 EXPORT_SYMBOL(md_check_no_bitmap
);
908 * load_super for 0.90.0
910 static int super_90_load(mdk_rdev_t
*rdev
, mdk_rdev_t
*refdev
, int minor_version
)
912 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
917 * Calculate the position of the superblock (512byte sectors),
918 * it's at the end of the disk.
920 * It also happens to be a multiple of 4Kb.
922 rdev
->sb_start
= calc_dev_sboffset(rdev
->bdev
);
924 ret
= read_disk_sb(rdev
, MD_SB_BYTES
);
929 bdevname(rdev
->bdev
, b
);
930 sb
= (mdp_super_t
*)page_address(rdev
->sb_page
);
932 if (sb
->md_magic
!= MD_SB_MAGIC
) {
933 printk(KERN_ERR
"md: invalid raid superblock magic on %s\n",
938 if (sb
->major_version
!= 0 ||
939 sb
->minor_version
< 90 ||
940 sb
->minor_version
> 91) {
941 printk(KERN_WARNING
"Bad version number %d.%d on %s\n",
942 sb
->major_version
, sb
->minor_version
,
947 if (sb
->raid_disks
<= 0)
950 if (md_csum_fold(calc_sb_csum(sb
)) != md_csum_fold(sb
->sb_csum
)) {
951 printk(KERN_WARNING
"md: invalid superblock checksum on %s\n",
956 rdev
->preferred_minor
= sb
->md_minor
;
957 rdev
->data_offset
= 0;
958 rdev
->sb_size
= MD_SB_BYTES
;
960 if (sb
->level
== LEVEL_MULTIPATH
)
963 rdev
->desc_nr
= sb
->this_disk
.number
;
969 mdp_super_t
*refsb
= (mdp_super_t
*)page_address(refdev
->sb_page
);
970 if (!uuid_equal(refsb
, sb
)) {
971 printk(KERN_WARNING
"md: %s has different UUID to %s\n",
972 b
, bdevname(refdev
->bdev
,b2
));
975 if (!sb_equal(refsb
, sb
)) {
976 printk(KERN_WARNING
"md: %s has same UUID"
977 " but different superblock to %s\n",
978 b
, bdevname(refdev
->bdev
, b2
));
982 ev2
= md_event(refsb
);
988 rdev
->sectors
= rdev
->sb_start
;
990 if (rdev
->sectors
< sb
->size
* 2 && sb
->level
> 1)
991 /* "this cannot possibly happen" ... */
999 * validate_super for 0.90.0
1001 static int super_90_validate(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1004 mdp_super_t
*sb
= (mdp_super_t
*)page_address(rdev
->sb_page
);
1005 __u64 ev1
= md_event(sb
);
1007 rdev
->raid_disk
= -1;
1008 clear_bit(Faulty
, &rdev
->flags
);
1009 clear_bit(In_sync
, &rdev
->flags
);
1010 clear_bit(WriteMostly
, &rdev
->flags
);
1012 if (mddev
->raid_disks
== 0) {
1013 mddev
->major_version
= 0;
1014 mddev
->minor_version
= sb
->minor_version
;
1015 mddev
->patch_version
= sb
->patch_version
;
1016 mddev
->external
= 0;
1017 mddev
->chunk_sectors
= sb
->chunk_size
>> 9;
1018 mddev
->ctime
= sb
->ctime
;
1019 mddev
->utime
= sb
->utime
;
1020 mddev
->level
= sb
->level
;
1021 mddev
->clevel
[0] = 0;
1022 mddev
->layout
= sb
->layout
;
1023 mddev
->raid_disks
= sb
->raid_disks
;
1024 mddev
->dev_sectors
= sb
->size
* 2;
1025 mddev
->events
= ev1
;
1026 mddev
->bitmap_info
.offset
= 0;
1027 mddev
->bitmap_info
.default_offset
= MD_SB_BYTES
>> 9;
1029 if (mddev
->minor_version
>= 91) {
1030 mddev
->reshape_position
= sb
->reshape_position
;
1031 mddev
->delta_disks
= sb
->delta_disks
;
1032 mddev
->new_level
= sb
->new_level
;
1033 mddev
->new_layout
= sb
->new_layout
;
1034 mddev
->new_chunk_sectors
= sb
->new_chunk
>> 9;
1036 mddev
->reshape_position
= MaxSector
;
1037 mddev
->delta_disks
= 0;
1038 mddev
->new_level
= mddev
->level
;
1039 mddev
->new_layout
= mddev
->layout
;
1040 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
1043 if (sb
->state
& (1<<MD_SB_CLEAN
))
1044 mddev
->recovery_cp
= MaxSector
;
1046 if (sb
->events_hi
== sb
->cp_events_hi
&&
1047 sb
->events_lo
== sb
->cp_events_lo
) {
1048 mddev
->recovery_cp
= sb
->recovery_cp
;
1050 mddev
->recovery_cp
= 0;
1053 memcpy(mddev
->uuid
+0, &sb
->set_uuid0
, 4);
1054 memcpy(mddev
->uuid
+4, &sb
->set_uuid1
, 4);
1055 memcpy(mddev
->uuid
+8, &sb
->set_uuid2
, 4);
1056 memcpy(mddev
->uuid
+12,&sb
->set_uuid3
, 4);
1058 mddev
->max_disks
= MD_SB_DISKS
;
1060 if (sb
->state
& (1<<MD_SB_BITMAP_PRESENT
) &&
1061 mddev
->bitmap_info
.file
== NULL
)
1062 mddev
->bitmap_info
.offset
=
1063 mddev
->bitmap_info
.default_offset
;
1065 } else if (mddev
->pers
== NULL
) {
1066 /* Insist on good event counter while assembling, except
1067 * for spares (which don't need an event count) */
1069 if (sb
->disks
[rdev
->desc_nr
].state
& (
1070 (1<<MD_DISK_SYNC
) | (1 << MD_DISK_ACTIVE
)))
1071 if (ev1
< mddev
->events
)
1073 } else if (mddev
->bitmap
) {
1074 /* if adding to array with a bitmap, then we can accept an
1075 * older device ... but not too old.
1077 if (ev1
< mddev
->bitmap
->events_cleared
)
1080 if (ev1
< mddev
->events
)
1081 /* just a hot-add of a new device, leave raid_disk at -1 */
1085 if (mddev
->level
!= LEVEL_MULTIPATH
) {
1086 desc
= sb
->disks
+ rdev
->desc_nr
;
1088 if (desc
->state
& (1<<MD_DISK_FAULTY
))
1089 set_bit(Faulty
, &rdev
->flags
);
1090 else if (desc
->state
& (1<<MD_DISK_SYNC
) /* &&
1091 desc->raid_disk < mddev->raid_disks */) {
1092 set_bit(In_sync
, &rdev
->flags
);
1093 rdev
->raid_disk
= desc
->raid_disk
;
1094 } else if (desc
->state
& (1<<MD_DISK_ACTIVE
)) {
1095 /* active but not in sync implies recovery up to
1096 * reshape position. We don't know exactly where
1097 * that is, so set to zero for now */
1098 if (mddev
->minor_version
>= 91) {
1099 rdev
->recovery_offset
= 0;
1100 rdev
->raid_disk
= desc
->raid_disk
;
1103 if (desc
->state
& (1<<MD_DISK_WRITEMOSTLY
))
1104 set_bit(WriteMostly
, &rdev
->flags
);
1105 } else /* MULTIPATH are always insync */
1106 set_bit(In_sync
, &rdev
->flags
);
1111 * sync_super for 0.90.0
1113 static void super_90_sync(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1117 int next_spare
= mddev
->raid_disks
;
1120 /* make rdev->sb match mddev data..
1123 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1124 * 3/ any empty disks < next_spare become removed
1126 * disks[0] gets initialised to REMOVED because
1127 * we cannot be sure from other fields if it has
1128 * been initialised or not.
1131 int active
=0, working
=0,failed
=0,spare
=0,nr_disks
=0;
1133 rdev
->sb_size
= MD_SB_BYTES
;
1135 sb
= (mdp_super_t
*)page_address(rdev
->sb_page
);
1137 memset(sb
, 0, sizeof(*sb
));
1139 sb
->md_magic
= MD_SB_MAGIC
;
1140 sb
->major_version
= mddev
->major_version
;
1141 sb
->patch_version
= mddev
->patch_version
;
1142 sb
->gvalid_words
= 0; /* ignored */
1143 memcpy(&sb
->set_uuid0
, mddev
->uuid
+0, 4);
1144 memcpy(&sb
->set_uuid1
, mddev
->uuid
+4, 4);
1145 memcpy(&sb
->set_uuid2
, mddev
->uuid
+8, 4);
1146 memcpy(&sb
->set_uuid3
, mddev
->uuid
+12,4);
1148 sb
->ctime
= mddev
->ctime
;
1149 sb
->level
= mddev
->level
;
1150 sb
->size
= mddev
->dev_sectors
/ 2;
1151 sb
->raid_disks
= mddev
->raid_disks
;
1152 sb
->md_minor
= mddev
->md_minor
;
1153 sb
->not_persistent
= 0;
1154 sb
->utime
= mddev
->utime
;
1156 sb
->events_hi
= (mddev
->events
>>32);
1157 sb
->events_lo
= (u32
)mddev
->events
;
1159 if (mddev
->reshape_position
== MaxSector
)
1160 sb
->minor_version
= 90;
1162 sb
->minor_version
= 91;
1163 sb
->reshape_position
= mddev
->reshape_position
;
1164 sb
->new_level
= mddev
->new_level
;
1165 sb
->delta_disks
= mddev
->delta_disks
;
1166 sb
->new_layout
= mddev
->new_layout
;
1167 sb
->new_chunk
= mddev
->new_chunk_sectors
<< 9;
1169 mddev
->minor_version
= sb
->minor_version
;
1172 sb
->recovery_cp
= mddev
->recovery_cp
;
1173 sb
->cp_events_hi
= (mddev
->events
>>32);
1174 sb
->cp_events_lo
= (u32
)mddev
->events
;
1175 if (mddev
->recovery_cp
== MaxSector
)
1176 sb
->state
= (1<< MD_SB_CLEAN
);
1178 sb
->recovery_cp
= 0;
1180 sb
->layout
= mddev
->layout
;
1181 sb
->chunk_size
= mddev
->chunk_sectors
<< 9;
1183 if (mddev
->bitmap
&& mddev
->bitmap_info
.file
== NULL
)
1184 sb
->state
|= (1<<MD_SB_BITMAP_PRESENT
);
1186 sb
->disks
[0].state
= (1<<MD_DISK_REMOVED
);
1187 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
) {
1190 int is_active
= test_bit(In_sync
, &rdev2
->flags
);
1192 if (rdev2
->raid_disk
>= 0 &&
1193 sb
->minor_version
>= 91)
1194 /* we have nowhere to store the recovery_offset,
1195 * but if it is not below the reshape_position,
1196 * we can piggy-back on that.
1199 if (rdev2
->raid_disk
< 0 ||
1200 test_bit(Faulty
, &rdev2
->flags
))
1203 desc_nr
= rdev2
->raid_disk
;
1205 desc_nr
= next_spare
++;
1206 rdev2
->desc_nr
= desc_nr
;
1207 d
= &sb
->disks
[rdev2
->desc_nr
];
1209 d
->number
= rdev2
->desc_nr
;
1210 d
->major
= MAJOR(rdev2
->bdev
->bd_dev
);
1211 d
->minor
= MINOR(rdev2
->bdev
->bd_dev
);
1213 d
->raid_disk
= rdev2
->raid_disk
;
1215 d
->raid_disk
= rdev2
->desc_nr
; /* compatibility */
1216 if (test_bit(Faulty
, &rdev2
->flags
))
1217 d
->state
= (1<<MD_DISK_FAULTY
);
1218 else if (is_active
) {
1219 d
->state
= (1<<MD_DISK_ACTIVE
);
1220 if (test_bit(In_sync
, &rdev2
->flags
))
1221 d
->state
|= (1<<MD_DISK_SYNC
);
1229 if (test_bit(WriteMostly
, &rdev2
->flags
))
1230 d
->state
|= (1<<MD_DISK_WRITEMOSTLY
);
1232 /* now set the "removed" and "faulty" bits on any missing devices */
1233 for (i
=0 ; i
< mddev
->raid_disks
; i
++) {
1234 mdp_disk_t
*d
= &sb
->disks
[i
];
1235 if (d
->state
== 0 && d
->number
== 0) {
1238 d
->state
= (1<<MD_DISK_REMOVED
);
1239 d
->state
|= (1<<MD_DISK_FAULTY
);
1243 sb
->nr_disks
= nr_disks
;
1244 sb
->active_disks
= active
;
1245 sb
->working_disks
= working
;
1246 sb
->failed_disks
= failed
;
1247 sb
->spare_disks
= spare
;
1249 sb
->this_disk
= sb
->disks
[rdev
->desc_nr
];
1250 sb
->sb_csum
= calc_sb_csum(sb
);
1254 * rdev_size_change for 0.90.0
1256 static unsigned long long
1257 super_90_rdev_size_change(mdk_rdev_t
*rdev
, sector_t num_sectors
)
1259 if (num_sectors
&& num_sectors
< rdev
->mddev
->dev_sectors
)
1260 return 0; /* component must fit device */
1261 if (rdev
->mddev
->bitmap_info
.offset
)
1262 return 0; /* can't move bitmap */
1263 rdev
->sb_start
= calc_dev_sboffset(rdev
->bdev
);
1264 if (!num_sectors
|| num_sectors
> rdev
->sb_start
)
1265 num_sectors
= rdev
->sb_start
;
1266 md_super_write(rdev
->mddev
, rdev
, rdev
->sb_start
, rdev
->sb_size
,
1268 md_super_wait(rdev
->mddev
);
1269 return num_sectors
/ 2; /* kB for sysfs */
1274 * version 1 superblock
1277 static __le32
calc_sb_1_csum(struct mdp_superblock_1
* sb
)
1281 unsigned long long newcsum
;
1282 int size
= 256 + le32_to_cpu(sb
->max_dev
)*2;
1283 __le32
*isuper
= (__le32
*)sb
;
1286 disk_csum
= sb
->sb_csum
;
1289 for (i
=0; size
>=4; size
-= 4 )
1290 newcsum
+= le32_to_cpu(*isuper
++);
1293 newcsum
+= le16_to_cpu(*(__le16
*) isuper
);
1295 csum
= (newcsum
& 0xffffffff) + (newcsum
>> 32);
1296 sb
->sb_csum
= disk_csum
;
1297 return cpu_to_le32(csum
);
1300 static int super_1_load(mdk_rdev_t
*rdev
, mdk_rdev_t
*refdev
, int minor_version
)
1302 struct mdp_superblock_1
*sb
;
1305 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
1309 * Calculate the position of the superblock in 512byte sectors.
1310 * It is always aligned to a 4K boundary and
1311 * depeding on minor_version, it can be:
1312 * 0: At least 8K, but less than 12K, from end of device
1313 * 1: At start of device
1314 * 2: 4K from start of device.
1316 switch(minor_version
) {
1318 sb_start
= rdev
->bdev
->bd_inode
->i_size
>> 9;
1320 sb_start
&= ~(sector_t
)(4*2-1);
1331 rdev
->sb_start
= sb_start
;
1333 /* superblock is rarely larger than 1K, but it can be larger,
1334 * and it is safe to read 4k, so we do that
1336 ret
= read_disk_sb(rdev
, 4096);
1337 if (ret
) return ret
;
1340 sb
= (struct mdp_superblock_1
*)page_address(rdev
->sb_page
);
1342 if (sb
->magic
!= cpu_to_le32(MD_SB_MAGIC
) ||
1343 sb
->major_version
!= cpu_to_le32(1) ||
1344 le32_to_cpu(sb
->max_dev
) > (4096-256)/2 ||
1345 le64_to_cpu(sb
->super_offset
) != rdev
->sb_start
||
1346 (le32_to_cpu(sb
->feature_map
) & ~MD_FEATURE_ALL
) != 0)
1349 if (calc_sb_1_csum(sb
) != sb
->sb_csum
) {
1350 printk("md: invalid superblock checksum on %s\n",
1351 bdevname(rdev
->bdev
,b
));
1354 if (le64_to_cpu(sb
->data_size
) < 10) {
1355 printk("md: data_size too small on %s\n",
1356 bdevname(rdev
->bdev
,b
));
1360 rdev
->preferred_minor
= 0xffff;
1361 rdev
->data_offset
= le64_to_cpu(sb
->data_offset
);
1362 atomic_set(&rdev
->corrected_errors
, le32_to_cpu(sb
->cnt_corrected_read
));
1364 rdev
->sb_size
= le32_to_cpu(sb
->max_dev
) * 2 + 256;
1365 bmask
= queue_logical_block_size(rdev
->bdev
->bd_disk
->queue
)-1;
1366 if (rdev
->sb_size
& bmask
)
1367 rdev
->sb_size
= (rdev
->sb_size
| bmask
) + 1;
1370 && rdev
->data_offset
< sb_start
+ (rdev
->sb_size
/512))
1373 if (sb
->level
== cpu_to_le32(LEVEL_MULTIPATH
))
1376 rdev
->desc_nr
= le32_to_cpu(sb
->dev_number
);
1382 struct mdp_superblock_1
*refsb
=
1383 (struct mdp_superblock_1
*)page_address(refdev
->sb_page
);
1385 if (memcmp(sb
->set_uuid
, refsb
->set_uuid
, 16) != 0 ||
1386 sb
->level
!= refsb
->level
||
1387 sb
->layout
!= refsb
->layout
||
1388 sb
->chunksize
!= refsb
->chunksize
) {
1389 printk(KERN_WARNING
"md: %s has strangely different"
1390 " superblock to %s\n",
1391 bdevname(rdev
->bdev
,b
),
1392 bdevname(refdev
->bdev
,b2
));
1395 ev1
= le64_to_cpu(sb
->events
);
1396 ev2
= le64_to_cpu(refsb
->events
);
1404 rdev
->sectors
= (rdev
->bdev
->bd_inode
->i_size
>> 9) -
1405 le64_to_cpu(sb
->data_offset
);
1407 rdev
->sectors
= rdev
->sb_start
;
1408 if (rdev
->sectors
< le64_to_cpu(sb
->data_size
))
1410 rdev
->sectors
= le64_to_cpu(sb
->data_size
);
1411 if (le64_to_cpu(sb
->size
) > rdev
->sectors
)
1416 static int super_1_validate(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1418 struct mdp_superblock_1
*sb
= (struct mdp_superblock_1
*)page_address(rdev
->sb_page
);
1419 __u64 ev1
= le64_to_cpu(sb
->events
);
1421 rdev
->raid_disk
= -1;
1422 clear_bit(Faulty
, &rdev
->flags
);
1423 clear_bit(In_sync
, &rdev
->flags
);
1424 clear_bit(WriteMostly
, &rdev
->flags
);
1426 if (mddev
->raid_disks
== 0) {
1427 mddev
->major_version
= 1;
1428 mddev
->patch_version
= 0;
1429 mddev
->external
= 0;
1430 mddev
->chunk_sectors
= le32_to_cpu(sb
->chunksize
);
1431 mddev
->ctime
= le64_to_cpu(sb
->ctime
) & ((1ULL << 32)-1);
1432 mddev
->utime
= le64_to_cpu(sb
->utime
) & ((1ULL << 32)-1);
1433 mddev
->level
= le32_to_cpu(sb
->level
);
1434 mddev
->clevel
[0] = 0;
1435 mddev
->layout
= le32_to_cpu(sb
->layout
);
1436 mddev
->raid_disks
= le32_to_cpu(sb
->raid_disks
);
1437 mddev
->dev_sectors
= le64_to_cpu(sb
->size
);
1438 mddev
->events
= ev1
;
1439 mddev
->bitmap_info
.offset
= 0;
1440 mddev
->bitmap_info
.default_offset
= 1024 >> 9;
1442 mddev
->recovery_cp
= le64_to_cpu(sb
->resync_offset
);
1443 memcpy(mddev
->uuid
, sb
->set_uuid
, 16);
1445 mddev
->max_disks
= (4096-256)/2;
1447 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_BITMAP_OFFSET
) &&
1448 mddev
->bitmap_info
.file
== NULL
)
1449 mddev
->bitmap_info
.offset
=
1450 (__s32
)le32_to_cpu(sb
->bitmap_offset
);
1452 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RESHAPE_ACTIVE
)) {
1453 mddev
->reshape_position
= le64_to_cpu(sb
->reshape_position
);
1454 mddev
->delta_disks
= le32_to_cpu(sb
->delta_disks
);
1455 mddev
->new_level
= le32_to_cpu(sb
->new_level
);
1456 mddev
->new_layout
= le32_to_cpu(sb
->new_layout
);
1457 mddev
->new_chunk_sectors
= le32_to_cpu(sb
->new_chunk
);
1459 mddev
->reshape_position
= MaxSector
;
1460 mddev
->delta_disks
= 0;
1461 mddev
->new_level
= mddev
->level
;
1462 mddev
->new_layout
= mddev
->layout
;
1463 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
1466 } else if (mddev
->pers
== NULL
) {
1467 /* Insist of good event counter while assembling, except for
1468 * spares (which don't need an event count) */
1470 if (rdev
->desc_nr
>= 0 &&
1471 rdev
->desc_nr
< le32_to_cpu(sb
->max_dev
) &&
1472 le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]) < 0xfffe)
1473 if (ev1
< mddev
->events
)
1475 } else if (mddev
->bitmap
) {
1476 /* If adding to array with a bitmap, then we can accept an
1477 * older device, but not too old.
1479 if (ev1
< mddev
->bitmap
->events_cleared
)
1482 if (ev1
< mddev
->events
)
1483 /* just a hot-add of a new device, leave raid_disk at -1 */
1486 if (mddev
->level
!= LEVEL_MULTIPATH
) {
1488 if (rdev
->desc_nr
< 0 ||
1489 rdev
->desc_nr
>= le32_to_cpu(sb
->max_dev
)) {
1493 role
= le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]);
1495 case 0xffff: /* spare */
1497 case 0xfffe: /* faulty */
1498 set_bit(Faulty
, &rdev
->flags
);
1501 if ((le32_to_cpu(sb
->feature_map
) &
1502 MD_FEATURE_RECOVERY_OFFSET
))
1503 rdev
->recovery_offset
= le64_to_cpu(sb
->recovery_offset
);
1505 set_bit(In_sync
, &rdev
->flags
);
1506 rdev
->raid_disk
= role
;
1509 if (sb
->devflags
& WriteMostly1
)
1510 set_bit(WriteMostly
, &rdev
->flags
);
1511 } else /* MULTIPATH are always insync */
1512 set_bit(In_sync
, &rdev
->flags
);
1517 static void super_1_sync(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1519 struct mdp_superblock_1
*sb
;
1522 /* make rdev->sb match mddev and rdev data. */
1524 sb
= (struct mdp_superblock_1
*)page_address(rdev
->sb_page
);
1526 sb
->feature_map
= 0;
1528 sb
->recovery_offset
= cpu_to_le64(0);
1529 memset(sb
->pad1
, 0, sizeof(sb
->pad1
));
1530 memset(sb
->pad2
, 0, sizeof(sb
->pad2
));
1531 memset(sb
->pad3
, 0, sizeof(sb
->pad3
));
1533 sb
->utime
= cpu_to_le64((__u64
)mddev
->utime
);
1534 sb
->events
= cpu_to_le64(mddev
->events
);
1536 sb
->resync_offset
= cpu_to_le64(mddev
->recovery_cp
);
1538 sb
->resync_offset
= cpu_to_le64(0);
1540 sb
->cnt_corrected_read
= cpu_to_le32(atomic_read(&rdev
->corrected_errors
));
1542 sb
->raid_disks
= cpu_to_le32(mddev
->raid_disks
);
1543 sb
->size
= cpu_to_le64(mddev
->dev_sectors
);
1544 sb
->chunksize
= cpu_to_le32(mddev
->chunk_sectors
);
1545 sb
->level
= cpu_to_le32(mddev
->level
);
1546 sb
->layout
= cpu_to_le32(mddev
->layout
);
1548 if (mddev
->bitmap
&& mddev
->bitmap_info
.file
== NULL
) {
1549 sb
->bitmap_offset
= cpu_to_le32((__u32
)mddev
->bitmap_info
.offset
);
1550 sb
->feature_map
= cpu_to_le32(MD_FEATURE_BITMAP_OFFSET
);
1553 if (rdev
->raid_disk
>= 0 &&
1554 !test_bit(In_sync
, &rdev
->flags
)) {
1556 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET
);
1557 sb
->recovery_offset
=
1558 cpu_to_le64(rdev
->recovery_offset
);
1561 if (mddev
->reshape_position
!= MaxSector
) {
1562 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE
);
1563 sb
->reshape_position
= cpu_to_le64(mddev
->reshape_position
);
1564 sb
->new_layout
= cpu_to_le32(mddev
->new_layout
);
1565 sb
->delta_disks
= cpu_to_le32(mddev
->delta_disks
);
1566 sb
->new_level
= cpu_to_le32(mddev
->new_level
);
1567 sb
->new_chunk
= cpu_to_le32(mddev
->new_chunk_sectors
);
1571 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
)
1572 if (rdev2
->desc_nr
+1 > max_dev
)
1573 max_dev
= rdev2
->desc_nr
+1;
1575 if (max_dev
> le32_to_cpu(sb
->max_dev
)) {
1577 sb
->max_dev
= cpu_to_le32(max_dev
);
1578 rdev
->sb_size
= max_dev
* 2 + 256;
1579 bmask
= queue_logical_block_size(rdev
->bdev
->bd_disk
->queue
)-1;
1580 if (rdev
->sb_size
& bmask
)
1581 rdev
->sb_size
= (rdev
->sb_size
| bmask
) + 1;
1583 max_dev
= le32_to_cpu(sb
->max_dev
);
1585 for (i
=0; i
<max_dev
;i
++)
1586 sb
->dev_roles
[i
] = cpu_to_le16(0xfffe);
1588 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
) {
1590 if (test_bit(Faulty
, &rdev2
->flags
))
1591 sb
->dev_roles
[i
] = cpu_to_le16(0xfffe);
1592 else if (test_bit(In_sync
, &rdev2
->flags
))
1593 sb
->dev_roles
[i
] = cpu_to_le16(rdev2
->raid_disk
);
1594 else if (rdev2
->raid_disk
>= 0)
1595 sb
->dev_roles
[i
] = cpu_to_le16(rdev2
->raid_disk
);
1597 sb
->dev_roles
[i
] = cpu_to_le16(0xffff);
1600 sb
->sb_csum
= calc_sb_1_csum(sb
);
1603 static unsigned long long
1604 super_1_rdev_size_change(mdk_rdev_t
*rdev
, sector_t num_sectors
)
1606 struct mdp_superblock_1
*sb
;
1607 sector_t max_sectors
;
1608 if (num_sectors
&& num_sectors
< rdev
->mddev
->dev_sectors
)
1609 return 0; /* component must fit device */
1610 if (rdev
->sb_start
< rdev
->data_offset
) {
1611 /* minor versions 1 and 2; superblock before data */
1612 max_sectors
= rdev
->bdev
->bd_inode
->i_size
>> 9;
1613 max_sectors
-= rdev
->data_offset
;
1614 if (!num_sectors
|| num_sectors
> max_sectors
)
1615 num_sectors
= max_sectors
;
1616 } else if (rdev
->mddev
->bitmap_info
.offset
) {
1617 /* minor version 0 with bitmap we can't move */
1620 /* minor version 0; superblock after data */
1622 sb_start
= (rdev
->bdev
->bd_inode
->i_size
>> 9) - 8*2;
1623 sb_start
&= ~(sector_t
)(4*2 - 1);
1624 max_sectors
= rdev
->sectors
+ sb_start
- rdev
->sb_start
;
1625 if (!num_sectors
|| num_sectors
> max_sectors
)
1626 num_sectors
= max_sectors
;
1627 rdev
->sb_start
= sb_start
;
1629 sb
= (struct mdp_superblock_1
*) page_address(rdev
->sb_page
);
1630 sb
->data_size
= cpu_to_le64(num_sectors
);
1631 sb
->super_offset
= rdev
->sb_start
;
1632 sb
->sb_csum
= calc_sb_1_csum(sb
);
1633 md_super_write(rdev
->mddev
, rdev
, rdev
->sb_start
, rdev
->sb_size
,
1635 md_super_wait(rdev
->mddev
);
1636 return num_sectors
/ 2; /* kB for sysfs */
1639 static struct super_type super_types
[] = {
1642 .owner
= THIS_MODULE
,
1643 .load_super
= super_90_load
,
1644 .validate_super
= super_90_validate
,
1645 .sync_super
= super_90_sync
,
1646 .rdev_size_change
= super_90_rdev_size_change
,
1650 .owner
= THIS_MODULE
,
1651 .load_super
= super_1_load
,
1652 .validate_super
= super_1_validate
,
1653 .sync_super
= super_1_sync
,
1654 .rdev_size_change
= super_1_rdev_size_change
,
1658 static int match_mddev_units(mddev_t
*mddev1
, mddev_t
*mddev2
)
1660 mdk_rdev_t
*rdev
, *rdev2
;
1663 rdev_for_each_rcu(rdev
, mddev1
)
1664 rdev_for_each_rcu(rdev2
, mddev2
)
1665 if (rdev
->bdev
->bd_contains
==
1666 rdev2
->bdev
->bd_contains
) {
1674 static LIST_HEAD(pending_raid_disks
);
1677 * Try to register data integrity profile for an mddev
1679 * This is called when an array is started and after a disk has been kicked
1680 * from the array. It only succeeds if all working and active component devices
1681 * are integrity capable with matching profiles.
1683 int md_integrity_register(mddev_t
*mddev
)
1685 mdk_rdev_t
*rdev
, *reference
= NULL
;
1687 if (list_empty(&mddev
->disks
))
1688 return 0; /* nothing to do */
1689 if (blk_get_integrity(mddev
->gendisk
))
1690 return 0; /* already registered */
1691 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
1692 /* skip spares and non-functional disks */
1693 if (test_bit(Faulty
, &rdev
->flags
))
1695 if (rdev
->raid_disk
< 0)
1698 * If at least one rdev is not integrity capable, we can not
1699 * enable data integrity for the md device.
1701 if (!bdev_get_integrity(rdev
->bdev
))
1704 /* Use the first rdev as the reference */
1708 /* does this rdev's profile match the reference profile? */
1709 if (blk_integrity_compare(reference
->bdev
->bd_disk
,
1710 rdev
->bdev
->bd_disk
) < 0)
1714 * All component devices are integrity capable and have matching
1715 * profiles, register the common profile for the md device.
1717 if (blk_integrity_register(mddev
->gendisk
,
1718 bdev_get_integrity(reference
->bdev
)) != 0) {
1719 printk(KERN_ERR
"md: failed to register integrity for %s\n",
1723 printk(KERN_NOTICE
"md: data integrity on %s enabled\n",
1727 EXPORT_SYMBOL(md_integrity_register
);
1729 /* Disable data integrity if non-capable/non-matching disk is being added */
1730 void md_integrity_add_rdev(mdk_rdev_t
*rdev
, mddev_t
*mddev
)
1732 struct blk_integrity
*bi_rdev
= bdev_get_integrity(rdev
->bdev
);
1733 struct blk_integrity
*bi_mddev
= blk_get_integrity(mddev
->gendisk
);
1735 if (!bi_mddev
) /* nothing to do */
1737 if (rdev
->raid_disk
< 0) /* skip spares */
1739 if (bi_rdev
&& blk_integrity_compare(mddev
->gendisk
,
1740 rdev
->bdev
->bd_disk
) >= 0)
1742 printk(KERN_NOTICE
"disabling data integrity on %s\n", mdname(mddev
));
1743 blk_integrity_unregister(mddev
->gendisk
);
1745 EXPORT_SYMBOL(md_integrity_add_rdev
);
1747 static int bind_rdev_to_array(mdk_rdev_t
* rdev
, mddev_t
* mddev
)
1749 char b
[BDEVNAME_SIZE
];
1759 /* prevent duplicates */
1760 if (find_rdev(mddev
, rdev
->bdev
->bd_dev
))
1763 /* make sure rdev->sectors exceeds mddev->dev_sectors */
1764 if (rdev
->sectors
&& (mddev
->dev_sectors
== 0 ||
1765 rdev
->sectors
< mddev
->dev_sectors
)) {
1767 /* Cannot change size, so fail
1768 * If mddev->level <= 0, then we don't care
1769 * about aligning sizes (e.g. linear)
1771 if (mddev
->level
> 0)
1774 mddev
->dev_sectors
= rdev
->sectors
;
1777 /* Verify rdev->desc_nr is unique.
1778 * If it is -1, assign a free number, else
1779 * check number is not in use
1781 if (rdev
->desc_nr
< 0) {
1783 if (mddev
->pers
) choice
= mddev
->raid_disks
;
1784 while (find_rdev_nr(mddev
, choice
))
1786 rdev
->desc_nr
= choice
;
1788 if (find_rdev_nr(mddev
, rdev
->desc_nr
))
1791 if (mddev
->max_disks
&& rdev
->desc_nr
>= mddev
->max_disks
) {
1792 printk(KERN_WARNING
"md: %s: array is limited to %d devices\n",
1793 mdname(mddev
), mddev
->max_disks
);
1796 bdevname(rdev
->bdev
,b
);
1797 while ( (s
=strchr(b
, '/')) != NULL
)
1800 rdev
->mddev
= mddev
;
1801 printk(KERN_INFO
"md: bind<%s>\n", b
);
1803 if ((err
= kobject_add(&rdev
->kobj
, &mddev
->kobj
, "dev-%s", b
)))
1806 ko
= &part_to_dev(rdev
->bdev
->bd_part
)->kobj
;
1807 if (sysfs_create_link(&rdev
->kobj
, ko
, "block"))
1808 /* failure here is OK */;
1809 rdev
->sysfs_state
= sysfs_get_dirent_safe(rdev
->kobj
.sd
, "state");
1811 list_add_rcu(&rdev
->same_set
, &mddev
->disks
);
1812 bd_claim_by_disk(rdev
->bdev
, rdev
->bdev
->bd_holder
, mddev
->gendisk
);
1814 /* May as well allow recovery to be retried once */
1815 mddev
->recovery_disabled
= 0;
1820 printk(KERN_WARNING
"md: failed to register dev-%s for %s\n",
1825 static void md_delayed_delete(struct work_struct
*ws
)
1827 mdk_rdev_t
*rdev
= container_of(ws
, mdk_rdev_t
, del_work
);
1828 kobject_del(&rdev
->kobj
);
1829 kobject_put(&rdev
->kobj
);
1832 static void unbind_rdev_from_array(mdk_rdev_t
* rdev
)
1834 char b
[BDEVNAME_SIZE
];
1839 bd_release_from_disk(rdev
->bdev
, rdev
->mddev
->gendisk
);
1840 list_del_rcu(&rdev
->same_set
);
1841 printk(KERN_INFO
"md: unbind<%s>\n", bdevname(rdev
->bdev
,b
));
1843 sysfs_remove_link(&rdev
->kobj
, "block");
1844 sysfs_put(rdev
->sysfs_state
);
1845 rdev
->sysfs_state
= NULL
;
1846 /* We need to delay this, otherwise we can deadlock when
1847 * writing to 'remove' to "dev/state". We also need
1848 * to delay it due to rcu usage.
1851 INIT_WORK(&rdev
->del_work
, md_delayed_delete
);
1852 kobject_get(&rdev
->kobj
);
1853 schedule_work(&rdev
->del_work
);
1857 * prevent the device from being mounted, repartitioned or
1858 * otherwise reused by a RAID array (or any other kernel
1859 * subsystem), by bd_claiming the device.
1861 static int lock_rdev(mdk_rdev_t
*rdev
, dev_t dev
, int shared
)
1864 struct block_device
*bdev
;
1865 char b
[BDEVNAME_SIZE
];
1867 bdev
= open_by_devnum(dev
, FMODE_READ
|FMODE_WRITE
);
1869 printk(KERN_ERR
"md: could not open %s.\n",
1870 __bdevname(dev
, b
));
1871 return PTR_ERR(bdev
);
1873 err
= bd_claim(bdev
, shared
? (mdk_rdev_t
*)lock_rdev
: rdev
);
1875 printk(KERN_ERR
"md: could not bd_claim %s.\n",
1877 blkdev_put(bdev
, FMODE_READ
|FMODE_WRITE
);
1881 set_bit(AllReserved
, &rdev
->flags
);
1886 static void unlock_rdev(mdk_rdev_t
*rdev
)
1888 struct block_device
*bdev
= rdev
->bdev
;
1893 blkdev_put(bdev
, FMODE_READ
|FMODE_WRITE
);
1896 void md_autodetect_dev(dev_t dev
);
1898 static void export_rdev(mdk_rdev_t
* rdev
)
1900 char b
[BDEVNAME_SIZE
];
1901 printk(KERN_INFO
"md: export_rdev(%s)\n",
1902 bdevname(rdev
->bdev
,b
));
1907 if (test_bit(AutoDetected
, &rdev
->flags
))
1908 md_autodetect_dev(rdev
->bdev
->bd_dev
);
1911 kobject_put(&rdev
->kobj
);
1914 static void kick_rdev_from_array(mdk_rdev_t
* rdev
)
1916 unbind_rdev_from_array(rdev
);
1920 static void export_array(mddev_t
*mddev
)
1922 mdk_rdev_t
*rdev
, *tmp
;
1924 rdev_for_each(rdev
, tmp
, mddev
) {
1929 kick_rdev_from_array(rdev
);
1931 if (!list_empty(&mddev
->disks
))
1933 mddev
->raid_disks
= 0;
1934 mddev
->major_version
= 0;
1937 static void print_desc(mdp_disk_t
*desc
)
1939 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc
->number
,
1940 desc
->major
,desc
->minor
,desc
->raid_disk
,desc
->state
);
1943 static void print_sb_90(mdp_super_t
*sb
)
1948 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1949 sb
->major_version
, sb
->minor_version
, sb
->patch_version
,
1950 sb
->set_uuid0
, sb
->set_uuid1
, sb
->set_uuid2
, sb
->set_uuid3
,
1952 printk(KERN_INFO
"md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1953 sb
->level
, sb
->size
, sb
->nr_disks
, sb
->raid_disks
,
1954 sb
->md_minor
, sb
->layout
, sb
->chunk_size
);
1955 printk(KERN_INFO
"md: UT:%08x ST:%d AD:%d WD:%d"
1956 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1957 sb
->utime
, sb
->state
, sb
->active_disks
, sb
->working_disks
,
1958 sb
->failed_disks
, sb
->spare_disks
,
1959 sb
->sb_csum
, (unsigned long)sb
->events_lo
);
1962 for (i
= 0; i
< MD_SB_DISKS
; i
++) {
1965 desc
= sb
->disks
+ i
;
1966 if (desc
->number
|| desc
->major
|| desc
->minor
||
1967 desc
->raid_disk
|| (desc
->state
&& (desc
->state
!= 4))) {
1968 printk(" D %2d: ", i
);
1972 printk(KERN_INFO
"md: THIS: ");
1973 print_desc(&sb
->this_disk
);
1976 static void print_sb_1(struct mdp_superblock_1
*sb
)
1980 uuid
= sb
->set_uuid
;
1982 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n"
1983 "md: Name: \"%s\" CT:%llu\n",
1984 le32_to_cpu(sb
->major_version
),
1985 le32_to_cpu(sb
->feature_map
),
1988 (unsigned long long)le64_to_cpu(sb
->ctime
)
1989 & MD_SUPERBLOCK_1_TIME_SEC_MASK
);
1991 uuid
= sb
->device_uuid
;
1993 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
1995 "md: Dev:%08x UUID: %pU\n"
1996 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
1997 "md: (MaxDev:%u) \n",
1998 le32_to_cpu(sb
->level
),
1999 (unsigned long long)le64_to_cpu(sb
->size
),
2000 le32_to_cpu(sb
->raid_disks
),
2001 le32_to_cpu(sb
->layout
),
2002 le32_to_cpu(sb
->chunksize
),
2003 (unsigned long long)le64_to_cpu(sb
->data_offset
),
2004 (unsigned long long)le64_to_cpu(sb
->data_size
),
2005 (unsigned long long)le64_to_cpu(sb
->super_offset
),
2006 (unsigned long long)le64_to_cpu(sb
->recovery_offset
),
2007 le32_to_cpu(sb
->dev_number
),
2010 (unsigned long long)le64_to_cpu(sb
->utime
) & MD_SUPERBLOCK_1_TIME_SEC_MASK
,
2011 (unsigned long long)le64_to_cpu(sb
->events
),
2012 (unsigned long long)le64_to_cpu(sb
->resync_offset
),
2013 le32_to_cpu(sb
->sb_csum
),
2014 le32_to_cpu(sb
->max_dev
)
2018 static void print_rdev(mdk_rdev_t
*rdev
, int major_version
)
2020 char b
[BDEVNAME_SIZE
];
2021 printk(KERN_INFO
"md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
2022 bdevname(rdev
->bdev
, b
), (unsigned long long)rdev
->sectors
,
2023 test_bit(Faulty
, &rdev
->flags
), test_bit(In_sync
, &rdev
->flags
),
2025 if (rdev
->sb_loaded
) {
2026 printk(KERN_INFO
"md: rdev superblock (MJ:%d):\n", major_version
);
2027 switch (major_version
) {
2029 print_sb_90((mdp_super_t
*)page_address(rdev
->sb_page
));
2032 print_sb_1((struct mdp_superblock_1
*)page_address(rdev
->sb_page
));
2036 printk(KERN_INFO
"md: no rdev superblock!\n");
2039 static void md_print_devices(void)
2041 struct list_head
*tmp
;
2044 char b
[BDEVNAME_SIZE
];
2047 printk("md: **********************************\n");
2048 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
2049 printk("md: **********************************\n");
2050 for_each_mddev(mddev
, tmp
) {
2053 bitmap_print_sb(mddev
->bitmap
);
2055 printk("%s: ", mdname(mddev
));
2056 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
2057 printk("<%s>", bdevname(rdev
->bdev
,b
));
2060 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
2061 print_rdev(rdev
, mddev
->major_version
);
2063 printk("md: **********************************\n");
2068 static void sync_sbs(mddev_t
* mddev
, int nospares
)
2070 /* Update each superblock (in-memory image), but
2071 * if we are allowed to, skip spares which already
2072 * have the right event counter, or have one earlier
2073 * (which would mean they aren't being marked as dirty
2074 * with the rest of the array)
2077 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
2078 if (rdev
->sb_events
== mddev
->events
||
2080 rdev
->raid_disk
< 0 &&
2081 rdev
->sb_events
+1 == mddev
->events
)) {
2082 /* Don't update this superblock */
2083 rdev
->sb_loaded
= 2;
2085 super_types
[mddev
->major_version
].
2086 sync_super(mddev
, rdev
);
2087 rdev
->sb_loaded
= 1;
2092 static void md_update_sb(mddev_t
* mddev
, int force_change
)
2099 /* First make sure individual recovery_offsets are correct */
2100 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
2101 if (rdev
->raid_disk
>= 0 &&
2102 mddev
->delta_disks
>= 0 &&
2103 !test_bit(In_sync
, &rdev
->flags
) &&
2104 mddev
->curr_resync_completed
> rdev
->recovery_offset
)
2105 rdev
->recovery_offset
= mddev
->curr_resync_completed
;
2108 if (!mddev
->persistent
) {
2109 clear_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
2110 clear_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
2111 wake_up(&mddev
->sb_wait
);
2115 spin_lock_irq(&mddev
->write_lock
);
2117 mddev
->utime
= get_seconds();
2119 if (test_and_clear_bit(MD_CHANGE_DEVS
, &mddev
->flags
))
2121 if (test_and_clear_bit(MD_CHANGE_CLEAN
, &mddev
->flags
))
2122 /* just a clean<-> dirty transition, possibly leave spares alone,
2123 * though if events isn't the right even/odd, we will have to do
2129 if (mddev
->degraded
)
2130 /* If the array is degraded, then skipping spares is both
2131 * dangerous and fairly pointless.
2132 * Dangerous because a device that was removed from the array
2133 * might have a event_count that still looks up-to-date,
2134 * so it can be re-added without a resync.
2135 * Pointless because if there are any spares to skip,
2136 * then a recovery will happen and soon that array won't
2137 * be degraded any more and the spare can go back to sleep then.
2141 sync_req
= mddev
->in_sync
;
2143 /* If this is just a dirty<->clean transition, and the array is clean
2144 * and 'events' is odd, we can roll back to the previous clean state */
2146 && (mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
)
2147 && mddev
->can_decrease_events
2148 && mddev
->events
!= 1) {
2150 mddev
->can_decrease_events
= 0;
2152 /* otherwise we have to go forward and ... */
2154 mddev
->can_decrease_events
= nospares
;
2157 if (!mddev
->events
) {
2159 * oops, this 64-bit counter should never wrap.
2160 * Either we are in around ~1 trillion A.C., assuming
2161 * 1 reboot per second, or we have a bug:
2166 sync_sbs(mddev
, nospares
);
2167 spin_unlock_irq(&mddev
->write_lock
);
2170 "md: updating %s RAID superblock on device (in sync %d)\n",
2171 mdname(mddev
),mddev
->in_sync
);
2173 bitmap_update_sb(mddev
->bitmap
);
2174 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
2175 char b
[BDEVNAME_SIZE
];
2176 dprintk(KERN_INFO
"md: ");
2177 if (rdev
->sb_loaded
!= 1)
2178 continue; /* no noise on spare devices */
2179 if (test_bit(Faulty
, &rdev
->flags
))
2180 dprintk("(skipping faulty ");
2182 dprintk("%s ", bdevname(rdev
->bdev
,b
));
2183 if (!test_bit(Faulty
, &rdev
->flags
)) {
2184 md_super_write(mddev
,rdev
,
2185 rdev
->sb_start
, rdev
->sb_size
,
2187 dprintk(KERN_INFO
"(write) %s's sb offset: %llu\n",
2188 bdevname(rdev
->bdev
,b
),
2189 (unsigned long long)rdev
->sb_start
);
2190 rdev
->sb_events
= mddev
->events
;
2194 if (mddev
->level
== LEVEL_MULTIPATH
)
2195 /* only need to write one superblock... */
2198 md_super_wait(mddev
);
2199 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
2201 spin_lock_irq(&mddev
->write_lock
);
2202 if (mddev
->in_sync
!= sync_req
||
2203 test_bit(MD_CHANGE_DEVS
, &mddev
->flags
)) {
2204 /* have to write it out again */
2205 spin_unlock_irq(&mddev
->write_lock
);
2208 clear_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
2209 spin_unlock_irq(&mddev
->write_lock
);
2210 wake_up(&mddev
->sb_wait
);
2211 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
2212 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
2216 /* words written to sysfs files may, or may not, be \n terminated.
2217 * We want to accept with case. For this we use cmd_match.
2219 static int cmd_match(const char *cmd
, const char *str
)
2221 /* See if cmd, written into a sysfs file, matches
2222 * str. They must either be the same, or cmd can
2223 * have a trailing newline
2225 while (*cmd
&& *str
&& *cmd
== *str
) {
2236 struct rdev_sysfs_entry
{
2237 struct attribute attr
;
2238 ssize_t (*show
)(mdk_rdev_t
*, char *);
2239 ssize_t (*store
)(mdk_rdev_t
*, const char *, size_t);
2243 state_show(mdk_rdev_t
*rdev
, char *page
)
2248 if (test_bit(Faulty
, &rdev
->flags
)) {
2249 len
+= sprintf(page
+len
, "%sfaulty",sep
);
2252 if (test_bit(In_sync
, &rdev
->flags
)) {
2253 len
+= sprintf(page
+len
, "%sin_sync",sep
);
2256 if (test_bit(WriteMostly
, &rdev
->flags
)) {
2257 len
+= sprintf(page
+len
, "%swrite_mostly",sep
);
2260 if (test_bit(Blocked
, &rdev
->flags
)) {
2261 len
+= sprintf(page
+len
, "%sblocked", sep
);
2264 if (!test_bit(Faulty
, &rdev
->flags
) &&
2265 !test_bit(In_sync
, &rdev
->flags
)) {
2266 len
+= sprintf(page
+len
, "%sspare", sep
);
2269 return len
+sprintf(page
+len
, "\n");
2273 state_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2276 * faulty - simulates and error
2277 * remove - disconnects the device
2278 * writemostly - sets write_mostly
2279 * -writemostly - clears write_mostly
2280 * blocked - sets the Blocked flag
2281 * -blocked - clears the Blocked flag
2282 * insync - sets Insync providing device isn't active
2285 if (cmd_match(buf
, "faulty") && rdev
->mddev
->pers
) {
2286 md_error(rdev
->mddev
, rdev
);
2288 } else if (cmd_match(buf
, "remove")) {
2289 if (rdev
->raid_disk
>= 0)
2292 mddev_t
*mddev
= rdev
->mddev
;
2293 kick_rdev_from_array(rdev
);
2295 md_update_sb(mddev
, 1);
2296 md_new_event(mddev
);
2299 } else if (cmd_match(buf
, "writemostly")) {
2300 set_bit(WriteMostly
, &rdev
->flags
);
2302 } else if (cmd_match(buf
, "-writemostly")) {
2303 clear_bit(WriteMostly
, &rdev
->flags
);
2305 } else if (cmd_match(buf
, "blocked")) {
2306 set_bit(Blocked
, &rdev
->flags
);
2308 } else if (cmd_match(buf
, "-blocked")) {
2309 clear_bit(Blocked
, &rdev
->flags
);
2310 wake_up(&rdev
->blocked_wait
);
2311 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
2312 md_wakeup_thread(rdev
->mddev
->thread
);
2315 } else if (cmd_match(buf
, "insync") && rdev
->raid_disk
== -1) {
2316 set_bit(In_sync
, &rdev
->flags
);
2320 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
2321 return err
? err
: len
;
2323 static struct rdev_sysfs_entry rdev_state
=
2324 __ATTR(state
, S_IRUGO
|S_IWUSR
, state_show
, state_store
);
2327 errors_show(mdk_rdev_t
*rdev
, char *page
)
2329 return sprintf(page
, "%d\n", atomic_read(&rdev
->corrected_errors
));
2333 errors_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2336 unsigned long n
= simple_strtoul(buf
, &e
, 10);
2337 if (*buf
&& (*e
== 0 || *e
== '\n')) {
2338 atomic_set(&rdev
->corrected_errors
, n
);
2343 static struct rdev_sysfs_entry rdev_errors
=
2344 __ATTR(errors
, S_IRUGO
|S_IWUSR
, errors_show
, errors_store
);
2347 slot_show(mdk_rdev_t
*rdev
, char *page
)
2349 if (rdev
->raid_disk
< 0)
2350 return sprintf(page
, "none\n");
2352 return sprintf(page
, "%d\n", rdev
->raid_disk
);
2356 slot_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2361 int slot
= simple_strtoul(buf
, &e
, 10);
2362 if (strncmp(buf
, "none", 4)==0)
2364 else if (e
==buf
|| (*e
&& *e
!= '\n'))
2366 if (rdev
->mddev
->pers
&& slot
== -1) {
2367 /* Setting 'slot' on an active array requires also
2368 * updating the 'rd%d' link, and communicating
2369 * with the personality with ->hot_*_disk.
2370 * For now we only support removing
2371 * failed/spare devices. This normally happens automatically,
2372 * but not when the metadata is externally managed.
2374 if (rdev
->raid_disk
== -1)
2376 /* personality does all needed checks */
2377 if (rdev
->mddev
->pers
->hot_add_disk
== NULL
)
2379 err
= rdev
->mddev
->pers
->
2380 hot_remove_disk(rdev
->mddev
, rdev
->raid_disk
);
2383 sprintf(nm
, "rd%d", rdev
->raid_disk
);
2384 sysfs_remove_link(&rdev
->mddev
->kobj
, nm
);
2385 rdev
->raid_disk
= -1;
2386 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
2387 md_wakeup_thread(rdev
->mddev
->thread
);
2388 } else if (rdev
->mddev
->pers
) {
2390 /* Activating a spare .. or possibly reactivating
2391 * if we ever get bitmaps working here.
2394 if (rdev
->raid_disk
!= -1)
2397 if (rdev
->mddev
->pers
->hot_add_disk
== NULL
)
2400 list_for_each_entry(rdev2
, &rdev
->mddev
->disks
, same_set
)
2401 if (rdev2
->raid_disk
== slot
)
2404 rdev
->raid_disk
= slot
;
2405 if (test_bit(In_sync
, &rdev
->flags
))
2406 rdev
->saved_raid_disk
= slot
;
2408 rdev
->saved_raid_disk
= -1;
2409 err
= rdev
->mddev
->pers
->
2410 hot_add_disk(rdev
->mddev
, rdev
);
2412 rdev
->raid_disk
= -1;
2415 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
2416 sprintf(nm
, "rd%d", rdev
->raid_disk
);
2417 if (sysfs_create_link(&rdev
->mddev
->kobj
, &rdev
->kobj
, nm
))
2418 /* failure here is OK */;
2419 /* don't wakeup anyone, leave that to userspace. */
2421 if (slot
>= rdev
->mddev
->raid_disks
)
2423 rdev
->raid_disk
= slot
;
2424 /* assume it is working */
2425 clear_bit(Faulty
, &rdev
->flags
);
2426 clear_bit(WriteMostly
, &rdev
->flags
);
2427 set_bit(In_sync
, &rdev
->flags
);
2428 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
2434 static struct rdev_sysfs_entry rdev_slot
=
2435 __ATTR(slot
, S_IRUGO
|S_IWUSR
, slot_show
, slot_store
);
2438 offset_show(mdk_rdev_t
*rdev
, char *page
)
2440 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->data_offset
);
2444 offset_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2447 unsigned long long offset
= simple_strtoull(buf
, &e
, 10);
2448 if (e
==buf
|| (*e
&& *e
!= '\n'))
2450 if (rdev
->mddev
->pers
&& rdev
->raid_disk
>= 0)
2452 if (rdev
->sectors
&& rdev
->mddev
->external
)
2453 /* Must set offset before size, so overlap checks
2456 rdev
->data_offset
= offset
;
2460 static struct rdev_sysfs_entry rdev_offset
=
2461 __ATTR(offset
, S_IRUGO
|S_IWUSR
, offset_show
, offset_store
);
2464 rdev_size_show(mdk_rdev_t
*rdev
, char *page
)
2466 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->sectors
/ 2);
2469 static int overlaps(sector_t s1
, sector_t l1
, sector_t s2
, sector_t l2
)
2471 /* check if two start/length pairs overlap */
2479 static int strict_blocks_to_sectors(const char *buf
, sector_t
*sectors
)
2481 unsigned long long blocks
;
2484 if (strict_strtoull(buf
, 10, &blocks
) < 0)
2487 if (blocks
& 1ULL << (8 * sizeof(blocks
) - 1))
2488 return -EINVAL
; /* sector conversion overflow */
2491 if (new != blocks
* 2)
2492 return -EINVAL
; /* unsigned long long to sector_t overflow */
2499 rdev_size_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2501 mddev_t
*my_mddev
= rdev
->mddev
;
2502 sector_t oldsectors
= rdev
->sectors
;
2505 if (strict_blocks_to_sectors(buf
, §ors
) < 0)
2507 if (my_mddev
->pers
&& rdev
->raid_disk
>= 0) {
2508 if (my_mddev
->persistent
) {
2509 sectors
= super_types
[my_mddev
->major_version
].
2510 rdev_size_change(rdev
, sectors
);
2513 } else if (!sectors
)
2514 sectors
= (rdev
->bdev
->bd_inode
->i_size
>> 9) -
2517 if (sectors
< my_mddev
->dev_sectors
)
2518 return -EINVAL
; /* component must fit device */
2520 rdev
->sectors
= sectors
;
2521 if (sectors
> oldsectors
&& my_mddev
->external
) {
2522 /* need to check that all other rdevs with the same ->bdev
2523 * do not overlap. We need to unlock the mddev to avoid
2524 * a deadlock. We have already changed rdev->sectors, and if
2525 * we have to change it back, we will have the lock again.
2529 struct list_head
*tmp
;
2531 mddev_unlock(my_mddev
);
2532 for_each_mddev(mddev
, tmp
) {
2536 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
)
2537 if (test_bit(AllReserved
, &rdev2
->flags
) ||
2538 (rdev
->bdev
== rdev2
->bdev
&&
2540 overlaps(rdev
->data_offset
, rdev
->sectors
,
2546 mddev_unlock(mddev
);
2552 mddev_lock(my_mddev
);
2554 /* Someone else could have slipped in a size
2555 * change here, but doing so is just silly.
2556 * We put oldsectors back because we *know* it is
2557 * safe, and trust userspace not to race with
2560 rdev
->sectors
= oldsectors
;
2567 static struct rdev_sysfs_entry rdev_size
=
2568 __ATTR(size
, S_IRUGO
|S_IWUSR
, rdev_size_show
, rdev_size_store
);
2571 static ssize_t
recovery_start_show(mdk_rdev_t
*rdev
, char *page
)
2573 unsigned long long recovery_start
= rdev
->recovery_offset
;
2575 if (test_bit(In_sync
, &rdev
->flags
) ||
2576 recovery_start
== MaxSector
)
2577 return sprintf(page
, "none\n");
2579 return sprintf(page
, "%llu\n", recovery_start
);
2582 static ssize_t
recovery_start_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2584 unsigned long long recovery_start
;
2586 if (cmd_match(buf
, "none"))
2587 recovery_start
= MaxSector
;
2588 else if (strict_strtoull(buf
, 10, &recovery_start
))
2591 if (rdev
->mddev
->pers
&&
2592 rdev
->raid_disk
>= 0)
2595 rdev
->recovery_offset
= recovery_start
;
2596 if (recovery_start
== MaxSector
)
2597 set_bit(In_sync
, &rdev
->flags
);
2599 clear_bit(In_sync
, &rdev
->flags
);
2603 static struct rdev_sysfs_entry rdev_recovery_start
=
2604 __ATTR(recovery_start
, S_IRUGO
|S_IWUSR
, recovery_start_show
, recovery_start_store
);
2606 static struct attribute
*rdev_default_attrs
[] = {
2612 &rdev_recovery_start
.attr
,
2616 rdev_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
2618 struct rdev_sysfs_entry
*entry
= container_of(attr
, struct rdev_sysfs_entry
, attr
);
2619 mdk_rdev_t
*rdev
= container_of(kobj
, mdk_rdev_t
, kobj
);
2620 mddev_t
*mddev
= rdev
->mddev
;
2626 rv
= mddev
? mddev_lock(mddev
) : -EBUSY
;
2628 if (rdev
->mddev
== NULL
)
2631 rv
= entry
->show(rdev
, page
);
2632 mddev_unlock(mddev
);
2638 rdev_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
2639 const char *page
, size_t length
)
2641 struct rdev_sysfs_entry
*entry
= container_of(attr
, struct rdev_sysfs_entry
, attr
);
2642 mdk_rdev_t
*rdev
= container_of(kobj
, mdk_rdev_t
, kobj
);
2644 mddev_t
*mddev
= rdev
->mddev
;
2648 if (!capable(CAP_SYS_ADMIN
))
2650 rv
= mddev
? mddev_lock(mddev
): -EBUSY
;
2652 if (rdev
->mddev
== NULL
)
2655 rv
= entry
->store(rdev
, page
, length
);
2656 mddev_unlock(mddev
);
2661 static void rdev_free(struct kobject
*ko
)
2663 mdk_rdev_t
*rdev
= container_of(ko
, mdk_rdev_t
, kobj
);
2666 static const struct sysfs_ops rdev_sysfs_ops
= {
2667 .show
= rdev_attr_show
,
2668 .store
= rdev_attr_store
,
2670 static struct kobj_type rdev_ktype
= {
2671 .release
= rdev_free
,
2672 .sysfs_ops
= &rdev_sysfs_ops
,
2673 .default_attrs
= rdev_default_attrs
,
2676 void md_rdev_init(mdk_rdev_t
*rdev
)
2679 rdev
->saved_raid_disk
= -1;
2680 rdev
->raid_disk
= -1;
2682 rdev
->data_offset
= 0;
2683 rdev
->sb_events
= 0;
2684 rdev
->last_read_error
.tv_sec
= 0;
2685 rdev
->last_read_error
.tv_nsec
= 0;
2686 atomic_set(&rdev
->nr_pending
, 0);
2687 atomic_set(&rdev
->read_errors
, 0);
2688 atomic_set(&rdev
->corrected_errors
, 0);
2690 INIT_LIST_HEAD(&rdev
->same_set
);
2691 init_waitqueue_head(&rdev
->blocked_wait
);
2693 EXPORT_SYMBOL_GPL(md_rdev_init
);
2695 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2697 * mark the device faulty if:
2699 * - the device is nonexistent (zero size)
2700 * - the device has no valid superblock
2702 * a faulty rdev _never_ has rdev->sb set.
2704 static mdk_rdev_t
*md_import_device(dev_t newdev
, int super_format
, int super_minor
)
2706 char b
[BDEVNAME_SIZE
];
2711 rdev
= kzalloc(sizeof(*rdev
), GFP_KERNEL
);
2713 printk(KERN_ERR
"md: could not alloc mem for new device!\n");
2714 return ERR_PTR(-ENOMEM
);
2718 if ((err
= alloc_disk_sb(rdev
)))
2721 err
= lock_rdev(rdev
, newdev
, super_format
== -2);
2725 kobject_init(&rdev
->kobj
, &rdev_ktype
);
2727 size
= rdev
->bdev
->bd_inode
->i_size
>> BLOCK_SIZE_BITS
;
2730 "md: %s has zero or unknown size, marking faulty!\n",
2731 bdevname(rdev
->bdev
,b
));
2736 if (super_format
>= 0) {
2737 err
= super_types
[super_format
].
2738 load_super(rdev
, NULL
, super_minor
);
2739 if (err
== -EINVAL
) {
2741 "md: %s does not have a valid v%d.%d "
2742 "superblock, not importing!\n",
2743 bdevname(rdev
->bdev
,b
),
2744 super_format
, super_minor
);
2749 "md: could not read %s's sb, not importing!\n",
2750 bdevname(rdev
->bdev
,b
));
2758 if (rdev
->sb_page
) {
2764 return ERR_PTR(err
);
2768 * Check a full RAID array for plausibility
2772 static void analyze_sbs(mddev_t
* mddev
)
2775 mdk_rdev_t
*rdev
, *freshest
, *tmp
;
2776 char b
[BDEVNAME_SIZE
];
2779 rdev_for_each(rdev
, tmp
, mddev
)
2780 switch (super_types
[mddev
->major_version
].
2781 load_super(rdev
, freshest
, mddev
->minor_version
)) {
2789 "md: fatal superblock inconsistency in %s"
2790 " -- removing from array\n",
2791 bdevname(rdev
->bdev
,b
));
2792 kick_rdev_from_array(rdev
);
2796 super_types
[mddev
->major_version
].
2797 validate_super(mddev
, freshest
);
2800 rdev_for_each(rdev
, tmp
, mddev
) {
2801 if (mddev
->max_disks
&&
2802 (rdev
->desc_nr
>= mddev
->max_disks
||
2803 i
> mddev
->max_disks
)) {
2805 "md: %s: %s: only %d devices permitted\n",
2806 mdname(mddev
), bdevname(rdev
->bdev
, b
),
2808 kick_rdev_from_array(rdev
);
2811 if (rdev
!= freshest
)
2812 if (super_types
[mddev
->major_version
].
2813 validate_super(mddev
, rdev
)) {
2814 printk(KERN_WARNING
"md: kicking non-fresh %s"
2816 bdevname(rdev
->bdev
,b
));
2817 kick_rdev_from_array(rdev
);
2820 if (mddev
->level
== LEVEL_MULTIPATH
) {
2821 rdev
->desc_nr
= i
++;
2822 rdev
->raid_disk
= rdev
->desc_nr
;
2823 set_bit(In_sync
, &rdev
->flags
);
2824 } else if (rdev
->raid_disk
>= (mddev
->raid_disks
- min(0, mddev
->delta_disks
))) {
2825 rdev
->raid_disk
= -1;
2826 clear_bit(In_sync
, &rdev
->flags
);
2831 /* Read a fixed-point number.
2832 * Numbers in sysfs attributes should be in "standard" units where
2833 * possible, so time should be in seconds.
2834 * However we internally use a a much smaller unit such as
2835 * milliseconds or jiffies.
2836 * This function takes a decimal number with a possible fractional
2837 * component, and produces an integer which is the result of
2838 * multiplying that number by 10^'scale'.
2839 * all without any floating-point arithmetic.
2841 int strict_strtoul_scaled(const char *cp
, unsigned long *res
, int scale
)
2843 unsigned long result
= 0;
2845 while (isdigit(*cp
) || (*cp
== '.' && decimals
< 0)) {
2848 else if (decimals
< scale
) {
2851 result
= result
* 10 + value
;
2863 while (decimals
< scale
) {
2872 static void md_safemode_timeout(unsigned long data
);
2875 safe_delay_show(mddev_t
*mddev
, char *page
)
2877 int msec
= (mddev
->safemode_delay
*1000)/HZ
;
2878 return sprintf(page
, "%d.%03d\n", msec
/1000, msec
%1000);
2881 safe_delay_store(mddev_t
*mddev
, const char *cbuf
, size_t len
)
2885 if (strict_strtoul_scaled(cbuf
, &msec
, 3) < 0)
2888 mddev
->safemode_delay
= 0;
2890 unsigned long old_delay
= mddev
->safemode_delay
;
2891 mddev
->safemode_delay
= (msec
*HZ
)/1000;
2892 if (mddev
->safemode_delay
== 0)
2893 mddev
->safemode_delay
= 1;
2894 if (mddev
->safemode_delay
< old_delay
)
2895 md_safemode_timeout((unsigned long)mddev
);
2899 static struct md_sysfs_entry md_safe_delay
=
2900 __ATTR(safe_mode_delay
, S_IRUGO
|S_IWUSR
,safe_delay_show
, safe_delay_store
);
2903 level_show(mddev_t
*mddev
, char *page
)
2905 struct mdk_personality
*p
= mddev
->pers
;
2907 return sprintf(page
, "%s\n", p
->name
);
2908 else if (mddev
->clevel
[0])
2909 return sprintf(page
, "%s\n", mddev
->clevel
);
2910 else if (mddev
->level
!= LEVEL_NONE
)
2911 return sprintf(page
, "%d\n", mddev
->level
);
2917 level_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2921 struct mdk_personality
*pers
;
2926 if (mddev
->pers
== NULL
) {
2929 if (len
>= sizeof(mddev
->clevel
))
2931 strncpy(mddev
->clevel
, buf
, len
);
2932 if (mddev
->clevel
[len
-1] == '\n')
2934 mddev
->clevel
[len
] = 0;
2935 mddev
->level
= LEVEL_NONE
;
2939 /* request to change the personality. Need to ensure:
2940 * - array is not engaged in resync/recovery/reshape
2941 * - old personality can be suspended
2942 * - new personality will access other array.
2945 if (mddev
->sync_thread
||
2946 mddev
->reshape_position
!= MaxSector
||
2947 mddev
->sysfs_active
)
2950 if (!mddev
->pers
->quiesce
) {
2951 printk(KERN_WARNING
"md: %s: %s does not support online personality change\n",
2952 mdname(mddev
), mddev
->pers
->name
);
2956 /* Now find the new personality */
2957 if (len
== 0 || len
>= sizeof(clevel
))
2959 strncpy(clevel
, buf
, len
);
2960 if (clevel
[len
-1] == '\n')
2963 if (strict_strtol(clevel
, 10, &level
))
2966 if (request_module("md-%s", clevel
) != 0)
2967 request_module("md-level-%s", clevel
);
2968 spin_lock(&pers_lock
);
2969 pers
= find_pers(level
, clevel
);
2970 if (!pers
|| !try_module_get(pers
->owner
)) {
2971 spin_unlock(&pers_lock
);
2972 printk(KERN_WARNING
"md: personality %s not loaded\n", clevel
);
2975 spin_unlock(&pers_lock
);
2977 if (pers
== mddev
->pers
) {
2978 /* Nothing to do! */
2979 module_put(pers
->owner
);
2982 if (!pers
->takeover
) {
2983 module_put(pers
->owner
);
2984 printk(KERN_WARNING
"md: %s: %s does not support personality takeover\n",
2985 mdname(mddev
), clevel
);
2989 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
2990 rdev
->new_raid_disk
= rdev
->raid_disk
;
2992 /* ->takeover must set new_* and/or delta_disks
2993 * if it succeeds, and may set them when it fails.
2995 priv
= pers
->takeover(mddev
);
2997 mddev
->new_level
= mddev
->level
;
2998 mddev
->new_layout
= mddev
->layout
;
2999 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
3000 mddev
->raid_disks
-= mddev
->delta_disks
;
3001 mddev
->delta_disks
= 0;
3002 module_put(pers
->owner
);
3003 printk(KERN_WARNING
"md: %s: %s would not accept array\n",
3004 mdname(mddev
), clevel
);
3005 return PTR_ERR(priv
);
3008 /* Looks like we have a winner */
3009 mddev_suspend(mddev
);
3010 mddev
->pers
->stop(mddev
);
3012 if (mddev
->pers
->sync_request
== NULL
&&
3013 pers
->sync_request
!= NULL
) {
3014 /* need to add the md_redundancy_group */
3015 if (sysfs_create_group(&mddev
->kobj
, &md_redundancy_group
))
3017 "md: cannot register extra attributes for %s\n",
3019 mddev
->sysfs_action
= sysfs_get_dirent(mddev
->kobj
.sd
, NULL
, "sync_action");
3021 if (mddev
->pers
->sync_request
!= NULL
&&
3022 pers
->sync_request
== NULL
) {
3023 /* need to remove the md_redundancy_group */
3024 if (mddev
->to_remove
== NULL
)
3025 mddev
->to_remove
= &md_redundancy_group
;
3028 if (mddev
->pers
->sync_request
== NULL
&&
3030 /* We are converting from a no-redundancy array
3031 * to a redundancy array and metadata is managed
3032 * externally so we need to be sure that writes
3033 * won't block due to a need to transition
3035 * until external management is started.
3038 mddev
->safemode_delay
= 0;
3039 mddev
->safemode
= 0;
3042 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
3044 if (rdev
->raid_disk
< 0)
3046 if (rdev
->new_raid_disk
> mddev
->raid_disks
)
3047 rdev
->new_raid_disk
= -1;
3048 if (rdev
->new_raid_disk
== rdev
->raid_disk
)
3050 sprintf(nm
, "rd%d", rdev
->raid_disk
);
3051 sysfs_remove_link(&mddev
->kobj
, nm
);
3053 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
3054 if (rdev
->raid_disk
< 0)
3056 if (rdev
->new_raid_disk
== rdev
->raid_disk
)
3058 rdev
->raid_disk
= rdev
->new_raid_disk
;
3059 if (rdev
->raid_disk
< 0)
3060 clear_bit(In_sync
, &rdev
->flags
);
3063 sprintf(nm
, "rd%d", rdev
->raid_disk
);
3064 if(sysfs_create_link(&mddev
->kobj
, &rdev
->kobj
, nm
))
3065 printk("md: cannot register %s for %s after level change\n",
3070 module_put(mddev
->pers
->owner
);
3072 mddev
->private = priv
;
3073 strlcpy(mddev
->clevel
, pers
->name
, sizeof(mddev
->clevel
));
3074 mddev
->level
= mddev
->new_level
;
3075 mddev
->layout
= mddev
->new_layout
;
3076 mddev
->chunk_sectors
= mddev
->new_chunk_sectors
;
3077 mddev
->delta_disks
= 0;
3078 if (mddev
->pers
->sync_request
== NULL
) {
3079 /* this is now an array without redundancy, so
3080 * it must always be in_sync
3083 del_timer_sync(&mddev
->safemode_timer
);
3086 mddev_resume(mddev
);
3087 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
3088 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3089 md_wakeup_thread(mddev
->thread
);
3090 sysfs_notify(&mddev
->kobj
, NULL
, "level");
3091 md_new_event(mddev
);
3095 static struct md_sysfs_entry md_level
=
3096 __ATTR(level
, S_IRUGO
|S_IWUSR
, level_show
, level_store
);
3100 layout_show(mddev_t
*mddev
, char *page
)
3102 /* just a number, not meaningful for all levels */
3103 if (mddev
->reshape_position
!= MaxSector
&&
3104 mddev
->layout
!= mddev
->new_layout
)
3105 return sprintf(page
, "%d (%d)\n",
3106 mddev
->new_layout
, mddev
->layout
);
3107 return sprintf(page
, "%d\n", mddev
->layout
);
3111 layout_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3114 unsigned long n
= simple_strtoul(buf
, &e
, 10);
3116 if (!*buf
|| (*e
&& *e
!= '\n'))
3121 if (mddev
->pers
->check_reshape
== NULL
)
3123 mddev
->new_layout
= n
;
3124 err
= mddev
->pers
->check_reshape(mddev
);
3126 mddev
->new_layout
= mddev
->layout
;
3130 mddev
->new_layout
= n
;
3131 if (mddev
->reshape_position
== MaxSector
)
3136 static struct md_sysfs_entry md_layout
=
3137 __ATTR(layout
, S_IRUGO
|S_IWUSR
, layout_show
, layout_store
);
3141 raid_disks_show(mddev_t
*mddev
, char *page
)
3143 if (mddev
->raid_disks
== 0)
3145 if (mddev
->reshape_position
!= MaxSector
&&
3146 mddev
->delta_disks
!= 0)
3147 return sprintf(page
, "%d (%d)\n", mddev
->raid_disks
,
3148 mddev
->raid_disks
- mddev
->delta_disks
);
3149 return sprintf(page
, "%d\n", mddev
->raid_disks
);
3152 static int update_raid_disks(mddev_t
*mddev
, int raid_disks
);
3155 raid_disks_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3159 unsigned long n
= simple_strtoul(buf
, &e
, 10);
3161 if (!*buf
|| (*e
&& *e
!= '\n'))
3165 rv
= update_raid_disks(mddev
, n
);
3166 else if (mddev
->reshape_position
!= MaxSector
) {
3167 int olddisks
= mddev
->raid_disks
- mddev
->delta_disks
;
3168 mddev
->delta_disks
= n
- olddisks
;
3169 mddev
->raid_disks
= n
;
3171 mddev
->raid_disks
= n
;
3172 return rv
? rv
: len
;
3174 static struct md_sysfs_entry md_raid_disks
=
3175 __ATTR(raid_disks
, S_IRUGO
|S_IWUSR
, raid_disks_show
, raid_disks_store
);
3178 chunk_size_show(mddev_t
*mddev
, char *page
)
3180 if (mddev
->reshape_position
!= MaxSector
&&
3181 mddev
->chunk_sectors
!= mddev
->new_chunk_sectors
)
3182 return sprintf(page
, "%d (%d)\n",
3183 mddev
->new_chunk_sectors
<< 9,
3184 mddev
->chunk_sectors
<< 9);
3185 return sprintf(page
, "%d\n", mddev
->chunk_sectors
<< 9);
3189 chunk_size_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3192 unsigned long n
= simple_strtoul(buf
, &e
, 10);
3194 if (!*buf
|| (*e
&& *e
!= '\n'))
3199 if (mddev
->pers
->check_reshape
== NULL
)
3201 mddev
->new_chunk_sectors
= n
>> 9;
3202 err
= mddev
->pers
->check_reshape(mddev
);
3204 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
3208 mddev
->new_chunk_sectors
= n
>> 9;
3209 if (mddev
->reshape_position
== MaxSector
)
3210 mddev
->chunk_sectors
= n
>> 9;
3214 static struct md_sysfs_entry md_chunk_size
=
3215 __ATTR(chunk_size
, S_IRUGO
|S_IWUSR
, chunk_size_show
, chunk_size_store
);
3218 resync_start_show(mddev_t
*mddev
, char *page
)
3220 if (mddev
->recovery_cp
== MaxSector
)
3221 return sprintf(page
, "none\n");
3222 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->recovery_cp
);
3226 resync_start_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3229 unsigned long long n
= simple_strtoull(buf
, &e
, 10);
3233 if (cmd_match(buf
, "none"))
3235 else if (!*buf
|| (*e
&& *e
!= '\n'))
3238 mddev
->recovery_cp
= n
;
3241 static struct md_sysfs_entry md_resync_start
=
3242 __ATTR(resync_start
, S_IRUGO
|S_IWUSR
, resync_start_show
, resync_start_store
);
3245 * The array state can be:
3248 * No devices, no size, no level
3249 * Equivalent to STOP_ARRAY ioctl
3251 * May have some settings, but array is not active
3252 * all IO results in error
3253 * When written, doesn't tear down array, but just stops it
3254 * suspended (not supported yet)
3255 * All IO requests will block. The array can be reconfigured.
3256 * Writing this, if accepted, will block until array is quiescent
3258 * no resync can happen. no superblocks get written.
3259 * write requests fail
3261 * like readonly, but behaves like 'clean' on a write request.
3263 * clean - no pending writes, but otherwise active.
3264 * When written to inactive array, starts without resync
3265 * If a write request arrives then
3266 * if metadata is known, mark 'dirty' and switch to 'active'.
3267 * if not known, block and switch to write-pending
3268 * If written to an active array that has pending writes, then fails.
3270 * fully active: IO and resync can be happening.
3271 * When written to inactive array, starts with resync
3274 * clean, but writes are blocked waiting for 'active' to be written.
3277 * like active, but no writes have been seen for a while (100msec).
3280 enum array_state
{ clear
, inactive
, suspended
, readonly
, read_auto
, clean
, active
,
3281 write_pending
, active_idle
, bad_word
};
3282 static char *array_states
[] = {
3283 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3284 "write-pending", "active-idle", NULL
};
3286 static int match_word(const char *word
, char **list
)
3289 for (n
=0; list
[n
]; n
++)
3290 if (cmd_match(word
, list
[n
]))
3296 array_state_show(mddev_t
*mddev
, char *page
)
3298 enum array_state st
= inactive
;
3311 else if (test_bit(MD_CHANGE_PENDING
, &mddev
->flags
))
3313 else if (mddev
->safemode
)
3319 if (list_empty(&mddev
->disks
) &&
3320 mddev
->raid_disks
== 0 &&
3321 mddev
->dev_sectors
== 0)
3326 return sprintf(page
, "%s\n", array_states
[st
]);
3329 static int do_md_stop(mddev_t
* mddev
, int ro
, int is_open
);
3330 static int md_set_readonly(mddev_t
* mddev
, int is_open
);
3331 static int do_md_run(mddev_t
* mddev
);
3332 static int restart_array(mddev_t
*mddev
);
3335 array_state_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3338 enum array_state st
= match_word(buf
, array_states
);
3343 /* stopping an active array */
3344 if (atomic_read(&mddev
->openers
) > 0)
3346 err
= do_md_stop(mddev
, 0, 0);
3349 /* stopping an active array */
3351 if (atomic_read(&mddev
->openers
) > 0)
3353 err
= do_md_stop(mddev
, 2, 0);
3355 err
= 0; /* already inactive */
3358 break; /* not supported yet */
3361 err
= md_set_readonly(mddev
, 0);
3364 set_disk_ro(mddev
->gendisk
, 1);
3365 err
= do_md_run(mddev
);
3371 err
= md_set_readonly(mddev
, 0);
3372 else if (mddev
->ro
== 1)
3373 err
= restart_array(mddev
);
3376 set_disk_ro(mddev
->gendisk
, 0);
3380 err
= do_md_run(mddev
);
3385 restart_array(mddev
);
3386 spin_lock_irq(&mddev
->write_lock
);
3387 if (atomic_read(&mddev
->writes_pending
) == 0) {
3388 if (mddev
->in_sync
== 0) {
3390 if (mddev
->safemode
== 1)
3391 mddev
->safemode
= 0;
3392 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
3397 spin_unlock_irq(&mddev
->write_lock
);
3403 restart_array(mddev
);
3404 clear_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
3405 wake_up(&mddev
->sb_wait
);
3409 set_disk_ro(mddev
->gendisk
, 0);
3410 err
= do_md_run(mddev
);
3415 /* these cannot be set */
3421 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
3425 static struct md_sysfs_entry md_array_state
=
3426 __ATTR(array_state
, S_IRUGO
|S_IWUSR
, array_state_show
, array_state_store
);
3429 max_corrected_read_errors_show(mddev_t
*mddev
, char *page
) {
3430 return sprintf(page
, "%d\n",
3431 atomic_read(&mddev
->max_corr_read_errors
));
3435 max_corrected_read_errors_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3438 unsigned long n
= simple_strtoul(buf
, &e
, 10);
3440 if (*buf
&& (*e
== 0 || *e
== '\n')) {
3441 atomic_set(&mddev
->max_corr_read_errors
, n
);
3447 static struct md_sysfs_entry max_corr_read_errors
=
3448 __ATTR(max_read_errors
, S_IRUGO
|S_IWUSR
, max_corrected_read_errors_show
,
3449 max_corrected_read_errors_store
);
3452 null_show(mddev_t
*mddev
, char *page
)
3458 new_dev_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3460 /* buf must be %d:%d\n? giving major and minor numbers */
3461 /* The new device is added to the array.
3462 * If the array has a persistent superblock, we read the
3463 * superblock to initialise info and check validity.
3464 * Otherwise, only checking done is that in bind_rdev_to_array,
3465 * which mainly checks size.
3468 int major
= simple_strtoul(buf
, &e
, 10);
3474 if (!*buf
|| *e
!= ':' || !e
[1] || e
[1] == '\n')
3476 minor
= simple_strtoul(e
+1, &e
, 10);
3477 if (*e
&& *e
!= '\n')
3479 dev
= MKDEV(major
, minor
);
3480 if (major
!= MAJOR(dev
) ||
3481 minor
!= MINOR(dev
))
3485 if (mddev
->persistent
) {
3486 rdev
= md_import_device(dev
, mddev
->major_version
,
3487 mddev
->minor_version
);
3488 if (!IS_ERR(rdev
) && !list_empty(&mddev
->disks
)) {
3489 mdk_rdev_t
*rdev0
= list_entry(mddev
->disks
.next
,
3490 mdk_rdev_t
, same_set
);
3491 err
= super_types
[mddev
->major_version
]
3492 .load_super(rdev
, rdev0
, mddev
->minor_version
);
3496 } else if (mddev
->external
)
3497 rdev
= md_import_device(dev
, -2, -1);
3499 rdev
= md_import_device(dev
, -1, -1);
3502 return PTR_ERR(rdev
);
3503 err
= bind_rdev_to_array(rdev
, mddev
);
3507 return err
? err
: len
;
3510 static struct md_sysfs_entry md_new_device
=
3511 __ATTR(new_dev
, S_IWUSR
, null_show
, new_dev_store
);
3514 bitmap_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3517 unsigned long chunk
, end_chunk
;
3521 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3523 chunk
= end_chunk
= simple_strtoul(buf
, &end
, 0);
3524 if (buf
== end
) break;
3525 if (*end
== '-') { /* range */
3527 end_chunk
= simple_strtoul(buf
, &end
, 0);
3528 if (buf
== end
) break;
3530 if (*end
&& !isspace(*end
)) break;
3531 bitmap_dirty_bits(mddev
->bitmap
, chunk
, end_chunk
);
3532 buf
= skip_spaces(end
);
3534 bitmap_unplug(mddev
->bitmap
); /* flush the bits to disk */
3539 static struct md_sysfs_entry md_bitmap
=
3540 __ATTR(bitmap_set_bits
, S_IWUSR
, null_show
, bitmap_store
);
3543 size_show(mddev_t
*mddev
, char *page
)
3545 return sprintf(page
, "%llu\n",
3546 (unsigned long long)mddev
->dev_sectors
/ 2);
3549 static int update_size(mddev_t
*mddev
, sector_t num_sectors
);
3552 size_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3554 /* If array is inactive, we can reduce the component size, but
3555 * not increase it (except from 0).
3556 * If array is active, we can try an on-line resize
3559 int err
= strict_blocks_to_sectors(buf
, §ors
);
3564 err
= update_size(mddev
, sectors
);
3565 md_update_sb(mddev
, 1);
3567 if (mddev
->dev_sectors
== 0 ||
3568 mddev
->dev_sectors
> sectors
)
3569 mddev
->dev_sectors
= sectors
;
3573 return err
? err
: len
;
3576 static struct md_sysfs_entry md_size
=
3577 __ATTR(component_size
, S_IRUGO
|S_IWUSR
, size_show
, size_store
);
3582 * 'none' for arrays with no metadata (good luck...)
3583 * 'external' for arrays with externally managed metadata,
3584 * or N.M for internally known formats
3587 metadata_show(mddev_t
*mddev
, char *page
)
3589 if (mddev
->persistent
)
3590 return sprintf(page
, "%d.%d\n",
3591 mddev
->major_version
, mddev
->minor_version
);
3592 else if (mddev
->external
)
3593 return sprintf(page
, "external:%s\n", mddev
->metadata_type
);
3595 return sprintf(page
, "none\n");
3599 metadata_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3603 /* Changing the details of 'external' metadata is
3604 * always permitted. Otherwise there must be
3605 * no devices attached to the array.
3607 if (mddev
->external
&& strncmp(buf
, "external:", 9) == 0)
3609 else if (!list_empty(&mddev
->disks
))
3612 if (cmd_match(buf
, "none")) {
3613 mddev
->persistent
= 0;
3614 mddev
->external
= 0;
3615 mddev
->major_version
= 0;
3616 mddev
->minor_version
= 90;
3619 if (strncmp(buf
, "external:", 9) == 0) {
3620 size_t namelen
= len
-9;
3621 if (namelen
>= sizeof(mddev
->metadata_type
))
3622 namelen
= sizeof(mddev
->metadata_type
)-1;
3623 strncpy(mddev
->metadata_type
, buf
+9, namelen
);
3624 mddev
->metadata_type
[namelen
] = 0;
3625 if (namelen
&& mddev
->metadata_type
[namelen
-1] == '\n')
3626 mddev
->metadata_type
[--namelen
] = 0;
3627 mddev
->persistent
= 0;
3628 mddev
->external
= 1;
3629 mddev
->major_version
= 0;
3630 mddev
->minor_version
= 90;
3633 major
= simple_strtoul(buf
, &e
, 10);
3634 if (e
==buf
|| *e
!= '.')
3637 minor
= simple_strtoul(buf
, &e
, 10);
3638 if (e
==buf
|| (*e
&& *e
!= '\n') )
3640 if (major
>= ARRAY_SIZE(super_types
) || super_types
[major
].name
== NULL
)
3642 mddev
->major_version
= major
;
3643 mddev
->minor_version
= minor
;
3644 mddev
->persistent
= 1;
3645 mddev
->external
= 0;
3649 static struct md_sysfs_entry md_metadata
=
3650 __ATTR(metadata_version
, S_IRUGO
|S_IWUSR
, metadata_show
, metadata_store
);
3653 action_show(mddev_t
*mddev
, char *page
)
3655 char *type
= "idle";
3656 if (test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
))
3658 else if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
3659 (!mddev
->ro
&& test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))) {
3660 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
3662 else if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
3663 if (!test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
3665 else if (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
))
3669 } else if (test_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
))
3672 return sprintf(page
, "%s\n", type
);
3676 action_store(mddev_t
*mddev
, const char *page
, size_t len
)
3678 if (!mddev
->pers
|| !mddev
->pers
->sync_request
)
3681 if (cmd_match(page
, "frozen"))
3682 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
3684 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
3686 if (cmd_match(page
, "idle") || cmd_match(page
, "frozen")) {
3687 if (mddev
->sync_thread
) {
3688 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
3689 md_unregister_thread(mddev
->sync_thread
);
3690 mddev
->sync_thread
= NULL
;
3691 mddev
->recovery
= 0;
3693 } else if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
3694 test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))
3696 else if (cmd_match(page
, "resync"))
3697 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3698 else if (cmd_match(page
, "recover")) {
3699 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
3700 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3701 } else if (cmd_match(page
, "reshape")) {
3703 if (mddev
->pers
->start_reshape
== NULL
)
3705 err
= mddev
->pers
->start_reshape(mddev
);
3708 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
3710 if (cmd_match(page
, "check"))
3711 set_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
3712 else if (!cmd_match(page
, "repair"))
3714 set_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
3715 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
3717 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3718 md_wakeup_thread(mddev
->thread
);
3719 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
3724 mismatch_cnt_show(mddev_t
*mddev
, char *page
)
3726 return sprintf(page
, "%llu\n",
3727 (unsigned long long) mddev
->resync_mismatches
);
3730 static struct md_sysfs_entry md_scan_mode
=
3731 __ATTR(sync_action
, S_IRUGO
|S_IWUSR
, action_show
, action_store
);
3734 static struct md_sysfs_entry md_mismatches
= __ATTR_RO(mismatch_cnt
);
3737 sync_min_show(mddev_t
*mddev
, char *page
)
3739 return sprintf(page
, "%d (%s)\n", speed_min(mddev
),
3740 mddev
->sync_speed_min
? "local": "system");
3744 sync_min_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3748 if (strncmp(buf
, "system", 6)==0) {
3749 mddev
->sync_speed_min
= 0;
3752 min
= simple_strtoul(buf
, &e
, 10);
3753 if (buf
== e
|| (*e
&& *e
!= '\n') || min
<= 0)
3755 mddev
->sync_speed_min
= min
;
3759 static struct md_sysfs_entry md_sync_min
=
3760 __ATTR(sync_speed_min
, S_IRUGO
|S_IWUSR
, sync_min_show
, sync_min_store
);
3763 sync_max_show(mddev_t
*mddev
, char *page
)
3765 return sprintf(page
, "%d (%s)\n", speed_max(mddev
),
3766 mddev
->sync_speed_max
? "local": "system");
3770 sync_max_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3774 if (strncmp(buf
, "system", 6)==0) {
3775 mddev
->sync_speed_max
= 0;
3778 max
= simple_strtoul(buf
, &e
, 10);
3779 if (buf
== e
|| (*e
&& *e
!= '\n') || max
<= 0)
3781 mddev
->sync_speed_max
= max
;
3785 static struct md_sysfs_entry md_sync_max
=
3786 __ATTR(sync_speed_max
, S_IRUGO
|S_IWUSR
, sync_max_show
, sync_max_store
);
3789 degraded_show(mddev_t
*mddev
, char *page
)
3791 return sprintf(page
, "%d\n", mddev
->degraded
);
3793 static struct md_sysfs_entry md_degraded
= __ATTR_RO(degraded
);
3796 sync_force_parallel_show(mddev_t
*mddev
, char *page
)
3798 return sprintf(page
, "%d\n", mddev
->parallel_resync
);
3802 sync_force_parallel_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3806 if (strict_strtol(buf
, 10, &n
))
3809 if (n
!= 0 && n
!= 1)
3812 mddev
->parallel_resync
= n
;
3814 if (mddev
->sync_thread
)
3815 wake_up(&resync_wait
);
3820 /* force parallel resync, even with shared block devices */
3821 static struct md_sysfs_entry md_sync_force_parallel
=
3822 __ATTR(sync_force_parallel
, S_IRUGO
|S_IWUSR
,
3823 sync_force_parallel_show
, sync_force_parallel_store
);
3826 sync_speed_show(mddev_t
*mddev
, char *page
)
3828 unsigned long resync
, dt
, db
;
3829 if (mddev
->curr_resync
== 0)
3830 return sprintf(page
, "none\n");
3831 resync
= mddev
->curr_mark_cnt
- atomic_read(&mddev
->recovery_active
);
3832 dt
= (jiffies
- mddev
->resync_mark
) / HZ
;
3834 db
= resync
- mddev
->resync_mark_cnt
;
3835 return sprintf(page
, "%lu\n", db
/dt
/2); /* K/sec */
3838 static struct md_sysfs_entry md_sync_speed
= __ATTR_RO(sync_speed
);
3841 sync_completed_show(mddev_t
*mddev
, char *page
)
3843 unsigned long max_sectors
, resync
;
3845 if (!test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
3846 return sprintf(page
, "none\n");
3848 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
3849 max_sectors
= mddev
->resync_max_sectors
;
3851 max_sectors
= mddev
->dev_sectors
;
3853 resync
= mddev
->curr_resync_completed
;
3854 return sprintf(page
, "%lu / %lu\n", resync
, max_sectors
);
3857 static struct md_sysfs_entry md_sync_completed
= __ATTR_RO(sync_completed
);
3860 min_sync_show(mddev_t
*mddev
, char *page
)
3862 return sprintf(page
, "%llu\n",
3863 (unsigned long long)mddev
->resync_min
);
3866 min_sync_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3868 unsigned long long min
;
3869 if (strict_strtoull(buf
, 10, &min
))
3871 if (min
> mddev
->resync_max
)
3873 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
3876 /* Must be a multiple of chunk_size */
3877 if (mddev
->chunk_sectors
) {
3878 sector_t temp
= min
;
3879 if (sector_div(temp
, mddev
->chunk_sectors
))
3882 mddev
->resync_min
= min
;
3887 static struct md_sysfs_entry md_min_sync
=
3888 __ATTR(sync_min
, S_IRUGO
|S_IWUSR
, min_sync_show
, min_sync_store
);
3891 max_sync_show(mddev_t
*mddev
, char *page
)
3893 if (mddev
->resync_max
== MaxSector
)
3894 return sprintf(page
, "max\n");
3896 return sprintf(page
, "%llu\n",
3897 (unsigned long long)mddev
->resync_max
);
3900 max_sync_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3902 if (strncmp(buf
, "max", 3) == 0)
3903 mddev
->resync_max
= MaxSector
;
3905 unsigned long long max
;
3906 if (strict_strtoull(buf
, 10, &max
))
3908 if (max
< mddev
->resync_min
)
3910 if (max
< mddev
->resync_max
&&
3912 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
3915 /* Must be a multiple of chunk_size */
3916 if (mddev
->chunk_sectors
) {
3917 sector_t temp
= max
;
3918 if (sector_div(temp
, mddev
->chunk_sectors
))
3921 mddev
->resync_max
= max
;
3923 wake_up(&mddev
->recovery_wait
);
3927 static struct md_sysfs_entry md_max_sync
=
3928 __ATTR(sync_max
, S_IRUGO
|S_IWUSR
, max_sync_show
, max_sync_store
);
3931 suspend_lo_show(mddev_t
*mddev
, char *page
)
3933 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->suspend_lo
);
3937 suspend_lo_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3940 unsigned long long new = simple_strtoull(buf
, &e
, 10);
3942 if (mddev
->pers
== NULL
||
3943 mddev
->pers
->quiesce
== NULL
)
3945 if (buf
== e
|| (*e
&& *e
!= '\n'))
3947 if (new >= mddev
->suspend_hi
||
3948 (new > mddev
->suspend_lo
&& new < mddev
->suspend_hi
)) {
3949 mddev
->suspend_lo
= new;
3950 mddev
->pers
->quiesce(mddev
, 2);
3955 static struct md_sysfs_entry md_suspend_lo
=
3956 __ATTR(suspend_lo
, S_IRUGO
|S_IWUSR
, suspend_lo_show
, suspend_lo_store
);
3960 suspend_hi_show(mddev_t
*mddev
, char *page
)
3962 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->suspend_hi
);
3966 suspend_hi_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3969 unsigned long long new = simple_strtoull(buf
, &e
, 10);
3971 if (mddev
->pers
== NULL
||
3972 mddev
->pers
->quiesce
== NULL
)
3974 if (buf
== e
|| (*e
&& *e
!= '\n'))
3976 if ((new <= mddev
->suspend_lo
&& mddev
->suspend_lo
>= mddev
->suspend_hi
) ||
3977 (new > mddev
->suspend_lo
&& new > mddev
->suspend_hi
)) {
3978 mddev
->suspend_hi
= new;
3979 mddev
->pers
->quiesce(mddev
, 1);
3980 mddev
->pers
->quiesce(mddev
, 0);
3985 static struct md_sysfs_entry md_suspend_hi
=
3986 __ATTR(suspend_hi
, S_IRUGO
|S_IWUSR
, suspend_hi_show
, suspend_hi_store
);
3989 reshape_position_show(mddev_t
*mddev
, char *page
)
3991 if (mddev
->reshape_position
!= MaxSector
)
3992 return sprintf(page
, "%llu\n",
3993 (unsigned long long)mddev
->reshape_position
);
3994 strcpy(page
, "none\n");
3999 reshape_position_store(mddev_t
*mddev
, const char *buf
, size_t len
)
4002 unsigned long long new = simple_strtoull(buf
, &e
, 10);
4005 if (buf
== e
|| (*e
&& *e
!= '\n'))
4007 mddev
->reshape_position
= new;
4008 mddev
->delta_disks
= 0;
4009 mddev
->new_level
= mddev
->level
;
4010 mddev
->new_layout
= mddev
->layout
;
4011 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
4015 static struct md_sysfs_entry md_reshape_position
=
4016 __ATTR(reshape_position
, S_IRUGO
|S_IWUSR
, reshape_position_show
,
4017 reshape_position_store
);
4020 array_size_show(mddev_t
*mddev
, char *page
)
4022 if (mddev
->external_size
)
4023 return sprintf(page
, "%llu\n",
4024 (unsigned long long)mddev
->array_sectors
/2);
4026 return sprintf(page
, "default\n");
4030 array_size_store(mddev_t
*mddev
, const char *buf
, size_t len
)
4034 if (strncmp(buf
, "default", 7) == 0) {
4036 sectors
= mddev
->pers
->size(mddev
, 0, 0);
4038 sectors
= mddev
->array_sectors
;
4040 mddev
->external_size
= 0;
4042 if (strict_blocks_to_sectors(buf
, §ors
) < 0)
4044 if (mddev
->pers
&& mddev
->pers
->size(mddev
, 0, 0) < sectors
)
4047 mddev
->external_size
= 1;
4050 mddev
->array_sectors
= sectors
;
4051 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
4053 revalidate_disk(mddev
->gendisk
);
4058 static struct md_sysfs_entry md_array_size
=
4059 __ATTR(array_size
, S_IRUGO
|S_IWUSR
, array_size_show
,
4062 static struct attribute
*md_default_attrs
[] = {
4065 &md_raid_disks
.attr
,
4066 &md_chunk_size
.attr
,
4068 &md_resync_start
.attr
,
4070 &md_new_device
.attr
,
4071 &md_safe_delay
.attr
,
4072 &md_array_state
.attr
,
4073 &md_reshape_position
.attr
,
4074 &md_array_size
.attr
,
4075 &max_corr_read_errors
.attr
,
4079 static struct attribute
*md_redundancy_attrs
[] = {
4081 &md_mismatches
.attr
,
4084 &md_sync_speed
.attr
,
4085 &md_sync_force_parallel
.attr
,
4086 &md_sync_completed
.attr
,
4089 &md_suspend_lo
.attr
,
4090 &md_suspend_hi
.attr
,
4095 static struct attribute_group md_redundancy_group
= {
4097 .attrs
= md_redundancy_attrs
,
4102 md_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
4104 struct md_sysfs_entry
*entry
= container_of(attr
, struct md_sysfs_entry
, attr
);
4105 mddev_t
*mddev
= container_of(kobj
, struct mddev_s
, kobj
);
4110 rv
= mddev_lock(mddev
);
4112 rv
= entry
->show(mddev
, page
);
4113 mddev_unlock(mddev
);
4119 md_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
4120 const char *page
, size_t length
)
4122 struct md_sysfs_entry
*entry
= container_of(attr
, struct md_sysfs_entry
, attr
);
4123 mddev_t
*mddev
= container_of(kobj
, struct mddev_s
, kobj
);
4128 if (!capable(CAP_SYS_ADMIN
))
4130 rv
= mddev_lock(mddev
);
4131 if (mddev
->hold_active
== UNTIL_IOCTL
)
4132 mddev
->hold_active
= 0;
4134 rv
= entry
->store(mddev
, page
, length
);
4135 mddev_unlock(mddev
);
4140 static void md_free(struct kobject
*ko
)
4142 mddev_t
*mddev
= container_of(ko
, mddev_t
, kobj
);
4144 if (mddev
->sysfs_state
)
4145 sysfs_put(mddev
->sysfs_state
);
4147 if (mddev
->gendisk
) {
4148 del_gendisk(mddev
->gendisk
);
4149 put_disk(mddev
->gendisk
);
4152 blk_cleanup_queue(mddev
->queue
);
4157 static const struct sysfs_ops md_sysfs_ops
= {
4158 .show
= md_attr_show
,
4159 .store
= md_attr_store
,
4161 static struct kobj_type md_ktype
= {
4163 .sysfs_ops
= &md_sysfs_ops
,
4164 .default_attrs
= md_default_attrs
,
4169 static void mddev_delayed_delete(struct work_struct
*ws
)
4171 mddev_t
*mddev
= container_of(ws
, mddev_t
, del_work
);
4173 sysfs_remove_group(&mddev
->kobj
, &md_bitmap_group
);
4174 kobject_del(&mddev
->kobj
);
4175 kobject_put(&mddev
->kobj
);
4178 static int md_alloc(dev_t dev
, char *name
)
4180 static DEFINE_MUTEX(disks_mutex
);
4181 mddev_t
*mddev
= mddev_find(dev
);
4182 struct gendisk
*disk
;
4191 partitioned
= (MAJOR(mddev
->unit
) != MD_MAJOR
);
4192 shift
= partitioned
? MdpMinorShift
: 0;
4193 unit
= MINOR(mddev
->unit
) >> shift
;
4195 /* wait for any previous instance if this device
4196 * to be completed removed (mddev_delayed_delete).
4198 flush_scheduled_work();
4200 mutex_lock(&disks_mutex
);
4206 /* Need to ensure that 'name' is not a duplicate.
4209 spin_lock(&all_mddevs_lock
);
4211 list_for_each_entry(mddev2
, &all_mddevs
, all_mddevs
)
4212 if (mddev2
->gendisk
&&
4213 strcmp(mddev2
->gendisk
->disk_name
, name
) == 0) {
4214 spin_unlock(&all_mddevs_lock
);
4217 spin_unlock(&all_mddevs_lock
);
4221 mddev
->queue
= blk_alloc_queue(GFP_KERNEL
);
4224 mddev
->queue
->queuedata
= mddev
;
4226 /* Can be unlocked because the queue is new: no concurrency */
4227 queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER
, mddev
->queue
);
4229 blk_queue_make_request(mddev
->queue
, md_make_request
);
4231 disk
= alloc_disk(1 << shift
);
4233 blk_cleanup_queue(mddev
->queue
);
4234 mddev
->queue
= NULL
;
4237 disk
->major
= MAJOR(mddev
->unit
);
4238 disk
->first_minor
= unit
<< shift
;
4240 strcpy(disk
->disk_name
, name
);
4241 else if (partitioned
)
4242 sprintf(disk
->disk_name
, "md_d%d", unit
);
4244 sprintf(disk
->disk_name
, "md%d", unit
);
4245 disk
->fops
= &md_fops
;
4246 disk
->private_data
= mddev
;
4247 disk
->queue
= mddev
->queue
;
4248 /* Allow extended partitions. This makes the
4249 * 'mdp' device redundant, but we can't really
4252 disk
->flags
|= GENHD_FL_EXT_DEVT
;
4254 mddev
->gendisk
= disk
;
4255 error
= kobject_init_and_add(&mddev
->kobj
, &md_ktype
,
4256 &disk_to_dev(disk
)->kobj
, "%s", "md");
4258 /* This isn't possible, but as kobject_init_and_add is marked
4259 * __must_check, we must do something with the result
4261 printk(KERN_WARNING
"md: cannot register %s/md - name in use\n",
4265 if (mddev
->kobj
.sd
&&
4266 sysfs_create_group(&mddev
->kobj
, &md_bitmap_group
))
4267 printk(KERN_DEBUG
"pointless warning\n");
4269 mutex_unlock(&disks_mutex
);
4270 if (!error
&& mddev
->kobj
.sd
) {
4271 kobject_uevent(&mddev
->kobj
, KOBJ_ADD
);
4272 mddev
->sysfs_state
= sysfs_get_dirent_safe(mddev
->kobj
.sd
, "array_state");
4278 static struct kobject
*md_probe(dev_t dev
, int *part
, void *data
)
4280 md_alloc(dev
, NULL
);
4284 static int add_named_array(const char *val
, struct kernel_param
*kp
)
4286 /* val must be "md_*" where * is not all digits.
4287 * We allocate an array with a large free minor number, and
4288 * set the name to val. val must not already be an active name.
4290 int len
= strlen(val
);
4291 char buf
[DISK_NAME_LEN
];
4293 while (len
&& val
[len
-1] == '\n')
4295 if (len
>= DISK_NAME_LEN
)
4297 strlcpy(buf
, val
, len
+1);
4298 if (strncmp(buf
, "md_", 3) != 0)
4300 return md_alloc(0, buf
);
4303 static void md_safemode_timeout(unsigned long data
)
4305 mddev_t
*mddev
= (mddev_t
*) data
;
4307 if (!atomic_read(&mddev
->writes_pending
)) {
4308 mddev
->safemode
= 1;
4309 if (mddev
->external
)
4310 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4312 md_wakeup_thread(mddev
->thread
);
4315 static int start_dirty_degraded
;
4317 int md_run(mddev_t
*mddev
)
4321 struct mdk_personality
*pers
;
4323 if (list_empty(&mddev
->disks
))
4324 /* cannot run an array with no devices.. */
4329 /* Cannot run until previous stop completes properly */
4330 if (mddev
->sysfs_active
)
4334 * Analyze all RAID superblock(s)
4336 if (!mddev
->raid_disks
) {
4337 if (!mddev
->persistent
)
4342 if (mddev
->level
!= LEVEL_NONE
)
4343 request_module("md-level-%d", mddev
->level
);
4344 else if (mddev
->clevel
[0])
4345 request_module("md-%s", mddev
->clevel
);
4348 * Drop all container device buffers, from now on
4349 * the only valid external interface is through the md
4352 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
4353 if (test_bit(Faulty
, &rdev
->flags
))
4355 sync_blockdev(rdev
->bdev
);
4356 invalidate_bdev(rdev
->bdev
);
4358 /* perform some consistency tests on the device.
4359 * We don't want the data to overlap the metadata,
4360 * Internal Bitmap issues have been handled elsewhere.
4362 if (rdev
->data_offset
< rdev
->sb_start
) {
4363 if (mddev
->dev_sectors
&&
4364 rdev
->data_offset
+ mddev
->dev_sectors
4366 printk("md: %s: data overlaps metadata\n",
4371 if (rdev
->sb_start
+ rdev
->sb_size
/512
4372 > rdev
->data_offset
) {
4373 printk("md: %s: metadata overlaps data\n",
4378 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
4381 spin_lock(&pers_lock
);
4382 pers
= find_pers(mddev
->level
, mddev
->clevel
);
4383 if (!pers
|| !try_module_get(pers
->owner
)) {
4384 spin_unlock(&pers_lock
);
4385 if (mddev
->level
!= LEVEL_NONE
)
4386 printk(KERN_WARNING
"md: personality for level %d is not loaded!\n",
4389 printk(KERN_WARNING
"md: personality for level %s is not loaded!\n",
4394 spin_unlock(&pers_lock
);
4395 if (mddev
->level
!= pers
->level
) {
4396 mddev
->level
= pers
->level
;
4397 mddev
->new_level
= pers
->level
;
4399 strlcpy(mddev
->clevel
, pers
->name
, sizeof(mddev
->clevel
));
4401 if (mddev
->reshape_position
!= MaxSector
&&
4402 pers
->start_reshape
== NULL
) {
4403 /* This personality cannot handle reshaping... */
4405 module_put(pers
->owner
);
4409 if (pers
->sync_request
) {
4410 /* Warn if this is a potentially silly
4413 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
4417 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
4418 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
) {
4420 rdev
->bdev
->bd_contains
==
4421 rdev2
->bdev
->bd_contains
) {
4423 "%s: WARNING: %s appears to be"
4424 " on the same physical disk as"
4427 bdevname(rdev
->bdev
,b
),
4428 bdevname(rdev2
->bdev
,b2
));
4435 "True protection against single-disk"
4436 " failure might be compromised.\n");
4439 mddev
->recovery
= 0;
4440 /* may be over-ridden by personality */
4441 mddev
->resync_max_sectors
= mddev
->dev_sectors
;
4443 mddev
->ok_start_degraded
= start_dirty_degraded
;
4445 if (start_readonly
&& mddev
->ro
== 0)
4446 mddev
->ro
= 2; /* read-only, but switch on first write */
4448 err
= mddev
->pers
->run(mddev
);
4450 printk(KERN_ERR
"md: pers->run() failed ...\n");
4451 else if (mddev
->pers
->size(mddev
, 0, 0) < mddev
->array_sectors
) {
4452 WARN_ONCE(!mddev
->external_size
, "%s: default size too small,"
4453 " but 'external_size' not in effect?\n", __func__
);
4455 "md: invalid array_size %llu > default size %llu\n",
4456 (unsigned long long)mddev
->array_sectors
/ 2,
4457 (unsigned long long)mddev
->pers
->size(mddev
, 0, 0) / 2);
4459 mddev
->pers
->stop(mddev
);
4461 if (err
== 0 && mddev
->pers
->sync_request
) {
4462 err
= bitmap_create(mddev
);
4464 printk(KERN_ERR
"%s: failed to create bitmap (%d)\n",
4465 mdname(mddev
), err
);
4466 mddev
->pers
->stop(mddev
);
4470 module_put(mddev
->pers
->owner
);
4472 bitmap_destroy(mddev
);
4475 if (mddev
->pers
->sync_request
) {
4476 if (mddev
->kobj
.sd
&&
4477 sysfs_create_group(&mddev
->kobj
, &md_redundancy_group
))
4479 "md: cannot register extra attributes for %s\n",
4481 mddev
->sysfs_action
= sysfs_get_dirent_safe(mddev
->kobj
.sd
, "sync_action");
4482 } else if (mddev
->ro
== 2) /* auto-readonly not meaningful */
4485 atomic_set(&mddev
->writes_pending
,0);
4486 atomic_set(&mddev
->max_corr_read_errors
,
4487 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS
);
4488 mddev
->safemode
= 0;
4489 mddev
->safemode_timer
.function
= md_safemode_timeout
;
4490 mddev
->safemode_timer
.data
= (unsigned long) mddev
;
4491 mddev
->safemode_delay
= (200 * HZ
)/1000 +1; /* 200 msec delay */
4494 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
4495 if (rdev
->raid_disk
>= 0) {
4497 sprintf(nm
, "rd%d", rdev
->raid_disk
);
4498 if (sysfs_create_link(&mddev
->kobj
, &rdev
->kobj
, nm
))
4499 /* failure here is OK */;
4502 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4505 md_update_sb(mddev
, 0);
4507 md_wakeup_thread(mddev
->thread
);
4508 md_wakeup_thread(mddev
->sync_thread
); /* possibly kick off a reshape */
4510 md_new_event(mddev
);
4511 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4512 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
4513 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
4516 EXPORT_SYMBOL_GPL(md_run
);
4518 static int do_md_run(mddev_t
*mddev
)
4522 err
= md_run(mddev
);
4525 err
= bitmap_load(mddev
);
4527 bitmap_destroy(mddev
);
4530 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
4531 revalidate_disk(mddev
->gendisk
);
4532 kobject_uevent(&disk_to_dev(mddev
->gendisk
)->kobj
, KOBJ_CHANGE
);
4537 static int restart_array(mddev_t
*mddev
)
4539 struct gendisk
*disk
= mddev
->gendisk
;
4541 /* Complain if it has no devices */
4542 if (list_empty(&mddev
->disks
))
4548 mddev
->safemode
= 0;
4550 set_disk_ro(disk
, 0);
4551 printk(KERN_INFO
"md: %s switched to read-write mode.\n",
4553 /* Kick recovery or resync if necessary */
4554 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4555 md_wakeup_thread(mddev
->thread
);
4556 md_wakeup_thread(mddev
->sync_thread
);
4557 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4561 /* similar to deny_write_access, but accounts for our holding a reference
4562 * to the file ourselves */
4563 static int deny_bitmap_write_access(struct file
* file
)
4565 struct inode
*inode
= file
->f_mapping
->host
;
4567 spin_lock(&inode
->i_lock
);
4568 if (atomic_read(&inode
->i_writecount
) > 1) {
4569 spin_unlock(&inode
->i_lock
);
4572 atomic_set(&inode
->i_writecount
, -1);
4573 spin_unlock(&inode
->i_lock
);
4578 void restore_bitmap_write_access(struct file
*file
)
4580 struct inode
*inode
= file
->f_mapping
->host
;
4582 spin_lock(&inode
->i_lock
);
4583 atomic_set(&inode
->i_writecount
, 1);
4584 spin_unlock(&inode
->i_lock
);
4587 static void md_clean(mddev_t
*mddev
)
4589 mddev
->array_sectors
= 0;
4590 mddev
->external_size
= 0;
4591 mddev
->dev_sectors
= 0;
4592 mddev
->raid_disks
= 0;
4593 mddev
->recovery_cp
= 0;
4594 mddev
->resync_min
= 0;
4595 mddev
->resync_max
= MaxSector
;
4596 mddev
->reshape_position
= MaxSector
;
4597 mddev
->external
= 0;
4598 mddev
->persistent
= 0;
4599 mddev
->level
= LEVEL_NONE
;
4600 mddev
->clevel
[0] = 0;
4603 mddev
->metadata_type
[0] = 0;
4604 mddev
->chunk_sectors
= 0;
4605 mddev
->ctime
= mddev
->utime
= 0;
4607 mddev
->max_disks
= 0;
4609 mddev
->can_decrease_events
= 0;
4610 mddev
->delta_disks
= 0;
4611 mddev
->new_level
= LEVEL_NONE
;
4612 mddev
->new_layout
= 0;
4613 mddev
->new_chunk_sectors
= 0;
4614 mddev
->curr_resync
= 0;
4615 mddev
->resync_mismatches
= 0;
4616 mddev
->suspend_lo
= mddev
->suspend_hi
= 0;
4617 mddev
->sync_speed_min
= mddev
->sync_speed_max
= 0;
4618 mddev
->recovery
= 0;
4620 mddev
->degraded
= 0;
4621 mddev
->safemode
= 0;
4622 mddev
->bitmap_info
.offset
= 0;
4623 mddev
->bitmap_info
.default_offset
= 0;
4624 mddev
->bitmap_info
.chunksize
= 0;
4625 mddev
->bitmap_info
.daemon_sleep
= 0;
4626 mddev
->bitmap_info
.max_write_behind
= 0;
4630 void md_stop_writes(mddev_t
*mddev
)
4632 if (mddev
->sync_thread
) {
4633 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4634 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
4635 md_unregister_thread(mddev
->sync_thread
);
4636 mddev
->sync_thread
= NULL
;
4639 del_timer_sync(&mddev
->safemode_timer
);
4641 bitmap_flush(mddev
);
4642 md_super_wait(mddev
);
4644 if (!mddev
->in_sync
|| mddev
->flags
) {
4645 /* mark array as shutdown cleanly */
4647 md_update_sb(mddev
, 1);
4650 EXPORT_SYMBOL_GPL(md_stop_writes
);
4652 void md_stop(mddev_t
*mddev
)
4654 mddev
->pers
->stop(mddev
);
4655 if (mddev
->pers
->sync_request
&& mddev
->to_remove
== NULL
)
4656 mddev
->to_remove
= &md_redundancy_group
;
4657 module_put(mddev
->pers
->owner
);
4659 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4661 EXPORT_SYMBOL_GPL(md_stop
);
4663 static int md_set_readonly(mddev_t
*mddev
, int is_open
)
4666 mutex_lock(&mddev
->open_mutex
);
4667 if (atomic_read(&mddev
->openers
) > is_open
) {
4668 printk("md: %s still in use.\n",mdname(mddev
));
4673 md_stop_writes(mddev
);
4679 set_disk_ro(mddev
->gendisk
, 1);
4680 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4681 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4685 mutex_unlock(&mddev
->open_mutex
);
4690 * 0 - completely stop and dis-assemble array
4691 * 2 - stop but do not disassemble array
4693 static int do_md_stop(mddev_t
* mddev
, int mode
, int is_open
)
4695 struct gendisk
*disk
= mddev
->gendisk
;
4698 mutex_lock(&mddev
->open_mutex
);
4699 if (atomic_read(&mddev
->openers
) > is_open
||
4700 mddev
->sysfs_active
) {
4701 printk("md: %s still in use.\n",mdname(mddev
));
4702 mutex_unlock(&mddev
->open_mutex
);
4708 set_disk_ro(disk
, 0);
4710 md_stop_writes(mddev
);
4712 mddev
->queue
->merge_bvec_fn
= NULL
;
4713 mddev
->queue
->unplug_fn
= NULL
;
4714 mddev
->queue
->backing_dev_info
.congested_fn
= NULL
;
4716 /* tell userspace to handle 'inactive' */
4717 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4719 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
4720 if (rdev
->raid_disk
>= 0) {
4722 sprintf(nm
, "rd%d", rdev
->raid_disk
);
4723 sysfs_remove_link(&mddev
->kobj
, nm
);
4726 set_capacity(disk
, 0);
4727 mutex_unlock(&mddev
->open_mutex
);
4728 revalidate_disk(disk
);
4733 mutex_unlock(&mddev
->open_mutex
);
4735 * Free resources if final stop
4738 printk(KERN_INFO
"md: %s stopped.\n", mdname(mddev
));
4740 bitmap_destroy(mddev
);
4741 if (mddev
->bitmap_info
.file
) {
4742 restore_bitmap_write_access(mddev
->bitmap_info
.file
);
4743 fput(mddev
->bitmap_info
.file
);
4744 mddev
->bitmap_info
.file
= NULL
;
4746 mddev
->bitmap_info
.offset
= 0;
4748 export_array(mddev
);
4751 kobject_uevent(&disk_to_dev(mddev
->gendisk
)->kobj
, KOBJ_CHANGE
);
4752 if (mddev
->hold_active
== UNTIL_STOP
)
4753 mddev
->hold_active
= 0;
4755 blk_integrity_unregister(disk
);
4756 md_new_event(mddev
);
4757 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4762 static void autorun_array(mddev_t
*mddev
)
4767 if (list_empty(&mddev
->disks
))
4770 printk(KERN_INFO
"md: running: ");
4772 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
4773 char b
[BDEVNAME_SIZE
];
4774 printk("<%s>", bdevname(rdev
->bdev
,b
));
4778 err
= do_md_run(mddev
);
4780 printk(KERN_WARNING
"md: do_md_run() returned %d\n", err
);
4781 do_md_stop(mddev
, 0, 0);
4786 * lets try to run arrays based on all disks that have arrived
4787 * until now. (those are in pending_raid_disks)
4789 * the method: pick the first pending disk, collect all disks with
4790 * the same UUID, remove all from the pending list and put them into
4791 * the 'same_array' list. Then order this list based on superblock
4792 * update time (freshest comes first), kick out 'old' disks and
4793 * compare superblocks. If everything's fine then run it.
4795 * If "unit" is allocated, then bump its reference count
4797 static void autorun_devices(int part
)
4799 mdk_rdev_t
*rdev0
, *rdev
, *tmp
;
4801 char b
[BDEVNAME_SIZE
];
4803 printk(KERN_INFO
"md: autorun ...\n");
4804 while (!list_empty(&pending_raid_disks
)) {
4807 LIST_HEAD(candidates
);
4808 rdev0
= list_entry(pending_raid_disks
.next
,
4809 mdk_rdev_t
, same_set
);
4811 printk(KERN_INFO
"md: considering %s ...\n",
4812 bdevname(rdev0
->bdev
,b
));
4813 INIT_LIST_HEAD(&candidates
);
4814 rdev_for_each_list(rdev
, tmp
, &pending_raid_disks
)
4815 if (super_90_load(rdev
, rdev0
, 0) >= 0) {
4816 printk(KERN_INFO
"md: adding %s ...\n",
4817 bdevname(rdev
->bdev
,b
));
4818 list_move(&rdev
->same_set
, &candidates
);
4821 * now we have a set of devices, with all of them having
4822 * mostly sane superblocks. It's time to allocate the
4826 dev
= MKDEV(mdp_major
,
4827 rdev0
->preferred_minor
<< MdpMinorShift
);
4828 unit
= MINOR(dev
) >> MdpMinorShift
;
4830 dev
= MKDEV(MD_MAJOR
, rdev0
->preferred_minor
);
4833 if (rdev0
->preferred_minor
!= unit
) {
4834 printk(KERN_INFO
"md: unit number in %s is bad: %d\n",
4835 bdevname(rdev0
->bdev
, b
), rdev0
->preferred_minor
);
4839 md_probe(dev
, NULL
, NULL
);
4840 mddev
= mddev_find(dev
);
4841 if (!mddev
|| !mddev
->gendisk
) {
4845 "md: cannot allocate memory for md drive.\n");
4848 if (mddev_lock(mddev
))
4849 printk(KERN_WARNING
"md: %s locked, cannot run\n",
4851 else if (mddev
->raid_disks
|| mddev
->major_version
4852 || !list_empty(&mddev
->disks
)) {
4854 "md: %s already running, cannot run %s\n",
4855 mdname(mddev
), bdevname(rdev0
->bdev
,b
));
4856 mddev_unlock(mddev
);
4858 printk(KERN_INFO
"md: created %s\n", mdname(mddev
));
4859 mddev
->persistent
= 1;
4860 rdev_for_each_list(rdev
, tmp
, &candidates
) {
4861 list_del_init(&rdev
->same_set
);
4862 if (bind_rdev_to_array(rdev
, mddev
))
4865 autorun_array(mddev
);
4866 mddev_unlock(mddev
);
4868 /* on success, candidates will be empty, on error
4871 rdev_for_each_list(rdev
, tmp
, &candidates
) {
4872 list_del_init(&rdev
->same_set
);
4877 printk(KERN_INFO
"md: ... autorun DONE.\n");
4879 #endif /* !MODULE */
4881 static int get_version(void __user
* arg
)
4885 ver
.major
= MD_MAJOR_VERSION
;
4886 ver
.minor
= MD_MINOR_VERSION
;
4887 ver
.patchlevel
= MD_PATCHLEVEL_VERSION
;
4889 if (copy_to_user(arg
, &ver
, sizeof(ver
)))
4895 static int get_array_info(mddev_t
* mddev
, void __user
* arg
)
4897 mdu_array_info_t info
;
4898 int nr
,working
,insync
,failed
,spare
;
4901 nr
=working
=insync
=failed
=spare
=0;
4902 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
4904 if (test_bit(Faulty
, &rdev
->flags
))
4908 if (test_bit(In_sync
, &rdev
->flags
))
4915 info
.major_version
= mddev
->major_version
;
4916 info
.minor_version
= mddev
->minor_version
;
4917 info
.patch_version
= MD_PATCHLEVEL_VERSION
;
4918 info
.ctime
= mddev
->ctime
;
4919 info
.level
= mddev
->level
;
4920 info
.size
= mddev
->dev_sectors
/ 2;
4921 if (info
.size
!= mddev
->dev_sectors
/ 2) /* overflow */
4924 info
.raid_disks
= mddev
->raid_disks
;
4925 info
.md_minor
= mddev
->md_minor
;
4926 info
.not_persistent
= !mddev
->persistent
;
4928 info
.utime
= mddev
->utime
;
4931 info
.state
= (1<<MD_SB_CLEAN
);
4932 if (mddev
->bitmap
&& mddev
->bitmap_info
.offset
)
4933 info
.state
= (1<<MD_SB_BITMAP_PRESENT
);
4934 info
.active_disks
= insync
;
4935 info
.working_disks
= working
;
4936 info
.failed_disks
= failed
;
4937 info
.spare_disks
= spare
;
4939 info
.layout
= mddev
->layout
;
4940 info
.chunk_size
= mddev
->chunk_sectors
<< 9;
4942 if (copy_to_user(arg
, &info
, sizeof(info
)))
4948 static int get_bitmap_file(mddev_t
* mddev
, void __user
* arg
)
4950 mdu_bitmap_file_t
*file
= NULL
; /* too big for stack allocation */
4951 char *ptr
, *buf
= NULL
;
4954 if (md_allow_write(mddev
))
4955 file
= kmalloc(sizeof(*file
), GFP_NOIO
);
4957 file
= kmalloc(sizeof(*file
), GFP_KERNEL
);
4962 /* bitmap disabled, zero the first byte and copy out */
4963 if (!mddev
->bitmap
|| !mddev
->bitmap
->file
) {
4964 file
->pathname
[0] = '\0';
4968 buf
= kmalloc(sizeof(file
->pathname
), GFP_KERNEL
);
4972 ptr
= d_path(&mddev
->bitmap
->file
->f_path
, buf
, sizeof(file
->pathname
));
4976 strcpy(file
->pathname
, ptr
);
4980 if (copy_to_user(arg
, file
, sizeof(*file
)))
4988 static int get_disk_info(mddev_t
* mddev
, void __user
* arg
)
4990 mdu_disk_info_t info
;
4993 if (copy_from_user(&info
, arg
, sizeof(info
)))
4996 rdev
= find_rdev_nr(mddev
, info
.number
);
4998 info
.major
= MAJOR(rdev
->bdev
->bd_dev
);
4999 info
.minor
= MINOR(rdev
->bdev
->bd_dev
);
5000 info
.raid_disk
= rdev
->raid_disk
;
5002 if (test_bit(Faulty
, &rdev
->flags
))
5003 info
.state
|= (1<<MD_DISK_FAULTY
);
5004 else if (test_bit(In_sync
, &rdev
->flags
)) {
5005 info
.state
|= (1<<MD_DISK_ACTIVE
);
5006 info
.state
|= (1<<MD_DISK_SYNC
);
5008 if (test_bit(WriteMostly
, &rdev
->flags
))
5009 info
.state
|= (1<<MD_DISK_WRITEMOSTLY
);
5011 info
.major
= info
.minor
= 0;
5012 info
.raid_disk
= -1;
5013 info
.state
= (1<<MD_DISK_REMOVED
);
5016 if (copy_to_user(arg
, &info
, sizeof(info
)))
5022 static int add_new_disk(mddev_t
* mddev
, mdu_disk_info_t
*info
)
5024 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
5026 dev_t dev
= MKDEV(info
->major
,info
->minor
);
5028 if (info
->major
!= MAJOR(dev
) || info
->minor
!= MINOR(dev
))
5031 if (!mddev
->raid_disks
) {
5033 /* expecting a device which has a superblock */
5034 rdev
= md_import_device(dev
, mddev
->major_version
, mddev
->minor_version
);
5037 "md: md_import_device returned %ld\n",
5039 return PTR_ERR(rdev
);
5041 if (!list_empty(&mddev
->disks
)) {
5042 mdk_rdev_t
*rdev0
= list_entry(mddev
->disks
.next
,
5043 mdk_rdev_t
, same_set
);
5044 err
= super_types
[mddev
->major_version
]
5045 .load_super(rdev
, rdev0
, mddev
->minor_version
);
5048 "md: %s has different UUID to %s\n",
5049 bdevname(rdev
->bdev
,b
),
5050 bdevname(rdev0
->bdev
,b2
));
5055 err
= bind_rdev_to_array(rdev
, mddev
);
5062 * add_new_disk can be used once the array is assembled
5063 * to add "hot spares". They must already have a superblock
5068 if (!mddev
->pers
->hot_add_disk
) {
5070 "%s: personality does not support diskops!\n",
5074 if (mddev
->persistent
)
5075 rdev
= md_import_device(dev
, mddev
->major_version
,
5076 mddev
->minor_version
);
5078 rdev
= md_import_device(dev
, -1, -1);
5081 "md: md_import_device returned %ld\n",
5083 return PTR_ERR(rdev
);
5085 /* set save_raid_disk if appropriate */
5086 if (!mddev
->persistent
) {
5087 if (info
->state
& (1<<MD_DISK_SYNC
) &&
5088 info
->raid_disk
< mddev
->raid_disks
)
5089 rdev
->raid_disk
= info
->raid_disk
;
5091 rdev
->raid_disk
= -1;
5093 super_types
[mddev
->major_version
].
5094 validate_super(mddev
, rdev
);
5095 rdev
->saved_raid_disk
= rdev
->raid_disk
;
5097 clear_bit(In_sync
, &rdev
->flags
); /* just to be sure */
5098 if (info
->state
& (1<<MD_DISK_WRITEMOSTLY
))
5099 set_bit(WriteMostly
, &rdev
->flags
);
5101 clear_bit(WriteMostly
, &rdev
->flags
);
5103 rdev
->raid_disk
= -1;
5104 err
= bind_rdev_to_array(rdev
, mddev
);
5105 if (!err
&& !mddev
->pers
->hot_remove_disk
) {
5106 /* If there is hot_add_disk but no hot_remove_disk
5107 * then added disks for geometry changes,
5108 * and should be added immediately.
5110 super_types
[mddev
->major_version
].
5111 validate_super(mddev
, rdev
);
5112 err
= mddev
->pers
->hot_add_disk(mddev
, rdev
);
5114 unbind_rdev_from_array(rdev
);
5119 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
5121 md_update_sb(mddev
, 1);
5122 if (mddev
->degraded
)
5123 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
5124 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5125 md_wakeup_thread(mddev
->thread
);
5129 /* otherwise, add_new_disk is only allowed
5130 * for major_version==0 superblocks
5132 if (mddev
->major_version
!= 0) {
5133 printk(KERN_WARNING
"%s: ADD_NEW_DISK not supported\n",
5138 if (!(info
->state
& (1<<MD_DISK_FAULTY
))) {
5140 rdev
= md_import_device(dev
, -1, 0);
5143 "md: error, md_import_device() returned %ld\n",
5145 return PTR_ERR(rdev
);
5147 rdev
->desc_nr
= info
->number
;
5148 if (info
->raid_disk
< mddev
->raid_disks
)
5149 rdev
->raid_disk
= info
->raid_disk
;
5151 rdev
->raid_disk
= -1;
5153 if (rdev
->raid_disk
< mddev
->raid_disks
)
5154 if (info
->state
& (1<<MD_DISK_SYNC
))
5155 set_bit(In_sync
, &rdev
->flags
);
5157 if (info
->state
& (1<<MD_DISK_WRITEMOSTLY
))
5158 set_bit(WriteMostly
, &rdev
->flags
);
5160 if (!mddev
->persistent
) {
5161 printk(KERN_INFO
"md: nonpersistent superblock ...\n");
5162 rdev
->sb_start
= rdev
->bdev
->bd_inode
->i_size
/ 512;
5164 rdev
->sb_start
= calc_dev_sboffset(rdev
->bdev
);
5165 rdev
->sectors
= rdev
->sb_start
;
5167 err
= bind_rdev_to_array(rdev
, mddev
);
5177 static int hot_remove_disk(mddev_t
* mddev
, dev_t dev
)
5179 char b
[BDEVNAME_SIZE
];
5182 rdev
= find_rdev(mddev
, dev
);
5186 if (rdev
->raid_disk
>= 0)
5189 kick_rdev_from_array(rdev
);
5190 md_update_sb(mddev
, 1);
5191 md_new_event(mddev
);
5195 printk(KERN_WARNING
"md: cannot remove active disk %s from %s ...\n",
5196 bdevname(rdev
->bdev
,b
), mdname(mddev
));
5200 static int hot_add_disk(mddev_t
* mddev
, dev_t dev
)
5202 char b
[BDEVNAME_SIZE
];
5209 if (mddev
->major_version
!= 0) {
5210 printk(KERN_WARNING
"%s: HOT_ADD may only be used with"
5211 " version-0 superblocks.\n",
5215 if (!mddev
->pers
->hot_add_disk
) {
5217 "%s: personality does not support diskops!\n",
5222 rdev
= md_import_device(dev
, -1, 0);
5225 "md: error, md_import_device() returned %ld\n",
5230 if (mddev
->persistent
)
5231 rdev
->sb_start
= calc_dev_sboffset(rdev
->bdev
);
5233 rdev
->sb_start
= rdev
->bdev
->bd_inode
->i_size
/ 512;
5235 rdev
->sectors
= rdev
->sb_start
;
5237 if (test_bit(Faulty
, &rdev
->flags
)) {
5239 "md: can not hot-add faulty %s disk to %s!\n",
5240 bdevname(rdev
->bdev
,b
), mdname(mddev
));
5244 clear_bit(In_sync
, &rdev
->flags
);
5246 rdev
->saved_raid_disk
= -1;
5247 err
= bind_rdev_to_array(rdev
, mddev
);
5252 * The rest should better be atomic, we can have disk failures
5253 * noticed in interrupt contexts ...
5256 rdev
->raid_disk
= -1;
5258 md_update_sb(mddev
, 1);
5261 * Kick recovery, maybe this spare has to be added to the
5262 * array immediately.
5264 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5265 md_wakeup_thread(mddev
->thread
);
5266 md_new_event(mddev
);
5274 static int set_bitmap_file(mddev_t
*mddev
, int fd
)
5279 if (!mddev
->pers
->quiesce
)
5281 if (mddev
->recovery
|| mddev
->sync_thread
)
5283 /* we should be able to change the bitmap.. */
5289 return -EEXIST
; /* cannot add when bitmap is present */
5290 mddev
->bitmap_info
.file
= fget(fd
);
5292 if (mddev
->bitmap_info
.file
== NULL
) {
5293 printk(KERN_ERR
"%s: error: failed to get bitmap file\n",
5298 err
= deny_bitmap_write_access(mddev
->bitmap_info
.file
);
5300 printk(KERN_ERR
"%s: error: bitmap file is already in use\n",
5302 fput(mddev
->bitmap_info
.file
);
5303 mddev
->bitmap_info
.file
= NULL
;
5306 mddev
->bitmap_info
.offset
= 0; /* file overrides offset */
5307 } else if (mddev
->bitmap
== NULL
)
5308 return -ENOENT
; /* cannot remove what isn't there */
5311 mddev
->pers
->quiesce(mddev
, 1);
5313 err
= bitmap_create(mddev
);
5315 err
= bitmap_load(mddev
);
5317 if (fd
< 0 || err
) {
5318 bitmap_destroy(mddev
);
5319 fd
= -1; /* make sure to put the file */
5321 mddev
->pers
->quiesce(mddev
, 0);
5324 if (mddev
->bitmap_info
.file
) {
5325 restore_bitmap_write_access(mddev
->bitmap_info
.file
);
5326 fput(mddev
->bitmap_info
.file
);
5328 mddev
->bitmap_info
.file
= NULL
;
5335 * set_array_info is used two different ways
5336 * The original usage is when creating a new array.
5337 * In this usage, raid_disks is > 0 and it together with
5338 * level, size, not_persistent,layout,chunksize determine the
5339 * shape of the array.
5340 * This will always create an array with a type-0.90.0 superblock.
5341 * The newer usage is when assembling an array.
5342 * In this case raid_disks will be 0, and the major_version field is
5343 * use to determine which style super-blocks are to be found on the devices.
5344 * The minor and patch _version numbers are also kept incase the
5345 * super_block handler wishes to interpret them.
5347 static int set_array_info(mddev_t
* mddev
, mdu_array_info_t
*info
)
5350 if (info
->raid_disks
== 0) {
5351 /* just setting version number for superblock loading */
5352 if (info
->major_version
< 0 ||
5353 info
->major_version
>= ARRAY_SIZE(super_types
) ||
5354 super_types
[info
->major_version
].name
== NULL
) {
5355 /* maybe try to auto-load a module? */
5357 "md: superblock version %d not known\n",
5358 info
->major_version
);
5361 mddev
->major_version
= info
->major_version
;
5362 mddev
->minor_version
= info
->minor_version
;
5363 mddev
->patch_version
= info
->patch_version
;
5364 mddev
->persistent
= !info
->not_persistent
;
5365 /* ensure mddev_put doesn't delete this now that there
5366 * is some minimal configuration.
5368 mddev
->ctime
= get_seconds();
5371 mddev
->major_version
= MD_MAJOR_VERSION
;
5372 mddev
->minor_version
= MD_MINOR_VERSION
;
5373 mddev
->patch_version
= MD_PATCHLEVEL_VERSION
;
5374 mddev
->ctime
= get_seconds();
5376 mddev
->level
= info
->level
;
5377 mddev
->clevel
[0] = 0;
5378 mddev
->dev_sectors
= 2 * (sector_t
)info
->size
;
5379 mddev
->raid_disks
= info
->raid_disks
;
5380 /* don't set md_minor, it is determined by which /dev/md* was
5383 if (info
->state
& (1<<MD_SB_CLEAN
))
5384 mddev
->recovery_cp
= MaxSector
;
5386 mddev
->recovery_cp
= 0;
5387 mddev
->persistent
= ! info
->not_persistent
;
5388 mddev
->external
= 0;
5390 mddev
->layout
= info
->layout
;
5391 mddev
->chunk_sectors
= info
->chunk_size
>> 9;
5393 mddev
->max_disks
= MD_SB_DISKS
;
5395 if (mddev
->persistent
)
5397 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
5399 mddev
->bitmap_info
.default_offset
= MD_SB_BYTES
>> 9;
5400 mddev
->bitmap_info
.offset
= 0;
5402 mddev
->reshape_position
= MaxSector
;
5405 * Generate a 128 bit UUID
5407 get_random_bytes(mddev
->uuid
, 16);
5409 mddev
->new_level
= mddev
->level
;
5410 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
5411 mddev
->new_layout
= mddev
->layout
;
5412 mddev
->delta_disks
= 0;
5417 void md_set_array_sectors(mddev_t
*mddev
, sector_t array_sectors
)
5419 WARN(!mddev_is_locked(mddev
), "%s: unlocked mddev!\n", __func__
);
5421 if (mddev
->external_size
)
5424 mddev
->array_sectors
= array_sectors
;
5426 EXPORT_SYMBOL(md_set_array_sectors
);
5428 static int update_size(mddev_t
*mddev
, sector_t num_sectors
)
5432 int fit
= (num_sectors
== 0);
5434 if (mddev
->pers
->resize
== NULL
)
5436 /* The "num_sectors" is the number of sectors of each device that
5437 * is used. This can only make sense for arrays with redundancy.
5438 * linear and raid0 always use whatever space is available. We can only
5439 * consider changing this number if no resync or reconstruction is
5440 * happening, and if the new size is acceptable. It must fit before the
5441 * sb_start or, if that is <data_offset, it must fit before the size
5442 * of each device. If num_sectors is zero, we find the largest size
5446 if (mddev
->sync_thread
)
5449 /* Sorry, cannot grow a bitmap yet, just remove it,
5453 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
5454 sector_t avail
= rdev
->sectors
;
5456 if (fit
&& (num_sectors
== 0 || num_sectors
> avail
))
5457 num_sectors
= avail
;
5458 if (avail
< num_sectors
)
5461 rv
= mddev
->pers
->resize(mddev
, num_sectors
);
5463 revalidate_disk(mddev
->gendisk
);
5467 static int update_raid_disks(mddev_t
*mddev
, int raid_disks
)
5470 /* change the number of raid disks */
5471 if (mddev
->pers
->check_reshape
== NULL
)
5473 if (raid_disks
<= 0 ||
5474 (mddev
->max_disks
&& raid_disks
>= mddev
->max_disks
))
5476 if (mddev
->sync_thread
|| mddev
->reshape_position
!= MaxSector
)
5478 mddev
->delta_disks
= raid_disks
- mddev
->raid_disks
;
5480 rv
= mddev
->pers
->check_reshape(mddev
);
5486 * update_array_info is used to change the configuration of an
5488 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
5489 * fields in the info are checked against the array.
5490 * Any differences that cannot be handled will cause an error.
5491 * Normally, only one change can be managed at a time.
5493 static int update_array_info(mddev_t
*mddev
, mdu_array_info_t
*info
)
5499 /* calculate expected state,ignoring low bits */
5500 if (mddev
->bitmap
&& mddev
->bitmap_info
.offset
)
5501 state
|= (1 << MD_SB_BITMAP_PRESENT
);
5503 if (mddev
->major_version
!= info
->major_version
||
5504 mddev
->minor_version
!= info
->minor_version
||
5505 /* mddev->patch_version != info->patch_version || */
5506 mddev
->ctime
!= info
->ctime
||
5507 mddev
->level
!= info
->level
||
5508 /* mddev->layout != info->layout || */
5509 !mddev
->persistent
!= info
->not_persistent
||
5510 mddev
->chunk_sectors
!= info
->chunk_size
>> 9 ||
5511 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
5512 ((state
^info
->state
) & 0xfffffe00)
5515 /* Check there is only one change */
5516 if (info
->size
>= 0 && mddev
->dev_sectors
/ 2 != info
->size
)
5518 if (mddev
->raid_disks
!= info
->raid_disks
)
5520 if (mddev
->layout
!= info
->layout
)
5522 if ((state
^ info
->state
) & (1<<MD_SB_BITMAP_PRESENT
))
5529 if (mddev
->layout
!= info
->layout
) {
5531 * we don't need to do anything at the md level, the
5532 * personality will take care of it all.
5534 if (mddev
->pers
->check_reshape
== NULL
)
5537 mddev
->new_layout
= info
->layout
;
5538 rv
= mddev
->pers
->check_reshape(mddev
);
5540 mddev
->new_layout
= mddev
->layout
;
5544 if (info
->size
>= 0 && mddev
->dev_sectors
/ 2 != info
->size
)
5545 rv
= update_size(mddev
, (sector_t
)info
->size
* 2);
5547 if (mddev
->raid_disks
!= info
->raid_disks
)
5548 rv
= update_raid_disks(mddev
, info
->raid_disks
);
5550 if ((state
^ info
->state
) & (1<<MD_SB_BITMAP_PRESENT
)) {
5551 if (mddev
->pers
->quiesce
== NULL
)
5553 if (mddev
->recovery
|| mddev
->sync_thread
)
5555 if (info
->state
& (1<<MD_SB_BITMAP_PRESENT
)) {
5556 /* add the bitmap */
5559 if (mddev
->bitmap_info
.default_offset
== 0)
5561 mddev
->bitmap_info
.offset
=
5562 mddev
->bitmap_info
.default_offset
;
5563 mddev
->pers
->quiesce(mddev
, 1);
5564 rv
= bitmap_create(mddev
);
5566 rv
= bitmap_load(mddev
);
5568 bitmap_destroy(mddev
);
5569 mddev
->pers
->quiesce(mddev
, 0);
5571 /* remove the bitmap */
5574 if (mddev
->bitmap
->file
)
5576 mddev
->pers
->quiesce(mddev
, 1);
5577 bitmap_destroy(mddev
);
5578 mddev
->pers
->quiesce(mddev
, 0);
5579 mddev
->bitmap_info
.offset
= 0;
5582 md_update_sb(mddev
, 1);
5586 static int set_disk_faulty(mddev_t
*mddev
, dev_t dev
)
5590 if (mddev
->pers
== NULL
)
5593 rdev
= find_rdev(mddev
, dev
);
5597 md_error(mddev
, rdev
);
5602 * We have a problem here : there is no easy way to give a CHS
5603 * virtual geometry. We currently pretend that we have a 2 heads
5604 * 4 sectors (with a BIG number of cylinders...). This drives
5605 * dosfs just mad... ;-)
5607 static int md_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
5609 mddev_t
*mddev
= bdev
->bd_disk
->private_data
;
5613 geo
->cylinders
= mddev
->array_sectors
/ 8;
5617 static int md_ioctl(struct block_device
*bdev
, fmode_t mode
,
5618 unsigned int cmd
, unsigned long arg
)
5621 void __user
*argp
= (void __user
*)arg
;
5622 mddev_t
*mddev
= NULL
;
5625 if (!capable(CAP_SYS_ADMIN
))
5629 * Commands dealing with the RAID driver but not any
5635 err
= get_version(argp
);
5638 case PRINT_RAID_DEBUG
:
5646 autostart_arrays(arg
);
5653 * Commands creating/starting a new array:
5656 mddev
= bdev
->bd_disk
->private_data
;
5663 err
= mddev_lock(mddev
);
5666 "md: ioctl lock interrupted, reason %d, cmd %d\n",
5673 case SET_ARRAY_INFO
:
5675 mdu_array_info_t info
;
5677 memset(&info
, 0, sizeof(info
));
5678 else if (copy_from_user(&info
, argp
, sizeof(info
))) {
5683 err
= update_array_info(mddev
, &info
);
5685 printk(KERN_WARNING
"md: couldn't update"
5686 " array info. %d\n", err
);
5691 if (!list_empty(&mddev
->disks
)) {
5693 "md: array %s already has disks!\n",
5698 if (mddev
->raid_disks
) {
5700 "md: array %s already initialised!\n",
5705 err
= set_array_info(mddev
, &info
);
5707 printk(KERN_WARNING
"md: couldn't set"
5708 " array info. %d\n", err
);
5718 * Commands querying/configuring an existing array:
5720 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
5721 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
5722 if ((!mddev
->raid_disks
&& !mddev
->external
)
5723 && cmd
!= ADD_NEW_DISK
&& cmd
!= STOP_ARRAY
5724 && cmd
!= RUN_ARRAY
&& cmd
!= SET_BITMAP_FILE
5725 && cmd
!= GET_BITMAP_FILE
) {
5731 * Commands even a read-only array can execute:
5735 case GET_ARRAY_INFO
:
5736 err
= get_array_info(mddev
, argp
);
5739 case GET_BITMAP_FILE
:
5740 err
= get_bitmap_file(mddev
, argp
);
5744 err
= get_disk_info(mddev
, argp
);
5747 case RESTART_ARRAY_RW
:
5748 err
= restart_array(mddev
);
5752 err
= do_md_stop(mddev
, 0, 1);
5756 err
= md_set_readonly(mddev
, 1);
5760 if (get_user(ro
, (int __user
*)(arg
))) {
5766 /* if the bdev is going readonly the value of mddev->ro
5767 * does not matter, no writes are coming
5772 /* are we are already prepared for writes? */
5776 /* transitioning to readauto need only happen for
5777 * arrays that call md_write_start
5780 err
= restart_array(mddev
);
5783 set_disk_ro(mddev
->gendisk
, 0);
5790 * The remaining ioctls are changing the state of the
5791 * superblock, so we do not allow them on read-only arrays.
5792 * However non-MD ioctls (e.g. get-size) will still come through
5793 * here and hit the 'default' below, so only disallow
5794 * 'md' ioctls, and switch to rw mode if started auto-readonly.
5796 if (_IOC_TYPE(cmd
) == MD_MAJOR
&& mddev
->ro
&& mddev
->pers
) {
5797 if (mddev
->ro
== 2) {
5799 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
5800 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5801 md_wakeup_thread(mddev
->thread
);
5812 mdu_disk_info_t info
;
5813 if (copy_from_user(&info
, argp
, sizeof(info
)))
5816 err
= add_new_disk(mddev
, &info
);
5820 case HOT_REMOVE_DISK
:
5821 err
= hot_remove_disk(mddev
, new_decode_dev(arg
));
5825 err
= hot_add_disk(mddev
, new_decode_dev(arg
));
5828 case SET_DISK_FAULTY
:
5829 err
= set_disk_faulty(mddev
, new_decode_dev(arg
));
5833 err
= do_md_run(mddev
);
5836 case SET_BITMAP_FILE
:
5837 err
= set_bitmap_file(mddev
, (int)arg
);
5847 if (mddev
->hold_active
== UNTIL_IOCTL
&&
5849 mddev
->hold_active
= 0;
5850 mddev_unlock(mddev
);
5859 #ifdef CONFIG_COMPAT
5860 static int md_compat_ioctl(struct block_device
*bdev
, fmode_t mode
,
5861 unsigned int cmd
, unsigned long arg
)
5864 case HOT_REMOVE_DISK
:
5866 case SET_DISK_FAULTY
:
5867 case SET_BITMAP_FILE
:
5868 /* These take in integer arg, do not convert */
5871 arg
= (unsigned long)compat_ptr(arg
);
5875 return md_ioctl(bdev
, mode
, cmd
, arg
);
5877 #endif /* CONFIG_COMPAT */
5879 static int md_open(struct block_device
*bdev
, fmode_t mode
)
5882 * Succeed if we can lock the mddev, which confirms that
5883 * it isn't being stopped right now.
5885 mddev_t
*mddev
= mddev_find(bdev
->bd_dev
);
5888 mutex_lock(&md_mutex
);
5889 if (mddev
->gendisk
!= bdev
->bd_disk
) {
5890 /* we are racing with mddev_put which is discarding this
5894 /* Wait until bdev->bd_disk is definitely gone */
5895 flush_scheduled_work();
5896 /* Then retry the open from the top */
5897 mutex_unlock(&md_mutex
);
5898 return -ERESTARTSYS
;
5900 BUG_ON(mddev
!= bdev
->bd_disk
->private_data
);
5902 if ((err
= mutex_lock_interruptible(&mddev
->open_mutex
)))
5906 atomic_inc(&mddev
->openers
);
5907 mutex_unlock(&mddev
->open_mutex
);
5909 check_disk_size_change(mddev
->gendisk
, bdev
);
5911 mutex_unlock(&md_mutex
);
5915 static int md_release(struct gendisk
*disk
, fmode_t mode
)
5917 mddev_t
*mddev
= disk
->private_data
;
5920 mutex_lock(&md_mutex
);
5921 atomic_dec(&mddev
->openers
);
5923 mutex_unlock(&md_mutex
);
5927 static const struct block_device_operations md_fops
=
5929 .owner
= THIS_MODULE
,
5931 .release
= md_release
,
5933 #ifdef CONFIG_COMPAT
5934 .compat_ioctl
= md_compat_ioctl
,
5936 .getgeo
= md_getgeo
,
5939 static int md_thread(void * arg
)
5941 mdk_thread_t
*thread
= arg
;
5944 * md_thread is a 'system-thread', it's priority should be very
5945 * high. We avoid resource deadlocks individually in each
5946 * raid personality. (RAID5 does preallocation) We also use RR and
5947 * the very same RT priority as kswapd, thus we will never get
5948 * into a priority inversion deadlock.
5950 * we definitely have to have equal or higher priority than
5951 * bdflush, otherwise bdflush will deadlock if there are too
5952 * many dirty RAID5 blocks.
5955 allow_signal(SIGKILL
);
5956 while (!kthread_should_stop()) {
5958 /* We need to wait INTERRUPTIBLE so that
5959 * we don't add to the load-average.
5960 * That means we need to be sure no signals are
5963 if (signal_pending(current
))
5964 flush_signals(current
);
5966 wait_event_interruptible_timeout
5968 test_bit(THREAD_WAKEUP
, &thread
->flags
)
5969 || kthread_should_stop(),
5972 clear_bit(THREAD_WAKEUP
, &thread
->flags
);
5974 thread
->run(thread
->mddev
);
5980 void md_wakeup_thread(mdk_thread_t
*thread
)
5983 dprintk("md: waking up MD thread %s.\n", thread
->tsk
->comm
);
5984 set_bit(THREAD_WAKEUP
, &thread
->flags
);
5985 wake_up(&thread
->wqueue
);
5989 mdk_thread_t
*md_register_thread(void (*run
) (mddev_t
*), mddev_t
*mddev
,
5992 mdk_thread_t
*thread
;
5994 thread
= kzalloc(sizeof(mdk_thread_t
), GFP_KERNEL
);
5998 init_waitqueue_head(&thread
->wqueue
);
6001 thread
->mddev
= mddev
;
6002 thread
->timeout
= MAX_SCHEDULE_TIMEOUT
;
6003 thread
->tsk
= kthread_run(md_thread
, thread
,
6005 mdname(thread
->mddev
),
6006 name
?: mddev
->pers
->name
);
6007 if (IS_ERR(thread
->tsk
)) {
6014 void md_unregister_thread(mdk_thread_t
*thread
)
6018 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread
->tsk
));
6020 kthread_stop(thread
->tsk
);
6024 void md_error(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
6031 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
))
6034 if (mddev
->external
)
6035 set_bit(Blocked
, &rdev
->flags
);
6037 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
6039 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
6040 __builtin_return_address(0),__builtin_return_address(1),
6041 __builtin_return_address(2),__builtin_return_address(3));
6045 if (!mddev
->pers
->error_handler
)
6047 mddev
->pers
->error_handler(mddev
,rdev
);
6048 if (mddev
->degraded
)
6049 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
6050 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
6051 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6052 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6053 md_wakeup_thread(mddev
->thread
);
6054 if (mddev
->event_work
.func
)
6055 schedule_work(&mddev
->event_work
);
6056 md_new_event_inintr(mddev
);
6059 /* seq_file implementation /proc/mdstat */
6061 static void status_unused(struct seq_file
*seq
)
6066 seq_printf(seq
, "unused devices: ");
6068 list_for_each_entry(rdev
, &pending_raid_disks
, same_set
) {
6069 char b
[BDEVNAME_SIZE
];
6071 seq_printf(seq
, "%s ",
6072 bdevname(rdev
->bdev
,b
));
6075 seq_printf(seq
, "<none>");
6077 seq_printf(seq
, "\n");
6081 static void status_resync(struct seq_file
*seq
, mddev_t
* mddev
)
6083 sector_t max_sectors
, resync
, res
;
6084 unsigned long dt
, db
;
6087 unsigned int per_milli
;
6089 resync
= mddev
->curr_resync
- atomic_read(&mddev
->recovery_active
);
6091 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
6092 max_sectors
= mddev
->resync_max_sectors
;
6094 max_sectors
= mddev
->dev_sectors
;
6097 * Should not happen.
6103 /* Pick 'scale' such that (resync>>scale)*1000 will fit
6104 * in a sector_t, and (max_sectors>>scale) will fit in a
6105 * u32, as those are the requirements for sector_div.
6106 * Thus 'scale' must be at least 10
6109 if (sizeof(sector_t
) > sizeof(unsigned long)) {
6110 while ( max_sectors
/2 > (1ULL<<(scale
+32)))
6113 res
= (resync
>>scale
)*1000;
6114 sector_div(res
, (u32
)((max_sectors
>>scale
)+1));
6118 int i
, x
= per_milli
/50, y
= 20-x
;
6119 seq_printf(seq
, "[");
6120 for (i
= 0; i
< x
; i
++)
6121 seq_printf(seq
, "=");
6122 seq_printf(seq
, ">");
6123 for (i
= 0; i
< y
; i
++)
6124 seq_printf(seq
, ".");
6125 seq_printf(seq
, "] ");
6127 seq_printf(seq
, " %s =%3u.%u%% (%llu/%llu)",
6128 (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
)?
6130 (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
)?
6132 (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ?
6133 "resync" : "recovery"))),
6134 per_milli
/10, per_milli
% 10,
6135 (unsigned long long) resync
/2,
6136 (unsigned long long) max_sectors
/2);
6139 * dt: time from mark until now
6140 * db: blocks written from mark until now
6141 * rt: remaining time
6143 * rt is a sector_t, so could be 32bit or 64bit.
6144 * So we divide before multiply in case it is 32bit and close
6146 * We scale the divisor (db) by 32 to avoid loosing precision
6147 * near the end of resync when the number of remaining sectors
6149 * We then divide rt by 32 after multiplying by db to compensate.
6150 * The '+1' avoids division by zero if db is very small.
6152 dt
= ((jiffies
- mddev
->resync_mark
) / HZ
);
6154 db
= (mddev
->curr_mark_cnt
- atomic_read(&mddev
->recovery_active
))
6155 - mddev
->resync_mark_cnt
;
6157 rt
= max_sectors
- resync
; /* number of remaining sectors */
6158 sector_div(rt
, db
/32+1);
6162 seq_printf(seq
, " finish=%lu.%lumin", (unsigned long)rt
/ 60,
6163 ((unsigned long)rt
% 60)/6);
6165 seq_printf(seq
, " speed=%ldK/sec", db
/2/dt
);
6168 static void *md_seq_start(struct seq_file
*seq
, loff_t
*pos
)
6170 struct list_head
*tmp
;
6180 spin_lock(&all_mddevs_lock
);
6181 list_for_each(tmp
,&all_mddevs
)
6183 mddev
= list_entry(tmp
, mddev_t
, all_mddevs
);
6185 spin_unlock(&all_mddevs_lock
);
6188 spin_unlock(&all_mddevs_lock
);
6190 return (void*)2;/* tail */
6194 static void *md_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
6196 struct list_head
*tmp
;
6197 mddev_t
*next_mddev
, *mddev
= v
;
6203 spin_lock(&all_mddevs_lock
);
6205 tmp
= all_mddevs
.next
;
6207 tmp
= mddev
->all_mddevs
.next
;
6208 if (tmp
!= &all_mddevs
)
6209 next_mddev
= mddev_get(list_entry(tmp
,mddev_t
,all_mddevs
));
6211 next_mddev
= (void*)2;
6214 spin_unlock(&all_mddevs_lock
);
6222 static void md_seq_stop(struct seq_file
*seq
, void *v
)
6226 if (mddev
&& v
!= (void*)1 && v
!= (void*)2)
6230 struct mdstat_info
{
6234 static int md_seq_show(struct seq_file
*seq
, void *v
)
6239 struct mdstat_info
*mi
= seq
->private;
6240 struct bitmap
*bitmap
;
6242 if (v
== (void*)1) {
6243 struct mdk_personality
*pers
;
6244 seq_printf(seq
, "Personalities : ");
6245 spin_lock(&pers_lock
);
6246 list_for_each_entry(pers
, &pers_list
, list
)
6247 seq_printf(seq
, "[%s] ", pers
->name
);
6249 spin_unlock(&pers_lock
);
6250 seq_printf(seq
, "\n");
6251 mi
->event
= atomic_read(&md_event_count
);
6254 if (v
== (void*)2) {
6259 if (mddev_lock(mddev
) < 0)
6262 if (mddev
->pers
|| mddev
->raid_disks
|| !list_empty(&mddev
->disks
)) {
6263 seq_printf(seq
, "%s : %sactive", mdname(mddev
),
6264 mddev
->pers
? "" : "in");
6267 seq_printf(seq
, " (read-only)");
6269 seq_printf(seq
, " (auto-read-only)");
6270 seq_printf(seq
, " %s", mddev
->pers
->name
);
6274 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
6275 char b
[BDEVNAME_SIZE
];
6276 seq_printf(seq
, " %s[%d]",
6277 bdevname(rdev
->bdev
,b
), rdev
->desc_nr
);
6278 if (test_bit(WriteMostly
, &rdev
->flags
))
6279 seq_printf(seq
, "(W)");
6280 if (test_bit(Faulty
, &rdev
->flags
)) {
6281 seq_printf(seq
, "(F)");
6283 } else if (rdev
->raid_disk
< 0)
6284 seq_printf(seq
, "(S)"); /* spare */
6285 sectors
+= rdev
->sectors
;
6288 if (!list_empty(&mddev
->disks
)) {
6290 seq_printf(seq
, "\n %llu blocks",
6291 (unsigned long long)
6292 mddev
->array_sectors
/ 2);
6294 seq_printf(seq
, "\n %llu blocks",
6295 (unsigned long long)sectors
/ 2);
6297 if (mddev
->persistent
) {
6298 if (mddev
->major_version
!= 0 ||
6299 mddev
->minor_version
!= 90) {
6300 seq_printf(seq
," super %d.%d",
6301 mddev
->major_version
,
6302 mddev
->minor_version
);
6304 } else if (mddev
->external
)
6305 seq_printf(seq
, " super external:%s",
6306 mddev
->metadata_type
);
6308 seq_printf(seq
, " super non-persistent");
6311 mddev
->pers
->status(seq
, mddev
);
6312 seq_printf(seq
, "\n ");
6313 if (mddev
->pers
->sync_request
) {
6314 if (mddev
->curr_resync
> 2) {
6315 status_resync(seq
, mddev
);
6316 seq_printf(seq
, "\n ");
6317 } else if (mddev
->curr_resync
== 1 || mddev
->curr_resync
== 2)
6318 seq_printf(seq
, "\tresync=DELAYED\n ");
6319 else if (mddev
->recovery_cp
< MaxSector
)
6320 seq_printf(seq
, "\tresync=PENDING\n ");
6323 seq_printf(seq
, "\n ");
6325 if ((bitmap
= mddev
->bitmap
)) {
6326 unsigned long chunk_kb
;
6327 unsigned long flags
;
6328 spin_lock_irqsave(&bitmap
->lock
, flags
);
6329 chunk_kb
= mddev
->bitmap_info
.chunksize
>> 10;
6330 seq_printf(seq
, "bitmap: %lu/%lu pages [%luKB], "
6332 bitmap
->pages
- bitmap
->missing_pages
,
6334 (bitmap
->pages
- bitmap
->missing_pages
)
6335 << (PAGE_SHIFT
- 10),
6336 chunk_kb
? chunk_kb
: mddev
->bitmap_info
.chunksize
,
6337 chunk_kb
? "KB" : "B");
6339 seq_printf(seq
, ", file: ");
6340 seq_path(seq
, &bitmap
->file
->f_path
, " \t\n");
6343 seq_printf(seq
, "\n");
6344 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
6347 seq_printf(seq
, "\n");
6349 mddev_unlock(mddev
);
6354 static const struct seq_operations md_seq_ops
= {
6355 .start
= md_seq_start
,
6356 .next
= md_seq_next
,
6357 .stop
= md_seq_stop
,
6358 .show
= md_seq_show
,
6361 static int md_seq_open(struct inode
*inode
, struct file
*file
)
6364 struct mdstat_info
*mi
= kmalloc(sizeof(*mi
), GFP_KERNEL
);
6368 error
= seq_open(file
, &md_seq_ops
);
6372 struct seq_file
*p
= file
->private_data
;
6374 mi
->event
= atomic_read(&md_event_count
);
6379 static unsigned int mdstat_poll(struct file
*filp
, poll_table
*wait
)
6381 struct seq_file
*m
= filp
->private_data
;
6382 struct mdstat_info
*mi
= m
->private;
6385 poll_wait(filp
, &md_event_waiters
, wait
);
6387 /* always allow read */
6388 mask
= POLLIN
| POLLRDNORM
;
6390 if (mi
->event
!= atomic_read(&md_event_count
))
6391 mask
|= POLLERR
| POLLPRI
;
6395 static const struct file_operations md_seq_fops
= {
6396 .owner
= THIS_MODULE
,
6397 .open
= md_seq_open
,
6399 .llseek
= seq_lseek
,
6400 .release
= seq_release_private
,
6401 .poll
= mdstat_poll
,
6404 int register_md_personality(struct mdk_personality
*p
)
6406 spin_lock(&pers_lock
);
6407 list_add_tail(&p
->list
, &pers_list
);
6408 printk(KERN_INFO
"md: %s personality registered for level %d\n", p
->name
, p
->level
);
6409 spin_unlock(&pers_lock
);
6413 int unregister_md_personality(struct mdk_personality
*p
)
6415 printk(KERN_INFO
"md: %s personality unregistered\n", p
->name
);
6416 spin_lock(&pers_lock
);
6417 list_del_init(&p
->list
);
6418 spin_unlock(&pers_lock
);
6422 static int is_mddev_idle(mddev_t
*mddev
, int init
)
6430 rdev_for_each_rcu(rdev
, mddev
) {
6431 struct gendisk
*disk
= rdev
->bdev
->bd_contains
->bd_disk
;
6432 curr_events
= (int)part_stat_read(&disk
->part0
, sectors
[0]) +
6433 (int)part_stat_read(&disk
->part0
, sectors
[1]) -
6434 atomic_read(&disk
->sync_io
);
6435 /* sync IO will cause sync_io to increase before the disk_stats
6436 * as sync_io is counted when a request starts, and
6437 * disk_stats is counted when it completes.
6438 * So resync activity will cause curr_events to be smaller than
6439 * when there was no such activity.
6440 * non-sync IO will cause disk_stat to increase without
6441 * increasing sync_io so curr_events will (eventually)
6442 * be larger than it was before. Once it becomes
6443 * substantially larger, the test below will cause
6444 * the array to appear non-idle, and resync will slow
6446 * If there is a lot of outstanding resync activity when
6447 * we set last_event to curr_events, then all that activity
6448 * completing might cause the array to appear non-idle
6449 * and resync will be slowed down even though there might
6450 * not have been non-resync activity. This will only
6451 * happen once though. 'last_events' will soon reflect
6452 * the state where there is little or no outstanding
6453 * resync requests, and further resync activity will
6454 * always make curr_events less than last_events.
6457 if (init
|| curr_events
- rdev
->last_events
> 64) {
6458 rdev
->last_events
= curr_events
;
6466 void md_done_sync(mddev_t
*mddev
, int blocks
, int ok
)
6468 /* another "blocks" (512byte) blocks have been synced */
6469 atomic_sub(blocks
, &mddev
->recovery_active
);
6470 wake_up(&mddev
->recovery_wait
);
6472 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6473 md_wakeup_thread(mddev
->thread
);
6474 // stop recovery, signal do_sync ....
6479 /* md_write_start(mddev, bi)
6480 * If we need to update some array metadata (e.g. 'active' flag
6481 * in superblock) before writing, schedule a superblock update
6482 * and wait for it to complete.
6484 void md_write_start(mddev_t
*mddev
, struct bio
*bi
)
6487 if (bio_data_dir(bi
) != WRITE
)
6490 BUG_ON(mddev
->ro
== 1);
6491 if (mddev
->ro
== 2) {
6492 /* need to switch to read/write */
6494 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6495 md_wakeup_thread(mddev
->thread
);
6496 md_wakeup_thread(mddev
->sync_thread
);
6499 atomic_inc(&mddev
->writes_pending
);
6500 if (mddev
->safemode
== 1)
6501 mddev
->safemode
= 0;
6502 if (mddev
->in_sync
) {
6503 spin_lock_irq(&mddev
->write_lock
);
6504 if (mddev
->in_sync
) {
6506 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
6507 set_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
6508 md_wakeup_thread(mddev
->thread
);
6511 spin_unlock_irq(&mddev
->write_lock
);
6514 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
6515 wait_event(mddev
->sb_wait
,
6516 !test_bit(MD_CHANGE_PENDING
, &mddev
->flags
));
6519 void md_write_end(mddev_t
*mddev
)
6521 if (atomic_dec_and_test(&mddev
->writes_pending
)) {
6522 if (mddev
->safemode
== 2)
6523 md_wakeup_thread(mddev
->thread
);
6524 else if (mddev
->safemode_delay
)
6525 mod_timer(&mddev
->safemode_timer
, jiffies
+ mddev
->safemode_delay
);
6529 /* md_allow_write(mddev)
6530 * Calling this ensures that the array is marked 'active' so that writes
6531 * may proceed without blocking. It is important to call this before
6532 * attempting a GFP_KERNEL allocation while holding the mddev lock.
6533 * Must be called with mddev_lock held.
6535 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
6536 * is dropped, so return -EAGAIN after notifying userspace.
6538 int md_allow_write(mddev_t
*mddev
)
6544 if (!mddev
->pers
->sync_request
)
6547 spin_lock_irq(&mddev
->write_lock
);
6548 if (mddev
->in_sync
) {
6550 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
6551 set_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
6552 if (mddev
->safemode_delay
&&
6553 mddev
->safemode
== 0)
6554 mddev
->safemode
= 1;
6555 spin_unlock_irq(&mddev
->write_lock
);
6556 md_update_sb(mddev
, 0);
6557 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
6559 spin_unlock_irq(&mddev
->write_lock
);
6561 if (test_bit(MD_CHANGE_PENDING
, &mddev
->flags
))
6566 EXPORT_SYMBOL_GPL(md_allow_write
);
6568 void md_unplug(mddev_t
*mddev
)
6571 blk_unplug(mddev
->queue
);
6573 mddev
->plug
->unplug_fn(mddev
->plug
);
6576 #define SYNC_MARKS 10
6577 #define SYNC_MARK_STEP (3*HZ)
6578 void md_do_sync(mddev_t
*mddev
)
6581 unsigned int currspeed
= 0,
6583 sector_t max_sectors
,j
, io_sectors
;
6584 unsigned long mark
[SYNC_MARKS
];
6585 sector_t mark_cnt
[SYNC_MARKS
];
6587 struct list_head
*tmp
;
6588 sector_t last_check
;
6593 /* just incase thread restarts... */
6594 if (test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
))
6596 if (mddev
->ro
) /* never try to sync a read-only array */
6599 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
6600 if (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
))
6601 desc
= "data-check";
6602 else if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
6603 desc
= "requested-resync";
6606 } else if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
6611 /* we overload curr_resync somewhat here.
6612 * 0 == not engaged in resync at all
6613 * 2 == checking that there is no conflict with another sync
6614 * 1 == like 2, but have yielded to allow conflicting resync to
6616 * other == active in resync - this many blocks
6618 * Before starting a resync we must have set curr_resync to
6619 * 2, and then checked that every "conflicting" array has curr_resync
6620 * less than ours. When we find one that is the same or higher
6621 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
6622 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
6623 * This will mean we have to start checking from the beginning again.
6628 mddev
->curr_resync
= 2;
6631 if (kthread_should_stop())
6632 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6634 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
6636 for_each_mddev(mddev2
, tmp
) {
6637 if (mddev2
== mddev
)
6639 if (!mddev
->parallel_resync
6640 && mddev2
->curr_resync
6641 && match_mddev_units(mddev
, mddev2
)) {
6643 if (mddev
< mddev2
&& mddev
->curr_resync
== 2) {
6644 /* arbitrarily yield */
6645 mddev
->curr_resync
= 1;
6646 wake_up(&resync_wait
);
6648 if (mddev
> mddev2
&& mddev
->curr_resync
== 1)
6649 /* no need to wait here, we can wait the next
6650 * time 'round when curr_resync == 2
6653 /* We need to wait 'interruptible' so as not to
6654 * contribute to the load average, and not to
6655 * be caught by 'softlockup'
6657 prepare_to_wait(&resync_wait
, &wq
, TASK_INTERRUPTIBLE
);
6658 if (!kthread_should_stop() &&
6659 mddev2
->curr_resync
>= mddev
->curr_resync
) {
6660 printk(KERN_INFO
"md: delaying %s of %s"
6661 " until %s has finished (they"
6662 " share one or more physical units)\n",
6663 desc
, mdname(mddev
), mdname(mddev2
));
6665 if (signal_pending(current
))
6666 flush_signals(current
);
6668 finish_wait(&resync_wait
, &wq
);
6671 finish_wait(&resync_wait
, &wq
);
6674 } while (mddev
->curr_resync
< 2);
6677 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
6678 /* resync follows the size requested by the personality,
6679 * which defaults to physical size, but can be virtual size
6681 max_sectors
= mddev
->resync_max_sectors
;
6682 mddev
->resync_mismatches
= 0;
6683 /* we don't use the checkpoint if there's a bitmap */
6684 if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
6685 j
= mddev
->resync_min
;
6686 else if (!mddev
->bitmap
)
6687 j
= mddev
->recovery_cp
;
6689 } else if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
6690 max_sectors
= mddev
->dev_sectors
;
6692 /* recovery follows the physical size of devices */
6693 max_sectors
= mddev
->dev_sectors
;
6696 list_for_each_entry_rcu(rdev
, &mddev
->disks
, same_set
)
6697 if (rdev
->raid_disk
>= 0 &&
6698 !test_bit(Faulty
, &rdev
->flags
) &&
6699 !test_bit(In_sync
, &rdev
->flags
) &&
6700 rdev
->recovery_offset
< j
)
6701 j
= rdev
->recovery_offset
;
6705 printk(KERN_INFO
"md: %s of RAID array %s\n", desc
, mdname(mddev
));
6706 printk(KERN_INFO
"md: minimum _guaranteed_ speed:"
6707 " %d KB/sec/disk.\n", speed_min(mddev
));
6708 printk(KERN_INFO
"md: using maximum available idle IO bandwidth "
6709 "(but not more than %d KB/sec) for %s.\n",
6710 speed_max(mddev
), desc
);
6712 is_mddev_idle(mddev
, 1); /* this initializes IO event counters */
6715 for (m
= 0; m
< SYNC_MARKS
; m
++) {
6717 mark_cnt
[m
] = io_sectors
;
6720 mddev
->resync_mark
= mark
[last_mark
];
6721 mddev
->resync_mark_cnt
= mark_cnt
[last_mark
];
6724 * Tune reconstruction:
6726 window
= 32*(PAGE_SIZE
/512);
6727 printk(KERN_INFO
"md: using %dk window, over a total of %llu blocks.\n",
6728 window
/2,(unsigned long long) max_sectors
/2);
6730 atomic_set(&mddev
->recovery_active
, 0);
6735 "md: resuming %s of %s from checkpoint.\n",
6736 desc
, mdname(mddev
));
6737 mddev
->curr_resync
= j
;
6739 mddev
->curr_resync_completed
= mddev
->curr_resync
;
6741 while (j
< max_sectors
) {
6746 if (!test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
6747 ((mddev
->curr_resync
> mddev
->curr_resync_completed
&&
6748 (mddev
->curr_resync
- mddev
->curr_resync_completed
)
6749 > (max_sectors
>> 4)) ||
6750 (j
- mddev
->curr_resync_completed
)*2
6751 >= mddev
->resync_max
- mddev
->curr_resync_completed
6753 /* time to update curr_resync_completed */
6755 wait_event(mddev
->recovery_wait
,
6756 atomic_read(&mddev
->recovery_active
) == 0);
6757 mddev
->curr_resync_completed
=
6759 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
6760 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
6763 while (j
>= mddev
->resync_max
&& !kthread_should_stop()) {
6764 /* As this condition is controlled by user-space,
6765 * we can block indefinitely, so use '_interruptible'
6766 * to avoid triggering warnings.
6768 flush_signals(current
); /* just in case */
6769 wait_event_interruptible(mddev
->recovery_wait
,
6770 mddev
->resync_max
> j
6771 || kthread_should_stop());
6774 if (kthread_should_stop())
6777 sectors
= mddev
->pers
->sync_request(mddev
, j
, &skipped
,
6778 currspeed
< speed_min(mddev
));
6780 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6784 if (!skipped
) { /* actual IO requested */
6785 io_sectors
+= sectors
;
6786 atomic_add(sectors
, &mddev
->recovery_active
);
6790 if (j
>1) mddev
->curr_resync
= j
;
6791 mddev
->curr_mark_cnt
= io_sectors
;
6792 if (last_check
== 0)
6793 /* this is the earliers that rebuilt will be
6794 * visible in /proc/mdstat
6796 md_new_event(mddev
);
6798 if (last_check
+ window
> io_sectors
|| j
== max_sectors
)
6801 last_check
= io_sectors
;
6803 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
6807 if (time_after_eq(jiffies
, mark
[last_mark
] + SYNC_MARK_STEP
)) {
6809 int next
= (last_mark
+1) % SYNC_MARKS
;
6811 mddev
->resync_mark
= mark
[next
];
6812 mddev
->resync_mark_cnt
= mark_cnt
[next
];
6813 mark
[next
] = jiffies
;
6814 mark_cnt
[next
] = io_sectors
- atomic_read(&mddev
->recovery_active
);
6819 if (kthread_should_stop())
6824 * this loop exits only if either when we are slower than
6825 * the 'hard' speed limit, or the system was IO-idle for
6827 * the system might be non-idle CPU-wise, but we only care
6828 * about not overloading the IO subsystem. (things like an
6829 * e2fsck being done on the RAID array should execute fast)
6834 currspeed
= ((unsigned long)(io_sectors
-mddev
->resync_mark_cnt
))/2
6835 /((jiffies
-mddev
->resync_mark
)/HZ
+1) +1;
6837 if (currspeed
> speed_min(mddev
)) {
6838 if ((currspeed
> speed_max(mddev
)) ||
6839 !is_mddev_idle(mddev
, 0)) {
6845 printk(KERN_INFO
"md: %s: %s done.\n",mdname(mddev
), desc
);
6847 * this also signals 'finished resyncing' to md_stop
6852 wait_event(mddev
->recovery_wait
, !atomic_read(&mddev
->recovery_active
));
6854 /* tell personality that we are finished */
6855 mddev
->pers
->sync_request(mddev
, max_sectors
, &skipped
, 1);
6857 if (!test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
) &&
6858 mddev
->curr_resync
> 2) {
6859 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
6860 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
6861 if (mddev
->curr_resync
>= mddev
->recovery_cp
) {
6863 "md: checkpointing %s of %s.\n",
6864 desc
, mdname(mddev
));
6865 mddev
->recovery_cp
= mddev
->curr_resync
;
6868 mddev
->recovery_cp
= MaxSector
;
6870 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
6871 mddev
->curr_resync
= MaxSector
;
6873 list_for_each_entry_rcu(rdev
, &mddev
->disks
, same_set
)
6874 if (rdev
->raid_disk
>= 0 &&
6875 mddev
->delta_disks
>= 0 &&
6876 !test_bit(Faulty
, &rdev
->flags
) &&
6877 !test_bit(In_sync
, &rdev
->flags
) &&
6878 rdev
->recovery_offset
< mddev
->curr_resync
)
6879 rdev
->recovery_offset
= mddev
->curr_resync
;
6883 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
6886 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
6887 /* We completed so min/max setting can be forgotten if used. */
6888 if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
6889 mddev
->resync_min
= 0;
6890 mddev
->resync_max
= MaxSector
;
6891 } else if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
6892 mddev
->resync_min
= mddev
->curr_resync_completed
;
6893 mddev
->curr_resync
= 0;
6894 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
6895 mddev
->curr_resync_completed
= 0;
6896 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
6897 wake_up(&resync_wait
);
6898 set_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
6899 md_wakeup_thread(mddev
->thread
);
6904 * got a signal, exit.
6907 "md: md_do_sync() got signal ... exiting\n");
6908 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6912 EXPORT_SYMBOL_GPL(md_do_sync
);
6915 static int remove_and_add_spares(mddev_t
*mddev
)
6920 mddev
->curr_resync_completed
= 0;
6922 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
6923 if (rdev
->raid_disk
>= 0 &&
6924 !test_bit(Blocked
, &rdev
->flags
) &&
6925 (test_bit(Faulty
, &rdev
->flags
) ||
6926 ! test_bit(In_sync
, &rdev
->flags
)) &&
6927 atomic_read(&rdev
->nr_pending
)==0) {
6928 if (mddev
->pers
->hot_remove_disk(
6929 mddev
, rdev
->raid_disk
)==0) {
6931 sprintf(nm
,"rd%d", rdev
->raid_disk
);
6932 sysfs_remove_link(&mddev
->kobj
, nm
);
6933 rdev
->raid_disk
= -1;
6937 if (mddev
->degraded
&& ! mddev
->ro
&& !mddev
->recovery_disabled
) {
6938 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
6939 if (rdev
->raid_disk
>= 0 &&
6940 !test_bit(In_sync
, &rdev
->flags
) &&
6941 !test_bit(Blocked
, &rdev
->flags
))
6943 if (rdev
->raid_disk
< 0
6944 && !test_bit(Faulty
, &rdev
->flags
)) {
6945 rdev
->recovery_offset
= 0;
6947 hot_add_disk(mddev
, rdev
) == 0) {
6949 sprintf(nm
, "rd%d", rdev
->raid_disk
);
6950 if (sysfs_create_link(&mddev
->kobj
,
6952 /* failure here is OK */;
6954 md_new_event(mddev
);
6955 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
6964 * This routine is regularly called by all per-raid-array threads to
6965 * deal with generic issues like resync and super-block update.
6966 * Raid personalities that don't have a thread (linear/raid0) do not
6967 * need this as they never do any recovery or update the superblock.
6969 * It does not do any resync itself, but rather "forks" off other threads
6970 * to do that as needed.
6971 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
6972 * "->recovery" and create a thread at ->sync_thread.
6973 * When the thread finishes it sets MD_RECOVERY_DONE
6974 * and wakeups up this thread which will reap the thread and finish up.
6975 * This thread also removes any faulty devices (with nr_pending == 0).
6977 * The overall approach is:
6978 * 1/ if the superblock needs updating, update it.
6979 * 2/ If a recovery thread is running, don't do anything else.
6980 * 3/ If recovery has finished, clean up, possibly marking spares active.
6981 * 4/ If there are any faulty devices, remove them.
6982 * 5/ If array is degraded, try to add spares devices
6983 * 6/ If array has spares or is not in-sync, start a resync thread.
6985 void md_check_recovery(mddev_t
*mddev
)
6991 bitmap_daemon_work(mddev
);
6996 if (signal_pending(current
)) {
6997 if (mddev
->pers
->sync_request
&& !mddev
->external
) {
6998 printk(KERN_INFO
"md: %s in immediate safe mode\n",
7000 mddev
->safemode
= 2;
7002 flush_signals(current
);
7005 if (mddev
->ro
&& !test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))
7008 (mddev
->flags
& ~ (1<<MD_CHANGE_PENDING
)) ||
7009 test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
) ||
7010 test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
) ||
7011 (mddev
->external
== 0 && mddev
->safemode
== 1) ||
7012 (mddev
->safemode
== 2 && ! atomic_read(&mddev
->writes_pending
)
7013 && !mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
)
7017 if (mddev_trylock(mddev
)) {
7021 /* Only thing we do on a ro array is remove
7024 remove_and_add_spares(mddev
);
7025 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7029 if (!mddev
->external
) {
7031 spin_lock_irq(&mddev
->write_lock
);
7032 if (mddev
->safemode
&&
7033 !atomic_read(&mddev
->writes_pending
) &&
7035 mddev
->recovery_cp
== MaxSector
) {
7038 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
7040 if (mddev
->safemode
== 1)
7041 mddev
->safemode
= 0;
7042 spin_unlock_irq(&mddev
->write_lock
);
7044 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
7048 md_update_sb(mddev
, 0);
7050 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) &&
7051 !test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
)) {
7052 /* resync/recovery still happening */
7053 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7056 if (mddev
->sync_thread
) {
7057 /* resync has finished, collect result */
7058 md_unregister_thread(mddev
->sync_thread
);
7059 mddev
->sync_thread
= NULL
;
7060 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
) &&
7061 !test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
)) {
7063 /* activate any spares */
7064 if (mddev
->pers
->spare_active(mddev
))
7065 sysfs_notify(&mddev
->kobj
, NULL
,
7068 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
7069 mddev
->pers
->finish_reshape
)
7070 mddev
->pers
->finish_reshape(mddev
);
7071 md_update_sb(mddev
, 1);
7073 /* if array is no-longer degraded, then any saved_raid_disk
7074 * information must be scrapped
7076 if (!mddev
->degraded
)
7077 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
7078 rdev
->saved_raid_disk
= -1;
7080 mddev
->recovery
= 0;
7081 /* flag recovery needed just to double check */
7082 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7083 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
7084 md_new_event(mddev
);
7087 /* Set RUNNING before clearing NEEDED to avoid
7088 * any transients in the value of "sync_action".
7090 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
7091 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7092 /* Clear some bits that don't mean anything, but
7095 clear_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
7096 clear_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
7098 if (test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
))
7100 /* no recovery is running.
7101 * remove any failed drives, then
7102 * add spares if possible.
7103 * Spare are also removed and re-added, to allow
7104 * the personality to fail the re-add.
7107 if (mddev
->reshape_position
!= MaxSector
) {
7108 if (mddev
->pers
->check_reshape
== NULL
||
7109 mddev
->pers
->check_reshape(mddev
) != 0)
7110 /* Cannot proceed */
7112 set_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
7113 clear_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
7114 } else if ((spares
= remove_and_add_spares(mddev
))) {
7115 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
7116 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
7117 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
7118 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
7119 } else if (mddev
->recovery_cp
< MaxSector
) {
7120 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
7121 clear_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
7122 } else if (!test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
7123 /* nothing to be done ... */
7126 if (mddev
->pers
->sync_request
) {
7127 if (spares
&& mddev
->bitmap
&& ! mddev
->bitmap
->file
) {
7128 /* We are adding a device or devices to an array
7129 * which has the bitmap stored on all devices.
7130 * So make sure all bitmap pages get written
7132 bitmap_write_all(mddev
->bitmap
);
7134 mddev
->sync_thread
= md_register_thread(md_do_sync
,
7137 if (!mddev
->sync_thread
) {
7138 printk(KERN_ERR
"%s: could not start resync"
7141 /* leave the spares where they are, it shouldn't hurt */
7142 mddev
->recovery
= 0;
7144 md_wakeup_thread(mddev
->sync_thread
);
7145 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
7146 md_new_event(mddev
);
7149 if (!mddev
->sync_thread
) {
7150 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
7151 if (test_and_clear_bit(MD_RECOVERY_RECOVER
,
7153 if (mddev
->sysfs_action
)
7154 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
7156 mddev_unlock(mddev
);
7160 void md_wait_for_blocked_rdev(mdk_rdev_t
*rdev
, mddev_t
*mddev
)
7162 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
7163 wait_event_timeout(rdev
->blocked_wait
,
7164 !test_bit(Blocked
, &rdev
->flags
),
7165 msecs_to_jiffies(5000));
7166 rdev_dec_pending(rdev
, mddev
);
7168 EXPORT_SYMBOL(md_wait_for_blocked_rdev
);
7170 static int md_notify_reboot(struct notifier_block
*this,
7171 unsigned long code
, void *x
)
7173 struct list_head
*tmp
;
7176 if ((code
== SYS_DOWN
) || (code
== SYS_HALT
) || (code
== SYS_POWER_OFF
)) {
7178 printk(KERN_INFO
"md: stopping all md devices.\n");
7180 for_each_mddev(mddev
, tmp
)
7181 if (mddev_trylock(mddev
)) {
7182 /* Force a switch to readonly even array
7183 * appears to still be in use. Hence
7186 md_set_readonly(mddev
, 100);
7187 mddev_unlock(mddev
);
7190 * certain more exotic SCSI devices are known to be
7191 * volatile wrt too early system reboots. While the
7192 * right place to handle this issue is the given
7193 * driver, we do want to have a safe RAID driver ...
7200 static struct notifier_block md_notifier
= {
7201 .notifier_call
= md_notify_reboot
,
7203 .priority
= INT_MAX
, /* before any real devices */
7206 static void md_geninit(void)
7208 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t
));
7210 proc_create("mdstat", S_IRUGO
, NULL
, &md_seq_fops
);
7213 static int __init
md_init(void)
7215 if (register_blkdev(MD_MAJOR
, "md"))
7217 if ((mdp_major
=register_blkdev(0, "mdp"))<=0) {
7218 unregister_blkdev(MD_MAJOR
, "md");
7221 blk_register_region(MKDEV(MD_MAJOR
, 0), 1UL<<MINORBITS
, THIS_MODULE
,
7222 md_probe
, NULL
, NULL
);
7223 blk_register_region(MKDEV(mdp_major
, 0), 1UL<<MINORBITS
, THIS_MODULE
,
7224 md_probe
, NULL
, NULL
);
7226 register_reboot_notifier(&md_notifier
);
7227 raid_table_header
= register_sysctl_table(raid_root_table
);
7237 * Searches all registered partitions for autorun RAID arrays
7241 static LIST_HEAD(all_detected_devices
);
7242 struct detected_devices_node
{
7243 struct list_head list
;
7247 void md_autodetect_dev(dev_t dev
)
7249 struct detected_devices_node
*node_detected_dev
;
7251 node_detected_dev
= kzalloc(sizeof(*node_detected_dev
), GFP_KERNEL
);
7252 if (node_detected_dev
) {
7253 node_detected_dev
->dev
= dev
;
7254 list_add_tail(&node_detected_dev
->list
, &all_detected_devices
);
7256 printk(KERN_CRIT
"md: md_autodetect_dev: kzalloc failed"
7257 ", skipping dev(%d,%d)\n", MAJOR(dev
), MINOR(dev
));
7262 static void autostart_arrays(int part
)
7265 struct detected_devices_node
*node_detected_dev
;
7267 int i_scanned
, i_passed
;
7272 printk(KERN_INFO
"md: Autodetecting RAID arrays.\n");
7274 while (!list_empty(&all_detected_devices
) && i_scanned
< INT_MAX
) {
7276 node_detected_dev
= list_entry(all_detected_devices
.next
,
7277 struct detected_devices_node
, list
);
7278 list_del(&node_detected_dev
->list
);
7279 dev
= node_detected_dev
->dev
;
7280 kfree(node_detected_dev
);
7281 rdev
= md_import_device(dev
,0, 90);
7285 if (test_bit(Faulty
, &rdev
->flags
)) {
7289 set_bit(AutoDetected
, &rdev
->flags
);
7290 list_add(&rdev
->same_set
, &pending_raid_disks
);
7294 printk(KERN_INFO
"md: Scanned %d and added %d devices.\n",
7295 i_scanned
, i_passed
);
7297 autorun_devices(part
);
7300 #endif /* !MODULE */
7302 static __exit
void md_exit(void)
7305 struct list_head
*tmp
;
7307 blk_unregister_region(MKDEV(MD_MAJOR
,0), 1U << MINORBITS
);
7308 blk_unregister_region(MKDEV(mdp_major
,0), 1U << MINORBITS
);
7310 unregister_blkdev(MD_MAJOR
,"md");
7311 unregister_blkdev(mdp_major
, "mdp");
7312 unregister_reboot_notifier(&md_notifier
);
7313 unregister_sysctl_table(raid_table_header
);
7314 remove_proc_entry("mdstat", NULL
);
7315 for_each_mddev(mddev
, tmp
) {
7316 export_array(mddev
);
7317 mddev
->hold_active
= 0;
7321 subsys_initcall(md_init
);
7322 module_exit(md_exit
)
7324 static int get_ro(char *buffer
, struct kernel_param
*kp
)
7326 return sprintf(buffer
, "%d", start_readonly
);
7328 static int set_ro(const char *val
, struct kernel_param
*kp
)
7331 int num
= simple_strtoul(val
, &e
, 10);
7332 if (*val
&& (*e
== '\0' || *e
== '\n')) {
7333 start_readonly
= num
;
7339 module_param_call(start_ro
, set_ro
, get_ro
, NULL
, S_IRUSR
|S_IWUSR
);
7340 module_param(start_dirty_degraded
, int, S_IRUGO
|S_IWUSR
);
7342 module_param_call(new_array
, add_named_array
, NULL
, NULL
, S_IWUSR
);
7344 EXPORT_SYMBOL(register_md_personality
);
7345 EXPORT_SYMBOL(unregister_md_personality
);
7346 EXPORT_SYMBOL(md_error
);
7347 EXPORT_SYMBOL(md_done_sync
);
7348 EXPORT_SYMBOL(md_write_start
);
7349 EXPORT_SYMBOL(md_write_end
);
7350 EXPORT_SYMBOL(md_register_thread
);
7351 EXPORT_SYMBOL(md_unregister_thread
);
7352 EXPORT_SYMBOL(md_wakeup_thread
);
7353 EXPORT_SYMBOL(md_check_recovery
);
7354 MODULE_LICENSE("GPL");
7355 MODULE_DESCRIPTION("MD RAID framework");
7357 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR
);