Skip to contentSkip to conversation

smb/server: improve async interim response handling for compound requests

PATCH Superseded ChenXiaoSong 2 participants 7 messages Jul 2 – Jul 6

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

ChenXiaoSong

author reviewer
#0 lore raw

cover smb/server: improve async interim response handling for compound requests

From: ChenXiaoSong <[email protected]>

I am currently implementing the ksmbd change notify feature (I will send it out soon), and I have encountered the following issue.

Before this patchset, no CREATE response:

Windows client request:
CREATE dir
CHANGE_NOTIFY, fid = compound fid

Server response:
CHANGE_NOTIFY STATUS_PENDING, AsyncId = N

After this patchset:

Server response:
CREATE dir SUCCESS, fid = X
CHANGE_NOTIFY STATUS_PENDING, AsyncId = N

ChenXiaoSong (2):
smb/server: factor out smb2_init_err_rsp() smb/server: preserve compound prefix for async interim responses

 fs/smb/server/smb2pdu.c | 59 +++++++++++++++++++++++++++++++++--------
 1 file changed, 48 insertions(+), 11 deletions(-)
Signature · 1 line
2.54.0
author reviewer
#1 lore raw

PATCH 1/2 smb/server: factor out smb2_init_err_rsp()

From: ChenXiaoSong <[email protected]>

Factor out SMB2 error response body initialization into a helper. Use a named length for the SMB2 error response.

This prepares the async interim response path to build an error response without calling smb2_set_err_rsp(), which also pins the response iov.

---
 fs/smb/server/smb2pdu.c | 21 ++++++++++++++-------
 1 file changed, 14 insertions(+), 7 deletions(-)
fs/smb/server/smb2pdu.c +14 -7
index 540b4affa9fb..43d0dacc7907 100644
@@ -58,6 +58,9 @@ static void __wbuf(struct ksmbd_work *work, void **req, void **rsp)
 
 #define WORK_BUFFERS(w, rq, rs)	__wbuf((w), (void **)&(rq), (void **)&(rs))
 
+#define SMB2_ERROR_RSP_LEN	(__SMB2_HEADER_STRUCTURE_SIZE + \
+				 SMB2_ERROR_STRUCTURE_SIZE2)
+
 #define SMB2_CREATE_FILE_ATTRIBUTE_MASK \
 	(FILE_ATTRIBUTE_MASK & ~(FILE_ATTRIBUTE_INTEGRITY_STREAM | \
 				 FILE_ATTRIBUTE_NO_SCRUB_DATA))
@@ -195,6 +198,15 @@ int smb2_get_ksmbd_tcon(struct ksmbd_work *work)
 	return 1;
 }
 
+static void smb2_init_err_rsp(struct smb2_err_rsp *err_rsp)
+{
+	err_rsp->StructureSize = SMB2_ERROR_STRUCTURE_SIZE2_LE;
+	err_rsp->ErrorContextCount = 0;
+	err_rsp->Reserved = 0;
+	err_rsp->ByteCount = 0;
+	err_rsp->ErrorData[0] = 0;
+}
+
 /**
  * smb2_set_err_rsp() - set error response code on smb response
  * @work:	smb work containing response buffer
@@ -211,14 +223,9 @@ void smb2_set_err_rsp(struct ksmbd_work *work)
 	if (err_rsp->hdr.Status != STATUS_STOPPED_ON_SYMLINK) {
 		int err;
 
-		err_rsp->StructureSize = SMB2_ERROR_STRUCTURE_SIZE2_LE;
-		err_rsp->ErrorContextCount = 0;
-		err_rsp->Reserved = 0;
-		err_rsp->ByteCount = 0;
-		err_rsp->ErrorData[0] = 0;
+		smb2_init_err_rsp(err_rsp);
 		err = ksmbd_iov_pin_rsp(work, (void *)err_rsp,
-					__SMB2_HEADER_STRUCTURE_SIZE +
-						SMB2_ERROR_STRUCTURE_SIZE2);
+					SMB2_ERROR_RSP_LEN);
 		if (err)
 			work->send_no_response = 1;
 	}
Signature · 1 line
2.54.0
author reviewer
#2 lore raw

PATCH 2/2 smb/server: preserve compound prefix for async interim responses

In reply to ChenXiaoSong

From: ChenXiaoSong <[email protected]>

Windows clients can issue a related compound request such as CREATE followed by CHANGE_NOTIFY. ksmbd defers CHANGE_NOTIFY and sends STATUS_PENDING through the async interim response path. That path only built a response for the current command, so responses generated for earlier commands in the compound chain were dropped.

This means the client never receives the CREATE response that contains the file id used by the pending notify request. Windows Explorer can then stall when navigating away from the directory until the connection is torn down and the pending notify requests are cleaned up.

When sending STATUS_PENDING for an async command, copy the completed compound response prefix before appending the async interim error response. Final async completions such as STATUS_CANCELLED and STATUS_NOTIFY_CLEANUP remain standalone async responses.

---
 fs/smb/server/smb2pdu.c | 38 ++++++++++++++++++++++++++++++++++----
 1 file changed, 34 insertions(+), 4 deletions(-)
fs/smb/server/smb2pdu.c +34 -4
index 43d0dacc7907..0a3997502732 100644
@@ -847,7 +847,11 @@ void release_async_work(struct ksmbd_work *work)
 void smb2_send_interim_resp(struct ksmbd_work *work, __le32 status)
 {
 	struct smb2_hdr *rsp_hdr;
+	struct smb2_err_rsp *err_rsp;
 	struct ksmbd_work *in_work = ksmbd_alloc_work_struct();
+	char *rsp_buf;
+	unsigned int compound_len = 0;
+	unsigned int msg_len;
 
 	if (!in_work)
 		return;
@@ -859,14 +863,40 @@ void smb2_send_interim_resp(struct ksmbd_work *work, __le32 status)
 	}
 
 	in_work->conn = work->conn;
-	memcpy(smb_get_msg(in_work->response_buf), ksmbd_resp_buf_next(work),
-	       __SMB2_HEADER_STRUCTURE_SIZE);
+	rsp_buf = smb_get_msg(in_work->response_buf);
+
+	/*
+	 * STATUS_PENDING can complete the last command in a compound chain.
+	 * Preserve the completed prefix before appending the async interim
+	 * response so the client can consume every response in the chain.
+	 */
+	if (status == STATUS_PENDING)
+		compound_len = work->next_smb2_rsp_hdr_off;
+
+	msg_len = compound_len + SMB2_ERROR_RSP_LEN;
+	if (msg_len > MAX_CIFS_SMALL_BUFFER_SIZE) {
+		pr_err("compound interim response too large: %u\n", msg_len);
+		ksmbd_free_work_struct(in_work);
+		return;
+	}
+
+	if (compound_len) {
+		memcpy(rsp_buf, smb_get_msg(work->response_buf),
+		       compound_len);
+	}
+
+	rsp_hdr = (struct smb2_hdr *)(rsp_buf + compound_len);
+	memcpy(rsp_hdr, ksmbd_resp_buf_next(work), __SMB2_HEADER_STRUCTURE_SIZE);
 
-	rsp_hdr = smb_get_msg(in_work->response_buf);
 	rsp_hdr->Flags |= SMB2_FLAGS_ASYNC_COMMAND;
 	rsp_hdr->Id.AsyncId = cpu_to_le64(work->async_id);
-	smb2_set_err_rsp(in_work);
 	rsp_hdr->Status = status;
+	err_rsp = (struct smb2_err_rsp *)rsp_hdr;
+	smb2_init_err_rsp(err_rsp);
+	if (ksmbd_iov_pin_rsp(in_work, rsp_buf, msg_len)) {
+		ksmbd_free_work_struct(in_work);
+		return;
+	}
 
 	ksmbd_conn_write(in_work);
 	ksmbd_free_work_struct(in_work);
Signature · 1 line
2.54.0
maintainer
#3 lore raw

Re: [PATCH 2/2] smb/server: preserve compound prefix for async interim responses

Quoted 14 lines · ChenXiaoSong

From: ChenXiaoSong <[email protected]>

Windows clients can issue a related compound request such as CREATE followed by CHANGE_NOTIFY. ksmbd defers CHANGE_NOTIFY and sends STATUS_PENDING through the async interim response path. That path only built a response for the current command, so responses generated for earlier commands in the compound chain were dropped.

This means the client never receives the CREATE response that contains the file id used by the pending notify request. Windows Explorer can then stall when navigating away from the directory until the connection is torn down and the pending notify requests are cleaned up.

Quoted 6 lines

When sending STATUS_PENDING for an async command, copy the completed compound response prefix before appending the async interim error response. Final async completions such as STATUS_CANCELLED and STATUS_NOTIFY_CLEANUP remain standalone async responses.

Could you please take a look at the three smbtorture failures caused by this patch?

smb2.compound.compound-padding
smb2.compound_async.write_write
smb2.compound_async.read_read

Thanks.

author reviewer
#4 lore raw

Re: [PATCH 2/2] smb/server: preserve compound prefix for async interim responses

I will debug these test cases to find the root cause. Thanks for your testing.

Quoted 6 lines · Namjae Jeon

Could you please take a look at the three smbtorture failures caused by this patch?

smb2.compound.compound-padding
smb2.compound_async.write_write
smb2.compound_async.read_read

Signature · 3 lines
ChenXiaoSong <[email protected]>
Chinese Homepage: https://chenxiaosong.com
English Homepage: https://chenxiaosong.com/en
author reviewer
#5 lore raw

Re: [PATCH 2/2] smb/server: preserve compound prefix for async interim responses

In reply to Namjae Jeon

These three test cases also fail when using Samba as the server. I will also debug why Samba fails.

smbd --version
Version 4.25.0pre1-GIT-bffb51f09ab

test: compound-padding
failure: compound-padding [
../../source4/torture/smb2/compound.c:1175: Incorrect status NT_STATUS_OBJECT_NAME_INVALID - should be NT_STATUS_OK ]

test: write_write
failure: write_write [
(../../source4/torture/smb2/compound.c:2549) Incorrect value req[1]->cancel.can_cancel=0 - should be 1 ]

test: read_read
failure: read_read [
(../../source4/torture/smb2/compound.c:2653) Incorrect value req[1]->cancel.can_cancel=0 - should be 1 ]

Quoted 3 lines · Namjae Jeon

smb2.compound.compound-padding
smb2.compound_async.write_write
smb2.compound_async.read_read

Signature · 3 lines
ChenXiaoSong <[email protected]>
Chinese Homepage: https://chenxiaosong.com
English Homepage: https://chenxiaosong.com/en
author reviewer
#6 lore raw

Re: [PATCH 2/2] smb/server: preserve compound prefix for async interim responses

Samba tests passed after adding the following config options to smb.conf:

vfs objects = delay_inject streams_xattr
delay_inject:pread_send = 2000
delay_inject:pwrite_send = 2000

Quoted 27 lines · ChenXiaoSong

These three test cases also fail when using Samba as the server. I will also debug why Samba fails.

smbd --version
Version 4.25.0pre1-GIT-bffb51f09ab

test: compound-padding
failure: compound-padding [
../../source4/torture/smb2/compound.c:1175: Incorrect status NT_STATUS_OBJECT_NAME_INVALID - should be NT_STATUS_OK ]

test: write_write
failure: write_write [ (../../source4/torture/smb2/compound.c:2549) Incorrect value req[1]-

Quoted 1 line

cancel.can_cancel=0 - should be 1

]

test: read_read
failure: read_read [ (../../source4/torture/smb2/compound.c:2653) Incorrect value req[1]-

Quoted 1 line

cancel.can_cancel=0 - should be 1

]

Quoted 3 lines · Namjae Jeon

smb2.compound.compound-padding
smb2.compound_async.write_write
smb2.compound_async.read_read

Signature · 3 lines
ChenXiaoSong <[email protected]>
Chinese Homepage: https://chenxiaosong.com
English Homepage: https://chenxiaosong.com/en