Line data Source code
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 : struct xfs_perag;
10 :
11 : static inline void
12 : xfs_rmap_ino_bmbt_owner(
13 : struct xfs_owner_info *oi,
14 : xfs_ino_t ino,
15 : int whichfork)
16 : {
17 43790922 : oi->oi_owner = ino;
18 43790922 : oi->oi_offset = 0;
19 43790922 : oi->oi_flags = XFS_OWNER_INFO_BMBT_BLOCK;
20 43790922 : if (whichfork == XFS_ATTR_FORK)
21 114552 : oi->oi_flags |= XFS_OWNER_INFO_ATTR_FORK;
22 : }
23 :
24 : static inline void
25 : xfs_rmap_ino_owner(
26 : struct xfs_owner_info *oi,
27 : xfs_ino_t ino,
28 : int whichfork,
29 : xfs_fileoff_t offset)
30 : {
31 1334725720 : oi->oi_owner = ino;
32 1334725720 : oi->oi_offset = offset;
33 1334725720 : oi->oi_flags = 0;
34 1334725720 : if (whichfork == XFS_ATTR_FORK)
35 17534134 : oi->oi_flags |= XFS_OWNER_INFO_ATTR_FORK;
36 : }
37 :
38 : static inline bool
39 : xfs_rmap_should_skip_owner_update(
40 : const struct xfs_owner_info *oi)
41 : {
42 440824497 : return oi->oi_owner == XFS_RMAP_OWN_NULL;
43 : }
44 :
45 : /* Reverse mapping functions. */
46 :
47 : struct xfs_buf;
48 :
49 : static inline __u64
50 14720842661 : xfs_rmap_irec_offset_pack(
51 : const struct xfs_rmap_irec *irec)
52 : {
53 14720842661 : __u64 x;
54 :
55 14720842661 : x = XFS_RMAP_OFF(irec->rm_offset);
56 14720842661 : if (irec->rm_flags & XFS_RMAP_ATTR_FORK)
57 2194097027 : x |= XFS_RMAP_OFF_ATTR_FORK;
58 14720842661 : if (irec->rm_flags & XFS_RMAP_BMBT_BLOCK)
59 2200423005 : x |= XFS_RMAP_OFF_BMBT_BLOCK;
60 14720842661 : if (irec->rm_flags & XFS_RMAP_UNWRITTEN)
61 2845822182 : x |= XFS_RMAP_OFF_UNWRITTEN;
62 14720842661 : return x;
63 : }
64 :
65 : static inline xfs_failaddr_t
66 >22266*10^7 : xfs_rmap_irec_offset_unpack(
67 : __u64 offset,
68 : struct xfs_rmap_irec *irec)
69 : {
70 >22266*10^7 : if (offset & ~(XFS_RMAP_OFF_MASK | XFS_RMAP_OFF_FLAGS))
71 0 : return __this_address;
72 :
73 >22266*10^7 : irec->rm_offset = XFS_RMAP_OFF(offset);
74 >22266*10^7 : irec->rm_flags = 0;
75 >22266*10^7 : if (offset & XFS_RMAP_OFF_ATTR_FORK)
76 3333828522 : irec->rm_flags |= XFS_RMAP_ATTR_FORK;
77 >22266*10^7 : if (offset & XFS_RMAP_OFF_BMBT_BLOCK)
78 4520100185 : irec->rm_flags |= XFS_RMAP_BMBT_BLOCK;
79 >22266*10^7 : if (offset & XFS_RMAP_OFF_UNWRITTEN)
80 32394541037 : irec->rm_flags |= XFS_RMAP_UNWRITTEN;
81 : return NULL;
82 : }
83 :
84 : static inline void
85 : xfs_owner_info_unpack(
86 : const struct xfs_owner_info *oinfo,
87 : uint64_t *owner,
88 : uint64_t *offset,
89 : unsigned int *flags)
90 : {
91 980705303 : unsigned int r = 0;
92 :
93 980705303 : *owner = oinfo->oi_owner;
94 980705303 : *offset = oinfo->oi_offset;
95 980705303 : if (oinfo->oi_flags & XFS_OWNER_INFO_ATTR_FORK)
96 15486591 : r |= XFS_RMAP_ATTR_FORK;
97 980705303 : if (oinfo->oi_flags & XFS_OWNER_INFO_BMBT_BLOCK)
98 15540860 : r |= XFS_RMAP_BMBT_BLOCK;
99 980705303 : *flags = r;
100 : }
101 :
102 : static inline void
103 : xfs_owner_info_pack(
104 : struct xfs_owner_info *oinfo,
105 : uint64_t owner,
106 : uint64_t offset,
107 : unsigned int flags)
108 : {
109 130567343 : oinfo->oi_owner = owner;
110 130567343 : oinfo->oi_offset = XFS_RMAP_OFF(offset);
111 130567343 : oinfo->oi_flags = 0;
112 130567343 : if (flags & XFS_RMAP_ATTR_FORK)
113 341595 : oinfo->oi_flags |= XFS_OWNER_INFO_ATTR_FORK;
114 130567343 : if (flags & XFS_RMAP_BMBT_BLOCK)
115 7514306 : oinfo->oi_flags |= XFS_OWNER_INFO_BMBT_BLOCK;
116 : }
117 :
118 : int xfs_rmap_alloc(struct xfs_trans *tp, struct xfs_buf *agbp,
119 : struct xfs_perag *pag, xfs_agblock_t bno, xfs_extlen_t len,
120 : const struct xfs_owner_info *oinfo);
121 : int xfs_rmap_free(struct xfs_trans *tp, struct xfs_buf *agbp,
122 : struct xfs_perag *pag, xfs_agblock_t bno, xfs_extlen_t len,
123 : const struct xfs_owner_info *oinfo);
124 :
125 : int xfs_rmap_lookup_le(struct xfs_btree_cur *cur, xfs_agblock_t bno,
126 : uint64_t owner, uint64_t offset, unsigned int flags,
127 : struct xfs_rmap_irec *irec, int *stat);
128 : int xfs_rmap_lookup_eq(struct xfs_btree_cur *cur, xfs_agblock_t bno,
129 : xfs_extlen_t len, uint64_t owner, uint64_t offset,
130 : unsigned int flags, int *stat);
131 : int xfs_rmap_insert(struct xfs_btree_cur *rcur, xfs_agblock_t agbno,
132 : xfs_extlen_t len, uint64_t owner, uint64_t offset,
133 : unsigned int flags);
134 : int xfs_rmap_get_rec(struct xfs_btree_cur *cur, struct xfs_rmap_irec *irec,
135 : int *stat);
136 :
137 : typedef int (*xfs_rmap_query_range_fn)(
138 : struct xfs_btree_cur *cur,
139 : const struct xfs_rmap_irec *rec,
140 : void *priv);
141 :
142 : int xfs_rmap_query_range(struct xfs_btree_cur *cur,
143 : const struct xfs_rmap_irec *low_rec,
144 : const struct xfs_rmap_irec *high_rec,
145 : xfs_rmap_query_range_fn fn, void *priv);
146 : int xfs_rmap_query_all(struct xfs_btree_cur *cur, xfs_rmap_query_range_fn fn,
147 : void *priv);
148 :
149 : enum xfs_rmap_intent_type {
150 : XFS_RMAP_MAP,
151 : XFS_RMAP_MAP_SHARED,
152 : XFS_RMAP_UNMAP,
153 : XFS_RMAP_UNMAP_SHARED,
154 : XFS_RMAP_CONVERT,
155 : XFS_RMAP_CONVERT_SHARED,
156 : XFS_RMAP_ALLOC,
157 : XFS_RMAP_FREE,
158 : };
159 :
160 : #define XFS_RMAP_INTENT_STRINGS \
161 : { XFS_RMAP_MAP, "map" }, \
162 : { XFS_RMAP_MAP_SHARED, "map_shared" }, \
163 : { XFS_RMAP_UNMAP, "unmap" }, \
164 : { XFS_RMAP_UNMAP_SHARED, "unmap_shared" }, \
165 : { XFS_RMAP_CONVERT, "cvt" }, \
166 : { XFS_RMAP_CONVERT_SHARED, "cvt_shared" }, \
167 : { XFS_RMAP_ALLOC, "alloc" }, \
168 : { XFS_RMAP_FREE, "free" }
169 :
170 : struct xfs_rmap_intent {
171 : struct list_head ri_list;
172 : enum xfs_rmap_intent_type ri_type;
173 : int ri_whichfork;
174 : uint64_t ri_owner;
175 : struct xfs_bmbt_irec ri_bmap;
176 : union {
177 : struct xfs_perag *ri_pag;
178 : struct xfs_rtgroup *ri_rtg;
179 : };
180 : bool ri_realtime;
181 : };
182 :
183 : void xfs_rmap_update_get_group(struct xfs_mount *mp,
184 : struct xfs_rmap_intent *ri);
185 :
186 : /* functions for updating the rmapbt based on bmbt map/unmap operations */
187 : void xfs_rmap_map_extent(struct xfs_trans *tp, struct xfs_inode *ip,
188 : int whichfork, struct xfs_bmbt_irec *imap);
189 : void xfs_rmap_unmap_extent(struct xfs_trans *tp, struct xfs_inode *ip,
190 : int whichfork, struct xfs_bmbt_irec *imap);
191 : void xfs_rmap_convert_extent(struct xfs_mount *mp, struct xfs_trans *tp,
192 : struct xfs_inode *ip, int whichfork,
193 : struct xfs_bmbt_irec *imap);
194 : void xfs_rmap_alloc_extent(struct xfs_trans *tp, bool isrt, xfs_fsblock_t fsbno,
195 : xfs_extlen_t len, uint64_t owner);
196 : void xfs_rmap_free_extent(struct xfs_trans *tp, bool isrt, xfs_fsblock_t fsbno,
197 : xfs_extlen_t len, uint64_t owner);
198 :
199 : void xfs_rmap_finish_one_cleanup(struct xfs_trans *tp,
200 : struct xfs_btree_cur *rcur, int error);
201 : int xfs_rmap_finish_one(struct xfs_trans *tp, struct xfs_rmap_intent *ri,
202 : struct xfs_btree_cur **pcur);
203 : int __xfs_rmap_finish_intent(struct xfs_btree_cur *rcur,
204 : enum xfs_rmap_intent_type op, xfs_agblock_t bno,
205 : xfs_extlen_t len, const struct xfs_owner_info *oinfo,
206 : bool unwritten);
207 :
208 : int xfs_rmap_lookup_le_range(struct xfs_btree_cur *cur, xfs_agblock_t bno,
209 : uint64_t owner, uint64_t offset, unsigned int flags,
210 : struct xfs_rmap_irec *irec, int *stat);
211 : int xfs_rmap_compare(const struct xfs_rmap_irec *a,
212 : const struct xfs_rmap_irec *b);
213 : union xfs_btree_rec;
214 : xfs_failaddr_t xfs_rmap_btrec_to_irec(const union xfs_btree_rec *rec,
215 : struct xfs_rmap_irec *irec);
216 : xfs_failaddr_t xfs_rmap_check_perag_irec(struct xfs_perag *pag,
217 : const struct xfs_rmap_irec *irec);
218 : xfs_failaddr_t xfs_rmap_check_rtgroup_irec(struct xfs_rtgroup *rtg,
219 : const struct xfs_rmap_irec *irec);
220 : xfs_failaddr_t xfs_rmap_check_irec(struct xfs_btree_cur *cur,
221 : const struct xfs_rmap_irec *irec);
222 :
223 : int xfs_rmap_has_records(struct xfs_btree_cur *cur, xfs_agblock_t bno,
224 : xfs_extlen_t len, enum xbtree_recpacking *outcome);
225 :
226 : struct xfs_rmap_matches {
227 : /* Number of owner matches. */
228 : unsigned long long matches;
229 :
230 : /* Number of non-owner matches. */
231 : unsigned long long non_owner_matches;
232 :
233 : /* Number of non-owner matches that conflict with the owner matches. */
234 : unsigned long long bad_non_owner_matches;
235 : };
236 :
237 : int xfs_rmap_count_owners(struct xfs_btree_cur *cur, xfs_agblock_t bno,
238 : xfs_extlen_t len, const struct xfs_owner_info *oinfo,
239 : struct xfs_rmap_matches *rmatch);
240 : int xfs_rmap_has_other_keys(struct xfs_btree_cur *cur, xfs_agblock_t bno,
241 : xfs_extlen_t len, const struct xfs_owner_info *oinfo,
242 : bool *has_other);
243 : int xfs_rmap_map_raw(struct xfs_btree_cur *cur, struct xfs_rmap_irec *rmap);
244 :
245 : extern const struct xfs_owner_info XFS_RMAP_OINFO_SKIP_UPDATE;
246 : extern const struct xfs_owner_info XFS_RMAP_OINFO_ANY_OWNER;
247 : extern const struct xfs_owner_info XFS_RMAP_OINFO_FS;
248 : extern const struct xfs_owner_info XFS_RMAP_OINFO_LOG;
249 : extern const struct xfs_owner_info XFS_RMAP_OINFO_AG;
250 : extern const struct xfs_owner_info XFS_RMAP_OINFO_INOBT;
251 : extern const struct xfs_owner_info XFS_RMAP_OINFO_INODES;
252 : extern const struct xfs_owner_info XFS_RMAP_OINFO_REFC;
253 : extern const struct xfs_owner_info XFS_RMAP_OINFO_COW;
254 :
255 : extern struct kmem_cache *xfs_rmap_intent_cache;
256 :
257 : int __init xfs_rmap_intent_init_cache(void);
258 : void xfs_rmap_intent_destroy_cache(void);
259 :
260 : /*
261 : * Parameters for tracking reverse mapping changes. The hook function arg
262 : * parameter is enum xfs_rmap_intent_type, and the rest is below.
263 : */
264 : struct xfs_rmap_update_params {
265 : xfs_agblock_t startblock;
266 : xfs_extlen_t blockcount;
267 : struct xfs_owner_info oinfo;
268 : bool unwritten;
269 : };
270 :
271 : #ifdef CONFIG_XFS_LIVE_HOOKS
272 :
273 : struct xfs_rmap_hook {
274 : struct xfs_hook update_hook;
275 : };
276 :
277 : void xfs_rmap_hook_disable(void);
278 : void xfs_rmap_hook_enable(void);
279 :
280 : int xfs_rmap_hook_add(struct xfs_perag *pag, struct xfs_rmap_hook *hook);
281 : void xfs_rmap_hook_del(struct xfs_perag *pag, struct xfs_rmap_hook *hook);
282 :
283 : # ifdef CONFIG_XFS_RT
284 : int xfs_rtrmap_hook_add(struct xfs_rtgroup *rtg, struct xfs_rmap_hook *hook);
285 : void xfs_rtrmap_hook_del(struct xfs_rtgroup *rtg, struct xfs_rmap_hook *hook);
286 : # endif /* CONFIG_XFS_RT */
287 :
288 : #endif
289 :
290 : #endif /* __XFS_RMAP_H__ */
|