Commit db4abb4a authored by Kuniyuki Iwashima's avatar Kuniyuki Iwashima Committed by Jeff Layton

fs/lock: Rearrange ops in flock syscall.

The previous patch added flock_translate_cmd() in flock syscall.
The test and the other one for LOCK_MAND do not depend on struct
fd and are cheaper, so we can put them at the top and defer
fdget() after that.

Also, we can remove the unlock variable and use type instead.
While at it, we fix this checkpatch error.

  CHECK: spaces preferred around that '|' (ctx:VxV)
  #45: FILE: fs/locks.c:2099:
  +	if (type != F_UNLCK && !(f.file->f_mode & (FMODE_READ|FMODE_WRITE)))
   	                                                     ^

Finally, we can move the can_sleep part just before we use it.
Signed-off-by: default avatarKuniyuki Iwashima <kuniyu@amazon.com>
Signed-off-by: default avatarJeff Layton <jlayton@kernel.org>
parent 4149be7b
...@@ -2083,20 +2083,9 @@ EXPORT_SYMBOL(locks_lock_inode_wait); ...@@ -2083,20 +2083,9 @@ EXPORT_SYMBOL(locks_lock_inode_wait);
*/ */
SYSCALL_DEFINE2(flock, unsigned int, fd, unsigned int, cmd) SYSCALL_DEFINE2(flock, unsigned int, fd, unsigned int, cmd)
{ {
int can_sleep, error, unlock, type; int can_sleep, error, type;
struct fd f = fdget(fd);
struct file_lock fl; struct file_lock fl;
struct fd f;
error = -EBADF;
if (!f.file)
goto out;
can_sleep = !(cmd & LOCK_NB);
cmd &= ~LOCK_NB;
unlock = (cmd == LOCK_UN);
if (!unlock && !(f.file->f_mode & (FMODE_READ|FMODE_WRITE)))
goto out_putf;
/* /*
* LOCK_MAND locks were broken for a long time in that they never * LOCK_MAND locks were broken for a long time in that they never
...@@ -2108,35 +2097,41 @@ SYSCALL_DEFINE2(flock, unsigned int, fd, unsigned int, cmd) ...@@ -2108,35 +2097,41 @@ SYSCALL_DEFINE2(flock, unsigned int, fd, unsigned int, cmd)
*/ */
if (cmd & LOCK_MAND) { if (cmd & LOCK_MAND) {
pr_warn_once("Attempt to set a LOCK_MAND lock via flock(2). This support has been removed and the request ignored.\n"); pr_warn_once("Attempt to set a LOCK_MAND lock via flock(2). This support has been removed and the request ignored.\n");
error = 0; return 0;
goto out_putf;
} }
type = flock_translate_cmd(cmd); type = flock_translate_cmd(cmd & ~LOCK_NB);
if (type < 0) { if (type < 0)
error = type; return type;
error = -EBADF;
f = fdget(fd);
if (!f.file)
return error;
if (type != F_UNLCK && !(f.file->f_mode & (FMODE_READ | FMODE_WRITE)))
goto out_putf; goto out_putf;
}
flock_make_lock(f.file, &fl, type); flock_make_lock(f.file, &fl, type);
if (can_sleep)
fl.fl_flags |= FL_SLEEP;
error = security_file_lock(f.file, fl.fl_type); error = security_file_lock(f.file, fl.fl_type);
if (error) if (error)
goto out_putf; goto out_putf;
can_sleep = !(cmd & LOCK_NB);
if (can_sleep)
fl.fl_flags |= FL_SLEEP;
if (f.file->f_op->flock) if (f.file->f_op->flock)
error = f.file->f_op->flock(f.file, error = f.file->f_op->flock(f.file,
(can_sleep) ? F_SETLKW : F_SETLK, (can_sleep) ? F_SETLKW : F_SETLK,
&fl); &fl);
else else
error = locks_lock_file_wait(f.file, &fl); error = locks_lock_file_wait(f.file, &fl);
out_putf: out_putf:
fdput(f); fdput(f);
out:
return error; return error;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment