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