Skip to content

Commit 3c6c23e

Browse files
haryvensmfrench
authored andcommitted
smb/client: use binary search for NT status to DOS mapping
The ntstatus_to_dos_map[] table is sorted now. Replace the linear search with binary search to improve lookup performance. Also remove the sentinel entry as it is 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 772d592 commit 3c6c23e

1 file changed

Lines changed: 19 additions & 13 deletions

File tree

fs/smb/client/smb1maperror.c

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -106,29 +106,35 @@ static const struct smb_to_posix_error mapping_table_ERRSRV[] = {
106106
/*****************************************************************************
107107
*convert a NT status code to a dos class/code
108108
*****************************************************************************/
109+
110+
static __always_inline int ntstatus_to_dos_cmp(const void *_key, const void *_pivot)
111+
{
112+
__u32 key = *(__u32 *)_key;
113+
const struct ntstatus_to_dos_err *pivot = _pivot;
114+
115+
if (key < pivot->ntstatus)
116+
return -1;
117+
if (key > pivot->ntstatus)
118+
return 1;
119+
return 0;
120+
}
121+
109122
/* NT status -> dos error map */
110123
static const struct ntstatus_to_dos_err ntstatus_to_dos_map[] = {
111124
/*
112125
* Automatically generated by the `gen_smb1_mapping` script,
113126
* sorted by NT status code (ascending).
114127
*/
115128
#include "smb1_mapping_table.c"
116-
{0, 0, 0, NULL}
117129
};
118130

119131
static const struct ntstatus_to_dos_err *
120-
ntstatus_to_dos(__u32 ntstatus)
132+
search_ntstatus_to_dos_map(__u32 ntstatus)
121133
{
122-
int i;
123-
124-
/* Check nt_errstr to allow mapping of NT_STATUS_OK (0) */
125-
for (i = 0; ntstatus_to_dos_map[i].nt_errstr; i++) {
126-
if (ntstatus == ntstatus_to_dos_map[i].ntstatus) {
127-
return &ntstatus_to_dos_map[i];
128-
}
129-
}
130-
131-
return NULL;
134+
return __inline_bsearch(&ntstatus, ntstatus_to_dos_map,
135+
ARRAY_SIZE(ntstatus_to_dos_map),
136+
sizeof(struct ntstatus_to_dos_err),
137+
ntstatus_to_dos_cmp);
132138
}
133139

134140
int
@@ -150,7 +156,7 @@ map_smb_to_linux_error(char *buf, bool logErr)
150156
/* translate the newer STATUS codes to old style SMB errors
151157
* and then to POSIX errors */
152158
__u32 err = le32_to_cpu(smb->Status.CifsError);
153-
const struct ntstatus_to_dos_err *map = ntstatus_to_dos(err);
159+
const struct ntstatus_to_dos_err *map = search_ntstatus_to_dos_map(err);
154160

155161
if (map) {
156162
if ((logErr && err != NT_STATUS_MORE_PROCESSING_REQUIRED) ||

0 commit comments

Comments
 (0)