xfs_refcount_btree.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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. #if defined(DEBUG) || defined(XFS_WARN)
  251. STATIC int
  252. xfs_refcountbt_keys_inorder(
  253. struct xfs_btree_cur *cur,
  254. union xfs_btree_key *k1,
  255. union xfs_btree_key *k2)
  256. {
  257. return be32_to_cpu(k1->refc.rc_startblock) <
  258. be32_to_cpu(k2->refc.rc_startblock);
  259. }
  260. STATIC int
  261. xfs_refcountbt_recs_inorder(
  262. struct xfs_btree_cur *cur,
  263. union xfs_btree_rec *r1,
  264. union xfs_btree_rec *r2)
  265. {
  266. return be32_to_cpu(r1->refc.rc_startblock) +
  267. be32_to_cpu(r1->refc.rc_blockcount) <=
  268. be32_to_cpu(r2->refc.rc_startblock);
  269. }
  270. #endif
  271. static const struct xfs_btree_ops xfs_refcountbt_ops = {
  272. .rec_len = sizeof(struct xfs_refcount_rec),
  273. .key_len = sizeof(struct xfs_refcount_key),
  274. .dup_cursor = xfs_refcountbt_dup_cursor,
  275. .set_root = xfs_refcountbt_set_root,
  276. .alloc_block = xfs_refcountbt_alloc_block,
  277. .free_block = xfs_refcountbt_free_block,
  278. .get_minrecs = xfs_refcountbt_get_minrecs,
  279. .get_maxrecs = xfs_refcountbt_get_maxrecs,
  280. .init_key_from_rec = xfs_refcountbt_init_key_from_rec,
  281. .init_high_key_from_rec = xfs_refcountbt_init_high_key_from_rec,
  282. .init_rec_from_cur = xfs_refcountbt_init_rec_from_cur,
  283. .init_ptr_from_cur = xfs_refcountbt_init_ptr_from_cur,
  284. .key_diff = xfs_refcountbt_key_diff,
  285. .buf_ops = &xfs_refcountbt_buf_ops,
  286. .diff_two_keys = xfs_refcountbt_diff_two_keys,
  287. #if defined(DEBUG) || defined(XFS_WARN)
  288. .keys_inorder = xfs_refcountbt_keys_inorder,
  289. .recs_inorder = xfs_refcountbt_recs_inorder,
  290. #endif
  291. };
  292. /*
  293. * Allocate a new refcount btree cursor.
  294. */
  295. struct xfs_btree_cur *
  296. xfs_refcountbt_init_cursor(
  297. struct xfs_mount *mp,
  298. struct xfs_trans *tp,
  299. struct xfs_buf *agbp,
  300. xfs_agnumber_t agno,
  301. struct xfs_defer_ops *dfops)
  302. {
  303. struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
  304. struct xfs_btree_cur *cur;
  305. ASSERT(agno != NULLAGNUMBER);
  306. ASSERT(agno < mp->m_sb.sb_agcount);
  307. cur = kmem_zone_zalloc(xfs_btree_cur_zone, KM_NOFS);
  308. cur->bc_tp = tp;
  309. cur->bc_mp = mp;
  310. cur->bc_btnum = XFS_BTNUM_REFC;
  311. cur->bc_blocklog = mp->m_sb.sb_blocklog;
  312. cur->bc_ops = &xfs_refcountbt_ops;
  313. cur->bc_nlevels = be32_to_cpu(agf->agf_refcount_level);
  314. cur->bc_private.a.agbp = agbp;
  315. cur->bc_private.a.agno = agno;
  316. cur->bc_private.a.dfops = dfops;
  317. cur->bc_flags |= XFS_BTREE_CRC_BLOCKS;
  318. cur->bc_private.a.priv.refc.nr_ops = 0;
  319. cur->bc_private.a.priv.refc.shape_changes = 0;
  320. return cur;
  321. }
  322. /*
  323. * Calculate the number of records in a refcount btree block.
  324. */
  325. int
  326. xfs_refcountbt_maxrecs(
  327. struct xfs_mount *mp,
  328. int blocklen,
  329. bool leaf)
  330. {
  331. blocklen -= XFS_REFCOUNT_BLOCK_LEN;
  332. if (leaf)
  333. return blocklen / sizeof(struct xfs_refcount_rec);
  334. return blocklen / (sizeof(struct xfs_refcount_key) +
  335. sizeof(xfs_refcount_ptr_t));
  336. }
  337. /* Compute the maximum height of a refcount btree. */
  338. void
  339. xfs_refcountbt_compute_maxlevels(
  340. struct xfs_mount *mp)
  341. {
  342. mp->m_refc_maxlevels = xfs_btree_compute_maxlevels(mp,
  343. mp->m_refc_mnr, mp->m_sb.sb_agblocks);
  344. }
  345. /* Calculate the refcount btree size for some records. */
  346. xfs_extlen_t
  347. xfs_refcountbt_calc_size(
  348. struct xfs_mount *mp,
  349. unsigned long long len)
  350. {
  351. return xfs_btree_calc_size(mp, mp->m_refc_mnr, len);
  352. }
  353. /*
  354. * Calculate the maximum refcount btree size.
  355. */
  356. xfs_extlen_t
  357. xfs_refcountbt_max_size(
  358. struct xfs_mount *mp,
  359. xfs_agblock_t agblocks)
  360. {
  361. /* Bail out if we're uninitialized, which can happen in mkfs. */
  362. if (mp->m_refc_mxr[0] == 0)
  363. return 0;
  364. return xfs_refcountbt_calc_size(mp, agblocks);
  365. }
  366. /*
  367. * Figure out how many blocks to reserve and how many are used by this btree.
  368. */
  369. int
  370. xfs_refcountbt_calc_reserves(
  371. struct xfs_mount *mp,
  372. xfs_agnumber_t agno,
  373. xfs_extlen_t *ask,
  374. xfs_extlen_t *used)
  375. {
  376. struct xfs_buf *agbp;
  377. struct xfs_agf *agf;
  378. xfs_agblock_t agblocks;
  379. xfs_extlen_t tree_len;
  380. int error;
  381. if (!xfs_sb_version_hasreflink(&mp->m_sb))
  382. return 0;
  383. error = xfs_alloc_read_agf(mp, NULL, agno, 0, &agbp);
  384. if (error)
  385. return error;
  386. agf = XFS_BUF_TO_AGF(agbp);
  387. agblocks = be32_to_cpu(agf->agf_length);
  388. tree_len = be32_to_cpu(agf->agf_refcount_blocks);
  389. xfs_buf_relse(agbp);
  390. *ask += xfs_refcountbt_max_size(mp, agblocks);
  391. *used += tree_len;
  392. return error;
  393. }