Commit 167cd4df authored by Stephen Rothwell's avatar Stephen Rothwell Committed by Russell King

[PATCH] fcntl_[sg]etlk() only need the file *

Another simple part of a Matthew Wilcox patch.

We do not need to pass the file descriptor to the fcntl_[sg]etlk
functions, only the struct file * which we have already got
from the file descriptor and verified.
parent 726efabd
...@@ -300,11 +300,11 @@ static long do_fcntl(unsigned int fd, unsigned int cmd, ...@@ -300,11 +300,11 @@ static long do_fcntl(unsigned int fd, unsigned int cmd,
unlock_kernel(); unlock_kernel();
break; break;
case F_GETLK: case F_GETLK:
err = fcntl_getlk(fd, (struct flock *) arg); err = fcntl_getlk(filp, (struct flock *) arg);
break; break;
case F_SETLK: case F_SETLK:
case F_SETLKW: case F_SETLKW:
err = fcntl_setlk(fd, cmd, (struct flock *) arg); err = fcntl_setlk(filp, cmd, (struct flock *) arg);
break; break;
case F_GETOWN: case F_GETOWN:
/* /*
...@@ -386,13 +386,11 @@ asmlinkage long sys_fcntl64(unsigned int fd, unsigned int cmd, unsigned long arg ...@@ -386,13 +386,11 @@ asmlinkage long sys_fcntl64(unsigned int fd, unsigned int cmd, unsigned long arg
switch (cmd) { switch (cmd) {
case F_GETLK64: case F_GETLK64:
err = fcntl_getlk64(fd, (struct flock64 *) arg); err = fcntl_getlk64(filp, (struct flock64 *) arg);
break; break;
case F_SETLK64: case F_SETLK64:
err = fcntl_setlk64(fd, cmd, (struct flock64 *) arg);
break;
case F_SETLKW64: case F_SETLKW64:
err = fcntl_setlk64(fd, cmd, (struct flock64 *) arg); err = fcntl_setlk64(filp, cmd, (struct flock64 *) arg);
break; break;
default: default:
err = do_fcntl(fd, cmd, arg, filp); err = do_fcntl(fd, cmd, arg, filp);
......
...@@ -1351,9 +1351,8 @@ asmlinkage long sys_flock(unsigned int fd, unsigned int cmd) ...@@ -1351,9 +1351,8 @@ asmlinkage long sys_flock(unsigned int fd, unsigned int cmd)
/* Report the first existing lock that would conflict with l. /* Report the first existing lock that would conflict with l.
* This implements the F_GETLK command of fcntl(). * This implements the F_GETLK command of fcntl().
*/ */
int fcntl_getlk(unsigned int fd, struct flock *l) int fcntl_getlk(struct file *filp, struct flock *l)
{ {
struct file *filp;
struct file_lock *fl, file_lock; struct file_lock *fl, file_lock;
struct flock flock; struct flock flock;
int error; int error;
...@@ -1365,19 +1364,14 @@ int fcntl_getlk(unsigned int fd, struct flock *l) ...@@ -1365,19 +1364,14 @@ int fcntl_getlk(unsigned int fd, struct flock *l)
if ((flock.l_type != F_RDLCK) && (flock.l_type != F_WRLCK)) if ((flock.l_type != F_RDLCK) && (flock.l_type != F_WRLCK))
goto out; goto out;
error = -EBADF;
filp = fget(fd);
if (!filp)
goto out;
error = flock_to_posix_lock(filp, &file_lock, &flock); error = flock_to_posix_lock(filp, &file_lock, &flock);
if (error) if (error)
goto out_putf; goto out;
if (filp->f_op && filp->f_op->lock) { if (filp->f_op && filp->f_op->lock) {
error = filp->f_op->lock(filp, F_GETLK, &file_lock); error = filp->f_op->lock(filp, F_GETLK, &file_lock);
if (error < 0) if (error < 0)
goto out_putf; goto out;
else if (error == LOCK_USE_CLNT) else if (error == LOCK_USE_CLNT)
/* Bypass for NFS with no locking - 2.0.36 compat */ /* Bypass for NFS with no locking - 2.0.36 compat */
fl = posix_test_lock(filp, &file_lock); fl = posix_test_lock(filp, &file_lock);
...@@ -1397,10 +1391,10 @@ int fcntl_getlk(unsigned int fd, struct flock *l) ...@@ -1397,10 +1391,10 @@ int fcntl_getlk(unsigned int fd, struct flock *l)
*/ */
error = -EOVERFLOW; error = -EOVERFLOW;
if (fl->fl_start > OFFT_OFFSET_MAX) if (fl->fl_start > OFFT_OFFSET_MAX)
goto out_putf; goto out;
if ((fl->fl_end != OFFSET_MAX) if ((fl->fl_end != OFFSET_MAX)
&& (fl->fl_end > OFFT_OFFSET_MAX)) && (fl->fl_end > OFFT_OFFSET_MAX))
goto out_putf; goto out;
#endif #endif
flock.l_start = fl->fl_start; flock.l_start = fl->fl_start;
flock.l_len = fl->fl_end == OFFSET_MAX ? 0 : flock.l_len = fl->fl_end == OFFSET_MAX ? 0 :
...@@ -1412,8 +1406,6 @@ int fcntl_getlk(unsigned int fd, struct flock *l) ...@@ -1412,8 +1406,6 @@ int fcntl_getlk(unsigned int fd, struct flock *l)
if (!copy_to_user(l, &flock, sizeof(flock))) if (!copy_to_user(l, &flock, sizeof(flock)))
error = 0; error = 0;
out_putf:
fput(filp);
out: out:
return error; return error;
} }
...@@ -1421,9 +1413,8 @@ int fcntl_getlk(unsigned int fd, struct flock *l) ...@@ -1421,9 +1413,8 @@ int fcntl_getlk(unsigned int fd, struct flock *l)
/* Apply the lock described by l to an open file descriptor. /* Apply the lock described by l to an open file descriptor.
* This implements both the F_SETLK and F_SETLKW commands of fcntl(). * This implements both the F_SETLK and F_SETLKW commands of fcntl().
*/ */
int fcntl_setlk(unsigned int fd, unsigned int cmd, struct flock *l) int fcntl_setlk(struct file *filp, unsigned int cmd, struct flock *l)
{ {
struct file *filp;
struct file_lock *file_lock = locks_alloc_lock(0); struct file_lock *file_lock = locks_alloc_lock(0);
struct flock flock; struct flock flock;
struct inode *inode; struct inode *inode;
...@@ -1441,13 +1432,6 @@ int fcntl_setlk(unsigned int fd, unsigned int cmd, struct flock *l) ...@@ -1441,13 +1432,6 @@ int fcntl_setlk(unsigned int fd, unsigned int cmd, struct flock *l)
/* Get arguments and validate them ... /* Get arguments and validate them ...
*/ */
error = -EBADF;
filp = fget(fd);
if (!filp)
goto out;
error = -EINVAL;
inode = filp->f_dentry->d_inode; inode = filp->f_dentry->d_inode;
/* Don't allow mandatory locks on files that may be memory mapped /* Don't allow mandatory locks on files that may be memory mapped
...@@ -1459,23 +1443,23 @@ int fcntl_setlk(unsigned int fd, unsigned int cmd, struct flock *l) ...@@ -1459,23 +1443,23 @@ int fcntl_setlk(unsigned int fd, unsigned int cmd, struct flock *l)
if (!list_empty(&mapping->i_mmap_shared)) { if (!list_empty(&mapping->i_mmap_shared)) {
error = -EAGAIN; error = -EAGAIN;
goto out_putf; goto out;
} }
} }
error = flock_to_posix_lock(filp, file_lock, &flock); error = flock_to_posix_lock(filp, file_lock, &flock);
if (error) if (error)
goto out_putf; goto out;
error = -EBADF; error = -EBADF;
switch (flock.l_type) { switch (flock.l_type) {
case F_RDLCK: case F_RDLCK:
if (!(filp->f_mode & FMODE_READ)) if (!(filp->f_mode & FMODE_READ))
goto out_putf; goto out;
break; break;
case F_WRLCK: case F_WRLCK:
if (!(filp->f_mode & FMODE_WRITE)) if (!(filp->f_mode & FMODE_WRITE))
goto out_putf; goto out;
break; break;
case F_UNLCK: case F_UNLCK:
break; break;
...@@ -1493,23 +1477,21 @@ int fcntl_setlk(unsigned int fd, unsigned int cmd, struct flock *l) ...@@ -1493,23 +1477,21 @@ int fcntl_setlk(unsigned int fd, unsigned int cmd, struct flock *l)
} }
} }
if (!(filp->f_mode & 3)) if (!(filp->f_mode & 3))
goto out_putf; goto out;
break; break;
#endif #endif
default: default:
error = -EINVAL; error = -EINVAL;
goto out_putf; goto out;
} }
if (filp->f_op && filp->f_op->lock != NULL) { if (filp->f_op && filp->f_op->lock != NULL) {
error = filp->f_op->lock(filp, cmd, file_lock); error = filp->f_op->lock(filp, cmd, file_lock);
if (error < 0) if (error < 0)
goto out_putf; goto out;
} }
error = posix_lock_file(filp, file_lock, cmd == F_SETLKW); error = posix_lock_file(filp, file_lock, cmd == F_SETLKW);
out_putf:
fput(filp);
out: out:
locks_free_lock(file_lock); locks_free_lock(file_lock);
return error; return error;
...@@ -1519,9 +1501,8 @@ int fcntl_setlk(unsigned int fd, unsigned int cmd, struct flock *l) ...@@ -1519,9 +1501,8 @@ int fcntl_setlk(unsigned int fd, unsigned int cmd, struct flock *l)
/* Report the first existing lock that would conflict with l. /* Report the first existing lock that would conflict with l.
* This implements the F_GETLK command of fcntl(). * This implements the F_GETLK command of fcntl().
*/ */
int fcntl_getlk64(unsigned int fd, struct flock64 *l) int fcntl_getlk64(struct file *filp, struct flock64 *l)
{ {
struct file *filp;
struct file_lock *fl, file_lock; struct file_lock *fl, file_lock;
struct flock64 flock; struct flock64 flock;
int error; int error;
...@@ -1533,19 +1514,14 @@ int fcntl_getlk64(unsigned int fd, struct flock64 *l) ...@@ -1533,19 +1514,14 @@ int fcntl_getlk64(unsigned int fd, struct flock64 *l)
if ((flock.l_type != F_RDLCK) && (flock.l_type != F_WRLCK)) if ((flock.l_type != F_RDLCK) && (flock.l_type != F_WRLCK))
goto out; goto out;
error = -EBADF;
filp = fget(fd);
if (!filp)
goto out;
error = flock64_to_posix_lock(filp, &file_lock, &flock); error = flock64_to_posix_lock(filp, &file_lock, &flock);
if (error) if (error)
goto out_putf; goto out;
if (filp->f_op && filp->f_op->lock) { if (filp->f_op && filp->f_op->lock) {
error = filp->f_op->lock(filp, F_GETLK, &file_lock); error = filp->f_op->lock(filp, F_GETLK, &file_lock);
if (error < 0) if (error < 0)
goto out_putf; goto out;
else if (error == LOCK_USE_CLNT) else if (error == LOCK_USE_CLNT)
/* Bypass for NFS with no locking - 2.0.36 compat */ /* Bypass for NFS with no locking - 2.0.36 compat */
fl = posix_test_lock(filp, &file_lock); fl = posix_test_lock(filp, &file_lock);
...@@ -1568,8 +1544,6 @@ int fcntl_getlk64(unsigned int fd, struct flock64 *l) ...@@ -1568,8 +1544,6 @@ int fcntl_getlk64(unsigned int fd, struct flock64 *l)
if (!copy_to_user(l, &flock, sizeof(flock))) if (!copy_to_user(l, &flock, sizeof(flock)))
error = 0; error = 0;
out_putf:
fput(filp);
out: out:
return error; return error;
} }
...@@ -1577,9 +1551,8 @@ int fcntl_getlk64(unsigned int fd, struct flock64 *l) ...@@ -1577,9 +1551,8 @@ int fcntl_getlk64(unsigned int fd, struct flock64 *l)
/* Apply the lock described by l to an open file descriptor. /* Apply the lock described by l to an open file descriptor.
* This implements both the F_SETLK and F_SETLKW commands of fcntl(). * This implements both the F_SETLK and F_SETLKW commands of fcntl().
*/ */
int fcntl_setlk64(unsigned int fd, unsigned int cmd, struct flock64 *l) int fcntl_setlk64(struct file *filp, unsigned int cmd, struct flock64 *l)
{ {
struct file *filp;
struct file_lock *file_lock = locks_alloc_lock(0); struct file_lock *file_lock = locks_alloc_lock(0);
struct flock64 flock; struct flock64 flock;
struct inode *inode; struct inode *inode;
...@@ -1597,13 +1570,6 @@ int fcntl_setlk64(unsigned int fd, unsigned int cmd, struct flock64 *l) ...@@ -1597,13 +1570,6 @@ int fcntl_setlk64(unsigned int fd, unsigned int cmd, struct flock64 *l)
/* Get arguments and validate them ... /* Get arguments and validate them ...
*/ */
error = -EBADF;
filp = fget(fd);
if (!filp)
goto out;
error = -EINVAL;
inode = filp->f_dentry->d_inode; inode = filp->f_dentry->d_inode;
/* Don't allow mandatory locks on files that may be memory mapped /* Don't allow mandatory locks on files that may be memory mapped
...@@ -1615,23 +1581,23 @@ int fcntl_setlk64(unsigned int fd, unsigned int cmd, struct flock64 *l) ...@@ -1615,23 +1581,23 @@ int fcntl_setlk64(unsigned int fd, unsigned int cmd, struct flock64 *l)
if (!list_empty(&mapping->i_mmap_shared)) { if (!list_empty(&mapping->i_mmap_shared)) {
error = -EAGAIN; error = -EAGAIN;
goto out_putf; goto out;
} }
} }
error = flock64_to_posix_lock(filp, file_lock, &flock); error = flock64_to_posix_lock(filp, file_lock, &flock);
if (error) if (error)
goto out_putf; goto out;
error = -EBADF; error = -EBADF;
switch (flock.l_type) { switch (flock.l_type) {
case F_RDLCK: case F_RDLCK:
if (!(filp->f_mode & FMODE_READ)) if (!(filp->f_mode & FMODE_READ))
goto out_putf; goto out;
break; break;
case F_WRLCK: case F_WRLCK:
if (!(filp->f_mode & FMODE_WRITE)) if (!(filp->f_mode & FMODE_WRITE))
goto out_putf; goto out;
break; break;
case F_UNLCK: case F_UNLCK:
break; break;
...@@ -1639,18 +1605,16 @@ int fcntl_setlk64(unsigned int fd, unsigned int cmd, struct flock64 *l) ...@@ -1639,18 +1605,16 @@ int fcntl_setlk64(unsigned int fd, unsigned int cmd, struct flock64 *l)
case F_EXLCK: case F_EXLCK:
default: default:
error = -EINVAL; error = -EINVAL;
goto out_putf; goto out;
} }
if (filp->f_op && filp->f_op->lock != NULL) { if (filp->f_op && filp->f_op->lock != NULL) {
error = filp->f_op->lock(filp, cmd, file_lock); error = filp->f_op->lock(filp, cmd, file_lock);
if (error < 0) if (error < 0)
goto out_putf; goto out;
} }
error = posix_lock_file(filp, file_lock, cmd == F_SETLKW64); error = posix_lock_file(filp, file_lock, cmd == F_SETLKW64);
out_putf:
fput(filp);
out: out:
locks_free_lock(file_lock); locks_free_lock(file_lock);
return error; return error;
......
...@@ -576,11 +576,11 @@ extern struct list_head file_lock_list; ...@@ -576,11 +576,11 @@ extern struct list_head file_lock_list;
#include <linux/fcntl.h> #include <linux/fcntl.h>
extern int fcntl_getlk(unsigned int, struct flock *); extern int fcntl_getlk(struct file *, struct flock *);
extern int fcntl_setlk(unsigned int, unsigned int, struct flock *); extern int fcntl_setlk(struct file *, unsigned int, struct flock *);
extern int fcntl_getlk64(unsigned int, struct flock64 *); extern int fcntl_getlk64(struct file *, struct flock64 *);
extern int fcntl_setlk64(unsigned int, unsigned int, struct flock64 *); extern int fcntl_setlk64(struct file *, unsigned int, struct flock64 *);
/* fs/locks.c */ /* fs/locks.c */
extern void locks_init_lock(struct file_lock *); extern void locks_init_lock(struct file_lock *);
......
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