xfs_rmap_btree.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  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 xfs_failaddr_t
  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. xfs_failaddr_t fa;
  278. unsigned int level;
  279. /*
  280. * magic number and level verification
  281. *
  282. * During growfs operations, we can't verify the exact level or owner as
  283. * the perag is not fully initialised and hence not attached to the
  284. * buffer. In this case, check against the maximum tree depth.
  285. *
  286. * Similarly, during log recovery we will have a perag structure
  287. * attached, but the agf information will not yet have been initialised
  288. * from the on disk AGF. Again, we can only check against maximum limits
  289. * in this case.
  290. */
  291. if (block->bb_magic != cpu_to_be32(XFS_RMAP_CRC_MAGIC))
  292. return __this_address;
  293. if (!xfs_sb_version_hasrmapbt(&mp->m_sb))
  294. return __this_address;
  295. fa = xfs_btree_sblock_v5hdr_verify(bp);
  296. if (fa)
  297. return fa;
  298. level = be16_to_cpu(block->bb_level);
  299. if (pag && pag->pagf_init) {
  300. if (level >= pag->pagf_levels[XFS_BTNUM_RMAPi])
  301. return __this_address;
  302. } else if (level >= mp->m_rmap_maxlevels)
  303. return __this_address;
  304. return xfs_btree_sblock_verify(bp, mp->m_rmap_mxr[level != 0]);
  305. }
  306. static void
  307. xfs_rmapbt_read_verify(
  308. struct xfs_buf *bp)
  309. {
  310. xfs_failaddr_t fa;
  311. if (!xfs_btree_sblock_verify_crc(bp))
  312. xfs_verifier_error(bp, -EFSBADCRC, __this_address);
  313. else {
  314. fa = xfs_rmapbt_verify(bp);
  315. if (fa)
  316. xfs_verifier_error(bp, -EFSCORRUPTED, fa);
  317. }
  318. if (bp->b_error)
  319. trace_xfs_btree_corrupt(bp, _RET_IP_);
  320. }
  321. static void
  322. xfs_rmapbt_write_verify(
  323. struct xfs_buf *bp)
  324. {
  325. xfs_failaddr_t fa;
  326. fa = xfs_rmapbt_verify(bp);
  327. if (fa) {
  328. trace_xfs_btree_corrupt(bp, _RET_IP_);
  329. xfs_verifier_error(bp, -EFSCORRUPTED, fa);
  330. return;
  331. }
  332. xfs_btree_sblock_calc_crc(bp);
  333. }
  334. const struct xfs_buf_ops xfs_rmapbt_buf_ops = {
  335. .name = "xfs_rmapbt",
  336. .verify_read = xfs_rmapbt_read_verify,
  337. .verify_write = xfs_rmapbt_write_verify,
  338. };
  339. STATIC int
  340. xfs_rmapbt_keys_inorder(
  341. struct xfs_btree_cur *cur,
  342. union xfs_btree_key *k1,
  343. union xfs_btree_key *k2)
  344. {
  345. uint32_t x;
  346. uint32_t y;
  347. uint64_t a;
  348. uint64_t b;
  349. x = be32_to_cpu(k1->rmap.rm_startblock);
  350. y = be32_to_cpu(k2->rmap.rm_startblock);
  351. if (x < y)
  352. return 1;
  353. else if (x > y)
  354. return 0;
  355. a = be64_to_cpu(k1->rmap.rm_owner);
  356. b = be64_to_cpu(k2->rmap.rm_owner);
  357. if (a < b)
  358. return 1;
  359. else if (a > b)
  360. return 0;
  361. a = XFS_RMAP_OFF(be64_to_cpu(k1->rmap.rm_offset));
  362. b = XFS_RMAP_OFF(be64_to_cpu(k2->rmap.rm_offset));
  363. if (a <= b)
  364. return 1;
  365. return 0;
  366. }
  367. STATIC int
  368. xfs_rmapbt_recs_inorder(
  369. struct xfs_btree_cur *cur,
  370. union xfs_btree_rec *r1,
  371. union xfs_btree_rec *r2)
  372. {
  373. uint32_t x;
  374. uint32_t y;
  375. uint64_t a;
  376. uint64_t b;
  377. x = be32_to_cpu(r1->rmap.rm_startblock);
  378. y = be32_to_cpu(r2->rmap.rm_startblock);
  379. if (x < y)
  380. return 1;
  381. else if (x > y)
  382. return 0;
  383. a = be64_to_cpu(r1->rmap.rm_owner);
  384. b = be64_to_cpu(r2->rmap.rm_owner);
  385. if (a < b)
  386. return 1;
  387. else if (a > b)
  388. return 0;
  389. a = XFS_RMAP_OFF(be64_to_cpu(r1->rmap.rm_offset));
  390. b = XFS_RMAP_OFF(be64_to_cpu(r2->rmap.rm_offset));
  391. if (a <= b)
  392. return 1;
  393. return 0;
  394. }
  395. static const struct xfs_btree_ops xfs_rmapbt_ops = {
  396. .rec_len = sizeof(struct xfs_rmap_rec),
  397. .key_len = 2 * sizeof(struct xfs_rmap_key),
  398. .dup_cursor = xfs_rmapbt_dup_cursor,
  399. .set_root = xfs_rmapbt_set_root,
  400. .alloc_block = xfs_rmapbt_alloc_block,
  401. .free_block = xfs_rmapbt_free_block,
  402. .get_minrecs = xfs_rmapbt_get_minrecs,
  403. .get_maxrecs = xfs_rmapbt_get_maxrecs,
  404. .init_key_from_rec = xfs_rmapbt_init_key_from_rec,
  405. .init_high_key_from_rec = xfs_rmapbt_init_high_key_from_rec,
  406. .init_rec_from_cur = xfs_rmapbt_init_rec_from_cur,
  407. .init_ptr_from_cur = xfs_rmapbt_init_ptr_from_cur,
  408. .key_diff = xfs_rmapbt_key_diff,
  409. .buf_ops = &xfs_rmapbt_buf_ops,
  410. .diff_two_keys = xfs_rmapbt_diff_two_keys,
  411. .keys_inorder = xfs_rmapbt_keys_inorder,
  412. .recs_inorder = xfs_rmapbt_recs_inorder,
  413. };
  414. /*
  415. * Allocate a new allocation btree cursor.
  416. */
  417. struct xfs_btree_cur *
  418. xfs_rmapbt_init_cursor(
  419. struct xfs_mount *mp,
  420. struct xfs_trans *tp,
  421. struct xfs_buf *agbp,
  422. xfs_agnumber_t agno)
  423. {
  424. struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
  425. struct xfs_btree_cur *cur;
  426. cur = kmem_zone_zalloc(xfs_btree_cur_zone, KM_NOFS);
  427. cur->bc_tp = tp;
  428. cur->bc_mp = mp;
  429. /* Overlapping btree; 2 keys per pointer. */
  430. cur->bc_btnum = XFS_BTNUM_RMAP;
  431. cur->bc_flags = XFS_BTREE_CRC_BLOCKS | XFS_BTREE_OVERLAPPING;
  432. cur->bc_blocklog = mp->m_sb.sb_blocklog;
  433. cur->bc_ops = &xfs_rmapbt_ops;
  434. cur->bc_nlevels = be32_to_cpu(agf->agf_levels[XFS_BTNUM_RMAP]);
  435. cur->bc_statoff = XFS_STATS_CALC_INDEX(xs_rmap_2);
  436. cur->bc_private.a.agbp = agbp;
  437. cur->bc_private.a.agno = agno;
  438. return cur;
  439. }
  440. /*
  441. * Calculate number of records in an rmap btree block.
  442. */
  443. int
  444. xfs_rmapbt_maxrecs(
  445. struct xfs_mount *mp,
  446. int blocklen,
  447. int leaf)
  448. {
  449. blocklen -= XFS_RMAP_BLOCK_LEN;
  450. if (leaf)
  451. return blocklen / sizeof(struct xfs_rmap_rec);
  452. return blocklen /
  453. (2 * sizeof(struct xfs_rmap_key) + sizeof(xfs_rmap_ptr_t));
  454. }
  455. /* Compute the maximum height of an rmap btree. */
  456. void
  457. xfs_rmapbt_compute_maxlevels(
  458. struct xfs_mount *mp)
  459. {
  460. /*
  461. * On a non-reflink filesystem, the maximum number of rmap
  462. * records is the number of blocks in the AG, hence the max
  463. * rmapbt height is log_$maxrecs($agblocks). However, with
  464. * reflink each AG block can have up to 2^32 (per the refcount
  465. * record format) owners, which means that theoretically we
  466. * could face up to 2^64 rmap records.
  467. *
  468. * That effectively means that the max rmapbt height must be
  469. * XFS_BTREE_MAXLEVELS. "Fortunately" we'll run out of AG
  470. * blocks to feed the rmapbt long before the rmapbt reaches
  471. * maximum height. The reflink code uses ag_resv_critical to
  472. * disallow reflinking when less than 10% of the per-AG metadata
  473. * block reservation since the fallback is a regular file copy.
  474. */
  475. if (xfs_sb_version_hasreflink(&mp->m_sb))
  476. mp->m_rmap_maxlevels = XFS_BTREE_MAXLEVELS;
  477. else
  478. mp->m_rmap_maxlevels = xfs_btree_compute_maxlevels(mp,
  479. mp->m_rmap_mnr, mp->m_sb.sb_agblocks);
  480. }
  481. /* Calculate the refcount btree size for some records. */
  482. xfs_extlen_t
  483. xfs_rmapbt_calc_size(
  484. struct xfs_mount *mp,
  485. unsigned long long len)
  486. {
  487. return xfs_btree_calc_size(mp, mp->m_rmap_mnr, len);
  488. }
  489. /*
  490. * Calculate the maximum refcount btree size.
  491. */
  492. xfs_extlen_t
  493. xfs_rmapbt_max_size(
  494. struct xfs_mount *mp,
  495. xfs_agblock_t agblocks)
  496. {
  497. /* Bail out if we're uninitialized, which can happen in mkfs. */
  498. if (mp->m_rmap_mxr[0] == 0)
  499. return 0;
  500. return xfs_rmapbt_calc_size(mp, agblocks);
  501. }
  502. /*
  503. * Figure out how many blocks to reserve and how many are used by this btree.
  504. */
  505. int
  506. xfs_rmapbt_calc_reserves(
  507. struct xfs_mount *mp,
  508. xfs_agnumber_t agno,
  509. xfs_extlen_t *ask,
  510. xfs_extlen_t *used)
  511. {
  512. struct xfs_buf *agbp;
  513. struct xfs_agf *agf;
  514. xfs_agblock_t agblocks;
  515. xfs_extlen_t tree_len;
  516. int error;
  517. if (!xfs_sb_version_hasrmapbt(&mp->m_sb))
  518. return 0;
  519. error = xfs_alloc_read_agf(mp, NULL, agno, 0, &agbp);
  520. if (error)
  521. return error;
  522. agf = XFS_BUF_TO_AGF(agbp);
  523. agblocks = be32_to_cpu(agf->agf_length);
  524. tree_len = be32_to_cpu(agf->agf_rmap_blocks);
  525. xfs_buf_relse(agbp);
  526. /* Reserve 1% of the AG or enough for 1 block per record. */
  527. *ask += max(agblocks / 100, xfs_rmapbt_max_size(mp, agblocks));
  528. *used += tree_len;
  529. return error;
  530. }