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 22 - persistent bitmap code 23 Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc. 24 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/kthread.h> 36 #include <linux/blkdev.h> 37 #include <linux/sysctl.h> 38 #include <linux/seq_file.h> 39 #include <linux/mutex.h> 40 #include <linux/buffer_head.h> /* for invalidate_bdev */ 41 #include <linux/poll.h> 42 #include <linux/ctype.h> 43 #include <linux/string.h> 44 #include <linux/hdreg.h> 45 #include <linux/proc_fs.h> 46 #include <linux/random.h> 47 #include <linux/reboot.h> 48 #include <linux/file.h> 49 #include <linux/compat.h> 50 #include <linux/delay.h> 51 #include <linux/raid/md_p.h> 52 #include <linux/raid/md_u.h> 53 #include <linux/slab.h> 54 #include "md.h" 55 #include "bitmap.h" 56 57 #define DEBUG 0 58 #define dprintk(x...) ((void)(DEBUG && printk(x))) 59 60 #ifndef MODULE 61 static void autostart_arrays(int part); 62 #endif 63 64 static LIST_HEAD(pers_list); 65 static DEFINE_SPINLOCK(pers_lock); 66 67 static void md_print_devices(void); 68 69 static DECLARE_WAIT_QUEUE_HEAD(resync_wait); 70 static struct workqueue_struct *md_wq; 71 static struct workqueue_struct *md_misc_wq; 72 73 #define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); } 74 75 /* 76 * Default number of read corrections we'll attempt on an rdev 77 * before ejecting it from the array. We divide the read error 78 * count by 2 for every hour elapsed between read errors. 79 */ 80 #define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20 81 /* 82 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit' 83 * is 1000 KB/sec, so the extra system load does not show up that much. 84 * Increase it if you want to have more _guaranteed_ speed. Note that 85 * the RAID driver will use the maximum available bandwidth if the IO 86 * subsystem is idle. There is also an 'absolute maximum' reconstruction 87 * speed limit - in case reconstruction slows down your system despite 88 * idle IO detection. 89 * 90 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max. 91 * or /sys/block/mdX/md/sync_speed_{min,max} 92 */ 93 94 static int sysctl_speed_limit_min = 1000; 95 static int sysctl_speed_limit_max = 200000; 96 static inline int speed_min(mddev_t *mddev) 97 { 98 return mddev->sync_speed_min ? 99 mddev->sync_speed_min : sysctl_speed_limit_min; 100 } 101 102 static inline int speed_max(mddev_t *mddev) 103 { 104 return mddev->sync_speed_max ? 105 mddev->sync_speed_max : sysctl_speed_limit_max; 106 } 107 108 static struct ctl_table_header *raid_table_header; 109 110 static ctl_table raid_table[] = { 111 { 112 .procname = "speed_limit_min", 113 .data = &sysctl_speed_limit_min, 114 .maxlen = sizeof(int), 115 .mode = S_IRUGO|S_IWUSR, 116 .proc_handler = proc_dointvec, 117 }, 118 { 119 .procname = "speed_limit_max", 120 .data = &sysctl_speed_limit_max, 121 .maxlen = sizeof(int), 122 .mode = S_IRUGO|S_IWUSR, 123 .proc_handler = proc_dointvec, 124 }, 125 { } 126 }; 127 128 static ctl_table raid_dir_table[] = { 129 { 130 .procname = "raid", 131 .maxlen = 0, 132 .mode = S_IRUGO|S_IXUGO, 133 .child = raid_table, 134 }, 135 { } 136 }; 137 138 static ctl_table raid_root_table[] = { 139 { 140 .procname = "dev", 141 .maxlen = 0, 142 .mode = 0555, 143 .child = raid_dir_table, 144 }, 145 { } 146 }; 147 148 static const struct block_device_operations md_fops; 149 150 static int start_readonly; 151 152 /* bio_clone_mddev 153 * like bio_clone, but with a local bio set 154 */ 155 156 static void mddev_bio_destructor(struct bio *bio) 157 { 158 mddev_t *mddev, **mddevp; 159 160 mddevp = (void*)bio; 161 mddev = mddevp[-1]; 162 163 bio_free(bio, mddev->bio_set); 164 } 165 166 struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs, 167 mddev_t *mddev) 168 { 169 struct bio *b; 170 mddev_t **mddevp; 171 172 if (!mddev || !mddev->bio_set) 173 return bio_alloc(gfp_mask, nr_iovecs); 174 175 b = bio_alloc_bioset(gfp_mask, nr_iovecs, 176 mddev->bio_set); 177 if (!b) 178 return NULL; 179 mddevp = (void*)b; 180 mddevp[-1] = mddev; 181 b->bi_destructor = mddev_bio_destructor; 182 return b; 183 } 184 EXPORT_SYMBOL_GPL(bio_alloc_mddev); 185 186 struct bio *bio_clone_mddev(struct bio *bio, gfp_t gfp_mask, 187 mddev_t *mddev) 188 { 189 struct bio *b; 190 mddev_t **mddevp; 191 192 if (!mddev || !mddev->bio_set) 193 return bio_clone(bio, gfp_mask); 194 195 b = bio_alloc_bioset(gfp_mask, bio->bi_max_vecs, 196 mddev->bio_set); 197 if (!b) 198 return NULL; 199 mddevp = (void*)b; 200 mddevp[-1] = mddev; 201 b->bi_destructor = mddev_bio_destructor; 202 __bio_clone(b, bio); 203 if (bio_integrity(bio)) { 204 int ret; 205 206 ret = bio_integrity_clone(b, bio, gfp_mask, mddev->bio_set); 207 208 if (ret < 0) { 209 bio_put(b); 210 return NULL; 211 } 212 } 213 214 return b; 215 } 216 EXPORT_SYMBOL_GPL(bio_clone_mddev); 217 218 /* 219 * We have a system wide 'event count' that is incremented 220 * on any 'interesting' event, and readers of /proc/mdstat 221 * can use 'poll' or 'select' to find out when the event 222 * count increases. 223 * 224 * Events are: 225 * start array, stop array, error, add device, remove device, 226 * start build, activate spare 227 */ 228 static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters); 229 static atomic_t md_event_count; 230 void md_new_event(mddev_t *mddev) 231 { 232 atomic_inc(&md_event_count); 233 wake_up(&md_event_waiters); 234 } 235 EXPORT_SYMBOL_GPL(md_new_event); 236 237 /* Alternate version that can be called from interrupts 238 * when calling sysfs_notify isn't needed. 239 */ 240 static void md_new_event_inintr(mddev_t *mddev) 241 { 242 atomic_inc(&md_event_count); 243 wake_up(&md_event_waiters); 244 } 245 246 /* 247 * Enables to iterate over all existing md arrays 248 * all_mddevs_lock protects this list. 249 */ 250 static LIST_HEAD(all_mddevs); 251 static DEFINE_SPINLOCK(all_mddevs_lock); 252 253 254 /* 255 * iterates through all used mddevs in the system. 256 * We take care to grab the all_mddevs_lock whenever navigating 257 * the list, and to always hold a refcount when unlocked. 258 * Any code which breaks out of this loop while own 259 * a reference to the current mddev and must mddev_put it. 260 */ 261 #define for_each_mddev(mddev,tmp) \ 262 \ 263 for (({ spin_lock(&all_mddevs_lock); \ 264 tmp = all_mddevs.next; \ 265 mddev = NULL;}); \ 266 ({ if (tmp != &all_mddevs) \ 267 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\ 268 spin_unlock(&all_mddevs_lock); \ 269 if (mddev) mddev_put(mddev); \ 270 mddev = list_entry(tmp, mddev_t, all_mddevs); \ 271 tmp != &all_mddevs;}); \ 272 ({ spin_lock(&all_mddevs_lock); \ 273 tmp = tmp->next;}) \ 274 ) 275 276 277 /* Rather than calling directly into the personality make_request function, 278 * IO requests come here first so that we can check if the device is 279 * being suspended pending a reconfiguration. 280 * We hold a refcount over the call to ->make_request. By the time that 281 * call has finished, the bio has been linked into some internal structure 282 * and so is visible to ->quiesce(), so we don't need the refcount any more. 283 */ 284 static int md_make_request(struct request_queue *q, struct bio *bio) 285 { 286 const int rw = bio_data_dir(bio); 287 mddev_t *mddev = q->queuedata; 288 int rv; 289 int cpu; 290 unsigned int sectors; 291 292 if (mddev == NULL || mddev->pers == NULL 293 || !mddev->ready) { 294 bio_io_error(bio); 295 return 0; 296 } 297 smp_rmb(); /* Ensure implications of 'active' are visible */ 298 rcu_read_lock(); 299 if (mddev->suspended) { 300 DEFINE_WAIT(__wait); 301 for (;;) { 302 prepare_to_wait(&mddev->sb_wait, &__wait, 303 TASK_UNINTERRUPTIBLE); 304 if (!mddev->suspended) 305 break; 306 rcu_read_unlock(); 307 schedule(); 308 rcu_read_lock(); 309 } 310 finish_wait(&mddev->sb_wait, &__wait); 311 } 312 atomic_inc(&mddev->active_io); 313 rcu_read_unlock(); 314 315 /* 316 * save the sectors now since our bio can 317 * go away inside make_request 318 */ 319 sectors = bio_sectors(bio); 320 rv = mddev->pers->make_request(mddev, bio); 321 322 cpu = part_stat_lock(); 323 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]); 324 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw], sectors); 325 part_stat_unlock(); 326 327 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended) 328 wake_up(&mddev->sb_wait); 329 330 return rv; 331 } 332 333 /* mddev_suspend makes sure no new requests are submitted 334 * to the device, and that any requests that have been submitted 335 * are completely handled. 336 * Once ->stop is called and completes, the module will be completely 337 * unused. 338 */ 339 void mddev_suspend(mddev_t *mddev) 340 { 341 BUG_ON(mddev->suspended); 342 mddev->suspended = 1; 343 synchronize_rcu(); 344 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0); 345 mddev->pers->quiesce(mddev, 1); 346 } 347 EXPORT_SYMBOL_GPL(mddev_suspend); 348 349 void mddev_resume(mddev_t *mddev) 350 { 351 mddev->suspended = 0; 352 wake_up(&mddev->sb_wait); 353 mddev->pers->quiesce(mddev, 0); 354 } 355 EXPORT_SYMBOL_GPL(mddev_resume); 356 357 int mddev_congested(mddev_t *mddev, int bits) 358 { 359 return mddev->suspended; 360 } 361 EXPORT_SYMBOL(mddev_congested); 362 363 /* 364 * Generic flush handling for md 365 */ 366 367 static void md_end_flush(struct bio *bio, int err) 368 { 369 mdk_rdev_t *rdev = bio->bi_private; 370 mddev_t *mddev = rdev->mddev; 371 372 rdev_dec_pending(rdev, mddev); 373 374 if (atomic_dec_and_test(&mddev->flush_pending)) { 375 /* The pre-request flush has finished */ 376 queue_work(md_wq, &mddev->flush_work); 377 } 378 bio_put(bio); 379 } 380 381 static void md_submit_flush_data(struct work_struct *ws); 382 383 static void submit_flushes(struct work_struct *ws) 384 { 385 mddev_t *mddev = container_of(ws, mddev_t, flush_work); 386 mdk_rdev_t *rdev; 387 388 INIT_WORK(&mddev->flush_work, md_submit_flush_data); 389 atomic_set(&mddev->flush_pending, 1); 390 rcu_read_lock(); 391 list_for_each_entry_rcu(rdev, &mddev->disks, same_set) 392 if (rdev->raid_disk >= 0 && 393 !test_bit(Faulty, &rdev->flags)) { 394 /* Take two references, one is dropped 395 * when request finishes, one after 396 * we reclaim rcu_read_lock 397 */ 398 struct bio *bi; 399 atomic_inc(&rdev->nr_pending); 400 atomic_inc(&rdev->nr_pending); 401 rcu_read_unlock(); 402 bi = bio_alloc_mddev(GFP_KERNEL, 0, mddev); 403 bi->bi_end_io = md_end_flush; 404 bi->bi_private = rdev; 405 bi->bi_bdev = rdev->bdev; 406 atomic_inc(&mddev->flush_pending); 407 submit_bio(WRITE_FLUSH, bi); 408 rcu_read_lock(); 409 rdev_dec_pending(rdev, mddev); 410 } 411 rcu_read_unlock(); 412 if (atomic_dec_and_test(&mddev->flush_pending)) 413 queue_work(md_wq, &mddev->flush_work); 414 } 415 416 static void md_submit_flush_data(struct work_struct *ws) 417 { 418 mddev_t *mddev = container_of(ws, mddev_t, flush_work); 419 struct bio *bio = mddev->flush_bio; 420 421 if (bio->bi_size == 0) 422 /* an empty barrier - all done */ 423 bio_endio(bio, 0); 424 else { 425 bio->bi_rw &= ~REQ_FLUSH; 426 if (mddev->pers->make_request(mddev, bio)) 427 generic_make_request(bio); 428 } 429 430 mddev->flush_bio = NULL; 431 wake_up(&mddev->sb_wait); 432 } 433 434 void md_flush_request(mddev_t *mddev, struct bio *bio) 435 { 436 spin_lock_irq(&mddev->write_lock); 437 wait_event_lock_irq(mddev->sb_wait, 438 !mddev->flush_bio, 439 mddev->write_lock, /*nothing*/); 440 mddev->flush_bio = bio; 441 spin_unlock_irq(&mddev->write_lock); 442 443 INIT_WORK(&mddev->flush_work, submit_flushes); 444 queue_work(md_wq, &mddev->flush_work); 445 } 446 EXPORT_SYMBOL(md_flush_request); 447 448 /* Support for plugging. 449 * This mirrors the plugging support in request_queue, but does not 450 * require having a whole queue 451 */ 452 static void plugger_work(struct work_struct *work) 453 { 454 struct plug_handle *plug = 455 container_of(work, struct plug_handle, unplug_work); 456 plug->unplug_fn(plug); 457 } 458 static void plugger_timeout(unsigned long data) 459 { 460 struct plug_handle *plug = (void *)data; 461 kblockd_schedule_work(NULL, &plug->unplug_work); 462 } 463 void plugger_init(struct plug_handle *plug, 464 void (*unplug_fn)(struct plug_handle *)) 465 { 466 plug->unplug_flag = 0; 467 plug->unplug_fn = unplug_fn; 468 init_timer(&plug->unplug_timer); 469 plug->unplug_timer.function = plugger_timeout; 470 plug->unplug_timer.data = (unsigned long)plug; 471 INIT_WORK(&plug->unplug_work, plugger_work); 472 } 473 EXPORT_SYMBOL_GPL(plugger_init); 474 475 void plugger_set_plug(struct plug_handle *plug) 476 { 477 if (!test_and_set_bit(PLUGGED_FLAG, &plug->unplug_flag)) 478 mod_timer(&plug->unplug_timer, jiffies + msecs_to_jiffies(3)+1); 479 } 480 EXPORT_SYMBOL_GPL(plugger_set_plug); 481 482 int plugger_remove_plug(struct plug_handle *plug) 483 { 484 if (test_and_clear_bit(PLUGGED_FLAG, &plug->unplug_flag)) { 485 del_timer(&plug->unplug_timer); 486 return 1; 487 } else 488 return 0; 489 } 490 EXPORT_SYMBOL_GPL(plugger_remove_plug); 491 492 493 static inline mddev_t *mddev_get(mddev_t *mddev) 494 { 495 atomic_inc(&mddev->active); 496 return mddev; 497 } 498 499 static void mddev_delayed_delete(struct work_struct *ws); 500 501 static void mddev_put(mddev_t *mddev) 502 { 503 struct bio_set *bs = NULL; 504 505 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock)) 506 return; 507 if (!mddev->raid_disks && list_empty(&mddev->disks) && 508 mddev->ctime == 0 && !mddev->hold_active) { 509 /* Array is not configured at all, and not held active, 510 * so destroy it */ 511 list_del(&mddev->all_mddevs); 512 bs = mddev->bio_set; 513 mddev->bio_set = NULL; 514 if (mddev->gendisk) { 515 /* We did a probe so need to clean up. Call 516 * queue_work inside the spinlock so that 517 * flush_workqueue() after mddev_find will 518 * succeed in waiting for the work to be done. 519 */ 520 INIT_WORK(&mddev->del_work, mddev_delayed_delete); 521 queue_work(md_misc_wq, &mddev->del_work); 522 } else 523 kfree(mddev); 524 } 525 spin_unlock(&all_mddevs_lock); 526 if (bs) 527 bioset_free(bs); 528 } 529 530 void mddev_init(mddev_t *mddev) 531 { 532 mutex_init(&mddev->open_mutex); 533 mutex_init(&mddev->reconfig_mutex); 534 mutex_init(&mddev->bitmap_info.mutex); 535 INIT_LIST_HEAD(&mddev->disks); 536 INIT_LIST_HEAD(&mddev->all_mddevs); 537 init_timer(&mddev->safemode_timer); 538 atomic_set(&mddev->active, 1); 539 atomic_set(&mddev->openers, 0); 540 atomic_set(&mddev->active_io, 0); 541 spin_lock_init(&mddev->write_lock); 542 atomic_set(&mddev->flush_pending, 0); 543 init_waitqueue_head(&mddev->sb_wait); 544 init_waitqueue_head(&mddev->recovery_wait); 545 mddev->reshape_position = MaxSector; 546 mddev->resync_min = 0; 547 mddev->resync_max = MaxSector; 548 mddev->level = LEVEL_NONE; 549 } 550 EXPORT_SYMBOL_GPL(mddev_init); 551 552 static mddev_t * mddev_find(dev_t unit) 553 { 554 mddev_t *mddev, *new = NULL; 555 556 if (unit && MAJOR(unit) != MD_MAJOR) 557 unit &= ~((1<<MdpMinorShift)-1); 558 559 retry: 560 spin_lock(&all_mddevs_lock); 561 562 if (unit) { 563 list_for_each_entry(mddev, &all_mddevs, all_mddevs) 564 if (mddev->unit == unit) { 565 mddev_get(mddev); 566 spin_unlock(&all_mddevs_lock); 567 kfree(new); 568 return mddev; 569 } 570 571 if (new) { 572 list_add(&new->all_mddevs, &all_mddevs); 573 spin_unlock(&all_mddevs_lock); 574 new->hold_active = UNTIL_IOCTL; 575 return new; 576 } 577 } else if (new) { 578 /* find an unused unit number */ 579 static int next_minor = 512; 580 int start = next_minor; 581 int is_free = 0; 582 int dev = 0; 583 while (!is_free) { 584 dev = MKDEV(MD_MAJOR, next_minor); 585 next_minor++; 586 if (next_minor > MINORMASK) 587 next_minor = 0; 588 if (next_minor == start) { 589 /* Oh dear, all in use. */ 590 spin_unlock(&all_mddevs_lock); 591 kfree(new); 592 return NULL; 593 } 594 595 is_free = 1; 596 list_for_each_entry(mddev, &all_mddevs, all_mddevs) 597 if (mddev->unit == dev) { 598 is_free = 0; 599 break; 600 } 601 } 602 new->unit = dev; 603 new->md_minor = MINOR(dev); 604 new->hold_active = UNTIL_STOP; 605 list_add(&new->all_mddevs, &all_mddevs); 606 spin_unlock(&all_mddevs_lock); 607 return new; 608 } 609 spin_unlock(&all_mddevs_lock); 610 611 new = kzalloc(sizeof(*new), GFP_KERNEL); 612 if (!new) 613 return NULL; 614 615 new->unit = unit; 616 if (MAJOR(unit) == MD_MAJOR) 617 new->md_minor = MINOR(unit); 618 else 619 new->md_minor = MINOR(unit) >> MdpMinorShift; 620 621 mddev_init(new); 622 623 goto retry; 624 } 625 626 static inline int mddev_lock(mddev_t * mddev) 627 { 628 return mutex_lock_interruptible(&mddev->reconfig_mutex); 629 } 630 631 static inline int mddev_is_locked(mddev_t *mddev) 632 { 633 return mutex_is_locked(&mddev->reconfig_mutex); 634 } 635 636 static inline int mddev_trylock(mddev_t * mddev) 637 { 638 return mutex_trylock(&mddev->reconfig_mutex); 639 } 640 641 static struct attribute_group md_redundancy_group; 642 643 static void mddev_unlock(mddev_t * mddev) 644 { 645 if (mddev->to_remove) { 646 /* These cannot be removed under reconfig_mutex as 647 * an access to the files will try to take reconfig_mutex 648 * while holding the file unremovable, which leads to 649 * a deadlock. 650 * So hold set sysfs_active while the remove in happeing, 651 * and anything else which might set ->to_remove or my 652 * otherwise change the sysfs namespace will fail with 653 * -EBUSY if sysfs_active is still set. 654 * We set sysfs_active under reconfig_mutex and elsewhere 655 * test it under the same mutex to ensure its correct value 656 * is seen. 657 */ 658 struct attribute_group *to_remove = mddev->to_remove; 659 mddev->to_remove = NULL; 660 mddev->sysfs_active = 1; 661 mutex_unlock(&mddev->reconfig_mutex); 662 663 if (mddev->kobj.sd) { 664 if (to_remove != &md_redundancy_group) 665 sysfs_remove_group(&mddev->kobj, to_remove); 666 if (mddev->pers == NULL || 667 mddev->pers->sync_request == NULL) { 668 sysfs_remove_group(&mddev->kobj, &md_redundancy_group); 669 if (mddev->sysfs_action) 670 sysfs_put(mddev->sysfs_action); 671 mddev->sysfs_action = NULL; 672 } 673 } 674 mddev->sysfs_active = 0; 675 } else 676 mutex_unlock(&mddev->reconfig_mutex); 677 678 md_wakeup_thread(mddev->thread); 679 } 680 681 static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr) 682 { 683 mdk_rdev_t *rdev; 684 685 list_for_each_entry(rdev, &mddev->disks, same_set) 686 if (rdev->desc_nr == nr) 687 return rdev; 688 689 return NULL; 690 } 691 692 static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev) 693 { 694 mdk_rdev_t *rdev; 695 696 list_for_each_entry(rdev, &mddev->disks, same_set) 697 if (rdev->bdev->bd_dev == dev) 698 return rdev; 699 700 return NULL; 701 } 702 703 static struct mdk_personality *find_pers(int level, char *clevel) 704 { 705 struct mdk_personality *pers; 706 list_for_each_entry(pers, &pers_list, list) { 707 if (level != LEVEL_NONE && pers->level == level) 708 return pers; 709 if (strcmp(pers->name, clevel)==0) 710 return pers; 711 } 712 return NULL; 713 } 714 715 /* return the offset of the super block in 512byte sectors */ 716 static inline sector_t calc_dev_sboffset(mdk_rdev_t *rdev) 717 { 718 sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512; 719 return MD_NEW_SIZE_SECTORS(num_sectors); 720 } 721 722 static int alloc_disk_sb(mdk_rdev_t * rdev) 723 { 724 if (rdev->sb_page) 725 MD_BUG(); 726 727 rdev->sb_page = alloc_page(GFP_KERNEL); 728 if (!rdev->sb_page) { 729 printk(KERN_ALERT "md: out of memory.\n"); 730 return -ENOMEM; 731 } 732 733 return 0; 734 } 735 736 static void free_disk_sb(mdk_rdev_t * rdev) 737 { 738 if (rdev->sb_page) { 739 put_page(rdev->sb_page); 740 rdev->sb_loaded = 0; 741 rdev->sb_page = NULL; 742 rdev->sb_start = 0; 743 rdev->sectors = 0; 744 } 745 } 746 747 748 static void super_written(struct bio *bio, int error) 749 { 750 mdk_rdev_t *rdev = bio->bi_private; 751 mddev_t *mddev = rdev->mddev; 752 753 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) { 754 printk("md: super_written gets error=%d, uptodate=%d\n", 755 error, test_bit(BIO_UPTODATE, &bio->bi_flags)); 756 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags)); 757 md_error(mddev, rdev); 758 } 759 760 if (atomic_dec_and_test(&mddev->pending_writes)) 761 wake_up(&mddev->sb_wait); 762 bio_put(bio); 763 } 764 765 void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev, 766 sector_t sector, int size, struct page *page) 767 { 768 /* write first size bytes of page to sector of rdev 769 * Increment mddev->pending_writes before returning 770 * and decrement it on completion, waking up sb_wait 771 * if zero is reached. 772 * If an error occurred, call md_error 773 */ 774 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev); 775 776 bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev; 777 bio->bi_sector = sector; 778 bio_add_page(bio, page, size, 0); 779 bio->bi_private = rdev; 780 bio->bi_end_io = super_written; 781 782 atomic_inc(&mddev->pending_writes); 783 submit_bio(REQ_WRITE | REQ_SYNC | REQ_UNPLUG | REQ_FLUSH | REQ_FUA, 784 bio); 785 } 786 787 void md_super_wait(mddev_t *mddev) 788 { 789 /* wait for all superblock writes that were scheduled to complete */ 790 DEFINE_WAIT(wq); 791 for(;;) { 792 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE); 793 if (atomic_read(&mddev->pending_writes)==0) 794 break; 795 schedule(); 796 } 797 finish_wait(&mddev->sb_wait, &wq); 798 } 799 800 static void bi_complete(struct bio *bio, int error) 801 { 802 complete((struct completion*)bio->bi_private); 803 } 804 805 int sync_page_io(mdk_rdev_t *rdev, sector_t sector, int size, 806 struct page *page, int rw, bool metadata_op) 807 { 808 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev); 809 struct completion event; 810 int ret; 811 812 rw |= REQ_SYNC | REQ_UNPLUG; 813 814 bio->bi_bdev = (metadata_op && rdev->meta_bdev) ? 815 rdev->meta_bdev : rdev->bdev; 816 if (metadata_op) 817 bio->bi_sector = sector + rdev->sb_start; 818 else 819 bio->bi_sector = sector + rdev->data_offset; 820 bio_add_page(bio, page, size, 0); 821 init_completion(&event); 822 bio->bi_private = &event; 823 bio->bi_end_io = bi_complete; 824 submit_bio(rw, bio); 825 wait_for_completion(&event); 826 827 ret = test_bit(BIO_UPTODATE, &bio->bi_flags); 828 bio_put(bio); 829 return ret; 830 } 831 EXPORT_SYMBOL_GPL(sync_page_io); 832 833 static int read_disk_sb(mdk_rdev_t * rdev, int size) 834 { 835 char b[BDEVNAME_SIZE]; 836 if (!rdev->sb_page) { 837 MD_BUG(); 838 return -EINVAL; 839 } 840 if (rdev->sb_loaded) 841 return 0; 842 843 844 if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true)) 845 goto fail; 846 rdev->sb_loaded = 1; 847 return 0; 848 849 fail: 850 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n", 851 bdevname(rdev->bdev,b)); 852 return -EINVAL; 853 } 854 855 static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2) 856 { 857 return sb1->set_uuid0 == sb2->set_uuid0 && 858 sb1->set_uuid1 == sb2->set_uuid1 && 859 sb1->set_uuid2 == sb2->set_uuid2 && 860 sb1->set_uuid3 == sb2->set_uuid3; 861 } 862 863 static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2) 864 { 865 int ret; 866 mdp_super_t *tmp1, *tmp2; 867 868 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL); 869 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL); 870 871 if (!tmp1 || !tmp2) { 872 ret = 0; 873 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n"); 874 goto abort; 875 } 876 877 *tmp1 = *sb1; 878 *tmp2 = *sb2; 879 880 /* 881 * nr_disks is not constant 882 */ 883 tmp1->nr_disks = 0; 884 tmp2->nr_disks = 0; 885 886 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0); 887 abort: 888 kfree(tmp1); 889 kfree(tmp2); 890 return ret; 891 } 892 893 894 static u32 md_csum_fold(u32 csum) 895 { 896 csum = (csum & 0xffff) + (csum >> 16); 897 return (csum & 0xffff) + (csum >> 16); 898 } 899 900 static unsigned int calc_sb_csum(mdp_super_t * sb) 901 { 902 u64 newcsum = 0; 903 u32 *sb32 = (u32*)sb; 904 int i; 905 unsigned int disk_csum, csum; 906 907 disk_csum = sb->sb_csum; 908 sb->sb_csum = 0; 909 910 for (i = 0; i < MD_SB_BYTES/4 ; i++) 911 newcsum += sb32[i]; 912 csum = (newcsum & 0xffffffff) + (newcsum>>32); 913 914 915 #ifdef CONFIG_ALPHA 916 /* This used to use csum_partial, which was wrong for several 917 * reasons including that different results are returned on 918 * different architectures. It isn't critical that we get exactly 919 * the same return value as before (we always csum_fold before 920 * testing, and that removes any differences). However as we 921 * know that csum_partial always returned a 16bit value on 922 * alphas, do a fold to maximise conformity to previous behaviour. 923 */ 924 sb->sb_csum = md_csum_fold(disk_csum); 925 #else 926 sb->sb_csum = disk_csum; 927 #endif 928 return csum; 929 } 930 931 932 /* 933 * Handle superblock details. 934 * We want to be able to handle multiple superblock formats 935 * so we have a common interface to them all, and an array of 936 * different handlers. 937 * We rely on user-space to write the initial superblock, and support 938 * reading and updating of superblocks. 939 * Interface methods are: 940 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version) 941 * loads and validates a superblock on dev. 942 * if refdev != NULL, compare superblocks on both devices 943 * Return: 944 * 0 - dev has a superblock that is compatible with refdev 945 * 1 - dev has a superblock that is compatible and newer than refdev 946 * so dev should be used as the refdev in future 947 * -EINVAL superblock incompatible or invalid 948 * -othererror e.g. -EIO 949 * 950 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev) 951 * Verify that dev is acceptable into mddev. 952 * The first time, mddev->raid_disks will be 0, and data from 953 * dev should be merged in. Subsequent calls check that dev 954 * is new enough. Return 0 or -EINVAL 955 * 956 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev) 957 * Update the superblock for rdev with data in mddev 958 * This does not write to disc. 959 * 960 */ 961 962 struct super_type { 963 char *name; 964 struct module *owner; 965 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev, 966 int minor_version); 967 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev); 968 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev); 969 unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev, 970 sector_t num_sectors); 971 }; 972 973 /* 974 * Check that the given mddev has no bitmap. 975 * 976 * This function is called from the run method of all personalities that do not 977 * support bitmaps. It prints an error message and returns non-zero if mddev 978 * has a bitmap. Otherwise, it returns 0. 979 * 980 */ 981 int md_check_no_bitmap(mddev_t *mddev) 982 { 983 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset) 984 return 0; 985 printk(KERN_ERR "%s: bitmaps are not supported for %s\n", 986 mdname(mddev), mddev->pers->name); 987 return 1; 988 } 989 EXPORT_SYMBOL(md_check_no_bitmap); 990 991 /* 992 * load_super for 0.90.0 993 */ 994 static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version) 995 { 996 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; 997 mdp_super_t *sb; 998 int ret; 999 1000 /* 1001 * Calculate the position of the superblock (512byte sectors), 1002 * it's at the end of the disk. 1003 * 1004 * It also happens to be a multiple of 4Kb. 1005 */ 1006 rdev->sb_start = calc_dev_sboffset(rdev); 1007 1008 ret = read_disk_sb(rdev, MD_SB_BYTES); 1009 if (ret) return ret; 1010 1011 ret = -EINVAL; 1012 1013 bdevname(rdev->bdev, b); 1014 sb = (mdp_super_t*)page_address(rdev->sb_page); 1015 1016 if (sb->md_magic != MD_SB_MAGIC) { 1017 printk(KERN_ERR "md: invalid raid superblock magic on %s\n", 1018 b); 1019 goto abort; 1020 } 1021 1022 if (sb->major_version != 0 || 1023 sb->minor_version < 90 || 1024 sb->minor_version > 91) { 1025 printk(KERN_WARNING "Bad version number %d.%d on %s\n", 1026 sb->major_version, sb->minor_version, 1027 b); 1028 goto abort; 1029 } 1030 1031 if (sb->raid_disks <= 0) 1032 goto abort; 1033 1034 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) { 1035 printk(KERN_WARNING "md: invalid superblock checksum on %s\n", 1036 b); 1037 goto abort; 1038 } 1039 1040 rdev->preferred_minor = sb->md_minor; 1041 rdev->data_offset = 0; 1042 rdev->sb_size = MD_SB_BYTES; 1043 1044 if (sb->level == LEVEL_MULTIPATH) 1045 rdev->desc_nr = -1; 1046 else 1047 rdev->desc_nr = sb->this_disk.number; 1048 1049 if (!refdev) { 1050 ret = 1; 1051 } else { 1052 __u64 ev1, ev2; 1053 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page); 1054 if (!uuid_equal(refsb, sb)) { 1055 printk(KERN_WARNING "md: %s has different UUID to %s\n", 1056 b, bdevname(refdev->bdev,b2)); 1057 goto abort; 1058 } 1059 if (!sb_equal(refsb, sb)) { 1060 printk(KERN_WARNING "md: %s has same UUID" 1061 " but different superblock to %s\n", 1062 b, bdevname(refdev->bdev, b2)); 1063 goto abort; 1064 } 1065 ev1 = md_event(sb); 1066 ev2 = md_event(refsb); 1067 if (ev1 > ev2) 1068 ret = 1; 1069 else 1070 ret = 0; 1071 } 1072 rdev->sectors = rdev->sb_start; 1073 1074 if (rdev->sectors < sb->size * 2 && sb->level > 1) 1075 /* "this cannot possibly happen" ... */ 1076 ret = -EINVAL; 1077 1078 abort: 1079 return ret; 1080 } 1081 1082 /* 1083 * validate_super for 0.90.0 1084 */ 1085 static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev) 1086 { 1087 mdp_disk_t *desc; 1088 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page); 1089 __u64 ev1 = md_event(sb); 1090 1091 rdev->raid_disk = -1; 1092 clear_bit(Faulty, &rdev->flags); 1093 clear_bit(In_sync, &rdev->flags); 1094 clear_bit(WriteMostly, &rdev->flags); 1095 1096 if (mddev->raid_disks == 0) { 1097 mddev->major_version = 0; 1098 mddev->minor_version = sb->minor_version; 1099 mddev->patch_version = sb->patch_version; 1100 mddev->external = 0; 1101 mddev->chunk_sectors = sb->chunk_size >> 9; 1102 mddev->ctime = sb->ctime; 1103 mddev->utime = sb->utime; 1104 mddev->level = sb->level; 1105 mddev->clevel[0] = 0; 1106 mddev->layout = sb->layout; 1107 mddev->raid_disks = sb->raid_disks; 1108 mddev->dev_sectors = sb->size * 2; 1109 mddev->events = ev1; 1110 mddev->bitmap_info.offset = 0; 1111 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; 1112 1113 if (mddev->minor_version >= 91) { 1114 mddev->reshape_position = sb->reshape_position; 1115 mddev->delta_disks = sb->delta_disks; 1116 mddev->new_level = sb->new_level; 1117 mddev->new_layout = sb->new_layout; 1118 mddev->new_chunk_sectors = sb->new_chunk >> 9; 1119 } else { 1120 mddev->reshape_position = MaxSector; 1121 mddev->delta_disks = 0; 1122 mddev->new_level = mddev->level; 1123 mddev->new_layout = mddev->layout; 1124 mddev->new_chunk_sectors = mddev->chunk_sectors; 1125 } 1126 1127 if (sb->state & (1<<MD_SB_CLEAN)) 1128 mddev->recovery_cp = MaxSector; 1129 else { 1130 if (sb->events_hi == sb->cp_events_hi && 1131 sb->events_lo == sb->cp_events_lo) { 1132 mddev->recovery_cp = sb->recovery_cp; 1133 } else 1134 mddev->recovery_cp = 0; 1135 } 1136 1137 memcpy(mddev->uuid+0, &sb->set_uuid0, 4); 1138 memcpy(mddev->uuid+4, &sb->set_uuid1, 4); 1139 memcpy(mddev->uuid+8, &sb->set_uuid2, 4); 1140 memcpy(mddev->uuid+12,&sb->set_uuid3, 4); 1141 1142 mddev->max_disks = MD_SB_DISKS; 1143 1144 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) && 1145 mddev->bitmap_info.file == NULL) 1146 mddev->bitmap_info.offset = 1147 mddev->bitmap_info.default_offset; 1148 1149 } else if (mddev->pers == NULL) { 1150 /* Insist on good event counter while assembling, except 1151 * for spares (which don't need an event count) */ 1152 ++ev1; 1153 if (sb->disks[rdev->desc_nr].state & ( 1154 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE))) 1155 if (ev1 < mddev->events) 1156 return -EINVAL; 1157 } else if (mddev->bitmap) { 1158 /* if adding to array with a bitmap, then we can accept an 1159 * older device ... but not too old. 1160 */ 1161 if (ev1 < mddev->bitmap->events_cleared) 1162 return 0; 1163 } else { 1164 if (ev1 < mddev->events) 1165 /* just a hot-add of a new device, leave raid_disk at -1 */ 1166 return 0; 1167 } 1168 1169 if (mddev->level != LEVEL_MULTIPATH) { 1170 desc = sb->disks + rdev->desc_nr; 1171 1172 if (desc->state & (1<<MD_DISK_FAULTY)) 1173 set_bit(Faulty, &rdev->flags); 1174 else if (desc->state & (1<<MD_DISK_SYNC) /* && 1175 desc->raid_disk < mddev->raid_disks */) { 1176 set_bit(In_sync, &rdev->flags); 1177 rdev->raid_disk = desc->raid_disk; 1178 } else if (desc->state & (1<<MD_DISK_ACTIVE)) { 1179 /* active but not in sync implies recovery up to 1180 * reshape position. We don't know exactly where 1181 * that is, so set to zero for now */ 1182 if (mddev->minor_version >= 91) { 1183 rdev->recovery_offset = 0; 1184 rdev->raid_disk = desc->raid_disk; 1185 } 1186 } 1187 if (desc->state & (1<<MD_DISK_WRITEMOSTLY)) 1188 set_bit(WriteMostly, &rdev->flags); 1189 } else /* MULTIPATH are always insync */ 1190 set_bit(In_sync, &rdev->flags); 1191 return 0; 1192 } 1193 1194 /* 1195 * sync_super for 0.90.0 1196 */ 1197 static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev) 1198 { 1199 mdp_super_t *sb; 1200 mdk_rdev_t *rdev2; 1201 int next_spare = mddev->raid_disks; 1202 1203 1204 /* make rdev->sb match mddev data.. 1205 * 1206 * 1/ zero out disks 1207 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare); 1208 * 3/ any empty disks < next_spare become removed 1209 * 1210 * disks[0] gets initialised to REMOVED because 1211 * we cannot be sure from other fields if it has 1212 * been initialised or not. 1213 */ 1214 int i; 1215 int active=0, working=0,failed=0,spare=0,nr_disks=0; 1216 1217 rdev->sb_size = MD_SB_BYTES; 1218 1219 sb = (mdp_super_t*)page_address(rdev->sb_page); 1220 1221 memset(sb, 0, sizeof(*sb)); 1222 1223 sb->md_magic = MD_SB_MAGIC; 1224 sb->major_version = mddev->major_version; 1225 sb->patch_version = mddev->patch_version; 1226 sb->gvalid_words = 0; /* ignored */ 1227 memcpy(&sb->set_uuid0, mddev->uuid+0, 4); 1228 memcpy(&sb->set_uuid1, mddev->uuid+4, 4); 1229 memcpy(&sb->set_uuid2, mddev->uuid+8, 4); 1230 memcpy(&sb->set_uuid3, mddev->uuid+12,4); 1231 1232 sb->ctime = mddev->ctime; 1233 sb->level = mddev->level; 1234 sb->size = mddev->dev_sectors / 2; 1235 sb->raid_disks = mddev->raid_disks; 1236 sb->md_minor = mddev->md_minor; 1237 sb->not_persistent = 0; 1238 sb->utime = mddev->utime; 1239 sb->state = 0; 1240 sb->events_hi = (mddev->events>>32); 1241 sb->events_lo = (u32)mddev->events; 1242 1243 if (mddev->reshape_position == MaxSector) 1244 sb->minor_version = 90; 1245 else { 1246 sb->minor_version = 91; 1247 sb->reshape_position = mddev->reshape_position; 1248 sb->new_level = mddev->new_level; 1249 sb->delta_disks = mddev->delta_disks; 1250 sb->new_layout = mddev->new_layout; 1251 sb->new_chunk = mddev->new_chunk_sectors << 9; 1252 } 1253 mddev->minor_version = sb->minor_version; 1254 if (mddev->in_sync) 1255 { 1256 sb->recovery_cp = mddev->recovery_cp; 1257 sb->cp_events_hi = (mddev->events>>32); 1258 sb->cp_events_lo = (u32)mddev->events; 1259 if (mddev->recovery_cp == MaxSector) 1260 sb->state = (1<< MD_SB_CLEAN); 1261 } else 1262 sb->recovery_cp = 0; 1263 1264 sb->layout = mddev->layout; 1265 sb->chunk_size = mddev->chunk_sectors << 9; 1266 1267 if (mddev->bitmap && mddev->bitmap_info.file == NULL) 1268 sb->state |= (1<<MD_SB_BITMAP_PRESENT); 1269 1270 sb->disks[0].state = (1<<MD_DISK_REMOVED); 1271 list_for_each_entry(rdev2, &mddev->disks, same_set) { 1272 mdp_disk_t *d; 1273 int desc_nr; 1274 int is_active = test_bit(In_sync, &rdev2->flags); 1275 1276 if (rdev2->raid_disk >= 0 && 1277 sb->minor_version >= 91) 1278 /* we have nowhere to store the recovery_offset, 1279 * but if it is not below the reshape_position, 1280 * we can piggy-back on that. 1281 */ 1282 is_active = 1; 1283 if (rdev2->raid_disk < 0 || 1284 test_bit(Faulty, &rdev2->flags)) 1285 is_active = 0; 1286 if (is_active) 1287 desc_nr = rdev2->raid_disk; 1288 else 1289 desc_nr = next_spare++; 1290 rdev2->desc_nr = desc_nr; 1291 d = &sb->disks[rdev2->desc_nr]; 1292 nr_disks++; 1293 d->number = rdev2->desc_nr; 1294 d->major = MAJOR(rdev2->bdev->bd_dev); 1295 d->minor = MINOR(rdev2->bdev->bd_dev); 1296 if (is_active) 1297 d->raid_disk = rdev2->raid_disk; 1298 else 1299 d->raid_disk = rdev2->desc_nr; /* compatibility */ 1300 if (test_bit(Faulty, &rdev2->flags)) 1301 d->state = (1<<MD_DISK_FAULTY); 1302 else if (is_active) { 1303 d->state = (1<<MD_DISK_ACTIVE); 1304 if (test_bit(In_sync, &rdev2->flags)) 1305 d->state |= (1<<MD_DISK_SYNC); 1306 active++; 1307 working++; 1308 } else { 1309 d->state = 0; 1310 spare++; 1311 working++; 1312 } 1313 if (test_bit(WriteMostly, &rdev2->flags)) 1314 d->state |= (1<<MD_DISK_WRITEMOSTLY); 1315 } 1316 /* now set the "removed" and "faulty" bits on any missing devices */ 1317 for (i=0 ; i < mddev->raid_disks ; i++) { 1318 mdp_disk_t *d = &sb->disks[i]; 1319 if (d->state == 0 && d->number == 0) { 1320 d->number = i; 1321 d->raid_disk = i; 1322 d->state = (1<<MD_DISK_REMOVED); 1323 d->state |= (1<<MD_DISK_FAULTY); 1324 failed++; 1325 } 1326 } 1327 sb->nr_disks = nr_disks; 1328 sb->active_disks = active; 1329 sb->working_disks = working; 1330 sb->failed_disks = failed; 1331 sb->spare_disks = spare; 1332 1333 sb->this_disk = sb->disks[rdev->desc_nr]; 1334 sb->sb_csum = calc_sb_csum(sb); 1335 } 1336 1337 /* 1338 * rdev_size_change for 0.90.0 1339 */ 1340 static unsigned long long 1341 super_90_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors) 1342 { 1343 if (num_sectors && num_sectors < rdev->mddev->dev_sectors) 1344 return 0; /* component must fit device */ 1345 if (rdev->mddev->bitmap_info.offset) 1346 return 0; /* can't move bitmap */ 1347 rdev->sb_start = calc_dev_sboffset(rdev); 1348 if (!num_sectors || num_sectors > rdev->sb_start) 1349 num_sectors = rdev->sb_start; 1350 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, 1351 rdev->sb_page); 1352 md_super_wait(rdev->mddev); 1353 return num_sectors; 1354 } 1355 1356 1357 /* 1358 * version 1 superblock 1359 */ 1360 1361 static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb) 1362 { 1363 __le32 disk_csum; 1364 u32 csum; 1365 unsigned long long newcsum; 1366 int size = 256 + le32_to_cpu(sb->max_dev)*2; 1367 __le32 *isuper = (__le32*)sb; 1368 int i; 1369 1370 disk_csum = sb->sb_csum; 1371 sb->sb_csum = 0; 1372 newcsum = 0; 1373 for (i=0; size>=4; size -= 4 ) 1374 newcsum += le32_to_cpu(*isuper++); 1375 1376 if (size == 2) 1377 newcsum += le16_to_cpu(*(__le16*) isuper); 1378 1379 csum = (newcsum & 0xffffffff) + (newcsum >> 32); 1380 sb->sb_csum = disk_csum; 1381 return cpu_to_le32(csum); 1382 } 1383 1384 static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version) 1385 { 1386 struct mdp_superblock_1 *sb; 1387 int ret; 1388 sector_t sb_start; 1389 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; 1390 int bmask; 1391 1392 /* 1393 * Calculate the position of the superblock in 512byte sectors. 1394 * It is always aligned to a 4K boundary and 1395 * depeding on minor_version, it can be: 1396 * 0: At least 8K, but less than 12K, from end of device 1397 * 1: At start of device 1398 * 2: 4K from start of device. 1399 */ 1400 switch(minor_version) { 1401 case 0: 1402 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9; 1403 sb_start -= 8*2; 1404 sb_start &= ~(sector_t)(4*2-1); 1405 break; 1406 case 1: 1407 sb_start = 0; 1408 break; 1409 case 2: 1410 sb_start = 8; 1411 break; 1412 default: 1413 return -EINVAL; 1414 } 1415 rdev->sb_start = sb_start; 1416 1417 /* superblock is rarely larger than 1K, but it can be larger, 1418 * and it is safe to read 4k, so we do that 1419 */ 1420 ret = read_disk_sb(rdev, 4096); 1421 if (ret) return ret; 1422 1423 1424 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page); 1425 1426 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) || 1427 sb->major_version != cpu_to_le32(1) || 1428 le32_to_cpu(sb->max_dev) > (4096-256)/2 || 1429 le64_to_cpu(sb->super_offset) != rdev->sb_start || 1430 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0) 1431 return -EINVAL; 1432 1433 if (calc_sb_1_csum(sb) != sb->sb_csum) { 1434 printk("md: invalid superblock checksum on %s\n", 1435 bdevname(rdev->bdev,b)); 1436 return -EINVAL; 1437 } 1438 if (le64_to_cpu(sb->data_size) < 10) { 1439 printk("md: data_size too small on %s\n", 1440 bdevname(rdev->bdev,b)); 1441 return -EINVAL; 1442 } 1443 1444 rdev->preferred_minor = 0xffff; 1445 rdev->data_offset = le64_to_cpu(sb->data_offset); 1446 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read)); 1447 1448 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256; 1449 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; 1450 if (rdev->sb_size & bmask) 1451 rdev->sb_size = (rdev->sb_size | bmask) + 1; 1452 1453 if (minor_version 1454 && rdev->data_offset < sb_start + (rdev->sb_size/512)) 1455 return -EINVAL; 1456 1457 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH)) 1458 rdev->desc_nr = -1; 1459 else 1460 rdev->desc_nr = le32_to_cpu(sb->dev_number); 1461 1462 if (!refdev) { 1463 ret = 1; 1464 } else { 1465 __u64 ev1, ev2; 1466 struct mdp_superblock_1 *refsb = 1467 (struct mdp_superblock_1*)page_address(refdev->sb_page); 1468 1469 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 || 1470 sb->level != refsb->level || 1471 sb->layout != refsb->layout || 1472 sb->chunksize != refsb->chunksize) { 1473 printk(KERN_WARNING "md: %s has strangely different" 1474 " superblock to %s\n", 1475 bdevname(rdev->bdev,b), 1476 bdevname(refdev->bdev,b2)); 1477 return -EINVAL; 1478 } 1479 ev1 = le64_to_cpu(sb->events); 1480 ev2 = le64_to_cpu(refsb->events); 1481 1482 if (ev1 > ev2) 1483 ret = 1; 1484 else 1485 ret = 0; 1486 } 1487 if (minor_version) 1488 rdev->sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) - 1489 le64_to_cpu(sb->data_offset); 1490 else 1491 rdev->sectors = rdev->sb_start; 1492 if (rdev->sectors < le64_to_cpu(sb->data_size)) 1493 return -EINVAL; 1494 rdev->sectors = le64_to_cpu(sb->data_size); 1495 if (le64_to_cpu(sb->size) > rdev->sectors) 1496 return -EINVAL; 1497 return ret; 1498 } 1499 1500 static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev) 1501 { 1502 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page); 1503 __u64 ev1 = le64_to_cpu(sb->events); 1504 1505 rdev->raid_disk = -1; 1506 clear_bit(Faulty, &rdev->flags); 1507 clear_bit(In_sync, &rdev->flags); 1508 clear_bit(WriteMostly, &rdev->flags); 1509 1510 if (mddev->raid_disks == 0) { 1511 mddev->major_version = 1; 1512 mddev->patch_version = 0; 1513 mddev->external = 0; 1514 mddev->chunk_sectors = le32_to_cpu(sb->chunksize); 1515 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1); 1516 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1); 1517 mddev->level = le32_to_cpu(sb->level); 1518 mddev->clevel[0] = 0; 1519 mddev->layout = le32_to_cpu(sb->layout); 1520 mddev->raid_disks = le32_to_cpu(sb->raid_disks); 1521 mddev->dev_sectors = le64_to_cpu(sb->size); 1522 mddev->events = ev1; 1523 mddev->bitmap_info.offset = 0; 1524 mddev->bitmap_info.default_offset = 1024 >> 9; 1525 1526 mddev->recovery_cp = le64_to_cpu(sb->resync_offset); 1527 memcpy(mddev->uuid, sb->set_uuid, 16); 1528 1529 mddev->max_disks = (4096-256)/2; 1530 1531 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) && 1532 mddev->bitmap_info.file == NULL ) 1533 mddev->bitmap_info.offset = 1534 (__s32)le32_to_cpu(sb->bitmap_offset); 1535 1536 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) { 1537 mddev->reshape_position = le64_to_cpu(sb->reshape_position); 1538 mddev->delta_disks = le32_to_cpu(sb->delta_disks); 1539 mddev->new_level = le32_to_cpu(sb->new_level); 1540 mddev->new_layout = le32_to_cpu(sb->new_layout); 1541 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk); 1542 } else { 1543 mddev->reshape_position = MaxSector; 1544 mddev->delta_disks = 0; 1545 mddev->new_level = mddev->level; 1546 mddev->new_layout = mddev->layout; 1547 mddev->new_chunk_sectors = mddev->chunk_sectors; 1548 } 1549 1550 } else if (mddev->pers == NULL) { 1551 /* Insist of good event counter while assembling, except for 1552 * spares (which don't need an event count) */ 1553 ++ev1; 1554 if (rdev->desc_nr >= 0 && 1555 rdev->desc_nr < le32_to_cpu(sb->max_dev) && 1556 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe) 1557 if (ev1 < mddev->events) 1558 return -EINVAL; 1559 } else if (mddev->bitmap) { 1560 /* If adding to array with a bitmap, then we can accept an 1561 * older device, but not too old. 1562 */ 1563 if (ev1 < mddev->bitmap->events_cleared) 1564 return 0; 1565 } else { 1566 if (ev1 < mddev->events) 1567 /* just a hot-add of a new device, leave raid_disk at -1 */ 1568 return 0; 1569 } 1570 if (mddev->level != LEVEL_MULTIPATH) { 1571 int role; 1572 if (rdev->desc_nr < 0 || 1573 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) { 1574 role = 0xffff; 1575 rdev->desc_nr = -1; 1576 } else 1577 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]); 1578 switch(role) { 1579 case 0xffff: /* spare */ 1580 break; 1581 case 0xfffe: /* faulty */ 1582 set_bit(Faulty, &rdev->flags); 1583 break; 1584 default: 1585 if ((le32_to_cpu(sb->feature_map) & 1586 MD_FEATURE_RECOVERY_OFFSET)) 1587 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset); 1588 else 1589 set_bit(In_sync, &rdev->flags); 1590 rdev->raid_disk = role; 1591 break; 1592 } 1593 if (sb->devflags & WriteMostly1) 1594 set_bit(WriteMostly, &rdev->flags); 1595 } else /* MULTIPATH are always insync */ 1596 set_bit(In_sync, &rdev->flags); 1597 1598 return 0; 1599 } 1600 1601 static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev) 1602 { 1603 struct mdp_superblock_1 *sb; 1604 mdk_rdev_t *rdev2; 1605 int max_dev, i; 1606 /* make rdev->sb match mddev and rdev data. */ 1607 1608 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page); 1609 1610 sb->feature_map = 0; 1611 sb->pad0 = 0; 1612 sb->recovery_offset = cpu_to_le64(0); 1613 memset(sb->pad1, 0, sizeof(sb->pad1)); 1614 memset(sb->pad2, 0, sizeof(sb->pad2)); 1615 memset(sb->pad3, 0, sizeof(sb->pad3)); 1616 1617 sb->utime = cpu_to_le64((__u64)mddev->utime); 1618 sb->events = cpu_to_le64(mddev->events); 1619 if (mddev->in_sync) 1620 sb->resync_offset = cpu_to_le64(mddev->recovery_cp); 1621 else 1622 sb->resync_offset = cpu_to_le64(0); 1623 1624 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors)); 1625 1626 sb->raid_disks = cpu_to_le32(mddev->raid_disks); 1627 sb->size = cpu_to_le64(mddev->dev_sectors); 1628 sb->chunksize = cpu_to_le32(mddev->chunk_sectors); 1629 sb->level = cpu_to_le32(mddev->level); 1630 sb->layout = cpu_to_le32(mddev->layout); 1631 1632 if (mddev->bitmap && mddev->bitmap_info.file == NULL) { 1633 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset); 1634 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET); 1635 } 1636 1637 if (rdev->raid_disk >= 0 && 1638 !test_bit(In_sync, &rdev->flags)) { 1639 sb->feature_map |= 1640 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET); 1641 sb->recovery_offset = 1642 cpu_to_le64(rdev->recovery_offset); 1643 } 1644 1645 if (mddev->reshape_position != MaxSector) { 1646 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE); 1647 sb->reshape_position = cpu_to_le64(mddev->reshape_position); 1648 sb->new_layout = cpu_to_le32(mddev->new_layout); 1649 sb->delta_disks = cpu_to_le32(mddev->delta_disks); 1650 sb->new_level = cpu_to_le32(mddev->new_level); 1651 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors); 1652 } 1653 1654 max_dev = 0; 1655 list_for_each_entry(rdev2, &mddev->disks, same_set) 1656 if (rdev2->desc_nr+1 > max_dev) 1657 max_dev = rdev2->desc_nr+1; 1658 1659 if (max_dev > le32_to_cpu(sb->max_dev)) { 1660 int bmask; 1661 sb->max_dev = cpu_to_le32(max_dev); 1662 rdev->sb_size = max_dev * 2 + 256; 1663 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; 1664 if (rdev->sb_size & bmask) 1665 rdev->sb_size = (rdev->sb_size | bmask) + 1; 1666 } else 1667 max_dev = le32_to_cpu(sb->max_dev); 1668 1669 for (i=0; i<max_dev;i++) 1670 sb->dev_roles[i] = cpu_to_le16(0xfffe); 1671 1672 list_for_each_entry(rdev2, &mddev->disks, same_set) { 1673 i = rdev2->desc_nr; 1674 if (test_bit(Faulty, &rdev2->flags)) 1675 sb->dev_roles[i] = cpu_to_le16(0xfffe); 1676 else if (test_bit(In_sync, &rdev2->flags)) 1677 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); 1678 else if (rdev2->raid_disk >= 0) 1679 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); 1680 else 1681 sb->dev_roles[i] = cpu_to_le16(0xffff); 1682 } 1683 1684 sb->sb_csum = calc_sb_1_csum(sb); 1685 } 1686 1687 static unsigned long long 1688 super_1_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors) 1689 { 1690 struct mdp_superblock_1 *sb; 1691 sector_t max_sectors; 1692 if (num_sectors && num_sectors < rdev->mddev->dev_sectors) 1693 return 0; /* component must fit device */ 1694 if (rdev->sb_start < rdev->data_offset) { 1695 /* minor versions 1 and 2; superblock before data */ 1696 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9; 1697 max_sectors -= rdev->data_offset; 1698 if (!num_sectors || num_sectors > max_sectors) 1699 num_sectors = max_sectors; 1700 } else if (rdev->mddev->bitmap_info.offset) { 1701 /* minor version 0 with bitmap we can't move */ 1702 return 0; 1703 } else { 1704 /* minor version 0; superblock after data */ 1705 sector_t sb_start; 1706 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2; 1707 sb_start &= ~(sector_t)(4*2 - 1); 1708 max_sectors = rdev->sectors + sb_start - rdev->sb_start; 1709 if (!num_sectors || num_sectors > max_sectors) 1710 num_sectors = max_sectors; 1711 rdev->sb_start = sb_start; 1712 } 1713 sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page); 1714 sb->data_size = cpu_to_le64(num_sectors); 1715 sb->super_offset = rdev->sb_start; 1716 sb->sb_csum = calc_sb_1_csum(sb); 1717 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, 1718 rdev->sb_page); 1719 md_super_wait(rdev->mddev); 1720 return num_sectors; 1721 } 1722 1723 static struct super_type super_types[] = { 1724 [0] = { 1725 .name = "0.90.0", 1726 .owner = THIS_MODULE, 1727 .load_super = super_90_load, 1728 .validate_super = super_90_validate, 1729 .sync_super = super_90_sync, 1730 .rdev_size_change = super_90_rdev_size_change, 1731 }, 1732 [1] = { 1733 .name = "md-1", 1734 .owner = THIS_MODULE, 1735 .load_super = super_1_load, 1736 .validate_super = super_1_validate, 1737 .sync_super = super_1_sync, 1738 .rdev_size_change = super_1_rdev_size_change, 1739 }, 1740 }; 1741 1742 static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2) 1743 { 1744 mdk_rdev_t *rdev, *rdev2; 1745 1746 rcu_read_lock(); 1747 rdev_for_each_rcu(rdev, mddev1) 1748 rdev_for_each_rcu(rdev2, mddev2) 1749 if (rdev->bdev->bd_contains == 1750 rdev2->bdev->bd_contains) { 1751 rcu_read_unlock(); 1752 return 1; 1753 } 1754 rcu_read_unlock(); 1755 return 0; 1756 } 1757 1758 static LIST_HEAD(pending_raid_disks); 1759 1760 /* 1761 * Try to register data integrity profile for an mddev 1762 * 1763 * This is called when an array is started and after a disk has been kicked 1764 * from the array. It only succeeds if all working and active component devices 1765 * are integrity capable with matching profiles. 1766 */ 1767 int md_integrity_register(mddev_t *mddev) 1768 { 1769 mdk_rdev_t *rdev, *reference = NULL; 1770 1771 if (list_empty(&mddev->disks)) 1772 return 0; /* nothing to do */ 1773 if (blk_get_integrity(mddev->gendisk)) 1774 return 0; /* already registered */ 1775 list_for_each_entry(rdev, &mddev->disks, same_set) { 1776 /* skip spares and non-functional disks */ 1777 if (test_bit(Faulty, &rdev->flags)) 1778 continue; 1779 if (rdev->raid_disk < 0) 1780 continue; 1781 /* 1782 * If at least one rdev is not integrity capable, we can not 1783 * enable data integrity for the md device. 1784 */ 1785 if (!bdev_get_integrity(rdev->bdev)) 1786 return -EINVAL; 1787 if (!reference) { 1788 /* Use the first rdev as the reference */ 1789 reference = rdev; 1790 continue; 1791 } 1792 /* does this rdev's profile match the reference profile? */ 1793 if (blk_integrity_compare(reference->bdev->bd_disk, 1794 rdev->bdev->bd_disk) < 0) 1795 return -EINVAL; 1796 } 1797 /* 1798 * All component devices are integrity capable and have matching 1799 * profiles, register the common profile for the md device. 1800 */ 1801 if (blk_integrity_register(mddev->gendisk, 1802 bdev_get_integrity(reference->bdev)) != 0) { 1803 printk(KERN_ERR "md: failed to register integrity for %s\n", 1804 mdname(mddev)); 1805 return -EINVAL; 1806 } 1807 printk(KERN_NOTICE "md: data integrity on %s enabled\n", 1808 mdname(mddev)); 1809 return 0; 1810 } 1811 EXPORT_SYMBOL(md_integrity_register); 1812 1813 /* Disable data integrity if non-capable/non-matching disk is being added */ 1814 void md_integrity_add_rdev(mdk_rdev_t *rdev, mddev_t *mddev) 1815 { 1816 struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev); 1817 struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk); 1818 1819 if (!bi_mddev) /* nothing to do */ 1820 return; 1821 if (rdev->raid_disk < 0) /* skip spares */ 1822 return; 1823 if (bi_rdev && blk_integrity_compare(mddev->gendisk, 1824 rdev->bdev->bd_disk) >= 0) 1825 return; 1826 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev)); 1827 blk_integrity_unregister(mddev->gendisk); 1828 } 1829 EXPORT_SYMBOL(md_integrity_add_rdev); 1830 1831 static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev) 1832 { 1833 char b[BDEVNAME_SIZE]; 1834 struct kobject *ko; 1835 char *s; 1836 int err; 1837 1838 if (rdev->mddev) { 1839 MD_BUG(); 1840 return -EINVAL; 1841 } 1842 1843 /* prevent duplicates */ 1844 if (find_rdev(mddev, rdev->bdev->bd_dev)) 1845 return -EEXIST; 1846 1847 /* make sure rdev->sectors exceeds mddev->dev_sectors */ 1848 if (rdev->sectors && (mddev->dev_sectors == 0 || 1849 rdev->sectors < mddev->dev_sectors)) { 1850 if (mddev->pers) { 1851 /* Cannot change size, so fail 1852 * If mddev->level <= 0, then we don't care 1853 * about aligning sizes (e.g. linear) 1854 */ 1855 if (mddev->level > 0) 1856 return -ENOSPC; 1857 } else 1858 mddev->dev_sectors = rdev->sectors; 1859 } 1860 1861 /* Verify rdev->desc_nr is unique. 1862 * If it is -1, assign a free number, else 1863 * check number is not in use 1864 */ 1865 if (rdev->desc_nr < 0) { 1866 int choice = 0; 1867 if (mddev->pers) choice = mddev->raid_disks; 1868 while (find_rdev_nr(mddev, choice)) 1869 choice++; 1870 rdev->desc_nr = choice; 1871 } else { 1872 if (find_rdev_nr(mddev, rdev->desc_nr)) 1873 return -EBUSY; 1874 } 1875 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) { 1876 printk(KERN_WARNING "md: %s: array is limited to %d devices\n", 1877 mdname(mddev), mddev->max_disks); 1878 return -EBUSY; 1879 } 1880 bdevname(rdev->bdev,b); 1881 while ( (s=strchr(b, '/')) != NULL) 1882 *s = '!'; 1883 1884 rdev->mddev = mddev; 1885 printk(KERN_INFO "md: bind<%s>\n", b); 1886 1887 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b))) 1888 goto fail; 1889 1890 ko = &part_to_dev(rdev->bdev->bd_part)->kobj; 1891 if (sysfs_create_link(&rdev->kobj, ko, "block")) 1892 /* failure here is OK */; 1893 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state"); 1894 1895 list_add_rcu(&rdev->same_set, &mddev->disks); 1896 bd_link_disk_holder(rdev->bdev, mddev->gendisk); 1897 1898 /* May as well allow recovery to be retried once */ 1899 mddev->recovery_disabled = 0; 1900 1901 return 0; 1902 1903 fail: 1904 printk(KERN_WARNING "md: failed to register dev-%s for %s\n", 1905 b, mdname(mddev)); 1906 return err; 1907 } 1908 1909 static void md_delayed_delete(struct work_struct *ws) 1910 { 1911 mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work); 1912 kobject_del(&rdev->kobj); 1913 kobject_put(&rdev->kobj); 1914 } 1915 1916 static void unbind_rdev_from_array(mdk_rdev_t * rdev) 1917 { 1918 char b[BDEVNAME_SIZE]; 1919 if (!rdev->mddev) { 1920 MD_BUG(); 1921 return; 1922 } 1923 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk); 1924 list_del_rcu(&rdev->same_set); 1925 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b)); 1926 rdev->mddev = NULL; 1927 sysfs_remove_link(&rdev->kobj, "block"); 1928 sysfs_put(rdev->sysfs_state); 1929 rdev->sysfs_state = NULL; 1930 /* We need to delay this, otherwise we can deadlock when 1931 * writing to 'remove' to "dev/state". We also need 1932 * to delay it due to rcu usage. 1933 */ 1934 synchronize_rcu(); 1935 INIT_WORK(&rdev->del_work, md_delayed_delete); 1936 kobject_get(&rdev->kobj); 1937 queue_work(md_misc_wq, &rdev->del_work); 1938 } 1939 1940 /* 1941 * prevent the device from being mounted, repartitioned or 1942 * otherwise reused by a RAID array (or any other kernel 1943 * subsystem), by bd_claiming the device. 1944 */ 1945 static int lock_rdev(mdk_rdev_t *rdev, dev_t dev, int shared) 1946 { 1947 int err = 0; 1948 struct block_device *bdev; 1949 char b[BDEVNAME_SIZE]; 1950 1951 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL, 1952 shared ? (mdk_rdev_t *)lock_rdev : rdev); 1953 if (IS_ERR(bdev)) { 1954 printk(KERN_ERR "md: could not open %s.\n", 1955 __bdevname(dev, b)); 1956 return PTR_ERR(bdev); 1957 } 1958 rdev->bdev = bdev; 1959 return err; 1960 } 1961 1962 static void unlock_rdev(mdk_rdev_t *rdev) 1963 { 1964 struct block_device *bdev = rdev->bdev; 1965 rdev->bdev = NULL; 1966 if (!bdev) 1967 MD_BUG(); 1968 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL); 1969 } 1970 1971 void md_autodetect_dev(dev_t dev); 1972 1973 static void export_rdev(mdk_rdev_t * rdev) 1974 { 1975 char b[BDEVNAME_SIZE]; 1976 printk(KERN_INFO "md: export_rdev(%s)\n", 1977 bdevname(rdev->bdev,b)); 1978 if (rdev->mddev) 1979 MD_BUG(); 1980 free_disk_sb(rdev); 1981 #ifndef MODULE 1982 if (test_bit(AutoDetected, &rdev->flags)) 1983 md_autodetect_dev(rdev->bdev->bd_dev); 1984 #endif 1985 unlock_rdev(rdev); 1986 kobject_put(&rdev->kobj); 1987 } 1988 1989 static void kick_rdev_from_array(mdk_rdev_t * rdev) 1990 { 1991 unbind_rdev_from_array(rdev); 1992 export_rdev(rdev); 1993 } 1994 1995 static void export_array(mddev_t *mddev) 1996 { 1997 mdk_rdev_t *rdev, *tmp; 1998 1999 rdev_for_each(rdev, tmp, mddev) { 2000 if (!rdev->mddev) { 2001 MD_BUG(); 2002 continue; 2003 } 2004 kick_rdev_from_array(rdev); 2005 } 2006 if (!list_empty(&mddev->disks)) 2007 MD_BUG(); 2008 mddev->raid_disks = 0; 2009 mddev->major_version = 0; 2010 } 2011 2012 static void print_desc(mdp_disk_t *desc) 2013 { 2014 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number, 2015 desc->major,desc->minor,desc->raid_disk,desc->state); 2016 } 2017 2018 static void print_sb_90(mdp_super_t *sb) 2019 { 2020 int i; 2021 2022 printk(KERN_INFO 2023 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n", 2024 sb->major_version, sb->minor_version, sb->patch_version, 2025 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3, 2026 sb->ctime); 2027 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n", 2028 sb->level, sb->size, sb->nr_disks, sb->raid_disks, 2029 sb->md_minor, sb->layout, sb->chunk_size); 2030 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d" 2031 " FD:%d SD:%d CSUM:%08x E:%08lx\n", 2032 sb->utime, sb->state, sb->active_disks, sb->working_disks, 2033 sb->failed_disks, sb->spare_disks, 2034 sb->sb_csum, (unsigned long)sb->events_lo); 2035 2036 printk(KERN_INFO); 2037 for (i = 0; i < MD_SB_DISKS; i++) { 2038 mdp_disk_t *desc; 2039 2040 desc = sb->disks + i; 2041 if (desc->number || desc->major || desc->minor || 2042 desc->raid_disk || (desc->state && (desc->state != 4))) { 2043 printk(" D %2d: ", i); 2044 print_desc(desc); 2045 } 2046 } 2047 printk(KERN_INFO "md: THIS: "); 2048 print_desc(&sb->this_disk); 2049 } 2050 2051 static void print_sb_1(struct mdp_superblock_1 *sb) 2052 { 2053 __u8 *uuid; 2054 2055 uuid = sb->set_uuid; 2056 printk(KERN_INFO 2057 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n" 2058 "md: Name: \"%s\" CT:%llu\n", 2059 le32_to_cpu(sb->major_version), 2060 le32_to_cpu(sb->feature_map), 2061 uuid, 2062 sb->set_name, 2063 (unsigned long long)le64_to_cpu(sb->ctime) 2064 & MD_SUPERBLOCK_1_TIME_SEC_MASK); 2065 2066 uuid = sb->device_uuid; 2067 printk(KERN_INFO 2068 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu" 2069 " RO:%llu\n" 2070 "md: Dev:%08x UUID: %pU\n" 2071 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n" 2072 "md: (MaxDev:%u) \n", 2073 le32_to_cpu(sb->level), 2074 (unsigned long long)le64_to_cpu(sb->size), 2075 le32_to_cpu(sb->raid_disks), 2076 le32_to_cpu(sb->layout), 2077 le32_to_cpu(sb->chunksize), 2078 (unsigned long long)le64_to_cpu(sb->data_offset), 2079 (unsigned long long)le64_to_cpu(sb->data_size), 2080 (unsigned long long)le64_to_cpu(sb->super_offset), 2081 (unsigned long long)le64_to_cpu(sb->recovery_offset), 2082 le32_to_cpu(sb->dev_number), 2083 uuid, 2084 sb->devflags, 2085 (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK, 2086 (unsigned long long)le64_to_cpu(sb->events), 2087 (unsigned long long)le64_to_cpu(sb->resync_offset), 2088 le32_to_cpu(sb->sb_csum), 2089 le32_to_cpu(sb->max_dev) 2090 ); 2091 } 2092 2093 static void print_rdev(mdk_rdev_t *rdev, int major_version) 2094 { 2095 char b[BDEVNAME_SIZE]; 2096 printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n", 2097 bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors, 2098 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags), 2099 rdev->desc_nr); 2100 if (rdev->sb_loaded) { 2101 printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version); 2102 switch (major_version) { 2103 case 0: 2104 print_sb_90((mdp_super_t*)page_address(rdev->sb_page)); 2105 break; 2106 case 1: 2107 print_sb_1((struct mdp_superblock_1 *)page_address(rdev->sb_page)); 2108 break; 2109 } 2110 } else 2111 printk(KERN_INFO "md: no rdev superblock!\n"); 2112 } 2113 2114 static void md_print_devices(void) 2115 { 2116 struct list_head *tmp; 2117 mdk_rdev_t *rdev; 2118 mddev_t *mddev; 2119 char b[BDEVNAME_SIZE]; 2120 2121 printk("\n"); 2122 printk("md: **********************************\n"); 2123 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n"); 2124 printk("md: **********************************\n"); 2125 for_each_mddev(mddev, tmp) { 2126 2127 if (mddev->bitmap) 2128 bitmap_print_sb(mddev->bitmap); 2129 else 2130 printk("%s: ", mdname(mddev)); 2131 list_for_each_entry(rdev, &mddev->disks, same_set) 2132 printk("<%s>", bdevname(rdev->bdev,b)); 2133 printk("\n"); 2134 2135 list_for_each_entry(rdev, &mddev->disks, same_set) 2136 print_rdev(rdev, mddev->major_version); 2137 } 2138 printk("md: **********************************\n"); 2139 printk("\n"); 2140 } 2141 2142 2143 static void sync_sbs(mddev_t * mddev, int nospares) 2144 { 2145 /* Update each superblock (in-memory image), but 2146 * if we are allowed to, skip spares which already 2147 * have the right event counter, or have one earlier 2148 * (which would mean they aren't being marked as dirty 2149 * with the rest of the array) 2150 */ 2151 mdk_rdev_t *rdev; 2152 list_for_each_entry(rdev, &mddev->disks, same_set) { 2153 if (rdev->sb_events == mddev->events || 2154 (nospares && 2155 rdev->raid_disk < 0 && 2156 rdev->sb_events+1 == mddev->events)) { 2157 /* Don't update this superblock */ 2158 rdev->sb_loaded = 2; 2159 } else { 2160 super_types[mddev->major_version]. 2161 sync_super(mddev, rdev); 2162 rdev->sb_loaded = 1; 2163 } 2164 } 2165 } 2166 2167 static void md_update_sb(mddev_t * mddev, int force_change) 2168 { 2169 mdk_rdev_t *rdev; 2170 int sync_req; 2171 int nospares = 0; 2172 2173 repeat: 2174 /* First make sure individual recovery_offsets are correct */ 2175 list_for_each_entry(rdev, &mddev->disks, same_set) { 2176 if (rdev->raid_disk >= 0 && 2177 mddev->delta_disks >= 0 && 2178 !test_bit(In_sync, &rdev->flags) && 2179 mddev->curr_resync_completed > rdev->recovery_offset) 2180 rdev->recovery_offset = mddev->curr_resync_completed; 2181 2182 } 2183 if (!mddev->persistent) { 2184 clear_bit(MD_CHANGE_CLEAN, &mddev->flags); 2185 clear_bit(MD_CHANGE_DEVS, &mddev->flags); 2186 if (!mddev->external) 2187 clear_bit(MD_CHANGE_PENDING, &mddev->flags); 2188 wake_up(&mddev->sb_wait); 2189 return; 2190 } 2191 2192 spin_lock_irq(&mddev->write_lock); 2193 2194 mddev->utime = get_seconds(); 2195 2196 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags)) 2197 force_change = 1; 2198 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags)) 2199 /* just a clean<-> dirty transition, possibly leave spares alone, 2200 * though if events isn't the right even/odd, we will have to do 2201 * spares after all 2202 */ 2203 nospares = 1; 2204 if (force_change) 2205 nospares = 0; 2206 if (mddev->degraded) 2207 /* If the array is degraded, then skipping spares is both 2208 * dangerous and fairly pointless. 2209 * Dangerous because a device that was removed from the array 2210 * might have a event_count that still looks up-to-date, 2211 * so it can be re-added without a resync. 2212 * Pointless because if there are any spares to skip, 2213 * then a recovery will happen and soon that array won't 2214 * be degraded any more and the spare can go back to sleep then. 2215 */ 2216 nospares = 0; 2217 2218 sync_req = mddev->in_sync; 2219 2220 /* If this is just a dirty<->clean transition, and the array is clean 2221 * and 'events' is odd, we can roll back to the previous clean state */ 2222 if (nospares 2223 && (mddev->in_sync && mddev->recovery_cp == MaxSector) 2224 && mddev->can_decrease_events 2225 && mddev->events != 1) { 2226 mddev->events--; 2227 mddev->can_decrease_events = 0; 2228 } else { 2229 /* otherwise we have to go forward and ... */ 2230 mddev->events ++; 2231 mddev->can_decrease_events = nospares; 2232 } 2233 2234 if (!mddev->events) { 2235 /* 2236 * oops, this 64-bit counter should never wrap. 2237 * Either we are in around ~1 trillion A.C., assuming 2238 * 1 reboot per second, or we have a bug: 2239 */ 2240 MD_BUG(); 2241 mddev->events --; 2242 } 2243 sync_sbs(mddev, nospares); 2244 spin_unlock_irq(&mddev->write_lock); 2245 2246 dprintk(KERN_INFO 2247 "md: updating %s RAID superblock on device (in sync %d)\n", 2248 mdname(mddev),mddev->in_sync); 2249 2250 bitmap_update_sb(mddev->bitmap); 2251 list_for_each_entry(rdev, &mddev->disks, same_set) { 2252 char b[BDEVNAME_SIZE]; 2253 dprintk(KERN_INFO "md: "); 2254 if (rdev->sb_loaded != 1) 2255 continue; /* no noise on spare devices */ 2256 if (test_bit(Faulty, &rdev->flags)) 2257 dprintk("(skipping faulty "); 2258 2259 dprintk("%s ", bdevname(rdev->bdev,b)); 2260 if (!test_bit(Faulty, &rdev->flags)) { 2261 md_super_write(mddev,rdev, 2262 rdev->sb_start, rdev->sb_size, 2263 rdev->sb_page); 2264 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n", 2265 bdevname(rdev->bdev,b), 2266 (unsigned long long)rdev->sb_start); 2267 rdev->sb_events = mddev->events; 2268 2269 } else 2270 dprintk(")\n"); 2271 if (mddev->level == LEVEL_MULTIPATH) 2272 /* only need to write one superblock... */ 2273 break; 2274 } 2275 md_super_wait(mddev); 2276 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */ 2277 2278 spin_lock_irq(&mddev->write_lock); 2279 if (mddev->in_sync != sync_req || 2280 test_bit(MD_CHANGE_DEVS, &mddev->flags)) { 2281 /* have to write it out again */ 2282 spin_unlock_irq(&mddev->write_lock); 2283 goto repeat; 2284 } 2285 clear_bit(MD_CHANGE_PENDING, &mddev->flags); 2286 spin_unlock_irq(&mddev->write_lock); 2287 wake_up(&mddev->sb_wait); 2288 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) 2289 sysfs_notify(&mddev->kobj, NULL, "sync_completed"); 2290 2291 } 2292 2293 /* words written to sysfs files may, or may not, be \n terminated. 2294 * We want to accept with case. For this we use cmd_match. 2295 */ 2296 static int cmd_match(const char *cmd, const char *str) 2297 { 2298 /* See if cmd, written into a sysfs file, matches 2299 * str. They must either be the same, or cmd can 2300 * have a trailing newline 2301 */ 2302 while (*cmd && *str && *cmd == *str) { 2303 cmd++; 2304 str++; 2305 } 2306 if (*cmd == '\n') 2307 cmd++; 2308 if (*str || *cmd) 2309 return 0; 2310 return 1; 2311 } 2312 2313 struct rdev_sysfs_entry { 2314 struct attribute attr; 2315 ssize_t (*show)(mdk_rdev_t *, char *); 2316 ssize_t (*store)(mdk_rdev_t *, const char *, size_t); 2317 }; 2318 2319 static ssize_t 2320 state_show(mdk_rdev_t *rdev, char *page) 2321 { 2322 char *sep = ""; 2323 size_t len = 0; 2324 2325 if (test_bit(Faulty, &rdev->flags)) { 2326 len+= sprintf(page+len, "%sfaulty",sep); 2327 sep = ","; 2328 } 2329 if (test_bit(In_sync, &rdev->flags)) { 2330 len += sprintf(page+len, "%sin_sync",sep); 2331 sep = ","; 2332 } 2333 if (test_bit(WriteMostly, &rdev->flags)) { 2334 len += sprintf(page+len, "%swrite_mostly",sep); 2335 sep = ","; 2336 } 2337 if (test_bit(Blocked, &rdev->flags)) { 2338 len += sprintf(page+len, "%sblocked", sep); 2339 sep = ","; 2340 } 2341 if (!test_bit(Faulty, &rdev->flags) && 2342 !test_bit(In_sync, &rdev->flags)) { 2343 len += sprintf(page+len, "%sspare", sep); 2344 sep = ","; 2345 } 2346 return len+sprintf(page+len, "\n"); 2347 } 2348 2349 static ssize_t 2350 state_store(mdk_rdev_t *rdev, const char *buf, size_t len) 2351 { 2352 /* can write 2353 * faulty - simulates and error 2354 * remove - disconnects the device 2355 * writemostly - sets write_mostly 2356 * -writemostly - clears write_mostly 2357 * blocked - sets the Blocked flag 2358 * -blocked - clears the Blocked flag 2359 * insync - sets Insync providing device isn't active 2360 */ 2361 int err = -EINVAL; 2362 if (cmd_match(buf, "faulty") && rdev->mddev->pers) { 2363 md_error(rdev->mddev, rdev); 2364 err = 0; 2365 } else if (cmd_match(buf, "remove")) { 2366 if (rdev->raid_disk >= 0) 2367 err = -EBUSY; 2368 else { 2369 mddev_t *mddev = rdev->mddev; 2370 kick_rdev_from_array(rdev); 2371 if (mddev->pers) 2372 md_update_sb(mddev, 1); 2373 md_new_event(mddev); 2374 err = 0; 2375 } 2376 } else if (cmd_match(buf, "writemostly")) { 2377 set_bit(WriteMostly, &rdev->flags); 2378 err = 0; 2379 } else if (cmd_match(buf, "-writemostly")) { 2380 clear_bit(WriteMostly, &rdev->flags); 2381 err = 0; 2382 } else if (cmd_match(buf, "blocked")) { 2383 set_bit(Blocked, &rdev->flags); 2384 err = 0; 2385 } else if (cmd_match(buf, "-blocked")) { 2386 clear_bit(Blocked, &rdev->flags); 2387 wake_up(&rdev->blocked_wait); 2388 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); 2389 md_wakeup_thread(rdev->mddev->thread); 2390 2391 err = 0; 2392 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) { 2393 set_bit(In_sync, &rdev->flags); 2394 err = 0; 2395 } 2396 if (!err) 2397 sysfs_notify_dirent_safe(rdev->sysfs_state); 2398 return err ? err : len; 2399 } 2400 static struct rdev_sysfs_entry rdev_state = 2401 __ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store); 2402 2403 static ssize_t 2404 errors_show(mdk_rdev_t *rdev, char *page) 2405 { 2406 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors)); 2407 } 2408 2409 static ssize_t 2410 errors_store(mdk_rdev_t *rdev, const char *buf, size_t len) 2411 { 2412 char *e; 2413 unsigned long n = simple_strtoul(buf, &e, 10); 2414 if (*buf && (*e == 0 || *e == '\n')) { 2415 atomic_set(&rdev->corrected_errors, n); 2416 return len; 2417 } 2418 return -EINVAL; 2419 } 2420 static struct rdev_sysfs_entry rdev_errors = 2421 __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store); 2422 2423 static ssize_t 2424 slot_show(mdk_rdev_t *rdev, char *page) 2425 { 2426 if (rdev->raid_disk < 0) 2427 return sprintf(page, "none\n"); 2428 else 2429 return sprintf(page, "%d\n", rdev->raid_disk); 2430 } 2431 2432 static ssize_t 2433 slot_store(mdk_rdev_t *rdev, const char *buf, size_t len) 2434 { 2435 char *e; 2436 int err; 2437 char nm[20]; 2438 int slot = simple_strtoul(buf, &e, 10); 2439 if (strncmp(buf, "none", 4)==0) 2440 slot = -1; 2441 else if (e==buf || (*e && *e!= '\n')) 2442 return -EINVAL; 2443 if (rdev->mddev->pers && slot == -1) { 2444 /* Setting 'slot' on an active array requires also 2445 * updating the 'rd%d' link, and communicating 2446 * with the personality with ->hot_*_disk. 2447 * For now we only support removing 2448 * failed/spare devices. This normally happens automatically, 2449 * but not when the metadata is externally managed. 2450 */ 2451 if (rdev->raid_disk == -1) 2452 return -EEXIST; 2453 /* personality does all needed checks */ 2454 if (rdev->mddev->pers->hot_add_disk == NULL) 2455 return -EINVAL; 2456 err = rdev->mddev->pers-> 2457 hot_remove_disk(rdev->mddev, rdev->raid_disk); 2458 if (err) 2459 return err; 2460 sprintf(nm, "rd%d", rdev->raid_disk); 2461 sysfs_remove_link(&rdev->mddev->kobj, nm); 2462 rdev->raid_disk = -1; 2463 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); 2464 md_wakeup_thread(rdev->mddev->thread); 2465 } else if (rdev->mddev->pers) { 2466 mdk_rdev_t *rdev2; 2467 /* Activating a spare .. or possibly reactivating 2468 * if we ever get bitmaps working here. 2469 */ 2470 2471 if (rdev->raid_disk != -1) 2472 return -EBUSY; 2473 2474 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery)) 2475 return -EBUSY; 2476 2477 if (rdev->mddev->pers->hot_add_disk == NULL) 2478 return -EINVAL; 2479 2480 list_for_each_entry(rdev2, &rdev->mddev->disks, same_set) 2481 if (rdev2->raid_disk == slot) 2482 return -EEXIST; 2483 2484 if (slot >= rdev->mddev->raid_disks && 2485 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks) 2486 return -ENOSPC; 2487 2488 rdev->raid_disk = slot; 2489 if (test_bit(In_sync, &rdev->flags)) 2490 rdev->saved_raid_disk = slot; 2491 else 2492 rdev->saved_raid_disk = -1; 2493 err = rdev->mddev->pers-> 2494 hot_add_disk(rdev->mddev, rdev); 2495 if (err) { 2496 rdev->raid_disk = -1; 2497 return err; 2498 } else 2499 sysfs_notify_dirent_safe(rdev->sysfs_state); 2500 sprintf(nm, "rd%d", rdev->raid_disk); 2501 if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm)) 2502 /* failure here is OK */; 2503 /* don't wakeup anyone, leave that to userspace. */ 2504 } else { 2505 if (slot >= rdev->mddev->raid_disks && 2506 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks) 2507 return -ENOSPC; 2508 rdev->raid_disk = slot; 2509 /* assume it is working */ 2510 clear_bit(Faulty, &rdev->flags); 2511 clear_bit(WriteMostly, &rdev->flags); 2512 set_bit(In_sync, &rdev->flags); 2513 sysfs_notify_dirent_safe(rdev->sysfs_state); 2514 } 2515 return len; 2516 } 2517 2518 2519 static struct rdev_sysfs_entry rdev_slot = 2520 __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store); 2521 2522 static ssize_t 2523 offset_show(mdk_rdev_t *rdev, char *page) 2524 { 2525 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset); 2526 } 2527 2528 static ssize_t 2529 offset_store(mdk_rdev_t *rdev, const char *buf, size_t len) 2530 { 2531 char *e; 2532 unsigned long long offset = simple_strtoull(buf, &e, 10); 2533 if (e==buf || (*e && *e != '\n')) 2534 return -EINVAL; 2535 if (rdev->mddev->pers && rdev->raid_disk >= 0) 2536 return -EBUSY; 2537 if (rdev->sectors && rdev->mddev->external) 2538 /* Must set offset before size, so overlap checks 2539 * can be sane */ 2540 return -EBUSY; 2541 rdev->data_offset = offset; 2542 return len; 2543 } 2544 2545 static struct rdev_sysfs_entry rdev_offset = 2546 __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store); 2547 2548 static ssize_t 2549 rdev_size_show(mdk_rdev_t *rdev, char *page) 2550 { 2551 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2); 2552 } 2553 2554 static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2) 2555 { 2556 /* check if two start/length pairs overlap */ 2557 if (s1+l1 <= s2) 2558 return 0; 2559 if (s2+l2 <= s1) 2560 return 0; 2561 return 1; 2562 } 2563 2564 static int strict_blocks_to_sectors(const char *buf, sector_t *sectors) 2565 { 2566 unsigned long long blocks; 2567 sector_t new; 2568 2569 if (strict_strtoull(buf, 10, &blocks) < 0) 2570 return -EINVAL; 2571 2572 if (blocks & 1ULL << (8 * sizeof(blocks) - 1)) 2573 return -EINVAL; /* sector conversion overflow */ 2574 2575 new = blocks * 2; 2576 if (new != blocks * 2) 2577 return -EINVAL; /* unsigned long long to sector_t overflow */ 2578 2579 *sectors = new; 2580 return 0; 2581 } 2582 2583 static ssize_t 2584 rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len) 2585 { 2586 mddev_t *my_mddev = rdev->mddev; 2587 sector_t oldsectors = rdev->sectors; 2588 sector_t sectors; 2589 2590 if (strict_blocks_to_sectors(buf, §ors) < 0) 2591 return -EINVAL; 2592 if (my_mddev->pers && rdev->raid_disk >= 0) { 2593 if (my_mddev->persistent) { 2594 sectors = super_types[my_mddev->major_version]. 2595 rdev_size_change(rdev, sectors); 2596 if (!sectors) 2597 return -EBUSY; 2598 } else if (!sectors) 2599 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) - 2600 rdev->data_offset; 2601 } 2602 if (sectors < my_mddev->dev_sectors) 2603 return -EINVAL; /* component must fit device */ 2604 2605 rdev->sectors = sectors; 2606 if (sectors > oldsectors && my_mddev->external) { 2607 /* need to check that all other rdevs with the same ->bdev 2608 * do not overlap. We need to unlock the mddev to avoid 2609 * a deadlock. We have already changed rdev->sectors, and if 2610 * we have to change it back, we will have the lock again. 2611 */ 2612 mddev_t *mddev; 2613 int overlap = 0; 2614 struct list_head *tmp; 2615 2616 mddev_unlock(my_mddev); 2617 for_each_mddev(mddev, tmp) { 2618 mdk_rdev_t *rdev2; 2619 2620 mddev_lock(mddev); 2621 list_for_each_entry(rdev2, &mddev->disks, same_set) 2622 if (rdev->bdev == rdev2->bdev && 2623 rdev != rdev2 && 2624 overlaps(rdev->data_offset, rdev->sectors, 2625 rdev2->data_offset, 2626 rdev2->sectors)) { 2627 overlap = 1; 2628 break; 2629 } 2630 mddev_unlock(mddev); 2631 if (overlap) { 2632 mddev_put(mddev); 2633 break; 2634 } 2635 } 2636 mddev_lock(my_mddev); 2637 if (overlap) { 2638 /* Someone else could have slipped in a size 2639 * change here, but doing so is just silly. 2640 * We put oldsectors back because we *know* it is 2641 * safe, and trust userspace not to race with 2642 * itself 2643 */ 2644 rdev->sectors = oldsectors; 2645 return -EBUSY; 2646 } 2647 } 2648 return len; 2649 } 2650 2651 static struct rdev_sysfs_entry rdev_size = 2652 __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store); 2653 2654 2655 static ssize_t recovery_start_show(mdk_rdev_t *rdev, char *page) 2656 { 2657 unsigned long long recovery_start = rdev->recovery_offset; 2658 2659 if (test_bit(In_sync, &rdev->flags) || 2660 recovery_start == MaxSector) 2661 return sprintf(page, "none\n"); 2662 2663 return sprintf(page, "%llu\n", recovery_start); 2664 } 2665 2666 static ssize_t recovery_start_store(mdk_rdev_t *rdev, const char *buf, size_t len) 2667 { 2668 unsigned long long recovery_start; 2669 2670 if (cmd_match(buf, "none")) 2671 recovery_start = MaxSector; 2672 else if (strict_strtoull(buf, 10, &recovery_start)) 2673 return -EINVAL; 2674 2675 if (rdev->mddev->pers && 2676 rdev->raid_disk >= 0) 2677 return -EBUSY; 2678 2679 rdev->recovery_offset = recovery_start; 2680 if (recovery_start == MaxSector) 2681 set_bit(In_sync, &rdev->flags); 2682 else 2683 clear_bit(In_sync, &rdev->flags); 2684 return len; 2685 } 2686 2687 static struct rdev_sysfs_entry rdev_recovery_start = 2688 __ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store); 2689 2690 static struct attribute *rdev_default_attrs[] = { 2691 &rdev_state.attr, 2692 &rdev_errors.attr, 2693 &rdev_slot.attr, 2694 &rdev_offset.attr, 2695 &rdev_size.attr, 2696 &rdev_recovery_start.attr, 2697 NULL, 2698 }; 2699 static ssize_t 2700 rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page) 2701 { 2702 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); 2703 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj); 2704 mddev_t *mddev = rdev->mddev; 2705 ssize_t rv; 2706 2707 if (!entry->show) 2708 return -EIO; 2709 2710 rv = mddev ? mddev_lock(mddev) : -EBUSY; 2711 if (!rv) { 2712 if (rdev->mddev == NULL) 2713 rv = -EBUSY; 2714 else 2715 rv = entry->show(rdev, page); 2716 mddev_unlock(mddev); 2717 } 2718 return rv; 2719 } 2720 2721 static ssize_t 2722 rdev_attr_store(struct kobject *kobj, struct attribute *attr, 2723 const char *page, size_t length) 2724 { 2725 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); 2726 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj); 2727 ssize_t rv; 2728 mddev_t *mddev = rdev->mddev; 2729 2730 if (!entry->store) 2731 return -EIO; 2732 if (!capable(CAP_SYS_ADMIN)) 2733 return -EACCES; 2734 rv = mddev ? mddev_lock(mddev): -EBUSY; 2735 if (!rv) { 2736 if (rdev->mddev == NULL) 2737 rv = -EBUSY; 2738 else 2739 rv = entry->store(rdev, page, length); 2740 mddev_unlock(mddev); 2741 } 2742 return rv; 2743 } 2744 2745 static void rdev_free(struct kobject *ko) 2746 { 2747 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj); 2748 kfree(rdev); 2749 } 2750 static const struct sysfs_ops rdev_sysfs_ops = { 2751 .show = rdev_attr_show, 2752 .store = rdev_attr_store, 2753 }; 2754 static struct kobj_type rdev_ktype = { 2755 .release = rdev_free, 2756 .sysfs_ops = &rdev_sysfs_ops, 2757 .default_attrs = rdev_default_attrs, 2758 }; 2759 2760 void md_rdev_init(mdk_rdev_t *rdev) 2761 { 2762 rdev->desc_nr = -1; 2763 rdev->saved_raid_disk = -1; 2764 rdev->raid_disk = -1; 2765 rdev->flags = 0; 2766 rdev->data_offset = 0; 2767 rdev->sb_events = 0; 2768 rdev->last_read_error.tv_sec = 0; 2769 rdev->last_read_error.tv_nsec = 0; 2770 atomic_set(&rdev->nr_pending, 0); 2771 atomic_set(&rdev->read_errors, 0); 2772 atomic_set(&rdev->corrected_errors, 0); 2773 2774 INIT_LIST_HEAD(&rdev->same_set); 2775 init_waitqueue_head(&rdev->blocked_wait); 2776 } 2777 EXPORT_SYMBOL_GPL(md_rdev_init); 2778 /* 2779 * Import a device. If 'super_format' >= 0, then sanity check the superblock 2780 * 2781 * mark the device faulty if: 2782 * 2783 * - the device is nonexistent (zero size) 2784 * - the device has no valid superblock 2785 * 2786 * a faulty rdev _never_ has rdev->sb set. 2787 */ 2788 static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor) 2789 { 2790 char b[BDEVNAME_SIZE]; 2791 int err; 2792 mdk_rdev_t *rdev; 2793 sector_t size; 2794 2795 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL); 2796 if (!rdev) { 2797 printk(KERN_ERR "md: could not alloc mem for new device!\n"); 2798 return ERR_PTR(-ENOMEM); 2799 } 2800 2801 md_rdev_init(rdev); 2802 if ((err = alloc_disk_sb(rdev))) 2803 goto abort_free; 2804 2805 err = lock_rdev(rdev, newdev, super_format == -2); 2806 if (err) 2807 goto abort_free; 2808 2809 kobject_init(&rdev->kobj, &rdev_ktype); 2810 2811 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS; 2812 if (!size) { 2813 printk(KERN_WARNING 2814 "md: %s has zero or unknown size, marking faulty!\n", 2815 bdevname(rdev->bdev,b)); 2816 err = -EINVAL; 2817 goto abort_free; 2818 } 2819 2820 if (super_format >= 0) { 2821 err = super_types[super_format]. 2822 load_super(rdev, NULL, super_minor); 2823 if (err == -EINVAL) { 2824 printk(KERN_WARNING 2825 "md: %s does not have a valid v%d.%d " 2826 "superblock, not importing!\n", 2827 bdevname(rdev->bdev,b), 2828 super_format, super_minor); 2829 goto abort_free; 2830 } 2831 if (err < 0) { 2832 printk(KERN_WARNING 2833 "md: could not read %s's sb, not importing!\n", 2834 bdevname(rdev->bdev,b)); 2835 goto abort_free; 2836 } 2837 } 2838 2839 return rdev; 2840 2841 abort_free: 2842 if (rdev->sb_page) { 2843 if (rdev->bdev) 2844 unlock_rdev(rdev); 2845 free_disk_sb(rdev); 2846 } 2847 kfree(rdev); 2848 return ERR_PTR(err); 2849 } 2850 2851 /* 2852 * Check a full RAID array for plausibility 2853 */ 2854 2855 2856 static void analyze_sbs(mddev_t * mddev) 2857 { 2858 int i; 2859 mdk_rdev_t *rdev, *freshest, *tmp; 2860 char b[BDEVNAME_SIZE]; 2861 2862 freshest = NULL; 2863 rdev_for_each(rdev, tmp, mddev) 2864 switch (super_types[mddev->major_version]. 2865 load_super(rdev, freshest, mddev->minor_version)) { 2866 case 1: 2867 freshest = rdev; 2868 break; 2869 case 0: 2870 break; 2871 default: 2872 printk( KERN_ERR \ 2873 "md: fatal superblock inconsistency in %s" 2874 " -- removing from array\n", 2875 bdevname(rdev->bdev,b)); 2876 kick_rdev_from_array(rdev); 2877 } 2878 2879 2880 super_types[mddev->major_version]. 2881 validate_super(mddev, freshest); 2882 2883 i = 0; 2884 rdev_for_each(rdev, tmp, mddev) { 2885 if (mddev->max_disks && 2886 (rdev->desc_nr >= mddev->max_disks || 2887 i > mddev->max_disks)) { 2888 printk(KERN_WARNING 2889 "md: %s: %s: only %d devices permitted\n", 2890 mdname(mddev), bdevname(rdev->bdev, b), 2891 mddev->max_disks); 2892 kick_rdev_from_array(rdev); 2893 continue; 2894 } 2895 if (rdev != freshest) 2896 if (super_types[mddev->major_version]. 2897 validate_super(mddev, rdev)) { 2898 printk(KERN_WARNING "md: kicking non-fresh %s" 2899 " from array!\n", 2900 bdevname(rdev->bdev,b)); 2901 kick_rdev_from_array(rdev); 2902 continue; 2903 } 2904 if (mddev->level == LEVEL_MULTIPATH) { 2905 rdev->desc_nr = i++; 2906 rdev->raid_disk = rdev->desc_nr; 2907 set_bit(In_sync, &rdev->flags); 2908 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) { 2909 rdev->raid_disk = -1; 2910 clear_bit(In_sync, &rdev->flags); 2911 } 2912 } 2913 } 2914 2915 /* Read a fixed-point number. 2916 * Numbers in sysfs attributes should be in "standard" units where 2917 * possible, so time should be in seconds. 2918 * However we internally use a a much smaller unit such as 2919 * milliseconds or jiffies. 2920 * This function takes a decimal number with a possible fractional 2921 * component, and produces an integer which is the result of 2922 * multiplying that number by 10^'scale'. 2923 * all without any floating-point arithmetic. 2924 */ 2925 int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale) 2926 { 2927 unsigned long result = 0; 2928 long decimals = -1; 2929 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) { 2930 if (*cp == '.') 2931 decimals = 0; 2932 else if (decimals < scale) { 2933 unsigned int value; 2934 value = *cp - '0'; 2935 result = result * 10 + value; 2936 if (decimals >= 0) 2937 decimals++; 2938 } 2939 cp++; 2940 } 2941 if (*cp == '\n') 2942 cp++; 2943 if (*cp) 2944 return -EINVAL; 2945 if (decimals < 0) 2946 decimals = 0; 2947 while (decimals < scale) { 2948 result *= 10; 2949 decimals ++; 2950 } 2951 *res = result; 2952 return 0; 2953 } 2954 2955 2956 static void md_safemode_timeout(unsigned long data); 2957 2958 static ssize_t 2959 safe_delay_show(mddev_t *mddev, char *page) 2960 { 2961 int msec = (mddev->safemode_delay*1000)/HZ; 2962 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000); 2963 } 2964 static ssize_t 2965 safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len) 2966 { 2967 unsigned long msec; 2968 2969 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0) 2970 return -EINVAL; 2971 if (msec == 0) 2972 mddev->safemode_delay = 0; 2973 else { 2974 unsigned long old_delay = mddev->safemode_delay; 2975 mddev->safemode_delay = (msec*HZ)/1000; 2976 if (mddev->safemode_delay == 0) 2977 mddev->safemode_delay = 1; 2978 if (mddev->safemode_delay < old_delay) 2979 md_safemode_timeout((unsigned long)mddev); 2980 } 2981 return len; 2982 } 2983 static struct md_sysfs_entry md_safe_delay = 2984 __ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store); 2985 2986 static ssize_t 2987 level_show(mddev_t *mddev, char *page) 2988 { 2989 struct mdk_personality *p = mddev->pers; 2990 if (p) 2991 return sprintf(page, "%s\n", p->name); 2992 else if (mddev->clevel[0]) 2993 return sprintf(page, "%s\n", mddev->clevel); 2994 else if (mddev->level != LEVEL_NONE) 2995 return sprintf(page, "%d\n", mddev->level); 2996 else 2997 return 0; 2998 } 2999 3000 static ssize_t 3001 level_store(mddev_t *mddev, const char *buf, size_t len) 3002 { 3003 char clevel[16]; 3004 ssize_t rv = len; 3005 struct mdk_personality *pers; 3006 long level; 3007 void *priv; 3008 mdk_rdev_t *rdev; 3009 3010 if (mddev->pers == NULL) { 3011 if (len == 0) 3012 return 0; 3013 if (len >= sizeof(mddev->clevel)) 3014 return -ENOSPC; 3015 strncpy(mddev->clevel, buf, len); 3016 if (mddev->clevel[len-1] == '\n') 3017 len--; 3018 mddev->clevel[len] = 0; 3019 mddev->level = LEVEL_NONE; 3020 return rv; 3021 } 3022 3023 /* request to change the personality. Need to ensure: 3024 * - array is not engaged in resync/recovery/reshape 3025 * - old personality can be suspended 3026 * - new personality will access other array. 3027 */ 3028 3029 if (mddev->sync_thread || 3030 mddev->reshape_position != MaxSector || 3031 mddev->sysfs_active) 3032 return -EBUSY; 3033 3034 if (!mddev->pers->quiesce) { 3035 printk(KERN_WARNING "md: %s: %s does not support online personality change\n", 3036 mdname(mddev), mddev->pers->name); 3037 return -EINVAL; 3038 } 3039 3040 /* Now find the new personality */ 3041 if (len == 0 || len >= sizeof(clevel)) 3042 return -EINVAL; 3043 strncpy(clevel, buf, len); 3044 if (clevel[len-1] == '\n') 3045 len--; 3046 clevel[len] = 0; 3047 if (strict_strtol(clevel, 10, &level)) 3048 level = LEVEL_NONE; 3049 3050 if (request_module("md-%s", clevel) != 0) 3051 request_module("md-level-%s", clevel); 3052 spin_lock(&pers_lock); 3053 pers = find_pers(level, clevel); 3054 if (!pers || !try_module_get(pers->owner)) { 3055 spin_unlock(&pers_lock); 3056 printk(KERN_WARNING "md: personality %s not loaded\n", clevel); 3057 return -EINVAL; 3058 } 3059 spin_unlock(&pers_lock); 3060 3061 if (pers == mddev->pers) { 3062 /* Nothing to do! */ 3063 module_put(pers->owner); 3064 return rv; 3065 } 3066 if (!pers->takeover) { 3067 module_put(pers->owner); 3068 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n", 3069 mdname(mddev), clevel); 3070 return -EINVAL; 3071 } 3072 3073 list_for_each_entry(rdev, &mddev->disks, same_set) 3074 rdev->new_raid_disk = rdev->raid_disk; 3075 3076 /* ->takeover must set new_* and/or delta_disks 3077 * if it succeeds, and may set them when it fails. 3078 */ 3079 priv = pers->takeover(mddev); 3080 if (IS_ERR(priv)) { 3081 mddev->new_level = mddev->level; 3082 mddev->new_layout = mddev->layout; 3083 mddev->new_chunk_sectors = mddev->chunk_sectors; 3084 mddev->raid_disks -= mddev->delta_disks; 3085 mddev->delta_disks = 0; 3086 module_put(pers->owner); 3087 printk(KERN_WARNING "md: %s: %s would not accept array\n", 3088 mdname(mddev), clevel); 3089 return PTR_ERR(priv); 3090 } 3091 3092 /* Looks like we have a winner */ 3093 mddev_suspend(mddev); 3094 mddev->pers->stop(mddev); 3095 3096 if (mddev->pers->sync_request == NULL && 3097 pers->sync_request != NULL) { 3098 /* need to add the md_redundancy_group */ 3099 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group)) 3100 printk(KERN_WARNING 3101 "md: cannot register extra attributes for %s\n", 3102 mdname(mddev)); 3103 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, NULL, "sync_action"); 3104 } 3105 if (mddev->pers->sync_request != NULL && 3106 pers->sync_request == NULL) { 3107 /* need to remove the md_redundancy_group */ 3108 if (mddev->to_remove == NULL) 3109 mddev->to_remove = &md_redundancy_group; 3110 } 3111 3112 if (mddev->pers->sync_request == NULL && 3113 mddev->external) { 3114 /* We are converting from a no-redundancy array 3115 * to a redundancy array and metadata is managed 3116 * externally so we need to be sure that writes 3117 * won't block due to a need to transition 3118 * clean->dirty 3119 * until external management is started. 3120 */ 3121 mddev->in_sync = 0; 3122 mddev->safemode_delay = 0; 3123 mddev->safemode = 0; 3124 } 3125 3126 list_for_each_entry(rdev, &mddev->disks, same_set) { 3127 char nm[20]; 3128 if (rdev->raid_disk < 0) 3129 continue; 3130 if (rdev->new_raid_disk >= mddev->raid_disks) 3131 rdev->new_raid_disk = -1; 3132 if (rdev->new_raid_disk == rdev->raid_disk) 3133 continue; 3134 sprintf(nm, "rd%d", rdev->raid_disk); 3135 sysfs_remove_link(&mddev->kobj, nm); 3136 } 3137 list_for_each_entry(rdev, &mddev->disks, same_set) { 3138 if (rdev->raid_disk < 0) 3139 continue; 3140 if (rdev->new_raid_disk == rdev->raid_disk) 3141 continue; 3142 rdev->raid_disk = rdev->new_raid_disk; 3143 if (rdev->raid_disk < 0) 3144 clear_bit(In_sync, &rdev->flags); 3145 else { 3146 char nm[20]; 3147 sprintf(nm, "rd%d", rdev->raid_disk); 3148 if(sysfs_create_link(&mddev->kobj, &rdev->kobj, nm)) 3149 printk("md: cannot register %s for %s after level change\n", 3150 nm, mdname(mddev)); 3151 } 3152 } 3153 3154 module_put(mddev->pers->owner); 3155 mddev->pers = pers; 3156 mddev->private = priv; 3157 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); 3158 mddev->level = mddev->new_level; 3159 mddev->layout = mddev->new_layout; 3160 mddev->chunk_sectors = mddev->new_chunk_sectors; 3161 mddev->delta_disks = 0; 3162 if (mddev->pers->sync_request == NULL) { 3163 /* this is now an array without redundancy, so 3164 * it must always be in_sync 3165 */ 3166 mddev->in_sync = 1; 3167 del_timer_sync(&mddev->safemode_timer); 3168 } 3169 pers->run(mddev); 3170 mddev_resume(mddev); 3171 set_bit(MD_CHANGE_DEVS, &mddev->flags); 3172 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); 3173 md_wakeup_thread(mddev->thread); 3174 sysfs_notify(&mddev->kobj, NULL, "level"); 3175 md_new_event(mddev); 3176 return rv; 3177 } 3178 3179 static struct md_sysfs_entry md_level = 3180 __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store); 3181 3182 3183 static ssize_t 3184 layout_show(mddev_t *mddev, char *page) 3185 { 3186 /* just a number, not meaningful for all levels */ 3187 if (mddev->reshape_position != MaxSector && 3188 mddev->layout != mddev->new_layout) 3189 return sprintf(page, "%d (%d)\n", 3190 mddev->new_layout, mddev->layout); 3191 return sprintf(page, "%d\n", mddev->layout); 3192 } 3193 3194 static ssize_t 3195 layout_store(mddev_t *mddev, const char *buf, size_t len) 3196 { 3197 char *e; 3198 unsigned long n = simple_strtoul(buf, &e, 10); 3199 3200 if (!*buf || (*e && *e != '\n')) 3201 return -EINVAL; 3202 3203 if (mddev->pers) { 3204 int err; 3205 if (mddev->pers->check_reshape == NULL) 3206 return -EBUSY; 3207 mddev->new_layout = n; 3208 err = mddev->pers->check_reshape(mddev); 3209 if (err) { 3210 mddev->new_layout = mddev->layout; 3211 return err; 3212 } 3213 } else { 3214 mddev->new_layout = n; 3215 if (mddev->reshape_position == MaxSector) 3216 mddev->layout = n; 3217 } 3218 return len; 3219 } 3220 static struct md_sysfs_entry md_layout = 3221 __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store); 3222 3223 3224 static ssize_t 3225 raid_disks_show(mddev_t *mddev, char *page) 3226 { 3227 if (mddev->raid_disks == 0) 3228 return 0; 3229 if (mddev->reshape_position != MaxSector && 3230 mddev->delta_disks != 0) 3231 return sprintf(page, "%d (%d)\n", mddev->raid_disks, 3232 mddev->raid_disks - mddev->delta_disks); 3233 return sprintf(page, "%d\n", mddev->raid_disks); 3234 } 3235 3236 static int update_raid_disks(mddev_t *mddev, int raid_disks); 3237 3238 static ssize_t 3239 raid_disks_store(mddev_t *mddev, const char *buf, size_t len) 3240 { 3241 char *e; 3242 int rv = 0; 3243 unsigned long n = simple_strtoul(buf, &e, 10); 3244 3245 if (!*buf || (*e && *e != '\n')) 3246 return -EINVAL; 3247 3248 if (mddev->pers) 3249 rv = update_raid_disks(mddev, n); 3250 else if (mddev->reshape_position != MaxSector) { 3251 int olddisks = mddev->raid_disks - mddev->delta_disks; 3252 mddev->delta_disks = n - olddisks; 3253 mddev->raid_disks = n; 3254 } else 3255 mddev->raid_disks = n; 3256 return rv ? rv : len; 3257 } 3258 static struct md_sysfs_entry md_raid_disks = 3259 __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store); 3260 3261 static ssize_t 3262 chunk_size_show(mddev_t *mddev, char *page) 3263 { 3264 if (mddev->reshape_position != MaxSector && 3265 mddev->chunk_sectors != mddev->new_chunk_sectors) 3266 return sprintf(page, "%d (%d)\n", 3267 mddev->new_chunk_sectors << 9, 3268 mddev->chunk_sectors << 9); 3269 return sprintf(page, "%d\n", mddev->chunk_sectors << 9); 3270 } 3271 3272 static ssize_t 3273 chunk_size_store(mddev_t *mddev, const char *buf, size_t len) 3274 { 3275 char *e; 3276 unsigned long n = simple_strtoul(buf, &e, 10); 3277 3278 if (!*buf || (*e && *e != '\n')) 3279 return -EINVAL; 3280 3281 if (mddev->pers) { 3282 int err; 3283 if (mddev->pers->check_reshape == NULL) 3284 return -EBUSY; 3285 mddev->new_chunk_sectors = n >> 9; 3286 err = mddev->pers->check_reshape(mddev); 3287 if (err) { 3288 mddev->new_chunk_sectors = mddev->chunk_sectors; 3289 return err; 3290 } 3291 } else { 3292 mddev->new_chunk_sectors = n >> 9; 3293 if (mddev->reshape_position == MaxSector) 3294 mddev->chunk_sectors = n >> 9; 3295 } 3296 return len; 3297 } 3298 static struct md_sysfs_entry md_chunk_size = 3299 __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store); 3300 3301 static ssize_t 3302 resync_start_show(mddev_t *mddev, char *page) 3303 { 3304 if (mddev->recovery_cp == MaxSector) 3305 return sprintf(page, "none\n"); 3306 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp); 3307 } 3308 3309 static ssize_t 3310 resync_start_store(mddev_t *mddev, const char *buf, size_t len) 3311 { 3312 char *e; 3313 unsigned long long n = simple_strtoull(buf, &e, 10); 3314 3315 if (mddev->pers) 3316 return -EBUSY; 3317 if (cmd_match(buf, "none")) 3318 n = MaxSector; 3319 else if (!*buf || (*e && *e != '\n')) 3320 return -EINVAL; 3321 3322 mddev->recovery_cp = n; 3323 return len; 3324 } 3325 static struct md_sysfs_entry md_resync_start = 3326 __ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store); 3327 3328 /* 3329 * The array state can be: 3330 * 3331 * clear 3332 * No devices, no size, no level 3333 * Equivalent to STOP_ARRAY ioctl 3334 * inactive 3335 * May have some settings, but array is not active 3336 * all IO results in error 3337 * When written, doesn't tear down array, but just stops it 3338 * suspended (not supported yet) 3339 * All IO requests will block. The array can be reconfigured. 3340 * Writing this, if accepted, will block until array is quiescent 3341 * readonly 3342 * no resync can happen. no superblocks get written. 3343 * write requests fail 3344 * read-auto 3345 * like readonly, but behaves like 'clean' on a write request. 3346 * 3347 * clean - no pending writes, but otherwise active. 3348 * When written to inactive array, starts without resync 3349 * If a write request arrives then 3350 * if metadata is known, mark 'dirty' and switch to 'active'. 3351 * if not known, block and switch to write-pending 3352 * If written to an active array that has pending writes, then fails. 3353 * active 3354 * fully active: IO and resync can be happening. 3355 * When written to inactive array, starts with resync 3356 * 3357 * write-pending 3358 * clean, but writes are blocked waiting for 'active' to be written. 3359 * 3360 * active-idle 3361 * like active, but no writes have been seen for a while (100msec). 3362 * 3363 */ 3364 enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active, 3365 write_pending, active_idle, bad_word}; 3366 static char *array_states[] = { 3367 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active", 3368 "write-pending", "active-idle", NULL }; 3369 3370 static int match_word(const char *word, char **list) 3371 { 3372 int n; 3373 for (n=0; list[n]; n++) 3374 if (cmd_match(word, list[n])) 3375 break; 3376 return n; 3377 } 3378 3379 static ssize_t 3380 array_state_show(mddev_t *mddev, char *page) 3381 { 3382 enum array_state st = inactive; 3383 3384 if (mddev->pers) 3385 switch(mddev->ro) { 3386 case 1: 3387 st = readonly; 3388 break; 3389 case 2: 3390 st = read_auto; 3391 break; 3392 case 0: 3393 if (mddev->in_sync) 3394 st = clean; 3395 else if (test_bit(MD_CHANGE_PENDING, &mddev->flags)) 3396 st = write_pending; 3397 else if (mddev->safemode) 3398 st = active_idle; 3399 else 3400 st = active; 3401 } 3402 else { 3403 if (list_empty(&mddev->disks) && 3404 mddev->raid_disks == 0 && 3405 mddev->dev_sectors == 0) 3406 st = clear; 3407 else 3408 st = inactive; 3409 } 3410 return sprintf(page, "%s\n", array_states[st]); 3411 } 3412 3413 static int do_md_stop(mddev_t * mddev, int ro, int is_open); 3414 static int md_set_readonly(mddev_t * mddev, int is_open); 3415 static int do_md_run(mddev_t * mddev); 3416 static int restart_array(mddev_t *mddev); 3417 3418 static ssize_t 3419 array_state_store(mddev_t *mddev, const char *buf, size_t len) 3420 { 3421 int err = -EINVAL; 3422 enum array_state st = match_word(buf, array_states); 3423 switch(st) { 3424 case bad_word: 3425 break; 3426 case clear: 3427 /* stopping an active array */ 3428 if (atomic_read(&mddev->openers) > 0) 3429 return -EBUSY; 3430 err = do_md_stop(mddev, 0, 0); 3431 break; 3432 case inactive: 3433 /* stopping an active array */ 3434 if (mddev->pers) { 3435 if (atomic_read(&mddev->openers) > 0) 3436 return -EBUSY; 3437 err = do_md_stop(mddev, 2, 0); 3438 } else 3439 err = 0; /* already inactive */ 3440 break; 3441 case suspended: 3442 break; /* not supported yet */ 3443 case readonly: 3444 if (mddev->pers) 3445 err = md_set_readonly(mddev, 0); 3446 else { 3447 mddev->ro = 1; 3448 set_disk_ro(mddev->gendisk, 1); 3449 err = do_md_run(mddev); 3450 } 3451 break; 3452 case read_auto: 3453 if (mddev->pers) { 3454 if (mddev->ro == 0) 3455 err = md_set_readonly(mddev, 0); 3456 else if (mddev->ro == 1) 3457 err = restart_array(mddev); 3458 if (err == 0) { 3459 mddev->ro = 2; 3460 set_disk_ro(mddev->gendisk, 0); 3461 } 3462 } else { 3463 mddev->ro = 2; 3464 err = do_md_run(mddev); 3465 } 3466 break; 3467 case clean: 3468 if (mddev->pers) { 3469 restart_array(mddev); 3470 spin_lock_irq(&mddev->write_lock); 3471 if (atomic_read(&mddev->writes_pending) == 0) { 3472 if (mddev->in_sync == 0) { 3473 mddev->in_sync = 1; 3474 if (mddev->safemode == 1) 3475 mddev->safemode = 0; 3476 set_bit(MD_CHANGE_CLEAN, &mddev->flags); 3477 } 3478 err = 0; 3479 } else 3480 err = -EBUSY; 3481 spin_unlock_irq(&mddev->write_lock); 3482 } else 3483 err = -EINVAL; 3484 break; 3485 case active: 3486 if (mddev->pers) { 3487 restart_array(mddev); 3488 clear_bit(MD_CHANGE_PENDING, &mddev->flags); 3489 wake_up(&mddev->sb_wait); 3490 err = 0; 3491 } else { 3492 mddev->ro = 0; 3493 set_disk_ro(mddev->gendisk, 0); 3494 err = do_md_run(mddev); 3495 } 3496 break; 3497 case write_pending: 3498 case active_idle: 3499 /* these cannot be set */ 3500 break; 3501 } 3502 if (err) 3503 return err; 3504 else { 3505 sysfs_notify_dirent_safe(mddev->sysfs_state); 3506 return len; 3507 } 3508 } 3509 static struct md_sysfs_entry md_array_state = 3510 __ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store); 3511 3512 static ssize_t 3513 max_corrected_read_errors_show(mddev_t *mddev, char *page) { 3514 return sprintf(page, "%d\n", 3515 atomic_read(&mddev->max_corr_read_errors)); 3516 } 3517 3518 static ssize_t 3519 max_corrected_read_errors_store(mddev_t *mddev, const char *buf, size_t len) 3520 { 3521 char *e; 3522 unsigned long n = simple_strtoul(buf, &e, 10); 3523 3524 if (*buf && (*e == 0 || *e == '\n')) { 3525 atomic_set(&mddev->max_corr_read_errors, n); 3526 return len; 3527 } 3528 return -EINVAL; 3529 } 3530 3531 static struct md_sysfs_entry max_corr_read_errors = 3532 __ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show, 3533 max_corrected_read_errors_store); 3534 3535 static ssize_t 3536 null_show(mddev_t *mddev, char *page) 3537 { 3538 return -EINVAL; 3539 } 3540 3541 static ssize_t 3542 new_dev_store(mddev_t *mddev, const char *buf, size_t len) 3543 { 3544 /* buf must be %d:%d\n? giving major and minor numbers */ 3545 /* The new device is added to the array. 3546 * If the array has a persistent superblock, we read the 3547 * superblock to initialise info and check validity. 3548 * Otherwise, only checking done is that in bind_rdev_to_array, 3549 * which mainly checks size. 3550 */ 3551 char *e; 3552 int major = simple_strtoul(buf, &e, 10); 3553 int minor; 3554 dev_t dev; 3555 mdk_rdev_t *rdev; 3556 int err; 3557 3558 if (!*buf || *e != ':' || !e[1] || e[1] == '\n') 3559 return -EINVAL; 3560 minor = simple_strtoul(e+1, &e, 10); 3561 if (*e && *e != '\n') 3562 return -EINVAL; 3563 dev = MKDEV(major, minor); 3564 if (major != MAJOR(dev) || 3565 minor != MINOR(dev)) 3566 return -EOVERFLOW; 3567 3568 3569 if (mddev->persistent) { 3570 rdev = md_import_device(dev, mddev->major_version, 3571 mddev->minor_version); 3572 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) { 3573 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next, 3574 mdk_rdev_t, same_set); 3575 err = super_types[mddev->major_version] 3576 .load_super(rdev, rdev0, mddev->minor_version); 3577 if (err < 0) 3578 goto out; 3579 } 3580 } else if (mddev->external) 3581 rdev = md_import_device(dev, -2, -1); 3582 else 3583 rdev = md_import_device(dev, -1, -1); 3584 3585 if (IS_ERR(rdev)) 3586 return PTR_ERR(rdev); 3587 err = bind_rdev_to_array(rdev, mddev); 3588 out: 3589 if (err) 3590 export_rdev(rdev); 3591 return err ? err : len; 3592 } 3593 3594 static struct md_sysfs_entry md_new_device = 3595 __ATTR(new_dev, S_IWUSR, null_show, new_dev_store); 3596 3597 static ssize_t 3598 bitmap_store(mddev_t *mddev, const char *buf, size_t len) 3599 { 3600 char *end; 3601 unsigned long chunk, end_chunk; 3602 3603 if (!mddev->bitmap) 3604 goto out; 3605 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */ 3606 while (*buf) { 3607 chunk = end_chunk = simple_strtoul(buf, &end, 0); 3608 if (buf == end) break; 3609 if (*end == '-') { /* range */ 3610 buf = end + 1; 3611 end_chunk = simple_strtoul(buf, &end, 0); 3612 if (buf == end) break; 3613 } 3614 if (*end && !isspace(*end)) break; 3615 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk); 3616 buf = skip_spaces(end); 3617 } 3618 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */ 3619 out: 3620 return len; 3621 } 3622 3623 static struct md_sysfs_entry md_bitmap = 3624 __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store); 3625 3626 static ssize_t 3627 size_show(mddev_t *mddev, char *page) 3628 { 3629 return sprintf(page, "%llu\n", 3630 (unsigned long long)mddev->dev_sectors / 2); 3631 } 3632 3633 static int update_size(mddev_t *mddev, sector_t num_sectors); 3634 3635 static ssize_t 3636 size_store(mddev_t *mddev, const char *buf, size_t len) 3637 { 3638 /* If array is inactive, we can reduce the component size, but 3639 * not increase it (except from 0). 3640 * If array is active, we can try an on-line resize 3641 */ 3642 sector_t sectors; 3643 int err = strict_blocks_to_sectors(buf, §ors); 3644 3645 if (err < 0) 3646 return err; 3647 if (mddev->pers) { 3648 err = update_size(mddev, sectors); 3649 md_update_sb(mddev, 1); 3650 } else { 3651 if (mddev->dev_sectors == 0 || 3652 mddev->dev_sectors > sectors) 3653 mddev->dev_sectors = sectors; 3654 else 3655 err = -ENOSPC; 3656 } 3657 return err ? err : len; 3658 } 3659 3660 static struct md_sysfs_entry md_size = 3661 __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store); 3662 3663 3664 /* Metdata version. 3665 * This is one of 3666 * 'none' for arrays with no metadata (good luck...) 3667 * 'external' for arrays with externally managed metadata, 3668 * or N.M for internally known formats 3669 */ 3670 static ssize_t 3671 metadata_show(mddev_t *mddev, char *page) 3672 { 3673 if (mddev->persistent) 3674 return sprintf(page, "%d.%d\n", 3675 mddev->major_version, mddev->minor_version); 3676 else if (mddev->external) 3677 return sprintf(page, "external:%s\n", mddev->metadata_type); 3678 else 3679 return sprintf(page, "none\n"); 3680 } 3681 3682 static ssize_t 3683 metadata_store(mddev_t *mddev, const char *buf, size_t len) 3684 { 3685 int major, minor; 3686 char *e; 3687 /* Changing the details of 'external' metadata is 3688 * always permitted. Otherwise there must be 3689 * no devices attached to the array. 3690 */ 3691 if (mddev->external && strncmp(buf, "external:", 9) == 0) 3692 ; 3693 else if (!list_empty(&mddev->disks)) 3694 return -EBUSY; 3695 3696 if (cmd_match(buf, "none")) { 3697 mddev->persistent = 0; 3698 mddev->external = 0; 3699 mddev->major_version = 0; 3700 mddev->minor_version = 90; 3701 return len; 3702 } 3703 if (strncmp(buf, "external:", 9) == 0) { 3704 size_t namelen = len-9; 3705 if (namelen >= sizeof(mddev->metadata_type)) 3706 namelen = sizeof(mddev->metadata_type)-1; 3707 strncpy(mddev->metadata_type, buf+9, namelen); 3708 mddev->metadata_type[namelen] = 0; 3709 if (namelen && mddev->metadata_type[namelen-1] == '\n') 3710 mddev->metadata_type[--namelen] = 0; 3711 mddev->persistent = 0; 3712 mddev->external = 1; 3713 mddev->major_version = 0; 3714 mddev->minor_version = 90; 3715 return len; 3716 } 3717 major = simple_strtoul(buf, &e, 10); 3718 if (e==buf || *e != '.') 3719 return -EINVAL; 3720 buf = e+1; 3721 minor = simple_strtoul(buf, &e, 10); 3722 if (e==buf || (*e && *e != '\n') ) 3723 return -EINVAL; 3724 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL) 3725 return -ENOENT; 3726 mddev->major_version = major; 3727 mddev->minor_version = minor; 3728 mddev->persistent = 1; 3729 mddev->external = 0; 3730 return len; 3731 } 3732 3733 static struct md_sysfs_entry md_metadata = 3734 __ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store); 3735 3736 static ssize_t 3737 action_show(mddev_t *mddev, char *page) 3738 { 3739 char *type = "idle"; 3740 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) 3741 type = "frozen"; 3742 else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || 3743 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) { 3744 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) 3745 type = "reshape"; 3746 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { 3747 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) 3748 type = "resync"; 3749 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) 3750 type = "check"; 3751 else 3752 type = "repair"; 3753 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery)) 3754 type = "recover"; 3755 } 3756 return sprintf(page, "%s\n", type); 3757 } 3758 3759 static void reap_sync_thread(mddev_t *mddev); 3760 3761 static ssize_t 3762 action_store(mddev_t *mddev, const char *page, size_t len) 3763 { 3764 if (!mddev->pers || !mddev->pers->sync_request) 3765 return -EINVAL; 3766 3767 if (cmd_match(page, "frozen")) 3768 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); 3769 else 3770 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); 3771 3772 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) { 3773 if (mddev->sync_thread) { 3774 set_bit(MD_RECOVERY_INTR, &mddev->recovery); 3775 reap_sync_thread(mddev); 3776 } 3777 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || 3778 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) 3779 return -EBUSY; 3780 else if (cmd_match(page, "resync")) 3781 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); 3782 else if (cmd_match(page, "recover")) { 3783 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); 3784 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); 3785 } else if (cmd_match(page, "reshape")) { 3786 int err; 3787 if (mddev->pers->start_reshape == NULL) 3788 return -EINVAL; 3789 err = mddev->pers->start_reshape(mddev); 3790 if (err) 3791 return err; 3792 sysfs_notify(&mddev->kobj, NULL, "degraded"); 3793 } else { 3794 if (cmd_match(page, "check")) 3795 set_bit(MD_RECOVERY_CHECK, &mddev->recovery); 3796 else if (!cmd_match(page, "repair")) 3797 return -EINVAL; 3798 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); 3799 set_bit(MD_RECOVERY_SYNC, &mddev->recovery); 3800 } 3801 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); 3802 md_wakeup_thread(mddev->thread); 3803 sysfs_notify_dirent_safe(mddev->sysfs_action); 3804 return len; 3805 } 3806 3807 static ssize_t 3808 mismatch_cnt_show(mddev_t *mddev, char *page) 3809 { 3810 return sprintf(page, "%llu\n", 3811 (unsigned long long) mddev->resync_mismatches); 3812 } 3813 3814 static struct md_sysfs_entry md_scan_mode = 3815 __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store); 3816 3817 3818 static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt); 3819 3820 static ssize_t 3821 sync_min_show(mddev_t *mddev, char *page) 3822 { 3823 return sprintf(page, "%d (%s)\n", speed_min(mddev), 3824 mddev->sync_speed_min ? "local": "system"); 3825 } 3826 3827 static ssize_t 3828 sync_min_store(mddev_t *mddev, const char *buf, size_t len) 3829 { 3830 int min; 3831 char *e; 3832 if (strncmp(buf, "system", 6)==0) { 3833 mddev->sync_speed_min = 0; 3834 return len; 3835 } 3836 min = simple_strtoul(buf, &e, 10); 3837 if (buf == e || (*e && *e != '\n') || min <= 0) 3838 return -EINVAL; 3839 mddev->sync_speed_min = min; 3840 return len; 3841 } 3842 3843 static struct md_sysfs_entry md_sync_min = 3844 __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store); 3845 3846 static ssize_t 3847 sync_max_show(mddev_t *mddev, char *page) 3848 { 3849 return sprintf(page, "%d (%s)\n", speed_max(mddev), 3850 mddev->sync_speed_max ? "local": "system"); 3851 } 3852 3853 static ssize_t 3854 sync_max_store(mddev_t *mddev, const char *buf, size_t len) 3855 { 3856 int max; 3857 char *e; 3858 if (strncmp(buf, "system", 6)==0) { 3859 mddev->sync_speed_max = 0; 3860 return len; 3861 } 3862 max = simple_strtoul(buf, &e, 10); 3863 if (buf == e || (*e && *e != '\n') || max <= 0) 3864 return -EINVAL; 3865 mddev->sync_speed_max = max; 3866 return len; 3867 } 3868 3869 static struct md_sysfs_entry md_sync_max = 3870 __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store); 3871 3872 static ssize_t 3873 degraded_show(mddev_t *mddev, char *page) 3874 { 3875 return sprintf(page, "%d\n", mddev->degraded); 3876 } 3877 static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded); 3878 3879 static ssize_t 3880 sync_force_parallel_show(mddev_t *mddev, char *page) 3881 { 3882 return sprintf(page, "%d\n", mddev->parallel_resync); 3883 } 3884 3885 static ssize_t 3886 sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len) 3887 { 3888 long n; 3889 3890 if (strict_strtol(buf, 10, &n)) 3891 return -EINVAL; 3892 3893 if (n != 0 && n != 1) 3894 return -EINVAL; 3895 3896 mddev->parallel_resync = n; 3897 3898 if (mddev->sync_thread) 3899 wake_up(&resync_wait); 3900 3901 return len; 3902 } 3903 3904 /* force parallel resync, even with shared block devices */ 3905 static struct md_sysfs_entry md_sync_force_parallel = 3906 __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR, 3907 sync_force_parallel_show, sync_force_parallel_store); 3908 3909 static ssize_t 3910 sync_speed_show(mddev_t *mddev, char *page) 3911 { 3912 unsigned long resync, dt, db; 3913 if (mddev->curr_resync == 0) 3914 return sprintf(page, "none\n"); 3915 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active); 3916 dt = (jiffies - mddev->resync_mark) / HZ; 3917 if (!dt) dt++; 3918 db = resync - mddev->resync_mark_cnt; 3919 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */ 3920 } 3921 3922 static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed); 3923 3924 static ssize_t 3925 sync_completed_show(mddev_t *mddev, char *page) 3926 { 3927 unsigned long long max_sectors, resync; 3928 3929 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) 3930 return sprintf(page, "none\n"); 3931 3932 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) 3933 max_sectors = mddev->resync_max_sectors; 3934 else 3935 max_sectors = mddev->dev_sectors; 3936 3937 resync = mddev->curr_resync_completed; 3938 return sprintf(page, "%llu / %llu\n", resync, max_sectors); 3939 } 3940 3941 static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed); 3942 3943 static ssize_t 3944 min_sync_show(mddev_t *mddev, char *page) 3945 { 3946 return sprintf(page, "%llu\n", 3947 (unsigned long long)mddev->resync_min); 3948 } 3949 static ssize_t 3950 min_sync_store(mddev_t *mddev, const char *buf, size_t len) 3951 { 3952 unsigned long long min; 3953 if (strict_strtoull(buf, 10, &min)) 3954 return -EINVAL; 3955 if (min > mddev->resync_max) 3956 return -EINVAL; 3957 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) 3958 return -EBUSY; 3959 3960 /* Must be a multiple of chunk_size */ 3961 if (mddev->chunk_sectors) { 3962 sector_t temp = min; 3963 if (sector_div(temp, mddev->chunk_sectors)) 3964 return -EINVAL; 3965 } 3966 mddev->resync_min = min; 3967 3968 return len; 3969 } 3970 3971 static struct md_sysfs_entry md_min_sync = 3972 __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store); 3973 3974 static ssize_t 3975 max_sync_show(mddev_t *mddev, char *page) 3976 { 3977 if (mddev->resync_max == MaxSector) 3978 return sprintf(page, "max\n"); 3979 else 3980 return sprintf(page, "%llu\n", 3981 (unsigned long long)mddev->resync_max); 3982 } 3983 static ssize_t 3984 max_sync_store(mddev_t *mddev, const char *buf, size_t len) 3985 { 3986 if (strncmp(buf, "max", 3) == 0) 3987 mddev->resync_max = MaxSector; 3988 else { 3989 unsigned long long max; 3990 if (strict_strtoull(buf, 10, &max)) 3991 return -EINVAL; 3992 if (max < mddev->resync_min) 3993 return -EINVAL; 3994 if (max < mddev->resync_max && 3995 mddev->ro == 0 && 3996 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) 3997 return -EBUSY; 3998 3999 /* Must be a multiple of chunk_size */ 4000 if (mddev->chunk_sectors) { 4001 sector_t temp = max; 4002 if (sector_div(temp, mddev->chunk_sectors)) 4003 return -EINVAL; 4004 } 4005 mddev->resync_max = max; 4006 } 4007 wake_up(&mddev->recovery_wait); 4008 return len; 4009 } 4010 4011 static struct md_sysfs_entry md_max_sync = 4012 __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store); 4013 4014 static ssize_t 4015 suspend_lo_show(mddev_t *mddev, char *page) 4016 { 4017 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo); 4018 } 4019 4020 static ssize_t 4021 suspend_lo_store(mddev_t *mddev, const char *buf, size_t len) 4022 { 4023 char *e; 4024 unsigned long long new = simple_strtoull(buf, &e, 10); 4025 unsigned long long old = mddev->suspend_lo; 4026 4027 if (mddev->pers == NULL || 4028 mddev->pers->quiesce == NULL) 4029 return -EINVAL; 4030 if (buf == e || (*e && *e != '\n')) 4031 return -EINVAL; 4032 4033 mddev->suspend_lo = new; 4034 if (new >= old) 4035 /* Shrinking suspended region */ 4036 mddev->pers->quiesce(mddev, 2); 4037 else { 4038 /* Expanding suspended region - need to wait */ 4039 mddev->pers->quiesce(mddev, 1); 4040 mddev->pers->quiesce(mddev, 0); 4041 } 4042 return len; 4043 } 4044 static struct md_sysfs_entry md_suspend_lo = 4045 __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store); 4046 4047 4048 static ssize_t 4049 suspend_hi_show(mddev_t *mddev, char *page) 4050 { 4051 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi); 4052 } 4053 4054 static ssize_t 4055 suspend_hi_store(mddev_t *mddev, const char *buf, size_t len) 4056 { 4057 char *e; 4058 unsigned long long new = simple_strtoull(buf, &e, 10); 4059 unsigned long long old = mddev->suspend_hi; 4060 4061 if (mddev->pers == NULL || 4062 mddev->pers->quiesce == NULL) 4063 return -EINVAL; 4064 if (buf == e || (*e && *e != '\n')) 4065 return -EINVAL; 4066 4067 mddev->suspend_hi = new; 4068 if (new <= old) 4069 /* Shrinking suspended region */ 4070 mddev->pers->quiesce(mddev, 2); 4071 else { 4072 /* Expanding suspended region - need to wait */ 4073 mddev->pers->quiesce(mddev, 1); 4074 mddev->pers->quiesce(mddev, 0); 4075 } 4076 return len; 4077 } 4078 static struct md_sysfs_entry md_suspend_hi = 4079 __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store); 4080 4081 static ssize_t 4082 reshape_position_show(mddev_t *mddev, char *page) 4083 { 4084 if (mddev->reshape_position != MaxSector) 4085 return sprintf(page, "%llu\n", 4086 (unsigned long long)mddev->reshape_position); 4087 strcpy(page, "none\n"); 4088 return 5; 4089 } 4090 4091 static ssize_t 4092 reshape_position_store(mddev_t *mddev, const char *buf, size_t len) 4093 { 4094 char *e; 4095 unsigned long long new = simple_strtoull(buf, &e, 10); 4096 if (mddev->pers) 4097 return -EBUSY; 4098 if (buf == e || (*e && *e != '\n')) 4099 return -EINVAL; 4100 mddev->reshape_position = new; 4101 mddev->delta_disks = 0; 4102 mddev->new_level = mddev->level; 4103 mddev->new_layout = mddev->layout; 4104 mddev->new_chunk_sectors = mddev->chunk_sectors; 4105 return len; 4106 } 4107 4108 static struct md_sysfs_entry md_reshape_position = 4109 __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show, 4110 reshape_position_store); 4111 4112 static ssize_t 4113 array_size_show(mddev_t *mddev, char *page) 4114 { 4115 if (mddev->external_size) 4116 return sprintf(page, "%llu\n", 4117 (unsigned long long)mddev->array_sectors/2); 4118 else 4119 return sprintf(page, "default\n"); 4120 } 4121 4122 static ssize_t 4123 array_size_store(mddev_t *mddev, const char *buf, size_t len) 4124 { 4125 sector_t sectors; 4126 4127 if (strncmp(buf, "default", 7) == 0) { 4128 if (mddev->pers) 4129 sectors = mddev->pers->size(mddev, 0, 0); 4130 else 4131 sectors = mddev->array_sectors; 4132 4133 mddev->external_size = 0; 4134 } else { 4135 if (strict_blocks_to_sectors(buf, §ors) < 0) 4136 return -EINVAL; 4137 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors) 4138 return -E2BIG; 4139 4140 mddev->external_size = 1; 4141 } 4142 4143 mddev->array_sectors = sectors; 4144 if (mddev->pers) { 4145 set_capacity(mddev->gendisk, mddev->array_sectors); 4146 revalidate_disk(mddev->gendisk); 4147 } 4148 return len; 4149 } 4150 4151 static struct md_sysfs_entry md_array_size = 4152 __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show, 4153 array_size_store); 4154 4155 static struct attribute *md_default_attrs[] = { 4156 &md_level.attr, 4157 &md_layout.attr, 4158 &md_raid_disks.attr, 4159 &md_chunk_size.attr, 4160 &md_size.attr, 4161 &md_resync_start.attr, 4162 &md_metadata.attr, 4163 &md_new_device.attr, 4164 &md_safe_delay.attr, 4165 &md_array_state.attr, 4166 &md_reshape_position.attr, 4167 &md_array_size.attr, 4168 &max_corr_read_errors.attr, 4169 NULL, 4170 }; 4171 4172 static struct attribute *md_redundancy_attrs[] = { 4173 &md_scan_mode.attr, 4174 &md_mismatches.attr, 4175 &md_sync_min.attr, 4176 &md_sync_max.attr, 4177 &md_sync_speed.attr, 4178 &md_sync_force_parallel.attr, 4179 &md_sync_completed.attr, 4180 &md_min_sync.attr, 4181 &md_max_sync.attr, 4182 &md_suspend_lo.attr, 4183 &md_suspend_hi.attr, 4184 &md_bitmap.attr, 4185 &md_degraded.attr, 4186 NULL, 4187 }; 4188 static struct attribute_group md_redundancy_group = { 4189 .name = NULL, 4190 .attrs = md_redundancy_attrs, 4191 }; 4192 4193 4194 static ssize_t 4195 md_attr_show(struct kobject *kobj, struct attribute *attr, char *page) 4196 { 4197 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); 4198 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); 4199 ssize_t rv; 4200 4201 if (!entry->show) 4202 return -EIO; 4203 rv = mddev_lock(mddev); 4204 if (!rv) { 4205 rv = entry->show(mddev, page); 4206 mddev_unlock(mddev); 4207 } 4208 return rv; 4209 } 4210 4211 static ssize_t 4212 md_attr_store(struct kobject *kobj, struct attribute *attr, 4213 const char *page, size_t length) 4214 { 4215 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); 4216 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); 4217 ssize_t rv; 4218 4219 if (!entry->store) 4220 return -EIO; 4221 if (!capable(CAP_SYS_ADMIN)) 4222 return -EACCES; 4223 rv = mddev_lock(mddev); 4224 if (mddev->hold_active == UNTIL_IOCTL) 4225 mddev->hold_active = 0; 4226 if (!rv) { 4227 rv = entry->store(mddev, page, length); 4228 mddev_unlock(mddev); 4229 } 4230 return rv; 4231 } 4232 4233 static void md_free(struct kobject *ko) 4234 { 4235 mddev_t *mddev = container_of(ko, mddev_t, kobj); 4236 4237 if (mddev->sysfs_state) 4238 sysfs_put(mddev->sysfs_state); 4239 4240 if (mddev->gendisk) { 4241 del_gendisk(mddev->gendisk); 4242 put_disk(mddev->gendisk); 4243 } 4244 if (mddev->queue) 4245 blk_cleanup_queue(mddev->queue); 4246 4247 kfree(mddev); 4248 } 4249 4250 static const struct sysfs_ops md_sysfs_ops = { 4251 .show = md_attr_show, 4252 .store = md_attr_store, 4253 }; 4254 static struct kobj_type md_ktype = { 4255 .release = md_free, 4256 .sysfs_ops = &md_sysfs_ops, 4257 .default_attrs = md_default_attrs, 4258 }; 4259 4260 int mdp_major = 0; 4261 4262 static void mddev_delayed_delete(struct work_struct *ws) 4263 { 4264 mddev_t *mddev = container_of(ws, mddev_t, del_work); 4265 4266 sysfs_remove_group(&mddev->kobj, &md_bitmap_group); 4267 kobject_del(&mddev->kobj); 4268 kobject_put(&mddev->kobj); 4269 } 4270 4271 static int md_alloc(dev_t dev, char *name) 4272 { 4273 static DEFINE_MUTEX(disks_mutex); 4274 mddev_t *mddev = mddev_find(dev); 4275 struct gendisk *disk; 4276 int partitioned; 4277 int shift; 4278 int unit; 4279 int error; 4280 4281 if (!mddev) 4282 return -ENODEV; 4283 4284 partitioned = (MAJOR(mddev->unit) != MD_MAJOR); 4285 shift = partitioned ? MdpMinorShift : 0; 4286 unit = MINOR(mddev->unit) >> shift; 4287 4288 /* wait for any previous instance of this device to be 4289 * completely removed (mddev_delayed_delete). 4290 */ 4291 flush_workqueue(md_misc_wq); 4292 4293 mutex_lock(&disks_mutex); 4294 error = -EEXIST; 4295 if (mddev->gendisk) 4296 goto abort; 4297 4298 if (name) { 4299 /* Need to ensure that 'name' is not a duplicate. 4300 */ 4301 mddev_t *mddev2; 4302 spin_lock(&all_mddevs_lock); 4303 4304 list_for_each_entry(mddev2, &all_mddevs, all_mddevs) 4305 if (mddev2->gendisk && 4306 strcmp(mddev2->gendisk->disk_name, name) == 0) { 4307 spin_unlock(&all_mddevs_lock); 4308 goto abort; 4309 } 4310 spin_unlock(&all_mddevs_lock); 4311 } 4312 4313 error = -ENOMEM; 4314 mddev->queue = blk_alloc_queue(GFP_KERNEL); 4315 if (!mddev->queue) 4316 goto abort; 4317 mddev->queue->queuedata = mddev; 4318 4319 blk_queue_make_request(mddev->queue, md_make_request); 4320 4321 disk = alloc_disk(1 << shift); 4322 if (!disk) { 4323 blk_cleanup_queue(mddev->queue); 4324 mddev->queue = NULL; 4325 goto abort; 4326 } 4327 disk->major = MAJOR(mddev->unit); 4328 disk->first_minor = unit << shift; 4329 if (name) 4330 strcpy(disk->disk_name, name); 4331 else if (partitioned) 4332 sprintf(disk->disk_name, "md_d%d", unit); 4333 else 4334 sprintf(disk->disk_name, "md%d", unit); 4335 disk->fops = &md_fops; 4336 disk->private_data = mddev; 4337 disk->queue = mddev->queue; 4338 /* Allow extended partitions. This makes the 4339 * 'mdp' device redundant, but we can't really 4340 * remove it now. 4341 */ 4342 disk->flags |= GENHD_FL_EXT_DEVT; 4343 add_disk(disk); 4344 mddev->gendisk = disk; 4345 error = kobject_init_and_add(&mddev->kobj, &md_ktype, 4346 &disk_to_dev(disk)->kobj, "%s", "md"); 4347 if (error) { 4348 /* This isn't possible, but as kobject_init_and_add is marked 4349 * __must_check, we must do something with the result 4350 */ 4351 printk(KERN_WARNING "md: cannot register %s/md - name in use\n", 4352 disk->disk_name); 4353 error = 0; 4354 } 4355 if (mddev->kobj.sd && 4356 sysfs_create_group(&mddev->kobj, &md_bitmap_group)) 4357 printk(KERN_DEBUG "pointless warning\n"); 4358 4359 blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA); 4360 abort: 4361 mutex_unlock(&disks_mutex); 4362 if (!error && mddev->kobj.sd) { 4363 kobject_uevent(&mddev->kobj, KOBJ_ADD); 4364 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state"); 4365 } 4366 mddev_put(mddev); 4367 return error; 4368 } 4369 4370 static struct kobject *md_probe(dev_t dev, int *part, void *data) 4371 { 4372 md_alloc(dev, NULL); 4373 return NULL; 4374 } 4375 4376 static int add_named_array(const char *val, struct kernel_param *kp) 4377 { 4378 /* val must be "md_*" where * is not all digits. 4379 * We allocate an array with a large free minor number, and 4380 * set the name to val. val must not already be an active name. 4381 */ 4382 int len = strlen(val); 4383 char buf[DISK_NAME_LEN]; 4384 4385 while (len && val[len-1] == '\n') 4386 len--; 4387 if (len >= DISK_NAME_LEN) 4388 return -E2BIG; 4389 strlcpy(buf, val, len+1); 4390 if (strncmp(buf, "md_", 3) != 0) 4391 return -EINVAL; 4392 return md_alloc(0, buf); 4393 } 4394 4395 static void md_safemode_timeout(unsigned long data) 4396 { 4397 mddev_t *mddev = (mddev_t *) data; 4398 4399 if (!atomic_read(&mddev->writes_pending)) { 4400 mddev->safemode = 1; 4401 if (mddev->external) 4402 sysfs_notify_dirent_safe(mddev->sysfs_state); 4403 } 4404 md_wakeup_thread(mddev->thread); 4405 } 4406 4407 static int start_dirty_degraded; 4408 4409 int md_run(mddev_t *mddev) 4410 { 4411 int err; 4412 mdk_rdev_t *rdev; 4413 struct mdk_personality *pers; 4414 4415 if (list_empty(&mddev->disks)) 4416 /* cannot run an array with no devices.. */ 4417 return -EINVAL; 4418 4419 if (mddev->pers) 4420 return -EBUSY; 4421 /* Cannot run until previous stop completes properly */ 4422 if (mddev->sysfs_active) 4423 return -EBUSY; 4424 4425 /* 4426 * Analyze all RAID superblock(s) 4427 */ 4428 if (!mddev->raid_disks) { 4429 if (!mddev->persistent) 4430 return -EINVAL; 4431 analyze_sbs(mddev); 4432 } 4433 4434 if (mddev->level != LEVEL_NONE) 4435 request_module("md-level-%d", mddev->level); 4436 else if (mddev->clevel[0]) 4437 request_module("md-%s", mddev->clevel); 4438 4439 /* 4440 * Drop all container device buffers, from now on 4441 * the only valid external interface is through the md 4442 * device. 4443 */ 4444 list_for_each_entry(rdev, &mddev->disks, same_set) { 4445 if (test_bit(Faulty, &rdev->flags)) 4446 continue; 4447 sync_blockdev(rdev->bdev); 4448 invalidate_bdev(rdev->bdev); 4449 4450 /* perform some consistency tests on the device. 4451 * We don't want the data to overlap the metadata, 4452 * Internal Bitmap issues have been handled elsewhere. 4453 */ 4454 if (rdev->meta_bdev) { 4455 /* Nothing to check */; 4456 } else if (rdev->data_offset < rdev->sb_start) { 4457 if (mddev->dev_sectors && 4458 rdev->data_offset + mddev->dev_sectors 4459 > rdev->sb_start) { 4460 printk("md: %s: data overlaps metadata\n", 4461 mdname(mddev)); 4462 return -EINVAL; 4463 } 4464 } else { 4465 if (rdev->sb_start + rdev->sb_size/512 4466 > rdev->data_offset) { 4467 printk("md: %s: metadata overlaps data\n", 4468 mdname(mddev)); 4469 return -EINVAL; 4470 } 4471 } 4472 sysfs_notify_dirent_safe(rdev->sysfs_state); 4473 } 4474 4475 if (mddev->bio_set == NULL) 4476 mddev->bio_set = bioset_create(BIO_POOL_SIZE, sizeof(mddev)); 4477 4478 spin_lock(&pers_lock); 4479 pers = find_pers(mddev->level, mddev->clevel); 4480 if (!pers || !try_module_get(pers->owner)) { 4481 spin_unlock(&pers_lock); 4482 if (mddev->level != LEVEL_NONE) 4483 printk(KERN_WARNING "md: personality for level %d is not loaded!\n", 4484 mddev->level); 4485 else 4486 printk(KERN_WARNING "md: personality for level %s is not loaded!\n", 4487 mddev->clevel); 4488 return -EINVAL; 4489 } 4490 mddev->pers = pers; 4491 spin_unlock(&pers_lock); 4492 if (mddev->level != pers->level) { 4493 mddev->level = pers->level; 4494 mddev->new_level = pers->level; 4495 } 4496 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); 4497 4498 if (mddev->reshape_position != MaxSector && 4499 pers->start_reshape == NULL) { 4500 /* This personality cannot handle reshaping... */ 4501 mddev->pers = NULL; 4502 module_put(pers->owner); 4503 return -EINVAL; 4504 } 4505 4506 if (pers->sync_request) { 4507 /* Warn if this is a potentially silly 4508 * configuration. 4509 */ 4510 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; 4511 mdk_rdev_t *rdev2; 4512 int warned = 0; 4513 4514 list_for_each_entry(rdev, &mddev->disks, same_set) 4515 list_for_each_entry(rdev2, &mddev->disks, same_set) { 4516 if (rdev < rdev2 && 4517 rdev->bdev->bd_contains == 4518 rdev2->bdev->bd_contains) { 4519 printk(KERN_WARNING 4520 "%s: WARNING: %s appears to be" 4521 " on the same physical disk as" 4522 " %s.\n", 4523 mdname(mddev), 4524 bdevname(rdev->bdev,b), 4525 bdevname(rdev2->bdev,b2)); 4526 warned = 1; 4527 } 4528 } 4529 4530 if (warned) 4531 printk(KERN_WARNING 4532 "True protection against single-disk" 4533 " failure might be compromised.\n"); 4534 } 4535 4536 mddev->recovery = 0; 4537 /* may be over-ridden by personality */ 4538 mddev->resync_max_sectors = mddev->dev_sectors; 4539 4540 mddev->ok_start_degraded = start_dirty_degraded; 4541 4542 if (start_readonly && mddev->ro == 0) 4543 mddev->ro = 2; /* read-only, but switch on first write */ 4544 4545 err = mddev->pers->run(mddev); 4546 if (err) 4547 printk(KERN_ERR "md: pers->run() failed ...\n"); 4548 else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) { 4549 WARN_ONCE(!mddev->external_size, "%s: default size too small," 4550 " but 'external_size' not in effect?\n", __func__); 4551 printk(KERN_ERR 4552 "md: invalid array_size %llu > default size %llu\n", 4553 (unsigned long long)mddev->array_sectors / 2, 4554 (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2); 4555 err = -EINVAL; 4556 mddev->pers->stop(mddev); 4557 } 4558 if (err == 0 && mddev->pers->sync_request) { 4559 err = bitmap_create(mddev); 4560 if (err) { 4561 printk(KERN_ERR "%s: failed to create bitmap (%d)\n", 4562 mdname(mddev), err); 4563 mddev->pers->stop(mddev); 4564 } 4565 } 4566 if (err) { 4567 module_put(mddev->pers->owner); 4568 mddev->pers = NULL; 4569 bitmap_destroy(mddev); 4570 return err; 4571 } 4572 if (mddev->pers->sync_request) { 4573 if (mddev->kobj.sd && 4574 sysfs_create_group(&mddev->kobj, &md_redundancy_group)) 4575 printk(KERN_WARNING 4576 "md: cannot register extra attributes for %s\n", 4577 mdname(mddev)); 4578 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action"); 4579 } else if (mddev->ro == 2) /* auto-readonly not meaningful */ 4580 mddev->ro = 0; 4581 4582 atomic_set(&mddev->writes_pending,0); 4583 atomic_set(&mddev->max_corr_read_errors, 4584 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS); 4585 mddev->safemode = 0; 4586 mddev->safemode_timer.function = md_safemode_timeout; 4587 mddev->safemode_timer.data = (unsigned long) mddev; 4588 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */ 4589 mddev->in_sync = 1; 4590 smp_wmb(); 4591 mddev->ready = 1; 4592 list_for_each_entry(rdev, &mddev->disks, same_set) 4593 if (rdev->raid_disk >= 0) { 4594 char nm[20]; 4595 sprintf(nm, "rd%d", rdev->raid_disk); 4596 if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm)) 4597 /* failure here is OK */; 4598 } 4599 4600 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); 4601 4602 if (mddev->flags) 4603 md_update_sb(mddev, 0); 4604 4605 md_wakeup_thread(mddev->thread); 4606 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ 4607 4608 md_new_event(mddev); 4609 sysfs_notify_dirent_safe(mddev->sysfs_state); 4610 sysfs_notify_dirent_safe(mddev->sysfs_action); 4611 sysfs_notify(&mddev->kobj, NULL, "degraded"); 4612 return 0; 4613 } 4614 EXPORT_SYMBOL_GPL(md_run); 4615 4616 static int do_md_run(mddev_t *mddev) 4617 { 4618 int err; 4619 4620 err = md_run(mddev); 4621 if (err) 4622 goto out; 4623 err = bitmap_load(mddev); 4624 if (err) { 4625 bitmap_destroy(mddev); 4626 goto out; 4627 } 4628 set_capacity(mddev->gendisk, mddev->array_sectors); 4629 revalidate_disk(mddev->gendisk); 4630 mddev->changed = 1; 4631 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); 4632 out: 4633 return err; 4634 } 4635 4636 static int restart_array(mddev_t *mddev) 4637 { 4638 struct gendisk *disk = mddev->gendisk; 4639 4640 /* Complain if it has no devices */ 4641 if (list_empty(&mddev->disks)) 4642 return -ENXIO; 4643 if (!mddev->pers) 4644 return -EINVAL; 4645 if (!mddev->ro) 4646 return -EBUSY; 4647 mddev->safemode = 0; 4648 mddev->ro = 0; 4649 set_disk_ro(disk, 0); 4650 printk(KERN_INFO "md: %s switched to read-write mode.\n", 4651 mdname(mddev)); 4652 /* Kick recovery or resync if necessary */ 4653 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); 4654 md_wakeup_thread(mddev->thread); 4655 md_wakeup_thread(mddev->sync_thread); 4656 sysfs_notify_dirent_safe(mddev->sysfs_state); 4657 return 0; 4658 } 4659 4660 /* similar to deny_write_access, but accounts for our holding a reference 4661 * to the file ourselves */ 4662 static int deny_bitmap_write_access(struct file * file) 4663 { 4664 struct inode *inode = file->f_mapping->host; 4665 4666 spin_lock(&inode->i_lock); 4667 if (atomic_read(&inode->i_writecount) > 1) { 4668 spin_unlock(&inode->i_lock); 4669 return -ETXTBSY; 4670 } 4671 atomic_set(&inode->i_writecount, -1); 4672 spin_unlock(&inode->i_lock); 4673 4674 return 0; 4675 } 4676 4677 void restore_bitmap_write_access(struct file *file) 4678 { 4679 struct inode *inode = file->f_mapping->host; 4680 4681 spin_lock(&inode->i_lock); 4682 atomic_set(&inode->i_writecount, 1); 4683 spin_unlock(&inode->i_lock); 4684 } 4685 4686 static void md_clean(mddev_t *mddev) 4687 { 4688 mddev->array_sectors = 0; 4689 mddev->external_size = 0; 4690 mddev->dev_sectors = 0; 4691 mddev->raid_disks = 0; 4692 mddev->recovery_cp = 0; 4693 mddev->resync_min = 0; 4694 mddev->resync_max = MaxSector; 4695 mddev->reshape_position = MaxSector; 4696 mddev->external = 0; 4697 mddev->persistent = 0; 4698 mddev->level = LEVEL_NONE; 4699 mddev->clevel[0] = 0; 4700 mddev->flags = 0; 4701 mddev->ro = 0; 4702 mddev->metadata_type[0] = 0; 4703 mddev->chunk_sectors = 0; 4704 mddev->ctime = mddev->utime = 0; 4705 mddev->layout = 0; 4706 mddev->max_disks = 0; 4707 mddev->events = 0; 4708 mddev->can_decrease_events = 0; 4709 mddev->delta_disks = 0; 4710 mddev->new_level = LEVEL_NONE; 4711 mddev->new_layout = 0; 4712 mddev->new_chunk_sectors = 0; 4713 mddev->curr_resync = 0; 4714 mddev->resync_mismatches = 0; 4715 mddev->suspend_lo = mddev->suspend_hi = 0; 4716 mddev->sync_speed_min = mddev->sync_speed_max = 0; 4717 mddev->recovery = 0; 4718 mddev->in_sync = 0; 4719 mddev->changed = 0; 4720 mddev->degraded = 0; 4721 mddev->safemode = 0; 4722 mddev->bitmap_info.offset = 0; 4723 mddev->bitmap_info.default_offset = 0; 4724 mddev->bitmap_info.chunksize = 0; 4725 mddev->bitmap_info.daemon_sleep = 0; 4726 mddev->bitmap_info.max_write_behind = 0; 4727 mddev->plug = NULL; 4728 } 4729 4730 static void __md_stop_writes(mddev_t *mddev) 4731 { 4732 if (mddev->sync_thread) { 4733 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); 4734 set_bit(MD_RECOVERY_INTR, &mddev->recovery); 4735 reap_sync_thread(mddev); 4736 } 4737 4738 del_timer_sync(&mddev->safemode_timer); 4739 4740 bitmap_flush(mddev); 4741 md_super_wait(mddev); 4742 4743 if (!mddev->in_sync || mddev->flags) { 4744 /* mark array as shutdown cleanly */ 4745 mddev->in_sync = 1; 4746 md_update_sb(mddev, 1); 4747 } 4748 } 4749 4750 void md_stop_writes(mddev_t *mddev) 4751 { 4752 mddev_lock(mddev); 4753 __md_stop_writes(mddev); 4754 mddev_unlock(mddev); 4755 } 4756 EXPORT_SYMBOL_GPL(md_stop_writes); 4757 4758 void md_stop(mddev_t *mddev) 4759 { 4760 mddev->ready = 0; 4761 mddev->pers->stop(mddev); 4762 if (mddev->pers->sync_request && mddev->to_remove == NULL) 4763 mddev->to_remove = &md_redundancy_group; 4764 module_put(mddev->pers->owner); 4765 mddev->pers = NULL; 4766 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); 4767 } 4768 EXPORT_SYMBOL_GPL(md_stop); 4769 4770 static int md_set_readonly(mddev_t *mddev, int is_open) 4771 { 4772 int err = 0; 4773 mutex_lock(&mddev->open_mutex); 4774 if (atomic_read(&mddev->openers) > is_open) { 4775 printk("md: %s still in use.\n",mdname(mddev)); 4776 err = -EBUSY; 4777 goto out; 4778 } 4779 if (mddev->pers) { 4780 __md_stop_writes(mddev); 4781 4782 err = -ENXIO; 4783 if (mddev->ro==1) 4784 goto out; 4785 mddev->ro = 1; 4786 set_disk_ro(mddev->gendisk, 1); 4787 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); 4788 sysfs_notify_dirent_safe(mddev->sysfs_state); 4789 err = 0; 4790 } 4791 out: 4792 mutex_unlock(&mddev->open_mutex); 4793 return err; 4794 } 4795 4796 /* mode: 4797 * 0 - completely stop and dis-assemble array 4798 * 2 - stop but do not disassemble array 4799 */ 4800 static int do_md_stop(mddev_t * mddev, int mode, int is_open) 4801 { 4802 struct gendisk *disk = mddev->gendisk; 4803 mdk_rdev_t *rdev; 4804 4805 mutex_lock(&mddev->open_mutex); 4806 if (atomic_read(&mddev->openers) > is_open || 4807 mddev->sysfs_active) { 4808 printk("md: %s still in use.\n",mdname(mddev)); 4809 mutex_unlock(&mddev->open_mutex); 4810 return -EBUSY; 4811 } 4812 4813 if (mddev->pers) { 4814 if (mddev->ro) 4815 set_disk_ro(disk, 0); 4816 4817 __md_stop_writes(mddev); 4818 md_stop(mddev); 4819 mddev->queue->merge_bvec_fn = NULL; 4820 mddev->queue->unplug_fn = NULL; 4821 mddev->queue->backing_dev_info.congested_fn = NULL; 4822 4823 /* tell userspace to handle 'inactive' */ 4824 sysfs_notify_dirent_safe(mddev->sysfs_state); 4825 4826 list_for_each_entry(rdev, &mddev->disks, same_set) 4827 if (rdev->raid_disk >= 0) { 4828 char nm[20]; 4829 sprintf(nm, "rd%d", rdev->raid_disk); 4830 sysfs_remove_link(&mddev->kobj, nm); 4831 } 4832 4833 set_capacity(disk, 0); 4834 mutex_unlock(&mddev->open_mutex); 4835 mddev->changed = 1; 4836 revalidate_disk(disk); 4837 4838 if (mddev->ro) 4839 mddev->ro = 0; 4840 } else 4841 mutex_unlock(&mddev->open_mutex); 4842 /* 4843 * Free resources if final stop 4844 */ 4845 if (mode == 0) { 4846 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev)); 4847 4848 bitmap_destroy(mddev); 4849 if (mddev->bitmap_info.file) { 4850 restore_bitmap_write_access(mddev->bitmap_info.file); 4851 fput(mddev->bitmap_info.file); 4852 mddev->bitmap_info.file = NULL; 4853 } 4854 mddev->bitmap_info.offset = 0; 4855 4856 export_array(mddev); 4857 4858 md_clean(mddev); 4859 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); 4860 if (mddev->hold_active == UNTIL_STOP) 4861 mddev->hold_active = 0; 4862 } 4863 blk_integrity_unregister(disk); 4864 md_new_event(mddev); 4865 sysfs_notify_dirent_safe(mddev->sysfs_state); 4866 return 0; 4867 } 4868 4869 #ifndef MODULE 4870 static void autorun_array(mddev_t *mddev) 4871 { 4872 mdk_rdev_t *rdev; 4873 int err; 4874 4875 if (list_empty(&mddev->disks)) 4876 return; 4877 4878 printk(KERN_INFO "md: running: "); 4879 4880 list_for_each_entry(rdev, &mddev->disks, same_set) { 4881 char b[BDEVNAME_SIZE]; 4882 printk("<%s>", bdevname(rdev->bdev,b)); 4883 } 4884 printk("\n"); 4885 4886 err = do_md_run(mddev); 4887 if (err) { 4888 printk(KERN_WARNING "md: do_md_run() returned %d\n", err); 4889 do_md_stop(mddev, 0, 0); 4890 } 4891 } 4892 4893 /* 4894 * lets try to run arrays based on all disks that have arrived 4895 * until now. (those are in pending_raid_disks) 4896 * 4897 * the method: pick the first pending disk, collect all disks with 4898 * the same UUID, remove all from the pending list and put them into 4899 * the 'same_array' list. Then order this list based on superblock 4900 * update time (freshest comes first), kick out 'old' disks and 4901 * compare superblocks. If everything's fine then run it. 4902 * 4903 * If "unit" is allocated, then bump its reference count 4904 */ 4905 static void autorun_devices(int part) 4906 { 4907 mdk_rdev_t *rdev0, *rdev, *tmp; 4908 mddev_t *mddev; 4909 char b[BDEVNAME_SIZE]; 4910 4911 printk(KERN_INFO "md: autorun ...\n"); 4912 while (!list_empty(&pending_raid_disks)) { 4913 int unit; 4914 dev_t dev; 4915 LIST_HEAD(candidates); 4916 rdev0 = list_entry(pending_raid_disks.next, 4917 mdk_rdev_t, same_set); 4918 4919 printk(KERN_INFO "md: considering %s ...\n", 4920 bdevname(rdev0->bdev,b)); 4921 INIT_LIST_HEAD(&candidates); 4922 rdev_for_each_list(rdev, tmp, &pending_raid_disks) 4923 if (super_90_load(rdev, rdev0, 0) >= 0) { 4924 printk(KERN_INFO "md: adding %s ...\n", 4925 bdevname(rdev->bdev,b)); 4926 list_move(&rdev->same_set, &candidates); 4927 } 4928 /* 4929 * now we have a set of devices, with all of them having 4930 * mostly sane superblocks. It's time to allocate the 4931 * mddev. 4932 */ 4933 if (part) { 4934 dev = MKDEV(mdp_major, 4935 rdev0->preferred_minor << MdpMinorShift); 4936 unit = MINOR(dev) >> MdpMinorShift; 4937 } else { 4938 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor); 4939 unit = MINOR(dev); 4940 } 4941 if (rdev0->preferred_minor != unit) { 4942 printk(KERN_INFO "md: unit number in %s is bad: %d\n", 4943 bdevname(rdev0->bdev, b), rdev0->preferred_minor); 4944 break; 4945 } 4946 4947 md_probe(dev, NULL, NULL); 4948 mddev = mddev_find(dev); 4949 if (!mddev || !mddev->gendisk) { 4950 if (mddev) 4951 mddev_put(mddev); 4952 printk(KERN_ERR 4953 "md: cannot allocate memory for md drive.\n"); 4954 break; 4955 } 4956 if (mddev_lock(mddev)) 4957 printk(KERN_WARNING "md: %s locked, cannot run\n", 4958 mdname(mddev)); 4959 else if (mddev->raid_disks || mddev->major_version 4960 || !list_empty(&mddev->disks)) { 4961 printk(KERN_WARNING 4962 "md: %s already running, cannot run %s\n", 4963 mdname(mddev), bdevname(rdev0->bdev,b)); 4964 mddev_unlock(mddev); 4965 } else { 4966 printk(KERN_INFO "md: created %s\n", mdname(mddev)); 4967 mddev->persistent = 1; 4968 rdev_for_each_list(rdev, tmp, &candidates) { 4969 list_del_init(&rdev->same_set); 4970 if (bind_rdev_to_array(rdev, mddev)) 4971 export_rdev(rdev); 4972 } 4973 autorun_array(mddev); 4974 mddev_unlock(mddev); 4975 } 4976 /* on success, candidates will be empty, on error 4977 * it won't... 4978 */ 4979 rdev_for_each_list(rdev, tmp, &candidates) { 4980 list_del_init(&rdev->same_set); 4981 export_rdev(rdev); 4982 } 4983 mddev_put(mddev); 4984 } 4985 printk(KERN_INFO "md: ... autorun DONE.\n"); 4986 } 4987 #endif /* !MODULE */ 4988 4989 static int get_version(void __user * arg) 4990 { 4991 mdu_version_t ver; 4992 4993 ver.major = MD_MAJOR_VERSION; 4994 ver.minor = MD_MINOR_VERSION; 4995 ver.patchlevel = MD_PATCHLEVEL_VERSION; 4996 4997 if (copy_to_user(arg, &ver, sizeof(ver))) 4998 return -EFAULT; 4999 5000 return 0; 5001 } 5002 5003 static int get_array_info(mddev_t * mddev, void __user * arg) 5004 { 5005 mdu_array_info_t info; 5006 int nr,working,insync,failed,spare; 5007 mdk_rdev_t *rdev; 5008 5009 nr=working=insync=failed=spare=0; 5010 list_for_each_entry(rdev, &mddev->disks, same_set) { 5011 nr++; 5012 if (test_bit(Faulty, &rdev->flags)) 5013 failed++; 5014 else { 5015 working++; 5016 if (test_bit(In_sync, &rdev->flags)) 5017 insync++; 5018 else 5019 spare++; 5020 } 5021 } 5022 5023 info.major_version = mddev->major_version; 5024 info.minor_version = mddev->minor_version; 5025 info.patch_version = MD_PATCHLEVEL_VERSION; 5026 info.ctime = mddev->ctime; 5027 info.level = mddev->level; 5028 info.size = mddev->dev_sectors / 2; 5029 if (info.size != mddev->dev_sectors / 2) /* overflow */ 5030 info.size = -1; 5031 info.nr_disks = nr; 5032 info.raid_disks = mddev->raid_disks; 5033 info.md_minor = mddev->md_minor; 5034 info.not_persistent= !mddev->persistent; 5035 5036 info.utime = mddev->utime; 5037 info.state = 0; 5038 if (mddev->in_sync) 5039 info.state = (1<<MD_SB_CLEAN); 5040 if (mddev->bitmap && mddev->bitmap_info.offset) 5041 info.state = (1<<MD_SB_BITMAP_PRESENT); 5042 info.active_disks = insync; 5043 info.working_disks = working; 5044 info.failed_disks = failed; 5045 info.spare_disks = spare; 5046 5047 info.layout = mddev->layout; 5048 info.chunk_size = mddev->chunk_sectors << 9; 5049 5050 if (copy_to_user(arg, &info, sizeof(info))) 5051 return -EFAULT; 5052 5053 return 0; 5054 } 5055 5056 static int get_bitmap_file(mddev_t * mddev, void __user * arg) 5057 { 5058 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */ 5059 char *ptr, *buf = NULL; 5060 int err = -ENOMEM; 5061 5062 if (md_allow_write(mddev)) 5063 file = kmalloc(sizeof(*file), GFP_NOIO); 5064 else 5065 file = kmalloc(sizeof(*file), GFP_KERNEL); 5066 5067 if (!file) 5068 goto out; 5069 5070 /* bitmap disabled, zero the first byte and copy out */ 5071 if (!mddev->bitmap || !mddev->bitmap->file) { 5072 file->pathname[0] = '\0'; 5073 goto copy_out; 5074 } 5075 5076 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL); 5077 if (!buf) 5078 goto out; 5079 5080 ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname)); 5081 if (IS_ERR(ptr)) 5082 goto out; 5083 5084 strcpy(file->pathname, ptr); 5085 5086 copy_out: 5087 err = 0; 5088 if (copy_to_user(arg, file, sizeof(*file))) 5089 err = -EFAULT; 5090 out: 5091 kfree(buf); 5092 kfree(file); 5093 return err; 5094 } 5095 5096 static int get_disk_info(mddev_t * mddev, void __user * arg) 5097 { 5098 mdu_disk_info_t info; 5099 mdk_rdev_t *rdev; 5100 5101 if (copy_from_user(&info, arg, sizeof(info))) 5102 return -EFAULT; 5103 5104 rdev = find_rdev_nr(mddev, info.number); 5105 if (rdev) { 5106 info.major = MAJOR(rdev->bdev->bd_dev); 5107 info.minor = MINOR(rdev->bdev->bd_dev); 5108 info.raid_disk = rdev->raid_disk; 5109 info.state = 0; 5110 if (test_bit(Faulty, &rdev->flags)) 5111 info.state |= (1<<MD_DISK_FAULTY); 5112 else if (test_bit(In_sync, &rdev->flags)) { 5113 info.state |= (1<<MD_DISK_ACTIVE); 5114 info.state |= (1<<MD_DISK_SYNC); 5115 } 5116 if (test_bit(WriteMostly, &rdev->flags)) 5117 info.state |= (1<<MD_DISK_WRITEMOSTLY); 5118 } else { 5119 info.major = info.minor = 0; 5120 info.raid_disk = -1; 5121 info.state = (1<<MD_DISK_REMOVED); 5122 } 5123 5124 if (copy_to_user(arg, &info, sizeof(info))) 5125 return -EFAULT; 5126 5127 return 0; 5128 } 5129 5130 static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info) 5131 { 5132 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; 5133 mdk_rdev_t *rdev; 5134 dev_t dev = MKDEV(info->major,info->minor); 5135 5136 if (info->major != MAJOR(dev) || info->minor != MINOR(dev)) 5137 return -EOVERFLOW; 5138 5139 if (!mddev->raid_disks) { 5140 int err; 5141 /* expecting a device which has a superblock */ 5142 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version); 5143 if (IS_ERR(rdev)) { 5144 printk(KERN_WARNING 5145 "md: md_import_device returned %ld\n", 5146 PTR_ERR(rdev)); 5147 return PTR_ERR(rdev); 5148 } 5149 if (!list_empty(&mddev->disks)) { 5150 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next, 5151 mdk_rdev_t, same_set); 5152 err = super_types[mddev->major_version] 5153 .load_super(rdev, rdev0, mddev->minor_version); 5154 if (err < 0) { 5155 printk(KERN_WARNING 5156 "md: %s has different UUID to %s\n", 5157 bdevname(rdev->bdev,b), 5158 bdevname(rdev0->bdev,b2)); 5159 export_rdev(rdev); 5160 return -EINVAL; 5161 } 5162 } 5163 err = bind_rdev_to_array(rdev, mddev); 5164 if (err) 5165 export_rdev(rdev); 5166 return err; 5167 } 5168 5169 /* 5170 * add_new_disk can be used once the array is assembled 5171 * to add "hot spares". They must already have a superblock 5172 * written 5173 */ 5174 if (mddev->pers) { 5175 int err; 5176 if (!mddev->pers->hot_add_disk) { 5177 printk(KERN_WARNING 5178 "%s: personality does not support diskops!\n", 5179 mdname(mddev)); 5180 return -EINVAL; 5181 } 5182 if (mddev->persistent) 5183 rdev = md_import_device(dev, mddev->major_version, 5184 mddev->minor_version); 5185 else 5186 rdev = md_import_device(dev, -1, -1); 5187 if (IS_ERR(rdev)) { 5188 printk(KERN_WARNING 5189 "md: md_import_device returned %ld\n", 5190 PTR_ERR(rdev)); 5191 return PTR_ERR(rdev); 5192 } 5193 /* set saved_raid_disk if appropriate */ 5194 if (!mddev->persistent) { 5195 if (info->state & (1<<MD_DISK_SYNC) && 5196 info->raid_disk < mddev->raid_disks) { 5197 rdev->raid_disk = info->raid_disk; 5198 set_bit(In_sync, &rdev->flags); 5199 } else 5200 rdev->raid_disk = -1; 5201 } else 5202 super_types[mddev->major_version]. 5203 validate_super(mddev, rdev); 5204 if (test_bit(In_sync, &rdev->flags)) 5205 rdev->saved_raid_disk = rdev->raid_disk; 5206 else 5207 rdev->saved_raid_disk = -1; 5208 5209 clear_bit(In_sync, &rdev->flags); /* just to be sure */ 5210 if (info->state & (1<<MD_DISK_WRITEMOSTLY)) 5211 set_bit(WriteMostly, &rdev->flags); 5212 else 5213 clear_bit(WriteMostly, &rdev->flags); 5214 5215 rdev->raid_disk = -1; 5216 err = bind_rdev_to_array(rdev, mddev); 5217 if (!err && !mddev->pers->hot_remove_disk) { 5218 /* If there is hot_add_disk but no hot_remove_disk 5219 * then added disks for geometry changes, 5220 * and should be added immediately. 5221 */ 5222 super_types[mddev->major_version]. 5223 validate_super(mddev, rdev); 5224 err = mddev->pers->hot_add_disk(mddev, rdev); 5225 if (err) 5226 unbind_rdev_from_array(rdev); 5227 } 5228 if (err) 5229 export_rdev(rdev); 5230 else 5231 sysfs_notify_dirent_safe(rdev->sysfs_state); 5232 5233 md_update_sb(mddev, 1); 5234 if (mddev->degraded) 5235 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); 5236 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); 5237 md_wakeup_thread(mddev->thread); 5238 return err; 5239 } 5240 5241 /* otherwise, add_new_disk is only allowed 5242 * for major_version==0 superblocks 5243 */ 5244 if (mddev->major_version != 0) { 5245 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n", 5246 mdname(mddev)); 5247 return -EINVAL; 5248 } 5249 5250 if (!(info->state & (1<<MD_DISK_FAULTY))) { 5251 int err; 5252 rdev = md_import_device(dev, -1, 0); 5253 if (IS_ERR(rdev)) { 5254 printk(KERN_WARNING 5255 "md: error, md_import_device() returned %ld\n", 5256 PTR_ERR(rdev)); 5257 return PTR_ERR(rdev); 5258 } 5259 rdev->desc_nr = info->number; 5260 if (info->raid_disk < mddev->raid_disks) 5261 rdev->raid_disk = info->raid_disk; 5262 else 5263 rdev->raid_disk = -1; 5264 5265 if (rdev->raid_disk < mddev->raid_disks) 5266 if (info->state & (1<<MD_DISK_SYNC)) 5267 set_bit(In_sync, &rdev->flags); 5268 5269 if (info->state & (1<<MD_DISK_WRITEMOSTLY)) 5270 set_bit(WriteMostly, &rdev->flags); 5271 5272 if (!mddev->persistent) { 5273 printk(KERN_INFO "md: nonpersistent superblock ...\n"); 5274 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512; 5275 } else 5276 rdev->sb_start = calc_dev_sboffset(rdev); 5277 rdev->sectors = rdev->sb_start; 5278 5279 err = bind_rdev_to_array(rdev, mddev); 5280 if (err) { 5281 export_rdev(rdev); 5282 return err; 5283 } 5284 } 5285 5286 return 0; 5287 } 5288 5289 static int hot_remove_disk(mddev_t * mddev, dev_t dev) 5290 { 5291 char b[BDEVNAME_SIZE]; 5292 mdk_rdev_t *rdev; 5293 5294 rdev = find_rdev(mddev, dev); 5295 if (!rdev) 5296 return -ENXIO; 5297 5298 if (rdev->raid_disk >= 0) 5299 goto busy; 5300 5301 kick_rdev_from_array(rdev); 5302 md_update_sb(mddev, 1); 5303 md_new_event(mddev); 5304 5305 return 0; 5306 busy: 5307 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n", 5308 bdevname(rdev->bdev,b), mdname(mddev)); 5309 return -EBUSY; 5310 } 5311 5312 static int hot_add_disk(mddev_t * mddev, dev_t dev) 5313 { 5314 char b[BDEVNAME_SIZE]; 5315 int err; 5316 mdk_rdev_t *rdev; 5317 5318 if (!mddev->pers) 5319 return -ENODEV; 5320 5321 if (mddev->major_version != 0) { 5322 printk(KERN_WARNING "%s: HOT_ADD may only be used with" 5323 " version-0 superblocks.\n", 5324 mdname(mddev)); 5325 return -EINVAL; 5326 } 5327 if (!mddev->pers->hot_add_disk) { 5328 printk(KERN_WARNING 5329 "%s: personality does not support diskops!\n", 5330 mdname(mddev)); 5331 return -EINVAL; 5332 } 5333 5334 rdev = md_import_device(dev, -1, 0); 5335 if (IS_ERR(rdev)) { 5336 printk(KERN_WARNING 5337 "md: error, md_import_device() returned %ld\n", 5338 PTR_ERR(rdev)); 5339 return -EINVAL; 5340 } 5341 5342 if (mddev->persistent) 5343 rdev->sb_start = calc_dev_sboffset(rdev); 5344 else 5345 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512; 5346 5347 rdev->sectors = rdev->sb_start; 5348 5349 if (test_bit(Faulty, &rdev->flags)) { 5350 printk(KERN_WARNING 5351 "md: can not hot-add faulty %s disk to %s!\n", 5352 bdevname(rdev->bdev,b), mdname(mddev)); 5353 err = -EINVAL; 5354 goto abort_export; 5355 } 5356 clear_bit(In_sync, &rdev->flags); 5357 rdev->desc_nr = -1; 5358 rdev->saved_raid_disk = -1; 5359 err = bind_rdev_to_array(rdev, mddev); 5360 if (err) 5361 goto abort_export; 5362 5363 /* 5364 * The rest should better be atomic, we can have disk failures 5365 * noticed in interrupt contexts ... 5366 */ 5367 5368 rdev->raid_disk = -1; 5369 5370 md_update_sb(mddev, 1); 5371 5372 /* 5373 * Kick recovery, maybe this spare has to be added to the 5374 * array immediately. 5375 */ 5376 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); 5377 md_wakeup_thread(mddev->thread); 5378 md_new_event(mddev); 5379 return 0; 5380 5381 abort_export: 5382 export_rdev(rdev); 5383 return err; 5384 } 5385 5386 static int set_bitmap_file(mddev_t *mddev, int fd) 5387 { 5388 int err; 5389 5390 if (mddev->pers) { 5391 if (!mddev->pers->quiesce) 5392 return -EBUSY; 5393 if (mddev->recovery || mddev->sync_thread) 5394 return -EBUSY; 5395 /* we should be able to change the bitmap.. */ 5396 } 5397 5398 5399 if (fd >= 0) { 5400 if (mddev->bitmap) 5401 return -EEXIST; /* cannot add when bitmap is present */ 5402 mddev->bitmap_info.file = fget(fd); 5403 5404 if (mddev->bitmap_info.file == NULL) { 5405 printk(KERN_ERR "%s: error: failed to get bitmap file\n", 5406 mdname(mddev)); 5407 return -EBADF; 5408 } 5409 5410 err = deny_bitmap_write_access(mddev->bitmap_info.file); 5411 if (err) { 5412 printk(KERN_ERR "%s: error: bitmap file is already in use\n", 5413 mdname(mddev)); 5414 fput(mddev->bitmap_info.file); 5415 mddev->bitmap_info.file = NULL; 5416 return err; 5417 } 5418 mddev->bitmap_info.offset = 0; /* file overrides offset */ 5419 } else if (mddev->bitmap == NULL) 5420 return -ENOENT; /* cannot remove what isn't there */ 5421 err = 0; 5422 if (mddev->pers) { 5423 mddev->pers->quiesce(mddev, 1); 5424 if (fd >= 0) { 5425 err = bitmap_create(mddev); 5426 if (!err) 5427 err = bitmap_load(mddev); 5428 } 5429 if (fd < 0 || err) { 5430 bitmap_destroy(mddev); 5431 fd = -1; /* make sure to put the file */ 5432 } 5433 mddev->pers->quiesce(mddev, 0); 5434 } 5435 if (fd < 0) { 5436 if (mddev->bitmap_info.file) { 5437 restore_bitmap_write_access(mddev->bitmap_info.file); 5438 fput(mddev->bitmap_info.file); 5439 } 5440 mddev->bitmap_info.file = NULL; 5441 } 5442 5443 return err; 5444 } 5445 5446 /* 5447 * set_array_info is used two different ways 5448 * The original usage is when creating a new array. 5449 * In this usage, raid_disks is > 0 and it together with 5450 * level, size, not_persistent,layout,chunksize determine the 5451 * shape of the array. 5452 * This will always create an array with a type-0.90.0 superblock. 5453 * The newer usage is when assembling an array. 5454 * In this case raid_disks will be 0, and the major_version field is 5455 * use to determine which style super-blocks are to be found on the devices. 5456 * The minor and patch _version numbers are also kept incase the 5457 * super_block handler wishes to interpret them. 5458 */ 5459 static int set_array_info(mddev_t * mddev, mdu_array_info_t *info) 5460 { 5461 5462 if (info->raid_disks == 0) { 5463 /* just setting version number for superblock loading */ 5464 if (info->major_version < 0 || 5465 info->major_version >= ARRAY_SIZE(super_types) || 5466 super_types[info->major_version].name == NULL) { 5467 /* maybe try to auto-load a module? */ 5468 printk(KERN_INFO 5469 "md: superblock version %d not known\n", 5470 info->major_version); 5471 return -EINVAL; 5472 } 5473 mddev->major_version = info->major_version; 5474 mddev->minor_version = info->minor_version; 5475 mddev->patch_version = info->patch_version; 5476 mddev->persistent = !info->not_persistent; 5477 /* ensure mddev_put doesn't delete this now that there 5478 * is some minimal configuration. 5479 */ 5480 mddev->ctime = get_seconds(); 5481 return 0; 5482 } 5483 mddev->major_version = MD_MAJOR_VERSION; 5484 mddev->minor_version = MD_MINOR_VERSION; 5485 mddev->patch_version = MD_PATCHLEVEL_VERSION; 5486 mddev->ctime = get_seconds(); 5487 5488 mddev->level = info->level; 5489 mddev->clevel[0] = 0; 5490 mddev->dev_sectors = 2 * (sector_t)info->size; 5491 mddev->raid_disks = info->raid_disks; 5492 /* don't set md_minor, it is determined by which /dev/md* was 5493 * openned 5494 */ 5495 if (info->state & (1<<MD_SB_CLEAN)) 5496 mddev->recovery_cp = MaxSector; 5497 else 5498 mddev->recovery_cp = 0; 5499 mddev->persistent = ! info->not_persistent; 5500 mddev->external = 0; 5501 5502 mddev->layout = info->layout; 5503 mddev->chunk_sectors = info->chunk_size >> 9; 5504 5505 mddev->max_disks = MD_SB_DISKS; 5506 5507 if (mddev->persistent) 5508 mddev->flags = 0; 5509 set_bit(MD_CHANGE_DEVS, &mddev->flags); 5510 5511 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; 5512 mddev->bitmap_info.offset = 0; 5513 5514 mddev->reshape_position = MaxSector; 5515 5516 /* 5517 * Generate a 128 bit UUID 5518 */ 5519 get_random_bytes(mddev->uuid, 16); 5520 5521 mddev->new_level = mddev->level; 5522 mddev->new_chunk_sectors = mddev->chunk_sectors; 5523 mddev->new_layout = mddev->layout; 5524 mddev->delta_disks = 0; 5525 5526 return 0; 5527 } 5528 5529 void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors) 5530 { 5531 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__); 5532 5533 if (mddev->external_size) 5534 return; 5535 5536 mddev->array_sectors = array_sectors; 5537 } 5538 EXPORT_SYMBOL(md_set_array_sectors); 5539 5540 static int update_size(mddev_t *mddev, sector_t num_sectors) 5541 { 5542 mdk_rdev_t *rdev; 5543 int rv; 5544 int fit = (num_sectors == 0); 5545 5546 if (mddev->pers->resize == NULL) 5547 return -EINVAL; 5548 /* The "num_sectors" is the number of sectors of each device that 5549 * is used. This can only make sense for arrays with redundancy. 5550 * linear and raid0 always use whatever space is available. We can only 5551 * consider changing this number if no resync or reconstruction is 5552 * happening, and if the new size is acceptable. It must fit before the 5553 * sb_start or, if that is <data_offset, it must fit before the size 5554 * of each device. If num_sectors is zero, we find the largest size 5555 * that fits. 5556 */ 5557 if (mddev->sync_thread) 5558 return -EBUSY; 5559 if (mddev->bitmap) 5560 /* Sorry, cannot grow a bitmap yet, just remove it, 5561 * grow, and re-add. 5562 */ 5563 return -EBUSY; 5564 list_for_each_entry(rdev, &mddev->disks, same_set) { 5565 sector_t avail = rdev->sectors; 5566 5567 if (fit && (num_sectors == 0 || num_sectors > avail)) 5568 num_sectors = avail; 5569 if (avail < num_sectors) 5570 return -ENOSPC; 5571 } 5572 rv = mddev->pers->resize(mddev, num_sectors); 5573 if (!rv) 5574 revalidate_disk(mddev->gendisk); 5575 return rv; 5576 } 5577 5578 static int update_raid_disks(mddev_t *mddev, int raid_disks) 5579 { 5580 int rv; 5581 /* change the number of raid disks */ 5582 if (mddev->pers->check_reshape == NULL) 5583 return -EINVAL; 5584 if (raid_disks <= 0 || 5585 (mddev->max_disks && raid_disks >= mddev->max_disks)) 5586 return -EINVAL; 5587 if (mddev->sync_thread || mddev->reshape_position != MaxSector) 5588 return -EBUSY; 5589 mddev->delta_disks = raid_disks - mddev->raid_disks; 5590 5591 rv = mddev->pers->check_reshape(mddev); 5592 if (rv < 0) 5593 mddev->delta_disks = 0; 5594 return rv; 5595 } 5596 5597 5598 /* 5599 * update_array_info is used to change the configuration of an 5600 * on-line array. 5601 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size 5602 * fields in the info are checked against the array. 5603 * Any differences that cannot be handled will cause an error. 5604 * Normally, only one change can be managed at a time. 5605 */ 5606 static int update_array_info(mddev_t *mddev, mdu_array_info_t *info) 5607 { 5608 int rv = 0; 5609 int cnt = 0; 5610 int state = 0; 5611 5612 /* calculate expected state,ignoring low bits */ 5613 if (mddev->bitmap && mddev->bitmap_info.offset) 5614 state |= (1 << MD_SB_BITMAP_PRESENT); 5615 5616 if (mddev->major_version != info->major_version || 5617 mddev->minor_version != info->minor_version || 5618 /* mddev->patch_version != info->patch_version || */ 5619 mddev->ctime != info->ctime || 5620 mddev->level != info->level || 5621 /* mddev->layout != info->layout || */ 5622 !mddev->persistent != info->not_persistent|| 5623 mddev->chunk_sectors != info->chunk_size >> 9 || 5624 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */ 5625 ((state^info->state) & 0xfffffe00) 5626 ) 5627 return -EINVAL; 5628 /* Check there is only one change */ 5629 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) 5630 cnt++; 5631 if (mddev->raid_disks != info->raid_disks) 5632 cnt++; 5633 if (mddev->layout != info->layout) 5634 cnt++; 5635 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) 5636 cnt++; 5637 if (cnt == 0) 5638 return 0; 5639 if (cnt > 1) 5640 return -EINVAL; 5641 5642 if (mddev->layout != info->layout) { 5643 /* Change layout 5644 * we don't need to do anything at the md level, the 5645 * personality will take care of it all. 5646 */ 5647 if (mddev->pers->check_reshape == NULL) 5648 return -EINVAL; 5649 else { 5650 mddev->new_layout = info->layout; 5651 rv = mddev->pers->check_reshape(mddev); 5652 if (rv) 5653 mddev->new_layout = mddev->layout; 5654 return rv; 5655 } 5656 } 5657 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) 5658 rv = update_size(mddev, (sector_t)info->size * 2); 5659 5660 if (mddev->raid_disks != info->raid_disks) 5661 rv = update_raid_disks(mddev, info->raid_disks); 5662 5663 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) { 5664 if (mddev->pers->quiesce == NULL) 5665 return -EINVAL; 5666 if (mddev->recovery || mddev->sync_thread) 5667 return -EBUSY; 5668 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) { 5669 /* add the bitmap */ 5670 if (mddev->bitmap) 5671 return -EEXIST; 5672 if (mddev->bitmap_info.default_offset == 0) 5673 return -EINVAL; 5674 mddev->bitmap_info.offset = 5675 mddev->bitmap_info.default_offset; 5676 mddev->pers->quiesce(mddev, 1); 5677 rv = bitmap_create(mddev); 5678 if (!rv) 5679 rv = bitmap_load(mddev); 5680 if (rv) 5681 bitmap_destroy(mddev); 5682 mddev->pers->quiesce(mddev, 0); 5683 } else { 5684 /* remove the bitmap */ 5685 if (!mddev->bitmap) 5686 return -ENOENT; 5687 if (mddev->bitmap->file) 5688 return -EINVAL; 5689 mddev->pers->quiesce(mddev, 1); 5690 bitmap_destroy(mddev); 5691 mddev->pers->quiesce(mddev, 0); 5692 mddev->bitmap_info.offset = 0; 5693 } 5694 } 5695 md_update_sb(mddev, 1); 5696 return rv; 5697 } 5698 5699 static int set_disk_faulty(mddev_t *mddev, dev_t dev) 5700 { 5701 mdk_rdev_t *rdev; 5702 5703 if (mddev->pers == NULL) 5704 return -ENODEV; 5705 5706 rdev = find_rdev(mddev, dev); 5707 if (!rdev) 5708 return -ENODEV; 5709 5710 md_error(mddev, rdev); 5711 return 0; 5712 } 5713 5714 /* 5715 * We have a problem here : there is no easy way to give a CHS 5716 * virtual geometry. We currently pretend that we have a 2 heads 5717 * 4 sectors (with a BIG number of cylinders...). This drives 5718 * dosfs just mad... ;-) 5719 */ 5720 static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo) 5721 { 5722 mddev_t *mddev = bdev->bd_disk->private_data; 5723 5724 geo->heads = 2; 5725 geo->sectors = 4; 5726 geo->cylinders = mddev->array_sectors / 8; 5727 return 0; 5728 } 5729 5730 static int md_ioctl(struct block_device *bdev, fmode_t mode, 5731 unsigned int cmd, unsigned long arg) 5732 { 5733 int err = 0; 5734 void __user *argp = (void __user *)arg; 5735 mddev_t *mddev = NULL; 5736 int ro; 5737 5738 if (!capable(CAP_SYS_ADMIN)) 5739 return -EACCES; 5740 5741 /* 5742 * Commands dealing with the RAID driver but not any 5743 * particular array: 5744 */ 5745 switch (cmd) 5746 { 5747 case RAID_VERSION: 5748 err = get_version(argp); 5749 goto done; 5750 5751 case PRINT_RAID_DEBUG: 5752 err = 0; 5753 md_print_devices(); 5754 goto done; 5755 5756 #ifndef MODULE 5757 case RAID_AUTORUN: 5758 err = 0; 5759 autostart_arrays(arg); 5760 goto done; 5761 #endif 5762 default:; 5763 } 5764 5765 /* 5766 * Commands creating/starting a new array: 5767 */ 5768 5769 mddev = bdev->bd_disk->private_data; 5770 5771 if (!mddev) { 5772 BUG(); 5773 goto abort; 5774 } 5775 5776 err = mddev_lock(mddev); 5777 if (err) { 5778 printk(KERN_INFO 5779 "md: ioctl lock interrupted, reason %d, cmd %d\n", 5780 err, cmd); 5781 goto abort; 5782 } 5783 5784 switch (cmd) 5785 { 5786 case SET_ARRAY_INFO: 5787 { 5788 mdu_array_info_t info; 5789 if (!arg) 5790 memset(&info, 0, sizeof(info)); 5791 else if (copy_from_user(&info, argp, sizeof(info))) { 5792 err = -EFAULT; 5793 goto abort_unlock; 5794 } 5795 if (mddev->pers) { 5796 err = update_array_info(mddev, &info); 5797 if (err) { 5798 printk(KERN_WARNING "md: couldn't update" 5799 " array info. %d\n", err); 5800 goto abort_unlock; 5801 } 5802 goto done_unlock; 5803 } 5804 if (!list_empty(&mddev->disks)) { 5805 printk(KERN_WARNING 5806 "md: array %s already has disks!\n", 5807 mdname(mddev)); 5808 err = -EBUSY; 5809 goto abort_unlock; 5810 } 5811 if (mddev->raid_disks) { 5812 printk(KERN_WARNING 5813 "md: array %s already initialised!\n", 5814 mdname(mddev)); 5815 err = -EBUSY; 5816 goto abort_unlock; 5817 } 5818 err = set_array_info(mddev, &info); 5819 if (err) { 5820 printk(KERN_WARNING "md: couldn't set" 5821 " array info. %d\n", err); 5822 goto abort_unlock; 5823 } 5824 } 5825 goto done_unlock; 5826 5827 default:; 5828 } 5829 5830 /* 5831 * Commands querying/configuring an existing array: 5832 */ 5833 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY, 5834 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */ 5835 if ((!mddev->raid_disks && !mddev->external) 5836 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY 5837 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE 5838 && cmd != GET_BITMAP_FILE) { 5839 err = -ENODEV; 5840 goto abort_unlock; 5841 } 5842 5843 /* 5844 * Commands even a read-only array can execute: 5845 */ 5846 switch (cmd) 5847 { 5848 case GET_ARRAY_INFO: 5849 err = get_array_info(mddev, argp); 5850 goto done_unlock; 5851 5852 case GET_BITMAP_FILE: 5853 err = get_bitmap_file(mddev, argp); 5854 goto done_unlock; 5855 5856 case GET_DISK_INFO: 5857 err = get_disk_info(mddev, argp); 5858 goto done_unlock; 5859 5860 case RESTART_ARRAY_RW: 5861 err = restart_array(mddev); 5862 goto done_unlock; 5863 5864 case STOP_ARRAY: 5865 err = do_md_stop(mddev, 0, 1); 5866 goto done_unlock; 5867 5868 case STOP_ARRAY_RO: 5869 err = md_set_readonly(mddev, 1); 5870 goto done_unlock; 5871 5872 case BLKROSET: 5873 if (get_user(ro, (int __user *)(arg))) { 5874 err = -EFAULT; 5875 goto done_unlock; 5876 } 5877 err = -EINVAL; 5878 5879 /* if the bdev is going readonly the value of mddev->ro 5880 * does not matter, no writes are coming 5881 */ 5882 if (ro) 5883 goto done_unlock; 5884 5885 /* are we are already prepared for writes? */ 5886 if (mddev->ro != 1) 5887 goto done_unlock; 5888 5889 /* transitioning to readauto need only happen for 5890 * arrays that call md_write_start 5891 */ 5892 if (mddev->pers) { 5893 err = restart_array(mddev); 5894 if (err == 0) { 5895 mddev->ro = 2; 5896 set_disk_ro(mddev->gendisk, 0); 5897 } 5898 } 5899 goto done_unlock; 5900 } 5901 5902 /* 5903 * The remaining ioctls are changing the state of the 5904 * superblock, so we do not allow them on read-only arrays. 5905 * However non-MD ioctls (e.g. get-size) will still come through 5906 * here and hit the 'default' below, so only disallow 5907 * 'md' ioctls, and switch to rw mode if started auto-readonly. 5908 */ 5909 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) { 5910 if (mddev->ro == 2) { 5911 mddev->ro = 0; 5912 sysfs_notify_dirent_safe(mddev->sysfs_state); 5913 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); 5914 md_wakeup_thread(mddev->thread); 5915 } else { 5916 err = -EROFS; 5917 goto abort_unlock; 5918 } 5919 } 5920 5921 switch (cmd) 5922 { 5923 case ADD_NEW_DISK: 5924 { 5925 mdu_disk_info_t info; 5926 if (copy_from_user(&info, argp, sizeof(info))) 5927 err = -EFAULT; 5928 else 5929 err = add_new_disk(mddev, &info); 5930 goto done_unlock; 5931 } 5932 5933 case HOT_REMOVE_DISK: 5934 err = hot_remove_disk(mddev, new_decode_dev(arg)); 5935 goto done_unlock; 5936 5937 case HOT_ADD_DISK: 5938 err = hot_add_disk(mddev, new_decode_dev(arg)); 5939 goto done_unlock; 5940 5941 case SET_DISK_FAULTY: 5942 err = set_disk_faulty(mddev, new_decode_dev(arg)); 5943 goto done_unlock; 5944 5945 case RUN_ARRAY: 5946 err = do_md_run(mddev); 5947 goto done_unlock; 5948 5949 case SET_BITMAP_FILE: 5950 err = set_bitmap_file(mddev, (int)arg); 5951 goto done_unlock; 5952 5953 default: 5954 err = -EINVAL; 5955 goto abort_unlock; 5956 } 5957 5958 done_unlock: 5959 abort_unlock: 5960 if (mddev->hold_active == UNTIL_IOCTL && 5961 err != -EINVAL) 5962 mddev->hold_active = 0; 5963 mddev_unlock(mddev); 5964 5965 return err; 5966 done: 5967 if (err) 5968 MD_BUG(); 5969 abort: 5970 return err; 5971 } 5972 #ifdef CONFIG_COMPAT 5973 static int md_compat_ioctl(struct block_device *bdev, fmode_t mode, 5974 unsigned int cmd, unsigned long arg) 5975 { 5976 switch (cmd) { 5977 case HOT_REMOVE_DISK: 5978 case HOT_ADD_DISK: 5979 case SET_DISK_FAULTY: 5980 case SET_BITMAP_FILE: 5981 /* These take in integer arg, do not convert */ 5982 break; 5983 default: 5984 arg = (unsigned long)compat_ptr(arg); 5985 break; 5986 } 5987 5988 return md_ioctl(bdev, mode, cmd, arg); 5989 } 5990 #endif /* CONFIG_COMPAT */ 5991 5992 static int md_open(struct block_device *bdev, fmode_t mode) 5993 { 5994 /* 5995 * Succeed if we can lock the mddev, which confirms that 5996 * it isn't being stopped right now. 5997 */ 5998 mddev_t *mddev = mddev_find(bdev->bd_dev); 5999 int err; 6000 6001 if (mddev->gendisk != bdev->bd_disk) { 6002 /* we are racing with mddev_put which is discarding this 6003 * bd_disk. 6004 */ 6005 mddev_put(mddev); 6006 /* Wait until bdev->bd_disk is definitely gone */ 6007 flush_workqueue(md_misc_wq); 6008 /* Then retry the open from the top */ 6009 return -ERESTARTSYS; 6010 } 6011 BUG_ON(mddev != bdev->bd_disk->private_data); 6012 6013 if ((err = mutex_lock_interruptible(&mddev->open_mutex))) 6014 goto out; 6015 6016 err = 0; 6017 atomic_inc(&mddev->openers); 6018 mutex_unlock(&mddev->open_mutex); 6019 6020 check_disk_change(bdev); 6021 out: 6022 return err; 6023 } 6024 6025 static int md_release(struct gendisk *disk, fmode_t mode) 6026 { 6027 mddev_t *mddev = disk->private_data; 6028 6029 BUG_ON(!mddev); 6030 atomic_dec(&mddev->openers); 6031 mddev_put(mddev); 6032 6033 return 0; 6034 } 6035 6036 static int md_media_changed(struct gendisk *disk) 6037 { 6038 mddev_t *mddev = disk->private_data; 6039 6040 return mddev->changed; 6041 } 6042 6043 static int md_revalidate(struct gendisk *disk) 6044 { 6045 mddev_t *mddev = disk->private_data; 6046 6047 mddev->changed = 0; 6048 return 0; 6049 } 6050 static const struct block_device_operations md_fops = 6051 { 6052 .owner = THIS_MODULE, 6053 .open = md_open, 6054 .release = md_release, 6055 .ioctl = md_ioctl, 6056 #ifdef CONFIG_COMPAT 6057 .compat_ioctl = md_compat_ioctl, 6058 #endif 6059 .getgeo = md_getgeo, 6060 .media_changed = md_media_changed, 6061 .revalidate_disk= md_revalidate, 6062 }; 6063 6064 static int md_thread(void * arg) 6065 { 6066 mdk_thread_t *thread = arg; 6067 6068 /* 6069 * md_thread is a 'system-thread', it's priority should be very 6070 * high. We avoid resource deadlocks individually in each 6071 * raid personality. (RAID5 does preallocation) We also use RR and 6072 * the very same RT priority as kswapd, thus we will never get 6073 * into a priority inversion deadlock. 6074 * 6075 * we definitely have to have equal or higher priority than 6076 * bdflush, otherwise bdflush will deadlock if there are too 6077 * many dirty RAID5 blocks. 6078 */ 6079 6080 allow_signal(SIGKILL); 6081 while (!kthread_should_stop()) { 6082 6083 /* We need to wait INTERRUPTIBLE so that 6084 * we don't add to the load-average. 6085 * That means we need to be sure no signals are 6086 * pending 6087 */ 6088 if (signal_pending(current)) 6089 flush_signals(current); 6090 6091 wait_event_interruptible_timeout 6092 (thread->wqueue, 6093 test_bit(THREAD_WAKEUP, &thread->flags) 6094 || kthread_should_stop(), 6095 thread->timeout); 6096 6097 clear_bit(THREAD_WAKEUP, &thread->flags); 6098 if (!kthread_should_stop()) 6099 thread->run(thread->mddev); 6100 } 6101 6102 return 0; 6103 } 6104 6105 void md_wakeup_thread(mdk_thread_t *thread) 6106 { 6107 if (thread) { 6108 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm); 6109 set_bit(THREAD_WAKEUP, &thread->flags); 6110 wake_up(&thread->wqueue); 6111 } 6112 } 6113 6114 mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev, 6115 const char *name) 6116 { 6117 mdk_thread_t *thread; 6118 6119 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL); 6120 if (!thread) 6121 return NULL; 6122 6123 init_waitqueue_head(&thread->wqueue); 6124 6125 thread->run = run; 6126 thread->mddev = mddev; 6127 thread->timeout = MAX_SCHEDULE_TIMEOUT; 6128 thread->tsk = kthread_run(md_thread, thread, 6129 "%s_%s", 6130 mdname(thread->mddev), 6131 name ?: mddev->pers->name); 6132 if (IS_ERR(thread->tsk)) { 6133 kfree(thread); 6134 return NULL; 6135 } 6136 return thread; 6137 } 6138 6139 void md_unregister_thread(mdk_thread_t *thread) 6140 { 6141 if (!thread) 6142 return; 6143 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk)); 6144 6145 kthread_stop(thread->tsk); 6146 kfree(thread); 6147 } 6148 6149 void md_error(mddev_t *mddev, mdk_rdev_t *rdev) 6150 { 6151 if (!mddev) { 6152 MD_BUG(); 6153 return; 6154 } 6155 6156 if (!rdev || test_bit(Faulty, &rdev->flags)) 6157 return; 6158 6159 if (mddev->external) 6160 set_bit(Blocked, &rdev->flags); 6161 /* 6162 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n", 6163 mdname(mddev), 6164 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev), 6165 __builtin_return_address(0),__builtin_return_address(1), 6166 __builtin_return_address(2),__builtin_return_address(3)); 6167 */ 6168 if (!mddev->pers) 6169 return; 6170 if (!mddev->pers->error_handler) 6171 return; 6172 mddev->pers->error_handler(mddev,rdev); 6173 if (mddev->degraded) 6174 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); 6175 sysfs_notify_dirent_safe(rdev->sysfs_state); 6176 set_bit(MD_RECOVERY_INTR, &mddev->recovery); 6177 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); 6178 md_wakeup_thread(mddev->thread); 6179 if (mddev->event_work.func) 6180 queue_work(md_misc_wq, &mddev->event_work); 6181 md_new_event_inintr(mddev); 6182 } 6183 6184 /* seq_file implementation /proc/mdstat */ 6185 6186 static void status_unused(struct seq_file *seq) 6187 { 6188 int i = 0; 6189 mdk_rdev_t *rdev; 6190 6191 seq_printf(seq, "unused devices: "); 6192 6193 list_for_each_entry(rdev, &pending_raid_disks, same_set) { 6194 char b[BDEVNAME_SIZE]; 6195 i++; 6196 seq_printf(seq, "%s ", 6197 bdevname(rdev->bdev,b)); 6198 } 6199 if (!i) 6200 seq_printf(seq, "<none>"); 6201 6202 seq_printf(seq, "\n"); 6203 } 6204 6205 6206 static void status_resync(struct seq_file *seq, mddev_t * mddev) 6207 { 6208 sector_t max_sectors, resync, res; 6209 unsigned long dt, db; 6210 sector_t rt; 6211 int scale; 6212 unsigned int per_milli; 6213 6214 resync = mddev->curr_resync - atomic_read(&mddev->recovery_active); 6215 6216 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) 6217 max_sectors = mddev->resync_max_sectors; 6218 else 6219 max_sectors = mddev->dev_sectors; 6220 6221 /* 6222 * Should not happen. 6223 */ 6224 if (!max_sectors) { 6225 MD_BUG(); 6226 return; 6227 } 6228 /* Pick 'scale' such that (resync>>scale)*1000 will fit 6229 * in a sector_t, and (max_sectors>>scale) will fit in a 6230 * u32, as those are the requirements for sector_div. 6231 * Thus 'scale' must be at least 10 6232 */ 6233 scale = 10; 6234 if (sizeof(sector_t) > sizeof(unsigned long)) { 6235 while ( max_sectors/2 > (1ULL<<(scale+32))) 6236 scale++; 6237 } 6238 res = (resync>>scale)*1000; 6239 sector_div(res, (u32)((max_sectors>>scale)+1)); 6240 6241 per_milli = res; 6242 { 6243 int i, x = per_milli/50, y = 20-x; 6244 seq_printf(seq, "["); 6245 for (i = 0; i < x; i++) 6246 seq_printf(seq, "="); 6247 seq_printf(seq, ">"); 6248 for (i = 0; i < y; i++) 6249 seq_printf(seq, "."); 6250 seq_printf(seq, "] "); 6251 } 6252 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)", 6253 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)? 6254 "reshape" : 6255 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)? 6256 "check" : 6257 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ? 6258 "resync" : "recovery"))), 6259 per_milli/10, per_milli % 10, 6260 (unsigned long long) resync/2, 6261 (unsigned long long) max_sectors/2); 6262 6263 /* 6264 * dt: time from mark until now 6265 * db: blocks written from mark until now 6266 * rt: remaining time 6267 * 6268 * rt is a sector_t, so could be 32bit or 64bit. 6269 * So we divide before multiply in case it is 32bit and close 6270 * to the limit. 6271 * We scale the divisor (db) by 32 to avoid loosing precision 6272 * near the end of resync when the number of remaining sectors 6273 * is close to 'db'. 6274 * We then divide rt by 32 after multiplying by db to compensate. 6275 * The '+1' avoids division by zero if db is very small. 6276 */ 6277 dt = ((jiffies - mddev->resync_mark) / HZ); 6278 if (!dt) dt++; 6279 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active)) 6280 - mddev->resync_mark_cnt; 6281 6282 rt = max_sectors - resync; /* number of remaining sectors */ 6283 sector_div(rt, db/32+1); 6284 rt *= dt; 6285 rt >>= 5; 6286 6287 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60, 6288 ((unsigned long)rt % 60)/6); 6289 6290 seq_printf(seq, " speed=%ldK/sec", db/2/dt); 6291 } 6292 6293 static void *md_seq_start(struct seq_file *seq, loff_t *pos) 6294 { 6295 struct list_head *tmp; 6296 loff_t l = *pos; 6297 mddev_t *mddev; 6298 6299 if (l >= 0x10000) 6300 return NULL; 6301 if (!l--) 6302 /* header */ 6303 return (void*)1; 6304 6305 spin_lock(&all_mddevs_lock); 6306 list_for_each(tmp,&all_mddevs) 6307 if (!l--) { 6308 mddev = list_entry(tmp, mddev_t, all_mddevs); 6309 mddev_get(mddev); 6310 spin_unlock(&all_mddevs_lock); 6311 return mddev; 6312 } 6313 spin_unlock(&all_mddevs_lock); 6314 if (!l--) 6315 return (void*)2;/* tail */ 6316 return NULL; 6317 } 6318 6319 static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos) 6320 { 6321 struct list_head *tmp; 6322 mddev_t *next_mddev, *mddev = v; 6323 6324 ++*pos; 6325 if (v == (void*)2) 6326 return NULL; 6327 6328 spin_lock(&all_mddevs_lock); 6329 if (v == (void*)1) 6330 tmp = all_mddevs.next; 6331 else 6332 tmp = mddev->all_mddevs.next; 6333 if (tmp != &all_mddevs) 6334 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs)); 6335 else { 6336 next_mddev = (void*)2; 6337 *pos = 0x10000; 6338 } 6339 spin_unlock(&all_mddevs_lock); 6340 6341 if (v != (void*)1) 6342 mddev_put(mddev); 6343 return next_mddev; 6344 6345 } 6346 6347 static void md_seq_stop(struct seq_file *seq, void *v) 6348 { 6349 mddev_t *mddev = v; 6350 6351 if (mddev && v != (void*)1 && v != (void*)2) 6352 mddev_put(mddev); 6353 } 6354 6355 struct mdstat_info { 6356 int event; 6357 }; 6358 6359 static int md_seq_show(struct seq_file *seq, void *v) 6360 { 6361 mddev_t *mddev = v; 6362 sector_t sectors; 6363 mdk_rdev_t *rdev; 6364 struct mdstat_info *mi = seq->private; 6365 struct bitmap *bitmap; 6366 6367 if (v == (void*)1) { 6368 struct mdk_personality *pers; 6369 seq_printf(seq, "Personalities : "); 6370 spin_lock(&pers_lock); 6371 list_for_each_entry(pers, &pers_list, list) 6372 seq_printf(seq, "[%s] ", pers->name); 6373 6374 spin_unlock(&pers_lock); 6375 seq_printf(seq, "\n"); 6376 mi->event = atomic_read(&md_event_count); 6377 return 0; 6378 } 6379 if (v == (void*)2) { 6380 status_unused(seq); 6381 return 0; 6382 } 6383 6384 if (mddev_lock(mddev) < 0) 6385 return -EINTR; 6386 6387 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) { 6388 seq_printf(seq, "%s : %sactive", mdname(mddev), 6389 mddev->pers ? "" : "in"); 6390 if (mddev->pers) { 6391 if (mddev->ro==1) 6392 seq_printf(seq, " (read-only)"); 6393 if (mddev->ro==2) 6394 seq_printf(seq, " (auto-read-only)"); 6395 seq_printf(seq, " %s", mddev->pers->name); 6396 } 6397 6398 sectors = 0; 6399 list_for_each_entry(rdev, &mddev->disks, same_set) { 6400 char b[BDEVNAME_SIZE]; 6401 seq_printf(seq, " %s[%d]", 6402 bdevname(rdev->bdev,b), rdev->desc_nr); 6403 if (test_bit(WriteMostly, &rdev->flags)) 6404 seq_printf(seq, "(W)"); 6405 if (test_bit(Faulty, &rdev->flags)) { 6406 seq_printf(seq, "(F)"); 6407 continue; 6408 } else if (rdev->raid_disk < 0) 6409 seq_printf(seq, "(S)"); /* spare */ 6410 sectors += rdev->sectors; 6411 } 6412 6413 if (!list_empty(&mddev->disks)) { 6414 if (mddev->pers) 6415 seq_printf(seq, "\n %llu blocks", 6416 (unsigned long long) 6417 mddev->array_sectors / 2); 6418 else 6419 seq_printf(seq, "\n %llu blocks", 6420 (unsigned long long)sectors / 2); 6421 } 6422 if (mddev->persistent) { 6423 if (mddev->major_version != 0 || 6424 mddev->minor_version != 90) { 6425 seq_printf(seq," super %d.%d", 6426 mddev->major_version, 6427 mddev->minor_version); 6428 } 6429 } else if (mddev->external) 6430 seq_printf(seq, " super external:%s", 6431 mddev->metadata_type); 6432 else 6433 seq_printf(seq, " super non-persistent"); 6434 6435 if (mddev->pers) { 6436 mddev->pers->status(seq, mddev); 6437 seq_printf(seq, "\n "); 6438 if (mddev->pers->sync_request) { 6439 if (mddev->curr_resync > 2) { 6440 status_resync(seq, mddev); 6441 seq_printf(seq, "\n "); 6442 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2) 6443 seq_printf(seq, "\tresync=DELAYED\n "); 6444 else if (mddev->recovery_cp < MaxSector) 6445 seq_printf(seq, "\tresync=PENDING\n "); 6446 } 6447 } else 6448 seq_printf(seq, "\n "); 6449 6450 if ((bitmap = mddev->bitmap)) { 6451 unsigned long chunk_kb; 6452 unsigned long flags; 6453 spin_lock_irqsave(&bitmap->lock, flags); 6454 chunk_kb = mddev->bitmap_info.chunksize >> 10; 6455 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], " 6456 "%lu%s chunk", 6457 bitmap->pages - bitmap->missing_pages, 6458 bitmap->pages, 6459 (bitmap->pages - bitmap->missing_pages) 6460 << (PAGE_SHIFT - 10), 6461 chunk_kb ? chunk_kb : mddev->bitmap_info.chunksize, 6462 chunk_kb ? "KB" : "B"); 6463 if (bitmap->file) { 6464 seq_printf(seq, ", file: "); 6465 seq_path(seq, &bitmap->file->f_path, " \t\n"); 6466 } 6467 6468 seq_printf(seq, "\n"); 6469 spin_unlock_irqrestore(&bitmap->lock, flags); 6470 } 6471 6472 seq_printf(seq, "\n"); 6473 } 6474 mddev_unlock(mddev); 6475 6476 return 0; 6477 } 6478 6479 static const struct seq_operations md_seq_ops = { 6480 .start = md_seq_start, 6481 .next = md_seq_next, 6482 .stop = md_seq_stop, 6483 .show = md_seq_show, 6484 }; 6485 6486 static int md_seq_open(struct inode *inode, struct file *file) 6487 { 6488 int error; 6489 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL); 6490 if (mi == NULL) 6491 return -ENOMEM; 6492 6493 error = seq_open(file, &md_seq_ops); 6494 if (error) 6495 kfree(mi); 6496 else { 6497 struct seq_file *p = file->private_data; 6498 p->private = mi; 6499 mi->event = atomic_read(&md_event_count); 6500 } 6501 return error; 6502 } 6503 6504 static unsigned int mdstat_poll(struct file *filp, poll_table *wait) 6505 { 6506 struct seq_file *m = filp->private_data; 6507 struct mdstat_info *mi = m->private; 6508 int mask; 6509 6510 poll_wait(filp, &md_event_waiters, wait); 6511 6512 /* always allow read */ 6513 mask = POLLIN | POLLRDNORM; 6514 6515 if (mi->event != atomic_read(&md_event_count)) 6516 mask |= POLLERR | POLLPRI; 6517 return mask; 6518 } 6519 6520 static const struct file_operations md_seq_fops = { 6521 .owner = THIS_MODULE, 6522 .open = md_seq_open, 6523 .read = seq_read, 6524 .llseek = seq_lseek, 6525 .release = seq_release_private, 6526 .poll = mdstat_poll, 6527 }; 6528 6529 int register_md_personality(struct mdk_personality *p) 6530 { 6531 spin_lock(&pers_lock); 6532 list_add_tail(&p->list, &pers_list); 6533 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level); 6534 spin_unlock(&pers_lock); 6535 return 0; 6536 } 6537 6538 int unregister_md_personality(struct mdk_personality *p) 6539 { 6540 printk(KERN_INFO "md: %s personality unregistered\n", p->name); 6541 spin_lock(&pers_lock); 6542 list_del_init(&p->list); 6543 spin_unlock(&pers_lock); 6544 return 0; 6545 } 6546 6547 static int is_mddev_idle(mddev_t *mddev, int init) 6548 { 6549 mdk_rdev_t * rdev; 6550 int idle; 6551 int curr_events; 6552 6553 idle = 1; 6554 rcu_read_lock(); 6555 rdev_for_each_rcu(rdev, mddev) { 6556 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk; 6557 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) + 6558 (int)part_stat_read(&disk->part0, sectors[1]) - 6559 atomic_read(&disk->sync_io); 6560 /* sync IO will cause sync_io to increase before the disk_stats 6561 * as sync_io is counted when a request starts, and 6562 * disk_stats is counted when it completes. 6563 * So resync activity will cause curr_events to be smaller than 6564 * when there was no such activity. 6565 * non-sync IO will cause disk_stat to increase without 6566 * increasing sync_io so curr_events will (eventually) 6567 * be larger than it was before. Once it becomes 6568 * substantially larger, the test below will cause 6569 * the array to appear non-idle, and resync will slow 6570 * down. 6571 * If there is a lot of outstanding resync activity when 6572 * we set last_event to curr_events, then all that activity 6573 * completing might cause the array to appear non-idle 6574 * and resync will be slowed down even though there might 6575 * not have been non-resync activity. This will only 6576 * happen once though. 'last_events' will soon reflect 6577 * the state where there is little or no outstanding 6578 * resync requests, and further resync activity will 6579 * always make curr_events less than last_events. 6580 * 6581 */ 6582 if (init || curr_events - rdev->last_events > 64) { 6583 rdev->last_events = curr_events; 6584 idle = 0; 6585 } 6586 } 6587 rcu_read_unlock(); 6588 return idle; 6589 } 6590 6591 void md_done_sync(mddev_t *mddev, int blocks, int ok) 6592 { 6593 /* another "blocks" (512byte) blocks have been synced */ 6594 atomic_sub(blocks, &mddev->recovery_active); 6595 wake_up(&mddev->recovery_wait); 6596 if (!ok) { 6597 set_bit(MD_RECOVERY_INTR, &mddev->recovery); 6598 md_wakeup_thread(mddev->thread); 6599 // stop recovery, signal do_sync .... 6600 } 6601 } 6602 6603 6604 /* md_write_start(mddev, bi) 6605 * If we need to update some array metadata (e.g. 'active' flag 6606 * in superblock) before writing, schedule a superblock update 6607 * and wait for it to complete. 6608 */ 6609 void md_write_start(mddev_t *mddev, struct bio *bi) 6610 { 6611 int did_change = 0; 6612 if (bio_data_dir(bi) != WRITE) 6613 return; 6614 6615 BUG_ON(mddev->ro == 1); 6616 if (mddev->ro == 2) { 6617 /* need to switch to read/write */ 6618 mddev->ro = 0; 6619 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); 6620 md_wakeup_thread(mddev->thread); 6621 md_wakeup_thread(mddev->sync_thread); 6622 did_change = 1; 6623 } 6624 atomic_inc(&mddev->writes_pending); 6625 if (mddev->safemode == 1) 6626 mddev->safemode = 0; 6627 if (mddev->in_sync) { 6628 spin_lock_irq(&mddev->write_lock); 6629 if (mddev->in_sync) { 6630 mddev->in_sync = 0; 6631 set_bit(MD_CHANGE_CLEAN, &mddev->flags); 6632 set_bit(MD_CHANGE_PENDING, &mddev->flags); 6633 md_wakeup_thread(mddev->thread); 6634 did_change = 1; 6635 } 6636 spin_unlock_irq(&mddev->write_lock); 6637 } 6638 if (did_change) 6639 sysfs_notify_dirent_safe(mddev->sysfs_state); 6640 wait_event(mddev->sb_wait, 6641 !test_bit(MD_CHANGE_PENDING, &mddev->flags)); 6642 } 6643 6644 void md_write_end(mddev_t *mddev) 6645 { 6646 if (atomic_dec_and_test(&mddev->writes_pending)) { 6647 if (mddev->safemode == 2) 6648 md_wakeup_thread(mddev->thread); 6649 else if (mddev->safemode_delay) 6650 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay); 6651 } 6652 } 6653 6654 /* md_allow_write(mddev) 6655 * Calling this ensures that the array is marked 'active' so that writes 6656 * may proceed without blocking. It is important to call this before 6657 * attempting a GFP_KERNEL allocation while holding the mddev lock. 6658 * Must be called with mddev_lock held. 6659 * 6660 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock 6661 * is dropped, so return -EAGAIN after notifying userspace. 6662 */ 6663 int md_allow_write(mddev_t *mddev) 6664 { 6665 if (!mddev->pers) 6666 return 0; 6667 if (mddev->ro) 6668 return 0; 6669 if (!mddev->pers->sync_request) 6670 return 0; 6671 6672 spin_lock_irq(&mddev->write_lock); 6673 if (mddev->in_sync) { 6674 mddev->in_sync = 0; 6675 set_bit(MD_CHANGE_CLEAN, &mddev->flags); 6676 set_bit(MD_CHANGE_PENDING, &mddev->flags); 6677 if (mddev->safemode_delay && 6678 mddev->safemode == 0) 6679 mddev->safemode = 1; 6680 spin_unlock_irq(&mddev->write_lock); 6681 md_update_sb(mddev, 0); 6682 sysfs_notify_dirent_safe(mddev->sysfs_state); 6683 } else 6684 spin_unlock_irq(&mddev->write_lock); 6685 6686 if (test_bit(MD_CHANGE_PENDING, &mddev->flags)) 6687 return -EAGAIN; 6688 else 6689 return 0; 6690 } 6691 EXPORT_SYMBOL_GPL(md_allow_write); 6692 6693 void md_unplug(mddev_t *mddev) 6694 { 6695 if (mddev->queue) 6696 blk_unplug(mddev->queue); 6697 if (mddev->plug) 6698 mddev->plug->unplug_fn(mddev->plug); 6699 } 6700 6701 #define SYNC_MARKS 10 6702 #define SYNC_MARK_STEP (3*HZ) 6703 void md_do_sync(mddev_t *mddev) 6704 { 6705 mddev_t *mddev2; 6706 unsigned int currspeed = 0, 6707 window; 6708 sector_t max_sectors,j, io_sectors; 6709 unsigned long mark[SYNC_MARKS]; 6710 sector_t mark_cnt[SYNC_MARKS]; 6711 int last_mark,m; 6712 struct list_head *tmp; 6713 sector_t last_check; 6714 int skipped = 0; 6715 mdk_rdev_t *rdev; 6716 char *desc; 6717 6718 /* just incase thread restarts... */ 6719 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery)) 6720 return; 6721 if (mddev->ro) /* never try to sync a read-only array */ 6722 return; 6723 6724 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { 6725 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) 6726 desc = "data-check"; 6727 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) 6728 desc = "requested-resync"; 6729 else 6730 desc = "resync"; 6731 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) 6732 desc = "reshape"; 6733 else 6734 desc = "recovery"; 6735 6736 /* we overload curr_resync somewhat here. 6737 * 0 == not engaged in resync at all 6738 * 2 == checking that there is no conflict with another sync 6739 * 1 == like 2, but have yielded to allow conflicting resync to 6740 * commense 6741 * other == active in resync - this many blocks 6742 * 6743 * Before starting a resync we must have set curr_resync to 6744 * 2, and then checked that every "conflicting" array has curr_resync 6745 * less than ours. When we find one that is the same or higher 6746 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync 6747 * to 1 if we choose to yield (based arbitrarily on address of mddev structure). 6748 * This will mean we have to start checking from the beginning again. 6749 * 6750 */ 6751 6752 do { 6753 mddev->curr_resync = 2; 6754 6755 try_again: 6756 if (kthread_should_stop()) 6757 set_bit(MD_RECOVERY_INTR, &mddev->recovery); 6758 6759 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) 6760 goto skip; 6761 for_each_mddev(mddev2, tmp) { 6762 if (mddev2 == mddev) 6763 continue; 6764 if (!mddev->parallel_resync 6765 && mddev2->curr_resync 6766 && match_mddev_units(mddev, mddev2)) { 6767 DEFINE_WAIT(wq); 6768 if (mddev < mddev2 && mddev->curr_resync == 2) { 6769 /* arbitrarily yield */ 6770 mddev->curr_resync = 1; 6771 wake_up(&resync_wait); 6772 } 6773 if (mddev > mddev2 && mddev->curr_resync == 1) 6774 /* no need to wait here, we can wait the next 6775 * time 'round when curr_resync == 2 6776 */ 6777 continue; 6778 /* We need to wait 'interruptible' so as not to 6779 * contribute to the load average, and not to 6780 * be caught by 'softlockup' 6781 */ 6782 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE); 6783 if (!kthread_should_stop() && 6784 mddev2->curr_resync >= mddev->curr_resync) { 6785 printk(KERN_INFO "md: delaying %s of %s" 6786 " until %s has finished (they" 6787 " share one or more physical units)\n", 6788 desc, mdname(mddev), mdname(mddev2)); 6789 mddev_put(mddev2); 6790 if (signal_pending(current)) 6791 flush_signals(current); 6792 schedule(); 6793 finish_wait(&resync_wait, &wq); 6794 goto try_again; 6795 } 6796 finish_wait(&resync_wait, &wq); 6797 } 6798 } 6799 } while (mddev->curr_resync < 2); 6800 6801 j = 0; 6802 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { 6803 /* resync follows the size requested by the personality, 6804 * which defaults to physical size, but can be virtual size 6805 */ 6806 max_sectors = mddev->resync_max_sectors; 6807 mddev->resync_mismatches = 0; 6808 /* we don't use the checkpoint if there's a bitmap */ 6809 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) 6810 j = mddev->resync_min; 6811 else if (!mddev->bitmap) 6812 j = mddev->recovery_cp; 6813 6814 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) 6815 max_sectors = mddev->dev_sectors; 6816 else { 6817 /* recovery follows the physical size of devices */ 6818 max_sectors = mddev->dev_sectors; 6819 j = MaxSector; 6820 rcu_read_lock(); 6821 list_for_each_entry_rcu(rdev, &mddev->disks, same_set) 6822 if (rdev->raid_disk >= 0 && 6823 !test_bit(Faulty, &rdev->flags) && 6824 !test_bit(In_sync, &rdev->flags) && 6825 rdev->recovery_offset < j) 6826 j = rdev->recovery_offset; 6827 rcu_read_unlock(); 6828 } 6829 6830 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev)); 6831 printk(KERN_INFO "md: minimum _guaranteed_ speed:" 6832 " %d KB/sec/disk.\n", speed_min(mddev)); 6833 printk(KERN_INFO "md: using maximum available idle IO bandwidth " 6834 "(but not more than %d KB/sec) for %s.\n", 6835 speed_max(mddev), desc); 6836 6837 is_mddev_idle(mddev, 1); /* this initializes IO event counters */ 6838 6839 io_sectors = 0; 6840 for (m = 0; m < SYNC_MARKS; m++) { 6841 mark[m] = jiffies; 6842 mark_cnt[m] = io_sectors; 6843 } 6844 last_mark = 0; 6845 mddev->resync_mark = mark[last_mark]; 6846 mddev->resync_mark_cnt = mark_cnt[last_mark]; 6847 6848 /* 6849 * Tune reconstruction: 6850 */ 6851 window = 32*(PAGE_SIZE/512); 6852 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n", 6853 window/2,(unsigned long long) max_sectors/2); 6854 6855 atomic_set(&mddev->recovery_active, 0); 6856 last_check = 0; 6857 6858 if (j>2) { 6859 printk(KERN_INFO 6860 "md: resuming %s of %s from checkpoint.\n", 6861 desc, mdname(mddev)); 6862 mddev->curr_resync = j; 6863 } 6864 mddev->curr_resync_completed = j; 6865 6866 while (j < max_sectors) { 6867 sector_t sectors; 6868 6869 skipped = 0; 6870 6871 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && 6872 ((mddev->curr_resync > mddev->curr_resync_completed && 6873 (mddev->curr_resync - mddev->curr_resync_completed) 6874 > (max_sectors >> 4)) || 6875 (j - mddev->curr_resync_completed)*2 6876 >= mddev->resync_max - mddev->curr_resync_completed 6877 )) { 6878 /* time to update curr_resync_completed */ 6879 md_unplug(mddev); 6880 wait_event(mddev->recovery_wait, 6881 atomic_read(&mddev->recovery_active) == 0); 6882 mddev->curr_resync_completed = j; 6883 set_bit(MD_CHANGE_CLEAN, &mddev->flags); 6884 sysfs_notify(&mddev->kobj, NULL, "sync_completed"); 6885 } 6886 6887 while (j >= mddev->resync_max && !kthread_should_stop()) { 6888 /* As this condition is controlled by user-space, 6889 * we can block indefinitely, so use '_interruptible' 6890 * to avoid triggering warnings. 6891 */ 6892 flush_signals(current); /* just in case */ 6893 wait_event_interruptible(mddev->recovery_wait, 6894 mddev->resync_max > j 6895 || kthread_should_stop()); 6896 } 6897 6898 if (kthread_should_stop()) 6899 goto interrupted; 6900 6901 sectors = mddev->pers->sync_request(mddev, j, &skipped, 6902 currspeed < speed_min(mddev)); 6903 if (sectors == 0) { 6904 set_bit(MD_RECOVERY_INTR, &mddev->recovery); 6905 goto out; 6906 } 6907 6908 if (!skipped) { /* actual IO requested */ 6909 io_sectors += sectors; 6910 atomic_add(sectors, &mddev->recovery_active); 6911 } 6912 6913 j += sectors; 6914 if (j>1) mddev->curr_resync = j; 6915 mddev->curr_mark_cnt = io_sectors; 6916 if (last_check == 0) 6917 /* this is the earliers that rebuilt will be 6918 * visible in /proc/mdstat 6919 */ 6920 md_new_event(mddev); 6921 6922 if (last_check + window > io_sectors || j == max_sectors) 6923 continue; 6924 6925 last_check = io_sectors; 6926 6927 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) 6928 break; 6929 6930 repeat: 6931 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) { 6932 /* step marks */ 6933 int next = (last_mark+1) % SYNC_MARKS; 6934 6935 mddev->resync_mark = mark[next]; 6936 mddev->resync_mark_cnt = mark_cnt[next]; 6937 mark[next] = jiffies; 6938 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active); 6939 last_mark = next; 6940 } 6941 6942 6943 if (kthread_should_stop()) 6944 goto interrupted; 6945 6946 6947 /* 6948 * this loop exits only if either when we are slower than 6949 * the 'hard' speed limit, or the system was IO-idle for 6950 * a jiffy. 6951 * the system might be non-idle CPU-wise, but we only care 6952 * about not overloading the IO subsystem. (things like an 6953 * e2fsck being done on the RAID array should execute fast) 6954 */ 6955 md_unplug(mddev); 6956 cond_resched(); 6957 6958 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2 6959 /((jiffies-mddev->resync_mark)/HZ +1) +1; 6960 6961 if (currspeed > speed_min(mddev)) { 6962 if ((currspeed > speed_max(mddev)) || 6963 !is_mddev_idle(mddev, 0)) { 6964 msleep(500); 6965 goto repeat; 6966 } 6967 } 6968 } 6969 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc); 6970 /* 6971 * this also signals 'finished resyncing' to md_stop 6972 */ 6973 out: 6974 md_unplug(mddev); 6975 6976 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active)); 6977 6978 /* tell personality that we are finished */ 6979 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1); 6980 6981 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) && 6982 mddev->curr_resync > 2) { 6983 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { 6984 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { 6985 if (mddev->curr_resync >= mddev->recovery_cp) { 6986 printk(KERN_INFO 6987 "md: checkpointing %s of %s.\n", 6988 desc, mdname(mddev)); 6989 mddev->recovery_cp = mddev->curr_resync; 6990 } 6991 } else 6992 mddev->recovery_cp = MaxSector; 6993 } else { 6994 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) 6995 mddev->curr_resync = MaxSector; 6996 rcu_read_lock(); 6997 list_for_each_entry_rcu(rdev, &mddev->disks, same_set) 6998 if (rdev->raid_disk >= 0 && 6999 mddev->delta_disks >= 0 && 7000 !test_bit(Faulty, &rdev->flags) && 7001 !test_bit(In_sync, &rdev->flags) && 7002 rdev->recovery_offset < mddev->curr_resync) 7003 rdev->recovery_offset = mddev->curr_resync; 7004 rcu_read_unlock(); 7005 } 7006 } 7007 set_bit(MD_CHANGE_DEVS, &mddev->flags); 7008 7009 skip: 7010 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { 7011 /* We completed so min/max setting can be forgotten if used. */ 7012 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) 7013 mddev->resync_min = 0; 7014 mddev->resync_max = MaxSector; 7015 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) 7016 mddev->resync_min = mddev->curr_resync_completed; 7017 mddev->curr_resync = 0; 7018 wake_up(&resync_wait); 7019 set_bit(MD_RECOVERY_DONE, &mddev->recovery); 7020 md_wakeup_thread(mddev->thread); 7021 return; 7022 7023 interrupted: 7024 /* 7025 * got a signal, exit. 7026 */ 7027 printk(KERN_INFO 7028 "md: md_do_sync() got signal ... exiting\n"); 7029 set_bit(MD_RECOVERY_INTR, &mddev->recovery); 7030 goto out; 7031 7032 } 7033 EXPORT_SYMBOL_GPL(md_do_sync); 7034 7035 7036 static int remove_and_add_spares(mddev_t *mddev) 7037 { 7038 mdk_rdev_t *rdev; 7039 int spares = 0; 7040 7041 mddev->curr_resync_completed = 0; 7042 7043 list_for_each_entry(rdev, &mddev->disks, same_set) 7044 if (rdev->raid_disk >= 0 && 7045 !test_bit(Blocked, &rdev->flags) && 7046 (test_bit(Faulty, &rdev->flags) || 7047 ! test_bit(In_sync, &rdev->flags)) && 7048 atomic_read(&rdev->nr_pending)==0) { 7049 if (mddev->pers->hot_remove_disk( 7050 mddev, rdev->raid_disk)==0) { 7051 char nm[20]; 7052 sprintf(nm,"rd%d", rdev->raid_disk); 7053 sysfs_remove_link(&mddev->kobj, nm); 7054 rdev->raid_disk = -1; 7055 } 7056 } 7057 7058 if (mddev->degraded && !mddev->recovery_disabled) { 7059 list_for_each_entry(rdev, &mddev->disks, same_set) { 7060 if (rdev->raid_disk >= 0 && 7061 !test_bit(In_sync, &rdev->flags) && 7062 !test_bit(Blocked, &rdev->flags)) 7063 spares++; 7064 if (rdev->raid_disk < 0 7065 && !test_bit(Faulty, &rdev->flags)) { 7066 rdev->recovery_offset = 0; 7067 if (mddev->pers-> 7068 hot_add_disk(mddev, rdev) == 0) { 7069 char nm[20]; 7070 sprintf(nm, "rd%d", rdev->raid_disk); 7071 if (sysfs_create_link(&mddev->kobj, 7072 &rdev->kobj, nm)) 7073 /* failure here is OK */; 7074 spares++; 7075 md_new_event(mddev); 7076 set_bit(MD_CHANGE_DEVS, &mddev->flags); 7077 } else 7078 break; 7079 } 7080 } 7081 } 7082 return spares; 7083 } 7084 7085 static void reap_sync_thread(mddev_t *mddev) 7086 { 7087 mdk_rdev_t *rdev; 7088 7089 /* resync has finished, collect result */ 7090 md_unregister_thread(mddev->sync_thread); 7091 mddev->sync_thread = NULL; 7092 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && 7093 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { 7094 /* success...*/ 7095 /* activate any spares */ 7096 if (mddev->pers->spare_active(mddev)) 7097 sysfs_notify(&mddev->kobj, NULL, 7098 "degraded"); 7099 } 7100 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && 7101 mddev->pers->finish_reshape) 7102 mddev->pers->finish_reshape(mddev); 7103 md_update_sb(mddev, 1); 7104 7105 /* if array is no-longer degraded, then any saved_raid_disk 7106 * information must be scrapped 7107 */ 7108 if (!mddev->degraded) 7109 list_for_each_entry(rdev, &mddev->disks, same_set) 7110 rdev->saved_raid_disk = -1; 7111 7112 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); 7113 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); 7114 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); 7115 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); 7116 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); 7117 /* flag recovery needed just to double check */ 7118 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); 7119 sysfs_notify_dirent_safe(mddev->sysfs_action); 7120 md_new_event(mddev); 7121 } 7122 7123 /* 7124 * This routine is regularly called by all per-raid-array threads to 7125 * deal with generic issues like resync and super-block update. 7126 * Raid personalities that don't have a thread (linear/raid0) do not 7127 * need this as they never do any recovery or update the superblock. 7128 * 7129 * It does not do any resync itself, but rather "forks" off other threads 7130 * to do that as needed. 7131 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in 7132 * "->recovery" and create a thread at ->sync_thread. 7133 * When the thread finishes it sets MD_RECOVERY_DONE 7134 * and wakeups up this thread which will reap the thread and finish up. 7135 * This thread also removes any faulty devices (with nr_pending == 0). 7136 * 7137 * The overall approach is: 7138 * 1/ if the superblock needs updating, update it. 7139 * 2/ If a recovery thread is running, don't do anything else. 7140 * 3/ If recovery has finished, clean up, possibly marking spares active. 7141 * 4/ If there are any faulty devices, remove them. 7142 * 5/ If array is degraded, try to add spares devices 7143 * 6/ If array has spares or is not in-sync, start a resync thread. 7144 */ 7145 void md_check_recovery(mddev_t *mddev) 7146 { 7147 if (mddev->bitmap) 7148 bitmap_daemon_work(mddev); 7149 7150 if (mddev->ro) 7151 return; 7152 7153 if (signal_pending(current)) { 7154 if (mddev->pers->sync_request && !mddev->external) { 7155 printk(KERN_INFO "md: %s in immediate safe mode\n", 7156 mdname(mddev)); 7157 mddev->safemode = 2; 7158 } 7159 flush_signals(current); 7160 } 7161 7162 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) 7163 return; 7164 if ( ! ( 7165 (mddev->flags & ~ (1<<MD_CHANGE_PENDING)) || 7166 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || 7167 test_bit(MD_RECOVERY_DONE, &mddev->recovery) || 7168 (mddev->external == 0 && mddev->safemode == 1) || 7169 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending) 7170 && !mddev->in_sync && mddev->recovery_cp == MaxSector) 7171 )) 7172 return; 7173 7174 if (mddev_trylock(mddev)) { 7175 int spares = 0; 7176 7177 if (mddev->ro) { 7178 /* Only thing we do on a ro array is remove 7179 * failed devices. 7180 */ 7181 mdk_rdev_t *rdev; 7182 list_for_each_entry(rdev, &mddev->disks, same_set) 7183 if (rdev->raid_disk >= 0 && 7184 !test_bit(Blocked, &rdev->flags) && 7185 test_bit(Faulty, &rdev->flags) && 7186 atomic_read(&rdev->nr_pending)==0) { 7187 if (mddev->pers->hot_remove_disk( 7188 mddev, rdev->raid_disk)==0) { 7189 char nm[20]; 7190 sprintf(nm,"rd%d", rdev->raid_disk); 7191 sysfs_remove_link(&mddev->kobj, nm); 7192 rdev->raid_disk = -1; 7193 } 7194 } 7195 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); 7196 goto unlock; 7197 } 7198 7199 if (!mddev->external) { 7200 int did_change = 0; 7201 spin_lock_irq(&mddev->write_lock); 7202 if (mddev->safemode && 7203 !atomic_read(&mddev->writes_pending) && 7204 !mddev->in_sync && 7205 mddev->recovery_cp == MaxSector) { 7206 mddev->in_sync = 1; 7207 did_change = 1; 7208 set_bit(MD_CHANGE_CLEAN, &mddev->flags); 7209 } 7210 if (mddev->safemode == 1) 7211 mddev->safemode = 0; 7212 spin_unlock_irq(&mddev->write_lock); 7213 if (did_change) 7214 sysfs_notify_dirent_safe(mddev->sysfs_state); 7215 } 7216 7217 if (mddev->flags) 7218 md_update_sb(mddev, 0); 7219 7220 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && 7221 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) { 7222 /* resync/recovery still happening */ 7223 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); 7224 goto unlock; 7225 } 7226 if (mddev->sync_thread) { 7227 reap_sync_thread(mddev); 7228 goto unlock; 7229 } 7230 /* Set RUNNING before clearing NEEDED to avoid 7231 * any transients in the value of "sync_action". 7232 */ 7233 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); 7234 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); 7235 /* Clear some bits that don't mean anything, but 7236 * might be left set 7237 */ 7238 clear_bit(MD_RECOVERY_INTR, &mddev->recovery); 7239 clear_bit(MD_RECOVERY_DONE, &mddev->recovery); 7240 7241 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) 7242 goto unlock; 7243 /* no recovery is running. 7244 * remove any failed drives, then 7245 * add spares if possible. 7246 * Spare are also removed and re-added, to allow 7247 * the personality to fail the re-add. 7248 */ 7249 7250 if (mddev->reshape_position != MaxSector) { 7251 if (mddev->pers->check_reshape == NULL || 7252 mddev->pers->check_reshape(mddev) != 0) 7253 /* Cannot proceed */ 7254 goto unlock; 7255 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); 7256 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); 7257 } else if ((spares = remove_and_add_spares(mddev))) { 7258 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); 7259 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); 7260 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); 7261 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); 7262 } else if (mddev->recovery_cp < MaxSector) { 7263 set_bit(MD_RECOVERY_SYNC, &mddev->recovery); 7264 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); 7265 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) 7266 /* nothing to be done ... */ 7267 goto unlock; 7268 7269 if (mddev->pers->sync_request) { 7270 if (spares && mddev->bitmap && ! mddev->bitmap->file) { 7271 /* We are adding a device or devices to an array 7272 * which has the bitmap stored on all devices. 7273 * So make sure all bitmap pages get written 7274 */ 7275 bitmap_write_all(mddev->bitmap); 7276 } 7277 mddev->sync_thread = md_register_thread(md_do_sync, 7278 mddev, 7279 "resync"); 7280 if (!mddev->sync_thread) { 7281 printk(KERN_ERR "%s: could not start resync" 7282 " thread...\n", 7283 mdname(mddev)); 7284 /* leave the spares where they are, it shouldn't hurt */ 7285 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); 7286 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); 7287 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); 7288 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); 7289 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); 7290 } else 7291 md_wakeup_thread(mddev->sync_thread); 7292 sysfs_notify_dirent_safe(mddev->sysfs_action); 7293 md_new_event(mddev); 7294 } 7295 unlock: 7296 if (!mddev->sync_thread) { 7297 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); 7298 if (test_and_clear_bit(MD_RECOVERY_RECOVER, 7299 &mddev->recovery)) 7300 if (mddev->sysfs_action) 7301 sysfs_notify_dirent_safe(mddev->sysfs_action); 7302 } 7303 mddev_unlock(mddev); 7304 } 7305 } 7306 7307 void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev) 7308 { 7309 sysfs_notify_dirent_safe(rdev->sysfs_state); 7310 wait_event_timeout(rdev->blocked_wait, 7311 !test_bit(Blocked, &rdev->flags), 7312 msecs_to_jiffies(5000)); 7313 rdev_dec_pending(rdev, mddev); 7314 } 7315 EXPORT_SYMBOL(md_wait_for_blocked_rdev); 7316 7317 static int md_notify_reboot(struct notifier_block *this, 7318 unsigned long code, void *x) 7319 { 7320 struct list_head *tmp; 7321 mddev_t *mddev; 7322 7323 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) { 7324 7325 printk(KERN_INFO "md: stopping all md devices.\n"); 7326 7327 for_each_mddev(mddev, tmp) 7328 if (mddev_trylock(mddev)) { 7329 /* Force a switch to readonly even array 7330 * appears to still be in use. Hence 7331 * the '100'. 7332 */ 7333 md_set_readonly(mddev, 100); 7334 mddev_unlock(mddev); 7335 } 7336 /* 7337 * certain more exotic SCSI devices are known to be 7338 * volatile wrt too early system reboots. While the 7339 * right place to handle this issue is the given 7340 * driver, we do want to have a safe RAID driver ... 7341 */ 7342 mdelay(1000*1); 7343 } 7344 return NOTIFY_DONE; 7345 } 7346 7347 static struct notifier_block md_notifier = { 7348 .notifier_call = md_notify_reboot, 7349 .next = NULL, 7350 .priority = INT_MAX, /* before any real devices */ 7351 }; 7352 7353 static void md_geninit(void) 7354 { 7355 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t)); 7356 7357 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops); 7358 } 7359 7360 static int __init md_init(void) 7361 { 7362 int ret = -ENOMEM; 7363 7364 md_wq = alloc_workqueue("md", WQ_RESCUER, 0); 7365 if (!md_wq) 7366 goto err_wq; 7367 7368 md_misc_wq = alloc_workqueue("md_misc", 0, 0); 7369 if (!md_misc_wq) 7370 goto err_misc_wq; 7371 7372 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0) 7373 goto err_md; 7374 7375 if ((ret = register_blkdev(0, "mdp")) < 0) 7376 goto err_mdp; 7377 mdp_major = ret; 7378 7379 blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE, 7380 md_probe, NULL, NULL); 7381 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE, 7382 md_probe, NULL, NULL); 7383 7384 register_reboot_notifier(&md_notifier); 7385 raid_table_header = register_sysctl_table(raid_root_table); 7386 7387 md_geninit(); 7388 return 0; 7389 7390 err_mdp: 7391 unregister_blkdev(MD_MAJOR, "md"); 7392 err_md: 7393 destroy_workqueue(md_misc_wq); 7394 err_misc_wq: 7395 destroy_workqueue(md_wq); 7396 err_wq: 7397 return ret; 7398 } 7399 7400 #ifndef MODULE 7401 7402 /* 7403 * Searches all registered partitions for autorun RAID arrays 7404 * at boot time. 7405 */ 7406 7407 static LIST_HEAD(all_detected_devices); 7408 struct detected_devices_node { 7409 struct list_head list; 7410 dev_t dev; 7411 }; 7412 7413 void md_autodetect_dev(dev_t dev) 7414 { 7415 struct detected_devices_node *node_detected_dev; 7416 7417 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL); 7418 if (node_detected_dev) { 7419 node_detected_dev->dev = dev; 7420 list_add_tail(&node_detected_dev->list, &all_detected_devices); 7421 } else { 7422 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed" 7423 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev)); 7424 } 7425 } 7426 7427 7428 static void autostart_arrays(int part) 7429 { 7430 mdk_rdev_t *rdev; 7431 struct detected_devices_node *node_detected_dev; 7432 dev_t dev; 7433 int i_scanned, i_passed; 7434 7435 i_scanned = 0; 7436 i_passed = 0; 7437 7438 printk(KERN_INFO "md: Autodetecting RAID arrays.\n"); 7439 7440 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) { 7441 i_scanned++; 7442 node_detected_dev = list_entry(all_detected_devices.next, 7443 struct detected_devices_node, list); 7444 list_del(&node_detected_dev->list); 7445 dev = node_detected_dev->dev; 7446 kfree(node_detected_dev); 7447 rdev = md_import_device(dev,0, 90); 7448 if (IS_ERR(rdev)) 7449 continue; 7450 7451 if (test_bit(Faulty, &rdev->flags)) { 7452 MD_BUG(); 7453 continue; 7454 } 7455 set_bit(AutoDetected, &rdev->flags); 7456 list_add(&rdev->same_set, &pending_raid_disks); 7457 i_passed++; 7458 } 7459 7460 printk(KERN_INFO "md: Scanned %d and added %d devices.\n", 7461 i_scanned, i_passed); 7462 7463 autorun_devices(part); 7464 } 7465 7466 #endif /* !MODULE */ 7467 7468 static __exit void md_exit(void) 7469 { 7470 mddev_t *mddev; 7471 struct list_head *tmp; 7472 7473 blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS); 7474 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS); 7475 7476 unregister_blkdev(MD_MAJOR,"md"); 7477 unregister_blkdev(mdp_major, "mdp"); 7478 unregister_reboot_notifier(&md_notifier); 7479 unregister_sysctl_table(raid_table_header); 7480 remove_proc_entry("mdstat", NULL); 7481 for_each_mddev(mddev, tmp) { 7482 export_array(mddev); 7483 mddev->hold_active = 0; 7484 } 7485 destroy_workqueue(md_misc_wq); 7486 destroy_workqueue(md_wq); 7487 } 7488 7489 subsys_initcall(md_init); 7490 module_exit(md_exit) 7491 7492 static int get_ro(char *buffer, struct kernel_param *kp) 7493 { 7494 return sprintf(buffer, "%d", start_readonly); 7495 } 7496 static int set_ro(const char *val, struct kernel_param *kp) 7497 { 7498 char *e; 7499 int num = simple_strtoul(val, &e, 10); 7500 if (*val && (*e == '\0' || *e == '\n')) { 7501 start_readonly = num; 7502 return 0; 7503 } 7504 return -EINVAL; 7505 } 7506 7507 module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR); 7508 module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR); 7509 7510 module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR); 7511 7512 EXPORT_SYMBOL(register_md_personality); 7513 EXPORT_SYMBOL(unregister_md_personality); 7514 EXPORT_SYMBOL(md_error); 7515 EXPORT_SYMBOL(md_done_sync); 7516 EXPORT_SYMBOL(md_write_start); 7517 EXPORT_SYMBOL(md_write_end); 7518 EXPORT_SYMBOL(md_register_thread); 7519 EXPORT_SYMBOL(md_unregister_thread); 7520 EXPORT_SYMBOL(md_wakeup_thread); 7521 EXPORT_SYMBOL(md_check_recovery); 7522 MODULE_LICENSE("GPL"); 7523 MODULE_DESCRIPTION("MD RAID framework"); 7524 MODULE_ALIAS("md"); 7525 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR); 7526