smb: improve search speed of SMB2 maperror
Subsystems (2)
v3 cover smb: improve search speed of SMB2 maperror
From: ChenXiaoSong <[email protected]>
Before applying this patchset, when searching for the last element of smb2_error_map_table array and calling smb2_print_status(), 3486 comparisons are needed.
After applying this patchset, only 10 comparisons are required.
v1: https://lore.kernel.org/linux-cifs/[email protected]/
The three patches from v1 have already been applied to the for-next branch of cifs-2.6.git. Please replace the following patches:
- [v1 01/10] smb/client: reduce loop count in map_smb2_to_linux_error() by half: https://git.samba.org/sfrench/?p=sfrench/cifs-2.6.git;a=commitdiff;h=26866d690bd180e1860548c43e70fdefe50638ff
- Replace it with this version(v3) patch #0001: update commit message: array has 1743 elements
- [v1 02/10] smb/client: remove unused elements from smb2_error_map_table array: https://git.samba.org/sfrench/?p=sfrench/cifs-2.6.git;a=commitdiff;h=905d8999d67dcbe4ce12ef87996e4440e068196d
- It is the same as patch #0002 in this version(v3).
- [v1 03/10] smb: add two elements to smb2_error_map_table array: https://git.samba.org/sfrench/?p=sfrench/cifs-2.6.git;a=commitdiff;h=ba521f56912f6ff5121e54c17c855298f947c9ea
- Replace it with this version(v3) patch #0003 #0004.
v2: https://lore.kernel.org/all/[email protected]/v2->v3:
- Patch #0007: move struct status_to_posix_error to smb2glob.h,
change smb2_get_err_map to be a global function,
delete unused forward declaration struct statfs in smb2proto.h.
- Patch #0008: create new smb2maperror_test.c, refer to the implementation in fs/ext4/mballoc-test.c.
ChenXiaoSong (9):
smb/client: reduce loop count in map_smb2_to_linux_error() by half smb/client: remove unused elements from smb2_error_map_table array
smb: rename to STATUS_SMB_NO_PREAUTH_INTEGRITY_HASH_OVERLAP smb/client: add two elements to smb2_error_map_table array smb/client: sort smb2_error_map_table array
smb/client: use bsearch() to find target status code smb/client: introduce smb2_get_err_map()
smb/client: introduce smb2maperror KUnit tests smb/server: rename include guard in smb_common.h
fs/smb/Kconfig | 17 ++++++ fs/smb/client/cifsfs.c | 2 + fs/smb/client/smb2glob.h | 6 ++ fs/smb/client/smb2maperror.c | 91 ++++++++++++++++++------------- fs/smb/client/smb2maperror_test.c | 86 +++++++++++++++++++++++++++++ fs/smb/client/smb2proto.h | 4 +- fs/smb/common/smb2status.h | 5 +- fs/smb/server/smb2pdu.c | 2 +- fs/smb/server/smb_common.h | 6 +- 9 files changed, 174 insertions(+), 45 deletions(-) create mode 100644 fs/smb/client/smb2maperror_test.c
Signature · 1 line
2.43.0
PATCH v3 1/9 smb/client: reduce loop count in map_smb2_to_linux_error() by half
From: ChenXiaoSong <[email protected]>
The smb2_error_map_table array currently has 1743 elements. When searching for the last element and calling smb2_print_status(), 3486 comparisons are needed.
The loop in smb2_print_status() is unnecessary, smb2_print_status() can be removed, and only iterate over the array once, printing the message when the target status code is found.
--- fs/smb/client/smb2maperror.c | 30 ++++++------------------------ 1 file changed, 6 insertions(+), 24 deletions(-)
fs/smb/client/smb2maperror.c +6 -24
@@ -2418,24 +2418,6 @@ static const struct status_to_posix_error smb2_error_map_table[] = { {0, 0, NULL} }; -/***************************************************************************** -Print an error message from the status code -*****************************************************************************/ -static void -smb2_print_status (__le32 status) -{ -int idx =0 ; - -while (smb2_error_map_table[idx].status_string !=NULL ) { -if ((smb2_error_map_table[idx].smb2_status) == status) { -pr_notice ("Status code returned 0x%08x %s\n" , status, - smb2_error_map_table[idx].status_string); - } - idx++; - } -return ; -} -int map_smb2_to_linux_error (char *buf,bool log_err) {
@@ -2452,16 +2434,16 @@ map_smb2_to_linux_error(char *buf, bool log_err)return 0 ; } -/* mask facility */ -if (log_err && (smb2err !=STATUS_MORE_PROCESSING_REQUIRED ) && - (smb2err !=STATUS_END_OF_FILE )) -smb2_print_status (smb2err); -else if (cifsFYI &CIFS_RC ) -smb2_print_status (smb2err); + log_err = (log_err && (smb2err !=STATUS_MORE_PROCESSING_REQUIRED ) && + (smb2err !=STATUS_END_OF_FILE )) || + (cifsFYI &CIFS_RC );for (i =0 ; i <sizeof (smb2_error_map_table) /sizeof (struct status_to_posix_error ); i++) {if (smb2_error_map_table[i].smb2_status == smb2err) { +if (log_err) +pr_notice ("Status code returned 0x%08x %s\n" , smb2err, + smb2_error_map_table[i].status_string); rc = smb2_error_map_table[i].posix_error;break ; }
Signature · 1 line
2.43.0
PATCH v3 2/9 smb/client: remove unused elements from smb2_error_map_table array
In reply to [email protected]From: ChenXiaoSong <[email protected]>
STATUS_SUCCESS and STATUS_WAIT_0 are both zero, and since zero indicates success, they are not needed.
Since smb2_print_status() has been removed, the last element in the array is no longer needed.
--- fs/smb/client/smb2maperror.c | 3 --- 1 file changed, 3 deletions(-)
fs/smb/client/smb2maperror.c +0 -3
@@ -23,8 +23,6 @@ struct status_to_posix_error { };static const struct status_to_posix_error smb2_error_map_table[] = { - {STATUS_SUCCESS ,0 ,"STATUS_SUCCESS" }, - {STATUS_WAIT_0 ,0 ,"STATUS_WAIT_0" }, {STATUS_WAIT_1 , -EIO ,"STATUS_WAIT_1" }, {STATUS_WAIT_2 , -EIO ,"STATUS_WAIT_2" }, {STATUS_WAIT_3 , -EIO ,"STATUS_WAIT_3" },
@@ -2415,7 +2413,6 @@ static const struct status_to_posix_error smb2_error_map_table[] = { {STATUS_IPSEC_INTEGRITY_CHECK_FAILED , -EIO , "STATUS_IPSEC_INTEGRITY_CHECK_FAILED"}, {STATUS_IPSEC_CLEAR_TEXT_DROP, -EIO, "STATUS_IPSEC_CLEAR_TEXT_DROP"}, - {0, 0, NULL} };int
Signature · 1 line
2.43.0
PATCH v3 3/9 smb: rename to STATUS_SMB_NO_PREAUTH_INTEGRITY_HASH_OVERLAP
In reply to [email protected]From: ChenXiaoSong <[email protected]>
See MS-SMB2 3.3.5.4. To keep the name consistent with the documentation.
Additionally, move STATUS_INVALID_LOCK_RANGE to correct position in order.
--- fs/smb/common/smb2status.h | 5 +++-- fs/smb/server/smb2pdu.c | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-)
fs/smb/common/smb2status.h +3 -2
@@ -631,6 +631,7 @@ struct ntstatus {#define STATUS_DOMAIN_TRUST_INCONSISTENT cpu_to_le32(0xC000019B)#define STATUS_FS_DRIVER_REQUIRED cpu_to_le32(0xC000019C)#define STATUS_IMAGE_ALREADY_LOADED_AS_DLL cpu_to_le32(0xC000019D) +#define STATUS_INVALID_LOCK_RANGE cpu_to_le32(0xC00001A1)#define STATUS_NETWORK_OPEN_RESTRICTION cpu_to_le32(0xC0000201)#define STATUS_NO_USER_SESSION_KEY cpu_to_le32(0xC0000202)#define STATUS_USER_SESSION_DELETED cpu_to_le32(0xC0000203)
@@ -1773,5 +1774,5 @@ struct ntstatus {#define STATUS_IPSEC_INVALID_PACKET cpu_to_le32(0xC0360005)#define STATUS_IPSEC_INTEGRITY_CHECK_FAILED cpu_to_le32(0xC0360006)#define STATUS_IPSEC_CLEAR_TEXT_DROP cpu_to_le32(0xC0360007) -#define STATUS_NO_PREAUTH_INTEGRITY_HASH_OVERLAP cpu_to_le32(0xC05D0000) -#define STATUS_INVALID_LOCK_RANGE cpu_to_le32(0xC00001a1) +/* See MS-SMB2 3.3.5.4 */ +#define STATUS_SMB_NO_PREAUTH_INTEGRITY_HASH_OVERLAP cpu_to_le32(0xC05D0000)
fs/smb/server/smb2pdu.c +1 -1
@@ -896,7 +896,7 @@ static __le32 decode_preauth_ctxt(struct ksmbd_conn *conn,return STATUS_INVALID_PARAMETER ;if (pneg_ctxt->HashAlgorithms !=SMB2_PREAUTH_INTEGRITY_SHA512 ) -return STATUS_NO_PREAUTH_INTEGRITY_HASH_OVERLAP ; +return STATUS_SMB_NO_PREAUTH_INTEGRITY_HASH_OVERLAP ; conn->preauth_info->Preauth_HashId =SMB2_PREAUTH_INTEGRITY_SHA512 ;return STATUS_SUCCESS ;
Signature · 1 line
2.43.0
PATCH v3 4/9 smb/client: add two elements to smb2_error_map_table array
In reply to [email protected]From: ChenXiaoSong <[email protected]>
Both status codes are mapped to -EIO.
Now all status codes from common/smb2status.h are included in the smb2_error_map_table array(except for the first two zero definitions).
--- fs/smb/client/smb2maperror.c | 3 +++ 1 file changed, 3 insertions(+)
fs/smb/client/smb2maperror.c +3 -0
@@ -734,6 +734,7 @@ static const struct status_to_posix_error smb2_error_map_table[] = { {STATUS_FS_DRIVER_REQUIRED, -EOPNOTSUPP, "STATUS_FS_DRIVER_REQUIRED"}, {STATUS_IMAGE_ALREADY_LOADED_AS_DLL, -EIO, "STATUS_IMAGE_ALREADY_LOADED_AS_DLL"}, + {STATUS_INVALID_LOCK_RANGE, -EIO, "STATUS_INVALID_LOCK_RANGE"}, {STATUS_NETWORK_OPEN_RESTRICTION, -EIO, "STATUS_NETWORK_OPEN_RESTRICTION"}, {STATUS_NO_USER_SESSION_KEY, -EIO, "STATUS_NO_USER_SESSION_KEY"},
@@ -2413,6 +2414,8 @@ static const struct status_to_posix_error smb2_error_map_table[] = { {STATUS_IPSEC_INTEGRITY_CHECK_FAILED , -EIO , "STATUS_IPSEC_INTEGRITY_CHECK_FAILED"}, {STATUS_IPSEC_CLEAR_TEXT_DROP, -EIO, "STATUS_IPSEC_CLEAR_TEXT_DROP"}, + {STATUS_SMB_NO_PREAUTH_INTEGRITY_HASH_OVERLAP, -EIO, + "STATUS_SMB_NO_PREAUTH_INTEGRITY_HASH_OVERLAP"}, };int
Signature · 1 line
2.43.0
PATCH v3 5/9 smb/client: sort smb2_error_map_table array
In reply to [email protected]From: ChenXiaoSong <[email protected]>
Sort the array in ascending order, and then we can use binary search algorithm to quickly find the target status code.
Since the smb2_init_maperror() is called only once when the module is loaded, the array is sorted just once.
--- fs/smb/client/cifsfs.c | 2 ++ fs/smb/client/smb2maperror.c | 25 ++++++++++++++++++++++++- fs/smb/client/smb2proto.h | 1 + 3 files changed, 27 insertions(+), 1 deletion(-)
fs/smb/client/cifsfs.c +2 -0
@@ -49,6 +49,7 @@#endif #include "fs_context.h" #include "cached_dir.h" +#include "smb2proto.h" /* * DOS dates from 1980/1/1 through 2107/12/31
@@ -1908,6 +1909,7 @@ static int __initinit_cifs (void) {int rc =0 ; +smb2_init_maperror ();cifs_proc_init ();INIT_LIST_HEAD (&cifs_tcp_ses_list);/*
fs/smb/client/smb2maperror.c +24 -1
@@ -8,6 +8,7 @@* */ #include <linux/errno.h> +#include <linux/sort.h> #include "cifsglob.h" #include "cifs_debug.h" #include "smb2pdu.h"
@@ -22,7 +23,7 @@ struct status_to_posix_error {char *status_string; }; -static const struct status_to_posix_error smb2_error_map_table[] = { +static struct status_to_posix_error smb2_error_map_table[] = { {STATUS_WAIT_1 , -EIO ,"STATUS_WAIT_1" }, {STATUS_WAIT_2 , -EIO ,"STATUS_WAIT_2" }, {STATUS_WAIT_3 , -EIO ,"STATUS_WAIT_3" },
@@ -2418,6 +2419,20 @@ static const struct status_to_posix_error smb2_error_map_table[] = { "STATUS_SMB_NO_PREAUTH_INTEGRITY_HASH_OVERLAP"}, }; +static unsigned int err_map_num =sizeof (smb2_error_map_table) / +sizeof (struct status_to_posix_error ); + +static int cmp_smb2_status (const void *_a,const void *_b) +{ +const struct status_to_posix_error *a = _a, *b = _b; + +if (a->smb2_status < b->smb2_status) +return -1 ; +if (a->smb2_status > b->smb2_status) +return 1 ; +return 0 ; +} +int map_smb2_to_linux_error (char *buf,bool log_err) {
@@ -2461,3 +2476,11 @@ map_smb2_to_linux_error(char *buf, bool log_err) le32_to_cpu(smb2err), rc);return rc; } + +void smb2_init_maperror (void ) +{ +/* Sort in ascending order */ +sort (smb2_error_map_table, err_map_num, +sizeof (struct status_to_posix_error ), + cmp_smb2_status,NULL ); +}
fs/smb/client/smb2proto.h +1 -0
@@ -21,6 +21,7 @@ struct smb_rqst;***************************************************************** */ extern int map_smb2_to_linux_error (char *buf,bool log_err); +extern void smb2_init_maperror (void );extern int smb2_check_message (char *buf,unsigned int length,struct TCP_Server_Info *server);extern unsigned int smb2_calc_size (void *buf);
Signature · 1 line
2.43.0
PATCH v3 6/9 smb/client: use bsearch() to find target status code
In reply to [email protected]From: ChenXiaoSong <[email protected]>
The smb2_error_map_table array currently has 1742 elements. When searching for the last element, the original loop-based search method requires 1742 comparisons, while binary search algorithm requires only 10 comparisons.
--- fs/smb/client/smb2maperror.c | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-)
fs/smb/client/smb2maperror.c +15 -11
@@ -2437,9 +2437,9 @@ int map_smb2_to_linux_error(char *buf, bool log_err) {struct smb2_hdr *shdr = (struct smb2_hdr *)buf; -unsigned int i;int rc = -EIO ;__le32 smb2err = shdr->Status; +struct status_to_posix_error *err_map, key;if (smb2err ==0 ) { trace_smb3_cmd_done(le32_to_cpu(shdr->Id.SyncId.TreeId),
@@ -2453,17 +2453,21 @@ map_smb2_to_linux_error(char *buf, bool log_err) (smb2err != STATUS_END_OF_FILE)) || (cifsFYI &CIFS_RC ); -for (i =0 ; i <sizeof (smb2_error_map_table) / -sizeof (struct status_to_posix_error ); i++) { -if (smb2_error_map_table[i].smb2_status == smb2err) { -if (log_err) -pr_notice ("Status code returned 0x%08x %s\n" , smb2err, - smb2_error_map_table[i].status_string); - rc = smb2_error_map_table[i].posix_error; -break ; - } - } + key = (struct status_to_posix_error ) { + .smb2_status = smb2err, + }; + err_map =bsearch (&key, smb2_error_map_table, err_map_num, +sizeof (struct status_to_posix_error ), + cmp_smb2_status); +if (!err_map) + goto out; + + rc = err_map->posix_error; +if (log_err) +pr_notice ("Status code returned 0x%08x %s\n" , smb2err, + err_map->status_string); +out:/* on error mapping not found - return EIO */ cifs_dbg(FYI, "Mapping SMB2 status code 0x%08x to POSIX err %d\n",
Signature · 1 line
2.43.0
PATCH v3 7/9 smb/client: introduce smb2_get_err_map()
In reply to [email protected]From: ChenXiaoSong <[email protected]>
We may need to get struct status_to_posix_error information in other files in the future. So we move struct status_to_posix_error to smb2glob.h, and introduce new global function smb2_get_err_map().
Additionally, delete unused forward declaration struct statfs in smb2proto.h.
--- fs/smb/client/smb2glob.h | 6 ++++++ fs/smb/client/smb2maperror.c | 28 +++++++++++++++------------- fs/smb/client/smb2proto.h | 3 ++- 3 files changed, 23 insertions(+), 14 deletions(-)
fs/smb/client/smb2glob.h +6 -0
@@ -13,6 +13,12 @@#ifndef _SMB2_GLOB_H#define _SMB2_GLOB_H +struct status_to_posix_error { +__le32 smb2_status; +int posix_error; +char *status_string; +}; +/* ***************************************************************** * Constants go here
fs/smb/client/smb2maperror.c +15 -13
@@ -17,12 +17,6 @@#include "smb2glob.h" #include "trace.h" -struct status_to_posix_error { -__le32 smb2_status; -int posix_error; -char *status_string; -}; -static struct status_to_posix_error smb2_error_map_table[] = { {STATUS_WAIT_1 , -EIO ,"STATUS_WAIT_1" }, {STATUS_WAIT_2 , -EIO ,"STATUS_WAIT_2" },
@@ -2433,13 +2427,26 @@ static int cmp_smb2_status(const void *_a, const void *_b)return 0 ; } +struct status_to_posix_error *smb2_get_err_map (__le32 smb2_status) +{ +struct status_to_posix_error *err_map, key; + + key = (struct status_to_posix_error ) { + .smb2_status = smb2_status, + }; + err_map =bsearch (&key, smb2_error_map_table, err_map_num, +sizeof (struct status_to_posix_error ), + cmp_smb2_status); +return err_map; +} +int map_smb2_to_linux_error (char *buf,bool log_err) {struct smb2_hdr *shdr = (struct smb2_hdr *)buf;int rc = -EIO ;__le32 smb2err = shdr->Status; -struct status_to_posix_error *err_map, key; +struct status_to_posix_error *err_map;if (smb2err ==0 ) { trace_smb3_cmd_done(le32_to_cpu(shdr->Id.SyncId.TreeId),
@@ -2453,12 +2460,7 @@ map_smb2_to_linux_error(char *buf, bool log_err) (smb2err != STATUS_END_OF_FILE)) || (cifsFYI &CIFS_RC ); - key = (struct status_to_posix_error ) { - .smb2_status = smb2err, - }; - err_map =bsearch (&key, smb2_error_map_table, err_map_num, -sizeof (struct status_to_posix_error ), - cmp_smb2_status); + err_map =smb2_get_err_map (smb2err);if (!err_map) goto out;
fs/smb/client/smb2proto.h +2 -1
@@ -12,8 +12,8 @@#include <linux/nls.h> #include <linux/key-type.h> -struct statfs ;struct smb_rqst ; +struct status_to_posix_error ;/* *****************************************************************
@@ -21,6 +21,7 @@ struct smb_rqst;***************************************************************** */ extern int map_smb2_to_linux_error (char *buf,bool log_err); +extern struct status_to_posix_error *smb2_get_err_map (__le32 smb2_status);extern void smb2_init_maperror (void );extern int smb2_check_message (char *buf,unsigned int length,struct TCP_Server_Info *server);
Signature · 1 line
2.43.0
PATCH v3 8/9 smb/client: introduce smb2maperror KUnit tests
In reply to [email protected]From: ChenXiaoSong <[email protected]>
The KUnit tests are executed when cifs.ko is loaded.
Just like fs/ext4/mballoc.c includes fs/ext4/mballoc-test.c. smb2maperror.c also includes smb2maperror_test.c, allowing KUnit tests to access any functions and variables in smb2maperror.c.
The maperror_test_check_sort() checks whether the array is properly sorted.
The maperror_test_get_err_map() checks whether the expected element can be correctly searched for in the smb2_error_map_table array.
--- fs/smb/Kconfig | 17 ++++++ fs/smb/client/smb2maperror.c | 4 ++ fs/smb/client/smb2maperror_test.c | 86 +++++++++++++++++++++++++++++++ 3 files changed, 107 insertions(+) create mode 100644 fs/smb/client/smb2maperror_test.c
fs/smb/Kconfig +17 -0
@@ -9,3 +9,20 @@ config SMBFS tristate default y if CIFS=y || SMB_SERVER=y default m if CIFS=m || SMB_SERVER=m + +config SMB_KUNIT_TESTS + tristate "KUnit tests for SMB" if !KUNIT_ALL_TESTS + depends on SMBFS && KUNIT + default KUNIT_ALL_TESTS + help + This builds the SMB KUnit tests. + + KUnit tests run during boot and output the results to the debug log + in TAP format (https://testanything.org/). Only useful for kernel devs + running KUnit test harness and are not for inclusion into a production + build. + + For more information on KUnit and unit tests in general please refer + to the KUnit documentation in Documentation/dev-tools/kunit/. + + If unsure, say N.
fs/smb/client/smb2maperror.c +4 -0
@@ -2490,3 +2490,7 @@ void smb2_init_maperror(void)sizeof (struct status_to_posix_error ), cmp_smb2_status, NULL); } + +#if IS_ENABLED (CONFIG_SMB_KUNIT_TESTS ) +#include "smb2maperror_test.c" +#endif /* CONFIG_SMB_KUNIT_TESTS */
fs/smb/client/smb2maperror_test.c new file +86 -0
86 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH v3 9/9 smb/server: rename include guard in smb_common.h
In reply to [email protected]From: ChenXiaoSong <[email protected]>
Make the include guard more descriptive to avoid conflicts with include guards that may be used in the future.
--- fs/smb/server/smb_common.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
fs/smb/server/smb_common.h +3 -3
@@ -3,8 +3,8 @@* Copyright (C) 2018 Samsung Electronics Co., Ltd. */ -#ifndef __SMB_COMMON_H__ -#define __SMB_COMMON_H__ +#ifndef __SMB_SERVER_COMMON_H__ +#define __SMB_SERVER_COMMON_H__#include <linux/kernel.h>
@@ -196,4 +196,4 @@ unsigned int ksmbd_server_side_copy_max_chunk_size(void);unsigned int ksmbd_server_side_copy_max_total_size (void );bool is_asterisk (char *p);__le32 smb_map_generic_desired_access (__le32 daccess); -#endif /* __SMB_COMMON_H__ */ +#endif /* __SMB_SERVER_COMMON_H__ */
Signature · 1 line
2.43.0