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