1 /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 /* 3 md.h : kernel internal structure of the Linux MD driver 4 Copyright (C) 1996-98 Ingo Molnar, Gadi Oxman 5 6 */ 7 8 #ifndef _MD_MD_H 9 #define _MD_MD_H 10 11 #include <linux/blkdev.h> 12 #include <linux/backing-dev.h> 13 #include <linux/badblocks.h> 14 #include <linux/kobject.h> 15 #include <linux/list.h> 16 #include <linux/mm.h> 17 #include <linux/mutex.h> 18 #include <linux/timer.h> 19 #include <linux/wait.h> 20 #include <linux/workqueue.h> 21 #include <linux/raid/md_u.h> 22 #include <trace/events/block.h> 23 24 #define MaxSector (~(sector_t)0) 25 /* 26 * Number of guaranteed raid bios in case of extreme VM load: 27 */ 28 #define NR_RAID_BIOS 256 29 30 enum md_submodule_type { 31 MD_PERSONALITY = 0, 32 MD_CLUSTER, 33 MD_BITMAP, 34 }; 35 36 enum md_submodule_id { 37 ID_LINEAR = LEVEL_LINEAR, 38 ID_RAID0 = 0, 39 ID_RAID1 = 1, 40 ID_RAID4 = 4, 41 ID_RAID5 = 5, 42 ID_RAID6 = 6, 43 ID_RAID10 = 10, 44 ID_CLUSTER, 45 ID_BITMAP, 46 ID_LLBITMAP, 47 ID_BITMAP_NONE, 48 }; 49 50 struct md_submodule_head { 51 enum md_submodule_type type; 52 enum md_submodule_id id; 53 const char *name; 54 struct module *owner; 55 }; 56 57 /* 58 * These flags should really be called "NO_RETRY" rather than 59 * "FAILFAST" because they don't make any promise about time lapse, 60 * only about the number of retries, which will be zero. 61 * REQ_FAILFAST_DRIVER is not included because 62 * Commit: 4a27446f3e39 ("[SCSI] modify scsi to handle new fail fast flags.") 63 * seems to suggest that the errors it avoids retrying should usually 64 * be retried. 65 */ 66 #define MD_FAILFAST (REQ_FAILFAST_DEV | REQ_FAILFAST_TRANSPORT) 67 68 /* Status of sync thread. */ 69 enum sync_action { 70 /* 71 * Represent by MD_RECOVERY_SYNC, start when: 72 * 1) after assemble, sync data from first rdev to other copies, this 73 * must be done first before other sync actions and will only execute 74 * once; 75 * 2) resize the array(notice that this is not reshape), sync data for 76 * the new range; 77 */ 78 ACTION_RESYNC, 79 /* 80 * Represent by MD_RECOVERY_RECOVER, start when: 81 * 1) for new replacement, sync data based on the replace rdev or 82 * available copies from other rdev; 83 * 2) for new member disk while the array is degraded, sync data from 84 * other rdev; 85 * 3) reassemble after power failure or re-add a hot removed rdev, sync 86 * data from first rdev to other copies based on bitmap; 87 */ 88 ACTION_RECOVER, 89 /* 90 * Represent by MD_RECOVERY_SYNC | MD_RECOVERY_REQUESTED | 91 * MD_RECOVERY_CHECK, start when user echo "check" to sysfs api 92 * sync_action, used to check if data copies from differenct rdev are 93 * the same. The number of mismatch sectors will be exported to user 94 * by sysfs api mismatch_cnt; 95 */ 96 ACTION_CHECK, 97 /* 98 * Represent by MD_RECOVERY_SYNC | MD_RECOVERY_REQUESTED, start when 99 * user echo "repair" to sysfs api sync_action, usually paired with 100 * ACTION_CHECK, used to force syncing data once user found that there 101 * are inconsistent data, 102 */ 103 ACTION_REPAIR, 104 /* 105 * Represent by MD_RECOVERY_RESHAPE, start when new member disk is added 106 * to the conf, notice that this is different from spares or 107 * replacement; 108 */ 109 ACTION_RESHAPE, 110 /* 111 * Represent by MD_RECOVERY_FROZEN, can be set by sysfs api sync_action 112 * or internal usage like setting the array read-only, will forbid above 113 * actions. 114 */ 115 ACTION_FROZEN, 116 /* 117 * All above actions don't match. 118 */ 119 ACTION_IDLE, 120 NR_SYNC_ACTIONS, 121 }; 122 123 /* 124 * The struct embedded in rdev is used to serialize IO. 125 */ 126 struct serial_in_rdev { 127 struct rb_root_cached serial_rb; 128 spinlock_t serial_lock; 129 }; 130 131 /* 132 * MD's 'extended' device 133 */ 134 struct md_rdev { 135 struct list_head same_set; /* RAID devices within the same set */ 136 137 sector_t sectors; /* Device size (in 512bytes sectors) */ 138 struct mddev *mddev; /* RAID array if running */ 139 unsigned long last_events; /* IO event timestamp */ 140 141 /* 142 * If meta_bdev is non-NULL, it means that a separate device is 143 * being used to store the metadata (superblock/bitmap) which 144 * would otherwise be contained on the same device as the data (bdev). 145 */ 146 struct block_device *meta_bdev; 147 struct block_device *bdev; /* block device handle */ 148 struct file *bdev_file; /* Handle from open for bdev */ 149 150 struct page *sb_page, *bb_page; 151 int sb_loaded; 152 __u64 sb_events; 153 sector_t data_offset; /* start of data in array */ 154 sector_t new_data_offset;/* only relevant while reshaping */ 155 sector_t sb_start; /* offset of the super block (in 512byte sectors) */ 156 int sb_size; /* bytes in the superblock */ 157 int preferred_minor; /* autorun support */ 158 159 struct kobject kobj; 160 161 /* A device can be in one of three states based on two flags: 162 * Not working: faulty==1 in_sync==0 163 * Fully working: faulty==0 in_sync==1 164 * Working, but not 165 * in sync with array 166 * faulty==0 in_sync==0 167 * 168 * It can never have faulty==1, in_sync==1 169 * This reduces the burden of testing multiple flags in many cases 170 */ 171 172 unsigned long flags; /* bit set of 'enum flag_bits' bits. */ 173 wait_queue_head_t blocked_wait; 174 175 int desc_nr; /* descriptor index in the superblock */ 176 int raid_disk; /* role of device in array */ 177 int new_raid_disk; /* role that the device will have in 178 * the array after a level-change completes. 179 */ 180 int saved_raid_disk; /* role that device used to have in the 181 * array and could again if we did a partial 182 * resync from the bitmap 183 */ 184 union { 185 sector_t recovery_offset;/* If this device has been partially 186 * recovered, this is where we were 187 * up to. 188 */ 189 sector_t journal_tail; /* If this device is a journal device, 190 * this is the journal tail (journal 191 * recovery start point) 192 */ 193 }; 194 195 atomic_t nr_pending; /* number of pending requests. 196 * only maintained for arrays that 197 * support hot removal 198 */ 199 atomic_t read_errors; /* number of consecutive read errors that 200 * we have tried to ignore. 201 */ 202 time64_t last_read_error; /* monotonic time since our 203 * last read error 204 */ 205 atomic_t corrected_errors; /* number of corrected read errors, 206 * for reporting to userspace and storing 207 * in superblock. 208 */ 209 210 struct serial_in_rdev *serial; /* used for raid1 io serialization */ 211 212 struct kernfs_node *sysfs_state; /* handle for 'state' 213 * sysfs entry */ 214 /* handle for 'unacknowledged_bad_blocks' sysfs dentry */ 215 struct kernfs_node *sysfs_unack_badblocks; 216 /* handle for 'bad_blocks' sysfs dentry */ 217 struct kernfs_node *sysfs_badblocks; 218 struct badblocks badblocks; 219 220 struct { 221 short offset; /* Offset from superblock to start of PPL. 222 * Not used by external metadata. */ 223 unsigned int size; /* Size in sectors of the PPL space */ 224 sector_t sector; /* First sector of the PPL space */ 225 } ppl; 226 }; 227 enum flag_bits { 228 Faulty, /* device is known to have a fault */ 229 In_sync, /* device is in_sync with rest of array */ 230 Bitmap_sync, /* ..actually, not quite In_sync. Need a 231 * bitmap-based recovery to get fully in sync. 232 * The bit is only meaningful before device 233 * has been passed to pers->hot_add_disk. 234 */ 235 WriteMostly, /* Avoid reading if at all possible */ 236 AutoDetected, /* added by auto-detect */ 237 Blocked, /* An error occurred but has not yet 238 * been acknowledged by the metadata 239 * handler, so don't allow writes 240 * until it is cleared */ 241 WriteErrorSeen, /* A write error has been seen on this 242 * device 243 */ 244 FaultRecorded, /* Intermediate state for clearing 245 * Blocked. The Fault is/will-be 246 * recorded in the metadata, but that 247 * metadata hasn't been stored safely 248 * on disk yet. 249 */ 250 BlockedBadBlocks, /* A writer is blocked because they 251 * found an unacknowledged bad-block. 252 * This can safely be cleared at any 253 * time, and the writer will re-check. 254 * It may be set at any time, and at 255 * worst the writer will timeout and 256 * re-check. So setting it as 257 * accurately as possible is good, but 258 * not absolutely critical. 259 */ 260 WantReplacement, /* This device is a candidate to be 261 * hot-replaced, either because it has 262 * reported some faults, or because 263 * of explicit request. 264 */ 265 Replacement, /* This device is a replacement for 266 * a want_replacement device with same 267 * raid_disk number. 268 */ 269 Candidate, /* For clustered environments only: 270 * This device is seen locally but not 271 * by the whole cluster 272 */ 273 Journal, /* This device is used as journal for 274 * raid-5/6. 275 * Usually, this device should be faster 276 * than other devices in the array 277 */ 278 ClusterRemove, 279 ExternalBbl, /* External metadata provides bad 280 * block management for a disk 281 */ 282 FailFast, /* Minimal retries should be attempted on 283 * this device, so use REQ_FAILFAST_DEV. 284 * Also don't try to repair failed reads. 285 * It is expects that no bad block log 286 * is present. 287 */ 288 LastDev, /* Seems to be the last working dev as 289 * it didn't fail, so don't use FailFast 290 * any more for metadata 291 */ 292 CollisionCheck, /* 293 * check if there is collision between raid1 294 * serial bios. 295 */ 296 Nonrot, /* non-rotational device (SSD) */ 297 }; 298 299 static inline int is_badblock(struct md_rdev *rdev, sector_t s, sector_t sectors, 300 sector_t *first_bad, sector_t *bad_sectors) 301 { 302 if (unlikely(rdev->badblocks.count)) { 303 int rv = badblocks_check(&rdev->badblocks, rdev->data_offset + s, 304 sectors, 305 first_bad, bad_sectors); 306 if (rv) 307 *first_bad -= rdev->data_offset; 308 return rv; 309 } 310 return 0; 311 } 312 313 static inline int rdev_has_badblock(struct md_rdev *rdev, sector_t s, 314 int sectors) 315 { 316 sector_t first_bad; 317 sector_t bad_sectors; 318 319 return is_badblock(rdev, s, sectors, &first_bad, &bad_sectors); 320 } 321 322 extern bool rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors, 323 int is_new); 324 extern void rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors, 325 int is_new); 326 struct md_cluster_info; 327 struct md_cluster_operations; 328 329 /** 330 * enum mddev_flags - md device flags. 331 * @MD_ARRAY_FIRST_USE: First use of array, needs initialization. 332 * @MD_CLOSING: If set, we are closing the array, do not open it then. 333 * @MD_JOURNAL_CLEAN: A raid with journal is already clean. 334 * @MD_HAS_JOURNAL: The raid array has journal feature set. 335 * @MD_CLUSTER_RESYNC_LOCKED: cluster raid only, which means node, already took 336 * resync lock, need to release the lock. 337 * @MD_FAILFAST_SUPPORTED: Using MD_FAILFAST on metadata writes is supported as 338 * calls to md_error() will never cause the array to 339 * become failed. 340 * @MD_HAS_PPL: The raid array has PPL feature set. 341 * @MD_HAS_MULTIPLE_PPLS: The raid array has multiple PPLs feature set. 342 * @MD_NOT_READY: do_md_run() is active, so 'array_state', ust not report that 343 * array is ready yet. 344 * @MD_BROKEN: This is used to stop writes and mark array as failed. 345 * @MD_DELETED: This device is being deleted 346 * @MD_HAS_SUPERBLOCK: There is persistence sb in member disks. 347 * @MD_FAILLAST_DEV: Allow last rdev to be removed. 348 * @MD_SERIALIZE_POLICY: Enforce write IO is not reordered, just used by raid1. 349 * @MD_DM_SUSPENDING: This DM raid device is suspending. 350 * 351 * change UNSUPPORTED_MDDEV_FLAGS for each array type if new flag is added 352 */ 353 enum mddev_flags { 354 MD_ARRAY_FIRST_USE, 355 MD_CLOSING, 356 MD_JOURNAL_CLEAN, 357 MD_HAS_JOURNAL, 358 MD_CLUSTER_RESYNC_LOCKED, 359 MD_FAILFAST_SUPPORTED, 360 MD_HAS_PPL, 361 MD_HAS_MULTIPLE_PPLS, 362 MD_NOT_READY, 363 MD_BROKEN, 364 MD_DO_DELETE, 365 MD_DELETED, 366 MD_HAS_SUPERBLOCK, 367 MD_FAILLAST_DEV, 368 MD_SERIALIZE_POLICY, 369 MD_DM_SUSPENDING, 370 }; 371 372 enum mddev_sb_flags { 373 MD_SB_CHANGE_DEVS, /* Some device status has changed */ 374 MD_SB_CHANGE_CLEAN, /* transition to or from 'clean' */ 375 MD_SB_CHANGE_PENDING, /* switch from 'clean' to 'active' in progress */ 376 MD_SB_NEED_REWRITE, /* metadata write needs to be repeated */ 377 }; 378 379 #define NR_SERIAL_INFOS 8 380 /* record current range of serialize IOs */ 381 struct serial_info { 382 struct rb_node node; 383 sector_t start; /* start sector of rb node */ 384 sector_t last; /* end sector of rb node */ 385 sector_t wnode_start; /* address of waiting nodes on the same list */ 386 sector_t _subtree_last; /* highest sector in subtree of rb node */ 387 struct list_head list_node; 388 struct list_head waiters; 389 struct completion ready; 390 }; 391 392 /* 393 * mddev->curr_resync stores the current sector of the resync but 394 * also has some overloaded values. 395 */ 396 enum { 397 /* No resync in progress */ 398 MD_RESYNC_NONE = 0, 399 /* Yielded to allow another conflicting resync to commence */ 400 MD_RESYNC_YIELDED = 1, 401 /* Delayed to check that there is no conflict with another sync */ 402 MD_RESYNC_DELAYED = 2, 403 /* Any value greater than or equal to this is in an active resync */ 404 MD_RESYNC_ACTIVE = 3, 405 }; 406 407 struct mddev { 408 void *private; 409 struct md_personality *pers; 410 dev_t unit; 411 int md_minor; 412 struct list_head disks; 413 unsigned long flags; 414 unsigned long sb_flags; 415 416 int suspended; 417 struct mutex suspend_mutex; 418 struct percpu_ref active_io; 419 int ro; 420 int sysfs_active; /* set when sysfs deletes 421 * are happening, so run/ 422 * takeover/stop are not safe 423 */ 424 struct gendisk *gendisk; /* mdraid gendisk */ 425 struct gendisk *dm_gendisk; /* dm-raid gendisk */ 426 427 struct kobject kobj; 428 int hold_active; 429 #define UNTIL_IOCTL 1 430 #define UNTIL_STOP 2 431 432 /* Superblock information */ 433 int major_version, 434 minor_version, 435 patch_version; 436 int persistent; 437 int external; /* metadata is 438 * managed externally */ 439 char metadata_type[17]; /* externally set*/ 440 int chunk_sectors; 441 time64_t ctime, utime; 442 int level, layout; 443 char clevel[16]; 444 int raid_disks; 445 int max_disks; 446 sector_t dev_sectors; /* used size of 447 * component devices */ 448 sector_t array_sectors; /* exported array size */ 449 int external_size; /* size managed 450 * externally */ 451 unsigned int logical_block_size; 452 __u64 events; 453 /* If the last 'event' was simply a clean->dirty transition, and 454 * we didn't write it to the spares, then it is safe and simple 455 * to just decrement the event count on a dirty->clean transition. 456 * So we record that possibility here. 457 */ 458 int can_decrease_events; 459 460 char uuid[16]; 461 462 /* If the array is being reshaped, we need to record the 463 * new shape and an indication of where we are up to. 464 * This is written to the superblock. 465 * If reshape_position is MaxSector, then no reshape is happening (yet). 466 */ 467 sector_t reshape_position; 468 int delta_disks, new_level, new_layout; 469 int new_chunk_sectors; 470 int reshape_backwards; 471 472 struct md_thread __rcu *thread; /* management thread */ 473 struct md_thread __rcu *sync_thread; /* doing resync or reconstruct */ 474 475 /* 476 * Set when a sync operation is started. It holds this value even 477 * when the sync thread is "frozen" (interrupted) or "idle" (stopped 478 * or finished). It is overwritten when a new sync operation is begun. 479 */ 480 enum sync_action last_sync_action; 481 sector_t curr_resync; /* last block scheduled */ 482 /* As resync requests can complete out of order, we cannot easily track 483 * how much resync has been completed. So we occasionally pause until 484 * everything completes, then set curr_resync_completed to curr_resync. 485 * As such it may be well behind the real resync mark, but it is a value 486 * we are certain of. 487 */ 488 sector_t curr_resync_completed; 489 unsigned long resync_mark; /* a recent timestamp */ 490 sector_t resync_mark_cnt;/* blocks written at resync_mark */ 491 sector_t curr_mark_cnt; /* blocks scheduled now */ 492 493 sector_t resync_max_sectors; /* may be set by personality */ 494 495 atomic64_t resync_mismatches; /* count of sectors where 496 * parity/replica mismatch found 497 */ 498 499 /* allow user-space to request suspension of IO to regions of the array */ 500 sector_t suspend_lo; 501 sector_t suspend_hi; 502 /* if zero, use the system-wide default */ 503 int sync_speed_min; 504 int sync_speed_max; 505 int sync_io_depth; 506 507 /* resync even though the same disks are shared among md-devices */ 508 int parallel_resync; 509 510 int ok_start_degraded; 511 512 unsigned long recovery; 513 514 int in_sync; /* know to not need resync */ 515 /* 'open_mutex' avoids races between 'md_open' and 'do_md_stop', so 516 * that we are never stopping an array while it is open. 517 * 'reconfig_mutex' protects all other reconfiguration. 518 * These locks are separate due to conflicting interactions 519 * with disk->open_mutex. 520 * Lock ordering is: 521 * reconfig_mutex -> disk->open_mutex 522 * disk->open_mutex -> open_mutex: e.g. __blkdev_get -> md_open 523 */ 524 struct mutex open_mutex; 525 struct mutex reconfig_mutex; 526 atomic_t active; /* general refcount */ 527 atomic_t openers; /* number of active opens */ 528 529 int changed; /* True if we might need to 530 * reread partition info */ 531 int degraded; /* whether md should consider 532 * adding a spare 533 */ 534 535 unsigned long normal_io_events; /* IO event timestamp */ 536 atomic_t recovery_active; /* blocks scheduled, but not written */ 537 wait_queue_head_t recovery_wait; 538 sector_t resync_offset; 539 sector_t resync_min; /* user requested sync 540 * starts here */ 541 sector_t resync_max; /* resync should pause 542 * when it gets here */ 543 544 struct kernfs_node *sysfs_state; /* handle for 'array_state' 545 * file in sysfs. 546 */ 547 struct kernfs_node *sysfs_action; /* handle for 'sync_action' */ 548 struct kernfs_node *sysfs_completed; /*handle for 'sync_completed' */ 549 struct kernfs_node *sysfs_degraded; /*handle for 'degraded' */ 550 struct kernfs_node *sysfs_level; /*handle for 'level' */ 551 552 /* used for delayed sysfs removal */ 553 struct work_struct del_work; 554 /* used for register new sync thread */ 555 struct work_struct sync_work; 556 557 /* "lock" protects: 558 * flush_bio transition from NULL to !NULL 559 * rdev superblocks, events 560 * clearing MD_CHANGE_* 561 * in_sync - and related safemode and MD_CHANGE changes 562 * pers (also protected by reconfig_mutex and pending IO). 563 * clearing ->bitmap 564 * clearing ->bitmap_info.file 565 * changing ->resync_{min,max} 566 * setting MD_RECOVERY_RUNNING (which interacts with resync_{min,max}) 567 */ 568 spinlock_t lock; 569 wait_queue_head_t sb_wait; /* for waiting on superblock updates */ 570 atomic_t pending_writes; /* number of active superblock writes */ 571 572 unsigned int safemode; /* if set, update "clean" superblock 573 * when no writes pending. 574 */ 575 unsigned int safemode_delay; 576 struct timer_list safemode_timer; 577 struct percpu_ref writes_pending; 578 int sync_checkers; /* # of threads checking writes_pending */ 579 580 enum md_submodule_id bitmap_id; 581 void *bitmap; /* the bitmap for the device */ 582 struct bitmap_operations *bitmap_ops; 583 struct { 584 struct file *file; /* the bitmap file */ 585 loff_t offset; /* offset from superblock of 586 * start of bitmap. May be 587 * negative, but not '0' 588 * For external metadata, offset 589 * from start of device. 590 */ 591 unsigned long space; /* space available at this offset */ 592 loff_t default_offset; /* this is the offset to use when 593 * hot-adding a bitmap. It should 594 * eventually be settable by sysfs. 595 */ 596 unsigned long default_space; /* space available at 597 * default offset */ 598 struct mutex mutex; 599 unsigned long chunksize; 600 unsigned long daemon_sleep; /* how many jiffies between updates? */ 601 unsigned long max_write_behind; /* write-behind mode */ 602 int external; 603 int nodes; /* Maximum number of nodes in the cluster */ 604 char cluster_name[64]; /* Name of the cluster */ 605 } bitmap_info; 606 607 atomic_t max_corr_read_errors; /* max read retries */ 608 struct list_head all_mddevs; 609 610 const struct attribute_group *to_remove; 611 612 struct bio_set bio_set; 613 struct bio_set sync_set; /* for sync operations like 614 * metadata and bitmap writes 615 */ 616 struct bio_set io_clone_set; 617 618 struct work_struct event_work; /* used by dm to report failure event */ 619 mempool_t *serial_info_pool; 620 void (*sync_super)(struct mddev *mddev, struct md_rdev *rdev); 621 struct md_cluster_info *cluster_info; 622 struct md_cluster_operations *cluster_ops; 623 unsigned int good_device_nr; /* good device num within cluster raid */ 624 unsigned int noio_flag; /* for memalloc scope API */ 625 626 /* 627 * Temporarily store rdev that will be finally removed when 628 * reconfig_mutex is unlocked, protected by reconfig_mutex. 629 */ 630 struct list_head deleting; 631 632 /* The sequence number for sync thread */ 633 atomic_t sync_seq; 634 }; 635 636 enum recovery_flags { 637 /* flags for sync thread running status */ 638 639 /* 640 * set when one of sync action is set and new sync thread need to be 641 * registered, or just add/remove spares from conf. 642 */ 643 MD_RECOVERY_NEEDED, 644 /* sync thread is running, or about to be started */ 645 MD_RECOVERY_RUNNING, 646 /* sync thread needs to be aborted for some reason */ 647 MD_RECOVERY_INTR, 648 /* sync thread is done and is waiting to be unregistered */ 649 MD_RECOVERY_DONE, 650 /* running sync thread must abort immediately, and not restart */ 651 MD_RECOVERY_FROZEN, 652 /* waiting for pers->start() to finish */ 653 MD_RECOVERY_WAIT, 654 655 /* flags determines sync action, see details in enum sync_action */ 656 657 /* if just this flag is set, action is resync. */ 658 MD_RECOVERY_SYNC, 659 /* 660 * paired with MD_RECOVERY_SYNC, if MD_RECOVERY_CHECK is not set, 661 * action is repair, means user requested resync. 662 */ 663 MD_RECOVERY_REQUESTED, 664 /* 665 * paired with MD_RECOVERY_SYNC and MD_RECOVERY_REQUESTED, action is 666 * check. 667 */ 668 MD_RECOVERY_CHECK, 669 /* recovery, or need to try it */ 670 MD_RECOVERY_RECOVER, 671 /* reshape */ 672 MD_RECOVERY_RESHAPE, 673 /* remote node is running resync thread */ 674 MD_RESYNCING_REMOTE, 675 /* raid456 lazy initial recover */ 676 MD_RECOVERY_LAZY_RECOVER, 677 }; 678 679 enum md_ro_state { 680 MD_RDWR, 681 MD_RDONLY, 682 MD_AUTO_READ, 683 MD_MAX_STATE 684 }; 685 686 static inline bool md_is_rdwr(struct mddev *mddev) 687 { 688 return (mddev->ro == MD_RDWR); 689 } 690 691 static inline bool reshape_interrupted(struct mddev *mddev) 692 { 693 /* reshape never start */ 694 if (mddev->reshape_position == MaxSector) 695 return false; 696 697 /* interrupted */ 698 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) 699 return true; 700 701 /* running reshape will be interrupted soon. */ 702 if (test_bit(MD_RECOVERY_WAIT, &mddev->recovery) || 703 test_bit(MD_RECOVERY_INTR, &mddev->recovery) || 704 test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) 705 return true; 706 707 return false; 708 } 709 710 static inline int __must_check mddev_lock(struct mddev *mddev) 711 { 712 int ret; 713 714 ret = mutex_lock_interruptible(&mddev->reconfig_mutex); 715 716 /* MD_DELETED is set in do_md_stop with reconfig_mutex. 717 * So check it here. 718 */ 719 if (!ret && test_bit(MD_DELETED, &mddev->flags)) { 720 ret = -ENODEV; 721 mutex_unlock(&mddev->reconfig_mutex); 722 } 723 724 return ret; 725 } 726 727 /* Sometimes we need to take the lock in a situation where 728 * failure due to interrupts is not acceptable. 729 * It doesn't need to check MD_DELETED here, the owner which 730 * holds the lock here can't be stopped. And all paths can't 731 * call this function after do_md_stop. 732 */ 733 static inline void mddev_lock_nointr(struct mddev *mddev) 734 { 735 mutex_lock(&mddev->reconfig_mutex); 736 } 737 738 static inline int mddev_trylock(struct mddev *mddev) 739 { 740 int ret; 741 742 ret = mutex_trylock(&mddev->reconfig_mutex); 743 if (ret && test_bit(MD_DELETED, &mddev->flags)) { 744 ret = 0; 745 mutex_unlock(&mddev->reconfig_mutex); 746 } 747 return ret; 748 } 749 extern void mddev_unlock(struct mddev *mddev); 750 751 struct md_personality 752 { 753 struct md_submodule_head head; 754 755 bool __must_check (*make_request)(struct mddev *mddev, struct bio *bio); 756 /* 757 * start up works that do NOT require md_thread. tasks that 758 * requires md_thread should go into start() 759 */ 760 int (*run)(struct mddev *mddev); 761 /* start up works that require md threads */ 762 int (*start)(struct mddev *mddev); 763 void (*free)(struct mddev *mddev, void *priv); 764 void (*status)(struct seq_file *seq, struct mddev *mddev); 765 /* error_handler must set ->faulty and clear ->in_sync 766 * if appropriate, and should abort recovery if needed 767 */ 768 void (*error_handler)(struct mddev *mddev, struct md_rdev *rdev); 769 int (*hot_add_disk) (struct mddev *mddev, struct md_rdev *rdev); 770 int (*hot_remove_disk) (struct mddev *mddev, struct md_rdev *rdev); 771 int (*spare_active) (struct mddev *mddev); 772 sector_t (*sync_request)(struct mddev *mddev, sector_t sector_nr, 773 sector_t max_sector, int *skipped); 774 int (*resize) (struct mddev *mddev, sector_t sectors); 775 sector_t (*size) (struct mddev *mddev, sector_t sectors, int raid_disks); 776 int (*check_reshape) (struct mddev *mddev); 777 int (*start_reshape) (struct mddev *mddev); 778 void (*finish_reshape) (struct mddev *mddev); 779 void (*update_reshape_pos) (struct mddev *mddev); 780 void (*prepare_suspend) (struct mddev *mddev); 781 /* quiesce suspends or resumes internal processing. 782 * 1 - stop new actions and wait for action io to complete 783 * 0 - return to normal behaviour 784 */ 785 void (*quiesce) (struct mddev *mddev, int quiesce); 786 /* takeover is used to transition an array from one 787 * personality to another. The new personality must be able 788 * to handle the data in the current layout. 789 * e.g. 2drive raid1 -> 2drive raid5 790 * ndrive raid5 -> degraded n+1drive raid6 with special layout 791 * If the takeover succeeds, a new 'private' structure is returned. 792 * This needs to be installed and then ->run used to activate the 793 * array. 794 */ 795 void *(*takeover) (struct mddev *mddev); 796 /* Changes the consistency policy of an active array. */ 797 int (*change_consistency_policy)(struct mddev *mddev, const char *buf); 798 /* convert io ranges from array to bitmap */ 799 void (*bitmap_sector)(struct mddev *mddev, sector_t *offset, 800 unsigned long *sectors); 801 }; 802 803 struct md_sysfs_entry { 804 struct attribute attr; 805 ssize_t (*show)(struct mddev *, char *); 806 ssize_t (*store)(struct mddev *, const char *, size_t); 807 }; 808 809 static inline struct kernfs_node *sysfs_get_dirent_safe(struct kernfs_node *sd, char *name) 810 { 811 if (sd) 812 return sysfs_get_dirent(sd, name); 813 return sd; 814 } 815 static inline void sysfs_notify_dirent_safe(struct kernfs_node *sd) 816 { 817 if (sd) 818 sysfs_notify_dirent(sd); 819 } 820 821 static inline char * mdname (struct mddev * mddev) 822 { 823 return mddev->gendisk ? mddev->gendisk->disk_name : "mdX"; 824 } 825 826 static inline int sysfs_link_rdev(struct mddev *mddev, struct md_rdev *rdev) 827 { 828 char nm[20]; 829 if (!test_bit(Replacement, &rdev->flags) && 830 !test_bit(Journal, &rdev->flags) && 831 mddev->kobj.sd) { 832 sprintf(nm, "rd%d", rdev->raid_disk); 833 return sysfs_create_link(&mddev->kobj, &rdev->kobj, nm); 834 } else 835 return 0; 836 } 837 838 static inline void sysfs_unlink_rdev(struct mddev *mddev, struct md_rdev *rdev) 839 { 840 char nm[20]; 841 if (!test_bit(Replacement, &rdev->flags) && 842 !test_bit(Journal, &rdev->flags) && 843 mddev->kobj.sd) { 844 sprintf(nm, "rd%d", rdev->raid_disk); 845 sysfs_remove_link(&mddev->kobj, nm); 846 } 847 } 848 849 /* 850 * iterates through some rdev ringlist. It's safe to remove the 851 * current 'rdev'. Dont touch 'tmp' though. 852 */ 853 #define rdev_for_each_list(rdev, tmp, head) \ 854 list_for_each_entry_safe(rdev, tmp, head, same_set) 855 856 /* 857 * iterates through the 'same array disks' ringlist 858 */ 859 #define rdev_for_each(rdev, mddev) \ 860 list_for_each_entry(rdev, &((mddev)->disks), same_set) 861 862 #define rdev_for_each_safe(rdev, tmp, mddev) \ 863 list_for_each_entry_safe(rdev, tmp, &((mddev)->disks), same_set) 864 865 #define rdev_for_each_rcu(rdev, mddev) \ 866 list_for_each_entry_rcu(rdev, &((mddev)->disks), same_set) 867 868 struct md_thread { 869 void (*run) (struct md_thread *thread); 870 struct mddev *mddev; 871 wait_queue_head_t wqueue; 872 unsigned long flags; 873 struct task_struct *tsk; 874 unsigned long timeout; 875 void *private; 876 }; 877 878 struct md_io_clone { 879 struct mddev *mddev; 880 struct bio *orig_bio; 881 unsigned long start_time; 882 sector_t offset; 883 unsigned long sectors; 884 enum stat_group rw; 885 struct bio bio_clone; 886 }; 887 888 #define THREAD_WAKEUP 0 889 890 #define md_wakeup_thread(thread) do { \ 891 rcu_read_lock(); \ 892 __md_wakeup_thread(thread); \ 893 rcu_read_unlock(); \ 894 } while (0) 895 896 static inline void safe_put_page(struct page *p) 897 { 898 if (p) put_page(p); 899 } 900 901 int register_md_submodule(struct md_submodule_head *msh); 902 void unregister_md_submodule(struct md_submodule_head *msh); 903 904 extern struct md_thread *md_register_thread( 905 void (*run)(struct md_thread *thread), 906 struct mddev *mddev, 907 const char *name); 908 extern void md_unregister_thread(struct mddev *mddev, struct md_thread __rcu **threadp); 909 extern void __md_wakeup_thread(struct md_thread __rcu *thread); 910 extern void md_check_recovery(struct mddev *mddev); 911 extern void md_reap_sync_thread(struct mddev *mddev); 912 extern enum sync_action md_sync_action(struct mddev *mddev); 913 extern enum sync_action md_sync_action_by_name(const char *page); 914 extern const char *md_sync_action_name(enum sync_action action); 915 extern void md_write_start(struct mddev *mddev, struct bio *bi); 916 extern void md_write_inc(struct mddev *mddev, struct bio *bi); 917 extern void md_write_end(struct mddev *mddev); 918 extern void md_done_sync(struct mddev *mddev, int blocks); 919 extern void md_sync_error(struct mddev *mddev); 920 extern void md_error(struct mddev *mddev, struct md_rdev *rdev); 921 extern void md_finish_reshape(struct mddev *mddev); 922 void md_submit_discard_bio(struct mddev *mddev, struct md_rdev *rdev, 923 struct bio *bio, sector_t start, sector_t size); 924 void md_account_bio(struct mddev *mddev, struct bio **bio); 925 926 extern bool __must_check md_flush_request(struct mddev *mddev, struct bio *bio); 927 void md_write_metadata(struct mddev *mddev, struct md_rdev *rdev, 928 sector_t sector, int size, struct page *page, 929 unsigned int offset); 930 extern int md_super_wait(struct mddev *mddev); 931 extern int sync_page_io(struct md_rdev *rdev, sector_t sector, int size, 932 struct page *page, blk_opf_t opf, bool metadata_op); 933 extern void md_do_sync(struct md_thread *thread); 934 extern void md_new_event(void); 935 extern void md_allow_write(struct mddev *mddev); 936 extern void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev); 937 extern void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors); 938 extern int md_check_no_bitmap(struct mddev *mddev); 939 bool mddev_set_bitmap_ops_nosysfs(struct mddev *mddev); 940 int md_bitmap_create_nosysfs(struct mddev *mddev); 941 void md_bitmap_destroy_nosysfs(struct mddev *mddev); 942 extern int md_integrity_register(struct mddev *mddev); 943 extern int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale); 944 945 extern int mddev_init(struct mddev *mddev); 946 extern void mddev_destroy(struct mddev *mddev); 947 void md_init_stacking_limits(struct queue_limits *lim); 948 struct mddev *md_alloc(dev_t dev, char *name); 949 void mddev_put(struct mddev *mddev); 950 extern int md_run(struct mddev *mddev); 951 extern int md_start(struct mddev *mddev); 952 extern void md_stop(struct mddev *mddev); 953 extern void md_stop_writes(struct mddev *mddev); 954 extern int md_rdev_init(struct md_rdev *rdev); 955 extern void md_rdev_clear(struct md_rdev *rdev); 956 957 extern bool md_handle_request(struct mddev *mddev, struct bio *bio); 958 extern int mddev_suspend(struct mddev *mddev, bool interruptible); 959 extern void mddev_resume(struct mddev *mddev); 960 extern void md_idle_sync_thread(struct mddev *mddev); 961 extern void md_frozen_sync_thread(struct mddev *mddev); 962 extern void md_unfrozen_sync_thread(struct mddev *mddev); 963 964 extern void md_update_sb(struct mddev *mddev, int force); 965 extern void mddev_create_serial_pool(struct mddev *mddev, struct md_rdev *rdev); 966 extern void mddev_destroy_serial_pool(struct mddev *mddev, 967 struct md_rdev *rdev); 968 struct md_rdev *md_find_rdev_nr_rcu(struct mddev *mddev, int nr); 969 struct md_rdev *md_find_rdev_rcu(struct mddev *mddev, dev_t dev); 970 971 static inline bool is_rdev_broken(struct md_rdev *rdev) 972 { 973 return !disk_live(rdev->bdev->bd_disk); 974 } 975 976 static inline void rdev_dec_pending(struct md_rdev *rdev, struct mddev *mddev) 977 { 978 int faulty = test_bit(Faulty, &rdev->flags); 979 if (atomic_dec_and_test(&rdev->nr_pending) && faulty) { 980 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); 981 md_wakeup_thread(mddev->thread); 982 } 983 } 984 985 static inline int mddev_is_clustered(struct mddev *mddev) 986 { 987 return mddev->cluster_info && mddev->bitmap_info.nodes > 1; 988 } 989 990 /* clear unsupported mddev_flags */ 991 static inline void mddev_clear_unsupported_flags(struct mddev *mddev, 992 unsigned long unsupported_flags) 993 { 994 mddev->flags &= ~unsupported_flags; 995 } 996 997 static inline void mddev_check_write_zeroes(struct mddev *mddev, struct bio *bio) 998 { 999 if (bio_op(bio) == REQ_OP_WRITE_ZEROES && 1000 !bio->bi_bdev->bd_disk->queue->limits.max_write_zeroes_sectors) 1001 mddev->gendisk->queue->limits.max_write_zeroes_sectors = 0; 1002 } 1003 1004 static inline int mddev_suspend_and_lock(struct mddev *mddev) 1005 { 1006 int ret; 1007 1008 ret = mddev_suspend(mddev, true); 1009 if (ret) 1010 return ret; 1011 1012 ret = mddev_lock(mddev); 1013 if (ret) 1014 mddev_resume(mddev); 1015 1016 return ret; 1017 } 1018 1019 static inline void mddev_suspend_and_lock_nointr(struct mddev *mddev) 1020 { 1021 mddev_suspend(mddev, false); 1022 mddev_lock_nointr(mddev); 1023 } 1024 1025 static inline void mddev_unlock_and_resume(struct mddev *mddev) 1026 { 1027 mddev_unlock(mddev); 1028 mddev_resume(mddev); 1029 } 1030 1031 struct mdu_array_info_s; 1032 struct mdu_disk_info_s; 1033 1034 extern int mdp_major; 1035 void md_autostart_arrays(int part); 1036 int md_set_array_info(struct mddev *mddev, struct mdu_array_info_s *info); 1037 int md_add_new_disk(struct mddev *mddev, struct mdu_disk_info_s *info); 1038 int do_md_run(struct mddev *mddev); 1039 #define MDDEV_STACK_INTEGRITY (1u << 0) 1040 int mddev_stack_rdev_limits(struct mddev *mddev, struct queue_limits *lim, 1041 unsigned int flags); 1042 int mddev_stack_new_rdev(struct mddev *mddev, struct md_rdev *rdev); 1043 void mddev_update_io_opt(struct mddev *mddev, unsigned int nr_stripes); 1044 1045 extern const struct block_device_operations md_fops; 1046 1047 static inline bool md_cloned_bio(struct mddev *mddev, struct bio *bio) 1048 { 1049 return bio->bi_pool == &mddev->io_clone_set; 1050 } 1051 1052 /* 1053 * MD devices can be used undeneath by DM, in which case ->gendisk is NULL. 1054 */ 1055 static inline bool mddev_is_dm(struct mddev *mddev) 1056 { 1057 return !mddev->gendisk; 1058 } 1059 1060 static inline bool raid_is_456(struct mddev *mddev) 1061 { 1062 return mddev->level == ID_RAID4 || mddev->level == ID_RAID5 || 1063 mddev->level == ID_RAID6; 1064 } 1065 1066 static inline void mddev_trace_remap(struct mddev *mddev, struct bio *bio, 1067 sector_t sector) 1068 { 1069 if (!mddev_is_dm(mddev)) 1070 trace_block_bio_remap(bio, disk_devt(mddev->gendisk), sector); 1071 } 1072 1073 static inline bool rdev_blocked(struct md_rdev *rdev) 1074 { 1075 /* 1076 * Blocked will be set by error handler and cleared by daemon after 1077 * updating superblock, meanwhile write IO should be blocked to prevent 1078 * reading old data after power failure. 1079 */ 1080 if (test_bit(Blocked, &rdev->flags)) 1081 return true; 1082 1083 /* 1084 * Faulty device should not be accessed anymore, there is no need to 1085 * wait for bad block to be acknowledged. 1086 */ 1087 if (test_bit(Faulty, &rdev->flags)) 1088 return false; 1089 1090 /* rdev is blocked by badblocks. */ 1091 if (test_bit(BlockedBadBlocks, &rdev->flags)) 1092 return true; 1093 1094 return false; 1095 } 1096 1097 #define mddev_add_trace_msg(mddev, fmt, args...) \ 1098 do { \ 1099 if (!mddev_is_dm(mddev)) \ 1100 blk_add_trace_msg((mddev)->gendisk->queue, fmt, ##args); \ 1101 } while (0) 1102 1103 #endif /* _MD_MD_H */ 1104