Skip to content

Commit 2454ad8

Browse files
jankarabrauner
authored andcommitted
fs: Restrict lock_two_nondirectories() to non-directory inodes
Currently lock_two_nondirectories() is skipping any passed directories. After vfs_rename() uses lock_two_inodes(), all the remaining four users of this function pass only regular files to it. So drop the somewhat unusual "skip directory" logic and instead warn if anybody passes directory to it. This also allows us to use lock_two_inodes() in lock_two_nondirectories() to concentrate the lock ordering logic in less places. Signed-off-by: Jan Kara <[email protected]> Message-Id: <[email protected]> Signed-off-by: Christian Brauner <[email protected]>
1 parent 28eceed commit 2454ad8

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

fs/inode.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,21 +1148,17 @@ void lock_two_inodes(struct inode *inode1, struct inode *inode2,
11481148
/**
11491149
* lock_two_nondirectories - take two i_mutexes on non-directory objects
11501150
*
1151-
* Lock any non-NULL argument that is not a directory.
1151+
* Lock any non-NULL argument. Passed objects must not be directories.
11521152
* Zero, one or two objects may be locked by this function.
11531153
*
11541154
* @inode1: first inode to lock
11551155
* @inode2: second inode to lock
11561156
*/
11571157
void lock_two_nondirectories(struct inode *inode1, struct inode *inode2)
11581158
{
1159-
if (inode1 > inode2)
1160-
swap(inode1, inode2);
1161-
1162-
if (inode1 && !S_ISDIR(inode1->i_mode))
1163-
inode_lock(inode1);
1164-
if (inode2 && !S_ISDIR(inode2->i_mode) && inode2 != inode1)
1165-
inode_lock_nested(inode2, I_MUTEX_NONDIR2);
1159+
WARN_ON_ONCE(S_ISDIR(inode1->i_mode));
1160+
WARN_ON_ONCE(S_ISDIR(inode2->i_mode));
1161+
lock_two_inodes(inode1, inode2, I_MUTEX_NORMAL, I_MUTEX_NONDIR2);
11661162
}
11671163
EXPORT_SYMBOL(lock_two_nondirectories);
11681164

@@ -1173,10 +1169,14 @@ EXPORT_SYMBOL(lock_two_nondirectories);
11731169
*/
11741170
void unlock_two_nondirectories(struct inode *inode1, struct inode *inode2)
11751171
{
1176-
if (inode1 && !S_ISDIR(inode1->i_mode))
1172+
if (inode1) {
1173+
WARN_ON_ONCE(S_ISDIR(inode1->i_mode));
11771174
inode_unlock(inode1);
1178-
if (inode2 && !S_ISDIR(inode2->i_mode) && inode2 != inode1)
1175+
}
1176+
if (inode2 && inode2 != inode1) {
1177+
WARN_ON_ONCE(S_ISDIR(inode2->i_mode));
11791178
inode_unlock(inode2);
1179+
}
11801180
}
11811181
EXPORT_SYMBOL(unlock_two_nondirectories);
11821182

0 commit comments

Comments
 (0)