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

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

The array currently has 37 elements. When searching for the last element,
the original loop-based search method requires 37 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, 5 insertions(+), 14 deletions(-)

diff --git a/fs/smb/client/netmisc.c b/fs/smb/client/netmisc.c
index 4b5c049d69b9..8f3242c1d3da 100644
--- a/fs/smb/client/netmisc.c
+++ b/fs/smb/client/netmisc.c
@@ -817,6 +817,8 @@ DEFINE_SEARCH_FUNC(ntstatus_to_dos, ntstatus, ntstatus_to_dos_map, ntstatus_to_d
 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);
+/* search_in_mapping_table_ERRSRV */
+DEFINE_SEARCH_FUNC(smb_to_posix_error, smb_err, mapping_table_ERRSRV, errsrv_num);
 
 /*****************************************************************************
  Print an error message from the status code
@@ -861,7 +863,6 @@ int
 map_smb_to_linux_error(char *buf, bool logErr)
 {
 	struct smb_hdr *smb = (struct smb_hdr *)buf;
-	unsigned int i;
 	int rc = -EIO;	/* if transport error smb error may not be set */
 	__u8 smberrclass;
 	__u16 smberrcode;
@@ -897,19 +898,9 @@ map_smb_to_linux_error(char *buf, bool logErr)
 			rc = err_map->posix_code;
 	} else if (smberrclass == ERRSRV) {
 		/* server class of error codes */
-		for (i = 0;
-		     i <
-		     sizeof(mapping_table_ERRSRV) /
-		     sizeof(struct smb_to_posix_error); i++) {
-			if (mapping_table_ERRSRV[i].smb_err == 0)
-				break;
-			else if (mapping_table_ERRSRV[i].smb_err ==
-								smberrcode) {
-				rc = mapping_table_ERRSRV[i].posix_code;
-				break;
-			}
-			/* else try next error mapping to see if match */
-		}
+		err_map = search_in_mapping_table_ERRSRV(smberrcode);
+		if (err_map)
+			rc = err_map->posix_code;
 	}
 	/* else ERRHRD class errors or junk  - return EIO */
 
-- 
2.43.0