xref: /openbmc/linux/fs/xfs/libxfs/xfs_inode_fork.h (revision 31e67366)
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_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 		char		*if_data;	/* inline file data */
23 	} if_u1;
24 	short			if_broot_bytes;	/* bytes allocated for root */
25 	unsigned char		if_flags;	/* per-fork flags */
26 	int8_t			if_format;	/* format of this fork */
27 	xfs_extnum_t		if_nextents;	/* # of extents in this fork */
28 };
29 
30 /*
31  * Per-fork incore inode flags.
32  */
33 #define	XFS_IFINLINE	0x01	/* Inline data is read in */
34 #define	XFS_IFEXTENTS	0x02	/* All extent pointers are read in */
35 #define	XFS_IFBROOT	0x04	/* i_broot points to the bmap b-tree root */
36 
37 /*
38  * Worst-case increase in the fork extent count when we're adding a single
39  * extent to a fork and there's no possibility of splitting an existing mapping.
40  */
41 #define XFS_IEXT_ADD_NOSPLIT_CNT	(1)
42 
43 /*
44  * Punching out an extent from the middle of an existing extent can cause the
45  * extent count to increase by 1.
46  * i.e. | Old extent | Hole | Old extent |
47  */
48 #define XFS_IEXT_PUNCH_HOLE_CNT		(1)
49 
50 /*
51  * Directory entry addition can cause the following,
52  * 1. Data block can be added/removed.
53  *    A new extent can cause extent count to increase by 1.
54  * 2. Free disk block can be added/removed.
55  *    Same behaviour as described above for Data block.
56  * 3. Dabtree blocks.
57  *    XFS_DA_NODE_MAXDEPTH blocks can be added. Each of these can be new
58  *    extents. Hence extent count can increase by XFS_DA_NODE_MAXDEPTH.
59  */
60 #define XFS_IEXT_DIR_MANIP_CNT(mp) \
61 	((XFS_DA_NODE_MAXDEPTH + 1 + 1) * (mp)->m_dir_geo->fsbcount)
62 
63 /*
64  * Adding/removing an xattr can cause XFS_DA_NODE_MAXDEPTH extents to
65  * be added. One extra extent for dabtree in case a local attr is
66  * large enough to cause a double split.  It can also cause extent
67  * count to increase proportional to the size of a remote xattr's
68  * value.
69  */
70 #define XFS_IEXT_ATTR_MANIP_CNT(rmt_blks) \
71 	(XFS_DA_NODE_MAXDEPTH + max(1, rmt_blks))
72 
73 /*
74  * A write to a sub-interval of an existing unwritten extent causes the original
75  * extent to be split into 3 extents
76  * i.e. | Unwritten | Real | Unwritten |
77  * Hence extent count can increase by 2.
78  */
79 #define XFS_IEXT_WRITE_UNWRITTEN_CNT	(2)
80 
81 
82 /*
83  * Moving an extent to data fork can cause a sub-interval of an existing extent
84  * to be unmapped. This will increase extent count by 1. Mapping in the new
85  * extent can increase the extent count by 1 again i.e.
86  * | Old extent | New extent | Old extent |
87  * Hence number of extents increases by 2.
88  */
89 #define XFS_IEXT_REFLINK_END_COW_CNT	(2)
90 
91 /*
92  * Removing an initial range of source/donor file's extent and adding a new
93  * extent (from donor/source file) in its place will cause extent count to
94  * increase by 1.
95  */
96 #define XFS_IEXT_SWAP_RMAP_CNT		(1)
97 
98 /*
99  * Fork handling.
100  */
101 
102 #define XFS_IFORK_Q(ip)			((ip)->i_d.di_forkoff != 0)
103 #define XFS_IFORK_BOFF(ip)		((int)((ip)->i_d.di_forkoff << 3))
104 
105 #define XFS_IFORK_PTR(ip,w)		\
106 	((w) == XFS_DATA_FORK ? \
107 		&(ip)->i_df : \
108 		((w) == XFS_ATTR_FORK ? \
109 			(ip)->i_afp : \
110 			(ip)->i_cowfp))
111 #define XFS_IFORK_DSIZE(ip) \
112 	(XFS_IFORK_Q(ip) ? XFS_IFORK_BOFF(ip) : XFS_LITINO((ip)->i_mount))
113 #define XFS_IFORK_ASIZE(ip) \
114 	(XFS_IFORK_Q(ip) ? XFS_LITINO((ip)->i_mount) - XFS_IFORK_BOFF(ip) : 0)
115 #define XFS_IFORK_SIZE(ip,w) \
116 	((w) == XFS_DATA_FORK ? \
117 		XFS_IFORK_DSIZE(ip) : \
118 		((w) == XFS_ATTR_FORK ? \
119 			XFS_IFORK_ASIZE(ip) : \
120 			0))
121 #define XFS_IFORK_MAXEXT(ip, w) \
122 	(XFS_IFORK_SIZE(ip, w) / sizeof(xfs_bmbt_rec_t))
123 
124 static inline bool xfs_ifork_has_extents(struct xfs_ifork *ifp)
125 {
126 	return ifp->if_format == XFS_DINODE_FMT_EXTENTS ||
127 		ifp->if_format == XFS_DINODE_FMT_BTREE;
128 }
129 
130 static inline xfs_extnum_t xfs_ifork_nextents(struct xfs_ifork *ifp)
131 {
132 	if (!ifp)
133 		return 0;
134 	return ifp->if_nextents;
135 }
136 
137 static inline int8_t xfs_ifork_format(struct xfs_ifork *ifp)
138 {
139 	if (!ifp)
140 		return XFS_DINODE_FMT_EXTENTS;
141 	return ifp->if_format;
142 }
143 
144 struct xfs_ifork *xfs_iext_state_to_fork(struct xfs_inode *ip, int state);
145 
146 int		xfs_iformat_data_fork(struct xfs_inode *, struct xfs_dinode *);
147 int		xfs_iformat_attr_fork(struct xfs_inode *, struct xfs_dinode *);
148 void		xfs_iflush_fork(struct xfs_inode *, struct xfs_dinode *,
149 				struct xfs_inode_log_item *, int);
150 void		xfs_idestroy_fork(struct xfs_ifork *ifp);
151 void		xfs_idata_realloc(struct xfs_inode *ip, int64_t byte_diff,
152 				int whichfork);
153 void		xfs_iroot_realloc(struct xfs_inode *, int, int);
154 int		xfs_iread_extents(struct xfs_trans *, struct xfs_inode *, int);
155 int		xfs_iextents_copy(struct xfs_inode *, struct xfs_bmbt_rec *,
156 				  int);
157 void		xfs_init_local_fork(struct xfs_inode *ip, int whichfork,
158 				const void *data, int64_t size);
159 
160 xfs_extnum_t	xfs_iext_count(struct xfs_ifork *ifp);
161 void		xfs_iext_insert(struct xfs_inode *, struct xfs_iext_cursor *cur,
162 			struct xfs_bmbt_irec *, int);
163 void		xfs_iext_remove(struct xfs_inode *, struct xfs_iext_cursor *,
164 			int);
165 void		xfs_iext_destroy(struct xfs_ifork *);
166 
167 bool		xfs_iext_lookup_extent(struct xfs_inode *ip,
168 			struct xfs_ifork *ifp, xfs_fileoff_t bno,
169 			struct xfs_iext_cursor *cur,
170 			struct xfs_bmbt_irec *gotp);
171 bool		xfs_iext_lookup_extent_before(struct xfs_inode *ip,
172 			struct xfs_ifork *ifp, xfs_fileoff_t *end,
173 			struct xfs_iext_cursor *cur,
174 			struct xfs_bmbt_irec *gotp);
175 bool		xfs_iext_get_extent(struct xfs_ifork *ifp,
176 			struct xfs_iext_cursor *cur,
177 			struct xfs_bmbt_irec *gotp);
178 void		xfs_iext_update_extent(struct xfs_inode *ip, int state,
179 			struct xfs_iext_cursor *cur,
180 			struct xfs_bmbt_irec *gotp);
181 
182 void		xfs_iext_first(struct xfs_ifork *, struct xfs_iext_cursor *);
183 void		xfs_iext_last(struct xfs_ifork *, struct xfs_iext_cursor *);
184 void		xfs_iext_next(struct xfs_ifork *, struct xfs_iext_cursor *);
185 void		xfs_iext_prev(struct xfs_ifork *, struct xfs_iext_cursor *);
186 
187 static inline bool xfs_iext_next_extent(struct xfs_ifork *ifp,
188 		struct xfs_iext_cursor *cur, struct xfs_bmbt_irec *gotp)
189 {
190 	xfs_iext_next(ifp, cur);
191 	return xfs_iext_get_extent(ifp, cur, gotp);
192 }
193 
194 static inline bool xfs_iext_prev_extent(struct xfs_ifork *ifp,
195 		struct xfs_iext_cursor *cur, struct xfs_bmbt_irec *gotp)
196 {
197 	xfs_iext_prev(ifp, cur);
198 	return xfs_iext_get_extent(ifp, cur, gotp);
199 }
200 
201 /*
202  * Return the extent after cur in gotp without updating the cursor.
203  */
204 static inline bool xfs_iext_peek_next_extent(struct xfs_ifork *ifp,
205 		struct xfs_iext_cursor *cur, struct xfs_bmbt_irec *gotp)
206 {
207 	struct xfs_iext_cursor ncur = *cur;
208 
209 	xfs_iext_next(ifp, &ncur);
210 	return xfs_iext_get_extent(ifp, &ncur, gotp);
211 }
212 
213 /*
214  * Return the extent before cur in gotp without updating the cursor.
215  */
216 static inline bool xfs_iext_peek_prev_extent(struct xfs_ifork *ifp,
217 		struct xfs_iext_cursor *cur, struct xfs_bmbt_irec *gotp)
218 {
219 	struct xfs_iext_cursor ncur = *cur;
220 
221 	xfs_iext_prev(ifp, &ncur);
222 	return xfs_iext_get_extent(ifp, &ncur, gotp);
223 }
224 
225 #define for_each_xfs_iext(ifp, ext, got)		\
226 	for (xfs_iext_first((ifp), (ext));		\
227 	     xfs_iext_get_extent((ifp), (ext), (got));	\
228 	     xfs_iext_next((ifp), (ext)))
229 
230 extern struct kmem_zone	*xfs_ifork_zone;
231 
232 extern void xfs_ifork_init_cow(struct xfs_inode *ip);
233 
234 int xfs_ifork_verify_local_data(struct xfs_inode *ip);
235 int xfs_ifork_verify_local_attr(struct xfs_inode *ip);
236 int xfs_iext_count_may_overflow(struct xfs_inode *ip, int whichfork,
237 		int nr_to_add);
238 
239 #endif	/* __XFS_INODE_FORK_H__ */
240