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