xfs_refcount_btree.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2016 Oracle. All Rights Reserved.
  4. * Author: Darrick J. Wong <darrick.wong@oracle.com>
  5. */
  6. #include "xfs.h"
  7. #include "xfs_fs.h"
  8. #include "xfs_shared.h"
  9. #include "xfs_format.h"
  10. #include "xfs_log_format.h"
  11. #include "xfs_trans_resv.h"
  12. #include "xfs_sb.h"
  13. #include "xfs_mount.h"
  14. #include "xfs_btree.h"
  15. #include "xfs_bmap.h"
  16. #include "xfs_refcount_btree.h"
  17. #include "xfs_alloc.h"
  18. #include "xfs_error.h"
  19. #include "xfs_trace.h"
  20. #include "xfs_cksum.h"
  21. #include "xfs_trans.h"
  22. #include "xfs_bit.h"
  23. #include "xfs_rmap.h"
  24. static struct xfs_btree_cur *
  25. xfs_refcountbt_dup_cursor(
  26. struct xfs_btree_cur *cur)
  27. {
  28. return xfs_refcountbt_init_cursor(cur->bc_mp, cur->bc_tp,
  29. cur->bc_private.a.agbp, cur->bc_private.a.agno);
  30. }
  31. STATIC void
  32. xfs_refcountbt_set_root(
  33. struct xfs_btree_cur *cur,
  34. union xfs_btree_ptr *ptr,
  35. int inc)
  36. {
  37. struct xfs_buf *agbp = cur->bc_private.a.agbp;
  38. struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
  39. xfs_agnumber_t seqno = be32_to_cpu(agf->agf_seqno);
  40. struct xfs_perag *pag = xfs_perag_get(cur->bc_mp, seqno);
  41. ASSERT(ptr->s != 0);
  42. agf->agf_refcount_root = ptr->s;
  43. be32_add_cpu(&agf->agf_refcount_level, inc);
  44. pag->pagf_refcount_level += inc;
  45. xfs_perag_put(pag);
  46. xfs_alloc_log_agf(cur->bc_tp, agbp,
  47. XFS_AGF_REFCOUNT_ROOT | XFS_AGF_REFCOUNT_LEVEL);
  48. }
  49. STATIC int
  50. xfs_refcountbt_alloc_block(
  51. struct xfs_btree_cur *cur,
  52. union xfs_btree_ptr *start,
  53. union xfs_btree_ptr *new,
  54. int *stat)
  55. {
  56. struct xfs_buf *agbp = cur->bc_private.a.agbp;
  57. struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
  58. struct xfs_alloc_arg args; /* block allocation args */
  59. int error; /* error return value */
  60. memset(&args, 0, sizeof(args));
  61. args.tp = cur->bc_tp;
  62. args.mp = cur->bc_mp;
  63. args.type = XFS_ALLOCTYPE_NEAR_BNO;
  64. args.fsbno = XFS_AGB_TO_FSB(cur->bc_mp, cur->bc_private.a.agno,
  65. xfs_refc_block(args.mp));
  66. xfs_rmap_ag_owner(&args.oinfo, XFS_RMAP_OWN_REFC);
  67. args.minlen = args.maxlen = args.prod = 1;
  68. args.resv = XFS_AG_RESV_METADATA;
  69. error = xfs_alloc_vextent(&args);
  70. if (error)
  71. goto out_error;
  72. trace_xfs_refcountbt_alloc_block(cur->bc_mp, cur->bc_private.a.agno,
  73. args.agbno, 1);
  74. if (args.fsbno == NULLFSBLOCK) {
  75. *stat = 0;
  76. return 0;
  77. }
  78. ASSERT(args.agno == cur->bc_private.a.agno);
  79. ASSERT(args.len == 1);
  80. new->s = cpu_to_be32(args.agbno);
  81. be32_add_cpu(&agf->agf_refcount_blocks, 1);
  82. xfs_alloc_log_agf(cur->bc_tp, agbp, XFS_AGF_REFCOUNT_BLOCKS);
  83. *stat = 1;
  84. return 0;
  85. out_error:
  86. return error;
  87. }
  88. STATIC int
  89. xfs_refcountbt_free_block(
  90. struct xfs_btree_cur *cur,
  91. struct xfs_buf *bp)
  92. {
  93. struct xfs_mount *mp = cur->bc_mp;
  94. struct xfs_buf *agbp = cur->bc_private.a.agbp;
  95. struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
  96. xfs_fsblock_t fsbno = XFS_DADDR_TO_FSB(mp, XFS_BUF_ADDR(bp));
  97. struct xfs_owner_info oinfo;
  98. int error;
  99. trace_xfs_refcountbt_free_block(cur->bc_mp, cur->bc_private.a.agno,
  100. XFS_FSB_TO_AGBNO(cur->bc_mp, fsbno), 1);
  101. xfs_rmap_ag_owner(&oinfo, XFS_RMAP_OWN_REFC);
  102. be32_add_cpu(&agf->agf_refcount_blocks, -1);
  103. xfs_alloc_log_agf(cur->bc_tp, agbp, XFS_AGF_REFCOUNT_BLOCKS);
  104. error = xfs_free_extent(cur->bc_tp, fsbno, 1, &oinfo,
  105. XFS_AG_RESV_METADATA);
  106. if (error)
  107. return error;
  108. return error;
  109. }
  110. STATIC int
  111. xfs_refcountbt_get_minrecs(
  112. struct xfs_btree_cur *cur,
  113. int level)
  114. {
  115. return cur->bc_mp->m_refc_mnr[level != 0];
  116. }
  117. STATIC int
  118. xfs_refcountbt_get_maxrecs(
  119. struct xfs_btree_cur *cur,
  120. int level)
  121. {
  122. return cur->bc_mp->m_refc_mxr[level != 0];
  123. }
  124. STATIC void
  125. xfs_refcountbt_init_key_from_rec(
  126. union xfs_btree_key *key,
  127. union xfs_btree_rec *rec)
  128. {
  129. key->refc.rc_startblock = rec->refc.rc_startblock;
  130. }
  131. STATIC void
  132. xfs_refcountbt_init_high_key_from_rec(
  133. union xfs_btree_key *key,
  134. union xfs_btree_rec *rec)
  135. {
  136. __u32 x;
  137. x = be32_to_cpu(rec->refc.rc_startblock);
  138. x += be32_to_cpu(rec->refc.rc_blockcount) - 1;
  139. key->refc.rc_startblock = cpu_to_be32(x);
  140. }
  141. STATIC void
  142. xfs_refcountbt_init_rec_from_cur(
  143. struct xfs_btree_cur *cur,
  144. union xfs_btree_rec *rec)
  145. {
  146. rec->refc.rc_startblock = cpu_to_be32(cur->bc_rec.rc.rc_startblock);
  147. rec->refc.rc_blockcount = cpu_to_be32(cur->bc_rec.rc.rc_blockcount);
  148. rec->refc.rc_refcount = cpu_to_be32(cur->bc_rec.rc.rc_refcount);
  149. }
  150. STATIC void
  151. xfs_refcountbt_init_ptr_from_cur(
  152. struct xfs_btree_cur *cur,
  153. union xfs_btree_ptr *ptr)
  154. {
  155. struct xfs_agf *agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
  156. ASSERT(cur->bc_private.a.agno == be32_to_cpu(agf->agf_seqno));
  157. ptr->s = agf->agf_refcount_root;
  158. }
  159. STATIC int64_t
  160. xfs_refcountbt_key_diff(
  161. struct xfs_btree_cur *cur,
  162. union xfs_btree_key *key)
  163. {
  164. struct xfs_refcount_irec *rec = &cur->bc_rec.rc;
  165. struct xfs_refcount_key *kp = &key->refc;
  166. return (int64_t)be32_to_cpu(kp->rc_startblock) - rec->rc_startblock;
  167. }
  168. STATIC int64_t
  169. xfs_refcountbt_diff_two_keys(
  170. struct xfs_btree_cur *cur,
  171. union xfs_btree_key *k1,
  172. union xfs_btree_key *k2)
  173. {
  174. return (int64_t)be32_to_cpu(k1->refc.rc_startblock) -
  175. be32_to_cpu(k2->refc.rc_startblock);
  176. }
  177. STATIC xfs_failaddr_t
  178. xfs_refcountbt_verify(
  179. struct xfs_buf *bp)
  180. {
  181. struct xfs_mount *mp = bp->b_target->bt_mount;
  182. struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
  183. struct xfs_perag *pag = bp->b_pag;
  184. xfs_failaddr_t fa;
  185. unsigned int level;
  186. if (block->bb_magic != cpu_to_be32(XFS_REFC_CRC_MAGIC))
  187. return __this_address;
  188. if (!xfs_sb_version_hasreflink(&mp->m_sb))
  189. return __this_address;
  190. fa = xfs_btree_sblock_v5hdr_verify(bp);
  191. if (fa)
  192. return fa;
  193. level = be16_to_cpu(block->bb_level);
  194. if (pag && pag->pagf_init) {
  195. if (level >= pag->pagf_refcount_level)
  196. return __this_address;
  197. } else if (level >= mp->m_refc_maxlevels)
  198. return __this_address;
  199. return xfs_btree_sblock_verify(bp, mp->m_refc_mxr[level != 0]);
  200. }
  201. STATIC void
  202. xfs_refcountbt_read_verify(
  203. struct xfs_buf *bp)
  204. {
  205. xfs_failaddr_t fa;
  206. if (!xfs_btree_sblock_verify_crc(bp))
  207. xfs_verifier_error(bp, -EFSBADCRC, __this_address);
  208. else {
  209. fa = xfs_refcountbt_verify(bp);
  210. if (fa)
  211. xfs_verifier_error(bp, -EFSCORRUPTED, fa);
  212. }
  213. if (bp->b_error)
  214. trace_xfs_btree_corrupt(bp, _RET_IP_);
  215. }
  216. STATIC void
  217. xfs_refcountbt_write_verify(
  218. struct xfs_buf *bp)
  219. {
  220. xfs_failaddr_t fa;
  221. fa = xfs_refcountbt_verify(bp);
  222. if (fa) {
  223. trace_xfs_btree_corrupt(bp, _RET_IP_);
  224. xfs_verifier_error(bp, -EFSCORRUPTED, fa);
  225. return;
  226. }
  227. xfs_btree_sblock_calc_crc(bp);
  228. }
  229. const struct xfs_buf_ops xfs_refcountbt_buf_ops = {
  230. .name = "xfs_refcountbt",
  231. .verify_read = xfs_refcountbt_read_verify,
  232. .verify_write = xfs_refcountbt_write_verify,
  233. .verify_struct = xfs_refcountbt_verify,
  234. };
  235. STATIC int
  236. xfs_refcountbt_keys_inorder(
  237. struct xfs_btree_cur *cur,
  238. union xfs_btree_key *k1,
  239. union xfs_btree_key *k2)
  240. {
  241. return be32_to_cpu(k1->refc.rc_startblock) <
  242. be32_to_cpu(k2->refc.rc_startblock);
  243. }
  244. STATIC int
  245. xfs_refcountbt_recs_inorder(
  246. struct xfs_btree_cur *cur,
  247. union xfs_btree_rec *r1,
  248. union xfs_btree_rec *r2)
  249. {
  250. return be32_to_cpu(r1->refc.rc_startblock) +
  251. be32_to_cpu(r1->refc.rc_blockcount) <=
  252. be32_to_cpu(r2->refc.rc_startblock);
  253. }
  254. static const struct xfs_btree_ops xfs_refcountbt_ops = {
  255. .rec_len = sizeof(struct xfs_refcount_rec),
  256. .key_len = sizeof(struct xfs_refcount_key),
  257. .dup_cursor = xfs_refcountbt_dup_cursor,
  258. .set_root = xfs_refcountbt_set_root,
  259. .alloc_block = xfs_refcountbt_alloc_block,
  260. .free_block = xfs_refcountbt_free_block,
  261. .get_minrecs = xfs_refcountbt_get_minrecs,
  262. .get_maxrecs = xfs_refcountbt_get_maxrecs,
  263. .init_key_from_rec = xfs_refcountbt_init_key_from_rec,
  264. .init_high_key_from_rec = xfs_refcountbt_init_high_key_from_rec,
  265. .init_rec_from_cur = xfs_refcountbt_init_rec_from_cur,
  266. .init_ptr_from_cur = xfs_refcountbt_init_ptr_from_cur,
  267. .key_diff = xfs_refcountbt_key_diff,
  268. .buf_ops = &xfs_refcountbt_buf_ops,
  269. .diff_two_keys = xfs_refcountbt_diff_two_keys,
  270. .keys_inorder = xfs_refcountbt_keys_inorder,
  271. .recs_inorder = xfs_refcountbt_recs_inorder,
  272. };
  273. /*
  274. * Allocate a new refcount btree cursor.
  275. */
  276. struct xfs_btree_cur *
  277. xfs_refcountbt_init_cursor(
  278. struct xfs_mount *mp,
  279. struct xfs_trans *tp,
  280. struct xfs_buf *agbp,
  281. xfs_agnumber_t agno)
  282. {
  283. struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
  284. struct xfs_btree_cur *cur;
  285. ASSERT(agno != NULLAGNUMBER);
  286. ASSERT(agno < mp->m_sb.sb_agcount);
  287. cur = kmem_zone_zalloc(xfs_btree_cur_zone, KM_NOFS);
  288. cur->bc_tp = tp;
  289. cur->bc_mp = mp;
  290. cur->bc_btnum = XFS_BTNUM_REFC;
  291. cur->bc_blocklog = mp->m_sb.sb_blocklog;
  292. cur->bc_ops = &xfs_refcountbt_ops;
  293. cur->bc_statoff = XFS_STATS_CALC_INDEX(xs_refcbt_2);
  294. cur->bc_nlevels = be32_to_cpu(agf->agf_refcount_level);
  295. cur->bc_private.a.agbp = agbp;
  296. cur->bc_private.a.agno = agno;
  297. cur->bc_flags |= XFS_BTREE_CRC_BLOCKS;
  298. cur->bc_private.a.priv.refc.nr_ops = 0;
  299. cur->bc_private.a.priv.refc.shape_changes = 0;
  300. return cur;
  301. }
  302. /*
  303. * Calculate the number of records in a refcount btree block.
  304. */
  305. int
  306. xfs_refcountbt_maxrecs(
  307. int blocklen,
  308. bool leaf)
  309. {
  310. blocklen -= XFS_REFCOUNT_BLOCK_LEN;
  311. if (leaf)
  312. return blocklen / sizeof(struct xfs_refcount_rec);
  313. return blocklen / (sizeof(struct xfs_refcount_key) +
  314. sizeof(xfs_refcount_ptr_t));
  315. }
  316. /* Compute the maximum height of a refcount btree. */
  317. void
  318. xfs_refcountbt_compute_maxlevels(
  319. struct xfs_mount *mp)
  320. {
  321. mp->m_refc_maxlevels = xfs_btree_compute_maxlevels(
  322. mp->m_refc_mnr, mp->m_sb.sb_agblocks);
  323. }
  324. /* Calculate the refcount btree size for some records. */
  325. xfs_extlen_t
  326. xfs_refcountbt_calc_size(
  327. struct xfs_mount *mp,
  328. unsigned long long len)
  329. {
  330. return xfs_btree_calc_size(mp->m_refc_mnr, len);
  331. }
  332. /*
  333. * Calculate the maximum refcount btree size.
  334. */
  335. xfs_extlen_t
  336. xfs_refcountbt_max_size(
  337. struct xfs_mount *mp,
  338. xfs_agblock_t agblocks)
  339. {
  340. /* Bail out if we're uninitialized, which can happen in mkfs. */
  341. if (mp->m_refc_mxr[0] == 0)
  342. return 0;
  343. return xfs_refcountbt_calc_size(mp, agblocks);
  344. }
  345. /*
  346. * Figure out how many blocks to reserve and how many are used by this btree.
  347. */
  348. int
  349. xfs_refcountbt_calc_reserves(
  350. struct xfs_mount *mp,
  351. struct xfs_trans *tp,
  352. xfs_agnumber_t agno,
  353. xfs_extlen_t *ask,
  354. xfs_extlen_t *used)
  355. {
  356. struct xfs_buf *agbp;
  357. struct xfs_agf *agf;
  358. xfs_agblock_t agblocks;
  359. xfs_extlen_t tree_len;
  360. int error;
  361. if (!xfs_sb_version_hasreflink(&mp->m_sb))
  362. return 0;
  363. error = xfs_alloc_read_agf(mp, tp, agno, 0, &agbp);
  364. if (error)
  365. return error;
  366. agf = XFS_BUF_TO_AGF(agbp);
  367. agblocks = be32_to_cpu(agf->agf_length);
  368. tree_len = be32_to_cpu(agf->agf_refcount_blocks);
  369. xfs_trans_brelse(tp, agbp);
  370. *ask += xfs_refcountbt_max_size(mp, agblocks);
  371. *used += tree_len;
  372. return error;
  373. }