xfs_inode_fork.h 7.6 KB
Newer Older
D
Dave Chinner 已提交
1
// SPDX-License-Identifier: GPL-2.0
2 3 4 5 6 7 8 9
/*
 * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
 * All Rights Reserved.
 */
#ifndef	__XFS_INODE_FORK_H__
#define	__XFS_INODE_FORK_H__

struct xfs_inode_log_item;
10
struct xfs_dinode;
11 12 13 14

/*
 * File incore extent information, present for each of data & attr forks.
 */
15
struct xfs_ifork {
16
	int64_t			if_bytes;	/* bytes in if_u1 */
17
	struct xfs_btree_block	*if_broot;	/* file's incore btree root */
18
	unsigned int		if_seq;		/* fork mod counter */
19
	int			if_height;	/* height of the extent tree */
20
	union {
21
		void		*if_root;	/* extent tree root */
22 23
		char		*if_data;	/* inline file data */
	} if_u1;
24
	short			if_broot_bytes;	/* bytes allocated for root */
25
	int8_t			if_format;	/* format of this fork */
26
	xfs_extnum_t		if_nextents;	/* # of extents in this fork */
27
};
28

29 30 31 32 33 34
/*
 * Worst-case increase in the fork extent count when we're adding a single
 * extent to a fork and there's no possibility of splitting an existing mapping.
 */
#define XFS_IEXT_ADD_NOSPLIT_CNT	(1)

35 36 37 38 39 40 41
/*
 * Punching out an extent from the middle of an existing extent can cause the
 * extent count to increase by 1.
 * i.e. | Old extent | Hole | Old extent |
 */
#define XFS_IEXT_PUNCH_HOLE_CNT		(1)

42 43 44 45 46 47 48 49 50 51 52 53 54
/*
 * Directory entry addition can cause the following,
 * 1. Data block can be added/removed.
 *    A new extent can cause extent count to increase by 1.
 * 2. Free disk block can be added/removed.
 *    Same behaviour as described above for Data block.
 * 3. Dabtree blocks.
 *    XFS_DA_NODE_MAXDEPTH blocks can be added. Each of these can be new
 *    extents. Hence extent count can increase by XFS_DA_NODE_MAXDEPTH.
 */
#define XFS_IEXT_DIR_MANIP_CNT(mp) \
	((XFS_DA_NODE_MAXDEPTH + 1 + 1) * (mp)->m_dir_geo->fsbcount)

55 56 57 58 59 60 61 62 63 64
/*
 * Adding/removing an xattr can cause XFS_DA_NODE_MAXDEPTH extents to
 * be added. One extra extent for dabtree in case a local attr is
 * large enough to cause a double split.  It can also cause extent
 * count to increase proportional to the size of a remote xattr's
 * value.
 */
#define XFS_IEXT_ATTR_MANIP_CNT(rmt_blks) \
	(XFS_DA_NODE_MAXDEPTH + max(1, rmt_blks))

65 66 67 68 69 70 71 72 73
/*
 * A write to a sub-interval of an existing unwritten extent causes the original
 * extent to be split into 3 extents
 * i.e. | Unwritten | Real | Unwritten |
 * Hence extent count can increase by 2.
 */
#define XFS_IEXT_WRITE_UNWRITTEN_CNT	(2)


74 75 76 77 78 79 80 81 82
/*
 * Moving an extent to data fork can cause a sub-interval of an existing extent
 * to be unmapped. This will increase extent count by 1. Mapping in the new
 * extent can increase the extent count by 1 again i.e.
 * | Old extent | New extent | Old extent |
 * Hence number of extents increases by 2.
 */
#define XFS_IEXT_REFLINK_END_COW_CNT	(2)

83 84 85 86 87 88 89
/*
 * Removing an initial range of source/donor file's extent and adding a new
 * extent (from donor/source file) in its place will cause extent count to
 * increase by 1.
 */
#define XFS_IEXT_SWAP_RMAP_CNT		(1)

90 91 92 93
/*
 * Fork handling.
 */

94 95
#define XFS_IFORK_Q(ip)			((ip)->i_forkoff != 0)
#define XFS_IFORK_BOFF(ip)		((int)((ip)->i_forkoff << 3))
96 97 98 99

#define XFS_IFORK_PTR(ip,w)		\
	((w) == XFS_DATA_FORK ? \
		&(ip)->i_df : \
D
Darrick J. Wong 已提交
100 101 102
		((w) == XFS_ATTR_FORK ? \
			(ip)->i_afp : \
			(ip)->i_cowfp))
103
#define XFS_IFORK_DSIZE(ip) \
104
	(XFS_IFORK_Q(ip) ? XFS_IFORK_BOFF(ip) : XFS_LITINO((ip)->i_mount))
105
#define XFS_IFORK_ASIZE(ip) \
106
	(XFS_IFORK_Q(ip) ? XFS_LITINO((ip)->i_mount) - XFS_IFORK_BOFF(ip) : 0)
107 108 109
#define XFS_IFORK_SIZE(ip,w) \
	((w) == XFS_DATA_FORK ? \
		XFS_IFORK_DSIZE(ip) : \
D
Darrick J. Wong 已提交
110 111 112
		((w) == XFS_ATTR_FORK ? \
			XFS_IFORK_ASIZE(ip) : \
			0))
113 114 115
#define XFS_IFORK_MAXEXT(ip, w) \
	(XFS_IFORK_SIZE(ip, w) / sizeof(xfs_bmbt_rec_t))

116 117 118 119 120
static inline bool xfs_ifork_has_extents(struct xfs_ifork *ifp)
{
	return ifp->if_format == XFS_DINODE_FMT_EXTENTS ||
		ifp->if_format == XFS_DINODE_FMT_BTREE;
}
121

122 123 124 125 126 127 128
static inline xfs_extnum_t xfs_ifork_nextents(struct xfs_ifork *ifp)
{
	if (!ifp)
		return 0;
	return ifp->if_nextents;
}

129 130 131 132 133 134 135
static inline int8_t xfs_ifork_format(struct xfs_ifork *ifp)
{
	if (!ifp)
		return XFS_DINODE_FMT_EXTENTS;
	return ifp->if_format;
}

136 137
struct xfs_ifork *xfs_ifork_alloc(enum xfs_dinode_fmt format,
				xfs_extnum_t nextents);
D
Darrick J. Wong 已提交
138 139
struct xfs_ifork *xfs_iext_state_to_fork(struct xfs_inode *ip, int state);

C
Christoph Hellwig 已提交
140 141
int		xfs_iformat_data_fork(struct xfs_inode *, struct xfs_dinode *);
int		xfs_iformat_attr_fork(struct xfs_inode *, struct xfs_dinode *);
142
void		xfs_iflush_fork(struct xfs_inode *, struct xfs_dinode *,
143
				struct xfs_inode_log_item *, int);
144
void		xfs_idestroy_fork(struct xfs_ifork *ifp);
145 146
void		xfs_idata_realloc(struct xfs_inode *ip, int64_t byte_diff,
				int whichfork);
147 148 149 150
void		xfs_iroot_realloc(struct xfs_inode *, int, int);
int		xfs_iread_extents(struct xfs_trans *, struct xfs_inode *, int);
int		xfs_iextents_copy(struct xfs_inode *, struct xfs_bmbt_rec *,
				  int);
151 152
void		xfs_init_local_fork(struct xfs_inode *ip, int whichfork,
				const void *data, int64_t size);
153

154
xfs_extnum_t	xfs_iext_count(struct xfs_ifork *ifp);
155
void		xfs_iext_insert(struct xfs_inode *, struct xfs_iext_cursor *cur,
156
			struct xfs_bmbt_irec *, int);
157
void		xfs_iext_remove(struct xfs_inode *, struct xfs_iext_cursor *,
158
			int);
159 160
void		xfs_iext_destroy(struct xfs_ifork *);

161 162
bool		xfs_iext_lookup_extent(struct xfs_inode *ip,
			struct xfs_ifork *ifp, xfs_fileoff_t bno,
163 164
			struct xfs_iext_cursor *cur,
			struct xfs_bmbt_irec *gotp);
165 166
bool		xfs_iext_lookup_extent_before(struct xfs_inode *ip,
			struct xfs_ifork *ifp, xfs_fileoff_t *end,
167 168 169 170
			struct xfs_iext_cursor *cur,
			struct xfs_bmbt_irec *gotp);
bool		xfs_iext_get_extent(struct xfs_ifork *ifp,
			struct xfs_iext_cursor *cur,
171
			struct xfs_bmbt_irec *gotp);
172
void		xfs_iext_update_extent(struct xfs_inode *ip, int state,
173 174 175
			struct xfs_iext_cursor *cur,
			struct xfs_bmbt_irec *gotp);

176 177 178 179
void		xfs_iext_first(struct xfs_ifork *, struct xfs_iext_cursor *);
void		xfs_iext_last(struct xfs_ifork *, struct xfs_iext_cursor *);
void		xfs_iext_next(struct xfs_ifork *, struct xfs_iext_cursor *);
void		xfs_iext_prev(struct xfs_ifork *, struct xfs_iext_cursor *);
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222

static inline bool xfs_iext_next_extent(struct xfs_ifork *ifp,
		struct xfs_iext_cursor *cur, struct xfs_bmbt_irec *gotp)
{
	xfs_iext_next(ifp, cur);
	return xfs_iext_get_extent(ifp, cur, gotp);
}

static inline bool xfs_iext_prev_extent(struct xfs_ifork *ifp,
		struct xfs_iext_cursor *cur, struct xfs_bmbt_irec *gotp)
{
	xfs_iext_prev(ifp, cur);
	return xfs_iext_get_extent(ifp, cur, gotp);
}

/*
 * Return the extent after cur in gotp without updating the cursor.
 */
static inline bool xfs_iext_peek_next_extent(struct xfs_ifork *ifp,
		struct xfs_iext_cursor *cur, struct xfs_bmbt_irec *gotp)
{
	struct xfs_iext_cursor ncur = *cur;

	xfs_iext_next(ifp, &ncur);
	return xfs_iext_get_extent(ifp, &ncur, gotp);
}

/*
 * Return the extent before cur in gotp without updating the cursor.
 */
static inline bool xfs_iext_peek_prev_extent(struct xfs_ifork *ifp,
		struct xfs_iext_cursor *cur, struct xfs_bmbt_irec *gotp)
{
	struct xfs_iext_cursor ncur = *cur;

	xfs_iext_prev(ifp, &ncur);
	return xfs_iext_get_extent(ifp, &ncur, gotp);
}

#define for_each_xfs_iext(ifp, ext, got)		\
	for (xfs_iext_first((ifp), (ext));		\
	     xfs_iext_get_extent((ifp), (ext), (got));	\
	     xfs_iext_next((ifp), (ext)))
223

D
Darrick J. Wong 已提交
224
extern struct kmem_cache	*xfs_ifork_zone;
225

D
Darrick J. Wong 已提交
226 227
extern void xfs_ifork_init_cow(struct xfs_inode *ip);

228 229
int xfs_ifork_verify_local_data(struct xfs_inode *ip);
int xfs_ifork_verify_local_attr(struct xfs_inode *ip);
230 231
int xfs_iext_count_may_overflow(struct xfs_inode *ip, int whichfork,
		int nr_to_add);
232

C
Christoph Hellwig 已提交
233 234 235 236 237 238
/* returns true if the fork has extents but they are not read in yet. */
static inline bool xfs_need_iread_extents(struct xfs_ifork *ifp)
{
	return ifp->if_format == XFS_DINODE_FMT_BTREE && ifp->if_height == 0;
}

239
#endif	/* __XFS_INODE_FORK_H__ */