public inbox for linux-cifs@vger.kernel.org
 help / color / mirror / Atom feed
From: huiwen.he@linux.dev
To: smfrench@gmail.com, linkinjeon@kernel.org, pc@manguebit.org,
	ronniesahlberg@gmail.com, sprasad@microsoft.com, tom@talpey.com,
	bharathsm@microsoft.com, senozhatsky@chromium.org,
	dhowells@redhat.com, chenxiaosong@kylinos.cn,
	chenxiaosong@chenxiaosong.com, tangyouling@kylinos.cn
Cc: linux-cifs@vger.kernel.org, Huiwen He <hehuiwen@kylinos.cn>
Subject: [PATCH v2 09/12] smb/client: autogenerate SMB1 DOS/SRV to POSIX error mapping
Date: Wed,  1 Apr 2026 07:29:09 +0000	[thread overview]
Message-ID: <20260401072912.355072-10-huiwen.he@linux.dev> (raw)
In-Reply-To: <20260401072912.355072-1-huiwen.he@linux.dev>

From: Huiwen He <hehuiwen@kylinos.cn>

Extend the `gen_smb1_mapping` script to support generating sorted POSIX
error mapping tables for both ERRDOS and ERRSRV classes at compile time.

The script parses annotations from smberr.h to generate smb1_err_dos_map.c
and smb1_err_srv_map.c, which are included as the contents of the arrays
mapping_table_ERRDOS[] and mapping_table_ERRSRV[], respectively.

This ensures that the mapping logic remains synchronized with the source
headers and prepares for faster error lookups using binary search in the
future.

Signed-off-by: Huiwen He <hehuiwen@kylinos.cn>
Reviewed-by: ChenXiaoSong <chenxiaosong@kylinos.cn>
---
 fs/smb/client/.gitignore       |  2 +
 fs/smb/client/Makefile         |  9 +++-
 fs/smb/client/gen_smb1_mapping | 25 ++++++++++
 fs/smb/client/smb1maperror.c   | 85 ++++------------------------------
 fs/smb/client/smberr.h         | 12 ++++-
 5 files changed, 54 insertions(+), 79 deletions(-)

diff --git a/fs/smb/client/.gitignore b/fs/smb/client/.gitignore
index d5ea5ac6015d..66e6e2ade0bd 100644
--- a/fs/smb/client/.gitignore
+++ b/fs/smb/client/.gitignore
@@ -1,2 +1,4 @@
 smb1_mapping_table.c
+smb1_err_dos_map.c
+smb1_err_srv_map.c
 smb2_mapping_table.c
diff --git a/fs/smb/client/Makefile b/fs/smb/client/Makefile
index 220a97c8a488..6e83b5204699 100644
--- a/fs/smb/client/Makefile
+++ b/fs/smb/client/Makefile
@@ -46,13 +46,18 @@ cifs-$(CONFIG_CIFS_COMPRESSION) += compress.o compress/lz77.o
 
 ifneq ($(CONFIG_CIFS_ALLOW_INSECURE_LEGACY),)
 #
-# Build the SMB1 error mapping tables from nterr.h
+# Build the SMB1 error mapping tables from nterr.h and smberr.h
 #
-smb1-gen-y := smb1_mapping_table.c
+smb1-gen-y := smb1_mapping_table.c \
+	      smb1_err_dos_map.c \
+	      smb1_err_srv_map.c
 
 $(obj)/smb1_mapping_table.c: $(src)/nterr.h $(src)/gen_smb1_mapping FORCE
 	$(call if_changed,gen_smb1_mapping)
 
+$(obj)/smb1_err_%.c: $(src)/smberr.h $(src)/gen_smb1_mapping FORCE
+	$(call if_changed,gen_smb1_mapping)
+
 $(obj)/smb1maperror.o: $(addprefix $(obj)/, $(smb1-gen-y))
 
 quiet_cmd_gen_smb1_mapping = GEN     $@
diff --git a/fs/smb/client/gen_smb1_mapping b/fs/smb/client/gen_smb1_mapping
index 52ea4fdccbcd..51a77e745bfa 100644
--- a/fs/smb/client/gen_smb1_mapping
+++ b/fs/smb/client/gen_smb1_mapping
@@ -21,6 +21,7 @@ my $input_name  = (split m|/|, $in_file)[-1];
 my $script_name = (split m|/|, $0)[-1];
 my @list     = ();
 my %seen     = ();
+my $current_class = "";
 
 # Parse annotated entries from the input file
 open(my $in, "<", $in_file) or die "Cannot open $in_file: $!";
@@ -45,6 +46,20 @@ if ($in_file =~ /nterr\.h$/) {
 			push @list, { val => $val, name => $name, class => $class, code => $code };
 		}
 	}
+} elsif ($in_file =~ /smberr\.h$/) {
+	while (<$in>) {
+		# Detect current error class from header comments (ERRDOS or ERRSRV)
+		if (/generated with the (\w+) error class/) {
+			$current_class = $1;
+		}
+
+		# Match #define ERR... <value> // -POSIX_ERR
+		if (/^\s*#define\s+(ERR[A-Za-z0-9_]+)\s+([0-9a-fA-FxX]+)\s*\/\/\s*(-[A-Z0-9_]+)/) {
+			my ($name, $val_str, $error) = ($1, $2, $3);
+			my $val = ($val_str =~ /^0x/i) ? hex($val_str) : $val_str;
+			push @list, { val => $val, name => $name, error => $error, class => $current_class };
+		}
+	}
 }
 close($in);
 
@@ -80,5 +95,15 @@ if ($in_file =~ /nterr\.h$/) {
 
 		$full_names = "";
 	}
+} elsif ($in_file =~ /smberr\.h$/) {
+	# Generate SMB1 error -> POSIX error mapping file smb1_err_dos_map.c and smb1_err_srv_map.c
+
+	# Filtered by dos/srv
+	my $filter = ($out_file =~ /dos/) ? "ERRDOS" : ($out_file =~ /srv/ ? "ERRSRV" : "");
+	foreach my $e (@list) {
+		if (!$filter || $e->{class} eq $filter) {
+			printf $out "\t{%s, %s},\n", $e->{name}, $e->{error};
+		}
+	}
 }
 close($out);
diff --git a/fs/smb/client/smb1maperror.c b/fs/smb/client/smb1maperror.c
index 499cacfeba6d..bed579afc37b 100644
--- a/fs/smb/client/smb1maperror.c
+++ b/fs/smb/client/smb1maperror.c
@@ -21,85 +21,20 @@ struct smb_to_posix_error {
 };
 
 static const struct smb_to_posix_error mapping_table_ERRDOS[] = {
-	{ERRbadfunc, -EINVAL},
-	{ERRbadfile, -ENOENT},
-	{ERRbadpath, -ENOTDIR},
-	{ERRnofids, -EMFILE},
-	{ERRnoaccess, -EACCES},
-	{ERRbadfid, -EBADF},
-	{ERRbadmcb, -EIO},
-	{ERRnomem, -EREMOTEIO},
-	{ERRbadmem, -EFAULT},
-	{ERRbadenv, -EFAULT},
-	{ERRbadformat, -EINVAL},
-	{ERRbadaccess, -EACCES},
-	{ERRbaddata, -EIO},
-	{ERRbaddrive, -ENXIO},
-	{ERRremcd, -EACCES},
-	{ERRdiffdevice, -EXDEV},
-	{ERRnofiles, -ENOENT},
-	{ERRwriteprot, -EROFS},
-	{ERRbadshare, -EBUSY},
-	{ERRlock, -EACCES},
-	{ERRunsup, -EINVAL},
-	{ERRnosuchshare, -ENXIO},
-	{ERRfilexists, -EEXIST},
-	{ERRinvparm, -EINVAL},
-	{ERRdiskfull, -ENOSPC},
-	{ERRinvname, -ENOENT},
-	{ERRunknownlevel, -EOPNOTSUPP},
-	{ERRdirnotempty, -ENOTEMPTY},
-	{ERRnotlocked, -ENOLCK},
-	{ERRcancelviolation, -ENOLCK},
-	{ERRalreadyexists, -EEXIST},
-	{ERRmoredata, -EOVERFLOW},
-	{ERReasnotsupported, -EOPNOTSUPP},
-	{ErrQuota, -EDQUOT},
-	{ErrNotALink, -ENOLINK},
-	{ERRnetlogonNotStarted, -ENOPROTOOPT},
-	{ERRsymlink, -EOPNOTSUPP},
-	{ErrTooManyLinks, -EMLINK},
+/*
+ * Automatically generated by the `gen_smb1_mapping` script,
+ * sorted by DOS error code (ascending).
+ */
+#include "smb1_err_dos_map.c"
 	{0, 0}
 };
 
 static const struct smb_to_posix_error mapping_table_ERRSRV[] = {
-	{ERRerror, -EIO},
-	{ERRbadpw, -EACCES},  /* was EPERM */
-	{ERRbadtype, -EREMOTE},
-	{ERRaccess, -EACCES},
-	{ERRinvtid, -ENXIO},
-	{ERRinvnetname, -ENXIO},
-	{ERRinvdevice, -ENXIO},
-	{ERRqfull, -ENOSPC},
-	{ERRqtoobig, -ENOSPC},
-	{ERRqeof, -EIO},
-	{ERRinvpfid, -EBADF},
-	{ERRsmbcmd, -EBADRQC},
-	{ERRsrverror, -EIO},
-	{ERRbadBID, -EIO},
-	{ERRfilespecs, -EINVAL},
-	{ERRbadLink, -EIO},
-	{ERRbadpermits, -EINVAL},
-	{ERRbadPID, -ESRCH},
-	{ERRsetattrmode, -EINVAL},
-	{ERRpaused, -EHOSTDOWN},
-	{ERRmsgoff, -EHOSTDOWN},
-	{ERRnoroom, -ENOSPC},
-	{ERRrmuns, -EUSERS},
-	{ERRtimeout, -ETIME},
-	{ERRnoresource, -EREMOTEIO},
-	{ERRtoomanyuids, -EUSERS},
-	{ERRbaduid, -EACCES},
-	{ERRusempx, -EIO},
-	{ERRusestd, -EIO},
-	{ERR_NOTIFY_ENUM_DIR, -ENOBUFS},
-	{ERRnoSuchUser, -EACCES},
-	{ERRaccountexpired, -EKEYEXPIRED},
-	{ERRbadclient, -EACCES},
-	{ERRbadLogonTime, -EACCES},
-	{ERRpasswordExpired, -EKEYEXPIRED},
-
-	{ERRnosupport, -EINVAL},
+/*
+ * Automatically generated by the `gen_smb1_mapping` script,
+ * sorted by SRV error code (ascending).
+ */
+#include "smb1_err_srv_map.c"
 	{0, 0}
 };
 
diff --git a/fs/smb/client/smberr.h b/fs/smb/client/smberr.h
index a848b6ad987a..98074c64587d 100644
--- a/fs/smb/client/smberr.h
+++ b/fs/smb/client/smberr.h
@@ -24,7 +24,11 @@
 
 /*#define SUCCESS	0	The request was successful. */
 
-/* The following error codes may be generated with the ERRDOS error class.*/
+/*
+ * The following error codes may be generated with the ERRDOS error class.
+ * The comment at the end of each definition indicates the POSIX error
+ * code; it is used to generate the `mapping_table_ERRDOS` array.
+ */
 
 /*
  * Invalid function. The server did not
@@ -161,7 +165,11 @@
 #define ERRsymlink              0xFFFD	// -EOPNOTSUPP
 #define ErrTooManyLinks         0xFFFE	// -EMLINK
 
-/* Following error codes may be generated with the ERRSRV error class.*/
+/*
+ * The following error codes may be generated with the ERRSRV error class.
+ * The comment at the end of each definition indicates the POSIX error
+ * code; it is used to generate the `mapping_table_ERRSRV` array.
+ */
 
 /*
  * Non-specific error code. It is
-- 
2.52.0


  parent reply	other threads:[~2026-04-01  7:30 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-01  7:29 [PATCH v2 00/12] smb: improve search speed of SMB1 maperror huiwen.he
2026-04-01  7:29 ` [PATCH v2 01/12] smb/client: annotate nterr.h with DOS error codes huiwen.he
2026-04-01  7:29 ` [PATCH v2 02/12] smb/client: autogenerate SMB1 NT status to DOS error mapping huiwen.he
2026-04-01  7:29 ` [PATCH v2 03/12] smb/client: replace nt_errs with ntstatus_to_dos_map huiwen.he
2026-04-01  7:29 ` [PATCH v2 04/12] smb/client: refactor ntstatus_to_dos() to return mapping entry huiwen.he
2026-04-01  7:29 ` [PATCH v2 05/12] smb/client: use binary search for NT status to DOS mapping huiwen.he
2026-04-01  7:29 ` [PATCH v2 06/12] smb/client: check if ntstatus_to_dos_map is sorted huiwen.he
2026-04-01  7:29 ` [PATCH v2 07/12] smb/client: introduce KUnit test to check ntstatus_to_dos_map search huiwen.he
2026-04-01  7:29 ` [PATCH v2 08/12] smb/client: annotate smberr.h with POSIX error codes huiwen.he
2026-04-01  7:29 ` huiwen.he [this message]
2026-04-01  7:29 ` [PATCH v2 10/12] smb/client: use binary search for SMB1 DOS/SRV error mapping huiwen.he
2026-04-01  7:29 ` [PATCH v2 11/12] smb/client: check if SMB1 DOS/SRV error mapping arrays are sorted huiwen.he
2026-04-01  7:29 ` [PATCH v2 12/12] smb/client: introduce KUnit tests to check DOS/SRV err mapping search huiwen.he
2026-04-02  8:38 ` [PATCH v2 00/12] smb: improve search speed of SMB1 maperror ChenXiaoSong

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=20260401072912.355072-10-huiwen.he@linux.dev \
    --to=huiwen.he@linux.dev \
    --cc=bharathsm@microsoft.com \
    --cc=chenxiaosong@chenxiaosong.com \
    --cc=chenxiaosong@kylinos.cn \
    --cc=dhowells@redhat.com \
    --cc=hehuiwen@kylinos.cn \
    --cc=linkinjeon@kernel.org \
    --cc=linux-cifs@vger.kernel.org \
    --cc=pc@manguebit.org \
    --cc=ronniesahlberg@gmail.com \
    --cc=senozhatsky@chromium.org \
    --cc=smfrench@gmail.com \
    --cc=sprasad@microsoft.com \
    --cc=tangyouling@kylinos.cn \
    --cc=tom@talpey.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