1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2016 Oracle.  All Rights Reserved.
4  * Author: Darrick J. Wong <darrick.wong@oracle.com>
5  */
6 #ifndef __XFS_RMAP_H__
7 #define __XFS_RMAP_H__
8 
9 static inline void
xfs_rmap_ino_bmbt_owner(struct xfs_owner_info * oi,xfs_ino_t ino,int whichfork)10 xfs_rmap_ino_bmbt_owner(
11 	struct xfs_owner_info	*oi,
12 	xfs_ino_t		ino,
13 	int			whichfork)
14 {
15 	oi->oi_owner = ino;
16 	oi->oi_offset = 0;
17 	oi->oi_flags = XFS_OWNER_INFO_BMBT_BLOCK;
18 	if (whichfork == XFS_ATTR_FORK)
19 		oi->oi_flags |= XFS_OWNER_INFO_ATTR_FORK;
20 }
21 
22 static inline void
xfs_rmap_ino_owner(struct xfs_owner_info * oi,xfs_ino_t ino,int whichfork,xfs_fileoff_t offset)23 xfs_rmap_ino_owner(
24 	struct xfs_owner_info	*oi,
25 	xfs_ino_t		ino,
26 	int			whichfork,
27 	xfs_fileoff_t		offset)
28 {
29 	oi->oi_owner = ino;
30 	oi->oi_offset = offset;
31 	oi->oi_flags = 0;
32 	if (whichfork == XFS_ATTR_FORK)
33 		oi->oi_flags |= XFS_OWNER_INFO_ATTR_FORK;
34 }
35 
36 static inline bool
xfs_rmap_should_skip_owner_update(const struct xfs_owner_info * oi)37 xfs_rmap_should_skip_owner_update(
38 	const struct xfs_owner_info	*oi)
39 {
40 	return oi->oi_owner == XFS_RMAP_OWN_NULL;
41 }
42 
43 /* Reverse mapping functions. */
44 
45 struct xfs_buf;
46 
47 static inline __u64
xfs_rmap_irec_offset_pack(const struct xfs_rmap_irec * irec)48 xfs_rmap_irec_offset_pack(
49 	const struct xfs_rmap_irec	*irec)
50 {
51 	__u64			x;
52 
53 	x = XFS_RMAP_OFF(irec->rm_offset);
54 	if (irec->rm_flags & XFS_RMAP_ATTR_FORK)
55 		x |= XFS_RMAP_OFF_ATTR_FORK;
56 	if (irec->rm_flags & XFS_RMAP_BMBT_BLOCK)
57 		x |= XFS_RMAP_OFF_BMBT_BLOCK;
58 	if (irec->rm_flags & XFS_RMAP_UNWRITTEN)
59 		x |= XFS_RMAP_OFF_UNWRITTEN;
60 	return x;
61 }
62 
63 static inline int
xfs_rmap_irec_offset_unpack(__u64 offset,struct xfs_rmap_irec * irec)64 xfs_rmap_irec_offset_unpack(
65 	__u64			offset,
66 	struct xfs_rmap_irec	*irec)
67 {
68 	if (offset & ~(XFS_RMAP_OFF_MASK | XFS_RMAP_OFF_FLAGS))
69 		return -EFSCORRUPTED;
70 	irec->rm_offset = XFS_RMAP_OFF(offset);
71 	irec->rm_flags = 0;
72 	if (offset & XFS_RMAP_OFF_ATTR_FORK)
73 		irec->rm_flags |= XFS_RMAP_ATTR_FORK;
74 	if (offset & XFS_RMAP_OFF_BMBT_BLOCK)
75 		irec->rm_flags |= XFS_RMAP_BMBT_BLOCK;
76 	if (offset & XFS_RMAP_OFF_UNWRITTEN)
77 		irec->rm_flags |= XFS_RMAP_UNWRITTEN;
78 	return 0;
79 }
80 
81 static inline void
xfs_owner_info_unpack(const struct xfs_owner_info * oinfo,uint64_t * owner,uint64_t * offset,unsigned int * flags)82 xfs_owner_info_unpack(
83 	const struct xfs_owner_info	*oinfo,
84 	uint64_t			*owner,
85 	uint64_t			*offset,
86 	unsigned int			*flags)
87 {
88 	unsigned int			r = 0;
89 
90 	*owner = oinfo->oi_owner;
91 	*offset = oinfo->oi_offset;
92 	if (oinfo->oi_flags & XFS_OWNER_INFO_ATTR_FORK)
93 		r |= XFS_RMAP_ATTR_FORK;
94 	if (oinfo->oi_flags & XFS_OWNER_INFO_BMBT_BLOCK)
95 		r |= XFS_RMAP_BMBT_BLOCK;
96 	*flags = r;
97 }
98 
99 static inline void
xfs_owner_info_pack(struct xfs_owner_info * oinfo,uint64_t owner,uint64_t offset,unsigned int flags)100 xfs_owner_info_pack(
101 	struct xfs_owner_info	*oinfo,
102 	uint64_t		owner,
103 	uint64_t		offset,
104 	unsigned int		flags)
105 {
106 	oinfo->oi_owner = owner;
107 	oinfo->oi_offset = XFS_RMAP_OFF(offset);
108 	oinfo->oi_flags = 0;
109 	if (flags & XFS_RMAP_ATTR_FORK)
110 		oinfo->oi_flags |= XFS_OWNER_INFO_ATTR_FORK;
111 	if (flags & XFS_RMAP_BMBT_BLOCK)
112 		oinfo->oi_flags |= XFS_OWNER_INFO_BMBT_BLOCK;
113 }
114 
115 int xfs_rmap_alloc(struct xfs_trans *tp, struct xfs_buf *agbp,
116 		   xfs_agnumber_t agno, xfs_agblock_t bno, xfs_extlen_t len,
117 		   const struct xfs_owner_info *oinfo);
118 int xfs_rmap_free(struct xfs_trans *tp, struct xfs_buf *agbp,
119 		  xfs_agnumber_t agno, xfs_agblock_t bno, xfs_extlen_t len,
120 		  const struct xfs_owner_info *oinfo);
121 
122 int xfs_rmap_lookup_le(struct xfs_btree_cur *cur, xfs_agblock_t bno,
123 		xfs_extlen_t len, uint64_t owner, uint64_t offset,
124 		unsigned int flags, int *stat);
125 int xfs_rmap_lookup_eq(struct xfs_btree_cur *cur, xfs_agblock_t bno,
126 		xfs_extlen_t len, uint64_t owner, uint64_t offset,
127 		unsigned int flags, int *stat);
128 int xfs_rmap_insert(struct xfs_btree_cur *rcur, xfs_agblock_t agbno,
129 		xfs_extlen_t len, uint64_t owner, uint64_t offset,
130 		unsigned int flags);
131 int xfs_rmap_get_rec(struct xfs_btree_cur *cur, struct xfs_rmap_irec *irec,
132 		int *stat);
133 
134 typedef int (*xfs_rmap_query_range_fn)(
135 	struct xfs_btree_cur	*cur,
136 	struct xfs_rmap_irec	*rec,
137 	void			*priv);
138 
139 int xfs_rmap_query_range(struct xfs_btree_cur *cur,
140 		struct xfs_rmap_irec *low_rec, struct xfs_rmap_irec *high_rec,
141 		xfs_rmap_query_range_fn fn, void *priv);
142 int xfs_rmap_query_all(struct xfs_btree_cur *cur, xfs_rmap_query_range_fn fn,
143 		void *priv);
144 
145 enum xfs_rmap_intent_type {
146 	XFS_RMAP_MAP,
147 	XFS_RMAP_MAP_SHARED,
148 	XFS_RMAP_UNMAP,
149 	XFS_RMAP_UNMAP_SHARED,
150 	XFS_RMAP_CONVERT,
151 	XFS_RMAP_CONVERT_SHARED,
152 	XFS_RMAP_ALLOC,
153 	XFS_RMAP_FREE,
154 };
155 
156 struct xfs_rmap_intent {
157 	struct list_head			ri_list;
158 	enum xfs_rmap_intent_type		ri_type;
159 	uint64_t				ri_owner;
160 	int					ri_whichfork;
161 	struct xfs_bmbt_irec			ri_bmap;
162 };
163 
164 /* functions for updating the rmapbt based on bmbt map/unmap operations */
165 void xfs_rmap_map_extent(struct xfs_trans *tp, struct xfs_inode *ip,
166 		int whichfork, struct xfs_bmbt_irec *imap);
167 void xfs_rmap_unmap_extent(struct xfs_trans *tp, struct xfs_inode *ip,
168 		int whichfork, struct xfs_bmbt_irec *imap);
169 void xfs_rmap_convert_extent(struct xfs_mount *mp, struct xfs_trans *tp,
170 		struct xfs_inode *ip, int whichfork,
171 		struct xfs_bmbt_irec *imap);
172 void xfs_rmap_alloc_extent(struct xfs_trans *tp, xfs_agnumber_t agno,
173 		xfs_agblock_t bno, xfs_extlen_t len, uint64_t owner);
174 void xfs_rmap_free_extent(struct xfs_trans *tp, xfs_agnumber_t agno,
175 		xfs_agblock_t bno, xfs_extlen_t len, uint64_t owner);
176 
177 void xfs_rmap_finish_one_cleanup(struct xfs_trans *tp,
178 		struct xfs_btree_cur *rcur, int error);
179 int xfs_rmap_finish_one(struct xfs_trans *tp, enum xfs_rmap_intent_type type,
180 		uint64_t owner, int whichfork, xfs_fileoff_t startoff,
181 		xfs_fsblock_t startblock, xfs_filblks_t blockcount,
182 		xfs_exntst_t state, struct xfs_btree_cur **pcur);
183 
184 int xfs_rmap_find_left_neighbor(struct xfs_btree_cur *cur, xfs_agblock_t bno,
185 		uint64_t owner, uint64_t offset, unsigned int flags,
186 		struct xfs_rmap_irec *irec, int	*stat);
187 int xfs_rmap_lookup_le_range(struct xfs_btree_cur *cur, xfs_agblock_t bno,
188 		uint64_t owner, uint64_t offset, unsigned int flags,
189 		struct xfs_rmap_irec *irec, int	*stat);
190 int xfs_rmap_compare(const struct xfs_rmap_irec *a,
191 		const struct xfs_rmap_irec *b);
192 union xfs_btree_rec;
193 int xfs_rmap_btrec_to_irec(union xfs_btree_rec *rec,
194 		struct xfs_rmap_irec *irec);
195 int xfs_rmap_has_record(struct xfs_btree_cur *cur, xfs_agblock_t bno,
196 		xfs_extlen_t len, bool *exists);
197 int xfs_rmap_record_exists(struct xfs_btree_cur *cur, xfs_agblock_t bno,
198 		xfs_extlen_t len, const struct xfs_owner_info *oinfo,
199 		bool *has_rmap);
200 int xfs_rmap_has_other_keys(struct xfs_btree_cur *cur, xfs_agblock_t bno,
201 		xfs_extlen_t len, const struct xfs_owner_info *oinfo,
202 		bool *has_rmap);
203 int xfs_rmap_map_raw(struct xfs_btree_cur *cur, struct xfs_rmap_irec *rmap);
204 
205 extern const struct xfs_owner_info XFS_RMAP_OINFO_SKIP_UPDATE;
206 extern const struct xfs_owner_info XFS_RMAP_OINFO_ANY_OWNER;
207 extern const struct xfs_owner_info XFS_RMAP_OINFO_FS;
208 extern const struct xfs_owner_info XFS_RMAP_OINFO_LOG;
209 extern const struct xfs_owner_info XFS_RMAP_OINFO_AG;
210 extern const struct xfs_owner_info XFS_RMAP_OINFO_INOBT;
211 extern const struct xfs_owner_info XFS_RMAP_OINFO_INODES;
212 extern const struct xfs_owner_info XFS_RMAP_OINFO_REFC;
213 extern const struct xfs_owner_info XFS_RMAP_OINFO_COW;
214 
215 #endif	/* __XFS_RMAP_H__ */
216