xfs_buf.h 13 KB

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