xfs_buf.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2000-2005 Silicon Graphics, Inc.
  4. * All Rights Reserved.
  5. */
  6. #ifndef __XFS_BUF_H__
  7. #define __XFS_BUF_H__
  8. #include <linux/list.h>
  9. #include <linux/types.h>
  10. #include <linux/spinlock.h>
  11. #include <linux/mm.h>
  12. #include <linux/fs.h>
  13. #include <linux/dax.h>
  14. #include <linux/uio.h>
  15. #include <linux/list_lru.h>
  16. extern struct kmem_cache *xfs_buf_cache;
  17. /*
  18. * Base types
  19. */
  20. struct xfs_buf;
  21. #define XFS_BUF_DADDR_NULL ((xfs_daddr_t) (-1LL))
  22. #define XBF_READ (1u << 0) /* buffer intended for reading from device */
  23. #define XBF_WRITE (1u << 1) /* buffer intended for writing to device */
  24. #define XBF_READ_AHEAD (1u << 2) /* asynchronous read-ahead */
  25. #define XBF_NO_IOACCT (1u << 3) /* bypass I/O accounting (non-LRU bufs) */
  26. #define XBF_ASYNC (1u << 4) /* initiator will not wait for completion */
  27. #define XBF_DONE (1u << 5) /* all pages in the buffer uptodate */
  28. #define XBF_STALE (1u << 6) /* buffer has been staled, do not find it */
  29. #define XBF_WRITE_FAIL (1u << 7) /* async writes have failed on this buffer */
  30. /* buffer type flags for write callbacks */
  31. #define _XBF_INODES (1u << 16)/* inode buffer */
  32. #define _XBF_DQUOTS (1u << 17)/* dquot buffer */
  33. #define _XBF_LOGRECOVERY (1u << 18)/* log recovery buffer */
  34. /* flags used only internally */
  35. #define _XBF_PAGES (1u << 20)/* backed by refcounted pages */
  36. #define _XBF_KMEM (1u << 21)/* backed by heap memory */
  37. #define _XBF_DELWRI_Q (1u << 22)/* buffer on a delwri queue */
  38. /* flags used only as arguments to access routines */
  39. #define XBF_INCORE (1u << 29)/* lookup only, return if found in cache */
  40. #define XBF_TRYLOCK (1u << 30)/* lock requested, but do not wait */
  41. #define XBF_UNMAPPED (1u << 31)/* do not map the buffer */
  42. typedef unsigned int xfs_buf_flags_t;
  43. #define XFS_BUF_FLAGS \
  44. { XBF_READ, "READ" }, \
  45. { XBF_WRITE, "WRITE" }, \
  46. { XBF_READ_AHEAD, "READ_AHEAD" }, \
  47. { XBF_NO_IOACCT, "NO_IOACCT" }, \
  48. { XBF_ASYNC, "ASYNC" }, \
  49. { XBF_DONE, "DONE" }, \
  50. { XBF_STALE, "STALE" }, \
  51. { XBF_WRITE_FAIL, "WRITE_FAIL" }, \
  52. { _XBF_INODES, "INODES" }, \
  53. { _XBF_DQUOTS, "DQUOTS" }, \
  54. { _XBF_LOGRECOVERY, "LOG_RECOVERY" }, \
  55. { _XBF_PAGES, "PAGES" }, \
  56. { _XBF_KMEM, "KMEM" }, \
  57. { _XBF_DELWRI_Q, "DELWRI_Q" }, \
  58. /* The following interface flags should never be set */ \
  59. { XBF_INCORE, "INCORE" }, \
  60. { XBF_TRYLOCK, "TRYLOCK" }, \
  61. { XBF_UNMAPPED, "UNMAPPED" }
  62. /*
  63. * Internal state flags.
  64. */
  65. #define XFS_BSTATE_DISPOSE (1 << 0) /* buffer being discarded */
  66. #define XFS_BSTATE_IN_FLIGHT (1 << 1) /* I/O in flight */
  67. /*
  68. * The xfs_buftarg contains 2 notions of "sector size" -
  69. *
  70. * 1) The metadata sector size, which is the minimum unit and
  71. * alignment of IO which will be performed by metadata operations.
  72. * 2) The device logical sector size
  73. *
  74. * The first is specified at mkfs time, and is stored on-disk in the
  75. * superblock's sb_sectsize.
  76. *
  77. * The latter is derived from the underlying device, and controls direct IO
  78. * alignment constraints.
  79. */
  80. typedef struct xfs_buftarg {
  81. dev_t bt_dev;
  82. struct block_device *bt_bdev;
  83. struct dax_device *bt_daxdev;
  84. u64 bt_dax_part_off;
  85. struct xfs_mount *bt_mount;
  86. unsigned int bt_meta_sectorsize;
  87. size_t bt_meta_sectormask;
  88. size_t bt_logical_sectorsize;
  89. size_t bt_logical_sectormask;
  90. /* LRU control structures */
  91. struct shrinker bt_shrinker;
  92. struct list_lru bt_lru;
  93. struct percpu_counter bt_io_count;
  94. struct ratelimit_state bt_ioerror_rl;
  95. } xfs_buftarg_t;
  96. #define XB_PAGES 2
  97. struct xfs_buf_map {
  98. xfs_daddr_t bm_bn; /* block number for I/O */
  99. int bm_len; /* size of I/O */
  100. };
  101. #define DEFINE_SINGLE_BUF_MAP(map, blkno, numblk) \
  102. struct xfs_buf_map (map) = { .bm_bn = (blkno), .bm_len = (numblk) };
  103. struct xfs_buf_ops {
  104. char *name;
  105. union {
  106. __be32 magic[2]; /* v4 and v5 on disk magic values */
  107. __be16 magic16[2]; /* v4 and v5 on disk magic values */
  108. };
  109. void (*verify_read)(struct xfs_buf *);
  110. void (*verify_write)(struct xfs_buf *);
  111. xfs_failaddr_t (*verify_struct)(struct xfs_buf *bp);
  112. };
  113. struct xfs_buf {
  114. /*
  115. * first cacheline holds all the fields needed for an uncontended cache
  116. * hit to be fully processed. The semaphore straddles the cacheline
  117. * boundary, but the counter and lock sits on the first cacheline,
  118. * which is the only bit that is touched if we hit the semaphore
  119. * fast-path on locking.
  120. */
  121. struct rhash_head b_rhash_head; /* pag buffer hash node */
  122. xfs_daddr_t b_rhash_key; /* buffer cache index */
  123. int b_length; /* size of buffer in BBs */
  124. atomic_t b_hold; /* reference count */
  125. atomic_t b_lru_ref; /* lru reclaim ref count */
  126. xfs_buf_flags_t b_flags; /* status flags */
  127. struct semaphore b_sema; /* semaphore for lockables */
  128. /*
  129. * concurrent access to b_lru and b_lru_flags are protected by
  130. * bt_lru_lock and not by b_sema
  131. */
  132. struct list_head b_lru; /* lru list */
  133. spinlock_t b_lock; /* internal state lock */
  134. unsigned int b_state; /* internal state flags */
  135. int b_io_error; /* internal IO error state */
  136. wait_queue_head_t b_waiters; /* unpin waiters */
  137. struct list_head b_list;
  138. struct xfs_perag *b_pag; /* contains rbtree root */
  139. struct xfs_mount *b_mount;
  140. struct xfs_buftarg *b_target; /* buffer target (device) */
  141. void *b_addr; /* virtual address of buffer */
  142. struct work_struct b_ioend_work;
  143. struct completion b_iowait; /* queue for I/O waiters */
  144. struct xfs_buf_log_item *b_log_item;
  145. struct list_head b_li_list; /* Log items list head */
  146. struct xfs_trans *b_transp;
  147. struct page **b_pages; /* array of page pointers */
  148. struct page *b_page_array[XB_PAGES]; /* inline pages */
  149. struct xfs_buf_map *b_maps; /* compound buffer map */
  150. struct xfs_buf_map __b_map; /* inline compound buffer map */
  151. int b_map_count;
  152. atomic_t b_pin_count; /* pin count */
  153. atomic_t b_io_remaining; /* #outstanding I/O requests */
  154. unsigned int b_page_count; /* size of page array */
  155. unsigned int b_offset; /* page offset of b_addr,
  156. only for _XBF_KMEM buffers */
  157. int b_error; /* error code on I/O */
  158. /*
  159. * async write failure retry count. Initialised to zero on the first
  160. * failure, then when it exceeds the maximum configured without a
  161. * success the write is considered to be failed permanently and the
  162. * iodone handler will take appropriate action.
  163. *
  164. * For retry timeouts, we record the jiffie of the first failure. This
  165. * means that we can change the retry timeout for buffers already under
  166. * I/O and thus avoid getting stuck in a retry loop with a long timeout.
  167. *
  168. * last_error is used to ensure that we are getting repeated errors, not
  169. * different errors. e.g. a block device might change ENOSPC to EIO when
  170. * a failure timeout occurs, so we want to re-initialise the error
  171. * retry behaviour appropriately when that happens.
  172. */
  173. int b_retries;
  174. unsigned long b_first_retry_time; /* in jiffies */
  175. int b_last_error;
  176. const struct xfs_buf_ops *b_ops;
  177. struct rcu_head b_rcu;
  178. };
  179. /* Finding and Reading Buffers */
  180. int xfs_buf_get_map(struct xfs_buftarg *target, struct xfs_buf_map *map,
  181. int nmaps, xfs_buf_flags_t flags, struct xfs_buf **bpp);
  182. int xfs_buf_read_map(struct xfs_buftarg *target, struct xfs_buf_map *map,
  183. int nmaps, xfs_buf_flags_t flags, struct xfs_buf **bpp,
  184. const struct xfs_buf_ops *ops, xfs_failaddr_t fa);
  185. void xfs_buf_readahead_map(struct xfs_buftarg *target,
  186. struct xfs_buf_map *map, int nmaps,
  187. const struct xfs_buf_ops *ops);
  188. static inline int
  189. xfs_buf_incore(
  190. struct xfs_buftarg *target,
  191. xfs_daddr_t blkno,
  192. size_t numblks,
  193. xfs_buf_flags_t flags,
  194. struct xfs_buf **bpp)
  195. {
  196. DEFINE_SINGLE_BUF_MAP(map, blkno, numblks);
  197. return xfs_buf_get_map(target, &map, 1, XBF_INCORE | flags, bpp);
  198. }
  199. static inline int
  200. xfs_buf_get(
  201. struct xfs_buftarg *target,
  202. xfs_daddr_t blkno,
  203. size_t numblks,
  204. struct xfs_buf **bpp)
  205. {
  206. DEFINE_SINGLE_BUF_MAP(map, blkno, numblks);
  207. return xfs_buf_get_map(target, &map, 1, 0, bpp);
  208. }
  209. static inline int
  210. xfs_buf_read(
  211. struct xfs_buftarg *target,
  212. xfs_daddr_t blkno,
  213. size_t numblks,
  214. xfs_buf_flags_t flags,
  215. struct xfs_buf **bpp,
  216. const struct xfs_buf_ops *ops)
  217. {
  218. DEFINE_SINGLE_BUF_MAP(map, blkno, numblks);
  219. return xfs_buf_read_map(target, &map, 1, flags, bpp, ops,
  220. __builtin_return_address(0));
  221. }
  222. static inline void
  223. xfs_buf_readahead(
  224. struct xfs_buftarg *target,
  225. xfs_daddr_t blkno,
  226. size_t numblks,
  227. const struct xfs_buf_ops *ops)
  228. {
  229. DEFINE_SINGLE_BUF_MAP(map, blkno, numblks);
  230. return xfs_buf_readahead_map(target, &map, 1, ops);
  231. }
  232. int xfs_buf_get_uncached(struct xfs_buftarg *target, size_t numblks,
  233. xfs_buf_flags_t flags, struct xfs_buf **bpp);
  234. int xfs_buf_read_uncached(struct xfs_buftarg *target, xfs_daddr_t daddr,
  235. size_t numblks, xfs_buf_flags_t flags, struct xfs_buf **bpp,
  236. const struct xfs_buf_ops *ops);
  237. int _xfs_buf_read(struct xfs_buf *bp, xfs_buf_flags_t flags);
  238. void xfs_buf_hold(struct xfs_buf *bp);
  239. /* Releasing Buffers */
  240. extern void xfs_buf_rele(struct xfs_buf *);
  241. /* Locking and Unlocking Buffers */
  242. extern int xfs_buf_trylock(struct xfs_buf *);
  243. extern void xfs_buf_lock(struct xfs_buf *);
  244. extern void xfs_buf_unlock(struct xfs_buf *);
  245. #define xfs_buf_islocked(bp) \
  246. ((bp)->b_sema.count <= 0)
  247. static inline void xfs_buf_relse(struct xfs_buf *bp)
  248. {
  249. xfs_buf_unlock(bp);
  250. xfs_buf_rele(bp);
  251. }
  252. /* Buffer Read and Write Routines */
  253. extern int xfs_bwrite(struct xfs_buf *bp);
  254. extern void __xfs_buf_ioerror(struct xfs_buf *bp, int error,
  255. xfs_failaddr_t failaddr);
  256. #define xfs_buf_ioerror(bp, err) __xfs_buf_ioerror((bp), (err), __this_address)
  257. extern void xfs_buf_ioerror_alert(struct xfs_buf *bp, xfs_failaddr_t fa);
  258. void xfs_buf_ioend_fail(struct xfs_buf *);
  259. void xfs_buf_zero(struct xfs_buf *bp, size_t boff, size_t bsize);
  260. void __xfs_buf_mark_corrupt(struct xfs_buf *bp, xfs_failaddr_t fa);
  261. #define xfs_buf_mark_corrupt(bp) __xfs_buf_mark_corrupt((bp), __this_address)
  262. /* Buffer Utility Routines */
  263. extern void *xfs_buf_offset(struct xfs_buf *, size_t);
  264. extern void xfs_buf_stale(struct xfs_buf *bp);
  265. /* Delayed Write Buffer Routines */
  266. extern void xfs_buf_delwri_cancel(struct list_head *);
  267. extern bool xfs_buf_delwri_queue(struct xfs_buf *, struct list_head *);
  268. extern int xfs_buf_delwri_submit(struct list_head *);
  269. extern int xfs_buf_delwri_submit_nowait(struct list_head *);
  270. extern int xfs_buf_delwri_pushbuf(struct xfs_buf *, struct list_head *);
  271. static inline xfs_daddr_t xfs_buf_daddr(struct xfs_buf *bp)
  272. {
  273. return bp->b_maps[0].bm_bn;
  274. }
  275. void xfs_buf_set_ref(struct xfs_buf *bp, int lru_ref);
  276. /*
  277. * If the buffer is already on the LRU, do nothing. Otherwise set the buffer
  278. * up with a reference count of 0 so it will be tossed from the cache when
  279. * released.
  280. */
  281. static inline void xfs_buf_oneshot(struct xfs_buf *bp)
  282. {
  283. if (!list_empty(&bp->b_lru) || atomic_read(&bp->b_lru_ref) > 1)
  284. return;
  285. atomic_set(&bp->b_lru_ref, 0);
  286. }
  287. static inline int xfs_buf_ispinned(struct xfs_buf *bp)
  288. {
  289. return atomic_read(&bp->b_pin_count);
  290. }
  291. static inline int
  292. xfs_buf_verify_cksum(struct xfs_buf *bp, unsigned long cksum_offset)
  293. {
  294. return xfs_verify_cksum(bp->b_addr, BBTOB(bp->b_length),
  295. cksum_offset);
  296. }
  297. static inline void
  298. xfs_buf_update_cksum(struct xfs_buf *bp, unsigned long cksum_offset)
  299. {
  300. xfs_update_cksum(bp->b_addr, BBTOB(bp->b_length),
  301. cksum_offset);
  302. }
  303. /*
  304. * Handling of buftargs.
  305. */
  306. struct xfs_buftarg *xfs_alloc_buftarg(struct xfs_mount *mp,
  307. struct block_device *bdev);
  308. extern void xfs_free_buftarg(struct xfs_buftarg *);
  309. extern void xfs_buftarg_wait(struct xfs_buftarg *);
  310. extern void xfs_buftarg_drain(struct xfs_buftarg *);
  311. extern int xfs_setsize_buftarg(struct xfs_buftarg *, unsigned int);
  312. #define xfs_getsize_buftarg(buftarg) block_size((buftarg)->bt_bdev)
  313. #define xfs_readonly_buftarg(buftarg) bdev_read_only((buftarg)->bt_bdev)
  314. int xfs_buf_reverify(struct xfs_buf *bp, const struct xfs_buf_ops *ops);
  315. bool xfs_verify_magic(struct xfs_buf *bp, __be32 dmagic);
  316. bool xfs_verify_magic16(struct xfs_buf *bp, __be16 dmagic);
  317. #endif /* __XFS_BUF_H__ */