diff mbox series

ceph: check if file lock exists before sending unlock request

Message ID 20200211085901.16256-1-zyan@redhat.com (mailing list archive)
State New, archived
Headers show
Series ceph: check if file lock exists before sending unlock request | expand

Commit Message

Yan, Zheng Feb. 11, 2020, 8:59 a.m. UTC
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(-)

Comments

Jeff Layton Feb. 11, 2020, 12:04 p.m. UTC | #1
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,
diff mbox series

Patch

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,