xfs_bmap_btree.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
  4. * All Rights Reserved.
  5. */
  6. #include "xfs.h"
  7. #include "xfs_fs.h"
  8. #include "xfs_shared.h"
  9. #include "xfs_format.h"
  10. #include "xfs_log_format.h"
  11. #include "xfs_trans_resv.h"
  12. #include "xfs_bit.h"
  13. #include "xfs_mount.h"
  14. #include "xfs_inode.h"
  15. #include "xfs_trans.h"
  16. #include "xfs_alloc.h"
  17. #include "xfs_btree.h"
  18. #include "xfs_bmap_btree.h"
  19. #include "xfs_bmap.h"
  20. #include "xfs_error.h"
  21. #include "xfs_quota.h"
  22. #include "xfs_trace.h"
  23. #include "xfs_rmap.h"
  24. static struct kmem_cache *xfs_bmbt_cur_cache;
  25. /*
  26. * Convert on-disk form of btree root to in-memory form.
  27. */
  28. void
  29. xfs_bmdr_to_bmbt(
  30. struct xfs_inode *ip,
  31. xfs_bmdr_block_t *dblock,
  32. int dblocklen,
  33. struct xfs_btree_block *rblock,
  34. int rblocklen)
  35. {
  36. struct xfs_mount *mp = ip->i_mount;
  37. int dmxr;
  38. xfs_bmbt_key_t *fkp;
  39. __be64 *fpp;
  40. xfs_bmbt_key_t *tkp;
  41. __be64 *tpp;
  42. xfs_btree_init_block_int(mp, rblock, XFS_BUF_DADDR_NULL,
  43. XFS_BTNUM_BMAP, 0, 0, ip->i_ino,
  44. XFS_BTREE_LONG_PTRS);
  45. rblock->bb_level = dblock->bb_level;
  46. ASSERT(be16_to_cpu(rblock->bb_level) > 0);
  47. rblock->bb_numrecs = dblock->bb_numrecs;
  48. dmxr = xfs_bmdr_maxrecs(dblocklen, 0);
  49. fkp = XFS_BMDR_KEY_ADDR(dblock, 1);
  50. tkp = XFS_BMBT_KEY_ADDR(mp, rblock, 1);
  51. fpp = XFS_BMDR_PTR_ADDR(dblock, 1, dmxr);
  52. tpp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, rblocklen);
  53. dmxr = be16_to_cpu(dblock->bb_numrecs);
  54. memcpy(tkp, fkp, sizeof(*fkp) * dmxr);
  55. memcpy(tpp, fpp, sizeof(*fpp) * dmxr);
  56. }
  57. void
  58. xfs_bmbt_disk_get_all(
  59. const struct xfs_bmbt_rec *rec,
  60. struct xfs_bmbt_irec *irec)
  61. {
  62. uint64_t l0 = get_unaligned_be64(&rec->l0);
  63. uint64_t l1 = get_unaligned_be64(&rec->l1);
  64. irec->br_startoff = (l0 & xfs_mask64lo(64 - BMBT_EXNTFLAG_BITLEN)) >> 9;
  65. irec->br_startblock = ((l0 & xfs_mask64lo(9)) << 43) | (l1 >> 21);
  66. irec->br_blockcount = l1 & xfs_mask64lo(21);
  67. if (l0 >> (64 - BMBT_EXNTFLAG_BITLEN))
  68. irec->br_state = XFS_EXT_UNWRITTEN;
  69. else
  70. irec->br_state = XFS_EXT_NORM;
  71. }
  72. /*
  73. * Extract the blockcount field from an on disk bmap extent record.
  74. */
  75. xfs_filblks_t
  76. xfs_bmbt_disk_get_blockcount(
  77. const struct xfs_bmbt_rec *r)
  78. {
  79. return (xfs_filblks_t)(be64_to_cpu(r->l1) & xfs_mask64lo(21));
  80. }
  81. /*
  82. * Extract the startoff field from a disk format bmap extent record.
  83. */
  84. xfs_fileoff_t
  85. xfs_bmbt_disk_get_startoff(
  86. const struct xfs_bmbt_rec *r)
  87. {
  88. return ((xfs_fileoff_t)be64_to_cpu(r->l0) &
  89. xfs_mask64lo(64 - BMBT_EXNTFLAG_BITLEN)) >> 9;
  90. }
  91. /*
  92. * Set all the fields in a bmap extent record from the uncompressed form.
  93. */
  94. void
  95. xfs_bmbt_disk_set_all(
  96. struct xfs_bmbt_rec *r,
  97. struct xfs_bmbt_irec *s)
  98. {
  99. int extent_flag = (s->br_state != XFS_EXT_NORM);
  100. ASSERT(s->br_state == XFS_EXT_NORM || s->br_state == XFS_EXT_UNWRITTEN);
  101. ASSERT(!(s->br_startoff & xfs_mask64hi(64-BMBT_STARTOFF_BITLEN)));
  102. ASSERT(!(s->br_blockcount & xfs_mask64hi(64-BMBT_BLOCKCOUNT_BITLEN)));
  103. ASSERT(!(s->br_startblock & xfs_mask64hi(64-BMBT_STARTBLOCK_BITLEN)));
  104. put_unaligned_be64(
  105. ((xfs_bmbt_rec_base_t)extent_flag << 63) |
  106. ((xfs_bmbt_rec_base_t)s->br_startoff << 9) |
  107. ((xfs_bmbt_rec_base_t)s->br_startblock >> 43), &r->l0);
  108. put_unaligned_be64(
  109. ((xfs_bmbt_rec_base_t)s->br_startblock << 21) |
  110. ((xfs_bmbt_rec_base_t)s->br_blockcount &
  111. (xfs_bmbt_rec_base_t)xfs_mask64lo(21)), &r->l1);
  112. }
  113. /*
  114. * Convert in-memory form of btree root to on-disk form.
  115. */
  116. void
  117. xfs_bmbt_to_bmdr(
  118. struct xfs_mount *mp,
  119. struct xfs_btree_block *rblock,
  120. int rblocklen,
  121. xfs_bmdr_block_t *dblock,
  122. int dblocklen)
  123. {
  124. int dmxr;
  125. xfs_bmbt_key_t *fkp;
  126. __be64 *fpp;
  127. xfs_bmbt_key_t *tkp;
  128. __be64 *tpp;
  129. if (xfs_has_crc(mp)) {
  130. ASSERT(rblock->bb_magic == cpu_to_be32(XFS_BMAP_CRC_MAGIC));
  131. ASSERT(uuid_equal(&rblock->bb_u.l.bb_uuid,
  132. &mp->m_sb.sb_meta_uuid));
  133. ASSERT(rblock->bb_u.l.bb_blkno ==
  134. cpu_to_be64(XFS_BUF_DADDR_NULL));
  135. } else
  136. ASSERT(rblock->bb_magic == cpu_to_be32(XFS_BMAP_MAGIC));
  137. ASSERT(rblock->bb_u.l.bb_leftsib == cpu_to_be64(NULLFSBLOCK));
  138. ASSERT(rblock->bb_u.l.bb_rightsib == cpu_to_be64(NULLFSBLOCK));
  139. ASSERT(rblock->bb_level != 0);
  140. dblock->bb_level = rblock->bb_level;
  141. dblock->bb_numrecs = rblock->bb_numrecs;
  142. dmxr = xfs_bmdr_maxrecs(dblocklen, 0);
  143. fkp = XFS_BMBT_KEY_ADDR(mp, rblock, 1);
  144. tkp = XFS_BMDR_KEY_ADDR(dblock, 1);
  145. fpp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, rblocklen);
  146. tpp = XFS_BMDR_PTR_ADDR(dblock, 1, dmxr);
  147. dmxr = be16_to_cpu(dblock->bb_numrecs);
  148. memcpy(tkp, fkp, sizeof(*fkp) * dmxr);
  149. memcpy(tpp, fpp, sizeof(*fpp) * dmxr);
  150. }
  151. STATIC struct xfs_btree_cur *
  152. xfs_bmbt_dup_cursor(
  153. struct xfs_btree_cur *cur)
  154. {
  155. struct xfs_btree_cur *new;
  156. new = xfs_bmbt_init_cursor(cur->bc_mp, cur->bc_tp,
  157. cur->bc_ino.ip, cur->bc_ino.whichfork);
  158. /*
  159. * Copy the firstblock, dfops, and flags values,
  160. * since init cursor doesn't get them.
  161. */
  162. new->bc_ino.flags = cur->bc_ino.flags;
  163. return new;
  164. }
  165. STATIC void
  166. xfs_bmbt_update_cursor(
  167. struct xfs_btree_cur *src,
  168. struct xfs_btree_cur *dst)
  169. {
  170. ASSERT((dst->bc_tp->t_firstblock != NULLFSBLOCK) ||
  171. (dst->bc_ino.ip->i_diflags & XFS_DIFLAG_REALTIME));
  172. dst->bc_ino.allocated += src->bc_ino.allocated;
  173. dst->bc_tp->t_firstblock = src->bc_tp->t_firstblock;
  174. src->bc_ino.allocated = 0;
  175. }
  176. STATIC int
  177. xfs_bmbt_alloc_block(
  178. struct xfs_btree_cur *cur,
  179. const union xfs_btree_ptr *start,
  180. union xfs_btree_ptr *new,
  181. int *stat)
  182. {
  183. xfs_alloc_arg_t args; /* block allocation args */
  184. int error; /* error return value */
  185. memset(&args, 0, sizeof(args));
  186. args.tp = cur->bc_tp;
  187. args.mp = cur->bc_mp;
  188. args.fsbno = cur->bc_tp->t_firstblock;
  189. xfs_rmap_ino_bmbt_owner(&args.oinfo, cur->bc_ino.ip->i_ino,
  190. cur->bc_ino.whichfork);
  191. if (args.fsbno == NULLFSBLOCK) {
  192. args.fsbno = be64_to_cpu(start->l);
  193. args.type = XFS_ALLOCTYPE_START_BNO;
  194. /*
  195. * Make sure there is sufficient room left in the AG to
  196. * complete a full tree split for an extent insert. If
  197. * we are converting the middle part of an extent then
  198. * we may need space for two tree splits.
  199. *
  200. * We are relying on the caller to make the correct block
  201. * reservation for this operation to succeed. If the
  202. * reservation amount is insufficient then we may fail a
  203. * block allocation here and corrupt the filesystem.
  204. */
  205. args.minleft = args.tp->t_blk_res;
  206. } else if (cur->bc_tp->t_flags & XFS_TRANS_LOWMODE) {
  207. args.type = XFS_ALLOCTYPE_START_BNO;
  208. } else {
  209. args.type = XFS_ALLOCTYPE_NEAR_BNO;
  210. }
  211. args.minlen = args.maxlen = args.prod = 1;
  212. args.wasdel = cur->bc_ino.flags & XFS_BTCUR_BMBT_WASDEL;
  213. if (!args.wasdel && args.tp->t_blk_res == 0) {
  214. error = -ENOSPC;
  215. goto error0;
  216. }
  217. error = xfs_alloc_vextent(&args);
  218. if (error)
  219. goto error0;
  220. if (args.fsbno == NULLFSBLOCK && args.minleft) {
  221. /*
  222. * Could not find an AG with enough free space to satisfy
  223. * a full btree split. Try again and if
  224. * successful activate the lowspace algorithm.
  225. */
  226. args.fsbno = 0;
  227. args.type = XFS_ALLOCTYPE_FIRST_AG;
  228. error = xfs_alloc_vextent(&args);
  229. if (error)
  230. goto error0;
  231. cur->bc_tp->t_flags |= XFS_TRANS_LOWMODE;
  232. }
  233. if (WARN_ON_ONCE(args.fsbno == NULLFSBLOCK)) {
  234. *stat = 0;
  235. return 0;
  236. }
  237. ASSERT(args.len == 1);
  238. cur->bc_tp->t_firstblock = args.fsbno;
  239. cur->bc_ino.allocated++;
  240. cur->bc_ino.ip->i_nblocks++;
  241. xfs_trans_log_inode(args.tp, cur->bc_ino.ip, XFS_ILOG_CORE);
  242. xfs_trans_mod_dquot_byino(args.tp, cur->bc_ino.ip,
  243. XFS_TRANS_DQ_BCOUNT, 1L);
  244. new->l = cpu_to_be64(args.fsbno);
  245. *stat = 1;
  246. return 0;
  247. error0:
  248. return error;
  249. }
  250. STATIC int
  251. xfs_bmbt_free_block(
  252. struct xfs_btree_cur *cur,
  253. struct xfs_buf *bp)
  254. {
  255. struct xfs_mount *mp = cur->bc_mp;
  256. struct xfs_inode *ip = cur->bc_ino.ip;
  257. struct xfs_trans *tp = cur->bc_tp;
  258. xfs_fsblock_t fsbno = XFS_DADDR_TO_FSB(mp, xfs_buf_daddr(bp));
  259. struct xfs_owner_info oinfo;
  260. xfs_rmap_ino_bmbt_owner(&oinfo, ip->i_ino, cur->bc_ino.whichfork);
  261. xfs_free_extent_later(cur->bc_tp, fsbno, 1, &oinfo);
  262. ip->i_nblocks--;
  263. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  264. xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
  265. return 0;
  266. }
  267. STATIC int
  268. xfs_bmbt_get_minrecs(
  269. struct xfs_btree_cur *cur,
  270. int level)
  271. {
  272. if (level == cur->bc_nlevels - 1) {
  273. struct xfs_ifork *ifp;
  274. ifp = xfs_ifork_ptr(cur->bc_ino.ip,
  275. cur->bc_ino.whichfork);
  276. return xfs_bmbt_maxrecs(cur->bc_mp,
  277. ifp->if_broot_bytes, level == 0) / 2;
  278. }
  279. return cur->bc_mp->m_bmap_dmnr[level != 0];
  280. }
  281. int
  282. xfs_bmbt_get_maxrecs(
  283. struct xfs_btree_cur *cur,
  284. int level)
  285. {
  286. if (level == cur->bc_nlevels - 1) {
  287. struct xfs_ifork *ifp;
  288. ifp = xfs_ifork_ptr(cur->bc_ino.ip,
  289. cur->bc_ino.whichfork);
  290. return xfs_bmbt_maxrecs(cur->bc_mp,
  291. ifp->if_broot_bytes, level == 0);
  292. }
  293. return cur->bc_mp->m_bmap_dmxr[level != 0];
  294. }
  295. /*
  296. * Get the maximum records we could store in the on-disk format.
  297. *
  298. * For non-root nodes this is equivalent to xfs_bmbt_get_maxrecs, but
  299. * for the root node this checks the available space in the dinode fork
  300. * so that we can resize the in-memory buffer to match it. After a
  301. * resize to the maximum size this function returns the same value
  302. * as xfs_bmbt_get_maxrecs for the root node, too.
  303. */
  304. STATIC int
  305. xfs_bmbt_get_dmaxrecs(
  306. struct xfs_btree_cur *cur,
  307. int level)
  308. {
  309. if (level != cur->bc_nlevels - 1)
  310. return cur->bc_mp->m_bmap_dmxr[level != 0];
  311. return xfs_bmdr_maxrecs(cur->bc_ino.forksize, level == 0);
  312. }
  313. STATIC void
  314. xfs_bmbt_init_key_from_rec(
  315. union xfs_btree_key *key,
  316. const union xfs_btree_rec *rec)
  317. {
  318. key->bmbt.br_startoff =
  319. cpu_to_be64(xfs_bmbt_disk_get_startoff(&rec->bmbt));
  320. }
  321. STATIC void
  322. xfs_bmbt_init_high_key_from_rec(
  323. union xfs_btree_key *key,
  324. const union xfs_btree_rec *rec)
  325. {
  326. key->bmbt.br_startoff = cpu_to_be64(
  327. xfs_bmbt_disk_get_startoff(&rec->bmbt) +
  328. xfs_bmbt_disk_get_blockcount(&rec->bmbt) - 1);
  329. }
  330. STATIC void
  331. xfs_bmbt_init_rec_from_cur(
  332. struct xfs_btree_cur *cur,
  333. union xfs_btree_rec *rec)
  334. {
  335. xfs_bmbt_disk_set_all(&rec->bmbt, &cur->bc_rec.b);
  336. }
  337. STATIC void
  338. xfs_bmbt_init_ptr_from_cur(
  339. struct xfs_btree_cur *cur,
  340. union xfs_btree_ptr *ptr)
  341. {
  342. ptr->l = 0;
  343. }
  344. STATIC int64_t
  345. xfs_bmbt_key_diff(
  346. struct xfs_btree_cur *cur,
  347. const union xfs_btree_key *key)
  348. {
  349. return (int64_t)be64_to_cpu(key->bmbt.br_startoff) -
  350. cur->bc_rec.b.br_startoff;
  351. }
  352. STATIC int64_t
  353. xfs_bmbt_diff_two_keys(
  354. struct xfs_btree_cur *cur,
  355. const union xfs_btree_key *k1,
  356. const union xfs_btree_key *k2)
  357. {
  358. uint64_t a = be64_to_cpu(k1->bmbt.br_startoff);
  359. uint64_t b = be64_to_cpu(k2->bmbt.br_startoff);
  360. /*
  361. * Note: This routine previously casted a and b to int64 and subtracted
  362. * them to generate a result. This lead to problems if b was the
  363. * "maximum" key value (all ones) being signed incorrectly, hence this
  364. * somewhat less efficient version.
  365. */
  366. if (a > b)
  367. return 1;
  368. if (b > a)
  369. return -1;
  370. return 0;
  371. }
  372. static xfs_failaddr_t
  373. xfs_bmbt_verify(
  374. struct xfs_buf *bp)
  375. {
  376. struct xfs_mount *mp = bp->b_mount;
  377. struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
  378. xfs_failaddr_t fa;
  379. unsigned int level;
  380. if (!xfs_verify_magic(bp, block->bb_magic))
  381. return __this_address;
  382. if (xfs_has_crc(mp)) {
  383. /*
  384. * XXX: need a better way of verifying the owner here. Right now
  385. * just make sure there has been one set.
  386. */
  387. fa = xfs_btree_lblock_v5hdr_verify(bp, XFS_RMAP_OWN_UNKNOWN);
  388. if (fa)
  389. return fa;
  390. }
  391. /*
  392. * numrecs and level verification.
  393. *
  394. * We don't know what fork we belong to, so just verify that the level
  395. * is less than the maximum of the two. Later checks will be more
  396. * precise.
  397. */
  398. level = be16_to_cpu(block->bb_level);
  399. if (level > max(mp->m_bm_maxlevels[0], mp->m_bm_maxlevels[1]))
  400. return __this_address;
  401. return xfs_btree_lblock_verify(bp, mp->m_bmap_dmxr[level != 0]);
  402. }
  403. static void
  404. xfs_bmbt_read_verify(
  405. struct xfs_buf *bp)
  406. {
  407. xfs_failaddr_t fa;
  408. if (!xfs_btree_lblock_verify_crc(bp))
  409. xfs_verifier_error(bp, -EFSBADCRC, __this_address);
  410. else {
  411. fa = xfs_bmbt_verify(bp);
  412. if (fa)
  413. xfs_verifier_error(bp, -EFSCORRUPTED, fa);
  414. }
  415. if (bp->b_error)
  416. trace_xfs_btree_corrupt(bp, _RET_IP_);
  417. }
  418. static void
  419. xfs_bmbt_write_verify(
  420. struct xfs_buf *bp)
  421. {
  422. xfs_failaddr_t fa;
  423. fa = xfs_bmbt_verify(bp);
  424. if (fa) {
  425. trace_xfs_btree_corrupt(bp, _RET_IP_);
  426. xfs_verifier_error(bp, -EFSCORRUPTED, fa);
  427. return;
  428. }
  429. xfs_btree_lblock_calc_crc(bp);
  430. }
  431. const struct xfs_buf_ops xfs_bmbt_buf_ops = {
  432. .name = "xfs_bmbt",
  433. .magic = { cpu_to_be32(XFS_BMAP_MAGIC),
  434. cpu_to_be32(XFS_BMAP_CRC_MAGIC) },
  435. .verify_read = xfs_bmbt_read_verify,
  436. .verify_write = xfs_bmbt_write_verify,
  437. .verify_struct = xfs_bmbt_verify,
  438. };
  439. STATIC int
  440. xfs_bmbt_keys_inorder(
  441. struct xfs_btree_cur *cur,
  442. const union xfs_btree_key *k1,
  443. const union xfs_btree_key *k2)
  444. {
  445. return be64_to_cpu(k1->bmbt.br_startoff) <
  446. be64_to_cpu(k2->bmbt.br_startoff);
  447. }
  448. STATIC int
  449. xfs_bmbt_recs_inorder(
  450. struct xfs_btree_cur *cur,
  451. const union xfs_btree_rec *r1,
  452. const union xfs_btree_rec *r2)
  453. {
  454. return xfs_bmbt_disk_get_startoff(&r1->bmbt) +
  455. xfs_bmbt_disk_get_blockcount(&r1->bmbt) <=
  456. xfs_bmbt_disk_get_startoff(&r2->bmbt);
  457. }
  458. static const struct xfs_btree_ops xfs_bmbt_ops = {
  459. .rec_len = sizeof(xfs_bmbt_rec_t),
  460. .key_len = sizeof(xfs_bmbt_key_t),
  461. .dup_cursor = xfs_bmbt_dup_cursor,
  462. .update_cursor = xfs_bmbt_update_cursor,
  463. .alloc_block = xfs_bmbt_alloc_block,
  464. .free_block = xfs_bmbt_free_block,
  465. .get_maxrecs = xfs_bmbt_get_maxrecs,
  466. .get_minrecs = xfs_bmbt_get_minrecs,
  467. .get_dmaxrecs = xfs_bmbt_get_dmaxrecs,
  468. .init_key_from_rec = xfs_bmbt_init_key_from_rec,
  469. .init_high_key_from_rec = xfs_bmbt_init_high_key_from_rec,
  470. .init_rec_from_cur = xfs_bmbt_init_rec_from_cur,
  471. .init_ptr_from_cur = xfs_bmbt_init_ptr_from_cur,
  472. .key_diff = xfs_bmbt_key_diff,
  473. .diff_two_keys = xfs_bmbt_diff_two_keys,
  474. .buf_ops = &xfs_bmbt_buf_ops,
  475. .keys_inorder = xfs_bmbt_keys_inorder,
  476. .recs_inorder = xfs_bmbt_recs_inorder,
  477. };
  478. /*
  479. * Allocate a new bmap btree cursor.
  480. */
  481. struct xfs_btree_cur * /* new bmap btree cursor */
  482. xfs_bmbt_init_cursor(
  483. struct xfs_mount *mp, /* file system mount point */
  484. struct xfs_trans *tp, /* transaction pointer */
  485. struct xfs_inode *ip, /* inode owning the btree */
  486. int whichfork) /* data or attr fork */
  487. {
  488. struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
  489. struct xfs_btree_cur *cur;
  490. ASSERT(whichfork != XFS_COW_FORK);
  491. cur = xfs_btree_alloc_cursor(mp, tp, XFS_BTNUM_BMAP,
  492. mp->m_bm_maxlevels[whichfork], xfs_bmbt_cur_cache);
  493. cur->bc_nlevels = be16_to_cpu(ifp->if_broot->bb_level) + 1;
  494. cur->bc_statoff = XFS_STATS_CALC_INDEX(xs_bmbt_2);
  495. cur->bc_ops = &xfs_bmbt_ops;
  496. cur->bc_flags = XFS_BTREE_LONG_PTRS | XFS_BTREE_ROOT_IN_INODE;
  497. if (xfs_has_crc(mp))
  498. cur->bc_flags |= XFS_BTREE_CRC_BLOCKS;
  499. cur->bc_ino.forksize = xfs_inode_fork_size(ip, whichfork);
  500. cur->bc_ino.ip = ip;
  501. cur->bc_ino.allocated = 0;
  502. cur->bc_ino.flags = 0;
  503. cur->bc_ino.whichfork = whichfork;
  504. return cur;
  505. }
  506. /* Calculate number of records in a block mapping btree block. */
  507. static inline unsigned int
  508. xfs_bmbt_block_maxrecs(
  509. unsigned int blocklen,
  510. bool leaf)
  511. {
  512. if (leaf)
  513. return blocklen / sizeof(xfs_bmbt_rec_t);
  514. return blocklen / (sizeof(xfs_bmbt_key_t) + sizeof(xfs_bmbt_ptr_t));
  515. }
  516. /*
  517. * Calculate number of records in a bmap btree block.
  518. */
  519. int
  520. xfs_bmbt_maxrecs(
  521. struct xfs_mount *mp,
  522. int blocklen,
  523. int leaf)
  524. {
  525. blocklen -= XFS_BMBT_BLOCK_LEN(mp);
  526. return xfs_bmbt_block_maxrecs(blocklen, leaf);
  527. }
  528. /*
  529. * Calculate the maximum possible height of the btree that the on-disk format
  530. * supports. This is used for sizing structures large enough to support every
  531. * possible configuration of a filesystem that might get mounted.
  532. */
  533. unsigned int
  534. xfs_bmbt_maxlevels_ondisk(void)
  535. {
  536. unsigned int minrecs[2];
  537. unsigned int blocklen;
  538. blocklen = min(XFS_MIN_BLOCKSIZE - XFS_BTREE_SBLOCK_LEN,
  539. XFS_MIN_CRC_BLOCKSIZE - XFS_BTREE_SBLOCK_CRC_LEN);
  540. minrecs[0] = xfs_bmbt_block_maxrecs(blocklen, true) / 2;
  541. minrecs[1] = xfs_bmbt_block_maxrecs(blocklen, false) / 2;
  542. /* One extra level for the inode root. */
  543. return xfs_btree_compute_maxlevels(minrecs,
  544. XFS_MAX_EXTCNT_DATA_FORK_LARGE) + 1;
  545. }
  546. /*
  547. * Calculate number of records in a bmap btree inode root.
  548. */
  549. int
  550. xfs_bmdr_maxrecs(
  551. int blocklen,
  552. int leaf)
  553. {
  554. blocklen -= sizeof(xfs_bmdr_block_t);
  555. if (leaf)
  556. return blocklen / sizeof(xfs_bmdr_rec_t);
  557. return blocklen / (sizeof(xfs_bmdr_key_t) + sizeof(xfs_bmdr_ptr_t));
  558. }
  559. /*
  560. * Change the owner of a btree format fork fo the inode passed in. Change it to
  561. * the owner of that is passed in so that we can change owners before or after
  562. * we switch forks between inodes. The operation that the caller is doing will
  563. * determine whether is needs to change owner before or after the switch.
  564. *
  565. * For demand paged transactional modification, the fork switch should be done
  566. * after reading in all the blocks, modifying them and pinning them in the
  567. * transaction. For modification when the buffers are already pinned in memory,
  568. * the fork switch can be done before changing the owner as we won't need to
  569. * validate the owner until the btree buffers are unpinned and writes can occur
  570. * again.
  571. *
  572. * For recovery based ownership change, there is no transactional context and
  573. * so a buffer list must be supplied so that we can record the buffers that we
  574. * modified for the caller to issue IO on.
  575. */
  576. int
  577. xfs_bmbt_change_owner(
  578. struct xfs_trans *tp,
  579. struct xfs_inode *ip,
  580. int whichfork,
  581. xfs_ino_t new_owner,
  582. struct list_head *buffer_list)
  583. {
  584. struct xfs_btree_cur *cur;
  585. int error;
  586. ASSERT(tp || buffer_list);
  587. ASSERT(!(tp && buffer_list));
  588. ASSERT(xfs_ifork_ptr(ip, whichfork)->if_format == XFS_DINODE_FMT_BTREE);
  589. cur = xfs_bmbt_init_cursor(ip->i_mount, tp, ip, whichfork);
  590. cur->bc_ino.flags |= XFS_BTCUR_BMBT_INVALID_OWNER;
  591. error = xfs_btree_change_owner(cur, new_owner, buffer_list);
  592. xfs_btree_del_cursor(cur, error);
  593. return error;
  594. }
  595. /* Calculate the bmap btree size for some records. */
  596. unsigned long long
  597. xfs_bmbt_calc_size(
  598. struct xfs_mount *mp,
  599. unsigned long long len)
  600. {
  601. return xfs_btree_calc_size(mp->m_bmap_dmnr, len);
  602. }
  603. int __init
  604. xfs_bmbt_init_cur_cache(void)
  605. {
  606. xfs_bmbt_cur_cache = kmem_cache_create("xfs_bmbt_cur",
  607. xfs_btree_cur_sizeof(xfs_bmbt_maxlevels_ondisk()),
  608. 0, 0, NULL);
  609. if (!xfs_bmbt_cur_cache)
  610. return -ENOMEM;
  611. return 0;
  612. }
  613. void
  614. xfs_bmbt_destroy_cur_cache(void)
  615. {
  616. kmem_cache_destroy(xfs_bmbt_cur_cache);
  617. xfs_bmbt_cur_cache = NULL;
  618. }