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