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