xref: /OK3568_Linux_fs/kernel/fs/xfs/libxfs/xfs_rmap.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0+
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright (C) 2016 Oracle.  All Rights Reserved.
4*4882a593Smuzhiyun  * Author: Darrick J. Wong <darrick.wong@oracle.com>
5*4882a593Smuzhiyun  */
6*4882a593Smuzhiyun #ifndef __XFS_RMAP_H__
7*4882a593Smuzhiyun #define __XFS_RMAP_H__
8*4882a593Smuzhiyun 
9*4882a593Smuzhiyun static inline void
xfs_rmap_ino_bmbt_owner(struct xfs_owner_info * oi,xfs_ino_t ino,int whichfork)10*4882a593Smuzhiyun xfs_rmap_ino_bmbt_owner(
11*4882a593Smuzhiyun 	struct xfs_owner_info	*oi,
12*4882a593Smuzhiyun 	xfs_ino_t		ino,
13*4882a593Smuzhiyun 	int			whichfork)
14*4882a593Smuzhiyun {
15*4882a593Smuzhiyun 	oi->oi_owner = ino;
16*4882a593Smuzhiyun 	oi->oi_offset = 0;
17*4882a593Smuzhiyun 	oi->oi_flags = XFS_OWNER_INFO_BMBT_BLOCK;
18*4882a593Smuzhiyun 	if (whichfork == XFS_ATTR_FORK)
19*4882a593Smuzhiyun 		oi->oi_flags |= XFS_OWNER_INFO_ATTR_FORK;
20*4882a593Smuzhiyun }
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun static inline void
xfs_rmap_ino_owner(struct xfs_owner_info * oi,xfs_ino_t ino,int whichfork,xfs_fileoff_t offset)23*4882a593Smuzhiyun xfs_rmap_ino_owner(
24*4882a593Smuzhiyun 	struct xfs_owner_info	*oi,
25*4882a593Smuzhiyun 	xfs_ino_t		ino,
26*4882a593Smuzhiyun 	int			whichfork,
27*4882a593Smuzhiyun 	xfs_fileoff_t		offset)
28*4882a593Smuzhiyun {
29*4882a593Smuzhiyun 	oi->oi_owner = ino;
30*4882a593Smuzhiyun 	oi->oi_offset = offset;
31*4882a593Smuzhiyun 	oi->oi_flags = 0;
32*4882a593Smuzhiyun 	if (whichfork == XFS_ATTR_FORK)
33*4882a593Smuzhiyun 		oi->oi_flags |= XFS_OWNER_INFO_ATTR_FORK;
34*4882a593Smuzhiyun }
35*4882a593Smuzhiyun 
36*4882a593Smuzhiyun static inline bool
xfs_rmap_should_skip_owner_update(const struct xfs_owner_info * oi)37*4882a593Smuzhiyun xfs_rmap_should_skip_owner_update(
38*4882a593Smuzhiyun 	const struct xfs_owner_info	*oi)
39*4882a593Smuzhiyun {
40*4882a593Smuzhiyun 	return oi->oi_owner == XFS_RMAP_OWN_NULL;
41*4882a593Smuzhiyun }
42*4882a593Smuzhiyun 
43*4882a593Smuzhiyun /* Reverse mapping functions. */
44*4882a593Smuzhiyun 
45*4882a593Smuzhiyun struct xfs_buf;
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun static inline __u64
xfs_rmap_irec_offset_pack(const struct xfs_rmap_irec * irec)48*4882a593Smuzhiyun xfs_rmap_irec_offset_pack(
49*4882a593Smuzhiyun 	const struct xfs_rmap_irec	*irec)
50*4882a593Smuzhiyun {
51*4882a593Smuzhiyun 	__u64			x;
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun 	x = XFS_RMAP_OFF(irec->rm_offset);
54*4882a593Smuzhiyun 	if (irec->rm_flags & XFS_RMAP_ATTR_FORK)
55*4882a593Smuzhiyun 		x |= XFS_RMAP_OFF_ATTR_FORK;
56*4882a593Smuzhiyun 	if (irec->rm_flags & XFS_RMAP_BMBT_BLOCK)
57*4882a593Smuzhiyun 		x |= XFS_RMAP_OFF_BMBT_BLOCK;
58*4882a593Smuzhiyun 	if (irec->rm_flags & XFS_RMAP_UNWRITTEN)
59*4882a593Smuzhiyun 		x |= XFS_RMAP_OFF_UNWRITTEN;
60*4882a593Smuzhiyun 	return x;
61*4882a593Smuzhiyun }
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun static inline int
xfs_rmap_irec_offset_unpack(__u64 offset,struct xfs_rmap_irec * irec)64*4882a593Smuzhiyun xfs_rmap_irec_offset_unpack(
65*4882a593Smuzhiyun 	__u64			offset,
66*4882a593Smuzhiyun 	struct xfs_rmap_irec	*irec)
67*4882a593Smuzhiyun {
68*4882a593Smuzhiyun 	if (offset & ~(XFS_RMAP_OFF_MASK | XFS_RMAP_OFF_FLAGS))
69*4882a593Smuzhiyun 		return -EFSCORRUPTED;
70*4882a593Smuzhiyun 	irec->rm_offset = XFS_RMAP_OFF(offset);
71*4882a593Smuzhiyun 	irec->rm_flags = 0;
72*4882a593Smuzhiyun 	if (offset & XFS_RMAP_OFF_ATTR_FORK)
73*4882a593Smuzhiyun 		irec->rm_flags |= XFS_RMAP_ATTR_FORK;
74*4882a593Smuzhiyun 	if (offset & XFS_RMAP_OFF_BMBT_BLOCK)
75*4882a593Smuzhiyun 		irec->rm_flags |= XFS_RMAP_BMBT_BLOCK;
76*4882a593Smuzhiyun 	if (offset & XFS_RMAP_OFF_UNWRITTEN)
77*4882a593Smuzhiyun 		irec->rm_flags |= XFS_RMAP_UNWRITTEN;
78*4882a593Smuzhiyun 	return 0;
79*4882a593Smuzhiyun }
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun static inline void
xfs_owner_info_unpack(const struct xfs_owner_info * oinfo,uint64_t * owner,uint64_t * offset,unsigned int * flags)82*4882a593Smuzhiyun xfs_owner_info_unpack(
83*4882a593Smuzhiyun 	const struct xfs_owner_info	*oinfo,
84*4882a593Smuzhiyun 	uint64_t			*owner,
85*4882a593Smuzhiyun 	uint64_t			*offset,
86*4882a593Smuzhiyun 	unsigned int			*flags)
87*4882a593Smuzhiyun {
88*4882a593Smuzhiyun 	unsigned int			r = 0;
89*4882a593Smuzhiyun 
90*4882a593Smuzhiyun 	*owner = oinfo->oi_owner;
91*4882a593Smuzhiyun 	*offset = oinfo->oi_offset;
92*4882a593Smuzhiyun 	if (oinfo->oi_flags & XFS_OWNER_INFO_ATTR_FORK)
93*4882a593Smuzhiyun 		r |= XFS_RMAP_ATTR_FORK;
94*4882a593Smuzhiyun 	if (oinfo->oi_flags & XFS_OWNER_INFO_BMBT_BLOCK)
95*4882a593Smuzhiyun 		r |= XFS_RMAP_BMBT_BLOCK;
96*4882a593Smuzhiyun 	*flags = r;
97*4882a593Smuzhiyun }
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun static inline void
xfs_owner_info_pack(struct xfs_owner_info * oinfo,uint64_t owner,uint64_t offset,unsigned int flags)100*4882a593Smuzhiyun xfs_owner_info_pack(
101*4882a593Smuzhiyun 	struct xfs_owner_info	*oinfo,
102*4882a593Smuzhiyun 	uint64_t		owner,
103*4882a593Smuzhiyun 	uint64_t		offset,
104*4882a593Smuzhiyun 	unsigned int		flags)
105*4882a593Smuzhiyun {
106*4882a593Smuzhiyun 	oinfo->oi_owner = owner;
107*4882a593Smuzhiyun 	oinfo->oi_offset = XFS_RMAP_OFF(offset);
108*4882a593Smuzhiyun 	oinfo->oi_flags = 0;
109*4882a593Smuzhiyun 	if (flags & XFS_RMAP_ATTR_FORK)
110*4882a593Smuzhiyun 		oinfo->oi_flags |= XFS_OWNER_INFO_ATTR_FORK;
111*4882a593Smuzhiyun 	if (flags & XFS_RMAP_BMBT_BLOCK)
112*4882a593Smuzhiyun 		oinfo->oi_flags |= XFS_OWNER_INFO_BMBT_BLOCK;
113*4882a593Smuzhiyun }
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun int xfs_rmap_alloc(struct xfs_trans *tp, struct xfs_buf *agbp,
116*4882a593Smuzhiyun 		   xfs_agnumber_t agno, xfs_agblock_t bno, xfs_extlen_t len,
117*4882a593Smuzhiyun 		   const struct xfs_owner_info *oinfo);
118*4882a593Smuzhiyun int xfs_rmap_free(struct xfs_trans *tp, struct xfs_buf *agbp,
119*4882a593Smuzhiyun 		  xfs_agnumber_t agno, xfs_agblock_t bno, xfs_extlen_t len,
120*4882a593Smuzhiyun 		  const struct xfs_owner_info *oinfo);
121*4882a593Smuzhiyun 
122*4882a593Smuzhiyun int xfs_rmap_lookup_le(struct xfs_btree_cur *cur, xfs_agblock_t bno,
123*4882a593Smuzhiyun 		xfs_extlen_t len, uint64_t owner, uint64_t offset,
124*4882a593Smuzhiyun 		unsigned int flags, int *stat);
125*4882a593Smuzhiyun int xfs_rmap_lookup_eq(struct xfs_btree_cur *cur, xfs_agblock_t bno,
126*4882a593Smuzhiyun 		xfs_extlen_t len, uint64_t owner, uint64_t offset,
127*4882a593Smuzhiyun 		unsigned int flags, int *stat);
128*4882a593Smuzhiyun int xfs_rmap_insert(struct xfs_btree_cur *rcur, xfs_agblock_t agbno,
129*4882a593Smuzhiyun 		xfs_extlen_t len, uint64_t owner, uint64_t offset,
130*4882a593Smuzhiyun 		unsigned int flags);
131*4882a593Smuzhiyun int xfs_rmap_get_rec(struct xfs_btree_cur *cur, struct xfs_rmap_irec *irec,
132*4882a593Smuzhiyun 		int *stat);
133*4882a593Smuzhiyun 
134*4882a593Smuzhiyun typedef int (*xfs_rmap_query_range_fn)(
135*4882a593Smuzhiyun 	struct xfs_btree_cur	*cur,
136*4882a593Smuzhiyun 	struct xfs_rmap_irec	*rec,
137*4882a593Smuzhiyun 	void			*priv);
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun int xfs_rmap_query_range(struct xfs_btree_cur *cur,
140*4882a593Smuzhiyun 		struct xfs_rmap_irec *low_rec, struct xfs_rmap_irec *high_rec,
141*4882a593Smuzhiyun 		xfs_rmap_query_range_fn fn, void *priv);
142*4882a593Smuzhiyun int xfs_rmap_query_all(struct xfs_btree_cur *cur, xfs_rmap_query_range_fn fn,
143*4882a593Smuzhiyun 		void *priv);
144*4882a593Smuzhiyun 
145*4882a593Smuzhiyun enum xfs_rmap_intent_type {
146*4882a593Smuzhiyun 	XFS_RMAP_MAP,
147*4882a593Smuzhiyun 	XFS_RMAP_MAP_SHARED,
148*4882a593Smuzhiyun 	XFS_RMAP_UNMAP,
149*4882a593Smuzhiyun 	XFS_RMAP_UNMAP_SHARED,
150*4882a593Smuzhiyun 	XFS_RMAP_CONVERT,
151*4882a593Smuzhiyun 	XFS_RMAP_CONVERT_SHARED,
152*4882a593Smuzhiyun 	XFS_RMAP_ALLOC,
153*4882a593Smuzhiyun 	XFS_RMAP_FREE,
154*4882a593Smuzhiyun };
155*4882a593Smuzhiyun 
156*4882a593Smuzhiyun struct xfs_rmap_intent {
157*4882a593Smuzhiyun 	struct list_head			ri_list;
158*4882a593Smuzhiyun 	enum xfs_rmap_intent_type		ri_type;
159*4882a593Smuzhiyun 	uint64_t				ri_owner;
160*4882a593Smuzhiyun 	int					ri_whichfork;
161*4882a593Smuzhiyun 	struct xfs_bmbt_irec			ri_bmap;
162*4882a593Smuzhiyun };
163*4882a593Smuzhiyun 
164*4882a593Smuzhiyun /* functions for updating the rmapbt based on bmbt map/unmap operations */
165*4882a593Smuzhiyun void xfs_rmap_map_extent(struct xfs_trans *tp, struct xfs_inode *ip,
166*4882a593Smuzhiyun 		int whichfork, struct xfs_bmbt_irec *imap);
167*4882a593Smuzhiyun void xfs_rmap_unmap_extent(struct xfs_trans *tp, struct xfs_inode *ip,
168*4882a593Smuzhiyun 		int whichfork, struct xfs_bmbt_irec *imap);
169*4882a593Smuzhiyun void xfs_rmap_convert_extent(struct xfs_mount *mp, struct xfs_trans *tp,
170*4882a593Smuzhiyun 		struct xfs_inode *ip, int whichfork,
171*4882a593Smuzhiyun 		struct xfs_bmbt_irec *imap);
172*4882a593Smuzhiyun void xfs_rmap_alloc_extent(struct xfs_trans *tp, xfs_agnumber_t agno,
173*4882a593Smuzhiyun 		xfs_agblock_t bno, xfs_extlen_t len, uint64_t owner);
174*4882a593Smuzhiyun void xfs_rmap_free_extent(struct xfs_trans *tp, xfs_agnumber_t agno,
175*4882a593Smuzhiyun 		xfs_agblock_t bno, xfs_extlen_t len, uint64_t owner);
176*4882a593Smuzhiyun 
177*4882a593Smuzhiyun void xfs_rmap_finish_one_cleanup(struct xfs_trans *tp,
178*4882a593Smuzhiyun 		struct xfs_btree_cur *rcur, int error);
179*4882a593Smuzhiyun int xfs_rmap_finish_one(struct xfs_trans *tp, enum xfs_rmap_intent_type type,
180*4882a593Smuzhiyun 		uint64_t owner, int whichfork, xfs_fileoff_t startoff,
181*4882a593Smuzhiyun 		xfs_fsblock_t startblock, xfs_filblks_t blockcount,
182*4882a593Smuzhiyun 		xfs_exntst_t state, struct xfs_btree_cur **pcur);
183*4882a593Smuzhiyun 
184*4882a593Smuzhiyun int xfs_rmap_find_left_neighbor(struct xfs_btree_cur *cur, xfs_agblock_t bno,
185*4882a593Smuzhiyun 		uint64_t owner, uint64_t offset, unsigned int flags,
186*4882a593Smuzhiyun 		struct xfs_rmap_irec *irec, int	*stat);
187*4882a593Smuzhiyun int xfs_rmap_lookup_le_range(struct xfs_btree_cur *cur, xfs_agblock_t bno,
188*4882a593Smuzhiyun 		uint64_t owner, uint64_t offset, unsigned int flags,
189*4882a593Smuzhiyun 		struct xfs_rmap_irec *irec, int	*stat);
190*4882a593Smuzhiyun int xfs_rmap_compare(const struct xfs_rmap_irec *a,
191*4882a593Smuzhiyun 		const struct xfs_rmap_irec *b);
192*4882a593Smuzhiyun union xfs_btree_rec;
193*4882a593Smuzhiyun int xfs_rmap_btrec_to_irec(union xfs_btree_rec *rec,
194*4882a593Smuzhiyun 		struct xfs_rmap_irec *irec);
195*4882a593Smuzhiyun int xfs_rmap_has_record(struct xfs_btree_cur *cur, xfs_agblock_t bno,
196*4882a593Smuzhiyun 		xfs_extlen_t len, bool *exists);
197*4882a593Smuzhiyun int xfs_rmap_record_exists(struct xfs_btree_cur *cur, xfs_agblock_t bno,
198*4882a593Smuzhiyun 		xfs_extlen_t len, const struct xfs_owner_info *oinfo,
199*4882a593Smuzhiyun 		bool *has_rmap);
200*4882a593Smuzhiyun int xfs_rmap_has_other_keys(struct xfs_btree_cur *cur, xfs_agblock_t bno,
201*4882a593Smuzhiyun 		xfs_extlen_t len, const struct xfs_owner_info *oinfo,
202*4882a593Smuzhiyun 		bool *has_rmap);
203*4882a593Smuzhiyun int xfs_rmap_map_raw(struct xfs_btree_cur *cur, struct xfs_rmap_irec *rmap);
204*4882a593Smuzhiyun 
205*4882a593Smuzhiyun extern const struct xfs_owner_info XFS_RMAP_OINFO_SKIP_UPDATE;
206*4882a593Smuzhiyun extern const struct xfs_owner_info XFS_RMAP_OINFO_ANY_OWNER;
207*4882a593Smuzhiyun extern const struct xfs_owner_info XFS_RMAP_OINFO_FS;
208*4882a593Smuzhiyun extern const struct xfs_owner_info XFS_RMAP_OINFO_LOG;
209*4882a593Smuzhiyun extern const struct xfs_owner_info XFS_RMAP_OINFO_AG;
210*4882a593Smuzhiyun extern const struct xfs_owner_info XFS_RMAP_OINFO_INOBT;
211*4882a593Smuzhiyun extern const struct xfs_owner_info XFS_RMAP_OINFO_INODES;
212*4882a593Smuzhiyun extern const struct xfs_owner_info XFS_RMAP_OINFO_REFC;
213*4882a593Smuzhiyun extern const struct xfs_owner_info XFS_RMAP_OINFO_COW;
214*4882a593Smuzhiyun 
215*4882a593Smuzhiyun #endif	/* __XFS_RMAP_H__ */
216