xfs_alloc.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. * Freespace allocation types. Argument to xfs_alloc_[v]extent.
  17. */
  18. #define XFS_ALLOCTYPE_FIRST_AG 0x02 /* ... start at ag 0 */
  19. #define XFS_ALLOCTYPE_THIS_AG 0x08 /* anywhere in this a.g. */
  20. #define XFS_ALLOCTYPE_START_BNO 0x10 /* near this block else anywhere */
  21. #define XFS_ALLOCTYPE_NEAR_BNO 0x20 /* in this a.g. and near this block */
  22. #define XFS_ALLOCTYPE_THIS_BNO 0x40 /* at exactly this block */
  23. /* this should become an enum again when the tracing code is fixed */
  24. typedef unsigned int xfs_alloctype_t;
  25. #define XFS_ALLOC_TYPES \
  26. { XFS_ALLOCTYPE_FIRST_AG, "FIRST_AG" }, \
  27. { XFS_ALLOCTYPE_THIS_AG, "THIS_AG" }, \
  28. { XFS_ALLOCTYPE_START_BNO, "START_BNO" }, \
  29. { XFS_ALLOCTYPE_NEAR_BNO, "NEAR_BNO" }, \
  30. { XFS_ALLOCTYPE_THIS_BNO, "THIS_BNO" }
  31. /*
  32. * Flags for xfs_alloc_fix_freelist.
  33. */
  34. #define XFS_ALLOC_FLAG_TRYLOCK 0x00000001 /* use trylock for buffer locking */
  35. #define XFS_ALLOC_FLAG_FREEING 0x00000002 /* indicate caller is freeing extents*/
  36. #define XFS_ALLOC_FLAG_NORMAP 0x00000004 /* don't modify the rmapbt */
  37. #define XFS_ALLOC_FLAG_NOSHRINK 0x00000008 /* don't shrink the freelist */
  38. #define XFS_ALLOC_FLAG_CHECK 0x00000010 /* test only, don't modify args */
  39. /*
  40. * Argument structure for xfs_alloc routines.
  41. * This is turned into a structure to avoid having 20 arguments passed
  42. * down several levels of the stack.
  43. */
  44. typedef struct xfs_alloc_arg {
  45. struct xfs_trans *tp; /* transaction pointer */
  46. struct xfs_mount *mp; /* file system mount point */
  47. struct xfs_buf *agbp; /* buffer for a.g. freelist header */
  48. struct xfs_perag *pag; /* per-ag struct for this agno */
  49. struct xfs_inode *ip; /* for userdata zeroing method */
  50. xfs_fsblock_t fsbno; /* file system block number */
  51. xfs_agnumber_t agno; /* allocation group number */
  52. xfs_agblock_t agbno; /* allocation group-relative block # */
  53. xfs_extlen_t minlen; /* minimum size of extent */
  54. xfs_extlen_t maxlen; /* maximum size of extent */
  55. xfs_extlen_t mod; /* mod value for extent size */
  56. xfs_extlen_t prod; /* prod value for extent size */
  57. xfs_extlen_t minleft; /* min blocks must be left after us */
  58. xfs_extlen_t total; /* total blocks needed in xaction */
  59. xfs_extlen_t alignment; /* align answer to multiple of this */
  60. xfs_extlen_t minalignslop; /* slop for minlen+alignment calcs */
  61. xfs_agblock_t min_agbno; /* set an agbno range for NEAR allocs */
  62. xfs_agblock_t max_agbno; /* ... */
  63. xfs_extlen_t len; /* output: actual size of extent */
  64. xfs_alloctype_t type; /* allocation type XFS_ALLOCTYPE_... */
  65. xfs_alloctype_t otype; /* original allocation type */
  66. int datatype; /* mask defining data type treatment */
  67. char wasdel; /* set if allocation was prev delayed */
  68. char wasfromfl; /* set if allocation is from freelist */
  69. struct xfs_owner_info oinfo; /* owner of blocks being allocated */
  70. enum xfs_ag_resv_type resv; /* block reservation to use */
  71. } xfs_alloc_arg_t;
  72. /*
  73. * Defines for datatype
  74. */
  75. #define XFS_ALLOC_USERDATA (1 << 0)/* allocation is for user data*/
  76. #define XFS_ALLOC_INITIAL_USER_DATA (1 << 1)/* special case start of file */
  77. #define XFS_ALLOC_USERDATA_ZERO (1 << 2)/* zero extent on allocation */
  78. #define XFS_ALLOC_NOBUSY (1 << 3)/* Busy extents not allowed */
  79. static inline bool
  80. xfs_alloc_is_userdata(int datatype)
  81. {
  82. return (datatype & ~XFS_ALLOC_NOBUSY) != 0;
  83. }
  84. static inline bool
  85. xfs_alloc_allow_busy_reuse(int datatype)
  86. {
  87. return (datatype & XFS_ALLOC_NOBUSY) == 0;
  88. }
  89. /* freespace limit calculations */
  90. #define XFS_ALLOC_AGFL_RESERVE 4
  91. unsigned int xfs_alloc_set_aside(struct xfs_mount *mp);
  92. unsigned int xfs_alloc_ag_max_usable(struct xfs_mount *mp);
  93. xfs_extlen_t xfs_alloc_longest_free_extent(struct xfs_perag *pag,
  94. xfs_extlen_t need, xfs_extlen_t reserved);
  95. unsigned int xfs_alloc_min_freelist(struct xfs_mount *mp,
  96. struct xfs_perag *pag);
  97. /*
  98. * Compute and fill in value of m_ag_maxlevels.
  99. */
  100. void
  101. xfs_alloc_compute_maxlevels(
  102. struct xfs_mount *mp); /* file system mount structure */
  103. /*
  104. * Get a block from the freelist.
  105. * Returns with the buffer for the block gotten.
  106. */
  107. int /* error */
  108. xfs_alloc_get_freelist(
  109. struct xfs_trans *tp, /* transaction pointer */
  110. struct xfs_buf *agbp, /* buffer containing the agf structure */
  111. xfs_agblock_t *bnop, /* block address retrieved from freelist */
  112. int btreeblk); /* destination is a AGF btree */
  113. /*
  114. * Log the given fields from the agf structure.
  115. */
  116. void
  117. xfs_alloc_log_agf(
  118. struct xfs_trans *tp, /* transaction pointer */
  119. struct xfs_buf *bp, /* buffer for a.g. freelist header */
  120. int fields);/* mask of fields to be logged (XFS_AGF_...) */
  121. /*
  122. * Interface for inode allocation to force the pag data to be initialized.
  123. */
  124. int /* error */
  125. xfs_alloc_pagf_init(
  126. struct xfs_mount *mp, /* file system mount structure */
  127. struct xfs_trans *tp, /* transaction pointer */
  128. xfs_agnumber_t agno, /* allocation group number */
  129. int flags); /* XFS_ALLOC_FLAGS_... */
  130. /*
  131. * Put the block on the freelist for the allocation group.
  132. */
  133. int /* error */
  134. xfs_alloc_put_freelist(
  135. struct xfs_trans *tp, /* transaction pointer */
  136. struct xfs_buf *agbp, /* buffer for a.g. freelist header */
  137. struct xfs_buf *agflbp,/* buffer for a.g. free block array */
  138. xfs_agblock_t bno, /* block being freed */
  139. int btreeblk); /* owner was a AGF btree */
  140. /*
  141. * Read in the allocation group header (free/alloc section).
  142. */
  143. int /* error */
  144. xfs_alloc_read_agf(
  145. struct xfs_mount *mp, /* mount point structure */
  146. struct xfs_trans *tp, /* transaction pointer */
  147. xfs_agnumber_t agno, /* allocation group number */
  148. int flags, /* XFS_ALLOC_FLAG_... */
  149. struct xfs_buf **bpp); /* buffer for the ag freelist header */
  150. /*
  151. * Allocate an extent (variable-size).
  152. */
  153. int /* error */
  154. xfs_alloc_vextent(
  155. xfs_alloc_arg_t *args); /* allocation argument structure */
  156. /*
  157. * Free an extent.
  158. */
  159. int /* error */
  160. __xfs_free_extent(
  161. struct xfs_trans *tp, /* transaction pointer */
  162. xfs_fsblock_t bno, /* starting block number of extent */
  163. xfs_extlen_t len, /* length of extent */
  164. struct xfs_owner_info *oinfo, /* extent owner */
  165. enum xfs_ag_resv_type type, /* block reservation type */
  166. bool skip_discard);
  167. static inline int
  168. xfs_free_extent(
  169. struct xfs_trans *tp,
  170. xfs_fsblock_t bno,
  171. xfs_extlen_t len,
  172. struct xfs_owner_info *oinfo,
  173. enum xfs_ag_resv_type type)
  174. {
  175. return __xfs_free_extent(tp, bno, len, oinfo, type, false);
  176. }
  177. int /* error */
  178. xfs_alloc_lookup_le(
  179. struct xfs_btree_cur *cur, /* btree cursor */
  180. xfs_agblock_t bno, /* starting block of extent */
  181. xfs_extlen_t len, /* length of extent */
  182. int *stat); /* success/failure */
  183. int /* error */
  184. xfs_alloc_lookup_ge(
  185. struct xfs_btree_cur *cur, /* btree cursor */
  186. xfs_agblock_t bno, /* starting block of extent */
  187. xfs_extlen_t len, /* length of extent */
  188. int *stat); /* success/failure */
  189. int /* error */
  190. xfs_alloc_get_rec(
  191. struct xfs_btree_cur *cur, /* btree cursor */
  192. xfs_agblock_t *bno, /* output: starting block of extent */
  193. xfs_extlen_t *len, /* output: length of extent */
  194. int *stat); /* output: success/failure */
  195. int xfs_read_agf(struct xfs_mount *mp, struct xfs_trans *tp,
  196. xfs_agnumber_t agno, int flags, struct xfs_buf **bpp);
  197. int xfs_alloc_read_agfl(struct xfs_mount *mp, struct xfs_trans *tp,
  198. xfs_agnumber_t agno, struct xfs_buf **bpp);
  199. int xfs_free_agfl_block(struct xfs_trans *, xfs_agnumber_t, xfs_agblock_t,
  200. struct xfs_buf *, struct xfs_owner_info *);
  201. int xfs_alloc_fix_freelist(struct xfs_alloc_arg *args, int flags);
  202. int xfs_free_extent_fix_freelist(struct xfs_trans *tp, xfs_agnumber_t agno,
  203. struct xfs_buf **agbp);
  204. xfs_extlen_t xfs_prealloc_blocks(struct xfs_mount *mp);
  205. typedef int (*xfs_alloc_query_range_fn)(
  206. struct xfs_btree_cur *cur,
  207. struct xfs_alloc_rec_incore *rec,
  208. void *priv);
  209. int xfs_alloc_query_range(struct xfs_btree_cur *cur,
  210. struct xfs_alloc_rec_incore *low_rec,
  211. struct xfs_alloc_rec_incore *high_rec,
  212. xfs_alloc_query_range_fn fn, void *priv);
  213. int xfs_alloc_query_all(struct xfs_btree_cur *cur, xfs_alloc_query_range_fn fn,
  214. void *priv);
  215. int xfs_alloc_has_record(struct xfs_btree_cur *cur, xfs_agblock_t bno,
  216. xfs_extlen_t len, bool *exist);
  217. typedef int (*xfs_agfl_walk_fn)(struct xfs_mount *mp, xfs_agblock_t bno,
  218. void *priv);
  219. int xfs_agfl_walk(struct xfs_mount *mp, struct xfs_agf *agf,
  220. struct xfs_buf *agflbp, xfs_agfl_walk_fn walk_fn, void *priv);
  221. #endif /* __XFS_ALLOC_H__ */