filelock: WARN when @filp and fl_file don't match
Subsystems (4)
Jeff Layton
maintainercover filelock: WARN when @filp and fl_file don't match
Eventually, I'd like to reduce the redundant arguments to the locking APIs, but to get there we need to ensure the callers all set their file locks sanely.
Adding the WARN_ON_ONCEs helped to find a couple of warts in lockd's file handling. The first 3 patches fix those.
I'd like to see these included in v6.2, with an eye toward a locking API cleanup in v6.3.
Thanks,
Jeff Layton (4):
lockd: set missing fl_flags field when retrieving args
lockd: ensure we use the correct file description when unlocking
lockd: fix file selection in nlmsvc_cancel_blocked
filelock: WARN_ON_ONCE when ->fl_file and filp don't match
fs/lockd/svc4proc.c | 1 + fs/lockd/svclock.c | 17 ++++++++++------- fs/lockd/svcproc.c | 1 + fs/locks.c | 3 +++ 4 files changed, 15 insertions(+), 7 deletions(-)
Signature · 1 line
2.38.1
Jeff Layton
maintainerPATCH 1/4 lockd: set missing fl_flags field when retrieving args
--- fs/lockd/svc4proc.c | 1 + fs/lockd/svcproc.c | 1 + 2 files changed, 2 insertions(+)
fs/lockd/svc4proc.c +1 -0
@@ -52,6 +52,7 @@ nlm4svc_retrieve_args(struct svc_rqst *rqstp, struct nlm_args *argp, *filp = file;/* Set up the missing parts of the file_lock structure */ + lock->fl.fl_flags =FL_POSIX ; lock->fl.fl_file = file->f_file[mode]; lock->fl.fl_pid = current->tgid; lock->fl.fl_start = (loff_t )lock->lock_start;
fs/lockd/svcproc.c +1 -0
@@ -77,6 +77,7 @@ nlmsvc_retrieve_args(struct svc_rqst *rqstp, struct nlm_args *argp,/* Set up the missing parts of the file_lock structure */ mode =lock_to_openmode (&lock->fl); + lock->fl.fl_flags =FL_POSIX ; lock->fl.fl_file = file->f_file[mode]; lock->fl.fl_pid = current->tgid; lock->fl.fl_lmops = &nlmsvc_lock_operations;
Signature · 1 line
2.38.1
Jeff Layton
maintainerPATCH 2/4 lockd: ensure we use the correct file description when unlocking
In reply to Jeff LaytonShared locks are set on O_RDONLY descriptors and exclusive locks are set on O_WRONLY ones. nlmsvc_unlock however calls vfs_lock_file twice, once for each descriptor, but it doesn't reset fl_file. Ensure that it does.
--- fs/lockd/svclock.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)
fs/lockd/svclock.c +6 -4
@@ -659,11 +659,13 @@ nlmsvc_unlock(struct net *net, struct nlm_file *file, struct nlm_lock *lock)nlmsvc_cancel_blocked (net, file, lock); lock->fl.fl_type =F_UNLCK ; -if (file->f_file[O_RDONLY ]) - error =vfs_lock_file (file->f_file[O_RDONLY ],F_SETLK , + lock->fl.fl_file = file->f_file[O_RDONLY ]; +if (lock->fl.fl_file) + error =vfs_lock_file (lock->fl.fl_file,F_SETLK , &lock->fl,NULL ); -if (file->f_file[O_WRONLY ]) - error =vfs_lock_file (file->f_file[O_WRONLY ],F_SETLK , + lock->fl.fl_file = file->f_file[O_WRONLY ]; +if (lock->fl.fl_file) + error |=vfs_lock_file (lock->fl.fl_file,F_SETLK , &lock->fl,NULL );return (error <0 )? nlm_lck_denied_nolocks : nlm_granted;
Signature · 1 line
2.38.1
Jeff Layton
maintainerPATCH 3/4 lockd: fix file selection in nlmsvc_cancel_blocked
In reply to Jeff LaytonWe currently do a lock_to_openmode call based on the arguments from the NLM_UNLOCK call, but that will always set the fl_type of the lock to F_UNLCK, and the O_RDONLY descriptor is always chosen.
Fix it to use the file_lock from the block instead.
--- fs/lockd/svclock.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
fs/lockd/svclock.c +4 -3
@@ -699,9 +699,10 @@ nlmsvc_cancel_blocked(struct net *net, struct nlm_file *file, struct nlm_lock *l block =nlmsvc_lookup_block (file, lock);mutex_unlock (&file->f_mutex);if (block !=NULL ) { - mode =lock_to_openmode (&lock->fl); -vfs_cancel_lock (block->b_file->f_file[mode], - &block->b_call->a_args.lock.fl); +struct file_lock *fl = &block->b_call->a_args.lock.fl; + + mode =lock_to_openmode (fl); +vfs_cancel_lock (block->b_file->f_file[mode], fl); status =nlmsvc_unlink_block (block);nlmsvc_release_block (block); }
Signature · 1 line
2.38.1
Jeff Layton
maintainerPATCH 4/4 filelock: WARN_ON_ONCE when ->fl_file and filp don't match
In reply to Jeff Laytonvfs_lock_file, vfs_test_lock and vfs_cancel_lock all take both a struct file argument and a file_lock. The file_lock has a fl_file field in it howevever and it _must_ match the file passed in.
While most of the locks.c routines use the separately-passed file argument, some filesystems rely on fl_file being filled out correctly.
I'm working on a patch series to remove the redundant argument from these routines, but for now, let's ensure that the callers always set this properly by issuing a WARN_ON_ONCE if they ever don't match.
--- fs/locks.c | 3 +++ 1 file changed, 3 insertions(+)
fs/locks.c +3 -0
@@ -2146,6 +2146,7 @@ SYSCALL_DEFINE2(flock, unsigned int, fd, unsigned int, cmd)*/ int vfs_test_lock (struct file *filp,struct file_lock *fl) { +WARN_ON_ONCE (filp != fl->fl_file);if (filp->f_op->lock)return filp->f_op->lock (filp,F_GETLK , fl);posix_test_lock (filp, fl);
@@ -2295,6 +2296,7 @@ int fcntl_getlk(struct file *filp, unsigned int cmd, struct flock *flock)*/ int vfs_lock_file (struct file *filp,unsigned int cmd,struct file_lock *fl,struct file_lock *conf) { +WARN_ON_ONCE (filp != fl->fl_file);if (filp->f_op->lock)return filp->f_op->lock (filp, cmd, fl);else
@@ -2663,6 +2665,7 @@ void locks_remove_file(struct file *filp)*/ int vfs_cancel_lock (struct file *filp,struct file_lock *fl) { +WARN_ON_ONCE (filp != fl->fl_file);if (filp->f_op->lock)return filp->f_op->lock (filp,F_CANCELLK , fl);return 0 ;
Signature · 1 line
2.38.1