md.h 34 KB

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