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/smp_lock.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)))
62 static void autostart_arrays(int part
);
65 static LIST_HEAD(pers_list
);
66 static DEFINE_SPINLOCK(pers_lock
);
68 static void md_print_devices(void);
70 static DECLARE_WAIT_QUEUE_HEAD(resync_wait
);
72 #define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
75 * Default number of read corrections we'll attempt on an rdev
76 * before ejecting it from the array. We divide the read error
77 * count by 2 for every hour elapsed between read errors.
79 #define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
81 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
82 * is 1000 KB/sec, so the extra system load does not show up that much.
83 * Increase it if you want to have more _guaranteed_ speed. Note that
84 * the RAID driver will use the maximum available bandwidth if the IO
85 * subsystem is idle. There is also an 'absolute maximum' reconstruction
86 * speed limit - in case reconstruction slows down your system despite
89 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
90 * or /sys/block/mdX/md/sync_speed_{min,max}
93 static int sysctl_speed_limit_min
= 1000;
94 static int sysctl_speed_limit_max
= 200000;
95 static inline int speed_min(mddev_t
*mddev
)
97 return mddev
->sync_speed_min
?
98 mddev
->sync_speed_min
: sysctl_speed_limit_min
;
101 static inline int speed_max(mddev_t
*mddev
)
103 return mddev
->sync_speed_max
?
104 mddev
->sync_speed_max
: sysctl_speed_limit_max
;
107 static struct ctl_table_header
*raid_table_header
;
109 static ctl_table raid_table
[] = {
111 .procname
= "speed_limit_min",
112 .data
= &sysctl_speed_limit_min
,
113 .maxlen
= sizeof(int),
114 .mode
= S_IRUGO
|S_IWUSR
,
115 .proc_handler
= proc_dointvec
,
118 .procname
= "speed_limit_max",
119 .data
= &sysctl_speed_limit_max
,
120 .maxlen
= sizeof(int),
121 .mode
= S_IRUGO
|S_IWUSR
,
122 .proc_handler
= proc_dointvec
,
127 static ctl_table raid_dir_table
[] = {
131 .mode
= S_IRUGO
|S_IXUGO
,
137 static ctl_table raid_root_table
[] = {
142 .child
= raid_dir_table
,
147 static const struct block_device_operations md_fops
;
149 static int start_readonly
;
152 * We have a system wide 'event count' that is incremented
153 * on any 'interesting' event, and readers of /proc/mdstat
154 * can use 'poll' or 'select' to find out when the event
158 * start array, stop array, error, add device, remove device,
159 * start build, activate spare
161 static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters
);
162 static atomic_t md_event_count
;
163 void md_new_event(mddev_t
*mddev
)
165 atomic_inc(&md_event_count
);
166 wake_up(&md_event_waiters
);
168 EXPORT_SYMBOL_GPL(md_new_event
);
170 /* Alternate version that can be called from interrupts
171 * when calling sysfs_notify isn't needed.
173 static void md_new_event_inintr(mddev_t
*mddev
)
175 atomic_inc(&md_event_count
);
176 wake_up(&md_event_waiters
);
180 * Enables to iterate over all existing md arrays
181 * all_mddevs_lock protects this list.
183 static LIST_HEAD(all_mddevs
);
184 static DEFINE_SPINLOCK(all_mddevs_lock
);
188 * iterates through all used mddevs in the system.
189 * We take care to grab the all_mddevs_lock whenever navigating
190 * the list, and to always hold a refcount when unlocked.
191 * Any code which breaks out of this loop while own
192 * a reference to the current mddev and must mddev_put it.
194 #define for_each_mddev(mddev,tmp) \
196 for (({ spin_lock(&all_mddevs_lock); \
197 tmp = all_mddevs.next; \
199 ({ if (tmp != &all_mddevs) \
200 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
201 spin_unlock(&all_mddevs_lock); \
202 if (mddev) mddev_put(mddev); \
203 mddev = list_entry(tmp, mddev_t, all_mddevs); \
204 tmp != &all_mddevs;}); \
205 ({ spin_lock(&all_mddevs_lock); \
210 /* Rather than calling directly into the personality make_request function,
211 * IO requests come here first so that we can check if the device is
212 * being suspended pending a reconfiguration.
213 * We hold a refcount over the call to ->make_request. By the time that
214 * call has finished, the bio has been linked into some internal structure
215 * and so is visible to ->quiesce(), so we don't need the refcount any more.
217 static int md_make_request(struct request_queue
*q
, struct bio
*bio
)
219 const int rw
= bio_data_dir(bio
);
220 mddev_t
*mddev
= q
->queuedata
;
224 if (mddev
== NULL
|| mddev
->pers
== NULL
) {
229 if (mddev
->suspended
) {
232 prepare_to_wait(&mddev
->sb_wait
, &__wait
,
233 TASK_UNINTERRUPTIBLE
);
234 if (!mddev
->suspended
)
240 finish_wait(&mddev
->sb_wait
, &__wait
);
242 atomic_inc(&mddev
->active_io
);
245 rv
= mddev
->pers
->make_request(mddev
, bio
);
247 cpu
= part_stat_lock();
248 part_stat_inc(cpu
, &mddev
->gendisk
->part0
, ios
[rw
]);
249 part_stat_add(cpu
, &mddev
->gendisk
->part0
, sectors
[rw
],
253 if (atomic_dec_and_test(&mddev
->active_io
) && mddev
->suspended
)
254 wake_up(&mddev
->sb_wait
);
259 /* mddev_suspend makes sure no new requests are submitted
260 * to the device, and that any requests that have been submitted
261 * are completely handled.
262 * Once ->stop is called and completes, the module will be completely
265 void mddev_suspend(mddev_t
*mddev
)
267 BUG_ON(mddev
->suspended
);
268 mddev
->suspended
= 1;
270 wait_event(mddev
->sb_wait
, atomic_read(&mddev
->active_io
) == 0);
271 mddev
->pers
->quiesce(mddev
, 1);
273 EXPORT_SYMBOL_GPL(mddev_suspend
);
275 void mddev_resume(mddev_t
*mddev
)
277 mddev
->suspended
= 0;
278 wake_up(&mddev
->sb_wait
);
279 mddev
->pers
->quiesce(mddev
, 0);
281 EXPORT_SYMBOL_GPL(mddev_resume
);
283 int mddev_congested(mddev_t
*mddev
, int bits
)
285 return mddev
->suspended
;
287 EXPORT_SYMBOL(mddev_congested
);
290 * Generic flush handling for md
293 static void md_end_flush(struct bio
*bio
, int err
)
295 mdk_rdev_t
*rdev
= bio
->bi_private
;
296 mddev_t
*mddev
= rdev
->mddev
;
298 rdev_dec_pending(rdev
, mddev
);
300 if (atomic_dec_and_test(&mddev
->flush_pending
)) {
301 /* The pre-request flush has finished */
302 schedule_work(&mddev
->flush_work
);
307 static void submit_flushes(mddev_t
*mddev
)
312 list_for_each_entry_rcu(rdev
, &mddev
->disks
, same_set
)
313 if (rdev
->raid_disk
>= 0 &&
314 !test_bit(Faulty
, &rdev
->flags
)) {
315 /* Take two references, one is dropped
316 * when request finishes, one after
317 * we reclaim rcu_read_lock
320 atomic_inc(&rdev
->nr_pending
);
321 atomic_inc(&rdev
->nr_pending
);
323 bi
= bio_alloc(GFP_KERNEL
, 0);
324 bi
->bi_end_io
= md_end_flush
;
325 bi
->bi_private
= rdev
;
326 bi
->bi_bdev
= rdev
->bdev
;
327 atomic_inc(&mddev
->flush_pending
);
328 submit_bio(WRITE_FLUSH
, bi
);
330 rdev_dec_pending(rdev
, mddev
);
335 static void md_submit_flush_data(struct work_struct
*ws
)
337 mddev_t
*mddev
= container_of(ws
, mddev_t
, flush_work
);
338 struct bio
*bio
= mddev
->flush_bio
;
340 atomic_set(&mddev
->flush_pending
, 1);
342 if (bio
->bi_size
== 0)
343 /* an empty barrier - all done */
346 bio
->bi_rw
&= ~REQ_FLUSH
;
347 if (mddev
->pers
->make_request(mddev
, bio
))
348 generic_make_request(bio
);
350 if (atomic_dec_and_test(&mddev
->flush_pending
)) {
351 mddev
->flush_bio
= NULL
;
352 wake_up(&mddev
->sb_wait
);
356 void md_flush_request(mddev_t
*mddev
, struct bio
*bio
)
358 spin_lock_irq(&mddev
->write_lock
);
359 wait_event_lock_irq(mddev
->sb_wait
,
361 mddev
->write_lock
, /*nothing*/);
362 mddev
->flush_bio
= bio
;
363 spin_unlock_irq(&mddev
->write_lock
);
365 atomic_set(&mddev
->flush_pending
, 1);
366 INIT_WORK(&mddev
->flush_work
, md_submit_flush_data
);
368 submit_flushes(mddev
);
370 if (atomic_dec_and_test(&mddev
->flush_pending
))
371 schedule_work(&mddev
->flush_work
);
373 EXPORT_SYMBOL(md_flush_request
);
375 /* Support for plugging.
376 * This mirrors the plugging support in request_queue, but does not
377 * require having a whole queue
379 static void plugger_work(struct work_struct
*work
)
381 struct plug_handle
*plug
=
382 container_of(work
, struct plug_handle
, unplug_work
);
383 plug
->unplug_fn(plug
);
385 static void plugger_timeout(unsigned long data
)
387 struct plug_handle
*plug
= (void *)data
;
388 kblockd_schedule_work(NULL
, &plug
->unplug_work
);
390 void plugger_init(struct plug_handle
*plug
,
391 void (*unplug_fn
)(struct plug_handle
*))
393 plug
->unplug_flag
= 0;
394 plug
->unplug_fn
= unplug_fn
;
395 init_timer(&plug
->unplug_timer
);
396 plug
->unplug_timer
.function
= plugger_timeout
;
397 plug
->unplug_timer
.data
= (unsigned long)plug
;
398 INIT_WORK(&plug
->unplug_work
, plugger_work
);
400 EXPORT_SYMBOL_GPL(plugger_init
);
402 void plugger_set_plug(struct plug_handle
*plug
)
404 if (!test_and_set_bit(PLUGGED_FLAG
, &plug
->unplug_flag
))
405 mod_timer(&plug
->unplug_timer
, jiffies
+ msecs_to_jiffies(3)+1);
407 EXPORT_SYMBOL_GPL(plugger_set_plug
);
409 int plugger_remove_plug(struct plug_handle
*plug
)
411 if (test_and_clear_bit(PLUGGED_FLAG
, &plug
->unplug_flag
)) {
412 del_timer(&plug
->unplug_timer
);
417 EXPORT_SYMBOL_GPL(plugger_remove_plug
);
420 static inline mddev_t
*mddev_get(mddev_t
*mddev
)
422 atomic_inc(&mddev
->active
);
426 static void mddev_delayed_delete(struct work_struct
*ws
);
428 static void mddev_put(mddev_t
*mddev
)
430 if (!atomic_dec_and_lock(&mddev
->active
, &all_mddevs_lock
))
432 if (!mddev
->raid_disks
&& list_empty(&mddev
->disks
) &&
433 mddev
->ctime
== 0 && !mddev
->hold_active
) {
434 /* Array is not configured at all, and not held active,
436 list_del(&mddev
->all_mddevs
);
437 if (mddev
->gendisk
) {
438 /* we did a probe so need to clean up.
439 * Call schedule_work inside the spinlock
440 * so that flush_scheduled_work() after
441 * mddev_find will succeed in waiting for the
444 INIT_WORK(&mddev
->del_work
, mddev_delayed_delete
);
445 schedule_work(&mddev
->del_work
);
449 spin_unlock(&all_mddevs_lock
);
452 void mddev_init(mddev_t
*mddev
)
454 mutex_init(&mddev
->open_mutex
);
455 mutex_init(&mddev
->reconfig_mutex
);
456 mutex_init(&mddev
->bitmap_info
.mutex
);
457 INIT_LIST_HEAD(&mddev
->disks
);
458 INIT_LIST_HEAD(&mddev
->all_mddevs
);
459 init_timer(&mddev
->safemode_timer
);
460 atomic_set(&mddev
->active
, 1);
461 atomic_set(&mddev
->openers
, 0);
462 atomic_set(&mddev
->active_io
, 0);
463 spin_lock_init(&mddev
->write_lock
);
464 atomic_set(&mddev
->flush_pending
, 0);
465 init_waitqueue_head(&mddev
->sb_wait
);
466 init_waitqueue_head(&mddev
->recovery_wait
);
467 mddev
->reshape_position
= MaxSector
;
468 mddev
->resync_min
= 0;
469 mddev
->resync_max
= MaxSector
;
470 mddev
->level
= LEVEL_NONE
;
472 EXPORT_SYMBOL_GPL(mddev_init
);
474 static mddev_t
* mddev_find(dev_t unit
)
476 mddev_t
*mddev
, *new = NULL
;
479 spin_lock(&all_mddevs_lock
);
482 list_for_each_entry(mddev
, &all_mddevs
, all_mddevs
)
483 if (mddev
->unit
== unit
) {
485 spin_unlock(&all_mddevs_lock
);
491 list_add(&new->all_mddevs
, &all_mddevs
);
492 spin_unlock(&all_mddevs_lock
);
493 new->hold_active
= UNTIL_IOCTL
;
497 /* find an unused unit number */
498 static int next_minor
= 512;
499 int start
= next_minor
;
503 dev
= MKDEV(MD_MAJOR
, next_minor
);
505 if (next_minor
> MINORMASK
)
507 if (next_minor
== start
) {
508 /* Oh dear, all in use. */
509 spin_unlock(&all_mddevs_lock
);
515 list_for_each_entry(mddev
, &all_mddevs
, all_mddevs
)
516 if (mddev
->unit
== dev
) {
522 new->md_minor
= MINOR(dev
);
523 new->hold_active
= UNTIL_STOP
;
524 list_add(&new->all_mddevs
, &all_mddevs
);
525 spin_unlock(&all_mddevs_lock
);
528 spin_unlock(&all_mddevs_lock
);
530 new = kzalloc(sizeof(*new), GFP_KERNEL
);
535 if (MAJOR(unit
) == MD_MAJOR
)
536 new->md_minor
= MINOR(unit
);
538 new->md_minor
= MINOR(unit
) >> MdpMinorShift
;
545 static inline int mddev_lock(mddev_t
* mddev
)
547 return mutex_lock_interruptible(&mddev
->reconfig_mutex
);
550 static inline int mddev_is_locked(mddev_t
*mddev
)
552 return mutex_is_locked(&mddev
->reconfig_mutex
);
555 static inline int mddev_trylock(mddev_t
* mddev
)
557 return mutex_trylock(&mddev
->reconfig_mutex
);
560 static struct attribute_group md_redundancy_group
;
562 static void mddev_unlock(mddev_t
* mddev
)
564 if (mddev
->to_remove
) {
565 /* These cannot be removed under reconfig_mutex as
566 * an access to the files will try to take reconfig_mutex
567 * while holding the file unremovable, which leads to
569 * So hold set sysfs_active while the remove in happeing,
570 * and anything else which might set ->to_remove or my
571 * otherwise change the sysfs namespace will fail with
572 * -EBUSY if sysfs_active is still set.
573 * We set sysfs_active under reconfig_mutex and elsewhere
574 * test it under the same mutex to ensure its correct value
577 struct attribute_group
*to_remove
= mddev
->to_remove
;
578 mddev
->to_remove
= NULL
;
579 mddev
->sysfs_active
= 1;
580 mutex_unlock(&mddev
->reconfig_mutex
);
582 if (mddev
->kobj
.sd
) {
583 if (to_remove
!= &md_redundancy_group
)
584 sysfs_remove_group(&mddev
->kobj
, to_remove
);
585 if (mddev
->pers
== NULL
||
586 mddev
->pers
->sync_request
== NULL
) {
587 sysfs_remove_group(&mddev
->kobj
, &md_redundancy_group
);
588 if (mddev
->sysfs_action
)
589 sysfs_put(mddev
->sysfs_action
);
590 mddev
->sysfs_action
= NULL
;
593 mddev
->sysfs_active
= 0;
595 mutex_unlock(&mddev
->reconfig_mutex
);
597 md_wakeup_thread(mddev
->thread
);
600 static mdk_rdev_t
* find_rdev_nr(mddev_t
*mddev
, int nr
)
604 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
605 if (rdev
->desc_nr
== nr
)
611 static mdk_rdev_t
* find_rdev(mddev_t
* mddev
, dev_t dev
)
615 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
616 if (rdev
->bdev
->bd_dev
== dev
)
622 static struct mdk_personality
*find_pers(int level
, char *clevel
)
624 struct mdk_personality
*pers
;
625 list_for_each_entry(pers
, &pers_list
, list
) {
626 if (level
!= LEVEL_NONE
&& pers
->level
== level
)
628 if (strcmp(pers
->name
, clevel
)==0)
634 /* return the offset of the super block in 512byte sectors */
635 static inline sector_t
calc_dev_sboffset(struct block_device
*bdev
)
637 sector_t num_sectors
= bdev
->bd_inode
->i_size
/ 512;
638 return MD_NEW_SIZE_SECTORS(num_sectors
);
641 static int alloc_disk_sb(mdk_rdev_t
* rdev
)
646 rdev
->sb_page
= alloc_page(GFP_KERNEL
);
647 if (!rdev
->sb_page
) {
648 printk(KERN_ALERT
"md: out of memory.\n");
655 static void free_disk_sb(mdk_rdev_t
* rdev
)
658 put_page(rdev
->sb_page
);
660 rdev
->sb_page
= NULL
;
667 static void super_written(struct bio
*bio
, int error
)
669 mdk_rdev_t
*rdev
= bio
->bi_private
;
670 mddev_t
*mddev
= rdev
->mddev
;
672 if (error
|| !test_bit(BIO_UPTODATE
, &bio
->bi_flags
)) {
673 printk("md: super_written gets error=%d, uptodate=%d\n",
674 error
, test_bit(BIO_UPTODATE
, &bio
->bi_flags
));
675 WARN_ON(test_bit(BIO_UPTODATE
, &bio
->bi_flags
));
676 md_error(mddev
, rdev
);
679 if (atomic_dec_and_test(&mddev
->pending_writes
))
680 wake_up(&mddev
->sb_wait
);
684 void md_super_write(mddev_t
*mddev
, mdk_rdev_t
*rdev
,
685 sector_t sector
, int size
, struct page
*page
)
687 /* write first size bytes of page to sector of rdev
688 * Increment mddev->pending_writes before returning
689 * and decrement it on completion, waking up sb_wait
690 * if zero is reached.
691 * If an error occurred, call md_error
693 struct bio
*bio
= bio_alloc(GFP_NOIO
, 1);
695 bio
->bi_bdev
= rdev
->bdev
;
696 bio
->bi_sector
= sector
;
697 bio_add_page(bio
, page
, size
, 0);
698 bio
->bi_private
= rdev
;
699 bio
->bi_end_io
= super_written
;
701 atomic_inc(&mddev
->pending_writes
);
702 submit_bio(REQ_WRITE
| REQ_SYNC
| REQ_UNPLUG
| REQ_FLUSH
| REQ_FUA
,
706 void md_super_wait(mddev_t
*mddev
)
708 /* wait for all superblock writes that were scheduled to complete */
711 prepare_to_wait(&mddev
->sb_wait
, &wq
, TASK_UNINTERRUPTIBLE
);
712 if (atomic_read(&mddev
->pending_writes
)==0)
716 finish_wait(&mddev
->sb_wait
, &wq
);
719 static void bi_complete(struct bio
*bio
, int error
)
721 complete((struct completion
*)bio
->bi_private
);
724 int sync_page_io(struct block_device
*bdev
, sector_t sector
, int size
,
725 struct page
*page
, int rw
)
727 struct bio
*bio
= bio_alloc(GFP_NOIO
, 1);
728 struct completion event
;
731 rw
|= REQ_SYNC
| REQ_UNPLUG
;
734 bio
->bi_sector
= sector
;
735 bio_add_page(bio
, page
, size
, 0);
736 init_completion(&event
);
737 bio
->bi_private
= &event
;
738 bio
->bi_end_io
= bi_complete
;
740 wait_for_completion(&event
);
742 ret
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
746 EXPORT_SYMBOL_GPL(sync_page_io
);
748 static int read_disk_sb(mdk_rdev_t
* rdev
, int size
)
750 char b
[BDEVNAME_SIZE
];
751 if (!rdev
->sb_page
) {
759 if (!sync_page_io(rdev
->bdev
, rdev
->sb_start
, size
, rdev
->sb_page
, READ
))
765 printk(KERN_WARNING
"md: disabled device %s, could not read superblock.\n",
766 bdevname(rdev
->bdev
,b
));
770 static int uuid_equal(mdp_super_t
*sb1
, mdp_super_t
*sb2
)
772 return sb1
->set_uuid0
== sb2
->set_uuid0
&&
773 sb1
->set_uuid1
== sb2
->set_uuid1
&&
774 sb1
->set_uuid2
== sb2
->set_uuid2
&&
775 sb1
->set_uuid3
== sb2
->set_uuid3
;
778 static int sb_equal(mdp_super_t
*sb1
, mdp_super_t
*sb2
)
781 mdp_super_t
*tmp1
, *tmp2
;
783 tmp1
= kmalloc(sizeof(*tmp1
),GFP_KERNEL
);
784 tmp2
= kmalloc(sizeof(*tmp2
),GFP_KERNEL
);
786 if (!tmp1
|| !tmp2
) {
788 printk(KERN_INFO
"md.c sb_equal(): failed to allocate memory!\n");
796 * nr_disks is not constant
801 ret
= (memcmp(tmp1
, tmp2
, MD_SB_GENERIC_CONSTANT_WORDS
* 4) == 0);
809 static u32
md_csum_fold(u32 csum
)
811 csum
= (csum
& 0xffff) + (csum
>> 16);
812 return (csum
& 0xffff) + (csum
>> 16);
815 static unsigned int calc_sb_csum(mdp_super_t
* sb
)
818 u32
*sb32
= (u32
*)sb
;
820 unsigned int disk_csum
, csum
;
822 disk_csum
= sb
->sb_csum
;
825 for (i
= 0; i
< MD_SB_BYTES
/4 ; i
++)
827 csum
= (newcsum
& 0xffffffff) + (newcsum
>>32);
831 /* This used to use csum_partial, which was wrong for several
832 * reasons including that different results are returned on
833 * different architectures. It isn't critical that we get exactly
834 * the same return value as before (we always csum_fold before
835 * testing, and that removes any differences). However as we
836 * know that csum_partial always returned a 16bit value on
837 * alphas, do a fold to maximise conformity to previous behaviour.
839 sb
->sb_csum
= md_csum_fold(disk_csum
);
841 sb
->sb_csum
= disk_csum
;
848 * Handle superblock details.
849 * We want to be able to handle multiple superblock formats
850 * so we have a common interface to them all, and an array of
851 * different handlers.
852 * We rely on user-space to write the initial superblock, and support
853 * reading and updating of superblocks.
854 * Interface methods are:
855 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
856 * loads and validates a superblock on dev.
857 * if refdev != NULL, compare superblocks on both devices
859 * 0 - dev has a superblock that is compatible with refdev
860 * 1 - dev has a superblock that is compatible and newer than refdev
861 * so dev should be used as the refdev in future
862 * -EINVAL superblock incompatible or invalid
863 * -othererror e.g. -EIO
865 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
866 * Verify that dev is acceptable into mddev.
867 * The first time, mddev->raid_disks will be 0, and data from
868 * dev should be merged in. Subsequent calls check that dev
869 * is new enough. Return 0 or -EINVAL
871 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
872 * Update the superblock for rdev with data in mddev
873 * This does not write to disc.
879 struct module
*owner
;
880 int (*load_super
)(mdk_rdev_t
*rdev
, mdk_rdev_t
*refdev
,
882 int (*validate_super
)(mddev_t
*mddev
, mdk_rdev_t
*rdev
);
883 void (*sync_super
)(mddev_t
*mddev
, mdk_rdev_t
*rdev
);
884 unsigned long long (*rdev_size_change
)(mdk_rdev_t
*rdev
,
885 sector_t num_sectors
);
889 * Check that the given mddev has no bitmap.
891 * This function is called from the run method of all personalities that do not
892 * support bitmaps. It prints an error message and returns non-zero if mddev
893 * has a bitmap. Otherwise, it returns 0.
896 int md_check_no_bitmap(mddev_t
*mddev
)
898 if (!mddev
->bitmap_info
.file
&& !mddev
->bitmap_info
.offset
)
900 printk(KERN_ERR
"%s: bitmaps are not supported for %s\n",
901 mdname(mddev
), mddev
->pers
->name
);
904 EXPORT_SYMBOL(md_check_no_bitmap
);
907 * load_super for 0.90.0
909 static int super_90_load(mdk_rdev_t
*rdev
, mdk_rdev_t
*refdev
, int minor_version
)
911 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
916 * Calculate the position of the superblock (512byte sectors),
917 * it's at the end of the disk.
919 * It also happens to be a multiple of 4Kb.
921 rdev
->sb_start
= calc_dev_sboffset(rdev
->bdev
);
923 ret
= read_disk_sb(rdev
, MD_SB_BYTES
);
928 bdevname(rdev
->bdev
, b
);
929 sb
= (mdp_super_t
*)page_address(rdev
->sb_page
);
931 if (sb
->md_magic
!= MD_SB_MAGIC
) {
932 printk(KERN_ERR
"md: invalid raid superblock magic on %s\n",
937 if (sb
->major_version
!= 0 ||
938 sb
->minor_version
< 90 ||
939 sb
->minor_version
> 91) {
940 printk(KERN_WARNING
"Bad version number %d.%d on %s\n",
941 sb
->major_version
, sb
->minor_version
,
946 if (sb
->raid_disks
<= 0)
949 if (md_csum_fold(calc_sb_csum(sb
)) != md_csum_fold(sb
->sb_csum
)) {
950 printk(KERN_WARNING
"md: invalid superblock checksum on %s\n",
955 rdev
->preferred_minor
= sb
->md_minor
;
956 rdev
->data_offset
= 0;
957 rdev
->sb_size
= MD_SB_BYTES
;
959 if (sb
->level
== LEVEL_MULTIPATH
)
962 rdev
->desc_nr
= sb
->this_disk
.number
;
968 mdp_super_t
*refsb
= (mdp_super_t
*)page_address(refdev
->sb_page
);
969 if (!uuid_equal(refsb
, sb
)) {
970 printk(KERN_WARNING
"md: %s has different UUID to %s\n",
971 b
, bdevname(refdev
->bdev
,b2
));
974 if (!sb_equal(refsb
, sb
)) {
975 printk(KERN_WARNING
"md: %s has same UUID"
976 " but different superblock to %s\n",
977 b
, bdevname(refdev
->bdev
, b2
));
981 ev2
= md_event(refsb
);
987 rdev
->sectors
= rdev
->sb_start
;
989 if (rdev
->sectors
< sb
->size
* 2 && sb
->level
> 1)
990 /* "this cannot possibly happen" ... */
998 * validate_super for 0.90.0
1000 static int super_90_validate(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1003 mdp_super_t
*sb
= (mdp_super_t
*)page_address(rdev
->sb_page
);
1004 __u64 ev1
= md_event(sb
);
1006 rdev
->raid_disk
= -1;
1007 clear_bit(Faulty
, &rdev
->flags
);
1008 clear_bit(In_sync
, &rdev
->flags
);
1009 clear_bit(WriteMostly
, &rdev
->flags
);
1011 if (mddev
->raid_disks
== 0) {
1012 mddev
->major_version
= 0;
1013 mddev
->minor_version
= sb
->minor_version
;
1014 mddev
->patch_version
= sb
->patch_version
;
1015 mddev
->external
= 0;
1016 mddev
->chunk_sectors
= sb
->chunk_size
>> 9;
1017 mddev
->ctime
= sb
->ctime
;
1018 mddev
->utime
= sb
->utime
;
1019 mddev
->level
= sb
->level
;
1020 mddev
->clevel
[0] = 0;
1021 mddev
->layout
= sb
->layout
;
1022 mddev
->raid_disks
= sb
->raid_disks
;
1023 mddev
->dev_sectors
= sb
->size
* 2;
1024 mddev
->events
= ev1
;
1025 mddev
->bitmap_info
.offset
= 0;
1026 mddev
->bitmap_info
.default_offset
= MD_SB_BYTES
>> 9;
1028 if (mddev
->minor_version
>= 91) {
1029 mddev
->reshape_position
= sb
->reshape_position
;
1030 mddev
->delta_disks
= sb
->delta_disks
;
1031 mddev
->new_level
= sb
->new_level
;
1032 mddev
->new_layout
= sb
->new_layout
;
1033 mddev
->new_chunk_sectors
= sb
->new_chunk
>> 9;
1035 mddev
->reshape_position
= MaxSector
;
1036 mddev
->delta_disks
= 0;
1037 mddev
->new_level
= mddev
->level
;
1038 mddev
->new_layout
= mddev
->layout
;
1039 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
1042 if (sb
->state
& (1<<MD_SB_CLEAN
))
1043 mddev
->recovery_cp
= MaxSector
;
1045 if (sb
->events_hi
== sb
->cp_events_hi
&&
1046 sb
->events_lo
== sb
->cp_events_lo
) {
1047 mddev
->recovery_cp
= sb
->recovery_cp
;
1049 mddev
->recovery_cp
= 0;
1052 memcpy(mddev
->uuid
+0, &sb
->set_uuid0
, 4);
1053 memcpy(mddev
->uuid
+4, &sb
->set_uuid1
, 4);
1054 memcpy(mddev
->uuid
+8, &sb
->set_uuid2
, 4);
1055 memcpy(mddev
->uuid
+12,&sb
->set_uuid3
, 4);
1057 mddev
->max_disks
= MD_SB_DISKS
;
1059 if (sb
->state
& (1<<MD_SB_BITMAP_PRESENT
) &&
1060 mddev
->bitmap_info
.file
== NULL
)
1061 mddev
->bitmap_info
.offset
=
1062 mddev
->bitmap_info
.default_offset
;
1064 } else if (mddev
->pers
== NULL
) {
1065 /* Insist on good event counter while assembling, except
1066 * for spares (which don't need an event count) */
1068 if (sb
->disks
[rdev
->desc_nr
].state
& (
1069 (1<<MD_DISK_SYNC
) | (1 << MD_DISK_ACTIVE
)))
1070 if (ev1
< mddev
->events
)
1072 } else if (mddev
->bitmap
) {
1073 /* if adding to array with a bitmap, then we can accept an
1074 * older device ... but not too old.
1076 if (ev1
< mddev
->bitmap
->events_cleared
)
1079 if (ev1
< mddev
->events
)
1080 /* just a hot-add of a new device, leave raid_disk at -1 */
1084 if (mddev
->level
!= LEVEL_MULTIPATH
) {
1085 desc
= sb
->disks
+ rdev
->desc_nr
;
1087 if (desc
->state
& (1<<MD_DISK_FAULTY
))
1088 set_bit(Faulty
, &rdev
->flags
);
1089 else if (desc
->state
& (1<<MD_DISK_SYNC
) /* &&
1090 desc->raid_disk < mddev->raid_disks */) {
1091 set_bit(In_sync
, &rdev
->flags
);
1092 rdev
->raid_disk
= desc
->raid_disk
;
1093 } else if (desc
->state
& (1<<MD_DISK_ACTIVE
)) {
1094 /* active but not in sync implies recovery up to
1095 * reshape position. We don't know exactly where
1096 * that is, so set to zero for now */
1097 if (mddev
->minor_version
>= 91) {
1098 rdev
->recovery_offset
= 0;
1099 rdev
->raid_disk
= desc
->raid_disk
;
1102 if (desc
->state
& (1<<MD_DISK_WRITEMOSTLY
))
1103 set_bit(WriteMostly
, &rdev
->flags
);
1104 } else /* MULTIPATH are always insync */
1105 set_bit(In_sync
, &rdev
->flags
);
1110 * sync_super for 0.90.0
1112 static void super_90_sync(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1116 int next_spare
= mddev
->raid_disks
;
1119 /* make rdev->sb match mddev data..
1122 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1123 * 3/ any empty disks < next_spare become removed
1125 * disks[0] gets initialised to REMOVED because
1126 * we cannot be sure from other fields if it has
1127 * been initialised or not.
1130 int active
=0, working
=0,failed
=0,spare
=0,nr_disks
=0;
1132 rdev
->sb_size
= MD_SB_BYTES
;
1134 sb
= (mdp_super_t
*)page_address(rdev
->sb_page
);
1136 memset(sb
, 0, sizeof(*sb
));
1138 sb
->md_magic
= MD_SB_MAGIC
;
1139 sb
->major_version
= mddev
->major_version
;
1140 sb
->patch_version
= mddev
->patch_version
;
1141 sb
->gvalid_words
= 0; /* ignored */
1142 memcpy(&sb
->set_uuid0
, mddev
->uuid
+0, 4);
1143 memcpy(&sb
->set_uuid1
, mddev
->uuid
+4, 4);
1144 memcpy(&sb
->set_uuid2
, mddev
->uuid
+8, 4);
1145 memcpy(&sb
->set_uuid3
, mddev
->uuid
+12,4);
1147 sb
->ctime
= mddev
->ctime
;
1148 sb
->level
= mddev
->level
;
1149 sb
->size
= mddev
->dev_sectors
/ 2;
1150 sb
->raid_disks
= mddev
->raid_disks
;
1151 sb
->md_minor
= mddev
->md_minor
;
1152 sb
->not_persistent
= 0;
1153 sb
->utime
= mddev
->utime
;
1155 sb
->events_hi
= (mddev
->events
>>32);
1156 sb
->events_lo
= (u32
)mddev
->events
;
1158 if (mddev
->reshape_position
== MaxSector
)
1159 sb
->minor_version
= 90;
1161 sb
->minor_version
= 91;
1162 sb
->reshape_position
= mddev
->reshape_position
;
1163 sb
->new_level
= mddev
->new_level
;
1164 sb
->delta_disks
= mddev
->delta_disks
;
1165 sb
->new_layout
= mddev
->new_layout
;
1166 sb
->new_chunk
= mddev
->new_chunk_sectors
<< 9;
1168 mddev
->minor_version
= sb
->minor_version
;
1171 sb
->recovery_cp
= mddev
->recovery_cp
;
1172 sb
->cp_events_hi
= (mddev
->events
>>32);
1173 sb
->cp_events_lo
= (u32
)mddev
->events
;
1174 if (mddev
->recovery_cp
== MaxSector
)
1175 sb
->state
= (1<< MD_SB_CLEAN
);
1177 sb
->recovery_cp
= 0;
1179 sb
->layout
= mddev
->layout
;
1180 sb
->chunk_size
= mddev
->chunk_sectors
<< 9;
1182 if (mddev
->bitmap
&& mddev
->bitmap_info
.file
== NULL
)
1183 sb
->state
|= (1<<MD_SB_BITMAP_PRESENT
);
1185 sb
->disks
[0].state
= (1<<MD_DISK_REMOVED
);
1186 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
) {
1189 int is_active
= test_bit(In_sync
, &rdev2
->flags
);
1191 if (rdev2
->raid_disk
>= 0 &&
1192 sb
->minor_version
>= 91)
1193 /* we have nowhere to store the recovery_offset,
1194 * but if it is not below the reshape_position,
1195 * we can piggy-back on that.
1198 if (rdev2
->raid_disk
< 0 ||
1199 test_bit(Faulty
, &rdev2
->flags
))
1202 desc_nr
= rdev2
->raid_disk
;
1204 desc_nr
= next_spare
++;
1205 rdev2
->desc_nr
= desc_nr
;
1206 d
= &sb
->disks
[rdev2
->desc_nr
];
1208 d
->number
= rdev2
->desc_nr
;
1209 d
->major
= MAJOR(rdev2
->bdev
->bd_dev
);
1210 d
->minor
= MINOR(rdev2
->bdev
->bd_dev
);
1212 d
->raid_disk
= rdev2
->raid_disk
;
1214 d
->raid_disk
= rdev2
->desc_nr
; /* compatibility */
1215 if (test_bit(Faulty
, &rdev2
->flags
))
1216 d
->state
= (1<<MD_DISK_FAULTY
);
1217 else if (is_active
) {
1218 d
->state
= (1<<MD_DISK_ACTIVE
);
1219 if (test_bit(In_sync
, &rdev2
->flags
))
1220 d
->state
|= (1<<MD_DISK_SYNC
);
1228 if (test_bit(WriteMostly
, &rdev2
->flags
))
1229 d
->state
|= (1<<MD_DISK_WRITEMOSTLY
);
1231 /* now set the "removed" and "faulty" bits on any missing devices */
1232 for (i
=0 ; i
< mddev
->raid_disks
; i
++) {
1233 mdp_disk_t
*d
= &sb
->disks
[i
];
1234 if (d
->state
== 0 && d
->number
== 0) {
1237 d
->state
= (1<<MD_DISK_REMOVED
);
1238 d
->state
|= (1<<MD_DISK_FAULTY
);
1242 sb
->nr_disks
= nr_disks
;
1243 sb
->active_disks
= active
;
1244 sb
->working_disks
= working
;
1245 sb
->failed_disks
= failed
;
1246 sb
->spare_disks
= spare
;
1248 sb
->this_disk
= sb
->disks
[rdev
->desc_nr
];
1249 sb
->sb_csum
= calc_sb_csum(sb
);
1253 * rdev_size_change for 0.90.0
1255 static unsigned long long
1256 super_90_rdev_size_change(mdk_rdev_t
*rdev
, sector_t num_sectors
)
1258 if (num_sectors
&& num_sectors
< rdev
->mddev
->dev_sectors
)
1259 return 0; /* component must fit device */
1260 if (rdev
->mddev
->bitmap_info
.offset
)
1261 return 0; /* can't move bitmap */
1262 rdev
->sb_start
= calc_dev_sboffset(rdev
->bdev
);
1263 if (!num_sectors
|| num_sectors
> rdev
->sb_start
)
1264 num_sectors
= rdev
->sb_start
;
1265 md_super_write(rdev
->mddev
, rdev
, rdev
->sb_start
, rdev
->sb_size
,
1267 md_super_wait(rdev
->mddev
);
1268 return num_sectors
/ 2; /* kB for sysfs */
1273 * version 1 superblock
1276 static __le32
calc_sb_1_csum(struct mdp_superblock_1
* sb
)
1280 unsigned long long newcsum
;
1281 int size
= 256 + le32_to_cpu(sb
->max_dev
)*2;
1282 __le32
*isuper
= (__le32
*)sb
;
1285 disk_csum
= sb
->sb_csum
;
1288 for (i
=0; size
>=4; size
-= 4 )
1289 newcsum
+= le32_to_cpu(*isuper
++);
1292 newcsum
+= le16_to_cpu(*(__le16
*) isuper
);
1294 csum
= (newcsum
& 0xffffffff) + (newcsum
>> 32);
1295 sb
->sb_csum
= disk_csum
;
1296 return cpu_to_le32(csum
);
1299 static int super_1_load(mdk_rdev_t
*rdev
, mdk_rdev_t
*refdev
, int minor_version
)
1301 struct mdp_superblock_1
*sb
;
1304 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
1308 * Calculate the position of the superblock in 512byte sectors.
1309 * It is always aligned to a 4K boundary and
1310 * depeding on minor_version, it can be:
1311 * 0: At least 8K, but less than 12K, from end of device
1312 * 1: At start of device
1313 * 2: 4K from start of device.
1315 switch(minor_version
) {
1317 sb_start
= rdev
->bdev
->bd_inode
->i_size
>> 9;
1319 sb_start
&= ~(sector_t
)(4*2-1);
1330 rdev
->sb_start
= sb_start
;
1332 /* superblock is rarely larger than 1K, but it can be larger,
1333 * and it is safe to read 4k, so we do that
1335 ret
= read_disk_sb(rdev
, 4096);
1336 if (ret
) return ret
;
1339 sb
= (struct mdp_superblock_1
*)page_address(rdev
->sb_page
);
1341 if (sb
->magic
!= cpu_to_le32(MD_SB_MAGIC
) ||
1342 sb
->major_version
!= cpu_to_le32(1) ||
1343 le32_to_cpu(sb
->max_dev
) > (4096-256)/2 ||
1344 le64_to_cpu(sb
->super_offset
) != rdev
->sb_start
||
1345 (le32_to_cpu(sb
->feature_map
) & ~MD_FEATURE_ALL
) != 0)
1348 if (calc_sb_1_csum(sb
) != sb
->sb_csum
) {
1349 printk("md: invalid superblock checksum on %s\n",
1350 bdevname(rdev
->bdev
,b
));
1353 if (le64_to_cpu(sb
->data_size
) < 10) {
1354 printk("md: data_size too small on %s\n",
1355 bdevname(rdev
->bdev
,b
));
1359 rdev
->preferred_minor
= 0xffff;
1360 rdev
->data_offset
= le64_to_cpu(sb
->data_offset
);
1361 atomic_set(&rdev
->corrected_errors
, le32_to_cpu(sb
->cnt_corrected_read
));
1363 rdev
->sb_size
= le32_to_cpu(sb
->max_dev
) * 2 + 256;
1364 bmask
= queue_logical_block_size(rdev
->bdev
->bd_disk
->queue
)-1;
1365 if (rdev
->sb_size
& bmask
)
1366 rdev
->sb_size
= (rdev
->sb_size
| bmask
) + 1;
1369 && rdev
->data_offset
< sb_start
+ (rdev
->sb_size
/512))
1372 if (sb
->level
== cpu_to_le32(LEVEL_MULTIPATH
))
1375 rdev
->desc_nr
= le32_to_cpu(sb
->dev_number
);
1381 struct mdp_superblock_1
*refsb
=
1382 (struct mdp_superblock_1
*)page_address(refdev
->sb_page
);
1384 if (memcmp(sb
->set_uuid
, refsb
->set_uuid
, 16) != 0 ||
1385 sb
->level
!= refsb
->level
||
1386 sb
->layout
!= refsb
->layout
||
1387 sb
->chunksize
!= refsb
->chunksize
) {
1388 printk(KERN_WARNING
"md: %s has strangely different"
1389 " superblock to %s\n",
1390 bdevname(rdev
->bdev
,b
),
1391 bdevname(refdev
->bdev
,b2
));
1394 ev1
= le64_to_cpu(sb
->events
);
1395 ev2
= le64_to_cpu(refsb
->events
);
1403 rdev
->sectors
= (rdev
->bdev
->bd_inode
->i_size
>> 9) -
1404 le64_to_cpu(sb
->data_offset
);
1406 rdev
->sectors
= rdev
->sb_start
;
1407 if (rdev
->sectors
< le64_to_cpu(sb
->data_size
))
1409 rdev
->sectors
= le64_to_cpu(sb
->data_size
);
1410 if (le64_to_cpu(sb
->size
) > rdev
->sectors
)
1415 static int super_1_validate(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1417 struct mdp_superblock_1
*sb
= (struct mdp_superblock_1
*)page_address(rdev
->sb_page
);
1418 __u64 ev1
= le64_to_cpu(sb
->events
);
1420 rdev
->raid_disk
= -1;
1421 clear_bit(Faulty
, &rdev
->flags
);
1422 clear_bit(In_sync
, &rdev
->flags
);
1423 clear_bit(WriteMostly
, &rdev
->flags
);
1425 if (mddev
->raid_disks
== 0) {
1426 mddev
->major_version
= 1;
1427 mddev
->patch_version
= 0;
1428 mddev
->external
= 0;
1429 mddev
->chunk_sectors
= le32_to_cpu(sb
->chunksize
);
1430 mddev
->ctime
= le64_to_cpu(sb
->ctime
) & ((1ULL << 32)-1);
1431 mddev
->utime
= le64_to_cpu(sb
->utime
) & ((1ULL << 32)-1);
1432 mddev
->level
= le32_to_cpu(sb
->level
);
1433 mddev
->clevel
[0] = 0;
1434 mddev
->layout
= le32_to_cpu(sb
->layout
);
1435 mddev
->raid_disks
= le32_to_cpu(sb
->raid_disks
);
1436 mddev
->dev_sectors
= le64_to_cpu(sb
->size
);
1437 mddev
->events
= ev1
;
1438 mddev
->bitmap_info
.offset
= 0;
1439 mddev
->bitmap_info
.default_offset
= 1024 >> 9;
1441 mddev
->recovery_cp
= le64_to_cpu(sb
->resync_offset
);
1442 memcpy(mddev
->uuid
, sb
->set_uuid
, 16);
1444 mddev
->max_disks
= (4096-256)/2;
1446 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_BITMAP_OFFSET
) &&
1447 mddev
->bitmap_info
.file
== NULL
)
1448 mddev
->bitmap_info
.offset
=
1449 (__s32
)le32_to_cpu(sb
->bitmap_offset
);
1451 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RESHAPE_ACTIVE
)) {
1452 mddev
->reshape_position
= le64_to_cpu(sb
->reshape_position
);
1453 mddev
->delta_disks
= le32_to_cpu(sb
->delta_disks
);
1454 mddev
->new_level
= le32_to_cpu(sb
->new_level
);
1455 mddev
->new_layout
= le32_to_cpu(sb
->new_layout
);
1456 mddev
->new_chunk_sectors
= le32_to_cpu(sb
->new_chunk
);
1458 mddev
->reshape_position
= MaxSector
;
1459 mddev
->delta_disks
= 0;
1460 mddev
->new_level
= mddev
->level
;
1461 mddev
->new_layout
= mddev
->layout
;
1462 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
1465 } else if (mddev
->pers
== NULL
) {
1466 /* Insist of good event counter while assembling, except for
1467 * spares (which don't need an event count) */
1469 if (rdev
->desc_nr
>= 0 &&
1470 rdev
->desc_nr
< le32_to_cpu(sb
->max_dev
) &&
1471 le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]) < 0xfffe)
1472 if (ev1
< mddev
->events
)
1474 } else if (mddev
->bitmap
) {
1475 /* If adding to array with a bitmap, then we can accept an
1476 * older device, but not too old.
1478 if (ev1
< mddev
->bitmap
->events_cleared
)
1481 if (ev1
< mddev
->events
)
1482 /* just a hot-add of a new device, leave raid_disk at -1 */
1485 if (mddev
->level
!= LEVEL_MULTIPATH
) {
1487 if (rdev
->desc_nr
< 0 ||
1488 rdev
->desc_nr
>= le32_to_cpu(sb
->max_dev
)) {
1492 role
= le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]);
1494 case 0xffff: /* spare */
1496 case 0xfffe: /* faulty */
1497 set_bit(Faulty
, &rdev
->flags
);
1500 if ((le32_to_cpu(sb
->feature_map
) &
1501 MD_FEATURE_RECOVERY_OFFSET
))
1502 rdev
->recovery_offset
= le64_to_cpu(sb
->recovery_offset
);
1504 set_bit(In_sync
, &rdev
->flags
);
1505 rdev
->raid_disk
= role
;
1508 if (sb
->devflags
& WriteMostly1
)
1509 set_bit(WriteMostly
, &rdev
->flags
);
1510 } else /* MULTIPATH are always insync */
1511 set_bit(In_sync
, &rdev
->flags
);
1516 static void super_1_sync(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1518 struct mdp_superblock_1
*sb
;
1521 /* make rdev->sb match mddev and rdev data. */
1523 sb
= (struct mdp_superblock_1
*)page_address(rdev
->sb_page
);
1525 sb
->feature_map
= 0;
1527 sb
->recovery_offset
= cpu_to_le64(0);
1528 memset(sb
->pad1
, 0, sizeof(sb
->pad1
));
1529 memset(sb
->pad2
, 0, sizeof(sb
->pad2
));
1530 memset(sb
->pad3
, 0, sizeof(sb
->pad3
));
1532 sb
->utime
= cpu_to_le64((__u64
)mddev
->utime
);
1533 sb
->events
= cpu_to_le64(mddev
->events
);
1535 sb
->resync_offset
= cpu_to_le64(mddev
->recovery_cp
);
1537 sb
->resync_offset
= cpu_to_le64(0);
1539 sb
->cnt_corrected_read
= cpu_to_le32(atomic_read(&rdev
->corrected_errors
));
1541 sb
->raid_disks
= cpu_to_le32(mddev
->raid_disks
);
1542 sb
->size
= cpu_to_le64(mddev
->dev_sectors
);
1543 sb
->chunksize
= cpu_to_le32(mddev
->chunk_sectors
);
1544 sb
->level
= cpu_to_le32(mddev
->level
);
1545 sb
->layout
= cpu_to_le32(mddev
->layout
);
1547 if (mddev
->bitmap
&& mddev
->bitmap_info
.file
== NULL
) {
1548 sb
->bitmap_offset
= cpu_to_le32((__u32
)mddev
->bitmap_info
.offset
);
1549 sb
->feature_map
= cpu_to_le32(MD_FEATURE_BITMAP_OFFSET
);
1552 if (rdev
->raid_disk
>= 0 &&
1553 !test_bit(In_sync
, &rdev
->flags
)) {
1555 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET
);
1556 sb
->recovery_offset
=
1557 cpu_to_le64(rdev
->recovery_offset
);
1560 if (mddev
->reshape_position
!= MaxSector
) {
1561 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE
);
1562 sb
->reshape_position
= cpu_to_le64(mddev
->reshape_position
);
1563 sb
->new_layout
= cpu_to_le32(mddev
->new_layout
);
1564 sb
->delta_disks
= cpu_to_le32(mddev
->delta_disks
);
1565 sb
->new_level
= cpu_to_le32(mddev
->new_level
);
1566 sb
->new_chunk
= cpu_to_le32(mddev
->new_chunk_sectors
);
1570 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
)
1571 if (rdev2
->desc_nr
+1 > max_dev
)
1572 max_dev
= rdev2
->desc_nr
+1;
1574 if (max_dev
> le32_to_cpu(sb
->max_dev
)) {
1576 sb
->max_dev
= cpu_to_le32(max_dev
);
1577 rdev
->sb_size
= max_dev
* 2 + 256;
1578 bmask
= queue_logical_block_size(rdev
->bdev
->bd_disk
->queue
)-1;
1579 if (rdev
->sb_size
& bmask
)
1580 rdev
->sb_size
= (rdev
->sb_size
| bmask
) + 1;
1582 for (i
=0; i
<max_dev
;i
++)
1583 sb
->dev_roles
[i
] = cpu_to_le16(0xfffe);
1585 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
) {
1587 if (test_bit(Faulty
, &rdev2
->flags
))
1588 sb
->dev_roles
[i
] = cpu_to_le16(0xfffe);
1589 else if (test_bit(In_sync
, &rdev2
->flags
))
1590 sb
->dev_roles
[i
] = cpu_to_le16(rdev2
->raid_disk
);
1591 else if (rdev2
->raid_disk
>= 0)
1592 sb
->dev_roles
[i
] = cpu_to_le16(rdev2
->raid_disk
);
1594 sb
->dev_roles
[i
] = cpu_to_le16(0xffff);
1597 sb
->sb_csum
= calc_sb_1_csum(sb
);
1600 static unsigned long long
1601 super_1_rdev_size_change(mdk_rdev_t
*rdev
, sector_t num_sectors
)
1603 struct mdp_superblock_1
*sb
;
1604 sector_t max_sectors
;
1605 if (num_sectors
&& num_sectors
< rdev
->mddev
->dev_sectors
)
1606 return 0; /* component must fit device */
1607 if (rdev
->sb_start
< rdev
->data_offset
) {
1608 /* minor versions 1 and 2; superblock before data */
1609 max_sectors
= rdev
->bdev
->bd_inode
->i_size
>> 9;
1610 max_sectors
-= rdev
->data_offset
;
1611 if (!num_sectors
|| num_sectors
> max_sectors
)
1612 num_sectors
= max_sectors
;
1613 } else if (rdev
->mddev
->bitmap_info
.offset
) {
1614 /* minor version 0 with bitmap we can't move */
1617 /* minor version 0; superblock after data */
1619 sb_start
= (rdev
->bdev
->bd_inode
->i_size
>> 9) - 8*2;
1620 sb_start
&= ~(sector_t
)(4*2 - 1);
1621 max_sectors
= rdev
->sectors
+ sb_start
- rdev
->sb_start
;
1622 if (!num_sectors
|| num_sectors
> max_sectors
)
1623 num_sectors
= max_sectors
;
1624 rdev
->sb_start
= sb_start
;
1626 sb
= (struct mdp_superblock_1
*) page_address(rdev
->sb_page
);
1627 sb
->data_size
= cpu_to_le64(num_sectors
);
1628 sb
->super_offset
= rdev
->sb_start
;
1629 sb
->sb_csum
= calc_sb_1_csum(sb
);
1630 md_super_write(rdev
->mddev
, rdev
, rdev
->sb_start
, rdev
->sb_size
,
1632 md_super_wait(rdev
->mddev
);
1633 return num_sectors
/ 2; /* kB for sysfs */
1636 static struct super_type super_types
[] = {
1639 .owner
= THIS_MODULE
,
1640 .load_super
= super_90_load
,
1641 .validate_super
= super_90_validate
,
1642 .sync_super
= super_90_sync
,
1643 .rdev_size_change
= super_90_rdev_size_change
,
1647 .owner
= THIS_MODULE
,
1648 .load_super
= super_1_load
,
1649 .validate_super
= super_1_validate
,
1650 .sync_super
= super_1_sync
,
1651 .rdev_size_change
= super_1_rdev_size_change
,
1655 static int match_mddev_units(mddev_t
*mddev1
, mddev_t
*mddev2
)
1657 mdk_rdev_t
*rdev
, *rdev2
;
1660 rdev_for_each_rcu(rdev
, mddev1
)
1661 rdev_for_each_rcu(rdev2
, mddev2
)
1662 if (rdev
->bdev
->bd_contains
==
1663 rdev2
->bdev
->bd_contains
) {
1671 static LIST_HEAD(pending_raid_disks
);
1674 * Try to register data integrity profile for an mddev
1676 * This is called when an array is started and after a disk has been kicked
1677 * from the array. It only succeeds if all working and active component devices
1678 * are integrity capable with matching profiles.
1680 int md_integrity_register(mddev_t
*mddev
)
1682 mdk_rdev_t
*rdev
, *reference
= NULL
;
1684 if (list_empty(&mddev
->disks
))
1685 return 0; /* nothing to do */
1686 if (blk_get_integrity(mddev
->gendisk
))
1687 return 0; /* already registered */
1688 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
1689 /* skip spares and non-functional disks */
1690 if (test_bit(Faulty
, &rdev
->flags
))
1692 if (rdev
->raid_disk
< 0)
1695 * If at least one rdev is not integrity capable, we can not
1696 * enable data integrity for the md device.
1698 if (!bdev_get_integrity(rdev
->bdev
))
1701 /* Use the first rdev as the reference */
1705 /* does this rdev's profile match the reference profile? */
1706 if (blk_integrity_compare(reference
->bdev
->bd_disk
,
1707 rdev
->bdev
->bd_disk
) < 0)
1711 * All component devices are integrity capable and have matching
1712 * profiles, register the common profile for the md device.
1714 if (blk_integrity_register(mddev
->gendisk
,
1715 bdev_get_integrity(reference
->bdev
)) != 0) {
1716 printk(KERN_ERR
"md: failed to register integrity for %s\n",
1720 printk(KERN_NOTICE
"md: data integrity on %s enabled\n",
1724 EXPORT_SYMBOL(md_integrity_register
);
1726 /* Disable data integrity if non-capable/non-matching disk is being added */
1727 void md_integrity_add_rdev(mdk_rdev_t
*rdev
, mddev_t
*mddev
)
1729 struct blk_integrity
*bi_rdev
= bdev_get_integrity(rdev
->bdev
);
1730 struct blk_integrity
*bi_mddev
= blk_get_integrity(mddev
->gendisk
);
1732 if (!bi_mddev
) /* nothing to do */
1734 if (rdev
->raid_disk
< 0) /* skip spares */
1736 if (bi_rdev
&& blk_integrity_compare(mddev
->gendisk
,
1737 rdev
->bdev
->bd_disk
) >= 0)
1739 printk(KERN_NOTICE
"disabling data integrity on %s\n", mdname(mddev
));
1740 blk_integrity_unregister(mddev
->gendisk
);
1742 EXPORT_SYMBOL(md_integrity_add_rdev
);
1744 static int bind_rdev_to_array(mdk_rdev_t
* rdev
, mddev_t
* mddev
)
1746 char b
[BDEVNAME_SIZE
];
1756 /* prevent duplicates */
1757 if (find_rdev(mddev
, rdev
->bdev
->bd_dev
))
1760 /* make sure rdev->sectors exceeds mddev->dev_sectors */
1761 if (rdev
->sectors
&& (mddev
->dev_sectors
== 0 ||
1762 rdev
->sectors
< mddev
->dev_sectors
)) {
1764 /* Cannot change size, so fail
1765 * If mddev->level <= 0, then we don't care
1766 * about aligning sizes (e.g. linear)
1768 if (mddev
->level
> 0)
1771 mddev
->dev_sectors
= rdev
->sectors
;
1774 /* Verify rdev->desc_nr is unique.
1775 * If it is -1, assign a free number, else
1776 * check number is not in use
1778 if (rdev
->desc_nr
< 0) {
1780 if (mddev
->pers
) choice
= mddev
->raid_disks
;
1781 while (find_rdev_nr(mddev
, choice
))
1783 rdev
->desc_nr
= choice
;
1785 if (find_rdev_nr(mddev
, rdev
->desc_nr
))
1788 if (mddev
->max_disks
&& rdev
->desc_nr
>= mddev
->max_disks
) {
1789 printk(KERN_WARNING
"md: %s: array is limited to %d devices\n",
1790 mdname(mddev
), mddev
->max_disks
);
1793 bdevname(rdev
->bdev
,b
);
1794 while ( (s
=strchr(b
, '/')) != NULL
)
1797 rdev
->mddev
= mddev
;
1798 printk(KERN_INFO
"md: bind<%s>\n", b
);
1800 if ((err
= kobject_add(&rdev
->kobj
, &mddev
->kobj
, "dev-%s", b
)))
1803 ko
= &part_to_dev(rdev
->bdev
->bd_part
)->kobj
;
1804 if (sysfs_create_link(&rdev
->kobj
, ko
, "block"))
1805 /* failure here is OK */;
1806 rdev
->sysfs_state
= sysfs_get_dirent_safe(rdev
->kobj
.sd
, "state");
1808 list_add_rcu(&rdev
->same_set
, &mddev
->disks
);
1809 bd_claim_by_disk(rdev
->bdev
, rdev
->bdev
->bd_holder
, mddev
->gendisk
);
1811 /* May as well allow recovery to be retried once */
1812 mddev
->recovery_disabled
= 0;
1817 printk(KERN_WARNING
"md: failed to register dev-%s for %s\n",
1822 static void md_delayed_delete(struct work_struct
*ws
)
1824 mdk_rdev_t
*rdev
= container_of(ws
, mdk_rdev_t
, del_work
);
1825 kobject_del(&rdev
->kobj
);
1826 kobject_put(&rdev
->kobj
);
1829 static void unbind_rdev_from_array(mdk_rdev_t
* rdev
)
1831 char b
[BDEVNAME_SIZE
];
1836 bd_release_from_disk(rdev
->bdev
, rdev
->mddev
->gendisk
);
1837 list_del_rcu(&rdev
->same_set
);
1838 printk(KERN_INFO
"md: unbind<%s>\n", bdevname(rdev
->bdev
,b
));
1840 sysfs_remove_link(&rdev
->kobj
, "block");
1841 sysfs_put(rdev
->sysfs_state
);
1842 rdev
->sysfs_state
= NULL
;
1843 /* We need to delay this, otherwise we can deadlock when
1844 * writing to 'remove' to "dev/state". We also need
1845 * to delay it due to rcu usage.
1848 INIT_WORK(&rdev
->del_work
, md_delayed_delete
);
1849 kobject_get(&rdev
->kobj
);
1850 schedule_work(&rdev
->del_work
);
1854 * prevent the device from being mounted, repartitioned or
1855 * otherwise reused by a RAID array (or any other kernel
1856 * subsystem), by bd_claiming the device.
1858 static int lock_rdev(mdk_rdev_t
*rdev
, dev_t dev
, int shared
)
1861 struct block_device
*bdev
;
1862 char b
[BDEVNAME_SIZE
];
1864 bdev
= open_by_devnum(dev
, FMODE_READ
|FMODE_WRITE
);
1866 printk(KERN_ERR
"md: could not open %s.\n",
1867 __bdevname(dev
, b
));
1868 return PTR_ERR(bdev
);
1870 err
= bd_claim(bdev
, shared
? (mdk_rdev_t
*)lock_rdev
: rdev
);
1872 printk(KERN_ERR
"md: could not bd_claim %s.\n",
1874 blkdev_put(bdev
, FMODE_READ
|FMODE_WRITE
);
1878 set_bit(AllReserved
, &rdev
->flags
);
1883 static void unlock_rdev(mdk_rdev_t
*rdev
)
1885 struct block_device
*bdev
= rdev
->bdev
;
1890 blkdev_put(bdev
, FMODE_READ
|FMODE_WRITE
);
1893 void md_autodetect_dev(dev_t dev
);
1895 static void export_rdev(mdk_rdev_t
* rdev
)
1897 char b
[BDEVNAME_SIZE
];
1898 printk(KERN_INFO
"md: export_rdev(%s)\n",
1899 bdevname(rdev
->bdev
,b
));
1904 if (test_bit(AutoDetected
, &rdev
->flags
))
1905 md_autodetect_dev(rdev
->bdev
->bd_dev
);
1908 kobject_put(&rdev
->kobj
);
1911 static void kick_rdev_from_array(mdk_rdev_t
* rdev
)
1913 unbind_rdev_from_array(rdev
);
1917 static void export_array(mddev_t
*mddev
)
1919 mdk_rdev_t
*rdev
, *tmp
;
1921 rdev_for_each(rdev
, tmp
, mddev
) {
1926 kick_rdev_from_array(rdev
);
1928 if (!list_empty(&mddev
->disks
))
1930 mddev
->raid_disks
= 0;
1931 mddev
->major_version
= 0;
1934 static void print_desc(mdp_disk_t
*desc
)
1936 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc
->number
,
1937 desc
->major
,desc
->minor
,desc
->raid_disk
,desc
->state
);
1940 static void print_sb_90(mdp_super_t
*sb
)
1945 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1946 sb
->major_version
, sb
->minor_version
, sb
->patch_version
,
1947 sb
->set_uuid0
, sb
->set_uuid1
, sb
->set_uuid2
, sb
->set_uuid3
,
1949 printk(KERN_INFO
"md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1950 sb
->level
, sb
->size
, sb
->nr_disks
, sb
->raid_disks
,
1951 sb
->md_minor
, sb
->layout
, sb
->chunk_size
);
1952 printk(KERN_INFO
"md: UT:%08x ST:%d AD:%d WD:%d"
1953 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1954 sb
->utime
, sb
->state
, sb
->active_disks
, sb
->working_disks
,
1955 sb
->failed_disks
, sb
->spare_disks
,
1956 sb
->sb_csum
, (unsigned long)sb
->events_lo
);
1959 for (i
= 0; i
< MD_SB_DISKS
; i
++) {
1962 desc
= sb
->disks
+ i
;
1963 if (desc
->number
|| desc
->major
|| desc
->minor
||
1964 desc
->raid_disk
|| (desc
->state
&& (desc
->state
!= 4))) {
1965 printk(" D %2d: ", i
);
1969 printk(KERN_INFO
"md: THIS: ");
1970 print_desc(&sb
->this_disk
);
1973 static void print_sb_1(struct mdp_superblock_1
*sb
)
1977 uuid
= sb
->set_uuid
;
1979 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n"
1980 "md: Name: \"%s\" CT:%llu\n",
1981 le32_to_cpu(sb
->major_version
),
1982 le32_to_cpu(sb
->feature_map
),
1985 (unsigned long long)le64_to_cpu(sb
->ctime
)
1986 & MD_SUPERBLOCK_1_TIME_SEC_MASK
);
1988 uuid
= sb
->device_uuid
;
1990 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
1992 "md: Dev:%08x UUID: %pU\n"
1993 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
1994 "md: (MaxDev:%u) \n",
1995 le32_to_cpu(sb
->level
),
1996 (unsigned long long)le64_to_cpu(sb
->size
),
1997 le32_to_cpu(sb
->raid_disks
),
1998 le32_to_cpu(sb
->layout
),
1999 le32_to_cpu(sb
->chunksize
),
2000 (unsigned long long)le64_to_cpu(sb
->data_offset
),
2001 (unsigned long long)le64_to_cpu(sb
->data_size
),
2002 (unsigned long long)le64_to_cpu(sb
->super_offset
),
2003 (unsigned long long)le64_to_cpu(sb
->recovery_offset
),
2004 le32_to_cpu(sb
->dev_number
),
2007 (unsigned long long)le64_to_cpu(sb
->utime
) & MD_SUPERBLOCK_1_TIME_SEC_MASK
,
2008 (unsigned long long)le64_to_cpu(sb
->events
),
2009 (unsigned long long)le64_to_cpu(sb
->resync_offset
),
2010 le32_to_cpu(sb
->sb_csum
),
2011 le32_to_cpu(sb
->max_dev
)
2015 static void print_rdev(mdk_rdev_t
*rdev
, int major_version
)
2017 char b
[BDEVNAME_SIZE
];
2018 printk(KERN_INFO
"md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
2019 bdevname(rdev
->bdev
, b
), (unsigned long long)rdev
->sectors
,
2020 test_bit(Faulty
, &rdev
->flags
), test_bit(In_sync
, &rdev
->flags
),
2022 if (rdev
->sb_loaded
) {
2023 printk(KERN_INFO
"md: rdev superblock (MJ:%d):\n", major_version
);
2024 switch (major_version
) {
2026 print_sb_90((mdp_super_t
*)page_address(rdev
->sb_page
));
2029 print_sb_1((struct mdp_superblock_1
*)page_address(rdev
->sb_page
));
2033 printk(KERN_INFO
"md: no rdev superblock!\n");
2036 static void md_print_devices(void)
2038 struct list_head
*tmp
;
2041 char b
[BDEVNAME_SIZE
];
2044 printk("md: **********************************\n");
2045 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
2046 printk("md: **********************************\n");
2047 for_each_mddev(mddev
, tmp
) {
2050 bitmap_print_sb(mddev
->bitmap
);
2052 printk("%s: ", mdname(mddev
));
2053 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
2054 printk("<%s>", bdevname(rdev
->bdev
,b
));
2057 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
2058 print_rdev(rdev
, mddev
->major_version
);
2060 printk("md: **********************************\n");
2065 static void sync_sbs(mddev_t
* mddev
, int nospares
)
2067 /* Update each superblock (in-memory image), but
2068 * if we are allowed to, skip spares which already
2069 * have the right event counter, or have one earlier
2070 * (which would mean they aren't being marked as dirty
2071 * with the rest of the array)
2074 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
2075 if (rdev
->sb_events
== mddev
->events
||
2077 rdev
->raid_disk
< 0 &&
2078 rdev
->sb_events
+1 == mddev
->events
)) {
2079 /* Don't update this superblock */
2080 rdev
->sb_loaded
= 2;
2082 super_types
[mddev
->major_version
].
2083 sync_super(mddev
, rdev
);
2084 rdev
->sb_loaded
= 1;
2089 static void md_update_sb(mddev_t
* mddev
, int force_change
)
2096 /* First make sure individual recovery_offsets are correct */
2097 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
2098 if (rdev
->raid_disk
>= 0 &&
2099 mddev
->delta_disks
>= 0 &&
2100 !test_bit(In_sync
, &rdev
->flags
) &&
2101 mddev
->curr_resync_completed
> rdev
->recovery_offset
)
2102 rdev
->recovery_offset
= mddev
->curr_resync_completed
;
2105 if (mddev
->external
|| !mddev
->persistent
) {
2106 clear_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
2107 clear_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
2108 wake_up(&mddev
->sb_wait
);
2112 spin_lock_irq(&mddev
->write_lock
);
2114 mddev
->utime
= get_seconds();
2116 set_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
2117 if (test_and_clear_bit(MD_CHANGE_DEVS
, &mddev
->flags
))
2119 if (test_and_clear_bit(MD_CHANGE_CLEAN
, &mddev
->flags
))
2120 /* just a clean<-> dirty transition, possibly leave spares alone,
2121 * though if events isn't the right even/odd, we will have to do
2127 if (mddev
->degraded
)
2128 /* If the array is degraded, then skipping spares is both
2129 * dangerous and fairly pointless.
2130 * Dangerous because a device that was removed from the array
2131 * might have a event_count that still looks up-to-date,
2132 * so it can be re-added without a resync.
2133 * Pointless because if there are any spares to skip,
2134 * then a recovery will happen and soon that array won't
2135 * be degraded any more and the spare can go back to sleep then.
2139 sync_req
= mddev
->in_sync
;
2141 /* If this is just a dirty<->clean transition, and the array is clean
2142 * and 'events' is odd, we can roll back to the previous clean state */
2144 && (mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
)
2145 && mddev
->can_decrease_events
2146 && mddev
->events
!= 1) {
2148 mddev
->can_decrease_events
= 0;
2150 /* otherwise we have to go forward and ... */
2152 mddev
->can_decrease_events
= nospares
;
2155 if (!mddev
->events
) {
2157 * oops, this 64-bit counter should never wrap.
2158 * Either we are in around ~1 trillion A.C., assuming
2159 * 1 reboot per second, or we have a bug:
2164 sync_sbs(mddev
, nospares
);
2165 spin_unlock_irq(&mddev
->write_lock
);
2168 "md: updating %s RAID superblock on device (in sync %d)\n",
2169 mdname(mddev
),mddev
->in_sync
);
2171 bitmap_update_sb(mddev
->bitmap
);
2172 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
2173 char b
[BDEVNAME_SIZE
];
2174 dprintk(KERN_INFO
"md: ");
2175 if (rdev
->sb_loaded
!= 1)
2176 continue; /* no noise on spare devices */
2177 if (test_bit(Faulty
, &rdev
->flags
))
2178 dprintk("(skipping faulty ");
2180 dprintk("%s ", bdevname(rdev
->bdev
,b
));
2181 if (!test_bit(Faulty
, &rdev
->flags
)) {
2182 md_super_write(mddev
,rdev
,
2183 rdev
->sb_start
, rdev
->sb_size
,
2185 dprintk(KERN_INFO
"(write) %s's sb offset: %llu\n",
2186 bdevname(rdev
->bdev
,b
),
2187 (unsigned long long)rdev
->sb_start
);
2188 rdev
->sb_events
= mddev
->events
;
2192 if (mddev
->level
== LEVEL_MULTIPATH
)
2193 /* only need to write one superblock... */
2196 md_super_wait(mddev
);
2197 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
2199 spin_lock_irq(&mddev
->write_lock
);
2200 if (mddev
->in_sync
!= sync_req
||
2201 test_bit(MD_CHANGE_DEVS
, &mddev
->flags
)) {
2202 /* have to write it out again */
2203 spin_unlock_irq(&mddev
->write_lock
);
2206 clear_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
2207 spin_unlock_irq(&mddev
->write_lock
);
2208 wake_up(&mddev
->sb_wait
);
2209 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
2210 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
2214 /* words written to sysfs files may, or may not, be \n terminated.
2215 * We want to accept with case. For this we use cmd_match.
2217 static int cmd_match(const char *cmd
, const char *str
)
2219 /* See if cmd, written into a sysfs file, matches
2220 * str. They must either be the same, or cmd can
2221 * have a trailing newline
2223 while (*cmd
&& *str
&& *cmd
== *str
) {
2234 struct rdev_sysfs_entry
{
2235 struct attribute attr
;
2236 ssize_t (*show
)(mdk_rdev_t
*, char *);
2237 ssize_t (*store
)(mdk_rdev_t
*, const char *, size_t);
2241 state_show(mdk_rdev_t
*rdev
, char *page
)
2246 if (test_bit(Faulty
, &rdev
->flags
)) {
2247 len
+= sprintf(page
+len
, "%sfaulty",sep
);
2250 if (test_bit(In_sync
, &rdev
->flags
)) {
2251 len
+= sprintf(page
+len
, "%sin_sync",sep
);
2254 if (test_bit(WriteMostly
, &rdev
->flags
)) {
2255 len
+= sprintf(page
+len
, "%swrite_mostly",sep
);
2258 if (test_bit(Blocked
, &rdev
->flags
)) {
2259 len
+= sprintf(page
+len
, "%sblocked", sep
);
2262 if (!test_bit(Faulty
, &rdev
->flags
) &&
2263 !test_bit(In_sync
, &rdev
->flags
)) {
2264 len
+= sprintf(page
+len
, "%sspare", sep
);
2267 return len
+sprintf(page
+len
, "\n");
2271 state_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2274 * faulty - simulates and error
2275 * remove - disconnects the device
2276 * writemostly - sets write_mostly
2277 * -writemostly - clears write_mostly
2278 * blocked - sets the Blocked flag
2279 * -blocked - clears the Blocked flag
2280 * insync - sets Insync providing device isn't active
2283 if (cmd_match(buf
, "faulty") && rdev
->mddev
->pers
) {
2284 md_error(rdev
->mddev
, rdev
);
2286 } else if (cmd_match(buf
, "remove")) {
2287 if (rdev
->raid_disk
>= 0)
2290 mddev_t
*mddev
= rdev
->mddev
;
2291 kick_rdev_from_array(rdev
);
2293 md_update_sb(mddev
, 1);
2294 md_new_event(mddev
);
2297 } else if (cmd_match(buf
, "writemostly")) {
2298 set_bit(WriteMostly
, &rdev
->flags
);
2300 } else if (cmd_match(buf
, "-writemostly")) {
2301 clear_bit(WriteMostly
, &rdev
->flags
);
2303 } else if (cmd_match(buf
, "blocked")) {
2304 set_bit(Blocked
, &rdev
->flags
);
2306 } else if (cmd_match(buf
, "-blocked")) {
2307 clear_bit(Blocked
, &rdev
->flags
);
2308 wake_up(&rdev
->blocked_wait
);
2309 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
2310 md_wakeup_thread(rdev
->mddev
->thread
);
2313 } else if (cmd_match(buf
, "insync") && rdev
->raid_disk
== -1) {
2314 set_bit(In_sync
, &rdev
->flags
);
2318 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
2319 return err
? err
: len
;
2321 static struct rdev_sysfs_entry rdev_state
=
2322 __ATTR(state
, S_IRUGO
|S_IWUSR
, state_show
, state_store
);
2325 errors_show(mdk_rdev_t
*rdev
, char *page
)
2327 return sprintf(page
, "%d\n", atomic_read(&rdev
->corrected_errors
));
2331 errors_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2334 unsigned long n
= simple_strtoul(buf
, &e
, 10);
2335 if (*buf
&& (*e
== 0 || *e
== '\n')) {
2336 atomic_set(&rdev
->corrected_errors
, n
);
2341 static struct rdev_sysfs_entry rdev_errors
=
2342 __ATTR(errors
, S_IRUGO
|S_IWUSR
, errors_show
, errors_store
);
2345 slot_show(mdk_rdev_t
*rdev
, char *page
)
2347 if (rdev
->raid_disk
< 0)
2348 return sprintf(page
, "none\n");
2350 return sprintf(page
, "%d\n", rdev
->raid_disk
);
2354 slot_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2359 int slot
= simple_strtoul(buf
, &e
, 10);
2360 if (strncmp(buf
, "none", 4)==0)
2362 else if (e
==buf
|| (*e
&& *e
!= '\n'))
2364 if (rdev
->mddev
->pers
&& slot
== -1) {
2365 /* Setting 'slot' on an active array requires also
2366 * updating the 'rd%d' link, and communicating
2367 * with the personality with ->hot_*_disk.
2368 * For now we only support removing
2369 * failed/spare devices. This normally happens automatically,
2370 * but not when the metadata is externally managed.
2372 if (rdev
->raid_disk
== -1)
2374 /* personality does all needed checks */
2375 if (rdev
->mddev
->pers
->hot_add_disk
== NULL
)
2377 err
= rdev
->mddev
->pers
->
2378 hot_remove_disk(rdev
->mddev
, rdev
->raid_disk
);
2381 sprintf(nm
, "rd%d", rdev
->raid_disk
);
2382 sysfs_remove_link(&rdev
->mddev
->kobj
, nm
);
2383 rdev
->raid_disk
= -1;
2384 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
2385 md_wakeup_thread(rdev
->mddev
->thread
);
2386 } else if (rdev
->mddev
->pers
) {
2388 /* Activating a spare .. or possibly reactivating
2389 * if we ever get bitmaps working here.
2392 if (rdev
->raid_disk
!= -1)
2395 if (rdev
->mddev
->pers
->hot_add_disk
== NULL
)
2398 list_for_each_entry(rdev2
, &rdev
->mddev
->disks
, same_set
)
2399 if (rdev2
->raid_disk
== slot
)
2402 rdev
->raid_disk
= slot
;
2403 if (test_bit(In_sync
, &rdev
->flags
))
2404 rdev
->saved_raid_disk
= slot
;
2406 rdev
->saved_raid_disk
= -1;
2407 err
= rdev
->mddev
->pers
->
2408 hot_add_disk(rdev
->mddev
, rdev
);
2410 rdev
->raid_disk
= -1;
2413 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
2414 sprintf(nm
, "rd%d", rdev
->raid_disk
);
2415 if (sysfs_create_link(&rdev
->mddev
->kobj
, &rdev
->kobj
, nm
))
2416 /* failure here is OK */;
2417 /* don't wakeup anyone, leave that to userspace. */
2419 if (slot
>= rdev
->mddev
->raid_disks
)
2421 rdev
->raid_disk
= slot
;
2422 /* assume it is working */
2423 clear_bit(Faulty
, &rdev
->flags
);
2424 clear_bit(WriteMostly
, &rdev
->flags
);
2425 set_bit(In_sync
, &rdev
->flags
);
2426 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
2432 static struct rdev_sysfs_entry rdev_slot
=
2433 __ATTR(slot
, S_IRUGO
|S_IWUSR
, slot_show
, slot_store
);
2436 offset_show(mdk_rdev_t
*rdev
, char *page
)
2438 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->data_offset
);
2442 offset_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2445 unsigned long long offset
= simple_strtoull(buf
, &e
, 10);
2446 if (e
==buf
|| (*e
&& *e
!= '\n'))
2448 if (rdev
->mddev
->pers
&& rdev
->raid_disk
>= 0)
2450 if (rdev
->sectors
&& rdev
->mddev
->external
)
2451 /* Must set offset before size, so overlap checks
2454 rdev
->data_offset
= offset
;
2458 static struct rdev_sysfs_entry rdev_offset
=
2459 __ATTR(offset
, S_IRUGO
|S_IWUSR
, offset_show
, offset_store
);
2462 rdev_size_show(mdk_rdev_t
*rdev
, char *page
)
2464 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->sectors
/ 2);
2467 static int overlaps(sector_t s1
, sector_t l1
, sector_t s2
, sector_t l2
)
2469 /* check if two start/length pairs overlap */
2477 static int strict_blocks_to_sectors(const char *buf
, sector_t
*sectors
)
2479 unsigned long long blocks
;
2482 if (strict_strtoull(buf
, 10, &blocks
) < 0)
2485 if (blocks
& 1ULL << (8 * sizeof(blocks
) - 1))
2486 return -EINVAL
; /* sector conversion overflow */
2489 if (new != blocks
* 2)
2490 return -EINVAL
; /* unsigned long long to sector_t overflow */
2497 rdev_size_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2499 mddev_t
*my_mddev
= rdev
->mddev
;
2500 sector_t oldsectors
= rdev
->sectors
;
2503 if (strict_blocks_to_sectors(buf
, §ors
) < 0)
2505 if (my_mddev
->pers
&& rdev
->raid_disk
>= 0) {
2506 if (my_mddev
->persistent
) {
2507 sectors
= super_types
[my_mddev
->major_version
].
2508 rdev_size_change(rdev
, sectors
);
2511 } else if (!sectors
)
2512 sectors
= (rdev
->bdev
->bd_inode
->i_size
>> 9) -
2515 if (sectors
< my_mddev
->dev_sectors
)
2516 return -EINVAL
; /* component must fit device */
2518 rdev
->sectors
= sectors
;
2519 if (sectors
> oldsectors
&& my_mddev
->external
) {
2520 /* need to check that all other rdevs with the same ->bdev
2521 * do not overlap. We need to unlock the mddev to avoid
2522 * a deadlock. We have already changed rdev->sectors, and if
2523 * we have to change it back, we will have the lock again.
2527 struct list_head
*tmp
;
2529 mddev_unlock(my_mddev
);
2530 for_each_mddev(mddev
, tmp
) {
2534 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
)
2535 if (test_bit(AllReserved
, &rdev2
->flags
) ||
2536 (rdev
->bdev
== rdev2
->bdev
&&
2538 overlaps(rdev
->data_offset
, rdev
->sectors
,
2544 mddev_unlock(mddev
);
2550 mddev_lock(my_mddev
);
2552 /* Someone else could have slipped in a size
2553 * change here, but doing so is just silly.
2554 * We put oldsectors back because we *know* it is
2555 * safe, and trust userspace not to race with
2558 rdev
->sectors
= oldsectors
;
2565 static struct rdev_sysfs_entry rdev_size
=
2566 __ATTR(size
, S_IRUGO
|S_IWUSR
, rdev_size_show
, rdev_size_store
);
2569 static ssize_t
recovery_start_show(mdk_rdev_t
*rdev
, char *page
)
2571 unsigned long long recovery_start
= rdev
->recovery_offset
;
2573 if (test_bit(In_sync
, &rdev
->flags
) ||
2574 recovery_start
== MaxSector
)
2575 return sprintf(page
, "none\n");
2577 return sprintf(page
, "%llu\n", recovery_start
);
2580 static ssize_t
recovery_start_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2582 unsigned long long recovery_start
;
2584 if (cmd_match(buf
, "none"))
2585 recovery_start
= MaxSector
;
2586 else if (strict_strtoull(buf
, 10, &recovery_start
))
2589 if (rdev
->mddev
->pers
&&
2590 rdev
->raid_disk
>= 0)
2593 rdev
->recovery_offset
= recovery_start
;
2594 if (recovery_start
== MaxSector
)
2595 set_bit(In_sync
, &rdev
->flags
);
2597 clear_bit(In_sync
, &rdev
->flags
);
2601 static struct rdev_sysfs_entry rdev_recovery_start
=
2602 __ATTR(recovery_start
, S_IRUGO
|S_IWUSR
, recovery_start_show
, recovery_start_store
);
2604 static struct attribute
*rdev_default_attrs
[] = {
2610 &rdev_recovery_start
.attr
,
2614 rdev_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
2616 struct rdev_sysfs_entry
*entry
= container_of(attr
, struct rdev_sysfs_entry
, attr
);
2617 mdk_rdev_t
*rdev
= container_of(kobj
, mdk_rdev_t
, kobj
);
2618 mddev_t
*mddev
= rdev
->mddev
;
2624 rv
= mddev
? mddev_lock(mddev
) : -EBUSY
;
2626 if (rdev
->mddev
== NULL
)
2629 rv
= entry
->show(rdev
, page
);
2630 mddev_unlock(mddev
);
2636 rdev_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
2637 const char *page
, size_t length
)
2639 struct rdev_sysfs_entry
*entry
= container_of(attr
, struct rdev_sysfs_entry
, attr
);
2640 mdk_rdev_t
*rdev
= container_of(kobj
, mdk_rdev_t
, kobj
);
2642 mddev_t
*mddev
= rdev
->mddev
;
2646 if (!capable(CAP_SYS_ADMIN
))
2648 rv
= mddev
? mddev_lock(mddev
): -EBUSY
;
2650 if (rdev
->mddev
== NULL
)
2653 rv
= entry
->store(rdev
, page
, length
);
2654 mddev_unlock(mddev
);
2659 static void rdev_free(struct kobject
*ko
)
2661 mdk_rdev_t
*rdev
= container_of(ko
, mdk_rdev_t
, kobj
);
2664 static const struct sysfs_ops rdev_sysfs_ops
= {
2665 .show
= rdev_attr_show
,
2666 .store
= rdev_attr_store
,
2668 static struct kobj_type rdev_ktype
= {
2669 .release
= rdev_free
,
2670 .sysfs_ops
= &rdev_sysfs_ops
,
2671 .default_attrs
= rdev_default_attrs
,
2674 void md_rdev_init(mdk_rdev_t
*rdev
)
2677 rdev
->saved_raid_disk
= -1;
2678 rdev
->raid_disk
= -1;
2680 rdev
->data_offset
= 0;
2681 rdev
->sb_events
= 0;
2682 rdev
->last_read_error
.tv_sec
= 0;
2683 rdev
->last_read_error
.tv_nsec
= 0;
2684 atomic_set(&rdev
->nr_pending
, 0);
2685 atomic_set(&rdev
->read_errors
, 0);
2686 atomic_set(&rdev
->corrected_errors
, 0);
2688 INIT_LIST_HEAD(&rdev
->same_set
);
2689 init_waitqueue_head(&rdev
->blocked_wait
);
2691 EXPORT_SYMBOL_GPL(md_rdev_init
);
2693 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2695 * mark the device faulty if:
2697 * - the device is nonexistent (zero size)
2698 * - the device has no valid superblock
2700 * a faulty rdev _never_ has rdev->sb set.
2702 static mdk_rdev_t
*md_import_device(dev_t newdev
, int super_format
, int super_minor
)
2704 char b
[BDEVNAME_SIZE
];
2709 rdev
= kzalloc(sizeof(*rdev
), GFP_KERNEL
);
2711 printk(KERN_ERR
"md: could not alloc mem for new device!\n");
2712 return ERR_PTR(-ENOMEM
);
2716 if ((err
= alloc_disk_sb(rdev
)))
2719 err
= lock_rdev(rdev
, newdev
, super_format
== -2);
2723 kobject_init(&rdev
->kobj
, &rdev_ktype
);
2725 size
= rdev
->bdev
->bd_inode
->i_size
>> BLOCK_SIZE_BITS
;
2728 "md: %s has zero or unknown size, marking faulty!\n",
2729 bdevname(rdev
->bdev
,b
));
2734 if (super_format
>= 0) {
2735 err
= super_types
[super_format
].
2736 load_super(rdev
, NULL
, super_minor
);
2737 if (err
== -EINVAL
) {
2739 "md: %s does not have a valid v%d.%d "
2740 "superblock, not importing!\n",
2741 bdevname(rdev
->bdev
,b
),
2742 super_format
, super_minor
);
2747 "md: could not read %s's sb, not importing!\n",
2748 bdevname(rdev
->bdev
,b
));
2756 if (rdev
->sb_page
) {
2762 return ERR_PTR(err
);
2766 * Check a full RAID array for plausibility
2770 static void analyze_sbs(mddev_t
* mddev
)
2773 mdk_rdev_t
*rdev
, *freshest
, *tmp
;
2774 char b
[BDEVNAME_SIZE
];
2777 rdev_for_each(rdev
, tmp
, mddev
)
2778 switch (super_types
[mddev
->major_version
].
2779 load_super(rdev
, freshest
, mddev
->minor_version
)) {
2787 "md: fatal superblock inconsistency in %s"
2788 " -- removing from array\n",
2789 bdevname(rdev
->bdev
,b
));
2790 kick_rdev_from_array(rdev
);
2794 super_types
[mddev
->major_version
].
2795 validate_super(mddev
, freshest
);
2798 rdev_for_each(rdev
, tmp
, mddev
) {
2799 if (mddev
->max_disks
&&
2800 (rdev
->desc_nr
>= mddev
->max_disks
||
2801 i
> mddev
->max_disks
)) {
2803 "md: %s: %s: only %d devices permitted\n",
2804 mdname(mddev
), bdevname(rdev
->bdev
, b
),
2806 kick_rdev_from_array(rdev
);
2809 if (rdev
!= freshest
)
2810 if (super_types
[mddev
->major_version
].
2811 validate_super(mddev
, rdev
)) {
2812 printk(KERN_WARNING
"md: kicking non-fresh %s"
2814 bdevname(rdev
->bdev
,b
));
2815 kick_rdev_from_array(rdev
);
2818 if (mddev
->level
== LEVEL_MULTIPATH
) {
2819 rdev
->desc_nr
= i
++;
2820 rdev
->raid_disk
= rdev
->desc_nr
;
2821 set_bit(In_sync
, &rdev
->flags
);
2822 } else if (rdev
->raid_disk
>= (mddev
->raid_disks
- min(0, mddev
->delta_disks
))) {
2823 rdev
->raid_disk
= -1;
2824 clear_bit(In_sync
, &rdev
->flags
);
2829 /* Read a fixed-point number.
2830 * Numbers in sysfs attributes should be in "standard" units where
2831 * possible, so time should be in seconds.
2832 * However we internally use a a much smaller unit such as
2833 * milliseconds or jiffies.
2834 * This function takes a decimal number with a possible fractional
2835 * component, and produces an integer which is the result of
2836 * multiplying that number by 10^'scale'.
2837 * all without any floating-point arithmetic.
2839 int strict_strtoul_scaled(const char *cp
, unsigned long *res
, int scale
)
2841 unsigned long result
= 0;
2843 while (isdigit(*cp
) || (*cp
== '.' && decimals
< 0)) {
2846 else if (decimals
< scale
) {
2849 result
= result
* 10 + value
;
2861 while (decimals
< scale
) {
2870 static void md_safemode_timeout(unsigned long data
);
2873 safe_delay_show(mddev_t
*mddev
, char *page
)
2875 int msec
= (mddev
->safemode_delay
*1000)/HZ
;
2876 return sprintf(page
, "%d.%03d\n", msec
/1000, msec
%1000);
2879 safe_delay_store(mddev_t
*mddev
, const char *cbuf
, size_t len
)
2883 if (strict_strtoul_scaled(cbuf
, &msec
, 3) < 0)
2886 mddev
->safemode_delay
= 0;
2888 unsigned long old_delay
= mddev
->safemode_delay
;
2889 mddev
->safemode_delay
= (msec
*HZ
)/1000;
2890 if (mddev
->safemode_delay
== 0)
2891 mddev
->safemode_delay
= 1;
2892 if (mddev
->safemode_delay
< old_delay
)
2893 md_safemode_timeout((unsigned long)mddev
);
2897 static struct md_sysfs_entry md_safe_delay
=
2898 __ATTR(safe_mode_delay
, S_IRUGO
|S_IWUSR
,safe_delay_show
, safe_delay_store
);
2901 level_show(mddev_t
*mddev
, char *page
)
2903 struct mdk_personality
*p
= mddev
->pers
;
2905 return sprintf(page
, "%s\n", p
->name
);
2906 else if (mddev
->clevel
[0])
2907 return sprintf(page
, "%s\n", mddev
->clevel
);
2908 else if (mddev
->level
!= LEVEL_NONE
)
2909 return sprintf(page
, "%d\n", mddev
->level
);
2915 level_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2919 struct mdk_personality
*pers
;
2924 if (mddev
->pers
== NULL
) {
2927 if (len
>= sizeof(mddev
->clevel
))
2929 strncpy(mddev
->clevel
, buf
, len
);
2930 if (mddev
->clevel
[len
-1] == '\n')
2932 mddev
->clevel
[len
] = 0;
2933 mddev
->level
= LEVEL_NONE
;
2937 /* request to change the personality. Need to ensure:
2938 * - array is not engaged in resync/recovery/reshape
2939 * - old personality can be suspended
2940 * - new personality will access other array.
2943 if (mddev
->sync_thread
||
2944 mddev
->reshape_position
!= MaxSector
||
2945 mddev
->sysfs_active
)
2948 if (!mddev
->pers
->quiesce
) {
2949 printk(KERN_WARNING
"md: %s: %s does not support online personality change\n",
2950 mdname(mddev
), mddev
->pers
->name
);
2954 /* Now find the new personality */
2955 if (len
== 0 || len
>= sizeof(clevel
))
2957 strncpy(clevel
, buf
, len
);
2958 if (clevel
[len
-1] == '\n')
2961 if (strict_strtol(clevel
, 10, &level
))
2964 if (request_module("md-%s", clevel
) != 0)
2965 request_module("md-level-%s", clevel
);
2966 spin_lock(&pers_lock
);
2967 pers
= find_pers(level
, clevel
);
2968 if (!pers
|| !try_module_get(pers
->owner
)) {
2969 spin_unlock(&pers_lock
);
2970 printk(KERN_WARNING
"md: personality %s not loaded\n", clevel
);
2973 spin_unlock(&pers_lock
);
2975 if (pers
== mddev
->pers
) {
2976 /* Nothing to do! */
2977 module_put(pers
->owner
);
2980 if (!pers
->takeover
) {
2981 module_put(pers
->owner
);
2982 printk(KERN_WARNING
"md: %s: %s does not support personality takeover\n",
2983 mdname(mddev
), clevel
);
2987 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
2988 rdev
->new_raid_disk
= rdev
->raid_disk
;
2990 /* ->takeover must set new_* and/or delta_disks
2991 * if it succeeds, and may set them when it fails.
2993 priv
= pers
->takeover(mddev
);
2995 mddev
->new_level
= mddev
->level
;
2996 mddev
->new_layout
= mddev
->layout
;
2997 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
2998 mddev
->raid_disks
-= mddev
->delta_disks
;
2999 mddev
->delta_disks
= 0;
3000 module_put(pers
->owner
);
3001 printk(KERN_WARNING
"md: %s: %s would not accept array\n",
3002 mdname(mddev
), clevel
);
3003 return PTR_ERR(priv
);
3006 /* Looks like we have a winner */
3007 mddev_suspend(mddev
);
3008 mddev
->pers
->stop(mddev
);
3010 if (mddev
->pers
->sync_request
== NULL
&&
3011 pers
->sync_request
!= NULL
) {
3012 /* need to add the md_redundancy_group */
3013 if (sysfs_create_group(&mddev
->kobj
, &md_redundancy_group
))
3015 "md: cannot register extra attributes for %s\n",
3017 mddev
->sysfs_action
= sysfs_get_dirent(mddev
->kobj
.sd
, NULL
, "sync_action");
3019 if (mddev
->pers
->sync_request
!= NULL
&&
3020 pers
->sync_request
== NULL
) {
3021 /* need to remove the md_redundancy_group */
3022 if (mddev
->to_remove
== NULL
)
3023 mddev
->to_remove
= &md_redundancy_group
;
3026 if (mddev
->pers
->sync_request
== NULL
&&
3028 /* We are converting from a no-redundancy array
3029 * to a redundancy array and metadata is managed
3030 * externally so we need to be sure that writes
3031 * won't block due to a need to transition
3033 * until external management is started.
3036 mddev
->safemode_delay
= 0;
3037 mddev
->safemode
= 0;
3040 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
3042 if (rdev
->raid_disk
< 0)
3044 if (rdev
->new_raid_disk
> mddev
->raid_disks
)
3045 rdev
->new_raid_disk
= -1;
3046 if (rdev
->new_raid_disk
== rdev
->raid_disk
)
3048 sprintf(nm
, "rd%d", rdev
->raid_disk
);
3049 sysfs_remove_link(&mddev
->kobj
, nm
);
3051 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
3052 if (rdev
->raid_disk
< 0)
3054 if (rdev
->new_raid_disk
== rdev
->raid_disk
)
3056 rdev
->raid_disk
= rdev
->new_raid_disk
;
3057 if (rdev
->raid_disk
< 0)
3058 clear_bit(In_sync
, &rdev
->flags
);
3061 sprintf(nm
, "rd%d", rdev
->raid_disk
);
3062 if(sysfs_create_link(&mddev
->kobj
, &rdev
->kobj
, nm
))
3063 printk("md: cannot register %s for %s after level change\n",
3068 module_put(mddev
->pers
->owner
);
3070 mddev
->private = priv
;
3071 strlcpy(mddev
->clevel
, pers
->name
, sizeof(mddev
->clevel
));
3072 mddev
->level
= mddev
->new_level
;
3073 mddev
->layout
= mddev
->new_layout
;
3074 mddev
->chunk_sectors
= mddev
->new_chunk_sectors
;
3075 mddev
->delta_disks
= 0;
3076 if (mddev
->pers
->sync_request
== NULL
) {
3077 /* this is now an array without redundancy, so
3078 * it must always be in_sync
3081 del_timer_sync(&mddev
->safemode_timer
);
3084 mddev_resume(mddev
);
3085 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
3086 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3087 md_wakeup_thread(mddev
->thread
);
3088 sysfs_notify(&mddev
->kobj
, NULL
, "level");
3089 md_new_event(mddev
);
3093 static struct md_sysfs_entry md_level
=
3094 __ATTR(level
, S_IRUGO
|S_IWUSR
, level_show
, level_store
);
3098 layout_show(mddev_t
*mddev
, char *page
)
3100 /* just a number, not meaningful for all levels */
3101 if (mddev
->reshape_position
!= MaxSector
&&
3102 mddev
->layout
!= mddev
->new_layout
)
3103 return sprintf(page
, "%d (%d)\n",
3104 mddev
->new_layout
, mddev
->layout
);
3105 return sprintf(page
, "%d\n", mddev
->layout
);
3109 layout_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3112 unsigned long n
= simple_strtoul(buf
, &e
, 10);
3114 if (!*buf
|| (*e
&& *e
!= '\n'))
3119 if (mddev
->pers
->check_reshape
== NULL
)
3121 mddev
->new_layout
= n
;
3122 err
= mddev
->pers
->check_reshape(mddev
);
3124 mddev
->new_layout
= mddev
->layout
;
3128 mddev
->new_layout
= n
;
3129 if (mddev
->reshape_position
== MaxSector
)
3134 static struct md_sysfs_entry md_layout
=
3135 __ATTR(layout
, S_IRUGO
|S_IWUSR
, layout_show
, layout_store
);
3139 raid_disks_show(mddev_t
*mddev
, char *page
)
3141 if (mddev
->raid_disks
== 0)
3143 if (mddev
->reshape_position
!= MaxSector
&&
3144 mddev
->delta_disks
!= 0)
3145 return sprintf(page
, "%d (%d)\n", mddev
->raid_disks
,
3146 mddev
->raid_disks
- mddev
->delta_disks
);
3147 return sprintf(page
, "%d\n", mddev
->raid_disks
);
3150 static int update_raid_disks(mddev_t
*mddev
, int raid_disks
);
3153 raid_disks_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3157 unsigned long n
= simple_strtoul(buf
, &e
, 10);
3159 if (!*buf
|| (*e
&& *e
!= '\n'))
3163 rv
= update_raid_disks(mddev
, n
);
3164 else if (mddev
->reshape_position
!= MaxSector
) {
3165 int olddisks
= mddev
->raid_disks
- mddev
->delta_disks
;
3166 mddev
->delta_disks
= n
- olddisks
;
3167 mddev
->raid_disks
= n
;
3169 mddev
->raid_disks
= n
;
3170 return rv
? rv
: len
;
3172 static struct md_sysfs_entry md_raid_disks
=
3173 __ATTR(raid_disks
, S_IRUGO
|S_IWUSR
, raid_disks_show
, raid_disks_store
);
3176 chunk_size_show(mddev_t
*mddev
, char *page
)
3178 if (mddev
->reshape_position
!= MaxSector
&&
3179 mddev
->chunk_sectors
!= mddev
->new_chunk_sectors
)
3180 return sprintf(page
, "%d (%d)\n",
3181 mddev
->new_chunk_sectors
<< 9,
3182 mddev
->chunk_sectors
<< 9);
3183 return sprintf(page
, "%d\n", mddev
->chunk_sectors
<< 9);
3187 chunk_size_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3190 unsigned long n
= simple_strtoul(buf
, &e
, 10);
3192 if (!*buf
|| (*e
&& *e
!= '\n'))
3197 if (mddev
->pers
->check_reshape
== NULL
)
3199 mddev
->new_chunk_sectors
= n
>> 9;
3200 err
= mddev
->pers
->check_reshape(mddev
);
3202 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
3206 mddev
->new_chunk_sectors
= n
>> 9;
3207 if (mddev
->reshape_position
== MaxSector
)
3208 mddev
->chunk_sectors
= n
>> 9;
3212 static struct md_sysfs_entry md_chunk_size
=
3213 __ATTR(chunk_size
, S_IRUGO
|S_IWUSR
, chunk_size_show
, chunk_size_store
);
3216 resync_start_show(mddev_t
*mddev
, char *page
)
3218 if (mddev
->recovery_cp
== MaxSector
)
3219 return sprintf(page
, "none\n");
3220 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->recovery_cp
);
3224 resync_start_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3227 unsigned long long n
= simple_strtoull(buf
, &e
, 10);
3231 if (cmd_match(buf
, "none"))
3233 else if (!*buf
|| (*e
&& *e
!= '\n'))
3236 mddev
->recovery_cp
= n
;
3239 static struct md_sysfs_entry md_resync_start
=
3240 __ATTR(resync_start
, S_IRUGO
|S_IWUSR
, resync_start_show
, resync_start_store
);
3243 * The array state can be:
3246 * No devices, no size, no level
3247 * Equivalent to STOP_ARRAY ioctl
3249 * May have some settings, but array is not active
3250 * all IO results in error
3251 * When written, doesn't tear down array, but just stops it
3252 * suspended (not supported yet)
3253 * All IO requests will block. The array can be reconfigured.
3254 * Writing this, if accepted, will block until array is quiescent
3256 * no resync can happen. no superblocks get written.
3257 * write requests fail
3259 * like readonly, but behaves like 'clean' on a write request.
3261 * clean - no pending writes, but otherwise active.
3262 * When written to inactive array, starts without resync
3263 * If a write request arrives then
3264 * if metadata is known, mark 'dirty' and switch to 'active'.
3265 * if not known, block and switch to write-pending
3266 * If written to an active array that has pending writes, then fails.
3268 * fully active: IO and resync can be happening.
3269 * When written to inactive array, starts with resync
3272 * clean, but writes are blocked waiting for 'active' to be written.
3275 * like active, but no writes have been seen for a while (100msec).
3278 enum array_state
{ clear
, inactive
, suspended
, readonly
, read_auto
, clean
, active
,
3279 write_pending
, active_idle
, bad_word
};
3280 static char *array_states
[] = {
3281 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3282 "write-pending", "active-idle", NULL
};
3284 static int match_word(const char *word
, char **list
)
3287 for (n
=0; list
[n
]; n
++)
3288 if (cmd_match(word
, list
[n
]))
3294 array_state_show(mddev_t
*mddev
, char *page
)
3296 enum array_state st
= inactive
;
3309 else if (test_bit(MD_CHANGE_CLEAN
, &mddev
->flags
))
3311 else if (mddev
->safemode
)
3317 if (list_empty(&mddev
->disks
) &&
3318 mddev
->raid_disks
== 0 &&
3319 mddev
->dev_sectors
== 0)
3324 return sprintf(page
, "%s\n", array_states
[st
]);
3327 static int do_md_stop(mddev_t
* mddev
, int ro
, int is_open
);
3328 static int md_set_readonly(mddev_t
* mddev
, int is_open
);
3329 static int do_md_run(mddev_t
* mddev
);
3330 static int restart_array(mddev_t
*mddev
);
3333 array_state_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3336 enum array_state st
= match_word(buf
, array_states
);
3341 /* stopping an active array */
3342 if (atomic_read(&mddev
->openers
) > 0)
3344 err
= do_md_stop(mddev
, 0, 0);
3347 /* stopping an active array */
3349 if (atomic_read(&mddev
->openers
) > 0)
3351 err
= do_md_stop(mddev
, 2, 0);
3353 err
= 0; /* already inactive */
3356 break; /* not supported yet */
3359 err
= md_set_readonly(mddev
, 0);
3362 set_disk_ro(mddev
->gendisk
, 1);
3363 err
= do_md_run(mddev
);
3369 err
= md_set_readonly(mddev
, 0);
3370 else if (mddev
->ro
== 1)
3371 err
= restart_array(mddev
);
3374 set_disk_ro(mddev
->gendisk
, 0);
3378 err
= do_md_run(mddev
);
3383 restart_array(mddev
);
3384 spin_lock_irq(&mddev
->write_lock
);
3385 if (atomic_read(&mddev
->writes_pending
) == 0) {
3386 if (mddev
->in_sync
== 0) {
3388 if (mddev
->safemode
== 1)
3389 mddev
->safemode
= 0;
3390 if (mddev
->persistent
)
3391 set_bit(MD_CHANGE_CLEAN
,
3397 spin_unlock_irq(&mddev
->write_lock
);
3403 restart_array(mddev
);
3404 if (mddev
->external
)
3405 clear_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
3406 wake_up(&mddev
->sb_wait
);
3410 set_disk_ro(mddev
->gendisk
, 0);
3411 err
= do_md_run(mddev
);
3416 /* these cannot be set */
3422 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
3426 static struct md_sysfs_entry md_array_state
=
3427 __ATTR(array_state
, S_IRUGO
|S_IWUSR
, array_state_show
, array_state_store
);
3430 max_corrected_read_errors_show(mddev_t
*mddev
, char *page
) {
3431 return sprintf(page
, "%d\n",
3432 atomic_read(&mddev
->max_corr_read_errors
));
3436 max_corrected_read_errors_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3439 unsigned long n
= simple_strtoul(buf
, &e
, 10);
3441 if (*buf
&& (*e
== 0 || *e
== '\n')) {
3442 atomic_set(&mddev
->max_corr_read_errors
, n
);
3448 static struct md_sysfs_entry max_corr_read_errors
=
3449 __ATTR(max_read_errors
, S_IRUGO
|S_IWUSR
, max_corrected_read_errors_show
,
3450 max_corrected_read_errors_store
);
3453 null_show(mddev_t
*mddev
, char *page
)
3459 new_dev_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3461 /* buf must be %d:%d\n? giving major and minor numbers */
3462 /* The new device is added to the array.
3463 * If the array has a persistent superblock, we read the
3464 * superblock to initialise info and check validity.
3465 * Otherwise, only checking done is that in bind_rdev_to_array,
3466 * which mainly checks size.
3469 int major
= simple_strtoul(buf
, &e
, 10);
3475 if (!*buf
|| *e
!= ':' || !e
[1] || e
[1] == '\n')
3477 minor
= simple_strtoul(e
+1, &e
, 10);
3478 if (*e
&& *e
!= '\n')
3480 dev
= MKDEV(major
, minor
);
3481 if (major
!= MAJOR(dev
) ||
3482 minor
!= MINOR(dev
))
3486 if (mddev
->persistent
) {
3487 rdev
= md_import_device(dev
, mddev
->major_version
,
3488 mddev
->minor_version
);
3489 if (!IS_ERR(rdev
) && !list_empty(&mddev
->disks
)) {
3490 mdk_rdev_t
*rdev0
= list_entry(mddev
->disks
.next
,
3491 mdk_rdev_t
, same_set
);
3492 err
= super_types
[mddev
->major_version
]
3493 .load_super(rdev
, rdev0
, mddev
->minor_version
);
3497 } else if (mddev
->external
)
3498 rdev
= md_import_device(dev
, -2, -1);
3500 rdev
= md_import_device(dev
, -1, -1);
3503 return PTR_ERR(rdev
);
3504 err
= bind_rdev_to_array(rdev
, mddev
);
3508 return err
? err
: len
;
3511 static struct md_sysfs_entry md_new_device
=
3512 __ATTR(new_dev
, S_IWUSR
, null_show
, new_dev_store
);
3515 bitmap_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3518 unsigned long chunk
, end_chunk
;
3522 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3524 chunk
= end_chunk
= simple_strtoul(buf
, &end
, 0);
3525 if (buf
== end
) break;
3526 if (*end
== '-') { /* range */
3528 end_chunk
= simple_strtoul(buf
, &end
, 0);
3529 if (buf
== end
) break;
3531 if (*end
&& !isspace(*end
)) break;
3532 bitmap_dirty_bits(mddev
->bitmap
, chunk
, end_chunk
);
3533 buf
= skip_spaces(end
);
3535 bitmap_unplug(mddev
->bitmap
); /* flush the bits to disk */
3540 static struct md_sysfs_entry md_bitmap
=
3541 __ATTR(bitmap_set_bits
, S_IWUSR
, null_show
, bitmap_store
);
3544 size_show(mddev_t
*mddev
, char *page
)
3546 return sprintf(page
, "%llu\n",
3547 (unsigned long long)mddev
->dev_sectors
/ 2);
3550 static int update_size(mddev_t
*mddev
, sector_t num_sectors
);
3553 size_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3555 /* If array is inactive, we can reduce the component size, but
3556 * not increase it (except from 0).
3557 * If array is active, we can try an on-line resize
3560 int err
= strict_blocks_to_sectors(buf
, §ors
);
3565 err
= update_size(mddev
, sectors
);
3566 md_update_sb(mddev
, 1);
3568 if (mddev
->dev_sectors
== 0 ||
3569 mddev
->dev_sectors
> sectors
)
3570 mddev
->dev_sectors
= sectors
;
3574 return err
? err
: len
;
3577 static struct md_sysfs_entry md_size
=
3578 __ATTR(component_size
, S_IRUGO
|S_IWUSR
, size_show
, size_store
);
3583 * 'none' for arrays with no metadata (good luck...)
3584 * 'external' for arrays with externally managed metadata,
3585 * or N.M for internally known formats
3588 metadata_show(mddev_t
*mddev
, char *page
)
3590 if (mddev
->persistent
)
3591 return sprintf(page
, "%d.%d\n",
3592 mddev
->major_version
, mddev
->minor_version
);
3593 else if (mddev
->external
)
3594 return sprintf(page
, "external:%s\n", mddev
->metadata_type
);
3596 return sprintf(page
, "none\n");
3600 metadata_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3604 /* Changing the details of 'external' metadata is
3605 * always permitted. Otherwise there must be
3606 * no devices attached to the array.
3608 if (mddev
->external
&& strncmp(buf
, "external:", 9) == 0)
3610 else if (!list_empty(&mddev
->disks
))
3613 if (cmd_match(buf
, "none")) {
3614 mddev
->persistent
= 0;
3615 mddev
->external
= 0;
3616 mddev
->major_version
= 0;
3617 mddev
->minor_version
= 90;
3620 if (strncmp(buf
, "external:", 9) == 0) {
3621 size_t namelen
= len
-9;
3622 if (namelen
>= sizeof(mddev
->metadata_type
))
3623 namelen
= sizeof(mddev
->metadata_type
)-1;
3624 strncpy(mddev
->metadata_type
, buf
+9, namelen
);
3625 mddev
->metadata_type
[namelen
] = 0;
3626 if (namelen
&& mddev
->metadata_type
[namelen
-1] == '\n')
3627 mddev
->metadata_type
[--namelen
] = 0;
3628 mddev
->persistent
= 0;
3629 mddev
->external
= 1;
3630 mddev
->major_version
= 0;
3631 mddev
->minor_version
= 90;
3634 major
= simple_strtoul(buf
, &e
, 10);
3635 if (e
==buf
|| *e
!= '.')
3638 minor
= simple_strtoul(buf
, &e
, 10);
3639 if (e
==buf
|| (*e
&& *e
!= '\n') )
3641 if (major
>= ARRAY_SIZE(super_types
) || super_types
[major
].name
== NULL
)
3643 mddev
->major_version
= major
;
3644 mddev
->minor_version
= minor
;
3645 mddev
->persistent
= 1;
3646 mddev
->external
= 0;
3650 static struct md_sysfs_entry md_metadata
=
3651 __ATTR(metadata_version
, S_IRUGO
|S_IWUSR
, metadata_show
, metadata_store
);
3654 action_show(mddev_t
*mddev
, char *page
)
3656 char *type
= "idle";
3657 if (test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
))
3659 else if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
3660 (!mddev
->ro
&& test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))) {
3661 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
3663 else if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
3664 if (!test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
3666 else if (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
))
3670 } else if (test_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
))
3673 return sprintf(page
, "%s\n", type
);
3677 action_store(mddev_t
*mddev
, const char *page
, size_t len
)
3679 if (!mddev
->pers
|| !mddev
->pers
->sync_request
)
3682 if (cmd_match(page
, "frozen"))
3683 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
3685 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
3687 if (cmd_match(page
, "idle") || cmd_match(page
, "frozen")) {
3688 if (mddev
->sync_thread
) {
3689 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
3690 md_unregister_thread(mddev
->sync_thread
);
3691 mddev
->sync_thread
= NULL
;
3692 mddev
->recovery
= 0;
3694 } else if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
3695 test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))
3697 else if (cmd_match(page
, "resync"))
3698 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3699 else if (cmd_match(page
, "recover")) {
3700 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
3701 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3702 } else if (cmd_match(page
, "reshape")) {
3704 if (mddev
->pers
->start_reshape
== NULL
)
3706 err
= mddev
->pers
->start_reshape(mddev
);
3709 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
3711 if (cmd_match(page
, "check"))
3712 set_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
3713 else if (!cmd_match(page
, "repair"))
3715 set_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
3716 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
3718 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3719 md_wakeup_thread(mddev
->thread
);
3720 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
3725 mismatch_cnt_show(mddev_t
*mddev
, char *page
)
3727 return sprintf(page
, "%llu\n",
3728 (unsigned long long) mddev
->resync_mismatches
);
3731 static struct md_sysfs_entry md_scan_mode
=
3732 __ATTR(sync_action
, S_IRUGO
|S_IWUSR
, action_show
, action_store
);
3735 static struct md_sysfs_entry md_mismatches
= __ATTR_RO(mismatch_cnt
);
3738 sync_min_show(mddev_t
*mddev
, char *page
)
3740 return sprintf(page
, "%d (%s)\n", speed_min(mddev
),
3741 mddev
->sync_speed_min
? "local": "system");
3745 sync_min_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3749 if (strncmp(buf
, "system", 6)==0) {
3750 mddev
->sync_speed_min
= 0;
3753 min
= simple_strtoul(buf
, &e
, 10);
3754 if (buf
== e
|| (*e
&& *e
!= '\n') || min
<= 0)
3756 mddev
->sync_speed_min
= min
;
3760 static struct md_sysfs_entry md_sync_min
=
3761 __ATTR(sync_speed_min
, S_IRUGO
|S_IWUSR
, sync_min_show
, sync_min_store
);
3764 sync_max_show(mddev_t
*mddev
, char *page
)
3766 return sprintf(page
, "%d (%s)\n", speed_max(mddev
),
3767 mddev
->sync_speed_max
? "local": "system");
3771 sync_max_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3775 if (strncmp(buf
, "system", 6)==0) {
3776 mddev
->sync_speed_max
= 0;
3779 max
= simple_strtoul(buf
, &e
, 10);
3780 if (buf
== e
|| (*e
&& *e
!= '\n') || max
<= 0)
3782 mddev
->sync_speed_max
= max
;
3786 static struct md_sysfs_entry md_sync_max
=
3787 __ATTR(sync_speed_max
, S_IRUGO
|S_IWUSR
, sync_max_show
, sync_max_store
);
3790 degraded_show(mddev_t
*mddev
, char *page
)
3792 return sprintf(page
, "%d\n", mddev
->degraded
);
3794 static struct md_sysfs_entry md_degraded
= __ATTR_RO(degraded
);
3797 sync_force_parallel_show(mddev_t
*mddev
, char *page
)
3799 return sprintf(page
, "%d\n", mddev
->parallel_resync
);
3803 sync_force_parallel_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3807 if (strict_strtol(buf
, 10, &n
))
3810 if (n
!= 0 && n
!= 1)
3813 mddev
->parallel_resync
= n
;
3815 if (mddev
->sync_thread
)
3816 wake_up(&resync_wait
);
3821 /* force parallel resync, even with shared block devices */
3822 static struct md_sysfs_entry md_sync_force_parallel
=
3823 __ATTR(sync_force_parallel
, S_IRUGO
|S_IWUSR
,
3824 sync_force_parallel_show
, sync_force_parallel_store
);
3827 sync_speed_show(mddev_t
*mddev
, char *page
)
3829 unsigned long resync
, dt
, db
;
3830 if (mddev
->curr_resync
== 0)
3831 return sprintf(page
, "none\n");
3832 resync
= mddev
->curr_mark_cnt
- atomic_read(&mddev
->recovery_active
);
3833 dt
= (jiffies
- mddev
->resync_mark
) / HZ
;
3835 db
= resync
- mddev
->resync_mark_cnt
;
3836 return sprintf(page
, "%lu\n", db
/dt
/2); /* K/sec */
3839 static struct md_sysfs_entry md_sync_speed
= __ATTR_RO(sync_speed
);
3842 sync_completed_show(mddev_t
*mddev
, char *page
)
3844 unsigned long max_sectors
, resync
;
3846 if (!test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
3847 return sprintf(page
, "none\n");
3849 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
3850 max_sectors
= mddev
->resync_max_sectors
;
3852 max_sectors
= mddev
->dev_sectors
;
3854 resync
= mddev
->curr_resync_completed
;
3855 return sprintf(page
, "%lu / %lu\n", resync
, max_sectors
);
3858 static struct md_sysfs_entry md_sync_completed
= __ATTR_RO(sync_completed
);
3861 min_sync_show(mddev_t
*mddev
, char *page
)
3863 return sprintf(page
, "%llu\n",
3864 (unsigned long long)mddev
->resync_min
);
3867 min_sync_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3869 unsigned long long min
;
3870 if (strict_strtoull(buf
, 10, &min
))
3872 if (min
> mddev
->resync_max
)
3874 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
3877 /* Must be a multiple of chunk_size */
3878 if (mddev
->chunk_sectors
) {
3879 sector_t temp
= min
;
3880 if (sector_div(temp
, mddev
->chunk_sectors
))
3883 mddev
->resync_min
= min
;
3888 static struct md_sysfs_entry md_min_sync
=
3889 __ATTR(sync_min
, S_IRUGO
|S_IWUSR
, min_sync_show
, min_sync_store
);
3892 max_sync_show(mddev_t
*mddev
, char *page
)
3894 if (mddev
->resync_max
== MaxSector
)
3895 return sprintf(page
, "max\n");
3897 return sprintf(page
, "%llu\n",
3898 (unsigned long long)mddev
->resync_max
);
3901 max_sync_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3903 if (strncmp(buf
, "max", 3) == 0)
3904 mddev
->resync_max
= MaxSector
;
3906 unsigned long long max
;
3907 if (strict_strtoull(buf
, 10, &max
))
3909 if (max
< mddev
->resync_min
)
3911 if (max
< mddev
->resync_max
&&
3913 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
3916 /* Must be a multiple of chunk_size */
3917 if (mddev
->chunk_sectors
) {
3918 sector_t temp
= max
;
3919 if (sector_div(temp
, mddev
->chunk_sectors
))
3922 mddev
->resync_max
= max
;
3924 wake_up(&mddev
->recovery_wait
);
3928 static struct md_sysfs_entry md_max_sync
=
3929 __ATTR(sync_max
, S_IRUGO
|S_IWUSR
, max_sync_show
, max_sync_store
);
3932 suspend_lo_show(mddev_t
*mddev
, char *page
)
3934 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->suspend_lo
);
3938 suspend_lo_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3941 unsigned long long new = simple_strtoull(buf
, &e
, 10);
3943 if (mddev
->pers
== NULL
||
3944 mddev
->pers
->quiesce
== NULL
)
3946 if (buf
== e
|| (*e
&& *e
!= '\n'))
3948 if (new >= mddev
->suspend_hi
||
3949 (new > mddev
->suspend_lo
&& new < mddev
->suspend_hi
)) {
3950 mddev
->suspend_lo
= new;
3951 mddev
->pers
->quiesce(mddev
, 2);
3956 static struct md_sysfs_entry md_suspend_lo
=
3957 __ATTR(suspend_lo
, S_IRUGO
|S_IWUSR
, suspend_lo_show
, suspend_lo_store
);
3961 suspend_hi_show(mddev_t
*mddev
, char *page
)
3963 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->suspend_hi
);
3967 suspend_hi_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3970 unsigned long long new = simple_strtoull(buf
, &e
, 10);
3972 if (mddev
->pers
== NULL
||
3973 mddev
->pers
->quiesce
== NULL
)
3975 if (buf
== e
|| (*e
&& *e
!= '\n'))
3977 if ((new <= mddev
->suspend_lo
&& mddev
->suspend_lo
>= mddev
->suspend_hi
) ||
3978 (new > mddev
->suspend_lo
&& new > mddev
->suspend_hi
)) {
3979 mddev
->suspend_hi
= new;
3980 mddev
->pers
->quiesce(mddev
, 1);
3981 mddev
->pers
->quiesce(mddev
, 0);
3986 static struct md_sysfs_entry md_suspend_hi
=
3987 __ATTR(suspend_hi
, S_IRUGO
|S_IWUSR
, suspend_hi_show
, suspend_hi_store
);
3990 reshape_position_show(mddev_t
*mddev
, char *page
)
3992 if (mddev
->reshape_position
!= MaxSector
)
3993 return sprintf(page
, "%llu\n",
3994 (unsigned long long)mddev
->reshape_position
);
3995 strcpy(page
, "none\n");
4000 reshape_position_store(mddev_t
*mddev
, const char *buf
, size_t len
)
4003 unsigned long long new = simple_strtoull(buf
, &e
, 10);
4006 if (buf
== e
|| (*e
&& *e
!= '\n'))
4008 mddev
->reshape_position
= new;
4009 mddev
->delta_disks
= 0;
4010 mddev
->new_level
= mddev
->level
;
4011 mddev
->new_layout
= mddev
->layout
;
4012 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
4016 static struct md_sysfs_entry md_reshape_position
=
4017 __ATTR(reshape_position
, S_IRUGO
|S_IWUSR
, reshape_position_show
,
4018 reshape_position_store
);
4021 array_size_show(mddev_t
*mddev
, char *page
)
4023 if (mddev
->external_size
)
4024 return sprintf(page
, "%llu\n",
4025 (unsigned long long)mddev
->array_sectors
/2);
4027 return sprintf(page
, "default\n");
4031 array_size_store(mddev_t
*mddev
, const char *buf
, size_t len
)
4035 if (strncmp(buf
, "default", 7) == 0) {
4037 sectors
= mddev
->pers
->size(mddev
, 0, 0);
4039 sectors
= mddev
->array_sectors
;
4041 mddev
->external_size
= 0;
4043 if (strict_blocks_to_sectors(buf
, §ors
) < 0)
4045 if (mddev
->pers
&& mddev
->pers
->size(mddev
, 0, 0) < sectors
)
4048 mddev
->external_size
= 1;
4051 mddev
->array_sectors
= sectors
;
4052 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
4054 revalidate_disk(mddev
->gendisk
);
4059 static struct md_sysfs_entry md_array_size
=
4060 __ATTR(array_size
, S_IRUGO
|S_IWUSR
, array_size_show
,
4063 static struct attribute
*md_default_attrs
[] = {
4066 &md_raid_disks
.attr
,
4067 &md_chunk_size
.attr
,
4069 &md_resync_start
.attr
,
4071 &md_new_device
.attr
,
4072 &md_safe_delay
.attr
,
4073 &md_array_state
.attr
,
4074 &md_reshape_position
.attr
,
4075 &md_array_size
.attr
,
4076 &max_corr_read_errors
.attr
,
4080 static struct attribute
*md_redundancy_attrs
[] = {
4082 &md_mismatches
.attr
,
4085 &md_sync_speed
.attr
,
4086 &md_sync_force_parallel
.attr
,
4087 &md_sync_completed
.attr
,
4090 &md_suspend_lo
.attr
,
4091 &md_suspend_hi
.attr
,
4096 static struct attribute_group md_redundancy_group
= {
4098 .attrs
= md_redundancy_attrs
,
4103 md_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
4105 struct md_sysfs_entry
*entry
= container_of(attr
, struct md_sysfs_entry
, attr
);
4106 mddev_t
*mddev
= container_of(kobj
, struct mddev_s
, kobj
);
4111 rv
= mddev_lock(mddev
);
4113 rv
= entry
->show(mddev
, page
);
4114 mddev_unlock(mddev
);
4120 md_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
4121 const char *page
, size_t length
)
4123 struct md_sysfs_entry
*entry
= container_of(attr
, struct md_sysfs_entry
, attr
);
4124 mddev_t
*mddev
= container_of(kobj
, struct mddev_s
, kobj
);
4129 if (!capable(CAP_SYS_ADMIN
))
4131 rv
= mddev_lock(mddev
);
4132 if (mddev
->hold_active
== UNTIL_IOCTL
)
4133 mddev
->hold_active
= 0;
4135 rv
= entry
->store(mddev
, page
, length
);
4136 mddev_unlock(mddev
);
4141 static void md_free(struct kobject
*ko
)
4143 mddev_t
*mddev
= container_of(ko
, mddev_t
, kobj
);
4145 if (mddev
->sysfs_state
)
4146 sysfs_put(mddev
->sysfs_state
);
4148 if (mddev
->gendisk
) {
4149 del_gendisk(mddev
->gendisk
);
4150 put_disk(mddev
->gendisk
);
4153 blk_cleanup_queue(mddev
->queue
);
4158 static const struct sysfs_ops md_sysfs_ops
= {
4159 .show
= md_attr_show
,
4160 .store
= md_attr_store
,
4162 static struct kobj_type md_ktype
= {
4164 .sysfs_ops
= &md_sysfs_ops
,
4165 .default_attrs
= md_default_attrs
,
4170 static void mddev_delayed_delete(struct work_struct
*ws
)
4172 mddev_t
*mddev
= container_of(ws
, mddev_t
, del_work
);
4174 sysfs_remove_group(&mddev
->kobj
, &md_bitmap_group
);
4175 kobject_del(&mddev
->kobj
);
4176 kobject_put(&mddev
->kobj
);
4179 static int md_alloc(dev_t dev
, char *name
)
4181 static DEFINE_MUTEX(disks_mutex
);
4182 mddev_t
*mddev
= mddev_find(dev
);
4183 struct gendisk
*disk
;
4192 partitioned
= (MAJOR(mddev
->unit
) != MD_MAJOR
);
4193 shift
= partitioned
? MdpMinorShift
: 0;
4194 unit
= MINOR(mddev
->unit
) >> shift
;
4196 /* wait for any previous instance if this device
4197 * to be completed removed (mddev_delayed_delete).
4199 flush_scheduled_work();
4201 mutex_lock(&disks_mutex
);
4207 /* Need to ensure that 'name' is not a duplicate.
4210 spin_lock(&all_mddevs_lock
);
4212 list_for_each_entry(mddev2
, &all_mddevs
, all_mddevs
)
4213 if (mddev2
->gendisk
&&
4214 strcmp(mddev2
->gendisk
->disk_name
, name
) == 0) {
4215 spin_unlock(&all_mddevs_lock
);
4218 spin_unlock(&all_mddevs_lock
);
4222 mddev
->queue
= blk_alloc_queue(GFP_KERNEL
);
4225 mddev
->queue
->queuedata
= mddev
;
4227 /* Can be unlocked because the queue is new: no concurrency */
4228 queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER
, mddev
->queue
);
4230 blk_queue_make_request(mddev
->queue
, md_make_request
);
4232 disk
= alloc_disk(1 << shift
);
4234 blk_cleanup_queue(mddev
->queue
);
4235 mddev
->queue
= NULL
;
4238 disk
->major
= MAJOR(mddev
->unit
);
4239 disk
->first_minor
= unit
<< shift
;
4241 strcpy(disk
->disk_name
, name
);
4242 else if (partitioned
)
4243 sprintf(disk
->disk_name
, "md_d%d", unit
);
4245 sprintf(disk
->disk_name
, "md%d", unit
);
4246 disk
->fops
= &md_fops
;
4247 disk
->private_data
= mddev
;
4248 disk
->queue
= mddev
->queue
;
4249 /* Allow extended partitions. This makes the
4250 * 'mdp' device redundant, but we can't really
4253 disk
->flags
|= GENHD_FL_EXT_DEVT
;
4255 mddev
->gendisk
= disk
;
4256 error
= kobject_init_and_add(&mddev
->kobj
, &md_ktype
,
4257 &disk_to_dev(disk
)->kobj
, "%s", "md");
4259 /* This isn't possible, but as kobject_init_and_add is marked
4260 * __must_check, we must do something with the result
4262 printk(KERN_WARNING
"md: cannot register %s/md - name in use\n",
4266 if (mddev
->kobj
.sd
&&
4267 sysfs_create_group(&mddev
->kobj
, &md_bitmap_group
))
4268 printk(KERN_DEBUG
"pointless warning\n");
4270 mutex_unlock(&disks_mutex
);
4271 if (!error
&& mddev
->kobj
.sd
) {
4272 kobject_uevent(&mddev
->kobj
, KOBJ_ADD
);
4273 mddev
->sysfs_state
= sysfs_get_dirent_safe(mddev
->kobj
.sd
, "array_state");
4279 static struct kobject
*md_probe(dev_t dev
, int *part
, void *data
)
4281 md_alloc(dev
, NULL
);
4285 static int add_named_array(const char *val
, struct kernel_param
*kp
)
4287 /* val must be "md_*" where * is not all digits.
4288 * We allocate an array with a large free minor number, and
4289 * set the name to val. val must not already be an active name.
4291 int len
= strlen(val
);
4292 char buf
[DISK_NAME_LEN
];
4294 while (len
&& val
[len
-1] == '\n')
4296 if (len
>= DISK_NAME_LEN
)
4298 strlcpy(buf
, val
, len
+1);
4299 if (strncmp(buf
, "md_", 3) != 0)
4301 return md_alloc(0, buf
);
4304 static void md_safemode_timeout(unsigned long data
)
4306 mddev_t
*mddev
= (mddev_t
*) data
;
4308 if (!atomic_read(&mddev
->writes_pending
)) {
4309 mddev
->safemode
= 1;
4310 if (mddev
->external
)
4311 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4313 md_wakeup_thread(mddev
->thread
);
4316 static int start_dirty_degraded
;
4318 int md_run(mddev_t
*mddev
)
4322 struct mdk_personality
*pers
;
4324 if (list_empty(&mddev
->disks
))
4325 /* cannot run an array with no devices.. */
4330 /* Cannot run until previous stop completes properly */
4331 if (mddev
->sysfs_active
)
4335 * Analyze all RAID superblock(s)
4337 if (!mddev
->raid_disks
) {
4338 if (!mddev
->persistent
)
4343 if (mddev
->level
!= LEVEL_NONE
)
4344 request_module("md-level-%d", mddev
->level
);
4345 else if (mddev
->clevel
[0])
4346 request_module("md-%s", mddev
->clevel
);
4349 * Drop all container device buffers, from now on
4350 * the only valid external interface is through the md
4353 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
4354 if (test_bit(Faulty
, &rdev
->flags
))
4356 sync_blockdev(rdev
->bdev
);
4357 invalidate_bdev(rdev
->bdev
);
4359 /* perform some consistency tests on the device.
4360 * We don't want the data to overlap the metadata,
4361 * Internal Bitmap issues have been handled elsewhere.
4363 if (rdev
->data_offset
< rdev
->sb_start
) {
4364 if (mddev
->dev_sectors
&&
4365 rdev
->data_offset
+ mddev
->dev_sectors
4367 printk("md: %s: data overlaps metadata\n",
4372 if (rdev
->sb_start
+ rdev
->sb_size
/512
4373 > rdev
->data_offset
) {
4374 printk("md: %s: metadata overlaps data\n",
4379 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
4382 spin_lock(&pers_lock
);
4383 pers
= find_pers(mddev
->level
, mddev
->clevel
);
4384 if (!pers
|| !try_module_get(pers
->owner
)) {
4385 spin_unlock(&pers_lock
);
4386 if (mddev
->level
!= LEVEL_NONE
)
4387 printk(KERN_WARNING
"md: personality for level %d is not loaded!\n",
4390 printk(KERN_WARNING
"md: personality for level %s is not loaded!\n",
4395 spin_unlock(&pers_lock
);
4396 if (mddev
->level
!= pers
->level
) {
4397 mddev
->level
= pers
->level
;
4398 mddev
->new_level
= pers
->level
;
4400 strlcpy(mddev
->clevel
, pers
->name
, sizeof(mddev
->clevel
));
4402 if (mddev
->reshape_position
!= MaxSector
&&
4403 pers
->start_reshape
== NULL
) {
4404 /* This personality cannot handle reshaping... */
4406 module_put(pers
->owner
);
4410 if (pers
->sync_request
) {
4411 /* Warn if this is a potentially silly
4414 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
4418 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
4419 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
) {
4421 rdev
->bdev
->bd_contains
==
4422 rdev2
->bdev
->bd_contains
) {
4424 "%s: WARNING: %s appears to be"
4425 " on the same physical disk as"
4428 bdevname(rdev
->bdev
,b
),
4429 bdevname(rdev2
->bdev
,b2
));
4436 "True protection against single-disk"
4437 " failure might be compromised.\n");
4440 mddev
->recovery
= 0;
4441 /* may be over-ridden by personality */
4442 mddev
->resync_max_sectors
= mddev
->dev_sectors
;
4444 mddev
->ok_start_degraded
= start_dirty_degraded
;
4446 if (start_readonly
&& mddev
->ro
== 0)
4447 mddev
->ro
= 2; /* read-only, but switch on first write */
4449 err
= mddev
->pers
->run(mddev
);
4451 printk(KERN_ERR
"md: pers->run() failed ...\n");
4452 else if (mddev
->pers
->size(mddev
, 0, 0) < mddev
->array_sectors
) {
4453 WARN_ONCE(!mddev
->external_size
, "%s: default size too small,"
4454 " but 'external_size' not in effect?\n", __func__
);
4456 "md: invalid array_size %llu > default size %llu\n",
4457 (unsigned long long)mddev
->array_sectors
/ 2,
4458 (unsigned long long)mddev
->pers
->size(mddev
, 0, 0) / 2);
4460 mddev
->pers
->stop(mddev
);
4462 if (err
== 0 && mddev
->pers
->sync_request
) {
4463 err
= bitmap_create(mddev
);
4465 printk(KERN_ERR
"%s: failed to create bitmap (%d)\n",
4466 mdname(mddev
), err
);
4467 mddev
->pers
->stop(mddev
);
4471 module_put(mddev
->pers
->owner
);
4473 bitmap_destroy(mddev
);
4476 if (mddev
->pers
->sync_request
) {
4477 if (mddev
->kobj
.sd
&&
4478 sysfs_create_group(&mddev
->kobj
, &md_redundancy_group
))
4480 "md: cannot register extra attributes for %s\n",
4482 mddev
->sysfs_action
= sysfs_get_dirent_safe(mddev
->kobj
.sd
, "sync_action");
4483 } else if (mddev
->ro
== 2) /* auto-readonly not meaningful */
4486 atomic_set(&mddev
->writes_pending
,0);
4487 atomic_set(&mddev
->max_corr_read_errors
,
4488 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS
);
4489 mddev
->safemode
= 0;
4490 mddev
->safemode_timer
.function
= md_safemode_timeout
;
4491 mddev
->safemode_timer
.data
= (unsigned long) mddev
;
4492 mddev
->safemode_delay
= (200 * HZ
)/1000 +1; /* 200 msec delay */
4495 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
4496 if (rdev
->raid_disk
>= 0) {
4498 sprintf(nm
, "rd%d", rdev
->raid_disk
);
4499 if (sysfs_create_link(&mddev
->kobj
, &rdev
->kobj
, nm
))
4500 /* failure here is OK */;
4503 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4506 md_update_sb(mddev
, 0);
4508 md_wakeup_thread(mddev
->thread
);
4509 md_wakeup_thread(mddev
->sync_thread
); /* possibly kick off a reshape */
4511 md_new_event(mddev
);
4512 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4513 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
4514 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
4517 EXPORT_SYMBOL_GPL(md_run
);
4519 static int do_md_run(mddev_t
*mddev
)
4523 err
= md_run(mddev
);
4526 err
= bitmap_load(mddev
);
4528 bitmap_destroy(mddev
);
4531 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
4532 revalidate_disk(mddev
->gendisk
);
4533 kobject_uevent(&disk_to_dev(mddev
->gendisk
)->kobj
, KOBJ_CHANGE
);
4538 static int restart_array(mddev_t
*mddev
)
4540 struct gendisk
*disk
= mddev
->gendisk
;
4542 /* Complain if it has no devices */
4543 if (list_empty(&mddev
->disks
))
4549 mddev
->safemode
= 0;
4551 set_disk_ro(disk
, 0);
4552 printk(KERN_INFO
"md: %s switched to read-write mode.\n",
4554 /* Kick recovery or resync if necessary */
4555 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4556 md_wakeup_thread(mddev
->thread
);
4557 md_wakeup_thread(mddev
->sync_thread
);
4558 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4562 /* similar to deny_write_access, but accounts for our holding a reference
4563 * to the file ourselves */
4564 static int deny_bitmap_write_access(struct file
* file
)
4566 struct inode
*inode
= file
->f_mapping
->host
;
4568 spin_lock(&inode
->i_lock
);
4569 if (atomic_read(&inode
->i_writecount
) > 1) {
4570 spin_unlock(&inode
->i_lock
);
4573 atomic_set(&inode
->i_writecount
, -1);
4574 spin_unlock(&inode
->i_lock
);
4579 void restore_bitmap_write_access(struct file
*file
)
4581 struct inode
*inode
= file
->f_mapping
->host
;
4583 spin_lock(&inode
->i_lock
);
4584 atomic_set(&inode
->i_writecount
, 1);
4585 spin_unlock(&inode
->i_lock
);
4588 static void md_clean(mddev_t
*mddev
)
4590 mddev
->array_sectors
= 0;
4591 mddev
->external_size
= 0;
4592 mddev
->dev_sectors
= 0;
4593 mddev
->raid_disks
= 0;
4594 mddev
->recovery_cp
= 0;
4595 mddev
->resync_min
= 0;
4596 mddev
->resync_max
= MaxSector
;
4597 mddev
->reshape_position
= MaxSector
;
4598 mddev
->external
= 0;
4599 mddev
->persistent
= 0;
4600 mddev
->level
= LEVEL_NONE
;
4601 mddev
->clevel
[0] = 0;
4604 mddev
->metadata_type
[0] = 0;
4605 mddev
->chunk_sectors
= 0;
4606 mddev
->ctime
= mddev
->utime
= 0;
4608 mddev
->max_disks
= 0;
4610 mddev
->can_decrease_events
= 0;
4611 mddev
->delta_disks
= 0;
4612 mddev
->new_level
= LEVEL_NONE
;
4613 mddev
->new_layout
= 0;
4614 mddev
->new_chunk_sectors
= 0;
4615 mddev
->curr_resync
= 0;
4616 mddev
->resync_mismatches
= 0;
4617 mddev
->suspend_lo
= mddev
->suspend_hi
= 0;
4618 mddev
->sync_speed_min
= mddev
->sync_speed_max
= 0;
4619 mddev
->recovery
= 0;
4621 mddev
->degraded
= 0;
4622 mddev
->safemode
= 0;
4623 mddev
->bitmap_info
.offset
= 0;
4624 mddev
->bitmap_info
.default_offset
= 0;
4625 mddev
->bitmap_info
.chunksize
= 0;
4626 mddev
->bitmap_info
.daemon_sleep
= 0;
4627 mddev
->bitmap_info
.max_write_behind
= 0;
4631 void md_stop_writes(mddev_t
*mddev
)
4633 if (mddev
->sync_thread
) {
4634 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4635 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
4636 md_unregister_thread(mddev
->sync_thread
);
4637 mddev
->sync_thread
= NULL
;
4640 del_timer_sync(&mddev
->safemode_timer
);
4642 bitmap_flush(mddev
);
4643 md_super_wait(mddev
);
4645 if (!mddev
->in_sync
|| mddev
->flags
) {
4646 /* mark array as shutdown cleanly */
4648 md_update_sb(mddev
, 1);
4651 EXPORT_SYMBOL_GPL(md_stop_writes
);
4653 void md_stop(mddev_t
*mddev
)
4655 mddev
->pers
->stop(mddev
);
4656 if (mddev
->pers
->sync_request
&& mddev
->to_remove
== NULL
)
4657 mddev
->to_remove
= &md_redundancy_group
;
4658 module_put(mddev
->pers
->owner
);
4660 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4662 EXPORT_SYMBOL_GPL(md_stop
);
4664 static int md_set_readonly(mddev_t
*mddev
, int is_open
)
4667 mutex_lock(&mddev
->open_mutex
);
4668 if (atomic_read(&mddev
->openers
) > is_open
) {
4669 printk("md: %s still in use.\n",mdname(mddev
));
4674 md_stop_writes(mddev
);
4680 set_disk_ro(mddev
->gendisk
, 1);
4681 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4682 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4686 mutex_unlock(&mddev
->open_mutex
);
4691 * 0 - completely stop and dis-assemble array
4692 * 2 - stop but do not disassemble array
4694 static int do_md_stop(mddev_t
* mddev
, int mode
, int is_open
)
4696 struct gendisk
*disk
= mddev
->gendisk
;
4699 mutex_lock(&mddev
->open_mutex
);
4700 if (atomic_read(&mddev
->openers
) > is_open
||
4701 mddev
->sysfs_active
) {
4702 printk("md: %s still in use.\n",mdname(mddev
));
4703 mutex_unlock(&mddev
->open_mutex
);
4709 set_disk_ro(disk
, 0);
4711 md_stop_writes(mddev
);
4713 mddev
->queue
->merge_bvec_fn
= NULL
;
4714 mddev
->queue
->unplug_fn
= NULL
;
4715 mddev
->queue
->backing_dev_info
.congested_fn
= NULL
;
4717 /* tell userspace to handle 'inactive' */
4718 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4720 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
4721 if (rdev
->raid_disk
>= 0) {
4723 sprintf(nm
, "rd%d", rdev
->raid_disk
);
4724 sysfs_remove_link(&mddev
->kobj
, nm
);
4727 set_capacity(disk
, 0);
4728 mutex_unlock(&mddev
->open_mutex
);
4729 revalidate_disk(disk
);
4734 mutex_unlock(&mddev
->open_mutex
);
4736 * Free resources if final stop
4739 printk(KERN_INFO
"md: %s stopped.\n", mdname(mddev
));
4741 bitmap_destroy(mddev
);
4742 if (mddev
->bitmap_info
.file
) {
4743 restore_bitmap_write_access(mddev
->bitmap_info
.file
);
4744 fput(mddev
->bitmap_info
.file
);
4745 mddev
->bitmap_info
.file
= NULL
;
4747 mddev
->bitmap_info
.offset
= 0;
4749 export_array(mddev
);
4752 kobject_uevent(&disk_to_dev(mddev
->gendisk
)->kobj
, KOBJ_CHANGE
);
4753 if (mddev
->hold_active
== UNTIL_STOP
)
4754 mddev
->hold_active
= 0;
4756 blk_integrity_unregister(disk
);
4757 md_new_event(mddev
);
4758 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4763 static void autorun_array(mddev_t
*mddev
)
4768 if (list_empty(&mddev
->disks
))
4771 printk(KERN_INFO
"md: running: ");
4773 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
4774 char b
[BDEVNAME_SIZE
];
4775 printk("<%s>", bdevname(rdev
->bdev
,b
));
4779 err
= do_md_run(mddev
);
4781 printk(KERN_WARNING
"md: do_md_run() returned %d\n", err
);
4782 do_md_stop(mddev
, 0, 0);
4787 * lets try to run arrays based on all disks that have arrived
4788 * until now. (those are in pending_raid_disks)
4790 * the method: pick the first pending disk, collect all disks with
4791 * the same UUID, remove all from the pending list and put them into
4792 * the 'same_array' list. Then order this list based on superblock
4793 * update time (freshest comes first), kick out 'old' disks and
4794 * compare superblocks. If everything's fine then run it.
4796 * If "unit" is allocated, then bump its reference count
4798 static void autorun_devices(int part
)
4800 mdk_rdev_t
*rdev0
, *rdev
, *tmp
;
4802 char b
[BDEVNAME_SIZE
];
4804 printk(KERN_INFO
"md: autorun ...\n");
4805 while (!list_empty(&pending_raid_disks
)) {
4808 LIST_HEAD(candidates
);
4809 rdev0
= list_entry(pending_raid_disks
.next
,
4810 mdk_rdev_t
, same_set
);
4812 printk(KERN_INFO
"md: considering %s ...\n",
4813 bdevname(rdev0
->bdev
,b
));
4814 INIT_LIST_HEAD(&candidates
);
4815 rdev_for_each_list(rdev
, tmp
, &pending_raid_disks
)
4816 if (super_90_load(rdev
, rdev0
, 0) >= 0) {
4817 printk(KERN_INFO
"md: adding %s ...\n",
4818 bdevname(rdev
->bdev
,b
));
4819 list_move(&rdev
->same_set
, &candidates
);
4822 * now we have a set of devices, with all of them having
4823 * mostly sane superblocks. It's time to allocate the
4827 dev
= MKDEV(mdp_major
,
4828 rdev0
->preferred_minor
<< MdpMinorShift
);
4829 unit
= MINOR(dev
) >> MdpMinorShift
;
4831 dev
= MKDEV(MD_MAJOR
, rdev0
->preferred_minor
);
4834 if (rdev0
->preferred_minor
!= unit
) {
4835 printk(KERN_INFO
"md: unit number in %s is bad: %d\n",
4836 bdevname(rdev0
->bdev
, b
), rdev0
->preferred_minor
);
4840 md_probe(dev
, NULL
, NULL
);
4841 mddev
= mddev_find(dev
);
4842 if (!mddev
|| !mddev
->gendisk
) {
4846 "md: cannot allocate memory for md drive.\n");
4849 if (mddev_lock(mddev
))
4850 printk(KERN_WARNING
"md: %s locked, cannot run\n",
4852 else if (mddev
->raid_disks
|| mddev
->major_version
4853 || !list_empty(&mddev
->disks
)) {
4855 "md: %s already running, cannot run %s\n",
4856 mdname(mddev
), bdevname(rdev0
->bdev
,b
));
4857 mddev_unlock(mddev
);
4859 printk(KERN_INFO
"md: created %s\n", mdname(mddev
));
4860 mddev
->persistent
= 1;
4861 rdev_for_each_list(rdev
, tmp
, &candidates
) {
4862 list_del_init(&rdev
->same_set
);
4863 if (bind_rdev_to_array(rdev
, mddev
))
4866 autorun_array(mddev
);
4867 mddev_unlock(mddev
);
4869 /* on success, candidates will be empty, on error
4872 rdev_for_each_list(rdev
, tmp
, &candidates
) {
4873 list_del_init(&rdev
->same_set
);
4878 printk(KERN_INFO
"md: ... autorun DONE.\n");
4880 #endif /* !MODULE */
4882 static int get_version(void __user
* arg
)
4886 ver
.major
= MD_MAJOR_VERSION
;
4887 ver
.minor
= MD_MINOR_VERSION
;
4888 ver
.patchlevel
= MD_PATCHLEVEL_VERSION
;
4890 if (copy_to_user(arg
, &ver
, sizeof(ver
)))
4896 static int get_array_info(mddev_t
* mddev
, void __user
* arg
)
4898 mdu_array_info_t info
;
4899 int nr
,working
,insync
,failed
,spare
;
4902 nr
=working
=insync
=failed
=spare
=0;
4903 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
4905 if (test_bit(Faulty
, &rdev
->flags
))
4909 if (test_bit(In_sync
, &rdev
->flags
))
4916 info
.major_version
= mddev
->major_version
;
4917 info
.minor_version
= mddev
->minor_version
;
4918 info
.patch_version
= MD_PATCHLEVEL_VERSION
;
4919 info
.ctime
= mddev
->ctime
;
4920 info
.level
= mddev
->level
;
4921 info
.size
= mddev
->dev_sectors
/ 2;
4922 if (info
.size
!= mddev
->dev_sectors
/ 2) /* overflow */
4925 info
.raid_disks
= mddev
->raid_disks
;
4926 info
.md_minor
= mddev
->md_minor
;
4927 info
.not_persistent
= !mddev
->persistent
;
4929 info
.utime
= mddev
->utime
;
4932 info
.state
= (1<<MD_SB_CLEAN
);
4933 if (mddev
->bitmap
&& mddev
->bitmap_info
.offset
)
4934 info
.state
= (1<<MD_SB_BITMAP_PRESENT
);
4935 info
.active_disks
= insync
;
4936 info
.working_disks
= working
;
4937 info
.failed_disks
= failed
;
4938 info
.spare_disks
= spare
;
4940 info
.layout
= mddev
->layout
;
4941 info
.chunk_size
= mddev
->chunk_sectors
<< 9;
4943 if (copy_to_user(arg
, &info
, sizeof(info
)))
4949 static int get_bitmap_file(mddev_t
* mddev
, void __user
* arg
)
4951 mdu_bitmap_file_t
*file
= NULL
; /* too big for stack allocation */
4952 char *ptr
, *buf
= NULL
;
4955 if (md_allow_write(mddev
))
4956 file
= kmalloc(sizeof(*file
), GFP_NOIO
);
4958 file
= kmalloc(sizeof(*file
), GFP_KERNEL
);
4963 /* bitmap disabled, zero the first byte and copy out */
4964 if (!mddev
->bitmap
|| !mddev
->bitmap
->file
) {
4965 file
->pathname
[0] = '\0';
4969 buf
= kmalloc(sizeof(file
->pathname
), GFP_KERNEL
);
4973 ptr
= d_path(&mddev
->bitmap
->file
->f_path
, buf
, sizeof(file
->pathname
));
4977 strcpy(file
->pathname
, ptr
);
4981 if (copy_to_user(arg
, file
, sizeof(*file
)))
4989 static int get_disk_info(mddev_t
* mddev
, void __user
* arg
)
4991 mdu_disk_info_t info
;
4994 if (copy_from_user(&info
, arg
, sizeof(info
)))
4997 rdev
= find_rdev_nr(mddev
, info
.number
);
4999 info
.major
= MAJOR(rdev
->bdev
->bd_dev
);
5000 info
.minor
= MINOR(rdev
->bdev
->bd_dev
);
5001 info
.raid_disk
= rdev
->raid_disk
;
5003 if (test_bit(Faulty
, &rdev
->flags
))
5004 info
.state
|= (1<<MD_DISK_FAULTY
);
5005 else if (test_bit(In_sync
, &rdev
->flags
)) {
5006 info
.state
|= (1<<MD_DISK_ACTIVE
);
5007 info
.state
|= (1<<MD_DISK_SYNC
);
5009 if (test_bit(WriteMostly
, &rdev
->flags
))
5010 info
.state
|= (1<<MD_DISK_WRITEMOSTLY
);
5012 info
.major
= info
.minor
= 0;
5013 info
.raid_disk
= -1;
5014 info
.state
= (1<<MD_DISK_REMOVED
);
5017 if (copy_to_user(arg
, &info
, sizeof(info
)))
5023 static int add_new_disk(mddev_t
* mddev
, mdu_disk_info_t
*info
)
5025 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
5027 dev_t dev
= MKDEV(info
->major
,info
->minor
);
5029 if (info
->major
!= MAJOR(dev
) || info
->minor
!= MINOR(dev
))
5032 if (!mddev
->raid_disks
) {
5034 /* expecting a device which has a superblock */
5035 rdev
= md_import_device(dev
, mddev
->major_version
, mddev
->minor_version
);
5038 "md: md_import_device returned %ld\n",
5040 return PTR_ERR(rdev
);
5042 if (!list_empty(&mddev
->disks
)) {
5043 mdk_rdev_t
*rdev0
= list_entry(mddev
->disks
.next
,
5044 mdk_rdev_t
, same_set
);
5045 err
= super_types
[mddev
->major_version
]
5046 .load_super(rdev
, rdev0
, mddev
->minor_version
);
5049 "md: %s has different UUID to %s\n",
5050 bdevname(rdev
->bdev
,b
),
5051 bdevname(rdev0
->bdev
,b2
));
5056 err
= bind_rdev_to_array(rdev
, mddev
);
5063 * add_new_disk can be used once the array is assembled
5064 * to add "hot spares". They must already have a superblock
5069 if (!mddev
->pers
->hot_add_disk
) {
5071 "%s: personality does not support diskops!\n",
5075 if (mddev
->persistent
)
5076 rdev
= md_import_device(dev
, mddev
->major_version
,
5077 mddev
->minor_version
);
5079 rdev
= md_import_device(dev
, -1, -1);
5082 "md: md_import_device returned %ld\n",
5084 return PTR_ERR(rdev
);
5086 /* set save_raid_disk if appropriate */
5087 if (!mddev
->persistent
) {
5088 if (info
->state
& (1<<MD_DISK_SYNC
) &&
5089 info
->raid_disk
< mddev
->raid_disks
)
5090 rdev
->raid_disk
= info
->raid_disk
;
5092 rdev
->raid_disk
= -1;
5094 super_types
[mddev
->major_version
].
5095 validate_super(mddev
, rdev
);
5096 rdev
->saved_raid_disk
= rdev
->raid_disk
;
5098 clear_bit(In_sync
, &rdev
->flags
); /* just to be sure */
5099 if (info
->state
& (1<<MD_DISK_WRITEMOSTLY
))
5100 set_bit(WriteMostly
, &rdev
->flags
);
5102 clear_bit(WriteMostly
, &rdev
->flags
);
5104 rdev
->raid_disk
= -1;
5105 err
= bind_rdev_to_array(rdev
, mddev
);
5106 if (!err
&& !mddev
->pers
->hot_remove_disk
) {
5107 /* If there is hot_add_disk but no hot_remove_disk
5108 * then added disks for geometry changes,
5109 * and should be added immediately.
5111 super_types
[mddev
->major_version
].
5112 validate_super(mddev
, rdev
);
5113 err
= mddev
->pers
->hot_add_disk(mddev
, rdev
);
5115 unbind_rdev_from_array(rdev
);
5120 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
5122 md_update_sb(mddev
, 1);
5123 if (mddev
->degraded
)
5124 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
5125 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5126 md_wakeup_thread(mddev
->thread
);
5130 /* otherwise, add_new_disk is only allowed
5131 * for major_version==0 superblocks
5133 if (mddev
->major_version
!= 0) {
5134 printk(KERN_WARNING
"%s: ADD_NEW_DISK not supported\n",
5139 if (!(info
->state
& (1<<MD_DISK_FAULTY
))) {
5141 rdev
= md_import_device(dev
, -1, 0);
5144 "md: error, md_import_device() returned %ld\n",
5146 return PTR_ERR(rdev
);
5148 rdev
->desc_nr
= info
->number
;
5149 if (info
->raid_disk
< mddev
->raid_disks
)
5150 rdev
->raid_disk
= info
->raid_disk
;
5152 rdev
->raid_disk
= -1;
5154 if (rdev
->raid_disk
< mddev
->raid_disks
)
5155 if (info
->state
& (1<<MD_DISK_SYNC
))
5156 set_bit(In_sync
, &rdev
->flags
);
5158 if (info
->state
& (1<<MD_DISK_WRITEMOSTLY
))
5159 set_bit(WriteMostly
, &rdev
->flags
);
5161 if (!mddev
->persistent
) {
5162 printk(KERN_INFO
"md: nonpersistent superblock ...\n");
5163 rdev
->sb_start
= rdev
->bdev
->bd_inode
->i_size
/ 512;
5165 rdev
->sb_start
= calc_dev_sboffset(rdev
->bdev
);
5166 rdev
->sectors
= rdev
->sb_start
;
5168 err
= bind_rdev_to_array(rdev
, mddev
);
5178 static int hot_remove_disk(mddev_t
* mddev
, dev_t dev
)
5180 char b
[BDEVNAME_SIZE
];
5183 rdev
= find_rdev(mddev
, dev
);
5187 if (rdev
->raid_disk
>= 0)
5190 kick_rdev_from_array(rdev
);
5191 md_update_sb(mddev
, 1);
5192 md_new_event(mddev
);
5196 printk(KERN_WARNING
"md: cannot remove active disk %s from %s ...\n",
5197 bdevname(rdev
->bdev
,b
), mdname(mddev
));
5201 static int hot_add_disk(mddev_t
* mddev
, dev_t dev
)
5203 char b
[BDEVNAME_SIZE
];
5210 if (mddev
->major_version
!= 0) {
5211 printk(KERN_WARNING
"%s: HOT_ADD may only be used with"
5212 " version-0 superblocks.\n",
5216 if (!mddev
->pers
->hot_add_disk
) {
5218 "%s: personality does not support diskops!\n",
5223 rdev
= md_import_device(dev
, -1, 0);
5226 "md: error, md_import_device() returned %ld\n",
5231 if (mddev
->persistent
)
5232 rdev
->sb_start
= calc_dev_sboffset(rdev
->bdev
);
5234 rdev
->sb_start
= rdev
->bdev
->bd_inode
->i_size
/ 512;
5236 rdev
->sectors
= rdev
->sb_start
;
5238 if (test_bit(Faulty
, &rdev
->flags
)) {
5240 "md: can not hot-add faulty %s disk to %s!\n",
5241 bdevname(rdev
->bdev
,b
), mdname(mddev
));
5245 clear_bit(In_sync
, &rdev
->flags
);
5247 rdev
->saved_raid_disk
= -1;
5248 err
= bind_rdev_to_array(rdev
, mddev
);
5253 * The rest should better be atomic, we can have disk failures
5254 * noticed in interrupt contexts ...
5257 rdev
->raid_disk
= -1;
5259 md_update_sb(mddev
, 1);
5262 * Kick recovery, maybe this spare has to be added to the
5263 * array immediately.
5265 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5266 md_wakeup_thread(mddev
->thread
);
5267 md_new_event(mddev
);
5275 static int set_bitmap_file(mddev_t
*mddev
, int fd
)
5280 if (!mddev
->pers
->quiesce
)
5282 if (mddev
->recovery
|| mddev
->sync_thread
)
5284 /* we should be able to change the bitmap.. */
5290 return -EEXIST
; /* cannot add when bitmap is present */
5291 mddev
->bitmap_info
.file
= fget(fd
);
5293 if (mddev
->bitmap_info
.file
== NULL
) {
5294 printk(KERN_ERR
"%s: error: failed to get bitmap file\n",
5299 err
= deny_bitmap_write_access(mddev
->bitmap_info
.file
);
5301 printk(KERN_ERR
"%s: error: bitmap file is already in use\n",
5303 fput(mddev
->bitmap_info
.file
);
5304 mddev
->bitmap_info
.file
= NULL
;
5307 mddev
->bitmap_info
.offset
= 0; /* file overrides offset */
5308 } else if (mddev
->bitmap
== NULL
)
5309 return -ENOENT
; /* cannot remove what isn't there */
5312 mddev
->pers
->quiesce(mddev
, 1);
5314 err
= bitmap_create(mddev
);
5316 err
= bitmap_load(mddev
);
5318 if (fd
< 0 || err
) {
5319 bitmap_destroy(mddev
);
5320 fd
= -1; /* make sure to put the file */
5322 mddev
->pers
->quiesce(mddev
, 0);
5325 if (mddev
->bitmap_info
.file
) {
5326 restore_bitmap_write_access(mddev
->bitmap_info
.file
);
5327 fput(mddev
->bitmap_info
.file
);
5329 mddev
->bitmap_info
.file
= NULL
;
5336 * set_array_info is used two different ways
5337 * The original usage is when creating a new array.
5338 * In this usage, raid_disks is > 0 and it together with
5339 * level, size, not_persistent,layout,chunksize determine the
5340 * shape of the array.
5341 * This will always create an array with a type-0.90.0 superblock.
5342 * The newer usage is when assembling an array.
5343 * In this case raid_disks will be 0, and the major_version field is
5344 * use to determine which style super-blocks are to be found on the devices.
5345 * The minor and patch _version numbers are also kept incase the
5346 * super_block handler wishes to interpret them.
5348 static int set_array_info(mddev_t
* mddev
, mdu_array_info_t
*info
)
5351 if (info
->raid_disks
== 0) {
5352 /* just setting version number for superblock loading */
5353 if (info
->major_version
< 0 ||
5354 info
->major_version
>= ARRAY_SIZE(super_types
) ||
5355 super_types
[info
->major_version
].name
== NULL
) {
5356 /* maybe try to auto-load a module? */
5358 "md: superblock version %d not known\n",
5359 info
->major_version
);
5362 mddev
->major_version
= info
->major_version
;
5363 mddev
->minor_version
= info
->minor_version
;
5364 mddev
->patch_version
= info
->patch_version
;
5365 mddev
->persistent
= !info
->not_persistent
;
5366 /* ensure mddev_put doesn't delete this now that there
5367 * is some minimal configuration.
5369 mddev
->ctime
= get_seconds();
5372 mddev
->major_version
= MD_MAJOR_VERSION
;
5373 mddev
->minor_version
= MD_MINOR_VERSION
;
5374 mddev
->patch_version
= MD_PATCHLEVEL_VERSION
;
5375 mddev
->ctime
= get_seconds();
5377 mddev
->level
= info
->level
;
5378 mddev
->clevel
[0] = 0;
5379 mddev
->dev_sectors
= 2 * (sector_t
)info
->size
;
5380 mddev
->raid_disks
= info
->raid_disks
;
5381 /* don't set md_minor, it is determined by which /dev/md* was
5384 if (info
->state
& (1<<MD_SB_CLEAN
))
5385 mddev
->recovery_cp
= MaxSector
;
5387 mddev
->recovery_cp
= 0;
5388 mddev
->persistent
= ! info
->not_persistent
;
5389 mddev
->external
= 0;
5391 mddev
->layout
= info
->layout
;
5392 mddev
->chunk_sectors
= info
->chunk_size
>> 9;
5394 mddev
->max_disks
= MD_SB_DISKS
;
5396 if (mddev
->persistent
)
5398 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
5400 mddev
->bitmap_info
.default_offset
= MD_SB_BYTES
>> 9;
5401 mddev
->bitmap_info
.offset
= 0;
5403 mddev
->reshape_position
= MaxSector
;
5406 * Generate a 128 bit UUID
5408 get_random_bytes(mddev
->uuid
, 16);
5410 mddev
->new_level
= mddev
->level
;
5411 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
5412 mddev
->new_layout
= mddev
->layout
;
5413 mddev
->delta_disks
= 0;
5418 void md_set_array_sectors(mddev_t
*mddev
, sector_t array_sectors
)
5420 WARN(!mddev_is_locked(mddev
), "%s: unlocked mddev!\n", __func__
);
5422 if (mddev
->external_size
)
5425 mddev
->array_sectors
= array_sectors
;
5427 EXPORT_SYMBOL(md_set_array_sectors
);
5429 static int update_size(mddev_t
*mddev
, sector_t num_sectors
)
5433 int fit
= (num_sectors
== 0);
5435 if (mddev
->pers
->resize
== NULL
)
5437 /* The "num_sectors" is the number of sectors of each device that
5438 * is used. This can only make sense for arrays with redundancy.
5439 * linear and raid0 always use whatever space is available. We can only
5440 * consider changing this number if no resync or reconstruction is
5441 * happening, and if the new size is acceptable. It must fit before the
5442 * sb_start or, if that is <data_offset, it must fit before the size
5443 * of each device. If num_sectors is zero, we find the largest size
5447 if (mddev
->sync_thread
)
5450 /* Sorry, cannot grow a bitmap yet, just remove it,
5454 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
5455 sector_t avail
= rdev
->sectors
;
5457 if (fit
&& (num_sectors
== 0 || num_sectors
> avail
))
5458 num_sectors
= avail
;
5459 if (avail
< num_sectors
)
5462 rv
= mddev
->pers
->resize(mddev
, num_sectors
);
5464 revalidate_disk(mddev
->gendisk
);
5468 static int update_raid_disks(mddev_t
*mddev
, int raid_disks
)
5471 /* change the number of raid disks */
5472 if (mddev
->pers
->check_reshape
== NULL
)
5474 if (raid_disks
<= 0 ||
5475 (mddev
->max_disks
&& raid_disks
>= mddev
->max_disks
))
5477 if (mddev
->sync_thread
|| mddev
->reshape_position
!= MaxSector
)
5479 mddev
->delta_disks
= raid_disks
- mddev
->raid_disks
;
5481 rv
= mddev
->pers
->check_reshape(mddev
);
5487 * update_array_info is used to change the configuration of an
5489 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
5490 * fields in the info are checked against the array.
5491 * Any differences that cannot be handled will cause an error.
5492 * Normally, only one change can be managed at a time.
5494 static int update_array_info(mddev_t
*mddev
, mdu_array_info_t
*info
)
5500 /* calculate expected state,ignoring low bits */
5501 if (mddev
->bitmap
&& mddev
->bitmap_info
.offset
)
5502 state
|= (1 << MD_SB_BITMAP_PRESENT
);
5504 if (mddev
->major_version
!= info
->major_version
||
5505 mddev
->minor_version
!= info
->minor_version
||
5506 /* mddev->patch_version != info->patch_version || */
5507 mddev
->ctime
!= info
->ctime
||
5508 mddev
->level
!= info
->level
||
5509 /* mddev->layout != info->layout || */
5510 !mddev
->persistent
!= info
->not_persistent
||
5511 mddev
->chunk_sectors
!= info
->chunk_size
>> 9 ||
5512 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
5513 ((state
^info
->state
) & 0xfffffe00)
5516 /* Check there is only one change */
5517 if (info
->size
>= 0 && mddev
->dev_sectors
/ 2 != info
->size
)
5519 if (mddev
->raid_disks
!= info
->raid_disks
)
5521 if (mddev
->layout
!= info
->layout
)
5523 if ((state
^ info
->state
) & (1<<MD_SB_BITMAP_PRESENT
))
5530 if (mddev
->layout
!= info
->layout
) {
5532 * we don't need to do anything at the md level, the
5533 * personality will take care of it all.
5535 if (mddev
->pers
->check_reshape
== NULL
)
5538 mddev
->new_layout
= info
->layout
;
5539 rv
= mddev
->pers
->check_reshape(mddev
);
5541 mddev
->new_layout
= mddev
->layout
;
5545 if (info
->size
>= 0 && mddev
->dev_sectors
/ 2 != info
->size
)
5546 rv
= update_size(mddev
, (sector_t
)info
->size
* 2);
5548 if (mddev
->raid_disks
!= info
->raid_disks
)
5549 rv
= update_raid_disks(mddev
, info
->raid_disks
);
5551 if ((state
^ info
->state
) & (1<<MD_SB_BITMAP_PRESENT
)) {
5552 if (mddev
->pers
->quiesce
== NULL
)
5554 if (mddev
->recovery
|| mddev
->sync_thread
)
5556 if (info
->state
& (1<<MD_SB_BITMAP_PRESENT
)) {
5557 /* add the bitmap */
5560 if (mddev
->bitmap_info
.default_offset
== 0)
5562 mddev
->bitmap_info
.offset
=
5563 mddev
->bitmap_info
.default_offset
;
5564 mddev
->pers
->quiesce(mddev
, 1);
5565 rv
= bitmap_create(mddev
);
5567 rv
= bitmap_load(mddev
);
5569 bitmap_destroy(mddev
);
5570 mddev
->pers
->quiesce(mddev
, 0);
5572 /* remove the bitmap */
5575 if (mddev
->bitmap
->file
)
5577 mddev
->pers
->quiesce(mddev
, 1);
5578 bitmap_destroy(mddev
);
5579 mddev
->pers
->quiesce(mddev
, 0);
5580 mddev
->bitmap_info
.offset
= 0;
5583 md_update_sb(mddev
, 1);
5587 static int set_disk_faulty(mddev_t
*mddev
, dev_t dev
)
5591 if (mddev
->pers
== NULL
)
5594 rdev
= find_rdev(mddev
, dev
);
5598 md_error(mddev
, rdev
);
5603 * We have a problem here : there is no easy way to give a CHS
5604 * virtual geometry. We currently pretend that we have a 2 heads
5605 * 4 sectors (with a BIG number of cylinders...). This drives
5606 * dosfs just mad... ;-)
5608 static int md_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
5610 mddev_t
*mddev
= bdev
->bd_disk
->private_data
;
5614 geo
->cylinders
= mddev
->array_sectors
/ 8;
5618 static int md_ioctl(struct block_device
*bdev
, fmode_t mode
,
5619 unsigned int cmd
, unsigned long arg
)
5622 void __user
*argp
= (void __user
*)arg
;
5623 mddev_t
*mddev
= NULL
;
5626 if (!capable(CAP_SYS_ADMIN
))
5630 * Commands dealing with the RAID driver but not any
5636 err
= get_version(argp
);
5639 case PRINT_RAID_DEBUG
:
5647 autostart_arrays(arg
);
5654 * Commands creating/starting a new array:
5657 mddev
= bdev
->bd_disk
->private_data
;
5664 err
= mddev_lock(mddev
);
5667 "md: ioctl lock interrupted, reason %d, cmd %d\n",
5674 case SET_ARRAY_INFO
:
5676 mdu_array_info_t info
;
5678 memset(&info
, 0, sizeof(info
));
5679 else if (copy_from_user(&info
, argp
, sizeof(info
))) {
5684 err
= update_array_info(mddev
, &info
);
5686 printk(KERN_WARNING
"md: couldn't update"
5687 " array info. %d\n", err
);
5692 if (!list_empty(&mddev
->disks
)) {
5694 "md: array %s already has disks!\n",
5699 if (mddev
->raid_disks
) {
5701 "md: array %s already initialised!\n",
5706 err
= set_array_info(mddev
, &info
);
5708 printk(KERN_WARNING
"md: couldn't set"
5709 " array info. %d\n", err
);
5719 * Commands querying/configuring an existing array:
5721 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
5722 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
5723 if ((!mddev
->raid_disks
&& !mddev
->external
)
5724 && cmd
!= ADD_NEW_DISK
&& cmd
!= STOP_ARRAY
5725 && cmd
!= RUN_ARRAY
&& cmd
!= SET_BITMAP_FILE
5726 && cmd
!= GET_BITMAP_FILE
) {
5732 * Commands even a read-only array can execute:
5736 case GET_ARRAY_INFO
:
5737 err
= get_array_info(mddev
, argp
);
5740 case GET_BITMAP_FILE
:
5741 err
= get_bitmap_file(mddev
, argp
);
5745 err
= get_disk_info(mddev
, argp
);
5748 case RESTART_ARRAY_RW
:
5749 err
= restart_array(mddev
);
5753 err
= do_md_stop(mddev
, 0, 1);
5757 err
= md_set_readonly(mddev
, 1);
5761 if (get_user(ro
, (int __user
*)(arg
))) {
5767 /* if the bdev is going readonly the value of mddev->ro
5768 * does not matter, no writes are coming
5773 /* are we are already prepared for writes? */
5777 /* transitioning to readauto need only happen for
5778 * arrays that call md_write_start
5781 err
= restart_array(mddev
);
5784 set_disk_ro(mddev
->gendisk
, 0);
5791 * The remaining ioctls are changing the state of the
5792 * superblock, so we do not allow them on read-only arrays.
5793 * However non-MD ioctls (e.g. get-size) will still come through
5794 * here and hit the 'default' below, so only disallow
5795 * 'md' ioctls, and switch to rw mode if started auto-readonly.
5797 if (_IOC_TYPE(cmd
) == MD_MAJOR
&& mddev
->ro
&& mddev
->pers
) {
5798 if (mddev
->ro
== 2) {
5800 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
5801 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5802 md_wakeup_thread(mddev
->thread
);
5813 mdu_disk_info_t info
;
5814 if (copy_from_user(&info
, argp
, sizeof(info
)))
5817 err
= add_new_disk(mddev
, &info
);
5821 case HOT_REMOVE_DISK
:
5822 err
= hot_remove_disk(mddev
, new_decode_dev(arg
));
5826 err
= hot_add_disk(mddev
, new_decode_dev(arg
));
5829 case SET_DISK_FAULTY
:
5830 err
= set_disk_faulty(mddev
, new_decode_dev(arg
));
5834 err
= do_md_run(mddev
);
5837 case SET_BITMAP_FILE
:
5838 err
= set_bitmap_file(mddev
, (int)arg
);
5848 if (mddev
->hold_active
== UNTIL_IOCTL
&&
5850 mddev
->hold_active
= 0;
5851 mddev_unlock(mddev
);
5860 #ifdef CONFIG_COMPAT
5861 static int md_compat_ioctl(struct block_device
*bdev
, fmode_t mode
,
5862 unsigned int cmd
, unsigned long arg
)
5865 case HOT_REMOVE_DISK
:
5867 case SET_DISK_FAULTY
:
5868 case SET_BITMAP_FILE
:
5869 /* These take in integer arg, do not convert */
5872 arg
= (unsigned long)compat_ptr(arg
);
5876 return md_ioctl(bdev
, mode
, cmd
, arg
);
5878 #endif /* CONFIG_COMPAT */
5880 static int md_open(struct block_device
*bdev
, fmode_t mode
)
5883 * Succeed if we can lock the mddev, which confirms that
5884 * it isn't being stopped right now.
5886 mddev_t
*mddev
= mddev_find(bdev
->bd_dev
);
5890 if (mddev
->gendisk
!= bdev
->bd_disk
) {
5891 /* we are racing with mddev_put which is discarding this
5895 /* Wait until bdev->bd_disk is definitely gone */
5896 flush_scheduled_work();
5897 /* Then retry the open from the top */
5899 return -ERESTARTSYS
;
5901 BUG_ON(mddev
!= bdev
->bd_disk
->private_data
);
5903 if ((err
= mutex_lock_interruptible(&mddev
->open_mutex
)))
5907 atomic_inc(&mddev
->openers
);
5908 mutex_unlock(&mddev
->open_mutex
);
5910 check_disk_size_change(mddev
->gendisk
, bdev
);
5916 static int md_release(struct gendisk
*disk
, fmode_t mode
)
5918 mddev_t
*mddev
= disk
->private_data
;
5922 atomic_dec(&mddev
->openers
);
5928 static const struct block_device_operations md_fops
=
5930 .owner
= THIS_MODULE
,
5932 .release
= md_release
,
5934 #ifdef CONFIG_COMPAT
5935 .compat_ioctl
= md_compat_ioctl
,
5937 .getgeo
= md_getgeo
,
5940 static int md_thread(void * arg
)
5942 mdk_thread_t
*thread
= arg
;
5945 * md_thread is a 'system-thread', it's priority should be very
5946 * high. We avoid resource deadlocks individually in each
5947 * raid personality. (RAID5 does preallocation) We also use RR and
5948 * the very same RT priority as kswapd, thus we will never get
5949 * into a priority inversion deadlock.
5951 * we definitely have to have equal or higher priority than
5952 * bdflush, otherwise bdflush will deadlock if there are too
5953 * many dirty RAID5 blocks.
5956 allow_signal(SIGKILL
);
5957 while (!kthread_should_stop()) {
5959 /* We need to wait INTERRUPTIBLE so that
5960 * we don't add to the load-average.
5961 * That means we need to be sure no signals are
5964 if (signal_pending(current
))
5965 flush_signals(current
);
5967 wait_event_interruptible_timeout
5969 test_bit(THREAD_WAKEUP
, &thread
->flags
)
5970 || kthread_should_stop(),
5973 clear_bit(THREAD_WAKEUP
, &thread
->flags
);
5975 thread
->run(thread
->mddev
);
5981 void md_wakeup_thread(mdk_thread_t
*thread
)
5984 dprintk("md: waking up MD thread %s.\n", thread
->tsk
->comm
);
5985 set_bit(THREAD_WAKEUP
, &thread
->flags
);
5986 wake_up(&thread
->wqueue
);
5990 mdk_thread_t
*md_register_thread(void (*run
) (mddev_t
*), mddev_t
*mddev
,
5993 mdk_thread_t
*thread
;
5995 thread
= kzalloc(sizeof(mdk_thread_t
), GFP_KERNEL
);
5999 init_waitqueue_head(&thread
->wqueue
);
6002 thread
->mddev
= mddev
;
6003 thread
->timeout
= MAX_SCHEDULE_TIMEOUT
;
6004 thread
->tsk
= kthread_run(md_thread
, thread
,
6006 mdname(thread
->mddev
),
6007 name
?: mddev
->pers
->name
);
6008 if (IS_ERR(thread
->tsk
)) {
6015 void md_unregister_thread(mdk_thread_t
*thread
)
6019 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread
->tsk
));
6021 kthread_stop(thread
->tsk
);
6025 void md_error(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
6032 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
))
6035 if (mddev
->external
)
6036 set_bit(Blocked
, &rdev
->flags
);
6038 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
6040 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
6041 __builtin_return_address(0),__builtin_return_address(1),
6042 __builtin_return_address(2),__builtin_return_address(3));
6046 if (!mddev
->pers
->error_handler
)
6048 mddev
->pers
->error_handler(mddev
,rdev
);
6049 if (mddev
->degraded
)
6050 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
6051 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
6052 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6053 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6054 md_wakeup_thread(mddev
->thread
);
6055 if (mddev
->event_work
.func
)
6056 schedule_work(&mddev
->event_work
);
6057 md_new_event_inintr(mddev
);
6060 /* seq_file implementation /proc/mdstat */
6062 static void status_unused(struct seq_file
*seq
)
6067 seq_printf(seq
, "unused devices: ");
6069 list_for_each_entry(rdev
, &pending_raid_disks
, same_set
) {
6070 char b
[BDEVNAME_SIZE
];
6072 seq_printf(seq
, "%s ",
6073 bdevname(rdev
->bdev
,b
));
6076 seq_printf(seq
, "<none>");
6078 seq_printf(seq
, "\n");
6082 static void status_resync(struct seq_file
*seq
, mddev_t
* mddev
)
6084 sector_t max_sectors
, resync
, res
;
6085 unsigned long dt
, db
;
6088 unsigned int per_milli
;
6090 resync
= mddev
->curr_resync
- atomic_read(&mddev
->recovery_active
);
6092 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
6093 max_sectors
= mddev
->resync_max_sectors
;
6095 max_sectors
= mddev
->dev_sectors
;
6098 * Should not happen.
6104 /* Pick 'scale' such that (resync>>scale)*1000 will fit
6105 * in a sector_t, and (max_sectors>>scale) will fit in a
6106 * u32, as those are the requirements for sector_div.
6107 * Thus 'scale' must be at least 10
6110 if (sizeof(sector_t
) > sizeof(unsigned long)) {
6111 while ( max_sectors
/2 > (1ULL<<(scale
+32)))
6114 res
= (resync
>>scale
)*1000;
6115 sector_div(res
, (u32
)((max_sectors
>>scale
)+1));
6119 int i
, x
= per_milli
/50, y
= 20-x
;
6120 seq_printf(seq
, "[");
6121 for (i
= 0; i
< x
; i
++)
6122 seq_printf(seq
, "=");
6123 seq_printf(seq
, ">");
6124 for (i
= 0; i
< y
; i
++)
6125 seq_printf(seq
, ".");
6126 seq_printf(seq
, "] ");
6128 seq_printf(seq
, " %s =%3u.%u%% (%llu/%llu)",
6129 (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
)?
6131 (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
)?
6133 (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ?
6134 "resync" : "recovery"))),
6135 per_milli
/10, per_milli
% 10,
6136 (unsigned long long) resync
/2,
6137 (unsigned long long) max_sectors
/2);
6140 * dt: time from mark until now
6141 * db: blocks written from mark until now
6142 * rt: remaining time
6144 * rt is a sector_t, so could be 32bit or 64bit.
6145 * So we divide before multiply in case it is 32bit and close
6147 * We scale the divisor (db) by 32 to avoid loosing precision
6148 * near the end of resync when the number of remaining sectors
6150 * We then divide rt by 32 after multiplying by db to compensate.
6151 * The '+1' avoids division by zero if db is very small.
6153 dt
= ((jiffies
- mddev
->resync_mark
) / HZ
);
6155 db
= (mddev
->curr_mark_cnt
- atomic_read(&mddev
->recovery_active
))
6156 - mddev
->resync_mark_cnt
;
6158 rt
= max_sectors
- resync
; /* number of remaining sectors */
6159 sector_div(rt
, db
/32+1);
6163 seq_printf(seq
, " finish=%lu.%lumin", (unsigned long)rt
/ 60,
6164 ((unsigned long)rt
% 60)/6);
6166 seq_printf(seq
, " speed=%ldK/sec", db
/2/dt
);
6169 static void *md_seq_start(struct seq_file
*seq
, loff_t
*pos
)
6171 struct list_head
*tmp
;
6181 spin_lock(&all_mddevs_lock
);
6182 list_for_each(tmp
,&all_mddevs
)
6184 mddev
= list_entry(tmp
, mddev_t
, all_mddevs
);
6186 spin_unlock(&all_mddevs_lock
);
6189 spin_unlock(&all_mddevs_lock
);
6191 return (void*)2;/* tail */
6195 static void *md_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
6197 struct list_head
*tmp
;
6198 mddev_t
*next_mddev
, *mddev
= v
;
6204 spin_lock(&all_mddevs_lock
);
6206 tmp
= all_mddevs
.next
;
6208 tmp
= mddev
->all_mddevs
.next
;
6209 if (tmp
!= &all_mddevs
)
6210 next_mddev
= mddev_get(list_entry(tmp
,mddev_t
,all_mddevs
));
6212 next_mddev
= (void*)2;
6215 spin_unlock(&all_mddevs_lock
);
6223 static void md_seq_stop(struct seq_file
*seq
, void *v
)
6227 if (mddev
&& v
!= (void*)1 && v
!= (void*)2)
6231 struct mdstat_info
{
6235 static int md_seq_show(struct seq_file
*seq
, void *v
)
6240 struct mdstat_info
*mi
= seq
->private;
6241 struct bitmap
*bitmap
;
6243 if (v
== (void*)1) {
6244 struct mdk_personality
*pers
;
6245 seq_printf(seq
, "Personalities : ");
6246 spin_lock(&pers_lock
);
6247 list_for_each_entry(pers
, &pers_list
, list
)
6248 seq_printf(seq
, "[%s] ", pers
->name
);
6250 spin_unlock(&pers_lock
);
6251 seq_printf(seq
, "\n");
6252 mi
->event
= atomic_read(&md_event_count
);
6255 if (v
== (void*)2) {
6260 if (mddev_lock(mddev
) < 0)
6263 if (mddev
->pers
|| mddev
->raid_disks
|| !list_empty(&mddev
->disks
)) {
6264 seq_printf(seq
, "%s : %sactive", mdname(mddev
),
6265 mddev
->pers
? "" : "in");
6268 seq_printf(seq
, " (read-only)");
6270 seq_printf(seq
, " (auto-read-only)");
6271 seq_printf(seq
, " %s", mddev
->pers
->name
);
6275 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
6276 char b
[BDEVNAME_SIZE
];
6277 seq_printf(seq
, " %s[%d]",
6278 bdevname(rdev
->bdev
,b
), rdev
->desc_nr
);
6279 if (test_bit(WriteMostly
, &rdev
->flags
))
6280 seq_printf(seq
, "(W)");
6281 if (test_bit(Faulty
, &rdev
->flags
)) {
6282 seq_printf(seq
, "(F)");
6284 } else if (rdev
->raid_disk
< 0)
6285 seq_printf(seq
, "(S)"); /* spare */
6286 sectors
+= rdev
->sectors
;
6289 if (!list_empty(&mddev
->disks
)) {
6291 seq_printf(seq
, "\n %llu blocks",
6292 (unsigned long long)
6293 mddev
->array_sectors
/ 2);
6295 seq_printf(seq
, "\n %llu blocks",
6296 (unsigned long long)sectors
/ 2);
6298 if (mddev
->persistent
) {
6299 if (mddev
->major_version
!= 0 ||
6300 mddev
->minor_version
!= 90) {
6301 seq_printf(seq
," super %d.%d",
6302 mddev
->major_version
,
6303 mddev
->minor_version
);
6305 } else if (mddev
->external
)
6306 seq_printf(seq
, " super external:%s",
6307 mddev
->metadata_type
);
6309 seq_printf(seq
, " super non-persistent");
6312 mddev
->pers
->status(seq
, mddev
);
6313 seq_printf(seq
, "\n ");
6314 if (mddev
->pers
->sync_request
) {
6315 if (mddev
->curr_resync
> 2) {
6316 status_resync(seq
, mddev
);
6317 seq_printf(seq
, "\n ");
6318 } else if (mddev
->curr_resync
== 1 || mddev
->curr_resync
== 2)
6319 seq_printf(seq
, "\tresync=DELAYED\n ");
6320 else if (mddev
->recovery_cp
< MaxSector
)
6321 seq_printf(seq
, "\tresync=PENDING\n ");
6324 seq_printf(seq
, "\n ");
6326 if ((bitmap
= mddev
->bitmap
)) {
6327 unsigned long chunk_kb
;
6328 unsigned long flags
;
6329 spin_lock_irqsave(&bitmap
->lock
, flags
);
6330 chunk_kb
= mddev
->bitmap_info
.chunksize
>> 10;
6331 seq_printf(seq
, "bitmap: %lu/%lu pages [%luKB], "
6333 bitmap
->pages
- bitmap
->missing_pages
,
6335 (bitmap
->pages
- bitmap
->missing_pages
)
6336 << (PAGE_SHIFT
- 10),
6337 chunk_kb
? chunk_kb
: mddev
->bitmap_info
.chunksize
,
6338 chunk_kb
? "KB" : "B");
6340 seq_printf(seq
, ", file: ");
6341 seq_path(seq
, &bitmap
->file
->f_path
, " \t\n");
6344 seq_printf(seq
, "\n");
6345 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
6348 seq_printf(seq
, "\n");
6350 mddev_unlock(mddev
);
6355 static const struct seq_operations md_seq_ops
= {
6356 .start
= md_seq_start
,
6357 .next
= md_seq_next
,
6358 .stop
= md_seq_stop
,
6359 .show
= md_seq_show
,
6362 static int md_seq_open(struct inode
*inode
, struct file
*file
)
6365 struct mdstat_info
*mi
= kmalloc(sizeof(*mi
), GFP_KERNEL
);
6369 error
= seq_open(file
, &md_seq_ops
);
6373 struct seq_file
*p
= file
->private_data
;
6375 mi
->event
= atomic_read(&md_event_count
);
6380 static unsigned int mdstat_poll(struct file
*filp
, poll_table
*wait
)
6382 struct seq_file
*m
= filp
->private_data
;
6383 struct mdstat_info
*mi
= m
->private;
6386 poll_wait(filp
, &md_event_waiters
, wait
);
6388 /* always allow read */
6389 mask
= POLLIN
| POLLRDNORM
;
6391 if (mi
->event
!= atomic_read(&md_event_count
))
6392 mask
|= POLLERR
| POLLPRI
;
6396 static const struct file_operations md_seq_fops
= {
6397 .owner
= THIS_MODULE
,
6398 .open
= md_seq_open
,
6400 .llseek
= seq_lseek
,
6401 .release
= seq_release_private
,
6402 .poll
= mdstat_poll
,
6405 int register_md_personality(struct mdk_personality
*p
)
6407 spin_lock(&pers_lock
);
6408 list_add_tail(&p
->list
, &pers_list
);
6409 printk(KERN_INFO
"md: %s personality registered for level %d\n", p
->name
, p
->level
);
6410 spin_unlock(&pers_lock
);
6414 int unregister_md_personality(struct mdk_personality
*p
)
6416 printk(KERN_INFO
"md: %s personality unregistered\n", p
->name
);
6417 spin_lock(&pers_lock
);
6418 list_del_init(&p
->list
);
6419 spin_unlock(&pers_lock
);
6423 static int is_mddev_idle(mddev_t
*mddev
, int init
)
6431 rdev_for_each_rcu(rdev
, mddev
) {
6432 struct gendisk
*disk
= rdev
->bdev
->bd_contains
->bd_disk
;
6433 curr_events
= (int)part_stat_read(&disk
->part0
, sectors
[0]) +
6434 (int)part_stat_read(&disk
->part0
, sectors
[1]) -
6435 atomic_read(&disk
->sync_io
);
6436 /* sync IO will cause sync_io to increase before the disk_stats
6437 * as sync_io is counted when a request starts, and
6438 * disk_stats is counted when it completes.
6439 * So resync activity will cause curr_events to be smaller than
6440 * when there was no such activity.
6441 * non-sync IO will cause disk_stat to increase without
6442 * increasing sync_io so curr_events will (eventually)
6443 * be larger than it was before. Once it becomes
6444 * substantially larger, the test below will cause
6445 * the array to appear non-idle, and resync will slow
6447 * If there is a lot of outstanding resync activity when
6448 * we set last_event to curr_events, then all that activity
6449 * completing might cause the array to appear non-idle
6450 * and resync will be slowed down even though there might
6451 * not have been non-resync activity. This will only
6452 * happen once though. 'last_events' will soon reflect
6453 * the state where there is little or no outstanding
6454 * resync requests, and further resync activity will
6455 * always make curr_events less than last_events.
6458 if (init
|| curr_events
- rdev
->last_events
> 64) {
6459 rdev
->last_events
= curr_events
;
6467 void md_done_sync(mddev_t
*mddev
, int blocks
, int ok
)
6469 /* another "blocks" (512byte) blocks have been synced */
6470 atomic_sub(blocks
, &mddev
->recovery_active
);
6471 wake_up(&mddev
->recovery_wait
);
6473 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6474 md_wakeup_thread(mddev
->thread
);
6475 // stop recovery, signal do_sync ....
6480 /* md_write_start(mddev, bi)
6481 * If we need to update some array metadata (e.g. 'active' flag
6482 * in superblock) before writing, schedule a superblock update
6483 * and wait for it to complete.
6485 void md_write_start(mddev_t
*mddev
, struct bio
*bi
)
6488 if (bio_data_dir(bi
) != WRITE
)
6491 BUG_ON(mddev
->ro
== 1);
6492 if (mddev
->ro
== 2) {
6493 /* need to switch to read/write */
6495 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6496 md_wakeup_thread(mddev
->thread
);
6497 md_wakeup_thread(mddev
->sync_thread
);
6500 atomic_inc(&mddev
->writes_pending
);
6501 if (mddev
->safemode
== 1)
6502 mddev
->safemode
= 0;
6503 if (mddev
->in_sync
) {
6504 spin_lock_irq(&mddev
->write_lock
);
6505 if (mddev
->in_sync
) {
6507 set_bit(MD_CHANGE_CLEAN
, &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_CLEAN
, &mddev
->flags
) &&
6517 !test_bit(MD_CHANGE_PENDING
, &mddev
->flags
));
6520 void md_write_end(mddev_t
*mddev
)
6522 if (atomic_dec_and_test(&mddev
->writes_pending
)) {
6523 if (mddev
->safemode
== 2)
6524 md_wakeup_thread(mddev
->thread
);
6525 else if (mddev
->safemode_delay
)
6526 mod_timer(&mddev
->safemode_timer
, jiffies
+ mddev
->safemode_delay
);
6530 /* md_allow_write(mddev)
6531 * Calling this ensures that the array is marked 'active' so that writes
6532 * may proceed without blocking. It is important to call this before
6533 * attempting a GFP_KERNEL allocation while holding the mddev lock.
6534 * Must be called with mddev_lock held.
6536 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
6537 * is dropped, so return -EAGAIN after notifying userspace.
6539 int md_allow_write(mddev_t
*mddev
)
6545 if (!mddev
->pers
->sync_request
)
6548 spin_lock_irq(&mddev
->write_lock
);
6549 if (mddev
->in_sync
) {
6551 set_bit(MD_CHANGE_CLEAN
, &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_CLEAN
, &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 if (mddev
->persistent
)
6760 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
6761 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
6764 while (j
>= mddev
->resync_max
&& !kthread_should_stop()) {
6765 /* As this condition is controlled by user-space,
6766 * we can block indefinitely, so use '_interruptible'
6767 * to avoid triggering warnings.
6769 flush_signals(current
); /* just in case */
6770 wait_event_interruptible(mddev
->recovery_wait
,
6771 mddev
->resync_max
> j
6772 || kthread_should_stop());
6775 if (kthread_should_stop())
6778 sectors
= mddev
->pers
->sync_request(mddev
, j
, &skipped
,
6779 currspeed
< speed_min(mddev
));
6781 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6785 if (!skipped
) { /* actual IO requested */
6786 io_sectors
+= sectors
;
6787 atomic_add(sectors
, &mddev
->recovery_active
);
6791 if (j
>1) mddev
->curr_resync
= j
;
6792 mddev
->curr_mark_cnt
= io_sectors
;
6793 if (last_check
== 0)
6794 /* this is the earliers that rebuilt will be
6795 * visible in /proc/mdstat
6797 md_new_event(mddev
);
6799 if (last_check
+ window
> io_sectors
|| j
== max_sectors
)
6802 last_check
= io_sectors
;
6804 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
6808 if (time_after_eq(jiffies
, mark
[last_mark
] + SYNC_MARK_STEP
)) {
6810 int next
= (last_mark
+1) % SYNC_MARKS
;
6812 mddev
->resync_mark
= mark
[next
];
6813 mddev
->resync_mark_cnt
= mark_cnt
[next
];
6814 mark
[next
] = jiffies
;
6815 mark_cnt
[next
] = io_sectors
- atomic_read(&mddev
->recovery_active
);
6820 if (kthread_should_stop())
6825 * this loop exits only if either when we are slower than
6826 * the 'hard' speed limit, or the system was IO-idle for
6828 * the system might be non-idle CPU-wise, but we only care
6829 * about not overloading the IO subsystem. (things like an
6830 * e2fsck being done on the RAID array should execute fast)
6835 currspeed
= ((unsigned long)(io_sectors
-mddev
->resync_mark_cnt
))/2
6836 /((jiffies
-mddev
->resync_mark
)/HZ
+1) +1;
6838 if (currspeed
> speed_min(mddev
)) {
6839 if ((currspeed
> speed_max(mddev
)) ||
6840 !is_mddev_idle(mddev
, 0)) {
6846 printk(KERN_INFO
"md: %s: %s done.\n",mdname(mddev
), desc
);
6848 * this also signals 'finished resyncing' to md_stop
6853 wait_event(mddev
->recovery_wait
, !atomic_read(&mddev
->recovery_active
));
6855 /* tell personality that we are finished */
6856 mddev
->pers
->sync_request(mddev
, max_sectors
, &skipped
, 1);
6858 if (!test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
) &&
6859 mddev
->curr_resync
> 2) {
6860 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
6861 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
6862 if (mddev
->curr_resync
>= mddev
->recovery_cp
) {
6864 "md: checkpointing %s of %s.\n",
6865 desc
, mdname(mddev
));
6866 mddev
->recovery_cp
= mddev
->curr_resync
;
6869 mddev
->recovery_cp
= MaxSector
;
6871 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
6872 mddev
->curr_resync
= MaxSector
;
6874 list_for_each_entry_rcu(rdev
, &mddev
->disks
, same_set
)
6875 if (rdev
->raid_disk
>= 0 &&
6876 mddev
->delta_disks
>= 0 &&
6877 !test_bit(Faulty
, &rdev
->flags
) &&
6878 !test_bit(In_sync
, &rdev
->flags
) &&
6879 rdev
->recovery_offset
< mddev
->curr_resync
)
6880 rdev
->recovery_offset
= mddev
->curr_resync
;
6884 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
6887 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
6888 /* We completed so min/max setting can be forgotten if used. */
6889 if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
6890 mddev
->resync_min
= 0;
6891 mddev
->resync_max
= MaxSector
;
6892 } else if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
6893 mddev
->resync_min
= mddev
->curr_resync_completed
;
6894 mddev
->curr_resync
= 0;
6895 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
6896 mddev
->curr_resync_completed
= 0;
6897 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
6898 wake_up(&resync_wait
);
6899 set_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
6900 md_wakeup_thread(mddev
->thread
);
6905 * got a signal, exit.
6908 "md: md_do_sync() got signal ... exiting\n");
6909 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6913 EXPORT_SYMBOL_GPL(md_do_sync
);
6916 static int remove_and_add_spares(mddev_t
*mddev
)
6921 mddev
->curr_resync_completed
= 0;
6923 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
6924 if (rdev
->raid_disk
>= 0 &&
6925 !test_bit(Blocked
, &rdev
->flags
) &&
6926 (test_bit(Faulty
, &rdev
->flags
) ||
6927 ! test_bit(In_sync
, &rdev
->flags
)) &&
6928 atomic_read(&rdev
->nr_pending
)==0) {
6929 if (mddev
->pers
->hot_remove_disk(
6930 mddev
, rdev
->raid_disk
)==0) {
6932 sprintf(nm
,"rd%d", rdev
->raid_disk
);
6933 sysfs_remove_link(&mddev
->kobj
, nm
);
6934 rdev
->raid_disk
= -1;
6938 if (mddev
->degraded
&& ! mddev
->ro
&& !mddev
->recovery_disabled
) {
6939 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
6940 if (rdev
->raid_disk
>= 0 &&
6941 !test_bit(In_sync
, &rdev
->flags
) &&
6942 !test_bit(Blocked
, &rdev
->flags
))
6944 if (rdev
->raid_disk
< 0
6945 && !test_bit(Faulty
, &rdev
->flags
)) {
6946 rdev
->recovery_offset
= 0;
6948 hot_add_disk(mddev
, rdev
) == 0) {
6950 sprintf(nm
, "rd%d", rdev
->raid_disk
);
6951 if (sysfs_create_link(&mddev
->kobj
,
6953 /* failure here is OK */;
6955 md_new_event(mddev
);
6956 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
6965 * This routine is regularly called by all per-raid-array threads to
6966 * deal with generic issues like resync and super-block update.
6967 * Raid personalities that don't have a thread (linear/raid0) do not
6968 * need this as they never do any recovery or update the superblock.
6970 * It does not do any resync itself, but rather "forks" off other threads
6971 * to do that as needed.
6972 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
6973 * "->recovery" and create a thread at ->sync_thread.
6974 * When the thread finishes it sets MD_RECOVERY_DONE
6975 * and wakeups up this thread which will reap the thread and finish up.
6976 * This thread also removes any faulty devices (with nr_pending == 0).
6978 * The overall approach is:
6979 * 1/ if the superblock needs updating, update it.
6980 * 2/ If a recovery thread is running, don't do anything else.
6981 * 3/ If recovery has finished, clean up, possibly marking spares active.
6982 * 4/ If there are any faulty devices, remove them.
6983 * 5/ If array is degraded, try to add spares devices
6984 * 6/ If array has spares or is not in-sync, start a resync thread.
6986 void md_check_recovery(mddev_t
*mddev
)
6992 bitmap_daemon_work(mddev
);
6997 if (signal_pending(current
)) {
6998 if (mddev
->pers
->sync_request
&& !mddev
->external
) {
6999 printk(KERN_INFO
"md: %s in immediate safe mode\n",
7001 mddev
->safemode
= 2;
7003 flush_signals(current
);
7006 if (mddev
->ro
&& !test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))
7009 (mddev
->flags
&& !mddev
->external
) ||
7010 test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
) ||
7011 test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
) ||
7012 (mddev
->external
== 0 && mddev
->safemode
== 1) ||
7013 (mddev
->safemode
== 2 && ! atomic_read(&mddev
->writes_pending
)
7014 && !mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
)
7018 if (mddev_trylock(mddev
)) {
7022 /* Only thing we do on a ro array is remove
7025 remove_and_add_spares(mddev
);
7026 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7030 if (!mddev
->external
) {
7032 spin_lock_irq(&mddev
->write_lock
);
7033 if (mddev
->safemode
&&
7034 !atomic_read(&mddev
->writes_pending
) &&
7036 mddev
->recovery_cp
== MaxSector
) {
7039 if (mddev
->persistent
)
7040 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
7042 if (mddev
->safemode
== 1)
7043 mddev
->safemode
= 0;
7044 spin_unlock_irq(&mddev
->write_lock
);
7046 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
7050 md_update_sb(mddev
, 0);
7052 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) &&
7053 !test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
)) {
7054 /* resync/recovery still happening */
7055 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7058 if (mddev
->sync_thread
) {
7059 /* resync has finished, collect result */
7060 md_unregister_thread(mddev
->sync_thread
);
7061 mddev
->sync_thread
= NULL
;
7062 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
) &&
7063 !test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
)) {
7065 /* activate any spares */
7066 if (mddev
->pers
->spare_active(mddev
))
7067 sysfs_notify(&mddev
->kobj
, NULL
,
7070 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
7071 mddev
->pers
->finish_reshape
)
7072 mddev
->pers
->finish_reshape(mddev
);
7073 md_update_sb(mddev
, 1);
7075 /* if array is no-longer degraded, then any saved_raid_disk
7076 * information must be scrapped
7078 if (!mddev
->degraded
)
7079 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
7080 rdev
->saved_raid_disk
= -1;
7082 mddev
->recovery
= 0;
7083 /* flag recovery needed just to double check */
7084 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7085 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
7086 md_new_event(mddev
);
7089 /* Set RUNNING before clearing NEEDED to avoid
7090 * any transients in the value of "sync_action".
7092 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
7093 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7094 /* Clear some bits that don't mean anything, but
7097 clear_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
7098 clear_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
7100 if (test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
))
7102 /* no recovery is running.
7103 * remove any failed drives, then
7104 * add spares if possible.
7105 * Spare are also removed and re-added, to allow
7106 * the personality to fail the re-add.
7109 if (mddev
->reshape_position
!= MaxSector
) {
7110 if (mddev
->pers
->check_reshape
== NULL
||
7111 mddev
->pers
->check_reshape(mddev
) != 0)
7112 /* Cannot proceed */
7114 set_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
7115 clear_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
7116 } else if ((spares
= remove_and_add_spares(mddev
))) {
7117 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
7118 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
7119 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
7120 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
7121 } else if (mddev
->recovery_cp
< MaxSector
) {
7122 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
7123 clear_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
7124 } else if (!test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
7125 /* nothing to be done ... */
7128 if (mddev
->pers
->sync_request
) {
7129 if (spares
&& mddev
->bitmap
&& ! mddev
->bitmap
->file
) {
7130 /* We are adding a device or devices to an array
7131 * which has the bitmap stored on all devices.
7132 * So make sure all bitmap pages get written
7134 bitmap_write_all(mddev
->bitmap
);
7136 mddev
->sync_thread
= md_register_thread(md_do_sync
,
7139 if (!mddev
->sync_thread
) {
7140 printk(KERN_ERR
"%s: could not start resync"
7143 /* leave the spares where they are, it shouldn't hurt */
7144 mddev
->recovery
= 0;
7146 md_wakeup_thread(mddev
->sync_thread
);
7147 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
7148 md_new_event(mddev
);
7151 if (!mddev
->sync_thread
) {
7152 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
7153 if (test_and_clear_bit(MD_RECOVERY_RECOVER
,
7155 if (mddev
->sysfs_action
)
7156 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
7158 mddev_unlock(mddev
);
7162 void md_wait_for_blocked_rdev(mdk_rdev_t
*rdev
, mddev_t
*mddev
)
7164 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
7165 wait_event_timeout(rdev
->blocked_wait
,
7166 !test_bit(Blocked
, &rdev
->flags
),
7167 msecs_to_jiffies(5000));
7168 rdev_dec_pending(rdev
, mddev
);
7170 EXPORT_SYMBOL(md_wait_for_blocked_rdev
);
7172 static int md_notify_reboot(struct notifier_block
*this,
7173 unsigned long code
, void *x
)
7175 struct list_head
*tmp
;
7178 if ((code
== SYS_DOWN
) || (code
== SYS_HALT
) || (code
== SYS_POWER_OFF
)) {
7180 printk(KERN_INFO
"md: stopping all md devices.\n");
7182 for_each_mddev(mddev
, tmp
)
7183 if (mddev_trylock(mddev
)) {
7184 /* Force a switch to readonly even array
7185 * appears to still be in use. Hence
7188 md_set_readonly(mddev
, 100);
7189 mddev_unlock(mddev
);
7192 * certain more exotic SCSI devices are known to be
7193 * volatile wrt too early system reboots. While the
7194 * right place to handle this issue is the given
7195 * driver, we do want to have a safe RAID driver ...
7202 static struct notifier_block md_notifier
= {
7203 .notifier_call
= md_notify_reboot
,
7205 .priority
= INT_MAX
, /* before any real devices */
7208 static void md_geninit(void)
7210 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t
));
7212 proc_create("mdstat", S_IRUGO
, NULL
, &md_seq_fops
);
7215 static int __init
md_init(void)
7217 if (register_blkdev(MD_MAJOR
, "md"))
7219 if ((mdp_major
=register_blkdev(0, "mdp"))<=0) {
7220 unregister_blkdev(MD_MAJOR
, "md");
7223 blk_register_region(MKDEV(MD_MAJOR
, 0), 1UL<<MINORBITS
, THIS_MODULE
,
7224 md_probe
, NULL
, NULL
);
7225 blk_register_region(MKDEV(mdp_major
, 0), 1UL<<MINORBITS
, THIS_MODULE
,
7226 md_probe
, NULL
, NULL
);
7228 register_reboot_notifier(&md_notifier
);
7229 raid_table_header
= register_sysctl_table(raid_root_table
);
7239 * Searches all registered partitions for autorun RAID arrays
7243 static LIST_HEAD(all_detected_devices
);
7244 struct detected_devices_node
{
7245 struct list_head list
;
7249 void md_autodetect_dev(dev_t dev
)
7251 struct detected_devices_node
*node_detected_dev
;
7253 node_detected_dev
= kzalloc(sizeof(*node_detected_dev
), GFP_KERNEL
);
7254 if (node_detected_dev
) {
7255 node_detected_dev
->dev
= dev
;
7256 list_add_tail(&node_detected_dev
->list
, &all_detected_devices
);
7258 printk(KERN_CRIT
"md: md_autodetect_dev: kzalloc failed"
7259 ", skipping dev(%d,%d)\n", MAJOR(dev
), MINOR(dev
));
7264 static void autostart_arrays(int part
)
7267 struct detected_devices_node
*node_detected_dev
;
7269 int i_scanned
, i_passed
;
7274 printk(KERN_INFO
"md: Autodetecting RAID arrays.\n");
7276 while (!list_empty(&all_detected_devices
) && i_scanned
< INT_MAX
) {
7278 node_detected_dev
= list_entry(all_detected_devices
.next
,
7279 struct detected_devices_node
, list
);
7280 list_del(&node_detected_dev
->list
);
7281 dev
= node_detected_dev
->dev
;
7282 kfree(node_detected_dev
);
7283 rdev
= md_import_device(dev
,0, 90);
7287 if (test_bit(Faulty
, &rdev
->flags
)) {
7291 set_bit(AutoDetected
, &rdev
->flags
);
7292 list_add(&rdev
->same_set
, &pending_raid_disks
);
7296 printk(KERN_INFO
"md: Scanned %d and added %d devices.\n",
7297 i_scanned
, i_passed
);
7299 autorun_devices(part
);
7302 #endif /* !MODULE */
7304 static __exit
void md_exit(void)
7307 struct list_head
*tmp
;
7309 blk_unregister_region(MKDEV(MD_MAJOR
,0), 1U << MINORBITS
);
7310 blk_unregister_region(MKDEV(mdp_major
,0), 1U << MINORBITS
);
7312 unregister_blkdev(MD_MAJOR
,"md");
7313 unregister_blkdev(mdp_major
, "mdp");
7314 unregister_reboot_notifier(&md_notifier
);
7315 unregister_sysctl_table(raid_table_header
);
7316 remove_proc_entry("mdstat", NULL
);
7317 for_each_mddev(mddev
, tmp
) {
7318 export_array(mddev
);
7319 mddev
->hold_active
= 0;
7323 subsys_initcall(md_init
);
7324 module_exit(md_exit
)
7326 static int get_ro(char *buffer
, struct kernel_param
*kp
)
7328 return sprintf(buffer
, "%d", start_readonly
);
7330 static int set_ro(const char *val
, struct kernel_param
*kp
)
7333 int num
= simple_strtoul(val
, &e
, 10);
7334 if (*val
&& (*e
== '\0' || *e
== '\n')) {
7335 start_readonly
= num
;
7341 module_param_call(start_ro
, set_ro
, get_ro
, NULL
, S_IRUSR
|S_IWUSR
);
7342 module_param(start_dirty_degraded
, int, S_IRUGO
|S_IWUSR
);
7344 module_param_call(new_array
, add_named_array
, NULL
, NULL
, S_IWUSR
);
7346 EXPORT_SYMBOL(register_md_personality
);
7347 EXPORT_SYMBOL(unregister_md_personality
);
7348 EXPORT_SYMBOL(md_error
);
7349 EXPORT_SYMBOL(md_done_sync
);
7350 EXPORT_SYMBOL(md_write_start
);
7351 EXPORT_SYMBOL(md_write_end
);
7352 EXPORT_SYMBOL(md_register_thread
);
7353 EXPORT_SYMBOL(md_unregister_thread
);
7354 EXPORT_SYMBOL(md_wakeup_thread
);
7355 EXPORT_SYMBOL(md_check_recovery
);
7356 MODULE_LICENSE("GPL");
7357 MODULE_DESCRIPTION("MD RAID framework");
7359 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR
);