xfs_rmap_btree.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2014 Red Hat, 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_mount.h"
  13. #include "xfs_trans.h"
  14. #include "xfs_alloc.h"
  15. #include "xfs_btree.h"
  16. #include "xfs_btree_staging.h"
  17. #include "xfs_rmap.h"
  18. #include "xfs_rmap_btree.h"
  19. #include "xfs_health.h"
  20. #include "xfs_trace.h"
  21. #include "xfs_error.h"
  22. #include "xfs_extent_busy.h"
  23. #include "xfs_ag.h"
  24. #include "xfs_ag_resv.h"
  25. #include "xfs_buf_mem.h"
  26. #include "xfs_btree_mem.h"
  27. static struct kmem_cache *xfs_rmapbt_cur_cache;
  28. /*
  29. * Reverse map btree.
  30. *
  31. * This is a per-ag tree used to track the owner(s) of a given extent. With
  32. * reflink it is possible for there to be multiple owners, which is a departure
  33. * from classic XFS. Owner records for data extents are inserted when the
  34. * extent is mapped and removed when an extent is unmapped. Owner records for
  35. * all other block types (i.e. metadata) are inserted when an extent is
  36. * allocated and removed when an extent is freed. There can only be one owner
  37. * of a metadata extent, usually an inode or some other metadata structure like
  38. * an AG btree.
  39. *
  40. * The rmap btree is part of the free space management, so blocks for the tree
  41. * are sourced from the agfl. Hence we need transaction reservation support for
  42. * this tree so that the freelist is always large enough. This also impacts on
  43. * the minimum space we need to leave free in the AG.
  44. *
  45. * The tree is ordered by [ag block, owner, offset]. This is a large key size,
  46. * but it is the only way to enforce unique keys when a block can be owned by
  47. * multiple files at any offset. There's no need to order/search by extent
  48. * size for online updating/management of the tree. It is intended that most
  49. * reverse lookups will be to find the owner(s) of a particular block, or to
  50. * try to recover tree and file data from corrupt primary metadata.
  51. */
  52. static struct xfs_btree_cur *
  53. xfs_rmapbt_dup_cursor(
  54. struct xfs_btree_cur *cur)
  55. {
  56. return xfs_rmapbt_init_cursor(cur->bc_mp, cur->bc_tp,
  57. cur->bc_ag.agbp, cur->bc_ag.pag);
  58. }
  59. STATIC void
  60. xfs_rmapbt_set_root(
  61. struct xfs_btree_cur *cur,
  62. const union xfs_btree_ptr *ptr,
  63. int inc)
  64. {
  65. struct xfs_buf *agbp = cur->bc_ag.agbp;
  66. struct xfs_agf *agf = agbp->b_addr;
  67. ASSERT(ptr->s != 0);
  68. agf->agf_rmap_root = ptr->s;
  69. be32_add_cpu(&agf->agf_rmap_level, inc);
  70. cur->bc_ag.pag->pagf_rmap_level += inc;
  71. xfs_alloc_log_agf(cur->bc_tp, agbp, XFS_AGF_ROOTS | XFS_AGF_LEVELS);
  72. }
  73. STATIC int
  74. xfs_rmapbt_alloc_block(
  75. struct xfs_btree_cur *cur,
  76. const union xfs_btree_ptr *start,
  77. union xfs_btree_ptr *new,
  78. int *stat)
  79. {
  80. struct xfs_buf *agbp = cur->bc_ag.agbp;
  81. struct xfs_agf *agf = agbp->b_addr;
  82. struct xfs_perag *pag = cur->bc_ag.pag;
  83. struct xfs_alloc_arg args = { .len = 1 };
  84. int error;
  85. xfs_agblock_t bno;
  86. /* Allocate the new block from the freelist. If we can't, give up. */
  87. error = xfs_alloc_get_freelist(pag, cur->bc_tp, cur->bc_ag.agbp,
  88. &bno, 1);
  89. if (error)
  90. return error;
  91. if (bno == NULLAGBLOCK) {
  92. *stat = 0;
  93. return 0;
  94. }
  95. xfs_extent_busy_reuse(cur->bc_mp, pag, bno, 1, false);
  96. new->s = cpu_to_be32(bno);
  97. be32_add_cpu(&agf->agf_rmap_blocks, 1);
  98. xfs_alloc_log_agf(cur->bc_tp, agbp, XFS_AGF_RMAP_BLOCKS);
  99. /*
  100. * Since rmapbt blocks are sourced from the AGFL, they are allocated one
  101. * at a time and the reservation updates don't require a transaction.
  102. */
  103. xfs_ag_resv_alloc_extent(pag, XFS_AG_RESV_RMAPBT, &args);
  104. *stat = 1;
  105. return 0;
  106. }
  107. STATIC int
  108. xfs_rmapbt_free_block(
  109. struct xfs_btree_cur *cur,
  110. struct xfs_buf *bp)
  111. {
  112. struct xfs_buf *agbp = cur->bc_ag.agbp;
  113. struct xfs_agf *agf = agbp->b_addr;
  114. struct xfs_perag *pag = cur->bc_ag.pag;
  115. xfs_agblock_t bno;
  116. int error;
  117. bno = xfs_daddr_to_agbno(cur->bc_mp, xfs_buf_daddr(bp));
  118. be32_add_cpu(&agf->agf_rmap_blocks, -1);
  119. xfs_alloc_log_agf(cur->bc_tp, agbp, XFS_AGF_RMAP_BLOCKS);
  120. error = xfs_alloc_put_freelist(pag, cur->bc_tp, agbp, NULL, bno, 1);
  121. if (error)
  122. return error;
  123. xfs_extent_busy_insert(cur->bc_tp, pag, bno, 1,
  124. XFS_EXTENT_BUSY_SKIP_DISCARD);
  125. xfs_ag_resv_free_extent(pag, XFS_AG_RESV_RMAPBT, NULL, 1);
  126. return 0;
  127. }
  128. STATIC int
  129. xfs_rmapbt_get_minrecs(
  130. struct xfs_btree_cur *cur,
  131. int level)
  132. {
  133. return cur->bc_mp->m_rmap_mnr[level != 0];
  134. }
  135. STATIC int
  136. xfs_rmapbt_get_maxrecs(
  137. struct xfs_btree_cur *cur,
  138. int level)
  139. {
  140. return cur->bc_mp->m_rmap_mxr[level != 0];
  141. }
  142. /*
  143. * Convert the ondisk record's offset field into the ondisk key's offset field.
  144. * Fork and bmbt are significant parts of the rmap record key, but written
  145. * status is merely a record attribute.
  146. */
  147. static inline __be64 ondisk_rec_offset_to_key(const union xfs_btree_rec *rec)
  148. {
  149. return rec->rmap.rm_offset & ~cpu_to_be64(XFS_RMAP_OFF_UNWRITTEN);
  150. }
  151. STATIC void
  152. xfs_rmapbt_init_key_from_rec(
  153. union xfs_btree_key *key,
  154. const union xfs_btree_rec *rec)
  155. {
  156. key->rmap.rm_startblock = rec->rmap.rm_startblock;
  157. key->rmap.rm_owner = rec->rmap.rm_owner;
  158. key->rmap.rm_offset = ondisk_rec_offset_to_key(rec);
  159. }
  160. /*
  161. * The high key for a reverse mapping record can be computed by shifting
  162. * the startblock and offset to the highest value that would still map
  163. * to that record. In practice this means that we add blockcount-1 to
  164. * the startblock for all records, and if the record is for a data/attr
  165. * fork mapping, we add blockcount-1 to the offset too.
  166. */
  167. STATIC void
  168. xfs_rmapbt_init_high_key_from_rec(
  169. union xfs_btree_key *key,
  170. const union xfs_btree_rec *rec)
  171. {
  172. uint64_t off;
  173. int adj;
  174. adj = be32_to_cpu(rec->rmap.rm_blockcount) - 1;
  175. key->rmap.rm_startblock = rec->rmap.rm_startblock;
  176. be32_add_cpu(&key->rmap.rm_startblock, adj);
  177. key->rmap.rm_owner = rec->rmap.rm_owner;
  178. key->rmap.rm_offset = ondisk_rec_offset_to_key(rec);
  179. if (XFS_RMAP_NON_INODE_OWNER(be64_to_cpu(rec->rmap.rm_owner)) ||
  180. XFS_RMAP_IS_BMBT_BLOCK(be64_to_cpu(rec->rmap.rm_offset)))
  181. return;
  182. off = be64_to_cpu(key->rmap.rm_offset);
  183. off = (XFS_RMAP_OFF(off) + adj) | (off & ~XFS_RMAP_OFF_MASK);
  184. key->rmap.rm_offset = cpu_to_be64(off);
  185. }
  186. STATIC void
  187. xfs_rmapbt_init_rec_from_cur(
  188. struct xfs_btree_cur *cur,
  189. union xfs_btree_rec *rec)
  190. {
  191. rec->rmap.rm_startblock = cpu_to_be32(cur->bc_rec.r.rm_startblock);
  192. rec->rmap.rm_blockcount = cpu_to_be32(cur->bc_rec.r.rm_blockcount);
  193. rec->rmap.rm_owner = cpu_to_be64(cur->bc_rec.r.rm_owner);
  194. rec->rmap.rm_offset = cpu_to_be64(
  195. xfs_rmap_irec_offset_pack(&cur->bc_rec.r));
  196. }
  197. STATIC void
  198. xfs_rmapbt_init_ptr_from_cur(
  199. struct xfs_btree_cur *cur,
  200. union xfs_btree_ptr *ptr)
  201. {
  202. struct xfs_agf *agf = cur->bc_ag.agbp->b_addr;
  203. ASSERT(cur->bc_ag.pag->pag_agno == be32_to_cpu(agf->agf_seqno));
  204. ptr->s = agf->agf_rmap_root;
  205. }
  206. /*
  207. * Mask the appropriate parts of the ondisk key field for a key comparison.
  208. * Fork and bmbt are significant parts of the rmap record key, but written
  209. * status is merely a record attribute.
  210. */
  211. static inline uint64_t offset_keymask(uint64_t offset)
  212. {
  213. return offset & ~XFS_RMAP_OFF_UNWRITTEN;
  214. }
  215. STATIC int64_t
  216. xfs_rmapbt_key_diff(
  217. struct xfs_btree_cur *cur,
  218. const union xfs_btree_key *key)
  219. {
  220. struct xfs_rmap_irec *rec = &cur->bc_rec.r;
  221. const struct xfs_rmap_key *kp = &key->rmap;
  222. __u64 x, y;
  223. int64_t d;
  224. d = (int64_t)be32_to_cpu(kp->rm_startblock) - rec->rm_startblock;
  225. if (d)
  226. return d;
  227. x = be64_to_cpu(kp->rm_owner);
  228. y = rec->rm_owner;
  229. if (x > y)
  230. return 1;
  231. else if (y > x)
  232. return -1;
  233. x = offset_keymask(be64_to_cpu(kp->rm_offset));
  234. y = offset_keymask(xfs_rmap_irec_offset_pack(rec));
  235. if (x > y)
  236. return 1;
  237. else if (y > x)
  238. return -1;
  239. return 0;
  240. }
  241. STATIC int64_t
  242. xfs_rmapbt_diff_two_keys(
  243. struct xfs_btree_cur *cur,
  244. const union xfs_btree_key *k1,
  245. const union xfs_btree_key *k2,
  246. const union xfs_btree_key *mask)
  247. {
  248. const struct xfs_rmap_key *kp1 = &k1->rmap;
  249. const struct xfs_rmap_key *kp2 = &k2->rmap;
  250. int64_t d;
  251. __u64 x, y;
  252. /* Doesn't make sense to mask off the physical space part */
  253. ASSERT(!mask || mask->rmap.rm_startblock);
  254. d = (int64_t)be32_to_cpu(kp1->rm_startblock) -
  255. be32_to_cpu(kp2->rm_startblock);
  256. if (d)
  257. return d;
  258. if (!mask || mask->rmap.rm_owner) {
  259. x = be64_to_cpu(kp1->rm_owner);
  260. y = be64_to_cpu(kp2->rm_owner);
  261. if (x > y)
  262. return 1;
  263. else if (y > x)
  264. return -1;
  265. }
  266. if (!mask || mask->rmap.rm_offset) {
  267. /* Doesn't make sense to allow offset but not owner */
  268. ASSERT(!mask || mask->rmap.rm_owner);
  269. x = offset_keymask(be64_to_cpu(kp1->rm_offset));
  270. y = offset_keymask(be64_to_cpu(kp2->rm_offset));
  271. if (x > y)
  272. return 1;
  273. else if (y > x)
  274. return -1;
  275. }
  276. return 0;
  277. }
  278. static xfs_failaddr_t
  279. xfs_rmapbt_verify(
  280. struct xfs_buf *bp)
  281. {
  282. struct xfs_mount *mp = bp->b_mount;
  283. struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
  284. struct xfs_perag *pag = bp->b_pag;
  285. xfs_failaddr_t fa;
  286. unsigned int level;
  287. /*
  288. * magic number and level verification
  289. *
  290. * During growfs operations, we can't verify the exact level or owner as
  291. * the perag is not fully initialised and hence not attached to the
  292. * buffer. In this case, check against the maximum tree depth.
  293. *
  294. * Similarly, during log recovery we will have a perag structure
  295. * attached, but the agf information will not yet have been initialised
  296. * from the on disk AGF. Again, we can only check against maximum limits
  297. * in this case.
  298. */
  299. if (!xfs_verify_magic(bp, block->bb_magic))
  300. return __this_address;
  301. if (!xfs_has_rmapbt(mp))
  302. return __this_address;
  303. fa = xfs_btree_agblock_v5hdr_verify(bp);
  304. if (fa)
  305. return fa;
  306. level = be16_to_cpu(block->bb_level);
  307. if (pag && xfs_perag_initialised_agf(pag)) {
  308. unsigned int maxlevel = pag->pagf_rmap_level;
  309. #ifdef CONFIG_XFS_ONLINE_REPAIR
  310. /*
  311. * Online repair could be rewriting the free space btrees, so
  312. * we'll validate against the larger of either tree while this
  313. * is going on.
  314. */
  315. maxlevel = max_t(unsigned int, maxlevel,
  316. pag->pagf_repair_rmap_level);
  317. #endif
  318. if (level >= maxlevel)
  319. return __this_address;
  320. } else if (level >= mp->m_rmap_maxlevels)
  321. return __this_address;
  322. return xfs_btree_agblock_verify(bp, mp->m_rmap_mxr[level != 0]);
  323. }
  324. static void
  325. xfs_rmapbt_read_verify(
  326. struct xfs_buf *bp)
  327. {
  328. xfs_failaddr_t fa;
  329. if (!xfs_btree_agblock_verify_crc(bp))
  330. xfs_verifier_error(bp, -EFSBADCRC, __this_address);
  331. else {
  332. fa = xfs_rmapbt_verify(bp);
  333. if (fa)
  334. xfs_verifier_error(bp, -EFSCORRUPTED, fa);
  335. }
  336. if (bp->b_error)
  337. trace_xfs_btree_corrupt(bp, _RET_IP_);
  338. }
  339. static void
  340. xfs_rmapbt_write_verify(
  341. struct xfs_buf *bp)
  342. {
  343. xfs_failaddr_t fa;
  344. fa = xfs_rmapbt_verify(bp);
  345. if (fa) {
  346. trace_xfs_btree_corrupt(bp, _RET_IP_);
  347. xfs_verifier_error(bp, -EFSCORRUPTED, fa);
  348. return;
  349. }
  350. xfs_btree_agblock_calc_crc(bp);
  351. }
  352. const struct xfs_buf_ops xfs_rmapbt_buf_ops = {
  353. .name = "xfs_rmapbt",
  354. .magic = { 0, cpu_to_be32(XFS_RMAP_CRC_MAGIC) },
  355. .verify_read = xfs_rmapbt_read_verify,
  356. .verify_write = xfs_rmapbt_write_verify,
  357. .verify_struct = xfs_rmapbt_verify,
  358. };
  359. STATIC int
  360. xfs_rmapbt_keys_inorder(
  361. struct xfs_btree_cur *cur,
  362. const union xfs_btree_key *k1,
  363. const union xfs_btree_key *k2)
  364. {
  365. uint32_t x;
  366. uint32_t y;
  367. uint64_t a;
  368. uint64_t b;
  369. x = be32_to_cpu(k1->rmap.rm_startblock);
  370. y = be32_to_cpu(k2->rmap.rm_startblock);
  371. if (x < y)
  372. return 1;
  373. else if (x > y)
  374. return 0;
  375. a = be64_to_cpu(k1->rmap.rm_owner);
  376. b = be64_to_cpu(k2->rmap.rm_owner);
  377. if (a < b)
  378. return 1;
  379. else if (a > b)
  380. return 0;
  381. a = offset_keymask(be64_to_cpu(k1->rmap.rm_offset));
  382. b = offset_keymask(be64_to_cpu(k2->rmap.rm_offset));
  383. if (a <= b)
  384. return 1;
  385. return 0;
  386. }
  387. STATIC int
  388. xfs_rmapbt_recs_inorder(
  389. struct xfs_btree_cur *cur,
  390. const union xfs_btree_rec *r1,
  391. const union xfs_btree_rec *r2)
  392. {
  393. uint32_t x;
  394. uint32_t y;
  395. uint64_t a;
  396. uint64_t b;
  397. x = be32_to_cpu(r1->rmap.rm_startblock);
  398. y = be32_to_cpu(r2->rmap.rm_startblock);
  399. if (x < y)
  400. return 1;
  401. else if (x > y)
  402. return 0;
  403. a = be64_to_cpu(r1->rmap.rm_owner);
  404. b = be64_to_cpu(r2->rmap.rm_owner);
  405. if (a < b)
  406. return 1;
  407. else if (a > b)
  408. return 0;
  409. a = offset_keymask(be64_to_cpu(r1->rmap.rm_offset));
  410. b = offset_keymask(be64_to_cpu(r2->rmap.rm_offset));
  411. if (a <= b)
  412. return 1;
  413. return 0;
  414. }
  415. STATIC enum xbtree_key_contig
  416. xfs_rmapbt_keys_contiguous(
  417. struct xfs_btree_cur *cur,
  418. const union xfs_btree_key *key1,
  419. const union xfs_btree_key *key2,
  420. const union xfs_btree_key *mask)
  421. {
  422. ASSERT(!mask || mask->rmap.rm_startblock);
  423. /*
  424. * We only support checking contiguity of the physical space component.
  425. * If any callers ever need more specificity than that, they'll have to
  426. * implement it here.
  427. */
  428. ASSERT(!mask || (!mask->rmap.rm_owner && !mask->rmap.rm_offset));
  429. return xbtree_key_contig(be32_to_cpu(key1->rmap.rm_startblock),
  430. be32_to_cpu(key2->rmap.rm_startblock));
  431. }
  432. const struct xfs_btree_ops xfs_rmapbt_ops = {
  433. .name = "rmap",
  434. .type = XFS_BTREE_TYPE_AG,
  435. .geom_flags = XFS_BTGEO_OVERLAPPING,
  436. .rec_len = sizeof(struct xfs_rmap_rec),
  437. /* Overlapping btree; 2 keys per pointer. */
  438. .key_len = 2 * sizeof(struct xfs_rmap_key),
  439. .ptr_len = XFS_BTREE_SHORT_PTR_LEN,
  440. .lru_refs = XFS_RMAP_BTREE_REF,
  441. .statoff = XFS_STATS_CALC_INDEX(xs_rmap_2),
  442. .sick_mask = XFS_SICK_AG_RMAPBT,
  443. .dup_cursor = xfs_rmapbt_dup_cursor,
  444. .set_root = xfs_rmapbt_set_root,
  445. .alloc_block = xfs_rmapbt_alloc_block,
  446. .free_block = xfs_rmapbt_free_block,
  447. .get_minrecs = xfs_rmapbt_get_minrecs,
  448. .get_maxrecs = xfs_rmapbt_get_maxrecs,
  449. .init_key_from_rec = xfs_rmapbt_init_key_from_rec,
  450. .init_high_key_from_rec = xfs_rmapbt_init_high_key_from_rec,
  451. .init_rec_from_cur = xfs_rmapbt_init_rec_from_cur,
  452. .init_ptr_from_cur = xfs_rmapbt_init_ptr_from_cur,
  453. .key_diff = xfs_rmapbt_key_diff,
  454. .buf_ops = &xfs_rmapbt_buf_ops,
  455. .diff_two_keys = xfs_rmapbt_diff_two_keys,
  456. .keys_inorder = xfs_rmapbt_keys_inorder,
  457. .recs_inorder = xfs_rmapbt_recs_inorder,
  458. .keys_contiguous = xfs_rmapbt_keys_contiguous,
  459. };
  460. /*
  461. * Create a new reverse mapping btree cursor.
  462. *
  463. * For staging cursors tp and agbp are NULL.
  464. */
  465. struct xfs_btree_cur *
  466. xfs_rmapbt_init_cursor(
  467. struct xfs_mount *mp,
  468. struct xfs_trans *tp,
  469. struct xfs_buf *agbp,
  470. struct xfs_perag *pag)
  471. {
  472. struct xfs_btree_cur *cur;
  473. cur = xfs_btree_alloc_cursor(mp, tp, &xfs_rmapbt_ops,
  474. mp->m_rmap_maxlevels, xfs_rmapbt_cur_cache);
  475. cur->bc_ag.pag = xfs_perag_hold(pag);
  476. cur->bc_ag.agbp = agbp;
  477. if (agbp) {
  478. struct xfs_agf *agf = agbp->b_addr;
  479. cur->bc_nlevels = be32_to_cpu(agf->agf_rmap_level);
  480. }
  481. return cur;
  482. }
  483. #ifdef CONFIG_XFS_BTREE_IN_MEM
  484. static inline unsigned int
  485. xfs_rmapbt_mem_block_maxrecs(
  486. unsigned int blocklen,
  487. bool leaf)
  488. {
  489. if (leaf)
  490. return blocklen / sizeof(struct xfs_rmap_rec);
  491. return blocklen /
  492. (2 * sizeof(struct xfs_rmap_key) + sizeof(__be64));
  493. }
  494. /*
  495. * Validate an in-memory rmap btree block. Callers are allowed to generate an
  496. * in-memory btree even if the ondisk feature is not enabled.
  497. */
  498. static xfs_failaddr_t
  499. xfs_rmapbt_mem_verify(
  500. struct xfs_buf *bp)
  501. {
  502. struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
  503. xfs_failaddr_t fa;
  504. unsigned int level;
  505. unsigned int maxrecs;
  506. if (!xfs_verify_magic(bp, block->bb_magic))
  507. return __this_address;
  508. fa = xfs_btree_fsblock_v5hdr_verify(bp, XFS_RMAP_OWN_UNKNOWN);
  509. if (fa)
  510. return fa;
  511. level = be16_to_cpu(block->bb_level);
  512. if (level >= xfs_rmapbt_maxlevels_ondisk())
  513. return __this_address;
  514. maxrecs = xfs_rmapbt_mem_block_maxrecs(
  515. XFBNO_BLOCKSIZE - XFS_BTREE_LBLOCK_CRC_LEN, level == 0);
  516. return xfs_btree_memblock_verify(bp, maxrecs);
  517. }
  518. static void
  519. xfs_rmapbt_mem_rw_verify(
  520. struct xfs_buf *bp)
  521. {
  522. xfs_failaddr_t fa = xfs_rmapbt_mem_verify(bp);
  523. if (fa)
  524. xfs_verifier_error(bp, -EFSCORRUPTED, fa);
  525. }
  526. /* skip crc checks on in-memory btrees to save time */
  527. static const struct xfs_buf_ops xfs_rmapbt_mem_buf_ops = {
  528. .name = "xfs_rmapbt_mem",
  529. .magic = { 0, cpu_to_be32(XFS_RMAP_CRC_MAGIC) },
  530. .verify_read = xfs_rmapbt_mem_rw_verify,
  531. .verify_write = xfs_rmapbt_mem_rw_verify,
  532. .verify_struct = xfs_rmapbt_mem_verify,
  533. };
  534. const struct xfs_btree_ops xfs_rmapbt_mem_ops = {
  535. .name = "mem_rmap",
  536. .type = XFS_BTREE_TYPE_MEM,
  537. .geom_flags = XFS_BTGEO_OVERLAPPING,
  538. .rec_len = sizeof(struct xfs_rmap_rec),
  539. /* Overlapping btree; 2 keys per pointer. */
  540. .key_len = 2 * sizeof(struct xfs_rmap_key),
  541. .ptr_len = XFS_BTREE_LONG_PTR_LEN,
  542. .lru_refs = XFS_RMAP_BTREE_REF,
  543. .statoff = XFS_STATS_CALC_INDEX(xs_rmap_mem_2),
  544. .dup_cursor = xfbtree_dup_cursor,
  545. .set_root = xfbtree_set_root,
  546. .alloc_block = xfbtree_alloc_block,
  547. .free_block = xfbtree_free_block,
  548. .get_minrecs = xfbtree_get_minrecs,
  549. .get_maxrecs = xfbtree_get_maxrecs,
  550. .init_key_from_rec = xfs_rmapbt_init_key_from_rec,
  551. .init_high_key_from_rec = xfs_rmapbt_init_high_key_from_rec,
  552. .init_rec_from_cur = xfs_rmapbt_init_rec_from_cur,
  553. .init_ptr_from_cur = xfbtree_init_ptr_from_cur,
  554. .key_diff = xfs_rmapbt_key_diff,
  555. .buf_ops = &xfs_rmapbt_mem_buf_ops,
  556. .diff_two_keys = xfs_rmapbt_diff_two_keys,
  557. .keys_inorder = xfs_rmapbt_keys_inorder,
  558. .recs_inorder = xfs_rmapbt_recs_inorder,
  559. .keys_contiguous = xfs_rmapbt_keys_contiguous,
  560. };
  561. /* Create a cursor for an in-memory btree. */
  562. struct xfs_btree_cur *
  563. xfs_rmapbt_mem_cursor(
  564. struct xfs_perag *pag,
  565. struct xfs_trans *tp,
  566. struct xfbtree *xfbt)
  567. {
  568. struct xfs_btree_cur *cur;
  569. struct xfs_mount *mp = pag->pag_mount;
  570. cur = xfs_btree_alloc_cursor(mp, tp, &xfs_rmapbt_mem_ops,
  571. xfs_rmapbt_maxlevels_ondisk(), xfs_rmapbt_cur_cache);
  572. cur->bc_mem.xfbtree = xfbt;
  573. cur->bc_nlevels = xfbt->nlevels;
  574. cur->bc_mem.pag = xfs_perag_hold(pag);
  575. return cur;
  576. }
  577. /* Create an in-memory rmap btree. */
  578. int
  579. xfs_rmapbt_mem_init(
  580. struct xfs_mount *mp,
  581. struct xfbtree *xfbt,
  582. struct xfs_buftarg *btp,
  583. xfs_agnumber_t agno)
  584. {
  585. xfbt->owner = agno;
  586. return xfbtree_init(mp, xfbt, btp, &xfs_rmapbt_mem_ops);
  587. }
  588. /* Compute the max possible height for reverse mapping btrees in memory. */
  589. static unsigned int
  590. xfs_rmapbt_mem_maxlevels(void)
  591. {
  592. unsigned int minrecs[2];
  593. unsigned int blocklen;
  594. blocklen = XFBNO_BLOCKSIZE - XFS_BTREE_LBLOCK_CRC_LEN;
  595. minrecs[0] = xfs_rmapbt_mem_block_maxrecs(blocklen, true) / 2;
  596. minrecs[1] = xfs_rmapbt_mem_block_maxrecs(blocklen, false) / 2;
  597. /*
  598. * How tall can an in-memory rmap btree become if we filled the entire
  599. * AG with rmap records?
  600. */
  601. return xfs_btree_compute_maxlevels(minrecs,
  602. XFS_MAX_AG_BYTES / sizeof(struct xfs_rmap_rec));
  603. }
  604. #else
  605. # define xfs_rmapbt_mem_maxlevels() (0)
  606. #endif /* CONFIG_XFS_BTREE_IN_MEM */
  607. /*
  608. * Install a new reverse mapping btree root. Caller is responsible for
  609. * invalidating and freeing the old btree blocks.
  610. */
  611. void
  612. xfs_rmapbt_commit_staged_btree(
  613. struct xfs_btree_cur *cur,
  614. struct xfs_trans *tp,
  615. struct xfs_buf *agbp)
  616. {
  617. struct xfs_agf *agf = agbp->b_addr;
  618. struct xbtree_afakeroot *afake = cur->bc_ag.afake;
  619. ASSERT(cur->bc_flags & XFS_BTREE_STAGING);
  620. agf->agf_rmap_root = cpu_to_be32(afake->af_root);
  621. agf->agf_rmap_level = cpu_to_be32(afake->af_levels);
  622. agf->agf_rmap_blocks = cpu_to_be32(afake->af_blocks);
  623. xfs_alloc_log_agf(tp, agbp, XFS_AGF_ROOTS | XFS_AGF_LEVELS |
  624. XFS_AGF_RMAP_BLOCKS);
  625. xfs_btree_commit_afakeroot(cur, tp, agbp);
  626. }
  627. /* Calculate number of records in a reverse mapping btree block. */
  628. static inline unsigned int
  629. xfs_rmapbt_block_maxrecs(
  630. unsigned int blocklen,
  631. bool leaf)
  632. {
  633. if (leaf)
  634. return blocklen / sizeof(struct xfs_rmap_rec);
  635. return blocklen /
  636. (2 * sizeof(struct xfs_rmap_key) + sizeof(xfs_rmap_ptr_t));
  637. }
  638. /*
  639. * Calculate number of records in an rmap btree block.
  640. */
  641. unsigned int
  642. xfs_rmapbt_maxrecs(
  643. struct xfs_mount *mp,
  644. unsigned int blocklen,
  645. bool leaf)
  646. {
  647. blocklen -= XFS_RMAP_BLOCK_LEN;
  648. return xfs_rmapbt_block_maxrecs(blocklen, leaf);
  649. }
  650. /* Compute the max possible height for reverse mapping btrees. */
  651. unsigned int
  652. xfs_rmapbt_maxlevels_ondisk(void)
  653. {
  654. unsigned int minrecs[2];
  655. unsigned int blocklen;
  656. blocklen = XFS_MIN_CRC_BLOCKSIZE - XFS_BTREE_SBLOCK_CRC_LEN;
  657. minrecs[0] = xfs_rmapbt_block_maxrecs(blocklen, true) / 2;
  658. minrecs[1] = xfs_rmapbt_block_maxrecs(blocklen, false) / 2;
  659. /*
  660. * Compute the asymptotic maxlevels for an rmapbt on any reflink fs.
  661. *
  662. * On a reflink filesystem, each AG block can have up to 2^32 (per the
  663. * refcount record format) owners, which means that theoretically we
  664. * could face up to 2^64 rmap records. However, we're likely to run
  665. * out of blocks in the AG long before that happens, which means that
  666. * we must compute the max height based on what the btree will look
  667. * like if it consumes almost all the blocks in the AG due to maximal
  668. * sharing factor.
  669. */
  670. return max(xfs_btree_space_to_height(minrecs, XFS_MAX_CRC_AG_BLOCKS),
  671. xfs_rmapbt_mem_maxlevels());
  672. }
  673. /* Compute the maximum height of an rmap btree. */
  674. void
  675. xfs_rmapbt_compute_maxlevels(
  676. struct xfs_mount *mp)
  677. {
  678. if (!xfs_has_rmapbt(mp)) {
  679. mp->m_rmap_maxlevels = 0;
  680. return;
  681. }
  682. if (xfs_has_reflink(mp)) {
  683. /*
  684. * Compute the asymptotic maxlevels for an rmap btree on a
  685. * filesystem that supports reflink.
  686. *
  687. * On a reflink filesystem, each AG block can have up to 2^32
  688. * (per the refcount record format) owners, which means that
  689. * theoretically we could face up to 2^64 rmap records.
  690. * However, we're likely to run out of blocks in the AG long
  691. * before that happens, which means that we must compute the
  692. * max height based on what the btree will look like if it
  693. * consumes almost all the blocks in the AG due to maximal
  694. * sharing factor.
  695. */
  696. mp->m_rmap_maxlevels = xfs_btree_space_to_height(mp->m_rmap_mnr,
  697. mp->m_sb.sb_agblocks);
  698. } else {
  699. /*
  700. * If there's no block sharing, compute the maximum rmapbt
  701. * height assuming one rmap record per AG block.
  702. */
  703. mp->m_rmap_maxlevels = xfs_btree_compute_maxlevels(
  704. mp->m_rmap_mnr, mp->m_sb.sb_agblocks);
  705. }
  706. ASSERT(mp->m_rmap_maxlevels <= xfs_rmapbt_maxlevels_ondisk());
  707. }
  708. /* Calculate the refcount btree size for some records. */
  709. xfs_extlen_t
  710. xfs_rmapbt_calc_size(
  711. struct xfs_mount *mp,
  712. unsigned long long len)
  713. {
  714. return xfs_btree_calc_size(mp->m_rmap_mnr, len);
  715. }
  716. /*
  717. * Calculate the maximum refcount btree size.
  718. */
  719. xfs_extlen_t
  720. xfs_rmapbt_max_size(
  721. struct xfs_mount *mp,
  722. xfs_agblock_t agblocks)
  723. {
  724. /* Bail out if we're uninitialized, which can happen in mkfs. */
  725. if (mp->m_rmap_mxr[0] == 0)
  726. return 0;
  727. return xfs_rmapbt_calc_size(mp, agblocks);
  728. }
  729. /*
  730. * Figure out how many blocks to reserve and how many are used by this btree.
  731. */
  732. int
  733. xfs_rmapbt_calc_reserves(
  734. struct xfs_mount *mp,
  735. struct xfs_trans *tp,
  736. struct xfs_perag *pag,
  737. xfs_extlen_t *ask,
  738. xfs_extlen_t *used)
  739. {
  740. struct xfs_buf *agbp;
  741. struct xfs_agf *agf;
  742. xfs_agblock_t agblocks;
  743. xfs_extlen_t tree_len;
  744. int error;
  745. if (!xfs_has_rmapbt(mp))
  746. return 0;
  747. error = xfs_alloc_read_agf(pag, tp, 0, &agbp);
  748. if (error)
  749. return error;
  750. agf = agbp->b_addr;
  751. agblocks = be32_to_cpu(agf->agf_length);
  752. tree_len = be32_to_cpu(agf->agf_rmap_blocks);
  753. xfs_trans_brelse(tp, agbp);
  754. /*
  755. * The log is permanently allocated, so the space it occupies will
  756. * never be available for the kinds of things that would require btree
  757. * expansion. We therefore can pretend the space isn't there.
  758. */
  759. if (xfs_ag_contains_log(mp, pag->pag_agno))
  760. agblocks -= mp->m_sb.sb_logblocks;
  761. /* Reserve 1% of the AG or enough for 1 block per record. */
  762. *ask += max(agblocks / 100, xfs_rmapbt_max_size(mp, agblocks));
  763. *used += tree_len;
  764. return error;
  765. }
  766. int __init
  767. xfs_rmapbt_init_cur_cache(void)
  768. {
  769. xfs_rmapbt_cur_cache = kmem_cache_create("xfs_rmapbt_cur",
  770. xfs_btree_cur_sizeof(xfs_rmapbt_maxlevels_ondisk()),
  771. 0, 0, NULL);
  772. if (!xfs_rmapbt_cur_cache)
  773. return -ENOMEM;
  774. return 0;
  775. }
  776. void
  777. xfs_rmapbt_destroy_cur_cache(void)
  778. {
  779. kmem_cache_destroy(xfs_rmapbt_cur_cache);
  780. xfs_rmapbt_cur_cache = NULL;
  781. }