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