]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
smb/client: autogenerate SMB1 DOS/SRV to POSIX error mapping
authorHuiwen He <hehuiwen@kylinos.cn>
Thu, 2 Apr 2026 14:18:36 +0000 (14:18 +0000)
committerSteve French <stfrench@microsoft.com>
Mon, 6 Apr 2026 00:58:40 +0000 (19:58 -0500)
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>
Signed-off-by: Steve French <stfrench@microsoft.com>
fs/smb/client/.gitignore
fs/smb/client/Makefile
fs/smb/client/gen_smb1_mapping
fs/smb/client/smb1maperror.c
fs/smb/client/smberr.h

index d5ea5ac6015dac2c450ec702b4b88813a8e0d50e..66e6e2ade0bd88d5eaadf881e5439f96f1fcdca2 100644 (file)
@@ -1,2 +1,4 @@
 smb1_mapping_table.c
+smb1_err_dos_map.c
+smb1_err_srv_map.c
 smb2_mapping_table.c
index 220a97c8a488d55c505371c13d4de1b3f8f83a2b..6e83b5204699c66b492ca474aa326d519354aa4a 100644 (file)
@@ -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     $@
index b2c373a73f3aa49a362049013f509648a6f1e315..c2b2939a83c6f8d5612fa3bc9bcabe685fd8228d 100644 (file)
@@ -22,6 +22,7 @@ my $output_name = (split m|/|, $out_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: $!";
@@ -52,6 +53,26 @@ if ($in_file =~ /nterr\.h$/) {
                        die "Error: Invalid mapping comment format in $in_file: $_";
                }
        }
+} elsif ($in_file =~ /smberr\.h$/) {
+       while (<$in>) {
+               # Handle backslash line continuation
+               $_ .= <$in> while s/\\\s*\n//;
+
+               # Detect current error class from header comments (ERRDOS or ERRSRV)
+               if (/generated with the (\w+) error class/) {
+                       $current_class = $1;
+               }
+
+               # Match #define ERR/Err_... <value> followed by // -POSIX_ERR or /* -POSIX_ERR */
+               if (/^\s*#define\s+((?:ERR|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 };
+               } elsif ($current_class && /^\s*#define\s+(?:ERR|Err).*?(?:\/\/|\/\*)/) {
+                       # Error if macro has a comment (// or /*) but fails mapping format
+                       die "Error: Invalid mapping comment format in $in_file: $_";
+               }
+       }
 }
 close($in);
 
@@ -87,5 +108,17 @@ if ($output_name eq "smb1_mapping_table.c") {
 
                $full_names = "";
        }
+} elsif ($output_name eq "smb1_err_dos_map.c" || $output_name eq "smb1_err_srv_map.c") {
+       # Generate SMB1 error -> POSIX error mapping file
+
+       # Filtered by exact output filename
+       my $filter = ($output_name eq "smb1_err_dos_map.c") ? "ERRDOS" : "ERRSRV";
+       foreach my $e (@list) {
+               if (!$filter || $e->{class} eq $filter) {
+                       printf $out "\t{%s, %s},\n", $e->{name}, $e->{error};
+               }
+       }
+} else {
+       die "Error: Unsupported output target: $output_name\n";
 }
 close($out);
index 419057f296a75a24ffd0eb2f0b30095f3b3c5483..294ac9646bff810ad236d4c142f2b0f7e698a320 100644 (file)
@@ -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}
 };
 
index d3633623473a593296d9ee2894ef0908118cde36..4ec2c5ffae253c6c4787bcee9a6eb5dc644bb6a3 100644 (file)
 
 /*#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
 #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