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_btree.h"
13*4882a593Smuzhiyun #include "xfs_bit.h"
14*4882a593Smuzhiyun #include "xfs_log_format.h"
15*4882a593Smuzhiyun #include "xfs_trans.h"
16*4882a593Smuzhiyun #include "xfs_inode.h"
17*4882a593Smuzhiyun #include "xfs_alloc.h"
18*4882a593Smuzhiyun #include "xfs_bmap.h"
19*4882a593Smuzhiyun #include "xfs_bmap_btree.h"
20*4882a593Smuzhiyun #include "xfs_rmap.h"
21*4882a593Smuzhiyun #include "xfs_rmap_btree.h"
22*4882a593Smuzhiyun #include "scrub/scrub.h"
23*4882a593Smuzhiyun #include "scrub/common.h"
24*4882a593Smuzhiyun #include "scrub/btree.h"
25*4882a593Smuzhiyun
26*4882a593Smuzhiyun /* Set us up with an inode's bmap. */
27*4882a593Smuzhiyun int
xchk_setup_inode_bmap(struct xfs_scrub * sc,struct xfs_inode * ip)28*4882a593Smuzhiyun xchk_setup_inode_bmap(
29*4882a593Smuzhiyun struct xfs_scrub *sc,
30*4882a593Smuzhiyun struct xfs_inode *ip)
31*4882a593Smuzhiyun {
32*4882a593Smuzhiyun int error;
33*4882a593Smuzhiyun
34*4882a593Smuzhiyun error = xchk_get_inode(sc, ip);
35*4882a593Smuzhiyun if (error)
36*4882a593Smuzhiyun goto out;
37*4882a593Smuzhiyun
38*4882a593Smuzhiyun sc->ilock_flags = XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL;
39*4882a593Smuzhiyun xfs_ilock(sc->ip, sc->ilock_flags);
40*4882a593Smuzhiyun
41*4882a593Smuzhiyun /*
42*4882a593Smuzhiyun * We don't want any ephemeral data fork updates sitting around
43*4882a593Smuzhiyun * while we inspect block mappings, so wait for directio to finish
44*4882a593Smuzhiyun * and flush dirty data if we have delalloc reservations.
45*4882a593Smuzhiyun */
46*4882a593Smuzhiyun if (S_ISREG(VFS_I(sc->ip)->i_mode) &&
47*4882a593Smuzhiyun sc->sm->sm_type == XFS_SCRUB_TYPE_BMBTD) {
48*4882a593Smuzhiyun struct address_space *mapping = VFS_I(sc->ip)->i_mapping;
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun inode_dio_wait(VFS_I(sc->ip));
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun /*
53*4882a593Smuzhiyun * Try to flush all incore state to disk before we examine the
54*4882a593Smuzhiyun * space mappings for the data fork. Leave accumulated errors
55*4882a593Smuzhiyun * in the mapping for the writer threads to consume.
56*4882a593Smuzhiyun *
57*4882a593Smuzhiyun * On ENOSPC or EIO writeback errors, we continue into the
58*4882a593Smuzhiyun * extent mapping checks because write failures do not
59*4882a593Smuzhiyun * necessarily imply anything about the correctness of the file
60*4882a593Smuzhiyun * metadata. The metadata and the file data could be on
61*4882a593Smuzhiyun * completely separate devices; a media failure might only
62*4882a593Smuzhiyun * affect a subset of the disk, etc. We can handle delalloc
63*4882a593Smuzhiyun * extents in the scrubber, so leaving them in memory is fine.
64*4882a593Smuzhiyun */
65*4882a593Smuzhiyun error = filemap_fdatawrite(mapping);
66*4882a593Smuzhiyun if (!error)
67*4882a593Smuzhiyun error = filemap_fdatawait_keep_errors(mapping);
68*4882a593Smuzhiyun if (error && (error != -ENOSPC && error != -EIO))
69*4882a593Smuzhiyun goto out;
70*4882a593Smuzhiyun }
71*4882a593Smuzhiyun
72*4882a593Smuzhiyun /* Got the inode, lock it and we're ready to go. */
73*4882a593Smuzhiyun error = xchk_trans_alloc(sc, 0);
74*4882a593Smuzhiyun if (error)
75*4882a593Smuzhiyun goto out;
76*4882a593Smuzhiyun sc->ilock_flags |= XFS_ILOCK_EXCL;
77*4882a593Smuzhiyun xfs_ilock(sc->ip, XFS_ILOCK_EXCL);
78*4882a593Smuzhiyun
79*4882a593Smuzhiyun out:
80*4882a593Smuzhiyun /* scrub teardown will unlock and release the inode */
81*4882a593Smuzhiyun return error;
82*4882a593Smuzhiyun }
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun /*
85*4882a593Smuzhiyun * Inode fork block mapping (BMBT) scrubber.
86*4882a593Smuzhiyun * More complex than the others because we have to scrub
87*4882a593Smuzhiyun * all the extents regardless of whether or not the fork
88*4882a593Smuzhiyun * is in btree format.
89*4882a593Smuzhiyun */
90*4882a593Smuzhiyun
91*4882a593Smuzhiyun struct xchk_bmap_info {
92*4882a593Smuzhiyun struct xfs_scrub *sc;
93*4882a593Smuzhiyun xfs_fileoff_t lastoff;
94*4882a593Smuzhiyun bool is_rt;
95*4882a593Smuzhiyun bool is_shared;
96*4882a593Smuzhiyun bool was_loaded;
97*4882a593Smuzhiyun int whichfork;
98*4882a593Smuzhiyun };
99*4882a593Smuzhiyun
100*4882a593Smuzhiyun /* Look for a corresponding rmap for this irec. */
101*4882a593Smuzhiyun static inline bool
xchk_bmap_get_rmap(struct xchk_bmap_info * info,struct xfs_bmbt_irec * irec,xfs_agblock_t agbno,uint64_t owner,struct xfs_rmap_irec * rmap)102*4882a593Smuzhiyun xchk_bmap_get_rmap(
103*4882a593Smuzhiyun struct xchk_bmap_info *info,
104*4882a593Smuzhiyun struct xfs_bmbt_irec *irec,
105*4882a593Smuzhiyun xfs_agblock_t agbno,
106*4882a593Smuzhiyun uint64_t owner,
107*4882a593Smuzhiyun struct xfs_rmap_irec *rmap)
108*4882a593Smuzhiyun {
109*4882a593Smuzhiyun xfs_fileoff_t offset;
110*4882a593Smuzhiyun unsigned int rflags = 0;
111*4882a593Smuzhiyun int has_rmap;
112*4882a593Smuzhiyun int error;
113*4882a593Smuzhiyun
114*4882a593Smuzhiyun if (info->whichfork == XFS_ATTR_FORK)
115*4882a593Smuzhiyun rflags |= XFS_RMAP_ATTR_FORK;
116*4882a593Smuzhiyun if (irec->br_state == XFS_EXT_UNWRITTEN)
117*4882a593Smuzhiyun rflags |= XFS_RMAP_UNWRITTEN;
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun /*
120*4882a593Smuzhiyun * CoW staging extents are owned (on disk) by the refcountbt, so
121*4882a593Smuzhiyun * their rmaps do not have offsets.
122*4882a593Smuzhiyun */
123*4882a593Smuzhiyun if (info->whichfork == XFS_COW_FORK)
124*4882a593Smuzhiyun offset = 0;
125*4882a593Smuzhiyun else
126*4882a593Smuzhiyun offset = irec->br_startoff;
127*4882a593Smuzhiyun
128*4882a593Smuzhiyun /*
129*4882a593Smuzhiyun * If the caller thinks this could be a shared bmbt extent (IOWs,
130*4882a593Smuzhiyun * any data fork extent of a reflink inode) then we have to use the
131*4882a593Smuzhiyun * range rmap lookup to make sure we get the correct owner/offset.
132*4882a593Smuzhiyun */
133*4882a593Smuzhiyun if (info->is_shared) {
134*4882a593Smuzhiyun error = xfs_rmap_lookup_le_range(info->sc->sa.rmap_cur, agbno,
135*4882a593Smuzhiyun owner, offset, rflags, rmap, &has_rmap);
136*4882a593Smuzhiyun if (!xchk_should_check_xref(info->sc, &error,
137*4882a593Smuzhiyun &info->sc->sa.rmap_cur))
138*4882a593Smuzhiyun return false;
139*4882a593Smuzhiyun goto out;
140*4882a593Smuzhiyun }
141*4882a593Smuzhiyun
142*4882a593Smuzhiyun /*
143*4882a593Smuzhiyun * Otherwise, use the (faster) regular lookup.
144*4882a593Smuzhiyun */
145*4882a593Smuzhiyun error = xfs_rmap_lookup_le(info->sc->sa.rmap_cur, agbno, 0, owner,
146*4882a593Smuzhiyun offset, rflags, &has_rmap);
147*4882a593Smuzhiyun if (!xchk_should_check_xref(info->sc, &error,
148*4882a593Smuzhiyun &info->sc->sa.rmap_cur))
149*4882a593Smuzhiyun return false;
150*4882a593Smuzhiyun if (!has_rmap)
151*4882a593Smuzhiyun goto out;
152*4882a593Smuzhiyun
153*4882a593Smuzhiyun error = xfs_rmap_get_rec(info->sc->sa.rmap_cur, rmap, &has_rmap);
154*4882a593Smuzhiyun if (!xchk_should_check_xref(info->sc, &error,
155*4882a593Smuzhiyun &info->sc->sa.rmap_cur))
156*4882a593Smuzhiyun return false;
157*4882a593Smuzhiyun
158*4882a593Smuzhiyun out:
159*4882a593Smuzhiyun if (!has_rmap)
160*4882a593Smuzhiyun xchk_fblock_xref_set_corrupt(info->sc, info->whichfork,
161*4882a593Smuzhiyun irec->br_startoff);
162*4882a593Smuzhiyun return has_rmap;
163*4882a593Smuzhiyun }
164*4882a593Smuzhiyun
165*4882a593Smuzhiyun /* Make sure that we have rmapbt records for this extent. */
166*4882a593Smuzhiyun STATIC void
xchk_bmap_xref_rmap(struct xchk_bmap_info * info,struct xfs_bmbt_irec * irec,xfs_agblock_t agbno)167*4882a593Smuzhiyun xchk_bmap_xref_rmap(
168*4882a593Smuzhiyun struct xchk_bmap_info *info,
169*4882a593Smuzhiyun struct xfs_bmbt_irec *irec,
170*4882a593Smuzhiyun xfs_agblock_t agbno)
171*4882a593Smuzhiyun {
172*4882a593Smuzhiyun struct xfs_rmap_irec rmap;
173*4882a593Smuzhiyun unsigned long long rmap_end;
174*4882a593Smuzhiyun uint64_t owner;
175*4882a593Smuzhiyun
176*4882a593Smuzhiyun if (!info->sc->sa.rmap_cur || xchk_skip_xref(info->sc->sm))
177*4882a593Smuzhiyun return;
178*4882a593Smuzhiyun
179*4882a593Smuzhiyun if (info->whichfork == XFS_COW_FORK)
180*4882a593Smuzhiyun owner = XFS_RMAP_OWN_COW;
181*4882a593Smuzhiyun else
182*4882a593Smuzhiyun owner = info->sc->ip->i_ino;
183*4882a593Smuzhiyun
184*4882a593Smuzhiyun /* Find the rmap record for this irec. */
185*4882a593Smuzhiyun if (!xchk_bmap_get_rmap(info, irec, agbno, owner, &rmap))
186*4882a593Smuzhiyun return;
187*4882a593Smuzhiyun
188*4882a593Smuzhiyun /* Check the rmap. */
189*4882a593Smuzhiyun rmap_end = (unsigned long long)rmap.rm_startblock + rmap.rm_blockcount;
190*4882a593Smuzhiyun if (rmap.rm_startblock > agbno ||
191*4882a593Smuzhiyun agbno + irec->br_blockcount > rmap_end)
192*4882a593Smuzhiyun xchk_fblock_xref_set_corrupt(info->sc, info->whichfork,
193*4882a593Smuzhiyun irec->br_startoff);
194*4882a593Smuzhiyun
195*4882a593Smuzhiyun /*
196*4882a593Smuzhiyun * Check the logical offsets if applicable. CoW staging extents
197*4882a593Smuzhiyun * don't track logical offsets since the mappings only exist in
198*4882a593Smuzhiyun * memory.
199*4882a593Smuzhiyun */
200*4882a593Smuzhiyun if (info->whichfork != XFS_COW_FORK) {
201*4882a593Smuzhiyun rmap_end = (unsigned long long)rmap.rm_offset +
202*4882a593Smuzhiyun rmap.rm_blockcount;
203*4882a593Smuzhiyun if (rmap.rm_offset > irec->br_startoff ||
204*4882a593Smuzhiyun irec->br_startoff + irec->br_blockcount > rmap_end)
205*4882a593Smuzhiyun xchk_fblock_xref_set_corrupt(info->sc,
206*4882a593Smuzhiyun info->whichfork, irec->br_startoff);
207*4882a593Smuzhiyun }
208*4882a593Smuzhiyun
209*4882a593Smuzhiyun if (rmap.rm_owner != owner)
210*4882a593Smuzhiyun xchk_fblock_xref_set_corrupt(info->sc, info->whichfork,
211*4882a593Smuzhiyun irec->br_startoff);
212*4882a593Smuzhiyun
213*4882a593Smuzhiyun /*
214*4882a593Smuzhiyun * Check for discrepancies between the unwritten flag in the irec and
215*4882a593Smuzhiyun * the rmap. Note that the (in-memory) CoW fork distinguishes between
216*4882a593Smuzhiyun * unwritten and written extents, but we don't track that in the rmap
217*4882a593Smuzhiyun * records because the blocks are owned (on-disk) by the refcountbt,
218*4882a593Smuzhiyun * which doesn't track unwritten state.
219*4882a593Smuzhiyun */
220*4882a593Smuzhiyun if (owner != XFS_RMAP_OWN_COW &&
221*4882a593Smuzhiyun !!(irec->br_state == XFS_EXT_UNWRITTEN) !=
222*4882a593Smuzhiyun !!(rmap.rm_flags & XFS_RMAP_UNWRITTEN))
223*4882a593Smuzhiyun xchk_fblock_xref_set_corrupt(info->sc, info->whichfork,
224*4882a593Smuzhiyun irec->br_startoff);
225*4882a593Smuzhiyun
226*4882a593Smuzhiyun if (!!(info->whichfork == XFS_ATTR_FORK) !=
227*4882a593Smuzhiyun !!(rmap.rm_flags & XFS_RMAP_ATTR_FORK))
228*4882a593Smuzhiyun xchk_fblock_xref_set_corrupt(info->sc, info->whichfork,
229*4882a593Smuzhiyun irec->br_startoff);
230*4882a593Smuzhiyun if (rmap.rm_flags & XFS_RMAP_BMBT_BLOCK)
231*4882a593Smuzhiyun xchk_fblock_xref_set_corrupt(info->sc, info->whichfork,
232*4882a593Smuzhiyun irec->br_startoff);
233*4882a593Smuzhiyun }
234*4882a593Smuzhiyun
235*4882a593Smuzhiyun /* Cross-reference a single rtdev extent record. */
236*4882a593Smuzhiyun STATIC void
xchk_bmap_rt_iextent_xref(struct xfs_inode * ip,struct xchk_bmap_info * info,struct xfs_bmbt_irec * irec)237*4882a593Smuzhiyun xchk_bmap_rt_iextent_xref(
238*4882a593Smuzhiyun struct xfs_inode *ip,
239*4882a593Smuzhiyun struct xchk_bmap_info *info,
240*4882a593Smuzhiyun struct xfs_bmbt_irec *irec)
241*4882a593Smuzhiyun {
242*4882a593Smuzhiyun xchk_xref_is_used_rt_space(info->sc, irec->br_startblock,
243*4882a593Smuzhiyun irec->br_blockcount);
244*4882a593Smuzhiyun }
245*4882a593Smuzhiyun
246*4882a593Smuzhiyun /* Cross-reference a single datadev extent record. */
247*4882a593Smuzhiyun STATIC void
xchk_bmap_iextent_xref(struct xfs_inode * ip,struct xchk_bmap_info * info,struct xfs_bmbt_irec * irec)248*4882a593Smuzhiyun xchk_bmap_iextent_xref(
249*4882a593Smuzhiyun struct xfs_inode *ip,
250*4882a593Smuzhiyun struct xchk_bmap_info *info,
251*4882a593Smuzhiyun struct xfs_bmbt_irec *irec)
252*4882a593Smuzhiyun {
253*4882a593Smuzhiyun struct xfs_mount *mp = info->sc->mp;
254*4882a593Smuzhiyun xfs_agnumber_t agno;
255*4882a593Smuzhiyun xfs_agblock_t agbno;
256*4882a593Smuzhiyun xfs_extlen_t len;
257*4882a593Smuzhiyun int error;
258*4882a593Smuzhiyun
259*4882a593Smuzhiyun agno = XFS_FSB_TO_AGNO(mp, irec->br_startblock);
260*4882a593Smuzhiyun agbno = XFS_FSB_TO_AGBNO(mp, irec->br_startblock);
261*4882a593Smuzhiyun len = irec->br_blockcount;
262*4882a593Smuzhiyun
263*4882a593Smuzhiyun error = xchk_ag_init(info->sc, agno, &info->sc->sa);
264*4882a593Smuzhiyun if (!xchk_fblock_process_error(info->sc, info->whichfork,
265*4882a593Smuzhiyun irec->br_startoff, &error))
266*4882a593Smuzhiyun return;
267*4882a593Smuzhiyun
268*4882a593Smuzhiyun xchk_xref_is_used_space(info->sc, agbno, len);
269*4882a593Smuzhiyun xchk_xref_is_not_inode_chunk(info->sc, agbno, len);
270*4882a593Smuzhiyun xchk_bmap_xref_rmap(info, irec, agbno);
271*4882a593Smuzhiyun switch (info->whichfork) {
272*4882a593Smuzhiyun case XFS_DATA_FORK:
273*4882a593Smuzhiyun if (xfs_is_reflink_inode(info->sc->ip))
274*4882a593Smuzhiyun break;
275*4882a593Smuzhiyun /* fall through */
276*4882a593Smuzhiyun case XFS_ATTR_FORK:
277*4882a593Smuzhiyun xchk_xref_is_not_shared(info->sc, agbno,
278*4882a593Smuzhiyun irec->br_blockcount);
279*4882a593Smuzhiyun break;
280*4882a593Smuzhiyun case XFS_COW_FORK:
281*4882a593Smuzhiyun xchk_xref_is_cow_staging(info->sc, agbno,
282*4882a593Smuzhiyun irec->br_blockcount);
283*4882a593Smuzhiyun break;
284*4882a593Smuzhiyun }
285*4882a593Smuzhiyun
286*4882a593Smuzhiyun xchk_ag_free(info->sc, &info->sc->sa);
287*4882a593Smuzhiyun }
288*4882a593Smuzhiyun
289*4882a593Smuzhiyun /*
290*4882a593Smuzhiyun * Directories and attr forks should never have blocks that can't be addressed
291*4882a593Smuzhiyun * by a xfs_dablk_t.
292*4882a593Smuzhiyun */
293*4882a593Smuzhiyun STATIC void
xchk_bmap_dirattr_extent(struct xfs_inode * ip,struct xchk_bmap_info * info,struct xfs_bmbt_irec * irec)294*4882a593Smuzhiyun xchk_bmap_dirattr_extent(
295*4882a593Smuzhiyun struct xfs_inode *ip,
296*4882a593Smuzhiyun struct xchk_bmap_info *info,
297*4882a593Smuzhiyun struct xfs_bmbt_irec *irec)
298*4882a593Smuzhiyun {
299*4882a593Smuzhiyun struct xfs_mount *mp = ip->i_mount;
300*4882a593Smuzhiyun xfs_fileoff_t off;
301*4882a593Smuzhiyun
302*4882a593Smuzhiyun if (!S_ISDIR(VFS_I(ip)->i_mode) && info->whichfork != XFS_ATTR_FORK)
303*4882a593Smuzhiyun return;
304*4882a593Smuzhiyun
305*4882a593Smuzhiyun if (!xfs_verify_dablk(mp, irec->br_startoff))
306*4882a593Smuzhiyun xchk_fblock_set_corrupt(info->sc, info->whichfork,
307*4882a593Smuzhiyun irec->br_startoff);
308*4882a593Smuzhiyun
309*4882a593Smuzhiyun off = irec->br_startoff + irec->br_blockcount - 1;
310*4882a593Smuzhiyun if (!xfs_verify_dablk(mp, off))
311*4882a593Smuzhiyun xchk_fblock_set_corrupt(info->sc, info->whichfork, off);
312*4882a593Smuzhiyun }
313*4882a593Smuzhiyun
314*4882a593Smuzhiyun /* Scrub a single extent record. */
315*4882a593Smuzhiyun STATIC int
xchk_bmap_iextent(struct xfs_inode * ip,struct xchk_bmap_info * info,struct xfs_bmbt_irec * irec)316*4882a593Smuzhiyun xchk_bmap_iextent(
317*4882a593Smuzhiyun struct xfs_inode *ip,
318*4882a593Smuzhiyun struct xchk_bmap_info *info,
319*4882a593Smuzhiyun struct xfs_bmbt_irec *irec)
320*4882a593Smuzhiyun {
321*4882a593Smuzhiyun struct xfs_mount *mp = info->sc->mp;
322*4882a593Smuzhiyun xfs_filblks_t end;
323*4882a593Smuzhiyun int error = 0;
324*4882a593Smuzhiyun
325*4882a593Smuzhiyun /*
326*4882a593Smuzhiyun * Check for out-of-order extents. This record could have come
327*4882a593Smuzhiyun * from the incore list, for which there is no ordering check.
328*4882a593Smuzhiyun */
329*4882a593Smuzhiyun if (irec->br_startoff < info->lastoff)
330*4882a593Smuzhiyun xchk_fblock_set_corrupt(info->sc, info->whichfork,
331*4882a593Smuzhiyun irec->br_startoff);
332*4882a593Smuzhiyun
333*4882a593Smuzhiyun xchk_bmap_dirattr_extent(ip, info, irec);
334*4882a593Smuzhiyun
335*4882a593Smuzhiyun /* There should never be a "hole" extent in either extent list. */
336*4882a593Smuzhiyun if (irec->br_startblock == HOLESTARTBLOCK)
337*4882a593Smuzhiyun xchk_fblock_set_corrupt(info->sc, info->whichfork,
338*4882a593Smuzhiyun irec->br_startoff);
339*4882a593Smuzhiyun
340*4882a593Smuzhiyun /*
341*4882a593Smuzhiyun * Check for delalloc extents. We never iterate the ones in the
342*4882a593Smuzhiyun * in-core extent scan, and we should never see these in the bmbt.
343*4882a593Smuzhiyun */
344*4882a593Smuzhiyun if (isnullstartblock(irec->br_startblock))
345*4882a593Smuzhiyun xchk_fblock_set_corrupt(info->sc, info->whichfork,
346*4882a593Smuzhiyun irec->br_startoff);
347*4882a593Smuzhiyun
348*4882a593Smuzhiyun /* Make sure the extent points to a valid place. */
349*4882a593Smuzhiyun if (irec->br_blockcount > MAXEXTLEN)
350*4882a593Smuzhiyun xchk_fblock_set_corrupt(info->sc, info->whichfork,
351*4882a593Smuzhiyun irec->br_startoff);
352*4882a593Smuzhiyun if (irec->br_startblock + irec->br_blockcount <= irec->br_startblock)
353*4882a593Smuzhiyun xchk_fblock_set_corrupt(info->sc, info->whichfork,
354*4882a593Smuzhiyun irec->br_startoff);
355*4882a593Smuzhiyun end = irec->br_startblock + irec->br_blockcount - 1;
356*4882a593Smuzhiyun if (info->is_rt &&
357*4882a593Smuzhiyun (!xfs_verify_rtbno(mp, irec->br_startblock) ||
358*4882a593Smuzhiyun !xfs_verify_rtbno(mp, end)))
359*4882a593Smuzhiyun xchk_fblock_set_corrupt(info->sc, info->whichfork,
360*4882a593Smuzhiyun irec->br_startoff);
361*4882a593Smuzhiyun if (!info->is_rt &&
362*4882a593Smuzhiyun (!xfs_verify_fsbno(mp, irec->br_startblock) ||
363*4882a593Smuzhiyun !xfs_verify_fsbno(mp, end) ||
364*4882a593Smuzhiyun XFS_FSB_TO_AGNO(mp, irec->br_startblock) !=
365*4882a593Smuzhiyun XFS_FSB_TO_AGNO(mp, end)))
366*4882a593Smuzhiyun xchk_fblock_set_corrupt(info->sc, info->whichfork,
367*4882a593Smuzhiyun irec->br_startoff);
368*4882a593Smuzhiyun
369*4882a593Smuzhiyun /* We don't allow unwritten extents on attr forks. */
370*4882a593Smuzhiyun if (irec->br_state == XFS_EXT_UNWRITTEN &&
371*4882a593Smuzhiyun info->whichfork == XFS_ATTR_FORK)
372*4882a593Smuzhiyun xchk_fblock_set_corrupt(info->sc, info->whichfork,
373*4882a593Smuzhiyun irec->br_startoff);
374*4882a593Smuzhiyun
375*4882a593Smuzhiyun if (info->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
376*4882a593Smuzhiyun return 0;
377*4882a593Smuzhiyun
378*4882a593Smuzhiyun if (info->is_rt)
379*4882a593Smuzhiyun xchk_bmap_rt_iextent_xref(ip, info, irec);
380*4882a593Smuzhiyun else
381*4882a593Smuzhiyun xchk_bmap_iextent_xref(ip, info, irec);
382*4882a593Smuzhiyun
383*4882a593Smuzhiyun info->lastoff = irec->br_startoff + irec->br_blockcount;
384*4882a593Smuzhiyun return error;
385*4882a593Smuzhiyun }
386*4882a593Smuzhiyun
387*4882a593Smuzhiyun /* Scrub a bmbt record. */
388*4882a593Smuzhiyun STATIC int
xchk_bmapbt_rec(struct xchk_btree * bs,union xfs_btree_rec * rec)389*4882a593Smuzhiyun xchk_bmapbt_rec(
390*4882a593Smuzhiyun struct xchk_btree *bs,
391*4882a593Smuzhiyun union xfs_btree_rec *rec)
392*4882a593Smuzhiyun {
393*4882a593Smuzhiyun struct xfs_bmbt_irec irec;
394*4882a593Smuzhiyun struct xfs_bmbt_irec iext_irec;
395*4882a593Smuzhiyun struct xfs_iext_cursor icur;
396*4882a593Smuzhiyun struct xchk_bmap_info *info = bs->private;
397*4882a593Smuzhiyun struct xfs_inode *ip = bs->cur->bc_ino.ip;
398*4882a593Smuzhiyun struct xfs_buf *bp = NULL;
399*4882a593Smuzhiyun struct xfs_btree_block *block;
400*4882a593Smuzhiyun struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, info->whichfork);
401*4882a593Smuzhiyun uint64_t owner;
402*4882a593Smuzhiyun int i;
403*4882a593Smuzhiyun
404*4882a593Smuzhiyun /*
405*4882a593Smuzhiyun * Check the owners of the btree blocks up to the level below
406*4882a593Smuzhiyun * the root since the verifiers don't do that.
407*4882a593Smuzhiyun */
408*4882a593Smuzhiyun if (xfs_sb_version_hascrc(&bs->cur->bc_mp->m_sb) &&
409*4882a593Smuzhiyun bs->cur->bc_ptrs[0] == 1) {
410*4882a593Smuzhiyun for (i = 0; i < bs->cur->bc_nlevels - 1; i++) {
411*4882a593Smuzhiyun block = xfs_btree_get_block(bs->cur, i, &bp);
412*4882a593Smuzhiyun owner = be64_to_cpu(block->bb_u.l.bb_owner);
413*4882a593Smuzhiyun if (owner != ip->i_ino)
414*4882a593Smuzhiyun xchk_fblock_set_corrupt(bs->sc,
415*4882a593Smuzhiyun info->whichfork, 0);
416*4882a593Smuzhiyun }
417*4882a593Smuzhiyun }
418*4882a593Smuzhiyun
419*4882a593Smuzhiyun /*
420*4882a593Smuzhiyun * Check that the incore extent tree contains an extent that matches
421*4882a593Smuzhiyun * this one exactly. We validate those cached bmaps later, so we don't
422*4882a593Smuzhiyun * need to check them here. If the incore extent tree was just loaded
423*4882a593Smuzhiyun * from disk by the scrubber, we assume that its contents match what's
424*4882a593Smuzhiyun * on disk (we still hold the ILOCK) and skip the equivalence check.
425*4882a593Smuzhiyun */
426*4882a593Smuzhiyun if (!info->was_loaded)
427*4882a593Smuzhiyun return 0;
428*4882a593Smuzhiyun
429*4882a593Smuzhiyun xfs_bmbt_disk_get_all(&rec->bmbt, &irec);
430*4882a593Smuzhiyun if (!xfs_iext_lookup_extent(ip, ifp, irec.br_startoff, &icur,
431*4882a593Smuzhiyun &iext_irec) ||
432*4882a593Smuzhiyun irec.br_startoff != iext_irec.br_startoff ||
433*4882a593Smuzhiyun irec.br_startblock != iext_irec.br_startblock ||
434*4882a593Smuzhiyun irec.br_blockcount != iext_irec.br_blockcount ||
435*4882a593Smuzhiyun irec.br_state != iext_irec.br_state)
436*4882a593Smuzhiyun xchk_fblock_set_corrupt(bs->sc, info->whichfork,
437*4882a593Smuzhiyun irec.br_startoff);
438*4882a593Smuzhiyun return 0;
439*4882a593Smuzhiyun }
440*4882a593Smuzhiyun
441*4882a593Smuzhiyun /* Scan the btree records. */
442*4882a593Smuzhiyun STATIC int
xchk_bmap_btree(struct xfs_scrub * sc,int whichfork,struct xchk_bmap_info * info)443*4882a593Smuzhiyun xchk_bmap_btree(
444*4882a593Smuzhiyun struct xfs_scrub *sc,
445*4882a593Smuzhiyun int whichfork,
446*4882a593Smuzhiyun struct xchk_bmap_info *info)
447*4882a593Smuzhiyun {
448*4882a593Smuzhiyun struct xfs_owner_info oinfo;
449*4882a593Smuzhiyun struct xfs_ifork *ifp = XFS_IFORK_PTR(sc->ip, whichfork);
450*4882a593Smuzhiyun struct xfs_mount *mp = sc->mp;
451*4882a593Smuzhiyun struct xfs_inode *ip = sc->ip;
452*4882a593Smuzhiyun struct xfs_btree_cur *cur;
453*4882a593Smuzhiyun int error;
454*4882a593Smuzhiyun
455*4882a593Smuzhiyun /* Load the incore bmap cache if it's not loaded. */
456*4882a593Smuzhiyun info->was_loaded = ifp->if_flags & XFS_IFEXTENTS;
457*4882a593Smuzhiyun if (!info->was_loaded) {
458*4882a593Smuzhiyun error = xfs_iread_extents(sc->tp, ip, whichfork);
459*4882a593Smuzhiyun if (!xchk_fblock_process_error(sc, whichfork, 0, &error))
460*4882a593Smuzhiyun goto out;
461*4882a593Smuzhiyun }
462*4882a593Smuzhiyun
463*4882a593Smuzhiyun /* Check the btree structure. */
464*4882a593Smuzhiyun cur = xfs_bmbt_init_cursor(mp, sc->tp, ip, whichfork);
465*4882a593Smuzhiyun xfs_rmap_ino_bmbt_owner(&oinfo, ip->i_ino, whichfork);
466*4882a593Smuzhiyun error = xchk_btree(sc, cur, xchk_bmapbt_rec, &oinfo, info);
467*4882a593Smuzhiyun xfs_btree_del_cursor(cur, error);
468*4882a593Smuzhiyun out:
469*4882a593Smuzhiyun return error;
470*4882a593Smuzhiyun }
471*4882a593Smuzhiyun
472*4882a593Smuzhiyun struct xchk_bmap_check_rmap_info {
473*4882a593Smuzhiyun struct xfs_scrub *sc;
474*4882a593Smuzhiyun int whichfork;
475*4882a593Smuzhiyun struct xfs_iext_cursor icur;
476*4882a593Smuzhiyun };
477*4882a593Smuzhiyun
478*4882a593Smuzhiyun /* Can we find bmaps that fit this rmap? */
479*4882a593Smuzhiyun STATIC int
xchk_bmap_check_rmap(struct xfs_btree_cur * cur,struct xfs_rmap_irec * rec,void * priv)480*4882a593Smuzhiyun xchk_bmap_check_rmap(
481*4882a593Smuzhiyun struct xfs_btree_cur *cur,
482*4882a593Smuzhiyun struct xfs_rmap_irec *rec,
483*4882a593Smuzhiyun void *priv)
484*4882a593Smuzhiyun {
485*4882a593Smuzhiyun struct xfs_bmbt_irec irec;
486*4882a593Smuzhiyun struct xchk_bmap_check_rmap_info *sbcri = priv;
487*4882a593Smuzhiyun struct xfs_ifork *ifp;
488*4882a593Smuzhiyun struct xfs_scrub *sc = sbcri->sc;
489*4882a593Smuzhiyun bool have_map;
490*4882a593Smuzhiyun
491*4882a593Smuzhiyun /* Is this even the right fork? */
492*4882a593Smuzhiyun if (rec->rm_owner != sc->ip->i_ino)
493*4882a593Smuzhiyun return 0;
494*4882a593Smuzhiyun if ((sbcri->whichfork == XFS_ATTR_FORK) ^
495*4882a593Smuzhiyun !!(rec->rm_flags & XFS_RMAP_ATTR_FORK))
496*4882a593Smuzhiyun return 0;
497*4882a593Smuzhiyun if (rec->rm_flags & XFS_RMAP_BMBT_BLOCK)
498*4882a593Smuzhiyun return 0;
499*4882a593Smuzhiyun
500*4882a593Smuzhiyun /* Now look up the bmbt record. */
501*4882a593Smuzhiyun ifp = XFS_IFORK_PTR(sc->ip, sbcri->whichfork);
502*4882a593Smuzhiyun if (!ifp) {
503*4882a593Smuzhiyun xchk_fblock_set_corrupt(sc, sbcri->whichfork,
504*4882a593Smuzhiyun rec->rm_offset);
505*4882a593Smuzhiyun goto out;
506*4882a593Smuzhiyun }
507*4882a593Smuzhiyun have_map = xfs_iext_lookup_extent(sc->ip, ifp, rec->rm_offset,
508*4882a593Smuzhiyun &sbcri->icur, &irec);
509*4882a593Smuzhiyun if (!have_map)
510*4882a593Smuzhiyun xchk_fblock_set_corrupt(sc, sbcri->whichfork,
511*4882a593Smuzhiyun rec->rm_offset);
512*4882a593Smuzhiyun /*
513*4882a593Smuzhiyun * bmap extent record lengths are constrained to 2^21 blocks in length
514*4882a593Smuzhiyun * because of space constraints in the on-disk metadata structure.
515*4882a593Smuzhiyun * However, rmap extent record lengths are constrained only by AG
516*4882a593Smuzhiyun * length, so we have to loop through the bmbt to make sure that the
517*4882a593Smuzhiyun * entire rmap is covered by bmbt records.
518*4882a593Smuzhiyun */
519*4882a593Smuzhiyun while (have_map) {
520*4882a593Smuzhiyun if (irec.br_startoff != rec->rm_offset)
521*4882a593Smuzhiyun xchk_fblock_set_corrupt(sc, sbcri->whichfork,
522*4882a593Smuzhiyun rec->rm_offset);
523*4882a593Smuzhiyun if (irec.br_startblock != XFS_AGB_TO_FSB(sc->mp,
524*4882a593Smuzhiyun cur->bc_ag.agno, rec->rm_startblock))
525*4882a593Smuzhiyun xchk_fblock_set_corrupt(sc, sbcri->whichfork,
526*4882a593Smuzhiyun rec->rm_offset);
527*4882a593Smuzhiyun if (irec.br_blockcount > rec->rm_blockcount)
528*4882a593Smuzhiyun xchk_fblock_set_corrupt(sc, sbcri->whichfork,
529*4882a593Smuzhiyun rec->rm_offset);
530*4882a593Smuzhiyun if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
531*4882a593Smuzhiyun break;
532*4882a593Smuzhiyun rec->rm_startblock += irec.br_blockcount;
533*4882a593Smuzhiyun rec->rm_offset += irec.br_blockcount;
534*4882a593Smuzhiyun rec->rm_blockcount -= irec.br_blockcount;
535*4882a593Smuzhiyun if (rec->rm_blockcount == 0)
536*4882a593Smuzhiyun break;
537*4882a593Smuzhiyun have_map = xfs_iext_next_extent(ifp, &sbcri->icur, &irec);
538*4882a593Smuzhiyun if (!have_map)
539*4882a593Smuzhiyun xchk_fblock_set_corrupt(sc, sbcri->whichfork,
540*4882a593Smuzhiyun rec->rm_offset);
541*4882a593Smuzhiyun }
542*4882a593Smuzhiyun
543*4882a593Smuzhiyun out:
544*4882a593Smuzhiyun if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
545*4882a593Smuzhiyun return -ECANCELED;
546*4882a593Smuzhiyun return 0;
547*4882a593Smuzhiyun }
548*4882a593Smuzhiyun
549*4882a593Smuzhiyun /* Make sure each rmap has a corresponding bmbt entry. */
550*4882a593Smuzhiyun STATIC int
xchk_bmap_check_ag_rmaps(struct xfs_scrub * sc,int whichfork,xfs_agnumber_t agno)551*4882a593Smuzhiyun xchk_bmap_check_ag_rmaps(
552*4882a593Smuzhiyun struct xfs_scrub *sc,
553*4882a593Smuzhiyun int whichfork,
554*4882a593Smuzhiyun xfs_agnumber_t agno)
555*4882a593Smuzhiyun {
556*4882a593Smuzhiyun struct xchk_bmap_check_rmap_info sbcri;
557*4882a593Smuzhiyun struct xfs_btree_cur *cur;
558*4882a593Smuzhiyun struct xfs_buf *agf;
559*4882a593Smuzhiyun int error;
560*4882a593Smuzhiyun
561*4882a593Smuzhiyun error = xfs_alloc_read_agf(sc->mp, sc->tp, agno, 0, &agf);
562*4882a593Smuzhiyun if (error)
563*4882a593Smuzhiyun return error;
564*4882a593Smuzhiyun
565*4882a593Smuzhiyun cur = xfs_rmapbt_init_cursor(sc->mp, sc->tp, agf, agno);
566*4882a593Smuzhiyun if (!cur) {
567*4882a593Smuzhiyun error = -ENOMEM;
568*4882a593Smuzhiyun goto out_agf;
569*4882a593Smuzhiyun }
570*4882a593Smuzhiyun
571*4882a593Smuzhiyun sbcri.sc = sc;
572*4882a593Smuzhiyun sbcri.whichfork = whichfork;
573*4882a593Smuzhiyun error = xfs_rmap_query_all(cur, xchk_bmap_check_rmap, &sbcri);
574*4882a593Smuzhiyun if (error == -ECANCELED)
575*4882a593Smuzhiyun error = 0;
576*4882a593Smuzhiyun
577*4882a593Smuzhiyun xfs_btree_del_cursor(cur, error);
578*4882a593Smuzhiyun out_agf:
579*4882a593Smuzhiyun xfs_trans_brelse(sc->tp, agf);
580*4882a593Smuzhiyun return error;
581*4882a593Smuzhiyun }
582*4882a593Smuzhiyun
583*4882a593Smuzhiyun /* Make sure each rmap has a corresponding bmbt entry. */
584*4882a593Smuzhiyun STATIC int
xchk_bmap_check_rmaps(struct xfs_scrub * sc,int whichfork)585*4882a593Smuzhiyun xchk_bmap_check_rmaps(
586*4882a593Smuzhiyun struct xfs_scrub *sc,
587*4882a593Smuzhiyun int whichfork)
588*4882a593Smuzhiyun {
589*4882a593Smuzhiyun struct xfs_ifork *ifp = XFS_IFORK_PTR(sc->ip, whichfork);
590*4882a593Smuzhiyun xfs_agnumber_t agno;
591*4882a593Smuzhiyun bool zero_size;
592*4882a593Smuzhiyun int error;
593*4882a593Smuzhiyun
594*4882a593Smuzhiyun if (!xfs_sb_version_hasrmapbt(&sc->mp->m_sb) ||
595*4882a593Smuzhiyun whichfork == XFS_COW_FORK ||
596*4882a593Smuzhiyun (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT))
597*4882a593Smuzhiyun return 0;
598*4882a593Smuzhiyun
599*4882a593Smuzhiyun /* Don't support realtime rmap checks yet. */
600*4882a593Smuzhiyun if (XFS_IS_REALTIME_INODE(sc->ip) && whichfork == XFS_DATA_FORK)
601*4882a593Smuzhiyun return 0;
602*4882a593Smuzhiyun
603*4882a593Smuzhiyun ASSERT(XFS_IFORK_PTR(sc->ip, whichfork) != NULL);
604*4882a593Smuzhiyun
605*4882a593Smuzhiyun /*
606*4882a593Smuzhiyun * Only do this for complex maps that are in btree format, or for
607*4882a593Smuzhiyun * situations where we would seem to have a size but zero extents.
608*4882a593Smuzhiyun * The inode repair code can zap broken iforks, which means we have
609*4882a593Smuzhiyun * to flag this bmap as corrupt if there are rmaps that need to be
610*4882a593Smuzhiyun * reattached.
611*4882a593Smuzhiyun */
612*4882a593Smuzhiyun
613*4882a593Smuzhiyun if (whichfork == XFS_DATA_FORK)
614*4882a593Smuzhiyun zero_size = i_size_read(VFS_I(sc->ip)) == 0;
615*4882a593Smuzhiyun else
616*4882a593Smuzhiyun zero_size = false;
617*4882a593Smuzhiyun
618*4882a593Smuzhiyun if (ifp->if_format != XFS_DINODE_FMT_BTREE &&
619*4882a593Smuzhiyun (zero_size || ifp->if_nextents > 0))
620*4882a593Smuzhiyun return 0;
621*4882a593Smuzhiyun
622*4882a593Smuzhiyun for (agno = 0; agno < sc->mp->m_sb.sb_agcount; agno++) {
623*4882a593Smuzhiyun error = xchk_bmap_check_ag_rmaps(sc, whichfork, agno);
624*4882a593Smuzhiyun if (error)
625*4882a593Smuzhiyun return error;
626*4882a593Smuzhiyun if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
627*4882a593Smuzhiyun break;
628*4882a593Smuzhiyun }
629*4882a593Smuzhiyun
630*4882a593Smuzhiyun return 0;
631*4882a593Smuzhiyun }
632*4882a593Smuzhiyun
633*4882a593Smuzhiyun /*
634*4882a593Smuzhiyun * Scrub an inode fork's block mappings.
635*4882a593Smuzhiyun *
636*4882a593Smuzhiyun * First we scan every record in every btree block, if applicable.
637*4882a593Smuzhiyun * Then we unconditionally scan the incore extent cache.
638*4882a593Smuzhiyun */
639*4882a593Smuzhiyun STATIC int
xchk_bmap(struct xfs_scrub * sc,int whichfork)640*4882a593Smuzhiyun xchk_bmap(
641*4882a593Smuzhiyun struct xfs_scrub *sc,
642*4882a593Smuzhiyun int whichfork)
643*4882a593Smuzhiyun {
644*4882a593Smuzhiyun struct xfs_bmbt_irec irec;
645*4882a593Smuzhiyun struct xchk_bmap_info info = { NULL };
646*4882a593Smuzhiyun struct xfs_mount *mp = sc->mp;
647*4882a593Smuzhiyun struct xfs_inode *ip = sc->ip;
648*4882a593Smuzhiyun struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
649*4882a593Smuzhiyun xfs_fileoff_t endoff;
650*4882a593Smuzhiyun struct xfs_iext_cursor icur;
651*4882a593Smuzhiyun int error = 0;
652*4882a593Smuzhiyun
653*4882a593Smuzhiyun /* Non-existent forks can be ignored. */
654*4882a593Smuzhiyun if (!ifp)
655*4882a593Smuzhiyun goto out;
656*4882a593Smuzhiyun
657*4882a593Smuzhiyun info.is_rt = whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip);
658*4882a593Smuzhiyun info.whichfork = whichfork;
659*4882a593Smuzhiyun info.is_shared = whichfork == XFS_DATA_FORK && xfs_is_reflink_inode(ip);
660*4882a593Smuzhiyun info.sc = sc;
661*4882a593Smuzhiyun
662*4882a593Smuzhiyun switch (whichfork) {
663*4882a593Smuzhiyun case XFS_COW_FORK:
664*4882a593Smuzhiyun /* No CoW forks on non-reflink inodes/filesystems. */
665*4882a593Smuzhiyun if (!xfs_is_reflink_inode(ip)) {
666*4882a593Smuzhiyun xchk_ino_set_corrupt(sc, sc->ip->i_ino);
667*4882a593Smuzhiyun goto out;
668*4882a593Smuzhiyun }
669*4882a593Smuzhiyun break;
670*4882a593Smuzhiyun case XFS_ATTR_FORK:
671*4882a593Smuzhiyun if (!xfs_sb_version_hasattr(&mp->m_sb) &&
672*4882a593Smuzhiyun !xfs_sb_version_hasattr2(&mp->m_sb))
673*4882a593Smuzhiyun xchk_ino_set_corrupt(sc, sc->ip->i_ino);
674*4882a593Smuzhiyun break;
675*4882a593Smuzhiyun default:
676*4882a593Smuzhiyun ASSERT(whichfork == XFS_DATA_FORK);
677*4882a593Smuzhiyun break;
678*4882a593Smuzhiyun }
679*4882a593Smuzhiyun
680*4882a593Smuzhiyun /* Check the fork values */
681*4882a593Smuzhiyun switch (ifp->if_format) {
682*4882a593Smuzhiyun case XFS_DINODE_FMT_UUID:
683*4882a593Smuzhiyun case XFS_DINODE_FMT_DEV:
684*4882a593Smuzhiyun case XFS_DINODE_FMT_LOCAL:
685*4882a593Smuzhiyun /* No mappings to check. */
686*4882a593Smuzhiyun goto out;
687*4882a593Smuzhiyun case XFS_DINODE_FMT_EXTENTS:
688*4882a593Smuzhiyun if (!(ifp->if_flags & XFS_IFEXTENTS)) {
689*4882a593Smuzhiyun xchk_fblock_set_corrupt(sc, whichfork, 0);
690*4882a593Smuzhiyun goto out;
691*4882a593Smuzhiyun }
692*4882a593Smuzhiyun break;
693*4882a593Smuzhiyun case XFS_DINODE_FMT_BTREE:
694*4882a593Smuzhiyun if (whichfork == XFS_COW_FORK) {
695*4882a593Smuzhiyun xchk_fblock_set_corrupt(sc, whichfork, 0);
696*4882a593Smuzhiyun goto out;
697*4882a593Smuzhiyun }
698*4882a593Smuzhiyun
699*4882a593Smuzhiyun error = xchk_bmap_btree(sc, whichfork, &info);
700*4882a593Smuzhiyun if (error)
701*4882a593Smuzhiyun goto out;
702*4882a593Smuzhiyun break;
703*4882a593Smuzhiyun default:
704*4882a593Smuzhiyun xchk_fblock_set_corrupt(sc, whichfork, 0);
705*4882a593Smuzhiyun goto out;
706*4882a593Smuzhiyun }
707*4882a593Smuzhiyun
708*4882a593Smuzhiyun if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
709*4882a593Smuzhiyun goto out;
710*4882a593Smuzhiyun
711*4882a593Smuzhiyun /* Find the offset of the last extent in the mapping. */
712*4882a593Smuzhiyun error = xfs_bmap_last_offset(ip, &endoff, whichfork);
713*4882a593Smuzhiyun if (!xchk_fblock_process_error(sc, whichfork, 0, &error))
714*4882a593Smuzhiyun goto out;
715*4882a593Smuzhiyun
716*4882a593Smuzhiyun /* Scrub extent records. */
717*4882a593Smuzhiyun info.lastoff = 0;
718*4882a593Smuzhiyun ifp = XFS_IFORK_PTR(ip, whichfork);
719*4882a593Smuzhiyun for_each_xfs_iext(ifp, &icur, &irec) {
720*4882a593Smuzhiyun if (xchk_should_terminate(sc, &error) ||
721*4882a593Smuzhiyun (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT))
722*4882a593Smuzhiyun goto out;
723*4882a593Smuzhiyun if (isnullstartblock(irec.br_startblock))
724*4882a593Smuzhiyun continue;
725*4882a593Smuzhiyun if (irec.br_startoff >= endoff) {
726*4882a593Smuzhiyun xchk_fblock_set_corrupt(sc, whichfork,
727*4882a593Smuzhiyun irec.br_startoff);
728*4882a593Smuzhiyun goto out;
729*4882a593Smuzhiyun }
730*4882a593Smuzhiyun error = xchk_bmap_iextent(ip, &info, &irec);
731*4882a593Smuzhiyun if (error)
732*4882a593Smuzhiyun goto out;
733*4882a593Smuzhiyun }
734*4882a593Smuzhiyun
735*4882a593Smuzhiyun error = xchk_bmap_check_rmaps(sc, whichfork);
736*4882a593Smuzhiyun if (!xchk_fblock_xref_process_error(sc, whichfork, 0, &error))
737*4882a593Smuzhiyun goto out;
738*4882a593Smuzhiyun out:
739*4882a593Smuzhiyun return error;
740*4882a593Smuzhiyun }
741*4882a593Smuzhiyun
742*4882a593Smuzhiyun /* Scrub an inode's data fork. */
743*4882a593Smuzhiyun int
xchk_bmap_data(struct xfs_scrub * sc)744*4882a593Smuzhiyun xchk_bmap_data(
745*4882a593Smuzhiyun struct xfs_scrub *sc)
746*4882a593Smuzhiyun {
747*4882a593Smuzhiyun return xchk_bmap(sc, XFS_DATA_FORK);
748*4882a593Smuzhiyun }
749*4882a593Smuzhiyun
750*4882a593Smuzhiyun /* Scrub an inode's attr fork. */
751*4882a593Smuzhiyun int
xchk_bmap_attr(struct xfs_scrub * sc)752*4882a593Smuzhiyun xchk_bmap_attr(
753*4882a593Smuzhiyun struct xfs_scrub *sc)
754*4882a593Smuzhiyun {
755*4882a593Smuzhiyun return xchk_bmap(sc, XFS_ATTR_FORK);
756*4882a593Smuzhiyun }
757*4882a593Smuzhiyun
758*4882a593Smuzhiyun /* Scrub an inode's CoW fork. */
759*4882a593Smuzhiyun int
xchk_bmap_cow(struct xfs_scrub * sc)760*4882a593Smuzhiyun xchk_bmap_cow(
761*4882a593Smuzhiyun struct xfs_scrub *sc)
762*4882a593Smuzhiyun {
763*4882a593Smuzhiyun if (!xfs_is_reflink_inode(sc->ip))
764*4882a593Smuzhiyun return -ENOENT;
765*4882a593Smuzhiyun
766*4882a593Smuzhiyun return xchk_bmap(sc, XFS_COW_FORK);
767*4882a593Smuzhiyun }
768