1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0+
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Copyright (C) 2017 Oracle. All Rights Reserved.
4*4882a593Smuzhiyun * Author: Darrick J. Wong <darrick.wong@oracle.com>
5*4882a593Smuzhiyun */
6*4882a593Smuzhiyun #include "xfs.h"
7*4882a593Smuzhiyun #include "xfs_fs.h"
8*4882a593Smuzhiyun #include "xfs_shared.h"
9*4882a593Smuzhiyun #include "xfs_format.h"
10*4882a593Smuzhiyun #include "xfs_trans_resv.h"
11*4882a593Smuzhiyun #include "xfs_mount.h"
12*4882a593Smuzhiyun #include "xfs_log_format.h"
13*4882a593Smuzhiyun #include "xfs_trans.h"
14*4882a593Smuzhiyun #include "xfs_rtalloc.h"
15*4882a593Smuzhiyun #include "xfs_inode.h"
16*4882a593Smuzhiyun #include "xfs_bmap.h"
17*4882a593Smuzhiyun #include "scrub/scrub.h"
18*4882a593Smuzhiyun #include "scrub/common.h"
19*4882a593Smuzhiyun
20*4882a593Smuzhiyun /* Set us up with the realtime metadata locked. */
21*4882a593Smuzhiyun int
xchk_setup_rt(struct xfs_scrub * sc,struct xfs_inode * ip)22*4882a593Smuzhiyun xchk_setup_rt(
23*4882a593Smuzhiyun struct xfs_scrub *sc,
24*4882a593Smuzhiyun struct xfs_inode *ip)
25*4882a593Smuzhiyun {
26*4882a593Smuzhiyun int error;
27*4882a593Smuzhiyun
28*4882a593Smuzhiyun error = xchk_setup_fs(sc, ip);
29*4882a593Smuzhiyun if (error)
30*4882a593Smuzhiyun return error;
31*4882a593Smuzhiyun
32*4882a593Smuzhiyun sc->ilock_flags = XFS_ILOCK_EXCL | XFS_ILOCK_RTBITMAP;
33*4882a593Smuzhiyun sc->ip = sc->mp->m_rbmip;
34*4882a593Smuzhiyun xfs_ilock(sc->ip, sc->ilock_flags);
35*4882a593Smuzhiyun
36*4882a593Smuzhiyun return 0;
37*4882a593Smuzhiyun }
38*4882a593Smuzhiyun
39*4882a593Smuzhiyun /* Realtime bitmap. */
40*4882a593Smuzhiyun
41*4882a593Smuzhiyun /* Scrub a free extent record from the realtime bitmap. */
42*4882a593Smuzhiyun STATIC int
xchk_rtbitmap_rec(struct xfs_trans * tp,struct xfs_rtalloc_rec * rec,void * priv)43*4882a593Smuzhiyun xchk_rtbitmap_rec(
44*4882a593Smuzhiyun struct xfs_trans *tp,
45*4882a593Smuzhiyun struct xfs_rtalloc_rec *rec,
46*4882a593Smuzhiyun void *priv)
47*4882a593Smuzhiyun {
48*4882a593Smuzhiyun struct xfs_scrub *sc = priv;
49*4882a593Smuzhiyun xfs_rtblock_t startblock;
50*4882a593Smuzhiyun xfs_rtblock_t blockcount;
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun startblock = rec->ar_startext * tp->t_mountp->m_sb.sb_rextsize;
53*4882a593Smuzhiyun blockcount = rec->ar_extcount * tp->t_mountp->m_sb.sb_rextsize;
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun if (startblock + blockcount <= startblock ||
56*4882a593Smuzhiyun !xfs_verify_rtbno(sc->mp, startblock) ||
57*4882a593Smuzhiyun !xfs_verify_rtbno(sc->mp, startblock + blockcount - 1))
58*4882a593Smuzhiyun xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, 0);
59*4882a593Smuzhiyun return 0;
60*4882a593Smuzhiyun }
61*4882a593Smuzhiyun
62*4882a593Smuzhiyun /* Make sure the entire rtbitmap file is mapped with written extents. */
63*4882a593Smuzhiyun STATIC int
xchk_rtbitmap_check_extents(struct xfs_scrub * sc)64*4882a593Smuzhiyun xchk_rtbitmap_check_extents(
65*4882a593Smuzhiyun struct xfs_scrub *sc)
66*4882a593Smuzhiyun {
67*4882a593Smuzhiyun struct xfs_mount *mp = sc->mp;
68*4882a593Smuzhiyun struct xfs_bmbt_irec map;
69*4882a593Smuzhiyun xfs_rtblock_t off;
70*4882a593Smuzhiyun int nmap;
71*4882a593Smuzhiyun int error = 0;
72*4882a593Smuzhiyun
73*4882a593Smuzhiyun for (off = 0; off < mp->m_sb.sb_rbmblocks;) {
74*4882a593Smuzhiyun if (xchk_should_terminate(sc, &error) ||
75*4882a593Smuzhiyun (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT))
76*4882a593Smuzhiyun break;
77*4882a593Smuzhiyun
78*4882a593Smuzhiyun /* Make sure we have a written extent. */
79*4882a593Smuzhiyun nmap = 1;
80*4882a593Smuzhiyun error = xfs_bmapi_read(mp->m_rbmip, off,
81*4882a593Smuzhiyun mp->m_sb.sb_rbmblocks - off, &map, &nmap,
82*4882a593Smuzhiyun XFS_DATA_FORK);
83*4882a593Smuzhiyun if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, off, &error))
84*4882a593Smuzhiyun break;
85*4882a593Smuzhiyun
86*4882a593Smuzhiyun if (nmap != 1 || !xfs_bmap_is_written_extent(&map)) {
87*4882a593Smuzhiyun xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, off);
88*4882a593Smuzhiyun break;
89*4882a593Smuzhiyun }
90*4882a593Smuzhiyun
91*4882a593Smuzhiyun off += map.br_blockcount;
92*4882a593Smuzhiyun }
93*4882a593Smuzhiyun
94*4882a593Smuzhiyun return error;
95*4882a593Smuzhiyun }
96*4882a593Smuzhiyun
97*4882a593Smuzhiyun /* Scrub the realtime bitmap. */
98*4882a593Smuzhiyun int
xchk_rtbitmap(struct xfs_scrub * sc)99*4882a593Smuzhiyun xchk_rtbitmap(
100*4882a593Smuzhiyun struct xfs_scrub *sc)
101*4882a593Smuzhiyun {
102*4882a593Smuzhiyun int error;
103*4882a593Smuzhiyun
104*4882a593Smuzhiyun /* Is the size of the rtbitmap correct? */
105*4882a593Smuzhiyun if (sc->mp->m_rbmip->i_d.di_size !=
106*4882a593Smuzhiyun XFS_FSB_TO_B(sc->mp, sc->mp->m_sb.sb_rbmblocks)) {
107*4882a593Smuzhiyun xchk_ino_set_corrupt(sc, sc->mp->m_rbmip->i_ino);
108*4882a593Smuzhiyun return 0;
109*4882a593Smuzhiyun }
110*4882a593Smuzhiyun
111*4882a593Smuzhiyun /* Invoke the fork scrubber. */
112*4882a593Smuzhiyun error = xchk_metadata_inode_forks(sc);
113*4882a593Smuzhiyun if (error || (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT))
114*4882a593Smuzhiyun return error;
115*4882a593Smuzhiyun
116*4882a593Smuzhiyun error = xchk_rtbitmap_check_extents(sc);
117*4882a593Smuzhiyun if (error || (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT))
118*4882a593Smuzhiyun return error;
119*4882a593Smuzhiyun
120*4882a593Smuzhiyun error = xfs_rtalloc_query_all(sc->tp, xchk_rtbitmap_rec, sc);
121*4882a593Smuzhiyun if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, 0, &error))
122*4882a593Smuzhiyun goto out;
123*4882a593Smuzhiyun
124*4882a593Smuzhiyun out:
125*4882a593Smuzhiyun return error;
126*4882a593Smuzhiyun }
127*4882a593Smuzhiyun
128*4882a593Smuzhiyun /* Scrub the realtime summary. */
129*4882a593Smuzhiyun int
xchk_rtsummary(struct xfs_scrub * sc)130*4882a593Smuzhiyun xchk_rtsummary(
131*4882a593Smuzhiyun struct xfs_scrub *sc)
132*4882a593Smuzhiyun {
133*4882a593Smuzhiyun struct xfs_inode *rsumip = sc->mp->m_rsumip;
134*4882a593Smuzhiyun struct xfs_inode *old_ip = sc->ip;
135*4882a593Smuzhiyun uint old_ilock_flags = sc->ilock_flags;
136*4882a593Smuzhiyun int error = 0;
137*4882a593Smuzhiyun
138*4882a593Smuzhiyun /*
139*4882a593Smuzhiyun * We ILOCK'd the rt bitmap ip in the setup routine, now lock the
140*4882a593Smuzhiyun * rt summary ip in compliance with the rt inode locking rules.
141*4882a593Smuzhiyun *
142*4882a593Smuzhiyun * Since we switch sc->ip to rsumip we have to save the old ilock
143*4882a593Smuzhiyun * flags so that we don't mix up the inode state that @sc tracks.
144*4882a593Smuzhiyun */
145*4882a593Smuzhiyun sc->ip = rsumip;
146*4882a593Smuzhiyun sc->ilock_flags = XFS_ILOCK_EXCL | XFS_ILOCK_RTSUM;
147*4882a593Smuzhiyun xfs_ilock(sc->ip, sc->ilock_flags);
148*4882a593Smuzhiyun
149*4882a593Smuzhiyun /* Invoke the fork scrubber. */
150*4882a593Smuzhiyun error = xchk_metadata_inode_forks(sc);
151*4882a593Smuzhiyun if (error || (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT))
152*4882a593Smuzhiyun goto out;
153*4882a593Smuzhiyun
154*4882a593Smuzhiyun /* XXX: implement this some day */
155*4882a593Smuzhiyun xchk_set_incomplete(sc);
156*4882a593Smuzhiyun out:
157*4882a593Smuzhiyun /* Switch back to the rtbitmap inode and lock flags. */
158*4882a593Smuzhiyun xfs_iunlock(sc->ip, sc->ilock_flags);
159*4882a593Smuzhiyun sc->ilock_flags = old_ilock_flags;
160*4882a593Smuzhiyun sc->ip = old_ip;
161*4882a593Smuzhiyun return error;
162*4882a593Smuzhiyun }
163*4882a593Smuzhiyun
164*4882a593Smuzhiyun
165*4882a593Smuzhiyun /* xref check that the extent is not free in the rtbitmap */
166*4882a593Smuzhiyun void
xchk_xref_is_used_rt_space(struct xfs_scrub * sc,xfs_rtblock_t fsbno,xfs_extlen_t len)167*4882a593Smuzhiyun xchk_xref_is_used_rt_space(
168*4882a593Smuzhiyun struct xfs_scrub *sc,
169*4882a593Smuzhiyun xfs_rtblock_t fsbno,
170*4882a593Smuzhiyun xfs_extlen_t len)
171*4882a593Smuzhiyun {
172*4882a593Smuzhiyun xfs_rtblock_t startext;
173*4882a593Smuzhiyun xfs_rtblock_t endext;
174*4882a593Smuzhiyun xfs_rtblock_t extcount;
175*4882a593Smuzhiyun bool is_free;
176*4882a593Smuzhiyun int error;
177*4882a593Smuzhiyun
178*4882a593Smuzhiyun if (xchk_skip_xref(sc->sm))
179*4882a593Smuzhiyun return;
180*4882a593Smuzhiyun
181*4882a593Smuzhiyun startext = fsbno;
182*4882a593Smuzhiyun endext = fsbno + len - 1;
183*4882a593Smuzhiyun do_div(startext, sc->mp->m_sb.sb_rextsize);
184*4882a593Smuzhiyun do_div(endext, sc->mp->m_sb.sb_rextsize);
185*4882a593Smuzhiyun extcount = endext - startext + 1;
186*4882a593Smuzhiyun xfs_ilock(sc->mp->m_rbmip, XFS_ILOCK_SHARED | XFS_ILOCK_RTBITMAP);
187*4882a593Smuzhiyun error = xfs_rtalloc_extent_is_free(sc->mp, sc->tp, startext, extcount,
188*4882a593Smuzhiyun &is_free);
189*4882a593Smuzhiyun if (!xchk_should_check_xref(sc, &error, NULL))
190*4882a593Smuzhiyun goto out_unlock;
191*4882a593Smuzhiyun if (is_free)
192*4882a593Smuzhiyun xchk_ino_xref_set_corrupt(sc, sc->mp->m_rbmip->i_ino);
193*4882a593Smuzhiyun out_unlock:
194*4882a593Smuzhiyun xfs_iunlock(sc->mp->m_rbmip, XFS_ILOCK_SHARED | XFS_ILOCK_RTBITMAP);
195*4882a593Smuzhiyun }
196