xfs_bmap_btree.c 19 KB

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