xfs_alloc.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write the Free Software Foundation,
  16. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #ifndef __XFS_ALLOC_H__
  19. #define __XFS_ALLOC_H__
  20. struct xfs_buf;
  21. struct xfs_btree_cur;
  22. struct xfs_mount;
  23. struct xfs_perag;
  24. struct xfs_trans;
  25. struct xfs_busy_extent;
  26. extern struct workqueue_struct *xfs_alloc_wq;
  27. /*
  28. * Freespace allocation types. Argument to xfs_alloc_[v]extent.
  29. */
  30. #define XFS_ALLOCTYPE_ANY_AG 0x01 /* allocate anywhere, use rotor */
  31. #define XFS_ALLOCTYPE_FIRST_AG 0x02 /* ... start at ag 0 */
  32. #define XFS_ALLOCTYPE_START_AG 0x04 /* anywhere, start in this a.g. */
  33. #define XFS_ALLOCTYPE_THIS_AG 0x08 /* anywhere in this a.g. */
  34. #define XFS_ALLOCTYPE_START_BNO 0x10 /* near this block else anywhere */
  35. #define XFS_ALLOCTYPE_NEAR_BNO 0x20 /* in this a.g. and near this block */
  36. #define XFS_ALLOCTYPE_THIS_BNO 0x40 /* at exactly this block */
  37. /* this should become an enum again when the tracing code is fixed */
  38. typedef unsigned int xfs_alloctype_t;
  39. #define XFS_ALLOC_TYPES \
  40. { XFS_ALLOCTYPE_ANY_AG, "ANY_AG" }, \
  41. { XFS_ALLOCTYPE_FIRST_AG, "FIRST_AG" }, \
  42. { XFS_ALLOCTYPE_START_AG, "START_AG" }, \
  43. { XFS_ALLOCTYPE_THIS_AG, "THIS_AG" }, \
  44. { XFS_ALLOCTYPE_START_BNO, "START_BNO" }, \
  45. { XFS_ALLOCTYPE_NEAR_BNO, "NEAR_BNO" }, \
  46. { XFS_ALLOCTYPE_THIS_BNO, "THIS_BNO" }
  47. /*
  48. * Flags for xfs_alloc_fix_freelist.
  49. */
  50. #define XFS_ALLOC_FLAG_TRYLOCK 0x00000001 /* use trylock for buffer locking */
  51. #define XFS_ALLOC_FLAG_FREEING 0x00000002 /* indicate caller is freeing extents*/
  52. /*
  53. * In order to avoid ENOSPC-related deadlock caused by
  54. * out-of-order locking of AGF buffer (PV 947395), we place
  55. * constraints on the relationship among actual allocations for
  56. * data blocks, freelist blocks, and potential file data bmap
  57. * btree blocks. However, these restrictions may result in no
  58. * actual space allocated for a delayed extent, for example, a data
  59. * block in a certain AG is allocated but there is no additional
  60. * block for the additional bmap btree block due to a split of the
  61. * bmap btree of the file. The result of this may lead to an
  62. * infinite loop in xfssyncd when the file gets flushed to disk and
  63. * all delayed extents need to be actually allocated. To get around
  64. * this, we explicitly set aside a few blocks which will not be
  65. * reserved in delayed allocation. Considering the minimum number of
  66. * needed freelist blocks is 4 fsbs _per AG_, a potential split of file's bmap
  67. * btree requires 1 fsb, so we set the number of set-aside blocks
  68. * to 4 + 4*agcount.
  69. */
  70. #define XFS_ALLOC_SET_ASIDE(mp) (4 + ((mp)->m_sb.sb_agcount * 4))
  71. /*
  72. * When deciding how much space to allocate out of an AG, we limit the
  73. * allocation maximum size to the size the AG. However, we cannot use all the
  74. * blocks in the AG - some are permanently used by metadata. These
  75. * blocks are generally:
  76. * - the AG superblock, AGF, AGI and AGFL
  77. * - the AGF (bno and cnt) and AGI btree root blocks
  78. * - 4 blocks on the AGFL according to XFS_ALLOC_SET_ASIDE() limits
  79. *
  80. * The AG headers are sector sized, so the amount of space they take up is
  81. * dependent on filesystem geometry. The others are all single blocks.
  82. */
  83. #define XFS_ALLOC_AG_MAX_USABLE(mp) \
  84. ((mp)->m_sb.sb_agblocks - XFS_BB_TO_FSB(mp, XFS_FSS_TO_BB(mp, 4)) - 7)
  85. /*
  86. * Argument structure for xfs_alloc routines.
  87. * This is turned into a structure to avoid having 20 arguments passed
  88. * down several levels of the stack.
  89. */
  90. typedef struct xfs_alloc_arg {
  91. struct xfs_trans *tp; /* transaction pointer */
  92. struct xfs_mount *mp; /* file system mount point */
  93. struct xfs_buf *agbp; /* buffer for a.g. freelist header */
  94. struct xfs_perag *pag; /* per-ag struct for this agno */
  95. xfs_fsblock_t fsbno; /* file system block number */
  96. xfs_agnumber_t agno; /* allocation group number */
  97. xfs_agblock_t agbno; /* allocation group-relative block # */
  98. xfs_extlen_t minlen; /* minimum size of extent */
  99. xfs_extlen_t maxlen; /* maximum size of extent */
  100. xfs_extlen_t mod; /* mod value for extent size */
  101. xfs_extlen_t prod; /* prod value for extent size */
  102. xfs_extlen_t minleft; /* min blocks must be left after us */
  103. xfs_extlen_t total; /* total blocks needed in xaction */
  104. xfs_extlen_t alignment; /* align answer to multiple of this */
  105. xfs_extlen_t minalignslop; /* slop for minlen+alignment calcs */
  106. xfs_extlen_t len; /* output: actual size of extent */
  107. xfs_alloctype_t type; /* allocation type XFS_ALLOCTYPE_... */
  108. xfs_alloctype_t otype; /* original allocation type */
  109. char wasdel; /* set if allocation was prev delayed */
  110. char wasfromfl; /* set if allocation is from freelist */
  111. char isfl; /* set if is freelist blocks - !acctg */
  112. char userdata; /* set if this is user data */
  113. xfs_fsblock_t firstblock; /* io first block allocated */
  114. struct completion *done;
  115. struct work_struct work;
  116. int result;
  117. } xfs_alloc_arg_t;
  118. /*
  119. * Defines for userdata
  120. */
  121. #define XFS_ALLOC_USERDATA 1 /* allocation is for user data*/
  122. #define XFS_ALLOC_INITIAL_USER_DATA 2 /* special case start of file */
  123. /*
  124. * Find the length of the longest extent in an AG.
  125. */
  126. xfs_extlen_t
  127. xfs_alloc_longest_free_extent(struct xfs_mount *mp,
  128. struct xfs_perag *pag);
  129. #ifdef __KERNEL__
  130. void
  131. xfs_alloc_busy_insert(struct xfs_trans *tp, xfs_agnumber_t agno,
  132. xfs_agblock_t bno, xfs_extlen_t len, unsigned int flags);
  133. void
  134. xfs_alloc_busy_clear(struct xfs_mount *mp, struct list_head *list,
  135. bool do_discard);
  136. int
  137. xfs_alloc_busy_search(struct xfs_mount *mp, xfs_agnumber_t agno,
  138. xfs_agblock_t bno, xfs_extlen_t len);
  139. void
  140. xfs_alloc_busy_reuse(struct xfs_mount *mp, xfs_agnumber_t agno,
  141. xfs_agblock_t fbno, xfs_extlen_t flen, bool userdata);
  142. int
  143. xfs_busy_extent_ag_cmp(void *priv, struct list_head *a, struct list_head *b);
  144. static inline void xfs_alloc_busy_sort(struct list_head *list)
  145. {
  146. list_sort(NULL, list, xfs_busy_extent_ag_cmp);
  147. }
  148. #endif /* __KERNEL__ */
  149. /*
  150. * Compute and fill in value of m_ag_maxlevels.
  151. */
  152. void
  153. xfs_alloc_compute_maxlevels(
  154. struct xfs_mount *mp); /* file system mount structure */
  155. /*
  156. * Get a block from the freelist.
  157. * Returns with the buffer for the block gotten.
  158. */
  159. int /* error */
  160. xfs_alloc_get_freelist(
  161. struct xfs_trans *tp, /* transaction pointer */
  162. struct xfs_buf *agbp, /* buffer containing the agf structure */
  163. xfs_agblock_t *bnop, /* block address retrieved from freelist */
  164. int btreeblk); /* destination is a AGF btree */
  165. /*
  166. * Log the given fields from the agf structure.
  167. */
  168. void
  169. xfs_alloc_log_agf(
  170. struct xfs_trans *tp, /* transaction pointer */
  171. struct xfs_buf *bp, /* buffer for a.g. freelist header */
  172. int fields);/* mask of fields to be logged (XFS_AGF_...) */
  173. /*
  174. * Interface for inode allocation to force the pag data to be initialized.
  175. */
  176. int /* error */
  177. xfs_alloc_pagf_init(
  178. struct xfs_mount *mp, /* file system mount structure */
  179. struct xfs_trans *tp, /* transaction pointer */
  180. xfs_agnumber_t agno, /* allocation group number */
  181. int flags); /* XFS_ALLOC_FLAGS_... */
  182. /*
  183. * Put the block on the freelist for the allocation group.
  184. */
  185. int /* error */
  186. xfs_alloc_put_freelist(
  187. struct xfs_trans *tp, /* transaction pointer */
  188. struct xfs_buf *agbp, /* buffer for a.g. freelist header */
  189. struct xfs_buf *agflbp,/* buffer for a.g. free block array */
  190. xfs_agblock_t bno, /* block being freed */
  191. int btreeblk); /* owner was a AGF btree */
  192. /*
  193. * Read in the allocation group header (free/alloc section).
  194. */
  195. int /* error */
  196. xfs_alloc_read_agf(
  197. struct xfs_mount *mp, /* mount point structure */
  198. struct xfs_trans *tp, /* transaction pointer */
  199. xfs_agnumber_t agno, /* allocation group number */
  200. int flags, /* XFS_ALLOC_FLAG_... */
  201. struct xfs_buf **bpp); /* buffer for the ag freelist header */
  202. /*
  203. * Allocate an extent (variable-size).
  204. */
  205. int /* error */
  206. xfs_alloc_vextent(
  207. xfs_alloc_arg_t *args); /* allocation argument structure */
  208. /*
  209. * Free an extent.
  210. */
  211. int /* error */
  212. xfs_free_extent(
  213. struct xfs_trans *tp, /* transaction pointer */
  214. xfs_fsblock_t bno, /* starting block number of extent */
  215. xfs_extlen_t len); /* length of extent */
  216. int /* error */
  217. xfs_alloc_lookup_le(
  218. struct xfs_btree_cur *cur, /* btree cursor */
  219. xfs_agblock_t bno, /* starting block of extent */
  220. xfs_extlen_t len, /* length of extent */
  221. int *stat); /* success/failure */
  222. int /* error */
  223. xfs_alloc_lookup_ge(
  224. struct xfs_btree_cur *cur, /* btree cursor */
  225. xfs_agblock_t bno, /* starting block of extent */
  226. xfs_extlen_t len, /* length of extent */
  227. int *stat); /* success/failure */
  228. int /* error */
  229. xfs_alloc_get_rec(
  230. struct xfs_btree_cur *cur, /* btree cursor */
  231. xfs_agblock_t *bno, /* output: starting block of extent */
  232. xfs_extlen_t *len, /* output: length of extent */
  233. int *stat); /* output: success/failure */
  234. #endif /* __XFS_ALLOC_H__ */