[PATCH 23/30] smb/client: use bsearch() to find target in mapping_table_ERRDOS array

chenxiaosong.chenxiaosong@linux.dev posted 30 patches 1 week, 4 days ago
[PATCH 23/30] smb/client: use bsearch() to find target in mapping_table_ERRDOS array
Posted by chenxiaosong.chenxiaosong@linux.dev 1 week, 4 days ago
From: ChenXiaoSong <chenxiaosong@kylinos.cn>

The array currently has 39 elements. When searching for the last element,
the original loop-based search method requires 39 comparisons, while
binary search algorithm requires only 5 comparisons.

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

diff --git a/fs/smb/client/netmisc.c b/fs/smb/client/netmisc.c
index 594a7fae0060..d7d1b9b4abcd 100644
--- a/fs/smb/client/netmisc.c
+++ b/fs/smb/client/netmisc.c
@@ -813,6 +813,8 @@ DEFINE_CMP_FUNC(smb_to_posix_error, smb_err);
 DEFINE_SEARCH_FUNC(ntstatus_to_dos, ntstatus, ntstatus_to_dos_map, ntstatus_to_dos_num);
 /* search_in_nt_errs */
 DEFINE_SEARCH_FUNC(nt_err_code_struct, nt_errcode, nt_errs, nt_err_num);
+/* search_in_mapping_table_ERRDOS */
+DEFINE_SEARCH_FUNC(smb_to_posix_error, smb_err, mapping_table_ERRDOS, errdos_num);
 
 /*****************************************************************************
  Print an error message from the status code
@@ -861,6 +863,7 @@ map_smb_to_linux_error(char *buf, bool logErr)
 	int rc = -EIO;	/* if transport error smb error may not be set */
 	__u8 smberrclass;
 	__u16 smberrcode;
+	struct smb_to_posix_error *err_map;
 
 	/* BB if NT Status codes - map NT BB */
 
@@ -887,19 +890,9 @@ map_smb_to_linux_error(char *buf, bool logErr)
 	/* DOS class smb error codes - map DOS */
 	if (smberrclass == ERRDOS) {
 		/* 1 byte field no need to byte reverse */
-		for (i = 0;
-		     i <
-		     sizeof(mapping_table_ERRDOS) /
-		     sizeof(struct smb_to_posix_error); i++) {
-			if (mapping_table_ERRDOS[i].smb_err == 0)
-				break;
-			else if (mapping_table_ERRDOS[i].smb_err ==
-								smberrcode) {
-				rc = mapping_table_ERRDOS[i].posix_code;
-				break;
-			}
-			/* else try next error mapping one to see if match */
-		}
+		err_map = search_in_mapping_table_ERRDOS(smberrcode);
+		if (err_map)
+			rc = err_map->posix_code;
 	} else if (smberrclass == ERRSRV) {
 		/* server class of error codes */
 		for (i = 0;
-- 
2.43.0