NFSv4: Add support for application leases underpinned by a delegation
authorTrond Myklebust <trond.myklebust@hammerspace.com>
Fri, 7 May 2021 14:06:13 +0000 (10:06 -0400)
committerTrond Myklebust <trond.myklebust@hammerspace.com>
Sun, 13 Jun 2021 23:36:28 +0000 (19:36 -0400)
If the NFSv4 client already holds a delegation for a file, then we can
support application leases (i.e. fcntl(fd, F_SETLEASE,...)) because the
underlying delegation guarantees that the file is not being modified on
the server by another client in a way that might conflict with the lease
guarantees.

Signed-off-by: Trond Myklebust <trond.myklebust@hammerspace.com>
fs/nfs/nfs4_fs.h
fs/nfs/nfs4file.c
fs/nfs/nfs4proc.c

index 3e344bec3647b09889b113c6b568d6316b41140d..ba78df4b13d94bf974738197ed595b95e02d388a 100644 (file)
@@ -323,7 +323,8 @@ extern int update_open_stateid(struct nfs4_state *state,
                                const nfs4_stateid *open_stateid,
                                const nfs4_stateid *deleg_stateid,
                                fmode_t fmode);
-
+extern int nfs4_proc_setlease(struct file *file, long arg,
+                             struct file_lock **lease, void **priv);
 extern int nfs4_proc_get_lease_time(struct nfs_client *clp,
                struct nfs_fsinfo *fsinfo);
 extern void nfs4_update_changeattr(struct inode *dir,
index a1e5c6b85dedc5836b5ecbc407bc02a29dad3e72..c820de58a661520aee268cebd55725f5761f32ba 100644 (file)
@@ -435,6 +435,12 @@ void nfs42_ssc_unregister_ops(void)
 }
 #endif /* CONFIG_NFS_V4_2 */
 
+static int nfs4_setlease(struct file *file, long arg, struct file_lock **lease,
+                        void **priv)
+{
+       return nfs4_proc_setlease(file, arg, lease, priv);
+}
+
 const struct file_operations nfs4_file_operations = {
        .read_iter      = nfs_file_read,
        .write_iter     = nfs_file_write,
@@ -448,7 +454,7 @@ const struct file_operations nfs4_file_operations = {
        .splice_read    = generic_file_splice_read,
        .splice_write   = iter_file_splice_write,
        .check_flags    = nfs_check_flags,
-       .setlease       = simple_nosetlease,
+       .setlease       = nfs4_setlease,
 #ifdef CONFIG_NFS_V4_2
        .copy_file_range = nfs4_copy_file_range,
        .llseek         = nfs4_file_llseek,
index e653654c10bcd8e49c2809891966448dc9975a41..35df44ef2c355e8ea2be487b21ce1ee2641f7a58 100644 (file)
@@ -7438,6 +7438,43 @@ nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
        return nfs4_retry_setlk(state, cmd, request);
 }
 
+static int nfs4_delete_lease(struct file *file, void **priv)
+{
+       return generic_setlease(file, F_UNLCK, NULL, priv);
+}
+
+static int nfs4_add_lease(struct file *file, long arg, struct file_lock **lease,
+                         void **priv)
+{
+       struct inode *inode = file_inode(file);
+       fmode_t type = arg == F_RDLCK ? FMODE_READ : FMODE_WRITE;
+       int ret;
+
+       /* No delegation, no lease */
+       if (!nfs4_have_delegation(inode, type))
+               return -ENOLCK;
+       ret = generic_setlease(file, arg, lease, priv);
+       if (ret || nfs4_have_delegation(inode, type))
+               return ret;
+       /* We raced with a delegation return */
+       nfs4_delete_lease(file, priv);
+       return -ENOLCK;
+}
+
+int nfs4_proc_setlease(struct file *file, long arg, struct file_lock **lease,
+                      void **priv)
+{
+       switch (arg) {
+       case F_RDLCK:
+       case F_WRLCK:
+               return nfs4_add_lease(file, arg, lease, priv);
+       case F_UNLCK:
+               return nfs4_delete_lease(file, priv);
+       default:
+               return -EINVAL;
+       }
+}
+
 int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
 {
        struct nfs_server *server = NFS_SERVER(state->inode);