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