Skip to contentSkip to conversation

ceph: discard r_new_inode if open O_CREAT opened existing inode

PATCH Superseded Jeff Layton 3 participants 6 messages Mar 30, 2022 – Apr 1, 2022 Ceph client

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ceph: discard r_new_inode if open O_CREAT opened existing inode

When we do an unchecked create, we optimistically pre-create an inode and populate it, including its fscrypt context. It's possible though that we'll end up opening an existing inode, in which case the precreated inode will have a crypto context that doesn't match the existing data.

If we're issuing an O_CREAT open and find an existing inode, just discard the precreated inode and create a new one to ensure the context is properly set.

Cc Luís Henriques <[email protected]>
---
 fs/ceph/mds_client.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
fs/ceph/mds_client.c +8 -2
index 840a60b812fc..b03128fdbb07 100644
@@ -3504,13 +3504,19 @@ static void handle_reply(struct ceph_mds_session *session, struct ceph_msg *msg)
 	/* Must find target inode outside of mutexes to avoid deadlocks */
 	rinfo = &req->r_reply_info;
 	if ((err >= 0) && rinfo->head->is_target) {
-		struct inode *in;
+		struct inode *in = xchg(&req->r_new_inode, NULL);
 		struct ceph_vino tvino = {
 			.ino  = le64_to_cpu(rinfo->targeti.in->ino),
 			.snap = le64_to_cpu(rinfo->targeti.in->snapid)
 		};
 
-		in = ceph_get_inode(mdsc->fsc->sb, tvino, xchg(&req->r_new_inode, NULL));
+		/* If we ended up opening an existing inode, discard r_new_inode */
+		if (req->r_op == CEPH_MDS_OP_CREATE && !req->r_reply_info.has_create_ino) {
+			iput(in);
+			in = NULL;
+		}
+
+		in = ceph_get_inode(mdsc->fsc->sb, tvino, in);
 		if (IS_ERR(in)) {
 			err = PTR_ERR(in);
 			mutex_lock(&session->s_mutex);
Signature · 1 line
2.35.1
#1 lore raw
Quoted 32 lines · Jeff Layton

When we do an unchecked create, we optimistically pre-create an inode and populate it, including its fscrypt context. It's possible though that we'll end up opening an existing inode, in which case the precreated inode will have a crypto context that doesn't match the existing data.

If we're issuing an O_CREAT open and find an existing inode, just discard the precreated inode and create a new one to ensure the context is properly set.

Cc Luís Henriques <[email protected]>
---
 fs/ceph/mds_client.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
fs/ceph/mds_client.c +1 -2
index 840a60b812fc..b03128fdbb07 100644
@@ -3504,13 +3504,19 @@ static void handle_reply(struct ceph_mds_session *session, struct ceph_msg *msg)
 	/* Must find target inode outside of mutexes to avoid deadlocks */
 	rinfo = &req->r_reply_info;
 	if ((err >= 0) && rinfo->head->is_target) {
-		struct inode *in;
+		struct inode *in = xchg(&req->r_new_inode, NULL);
 		struct ceph_vino tvino = {
 			.ino  = le64_to_cpu(rinfo->targeti.in->ino),
 			.snap = le64_to_cpu(rinfo->targeti.in->snapid)
 		};
 
-		in = ceph_get_inode(mdsc->fsc->sb, tvino, xchg(&req->r_new_inode, NULL));

fs/ceph/mds_client.c static void handle_reply(struct ceph_mds_session *session, struct ceph_msg *msg) · line 3522

Quoted 10 lines
fs/ceph/mds_client.c +7 -0
+		/* If we ended up opening an existing inode, discard r_new_inode */
+		if (req->r_op == CEPH_MDS_OP_CREATE && !req->r_reply_info.has_create_ino) {
+			iput(in);
+			in = NULL;
+		}
+
+		in = ceph_get_inode(mdsc->fsc->sb, tvino, in);
 		if (IS_ERR(in)) {
 			err = PTR_ERR(in);
 			mutex_lock(&session->s_mutex);

Forgot to mention that this one is for the fscrypt pile. This patch fixes generic/595 for me.

Signature · 1 line
Jeff Layton <[email protected]>
#3 lore raw
In reply to Jeff Layton
Quoted 25 lines · Jeff Layton

When we do an unchecked create, we optimistically pre-create an inode and populate it, including its fscrypt context. It's possible though that we'll end up opening an existing inode, in which case the precreated inode will have a crypto context that doesn't match the existing data.

If we're issuing an O_CREAT open and find an existing inode, just discard the precreated inode and create a new one to ensure the context is properly set.

Cc Luís Henriques <[email protected]>
---
  fs/ceph/mds_client.c | 10 ++++++++--
  1 file changed, 8 insertions(+), 2 deletions(-)
fs/ceph/mds_client.c +0 -1
index 840a60b812fc..b03128fdbb07 100644
@@ -3504,13 +3504,19 @@ static void handle_reply(struct ceph_mds_session *session, struct ceph_msg *msg)
  	/* Must find target inode outside of mutexes to avoid deadlocks */
  	rinfo = &req->r_reply_info;
  	if ((err >= 0) && rinfo->head->is_target) {
-		struct inode *in;

fs/ceph/mds_client.c static void handle_reply(struct ceph_mds_session *session, struct ceph_msg *msg) · line 3515

Quoted 10 lines
fs/ceph/mds_client.c +4 -1
+		struct inode *in = xchg(&req->r_new_inode, NULL);
  		struct ceph_vino tvino = {
  			.ino  = le64_to_cpu(rinfo->targeti.in->ino),
  			.snap = le64_to_cpu(rinfo->targeti.in->snapid)
  		};
 
-		in = ceph_get_inode(mdsc->fsc->sb, tvino, xchg(&req->r_new_inode, NULL));
+		/* If we ended up opening an existing inode, discard r_new_inode */
+		if (req->r_op == CEPH_MDS_OP_CREATE && !req->r_reply_info.has_create_ino) {
+			iput(in);

If the 'in' has a delegated ino, should we give it back here ?

-- Xiubo

Quoted 7 lines
fs/ceph/mds_client.c +4 -0
+			in = NULL;
+		}
+
+		in = ceph_get_inode(mdsc->fsc->sb, tvino, in);
  		if (IS_ERR(in)) {
  			err = PTR_ERR(in);
  			mutex_lock(&session->s_mutex);
#2 lore raw
In reply to Jeff Layton
Quoted 32 lines · Jeff Layton
Quoted 31 lines · Jeff Layton

When we do an unchecked create, we optimistically pre-create an inode and populate it, including its fscrypt context. It's possible though that we'll end up opening an existing inode, in which case the precreated inode will have a crypto context that doesn't match the existing data.

If we're issuing an O_CREAT open and find an existing inode, just discard the precreated inode and create a new one to ensure the context is properly set.

Cc Luís Henriques <[email protected]>
---
 fs/ceph/mds_client.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
fs/ceph/mds_client.c +1 -1
index 840a60b812fc..b03128fdbb07 100644
@@ -3504,13 +3504,19 @@ static void handle_reply(struct ceph_mds_session *session, struct ceph_msg *msg)
 	/* Must find target inode outside of mutexes to avoid deadlocks */
 	rinfo = &req->r_reply_info;
 	if ((err >= 0) && rinfo->head->is_target) {
-		struct inode *in;
+		struct inode *in = xchg(&req->r_new_inode, NULL);
 		struct ceph_vino tvino = {
 			.ino  = le64_to_cpu(rinfo->targeti.in->ino),
 			.snap = le64_to_cpu(rinfo->targeti.in->snapid)
 		};
 
Quoted 14 lines

fs/ceph/mds_client.c static void handle_reply(struct ceph_mds_session *session, struct ceph_msg *msg) · line 3522

Quoted 11 lines
fs/ceph/mds_client.c +7 -1
-		in = ceph_get_inode(mdsc->fsc->sb, tvino, xchg(&req->r_new_inode, NULL));
+		/* If we ended up opening an existing inode, discard r_new_inode */
+		if (req->r_op == CEPH_MDS_OP_CREATE && !req->r_reply_info.has_create_ino) {
+			iput(in);
+			in = NULL;
+		}
+
+		in = ceph_get_inode(mdsc->fsc->sb, tvino, in);
 		if (IS_ERR(in)) {
 			err = PTR_ERR(in);
 			mutex_lock(&session->s_mutex);

Forgot to mention that this one is for the fscrypt pile. This patch fixes generic/595 for me.

Wow! I must say it: the issue is very far from the places I was looking at. Thanks!

Cheers,

Signature · 1 line
Luís
#4 lore raw
In reply to Xiubo Li
Quoted 32 lines · Xiubo Li
Quoted 31 lines · Jeff Layton

When we do an unchecked create, we optimistically pre-create an inode and populate it, including its fscrypt context. It's possible though that we'll end up opening an existing inode, in which case the precreated inode will have a crypto context that doesn't match the existing data.

If we're issuing an O_CREAT open and find an existing inode, just discard the precreated inode and create a new one to ensure the context is properly set.

Cc Luís Henriques <[email protected]>
---
  fs/ceph/mds_client.c | 10 ++++++++--
  1 file changed, 8 insertions(+), 2 deletions(-)
fs/ceph/mds_client.c +1 -1
index 840a60b812fc..b03128fdbb07 100644
@@ -3504,13 +3504,19 @@ static void handle_reply(struct ceph_mds_session *session, struct ceph_msg *msg)
  	/* Must find target inode outside of mutexes to avoid deadlocks */
  	rinfo = &req->r_reply_info;
  	if ((err >= 0) && rinfo->head->is_target) {
-		struct inode *in;
+		struct inode *in = xchg(&req->r_new_inode, NULL);
  		struct ceph_vino tvino = {
  			.ino  = le64_to_cpu(rinfo->targeti.in->ino),
  			.snap = le64_to_cpu(rinfo->targeti.in->snapid)
  		};
 
Quoted 10 lines

fs/ceph/mds_client.c static void handle_reply(struct ceph_mds_session *session, struct ceph_msg *msg) · line 3515

Quoted 4 lines
fs/ceph/mds_client.c +3 -1
-		in = ceph_get_inode(mdsc->fsc->sb, tvino, xchg(&req->r_new_inode, NULL));
+		/* If we ended up opening an existing inode, discard r_new_inode */
+		if (req->r_op == CEPH_MDS_OP_CREATE && !req->r_reply_info.has_create_ino) {
+			iput(in);

If the 'in' has a delegated ino, should we give it back here ?

-- Xiubo

This really shouldn't be a delegated ino. We only grab a delegated ino if we're doing an async create, and in that case we should know that the dentry doesn't exist and the create will succeed or fail without opening the file.

It's probably worth throwing a warning though if we ever _do_ get a delegated ino here. Let me consider how best to catch that situation.

Quoted 8 lines

fs/ceph/mds_client.c static void handle_reply(struct ceph_mds_session *session, struct ceph_msg *msg) · line 3522

Quoted 7 lines
fs/ceph/mds_client.c +4 -0
+			in = NULL;
+		}
+
+		in = ceph_get_inode(mdsc->fsc->sb, tvino, in);
  		if (IS_ERR(in)) {
  			err = PTR_ERR(in);
  			mutex_lock(&session->s_mutex);

Thanks,

Signature · 1 line
Jeff Layton <[email protected]>
#5 lore raw
Quoted 39 lines · Jeff Layton
Quoted 38 lines · Xiubo Li
Quoted 25 lines · Jeff Layton

When we do an unchecked create, we optimistically pre-create an inode and populate it, including its fscrypt context. It's possible though that we'll end up opening an existing inode, in which case the precreated inode will have a crypto context that doesn't match the existing data.

If we're issuing an O_CREAT open and find an existing inode, just discard the precreated inode and create a new one to ensure the context is properly set.

Cc Luís Henriques <[email protected]>
---
   fs/ceph/mds_client.c | 10 ++++++++--
   1 file changed, 8 insertions(+), 2 deletions(-)
fs/ceph/mds_client.c +0 -1
index 840a60b812fc..b03128fdbb07 100644
@@ -3504,13 +3504,19 @@ static void handle_reply(struct ceph_mds_session *session, struct ceph_msg *msg)
   	/* Must find target inode outside of mutexes to avoid deadlocks */
   	rinfo = &req->r_reply_info;
   	if ((err >= 0) && rinfo->head->is_target) {
-		struct inode *in;

fs/ceph/mds_client.c static void handle_reply(struct ceph_mds_session *session, struct ceph_msg *msg) · line 3515

Quoted 10 lines
fs/ceph/mds_client.c +4 -1
+		struct inode *in = xchg(&req->r_new_inode, NULL);
   		struct ceph_vino tvino = {
   			.ino  = le64_to_cpu(rinfo->targeti.in->ino),
   			.snap = le64_to_cpu(rinfo->targeti.in->snapid)
   		};
 
-		in = ceph_get_inode(mdsc->fsc->sb, tvino, xchg(&req->r_new_inode, NULL));
+		/* If we ended up opening an existing inode, discard r_new_inode */
+		if (req->r_op == CEPH_MDS_OP_CREATE && !req->r_reply_info.has_create_ino) {
+			iput(in);

If the 'in' has a delegated ino, should we give it back here ?

Quoted 7 lines
Quoted 3 lines

-- Xiubo

This really shouldn't be a delegated ino. We only grab a delegated ino if we're doing an async create, and in that case we should know that the dentry doesn't exist and the create will succeed or fail without opening the file.

Yeah, right.

-- Xiubo

Quoted 11 lines

It's probably worth throwing a warning though if we ever _do_ get a delegated ino here. Let me consider how best to catch that situation.

Quoted 7 lines

fs/ceph/mds_client.c static void handle_reply(struct ceph_mds_session *session, struct ceph_msg *msg) · line 3522

Quoted 7 lines
fs/ceph/mds_client.c +4 -0
+			in = NULL;
+		}
+
+		in = ceph_get_inode(mdsc->fsc->sb, tvino, in);
   		if (IS_ERR(in)) {
   			err = PTR_ERR(in);
   			mutex_lock(&session->s_mutex);

Thanks,