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