File Index
Files and directories
Sign in
FILE

xfs_buf.h

File preview with syntax highlighting.

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