blob: b718ebeda372d67191801c39246b5198d798f7ab [file] [log] [blame]
Dave Chinner0b61f8a2018-06-05 19:42:14 -07001// SPDX-License-Identifier: GPL-2.0+
Darrick J. Wong340785c2016-08-03 11:33:42 +10002/*
3 * Copyright (C) 2016 Oracle. All Rights Reserved.
Darrick J. Wong340785c2016-08-03 11:33:42 +10004 * Author: Darrick J. Wong <darrick.wong@oracle.com>
Darrick J. Wong340785c2016-08-03 11:33:42 +10005 */
6#ifndef __XFS_RMAP_H__
7#define __XFS_RMAP_H__
8
Dave Chinnerfa9c3c12021-06-02 10:48:24 +10009struct xfs_perag;
10
Darrick J. Wong340785c2016-08-03 11:33:42 +100011static inline void
Darrick J. Wong340785c2016-08-03 11:33:42 +100012xfs_rmap_ino_bmbt_owner(
13 struct xfs_owner_info *oi,
14 xfs_ino_t ino,
15 int whichfork)
16{
17 oi->oi_owner = ino;
18 oi->oi_offset = 0;
19 oi->oi_flags = XFS_OWNER_INFO_BMBT_BLOCK;
20 if (whichfork == XFS_ATTR_FORK)
21 oi->oi_flags |= XFS_OWNER_INFO_ATTR_FORK;
22}
23
24static inline void
25xfs_rmap_ino_owner(
26 struct xfs_owner_info *oi,
27 xfs_ino_t ino,
28 int whichfork,
29 xfs_fileoff_t offset)
30{
31 oi->oi_owner = ino;
32 oi->oi_offset = offset;
33 oi->oi_flags = 0;
34 if (whichfork == XFS_ATTR_FORK)
35 oi->oi_flags |= XFS_OWNER_INFO_ATTR_FORK;
36}
37
Darrick J. Wong33df3a92017-12-07 19:07:27 -080038static inline bool
39xfs_rmap_should_skip_owner_update(
Darrick J. Wong66e32372018-12-12 08:46:23 -080040 const struct xfs_owner_info *oi)
Darrick J. Wong33df3a92017-12-07 19:07:27 -080041{
42 return oi->oi_owner == XFS_RMAP_OWN_NULL;
43}
44
Darrick J. Wong673930c2016-08-03 11:33:43 +100045/* Reverse mapping functions. */
46
47struct xfs_buf;
48
Darrick J. Wong035e00a2016-08-03 11:36:07 +100049static inline __u64
50xfs_rmap_irec_offset_pack(
51 const struct xfs_rmap_irec *irec)
52{
53 __u64 x;
54
55 x = XFS_RMAP_OFF(irec->rm_offset);
56 if (irec->rm_flags & XFS_RMAP_ATTR_FORK)
57 x |= XFS_RMAP_OFF_ATTR_FORK;
58 if (irec->rm_flags & XFS_RMAP_BMBT_BLOCK)
59 x |= XFS_RMAP_OFF_BMBT_BLOCK;
60 if (irec->rm_flags & XFS_RMAP_UNWRITTEN)
61 x |= XFS_RMAP_OFF_UNWRITTEN;
62 return x;
63}
64
65static inline int
66xfs_rmap_irec_offset_unpack(
67 __u64 offset,
68 struct xfs_rmap_irec *irec)
69{
70 if (offset & ~(XFS_RMAP_OFF_MASK | XFS_RMAP_OFF_FLAGS))
71 return -EFSCORRUPTED;
72 irec->rm_offset = XFS_RMAP_OFF(offset);
Darrick J. Wongffb56962019-08-26 17:06:05 -070073 irec->rm_flags = 0;
Darrick J. Wong035e00a2016-08-03 11:36:07 +100074 if (offset & XFS_RMAP_OFF_ATTR_FORK)
75 irec->rm_flags |= XFS_RMAP_ATTR_FORK;
76 if (offset & XFS_RMAP_OFF_BMBT_BLOCK)
77 irec->rm_flags |= XFS_RMAP_BMBT_BLOCK;
78 if (offset & XFS_RMAP_OFF_UNWRITTEN)
79 irec->rm_flags |= XFS_RMAP_UNWRITTEN;
80 return 0;
81}
82
83static inline void
84xfs_owner_info_unpack(
Darrick J. Wong66e32372018-12-12 08:46:23 -080085 const struct xfs_owner_info *oinfo,
86 uint64_t *owner,
87 uint64_t *offset,
88 unsigned int *flags)
Darrick J. Wong035e00a2016-08-03 11:36:07 +100089{
Darrick J. Wong66e32372018-12-12 08:46:23 -080090 unsigned int r = 0;
Darrick J. Wong035e00a2016-08-03 11:36:07 +100091
92 *owner = oinfo->oi_owner;
93 *offset = oinfo->oi_offset;
94 if (oinfo->oi_flags & XFS_OWNER_INFO_ATTR_FORK)
95 r |= XFS_RMAP_ATTR_FORK;
96 if (oinfo->oi_flags & XFS_OWNER_INFO_BMBT_BLOCK)
97 r |= XFS_RMAP_BMBT_BLOCK;
98 *flags = r;
99}
100
101static inline void
102xfs_owner_info_pack(
103 struct xfs_owner_info *oinfo,
104 uint64_t owner,
105 uint64_t offset,
106 unsigned int flags)
107{
108 oinfo->oi_owner = owner;
109 oinfo->oi_offset = XFS_RMAP_OFF(offset);
110 oinfo->oi_flags = 0;
111 if (flags & XFS_RMAP_ATTR_FORK)
112 oinfo->oi_flags |= XFS_OWNER_INFO_ATTR_FORK;
113 if (flags & XFS_RMAP_BMBT_BLOCK)
114 oinfo->oi_flags |= XFS_OWNER_INFO_BMBT_BLOCK;
115}
116
Darrick J. Wong673930c2016-08-03 11:33:43 +1000117int xfs_rmap_alloc(struct xfs_trans *tp, struct xfs_buf *agbp,
Dave Chinnerfa9c3c12021-06-02 10:48:24 +1000118 struct xfs_perag *pag, xfs_agblock_t bno, xfs_extlen_t len,
Darrick J. Wong66e32372018-12-12 08:46:23 -0800119 const struct xfs_owner_info *oinfo);
Darrick J. Wong673930c2016-08-03 11:33:43 +1000120int xfs_rmap_free(struct xfs_trans *tp, struct xfs_buf *agbp,
Dave Chinnerfa9c3c12021-06-02 10:48:24 +1000121 struct xfs_perag *pag, xfs_agblock_t bno, xfs_extlen_t len,
Darrick J. Wong66e32372018-12-12 08:46:23 -0800122 const struct xfs_owner_info *oinfo);
Darrick J. Wong673930c2016-08-03 11:33:43 +1000123
Darrick J. Wong4b8ed672016-08-03 11:39:05 +1000124int xfs_rmap_lookup_le(struct xfs_btree_cur *cur, xfs_agblock_t bno,
125 xfs_extlen_t len, uint64_t owner, uint64_t offset,
126 unsigned int flags, int *stat);
127int xfs_rmap_lookup_eq(struct xfs_btree_cur *cur, xfs_agblock_t bno,
128 xfs_extlen_t len, uint64_t owner, uint64_t offset,
129 unsigned int flags, int *stat);
Darrick J. Wongabf09232016-08-03 12:03:58 +1000130int xfs_rmap_insert(struct xfs_btree_cur *rcur, xfs_agblock_t agbno,
131 xfs_extlen_t len, uint64_t owner, uint64_t offset,
132 unsigned int flags);
Darrick J. Wong4b8ed672016-08-03 11:39:05 +1000133int xfs_rmap_get_rec(struct xfs_btree_cur *cur, struct xfs_rmap_irec *irec,
134 int *stat);
135
Darrick J. Wongc5438382016-08-03 11:42:39 +1000136typedef int (*xfs_rmap_query_range_fn)(
Darrick J. Wong159eb692021-08-10 17:02:16 -0700137 struct xfs_btree_cur *cur,
138 const struct xfs_rmap_irec *rec,
139 void *priv);
Darrick J. Wongc5438382016-08-03 11:42:39 +1000140
141int xfs_rmap_query_range(struct xfs_btree_cur *cur,
Darrick J. Wong04dcb472021-08-10 17:02:15 -0700142 const struct xfs_rmap_irec *low_rec,
143 const struct xfs_rmap_irec *high_rec,
Darrick J. Wongc5438382016-08-03 11:42:39 +1000144 xfs_rmap_query_range_fn fn, void *priv);
Darrick J. Wonge9a25992017-03-28 14:56:35 -0700145int xfs_rmap_query_all(struct xfs_btree_cur *cur, xfs_rmap_query_range_fn fn,
146 void *priv);
Darrick J. Wongc5438382016-08-03 11:42:39 +1000147
Darrick J. Wong5880f2d72016-08-03 12:04:45 +1000148enum xfs_rmap_intent_type {
149 XFS_RMAP_MAP,
150 XFS_RMAP_MAP_SHARED,
151 XFS_RMAP_UNMAP,
152 XFS_RMAP_UNMAP_SHARED,
153 XFS_RMAP_CONVERT,
154 XFS_RMAP_CONVERT_SHARED,
155 XFS_RMAP_ALLOC,
156 XFS_RMAP_FREE,
157};
158
159struct xfs_rmap_intent {
160 struct list_head ri_list;
161 enum xfs_rmap_intent_type ri_type;
Darrick J. Wong5880f2d72016-08-03 12:04:45 +1000162 int ri_whichfork;
Darrick J. Wong9e253952021-10-12 15:29:33 -0700163 uint64_t ri_owner;
Darrick J. Wong5880f2d72016-08-03 12:04:45 +1000164 struct xfs_bmbt_irec ri_bmap;
165};
166
Darrick J. Wong9c194642016-08-03 12:16:05 +1000167/* functions for updating the rmapbt based on bmbt map/unmap operations */
Darrick J. Wongbc46ac62019-08-26 17:06:03 -0700168void xfs_rmap_map_extent(struct xfs_trans *tp, struct xfs_inode *ip,
Brian Foster0f37d172018-08-01 07:20:34 -0700169 int whichfork, struct xfs_bmbt_irec *imap);
Darrick J. Wongbc46ac62019-08-26 17:06:03 -0700170void xfs_rmap_unmap_extent(struct xfs_trans *tp, struct xfs_inode *ip,
Brian Foster0f37d172018-08-01 07:20:34 -0700171 int whichfork, struct xfs_bmbt_irec *imap);
Darrick J. Wongbc46ac62019-08-26 17:06:03 -0700172void xfs_rmap_convert_extent(struct xfs_mount *mp, struct xfs_trans *tp,
Darrick J. Wong9c194642016-08-03 12:16:05 +1000173 struct xfs_inode *ip, int whichfork,
174 struct xfs_bmbt_irec *imap);
Darrick J. Wongbc46ac62019-08-26 17:06:03 -0700175void xfs_rmap_alloc_extent(struct xfs_trans *tp, xfs_agnumber_t agno,
Brian Foster0f37d172018-08-01 07:20:34 -0700176 xfs_agblock_t bno, xfs_extlen_t len, uint64_t owner);
Darrick J. Wongbc46ac62019-08-26 17:06:03 -0700177void xfs_rmap_free_extent(struct xfs_trans *tp, xfs_agnumber_t agno,
Brian Foster0f37d172018-08-01 07:20:34 -0700178 xfs_agblock_t bno, xfs_extlen_t len, uint64_t owner);
Darrick J. Wong9c194642016-08-03 12:16:05 +1000179
180void xfs_rmap_finish_one_cleanup(struct xfs_trans *tp,
181 struct xfs_btree_cur *rcur, int error);
182int xfs_rmap_finish_one(struct xfs_trans *tp, enum xfs_rmap_intent_type type,
Darrick J. Wongc8ce5402017-06-16 11:00:05 -0700183 uint64_t owner, int whichfork, xfs_fileoff_t startoff,
Darrick J. Wong9c194642016-08-03 12:16:05 +1000184 xfs_fsblock_t startblock, xfs_filblks_t blockcount,
185 xfs_exntst_t state, struct xfs_btree_cur **pcur);
186
Darrick J. Wongceeb9c82016-10-03 09:11:48 -0700187int xfs_rmap_find_left_neighbor(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);
190int xfs_rmap_lookup_le_range(struct xfs_btree_cur *cur, xfs_agblock_t bno,
191 uint64_t owner, uint64_t offset, unsigned int flags,
192 struct xfs_rmap_irec *irec, int *stat);
Darrick J. Wonge89c0412017-03-28 14:56:37 -0700193int xfs_rmap_compare(const struct xfs_rmap_irec *a,
194 const struct xfs_rmap_irec *b);
Darrick J. Wong26788092017-06-16 11:00:07 -0700195union xfs_btree_rec;
Darrick J. Wong159eb692021-08-10 17:02:16 -0700196int xfs_rmap_btrec_to_irec(const union xfs_btree_rec *rec,
Darrick J. Wong26788092017-06-16 11:00:07 -0700197 struct xfs_rmap_irec *irec);
Darrick J. Wonged7c52d2018-01-16 18:52:13 -0800198int xfs_rmap_has_record(struct xfs_btree_cur *cur, xfs_agblock_t bno,
199 xfs_extlen_t len, bool *exists);
200int xfs_rmap_record_exists(struct xfs_btree_cur *cur, xfs_agblock_t bno,
Darrick J. Wong66e32372018-12-12 08:46:23 -0800201 xfs_extlen_t len, const struct xfs_owner_info *oinfo,
Darrick J. Wonged7c52d2018-01-16 18:52:13 -0800202 bool *has_rmap);
Darrick J. Wong4d4f86b2018-05-09 10:02:02 -0700203int xfs_rmap_has_other_keys(struct xfs_btree_cur *cur, xfs_agblock_t bno,
Darrick J. Wong66e32372018-12-12 08:46:23 -0800204 xfs_extlen_t len, const struct xfs_owner_info *oinfo,
Darrick J. Wong4d4f86b2018-05-09 10:02:02 -0700205 bool *has_rmap);
206int xfs_rmap_map_raw(struct xfs_btree_cur *cur, struct xfs_rmap_irec *rmap);
Darrick J. Wongceeb9c82016-10-03 09:11:48 -0700207
Darrick J. Wong7280fed2018-12-12 08:46:23 -0800208extern const struct xfs_owner_info XFS_RMAP_OINFO_SKIP_UPDATE;
209extern const struct xfs_owner_info XFS_RMAP_OINFO_ANY_OWNER;
210extern const struct xfs_owner_info XFS_RMAP_OINFO_FS;
211extern const struct xfs_owner_info XFS_RMAP_OINFO_LOG;
212extern const struct xfs_owner_info XFS_RMAP_OINFO_AG;
213extern const struct xfs_owner_info XFS_RMAP_OINFO_INOBT;
214extern const struct xfs_owner_info XFS_RMAP_OINFO_INODES;
215extern const struct xfs_owner_info XFS_RMAP_OINFO_REFC;
216extern const struct xfs_owner_info XFS_RMAP_OINFO_COW;
217
Darrick J. Wongf3c799c2021-10-12 14:11:01 -0700218extern struct kmem_cache *xfs_rmap_intent_cache;
219
220int __init xfs_rmap_intent_init_cache(void);
221void xfs_rmap_intent_destroy_cache(void);
222
Darrick J. Wong340785c2016-08-03 11:33:42 +1000223#endif /* __XFS_RMAP_H__ */