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