Skip to content

Commit db4abb4

Browse files
q2venjtlayton
authored andcommitted
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: Kuniyuki Iwashima <kuniyu@amazon.com> Signed-off-by: Jeff Layton <jlayton@kernel.org>
1 parent 4149be7 commit db4abb4

1 file changed

Lines changed: 19 additions & 24 deletions

File tree

fs/locks.c

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2083,20 +2083,9 @@ EXPORT_SYMBOL(locks_lock_inode_wait);
20832083
*/
20842084
SYSCALL_DEFINE2(flock, unsigned int, fd, unsigned int, cmd)
20852085
{
2086-
int can_sleep, error, unlock, type;
2087-
struct fd f = fdget(fd);
2086+
int can_sleep, error, type;
20882087
struct file_lock fl;
2089-
2090-
error = -EBADF;
2091-
if (!f.file)
2092-
goto out;
2093-
2094-
can_sleep = !(cmd & LOCK_NB);
2095-
cmd &= ~LOCK_NB;
2096-
unlock = (cmd == LOCK_UN);
2097-
2098-
if (!unlock && !(f.file->f_mode & (FMODE_READ|FMODE_WRITE)))
2099-
goto out_putf;
2088+
struct fd f;
21002089

21012090
/*
21022091
* 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)
21082097
*/
21092098
if (cmd & LOCK_MAND) {
21102099
pr_warn_once("Attempt to set a LOCK_MAND lock via flock(2). This support has been removed and the request ignored.\n");
2111-
error = 0;
2112-
goto out_putf;
2100+
return 0;
21132101
}
21142102

2115-
type = flock_translate_cmd(cmd);
2116-
if (type < 0) {
2117-
error = type;
2103+
type = flock_translate_cmd(cmd & ~LOCK_NB);
2104+
if (type < 0)
2105+
return type;
2106+
2107+
error = -EBADF;
2108+
f = fdget(fd);
2109+
if (!f.file)
2110+
return error;
2111+
2112+
if (type != F_UNLCK && !(f.file->f_mode & (FMODE_READ | FMODE_WRITE)))
21182113
goto out_putf;
2119-
}
21202114

21212115
flock_make_lock(f.file, &fl, type);
21222116

2123-
if (can_sleep)
2124-
fl.fl_flags |= FL_SLEEP;
2125-
21262117
error = security_file_lock(f.file, fl.fl_type);
21272118
if (error)
21282119
goto out_putf;
21292120

2121+
can_sleep = !(cmd & LOCK_NB);
2122+
if (can_sleep)
2123+
fl.fl_flags |= FL_SLEEP;
2124+
21302125
if (f.file->f_op->flock)
21312126
error = f.file->f_op->flock(f.file,
2132-
(can_sleep) ? F_SETLKW : F_SETLK,
2127+
(can_sleep) ? F_SETLKW : F_SETLK,
21332128
&fl);
21342129
else
21352130
error = locks_lock_file_wait(f.file, &fl);
21362131

21372132
out_putf:
21382133
fdput(f);
2139-
out:
2134+
21402135
return error;
21412136
}
21422137

0 commit comments

Comments
 (0)