alloc.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2017 Oracle. All Rights Reserved.
  4. * Author: Darrick J. Wong <[email protected]>
  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_btree.h"
  13. #include "xfs_alloc.h"
  14. #include "xfs_rmap.h"
  15. #include "scrub/scrub.h"
  16. #include "scrub/common.h"
  17. #include "scrub/btree.h"
  18. #include "xfs_ag.h"
  19. /*
  20. * Set us up to scrub free space btrees.
  21. */
  22. int
  23. xchk_setup_ag_allocbt(
  24. struct xfs_scrub *sc)
  25. {
  26. return xchk_setup_ag_btree(sc, false);
  27. }
  28. /* Free space btree scrubber. */
  29. /*
  30. * Ensure there's a corresponding cntbt/bnobt record matching this
  31. * bnobt/cntbt record, respectively.
  32. */
  33. STATIC void
  34. xchk_allocbt_xref_other(
  35. struct xfs_scrub *sc,
  36. xfs_agblock_t agbno,
  37. xfs_extlen_t len)
  38. {
  39. struct xfs_btree_cur **pcur;
  40. xfs_agblock_t fbno;
  41. xfs_extlen_t flen;
  42. int has_otherrec;
  43. int error;
  44. if (sc->sm->sm_type == XFS_SCRUB_TYPE_BNOBT)
  45. pcur = &sc->sa.cnt_cur;
  46. else
  47. pcur = &sc->sa.bno_cur;
  48. if (!*pcur || xchk_skip_xref(sc->sm))
  49. return;
  50. error = xfs_alloc_lookup_le(*pcur, agbno, len, &has_otherrec);
  51. if (!xchk_should_check_xref(sc, &error, pcur))
  52. return;
  53. if (!has_otherrec) {
  54. xchk_btree_xref_set_corrupt(sc, *pcur, 0);
  55. return;
  56. }
  57. error = xfs_alloc_get_rec(*pcur, &fbno, &flen, &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. if (fbno != agbno || flen != len)
  65. xchk_btree_xref_set_corrupt(sc, *pcur, 0);
  66. }
  67. /* Cross-reference with the other btrees. */
  68. STATIC void
  69. xchk_allocbt_xref(
  70. struct xfs_scrub *sc,
  71. xfs_agblock_t agbno,
  72. xfs_extlen_t len)
  73. {
  74. if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
  75. return;
  76. xchk_allocbt_xref_other(sc, agbno, len);
  77. xchk_xref_is_not_inode_chunk(sc, agbno, len);
  78. xchk_xref_has_no_owner(sc, agbno, len);
  79. xchk_xref_is_not_shared(sc, agbno, len);
  80. }
  81. /* Scrub a bnobt/cntbt record. */
  82. STATIC int
  83. xchk_allocbt_rec(
  84. struct xchk_btree *bs,
  85. const union xfs_btree_rec *rec)
  86. {
  87. struct xfs_perag *pag = bs->cur->bc_ag.pag;
  88. xfs_agblock_t bno;
  89. xfs_extlen_t len;
  90. bno = be32_to_cpu(rec->alloc.ar_startblock);
  91. len = be32_to_cpu(rec->alloc.ar_blockcount);
  92. if (!xfs_verify_agbext(pag, bno, len))
  93. xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
  94. xchk_allocbt_xref(bs->sc, bno, len);
  95. return 0;
  96. }
  97. /* Scrub the freespace btrees for some AG. */
  98. STATIC int
  99. xchk_allocbt(
  100. struct xfs_scrub *sc,
  101. xfs_btnum_t which)
  102. {
  103. struct xfs_btree_cur *cur;
  104. cur = which == XFS_BTNUM_BNO ? sc->sa.bno_cur : sc->sa.cnt_cur;
  105. return xchk_btree(sc, cur, xchk_allocbt_rec, &XFS_RMAP_OINFO_AG, NULL);
  106. }
  107. int
  108. xchk_bnobt(
  109. struct xfs_scrub *sc)
  110. {
  111. return xchk_allocbt(sc, XFS_BTNUM_BNO);
  112. }
  113. int
  114. xchk_cntbt(
  115. struct xfs_scrub *sc)
  116. {
  117. return xchk_allocbt(sc, XFS_BTNUM_CNT);
  118. }
  119. /* xref check that the extent is not free */
  120. void
  121. xchk_xref_is_used_space(
  122. struct xfs_scrub *sc,
  123. xfs_agblock_t agbno,
  124. xfs_extlen_t len)
  125. {
  126. bool is_freesp;
  127. int error;
  128. if (!sc->sa.bno_cur || xchk_skip_xref(sc->sm))
  129. return;
  130. error = xfs_alloc_has_record(sc->sa.bno_cur, agbno, len, &is_freesp);
  131. if (!xchk_should_check_xref(sc, &error, &sc->sa.bno_cur))
  132. return;
  133. if (is_freesp)
  134. xchk_btree_xref_set_corrupt(sc, sc->sa.bno_cur, 0);
  135. }