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