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/module.h>
36 #include <linux/kthread.h>
37 #include <linux/linkage.h>
38 #include <linux/raid/md.h>
39 #include <linux/raid/bitmap.h>
40 #include <linux/sysctl.h>
41 #include <linux/buffer_head.h> /* for invalidate_bdev */
42 #include <linux/suspend.h>
43 #include <linux/poll.h>
44 #include <linux/mutex.h>
45 #include <linux/ctype.h>
47 #include <linux/init.h>
49 #include <linux/file.h>
52 #include <linux/kmod.h>
55 #include <asm/unaligned.h>
57 #define MAJOR_NR MD_MAJOR
60 /* 63 partitions with the alternate major number (mdp) */
61 #define MdpMinorShift 6
64 #define dprintk(x...) ((void)(DEBUG && printk(x)))
68 static void autostart_arrays (int part
);
71 static LIST_HEAD(pers_list
);
72 static DEFINE_SPINLOCK(pers_lock
);
74 static void md_print_devices(void);
76 #define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
79 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
80 * is 1000 KB/sec, so the extra system load does not show up that much.
81 * Increase it if you want to have more _guaranteed_ speed. Note that
82 * the RAID driver will use the maximum available bandwidth if the IO
83 * subsystem is idle. There is also an 'absolute maximum' reconstruction
84 * speed limit - in case reconstruction slows down your system despite
87 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
88 * or /sys/block/mdX/md/sync_speed_{min,max}
91 static int sysctl_speed_limit_min
= 1000;
92 static int sysctl_speed_limit_max
= 200000;
93 static inline int speed_min(mddev_t
*mddev
)
95 return mddev
->sync_speed_min
?
96 mddev
->sync_speed_min
: sysctl_speed_limit_min
;
99 static inline int speed_max(mddev_t
*mddev
)
101 return mddev
->sync_speed_max
?
102 mddev
->sync_speed_max
: sysctl_speed_limit_max
;
105 static struct ctl_table_header
*raid_table_header
;
107 static ctl_table raid_table
[] = {
109 .ctl_name
= DEV_RAID_SPEED_LIMIT_MIN
,
110 .procname
= "speed_limit_min",
111 .data
= &sysctl_speed_limit_min
,
112 .maxlen
= sizeof(int),
114 .proc_handler
= &proc_dointvec
,
117 .ctl_name
= DEV_RAID_SPEED_LIMIT_MAX
,
118 .procname
= "speed_limit_max",
119 .data
= &sysctl_speed_limit_max
,
120 .maxlen
= sizeof(int),
122 .proc_handler
= &proc_dointvec
,
127 static ctl_table raid_dir_table
[] = {
129 .ctl_name
= DEV_RAID
,
138 static ctl_table raid_root_table
[] = {
144 .child
= raid_dir_table
,
149 static struct block_device_operations md_fops
;
151 static int start_readonly
;
154 * We have a system wide 'event count' that is incremented
155 * on any 'interesting' event, and readers of /proc/mdstat
156 * can use 'poll' or 'select' to find out when the event
160 * start array, stop array, error, add device, remove device,
161 * start build, activate spare
163 static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters
);
164 static atomic_t md_event_count
;
165 void md_new_event(mddev_t
*mddev
)
167 atomic_inc(&md_event_count
);
168 wake_up(&md_event_waiters
);
169 sysfs_notify(&mddev
->kobj
, NULL
, "sync_action");
171 EXPORT_SYMBOL_GPL(md_new_event
);
173 /* Alternate version that can be called from interrupts
174 * when calling sysfs_notify isn't needed.
176 static void md_new_event_inintr(mddev_t
*mddev
)
178 atomic_inc(&md_event_count
);
179 wake_up(&md_event_waiters
);
183 * Enables to iterate over all existing md arrays
184 * all_mddevs_lock protects this list.
186 static LIST_HEAD(all_mddevs
);
187 static DEFINE_SPINLOCK(all_mddevs_lock
);
191 * iterates through all used mddevs in the system.
192 * We take care to grab the all_mddevs_lock whenever navigating
193 * the list, and to always hold a refcount when unlocked.
194 * Any code which breaks out of this loop while own
195 * a reference to the current mddev and must mddev_put it.
197 #define ITERATE_MDDEV(mddev,tmp) \
199 for (({ spin_lock(&all_mddevs_lock); \
200 tmp = all_mddevs.next; \
202 ({ if (tmp != &all_mddevs) \
203 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
204 spin_unlock(&all_mddevs_lock); \
205 if (mddev) mddev_put(mddev); \
206 mddev = list_entry(tmp, mddev_t, all_mddevs); \
207 tmp != &all_mddevs;}); \
208 ({ spin_lock(&all_mddevs_lock); \
213 static int md_fail_request (request_queue_t
*q
, struct bio
*bio
)
215 bio_io_error(bio
, bio
->bi_size
);
219 static inline mddev_t
*mddev_get(mddev_t
*mddev
)
221 atomic_inc(&mddev
->active
);
225 static void mddev_put(mddev_t
*mddev
)
227 if (!atomic_dec_and_lock(&mddev
->active
, &all_mddevs_lock
))
229 if (!mddev
->raid_disks
&& list_empty(&mddev
->disks
)) {
230 list_del(&mddev
->all_mddevs
);
231 spin_unlock(&all_mddevs_lock
);
232 blk_cleanup_queue(mddev
->queue
);
233 kobject_unregister(&mddev
->kobj
);
235 spin_unlock(&all_mddevs_lock
);
238 static mddev_t
* mddev_find(dev_t unit
)
240 mddev_t
*mddev
, *new = NULL
;
243 spin_lock(&all_mddevs_lock
);
244 list_for_each_entry(mddev
, &all_mddevs
, all_mddevs
)
245 if (mddev
->unit
== unit
) {
247 spin_unlock(&all_mddevs_lock
);
253 list_add(&new->all_mddevs
, &all_mddevs
);
254 spin_unlock(&all_mddevs_lock
);
257 spin_unlock(&all_mddevs_lock
);
259 new = kzalloc(sizeof(*new), GFP_KERNEL
);
264 if (MAJOR(unit
) == MD_MAJOR
)
265 new->md_minor
= MINOR(unit
);
267 new->md_minor
= MINOR(unit
) >> MdpMinorShift
;
269 mutex_init(&new->reconfig_mutex
);
270 INIT_LIST_HEAD(&new->disks
);
271 INIT_LIST_HEAD(&new->all_mddevs
);
272 init_timer(&new->safemode_timer
);
273 atomic_set(&new->active
, 1);
274 spin_lock_init(&new->write_lock
);
275 init_waitqueue_head(&new->sb_wait
);
277 new->queue
= blk_alloc_queue(GFP_KERNEL
);
282 set_bit(QUEUE_FLAG_CLUSTER
, &new->queue
->queue_flags
);
284 blk_queue_make_request(new->queue
, md_fail_request
);
289 static inline int mddev_lock(mddev_t
* mddev
)
291 return mutex_lock_interruptible(&mddev
->reconfig_mutex
);
294 static inline int mddev_trylock(mddev_t
* mddev
)
296 return mutex_trylock(&mddev
->reconfig_mutex
);
299 static inline void mddev_unlock(mddev_t
* mddev
)
301 mutex_unlock(&mddev
->reconfig_mutex
);
303 md_wakeup_thread(mddev
->thread
);
306 static mdk_rdev_t
* find_rdev_nr(mddev_t
*mddev
, int nr
)
309 struct list_head
*tmp
;
311 ITERATE_RDEV(mddev
,rdev
,tmp
) {
312 if (rdev
->desc_nr
== nr
)
318 static mdk_rdev_t
* find_rdev(mddev_t
* mddev
, dev_t dev
)
320 struct list_head
*tmp
;
323 ITERATE_RDEV(mddev
,rdev
,tmp
) {
324 if (rdev
->bdev
->bd_dev
== dev
)
330 static struct mdk_personality
*find_pers(int level
, char *clevel
)
332 struct mdk_personality
*pers
;
333 list_for_each_entry(pers
, &pers_list
, list
) {
334 if (level
!= LEVEL_NONE
&& pers
->level
== level
)
336 if (strcmp(pers
->name
, clevel
)==0)
342 static inline sector_t
calc_dev_sboffset(struct block_device
*bdev
)
344 sector_t size
= bdev
->bd_inode
->i_size
>> BLOCK_SIZE_BITS
;
345 return MD_NEW_SIZE_BLOCKS(size
);
348 static sector_t
calc_dev_size(mdk_rdev_t
*rdev
, unsigned chunk_size
)
352 size
= rdev
->sb_offset
;
355 size
&= ~((sector_t
)chunk_size
/1024 - 1);
359 static int alloc_disk_sb(mdk_rdev_t
* rdev
)
364 rdev
->sb_page
= alloc_page(GFP_KERNEL
);
365 if (!rdev
->sb_page
) {
366 printk(KERN_ALERT
"md: out of memory.\n");
373 static void free_disk_sb(mdk_rdev_t
* rdev
)
376 put_page(rdev
->sb_page
);
378 rdev
->sb_page
= NULL
;
385 static int super_written(struct bio
*bio
, unsigned int bytes_done
, int error
)
387 mdk_rdev_t
*rdev
= bio
->bi_private
;
388 mddev_t
*mddev
= rdev
->mddev
;
392 if (error
|| !test_bit(BIO_UPTODATE
, &bio
->bi_flags
))
393 md_error(mddev
, rdev
);
395 if (atomic_dec_and_test(&mddev
->pending_writes
))
396 wake_up(&mddev
->sb_wait
);
401 static int super_written_barrier(struct bio
*bio
, unsigned int bytes_done
, int error
)
403 struct bio
*bio2
= bio
->bi_private
;
404 mdk_rdev_t
*rdev
= bio2
->bi_private
;
405 mddev_t
*mddev
= rdev
->mddev
;
409 if (!test_bit(BIO_UPTODATE
, &bio
->bi_flags
) &&
410 error
== -EOPNOTSUPP
) {
412 /* barriers don't appear to be supported :-( */
413 set_bit(BarriersNotsupp
, &rdev
->flags
);
414 mddev
->barriers_work
= 0;
415 spin_lock_irqsave(&mddev
->write_lock
, flags
);
416 bio2
->bi_next
= mddev
->biolist
;
417 mddev
->biolist
= bio2
;
418 spin_unlock_irqrestore(&mddev
->write_lock
, flags
);
419 wake_up(&mddev
->sb_wait
);
424 bio
->bi_private
= rdev
;
425 return super_written(bio
, bytes_done
, error
);
428 void md_super_write(mddev_t
*mddev
, mdk_rdev_t
*rdev
,
429 sector_t sector
, int size
, struct page
*page
)
431 /* write first size bytes of page to sector of rdev
432 * Increment mddev->pending_writes before returning
433 * and decrement it on completion, waking up sb_wait
434 * if zero is reached.
435 * If an error occurred, call md_error
437 * As we might need to resubmit the request if BIO_RW_BARRIER
438 * causes ENOTSUPP, we allocate a spare bio...
440 struct bio
*bio
= bio_alloc(GFP_NOIO
, 1);
441 int rw
= (1<<BIO_RW
) | (1<<BIO_RW_SYNC
);
443 bio
->bi_bdev
= rdev
->bdev
;
444 bio
->bi_sector
= sector
;
445 bio_add_page(bio
, page
, size
, 0);
446 bio
->bi_private
= rdev
;
447 bio
->bi_end_io
= super_written
;
450 atomic_inc(&mddev
->pending_writes
);
451 if (!test_bit(BarriersNotsupp
, &rdev
->flags
)) {
453 rw
|= (1<<BIO_RW_BARRIER
);
454 rbio
= bio_clone(bio
, GFP_NOIO
);
455 rbio
->bi_private
= bio
;
456 rbio
->bi_end_io
= super_written_barrier
;
457 submit_bio(rw
, rbio
);
462 void md_super_wait(mddev_t
*mddev
)
464 /* wait for all superblock writes that were scheduled to complete.
465 * if any had to be retried (due to BARRIER problems), retry them
469 prepare_to_wait(&mddev
->sb_wait
, &wq
, TASK_UNINTERRUPTIBLE
);
470 if (atomic_read(&mddev
->pending_writes
)==0)
472 while (mddev
->biolist
) {
474 spin_lock_irq(&mddev
->write_lock
);
475 bio
= mddev
->biolist
;
476 mddev
->biolist
= bio
->bi_next
;
478 spin_unlock_irq(&mddev
->write_lock
);
479 submit_bio(bio
->bi_rw
, bio
);
483 finish_wait(&mddev
->sb_wait
, &wq
);
486 static int bi_complete(struct bio
*bio
, unsigned int bytes_done
, int error
)
491 complete((struct completion
*)bio
->bi_private
);
495 int sync_page_io(struct block_device
*bdev
, sector_t sector
, int size
,
496 struct page
*page
, int rw
)
498 struct bio
*bio
= bio_alloc(GFP_NOIO
, 1);
499 struct completion event
;
502 rw
|= (1 << BIO_RW_SYNC
);
505 bio
->bi_sector
= sector
;
506 bio_add_page(bio
, page
, size
, 0);
507 init_completion(&event
);
508 bio
->bi_private
= &event
;
509 bio
->bi_end_io
= bi_complete
;
511 wait_for_completion(&event
);
513 ret
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
517 EXPORT_SYMBOL_GPL(sync_page_io
);
519 static int read_disk_sb(mdk_rdev_t
* rdev
, int size
)
521 char b
[BDEVNAME_SIZE
];
522 if (!rdev
->sb_page
) {
530 if (!sync_page_io(rdev
->bdev
, rdev
->sb_offset
<<1, size
, rdev
->sb_page
, READ
))
536 printk(KERN_WARNING
"md: disabled device %s, could not read superblock.\n",
537 bdevname(rdev
->bdev
,b
));
541 static int uuid_equal(mdp_super_t
*sb1
, mdp_super_t
*sb2
)
543 if ( (sb1
->set_uuid0
== sb2
->set_uuid0
) &&
544 (sb1
->set_uuid1
== sb2
->set_uuid1
) &&
545 (sb1
->set_uuid2
== sb2
->set_uuid2
) &&
546 (sb1
->set_uuid3
== sb2
->set_uuid3
))
554 static int sb_equal(mdp_super_t
*sb1
, mdp_super_t
*sb2
)
557 mdp_super_t
*tmp1
, *tmp2
;
559 tmp1
= kmalloc(sizeof(*tmp1
),GFP_KERNEL
);
560 tmp2
= kmalloc(sizeof(*tmp2
),GFP_KERNEL
);
562 if (!tmp1
|| !tmp2
) {
564 printk(KERN_INFO
"md.c: sb1 is not equal to sb2!\n");
572 * nr_disks is not constant
577 if (memcmp(tmp1
, tmp2
, MD_SB_GENERIC_CONSTANT_WORDS
* 4))
588 static unsigned int calc_sb_csum(mdp_super_t
* sb
)
590 unsigned int disk_csum
, csum
;
592 disk_csum
= sb
->sb_csum
;
594 csum
= csum_partial((void *)sb
, MD_SB_BYTES
, 0);
595 sb
->sb_csum
= disk_csum
;
601 * Handle superblock details.
602 * We want to be able to handle multiple superblock formats
603 * so we have a common interface to them all, and an array of
604 * different handlers.
605 * We rely on user-space to write the initial superblock, and support
606 * reading and updating of superblocks.
607 * Interface methods are:
608 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
609 * loads and validates a superblock on dev.
610 * if refdev != NULL, compare superblocks on both devices
612 * 0 - dev has a superblock that is compatible with refdev
613 * 1 - dev has a superblock that is compatible and newer than refdev
614 * so dev should be used as the refdev in future
615 * -EINVAL superblock incompatible or invalid
616 * -othererror e.g. -EIO
618 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
619 * Verify that dev is acceptable into mddev.
620 * The first time, mddev->raid_disks will be 0, and data from
621 * dev should be merged in. Subsequent calls check that dev
622 * is new enough. Return 0 or -EINVAL
624 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
625 * Update the superblock for rdev with data in mddev
626 * This does not write to disc.
632 struct module
*owner
;
633 int (*load_super
)(mdk_rdev_t
*rdev
, mdk_rdev_t
*refdev
, int minor_version
);
634 int (*validate_super
)(mddev_t
*mddev
, mdk_rdev_t
*rdev
);
635 void (*sync_super
)(mddev_t
*mddev
, mdk_rdev_t
*rdev
);
639 * load_super for 0.90.0
641 static int super_90_load(mdk_rdev_t
*rdev
, mdk_rdev_t
*refdev
, int minor_version
)
643 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
649 * Calculate the position of the superblock,
650 * it's at the end of the disk.
652 * It also happens to be a multiple of 4Kb.
654 sb_offset
= calc_dev_sboffset(rdev
->bdev
);
655 rdev
->sb_offset
= sb_offset
;
657 ret
= read_disk_sb(rdev
, MD_SB_BYTES
);
662 bdevname(rdev
->bdev
, b
);
663 sb
= (mdp_super_t
*)page_address(rdev
->sb_page
);
665 if (sb
->md_magic
!= MD_SB_MAGIC
) {
666 printk(KERN_ERR
"md: invalid raid superblock magic on %s\n",
671 if (sb
->major_version
!= 0 ||
672 sb
->minor_version
< 90 ||
673 sb
->minor_version
> 91) {
674 printk(KERN_WARNING
"Bad version number %d.%d on %s\n",
675 sb
->major_version
, sb
->minor_version
,
680 if (sb
->raid_disks
<= 0)
683 if (csum_fold(calc_sb_csum(sb
)) != csum_fold(sb
->sb_csum
)) {
684 printk(KERN_WARNING
"md: invalid superblock checksum on %s\n",
689 rdev
->preferred_minor
= sb
->md_minor
;
690 rdev
->data_offset
= 0;
691 rdev
->sb_size
= MD_SB_BYTES
;
693 if (sb
->level
== LEVEL_MULTIPATH
)
696 rdev
->desc_nr
= sb
->this_disk
.number
;
702 mdp_super_t
*refsb
= (mdp_super_t
*)page_address(refdev
->sb_page
);
703 if (!uuid_equal(refsb
, sb
)) {
704 printk(KERN_WARNING
"md: %s has different UUID to %s\n",
705 b
, bdevname(refdev
->bdev
,b2
));
708 if (!sb_equal(refsb
, sb
)) {
709 printk(KERN_WARNING
"md: %s has same UUID"
710 " but different superblock to %s\n",
711 b
, bdevname(refdev
->bdev
, b2
));
715 ev2
= md_event(refsb
);
721 rdev
->size
= calc_dev_size(rdev
, sb
->chunk_size
);
723 if (rdev
->size
< sb
->size
&& sb
->level
> 1)
724 /* "this cannot possibly happen" ... */
732 * validate_super for 0.90.0
734 static int super_90_validate(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
737 mdp_super_t
*sb
= (mdp_super_t
*)page_address(rdev
->sb_page
);
738 __u64 ev1
= md_event(sb
);
740 rdev
->raid_disk
= -1;
742 if (mddev
->raid_disks
== 0) {
743 mddev
->major_version
= 0;
744 mddev
->minor_version
= sb
->minor_version
;
745 mddev
->patch_version
= sb
->patch_version
;
746 mddev
->persistent
= ! sb
->not_persistent
;
747 mddev
->chunk_size
= sb
->chunk_size
;
748 mddev
->ctime
= sb
->ctime
;
749 mddev
->utime
= sb
->utime
;
750 mddev
->level
= sb
->level
;
751 mddev
->clevel
[0] = 0;
752 mddev
->layout
= sb
->layout
;
753 mddev
->raid_disks
= sb
->raid_disks
;
754 mddev
->size
= sb
->size
;
756 mddev
->bitmap_offset
= 0;
757 mddev
->default_bitmap_offset
= MD_SB_BYTES
>> 9;
759 if (mddev
->minor_version
>= 91) {
760 mddev
->reshape_position
= sb
->reshape_position
;
761 mddev
->delta_disks
= sb
->delta_disks
;
762 mddev
->new_level
= sb
->new_level
;
763 mddev
->new_layout
= sb
->new_layout
;
764 mddev
->new_chunk
= sb
->new_chunk
;
766 mddev
->reshape_position
= MaxSector
;
767 mddev
->delta_disks
= 0;
768 mddev
->new_level
= mddev
->level
;
769 mddev
->new_layout
= mddev
->layout
;
770 mddev
->new_chunk
= mddev
->chunk_size
;
773 if (sb
->state
& (1<<MD_SB_CLEAN
))
774 mddev
->recovery_cp
= MaxSector
;
776 if (sb
->events_hi
== sb
->cp_events_hi
&&
777 sb
->events_lo
== sb
->cp_events_lo
) {
778 mddev
->recovery_cp
= sb
->recovery_cp
;
780 mddev
->recovery_cp
= 0;
783 memcpy(mddev
->uuid
+0, &sb
->set_uuid0
, 4);
784 memcpy(mddev
->uuid
+4, &sb
->set_uuid1
, 4);
785 memcpy(mddev
->uuid
+8, &sb
->set_uuid2
, 4);
786 memcpy(mddev
->uuid
+12,&sb
->set_uuid3
, 4);
788 mddev
->max_disks
= MD_SB_DISKS
;
790 if (sb
->state
& (1<<MD_SB_BITMAP_PRESENT
) &&
791 mddev
->bitmap_file
== NULL
) {
792 if (mddev
->level
!= 1 && mddev
->level
!= 4
793 && mddev
->level
!= 5 && mddev
->level
!= 6
794 && mddev
->level
!= 10) {
795 /* FIXME use a better test */
796 printk(KERN_WARNING
"md: bitmaps not supported for this level.\n");
799 mddev
->bitmap_offset
= mddev
->default_bitmap_offset
;
802 } else if (mddev
->pers
== NULL
) {
803 /* Insist on good event counter while assembling */
805 if (ev1
< mddev
->events
)
807 } else if (mddev
->bitmap
) {
808 /* if adding to array with a bitmap, then we can accept an
809 * older device ... but not too old.
811 if (ev1
< mddev
->bitmap
->events_cleared
)
814 if (ev1
< mddev
->events
)
815 /* just a hot-add of a new device, leave raid_disk at -1 */
819 if (mddev
->level
!= LEVEL_MULTIPATH
) {
820 desc
= sb
->disks
+ rdev
->desc_nr
;
822 if (desc
->state
& (1<<MD_DISK_FAULTY
))
823 set_bit(Faulty
, &rdev
->flags
);
824 else if (desc
->state
& (1<<MD_DISK_SYNC
) /* &&
825 desc->raid_disk < mddev->raid_disks */) {
826 set_bit(In_sync
, &rdev
->flags
);
827 rdev
->raid_disk
= desc
->raid_disk
;
829 if (desc
->state
& (1<<MD_DISK_WRITEMOSTLY
))
830 set_bit(WriteMostly
, &rdev
->flags
);
831 } else /* MULTIPATH are always insync */
832 set_bit(In_sync
, &rdev
->flags
);
837 * sync_super for 0.90.0
839 static void super_90_sync(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
842 struct list_head
*tmp
;
844 int next_spare
= mddev
->raid_disks
;
847 /* make rdev->sb match mddev data..
850 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
851 * 3/ any empty disks < next_spare become removed
853 * disks[0] gets initialised to REMOVED because
854 * we cannot be sure from other fields if it has
855 * been initialised or not.
858 int active
=0, working
=0,failed
=0,spare
=0,nr_disks
=0;
860 rdev
->sb_size
= MD_SB_BYTES
;
862 sb
= (mdp_super_t
*)page_address(rdev
->sb_page
);
864 memset(sb
, 0, sizeof(*sb
));
866 sb
->md_magic
= MD_SB_MAGIC
;
867 sb
->major_version
= mddev
->major_version
;
868 sb
->patch_version
= mddev
->patch_version
;
869 sb
->gvalid_words
= 0; /* ignored */
870 memcpy(&sb
->set_uuid0
, mddev
->uuid
+0, 4);
871 memcpy(&sb
->set_uuid1
, mddev
->uuid
+4, 4);
872 memcpy(&sb
->set_uuid2
, mddev
->uuid
+8, 4);
873 memcpy(&sb
->set_uuid3
, mddev
->uuid
+12,4);
875 sb
->ctime
= mddev
->ctime
;
876 sb
->level
= mddev
->level
;
877 sb
->size
= mddev
->size
;
878 sb
->raid_disks
= mddev
->raid_disks
;
879 sb
->md_minor
= mddev
->md_minor
;
880 sb
->not_persistent
= !mddev
->persistent
;
881 sb
->utime
= mddev
->utime
;
883 sb
->events_hi
= (mddev
->events
>>32);
884 sb
->events_lo
= (u32
)mddev
->events
;
886 if (mddev
->reshape_position
== MaxSector
)
887 sb
->minor_version
= 90;
889 sb
->minor_version
= 91;
890 sb
->reshape_position
= mddev
->reshape_position
;
891 sb
->new_level
= mddev
->new_level
;
892 sb
->delta_disks
= mddev
->delta_disks
;
893 sb
->new_layout
= mddev
->new_layout
;
894 sb
->new_chunk
= mddev
->new_chunk
;
896 mddev
->minor_version
= sb
->minor_version
;
899 sb
->recovery_cp
= mddev
->recovery_cp
;
900 sb
->cp_events_hi
= (mddev
->events
>>32);
901 sb
->cp_events_lo
= (u32
)mddev
->events
;
902 if (mddev
->recovery_cp
== MaxSector
)
903 sb
->state
= (1<< MD_SB_CLEAN
);
907 sb
->layout
= mddev
->layout
;
908 sb
->chunk_size
= mddev
->chunk_size
;
910 if (mddev
->bitmap
&& mddev
->bitmap_file
== NULL
)
911 sb
->state
|= (1<<MD_SB_BITMAP_PRESENT
);
913 sb
->disks
[0].state
= (1<<MD_DISK_REMOVED
);
914 ITERATE_RDEV(mddev
,rdev2
,tmp
) {
917 if (rdev2
->raid_disk
>= 0 && test_bit(In_sync
, &rdev2
->flags
)
918 && !test_bit(Faulty
, &rdev2
->flags
))
919 desc_nr
= rdev2
->raid_disk
;
921 desc_nr
= next_spare
++;
922 rdev2
->desc_nr
= desc_nr
;
923 d
= &sb
->disks
[rdev2
->desc_nr
];
925 d
->number
= rdev2
->desc_nr
;
926 d
->major
= MAJOR(rdev2
->bdev
->bd_dev
);
927 d
->minor
= MINOR(rdev2
->bdev
->bd_dev
);
928 if (rdev2
->raid_disk
>= 0 && test_bit(In_sync
, &rdev2
->flags
)
929 && !test_bit(Faulty
, &rdev2
->flags
))
930 d
->raid_disk
= rdev2
->raid_disk
;
932 d
->raid_disk
= rdev2
->desc_nr
; /* compatibility */
933 if (test_bit(Faulty
, &rdev2
->flags
))
934 d
->state
= (1<<MD_DISK_FAULTY
);
935 else if (test_bit(In_sync
, &rdev2
->flags
)) {
936 d
->state
= (1<<MD_DISK_ACTIVE
);
937 d
->state
|= (1<<MD_DISK_SYNC
);
945 if (test_bit(WriteMostly
, &rdev2
->flags
))
946 d
->state
|= (1<<MD_DISK_WRITEMOSTLY
);
948 /* now set the "removed" and "faulty" bits on any missing devices */
949 for (i
=0 ; i
< mddev
->raid_disks
; i
++) {
950 mdp_disk_t
*d
= &sb
->disks
[i
];
951 if (d
->state
== 0 && d
->number
== 0) {
954 d
->state
= (1<<MD_DISK_REMOVED
);
955 d
->state
|= (1<<MD_DISK_FAULTY
);
959 sb
->nr_disks
= nr_disks
;
960 sb
->active_disks
= active
;
961 sb
->working_disks
= working
;
962 sb
->failed_disks
= failed
;
963 sb
->spare_disks
= spare
;
965 sb
->this_disk
= sb
->disks
[rdev
->desc_nr
];
966 sb
->sb_csum
= calc_sb_csum(sb
);
970 * version 1 superblock
973 static unsigned int calc_sb_1_csum(struct mdp_superblock_1
* sb
)
975 unsigned int disk_csum
, csum
;
976 unsigned long long newcsum
;
977 int size
= 256 + le32_to_cpu(sb
->max_dev
)*2;
978 unsigned int *isuper
= (unsigned int*)sb
;
981 disk_csum
= sb
->sb_csum
;
984 for (i
=0; size
>=4; size
-= 4 )
985 newcsum
+= le32_to_cpu(*isuper
++);
988 newcsum
+= le16_to_cpu(*(unsigned short*) isuper
);
990 csum
= (newcsum
& 0xffffffff) + (newcsum
>> 32);
991 sb
->sb_csum
= disk_csum
;
992 return cpu_to_le32(csum
);
995 static int super_1_load(mdk_rdev_t
*rdev
, mdk_rdev_t
*refdev
, int minor_version
)
997 struct mdp_superblock_1
*sb
;
1000 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
1004 * Calculate the position of the superblock.
1005 * It is always aligned to a 4K boundary and
1006 * depeding on minor_version, it can be:
1007 * 0: At least 8K, but less than 12K, from end of device
1008 * 1: At start of device
1009 * 2: 4K from start of device.
1011 switch(minor_version
) {
1013 sb_offset
= rdev
->bdev
->bd_inode
->i_size
>> 9;
1015 sb_offset
&= ~(sector_t
)(4*2-1);
1016 /* convert from sectors to K */
1028 rdev
->sb_offset
= sb_offset
;
1030 /* superblock is rarely larger than 1K, but it can be larger,
1031 * and it is safe to read 4k, so we do that
1033 ret
= read_disk_sb(rdev
, 4096);
1034 if (ret
) return ret
;
1037 sb
= (struct mdp_superblock_1
*)page_address(rdev
->sb_page
);
1039 if (sb
->magic
!= cpu_to_le32(MD_SB_MAGIC
) ||
1040 sb
->major_version
!= cpu_to_le32(1) ||
1041 le32_to_cpu(sb
->max_dev
) > (4096-256)/2 ||
1042 le64_to_cpu(sb
->super_offset
) != (rdev
->sb_offset
<<1) ||
1043 (le32_to_cpu(sb
->feature_map
) & ~MD_FEATURE_ALL
) != 0)
1046 if (calc_sb_1_csum(sb
) != sb
->sb_csum
) {
1047 printk("md: invalid superblock checksum on %s\n",
1048 bdevname(rdev
->bdev
,b
));
1051 if (le64_to_cpu(sb
->data_size
) < 10) {
1052 printk("md: data_size too small on %s\n",
1053 bdevname(rdev
->bdev
,b
));
1056 rdev
->preferred_minor
= 0xffff;
1057 rdev
->data_offset
= le64_to_cpu(sb
->data_offset
);
1058 atomic_set(&rdev
->corrected_errors
, le32_to_cpu(sb
->cnt_corrected_read
));
1060 rdev
->sb_size
= le32_to_cpu(sb
->max_dev
) * 2 + 256;
1061 bmask
= queue_hardsect_size(rdev
->bdev
->bd_disk
->queue
)-1;
1062 if (rdev
->sb_size
& bmask
)
1063 rdev
-> sb_size
= (rdev
->sb_size
| bmask
)+1;
1069 struct mdp_superblock_1
*refsb
=
1070 (struct mdp_superblock_1
*)page_address(refdev
->sb_page
);
1072 if (memcmp(sb
->set_uuid
, refsb
->set_uuid
, 16) != 0 ||
1073 sb
->level
!= refsb
->level
||
1074 sb
->layout
!= refsb
->layout
||
1075 sb
->chunksize
!= refsb
->chunksize
) {
1076 printk(KERN_WARNING
"md: %s has strangely different"
1077 " superblock to %s\n",
1078 bdevname(rdev
->bdev
,b
),
1079 bdevname(refdev
->bdev
,b2
));
1082 ev1
= le64_to_cpu(sb
->events
);
1083 ev2
= le64_to_cpu(refsb
->events
);
1091 rdev
->size
= ((rdev
->bdev
->bd_inode
->i_size
>>9) - le64_to_cpu(sb
->data_offset
)) / 2;
1093 rdev
->size
= rdev
->sb_offset
;
1094 if (rdev
->size
< le64_to_cpu(sb
->data_size
)/2)
1096 rdev
->size
= le64_to_cpu(sb
->data_size
)/2;
1097 if (le32_to_cpu(sb
->chunksize
))
1098 rdev
->size
&= ~((sector_t
)le32_to_cpu(sb
->chunksize
)/2 - 1);
1100 if (le32_to_cpu(sb
->size
) > rdev
->size
*2)
1105 static int super_1_validate(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1107 struct mdp_superblock_1
*sb
= (struct mdp_superblock_1
*)page_address(rdev
->sb_page
);
1108 __u64 ev1
= le64_to_cpu(sb
->events
);
1110 rdev
->raid_disk
= -1;
1112 if (mddev
->raid_disks
== 0) {
1113 mddev
->major_version
= 1;
1114 mddev
->patch_version
= 0;
1115 mddev
->persistent
= 1;
1116 mddev
->chunk_size
= le32_to_cpu(sb
->chunksize
) << 9;
1117 mddev
->ctime
= le64_to_cpu(sb
->ctime
) & ((1ULL << 32)-1);
1118 mddev
->utime
= le64_to_cpu(sb
->utime
) & ((1ULL << 32)-1);
1119 mddev
->level
= le32_to_cpu(sb
->level
);
1120 mddev
->clevel
[0] = 0;
1121 mddev
->layout
= le32_to_cpu(sb
->layout
);
1122 mddev
->raid_disks
= le32_to_cpu(sb
->raid_disks
);
1123 mddev
->size
= le64_to_cpu(sb
->size
)/2;
1124 mddev
->events
= ev1
;
1125 mddev
->bitmap_offset
= 0;
1126 mddev
->default_bitmap_offset
= 1024 >> 9;
1128 mddev
->recovery_cp
= le64_to_cpu(sb
->resync_offset
);
1129 memcpy(mddev
->uuid
, sb
->set_uuid
, 16);
1131 mddev
->max_disks
= (4096-256)/2;
1133 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_BITMAP_OFFSET
) &&
1134 mddev
->bitmap_file
== NULL
) {
1135 if (mddev
->level
!= 1 && mddev
->level
!= 5 && mddev
->level
!= 6
1136 && mddev
->level
!= 10) {
1137 printk(KERN_WARNING
"md: bitmaps not supported for this level.\n");
1140 mddev
->bitmap_offset
= (__s32
)le32_to_cpu(sb
->bitmap_offset
);
1142 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RESHAPE_ACTIVE
)) {
1143 mddev
->reshape_position
= le64_to_cpu(sb
->reshape_position
);
1144 mddev
->delta_disks
= le32_to_cpu(sb
->delta_disks
);
1145 mddev
->new_level
= le32_to_cpu(sb
->new_level
);
1146 mddev
->new_layout
= le32_to_cpu(sb
->new_layout
);
1147 mddev
->new_chunk
= le32_to_cpu(sb
->new_chunk
)<<9;
1149 mddev
->reshape_position
= MaxSector
;
1150 mddev
->delta_disks
= 0;
1151 mddev
->new_level
= mddev
->level
;
1152 mddev
->new_layout
= mddev
->layout
;
1153 mddev
->new_chunk
= mddev
->chunk_size
;
1156 } else if (mddev
->pers
== NULL
) {
1157 /* Insist of good event counter while assembling */
1159 if (ev1
< mddev
->events
)
1161 } else if (mddev
->bitmap
) {
1162 /* If adding to array with a bitmap, then we can accept an
1163 * older device, but not too old.
1165 if (ev1
< mddev
->bitmap
->events_cleared
)
1168 if (ev1
< mddev
->events
)
1169 /* just a hot-add of a new device, leave raid_disk at -1 */
1172 if (mddev
->level
!= LEVEL_MULTIPATH
) {
1174 rdev
->desc_nr
= le32_to_cpu(sb
->dev_number
);
1175 role
= le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]);
1177 case 0xffff: /* spare */
1179 case 0xfffe: /* faulty */
1180 set_bit(Faulty
, &rdev
->flags
);
1183 if ((le32_to_cpu(sb
->feature_map
) &
1184 MD_FEATURE_RECOVERY_OFFSET
))
1185 rdev
->recovery_offset
= le64_to_cpu(sb
->recovery_offset
);
1187 set_bit(In_sync
, &rdev
->flags
);
1188 rdev
->raid_disk
= role
;
1191 if (sb
->devflags
& WriteMostly1
)
1192 set_bit(WriteMostly
, &rdev
->flags
);
1193 } else /* MULTIPATH are always insync */
1194 set_bit(In_sync
, &rdev
->flags
);
1199 static void super_1_sync(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1201 struct mdp_superblock_1
*sb
;
1202 struct list_head
*tmp
;
1205 /* make rdev->sb match mddev and rdev data. */
1207 sb
= (struct mdp_superblock_1
*)page_address(rdev
->sb_page
);
1209 sb
->feature_map
= 0;
1211 sb
->recovery_offset
= cpu_to_le64(0);
1212 memset(sb
->pad1
, 0, sizeof(sb
->pad1
));
1213 memset(sb
->pad2
, 0, sizeof(sb
->pad2
));
1214 memset(sb
->pad3
, 0, sizeof(sb
->pad3
));
1216 sb
->utime
= cpu_to_le64((__u64
)mddev
->utime
);
1217 sb
->events
= cpu_to_le64(mddev
->events
);
1219 sb
->resync_offset
= cpu_to_le64(mddev
->recovery_cp
);
1221 sb
->resync_offset
= cpu_to_le64(0);
1223 sb
->cnt_corrected_read
= atomic_read(&rdev
->corrected_errors
);
1225 sb
->raid_disks
= cpu_to_le32(mddev
->raid_disks
);
1226 sb
->size
= cpu_to_le64(mddev
->size
<<1);
1228 if (mddev
->bitmap
&& mddev
->bitmap_file
== NULL
) {
1229 sb
->bitmap_offset
= cpu_to_le32((__u32
)mddev
->bitmap_offset
);
1230 sb
->feature_map
= cpu_to_le32(MD_FEATURE_BITMAP_OFFSET
);
1233 if (rdev
->raid_disk
>= 0 &&
1234 !test_bit(In_sync
, &rdev
->flags
) &&
1235 rdev
->recovery_offset
> 0) {
1236 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET
);
1237 sb
->recovery_offset
= cpu_to_le64(rdev
->recovery_offset
);
1240 if (mddev
->reshape_position
!= MaxSector
) {
1241 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE
);
1242 sb
->reshape_position
= cpu_to_le64(mddev
->reshape_position
);
1243 sb
->new_layout
= cpu_to_le32(mddev
->new_layout
);
1244 sb
->delta_disks
= cpu_to_le32(mddev
->delta_disks
);
1245 sb
->new_level
= cpu_to_le32(mddev
->new_level
);
1246 sb
->new_chunk
= cpu_to_le32(mddev
->new_chunk
>>9);
1250 ITERATE_RDEV(mddev
,rdev2
,tmp
)
1251 if (rdev2
->desc_nr
+1 > max_dev
)
1252 max_dev
= rdev2
->desc_nr
+1;
1254 sb
->max_dev
= cpu_to_le32(max_dev
);
1255 for (i
=0; i
<max_dev
;i
++)
1256 sb
->dev_roles
[i
] = cpu_to_le16(0xfffe);
1258 ITERATE_RDEV(mddev
,rdev2
,tmp
) {
1260 if (test_bit(Faulty
, &rdev2
->flags
))
1261 sb
->dev_roles
[i
] = cpu_to_le16(0xfffe);
1262 else if (test_bit(In_sync
, &rdev2
->flags
))
1263 sb
->dev_roles
[i
] = cpu_to_le16(rdev2
->raid_disk
);
1264 else if (rdev2
->raid_disk
>= 0 && rdev2
->recovery_offset
> 0)
1265 sb
->dev_roles
[i
] = cpu_to_le16(rdev2
->raid_disk
);
1267 sb
->dev_roles
[i
] = cpu_to_le16(0xffff);
1270 sb
->sb_csum
= calc_sb_1_csum(sb
);
1274 static struct super_type super_types
[] = {
1277 .owner
= THIS_MODULE
,
1278 .load_super
= super_90_load
,
1279 .validate_super
= super_90_validate
,
1280 .sync_super
= super_90_sync
,
1284 .owner
= THIS_MODULE
,
1285 .load_super
= super_1_load
,
1286 .validate_super
= super_1_validate
,
1287 .sync_super
= super_1_sync
,
1291 static mdk_rdev_t
* match_dev_unit(mddev_t
*mddev
, mdk_rdev_t
*dev
)
1293 struct list_head
*tmp
;
1296 ITERATE_RDEV(mddev
,rdev
,tmp
)
1297 if (rdev
->bdev
->bd_contains
== dev
->bdev
->bd_contains
)
1303 static int match_mddev_units(mddev_t
*mddev1
, mddev_t
*mddev2
)
1305 struct list_head
*tmp
;
1308 ITERATE_RDEV(mddev1
,rdev
,tmp
)
1309 if (match_dev_unit(mddev2
, rdev
))
1315 static LIST_HEAD(pending_raid_disks
);
1317 static int bind_rdev_to_array(mdk_rdev_t
* rdev
, mddev_t
* mddev
)
1319 mdk_rdev_t
*same_pdev
;
1320 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
1328 /* make sure rdev->size exceeds mddev->size */
1329 if (rdev
->size
&& (mddev
->size
== 0 || rdev
->size
< mddev
->size
)) {
1331 /* Cannot change size, so fail */
1334 mddev
->size
= rdev
->size
;
1336 same_pdev
= match_dev_unit(mddev
, rdev
);
1339 "%s: WARNING: %s appears to be on the same physical"
1340 " disk as %s. True\n protection against single-disk"
1341 " failure might be compromised.\n",
1342 mdname(mddev
), bdevname(rdev
->bdev
,b
),
1343 bdevname(same_pdev
->bdev
,b2
));
1345 /* Verify rdev->desc_nr is unique.
1346 * If it is -1, assign a free number, else
1347 * check number is not in use
1349 if (rdev
->desc_nr
< 0) {
1351 if (mddev
->pers
) choice
= mddev
->raid_disks
;
1352 while (find_rdev_nr(mddev
, choice
))
1354 rdev
->desc_nr
= choice
;
1356 if (find_rdev_nr(mddev
, rdev
->desc_nr
))
1359 bdevname(rdev
->bdev
,b
);
1360 if (kobject_set_name(&rdev
->kobj
, "dev-%s", b
) < 0)
1362 while ( (s
=strchr(rdev
->kobj
.k_name
, '/')) != NULL
)
1365 list_add(&rdev
->same_set
, &mddev
->disks
);
1366 rdev
->mddev
= mddev
;
1367 printk(KERN_INFO
"md: bind<%s>\n", b
);
1369 rdev
->kobj
.parent
= &mddev
->kobj
;
1370 kobject_add(&rdev
->kobj
);
1372 if (rdev
->bdev
->bd_part
)
1373 ko
= &rdev
->bdev
->bd_part
->kobj
;
1375 ko
= &rdev
->bdev
->bd_disk
->kobj
;
1376 sysfs_create_link(&rdev
->kobj
, ko
, "block");
1377 bd_claim_by_disk(rdev
->bdev
, rdev
, mddev
->gendisk
);
1381 static void unbind_rdev_from_array(mdk_rdev_t
* rdev
)
1383 char b
[BDEVNAME_SIZE
];
1388 bd_release_from_disk(rdev
->bdev
, rdev
->mddev
->gendisk
);
1389 list_del_init(&rdev
->same_set
);
1390 printk(KERN_INFO
"md: unbind<%s>\n", bdevname(rdev
->bdev
,b
));
1392 sysfs_remove_link(&rdev
->kobj
, "block");
1393 kobject_del(&rdev
->kobj
);
1397 * prevent the device from being mounted, repartitioned or
1398 * otherwise reused by a RAID array (or any other kernel
1399 * subsystem), by bd_claiming the device.
1401 static int lock_rdev(mdk_rdev_t
*rdev
, dev_t dev
)
1404 struct block_device
*bdev
;
1405 char b
[BDEVNAME_SIZE
];
1407 bdev
= open_partition_by_devnum(dev
, FMODE_READ
|FMODE_WRITE
);
1409 printk(KERN_ERR
"md: could not open %s.\n",
1410 __bdevname(dev
, b
));
1411 return PTR_ERR(bdev
);
1413 err
= bd_claim(bdev
, rdev
);
1415 printk(KERN_ERR
"md: could not bd_claim %s.\n",
1417 blkdev_put_partition(bdev
);
1424 static void unlock_rdev(mdk_rdev_t
*rdev
)
1426 struct block_device
*bdev
= rdev
->bdev
;
1431 blkdev_put_partition(bdev
);
1434 void md_autodetect_dev(dev_t dev
);
1436 static void export_rdev(mdk_rdev_t
* rdev
)
1438 char b
[BDEVNAME_SIZE
];
1439 printk(KERN_INFO
"md: export_rdev(%s)\n",
1440 bdevname(rdev
->bdev
,b
));
1444 list_del_init(&rdev
->same_set
);
1446 md_autodetect_dev(rdev
->bdev
->bd_dev
);
1449 kobject_put(&rdev
->kobj
);
1452 static void kick_rdev_from_array(mdk_rdev_t
* rdev
)
1454 unbind_rdev_from_array(rdev
);
1458 static void export_array(mddev_t
*mddev
)
1460 struct list_head
*tmp
;
1463 ITERATE_RDEV(mddev
,rdev
,tmp
) {
1468 kick_rdev_from_array(rdev
);
1470 if (!list_empty(&mddev
->disks
))
1472 mddev
->raid_disks
= 0;
1473 mddev
->major_version
= 0;
1476 static void print_desc(mdp_disk_t
*desc
)
1478 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc
->number
,
1479 desc
->major
,desc
->minor
,desc
->raid_disk
,desc
->state
);
1482 static void print_sb(mdp_super_t
*sb
)
1487 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1488 sb
->major_version
, sb
->minor_version
, sb
->patch_version
,
1489 sb
->set_uuid0
, sb
->set_uuid1
, sb
->set_uuid2
, sb
->set_uuid3
,
1491 printk(KERN_INFO
"md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1492 sb
->level
, sb
->size
, sb
->nr_disks
, sb
->raid_disks
,
1493 sb
->md_minor
, sb
->layout
, sb
->chunk_size
);
1494 printk(KERN_INFO
"md: UT:%08x ST:%d AD:%d WD:%d"
1495 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1496 sb
->utime
, sb
->state
, sb
->active_disks
, sb
->working_disks
,
1497 sb
->failed_disks
, sb
->spare_disks
,
1498 sb
->sb_csum
, (unsigned long)sb
->events_lo
);
1501 for (i
= 0; i
< MD_SB_DISKS
; i
++) {
1504 desc
= sb
->disks
+ i
;
1505 if (desc
->number
|| desc
->major
|| desc
->minor
||
1506 desc
->raid_disk
|| (desc
->state
&& (desc
->state
!= 4))) {
1507 printk(" D %2d: ", i
);
1511 printk(KERN_INFO
"md: THIS: ");
1512 print_desc(&sb
->this_disk
);
1516 static void print_rdev(mdk_rdev_t
*rdev
)
1518 char b
[BDEVNAME_SIZE
];
1519 printk(KERN_INFO
"md: rdev %s, SZ:%08llu F:%d S:%d DN:%u\n",
1520 bdevname(rdev
->bdev
,b
), (unsigned long long)rdev
->size
,
1521 test_bit(Faulty
, &rdev
->flags
), test_bit(In_sync
, &rdev
->flags
),
1523 if (rdev
->sb_loaded
) {
1524 printk(KERN_INFO
"md: rdev superblock:\n");
1525 print_sb((mdp_super_t
*)page_address(rdev
->sb_page
));
1527 printk(KERN_INFO
"md: no rdev superblock!\n");
1530 static void md_print_devices(void)
1532 struct list_head
*tmp
, *tmp2
;
1535 char b
[BDEVNAME_SIZE
];
1538 printk("md: **********************************\n");
1539 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
1540 printk("md: **********************************\n");
1541 ITERATE_MDDEV(mddev
,tmp
) {
1544 bitmap_print_sb(mddev
->bitmap
);
1546 printk("%s: ", mdname(mddev
));
1547 ITERATE_RDEV(mddev
,rdev
,tmp2
)
1548 printk("<%s>", bdevname(rdev
->bdev
,b
));
1551 ITERATE_RDEV(mddev
,rdev
,tmp2
)
1554 printk("md: **********************************\n");
1559 static void sync_sbs(mddev_t
* mddev
, int nospares
)
1561 /* Update each superblock (in-memory image), but
1562 * if we are allowed to, skip spares which already
1563 * have the right event counter, or have one earlier
1564 * (which would mean they aren't being marked as dirty
1565 * with the rest of the array)
1568 struct list_head
*tmp
;
1570 ITERATE_RDEV(mddev
,rdev
,tmp
) {
1571 if (rdev
->sb_events
== mddev
->events
||
1573 rdev
->raid_disk
< 0 &&
1574 (rdev
->sb_events
&1)==0 &&
1575 rdev
->sb_events
+1 == mddev
->events
)) {
1576 /* Don't update this superblock */
1577 rdev
->sb_loaded
= 2;
1579 super_types
[mddev
->major_version
].
1580 sync_super(mddev
, rdev
);
1581 rdev
->sb_loaded
= 1;
1586 void md_update_sb(mddev_t
* mddev
)
1589 struct list_head
*tmp
;
1595 spin_lock_irq(&mddev
->write_lock
);
1596 sync_req
= mddev
->in_sync
;
1597 mddev
->utime
= get_seconds();
1598 if (mddev
->sb_dirty
== 3)
1599 /* just a clean<-> dirty transition, possibly leave spares alone,
1600 * though if events isn't the right even/odd, we will have to do
1605 /* If this is just a dirty<->clean transition, and the array is clean
1606 * and 'events' is odd, we can roll back to the previous clean state */
1607 if (mddev
->sb_dirty
== 3
1608 && (mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
)
1609 && (mddev
->events
& 1))
1612 /* otherwise we have to go forward and ... */
1614 if (!mddev
->in_sync
|| mddev
->recovery_cp
!= MaxSector
) { /* not clean */
1615 /* .. if the array isn't clean, insist on an odd 'events' */
1616 if ((mddev
->events
&1)==0) {
1621 /* otherwise insist on an even 'events' (for clean states) */
1622 if ((mddev
->events
&1)) {
1629 if (!mddev
->events
) {
1631 * oops, this 64-bit counter should never wrap.
1632 * Either we are in around ~1 trillion A.C., assuming
1633 * 1 reboot per second, or we have a bug:
1638 mddev
->sb_dirty
= 2;
1639 sync_sbs(mddev
, nospares
);
1642 * do not write anything to disk if using
1643 * nonpersistent superblocks
1645 if (!mddev
->persistent
) {
1646 mddev
->sb_dirty
= 0;
1647 spin_unlock_irq(&mddev
->write_lock
);
1648 wake_up(&mddev
->sb_wait
);
1651 spin_unlock_irq(&mddev
->write_lock
);
1654 "md: updating %s RAID superblock on device (in sync %d)\n",
1655 mdname(mddev
),mddev
->in_sync
);
1657 err
= bitmap_update_sb(mddev
->bitmap
);
1658 ITERATE_RDEV(mddev
,rdev
,tmp
) {
1659 char b
[BDEVNAME_SIZE
];
1660 dprintk(KERN_INFO
"md: ");
1661 if (rdev
->sb_loaded
!= 1)
1662 continue; /* no noise on spare devices */
1663 if (test_bit(Faulty
, &rdev
->flags
))
1664 dprintk("(skipping faulty ");
1666 dprintk("%s ", bdevname(rdev
->bdev
,b
));
1667 if (!test_bit(Faulty
, &rdev
->flags
)) {
1668 md_super_write(mddev
,rdev
,
1669 rdev
->sb_offset
<<1, rdev
->sb_size
,
1671 dprintk(KERN_INFO
"(write) %s's sb offset: %llu\n",
1672 bdevname(rdev
->bdev
,b
),
1673 (unsigned long long)rdev
->sb_offset
);
1674 rdev
->sb_events
= mddev
->events
;
1678 if (mddev
->level
== LEVEL_MULTIPATH
)
1679 /* only need to write one superblock... */
1682 md_super_wait(mddev
);
1683 /* if there was a failure, sb_dirty was set to 1, and we re-write super */
1685 spin_lock_irq(&mddev
->write_lock
);
1686 if (mddev
->in_sync
!= sync_req
|| mddev
->sb_dirty
== 1) {
1687 /* have to write it out again */
1688 spin_unlock_irq(&mddev
->write_lock
);
1691 mddev
->sb_dirty
= 0;
1692 spin_unlock_irq(&mddev
->write_lock
);
1693 wake_up(&mddev
->sb_wait
);
1696 EXPORT_SYMBOL_GPL(md_update_sb
);
1698 /* words written to sysfs files may, or my not, be \n terminated.
1699 * We want to accept with case. For this we use cmd_match.
1701 static int cmd_match(const char *cmd
, const char *str
)
1703 /* See if cmd, written into a sysfs file, matches
1704 * str. They must either be the same, or cmd can
1705 * have a trailing newline
1707 while (*cmd
&& *str
&& *cmd
== *str
) {
1718 struct rdev_sysfs_entry
{
1719 struct attribute attr
;
1720 ssize_t (*show
)(mdk_rdev_t
*, char *);
1721 ssize_t (*store
)(mdk_rdev_t
*, const char *, size_t);
1725 state_show(mdk_rdev_t
*rdev
, char *page
)
1730 if (test_bit(Faulty
, &rdev
->flags
)) {
1731 len
+= sprintf(page
+len
, "%sfaulty",sep
);
1734 if (test_bit(In_sync
, &rdev
->flags
)) {
1735 len
+= sprintf(page
+len
, "%sin_sync",sep
);
1738 if (test_bit(WriteMostly
, &rdev
->flags
)) {
1739 len
+= sprintf(page
+len
, "%swrite_mostly",sep
);
1742 if (!test_bit(Faulty
, &rdev
->flags
) &&
1743 !test_bit(In_sync
, &rdev
->flags
)) {
1744 len
+= sprintf(page
+len
, "%sspare", sep
);
1747 return len
+sprintf(page
+len
, "\n");
1751 state_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
1754 * faulty - simulates and error
1755 * remove - disconnects the device
1756 * writemostly - sets write_mostly
1757 * -writemostly - clears write_mostly
1760 if (cmd_match(buf
, "faulty") && rdev
->mddev
->pers
) {
1761 md_error(rdev
->mddev
, rdev
);
1763 } else if (cmd_match(buf
, "remove")) {
1764 if (rdev
->raid_disk
>= 0)
1767 mddev_t
*mddev
= rdev
->mddev
;
1768 kick_rdev_from_array(rdev
);
1769 md_update_sb(mddev
);
1770 md_new_event(mddev
);
1773 } else if (cmd_match(buf
, "writemostly")) {
1774 set_bit(WriteMostly
, &rdev
->flags
);
1776 } else if (cmd_match(buf
, "-writemostly")) {
1777 clear_bit(WriteMostly
, &rdev
->flags
);
1780 return err
? err
: len
;
1782 static struct rdev_sysfs_entry
1783 rdev_state
= __ATTR(state
, 0644, state_show
, state_store
);
1786 super_show(mdk_rdev_t
*rdev
, char *page
)
1788 if (rdev
->sb_loaded
&& rdev
->sb_size
) {
1789 memcpy(page
, page_address(rdev
->sb_page
), rdev
->sb_size
);
1790 return rdev
->sb_size
;
1794 static struct rdev_sysfs_entry rdev_super
= __ATTR_RO(super
);
1797 errors_show(mdk_rdev_t
*rdev
, char *page
)
1799 return sprintf(page
, "%d\n", atomic_read(&rdev
->corrected_errors
));
1803 errors_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
1806 unsigned long n
= simple_strtoul(buf
, &e
, 10);
1807 if (*buf
&& (*e
== 0 || *e
== '\n')) {
1808 atomic_set(&rdev
->corrected_errors
, n
);
1813 static struct rdev_sysfs_entry rdev_errors
=
1814 __ATTR(errors
, 0644, errors_show
, errors_store
);
1817 slot_show(mdk_rdev_t
*rdev
, char *page
)
1819 if (rdev
->raid_disk
< 0)
1820 return sprintf(page
, "none\n");
1822 return sprintf(page
, "%d\n", rdev
->raid_disk
);
1826 slot_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
1829 int slot
= simple_strtoul(buf
, &e
, 10);
1830 if (strncmp(buf
, "none", 4)==0)
1832 else if (e
==buf
|| (*e
&& *e
!= '\n'))
1834 if (rdev
->mddev
->pers
)
1835 /* Cannot set slot in active array (yet) */
1837 if (slot
>= rdev
->mddev
->raid_disks
)
1839 rdev
->raid_disk
= slot
;
1840 /* assume it is working */
1842 set_bit(In_sync
, &rdev
->flags
);
1847 static struct rdev_sysfs_entry rdev_slot
=
1848 __ATTR(slot
, 0644, slot_show
, slot_store
);
1851 offset_show(mdk_rdev_t
*rdev
, char *page
)
1853 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->data_offset
);
1857 offset_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
1860 unsigned long long offset
= simple_strtoull(buf
, &e
, 10);
1861 if (e
==buf
|| (*e
&& *e
!= '\n'))
1863 if (rdev
->mddev
->pers
)
1865 rdev
->data_offset
= offset
;
1869 static struct rdev_sysfs_entry rdev_offset
=
1870 __ATTR(offset
, 0644, offset_show
, offset_store
);
1873 rdev_size_show(mdk_rdev_t
*rdev
, char *page
)
1875 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->size
);
1879 rdev_size_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
1882 unsigned long long size
= simple_strtoull(buf
, &e
, 10);
1883 if (e
==buf
|| (*e
&& *e
!= '\n'))
1885 if (rdev
->mddev
->pers
)
1888 if (size
< rdev
->mddev
->size
|| rdev
->mddev
->size
== 0)
1889 rdev
->mddev
->size
= size
;
1893 static struct rdev_sysfs_entry rdev_size
=
1894 __ATTR(size
, 0644, rdev_size_show
, rdev_size_store
);
1896 static struct attribute
*rdev_default_attrs
[] = {
1906 rdev_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
1908 struct rdev_sysfs_entry
*entry
= container_of(attr
, struct rdev_sysfs_entry
, attr
);
1909 mdk_rdev_t
*rdev
= container_of(kobj
, mdk_rdev_t
, kobj
);
1913 return entry
->show(rdev
, page
);
1917 rdev_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
1918 const char *page
, size_t length
)
1920 struct rdev_sysfs_entry
*entry
= container_of(attr
, struct rdev_sysfs_entry
, attr
);
1921 mdk_rdev_t
*rdev
= container_of(kobj
, mdk_rdev_t
, kobj
);
1925 return entry
->store(rdev
, page
, length
);
1928 static void rdev_free(struct kobject
*ko
)
1930 mdk_rdev_t
*rdev
= container_of(ko
, mdk_rdev_t
, kobj
);
1933 static struct sysfs_ops rdev_sysfs_ops
= {
1934 .show
= rdev_attr_show
,
1935 .store
= rdev_attr_store
,
1937 static struct kobj_type rdev_ktype
= {
1938 .release
= rdev_free
,
1939 .sysfs_ops
= &rdev_sysfs_ops
,
1940 .default_attrs
= rdev_default_attrs
,
1944 * Import a device. If 'super_format' >= 0, then sanity check the superblock
1946 * mark the device faulty if:
1948 * - the device is nonexistent (zero size)
1949 * - the device has no valid superblock
1951 * a faulty rdev _never_ has rdev->sb set.
1953 static mdk_rdev_t
*md_import_device(dev_t newdev
, int super_format
, int super_minor
)
1955 char b
[BDEVNAME_SIZE
];
1960 rdev
= kzalloc(sizeof(*rdev
), GFP_KERNEL
);
1962 printk(KERN_ERR
"md: could not alloc mem for new device!\n");
1963 return ERR_PTR(-ENOMEM
);
1966 if ((err
= alloc_disk_sb(rdev
)))
1969 err
= lock_rdev(rdev
, newdev
);
1973 rdev
->kobj
.parent
= NULL
;
1974 rdev
->kobj
.ktype
= &rdev_ktype
;
1975 kobject_init(&rdev
->kobj
);
1979 rdev
->data_offset
= 0;
1980 rdev
->sb_events
= 0;
1981 atomic_set(&rdev
->nr_pending
, 0);
1982 atomic_set(&rdev
->read_errors
, 0);
1983 atomic_set(&rdev
->corrected_errors
, 0);
1985 size
= rdev
->bdev
->bd_inode
->i_size
>> BLOCK_SIZE_BITS
;
1988 "md: %s has zero or unknown size, marking faulty!\n",
1989 bdevname(rdev
->bdev
,b
));
1994 if (super_format
>= 0) {
1995 err
= super_types
[super_format
].
1996 load_super(rdev
, NULL
, super_minor
);
1997 if (err
== -EINVAL
) {
1999 "md: %s has invalid sb, not importing!\n",
2000 bdevname(rdev
->bdev
,b
));
2005 "md: could not read %s's sb, not importing!\n",
2006 bdevname(rdev
->bdev
,b
));
2010 INIT_LIST_HEAD(&rdev
->same_set
);
2015 if (rdev
->sb_page
) {
2021 return ERR_PTR(err
);
2025 * Check a full RAID array for plausibility
2029 static void analyze_sbs(mddev_t
* mddev
)
2032 struct list_head
*tmp
;
2033 mdk_rdev_t
*rdev
, *freshest
;
2034 char b
[BDEVNAME_SIZE
];
2037 ITERATE_RDEV(mddev
,rdev
,tmp
)
2038 switch (super_types
[mddev
->major_version
].
2039 load_super(rdev
, freshest
, mddev
->minor_version
)) {
2047 "md: fatal superblock inconsistency in %s"
2048 " -- removing from array\n",
2049 bdevname(rdev
->bdev
,b
));
2050 kick_rdev_from_array(rdev
);
2054 super_types
[mddev
->major_version
].
2055 validate_super(mddev
, freshest
);
2058 ITERATE_RDEV(mddev
,rdev
,tmp
) {
2059 if (rdev
!= freshest
)
2060 if (super_types
[mddev
->major_version
].
2061 validate_super(mddev
, rdev
)) {
2062 printk(KERN_WARNING
"md: kicking non-fresh %s"
2064 bdevname(rdev
->bdev
,b
));
2065 kick_rdev_from_array(rdev
);
2068 if (mddev
->level
== LEVEL_MULTIPATH
) {
2069 rdev
->desc_nr
= i
++;
2070 rdev
->raid_disk
= rdev
->desc_nr
;
2071 set_bit(In_sync
, &rdev
->flags
);
2077 if (mddev
->recovery_cp
!= MaxSector
&&
2079 printk(KERN_ERR
"md: %s: raid array is not clean"
2080 " -- starting background reconstruction\n",
2086 safe_delay_show(mddev_t
*mddev
, char *page
)
2088 int msec
= (mddev
->safemode_delay
*1000)/HZ
;
2089 return sprintf(page
, "%d.%03d\n", msec
/1000, msec
%1000);
2092 safe_delay_store(mddev_t
*mddev
, const char *cbuf
, size_t len
)
2100 /* remove a period, and count digits after it */
2101 if (len
>= sizeof(buf
))
2103 strlcpy(buf
, cbuf
, len
);
2105 for (i
=0; i
<len
; i
++) {
2107 if (isdigit(buf
[i
])) {
2112 } else if (buf
[i
] == '.') {
2117 msec
= simple_strtoul(buf
, &e
, 10);
2118 if (e
== buf
|| (*e
&& *e
!= '\n'))
2120 msec
= (msec
* 1000) / scale
;
2122 mddev
->safemode_delay
= 0;
2124 mddev
->safemode_delay
= (msec
*HZ
)/1000;
2125 if (mddev
->safemode_delay
== 0)
2126 mddev
->safemode_delay
= 1;
2130 static struct md_sysfs_entry md_safe_delay
=
2131 __ATTR(safe_mode_delay
, 0644,safe_delay_show
, safe_delay_store
);
2134 level_show(mddev_t
*mddev
, char *page
)
2136 struct mdk_personality
*p
= mddev
->pers
;
2138 return sprintf(page
, "%s\n", p
->name
);
2139 else if (mddev
->clevel
[0])
2140 return sprintf(page
, "%s\n", mddev
->clevel
);
2141 else if (mddev
->level
!= LEVEL_NONE
)
2142 return sprintf(page
, "%d\n", mddev
->level
);
2148 level_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2155 if (len
>= sizeof(mddev
->clevel
))
2157 strncpy(mddev
->clevel
, buf
, len
);
2158 if (mddev
->clevel
[len
-1] == '\n')
2160 mddev
->clevel
[len
] = 0;
2161 mddev
->level
= LEVEL_NONE
;
2165 static struct md_sysfs_entry md_level
=
2166 __ATTR(level
, 0644, level_show
, level_store
);
2170 layout_show(mddev_t
*mddev
, char *page
)
2172 /* just a number, not meaningful for all levels */
2173 return sprintf(page
, "%d\n", mddev
->layout
);
2177 layout_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2180 unsigned long n
= simple_strtoul(buf
, &e
, 10);
2184 if (!*buf
|| (*e
&& *e
!= '\n'))
2190 static struct md_sysfs_entry md_layout
=
2191 __ATTR(layout
, 0655, layout_show
, layout_store
);
2195 raid_disks_show(mddev_t
*mddev
, char *page
)
2197 if (mddev
->raid_disks
== 0)
2199 return sprintf(page
, "%d\n", mddev
->raid_disks
);
2202 static int update_raid_disks(mddev_t
*mddev
, int raid_disks
);
2205 raid_disks_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2207 /* can only set raid_disks if array is not yet active */
2210 unsigned long n
= simple_strtoul(buf
, &e
, 10);
2212 if (!*buf
|| (*e
&& *e
!= '\n'))
2216 rv
= update_raid_disks(mddev
, n
);
2218 mddev
->raid_disks
= n
;
2219 return rv
? rv
: len
;
2221 static struct md_sysfs_entry md_raid_disks
=
2222 __ATTR(raid_disks
, 0644, raid_disks_show
, raid_disks_store
);
2225 chunk_size_show(mddev_t
*mddev
, char *page
)
2227 return sprintf(page
, "%d\n", mddev
->chunk_size
);
2231 chunk_size_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2233 /* can only set chunk_size if array is not yet active */
2235 unsigned long n
= simple_strtoul(buf
, &e
, 10);
2239 if (!*buf
|| (*e
&& *e
!= '\n'))
2242 mddev
->chunk_size
= n
;
2245 static struct md_sysfs_entry md_chunk_size
=
2246 __ATTR(chunk_size
, 0644, chunk_size_show
, chunk_size_store
);
2249 resync_start_show(mddev_t
*mddev
, char *page
)
2251 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->recovery_cp
);
2255 resync_start_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2257 /* can only set chunk_size if array is not yet active */
2259 unsigned long long n
= simple_strtoull(buf
, &e
, 10);
2263 if (!*buf
|| (*e
&& *e
!= '\n'))
2266 mddev
->recovery_cp
= n
;
2269 static struct md_sysfs_entry md_resync_start
=
2270 __ATTR(resync_start
, 0644, resync_start_show
, resync_start_store
);
2273 * The array state can be:
2276 * No devices, no size, no level
2277 * Equivalent to STOP_ARRAY ioctl
2279 * May have some settings, but array is not active
2280 * all IO results in error
2281 * When written, doesn't tear down array, but just stops it
2282 * suspended (not supported yet)
2283 * All IO requests will block. The array can be reconfigured.
2284 * Writing this, if accepted, will block until array is quiessent
2286 * no resync can happen. no superblocks get written.
2287 * write requests fail
2289 * like readonly, but behaves like 'clean' on a write request.
2291 * clean - no pending writes, but otherwise active.
2292 * When written to inactive array, starts without resync
2293 * If a write request arrives then
2294 * if metadata is known, mark 'dirty' and switch to 'active'.
2295 * if not known, block and switch to write-pending
2296 * If written to an active array that has pending writes, then fails.
2298 * fully active: IO and resync can be happening.
2299 * When written to inactive array, starts with resync
2302 * clean, but writes are blocked waiting for 'active' to be written.
2305 * like active, but no writes have been seen for a while (100msec).
2308 enum array_state
{ clear
, inactive
, suspended
, readonly
, read_auto
, clean
, active
,
2309 write_pending
, active_idle
, bad_word
};
2310 static char *array_states
[] = {
2311 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
2312 "write-pending", "active-idle", NULL
};
2314 static int match_word(const char *word
, char **list
)
2317 for (n
=0; list
[n
]; n
++)
2318 if (cmd_match(word
, list
[n
]))
2324 array_state_show(mddev_t
*mddev
, char *page
)
2326 enum array_state st
= inactive
;
2339 else if (mddev
->safemode
)
2345 if (list_empty(&mddev
->disks
) &&
2346 mddev
->raid_disks
== 0 &&
2352 return sprintf(page
, "%s\n", array_states
[st
]);
2355 static int do_md_stop(mddev_t
* mddev
, int ro
);
2356 static int do_md_run(mddev_t
* mddev
);
2357 static int restart_array(mddev_t
*mddev
);
2360 array_state_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2363 enum array_state st
= match_word(buf
, array_states
);
2368 /* stopping an active array */
2370 if (atomic_read(&mddev
->active
) > 1)
2372 err
= do_md_stop(mddev
, 0);
2376 /* stopping an active array */
2378 if (atomic_read(&mddev
->active
) > 1)
2380 err
= do_md_stop(mddev
, 2);
2384 break; /* not supported yet */
2387 err
= do_md_stop(mddev
, 1);
2390 err
= do_md_run(mddev
);
2394 /* stopping an active array */
2396 err
= do_md_stop(mddev
, 1);
2398 mddev
->ro
= 2; /* FIXME mark devices writable */
2401 err
= do_md_run(mddev
);
2406 restart_array(mddev
);
2407 spin_lock_irq(&mddev
->write_lock
);
2408 if (atomic_read(&mddev
->writes_pending
) == 0) {
2410 mddev
->sb_dirty
= 1;
2412 spin_unlock_irq(&mddev
->write_lock
);
2415 mddev
->recovery_cp
= MaxSector
;
2416 err
= do_md_run(mddev
);
2421 restart_array(mddev
);
2422 mddev
->sb_dirty
= 0;
2423 wake_up(&mddev
->sb_wait
);
2427 err
= do_md_run(mddev
);
2432 /* these cannot be set */
2440 static struct md_sysfs_entry md_array_state
= __ATTR(array_state
, 0644, array_state_show
, array_state_store
);
2443 null_show(mddev_t
*mddev
, char *page
)
2449 new_dev_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2451 /* buf must be %d:%d\n? giving major and minor numbers */
2452 /* The new device is added to the array.
2453 * If the array has a persistent superblock, we read the
2454 * superblock to initialise info and check validity.
2455 * Otherwise, only checking done is that in bind_rdev_to_array,
2456 * which mainly checks size.
2459 int major
= simple_strtoul(buf
, &e
, 10);
2465 if (!*buf
|| *e
!= ':' || !e
[1] || e
[1] == '\n')
2467 minor
= simple_strtoul(e
+1, &e
, 10);
2468 if (*e
&& *e
!= '\n')
2470 dev
= MKDEV(major
, minor
);
2471 if (major
!= MAJOR(dev
) ||
2472 minor
!= MINOR(dev
))
2476 if (mddev
->persistent
) {
2477 rdev
= md_import_device(dev
, mddev
->major_version
,
2478 mddev
->minor_version
);
2479 if (!IS_ERR(rdev
) && !list_empty(&mddev
->disks
)) {
2480 mdk_rdev_t
*rdev0
= list_entry(mddev
->disks
.next
,
2481 mdk_rdev_t
, same_set
);
2482 err
= super_types
[mddev
->major_version
]
2483 .load_super(rdev
, rdev0
, mddev
->minor_version
);
2488 rdev
= md_import_device(dev
, -1, -1);
2491 return PTR_ERR(rdev
);
2492 err
= bind_rdev_to_array(rdev
, mddev
);
2496 return err
? err
: len
;
2499 static struct md_sysfs_entry md_new_device
=
2500 __ATTR(new_dev
, 0200, null_show
, new_dev_store
);
2503 size_show(mddev_t
*mddev
, char *page
)
2505 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->size
);
2508 static int update_size(mddev_t
*mddev
, unsigned long size
);
2511 size_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2513 /* If array is inactive, we can reduce the component size, but
2514 * not increase it (except from 0).
2515 * If array is active, we can try an on-line resize
2519 unsigned long long size
= simple_strtoull(buf
, &e
, 10);
2520 if (!*buf
|| *buf
== '\n' ||
2525 err
= update_size(mddev
, size
);
2526 md_update_sb(mddev
);
2528 if (mddev
->size
== 0 ||
2534 return err
? err
: len
;
2537 static struct md_sysfs_entry md_size
=
2538 __ATTR(component_size
, 0644, size_show
, size_store
);
2542 * This is either 'none' for arrays with externally managed metadata,
2543 * or N.M for internally known formats
2546 metadata_show(mddev_t
*mddev
, char *page
)
2548 if (mddev
->persistent
)
2549 return sprintf(page
, "%d.%d\n",
2550 mddev
->major_version
, mddev
->minor_version
);
2552 return sprintf(page
, "none\n");
2556 metadata_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2560 if (!list_empty(&mddev
->disks
))
2563 if (cmd_match(buf
, "none")) {
2564 mddev
->persistent
= 0;
2565 mddev
->major_version
= 0;
2566 mddev
->minor_version
= 90;
2569 major
= simple_strtoul(buf
, &e
, 10);
2570 if (e
==buf
|| *e
!= '.')
2573 minor
= simple_strtoul(buf
, &e
, 10);
2574 if (e
==buf
|| *e
!= '\n')
2576 if (major
>= sizeof(super_types
)/sizeof(super_types
[0]) ||
2577 super_types
[major
].name
== NULL
)
2579 mddev
->major_version
= major
;
2580 mddev
->minor_version
= minor
;
2581 mddev
->persistent
= 1;
2585 static struct md_sysfs_entry md_metadata
=
2586 __ATTR(metadata_version
, 0644, metadata_show
, metadata_store
);
2589 action_show(mddev_t
*mddev
, char *page
)
2591 char *type
= "idle";
2592 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
2593 test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
)) {
2594 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
2596 else if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
2597 if (!test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
2599 else if (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
))
2606 return sprintf(page
, "%s\n", type
);
2610 action_store(mddev_t
*mddev
, const char *page
, size_t len
)
2612 if (!mddev
->pers
|| !mddev
->pers
->sync_request
)
2615 if (cmd_match(page
, "idle")) {
2616 if (mddev
->sync_thread
) {
2617 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
2618 md_unregister_thread(mddev
->sync_thread
);
2619 mddev
->sync_thread
= NULL
;
2620 mddev
->recovery
= 0;
2622 } else if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
2623 test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))
2625 else if (cmd_match(page
, "resync") || cmd_match(page
, "recover"))
2626 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
2627 else if (cmd_match(page
, "reshape")) {
2629 if (mddev
->pers
->start_reshape
== NULL
)
2631 err
= mddev
->pers
->start_reshape(mddev
);
2635 if (cmd_match(page
, "check"))
2636 set_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
2637 else if (!cmd_match(page
, "repair"))
2639 set_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
2640 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
2642 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
2643 md_wakeup_thread(mddev
->thread
);
2648 mismatch_cnt_show(mddev_t
*mddev
, char *page
)
2650 return sprintf(page
, "%llu\n",
2651 (unsigned long long) mddev
->resync_mismatches
);
2654 static struct md_sysfs_entry
2655 md_scan_mode
= __ATTR(sync_action
, S_IRUGO
|S_IWUSR
, action_show
, action_store
);
2658 static struct md_sysfs_entry
2659 md_mismatches
= __ATTR_RO(mismatch_cnt
);
2662 sync_min_show(mddev_t
*mddev
, char *page
)
2664 return sprintf(page
, "%d (%s)\n", speed_min(mddev
),
2665 mddev
->sync_speed_min
? "local": "system");
2669 sync_min_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2673 if (strncmp(buf
, "system", 6)==0) {
2674 mddev
->sync_speed_min
= 0;
2677 min
= simple_strtoul(buf
, &e
, 10);
2678 if (buf
== e
|| (*e
&& *e
!= '\n') || min
<= 0)
2680 mddev
->sync_speed_min
= min
;
2684 static struct md_sysfs_entry md_sync_min
=
2685 __ATTR(sync_speed_min
, S_IRUGO
|S_IWUSR
, sync_min_show
, sync_min_store
);
2688 sync_max_show(mddev_t
*mddev
, char *page
)
2690 return sprintf(page
, "%d (%s)\n", speed_max(mddev
),
2691 mddev
->sync_speed_max
? "local": "system");
2695 sync_max_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2699 if (strncmp(buf
, "system", 6)==0) {
2700 mddev
->sync_speed_max
= 0;
2703 max
= simple_strtoul(buf
, &e
, 10);
2704 if (buf
== e
|| (*e
&& *e
!= '\n') || max
<= 0)
2706 mddev
->sync_speed_max
= max
;
2710 static struct md_sysfs_entry md_sync_max
=
2711 __ATTR(sync_speed_max
, S_IRUGO
|S_IWUSR
, sync_max_show
, sync_max_store
);
2715 sync_speed_show(mddev_t
*mddev
, char *page
)
2717 unsigned long resync
, dt
, db
;
2718 resync
= (mddev
->curr_resync
- atomic_read(&mddev
->recovery_active
));
2719 dt
= ((jiffies
- mddev
->resync_mark
) / HZ
);
2721 db
= resync
- (mddev
->resync_mark_cnt
);
2722 return sprintf(page
, "%ld\n", db
/dt
/2); /* K/sec */
2725 static struct md_sysfs_entry
2726 md_sync_speed
= __ATTR_RO(sync_speed
);
2729 sync_completed_show(mddev_t
*mddev
, char *page
)
2731 unsigned long max_blocks
, resync
;
2733 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
2734 max_blocks
= mddev
->resync_max_sectors
;
2736 max_blocks
= mddev
->size
<< 1;
2738 resync
= (mddev
->curr_resync
- atomic_read(&mddev
->recovery_active
));
2739 return sprintf(page
, "%lu / %lu\n", resync
, max_blocks
);
2742 static struct md_sysfs_entry
2743 md_sync_completed
= __ATTR_RO(sync_completed
);
2746 suspend_lo_show(mddev_t
*mddev
, char *page
)
2748 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->suspend_lo
);
2752 suspend_lo_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2755 unsigned long long new = simple_strtoull(buf
, &e
, 10);
2757 if (mddev
->pers
->quiesce
== NULL
)
2759 if (buf
== e
|| (*e
&& *e
!= '\n'))
2761 if (new >= mddev
->suspend_hi
||
2762 (new > mddev
->suspend_lo
&& new < mddev
->suspend_hi
)) {
2763 mddev
->suspend_lo
= new;
2764 mddev
->pers
->quiesce(mddev
, 2);
2769 static struct md_sysfs_entry md_suspend_lo
=
2770 __ATTR(suspend_lo
, S_IRUGO
|S_IWUSR
, suspend_lo_show
, suspend_lo_store
);
2774 suspend_hi_show(mddev_t
*mddev
, char *page
)
2776 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->suspend_hi
);
2780 suspend_hi_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2783 unsigned long long new = simple_strtoull(buf
, &e
, 10);
2785 if (mddev
->pers
->quiesce
== NULL
)
2787 if (buf
== e
|| (*e
&& *e
!= '\n'))
2789 if ((new <= mddev
->suspend_lo
&& mddev
->suspend_lo
>= mddev
->suspend_hi
) ||
2790 (new > mddev
->suspend_lo
&& new > mddev
->suspend_hi
)) {
2791 mddev
->suspend_hi
= new;
2792 mddev
->pers
->quiesce(mddev
, 1);
2793 mddev
->pers
->quiesce(mddev
, 0);
2798 static struct md_sysfs_entry md_suspend_hi
=
2799 __ATTR(suspend_hi
, S_IRUGO
|S_IWUSR
, suspend_hi_show
, suspend_hi_store
);
2802 static struct attribute
*md_default_attrs
[] = {
2805 &md_raid_disks
.attr
,
2806 &md_chunk_size
.attr
,
2808 &md_resync_start
.attr
,
2810 &md_new_device
.attr
,
2811 &md_safe_delay
.attr
,
2812 &md_array_state
.attr
,
2816 static struct attribute
*md_redundancy_attrs
[] = {
2818 &md_mismatches
.attr
,
2821 &md_sync_speed
.attr
,
2822 &md_sync_completed
.attr
,
2823 &md_suspend_lo
.attr
,
2824 &md_suspend_hi
.attr
,
2827 static struct attribute_group md_redundancy_group
= {
2829 .attrs
= md_redundancy_attrs
,
2834 md_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
2836 struct md_sysfs_entry
*entry
= container_of(attr
, struct md_sysfs_entry
, attr
);
2837 mddev_t
*mddev
= container_of(kobj
, struct mddev_s
, kobj
);
2842 rv
= mddev_lock(mddev
);
2844 rv
= entry
->show(mddev
, page
);
2845 mddev_unlock(mddev
);
2851 md_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
2852 const char *page
, size_t length
)
2854 struct md_sysfs_entry
*entry
= container_of(attr
, struct md_sysfs_entry
, attr
);
2855 mddev_t
*mddev
= container_of(kobj
, struct mddev_s
, kobj
);
2860 rv
= mddev_lock(mddev
);
2862 rv
= entry
->store(mddev
, page
, length
);
2863 mddev_unlock(mddev
);
2868 static void md_free(struct kobject
*ko
)
2870 mddev_t
*mddev
= container_of(ko
, mddev_t
, kobj
);
2874 static struct sysfs_ops md_sysfs_ops
= {
2875 .show
= md_attr_show
,
2876 .store
= md_attr_store
,
2878 static struct kobj_type md_ktype
= {
2880 .sysfs_ops
= &md_sysfs_ops
,
2881 .default_attrs
= md_default_attrs
,
2886 static struct kobject
*md_probe(dev_t dev
, int *part
, void *data
)
2888 static DEFINE_MUTEX(disks_mutex
);
2889 mddev_t
*mddev
= mddev_find(dev
);
2890 struct gendisk
*disk
;
2891 int partitioned
= (MAJOR(dev
) != MD_MAJOR
);
2892 int shift
= partitioned
? MdpMinorShift
: 0;
2893 int unit
= MINOR(dev
) >> shift
;
2898 mutex_lock(&disks_mutex
);
2899 if (mddev
->gendisk
) {
2900 mutex_unlock(&disks_mutex
);
2904 disk
= alloc_disk(1 << shift
);
2906 mutex_unlock(&disks_mutex
);
2910 disk
->major
= MAJOR(dev
);
2911 disk
->first_minor
= unit
<< shift
;
2913 sprintf(disk
->disk_name
, "md_d%d", unit
);
2915 sprintf(disk
->disk_name
, "md%d", unit
);
2916 disk
->fops
= &md_fops
;
2917 disk
->private_data
= mddev
;
2918 disk
->queue
= mddev
->queue
;
2920 mddev
->gendisk
= disk
;
2921 mutex_unlock(&disks_mutex
);
2922 mddev
->kobj
.parent
= &disk
->kobj
;
2923 mddev
->kobj
.k_name
= NULL
;
2924 snprintf(mddev
->kobj
.name
, KOBJ_NAME_LEN
, "%s", "md");
2925 mddev
->kobj
.ktype
= &md_ktype
;
2926 kobject_register(&mddev
->kobj
);
2930 static void md_safemode_timeout(unsigned long data
)
2932 mddev_t
*mddev
= (mddev_t
*) data
;
2934 mddev
->safemode
= 1;
2935 md_wakeup_thread(mddev
->thread
);
2938 static int start_dirty_degraded
;
2940 static int do_md_run(mddev_t
* mddev
)
2944 struct list_head
*tmp
;
2946 struct gendisk
*disk
;
2947 struct mdk_personality
*pers
;
2948 char b
[BDEVNAME_SIZE
];
2950 if (list_empty(&mddev
->disks
))
2951 /* cannot run an array with no devices.. */
2958 * Analyze all RAID superblock(s)
2960 if (!mddev
->raid_disks
)
2963 chunk_size
= mddev
->chunk_size
;
2966 if (chunk_size
> MAX_CHUNK_SIZE
) {
2967 printk(KERN_ERR
"too big chunk_size: %d > %d\n",
2968 chunk_size
, MAX_CHUNK_SIZE
);
2972 * chunk-size has to be a power of 2 and multiples of PAGE_SIZE
2974 if ( (1 << ffz(~chunk_size
)) != chunk_size
) {
2975 printk(KERN_ERR
"chunk_size of %d not valid\n", chunk_size
);
2978 if (chunk_size
< PAGE_SIZE
) {
2979 printk(KERN_ERR
"too small chunk_size: %d < %ld\n",
2980 chunk_size
, PAGE_SIZE
);
2984 /* devices must have minimum size of one chunk */
2985 ITERATE_RDEV(mddev
,rdev
,tmp
) {
2986 if (test_bit(Faulty
, &rdev
->flags
))
2988 if (rdev
->size
< chunk_size
/ 1024) {
2990 "md: Dev %s smaller than chunk_size:"
2992 bdevname(rdev
->bdev
,b
),
2993 (unsigned long long)rdev
->size
,
3001 if (mddev
->level
!= LEVEL_NONE
)
3002 request_module("md-level-%d", mddev
->level
);
3003 else if (mddev
->clevel
[0])
3004 request_module("md-%s", mddev
->clevel
);
3008 * Drop all container device buffers, from now on
3009 * the only valid external interface is through the md
3011 * Also find largest hardsector size
3013 ITERATE_RDEV(mddev
,rdev
,tmp
) {
3014 if (test_bit(Faulty
, &rdev
->flags
))
3016 sync_blockdev(rdev
->bdev
);
3017 invalidate_bdev(rdev
->bdev
, 0);
3020 md_probe(mddev
->unit
, NULL
, NULL
);
3021 disk
= mddev
->gendisk
;
3025 spin_lock(&pers_lock
);
3026 pers
= find_pers(mddev
->level
, mddev
->clevel
);
3027 if (!pers
|| !try_module_get(pers
->owner
)) {
3028 spin_unlock(&pers_lock
);
3029 if (mddev
->level
!= LEVEL_NONE
)
3030 printk(KERN_WARNING
"md: personality for level %d is not loaded!\n",
3033 printk(KERN_WARNING
"md: personality for level %s is not loaded!\n",
3038 spin_unlock(&pers_lock
);
3039 mddev
->level
= pers
->level
;
3040 strlcpy(mddev
->clevel
, pers
->name
, sizeof(mddev
->clevel
));
3042 if (mddev
->reshape_position
!= MaxSector
&&
3043 pers
->start_reshape
== NULL
) {
3044 /* This personality cannot handle reshaping... */
3046 module_put(pers
->owner
);
3050 mddev
->recovery
= 0;
3051 mddev
->resync_max_sectors
= mddev
->size
<< 1; /* may be over-ridden by personality */
3052 mddev
->barriers_work
= 1;
3053 mddev
->ok_start_degraded
= start_dirty_degraded
;
3056 mddev
->ro
= 2; /* read-only, but switch on first write */
3058 err
= mddev
->pers
->run(mddev
);
3059 if (!err
&& mddev
->pers
->sync_request
) {
3060 err
= bitmap_create(mddev
);
3062 printk(KERN_ERR
"%s: failed to create bitmap (%d)\n",
3063 mdname(mddev
), err
);
3064 mddev
->pers
->stop(mddev
);
3068 printk(KERN_ERR
"md: pers->run() failed ...\n");
3069 module_put(mddev
->pers
->owner
);
3071 bitmap_destroy(mddev
);
3074 if (mddev
->pers
->sync_request
)
3075 sysfs_create_group(&mddev
->kobj
, &md_redundancy_group
);
3076 else if (mddev
->ro
== 2) /* auto-readonly not meaningful */
3079 atomic_set(&mddev
->writes_pending
,0);
3080 mddev
->safemode
= 0;
3081 mddev
->safemode_timer
.function
= md_safemode_timeout
;
3082 mddev
->safemode_timer
.data
= (unsigned long) mddev
;
3083 mddev
->safemode_delay
= (200 * HZ
)/1000 +1; /* 200 msec delay */
3086 ITERATE_RDEV(mddev
,rdev
,tmp
)
3087 if (rdev
->raid_disk
>= 0) {
3089 sprintf(nm
, "rd%d", rdev
->raid_disk
);
3090 sysfs_create_link(&mddev
->kobj
, &rdev
->kobj
, nm
);
3093 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3094 md_wakeup_thread(mddev
->thread
);
3096 if (mddev
->sb_dirty
)
3097 md_update_sb(mddev
);
3099 set_capacity(disk
, mddev
->array_size
<<1);
3101 /* If we call blk_queue_make_request here, it will
3102 * re-initialise max_sectors etc which may have been
3103 * refined inside -> run. So just set the bits we need to set.
3104 * Most initialisation happended when we called
3105 * blk_queue_make_request(..., md_fail_request)
3108 mddev
->queue
->queuedata
= mddev
;
3109 mddev
->queue
->make_request_fn
= mddev
->pers
->make_request
;
3111 /* If there is a partially-recovered drive we need to
3112 * start recovery here. If we leave it to md_check_recovery,
3113 * it will remove the drives and not do the right thing
3115 if (mddev
->degraded
) {
3116 struct list_head
*rtmp
;
3118 ITERATE_RDEV(mddev
,rdev
,rtmp
)
3119 if (rdev
->raid_disk
>= 0 &&
3120 !test_bit(In_sync
, &rdev
->flags
) &&
3121 !test_bit(Faulty
, &rdev
->flags
))
3122 /* complete an interrupted recovery */
3124 if (spares
&& mddev
->pers
->sync_request
) {
3125 mddev
->recovery
= 0;
3126 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
3127 mddev
->sync_thread
= md_register_thread(md_do_sync
,
3130 if (!mddev
->sync_thread
) {
3131 printk(KERN_ERR
"%s: could not start resync"
3134 /* leave the spares where they are, it shouldn't hurt */
3135 mddev
->recovery
= 0;
3137 md_wakeup_thread(mddev
->sync_thread
);
3142 md_new_event(mddev
);
3146 static int restart_array(mddev_t
*mddev
)
3148 struct gendisk
*disk
= mddev
->gendisk
;
3152 * Complain if it has no devices
3155 if (list_empty(&mddev
->disks
))
3163 mddev
->safemode
= 0;
3165 set_disk_ro(disk
, 0);
3167 printk(KERN_INFO
"md: %s switched to read-write mode.\n",
3170 * Kick recovery or resync if necessary
3172 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3173 md_wakeup_thread(mddev
->thread
);
3174 md_wakeup_thread(mddev
->sync_thread
);
3183 /* similar to deny_write_access, but accounts for our holding a reference
3184 * to the file ourselves */
3185 static int deny_bitmap_write_access(struct file
* file
)
3187 struct inode
*inode
= file
->f_mapping
->host
;
3189 spin_lock(&inode
->i_lock
);
3190 if (atomic_read(&inode
->i_writecount
) > 1) {
3191 spin_unlock(&inode
->i_lock
);
3194 atomic_set(&inode
->i_writecount
, -1);
3195 spin_unlock(&inode
->i_lock
);
3200 static void restore_bitmap_write_access(struct file
*file
)
3202 struct inode
*inode
= file
->f_mapping
->host
;
3204 spin_lock(&inode
->i_lock
);
3205 atomic_set(&inode
->i_writecount
, 1);
3206 spin_unlock(&inode
->i_lock
);
3210 * 0 - completely stop and dis-assemble array
3211 * 1 - switch to readonly
3212 * 2 - stop but do not disassemble array
3214 static int do_md_stop(mddev_t
* mddev
, int mode
)
3217 struct gendisk
*disk
= mddev
->gendisk
;
3220 if (atomic_read(&mddev
->active
)>2) {
3221 printk("md: %s still in use.\n",mdname(mddev
));
3225 if (mddev
->sync_thread
) {
3226 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
3227 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
3228 md_unregister_thread(mddev
->sync_thread
);
3229 mddev
->sync_thread
= NULL
;
3232 del_timer_sync(&mddev
->safemode_timer
);
3234 invalidate_partition(disk
, 0);
3237 case 1: /* readonly */
3243 case 0: /* disassemble */
3245 bitmap_flush(mddev
);
3246 md_super_wait(mddev
);
3248 set_disk_ro(disk
, 0);
3249 blk_queue_make_request(mddev
->queue
, md_fail_request
);
3250 mddev
->pers
->stop(mddev
);
3251 if (mddev
->pers
->sync_request
)
3252 sysfs_remove_group(&mddev
->kobj
, &md_redundancy_group
);
3254 module_put(mddev
->pers
->owner
);
3259 if (!mddev
->in_sync
|| mddev
->sb_dirty
) {
3260 /* mark array as shutdown cleanly */
3262 md_update_sb(mddev
);
3265 set_disk_ro(disk
, 1);
3266 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
3270 * Free resources if final stop
3274 struct list_head
*tmp
;
3275 struct gendisk
*disk
;
3276 printk(KERN_INFO
"md: %s stopped.\n", mdname(mddev
));
3278 bitmap_destroy(mddev
);
3279 if (mddev
->bitmap_file
) {
3280 restore_bitmap_write_access(mddev
->bitmap_file
);
3281 fput(mddev
->bitmap_file
);
3282 mddev
->bitmap_file
= NULL
;
3284 mddev
->bitmap_offset
= 0;
3286 ITERATE_RDEV(mddev
,rdev
,tmp
)
3287 if (rdev
->raid_disk
>= 0) {
3289 sprintf(nm
, "rd%d", rdev
->raid_disk
);
3290 sysfs_remove_link(&mddev
->kobj
, nm
);
3293 export_array(mddev
);
3295 mddev
->array_size
= 0;
3297 mddev
->raid_disks
= 0;
3298 mddev
->recovery_cp
= 0;
3300 disk
= mddev
->gendisk
;
3302 set_capacity(disk
, 0);
3304 } else if (mddev
->pers
)
3305 printk(KERN_INFO
"md: %s switched to read-only mode.\n",
3308 md_new_event(mddev
);
3313 static void autorun_array(mddev_t
*mddev
)
3316 struct list_head
*tmp
;
3319 if (list_empty(&mddev
->disks
))
3322 printk(KERN_INFO
"md: running: ");
3324 ITERATE_RDEV(mddev
,rdev
,tmp
) {
3325 char b
[BDEVNAME_SIZE
];
3326 printk("<%s>", bdevname(rdev
->bdev
,b
));
3330 err
= do_md_run (mddev
);
3332 printk(KERN_WARNING
"md: do_md_run() returned %d\n", err
);
3333 do_md_stop (mddev
, 0);
3338 * lets try to run arrays based on all disks that have arrived
3339 * until now. (those are in pending_raid_disks)
3341 * the method: pick the first pending disk, collect all disks with
3342 * the same UUID, remove all from the pending list and put them into
3343 * the 'same_array' list. Then order this list based on superblock
3344 * update time (freshest comes first), kick out 'old' disks and
3345 * compare superblocks. If everything's fine then run it.
3347 * If "unit" is allocated, then bump its reference count
3349 static void autorun_devices(int part
)
3351 struct list_head
*tmp
;
3352 mdk_rdev_t
*rdev0
, *rdev
;
3354 char b
[BDEVNAME_SIZE
];
3356 printk(KERN_INFO
"md: autorun ...\n");
3357 while (!list_empty(&pending_raid_disks
)) {
3359 LIST_HEAD(candidates
);
3360 rdev0
= list_entry(pending_raid_disks
.next
,
3361 mdk_rdev_t
, same_set
);
3363 printk(KERN_INFO
"md: considering %s ...\n",
3364 bdevname(rdev0
->bdev
,b
));
3365 INIT_LIST_HEAD(&candidates
);
3366 ITERATE_RDEV_PENDING(rdev
,tmp
)
3367 if (super_90_load(rdev
, rdev0
, 0) >= 0) {
3368 printk(KERN_INFO
"md: adding %s ...\n",
3369 bdevname(rdev
->bdev
,b
));
3370 list_move(&rdev
->same_set
, &candidates
);
3373 * now we have a set of devices, with all of them having
3374 * mostly sane superblocks. It's time to allocate the
3377 if (rdev0
->preferred_minor
< 0 || rdev0
->preferred_minor
>= MAX_MD_DEVS
) {
3378 printk(KERN_INFO
"md: unit number in %s is bad: %d\n",
3379 bdevname(rdev0
->bdev
, b
), rdev0
->preferred_minor
);
3383 dev
= MKDEV(mdp_major
,
3384 rdev0
->preferred_minor
<< MdpMinorShift
);
3386 dev
= MKDEV(MD_MAJOR
, rdev0
->preferred_minor
);
3388 md_probe(dev
, NULL
, NULL
);
3389 mddev
= mddev_find(dev
);
3392 "md: cannot allocate memory for md drive.\n");
3395 if (mddev_lock(mddev
))
3396 printk(KERN_WARNING
"md: %s locked, cannot run\n",
3398 else if (mddev
->raid_disks
|| mddev
->major_version
3399 || !list_empty(&mddev
->disks
)) {
3401 "md: %s already running, cannot run %s\n",
3402 mdname(mddev
), bdevname(rdev0
->bdev
,b
));
3403 mddev_unlock(mddev
);
3405 printk(KERN_INFO
"md: created %s\n", mdname(mddev
));
3406 ITERATE_RDEV_GENERIC(candidates
,rdev
,tmp
) {
3407 list_del_init(&rdev
->same_set
);
3408 if (bind_rdev_to_array(rdev
, mddev
))
3411 autorun_array(mddev
);
3412 mddev_unlock(mddev
);
3414 /* on success, candidates will be empty, on error
3417 ITERATE_RDEV_GENERIC(candidates
,rdev
,tmp
)
3421 printk(KERN_INFO
"md: ... autorun DONE.\n");
3425 * import RAID devices based on one partition
3426 * if possible, the array gets run as well.
3429 static int autostart_array(dev_t startdev
)
3431 char b
[BDEVNAME_SIZE
];
3432 int err
= -EINVAL
, i
;
3433 mdp_super_t
*sb
= NULL
;
3434 mdk_rdev_t
*start_rdev
= NULL
, *rdev
;
3436 start_rdev
= md_import_device(startdev
, 0, 0);
3437 if (IS_ERR(start_rdev
))
3441 /* NOTE: this can only work for 0.90.0 superblocks */
3442 sb
= (mdp_super_t
*)page_address(start_rdev
->sb_page
);
3443 if (sb
->major_version
!= 0 ||
3444 sb
->minor_version
!= 90 ) {
3445 printk(KERN_WARNING
"md: can only autostart 0.90.0 arrays\n");
3446 export_rdev(start_rdev
);
3450 if (test_bit(Faulty
, &start_rdev
->flags
)) {
3452 "md: can not autostart based on faulty %s!\n",
3453 bdevname(start_rdev
->bdev
,b
));
3454 export_rdev(start_rdev
);
3457 list_add(&start_rdev
->same_set
, &pending_raid_disks
);
3459 for (i
= 0; i
< MD_SB_DISKS
; i
++) {
3460 mdp_disk_t
*desc
= sb
->disks
+ i
;
3461 dev_t dev
= MKDEV(desc
->major
, desc
->minor
);
3465 if (dev
== startdev
)
3467 if (MAJOR(dev
) != desc
->major
|| MINOR(dev
) != desc
->minor
)
3469 rdev
= md_import_device(dev
, 0, 0);
3473 list_add(&rdev
->same_set
, &pending_raid_disks
);
3477 * possibly return codes
3485 static int get_version(void __user
* arg
)
3489 ver
.major
= MD_MAJOR_VERSION
;
3490 ver
.minor
= MD_MINOR_VERSION
;
3491 ver
.patchlevel
= MD_PATCHLEVEL_VERSION
;
3493 if (copy_to_user(arg
, &ver
, sizeof(ver
)))
3499 static int get_array_info(mddev_t
* mddev
, void __user
* arg
)
3501 mdu_array_info_t info
;
3502 int nr
,working
,active
,failed
,spare
;
3504 struct list_head
*tmp
;
3506 nr
=working
=active
=failed
=spare
=0;
3507 ITERATE_RDEV(mddev
,rdev
,tmp
) {
3509 if (test_bit(Faulty
, &rdev
->flags
))
3513 if (test_bit(In_sync
, &rdev
->flags
))
3520 info
.major_version
= mddev
->major_version
;
3521 info
.minor_version
= mddev
->minor_version
;
3522 info
.patch_version
= MD_PATCHLEVEL_VERSION
;
3523 info
.ctime
= mddev
->ctime
;
3524 info
.level
= mddev
->level
;
3525 info
.size
= mddev
->size
;
3526 if (info
.size
!= mddev
->size
) /* overflow */
3529 info
.raid_disks
= mddev
->raid_disks
;
3530 info
.md_minor
= mddev
->md_minor
;
3531 info
.not_persistent
= !mddev
->persistent
;
3533 info
.utime
= mddev
->utime
;
3536 info
.state
= (1<<MD_SB_CLEAN
);
3537 if (mddev
->bitmap
&& mddev
->bitmap_offset
)
3538 info
.state
= (1<<MD_SB_BITMAP_PRESENT
);
3539 info
.active_disks
= active
;
3540 info
.working_disks
= working
;
3541 info
.failed_disks
= failed
;
3542 info
.spare_disks
= spare
;
3544 info
.layout
= mddev
->layout
;
3545 info
.chunk_size
= mddev
->chunk_size
;
3547 if (copy_to_user(arg
, &info
, sizeof(info
)))
3553 static int get_bitmap_file(mddev_t
* mddev
, void __user
* arg
)
3555 mdu_bitmap_file_t
*file
= NULL
; /* too big for stack allocation */
3556 char *ptr
, *buf
= NULL
;
3559 file
= kmalloc(sizeof(*file
), GFP_KERNEL
);
3563 /* bitmap disabled, zero the first byte and copy out */
3564 if (!mddev
->bitmap
|| !mddev
->bitmap
->file
) {
3565 file
->pathname
[0] = '\0';
3569 buf
= kmalloc(sizeof(file
->pathname
), GFP_KERNEL
);
3573 ptr
= file_path(mddev
->bitmap
->file
, buf
, sizeof(file
->pathname
));
3577 strcpy(file
->pathname
, ptr
);
3581 if (copy_to_user(arg
, file
, sizeof(*file
)))
3589 static int get_disk_info(mddev_t
* mddev
, void __user
* arg
)
3591 mdu_disk_info_t info
;
3595 if (copy_from_user(&info
, arg
, sizeof(info
)))
3600 rdev
= find_rdev_nr(mddev
, nr
);
3602 info
.major
= MAJOR(rdev
->bdev
->bd_dev
);
3603 info
.minor
= MINOR(rdev
->bdev
->bd_dev
);
3604 info
.raid_disk
= rdev
->raid_disk
;
3606 if (test_bit(Faulty
, &rdev
->flags
))
3607 info
.state
|= (1<<MD_DISK_FAULTY
);
3608 else if (test_bit(In_sync
, &rdev
->flags
)) {
3609 info
.state
|= (1<<MD_DISK_ACTIVE
);
3610 info
.state
|= (1<<MD_DISK_SYNC
);
3612 if (test_bit(WriteMostly
, &rdev
->flags
))
3613 info
.state
|= (1<<MD_DISK_WRITEMOSTLY
);
3615 info
.major
= info
.minor
= 0;
3616 info
.raid_disk
= -1;
3617 info
.state
= (1<<MD_DISK_REMOVED
);
3620 if (copy_to_user(arg
, &info
, sizeof(info
)))
3626 static int add_new_disk(mddev_t
* mddev
, mdu_disk_info_t
*info
)
3628 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
3630 dev_t dev
= MKDEV(info
->major
,info
->minor
);
3632 if (info
->major
!= MAJOR(dev
) || info
->minor
!= MINOR(dev
))
3635 if (!mddev
->raid_disks
) {
3637 /* expecting a device which has a superblock */
3638 rdev
= md_import_device(dev
, mddev
->major_version
, mddev
->minor_version
);
3641 "md: md_import_device returned %ld\n",
3643 return PTR_ERR(rdev
);
3645 if (!list_empty(&mddev
->disks
)) {
3646 mdk_rdev_t
*rdev0
= list_entry(mddev
->disks
.next
,
3647 mdk_rdev_t
, same_set
);
3648 int err
= super_types
[mddev
->major_version
]
3649 .load_super(rdev
, rdev0
, mddev
->minor_version
);
3652 "md: %s has different UUID to %s\n",
3653 bdevname(rdev
->bdev
,b
),
3654 bdevname(rdev0
->bdev
,b2
));
3659 err
= bind_rdev_to_array(rdev
, mddev
);
3666 * add_new_disk can be used once the array is assembled
3667 * to add "hot spares". They must already have a superblock
3672 if (!mddev
->pers
->hot_add_disk
) {
3674 "%s: personality does not support diskops!\n",
3678 if (mddev
->persistent
)
3679 rdev
= md_import_device(dev
, mddev
->major_version
,
3680 mddev
->minor_version
);
3682 rdev
= md_import_device(dev
, -1, -1);
3685 "md: md_import_device returned %ld\n",
3687 return PTR_ERR(rdev
);
3689 /* set save_raid_disk if appropriate */
3690 if (!mddev
->persistent
) {
3691 if (info
->state
& (1<<MD_DISK_SYNC
) &&
3692 info
->raid_disk
< mddev
->raid_disks
)
3693 rdev
->raid_disk
= info
->raid_disk
;
3695 rdev
->raid_disk
= -1;
3697 super_types
[mddev
->major_version
].
3698 validate_super(mddev
, rdev
);
3699 rdev
->saved_raid_disk
= rdev
->raid_disk
;
3701 clear_bit(In_sync
, &rdev
->flags
); /* just to be sure */
3702 if (info
->state
& (1<<MD_DISK_WRITEMOSTLY
))
3703 set_bit(WriteMostly
, &rdev
->flags
);
3705 rdev
->raid_disk
= -1;
3706 err
= bind_rdev_to_array(rdev
, mddev
);
3707 if (!err
&& !mddev
->pers
->hot_remove_disk
) {
3708 /* If there is hot_add_disk but no hot_remove_disk
3709 * then added disks for geometry changes,
3710 * and should be added immediately.
3712 super_types
[mddev
->major_version
].
3713 validate_super(mddev
, rdev
);
3714 err
= mddev
->pers
->hot_add_disk(mddev
, rdev
);
3716 unbind_rdev_from_array(rdev
);
3721 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3722 md_wakeup_thread(mddev
->thread
);
3726 /* otherwise, add_new_disk is only allowed
3727 * for major_version==0 superblocks
3729 if (mddev
->major_version
!= 0) {
3730 printk(KERN_WARNING
"%s: ADD_NEW_DISK not supported\n",
3735 if (!(info
->state
& (1<<MD_DISK_FAULTY
))) {
3737 rdev
= md_import_device (dev
, -1, 0);
3740 "md: error, md_import_device() returned %ld\n",
3742 return PTR_ERR(rdev
);
3744 rdev
->desc_nr
= info
->number
;
3745 if (info
->raid_disk
< mddev
->raid_disks
)
3746 rdev
->raid_disk
= info
->raid_disk
;
3748 rdev
->raid_disk
= -1;
3752 if (rdev
->raid_disk
< mddev
->raid_disks
)
3753 if (info
->state
& (1<<MD_DISK_SYNC
))
3754 set_bit(In_sync
, &rdev
->flags
);
3756 if (info
->state
& (1<<MD_DISK_WRITEMOSTLY
))
3757 set_bit(WriteMostly
, &rdev
->flags
);
3759 if (!mddev
->persistent
) {
3760 printk(KERN_INFO
"md: nonpersistent superblock ...\n");
3761 rdev
->sb_offset
= rdev
->bdev
->bd_inode
->i_size
>> BLOCK_SIZE_BITS
;
3763 rdev
->sb_offset
= calc_dev_sboffset(rdev
->bdev
);
3764 rdev
->size
= calc_dev_size(rdev
, mddev
->chunk_size
);
3766 err
= bind_rdev_to_array(rdev
, mddev
);
3776 static int hot_remove_disk(mddev_t
* mddev
, dev_t dev
)
3778 char b
[BDEVNAME_SIZE
];
3784 rdev
= find_rdev(mddev
, dev
);
3788 if (rdev
->raid_disk
>= 0)
3791 kick_rdev_from_array(rdev
);
3792 md_update_sb(mddev
);
3793 md_new_event(mddev
);
3797 printk(KERN_WARNING
"md: cannot remove active disk %s from %s ... \n",
3798 bdevname(rdev
->bdev
,b
), mdname(mddev
));
3802 static int hot_add_disk(mddev_t
* mddev
, dev_t dev
)
3804 char b
[BDEVNAME_SIZE
];
3812 if (mddev
->major_version
!= 0) {
3813 printk(KERN_WARNING
"%s: HOT_ADD may only be used with"
3814 " version-0 superblocks.\n",
3818 if (!mddev
->pers
->hot_add_disk
) {
3820 "%s: personality does not support diskops!\n",
3825 rdev
= md_import_device (dev
, -1, 0);
3828 "md: error, md_import_device() returned %ld\n",
3833 if (mddev
->persistent
)
3834 rdev
->sb_offset
= calc_dev_sboffset(rdev
->bdev
);
3837 rdev
->bdev
->bd_inode
->i_size
>> BLOCK_SIZE_BITS
;
3839 size
= calc_dev_size(rdev
, mddev
->chunk_size
);
3842 if (test_bit(Faulty
, &rdev
->flags
)) {
3844 "md: can not hot-add faulty %s disk to %s!\n",
3845 bdevname(rdev
->bdev
,b
), mdname(mddev
));
3849 clear_bit(In_sync
, &rdev
->flags
);
3851 err
= bind_rdev_to_array(rdev
, mddev
);
3856 * The rest should better be atomic, we can have disk failures
3857 * noticed in interrupt contexts ...
3860 if (rdev
->desc_nr
== mddev
->max_disks
) {
3861 printk(KERN_WARNING
"%s: can not hot-add to full array!\n",
3864 goto abort_unbind_export
;
3867 rdev
->raid_disk
= -1;
3869 md_update_sb(mddev
);
3872 * Kick recovery, maybe this spare has to be added to the
3873 * array immediately.
3875 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3876 md_wakeup_thread(mddev
->thread
);
3877 md_new_event(mddev
);
3880 abort_unbind_export
:
3881 unbind_rdev_from_array(rdev
);
3888 static int set_bitmap_file(mddev_t
*mddev
, int fd
)
3893 if (!mddev
->pers
->quiesce
)
3895 if (mddev
->recovery
|| mddev
->sync_thread
)
3897 /* we should be able to change the bitmap.. */
3903 return -EEXIST
; /* cannot add when bitmap is present */
3904 mddev
->bitmap_file
= fget(fd
);
3906 if (mddev
->bitmap_file
== NULL
) {
3907 printk(KERN_ERR
"%s: error: failed to get bitmap file\n",
3912 err
= deny_bitmap_write_access(mddev
->bitmap_file
);
3914 printk(KERN_ERR
"%s: error: bitmap file is already in use\n",
3916 fput(mddev
->bitmap_file
);
3917 mddev
->bitmap_file
= NULL
;
3920 mddev
->bitmap_offset
= 0; /* file overrides offset */
3921 } else if (mddev
->bitmap
== NULL
)
3922 return -ENOENT
; /* cannot remove what isn't there */
3925 mddev
->pers
->quiesce(mddev
, 1);
3927 err
= bitmap_create(mddev
);
3928 if (fd
< 0 || err
) {
3929 bitmap_destroy(mddev
);
3930 fd
= -1; /* make sure to put the file */
3932 mddev
->pers
->quiesce(mddev
, 0);
3935 if (mddev
->bitmap_file
) {
3936 restore_bitmap_write_access(mddev
->bitmap_file
);
3937 fput(mddev
->bitmap_file
);
3939 mddev
->bitmap_file
= NULL
;
3946 * set_array_info is used two different ways
3947 * The original usage is when creating a new array.
3948 * In this usage, raid_disks is > 0 and it together with
3949 * level, size, not_persistent,layout,chunksize determine the
3950 * shape of the array.
3951 * This will always create an array with a type-0.90.0 superblock.
3952 * The newer usage is when assembling an array.
3953 * In this case raid_disks will be 0, and the major_version field is
3954 * use to determine which style super-blocks are to be found on the devices.
3955 * The minor and patch _version numbers are also kept incase the
3956 * super_block handler wishes to interpret them.
3958 static int set_array_info(mddev_t
* mddev
, mdu_array_info_t
*info
)
3961 if (info
->raid_disks
== 0) {
3962 /* just setting version number for superblock loading */
3963 if (info
->major_version
< 0 ||
3964 info
->major_version
>= sizeof(super_types
)/sizeof(super_types
[0]) ||
3965 super_types
[info
->major_version
].name
== NULL
) {
3966 /* maybe try to auto-load a module? */
3968 "md: superblock version %d not known\n",
3969 info
->major_version
);
3972 mddev
->major_version
= info
->major_version
;
3973 mddev
->minor_version
= info
->minor_version
;
3974 mddev
->patch_version
= info
->patch_version
;
3977 mddev
->major_version
= MD_MAJOR_VERSION
;
3978 mddev
->minor_version
= MD_MINOR_VERSION
;
3979 mddev
->patch_version
= MD_PATCHLEVEL_VERSION
;
3980 mddev
->ctime
= get_seconds();
3982 mddev
->level
= info
->level
;
3983 mddev
->clevel
[0] = 0;
3984 mddev
->size
= info
->size
;
3985 mddev
->raid_disks
= info
->raid_disks
;
3986 /* don't set md_minor, it is determined by which /dev/md* was
3989 if (info
->state
& (1<<MD_SB_CLEAN
))
3990 mddev
->recovery_cp
= MaxSector
;
3992 mddev
->recovery_cp
= 0;
3993 mddev
->persistent
= ! info
->not_persistent
;
3995 mddev
->layout
= info
->layout
;
3996 mddev
->chunk_size
= info
->chunk_size
;
3998 mddev
->max_disks
= MD_SB_DISKS
;
4000 mddev
->sb_dirty
= 1;
4002 mddev
->default_bitmap_offset
= MD_SB_BYTES
>> 9;
4003 mddev
->bitmap_offset
= 0;
4005 mddev
->reshape_position
= MaxSector
;
4008 * Generate a 128 bit UUID
4010 get_random_bytes(mddev
->uuid
, 16);
4012 mddev
->new_level
= mddev
->level
;
4013 mddev
->new_chunk
= mddev
->chunk_size
;
4014 mddev
->new_layout
= mddev
->layout
;
4015 mddev
->delta_disks
= 0;
4020 static int update_size(mddev_t
*mddev
, unsigned long size
)
4024 struct list_head
*tmp
;
4025 int fit
= (size
== 0);
4027 if (mddev
->pers
->resize
== NULL
)
4029 /* The "size" is the amount of each device that is used.
4030 * This can only make sense for arrays with redundancy.
4031 * linear and raid0 always use whatever space is available
4032 * We can only consider changing the size if no resync
4033 * or reconstruction is happening, and if the new size
4034 * is acceptable. It must fit before the sb_offset or,
4035 * if that is <data_offset, it must fit before the
4036 * size of each device.
4037 * If size is zero, we find the largest size that fits.
4039 if (mddev
->sync_thread
)
4041 ITERATE_RDEV(mddev
,rdev
,tmp
) {
4043 if (rdev
->sb_offset
> rdev
->data_offset
)
4044 avail
= (rdev
->sb_offset
*2) - rdev
->data_offset
;
4046 avail
= get_capacity(rdev
->bdev
->bd_disk
)
4047 - rdev
->data_offset
;
4048 if (fit
&& (size
== 0 || size
> avail
/2))
4050 if (avail
< ((sector_t
)size
<< 1))
4053 rv
= mddev
->pers
->resize(mddev
, (sector_t
)size
*2);
4055 struct block_device
*bdev
;
4057 bdev
= bdget_disk(mddev
->gendisk
, 0);
4059 mutex_lock(&bdev
->bd_inode
->i_mutex
);
4060 i_size_write(bdev
->bd_inode
, (loff_t
)mddev
->array_size
<< 10);
4061 mutex_unlock(&bdev
->bd_inode
->i_mutex
);
4068 static int update_raid_disks(mddev_t
*mddev
, int raid_disks
)
4071 /* change the number of raid disks */
4072 if (mddev
->pers
->check_reshape
== NULL
)
4074 if (raid_disks
<= 0 ||
4075 raid_disks
>= mddev
->max_disks
)
4077 if (mddev
->sync_thread
|| mddev
->reshape_position
!= MaxSector
)
4079 mddev
->delta_disks
= raid_disks
- mddev
->raid_disks
;
4081 rv
= mddev
->pers
->check_reshape(mddev
);
4087 * update_array_info is used to change the configuration of an
4089 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
4090 * fields in the info are checked against the array.
4091 * Any differences that cannot be handled will cause an error.
4092 * Normally, only one change can be managed at a time.
4094 static int update_array_info(mddev_t
*mddev
, mdu_array_info_t
*info
)
4100 /* calculate expected state,ignoring low bits */
4101 if (mddev
->bitmap
&& mddev
->bitmap_offset
)
4102 state
|= (1 << MD_SB_BITMAP_PRESENT
);
4104 if (mddev
->major_version
!= info
->major_version
||
4105 mddev
->minor_version
!= info
->minor_version
||
4106 /* mddev->patch_version != info->patch_version || */
4107 mddev
->ctime
!= info
->ctime
||
4108 mddev
->level
!= info
->level
||
4109 /* mddev->layout != info->layout || */
4110 !mddev
->persistent
!= info
->not_persistent
||
4111 mddev
->chunk_size
!= info
->chunk_size
||
4112 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
4113 ((state
^info
->state
) & 0xfffffe00)
4116 /* Check there is only one change */
4117 if (info
->size
>= 0 && mddev
->size
!= info
->size
) cnt
++;
4118 if (mddev
->raid_disks
!= info
->raid_disks
) cnt
++;
4119 if (mddev
->layout
!= info
->layout
) cnt
++;
4120 if ((state
^ info
->state
) & (1<<MD_SB_BITMAP_PRESENT
)) cnt
++;
4121 if (cnt
== 0) return 0;
4122 if (cnt
> 1) return -EINVAL
;
4124 if (mddev
->layout
!= info
->layout
) {
4126 * we don't need to do anything at the md level, the
4127 * personality will take care of it all.
4129 if (mddev
->pers
->reconfig
== NULL
)
4132 return mddev
->pers
->reconfig(mddev
, info
->layout
, -1);
4134 if (info
->size
>= 0 && mddev
->size
!= info
->size
)
4135 rv
= update_size(mddev
, info
->size
);
4137 if (mddev
->raid_disks
!= info
->raid_disks
)
4138 rv
= update_raid_disks(mddev
, info
->raid_disks
);
4140 if ((state
^ info
->state
) & (1<<MD_SB_BITMAP_PRESENT
)) {
4141 if (mddev
->pers
->quiesce
== NULL
)
4143 if (mddev
->recovery
|| mddev
->sync_thread
)
4145 if (info
->state
& (1<<MD_SB_BITMAP_PRESENT
)) {
4146 /* add the bitmap */
4149 if (mddev
->default_bitmap_offset
== 0)
4151 mddev
->bitmap_offset
= mddev
->default_bitmap_offset
;
4152 mddev
->pers
->quiesce(mddev
, 1);
4153 rv
= bitmap_create(mddev
);
4155 bitmap_destroy(mddev
);
4156 mddev
->pers
->quiesce(mddev
, 0);
4158 /* remove the bitmap */
4161 if (mddev
->bitmap
->file
)
4163 mddev
->pers
->quiesce(mddev
, 1);
4164 bitmap_destroy(mddev
);
4165 mddev
->pers
->quiesce(mddev
, 0);
4166 mddev
->bitmap_offset
= 0;
4169 md_update_sb(mddev
);
4173 static int set_disk_faulty(mddev_t
*mddev
, dev_t dev
)
4177 if (mddev
->pers
== NULL
)
4180 rdev
= find_rdev(mddev
, dev
);
4184 md_error(mddev
, rdev
);
4188 static int md_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
4190 mddev_t
*mddev
= bdev
->bd_disk
->private_data
;
4194 geo
->cylinders
= get_capacity(mddev
->gendisk
) / 8;
4198 static int md_ioctl(struct inode
*inode
, struct file
*file
,
4199 unsigned int cmd
, unsigned long arg
)
4202 void __user
*argp
= (void __user
*)arg
;
4203 mddev_t
*mddev
= NULL
;
4205 if (!capable(CAP_SYS_ADMIN
))
4209 * Commands dealing with the RAID driver but not any
4215 err
= get_version(argp
);
4218 case PRINT_RAID_DEBUG
:
4226 autostart_arrays(arg
);
4233 * Commands creating/starting a new array:
4236 mddev
= inode
->i_bdev
->bd_disk
->private_data
;
4244 if (cmd
== START_ARRAY
) {
4245 /* START_ARRAY doesn't need to lock the array as autostart_array
4246 * does the locking, and it could even be a different array
4251 "md: %s(pid %d) used deprecated START_ARRAY ioctl. "
4252 "This will not be supported beyond July 2006\n",
4253 current
->comm
, current
->pid
);
4256 err
= autostart_array(new_decode_dev(arg
));
4258 printk(KERN_WARNING
"md: autostart failed!\n");
4264 err
= mddev_lock(mddev
);
4267 "md: ioctl lock interrupted, reason %d, cmd %d\n",
4274 case SET_ARRAY_INFO
:
4276 mdu_array_info_t info
;
4278 memset(&info
, 0, sizeof(info
));
4279 else if (copy_from_user(&info
, argp
, sizeof(info
))) {
4284 err
= update_array_info(mddev
, &info
);
4286 printk(KERN_WARNING
"md: couldn't update"
4287 " array info. %d\n", err
);
4292 if (!list_empty(&mddev
->disks
)) {
4294 "md: array %s already has disks!\n",
4299 if (mddev
->raid_disks
) {
4301 "md: array %s already initialised!\n",
4306 err
= set_array_info(mddev
, &info
);
4308 printk(KERN_WARNING
"md: couldn't set"
4309 " array info. %d\n", err
);
4319 * Commands querying/configuring an existing array:
4321 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
4322 * RUN_ARRAY, and SET_BITMAP_FILE are allowed */
4323 if (!mddev
->raid_disks
&& cmd
!= ADD_NEW_DISK
&& cmd
!= STOP_ARRAY
4324 && cmd
!= RUN_ARRAY
&& cmd
!= SET_BITMAP_FILE
) {
4330 * Commands even a read-only array can execute:
4334 case GET_ARRAY_INFO
:
4335 err
= get_array_info(mddev
, argp
);
4338 case GET_BITMAP_FILE
:
4339 err
= get_bitmap_file(mddev
, argp
);
4343 err
= get_disk_info(mddev
, argp
);
4346 case RESTART_ARRAY_RW
:
4347 err
= restart_array(mddev
);
4351 err
= do_md_stop (mddev
, 0);
4355 err
= do_md_stop (mddev
, 1);
4359 * We have a problem here : there is no easy way to give a CHS
4360 * virtual geometry. We currently pretend that we have a 2 heads
4361 * 4 sectors (with a BIG number of cylinders...). This drives
4362 * dosfs just mad... ;-)
4367 * The remaining ioctls are changing the state of the
4368 * superblock, so we do not allow them on read-only arrays.
4369 * However non-MD ioctls (e.g. get-size) will still come through
4370 * here and hit the 'default' below, so only disallow
4371 * 'md' ioctls, and switch to rw mode if started auto-readonly.
4373 if (_IOC_TYPE(cmd
) == MD_MAJOR
&&
4374 mddev
->ro
&& mddev
->pers
) {
4375 if (mddev
->ro
== 2) {
4377 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4378 md_wakeup_thread(mddev
->thread
);
4390 mdu_disk_info_t info
;
4391 if (copy_from_user(&info
, argp
, sizeof(info
)))
4394 err
= add_new_disk(mddev
, &info
);
4398 case HOT_REMOVE_DISK
:
4399 err
= hot_remove_disk(mddev
, new_decode_dev(arg
));
4403 err
= hot_add_disk(mddev
, new_decode_dev(arg
));
4406 case SET_DISK_FAULTY
:
4407 err
= set_disk_faulty(mddev
, new_decode_dev(arg
));
4411 err
= do_md_run (mddev
);
4414 case SET_BITMAP_FILE
:
4415 err
= set_bitmap_file(mddev
, (int)arg
);
4425 mddev_unlock(mddev
);
4435 static int md_open(struct inode
*inode
, struct file
*file
)
4438 * Succeed if we can lock the mddev, which confirms that
4439 * it isn't being stopped right now.
4441 mddev_t
*mddev
= inode
->i_bdev
->bd_disk
->private_data
;
4444 if ((err
= mddev_lock(mddev
)))
4449 mddev_unlock(mddev
);
4451 check_disk_change(inode
->i_bdev
);
4456 static int md_release(struct inode
*inode
, struct file
* file
)
4458 mddev_t
*mddev
= inode
->i_bdev
->bd_disk
->private_data
;
4467 static int md_media_changed(struct gendisk
*disk
)
4469 mddev_t
*mddev
= disk
->private_data
;
4471 return mddev
->changed
;
4474 static int md_revalidate(struct gendisk
*disk
)
4476 mddev_t
*mddev
= disk
->private_data
;
4481 static struct block_device_operations md_fops
=
4483 .owner
= THIS_MODULE
,
4485 .release
= md_release
,
4487 .getgeo
= md_getgeo
,
4488 .media_changed
= md_media_changed
,
4489 .revalidate_disk
= md_revalidate
,
4492 static int md_thread(void * arg
)
4494 mdk_thread_t
*thread
= arg
;
4497 * md_thread is a 'system-thread', it's priority should be very
4498 * high. We avoid resource deadlocks individually in each
4499 * raid personality. (RAID5 does preallocation) We also use RR and
4500 * the very same RT priority as kswapd, thus we will never get
4501 * into a priority inversion deadlock.
4503 * we definitely have to have equal or higher priority than
4504 * bdflush, otherwise bdflush will deadlock if there are too
4505 * many dirty RAID5 blocks.
4508 allow_signal(SIGKILL
);
4509 while (!kthread_should_stop()) {
4511 /* We need to wait INTERRUPTIBLE so that
4512 * we don't add to the load-average.
4513 * That means we need to be sure no signals are
4516 if (signal_pending(current
))
4517 flush_signals(current
);
4519 wait_event_interruptible_timeout
4521 test_bit(THREAD_WAKEUP
, &thread
->flags
)
4522 || kthread_should_stop(),
4526 clear_bit(THREAD_WAKEUP
, &thread
->flags
);
4528 thread
->run(thread
->mddev
);
4534 void md_wakeup_thread(mdk_thread_t
*thread
)
4537 dprintk("md: waking up MD thread %s.\n", thread
->tsk
->comm
);
4538 set_bit(THREAD_WAKEUP
, &thread
->flags
);
4539 wake_up(&thread
->wqueue
);
4543 mdk_thread_t
*md_register_thread(void (*run
) (mddev_t
*), mddev_t
*mddev
,
4546 mdk_thread_t
*thread
;
4548 thread
= kzalloc(sizeof(mdk_thread_t
), GFP_KERNEL
);
4552 init_waitqueue_head(&thread
->wqueue
);
4555 thread
->mddev
= mddev
;
4556 thread
->timeout
= MAX_SCHEDULE_TIMEOUT
;
4557 thread
->tsk
= kthread_run(md_thread
, thread
, name
, mdname(thread
->mddev
));
4558 if (IS_ERR(thread
->tsk
)) {
4565 void md_unregister_thread(mdk_thread_t
*thread
)
4567 dprintk("interrupting MD-thread pid %d\n", thread
->tsk
->pid
);
4569 kthread_stop(thread
->tsk
);
4573 void md_error(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
4580 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
))
4583 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
4585 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
4586 __builtin_return_address(0),__builtin_return_address(1),
4587 __builtin_return_address(2),__builtin_return_address(3));
4589 if (!mddev
->pers
->error_handler
)
4591 mddev
->pers
->error_handler(mddev
,rdev
);
4592 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
4593 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4594 md_wakeup_thread(mddev
->thread
);
4595 md_new_event_inintr(mddev
);
4598 /* seq_file implementation /proc/mdstat */
4600 static void status_unused(struct seq_file
*seq
)
4604 struct list_head
*tmp
;
4606 seq_printf(seq
, "unused devices: ");
4608 ITERATE_RDEV_PENDING(rdev
,tmp
) {
4609 char b
[BDEVNAME_SIZE
];
4611 seq_printf(seq
, "%s ",
4612 bdevname(rdev
->bdev
,b
));
4615 seq_printf(seq
, "<none>");
4617 seq_printf(seq
, "\n");
4621 static void status_resync(struct seq_file
*seq
, mddev_t
* mddev
)
4623 sector_t max_blocks
, resync
, res
;
4624 unsigned long dt
, db
, rt
;
4626 unsigned int per_milli
;
4628 resync
= (mddev
->curr_resync
- atomic_read(&mddev
->recovery_active
))/2;
4630 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
4631 max_blocks
= mddev
->resync_max_sectors
>> 1;
4633 max_blocks
= mddev
->size
;
4636 * Should not happen.
4642 /* Pick 'scale' such that (resync>>scale)*1000 will fit
4643 * in a sector_t, and (max_blocks>>scale) will fit in a
4644 * u32, as those are the requirements for sector_div.
4645 * Thus 'scale' must be at least 10
4648 if (sizeof(sector_t
) > sizeof(unsigned long)) {
4649 while ( max_blocks
/2 > (1ULL<<(scale
+32)))
4652 res
= (resync
>>scale
)*1000;
4653 sector_div(res
, (u32
)((max_blocks
>>scale
)+1));
4657 int i
, x
= per_milli
/50, y
= 20-x
;
4658 seq_printf(seq
, "[");
4659 for (i
= 0; i
< x
; i
++)
4660 seq_printf(seq
, "=");
4661 seq_printf(seq
, ">");
4662 for (i
= 0; i
< y
; i
++)
4663 seq_printf(seq
, ".");
4664 seq_printf(seq
, "] ");
4666 seq_printf(seq
, " %s =%3u.%u%% (%llu/%llu)",
4667 (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
)?
4669 (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ?
4670 "resync" : "recovery")),
4671 per_milli
/10, per_milli
% 10,
4672 (unsigned long long) resync
,
4673 (unsigned long long) max_blocks
);
4676 * We do not want to overflow, so the order of operands and
4677 * the * 100 / 100 trick are important. We do a +1 to be
4678 * safe against division by zero. We only estimate anyway.
4680 * dt: time from mark until now
4681 * db: blocks written from mark until now
4682 * rt: remaining time
4684 dt
= ((jiffies
- mddev
->resync_mark
) / HZ
);
4686 db
= resync
- (mddev
->resync_mark_cnt
/2);
4687 rt
= (dt
* ((unsigned long)(max_blocks
-resync
) / (db
/100+1)))/100;
4689 seq_printf(seq
, " finish=%lu.%lumin", rt
/ 60, (rt
% 60)/6);
4691 seq_printf(seq
, " speed=%ldK/sec", db
/dt
);
4694 static void *md_seq_start(struct seq_file
*seq
, loff_t
*pos
)
4696 struct list_head
*tmp
;
4706 spin_lock(&all_mddevs_lock
);
4707 list_for_each(tmp
,&all_mddevs
)
4709 mddev
= list_entry(tmp
, mddev_t
, all_mddevs
);
4711 spin_unlock(&all_mddevs_lock
);
4714 spin_unlock(&all_mddevs_lock
);
4716 return (void*)2;/* tail */
4720 static void *md_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
4722 struct list_head
*tmp
;
4723 mddev_t
*next_mddev
, *mddev
= v
;
4729 spin_lock(&all_mddevs_lock
);
4731 tmp
= all_mddevs
.next
;
4733 tmp
= mddev
->all_mddevs
.next
;
4734 if (tmp
!= &all_mddevs
)
4735 next_mddev
= mddev_get(list_entry(tmp
,mddev_t
,all_mddevs
));
4737 next_mddev
= (void*)2;
4740 spin_unlock(&all_mddevs_lock
);
4748 static void md_seq_stop(struct seq_file
*seq
, void *v
)
4752 if (mddev
&& v
!= (void*)1 && v
!= (void*)2)
4756 struct mdstat_info
{
4760 static int md_seq_show(struct seq_file
*seq
, void *v
)
4764 struct list_head
*tmp2
;
4766 struct mdstat_info
*mi
= seq
->private;
4767 struct bitmap
*bitmap
;
4769 if (v
== (void*)1) {
4770 struct mdk_personality
*pers
;
4771 seq_printf(seq
, "Personalities : ");
4772 spin_lock(&pers_lock
);
4773 list_for_each_entry(pers
, &pers_list
, list
)
4774 seq_printf(seq
, "[%s] ", pers
->name
);
4776 spin_unlock(&pers_lock
);
4777 seq_printf(seq
, "\n");
4778 mi
->event
= atomic_read(&md_event_count
);
4781 if (v
== (void*)2) {
4786 if (mddev_lock(mddev
) < 0)
4789 if (mddev
->pers
|| mddev
->raid_disks
|| !list_empty(&mddev
->disks
)) {
4790 seq_printf(seq
, "%s : %sactive", mdname(mddev
),
4791 mddev
->pers
? "" : "in");
4794 seq_printf(seq
, " (read-only)");
4796 seq_printf(seq
, "(auto-read-only)");
4797 seq_printf(seq
, " %s", mddev
->pers
->name
);
4801 ITERATE_RDEV(mddev
,rdev
,tmp2
) {
4802 char b
[BDEVNAME_SIZE
];
4803 seq_printf(seq
, " %s[%d]",
4804 bdevname(rdev
->bdev
,b
), rdev
->desc_nr
);
4805 if (test_bit(WriteMostly
, &rdev
->flags
))
4806 seq_printf(seq
, "(W)");
4807 if (test_bit(Faulty
, &rdev
->flags
)) {
4808 seq_printf(seq
, "(F)");
4810 } else if (rdev
->raid_disk
< 0)
4811 seq_printf(seq
, "(S)"); /* spare */
4815 if (!list_empty(&mddev
->disks
)) {
4817 seq_printf(seq
, "\n %llu blocks",
4818 (unsigned long long)mddev
->array_size
);
4820 seq_printf(seq
, "\n %llu blocks",
4821 (unsigned long long)size
);
4823 if (mddev
->persistent
) {
4824 if (mddev
->major_version
!= 0 ||
4825 mddev
->minor_version
!= 90) {
4826 seq_printf(seq
," super %d.%d",
4827 mddev
->major_version
,
4828 mddev
->minor_version
);
4831 seq_printf(seq
, " super non-persistent");
4834 mddev
->pers
->status (seq
, mddev
);
4835 seq_printf(seq
, "\n ");
4836 if (mddev
->pers
->sync_request
) {
4837 if (mddev
->curr_resync
> 2) {
4838 status_resync (seq
, mddev
);
4839 seq_printf(seq
, "\n ");
4840 } else if (mddev
->curr_resync
== 1 || mddev
->curr_resync
== 2)
4841 seq_printf(seq
, "\tresync=DELAYED\n ");
4842 else if (mddev
->recovery_cp
< MaxSector
)
4843 seq_printf(seq
, "\tresync=PENDING\n ");
4846 seq_printf(seq
, "\n ");
4848 if ((bitmap
= mddev
->bitmap
)) {
4849 unsigned long chunk_kb
;
4850 unsigned long flags
;
4851 spin_lock_irqsave(&bitmap
->lock
, flags
);
4852 chunk_kb
= bitmap
->chunksize
>> 10;
4853 seq_printf(seq
, "bitmap: %lu/%lu pages [%luKB], "
4855 bitmap
->pages
- bitmap
->missing_pages
,
4857 (bitmap
->pages
- bitmap
->missing_pages
)
4858 << (PAGE_SHIFT
- 10),
4859 chunk_kb
? chunk_kb
: bitmap
->chunksize
,
4860 chunk_kb
? "KB" : "B");
4862 seq_printf(seq
, ", file: ");
4863 seq_path(seq
, bitmap
->file
->f_vfsmnt
,
4864 bitmap
->file
->f_dentry
," \t\n");
4867 seq_printf(seq
, "\n");
4868 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
4871 seq_printf(seq
, "\n");
4873 mddev_unlock(mddev
);
4878 static struct seq_operations md_seq_ops
= {
4879 .start
= md_seq_start
,
4880 .next
= md_seq_next
,
4881 .stop
= md_seq_stop
,
4882 .show
= md_seq_show
,
4885 static int md_seq_open(struct inode
*inode
, struct file
*file
)
4888 struct mdstat_info
*mi
= kmalloc(sizeof(*mi
), GFP_KERNEL
);
4892 error
= seq_open(file
, &md_seq_ops
);
4896 struct seq_file
*p
= file
->private_data
;
4898 mi
->event
= atomic_read(&md_event_count
);
4903 static int md_seq_release(struct inode
*inode
, struct file
*file
)
4905 struct seq_file
*m
= file
->private_data
;
4906 struct mdstat_info
*mi
= m
->private;
4909 return seq_release(inode
, file
);
4912 static unsigned int mdstat_poll(struct file
*filp
, poll_table
*wait
)
4914 struct seq_file
*m
= filp
->private_data
;
4915 struct mdstat_info
*mi
= m
->private;
4918 poll_wait(filp
, &md_event_waiters
, wait
);
4920 /* always allow read */
4921 mask
= POLLIN
| POLLRDNORM
;
4923 if (mi
->event
!= atomic_read(&md_event_count
))
4924 mask
|= POLLERR
| POLLPRI
;
4928 static struct file_operations md_seq_fops
= {
4929 .open
= md_seq_open
,
4931 .llseek
= seq_lseek
,
4932 .release
= md_seq_release
,
4933 .poll
= mdstat_poll
,
4936 int register_md_personality(struct mdk_personality
*p
)
4938 spin_lock(&pers_lock
);
4939 list_add_tail(&p
->list
, &pers_list
);
4940 printk(KERN_INFO
"md: %s personality registered for level %d\n", p
->name
, p
->level
);
4941 spin_unlock(&pers_lock
);
4945 int unregister_md_personality(struct mdk_personality
*p
)
4947 printk(KERN_INFO
"md: %s personality unregistered\n", p
->name
);
4948 spin_lock(&pers_lock
);
4949 list_del_init(&p
->list
);
4950 spin_unlock(&pers_lock
);
4954 static int is_mddev_idle(mddev_t
*mddev
)
4957 struct list_head
*tmp
;
4959 unsigned long curr_events
;
4962 ITERATE_RDEV(mddev
,rdev
,tmp
) {
4963 struct gendisk
*disk
= rdev
->bdev
->bd_contains
->bd_disk
;
4964 curr_events
= disk_stat_read(disk
, sectors
[0]) +
4965 disk_stat_read(disk
, sectors
[1]) -
4966 atomic_read(&disk
->sync_io
);
4967 /* The difference between curr_events and last_events
4968 * will be affected by any new non-sync IO (making
4969 * curr_events bigger) and any difference in the amount of
4970 * in-flight syncio (making current_events bigger or smaller)
4971 * The amount in-flight is currently limited to
4972 * 32*64K in raid1/10 and 256*PAGE_SIZE in raid5/6
4973 * which is at most 4096 sectors.
4974 * These numbers are fairly fragile and should be made
4975 * more robust, probably by enforcing the
4976 * 'window size' that md_do_sync sort-of uses.
4978 * Note: the following is an unsigned comparison.
4980 if ((curr_events
- rdev
->last_events
+ 4096) > 8192) {
4981 rdev
->last_events
= curr_events
;
4988 void md_done_sync(mddev_t
*mddev
, int blocks
, int ok
)
4990 /* another "blocks" (512byte) blocks have been synced */
4991 atomic_sub(blocks
, &mddev
->recovery_active
);
4992 wake_up(&mddev
->recovery_wait
);
4994 set_bit(MD_RECOVERY_ERR
, &mddev
->recovery
);
4995 md_wakeup_thread(mddev
->thread
);
4996 // stop recovery, signal do_sync ....
5001 /* md_write_start(mddev, bi)
5002 * If we need to update some array metadata (e.g. 'active' flag
5003 * in superblock) before writing, schedule a superblock update
5004 * and wait for it to complete.
5006 void md_write_start(mddev_t
*mddev
, struct bio
*bi
)
5008 if (bio_data_dir(bi
) != WRITE
)
5011 BUG_ON(mddev
->ro
== 1);
5012 if (mddev
->ro
== 2) {
5013 /* need to switch to read/write */
5015 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5016 md_wakeup_thread(mddev
->thread
);
5018 atomic_inc(&mddev
->writes_pending
);
5019 if (mddev
->in_sync
) {
5020 spin_lock_irq(&mddev
->write_lock
);
5021 if (mddev
->in_sync
) {
5023 mddev
->sb_dirty
= 3;
5024 md_wakeup_thread(mddev
->thread
);
5026 spin_unlock_irq(&mddev
->write_lock
);
5028 wait_event(mddev
->sb_wait
, mddev
->sb_dirty
==0);
5031 void md_write_end(mddev_t
*mddev
)
5033 if (atomic_dec_and_test(&mddev
->writes_pending
)) {
5034 if (mddev
->safemode
== 2)
5035 md_wakeup_thread(mddev
->thread
);
5036 else if (mddev
->safemode_delay
)
5037 mod_timer(&mddev
->safemode_timer
, jiffies
+ mddev
->safemode_delay
);
5041 static DECLARE_WAIT_QUEUE_HEAD(resync_wait
);
5043 #define SYNC_MARKS 10
5044 #define SYNC_MARK_STEP (3*HZ)
5045 void md_do_sync(mddev_t
*mddev
)
5048 unsigned int currspeed
= 0,
5050 sector_t max_sectors
,j
, io_sectors
;
5051 unsigned long mark
[SYNC_MARKS
];
5052 sector_t mark_cnt
[SYNC_MARKS
];
5054 struct list_head
*tmp
;
5055 sector_t last_check
;
5057 struct list_head
*rtmp
;
5060 /* just incase thread restarts... */
5061 if (test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
))
5063 if (mddev
->ro
) /* never try to sync a read-only array */
5066 /* we overload curr_resync somewhat here.
5067 * 0 == not engaged in resync at all
5068 * 2 == checking that there is no conflict with another sync
5069 * 1 == like 2, but have yielded to allow conflicting resync to
5071 * other == active in resync - this many blocks
5073 * Before starting a resync we must have set curr_resync to
5074 * 2, and then checked that every "conflicting" array has curr_resync
5075 * less than ours. When we find one that is the same or higher
5076 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
5077 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
5078 * This will mean we have to start checking from the beginning again.
5083 mddev
->curr_resync
= 2;
5086 if (kthread_should_stop()) {
5087 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
5090 ITERATE_MDDEV(mddev2
,tmp
) {
5091 if (mddev2
== mddev
)
5093 if (mddev2
->curr_resync
&&
5094 match_mddev_units(mddev
,mddev2
)) {
5096 if (mddev
< mddev2
&& mddev
->curr_resync
== 2) {
5097 /* arbitrarily yield */
5098 mddev
->curr_resync
= 1;
5099 wake_up(&resync_wait
);
5101 if (mddev
> mddev2
&& mddev
->curr_resync
== 1)
5102 /* no need to wait here, we can wait the next
5103 * time 'round when curr_resync == 2
5106 prepare_to_wait(&resync_wait
, &wq
, TASK_UNINTERRUPTIBLE
);
5107 if (!kthread_should_stop() &&
5108 mddev2
->curr_resync
>= mddev
->curr_resync
) {
5109 printk(KERN_INFO
"md: delaying resync of %s"
5110 " until %s has finished resync (they"
5111 " share one or more physical units)\n",
5112 mdname(mddev
), mdname(mddev2
));
5115 finish_wait(&resync_wait
, &wq
);
5118 finish_wait(&resync_wait
, &wq
);
5121 } while (mddev
->curr_resync
< 2);
5124 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
5125 /* resync follows the size requested by the personality,
5126 * which defaults to physical size, but can be virtual size
5128 max_sectors
= mddev
->resync_max_sectors
;
5129 mddev
->resync_mismatches
= 0;
5130 /* we don't use the checkpoint if there's a bitmap */
5131 if (!mddev
->bitmap
&&
5132 !test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
5133 j
= mddev
->recovery_cp
;
5134 } else if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
5135 max_sectors
= mddev
->size
<< 1;
5137 /* recovery follows the physical size of devices */
5138 max_sectors
= mddev
->size
<< 1;
5140 ITERATE_RDEV(mddev
,rdev
,rtmp
)
5141 if (rdev
->raid_disk
>= 0 &&
5142 !test_bit(Faulty
, &rdev
->flags
) &&
5143 !test_bit(In_sync
, &rdev
->flags
) &&
5144 rdev
->recovery_offset
< j
)
5145 j
= rdev
->recovery_offset
;
5148 printk(KERN_INFO
"md: syncing RAID array %s\n", mdname(mddev
));
5149 printk(KERN_INFO
"md: minimum _guaranteed_ reconstruction speed:"
5150 " %d KB/sec/disc.\n", speed_min(mddev
));
5151 printk(KERN_INFO
"md: using maximum available idle IO bandwidth "
5152 "(but not more than %d KB/sec) for reconstruction.\n",
5155 is_mddev_idle(mddev
); /* this also initializes IO event counters */
5158 for (m
= 0; m
< SYNC_MARKS
; m
++) {
5160 mark_cnt
[m
] = io_sectors
;
5163 mddev
->resync_mark
= mark
[last_mark
];
5164 mddev
->resync_mark_cnt
= mark_cnt
[last_mark
];
5167 * Tune reconstruction:
5169 window
= 32*(PAGE_SIZE
/512);
5170 printk(KERN_INFO
"md: using %dk window, over a total of %llu blocks.\n",
5171 window
/2,(unsigned long long) max_sectors
/2);
5173 atomic_set(&mddev
->recovery_active
, 0);
5174 init_waitqueue_head(&mddev
->recovery_wait
);
5179 "md: resuming recovery of %s from checkpoint.\n",
5181 mddev
->curr_resync
= j
;
5184 while (j
< max_sectors
) {
5188 sectors
= mddev
->pers
->sync_request(mddev
, j
, &skipped
,
5189 currspeed
< speed_min(mddev
));
5191 set_bit(MD_RECOVERY_ERR
, &mddev
->recovery
);
5195 if (!skipped
) { /* actual IO requested */
5196 io_sectors
+= sectors
;
5197 atomic_add(sectors
, &mddev
->recovery_active
);
5201 if (j
>1) mddev
->curr_resync
= j
;
5202 if (last_check
== 0)
5203 /* this is the earliers that rebuilt will be
5204 * visible in /proc/mdstat
5206 md_new_event(mddev
);
5208 if (last_check
+ window
> io_sectors
|| j
== max_sectors
)
5211 last_check
= io_sectors
;
5213 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
) ||
5214 test_bit(MD_RECOVERY_ERR
, &mddev
->recovery
))
5218 if (time_after_eq(jiffies
, mark
[last_mark
] + SYNC_MARK_STEP
)) {
5220 int next
= (last_mark
+1) % SYNC_MARKS
;
5222 mddev
->resync_mark
= mark
[next
];
5223 mddev
->resync_mark_cnt
= mark_cnt
[next
];
5224 mark
[next
] = jiffies
;
5225 mark_cnt
[next
] = io_sectors
- atomic_read(&mddev
->recovery_active
);
5230 if (kthread_should_stop()) {
5232 * got a signal, exit.
5235 "md: md_do_sync() got signal ... exiting\n");
5236 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
5241 * this loop exits only if either when we are slower than
5242 * the 'hard' speed limit, or the system was IO-idle for
5244 * the system might be non-idle CPU-wise, but we only care
5245 * about not overloading the IO subsystem. (things like an
5246 * e2fsck being done on the RAID array should execute fast)
5248 mddev
->queue
->unplug_fn(mddev
->queue
);
5251 currspeed
= ((unsigned long)(io_sectors
-mddev
->resync_mark_cnt
))/2
5252 /((jiffies
-mddev
->resync_mark
)/HZ
+1) +1;
5254 if (currspeed
> speed_min(mddev
)) {
5255 if ((currspeed
> speed_max(mddev
)) ||
5256 !is_mddev_idle(mddev
)) {
5262 printk(KERN_INFO
"md: %s: sync done.\n",mdname(mddev
));
5264 * this also signals 'finished resyncing' to md_stop
5267 mddev
->queue
->unplug_fn(mddev
->queue
);
5269 wait_event(mddev
->recovery_wait
, !atomic_read(&mddev
->recovery_active
));
5271 /* tell personality that we are finished */
5272 mddev
->pers
->sync_request(mddev
, max_sectors
, &skipped
, 1);
5274 if (!test_bit(MD_RECOVERY_ERR
, &mddev
->recovery
) &&
5275 test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) &&
5276 !test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
) &&
5277 mddev
->curr_resync
> 2) {
5278 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
5279 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
5280 if (mddev
->curr_resync
>= mddev
->recovery_cp
) {
5282 "md: checkpointing recovery of %s.\n",
5284 mddev
->recovery_cp
= mddev
->curr_resync
;
5287 mddev
->recovery_cp
= MaxSector
;
5289 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
5290 mddev
->curr_resync
= MaxSector
;
5291 ITERATE_RDEV(mddev
,rdev
,rtmp
)
5292 if (rdev
->raid_disk
>= 0 &&
5293 !test_bit(Faulty
, &rdev
->flags
) &&
5294 !test_bit(In_sync
, &rdev
->flags
) &&
5295 rdev
->recovery_offset
< mddev
->curr_resync
)
5296 rdev
->recovery_offset
= mddev
->curr_resync
;
5297 mddev
->sb_dirty
= 1;
5302 mddev
->curr_resync
= 0;
5303 wake_up(&resync_wait
);
5304 set_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
5305 md_wakeup_thread(mddev
->thread
);
5307 EXPORT_SYMBOL_GPL(md_do_sync
);
5311 * This routine is regularly called by all per-raid-array threads to
5312 * deal with generic issues like resync and super-block update.
5313 * Raid personalities that don't have a thread (linear/raid0) do not
5314 * need this as they never do any recovery or update the superblock.
5316 * It does not do any resync itself, but rather "forks" off other threads
5317 * to do that as needed.
5318 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
5319 * "->recovery" and create a thread at ->sync_thread.
5320 * When the thread finishes it sets MD_RECOVERY_DONE (and might set MD_RECOVERY_ERR)
5321 * and wakeups up this thread which will reap the thread and finish up.
5322 * This thread also removes any faulty devices (with nr_pending == 0).
5324 * The overall approach is:
5325 * 1/ if the superblock needs updating, update it.
5326 * 2/ If a recovery thread is running, don't do anything else.
5327 * 3/ If recovery has finished, clean up, possibly marking spares active.
5328 * 4/ If there are any faulty devices, remove them.
5329 * 5/ If array is degraded, try to add spares devices
5330 * 6/ If array has spares or is not in-sync, start a resync thread.
5332 void md_check_recovery(mddev_t
*mddev
)
5335 struct list_head
*rtmp
;
5339 bitmap_daemon_work(mddev
->bitmap
);
5344 if (signal_pending(current
)) {
5345 if (mddev
->pers
->sync_request
) {
5346 printk(KERN_INFO
"md: %s in immediate safe mode\n",
5348 mddev
->safemode
= 2;
5350 flush_signals(current
);
5355 test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
) ||
5356 test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
) ||
5357 (mddev
->safemode
== 1) ||
5358 (mddev
->safemode
== 2 && ! atomic_read(&mddev
->writes_pending
)
5359 && !mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
)
5363 if (mddev_trylock(mddev
)) {
5366 spin_lock_irq(&mddev
->write_lock
);
5367 if (mddev
->safemode
&& !atomic_read(&mddev
->writes_pending
) &&
5368 !mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
) {
5370 mddev
->sb_dirty
= 3;
5372 if (mddev
->safemode
== 1)
5373 mddev
->safemode
= 0;
5374 spin_unlock_irq(&mddev
->write_lock
);
5376 if (mddev
->sb_dirty
)
5377 md_update_sb(mddev
);
5380 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) &&
5381 !test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
)) {
5382 /* resync/recovery still happening */
5383 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5386 if (mddev
->sync_thread
) {
5387 /* resync has finished, collect result */
5388 md_unregister_thread(mddev
->sync_thread
);
5389 mddev
->sync_thread
= NULL
;
5390 if (!test_bit(MD_RECOVERY_ERR
, &mddev
->recovery
) &&
5391 !test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
5393 /* activate any spares */
5394 mddev
->pers
->spare_active(mddev
);
5396 md_update_sb(mddev
);
5398 /* if array is no-longer degraded, then any saved_raid_disk
5399 * information must be scrapped
5401 if (!mddev
->degraded
)
5402 ITERATE_RDEV(mddev
,rdev
,rtmp
)
5403 rdev
->saved_raid_disk
= -1;
5405 mddev
->recovery
= 0;
5406 /* flag recovery needed just to double check */
5407 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5408 md_new_event(mddev
);
5411 /* Clear some bits that don't mean anything, but
5414 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5415 clear_bit(MD_RECOVERY_ERR
, &mddev
->recovery
);
5416 clear_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
5417 clear_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
5419 if (test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
))
5421 /* no recovery is running.
5422 * remove any failed drives, then
5423 * add spares if possible.
5424 * Spare are also removed and re-added, to allow
5425 * the personality to fail the re-add.
5427 ITERATE_RDEV(mddev
,rdev
,rtmp
)
5428 if (rdev
->raid_disk
>= 0 &&
5429 (test_bit(Faulty
, &rdev
->flags
) || ! test_bit(In_sync
, &rdev
->flags
)) &&
5430 atomic_read(&rdev
->nr_pending
)==0) {
5431 if (mddev
->pers
->hot_remove_disk(mddev
, rdev
->raid_disk
)==0) {
5433 sprintf(nm
,"rd%d", rdev
->raid_disk
);
5434 sysfs_remove_link(&mddev
->kobj
, nm
);
5435 rdev
->raid_disk
= -1;
5439 if (mddev
->degraded
) {
5440 ITERATE_RDEV(mddev
,rdev
,rtmp
)
5441 if (rdev
->raid_disk
< 0
5442 && !test_bit(Faulty
, &rdev
->flags
)) {
5443 rdev
->recovery_offset
= 0;
5444 if (mddev
->pers
->hot_add_disk(mddev
,rdev
)) {
5446 sprintf(nm
, "rd%d", rdev
->raid_disk
);
5447 sysfs_create_link(&mddev
->kobj
, &rdev
->kobj
, nm
);
5449 md_new_event(mddev
);
5456 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
5457 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
5458 } else if (mddev
->recovery_cp
< MaxSector
) {
5459 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
5460 } else if (!test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
5461 /* nothing to be done ... */
5464 if (mddev
->pers
->sync_request
) {
5465 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
5466 if (spares
&& mddev
->bitmap
&& ! mddev
->bitmap
->file
) {
5467 /* We are adding a device or devices to an array
5468 * which has the bitmap stored on all devices.
5469 * So make sure all bitmap pages get written
5471 bitmap_write_all(mddev
->bitmap
);
5473 mddev
->sync_thread
= md_register_thread(md_do_sync
,
5476 if (!mddev
->sync_thread
) {
5477 printk(KERN_ERR
"%s: could not start resync"
5480 /* leave the spares where they are, it shouldn't hurt */
5481 mddev
->recovery
= 0;
5483 md_wakeup_thread(mddev
->sync_thread
);
5484 md_new_event(mddev
);
5487 mddev_unlock(mddev
);
5491 static int md_notify_reboot(struct notifier_block
*this,
5492 unsigned long code
, void *x
)
5494 struct list_head
*tmp
;
5497 if ((code
== SYS_DOWN
) || (code
== SYS_HALT
) || (code
== SYS_POWER_OFF
)) {
5499 printk(KERN_INFO
"md: stopping all md devices.\n");
5501 ITERATE_MDDEV(mddev
,tmp
)
5502 if (mddev_trylock(mddev
)) {
5503 do_md_stop (mddev
, 1);
5504 mddev_unlock(mddev
);
5507 * certain more exotic SCSI devices are known to be
5508 * volatile wrt too early system reboots. While the
5509 * right place to handle this issue is the given
5510 * driver, we do want to have a safe RAID driver ...
5517 static struct notifier_block md_notifier
= {
5518 .notifier_call
= md_notify_reboot
,
5520 .priority
= INT_MAX
, /* before any real devices */
5523 static void md_geninit(void)
5525 struct proc_dir_entry
*p
;
5527 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t
));
5529 p
= create_proc_entry("mdstat", S_IRUGO
, NULL
);
5531 p
->proc_fops
= &md_seq_fops
;
5534 static int __init
md_init(void)
5536 printk(KERN_INFO
"md: md driver %d.%d.%d MAX_MD_DEVS=%d,"
5537 " MD_SB_DISKS=%d\n",
5538 MD_MAJOR_VERSION
, MD_MINOR_VERSION
,
5539 MD_PATCHLEVEL_VERSION
, MAX_MD_DEVS
, MD_SB_DISKS
);
5540 printk(KERN_INFO
"md: bitmap version %d.%d\n", BITMAP_MAJOR_HI
,
5543 if (register_blkdev(MAJOR_NR
, "md"))
5545 if ((mdp_major
=register_blkdev(0, "mdp"))<=0) {
5546 unregister_blkdev(MAJOR_NR
, "md");
5549 blk_register_region(MKDEV(MAJOR_NR
, 0), MAX_MD_DEVS
, THIS_MODULE
,
5550 md_probe
, NULL
, NULL
);
5551 blk_register_region(MKDEV(mdp_major
, 0), MAX_MD_DEVS
<<MdpMinorShift
, THIS_MODULE
,
5552 md_probe
, NULL
, NULL
);
5554 register_reboot_notifier(&md_notifier
);
5555 raid_table_header
= register_sysctl_table(raid_root_table
, 1);
5565 * Searches all registered partitions for autorun RAID arrays
5568 static dev_t detected_devices
[128];
5571 void md_autodetect_dev(dev_t dev
)
5573 if (dev_cnt
>= 0 && dev_cnt
< 127)
5574 detected_devices
[dev_cnt
++] = dev
;
5578 static void autostart_arrays(int part
)
5583 printk(KERN_INFO
"md: Autodetecting RAID arrays.\n");
5585 for (i
= 0; i
< dev_cnt
; i
++) {
5586 dev_t dev
= detected_devices
[i
];
5588 rdev
= md_import_device(dev
,0, 0);
5592 if (test_bit(Faulty
, &rdev
->flags
)) {
5596 list_add(&rdev
->same_set
, &pending_raid_disks
);
5600 autorun_devices(part
);
5605 static __exit
void md_exit(void)
5608 struct list_head
*tmp
;
5610 blk_unregister_region(MKDEV(MAJOR_NR
,0), MAX_MD_DEVS
);
5611 blk_unregister_region(MKDEV(mdp_major
,0), MAX_MD_DEVS
<< MdpMinorShift
);
5613 unregister_blkdev(MAJOR_NR
,"md");
5614 unregister_blkdev(mdp_major
, "mdp");
5615 unregister_reboot_notifier(&md_notifier
);
5616 unregister_sysctl_table(raid_table_header
);
5617 remove_proc_entry("mdstat", NULL
);
5618 ITERATE_MDDEV(mddev
,tmp
) {
5619 struct gendisk
*disk
= mddev
->gendisk
;
5622 export_array(mddev
);
5625 mddev
->gendisk
= NULL
;
5630 module_init(md_init
)
5631 module_exit(md_exit
)
5633 static int get_ro(char *buffer
, struct kernel_param
*kp
)
5635 return sprintf(buffer
, "%d", start_readonly
);
5637 static int set_ro(const char *val
, struct kernel_param
*kp
)
5640 int num
= simple_strtoul(val
, &e
, 10);
5641 if (*val
&& (*e
== '\0' || *e
== '\n')) {
5642 start_readonly
= num
;
5648 module_param_call(start_ro
, set_ro
, get_ro
, NULL
, 0600);
5649 module_param(start_dirty_degraded
, int, 0644);
5652 EXPORT_SYMBOL(register_md_personality
);
5653 EXPORT_SYMBOL(unregister_md_personality
);
5654 EXPORT_SYMBOL(md_error
);
5655 EXPORT_SYMBOL(md_done_sync
);
5656 EXPORT_SYMBOL(md_write_start
);
5657 EXPORT_SYMBOL(md_write_end
);
5658 EXPORT_SYMBOL(md_register_thread
);
5659 EXPORT_SYMBOL(md_unregister_thread
);
5660 EXPORT_SYMBOL(md_wakeup_thread
);
5661 EXPORT_SYMBOL(md_check_recovery
);
5662 MODULE_LICENSE("GPL");
5664 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR
);