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