xref: /openbmc/linux/fs/xfs/libxfs/xfs_refcount.h (revision 6396bb221514d2876fd6dc0aa2a1f240d99b37bb)
1 /*
2  * Copyright (C) 2016 Oracle.  All Rights Reserved.
3  *
4  * Author: Darrick J. Wong <darrick.wong@oracle.com>
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it would be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write the Free Software Foundation,
18  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301, USA.
19  */
20 #ifndef __XFS_REFCOUNT_H__
21 #define __XFS_REFCOUNT_H__
22 
23 extern int xfs_refcount_lookup_le(struct xfs_btree_cur *cur,
24 		xfs_agblock_t bno, int *stat);
25 extern int xfs_refcount_lookup_ge(struct xfs_btree_cur *cur,
26 		xfs_agblock_t bno, int *stat);
27 extern int xfs_refcount_lookup_eq(struct xfs_btree_cur *cur,
28 		xfs_agblock_t bno, int *stat);
29 extern int xfs_refcount_get_rec(struct xfs_btree_cur *cur,
30 		struct xfs_refcount_irec *irec, int *stat);
31 
32 enum xfs_refcount_intent_type {
33 	XFS_REFCOUNT_INCREASE = 1,
34 	XFS_REFCOUNT_DECREASE,
35 	XFS_REFCOUNT_ALLOC_COW,
36 	XFS_REFCOUNT_FREE_COW,
37 };
38 
39 struct xfs_refcount_intent {
40 	struct list_head			ri_list;
41 	enum xfs_refcount_intent_type		ri_type;
42 	xfs_fsblock_t				ri_startblock;
43 	xfs_extlen_t				ri_blockcount;
44 };
45 
46 extern int xfs_refcount_increase_extent(struct xfs_mount *mp,
47 		struct xfs_defer_ops *dfops, struct xfs_bmbt_irec *irec);
48 extern int xfs_refcount_decrease_extent(struct xfs_mount *mp,
49 		struct xfs_defer_ops *dfops, struct xfs_bmbt_irec *irec);
50 
51 extern void xfs_refcount_finish_one_cleanup(struct xfs_trans *tp,
52 		struct xfs_btree_cur *rcur, int error);
53 extern int xfs_refcount_finish_one(struct xfs_trans *tp,
54 		struct xfs_defer_ops *dfops, enum xfs_refcount_intent_type type,
55 		xfs_fsblock_t startblock, xfs_extlen_t blockcount,
56 		xfs_fsblock_t *new_fsb, xfs_extlen_t *new_len,
57 		struct xfs_btree_cur **pcur);
58 
59 extern int xfs_refcount_find_shared(struct xfs_btree_cur *cur,
60 		xfs_agblock_t agbno, xfs_extlen_t aglen, xfs_agblock_t *fbno,
61 		xfs_extlen_t *flen, bool find_end_of_shared);
62 
63 extern int xfs_refcount_alloc_cow_extent(struct xfs_mount *mp,
64 		struct xfs_defer_ops *dfops, xfs_fsblock_t fsb,
65 		xfs_extlen_t len);
66 extern int xfs_refcount_free_cow_extent(struct xfs_mount *mp,
67 		struct xfs_defer_ops *dfops, xfs_fsblock_t fsb,
68 		xfs_extlen_t len);
69 extern int xfs_refcount_recover_cow_leftovers(struct xfs_mount *mp,
70 		xfs_agnumber_t agno);
71 
72 /*
73  * While we're adjusting the refcounts records of an extent, we have
74  * to keep an eye on the number of extents we're dirtying -- run too
75  * many in a single transaction and we'll exceed the transaction's
76  * reservation and crash the fs.  Each record adds 12 bytes to the
77  * log (plus any key updates) so we'll conservatively assume 32 bytes
78  * per record.  We must also leave space for btree splits on both ends
79  * of the range and space for the CUD and a new CUI.
80  */
81 #define XFS_REFCOUNT_ITEM_OVERHEAD	32
82 
83 static inline xfs_fileoff_t xfs_refcount_max_unmap(int log_res)
84 {
85 	return (log_res * 3 / 4) / XFS_REFCOUNT_ITEM_OVERHEAD;
86 }
87 
88 extern int xfs_refcount_has_record(struct xfs_btree_cur *cur,
89 		xfs_agblock_t bno, xfs_extlen_t len, bool *exists);
90 union xfs_btree_rec;
91 extern void xfs_refcount_btrec_to_irec(union xfs_btree_rec *rec,
92 		struct xfs_refcount_irec *irec);
93 extern int xfs_refcount_insert(struct xfs_btree_cur *cur,
94 		struct xfs_refcount_irec *irec, int *stat);
95 
96 #endif	/* __XFS_REFCOUNT_H__ */
97