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