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