xfs_refcount.h 4.3 KB

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