xref: /linux/fs/xfs/libxfs/xfs_inode_fork.h (revision 26fbb4c8c7c3ee9a4c3b4de555a8587b5a19154e)
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  * Fork handling.
39  */
40 
41 #define XFS_IFORK_Q(ip)			((ip)->i_d.di_forkoff != 0)
42 #define XFS_IFORK_BOFF(ip)		((int)((ip)->i_d.di_forkoff << 3))
43 
44 #define XFS_IFORK_PTR(ip,w)		\
45 	((w) == XFS_DATA_FORK ? \
46 		&(ip)->i_df : \
47 		((w) == XFS_ATTR_FORK ? \
48 			(ip)->i_afp : \
49 			(ip)->i_cowfp))
50 #define XFS_IFORK_DSIZE(ip) \
51 	(XFS_IFORK_Q(ip) ? XFS_IFORK_BOFF(ip) : XFS_LITINO((ip)->i_mount))
52 #define XFS_IFORK_ASIZE(ip) \
53 	(XFS_IFORK_Q(ip) ? XFS_LITINO((ip)->i_mount) - XFS_IFORK_BOFF(ip) : 0)
54 #define XFS_IFORK_SIZE(ip,w) \
55 	((w) == XFS_DATA_FORK ? \
56 		XFS_IFORK_DSIZE(ip) : \
57 		((w) == XFS_ATTR_FORK ? \
58 			XFS_IFORK_ASIZE(ip) : \
59 			0))
60 #define XFS_IFORK_MAXEXT(ip, w) \
61 	(XFS_IFORK_SIZE(ip, w) / sizeof(xfs_bmbt_rec_t))
62 
63 static inline bool xfs_ifork_has_extents(struct xfs_ifork *ifp)
64 {
65 	return ifp->if_format == XFS_DINODE_FMT_EXTENTS ||
66 		ifp->if_format == XFS_DINODE_FMT_BTREE;
67 }
68 
69 static inline xfs_extnum_t xfs_ifork_nextents(struct xfs_ifork *ifp)
70 {
71 	if (!ifp)
72 		return 0;
73 	return ifp->if_nextents;
74 }
75 
76 static inline int8_t xfs_ifork_format(struct xfs_ifork *ifp)
77 {
78 	if (!ifp)
79 		return XFS_DINODE_FMT_EXTENTS;
80 	return ifp->if_format;
81 }
82 
83 struct xfs_ifork *xfs_iext_state_to_fork(struct xfs_inode *ip, int state);
84 
85 int		xfs_iformat_data_fork(struct xfs_inode *, struct xfs_dinode *);
86 int		xfs_iformat_attr_fork(struct xfs_inode *, struct xfs_dinode *);
87 void		xfs_iflush_fork(struct xfs_inode *, struct xfs_dinode *,
88 				struct xfs_inode_log_item *, int);
89 void		xfs_idestroy_fork(struct xfs_ifork *ifp);
90 void		xfs_idata_realloc(struct xfs_inode *ip, int64_t byte_diff,
91 				int whichfork);
92 void		xfs_iroot_realloc(struct xfs_inode *, int, int);
93 int		xfs_iread_extents(struct xfs_trans *, struct xfs_inode *, int);
94 int		xfs_iextents_copy(struct xfs_inode *, struct xfs_bmbt_rec *,
95 				  int);
96 void		xfs_init_local_fork(struct xfs_inode *ip, int whichfork,
97 				const void *data, int64_t size);
98 
99 xfs_extnum_t	xfs_iext_count(struct xfs_ifork *ifp);
100 void		xfs_iext_insert(struct xfs_inode *, struct xfs_iext_cursor *cur,
101 			struct xfs_bmbt_irec *, int);
102 void		xfs_iext_remove(struct xfs_inode *, struct xfs_iext_cursor *,
103 			int);
104 void		xfs_iext_destroy(struct xfs_ifork *);
105 
106 bool		xfs_iext_lookup_extent(struct xfs_inode *ip,
107 			struct xfs_ifork *ifp, xfs_fileoff_t bno,
108 			struct xfs_iext_cursor *cur,
109 			struct xfs_bmbt_irec *gotp);
110 bool		xfs_iext_lookup_extent_before(struct xfs_inode *ip,
111 			struct xfs_ifork *ifp, xfs_fileoff_t *end,
112 			struct xfs_iext_cursor *cur,
113 			struct xfs_bmbt_irec *gotp);
114 bool		xfs_iext_get_extent(struct xfs_ifork *ifp,
115 			struct xfs_iext_cursor *cur,
116 			struct xfs_bmbt_irec *gotp);
117 void		xfs_iext_update_extent(struct xfs_inode *ip, int state,
118 			struct xfs_iext_cursor *cur,
119 			struct xfs_bmbt_irec *gotp);
120 
121 void		xfs_iext_first(struct xfs_ifork *, struct xfs_iext_cursor *);
122 void		xfs_iext_last(struct xfs_ifork *, struct xfs_iext_cursor *);
123 void		xfs_iext_next(struct xfs_ifork *, struct xfs_iext_cursor *);
124 void		xfs_iext_prev(struct xfs_ifork *, struct xfs_iext_cursor *);
125 
126 static inline bool xfs_iext_next_extent(struct xfs_ifork *ifp,
127 		struct xfs_iext_cursor *cur, struct xfs_bmbt_irec *gotp)
128 {
129 	xfs_iext_next(ifp, cur);
130 	return xfs_iext_get_extent(ifp, cur, gotp);
131 }
132 
133 static inline bool xfs_iext_prev_extent(struct xfs_ifork *ifp,
134 		struct xfs_iext_cursor *cur, struct xfs_bmbt_irec *gotp)
135 {
136 	xfs_iext_prev(ifp, cur);
137 	return xfs_iext_get_extent(ifp, cur, gotp);
138 }
139 
140 /*
141  * Return the extent after cur in gotp without updating the cursor.
142  */
143 static inline bool xfs_iext_peek_next_extent(struct xfs_ifork *ifp,
144 		struct xfs_iext_cursor *cur, struct xfs_bmbt_irec *gotp)
145 {
146 	struct xfs_iext_cursor ncur = *cur;
147 
148 	xfs_iext_next(ifp, &ncur);
149 	return xfs_iext_get_extent(ifp, &ncur, gotp);
150 }
151 
152 /*
153  * Return the extent before cur in gotp without updating the cursor.
154  */
155 static inline bool xfs_iext_peek_prev_extent(struct xfs_ifork *ifp,
156 		struct xfs_iext_cursor *cur, struct xfs_bmbt_irec *gotp)
157 {
158 	struct xfs_iext_cursor ncur = *cur;
159 
160 	xfs_iext_prev(ifp, &ncur);
161 	return xfs_iext_get_extent(ifp, &ncur, gotp);
162 }
163 
164 #define for_each_xfs_iext(ifp, ext, got)		\
165 	for (xfs_iext_first((ifp), (ext));		\
166 	     xfs_iext_get_extent((ifp), (ext), (got));	\
167 	     xfs_iext_next((ifp), (ext)))
168 
169 extern struct kmem_zone	*xfs_ifork_zone;
170 
171 extern void xfs_ifork_init_cow(struct xfs_inode *ip);
172 
173 int xfs_ifork_verify_local_data(struct xfs_inode *ip);
174 int xfs_ifork_verify_local_attr(struct xfs_inode *ip);
175 
176 #endif	/* __XFS_INODE_FORK_H__ */
177