xfs_rmap_btree.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. /*
  2. * Copyright (c) 2014 Red Hat, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write the Free Software Foundation,
  16. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "xfs.h"
  19. #include "xfs_fs.h"
  20. #include "xfs_shared.h"
  21. #include "xfs_format.h"
  22. #include "xfs_log_format.h"
  23. #include "xfs_trans_resv.h"
  24. #include "xfs_bit.h"
  25. #include "xfs_sb.h"
  26. #include "xfs_mount.h"
  27. #include "xfs_defer.h"
  28. #include "xfs_inode.h"
  29. #include "xfs_trans.h"
  30. #include "xfs_alloc.h"
  31. #include "xfs_btree.h"
  32. #include "xfs_rmap.h"
  33. #include "xfs_rmap_btree.h"
  34. #include "xfs_trace.h"
  35. #include "xfs_cksum.h"
  36. #include "xfs_error.h"
  37. #include "xfs_extent_busy.h"
  38. /*
  39. * Reverse map btree.
  40. *
  41. * This is a per-ag tree used to track the owner(s) of a given extent. With
  42. * reflink it is possible for there to be multiple owners, which is a departure
  43. * from classic XFS. Owner records for data extents are inserted when the
  44. * extent is mapped and removed when an extent is unmapped. Owner records for
  45. * all other block types (i.e. metadata) are inserted when an extent is
  46. * allocated and removed when an extent is freed. There can only be one owner
  47. * of a metadata extent, usually an inode or some other metadata structure like
  48. * an AG btree.
  49. *
  50. * The rmap btree is part of the free space management, so blocks for the tree
  51. * are sourced from the agfl. Hence we need transaction reservation support for
  52. * this tree so that the freelist is always large enough. This also impacts on
  53. * the minimum space we need to leave free in the AG.
  54. *
  55. * The tree is ordered by [ag block, owner, offset]. This is a large key size,
  56. * but it is the only way to enforce unique keys when a block can be owned by
  57. * multiple files at any offset. There's no need to order/search by extent
  58. * size for online updating/management of the tree. It is intended that most
  59. * reverse lookups will be to find the owner(s) of a particular block, or to
  60. * try to recover tree and file data from corrupt primary metadata.
  61. */
  62. static struct xfs_btree_cur *
  63. xfs_rmapbt_dup_cursor(
  64. struct xfs_btree_cur *cur)
  65. {
  66. return xfs_rmapbt_init_cursor(cur->bc_mp, cur->bc_tp,
  67. cur->bc_private.a.agbp, cur->bc_private.a.agno);
  68. }
  69. STATIC void
  70. xfs_rmapbt_set_root(
  71. struct xfs_btree_cur *cur,
  72. union xfs_btree_ptr *ptr,
  73. int inc)
  74. {
  75. struct xfs_buf *agbp = cur->bc_private.a.agbp;
  76. struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
  77. xfs_agnumber_t seqno = be32_to_cpu(agf->agf_seqno);
  78. int btnum = cur->bc_btnum;
  79. struct xfs_perag *pag = xfs_perag_get(cur->bc_mp, seqno);
  80. ASSERT(ptr->s != 0);
  81. agf->agf_roots[btnum] = ptr->s;
  82. be32_add_cpu(&agf->agf_levels[btnum], inc);
  83. pag->pagf_levels[btnum] += inc;
  84. xfs_perag_put(pag);
  85. xfs_alloc_log_agf(cur->bc_tp, agbp, XFS_AGF_ROOTS | XFS_AGF_LEVELS);
  86. }
  87. STATIC int
  88. xfs_rmapbt_alloc_block(
  89. struct xfs_btree_cur *cur,
  90. union xfs_btree_ptr *start,
  91. union xfs_btree_ptr *new,
  92. int *stat)
  93. {
  94. struct xfs_buf *agbp = cur->bc_private.a.agbp;
  95. struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
  96. int error;
  97. xfs_agblock_t bno;
  98. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  99. /* Allocate the new block from the freelist. If we can't, give up. */
  100. error = xfs_alloc_get_freelist(cur->bc_tp, cur->bc_private.a.agbp,
  101. &bno, 1);
  102. if (error) {
  103. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  104. return error;
  105. }
  106. trace_xfs_rmapbt_alloc_block(cur->bc_mp, cur->bc_private.a.agno,
  107. bno, 1);
  108. if (bno == NULLAGBLOCK) {
  109. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  110. *stat = 0;
  111. return 0;
  112. }
  113. xfs_extent_busy_reuse(cur->bc_mp, cur->bc_private.a.agno, bno, 1,
  114. false);
  115. xfs_trans_agbtree_delta(cur->bc_tp, 1);
  116. new->s = cpu_to_be32(bno);
  117. be32_add_cpu(&agf->agf_rmap_blocks, 1);
  118. xfs_alloc_log_agf(cur->bc_tp, agbp, XFS_AGF_RMAP_BLOCKS);
  119. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  120. *stat = 1;
  121. return 0;
  122. }
  123. STATIC int
  124. xfs_rmapbt_free_block(
  125. struct xfs_btree_cur *cur,
  126. struct xfs_buf *bp)
  127. {
  128. struct xfs_buf *agbp = cur->bc_private.a.agbp;
  129. struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
  130. xfs_agblock_t bno;
  131. int error;
  132. bno = xfs_daddr_to_agbno(cur->bc_mp, XFS_BUF_ADDR(bp));
  133. trace_xfs_rmapbt_free_block(cur->bc_mp, cur->bc_private.a.agno,
  134. bno, 1);
  135. be32_add_cpu(&agf->agf_rmap_blocks, -1);
  136. xfs_alloc_log_agf(cur->bc_tp, agbp, XFS_AGF_RMAP_BLOCKS);
  137. error = xfs_alloc_put_freelist(cur->bc_tp, agbp, NULL, bno, 1);
  138. if (error)
  139. return error;
  140. xfs_extent_busy_insert(cur->bc_tp, be32_to_cpu(agf->agf_seqno), bno, 1,
  141. XFS_EXTENT_BUSY_SKIP_DISCARD);
  142. xfs_trans_agbtree_delta(cur->bc_tp, -1);
  143. return 0;
  144. }
  145. STATIC int
  146. xfs_rmapbt_get_minrecs(
  147. struct xfs_btree_cur *cur,
  148. int level)
  149. {
  150. return cur->bc_mp->m_rmap_mnr[level != 0];
  151. }
  152. STATIC int
  153. xfs_rmapbt_get_maxrecs(
  154. struct xfs_btree_cur *cur,
  155. int level)
  156. {
  157. return cur->bc_mp->m_rmap_mxr[level != 0];
  158. }
  159. STATIC void
  160. xfs_rmapbt_init_key_from_rec(
  161. union xfs_btree_key *key,
  162. union xfs_btree_rec *rec)
  163. {
  164. key->rmap.rm_startblock = rec->rmap.rm_startblock;
  165. key->rmap.rm_owner = rec->rmap.rm_owner;
  166. key->rmap.rm_offset = rec->rmap.rm_offset;
  167. }
  168. /*
  169. * The high key for a reverse mapping record can be computed by shifting
  170. * the startblock and offset to the highest value that would still map
  171. * to that record. In practice this means that we add blockcount-1 to
  172. * the startblock for all records, and if the record is for a data/attr
  173. * fork mapping, we add blockcount-1 to the offset too.
  174. */
  175. STATIC void
  176. xfs_rmapbt_init_high_key_from_rec(
  177. union xfs_btree_key *key,
  178. union xfs_btree_rec *rec)
  179. {
  180. __uint64_t off;
  181. int adj;
  182. adj = be32_to_cpu(rec->rmap.rm_blockcount) - 1;
  183. key->rmap.rm_startblock = rec->rmap.rm_startblock;
  184. be32_add_cpu(&key->rmap.rm_startblock, adj);
  185. key->rmap.rm_owner = rec->rmap.rm_owner;
  186. key->rmap.rm_offset = rec->rmap.rm_offset;
  187. if (XFS_RMAP_NON_INODE_OWNER(be64_to_cpu(rec->rmap.rm_owner)) ||
  188. XFS_RMAP_IS_BMBT_BLOCK(be64_to_cpu(rec->rmap.rm_offset)))
  189. return;
  190. off = be64_to_cpu(key->rmap.rm_offset);
  191. off = (XFS_RMAP_OFF(off) + adj) | (off & ~XFS_RMAP_OFF_MASK);
  192. key->rmap.rm_offset = cpu_to_be64(off);
  193. }
  194. STATIC void
  195. xfs_rmapbt_init_rec_from_cur(
  196. struct xfs_btree_cur *cur,
  197. union xfs_btree_rec *rec)
  198. {
  199. rec->rmap.rm_startblock = cpu_to_be32(cur->bc_rec.r.rm_startblock);
  200. rec->rmap.rm_blockcount = cpu_to_be32(cur->bc_rec.r.rm_blockcount);
  201. rec->rmap.rm_owner = cpu_to_be64(cur->bc_rec.r.rm_owner);
  202. rec->rmap.rm_offset = cpu_to_be64(
  203. xfs_rmap_irec_offset_pack(&cur->bc_rec.r));
  204. }
  205. STATIC void
  206. xfs_rmapbt_init_ptr_from_cur(
  207. struct xfs_btree_cur *cur,
  208. union xfs_btree_ptr *ptr)
  209. {
  210. struct xfs_agf *agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
  211. ASSERT(cur->bc_private.a.agno == be32_to_cpu(agf->agf_seqno));
  212. ASSERT(agf->agf_roots[cur->bc_btnum] != 0);
  213. ptr->s = agf->agf_roots[cur->bc_btnum];
  214. }
  215. STATIC __int64_t
  216. xfs_rmapbt_key_diff(
  217. struct xfs_btree_cur *cur,
  218. union xfs_btree_key *key)
  219. {
  220. struct xfs_rmap_irec *rec = &cur->bc_rec.r;
  221. 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 = XFS_RMAP_OFF(be64_to_cpu(kp->rm_offset));
  234. y = rec->rm_offset;
  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. union xfs_btree_key *k1,
  245. union xfs_btree_key *k2)
  246. {
  247. struct xfs_rmap_key *kp1 = &k1->rmap;
  248. struct xfs_rmap_key *kp2 = &k2->rmap;
  249. __int64_t d;
  250. __u64 x, y;
  251. d = (__int64_t)be32_to_cpu(kp1->rm_startblock) -
  252. be32_to_cpu(kp2->rm_startblock);
  253. if (d)
  254. return d;
  255. x = be64_to_cpu(kp1->rm_owner);
  256. y = be64_to_cpu(kp2->rm_owner);
  257. if (x > y)
  258. return 1;
  259. else if (y > x)
  260. return -1;
  261. x = XFS_RMAP_OFF(be64_to_cpu(kp1->rm_offset));
  262. y = XFS_RMAP_OFF(be64_to_cpu(kp2->rm_offset));
  263. if (x > y)
  264. return 1;
  265. else if (y > x)
  266. return -1;
  267. return 0;
  268. }
  269. static bool
  270. xfs_rmapbt_verify(
  271. struct xfs_buf *bp)
  272. {
  273. struct xfs_mount *mp = bp->b_target->bt_mount;
  274. struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
  275. struct xfs_perag *pag = bp->b_pag;
  276. unsigned int level;
  277. /*
  278. * magic number and level verification
  279. *
  280. * During growfs operations, we can't verify the exact level or owner as
  281. * the perag is not fully initialised and hence not attached to the
  282. * buffer. In this case, check against the maximum tree depth.
  283. *
  284. * Similarly, during log recovery we will have a perag structure
  285. * attached, but the agf information will not yet have been initialised
  286. * from the on disk AGF. Again, we can only check against maximum limits
  287. * in this case.
  288. */
  289. if (block->bb_magic != cpu_to_be32(XFS_RMAP_CRC_MAGIC))
  290. return false;
  291. if (!xfs_sb_version_hasrmapbt(&mp->m_sb))
  292. return false;
  293. if (!xfs_btree_sblock_v5hdr_verify(bp))
  294. return false;
  295. level = be16_to_cpu(block->bb_level);
  296. if (pag && pag->pagf_init) {
  297. if (level >= pag->pagf_levels[XFS_BTNUM_RMAPi])
  298. return false;
  299. } else if (level >= mp->m_rmap_maxlevels)
  300. return false;
  301. return xfs_btree_sblock_verify(bp, mp->m_rmap_mxr[level != 0]);
  302. }
  303. static void
  304. xfs_rmapbt_read_verify(
  305. struct xfs_buf *bp)
  306. {
  307. if (!xfs_btree_sblock_verify_crc(bp))
  308. xfs_buf_ioerror(bp, -EFSBADCRC);
  309. else if (!xfs_rmapbt_verify(bp))
  310. xfs_buf_ioerror(bp, -EFSCORRUPTED);
  311. if (bp->b_error) {
  312. trace_xfs_btree_corrupt(bp, _RET_IP_);
  313. xfs_verifier_error(bp);
  314. }
  315. }
  316. static void
  317. xfs_rmapbt_write_verify(
  318. struct xfs_buf *bp)
  319. {
  320. if (!xfs_rmapbt_verify(bp)) {
  321. trace_xfs_btree_corrupt(bp, _RET_IP_);
  322. xfs_buf_ioerror(bp, -EFSCORRUPTED);
  323. xfs_verifier_error(bp);
  324. return;
  325. }
  326. xfs_btree_sblock_calc_crc(bp);
  327. }
  328. const struct xfs_buf_ops xfs_rmapbt_buf_ops = {
  329. .name = "xfs_rmapbt",
  330. .verify_read = xfs_rmapbt_read_verify,
  331. .verify_write = xfs_rmapbt_write_verify,
  332. };
  333. #if defined(DEBUG) || defined(XFS_WARN)
  334. STATIC int
  335. xfs_rmapbt_keys_inorder(
  336. struct xfs_btree_cur *cur,
  337. union xfs_btree_key *k1,
  338. union xfs_btree_key *k2)
  339. {
  340. __uint32_t x;
  341. __uint32_t y;
  342. __uint64_t a;
  343. __uint64_t b;
  344. x = be32_to_cpu(k1->rmap.rm_startblock);
  345. y = be32_to_cpu(k2->rmap.rm_startblock);
  346. if (x < y)
  347. return 1;
  348. else if (x > y)
  349. return 0;
  350. a = be64_to_cpu(k1->rmap.rm_owner);
  351. b = be64_to_cpu(k2->rmap.rm_owner);
  352. if (a < b)
  353. return 1;
  354. else if (a > b)
  355. return 0;
  356. a = XFS_RMAP_OFF(be64_to_cpu(k1->rmap.rm_offset));
  357. b = XFS_RMAP_OFF(be64_to_cpu(k2->rmap.rm_offset));
  358. if (a <= b)
  359. return 1;
  360. return 0;
  361. }
  362. STATIC int
  363. xfs_rmapbt_recs_inorder(
  364. struct xfs_btree_cur *cur,
  365. union xfs_btree_rec *r1,
  366. union xfs_btree_rec *r2)
  367. {
  368. __uint32_t x;
  369. __uint32_t y;
  370. __uint64_t a;
  371. __uint64_t b;
  372. x = be32_to_cpu(r1->rmap.rm_startblock);
  373. y = be32_to_cpu(r2->rmap.rm_startblock);
  374. if (x < y)
  375. return 1;
  376. else if (x > y)
  377. return 0;
  378. a = be64_to_cpu(r1->rmap.rm_owner);
  379. b = be64_to_cpu(r2->rmap.rm_owner);
  380. if (a < b)
  381. return 1;
  382. else if (a > b)
  383. return 0;
  384. a = XFS_RMAP_OFF(be64_to_cpu(r1->rmap.rm_offset));
  385. b = XFS_RMAP_OFF(be64_to_cpu(r2->rmap.rm_offset));
  386. if (a <= b)
  387. return 1;
  388. return 0;
  389. }
  390. #endif /* DEBUG */
  391. static const struct xfs_btree_ops xfs_rmapbt_ops = {
  392. .rec_len = sizeof(struct xfs_rmap_rec),
  393. .key_len = 2 * sizeof(struct xfs_rmap_key),
  394. .dup_cursor = xfs_rmapbt_dup_cursor,
  395. .set_root = xfs_rmapbt_set_root,
  396. .alloc_block = xfs_rmapbt_alloc_block,
  397. .free_block = xfs_rmapbt_free_block,
  398. .get_minrecs = xfs_rmapbt_get_minrecs,
  399. .get_maxrecs = xfs_rmapbt_get_maxrecs,
  400. .init_key_from_rec = xfs_rmapbt_init_key_from_rec,
  401. .init_high_key_from_rec = xfs_rmapbt_init_high_key_from_rec,
  402. .init_rec_from_cur = xfs_rmapbt_init_rec_from_cur,
  403. .init_ptr_from_cur = xfs_rmapbt_init_ptr_from_cur,
  404. .key_diff = xfs_rmapbt_key_diff,
  405. .buf_ops = &xfs_rmapbt_buf_ops,
  406. .diff_two_keys = xfs_rmapbt_diff_two_keys,
  407. #if defined(DEBUG) || defined(XFS_WARN)
  408. .keys_inorder = xfs_rmapbt_keys_inorder,
  409. .recs_inorder = xfs_rmapbt_recs_inorder,
  410. #endif
  411. };
  412. /*
  413. * Allocate a new allocation btree cursor.
  414. */
  415. struct xfs_btree_cur *
  416. xfs_rmapbt_init_cursor(
  417. struct xfs_mount *mp,
  418. struct xfs_trans *tp,
  419. struct xfs_buf *agbp,
  420. xfs_agnumber_t agno)
  421. {
  422. struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
  423. struct xfs_btree_cur *cur;
  424. cur = kmem_zone_zalloc(xfs_btree_cur_zone, KM_NOFS);
  425. cur->bc_tp = tp;
  426. cur->bc_mp = mp;
  427. /* Overlapping btree; 2 keys per pointer. */
  428. cur->bc_btnum = XFS_BTNUM_RMAP;
  429. cur->bc_flags = XFS_BTREE_CRC_BLOCKS | XFS_BTREE_OVERLAPPING;
  430. cur->bc_blocklog = mp->m_sb.sb_blocklog;
  431. cur->bc_ops = &xfs_rmapbt_ops;
  432. cur->bc_nlevels = be32_to_cpu(agf->agf_levels[XFS_BTNUM_RMAP]);
  433. cur->bc_private.a.agbp = agbp;
  434. cur->bc_private.a.agno = agno;
  435. return cur;
  436. }
  437. /*
  438. * Calculate number of records in an rmap btree block.
  439. */
  440. int
  441. xfs_rmapbt_maxrecs(
  442. struct xfs_mount *mp,
  443. int blocklen,
  444. int leaf)
  445. {
  446. blocklen -= XFS_RMAP_BLOCK_LEN;
  447. if (leaf)
  448. return blocklen / sizeof(struct xfs_rmap_rec);
  449. return blocklen /
  450. (2 * sizeof(struct xfs_rmap_key) + sizeof(xfs_rmap_ptr_t));
  451. }
  452. /* Compute the maximum height of an rmap btree. */
  453. void
  454. xfs_rmapbt_compute_maxlevels(
  455. struct xfs_mount *mp)
  456. {
  457. mp->m_rmap_maxlevels = xfs_btree_compute_maxlevels(mp,
  458. mp->m_rmap_mnr, mp->m_sb.sb_agblocks);
  459. }