Skip to content

Commit 3661a78

Browse files
neilbrownbrauner
authored andcommitted
VFS: tidy up do_unlinkat()
The simplification of locking in the previous patch opens up some room for tidying up do_unlinkat() - change all "exit" labels to describe what will happen at the label. - always goto an exit label on an error - unwrap the "if (!IS_ERR())" branch. - Move the "slashes" handing inline, but mark it as unlikely() - simplify use of the "inode" variable - we no longer need to test for NULL. Reviewed-by: Amir Goldstein <amir73il@gmail.com> Reviewed-by: Jeff Layton <jlayton@kernel.org> Signed-off-by: NeilBrown <neil@brown.name> Link: https://patch.msgid.link/20251113002050.676694-4-neilb@ownmail.net Tested-by: syzbot@syzkaller.appspotmail.com Signed-off-by: Christian Brauner <brauner@kernel.org>
1 parent 4037d96 commit 3661a78

1 file changed

Lines changed: 26 additions & 29 deletions

File tree

fs/namei.c

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4755,65 +4755,62 @@ int do_unlinkat(int dfd, struct filename *name)
47554755
struct path path;
47564756
struct qstr last;
47574757
int type;
4758-
struct inode *inode = NULL;
4758+
struct inode *inode;
47594759
struct inode *delegated_inode = NULL;
47604760
unsigned int lookup_flags = 0;
47614761
retry:
47624762
error = filename_parentat(dfd, name, lookup_flags, &path, &last, &type);
47634763
if (error)
4764-
goto exit1;
4764+
goto exit_putname;
47654765

47664766
error = -EISDIR;
47674767
if (type != LAST_NORM)
4768-
goto exit2;
4768+
goto exit_path_put;
47694769

47704770
error = mnt_want_write(path.mnt);
47714771
if (error)
4772-
goto exit2;
4772+
goto exit_path_put;
47734773
retry_deleg:
47744774
dentry = start_dirop(path.dentry, &last, lookup_flags);
47754775
error = PTR_ERR(dentry);
4776-
if (!IS_ERR(dentry)) {
4776+
if (IS_ERR(dentry))
4777+
goto exit_drop_write;
47774778

4778-
/* Why not before? Because we want correct error value */
4779-
if (last.name[last.len])
4780-
goto slashes;
4781-
inode = dentry->d_inode;
4782-
ihold(inode);
4783-
error = security_path_unlink(&path, dentry);
4784-
if (error)
4785-
goto exit3;
4786-
error = vfs_unlink(mnt_idmap(path.mnt), path.dentry->d_inode,
4787-
dentry, &delegated_inode);
4788-
exit3:
4779+
/* Why not before? Because we want correct error value */
4780+
if (unlikely(last.name[last.len])) {
4781+
if (d_is_dir(dentry))
4782+
error = -EISDIR;
4783+
else
4784+
error = -ENOTDIR;
47894785
end_dirop(dentry);
4786+
goto exit_drop_write;
47904787
}
4791-
if (inode)
4792-
iput(inode); /* truncate the inode here */
4793-
inode = NULL;
4788+
inode = dentry->d_inode;
4789+
ihold(inode);
4790+
error = security_path_unlink(&path, dentry);
4791+
if (error)
4792+
goto exit_end_dirop;
4793+
error = vfs_unlink(mnt_idmap(path.mnt), path.dentry->d_inode,
4794+
dentry, &delegated_inode);
4795+
exit_end_dirop:
4796+
end_dirop(dentry);
4797+
iput(inode); /* truncate the inode here */
47944798
if (delegated_inode) {
47954799
error = break_deleg_wait(&delegated_inode);
47964800
if (!error)
47974801
goto retry_deleg;
47984802
}
4803+
exit_drop_write:
47994804
mnt_drop_write(path.mnt);
4800-
exit2:
4805+
exit_path_put:
48014806
path_put(&path);
48024807
if (retry_estale(error, lookup_flags)) {
48034808
lookup_flags |= LOOKUP_REVAL;
4804-
inode = NULL;
48054809
goto retry;
48064810
}
4807-
exit1:
4811+
exit_putname:
48084812
putname(name);
48094813
return error;
4810-
4811-
slashes:
4812-
if (d_is_dir(dentry))
4813-
error = -EISDIR;
4814-
else
4815-
error = -ENOTDIR;
4816-
goto exit3;
48174814
}
48184815

48194816
SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)

0 commit comments

Comments
 (0)