1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2017 Oracle. All Rights Reserved.
4 * Author: Darrick J. Wong <darrick.wong@oracle.com>
8 #include "xfs_shared.h"
9 #include "xfs_format.h"
10 #include "xfs_trans_resv.h"
11 #include "xfs_mount.h"
12 #include "xfs_log_format.h"
13 #include "xfs_inode.h"
14 #include "xfs_icache.h"
16 #include "xfs_dir2_priv.h"
17 #include "scrub/scrub.h"
18 #include "scrub/common.h"
20 /* Set us up to scrub parents. */
26 return xchk_setup_inode_contents(sc
, ip
, 0);
31 /* Look for an entry in a parent pointing to this inode. */
33 struct xchk_parent_ctx
{
34 struct dir_context dc
;
41 /* Look for a single entry in a directory pointing to an inode. */
44 struct dir_context
*dc
,
51 struct xchk_parent_ctx
*spc
;
54 spc
= container_of(dc
, struct xchk_parent_ctx
, dc
);
59 * If we're facing a fatal signal, bail out. Store the cancellation
60 * status separately because the VFS readdir code squashes error codes
61 * into short directory reads.
63 if (xchk_should_terminate(spc
->sc
, &error
))
64 spc
->cancelled
= true;
69 /* Count the number of dentries in the parent dir that point to this inode. */
71 xchk_parent_count_parent_dentries(
73 struct xfs_inode
*parent
,
76 struct xchk_parent_ctx spc
= {
77 .dc
.actor
= xchk_parent_actor
,
87 * If there are any blocks, read-ahead block 0 as we're almost
88 * certain to have the next operation be a read there. This is
89 * how we guarantee that the parent's extent map has been loaded,
92 lock_mode
= xfs_ilock_data_map_shared(parent
);
93 if (parent
->i_d
.di_nextents
> 0)
94 error
= xfs_dir3_data_readahead(parent
, 0, 0);
95 xfs_iunlock(parent
, lock_mode
);
100 * Iterate the parent dir to confirm that there is
101 * exactly one entry pointing back to the inode being
104 bufsize
= (size_t)min_t(loff_t
, XFS_READDIR_BUFSIZE
,
105 parent
->i_d
.di_size
);
108 error
= xfs_readdir(sc
->tp
, parent
, &spc
.dc
, bufsize
);
115 if (oldpos
== spc
.dc
.pos
)
125 * Given the inode number of the alleged parent of the inode being
126 * scrubbed, try to validate that the parent has exactly one directory
127 * entry pointing back to the inode being scrubbed.
130 xchk_parent_validate(
131 struct xfs_scrub
*sc
,
135 struct xfs_mount
*mp
= sc
->mp
;
136 struct xfs_inode
*dp
= NULL
;
137 xfs_nlink_t expected_nlink
;
143 if (sc
->sm
->sm_flags
& XFS_SCRUB_OFLAG_CORRUPT
)
146 /* '..' must not point to ourselves. */
147 if (sc
->ip
->i_ino
== dnum
) {
148 xchk_fblock_set_corrupt(sc
, XFS_DATA_FORK
, 0);
153 * If we're an unlinked directory, the parent /won't/ have a link
154 * to us. Otherwise, it should have one link.
156 expected_nlink
= VFS_I(sc
->ip
)->i_nlink
== 0 ? 0 : 1;
159 * Grab this parent inode. We release the inode before we
160 * cancel the scrub transaction. Since we're don't know a
161 * priori that releasing the inode won't trigger eofblocks
162 * cleanup (which allocates what would be a nested transaction)
163 * if the parent pointer erroneously points to a file, we
164 * can't use DONTCACHE here because DONTCACHE inodes can trigger
165 * immediate inactive cleanup of the inode.
167 * If _iget returns -EINVAL then the parent inode number is garbage
168 * and the directory is corrupt. If the _iget returns -EFSCORRUPTED
169 * or -EFSBADCRC then the parent is corrupt which is a cross
170 * referencing error. Any other error is an operational error.
172 error
= xfs_iget(mp
, sc
->tp
, dnum
, XFS_IGET_UNTRUSTED
, 0, &dp
);
173 if (error
== -EINVAL
) {
174 error
= -EFSCORRUPTED
;
175 xchk_fblock_process_error(sc
, XFS_DATA_FORK
, 0, &error
);
178 if (!xchk_fblock_xref_process_error(sc
, XFS_DATA_FORK
, 0, &error
))
180 if (dp
== sc
->ip
|| !S_ISDIR(VFS_I(dp
)->i_mode
)) {
181 xchk_fblock_set_corrupt(sc
, XFS_DATA_FORK
, 0);
186 * We prefer to keep the inode locked while we lock and search
187 * its alleged parent for a forward reference. If we can grab
188 * the iolock, validate the pointers and we're done. We must
189 * use nowait here to avoid an ABBA deadlock on the parent and
192 if (xfs_ilock_nowait(dp
, XFS_IOLOCK_SHARED
)) {
193 error
= xchk_parent_count_parent_dentries(sc
, dp
, &nlink
);
194 if (!xchk_fblock_xref_process_error(sc
, XFS_DATA_FORK
, 0,
197 if (nlink
!= expected_nlink
)
198 xchk_fblock_set_corrupt(sc
, XFS_DATA_FORK
, 0);
203 * The game changes if we get here. We failed to lock the parent,
204 * so we're going to try to verify both pointers while only holding
205 * one lock so as to avoid deadlocking with something that's actually
206 * trying to traverse down the directory tree.
208 xfs_iunlock(sc
->ip
, sc
->ilock_flags
);
210 error
= xchk_ilock_inverted(dp
, XFS_IOLOCK_SHARED
);
214 /* Go looking for our dentry. */
215 error
= xchk_parent_count_parent_dentries(sc
, dp
, &nlink
);
216 if (!xchk_fblock_xref_process_error(sc
, XFS_DATA_FORK
, 0, &error
))
219 /* Drop the parent lock, relock this inode. */
220 xfs_iunlock(dp
, XFS_IOLOCK_SHARED
);
221 error
= xchk_ilock_inverted(sc
->ip
, XFS_IOLOCK_EXCL
);
224 sc
->ilock_flags
= XFS_IOLOCK_EXCL
;
227 * If we're an unlinked directory, the parent /won't/ have a link
228 * to us. Otherwise, it should have one link. We have to re-set
229 * it here because we dropped the lock on sc->ip.
231 expected_nlink
= VFS_I(sc
->ip
)->i_nlink
== 0 ? 0 : 1;
233 /* Look up '..' to see if the inode changed. */
234 error
= xfs_dir_lookup(sc
->tp
, sc
->ip
, &xfs_name_dotdot
, &dnum
, NULL
);
235 if (!xchk_fblock_process_error(sc
, XFS_DATA_FORK
, 0, &error
))
238 /* Drat, parent changed. Try again! */
239 if (dnum
!= dp
->i_ino
) {
247 * '..' didn't change, so check that there was only one entry
248 * for us in the parent.
250 if (nlink
!= expected_nlink
)
251 xchk_fblock_set_corrupt(sc
, XFS_DATA_FORK
, 0);
255 xfs_iunlock(dp
, XFS_IOLOCK_SHARED
);
262 /* Scrub a parent pointer. */
265 struct xfs_scrub
*sc
)
267 struct xfs_mount
*mp
= sc
->mp
;
274 * If we're a directory, check that the '..' link points up to
275 * a directory that has one entry pointing to us.
277 if (!S_ISDIR(VFS_I(sc
->ip
)->i_mode
))
280 /* We're not a special inode, are we? */
281 if (!xfs_verify_dir_ino(mp
, sc
->ip
->i_ino
)) {
282 xchk_fblock_set_corrupt(sc
, XFS_DATA_FORK
, 0);
287 * The VFS grabs a read or write lock via i_rwsem before it reads
288 * or writes to a directory. If we've gotten this far we've
289 * already obtained IOLOCK_EXCL, which (since 4.10) is the same as
290 * getting a write lock on i_rwsem. Therefore, it is safe for us
291 * to drop the ILOCK here in order to do directory lookups.
293 sc
->ilock_flags
&= ~(XFS_ILOCK_EXCL
| XFS_MMAPLOCK_EXCL
);
294 xfs_iunlock(sc
->ip
, XFS_ILOCK_EXCL
| XFS_MMAPLOCK_EXCL
);
297 error
= xfs_dir_lookup(sc
->tp
, sc
->ip
, &xfs_name_dotdot
, &dnum
, NULL
);
298 if (!xchk_fblock_process_error(sc
, XFS_DATA_FORK
, 0, &error
))
300 if (!xfs_verify_dir_ino(mp
, dnum
)) {
301 xchk_fblock_set_corrupt(sc
, XFS_DATA_FORK
, 0);
305 /* Is this the root dir? Then '..' must point to itself. */
306 if (sc
->ip
== mp
->m_rootip
) {
307 if (sc
->ip
->i_ino
!= mp
->m_sb
.sb_rootino
||
308 sc
->ip
->i_ino
!= dnum
)
309 xchk_fblock_set_corrupt(sc
, XFS_DATA_FORK
, 0);
314 error
= xchk_parent_validate(sc
, dnum
, &try_again
);
317 } while (try_again
&& ++tries
< 20);
320 * We gave it our best shot but failed, so mark this scrub
321 * incomplete. Userspace can decide if it wants to try again.
323 if (try_again
&& tries
== 20)
324 xchk_set_incomplete(sc
);
327 * If we failed to lock the parent inode even after a retry, just mark
328 * this scrub incomplete and return.
330 if ((sc
->flags
& XCHK_TRY_HARDER
) && error
== -EDEADLOCK
) {
332 xchk_set_incomplete(sc
);