162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright (C) 2016 Oracle.  All Rights Reserved.
462306a36Sopenharmony_ci * Author: Darrick J. Wong <darrick.wong@oracle.com>
562306a36Sopenharmony_ci */
662306a36Sopenharmony_ci#ifndef __XFS_RMAP_H__
762306a36Sopenharmony_ci#define __XFS_RMAP_H__
862306a36Sopenharmony_ci
962306a36Sopenharmony_cistruct xfs_perag;
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_cistatic inline void
1262306a36Sopenharmony_cixfs_rmap_ino_bmbt_owner(
1362306a36Sopenharmony_ci	struct xfs_owner_info	*oi,
1462306a36Sopenharmony_ci	xfs_ino_t		ino,
1562306a36Sopenharmony_ci	int			whichfork)
1662306a36Sopenharmony_ci{
1762306a36Sopenharmony_ci	oi->oi_owner = ino;
1862306a36Sopenharmony_ci	oi->oi_offset = 0;
1962306a36Sopenharmony_ci	oi->oi_flags = XFS_OWNER_INFO_BMBT_BLOCK;
2062306a36Sopenharmony_ci	if (whichfork == XFS_ATTR_FORK)
2162306a36Sopenharmony_ci		oi->oi_flags |= XFS_OWNER_INFO_ATTR_FORK;
2262306a36Sopenharmony_ci}
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_cistatic inline void
2562306a36Sopenharmony_cixfs_rmap_ino_owner(
2662306a36Sopenharmony_ci	struct xfs_owner_info	*oi,
2762306a36Sopenharmony_ci	xfs_ino_t		ino,
2862306a36Sopenharmony_ci	int			whichfork,
2962306a36Sopenharmony_ci	xfs_fileoff_t		offset)
3062306a36Sopenharmony_ci{
3162306a36Sopenharmony_ci	oi->oi_owner = ino;
3262306a36Sopenharmony_ci	oi->oi_offset = offset;
3362306a36Sopenharmony_ci	oi->oi_flags = 0;
3462306a36Sopenharmony_ci	if (whichfork == XFS_ATTR_FORK)
3562306a36Sopenharmony_ci		oi->oi_flags |= XFS_OWNER_INFO_ATTR_FORK;
3662306a36Sopenharmony_ci}
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_cistatic inline bool
3962306a36Sopenharmony_cixfs_rmap_should_skip_owner_update(
4062306a36Sopenharmony_ci	const struct xfs_owner_info	*oi)
4162306a36Sopenharmony_ci{
4262306a36Sopenharmony_ci	return oi->oi_owner == XFS_RMAP_OWN_NULL;
4362306a36Sopenharmony_ci}
4462306a36Sopenharmony_ci
4562306a36Sopenharmony_ci/* Reverse mapping functions. */
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_cistruct xfs_buf;
4862306a36Sopenharmony_ci
4962306a36Sopenharmony_cistatic inline __u64
5062306a36Sopenharmony_cixfs_rmap_irec_offset_pack(
5162306a36Sopenharmony_ci	const struct xfs_rmap_irec	*irec)
5262306a36Sopenharmony_ci{
5362306a36Sopenharmony_ci	__u64			x;
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_ci	x = XFS_RMAP_OFF(irec->rm_offset);
5662306a36Sopenharmony_ci	if (irec->rm_flags & XFS_RMAP_ATTR_FORK)
5762306a36Sopenharmony_ci		x |= XFS_RMAP_OFF_ATTR_FORK;
5862306a36Sopenharmony_ci	if (irec->rm_flags & XFS_RMAP_BMBT_BLOCK)
5962306a36Sopenharmony_ci		x |= XFS_RMAP_OFF_BMBT_BLOCK;
6062306a36Sopenharmony_ci	if (irec->rm_flags & XFS_RMAP_UNWRITTEN)
6162306a36Sopenharmony_ci		x |= XFS_RMAP_OFF_UNWRITTEN;
6262306a36Sopenharmony_ci	return x;
6362306a36Sopenharmony_ci}
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_cistatic inline xfs_failaddr_t
6662306a36Sopenharmony_cixfs_rmap_irec_offset_unpack(
6762306a36Sopenharmony_ci	__u64			offset,
6862306a36Sopenharmony_ci	struct xfs_rmap_irec	*irec)
6962306a36Sopenharmony_ci{
7062306a36Sopenharmony_ci	if (offset & ~(XFS_RMAP_OFF_MASK | XFS_RMAP_OFF_FLAGS))
7162306a36Sopenharmony_ci		return __this_address;
7262306a36Sopenharmony_ci
7362306a36Sopenharmony_ci	irec->rm_offset = XFS_RMAP_OFF(offset);
7462306a36Sopenharmony_ci	irec->rm_flags = 0;
7562306a36Sopenharmony_ci	if (offset & XFS_RMAP_OFF_ATTR_FORK)
7662306a36Sopenharmony_ci		irec->rm_flags |= XFS_RMAP_ATTR_FORK;
7762306a36Sopenharmony_ci	if (offset & XFS_RMAP_OFF_BMBT_BLOCK)
7862306a36Sopenharmony_ci		irec->rm_flags |= XFS_RMAP_BMBT_BLOCK;
7962306a36Sopenharmony_ci	if (offset & XFS_RMAP_OFF_UNWRITTEN)
8062306a36Sopenharmony_ci		irec->rm_flags |= XFS_RMAP_UNWRITTEN;
8162306a36Sopenharmony_ci	return NULL;
8262306a36Sopenharmony_ci}
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_cistatic inline void
8562306a36Sopenharmony_cixfs_owner_info_unpack(
8662306a36Sopenharmony_ci	const struct xfs_owner_info	*oinfo,
8762306a36Sopenharmony_ci	uint64_t			*owner,
8862306a36Sopenharmony_ci	uint64_t			*offset,
8962306a36Sopenharmony_ci	unsigned int			*flags)
9062306a36Sopenharmony_ci{
9162306a36Sopenharmony_ci	unsigned int			r = 0;
9262306a36Sopenharmony_ci
9362306a36Sopenharmony_ci	*owner = oinfo->oi_owner;
9462306a36Sopenharmony_ci	*offset = oinfo->oi_offset;
9562306a36Sopenharmony_ci	if (oinfo->oi_flags & XFS_OWNER_INFO_ATTR_FORK)
9662306a36Sopenharmony_ci		r |= XFS_RMAP_ATTR_FORK;
9762306a36Sopenharmony_ci	if (oinfo->oi_flags & XFS_OWNER_INFO_BMBT_BLOCK)
9862306a36Sopenharmony_ci		r |= XFS_RMAP_BMBT_BLOCK;
9962306a36Sopenharmony_ci	*flags = r;
10062306a36Sopenharmony_ci}
10162306a36Sopenharmony_ci
10262306a36Sopenharmony_cistatic inline void
10362306a36Sopenharmony_cixfs_owner_info_pack(
10462306a36Sopenharmony_ci	struct xfs_owner_info	*oinfo,
10562306a36Sopenharmony_ci	uint64_t		owner,
10662306a36Sopenharmony_ci	uint64_t		offset,
10762306a36Sopenharmony_ci	unsigned int		flags)
10862306a36Sopenharmony_ci{
10962306a36Sopenharmony_ci	oinfo->oi_owner = owner;
11062306a36Sopenharmony_ci	oinfo->oi_offset = XFS_RMAP_OFF(offset);
11162306a36Sopenharmony_ci	oinfo->oi_flags = 0;
11262306a36Sopenharmony_ci	if (flags & XFS_RMAP_ATTR_FORK)
11362306a36Sopenharmony_ci		oinfo->oi_flags |= XFS_OWNER_INFO_ATTR_FORK;
11462306a36Sopenharmony_ci	if (flags & XFS_RMAP_BMBT_BLOCK)
11562306a36Sopenharmony_ci		oinfo->oi_flags |= XFS_OWNER_INFO_BMBT_BLOCK;
11662306a36Sopenharmony_ci}
11762306a36Sopenharmony_ci
11862306a36Sopenharmony_ciint xfs_rmap_alloc(struct xfs_trans *tp, struct xfs_buf *agbp,
11962306a36Sopenharmony_ci		   struct xfs_perag *pag, xfs_agblock_t bno, xfs_extlen_t len,
12062306a36Sopenharmony_ci		   const struct xfs_owner_info *oinfo);
12162306a36Sopenharmony_ciint xfs_rmap_free(struct xfs_trans *tp, struct xfs_buf *agbp,
12262306a36Sopenharmony_ci		  struct xfs_perag *pag, xfs_agblock_t bno, xfs_extlen_t len,
12362306a36Sopenharmony_ci		  const struct xfs_owner_info *oinfo);
12462306a36Sopenharmony_ci
12562306a36Sopenharmony_ciint xfs_rmap_lookup_le(struct xfs_btree_cur *cur, xfs_agblock_t bno,
12662306a36Sopenharmony_ci		uint64_t owner, uint64_t offset, unsigned int flags,
12762306a36Sopenharmony_ci		struct xfs_rmap_irec *irec, int *stat);
12862306a36Sopenharmony_ciint xfs_rmap_lookup_eq(struct xfs_btree_cur *cur, xfs_agblock_t bno,
12962306a36Sopenharmony_ci		xfs_extlen_t len, uint64_t owner, uint64_t offset,
13062306a36Sopenharmony_ci		unsigned int flags, int *stat);
13162306a36Sopenharmony_ciint xfs_rmap_insert(struct xfs_btree_cur *rcur, xfs_agblock_t agbno,
13262306a36Sopenharmony_ci		xfs_extlen_t len, uint64_t owner, uint64_t offset,
13362306a36Sopenharmony_ci		unsigned int flags);
13462306a36Sopenharmony_ciint xfs_rmap_get_rec(struct xfs_btree_cur *cur, struct xfs_rmap_irec *irec,
13562306a36Sopenharmony_ci		int *stat);
13662306a36Sopenharmony_ci
13762306a36Sopenharmony_citypedef int (*xfs_rmap_query_range_fn)(
13862306a36Sopenharmony_ci	struct xfs_btree_cur		*cur,
13962306a36Sopenharmony_ci	const struct xfs_rmap_irec	*rec,
14062306a36Sopenharmony_ci	void				*priv);
14162306a36Sopenharmony_ci
14262306a36Sopenharmony_ciint xfs_rmap_query_range(struct xfs_btree_cur *cur,
14362306a36Sopenharmony_ci		const struct xfs_rmap_irec *low_rec,
14462306a36Sopenharmony_ci		const struct xfs_rmap_irec *high_rec,
14562306a36Sopenharmony_ci		xfs_rmap_query_range_fn fn, void *priv);
14662306a36Sopenharmony_ciint xfs_rmap_query_all(struct xfs_btree_cur *cur, xfs_rmap_query_range_fn fn,
14762306a36Sopenharmony_ci		void *priv);
14862306a36Sopenharmony_ci
14962306a36Sopenharmony_cienum xfs_rmap_intent_type {
15062306a36Sopenharmony_ci	XFS_RMAP_MAP,
15162306a36Sopenharmony_ci	XFS_RMAP_MAP_SHARED,
15262306a36Sopenharmony_ci	XFS_RMAP_UNMAP,
15362306a36Sopenharmony_ci	XFS_RMAP_UNMAP_SHARED,
15462306a36Sopenharmony_ci	XFS_RMAP_CONVERT,
15562306a36Sopenharmony_ci	XFS_RMAP_CONVERT_SHARED,
15662306a36Sopenharmony_ci	XFS_RMAP_ALLOC,
15762306a36Sopenharmony_ci	XFS_RMAP_FREE,
15862306a36Sopenharmony_ci};
15962306a36Sopenharmony_ci
16062306a36Sopenharmony_cistruct xfs_rmap_intent {
16162306a36Sopenharmony_ci	struct list_head			ri_list;
16262306a36Sopenharmony_ci	enum xfs_rmap_intent_type		ri_type;
16362306a36Sopenharmony_ci	int					ri_whichfork;
16462306a36Sopenharmony_ci	uint64_t				ri_owner;
16562306a36Sopenharmony_ci	struct xfs_bmbt_irec			ri_bmap;
16662306a36Sopenharmony_ci	struct xfs_perag			*ri_pag;
16762306a36Sopenharmony_ci};
16862306a36Sopenharmony_ci
16962306a36Sopenharmony_civoid xfs_rmap_update_get_group(struct xfs_mount *mp,
17062306a36Sopenharmony_ci		struct xfs_rmap_intent *ri);
17162306a36Sopenharmony_ci
17262306a36Sopenharmony_ci/* functions for updating the rmapbt based on bmbt map/unmap operations */
17362306a36Sopenharmony_civoid xfs_rmap_map_extent(struct xfs_trans *tp, struct xfs_inode *ip,
17462306a36Sopenharmony_ci		int whichfork, struct xfs_bmbt_irec *imap);
17562306a36Sopenharmony_civoid xfs_rmap_unmap_extent(struct xfs_trans *tp, struct xfs_inode *ip,
17662306a36Sopenharmony_ci		int whichfork, struct xfs_bmbt_irec *imap);
17762306a36Sopenharmony_civoid xfs_rmap_convert_extent(struct xfs_mount *mp, struct xfs_trans *tp,
17862306a36Sopenharmony_ci		struct xfs_inode *ip, int whichfork,
17962306a36Sopenharmony_ci		struct xfs_bmbt_irec *imap);
18062306a36Sopenharmony_civoid xfs_rmap_alloc_extent(struct xfs_trans *tp, xfs_agnumber_t agno,
18162306a36Sopenharmony_ci		xfs_agblock_t bno, xfs_extlen_t len, uint64_t owner);
18262306a36Sopenharmony_civoid xfs_rmap_free_extent(struct xfs_trans *tp, xfs_agnumber_t agno,
18362306a36Sopenharmony_ci		xfs_agblock_t bno, xfs_extlen_t len, uint64_t owner);
18462306a36Sopenharmony_ci
18562306a36Sopenharmony_civoid xfs_rmap_finish_one_cleanup(struct xfs_trans *tp,
18662306a36Sopenharmony_ci		struct xfs_btree_cur *rcur, int error);
18762306a36Sopenharmony_ciint xfs_rmap_finish_one(struct xfs_trans *tp, struct xfs_rmap_intent *ri,
18862306a36Sopenharmony_ci		struct xfs_btree_cur **pcur);
18962306a36Sopenharmony_ci
19062306a36Sopenharmony_ciint xfs_rmap_lookup_le_range(struct xfs_btree_cur *cur, xfs_agblock_t bno,
19162306a36Sopenharmony_ci		uint64_t owner, uint64_t offset, unsigned int flags,
19262306a36Sopenharmony_ci		struct xfs_rmap_irec *irec, int	*stat);
19362306a36Sopenharmony_ciint xfs_rmap_compare(const struct xfs_rmap_irec *a,
19462306a36Sopenharmony_ci		const struct xfs_rmap_irec *b);
19562306a36Sopenharmony_ciunion xfs_btree_rec;
19662306a36Sopenharmony_cixfs_failaddr_t xfs_rmap_btrec_to_irec(const union xfs_btree_rec *rec,
19762306a36Sopenharmony_ci		struct xfs_rmap_irec *irec);
19862306a36Sopenharmony_cixfs_failaddr_t xfs_rmap_check_irec(struct xfs_btree_cur *cur,
19962306a36Sopenharmony_ci		const struct xfs_rmap_irec *irec);
20062306a36Sopenharmony_ci
20162306a36Sopenharmony_ciint xfs_rmap_has_records(struct xfs_btree_cur *cur, xfs_agblock_t bno,
20262306a36Sopenharmony_ci		xfs_extlen_t len, enum xbtree_recpacking *outcome);
20362306a36Sopenharmony_ci
20462306a36Sopenharmony_cistruct xfs_rmap_matches {
20562306a36Sopenharmony_ci	/* Number of owner matches. */
20662306a36Sopenharmony_ci	unsigned long long	matches;
20762306a36Sopenharmony_ci
20862306a36Sopenharmony_ci	/* Number of non-owner matches. */
20962306a36Sopenharmony_ci	unsigned long long	non_owner_matches;
21062306a36Sopenharmony_ci
21162306a36Sopenharmony_ci	/* Number of non-owner matches that conflict with the owner matches. */
21262306a36Sopenharmony_ci	unsigned long long	bad_non_owner_matches;
21362306a36Sopenharmony_ci};
21462306a36Sopenharmony_ci
21562306a36Sopenharmony_ciint xfs_rmap_count_owners(struct xfs_btree_cur *cur, xfs_agblock_t bno,
21662306a36Sopenharmony_ci		xfs_extlen_t len, const struct xfs_owner_info *oinfo,
21762306a36Sopenharmony_ci		struct xfs_rmap_matches *rmatch);
21862306a36Sopenharmony_ciint xfs_rmap_has_other_keys(struct xfs_btree_cur *cur, xfs_agblock_t bno,
21962306a36Sopenharmony_ci		xfs_extlen_t len, const struct xfs_owner_info *oinfo,
22062306a36Sopenharmony_ci		bool *has_other);
22162306a36Sopenharmony_ciint xfs_rmap_map_raw(struct xfs_btree_cur *cur, struct xfs_rmap_irec *rmap);
22262306a36Sopenharmony_ci
22362306a36Sopenharmony_ciextern const struct xfs_owner_info XFS_RMAP_OINFO_SKIP_UPDATE;
22462306a36Sopenharmony_ciextern const struct xfs_owner_info XFS_RMAP_OINFO_ANY_OWNER;
22562306a36Sopenharmony_ciextern const struct xfs_owner_info XFS_RMAP_OINFO_FS;
22662306a36Sopenharmony_ciextern const struct xfs_owner_info XFS_RMAP_OINFO_LOG;
22762306a36Sopenharmony_ciextern const struct xfs_owner_info XFS_RMAP_OINFO_AG;
22862306a36Sopenharmony_ciextern const struct xfs_owner_info XFS_RMAP_OINFO_INOBT;
22962306a36Sopenharmony_ciextern const struct xfs_owner_info XFS_RMAP_OINFO_INODES;
23062306a36Sopenharmony_ciextern const struct xfs_owner_info XFS_RMAP_OINFO_REFC;
23162306a36Sopenharmony_ciextern const struct xfs_owner_info XFS_RMAP_OINFO_COW;
23262306a36Sopenharmony_ci
23362306a36Sopenharmony_ciextern struct kmem_cache	*xfs_rmap_intent_cache;
23462306a36Sopenharmony_ci
23562306a36Sopenharmony_ciint __init xfs_rmap_intent_init_cache(void);
23662306a36Sopenharmony_civoid xfs_rmap_intent_destroy_cache(void);
23762306a36Sopenharmony_ci
23862306a36Sopenharmony_ci#endif	/* __XFS_RMAP_H__ */
239