xref: /openbmc/linux/fs/xfs/libxfs/xfs_refcount.h (revision 01ab991f)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2016 Oracle.  All Rights Reserved.
4  * Author: Darrick J. Wong <darrick.wong@oracle.com>
5  */
6 #ifndef __XFS_REFCOUNT_H__
7 #define __XFS_REFCOUNT_H__
8 
9 struct xfs_trans;
10 struct xfs_mount;
11 struct xfs_perag;
12 struct xfs_btree_cur;
13 struct xfs_bmbt_irec;
14 struct xfs_refcount_irec;
15 
16 extern int xfs_refcount_lookup_le(struct xfs_btree_cur *cur,
17 		enum xfs_refc_domain domain, xfs_agblock_t bno, int *stat);
18 extern int xfs_refcount_lookup_ge(struct xfs_btree_cur *cur,
19 		enum xfs_refc_domain domain, xfs_agblock_t bno, int *stat);
20 extern int xfs_refcount_lookup_eq(struct xfs_btree_cur *cur,
21 		enum xfs_refc_domain domain, xfs_agblock_t bno, int *stat);
22 extern int xfs_refcount_get_rec(struct xfs_btree_cur *cur,
23 		struct xfs_refcount_irec *irec, int *stat);
24 
25 static inline uint32_t
26 xfs_refcount_encode_startblock(
27 	xfs_agblock_t		startblock,
28 	enum xfs_refc_domain	domain)
29 {
30 	uint32_t		start;
31 
32 	/*
33 	 * low level btree operations need to handle the generic btree range
34 	 * query functions (which set rc_domain == -1U), so we check that the
35 	 * domain is /not/ shared.
36 	 */
37 	start = startblock & ~XFS_REFC_COWFLAG;
38 	if (domain != XFS_REFC_DOMAIN_SHARED)
39 		start |= XFS_REFC_COWFLAG;
40 
41 	return start;
42 }
43 
44 enum xfs_refcount_intent_type {
45 	XFS_REFCOUNT_INCREASE = 1,
46 	XFS_REFCOUNT_DECREASE,
47 	XFS_REFCOUNT_ALLOC_COW,
48 	XFS_REFCOUNT_FREE_COW,
49 };
50 
51 struct xfs_refcount_intent {
52 	struct list_head			ri_list;
53 	enum xfs_refcount_intent_type		ri_type;
54 	xfs_extlen_t				ri_blockcount;
55 	xfs_fsblock_t				ri_startblock;
56 };
57 
58 /* Check that the refcount is appropriate for the record domain. */
59 static inline bool
60 xfs_refcount_check_domain(
61 	const struct xfs_refcount_irec	*irec)
62 {
63 	if (irec->rc_domain == XFS_REFC_DOMAIN_COW && irec->rc_refcount != 1)
64 		return false;
65 	if (irec->rc_domain == XFS_REFC_DOMAIN_SHARED && irec->rc_refcount < 2)
66 		return false;
67 	return true;
68 }
69 
70 void xfs_refcount_increase_extent(struct xfs_trans *tp,
71 		struct xfs_bmbt_irec *irec);
72 void xfs_refcount_decrease_extent(struct xfs_trans *tp,
73 		struct xfs_bmbt_irec *irec);
74 
75 extern void xfs_refcount_finish_one_cleanup(struct xfs_trans *tp,
76 		struct xfs_btree_cur *rcur, int error);
77 extern int xfs_refcount_finish_one(struct xfs_trans *tp,
78 		enum xfs_refcount_intent_type type, xfs_fsblock_t startblock,
79 		xfs_extlen_t blockcount, xfs_fsblock_t *new_fsb,
80 		xfs_extlen_t *new_len, struct xfs_btree_cur **pcur);
81 
82 extern int xfs_refcount_find_shared(struct xfs_btree_cur *cur,
83 		xfs_agblock_t agbno, xfs_extlen_t aglen, xfs_agblock_t *fbno,
84 		xfs_extlen_t *flen, bool find_end_of_shared);
85 
86 void xfs_refcount_alloc_cow_extent(struct xfs_trans *tp, xfs_fsblock_t fsb,
87 		xfs_extlen_t len);
88 void xfs_refcount_free_cow_extent(struct xfs_trans *tp, xfs_fsblock_t fsb,
89 		xfs_extlen_t len);
90 extern int xfs_refcount_recover_cow_leftovers(struct xfs_mount *mp,
91 		struct xfs_perag *pag);
92 
93 /*
94  * While we're adjusting the refcounts records of an extent, we have
95  * to keep an eye on the number of extents we're dirtying -- run too
96  * many in a single transaction and we'll exceed the transaction's
97  * reservation and crash the fs.  Each record adds 12 bytes to the
98  * log (plus any key updates) so we'll conservatively assume 32 bytes
99  * per record.  We must also leave space for btree splits on both ends
100  * of the range and space for the CUD and a new CUI.
101  *
102  * Each EFI that we attach to the transaction is assumed to consume ~32 bytes.
103  * This is a low estimate for an EFI tracking a single extent (16 bytes for the
104  * EFI header, 16 for the extent, and 12 for the xlog op header), but the
105  * estimate is acceptable if there's more than one extent being freed.
106  * In the worst case of freeing every other block during a refcount decrease
107  * operation, we amortize the space used for one EFI log item across 16
108  * extents.
109  */
110 #define XFS_REFCOUNT_ITEM_OVERHEAD	32
111 
112 extern int xfs_refcount_has_record(struct xfs_btree_cur *cur,
113 		enum xfs_refc_domain domain, xfs_agblock_t bno,
114 		xfs_extlen_t len, bool *exists);
115 union xfs_btree_rec;
116 extern void xfs_refcount_btrec_to_irec(const union xfs_btree_rec *rec,
117 		struct xfs_refcount_irec *irec);
118 extern int xfs_refcount_insert(struct xfs_btree_cur *cur,
119 		struct xfs_refcount_irec *irec, int *stat);
120 
121 extern struct kmem_cache	*xfs_refcount_intent_cache;
122 
123 int __init xfs_refcount_intent_init_cache(void);
124 void xfs_refcount_intent_destroy_cache(void);
125 
126 #endif	/* __XFS_REFCOUNT_H__ */
127