Skip to content

Commit 58ac796

Browse files
haryvensmfrench
authored andcommitted
smb/client: autogenerate SMB1 DOS/SRV to POSIX error mapping
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>
1 parent cd4e653 commit 58ac796

5 files changed

Lines changed: 62 additions & 79 deletions

File tree

fs/smb/client/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
smb1_mapping_table.c
2+
smb1_err_dos_map.c
3+
smb1_err_srv_map.c
24
smb2_mapping_table.c

fs/smb/client/Makefile

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,18 @@ cifs-$(CONFIG_CIFS_COMPRESSION) += compress.o compress/lz77.o
4646

4747
ifneq ($(CONFIG_CIFS_ALLOW_INSECURE_LEGACY),)
4848
#
49-
# Build the SMB1 error mapping tables from nterr.h
49+
# Build the SMB1 error mapping tables from nterr.h and smberr.h
5050
#
51-
smb1-gen-y := smb1_mapping_table.c
51+
smb1-gen-y := smb1_mapping_table.c \
52+
smb1_err_dos_map.c \
53+
smb1_err_srv_map.c
5254

5355
$(obj)/smb1_mapping_table.c: $(src)/nterr.h $(src)/gen_smb1_mapping FORCE
5456
$(call if_changed,gen_smb1_mapping)
5557

58+
$(obj)/smb1_err_%.c: $(src)/smberr.h $(src)/gen_smb1_mapping FORCE
59+
$(call if_changed,gen_smb1_mapping)
60+
5661
$(obj)/smb1maperror.o: $(addprefix $(obj)/, $(smb1-gen-y))
5762

5863
quiet_cmd_gen_smb1_mapping = GEN $@

fs/smb/client/gen_smb1_mapping

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ my $output_name = (split m|/|, $out_file)[-1];
2222
my $script_name = (split m|/|, $0)[-1];
2323
my @list = ();
2424
my %seen = ();
25+
my $current_class = "";
2526

2627
# Parse annotated entries from the input file
2728
open(my $in, "<", $in_file) or die "Cannot open $in_file: $!";
@@ -52,6 +53,26 @@ if ($in_file =~ /nterr\.h$/) {
5253
die "Error: Invalid mapping comment format in $in_file: $_";
5354
}
5455
}
56+
} elsif ($in_file =~ /smberr\.h$/) {
57+
while (<$in>) {
58+
# Handle backslash line continuation
59+
$_ .= <$in> while s/\\\s*\n//;
60+
61+
# Detect current error class from header comments (ERRDOS or ERRSRV)
62+
if (/generated with the (\w+) error class/) {
63+
$current_class = $1;
64+
}
65+
66+
# Match #define ERR/Err_... <value> followed by // -POSIX_ERR or /* -POSIX_ERR */
67+
if (/^\s*#define\s+((?:ERR|Err)[A-Za-z0-9_]+)\s+([0-9a-fA-FxX]+)\s*(?:\/\/|\/\*)\s*(-[A-Z0-9_]+)/) {
68+
my ($name, $val_str, $error) = ($1, $2, $3);
69+
my $val = ($val_str =~ /^0x/i) ? hex($val_str) : $val_str;
70+
push @list, { val => $val, name => $name, error => $error, class => $current_class };
71+
} elsif ($current_class && /^\s*#define\s+(?:ERR|Err).*?(?:\/\/|\/\*)/) {
72+
# Error if macro has a comment (// or /*) but fails mapping format
73+
die "Error: Invalid mapping comment format in $in_file: $_";
74+
}
75+
}
5576
}
5677
close($in);
5778

@@ -87,5 +108,17 @@ if ($output_name eq "smb1_mapping_table.c") {
87108

88109
$full_names = "";
89110
}
111+
} elsif ($output_name eq "smb1_err_dos_map.c" || $output_name eq "smb1_err_srv_map.c") {
112+
# Generate SMB1 error -> POSIX error mapping file
113+
114+
# Filtered by exact output filename
115+
my $filter = ($output_name eq "smb1_err_dos_map.c") ? "ERRDOS" : "ERRSRV";
116+
foreach my $e (@list) {
117+
if (!$filter || $e->{class} eq $filter) {
118+
printf $out "\t{%s, %s},\n", $e->{name}, $e->{error};
119+
}
120+
}
121+
} else {
122+
die "Error: Unsupported output target: $output_name\n";
90123
}
91124
close($out);

fs/smb/client/smb1maperror.c

Lines changed: 10 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -21,85 +21,20 @@ struct smb_to_posix_error {
2121
};
2222

2323
static const struct smb_to_posix_error mapping_table_ERRDOS[] = {
24-
{ERRbadfunc, -EINVAL},
25-
{ERRbadfile, -ENOENT},
26-
{ERRbadpath, -ENOTDIR},
27-
{ERRnofids, -EMFILE},
28-
{ERRnoaccess, -EACCES},
29-
{ERRbadfid, -EBADF},
30-
{ERRbadmcb, -EIO},
31-
{ERRnomem, -EREMOTEIO},
32-
{ERRbadmem, -EFAULT},
33-
{ERRbadenv, -EFAULT},
34-
{ERRbadformat, -EINVAL},
35-
{ERRbadaccess, -EACCES},
36-
{ERRbaddata, -EIO},
37-
{ERRbaddrive, -ENXIO},
38-
{ERRremcd, -EACCES},
39-
{ERRdiffdevice, -EXDEV},
40-
{ERRnofiles, -ENOENT},
41-
{ERRwriteprot, -EROFS},
42-
{ERRbadshare, -EBUSY},
43-
{ERRlock, -EACCES},
44-
{ERRunsup, -EINVAL},
45-
{ERRnosuchshare, -ENXIO},
46-
{ERRfilexists, -EEXIST},
47-
{ERRinvparm, -EINVAL},
48-
{ERRdiskfull, -ENOSPC},
49-
{ERRinvname, -ENOENT},
50-
{ERRunknownlevel, -EOPNOTSUPP},
51-
{ERRdirnotempty, -ENOTEMPTY},
52-
{ERRnotlocked, -ENOLCK},
53-
{ERRcancelviolation, -ENOLCK},
54-
{ERRalreadyexists, -EEXIST},
55-
{ERRmoredata, -EOVERFLOW},
56-
{ERReasnotsupported, -EOPNOTSUPP},
57-
{ErrQuota, -EDQUOT},
58-
{ErrNotALink, -ENOLINK},
59-
{ERRnetlogonNotStarted, -ENOPROTOOPT},
60-
{ERRsymlink, -EOPNOTSUPP},
61-
{ErrTooManyLinks, -EMLINK},
24+
/*
25+
* Automatically generated by the `gen_smb1_mapping` script,
26+
* sorted by DOS error code (ascending).
27+
*/
28+
#include "smb1_err_dos_map.c"
6229
{0, 0}
6330
};
6431

6532
static const struct smb_to_posix_error mapping_table_ERRSRV[] = {
66-
{ERRerror, -EIO},
67-
{ERRbadpw, -EACCES}, /* was EPERM */
68-
{ERRbadtype, -EREMOTE},
69-
{ERRaccess, -EACCES},
70-
{ERRinvtid, -ENXIO},
71-
{ERRinvnetname, -ENXIO},
72-
{ERRinvdevice, -ENXIO},
73-
{ERRqfull, -ENOSPC},
74-
{ERRqtoobig, -ENOSPC},
75-
{ERRqeof, -EIO},
76-
{ERRinvpfid, -EBADF},
77-
{ERRsmbcmd, -EBADRQC},
78-
{ERRsrverror, -EIO},
79-
{ERRbadBID, -EIO},
80-
{ERRfilespecs, -EINVAL},
81-
{ERRbadLink, -EIO},
82-
{ERRbadpermits, -EINVAL},
83-
{ERRbadPID, -ESRCH},
84-
{ERRsetattrmode, -EINVAL},
85-
{ERRpaused, -EHOSTDOWN},
86-
{ERRmsgoff, -EHOSTDOWN},
87-
{ERRnoroom, -ENOSPC},
88-
{ERRrmuns, -EUSERS},
89-
{ERRtimeout, -ETIME},
90-
{ERRnoresource, -EREMOTEIO},
91-
{ERRtoomanyuids, -EUSERS},
92-
{ERRbaduid, -EACCES},
93-
{ERRusempx, -EIO},
94-
{ERRusestd, -EIO},
95-
{ERR_NOTIFY_ENUM_DIR, -ENOBUFS},
96-
{ERRnoSuchUser, -EACCES},
97-
{ERRaccountexpired, -EKEYEXPIRED},
98-
{ERRbadclient, -EACCES},
99-
{ERRbadLogonTime, -EACCES},
100-
{ERRpasswordExpired, -EKEYEXPIRED},
101-
102-
{ERRnosupport, -EINVAL},
33+
/*
34+
* Automatically generated by the `gen_smb1_mapping` script,
35+
* sorted by SRV error code (ascending).
36+
*/
37+
#include "smb1_err_srv_map.c"
10338
{0, 0}
10439
};
10540

fs/smb/client/smberr.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@
2424

2525
/*#define SUCCESS 0 The request was successful. */
2626

27-
/* The following error codes may be generated with the ERRDOS error class.*/
27+
/*
28+
* The following error codes may be generated with the ERRDOS error class.
29+
* The comment at the end of each definition indicates the POSIX error
30+
* code; it is used to generate the `mapping_table_ERRDOS` array.
31+
*/
2832

2933
/*
3034
* Invalid function. The server did not
@@ -162,7 +166,11 @@
162166
#define ERRsymlink 0xFFFD // -EOPNOTSUPP
163167
#define ErrTooManyLinks 0xFFFE // -EMLINK
164168

165-
/* Following error codes may be generated with the ERRSRV error class.*/
169+
/*
170+
* The following error codes may be generated with the ERRSRV error class.
171+
* The comment at the end of each definition indicates the POSIX error
172+
* code; it is used to generate the `mapping_table_ERRSRV` array.
173+
*/
166174

167175
/*
168176
* Non-specific error code. It is

0 commit comments

Comments
 (0)