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