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