linux-stable/Documentation/filesystems
Al Viro 1db06b3d7d rename(): fix the locking of subdirectories
commit 22e111ed6c upstream.

	We should never lock two subdirectories without having taken
->s_vfs_rename_mutex; inode pointer order or not, the "order" proposed
in 28eceeda13 "fs: Lock moved directories" is not transitive, with
the usual consequences.

	The rationale for locking renamed subdirectory in all cases was
the possibility of race between rename modifying .. in a subdirectory to
reflect the new parent and another thread modifying the same subdirectory.
For a lot of filesystems that's not a problem, but for some it can lead
to trouble (e.g. the case when short directory contents is kept in the
inode, but creating a file in it might push it across the size limit
and copy its contents into separate data block(s)).

	However, we need that only in case when the parent does change -
otherwise ->rename() doesn't need to do anything with .. entry in the
first place.  Some instances are lazy and do a tautological update anyway,
but it's really not hard to avoid.

Amended locking rules for rename():
	find the parent(s) of source and target
	if source and target have the same parent
		lock the common parent
	else
		lock ->s_vfs_rename_mutex
		lock both parents, in ancestor-first order; if neither
		is an ancestor of another, lock the parent of source
		first.
	find the source and target.
	if source and target have the same parent
		if operation is an overwriting rename of a subdirectory
			lock the target subdirectory
	else
		if source is a subdirectory
			lock the source
		if target is a subdirectory
			lock the target
	lock non-directories involved, in inode pointer order if both
	source and target are such.

That way we are guaranteed that parents are locked (for obvious reasons),
that any renamed non-directory is locked (nfsd relies upon that),
that any victim is locked (emptiness check needs that, among other things)
and subdirectory that changes parent is locked (needed to protect the update
of .. entries).  We are also guaranteed that any operation locking more
than one directory either takes ->s_vfs_rename_mutex or locks a parent
followed by its child.

Cc: stable@vger.kernel.org
Fixes: 28eceeda13 "fs: Lock moved directories"
Reviewed-by: Jan Kara <jack@suse.cz>
Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2024-01-31 16:18:57 -08:00
..
caching Documentation: Fix typos 2023-08-18 11:29:03 -06:00
ext4 Documentation: Fix typos 2023-08-18 11:29:03 -06:00
nfs NFSD 6.6 Release Notes 2023-08-31 15:32:18 -07:00
smb smb3: move Documentation/filesystems/cifs to Documentation/filesystems/smb 2023-05-24 16:29:21 -05:00
spufs Documentation: spufs: correct a duplicate word typo 2022-09-27 13:21:44 -06:00
9p.rst Documentation: Fix typos 2023-08-18 11:29:03 -06:00
adfs.rst
affs.rst
afs.rst afs: Documentation: correct reference to CONFIG_AFS_FS 2023-07-21 13:46:02 -06:00
api-summary.rst
autofs-mount-control.rst autofs: use flexible array in ioctl structure 2023-05-30 16:42:00 -07:00
autofs.rst autofs: use flexible array in ioctl structure 2023-05-30 16:42:00 -07:00
automount-support.rst
befs.rst Documentation: Fix typos 2023-08-18 11:29:03 -06:00
bfs.rst
btrfs.rst MAINTAINERS: remove links to obsolete btrfs.wiki.kernel.org 2023-09-08 14:21:27 +02:00
ceph.rst ceph: update documentation regarding snapshot naming limitations 2023-08-24 11:24:36 +02:00
coda.rst
configfs.rst Documentation: Fix typos 2023-08-18 11:29:03 -06:00
cramfs.rst
dax.rst Documentation: Fix typos 2023-08-18 11:29:03 -06:00
debugfs.rst debugfs: small Documentation cleaning 2022-11-09 13:58:55 -07:00
devpts.rst Documentation: Fix typos 2023-08-18 11:29:03 -06:00
directory-locking.rst rename(): fix the locking of subdirectories 2024-01-31 16:18:57 -08:00
dlmfs.rst docs: update ocfs2-devel mailing list address 2023-07-08 09:29:29 -07:00
dnotify.rst
ecryptfs.rst
efivarfs.rst
erofs.rst erofs: update documentation 2023-09-28 22:40:14 +08:00
ext2.rst ext2: remove nobh support 2022-08-02 12:34:04 -04:00
ext3.rst
f2fs.rst Documentation: Fix typos 2023-08-18 11:29:03 -06:00
fiemap.rst
files.rst
fscrypt.rst fscrypt: improve the "Encryption modes and usage" section 2023-07-11 22:56:24 -07:00
fsverity.rst ovl: Add framework for verity support 2023-08-12 19:02:38 +03:00
fuse-io.rst
fuse.rst fuse: Add module param for CAP_SYS_ADMIN access bypassing allow_other 2022-07-21 16:06:19 +02:00
gfs2-glocks.rst gfs2 fixes 2023-09-05 13:00:28 -07:00
gfs2-uevents.rst
gfs2.rst
hfs.rst
hfsplus.rst
hpfs.rst
idmappings.rst Documentation work keeps chugging along; stuff for 6.6 includes: 2023-08-30 20:05:42 -07:00
index.rst docs: add maintainer entry profile for XFS 2023-08-10 07:47:53 -07:00
inotify.rst
isofs.rst
journalling.rst
locking.rst rename(): fix the locking of subdirectories 2024-01-31 16:18:57 -08:00
locks.rst
mount_api.rst fs_context: drop the unused lsm_flags member 2023-03-16 14:38:28 +01:00
netfs_library.rst Documentation: Fix typos 2023-08-18 11:29:03 -06:00
nilfs2.rst Documentation: Fix typos 2023-08-18 11:29:03 -06:00
ntfs.rst
ntfs3.rst Documentation: Fix typos 2023-08-18 11:29:03 -06:00
ocfs2-online-filecheck.rst
ocfs2.rst docs: update ocfs2-devel mailing list address 2023-07-08 09:29:29 -07:00
omfs.rst
orangefs.rst Documentation: Fix typos 2023-08-18 11:29:03 -06:00
overlayfs.rst ovl: fix regression in showing lowerdir mount option 2023-10-14 09:24:11 +03:00
path-lookup.rst
path-lookup.txt
porting.rst rename(): fix the locking of subdirectories 2024-01-31 16:18:57 -08:00
proc.rst - Stefan Roesch has added ksm statistics to /proc/pid/smaps 2023-09-05 10:56:27 -07:00
qnx6.rst Documentation: Fix typos 2023-08-18 11:29:03 -06:00
quota.rst
ramfs-rootfs-initramfs.rst Documentation/filesystems: ramfs-rootfs-initramfs: use :Author: 2023-05-16 12:55:35 -06:00
relay.rst
romfs.rst
seq_file.rst Documentation: Fix typos 2023-08-18 11:29:03 -06:00
sharedsubtree.rst Documentation/filesystems: sharedsubtree: add section headings 2023-05-16 12:50:05 -06:00
splice.rst
squashfs.rst
sysfs.rst driver core: bus: mark the struct bus_type for sysfs callbacks as constant 2023-03-23 13:20:40 +01:00
sysv-fs.rst
tmpfs.rst tmpfs,xattr: enable limited user extended attributes 2023-08-10 12:06:04 +02:00
ubifs-authentication.rst Documentation: Fix typos 2023-08-18 11:29:03 -06:00
ubifs.rst Documentation: ubifs: Fix compression idiom 2022-10-10 13:01:10 -06:00
udf.rst
vfat.rst Documentation: Fix typos 2023-08-18 11:29:03 -06:00
vfs.rst Documentation work keeps chugging along; stuff for 6.6 includes: 2023-08-30 20:05:42 -07:00
virtiofs.rst
xfs-delayed-logging-design.rst Documentation: filesystems: correct possessive "its" 2022-09-27 13:21:44 -06:00
xfs-maintainer-entry-profile.rst docs: add maintainer entry profile for XFS 2023-08-10 07:47:53 -07:00
xfs-online-fsck-design.rst Documentation: Fix typos 2023-08-18 11:29:03 -06:00
xfs-self-describing-metadata.rst xfs: document the filesystem metadata checking strategy 2023-04-11 18:59:47 -07:00
zonefs.rst Documentation: Fix typos 2023-08-18 11:29:03 -06:00