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