xfs_rmap_btree.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  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. /* Allocate the new block from the freelist. If we can't, give up. */
  100. error = xfs_alloc_get_freelist(cur->bc_tp, cur->bc_private.a.agbp,
  101. &bno, 1);
  102. if (error)
  103. return error;
  104. trace_xfs_rmapbt_alloc_block(cur->bc_mp, cur->bc_private.a.agno,
  105. bno, 1);
  106. if (bno == NULLAGBLOCK) {
  107. *stat = 0;
  108. return 0;
  109. }
  110. xfs_extent_busy_reuse(cur->bc_mp, cur->bc_private.a.agno, bno, 1,
  111. false);
  112. xfs_trans_agbtree_delta(cur->bc_tp, 1);
  113. new->s = cpu_to_be32(bno);
  114. be32_add_cpu(&agf->agf_rmap_blocks, 1);
  115. xfs_alloc_log_agf(cur->bc_tp, agbp, XFS_AGF_RMAP_BLOCKS);
  116. xfs_ag_resv_rmapbt_alloc(cur->bc_mp, cur->bc_private.a.agno);
  117. *stat = 1;
  118. return 0;
  119. }
  120. STATIC int
  121. xfs_rmapbt_free_block(
  122. struct xfs_btree_cur *cur,
  123. struct xfs_buf *bp)
  124. {
  125. struct xfs_buf *agbp = cur->bc_private.a.agbp;
  126. struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
  127. xfs_agblock_t bno;
  128. int error;
  129. bno = xfs_daddr_to_agbno(cur->bc_mp, XFS_BUF_ADDR(bp));
  130. trace_xfs_rmapbt_free_block(cur->bc_mp, cur->bc_private.a.agno,
  131. bno, 1);
  132. be32_add_cpu(&agf->agf_rmap_blocks, -1);
  133. xfs_alloc_log_agf(cur->bc_tp, agbp, XFS_AGF_RMAP_BLOCKS);
  134. error = xfs_alloc_put_freelist(cur->bc_tp, agbp, NULL, bno, 1);
  135. if (error)
  136. return error;
  137. xfs_extent_busy_insert(cur->bc_tp, be32_to_cpu(agf->agf_seqno), bno, 1,
  138. XFS_EXTENT_BUSY_SKIP_DISCARD);
  139. xfs_trans_agbtree_delta(cur->bc_tp, -1);
  140. xfs_ag_resv_rmapbt_free(cur->bc_mp, cur->bc_private.a.agno);
  141. return 0;
  142. }
  143. STATIC int
  144. xfs_rmapbt_get_minrecs(
  145. struct xfs_btree_cur *cur,
  146. int level)
  147. {
  148. return cur->bc_mp->m_rmap_mnr[level != 0];
  149. }
  150. STATIC int
  151. xfs_rmapbt_get_maxrecs(
  152. struct xfs_btree_cur *cur,
  153. int level)
  154. {
  155. return cur->bc_mp->m_rmap_mxr[level != 0];
  156. }
  157. STATIC void
  158. xfs_rmapbt_init_key_from_rec(
  159. union xfs_btree_key *key,
  160. union xfs_btree_rec *rec)
  161. {
  162. key->rmap.rm_startblock = rec->rmap.rm_startblock;
  163. key->rmap.rm_owner = rec->rmap.rm_owner;
  164. key->rmap.rm_offset = rec->rmap.rm_offset;
  165. }
  166. /*
  167. * The high key for a reverse mapping record can be computed by shifting
  168. * the startblock and offset to the highest value that would still map
  169. * to that record. In practice this means that we add blockcount-1 to
  170. * the startblock for all records, and if the record is for a data/attr
  171. * fork mapping, we add blockcount-1 to the offset too.
  172. */
  173. STATIC void
  174. xfs_rmapbt_init_high_key_from_rec(
  175. union xfs_btree_key *key,
  176. union xfs_btree_rec *rec)
  177. {
  178. uint64_t off;
  179. int adj;
  180. adj = be32_to_cpu(rec->rmap.rm_blockcount) - 1;
  181. key->rmap.rm_startblock = rec->rmap.rm_startblock;
  182. be32_add_cpu(&key->rmap.rm_startblock, adj);
  183. key->rmap.rm_owner = rec->rmap.rm_owner;
  184. key->rmap.rm_offset = rec->rmap.rm_offset;
  185. if (XFS_RMAP_NON_INODE_OWNER(be64_to_cpu(rec->rmap.rm_owner)) ||
  186. XFS_RMAP_IS_BMBT_BLOCK(be64_to_cpu(rec->rmap.rm_offset)))
  187. return;
  188. off = be64_to_cpu(key->rmap.rm_offset);
  189. off = (XFS_RMAP_OFF(off) + adj) | (off & ~XFS_RMAP_OFF_MASK);
  190. key->rmap.rm_offset = cpu_to_be64(off);
  191. }
  192. STATIC void
  193. xfs_rmapbt_init_rec_from_cur(
  194. struct xfs_btree_cur *cur,
  195. union xfs_btree_rec *rec)
  196. {
  197. rec->rmap.rm_startblock = cpu_to_be32(cur->bc_rec.r.rm_startblock);
  198. rec->rmap.rm_blockcount = cpu_to_be32(cur->bc_rec.r.rm_blockcount);
  199. rec->rmap.rm_owner = cpu_to_be64(cur->bc_rec.r.rm_owner);
  200. rec->rmap.rm_offset = cpu_to_be64(
  201. xfs_rmap_irec_offset_pack(&cur->bc_rec.r));
  202. }
  203. STATIC void
  204. xfs_rmapbt_init_ptr_from_cur(
  205. struct xfs_btree_cur *cur,
  206. union xfs_btree_ptr *ptr)
  207. {
  208. struct xfs_agf *agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
  209. ASSERT(cur->bc_private.a.agno == be32_to_cpu(agf->agf_seqno));
  210. ASSERT(agf->agf_roots[cur->bc_btnum] != 0);
  211. ptr->s = agf->agf_roots[cur->bc_btnum];
  212. }
  213. STATIC int64_t
  214. xfs_rmapbt_key_diff(
  215. struct xfs_btree_cur *cur,
  216. union xfs_btree_key *key)
  217. {
  218. struct xfs_rmap_irec *rec = &cur->bc_rec.r;
  219. struct xfs_rmap_key *kp = &key->rmap;
  220. __u64 x, y;
  221. int64_t d;
  222. d = (int64_t)be32_to_cpu(kp->rm_startblock) - rec->rm_startblock;
  223. if (d)
  224. return d;
  225. x = be64_to_cpu(kp->rm_owner);
  226. y = rec->rm_owner;
  227. if (x > y)
  228. return 1;
  229. else if (y > x)
  230. return -1;
  231. x = XFS_RMAP_OFF(be64_to_cpu(kp->rm_offset));
  232. y = rec->rm_offset;
  233. if (x > y)
  234. return 1;
  235. else if (y > x)
  236. return -1;
  237. return 0;
  238. }
  239. STATIC int64_t
  240. xfs_rmapbt_diff_two_keys(
  241. struct xfs_btree_cur *cur,
  242. union xfs_btree_key *k1,
  243. union xfs_btree_key *k2)
  244. {
  245. struct xfs_rmap_key *kp1 = &k1->rmap;
  246. struct xfs_rmap_key *kp2 = &k2->rmap;
  247. int64_t d;
  248. __u64 x, y;
  249. d = (int64_t)be32_to_cpu(kp1->rm_startblock) -
  250. be32_to_cpu(kp2->rm_startblock);
  251. if (d)
  252. return d;
  253. x = be64_to_cpu(kp1->rm_owner);
  254. y = be64_to_cpu(kp2->rm_owner);
  255. if (x > y)
  256. return 1;
  257. else if (y > x)
  258. return -1;
  259. x = XFS_RMAP_OFF(be64_to_cpu(kp1->rm_offset));
  260. y = XFS_RMAP_OFF(be64_to_cpu(kp2->rm_offset));
  261. if (x > y)
  262. return 1;
  263. else if (y > x)
  264. return -1;
  265. return 0;
  266. }
  267. static xfs_failaddr_t
  268. xfs_rmapbt_verify(
  269. struct xfs_buf *bp)
  270. {
  271. struct xfs_mount *mp = bp->b_target->bt_mount;
  272. struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
  273. struct xfs_perag *pag = bp->b_pag;
  274. xfs_failaddr_t fa;
  275. unsigned int level;
  276. /*
  277. * magic number and level verification
  278. *
  279. * During growfs operations, we can't verify the exact level or owner as
  280. * the perag is not fully initialised and hence not attached to the
  281. * buffer. In this case, check against the maximum tree depth.
  282. *
  283. * Similarly, during log recovery we will have a perag structure
  284. * attached, but the agf information will not yet have been initialised
  285. * from the on disk AGF. Again, we can only check against maximum limits
  286. * in this case.
  287. */
  288. if (block->bb_magic != cpu_to_be32(XFS_RMAP_CRC_MAGIC))
  289. return __this_address;
  290. if (!xfs_sb_version_hasrmapbt(&mp->m_sb))
  291. return __this_address;
  292. fa = xfs_btree_sblock_v5hdr_verify(bp);
  293. if (fa)
  294. return fa;
  295. level = be16_to_cpu(block->bb_level);
  296. if (pag && pag->pagf_init) {
  297. if (level >= pag->pagf_levels[XFS_BTNUM_RMAPi])
  298. return __this_address;
  299. } else if (level >= mp->m_rmap_maxlevels)
  300. return __this_address;
  301. return xfs_btree_sblock_verify(bp, mp->m_rmap_mxr[level != 0]);
  302. }
  303. static void
  304. xfs_rmapbt_read_verify(
  305. struct xfs_buf *bp)
  306. {
  307. xfs_failaddr_t fa;
  308. if (!xfs_btree_sblock_verify_crc(bp))
  309. xfs_verifier_error(bp, -EFSBADCRC, __this_address);
  310. else {
  311. fa = xfs_rmapbt_verify(bp);
  312. if (fa)
  313. xfs_verifier_error(bp, -EFSCORRUPTED, fa);
  314. }
  315. if (bp->b_error)
  316. trace_xfs_btree_corrupt(bp, _RET_IP_);
  317. }
  318. static void
  319. xfs_rmapbt_write_verify(
  320. struct xfs_buf *bp)
  321. {
  322. xfs_failaddr_t fa;
  323. fa = xfs_rmapbt_verify(bp);
  324. if (fa) {
  325. trace_xfs_btree_corrupt(bp, _RET_IP_);
  326. xfs_verifier_error(bp, -EFSCORRUPTED, fa);
  327. return;
  328. }
  329. xfs_btree_sblock_calc_crc(bp);
  330. }
  331. const struct xfs_buf_ops xfs_rmapbt_buf_ops = {
  332. .name = "xfs_rmapbt",
  333. .verify_read = xfs_rmapbt_read_verify,
  334. .verify_write = xfs_rmapbt_write_verify,
  335. .verify_struct = xfs_rmapbt_verify,
  336. };
  337. STATIC int
  338. xfs_rmapbt_keys_inorder(
  339. struct xfs_btree_cur *cur,
  340. union xfs_btree_key *k1,
  341. union xfs_btree_key *k2)
  342. {
  343. uint32_t x;
  344. uint32_t y;
  345. uint64_t a;
  346. uint64_t b;
  347. x = be32_to_cpu(k1->rmap.rm_startblock);
  348. y = be32_to_cpu(k2->rmap.rm_startblock);
  349. if (x < y)
  350. return 1;
  351. else if (x > y)
  352. return 0;
  353. a = be64_to_cpu(k1->rmap.rm_owner);
  354. b = be64_to_cpu(k2->rmap.rm_owner);
  355. if (a < b)
  356. return 1;
  357. else if (a > b)
  358. return 0;
  359. a = XFS_RMAP_OFF(be64_to_cpu(k1->rmap.rm_offset));
  360. b = XFS_RMAP_OFF(be64_to_cpu(k2->rmap.rm_offset));
  361. if (a <= b)
  362. return 1;
  363. return 0;
  364. }
  365. STATIC int
  366. xfs_rmapbt_recs_inorder(
  367. struct xfs_btree_cur *cur,
  368. union xfs_btree_rec *r1,
  369. union xfs_btree_rec *r2)
  370. {
  371. uint32_t x;
  372. uint32_t y;
  373. uint64_t a;
  374. uint64_t b;
  375. x = be32_to_cpu(r1->rmap.rm_startblock);
  376. y = be32_to_cpu(r2->rmap.rm_startblock);
  377. if (x < y)
  378. return 1;
  379. else if (x > y)
  380. return 0;
  381. a = be64_to_cpu(r1->rmap.rm_owner);
  382. b = be64_to_cpu(r2->rmap.rm_owner);
  383. if (a < b)
  384. return 1;
  385. else if (a > b)
  386. return 0;
  387. a = XFS_RMAP_OFF(be64_to_cpu(r1->rmap.rm_offset));
  388. b = XFS_RMAP_OFF(be64_to_cpu(r2->rmap.rm_offset));
  389. if (a <= b)
  390. return 1;
  391. return 0;
  392. }
  393. static const struct xfs_btree_ops xfs_rmapbt_ops = {
  394. .rec_len = sizeof(struct xfs_rmap_rec),
  395. .key_len = 2 * sizeof(struct xfs_rmap_key),
  396. .dup_cursor = xfs_rmapbt_dup_cursor,
  397. .set_root = xfs_rmapbt_set_root,
  398. .alloc_block = xfs_rmapbt_alloc_block,
  399. .free_block = xfs_rmapbt_free_block,
  400. .get_minrecs = xfs_rmapbt_get_minrecs,
  401. .get_maxrecs = xfs_rmapbt_get_maxrecs,
  402. .init_key_from_rec = xfs_rmapbt_init_key_from_rec,
  403. .init_high_key_from_rec = xfs_rmapbt_init_high_key_from_rec,
  404. .init_rec_from_cur = xfs_rmapbt_init_rec_from_cur,
  405. .init_ptr_from_cur = xfs_rmapbt_init_ptr_from_cur,
  406. .key_diff = xfs_rmapbt_key_diff,
  407. .buf_ops = &xfs_rmapbt_buf_ops,
  408. .diff_two_keys = xfs_rmapbt_diff_two_keys,
  409. .keys_inorder = xfs_rmapbt_keys_inorder,
  410. .recs_inorder = xfs_rmapbt_recs_inorder,
  411. };
  412. /*
  413. * Allocate a new allocation btree cursor.
  414. */
  415. struct xfs_btree_cur *
  416. xfs_rmapbt_init_cursor(
  417. struct xfs_mount *mp,
  418. struct xfs_trans *tp,
  419. struct xfs_buf *agbp,
  420. xfs_agnumber_t agno)
  421. {
  422. struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
  423. struct xfs_btree_cur *cur;
  424. cur = kmem_zone_zalloc(xfs_btree_cur_zone, KM_NOFS);
  425. cur->bc_tp = tp;
  426. cur->bc_mp = mp;
  427. /* Overlapping btree; 2 keys per pointer. */
  428. cur->bc_btnum = XFS_BTNUM_RMAP;
  429. cur->bc_flags = XFS_BTREE_CRC_BLOCKS | XFS_BTREE_OVERLAPPING;
  430. cur->bc_blocklog = mp->m_sb.sb_blocklog;
  431. cur->bc_ops = &xfs_rmapbt_ops;
  432. cur->bc_nlevels = be32_to_cpu(agf->agf_levels[XFS_BTNUM_RMAP]);
  433. cur->bc_statoff = XFS_STATS_CALC_INDEX(xs_rmap_2);
  434. cur->bc_private.a.agbp = agbp;
  435. cur->bc_private.a.agno = agno;
  436. return cur;
  437. }
  438. /*
  439. * Calculate number of records in an rmap btree block.
  440. */
  441. int
  442. xfs_rmapbt_maxrecs(
  443. struct xfs_mount *mp,
  444. int blocklen,
  445. int leaf)
  446. {
  447. blocklen -= XFS_RMAP_BLOCK_LEN;
  448. if (leaf)
  449. return blocklen / sizeof(struct xfs_rmap_rec);
  450. return blocklen /
  451. (2 * sizeof(struct xfs_rmap_key) + sizeof(xfs_rmap_ptr_t));
  452. }
  453. /* Compute the maximum height of an rmap btree. */
  454. void
  455. xfs_rmapbt_compute_maxlevels(
  456. struct xfs_mount *mp)
  457. {
  458. /*
  459. * On a non-reflink filesystem, the maximum number of rmap
  460. * records is the number of blocks in the AG, hence the max
  461. * rmapbt height is log_$maxrecs($agblocks). However, with
  462. * reflink each AG block can have up to 2^32 (per the refcount
  463. * record format) owners, which means that theoretically we
  464. * could face up to 2^64 rmap records.
  465. *
  466. * That effectively means that the max rmapbt height must be
  467. * XFS_BTREE_MAXLEVELS. "Fortunately" we'll run out of AG
  468. * blocks to feed the rmapbt long before the rmapbt reaches
  469. * maximum height. The reflink code uses ag_resv_critical to
  470. * disallow reflinking when less than 10% of the per-AG metadata
  471. * block reservation since the fallback is a regular file copy.
  472. */
  473. if (xfs_sb_version_hasreflink(&mp->m_sb))
  474. mp->m_rmap_maxlevels = XFS_BTREE_MAXLEVELS;
  475. else
  476. mp->m_rmap_maxlevels = xfs_btree_compute_maxlevels(mp,
  477. mp->m_rmap_mnr, mp->m_sb.sb_agblocks);
  478. }
  479. /* Calculate the refcount btree size for some records. */
  480. xfs_extlen_t
  481. xfs_rmapbt_calc_size(
  482. struct xfs_mount *mp,
  483. unsigned long long len)
  484. {
  485. return xfs_btree_calc_size(mp, mp->m_rmap_mnr, len);
  486. }
  487. /*
  488. * Calculate the maximum refcount btree size.
  489. */
  490. xfs_extlen_t
  491. xfs_rmapbt_max_size(
  492. struct xfs_mount *mp,
  493. xfs_agblock_t agblocks)
  494. {
  495. /* Bail out if we're uninitialized, which can happen in mkfs. */
  496. if (mp->m_rmap_mxr[0] == 0)
  497. return 0;
  498. return xfs_rmapbt_calc_size(mp, agblocks);
  499. }
  500. /*
  501. * Figure out how many blocks to reserve and how many are used by this btree.
  502. */
  503. int
  504. xfs_rmapbt_calc_reserves(
  505. struct xfs_mount *mp,
  506. xfs_agnumber_t agno,
  507. xfs_extlen_t *ask,
  508. xfs_extlen_t *used)
  509. {
  510. struct xfs_buf *agbp;
  511. struct xfs_agf *agf;
  512. xfs_agblock_t agblocks;
  513. xfs_extlen_t tree_len;
  514. int error;
  515. if (!xfs_sb_version_hasrmapbt(&mp->m_sb))
  516. return 0;
  517. error = xfs_alloc_read_agf(mp, NULL, agno, 0, &agbp);
  518. if (error)
  519. return error;
  520. agf = XFS_BUF_TO_AGF(agbp);
  521. agblocks = be32_to_cpu(agf->agf_length);
  522. tree_len = be32_to_cpu(agf->agf_rmap_blocks);
  523. xfs_buf_relse(agbp);
  524. /* Reserve 1% of the AG or enough for 1 block per record. */
  525. *ask += max(agblocks / 100, xfs_rmapbt_max_size(mp, agblocks));
  526. *used += tree_len;
  527. return error;
  528. }