linux-cifs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v3 0/9] smb: improve search speed of SMB2 maperror
@ 2025-12-05 13:25 chenxiaosong.chenxiaosong
  2025-12-05 13:25 ` [PATCH v3 1/9] smb/client: reduce loop count in map_smb2_to_linux_error() by half chenxiaosong.chenxiaosong
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: chenxiaosong.chenxiaosong @ 2025-12-05 13:25 UTC (permalink / raw)
  To: sfrench, smfrench, linkinjeon, linkinjeon
  Cc: linux-cifs, linux-kernel, liuzhengyuan, huhai, liuyun01,
	ChenXiaoSong

From: ChenXiaoSong <chenxiaosong@kylinos.cn>

Before applying this patchset, when searching for the last element of
smb2_error_map_table array and calling smb2_print_status(),
3486 comparisons are needed.

After applying this patchset, only 10 comparisons are required.

v1: https://lore.kernel.org/linux-cifs/20251204045818.2590727-1-chenxiaosong.chenxiaosong@linux.dev/
The three patches from v1 have already been applied to the for-next branch of cifs-2.6.git.
Please replace the following patches:

  - [v1 01/10] smb/client: reduce loop count in map_smb2_to_linux_error() by half: https://git.samba.org/sfrench/?p=sfrench/cifs-2.6.git;a=commitdiff;h=26866d690bd180e1860548c43e70fdefe50638ff
    - Replace it with this version(v3) patch #0001: update commit message: array has 1743 elements

  - [v1 02/10] smb/client: remove unused elements from smb2_error_map_table array: https://git.samba.org/sfrench/?p=sfrench/cifs-2.6.git;a=commitdiff;h=905d8999d67dcbe4ce12ef87996e4440e068196d
    - It is the same as patch #0002 in this version(v3).

  - [v1 03/10] smb: add two elements to smb2_error_map_table array: https://git.samba.org/sfrench/?p=sfrench/cifs-2.6.git;a=commitdiff;h=ba521f56912f6ff5121e54c17c855298f947c9ea
    - Replace it with this version(v3) patch #0003 #0004.

v2: https://lore.kernel.org/all/20251205042958.2658496-1-chenxiaosong.chenxiaosong@linux.dev/
v2->v3:
  - Patch #0007: move struct status_to_posix_error to smb2glob.h,
                 change smb2_get_err_map to be a global function,
                 delete unused forward declaration `struct statfs` in smb2proto.h.
  - Patch #0008: create new smb2maperror_test.c, refer to the implementation in fs/ext4/mballoc-test.c.

ChenXiaoSong (9):
  smb/client: reduce loop count in map_smb2_to_linux_error() by half
  smb/client: remove unused elements from smb2_error_map_table array
  smb: rename to STATUS_SMB_NO_PREAUTH_INTEGRITY_HASH_OVERLAP
  smb/client: add two elements to smb2_error_map_table array
  smb/client: sort smb2_error_map_table array
  smb/client: use bsearch() to find target status code
  smb/client: introduce smb2_get_err_map()
  smb/client: introduce smb2maperror KUnit tests
  smb/server: rename include guard in smb_common.h

 fs/smb/Kconfig                    | 17 ++++++
 fs/smb/client/cifsfs.c            |  2 +
 fs/smb/client/smb2glob.h          |  6 ++
 fs/smb/client/smb2maperror.c      | 91 ++++++++++++++++++-------------
 fs/smb/client/smb2maperror_test.c | 86 +++++++++++++++++++++++++++++
 fs/smb/client/smb2proto.h         |  4 +-
 fs/smb/common/smb2status.h        |  5 +-
 fs/smb/server/smb2pdu.c           |  2 +-
 fs/smb/server/smb_common.h        |  6 +-
 9 files changed, 174 insertions(+), 45 deletions(-)
 create mode 100644 fs/smb/client/smb2maperror_test.c

-- 
2.43.0


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

* [PATCH v3 1/9] smb/client: reduce loop count in map_smb2_to_linux_error() by half
  2025-12-05 13:25 [PATCH v3 0/9] smb: improve search speed of SMB2 maperror chenxiaosong.chenxiaosong
@ 2025-12-05 13:25 ` chenxiaosong.chenxiaosong
  2025-12-05 13:25 ` [PATCH v3 2/9] smb/client: remove unused elements from smb2_error_map_table array chenxiaosong.chenxiaosong
                   ` (7 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: chenxiaosong.chenxiaosong @ 2025-12-05 13:25 UTC (permalink / raw)
  To: sfrench, smfrench, linkinjeon, linkinjeon
  Cc: linux-cifs, linux-kernel, liuzhengyuan, huhai, liuyun01,
	ChenXiaoSong

From: ChenXiaoSong <chenxiaosong@kylinos.cn>

The smb2_error_map_table array currently has 1743 elements. When searching
for the last element and calling smb2_print_status(), 3486 comparisons
are needed.

The loop in smb2_print_status() is unnecessary, smb2_print_status() can be
removed, and only iterate over the array once, printing the message when
the target status code is found.

Signed-off-by: ChenXiaoSong <chenxiaosong@kylinos.cn>
---
 fs/smb/client/smb2maperror.c | 30 ++++++------------------------
 1 file changed, 6 insertions(+), 24 deletions(-)

diff --git a/fs/smb/client/smb2maperror.c b/fs/smb/client/smb2maperror.c
index 12c2b868789f..d1df6e518d21 100644
--- a/fs/smb/client/smb2maperror.c
+++ b/fs/smb/client/smb2maperror.c
@@ -2418,24 +2418,6 @@ static const struct status_to_posix_error smb2_error_map_table[] = {
 	{0, 0, NULL}
 };
 
-/*****************************************************************************
- Print an error message from the status code
- *****************************************************************************/
-static void
-smb2_print_status(__le32 status)
-{
-	int idx = 0;
-
-	while (smb2_error_map_table[idx].status_string != NULL) {
-		if ((smb2_error_map_table[idx].smb2_status) == status) {
-			pr_notice("Status code returned 0x%08x %s\n", status,
-				  smb2_error_map_table[idx].status_string);
-		}
-		idx++;
-	}
-	return;
-}
-
 int
 map_smb2_to_linux_error(char *buf, bool log_err)
 {
@@ -2452,16 +2434,16 @@ map_smb2_to_linux_error(char *buf, bool log_err)
 		return 0;
 	}
 
-	/* mask facility */
-	if (log_err && (smb2err != STATUS_MORE_PROCESSING_REQUIRED) &&
-	    (smb2err != STATUS_END_OF_FILE))
-		smb2_print_status(smb2err);
-	else if (cifsFYI & CIFS_RC)
-		smb2_print_status(smb2err);
+	log_err = (log_err && (smb2err != STATUS_MORE_PROCESSING_REQUIRED) &&
+		   (smb2err != STATUS_END_OF_FILE)) ||
+		  (cifsFYI & CIFS_RC);
 
 	for (i = 0; i < sizeof(smb2_error_map_table) /
 			sizeof(struct status_to_posix_error); i++) {
 		if (smb2_error_map_table[i].smb2_status == smb2err) {
+			if (log_err)
+				pr_notice("Status code returned 0x%08x %s\n", smb2err,
+					  smb2_error_map_table[i].status_string);
 			rc = smb2_error_map_table[i].posix_error;
 			break;
 		}
-- 
2.43.0


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

* [PATCH v3 2/9] smb/client: remove unused elements from smb2_error_map_table array
  2025-12-05 13:25 [PATCH v3 0/9] smb: improve search speed of SMB2 maperror chenxiaosong.chenxiaosong
  2025-12-05 13:25 ` [PATCH v3 1/9] smb/client: reduce loop count in map_smb2_to_linux_error() by half chenxiaosong.chenxiaosong
@ 2025-12-05 13:25 ` chenxiaosong.chenxiaosong
  2025-12-05 13:25 ` [PATCH v3 3/9] smb: rename to STATUS_SMB_NO_PREAUTH_INTEGRITY_HASH_OVERLAP chenxiaosong.chenxiaosong
                   ` (6 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: chenxiaosong.chenxiaosong @ 2025-12-05 13:25 UTC (permalink / raw)
  To: sfrench, smfrench, linkinjeon, linkinjeon
  Cc: linux-cifs, linux-kernel, liuzhengyuan, huhai, liuyun01,
	ChenXiaoSong

From: ChenXiaoSong <chenxiaosong@kylinos.cn>

STATUS_SUCCESS and STATUS_WAIT_0 are both zero, and since zero indicates
success, they are not needed.

Since smb2_print_status() has been removed, the last element in the array
is no longer needed.

Signed-off-by: ChenXiaoSong <chenxiaosong@kylinos.cn>
---
 fs/smb/client/smb2maperror.c | 3 ---
 1 file changed, 3 deletions(-)

diff --git a/fs/smb/client/smb2maperror.c b/fs/smb/client/smb2maperror.c
index d1df6e518d21..118e32cc8edc 100644
--- a/fs/smb/client/smb2maperror.c
+++ b/fs/smb/client/smb2maperror.c
@@ -23,8 +23,6 @@ struct status_to_posix_error {
 };
 
 static const struct status_to_posix_error smb2_error_map_table[] = {
-	{STATUS_SUCCESS, 0, "STATUS_SUCCESS"},
-	{STATUS_WAIT_0,  0, "STATUS_WAIT_0"},
 	{STATUS_WAIT_1, -EIO, "STATUS_WAIT_1"},
 	{STATUS_WAIT_2, -EIO, "STATUS_WAIT_2"},
 	{STATUS_WAIT_3, -EIO, "STATUS_WAIT_3"},
@@ -2415,7 +2413,6 @@ static const struct status_to_posix_error smb2_error_map_table[] = {
 	{STATUS_IPSEC_INTEGRITY_CHECK_FAILED, -EIO,
 	"STATUS_IPSEC_INTEGRITY_CHECK_FAILED"},
 	{STATUS_IPSEC_CLEAR_TEXT_DROP, -EIO, "STATUS_IPSEC_CLEAR_TEXT_DROP"},
-	{0, 0, NULL}
 };
 
 int
-- 
2.43.0


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

* [PATCH v3 3/9] smb: rename to STATUS_SMB_NO_PREAUTH_INTEGRITY_HASH_OVERLAP
  2025-12-05 13:25 [PATCH v3 0/9] smb: improve search speed of SMB2 maperror chenxiaosong.chenxiaosong
  2025-12-05 13:25 ` [PATCH v3 1/9] smb/client: reduce loop count in map_smb2_to_linux_error() by half chenxiaosong.chenxiaosong
  2025-12-05 13:25 ` [PATCH v3 2/9] smb/client: remove unused elements from smb2_error_map_table array chenxiaosong.chenxiaosong
@ 2025-12-05 13:25 ` chenxiaosong.chenxiaosong
  2025-12-05 13:25 ` [PATCH v3 4/9] smb/client: add two elements to smb2_error_map_table array chenxiaosong.chenxiaosong
                   ` (5 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: chenxiaosong.chenxiaosong @ 2025-12-05 13:25 UTC (permalink / raw)
  To: sfrench, smfrench, linkinjeon, linkinjeon
  Cc: linux-cifs, linux-kernel, liuzhengyuan, huhai, liuyun01,
	ChenXiaoSong

From: ChenXiaoSong <chenxiaosong@kylinos.cn>

See MS-SMB2 3.3.5.4. To keep the name consistent with the documentation.

Additionally, move STATUS_INVALID_LOCK_RANGE to correct position in order.

Signed-off-by: ChenXiaoSong <chenxiaosong@kylinos.cn>
---
 fs/smb/common/smb2status.h | 5 +++--
 fs/smb/server/smb2pdu.c    | 2 +-
 2 files changed, 4 insertions(+), 3 deletions(-)

diff --git a/fs/smb/common/smb2status.h b/fs/smb/common/smb2status.h
index 14b4a5f04564..7d6b8ed304fc 100644
--- a/fs/smb/common/smb2status.h
+++ b/fs/smb/common/smb2status.h
@@ -631,6 +631,7 @@ struct ntstatus {
 #define STATUS_DOMAIN_TRUST_INCONSISTENT cpu_to_le32(0xC000019B)
 #define STATUS_FS_DRIVER_REQUIRED cpu_to_le32(0xC000019C)
 #define STATUS_IMAGE_ALREADY_LOADED_AS_DLL cpu_to_le32(0xC000019D)
+#define STATUS_INVALID_LOCK_RANGE cpu_to_le32(0xC00001A1)
 #define STATUS_NETWORK_OPEN_RESTRICTION cpu_to_le32(0xC0000201)
 #define STATUS_NO_USER_SESSION_KEY cpu_to_le32(0xC0000202)
 #define STATUS_USER_SESSION_DELETED cpu_to_le32(0xC0000203)
@@ -1773,5 +1774,5 @@ struct ntstatus {
 #define STATUS_IPSEC_INVALID_PACKET cpu_to_le32(0xC0360005)
 #define STATUS_IPSEC_INTEGRITY_CHECK_FAILED cpu_to_le32(0xC0360006)
 #define STATUS_IPSEC_CLEAR_TEXT_DROP cpu_to_le32(0xC0360007)
-#define STATUS_NO_PREAUTH_INTEGRITY_HASH_OVERLAP cpu_to_le32(0xC05D0000)
-#define STATUS_INVALID_LOCK_RANGE cpu_to_le32(0xC00001a1)
+/* See MS-SMB2 3.3.5.4 */
+#define STATUS_SMB_NO_PREAUTH_INTEGRITY_HASH_OVERLAP cpu_to_le32(0xC05D0000)
diff --git a/fs/smb/server/smb2pdu.c b/fs/smb/server/smb2pdu.c
index a4f6de350df8..b1323f1b100e 100644
--- a/fs/smb/server/smb2pdu.c
+++ b/fs/smb/server/smb2pdu.c
@@ -896,7 +896,7 @@ static __le32 decode_preauth_ctxt(struct ksmbd_conn *conn,
 		return STATUS_INVALID_PARAMETER;
 
 	if (pneg_ctxt->HashAlgorithms != SMB2_PREAUTH_INTEGRITY_SHA512)
-		return STATUS_NO_PREAUTH_INTEGRITY_HASH_OVERLAP;
+		return STATUS_SMB_NO_PREAUTH_INTEGRITY_HASH_OVERLAP;
 
 	conn->preauth_info->Preauth_HashId = SMB2_PREAUTH_INTEGRITY_SHA512;
 	return STATUS_SUCCESS;
-- 
2.43.0


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

* [PATCH v3 4/9] smb/client: add two elements to smb2_error_map_table array
  2025-12-05 13:25 [PATCH v3 0/9] smb: improve search speed of SMB2 maperror chenxiaosong.chenxiaosong
                   ` (2 preceding siblings ...)
  2025-12-05 13:25 ` [PATCH v3 3/9] smb: rename to STATUS_SMB_NO_PREAUTH_INTEGRITY_HASH_OVERLAP chenxiaosong.chenxiaosong
@ 2025-12-05 13:25 ` chenxiaosong.chenxiaosong
  2025-12-05 13:25 ` [PATCH v3 5/9] smb/client: sort " chenxiaosong.chenxiaosong
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: chenxiaosong.chenxiaosong @ 2025-12-05 13:25 UTC (permalink / raw)
  To: sfrench, smfrench, linkinjeon, linkinjeon
  Cc: linux-cifs, linux-kernel, liuzhengyuan, huhai, liuyun01,
	ChenXiaoSong

From: ChenXiaoSong <chenxiaosong@kylinos.cn>

Both status codes are mapped to -EIO.

Now all status codes from common/smb2status.h are included in the
smb2_error_map_table array(except for the first two zero definitions).

Signed-off-by: ChenXiaoSong <chenxiaosong@kylinos.cn>
---
 fs/smb/client/smb2maperror.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/fs/smb/client/smb2maperror.c b/fs/smb/client/smb2maperror.c
index 118e32cc8edc..a77467d2d81c 100644
--- a/fs/smb/client/smb2maperror.c
+++ b/fs/smb/client/smb2maperror.c
@@ -734,6 +734,7 @@ static const struct status_to_posix_error smb2_error_map_table[] = {
 	{STATUS_FS_DRIVER_REQUIRED, -EOPNOTSUPP, "STATUS_FS_DRIVER_REQUIRED"},
 	{STATUS_IMAGE_ALREADY_LOADED_AS_DLL, -EIO,
 	"STATUS_IMAGE_ALREADY_LOADED_AS_DLL"},
+	{STATUS_INVALID_LOCK_RANGE, -EIO, "STATUS_INVALID_LOCK_RANGE"},
 	{STATUS_NETWORK_OPEN_RESTRICTION, -EIO,
 	"STATUS_NETWORK_OPEN_RESTRICTION"},
 	{STATUS_NO_USER_SESSION_KEY, -EIO, "STATUS_NO_USER_SESSION_KEY"},
@@ -2413,6 +2414,8 @@ static const struct status_to_posix_error smb2_error_map_table[] = {
 	{STATUS_IPSEC_INTEGRITY_CHECK_FAILED, -EIO,
 	"STATUS_IPSEC_INTEGRITY_CHECK_FAILED"},
 	{STATUS_IPSEC_CLEAR_TEXT_DROP, -EIO, "STATUS_IPSEC_CLEAR_TEXT_DROP"},
+	{STATUS_SMB_NO_PREAUTH_INTEGRITY_HASH_OVERLAP, -EIO,
+	"STATUS_SMB_NO_PREAUTH_INTEGRITY_HASH_OVERLAP"},
 };
 
 int
-- 
2.43.0


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

* [PATCH v3 5/9] smb/client: sort smb2_error_map_table array
  2025-12-05 13:25 [PATCH v3 0/9] smb: improve search speed of SMB2 maperror chenxiaosong.chenxiaosong
                   ` (3 preceding siblings ...)
  2025-12-05 13:25 ` [PATCH v3 4/9] smb/client: add two elements to smb2_error_map_table array chenxiaosong.chenxiaosong
@ 2025-12-05 13:25 ` chenxiaosong.chenxiaosong
  2025-12-05 13:25 ` [PATCH v3 6/9] smb/client: use bsearch() to find target status code chenxiaosong.chenxiaosong
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: chenxiaosong.chenxiaosong @ 2025-12-05 13:25 UTC (permalink / raw)
  To: sfrench, smfrench, linkinjeon, linkinjeon
  Cc: linux-cifs, linux-kernel, liuzhengyuan, huhai, liuyun01,
	ChenXiaoSong

From: ChenXiaoSong <chenxiaosong@kylinos.cn>

Sort the array in ascending order, and then we can use binary search
algorithm to quickly find the target status code.

Since the smb2_init_maperror() is called only once when the module is
loaded, the array is sorted just once.

Signed-off-by: ChenXiaoSong <chenxiaosong@kylinos.cn>
---
 fs/smb/client/cifsfs.c       |  2 ++
 fs/smb/client/smb2maperror.c | 25 ++++++++++++++++++++++++-
 fs/smb/client/smb2proto.h    |  1 +
 3 files changed, 27 insertions(+), 1 deletion(-)

diff --git a/fs/smb/client/cifsfs.c b/fs/smb/client/cifsfs.c
index 6eccb9ed9daa..77d14b3dd650 100644
--- a/fs/smb/client/cifsfs.c
+++ b/fs/smb/client/cifsfs.c
@@ -49,6 +49,7 @@
 #endif
 #include "fs_context.h"
 #include "cached_dir.h"
+#include "smb2proto.h"
 
 /*
  * DOS dates from 1980/1/1 through 2107/12/31
@@ -1908,6 +1909,7 @@ static int __init
 init_cifs(void)
 {
 	int rc = 0;
+	smb2_init_maperror();
 	cifs_proc_init();
 	INIT_LIST_HEAD(&cifs_tcp_ses_list);
 /*
diff --git a/fs/smb/client/smb2maperror.c b/fs/smb/client/smb2maperror.c
index a77467d2d81c..1bd4196386dd 100644
--- a/fs/smb/client/smb2maperror.c
+++ b/fs/smb/client/smb2maperror.c
@@ -8,6 +8,7 @@
  *
  */
 #include <linux/errno.h>
+#include <linux/sort.h>
 #include "cifsglob.h"
 #include "cifs_debug.h"
 #include "smb2pdu.h"
@@ -22,7 +23,7 @@ struct status_to_posix_error {
 	char *status_string;
 };
 
-static const struct status_to_posix_error smb2_error_map_table[] = {
+static struct status_to_posix_error smb2_error_map_table[] = {
 	{STATUS_WAIT_1, -EIO, "STATUS_WAIT_1"},
 	{STATUS_WAIT_2, -EIO, "STATUS_WAIT_2"},
 	{STATUS_WAIT_3, -EIO, "STATUS_WAIT_3"},
@@ -2418,6 +2419,20 @@ static const struct status_to_posix_error smb2_error_map_table[] = {
 	"STATUS_SMB_NO_PREAUTH_INTEGRITY_HASH_OVERLAP"},
 };
 
+static unsigned int err_map_num = sizeof(smb2_error_map_table) /
+				     sizeof(struct status_to_posix_error);
+
+static int cmp_smb2_status(const void *_a, const void *_b)
+{
+	const struct status_to_posix_error *a = _a, *b = _b;
+
+	if (a->smb2_status < b->smb2_status)
+		return -1;
+	if (a->smb2_status > b->smb2_status)
+		return 1;
+	return 0;
+}
+
 int
 map_smb2_to_linux_error(char *buf, bool log_err)
 {
@@ -2461,3 +2476,11 @@ map_smb2_to_linux_error(char *buf, bool log_err)
 			   le32_to_cpu(smb2err), rc);
 	return rc;
 }
+
+void smb2_init_maperror(void)
+{
+	/* Sort in ascending order */
+	sort(smb2_error_map_table, err_map_num,
+	     sizeof(struct status_to_posix_error),
+	     cmp_smb2_status, NULL);
+}
diff --git a/fs/smb/client/smb2proto.h b/fs/smb/client/smb2proto.h
index 5241daaae543..c988f6b37a1b 100644
--- a/fs/smb/client/smb2proto.h
+++ b/fs/smb/client/smb2proto.h
@@ -21,6 +21,7 @@ struct smb_rqst;
  *****************************************************************
  */
 extern int map_smb2_to_linux_error(char *buf, bool log_err);
+extern void smb2_init_maperror(void);
 extern int smb2_check_message(char *buf, unsigned int length,
 			      struct TCP_Server_Info *server);
 extern unsigned int smb2_calc_size(void *buf);
-- 
2.43.0


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

* [PATCH v3 6/9] smb/client: use bsearch() to find target status code
  2025-12-05 13:25 [PATCH v3 0/9] smb: improve search speed of SMB2 maperror chenxiaosong.chenxiaosong
                   ` (4 preceding siblings ...)
  2025-12-05 13:25 ` [PATCH v3 5/9] smb/client: sort " chenxiaosong.chenxiaosong
@ 2025-12-05 13:25 ` chenxiaosong.chenxiaosong
  2025-12-05 13:25 ` [PATCH v3 7/9] smb/client: introduce smb2_get_err_map() chenxiaosong.chenxiaosong
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: chenxiaosong.chenxiaosong @ 2025-12-05 13:25 UTC (permalink / raw)
  To: sfrench, smfrench, linkinjeon, linkinjeon
  Cc: linux-cifs, linux-kernel, liuzhengyuan, huhai, liuyun01,
	ChenXiaoSong

From: ChenXiaoSong <chenxiaosong@kylinos.cn>

The smb2_error_map_table array currently has 1742 elements. When searching
for the last element, the original loop-based search method requires 1742
comparisons, while binary search algorithm requires only 10 comparisons.

Signed-off-by: ChenXiaoSong <chenxiaosong@kylinos.cn>
---
 fs/smb/client/smb2maperror.c | 26 +++++++++++++++-----------
 1 file changed, 15 insertions(+), 11 deletions(-)

diff --git a/fs/smb/client/smb2maperror.c b/fs/smb/client/smb2maperror.c
index 1bd4196386dd..df8db12ff7a9 100644
--- a/fs/smb/client/smb2maperror.c
+++ b/fs/smb/client/smb2maperror.c
@@ -2437,9 +2437,9 @@ int
 map_smb2_to_linux_error(char *buf, bool log_err)
 {
 	struct smb2_hdr *shdr = (struct smb2_hdr *)buf;
-	unsigned int i;
 	int rc = -EIO;
 	__le32 smb2err = shdr->Status;
+	struct status_to_posix_error *err_map, key;
 
 	if (smb2err == 0) {
 		trace_smb3_cmd_done(le32_to_cpu(shdr->Id.SyncId.TreeId),
@@ -2453,17 +2453,21 @@ map_smb2_to_linux_error(char *buf, bool log_err)
 		   (smb2err != STATUS_END_OF_FILE)) ||
 		  (cifsFYI & CIFS_RC);
 
-	for (i = 0; i < sizeof(smb2_error_map_table) /
-			sizeof(struct status_to_posix_error); i++) {
-		if (smb2_error_map_table[i].smb2_status == smb2err) {
-			if (log_err)
-				pr_notice("Status code returned 0x%08x %s\n", smb2err,
-					  smb2_error_map_table[i].status_string);
-			rc = smb2_error_map_table[i].posix_error;
-			break;
-		}
-	}
+	key = (struct status_to_posix_error) {
+		.smb2_status = smb2err,
+	};
+	err_map = bsearch(&key, smb2_error_map_table, err_map_num,
+			  sizeof(struct status_to_posix_error),
+			  cmp_smb2_status);
+	if (!err_map)
+		goto out;
+
+	rc = err_map->posix_error;
+	if (log_err)
+		pr_notice("Status code returned 0x%08x %s\n", smb2err,
+			  err_map->status_string);
 
+out:
 	/* on error mapping not found  - return EIO */
 
 	cifs_dbg(FYI, "Mapping SMB2 status code 0x%08x to POSIX err %d\n",
-- 
2.43.0


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

* [PATCH v3 7/9] smb/client: introduce smb2_get_err_map()
  2025-12-05 13:25 [PATCH v3 0/9] smb: improve search speed of SMB2 maperror chenxiaosong.chenxiaosong
                   ` (5 preceding siblings ...)
  2025-12-05 13:25 ` [PATCH v3 6/9] smb/client: use bsearch() to find target status code chenxiaosong.chenxiaosong
@ 2025-12-05 13:25 ` chenxiaosong.chenxiaosong
  2025-12-05 13:25 ` [PATCH v3 8/9] smb/client: introduce smb2maperror KUnit tests chenxiaosong.chenxiaosong
  2025-12-05 13:25 ` [PATCH v3 9/9] smb/server: rename include guard in smb_common.h chenxiaosong.chenxiaosong
  8 siblings, 0 replies; 10+ messages in thread
From: chenxiaosong.chenxiaosong @ 2025-12-05 13:25 UTC (permalink / raw)
  To: sfrench, smfrench, linkinjeon, linkinjeon
  Cc: linux-cifs, linux-kernel, liuzhengyuan, huhai, liuyun01,
	ChenXiaoSong

From: ChenXiaoSong <chenxiaosong@kylinos.cn>

We may need to get struct status_to_posix_error information in other files
in the future. So we move struct status_to_posix_error to smb2glob.h, and
introduce new global function smb2_get_err_map().

Additionally, delete unused forward declaration `struct statfs`
in smb2proto.h.

Signed-off-by: ChenXiaoSong <chenxiaosong@kylinos.cn>
---
 fs/smb/client/smb2glob.h     |  6 ++++++
 fs/smb/client/smb2maperror.c | 28 +++++++++++++++-------------
 fs/smb/client/smb2proto.h    |  3 ++-
 3 files changed, 23 insertions(+), 14 deletions(-)

diff --git a/fs/smb/client/smb2glob.h b/fs/smb/client/smb2glob.h
index e56e4d402f13..fe6a5c7f01c4 100644
--- a/fs/smb/client/smb2glob.h
+++ b/fs/smb/client/smb2glob.h
@@ -13,6 +13,12 @@
 #ifndef _SMB2_GLOB_H
 #define _SMB2_GLOB_H
 
+struct status_to_posix_error {
+	__le32 smb2_status;
+	int posix_error;
+	char *status_string;
+};
+
 /*
  *****************************************************************
  * Constants go here
diff --git a/fs/smb/client/smb2maperror.c b/fs/smb/client/smb2maperror.c
index df8db12ff7a9..0d46fa98952c 100644
--- a/fs/smb/client/smb2maperror.c
+++ b/fs/smb/client/smb2maperror.c
@@ -17,12 +17,6 @@
 #include "smb2glob.h"
 #include "trace.h"
 
-struct status_to_posix_error {
-	__le32 smb2_status;
-	int posix_error;
-	char *status_string;
-};
-
 static struct status_to_posix_error smb2_error_map_table[] = {
 	{STATUS_WAIT_1, -EIO, "STATUS_WAIT_1"},
 	{STATUS_WAIT_2, -EIO, "STATUS_WAIT_2"},
@@ -2433,13 +2427,26 @@ static int cmp_smb2_status(const void *_a, const void *_b)
 	return 0;
 }
 
+struct status_to_posix_error *smb2_get_err_map(__le32 smb2_status)
+{
+	struct status_to_posix_error *err_map, key;
+
+	key = (struct status_to_posix_error) {
+		.smb2_status = smb2_status,
+	};
+	err_map = bsearch(&key, smb2_error_map_table, err_map_num,
+			  sizeof(struct status_to_posix_error),
+			  cmp_smb2_status);
+	return err_map;
+}
+
 int
 map_smb2_to_linux_error(char *buf, bool log_err)
 {
 	struct smb2_hdr *shdr = (struct smb2_hdr *)buf;
 	int rc = -EIO;
 	__le32 smb2err = shdr->Status;
-	struct status_to_posix_error *err_map, key;
+	struct status_to_posix_error *err_map;
 
 	if (smb2err == 0) {
 		trace_smb3_cmd_done(le32_to_cpu(shdr->Id.SyncId.TreeId),
@@ -2453,12 +2460,7 @@ map_smb2_to_linux_error(char *buf, bool log_err)
 		   (smb2err != STATUS_END_OF_FILE)) ||
 		  (cifsFYI & CIFS_RC);
 
-	key = (struct status_to_posix_error) {
-		.smb2_status = smb2err,
-	};
-	err_map = bsearch(&key, smb2_error_map_table, err_map_num,
-			  sizeof(struct status_to_posix_error),
-			  cmp_smb2_status);
+	err_map = smb2_get_err_map(smb2err);
 	if (!err_map)
 		goto out;
 
diff --git a/fs/smb/client/smb2proto.h b/fs/smb/client/smb2proto.h
index c988f6b37a1b..224840e3a00d 100644
--- a/fs/smb/client/smb2proto.h
+++ b/fs/smb/client/smb2proto.h
@@ -12,8 +12,8 @@
 #include <linux/nls.h>
 #include <linux/key-type.h>
 
-struct statfs;
 struct smb_rqst;
+struct status_to_posix_error;
 
 /*
  *****************************************************************
@@ -21,6 +21,7 @@ struct smb_rqst;
  *****************************************************************
  */
 extern int map_smb2_to_linux_error(char *buf, bool log_err);
+extern struct status_to_posix_error *smb2_get_err_map(__le32 smb2_status);
 extern void smb2_init_maperror(void);
 extern int smb2_check_message(char *buf, unsigned int length,
 			      struct TCP_Server_Info *server);
-- 
2.43.0


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

* [PATCH v3 8/9] smb/client: introduce smb2maperror KUnit tests
  2025-12-05 13:25 [PATCH v3 0/9] smb: improve search speed of SMB2 maperror chenxiaosong.chenxiaosong
                   ` (6 preceding siblings ...)
  2025-12-05 13:25 ` [PATCH v3 7/9] smb/client: introduce smb2_get_err_map() chenxiaosong.chenxiaosong
@ 2025-12-05 13:25 ` chenxiaosong.chenxiaosong
  2025-12-05 13:25 ` [PATCH v3 9/9] smb/server: rename include guard in smb_common.h chenxiaosong.chenxiaosong
  8 siblings, 0 replies; 10+ messages in thread
From: chenxiaosong.chenxiaosong @ 2025-12-05 13:25 UTC (permalink / raw)
  To: sfrench, smfrench, linkinjeon, linkinjeon
  Cc: linux-cifs, linux-kernel, liuzhengyuan, huhai, liuyun01,
	ChenXiaoSong

From: ChenXiaoSong <chenxiaosong@kylinos.cn>

The KUnit tests are executed when cifs.ko is loaded.

Just like fs/ext4/mballoc.c includes fs/ext4/mballoc-test.c.
smb2maperror.c also includes smb2maperror_test.c, allowing KUnit tests to
access any functions and variables in smb2maperror.c.

The maperror_test_check_sort() checks whether the array is properly sorted.

The maperror_test_get_err_map() checks whether the expected element can be
correctly searched for in the smb2_error_map_table array.

Signed-off-by: ChenXiaoSong <chenxiaosong@kylinos.cn>
---
 fs/smb/Kconfig                    | 17 ++++++
 fs/smb/client/smb2maperror.c      |  4 ++
 fs/smb/client/smb2maperror_test.c | 86 +++++++++++++++++++++++++++++++
 3 files changed, 107 insertions(+)
 create mode 100644 fs/smb/client/smb2maperror_test.c

diff --git a/fs/smb/Kconfig b/fs/smb/Kconfig
index ef425789fa6a..85f7ad5fbc5e 100644
--- a/fs/smb/Kconfig
+++ b/fs/smb/Kconfig
@@ -9,3 +9,20 @@ config SMBFS
 	tristate
 	default y if CIFS=y || SMB_SERVER=y
 	default m if CIFS=m || SMB_SERVER=m
+
+config SMB_KUNIT_TESTS
+	tristate "KUnit tests for SMB" if !KUNIT_ALL_TESTS
+	depends on SMBFS && KUNIT
+	default KUNIT_ALL_TESTS
+	help
+	  This builds the SMB KUnit tests.
+
+	  KUnit tests run during boot and output the results to the debug log
+	  in TAP format (https://testanything.org/). Only useful for kernel devs
+	  running KUnit test harness and are not for inclusion into a production
+	  build.
+
+	  For more information on KUnit and unit tests in general please refer
+	  to the KUnit documentation in Documentation/dev-tools/kunit/.
+
+	  If unsure, say N.
diff --git a/fs/smb/client/smb2maperror.c b/fs/smb/client/smb2maperror.c
index 0d46fa98952c..dc2edeafc93b 100644
--- a/fs/smb/client/smb2maperror.c
+++ b/fs/smb/client/smb2maperror.c
@@ -2490,3 +2490,7 @@ void smb2_init_maperror(void)
 	     sizeof(struct status_to_posix_error),
 	     cmp_smb2_status, NULL);
 }
+
+#if IS_ENABLED(CONFIG_SMB_KUNIT_TESTS)
+#include "smb2maperror_test.c"
+#endif /* CONFIG_SMB_KUNIT_TESTS */
diff --git a/fs/smb/client/smb2maperror_test.c b/fs/smb/client/smb2maperror_test.c
new file mode 100644
index 000000000000..d2f3123e0676
--- /dev/null
+++ b/fs/smb/client/smb2maperror_test.c
@@ -0,0 +1,86 @@
+// SPDX-License-Identifier: LGPL-2.1
+/*
+ *
+ *   KUnit tests of SMB2 maperror
+ *
+ *   Copyright (C) 2025 KylinSoft Co., Ltd. All rights reserved.
+ *   Author(s): ChenXiaoSong <chenxiaosong@kylinos.cn>
+ *
+ */
+
+#include <kunit/test.h>
+
+static void maperror_test_check_sort(struct kunit *test)
+{
+	bool is_sorted = true;
+	unsigned int i;
+
+	for (i = 1; i < err_map_num; i++) {
+		if (smb2_error_map_table[i].smb2_status >=
+		    smb2_error_map_table[i - 1].smb2_status)
+			continue;
+
+		pr_err("smb2_error_map_table array order is incorrect\n");
+		is_sorted = false;
+		break;
+	}
+
+	KUNIT_EXPECT_EQ(test, true, is_sorted);
+}
+
+static void
+get_and_cmp_err_map(struct kunit *test, struct status_to_posix_error *expect)
+{
+	struct status_to_posix_error *result;
+
+	result = smb2_get_err_map(expect->smb2_status);
+	KUNIT_EXPECT_PTR_NE(test, NULL, result);
+	KUNIT_EXPECT_EQ(test, expect->posix_error, result->posix_error);
+	KUNIT_EXPECT_STREQ(test, expect->status_string, result->status_string);
+}
+
+static void maperror_test_get_err_map(struct kunit *test)
+{
+	struct status_to_posix_error expect;
+
+	/* first element */
+	expect = smb2_error_map_table[0];
+	get_and_cmp_err_map(test, &expect);
+
+	/* last element */
+	expect = smb2_error_map_table[err_map_num - 1];
+	get_and_cmp_err_map(test, &expect);
+
+	expect = (struct status_to_posix_error) {
+		.smb2_status = STATUS_SERIAL_COUNTER_TIMEOUT,
+		.posix_error = -ETIMEDOUT,
+		.status_string = "STATUS_SERIAL_COUNTER_TIMEOUT",
+	};
+	get_and_cmp_err_map(test, &expect);
+
+	expect = (struct status_to_posix_error) {
+		.smb2_status = STATUS_IO_REPARSE_TAG_NOT_HANDLED,
+		.posix_error = -EOPNOTSUPP,
+		.status_string = "STATUS_REPARSE_NOT_HANDLED",
+	};
+	get_and_cmp_err_map(test, &expect);
+}
+
+/*
+ * Before running these test cases, the smb2_init_maperror()
+ * function is called first.
+ */
+static struct kunit_case maperror_test_cases[] = {
+	KUNIT_CASE(maperror_test_check_sort),
+	KUNIT_CASE(maperror_test_get_err_map),
+	{}
+};
+
+static struct kunit_suite maperror_suite = {
+	.name = "smb2_maperror",
+	.test_cases = maperror_test_cases,
+};
+
+kunit_test_suite(maperror_suite);
+
+MODULE_LICENSE("GPL");
-- 
2.43.0


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

* [PATCH v3 9/9] smb/server: rename include guard in smb_common.h
  2025-12-05 13:25 [PATCH v3 0/9] smb: improve search speed of SMB2 maperror chenxiaosong.chenxiaosong
                   ` (7 preceding siblings ...)
  2025-12-05 13:25 ` [PATCH v3 8/9] smb/client: introduce smb2maperror KUnit tests chenxiaosong.chenxiaosong
@ 2025-12-05 13:25 ` chenxiaosong.chenxiaosong
  8 siblings, 0 replies; 10+ messages in thread
From: chenxiaosong.chenxiaosong @ 2025-12-05 13:25 UTC (permalink / raw)
  To: sfrench, smfrench, linkinjeon, linkinjeon
  Cc: linux-cifs, linux-kernel, liuzhengyuan, huhai, liuyun01,
	ChenXiaoSong

From: ChenXiaoSong <chenxiaosong@kylinos.cn>

Make the include guard more descriptive to avoid conflicts with include
guards that may be used in the future.

Signed-off-by: ChenXiaoSong <chenxiaosong@kylinos.cn>
---
 fs/smb/server/smb_common.h | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/fs/smb/server/smb_common.h b/fs/smb/server/smb_common.h
index f47ce4a6719c..302c82480799 100644
--- a/fs/smb/server/smb_common.h
+++ b/fs/smb/server/smb_common.h
@@ -3,8 +3,8 @@
  *   Copyright (C) 2018 Samsung Electronics Co., Ltd.
  */
 
-#ifndef __SMB_COMMON_H__
-#define __SMB_COMMON_H__
+#ifndef __SMB_SERVER_COMMON_H__
+#define __SMB_SERVER_COMMON_H__
 
 #include <linux/kernel.h>
 
@@ -196,4 +196,4 @@ unsigned int ksmbd_server_side_copy_max_chunk_size(void);
 unsigned int ksmbd_server_side_copy_max_total_size(void);
 bool is_asterisk(char *p);
 __le32 smb_map_generic_desired_access(__le32 daccess);
-#endif /* __SMB_COMMON_H__ */
+#endif /* __SMB_SERVER_COMMON_H__ */
-- 
2.43.0


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

end of thread, other threads:[~2025-12-05 13:27 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-12-05 13:25 [PATCH v3 0/9] smb: improve search speed of SMB2 maperror chenxiaosong.chenxiaosong
2025-12-05 13:25 ` [PATCH v3 1/9] smb/client: reduce loop count in map_smb2_to_linux_error() by half chenxiaosong.chenxiaosong
2025-12-05 13:25 ` [PATCH v3 2/9] smb/client: remove unused elements from smb2_error_map_table array chenxiaosong.chenxiaosong
2025-12-05 13:25 ` [PATCH v3 3/9] smb: rename to STATUS_SMB_NO_PREAUTH_INTEGRITY_HASH_OVERLAP chenxiaosong.chenxiaosong
2025-12-05 13:25 ` [PATCH v3 4/9] smb/client: add two elements to smb2_error_map_table array chenxiaosong.chenxiaosong
2025-12-05 13:25 ` [PATCH v3 5/9] smb/client: sort " chenxiaosong.chenxiaosong
2025-12-05 13:25 ` [PATCH v3 6/9] smb/client: use bsearch() to find target status code chenxiaosong.chenxiaosong
2025-12-05 13:25 ` [PATCH v3 7/9] smb/client: introduce smb2_get_err_map() chenxiaosong.chenxiaosong
2025-12-05 13:25 ` [PATCH v3 8/9] smb/client: introduce smb2maperror KUnit tests chenxiaosong.chenxiaosong
2025-12-05 13:25 ` [PATCH v3 9/9] smb/server: rename include guard in smb_common.h chenxiaosong.chenxiaosong

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).