Skip to content

Commit

Permalink
fs: sync: Avoid calling fdget without fdput
Browse files Browse the repository at this point in the history
When adding fsync support, we check for fsync_enabled() in several
cases but it appears that we should fdput() after fdget() but the
current code just check for fsync_enabled and directly return in
some cases after calling fdget().

Fix it by checking fsync_enabled first and move the f initialization
after the check. Also remove some unnecessary fsync_enabled checks as
this will be checked later in do_fsync().

Signed-off-by: Wang Han <416810799@qq.com>
Signed-off-by: Nathan Chancellor <natechancellor@gmail.com>
  • Loading branch information
aviraxp authored and khusika committed Jun 15, 2018
1 parent 52868dc commit 2ff6d95
Showing 1 changed file with 4 additions and 12 deletions.
16 changes: 4 additions & 12 deletions fs/sync.c
Expand Up @@ -152,7 +152,7 @@ void emergency_sync(void)
*/
SYSCALL_DEFINE1(syncfs, int, fd)
{
struct fd f = fdget(fd);
struct fd f;
struct super_block *sb;
int ret;

Expand Down Expand Up @@ -211,21 +211,19 @@ EXPORT_SYMBOL(vfs_fsync_range);
*/
int vfs_fsync(struct file *file, int datasync)
{
if (!fsync_enabled)
return 0;

return vfs_fsync_range(file, 0, LLONG_MAX, datasync);
}
EXPORT_SYMBOL(vfs_fsync);

static int do_fsync(unsigned int fd, int datasync)
{
struct fd f = fdget(fd);
struct fd f;
int ret = -EBADF;

if (!fsync_enabled)
return 0;

f = fdget(fd);
if (f.file) {
ret = vfs_fsync(f.file, datasync);
fdput(f);
Expand All @@ -236,17 +234,11 @@ static int do_fsync(unsigned int fd, int datasync)

SYSCALL_DEFINE1(fsync, unsigned int, fd)
{
if (!fsync_enabled)
return 0;

return do_fsync(fd, 0);
}

SYSCALL_DEFINE1(fdatasync, unsigned int, fd)
{
if (!fsync_enabled)
return 0;

{
return do_fsync(fd, 1);
}

Expand Down

0 comments on commit 2ff6d95

Please sign in to comment.