Skip to content

Commit 130d1f9

Browse files
committed
locks: ensure that fl_owner is always initialized properly in flock and lease codepaths
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>
1 parent cf01f4e commit 130d1f9

7 files changed

Lines changed: 11 additions & 32 deletions

File tree

‎drivers/staging/lustre/lustre/llite/file.c‎

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2691,20 +2691,15 @@ int ll_file_flock(struct file *file, int cmd, struct file_lock *file_lock)
26912691

26922692
ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_FLOCK, 1);
26932693

2694-
if (file_lock->fl_flags & FL_FLOCK) {
2694+
if (file_lock->fl_flags & FL_FLOCK)
26952695
LASSERT((cmd == F_SETLKW) || (cmd == F_SETLK));
2696-
/* flocks are whole-file locks */
2697-
flock.l_flock.end = OFFSET_MAX;
2698-
/* For flocks owner is determined by the local file descriptor*/
2699-
flock.l_flock.owner = (unsigned long)file_lock->fl_file;
2700-
} else if (file_lock->fl_flags & FL_POSIX) {
2701-
flock.l_flock.owner = (unsigned long)file_lock->fl_owner;
2702-
flock.l_flock.start = file_lock->fl_start;
2703-
flock.l_flock.end = file_lock->fl_end;
2704-
} else {
2696+
else if (!(file_lock->fl_flags & FL_POSIX))
27052697
return -EINVAL;
2706-
}
2698+
2699+
flock.l_flock.owner = (unsigned long)file_lock->fl_owner;
27072700
flock.l_flock.pid = file_lock->fl_pid;
2701+
flock.l_flock.start = file_lock->fl_start;
2702+
flock.l_flock.end = file_lock->fl_end;
27082703

27092704
/* Somewhat ugly workaround for svc lockd.
27102705
* lockd installs custom fl_lmops->lm_compare_owner that checks

‎fs/9p/vfs_file.c‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -352,9 +352,6 @@ static int v9fs_file_flock_dotl(struct file *filp, int cmd,
352352
invalidate_mapping_pages(&inode->i_data, 0, -1);
353353
}
354354
/* Convert flock to posix lock */
355-
fl->fl_owner = (fl_owner_t)filp;
356-
fl->fl_start = 0;
357-
fl->fl_end = OFFSET_MAX;
358355
fl->fl_flags |= FL_POSIX;
359356
fl->fl_flags ^= FL_FLOCK;
360357

‎fs/afs/flock.c‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -555,10 +555,6 @@ int afs_flock(struct file *file, int cmd, struct file_lock *fl)
555555
return -ENOLCK;
556556

557557
/* we're simulating flock() locks using posix locks on the server */
558-
fl->fl_owner = (fl_owner_t) file;
559-
fl->fl_start = 0;
560-
fl->fl_end = OFFSET_MAX;
561-
562558
if (fl->fl_type == F_UNLCK)
563559
return afs_do_unlk(file, fl);
564560
return afs_do_setlk(file, fl);

‎fs/ceph/locks.c‎

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,7 @@ static int ceph_lock_message(u8 lock_type, u16 operation, struct file *file,
5353
else
5454
length = fl->fl_end - fl->fl_start + 1;
5555

56-
if (lock_type == CEPH_LOCK_FCNTL)
57-
owner = secure_addr(fl->fl_owner);
58-
else
59-
owner = secure_addr(fl->fl_file);
56+
owner = secure_addr(fl->fl_owner);
6057

6158
dout("ceph_lock_message: rule: %d, op: %d, owner: %llx, pid: %llu, "
6259
"start: %llu, length: %llu, wait: %d, type: %d", (int)lock_type,
@@ -314,10 +311,7 @@ int lock_to_ceph_filelock(struct file_lock *lock,
314311
cephlock->length = cpu_to_le64(lock->fl_end - lock->fl_start + 1);
315312
cephlock->client = cpu_to_le64(0);
316313
cephlock->pid = cpu_to_le64((u64)lock->fl_pid);
317-
if (lock->fl_flags & FL_POSIX)
318-
cephlock->owner = cpu_to_le64(secure_addr(lock->fl_owner));
319-
else
320-
cephlock->owner = cpu_to_le64(secure_addr(lock->fl_file));
314+
cephlock->owner = cpu_to_le64(secure_addr(lock->fl_owner));
321315

322316
switch (lock->fl_type) {
323317
case F_RDLCK:

‎fs/fuse/file.c‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2304,7 +2304,6 @@ static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
23042304
struct fuse_file *ff = file->private_data;
23052305

23062306
/* emulate flock with POSIX locks */
2307-
fl->fl_owner = (fl_owner_t) file;
23082307
ff->flock = true;
23092308
err = fuse_setlk(file, fl, 1);
23102309
}

‎fs/locks.c‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,7 @@ static int flock_make_lock(struct file *filp, struct file_lock **lock,
322322
return -ENOMEM;
323323

324324
fl->fl_file = filp;
325+
fl->fl_owner = (fl_owner_t)filp;
325326
fl->fl_pid = current->tgid;
326327
fl->fl_flags = FL_FLOCK;
327328
fl->fl_type = type;
@@ -427,7 +428,7 @@ static int lease_init(struct file *filp, long type, struct file_lock *fl)
427428
if (assign_type(fl, type) != 0)
428429
return -EINVAL;
429430

430-
fl->fl_owner = current->files;
431+
fl->fl_owner = (fl_owner_t)filp;
431432
fl->fl_pid = current->tgid;
432433

433434
fl->fl_file = filp;
@@ -2316,6 +2317,7 @@ void locks_remove_file(struct file *filp)
23162317

23172318
if (filp->f_op->flock) {
23182319
struct file_lock fl = {
2320+
.fl_owner = (fl_owner_t)filp,
23192321
.fl_pid = current->tgid,
23202322
.fl_file = filp,
23212323
.fl_flags = FL_FLOCK,

‎fs/nfs/file.c‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -916,10 +916,6 @@ int nfs_flock(struct file *filp, int cmd, struct file_lock *fl)
916916
is_local = 1;
917917

918918
/* We're simulating flock() locks using posix locks on the server */
919-
fl->fl_owner = (fl_owner_t)filp;
920-
fl->fl_start = 0;
921-
fl->fl_end = OFFSET_MAX;
922-
923919
if (fl->fl_type == F_UNLCK)
924920
return do_unlk(filp, cmd, fl, is_local);
925921
return do_setlk(filp, cmd, fl, is_local);

0 commit comments

Comments
 (0)