Commit | Line | Data |
---|---|---|
0b61f8a4 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
340785cc DW |
2 | /* |
3 | * Copyright (C) 2016 Oracle. All Rights Reserved. | |
340785cc | 4 | * Author: Darrick J. Wong <darrick.wong@oracle.com> |
340785cc DW |
5 | */ |
6 | #ifndef __XFS_RMAP_H__ | |
7 | #define __XFS_RMAP_H__ | |
8 | ||
fa9c3c19 | 9 | struct xfs_perag; |
adafb31c | 10 | struct xfs_rtgroup; |
fa9c3c19 | 11 | |
340785cc DW |
12 | static inline void |
13 | xfs_rmap_ino_bmbt_owner( | |
14 | struct xfs_owner_info *oi, | |
15 | xfs_ino_t ino, | |
16 | int whichfork) | |
17 | { | |
18 | oi->oi_owner = ino; | |
19 | oi->oi_offset = 0; | |
20 | oi->oi_flags = XFS_OWNER_INFO_BMBT_BLOCK; | |
21 | if (whichfork == XFS_ATTR_FORK) | |
22 | oi->oi_flags |= XFS_OWNER_INFO_ATTR_FORK; | |
23 | } | |
24 | ||
25 | static inline void | |
26 | xfs_rmap_ino_owner( | |
27 | struct xfs_owner_info *oi, | |
28 | xfs_ino_t ino, | |
29 | int whichfork, | |
30 | xfs_fileoff_t offset) | |
31 | { | |
32 | oi->oi_owner = ino; | |
33 | oi->oi_offset = offset; | |
34 | oi->oi_flags = 0; | |
35 | if (whichfork == XFS_ATTR_FORK) | |
36 | oi->oi_flags |= XFS_OWNER_INFO_ATTR_FORK; | |
37 | } | |
38 | ||
33df3a9c DW |
39 | static inline bool |
40 | xfs_rmap_should_skip_owner_update( | |
66e3237e | 41 | const struct xfs_owner_info *oi) |
33df3a9c DW |
42 | { |
43 | return oi->oi_owner == XFS_RMAP_OWN_NULL; | |
44 | } | |
45 | ||
673930c3 DW |
46 | /* Reverse mapping functions. */ |
47 | ||
48 | struct xfs_buf; | |
49 | ||
035e00ac DW |
50 | static inline __u64 |
51 | xfs_rmap_irec_offset_pack( | |
52 | const struct xfs_rmap_irec *irec) | |
53 | { | |
54 | __u64 x; | |
55 | ||
56 | x = XFS_RMAP_OFF(irec->rm_offset); | |
57 | if (irec->rm_flags & XFS_RMAP_ATTR_FORK) | |
58 | x |= XFS_RMAP_OFF_ATTR_FORK; | |
59 | if (irec->rm_flags & XFS_RMAP_BMBT_BLOCK) | |
60 | x |= XFS_RMAP_OFF_BMBT_BLOCK; | |
61 | if (irec->rm_flags & XFS_RMAP_UNWRITTEN) | |
62 | x |= XFS_RMAP_OFF_UNWRITTEN; | |
63 | return x; | |
64 | } | |
65 | ||
39ab26d5 | 66 | static inline xfs_failaddr_t |
035e00ac DW |
67 | xfs_rmap_irec_offset_unpack( |
68 | __u64 offset, | |
69 | struct xfs_rmap_irec *irec) | |
70 | { | |
71 | if (offset & ~(XFS_RMAP_OFF_MASK | XFS_RMAP_OFF_FLAGS)) | |
39ab26d5 DW |
72 | return __this_address; |
73 | ||
035e00ac | 74 | irec->rm_offset = XFS_RMAP_OFF(offset); |
ffb5696f | 75 | irec->rm_flags = 0; |
035e00ac DW |
76 | if (offset & XFS_RMAP_OFF_ATTR_FORK) |
77 | irec->rm_flags |= XFS_RMAP_ATTR_FORK; | |
78 | if (offset & XFS_RMAP_OFF_BMBT_BLOCK) | |
79 | irec->rm_flags |= XFS_RMAP_BMBT_BLOCK; | |
80 | if (offset & XFS_RMAP_OFF_UNWRITTEN) | |
81 | irec->rm_flags |= XFS_RMAP_UNWRITTEN; | |
39ab26d5 | 82 | return NULL; |
035e00ac DW |
83 | } |
84 | ||
85 | static inline void | |
86 | xfs_owner_info_unpack( | |
66e3237e DW |
87 | const struct xfs_owner_info *oinfo, |
88 | uint64_t *owner, | |
89 | uint64_t *offset, | |
90 | unsigned int *flags) | |
035e00ac | 91 | { |
66e3237e | 92 | unsigned int r = 0; |
035e00ac DW |
93 | |
94 | *owner = oinfo->oi_owner; | |
95 | *offset = oinfo->oi_offset; | |
96 | if (oinfo->oi_flags & XFS_OWNER_INFO_ATTR_FORK) | |
97 | r |= XFS_RMAP_ATTR_FORK; | |
98 | if (oinfo->oi_flags & XFS_OWNER_INFO_BMBT_BLOCK) | |
99 | r |= XFS_RMAP_BMBT_BLOCK; | |
100 | *flags = r; | |
101 | } | |
102 | ||
103 | static inline void | |
104 | xfs_owner_info_pack( | |
105 | struct xfs_owner_info *oinfo, | |
106 | uint64_t owner, | |
107 | uint64_t offset, | |
108 | unsigned int flags) | |
109 | { | |
110 | oinfo->oi_owner = owner; | |
111 | oinfo->oi_offset = XFS_RMAP_OFF(offset); | |
112 | oinfo->oi_flags = 0; | |
113 | if (flags & XFS_RMAP_ATTR_FORK) | |
114 | oinfo->oi_flags |= XFS_OWNER_INFO_ATTR_FORK; | |
115 | if (flags & XFS_RMAP_BMBT_BLOCK) | |
116 | oinfo->oi_flags |= XFS_OWNER_INFO_BMBT_BLOCK; | |
117 | } | |
118 | ||
673930c3 | 119 | int xfs_rmap_alloc(struct xfs_trans *tp, struct xfs_buf *agbp, |
fa9c3c19 | 120 | struct xfs_perag *pag, xfs_agblock_t bno, xfs_extlen_t len, |
66e3237e | 121 | const struct xfs_owner_info *oinfo); |
673930c3 | 122 | int xfs_rmap_free(struct xfs_trans *tp, struct xfs_buf *agbp, |
fa9c3c19 | 123 | struct xfs_perag *pag, xfs_agblock_t bno, xfs_extlen_t len, |
66e3237e | 124 | const struct xfs_owner_info *oinfo); |
673930c3 | 125 | |
4b8ed677 | 126 | int xfs_rmap_lookup_le(struct xfs_btree_cur *cur, xfs_agblock_t bno, |
5b7ca8b3 DW |
127 | uint64_t owner, uint64_t offset, unsigned int flags, |
128 | struct xfs_rmap_irec *irec, int *stat); | |
4b8ed677 DW |
129 | int xfs_rmap_lookup_eq(struct xfs_btree_cur *cur, xfs_agblock_t bno, |
130 | xfs_extlen_t len, uint64_t owner, uint64_t offset, | |
131 | unsigned int flags, int *stat); | |
abf09233 DW |
132 | int xfs_rmap_insert(struct xfs_btree_cur *rcur, xfs_agblock_t agbno, |
133 | xfs_extlen_t len, uint64_t owner, uint64_t offset, | |
134 | unsigned int flags); | |
4b8ed677 DW |
135 | int xfs_rmap_get_rec(struct xfs_btree_cur *cur, struct xfs_rmap_irec *irec, |
136 | int *stat); | |
137 | ||
c543838a | 138 | typedef int (*xfs_rmap_query_range_fn)( |
159eb69d DW |
139 | struct xfs_btree_cur *cur, |
140 | const struct xfs_rmap_irec *rec, | |
141 | void *priv); | |
c543838a DW |
142 | |
143 | int xfs_rmap_query_range(struct xfs_btree_cur *cur, | |
04dcb474 DW |
144 | const struct xfs_rmap_irec *low_rec, |
145 | const struct xfs_rmap_irec *high_rec, | |
c543838a | 146 | xfs_rmap_query_range_fn fn, void *priv); |
e9a2599a DW |
147 | int xfs_rmap_query_all(struct xfs_btree_cur *cur, xfs_rmap_query_range_fn fn, |
148 | void *priv); | |
c543838a | 149 | |
5880f2d7 DW |
150 | enum xfs_rmap_intent_type { |
151 | XFS_RMAP_MAP, | |
152 | XFS_RMAP_MAP_SHARED, | |
153 | XFS_RMAP_UNMAP, | |
154 | XFS_RMAP_UNMAP_SHARED, | |
155 | XFS_RMAP_CONVERT, | |
156 | XFS_RMAP_CONVERT_SHARED, | |
157 | XFS_RMAP_ALLOC, | |
158 | XFS_RMAP_FREE, | |
159 | }; | |
160 | ||
fbe8c7e1 DW |
161 | #define XFS_RMAP_INTENT_STRINGS \ |
162 | { XFS_RMAP_MAP, "map" }, \ | |
163 | { XFS_RMAP_MAP_SHARED, "map_shared" }, \ | |
164 | { XFS_RMAP_UNMAP, "unmap" }, \ | |
165 | { XFS_RMAP_UNMAP_SHARED, "unmap_shared" }, \ | |
166 | { XFS_RMAP_CONVERT, "cvt" }, \ | |
167 | { XFS_RMAP_CONVERT_SHARED, "cvt_shared" }, \ | |
168 | { XFS_RMAP_ALLOC, "alloc" }, \ | |
169 | { XFS_RMAP_FREE, "free" } | |
170 | ||
5880f2d7 DW |
171 | struct xfs_rmap_intent { |
172 | struct list_head ri_list; | |
173 | enum xfs_rmap_intent_type ri_type; | |
5880f2d7 | 174 | int ri_whichfork; |
9e253954 | 175 | uint64_t ri_owner; |
5880f2d7 | 176 | struct xfs_bmbt_irec ri_bmap; |
e5e5cae0 | 177 | struct xfs_group *ri_group; |
9e823fc2 | 178 | bool ri_realtime; |
5880f2d7 DW |
179 | }; |
180 | ||
9c194644 | 181 | /* functions for updating the rmapbt based on bmbt map/unmap operations */ |
bc46ac64 | 182 | void xfs_rmap_map_extent(struct xfs_trans *tp, struct xfs_inode *ip, |
0f37d178 | 183 | int whichfork, struct xfs_bmbt_irec *imap); |
bc46ac64 | 184 | void xfs_rmap_unmap_extent(struct xfs_trans *tp, struct xfs_inode *ip, |
0f37d178 | 185 | int whichfork, struct xfs_bmbt_irec *imap); |
bc46ac64 | 186 | void xfs_rmap_convert_extent(struct xfs_mount *mp, struct xfs_trans *tp, |
9c194644 DW |
187 | struct xfs_inode *ip, int whichfork, |
188 | struct xfs_bmbt_irec *imap); | |
9e823fc2 | 189 | void xfs_rmap_alloc_extent(struct xfs_trans *tp, bool isrt, xfs_fsblock_t fsbno, |
953f76bf | 190 | xfs_extlen_t len, uint64_t owner); |
9e823fc2 | 191 | void xfs_rmap_free_extent(struct xfs_trans *tp, bool isrt, xfs_fsblock_t fsbno, |
953f76bf | 192 | xfs_extlen_t len, uint64_t owner); |
9c194644 | 193 | |
1534328b DW |
194 | int xfs_rmap_finish_one(struct xfs_trans *tp, struct xfs_rmap_intent *ri, |
195 | struct xfs_btree_cur **pcur); | |
7e1b84b2 DW |
196 | int __xfs_rmap_finish_intent(struct xfs_btree_cur *rcur, |
197 | enum xfs_rmap_intent_type op, xfs_agblock_t bno, | |
198 | xfs_extlen_t len, const struct xfs_owner_info *oinfo, | |
199 | bool unwritten); | |
9c194644 | 200 | |
ceeb9c83 DW |
201 | int xfs_rmap_lookup_le_range(struct xfs_btree_cur *cur, xfs_agblock_t bno, |
202 | uint64_t owner, uint64_t offset, unsigned int flags, | |
203 | struct xfs_rmap_irec *irec, int *stat); | |
e89c0413 DW |
204 | int xfs_rmap_compare(const struct xfs_rmap_irec *a, |
205 | const struct xfs_rmap_irec *b); | |
26788097 | 206 | union xfs_btree_rec; |
39ab26d5 | 207 | xfs_failaddr_t xfs_rmap_btrec_to_irec(const union xfs_btree_rec *rec, |
26788097 | 208 | struct xfs_rmap_irec *irec); |
32080a9b | 209 | xfs_failaddr_t xfs_rmap_check_irec(struct xfs_perag *pag, |
c4e34172 | 210 | const struct xfs_rmap_irec *irec); |
adafb31c DW |
211 | xfs_failaddr_t xfs_rtrmap_check_irec(struct xfs_rtgroup *rtg, |
212 | const struct xfs_rmap_irec *irec); | |
c4e34172 | 213 | |
6abc7aef DW |
214 | int xfs_rmap_has_records(struct xfs_btree_cur *cur, xfs_agblock_t bno, |
215 | xfs_extlen_t len, enum xbtree_recpacking *outcome); | |
69115f77 DW |
216 | |
217 | struct xfs_rmap_matches { | |
218 | /* Number of owner matches. */ | |
219 | unsigned long long matches; | |
220 | ||
221 | /* Number of non-owner matches. */ | |
222 | unsigned long long non_owner_matches; | |
223 | ||
224 | /* Number of non-owner matches that conflict with the owner matches. */ | |
225 | unsigned long long bad_non_owner_matches; | |
226 | }; | |
227 | ||
228 | int xfs_rmap_count_owners(struct xfs_btree_cur *cur, xfs_agblock_t bno, | |
66e3237e | 229 | xfs_extlen_t len, const struct xfs_owner_info *oinfo, |
69115f77 | 230 | struct xfs_rmap_matches *rmatch); |
4d4f86b4 | 231 | int xfs_rmap_has_other_keys(struct xfs_btree_cur *cur, xfs_agblock_t bno, |
66e3237e | 232 | xfs_extlen_t len, const struct xfs_owner_info *oinfo, |
69115f77 | 233 | bool *has_other); |
4d4f86b4 | 234 | int xfs_rmap_map_raw(struct xfs_btree_cur *cur, struct xfs_rmap_irec *rmap); |
ceeb9c83 | 235 | |
7280feda DW |
236 | extern const struct xfs_owner_info XFS_RMAP_OINFO_SKIP_UPDATE; |
237 | extern const struct xfs_owner_info XFS_RMAP_OINFO_ANY_OWNER; | |
238 | extern const struct xfs_owner_info XFS_RMAP_OINFO_FS; | |
239 | extern const struct xfs_owner_info XFS_RMAP_OINFO_LOG; | |
240 | extern const struct xfs_owner_info XFS_RMAP_OINFO_AG; | |
241 | extern const struct xfs_owner_info XFS_RMAP_OINFO_INOBT; | |
242 | extern const struct xfs_owner_info XFS_RMAP_OINFO_INODES; | |
243 | extern const struct xfs_owner_info XFS_RMAP_OINFO_REFC; | |
244 | extern const struct xfs_owner_info XFS_RMAP_OINFO_COW; | |
245 | ||
f3c799c2 DW |
246 | extern struct kmem_cache *xfs_rmap_intent_cache; |
247 | ||
248 | int __init xfs_rmap_intent_init_cache(void); | |
249 | void xfs_rmap_intent_destroy_cache(void); | |
250 | ||
7e1b84b2 DW |
251 | /* |
252 | * Parameters for tracking reverse mapping changes. The hook function arg | |
253 | * parameter is enum xfs_rmap_intent_type, and the rest is below. | |
254 | */ | |
255 | struct xfs_rmap_update_params { | |
256 | xfs_agblock_t startblock; | |
257 | xfs_extlen_t blockcount; | |
258 | struct xfs_owner_info oinfo; | |
259 | bool unwritten; | |
260 | }; | |
261 | ||
262 | #ifdef CONFIG_XFS_LIVE_HOOKS | |
263 | ||
264 | struct xfs_rmap_hook { | |
265 | struct xfs_hook rmap_hook; | |
266 | }; | |
267 | ||
268 | void xfs_rmap_hook_disable(void); | |
269 | void xfs_rmap_hook_enable(void); | |
270 | ||
eb4a84a3 CH |
271 | int xfs_rmap_hook_add(struct xfs_group *xg, struct xfs_rmap_hook *hook); |
272 | void xfs_rmap_hook_del(struct xfs_group *xg, struct xfs_rmap_hook *hook); | |
7e1b84b2 DW |
273 | void xfs_rmap_hook_setup(struct xfs_rmap_hook *hook, notifier_fn_t mod_fn); |
274 | #endif | |
275 | ||
340785cc | 276 | #endif /* __XFS_RMAP_H__ */ |