xfs_rmap_btree.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. /*
  2. * Copyright (c) 2014 Red Hat, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write the Free Software Foundation,
  16. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "xfs.h"
  19. #include "xfs_fs.h"
  20. #include "xfs_shared.h"
  21. #include "xfs_format.h"
  22. #include "xfs_log_format.h"
  23. #include "xfs_trans_resv.h"
  24. #include "xfs_bit.h"
  25. #include "xfs_sb.h"
  26. #include "xfs_mount.h"
  27. #include "xfs_defer.h"
  28. #include "xfs_inode.h"
  29. #include "xfs_trans.h"
  30. #include "xfs_alloc.h"
  31. #include "xfs_btree.h"
  32. #include "xfs_rmap.h"
  33. #include "xfs_rmap_btree.h"
  34. #include "xfs_trace.h"
  35. #include "xfs_cksum.h"
  36. #include "xfs_error.h"
  37. #include "xfs_extent_busy.h"
  38. #include "xfs_ag_resv.h"
  39. /*
  40. * Reverse map btree.
  41. *
  42. * This is a per-ag tree used to track the owner(s) of a given extent. With
  43. * reflink it is possible for there to be multiple owners, which is a departure
  44. * from classic XFS. Owner records for data extents are inserted when the
  45. * extent is mapped and removed when an extent is unmapped. Owner records for
  46. * all other block types (i.e. metadata) are inserted when an extent is
  47. * allocated and removed when an extent is freed. There can only be one owner
  48. * of a metadata extent, usually an inode or some other metadata structure like
  49. * an AG btree.
  50. *
  51. * The rmap btree is part of the free space management, so blocks for the tree
  52. * are sourced from the agfl. Hence we need transaction reservation support for
  53. * this tree so that the freelist is always large enough. This also impacts on
  54. * the minimum space we need to leave free in the AG.
  55. *
  56. * The tree is ordered by [ag block, owner, offset]. This is a large key size,
  57. * but it is the only way to enforce unique keys when a block can be owned by
  58. * multiple files at any offset. There's no need to order/search by extent
  59. * size for online updating/management of the tree. It is intended that most
  60. * reverse lookups will be to find the owner(s) of a particular block, or to
  61. * try to recover tree and file data from corrupt primary metadata.
  62. */
  63. static struct xfs_btree_cur *
  64. xfs_rmapbt_dup_cursor(
  65. struct xfs_btree_cur *cur)
  66. {
  67. return xfs_rmapbt_init_cursor(cur->bc_mp, cur->bc_tp,
  68. cur->bc_private.a.agbp, cur->bc_private.a.agno);
  69. }
  70. STATIC void
  71. xfs_rmapbt_set_root(
  72. struct xfs_btree_cur *cur,
  73. union xfs_btree_ptr *ptr,
  74. int inc)
  75. {
  76. struct xfs_buf *agbp = cur->bc_private.a.agbp;
  77. struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
  78. xfs_agnumber_t seqno = be32_to_cpu(agf->agf_seqno);
  79. int btnum = cur->bc_btnum;
  80. struct xfs_perag *pag = xfs_perag_get(cur->bc_mp, seqno);
  81. ASSERT(ptr->s != 0);
  82. agf->agf_roots[btnum] = ptr->s;
  83. be32_add_cpu(&agf->agf_levels[btnum], inc);
  84. pag->pagf_levels[btnum] += inc;
  85. xfs_perag_put(pag);
  86. xfs_alloc_log_agf(cur->bc_tp, agbp, XFS_AGF_ROOTS | XFS_AGF_LEVELS);
  87. }
  88. STATIC int
  89. xfs_rmapbt_alloc_block(
  90. struct xfs_btree_cur *cur,
  91. union xfs_btree_ptr *start,
  92. union xfs_btree_ptr *new,
  93. int *stat)
  94. {
  95. struct xfs_buf *agbp = cur->bc_private.a.agbp;
  96. struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
  97. int error;
  98. xfs_agblock_t bno;
  99. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  100. /* Allocate the new block from the freelist. If we can't, give up. */
  101. error = xfs_alloc_get_freelist(cur->bc_tp, cur->bc_private.a.agbp,
  102. &bno, 1);
  103. if (error) {
  104. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  105. return error;
  106. }
  107. trace_xfs_rmapbt_alloc_block(cur->bc_mp, cur->bc_private.a.agno,
  108. bno, 1);
  109. if (bno == NULLAGBLOCK) {
  110. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  111. *stat = 0;
  112. return 0;
  113. }
  114. xfs_extent_busy_reuse(cur->bc_mp, cur->bc_private.a.agno, bno, 1,
  115. false);
  116. xfs_trans_agbtree_delta(cur->bc_tp, 1);
  117. new->s = cpu_to_be32(bno);
  118. be32_add_cpu(&agf->agf_rmap_blocks, 1);
  119. xfs_alloc_log_agf(cur->bc_tp, agbp, XFS_AGF_RMAP_BLOCKS);
  120. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  121. *stat = 1;
  122. return 0;
  123. }
  124. STATIC int
  125. xfs_rmapbt_free_block(
  126. struct xfs_btree_cur *cur,
  127. struct xfs_buf *bp)
  128. {
  129. struct xfs_buf *agbp = cur->bc_private.a.agbp;
  130. struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
  131. xfs_agblock_t bno;
  132. int error;
  133. bno = xfs_daddr_to_agbno(cur->bc_mp, XFS_BUF_ADDR(bp));
  134. trace_xfs_rmapbt_free_block(cur->bc_mp, cur->bc_private.a.agno,
  135. bno, 1);
  136. be32_add_cpu(&agf->agf_rmap_blocks, -1);
  137. xfs_alloc_log_agf(cur->bc_tp, agbp, XFS_AGF_RMAP_BLOCKS);
  138. error = xfs_alloc_put_freelist(cur->bc_tp, agbp, NULL, bno, 1);
  139. if (error)
  140. return error;
  141. xfs_extent_busy_insert(cur->bc_tp, be32_to_cpu(agf->agf_seqno), bno, 1,
  142. XFS_EXTENT_BUSY_SKIP_DISCARD);
  143. xfs_trans_agbtree_delta(cur->bc_tp, -1);
  144. return 0;
  145. }
  146. STATIC int
  147. xfs_rmapbt_get_minrecs(
  148. struct xfs_btree_cur *cur,
  149. int level)
  150. {
  151. return cur->bc_mp->m_rmap_mnr[level != 0];
  152. }
  153. STATIC int
  154. xfs_rmapbt_get_maxrecs(
  155. struct xfs_btree_cur *cur,
  156. int level)
  157. {
  158. return cur->bc_mp->m_rmap_mxr[level != 0];
  159. }
  160. STATIC void
  161. xfs_rmapbt_init_key_from_rec(
  162. union xfs_btree_key *key,
  163. union xfs_btree_rec *rec)
  164. {
  165. key->rmap.rm_startblock = rec->rmap.rm_startblock;
  166. key->rmap.rm_owner = rec->rmap.rm_owner;
  167. key->rmap.rm_offset = rec->rmap.rm_offset;
  168. }
  169. /*
  170. * The high key for a reverse mapping record can be computed by shifting
  171. * the startblock and offset to the highest value that would still map
  172. * to that record. In practice this means that we add blockcount-1 to
  173. * the startblock for all records, and if the record is for a data/attr
  174. * fork mapping, we add blockcount-1 to the offset too.
  175. */
  176. STATIC void
  177. xfs_rmapbt_init_high_key_from_rec(
  178. union xfs_btree_key *key,
  179. union xfs_btree_rec *rec)
  180. {
  181. uint64_t off;
  182. int adj;
  183. adj = be32_to_cpu(rec->rmap.rm_blockcount) - 1;
  184. key->rmap.rm_startblock = rec->rmap.rm_startblock;
  185. be32_add_cpu(&key->rmap.rm_startblock, adj);
  186. key->rmap.rm_owner = rec->rmap.rm_owner;
  187. key->rmap.rm_offset = rec->rmap.rm_offset;
  188. if (XFS_RMAP_NON_INODE_OWNER(be64_to_cpu(rec->rmap.rm_owner)) ||
  189. XFS_RMAP_IS_BMBT_BLOCK(be64_to_cpu(rec->rmap.rm_offset)))
  190. return;
  191. off = be64_to_cpu(key->rmap.rm_offset);
  192. off = (XFS_RMAP_OFF(off) + adj) | (off & ~XFS_RMAP_OFF_MASK);
  193. key->rmap.rm_offset = cpu_to_be64(off);
  194. }
  195. STATIC void
  196. xfs_rmapbt_init_rec_from_cur(
  197. struct xfs_btree_cur *cur,
  198. union xfs_btree_rec *rec)
  199. {
  200. rec->rmap.rm_startblock = cpu_to_be32(cur->bc_rec.r.rm_startblock);
  201. rec->rmap.rm_blockcount = cpu_to_be32(cur->bc_rec.r.rm_blockcount);
  202. rec->rmap.rm_owner = cpu_to_be64(cur->bc_rec.r.rm_owner);
  203. rec->rmap.rm_offset = cpu_to_be64(
  204. xfs_rmap_irec_offset_pack(&cur->bc_rec.r));
  205. }
  206. STATIC void
  207. xfs_rmapbt_init_ptr_from_cur(
  208. struct xfs_btree_cur *cur,
  209. union xfs_btree_ptr *ptr)
  210. {
  211. struct xfs_agf *agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
  212. ASSERT(cur->bc_private.a.agno == be32_to_cpu(agf->agf_seqno));
  213. ASSERT(agf->agf_roots[cur->bc_btnum] != 0);
  214. ptr->s = agf->agf_roots[cur->bc_btnum];
  215. }
  216. STATIC int64_t
  217. xfs_rmapbt_key_diff(
  218. struct xfs_btree_cur *cur,
  219. union xfs_btree_key *key)
  220. {
  221. struct xfs_rmap_irec *rec = &cur->bc_rec.r;
  222. struct xfs_rmap_key *kp = &key->rmap;
  223. __u64 x, y;
  224. int64_t d;
  225. d = (int64_t)be32_to_cpu(kp->rm_startblock) - rec->rm_startblock;
  226. if (d)
  227. return d;
  228. x = be64_to_cpu(kp->rm_owner);
  229. y = rec->rm_owner;
  230. if (x > y)
  231. return 1;
  232. else if (y > x)
  233. return -1;
  234. x = XFS_RMAP_OFF(be64_to_cpu(kp->rm_offset));
  235. y = rec->rm_offset;
  236. if (x > y)
  237. return 1;
  238. else if (y > x)
  239. return -1;
  240. return 0;
  241. }
  242. STATIC int64_t
  243. xfs_rmapbt_diff_two_keys(
  244. struct xfs_btree_cur *cur,
  245. union xfs_btree_key *k1,
  246. union xfs_btree_key *k2)
  247. {
  248. struct xfs_rmap_key *kp1 = &k1->rmap;
  249. struct xfs_rmap_key *kp2 = &k2->rmap;
  250. int64_t d;
  251. __u64 x, y;
  252. d = (int64_t)be32_to_cpu(kp1->rm_startblock) -
  253. be32_to_cpu(kp2->rm_startblock);
  254. if (d)
  255. return d;
  256. x = be64_to_cpu(kp1->rm_owner);
  257. y = be64_to_cpu(kp2->rm_owner);
  258. if (x > y)
  259. return 1;
  260. else if (y > x)
  261. return -1;
  262. x = XFS_RMAP_OFF(be64_to_cpu(kp1->rm_offset));
  263. y = XFS_RMAP_OFF(be64_to_cpu(kp2->rm_offset));
  264. if (x > y)
  265. return 1;
  266. else if (y > x)
  267. return -1;
  268. return 0;
  269. }
  270. static bool
  271. xfs_rmapbt_verify(
  272. struct xfs_buf *bp)
  273. {
  274. struct xfs_mount *mp = bp->b_target->bt_mount;
  275. struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
  276. struct xfs_perag *pag = bp->b_pag;
  277. unsigned int level;
  278. /*
  279. * magic number and level verification
  280. *
  281. * During growfs operations, we can't verify the exact level or owner as
  282. * the perag is not fully initialised and hence not attached to the
  283. * buffer. In this case, check against the maximum tree depth.
  284. *
  285. * Similarly, during log recovery we will have a perag structure
  286. * attached, but the agf information will not yet have been initialised
  287. * from the on disk AGF. Again, we can only check against maximum limits
  288. * in this case.
  289. */
  290. if (block->bb_magic != cpu_to_be32(XFS_RMAP_CRC_MAGIC))
  291. return false;
  292. if (!xfs_sb_version_hasrmapbt(&mp->m_sb))
  293. return false;
  294. if (!xfs_btree_sblock_v5hdr_verify(bp))
  295. return false;
  296. level = be16_to_cpu(block->bb_level);
  297. if (pag && pag->pagf_init) {
  298. if (level >= pag->pagf_levels[XFS_BTNUM_RMAPi])
  299. return false;
  300. } else if (level >= mp->m_rmap_maxlevels)
  301. return false;
  302. return xfs_btree_sblock_verify(bp, mp->m_rmap_mxr[level != 0]);
  303. }
  304. static void
  305. xfs_rmapbt_read_verify(
  306. struct xfs_buf *bp)
  307. {
  308. if (!xfs_btree_sblock_verify_crc(bp))
  309. xfs_buf_ioerror(bp, -EFSBADCRC);
  310. else if (!xfs_rmapbt_verify(bp))
  311. xfs_buf_ioerror(bp, -EFSCORRUPTED);
  312. if (bp->b_error) {
  313. trace_xfs_btree_corrupt(bp, _RET_IP_);
  314. xfs_verifier_error(bp);
  315. }
  316. }
  317. static void
  318. xfs_rmapbt_write_verify(
  319. struct xfs_buf *bp)
  320. {
  321. if (!xfs_rmapbt_verify(bp)) {
  322. trace_xfs_btree_corrupt(bp, _RET_IP_);
  323. xfs_buf_ioerror(bp, -EFSCORRUPTED);
  324. xfs_verifier_error(bp);
  325. return;
  326. }
  327. xfs_btree_sblock_calc_crc(bp);
  328. }
  329. const struct xfs_buf_ops xfs_rmapbt_buf_ops = {
  330. .name = "xfs_rmapbt",
  331. .verify_read = xfs_rmapbt_read_verify,
  332. .verify_write = xfs_rmapbt_write_verify,
  333. };
  334. STATIC int
  335. xfs_rmapbt_keys_inorder(
  336. struct xfs_btree_cur *cur,
  337. union xfs_btree_key *k1,
  338. union xfs_btree_key *k2)
  339. {
  340. uint32_t x;
  341. uint32_t y;
  342. uint64_t a;
  343. uint64_t b;
  344. x = be32_to_cpu(k1->rmap.rm_startblock);
  345. y = be32_to_cpu(k2->rmap.rm_startblock);
  346. if (x < y)
  347. return 1;
  348. else if (x > y)
  349. return 0;
  350. a = be64_to_cpu(k1->rmap.rm_owner);
  351. b = be64_to_cpu(k2->rmap.rm_owner);
  352. if (a < b)
  353. return 1;
  354. else if (a > b)
  355. return 0;
  356. a = XFS_RMAP_OFF(be64_to_cpu(k1->rmap.rm_offset));
  357. b = XFS_RMAP_OFF(be64_to_cpu(k2->rmap.rm_offset));
  358. if (a <= b)
  359. return 1;
  360. return 0;
  361. }
  362. STATIC int
  363. xfs_rmapbt_recs_inorder(
  364. struct xfs_btree_cur *cur,
  365. union xfs_btree_rec *r1,
  366. union xfs_btree_rec *r2)
  367. {
  368. uint32_t x;
  369. uint32_t y;
  370. uint64_t a;
  371. uint64_t b;
  372. x = be32_to_cpu(r1->rmap.rm_startblock);
  373. y = be32_to_cpu(r2->rmap.rm_startblock);
  374. if (x < y)
  375. return 1;
  376. else if (x > y)
  377. return 0;
  378. a = be64_to_cpu(r1->rmap.rm_owner);
  379. b = be64_to_cpu(r2->rmap.rm_owner);
  380. if (a < b)
  381. return 1;
  382. else if (a > b)
  383. return 0;
  384. a = XFS_RMAP_OFF(be64_to_cpu(r1->rmap.rm_offset));
  385. b = XFS_RMAP_OFF(be64_to_cpu(r2->rmap.rm_offset));
  386. if (a <= b)
  387. return 1;
  388. return 0;
  389. }
  390. static const struct xfs_btree_ops xfs_rmapbt_ops = {
  391. .rec_len = sizeof(struct xfs_rmap_rec),
  392. .key_len = 2 * sizeof(struct xfs_rmap_key),
  393. .dup_cursor = xfs_rmapbt_dup_cursor,
  394. .set_root = xfs_rmapbt_set_root,
  395. .alloc_block = xfs_rmapbt_alloc_block,
  396. .free_block = xfs_rmapbt_free_block,
  397. .get_minrecs = xfs_rmapbt_get_minrecs,
  398. .get_maxrecs = xfs_rmapbt_get_maxrecs,
  399. .init_key_from_rec = xfs_rmapbt_init_key_from_rec,
  400. .init_high_key_from_rec = xfs_rmapbt_init_high_key_from_rec,
  401. .init_rec_from_cur = xfs_rmapbt_init_rec_from_cur,
  402. .init_ptr_from_cur = xfs_rmapbt_init_ptr_from_cur,
  403. .key_diff = xfs_rmapbt_key_diff,
  404. .buf_ops = &xfs_rmapbt_buf_ops,
  405. .diff_two_keys = xfs_rmapbt_diff_two_keys,
  406. .keys_inorder = xfs_rmapbt_keys_inorder,
  407. .recs_inorder = xfs_rmapbt_recs_inorder,
  408. };
  409. /*
  410. * Allocate a new allocation btree cursor.
  411. */
  412. struct xfs_btree_cur *
  413. xfs_rmapbt_init_cursor(
  414. struct xfs_mount *mp,
  415. struct xfs_trans *tp,
  416. struct xfs_buf *agbp,
  417. xfs_agnumber_t agno)
  418. {
  419. struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
  420. struct xfs_btree_cur *cur;
  421. cur = kmem_zone_zalloc(xfs_btree_cur_zone, KM_NOFS);
  422. cur->bc_tp = tp;
  423. cur->bc_mp = mp;
  424. /* Overlapping btree; 2 keys per pointer. */
  425. cur->bc_btnum = XFS_BTNUM_RMAP;
  426. cur->bc_flags = XFS_BTREE_CRC_BLOCKS | XFS_BTREE_OVERLAPPING;
  427. cur->bc_blocklog = mp->m_sb.sb_blocklog;
  428. cur->bc_ops = &xfs_rmapbt_ops;
  429. cur->bc_nlevels = be32_to_cpu(agf->agf_levels[XFS_BTNUM_RMAP]);
  430. cur->bc_statoff = XFS_STATS_CALC_INDEX(xs_rmap_2);
  431. cur->bc_private.a.agbp = agbp;
  432. cur->bc_private.a.agno = agno;
  433. return cur;
  434. }
  435. /*
  436. * Calculate number of records in an rmap btree block.
  437. */
  438. int
  439. xfs_rmapbt_maxrecs(
  440. struct xfs_mount *mp,
  441. int blocklen,
  442. int leaf)
  443. {
  444. blocklen -= XFS_RMAP_BLOCK_LEN;
  445. if (leaf)
  446. return blocklen / sizeof(struct xfs_rmap_rec);
  447. return blocklen /
  448. (2 * sizeof(struct xfs_rmap_key) + sizeof(xfs_rmap_ptr_t));
  449. }
  450. /* Compute the maximum height of an rmap btree. */
  451. void
  452. xfs_rmapbt_compute_maxlevels(
  453. struct xfs_mount *mp)
  454. {
  455. /*
  456. * On a non-reflink filesystem, the maximum number of rmap
  457. * records is the number of blocks in the AG, hence the max
  458. * rmapbt height is log_$maxrecs($agblocks). However, with
  459. * reflink each AG block can have up to 2^32 (per the refcount
  460. * record format) owners, which means that theoretically we
  461. * could face up to 2^64 rmap records.
  462. *
  463. * That effectively means that the max rmapbt height must be
  464. * XFS_BTREE_MAXLEVELS. "Fortunately" we'll run out of AG
  465. * blocks to feed the rmapbt long before the rmapbt reaches
  466. * maximum height. The reflink code uses ag_resv_critical to
  467. * disallow reflinking when less than 10% of the per-AG metadata
  468. * block reservation since the fallback is a regular file copy.
  469. */
  470. if (xfs_sb_version_hasreflink(&mp->m_sb))
  471. mp->m_rmap_maxlevels = XFS_BTREE_MAXLEVELS;
  472. else
  473. mp->m_rmap_maxlevels = xfs_btree_compute_maxlevels(mp,
  474. mp->m_rmap_mnr, mp->m_sb.sb_agblocks);
  475. }
  476. /* Calculate the refcount btree size for some records. */
  477. xfs_extlen_t
  478. xfs_rmapbt_calc_size(
  479. struct xfs_mount *mp,
  480. unsigned long long len)
  481. {
  482. return xfs_btree_calc_size(mp, mp->m_rmap_mnr, len);
  483. }
  484. /*
  485. * Calculate the maximum refcount btree size.
  486. */
  487. xfs_extlen_t
  488. xfs_rmapbt_max_size(
  489. struct xfs_mount *mp,
  490. xfs_agblock_t agblocks)
  491. {
  492. /* Bail out if we're uninitialized, which can happen in mkfs. */
  493. if (mp->m_rmap_mxr[0] == 0)
  494. return 0;
  495. return xfs_rmapbt_calc_size(mp, agblocks);
  496. }
  497. /*
  498. * Figure out how many blocks to reserve and how many are used by this btree.
  499. */
  500. int
  501. xfs_rmapbt_calc_reserves(
  502. struct xfs_mount *mp,
  503. xfs_agnumber_t agno,
  504. xfs_extlen_t *ask,
  505. xfs_extlen_t *used)
  506. {
  507. struct xfs_buf *agbp;
  508. struct xfs_agf *agf;
  509. xfs_agblock_t agblocks;
  510. xfs_extlen_t tree_len;
  511. int error;
  512. if (!xfs_sb_version_hasrmapbt(&mp->m_sb))
  513. return 0;
  514. error = xfs_alloc_read_agf(mp, NULL, agno, 0, &agbp);
  515. if (error)
  516. return error;
  517. agf = XFS_BUF_TO_AGF(agbp);
  518. agblocks = be32_to_cpu(agf->agf_length);
  519. tree_len = be32_to_cpu(agf->agf_rmap_blocks);
  520. xfs_buf_relse(agbp);
  521. /* Reserve 1% of the AG or enough for 1 block per record. */
  522. *ask += max(agblocks / 100, xfs_rmapbt_max_size(mp, agblocks));
  523. *used += tree_len;
  524. return error;
  525. }