blob: 0a3bde64c675ef2fe8ee9bcd15e569afc5307fba [file] [log] [blame]
Dave Chinner0b61f8a2018-06-05 19:42:14 -07001// SPDX-License-Identifier: GPL-2.0+
Darrick J. Wong29b07672017-10-17 21:37:46 -07002/*
3 * Copyright (C) 2017 Oracle. All Rights Reserved.
Darrick J. Wong29b07672017-10-17 21:37:46 -07004 * Author: Darrick J. Wong <darrick.wong@oracle.com>
Darrick J. Wong29b07672017-10-17 21:37:46 -07005 */
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"
Darrick J. Wong29b07672017-10-17 21:37:46 -070012#include "xfs_log_format.h"
13#include "xfs_trans.h"
Darrick J. Wong29b07672017-10-17 21:37:46 -070014#include "xfs_rtalloc.h"
15#include "xfs_inode.h"
Darrick J. Wongf866560b2020-07-02 08:42:12 -070016#include "xfs_bmap.h"
Darrick J. Wong29b07672017-10-17 21:37:46 -070017#include "scrub/scrub.h"
18#include "scrub/common.h"
Darrick J. Wong29b07672017-10-17 21:37:46 -070019
20/* Set us up with the realtime metadata locked. */
21int
Darrick J. Wongc517b3a2018-07-19 12:29:11 -070022xchk_setup_rt(
Darrick J. Wong026f57e2021-04-07 17:59:39 -070023 struct xfs_scrub *sc)
Darrick J. Wong29b07672017-10-17 21:37:46 -070024{
Darrick J. Wong032d91f2018-07-19 12:29:12 -070025 int error;
Darrick J. Wong29b07672017-10-17 21:37:46 -070026
Darrick J. Wong026f57e2021-04-07 17:59:39 -070027 error = xchk_setup_fs(sc);
Darrick J. Wong29b07672017-10-17 21:37:46 -070028 if (error)
29 return error;
30
31 sc->ilock_flags = XFS_ILOCK_EXCL | XFS_ILOCK_RTBITMAP;
Eric Sandeen8e630832018-01-08 10:41:34 -080032 sc->ip = sc->mp->m_rbmip;
Darrick J. Wong29b07672017-10-17 21:37:46 -070033 xfs_ilock(sc->ip, sc->ilock_flags);
34
35 return 0;
36}
37
38/* Realtime bitmap. */
39
40/* Scrub a free extent record from the realtime bitmap. */
41STATIC int
Darrick J. Wongc517b3a2018-07-19 12:29:11 -070042xchk_rtbitmap_rec(
Darrick J. Wongf34061f2022-04-12 06:49:41 +100043 struct xfs_mount *mp,
Darrick J. Wong032d91f2018-07-19 12:29:12 -070044 struct xfs_trans *tp,
Darrick J. Wong159eb692021-08-10 17:02:16 -070045 const struct xfs_rtalloc_rec *rec,
Darrick J. Wong032d91f2018-07-19 12:29:12 -070046 void *priv)
Darrick J. Wong29b07672017-10-17 21:37:46 -070047{
Darrick J. Wong1d8a7482018-07-19 12:29:12 -070048 struct xfs_scrub *sc = priv;
Darrick J. Wong032d91f2018-07-19 12:29:12 -070049 xfs_rtblock_t startblock;
50 xfs_rtblock_t blockcount;
Darrick J. Wong29b07672017-10-17 21:37:46 -070051
Darrick J. Wongf34061f2022-04-12 06:49:41 +100052 startblock = rec->ar_startext * mp->m_sb.sb_rextsize;
53 blockcount = rec->ar_extcount * mp->m_sb.sb_rextsize;
Darrick J. Wonga0e5c432018-05-31 09:12:10 -070054
Darrick J. Wongf34061f2022-04-12 06:49:41 +100055 if (!xfs_verify_rtext(mp, startblock, blockcount))
Darrick J. Wongc517b3a2018-07-19 12:29:11 -070056 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, 0);
Darrick J. Wong29b07672017-10-17 21:37:46 -070057 return 0;
58}
59
Darrick J. Wongf866560b2020-07-02 08:42:12 -070060/* Make sure the entire rtbitmap file is mapped with written extents. */
61STATIC int
62xchk_rtbitmap_check_extents(
63 struct xfs_scrub *sc)
64{
65 struct xfs_mount *mp = sc->mp;
66 struct xfs_bmbt_irec map;
67 xfs_rtblock_t off;
68 int nmap;
69 int error = 0;
70
71 for (off = 0; off < mp->m_sb.sb_rbmblocks;) {
72 if (xchk_should_terminate(sc, &error) ||
73 (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT))
74 break;
75
76 /* Make sure we have a written extent. */
77 nmap = 1;
78 error = xfs_bmapi_read(mp->m_rbmip, off,
79 mp->m_sb.sb_rbmblocks - off, &map, &nmap,
80 XFS_DATA_FORK);
81 if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, off, &error))
82 break;
83
84 if (nmap != 1 || !xfs_bmap_is_written_extent(&map)) {
85 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, off);
86 break;
87 }
88
89 off += map.br_blockcount;
90 }
91
92 return error;
93}
94
Darrick J. Wong29b07672017-10-17 21:37:46 -070095/* Scrub the realtime bitmap. */
96int
Darrick J. Wongc517b3a2018-07-19 12:29:11 -070097xchk_rtbitmap(
Darrick J. Wong1d8a7482018-07-19 12:29:12 -070098 struct xfs_scrub *sc)
Darrick J. Wong29b07672017-10-17 21:37:46 -070099{
Darrick J. Wong032d91f2018-07-19 12:29:12 -0700100 int error;
Darrick J. Wong29b07672017-10-17 21:37:46 -0700101
Darrick J. Wong2fb94e32020-07-02 08:42:13 -0700102 /* Is the size of the rtbitmap correct? */
Christoph Hellwig13d2c102021-03-29 11:11:40 -0700103 if (sc->mp->m_rbmip->i_disk_size !=
Darrick J. Wong2fb94e32020-07-02 08:42:13 -0700104 XFS_FSB_TO_B(sc->mp, sc->mp->m_sb.sb_rbmblocks)) {
105 xchk_ino_set_corrupt(sc, sc->mp->m_rbmip->i_ino);
106 return 0;
107 }
108
Darrick J. Wong517b32b2018-05-14 06:34:33 -0700109 /* Invoke the fork scrubber. */
Darrick J. Wongc517b3a2018-07-19 12:29:11 -0700110 error = xchk_metadata_inode_forks(sc);
Darrick J. Wong517b32b2018-05-14 06:34:33 -0700111 if (error || (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT))
112 return error;
113
Darrick J. Wongf866560b2020-07-02 08:42:12 -0700114 error = xchk_rtbitmap_check_extents(sc);
115 if (error || (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT))
116 return error;
117
Darrick J. Wongf34061f2022-04-12 06:49:41 +1000118 error = xfs_rtalloc_query_all(sc->mp, sc->tp, xchk_rtbitmap_rec, sc);
Darrick J. Wongc517b3a2018-07-19 12:29:11 -0700119 if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, 0, &error))
Darrick J. Wong29b07672017-10-17 21:37:46 -0700120 goto out;
121
122out:
123 return error;
124}
125
126/* Scrub the realtime summary. */
127int
Darrick J. Wongc517b3a2018-07-19 12:29:11 -0700128xchk_rtsummary(
Darrick J. Wong1d8a7482018-07-19 12:29:12 -0700129 struct xfs_scrub *sc)
Darrick J. Wong29b07672017-10-17 21:37:46 -0700130{
Darrick J. Wong032d91f2018-07-19 12:29:12 -0700131 struct xfs_inode *rsumip = sc->mp->m_rsumip;
132 struct xfs_inode *old_ip = sc->ip;
133 uint old_ilock_flags = sc->ilock_flags;
134 int error = 0;
Darrick J. Wong517b32b2018-05-14 06:34:33 -0700135
136 /*
137 * We ILOCK'd the rt bitmap ip in the setup routine, now lock the
138 * rt summary ip in compliance with the rt inode locking rules.
139 *
140 * Since we switch sc->ip to rsumip we have to save the old ilock
141 * flags so that we don't mix up the inode state that @sc tracks.
142 */
143 sc->ip = rsumip;
144 sc->ilock_flags = XFS_ILOCK_EXCL | XFS_ILOCK_RTSUM;
145 xfs_ilock(sc->ip, sc->ilock_flags);
146
147 /* Invoke the fork scrubber. */
Darrick J. Wongc517b3a2018-07-19 12:29:11 -0700148 error = xchk_metadata_inode_forks(sc);
Darrick J. Wong517b32b2018-05-14 06:34:33 -0700149 if (error || (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT))
150 goto out;
151
Darrick J. Wong29b07672017-10-17 21:37:46 -0700152 /* XXX: implement this some day */
Darrick J. Wongc517b3a2018-07-19 12:29:11 -0700153 xchk_set_incomplete(sc);
Darrick J. Wong517b32b2018-05-14 06:34:33 -0700154out:
155 /* Switch back to the rtbitmap inode and lock flags. */
156 xfs_iunlock(sc->ip, sc->ilock_flags);
157 sc->ilock_flags = old_ilock_flags;
158 sc->ip = old_ip;
159 return error;
Darrick J. Wong29b07672017-10-17 21:37:46 -0700160}
Darrick J. Wong46d9bfb2018-01-16 18:53:10 -0800161
162
163/* xref check that the extent is not free in the rtbitmap */
164void
Darrick J. Wongc517b3a2018-07-19 12:29:11 -0700165xchk_xref_is_used_rt_space(
Darrick J. Wong1d8a7482018-07-19 12:29:12 -0700166 struct xfs_scrub *sc,
Darrick J. Wong032d91f2018-07-19 12:29:12 -0700167 xfs_rtblock_t fsbno,
168 xfs_extlen_t len)
Darrick J. Wong46d9bfb2018-01-16 18:53:10 -0800169{
Darrick J. Wong032d91f2018-07-19 12:29:12 -0700170 xfs_rtblock_t startext;
171 xfs_rtblock_t endext;
172 xfs_rtblock_t extcount;
173 bool is_free;
174 int error;
Darrick J. Wong46d9bfb2018-01-16 18:53:10 -0800175
Darrick J. Wongc517b3a2018-07-19 12:29:11 -0700176 if (xchk_skip_xref(sc->sm))
Darrick J. Wong8389f3ff2018-05-14 06:34:31 -0700177 return;
178
Darrick J. Wonga0e5c432018-05-31 09:12:10 -0700179 startext = fsbno;
180 endext = fsbno + len - 1;
181 do_div(startext, sc->mp->m_sb.sb_rextsize);
Darrick J. Wong87c96072019-02-01 09:08:53 -0800182 do_div(endext, sc->mp->m_sb.sb_rextsize);
183 extcount = endext - startext + 1;
Darrick J. Wong46d9bfb2018-01-16 18:53:10 -0800184 xfs_ilock(sc->mp->m_rbmip, XFS_ILOCK_SHARED | XFS_ILOCK_RTBITMAP);
Darrick J. Wonga0e5c432018-05-31 09:12:10 -0700185 error = xfs_rtalloc_extent_is_free(sc->mp, sc->tp, startext, extcount,
Darrick J. Wong46d9bfb2018-01-16 18:53:10 -0800186 &is_free);
Darrick J. Wongc517b3a2018-07-19 12:29:11 -0700187 if (!xchk_should_check_xref(sc, &error, NULL))
Darrick J. Wong46d9bfb2018-01-16 18:53:10 -0800188 goto out_unlock;
189 if (is_free)
Darrick J. Wongc517b3a2018-07-19 12:29:11 -0700190 xchk_ino_xref_set_corrupt(sc, sc->mp->m_rbmip->i_ino);
Darrick J. Wong46d9bfb2018-01-16 18:53:10 -0800191out_unlock:
192 xfs_iunlock(sc->mp->m_rbmip, XFS_ILOCK_SHARED | XFS_ILOCK_RTBITMAP);
193}