xfs_ag.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright (c) 2018 Red Hat, Inc.
  4. * All rights reserved.
  5. */
  6. #ifndef __LIBXFS_AG_H
  7. #define __LIBXFS_AG_H 1
  8. struct xfs_mount;
  9. struct xfs_trans;
  10. struct xfs_perag;
  11. /*
  12. * Per-ag infrastructure
  13. */
  14. /* per-AG block reservation data structures*/
  15. struct xfs_ag_resv {
  16. /* number of blocks originally reserved here */
  17. xfs_extlen_t ar_orig_reserved;
  18. /* number of blocks reserved here */
  19. xfs_extlen_t ar_reserved;
  20. /* number of blocks originally asked for */
  21. xfs_extlen_t ar_asked;
  22. };
  23. /*
  24. * Per-ag incore structure, copies of information in agf and agi, to improve the
  25. * performance of allocation group selection.
  26. */
  27. struct xfs_perag {
  28. struct xfs_mount *pag_mount; /* owner filesystem */
  29. xfs_agnumber_t pag_agno; /* AG this structure belongs to */
  30. atomic_t pag_ref; /* perag reference count */
  31. char pagf_init; /* this agf's entry is initialized */
  32. char pagi_init; /* this agi's entry is initialized */
  33. char pagf_metadata; /* the agf is preferred to be metadata */
  34. char pagi_inodeok; /* The agi is ok for inodes */
  35. uint8_t pagf_levels[XFS_BTNUM_AGF];
  36. /* # of levels in bno & cnt btree */
  37. bool pagf_agflreset; /* agfl requires reset before use */
  38. uint32_t pagf_flcount; /* count of blocks in freelist */
  39. xfs_extlen_t pagf_freeblks; /* total free blocks */
  40. xfs_extlen_t pagf_longest; /* longest free space */
  41. uint32_t pagf_btreeblks; /* # of blocks held in AGF btrees */
  42. xfs_agino_t pagi_freecount; /* number of free inodes */
  43. xfs_agino_t pagi_count; /* number of allocated inodes */
  44. /*
  45. * Inode allocation search lookup optimisation.
  46. * If the pagino matches, the search for new inodes
  47. * doesn't need to search the near ones again straight away
  48. */
  49. xfs_agino_t pagl_pagino;
  50. xfs_agino_t pagl_leftrec;
  51. xfs_agino_t pagl_rightrec;
  52. int pagb_count; /* pagb slots in use */
  53. uint8_t pagf_refcount_level; /* recount btree height */
  54. /* Blocks reserved for all kinds of metadata. */
  55. struct xfs_ag_resv pag_meta_resv;
  56. /* Blocks reserved for the reverse mapping btree. */
  57. struct xfs_ag_resv pag_rmapbt_resv;
  58. /* for rcu-safe freeing */
  59. struct rcu_head rcu_head;
  60. /* Precalculated geometry info */
  61. xfs_agblock_t block_count;
  62. xfs_agblock_t min_block;
  63. xfs_agino_t agino_min;
  64. xfs_agino_t agino_max;
  65. #ifdef __KERNEL__
  66. /* -- kernel only structures below this line -- */
  67. /*
  68. * Bitsets of per-ag metadata that have been checked and/or are sick.
  69. * Callers should hold pag_state_lock before accessing this field.
  70. */
  71. uint16_t pag_checked;
  72. uint16_t pag_sick;
  73. spinlock_t pag_state_lock;
  74. spinlock_t pagb_lock; /* lock for pagb_tree */
  75. struct rb_root pagb_tree; /* ordered tree of busy extents */
  76. unsigned int pagb_gen; /* generation count for pagb_tree */
  77. wait_queue_head_t pagb_wait; /* woken when pagb_gen changes */
  78. atomic_t pagf_fstrms; /* # of filestreams active in this AG */
  79. spinlock_t pag_ici_lock; /* incore inode cache lock */
  80. struct radix_tree_root pag_ici_root; /* incore inode cache root */
  81. int pag_ici_reclaimable; /* reclaimable inodes */
  82. unsigned long pag_ici_reclaim_cursor; /* reclaim restart point */
  83. /* buffer cache index */
  84. spinlock_t pag_buf_lock; /* lock for pag_buf_hash */
  85. struct rhashtable pag_buf_hash;
  86. /* background prealloc block trimming */
  87. struct delayed_work pag_blockgc_work;
  88. #endif /* __KERNEL__ */
  89. };
  90. int xfs_initialize_perag(struct xfs_mount *mp, xfs_agnumber_t agcount,
  91. xfs_rfsblock_t dcount, xfs_agnumber_t *maxagi);
  92. int xfs_initialize_perag_data(struct xfs_mount *mp, xfs_agnumber_t agno);
  93. void xfs_free_perag(struct xfs_mount *mp);
  94. struct xfs_perag *xfs_perag_get(struct xfs_mount *mp, xfs_agnumber_t agno);
  95. struct xfs_perag *xfs_perag_get_tag(struct xfs_mount *mp, xfs_agnumber_t agno,
  96. unsigned int tag);
  97. void xfs_perag_put(struct xfs_perag *pag);
  98. /*
  99. * Per-ag geometry infomation and validation
  100. */
  101. xfs_agblock_t xfs_ag_block_count(struct xfs_mount *mp, xfs_agnumber_t agno);
  102. void xfs_agino_range(struct xfs_mount *mp, xfs_agnumber_t agno,
  103. xfs_agino_t *first, xfs_agino_t *last);
  104. static inline bool
  105. xfs_verify_agbno(struct xfs_perag *pag, xfs_agblock_t agbno)
  106. {
  107. if (agbno >= pag->block_count)
  108. return false;
  109. if (agbno <= pag->min_block)
  110. return false;
  111. return true;
  112. }
  113. static inline bool
  114. xfs_verify_agbext(
  115. struct xfs_perag *pag,
  116. xfs_agblock_t agbno,
  117. xfs_agblock_t len)
  118. {
  119. if (agbno + len <= agbno)
  120. return false;
  121. if (!xfs_verify_agbno(pag, agbno))
  122. return false;
  123. return xfs_verify_agbno(pag, agbno + len - 1);
  124. }
  125. /*
  126. * Verify that an AG inode number pointer neither points outside the AG
  127. * nor points at static metadata.
  128. */
  129. static inline bool
  130. xfs_verify_agino(struct xfs_perag *pag, xfs_agino_t agino)
  131. {
  132. if (agino < pag->agino_min)
  133. return false;
  134. if (agino > pag->agino_max)
  135. return false;
  136. return true;
  137. }
  138. /*
  139. * Verify that an AG inode number pointer neither points outside the AG
  140. * nor points at static metadata, or is NULLAGINO.
  141. */
  142. static inline bool
  143. xfs_verify_agino_or_null(struct xfs_perag *pag, xfs_agino_t agino)
  144. {
  145. if (agino == NULLAGINO)
  146. return true;
  147. return xfs_verify_agino(pag, agino);
  148. }
  149. static inline bool
  150. xfs_ag_contains_log(struct xfs_mount *mp, xfs_agnumber_t agno)
  151. {
  152. return mp->m_sb.sb_logstart > 0 &&
  153. agno == XFS_FSB_TO_AGNO(mp, mp->m_sb.sb_logstart);
  154. }
  155. /*
  156. * Perag iteration APIs
  157. */
  158. static inline struct xfs_perag *
  159. xfs_perag_next(
  160. struct xfs_perag *pag,
  161. xfs_agnumber_t *agno,
  162. xfs_agnumber_t end_agno)
  163. {
  164. struct xfs_mount *mp = pag->pag_mount;
  165. *agno = pag->pag_agno + 1;
  166. xfs_perag_put(pag);
  167. if (*agno > end_agno)
  168. return NULL;
  169. return xfs_perag_get(mp, *agno);
  170. }
  171. #define for_each_perag_range(mp, agno, end_agno, pag) \
  172. for ((pag) = xfs_perag_get((mp), (agno)); \
  173. (pag) != NULL; \
  174. (pag) = xfs_perag_next((pag), &(agno), (end_agno)))
  175. #define for_each_perag_from(mp, agno, pag) \
  176. for_each_perag_range((mp), (agno), (mp)->m_sb.sb_agcount - 1, (pag))
  177. #define for_each_perag(mp, agno, pag) \
  178. (agno) = 0; \
  179. for_each_perag_from((mp), (agno), (pag))
  180. #define for_each_perag_tag(mp, agno, pag, tag) \
  181. for ((agno) = 0, (pag) = xfs_perag_get_tag((mp), 0, (tag)); \
  182. (pag) != NULL; \
  183. (agno) = (pag)->pag_agno + 1, \
  184. xfs_perag_put(pag), \
  185. (pag) = xfs_perag_get_tag((mp), (agno), (tag)))
  186. struct aghdr_init_data {
  187. /* per ag data */
  188. xfs_agblock_t agno; /* ag to init */
  189. xfs_extlen_t agsize; /* new AG size */
  190. struct list_head buffer_list; /* buffer writeback list */
  191. xfs_rfsblock_t nfree; /* cumulative new free space */
  192. /* per header data */
  193. xfs_daddr_t daddr; /* header location */
  194. size_t numblks; /* size of header */
  195. xfs_btnum_t type; /* type of btree root block */
  196. };
  197. int xfs_ag_init_headers(struct xfs_mount *mp, struct aghdr_init_data *id);
  198. int xfs_ag_shrink_space(struct xfs_perag *pag, struct xfs_trans **tpp,
  199. xfs_extlen_t delta);
  200. int xfs_ag_extend_space(struct xfs_perag *pag, struct xfs_trans *tp,
  201. xfs_extlen_t len);
  202. int xfs_ag_get_geometry(struct xfs_perag *pag, struct xfs_ag_geometry *ageo);
  203. #endif /* __LIBXFS_AG_H */