xfs: take the ILOCK when readdir inspects directory mapping data
authorDarrick J. Wong <djwong@kernel.org>
Wed, 5 Jan 2022 01:38:36 +0000 (17:38 -0800)
committerDarrick J. Wong <djwong@kernel.org>
Tue, 11 Jan 2022 23:11:04 +0000 (15:11 -0800)
commit65552b02a10acea68127081faf414b84a65d1855
treefc7b6d9dc15c866923c23d224e79afcf75ed5a53
parent7e937bb3cbe1f6b9840a43f879aa6e3f1a5e6537
xfs: take the ILOCK when readdir inspects directory mapping data

I was poking around in the directory code while diagnosing online fsck
bugs, and noticed that xfs_readdir doesn't actually take the directory
ILOCK when it calls xfs_dir2_isblock.  xfs_dir_open most probably loaded
the data fork mappings and the VFS took i_rwsem (aka IOLOCK_SHARED) so
we're protected against writer threads, but we really need to follow the
locking model like we do in other places.

To avoid unnecessarily cycling the ILOCK for fairly small directories,
change the block/leaf _getdents functions to consume the ILOCK hold that
the parent readdir function took to decide on a _getdents implementation.

It is ok to cycle the ILOCK in readdir because the VFS takes the IOLOCK
in the appropriate mode during lookups and writes, and we don't want to
be holding the ILOCK when we copy directory entries to userspace in case
there's a page fault.  We really only need it to protect against data
fork lookups, like we do for other files.

Signed-off-by: Darrick J. Wong <djwong@kernel.org>
Reviewed-by: Dave Chinner <dchinner@redhat.com>
fs/xfs/xfs_dir2_readdir.c