xfs_rtbitmap.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
  4. * All Rights Reserved.
  5. */
  6. #ifndef __XFS_RTBITMAP_H__
  7. #define __XFS_RTBITMAP_H__
  8. struct xfs_rtalloc_args {
  9. struct xfs_mount *mp;
  10. struct xfs_trans *tp;
  11. struct xfs_buf *rbmbp; /* bitmap block buffer */
  12. struct xfs_buf *sumbp; /* summary block buffer */
  13. xfs_fileoff_t rbmoff; /* bitmap block number */
  14. xfs_fileoff_t sumoff; /* summary block number */
  15. };
  16. static inline xfs_rtblock_t
  17. xfs_rtx_to_rtb(
  18. struct xfs_mount *mp,
  19. xfs_rtxnum_t rtx)
  20. {
  21. if (mp->m_rtxblklog >= 0)
  22. return rtx << mp->m_rtxblklog;
  23. return rtx * mp->m_sb.sb_rextsize;
  24. }
  25. static inline xfs_extlen_t
  26. xfs_rtxlen_to_extlen(
  27. struct xfs_mount *mp,
  28. xfs_rtxlen_t rtxlen)
  29. {
  30. if (mp->m_rtxblklog >= 0)
  31. return rtxlen << mp->m_rtxblklog;
  32. return rtxlen * mp->m_sb.sb_rextsize;
  33. }
  34. /* Compute the misalignment between an extent length and a realtime extent .*/
  35. static inline unsigned int
  36. xfs_extlen_to_rtxmod(
  37. struct xfs_mount *mp,
  38. xfs_extlen_t len)
  39. {
  40. if (mp->m_rtxblklog >= 0)
  41. return len & mp->m_rtxblkmask;
  42. return len % mp->m_sb.sb_rextsize;
  43. }
  44. static inline xfs_rtxlen_t
  45. xfs_extlen_to_rtxlen(
  46. struct xfs_mount *mp,
  47. xfs_extlen_t len)
  48. {
  49. if (mp->m_rtxblklog >= 0)
  50. return len >> mp->m_rtxblklog;
  51. return len / mp->m_sb.sb_rextsize;
  52. }
  53. /* Convert an rt block number into an rt extent number. */
  54. static inline xfs_rtxnum_t
  55. xfs_rtb_to_rtx(
  56. struct xfs_mount *mp,
  57. xfs_rtblock_t rtbno)
  58. {
  59. if (likely(mp->m_rtxblklog >= 0))
  60. return rtbno >> mp->m_rtxblklog;
  61. return div_u64(rtbno, mp->m_sb.sb_rextsize);
  62. }
  63. /* Return the offset of an rt block number within an rt extent. */
  64. static inline xfs_extlen_t
  65. xfs_rtb_to_rtxoff(
  66. struct xfs_mount *mp,
  67. xfs_rtblock_t rtbno)
  68. {
  69. if (likely(mp->m_rtxblklog >= 0))
  70. return rtbno & mp->m_rtxblkmask;
  71. return do_div(rtbno, mp->m_sb.sb_rextsize);
  72. }
  73. /*
  74. * Convert an rt block number into an rt extent number, rounding up to the next
  75. * rt extent if the rt block is not aligned to an rt extent boundary.
  76. */
  77. static inline xfs_rtxnum_t
  78. xfs_rtb_to_rtxup(
  79. struct xfs_mount *mp,
  80. xfs_rtblock_t rtbno)
  81. {
  82. if (likely(mp->m_rtxblklog >= 0)) {
  83. if (rtbno & mp->m_rtxblkmask)
  84. return (rtbno >> mp->m_rtxblklog) + 1;
  85. return rtbno >> mp->m_rtxblklog;
  86. }
  87. if (do_div(rtbno, mp->m_sb.sb_rextsize))
  88. rtbno++;
  89. return rtbno;
  90. }
  91. /* Round this rtblock up to the nearest rt extent size. */
  92. static inline xfs_rtblock_t
  93. xfs_rtb_roundup_rtx(
  94. struct xfs_mount *mp,
  95. xfs_rtblock_t rtbno)
  96. {
  97. return roundup_64(rtbno, mp->m_sb.sb_rextsize);
  98. }
  99. /* Round this rtblock down to the nearest rt extent size. */
  100. static inline xfs_rtblock_t
  101. xfs_rtb_rounddown_rtx(
  102. struct xfs_mount *mp,
  103. xfs_rtblock_t rtbno)
  104. {
  105. return rounddown_64(rtbno, mp->m_sb.sb_rextsize);
  106. }
  107. /* Convert an rt extent number to a file block offset in the rt bitmap file. */
  108. static inline xfs_fileoff_t
  109. xfs_rtx_to_rbmblock(
  110. struct xfs_mount *mp,
  111. xfs_rtxnum_t rtx)
  112. {
  113. return rtx >> mp->m_blkbit_log;
  114. }
  115. /* Convert an rt extent number to a word offset within an rt bitmap block. */
  116. static inline unsigned int
  117. xfs_rtx_to_rbmword(
  118. struct xfs_mount *mp,
  119. xfs_rtxnum_t rtx)
  120. {
  121. return (rtx >> XFS_NBWORDLOG) & (mp->m_blockwsize - 1);
  122. }
  123. /* Convert a file block offset in the rt bitmap file to an rt extent number. */
  124. static inline xfs_rtxnum_t
  125. xfs_rbmblock_to_rtx(
  126. struct xfs_mount *mp,
  127. xfs_fileoff_t rbmoff)
  128. {
  129. return rbmoff << mp->m_blkbit_log;
  130. }
  131. /* Return a pointer to a bitmap word within a rt bitmap block. */
  132. static inline union xfs_rtword_raw *
  133. xfs_rbmblock_wordptr(
  134. struct xfs_rtalloc_args *args,
  135. unsigned int index)
  136. {
  137. union xfs_rtword_raw *words = args->rbmbp->b_addr;
  138. return words + index;
  139. }
  140. /* Convert an ondisk bitmap word to its incore representation. */
  141. static inline xfs_rtword_t
  142. xfs_rtbitmap_getword(
  143. struct xfs_rtalloc_args *args,
  144. unsigned int index)
  145. {
  146. union xfs_rtword_raw *word = xfs_rbmblock_wordptr(args, index);
  147. return word->old;
  148. }
  149. /* Set an ondisk bitmap word from an incore representation. */
  150. static inline void
  151. xfs_rtbitmap_setword(
  152. struct xfs_rtalloc_args *args,
  153. unsigned int index,
  154. xfs_rtword_t value)
  155. {
  156. union xfs_rtword_raw *word = xfs_rbmblock_wordptr(args, index);
  157. word->old = value;
  158. }
  159. /*
  160. * Convert a rt extent length and rt bitmap block number to a xfs_suminfo_t
  161. * offset within the rt summary file.
  162. */
  163. static inline xfs_rtsumoff_t
  164. xfs_rtsumoffs(
  165. struct xfs_mount *mp,
  166. int log2_len,
  167. xfs_fileoff_t rbmoff)
  168. {
  169. return log2_len * mp->m_sb.sb_rbmblocks + rbmoff;
  170. }
  171. /*
  172. * Convert an xfs_suminfo_t offset to a file block offset within the rt summary
  173. * file.
  174. */
  175. static inline xfs_fileoff_t
  176. xfs_rtsumoffs_to_block(
  177. struct xfs_mount *mp,
  178. xfs_rtsumoff_t rsumoff)
  179. {
  180. return XFS_B_TO_FSBT(mp, rsumoff * sizeof(xfs_suminfo_t));
  181. }
  182. /*
  183. * Convert an xfs_suminfo_t offset to an info word offset within an rt summary
  184. * block.
  185. */
  186. static inline unsigned int
  187. xfs_rtsumoffs_to_infoword(
  188. struct xfs_mount *mp,
  189. xfs_rtsumoff_t rsumoff)
  190. {
  191. unsigned int mask = mp->m_blockmask >> XFS_SUMINFOLOG;
  192. return rsumoff & mask;
  193. }
  194. /* Return a pointer to a summary info word within a rt summary block. */
  195. static inline union xfs_suminfo_raw *
  196. xfs_rsumblock_infoptr(
  197. struct xfs_rtalloc_args *args,
  198. unsigned int index)
  199. {
  200. union xfs_suminfo_raw *info = args->sumbp->b_addr;
  201. return info + index;
  202. }
  203. /* Get the current value of a summary counter. */
  204. static inline xfs_suminfo_t
  205. xfs_suminfo_get(
  206. struct xfs_rtalloc_args *args,
  207. unsigned int index)
  208. {
  209. union xfs_suminfo_raw *info = xfs_rsumblock_infoptr(args, index);
  210. return info->old;
  211. }
  212. /* Add to the current value of a summary counter and return the new value. */
  213. static inline xfs_suminfo_t
  214. xfs_suminfo_add(
  215. struct xfs_rtalloc_args *args,
  216. unsigned int index,
  217. int delta)
  218. {
  219. union xfs_suminfo_raw *info = xfs_rsumblock_infoptr(args, index);
  220. info->old += delta;
  221. return info->old;
  222. }
  223. /*
  224. * Functions for walking free space rtextents in the realtime bitmap.
  225. */
  226. struct xfs_rtalloc_rec {
  227. xfs_rtxnum_t ar_startext;
  228. xfs_rtbxlen_t ar_extcount;
  229. };
  230. typedef int (*xfs_rtalloc_query_range_fn)(
  231. struct xfs_mount *mp,
  232. struct xfs_trans *tp,
  233. const struct xfs_rtalloc_rec *rec,
  234. void *priv);
  235. #ifdef CONFIG_XFS_RT
  236. void xfs_rtbuf_cache_relse(struct xfs_rtalloc_args *args);
  237. int xfs_rtbitmap_read_buf(struct xfs_rtalloc_args *args, xfs_fileoff_t block);
  238. int xfs_rtsummary_read_buf(struct xfs_rtalloc_args *args, xfs_fileoff_t block);
  239. int xfs_rtcheck_range(struct xfs_rtalloc_args *args, xfs_rtxnum_t start,
  240. xfs_rtxlen_t len, int val, xfs_rtxnum_t *new, int *stat);
  241. int xfs_rtfind_back(struct xfs_rtalloc_args *args, xfs_rtxnum_t start,
  242. xfs_rtxnum_t *rtblock);
  243. int xfs_rtfind_forw(struct xfs_rtalloc_args *args, xfs_rtxnum_t start,
  244. xfs_rtxnum_t limit, xfs_rtxnum_t *rtblock);
  245. int xfs_rtmodify_range(struct xfs_rtalloc_args *args, xfs_rtxnum_t start,
  246. xfs_rtxlen_t len, int val);
  247. int xfs_rtget_summary(struct xfs_rtalloc_args *args, int log,
  248. xfs_fileoff_t bbno, xfs_suminfo_t *sum);
  249. int xfs_rtmodify_summary(struct xfs_rtalloc_args *args, int log,
  250. xfs_fileoff_t bbno, int delta);
  251. int xfs_rtfree_range(struct xfs_rtalloc_args *args, xfs_rtxnum_t start,
  252. xfs_rtxlen_t len);
  253. int xfs_rtalloc_query_range(struct xfs_mount *mp, struct xfs_trans *tp,
  254. xfs_rtxnum_t start, xfs_rtxnum_t end,
  255. xfs_rtalloc_query_range_fn fn, void *priv);
  256. int xfs_rtalloc_query_all(struct xfs_mount *mp, struct xfs_trans *tp,
  257. xfs_rtalloc_query_range_fn fn,
  258. void *priv);
  259. int xfs_rtalloc_extent_is_free(struct xfs_mount *mp, struct xfs_trans *tp,
  260. xfs_rtxnum_t start, xfs_rtxlen_t len,
  261. bool *is_free);
  262. /*
  263. * Free an extent in the realtime subvolume. Length is expressed in
  264. * realtime extents, as is the block number.
  265. */
  266. int /* error */
  267. xfs_rtfree_extent(
  268. struct xfs_trans *tp, /* transaction pointer */
  269. xfs_rtxnum_t start, /* starting rtext number to free */
  270. xfs_rtxlen_t len); /* length of extent freed */
  271. /* Same as above, but in units of rt blocks. */
  272. int xfs_rtfree_blocks(struct xfs_trans *tp, xfs_fsblock_t rtbno,
  273. xfs_filblks_t rtlen);
  274. xfs_filblks_t xfs_rtbitmap_blockcount(struct xfs_mount *mp, xfs_rtbxlen_t
  275. rtextents);
  276. xfs_filblks_t xfs_rtsummary_blockcount(struct xfs_mount *mp,
  277. unsigned int rsumlevels, xfs_extlen_t rbmblocks);
  278. int xfs_rtfile_initialize_blocks(struct xfs_inode *ip,
  279. xfs_fileoff_t offset_fsb, xfs_fileoff_t end_fsb, void *data);
  280. void xfs_rtbitmap_lock(struct xfs_mount *mp);
  281. void xfs_rtbitmap_unlock(struct xfs_mount *mp);
  282. void xfs_rtbitmap_trans_join(struct xfs_trans *tp);
  283. /* Lock the rt bitmap inode in shared mode */
  284. #define XFS_RBMLOCK_BITMAP (1U << 0)
  285. /* Lock the rt summary inode in shared mode */
  286. #define XFS_RBMLOCK_SUMMARY (1U << 1)
  287. void xfs_rtbitmap_lock_shared(struct xfs_mount *mp,
  288. unsigned int rbmlock_flags);
  289. void xfs_rtbitmap_unlock_shared(struct xfs_mount *mp,
  290. unsigned int rbmlock_flags);
  291. #else /* CONFIG_XFS_RT */
  292. # define xfs_rtfree_extent(t,b,l) (-ENOSYS)
  293. # define xfs_rtfree_blocks(t,rb,rl) (-ENOSYS)
  294. # define xfs_rtalloc_query_range(m,t,l,h,f,p) (-ENOSYS)
  295. # define xfs_rtalloc_query_all(m,t,f,p) (-ENOSYS)
  296. # define xfs_rtbitmap_read_buf(a,b) (-ENOSYS)
  297. # define xfs_rtsummary_read_buf(a,b) (-ENOSYS)
  298. # define xfs_rtbuf_cache_relse(a) (0)
  299. # define xfs_rtalloc_extent_is_free(m,t,s,l,i) (-ENOSYS)
  300. static inline xfs_filblks_t
  301. xfs_rtbitmap_blockcount(struct xfs_mount *mp, xfs_rtbxlen_t rtextents)
  302. {
  303. /* shut up gcc */
  304. return 0;
  305. }
  306. # define xfs_rtsummary_blockcount(mp, l, b) (0)
  307. # define xfs_rtbitmap_lock(mp) do { } while (0)
  308. # define xfs_rtbitmap_trans_join(tp) do { } while (0)
  309. # define xfs_rtbitmap_unlock(mp) do { } while (0)
  310. # define xfs_rtbitmap_lock_shared(mp, lf) do { } while (0)
  311. # define xfs_rtbitmap_unlock_shared(mp, lf) do { } while (0)
  312. #endif /* CONFIG_XFS_RT */
  313. #endif /* __XFS_RTBITMAP_H__ */