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