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_REFCOUNT_H__
78c2ecf20Sopenharmony_ci#define __XFS_REFCOUNT_H__
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ciextern int xfs_refcount_lookup_le(struct xfs_btree_cur *cur,
108c2ecf20Sopenharmony_ci		xfs_agblock_t bno, int *stat);
118c2ecf20Sopenharmony_ciextern int xfs_refcount_lookup_ge(struct xfs_btree_cur *cur,
128c2ecf20Sopenharmony_ci		xfs_agblock_t bno, int *stat);
138c2ecf20Sopenharmony_ciextern int xfs_refcount_lookup_eq(struct xfs_btree_cur *cur,
148c2ecf20Sopenharmony_ci		xfs_agblock_t bno, int *stat);
158c2ecf20Sopenharmony_ciextern int xfs_refcount_get_rec(struct xfs_btree_cur *cur,
168c2ecf20Sopenharmony_ci		struct xfs_refcount_irec *irec, int *stat);
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_cienum xfs_refcount_intent_type {
198c2ecf20Sopenharmony_ci	XFS_REFCOUNT_INCREASE = 1,
208c2ecf20Sopenharmony_ci	XFS_REFCOUNT_DECREASE,
218c2ecf20Sopenharmony_ci	XFS_REFCOUNT_ALLOC_COW,
228c2ecf20Sopenharmony_ci	XFS_REFCOUNT_FREE_COW,
238c2ecf20Sopenharmony_ci};
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_cistruct xfs_refcount_intent {
268c2ecf20Sopenharmony_ci	struct list_head			ri_list;
278c2ecf20Sopenharmony_ci	enum xfs_refcount_intent_type		ri_type;
288c2ecf20Sopenharmony_ci	xfs_fsblock_t				ri_startblock;
298c2ecf20Sopenharmony_ci	xfs_extlen_t				ri_blockcount;
308c2ecf20Sopenharmony_ci};
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_civoid xfs_refcount_increase_extent(struct xfs_trans *tp,
338c2ecf20Sopenharmony_ci		struct xfs_bmbt_irec *irec);
348c2ecf20Sopenharmony_civoid xfs_refcount_decrease_extent(struct xfs_trans *tp,
358c2ecf20Sopenharmony_ci		struct xfs_bmbt_irec *irec);
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ciextern void xfs_refcount_finish_one_cleanup(struct xfs_trans *tp,
388c2ecf20Sopenharmony_ci		struct xfs_btree_cur *rcur, int error);
398c2ecf20Sopenharmony_ciextern int xfs_refcount_finish_one(struct xfs_trans *tp,
408c2ecf20Sopenharmony_ci		enum xfs_refcount_intent_type type, xfs_fsblock_t startblock,
418c2ecf20Sopenharmony_ci		xfs_extlen_t blockcount, xfs_fsblock_t *new_fsb,
428c2ecf20Sopenharmony_ci		xfs_extlen_t *new_len, struct xfs_btree_cur **pcur);
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ciextern int xfs_refcount_find_shared(struct xfs_btree_cur *cur,
458c2ecf20Sopenharmony_ci		xfs_agblock_t agbno, xfs_extlen_t aglen, xfs_agblock_t *fbno,
468c2ecf20Sopenharmony_ci		xfs_extlen_t *flen, bool find_end_of_shared);
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_civoid xfs_refcount_alloc_cow_extent(struct xfs_trans *tp, xfs_fsblock_t fsb,
498c2ecf20Sopenharmony_ci		xfs_extlen_t len);
508c2ecf20Sopenharmony_civoid xfs_refcount_free_cow_extent(struct xfs_trans *tp, xfs_fsblock_t fsb,
518c2ecf20Sopenharmony_ci		xfs_extlen_t len);
528c2ecf20Sopenharmony_ciextern int xfs_refcount_recover_cow_leftovers(struct xfs_mount *mp,
538c2ecf20Sopenharmony_ci		xfs_agnumber_t agno);
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci/*
568c2ecf20Sopenharmony_ci * While we're adjusting the refcounts records of an extent, we have
578c2ecf20Sopenharmony_ci * to keep an eye on the number of extents we're dirtying -- run too
588c2ecf20Sopenharmony_ci * many in a single transaction and we'll exceed the transaction's
598c2ecf20Sopenharmony_ci * reservation and crash the fs.  Each record adds 12 bytes to the
608c2ecf20Sopenharmony_ci * log (plus any key updates) so we'll conservatively assume 32 bytes
618c2ecf20Sopenharmony_ci * per record.  We must also leave space for btree splits on both ends
628c2ecf20Sopenharmony_ci * of the range and space for the CUD and a new CUI.
638c2ecf20Sopenharmony_ci */
648c2ecf20Sopenharmony_ci#define XFS_REFCOUNT_ITEM_OVERHEAD	32
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_cistatic inline xfs_fileoff_t xfs_refcount_max_unmap(int log_res)
678c2ecf20Sopenharmony_ci{
688c2ecf20Sopenharmony_ci	return (log_res * 3 / 4) / XFS_REFCOUNT_ITEM_OVERHEAD;
698c2ecf20Sopenharmony_ci}
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ciextern int xfs_refcount_has_record(struct xfs_btree_cur *cur,
728c2ecf20Sopenharmony_ci		xfs_agblock_t bno, xfs_extlen_t len, bool *exists);
738c2ecf20Sopenharmony_ciunion xfs_btree_rec;
748c2ecf20Sopenharmony_ciextern void xfs_refcount_btrec_to_irec(union xfs_btree_rec *rec,
758c2ecf20Sopenharmony_ci		struct xfs_refcount_irec *irec);
768c2ecf20Sopenharmony_ciextern int xfs_refcount_insert(struct xfs_btree_cur *cur,
778c2ecf20Sopenharmony_ci		struct xfs_refcount_irec *irec, int *stat);
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci#endif	/* __XFS_REFCOUNT_H__ */
80