[PATCH 21/30] smb/client: sort mapping_table_ERRDOS array

chenxiaosong.chenxiaosong@linux.dev posted 30 patches 1 week, 4 days ago
[PATCH 21/30] smb/client: sort mapping_table_ERRDOS array
Posted by chenxiaosong.chenxiaosong@linux.dev 1 week, 4 days ago
From: ChenXiaoSong <chenxiaosong@kylinos.cn>

Sort the array in ascending order, and then we can use binary search
algorithm to quickly find the target DOS class smb error code.

The array is sorted only once when cifs.ko is loaded.

Signed-off-by: ChenXiaoSong <chenxiaosong@kylinos.cn>
---
 fs/smb/client/netmisc.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/fs/smb/client/netmisc.c b/fs/smb/client/netmisc.c
index 239e5287d4d6..594a7fae0060 100644
--- a/fs/smb/client/netmisc.c
+++ b/fs/smb/client/netmisc.c
@@ -59,7 +59,7 @@ struct smb_to_posix_error {
 	int posix_code;
 };
 
-static const struct smb_to_posix_error mapping_table_ERRDOS[] = {
+static struct smb_to_posix_error mapping_table_ERRDOS[] = {
 	{ERRbadfunc, -EINVAL},
 	{ERRbadfile, -ENOENT},
 	{ERRbadpath, -ENOTDIR},
@@ -101,6 +101,9 @@ static const struct smb_to_posix_error mapping_table_ERRDOS[] = {
 	{0, 0}
 };
 
+static unsigned int errdos_num = sizeof(mapping_table_ERRDOS) /
+				 sizeof(struct smb_to_posix_error);
+
 static const struct smb_to_posix_error mapping_table_ERRSRV[] = {
 	{ERRerror, -EIO},
 	{ERRbadpw, -EACCES},  /* was EPERM */
@@ -803,6 +806,8 @@ static unsigned int ntstatus_to_dos_num = sizeof(ntstatus_to_dos_map) /
 DEFINE_CMP_FUNC(ntstatus_to_dos, ntstatus);
 /* cmp_nt_err_code_struct */
 DEFINE_CMP_FUNC(nt_err_code_struct, nt_errcode);
+/* cmp_smb_to_posix_error */
+DEFINE_CMP_FUNC(smb_to_posix_error, smb_err);
 
 /* search_in_ntstatus_to_dos_map */
 DEFINE_SEARCH_FUNC(ntstatus_to_dos, ntstatus, ntstatus_to_dos_map, ntstatus_to_dos_num);
@@ -1078,6 +1083,8 @@ void smb_init_maperror(void)
 	     sizeof(struct ntstatus_to_dos), cmp_ntstatus_to_dos, NULL);
 	sort(nt_errs, nt_err_num, sizeof(struct nt_err_code_struct),
 	     cmp_nt_err_code_struct, NULL);
+	sort(mapping_table_ERRDOS, errdos_num,
+	     sizeof(struct smb_to_posix_error), cmp_smb_to_posix_error, NULL);
 }
 
 #if IS_ENABLED(CONFIG_SMB_KUNIT_TESTS)
-- 
2.43.0