142 lines · c
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 9struct xfs_trans;10struct xfs_mount;11struct xfs_perag;12struct xfs_btree_cur;13struct xfs_bmbt_irec;14struct xfs_refcount_irec;15 16extern int xfs_refcount_lookup_le(struct xfs_btree_cur *cur,17 enum xfs_refc_domain domain, xfs_agblock_t bno, int *stat);18extern int xfs_refcount_lookup_ge(struct xfs_btree_cur *cur,19 enum xfs_refc_domain domain, xfs_agblock_t bno, int *stat);20extern int xfs_refcount_lookup_eq(struct xfs_btree_cur *cur,21 enum xfs_refc_domain domain, xfs_agblock_t bno, int *stat);22extern int xfs_refcount_get_rec(struct xfs_btree_cur *cur,23 struct xfs_refcount_irec *irec, int *stat);24 25static inline uint32_t26xfs_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 range34 * query functions (which set rc_domain == -1U), so we check that the35 * 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 44enum 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#define XFS_REFCOUNT_INTENT_STRINGS \52 { XFS_REFCOUNT_INCREASE, "incr" }, \53 { XFS_REFCOUNT_DECREASE, "decr" }, \54 { XFS_REFCOUNT_ALLOC_COW, "alloc_cow" }, \55 { XFS_REFCOUNT_FREE_COW, "free_cow" }56 57struct xfs_refcount_intent {58 struct list_head ri_list;59 struct xfs_perag *ri_pag;60 enum xfs_refcount_intent_type ri_type;61 xfs_extlen_t ri_blockcount;62 xfs_fsblock_t ri_startblock;63};64 65/* Check that the refcount is appropriate for the record domain. */66static inline bool67xfs_refcount_check_domain(68 const struct xfs_refcount_irec *irec)69{70 if (irec->rc_domain == XFS_REFC_DOMAIN_COW && irec->rc_refcount != 1)71 return false;72 if (irec->rc_domain == XFS_REFC_DOMAIN_SHARED && irec->rc_refcount < 2)73 return false;74 return true;75}76 77void xfs_refcount_increase_extent(struct xfs_trans *tp,78 struct xfs_bmbt_irec *irec);79void xfs_refcount_decrease_extent(struct xfs_trans *tp,80 struct xfs_bmbt_irec *irec);81 82extern int xfs_refcount_finish_one(struct xfs_trans *tp,83 struct xfs_refcount_intent *ri, struct xfs_btree_cur **pcur);84 85extern int xfs_refcount_find_shared(struct xfs_btree_cur *cur,86 xfs_agblock_t agbno, xfs_extlen_t aglen, xfs_agblock_t *fbno,87 xfs_extlen_t *flen, bool find_end_of_shared);88 89void xfs_refcount_alloc_cow_extent(struct xfs_trans *tp, xfs_fsblock_t fsb,90 xfs_extlen_t len);91void xfs_refcount_free_cow_extent(struct xfs_trans *tp, xfs_fsblock_t fsb,92 xfs_extlen_t len);93extern int xfs_refcount_recover_cow_leftovers(struct xfs_mount *mp,94 struct xfs_perag *pag);95 96/*97 * While we're adjusting the refcounts records of an extent, we have98 * to keep an eye on the number of extents we're dirtying -- run too99 * many in a single transaction and we'll exceed the transaction's100 * reservation and crash the fs. Each record adds 12 bytes to the101 * log (plus any key updates) so we'll conservatively assume 32 bytes102 * per record. We must also leave space for btree splits on both ends103 * of the range and space for the CUD and a new CUI.104 *105 * Each EFI that we attach to the transaction is assumed to consume ~32 bytes.106 * This is a low estimate for an EFI tracking a single extent (16 bytes for the107 * EFI header, 16 for the extent, and 12 for the xlog op header), but the108 * estimate is acceptable if there's more than one extent being freed.109 * In the worst case of freeing every other block during a refcount decrease110 * operation, we amortize the space used for one EFI log item across 16111 * extents.112 */113#define XFS_REFCOUNT_ITEM_OVERHEAD 32114 115extern int xfs_refcount_has_records(struct xfs_btree_cur *cur,116 enum xfs_refc_domain domain, xfs_agblock_t bno,117 xfs_extlen_t len, enum xbtree_recpacking *outcome);118union xfs_btree_rec;119extern void xfs_refcount_btrec_to_irec(const union xfs_btree_rec *rec,120 struct xfs_refcount_irec *irec);121xfs_failaddr_t xfs_refcount_check_irec(struct xfs_perag *pag,122 const struct xfs_refcount_irec *irec);123extern int xfs_refcount_insert(struct xfs_btree_cur *cur,124 struct xfs_refcount_irec *irec, int *stat);125 126extern struct kmem_cache *xfs_refcount_intent_cache;127 128int __init xfs_refcount_intent_init_cache(void);129void xfs_refcount_intent_destroy_cache(void);130 131typedef int (*xfs_refcount_query_range_fn)(132 struct xfs_btree_cur *cur,133 const struct xfs_refcount_irec *rec,134 void *priv);135 136int xfs_refcount_query_range(struct xfs_btree_cur *cur,137 const struct xfs_refcount_irec *low_rec,138 const struct xfs_refcount_irec *high_rec,139 xfs_refcount_query_range_fn fn, void *priv);140 141#endif /* __XFS_REFCOUNT_H__ */142