From: Kuniyuki Iwashima <kuniyu@xxxxxxxxxx> Date: Sat, 16 Jul 2022 16:33:43 -0700 > The previous patch added flock_translate_cmd() in flock syscall. > The test does not depend on struct fd and the cheapest, so we can > put it at the top and defer fdget() after that. > > Also, we can remove the unlock variable and use type instead. > As a bonus, 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: Kuniyuki Iwashima <kuniyu@xxxxxxxxxx> > --- > fs/locks.c | 34 +++++++++++++++------------------- > 1 file changed, 15 insertions(+), 19 deletions(-) > > diff --git a/fs/locks.c b/fs/locks.c > index b134eaefd7d6..97581678c4d4 100644 > --- a/fs/locks.c > +++ b/fs/locks.c > @@ -2083,19 +2083,20 @@ EXPORT_SYMBOL(locks_lock_inode_wait); > */ > SYSCALL_DEFINE2(flock, unsigned int, fd, unsigned int, cmd) > { > - int can_sleep, error, unlock, type; > - struct fd f = fdget(fd); > + int can_sleep, error, type; > struct file_lock fl; > + struct fd f; > + > + type = flock_translate_cmd(cmd & ~LOCK_NB); > + if (type < 0) > + return type; Sorry, we have to check (cmd & LOCK_MAND) first. Will fix in v3. > > error = -EBADF; > + f = fdget(fd); > if (!f.file) > - goto out; > - > - can_sleep = !(cmd & LOCK_NB); > - cmd &= ~LOCK_NB; > - unlock = (cmd == LOCK_UN); > + return error; > > - if (!unlock && !(f.file->f_mode & (FMODE_READ|FMODE_WRITE))) > + if (type != F_UNLCK && !(f.file->f_mode & (FMODE_READ | FMODE_WRITE))) > goto out_putf; > > /* > @@ -2112,31 +2113,26 @@ SYSCALL_DEFINE2(flock, unsigned int, fd, unsigned int, cmd) > goto out_putf; > } > > - type = flock_translate_cmd(cmd); > - if (type < 0) { > - error = type; > - goto out_putf; > - } > - > flock_make_lock(f.file, &fl, type); > > - if (can_sleep) > - fl.fl_flags |= FL_SLEEP; > - > error = security_file_lock(f.file, fl.fl_type); > if (error) > goto out_putf; > > + can_sleep = !(cmd & LOCK_NB); > + if (can_sleep) > + fl.fl_flags |= FL_SLEEP; > + > if (f.file->f_op->flock) > error = f.file->f_op->flock(f.file, > - (can_sleep) ? F_SETLKW : F_SETLK, > + (can_sleep) ? F_SETLKW : F_SETLK, > &fl); > else > error = locks_lock_file_wait(f.file, &fl); > > out_putf: > fdput(f); > - out: > + > return error; > } > > -- > 2.30.2