18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2016 Oracle.  All Rights Reserved.
48c2ecf20Sopenharmony_ci * Author: Darrick J. Wong <darrick.wong@oracle.com>
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci#ifndef __XFS_RMAP_H__
78c2ecf20Sopenharmony_ci#define __XFS_RMAP_H__
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_cistatic inline void
108c2ecf20Sopenharmony_cixfs_rmap_ino_bmbt_owner(
118c2ecf20Sopenharmony_ci	struct xfs_owner_info	*oi,
128c2ecf20Sopenharmony_ci	xfs_ino_t		ino,
138c2ecf20Sopenharmony_ci	int			whichfork)
148c2ecf20Sopenharmony_ci{
158c2ecf20Sopenharmony_ci	oi->oi_owner = ino;
168c2ecf20Sopenharmony_ci	oi->oi_offset = 0;
178c2ecf20Sopenharmony_ci	oi->oi_flags = XFS_OWNER_INFO_BMBT_BLOCK;
188c2ecf20Sopenharmony_ci	if (whichfork == XFS_ATTR_FORK)
198c2ecf20Sopenharmony_ci		oi->oi_flags |= XFS_OWNER_INFO_ATTR_FORK;
208c2ecf20Sopenharmony_ci}
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_cistatic inline void
238c2ecf20Sopenharmony_cixfs_rmap_ino_owner(
248c2ecf20Sopenharmony_ci	struct xfs_owner_info	*oi,
258c2ecf20Sopenharmony_ci	xfs_ino_t		ino,
268c2ecf20Sopenharmony_ci	int			whichfork,
278c2ecf20Sopenharmony_ci	xfs_fileoff_t		offset)
288c2ecf20Sopenharmony_ci{
298c2ecf20Sopenharmony_ci	oi->oi_owner = ino;
308c2ecf20Sopenharmony_ci	oi->oi_offset = offset;
318c2ecf20Sopenharmony_ci	oi->oi_flags = 0;
328c2ecf20Sopenharmony_ci	if (whichfork == XFS_ATTR_FORK)
338c2ecf20Sopenharmony_ci		oi->oi_flags |= XFS_OWNER_INFO_ATTR_FORK;
348c2ecf20Sopenharmony_ci}
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_cistatic inline bool
378c2ecf20Sopenharmony_cixfs_rmap_should_skip_owner_update(
388c2ecf20Sopenharmony_ci	const struct xfs_owner_info	*oi)
398c2ecf20Sopenharmony_ci{
408c2ecf20Sopenharmony_ci	return oi->oi_owner == XFS_RMAP_OWN_NULL;
418c2ecf20Sopenharmony_ci}
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci/* Reverse mapping functions. */
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_cistruct xfs_buf;
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_cistatic inline __u64
488c2ecf20Sopenharmony_cixfs_rmap_irec_offset_pack(
498c2ecf20Sopenharmony_ci	const struct xfs_rmap_irec	*irec)
508c2ecf20Sopenharmony_ci{
518c2ecf20Sopenharmony_ci	__u64			x;
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci	x = XFS_RMAP_OFF(irec->rm_offset);
548c2ecf20Sopenharmony_ci	if (irec->rm_flags & XFS_RMAP_ATTR_FORK)
558c2ecf20Sopenharmony_ci		x |= XFS_RMAP_OFF_ATTR_FORK;
568c2ecf20Sopenharmony_ci	if (irec->rm_flags & XFS_RMAP_BMBT_BLOCK)
578c2ecf20Sopenharmony_ci		x |= XFS_RMAP_OFF_BMBT_BLOCK;
588c2ecf20Sopenharmony_ci	if (irec->rm_flags & XFS_RMAP_UNWRITTEN)
598c2ecf20Sopenharmony_ci		x |= XFS_RMAP_OFF_UNWRITTEN;
608c2ecf20Sopenharmony_ci	return x;
618c2ecf20Sopenharmony_ci}
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_cistatic inline int
648c2ecf20Sopenharmony_cixfs_rmap_irec_offset_unpack(
658c2ecf20Sopenharmony_ci	__u64			offset,
668c2ecf20Sopenharmony_ci	struct xfs_rmap_irec	*irec)
678c2ecf20Sopenharmony_ci{
688c2ecf20Sopenharmony_ci	if (offset & ~(XFS_RMAP_OFF_MASK | XFS_RMAP_OFF_FLAGS))
698c2ecf20Sopenharmony_ci		return -EFSCORRUPTED;
708c2ecf20Sopenharmony_ci	irec->rm_offset = XFS_RMAP_OFF(offset);
718c2ecf20Sopenharmony_ci	irec->rm_flags = 0;
728c2ecf20Sopenharmony_ci	if (offset & XFS_RMAP_OFF_ATTR_FORK)
738c2ecf20Sopenharmony_ci		irec->rm_flags |= XFS_RMAP_ATTR_FORK;
748c2ecf20Sopenharmony_ci	if (offset & XFS_RMAP_OFF_BMBT_BLOCK)
758c2ecf20Sopenharmony_ci		irec->rm_flags |= XFS_RMAP_BMBT_BLOCK;
768c2ecf20Sopenharmony_ci	if (offset & XFS_RMAP_OFF_UNWRITTEN)
778c2ecf20Sopenharmony_ci		irec->rm_flags |= XFS_RMAP_UNWRITTEN;
788c2ecf20Sopenharmony_ci	return 0;
798c2ecf20Sopenharmony_ci}
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_cistatic inline void
828c2ecf20Sopenharmony_cixfs_owner_info_unpack(
838c2ecf20Sopenharmony_ci	const struct xfs_owner_info	*oinfo,
848c2ecf20Sopenharmony_ci	uint64_t			*owner,
858c2ecf20Sopenharmony_ci	uint64_t			*offset,
868c2ecf20Sopenharmony_ci	unsigned int			*flags)
878c2ecf20Sopenharmony_ci{
888c2ecf20Sopenharmony_ci	unsigned int			r = 0;
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci	*owner = oinfo->oi_owner;
918c2ecf20Sopenharmony_ci	*offset = oinfo->oi_offset;
928c2ecf20Sopenharmony_ci	if (oinfo->oi_flags & XFS_OWNER_INFO_ATTR_FORK)
938c2ecf20Sopenharmony_ci		r |= XFS_RMAP_ATTR_FORK;
948c2ecf20Sopenharmony_ci	if (oinfo->oi_flags & XFS_OWNER_INFO_BMBT_BLOCK)
958c2ecf20Sopenharmony_ci		r |= XFS_RMAP_BMBT_BLOCK;
968c2ecf20Sopenharmony_ci	*flags = r;
978c2ecf20Sopenharmony_ci}
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_cistatic inline void
1008c2ecf20Sopenharmony_cixfs_owner_info_pack(
1018c2ecf20Sopenharmony_ci	struct xfs_owner_info	*oinfo,
1028c2ecf20Sopenharmony_ci	uint64_t		owner,
1038c2ecf20Sopenharmony_ci	uint64_t		offset,
1048c2ecf20Sopenharmony_ci	unsigned int		flags)
1058c2ecf20Sopenharmony_ci{
1068c2ecf20Sopenharmony_ci	oinfo->oi_owner = owner;
1078c2ecf20Sopenharmony_ci	oinfo->oi_offset = XFS_RMAP_OFF(offset);
1088c2ecf20Sopenharmony_ci	oinfo->oi_flags = 0;
1098c2ecf20Sopenharmony_ci	if (flags & XFS_RMAP_ATTR_FORK)
1108c2ecf20Sopenharmony_ci		oinfo->oi_flags |= XFS_OWNER_INFO_ATTR_FORK;
1118c2ecf20Sopenharmony_ci	if (flags & XFS_RMAP_BMBT_BLOCK)
1128c2ecf20Sopenharmony_ci		oinfo->oi_flags |= XFS_OWNER_INFO_BMBT_BLOCK;
1138c2ecf20Sopenharmony_ci}
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ciint xfs_rmap_alloc(struct xfs_trans *tp, struct xfs_buf *agbp,
1168c2ecf20Sopenharmony_ci		   xfs_agnumber_t agno, xfs_agblock_t bno, xfs_extlen_t len,
1178c2ecf20Sopenharmony_ci		   const struct xfs_owner_info *oinfo);
1188c2ecf20Sopenharmony_ciint xfs_rmap_free(struct xfs_trans *tp, struct xfs_buf *agbp,
1198c2ecf20Sopenharmony_ci		  xfs_agnumber_t agno, xfs_agblock_t bno, xfs_extlen_t len,
1208c2ecf20Sopenharmony_ci		  const struct xfs_owner_info *oinfo);
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ciint xfs_rmap_lookup_le(struct xfs_btree_cur *cur, xfs_agblock_t bno,
1238c2ecf20Sopenharmony_ci		xfs_extlen_t len, uint64_t owner, uint64_t offset,
1248c2ecf20Sopenharmony_ci		unsigned int flags, int *stat);
1258c2ecf20Sopenharmony_ciint xfs_rmap_lookup_eq(struct xfs_btree_cur *cur, xfs_agblock_t bno,
1268c2ecf20Sopenharmony_ci		xfs_extlen_t len, uint64_t owner, uint64_t offset,
1278c2ecf20Sopenharmony_ci		unsigned int flags, int *stat);
1288c2ecf20Sopenharmony_ciint xfs_rmap_insert(struct xfs_btree_cur *rcur, xfs_agblock_t agbno,
1298c2ecf20Sopenharmony_ci		xfs_extlen_t len, uint64_t owner, uint64_t offset,
1308c2ecf20Sopenharmony_ci		unsigned int flags);
1318c2ecf20Sopenharmony_ciint xfs_rmap_get_rec(struct xfs_btree_cur *cur, struct xfs_rmap_irec *irec,
1328c2ecf20Sopenharmony_ci		int *stat);
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_citypedef int (*xfs_rmap_query_range_fn)(
1358c2ecf20Sopenharmony_ci	struct xfs_btree_cur	*cur,
1368c2ecf20Sopenharmony_ci	struct xfs_rmap_irec	*rec,
1378c2ecf20Sopenharmony_ci	void			*priv);
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ciint xfs_rmap_query_range(struct xfs_btree_cur *cur,
1408c2ecf20Sopenharmony_ci		struct xfs_rmap_irec *low_rec, struct xfs_rmap_irec *high_rec,
1418c2ecf20Sopenharmony_ci		xfs_rmap_query_range_fn fn, void *priv);
1428c2ecf20Sopenharmony_ciint xfs_rmap_query_all(struct xfs_btree_cur *cur, xfs_rmap_query_range_fn fn,
1438c2ecf20Sopenharmony_ci		void *priv);
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_cienum xfs_rmap_intent_type {
1468c2ecf20Sopenharmony_ci	XFS_RMAP_MAP,
1478c2ecf20Sopenharmony_ci	XFS_RMAP_MAP_SHARED,
1488c2ecf20Sopenharmony_ci	XFS_RMAP_UNMAP,
1498c2ecf20Sopenharmony_ci	XFS_RMAP_UNMAP_SHARED,
1508c2ecf20Sopenharmony_ci	XFS_RMAP_CONVERT,
1518c2ecf20Sopenharmony_ci	XFS_RMAP_CONVERT_SHARED,
1528c2ecf20Sopenharmony_ci	XFS_RMAP_ALLOC,
1538c2ecf20Sopenharmony_ci	XFS_RMAP_FREE,
1548c2ecf20Sopenharmony_ci};
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_cistruct xfs_rmap_intent {
1578c2ecf20Sopenharmony_ci	struct list_head			ri_list;
1588c2ecf20Sopenharmony_ci	enum xfs_rmap_intent_type		ri_type;
1598c2ecf20Sopenharmony_ci	uint64_t				ri_owner;
1608c2ecf20Sopenharmony_ci	int					ri_whichfork;
1618c2ecf20Sopenharmony_ci	struct xfs_bmbt_irec			ri_bmap;
1628c2ecf20Sopenharmony_ci};
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci/* functions for updating the rmapbt based on bmbt map/unmap operations */
1658c2ecf20Sopenharmony_civoid xfs_rmap_map_extent(struct xfs_trans *tp, struct xfs_inode *ip,
1668c2ecf20Sopenharmony_ci		int whichfork, struct xfs_bmbt_irec *imap);
1678c2ecf20Sopenharmony_civoid xfs_rmap_unmap_extent(struct xfs_trans *tp, struct xfs_inode *ip,
1688c2ecf20Sopenharmony_ci		int whichfork, struct xfs_bmbt_irec *imap);
1698c2ecf20Sopenharmony_civoid xfs_rmap_convert_extent(struct xfs_mount *mp, struct xfs_trans *tp,
1708c2ecf20Sopenharmony_ci		struct xfs_inode *ip, int whichfork,
1718c2ecf20Sopenharmony_ci		struct xfs_bmbt_irec *imap);
1728c2ecf20Sopenharmony_civoid xfs_rmap_alloc_extent(struct xfs_trans *tp, xfs_agnumber_t agno,
1738c2ecf20Sopenharmony_ci		xfs_agblock_t bno, xfs_extlen_t len, uint64_t owner);
1748c2ecf20Sopenharmony_civoid xfs_rmap_free_extent(struct xfs_trans *tp, xfs_agnumber_t agno,
1758c2ecf20Sopenharmony_ci		xfs_agblock_t bno, xfs_extlen_t len, uint64_t owner);
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_civoid xfs_rmap_finish_one_cleanup(struct xfs_trans *tp,
1788c2ecf20Sopenharmony_ci		struct xfs_btree_cur *rcur, int error);
1798c2ecf20Sopenharmony_ciint xfs_rmap_finish_one(struct xfs_trans *tp, enum xfs_rmap_intent_type type,
1808c2ecf20Sopenharmony_ci		uint64_t owner, int whichfork, xfs_fileoff_t startoff,
1818c2ecf20Sopenharmony_ci		xfs_fsblock_t startblock, xfs_filblks_t blockcount,
1828c2ecf20Sopenharmony_ci		xfs_exntst_t state, struct xfs_btree_cur **pcur);
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ciint xfs_rmap_find_left_neighbor(struct xfs_btree_cur *cur, xfs_agblock_t bno,
1858c2ecf20Sopenharmony_ci		uint64_t owner, uint64_t offset, unsigned int flags,
1868c2ecf20Sopenharmony_ci		struct xfs_rmap_irec *irec, int	*stat);
1878c2ecf20Sopenharmony_ciint xfs_rmap_lookup_le_range(struct xfs_btree_cur *cur, xfs_agblock_t bno,
1888c2ecf20Sopenharmony_ci		uint64_t owner, uint64_t offset, unsigned int flags,
1898c2ecf20Sopenharmony_ci		struct xfs_rmap_irec *irec, int	*stat);
1908c2ecf20Sopenharmony_ciint xfs_rmap_compare(const struct xfs_rmap_irec *a,
1918c2ecf20Sopenharmony_ci		const struct xfs_rmap_irec *b);
1928c2ecf20Sopenharmony_ciunion xfs_btree_rec;
1938c2ecf20Sopenharmony_ciint xfs_rmap_btrec_to_irec(union xfs_btree_rec *rec,
1948c2ecf20Sopenharmony_ci		struct xfs_rmap_irec *irec);
1958c2ecf20Sopenharmony_ciint xfs_rmap_has_record(struct xfs_btree_cur *cur, xfs_agblock_t bno,
1968c2ecf20Sopenharmony_ci		xfs_extlen_t len, bool *exists);
1978c2ecf20Sopenharmony_ciint xfs_rmap_record_exists(struct xfs_btree_cur *cur, xfs_agblock_t bno,
1988c2ecf20Sopenharmony_ci		xfs_extlen_t len, const struct xfs_owner_info *oinfo,
1998c2ecf20Sopenharmony_ci		bool *has_rmap);
2008c2ecf20Sopenharmony_ciint xfs_rmap_has_other_keys(struct xfs_btree_cur *cur, xfs_agblock_t bno,
2018c2ecf20Sopenharmony_ci		xfs_extlen_t len, const struct xfs_owner_info *oinfo,
2028c2ecf20Sopenharmony_ci		bool *has_rmap);
2038c2ecf20Sopenharmony_ciint xfs_rmap_map_raw(struct xfs_btree_cur *cur, struct xfs_rmap_irec *rmap);
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_ciextern const struct xfs_owner_info XFS_RMAP_OINFO_SKIP_UPDATE;
2068c2ecf20Sopenharmony_ciextern const struct xfs_owner_info XFS_RMAP_OINFO_ANY_OWNER;
2078c2ecf20Sopenharmony_ciextern const struct xfs_owner_info XFS_RMAP_OINFO_FS;
2088c2ecf20Sopenharmony_ciextern const struct xfs_owner_info XFS_RMAP_OINFO_LOG;
2098c2ecf20Sopenharmony_ciextern const struct xfs_owner_info XFS_RMAP_OINFO_AG;
2108c2ecf20Sopenharmony_ciextern const struct xfs_owner_info XFS_RMAP_OINFO_INOBT;
2118c2ecf20Sopenharmony_ciextern const struct xfs_owner_info XFS_RMAP_OINFO_INODES;
2128c2ecf20Sopenharmony_ciextern const struct xfs_owner_info XFS_RMAP_OINFO_REFC;
2138c2ecf20Sopenharmony_ciextern const struct xfs_owner_info XFS_RMAP_OINFO_COW;
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_ci#endif	/* __XFS_RMAP_H__ */
216