xfs_buf.h 12 KB

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