Loading...
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
9struct xfs_inode_log_item;
10struct xfs_dinode;
11
12/*
13 * File incore extent information, present for each of data & attr forks.
14 */
15struct 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
82static 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
88static 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
95static 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
102static 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
123static inline xfs_extnum_t
124xfs_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
133static inline xfs_extnum_t
134xfs_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
143static inline xfs_extnum_t
144xfs_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
161void xfs_ifork_zap_attr(struct xfs_inode *ip);
162void xfs_ifork_init_attr(struct xfs_inode *ip, enum xfs_dinode_fmt format,
163 xfs_extnum_t nextents);
164struct xfs_ifork *xfs_iext_state_to_fork(struct xfs_inode *ip, int state);
165
166int xfs_iformat_data_fork(struct xfs_inode *, struct xfs_dinode *);
167int xfs_iformat_attr_fork(struct xfs_inode *, struct xfs_dinode *);
168void xfs_iflush_fork(struct xfs_inode *, struct xfs_dinode *,
169 struct xfs_inode_log_item *, int);
170void xfs_idestroy_fork(struct xfs_ifork *ifp);
171void * xfs_idata_realloc(struct xfs_inode *ip, int64_t byte_diff,
172 int whichfork);
173void xfs_iroot_realloc(struct xfs_inode *, int, int);
174int xfs_iread_extents(struct xfs_trans *, struct xfs_inode *, int);
175int xfs_iextents_copy(struct xfs_inode *, struct xfs_bmbt_rec *,
176 int);
177void xfs_init_local_fork(struct xfs_inode *ip, int whichfork,
178 const void *data, int64_t size);
179
180xfs_extnum_t xfs_iext_count(struct xfs_ifork *ifp);
181void xfs_iext_insert_raw(struct xfs_ifork *ifp,
182 struct xfs_iext_cursor *cur,
183 struct xfs_bmbt_irec *irec);
184void xfs_iext_insert(struct xfs_inode *, struct xfs_iext_cursor *cur,
185 struct xfs_bmbt_irec *, int);
186void xfs_iext_remove(struct xfs_inode *, struct xfs_iext_cursor *,
187 int);
188void xfs_iext_destroy(struct xfs_ifork *);
189
190bool xfs_iext_lookup_extent(struct xfs_inode *ip,
191 struct xfs_ifork *ifp, xfs_fileoff_t bno,
192 struct xfs_iext_cursor *cur,
193 struct xfs_bmbt_irec *gotp);
194bool xfs_iext_lookup_extent_before(struct xfs_inode *ip,
195 struct xfs_ifork *ifp, xfs_fileoff_t *end,
196 struct xfs_iext_cursor *cur,
197 struct xfs_bmbt_irec *gotp);
198bool xfs_iext_get_extent(struct xfs_ifork *ifp,
199 struct xfs_iext_cursor *cur,
200 struct xfs_bmbt_irec *gotp);
201void xfs_iext_update_extent(struct xfs_inode *ip, int state,
202 struct xfs_iext_cursor *cur,
203 struct xfs_bmbt_irec *gotp);
204
205void xfs_iext_first(struct xfs_ifork *, struct xfs_iext_cursor *);
206void xfs_iext_last(struct xfs_ifork *, struct xfs_iext_cursor *);
207void xfs_iext_next(struct xfs_ifork *, struct xfs_iext_cursor *);
208void xfs_iext_prev(struct xfs_ifork *, struct xfs_iext_cursor *);
209
210static inline bool xfs_iext_next_extent(struct xfs_ifork *ifp,
211 struct xfs_iext_cursor *cur, struct xfs_bmbt_irec *gotp)
212{
213 xfs_iext_next(ifp, cur);
214 return xfs_iext_get_extent(ifp, cur, gotp);
215}
216
217static inline bool xfs_iext_prev_extent(struct xfs_ifork *ifp,
218 struct xfs_iext_cursor *cur, struct xfs_bmbt_irec *gotp)
219{
220 xfs_iext_prev(ifp, cur);
221 return xfs_iext_get_extent(ifp, cur, gotp);
222}
223
224/*
225 * Return the extent after cur in gotp without updating the cursor.
226 */
227static inline bool xfs_iext_peek_next_extent(struct xfs_ifork *ifp,
228 struct xfs_iext_cursor *cur, struct xfs_bmbt_irec *gotp)
229{
230 struct xfs_iext_cursor ncur = *cur;
231
232 xfs_iext_next(ifp, &ncur);
233 return xfs_iext_get_extent(ifp, &ncur, gotp);
234}
235
236/*
237 * Return the extent before cur in gotp without updating the cursor.
238 */
239static inline bool xfs_iext_peek_prev_extent(struct xfs_ifork *ifp,
240 struct xfs_iext_cursor *cur, struct xfs_bmbt_irec *gotp)
241{
242 struct xfs_iext_cursor ncur = *cur;
243
244 xfs_iext_prev(ifp, &ncur);
245 return xfs_iext_get_extent(ifp, &ncur, gotp);
246}
247
248#define for_each_xfs_iext(ifp, ext, got) \
249 for (xfs_iext_first((ifp), (ext)); \
250 xfs_iext_get_extent((ifp), (ext), (got)); \
251 xfs_iext_next((ifp), (ext)))
252
253extern struct kmem_cache *xfs_ifork_cache;
254
255extern void xfs_ifork_init_cow(struct xfs_inode *ip);
256
257int xfs_ifork_verify_local_data(struct xfs_inode *ip);
258int xfs_ifork_verify_local_attr(struct xfs_inode *ip);
259int xfs_iext_count_may_overflow(struct xfs_inode *ip, int whichfork,
260 int nr_to_add);
261int xfs_iext_count_upgrade(struct xfs_trans *tp, struct xfs_inode *ip,
262 uint nr_to_add);
263bool xfs_ifork_is_realtime(struct xfs_inode *ip, int whichfork);
264
265/* returns true if the fork has extents but they are not read in yet. */
266static inline bool xfs_need_iread_extents(const struct xfs_ifork *ifp)
267{
268 /* see xfs_iformat_{data,attr}_fork() for needextents semantics */
269 return smp_load_acquire(&ifp->if_needextents) != 0;
270}
271
272#endif /* __XFS_INODE_FORK_H__ */
1/*
2 * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18#ifndef __XFS_INODE_FORK_H__
19#define __XFS_INODE_FORK_H__
20
21struct xfs_inode_log_item;
22struct xfs_dinode;
23
24/*
25 * File incore extent information, present for each of data & attr forks.
26 */
27typedef struct xfs_ifork {
28 int if_bytes; /* bytes in if_u1 */
29 int if_real_bytes; /* bytes allocated in if_u1 */
30 struct xfs_btree_block *if_broot; /* file's incore btree root */
31 short if_broot_bytes; /* bytes allocated for root */
32 unsigned char if_flags; /* per-fork flags */
33 int if_height; /* height of the extent tree */
34 union {
35 void *if_root; /* extent tree root */
36 char *if_data; /* inline file data */
37 } if_u1;
38} xfs_ifork_t;
39
40/*
41 * Per-fork incore inode flags.
42 */
43#define XFS_IFINLINE 0x01 /* Inline data is read in */
44#define XFS_IFEXTENTS 0x02 /* All extent pointers are read in */
45#define XFS_IFBROOT 0x04 /* i_broot points to the bmap b-tree root */
46
47/*
48 * Fork handling.
49 */
50
51#define XFS_IFORK_Q(ip) ((ip)->i_d.di_forkoff != 0)
52#define XFS_IFORK_BOFF(ip) ((int)((ip)->i_d.di_forkoff << 3))
53
54#define XFS_IFORK_PTR(ip,w) \
55 ((w) == XFS_DATA_FORK ? \
56 &(ip)->i_df : \
57 ((w) == XFS_ATTR_FORK ? \
58 (ip)->i_afp : \
59 (ip)->i_cowfp))
60#define XFS_IFORK_DSIZE(ip) \
61 (XFS_IFORK_Q(ip) ? \
62 XFS_IFORK_BOFF(ip) : \
63 XFS_LITINO((ip)->i_mount, (ip)->i_d.di_version))
64#define XFS_IFORK_ASIZE(ip) \
65 (XFS_IFORK_Q(ip) ? \
66 XFS_LITINO((ip)->i_mount, (ip)->i_d.di_version) - \
67 XFS_IFORK_BOFF(ip) : \
68 0)
69#define XFS_IFORK_SIZE(ip,w) \
70 ((w) == XFS_DATA_FORK ? \
71 XFS_IFORK_DSIZE(ip) : \
72 ((w) == XFS_ATTR_FORK ? \
73 XFS_IFORK_ASIZE(ip) : \
74 0))
75#define XFS_IFORK_FORMAT(ip,w) \
76 ((w) == XFS_DATA_FORK ? \
77 (ip)->i_d.di_format : \
78 ((w) == XFS_ATTR_FORK ? \
79 (ip)->i_d.di_aformat : \
80 (ip)->i_cformat))
81#define XFS_IFORK_FMT_SET(ip,w,n) \
82 ((w) == XFS_DATA_FORK ? \
83 ((ip)->i_d.di_format = (n)) : \
84 ((w) == XFS_ATTR_FORK ? \
85 ((ip)->i_d.di_aformat = (n)) : \
86 ((ip)->i_cformat = (n))))
87#define XFS_IFORK_NEXTENTS(ip,w) \
88 ((w) == XFS_DATA_FORK ? \
89 (ip)->i_d.di_nextents : \
90 ((w) == XFS_ATTR_FORK ? \
91 (ip)->i_d.di_anextents : \
92 (ip)->i_cnextents))
93#define XFS_IFORK_NEXT_SET(ip,w,n) \
94 ((w) == XFS_DATA_FORK ? \
95 ((ip)->i_d.di_nextents = (n)) : \
96 ((w) == XFS_ATTR_FORK ? \
97 ((ip)->i_d.di_anextents = (n)) : \
98 ((ip)->i_cnextents = (n))))
99#define XFS_IFORK_MAXEXT(ip, w) \
100 (XFS_IFORK_SIZE(ip, w) / sizeof(xfs_bmbt_rec_t))
101
102struct xfs_ifork *xfs_iext_state_to_fork(struct xfs_inode *ip, int state);
103
104int xfs_iformat_fork(struct xfs_inode *, struct xfs_dinode *);
105void xfs_iflush_fork(struct xfs_inode *, struct xfs_dinode *,
106 struct xfs_inode_log_item *, int);
107void xfs_idestroy_fork(struct xfs_inode *, int);
108void xfs_idata_realloc(struct xfs_inode *, int, int);
109void xfs_iroot_realloc(struct xfs_inode *, int, int);
110int xfs_iread_extents(struct xfs_trans *, struct xfs_inode *, int);
111int xfs_iextents_copy(struct xfs_inode *, struct xfs_bmbt_rec *,
112 int);
113void xfs_init_local_fork(struct xfs_inode *, int, const void *, int);
114
115xfs_extnum_t xfs_iext_count(struct xfs_ifork *ifp);
116void xfs_iext_insert(struct xfs_inode *, struct xfs_iext_cursor *cur,
117 struct xfs_bmbt_irec *, int);
118void xfs_iext_remove(struct xfs_inode *, struct xfs_iext_cursor *,
119 int);
120void xfs_iext_destroy(struct xfs_ifork *);
121
122bool xfs_iext_lookup_extent(struct xfs_inode *ip,
123 struct xfs_ifork *ifp, xfs_fileoff_t bno,
124 struct xfs_iext_cursor *cur,
125 struct xfs_bmbt_irec *gotp);
126bool xfs_iext_lookup_extent_before(struct xfs_inode *ip,
127 struct xfs_ifork *ifp, xfs_fileoff_t *end,
128 struct xfs_iext_cursor *cur,
129 struct xfs_bmbt_irec *gotp);
130bool xfs_iext_get_extent(struct xfs_ifork *ifp,
131 struct xfs_iext_cursor *cur,
132 struct xfs_bmbt_irec *gotp);
133void xfs_iext_update_extent(struct xfs_inode *ip, int state,
134 struct xfs_iext_cursor *cur,
135 struct xfs_bmbt_irec *gotp);
136
137void xfs_iext_first(struct xfs_ifork *, struct xfs_iext_cursor *);
138void xfs_iext_last(struct xfs_ifork *, struct xfs_iext_cursor *);
139void xfs_iext_next(struct xfs_ifork *, struct xfs_iext_cursor *);
140void xfs_iext_prev(struct xfs_ifork *, struct xfs_iext_cursor *);
141
142static inline bool xfs_iext_next_extent(struct xfs_ifork *ifp,
143 struct xfs_iext_cursor *cur, struct xfs_bmbt_irec *gotp)
144{
145 xfs_iext_next(ifp, cur);
146 return xfs_iext_get_extent(ifp, cur, gotp);
147}
148
149static inline bool xfs_iext_prev_extent(struct xfs_ifork *ifp,
150 struct xfs_iext_cursor *cur, struct xfs_bmbt_irec *gotp)
151{
152 xfs_iext_prev(ifp, cur);
153 return xfs_iext_get_extent(ifp, cur, gotp);
154}
155
156/*
157 * Return the extent after cur in gotp without updating the cursor.
158 */
159static inline bool xfs_iext_peek_next_extent(struct xfs_ifork *ifp,
160 struct xfs_iext_cursor *cur, struct xfs_bmbt_irec *gotp)
161{
162 struct xfs_iext_cursor ncur = *cur;
163
164 xfs_iext_next(ifp, &ncur);
165 return xfs_iext_get_extent(ifp, &ncur, gotp);
166}
167
168/*
169 * Return the extent before cur in gotp without updating the cursor.
170 */
171static inline bool xfs_iext_peek_prev_extent(struct xfs_ifork *ifp,
172 struct xfs_iext_cursor *cur, struct xfs_bmbt_irec *gotp)
173{
174 struct xfs_iext_cursor ncur = *cur;
175
176 xfs_iext_prev(ifp, &ncur);
177 return xfs_iext_get_extent(ifp, &ncur, gotp);
178}
179
180#define for_each_xfs_iext(ifp, ext, got) \
181 for (xfs_iext_first((ifp), (ext)); \
182 xfs_iext_get_extent((ifp), (ext), (got)); \
183 xfs_iext_next((ifp), (ext)))
184
185extern struct kmem_zone *xfs_ifork_zone;
186
187extern void xfs_ifork_init_cow(struct xfs_inode *ip);
188
189typedef xfs_failaddr_t (*xfs_ifork_verifier_t)(struct xfs_inode *);
190
191struct xfs_ifork_ops {
192 xfs_ifork_verifier_t verify_symlink;
193 xfs_ifork_verifier_t verify_dir;
194 xfs_ifork_verifier_t verify_attr;
195};
196extern struct xfs_ifork_ops xfs_default_ifork_ops;
197
198xfs_failaddr_t xfs_ifork_verify_data(struct xfs_inode *ip,
199 struct xfs_ifork_ops *ops);
200xfs_failaddr_t xfs_ifork_verify_attr(struct xfs_inode *ip,
201 struct xfs_ifork_ops *ops);
202
203#endif /* __XFS_INODE_FORK_H__ */