From: chenxiaosong.chenxiaosong@linux.dev
To: sfrench@samba.org, smfrench@gmail.com, linkinjeon@kernel.org,
linkinjeon@samba.org
Cc: linux-cifs@vger.kernel.org, linux-kernel@vger.kernel.org,
chenxiaosong@chenxiaosong.com,
ChenXiaoSong <chenxiaosong@kylinos.cn>
Subject: [PATCH 10/10] smb: move client/smb2maperror.c to common/
Date: Thu, 4 Dec 2025 12:58:18 +0800 [thread overview]
Message-ID: <20251204045818.2590727-11-chenxiaosong.chenxiaosong@linux.dev> (raw)
In-Reply-To: <20251204045818.2590727-1-chenxiaosong.chenxiaosong@linux.dev>
From: ChenXiaoSong <chenxiaosong@kylinos.cn>
The maperror can also be used by ksmbd, so move it to common/.
- client/smb2maperror.c -> common/smb2maperror.c
- Move map_smb2_to_linux_error() into client/smb2misc.c
- smb2_init_maperror() is called from smb_common_init()
- Move struct status_to_posix_error into common/common.h
- Export symbol smb2_get_err_map()
- The KUnit tests are executed when smb_common.ko is loaded
Signed-off-by: ChenXiaoSong <chenxiaosong@kylinos.cn>
---
fs/smb/client/Makefile | 2 +-
fs/smb/client/cifsfs.c | 2 -
fs/smb/client/smb2misc.c | 44 ++++++++++++++++
fs/smb/client/smb2proto.h | 1 -
fs/smb/common/Makefile | 2 +-
fs/smb/common/common.c | 2 +
fs/smb/common/common.h | 13 +++++
fs/smb/{client => common}/smb2maperror.c | 64 +++---------------------
8 files changed, 67 insertions(+), 63 deletions(-)
rename fs/smb/{client => common}/smb2maperror.c (98%)
diff --git a/fs/smb/client/Makefile b/fs/smb/client/Makefile
index 4c97b31a25c2..e84df2af8be6 100644
--- a/fs/smb/client/Makefile
+++ b/fs/smb/client/Makefile
@@ -9,7 +9,7 @@ cifs-y := trace.o cifsfs.o cifs_debug.o connect.o dir.o file.o \
inode.o link.o misc.o netmisc.o smbencrypt.o transport.o \
cached_dir.o cifs_unicode.o nterr.o cifsencrypt.o \
readdir.o ioctl.o sess.o export.o unc.o winucase.o \
- smb2ops.o smb2maperror.o smb2transport.o \
+ smb2ops.o smb2transport.o \
smb2misc.o smb2pdu.o smb2inode.o smb2file.o cifsacl.o fs_context.o \
dns_resolve.o cifs_spnego_negtokeninit.asn1.o asn1.o \
namespace.o reparse.o
diff --git a/fs/smb/client/cifsfs.c b/fs/smb/client/cifsfs.c
index 77d14b3dd650..6eccb9ed9daa 100644
--- a/fs/smb/client/cifsfs.c
+++ b/fs/smb/client/cifsfs.c
@@ -49,7 +49,6 @@
#endif
#include "fs_context.h"
#include "cached_dir.h"
-#include "smb2proto.h"
/*
* DOS dates from 1980/1/1 through 2107/12/31
@@ -1909,7 +1908,6 @@ 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/smb2misc.c b/fs/smb/client/smb2misc.c
index 96bfe4c63ccf..2cdcc9e6f47f 100644
--- a/fs/smb/client/smb2misc.c
+++ b/fs/smb/client/smb2misc.c
@@ -18,6 +18,7 @@
#include "smb2glob.h"
#include "nterr.h"
#include "cached_dir.h"
+#include "../common/common.h"
static int
check_smb2_hdr(struct smb2_hdr *shdr, __u64 mid)
@@ -927,3 +928,46 @@ smb311_update_preauth_hash(struct cifs_ses *ses, struct TCP_Server_Info *server,
sha512_update(&sha_ctx, iov[i].iov_base, iov[i].iov_len);
sha512_final(&sha_ctx, ses->preauth_sha_hash);
}
+
+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;
+
+ if (smb2err == 0) {
+ trace_smb3_cmd_done(le32_to_cpu(shdr->Id.SyncId.TreeId),
+ le64_to_cpu(shdr->SessionId),
+ le16_to_cpu(shdr->Command),
+ le64_to_cpu(shdr->MessageId));
+ return 0;
+ }
+
+ log_err = (log_err && (smb2err != STATUS_MORE_PROCESSING_REQUIRED) &&
+ (smb2err != STATUS_END_OF_FILE)) ||
+ (cifsFYI & CIFS_RC);
+
+ err_map = smb2_get_err_map(smb2err);
+ 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",
+ __le32_to_cpu(smb2err), rc);
+
+ trace_smb3_cmd_err(le32_to_cpu(shdr->Id.SyncId.TreeId),
+ le64_to_cpu(shdr->SessionId),
+ le16_to_cpu(shdr->Command),
+ le64_to_cpu(shdr->MessageId),
+ le32_to_cpu(smb2err), rc);
+ return rc;
+}
diff --git a/fs/smb/client/smb2proto.h b/fs/smb/client/smb2proto.h
index c988f6b37a1b..5241daaae543 100644
--- a/fs/smb/client/smb2proto.h
+++ b/fs/smb/client/smb2proto.h
@@ -21,7 +21,6 @@ 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);
diff --git a/fs/smb/common/Makefile b/fs/smb/common/Makefile
index 4f1dc5123e99..3bfd510a5204 100644
--- a/fs/smb/common/Makefile
+++ b/fs/smb/common/Makefile
@@ -4,4 +4,4 @@
#
obj-$(CONFIG_SMBFS) += smb_common.o
-smb_common-objs := common.o cifs_md4.o
+smb_common-objs := common.o cifs_md4.o smb2maperror.o
diff --git a/fs/smb/common/common.c b/fs/smb/common/common.c
index 4142e05039c0..eb3703b3cc22 100644
--- a/fs/smb/common/common.c
+++ b/fs/smb/common/common.c
@@ -13,6 +13,8 @@ static int __init smb_common_init(void)
{
int rc = 0;
+ smb2_init_maperror();
+
return rc;
}
diff --git a/fs/smb/common/common.h b/fs/smb/common/common.h
index 07ee24ecccb8..71933f8497b8 100644
--- a/fs/smb/common/common.h
+++ b/fs/smb/common/common.h
@@ -24,4 +24,17 @@ int cifs_md4_init(struct md4_ctx *mctx);
int cifs_md4_update(struct md4_ctx *mctx, const u8 *data, unsigned int len);
int cifs_md4_final(struct md4_ctx *mctx, u8 *out);
+/*
+ * Definitions for smb2 map error
+ */
+
+struct status_to_posix_error {
+ __le32 smb2_status;
+ int posix_error;
+ char *status_string;
+};
+
+struct status_to_posix_error *smb2_get_err_map(__le32 smb2_status);
+void smb2_init_maperror(void);
+
#endif /* __SMB_COMMON_H__ */
diff --git a/fs/smb/client/smb2maperror.c b/fs/smb/common/smb2maperror.c
similarity index 98%
rename from fs/smb/client/smb2maperror.c
rename to fs/smb/common/smb2maperror.c
index 95e4a41ecc5a..c4109fc6320f 100644
--- a/fs/smb/client/smb2maperror.c
+++ b/fs/smb/common/smb2maperror.c
@@ -7,21 +7,11 @@
* Author(s): Steve French (sfrench@us.ibm.com)
*
*/
-#include <linux/errno.h>
+#include <linux/kernel.h>
#include <linux/sort.h>
-#include "cifsglob.h"
-#include "cifs_debug.h"
-#include "smb2pdu.h"
-#include "smb2proto.h"
-#include "../common/smb2status.h"
-#include "smb2glob.h"
-#include "trace.h"
-
-struct status_to_posix_error {
- __le32 smb2_status;
- int posix_error;
- char *status_string;
-};
+#include <linux/bsearch.h>
+#include "common.h"
+#include "smb2status.h"
static struct status_to_posix_error smb2_error_map_table[] = {
{STATUS_WAIT_1, -EIO, "STATUS_WAIT_1"},
@@ -2433,7 +2423,7 @@ static int cmp_smb2_status(const void *_a, const void *_b)
return 0;
}
-static struct status_to_posix_error *
+struct status_to_posix_error *
smb2_get_err_map(__le32 smb2_status)
{
struct status_to_posix_error *err_map, key;
@@ -2446,49 +2436,7 @@ smb2_get_err_map(__le32 smb2_status)
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;
-
- if (smb2err == 0) {
- trace_smb3_cmd_done(le32_to_cpu(shdr->Id.SyncId.TreeId),
- le64_to_cpu(shdr->SessionId),
- le16_to_cpu(shdr->Command),
- le64_to_cpu(shdr->MessageId));
- return 0;
- }
-
- log_err = (log_err && (smb2err != STATUS_MORE_PROCESSING_REQUIRED) &&
- (smb2err != STATUS_END_OF_FILE)) ||
- (cifsFYI & CIFS_RC);
-
- err_map = smb2_get_err_map(smb2err);
- 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",
- __le32_to_cpu(smb2err), rc);
-
- trace_smb3_cmd_err(le32_to_cpu(shdr->Id.SyncId.TreeId),
- le64_to_cpu(shdr->SessionId),
- le16_to_cpu(shdr->Command),
- le64_to_cpu(shdr->MessageId),
- le32_to_cpu(smb2err), rc);
- return rc;
-}
+EXPORT_SYMBOL_GPL(smb2_get_err_map);
void smb2_init_maperror(void)
{
--
2.43.0
next prev parent reply other threads:[~2025-12-04 5:00 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-12-04 4:58 [PATCH 00/10] smb: improve search speed of SMB2 maperror chenxiaosong.chenxiaosong
2025-12-04 4:58 ` [PATCH 01/10] smb/client: reduce loop count in map_smb2_to_linux_error() by half chenxiaosong.chenxiaosong
2025-12-04 5:49 ` Steve French
2025-12-04 5:55 ` ChenXiaoSong
2025-12-04 4:58 ` [PATCH 02/10] smb/client: remove unused elements from smb2_error_map_table array chenxiaosong.chenxiaosong
2025-12-04 4:58 ` [PATCH 03/10] smb: add two elements to " chenxiaosong.chenxiaosong
2025-12-04 4:58 ` [PATCH 04/10] smb/client: sort " chenxiaosong.chenxiaosong
2025-12-04 4:58 ` [PATCH 05/10] smb/client: use bsearch() to find target status code chenxiaosong.chenxiaosong
2025-12-04 4:58 ` [PATCH 06/10] smb/client: introduce smb2_get_err_map() chenxiaosong.chenxiaosong
2025-12-04 4:58 ` [PATCH 07/10] smb/client: introduce smb2maperror KUnit tests chenxiaosong.chenxiaosong
2025-12-04 4:58 ` [PATCH 08/10] smb/server: rename include guard in smb_common.h chenxiaosong.chenxiaosong
2025-12-04 4:58 ` [PATCH 09/10] smb: create common/common.h and common/common.c chenxiaosong.chenxiaosong
2025-12-05 0:35 ` Namjae Jeon
2025-12-05 0:58 ` ChenXiaoSong
2025-12-05 1:36 ` Steve French
2025-12-05 1:44 ` ChenXiaoSong
2025-12-05 1:50 ` Steve French
2025-12-05 2:14 ` ChenXiaoSong
2025-12-05 3:02 ` ChenXiaoSong
2025-12-04 4:58 ` chenxiaosong.chenxiaosong [this message]
2025-12-04 20:39 ` [PATCH 10/10] smb: move client/smb2maperror.c to common/ kernel test robot
2025-12-04 21:12 ` kernel test robot
2025-12-05 2:35 ` kernel test robot
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20251204045818.2590727-11-chenxiaosong.chenxiaosong@linux.dev \
--to=chenxiaosong.chenxiaosong@linux.dev \
--cc=chenxiaosong@chenxiaosong.com \
--cc=chenxiaosong@kylinos.cn \
--cc=linkinjeon@kernel.org \
--cc=linkinjeon@samba.org \
--cc=linux-cifs@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=sfrench@samba.org \
--cc=smfrench@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox