2 * Copyright (C) 2017 Oracle. All Rights Reserved.
4 * Author: Darrick J. Wong <darrick.wong@oracle.com>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it would be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
22 #include "xfs_shared.h"
23 #include "xfs_format.h"
24 #include "xfs_trans_resv.h"
25 #include "xfs_mount.h"
26 #include "xfs_defer.h"
27 #include "xfs_btree.h"
29 #include "xfs_log_format.h"
30 #include "xfs_trans.h"
32 #include "xfs_inode.h"
33 #include "xfs_icache.h"
35 #include "xfs_dir2_priv.h"
36 #include "xfs_ialloc.h"
37 #include "scrub/xfs_scrub.h"
38 #include "scrub/scrub.h"
39 #include "scrub/common.h"
40 #include "scrub/trace.h"
42 /* Set us up to scrub parents. */
44 xfs_scrub_setup_parent(
45 struct xfs_scrub_context
*sc
,
48 return xfs_scrub_setup_inode_contents(sc
, ip
, 0);
53 /* Look for an entry in a parent pointing to this inode. */
55 struct xfs_scrub_parent_ctx
{
56 struct dir_context dc
;
61 /* Look for a single entry in a directory pointing to an inode. */
63 xfs_scrub_parent_actor(
64 struct dir_context
*dc
,
71 struct xfs_scrub_parent_ctx
*spc
;
73 spc
= container_of(dc
, struct xfs_scrub_parent_ctx
, dc
);
79 /* Count the number of dentries in the parent dir that point to this inode. */
81 xfs_scrub_parent_count_parent_dentries(
82 struct xfs_scrub_context
*sc
,
83 struct xfs_inode
*parent
,
86 struct xfs_scrub_parent_ctx spc
= {
87 .dc
.actor
= xfs_scrub_parent_actor
,
98 * If there are any blocks, read-ahead block 0 as we're almost
99 * certain to have the next operation be a read there. This is
100 * how we guarantee that the parent's extent map has been loaded,
103 lock_mode
= xfs_ilock_data_map_shared(parent
);
104 if (parent
->i_d
.di_nextents
> 0)
105 error
= xfs_dir3_data_readahead(parent
, 0, -1);
106 xfs_iunlock(parent
, lock_mode
);
111 * Iterate the parent dir to confirm that there is
112 * exactly one entry pointing back to the inode being
115 bufsize
= (size_t)min_t(loff_t
, XFS_READDIR_BUFSIZE
,
116 parent
->i_d
.di_size
);
119 error
= xfs_readdir(sc
->tp
, parent
, &spc
.dc
, bufsize
);
122 if (oldpos
== spc
.dc
.pos
)
132 * Given the inode number of the alleged parent of the inode being
133 * scrubbed, try to validate that the parent has exactly one directory
134 * entry pointing back to the inode being scrubbed.
137 xfs_scrub_parent_validate(
138 struct xfs_scrub_context
*sc
,
142 struct xfs_mount
*mp
= sc
->mp
;
143 struct xfs_inode
*dp
= NULL
;
144 xfs_nlink_t expected_nlink
;
150 /* '..' must not point to ourselves. */
151 if (sc
->ip
->i_ino
== dnum
) {
152 xfs_scrub_fblock_set_corrupt(sc
, XFS_DATA_FORK
, 0);
157 * If we're an unlinked directory, the parent /won't/ have a link
158 * to us. Otherwise, it should have one link.
160 expected_nlink
= VFS_I(sc
->ip
)->i_nlink
== 0 ? 0 : 1;
163 * Grab this parent inode. We release the inode before we
164 * cancel the scrub transaction. Since we're don't know a
165 * priori that releasing the inode won't trigger eofblocks
166 * cleanup (which allocates what would be a nested transaction)
167 * if the parent pointer erroneously points to a file, we
168 * can't use DONTCACHE here because DONTCACHE inodes can trigger
169 * immediate inactive cleanup of the inode.
171 error
= xfs_iget(mp
, sc
->tp
, dnum
, 0, 0, &dp
);
172 if (!xfs_scrub_fblock_xref_process_error(sc
, XFS_DATA_FORK
, 0, &error
))
174 if (dp
== sc
->ip
|| !S_ISDIR(VFS_I(dp
)->i_mode
)) {
175 xfs_scrub_fblock_set_corrupt(sc
, XFS_DATA_FORK
, 0);
180 * We prefer to keep the inode locked while we lock and search
181 * its alleged parent for a forward reference. If we can grab
182 * the iolock, validate the pointers and we're done. We must
183 * use nowait here to avoid an ABBA deadlock on the parent and
186 if (xfs_ilock_nowait(dp
, XFS_IOLOCK_SHARED
)) {
187 error
= xfs_scrub_parent_count_parent_dentries(sc
, dp
, &nlink
);
188 if (!xfs_scrub_fblock_xref_process_error(sc
, XFS_DATA_FORK
, 0,
191 if (nlink
!= expected_nlink
)
192 xfs_scrub_fblock_set_corrupt(sc
, XFS_DATA_FORK
, 0);
197 * The game changes if we get here. We failed to lock the parent,
198 * so we're going to try to verify both pointers while only holding
199 * one lock so as to avoid deadlocking with something that's actually
200 * trying to traverse down the directory tree.
202 xfs_iunlock(sc
->ip
, sc
->ilock_flags
);
204 xfs_ilock(dp
, XFS_IOLOCK_SHARED
);
206 /* Go looking for our dentry. */
207 error
= xfs_scrub_parent_count_parent_dentries(sc
, dp
, &nlink
);
208 if (!xfs_scrub_fblock_xref_process_error(sc
, XFS_DATA_FORK
, 0, &error
))
211 /* Drop the parent lock, relock this inode. */
212 xfs_iunlock(dp
, XFS_IOLOCK_SHARED
);
213 sc
->ilock_flags
= XFS_IOLOCK_EXCL
;
214 xfs_ilock(sc
->ip
, sc
->ilock_flags
);
217 * If we're an unlinked directory, the parent /won't/ have a link
218 * to us. Otherwise, it should have one link. We have to re-set
219 * it here because we dropped the lock on sc->ip.
221 expected_nlink
= VFS_I(sc
->ip
)->i_nlink
== 0 ? 0 : 1;
223 /* Look up '..' to see if the inode changed. */
224 error
= xfs_dir_lookup(sc
->tp
, sc
->ip
, &xfs_name_dotdot
, &dnum
, NULL
);
225 if (!xfs_scrub_fblock_process_error(sc
, XFS_DATA_FORK
, 0, &error
))
228 /* Drat, parent changed. Try again! */
229 if (dnum
!= dp
->i_ino
) {
237 * '..' didn't change, so check that there was only one entry
238 * for us in the parent.
240 if (nlink
!= expected_nlink
)
241 xfs_scrub_fblock_set_corrupt(sc
, XFS_DATA_FORK
, 0);
245 xfs_iunlock(dp
, XFS_IOLOCK_SHARED
);
252 /* Scrub a parent pointer. */
255 struct xfs_scrub_context
*sc
)
257 struct xfs_mount
*mp
= sc
->mp
;
264 * If we're a directory, check that the '..' link points up to
265 * a directory that has one entry pointing to us.
267 if (!S_ISDIR(VFS_I(sc
->ip
)->i_mode
))
270 /* We're not a special inode, are we? */
271 if (!xfs_verify_dir_ino(mp
, sc
->ip
->i_ino
)) {
272 xfs_scrub_fblock_set_corrupt(sc
, XFS_DATA_FORK
, 0);
277 * The VFS grabs a read or write lock via i_rwsem before it reads
278 * or writes to a directory. If we've gotten this far we've
279 * already obtained IOLOCK_EXCL, which (since 4.10) is the same as
280 * getting a write lock on i_rwsem. Therefore, it is safe for us
281 * to drop the ILOCK here in order to do directory lookups.
283 sc
->ilock_flags
&= ~(XFS_ILOCK_EXCL
| XFS_MMAPLOCK_EXCL
);
284 xfs_iunlock(sc
->ip
, XFS_ILOCK_EXCL
| XFS_MMAPLOCK_EXCL
);
287 error
= xfs_dir_lookup(sc
->tp
, sc
->ip
, &xfs_name_dotdot
, &dnum
, NULL
);
288 if (!xfs_scrub_fblock_process_error(sc
, XFS_DATA_FORK
, 0, &error
))
290 if (!xfs_verify_dir_ino(mp
, dnum
)) {
291 xfs_scrub_fblock_set_corrupt(sc
, XFS_DATA_FORK
, 0);
295 /* Is this the root dir? Then '..' must point to itself. */
296 if (sc
->ip
== mp
->m_rootip
) {
297 if (sc
->ip
->i_ino
!= mp
->m_sb
.sb_rootino
||
298 sc
->ip
->i_ino
!= dnum
)
299 xfs_scrub_fblock_set_corrupt(sc
, XFS_DATA_FORK
, 0);
304 error
= xfs_scrub_parent_validate(sc
, dnum
, &try_again
);
307 } while (try_again
&& ++tries
< 20);
310 * We gave it our best shot but failed, so mark this scrub
311 * incomplete. Userspace can decide if it wants to try again.
313 if (try_again
&& tries
== 20)
314 xfs_scrub_set_incomplete(sc
);