Skip to contentSkip to conversation

lockd: dlm: async lock request changes

PATCH Superseded Alexander Aring 4 participants 27 messages Aug 23, 2023 – Sep 11, 2024

Subsystems (8)
VFS NFS client NFS server OCFS2 +4 more subsystems

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Alexander Aring

author maintainer
#0 lore raw

cover lockd: dlm: async lock request changes

Hi,

I sent this as a PATCH now and drop the RFC. I got some review back from Jeff Layton and hope I was successful to follow it. There are still issues with lockd and asynchronous lock request but it will at least not directly crash when somebody is using nfs on top of GFS2. The issues are related to cancellation of locks/lockd decides to drop nlm_block for some reasons while pending request is happening.

I did not change more documentation about the asynchronous lock request functionality to not confuse users. In my opinion there should be more documentation about what you SHOULD NOT do with this API. Otherwise I think the documentation is still correct.

I will send a follow up patch to fsdevel to change all users using IS_SETLKW() to use FL_SLEEP to determine if it's blocking or non-blocking and send it to fsdevel as it was recommended. This will just be a grep and replace patch and look what happens.

- Alex

changes since RFC:

- drop the pending callback flag but introduce "lockd: don't call vfs_lock_file() for pending requests", see patch why I need it.
- drop per nlm_block callback mutex
- change async lock request documentation
- use always FL_SLEEP to determine if it's blocking vs non-blocking in DLM

Alexander Aring (7):
lockd: introduce safe async lock op
lockd: don't call vfs_lock_file() for pending requests
lockd: fix race in async lock request handling
lockd: add doc to enable EXPORT_OP_SAFE_ASYNC_LOCK
dlm: use fl_owner from lockd
dlm: use FL_SLEEP to determine blocking vs non-blocking
dlm: implement EXPORT_OP_SAFE_ASYNC_LOCK

 fs/dlm/plock.c           | 20 +++++---------------
 fs/gfs2/export.c         |  1 +
 fs/lockd/svclock.c       | 40 +++++++++++++++++++++++++++-------------
 fs/locks.c               | 12 +++++++-----
 fs/nfsd/nfs4state.c      | 13 ++++++++++---
 fs/ocfs2/export.c        |  1 +
 include/linux/exportfs.h |  8 ++++++++
 7 files changed, 59 insertions(+), 36 deletions(-)
Signature · 1 line
2.31.1
author maintainer
#19 lore raw

PATCH 4/7 lockd: add doc to enable EXPORT_OP_SAFE_ASYNC_LOCK

This patch adds a note to enable EXPORT_OP_SAFE_ASYNC_LOCK for asynchronous lock request handling.

---
 fs/locks.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
fs/locks.c +7 -5
index df8b26a42524..edee02d1ca93 100644
@@ -2255,11 +2255,13 @@ int fcntl_getlk(struct file *filp, unsigned int cmd, struct flock *flock)
  * To avoid blocking kernel daemons, such as lockd, that need to acquire POSIX
  * locks, the ->lock() interface may return asynchronously, before the lock has
  * been granted or denied by the underlying filesystem, if (and only if)
- * lm_grant is set. Callers expecting ->lock() to return asynchronously
- * will only use F_SETLK, not F_SETLKW; they will set FL_SLEEP if (and only if)
- * the request is for a blocking lock. When ->lock() does return asynchronously,
- * it must return FILE_LOCK_DEFERRED, and call ->lm_grant() when the lock
- * request completes.
+ * lm_grant is set. Additionally EXPORT_OP_SAFE_ASYNC_LOCK in export_operations
+ * flags need to be set.
+ *
+ * Callers expecting ->lock() to return asynchronously will only use F_SETLK,
+ * not F_SETLKW; they will set FL_SLEEP if (and only if) the request is for a
+ * blocking lock. When ->lock() does return asynchronously, it must return
+ * FILE_LOCK_DEFERRED, and call ->lm_grant() when the lock request completes.
  * If the request is for non-blocking lock the file system should return
  * FILE_LOCK_DEFERRED then try to get the lock and call the callback routine
  * with the result. If the request timed out the callback routine will return a
Signature · 1 line
2.31.1
author maintainer
#13 lore raw

PATCH 2/7 lockd: don't call vfs_lock_file() for pending requests

In reply to Alexander Aring

This patch returns nlm_lck_blocked in nlmsvc_lock() when an asynchronous lock request is pending. During testing I ran into the case with the side-effects that lockd is waiting for only one lm_grant() callback because it's already part of the nlm_blocked list. If another asynchronous for the same nlm_block is triggered two lm_grant() callbacks will occur but lockd was only waiting for one.

To avoid any change of existing users this handling will only being made when export_op_support_safe_async_lock() returns true.

---
 fs/lockd/svclock.c | 24 +++++++++++++++++-------
 1 file changed, 17 insertions(+), 7 deletions(-)
fs/lockd/svclock.c +17 -7
index 6e3b230e8317..aa4174fbaf5b 100644
@@ -531,6 +531,23 @@ nlmsvc_lock(struct svc_rqst *rqstp, struct nlm_file *file,
 		goto out;
 	}
 
+	spin_lock(&nlm_blocked_lock);
+	/*
+	 * If this is a lock request for an already pending
+	 * lock request we return nlm_lck_blocked without calling
+	 * vfs_lock_file() again. Otherwise we have two pending
+	 * requests on the underlaying ->lock() implementation but
+	 * only one nlm_block to being granted by lm_grant().
+	 */
+	if (export_op_support_safe_async_lock(inode->i_sb->s_export_op,
+					      nlmsvc_file_file(file)->f_op) &&
+	    !list_empty(&block->b_list)) {
+		spin_unlock(&nlm_blocked_lock);
+		ret = nlm_lck_blocked;
+		goto out;
+	}
+	spin_unlock(&nlm_blocked_lock);
+
 	if (!wait)
 		lock->fl.fl_flags &= ~FL_SLEEP;
 	mode = lock_to_openmode(&lock->fl);
@@ -543,13 +560,6 @@ nlmsvc_lock(struct svc_rqst *rqstp, struct nlm_file *file,
 			ret = nlm_granted;
 			goto out;
 		case -EAGAIN:
-			/*
-			 * If this is a blocking request for an
-			 * already pending lock request then we need
-			 * to put it back on lockd's block list
-			 */
-			if (wait)
-				break;
 			ret = async_block ? nlm_lck_blocked : nlm_lck_denied;
 			goto out;
 		case FILE_LOCK_DEFERRED:
Signature · 1 line
2.31.1
author maintainer
#16 lore raw

PATCH 3/7 lockd: fix race in async lock request handling

In reply to Alexander Aring

This patch fixes a race in async lock request handling between adding the relevant struct nlm_block to nlm_blocked list after the request was sent by vfs_lock_file() and nlmsvc_grant_deferred() does a lookup of the nlm_block in the nlm_blocked list. It could be that the async request is completed before the nlm_block was added to the list. This would end in a -ENOENT and a kernel log message of "lockd: grant for unknown block".

To solve this issue we add the nlm_block before the vfs_lock_file() call to be sure it has been added when a possible nlmsvc_grant_deferred() is called. If the vfs_lock_file() results in an case when it wouldn't be added to nlm_blocked list, the nlm_block struct will be removed from this list again.

The introducing of the new B_PENDING_CALLBACK nlm_block flag will handle async lock requests on a pending lock requests as a retry on the caller level to hit the -EAGAIN case.

---
 fs/lockd/svclock.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
fs/lockd/svclock.c +8 -3
index aa4174fbaf5b..3b158446203b 100644
@@ -546,6 +546,9 @@ nlmsvc_lock(struct svc_rqst *rqstp, struct nlm_file *file,
 		ret = nlm_lck_blocked;
 		goto out;
 	}
+
+	/* Append to list of blocked */
+	nlmsvc_insert_block_locked(block, NLM_NEVER);
 	spin_unlock(&nlm_blocked_lock);
 
 	if (!wait)
@@ -557,9 +560,12 @@ nlmsvc_lock(struct svc_rqst *rqstp, struct nlm_file *file,
 	dprintk("lockd: vfs_lock_file returned %d\n", error);
 	switch (error) {
 		case 0:
+			nlmsvc_remove_block(block);
 			ret = nlm_granted;
 			goto out;
 		case -EAGAIN:
+			if (!wait)
+				nlmsvc_remove_block(block);
 			ret = async_block ? nlm_lck_blocked : nlm_lck_denied;
 			goto out;
 		case FILE_LOCK_DEFERRED:
@@ -570,17 +576,16 @@ nlmsvc_lock(struct svc_rqst *rqstp, struct nlm_file *file,
 			ret = nlmsvc_defer_lock_rqst(rqstp, block);
 			goto out;
 		case -EDEADLK:
+			nlmsvc_remove_block(block);
 			ret = nlm_deadlock;
 			goto out;
 		default:			/* includes ENOLCK */
+			nlmsvc_remove_block(block);
 			ret = nlm_lck_denied_nolocks;
 			goto out;
 	}
 
 	ret = nlm_lck_blocked;
-
-	/* Append to list of blocked */
-	nlmsvc_insert_block(block, NLM_NEVER);
 out:
 	mutex_unlock(&file->f_mutex);
 	nlmsvc_release_block(block);
Signature · 1 line
2.31.1
author maintainer
#21 lore raw

PATCH 5/7 dlm: use fl_owner from lockd

In reply to Alexander Aring

This patch is changing the fl_owner value in case of an nfs lock request to not be the pid of lockd. Instead this patch changes it to be the owner value that nfs is giving us.

Currently there exists proved problems with this behaviour. One nfsd server was created to export a gfs2 filesystem mount. Two nfs clients doing a nfs mount of this export. Those two clients should conflict each other operating on the same nfs file.

A small test program was written:

int main(int argc, const char *argv[])

{
	struct flock fl = {
		.l_type = F_WRLCK,
		.l_whence = SEEK_SET,
		.l_start = 1L,
		.l_len = 1L,
	};
	int fd;

	fd = open("filename", O_RDWR | O_CREAT, 0700);
	printf("try to lock...\n");
	fcntl(fd, F_SETLKW, &fl);
	printf("locked!\n");
	getc(stdin);

	return 0;
}

Running on both clients at the same time and don't interrupting by pressing any key. It will show that both clients are able to acquire the lock which shouldn't be the case. The issue is here that the fl_owner value is the same and the lock context of both clients should be separated.

This patch lets lockd define how to deal with lock contexts and chose hopefully the right fl_owner value. A test after this patch was made and the locks conflicts each other which should be the case.

---
 fs/dlm/plock.c | 18 ++++--------------
 1 file changed, 4 insertions(+), 14 deletions(-)
fs/dlm/plock.c +4 -14
index 00e1d802a81c..0094fa4004cc 100644
@@ -145,6 +145,7 @@ int dlm_posix_lock(dlm_lockspace_t *lockspace, u64 number, struct file *file,
 	op->info.number		= number;
 	op->info.start		= fl->fl_start;
 	op->info.end		= fl->fl_end;
+	op->info.owner = (__u64)(long)fl->fl_owner;
 	/* async handling */
 	if (fl->fl_lmops && fl->fl_lmops->lm_grant) {
 		op_data = kzalloc(sizeof(*op_data), GFP_NOFS);
@@ -154,9 +155,6 @@ int dlm_posix_lock(dlm_lockspace_t *lockspace, u64 number, struct file *file,
 			goto out;
 		}
 
-		/* fl_owner is lockd which doesn't distinguish
-		   processes on the nfs client */
-		op->info.owner	= (__u64) fl->fl_pid;
 		op_data->callback = fl->fl_lmops->lm_grant;
 		locks_init_lock(&op_data->flc);
 		locks_copy_lock(&op_data->flc, fl);
@@ -168,8 +166,6 @@ int dlm_posix_lock(dlm_lockspace_t *lockspace, u64 number, struct file *file,
 		send_op(op);
 		rv = FILE_LOCK_DEFERRED;
 		goto out;
-	} else {
-		op->info.owner	= (__u64)(long) fl->fl_owner;
 	}
 
 	send_op(op);
@@ -326,10 +322,7 @@ int dlm_posix_unlock(dlm_lockspace_t *lockspace, u64 number, struct file *file,
 	op->info.number		= number;
 	op->info.start		= fl->fl_start;
 	op->info.end		= fl->fl_end;
-	if (fl->fl_lmops && fl->fl_lmops->lm_grant)
-		op->info.owner	= (__u64) fl->fl_pid;
-	else
-		op->info.owner	= (__u64)(long) fl->fl_owner;
+	op->info.owner = (__u64)(long)fl->fl_owner;
 
 	if (fl->fl_flags & FL_CLOSE) {
 		op->info.flags |= DLM_PLOCK_FL_CLOSE;
@@ -389,7 +382,7 @@ int dlm_posix_cancel(dlm_lockspace_t *lockspace, u64 number, struct file *file,
 	info.number = number;
 	info.start = fl->fl_start;
 	info.end = fl->fl_end;
-	info.owner = (__u64)fl->fl_pid;
+	info.owner = (__u64)(long)fl->fl_owner;
 
 	rv = do_lock_cancel(&info);
 	switch (rv) {
@@ -450,10 +443,7 @@ int dlm_posix_get(dlm_lockspace_t *lockspace, u64 number, struct file *file,
 	op->info.number		= number;
 	op->info.start		= fl->fl_start;
 	op->info.end		= fl->fl_end;
-	if (fl->fl_lmops && fl->fl_lmops->lm_grant)
-		op->info.owner	= (__u64) fl->fl_pid;
-	else
-		op->info.owner	= (__u64)(long) fl->fl_owner;
+	op->info.owner = (__u64)(long)fl->fl_owner;
 
 	send_op(op);
 	wait_event(recv_wq, (op->done != 0));
Signature · 1 line
2.31.1
author maintainer
#1 lore raw

PATCH 1/7 lockd: introduce safe async lock op

In reply to Alexander Aring

This patch reverts mostly commit 40595cdc93ed ("nfs: block notification on fs with its own ->lock") and introduces an EXPORT_OP_SAFE_ASYNC_LOCK export flag to signal that the "own ->lock" implementation supports async lock requests. The only main user is DLM that is used by GFS2 and OCFS2 filesystem. Those implement their own lock() implementation and return FILE_LOCK_DEFERRED as return value. Since commit 40595cdc93ed ("nfs: block notification on fs with its own ->lock") the DLM implementation were never updated. This patch should prepare for DLM to set the EXPORT_OP_SAFE_ASYNC_LOCK export flag and update the DLM plock implementation regarding to it.

---
 fs/lockd/svclock.c       |  5 ++---
 fs/nfsd/nfs4state.c      | 13 ++++++++++---
 include/linux/exportfs.h |  8 ++++++++
 3 files changed, 20 insertions(+), 6 deletions(-)
fs/lockd/svclock.c +2 -3
index c43ccdf28ed9..6e3b230e8317 100644
@@ -470,9 +470,7 @@ nlmsvc_lock(struct svc_rqst *rqstp, struct nlm_file *file,
 	    struct nlm_host *host, struct nlm_lock *lock, int wait,
 	    struct nlm_cookie *cookie, int reclaim)
 {
-#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
 	struct inode		*inode = nlmsvc_file_inode(file);
-#endif
 	struct nlm_block	*block = NULL;
 	int			error;
 	int			mode;
@@ -486,7 +484,8 @@ nlmsvc_lock(struct svc_rqst *rqstp, struct nlm_file *file,
 				(long long)lock->fl.fl_end,
 				wait);
 
-	if (nlmsvc_file_file(file)->f_op->lock) {
+	if (!export_op_support_safe_async_lock(inode->i_sb->s_export_op,
+					       nlmsvc_file_file(file)->f_op)) {
 		async_block = wait;
 		wait = 0;
 	}
fs/nfsd/nfs4state.c +10 -3
index 3aefbad4cc09..14ca06424ff1 100644
@@ -7430,6 +7430,7 @@ nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
 	struct nfsd4_blocked_lock *nbl = NULL;
 	struct file_lock *file_lock = NULL;
 	struct file_lock *conflock = NULL;
+	struct super_block *sb;
 	__be32 status = 0;
 	int lkflg;
 	int err;
@@ -7451,6 +7452,7 @@ nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
 		dprintk("NFSD: nfsd4_lock: permission denied!\n");
 		return status;
 	}
+	sb = cstate->current_fh.fh_dentry->d_sb;
 
 	if (lock->lk_is_new) {
 		if (nfsd4_has_session(cstate))
@@ -7502,7 +7504,9 @@ nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
 	fp = lock_stp->st_stid.sc_file;
 	switch (lock->lk_type) {
 		case NFS4_READW_LT:
-			if (nfsd4_has_session(cstate))
+			if (nfsd4_has_session(cstate) ||
+			    export_op_support_safe_async_lock(sb->s_export_op,
+							      nf->nf_file->f_op))
 				fl_flags |= FL_SLEEP;
 			fallthrough;
 		case NFS4_READ_LT:
@@ -7514,7 +7518,9 @@ nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
 			fl_type = F_RDLCK;
 			break;
 		case NFS4_WRITEW_LT:
-			if (nfsd4_has_session(cstate))
+			if (nfsd4_has_session(cstate) ||
+			    export_op_support_safe_async_lock(sb->s_export_op,
+							      nf->nf_file->f_op))
 				fl_flags |= FL_SLEEP;
 			fallthrough;
 		case NFS4_WRITE_LT:
@@ -7542,7 +7548,8 @@ nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
 	 * for file locks), so don't attempt blocking lock notifications
 	 * on those filesystems:
 	 */
-	if (nf->nf_file->f_op->lock)
+	if (!export_op_support_safe_async_lock(sb->s_export_op,
+					       nf->nf_file->f_op))
 		fl_flags &= ~FL_SLEEP;
 
 	nbl = find_or_allocate_block(lock_sop, &fp->fi_fhandle, nn);
include/linux/exportfs.h +8 -0
index 11fbd0ee1370..10358a93cdc1 100644
@@ -3,6 +3,7 @@
 #define LINUX_EXPORTFS_H 1
 
 #include <linux/types.h>
+#include <linux/fs.h>
 
 struct dentry;
 struct iattr;
@@ -224,9 +225,16 @@ struct export_operations {
 						  atomic attribute updates
 						*/
 #define EXPORT_OP_FLUSH_ON_CLOSE	(0x20) /* fs flushes file data on close */
+#define EXPORT_OP_SAFE_ASYNC_LOCK	(0x40) /* fs can do async lock request */
 	unsigned long	flags;
 };
 
+static inline bool export_op_support_safe_async_lock(const struct export_operations *export_ops,
+						     const struct file_operations *f_op)
+{
+	return (export_ops->flags & EXPORT_OP_SAFE_ASYNC_LOCK) || !f_op->lock;
+}
+
 extern int exportfs_encode_inode_fh(struct inode *inode, struct fid *fid,
 				    int *max_len, struct inode *parent,
 				    int flags);
Signature · 1 line
2.31.1
author maintainer
#26 lore raw

PATCH 7/7 dlm: implement EXPORT_OP_SAFE_ASYNC_LOCK

In reply to Alexander Aring

This patch is activating the EXPORT_OP_SAFE_ASYNC_LOCK export flag to signal lockd that both filesystems are able to handle async lock requests. The cluster filesystems gfs2 and ocfs2 will redirect their lock requests to DLMs plock implementation that can handle async lock requests.

---
 fs/gfs2/export.c  | 1 +
 fs/ocfs2/export.c | 1 +
 2 files changed, 2 insertions(+)
fs/gfs2/export.c +1 -0
index cf40895233f5..36bc43b9d141 100644
@@ -192,5 +192,6 @@ const struct export_operations gfs2_export_ops = {
 	.fh_to_parent = gfs2_fh_to_parent,
 	.get_name = gfs2_get_name,
 	.get_parent = gfs2_get_parent,
+	.flags = EXPORT_OP_SAFE_ASYNC_LOCK,
 };
 
fs/ocfs2/export.c +1 -0
index eaa8c80ace3c..8a1169e01dd9 100644
@@ -280,4 +280,5 @@ const struct export_operations ocfs2_export_ops = {
 	.fh_to_dentry	= ocfs2_fh_to_dentry,
 	.fh_to_parent	= ocfs2_fh_to_parent,
 	.get_parent	= ocfs2_get_parent,
+	.flags		= EXPORT_OP_SAFE_ASYNC_LOCK,
 };
Signature · 1 line
2.31.1
author maintainer
#22 lore raw

PATCH 6/7 dlm: use FL_SLEEP to determine blocking vs non-blocking

In reply to Alexander Aring

This patch uses the FL_SLEEP flag in struct file_lock to determine if the lock request is a blocking or non-blocking request. Before dlm was using IS_SETLKW() was being used which is not usable for lock requests coming from lockd when EXPORT_OP_SAFE_ASYNC_LOCK inside the export flags is set.

---
 fs/dlm/plock.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/dlm/plock.c +1 -1
index 0094fa4004cc..0c6ed5eeb840 100644
@@ -140,7 +140,7 @@ int dlm_posix_lock(dlm_lockspace_t *lockspace, u64 number, struct file *file,
 	op->info.optype		= DLM_PLOCK_OP_LOCK;
 	op->info.pid		= fl->fl_pid;
 	op->info.ex		= (fl->fl_type == F_WRLCK);
-	op->info.wait		= IS_SETLKW(cmd);
+	op->info.wait		= !!(fl->fl_flags & FL_SLEEP);
 	op->info.fsid		= ls->ls_global_id;
 	op->info.number		= number;
 	op->info.start		= fl->fl_start;
Signature · 1 line
2.31.1
#2 lore raw

Re: [PATCH 1/7] lockd: introduce safe async lock op

In reply to Alexander Aring
Quoted 11 lines · Alexander Aring

This patch reverts mostly commit 40595cdc93ed ("nfs: block notification on fs with its own ->lock") and introduces an EXPORT_OP_SAFE_ASYNC_LOCK export flag to signal that the "own ->lock" implementation supports async lock requests. The only main user is DLM that is used by GFS2 and OCFS2 filesystem. Those implement their own lock() implementation and return FILE_LOCK_DEFERRED as return value. Since commit 40595cdc93ed ("nfs: block notification on fs with its own ->lock") the DLM implementation were never updated. This patch should prepare for DLM to set the EXPORT_OP_SAFE_ASYNC_LOCK export flag and update the DLM plock implementation regarding to it.

Quoted 7 lines
---
 fs/lockd/svclock.c       |  5 ++---
 fs/nfsd/nfs4state.c      | 13 ++++++++++---
 include/linux/exportfs.h |  8 ++++++++
 3 files changed, 20 insertions(+), 6 deletions(-)

I'm starting to look at these. Just so you know, it's too late for inclusion in v6.6, but I think we can get these into shape for v6.7.

More below.

Quoted 89 lines
fs/lockd/svclock.c +2 -3
index c43ccdf28ed9..6e3b230e8317 100644
@@ -470,9 +470,7 @@ nlmsvc_lock(struct svc_rqst *rqstp, struct nlm_file *file,
 	    struct nlm_host *host, struct nlm_lock *lock, int wait,
 	    struct nlm_cookie *cookie, int reclaim)
 {
-#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
 	struct inode		*inode = nlmsvc_file_inode(file);
-#endif
 	struct nlm_block	*block = NULL;
 	int			error;
 	int			mode;
@@ -486,7 +484,8 @@ nlmsvc_lock(struct svc_rqst *rqstp, struct nlm_file *file,
 				(long long)lock->fl.fl_end,
 				wait);
 
-	if (nlmsvc_file_file(file)->f_op->lock) {
+	if (!export_op_support_safe_async_lock(inode->i_sb->s_export_op,
+					       nlmsvc_file_file(file)->f_op)) {
 		async_block = wait;
 		wait = 0;
 	}
fs/nfsd/nfs4state.c +10 -3
index 3aefbad4cc09..14ca06424ff1 100644
@@ -7430,6 +7430,7 @@ nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
 	struct nfsd4_blocked_lock *nbl = NULL;
 	struct file_lock *file_lock = NULL;
 	struct file_lock *conflock = NULL;
+	struct super_block *sb;
 	__be32 status = 0;
 	int lkflg;
 	int err;
@@ -7451,6 +7452,7 @@ nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
 		dprintk("NFSD: nfsd4_lock: permission denied!\n");
 		return status;
 	}
+	sb = cstate->current_fh.fh_dentry->d_sb;
 
 	if (lock->lk_is_new) {
 		if (nfsd4_has_session(cstate))
@@ -7502,7 +7504,9 @@ nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
 	fp = lock_stp->st_stid.sc_file;
 	switch (lock->lk_type) {
 		case NFS4_READW_LT:
-			if (nfsd4_has_session(cstate))
+			if (nfsd4_has_session(cstate) ||
+			    export_op_support_safe_async_lock(sb->s_export_op,
+							      nf->nf_file->f_op))
 				fl_flags |= FL_SLEEP;
 			fallthrough;
 		case NFS4_READ_LT:
@@ -7514,7 +7518,9 @@ nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
 			fl_type = F_RDLCK;
 			break;
 		case NFS4_WRITEW_LT:
-			if (nfsd4_has_session(cstate))
+			if (nfsd4_has_session(cstate) ||
+			    export_op_support_safe_async_lock(sb->s_export_op,
+							      nf->nf_file->f_op))
 				fl_flags |= FL_SLEEP;
 			fallthrough;
 		case NFS4_WRITE_LT:
@@ -7542,7 +7548,8 @@ nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
 	 * for file locks), so don't attempt blocking lock notifications
 	 * on those filesystems:
 	 */
-	if (nf->nf_file->f_op->lock)
+	if (!export_op_support_safe_async_lock(sb->s_export_op,
+					       nf->nf_file->f_op))
 		fl_flags &= ~FL_SLEEP;
 
 	nbl = find_or_allocate_block(lock_sop, &fp->fi_fhandle, nn);
include/linux/exportfs.h +1 -0
index 11fbd0ee1370..10358a93cdc1 100644
@@ -3,6 +3,7 @@
 #define LINUX_EXPORTFS_H 1
 
 #include <linux/types.h>
+#include <linux/fs.h>
 
 struct dentry;
 struct iattr;

include/linux/exportfs.h struct export_operations { · line 228

Quoted 4 lines
include/linux/exportfs.h +1 -0
@@ -224,9 +225,16 @@ struct export_operations {
 						  atomic attribute updates
 						*/
 #define EXPORT_OP_FLUSH_ON_CLOSE	(0x20) /* fs flushes file data on close */
+#define EXPORT_OP_SAFE_ASYNC_LOCK	(0x40) /* fs can do async lock request */

We haven't been good about this recently, but the addition of new EXPORT_OP flags need to be accompanied by updates to Documentation/filesystems/nfs/exporting.rst.

I will see about adding documentation for other recent flags, but please include an update to exporting.rst with this patch.

I'm not sure we need _SAFE_ in the flag name. Would EXPORT_OP_ASYNC_LOCK be OK with you?

include/linux/exportfs.h struct export_operations { · line 237

Quoted 9 lines
include/linux/exportfs.h +6 -0
 	unsigned long	flags;
 };
 
+static inline bool export_op_support_safe_async_lock(const struct export_operations *export_ops,
+						     const struct file_operations *f_op)
+{
+	return (export_ops->flags & EXPORT_OP_SAFE_ASYNC_LOCK) || !f_op->lock;
+}
+

I'd like some cosmetic changes to this API, since this seems to be the first utility function for checking EXPORT_OP flags.

- The function name is unwieldy. How about exportfs_lock_op_is_async() ?

- Break up the long lines. It's OK with me if the return value type is left on a different line than the function name and parameters.

- This function is globally visible, so a kdoc comment is needed.

- The f_op->lock check is common to all the call sites, but it is not at all related to the export AFAICT. Can it be removed from this inline function?

Quoted 6 lines
include/linux/exportfs.h +0 -0
 extern int exportfs_encode_inode_fh(struct inode *inode, struct fid *fid,
 				    int *max_len, struct inode *parent,
 				    int flags);
Signature · 1 line
2.31.1
Signature · 1 line
Chuck Lever
#17 lore raw

Re: [PATCH 3/7] lockd: fix race in async lock request handling

In reply to Alexander Aring
Quoted 14 lines · Alexander Aring

This patch fixes a race in async lock request handling between adding the relevant struct nlm_block to nlm_blocked list after the request was sent by vfs_lock_file() and nlmsvc_grant_deferred() does a lookup of the nlm_block in the nlm_blocked list. It could be that the async request is completed before the nlm_block was added to the list. This would end in a -ENOENT and a kernel log message of "lockd: grant for unknown block".

To solve this issue we add the nlm_block before the vfs_lock_file() call to be sure it has been added when a possible nlmsvc_grant_deferred() is called. If the vfs_lock_file() results in an case when it wouldn't be added to nlm_blocked list, the nlm_block struct will be removed from this list again.

Quoted 3 lines

The introducing of the new B_PENDING_CALLBACK nlm_block flag will handle async lock requests on a pending lock requests as a retry on the caller level to hit the -EAGAIN case.

This last paragraph might be obsolete? I don't see a B_PENDING_CALLBACK flag in the kernel or in this patch series.

Quoted 55 lines
---
 fs/lockd/svclock.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
fs/lockd/svclock.c +8 -3
index aa4174fbaf5b..3b158446203b 100644
@@ -546,6 +546,9 @@ nlmsvc_lock(struct svc_rqst *rqstp, struct nlm_file *file,
 		ret = nlm_lck_blocked;
 		goto out;
 	}
+
+	/* Append to list of blocked */
+	nlmsvc_insert_block_locked(block, NLM_NEVER);
 	spin_unlock(&nlm_blocked_lock);
 
 	if (!wait)
@@ -557,9 +560,12 @@ nlmsvc_lock(struct svc_rqst *rqstp, struct nlm_file *file,
 	dprintk("lockd: vfs_lock_file returned %d\n", error);
 	switch (error) {
 		case 0:
+			nlmsvc_remove_block(block);
 			ret = nlm_granted;
 			goto out;
 		case -EAGAIN:
+			if (!wait)
+				nlmsvc_remove_block(block);
 			ret = async_block ? nlm_lck_blocked : nlm_lck_denied;
 			goto out;
 		case FILE_LOCK_DEFERRED:
@@ -570,17 +576,16 @@ nlmsvc_lock(struct svc_rqst *rqstp, struct nlm_file *file,
 			ret = nlmsvc_defer_lock_rqst(rqstp, block);
 			goto out;
 		case -EDEADLK:
+			nlmsvc_remove_block(block);
 			ret = nlm_deadlock;
 			goto out;
 		default:			/* includes ENOLCK */
+			nlmsvc_remove_block(block);
 			ret = nlm_lck_denied_nolocks;
 			goto out;
 	}
 
 	ret = nlm_lck_blocked;
-
-	/* Append to list of blocked */
-	nlmsvc_insert_block(block, NLM_NEVER);
 out:
 	mutex_unlock(&file->f_mutex);
 	nlmsvc_release_block(block);
Signature · 1 line
2.31.1
Signature · 1 line
Chuck Lever
maintainer Reviewed-by on v1 2/7
#14 lore raw

Re: [PATCH 2/7] lockd: don't call vfs_lock_file() for pending requests

In reply to Alexander Aring
Quoted 28 lines · Alexander Aring

This patch returns nlm_lck_blocked in nlmsvc_lock() when an asynchronous lock request is pending. During testing I ran into the case with the side-effects that lockd is waiting for only one lm_grant() callback because it's already part of the nlm_blocked list. If another asynchronous for the same nlm_block is triggered two lm_grant() callbacks will occur but lockd was only waiting for one.

To avoid any change of existing users this handling will only being made when export_op_support_safe_async_lock() returns true.

---
 fs/lockd/svclock.c | 24 +++++++++++++++++-------
 1 file changed, 17 insertions(+), 7 deletions(-)
fs/lockd/svclock.c +5 -0
index 6e3b230e8317..aa4174fbaf5b 100644
@@ -531,6 +531,23 @@ nlmsvc_lock(struct svc_rqst *rqstp, struct nlm_file *file,
 		goto out;
 	}
 
+	spin_lock(&nlm_blocked_lock);
+	/*
+	 * If this is a lock request for an already pending
+	 * lock request we return nlm_lck_blocked without calling
+	 * vfs_lock_file() again. Otherwise we have two pending

fs/lockd/svclock.c nlmsvc_lock(struct svc_rqst *rqstp, struct nlm_file *file, · line 548

Quoted 10 lines
fs/lockd/svclock.c +10 -0
+	 * requests on the underlaying ->lock() implementation but
+	 * only one nlm_block to being granted by lm_grant().
+	 */
+	if (export_op_support_safe_async_lock(inode->i_sb->s_export_op,
+					      nlmsvc_file_file(file)->f_op) &&
+	    !list_empty(&block->b_list)) {
+		spin_unlock(&nlm_blocked_lock);
+		ret = nlm_lck_blocked;
+		goto out;
+	}

Looks reasonable. The block->b_list check is subtle, but the comment helps.

Quoted 9 lines
fs/lockd/svclock.c +2 -0
+	spin_unlock(&nlm_blocked_lock);
+
 	if (!wait)
 		lock->fl.fl_flags &= ~FL_SLEEP;
 	mode = lock_to_openmode(&lock->fl);
@@ -543,13 +560,6 @@ nlmsvc_lock(struct svc_rqst *rqstp, struct nlm_file *file,
 			ret = nlm_granted;
 			goto out;
 		case -EAGAIN:

fs/lockd/svclock.c nlmsvc_lock(struct svc_rqst *rqstp, struct nlm_file *file, · line 565

Quoted 10 lines
fs/lockd/svclock.c +0 -7
-			/*
-			 * If this is a blocking request for an
-			 * already pending lock request then we need
-			 * to put it back on lockd's block list
-			 */
-			if (wait)
-				break;
 			ret = async_block ? nlm_lck_blocked : nlm_lck_denied;
 			goto out;
 		case FILE_LOCK_DEFERRED:
maintainer
#5 lore raw

Re: [PATCH 1/7] lockd: introduce safe async lock op

In reply to Alexander Aring
Quoted 114 lines · Alexander Aring

This patch reverts mostly commit 40595cdc93ed ("nfs: block notification on fs with its own ->lock") and introduces an EXPORT_OP_SAFE_ASYNC_LOCK export flag to signal that the "own ->lock" implementation supports async lock requests. The only main user is DLM that is used by GFS2 and OCFS2 filesystem. Those implement their own lock() implementation and return FILE_LOCK_DEFERRED as return value. Since commit 40595cdc93ed ("nfs: block notification on fs with its own ->lock") the DLM implementation were never updated. This patch should prepare for DLM to set the EXPORT_OP_SAFE_ASYNC_LOCK export flag and update the DLM plock implementation regarding to it.

---
 fs/lockd/svclock.c       |  5 ++---
 fs/nfsd/nfs4state.c      | 13 ++++++++++---
 include/linux/exportfs.h |  8 ++++++++
 3 files changed, 20 insertions(+), 6 deletions(-)
fs/lockd/svclock.c +2 -3
index c43ccdf28ed9..6e3b230e8317 100644
@@ -470,9 +470,7 @@ nlmsvc_lock(struct svc_rqst *rqstp, struct nlm_file *file,
 	    struct nlm_host *host, struct nlm_lock *lock, int wait,
 	    struct nlm_cookie *cookie, int reclaim)
 {
-#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
 	struct inode		*inode = nlmsvc_file_inode(file);
-#endif
 	struct nlm_block	*block = NULL;
 	int			error;
 	int			mode;
@@ -486,7 +484,8 @@ nlmsvc_lock(struct svc_rqst *rqstp, struct nlm_file *file,
 				(long long)lock->fl.fl_end,
 				wait);
 
-	if (nlmsvc_file_file(file)->f_op->lock) {
+	if (!export_op_support_safe_async_lock(inode->i_sb->s_export_op,
+					       nlmsvc_file_file(file)->f_op)) {
 		async_block = wait;
 		wait = 0;
 	}
fs/nfsd/nfs4state.c +10 -3
index 3aefbad4cc09..14ca06424ff1 100644
@@ -7430,6 +7430,7 @@ nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
 	struct nfsd4_blocked_lock *nbl = NULL;
 	struct file_lock *file_lock = NULL;
 	struct file_lock *conflock = NULL;
+	struct super_block *sb;
 	__be32 status = 0;
 	int lkflg;
 	int err;
@@ -7451,6 +7452,7 @@ nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
 		dprintk("NFSD: nfsd4_lock: permission denied!\n");
 		return status;
 	}
+	sb = cstate->current_fh.fh_dentry->d_sb;
 
 	if (lock->lk_is_new) {
 		if (nfsd4_has_session(cstate))
@@ -7502,7 +7504,9 @@ nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
 	fp = lock_stp->st_stid.sc_file;
 	switch (lock->lk_type) {
 		case NFS4_READW_LT:
-			if (nfsd4_has_session(cstate))
+			if (nfsd4_has_session(cstate) ||
+			    export_op_support_safe_async_lock(sb->s_export_op,
+							      nf->nf_file->f_op))
 				fl_flags |= FL_SLEEP;
 			fallthrough;
 		case NFS4_READ_LT:
@@ -7514,7 +7518,9 @@ nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
 			fl_type = F_RDLCK;
 			break;
 		case NFS4_WRITEW_LT:
-			if (nfsd4_has_session(cstate))
+			if (nfsd4_has_session(cstate) ||
+			    export_op_support_safe_async_lock(sb->s_export_op,
+							      nf->nf_file->f_op))
 				fl_flags |= FL_SLEEP;
 			fallthrough;
 		case NFS4_WRITE_LT:
@@ -7542,7 +7548,8 @@ nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
 	 * for file locks), so don't attempt blocking lock notifications
 	 * on those filesystems:
 	 */
-	if (nf->nf_file->f_op->lock)
+	if (!export_op_support_safe_async_lock(sb->s_export_op,
+					       nf->nf_file->f_op))
 		fl_flags &= ~FL_SLEEP;
 
 	nbl = find_or_allocate_block(lock_sop, &fp->fi_fhandle, nn);
include/linux/exportfs.h +2 -0
index 11fbd0ee1370..10358a93cdc1 100644
@@ -3,6 +3,7 @@
 #define LINUX_EXPORTFS_H 1
 
 #include <linux/types.h>
+#include <linux/fs.h>
 
 struct dentry;
 struct iattr;
@@ -224,9 +225,16 @@ struct export_operations {
 						  atomic attribute updates
 						*/
 #define EXPORT_OP_FLUSH_ON_CLOSE	(0x20) /* fs flushes file data on close */
+#define EXPORT_OP_SAFE_ASYNC_LOCK	(0x40) /* fs can do async lock request */
 	unsigned long	flags;
 };

include/linux/exportfs.h struct export_operations { · line 240

Quoted 10 lines
include/linux/exportfs.h +6 -0
 
+static inline bool export_op_support_safe_async_lock(const struct export_operations *export_ops,
+						     const struct file_operations *f_op)
+{
+	return (export_ops->flags & EXPORT_OP_SAFE_ASYNC_LOCK) || !f_op->lock;
+}
+
 extern int exportfs_encode_inode_fh(struct inode *inode, struct fid *fid,
 				    int *max_len, struct inode *parent,
 				    int flags);

Conceptually, this looks fine, but I agree with Chuck's stylistic points.

Signature · 1 line
Jeff Layton <[email protected]>
maintainer Reviewed-by on v1 3/7
#18 lore raw

Re: [PATCH 3/7] lockd: fix race in async lock request handling

In reply to Alexander Aring
Quoted 60 lines · Alexander Aring

This patch fixes a race in async lock request handling between adding the relevant struct nlm_block to nlm_blocked list after the request was sent by vfs_lock_file() and nlmsvc_grant_deferred() does a lookup of the nlm_block in the nlm_blocked list. It could be that the async request is completed before the nlm_block was added to the list. This would end in a -ENOENT and a kernel log message of "lockd: grant for unknown block".

To solve this issue we add the nlm_block before the vfs_lock_file() call to be sure it has been added when a possible nlmsvc_grant_deferred() is called. If the vfs_lock_file() results in an case when it wouldn't be added to nlm_blocked list, the nlm_block struct will be removed from this list again.

The introducing of the new B_PENDING_CALLBACK nlm_block flag will handle async lock requests on a pending lock requests as a retry on the caller level to hit the -EAGAIN case.

---
 fs/lockd/svclock.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
fs/lockd/svclock.c +8 -0
index aa4174fbaf5b..3b158446203b 100644
@@ -546,6 +546,9 @@ nlmsvc_lock(struct svc_rqst *rqstp, struct nlm_file *file,
 		ret = nlm_lck_blocked;
 		goto out;
 	}
+
+	/* Append to list of blocked */
+	nlmsvc_insert_block_locked(block, NLM_NEVER);
 	spin_unlock(&nlm_blocked_lock);
 
 	if (!wait)
@@ -557,9 +560,12 @@ nlmsvc_lock(struct svc_rqst *rqstp, struct nlm_file *file,
 	dprintk("lockd: vfs_lock_file returned %d\n", error);
 	switch (error) {
 		case 0:
+			nlmsvc_remove_block(block);
 			ret = nlm_granted;
 			goto out;
 		case -EAGAIN:
+			if (!wait)
+				nlmsvc_remove_block(block);
 			ret = async_block ? nlm_lck_blocked : nlm_lck_denied;
 			goto out;
 		case FILE_LOCK_DEFERRED:
@@ -570,17 +576,16 @@ nlmsvc_lock(struct svc_rqst *rqstp, struct nlm_file *file,
 			ret = nlmsvc_defer_lock_rqst(rqstp, block);
 			goto out;
 		case -EDEADLK:
+			nlmsvc_remove_block(block);
 			ret = nlm_deadlock;
 			goto out;
 		default:			/* includes ENOLCK */
+			nlmsvc_remove_block(block);
 			ret = nlm_lck_denied_nolocks;

fs/lockd/svclock.c nlmsvc_lock(struct svc_rqst *rqstp, struct nlm_file *file, · line 591

Quoted 10 lines
fs/lockd/svclock.c +0 -3
 			goto out;
 	}
 
 	ret = nlm_lck_blocked;
-
-	/* Append to list of blocked */
-	nlmsvc_insert_block(block, NLM_NEVER);
 out:
 	mutex_unlock(&file->f_mutex);
 	nlmsvc_release_block(block);
maintainer Reviewed-by on v1 4/7
#20 lore raw

Re: [PATCH 4/7] lockd: add doc to enable EXPORT_OP_SAFE_ASYNC_LOCK

In reply to Alexander Aring
Quoted 21 lines · Alexander Aring

This patch adds a note to enable EXPORT_OP_SAFE_ASYNC_LOCK for asynchronous lock request handling.

---
 fs/locks.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
fs/locks.c +0 -5
index df8b26a42524..edee02d1ca93 100644
@@ -2255,11 +2255,13 @@ int fcntl_getlk(struct file *filp, unsigned int cmd, struct flock *flock)
  * To avoid blocking kernel daemons, such as lockd, that need to acquire POSIX
  * locks, the ->lock() interface may return asynchronously, before the lock has
  * been granted or denied by the underlying filesystem, if (and only if)
- * lm_grant is set. Callers expecting ->lock() to return asynchronously
- * will only use F_SETLK, not F_SETLKW; they will set FL_SLEEP if (and only if)
- * the request is for a blocking lock. When ->lock() does return asynchronously,
- * it must return FILE_LOCK_DEFERRED, and call ->lm_grant() when the lock
- * request completes.

fs/locks.c int fcntl_getlk(struct file *filp, unsigned int cmd, struct flock *flock) · line 2267

Quoted 10 lines
fs/locks.c +7 -0
+ * lm_grant is set. Additionally EXPORT_OP_SAFE_ASYNC_LOCK in export_operations
+ * flags need to be set.
+ *
+ * Callers expecting ->lock() to return asynchronously will only use F_SETLK,
+ * not F_SETLKW; they will set FL_SLEEP if (and only if) the request is for a
+ * blocking lock. When ->lock() does return asynchronously, it must return
+ * FILE_LOCK_DEFERRED, and call ->lm_grant() when the lock request completes.
  * If the request is for non-blocking lock the file system should return
  * FILE_LOCK_DEFERRED then try to get the lock and call the callback routine
  * with the result. If the request timed out the callback routine will return a
maintainer Reviewed-by on v1 6/7
#23 lore raw

Re: [PATCH 6/7] dlm: use FL_SLEEP to determine blocking vs non-blocking

In reply to Alexander Aring
Quoted 16 lines · Alexander Aring

This patch uses the FL_SLEEP flag in struct file_lock to determine if the lock request is a blocking or non-blocking request. Before dlm was using IS_SETLKW() was being used which is not usable for lock requests coming from lockd when EXPORT_OP_SAFE_ASYNC_LOCK inside the export flags is set.

---
 fs/dlm/plock.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/dlm/plock.c +0 -0
index 0094fa4004cc..0c6ed5eeb840 100644

fs/dlm/plock.c int dlm_posix_lock(dlm_lockspace_t *lockspace, u64 number, struct file *file, · line 146

Quoted 8 lines
fs/dlm/plock.c +1 -1
@@ -140,7 +140,7 @@ int dlm_posix_lock(dlm_lockspace_t *lockspace, u64 number, struct file *file,
 	op->info.optype		= DLM_PLOCK_OP_LOCK;
 	op->info.pid		= fl->fl_pid;
 	op->info.ex		= (fl->fl_type == F_WRLCK);
-	op->info.wait		= IS_SETLKW(cmd);
+	op->info.wait		= !!(fl->fl_flags & FL_SLEEP);
 	op->info.fsid		= ls->ls_global_id;
 	op->info.number		= number;
 	op->info.start		= fl->fl_start;

Not sure you really need the !!, but ok...

author maintainer
#15 lore raw

Re: [PATCH 2/7] lockd: don't call vfs_lock_file() for pending requests

In reply to Jeff Layton

Hi,

Quoted 35 lines · Jeff Layton
Quoted 33 lines · Alexander Aring

This patch returns nlm_lck_blocked in nlmsvc_lock() when an asynchronous lock request is pending. During testing I ran into the case with the side-effects that lockd is waiting for only one lm_grant() callback because it's already part of the nlm_blocked list. If another asynchronous for the same nlm_block is triggered two lm_grant() callbacks will occur but lockd was only waiting for one.

To avoid any change of existing users this handling will only being made when export_op_support_safe_async_lock() returns true.

---
 fs/lockd/svclock.c | 24 +++++++++++++++++-------
 1 file changed, 17 insertions(+), 7 deletions(-)
fs/lockd/svclock.c +10 -0
index 6e3b230e8317..aa4174fbaf5b 100644
@@ -531,6 +531,23 @@ nlmsvc_lock(struct svc_rqst *rqstp, struct nlm_file *file,
              goto out;
      }
 
+     spin_lock(&nlm_blocked_lock);
+     /*
+      * If this is a lock request for an already pending
+      * lock request we return nlm_lck_blocked without calling
+      * vfs_lock_file() again. Otherwise we have two pending
+      * requests on the underlaying ->lock() implementation but
+      * only one nlm_block to being granted by lm_grant().
+      */
+     if (export_op_support_safe_async_lock(inode->i_sb->s_export_op,
+                                           nlmsvc_file_file(file)->f_op) &&
Quoted 8 lines

fs/lockd/svclock.c nlmsvc_lock(struct svc_rqst *rqstp, struct nlm_file *file, · line 548

Quoted 5 lines
fs/lockd/svclock.c +5 -0
+         !list_empty(&block->b_list)) {
+             spin_unlock(&nlm_blocked_lock);
+             ret = nlm_lck_blocked;
+             goto out;
+     }

Looks reasonable. The block->b_list check is subtle, but the comment helps.

thanks. To be honest, I am "a little bit" worried (I am thinking of this scenario) that we might have a problem here with multiple identically lock requests being granted at the same time. In such cases the most fields of struct file_lock are mostly the same and nlm_compare_locks() checks exactly on those fields. I am concerned this corner case could cause problems, but it is a very rare case and it makes totally no sense that an application is doing such a request.

I am currently trying to get an xfstest for this upstream.

- Alex

author maintainer
#3 lore raw

Re: [PATCH 1/7] lockd: introduce safe async lock op

In reply to Chuck Lever

Hi,

Quoted 21 lines · Chuck Lever
Quoted 11 lines · Alexander Aring

This patch reverts mostly commit 40595cdc93ed ("nfs: block notification on fs with its own ->lock") and introduces an EXPORT_OP_SAFE_ASYNC_LOCK export flag to signal that the "own ->lock" implementation supports async lock requests. The only main user is DLM that is used by GFS2 and OCFS2 filesystem. Those implement their own lock() implementation and return FILE_LOCK_DEFERRED as return value. Since commit 40595cdc93ed ("nfs: block notification on fs with its own ->lock") the DLM implementation were never updated. This patch should prepare for DLM to set the EXPORT_OP_SAFE_ASYNC_LOCK export flag and update the DLM plock implementation regarding to it.

Quoted 7 lines
---
 fs/lockd/svclock.c       |  5 ++---
 fs/nfsd/nfs4state.c      | 13 ++++++++++---
 include/linux/exportfs.h |  8 ++++++++
 3 files changed, 20 insertions(+), 6 deletions(-)
Quoted 3 lines

I'm starting to look at these. Just so you know, it's too late for inclusion in v6.6, but I think we can get these into shape for v6.7.

ok. I base my work on [0], is this correct?

Quoted 89 lines

More below.

Quoted 86 lines
fs/lockd/svclock.c +2 -3
index c43ccdf28ed9..6e3b230e8317 100644
@@ -470,9 +470,7 @@ nlmsvc_lock(struct svc_rqst *rqstp, struct nlm_file *file,
          struct nlm_host *host, struct nlm_lock *lock, int wait,
          struct nlm_cookie *cookie, int reclaim)
 {
-#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
      struct inode            *inode = nlmsvc_file_inode(file);
-#endif
      struct nlm_block        *block = NULL;
      int                     error;
      int                     mode;
@@ -486,7 +484,8 @@ nlmsvc_lock(struct svc_rqst *rqstp, struct nlm_file *file,
                              (long long)lock->fl.fl_end,
                              wait);
 
-     if (nlmsvc_file_file(file)->f_op->lock) {
+     if (!export_op_support_safe_async_lock(inode->i_sb->s_export_op,
+                                            nlmsvc_file_file(file)->f_op)) {
              async_block = wait;
              wait = 0;
      }
fs/nfsd/nfs4state.c +10 -3
index 3aefbad4cc09..14ca06424ff1 100644
@@ -7430,6 +7430,7 @@ nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
      struct nfsd4_blocked_lock *nbl = NULL;
      struct file_lock *file_lock = NULL;
      struct file_lock *conflock = NULL;
+     struct super_block *sb;
      __be32 status = 0;
      int lkflg;
      int err;
@@ -7451,6 +7452,7 @@ nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
              dprintk("NFSD: nfsd4_lock: permission denied!\n");
              return status;
      }
+     sb = cstate->current_fh.fh_dentry->d_sb;
 
      if (lock->lk_is_new) {
              if (nfsd4_has_session(cstate))
@@ -7502,7 +7504,9 @@ nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
      fp = lock_stp->st_stid.sc_file;
      switch (lock->lk_type) {
              case NFS4_READW_LT:
-                     if (nfsd4_has_session(cstate))
+                     if (nfsd4_has_session(cstate) ||
+                         export_op_support_safe_async_lock(sb->s_export_op,
+                                                           nf->nf_file->f_op))
                              fl_flags |= FL_SLEEP;
                      fallthrough;
              case NFS4_READ_LT:
@@ -7514,7 +7518,9 @@ nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
                      fl_type = F_RDLCK;
                      break;
              case NFS4_WRITEW_LT:
-                     if (nfsd4_has_session(cstate))
+                     if (nfsd4_has_session(cstate) ||
+                         export_op_support_safe_async_lock(sb->s_export_op,
+                                                           nf->nf_file->f_op))
                              fl_flags |= FL_SLEEP;
                      fallthrough;
              case NFS4_WRITE_LT:
@@ -7542,7 +7548,8 @@ nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
       * for file locks), so don't attempt blocking lock notifications
       * on those filesystems:
       */
-     if (nf->nf_file->f_op->lock)
+     if (!export_op_support_safe_async_lock(sb->s_export_op,
+                                            nf->nf_file->f_op))
              fl_flags &= ~FL_SLEEP;
 
      nbl = find_or_allocate_block(lock_sop, &fp->fi_fhandle, nn);
include/linux/exportfs.h +1 -0
index 11fbd0ee1370..10358a93cdc1 100644
@@ -3,6 +3,7 @@
 #define LINUX_EXPORTFS_H 1
 
 #include <linux/types.h>
+#include <linux/fs.h>
 
Quoted 12 lines

include/linux/exportfs.h struct export_operations { · line 228

Quoted 7 lines
include/linux/exportfs.h +1 -0
 struct dentry;
 struct iattr;
@@ -224,9 +225,16 @@ struct export_operations {
                                                atomic attribute updates
                                              */
 #define EXPORT_OP_FLUSH_ON_CLOSE     (0x20) /* fs flushes file data on close */
+#define EXPORT_OP_SAFE_ASYNC_LOCK    (0x40) /* fs can do async lock request */

We haven't been good about this recently, but the addition of new EXPORT_OP flags need to be accompanied by updates to Documentation/filesystems/nfs/exporting.rst.

ok.

Quoted 3 lines

I will see about adding documentation for other recent flags, but please include an update to exporting.rst with this patch.

ok.

Quoted 3 lines

I'm not sure we need _SAFE_ in the flag name. Would EXPORT_OP_ASYNC_LOCK be OK with you?

sure, a vfs_file_lock() can return FILE_LOCK_DEFERRED as well, even without having this export flag set. How non upstream users use it, I have no idea as it has some races.

Quoted 14 lines

include/linux/exportfs.h struct export_operations { · line 237

Quoted 10 lines
include/linux/exportfs.h +6 -0
      unsigned long   flags;
 };
 
+static inline bool export_op_support_safe_async_lock(const struct export_operations *export_ops,
+                                                  const struct file_operations *f_op)
+{
+     return (export_ops->flags & EXPORT_OP_SAFE_ASYNC_LOCK) || !f_op->lock;
+}
+

I'd like some cosmetic changes to this API, since this seems to be the first utility function for checking EXPORT_OP flags.

Quoted 2 lines

- The function name is unwieldy. How about exportfs_lock_op_is_async() ?

ok.

Quoted 3 lines

- Break up the long lines. It's OK with me if the return value type is left on a different line than the function name and parameters.

ok.

Quoted 2 lines

- This function is globally visible, so a kdoc comment is needed.

ok.

Quoted 4 lines

- The f_op->lock check is common to all the call sites, but it is not at all related to the export AFAICT. Can it be removed from this inline function?

This flag implies it makes only sense if the filesystem has its own lock() implementation, if it doesn't have that I guess the core fs functions for local file locking are being used.
I guess it can be removed, but it should not be used when there is no own ->lock() implementation, at least not now until somebody might update the fs core functionality for local file locking to handle blocking lock requests asynchronously.

- Alex

[0] https://git.kernel.org/pub/scm/linux/kernel/git/cel/linux.git/log/?h=nfsd-next

author maintainer
#24 lore raw

Re: [PATCH 6/7] dlm: use FL_SLEEP to determine blocking vs non-blocking

In reply to Jeff Layton

Hi,

Quoted 13 lines · Jeff Layton
Quoted 11 lines · Alexander Aring

This patch uses the FL_SLEEP flag in struct file_lock to determine if the lock request is a blocking or non-blocking request. Before dlm was using IS_SETLKW() was being used which is not usable for lock requests coming from lockd when EXPORT_OP_SAFE_ASYNC_LOCK inside the export flags is set.

---
 fs/dlm/plock.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 16 lines

fs/dlm/plock.c int dlm_posix_lock(dlm_lockspace_t *lockspace, u64 number, struct file *file, · line 146

Quoted 13 lines
fs/dlm/plock.c +1 -1
index 0094fa4004cc..0c6ed5eeb840 100644
@@ -140,7 +140,7 @@ int dlm_posix_lock(dlm_lockspace_t *lockspace, u64 number, struct file *file,
      op->info.optype         = DLM_PLOCK_OP_LOCK;
      op->info.pid            = fl->fl_pid;
      op->info.ex             = (fl->fl_type == F_WRLCK);
-     op->info.wait           = IS_SETLKW(cmd);
+     op->info.wait           = !!(fl->fl_flags & FL_SLEEP);
      op->info.fsid           = ls->ls_global_id;
      op->info.number         = number;
      op->info.start          = fl->fl_start;

Not sure you really need the !!, but ok...

The wait is a byte value and FL_SLEEP doesn't fit into it, I already run into problems with it. I don't think somebody does a if (foo->wait == 1) but it should be set to 1 or 0.

An alternative would be: ((fl->fl_flags & FL_SLEEP) == FL_SLEEP). I am not sure what the coding style says here. I think it's more important what the C standard says about !!(condition), but there are other users of this in the Linux kernel. :-/

- Alex

#4 lore raw

Re: [PATCH 1/7] lockd: introduce safe async lock op

In reply to Alexander Aring
Quoted 24 lines · Alexander Aring

Hi,

Quoted 21 lines · Chuck Lever
Quoted 18 lines · Alexander Aring

This patch reverts mostly commit 40595cdc93ed ("nfs: block notification on fs with its own ->lock") and introduces an EXPORT_OP_SAFE_ASYNC_LOCK export flag to signal that the "own ->lock" implementation supports async lock requests. The only main user is DLM that is used by GFS2 and OCFS2 filesystem. Those implement their own lock() implementation and return FILE_LOCK_DEFERRED as return value. Since commit 40595cdc93ed ("nfs: block notification on fs with its own ->lock") the DLM implementation were never updated. This patch should prepare for DLM to set the EXPORT_OP_SAFE_ASYNC_LOCK export flag and update the DLM plock implementation regarding to it.

---
 fs/lockd/svclock.c       |  5 ++---
 fs/nfsd/nfs4state.c      | 13 ++++++++++---
 include/linux/exportfs.h |  8 ++++++++
 3 files changed, 20 insertions(+), 6 deletions(-)
Quoted 5 lines
Quoted 3 lines

I'm starting to look at these. Just so you know, it's too late for inclusion in v6.6, but I think we can get these into shape for v6.7.

ok. I base my work on [0], is this correct?

Correct.

Fyi, that is currently what is pending for v6.6. When the merge window closes, it will jump to what will go into v6.7.

Quoted 5 lines
Quoted 4 lines

- The f_op->lock check is common to all the call sites, but it is not at all related to the export AFAICT. Can it be removed from this inline function?

Quoted 7 lines

This flag implies it makes only sense if the filesystem has its own lock() implementation, if it doesn't have that I guess the core fs functions for local file locking are being used.
I guess it can be removed, but it should not be used when there is no own ->lock() implementation, at least not now until somebody might update the fs core functionality for local file locking to handle blocking lock requests asynchronously.

Can that be handled with a remark in the documenting comment?

Quoted 1 line

[0] https://git.kernel.org/pub/scm/linux/kernel/git/cel/linux.git/log/?h=nfsd-next

Signature · 1 line
Chuck Lever
maintainer
#25 lore raw

Re: [PATCH 6/7] dlm: use FL_SLEEP to determine blocking vs non-blocking

In reply to Alexander Aring
Quoted 33 lines · Alexander Aring

Hi,

Quoted 13 lines · Jeff Layton
Quoted 11 lines · Alexander Aring

This patch uses the FL_SLEEP flag in struct file_lock to determine if the lock request is a blocking or non-blocking request. Before dlm was using IS_SETLKW() was being used which is not usable for lock requests coming from lockd when EXPORT_OP_SAFE_ASYNC_LOCK inside the export flags is set.

---
 fs/dlm/plock.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 16 lines

fs/dlm/plock.c int dlm_posix_lock(dlm_lockspace_t *lockspace, u64 number, struct file *file, · line 146

Quoted 13 lines
fs/dlm/plock.c +1 -1
index 0094fa4004cc..0c6ed5eeb840 100644
@@ -140,7 +140,7 @@ int dlm_posix_lock(dlm_lockspace_t *lockspace, u64 number, struct file *file,
      op->info.optype         = DLM_PLOCK_OP_LOCK;
      op->info.pid            = fl->fl_pid;
      op->info.ex             = (fl->fl_type == F_WRLCK);
-     op->info.wait           = IS_SETLKW(cmd);
+     op->info.wait           = !!(fl->fl_flags & FL_SLEEP);
      op->info.fsid           = ls->ls_global_id;
      op->info.number         = number;
      op->info.start          = fl->fl_start;

Not sure you really need the !!, but ok...

Quoted 4 lines

The wait is a byte value and FL_SLEEP doesn't fit into it, I already run into problems with it. I don't think somebody does a if (foo->wait == 1) but it should be set to 1 or 0.

AIUI, any halfway decent compiler should take the result of the &, and implicitly cast that properly to bool. Basically, any value other than 0 should be true.

If the compiler just blindly casts the lowest byte though, then you do need the double-negative.

Quoted 4 lines

An alternative would be: ((fl->fl_flags & FL_SLEEP) == FL_SLEEP). I am not sure what the coding style says here. I think it's more important what the C standard says about !!(condition), but there are other users of this in the Linux kernel. :-/

I don't care too much either way, but my understanding was that you don't need to do the !! trick in most cases with modern compilers.

Signature · 1 line
Jeff Layton <[email protected]>
#6 lore raw

Re: [PATCH 1/7] lockd: introduce safe async lock op

In reply to Alexander Aring
Quoted 34 lines · Alexander Aring

This patch reverts mostly commit 40595cdc93ed ("nfs: block notification on fs with its own ->lock") and introduces an EXPORT_OP_SAFE_ASYNC_LOCK export flag to signal that the "own ->lock" implementation supports async lock requests. The only main user is DLM that is used by GFS2 and OCFS2 filesystem. Those implement their own lock() implementation and return FILE_LOCK_DEFERRED as return value. Since commit 40595cdc93ed ("nfs: block notification on fs with its own ->lock") the DLM implementation were never updated. This patch should prepare for DLM to set the EXPORT_OP_SAFE_ASYNC_LOCK export flag and update the DLM plock implementation regarding to it.

---
 fs/lockd/svclock.c       |  5 ++---
 fs/nfsd/nfs4state.c      | 13 ++++++++++---
 include/linux/exportfs.h |  8 ++++++++
 3 files changed, 20 insertions(+), 6 deletions(-)
fs/lockd/svclock.c +0 -2
index c43ccdf28ed9..6e3b230e8317 100644
@@ -470,9 +470,7 @@ nlmsvc_lock(struct svc_rqst *rqstp, struct nlm_file *file,
 	    struct nlm_host *host, struct nlm_lock *lock, int wait,
 	    struct nlm_cookie *cookie, int reclaim)
 {
-#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
 	struct inode		*inode = nlmsvc_file_inode(file);
-#endif
 	struct nlm_block	*block = NULL;
 	int			error;
 	int			mode;

fs/lockd/svclock.c nlmsvc_lock(struct svc_rqst *rqstp, struct nlm_file *file, · line 488

Quoted 6 lines
fs/lockd/svclock.c +2 -1
@@ -486,7 +484,8 @@ nlmsvc_lock(struct svc_rqst *rqstp, struct nlm_file *file,
 				(long long)lock->fl.fl_end,
 				wait);
 
-	if (nlmsvc_file_file(file)->f_op->lock) {
+	if (!export_op_support_safe_async_lock(inode->i_sb->s_export_op,
+					       nlmsvc_file_file(file)->f_op)) {

... but don't most filesystem use VFS' posix_lock_file(), which does the right thing? I think this patch has broken async lock callbacks for NLM for all the other filesystems that just use posix_lock_file().

Maybe I'm missing something, but why was that necessary?

Ben

maintainer
#7 lore raw

Re: [PATCH 1/7] lockd: introduce safe async lock op

Quoted 47 lines · Benjamin Coddington
Quoted 34 lines · Alexander Aring

This patch reverts mostly commit 40595cdc93ed ("nfs: block notification on fs with its own ->lock") and introduces an EXPORT_OP_SAFE_ASYNC_LOCK export flag to signal that the "own ->lock" implementation supports async lock requests. The only main user is DLM that is used by GFS2 and OCFS2 filesystem. Those implement their own lock() implementation and return FILE_LOCK_DEFERRED as return value. Since commit 40595cdc93ed ("nfs: block notification on fs with its own ->lock") the DLM implementation were never updated. This patch should prepare for DLM to set the EXPORT_OP_SAFE_ASYNC_LOCK export flag and update the DLM plock implementation regarding to it.

---
 fs/lockd/svclock.c       |  5 ++---
 fs/nfsd/nfs4state.c      | 13 ++++++++++---
 include/linux/exportfs.h |  8 ++++++++
 3 files changed, 20 insertions(+), 6 deletions(-)
fs/lockd/svclock.c +0 -2
index c43ccdf28ed9..6e3b230e8317 100644
@@ -470,9 +470,7 @@ nlmsvc_lock(struct svc_rqst *rqstp, struct nlm_file *file,
 	    struct nlm_host *host, struct nlm_lock *lock, int wait,
 	    struct nlm_cookie *cookie, int reclaim)
 {
-#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
 	struct inode		*inode = nlmsvc_file_inode(file);
-#endif
 	struct nlm_block	*block = NULL;
 	int			error;
 	int			mode;

fs/lockd/svclock.c nlmsvc_lock(struct svc_rqst *rqstp, struct nlm_file *file, · line 488

Quoted 6 lines
fs/lockd/svclock.c +2 -1
@@ -486,7 +484,8 @@ nlmsvc_lock(struct svc_rqst *rqstp, struct nlm_file *file,
 				(long long)lock->fl.fl_end,
 				wait);
 
-	if (nlmsvc_file_file(file)->f_op->lock) {
+	if (!export_op_support_safe_async_lock(inode->i_sb->s_export_op,
+					       nlmsvc_file_file(file)->f_op)) {

... but don't most filesystem use VFS' posix_lock_file(), which does the right thing? I think this patch has broken async lock callbacks for NLM for all the other filesystems that just use posix_lock_file().

Quoted 2 lines

Maybe I'm missing something, but why was that necessary?

Good catch. Yeah, I think that probably should have been an && condition. IOW:

	if (nlmsvc_file_file(file)->f_op->lock &&
            !export_op_support_safe_async_lock(inode->i_sb->s_export_op,

Alex, thoughts?

Signature · 1 line
Jeff Layton <[email protected]>
#8 lore raw

Re: [PATCH 1/7] lockd: introduce safe async lock op

Quoted 48 lines · Jeff Layton
Quoted 47 lines · Benjamin Coddington
Quoted 34 lines · Alexander Aring

This patch reverts mostly commit 40595cdc93ed ("nfs: block notification on fs with its own ->lock") and introduces an EXPORT_OP_SAFE_ASYNC_LOCK export flag to signal that the "own ->lock" implementation supports async lock requests. The only main user is DLM that is used by GFS2 and OCFS2 filesystem. Those implement their own lock() implementation and return FILE_LOCK_DEFERRED as return value. Since commit 40595cdc93ed ("nfs: block notification on fs with its own ->lock") the DLM implementation were never updated. This patch should prepare for DLM to set the EXPORT_OP_SAFE_ASYNC_LOCK export flag and update the DLM plock implementation regarding to it.

---
 fs/lockd/svclock.c       |  5 ++---
 fs/nfsd/nfs4state.c      | 13 ++++++++++---
 include/linux/exportfs.h |  8 ++++++++
 3 files changed, 20 insertions(+), 6 deletions(-)
fs/lockd/svclock.c +0 -2
index c43ccdf28ed9..6e3b230e8317 100644
@@ -470,9 +470,7 @@ nlmsvc_lock(struct svc_rqst *rqstp, struct nlm_file *file,
 	    struct nlm_host *host, struct nlm_lock *lock, int wait,
 	    struct nlm_cookie *cookie, int reclaim)
 {
-#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
 	struct inode		*inode = nlmsvc_file_inode(file);
-#endif
 	struct nlm_block	*block = NULL;
 	int			error;
 	int			mode;

fs/lockd/svclock.c nlmsvc_lock(struct svc_rqst *rqstp, struct nlm_file *file, · line 488

Quoted 6 lines
fs/lockd/svclock.c +2 -1
@@ -486,7 +484,8 @@ nlmsvc_lock(struct svc_rqst *rqstp, struct nlm_file *file,
 				(long long)lock->fl.fl_end,
 				wait);
 
-	if (nlmsvc_file_file(file)->f_op->lock) {
+	if (!export_op_support_safe_async_lock(inode->i_sb->s_export_op,
+					       nlmsvc_file_file(file)->f_op)) {

... but don't most filesystem use VFS' posix_lock_file(), which does the right thing? I think this patch has broken async lock callbacks for NLM for all the other filesystems that just use posix_lock_file().

Quoted 9 lines
Quoted 2 lines

Maybe I'm missing something, but why was that necessary?

Good catch. Yeah, I think that probably should have been an && condition. IOW:

	if (nlmsvc_file_file(file)->f_op->lock &&
            !export_op_support_safe_async_lock(inode->i_sb->s_export_op,

Ah Jeff, thanks for confirming - there's been a bunch of changes in there that made me uncertain. I can send a patch for this, I'd like to rename export_op_support_safe_async_lock to something like fs_can_defer_lock (suggestions) and put the test in there.

Ben

author maintainer
#12 lore raw

Re: [PATCH 1/7] lockd: introduce safe async lock op

In reply to Jeff Layton

Hi,

Quoted 52 lines · Jeff Layton
Quoted 47 lines · Benjamin Coddington
Quoted 34 lines · Alexander Aring

This patch reverts mostly commit 40595cdc93ed ("nfs: block notification on fs with its own ->lock") and introduces an EXPORT_OP_SAFE_ASYNC_LOCK export flag to signal that the "own ->lock" implementation supports async lock requests. The only main user is DLM that is used by GFS2 and OCFS2 filesystem. Those implement their own lock() implementation and return FILE_LOCK_DEFERRED as return value. Since commit 40595cdc93ed ("nfs: block notification on fs with its own ->lock") the DLM implementation were never updated. This patch should prepare for DLM to set the EXPORT_OP_SAFE_ASYNC_LOCK export flag and update the DLM plock implementation regarding to it.

---
 fs/lockd/svclock.c       |  5 ++---
 fs/nfsd/nfs4state.c      | 13 ++++++++++---
 include/linux/exportfs.h |  8 ++++++++
 3 files changed, 20 insertions(+), 6 deletions(-)
fs/lockd/svclock.c +0 -2
index c43ccdf28ed9..6e3b230e8317 100644
@@ -470,9 +470,7 @@ nlmsvc_lock(struct svc_rqst *rqstp, struct nlm_file *file,
        struct nlm_host *host, struct nlm_lock *lock, int wait,
        struct nlm_cookie *cookie, int reclaim)
 {
-#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
    struct inode            *inode = nlmsvc_file_inode(file);
-#endif
    struct nlm_block        *block = NULL;
    int                     error;
    int                     mode;

fs/lockd/svclock.c nlmsvc_lock(struct svc_rqst *rqstp, struct nlm_file *file, · line 488

Quoted 6 lines
fs/lockd/svclock.c +2 -1
@@ -486,7 +484,8 @@ nlmsvc_lock(struct svc_rqst *rqstp, struct nlm_file *file,
                            (long long)lock->fl.fl_end,
                            wait);
 
-   if (nlmsvc_file_file(file)->f_op->lock) {
+   if (!export_op_support_safe_async_lock(inode->i_sb->s_export_op,
+                                          nlmsvc_file_file(file)->f_op)) {

... but don't most filesystem use VFS' posix_lock_file(), which does the right thing? I think this patch has broken async lock callbacks for NLM for all the other filesystems that just use posix_lock_file().

Quoted 2 lines

Maybe I'm missing something, but why was that necessary?

Quoted 7 lines

Good catch. Yeah, I think that probably should have been an && condition. IOW:

        if (nlmsvc_file_file(file)->f_op->lock &&
            !export_op_support_safe_async_lock(inode->i_sb->s_export_op,

Alex, thoughts?

The question is here if we ever want that posix_lock_file() receives a posix lock that has flc_flags and the FL_SLEEP set. As mentioned, may "posix_lock_file()" can just deal with it and will not block?

This patch indeed broke it as posix_lock_file() will never see a lock request with FL_SLEEP set, but I remembered that nfs is only polling locks and "probably" never set FL_SLEEP?

Thanks.

- Alex

author maintainer
#9 lore raw

Re: [PATCH 1/7] lockd: introduce safe async lock op

In reply to Benjamin Coddington

Hi,

Quoted 57 lines · Benjamin Coddington
Quoted 54 lines · Jeff Layton
Quoted 47 lines · Benjamin Coddington
Quoted 34 lines · Alexander Aring

This patch reverts mostly commit 40595cdc93ed ("nfs: block notification on fs with its own ->lock") and introduces an EXPORT_OP_SAFE_ASYNC_LOCK export flag to signal that the "own ->lock" implementation supports async lock requests. The only main user is DLM that is used by GFS2 and OCFS2 filesystem. Those implement their own lock() implementation and return FILE_LOCK_DEFERRED as return value. Since commit 40595cdc93ed ("nfs: block notification on fs with its own ->lock") the DLM implementation were never updated. This patch should prepare for DLM to set the EXPORT_OP_SAFE_ASYNC_LOCK export flag and update the DLM plock implementation regarding to it.

---
 fs/lockd/svclock.c       |  5 ++---
 fs/nfsd/nfs4state.c      | 13 ++++++++++---
 include/linux/exportfs.h |  8 ++++++++
 3 files changed, 20 insertions(+), 6 deletions(-)
fs/lockd/svclock.c +0 -2
index c43ccdf28ed9..6e3b230e8317 100644
@@ -470,9 +470,7 @@ nlmsvc_lock(struct svc_rqst *rqstp, struct nlm_file *file,
        struct nlm_host *host, struct nlm_lock *lock, int wait,
        struct nlm_cookie *cookie, int reclaim)
 {
-#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
    struct inode            *inode = nlmsvc_file_inode(file);
-#endif
    struct nlm_block        *block = NULL;
    int                     error;
    int                     mode;

fs/lockd/svclock.c nlmsvc_lock(struct svc_rqst *rqstp, struct nlm_file *file, · line 488

Quoted 6 lines
fs/lockd/svclock.c +2 -1
@@ -486,7 +484,8 @@ nlmsvc_lock(struct svc_rqst *rqstp, struct nlm_file *file,
                            (long long)lock->fl.fl_end,
                            wait);
 
-   if (nlmsvc_file_file(file)->f_op->lock) {
+   if (!export_op_support_safe_async_lock(inode->i_sb->s_export_op,
+                                          nlmsvc_file_file(file)->f_op)) {

... but don't most filesystem use VFS' posix_lock_file(), which does the right thing? I think this patch has broken async lock callbacks for NLM for all the other filesystems that just use posix_lock_file().

Quoted 2 lines

Maybe I'm missing something, but why was that necessary?

Good catch. Yeah, I think that probably should have been an && condition. IOW:

Quoted 8 lines
Quoted 3 lines
      if (nlmsvc_file_file(file)->f_op->lock &&
            !export_op_support_safe_async_lock(inode->i_sb->s_export_op,

Ah Jeff, thanks for confirming - there's been a bunch of changes in there that made me uncertain. I can send a patch for this, I'd like to rename export_op_support_safe_async_lock to something like fs_can_defer_lock (suggestions) and put the test in there.

go ahead with the name change.

About the uncertainty the other changes, except this one mentioned above here in the reply, was a revert of commit 40595cdc93ed ("block notification on fs with its own ->lock") that had removed a similar flag to in kind of a reverse logic.
The flag means something that the commit message says "... filesystems with "good" ->lock methods to support blocking lock notifications.".

- Alex

maintainer
#10 lore raw

Re: [PATCH 1/7] lockd: introduce safe async lock op

In reply to Benjamin Coddington
Quoted 56 lines · Benjamin Coddington
Quoted 54 lines · Jeff Layton
Quoted 47 lines · Benjamin Coddington
Quoted 34 lines · Alexander Aring

This patch reverts mostly commit 40595cdc93ed ("nfs: block notification on fs with its own ->lock") and introduces an EXPORT_OP_SAFE_ASYNC_LOCK export flag to signal that the "own ->lock" implementation supports async lock requests. The only main user is DLM that is used by GFS2 and OCFS2 filesystem. Those implement their own lock() implementation and return FILE_LOCK_DEFERRED as return value. Since commit 40595cdc93ed ("nfs: block notification on fs with its own ->lock") the DLM implementation were never updated. This patch should prepare for DLM to set the EXPORT_OP_SAFE_ASYNC_LOCK export flag and update the DLM plock implementation regarding to it.

---
 fs/lockd/svclock.c       |  5 ++---
 fs/nfsd/nfs4state.c      | 13 ++++++++++---
 include/linux/exportfs.h |  8 ++++++++
 3 files changed, 20 insertions(+), 6 deletions(-)
fs/lockd/svclock.c +0 -2
index c43ccdf28ed9..6e3b230e8317 100644
@@ -470,9 +470,7 @@ nlmsvc_lock(struct svc_rqst *rqstp, struct nlm_file *file,
 	    struct nlm_host *host, struct nlm_lock *lock, int wait,
 	    struct nlm_cookie *cookie, int reclaim)
 {
-#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
 	struct inode		*inode = nlmsvc_file_inode(file);
-#endif
 	struct nlm_block	*block = NULL;
 	int			error;
 	int			mode;

fs/lockd/svclock.c nlmsvc_lock(struct svc_rqst *rqstp, struct nlm_file *file, · line 488

Quoted 6 lines
fs/lockd/svclock.c +2 -1
@@ -486,7 +484,8 @@ nlmsvc_lock(struct svc_rqst *rqstp, struct nlm_file *file,
 				(long long)lock->fl.fl_end,
 				wait);
 
-	if (nlmsvc_file_file(file)->f_op->lock) {
+	if (!export_op_support_safe_async_lock(inode->i_sb->s_export_op,
+					       nlmsvc_file_file(file)->f_op)) {

... but don't most filesystem use VFS' posix_lock_file(), which does the right thing? I think this patch has broken async lock callbacks for NLM for all the other filesystems that just use posix_lock_file().

Quoted 2 lines

Maybe I'm missing something, but why was that necessary?

Good catch. Yeah, I think that probably should have been an && condition. IOW:

Quoted 8 lines
Quoted 3 lines
	if (nlmsvc_file_file(file)->f_op->lock &&
            !export_op_support_safe_async_lock(inode->i_sb->s_export_op,

Ah Jeff, thanks for confirming - there's been a bunch of changes in there that made me uncertain. I can send a patch for this, I'd like to rename export_op_support_safe_async_lock to something like fs_can_defer_lock (suggestions) and put the test in there.

Actually, I take it back. The only callers that set export_op_support_safe_async_lock have ->lock as non-NULL, so that won't change anything, in practice.

Signature · 1 line
Jeff Layton <[email protected]>
#11 lore raw

Re: [PATCH 1/7] lockd: introduce safe async lock op

Quoted 10 lines · Jeff Layton
Quoted 9 lines · Benjamin Coddington
Quoted 6 lines · Jeff Layton

Good catch. Yeah, I think that probably should have been an && condition. IOW:

	if (nlmsvc_file_file(file)->f_op->lock &&
            !export_op_support_safe_async_lock(inode->i_sb->s_export_op,
Quoted 8 lines
Quoted 4 lines

Ah Jeff, thanks for confirming - there's been a bunch of changes in there that made me uncertain. I can send a patch for this, I'd like to rename export_op_support_safe_async_lock to something like fs_can_defer_lock (suggestions) and put the test in there.

Actually, I take it back. The only callers that set export_op_support_safe_async_lock have ->lock as non-NULL, so that won't change anything, in practice.

*nod*

In trying to conjoin the export flag test with the f_op->lock test, I'm just making a huge mess of layering violations. The changes for NFSD are terrible.

Seems like we want an FOP_ flag for this, I might ask for one. Wouldn't other users like FUSE be interested?

Ben