Merge tag 'mailbox-v6.3' of git://git.linaro.org/landing-teams/working/fujitsu/integr...
[linux-block.git] / fs / xfs / libxfs / xfs_inode_fork.h
CommitLineData
0b61f8a4 1// SPDX-License-Identifier: GPL-2.0
5c4d97d0
DC
2/*
3 * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
4 * All Rights Reserved.
5c4d97d0
DC
5 */
6#ifndef __XFS_INODE_FORK_H__
7#define __XFS_INODE_FORK_H__
8
9struct xfs_inode_log_item;
a4fbe6ab 10struct xfs_dinode;
5c4d97d0 11
5c4d97d0
DC
12/*
13 * File incore extent information, present for each of data & attr forks.
14 */
3ba738df 15struct xfs_ifork {
3f8a4f1d 16 int64_t if_bytes; /* bytes in if_u1 */
5c4d97d0 17 struct xfs_btree_block *if_broot; /* file's incore btree root */
3f8a4f1d 18 unsigned int if_seq; /* fork mod counter */
6bdcf26a 19 int if_height; /* height of the extent tree */
5c4d97d0 20 union {
6bdcf26a 21 void *if_root; /* extent tree root */
5c4d97d0
DC
22 char *if_data; /* inline file data */
23 } if_u1;
755c38ff 24 xfs_extnum_t if_nextents; /* # of extents in this fork */
3f8a4f1d 25 short if_broot_bytes; /* bytes allocated for root */
f7e67b20 26 int8_t if_format; /* format of this fork */
3ba738df 27};
5c4d97d0 28
727e1acd
CB
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
85ef08b5
CB
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
3a19bb14
CB
42/*
43 * Adding/removing an xattr can cause XFS_DA_NODE_MAXDEPTH extents to
44 * be added. One extra extent for dabtree in case a local attr is
45 * large enough to cause a double split. It can also cause extent
46 * count to increase proportional to the size of a remote xattr's
47 * value.
48 */
49#define XFS_IEXT_ATTR_MANIP_CNT(rmt_blks) \
50 (XFS_DA_NODE_MAXDEPTH + max(1, rmt_blks))
51
c442f308
CB
52/*
53 * A write to a sub-interval of an existing unwritten extent causes the original
54 * extent to be split into 3 extents
55 * i.e. | Unwritten | Real | Unwritten |
56 * Hence extent count can increase by 2.
57 */
58#define XFS_IEXT_WRITE_UNWRITTEN_CNT (2)
59
60
5f1d5bbf
CB
61/*
62 * Moving an extent to data fork can cause a sub-interval of an existing extent
63 * to be unmapped. This will increase extent count by 1. Mapping in the new
64 * extent can increase the extent count by 1 again i.e.
65 * | Old extent | New extent | Old extent |
66 * Hence number of extents increases by 2.
67 */
68#define XFS_IEXT_REFLINK_END_COW_CNT (2)
69
bcc561f2
CB
70/*
71 * Removing an initial range of source/donor file's extent and adding a new
72 * extent (from donor/source file) in its place will cause extent count to
73 * increase by 1.
74 */
75#define XFS_IEXT_SWAP_RMAP_CNT (1)
76
5c4d97d0
DC
77/*
78 * Fork handling.
79 */
5c4d97d0 80#define XFS_IFORK_MAXEXT(ip, w) \
c01147d9 81 (xfs_inode_fork_size(ip, w) / sizeof(xfs_bmbt_rec_t))
5c4d97d0 82
f7e67b20
CH
83static inline bool xfs_ifork_has_extents(struct xfs_ifork *ifp)
84{
85 return ifp->if_format == XFS_DINODE_FMT_EXTENTS ||
86 ifp->if_format == XFS_DINODE_FMT_BTREE;
87}
2fe4f928 88
daf83964
CH
89static inline xfs_extnum_t xfs_ifork_nextents(struct xfs_ifork *ifp)
90{
91 if (!ifp)
92 return 0;
93 return ifp->if_nextents;
94}
95
f7e67b20
CH
96static inline int8_t xfs_ifork_format(struct xfs_ifork *ifp)
97{
98 if (!ifp)
99 return XFS_DINODE_FMT_EXTENTS;
100 return ifp->if_format;
101}
102
df9ad5cc
CB
103static inline xfs_extnum_t xfs_iext_max_nextents(bool has_large_extent_counts,
104 int whichfork)
9feb8f19 105{
df9ad5cc
CB
106 switch (whichfork) {
107 case XFS_DATA_FORK:
108 case XFS_COW_FORK:
109 if (has_large_extent_counts)
110 return XFS_MAX_EXTCNT_DATA_FORK_LARGE;
111 return XFS_MAX_EXTCNT_DATA_FORK_SMALL;
112
113 case XFS_ATTR_FORK:
114 if (has_large_extent_counts)
115 return XFS_MAX_EXTCNT_ATTR_FORK_LARGE;
116 return XFS_MAX_EXTCNT_ATTR_FORK_SMALL;
117
118 default:
119 ASSERT(0);
120 return 0;
121 }
9feb8f19
CB
122}
123
dd95a6ce
CB
124static inline xfs_extnum_t
125xfs_dfork_data_extents(
126 struct xfs_dinode *dip)
127{
52a4a148
CB
128 if (xfs_dinode_has_large_extent_counts(dip))
129 return be64_to_cpu(dip->di_big_nextents);
130
dd95a6ce
CB
131 return be32_to_cpu(dip->di_nextents);
132}
133
134static inline xfs_extnum_t
135xfs_dfork_attr_extents(
136 struct xfs_dinode *dip)
137{
52a4a148
CB
138 if (xfs_dinode_has_large_extent_counts(dip))
139 return be32_to_cpu(dip->di_big_anextents);
140
dd95a6ce
CB
141 return be16_to_cpu(dip->di_anextents);
142}
143
144static inline xfs_extnum_t
145xfs_dfork_nextents(
146 struct xfs_dinode *dip,
147 int whichfork)
148{
149 switch (whichfork) {
150 case XFS_DATA_FORK:
151 return xfs_dfork_data_extents(dip);
152 case XFS_ATTR_FORK:
153 return xfs_dfork_attr_extents(dip);
154 default:
155 ASSERT(0);
156 break;
157 }
158
159 return 0;
160}
161
2ed5b09b
DW
162void xfs_ifork_zap_attr(struct xfs_inode *ip);
163void xfs_ifork_init_attr(struct xfs_inode *ip, enum xfs_dinode_fmt format,
164 xfs_extnum_t nextents);
3993baeb
DW
165struct xfs_ifork *xfs_iext_state_to_fork(struct xfs_inode *ip, int state);
166
9229d18e
CH
167int xfs_iformat_data_fork(struct xfs_inode *, struct xfs_dinode *);
168int xfs_iformat_attr_fork(struct xfs_inode *, struct xfs_dinode *);
78420281 169void xfs_iflush_fork(struct xfs_inode *, struct xfs_dinode *,
fd9fdba6 170 struct xfs_inode_log_item *, int);
ef838512 171void xfs_idestroy_fork(struct xfs_ifork *ifp);
3f8a4f1d
DC
172void xfs_idata_realloc(struct xfs_inode *ip, int64_t byte_diff,
173 int whichfork);
5c4d97d0
DC
174void xfs_iroot_realloc(struct xfs_inode *, int, int);
175int xfs_iread_extents(struct xfs_trans *, struct xfs_inode *, int);
176int xfs_iextents_copy(struct xfs_inode *, struct xfs_bmbt_rec *,
177 int);
3f8a4f1d
DC
178void xfs_init_local_fork(struct xfs_inode *ip, int whichfork,
179 const void *data, int64_t size);
5c4d97d0 180
6bdcf26a 181xfs_extnum_t xfs_iext_count(struct xfs_ifork *ifp);
b2b1712a 182void xfs_iext_insert(struct xfs_inode *, struct xfs_iext_cursor *cur,
0254c2f2 183 struct xfs_bmbt_irec *, int);
b2b1712a 184void xfs_iext_remove(struct xfs_inode *, struct xfs_iext_cursor *,
c38ccf59 185 int);
5c4d97d0 186void xfs_iext_destroy(struct xfs_ifork *);
5c4d97d0 187
93533c78
CH
188bool xfs_iext_lookup_extent(struct xfs_inode *ip,
189 struct xfs_ifork *ifp, xfs_fileoff_t bno,
b2b1712a
CH
190 struct xfs_iext_cursor *cur,
191 struct xfs_bmbt_irec *gotp);
dc56015f
CH
192bool xfs_iext_lookup_extent_before(struct xfs_inode *ip,
193 struct xfs_ifork *ifp, xfs_fileoff_t *end,
b2b1712a
CH
194 struct xfs_iext_cursor *cur,
195 struct xfs_bmbt_irec *gotp);
196bool xfs_iext_get_extent(struct xfs_ifork *ifp,
197 struct xfs_iext_cursor *cur,
93533c78 198 struct xfs_bmbt_irec *gotp);
ca5d8e5b 199void xfs_iext_update_extent(struct xfs_inode *ip, int state,
b2b1712a
CH
200 struct xfs_iext_cursor *cur,
201 struct xfs_bmbt_irec *gotp);
202
6bdcf26a
CH
203void xfs_iext_first(struct xfs_ifork *, struct xfs_iext_cursor *);
204void xfs_iext_last(struct xfs_ifork *, struct xfs_iext_cursor *);
205void xfs_iext_next(struct xfs_ifork *, struct xfs_iext_cursor *);
206void xfs_iext_prev(struct xfs_ifork *, struct xfs_iext_cursor *);
b2b1712a
CH
207
208static inline bool xfs_iext_next_extent(struct xfs_ifork *ifp,
209 struct xfs_iext_cursor *cur, struct xfs_bmbt_irec *gotp)
210{
211 xfs_iext_next(ifp, cur);
212 return xfs_iext_get_extent(ifp, cur, gotp);
213}
214
215static inline bool xfs_iext_prev_extent(struct xfs_ifork *ifp,
216 struct xfs_iext_cursor *cur, struct xfs_bmbt_irec *gotp)
217{
218 xfs_iext_prev(ifp, cur);
219 return xfs_iext_get_extent(ifp, cur, gotp);
220}
221
222/*
223 * Return the extent after cur in gotp without updating the cursor.
224 */
225static inline bool xfs_iext_peek_next_extent(struct xfs_ifork *ifp,
226 struct xfs_iext_cursor *cur, struct xfs_bmbt_irec *gotp)
227{
228 struct xfs_iext_cursor ncur = *cur;
229
230 xfs_iext_next(ifp, &ncur);
231 return xfs_iext_get_extent(ifp, &ncur, gotp);
232}
233
234/*
235 * Return the extent before cur in gotp without updating the cursor.
236 */
237static inline bool xfs_iext_peek_prev_extent(struct xfs_ifork *ifp,
238 struct xfs_iext_cursor *cur, struct xfs_bmbt_irec *gotp)
239{
240 struct xfs_iext_cursor ncur = *cur;
241
242 xfs_iext_prev(ifp, &ncur);
243 return xfs_iext_get_extent(ifp, &ncur, gotp);
244}
245
246#define for_each_xfs_iext(ifp, ext, got) \
247 for (xfs_iext_first((ifp), (ext)); \
248 xfs_iext_get_extent((ifp), (ext), (got)); \
249 xfs_iext_next((ifp), (ext)))
93533c78 250
182696fb 251extern struct kmem_cache *xfs_ifork_cache;
5c4d97d0 252
3993baeb
DW
253extern void xfs_ifork_init_cow(struct xfs_inode *ip);
254
7c7ba218
CH
255int xfs_ifork_verify_local_data(struct xfs_inode *ip);
256int xfs_ifork_verify_local_attr(struct xfs_inode *ip);
b9b7e1dc
CB
257int xfs_iext_count_may_overflow(struct xfs_inode *ip, int whichfork,
258 int nr_to_add);
4f86bb4b
CB
259int xfs_iext_count_upgrade(struct xfs_trans *tp, struct xfs_inode *ip,
260 uint nr_to_add);
9cfb9b47 261
b2197a36
CH
262/* returns true if the fork has extents but they are not read in yet. */
263static inline bool xfs_need_iread_extents(struct xfs_ifork *ifp)
264{
265 return ifp->if_format == XFS_DINODE_FMT_BTREE && ifp->if_height == 0;
266}
267
5c4d97d0 268#endif /* __XFS_INODE_FORK_H__ */