xref: /linux/fs/xfs/libxfs/xfs_inode_fork.h (revision b477ff98d903618a1ab8247861f2ea6e70c0f0f8)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
4  * All Rights Reserved.
5  */
6 #ifndef	__XFS_INODE_FORK_H__
7 #define	__XFS_INODE_FORK_H__
8 
9 struct xfs_inode_log_item;
10 struct xfs_dinode;
11 
12 /*
13  * File incore extent information, present for each of data & attr forks.
14  */
15 struct xfs_ifork {
16 	int64_t			if_bytes;	/* bytes in if_data */
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 	void			*if_data;	/* extent tree root or
21 						   inline data */
22 	xfs_extnum_t		if_nextents;	/* # of extents in this fork */
23 	short			if_broot_bytes;	/* bytes allocated for root */
24 	int8_t			if_format;	/* format of this fork */
25 	uint8_t			if_needextents;	/* extents have not been read */
26 };
27 
28 /*
29  * Worst-case increase in the fork extent count when we're adding a single
30  * extent to a fork and there's no possibility of splitting an existing mapping.
31  */
32 #define XFS_IEXT_ADD_NOSPLIT_CNT	(1)
33 
34 /*
35  * Punching out an extent from the middle of an existing extent can cause the
36  * extent count to increase by 1.
37  * i.e. | Old extent | Hole | Old extent |
38  */
39 #define XFS_IEXT_PUNCH_HOLE_CNT		(1)
40 
41 /*
42  * Adding/removing an xattr can cause XFS_DA_NODE_MAXDEPTH extents to
43  * be added. One extra extent for dabtree in case a local attr is
44  * large enough to cause a double split.  It can also cause extent
45  * count to increase proportional to the size of a remote xattr's
46  * value.
47  */
48 #define XFS_IEXT_ATTR_MANIP_CNT(rmt_blks) \
49 	(XFS_DA_NODE_MAXDEPTH + max(1, rmt_blks))
50 
51 /*
52  * A write to a sub-interval of an existing unwritten extent causes the original
53  * extent to be split into 3 extents
54  * i.e. | Unwritten | Real | Unwritten |
55  * Hence extent count can increase by 2.
56  */
57 #define XFS_IEXT_WRITE_UNWRITTEN_CNT	(2)
58 
59 
60 /*
61  * Moving an extent to data fork can cause a sub-interval of an existing extent
62  * to be unmapped. This will increase extent count by 1. Mapping in the new
63  * extent can increase the extent count by 1 again i.e.
64  * | Old extent | New extent | Old extent |
65  * Hence number of extents increases by 2.
66  */
67 #define XFS_IEXT_REFLINK_END_COW_CNT	(2)
68 
69 /*
70  * Removing an initial range of source/donor file's extent and adding a new
71  * extent (from donor/source file) in its place will cause extent count to
72  * increase by 1.
73  */
74 #define XFS_IEXT_SWAP_RMAP_CNT		(1)
75 
76 /*
77  * Fork handling.
78  */
79 #define XFS_IFORK_MAXEXT(ip, w) \
80 	(xfs_inode_fork_size(ip, w) / sizeof(xfs_bmbt_rec_t))
81 
xfs_ifork_has_extents(struct xfs_ifork * ifp)82 static inline bool xfs_ifork_has_extents(struct xfs_ifork *ifp)
83 {
84 	return ifp->if_format == XFS_DINODE_FMT_EXTENTS ||
85 		ifp->if_format == XFS_DINODE_FMT_BTREE;
86 }
87 
xfs_ifork_nextents(struct xfs_ifork * ifp)88 static inline xfs_extnum_t xfs_ifork_nextents(struct xfs_ifork *ifp)
89 {
90 	if (!ifp)
91 		return 0;
92 	return ifp->if_nextents;
93 }
94 
xfs_ifork_format(struct xfs_ifork * ifp)95 static inline int8_t xfs_ifork_format(struct xfs_ifork *ifp)
96 {
97 	if (!ifp)
98 		return XFS_DINODE_FMT_EXTENTS;
99 	return ifp->if_format;
100 }
101 
xfs_iext_max_nextents(bool has_large_extent_counts,int whichfork)102 static inline xfs_extnum_t xfs_iext_max_nextents(bool has_large_extent_counts,
103 				int whichfork)
104 {
105 	switch (whichfork) {
106 	case XFS_DATA_FORK:
107 	case XFS_COW_FORK:
108 		if (has_large_extent_counts)
109 			return XFS_MAX_EXTCNT_DATA_FORK_LARGE;
110 		return XFS_MAX_EXTCNT_DATA_FORK_SMALL;
111 
112 	case XFS_ATTR_FORK:
113 		if (has_large_extent_counts)
114 			return XFS_MAX_EXTCNT_ATTR_FORK_LARGE;
115 		return XFS_MAX_EXTCNT_ATTR_FORK_SMALL;
116 
117 	default:
118 		ASSERT(0);
119 		return 0;
120 	}
121 }
122 
123 static inline xfs_extnum_t
xfs_dfork_data_extents(struct xfs_dinode * dip)124 xfs_dfork_data_extents(
125 	struct xfs_dinode	*dip)
126 {
127 	if (xfs_dinode_has_large_extent_counts(dip))
128 		return be64_to_cpu(dip->di_big_nextents);
129 
130 	return be32_to_cpu(dip->di_nextents);
131 }
132 
133 static inline xfs_extnum_t
xfs_dfork_attr_extents(struct xfs_dinode * dip)134 xfs_dfork_attr_extents(
135 	struct xfs_dinode	*dip)
136 {
137 	if (xfs_dinode_has_large_extent_counts(dip))
138 		return be32_to_cpu(dip->di_big_anextents);
139 
140 	return be16_to_cpu(dip->di_anextents);
141 }
142 
143 static inline xfs_extnum_t
xfs_dfork_nextents(struct xfs_dinode * dip,int whichfork)144 xfs_dfork_nextents(
145 	struct xfs_dinode	*dip,
146 	int			whichfork)
147 {
148 	switch (whichfork) {
149 	case XFS_DATA_FORK:
150 		return xfs_dfork_data_extents(dip);
151 	case XFS_ATTR_FORK:
152 		return xfs_dfork_attr_extents(dip);
153 	default:
154 		ASSERT(0);
155 		break;
156 	}
157 
158 	return 0;
159 }
160 
161 void xfs_ifork_zap_attr(struct xfs_inode *ip);
162 void xfs_ifork_init_attr(struct xfs_inode *ip, enum xfs_dinode_fmt format,
163 		xfs_extnum_t nextents);
164 struct xfs_ifork *xfs_iext_state_to_fork(struct xfs_inode *ip, int state);
165 
166 int		xfs_iformat_data_fork(struct xfs_inode *, struct xfs_dinode *);
167 int		xfs_iformat_attr_fork(struct xfs_inode *, struct xfs_dinode *);
168 void		xfs_iflush_fork(struct xfs_inode *, struct xfs_dinode *,
169 				struct xfs_inode_log_item *, int);
170 void		xfs_idestroy_fork(struct xfs_ifork *ifp);
171 void *		xfs_idata_realloc(struct xfs_inode *ip, int64_t byte_diff,
172 				int whichfork);
173 struct xfs_btree_block *xfs_broot_alloc(struct xfs_ifork *ifp,
174 				size_t new_size);
175 struct xfs_btree_block *xfs_broot_realloc(struct xfs_ifork *ifp,
176 				size_t new_size);
177 
178 int		xfs_iread_extents(struct xfs_trans *, struct xfs_inode *, int);
179 int		xfs_iextents_copy(struct xfs_inode *, struct xfs_bmbt_rec *,
180 				  int);
181 void		xfs_init_local_fork(struct xfs_inode *ip, int whichfork,
182 				const void *data, int64_t size);
183 
184 xfs_extnum_t	xfs_iext_count(struct xfs_ifork *ifp);
185 void		xfs_iext_insert_raw(struct xfs_ifork *ifp,
186 			struct xfs_iext_cursor *cur,
187 			struct xfs_bmbt_irec *irec);
188 void		xfs_iext_insert(struct xfs_inode *, struct xfs_iext_cursor *cur,
189 			struct xfs_bmbt_irec *, int);
190 void		xfs_iext_remove(struct xfs_inode *, struct xfs_iext_cursor *,
191 			int);
192 void		xfs_iext_destroy(struct xfs_ifork *);
193 
194 bool		xfs_iext_lookup_extent(struct xfs_inode *ip,
195 			struct xfs_ifork *ifp, xfs_fileoff_t bno,
196 			struct xfs_iext_cursor *cur,
197 			struct xfs_bmbt_irec *gotp);
198 bool		xfs_iext_lookup_extent_before(struct xfs_inode *ip,
199 			struct xfs_ifork *ifp, xfs_fileoff_t *end,
200 			struct xfs_iext_cursor *cur,
201 			struct xfs_bmbt_irec *gotp);
202 bool		xfs_iext_get_extent(struct xfs_ifork *ifp,
203 			struct xfs_iext_cursor *cur,
204 			struct xfs_bmbt_irec *gotp);
205 void		xfs_iext_update_extent(struct xfs_inode *ip, int state,
206 			struct xfs_iext_cursor *cur,
207 			struct xfs_bmbt_irec *gotp);
208 
209 void		xfs_iext_first(struct xfs_ifork *, struct xfs_iext_cursor *);
210 void		xfs_iext_last(struct xfs_ifork *, struct xfs_iext_cursor *);
211 void		xfs_iext_next(struct xfs_ifork *, struct xfs_iext_cursor *);
212 void		xfs_iext_prev(struct xfs_ifork *, struct xfs_iext_cursor *);
213 
xfs_iext_next_extent(struct xfs_ifork * ifp,struct xfs_iext_cursor * cur,struct xfs_bmbt_irec * gotp)214 static inline bool xfs_iext_next_extent(struct xfs_ifork *ifp,
215 		struct xfs_iext_cursor *cur, struct xfs_bmbt_irec *gotp)
216 {
217 	xfs_iext_next(ifp, cur);
218 	return xfs_iext_get_extent(ifp, cur, gotp);
219 }
220 
xfs_iext_prev_extent(struct xfs_ifork * ifp,struct xfs_iext_cursor * cur,struct xfs_bmbt_irec * gotp)221 static inline bool xfs_iext_prev_extent(struct xfs_ifork *ifp,
222 		struct xfs_iext_cursor *cur, struct xfs_bmbt_irec *gotp)
223 {
224 	xfs_iext_prev(ifp, cur);
225 	return xfs_iext_get_extent(ifp, cur, gotp);
226 }
227 
228 /*
229  * Return the extent after cur in gotp without updating the cursor.
230  */
xfs_iext_peek_next_extent(struct xfs_ifork * ifp,struct xfs_iext_cursor * cur,struct xfs_bmbt_irec * gotp)231 static inline bool xfs_iext_peek_next_extent(struct xfs_ifork *ifp,
232 		struct xfs_iext_cursor *cur, struct xfs_bmbt_irec *gotp)
233 {
234 	struct xfs_iext_cursor ncur = *cur;
235 
236 	xfs_iext_next(ifp, &ncur);
237 	return xfs_iext_get_extent(ifp, &ncur, gotp);
238 }
239 
240 /*
241  * Return the extent before cur in gotp without updating the cursor.
242  */
xfs_iext_peek_prev_extent(struct xfs_ifork * ifp,struct xfs_iext_cursor * cur,struct xfs_bmbt_irec * gotp)243 static inline bool xfs_iext_peek_prev_extent(struct xfs_ifork *ifp,
244 		struct xfs_iext_cursor *cur, struct xfs_bmbt_irec *gotp)
245 {
246 	struct xfs_iext_cursor ncur = *cur;
247 
248 	xfs_iext_prev(ifp, &ncur);
249 	return xfs_iext_get_extent(ifp, &ncur, gotp);
250 }
251 
252 #define for_each_xfs_iext(ifp, ext, got)		\
253 	for (xfs_iext_first((ifp), (ext));		\
254 	     xfs_iext_get_extent((ifp), (ext), (got));	\
255 	     xfs_iext_next((ifp), (ext)))
256 
257 extern struct kmem_cache	*xfs_ifork_cache;
258 
259 extern void xfs_ifork_init_cow(struct xfs_inode *ip);
260 
261 int xfs_ifork_verify_local_data(struct xfs_inode *ip);
262 int xfs_ifork_verify_local_attr(struct xfs_inode *ip);
263 int xfs_iext_count_extend(struct xfs_trans *tp, struct xfs_inode *ip,
264 		int whichfork, uint nr_to_add);
265 bool xfs_ifork_is_realtime(struct xfs_inode *ip, int whichfork);
266 
267 /* returns true if the fork has extents but they are not read in yet. */
xfs_need_iread_extents(const struct xfs_ifork * ifp)268 static inline bool xfs_need_iread_extents(const struct xfs_ifork *ifp)
269 {
270 	/* see xfs_iformat_{data,attr}_fork() for needextents semantics */
271 	return smp_load_acquire(&ifp->if_needextents) != 0;
272 }
273 
274 #endif	/* __XFS_INODE_FORK_H__ */
275