xfs_refcount_btree.c 11 KB

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