Commit | Line | Data |
---|---|---|
0b61f8a4 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
80e4e126 DW |
2 | /* |
3 | * Copyright (C) 2017 Oracle. All Rights Reserved. | |
80e4e126 | 4 | * Author: Darrick J. Wong <darrick.wong@oracle.com> |
80e4e126 DW |
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_defer.h" | |
13 | #include "xfs_btree.h" | |
14 | #include "xfs_bit.h" | |
15 | #include "xfs_log_format.h" | |
16 | #include "xfs_trans.h" | |
17 | #include "xfs_sb.h" | |
18 | #include "xfs_inode.h" | |
19 | #include "xfs_icache.h" | |
20 | #include "xfs_inode_buf.h" | |
21 | #include "xfs_inode_fork.h" | |
22 | #include "xfs_ialloc.h" | |
23 | #include "xfs_da_format.h" | |
24 | #include "xfs_reflink.h" | |
d852657c | 25 | #include "xfs_rmap.h" |
561f648a DW |
26 | #include "xfs_bmap.h" |
27 | #include "xfs_bmap_util.h" | |
80e4e126 DW |
28 | #include "scrub/xfs_scrub.h" |
29 | #include "scrub/scrub.h" | |
30 | #include "scrub/common.h" | |
2e6f2756 | 31 | #include "scrub/btree.h" |
80e4e126 DW |
32 | #include "scrub/trace.h" |
33 | ||
34 | /* | |
35 | * Grab total control of the inode metadata. It doesn't matter here if | |
36 | * the file data is still changing; exclusive access to the metadata is | |
37 | * the goal. | |
38 | */ | |
39 | int | |
c517b3aa | 40 | xchk_setup_inode( |
1d8a748a | 41 | struct xfs_scrub *sc, |
032d91f9 | 42 | struct xfs_inode *ip) |
80e4e126 | 43 | { |
032d91f9 | 44 | int error; |
80e4e126 DW |
45 | |
46 | /* | |
47 | * Try to get the inode. If the verifiers fail, we try again | |
48 | * in raw mode. | |
49 | */ | |
c517b3aa | 50 | error = xchk_get_inode(sc, ip); |
80e4e126 DW |
51 | switch (error) { |
52 | case 0: | |
53 | break; | |
54 | case -EFSCORRUPTED: | |
55 | case -EFSBADCRC: | |
c517b3aa | 56 | return xchk_trans_alloc(sc, 0); |
80e4e126 DW |
57 | default: |
58 | return error; | |
59 | } | |
60 | ||
61 | /* Got the inode, lock it and we're ready to go. */ | |
62 | sc->ilock_flags = XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL; | |
63 | xfs_ilock(sc->ip, sc->ilock_flags); | |
c517b3aa | 64 | error = xchk_trans_alloc(sc, 0); |
80e4e126 DW |
65 | if (error) |
66 | goto out; | |
67 | sc->ilock_flags |= XFS_ILOCK_EXCL; | |
68 | xfs_ilock(sc->ip, XFS_ILOCK_EXCL); | |
69 | ||
70 | out: | |
71 | /* scrub teardown will unlock and release the inode for us */ | |
72 | return error; | |
73 | } | |
74 | ||
75 | /* Inode core */ | |
76 | ||
8bb82bc1 | 77 | /* Validate di_extsize hint. */ |
80e4e126 | 78 | STATIC void |
c517b3aa | 79 | xchk_inode_extsize( |
1d8a748a | 80 | struct xfs_scrub *sc, |
032d91f9 DW |
81 | struct xfs_dinode *dip, |
82 | xfs_ino_t ino, | |
83 | uint16_t mode, | |
84 | uint16_t flags) | |
80e4e126 | 85 | { |
032d91f9 | 86 | xfs_failaddr_t fa; |
80e4e126 | 87 | |
8bb82bc1 DW |
88 | fa = xfs_inode_validate_extsize(sc->mp, be32_to_cpu(dip->di_extsize), |
89 | mode, flags); | |
90 | if (fa) | |
c517b3aa | 91 | xchk_ino_set_corrupt(sc, ino); |
80e4e126 DW |
92 | } |
93 | ||
94 | /* | |
95 | * Validate di_cowextsize hint. | |
96 | * | |
97 | * The rules are documented at xfs_ioctl_setattr_check_cowextsize(). | |
98 | * These functions must be kept in sync with each other. | |
99 | */ | |
100 | STATIC void | |
c517b3aa | 101 | xchk_inode_cowextsize( |
1d8a748a | 102 | struct xfs_scrub *sc, |
032d91f9 DW |
103 | struct xfs_dinode *dip, |
104 | xfs_ino_t ino, | |
105 | uint16_t mode, | |
106 | uint16_t flags, | |
107 | uint64_t flags2) | |
80e4e126 | 108 | { |
032d91f9 | 109 | xfs_failaddr_t fa; |
80e4e126 | 110 | |
8bb82bc1 DW |
111 | fa = xfs_inode_validate_cowextsize(sc->mp, |
112 | be32_to_cpu(dip->di_cowextsize), mode, flags, | |
113 | flags2); | |
114 | if (fa) | |
c517b3aa | 115 | xchk_ino_set_corrupt(sc, ino); |
80e4e126 DW |
116 | } |
117 | ||
118 | /* Make sure the di_flags make sense for the inode. */ | |
119 | STATIC void | |
c517b3aa | 120 | xchk_inode_flags( |
1d8a748a | 121 | struct xfs_scrub *sc, |
032d91f9 DW |
122 | struct xfs_dinode *dip, |
123 | xfs_ino_t ino, | |
124 | uint16_t mode, | |
125 | uint16_t flags) | |
80e4e126 | 126 | { |
032d91f9 | 127 | struct xfs_mount *mp = sc->mp; |
80e4e126 | 128 | |
f369a13c | 129 | /* di_flags are all taken, last bit cannot be used */ |
80e4e126 DW |
130 | if (flags & ~XFS_DIFLAG_ANY) |
131 | goto bad; | |
132 | ||
133 | /* rt flags require rt device */ | |
134 | if ((flags & (XFS_DIFLAG_REALTIME | XFS_DIFLAG_RTINHERIT)) && | |
135 | !mp->m_rtdev_targp) | |
136 | goto bad; | |
137 | ||
138 | /* new rt bitmap flag only valid for rbmino */ | |
139 | if ((flags & XFS_DIFLAG_NEWRTBM) && ino != mp->m_sb.sb_rbmino) | |
140 | goto bad; | |
141 | ||
142 | /* directory-only flags */ | |
143 | if ((flags & (XFS_DIFLAG_RTINHERIT | | |
144 | XFS_DIFLAG_EXTSZINHERIT | | |
145 | XFS_DIFLAG_PROJINHERIT | | |
146 | XFS_DIFLAG_NOSYMLINKS)) && | |
147 | !S_ISDIR(mode)) | |
148 | goto bad; | |
149 | ||
150 | /* file-only flags */ | |
151 | if ((flags & (XFS_DIFLAG_REALTIME | FS_XFLAG_EXTSIZE)) && | |
152 | !S_ISREG(mode)) | |
153 | goto bad; | |
154 | ||
155 | /* filestreams and rt make no sense */ | |
156 | if ((flags & XFS_DIFLAG_FILESTREAM) && (flags & XFS_DIFLAG_REALTIME)) | |
157 | goto bad; | |
158 | ||
159 | return; | |
160 | bad: | |
c517b3aa | 161 | xchk_ino_set_corrupt(sc, ino); |
80e4e126 DW |
162 | } |
163 | ||
164 | /* Make sure the di_flags2 make sense for the inode. */ | |
165 | STATIC void | |
c517b3aa | 166 | xchk_inode_flags2( |
1d8a748a | 167 | struct xfs_scrub *sc, |
032d91f9 DW |
168 | struct xfs_dinode *dip, |
169 | xfs_ino_t ino, | |
170 | uint16_t mode, | |
171 | uint16_t flags, | |
172 | uint64_t flags2) | |
80e4e126 | 173 | { |
032d91f9 | 174 | struct xfs_mount *mp = sc->mp; |
80e4e126 | 175 | |
f369a13c | 176 | /* Unknown di_flags2 could be from a future kernel */ |
80e4e126 | 177 | if (flags2 & ~XFS_DIFLAG2_ANY) |
f369a13c | 178 | xchk_ino_set_warning(sc, ino); |
80e4e126 DW |
179 | |
180 | /* reflink flag requires reflink feature */ | |
181 | if ((flags2 & XFS_DIFLAG2_REFLINK) && | |
182 | !xfs_sb_version_hasreflink(&mp->m_sb)) | |
183 | goto bad; | |
184 | ||
185 | /* cowextsize flag is checked w.r.t. mode separately */ | |
186 | ||
187 | /* file/dir-only flags */ | |
188 | if ((flags2 & XFS_DIFLAG2_DAX) && !(S_ISREG(mode) || S_ISDIR(mode))) | |
189 | goto bad; | |
190 | ||
191 | /* file-only flags */ | |
192 | if ((flags2 & XFS_DIFLAG2_REFLINK) && !S_ISREG(mode)) | |
193 | goto bad; | |
194 | ||
195 | /* realtime and reflink make no sense, currently */ | |
196 | if ((flags & XFS_DIFLAG_REALTIME) && (flags2 & XFS_DIFLAG2_REFLINK)) | |
197 | goto bad; | |
198 | ||
199 | /* dax and reflink make no sense, currently */ | |
200 | if ((flags2 & XFS_DIFLAG2_DAX) && (flags2 & XFS_DIFLAG2_REFLINK)) | |
201 | goto bad; | |
202 | ||
203 | return; | |
204 | bad: | |
c517b3aa | 205 | xchk_ino_set_corrupt(sc, ino); |
80e4e126 DW |
206 | } |
207 | ||
208 | /* Scrub all the ondisk inode fields. */ | |
209 | STATIC void | |
c517b3aa | 210 | xchk_dinode( |
1d8a748a | 211 | struct xfs_scrub *sc, |
032d91f9 DW |
212 | struct xfs_dinode *dip, |
213 | xfs_ino_t ino) | |
80e4e126 | 214 | { |
032d91f9 DW |
215 | struct xfs_mount *mp = sc->mp; |
216 | size_t fork_recs; | |
217 | unsigned long long isize; | |
218 | uint64_t flags2; | |
219 | uint32_t nextents; | |
220 | uint16_t flags; | |
221 | uint16_t mode; | |
80e4e126 DW |
222 | |
223 | flags = be16_to_cpu(dip->di_flags); | |
224 | if (dip->di_version >= 3) | |
225 | flags2 = be64_to_cpu(dip->di_flags2); | |
226 | else | |
227 | flags2 = 0; | |
228 | ||
229 | /* di_mode */ | |
230 | mode = be16_to_cpu(dip->di_mode); | |
3b42d385 DW |
231 | switch (mode & S_IFMT) { |
232 | case S_IFLNK: | |
233 | case S_IFREG: | |
234 | case S_IFDIR: | |
235 | case S_IFCHR: | |
236 | case S_IFBLK: | |
237 | case S_IFIFO: | |
238 | case S_IFSOCK: | |
239 | /* mode is recognized */ | |
240 | break; | |
241 | default: | |
c517b3aa | 242 | xchk_ino_set_corrupt(sc, ino); |
3b42d385 DW |
243 | break; |
244 | } | |
80e4e126 DW |
245 | |
246 | /* v1/v2 fields */ | |
247 | switch (dip->di_version) { | |
248 | case 1: | |
249 | /* | |
250 | * We autoconvert v1 inodes into v2 inodes on writeout, | |
251 | * so just mark this inode for preening. | |
252 | */ | |
c517b3aa | 253 | xchk_ino_set_preen(sc, ino); |
80e4e126 DW |
254 | break; |
255 | case 2: | |
256 | case 3: | |
257 | if (dip->di_onlink != 0) | |
c517b3aa | 258 | xchk_ino_set_corrupt(sc, ino); |
80e4e126 DW |
259 | |
260 | if (dip->di_mode == 0 && sc->ip) | |
c517b3aa | 261 | xchk_ino_set_corrupt(sc, ino); |
80e4e126 DW |
262 | |
263 | if (dip->di_projid_hi != 0 && | |
264 | !xfs_sb_version_hasprojid32bit(&mp->m_sb)) | |
c517b3aa | 265 | xchk_ino_set_corrupt(sc, ino); |
80e4e126 DW |
266 | break; |
267 | default: | |
c517b3aa | 268 | xchk_ino_set_corrupt(sc, ino); |
80e4e126 DW |
269 | return; |
270 | } | |
271 | ||
272 | /* | |
273 | * di_uid/di_gid -- -1 isn't invalid, but there's no way that | |
274 | * userspace could have created that. | |
275 | */ | |
276 | if (dip->di_uid == cpu_to_be32(-1U) || | |
277 | dip->di_gid == cpu_to_be32(-1U)) | |
c517b3aa | 278 | xchk_ino_set_warning(sc, ino); |
80e4e126 DW |
279 | |
280 | /* di_format */ | |
281 | switch (dip->di_format) { | |
282 | case XFS_DINODE_FMT_DEV: | |
283 | if (!S_ISCHR(mode) && !S_ISBLK(mode) && | |
284 | !S_ISFIFO(mode) && !S_ISSOCK(mode)) | |
c517b3aa | 285 | xchk_ino_set_corrupt(sc, ino); |
80e4e126 DW |
286 | break; |
287 | case XFS_DINODE_FMT_LOCAL: | |
288 | if (!S_ISDIR(mode) && !S_ISLNK(mode)) | |
c517b3aa | 289 | xchk_ino_set_corrupt(sc, ino); |
80e4e126 DW |
290 | break; |
291 | case XFS_DINODE_FMT_EXTENTS: | |
292 | if (!S_ISREG(mode) && !S_ISDIR(mode) && !S_ISLNK(mode)) | |
c517b3aa | 293 | xchk_ino_set_corrupt(sc, ino); |
80e4e126 DW |
294 | break; |
295 | case XFS_DINODE_FMT_BTREE: | |
296 | if (!S_ISREG(mode) && !S_ISDIR(mode)) | |
c517b3aa | 297 | xchk_ino_set_corrupt(sc, ino); |
80e4e126 DW |
298 | break; |
299 | case XFS_DINODE_FMT_UUID: | |
300 | default: | |
c517b3aa | 301 | xchk_ino_set_corrupt(sc, ino); |
80e4e126 DW |
302 | break; |
303 | } | |
304 | ||
29c1c123 DW |
305 | /* di_[amc]time.nsec */ |
306 | if (be32_to_cpu(dip->di_atime.t_nsec) >= NSEC_PER_SEC) | |
c517b3aa | 307 | xchk_ino_set_corrupt(sc, ino); |
29c1c123 | 308 | if (be32_to_cpu(dip->di_mtime.t_nsec) >= NSEC_PER_SEC) |
c517b3aa | 309 | xchk_ino_set_corrupt(sc, ino); |
29c1c123 | 310 | if (be32_to_cpu(dip->di_ctime.t_nsec) >= NSEC_PER_SEC) |
c517b3aa | 311 | xchk_ino_set_corrupt(sc, ino); |
29c1c123 | 312 | |
80e4e126 DW |
313 | /* |
314 | * di_size. xfs_dinode_verify checks for things that screw up | |
315 | * the VFS such as the upper bit being set and zero-length | |
316 | * symlinks/directories, but we can do more here. | |
317 | */ | |
318 | isize = be64_to_cpu(dip->di_size); | |
319 | if (isize & (1ULL << 63)) | |
c517b3aa | 320 | xchk_ino_set_corrupt(sc, ino); |
80e4e126 DW |
321 | |
322 | /* Devices, fifos, and sockets must have zero size */ | |
323 | if (!S_ISDIR(mode) && !S_ISREG(mode) && !S_ISLNK(mode) && isize != 0) | |
c517b3aa | 324 | xchk_ino_set_corrupt(sc, ino); |
80e4e126 DW |
325 | |
326 | /* Directories can't be larger than the data section size (32G) */ | |
327 | if (S_ISDIR(mode) && (isize == 0 || isize >= XFS_DIR2_SPACE_SIZE)) | |
c517b3aa | 328 | xchk_ino_set_corrupt(sc, ino); |
80e4e126 DW |
329 | |
330 | /* Symlinks can't be larger than SYMLINK_MAXLEN */ | |
331 | if (S_ISLNK(mode) && (isize == 0 || isize >= XFS_SYMLINK_MAXLEN)) | |
c517b3aa | 332 | xchk_ino_set_corrupt(sc, ino); |
80e4e126 DW |
333 | |
334 | /* | |
335 | * Warn if the running kernel can't handle the kinds of offsets | |
336 | * needed to deal with the file size. In other words, if the | |
337 | * pagecache can't cache all the blocks in this file due to | |
338 | * overly large offsets, flag the inode for admin review. | |
339 | */ | |
340 | if (isize >= mp->m_super->s_maxbytes) | |
c517b3aa | 341 | xchk_ino_set_warning(sc, ino); |
80e4e126 DW |
342 | |
343 | /* di_nblocks */ | |
344 | if (flags2 & XFS_DIFLAG2_REFLINK) { | |
345 | ; /* nblocks can exceed dblocks */ | |
346 | } else if (flags & XFS_DIFLAG_REALTIME) { | |
347 | /* | |
348 | * nblocks is the sum of data extents (in the rtdev), | |
349 | * attr extents (in the datadev), and both forks' bmbt | |
350 | * blocks (in the datadev). This clumsy check is the | |
351 | * best we can do without cross-referencing with the | |
352 | * inode forks. | |
353 | */ | |
354 | if (be64_to_cpu(dip->di_nblocks) >= | |
355 | mp->m_sb.sb_dblocks + mp->m_sb.sb_rblocks) | |
c517b3aa | 356 | xchk_ino_set_corrupt(sc, ino); |
80e4e126 DW |
357 | } else { |
358 | if (be64_to_cpu(dip->di_nblocks) >= mp->m_sb.sb_dblocks) | |
c517b3aa | 359 | xchk_ino_set_corrupt(sc, ino); |
80e4e126 DW |
360 | } |
361 | ||
c517b3aa | 362 | xchk_inode_flags(sc, dip, ino, mode, flags); |
80e4e126 | 363 | |
c517b3aa | 364 | xchk_inode_extsize(sc, dip, ino, mode, flags); |
80e4e126 DW |
365 | |
366 | /* di_nextents */ | |
367 | nextents = be32_to_cpu(dip->di_nextents); | |
368 | fork_recs = XFS_DFORK_DSIZE(dip, mp) / sizeof(struct xfs_bmbt_rec); | |
369 | switch (dip->di_format) { | |
370 | case XFS_DINODE_FMT_EXTENTS: | |
371 | if (nextents > fork_recs) | |
c517b3aa | 372 | xchk_ino_set_corrupt(sc, ino); |
80e4e126 DW |
373 | break; |
374 | case XFS_DINODE_FMT_BTREE: | |
375 | if (nextents <= fork_recs) | |
c517b3aa | 376 | xchk_ino_set_corrupt(sc, ino); |
80e4e126 DW |
377 | break; |
378 | default: | |
379 | if (nextents != 0) | |
c517b3aa | 380 | xchk_ino_set_corrupt(sc, ino); |
80e4e126 DW |
381 | break; |
382 | } | |
383 | ||
384 | /* di_forkoff */ | |
385 | if (XFS_DFORK_APTR(dip) >= (char *)dip + mp->m_sb.sb_inodesize) | |
c517b3aa | 386 | xchk_ino_set_corrupt(sc, ino); |
80e4e126 | 387 | if (dip->di_anextents != 0 && dip->di_forkoff == 0) |
c517b3aa | 388 | xchk_ino_set_corrupt(sc, ino); |
80e4e126 | 389 | if (dip->di_forkoff == 0 && dip->di_aformat != XFS_DINODE_FMT_EXTENTS) |
c517b3aa | 390 | xchk_ino_set_corrupt(sc, ino); |
80e4e126 DW |
391 | |
392 | /* di_aformat */ | |
393 | if (dip->di_aformat != XFS_DINODE_FMT_LOCAL && | |
394 | dip->di_aformat != XFS_DINODE_FMT_EXTENTS && | |
395 | dip->di_aformat != XFS_DINODE_FMT_BTREE) | |
c517b3aa | 396 | xchk_ino_set_corrupt(sc, ino); |
80e4e126 DW |
397 | |
398 | /* di_anextents */ | |
399 | nextents = be16_to_cpu(dip->di_anextents); | |
400 | fork_recs = XFS_DFORK_ASIZE(dip, mp) / sizeof(struct xfs_bmbt_rec); | |
401 | switch (dip->di_aformat) { | |
402 | case XFS_DINODE_FMT_EXTENTS: | |
403 | if (nextents > fork_recs) | |
c517b3aa | 404 | xchk_ino_set_corrupt(sc, ino); |
80e4e126 DW |
405 | break; |
406 | case XFS_DINODE_FMT_BTREE: | |
407 | if (nextents <= fork_recs) | |
c517b3aa | 408 | xchk_ino_set_corrupt(sc, ino); |
80e4e126 DW |
409 | break; |
410 | default: | |
411 | if (nextents != 0) | |
c517b3aa | 412 | xchk_ino_set_corrupt(sc, ino); |
80e4e126 DW |
413 | } |
414 | ||
415 | if (dip->di_version >= 3) { | |
29c1c123 | 416 | if (be32_to_cpu(dip->di_crtime.t_nsec) >= NSEC_PER_SEC) |
c517b3aa DW |
417 | xchk_ino_set_corrupt(sc, ino); |
418 | xchk_inode_flags2(sc, dip, ino, mode, flags, flags2); | |
419 | xchk_inode_cowextsize(sc, dip, ino, mode, flags, | |
80e4e126 DW |
420 | flags2); |
421 | } | |
422 | } | |
423 | ||
2e6f2756 DW |
424 | /* |
425 | * Make sure the finobt doesn't think this inode is free. | |
426 | * We don't have to check the inobt ourselves because we got the inode via | |
427 | * IGET_UNTRUSTED, which checks the inobt for us. | |
428 | */ | |
429 | static void | |
c517b3aa | 430 | xchk_inode_xref_finobt( |
032d91f9 | 431 | struct xfs_scrub *sc, |
2e6f2756 DW |
432 | xfs_ino_t ino) |
433 | { | |
434 | struct xfs_inobt_rec_incore rec; | |
435 | xfs_agino_t agino; | |
436 | int has_record; | |
437 | int error; | |
438 | ||
c517b3aa | 439 | if (!sc->sa.fino_cur || xchk_skip_xref(sc->sm)) |
2e6f2756 DW |
440 | return; |
441 | ||
442 | agino = XFS_INO_TO_AGINO(sc->mp, ino); | |
443 | ||
444 | /* | |
445 | * Try to get the finobt record. If we can't get it, then we're | |
446 | * in good shape. | |
447 | */ | |
448 | error = xfs_inobt_lookup(sc->sa.fino_cur, agino, XFS_LOOKUP_LE, | |
449 | &has_record); | |
c517b3aa | 450 | if (!xchk_should_check_xref(sc, &error, &sc->sa.fino_cur) || |
2e6f2756 DW |
451 | !has_record) |
452 | return; | |
453 | ||
454 | error = xfs_inobt_get_rec(sc->sa.fino_cur, &rec, &has_record); | |
c517b3aa | 455 | if (!xchk_should_check_xref(sc, &error, &sc->sa.fino_cur) || |
2e6f2756 DW |
456 | !has_record) |
457 | return; | |
458 | ||
459 | /* | |
460 | * Otherwise, make sure this record either doesn't cover this inode, | |
461 | * or that it does but it's marked present. | |
462 | */ | |
463 | if (rec.ir_startino > agino || | |
464 | rec.ir_startino + XFS_INODES_PER_CHUNK <= agino) | |
465 | return; | |
466 | ||
467 | if (rec.ir_free & XFS_INOBT_MASK(agino - rec.ir_startino)) | |
c517b3aa | 468 | xchk_btree_xref_set_corrupt(sc, sc->sa.fino_cur, 0); |
2e6f2756 DW |
469 | } |
470 | ||
561f648a DW |
471 | /* Cross reference the inode fields with the forks. */ |
472 | STATIC void | |
c517b3aa | 473 | xchk_inode_xref_bmap( |
1d8a748a | 474 | struct xfs_scrub *sc, |
032d91f9 | 475 | struct xfs_dinode *dip) |
561f648a | 476 | { |
032d91f9 DW |
477 | xfs_extnum_t nextents; |
478 | xfs_filblks_t count; | |
479 | xfs_filblks_t acount; | |
480 | int error; | |
561f648a | 481 | |
c517b3aa | 482 | if (xchk_skip_xref(sc->sm)) |
8389f3ff DW |
483 | return; |
484 | ||
561f648a DW |
485 | /* Walk all the extents to check nextents/naextents/nblocks. */ |
486 | error = xfs_bmap_count_blocks(sc->tp, sc->ip, XFS_DATA_FORK, | |
487 | &nextents, &count); | |
c517b3aa | 488 | if (!xchk_should_check_xref(sc, &error, NULL)) |
561f648a DW |
489 | return; |
490 | if (nextents < be32_to_cpu(dip->di_nextents)) | |
c517b3aa | 491 | xchk_ino_xref_set_corrupt(sc, sc->ip->i_ino); |
561f648a DW |
492 | |
493 | error = xfs_bmap_count_blocks(sc->tp, sc->ip, XFS_ATTR_FORK, | |
494 | &nextents, &acount); | |
c517b3aa | 495 | if (!xchk_should_check_xref(sc, &error, NULL)) |
561f648a DW |
496 | return; |
497 | if (nextents != be16_to_cpu(dip->di_anextents)) | |
c517b3aa | 498 | xchk_ino_xref_set_corrupt(sc, sc->ip->i_ino); |
561f648a DW |
499 | |
500 | /* Check nblocks against the inode. */ | |
501 | if (count + acount != be64_to_cpu(dip->di_nblocks)) | |
c517b3aa | 502 | xchk_ino_xref_set_corrupt(sc, sc->ip->i_ino); |
561f648a DW |
503 | } |
504 | ||
166d7641 DW |
505 | /* Cross-reference with the other btrees. */ |
506 | STATIC void | |
c517b3aa | 507 | xchk_inode_xref( |
1d8a748a | 508 | struct xfs_scrub *sc, |
032d91f9 DW |
509 | xfs_ino_t ino, |
510 | struct xfs_dinode *dip) | |
166d7641 | 511 | { |
032d91f9 DW |
512 | xfs_agnumber_t agno; |
513 | xfs_agblock_t agbno; | |
514 | int error; | |
52dc4b44 | 515 | |
166d7641 DW |
516 | if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT) |
517 | return; | |
52dc4b44 DW |
518 | |
519 | agno = XFS_INO_TO_AGNO(sc->mp, ino); | |
520 | agbno = XFS_INO_TO_AGBNO(sc->mp, ino); | |
521 | ||
c517b3aa DW |
522 | error = xchk_ag_init(sc, agno, &sc->sa); |
523 | if (!xchk_xref_process_error(sc, agno, agbno, &error)) | |
52dc4b44 DW |
524 | return; |
525 | ||
c517b3aa DW |
526 | xchk_xref_is_used_space(sc, agbno, 1); |
527 | xchk_inode_xref_finobt(sc, ino); | |
7280feda | 528 | xchk_xref_is_owned_by(sc, agbno, 1, &XFS_RMAP_OINFO_INODES); |
c517b3aa DW |
529 | xchk_xref_is_not_shared(sc, agbno, 1); |
530 | xchk_inode_xref_bmap(sc, dip); | |
52dc4b44 | 531 | |
c517b3aa | 532 | xchk_ag_free(sc, &sc->sa); |
166d7641 DW |
533 | } |
534 | ||
f6d5fc21 DW |
535 | /* |
536 | * If the reflink iflag disagrees with a scan for shared data fork extents, | |
537 | * either flag an error (shared extents w/ no flag) or a preen (flag set w/o | |
538 | * any shared extents). We already checked for reflink iflag set on a non | |
539 | * reflink filesystem. | |
540 | */ | |
541 | static void | |
c517b3aa | 542 | xchk_inode_check_reflink_iflag( |
1d8a748a | 543 | struct xfs_scrub *sc, |
032d91f9 | 544 | xfs_ino_t ino) |
f6d5fc21 | 545 | { |
032d91f9 DW |
546 | struct xfs_mount *mp = sc->mp; |
547 | bool has_shared; | |
548 | int error; | |
f6d5fc21 DW |
549 | |
550 | if (!xfs_sb_version_hasreflink(&mp->m_sb)) | |
551 | return; | |
552 | ||
553 | error = xfs_reflink_inode_has_shared_extents(sc->tp, sc->ip, | |
554 | &has_shared); | |
c517b3aa | 555 | if (!xchk_xref_process_error(sc, XFS_INO_TO_AGNO(mp, ino), |
f6d5fc21 DW |
556 | XFS_INO_TO_AGBNO(mp, ino), &error)) |
557 | return; | |
558 | if (xfs_is_reflink_inode(sc->ip) && !has_shared) | |
c517b3aa | 559 | xchk_ino_set_preen(sc, ino); |
f6d5fc21 | 560 | else if (!xfs_is_reflink_inode(sc->ip) && has_shared) |
c517b3aa | 561 | xchk_ino_set_corrupt(sc, ino); |
f6d5fc21 DW |
562 | } |
563 | ||
80e4e126 DW |
564 | /* Scrub an inode. */ |
565 | int | |
c517b3aa | 566 | xchk_inode( |
1d8a748a | 567 | struct xfs_scrub *sc) |
80e4e126 | 568 | { |
032d91f9 DW |
569 | struct xfs_dinode di; |
570 | int error = 0; | |
80e4e126 | 571 | |
d0018ad8 DW |
572 | /* |
573 | * If sc->ip is NULL, that means that the setup function called | |
574 | * xfs_iget to look up the inode. xfs_iget returned a EFSCORRUPTED | |
575 | * and a NULL inode, so flag the corruption error and return. | |
576 | */ | |
577 | if (!sc->ip) { | |
c517b3aa | 578 | xchk_ino_set_corrupt(sc, sc->sm->sm_ino); |
d0018ad8 | 579 | return 0; |
80e4e126 DW |
580 | } |
581 | ||
d0018ad8 DW |
582 | /* Scrub the inode core. */ |
583 | xfs_inode_to_disk(sc->ip, &di, 0); | |
c517b3aa | 584 | xchk_dinode(sc, &di, sc->ip->i_ino); |
80e4e126 DW |
585 | if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT) |
586 | goto out; | |
587 | ||
80e4e126 | 588 | /* |
f6d5fc21 DW |
589 | * Look for discrepancies between file's data blocks and the reflink |
590 | * iflag. We already checked the iflag against the file mode when | |
591 | * we scrubbed the dinode. | |
80e4e126 | 592 | */ |
f6d5fc21 | 593 | if (S_ISREG(VFS_I(sc->ip)->i_mode)) |
c517b3aa | 594 | xchk_inode_check_reflink_iflag(sc, sc->ip->i_ino); |
80e4e126 | 595 | |
c517b3aa | 596 | xchk_inode_xref(sc, sc->ip->i_ino, &di); |
80e4e126 | 597 | out: |
80e4e126 DW |
598 | return error; |
599 | } |