Skip to contentSkip to conversation

ceph: check if file lock exists before sending unlock request

PATCH Superseded Yan, Zheng 2 participants 2 messages Feb 11, 2020 Ceph client

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Yan, Zheng

author
#0 lore raw

PATCH 1/1 ceph: check if file lock exists before sending unlock request

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.

---
 fs/ceph/locks.c | 28 ++++++++++++++++++++++++++--
 1 file changed, 26 insertions(+), 2 deletions(-)
fs/ceph/locks.c +26 -2
index 544e9e85b120..78be861259eb 100644
@@ -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,
Signature · 1 line
2.21.1
#1 lore raw
Quoted 17 lines · 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.

---
 fs/ceph/locks.c | 28 ++++++++++++++++++++++++++--
 1 file changed, 26 insertions(+), 2 deletions(-)
fs/ceph/locks.c +0 -0
index 544e9e85b120..78be861259eb 100644
@@ -255,9 +255,21 @@ int ceph_lock(struct file *file, int cmd, struct file_lock *fl)
 	else
 		lock_cmd = CEPH_LOCK_UNLOCK;
 

fs/ceph/locks.c int ceph_lock(struct file *file, int cmd, struct file_lock *fl) · line 267

Quoted 10 lines
fs/ceph/locks.c +10 -0
+	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.

Quoted 32 lines
fs/ceph/locks.c +16 -2
+	}
+
 	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,
Signature · 1 line
Jeff Layton <[email protected]>