alloc.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2017 Oracle. All Rights Reserved.
  4. * Author: Darrick J. Wong <darrick.wong@oracle.com>
  5. */
  6. #include "xfs.h"
  7. #include "xfs_fs.h"
  8. #include "xfs_shared.h"
  9. #include "xfs_format.h"
  10. #include "xfs_trans_resv.h"
  11. #include "xfs_mount.h"
  12. #include "xfs_defer.h"
  13. #include "xfs_btree.h"
  14. #include "xfs_bit.h"
  15. #include "xfs_log_format.h"
  16. #include "xfs_trans.h"
  17. #include "xfs_sb.h"
  18. #include "xfs_alloc.h"
  19. #include "xfs_rmap.h"
  20. #include "scrub/xfs_scrub.h"
  21. #include "scrub/scrub.h"
  22. #include "scrub/common.h"
  23. #include "scrub/btree.h"
  24. #include "scrub/trace.h"
  25. /*
  26. * Set us up to scrub free space btrees.
  27. */
  28. int
  29. xchk_setup_ag_allocbt(
  30. struct xfs_scrub *sc,
  31. struct xfs_inode *ip)
  32. {
  33. return xchk_setup_ag_btree(sc, ip, false);
  34. }
  35. /* Free space btree scrubber. */
  36. /*
  37. * Ensure there's a corresponding cntbt/bnobt record matching this
  38. * bnobt/cntbt record, respectively.
  39. */
  40. STATIC void
  41. xchk_allocbt_xref_other(
  42. struct xfs_scrub *sc,
  43. xfs_agblock_t agbno,
  44. xfs_extlen_t len)
  45. {
  46. struct xfs_btree_cur **pcur;
  47. xfs_agblock_t fbno;
  48. xfs_extlen_t flen;
  49. int has_otherrec;
  50. int error;
  51. if (sc->sm->sm_type == XFS_SCRUB_TYPE_BNOBT)
  52. pcur = &sc->sa.cnt_cur;
  53. else
  54. pcur = &sc->sa.bno_cur;
  55. if (!*pcur || xchk_skip_xref(sc->sm))
  56. return;
  57. error = xfs_alloc_lookup_le(*pcur, agbno, len, &has_otherrec);
  58. if (!xchk_should_check_xref(sc, &error, pcur))
  59. return;
  60. if (!has_otherrec) {
  61. xchk_btree_xref_set_corrupt(sc, *pcur, 0);
  62. return;
  63. }
  64. error = xfs_alloc_get_rec(*pcur, &fbno, &flen, &has_otherrec);
  65. if (!xchk_should_check_xref(sc, &error, pcur))
  66. return;
  67. if (!has_otherrec) {
  68. xchk_btree_xref_set_corrupt(sc, *pcur, 0);
  69. return;
  70. }
  71. if (fbno != agbno || flen != len)
  72. xchk_btree_xref_set_corrupt(sc, *pcur, 0);
  73. }
  74. /* Cross-reference with the other btrees. */
  75. STATIC void
  76. xchk_allocbt_xref(
  77. struct xfs_scrub *sc,
  78. xfs_agblock_t agbno,
  79. xfs_extlen_t len)
  80. {
  81. if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
  82. return;
  83. xchk_allocbt_xref_other(sc, agbno, len);
  84. xchk_xref_is_not_inode_chunk(sc, agbno, len);
  85. xchk_xref_has_no_owner(sc, agbno, len);
  86. xchk_xref_is_not_shared(sc, agbno, len);
  87. }
  88. /* Scrub a bnobt/cntbt record. */
  89. STATIC int
  90. xchk_allocbt_rec(
  91. struct xchk_btree *bs,
  92. union xfs_btree_rec *rec)
  93. {
  94. struct xfs_mount *mp = bs->cur->bc_mp;
  95. xfs_agnumber_t agno = bs->cur->bc_private.a.agno;
  96. xfs_agblock_t bno;
  97. xfs_extlen_t len;
  98. int error = 0;
  99. bno = be32_to_cpu(rec->alloc.ar_startblock);
  100. len = be32_to_cpu(rec->alloc.ar_blockcount);
  101. if (bno + len <= bno ||
  102. !xfs_verify_agbno(mp, agno, bno) ||
  103. !xfs_verify_agbno(mp, agno, bno + len - 1))
  104. xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
  105. xchk_allocbt_xref(bs->sc, bno, len);
  106. return error;
  107. }
  108. /* Scrub the freespace btrees for some AG. */
  109. STATIC int
  110. xchk_allocbt(
  111. struct xfs_scrub *sc,
  112. xfs_btnum_t which)
  113. {
  114. struct xfs_owner_info oinfo;
  115. struct xfs_btree_cur *cur;
  116. xfs_rmap_ag_owner(&oinfo, XFS_RMAP_OWN_AG);
  117. cur = which == XFS_BTNUM_BNO ? sc->sa.bno_cur : sc->sa.cnt_cur;
  118. return xchk_btree(sc, cur, xchk_allocbt_rec, &oinfo, NULL);
  119. }
  120. int
  121. xchk_bnobt(
  122. struct xfs_scrub *sc)
  123. {
  124. return xchk_allocbt(sc, XFS_BTNUM_BNO);
  125. }
  126. int
  127. xchk_cntbt(
  128. struct xfs_scrub *sc)
  129. {
  130. return xchk_allocbt(sc, XFS_BTNUM_CNT);
  131. }
  132. /* xref check that the extent is not free */
  133. void
  134. xchk_xref_is_used_space(
  135. struct xfs_scrub *sc,
  136. xfs_agblock_t agbno,
  137. xfs_extlen_t len)
  138. {
  139. bool is_freesp;
  140. int error;
  141. if (!sc->sa.bno_cur || xchk_skip_xref(sc->sm))
  142. return;
  143. error = xfs_alloc_has_record(sc->sa.bno_cur, agbno, len, &is_freesp);
  144. if (!xchk_should_check_xref(sc, &error, &sc->sa.bno_cur))
  145. return;
  146. if (is_freesp)
  147. xchk_btree_xref_set_corrupt(sc, sc->sa.bno_cur, 0);
  148. }