xfs_ialloc_btree.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. /*
  2. * Copyright (c) 2000-2001,2005 Silicon Graphics, 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_mount.h"
  26. #include "xfs_inode.h"
  27. #include "xfs_btree.h"
  28. #include "xfs_ialloc.h"
  29. #include "xfs_ialloc_btree.h"
  30. #include "xfs_alloc.h"
  31. #include "xfs_error.h"
  32. #include "xfs_trace.h"
  33. #include "xfs_cksum.h"
  34. #include "xfs_trans.h"
  35. #include "xfs_rmap.h"
  36. STATIC int
  37. xfs_inobt_get_minrecs(
  38. struct xfs_btree_cur *cur,
  39. int level)
  40. {
  41. return cur->bc_mp->m_inobt_mnr[level != 0];
  42. }
  43. STATIC struct xfs_btree_cur *
  44. xfs_inobt_dup_cursor(
  45. struct xfs_btree_cur *cur)
  46. {
  47. return xfs_inobt_init_cursor(cur->bc_mp, cur->bc_tp,
  48. cur->bc_private.a.agbp, cur->bc_private.a.agno,
  49. cur->bc_btnum);
  50. }
  51. STATIC void
  52. xfs_inobt_set_root(
  53. struct xfs_btree_cur *cur,
  54. union xfs_btree_ptr *nptr,
  55. int inc) /* level change */
  56. {
  57. struct xfs_buf *agbp = cur->bc_private.a.agbp;
  58. struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp);
  59. agi->agi_root = nptr->s;
  60. be32_add_cpu(&agi->agi_level, inc);
  61. xfs_ialloc_log_agi(cur->bc_tp, agbp, XFS_AGI_ROOT | XFS_AGI_LEVEL);
  62. }
  63. STATIC void
  64. xfs_finobt_set_root(
  65. struct xfs_btree_cur *cur,
  66. union xfs_btree_ptr *nptr,
  67. int inc) /* level change */
  68. {
  69. struct xfs_buf *agbp = cur->bc_private.a.agbp;
  70. struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp);
  71. agi->agi_free_root = nptr->s;
  72. be32_add_cpu(&agi->agi_free_level, inc);
  73. xfs_ialloc_log_agi(cur->bc_tp, agbp,
  74. XFS_AGI_FREE_ROOT | XFS_AGI_FREE_LEVEL);
  75. }
  76. STATIC int
  77. __xfs_inobt_alloc_block(
  78. struct xfs_btree_cur *cur,
  79. union xfs_btree_ptr *start,
  80. union xfs_btree_ptr *new,
  81. int *stat,
  82. enum xfs_ag_resv_type resv)
  83. {
  84. xfs_alloc_arg_t args; /* block allocation args */
  85. int error; /* error return value */
  86. xfs_agblock_t sbno = be32_to_cpu(start->s);
  87. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  88. memset(&args, 0, sizeof(args));
  89. args.tp = cur->bc_tp;
  90. args.mp = cur->bc_mp;
  91. xfs_rmap_ag_owner(&args.oinfo, XFS_RMAP_OWN_INOBT);
  92. args.fsbno = XFS_AGB_TO_FSB(args.mp, cur->bc_private.a.agno, sbno);
  93. args.minlen = 1;
  94. args.maxlen = 1;
  95. args.prod = 1;
  96. args.type = XFS_ALLOCTYPE_NEAR_BNO;
  97. args.resv = resv;
  98. error = xfs_alloc_vextent(&args);
  99. if (error) {
  100. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  101. return error;
  102. }
  103. if (args.fsbno == NULLFSBLOCK) {
  104. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  105. *stat = 0;
  106. return 0;
  107. }
  108. ASSERT(args.len == 1);
  109. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  110. new->s = cpu_to_be32(XFS_FSB_TO_AGBNO(args.mp, args.fsbno));
  111. *stat = 1;
  112. return 0;
  113. }
  114. STATIC int
  115. xfs_inobt_alloc_block(
  116. struct xfs_btree_cur *cur,
  117. union xfs_btree_ptr *start,
  118. union xfs_btree_ptr *new,
  119. int *stat)
  120. {
  121. return __xfs_inobt_alloc_block(cur, start, new, stat, XFS_AG_RESV_NONE);
  122. }
  123. STATIC int
  124. xfs_finobt_alloc_block(
  125. struct xfs_btree_cur *cur,
  126. union xfs_btree_ptr *start,
  127. union xfs_btree_ptr *new,
  128. int *stat)
  129. {
  130. if (cur->bc_mp->m_inotbt_nores)
  131. return xfs_inobt_alloc_block(cur, start, new, stat);
  132. return __xfs_inobt_alloc_block(cur, start, new, stat,
  133. XFS_AG_RESV_METADATA);
  134. }
  135. STATIC int
  136. __xfs_inobt_free_block(
  137. struct xfs_btree_cur *cur,
  138. struct xfs_buf *bp,
  139. enum xfs_ag_resv_type resv)
  140. {
  141. struct xfs_owner_info oinfo;
  142. xfs_rmap_ag_owner(&oinfo, XFS_RMAP_OWN_INOBT);
  143. return xfs_free_extent(cur->bc_tp,
  144. XFS_DADDR_TO_FSB(cur->bc_mp, XFS_BUF_ADDR(bp)), 1,
  145. &oinfo, resv);
  146. }
  147. STATIC int
  148. xfs_inobt_free_block(
  149. struct xfs_btree_cur *cur,
  150. struct xfs_buf *bp)
  151. {
  152. return __xfs_inobt_free_block(cur, bp, XFS_AG_RESV_NONE);
  153. }
  154. STATIC int
  155. xfs_finobt_free_block(
  156. struct xfs_btree_cur *cur,
  157. struct xfs_buf *bp)
  158. {
  159. if (cur->bc_mp->m_inotbt_nores)
  160. return xfs_inobt_free_block(cur, bp);
  161. return __xfs_inobt_free_block(cur, bp, XFS_AG_RESV_METADATA);
  162. }
  163. STATIC int
  164. xfs_inobt_get_maxrecs(
  165. struct xfs_btree_cur *cur,
  166. int level)
  167. {
  168. return cur->bc_mp->m_inobt_mxr[level != 0];
  169. }
  170. STATIC void
  171. xfs_inobt_init_key_from_rec(
  172. union xfs_btree_key *key,
  173. union xfs_btree_rec *rec)
  174. {
  175. key->inobt.ir_startino = rec->inobt.ir_startino;
  176. }
  177. STATIC void
  178. xfs_inobt_init_high_key_from_rec(
  179. union xfs_btree_key *key,
  180. union xfs_btree_rec *rec)
  181. {
  182. __u32 x;
  183. x = be32_to_cpu(rec->inobt.ir_startino);
  184. x += XFS_INODES_PER_CHUNK - 1;
  185. key->inobt.ir_startino = cpu_to_be32(x);
  186. }
  187. STATIC void
  188. xfs_inobt_init_rec_from_cur(
  189. struct xfs_btree_cur *cur,
  190. union xfs_btree_rec *rec)
  191. {
  192. rec->inobt.ir_startino = cpu_to_be32(cur->bc_rec.i.ir_startino);
  193. if (xfs_sb_version_hassparseinodes(&cur->bc_mp->m_sb)) {
  194. rec->inobt.ir_u.sp.ir_holemask =
  195. cpu_to_be16(cur->bc_rec.i.ir_holemask);
  196. rec->inobt.ir_u.sp.ir_count = cur->bc_rec.i.ir_count;
  197. rec->inobt.ir_u.sp.ir_freecount = cur->bc_rec.i.ir_freecount;
  198. } else {
  199. /* ir_holemask/ir_count not supported on-disk */
  200. rec->inobt.ir_u.f.ir_freecount =
  201. cpu_to_be32(cur->bc_rec.i.ir_freecount);
  202. }
  203. rec->inobt.ir_free = cpu_to_be64(cur->bc_rec.i.ir_free);
  204. }
  205. /*
  206. * initial value of ptr for lookup
  207. */
  208. STATIC void
  209. xfs_inobt_init_ptr_from_cur(
  210. struct xfs_btree_cur *cur,
  211. union xfs_btree_ptr *ptr)
  212. {
  213. struct xfs_agi *agi = XFS_BUF_TO_AGI(cur->bc_private.a.agbp);
  214. ASSERT(cur->bc_private.a.agno == be32_to_cpu(agi->agi_seqno));
  215. ptr->s = agi->agi_root;
  216. }
  217. STATIC void
  218. xfs_finobt_init_ptr_from_cur(
  219. struct xfs_btree_cur *cur,
  220. union xfs_btree_ptr *ptr)
  221. {
  222. struct xfs_agi *agi = XFS_BUF_TO_AGI(cur->bc_private.a.agbp);
  223. ASSERT(cur->bc_private.a.agno == be32_to_cpu(agi->agi_seqno));
  224. ptr->s = agi->agi_free_root;
  225. }
  226. STATIC int64_t
  227. xfs_inobt_key_diff(
  228. struct xfs_btree_cur *cur,
  229. union xfs_btree_key *key)
  230. {
  231. return (int64_t)be32_to_cpu(key->inobt.ir_startino) -
  232. cur->bc_rec.i.ir_startino;
  233. }
  234. STATIC int64_t
  235. xfs_inobt_diff_two_keys(
  236. struct xfs_btree_cur *cur,
  237. union xfs_btree_key *k1,
  238. union xfs_btree_key *k2)
  239. {
  240. return (int64_t)be32_to_cpu(k1->inobt.ir_startino) -
  241. be32_to_cpu(k2->inobt.ir_startino);
  242. }
  243. static xfs_failaddr_t
  244. xfs_inobt_verify(
  245. struct xfs_buf *bp)
  246. {
  247. struct xfs_mount *mp = bp->b_target->bt_mount;
  248. struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
  249. xfs_failaddr_t fa;
  250. unsigned int level;
  251. /*
  252. * During growfs operations, we can't verify the exact owner as the
  253. * perag is not fully initialised and hence not attached to the buffer.
  254. *
  255. * Similarly, during log recovery we will have a perag structure
  256. * attached, but the agi information will not yet have been initialised
  257. * from the on disk AGI. We don't currently use any of this information,
  258. * but beware of the landmine (i.e. need to check pag->pagi_init) if we
  259. * ever do.
  260. */
  261. switch (block->bb_magic) {
  262. case cpu_to_be32(XFS_IBT_CRC_MAGIC):
  263. case cpu_to_be32(XFS_FIBT_CRC_MAGIC):
  264. fa = xfs_btree_sblock_v5hdr_verify(bp);
  265. if (fa)
  266. return fa;
  267. /* fall through */
  268. case cpu_to_be32(XFS_IBT_MAGIC):
  269. case cpu_to_be32(XFS_FIBT_MAGIC):
  270. break;
  271. default:
  272. return NULL;
  273. }
  274. /* level verification */
  275. level = be16_to_cpu(block->bb_level);
  276. if (level >= mp->m_in_maxlevels)
  277. return __this_address;
  278. return xfs_btree_sblock_verify(bp, mp->m_inobt_mxr[level != 0]);
  279. }
  280. static void
  281. xfs_inobt_read_verify(
  282. struct xfs_buf *bp)
  283. {
  284. xfs_failaddr_t fa;
  285. if (!xfs_btree_sblock_verify_crc(bp))
  286. xfs_verifier_error(bp, -EFSBADCRC, __this_address);
  287. else {
  288. fa = xfs_inobt_verify(bp);
  289. if (fa)
  290. xfs_verifier_error(bp, -EFSCORRUPTED, fa);
  291. }
  292. if (bp->b_error)
  293. trace_xfs_btree_corrupt(bp, _RET_IP_);
  294. }
  295. static void
  296. xfs_inobt_write_verify(
  297. struct xfs_buf *bp)
  298. {
  299. xfs_failaddr_t fa;
  300. fa = xfs_inobt_verify(bp);
  301. if (fa) {
  302. trace_xfs_btree_corrupt(bp, _RET_IP_);
  303. xfs_verifier_error(bp, -EFSCORRUPTED, fa);
  304. return;
  305. }
  306. xfs_btree_sblock_calc_crc(bp);
  307. }
  308. const struct xfs_buf_ops xfs_inobt_buf_ops = {
  309. .name = "xfs_inobt",
  310. .verify_read = xfs_inobt_read_verify,
  311. .verify_write = xfs_inobt_write_verify,
  312. .verify_struct = xfs_inobt_verify,
  313. };
  314. STATIC int
  315. xfs_inobt_keys_inorder(
  316. struct xfs_btree_cur *cur,
  317. union xfs_btree_key *k1,
  318. union xfs_btree_key *k2)
  319. {
  320. return be32_to_cpu(k1->inobt.ir_startino) <
  321. be32_to_cpu(k2->inobt.ir_startino);
  322. }
  323. STATIC int
  324. xfs_inobt_recs_inorder(
  325. struct xfs_btree_cur *cur,
  326. union xfs_btree_rec *r1,
  327. union xfs_btree_rec *r2)
  328. {
  329. return be32_to_cpu(r1->inobt.ir_startino) + XFS_INODES_PER_CHUNK <=
  330. be32_to_cpu(r2->inobt.ir_startino);
  331. }
  332. static const struct xfs_btree_ops xfs_inobt_ops = {
  333. .rec_len = sizeof(xfs_inobt_rec_t),
  334. .key_len = sizeof(xfs_inobt_key_t),
  335. .dup_cursor = xfs_inobt_dup_cursor,
  336. .set_root = xfs_inobt_set_root,
  337. .alloc_block = xfs_inobt_alloc_block,
  338. .free_block = xfs_inobt_free_block,
  339. .get_minrecs = xfs_inobt_get_minrecs,
  340. .get_maxrecs = xfs_inobt_get_maxrecs,
  341. .init_key_from_rec = xfs_inobt_init_key_from_rec,
  342. .init_high_key_from_rec = xfs_inobt_init_high_key_from_rec,
  343. .init_rec_from_cur = xfs_inobt_init_rec_from_cur,
  344. .init_ptr_from_cur = xfs_inobt_init_ptr_from_cur,
  345. .key_diff = xfs_inobt_key_diff,
  346. .buf_ops = &xfs_inobt_buf_ops,
  347. .diff_two_keys = xfs_inobt_diff_two_keys,
  348. .keys_inorder = xfs_inobt_keys_inorder,
  349. .recs_inorder = xfs_inobt_recs_inorder,
  350. };
  351. static const struct xfs_btree_ops xfs_finobt_ops = {
  352. .rec_len = sizeof(xfs_inobt_rec_t),
  353. .key_len = sizeof(xfs_inobt_key_t),
  354. .dup_cursor = xfs_inobt_dup_cursor,
  355. .set_root = xfs_finobt_set_root,
  356. .alloc_block = xfs_finobt_alloc_block,
  357. .free_block = xfs_finobt_free_block,
  358. .get_minrecs = xfs_inobt_get_minrecs,
  359. .get_maxrecs = xfs_inobt_get_maxrecs,
  360. .init_key_from_rec = xfs_inobt_init_key_from_rec,
  361. .init_high_key_from_rec = xfs_inobt_init_high_key_from_rec,
  362. .init_rec_from_cur = xfs_inobt_init_rec_from_cur,
  363. .init_ptr_from_cur = xfs_finobt_init_ptr_from_cur,
  364. .key_diff = xfs_inobt_key_diff,
  365. .buf_ops = &xfs_inobt_buf_ops,
  366. .diff_two_keys = xfs_inobt_diff_two_keys,
  367. .keys_inorder = xfs_inobt_keys_inorder,
  368. .recs_inorder = xfs_inobt_recs_inorder,
  369. };
  370. /*
  371. * Allocate a new inode btree cursor.
  372. */
  373. struct xfs_btree_cur * /* new inode btree cursor */
  374. xfs_inobt_init_cursor(
  375. struct xfs_mount *mp, /* file system mount point */
  376. struct xfs_trans *tp, /* transaction pointer */
  377. struct xfs_buf *agbp, /* buffer for agi structure */
  378. xfs_agnumber_t agno, /* allocation group number */
  379. xfs_btnum_t btnum) /* ialloc or free ino btree */
  380. {
  381. struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp);
  382. struct xfs_btree_cur *cur;
  383. cur = kmem_zone_zalloc(xfs_btree_cur_zone, KM_NOFS);
  384. cur->bc_tp = tp;
  385. cur->bc_mp = mp;
  386. cur->bc_btnum = btnum;
  387. if (btnum == XFS_BTNUM_INO) {
  388. cur->bc_nlevels = be32_to_cpu(agi->agi_level);
  389. cur->bc_ops = &xfs_inobt_ops;
  390. cur->bc_statoff = XFS_STATS_CALC_INDEX(xs_ibt_2);
  391. } else {
  392. cur->bc_nlevels = be32_to_cpu(agi->agi_free_level);
  393. cur->bc_ops = &xfs_finobt_ops;
  394. cur->bc_statoff = XFS_STATS_CALC_INDEX(xs_fibt_2);
  395. }
  396. cur->bc_blocklog = mp->m_sb.sb_blocklog;
  397. if (xfs_sb_version_hascrc(&mp->m_sb))
  398. cur->bc_flags |= XFS_BTREE_CRC_BLOCKS;
  399. cur->bc_private.a.agbp = agbp;
  400. cur->bc_private.a.agno = agno;
  401. return cur;
  402. }
  403. /*
  404. * Calculate number of records in an inobt btree block.
  405. */
  406. int
  407. xfs_inobt_maxrecs(
  408. struct xfs_mount *mp,
  409. int blocklen,
  410. int leaf)
  411. {
  412. blocklen -= XFS_INOBT_BLOCK_LEN(mp);
  413. if (leaf)
  414. return blocklen / sizeof(xfs_inobt_rec_t);
  415. return blocklen / (sizeof(xfs_inobt_key_t) + sizeof(xfs_inobt_ptr_t));
  416. }
  417. /*
  418. * Convert the inode record holemask to an inode allocation bitmap. The inode
  419. * allocation bitmap is inode granularity and specifies whether an inode is
  420. * physically allocated on disk (not whether the inode is considered allocated
  421. * or free by the fs).
  422. *
  423. * A bit value of 1 means the inode is allocated, a value of 0 means it is free.
  424. */
  425. uint64_t
  426. xfs_inobt_irec_to_allocmask(
  427. struct xfs_inobt_rec_incore *rec)
  428. {
  429. uint64_t bitmap = 0;
  430. uint64_t inodespbit;
  431. int nextbit;
  432. uint allocbitmap;
  433. /*
  434. * The holemask has 16-bits for a 64 inode record. Therefore each
  435. * holemask bit represents multiple inodes. Create a mask of bits to set
  436. * in the allocmask for each holemask bit.
  437. */
  438. inodespbit = (1 << XFS_INODES_PER_HOLEMASK_BIT) - 1;
  439. /*
  440. * Allocated inodes are represented by 0 bits in holemask. Invert the 0
  441. * bits to 1 and convert to a uint so we can use xfs_next_bit(). Mask
  442. * anything beyond the 16 holemask bits since this casts to a larger
  443. * type.
  444. */
  445. allocbitmap = ~rec->ir_holemask & ((1 << XFS_INOBT_HOLEMASK_BITS) - 1);
  446. /*
  447. * allocbitmap is the inverted holemask so every set bit represents
  448. * allocated inodes. To expand from 16-bit holemask granularity to
  449. * 64-bit (e.g., bit-per-inode), set inodespbit bits in the target
  450. * bitmap for every holemask bit.
  451. */
  452. nextbit = xfs_next_bit(&allocbitmap, 1, 0);
  453. while (nextbit != -1) {
  454. ASSERT(nextbit < (sizeof(rec->ir_holemask) * NBBY));
  455. bitmap |= (inodespbit <<
  456. (nextbit * XFS_INODES_PER_HOLEMASK_BIT));
  457. nextbit = xfs_next_bit(&allocbitmap, 1, nextbit + 1);
  458. }
  459. return bitmap;
  460. }
  461. #if defined(DEBUG) || defined(XFS_WARN)
  462. /*
  463. * Verify that an in-core inode record has a valid inode count.
  464. */
  465. int
  466. xfs_inobt_rec_check_count(
  467. struct xfs_mount *mp,
  468. struct xfs_inobt_rec_incore *rec)
  469. {
  470. int inocount = 0;
  471. int nextbit = 0;
  472. uint64_t allocbmap;
  473. int wordsz;
  474. wordsz = sizeof(allocbmap) / sizeof(unsigned int);
  475. allocbmap = xfs_inobt_irec_to_allocmask(rec);
  476. nextbit = xfs_next_bit((uint *) &allocbmap, wordsz, nextbit);
  477. while (nextbit != -1) {
  478. inocount++;
  479. nextbit = xfs_next_bit((uint *) &allocbmap, wordsz,
  480. nextbit + 1);
  481. }
  482. if (inocount != rec->ir_count)
  483. return -EFSCORRUPTED;
  484. return 0;
  485. }
  486. #endif /* DEBUG */
  487. static xfs_extlen_t
  488. xfs_inobt_max_size(
  489. struct xfs_mount *mp)
  490. {
  491. /* Bail out if we're uninitialized, which can happen in mkfs. */
  492. if (mp->m_inobt_mxr[0] == 0)
  493. return 0;
  494. return xfs_btree_calc_size(mp, mp->m_inobt_mnr,
  495. (uint64_t)mp->m_sb.sb_agblocks * mp->m_sb.sb_inopblock /
  496. XFS_INODES_PER_CHUNK);
  497. }
  498. static int
  499. xfs_inobt_count_blocks(
  500. struct xfs_mount *mp,
  501. xfs_agnumber_t agno,
  502. xfs_btnum_t btnum,
  503. xfs_extlen_t *tree_blocks)
  504. {
  505. struct xfs_buf *agbp;
  506. struct xfs_btree_cur *cur;
  507. int error;
  508. error = xfs_ialloc_read_agi(mp, NULL, agno, &agbp);
  509. if (error)
  510. return error;
  511. cur = xfs_inobt_init_cursor(mp, NULL, agbp, agno, btnum);
  512. error = xfs_btree_count_blocks(cur, tree_blocks);
  513. xfs_btree_del_cursor(cur, error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
  514. xfs_buf_relse(agbp);
  515. return error;
  516. }
  517. /*
  518. * Figure out how many blocks to reserve and how many are used by this btree.
  519. */
  520. int
  521. xfs_finobt_calc_reserves(
  522. struct xfs_mount *mp,
  523. xfs_agnumber_t agno,
  524. xfs_extlen_t *ask,
  525. xfs_extlen_t *used)
  526. {
  527. xfs_extlen_t tree_len = 0;
  528. int error;
  529. if (!xfs_sb_version_hasfinobt(&mp->m_sb))
  530. return 0;
  531. error = xfs_inobt_count_blocks(mp, agno, XFS_BTNUM_FINO, &tree_len);
  532. if (error)
  533. return error;
  534. *ask += xfs_inobt_max_size(mp);
  535. *used += tree_len;
  536. return 0;
  537. }