xref: /OK3568_Linux_fs/kernel/fs/xfs/libxfs/xfs_refcount.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_REFCOUNT_H__
7*4882a593Smuzhiyun #define __XFS_REFCOUNT_H__
8*4882a593Smuzhiyun 
9*4882a593Smuzhiyun extern int xfs_refcount_lookup_le(struct xfs_btree_cur *cur,
10*4882a593Smuzhiyun 		xfs_agblock_t bno, int *stat);
11*4882a593Smuzhiyun extern int xfs_refcount_lookup_ge(struct xfs_btree_cur *cur,
12*4882a593Smuzhiyun 		xfs_agblock_t bno, int *stat);
13*4882a593Smuzhiyun extern int xfs_refcount_lookup_eq(struct xfs_btree_cur *cur,
14*4882a593Smuzhiyun 		xfs_agblock_t bno, int *stat);
15*4882a593Smuzhiyun extern int xfs_refcount_get_rec(struct xfs_btree_cur *cur,
16*4882a593Smuzhiyun 		struct xfs_refcount_irec *irec, int *stat);
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun enum xfs_refcount_intent_type {
19*4882a593Smuzhiyun 	XFS_REFCOUNT_INCREASE = 1,
20*4882a593Smuzhiyun 	XFS_REFCOUNT_DECREASE,
21*4882a593Smuzhiyun 	XFS_REFCOUNT_ALLOC_COW,
22*4882a593Smuzhiyun 	XFS_REFCOUNT_FREE_COW,
23*4882a593Smuzhiyun };
24*4882a593Smuzhiyun 
25*4882a593Smuzhiyun struct xfs_refcount_intent {
26*4882a593Smuzhiyun 	struct list_head			ri_list;
27*4882a593Smuzhiyun 	enum xfs_refcount_intent_type		ri_type;
28*4882a593Smuzhiyun 	xfs_fsblock_t				ri_startblock;
29*4882a593Smuzhiyun 	xfs_extlen_t				ri_blockcount;
30*4882a593Smuzhiyun };
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun void xfs_refcount_increase_extent(struct xfs_trans *tp,
33*4882a593Smuzhiyun 		struct xfs_bmbt_irec *irec);
34*4882a593Smuzhiyun void xfs_refcount_decrease_extent(struct xfs_trans *tp,
35*4882a593Smuzhiyun 		struct xfs_bmbt_irec *irec);
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun extern void xfs_refcount_finish_one_cleanup(struct xfs_trans *tp,
38*4882a593Smuzhiyun 		struct xfs_btree_cur *rcur, int error);
39*4882a593Smuzhiyun extern int xfs_refcount_finish_one(struct xfs_trans *tp,
40*4882a593Smuzhiyun 		enum xfs_refcount_intent_type type, xfs_fsblock_t startblock,
41*4882a593Smuzhiyun 		xfs_extlen_t blockcount, xfs_fsblock_t *new_fsb,
42*4882a593Smuzhiyun 		xfs_extlen_t *new_len, struct xfs_btree_cur **pcur);
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun extern int xfs_refcount_find_shared(struct xfs_btree_cur *cur,
45*4882a593Smuzhiyun 		xfs_agblock_t agbno, xfs_extlen_t aglen, xfs_agblock_t *fbno,
46*4882a593Smuzhiyun 		xfs_extlen_t *flen, bool find_end_of_shared);
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun void xfs_refcount_alloc_cow_extent(struct xfs_trans *tp, xfs_fsblock_t fsb,
49*4882a593Smuzhiyun 		xfs_extlen_t len);
50*4882a593Smuzhiyun void xfs_refcount_free_cow_extent(struct xfs_trans *tp, xfs_fsblock_t fsb,
51*4882a593Smuzhiyun 		xfs_extlen_t len);
52*4882a593Smuzhiyun extern int xfs_refcount_recover_cow_leftovers(struct xfs_mount *mp,
53*4882a593Smuzhiyun 		xfs_agnumber_t agno);
54*4882a593Smuzhiyun 
55*4882a593Smuzhiyun /*
56*4882a593Smuzhiyun  * While we're adjusting the refcounts records of an extent, we have
57*4882a593Smuzhiyun  * to keep an eye on the number of extents we're dirtying -- run too
58*4882a593Smuzhiyun  * many in a single transaction and we'll exceed the transaction's
59*4882a593Smuzhiyun  * reservation and crash the fs.  Each record adds 12 bytes to the
60*4882a593Smuzhiyun  * log (plus any key updates) so we'll conservatively assume 32 bytes
61*4882a593Smuzhiyun  * per record.  We must also leave space for btree splits on both ends
62*4882a593Smuzhiyun  * of the range and space for the CUD and a new CUI.
63*4882a593Smuzhiyun  */
64*4882a593Smuzhiyun #define XFS_REFCOUNT_ITEM_OVERHEAD	32
65*4882a593Smuzhiyun 
xfs_refcount_max_unmap(int log_res)66*4882a593Smuzhiyun static inline xfs_fileoff_t xfs_refcount_max_unmap(int log_res)
67*4882a593Smuzhiyun {
68*4882a593Smuzhiyun 	return (log_res * 3 / 4) / XFS_REFCOUNT_ITEM_OVERHEAD;
69*4882a593Smuzhiyun }
70*4882a593Smuzhiyun 
71*4882a593Smuzhiyun extern int xfs_refcount_has_record(struct xfs_btree_cur *cur,
72*4882a593Smuzhiyun 		xfs_agblock_t bno, xfs_extlen_t len, bool *exists);
73*4882a593Smuzhiyun union xfs_btree_rec;
74*4882a593Smuzhiyun extern void xfs_refcount_btrec_to_irec(union xfs_btree_rec *rec,
75*4882a593Smuzhiyun 		struct xfs_refcount_irec *irec);
76*4882a593Smuzhiyun extern int xfs_refcount_insert(struct xfs_btree_cur *cur,
77*4882a593Smuzhiyun 		struct xfs_refcount_irec *irec, int *stat);
78*4882a593Smuzhiyun 
79*4882a593Smuzhiyun #endif	/* __XFS_REFCOUNT_H__ */
80