Skip to content

Commit a03297a

Browse files
author
Darrick J. Wong
committed
xfs: manage inode DONTCACHE status at irele time
Right now, there are statements scattered all over the online fsck codebase about how we can't use XFS_IGET_DONTCACHE because of concerns about scrub's unusual practice of releasing inodes with transactions held. However, iget is the wrong place to handle this -- the DONTCACHE state doesn't matter at all until we try to *release* the inode, and here we get things wrong in multiple ways: First, if we /do/ have a transaction, we must NOT drop the inode, because the inode could have dirty pages, dropping the inode will trigger writeback, and writeback can trigger a nested transaction. Second, if the inode already had an active reference and the DONTCACHE flag set, the icache hit when scrub grabs another ref will not clear DONTCACHE. This is sort of by design, since DONTCACHE is now used to initiate cache drops so that sysadmins can change a file's access mode between pagecache and DAX. Third, if we do actually have the last active reference to the inode, we can set DONTCACHE to avoid polluting the cache. This is the /one/ case where we actually want that flag. Create an xchk_irele helper to encode all that logic and switch the online fsck code to use it. Since this now means that nearly all scrubbers use the same xfs_iget flags, we can wrap them too. Signed-off-by: Darrick J. Wong <djwong@kernel.org> Reviewed-by: Dave Chinner <dchinner@redhat.com>
1 parent 0916056 commit a03297a

5 files changed

Lines changed: 60 additions & 24 deletions

File tree

fs/xfs/scrub/common.c

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,16 @@ xchk_checkpoint_log(
718718
return 0;
719719
}
720720

721+
/* Verify that an inode is allocated ondisk, then return its cached inode. */
722+
int
723+
xchk_iget(
724+
struct xfs_scrub *sc,
725+
xfs_ino_t inum,
726+
struct xfs_inode **ipp)
727+
{
728+
return xfs_iget(sc->mp, sc->tp, inum, XFS_IGET_UNTRUSTED, 0, ipp);
729+
}
730+
721731
/*
722732
* Given an inode and the scrub control structure, grab either the
723733
* inode referenced in the control structure or the inode passed in.
@@ -743,8 +753,7 @@ xchk_get_inode(
743753
/* Look up the inode, see if the generation number matches. */
744754
if (xfs_internal_inum(mp, sc->sm->sm_ino))
745755
return -ENOENT;
746-
error = xfs_iget(mp, NULL, sc->sm->sm_ino,
747-
XFS_IGET_UNTRUSTED | XFS_IGET_DONTCACHE, 0, &ip);
756+
error = xchk_iget(sc, sc->sm->sm_ino, &ip);
748757
switch (error) {
749758
case -ENOENT:
750759
/* Inode doesn't exist, just bail out. */
@@ -768,7 +777,7 @@ xchk_get_inode(
768777
pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, sc->sm->sm_ino));
769778
if (pag) {
770779
error = xfs_imap(pag, sc->tp, sc->sm->sm_ino, &imap,
771-
XFS_IGET_UNTRUSTED | XFS_IGET_DONTCACHE);
780+
XFS_IGET_UNTRUSTED);
772781
xfs_perag_put(pag);
773782
if (error)
774783
return -ENOENT;
@@ -783,14 +792,49 @@ xchk_get_inode(
783792
return error;
784793
}
785794
if (VFS_I(ip)->i_generation != sc->sm->sm_gen) {
786-
xfs_irele(ip);
795+
xchk_irele(sc, ip);
787796
return -ENOENT;
788797
}
789798

790799
sc->ip = ip;
791800
return 0;
792801
}
793802

803+
/* Release an inode, possibly dropping it in the process. */
804+
void
805+
xchk_irele(
806+
struct xfs_scrub *sc,
807+
struct xfs_inode *ip)
808+
{
809+
if (current->journal_info != NULL) {
810+
ASSERT(current->journal_info == sc->tp);
811+
812+
/*
813+
* If we are in a transaction, we /cannot/ drop the inode
814+
* ourselves, because the VFS will trigger writeback, which
815+
* can require a transaction. Clear DONTCACHE to force the
816+
* inode to the LRU, where someone else can take care of
817+
* dropping it.
818+
*
819+
* Note that when we grabbed our reference to the inode, it
820+
* could have had an active ref and DONTCACHE set if a sysadmin
821+
* is trying to coerce a change in file access mode. icache
822+
* hits do not clear DONTCACHE, so we must do it here.
823+
*/
824+
spin_lock(&VFS_I(ip)->i_lock);
825+
VFS_I(ip)->i_state &= ~I_DONTCACHE;
826+
spin_unlock(&VFS_I(ip)->i_lock);
827+
} else if (atomic_read(&VFS_I(ip)->i_count) == 1) {
828+
/*
829+
* If this is the last reference to the inode and the caller
830+
* permits it, set DONTCACHE to avoid thrashing.
831+
*/
832+
d_mark_dontcache(VFS_I(ip));
833+
}
834+
835+
xfs_irele(ip);
836+
}
837+
794838
/* Set us up to scrub a file's contents. */
795839
int
796840
xchk_setup_inode_contents(

fs/xfs/scrub/common.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,9 @@ int xchk_get_inode(struct xfs_scrub *sc);
137137
int xchk_setup_inode_contents(struct xfs_scrub *sc, unsigned int resblks);
138138
void xchk_buffer_recheck(struct xfs_scrub *sc, struct xfs_buf *bp);
139139

140+
int xchk_iget(struct xfs_scrub *sc, xfs_ino_t inum, struct xfs_inode **ipp);
141+
void xchk_irele(struct xfs_scrub *sc, struct xfs_inode *ip);
142+
140143
/*
141144
* Don't bother cross-referencing if we already found corruption or cross
142145
* referencing discrepancies.

fs/xfs/scrub/dir.c

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -117,21 +117,15 @@ xchk_dir_actor(
117117
}
118118

119119
/*
120-
* Grab the inode pointed to by the dirent. We release the
121-
* inode before we cancel the scrub transaction. Since we're
122-
* don't know a priori that releasing the inode won't trigger
123-
* eofblocks cleanup (which allocates what would be a nested
124-
* transaction), we can't use DONTCACHE here because DONTCACHE
125-
* inodes can trigger immediate inactive cleanup of the inode.
126-
* Use UNTRUSTED here to check the allocation status of the inode in
127-
* the inode btrees.
120+
* Grab the inode pointed to by the dirent. We release the inode
121+
* before we cancel the scrub transaction.
128122
*
129123
* If _iget returns -EINVAL or -ENOENT then the child inode number is
130124
* garbage and the directory is corrupt. If the _iget returns
131125
* -EFSCORRUPTED or -EFSBADCRC then the child is corrupt which is a
132126
* cross referencing error. Any other error is an operational error.
133127
*/
134-
error = xfs_iget(mp, sc->tp, ino, XFS_IGET_UNTRUSTED, 0, &ip);
128+
error = xchk_iget(sc, ino, &ip);
135129
if (error == -EINVAL || error == -ENOENT) {
136130
error = -EFSCORRUPTED;
137131
xchk_fblock_process_error(sc, XFS_DATA_FORK, 0, &error);
@@ -141,7 +135,7 @@ xchk_dir_actor(
141135
goto out;
142136

143137
xchk_dir_check_ftype(sc, offset, ip, name->type);
144-
xfs_irele(ip);
138+
xchk_irele(sc, ip);
145139
out:
146140
if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
147141
return -ECANCELED;

fs/xfs/scrub/parent.c

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -127,20 +127,15 @@ xchk_parent_validate(
127127
expected_nlink = VFS_I(sc->ip)->i_nlink == 0 ? 0 : 1;
128128

129129
/*
130-
* Grab this parent inode. We release the inode before we
131-
* cancel the scrub transaction. Since we're don't know a
132-
* priori that releasing the inode won't trigger eofblocks
133-
* cleanup (which allocates what would be a nested transaction)
134-
* if the parent pointer erroneously points to a file, we
135-
* can't use DONTCACHE here because DONTCACHE inodes can trigger
136-
* immediate inactive cleanup of the inode.
130+
* Grab the parent directory inode. This must be released before we
131+
* cancel the scrub transaction.
137132
*
138133
* If _iget returns -EINVAL or -ENOENT then the parent inode number is
139134
* garbage and the directory is corrupt. If the _iget returns
140135
* -EFSCORRUPTED or -EFSBADCRC then the parent is corrupt which is a
141136
* cross referencing error. Any other error is an operational error.
142137
*/
143-
error = xfs_iget(mp, sc->tp, parent_ino, XFS_IGET_UNTRUSTED, 0, &dp);
138+
error = xchk_iget(sc, parent_ino, &dp);
144139
if (error == -EINVAL || error == -ENOENT) {
145140
error = -EFSCORRUPTED;
146141
xchk_fblock_process_error(sc, XFS_DATA_FORK, 0, &error);
@@ -176,7 +171,7 @@ xchk_parent_validate(
176171
out_unlock:
177172
xfs_iunlock(dp, lock_mode);
178173
out_rele:
179-
xfs_irele(dp);
174+
xchk_irele(sc, dp);
180175
return error;
181176
}
182177

fs/xfs/scrub/scrub.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ xchk_teardown(
181181
xfs_iunlock(sc->ip, sc->ilock_flags);
182182
if (sc->ip != ip_in &&
183183
!xfs_internal_inum(sc->mp, sc->ip->i_ino))
184-
xfs_irele(sc->ip);
184+
xchk_irele(sc, sc->ip);
185185
sc->ip = NULL;
186186
}
187187
if (sc->sm->sm_flags & XFS_SCRUB_IFLAG_REPAIR)

0 commit comments

Comments
 (0)