rtbitmap.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Copyright (C) 2017 Oracle. All Rights Reserved.
  3. *
  4. * Author: Darrick J. Wong <darrick.wong@oracle.com>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it would be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write the Free Software Foundation,
  18. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. #include "xfs.h"
  21. #include "xfs_fs.h"
  22. #include "xfs_shared.h"
  23. #include "xfs_format.h"
  24. #include "xfs_trans_resv.h"
  25. #include "xfs_mount.h"
  26. #include "xfs_defer.h"
  27. #include "xfs_btree.h"
  28. #include "xfs_bit.h"
  29. #include "xfs_log_format.h"
  30. #include "xfs_trans.h"
  31. #include "xfs_sb.h"
  32. #include "xfs_alloc.h"
  33. #include "xfs_rtalloc.h"
  34. #include "xfs_inode.h"
  35. #include "scrub/xfs_scrub.h"
  36. #include "scrub/scrub.h"
  37. #include "scrub/common.h"
  38. #include "scrub/trace.h"
  39. /* Set us up with the realtime metadata locked. */
  40. int
  41. xfs_scrub_setup_rt(
  42. struct xfs_scrub_context *sc,
  43. struct xfs_inode *ip)
  44. {
  45. int error;
  46. error = xfs_scrub_setup_fs(sc, ip);
  47. if (error)
  48. return error;
  49. sc->ilock_flags = XFS_ILOCK_EXCL | XFS_ILOCK_RTBITMAP;
  50. sc->ip = sc->mp->m_rbmip;
  51. xfs_ilock(sc->ip, sc->ilock_flags);
  52. return 0;
  53. }
  54. /* Realtime bitmap. */
  55. /* Scrub a free extent record from the realtime bitmap. */
  56. STATIC int
  57. xfs_scrub_rtbitmap_rec(
  58. struct xfs_trans *tp,
  59. struct xfs_rtalloc_rec *rec,
  60. void *priv)
  61. {
  62. struct xfs_scrub_context *sc = priv;
  63. if (rec->ar_startblock + rec->ar_blockcount <= rec->ar_startblock ||
  64. !xfs_verify_rtbno(sc->mp, rec->ar_startblock) ||
  65. !xfs_verify_rtbno(sc->mp, rec->ar_startblock +
  66. rec->ar_blockcount - 1))
  67. xfs_scrub_fblock_set_corrupt(sc, XFS_DATA_FORK, 0);
  68. return 0;
  69. }
  70. /* Scrub the realtime bitmap. */
  71. int
  72. xfs_scrub_rtbitmap(
  73. struct xfs_scrub_context *sc)
  74. {
  75. int error;
  76. /* Invoke the fork scrubber. */
  77. error = xfs_scrub_metadata_inode_forks(sc);
  78. if (error || (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT))
  79. return error;
  80. error = xfs_rtalloc_query_all(sc->tp, xfs_scrub_rtbitmap_rec, sc);
  81. if (!xfs_scrub_fblock_process_error(sc, XFS_DATA_FORK, 0, &error))
  82. goto out;
  83. out:
  84. return error;
  85. }
  86. /* Scrub the realtime summary. */
  87. int
  88. xfs_scrub_rtsummary(
  89. struct xfs_scrub_context *sc)
  90. {
  91. struct xfs_inode *rsumip = sc->mp->m_rsumip;
  92. struct xfs_inode *old_ip = sc->ip;
  93. uint old_ilock_flags = sc->ilock_flags;
  94. int error = 0;
  95. /*
  96. * We ILOCK'd the rt bitmap ip in the setup routine, now lock the
  97. * rt summary ip in compliance with the rt inode locking rules.
  98. *
  99. * Since we switch sc->ip to rsumip we have to save the old ilock
  100. * flags so that we don't mix up the inode state that @sc tracks.
  101. */
  102. sc->ip = rsumip;
  103. sc->ilock_flags = XFS_ILOCK_EXCL | XFS_ILOCK_RTSUM;
  104. xfs_ilock(sc->ip, sc->ilock_flags);
  105. /* Invoke the fork scrubber. */
  106. error = xfs_scrub_metadata_inode_forks(sc);
  107. if (error || (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT))
  108. goto out;
  109. /* XXX: implement this some day */
  110. xfs_scrub_set_incomplete(sc);
  111. out:
  112. /* Switch back to the rtbitmap inode and lock flags. */
  113. xfs_iunlock(sc->ip, sc->ilock_flags);
  114. sc->ilock_flags = old_ilock_flags;
  115. sc->ip = old_ip;
  116. return error;
  117. }
  118. /* xref check that the extent is not free in the rtbitmap */
  119. void
  120. xfs_scrub_xref_is_used_rt_space(
  121. struct xfs_scrub_context *sc,
  122. xfs_rtblock_t fsbno,
  123. xfs_extlen_t len)
  124. {
  125. bool is_free;
  126. int error;
  127. if (xfs_scrub_skip_xref(sc->sm))
  128. return;
  129. xfs_ilock(sc->mp->m_rbmip, XFS_ILOCK_SHARED | XFS_ILOCK_RTBITMAP);
  130. error = xfs_rtalloc_extent_is_free(sc->mp, sc->tp, fsbno, len,
  131. &is_free);
  132. if (!xfs_scrub_should_check_xref(sc, &error, NULL))
  133. goto out_unlock;
  134. if (is_free)
  135. xfs_scrub_ino_xref_set_corrupt(sc, sc->mp->m_rbmip->i_ino);
  136. out_unlock:
  137. xfs_iunlock(sc->mp->m_rbmip, XFS_ILOCK_SHARED | XFS_ILOCK_RTBITMAP);
  138. }