[PATCH 02/13] filelock: add a lm_may_setlease lease_manager callback

Jeff Layton posted 13 patches 2 months, 1 week ago
There is a newer version of this series
[PATCH 02/13] filelock: add a lm_may_setlease lease_manager callback
Posted by Jeff Layton 2 months, 1 week ago
The NFSv4.1 protocol adds support for directory delegations, but it
specifies that if you already have a delegation and try to request a new
one on the same filehandle, the server must reply that the delegation is
unavailable.

Add a new lease manager callback to allow the lease manager (nfsd in
this case) to impose this extra check when performing a setlease.

Signed-off-by: Jeff Layton <jlayton@kernel.org>
---
 fs/locks.c               |  5 +++++
 include/linux/filelock.h | 14 ++++++++++++++
 2 files changed, 19 insertions(+)

diff --git a/fs/locks.c b/fs/locks.c
index 0b16921fb52e602ea2e0c3de39d9d772af98ba7d..9e366b13674538dbf482ffdeee92fc717733ee20 100644
--- a/fs/locks.c
+++ b/fs/locks.c
@@ -1826,6 +1826,11 @@ generic_add_lease(struct file *filp, int arg, struct file_lease **flp, void **pr
 			continue;
 		}
 
+		/* Allow the lease manager to veto the setlease */
+		if (lease->fl_lmops->lm_may_setlease &&
+		    !lease->fl_lmops->lm_may_setlease(lease, fl))
+			goto out;
+
 		/*
 		 * No exclusive leases if someone else has a lease on
 		 * this file:
diff --git a/include/linux/filelock.h b/include/linux/filelock.h
index c2ce8ba05d068b451ecf8f513b7e532819a29944..70079beddf61aa32ef01f1114cf0cb3ffaf2131a 100644
--- a/include/linux/filelock.h
+++ b/include/linux/filelock.h
@@ -49,6 +49,20 @@ struct lease_manager_operations {
 	int (*lm_change)(struct file_lease *, int, struct list_head *);
 	void (*lm_setup)(struct file_lease *, void **);
 	bool (*lm_breaker_owns_lease)(struct file_lease *);
+
+	/**
+	 * lm_may_setlease - extra conditions for setlease
+	 * @new: new file_lease being set
+	 * @old: old (extant) file_lease
+	 *
+	 * This allows the lease manager to add extra conditions when
+	 * setting a lease, based on the presence of an existing lease.
+	 *
+	 * Return values:
+	 *   %false: @new and @old conflict
+	 *   %true: No conflict detected
+	 */
+	bool (*lm_may_setlease)(struct file_lease *new, struct file_lease *old);
 };
 
 struct lock_manager {

-- 
2.51.0
Re: [PATCH 02/13] filelock: add a lm_may_setlease lease_manager callback
Posted by NeilBrown 2 months ago
On Tue, 14 Oct 2025, Jeff Layton wrote:
> The NFSv4.1 protocol adds support for directory delegations, but it
> specifies that if you already have a delegation and try to request a new
> one on the same filehandle, the server must reply that the delegation is
> unavailable.
> 
> Add a new lease manager callback to allow the lease manager (nfsd in
> this case) to impose this extra check when performing a setlease.
> 
> Signed-off-by: Jeff Layton <jlayton@kernel.org>
> ---
>  fs/locks.c               |  5 +++++
>  include/linux/filelock.h | 14 ++++++++++++++
>  2 files changed, 19 insertions(+)
> 
> diff --git a/fs/locks.c b/fs/locks.c
> index 0b16921fb52e602ea2e0c3de39d9d772af98ba7d..9e366b13674538dbf482ffdeee92fc717733ee20 100644
> --- a/fs/locks.c
> +++ b/fs/locks.c
> @@ -1826,6 +1826,11 @@ generic_add_lease(struct file *filp, int arg, struct file_lease **flp, void **pr
>  			continue;
>  		}
>  
> +		/* Allow the lease manager to veto the setlease */
> +		if (lease->fl_lmops->lm_may_setlease &&
> +		    !lease->fl_lmops->lm_may_setlease(lease, fl))
> +			goto out;
> +

I don't see any locking around this.  What if the condition which
triggers a veto happens after this check, and before the lm_change
below?
Should lm_change implement the veto?  Return -EAGAIN?

NeilBrown


>  		/*
>  		 * No exclusive leases if someone else has a lease on
>  		 * this file:
> diff --git a/include/linux/filelock.h b/include/linux/filelock.h
> index c2ce8ba05d068b451ecf8f513b7e532819a29944..70079beddf61aa32ef01f1114cf0cb3ffaf2131a 100644
> --- a/include/linux/filelock.h
> +++ b/include/linux/filelock.h
> @@ -49,6 +49,20 @@ struct lease_manager_operations {
>  	int (*lm_change)(struct file_lease *, int, struct list_head *);
>  	void (*lm_setup)(struct file_lease *, void **);
>  	bool (*lm_breaker_owns_lease)(struct file_lease *);
> +
> +	/**
> +	 * lm_may_setlease - extra conditions for setlease
> +	 * @new: new file_lease being set
> +	 * @old: old (extant) file_lease
> +	 *
> +	 * This allows the lease manager to add extra conditions when
> +	 * setting a lease, based on the presence of an existing lease.
> +	 *
> +	 * Return values:
> +	 *   %false: @new and @old conflict
> +	 *   %true: No conflict detected
> +	 */
> +	bool (*lm_may_setlease)(struct file_lease *new, struct file_lease *old);
>  };
>  
>  struct lock_manager {
> 
> -- 
> 2.51.0
> 
> 
Re: [PATCH 02/13] filelock: add a lm_may_setlease lease_manager callback
Posted by Jeff Layton 2 months ago
On Tue, 2025-10-14 at 16:34 +1100, NeilBrown wrote:
> On Tue, 14 Oct 2025, Jeff Layton wrote:
> > The NFSv4.1 protocol adds support for directory delegations, but it
> > specifies that if you already have a delegation and try to request a new
> > one on the same filehandle, the server must reply that the delegation is
> > unavailable.
> > 
> > Add a new lease manager callback to allow the lease manager (nfsd in
> > this case) to impose this extra check when performing a setlease.
> > 
> > Signed-off-by: Jeff Layton <jlayton@kernel.org>
> > ---
> >  fs/locks.c               |  5 +++++
> >  include/linux/filelock.h | 14 ++++++++++++++
> >  2 files changed, 19 insertions(+)
> > 
> > diff --git a/fs/locks.c b/fs/locks.c
> > index 0b16921fb52e602ea2e0c3de39d9d772af98ba7d..9e366b13674538dbf482ffdeee92fc717733ee20 100644
> > --- a/fs/locks.c
> > +++ b/fs/locks.c
> > @@ -1826,6 +1826,11 @@ generic_add_lease(struct file *filp, int arg, struct file_lease **flp, void **pr
> >  			continue;
> >  		}
> >  
> > +		/* Allow the lease manager to veto the setlease */
> > +		if (lease->fl_lmops->lm_may_setlease &&
> > +		    !lease->fl_lmops->lm_may_setlease(lease, fl))
> > +			goto out;
> > +
> 
> I don't see any locking around this.  What if the condition which
> triggers a veto happens after this check, and before the lm_change
> below?
> Should lm_change implement the veto?  Return -EAGAIN?
> 
> 

The flc_lock is held over this check and any subsequent lease addition.
Is that not sufficient?

> >  		/*
> >  		 * No exclusive leases if someone else has a lease on
> >  		 * this file:
> > diff --git a/include/linux/filelock.h b/include/linux/filelock.h
> > index c2ce8ba05d068b451ecf8f513b7e532819a29944..70079beddf61aa32ef01f1114cf0cb3ffaf2131a 100644
> > --- a/include/linux/filelock.h
> > +++ b/include/linux/filelock.h
> > @@ -49,6 +49,20 @@ struct lease_manager_operations {
> >  	int (*lm_change)(struct file_lease *, int, struct list_head *);
> >  	void (*lm_setup)(struct file_lease *, void **);
> >  	bool (*lm_breaker_owns_lease)(struct file_lease *);
> > +
> > +	/**
> > +	 * lm_may_setlease - extra conditions for setlease
> > +	 * @new: new file_lease being set
> > +	 * @old: old (extant) file_lease
> > +	 *
> > +	 * This allows the lease manager to add extra conditions when
> > +	 * setting a lease, based on the presence of an existing lease.
> > +	 *
> > +	 * Return values:
> > +	 *   %false: @new and @old conflict
> > +	 *   %true: No conflict detected
> > +	 */
> > +	bool (*lm_may_setlease)(struct file_lease *new, struct file_lease *old);
> >  };
> >  
> >  struct lock_manager {
> > 
> > -- 
> > 2.51.0
> > 
> > 

-- 
Jeff Layton <jlayton@kernel.org>