locks: ensure that fl_owner is always initialized properly in flock and lease codepaths
authorJeff Layton <jlayton@poochiereds.net>
Fri, 9 May 2014 18:13:04 +0000 (14:13 -0400)
committerJeff Layton <jlayton@poochiereds.net>
Mon, 2 Jun 2014 12:09:29 +0000 (08:09 -0400)
Currently, the fl_owner isn't set for flock locks. Some filesystems use
byte-range locks to simulate flock locks and there is a common idiom in
those that does:

    fl->fl_owner = (fl_owner_t)filp;
    fl->fl_start = 0;
    fl->fl_end = OFFSET_MAX;

Since flock locks are generally "owned" by the open file description,
move this into the common flock lock setup code. The fl_start and fl_end
fields are already set appropriately, so remove the unneeded setting of
that in flock ops in those filesystems as well.

Finally, the lease code also sets the fl_owner as if they were owned by
the process and not the open file description. This is incorrect as
leases have the same ownership semantics as flock locks. Set them the
same way. The lease code doesn't actually use the fl_owner value for
anything, so this is more for consistency's sake than a bugfix.

Reported-by: Trond Myklebust <trond.myklebust@primarydata.com>
Signed-off-by: Jeff Layton <jlayton@poochiereds.net>
Acked-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> (Staging portion)
Acked-by: J. Bruce Fields <bfields@fieldses.org>
drivers/staging/lustre/lustre/llite/file.c
fs/9p/vfs_file.c
fs/afs/flock.c
fs/ceph/locks.c
fs/fuse/file.c
fs/locks.c
fs/nfs/file.c

index 8e844a6371e0600c76ae814e080df1cfd1d3099d..760ccd83f1f712e41fdad0aed0237bde77f2b710 100644 (file)
@@ -2691,20 +2691,15 @@ int ll_file_flock(struct file *file, int cmd, struct file_lock *file_lock)
 
        ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_FLOCK, 1);
 
-       if (file_lock->fl_flags & FL_FLOCK) {
+       if (file_lock->fl_flags & FL_FLOCK)
                LASSERT((cmd == F_SETLKW) || (cmd == F_SETLK));
-               /* flocks are whole-file locks */
-               flock.l_flock.end = OFFSET_MAX;
-               /* For flocks owner is determined by the local file descriptor*/
-               flock.l_flock.owner = (unsigned long)file_lock->fl_file;
-       } else if (file_lock->fl_flags & FL_POSIX) {
-               flock.l_flock.owner = (unsigned long)file_lock->fl_owner;
-               flock.l_flock.start = file_lock->fl_start;
-               flock.l_flock.end = file_lock->fl_end;
-       } else {
+       else if (!(file_lock->fl_flags & FL_POSIX))
                return -EINVAL;
-       }
+
+       flock.l_flock.owner = (unsigned long)file_lock->fl_owner;
        flock.l_flock.pid = file_lock->fl_pid;
+       flock.l_flock.start = file_lock->fl_start;
+       flock.l_flock.end = file_lock->fl_end;
 
        /* Somewhat ugly workaround for svc lockd.
         * lockd installs custom fl_lmops->lm_compare_owner that checks
index d8223209d4b1d58710dccf7f3eda0b429655fbb4..59e3fe3d56c0e97c924dacb7bc0b28095ae24b86 100644 (file)
@@ -352,9 +352,6 @@ static int v9fs_file_flock_dotl(struct file *filp, int cmd,
                invalidate_mapping_pages(&inode->i_data, 0, -1);
        }
        /* Convert flock to posix lock */
-       fl->fl_owner = (fl_owner_t)filp;
-       fl->fl_start = 0;
-       fl->fl_end = OFFSET_MAX;
        fl->fl_flags |= FL_POSIX;
        fl->fl_flags ^= FL_FLOCK;
 
index a8cf2cff836c337fa64f22009c7cc15e4aa8b6bf..4baf1d2b39e410ffb501c2aba457fdceadcc2cdb 100644 (file)
@@ -555,10 +555,6 @@ int afs_flock(struct file *file, int cmd, struct file_lock *fl)
                return -ENOLCK;
 
        /* we're simulating flock() locks using posix locks on the server */
-       fl->fl_owner = (fl_owner_t) file;
-       fl->fl_start = 0;
-       fl->fl_end = OFFSET_MAX;
-
        if (fl->fl_type == F_UNLCK)
                return afs_do_unlk(file, fl);
        return afs_do_setlk(file, fl);
index 191398852a2e8927b1ac193b53ceea623829caf1..fbc39c47bacd17150581e2741531c610e5e89f56 100644 (file)
@@ -53,10 +53,7 @@ static int ceph_lock_message(u8 lock_type, u16 operation, struct file *file,
        else
                length = fl->fl_end - fl->fl_start + 1;
 
-       if (lock_type == CEPH_LOCK_FCNTL)
-               owner = secure_addr(fl->fl_owner);
-       else
-               owner = secure_addr(fl->fl_file);
+       owner = secure_addr(fl->fl_owner);
 
        dout("ceph_lock_message: rule: %d, op: %d, owner: %llx, pid: %llu, "
             "start: %llu, length: %llu, wait: %d, type: %d", (int)lock_type,
@@ -314,10 +311,7 @@ int lock_to_ceph_filelock(struct file_lock *lock,
        cephlock->length = cpu_to_le64(lock->fl_end - lock->fl_start + 1);
        cephlock->client = cpu_to_le64(0);
        cephlock->pid = cpu_to_le64((u64)lock->fl_pid);
-       if (lock->fl_flags & FL_POSIX)
-               cephlock->owner = cpu_to_le64(secure_addr(lock->fl_owner));
-       else
-               cephlock->owner = cpu_to_le64(secure_addr(lock->fl_file));
+       cephlock->owner = cpu_to_le64(secure_addr(lock->fl_owner));
 
        switch (lock->fl_type) {
        case F_RDLCK:
index 96d513e01a5d534e8717622a4fb2c7b20bb98d64..f680d2c44e973bf34bb98f0352fca016e3776d7e 100644 (file)
@@ -2304,7 +2304,6 @@ static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
                struct fuse_file *ff = file->private_data;
 
                /* emulate flock with POSIX locks */
-               fl->fl_owner = (fl_owner_t) file;
                ff->flock = true;
                err = fuse_setlk(file, fl, 1);
        }
index e390bd9ae068696d4a5425057559d3037e6f4518..77aff897fef3a02c608deb87ef099e4c55a8c464 100644 (file)
@@ -322,6 +322,7 @@ static int flock_make_lock(struct file *filp, struct file_lock **lock,
                return -ENOMEM;
 
        fl->fl_file = filp;
+       fl->fl_owner = (fl_owner_t)filp;
        fl->fl_pid = current->tgid;
        fl->fl_flags = FL_FLOCK;
        fl->fl_type = type;
@@ -427,7 +428,7 @@ static int lease_init(struct file *filp, long type, struct file_lock *fl)
        if (assign_type(fl, type) != 0)
                return -EINVAL;
 
-       fl->fl_owner = current->files;
+       fl->fl_owner = (fl_owner_t)filp;
        fl->fl_pid = current->tgid;
 
        fl->fl_file = filp;
@@ -2316,6 +2317,7 @@ void locks_remove_file(struct file *filp)
 
        if (filp->f_op->flock) {
                struct file_lock fl = {
+                       .fl_owner = (fl_owner_t)filp,
                        .fl_pid = current->tgid,
                        .fl_file = filp,
                        .fl_flags = FL_FLOCK,
index 284ca901fe16cd26d064c603f2e51441708a5953..c1edf7336315c3f8ddffe45261d814b8f1877771 100644 (file)
@@ -916,10 +916,6 @@ int nfs_flock(struct file *filp, int cmd, struct file_lock *fl)
                is_local = 1;
 
        /* We're simulating flock() locks using posix locks on the server */
-       fl->fl_owner = (fl_owner_t)filp;
-       fl->fl_start = 0;
-       fl->fl_end = OFFSET_MAX;
-
        if (fl->fl_type == F_UNLCK)
                return do_unlk(filp, cmd, fl, is_local);
        return do_setlk(filp, cmd, fl, is_local);