xfs_alloc.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
  4. * All Rights Reserved.
  5. */
  6. #ifndef __XFS_ALLOC_H__
  7. #define __XFS_ALLOC_H__
  8. struct xfs_buf;
  9. struct xfs_btree_cur;
  10. struct xfs_mount;
  11. struct xfs_perag;
  12. struct xfs_trans;
  13. extern struct workqueue_struct *xfs_alloc_wq;
  14. unsigned int xfs_agfl_size(struct xfs_mount *mp);
  15. /*
  16. * Flags for xfs_alloc_fix_freelist.
  17. */
  18. #define XFS_ALLOC_FLAG_TRYLOCK (1U << 0) /* use trylock for buffer locking */
  19. #define XFS_ALLOC_FLAG_FREEING (1U << 1) /* indicate caller is freeing extents*/
  20. #define XFS_ALLOC_FLAG_NORMAP (1U << 2) /* don't modify the rmapbt */
  21. #define XFS_ALLOC_FLAG_NOSHRINK (1U << 3) /* don't shrink the freelist */
  22. #define XFS_ALLOC_FLAG_CHECK (1U << 4) /* test only, don't modify args */
  23. #define XFS_ALLOC_FLAG_TRYFLUSH (1U << 5) /* don't wait in busy extent flush */
  24. /*
  25. * Argument structure for xfs_alloc routines.
  26. * This is turned into a structure to avoid having 20 arguments passed
  27. * down several levels of the stack.
  28. */
  29. typedef struct xfs_alloc_arg {
  30. struct xfs_trans *tp; /* transaction pointer */
  31. struct xfs_mount *mp; /* file system mount point */
  32. struct xfs_buf *agbp; /* buffer for a.g. freelist header */
  33. struct xfs_perag *pag; /* per-ag struct for this agno */
  34. xfs_fsblock_t fsbno; /* file system block number */
  35. xfs_agnumber_t agno; /* allocation group number */
  36. xfs_agblock_t agbno; /* allocation group-relative block # */
  37. xfs_extlen_t minlen; /* minimum size of extent */
  38. xfs_extlen_t maxlen; /* maximum size of extent */
  39. xfs_extlen_t mod; /* mod value for extent size */
  40. xfs_extlen_t prod; /* prod value for extent size */
  41. xfs_extlen_t minleft; /* min blocks must be left after us */
  42. xfs_extlen_t total; /* total blocks needed in xaction */
  43. xfs_extlen_t alignment; /* align answer to multiple of this */
  44. xfs_extlen_t minalignslop; /* slop for minlen+alignment calcs */
  45. xfs_agblock_t min_agbno; /* set an agbno range for NEAR allocs */
  46. xfs_agblock_t max_agbno; /* ... */
  47. xfs_extlen_t len; /* output: actual size of extent */
  48. int datatype; /* mask defining data type treatment */
  49. char wasdel; /* set if allocation was prev delayed */
  50. char wasfromfl; /* set if allocation is from freelist */
  51. bool alloc_minlen_only; /* allocate exact minlen extent */
  52. struct xfs_owner_info oinfo; /* owner of blocks being allocated */
  53. enum xfs_ag_resv_type resv; /* block reservation to use */
  54. } xfs_alloc_arg_t;
  55. /*
  56. * Defines for datatype
  57. */
  58. #define XFS_ALLOC_USERDATA (1 << 0)/* allocation is for user data*/
  59. #define XFS_ALLOC_INITIAL_USER_DATA (1 << 1)/* special case start of file */
  60. #define XFS_ALLOC_NOBUSY (1 << 2)/* Busy extents not allowed */
  61. /* freespace limit calculations */
  62. unsigned int xfs_alloc_set_aside(struct xfs_mount *mp);
  63. unsigned int xfs_alloc_ag_max_usable(struct xfs_mount *mp);
  64. xfs_extlen_t xfs_alloc_longest_free_extent(struct xfs_perag *pag,
  65. xfs_extlen_t need, xfs_extlen_t reserved);
  66. unsigned int xfs_alloc_min_freelist(struct xfs_mount *mp,
  67. struct xfs_perag *pag);
  68. int xfs_alloc_get_freelist(struct xfs_perag *pag, struct xfs_trans *tp,
  69. struct xfs_buf *agfbp, xfs_agblock_t *bnop, int btreeblk);
  70. int xfs_alloc_put_freelist(struct xfs_perag *pag, struct xfs_trans *tp,
  71. struct xfs_buf *agfbp, struct xfs_buf *agflbp,
  72. xfs_agblock_t bno, int btreeblk);
  73. int xfs_free_ag_extent(struct xfs_trans *tp, struct xfs_buf *agbp,
  74. xfs_agnumber_t agno, xfs_agblock_t bno,
  75. xfs_extlen_t len, const struct xfs_owner_info *oinfo,
  76. enum xfs_ag_resv_type type);
  77. /*
  78. * Compute and fill in value of m_alloc_maxlevels.
  79. */
  80. void
  81. xfs_alloc_compute_maxlevels(
  82. struct xfs_mount *mp); /* file system mount structure */
  83. /*
  84. * Log the given fields from the agf structure.
  85. */
  86. void
  87. xfs_alloc_log_agf(
  88. struct xfs_trans *tp, /* transaction pointer */
  89. struct xfs_buf *bp, /* buffer for a.g. freelist header */
  90. uint32_t fields);/* mask of fields to be logged (XFS_AGF_...) */
  91. /*
  92. * Allocate an extent anywhere in the specific AG given. If there is no
  93. * space matching the requirements in that AG, then the allocation will fail.
  94. */
  95. int xfs_alloc_vextent_this_ag(struct xfs_alloc_arg *args, xfs_agnumber_t agno);
  96. /*
  97. * Allocate an extent as close to the target as possible. If there are not
  98. * viable candidates in the AG, then fail the allocation.
  99. */
  100. int xfs_alloc_vextent_near_bno(struct xfs_alloc_arg *args,
  101. xfs_fsblock_t target);
  102. /*
  103. * Allocate an extent exactly at the target given. If this is not possible
  104. * then the allocation fails.
  105. */
  106. int xfs_alloc_vextent_exact_bno(struct xfs_alloc_arg *args,
  107. xfs_fsblock_t target);
  108. /*
  109. * Best effort full filesystem allocation scan.
  110. *
  111. * Locality aware allocation will be attempted in the initial AG, but on failure
  112. * non-localised attempts will be made. The AGs are constrained by previous
  113. * allocations in the current transaction. Two passes will be made - the first
  114. * non-blocking, the second blocking.
  115. */
  116. int xfs_alloc_vextent_start_ag(struct xfs_alloc_arg *args,
  117. xfs_fsblock_t target);
  118. /*
  119. * Iterate from the AG indicated from args->fsbno through to the end of the
  120. * filesystem attempting blocking allocation. This is for use in last
  121. * resort allocation attempts when everything else has failed.
  122. */
  123. int xfs_alloc_vextent_first_ag(struct xfs_alloc_arg *args,
  124. xfs_fsblock_t target);
  125. /*
  126. * Free an extent.
  127. */
  128. int /* error */
  129. __xfs_free_extent(
  130. struct xfs_trans *tp, /* transaction pointer */
  131. struct xfs_perag *pag,
  132. xfs_agblock_t agbno,
  133. xfs_extlen_t len, /* length of extent */
  134. const struct xfs_owner_info *oinfo, /* extent owner */
  135. enum xfs_ag_resv_type type, /* block reservation type */
  136. bool skip_discard);
  137. static inline int
  138. xfs_free_extent(
  139. struct xfs_trans *tp,
  140. struct xfs_perag *pag,
  141. xfs_agblock_t agbno,
  142. xfs_extlen_t len,
  143. const struct xfs_owner_info *oinfo,
  144. enum xfs_ag_resv_type type)
  145. {
  146. return __xfs_free_extent(tp, pag, agbno, len, oinfo, type, false);
  147. }
  148. int /* error */
  149. xfs_alloc_lookup_le(
  150. struct xfs_btree_cur *cur, /* btree cursor */
  151. xfs_agblock_t bno, /* starting block of extent */
  152. xfs_extlen_t len, /* length of extent */
  153. int *stat); /* success/failure */
  154. int /* error */
  155. xfs_alloc_lookup_ge(
  156. struct xfs_btree_cur *cur, /* btree cursor */
  157. xfs_agblock_t bno, /* starting block of extent */
  158. xfs_extlen_t len, /* length of extent */
  159. int *stat); /* success/failure */
  160. int /* error */
  161. xfs_alloc_get_rec(
  162. struct xfs_btree_cur *cur, /* btree cursor */
  163. xfs_agblock_t *bno, /* output: starting block of extent */
  164. xfs_extlen_t *len, /* output: length of extent */
  165. int *stat); /* output: success/failure */
  166. union xfs_btree_rec;
  167. void xfs_alloc_btrec_to_irec(const union xfs_btree_rec *rec,
  168. struct xfs_alloc_rec_incore *irec);
  169. xfs_failaddr_t xfs_alloc_check_irec(struct xfs_perag *pag,
  170. const struct xfs_alloc_rec_incore *irec);
  171. int xfs_read_agf(struct xfs_perag *pag, struct xfs_trans *tp, int flags,
  172. struct xfs_buf **agfbpp);
  173. int xfs_alloc_read_agf(struct xfs_perag *pag, struct xfs_trans *tp, int flags,
  174. struct xfs_buf **agfbpp);
  175. int xfs_alloc_read_agfl(struct xfs_perag *pag, struct xfs_trans *tp,
  176. struct xfs_buf **bpp);
  177. int xfs_alloc_fix_freelist(struct xfs_alloc_arg *args, uint32_t alloc_flags);
  178. int xfs_free_extent_fix_freelist(struct xfs_trans *tp, struct xfs_perag *pag,
  179. struct xfs_buf **agbp);
  180. xfs_extlen_t xfs_prealloc_blocks(struct xfs_mount *mp);
  181. typedef int (*xfs_alloc_query_range_fn)(
  182. struct xfs_btree_cur *cur,
  183. const struct xfs_alloc_rec_incore *rec,
  184. void *priv);
  185. int xfs_alloc_query_range(struct xfs_btree_cur *cur,
  186. const struct xfs_alloc_rec_incore *low_rec,
  187. const struct xfs_alloc_rec_incore *high_rec,
  188. xfs_alloc_query_range_fn fn, void *priv);
  189. int xfs_alloc_query_all(struct xfs_btree_cur *cur, xfs_alloc_query_range_fn fn,
  190. void *priv);
  191. int xfs_alloc_has_records(struct xfs_btree_cur *cur, xfs_agblock_t bno,
  192. xfs_extlen_t len, enum xbtree_recpacking *outcome);
  193. typedef int (*xfs_agfl_walk_fn)(struct xfs_mount *mp, xfs_agblock_t bno,
  194. void *priv);
  195. int xfs_agfl_walk(struct xfs_mount *mp, struct xfs_agf *agf,
  196. struct xfs_buf *agflbp, xfs_agfl_walk_fn walk_fn, void *priv);
  197. static inline __be32 *
  198. xfs_buf_to_agfl_bno(
  199. struct xfs_buf *bp)
  200. {
  201. if (xfs_has_crc(bp->b_mount))
  202. return bp->b_addr + sizeof(struct xfs_agfl);
  203. return bp->b_addr;
  204. }
  205. int xfs_free_extent_later(struct xfs_trans *tp, xfs_fsblock_t bno,
  206. xfs_filblks_t len, const struct xfs_owner_info *oinfo,
  207. enum xfs_ag_resv_type type, unsigned int free_flags);
  208. /* Don't issue a discard for the blocks freed. */
  209. #define XFS_FREE_EXTENT_SKIP_DISCARD (1U << 0)
  210. #define XFS_FREE_EXTENT_ALL_FLAGS (XFS_FREE_EXTENT_SKIP_DISCARD)
  211. /*
  212. * List of extents to be free "later".
  213. * The list is kept sorted on xbf_startblock.
  214. */
  215. struct xfs_extent_free_item {
  216. struct list_head xefi_list;
  217. uint64_t xefi_owner;
  218. xfs_fsblock_t xefi_startblock;/* starting fs block number */
  219. xfs_extlen_t xefi_blockcount;/* number of blocks in extent */
  220. struct xfs_perag *xefi_pag;
  221. unsigned int xefi_flags;
  222. enum xfs_ag_resv_type xefi_agresv;
  223. };
  224. #define XFS_EFI_SKIP_DISCARD (1U << 0) /* don't issue discard */
  225. #define XFS_EFI_ATTR_FORK (1U << 1) /* freeing attr fork block */
  226. #define XFS_EFI_BMBT_BLOCK (1U << 2) /* freeing bmap btree block */
  227. #define XFS_EFI_CANCELLED (1U << 3) /* dont actually free the space */
  228. struct xfs_alloc_autoreap {
  229. struct xfs_defer_pending *dfp;
  230. };
  231. int xfs_alloc_schedule_autoreap(const struct xfs_alloc_arg *args,
  232. unsigned int free_flags, struct xfs_alloc_autoreap *aarp);
  233. void xfs_alloc_cancel_autoreap(struct xfs_trans *tp,
  234. struct xfs_alloc_autoreap *aarp);
  235. void xfs_alloc_commit_autoreap(struct xfs_trans *tp,
  236. struct xfs_alloc_autoreap *aarp);
  237. extern struct kmem_cache *xfs_extfree_item_cache;
  238. int __init xfs_extfree_intent_init_cache(void);
  239. void xfs_extfree_intent_destroy_cache(void);
  240. xfs_failaddr_t xfs_validate_ag_length(struct xfs_buf *bp, uint32_t seqno,
  241. uint32_t length);
  242. #endif /* __XFS_ALLOC_H__ */