md.h 32 KB

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