All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] ceph: check if file lock exists before sending unlock request
@ 2020-02-11  8:59 Yan, Zheng
  2020-02-11 12:04 ` Jeff Layton
  0 siblings, 1 reply; 2+ messages in thread
From: Yan, Zheng @ 2020-02-11  8:59 UTC (permalink / raw)
  To: jlayton, ceph-devel; +Cc: Yan, Zheng

When a process exits, kernel closes its files. locks_remove_file()
is called to remove file locks on these files. locks_remove_file()
tries unlocking files locks even there is no file locks.

Signed-off-by: "Yan, Zheng" <zyan@redhat.com>
---
 fs/ceph/locks.c | 28 ++++++++++++++++++++++++++--
 1 file changed, 26 insertions(+), 2 deletions(-)

diff --git a/fs/ceph/locks.c b/fs/ceph/locks.c
index 544e9e85b120..78be861259eb 100644
--- a/fs/ceph/locks.c
+++ b/fs/ceph/locks.c
@@ -255,9 +255,21 @@ int ceph_lock(struct file *file, int cmd, struct file_lock *fl)
 	else
 		lock_cmd = CEPH_LOCK_UNLOCK;
 
+	if (op == CEPH_MDS_OP_SETFILELOCK && F_UNLCK == fl->fl_type) {
+		unsigned int orig_flags = fl->fl_flags;
+		fl->fl_flags |= FL_EXISTS;
+		err = posix_lock_file(file, fl, NULL);
+		fl->fl_flags = orig_flags;
+		if (err == -ENOENT) {
+			if (!(orig_flags & FL_EXISTS))
+				err = 0;
+			return err;
+		}
+	}
+
 	err = ceph_lock_message(CEPH_LOCK_FCNTL, op, inode, lock_cmd, wait, fl);
 	if (!err) {
-		if (op == CEPH_MDS_OP_SETFILELOCK) {
+		if (op == CEPH_MDS_OP_SETFILELOCK && F_UNLCK != fl->fl_type) {
 			dout("mds locked, locking locally\n");
 			err = posix_lock_file(file, fl, NULL);
 			if (err) {
@@ -311,9 +323,21 @@ int ceph_flock(struct file *file, int cmd, struct file_lock *fl)
 	else
 		lock_cmd = CEPH_LOCK_UNLOCK;
 
+	if (F_UNLCK == fl->fl_type) {
+		unsigned int orig_flags = fl->fl_flags;
+		fl->fl_flags |= FL_EXISTS;
+		err = locks_lock_file_wait(file, fl);
+		fl->fl_flags = orig_flags;
+		if (err == -ENOENT) {
+			if (!(orig_flags & FL_EXISTS))
+				err = 0;
+			return err;
+		}
+	}
+
 	err = ceph_lock_message(CEPH_LOCK_FLOCK, CEPH_MDS_OP_SETFILELOCK,
 				inode, lock_cmd, wait, fl);
-	if (!err) {
+	if (!err && F_UNLCK != fl->fl_type) {
 		err = locks_lock_file_wait(file, fl);
 		if (err) {
 			ceph_lock_message(CEPH_LOCK_FLOCK,
-- 
2.21.1

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] ceph: check if file lock exists before sending unlock request
  2020-02-11  8:59 [PATCH] ceph: check if file lock exists before sending unlock request Yan, Zheng
@ 2020-02-11 12:04 ` Jeff Layton
  0 siblings, 0 replies; 2+ messages in thread
From: Jeff Layton @ 2020-02-11 12:04 UTC (permalink / raw)
  To: Yan, Zheng, ceph-devel

On Tue, 2020-02-11 at 16:59 +0800, Yan, Zheng wrote:
> When a process exits, kernel closes its files. locks_remove_file()
> is called to remove file locks on these files. locks_remove_file()
> tries unlocking files locks even there is no file locks.
> 
> Signed-off-by: "Yan, Zheng" <zyan@redhat.com>
> ---
>  fs/ceph/locks.c | 28 ++++++++++++++++++++++++++--
>  1 file changed, 26 insertions(+), 2 deletions(-)
> 
> diff --git a/fs/ceph/locks.c b/fs/ceph/locks.c
> index 544e9e85b120..78be861259eb 100644
> --- a/fs/ceph/locks.c
> +++ b/fs/ceph/locks.c
> @@ -255,9 +255,21 @@ int ceph_lock(struct file *file, int cmd, struct file_lock *fl)
>  	else
>  		lock_cmd = CEPH_LOCK_UNLOCK;
>  
> +	if (op == CEPH_MDS_OP_SETFILELOCK && F_UNLCK == fl->fl_type) {
> +		unsigned int orig_flags = fl->fl_flags;
> +		fl->fl_flags |= FL_EXISTS;
> +		err = posix_lock_file(file, fl, NULL);
> +		fl->fl_flags = orig_flags;
> +		if (err == -ENOENT) {
> +			if (!(orig_flags & FL_EXISTS))
> +				err = 0;
> +			return err;
> +		}

Maybe move the above logic into a helper function since this is copied
to the section below? The helper would need to use locks_lock_file_wait
instead of posix_lock_file, but that should be ok.

> +	}
> +
>  	err = ceph_lock_message(CEPH_LOCK_FCNTL, op, inode, lock_cmd, wait, fl);
>  	if (!err) {
> -		if (op == CEPH_MDS_OP_SETFILELOCK) {
> +		if (op == CEPH_MDS_OP_SETFILELOCK && F_UNLCK != fl->fl_type) {
>  			dout("mds locked, locking locally\n");
>  			err = posix_lock_file(file, fl, NULL);
>  			if (err) {
> @@ -311,9 +323,21 @@ int ceph_flock(struct file *file, int cmd, struct file_lock *fl)
>  	else
>  		lock_cmd = CEPH_LOCK_UNLOCK;
>  
> +	if (F_UNLCK == fl->fl_type) {
> +		unsigned int orig_flags = fl->fl_flags;
> +		fl->fl_flags |= FL_EXISTS;
> +		err = locks_lock_file_wait(file, fl);
> +		fl->fl_flags = orig_flags;
> +		if (err == -ENOENT) {
> +			if (!(orig_flags & FL_EXISTS))
> +				err = 0;
> +			return err;
> +		}
> +	}
> +
>  	err = ceph_lock_message(CEPH_LOCK_FLOCK, CEPH_MDS_OP_SETFILELOCK,
>  				inode, lock_cmd, wait, fl);
> -	if (!err) {
> +	if (!err && F_UNLCK != fl->fl_type) {
>  		err = locks_lock_file_wait(file, fl);
>  		if (err) {
>  			ceph_lock_message(CEPH_LOCK_FLOCK,

-- 
Jeff Layton <jlayton@kernel.org>

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2020-02-11 12:04 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-02-11  8:59 [PATCH] ceph: check if file lock exists before sending unlock request Yan, Zheng
2020-02-11 12:04 ` Jeff Layton

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.