xref: /linux/fs/xfs/libxfs/xfs_rmap.h (revision ec63e2a4897075e427c121d863bd89c44578094f)
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
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
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
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
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
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 	if (offset & XFS_RMAP_OFF_ATTR_FORK)
72 		irec->rm_flags |= XFS_RMAP_ATTR_FORK;
73 	if (offset & XFS_RMAP_OFF_BMBT_BLOCK)
74 		irec->rm_flags |= XFS_RMAP_BMBT_BLOCK;
75 	if (offset & XFS_RMAP_OFF_UNWRITTEN)
76 		irec->rm_flags |= XFS_RMAP_UNWRITTEN;
77 	return 0;
78 }
79 
80 static inline void
81 xfs_owner_info_unpack(
82 	const struct xfs_owner_info	*oinfo,
83 	uint64_t			*owner,
84 	uint64_t			*offset,
85 	unsigned int			*flags)
86 {
87 	unsigned int			r = 0;
88 
89 	*owner = oinfo->oi_owner;
90 	*offset = oinfo->oi_offset;
91 	if (oinfo->oi_flags & XFS_OWNER_INFO_ATTR_FORK)
92 		r |= XFS_RMAP_ATTR_FORK;
93 	if (oinfo->oi_flags & XFS_OWNER_INFO_BMBT_BLOCK)
94 		r |= XFS_RMAP_BMBT_BLOCK;
95 	*flags = r;
96 }
97 
98 static inline void
99 xfs_owner_info_pack(
100 	struct xfs_owner_info	*oinfo,
101 	uint64_t		owner,
102 	uint64_t		offset,
103 	unsigned int		flags)
104 {
105 	oinfo->oi_owner = owner;
106 	oinfo->oi_offset = XFS_RMAP_OFF(offset);
107 	oinfo->oi_flags = 0;
108 	if (flags & XFS_RMAP_ATTR_FORK)
109 		oinfo->oi_flags |= XFS_OWNER_INFO_ATTR_FORK;
110 	if (flags & XFS_RMAP_BMBT_BLOCK)
111 		oinfo->oi_flags |= XFS_OWNER_INFO_BMBT_BLOCK;
112 }
113 
114 int xfs_rmap_alloc(struct xfs_trans *tp, struct xfs_buf *agbp,
115 		   xfs_agnumber_t agno, xfs_agblock_t bno, xfs_extlen_t len,
116 		   const struct xfs_owner_info *oinfo);
117 int xfs_rmap_free(struct xfs_trans *tp, struct xfs_buf *agbp,
118 		  xfs_agnumber_t agno, xfs_agblock_t bno, xfs_extlen_t len,
119 		  const struct xfs_owner_info *oinfo);
120 
121 int xfs_rmap_lookup_le(struct xfs_btree_cur *cur, xfs_agblock_t bno,
122 		xfs_extlen_t len, uint64_t owner, uint64_t offset,
123 		unsigned int flags, int *stat);
124 int xfs_rmap_lookup_eq(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);
127 int xfs_rmap_insert(struct xfs_btree_cur *rcur, xfs_agblock_t agbno,
128 		xfs_extlen_t len, uint64_t owner, uint64_t offset,
129 		unsigned int flags);
130 int xfs_rmap_get_rec(struct xfs_btree_cur *cur, struct xfs_rmap_irec *irec,
131 		int *stat);
132 
133 typedef int (*xfs_rmap_query_range_fn)(
134 	struct xfs_btree_cur	*cur,
135 	struct xfs_rmap_irec	*rec,
136 	void			*priv);
137 
138 int xfs_rmap_query_range(struct xfs_btree_cur *cur,
139 		struct xfs_rmap_irec *low_rec, struct xfs_rmap_irec *high_rec,
140 		xfs_rmap_query_range_fn fn, void *priv);
141 int xfs_rmap_query_all(struct xfs_btree_cur *cur, xfs_rmap_query_range_fn fn,
142 		void *priv);
143 
144 enum xfs_rmap_intent_type {
145 	XFS_RMAP_MAP,
146 	XFS_RMAP_MAP_SHARED,
147 	XFS_RMAP_UNMAP,
148 	XFS_RMAP_UNMAP_SHARED,
149 	XFS_RMAP_CONVERT,
150 	XFS_RMAP_CONVERT_SHARED,
151 	XFS_RMAP_ALLOC,
152 	XFS_RMAP_FREE,
153 };
154 
155 struct xfs_rmap_intent {
156 	struct list_head			ri_list;
157 	enum xfs_rmap_intent_type		ri_type;
158 	uint64_t				ri_owner;
159 	int					ri_whichfork;
160 	struct xfs_bmbt_irec			ri_bmap;
161 };
162 
163 /* functions for updating the rmapbt based on bmbt map/unmap operations */
164 int xfs_rmap_map_extent(struct xfs_trans *tp, struct xfs_inode *ip,
165 		int whichfork, struct xfs_bmbt_irec *imap);
166 int xfs_rmap_unmap_extent(struct xfs_trans *tp, struct xfs_inode *ip,
167 		int whichfork, struct xfs_bmbt_irec *imap);
168 int xfs_rmap_convert_extent(struct xfs_mount *mp, struct xfs_trans *tp,
169 		struct xfs_inode *ip, int whichfork,
170 		struct xfs_bmbt_irec *imap);
171 int xfs_rmap_alloc_extent(struct xfs_trans *tp, xfs_agnumber_t agno,
172 		xfs_agblock_t bno, xfs_extlen_t len, uint64_t owner);
173 int xfs_rmap_free_extent(struct xfs_trans *tp, xfs_agnumber_t agno,
174 		xfs_agblock_t bno, xfs_extlen_t len, uint64_t owner);
175 
176 void xfs_rmap_finish_one_cleanup(struct xfs_trans *tp,
177 		struct xfs_btree_cur *rcur, int error);
178 int xfs_rmap_finish_one(struct xfs_trans *tp, enum xfs_rmap_intent_type type,
179 		uint64_t owner, int whichfork, xfs_fileoff_t startoff,
180 		xfs_fsblock_t startblock, xfs_filblks_t blockcount,
181 		xfs_exntst_t state, struct xfs_btree_cur **pcur);
182 
183 int xfs_rmap_find_left_neighbor(struct xfs_btree_cur *cur, xfs_agblock_t bno,
184 		uint64_t owner, uint64_t offset, unsigned int flags,
185 		struct xfs_rmap_irec *irec, int	*stat);
186 int xfs_rmap_lookup_le_range(struct xfs_btree_cur *cur, xfs_agblock_t bno,
187 		uint64_t owner, uint64_t offset, unsigned int flags,
188 		struct xfs_rmap_irec *irec, int	*stat);
189 int xfs_rmap_compare(const struct xfs_rmap_irec *a,
190 		const struct xfs_rmap_irec *b);
191 union xfs_btree_rec;
192 int xfs_rmap_btrec_to_irec(union xfs_btree_rec *rec,
193 		struct xfs_rmap_irec *irec);
194 int xfs_rmap_has_record(struct xfs_btree_cur *cur, xfs_agblock_t bno,
195 		xfs_extlen_t len, bool *exists);
196 int xfs_rmap_record_exists(struct xfs_btree_cur *cur, xfs_agblock_t bno,
197 		xfs_extlen_t len, const struct xfs_owner_info *oinfo,
198 		bool *has_rmap);
199 int xfs_rmap_has_other_keys(struct xfs_btree_cur *cur, xfs_agblock_t bno,
200 		xfs_extlen_t len, const struct xfs_owner_info *oinfo,
201 		bool *has_rmap);
202 int xfs_rmap_map_raw(struct xfs_btree_cur *cur, struct xfs_rmap_irec *rmap);
203 
204 extern const struct xfs_owner_info XFS_RMAP_OINFO_SKIP_UPDATE;
205 extern const struct xfs_owner_info XFS_RMAP_OINFO_ANY_OWNER;
206 extern const struct xfs_owner_info XFS_RMAP_OINFO_FS;
207 extern const struct xfs_owner_info XFS_RMAP_OINFO_LOG;
208 extern const struct xfs_owner_info XFS_RMAP_OINFO_AG;
209 extern const struct xfs_owner_info XFS_RMAP_OINFO_INOBT;
210 extern const struct xfs_owner_info XFS_RMAP_OINFO_INODES;
211 extern const struct xfs_owner_info XFS_RMAP_OINFO_REFC;
212 extern const struct xfs_owner_info XFS_RMAP_OINFO_COW;
213 
214 #endif	/* __XFS_RMAP_H__ */
215