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