10b61f8a4SDave Chinner // SPDX-License-Identifier: GPL-2.0
284be0ffcSDave Chinner /*
384be0ffcSDave Chinner * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
484be0ffcSDave Chinner * All Rights Reserved.
584be0ffcSDave Chinner */
684be0ffcSDave Chinner #ifndef __XFS_INODE_FORK_H__
784be0ffcSDave Chinner #define __XFS_INODE_FORK_H__
884be0ffcSDave Chinner
984be0ffcSDave Chinner struct xfs_inode_log_item;
1084be0ffcSDave Chinner struct xfs_dinode;
1184be0ffcSDave Chinner
1284be0ffcSDave Chinner /*
1384be0ffcSDave Chinner * File incore extent information, present for each of data & attr forks.
1484be0ffcSDave Chinner */
153ba738dfSChristoph Hellwig struct xfs_ifork {
163f8a4f1dSDave Chinner int64_t if_bytes; /* bytes in if_u1 */
1784be0ffcSDave Chinner struct xfs_btree_block *if_broot; /* file's incore btree root */
183f8a4f1dSDave Chinner unsigned int if_seq; /* fork mod counter */
196bdcf26aSChristoph Hellwig int if_height; /* height of the extent tree */
2084be0ffcSDave Chinner union {
216bdcf26aSChristoph Hellwig void *if_root; /* extent tree root */
2284be0ffcSDave Chinner char *if_data; /* inline file data */
2384be0ffcSDave Chinner } if_u1;
24755c38ffSChandan Babu R xfs_extnum_t if_nextents; /* # of extents in this fork */
253f8a4f1dSDave Chinner short if_broot_bytes; /* bytes allocated for root */
26f7e67b20SChristoph Hellwig int8_t if_format; /* format of this fork */
27*c95356caSDarrick J. Wong uint8_t if_needextents; /* extents have not been read */
283ba738dfSChristoph Hellwig };
2984be0ffcSDave Chinner
3084be0ffcSDave Chinner /*
31727e1acdSChandan Babu R * Worst-case increase in the fork extent count when we're adding a single
32727e1acdSChandan Babu R * extent to a fork and there's no possibility of splitting an existing mapping.
33727e1acdSChandan Babu R */
34727e1acdSChandan Babu R #define XFS_IEXT_ADD_NOSPLIT_CNT (1)
35727e1acdSChandan Babu R
36727e1acdSChandan Babu R /*
3785ef08b5SChandan Babu R * Punching out an extent from the middle of an existing extent can cause the
3885ef08b5SChandan Babu R * extent count to increase by 1.
3985ef08b5SChandan Babu R * i.e. | Old extent | Hole | Old extent |
4085ef08b5SChandan Babu R */
4185ef08b5SChandan Babu R #define XFS_IEXT_PUNCH_HOLE_CNT (1)
4285ef08b5SChandan Babu R
4385ef08b5SChandan Babu R /*
443a19bb14SChandan Babu R * Adding/removing an xattr can cause XFS_DA_NODE_MAXDEPTH extents to
453a19bb14SChandan Babu R * be added. One extra extent for dabtree in case a local attr is
463a19bb14SChandan Babu R * large enough to cause a double split. It can also cause extent
473a19bb14SChandan Babu R * count to increase proportional to the size of a remote xattr's
483a19bb14SChandan Babu R * value.
493a19bb14SChandan Babu R */
503a19bb14SChandan Babu R #define XFS_IEXT_ATTR_MANIP_CNT(rmt_blks) \
513a19bb14SChandan Babu R (XFS_DA_NODE_MAXDEPTH + max(1, rmt_blks))
523a19bb14SChandan Babu R
533a19bb14SChandan Babu R /*
54c442f308SChandan Babu R * A write to a sub-interval of an existing unwritten extent causes the original
55c442f308SChandan Babu R * extent to be split into 3 extents
56c442f308SChandan Babu R * i.e. | Unwritten | Real | Unwritten |
57c442f308SChandan Babu R * Hence extent count can increase by 2.
58c442f308SChandan Babu R */
59c442f308SChandan Babu R #define XFS_IEXT_WRITE_UNWRITTEN_CNT (2)
60c442f308SChandan Babu R
61c442f308SChandan Babu R
62c442f308SChandan Babu R /*
635f1d5bbfSChandan Babu R * Moving an extent to data fork can cause a sub-interval of an existing extent
645f1d5bbfSChandan Babu R * to be unmapped. This will increase extent count by 1. Mapping in the new
655f1d5bbfSChandan Babu R * extent can increase the extent count by 1 again i.e.
665f1d5bbfSChandan Babu R * | Old extent | New extent | Old extent |
675f1d5bbfSChandan Babu R * Hence number of extents increases by 2.
685f1d5bbfSChandan Babu R */
695f1d5bbfSChandan Babu R #define XFS_IEXT_REFLINK_END_COW_CNT (2)
705f1d5bbfSChandan Babu R
715f1d5bbfSChandan Babu R /*
72bcc561f2SChandan Babu R * Removing an initial range of source/donor file's extent and adding a new
73bcc561f2SChandan Babu R * extent (from donor/source file) in its place will cause extent count to
74bcc561f2SChandan Babu R * increase by 1.
75bcc561f2SChandan Babu R */
76bcc561f2SChandan Babu R #define XFS_IEXT_SWAP_RMAP_CNT (1)
77bcc561f2SChandan Babu R
78bcc561f2SChandan Babu R /*
7984be0ffcSDave Chinner * Fork handling.
8084be0ffcSDave Chinner */
8184be0ffcSDave Chinner #define XFS_IFORK_MAXEXT(ip, w) \
82c01147d9SDarrick J. Wong (xfs_inode_fork_size(ip, w) / sizeof(xfs_bmbt_rec_t))
8384be0ffcSDave Chinner
xfs_ifork_has_extents(struct xfs_ifork * ifp)84f7e67b20SChristoph Hellwig static inline bool xfs_ifork_has_extents(struct xfs_ifork *ifp)
85f7e67b20SChristoph Hellwig {
86f7e67b20SChristoph Hellwig return ifp->if_format == XFS_DINODE_FMT_EXTENTS ||
87f7e67b20SChristoph Hellwig ifp->if_format == XFS_DINODE_FMT_BTREE;
88f7e67b20SChristoph Hellwig }
892fe4f928SDarrick J. Wong
xfs_ifork_nextents(struct xfs_ifork * ifp)90daf83964SChristoph Hellwig static inline xfs_extnum_t xfs_ifork_nextents(struct xfs_ifork *ifp)
91daf83964SChristoph Hellwig {
92daf83964SChristoph Hellwig if (!ifp)
93daf83964SChristoph Hellwig return 0;
94daf83964SChristoph Hellwig return ifp->if_nextents;
95daf83964SChristoph Hellwig }
96daf83964SChristoph Hellwig
xfs_ifork_format(struct xfs_ifork * ifp)97f7e67b20SChristoph Hellwig static inline int8_t xfs_ifork_format(struct xfs_ifork *ifp)
98f7e67b20SChristoph Hellwig {
99f7e67b20SChristoph Hellwig if (!ifp)
100f7e67b20SChristoph Hellwig return XFS_DINODE_FMT_EXTENTS;
101f7e67b20SChristoph Hellwig return ifp->if_format;
102f7e67b20SChristoph Hellwig }
103f7e67b20SChristoph Hellwig
xfs_iext_max_nextents(bool has_large_extent_counts,int whichfork)104df9ad5ccSChandan Babu R static inline xfs_extnum_t xfs_iext_max_nextents(bool has_large_extent_counts,
105df9ad5ccSChandan Babu R int whichfork)
1069feb8f19SChandan Babu R {
107df9ad5ccSChandan Babu R switch (whichfork) {
108df9ad5ccSChandan Babu R case XFS_DATA_FORK:
109df9ad5ccSChandan Babu R case XFS_COW_FORK:
110df9ad5ccSChandan Babu R if (has_large_extent_counts)
111df9ad5ccSChandan Babu R return XFS_MAX_EXTCNT_DATA_FORK_LARGE;
112df9ad5ccSChandan Babu R return XFS_MAX_EXTCNT_DATA_FORK_SMALL;
1139feb8f19SChandan Babu R
114df9ad5ccSChandan Babu R case XFS_ATTR_FORK:
115df9ad5ccSChandan Babu R if (has_large_extent_counts)
116df9ad5ccSChandan Babu R return XFS_MAX_EXTCNT_ATTR_FORK_LARGE;
117df9ad5ccSChandan Babu R return XFS_MAX_EXTCNT_ATTR_FORK_SMALL;
118df9ad5ccSChandan Babu R
119df9ad5ccSChandan Babu R default:
120df9ad5ccSChandan Babu R ASSERT(0);
121df9ad5ccSChandan Babu R return 0;
122df9ad5ccSChandan Babu R }
1239feb8f19SChandan Babu R }
1249feb8f19SChandan Babu R
125dd95a6ceSChandan Babu R static inline xfs_extnum_t
xfs_dfork_data_extents(struct xfs_dinode * dip)126dd95a6ceSChandan Babu R xfs_dfork_data_extents(
127dd95a6ceSChandan Babu R struct xfs_dinode *dip)
128dd95a6ceSChandan Babu R {
12952a4a148SChandan Babu R if (xfs_dinode_has_large_extent_counts(dip))
13052a4a148SChandan Babu R return be64_to_cpu(dip->di_big_nextents);
13152a4a148SChandan Babu R
132dd95a6ceSChandan Babu R return be32_to_cpu(dip->di_nextents);
133dd95a6ceSChandan Babu R }
134dd95a6ceSChandan Babu R
135dd95a6ceSChandan Babu R static inline xfs_extnum_t
xfs_dfork_attr_extents(struct xfs_dinode * dip)136dd95a6ceSChandan Babu R xfs_dfork_attr_extents(
137dd95a6ceSChandan Babu R struct xfs_dinode *dip)
138dd95a6ceSChandan Babu R {
13952a4a148SChandan Babu R if (xfs_dinode_has_large_extent_counts(dip))
14052a4a148SChandan Babu R return be32_to_cpu(dip->di_big_anextents);
14152a4a148SChandan Babu R
142dd95a6ceSChandan Babu R return be16_to_cpu(dip->di_anextents);
143dd95a6ceSChandan Babu R }
144dd95a6ceSChandan Babu R
145dd95a6ceSChandan Babu R static inline xfs_extnum_t
xfs_dfork_nextents(struct xfs_dinode * dip,int whichfork)146dd95a6ceSChandan Babu R xfs_dfork_nextents(
147dd95a6ceSChandan Babu R struct xfs_dinode *dip,
148dd95a6ceSChandan Babu R int whichfork)
149dd95a6ceSChandan Babu R {
150dd95a6ceSChandan Babu R switch (whichfork) {
151dd95a6ceSChandan Babu R case XFS_DATA_FORK:
152dd95a6ceSChandan Babu R return xfs_dfork_data_extents(dip);
153dd95a6ceSChandan Babu R case XFS_ATTR_FORK:
154dd95a6ceSChandan Babu R return xfs_dfork_attr_extents(dip);
155dd95a6ceSChandan Babu R default:
156dd95a6ceSChandan Babu R ASSERT(0);
157dd95a6ceSChandan Babu R break;
158dd95a6ceSChandan Babu R }
159dd95a6ceSChandan Babu R
160dd95a6ceSChandan Babu R return 0;
161dd95a6ceSChandan Babu R }
162dd95a6ceSChandan Babu R
1632ed5b09bSDarrick J. Wong void xfs_ifork_zap_attr(struct xfs_inode *ip);
1642ed5b09bSDarrick J. Wong void xfs_ifork_init_attr(struct xfs_inode *ip, enum xfs_dinode_fmt format,
165e6a688c3SDave Chinner xfs_extnum_t nextents);
1663993baebSDarrick J. Wong struct xfs_ifork *xfs_iext_state_to_fork(struct xfs_inode *ip, int state);
1673993baebSDarrick J. Wong
1689229d18eSChristoph Hellwig int xfs_iformat_data_fork(struct xfs_inode *, struct xfs_dinode *);
1699229d18eSChristoph Hellwig int xfs_iformat_attr_fork(struct xfs_inode *, struct xfs_dinode *);
17078420281SDarrick J. Wong void xfs_iflush_fork(struct xfs_inode *, struct xfs_dinode *,
17184be0ffcSDave Chinner struct xfs_inode_log_item *, int);
172ef838512SChristoph Hellwig void xfs_idestroy_fork(struct xfs_ifork *ifp);
1733f8a4f1dSDave Chinner void xfs_idata_realloc(struct xfs_inode *ip, int64_t byte_diff,
1743f8a4f1dSDave Chinner int whichfork);
17584be0ffcSDave Chinner void xfs_iroot_realloc(struct xfs_inode *, int, int);
17684be0ffcSDave Chinner int xfs_iread_extents(struct xfs_trans *, struct xfs_inode *, int);
17784be0ffcSDave Chinner int xfs_iextents_copy(struct xfs_inode *, struct xfs_bmbt_rec *,
17884be0ffcSDave Chinner int);
1793f8a4f1dSDave Chinner void xfs_init_local_fork(struct xfs_inode *ip, int whichfork,
1803f8a4f1dSDave Chinner const void *data, int64_t size);
18184be0ffcSDave Chinner
1826bdcf26aSChristoph Hellwig xfs_extnum_t xfs_iext_count(struct xfs_ifork *ifp);
183b2b1712aSChristoph Hellwig void xfs_iext_insert(struct xfs_inode *, struct xfs_iext_cursor *cur,
1840254c2f2SChristoph Hellwig struct xfs_bmbt_irec *, int);
185b2b1712aSChristoph Hellwig void xfs_iext_remove(struct xfs_inode *, struct xfs_iext_cursor *,
186c38ccf59SChristoph Hellwig int);
18784be0ffcSDave Chinner void xfs_iext_destroy(struct xfs_ifork *);
18884be0ffcSDave Chinner
18993533c78SChristoph Hellwig bool xfs_iext_lookup_extent(struct xfs_inode *ip,
19093533c78SChristoph Hellwig struct xfs_ifork *ifp, xfs_fileoff_t bno,
191b2b1712aSChristoph Hellwig struct xfs_iext_cursor *cur,
192b2b1712aSChristoph Hellwig struct xfs_bmbt_irec *gotp);
193dc56015fSChristoph Hellwig bool xfs_iext_lookup_extent_before(struct xfs_inode *ip,
194dc56015fSChristoph Hellwig struct xfs_ifork *ifp, xfs_fileoff_t *end,
195b2b1712aSChristoph Hellwig struct xfs_iext_cursor *cur,
196b2b1712aSChristoph Hellwig struct xfs_bmbt_irec *gotp);
197b2b1712aSChristoph Hellwig bool xfs_iext_get_extent(struct xfs_ifork *ifp,
198b2b1712aSChristoph Hellwig struct xfs_iext_cursor *cur,
19993533c78SChristoph Hellwig struct xfs_bmbt_irec *gotp);
200ca5d8e5bSChristoph Hellwig void xfs_iext_update_extent(struct xfs_inode *ip, int state,
201b2b1712aSChristoph Hellwig struct xfs_iext_cursor *cur,
202b2b1712aSChristoph Hellwig struct xfs_bmbt_irec *gotp);
203b2b1712aSChristoph Hellwig
2046bdcf26aSChristoph Hellwig void xfs_iext_first(struct xfs_ifork *, struct xfs_iext_cursor *);
2056bdcf26aSChristoph Hellwig void xfs_iext_last(struct xfs_ifork *, struct xfs_iext_cursor *);
2066bdcf26aSChristoph Hellwig void xfs_iext_next(struct xfs_ifork *, struct xfs_iext_cursor *);
2076bdcf26aSChristoph Hellwig void xfs_iext_prev(struct xfs_ifork *, struct xfs_iext_cursor *);
208b2b1712aSChristoph Hellwig
xfs_iext_next_extent(struct xfs_ifork * ifp,struct xfs_iext_cursor * cur,struct xfs_bmbt_irec * gotp)209b2b1712aSChristoph Hellwig static inline bool xfs_iext_next_extent(struct xfs_ifork *ifp,
210b2b1712aSChristoph Hellwig struct xfs_iext_cursor *cur, struct xfs_bmbt_irec *gotp)
211b2b1712aSChristoph Hellwig {
212b2b1712aSChristoph Hellwig xfs_iext_next(ifp, cur);
213b2b1712aSChristoph Hellwig return xfs_iext_get_extent(ifp, cur, gotp);
214b2b1712aSChristoph Hellwig }
215b2b1712aSChristoph Hellwig
xfs_iext_prev_extent(struct xfs_ifork * ifp,struct xfs_iext_cursor * cur,struct xfs_bmbt_irec * gotp)216b2b1712aSChristoph Hellwig static inline bool xfs_iext_prev_extent(struct xfs_ifork *ifp,
217b2b1712aSChristoph Hellwig struct xfs_iext_cursor *cur, struct xfs_bmbt_irec *gotp)
218b2b1712aSChristoph Hellwig {
219b2b1712aSChristoph Hellwig xfs_iext_prev(ifp, cur);
220b2b1712aSChristoph Hellwig return xfs_iext_get_extent(ifp, cur, gotp);
221b2b1712aSChristoph Hellwig }
222b2b1712aSChristoph Hellwig
223b2b1712aSChristoph Hellwig /*
224b2b1712aSChristoph Hellwig * Return the extent after cur in gotp without updating the cursor.
225b2b1712aSChristoph Hellwig */
xfs_iext_peek_next_extent(struct xfs_ifork * ifp,struct xfs_iext_cursor * cur,struct xfs_bmbt_irec * gotp)226b2b1712aSChristoph Hellwig static inline bool xfs_iext_peek_next_extent(struct xfs_ifork *ifp,
227b2b1712aSChristoph Hellwig struct xfs_iext_cursor *cur, struct xfs_bmbt_irec *gotp)
228b2b1712aSChristoph Hellwig {
229b2b1712aSChristoph Hellwig struct xfs_iext_cursor ncur = *cur;
230b2b1712aSChristoph Hellwig
231b2b1712aSChristoph Hellwig xfs_iext_next(ifp, &ncur);
232b2b1712aSChristoph Hellwig return xfs_iext_get_extent(ifp, &ncur, gotp);
233b2b1712aSChristoph Hellwig }
234b2b1712aSChristoph Hellwig
235b2b1712aSChristoph Hellwig /*
236b2b1712aSChristoph Hellwig * Return the extent before cur in gotp without updating the cursor.
237b2b1712aSChristoph Hellwig */
xfs_iext_peek_prev_extent(struct xfs_ifork * ifp,struct xfs_iext_cursor * cur,struct xfs_bmbt_irec * gotp)238b2b1712aSChristoph Hellwig static inline bool xfs_iext_peek_prev_extent(struct xfs_ifork *ifp,
239b2b1712aSChristoph Hellwig struct xfs_iext_cursor *cur, struct xfs_bmbt_irec *gotp)
240b2b1712aSChristoph Hellwig {
241b2b1712aSChristoph Hellwig struct xfs_iext_cursor ncur = *cur;
242b2b1712aSChristoph Hellwig
243b2b1712aSChristoph Hellwig xfs_iext_prev(ifp, &ncur);
244b2b1712aSChristoph Hellwig return xfs_iext_get_extent(ifp, &ncur, gotp);
245b2b1712aSChristoph Hellwig }
246b2b1712aSChristoph Hellwig
247b2b1712aSChristoph Hellwig #define for_each_xfs_iext(ifp, ext, got) \
248b2b1712aSChristoph Hellwig for (xfs_iext_first((ifp), (ext)); \
249b2b1712aSChristoph Hellwig xfs_iext_get_extent((ifp), (ext), (got)); \
250b2b1712aSChristoph Hellwig xfs_iext_next((ifp), (ext)))
25193533c78SChristoph Hellwig
252182696fbSDarrick J. Wong extern struct kmem_cache *xfs_ifork_cache;
25384be0ffcSDave Chinner
2543993baebSDarrick J. Wong extern void xfs_ifork_init_cow(struct xfs_inode *ip);
2553993baebSDarrick J. Wong
2567c7ba218SChristoph Hellwig int xfs_ifork_verify_local_data(struct xfs_inode *ip);
2577c7ba218SChristoph Hellwig int xfs_ifork_verify_local_attr(struct xfs_inode *ip);
258b9b7e1dcSChandan Babu R int xfs_iext_count_may_overflow(struct xfs_inode *ip, int whichfork,
259b9b7e1dcSChandan Babu R int nr_to_add);
2604f86bb4bSChandan Babu R int xfs_iext_count_upgrade(struct xfs_trans *tp, struct xfs_inode *ip,
2614f86bb4bSChandan Babu R uint nr_to_add);
2629cfb9b47SDarrick J. Wong
263b2197a36SChristoph Hellwig /* returns true if the fork has extents but they are not read in yet. */
xfs_need_iread_extents(const struct xfs_ifork * ifp)264*c95356caSDarrick J. Wong static inline bool xfs_need_iread_extents(const struct xfs_ifork *ifp)
265b2197a36SChristoph Hellwig {
266*c95356caSDarrick J. Wong /* see xfs_iformat_{data,attr}_fork() for needextents semantics */
267*c95356caSDarrick J. Wong return smp_load_acquire(&ifp->if_needextents) != 0;
268b2197a36SChristoph Hellwig }
269b2197a36SChristoph Hellwig
27084be0ffcSDave Chinner #endif /* __XFS_INODE_FORK_H__ */
271