Skip to content

Commit 95e6b73

Browse files
haryvensmfrench
authored andcommitted
smb/client: use binary search for SMB1 DOS/SRV error mapping
Currently, map_smb_to_linux_error() uses linear searches for both mapping_table_ERRDOS[] and mapping_table_ERRSRV[]. Refactor this by introducing search_mapping_table_ERRDOS() and search_mapping_table_ERRSRV() that implements binary search(as the tables are sorted).This improves lookup performance and reduces code duplication. Also remove the sentinel entries from the mapping tables as they are no longer needed with ARRAY_SIZE(). Signed-off-by: Huiwen He <hehuiwen@kylinos.cn> Reviewed-by: ChenXiaoSong <chenxiaosong@kylinos.cn> Signed-off-by: Steve French <stfrench@microsoft.com>
1 parent 58ac796 commit 95e6b73

1 file changed

Lines changed: 37 additions & 30 deletions

File tree

fs/smb/client/smb1maperror.c

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* Copyright (C) Luke Kenneth Casson Leighton 1997-2001.
1010
*/
1111

12+
#include <linux/bsearch.h>
1213
#include "cifsproto.h"
1314
#include "smb1proto.h"
1415
#include "smberr.h"
@@ -20,13 +21,24 @@ struct smb_to_posix_error {
2021
int posix_code;
2122
};
2223

24+
static __always_inline int smb1_posix_error_cmp(const void *_key, const void *_pivot)
25+
{
26+
__u16 key = *(__u16 *)_key;
27+
const struct smb_to_posix_error *pivot = _pivot;
28+
29+
if (key < pivot->smb_err)
30+
return -1;
31+
if (key > pivot->smb_err)
32+
return 1;
33+
return 0;
34+
}
35+
2336
static const struct smb_to_posix_error mapping_table_ERRDOS[] = {
2437
/*
2538
* Automatically generated by the `gen_smb1_mapping` script,
2639
* sorted by DOS error code (ascending).
2740
*/
2841
#include "smb1_err_dos_map.c"
29-
{0, 0}
3042
};
3143

3244
static const struct smb_to_posix_error mapping_table_ERRSRV[] = {
@@ -35,7 +47,6 @@ static const struct smb_to_posix_error mapping_table_ERRSRV[] = {
3547
* sorted by SRV error code (ascending).
3648
*/
3749
#include "smb1_err_srv_map.c"
38-
{0, 0}
3950
};
4051

4152
/*****************************************************************************
@@ -72,14 +83,32 @@ search_ntstatus_to_dos_map(__u32 ntstatus)
7283
ntstatus_to_dos_cmp);
7384
}
7485

86+
static const struct smb_to_posix_error *
87+
search_mapping_table_ERRDOS(__u16 smb_err)
88+
{
89+
return __inline_bsearch(&smb_err, mapping_table_ERRDOS,
90+
ARRAY_SIZE(mapping_table_ERRDOS),
91+
sizeof(struct smb_to_posix_error),
92+
smb1_posix_error_cmp);
93+
}
94+
95+
static const struct smb_to_posix_error *
96+
search_mapping_table_ERRSRV(__u16 smb_err)
97+
{
98+
return __inline_bsearch(&smb_err, mapping_table_ERRSRV,
99+
ARRAY_SIZE(mapping_table_ERRSRV),
100+
sizeof(struct smb_to_posix_error),
101+
smb1_posix_error_cmp);
102+
}
103+
75104
int
76105
map_smb_to_linux_error(char *buf, bool logErr)
77106
{
78107
struct smb_hdr *smb = (struct smb_hdr *)buf;
79-
unsigned int i;
80108
int rc = -EIO; /* if transport error smb error may not be set */
81109
__u8 smberrclass;
82110
__u16 smberrcode;
111+
const struct smb_to_posix_error *err_map = NULL;
83112

84113
/* BB if NT Status codes - map NT BB */
85114

@@ -112,38 +141,16 @@ map_smb_to_linux_error(char *buf, bool logErr)
112141

113142
/* old style errors */
114143

115-
/* DOS class smb error codes - map DOS */
116144
if (smberrclass == ERRDOS) {
145+
/* DOS class smb error codes - map DOS */
117146
/* 1 byte field no need to byte reverse */
118-
for (i = 0;
119-
i <
120-
sizeof(mapping_table_ERRDOS) /
121-
sizeof(struct smb_to_posix_error); i++) {
122-
if (mapping_table_ERRDOS[i].smb_err == 0)
123-
break;
124-
else if (mapping_table_ERRDOS[i].smb_err ==
125-
smberrcode) {
126-
rc = mapping_table_ERRDOS[i].posix_code;
127-
break;
128-
}
129-
/* else try next error mapping one to see if match */
130-
}
147+
err_map = search_mapping_table_ERRDOS(smberrcode);
131148
} else if (smberrclass == ERRSRV) {
132149
/* server class of error codes */
133-
for (i = 0;
134-
i <
135-
sizeof(mapping_table_ERRSRV) /
136-
sizeof(struct smb_to_posix_error); i++) {
137-
if (mapping_table_ERRSRV[i].smb_err == 0)
138-
break;
139-
else if (mapping_table_ERRSRV[i].smb_err ==
140-
smberrcode) {
141-
rc = mapping_table_ERRSRV[i].posix_code;
142-
break;
143-
}
144-
/* else try next error mapping to see if match */
145-
}
150+
err_map = search_mapping_table_ERRSRV(smberrcode);
146151
}
152+
if (err_map)
153+
rc = err_map->posix_code;
147154
/* else ERRHRD class errors or junk - return EIO */
148155

149156
/* special cases for NT status codes which cannot be translated to DOS codes */

0 commit comments

Comments
 (0)