Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | md.c : Multiple Devices driver for Linux |
| 3 | Copyright (C) 1998, 1999, 2000 Ingo Molnar |
| 4 | |
| 5 | completely rewritten, based on the MD driver code from Marc Zyngier |
| 6 | |
| 7 | Changes: |
| 8 | |
| 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> |
| 16 | |
| 17 | - lots of fixes and improvements to the RAID1/RAID5 and generic |
| 18 | RAID code (such as request based resynchronization): |
| 19 | |
| 20 | Neil Brown <neilb@cse.unsw.edu.au>. |
| 21 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 22 | - persistent bitmap code |
| 23 | Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc. |
| 24 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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) |
| 28 | any later version. |
| 29 | |
| 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. |
| 33 | */ |
| 34 | |
| 35 | #include <linux/module.h> |
| 36 | #include <linux/config.h> |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 37 | #include <linux/kthread.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #include <linux/linkage.h> |
| 39 | #include <linux/raid/md.h> |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 40 | #include <linux/raid/bitmap.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #include <linux/sysctl.h> |
| 42 | #include <linux/devfs_fs_kernel.h> |
| 43 | #include <linux/buffer_head.h> /* for invalidate_bdev */ |
| 44 | #include <linux/suspend.h> |
| 45 | |
| 46 | #include <linux/init.h> |
| 47 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 48 | #include <linux/file.h> |
| 49 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | #ifdef CONFIG_KMOD |
| 51 | #include <linux/kmod.h> |
| 52 | #endif |
| 53 | |
| 54 | #include <asm/unaligned.h> |
| 55 | |
| 56 | #define MAJOR_NR MD_MAJOR |
| 57 | #define MD_DRIVER |
| 58 | |
| 59 | /* 63 partitions with the alternate major number (mdp) */ |
| 60 | #define MdpMinorShift 6 |
| 61 | |
| 62 | #define DEBUG 0 |
| 63 | #define dprintk(x...) ((void)(DEBUG && printk(x))) |
| 64 | |
| 65 | |
| 66 | #ifndef MODULE |
| 67 | static void autostart_arrays (int part); |
| 68 | #endif |
| 69 | |
| 70 | static mdk_personality_t *pers[MAX_PERSONALITY]; |
| 71 | static DEFINE_SPINLOCK(pers_lock); |
| 72 | |
| 73 | /* |
| 74 | * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit' |
| 75 | * is 1000 KB/sec, so the extra system load does not show up that much. |
| 76 | * Increase it if you want to have more _guaranteed_ speed. Note that |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 77 | * the RAID driver will use the maximum available bandwidth if the IO |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | * subsystem is idle. There is also an 'absolute maximum' reconstruction |
| 79 | * speed limit - in case reconstruction slows down your system despite |
| 80 | * idle IO detection. |
| 81 | * |
| 82 | * you can change it via /proc/sys/dev/raid/speed_limit_min and _max. |
| 83 | */ |
| 84 | |
| 85 | static int sysctl_speed_limit_min = 1000; |
| 86 | static int sysctl_speed_limit_max = 200000; |
| 87 | |
| 88 | static struct ctl_table_header *raid_table_header; |
| 89 | |
| 90 | static ctl_table raid_table[] = { |
| 91 | { |
| 92 | .ctl_name = DEV_RAID_SPEED_LIMIT_MIN, |
| 93 | .procname = "speed_limit_min", |
| 94 | .data = &sysctl_speed_limit_min, |
| 95 | .maxlen = sizeof(int), |
| 96 | .mode = 0644, |
| 97 | .proc_handler = &proc_dointvec, |
| 98 | }, |
| 99 | { |
| 100 | .ctl_name = DEV_RAID_SPEED_LIMIT_MAX, |
| 101 | .procname = "speed_limit_max", |
| 102 | .data = &sysctl_speed_limit_max, |
| 103 | .maxlen = sizeof(int), |
| 104 | .mode = 0644, |
| 105 | .proc_handler = &proc_dointvec, |
| 106 | }, |
| 107 | { .ctl_name = 0 } |
| 108 | }; |
| 109 | |
| 110 | static ctl_table raid_dir_table[] = { |
| 111 | { |
| 112 | .ctl_name = DEV_RAID, |
| 113 | .procname = "raid", |
| 114 | .maxlen = 0, |
| 115 | .mode = 0555, |
| 116 | .child = raid_table, |
| 117 | }, |
| 118 | { .ctl_name = 0 } |
| 119 | }; |
| 120 | |
| 121 | static ctl_table raid_root_table[] = { |
| 122 | { |
| 123 | .ctl_name = CTL_DEV, |
| 124 | .procname = "dev", |
| 125 | .maxlen = 0, |
| 126 | .mode = 0555, |
| 127 | .child = raid_dir_table, |
| 128 | }, |
| 129 | { .ctl_name = 0 } |
| 130 | }; |
| 131 | |
| 132 | static struct block_device_operations md_fops; |
| 133 | |
| 134 | /* |
| 135 | * Enables to iterate over all existing md arrays |
| 136 | * all_mddevs_lock protects this list. |
| 137 | */ |
| 138 | static LIST_HEAD(all_mddevs); |
| 139 | static DEFINE_SPINLOCK(all_mddevs_lock); |
| 140 | |
| 141 | |
| 142 | /* |
| 143 | * iterates through all used mddevs in the system. |
| 144 | * We take care to grab the all_mddevs_lock whenever navigating |
| 145 | * the list, and to always hold a refcount when unlocked. |
| 146 | * Any code which breaks out of this loop while own |
| 147 | * a reference to the current mddev and must mddev_put it. |
| 148 | */ |
| 149 | #define ITERATE_MDDEV(mddev,tmp) \ |
| 150 | \ |
| 151 | for (({ spin_lock(&all_mddevs_lock); \ |
| 152 | tmp = all_mddevs.next; \ |
| 153 | mddev = NULL;}); \ |
| 154 | ({ if (tmp != &all_mddevs) \ |
| 155 | mddev_get(list_entry(tmp, mddev_t, all_mddevs));\ |
| 156 | spin_unlock(&all_mddevs_lock); \ |
| 157 | if (mddev) mddev_put(mddev); \ |
| 158 | mddev = list_entry(tmp, mddev_t, all_mddevs); \ |
| 159 | tmp != &all_mddevs;}); \ |
| 160 | ({ spin_lock(&all_mddevs_lock); \ |
| 161 | tmp = tmp->next;}) \ |
| 162 | ) |
| 163 | |
| 164 | |
| 165 | static int md_fail_request (request_queue_t *q, struct bio *bio) |
| 166 | { |
| 167 | bio_io_error(bio, bio->bi_size); |
| 168 | return 0; |
| 169 | } |
| 170 | |
| 171 | static inline mddev_t *mddev_get(mddev_t *mddev) |
| 172 | { |
| 173 | atomic_inc(&mddev->active); |
| 174 | return mddev; |
| 175 | } |
| 176 | |
| 177 | static void mddev_put(mddev_t *mddev) |
| 178 | { |
| 179 | if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock)) |
| 180 | return; |
| 181 | if (!mddev->raid_disks && list_empty(&mddev->disks)) { |
| 182 | list_del(&mddev->all_mddevs); |
| 183 | blk_put_queue(mddev->queue); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 184 | kobject_unregister(&mddev->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | } |
| 186 | spin_unlock(&all_mddevs_lock); |
| 187 | } |
| 188 | |
| 189 | static mddev_t * mddev_find(dev_t unit) |
| 190 | { |
| 191 | mddev_t *mddev, *new = NULL; |
| 192 | |
| 193 | retry: |
| 194 | spin_lock(&all_mddevs_lock); |
| 195 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 196 | if (mddev->unit == unit) { |
| 197 | mddev_get(mddev); |
| 198 | spin_unlock(&all_mddevs_lock); |
Jesper Juhl | 990a8ba | 2005-06-21 17:17:30 -0700 | [diff] [blame] | 199 | kfree(new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | return mddev; |
| 201 | } |
| 202 | |
| 203 | if (new) { |
| 204 | list_add(&new->all_mddevs, &all_mddevs); |
| 205 | spin_unlock(&all_mddevs_lock); |
| 206 | return new; |
| 207 | } |
| 208 | spin_unlock(&all_mddevs_lock); |
| 209 | |
| 210 | new = (mddev_t *) kmalloc(sizeof(*new), GFP_KERNEL); |
| 211 | if (!new) |
| 212 | return NULL; |
| 213 | |
| 214 | memset(new, 0, sizeof(*new)); |
| 215 | |
| 216 | new->unit = unit; |
| 217 | if (MAJOR(unit) == MD_MAJOR) |
| 218 | new->md_minor = MINOR(unit); |
| 219 | else |
| 220 | new->md_minor = MINOR(unit) >> MdpMinorShift; |
| 221 | |
| 222 | init_MUTEX(&new->reconfig_sem); |
| 223 | INIT_LIST_HEAD(&new->disks); |
| 224 | INIT_LIST_HEAD(&new->all_mddevs); |
| 225 | init_timer(&new->safemode_timer); |
| 226 | atomic_set(&new->active, 1); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 227 | spin_lock_init(&new->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 228 | init_waitqueue_head(&new->sb_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | |
| 230 | new->queue = blk_alloc_queue(GFP_KERNEL); |
| 231 | if (!new->queue) { |
| 232 | kfree(new); |
| 233 | return NULL; |
| 234 | } |
| 235 | |
| 236 | blk_queue_make_request(new->queue, md_fail_request); |
| 237 | |
| 238 | goto retry; |
| 239 | } |
| 240 | |
| 241 | static inline int mddev_lock(mddev_t * mddev) |
| 242 | { |
| 243 | return down_interruptible(&mddev->reconfig_sem); |
| 244 | } |
| 245 | |
| 246 | static inline void mddev_lock_uninterruptible(mddev_t * mddev) |
| 247 | { |
| 248 | down(&mddev->reconfig_sem); |
| 249 | } |
| 250 | |
| 251 | static inline int mddev_trylock(mddev_t * mddev) |
| 252 | { |
| 253 | return down_trylock(&mddev->reconfig_sem); |
| 254 | } |
| 255 | |
| 256 | static inline void mddev_unlock(mddev_t * mddev) |
| 257 | { |
| 258 | up(&mddev->reconfig_sem); |
| 259 | |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 260 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr) |
| 264 | { |
| 265 | mdk_rdev_t * rdev; |
| 266 | struct list_head *tmp; |
| 267 | |
| 268 | ITERATE_RDEV(mddev,rdev,tmp) { |
| 269 | if (rdev->desc_nr == nr) |
| 270 | return rdev; |
| 271 | } |
| 272 | return NULL; |
| 273 | } |
| 274 | |
| 275 | static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev) |
| 276 | { |
| 277 | struct list_head *tmp; |
| 278 | mdk_rdev_t *rdev; |
| 279 | |
| 280 | ITERATE_RDEV(mddev,rdev,tmp) { |
| 281 | if (rdev->bdev->bd_dev == dev) |
| 282 | return rdev; |
| 283 | } |
| 284 | return NULL; |
| 285 | } |
| 286 | |
Jesper Juhl | 77933d7 | 2005-07-27 11:46:09 -0700 | [diff] [blame] | 287 | static inline sector_t calc_dev_sboffset(struct block_device *bdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | { |
| 289 | sector_t size = bdev->bd_inode->i_size >> BLOCK_SIZE_BITS; |
| 290 | return MD_NEW_SIZE_BLOCKS(size); |
| 291 | } |
| 292 | |
| 293 | static sector_t calc_dev_size(mdk_rdev_t *rdev, unsigned chunk_size) |
| 294 | { |
| 295 | sector_t size; |
| 296 | |
| 297 | size = rdev->sb_offset; |
| 298 | |
| 299 | if (chunk_size) |
| 300 | size &= ~((sector_t)chunk_size/1024 - 1); |
| 301 | return size; |
| 302 | } |
| 303 | |
| 304 | static int alloc_disk_sb(mdk_rdev_t * rdev) |
| 305 | { |
| 306 | if (rdev->sb_page) |
| 307 | MD_BUG(); |
| 308 | |
| 309 | rdev->sb_page = alloc_page(GFP_KERNEL); |
| 310 | if (!rdev->sb_page) { |
| 311 | printk(KERN_ALERT "md: out of memory.\n"); |
| 312 | return -EINVAL; |
| 313 | } |
| 314 | |
| 315 | return 0; |
| 316 | } |
| 317 | |
| 318 | static void free_disk_sb(mdk_rdev_t * rdev) |
| 319 | { |
| 320 | if (rdev->sb_page) { |
| 321 | page_cache_release(rdev->sb_page); |
| 322 | rdev->sb_loaded = 0; |
| 323 | rdev->sb_page = NULL; |
| 324 | rdev->sb_offset = 0; |
| 325 | rdev->size = 0; |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 330 | static int super_written(struct bio *bio, unsigned int bytes_done, int error) |
| 331 | { |
| 332 | mdk_rdev_t *rdev = bio->bi_private; |
| 333 | if (bio->bi_size) |
| 334 | return 1; |
| 335 | |
| 336 | if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) |
| 337 | md_error(rdev->mddev, rdev); |
| 338 | |
| 339 | if (atomic_dec_and_test(&rdev->mddev->pending_writes)) |
| 340 | wake_up(&rdev->mddev->sb_wait); |
Neil Brown | f8b58ed | 2005-06-27 22:29:34 -0700 | [diff] [blame] | 341 | bio_put(bio); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 342 | return 0; |
| 343 | } |
| 344 | |
| 345 | void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev, |
| 346 | sector_t sector, int size, struct page *page) |
| 347 | { |
| 348 | /* write first size bytes of page to sector of rdev |
| 349 | * Increment mddev->pending_writes before returning |
| 350 | * and decrement it on completion, waking up sb_wait |
| 351 | * if zero is reached. |
| 352 | * If an error occurred, call md_error |
| 353 | */ |
| 354 | struct bio *bio = bio_alloc(GFP_NOIO, 1); |
| 355 | |
| 356 | bio->bi_bdev = rdev->bdev; |
| 357 | bio->bi_sector = sector; |
| 358 | bio_add_page(bio, page, size, 0); |
| 359 | bio->bi_private = rdev; |
| 360 | bio->bi_end_io = super_written; |
| 361 | atomic_inc(&mddev->pending_writes); |
| 362 | submit_bio((1<<BIO_RW)|(1<<BIO_RW_SYNC), bio); |
| 363 | } |
| 364 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | static int bi_complete(struct bio *bio, unsigned int bytes_done, int error) |
| 366 | { |
| 367 | if (bio->bi_size) |
| 368 | return 1; |
| 369 | |
| 370 | complete((struct completion*)bio->bi_private); |
| 371 | return 0; |
| 372 | } |
| 373 | |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 374 | int sync_page_io(struct block_device *bdev, sector_t sector, int size, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | struct page *page, int rw) |
| 376 | { |
Neil Brown | baaa2c5 | 2005-04-16 15:23:54 -0700 | [diff] [blame] | 377 | struct bio *bio = bio_alloc(GFP_NOIO, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | struct completion event; |
| 379 | int ret; |
| 380 | |
| 381 | rw |= (1 << BIO_RW_SYNC); |
| 382 | |
| 383 | bio->bi_bdev = bdev; |
| 384 | bio->bi_sector = sector; |
| 385 | bio_add_page(bio, page, size, 0); |
| 386 | init_completion(&event); |
| 387 | bio->bi_private = &event; |
| 388 | bio->bi_end_io = bi_complete; |
| 389 | submit_bio(rw, bio); |
| 390 | wait_for_completion(&event); |
| 391 | |
| 392 | ret = test_bit(BIO_UPTODATE, &bio->bi_flags); |
| 393 | bio_put(bio); |
| 394 | return ret; |
| 395 | } |
| 396 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 397 | static int read_disk_sb(mdk_rdev_t * rdev, int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | { |
| 399 | char b[BDEVNAME_SIZE]; |
| 400 | if (!rdev->sb_page) { |
| 401 | MD_BUG(); |
| 402 | return -EINVAL; |
| 403 | } |
| 404 | if (rdev->sb_loaded) |
| 405 | return 0; |
| 406 | |
| 407 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 408 | if (!sync_page_io(rdev->bdev, rdev->sb_offset<<1, size, rdev->sb_page, READ)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | goto fail; |
| 410 | rdev->sb_loaded = 1; |
| 411 | return 0; |
| 412 | |
| 413 | fail: |
| 414 | printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n", |
| 415 | bdevname(rdev->bdev,b)); |
| 416 | return -EINVAL; |
| 417 | } |
| 418 | |
| 419 | static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 420 | { |
| 421 | if ( (sb1->set_uuid0 == sb2->set_uuid0) && |
| 422 | (sb1->set_uuid1 == sb2->set_uuid1) && |
| 423 | (sb1->set_uuid2 == sb2->set_uuid2) && |
| 424 | (sb1->set_uuid3 == sb2->set_uuid3)) |
| 425 | |
| 426 | return 1; |
| 427 | |
| 428 | return 0; |
| 429 | } |
| 430 | |
| 431 | |
| 432 | static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 433 | { |
| 434 | int ret; |
| 435 | mdp_super_t *tmp1, *tmp2; |
| 436 | |
| 437 | tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL); |
| 438 | tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL); |
| 439 | |
| 440 | if (!tmp1 || !tmp2) { |
| 441 | ret = 0; |
| 442 | printk(KERN_INFO "md.c: sb1 is not equal to sb2!\n"); |
| 443 | goto abort; |
| 444 | } |
| 445 | |
| 446 | *tmp1 = *sb1; |
| 447 | *tmp2 = *sb2; |
| 448 | |
| 449 | /* |
| 450 | * nr_disks is not constant |
| 451 | */ |
| 452 | tmp1->nr_disks = 0; |
| 453 | tmp2->nr_disks = 0; |
| 454 | |
| 455 | if (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4)) |
| 456 | ret = 0; |
| 457 | else |
| 458 | ret = 1; |
| 459 | |
| 460 | abort: |
Jesper Juhl | 990a8ba | 2005-06-21 17:17:30 -0700 | [diff] [blame] | 461 | kfree(tmp1); |
| 462 | kfree(tmp2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | return ret; |
| 464 | } |
| 465 | |
| 466 | static unsigned int calc_sb_csum(mdp_super_t * sb) |
| 467 | { |
| 468 | unsigned int disk_csum, csum; |
| 469 | |
| 470 | disk_csum = sb->sb_csum; |
| 471 | sb->sb_csum = 0; |
| 472 | csum = csum_partial((void *)sb, MD_SB_BYTES, 0); |
| 473 | sb->sb_csum = disk_csum; |
| 474 | return csum; |
| 475 | } |
| 476 | |
| 477 | |
| 478 | /* |
| 479 | * Handle superblock details. |
| 480 | * We want to be able to handle multiple superblock formats |
| 481 | * so we have a common interface to them all, and an array of |
| 482 | * different handlers. |
| 483 | * We rely on user-space to write the initial superblock, and support |
| 484 | * reading and updating of superblocks. |
| 485 | * Interface methods are: |
| 486 | * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version) |
| 487 | * loads and validates a superblock on dev. |
| 488 | * if refdev != NULL, compare superblocks on both devices |
| 489 | * Return: |
| 490 | * 0 - dev has a superblock that is compatible with refdev |
| 491 | * 1 - dev has a superblock that is compatible and newer than refdev |
| 492 | * so dev should be used as the refdev in future |
| 493 | * -EINVAL superblock incompatible or invalid |
| 494 | * -othererror e.g. -EIO |
| 495 | * |
| 496 | * int validate_super(mddev_t *mddev, mdk_rdev_t *dev) |
| 497 | * Verify that dev is acceptable into mddev. |
| 498 | * The first time, mddev->raid_disks will be 0, and data from |
| 499 | * dev should be merged in. Subsequent calls check that dev |
| 500 | * is new enough. Return 0 or -EINVAL |
| 501 | * |
| 502 | * void sync_super(mddev_t *mddev, mdk_rdev_t *dev) |
| 503 | * Update the superblock for rdev with data in mddev |
| 504 | * This does not write to disc. |
| 505 | * |
| 506 | */ |
| 507 | |
| 508 | struct super_type { |
| 509 | char *name; |
| 510 | struct module *owner; |
| 511 | int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version); |
| 512 | int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev); |
| 513 | void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev); |
| 514 | }; |
| 515 | |
| 516 | /* |
| 517 | * load_super for 0.90.0 |
| 518 | */ |
| 519 | static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version) |
| 520 | { |
| 521 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 522 | mdp_super_t *sb; |
| 523 | int ret; |
| 524 | sector_t sb_offset; |
| 525 | |
| 526 | /* |
| 527 | * Calculate the position of the superblock, |
| 528 | * it's at the end of the disk. |
| 529 | * |
| 530 | * It also happens to be a multiple of 4Kb. |
| 531 | */ |
| 532 | sb_offset = calc_dev_sboffset(rdev->bdev); |
| 533 | rdev->sb_offset = sb_offset; |
| 534 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 535 | ret = read_disk_sb(rdev, MD_SB_BYTES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | if (ret) return ret; |
| 537 | |
| 538 | ret = -EINVAL; |
| 539 | |
| 540 | bdevname(rdev->bdev, b); |
| 541 | sb = (mdp_super_t*)page_address(rdev->sb_page); |
| 542 | |
| 543 | if (sb->md_magic != MD_SB_MAGIC) { |
| 544 | printk(KERN_ERR "md: invalid raid superblock magic on %s\n", |
| 545 | b); |
| 546 | goto abort; |
| 547 | } |
| 548 | |
| 549 | if (sb->major_version != 0 || |
| 550 | sb->minor_version != 90) { |
| 551 | printk(KERN_WARNING "Bad version number %d.%d on %s\n", |
| 552 | sb->major_version, sb->minor_version, |
| 553 | b); |
| 554 | goto abort; |
| 555 | } |
| 556 | |
| 557 | if (sb->raid_disks <= 0) |
| 558 | goto abort; |
| 559 | |
| 560 | if (csum_fold(calc_sb_csum(sb)) != csum_fold(sb->sb_csum)) { |
| 561 | printk(KERN_WARNING "md: invalid superblock checksum on %s\n", |
| 562 | b); |
| 563 | goto abort; |
| 564 | } |
| 565 | |
| 566 | rdev->preferred_minor = sb->md_minor; |
| 567 | rdev->data_offset = 0; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 568 | rdev->sb_size = MD_SB_BYTES; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | |
| 570 | if (sb->level == LEVEL_MULTIPATH) |
| 571 | rdev->desc_nr = -1; |
| 572 | else |
| 573 | rdev->desc_nr = sb->this_disk.number; |
| 574 | |
| 575 | if (refdev == 0) |
| 576 | ret = 1; |
| 577 | else { |
| 578 | __u64 ev1, ev2; |
| 579 | mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page); |
| 580 | if (!uuid_equal(refsb, sb)) { |
| 581 | printk(KERN_WARNING "md: %s has different UUID to %s\n", |
| 582 | b, bdevname(refdev->bdev,b2)); |
| 583 | goto abort; |
| 584 | } |
| 585 | if (!sb_equal(refsb, sb)) { |
| 586 | printk(KERN_WARNING "md: %s has same UUID" |
| 587 | " but different superblock to %s\n", |
| 588 | b, bdevname(refdev->bdev, b2)); |
| 589 | goto abort; |
| 590 | } |
| 591 | ev1 = md_event(sb); |
| 592 | ev2 = md_event(refsb); |
| 593 | if (ev1 > ev2) |
| 594 | ret = 1; |
| 595 | else |
| 596 | ret = 0; |
| 597 | } |
| 598 | rdev->size = calc_dev_size(rdev, sb->chunk_size); |
| 599 | |
| 600 | abort: |
| 601 | return ret; |
| 602 | } |
| 603 | |
| 604 | /* |
| 605 | * validate_super for 0.90.0 |
| 606 | */ |
| 607 | static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev) |
| 608 | { |
| 609 | mdp_disk_t *desc; |
| 610 | mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page); |
| 611 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 612 | rdev->raid_disk = -1; |
| 613 | rdev->in_sync = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | if (mddev->raid_disks == 0) { |
| 615 | mddev->major_version = 0; |
| 616 | mddev->minor_version = sb->minor_version; |
| 617 | mddev->patch_version = sb->patch_version; |
| 618 | mddev->persistent = ! sb->not_persistent; |
| 619 | mddev->chunk_size = sb->chunk_size; |
| 620 | mddev->ctime = sb->ctime; |
| 621 | mddev->utime = sb->utime; |
| 622 | mddev->level = sb->level; |
| 623 | mddev->layout = sb->layout; |
| 624 | mddev->raid_disks = sb->raid_disks; |
| 625 | mddev->size = sb->size; |
| 626 | mddev->events = md_event(sb); |
NeilBrown | 9223214 | 2005-08-18 11:24:16 -0700 | [diff] [blame] | 627 | mddev->bitmap_offset = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 628 | mddev->default_bitmap_offset = MD_SB_BYTES >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | |
| 630 | if (sb->state & (1<<MD_SB_CLEAN)) |
| 631 | mddev->recovery_cp = MaxSector; |
| 632 | else { |
| 633 | if (sb->events_hi == sb->cp_events_hi && |
| 634 | sb->events_lo == sb->cp_events_lo) { |
| 635 | mddev->recovery_cp = sb->recovery_cp; |
| 636 | } else |
| 637 | mddev->recovery_cp = 0; |
| 638 | } |
| 639 | |
| 640 | memcpy(mddev->uuid+0, &sb->set_uuid0, 4); |
| 641 | memcpy(mddev->uuid+4, &sb->set_uuid1, 4); |
| 642 | memcpy(mddev->uuid+8, &sb->set_uuid2, 4); |
| 643 | memcpy(mddev->uuid+12,&sb->set_uuid3, 4); |
| 644 | |
| 645 | mddev->max_disks = MD_SB_DISKS; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 646 | |
| 647 | if (sb->state & (1<<MD_SB_BITMAP_PRESENT) && |
| 648 | mddev->bitmap_file == NULL) { |
NeilBrown | 934ce7c | 2005-09-09 16:23:55 -0700 | [diff] [blame] | 649 | if (mddev->level != 1 && mddev->level != 5 && mddev->level != 6) { |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 650 | /* FIXME use a better test */ |
| 651 | printk(KERN_WARNING "md: bitmaps only support for raid1\n"); |
| 652 | return -EINVAL; |
| 653 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 654 | mddev->bitmap_offset = mddev->default_bitmap_offset; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 655 | } |
| 656 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 657 | } else if (mddev->pers == NULL) { |
| 658 | /* Insist on good event counter while assembling */ |
| 659 | __u64 ev1 = md_event(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | ++ev1; |
| 661 | if (ev1 < mddev->events) |
| 662 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 663 | } else if (mddev->bitmap) { |
| 664 | /* if adding to array with a bitmap, then we can accept an |
| 665 | * older device ... but not too old. |
| 666 | */ |
| 667 | __u64 ev1 = md_event(sb); |
| 668 | if (ev1 < mddev->bitmap->events_cleared) |
| 669 | return 0; |
| 670 | } else /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 671 | return 0; |
| 672 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | if (mddev->level != LEVEL_MULTIPATH) { |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 674 | rdev->faulty = 0; |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 675 | rdev->flags = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | desc = sb->disks + rdev->desc_nr; |
| 677 | |
| 678 | if (desc->state & (1<<MD_DISK_FAULTY)) |
| 679 | rdev->faulty = 1; |
| 680 | else if (desc->state & (1<<MD_DISK_SYNC) && |
| 681 | desc->raid_disk < mddev->raid_disks) { |
| 682 | rdev->in_sync = 1; |
| 683 | rdev->raid_disk = desc->raid_disk; |
| 684 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 685 | if (desc->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 686 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 687 | } else /* MULTIPATH are always insync */ |
| 688 | rdev->in_sync = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | return 0; |
| 690 | } |
| 691 | |
| 692 | /* |
| 693 | * sync_super for 0.90.0 |
| 694 | */ |
| 695 | static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev) |
| 696 | { |
| 697 | mdp_super_t *sb; |
| 698 | struct list_head *tmp; |
| 699 | mdk_rdev_t *rdev2; |
| 700 | int next_spare = mddev->raid_disks; |
| 701 | |
| 702 | /* make rdev->sb match mddev data.. |
| 703 | * |
| 704 | * 1/ zero out disks |
| 705 | * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare); |
| 706 | * 3/ any empty disks < next_spare become removed |
| 707 | * |
| 708 | * disks[0] gets initialised to REMOVED because |
| 709 | * we cannot be sure from other fields if it has |
| 710 | * been initialised or not. |
| 711 | */ |
| 712 | int i; |
| 713 | int active=0, working=0,failed=0,spare=0,nr_disks=0; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 714 | unsigned int fixdesc=0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | |
NeilBrown | 6118156 | 2005-09-09 16:24:02 -0700 | [diff] [blame] | 716 | rdev->sb_size = MD_SB_BYTES; |
| 717 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | sb = (mdp_super_t*)page_address(rdev->sb_page); |
| 719 | |
| 720 | memset(sb, 0, sizeof(*sb)); |
| 721 | |
| 722 | sb->md_magic = MD_SB_MAGIC; |
| 723 | sb->major_version = mddev->major_version; |
| 724 | sb->minor_version = mddev->minor_version; |
| 725 | sb->patch_version = mddev->patch_version; |
| 726 | sb->gvalid_words = 0; /* ignored */ |
| 727 | memcpy(&sb->set_uuid0, mddev->uuid+0, 4); |
| 728 | memcpy(&sb->set_uuid1, mddev->uuid+4, 4); |
| 729 | memcpy(&sb->set_uuid2, mddev->uuid+8, 4); |
| 730 | memcpy(&sb->set_uuid3, mddev->uuid+12,4); |
| 731 | |
| 732 | sb->ctime = mddev->ctime; |
| 733 | sb->level = mddev->level; |
| 734 | sb->size = mddev->size; |
| 735 | sb->raid_disks = mddev->raid_disks; |
| 736 | sb->md_minor = mddev->md_minor; |
| 737 | sb->not_persistent = !mddev->persistent; |
| 738 | sb->utime = mddev->utime; |
| 739 | sb->state = 0; |
| 740 | sb->events_hi = (mddev->events>>32); |
| 741 | sb->events_lo = (u32)mddev->events; |
| 742 | |
| 743 | if (mddev->in_sync) |
| 744 | { |
| 745 | sb->recovery_cp = mddev->recovery_cp; |
| 746 | sb->cp_events_hi = (mddev->events>>32); |
| 747 | sb->cp_events_lo = (u32)mddev->events; |
| 748 | if (mddev->recovery_cp == MaxSector) |
| 749 | sb->state = (1<< MD_SB_CLEAN); |
| 750 | } else |
| 751 | sb->recovery_cp = 0; |
| 752 | |
| 753 | sb->layout = mddev->layout; |
| 754 | sb->chunk_size = mddev->chunk_size; |
| 755 | |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 756 | if (mddev->bitmap && mddev->bitmap_file == NULL) |
| 757 | sb->state |= (1<<MD_SB_BITMAP_PRESENT); |
| 758 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | sb->disks[0].state = (1<<MD_DISK_REMOVED); |
| 760 | ITERATE_RDEV(mddev,rdev2,tmp) { |
| 761 | mdp_disk_t *d; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 762 | int desc_nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | if (rdev2->raid_disk >= 0 && rdev2->in_sync && !rdev2->faulty) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 764 | desc_nr = rdev2->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | else |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 766 | desc_nr = next_spare++; |
| 767 | if (desc_nr != rdev2->desc_nr) { |
| 768 | fixdesc |= (1 << desc_nr); |
| 769 | rdev2->desc_nr = desc_nr; |
| 770 | if (rdev2->raid_disk >= 0) { |
| 771 | char nm[20]; |
| 772 | sprintf(nm, "rd%d", rdev2->raid_disk); |
| 773 | sysfs_remove_link(&mddev->kobj, nm); |
| 774 | } |
| 775 | sysfs_remove_link(&rdev2->kobj, "block"); |
| 776 | kobject_del(&rdev2->kobj); |
| 777 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | d = &sb->disks[rdev2->desc_nr]; |
| 779 | nr_disks++; |
| 780 | d->number = rdev2->desc_nr; |
| 781 | d->major = MAJOR(rdev2->bdev->bd_dev); |
| 782 | d->minor = MINOR(rdev2->bdev->bd_dev); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 783 | if (rdev2->raid_disk >= 0 && rdev2->in_sync && !rdev2->faulty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | d->raid_disk = rdev2->raid_disk; |
| 785 | else |
| 786 | d->raid_disk = rdev2->desc_nr; /* compatibility */ |
| 787 | if (rdev2->faulty) { |
| 788 | d->state = (1<<MD_DISK_FAULTY); |
| 789 | failed++; |
| 790 | } else if (rdev2->in_sync) { |
| 791 | d->state = (1<<MD_DISK_ACTIVE); |
| 792 | d->state |= (1<<MD_DISK_SYNC); |
| 793 | active++; |
| 794 | working++; |
| 795 | } else { |
| 796 | d->state = 0; |
| 797 | spare++; |
| 798 | working++; |
| 799 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 800 | if (test_bit(WriteMostly, &rdev2->flags)) |
| 801 | d->state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | } |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 803 | if (fixdesc) |
| 804 | ITERATE_RDEV(mddev,rdev2,tmp) |
| 805 | if (fixdesc & (1<<rdev2->desc_nr)) { |
| 806 | snprintf(rdev2->kobj.name, KOBJ_NAME_LEN, "dev%d", |
| 807 | rdev2->desc_nr); |
| 808 | kobject_add(&rdev2->kobj); |
| 809 | sysfs_create_link(&rdev2->kobj, |
| 810 | &rdev2->bdev->bd_disk->kobj, |
| 811 | "block"); |
| 812 | if (rdev2->raid_disk >= 0) { |
| 813 | char nm[20]; |
| 814 | sprintf(nm, "rd%d", rdev2->raid_disk); |
| 815 | sysfs_create_link(&mddev->kobj, |
| 816 | &rdev2->kobj, nm); |
| 817 | } |
| 818 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 819 | /* now set the "removed" and "faulty" bits on any missing devices */ |
| 820 | for (i=0 ; i < mddev->raid_disks ; i++) { |
| 821 | mdp_disk_t *d = &sb->disks[i]; |
| 822 | if (d->state == 0 && d->number == 0) { |
| 823 | d->number = i; |
| 824 | d->raid_disk = i; |
| 825 | d->state = (1<<MD_DISK_REMOVED); |
| 826 | d->state |= (1<<MD_DISK_FAULTY); |
| 827 | failed++; |
| 828 | } |
| 829 | } |
| 830 | sb->nr_disks = nr_disks; |
| 831 | sb->active_disks = active; |
| 832 | sb->working_disks = working; |
| 833 | sb->failed_disks = failed; |
| 834 | sb->spare_disks = spare; |
| 835 | |
| 836 | sb->this_disk = sb->disks[rdev->desc_nr]; |
| 837 | sb->sb_csum = calc_sb_csum(sb); |
| 838 | } |
| 839 | |
| 840 | /* |
| 841 | * version 1 superblock |
| 842 | */ |
| 843 | |
| 844 | static unsigned int calc_sb_1_csum(struct mdp_superblock_1 * sb) |
| 845 | { |
| 846 | unsigned int disk_csum, csum; |
| 847 | unsigned long long newcsum; |
| 848 | int size = 256 + le32_to_cpu(sb->max_dev)*2; |
| 849 | unsigned int *isuper = (unsigned int*)sb; |
| 850 | int i; |
| 851 | |
| 852 | disk_csum = sb->sb_csum; |
| 853 | sb->sb_csum = 0; |
| 854 | newcsum = 0; |
| 855 | for (i=0; size>=4; size -= 4 ) |
| 856 | newcsum += le32_to_cpu(*isuper++); |
| 857 | |
| 858 | if (size == 2) |
| 859 | newcsum += le16_to_cpu(*(unsigned short*) isuper); |
| 860 | |
| 861 | csum = (newcsum & 0xffffffff) + (newcsum >> 32); |
| 862 | sb->sb_csum = disk_csum; |
| 863 | return cpu_to_le32(csum); |
| 864 | } |
| 865 | |
| 866 | static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version) |
| 867 | { |
| 868 | struct mdp_superblock_1 *sb; |
| 869 | int ret; |
| 870 | sector_t sb_offset; |
| 871 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 872 | int bmask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 873 | |
| 874 | /* |
| 875 | * Calculate the position of the superblock. |
| 876 | * It is always aligned to a 4K boundary and |
| 877 | * depeding on minor_version, it can be: |
| 878 | * 0: At least 8K, but less than 12K, from end of device |
| 879 | * 1: At start of device |
| 880 | * 2: 4K from start of device. |
| 881 | */ |
| 882 | switch(minor_version) { |
| 883 | case 0: |
| 884 | sb_offset = rdev->bdev->bd_inode->i_size >> 9; |
| 885 | sb_offset -= 8*2; |
NeilBrown | 3973096 | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 886 | sb_offset &= ~(sector_t)(4*2-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 887 | /* convert from sectors to K */ |
| 888 | sb_offset /= 2; |
| 889 | break; |
| 890 | case 1: |
| 891 | sb_offset = 0; |
| 892 | break; |
| 893 | case 2: |
| 894 | sb_offset = 4; |
| 895 | break; |
| 896 | default: |
| 897 | return -EINVAL; |
| 898 | } |
| 899 | rdev->sb_offset = sb_offset; |
| 900 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 901 | /* superblock is rarely larger than 1K, but it can be larger, |
| 902 | * and it is safe to read 4k, so we do that |
| 903 | */ |
| 904 | ret = read_disk_sb(rdev, 4096); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 905 | if (ret) return ret; |
| 906 | |
| 907 | |
| 908 | sb = (struct mdp_superblock_1*)page_address(rdev->sb_page); |
| 909 | |
| 910 | if (sb->magic != cpu_to_le32(MD_SB_MAGIC) || |
| 911 | sb->major_version != cpu_to_le32(1) || |
| 912 | le32_to_cpu(sb->max_dev) > (4096-256)/2 || |
| 913 | le64_to_cpu(sb->super_offset) != (rdev->sb_offset<<1) || |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 914 | (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | return -EINVAL; |
| 916 | |
| 917 | if (calc_sb_1_csum(sb) != sb->sb_csum) { |
| 918 | printk("md: invalid superblock checksum on %s\n", |
| 919 | bdevname(rdev->bdev,b)); |
| 920 | return -EINVAL; |
| 921 | } |
| 922 | if (le64_to_cpu(sb->data_size) < 10) { |
| 923 | printk("md: data_size too small on %s\n", |
| 924 | bdevname(rdev->bdev,b)); |
| 925 | return -EINVAL; |
| 926 | } |
| 927 | rdev->preferred_minor = 0xffff; |
| 928 | rdev->data_offset = le64_to_cpu(sb->data_offset); |
| 929 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 930 | rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256; |
NeilBrown | 720a3dc | 2005-09-09 16:23:59 -0700 | [diff] [blame] | 931 | bmask = queue_hardsect_size(rdev->bdev->bd_disk->queue)-1; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 932 | if (rdev->sb_size & bmask) |
| 933 | rdev-> sb_size = (rdev->sb_size | bmask)+1; |
| 934 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 935 | if (refdev == 0) |
| 936 | return 1; |
| 937 | else { |
| 938 | __u64 ev1, ev2; |
| 939 | struct mdp_superblock_1 *refsb = |
| 940 | (struct mdp_superblock_1*)page_address(refdev->sb_page); |
| 941 | |
| 942 | if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 || |
| 943 | sb->level != refsb->level || |
| 944 | sb->layout != refsb->layout || |
| 945 | sb->chunksize != refsb->chunksize) { |
| 946 | printk(KERN_WARNING "md: %s has strangely different" |
| 947 | " superblock to %s\n", |
| 948 | bdevname(rdev->bdev,b), |
| 949 | bdevname(refdev->bdev,b2)); |
| 950 | return -EINVAL; |
| 951 | } |
| 952 | ev1 = le64_to_cpu(sb->events); |
| 953 | ev2 = le64_to_cpu(refsb->events); |
| 954 | |
| 955 | if (ev1 > ev2) |
| 956 | return 1; |
| 957 | } |
| 958 | if (minor_version) |
| 959 | rdev->size = ((rdev->bdev->bd_inode->i_size>>9) - le64_to_cpu(sb->data_offset)) / 2; |
| 960 | else |
| 961 | rdev->size = rdev->sb_offset; |
| 962 | if (rdev->size < le64_to_cpu(sb->data_size)/2) |
| 963 | return -EINVAL; |
| 964 | rdev->size = le64_to_cpu(sb->data_size)/2; |
| 965 | if (le32_to_cpu(sb->chunksize)) |
| 966 | rdev->size &= ~((sector_t)le32_to_cpu(sb->chunksize)/2 - 1); |
| 967 | return 0; |
| 968 | } |
| 969 | |
| 970 | static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev) |
| 971 | { |
| 972 | struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page); |
| 973 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 974 | rdev->raid_disk = -1; |
| 975 | rdev->in_sync = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 976 | if (mddev->raid_disks == 0) { |
| 977 | mddev->major_version = 1; |
| 978 | mddev->patch_version = 0; |
| 979 | mddev->persistent = 1; |
| 980 | mddev->chunk_size = le32_to_cpu(sb->chunksize) << 9; |
| 981 | mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1); |
| 982 | mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1); |
| 983 | mddev->level = le32_to_cpu(sb->level); |
| 984 | mddev->layout = le32_to_cpu(sb->layout); |
| 985 | mddev->raid_disks = le32_to_cpu(sb->raid_disks); |
| 986 | mddev->size = le64_to_cpu(sb->size)/2; |
| 987 | mddev->events = le64_to_cpu(sb->events); |
NeilBrown | 9223214 | 2005-08-18 11:24:16 -0700 | [diff] [blame] | 988 | mddev->bitmap_offset = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 989 | mddev->default_bitmap_offset = 0; |
NeilBrown | 53e87fb | 2005-09-09 16:23:58 -0700 | [diff] [blame] | 990 | mddev->default_bitmap_offset = 1024; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 991 | |
| 992 | mddev->recovery_cp = le64_to_cpu(sb->resync_offset); |
| 993 | memcpy(mddev->uuid, sb->set_uuid, 16); |
| 994 | |
| 995 | mddev->max_disks = (4096-256)/2; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 996 | |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 997 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) && |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 998 | mddev->bitmap_file == NULL ) { |
| 999 | if (mddev->level != 1) { |
| 1000 | printk(KERN_WARNING "md: bitmaps only supported for raid1\n"); |
| 1001 | return -EINVAL; |
| 1002 | } |
| 1003 | mddev->bitmap_offset = (__s32)le32_to_cpu(sb->bitmap_offset); |
| 1004 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1005 | } else if (mddev->pers == NULL) { |
| 1006 | /* Insist of good event counter while assembling */ |
| 1007 | __u64 ev1 = le64_to_cpu(sb->events); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1008 | ++ev1; |
| 1009 | if (ev1 < mddev->events) |
| 1010 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1011 | } else if (mddev->bitmap) { |
| 1012 | /* If adding to array with a bitmap, then we can accept an |
| 1013 | * older device, but not too old. |
| 1014 | */ |
| 1015 | __u64 ev1 = le64_to_cpu(sb->events); |
| 1016 | if (ev1 < mddev->bitmap->events_cleared) |
| 1017 | return 0; |
| 1018 | } else /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1019 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1020 | |
| 1021 | if (mddev->level != LEVEL_MULTIPATH) { |
| 1022 | int role; |
| 1023 | rdev->desc_nr = le32_to_cpu(sb->dev_number); |
| 1024 | role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]); |
| 1025 | switch(role) { |
| 1026 | case 0xffff: /* spare */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1027 | rdev->faulty = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1028 | break; |
| 1029 | case 0xfffe: /* faulty */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1030 | rdev->faulty = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1031 | break; |
| 1032 | default: |
| 1033 | rdev->in_sync = 1; |
| 1034 | rdev->faulty = 0; |
| 1035 | rdev->raid_disk = role; |
| 1036 | break; |
| 1037 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1038 | rdev->flags = 0; |
| 1039 | if (sb->devflags & WriteMostly1) |
| 1040 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1041 | } else /* MULTIPATH are always insync */ |
| 1042 | rdev->in_sync = 1; |
| 1043 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1044 | return 0; |
| 1045 | } |
| 1046 | |
| 1047 | static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1048 | { |
| 1049 | struct mdp_superblock_1 *sb; |
| 1050 | struct list_head *tmp; |
| 1051 | mdk_rdev_t *rdev2; |
| 1052 | int max_dev, i; |
| 1053 | /* make rdev->sb match mddev and rdev data. */ |
| 1054 | |
| 1055 | sb = (struct mdp_superblock_1*)page_address(rdev->sb_page); |
| 1056 | |
| 1057 | sb->feature_map = 0; |
| 1058 | sb->pad0 = 0; |
| 1059 | memset(sb->pad1, 0, sizeof(sb->pad1)); |
| 1060 | memset(sb->pad2, 0, sizeof(sb->pad2)); |
| 1061 | memset(sb->pad3, 0, sizeof(sb->pad3)); |
| 1062 | |
| 1063 | sb->utime = cpu_to_le64((__u64)mddev->utime); |
| 1064 | sb->events = cpu_to_le64(mddev->events); |
| 1065 | if (mddev->in_sync) |
| 1066 | sb->resync_offset = cpu_to_le64(mddev->recovery_cp); |
| 1067 | else |
| 1068 | sb->resync_offset = cpu_to_le64(0); |
| 1069 | |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1070 | if (mddev->bitmap && mddev->bitmap_file == NULL) { |
| 1071 | sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_offset); |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1072 | sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1073 | } |
| 1074 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1075 | max_dev = 0; |
| 1076 | ITERATE_RDEV(mddev,rdev2,tmp) |
| 1077 | if (rdev2->desc_nr+1 > max_dev) |
| 1078 | max_dev = rdev2->desc_nr+1; |
| 1079 | |
| 1080 | sb->max_dev = cpu_to_le32(max_dev); |
| 1081 | for (i=0; i<max_dev;i++) |
| 1082 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
| 1083 | |
| 1084 | ITERATE_RDEV(mddev,rdev2,tmp) { |
| 1085 | i = rdev2->desc_nr; |
| 1086 | if (rdev2->faulty) |
| 1087 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
| 1088 | else if (rdev2->in_sync) |
| 1089 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
| 1090 | else |
| 1091 | sb->dev_roles[i] = cpu_to_le16(0xffff); |
| 1092 | } |
| 1093 | |
| 1094 | sb->recovery_offset = cpu_to_le64(0); /* not supported yet */ |
| 1095 | sb->sb_csum = calc_sb_1_csum(sb); |
| 1096 | } |
| 1097 | |
| 1098 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 1099 | static struct super_type super_types[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1100 | [0] = { |
| 1101 | .name = "0.90.0", |
| 1102 | .owner = THIS_MODULE, |
| 1103 | .load_super = super_90_load, |
| 1104 | .validate_super = super_90_validate, |
| 1105 | .sync_super = super_90_sync, |
| 1106 | }, |
| 1107 | [1] = { |
| 1108 | .name = "md-1", |
| 1109 | .owner = THIS_MODULE, |
| 1110 | .load_super = super_1_load, |
| 1111 | .validate_super = super_1_validate, |
| 1112 | .sync_super = super_1_sync, |
| 1113 | }, |
| 1114 | }; |
| 1115 | |
| 1116 | static mdk_rdev_t * match_dev_unit(mddev_t *mddev, mdk_rdev_t *dev) |
| 1117 | { |
| 1118 | struct list_head *tmp; |
| 1119 | mdk_rdev_t *rdev; |
| 1120 | |
| 1121 | ITERATE_RDEV(mddev,rdev,tmp) |
| 1122 | if (rdev->bdev->bd_contains == dev->bdev->bd_contains) |
| 1123 | return rdev; |
| 1124 | |
| 1125 | return NULL; |
| 1126 | } |
| 1127 | |
| 1128 | static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2) |
| 1129 | { |
| 1130 | struct list_head *tmp; |
| 1131 | mdk_rdev_t *rdev; |
| 1132 | |
| 1133 | ITERATE_RDEV(mddev1,rdev,tmp) |
| 1134 | if (match_dev_unit(mddev2, rdev)) |
| 1135 | return 1; |
| 1136 | |
| 1137 | return 0; |
| 1138 | } |
| 1139 | |
| 1140 | static LIST_HEAD(pending_raid_disks); |
| 1141 | |
| 1142 | static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev) |
| 1143 | { |
| 1144 | mdk_rdev_t *same_pdev; |
| 1145 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 1146 | |
| 1147 | if (rdev->mddev) { |
| 1148 | MD_BUG(); |
| 1149 | return -EINVAL; |
| 1150 | } |
| 1151 | same_pdev = match_dev_unit(mddev, rdev); |
| 1152 | if (same_pdev) |
| 1153 | printk(KERN_WARNING |
| 1154 | "%s: WARNING: %s appears to be on the same physical" |
| 1155 | " disk as %s. True\n protection against single-disk" |
| 1156 | " failure might be compromised.\n", |
| 1157 | mdname(mddev), bdevname(rdev->bdev,b), |
| 1158 | bdevname(same_pdev->bdev,b2)); |
| 1159 | |
| 1160 | /* Verify rdev->desc_nr is unique. |
| 1161 | * If it is -1, assign a free number, else |
| 1162 | * check number is not in use |
| 1163 | */ |
| 1164 | if (rdev->desc_nr < 0) { |
| 1165 | int choice = 0; |
| 1166 | if (mddev->pers) choice = mddev->raid_disks; |
| 1167 | while (find_rdev_nr(mddev, choice)) |
| 1168 | choice++; |
| 1169 | rdev->desc_nr = choice; |
| 1170 | } else { |
| 1171 | if (find_rdev_nr(mddev, rdev->desc_nr)) |
| 1172 | return -EBUSY; |
| 1173 | } |
| 1174 | |
| 1175 | list_add(&rdev->same_set, &mddev->disks); |
| 1176 | rdev->mddev = mddev; |
| 1177 | printk(KERN_INFO "md: bind<%s>\n", bdevname(rdev->bdev,b)); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1178 | |
| 1179 | rdev->kobj.k_name = NULL; |
| 1180 | snprintf(rdev->kobj.name, KOBJ_NAME_LEN, "dev%d", rdev->desc_nr); |
| 1181 | rdev->kobj.parent = kobject_get(&mddev->kobj); |
| 1182 | kobject_add(&rdev->kobj); |
| 1183 | |
| 1184 | sysfs_create_link(&rdev->kobj, &rdev->bdev->bd_disk->kobj, "block"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1185 | return 0; |
| 1186 | } |
| 1187 | |
| 1188 | static void unbind_rdev_from_array(mdk_rdev_t * rdev) |
| 1189 | { |
| 1190 | char b[BDEVNAME_SIZE]; |
| 1191 | if (!rdev->mddev) { |
| 1192 | MD_BUG(); |
| 1193 | return; |
| 1194 | } |
| 1195 | list_del_init(&rdev->same_set); |
| 1196 | printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b)); |
| 1197 | rdev->mddev = NULL; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1198 | sysfs_remove_link(&rdev->kobj, "block"); |
| 1199 | kobject_del(&rdev->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1200 | } |
| 1201 | |
| 1202 | /* |
| 1203 | * prevent the device from being mounted, repartitioned or |
| 1204 | * otherwise reused by a RAID array (or any other kernel |
| 1205 | * subsystem), by bd_claiming the device. |
| 1206 | */ |
| 1207 | static int lock_rdev(mdk_rdev_t *rdev, dev_t dev) |
| 1208 | { |
| 1209 | int err = 0; |
| 1210 | struct block_device *bdev; |
| 1211 | char b[BDEVNAME_SIZE]; |
| 1212 | |
| 1213 | bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE); |
| 1214 | if (IS_ERR(bdev)) { |
| 1215 | printk(KERN_ERR "md: could not open %s.\n", |
| 1216 | __bdevname(dev, b)); |
| 1217 | return PTR_ERR(bdev); |
| 1218 | } |
| 1219 | err = bd_claim(bdev, rdev); |
| 1220 | if (err) { |
| 1221 | printk(KERN_ERR "md: could not bd_claim %s.\n", |
| 1222 | bdevname(bdev, b)); |
| 1223 | blkdev_put(bdev); |
| 1224 | return err; |
| 1225 | } |
| 1226 | rdev->bdev = bdev; |
| 1227 | return err; |
| 1228 | } |
| 1229 | |
| 1230 | static void unlock_rdev(mdk_rdev_t *rdev) |
| 1231 | { |
| 1232 | struct block_device *bdev = rdev->bdev; |
| 1233 | rdev->bdev = NULL; |
| 1234 | if (!bdev) |
| 1235 | MD_BUG(); |
| 1236 | bd_release(bdev); |
| 1237 | blkdev_put(bdev); |
| 1238 | } |
| 1239 | |
| 1240 | void md_autodetect_dev(dev_t dev); |
| 1241 | |
| 1242 | static void export_rdev(mdk_rdev_t * rdev) |
| 1243 | { |
| 1244 | char b[BDEVNAME_SIZE]; |
| 1245 | printk(KERN_INFO "md: export_rdev(%s)\n", |
| 1246 | bdevname(rdev->bdev,b)); |
| 1247 | if (rdev->mddev) |
| 1248 | MD_BUG(); |
| 1249 | free_disk_sb(rdev); |
| 1250 | list_del_init(&rdev->same_set); |
| 1251 | #ifndef MODULE |
| 1252 | md_autodetect_dev(rdev->bdev->bd_dev); |
| 1253 | #endif |
| 1254 | unlock_rdev(rdev); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1255 | kobject_put(&rdev->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1256 | } |
| 1257 | |
| 1258 | static void kick_rdev_from_array(mdk_rdev_t * rdev) |
| 1259 | { |
| 1260 | unbind_rdev_from_array(rdev); |
| 1261 | export_rdev(rdev); |
| 1262 | } |
| 1263 | |
| 1264 | static void export_array(mddev_t *mddev) |
| 1265 | { |
| 1266 | struct list_head *tmp; |
| 1267 | mdk_rdev_t *rdev; |
| 1268 | |
| 1269 | ITERATE_RDEV(mddev,rdev,tmp) { |
| 1270 | if (!rdev->mddev) { |
| 1271 | MD_BUG(); |
| 1272 | continue; |
| 1273 | } |
| 1274 | kick_rdev_from_array(rdev); |
| 1275 | } |
| 1276 | if (!list_empty(&mddev->disks)) |
| 1277 | MD_BUG(); |
| 1278 | mddev->raid_disks = 0; |
| 1279 | mddev->major_version = 0; |
| 1280 | } |
| 1281 | |
| 1282 | static void print_desc(mdp_disk_t *desc) |
| 1283 | { |
| 1284 | printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number, |
| 1285 | desc->major,desc->minor,desc->raid_disk,desc->state); |
| 1286 | } |
| 1287 | |
| 1288 | static void print_sb(mdp_super_t *sb) |
| 1289 | { |
| 1290 | int i; |
| 1291 | |
| 1292 | printk(KERN_INFO |
| 1293 | "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n", |
| 1294 | sb->major_version, sb->minor_version, sb->patch_version, |
| 1295 | sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3, |
| 1296 | sb->ctime); |
| 1297 | printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n", |
| 1298 | sb->level, sb->size, sb->nr_disks, sb->raid_disks, |
| 1299 | sb->md_minor, sb->layout, sb->chunk_size); |
| 1300 | printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d" |
| 1301 | " FD:%d SD:%d CSUM:%08x E:%08lx\n", |
| 1302 | sb->utime, sb->state, sb->active_disks, sb->working_disks, |
| 1303 | sb->failed_disks, sb->spare_disks, |
| 1304 | sb->sb_csum, (unsigned long)sb->events_lo); |
| 1305 | |
| 1306 | printk(KERN_INFO); |
| 1307 | for (i = 0; i < MD_SB_DISKS; i++) { |
| 1308 | mdp_disk_t *desc; |
| 1309 | |
| 1310 | desc = sb->disks + i; |
| 1311 | if (desc->number || desc->major || desc->minor || |
| 1312 | desc->raid_disk || (desc->state && (desc->state != 4))) { |
| 1313 | printk(" D %2d: ", i); |
| 1314 | print_desc(desc); |
| 1315 | } |
| 1316 | } |
| 1317 | printk(KERN_INFO "md: THIS: "); |
| 1318 | print_desc(&sb->this_disk); |
| 1319 | |
| 1320 | } |
| 1321 | |
| 1322 | static void print_rdev(mdk_rdev_t *rdev) |
| 1323 | { |
| 1324 | char b[BDEVNAME_SIZE]; |
| 1325 | printk(KERN_INFO "md: rdev %s, SZ:%08llu F:%d S:%d DN:%u\n", |
| 1326 | bdevname(rdev->bdev,b), (unsigned long long)rdev->size, |
| 1327 | rdev->faulty, rdev->in_sync, rdev->desc_nr); |
| 1328 | if (rdev->sb_loaded) { |
| 1329 | printk(KERN_INFO "md: rdev superblock:\n"); |
| 1330 | print_sb((mdp_super_t*)page_address(rdev->sb_page)); |
| 1331 | } else |
| 1332 | printk(KERN_INFO "md: no rdev superblock!\n"); |
| 1333 | } |
| 1334 | |
| 1335 | void md_print_devices(void) |
| 1336 | { |
| 1337 | struct list_head *tmp, *tmp2; |
| 1338 | mdk_rdev_t *rdev; |
| 1339 | mddev_t *mddev; |
| 1340 | char b[BDEVNAME_SIZE]; |
| 1341 | |
| 1342 | printk("\n"); |
| 1343 | printk("md: **********************************\n"); |
| 1344 | printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n"); |
| 1345 | printk("md: **********************************\n"); |
| 1346 | ITERATE_MDDEV(mddev,tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1347 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1348 | if (mddev->bitmap) |
| 1349 | bitmap_print_sb(mddev->bitmap); |
| 1350 | else |
| 1351 | printk("%s: ", mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1352 | ITERATE_RDEV(mddev,rdev,tmp2) |
| 1353 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 1354 | printk("\n"); |
| 1355 | |
| 1356 | ITERATE_RDEV(mddev,rdev,tmp2) |
| 1357 | print_rdev(rdev); |
| 1358 | } |
| 1359 | printk("md: **********************************\n"); |
| 1360 | printk("\n"); |
| 1361 | } |
| 1362 | |
| 1363 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1364 | static void sync_sbs(mddev_t * mddev) |
| 1365 | { |
| 1366 | mdk_rdev_t *rdev; |
| 1367 | struct list_head *tmp; |
| 1368 | |
| 1369 | ITERATE_RDEV(mddev,rdev,tmp) { |
| 1370 | super_types[mddev->major_version]. |
| 1371 | sync_super(mddev, rdev); |
| 1372 | rdev->sb_loaded = 1; |
| 1373 | } |
| 1374 | } |
| 1375 | |
| 1376 | static void md_update_sb(mddev_t * mddev) |
| 1377 | { |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 1378 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1379 | struct list_head *tmp; |
| 1380 | mdk_rdev_t *rdev; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1381 | int sync_req; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1382 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1383 | repeat: |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1384 | spin_lock(&mddev->write_lock); |
| 1385 | sync_req = mddev->in_sync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1386 | mddev->utime = get_seconds(); |
| 1387 | mddev->events ++; |
| 1388 | |
| 1389 | if (!mddev->events) { |
| 1390 | /* |
| 1391 | * oops, this 64-bit counter should never wrap. |
| 1392 | * Either we are in around ~1 trillion A.C., assuming |
| 1393 | * 1 reboot per second, or we have a bug: |
| 1394 | */ |
| 1395 | MD_BUG(); |
| 1396 | mddev->events --; |
| 1397 | } |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 1398 | mddev->sb_dirty = 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1399 | sync_sbs(mddev); |
| 1400 | |
| 1401 | /* |
| 1402 | * do not write anything to disk if using |
| 1403 | * nonpersistent superblocks |
| 1404 | */ |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1405 | if (!mddev->persistent) { |
| 1406 | mddev->sb_dirty = 0; |
| 1407 | spin_unlock(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 1408 | wake_up(&mddev->sb_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1409 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1410 | } |
| 1411 | spin_unlock(&mddev->write_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1412 | |
| 1413 | dprintk(KERN_INFO |
| 1414 | "md: updating %s RAID superblock on device (in sync %d)\n", |
| 1415 | mdname(mddev),mddev->in_sync); |
| 1416 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1417 | err = bitmap_update_sb(mddev->bitmap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1418 | ITERATE_RDEV(mddev,rdev,tmp) { |
| 1419 | char b[BDEVNAME_SIZE]; |
| 1420 | dprintk(KERN_INFO "md: "); |
| 1421 | if (rdev->faulty) |
| 1422 | dprintk("(skipping faulty "); |
| 1423 | |
| 1424 | dprintk("%s ", bdevname(rdev->bdev,b)); |
| 1425 | if (!rdev->faulty) { |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 1426 | md_super_write(mddev,rdev, |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1427 | rdev->sb_offset<<1, rdev->sb_size, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 1428 | rdev->sb_page); |
| 1429 | dprintk(KERN_INFO "(write) %s's sb offset: %llu\n", |
| 1430 | bdevname(rdev->bdev,b), |
| 1431 | (unsigned long long)rdev->sb_offset); |
| 1432 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1433 | } else |
| 1434 | dprintk(")\n"); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 1435 | if (mddev->level == LEVEL_MULTIPATH) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1436 | /* only need to write one superblock... */ |
| 1437 | break; |
| 1438 | } |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 1439 | wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0); |
| 1440 | /* if there was a failure, sb_dirty was set to 1, and we re-write super */ |
| 1441 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1442 | spin_lock(&mddev->write_lock); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 1443 | if (mddev->in_sync != sync_req|| mddev->sb_dirty == 1) { |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1444 | /* have to write it out again */ |
| 1445 | spin_unlock(&mddev->write_lock); |
| 1446 | goto repeat; |
| 1447 | } |
| 1448 | mddev->sb_dirty = 0; |
| 1449 | spin_unlock(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 1450 | wake_up(&mddev->sb_wait); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1451 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1452 | } |
| 1453 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1454 | struct rdev_sysfs_entry { |
| 1455 | struct attribute attr; |
| 1456 | ssize_t (*show)(mdk_rdev_t *, char *); |
| 1457 | ssize_t (*store)(mdk_rdev_t *, const char *, size_t); |
| 1458 | }; |
| 1459 | |
| 1460 | static ssize_t |
| 1461 | rdev_show_state(mdk_rdev_t *rdev, char *page) |
| 1462 | { |
| 1463 | char *sep = ""; |
| 1464 | int len=0; |
| 1465 | |
| 1466 | if (rdev->faulty) { |
| 1467 | len+= sprintf(page+len, "%sfaulty",sep); |
| 1468 | sep = ","; |
| 1469 | } |
| 1470 | if (rdev->in_sync) { |
| 1471 | len += sprintf(page+len, "%sin_sync",sep); |
| 1472 | sep = ","; |
| 1473 | } |
| 1474 | if (!rdev->faulty && !rdev->in_sync) { |
| 1475 | len += sprintf(page+len, "%sspare", sep); |
| 1476 | sep = ","; |
| 1477 | } |
| 1478 | return len+sprintf(page+len, "\n"); |
| 1479 | } |
| 1480 | |
| 1481 | static struct rdev_sysfs_entry rdev_state = { |
| 1482 | .attr = {.name = "state", .mode = S_IRUGO }, |
| 1483 | .show = rdev_show_state, |
| 1484 | }; |
| 1485 | |
| 1486 | static ssize_t |
| 1487 | rdev_show_super(mdk_rdev_t *rdev, char *page) |
| 1488 | { |
| 1489 | if (rdev->sb_loaded && rdev->sb_size) { |
| 1490 | memcpy(page, page_address(rdev->sb_page), rdev->sb_size); |
| 1491 | return rdev->sb_size; |
| 1492 | } else |
| 1493 | return 0; |
| 1494 | } |
| 1495 | static struct rdev_sysfs_entry rdev_super = { |
| 1496 | .attr = {.name = "super", .mode = S_IRUGO }, |
| 1497 | .show = rdev_show_super, |
| 1498 | }; |
| 1499 | static struct attribute *rdev_default_attrs[] = { |
| 1500 | &rdev_state.attr, |
| 1501 | &rdev_super.attr, |
| 1502 | NULL, |
| 1503 | }; |
| 1504 | static ssize_t |
| 1505 | rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 1506 | { |
| 1507 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
| 1508 | mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj); |
| 1509 | |
| 1510 | if (!entry->show) |
| 1511 | return -EIO; |
| 1512 | return entry->show(rdev, page); |
| 1513 | } |
| 1514 | |
| 1515 | static ssize_t |
| 1516 | rdev_attr_store(struct kobject *kobj, struct attribute *attr, |
| 1517 | const char *page, size_t length) |
| 1518 | { |
| 1519 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
| 1520 | mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj); |
| 1521 | |
| 1522 | if (!entry->store) |
| 1523 | return -EIO; |
| 1524 | return entry->store(rdev, page, length); |
| 1525 | } |
| 1526 | |
| 1527 | static void rdev_free(struct kobject *ko) |
| 1528 | { |
| 1529 | mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj); |
| 1530 | kfree(rdev); |
| 1531 | } |
| 1532 | static struct sysfs_ops rdev_sysfs_ops = { |
| 1533 | .show = rdev_attr_show, |
| 1534 | .store = rdev_attr_store, |
| 1535 | }; |
| 1536 | static struct kobj_type rdev_ktype = { |
| 1537 | .release = rdev_free, |
| 1538 | .sysfs_ops = &rdev_sysfs_ops, |
| 1539 | .default_attrs = rdev_default_attrs, |
| 1540 | }; |
| 1541 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1542 | /* |
| 1543 | * Import a device. If 'super_format' >= 0, then sanity check the superblock |
| 1544 | * |
| 1545 | * mark the device faulty if: |
| 1546 | * |
| 1547 | * - the device is nonexistent (zero size) |
| 1548 | * - the device has no valid superblock |
| 1549 | * |
| 1550 | * a faulty rdev _never_ has rdev->sb set. |
| 1551 | */ |
| 1552 | static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor) |
| 1553 | { |
| 1554 | char b[BDEVNAME_SIZE]; |
| 1555 | int err; |
| 1556 | mdk_rdev_t *rdev; |
| 1557 | sector_t size; |
| 1558 | |
| 1559 | rdev = (mdk_rdev_t *) kmalloc(sizeof(*rdev), GFP_KERNEL); |
| 1560 | if (!rdev) { |
| 1561 | printk(KERN_ERR "md: could not alloc mem for new device!\n"); |
| 1562 | return ERR_PTR(-ENOMEM); |
| 1563 | } |
| 1564 | memset(rdev, 0, sizeof(*rdev)); |
| 1565 | |
| 1566 | if ((err = alloc_disk_sb(rdev))) |
| 1567 | goto abort_free; |
| 1568 | |
| 1569 | err = lock_rdev(rdev, newdev); |
| 1570 | if (err) |
| 1571 | goto abort_free; |
| 1572 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1573 | rdev->kobj.parent = NULL; |
| 1574 | rdev->kobj.ktype = &rdev_ktype; |
| 1575 | kobject_init(&rdev->kobj); |
| 1576 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1577 | rdev->desc_nr = -1; |
| 1578 | rdev->faulty = 0; |
| 1579 | rdev->in_sync = 0; |
| 1580 | rdev->data_offset = 0; |
| 1581 | atomic_set(&rdev->nr_pending, 0); |
| 1582 | |
| 1583 | size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS; |
| 1584 | if (!size) { |
| 1585 | printk(KERN_WARNING |
| 1586 | "md: %s has zero or unknown size, marking faulty!\n", |
| 1587 | bdevname(rdev->bdev,b)); |
| 1588 | err = -EINVAL; |
| 1589 | goto abort_free; |
| 1590 | } |
| 1591 | |
| 1592 | if (super_format >= 0) { |
| 1593 | err = super_types[super_format]. |
| 1594 | load_super(rdev, NULL, super_minor); |
| 1595 | if (err == -EINVAL) { |
| 1596 | printk(KERN_WARNING |
| 1597 | "md: %s has invalid sb, not importing!\n", |
| 1598 | bdevname(rdev->bdev,b)); |
| 1599 | goto abort_free; |
| 1600 | } |
| 1601 | if (err < 0) { |
| 1602 | printk(KERN_WARNING |
| 1603 | "md: could not read %s's sb, not importing!\n", |
| 1604 | bdevname(rdev->bdev,b)); |
| 1605 | goto abort_free; |
| 1606 | } |
| 1607 | } |
| 1608 | INIT_LIST_HEAD(&rdev->same_set); |
| 1609 | |
| 1610 | return rdev; |
| 1611 | |
| 1612 | abort_free: |
| 1613 | if (rdev->sb_page) { |
| 1614 | if (rdev->bdev) |
| 1615 | unlock_rdev(rdev); |
| 1616 | free_disk_sb(rdev); |
| 1617 | } |
| 1618 | kfree(rdev); |
| 1619 | return ERR_PTR(err); |
| 1620 | } |
| 1621 | |
| 1622 | /* |
| 1623 | * Check a full RAID array for plausibility |
| 1624 | */ |
| 1625 | |
| 1626 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 1627 | static void analyze_sbs(mddev_t * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1628 | { |
| 1629 | int i; |
| 1630 | struct list_head *tmp; |
| 1631 | mdk_rdev_t *rdev, *freshest; |
| 1632 | char b[BDEVNAME_SIZE]; |
| 1633 | |
| 1634 | freshest = NULL; |
| 1635 | ITERATE_RDEV(mddev,rdev,tmp) |
| 1636 | switch (super_types[mddev->major_version]. |
| 1637 | load_super(rdev, freshest, mddev->minor_version)) { |
| 1638 | case 1: |
| 1639 | freshest = rdev; |
| 1640 | break; |
| 1641 | case 0: |
| 1642 | break; |
| 1643 | default: |
| 1644 | printk( KERN_ERR \ |
| 1645 | "md: fatal superblock inconsistency in %s" |
| 1646 | " -- removing from array\n", |
| 1647 | bdevname(rdev->bdev,b)); |
| 1648 | kick_rdev_from_array(rdev); |
| 1649 | } |
| 1650 | |
| 1651 | |
| 1652 | super_types[mddev->major_version]. |
| 1653 | validate_super(mddev, freshest); |
| 1654 | |
| 1655 | i = 0; |
| 1656 | ITERATE_RDEV(mddev,rdev,tmp) { |
| 1657 | if (rdev != freshest) |
| 1658 | if (super_types[mddev->major_version]. |
| 1659 | validate_super(mddev, rdev)) { |
| 1660 | printk(KERN_WARNING "md: kicking non-fresh %s" |
| 1661 | " from array!\n", |
| 1662 | bdevname(rdev->bdev,b)); |
| 1663 | kick_rdev_from_array(rdev); |
| 1664 | continue; |
| 1665 | } |
| 1666 | if (mddev->level == LEVEL_MULTIPATH) { |
| 1667 | rdev->desc_nr = i++; |
| 1668 | rdev->raid_disk = rdev->desc_nr; |
| 1669 | rdev->in_sync = 1; |
| 1670 | } |
| 1671 | } |
| 1672 | |
| 1673 | |
| 1674 | |
| 1675 | if (mddev->recovery_cp != MaxSector && |
| 1676 | mddev->level >= 1) |
| 1677 | printk(KERN_ERR "md: %s: raid array is not clean" |
| 1678 | " -- starting background reconstruction\n", |
| 1679 | mdname(mddev)); |
| 1680 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1681 | } |
| 1682 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 1683 | struct md_sysfs_entry { |
| 1684 | struct attribute attr; |
| 1685 | ssize_t (*show)(mddev_t *, char *); |
| 1686 | ssize_t (*store)(mddev_t *, const char *, size_t); |
| 1687 | }; |
| 1688 | |
| 1689 | static ssize_t |
| 1690 | md_show_level(mddev_t *mddev, char *page) |
| 1691 | { |
| 1692 | mdk_personality_t *p = mddev->pers; |
| 1693 | if (p == NULL) |
| 1694 | return 0; |
| 1695 | if (mddev->level >= 0) |
| 1696 | return sprintf(page, "RAID-%d\n", mddev->level); |
| 1697 | else |
| 1698 | return sprintf(page, "%s\n", p->name); |
| 1699 | } |
| 1700 | |
| 1701 | static struct md_sysfs_entry md_level = { |
| 1702 | .attr = {.name = "level", .mode = S_IRUGO }, |
| 1703 | .show = md_show_level, |
| 1704 | }; |
| 1705 | |
| 1706 | static ssize_t |
| 1707 | md_show_rdisks(mddev_t *mddev, char *page) |
| 1708 | { |
| 1709 | return sprintf(page, "%d\n", mddev->raid_disks); |
| 1710 | } |
| 1711 | |
| 1712 | static struct md_sysfs_entry md_raid_disks = { |
| 1713 | .attr = {.name = "raid_disks", .mode = S_IRUGO }, |
| 1714 | .show = md_show_rdisks, |
| 1715 | }; |
| 1716 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame^] | 1717 | static ssize_t |
| 1718 | md_show_scan(mddev_t *mddev, char *page) |
| 1719 | { |
| 1720 | char *type = "none"; |
| 1721 | if (mddev->recovery & |
| 1722 | ((1<<MD_RECOVERY_RUNNING) || (1<<MD_RECOVERY_NEEDED))) { |
| 1723 | if (mddev->recovery & (1<<MD_RECOVERY_SYNC)) { |
| 1724 | if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 1725 | type = "resync"; |
| 1726 | else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 1727 | type = "check"; |
| 1728 | else |
| 1729 | type = "repair"; |
| 1730 | } else |
| 1731 | type = "recover"; |
| 1732 | } |
| 1733 | return sprintf(page, "%s\n", type); |
| 1734 | } |
| 1735 | |
| 1736 | static ssize_t |
| 1737 | md_store_scan(mddev_t *mddev, const char *page, size_t len) |
| 1738 | { |
| 1739 | int canscan=0; |
| 1740 | if (mddev->recovery & |
| 1741 | ((1<<MD_RECOVERY_RUNNING) || (1<<MD_RECOVERY_NEEDED))) |
| 1742 | return -EBUSY; |
| 1743 | down(&mddev->reconfig_sem); |
| 1744 | if (mddev->pers && mddev->pers->sync_request) |
| 1745 | canscan=1; |
| 1746 | up(&mddev->reconfig_sem); |
| 1747 | if (!canscan) |
| 1748 | return -EINVAL; |
| 1749 | |
| 1750 | if (strcmp(page, "check")==0 || strcmp(page, "check\n")==0) |
| 1751 | set_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
| 1752 | else if (strcmp(page, "repair")!=0 && strcmp(page, "repair\n")!=0) |
| 1753 | return -EINVAL; |
| 1754 | set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 1755 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 1756 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 1757 | md_wakeup_thread(mddev->thread); |
| 1758 | return len; |
| 1759 | } |
| 1760 | |
| 1761 | static struct md_sysfs_entry md_scan_mode = { |
| 1762 | .attr = {.name = "scan_mode", .mode = S_IRUGO|S_IWUSR }, |
| 1763 | .show = md_show_scan, |
| 1764 | .store = md_store_scan, |
| 1765 | }; |
| 1766 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 1767 | static struct attribute *md_default_attrs[] = { |
| 1768 | &md_level.attr, |
| 1769 | &md_raid_disks.attr, |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame^] | 1770 | &md_scan_mode.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 1771 | NULL, |
| 1772 | }; |
| 1773 | |
| 1774 | static ssize_t |
| 1775 | md_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 1776 | { |
| 1777 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
| 1778 | mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); |
| 1779 | |
| 1780 | if (!entry->show) |
| 1781 | return -EIO; |
| 1782 | return entry->show(mddev, page); |
| 1783 | } |
| 1784 | |
| 1785 | static ssize_t |
| 1786 | md_attr_store(struct kobject *kobj, struct attribute *attr, |
| 1787 | const char *page, size_t length) |
| 1788 | { |
| 1789 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
| 1790 | mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); |
| 1791 | |
| 1792 | if (!entry->store) |
| 1793 | return -EIO; |
| 1794 | return entry->store(mddev, page, length); |
| 1795 | } |
| 1796 | |
| 1797 | static void md_free(struct kobject *ko) |
| 1798 | { |
| 1799 | mddev_t *mddev = container_of(ko, mddev_t, kobj); |
| 1800 | kfree(mddev); |
| 1801 | } |
| 1802 | |
| 1803 | static struct sysfs_ops md_sysfs_ops = { |
| 1804 | .show = md_attr_show, |
| 1805 | .store = md_attr_store, |
| 1806 | }; |
| 1807 | static struct kobj_type md_ktype = { |
| 1808 | .release = md_free, |
| 1809 | .sysfs_ops = &md_sysfs_ops, |
| 1810 | .default_attrs = md_default_attrs, |
| 1811 | }; |
| 1812 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1813 | int mdp_major = 0; |
| 1814 | |
| 1815 | static struct kobject *md_probe(dev_t dev, int *part, void *data) |
| 1816 | { |
| 1817 | static DECLARE_MUTEX(disks_sem); |
| 1818 | mddev_t *mddev = mddev_find(dev); |
| 1819 | struct gendisk *disk; |
| 1820 | int partitioned = (MAJOR(dev) != MD_MAJOR); |
| 1821 | int shift = partitioned ? MdpMinorShift : 0; |
| 1822 | int unit = MINOR(dev) >> shift; |
| 1823 | |
| 1824 | if (!mddev) |
| 1825 | return NULL; |
| 1826 | |
| 1827 | down(&disks_sem); |
| 1828 | if (mddev->gendisk) { |
| 1829 | up(&disks_sem); |
| 1830 | mddev_put(mddev); |
| 1831 | return NULL; |
| 1832 | } |
| 1833 | disk = alloc_disk(1 << shift); |
| 1834 | if (!disk) { |
| 1835 | up(&disks_sem); |
| 1836 | mddev_put(mddev); |
| 1837 | return NULL; |
| 1838 | } |
| 1839 | disk->major = MAJOR(dev); |
| 1840 | disk->first_minor = unit << shift; |
| 1841 | if (partitioned) { |
| 1842 | sprintf(disk->disk_name, "md_d%d", unit); |
| 1843 | sprintf(disk->devfs_name, "md/d%d", unit); |
| 1844 | } else { |
| 1845 | sprintf(disk->disk_name, "md%d", unit); |
| 1846 | sprintf(disk->devfs_name, "md/%d", unit); |
| 1847 | } |
| 1848 | disk->fops = &md_fops; |
| 1849 | disk->private_data = mddev; |
| 1850 | disk->queue = mddev->queue; |
| 1851 | add_disk(disk); |
| 1852 | mddev->gendisk = disk; |
| 1853 | up(&disks_sem); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 1854 | mddev->kobj.parent = kobject_get(&disk->kobj); |
| 1855 | mddev->kobj.k_name = NULL; |
| 1856 | snprintf(mddev->kobj.name, KOBJ_NAME_LEN, "%s", "md"); |
| 1857 | mddev->kobj.ktype = &md_ktype; |
| 1858 | kobject_register(&mddev->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1859 | return NULL; |
| 1860 | } |
| 1861 | |
| 1862 | void md_wakeup_thread(mdk_thread_t *thread); |
| 1863 | |
| 1864 | static void md_safemode_timeout(unsigned long data) |
| 1865 | { |
| 1866 | mddev_t *mddev = (mddev_t *) data; |
| 1867 | |
| 1868 | mddev->safemode = 1; |
| 1869 | md_wakeup_thread(mddev->thread); |
| 1870 | } |
| 1871 | |
| 1872 | |
| 1873 | static int do_md_run(mddev_t * mddev) |
| 1874 | { |
| 1875 | int pnum, err; |
| 1876 | int chunk_size; |
| 1877 | struct list_head *tmp; |
| 1878 | mdk_rdev_t *rdev; |
| 1879 | struct gendisk *disk; |
| 1880 | char b[BDEVNAME_SIZE]; |
| 1881 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 1882 | if (list_empty(&mddev->disks)) |
| 1883 | /* cannot run an array with no devices.. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1884 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1885 | |
| 1886 | if (mddev->pers) |
| 1887 | return -EBUSY; |
| 1888 | |
| 1889 | /* |
| 1890 | * Analyze all RAID superblock(s) |
| 1891 | */ |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 1892 | if (!mddev->raid_disks) |
| 1893 | analyze_sbs(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1894 | |
| 1895 | chunk_size = mddev->chunk_size; |
| 1896 | pnum = level_to_pers(mddev->level); |
| 1897 | |
| 1898 | if ((pnum != MULTIPATH) && (pnum != RAID1)) { |
| 1899 | if (!chunk_size) { |
| 1900 | /* |
| 1901 | * 'default chunksize' in the old md code used to |
| 1902 | * be PAGE_SIZE, baaad. |
| 1903 | * we abort here to be on the safe side. We don't |
| 1904 | * want to continue the bad practice. |
| 1905 | */ |
| 1906 | printk(KERN_ERR |
| 1907 | "no chunksize specified, see 'man raidtab'\n"); |
| 1908 | return -EINVAL; |
| 1909 | } |
| 1910 | if (chunk_size > MAX_CHUNK_SIZE) { |
| 1911 | printk(KERN_ERR "too big chunk_size: %d > %d\n", |
| 1912 | chunk_size, MAX_CHUNK_SIZE); |
| 1913 | return -EINVAL; |
| 1914 | } |
| 1915 | /* |
| 1916 | * chunk-size has to be a power of 2 and multiples of PAGE_SIZE |
| 1917 | */ |
| 1918 | if ( (1 << ffz(~chunk_size)) != chunk_size) { |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 1919 | printk(KERN_ERR "chunk_size of %d not valid\n", chunk_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1920 | return -EINVAL; |
| 1921 | } |
| 1922 | if (chunk_size < PAGE_SIZE) { |
| 1923 | printk(KERN_ERR "too small chunk_size: %d < %ld\n", |
| 1924 | chunk_size, PAGE_SIZE); |
| 1925 | return -EINVAL; |
| 1926 | } |
| 1927 | |
| 1928 | /* devices must have minimum size of one chunk */ |
| 1929 | ITERATE_RDEV(mddev,rdev,tmp) { |
| 1930 | if (rdev->faulty) |
| 1931 | continue; |
| 1932 | if (rdev->size < chunk_size / 1024) { |
| 1933 | printk(KERN_WARNING |
| 1934 | "md: Dev %s smaller than chunk_size:" |
| 1935 | " %lluk < %dk\n", |
| 1936 | bdevname(rdev->bdev,b), |
| 1937 | (unsigned long long)rdev->size, |
| 1938 | chunk_size / 1024); |
| 1939 | return -EINVAL; |
| 1940 | } |
| 1941 | } |
| 1942 | } |
| 1943 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1944 | #ifdef CONFIG_KMOD |
| 1945 | if (!pers[pnum]) |
| 1946 | { |
| 1947 | request_module("md-personality-%d", pnum); |
| 1948 | } |
| 1949 | #endif |
| 1950 | |
| 1951 | /* |
| 1952 | * Drop all container device buffers, from now on |
| 1953 | * the only valid external interface is through the md |
| 1954 | * device. |
| 1955 | * Also find largest hardsector size |
| 1956 | */ |
| 1957 | ITERATE_RDEV(mddev,rdev,tmp) { |
| 1958 | if (rdev->faulty) |
| 1959 | continue; |
| 1960 | sync_blockdev(rdev->bdev); |
| 1961 | invalidate_bdev(rdev->bdev, 0); |
| 1962 | } |
| 1963 | |
| 1964 | md_probe(mddev->unit, NULL, NULL); |
| 1965 | disk = mddev->gendisk; |
| 1966 | if (!disk) |
| 1967 | return -ENOMEM; |
| 1968 | |
| 1969 | spin_lock(&pers_lock); |
| 1970 | if (!pers[pnum] || !try_module_get(pers[pnum]->owner)) { |
| 1971 | spin_unlock(&pers_lock); |
| 1972 | printk(KERN_WARNING "md: personality %d is not loaded!\n", |
| 1973 | pnum); |
| 1974 | return -EINVAL; |
| 1975 | } |
| 1976 | |
| 1977 | mddev->pers = pers[pnum]; |
| 1978 | spin_unlock(&pers_lock); |
| 1979 | |
NeilBrown | 657390d | 2005-08-26 18:34:16 -0700 | [diff] [blame] | 1980 | mddev->recovery = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1981 | mddev->resync_max_sectors = mddev->size << 1; /* may be over-ridden by personality */ |
| 1982 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1983 | /* before we start the array running, initialise the bitmap */ |
| 1984 | err = bitmap_create(mddev); |
| 1985 | if (err) |
| 1986 | printk(KERN_ERR "%s: failed to create bitmap (%d)\n", |
| 1987 | mdname(mddev), err); |
| 1988 | else |
| 1989 | err = mddev->pers->run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1990 | if (err) { |
| 1991 | printk(KERN_ERR "md: pers->run() failed ...\n"); |
| 1992 | module_put(mddev->pers->owner); |
| 1993 | mddev->pers = NULL; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1994 | bitmap_destroy(mddev); |
| 1995 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1996 | } |
| 1997 | atomic_set(&mddev->writes_pending,0); |
| 1998 | mddev->safemode = 0; |
| 1999 | mddev->safemode_timer.function = md_safemode_timeout; |
| 2000 | mddev->safemode_timer.data = (unsigned long) mddev; |
| 2001 | mddev->safemode_delay = (20 * HZ)/1000 +1; /* 20 msec delay */ |
| 2002 | mddev->in_sync = 1; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2003 | |
| 2004 | ITERATE_RDEV(mddev,rdev,tmp) |
| 2005 | if (rdev->raid_disk >= 0) { |
| 2006 | char nm[20]; |
| 2007 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 2008 | sysfs_create_link(&mddev->kobj, &rdev->kobj, nm); |
| 2009 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2010 | |
| 2011 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 2012 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2013 | |
| 2014 | if (mddev->sb_dirty) |
| 2015 | md_update_sb(mddev); |
| 2016 | |
| 2017 | set_capacity(disk, mddev->array_size<<1); |
| 2018 | |
| 2019 | /* If we call blk_queue_make_request here, it will |
| 2020 | * re-initialise max_sectors etc which may have been |
| 2021 | * refined inside -> run. So just set the bits we need to set. |
| 2022 | * Most initialisation happended when we called |
| 2023 | * blk_queue_make_request(..., md_fail_request) |
| 2024 | * earlier. |
| 2025 | */ |
| 2026 | mddev->queue->queuedata = mddev; |
| 2027 | mddev->queue->make_request_fn = mddev->pers->make_request; |
| 2028 | |
| 2029 | mddev->changed = 1; |
| 2030 | return 0; |
| 2031 | } |
| 2032 | |
| 2033 | static int restart_array(mddev_t *mddev) |
| 2034 | { |
| 2035 | struct gendisk *disk = mddev->gendisk; |
| 2036 | int err; |
| 2037 | |
| 2038 | /* |
| 2039 | * Complain if it has no devices |
| 2040 | */ |
| 2041 | err = -ENXIO; |
| 2042 | if (list_empty(&mddev->disks)) |
| 2043 | goto out; |
| 2044 | |
| 2045 | if (mddev->pers) { |
| 2046 | err = -EBUSY; |
| 2047 | if (!mddev->ro) |
| 2048 | goto out; |
| 2049 | |
| 2050 | mddev->safemode = 0; |
| 2051 | mddev->ro = 0; |
| 2052 | set_disk_ro(disk, 0); |
| 2053 | |
| 2054 | printk(KERN_INFO "md: %s switched to read-write mode.\n", |
| 2055 | mdname(mddev)); |
| 2056 | /* |
| 2057 | * Kick recovery or resync if necessary |
| 2058 | */ |
| 2059 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 2060 | md_wakeup_thread(mddev->thread); |
| 2061 | err = 0; |
| 2062 | } else { |
| 2063 | printk(KERN_ERR "md: %s has no personality assigned.\n", |
| 2064 | mdname(mddev)); |
| 2065 | err = -EINVAL; |
| 2066 | } |
| 2067 | |
| 2068 | out: |
| 2069 | return err; |
| 2070 | } |
| 2071 | |
| 2072 | static int do_md_stop(mddev_t * mddev, int ro) |
| 2073 | { |
| 2074 | int err = 0; |
| 2075 | struct gendisk *disk = mddev->gendisk; |
| 2076 | |
| 2077 | if (mddev->pers) { |
| 2078 | if (atomic_read(&mddev->active)>2) { |
| 2079 | printk("md: %s still in use.\n",mdname(mddev)); |
| 2080 | return -EBUSY; |
| 2081 | } |
| 2082 | |
| 2083 | if (mddev->sync_thread) { |
| 2084 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 2085 | md_unregister_thread(mddev->sync_thread); |
| 2086 | mddev->sync_thread = NULL; |
| 2087 | } |
| 2088 | |
| 2089 | del_timer_sync(&mddev->safemode_timer); |
| 2090 | |
| 2091 | invalidate_partition(disk, 0); |
| 2092 | |
| 2093 | if (ro) { |
| 2094 | err = -ENXIO; |
| 2095 | if (mddev->ro) |
| 2096 | goto out; |
| 2097 | mddev->ro = 1; |
| 2098 | } else { |
NeilBrown | 6b8b3e8 | 2005-08-04 12:53:35 -0700 | [diff] [blame] | 2099 | bitmap_flush(mddev); |
| 2100 | wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2101 | if (mddev->ro) |
| 2102 | set_disk_ro(disk, 0); |
| 2103 | blk_queue_make_request(mddev->queue, md_fail_request); |
| 2104 | mddev->pers->stop(mddev); |
| 2105 | module_put(mddev->pers->owner); |
| 2106 | mddev->pers = NULL; |
| 2107 | if (mddev->ro) |
| 2108 | mddev->ro = 0; |
| 2109 | } |
| 2110 | if (!mddev->in_sync) { |
| 2111 | /* mark array as shutdown cleanly */ |
| 2112 | mddev->in_sync = 1; |
| 2113 | md_update_sb(mddev); |
| 2114 | } |
| 2115 | if (ro) |
| 2116 | set_disk_ro(disk, 1); |
| 2117 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 2118 | |
| 2119 | bitmap_destroy(mddev); |
| 2120 | if (mddev->bitmap_file) { |
| 2121 | atomic_set(&mddev->bitmap_file->f_dentry->d_inode->i_writecount, 1); |
| 2122 | fput(mddev->bitmap_file); |
| 2123 | mddev->bitmap_file = NULL; |
| 2124 | } |
NeilBrown | 9223214 | 2005-08-18 11:24:16 -0700 | [diff] [blame] | 2125 | mddev->bitmap_offset = 0; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 2126 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2127 | /* |
| 2128 | * Free resources if final stop |
| 2129 | */ |
| 2130 | if (!ro) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2131 | mdk_rdev_t *rdev; |
| 2132 | struct list_head *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2133 | struct gendisk *disk; |
| 2134 | printk(KERN_INFO "md: %s stopped.\n", mdname(mddev)); |
| 2135 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2136 | ITERATE_RDEV(mddev,rdev,tmp) |
| 2137 | if (rdev->raid_disk >= 0) { |
| 2138 | char nm[20]; |
| 2139 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 2140 | sysfs_remove_link(&mddev->kobj, nm); |
| 2141 | } |
| 2142 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2143 | export_array(mddev); |
| 2144 | |
| 2145 | mddev->array_size = 0; |
| 2146 | disk = mddev->gendisk; |
| 2147 | if (disk) |
| 2148 | set_capacity(disk, 0); |
| 2149 | mddev->changed = 1; |
| 2150 | } else |
| 2151 | printk(KERN_INFO "md: %s switched to read-only mode.\n", |
| 2152 | mdname(mddev)); |
| 2153 | err = 0; |
| 2154 | out: |
| 2155 | return err; |
| 2156 | } |
| 2157 | |
| 2158 | static void autorun_array(mddev_t *mddev) |
| 2159 | { |
| 2160 | mdk_rdev_t *rdev; |
| 2161 | struct list_head *tmp; |
| 2162 | int err; |
| 2163 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 2164 | if (list_empty(&mddev->disks)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2165 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2166 | |
| 2167 | printk(KERN_INFO "md: running: "); |
| 2168 | |
| 2169 | ITERATE_RDEV(mddev,rdev,tmp) { |
| 2170 | char b[BDEVNAME_SIZE]; |
| 2171 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 2172 | } |
| 2173 | printk("\n"); |
| 2174 | |
| 2175 | err = do_md_run (mddev); |
| 2176 | if (err) { |
| 2177 | printk(KERN_WARNING "md: do_md_run() returned %d\n", err); |
| 2178 | do_md_stop (mddev, 0); |
| 2179 | } |
| 2180 | } |
| 2181 | |
| 2182 | /* |
| 2183 | * lets try to run arrays based on all disks that have arrived |
| 2184 | * until now. (those are in pending_raid_disks) |
| 2185 | * |
| 2186 | * the method: pick the first pending disk, collect all disks with |
| 2187 | * the same UUID, remove all from the pending list and put them into |
| 2188 | * the 'same_array' list. Then order this list based on superblock |
| 2189 | * update time (freshest comes first), kick out 'old' disks and |
| 2190 | * compare superblocks. If everything's fine then run it. |
| 2191 | * |
| 2192 | * If "unit" is allocated, then bump its reference count |
| 2193 | */ |
| 2194 | static void autorun_devices(int part) |
| 2195 | { |
| 2196 | struct list_head candidates; |
| 2197 | struct list_head *tmp; |
| 2198 | mdk_rdev_t *rdev0, *rdev; |
| 2199 | mddev_t *mddev; |
| 2200 | char b[BDEVNAME_SIZE]; |
| 2201 | |
| 2202 | printk(KERN_INFO "md: autorun ...\n"); |
| 2203 | while (!list_empty(&pending_raid_disks)) { |
| 2204 | dev_t dev; |
| 2205 | rdev0 = list_entry(pending_raid_disks.next, |
| 2206 | mdk_rdev_t, same_set); |
| 2207 | |
| 2208 | printk(KERN_INFO "md: considering %s ...\n", |
| 2209 | bdevname(rdev0->bdev,b)); |
| 2210 | INIT_LIST_HEAD(&candidates); |
| 2211 | ITERATE_RDEV_PENDING(rdev,tmp) |
| 2212 | if (super_90_load(rdev, rdev0, 0) >= 0) { |
| 2213 | printk(KERN_INFO "md: adding %s ...\n", |
| 2214 | bdevname(rdev->bdev,b)); |
| 2215 | list_move(&rdev->same_set, &candidates); |
| 2216 | } |
| 2217 | /* |
| 2218 | * now we have a set of devices, with all of them having |
| 2219 | * mostly sane superblocks. It's time to allocate the |
| 2220 | * mddev. |
| 2221 | */ |
| 2222 | if (rdev0->preferred_minor < 0 || rdev0->preferred_minor >= MAX_MD_DEVS) { |
| 2223 | printk(KERN_INFO "md: unit number in %s is bad: %d\n", |
| 2224 | bdevname(rdev0->bdev, b), rdev0->preferred_minor); |
| 2225 | break; |
| 2226 | } |
| 2227 | if (part) |
| 2228 | dev = MKDEV(mdp_major, |
| 2229 | rdev0->preferred_minor << MdpMinorShift); |
| 2230 | else |
| 2231 | dev = MKDEV(MD_MAJOR, rdev0->preferred_minor); |
| 2232 | |
| 2233 | md_probe(dev, NULL, NULL); |
| 2234 | mddev = mddev_find(dev); |
| 2235 | if (!mddev) { |
| 2236 | printk(KERN_ERR |
| 2237 | "md: cannot allocate memory for md drive.\n"); |
| 2238 | break; |
| 2239 | } |
| 2240 | if (mddev_lock(mddev)) |
| 2241 | printk(KERN_WARNING "md: %s locked, cannot run\n", |
| 2242 | mdname(mddev)); |
| 2243 | else if (mddev->raid_disks || mddev->major_version |
| 2244 | || !list_empty(&mddev->disks)) { |
| 2245 | printk(KERN_WARNING |
| 2246 | "md: %s already running, cannot run %s\n", |
| 2247 | mdname(mddev), bdevname(rdev0->bdev,b)); |
| 2248 | mddev_unlock(mddev); |
| 2249 | } else { |
| 2250 | printk(KERN_INFO "md: created %s\n", mdname(mddev)); |
| 2251 | ITERATE_RDEV_GENERIC(candidates,rdev,tmp) { |
| 2252 | list_del_init(&rdev->same_set); |
| 2253 | if (bind_rdev_to_array(rdev, mddev)) |
| 2254 | export_rdev(rdev); |
| 2255 | } |
| 2256 | autorun_array(mddev); |
| 2257 | mddev_unlock(mddev); |
| 2258 | } |
| 2259 | /* on success, candidates will be empty, on error |
| 2260 | * it won't... |
| 2261 | */ |
| 2262 | ITERATE_RDEV_GENERIC(candidates,rdev,tmp) |
| 2263 | export_rdev(rdev); |
| 2264 | mddev_put(mddev); |
| 2265 | } |
| 2266 | printk(KERN_INFO "md: ... autorun DONE.\n"); |
| 2267 | } |
| 2268 | |
| 2269 | /* |
| 2270 | * import RAID devices based on one partition |
| 2271 | * if possible, the array gets run as well. |
| 2272 | */ |
| 2273 | |
| 2274 | static int autostart_array(dev_t startdev) |
| 2275 | { |
| 2276 | char b[BDEVNAME_SIZE]; |
| 2277 | int err = -EINVAL, i; |
| 2278 | mdp_super_t *sb = NULL; |
| 2279 | mdk_rdev_t *start_rdev = NULL, *rdev; |
| 2280 | |
| 2281 | start_rdev = md_import_device(startdev, 0, 0); |
| 2282 | if (IS_ERR(start_rdev)) |
| 2283 | return err; |
| 2284 | |
| 2285 | |
| 2286 | /* NOTE: this can only work for 0.90.0 superblocks */ |
| 2287 | sb = (mdp_super_t*)page_address(start_rdev->sb_page); |
| 2288 | if (sb->major_version != 0 || |
| 2289 | sb->minor_version != 90 ) { |
| 2290 | printk(KERN_WARNING "md: can only autostart 0.90.0 arrays\n"); |
| 2291 | export_rdev(start_rdev); |
| 2292 | return err; |
| 2293 | } |
| 2294 | |
| 2295 | if (start_rdev->faulty) { |
| 2296 | printk(KERN_WARNING |
| 2297 | "md: can not autostart based on faulty %s!\n", |
| 2298 | bdevname(start_rdev->bdev,b)); |
| 2299 | export_rdev(start_rdev); |
| 2300 | return err; |
| 2301 | } |
| 2302 | list_add(&start_rdev->same_set, &pending_raid_disks); |
| 2303 | |
| 2304 | for (i = 0; i < MD_SB_DISKS; i++) { |
| 2305 | mdp_disk_t *desc = sb->disks + i; |
| 2306 | dev_t dev = MKDEV(desc->major, desc->minor); |
| 2307 | |
| 2308 | if (!dev) |
| 2309 | continue; |
| 2310 | if (dev == startdev) |
| 2311 | continue; |
| 2312 | if (MAJOR(dev) != desc->major || MINOR(dev) != desc->minor) |
| 2313 | continue; |
| 2314 | rdev = md_import_device(dev, 0, 0); |
| 2315 | if (IS_ERR(rdev)) |
| 2316 | continue; |
| 2317 | |
| 2318 | list_add(&rdev->same_set, &pending_raid_disks); |
| 2319 | } |
| 2320 | |
| 2321 | /* |
| 2322 | * possibly return codes |
| 2323 | */ |
| 2324 | autorun_devices(0); |
| 2325 | return 0; |
| 2326 | |
| 2327 | } |
| 2328 | |
| 2329 | |
| 2330 | static int get_version(void __user * arg) |
| 2331 | { |
| 2332 | mdu_version_t ver; |
| 2333 | |
| 2334 | ver.major = MD_MAJOR_VERSION; |
| 2335 | ver.minor = MD_MINOR_VERSION; |
| 2336 | ver.patchlevel = MD_PATCHLEVEL_VERSION; |
| 2337 | |
| 2338 | if (copy_to_user(arg, &ver, sizeof(ver))) |
| 2339 | return -EFAULT; |
| 2340 | |
| 2341 | return 0; |
| 2342 | } |
| 2343 | |
| 2344 | static int get_array_info(mddev_t * mddev, void __user * arg) |
| 2345 | { |
| 2346 | mdu_array_info_t info; |
| 2347 | int nr,working,active,failed,spare; |
| 2348 | mdk_rdev_t *rdev; |
| 2349 | struct list_head *tmp; |
| 2350 | |
| 2351 | nr=working=active=failed=spare=0; |
| 2352 | ITERATE_RDEV(mddev,rdev,tmp) { |
| 2353 | nr++; |
| 2354 | if (rdev->faulty) |
| 2355 | failed++; |
| 2356 | else { |
| 2357 | working++; |
| 2358 | if (rdev->in_sync) |
| 2359 | active++; |
| 2360 | else |
| 2361 | spare++; |
| 2362 | } |
| 2363 | } |
| 2364 | |
| 2365 | info.major_version = mddev->major_version; |
| 2366 | info.minor_version = mddev->minor_version; |
| 2367 | info.patch_version = MD_PATCHLEVEL_VERSION; |
| 2368 | info.ctime = mddev->ctime; |
| 2369 | info.level = mddev->level; |
| 2370 | info.size = mddev->size; |
| 2371 | info.nr_disks = nr; |
| 2372 | info.raid_disks = mddev->raid_disks; |
| 2373 | info.md_minor = mddev->md_minor; |
| 2374 | info.not_persistent= !mddev->persistent; |
| 2375 | |
| 2376 | info.utime = mddev->utime; |
| 2377 | info.state = 0; |
| 2378 | if (mddev->in_sync) |
| 2379 | info.state = (1<<MD_SB_CLEAN); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 2380 | if (mddev->bitmap && mddev->bitmap_offset) |
| 2381 | info.state = (1<<MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2382 | info.active_disks = active; |
| 2383 | info.working_disks = working; |
| 2384 | info.failed_disks = failed; |
| 2385 | info.spare_disks = spare; |
| 2386 | |
| 2387 | info.layout = mddev->layout; |
| 2388 | info.chunk_size = mddev->chunk_size; |
| 2389 | |
| 2390 | if (copy_to_user(arg, &info, sizeof(info))) |
| 2391 | return -EFAULT; |
| 2392 | |
| 2393 | return 0; |
| 2394 | } |
| 2395 | |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 2396 | static int get_bitmap_file(mddev_t * mddev, void __user * arg) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 2397 | { |
| 2398 | mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */ |
| 2399 | char *ptr, *buf = NULL; |
| 2400 | int err = -ENOMEM; |
| 2401 | |
| 2402 | file = kmalloc(sizeof(*file), GFP_KERNEL); |
| 2403 | if (!file) |
| 2404 | goto out; |
| 2405 | |
| 2406 | /* bitmap disabled, zero the first byte and copy out */ |
| 2407 | if (!mddev->bitmap || !mddev->bitmap->file) { |
| 2408 | file->pathname[0] = '\0'; |
| 2409 | goto copy_out; |
| 2410 | } |
| 2411 | |
| 2412 | buf = kmalloc(sizeof(file->pathname), GFP_KERNEL); |
| 2413 | if (!buf) |
| 2414 | goto out; |
| 2415 | |
| 2416 | ptr = file_path(mddev->bitmap->file, buf, sizeof(file->pathname)); |
| 2417 | if (!ptr) |
| 2418 | goto out; |
| 2419 | |
| 2420 | strcpy(file->pathname, ptr); |
| 2421 | |
| 2422 | copy_out: |
| 2423 | err = 0; |
| 2424 | if (copy_to_user(arg, file, sizeof(*file))) |
| 2425 | err = -EFAULT; |
| 2426 | out: |
| 2427 | kfree(buf); |
| 2428 | kfree(file); |
| 2429 | return err; |
| 2430 | } |
| 2431 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2432 | static int get_disk_info(mddev_t * mddev, void __user * arg) |
| 2433 | { |
| 2434 | mdu_disk_info_t info; |
| 2435 | unsigned int nr; |
| 2436 | mdk_rdev_t *rdev; |
| 2437 | |
| 2438 | if (copy_from_user(&info, arg, sizeof(info))) |
| 2439 | return -EFAULT; |
| 2440 | |
| 2441 | nr = info.number; |
| 2442 | |
| 2443 | rdev = find_rdev_nr(mddev, nr); |
| 2444 | if (rdev) { |
| 2445 | info.major = MAJOR(rdev->bdev->bd_dev); |
| 2446 | info.minor = MINOR(rdev->bdev->bd_dev); |
| 2447 | info.raid_disk = rdev->raid_disk; |
| 2448 | info.state = 0; |
| 2449 | if (rdev->faulty) |
| 2450 | info.state |= (1<<MD_DISK_FAULTY); |
| 2451 | else if (rdev->in_sync) { |
| 2452 | info.state |= (1<<MD_DISK_ACTIVE); |
| 2453 | info.state |= (1<<MD_DISK_SYNC); |
| 2454 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 2455 | if (test_bit(WriteMostly, &rdev->flags)) |
| 2456 | info.state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2457 | } else { |
| 2458 | info.major = info.minor = 0; |
| 2459 | info.raid_disk = -1; |
| 2460 | info.state = (1<<MD_DISK_REMOVED); |
| 2461 | } |
| 2462 | |
| 2463 | if (copy_to_user(arg, &info, sizeof(info))) |
| 2464 | return -EFAULT; |
| 2465 | |
| 2466 | return 0; |
| 2467 | } |
| 2468 | |
| 2469 | static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info) |
| 2470 | { |
| 2471 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 2472 | mdk_rdev_t *rdev; |
| 2473 | dev_t dev = MKDEV(info->major,info->minor); |
| 2474 | |
| 2475 | if (info->major != MAJOR(dev) || info->minor != MINOR(dev)) |
| 2476 | return -EOVERFLOW; |
| 2477 | |
| 2478 | if (!mddev->raid_disks) { |
| 2479 | int err; |
| 2480 | /* expecting a device which has a superblock */ |
| 2481 | rdev = md_import_device(dev, mddev->major_version, mddev->minor_version); |
| 2482 | if (IS_ERR(rdev)) { |
| 2483 | printk(KERN_WARNING |
| 2484 | "md: md_import_device returned %ld\n", |
| 2485 | PTR_ERR(rdev)); |
| 2486 | return PTR_ERR(rdev); |
| 2487 | } |
| 2488 | if (!list_empty(&mddev->disks)) { |
| 2489 | mdk_rdev_t *rdev0 = list_entry(mddev->disks.next, |
| 2490 | mdk_rdev_t, same_set); |
| 2491 | int err = super_types[mddev->major_version] |
| 2492 | .load_super(rdev, rdev0, mddev->minor_version); |
| 2493 | if (err < 0) { |
| 2494 | printk(KERN_WARNING |
| 2495 | "md: %s has different UUID to %s\n", |
| 2496 | bdevname(rdev->bdev,b), |
| 2497 | bdevname(rdev0->bdev,b2)); |
| 2498 | export_rdev(rdev); |
| 2499 | return -EINVAL; |
| 2500 | } |
| 2501 | } |
| 2502 | err = bind_rdev_to_array(rdev, mddev); |
| 2503 | if (err) |
| 2504 | export_rdev(rdev); |
| 2505 | return err; |
| 2506 | } |
| 2507 | |
| 2508 | /* |
| 2509 | * add_new_disk can be used once the array is assembled |
| 2510 | * to add "hot spares". They must already have a superblock |
| 2511 | * written |
| 2512 | */ |
| 2513 | if (mddev->pers) { |
| 2514 | int err; |
| 2515 | if (!mddev->pers->hot_add_disk) { |
| 2516 | printk(KERN_WARNING |
| 2517 | "%s: personality does not support diskops!\n", |
| 2518 | mdname(mddev)); |
| 2519 | return -EINVAL; |
| 2520 | } |
NeilBrown | 7b1e35f | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 2521 | if (mddev->persistent) |
| 2522 | rdev = md_import_device(dev, mddev->major_version, |
| 2523 | mddev->minor_version); |
| 2524 | else |
| 2525 | rdev = md_import_device(dev, -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2526 | if (IS_ERR(rdev)) { |
| 2527 | printk(KERN_WARNING |
| 2528 | "md: md_import_device returned %ld\n", |
| 2529 | PTR_ERR(rdev)); |
| 2530 | return PTR_ERR(rdev); |
| 2531 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 2532 | /* set save_raid_disk if appropriate */ |
| 2533 | if (!mddev->persistent) { |
| 2534 | if (info->state & (1<<MD_DISK_SYNC) && |
| 2535 | info->raid_disk < mddev->raid_disks) |
| 2536 | rdev->raid_disk = info->raid_disk; |
| 2537 | else |
| 2538 | rdev->raid_disk = -1; |
| 2539 | } else |
| 2540 | super_types[mddev->major_version]. |
| 2541 | validate_super(mddev, rdev); |
| 2542 | rdev->saved_raid_disk = rdev->raid_disk; |
| 2543 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2544 | rdev->in_sync = 0; /* just to be sure */ |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 2545 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 2546 | set_bit(WriteMostly, &rdev->flags); |
| 2547 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2548 | rdev->raid_disk = -1; |
| 2549 | err = bind_rdev_to_array(rdev, mddev); |
| 2550 | if (err) |
| 2551 | export_rdev(rdev); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 2552 | |
| 2553 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 2554 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2555 | return err; |
| 2556 | } |
| 2557 | |
| 2558 | /* otherwise, add_new_disk is only allowed |
| 2559 | * for major_version==0 superblocks |
| 2560 | */ |
| 2561 | if (mddev->major_version != 0) { |
| 2562 | printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n", |
| 2563 | mdname(mddev)); |
| 2564 | return -EINVAL; |
| 2565 | } |
| 2566 | |
| 2567 | if (!(info->state & (1<<MD_DISK_FAULTY))) { |
| 2568 | int err; |
| 2569 | rdev = md_import_device (dev, -1, 0); |
| 2570 | if (IS_ERR(rdev)) { |
| 2571 | printk(KERN_WARNING |
| 2572 | "md: error, md_import_device() returned %ld\n", |
| 2573 | PTR_ERR(rdev)); |
| 2574 | return PTR_ERR(rdev); |
| 2575 | } |
| 2576 | rdev->desc_nr = info->number; |
| 2577 | if (info->raid_disk < mddev->raid_disks) |
| 2578 | rdev->raid_disk = info->raid_disk; |
| 2579 | else |
| 2580 | rdev->raid_disk = -1; |
| 2581 | |
| 2582 | rdev->faulty = 0; |
| 2583 | if (rdev->raid_disk < mddev->raid_disks) |
| 2584 | rdev->in_sync = (info->state & (1<<MD_DISK_SYNC)); |
| 2585 | else |
| 2586 | rdev->in_sync = 0; |
| 2587 | |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 2588 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 2589 | set_bit(WriteMostly, &rdev->flags); |
| 2590 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2591 | err = bind_rdev_to_array(rdev, mddev); |
| 2592 | if (err) { |
| 2593 | export_rdev(rdev); |
| 2594 | return err; |
| 2595 | } |
| 2596 | |
| 2597 | if (!mddev->persistent) { |
| 2598 | printk(KERN_INFO "md: nonpersistent superblock ...\n"); |
| 2599 | rdev->sb_offset = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS; |
| 2600 | } else |
| 2601 | rdev->sb_offset = calc_dev_sboffset(rdev->bdev); |
| 2602 | rdev->size = calc_dev_size(rdev, mddev->chunk_size); |
| 2603 | |
| 2604 | if (!mddev->size || (mddev->size > rdev->size)) |
| 2605 | mddev->size = rdev->size; |
| 2606 | } |
| 2607 | |
| 2608 | return 0; |
| 2609 | } |
| 2610 | |
| 2611 | static int hot_remove_disk(mddev_t * mddev, dev_t dev) |
| 2612 | { |
| 2613 | char b[BDEVNAME_SIZE]; |
| 2614 | mdk_rdev_t *rdev; |
| 2615 | |
| 2616 | if (!mddev->pers) |
| 2617 | return -ENODEV; |
| 2618 | |
| 2619 | rdev = find_rdev(mddev, dev); |
| 2620 | if (!rdev) |
| 2621 | return -ENXIO; |
| 2622 | |
| 2623 | if (rdev->raid_disk >= 0) |
| 2624 | goto busy; |
| 2625 | |
| 2626 | kick_rdev_from_array(rdev); |
| 2627 | md_update_sb(mddev); |
| 2628 | |
| 2629 | return 0; |
| 2630 | busy: |
| 2631 | printk(KERN_WARNING "md: cannot remove active disk %s from %s ... \n", |
| 2632 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 2633 | return -EBUSY; |
| 2634 | } |
| 2635 | |
| 2636 | static int hot_add_disk(mddev_t * mddev, dev_t dev) |
| 2637 | { |
| 2638 | char b[BDEVNAME_SIZE]; |
| 2639 | int err; |
| 2640 | unsigned int size; |
| 2641 | mdk_rdev_t *rdev; |
| 2642 | |
| 2643 | if (!mddev->pers) |
| 2644 | return -ENODEV; |
| 2645 | |
| 2646 | if (mddev->major_version != 0) { |
| 2647 | printk(KERN_WARNING "%s: HOT_ADD may only be used with" |
| 2648 | " version-0 superblocks.\n", |
| 2649 | mdname(mddev)); |
| 2650 | return -EINVAL; |
| 2651 | } |
| 2652 | if (!mddev->pers->hot_add_disk) { |
| 2653 | printk(KERN_WARNING |
| 2654 | "%s: personality does not support diskops!\n", |
| 2655 | mdname(mddev)); |
| 2656 | return -EINVAL; |
| 2657 | } |
| 2658 | |
| 2659 | rdev = md_import_device (dev, -1, 0); |
| 2660 | if (IS_ERR(rdev)) { |
| 2661 | printk(KERN_WARNING |
| 2662 | "md: error, md_import_device() returned %ld\n", |
| 2663 | PTR_ERR(rdev)); |
| 2664 | return -EINVAL; |
| 2665 | } |
| 2666 | |
| 2667 | if (mddev->persistent) |
| 2668 | rdev->sb_offset = calc_dev_sboffset(rdev->bdev); |
| 2669 | else |
| 2670 | rdev->sb_offset = |
| 2671 | rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS; |
| 2672 | |
| 2673 | size = calc_dev_size(rdev, mddev->chunk_size); |
| 2674 | rdev->size = size; |
| 2675 | |
| 2676 | if (size < mddev->size) { |
| 2677 | printk(KERN_WARNING |
| 2678 | "%s: disk size %llu blocks < array size %llu\n", |
| 2679 | mdname(mddev), (unsigned long long)size, |
| 2680 | (unsigned long long)mddev->size); |
| 2681 | err = -ENOSPC; |
| 2682 | goto abort_export; |
| 2683 | } |
| 2684 | |
| 2685 | if (rdev->faulty) { |
| 2686 | printk(KERN_WARNING |
| 2687 | "md: can not hot-add faulty %s disk to %s!\n", |
| 2688 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 2689 | err = -EINVAL; |
| 2690 | goto abort_export; |
| 2691 | } |
| 2692 | rdev->in_sync = 0; |
| 2693 | rdev->desc_nr = -1; |
| 2694 | bind_rdev_to_array(rdev, mddev); |
| 2695 | |
| 2696 | /* |
| 2697 | * The rest should better be atomic, we can have disk failures |
| 2698 | * noticed in interrupt contexts ... |
| 2699 | */ |
| 2700 | |
| 2701 | if (rdev->desc_nr == mddev->max_disks) { |
| 2702 | printk(KERN_WARNING "%s: can not hot-add to full array!\n", |
| 2703 | mdname(mddev)); |
| 2704 | err = -EBUSY; |
| 2705 | goto abort_unbind_export; |
| 2706 | } |
| 2707 | |
| 2708 | rdev->raid_disk = -1; |
| 2709 | |
| 2710 | md_update_sb(mddev); |
| 2711 | |
| 2712 | /* |
| 2713 | * Kick recovery, maybe this spare has to be added to the |
| 2714 | * array immediately. |
| 2715 | */ |
| 2716 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 2717 | md_wakeup_thread(mddev->thread); |
| 2718 | |
| 2719 | return 0; |
| 2720 | |
| 2721 | abort_unbind_export: |
| 2722 | unbind_rdev_from_array(rdev); |
| 2723 | |
| 2724 | abort_export: |
| 2725 | export_rdev(rdev); |
| 2726 | return err; |
| 2727 | } |
| 2728 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 2729 | /* similar to deny_write_access, but accounts for our holding a reference |
| 2730 | * to the file ourselves */ |
| 2731 | static int deny_bitmap_write_access(struct file * file) |
| 2732 | { |
| 2733 | struct inode *inode = file->f_mapping->host; |
| 2734 | |
| 2735 | spin_lock(&inode->i_lock); |
| 2736 | if (atomic_read(&inode->i_writecount) > 1) { |
| 2737 | spin_unlock(&inode->i_lock); |
| 2738 | return -ETXTBSY; |
| 2739 | } |
| 2740 | atomic_set(&inode->i_writecount, -1); |
| 2741 | spin_unlock(&inode->i_lock); |
| 2742 | |
| 2743 | return 0; |
| 2744 | } |
| 2745 | |
| 2746 | static int set_bitmap_file(mddev_t *mddev, int fd) |
| 2747 | { |
| 2748 | int err; |
| 2749 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 2750 | if (mddev->pers) { |
| 2751 | if (!mddev->pers->quiesce) |
| 2752 | return -EBUSY; |
| 2753 | if (mddev->recovery || mddev->sync_thread) |
| 2754 | return -EBUSY; |
| 2755 | /* we should be able to change the bitmap.. */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 2756 | } |
| 2757 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 2758 | |
| 2759 | if (fd >= 0) { |
| 2760 | if (mddev->bitmap) |
| 2761 | return -EEXIST; /* cannot add when bitmap is present */ |
| 2762 | mddev->bitmap_file = fget(fd); |
| 2763 | |
| 2764 | if (mddev->bitmap_file == NULL) { |
| 2765 | printk(KERN_ERR "%s: error: failed to get bitmap file\n", |
| 2766 | mdname(mddev)); |
| 2767 | return -EBADF; |
| 2768 | } |
| 2769 | |
| 2770 | err = deny_bitmap_write_access(mddev->bitmap_file); |
| 2771 | if (err) { |
| 2772 | printk(KERN_ERR "%s: error: bitmap file is already in use\n", |
| 2773 | mdname(mddev)); |
| 2774 | fput(mddev->bitmap_file); |
| 2775 | mddev->bitmap_file = NULL; |
| 2776 | return err; |
| 2777 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 2778 | mddev->bitmap_offset = 0; /* file overrides offset */ |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 2779 | } else if (mddev->bitmap == NULL) |
| 2780 | return -ENOENT; /* cannot remove what isn't there */ |
| 2781 | err = 0; |
| 2782 | if (mddev->pers) { |
| 2783 | mddev->pers->quiesce(mddev, 1); |
| 2784 | if (fd >= 0) |
| 2785 | err = bitmap_create(mddev); |
| 2786 | if (fd < 0 || err) |
| 2787 | bitmap_destroy(mddev); |
| 2788 | mddev->pers->quiesce(mddev, 0); |
| 2789 | } else if (fd < 0) { |
| 2790 | if (mddev->bitmap_file) |
| 2791 | fput(mddev->bitmap_file); |
| 2792 | mddev->bitmap_file = NULL; |
| 2793 | } |
| 2794 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 2795 | return err; |
| 2796 | } |
| 2797 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2798 | /* |
| 2799 | * set_array_info is used two different ways |
| 2800 | * The original usage is when creating a new array. |
| 2801 | * In this usage, raid_disks is > 0 and it together with |
| 2802 | * level, size, not_persistent,layout,chunksize determine the |
| 2803 | * shape of the array. |
| 2804 | * This will always create an array with a type-0.90.0 superblock. |
| 2805 | * The newer usage is when assembling an array. |
| 2806 | * In this case raid_disks will be 0, and the major_version field is |
| 2807 | * use to determine which style super-blocks are to be found on the devices. |
| 2808 | * The minor and patch _version numbers are also kept incase the |
| 2809 | * super_block handler wishes to interpret them. |
| 2810 | */ |
| 2811 | static int set_array_info(mddev_t * mddev, mdu_array_info_t *info) |
| 2812 | { |
| 2813 | |
| 2814 | if (info->raid_disks == 0) { |
| 2815 | /* just setting version number for superblock loading */ |
| 2816 | if (info->major_version < 0 || |
| 2817 | info->major_version >= sizeof(super_types)/sizeof(super_types[0]) || |
| 2818 | super_types[info->major_version].name == NULL) { |
| 2819 | /* maybe try to auto-load a module? */ |
| 2820 | printk(KERN_INFO |
| 2821 | "md: superblock version %d not known\n", |
| 2822 | info->major_version); |
| 2823 | return -EINVAL; |
| 2824 | } |
| 2825 | mddev->major_version = info->major_version; |
| 2826 | mddev->minor_version = info->minor_version; |
| 2827 | mddev->patch_version = info->patch_version; |
| 2828 | return 0; |
| 2829 | } |
| 2830 | mddev->major_version = MD_MAJOR_VERSION; |
| 2831 | mddev->minor_version = MD_MINOR_VERSION; |
| 2832 | mddev->patch_version = MD_PATCHLEVEL_VERSION; |
| 2833 | mddev->ctime = get_seconds(); |
| 2834 | |
| 2835 | mddev->level = info->level; |
| 2836 | mddev->size = info->size; |
| 2837 | mddev->raid_disks = info->raid_disks; |
| 2838 | /* don't set md_minor, it is determined by which /dev/md* was |
| 2839 | * openned |
| 2840 | */ |
| 2841 | if (info->state & (1<<MD_SB_CLEAN)) |
| 2842 | mddev->recovery_cp = MaxSector; |
| 2843 | else |
| 2844 | mddev->recovery_cp = 0; |
| 2845 | mddev->persistent = ! info->not_persistent; |
| 2846 | |
| 2847 | mddev->layout = info->layout; |
| 2848 | mddev->chunk_size = info->chunk_size; |
| 2849 | |
| 2850 | mddev->max_disks = MD_SB_DISKS; |
| 2851 | |
| 2852 | mddev->sb_dirty = 1; |
| 2853 | |
| 2854 | /* |
| 2855 | * Generate a 128 bit UUID |
| 2856 | */ |
| 2857 | get_random_bytes(mddev->uuid, 16); |
| 2858 | |
| 2859 | return 0; |
| 2860 | } |
| 2861 | |
| 2862 | /* |
| 2863 | * update_array_info is used to change the configuration of an |
| 2864 | * on-line array. |
| 2865 | * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size |
| 2866 | * fields in the info are checked against the array. |
| 2867 | * Any differences that cannot be handled will cause an error. |
| 2868 | * Normally, only one change can be managed at a time. |
| 2869 | */ |
| 2870 | static int update_array_info(mddev_t *mddev, mdu_array_info_t *info) |
| 2871 | { |
| 2872 | int rv = 0; |
| 2873 | int cnt = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 2874 | int state = 0; |
| 2875 | |
| 2876 | /* calculate expected state,ignoring low bits */ |
| 2877 | if (mddev->bitmap && mddev->bitmap_offset) |
| 2878 | state |= (1 << MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2879 | |
| 2880 | if (mddev->major_version != info->major_version || |
| 2881 | mddev->minor_version != info->minor_version || |
| 2882 | /* mddev->patch_version != info->patch_version || */ |
| 2883 | mddev->ctime != info->ctime || |
| 2884 | mddev->level != info->level || |
| 2885 | /* mddev->layout != info->layout || */ |
| 2886 | !mddev->persistent != info->not_persistent|| |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 2887 | mddev->chunk_size != info->chunk_size || |
| 2888 | /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */ |
| 2889 | ((state^info->state) & 0xfffffe00) |
| 2890 | ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2891 | return -EINVAL; |
| 2892 | /* Check there is only one change */ |
| 2893 | if (mddev->size != info->size) cnt++; |
| 2894 | if (mddev->raid_disks != info->raid_disks) cnt++; |
| 2895 | if (mddev->layout != info->layout) cnt++; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 2896 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) cnt++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2897 | if (cnt == 0) return 0; |
| 2898 | if (cnt > 1) return -EINVAL; |
| 2899 | |
| 2900 | if (mddev->layout != info->layout) { |
| 2901 | /* Change layout |
| 2902 | * we don't need to do anything at the md level, the |
| 2903 | * personality will take care of it all. |
| 2904 | */ |
| 2905 | if (mddev->pers->reconfig == NULL) |
| 2906 | return -EINVAL; |
| 2907 | else |
| 2908 | return mddev->pers->reconfig(mddev, info->layout, -1); |
| 2909 | } |
| 2910 | if (mddev->size != info->size) { |
| 2911 | mdk_rdev_t * rdev; |
| 2912 | struct list_head *tmp; |
| 2913 | if (mddev->pers->resize == NULL) |
| 2914 | return -EINVAL; |
| 2915 | /* The "size" is the amount of each device that is used. |
| 2916 | * This can only make sense for arrays with redundancy. |
| 2917 | * linear and raid0 always use whatever space is available |
| 2918 | * We can only consider changing the size if no resync |
| 2919 | * or reconstruction is happening, and if the new size |
| 2920 | * is acceptable. It must fit before the sb_offset or, |
| 2921 | * if that is <data_offset, it must fit before the |
| 2922 | * size of each device. |
| 2923 | * If size is zero, we find the largest size that fits. |
| 2924 | */ |
| 2925 | if (mddev->sync_thread) |
| 2926 | return -EBUSY; |
| 2927 | ITERATE_RDEV(mddev,rdev,tmp) { |
| 2928 | sector_t avail; |
| 2929 | int fit = (info->size == 0); |
| 2930 | if (rdev->sb_offset > rdev->data_offset) |
| 2931 | avail = (rdev->sb_offset*2) - rdev->data_offset; |
| 2932 | else |
| 2933 | avail = get_capacity(rdev->bdev->bd_disk) |
| 2934 | - rdev->data_offset; |
| 2935 | if (fit && (info->size == 0 || info->size > avail/2)) |
| 2936 | info->size = avail/2; |
| 2937 | if (avail < ((sector_t)info->size << 1)) |
| 2938 | return -ENOSPC; |
| 2939 | } |
| 2940 | rv = mddev->pers->resize(mddev, (sector_t)info->size *2); |
| 2941 | if (!rv) { |
| 2942 | struct block_device *bdev; |
| 2943 | |
| 2944 | bdev = bdget_disk(mddev->gendisk, 0); |
| 2945 | if (bdev) { |
| 2946 | down(&bdev->bd_inode->i_sem); |
| 2947 | i_size_write(bdev->bd_inode, mddev->array_size << 10); |
| 2948 | up(&bdev->bd_inode->i_sem); |
| 2949 | bdput(bdev); |
| 2950 | } |
| 2951 | } |
| 2952 | } |
| 2953 | if (mddev->raid_disks != info->raid_disks) { |
| 2954 | /* change the number of raid disks */ |
| 2955 | if (mddev->pers->reshape == NULL) |
| 2956 | return -EINVAL; |
| 2957 | if (info->raid_disks <= 0 || |
| 2958 | info->raid_disks >= mddev->max_disks) |
| 2959 | return -EINVAL; |
| 2960 | if (mddev->sync_thread) |
| 2961 | return -EBUSY; |
| 2962 | rv = mddev->pers->reshape(mddev, info->raid_disks); |
| 2963 | if (!rv) { |
| 2964 | struct block_device *bdev; |
| 2965 | |
| 2966 | bdev = bdget_disk(mddev->gendisk, 0); |
| 2967 | if (bdev) { |
| 2968 | down(&bdev->bd_inode->i_sem); |
| 2969 | i_size_write(bdev->bd_inode, mddev->array_size << 10); |
| 2970 | up(&bdev->bd_inode->i_sem); |
| 2971 | bdput(bdev); |
| 2972 | } |
| 2973 | } |
| 2974 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 2975 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) { |
| 2976 | if (mddev->pers->quiesce == NULL) |
| 2977 | return -EINVAL; |
| 2978 | if (mddev->recovery || mddev->sync_thread) |
| 2979 | return -EBUSY; |
| 2980 | if (info->state & (1<<MD_SB_BITMAP_PRESENT)) { |
| 2981 | /* add the bitmap */ |
| 2982 | if (mddev->bitmap) |
| 2983 | return -EEXIST; |
| 2984 | if (mddev->default_bitmap_offset == 0) |
| 2985 | return -EINVAL; |
| 2986 | mddev->bitmap_offset = mddev->default_bitmap_offset; |
| 2987 | mddev->pers->quiesce(mddev, 1); |
| 2988 | rv = bitmap_create(mddev); |
| 2989 | if (rv) |
| 2990 | bitmap_destroy(mddev); |
| 2991 | mddev->pers->quiesce(mddev, 0); |
| 2992 | } else { |
| 2993 | /* remove the bitmap */ |
| 2994 | if (!mddev->bitmap) |
| 2995 | return -ENOENT; |
| 2996 | if (mddev->bitmap->file) |
| 2997 | return -EINVAL; |
| 2998 | mddev->pers->quiesce(mddev, 1); |
| 2999 | bitmap_destroy(mddev); |
| 3000 | mddev->pers->quiesce(mddev, 0); |
| 3001 | mddev->bitmap_offset = 0; |
| 3002 | } |
| 3003 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3004 | md_update_sb(mddev); |
| 3005 | return rv; |
| 3006 | } |
| 3007 | |
| 3008 | static int set_disk_faulty(mddev_t *mddev, dev_t dev) |
| 3009 | { |
| 3010 | mdk_rdev_t *rdev; |
| 3011 | |
| 3012 | if (mddev->pers == NULL) |
| 3013 | return -ENODEV; |
| 3014 | |
| 3015 | rdev = find_rdev(mddev, dev); |
| 3016 | if (!rdev) |
| 3017 | return -ENODEV; |
| 3018 | |
| 3019 | md_error(mddev, rdev); |
| 3020 | return 0; |
| 3021 | } |
| 3022 | |
| 3023 | static int md_ioctl(struct inode *inode, struct file *file, |
| 3024 | unsigned int cmd, unsigned long arg) |
| 3025 | { |
| 3026 | int err = 0; |
| 3027 | void __user *argp = (void __user *)arg; |
| 3028 | struct hd_geometry __user *loc = argp; |
| 3029 | mddev_t *mddev = NULL; |
| 3030 | |
| 3031 | if (!capable(CAP_SYS_ADMIN)) |
| 3032 | return -EACCES; |
| 3033 | |
| 3034 | /* |
| 3035 | * Commands dealing with the RAID driver but not any |
| 3036 | * particular array: |
| 3037 | */ |
| 3038 | switch (cmd) |
| 3039 | { |
| 3040 | case RAID_VERSION: |
| 3041 | err = get_version(argp); |
| 3042 | goto done; |
| 3043 | |
| 3044 | case PRINT_RAID_DEBUG: |
| 3045 | err = 0; |
| 3046 | md_print_devices(); |
| 3047 | goto done; |
| 3048 | |
| 3049 | #ifndef MODULE |
| 3050 | case RAID_AUTORUN: |
| 3051 | err = 0; |
| 3052 | autostart_arrays(arg); |
| 3053 | goto done; |
| 3054 | #endif |
| 3055 | default:; |
| 3056 | } |
| 3057 | |
| 3058 | /* |
| 3059 | * Commands creating/starting a new array: |
| 3060 | */ |
| 3061 | |
| 3062 | mddev = inode->i_bdev->bd_disk->private_data; |
| 3063 | |
| 3064 | if (!mddev) { |
| 3065 | BUG(); |
| 3066 | goto abort; |
| 3067 | } |
| 3068 | |
| 3069 | |
| 3070 | if (cmd == START_ARRAY) { |
| 3071 | /* START_ARRAY doesn't need to lock the array as autostart_array |
| 3072 | * does the locking, and it could even be a different array |
| 3073 | */ |
| 3074 | static int cnt = 3; |
| 3075 | if (cnt > 0 ) { |
| 3076 | printk(KERN_WARNING |
| 3077 | "md: %s(pid %d) used deprecated START_ARRAY ioctl. " |
| 3078 | "This will not be supported beyond 2.6\n", |
| 3079 | current->comm, current->pid); |
| 3080 | cnt--; |
| 3081 | } |
| 3082 | err = autostart_array(new_decode_dev(arg)); |
| 3083 | if (err) { |
| 3084 | printk(KERN_WARNING "md: autostart failed!\n"); |
| 3085 | goto abort; |
| 3086 | } |
| 3087 | goto done; |
| 3088 | } |
| 3089 | |
| 3090 | err = mddev_lock(mddev); |
| 3091 | if (err) { |
| 3092 | printk(KERN_INFO |
| 3093 | "md: ioctl lock interrupted, reason %d, cmd %d\n", |
| 3094 | err, cmd); |
| 3095 | goto abort; |
| 3096 | } |
| 3097 | |
| 3098 | switch (cmd) |
| 3099 | { |
| 3100 | case SET_ARRAY_INFO: |
| 3101 | { |
| 3102 | mdu_array_info_t info; |
| 3103 | if (!arg) |
| 3104 | memset(&info, 0, sizeof(info)); |
| 3105 | else if (copy_from_user(&info, argp, sizeof(info))) { |
| 3106 | err = -EFAULT; |
| 3107 | goto abort_unlock; |
| 3108 | } |
| 3109 | if (mddev->pers) { |
| 3110 | err = update_array_info(mddev, &info); |
| 3111 | if (err) { |
| 3112 | printk(KERN_WARNING "md: couldn't update" |
| 3113 | " array info. %d\n", err); |
| 3114 | goto abort_unlock; |
| 3115 | } |
| 3116 | goto done_unlock; |
| 3117 | } |
| 3118 | if (!list_empty(&mddev->disks)) { |
| 3119 | printk(KERN_WARNING |
| 3120 | "md: array %s already has disks!\n", |
| 3121 | mdname(mddev)); |
| 3122 | err = -EBUSY; |
| 3123 | goto abort_unlock; |
| 3124 | } |
| 3125 | if (mddev->raid_disks) { |
| 3126 | printk(KERN_WARNING |
| 3127 | "md: array %s already initialised!\n", |
| 3128 | mdname(mddev)); |
| 3129 | err = -EBUSY; |
| 3130 | goto abort_unlock; |
| 3131 | } |
| 3132 | err = set_array_info(mddev, &info); |
| 3133 | if (err) { |
| 3134 | printk(KERN_WARNING "md: couldn't set" |
| 3135 | " array info. %d\n", err); |
| 3136 | goto abort_unlock; |
| 3137 | } |
| 3138 | } |
| 3139 | goto done_unlock; |
| 3140 | |
| 3141 | default:; |
| 3142 | } |
| 3143 | |
| 3144 | /* |
| 3145 | * Commands querying/configuring an existing array: |
| 3146 | */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 3147 | /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY, |
| 3148 | * RUN_ARRAY, and SET_BITMAP_FILE are allowed */ |
| 3149 | if (!mddev->raid_disks && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY |
| 3150 | && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3151 | err = -ENODEV; |
| 3152 | goto abort_unlock; |
| 3153 | } |
| 3154 | |
| 3155 | /* |
| 3156 | * Commands even a read-only array can execute: |
| 3157 | */ |
| 3158 | switch (cmd) |
| 3159 | { |
| 3160 | case GET_ARRAY_INFO: |
| 3161 | err = get_array_info(mddev, argp); |
| 3162 | goto done_unlock; |
| 3163 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 3164 | case GET_BITMAP_FILE: |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 3165 | err = get_bitmap_file(mddev, argp); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 3166 | goto done_unlock; |
| 3167 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3168 | case GET_DISK_INFO: |
| 3169 | err = get_disk_info(mddev, argp); |
| 3170 | goto done_unlock; |
| 3171 | |
| 3172 | case RESTART_ARRAY_RW: |
| 3173 | err = restart_array(mddev); |
| 3174 | goto done_unlock; |
| 3175 | |
| 3176 | case STOP_ARRAY: |
| 3177 | err = do_md_stop (mddev, 0); |
| 3178 | goto done_unlock; |
| 3179 | |
| 3180 | case STOP_ARRAY_RO: |
| 3181 | err = do_md_stop (mddev, 1); |
| 3182 | goto done_unlock; |
| 3183 | |
| 3184 | /* |
| 3185 | * We have a problem here : there is no easy way to give a CHS |
| 3186 | * virtual geometry. We currently pretend that we have a 2 heads |
| 3187 | * 4 sectors (with a BIG number of cylinders...). This drives |
| 3188 | * dosfs just mad... ;-) |
| 3189 | */ |
| 3190 | case HDIO_GETGEO: |
| 3191 | if (!loc) { |
| 3192 | err = -EINVAL; |
| 3193 | goto abort_unlock; |
| 3194 | } |
| 3195 | err = put_user (2, (char __user *) &loc->heads); |
| 3196 | if (err) |
| 3197 | goto abort_unlock; |
| 3198 | err = put_user (4, (char __user *) &loc->sectors); |
| 3199 | if (err) |
| 3200 | goto abort_unlock; |
| 3201 | err = put_user(get_capacity(mddev->gendisk)/8, |
| 3202 | (short __user *) &loc->cylinders); |
| 3203 | if (err) |
| 3204 | goto abort_unlock; |
| 3205 | err = put_user (get_start_sect(inode->i_bdev), |
| 3206 | (long __user *) &loc->start); |
| 3207 | goto done_unlock; |
| 3208 | } |
| 3209 | |
| 3210 | /* |
| 3211 | * The remaining ioctls are changing the state of the |
| 3212 | * superblock, so we do not allow read-only arrays |
| 3213 | * here: |
| 3214 | */ |
| 3215 | if (mddev->ro) { |
| 3216 | err = -EROFS; |
| 3217 | goto abort_unlock; |
| 3218 | } |
| 3219 | |
| 3220 | switch (cmd) |
| 3221 | { |
| 3222 | case ADD_NEW_DISK: |
| 3223 | { |
| 3224 | mdu_disk_info_t info; |
| 3225 | if (copy_from_user(&info, argp, sizeof(info))) |
| 3226 | err = -EFAULT; |
| 3227 | else |
| 3228 | err = add_new_disk(mddev, &info); |
| 3229 | goto done_unlock; |
| 3230 | } |
| 3231 | |
| 3232 | case HOT_REMOVE_DISK: |
| 3233 | err = hot_remove_disk(mddev, new_decode_dev(arg)); |
| 3234 | goto done_unlock; |
| 3235 | |
| 3236 | case HOT_ADD_DISK: |
| 3237 | err = hot_add_disk(mddev, new_decode_dev(arg)); |
| 3238 | goto done_unlock; |
| 3239 | |
| 3240 | case SET_DISK_FAULTY: |
| 3241 | err = set_disk_faulty(mddev, new_decode_dev(arg)); |
| 3242 | goto done_unlock; |
| 3243 | |
| 3244 | case RUN_ARRAY: |
| 3245 | err = do_md_run (mddev); |
| 3246 | goto done_unlock; |
| 3247 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 3248 | case SET_BITMAP_FILE: |
| 3249 | err = set_bitmap_file(mddev, (int)arg); |
| 3250 | goto done_unlock; |
| 3251 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3252 | default: |
| 3253 | if (_IOC_TYPE(cmd) == MD_MAJOR) |
| 3254 | printk(KERN_WARNING "md: %s(pid %d) used" |
| 3255 | " obsolete MD ioctl, upgrade your" |
| 3256 | " software to use new ictls.\n", |
| 3257 | current->comm, current->pid); |
| 3258 | err = -EINVAL; |
| 3259 | goto abort_unlock; |
| 3260 | } |
| 3261 | |
| 3262 | done_unlock: |
| 3263 | abort_unlock: |
| 3264 | mddev_unlock(mddev); |
| 3265 | |
| 3266 | return err; |
| 3267 | done: |
| 3268 | if (err) |
| 3269 | MD_BUG(); |
| 3270 | abort: |
| 3271 | return err; |
| 3272 | } |
| 3273 | |
| 3274 | static int md_open(struct inode *inode, struct file *file) |
| 3275 | { |
| 3276 | /* |
| 3277 | * Succeed if we can lock the mddev, which confirms that |
| 3278 | * it isn't being stopped right now. |
| 3279 | */ |
| 3280 | mddev_t *mddev = inode->i_bdev->bd_disk->private_data; |
| 3281 | int err; |
| 3282 | |
| 3283 | if ((err = mddev_lock(mddev))) |
| 3284 | goto out; |
| 3285 | |
| 3286 | err = 0; |
| 3287 | mddev_get(mddev); |
| 3288 | mddev_unlock(mddev); |
| 3289 | |
| 3290 | check_disk_change(inode->i_bdev); |
| 3291 | out: |
| 3292 | return err; |
| 3293 | } |
| 3294 | |
| 3295 | static int md_release(struct inode *inode, struct file * file) |
| 3296 | { |
| 3297 | mddev_t *mddev = inode->i_bdev->bd_disk->private_data; |
| 3298 | |
| 3299 | if (!mddev) |
| 3300 | BUG(); |
| 3301 | mddev_put(mddev); |
| 3302 | |
| 3303 | return 0; |
| 3304 | } |
| 3305 | |
| 3306 | static int md_media_changed(struct gendisk *disk) |
| 3307 | { |
| 3308 | mddev_t *mddev = disk->private_data; |
| 3309 | |
| 3310 | return mddev->changed; |
| 3311 | } |
| 3312 | |
| 3313 | static int md_revalidate(struct gendisk *disk) |
| 3314 | { |
| 3315 | mddev_t *mddev = disk->private_data; |
| 3316 | |
| 3317 | mddev->changed = 0; |
| 3318 | return 0; |
| 3319 | } |
| 3320 | static struct block_device_operations md_fops = |
| 3321 | { |
| 3322 | .owner = THIS_MODULE, |
| 3323 | .open = md_open, |
| 3324 | .release = md_release, |
| 3325 | .ioctl = md_ioctl, |
| 3326 | .media_changed = md_media_changed, |
| 3327 | .revalidate_disk= md_revalidate, |
| 3328 | }; |
| 3329 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 3330 | static int md_thread(void * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3331 | { |
| 3332 | mdk_thread_t *thread = arg; |
| 3333 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3334 | /* |
| 3335 | * md_thread is a 'system-thread', it's priority should be very |
| 3336 | * high. We avoid resource deadlocks individually in each |
| 3337 | * raid personality. (RAID5 does preallocation) We also use RR and |
| 3338 | * the very same RT priority as kswapd, thus we will never get |
| 3339 | * into a priority inversion deadlock. |
| 3340 | * |
| 3341 | * we definitely have to have equal or higher priority than |
| 3342 | * bdflush, otherwise bdflush will deadlock if there are too |
| 3343 | * many dirty RAID5 blocks. |
| 3344 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3345 | |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 3346 | allow_signal(SIGKILL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3347 | complete(thread->event); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 3348 | while (!kthread_should_stop()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3349 | void (*run)(mddev_t *); |
| 3350 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 3351 | wait_event_interruptible_timeout(thread->wqueue, |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 3352 | test_bit(THREAD_WAKEUP, &thread->flags) |
| 3353 | || kthread_should_stop(), |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 3354 | thread->timeout); |
Christoph Lameter | 3e1d1d2 | 2005-06-24 23:13:50 -0700 | [diff] [blame] | 3355 | try_to_freeze(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3356 | |
| 3357 | clear_bit(THREAD_WAKEUP, &thread->flags); |
| 3358 | |
| 3359 | run = thread->run; |
| 3360 | if (run) |
| 3361 | run(thread->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3362 | } |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 3363 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3364 | return 0; |
| 3365 | } |
| 3366 | |
| 3367 | void md_wakeup_thread(mdk_thread_t *thread) |
| 3368 | { |
| 3369 | if (thread) { |
| 3370 | dprintk("md: waking up MD thread %s.\n", thread->tsk->comm); |
| 3371 | set_bit(THREAD_WAKEUP, &thread->flags); |
| 3372 | wake_up(&thread->wqueue); |
| 3373 | } |
| 3374 | } |
| 3375 | |
| 3376 | mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev, |
| 3377 | const char *name) |
| 3378 | { |
| 3379 | mdk_thread_t *thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3380 | struct completion event; |
| 3381 | |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 3382 | thread = kmalloc(sizeof(mdk_thread_t), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3383 | if (!thread) |
| 3384 | return NULL; |
| 3385 | |
| 3386 | memset(thread, 0, sizeof(mdk_thread_t)); |
| 3387 | init_waitqueue_head(&thread->wqueue); |
| 3388 | |
| 3389 | init_completion(&event); |
| 3390 | thread->event = &event; |
| 3391 | thread->run = run; |
| 3392 | thread->mddev = mddev; |
| 3393 | thread->name = name; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 3394 | thread->timeout = MAX_SCHEDULE_TIMEOUT; |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 3395 | thread->tsk = kthread_run(md_thread, thread, name, mdname(thread->mddev)); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 3396 | if (IS_ERR(thread->tsk)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3397 | kfree(thread); |
| 3398 | return NULL; |
| 3399 | } |
| 3400 | wait_for_completion(&event); |
| 3401 | return thread; |
| 3402 | } |
| 3403 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3404 | void md_unregister_thread(mdk_thread_t *thread) |
| 3405 | { |
NeilBrown | d28446f | 2005-04-16 15:26:41 -0700 | [diff] [blame] | 3406 | dprintk("interrupting MD-thread pid %d\n", thread->tsk->pid); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 3407 | |
| 3408 | kthread_stop(thread->tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3409 | kfree(thread); |
| 3410 | } |
| 3411 | |
| 3412 | void md_error(mddev_t *mddev, mdk_rdev_t *rdev) |
| 3413 | { |
| 3414 | if (!mddev) { |
| 3415 | MD_BUG(); |
| 3416 | return; |
| 3417 | } |
| 3418 | |
| 3419 | if (!rdev || rdev->faulty) |
| 3420 | return; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 3421 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3422 | dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n", |
| 3423 | mdname(mddev), |
| 3424 | MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev), |
| 3425 | __builtin_return_address(0),__builtin_return_address(1), |
| 3426 | __builtin_return_address(2),__builtin_return_address(3)); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 3427 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3428 | if (!mddev->pers->error_handler) |
| 3429 | return; |
| 3430 | mddev->pers->error_handler(mddev,rdev); |
| 3431 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 3432 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 3433 | md_wakeup_thread(mddev->thread); |
| 3434 | } |
| 3435 | |
| 3436 | /* seq_file implementation /proc/mdstat */ |
| 3437 | |
| 3438 | static void status_unused(struct seq_file *seq) |
| 3439 | { |
| 3440 | int i = 0; |
| 3441 | mdk_rdev_t *rdev; |
| 3442 | struct list_head *tmp; |
| 3443 | |
| 3444 | seq_printf(seq, "unused devices: "); |
| 3445 | |
| 3446 | ITERATE_RDEV_PENDING(rdev,tmp) { |
| 3447 | char b[BDEVNAME_SIZE]; |
| 3448 | i++; |
| 3449 | seq_printf(seq, "%s ", |
| 3450 | bdevname(rdev->bdev,b)); |
| 3451 | } |
| 3452 | if (!i) |
| 3453 | seq_printf(seq, "<none>"); |
| 3454 | |
| 3455 | seq_printf(seq, "\n"); |
| 3456 | } |
| 3457 | |
| 3458 | |
| 3459 | static void status_resync(struct seq_file *seq, mddev_t * mddev) |
| 3460 | { |
| 3461 | unsigned long max_blocks, resync, res, dt, db, rt; |
| 3462 | |
| 3463 | resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active))/2; |
| 3464 | |
| 3465 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 3466 | max_blocks = mddev->resync_max_sectors >> 1; |
| 3467 | else |
| 3468 | max_blocks = mddev->size; |
| 3469 | |
| 3470 | /* |
| 3471 | * Should not happen. |
| 3472 | */ |
| 3473 | if (!max_blocks) { |
| 3474 | MD_BUG(); |
| 3475 | return; |
| 3476 | } |
| 3477 | res = (resync/1024)*1000/(max_blocks/1024 + 1); |
| 3478 | { |
| 3479 | int i, x = res/50, y = 20-x; |
| 3480 | seq_printf(seq, "["); |
| 3481 | for (i = 0; i < x; i++) |
| 3482 | seq_printf(seq, "="); |
| 3483 | seq_printf(seq, ">"); |
| 3484 | for (i = 0; i < y; i++) |
| 3485 | seq_printf(seq, "."); |
| 3486 | seq_printf(seq, "] "); |
| 3487 | } |
| 3488 | seq_printf(seq, " %s =%3lu.%lu%% (%lu/%lu)", |
| 3489 | (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ? |
| 3490 | "resync" : "recovery"), |
| 3491 | res/10, res % 10, resync, max_blocks); |
| 3492 | |
| 3493 | /* |
| 3494 | * We do not want to overflow, so the order of operands and |
| 3495 | * the * 100 / 100 trick are important. We do a +1 to be |
| 3496 | * safe against division by zero. We only estimate anyway. |
| 3497 | * |
| 3498 | * dt: time from mark until now |
| 3499 | * db: blocks written from mark until now |
| 3500 | * rt: remaining time |
| 3501 | */ |
| 3502 | dt = ((jiffies - mddev->resync_mark) / HZ); |
| 3503 | if (!dt) dt++; |
| 3504 | db = resync - (mddev->resync_mark_cnt/2); |
| 3505 | rt = (dt * ((max_blocks-resync) / (db/100+1)))/100; |
| 3506 | |
| 3507 | seq_printf(seq, " finish=%lu.%lumin", rt / 60, (rt % 60)/6); |
| 3508 | |
| 3509 | seq_printf(seq, " speed=%ldK/sec", db/dt); |
| 3510 | } |
| 3511 | |
| 3512 | static void *md_seq_start(struct seq_file *seq, loff_t *pos) |
| 3513 | { |
| 3514 | struct list_head *tmp; |
| 3515 | loff_t l = *pos; |
| 3516 | mddev_t *mddev; |
| 3517 | |
| 3518 | if (l >= 0x10000) |
| 3519 | return NULL; |
| 3520 | if (!l--) |
| 3521 | /* header */ |
| 3522 | return (void*)1; |
| 3523 | |
| 3524 | spin_lock(&all_mddevs_lock); |
| 3525 | list_for_each(tmp,&all_mddevs) |
| 3526 | if (!l--) { |
| 3527 | mddev = list_entry(tmp, mddev_t, all_mddevs); |
| 3528 | mddev_get(mddev); |
| 3529 | spin_unlock(&all_mddevs_lock); |
| 3530 | return mddev; |
| 3531 | } |
| 3532 | spin_unlock(&all_mddevs_lock); |
| 3533 | if (!l--) |
| 3534 | return (void*)2;/* tail */ |
| 3535 | return NULL; |
| 3536 | } |
| 3537 | |
| 3538 | static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 3539 | { |
| 3540 | struct list_head *tmp; |
| 3541 | mddev_t *next_mddev, *mddev = v; |
| 3542 | |
| 3543 | ++*pos; |
| 3544 | if (v == (void*)2) |
| 3545 | return NULL; |
| 3546 | |
| 3547 | spin_lock(&all_mddevs_lock); |
| 3548 | if (v == (void*)1) |
| 3549 | tmp = all_mddevs.next; |
| 3550 | else |
| 3551 | tmp = mddev->all_mddevs.next; |
| 3552 | if (tmp != &all_mddevs) |
| 3553 | next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs)); |
| 3554 | else { |
| 3555 | next_mddev = (void*)2; |
| 3556 | *pos = 0x10000; |
| 3557 | } |
| 3558 | spin_unlock(&all_mddevs_lock); |
| 3559 | |
| 3560 | if (v != (void*)1) |
| 3561 | mddev_put(mddev); |
| 3562 | return next_mddev; |
| 3563 | |
| 3564 | } |
| 3565 | |
| 3566 | static void md_seq_stop(struct seq_file *seq, void *v) |
| 3567 | { |
| 3568 | mddev_t *mddev = v; |
| 3569 | |
| 3570 | if (mddev && v != (void*)1 && v != (void*)2) |
| 3571 | mddev_put(mddev); |
| 3572 | } |
| 3573 | |
| 3574 | static int md_seq_show(struct seq_file *seq, void *v) |
| 3575 | { |
| 3576 | mddev_t *mddev = v; |
| 3577 | sector_t size; |
| 3578 | struct list_head *tmp2; |
| 3579 | mdk_rdev_t *rdev; |
| 3580 | int i; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 3581 | struct bitmap *bitmap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3582 | |
| 3583 | if (v == (void*)1) { |
| 3584 | seq_printf(seq, "Personalities : "); |
| 3585 | spin_lock(&pers_lock); |
| 3586 | for (i = 0; i < MAX_PERSONALITY; i++) |
| 3587 | if (pers[i]) |
| 3588 | seq_printf(seq, "[%s] ", pers[i]->name); |
| 3589 | |
| 3590 | spin_unlock(&pers_lock); |
| 3591 | seq_printf(seq, "\n"); |
| 3592 | return 0; |
| 3593 | } |
| 3594 | if (v == (void*)2) { |
| 3595 | status_unused(seq); |
| 3596 | return 0; |
| 3597 | } |
| 3598 | |
| 3599 | if (mddev_lock(mddev)!=0) |
| 3600 | return -EINTR; |
| 3601 | if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) { |
| 3602 | seq_printf(seq, "%s : %sactive", mdname(mddev), |
| 3603 | mddev->pers ? "" : "in"); |
| 3604 | if (mddev->pers) { |
| 3605 | if (mddev->ro) |
| 3606 | seq_printf(seq, " (read-only)"); |
| 3607 | seq_printf(seq, " %s", mddev->pers->name); |
| 3608 | } |
| 3609 | |
| 3610 | size = 0; |
| 3611 | ITERATE_RDEV(mddev,rdev,tmp2) { |
| 3612 | char b[BDEVNAME_SIZE]; |
| 3613 | seq_printf(seq, " %s[%d]", |
| 3614 | bdevname(rdev->bdev,b), rdev->desc_nr); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 3615 | if (test_bit(WriteMostly, &rdev->flags)) |
| 3616 | seq_printf(seq, "(W)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3617 | if (rdev->faulty) { |
| 3618 | seq_printf(seq, "(F)"); |
| 3619 | continue; |
NeilBrown | b325a32 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 3620 | } else if (rdev->raid_disk < 0) |
| 3621 | seq_printf(seq, "(S)"); /* spare */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3622 | size += rdev->size; |
| 3623 | } |
| 3624 | |
| 3625 | if (!list_empty(&mddev->disks)) { |
| 3626 | if (mddev->pers) |
| 3627 | seq_printf(seq, "\n %llu blocks", |
| 3628 | (unsigned long long)mddev->array_size); |
| 3629 | else |
| 3630 | seq_printf(seq, "\n %llu blocks", |
| 3631 | (unsigned long long)size); |
| 3632 | } |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 3633 | if (mddev->persistent) { |
| 3634 | if (mddev->major_version != 0 || |
| 3635 | mddev->minor_version != 90) { |
| 3636 | seq_printf(seq," super %d.%d", |
| 3637 | mddev->major_version, |
| 3638 | mddev->minor_version); |
| 3639 | } |
| 3640 | } else |
| 3641 | seq_printf(seq, " super non-persistent"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3642 | |
| 3643 | if (mddev->pers) { |
| 3644 | mddev->pers->status (seq, mddev); |
| 3645 | seq_printf(seq, "\n "); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 3646 | if (mddev->curr_resync > 2) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3647 | status_resync (seq, mddev); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 3648 | seq_printf(seq, "\n "); |
| 3649 | } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2) |
| 3650 | seq_printf(seq, " resync=DELAYED\n "); |
| 3651 | } else |
| 3652 | seq_printf(seq, "\n "); |
| 3653 | |
| 3654 | if ((bitmap = mddev->bitmap)) { |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 3655 | unsigned long chunk_kb; |
| 3656 | unsigned long flags; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 3657 | spin_lock_irqsave(&bitmap->lock, flags); |
| 3658 | chunk_kb = bitmap->chunksize >> 10; |
| 3659 | seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], " |
| 3660 | "%lu%s chunk", |
| 3661 | bitmap->pages - bitmap->missing_pages, |
| 3662 | bitmap->pages, |
| 3663 | (bitmap->pages - bitmap->missing_pages) |
| 3664 | << (PAGE_SHIFT - 10), |
| 3665 | chunk_kb ? chunk_kb : bitmap->chunksize, |
| 3666 | chunk_kb ? "KB" : "B"); |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 3667 | if (bitmap->file) { |
| 3668 | seq_printf(seq, ", file: "); |
| 3669 | seq_path(seq, bitmap->file->f_vfsmnt, |
| 3670 | bitmap->file->f_dentry," \t\n"); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 3671 | } |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 3672 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 3673 | seq_printf(seq, "\n"); |
| 3674 | spin_unlock_irqrestore(&bitmap->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3675 | } |
| 3676 | |
| 3677 | seq_printf(seq, "\n"); |
| 3678 | } |
| 3679 | mddev_unlock(mddev); |
| 3680 | |
| 3681 | return 0; |
| 3682 | } |
| 3683 | |
| 3684 | static struct seq_operations md_seq_ops = { |
| 3685 | .start = md_seq_start, |
| 3686 | .next = md_seq_next, |
| 3687 | .stop = md_seq_stop, |
| 3688 | .show = md_seq_show, |
| 3689 | }; |
| 3690 | |
| 3691 | static int md_seq_open(struct inode *inode, struct file *file) |
| 3692 | { |
| 3693 | int error; |
| 3694 | |
| 3695 | error = seq_open(file, &md_seq_ops); |
| 3696 | return error; |
| 3697 | } |
| 3698 | |
| 3699 | static struct file_operations md_seq_fops = { |
| 3700 | .open = md_seq_open, |
| 3701 | .read = seq_read, |
| 3702 | .llseek = seq_lseek, |
| 3703 | .release = seq_release, |
| 3704 | }; |
| 3705 | |
| 3706 | int register_md_personality(int pnum, mdk_personality_t *p) |
| 3707 | { |
| 3708 | if (pnum >= MAX_PERSONALITY) { |
| 3709 | printk(KERN_ERR |
| 3710 | "md: tried to install personality %s as nr %d, but max is %lu\n", |
| 3711 | p->name, pnum, MAX_PERSONALITY-1); |
| 3712 | return -EINVAL; |
| 3713 | } |
| 3714 | |
| 3715 | spin_lock(&pers_lock); |
| 3716 | if (pers[pnum]) { |
| 3717 | spin_unlock(&pers_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3718 | return -EBUSY; |
| 3719 | } |
| 3720 | |
| 3721 | pers[pnum] = p; |
| 3722 | printk(KERN_INFO "md: %s personality registered as nr %d\n", p->name, pnum); |
| 3723 | spin_unlock(&pers_lock); |
| 3724 | return 0; |
| 3725 | } |
| 3726 | |
| 3727 | int unregister_md_personality(int pnum) |
| 3728 | { |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 3729 | if (pnum >= MAX_PERSONALITY) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3730 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3731 | |
| 3732 | printk(KERN_INFO "md: %s personality unregistered\n", pers[pnum]->name); |
| 3733 | spin_lock(&pers_lock); |
| 3734 | pers[pnum] = NULL; |
| 3735 | spin_unlock(&pers_lock); |
| 3736 | return 0; |
| 3737 | } |
| 3738 | |
| 3739 | static int is_mddev_idle(mddev_t *mddev) |
| 3740 | { |
| 3741 | mdk_rdev_t * rdev; |
| 3742 | struct list_head *tmp; |
| 3743 | int idle; |
| 3744 | unsigned long curr_events; |
| 3745 | |
| 3746 | idle = 1; |
| 3747 | ITERATE_RDEV(mddev,rdev,tmp) { |
| 3748 | struct gendisk *disk = rdev->bdev->bd_contains->bd_disk; |
Jens Axboe | a362357 | 2005-11-01 09:26:16 +0100 | [diff] [blame] | 3749 | curr_events = disk_stat_read(disk, sectors[0]) + |
| 3750 | disk_stat_read(disk, sectors[1]) - |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3751 | atomic_read(&disk->sync_io); |
| 3752 | /* Allow some slack between valud of curr_events and last_events, |
| 3753 | * as there are some uninteresting races. |
| 3754 | * Note: the following is an unsigned comparison. |
| 3755 | */ |
| 3756 | if ((curr_events - rdev->last_events + 32) > 64) { |
| 3757 | rdev->last_events = curr_events; |
| 3758 | idle = 0; |
| 3759 | } |
| 3760 | } |
| 3761 | return idle; |
| 3762 | } |
| 3763 | |
| 3764 | void md_done_sync(mddev_t *mddev, int blocks, int ok) |
| 3765 | { |
| 3766 | /* another "blocks" (512byte) blocks have been synced */ |
| 3767 | atomic_sub(blocks, &mddev->recovery_active); |
| 3768 | wake_up(&mddev->recovery_wait); |
| 3769 | if (!ok) { |
| 3770 | set_bit(MD_RECOVERY_ERR, &mddev->recovery); |
| 3771 | md_wakeup_thread(mddev->thread); |
| 3772 | // stop recovery, signal do_sync .... |
| 3773 | } |
| 3774 | } |
| 3775 | |
| 3776 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 3777 | /* md_write_start(mddev, bi) |
| 3778 | * If we need to update some array metadata (e.g. 'active' flag |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 3779 | * in superblock) before writing, schedule a superblock update |
| 3780 | * and wait for it to complete. |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 3781 | */ |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 3782 | void md_write_start(mddev_t *mddev, struct bio *bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3783 | { |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 3784 | if (bio_data_dir(bi) != WRITE) |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 3785 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 3786 | |
| 3787 | atomic_inc(&mddev->writes_pending); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 3788 | if (mddev->in_sync) { |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 3789 | spin_lock(&mddev->write_lock); |
| 3790 | if (mddev->in_sync) { |
| 3791 | mddev->in_sync = 0; |
| 3792 | mddev->sb_dirty = 1; |
| 3793 | md_wakeup_thread(mddev->thread); |
| 3794 | } |
| 3795 | spin_unlock(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 3796 | } |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 3797 | wait_event(mddev->sb_wait, mddev->sb_dirty==0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3798 | } |
| 3799 | |
| 3800 | void md_write_end(mddev_t *mddev) |
| 3801 | { |
| 3802 | if (atomic_dec_and_test(&mddev->writes_pending)) { |
| 3803 | if (mddev->safemode == 2) |
| 3804 | md_wakeup_thread(mddev->thread); |
| 3805 | else |
| 3806 | mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay); |
| 3807 | } |
| 3808 | } |
| 3809 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 3810 | static DECLARE_WAIT_QUEUE_HEAD(resync_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3811 | |
| 3812 | #define SYNC_MARKS 10 |
| 3813 | #define SYNC_MARK_STEP (3*HZ) |
| 3814 | static void md_do_sync(mddev_t *mddev) |
| 3815 | { |
| 3816 | mddev_t *mddev2; |
| 3817 | unsigned int currspeed = 0, |
| 3818 | window; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 3819 | sector_t max_sectors,j, io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3820 | unsigned long mark[SYNC_MARKS]; |
| 3821 | sector_t mark_cnt[SYNC_MARKS]; |
| 3822 | int last_mark,m; |
| 3823 | struct list_head *tmp; |
| 3824 | sector_t last_check; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 3825 | int skipped = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3826 | |
| 3827 | /* just incase thread restarts... */ |
| 3828 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery)) |
| 3829 | return; |
| 3830 | |
| 3831 | /* we overload curr_resync somewhat here. |
| 3832 | * 0 == not engaged in resync at all |
| 3833 | * 2 == checking that there is no conflict with another sync |
| 3834 | * 1 == like 2, but have yielded to allow conflicting resync to |
| 3835 | * commense |
| 3836 | * other == active in resync - this many blocks |
| 3837 | * |
| 3838 | * Before starting a resync we must have set curr_resync to |
| 3839 | * 2, and then checked that every "conflicting" array has curr_resync |
| 3840 | * less than ours. When we find one that is the same or higher |
| 3841 | * we wait on resync_wait. To avoid deadlock, we reduce curr_resync |
| 3842 | * to 1 if we choose to yield (based arbitrarily on address of mddev structure). |
| 3843 | * This will mean we have to start checking from the beginning again. |
| 3844 | * |
| 3845 | */ |
| 3846 | |
| 3847 | do { |
| 3848 | mddev->curr_resync = 2; |
| 3849 | |
| 3850 | try_again: |
NeilBrown | 8712e55 | 2005-10-26 01:58:58 -0700 | [diff] [blame] | 3851 | if (signal_pending(current) || |
| 3852 | kthread_should_stop()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3853 | flush_signals(current); |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 3854 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3855 | goto skip; |
| 3856 | } |
| 3857 | ITERATE_MDDEV(mddev2,tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3858 | if (mddev2 == mddev) |
| 3859 | continue; |
| 3860 | if (mddev2->curr_resync && |
| 3861 | match_mddev_units(mddev,mddev2)) { |
| 3862 | DEFINE_WAIT(wq); |
| 3863 | if (mddev < mddev2 && mddev->curr_resync == 2) { |
| 3864 | /* arbitrarily yield */ |
| 3865 | mddev->curr_resync = 1; |
| 3866 | wake_up(&resync_wait); |
| 3867 | } |
| 3868 | if (mddev > mddev2 && mddev->curr_resync == 1) |
| 3869 | /* no need to wait here, we can wait the next |
| 3870 | * time 'round when curr_resync == 2 |
| 3871 | */ |
| 3872 | continue; |
| 3873 | prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE); |
NeilBrown | 8712e55 | 2005-10-26 01:58:58 -0700 | [diff] [blame] | 3874 | if (!signal_pending(current) && |
| 3875 | !kthread_should_stop() && |
| 3876 | mddev2->curr_resync >= mddev->curr_resync) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3877 | printk(KERN_INFO "md: delaying resync of %s" |
| 3878 | " until %s has finished resync (they" |
| 3879 | " share one or more physical units)\n", |
| 3880 | mdname(mddev), mdname(mddev2)); |
| 3881 | mddev_put(mddev2); |
| 3882 | schedule(); |
| 3883 | finish_wait(&resync_wait, &wq); |
| 3884 | goto try_again; |
| 3885 | } |
| 3886 | finish_wait(&resync_wait, &wq); |
| 3887 | } |
| 3888 | } |
| 3889 | } while (mddev->curr_resync < 2); |
| 3890 | |
| 3891 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 3892 | /* resync follows the size requested by the personality, |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 3893 | * which defaults to physical size, but can be virtual size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3894 | */ |
| 3895 | max_sectors = mddev->resync_max_sectors; |
| 3896 | else |
| 3897 | /* recovery follows the physical size of devices */ |
| 3898 | max_sectors = mddev->size << 1; |
| 3899 | |
| 3900 | printk(KERN_INFO "md: syncing RAID array %s\n", mdname(mddev)); |
| 3901 | printk(KERN_INFO "md: minimum _guaranteed_ reconstruction speed:" |
| 3902 | " %d KB/sec/disc.\n", sysctl_speed_limit_min); |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 3903 | printk(KERN_INFO "md: using maximum available idle IO bandwidth " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3904 | "(but not more than %d KB/sec) for reconstruction.\n", |
| 3905 | sysctl_speed_limit_max); |
| 3906 | |
| 3907 | is_mddev_idle(mddev); /* this also initializes IO event counters */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 3908 | /* we don't use the checkpoint if there's a bitmap */ |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame^] | 3909 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && !mddev->bitmap |
| 3910 | && ! test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3911 | j = mddev->recovery_cp; |
| 3912 | else |
| 3913 | j = 0; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 3914 | io_sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3915 | for (m = 0; m < SYNC_MARKS; m++) { |
| 3916 | mark[m] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 3917 | mark_cnt[m] = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3918 | } |
| 3919 | last_mark = 0; |
| 3920 | mddev->resync_mark = mark[last_mark]; |
| 3921 | mddev->resync_mark_cnt = mark_cnt[last_mark]; |
| 3922 | |
| 3923 | /* |
| 3924 | * Tune reconstruction: |
| 3925 | */ |
| 3926 | window = 32*(PAGE_SIZE/512); |
| 3927 | printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n", |
| 3928 | window/2,(unsigned long long) max_sectors/2); |
| 3929 | |
| 3930 | atomic_set(&mddev->recovery_active, 0); |
| 3931 | init_waitqueue_head(&mddev->recovery_wait); |
| 3932 | last_check = 0; |
| 3933 | |
| 3934 | if (j>2) { |
| 3935 | printk(KERN_INFO |
| 3936 | "md: resuming recovery of %s from checkpoint.\n", |
| 3937 | mdname(mddev)); |
| 3938 | mddev->curr_resync = j; |
| 3939 | } |
| 3940 | |
| 3941 | while (j < max_sectors) { |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 3942 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3943 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 3944 | skipped = 0; |
| 3945 | sectors = mddev->pers->sync_request(mddev, j, &skipped, |
| 3946 | currspeed < sysctl_speed_limit_min); |
| 3947 | if (sectors == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3948 | set_bit(MD_RECOVERY_ERR, &mddev->recovery); |
| 3949 | goto out; |
| 3950 | } |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 3951 | |
| 3952 | if (!skipped) { /* actual IO requested */ |
| 3953 | io_sectors += sectors; |
| 3954 | atomic_add(sectors, &mddev->recovery_active); |
| 3955 | } |
| 3956 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3957 | j += sectors; |
| 3958 | if (j>1) mddev->curr_resync = j; |
| 3959 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 3960 | |
| 3961 | if (last_check + window > io_sectors || j == max_sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3962 | continue; |
| 3963 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 3964 | last_check = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3965 | |
| 3966 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery) || |
| 3967 | test_bit(MD_RECOVERY_ERR, &mddev->recovery)) |
| 3968 | break; |
| 3969 | |
| 3970 | repeat: |
| 3971 | if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) { |
| 3972 | /* step marks */ |
| 3973 | int next = (last_mark+1) % SYNC_MARKS; |
| 3974 | |
| 3975 | mddev->resync_mark = mark[next]; |
| 3976 | mddev->resync_mark_cnt = mark_cnt[next]; |
| 3977 | mark[next] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 3978 | mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3979 | last_mark = next; |
| 3980 | } |
| 3981 | |
| 3982 | |
NeilBrown | 8712e55 | 2005-10-26 01:58:58 -0700 | [diff] [blame] | 3983 | if (signal_pending(current) || kthread_should_stop()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3984 | /* |
| 3985 | * got a signal, exit. |
| 3986 | */ |
| 3987 | printk(KERN_INFO |
| 3988 | "md: md_do_sync() got signal ... exiting\n"); |
| 3989 | flush_signals(current); |
| 3990 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 3991 | goto out; |
| 3992 | } |
| 3993 | |
| 3994 | /* |
| 3995 | * this loop exits only if either when we are slower than |
| 3996 | * the 'hard' speed limit, or the system was IO-idle for |
| 3997 | * a jiffy. |
| 3998 | * the system might be non-idle CPU-wise, but we only care |
| 3999 | * about not overloading the IO subsystem. (things like an |
| 4000 | * e2fsck being done on the RAID array should execute fast) |
| 4001 | */ |
| 4002 | mddev->queue->unplug_fn(mddev->queue); |
| 4003 | cond_resched(); |
| 4004 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 4005 | currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2 |
| 4006 | /((jiffies-mddev->resync_mark)/HZ +1) +1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4007 | |
| 4008 | if (currspeed > sysctl_speed_limit_min) { |
| 4009 | if ((currspeed > sysctl_speed_limit_max) || |
| 4010 | !is_mddev_idle(mddev)) { |
| 4011 | msleep_interruptible(250); |
| 4012 | goto repeat; |
| 4013 | } |
| 4014 | } |
| 4015 | } |
| 4016 | printk(KERN_INFO "md: %s: sync done.\n",mdname(mddev)); |
| 4017 | /* |
| 4018 | * this also signals 'finished resyncing' to md_stop |
| 4019 | */ |
| 4020 | out: |
| 4021 | mddev->queue->unplug_fn(mddev->queue); |
| 4022 | |
| 4023 | wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active)); |
| 4024 | |
| 4025 | /* tell personality that we are finished */ |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 4026 | mddev->pers->sync_request(mddev, max_sectors, &skipped, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4027 | |
| 4028 | if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) && |
| 4029 | mddev->curr_resync > 2 && |
| 4030 | mddev->curr_resync >= mddev->recovery_cp) { |
| 4031 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 4032 | printk(KERN_INFO |
| 4033 | "md: checkpointing recovery of %s.\n", |
| 4034 | mdname(mddev)); |
| 4035 | mddev->recovery_cp = mddev->curr_resync; |
| 4036 | } else |
| 4037 | mddev->recovery_cp = MaxSector; |
| 4038 | } |
| 4039 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4040 | skip: |
| 4041 | mddev->curr_resync = 0; |
| 4042 | wake_up(&resync_wait); |
| 4043 | set_bit(MD_RECOVERY_DONE, &mddev->recovery); |
| 4044 | md_wakeup_thread(mddev->thread); |
| 4045 | } |
| 4046 | |
| 4047 | |
| 4048 | /* |
| 4049 | * This routine is regularly called by all per-raid-array threads to |
| 4050 | * deal with generic issues like resync and super-block update. |
| 4051 | * Raid personalities that don't have a thread (linear/raid0) do not |
| 4052 | * need this as they never do any recovery or update the superblock. |
| 4053 | * |
| 4054 | * It does not do any resync itself, but rather "forks" off other threads |
| 4055 | * to do that as needed. |
| 4056 | * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in |
| 4057 | * "->recovery" and create a thread at ->sync_thread. |
| 4058 | * When the thread finishes it sets MD_RECOVERY_DONE (and might set MD_RECOVERY_ERR) |
| 4059 | * and wakeups up this thread which will reap the thread and finish up. |
| 4060 | * This thread also removes any faulty devices (with nr_pending == 0). |
| 4061 | * |
| 4062 | * The overall approach is: |
| 4063 | * 1/ if the superblock needs updating, update it. |
| 4064 | * 2/ If a recovery thread is running, don't do anything else. |
| 4065 | * 3/ If recovery has finished, clean up, possibly marking spares active. |
| 4066 | * 4/ If there are any faulty devices, remove them. |
| 4067 | * 5/ If array is degraded, try to add spares devices |
| 4068 | * 6/ If array has spares or is not in-sync, start a resync thread. |
| 4069 | */ |
| 4070 | void md_check_recovery(mddev_t *mddev) |
| 4071 | { |
| 4072 | mdk_rdev_t *rdev; |
| 4073 | struct list_head *rtmp; |
| 4074 | |
| 4075 | |
NeilBrown | 5f40402 | 2005-06-21 17:17:16 -0700 | [diff] [blame] | 4076 | if (mddev->bitmap) |
| 4077 | bitmap_daemon_work(mddev->bitmap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4078 | |
| 4079 | if (mddev->ro) |
| 4080 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 4081 | |
| 4082 | if (signal_pending(current)) { |
| 4083 | if (mddev->pers->sync_request) { |
| 4084 | printk(KERN_INFO "md: %s in immediate safe mode\n", |
| 4085 | mdname(mddev)); |
| 4086 | mddev->safemode = 2; |
| 4087 | } |
| 4088 | flush_signals(current); |
| 4089 | } |
| 4090 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4091 | if ( ! ( |
| 4092 | mddev->sb_dirty || |
| 4093 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 4094 | test_bit(MD_RECOVERY_DONE, &mddev->recovery) || |
| 4095 | (mddev->safemode == 1) || |
| 4096 | (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending) |
| 4097 | && !mddev->in_sync && mddev->recovery_cp == MaxSector) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4098 | )) |
| 4099 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 4100 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4101 | if (mddev_trylock(mddev)==0) { |
| 4102 | int spares =0; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 4103 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 4104 | spin_lock(&mddev->write_lock); |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 4105 | if (mddev->safemode && !atomic_read(&mddev->writes_pending) && |
| 4106 | !mddev->in_sync && mddev->recovery_cp == MaxSector) { |
| 4107 | mddev->in_sync = 1; |
| 4108 | mddev->sb_dirty = 1; |
| 4109 | } |
| 4110 | if (mddev->safemode == 1) |
| 4111 | mddev->safemode = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 4112 | spin_unlock(&mddev->write_lock); |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 4113 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4114 | if (mddev->sb_dirty) |
| 4115 | md_update_sb(mddev); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 4116 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 4117 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4118 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 4119 | !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) { |
| 4120 | /* resync/recovery still happening */ |
| 4121 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4122 | goto unlock; |
| 4123 | } |
| 4124 | if (mddev->sync_thread) { |
| 4125 | /* resync has finished, collect result */ |
| 4126 | md_unregister_thread(mddev->sync_thread); |
| 4127 | mddev->sync_thread = NULL; |
| 4128 | if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) && |
| 4129 | !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 4130 | /* success...*/ |
| 4131 | /* activate any spares */ |
| 4132 | mddev->pers->spare_active(mddev); |
| 4133 | } |
| 4134 | md_update_sb(mddev); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 4135 | |
| 4136 | /* if array is no-longer degraded, then any saved_raid_disk |
| 4137 | * information must be scrapped |
| 4138 | */ |
| 4139 | if (!mddev->degraded) |
| 4140 | ITERATE_RDEV(mddev,rdev,rtmp) |
| 4141 | rdev->saved_raid_disk = -1; |
| 4142 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4143 | mddev->recovery = 0; |
| 4144 | /* flag recovery needed just to double check */ |
| 4145 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4146 | goto unlock; |
| 4147 | } |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame^] | 4148 | /* Clear some bits that don't mean anything, but |
| 4149 | * might be left set |
| 4150 | */ |
| 4151 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4152 | clear_bit(MD_RECOVERY_ERR, &mddev->recovery); |
| 4153 | clear_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 4154 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4155 | |
| 4156 | /* no recovery is running. |
| 4157 | * remove any failed drives, then |
| 4158 | * add spares if possible. |
| 4159 | * Spare are also removed and re-added, to allow |
| 4160 | * the personality to fail the re-add. |
| 4161 | */ |
| 4162 | ITERATE_RDEV(mddev,rdev,rtmp) |
| 4163 | if (rdev->raid_disk >= 0 && |
| 4164 | (rdev->faulty || ! rdev->in_sync) && |
| 4165 | atomic_read(&rdev->nr_pending)==0) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 4166 | if (mddev->pers->hot_remove_disk(mddev, rdev->raid_disk)==0) { |
| 4167 | char nm[20]; |
| 4168 | sprintf(nm,"rd%d", rdev->raid_disk); |
| 4169 | sysfs_remove_link(&mddev->kobj, nm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4170 | rdev->raid_disk = -1; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 4171 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4172 | } |
| 4173 | |
| 4174 | if (mddev->degraded) { |
| 4175 | ITERATE_RDEV(mddev,rdev,rtmp) |
| 4176 | if (rdev->raid_disk < 0 |
| 4177 | && !rdev->faulty) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 4178 | if (mddev->pers->hot_add_disk(mddev,rdev)) { |
| 4179 | char nm[20]; |
| 4180 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 4181 | sysfs_create_link(&mddev->kobj, &rdev->kobj, nm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4182 | spares++; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 4183 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4184 | break; |
| 4185 | } |
| 4186 | } |
| 4187 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame^] | 4188 | if (spares) { |
| 4189 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 4190 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
| 4191 | } else if (mddev->recovery_cp < MaxSector) { |
| 4192 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 4193 | } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 4194 | /* nothing to be done ... */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4195 | goto unlock; |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame^] | 4196 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4197 | if (mddev->pers->sync_request) { |
| 4198 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 4199 | if (spares && mddev->bitmap && ! mddev->bitmap->file) { |
| 4200 | /* We are adding a device or devices to an array |
| 4201 | * which has the bitmap stored on all devices. |
| 4202 | * So make sure all bitmap pages get written |
| 4203 | */ |
| 4204 | bitmap_write_all(mddev->bitmap); |
| 4205 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4206 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 4207 | mddev, |
| 4208 | "%s_resync"); |
| 4209 | if (!mddev->sync_thread) { |
| 4210 | printk(KERN_ERR "%s: could not start resync" |
| 4211 | " thread...\n", |
| 4212 | mdname(mddev)); |
| 4213 | /* leave the spares where they are, it shouldn't hurt */ |
| 4214 | mddev->recovery = 0; |
| 4215 | } else { |
| 4216 | md_wakeup_thread(mddev->sync_thread); |
| 4217 | } |
| 4218 | } |
| 4219 | unlock: |
| 4220 | mddev_unlock(mddev); |
| 4221 | } |
| 4222 | } |
| 4223 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 4224 | static int md_notify_reboot(struct notifier_block *this, |
| 4225 | unsigned long code, void *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4226 | { |
| 4227 | struct list_head *tmp; |
| 4228 | mddev_t *mddev; |
| 4229 | |
| 4230 | if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) { |
| 4231 | |
| 4232 | printk(KERN_INFO "md: stopping all md devices.\n"); |
| 4233 | |
| 4234 | ITERATE_MDDEV(mddev,tmp) |
| 4235 | if (mddev_trylock(mddev)==0) |
| 4236 | do_md_stop (mddev, 1); |
| 4237 | /* |
| 4238 | * certain more exotic SCSI devices are known to be |
| 4239 | * volatile wrt too early system reboots. While the |
| 4240 | * right place to handle this issue is the given |
| 4241 | * driver, we do want to have a safe RAID driver ... |
| 4242 | */ |
| 4243 | mdelay(1000*1); |
| 4244 | } |
| 4245 | return NOTIFY_DONE; |
| 4246 | } |
| 4247 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 4248 | static struct notifier_block md_notifier = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4249 | .notifier_call = md_notify_reboot, |
| 4250 | .next = NULL, |
| 4251 | .priority = INT_MAX, /* before any real devices */ |
| 4252 | }; |
| 4253 | |
| 4254 | static void md_geninit(void) |
| 4255 | { |
| 4256 | struct proc_dir_entry *p; |
| 4257 | |
| 4258 | dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t)); |
| 4259 | |
| 4260 | p = create_proc_entry("mdstat", S_IRUGO, NULL); |
| 4261 | if (p) |
| 4262 | p->proc_fops = &md_seq_fops; |
| 4263 | } |
| 4264 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 4265 | static int __init md_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4266 | { |
| 4267 | int minor; |
| 4268 | |
| 4269 | printk(KERN_INFO "md: md driver %d.%d.%d MAX_MD_DEVS=%d," |
| 4270 | " MD_SB_DISKS=%d\n", |
| 4271 | MD_MAJOR_VERSION, MD_MINOR_VERSION, |
| 4272 | MD_PATCHLEVEL_VERSION, MAX_MD_DEVS, MD_SB_DISKS); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4273 | printk(KERN_INFO "md: bitmap version %d.%d\n", BITMAP_MAJOR, |
| 4274 | BITMAP_MINOR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4275 | |
| 4276 | if (register_blkdev(MAJOR_NR, "md")) |
| 4277 | return -1; |
| 4278 | if ((mdp_major=register_blkdev(0, "mdp"))<=0) { |
| 4279 | unregister_blkdev(MAJOR_NR, "md"); |
| 4280 | return -1; |
| 4281 | } |
| 4282 | devfs_mk_dir("md"); |
| 4283 | blk_register_region(MKDEV(MAJOR_NR, 0), MAX_MD_DEVS, THIS_MODULE, |
| 4284 | md_probe, NULL, NULL); |
| 4285 | blk_register_region(MKDEV(mdp_major, 0), MAX_MD_DEVS<<MdpMinorShift, THIS_MODULE, |
| 4286 | md_probe, NULL, NULL); |
| 4287 | |
| 4288 | for (minor=0; minor < MAX_MD_DEVS; ++minor) |
| 4289 | devfs_mk_bdev(MKDEV(MAJOR_NR, minor), |
| 4290 | S_IFBLK|S_IRUSR|S_IWUSR, |
| 4291 | "md/%d", minor); |
| 4292 | |
| 4293 | for (minor=0; minor < MAX_MD_DEVS; ++minor) |
| 4294 | devfs_mk_bdev(MKDEV(mdp_major, minor<<MdpMinorShift), |
| 4295 | S_IFBLK|S_IRUSR|S_IWUSR, |
| 4296 | "md/mdp%d", minor); |
| 4297 | |
| 4298 | |
| 4299 | register_reboot_notifier(&md_notifier); |
| 4300 | raid_table_header = register_sysctl_table(raid_root_table, 1); |
| 4301 | |
| 4302 | md_geninit(); |
| 4303 | return (0); |
| 4304 | } |
| 4305 | |
| 4306 | |
| 4307 | #ifndef MODULE |
| 4308 | |
| 4309 | /* |
| 4310 | * Searches all registered partitions for autorun RAID arrays |
| 4311 | * at boot time. |
| 4312 | */ |
| 4313 | static dev_t detected_devices[128]; |
| 4314 | static int dev_cnt; |
| 4315 | |
| 4316 | void md_autodetect_dev(dev_t dev) |
| 4317 | { |
| 4318 | if (dev_cnt >= 0 && dev_cnt < 127) |
| 4319 | detected_devices[dev_cnt++] = dev; |
| 4320 | } |
| 4321 | |
| 4322 | |
| 4323 | static void autostart_arrays(int part) |
| 4324 | { |
| 4325 | mdk_rdev_t *rdev; |
| 4326 | int i; |
| 4327 | |
| 4328 | printk(KERN_INFO "md: Autodetecting RAID arrays.\n"); |
| 4329 | |
| 4330 | for (i = 0; i < dev_cnt; i++) { |
| 4331 | dev_t dev = detected_devices[i]; |
| 4332 | |
| 4333 | rdev = md_import_device(dev,0, 0); |
| 4334 | if (IS_ERR(rdev)) |
| 4335 | continue; |
| 4336 | |
| 4337 | if (rdev->faulty) { |
| 4338 | MD_BUG(); |
| 4339 | continue; |
| 4340 | } |
| 4341 | list_add(&rdev->same_set, &pending_raid_disks); |
| 4342 | } |
| 4343 | dev_cnt = 0; |
| 4344 | |
| 4345 | autorun_devices(part); |
| 4346 | } |
| 4347 | |
| 4348 | #endif |
| 4349 | |
| 4350 | static __exit void md_exit(void) |
| 4351 | { |
| 4352 | mddev_t *mddev; |
| 4353 | struct list_head *tmp; |
| 4354 | int i; |
| 4355 | blk_unregister_region(MKDEV(MAJOR_NR,0), MAX_MD_DEVS); |
| 4356 | blk_unregister_region(MKDEV(mdp_major,0), MAX_MD_DEVS << MdpMinorShift); |
| 4357 | for (i=0; i < MAX_MD_DEVS; i++) |
| 4358 | devfs_remove("md/%d", i); |
| 4359 | for (i=0; i < MAX_MD_DEVS; i++) |
| 4360 | devfs_remove("md/d%d", i); |
| 4361 | |
| 4362 | devfs_remove("md"); |
| 4363 | |
| 4364 | unregister_blkdev(MAJOR_NR,"md"); |
| 4365 | unregister_blkdev(mdp_major, "mdp"); |
| 4366 | unregister_reboot_notifier(&md_notifier); |
| 4367 | unregister_sysctl_table(raid_table_header); |
| 4368 | remove_proc_entry("mdstat", NULL); |
| 4369 | ITERATE_MDDEV(mddev,tmp) { |
| 4370 | struct gendisk *disk = mddev->gendisk; |
| 4371 | if (!disk) |
| 4372 | continue; |
| 4373 | export_array(mddev); |
| 4374 | del_gendisk(disk); |
| 4375 | put_disk(disk); |
| 4376 | mddev->gendisk = NULL; |
| 4377 | mddev_put(mddev); |
| 4378 | } |
| 4379 | } |
| 4380 | |
| 4381 | module_init(md_init) |
| 4382 | module_exit(md_exit) |
| 4383 | |
| 4384 | EXPORT_SYMBOL(register_md_personality); |
| 4385 | EXPORT_SYMBOL(unregister_md_personality); |
| 4386 | EXPORT_SYMBOL(md_error); |
| 4387 | EXPORT_SYMBOL(md_done_sync); |
| 4388 | EXPORT_SYMBOL(md_write_start); |
| 4389 | EXPORT_SYMBOL(md_write_end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4390 | EXPORT_SYMBOL(md_register_thread); |
| 4391 | EXPORT_SYMBOL(md_unregister_thread); |
| 4392 | EXPORT_SYMBOL(md_wakeup_thread); |
| 4393 | EXPORT_SYMBOL(md_print_devices); |
| 4394 | EXPORT_SYMBOL(md_check_recovery); |
| 4395 | MODULE_LICENSE("GPL"); |
NeilBrown | aa1595e | 2005-08-04 12:53:32 -0700 | [diff] [blame] | 4396 | MODULE_ALIAS("md"); |
NeilBrown | 7200865 | 2005-08-26 18:34:15 -0700 | [diff] [blame] | 4397 | MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR); |