xfs_refcount_btree.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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_mount.h"
  13. #include "xfs_btree.h"
  14. #include "xfs_btree_staging.h"
  15. #include "xfs_refcount_btree.h"
  16. #include "xfs_refcount.h"
  17. #include "xfs_alloc.h"
  18. #include "xfs_error.h"
  19. #include "xfs_health.h"
  20. #include "xfs_trace.h"
  21. #include "xfs_trans.h"
  22. #include "xfs_bit.h"
  23. #include "xfs_rmap.h"
  24. #include "xfs_ag.h"
  25. static struct kmem_cache *xfs_refcountbt_cur_cache;
  26. static struct xfs_btree_cur *
  27. xfs_refcountbt_dup_cursor(
  28. struct xfs_btree_cur *cur)
  29. {
  30. return xfs_refcountbt_init_cursor(cur->bc_mp, cur->bc_tp,
  31. cur->bc_ag.agbp, cur->bc_ag.pag);
  32. }
  33. STATIC void
  34. xfs_refcountbt_set_root(
  35. struct xfs_btree_cur *cur,
  36. const union xfs_btree_ptr *ptr,
  37. int inc)
  38. {
  39. struct xfs_buf *agbp = cur->bc_ag.agbp;
  40. struct xfs_agf *agf = agbp->b_addr;
  41. struct xfs_perag *pag = agbp->b_pag;
  42. ASSERT(ptr->s != 0);
  43. agf->agf_refcount_root = ptr->s;
  44. be32_add_cpu(&agf->agf_refcount_level, inc);
  45. pag->pagf_refcount_level += inc;
  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. const union xfs_btree_ptr *start,
  53. union xfs_btree_ptr *new,
  54. int *stat)
  55. {
  56. struct xfs_buf *agbp = cur->bc_ag.agbp;
  57. struct xfs_agf *agf = agbp->b_addr;
  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.pag = cur->bc_ag.pag;
  64. args.oinfo = XFS_RMAP_OINFO_REFC;
  65. args.minlen = args.maxlen = args.prod = 1;
  66. args.resv = XFS_AG_RESV_METADATA;
  67. error = xfs_alloc_vextent_near_bno(&args,
  68. XFS_AGB_TO_FSB(args.mp, args.pag->pag_agno,
  69. xfs_refc_block(args.mp)));
  70. if (error)
  71. goto out_error;
  72. if (args.fsbno == NULLFSBLOCK) {
  73. *stat = 0;
  74. return 0;
  75. }
  76. ASSERT(args.agno == cur->bc_ag.pag->pag_agno);
  77. ASSERT(args.len == 1);
  78. new->s = cpu_to_be32(args.agbno);
  79. be32_add_cpu(&agf->agf_refcount_blocks, 1);
  80. xfs_alloc_log_agf(cur->bc_tp, agbp, XFS_AGF_REFCOUNT_BLOCKS);
  81. *stat = 1;
  82. return 0;
  83. out_error:
  84. return error;
  85. }
  86. STATIC int
  87. xfs_refcountbt_free_block(
  88. struct xfs_btree_cur *cur,
  89. struct xfs_buf *bp)
  90. {
  91. struct xfs_mount *mp = cur->bc_mp;
  92. struct xfs_buf *agbp = cur->bc_ag.agbp;
  93. struct xfs_agf *agf = agbp->b_addr;
  94. xfs_fsblock_t fsbno = XFS_DADDR_TO_FSB(mp, xfs_buf_daddr(bp));
  95. be32_add_cpu(&agf->agf_refcount_blocks, -1);
  96. xfs_alloc_log_agf(cur->bc_tp, agbp, XFS_AGF_REFCOUNT_BLOCKS);
  97. return xfs_free_extent_later(cur->bc_tp, fsbno, 1,
  98. &XFS_RMAP_OINFO_REFC, XFS_AG_RESV_METADATA, 0);
  99. }
  100. STATIC int
  101. xfs_refcountbt_get_minrecs(
  102. struct xfs_btree_cur *cur,
  103. int level)
  104. {
  105. return cur->bc_mp->m_refc_mnr[level != 0];
  106. }
  107. STATIC int
  108. xfs_refcountbt_get_maxrecs(
  109. struct xfs_btree_cur *cur,
  110. int level)
  111. {
  112. return cur->bc_mp->m_refc_mxr[level != 0];
  113. }
  114. STATIC void
  115. xfs_refcountbt_init_key_from_rec(
  116. union xfs_btree_key *key,
  117. const union xfs_btree_rec *rec)
  118. {
  119. key->refc.rc_startblock = rec->refc.rc_startblock;
  120. }
  121. STATIC void
  122. xfs_refcountbt_init_high_key_from_rec(
  123. union xfs_btree_key *key,
  124. const union xfs_btree_rec *rec)
  125. {
  126. __u32 x;
  127. x = be32_to_cpu(rec->refc.rc_startblock);
  128. x += be32_to_cpu(rec->refc.rc_blockcount) - 1;
  129. key->refc.rc_startblock = cpu_to_be32(x);
  130. }
  131. STATIC void
  132. xfs_refcountbt_init_rec_from_cur(
  133. struct xfs_btree_cur *cur,
  134. union xfs_btree_rec *rec)
  135. {
  136. const struct xfs_refcount_irec *irec = &cur->bc_rec.rc;
  137. uint32_t start;
  138. start = xfs_refcount_encode_startblock(irec->rc_startblock,
  139. irec->rc_domain);
  140. rec->refc.rc_startblock = cpu_to_be32(start);
  141. rec->refc.rc_blockcount = cpu_to_be32(cur->bc_rec.rc.rc_blockcount);
  142. rec->refc.rc_refcount = cpu_to_be32(cur->bc_rec.rc.rc_refcount);
  143. }
  144. STATIC void
  145. xfs_refcountbt_init_ptr_from_cur(
  146. struct xfs_btree_cur *cur,
  147. union xfs_btree_ptr *ptr)
  148. {
  149. struct xfs_agf *agf = cur->bc_ag.agbp->b_addr;
  150. ASSERT(cur->bc_ag.pag->pag_agno == be32_to_cpu(agf->agf_seqno));
  151. ptr->s = agf->agf_refcount_root;
  152. }
  153. STATIC int64_t
  154. xfs_refcountbt_key_diff(
  155. struct xfs_btree_cur *cur,
  156. const union xfs_btree_key *key)
  157. {
  158. const struct xfs_refcount_key *kp = &key->refc;
  159. const struct xfs_refcount_irec *irec = &cur->bc_rec.rc;
  160. uint32_t start;
  161. start = xfs_refcount_encode_startblock(irec->rc_startblock,
  162. irec->rc_domain);
  163. return (int64_t)be32_to_cpu(kp->rc_startblock) - start;
  164. }
  165. STATIC int64_t
  166. xfs_refcountbt_diff_two_keys(
  167. struct xfs_btree_cur *cur,
  168. const union xfs_btree_key *k1,
  169. const union xfs_btree_key *k2,
  170. const union xfs_btree_key *mask)
  171. {
  172. ASSERT(!mask || mask->refc.rc_startblock);
  173. return (int64_t)be32_to_cpu(k1->refc.rc_startblock) -
  174. be32_to_cpu(k2->refc.rc_startblock);
  175. }
  176. STATIC xfs_failaddr_t
  177. xfs_refcountbt_verify(
  178. struct xfs_buf *bp)
  179. {
  180. struct xfs_mount *mp = bp->b_mount;
  181. struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
  182. struct xfs_perag *pag = bp->b_pag;
  183. xfs_failaddr_t fa;
  184. unsigned int level;
  185. if (!xfs_verify_magic(bp, block->bb_magic))
  186. return __this_address;
  187. if (!xfs_has_reflink(mp))
  188. return __this_address;
  189. fa = xfs_btree_agblock_v5hdr_verify(bp);
  190. if (fa)
  191. return fa;
  192. level = be16_to_cpu(block->bb_level);
  193. if (pag && xfs_perag_initialised_agf(pag)) {
  194. unsigned int maxlevel = pag->pagf_refcount_level;
  195. #ifdef CONFIG_XFS_ONLINE_REPAIR
  196. /*
  197. * Online repair could be rewriting the refcount btree, so
  198. * we'll validate against the larger of either tree while this
  199. * is going on.
  200. */
  201. maxlevel = max_t(unsigned int, maxlevel,
  202. pag->pagf_repair_refcount_level);
  203. #endif
  204. if (level >= maxlevel)
  205. return __this_address;
  206. } else if (level >= mp->m_refc_maxlevels)
  207. return __this_address;
  208. return xfs_btree_agblock_verify(bp, mp->m_refc_mxr[level != 0]);
  209. }
  210. STATIC void
  211. xfs_refcountbt_read_verify(
  212. struct xfs_buf *bp)
  213. {
  214. xfs_failaddr_t fa;
  215. if (!xfs_btree_agblock_verify_crc(bp))
  216. xfs_verifier_error(bp, -EFSBADCRC, __this_address);
  217. else {
  218. fa = xfs_refcountbt_verify(bp);
  219. if (fa)
  220. xfs_verifier_error(bp, -EFSCORRUPTED, fa);
  221. }
  222. if (bp->b_error)
  223. trace_xfs_btree_corrupt(bp, _RET_IP_);
  224. }
  225. STATIC void
  226. xfs_refcountbt_write_verify(
  227. struct xfs_buf *bp)
  228. {
  229. xfs_failaddr_t fa;
  230. fa = xfs_refcountbt_verify(bp);
  231. if (fa) {
  232. trace_xfs_btree_corrupt(bp, _RET_IP_);
  233. xfs_verifier_error(bp, -EFSCORRUPTED, fa);
  234. return;
  235. }
  236. xfs_btree_agblock_calc_crc(bp);
  237. }
  238. const struct xfs_buf_ops xfs_refcountbt_buf_ops = {
  239. .name = "xfs_refcountbt",
  240. .magic = { 0, cpu_to_be32(XFS_REFC_CRC_MAGIC) },
  241. .verify_read = xfs_refcountbt_read_verify,
  242. .verify_write = xfs_refcountbt_write_verify,
  243. .verify_struct = xfs_refcountbt_verify,
  244. };
  245. STATIC int
  246. xfs_refcountbt_keys_inorder(
  247. struct xfs_btree_cur *cur,
  248. const union xfs_btree_key *k1,
  249. const union xfs_btree_key *k2)
  250. {
  251. return be32_to_cpu(k1->refc.rc_startblock) <
  252. be32_to_cpu(k2->refc.rc_startblock);
  253. }
  254. STATIC int
  255. xfs_refcountbt_recs_inorder(
  256. struct xfs_btree_cur *cur,
  257. const union xfs_btree_rec *r1,
  258. const union xfs_btree_rec *r2)
  259. {
  260. return be32_to_cpu(r1->refc.rc_startblock) +
  261. be32_to_cpu(r1->refc.rc_blockcount) <=
  262. be32_to_cpu(r2->refc.rc_startblock);
  263. }
  264. STATIC enum xbtree_key_contig
  265. xfs_refcountbt_keys_contiguous(
  266. struct xfs_btree_cur *cur,
  267. const union xfs_btree_key *key1,
  268. const union xfs_btree_key *key2,
  269. const union xfs_btree_key *mask)
  270. {
  271. ASSERT(!mask || mask->refc.rc_startblock);
  272. return xbtree_key_contig(be32_to_cpu(key1->refc.rc_startblock),
  273. be32_to_cpu(key2->refc.rc_startblock));
  274. }
  275. const struct xfs_btree_ops xfs_refcountbt_ops = {
  276. .name = "refcount",
  277. .type = XFS_BTREE_TYPE_AG,
  278. .rec_len = sizeof(struct xfs_refcount_rec),
  279. .key_len = sizeof(struct xfs_refcount_key),
  280. .ptr_len = XFS_BTREE_SHORT_PTR_LEN,
  281. .lru_refs = XFS_REFC_BTREE_REF,
  282. .statoff = XFS_STATS_CALC_INDEX(xs_refcbt_2),
  283. .sick_mask = XFS_SICK_AG_REFCNTBT,
  284. .dup_cursor = xfs_refcountbt_dup_cursor,
  285. .set_root = xfs_refcountbt_set_root,
  286. .alloc_block = xfs_refcountbt_alloc_block,
  287. .free_block = xfs_refcountbt_free_block,
  288. .get_minrecs = xfs_refcountbt_get_minrecs,
  289. .get_maxrecs = xfs_refcountbt_get_maxrecs,
  290. .init_key_from_rec = xfs_refcountbt_init_key_from_rec,
  291. .init_high_key_from_rec = xfs_refcountbt_init_high_key_from_rec,
  292. .init_rec_from_cur = xfs_refcountbt_init_rec_from_cur,
  293. .init_ptr_from_cur = xfs_refcountbt_init_ptr_from_cur,
  294. .key_diff = xfs_refcountbt_key_diff,
  295. .buf_ops = &xfs_refcountbt_buf_ops,
  296. .diff_two_keys = xfs_refcountbt_diff_two_keys,
  297. .keys_inorder = xfs_refcountbt_keys_inorder,
  298. .recs_inorder = xfs_refcountbt_recs_inorder,
  299. .keys_contiguous = xfs_refcountbt_keys_contiguous,
  300. };
  301. /*
  302. * Create a new refcount btree cursor.
  303. *
  304. * For staging cursors tp and agbp are NULL.
  305. */
  306. struct xfs_btree_cur *
  307. xfs_refcountbt_init_cursor(
  308. struct xfs_mount *mp,
  309. struct xfs_trans *tp,
  310. struct xfs_buf *agbp,
  311. struct xfs_perag *pag)
  312. {
  313. struct xfs_btree_cur *cur;
  314. ASSERT(pag->pag_agno < mp->m_sb.sb_agcount);
  315. cur = xfs_btree_alloc_cursor(mp, tp, &xfs_refcountbt_ops,
  316. mp->m_refc_maxlevels, xfs_refcountbt_cur_cache);
  317. cur->bc_ag.pag = xfs_perag_hold(pag);
  318. cur->bc_refc.nr_ops = 0;
  319. cur->bc_refc.shape_changes = 0;
  320. cur->bc_ag.agbp = agbp;
  321. if (agbp) {
  322. struct xfs_agf *agf = agbp->b_addr;
  323. cur->bc_nlevels = be32_to_cpu(agf->agf_refcount_level);
  324. }
  325. return cur;
  326. }
  327. /*
  328. * Swap in the new btree root. Once we pass this point the newly rebuilt btree
  329. * is in place and we have to kill off all the old btree blocks.
  330. */
  331. void
  332. xfs_refcountbt_commit_staged_btree(
  333. struct xfs_btree_cur *cur,
  334. struct xfs_trans *tp,
  335. struct xfs_buf *agbp)
  336. {
  337. struct xfs_agf *agf = agbp->b_addr;
  338. struct xbtree_afakeroot *afake = cur->bc_ag.afake;
  339. ASSERT(cur->bc_flags & XFS_BTREE_STAGING);
  340. agf->agf_refcount_root = cpu_to_be32(afake->af_root);
  341. agf->agf_refcount_level = cpu_to_be32(afake->af_levels);
  342. agf->agf_refcount_blocks = cpu_to_be32(afake->af_blocks);
  343. xfs_alloc_log_agf(tp, agbp, XFS_AGF_REFCOUNT_BLOCKS |
  344. XFS_AGF_REFCOUNT_ROOT |
  345. XFS_AGF_REFCOUNT_LEVEL);
  346. xfs_btree_commit_afakeroot(cur, tp, agbp);
  347. }
  348. /* Calculate number of records in a refcount btree block. */
  349. static inline unsigned int
  350. xfs_refcountbt_block_maxrecs(
  351. unsigned int blocklen,
  352. bool leaf)
  353. {
  354. if (leaf)
  355. return blocklen / sizeof(struct xfs_refcount_rec);
  356. return blocklen / (sizeof(struct xfs_refcount_key) +
  357. sizeof(xfs_refcount_ptr_t));
  358. }
  359. /*
  360. * Calculate the number of records in a refcount btree block.
  361. */
  362. unsigned int
  363. xfs_refcountbt_maxrecs(
  364. struct xfs_mount *mp,
  365. unsigned int blocklen,
  366. bool leaf)
  367. {
  368. blocklen -= XFS_REFCOUNT_BLOCK_LEN;
  369. return xfs_refcountbt_block_maxrecs(blocklen, leaf);
  370. }
  371. /* Compute the max possible height of the maximally sized refcount btree. */
  372. unsigned int
  373. xfs_refcountbt_maxlevels_ondisk(void)
  374. {
  375. unsigned int minrecs[2];
  376. unsigned int blocklen;
  377. blocklen = XFS_MIN_CRC_BLOCKSIZE - XFS_BTREE_SBLOCK_CRC_LEN;
  378. minrecs[0] = xfs_refcountbt_block_maxrecs(blocklen, true) / 2;
  379. minrecs[1] = xfs_refcountbt_block_maxrecs(blocklen, false) / 2;
  380. return xfs_btree_compute_maxlevels(minrecs, XFS_MAX_CRC_AG_BLOCKS);
  381. }
  382. /* Compute the maximum height of a refcount btree. */
  383. void
  384. xfs_refcountbt_compute_maxlevels(
  385. struct xfs_mount *mp)
  386. {
  387. if (!xfs_has_reflink(mp)) {
  388. mp->m_refc_maxlevels = 0;
  389. return;
  390. }
  391. mp->m_refc_maxlevels = xfs_btree_compute_maxlevels(
  392. mp->m_refc_mnr, mp->m_sb.sb_agblocks);
  393. ASSERT(mp->m_refc_maxlevels <= xfs_refcountbt_maxlevels_ondisk());
  394. }
  395. /* Calculate the refcount btree size for some records. */
  396. xfs_extlen_t
  397. xfs_refcountbt_calc_size(
  398. struct xfs_mount *mp,
  399. unsigned long long len)
  400. {
  401. return xfs_btree_calc_size(mp->m_refc_mnr, len);
  402. }
  403. /*
  404. * Calculate the maximum refcount btree size.
  405. */
  406. xfs_extlen_t
  407. xfs_refcountbt_max_size(
  408. struct xfs_mount *mp,
  409. xfs_agblock_t agblocks)
  410. {
  411. /* Bail out if we're uninitialized, which can happen in mkfs. */
  412. if (mp->m_refc_mxr[0] == 0)
  413. return 0;
  414. return xfs_refcountbt_calc_size(mp, agblocks);
  415. }
  416. /*
  417. * Figure out how many blocks to reserve and how many are used by this btree.
  418. */
  419. int
  420. xfs_refcountbt_calc_reserves(
  421. struct xfs_mount *mp,
  422. struct xfs_trans *tp,
  423. struct xfs_perag *pag,
  424. xfs_extlen_t *ask,
  425. xfs_extlen_t *used)
  426. {
  427. struct xfs_buf *agbp;
  428. struct xfs_agf *agf;
  429. xfs_agblock_t agblocks;
  430. xfs_extlen_t tree_len;
  431. int error;
  432. if (!xfs_has_reflink(mp))
  433. return 0;
  434. error = xfs_alloc_read_agf(pag, tp, 0, &agbp);
  435. if (error)
  436. return error;
  437. agf = agbp->b_addr;
  438. agblocks = be32_to_cpu(agf->agf_length);
  439. tree_len = be32_to_cpu(agf->agf_refcount_blocks);
  440. xfs_trans_brelse(tp, agbp);
  441. /*
  442. * The log is permanently allocated, so the space it occupies will
  443. * never be available for the kinds of things that would require btree
  444. * expansion. We therefore can pretend the space isn't there.
  445. */
  446. if (xfs_ag_contains_log(mp, pag->pag_agno))
  447. agblocks -= mp->m_sb.sb_logblocks;
  448. *ask += xfs_refcountbt_max_size(mp, agblocks);
  449. *used += tree_len;
  450. return error;
  451. }
  452. int __init
  453. xfs_refcountbt_init_cur_cache(void)
  454. {
  455. xfs_refcountbt_cur_cache = kmem_cache_create("xfs_refcbt_cur",
  456. xfs_btree_cur_sizeof(xfs_refcountbt_maxlevels_ondisk()),
  457. 0, 0, NULL);
  458. if (!xfs_refcountbt_cur_cache)
  459. return -ENOMEM;
  460. return 0;
  461. }
  462. void
  463. xfs_refcountbt_destroy_cur_cache(void)
  464. {
  465. kmem_cache_destroy(xfs_refcountbt_cur_cache);
  466. xfs_refcountbt_cur_cache = NULL;
  467. }