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