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