]>
Commit | Line | Data |
---|---|---|
0b61f8a4 | 1 | // SPDX-License-Identifier: GPL-2.0 |
1da177e4 | 2 | /* |
7b718769 NS |
3 | * Copyright (c) 2000-2005 Silicon Graphics, Inc. |
4 | * All Rights Reserved. | |
1da177e4 | 5 | */ |
1da177e4 LT |
6 | #ifndef __XFS_BUF_H__ |
7 | #define __XFS_BUF_H__ | |
8 | ||
1da177e4 LT |
9 | #include <linux/list.h> |
10 | #include <linux/types.h> | |
11 | #include <linux/spinlock.h> | |
1da177e4 LT |
12 | #include <linux/mm.h> |
13 | #include <linux/fs.h> | |
c94c2acf | 14 | #include <linux/dax.h> |
1da177e4 | 15 | #include <linux/uio.h> |
e80dfa19 | 16 | #include <linux/list_lru.h> |
1da177e4 LT |
17 | |
18 | /* | |
19 | * Base types | |
20 | */ | |
b01d1461 | 21 | struct xfs_buf; |
1da177e4 | 22 | |
ce8e922c NS |
23 | #define XFS_BUF_DADDR_NULL ((xfs_daddr_t) (-1LL)) |
24 | ||
b9b3fe15 DC |
25 | #define XBF_READ (1u << 0) /* buffer intended for reading from device */ |
26 | #define XBF_WRITE (1u << 1) /* buffer intended for writing to device */ | |
27 | #define XBF_READ_AHEAD (1u << 2) /* asynchronous read-ahead */ | |
28 | #define XBF_NO_IOACCT (1u << 3) /* bypass I/O accounting (non-LRU bufs) */ | |
29 | #define XBF_ASYNC (1u << 4) /* initiator will not wait for completion */ | |
30 | #define XBF_DONE (1u << 5) /* all pages in the buffer uptodate */ | |
31 | #define XBF_STALE (1u << 6) /* buffer has been staled, do not find it */ | |
32 | #define XBF_WRITE_FAIL (1u << 7) /* async writes have failed on this buffer */ | |
1d5ae5df | 33 | |
f593bf14 | 34 | /* buffer type flags for write callbacks */ |
b9b3fe15 DC |
35 | #define _XBF_INODES (1u << 16)/* inode buffer */ |
36 | #define _XBF_DQUOTS (1u << 17)/* dquot buffer */ | |
37 | #define _XBF_LOGRECOVERY (1u << 18)/* log recovery buffer */ | |
1da177e4 | 38 | |
807cbbdb | 39 | /* flags used only internally */ |
b9b3fe15 DC |
40 | #define _XBF_PAGES (1u << 20)/* backed by refcounted pages */ |
41 | #define _XBF_KMEM (1u << 21)/* backed by heap memory */ | |
42 | #define _XBF_DELWRI_Q (1u << 22)/* buffer on a delwri queue */ | |
6ab455ee | 43 | |
f593bf14 | 44 | /* flags used only as arguments to access routines */ |
85c73bf7 | 45 | #define XBF_INCORE (1u << 29)/* lookup only, return if found in cache */ |
b9b3fe15 DC |
46 | #define XBF_TRYLOCK (1u << 30)/* lock requested, but do not wait */ |
47 | #define XBF_UNMAPPED (1u << 31)/* do not map the buffer */ | |
f593bf14 | 48 | |
85c73bf7 | 49 | |
807cbbdb | 50 | typedef unsigned int xfs_buf_flags_t; |
1da177e4 | 51 | |
0b1b213f CH |
52 | #define XFS_BUF_FLAGS \ |
53 | { XBF_READ, "READ" }, \ | |
54 | { XBF_WRITE, "WRITE" }, \ | |
1d5ae5df | 55 | { XBF_READ_AHEAD, "READ_AHEAD" }, \ |
1247ec4c | 56 | { XBF_NO_IOACCT, "NO_IOACCT" }, \ |
0b1b213f CH |
57 | { XBF_ASYNC, "ASYNC" }, \ |
58 | { XBF_DONE, "DONE" }, \ | |
0b1b213f | 59 | { XBF_STALE, "STALE" }, \ |
ac8809f9 | 60 | { XBF_WRITE_FAIL, "WRITE_FAIL" }, \ |
f593bf14 | 61 | { _XBF_INODES, "INODES" }, \ |
0c7e5afb | 62 | { _XBF_DQUOTS, "DQUOTS" }, \ |
b9b3fe15 | 63 | { _XBF_LOGRECOVERY, "LOG_RECOVERY" }, \ |
0b1b213f | 64 | { _XBF_PAGES, "PAGES" }, \ |
0e6e847f | 65 | { _XBF_KMEM, "KMEM" }, \ |
f593bf14 DC |
66 | { _XBF_DELWRI_Q, "DELWRI_Q" }, \ |
67 | /* The following interface flags should never be set */ \ | |
85c73bf7 | 68 | { XBF_INCORE, "INCORE" }, \ |
f593bf14 DC |
69 | { XBF_TRYLOCK, "TRYLOCK" }, \ |
70 | { XBF_UNMAPPED, "UNMAPPED" } | |
ac8809f9 | 71 | |
a4082357 DC |
72 | /* |
73 | * Internal state flags. | |
74 | */ | |
75 | #define XFS_BSTATE_DISPOSE (1 << 0) /* buffer being discarded */ | |
63db7c81 | 76 | #define XFS_BSTATE_IN_FLIGHT (1 << 1) /* I/O in flight */ |
0b1b213f | 77 | |
7c71ee78 ES |
78 | /* |
79 | * The xfs_buftarg contains 2 notions of "sector size" - | |
80 | * | |
81 | * 1) The metadata sector size, which is the minimum unit and | |
82 | * alignment of IO which will be performed by metadata operations. | |
83 | * 2) The device logical sector size | |
84 | * | |
85 | * The first is specified at mkfs time, and is stored on-disk in the | |
86 | * superblock's sb_sectsize. | |
87 | * | |
88 | * The latter is derived from the underlying device, and controls direct IO | |
89 | * alignment constraints. | |
90 | */ | |
1da177e4 | 91 | typedef struct xfs_buftarg { |
ce8e922c NS |
92 | dev_t bt_dev; |
93 | struct block_device *bt_bdev; | |
486aff5e | 94 | struct dax_device *bt_daxdev; |
cd913c76 | 95 | u64 bt_dax_part_off; |
ebad861b | 96 | struct xfs_mount *bt_mount; |
6da54179 ES |
97 | unsigned int bt_meta_sectorsize; |
98 | size_t bt_meta_sectormask; | |
7c71ee78 ES |
99 | size_t bt_logical_sectorsize; |
100 | size_t bt_logical_sectormask; | |
ce8e922c | 101 | |
ff57ab21 DC |
102 | /* LRU control structures */ |
103 | struct shrinker bt_shrinker; | |
e80dfa19 | 104 | struct list_lru bt_lru; |
9c7504aa BF |
105 | |
106 | struct percpu_counter bt_io_count; | |
f9bccfcc | 107 | struct ratelimit_state bt_ioerror_rl; |
1da177e4 LT |
108 | } xfs_buftarg_t; |
109 | ||
ce8e922c | 110 | #define XB_PAGES 2 |
1da177e4 | 111 | |
cbb7baab DC |
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 | }; | |
116 | ||
3e85c868 DC |
117 | #define DEFINE_SINGLE_BUF_MAP(map, blkno, numblk) \ |
118 | struct xfs_buf_map (map) = { .bm_bn = (blkno), .bm_len = (numblk) }; | |
119 | ||
1813dd64 | 120 | struct xfs_buf_ops { |
233135b7 | 121 | char *name; |
15baadf7 DW |
122 | union { |
123 | __be32 magic[2]; /* v4 and v5 on disk magic values */ | |
124 | __be16 magic16[2]; /* v4 and v5 on disk magic values */ | |
125 | }; | |
1813dd64 DC |
126 | void (*verify_read)(struct xfs_buf *); |
127 | void (*verify_write)(struct xfs_buf *); | |
b5572597 | 128 | xfs_failaddr_t (*verify_struct)(struct xfs_buf *bp); |
1813dd64 DC |
129 | }; |
130 | ||
e8222613 | 131 | struct xfs_buf { |
50f59e8e DC |
132 | /* |
133 | * first cacheline holds all the fields needed for an uncontended cache | |
134 | * hit to be fully processed. The semaphore straddles the cacheline | |
135 | * boundary, but the counter and lock sits on the first cacheline, | |
136 | * which is the only bit that is touched if we hit the semaphore | |
137 | * fast-path on locking. | |
138 | */ | |
6031e73a | 139 | struct rhash_head b_rhash_head; /* pag buffer hash node */ |
8cf07f3d | 140 | |
4c7f65ae | 141 | xfs_daddr_t b_rhash_key; /* buffer cache index */ |
4e94b71b | 142 | int b_length; /* size of buffer in BBs */ |
50f59e8e | 143 | atomic_t b_hold; /* reference count */ |
430cbeb8 | 144 | atomic_t b_lru_ref; /* lru reclaim ref count */ |
50f59e8e | 145 | xfs_buf_flags_t b_flags; /* status flags */ |
ce8e922c | 146 | struct semaphore b_sema; /* semaphore for lockables */ |
50f59e8e | 147 | |
6fb8a90a CM |
148 | /* |
149 | * concurrent access to b_lru and b_lru_flags are protected by | |
150 | * bt_lru_lock and not by b_sema | |
151 | */ | |
430cbeb8 | 152 | struct list_head b_lru; /* lru list */ |
a4082357 DC |
153 | spinlock_t b_lock; /* internal state lock */ |
154 | unsigned int b_state; /* internal state flags */ | |
61be9c52 | 155 | int b_io_error; /* internal IO error state */ |
ce8e922c NS |
156 | wait_queue_head_t b_waiters; /* unpin waiters */ |
157 | struct list_head b_list; | |
74f75a0c | 158 | struct xfs_perag *b_pag; /* contains rbtree root */ |
dbd329f1 | 159 | struct xfs_mount *b_mount; |
10fb9ac1 | 160 | struct xfs_buftarg *b_target; /* buffer target (device) */ |
ce8e922c | 161 | void *b_addr; /* virtual address of buffer */ |
b29c70f5 | 162 | struct work_struct b_ioend_work; |
b4dd330b | 163 | struct completion b_iowait; /* queue for I/O waiters */ |
e99b4bd0 | 164 | struct xfs_buf_log_item *b_log_item; |
643c8c05 | 165 | struct list_head b_li_list; /* Log items list head */ |
bf9d9013 | 166 | struct xfs_trans *b_transp; |
ce8e922c NS |
167 | struct page **b_pages; /* array of page pointers */ |
168 | struct page *b_page_array[XB_PAGES]; /* inline pages */ | |
3e85c868 | 169 | struct xfs_buf_map *b_maps; /* compound buffer map */ |
d44d9bc6 | 170 | struct xfs_buf_map __b_map; /* inline compound buffer map */ |
3e85c868 | 171 | int b_map_count; |
50f59e8e DC |
172 | atomic_t b_pin_count; /* pin count */ |
173 | atomic_t b_io_remaining; /* #outstanding I/O requests */ | |
174 | unsigned int b_page_count; /* size of page array */ | |
54cd3aa6 CH |
175 | unsigned int b_offset; /* page offset of b_addr, |
176 | only for _XBF_KMEM buffers */ | |
2451337d | 177 | int b_error; /* error code on I/O */ |
a5ea70d2 CM |
178 | |
179 | /* | |
180 | * async write failure retry count. Initialised to zero on the first | |
181 | * failure, then when it exceeds the maximum configured without a | |
182 | * success the write is considered to be failed permanently and the | |
183 | * iodone handler will take appropriate action. | |
184 | * | |
185 | * For retry timeouts, we record the jiffie of the first failure. This | |
186 | * means that we can change the retry timeout for buffers already under | |
187 | * I/O and thus avoid getting stuck in a retry loop with a long timeout. | |
188 | * | |
189 | * last_error is used to ensure that we are getting repeated errors, not | |
190 | * different errors. e.g. a block device might change ENOSPC to EIO when | |
191 | * a failure timeout occurs, so we want to re-initialise the error | |
192 | * retry behaviour appropriately when that happens. | |
193 | */ | |
194 | int b_retries; | |
195 | unsigned long b_first_retry_time; /* in jiffies */ | |
196 | int b_last_error; | |
197 | ||
1813dd64 | 198 | const struct xfs_buf_ops *b_ops; |
e8222613 | 199 | }; |
1da177e4 | 200 | |
1da177e4 | 201 | /* Finding and Reading Buffers */ |
3848b5f6 DW |
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); | |
4ed8e27b DW |
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, | |
cdbcf82b | 206 | const struct xfs_buf_ops *ops, xfs_failaddr_t fa); |
6dde2707 | 207 | void xfs_buf_readahead_map(struct xfs_buftarg *target, |
c3f8fc73 | 208 | struct xfs_buf_map *map, int nmaps, |
1813dd64 | 209 | const struct xfs_buf_ops *ops); |
6dde2707 | 210 | |
85c73bf7 DC |
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 | ||
841263e9 | 224 | static inline int |
6dde2707 DC |
225 | xfs_buf_get( |
226 | struct xfs_buftarg *target, | |
227 | xfs_daddr_t blkno, | |
841263e9 DW |
228 | size_t numblks, |
229 | struct xfs_buf **bpp) | |
6dde2707 DC |
230 | { |
231 | DEFINE_SINGLE_BUF_MAP(map, blkno, numblks); | |
3848b5f6 | 232 | |
841263e9 | 233 | return xfs_buf_get_map(target, &map, 1, 0, bpp); |
6dde2707 DC |
234 | } |
235 | ||
0e3eccce | 236 | static inline int |
6dde2707 DC |
237 | xfs_buf_read( |
238 | struct xfs_buftarg *target, | |
239 | xfs_daddr_t blkno, | |
240 | size_t numblks, | |
c3f8fc73 | 241 | xfs_buf_flags_t flags, |
0e3eccce | 242 | struct xfs_buf **bpp, |
1813dd64 | 243 | const struct xfs_buf_ops *ops) |
6dde2707 DC |
244 | { |
245 | DEFINE_SINGLE_BUF_MAP(map, blkno, numblks); | |
4ed8e27b | 246 | |
cdbcf82b DW |
247 | return xfs_buf_read_map(target, &map, 1, flags, bpp, ops, |
248 | __builtin_return_address(0)); | |
6dde2707 DC |
249 | } |
250 | ||
251 | static inline void | |
252 | xfs_buf_readahead( | |
253 | struct xfs_buftarg *target, | |
254 | xfs_daddr_t blkno, | |
c3f8fc73 | 255 | size_t numblks, |
1813dd64 | 256 | const struct xfs_buf_ops *ops) |
6dde2707 DC |
257 | { |
258 | DEFINE_SINGLE_BUF_MAP(map, blkno, numblks); | |
1813dd64 | 259 | return xfs_buf_readahead_map(target, &map, 1, ops); |
6dde2707 | 260 | } |
e70b73f8 | 261 | |
b9b3fe15 DC |
262 | int xfs_buf_get_uncached(struct xfs_buftarg *target, size_t numblks, |
263 | xfs_buf_flags_t flags, struct xfs_buf **bpp); | |
ba372674 | 264 | int xfs_buf_read_uncached(struct xfs_buftarg *target, xfs_daddr_t daddr, |
b9b3fe15 DC |
265 | size_t numblks, xfs_buf_flags_t flags, struct xfs_buf **bpp, |
266 | const struct xfs_buf_ops *ops); | |
26e32875 | 267 | int _xfs_buf_read(struct xfs_buf *bp, xfs_buf_flags_t flags); |
e70b73f8 | 268 | void xfs_buf_hold(struct xfs_buf *bp); |
1da177e4 LT |
269 | |
270 | /* Releasing Buffers */ | |
e8222613 | 271 | extern void xfs_buf_rele(struct xfs_buf *); |
1da177e4 LT |
272 | |
273 | /* Locking and Unlocking Buffers */ | |
e8222613 DC |
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 *); | |
0c842ad4 CH |
277 | #define xfs_buf_islocked(bp) \ |
278 | ((bp)->b_sema.count <= 0) | |
1da177e4 | 279 | |
e8222613 | 280 | static inline void xfs_buf_relse(struct xfs_buf *bp) |
f593bf14 DC |
281 | { |
282 | xfs_buf_unlock(bp); | |
283 | xfs_buf_rele(bp); | |
284 | } | |
285 | ||
1da177e4 | 286 | /* Buffer Read and Write Routines */ |
c2b006c1 | 287 | extern int xfs_bwrite(struct xfs_buf *bp); |
f593bf14 | 288 | |
31ca03c9 DW |
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) | |
cdbcf82b | 292 | extern void xfs_buf_ioerror_alert(struct xfs_buf *bp, xfs_failaddr_t fa); |
54b3b1f6 | 293 | void xfs_buf_ioend_fail(struct xfs_buf *); |
f9a196ee | 294 | void xfs_buf_zero(struct xfs_buf *bp, size_t boff, size_t bsize); |
8d57c216 DW |
295 | void __xfs_buf_mark_corrupt(struct xfs_buf *bp, xfs_failaddr_t fa); |
296 | #define xfs_buf_mark_corrupt(bp) __xfs_buf_mark_corrupt((bp), __this_address) | |
ce8e922c | 297 | |
1da177e4 | 298 | /* Buffer Utility Routines */ |
88ee2df7 | 299 | extern void *xfs_buf_offset(struct xfs_buf *, size_t); |
5cfd28b6 | 300 | extern void xfs_buf_stale(struct xfs_buf *bp); |
1da177e4 | 301 | |
1da177e4 | 302 | /* Delayed Write Buffer Routines */ |
20e8a063 | 303 | extern void xfs_buf_delwri_cancel(struct list_head *); |
43ff2122 CH |
304 | extern bool xfs_buf_delwri_queue(struct xfs_buf *, struct list_head *); |
305 | extern int xfs_buf_delwri_submit(struct list_head *); | |
306 | extern int xfs_buf_delwri_submit_nowait(struct list_head *); | |
7912e7fe | 307 | extern int xfs_buf_delwri_pushbuf(struct xfs_buf *, struct list_head *); |
1da177e4 LT |
308 | |
309 | /* Buffer Daemon Setup Routines */ | |
ce8e922c NS |
310 | extern int xfs_buf_init(void); |
311 | extern void xfs_buf_terminate(void); | |
1da177e4 | 312 | |
04fcad80 DC |
313 | static inline xfs_daddr_t xfs_buf_daddr(struct xfs_buf *bp) |
314 | { | |
315 | return bp->b_maps[0].bm_bn; | |
316 | } | |
317 | ||
7561d27e | 318 | void xfs_buf_set_ref(struct xfs_buf *bp, int lru_ref); |
ce8e922c | 319 | |
879de98e DC |
320 | /* |
321 | * If the buffer is already on the LRU, do nothing. Otherwise set the buffer | |
322 | * up with a reference count of 0 so it will be tossed from the cache when | |
323 | * released. | |
324 | */ | |
325 | static inline void xfs_buf_oneshot(struct xfs_buf *bp) | |
326 | { | |
327 | if (!list_empty(&bp->b_lru) || atomic_read(&bp->b_lru_ref) > 1) | |
328 | return; | |
329 | atomic_set(&bp->b_lru_ref, 0); | |
330 | } | |
331 | ||
811e64c7 CS |
332 | static inline int xfs_buf_ispinned(struct xfs_buf *bp) |
333 | { | |
334 | return atomic_read(&bp->b_pin_count); | |
335 | } | |
ce8e922c | 336 | |
51582170 ES |
337 | static inline int |
338 | xfs_buf_verify_cksum(struct xfs_buf *bp, unsigned long cksum_offset) | |
339 | { | |
340 | return xfs_verify_cksum(bp->b_addr, BBTOB(bp->b_length), | |
341 | cksum_offset); | |
342 | } | |
343 | ||
f1dbcd7e ES |
344 | static inline void |
345 | xfs_buf_update_cksum(struct xfs_buf *bp, unsigned long cksum_offset) | |
346 | { | |
347 | xfs_update_cksum(bp->b_addr, BBTOB(bp->b_length), | |
348 | cksum_offset); | |
349 | } | |
350 | ||
1da177e4 LT |
351 | /* |
352 | * Handling of buftargs. | |
353 | */ | |
5b5abbef CH |
354 | struct xfs_buftarg *xfs_alloc_buftarg(struct xfs_mount *mp, |
355 | struct block_device *bdev); | |
a1f69417 | 356 | extern void xfs_free_buftarg(struct xfs_buftarg *); |
8321ddb2 | 357 | extern void xfs_buftarg_wait(struct xfs_buftarg *); |
10fb9ac1 BF |
358 | extern void xfs_buftarg_drain(struct xfs_buftarg *); |
359 | extern int xfs_setsize_buftarg(struct xfs_buftarg *, unsigned int); | |
d808f617 | 360 | |
ce8e922c NS |
361 | #define xfs_getsize_buftarg(buftarg) block_size((buftarg)->bt_bdev) |
362 | #define xfs_readonly_buftarg(buftarg) bdev_read_only((buftarg)->bt_bdev) | |
363 | ||
75d02303 | 364 | int xfs_buf_reverify(struct xfs_buf *bp, const struct xfs_buf_ops *ops); |
15baadf7 DW |
365 | bool xfs_verify_magic(struct xfs_buf *bp, __be32 dmagic); |
366 | bool xfs_verify_magic16(struct xfs_buf *bp, __be16 dmagic); | |
1aff5696 | 367 | |
1da177e4 | 368 | #endif /* __XFS_BUF_H__ */ |