| 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 | struct xfs_rtgroup; |
| 11 | |
| 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 | |
| 39 | static inline bool |
| 40 | xfs_rmap_should_skip_owner_update( |
| 41 | const struct xfs_owner_info *oi) |
| 42 | { |
| 43 | return oi->oi_owner == XFS_RMAP_OWN_NULL; |
| 44 | } |
| 45 | |
| 46 | /* Reverse mapping functions. */ |
| 47 | |
| 48 | struct xfs_buf; |
| 49 | |
| 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 | |
| 66 | static inline xfs_failaddr_t |
| 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)) |
| 72 | return __this_address; |
| 73 | |
| 74 | irec->rm_offset = XFS_RMAP_OFF(offset); |
| 75 | irec->rm_flags = 0; |
| 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; |
| 82 | return NULL; |
| 83 | } |
| 84 | |
| 85 | static inline void |
| 86 | xfs_owner_info_unpack( |
| 87 | const struct xfs_owner_info *oinfo, |
| 88 | uint64_t *owner, |
| 89 | uint64_t *offset, |
| 90 | unsigned int *flags) |
| 91 | { |
| 92 | unsigned int r = 0; |
| 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 | |
| 119 | int xfs_rmap_alloc(struct xfs_trans *tp, struct xfs_buf *agbp, |
| 120 | struct xfs_perag *pag, xfs_agblock_t bno, xfs_extlen_t len, |
| 121 | const struct xfs_owner_info *oinfo); |
| 122 | int xfs_rmap_free(struct xfs_trans *tp, struct xfs_buf *agbp, |
| 123 | struct xfs_perag *pag, xfs_agblock_t bno, xfs_extlen_t len, |
| 124 | const struct xfs_owner_info *oinfo); |
| 125 | |
| 126 | int xfs_rmap_lookup_le(struct xfs_btree_cur *cur, xfs_agblock_t bno, |
| 127 | uint64_t owner, uint64_t offset, unsigned int flags, |
| 128 | struct xfs_rmap_irec *irec, int *stat); |
| 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); |
| 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); |
| 135 | int xfs_rmap_get_rec(struct xfs_btree_cur *cur, struct xfs_rmap_irec *irec, |
| 136 | int *stat); |
| 137 | |
| 138 | typedef int (*xfs_rmap_query_range_fn)( |
| 139 | struct xfs_btree_cur *cur, |
| 140 | const struct xfs_rmap_irec *rec, |
| 141 | void *priv); |
| 142 | |
| 143 | int xfs_rmap_query_range(struct xfs_btree_cur *cur, |
| 144 | const struct xfs_rmap_irec *low_rec, |
| 145 | const struct xfs_rmap_irec *high_rec, |
| 146 | xfs_rmap_query_range_fn fn, void *priv); |
| 147 | int xfs_rmap_query_all(struct xfs_btree_cur *cur, xfs_rmap_query_range_fn fn, |
| 148 | void *priv); |
| 149 | |
| 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 | |
| 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 | |
| 171 | struct xfs_rmap_intent { |
| 172 | struct list_head ri_list; |
| 173 | enum xfs_rmap_intent_type ri_type; |
| 174 | int ri_whichfork; |
| 175 | uint64_t ri_owner; |
| 176 | struct xfs_bmbt_irec ri_bmap; |
| 177 | struct xfs_group *ri_group; |
| 178 | bool ri_realtime; |
| 179 | }; |
| 180 | |
| 181 | /* functions for updating the rmapbt based on bmbt map/unmap operations */ |
| 182 | void xfs_rmap_map_extent(struct xfs_trans *tp, struct xfs_inode *ip, |
| 183 | int whichfork, struct xfs_bmbt_irec *imap); |
| 184 | void xfs_rmap_unmap_extent(struct xfs_trans *tp, struct xfs_inode *ip, |
| 185 | int whichfork, struct xfs_bmbt_irec *imap); |
| 186 | void xfs_rmap_convert_extent(struct xfs_mount *mp, struct xfs_trans *tp, |
| 187 | struct xfs_inode *ip, int whichfork, |
| 188 | struct xfs_bmbt_irec *imap); |
| 189 | void xfs_rmap_alloc_extent(struct xfs_trans *tp, bool isrt, xfs_fsblock_t fsbno, |
| 190 | xfs_extlen_t len, uint64_t owner); |
| 191 | void xfs_rmap_free_extent(struct xfs_trans *tp, bool isrt, xfs_fsblock_t fsbno, |
| 192 | xfs_extlen_t len, uint64_t owner); |
| 193 | |
| 194 | int xfs_rmap_finish_one(struct xfs_trans *tp, struct xfs_rmap_intent *ri, |
| 195 | struct xfs_btree_cur **pcur); |
| 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); |
| 200 | |
| 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); |
| 204 | int xfs_rmap_compare(const struct xfs_rmap_irec *a, |
| 205 | const struct xfs_rmap_irec *b); |
| 206 | union xfs_btree_rec; |
| 207 | xfs_failaddr_t xfs_rmap_btrec_to_irec(const union xfs_btree_rec *rec, |
| 208 | struct xfs_rmap_irec *irec); |
| 209 | xfs_failaddr_t xfs_rmap_check_irec(struct xfs_perag *pag, |
| 210 | const struct xfs_rmap_irec *irec); |
| 211 | xfs_failaddr_t xfs_rtrmap_check_irec(struct xfs_rtgroup *rtg, |
| 212 | const struct xfs_rmap_irec *irec); |
| 213 | |
| 214 | int xfs_rmap_has_records(struct xfs_btree_cur *cur, xfs_agblock_t bno, |
| 215 | xfs_extlen_t len, enum xbtree_recpacking *outcome); |
| 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, |
| 229 | xfs_extlen_t len, const struct xfs_owner_info *oinfo, |
| 230 | struct xfs_rmap_matches *rmatch); |
| 231 | int xfs_rmap_has_other_keys(struct xfs_btree_cur *cur, xfs_agblock_t bno, |
| 232 | xfs_extlen_t len, const struct xfs_owner_info *oinfo, |
| 233 | bool *has_other); |
| 234 | int xfs_rmap_map_raw(struct xfs_btree_cur *cur, struct xfs_rmap_irec *rmap); |
| 235 | |
| 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 | |
| 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 | |
| 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 | |
| 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); |
| 273 | void xfs_rmap_hook_setup(struct xfs_rmap_hook *hook, notifier_fn_t mod_fn); |
| 274 | #endif |
| 275 | |
| 276 | #endif /* __XFS_RMAP_H__ */ |