{"id":2219182,"url":"http://patchwork.ozlabs.org/api/1.0/patches/2219182/?format=json","project":{"id":12,"url":"http://patchwork.ozlabs.org/api/1.0/projects/12/?format=json","name":"Linux CIFS Client","link_name":"linux-cifs-client","list_id":"linux-cifs.vger.kernel.org","list_email":"linux-cifs@vger.kernel.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20260402141839.461257-12-huiwen.he@linux.dev>","date":"2026-04-02T14:18:37","name":"[v3,11/13] smb/client: use binary search for SMB1 DOS/SRV error mapping","commit_ref":null,"pull_url":null,"state":"new","archived":false,"hash":"a23ef20e40f0b99e6dadabfaaa60a2bd8f038353","submitter":{"id":93029,"url":"http://patchwork.ozlabs.org/api/1.0/people/93029/?format=json","name":null,"email":"huiwen.he@linux.dev"},"delegate":null,"mbox":"http://patchwork.ozlabs.org/project/linux-cifs-client/patch/20260402141839.461257-12-huiwen.he@linux.dev/mbox/","series":[{"id":498490,"url":"http://patchwork.ozlabs.org/api/1.0/series/498490/?format=json","date":"2026-04-02T14:18:28","name":"smb: improve search speed of SMB1 maperror","version":3,"mbox":"http://patchwork.ozlabs.org/series/498490/mbox/"}],"check":"pending","checks":"http://patchwork.ozlabs.org/api/patches/2219182/checks/","tags":{},"headers":{"Return-Path":"\n <linux-cifs+bounces-10644-incoming=patchwork.ozlabs.org@vger.kernel.org>","X-Original-To":["incoming@patchwork.ozlabs.org","linux-cifs@vger.kernel.org"],"Delivered-To":"patchwork-incoming@legolas.ozlabs.org","Authentication-Results":["legolas.ozlabs.org;\n\tdkim=pass (1024-bit key;\n unprotected) header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256\n header.s=key1 header.b=I9dATJdk;\n\tdkim-atps=neutral","legolas.ozlabs.org;\n spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org\n (client-ip=2600:3c0a:e001:db::12fc:5321; helo=sea.lore.kernel.org;\n envelope-from=linux-cifs+bounces-10644-incoming=patchwork.ozlabs.org@vger.kernel.org;\n receiver=patchwork.ozlabs.org)","smtp.subspace.kernel.org;\n\tdkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev\n header.b=\"I9dATJdk\"","smtp.subspace.kernel.org;\n arc=none smtp.client-ip=91.218.175.177","smtp.subspace.kernel.org;\n dmarc=pass (p=none dis=none) header.from=linux.dev","smtp.subspace.kernel.org;\n spf=pass smtp.mailfrom=linux.dev"],"Received":["from sea.lore.kernel.org (sea.lore.kernel.org\n [IPv6:2600:3c0a:e001:db::12fc:5321])\n\t(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)\n\t key-exchange x25519)\n\t(No client certificate requested)\n\tby legolas.ozlabs.org (Postfix) with ESMTPS id 4fmkhF6H9Pz1yGJ\n\tfor <incoming@patchwork.ozlabs.org>; Fri, 03 Apr 2026 01:27:09 +1100 (AEDT)","from smtp.subspace.kernel.org (conduit.subspace.kernel.org\n [100.90.174.1])\n\tby sea.lore.kernel.org (Postfix) with ESMTP id B744030B37CB\n\tfor <incoming@patchwork.ozlabs.org>; Thu,  2 Apr 2026 14:20:19 +0000 (UTC)","from localhost.localdomain (localhost.localdomain [127.0.0.1])\n\tby smtp.subspace.kernel.org (Postfix) with ESMTP id EE06B314B6A;\n\tThu,  2 Apr 2026 14:20:18 +0000 (UTC)","from out-177.mta0.migadu.com (out-177.mta0.migadu.com\n [91.218.175.177])\n\t(using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits))\n\t(No client certificate requested)\n\tby smtp.subspace.kernel.org (Postfix) with ESMTPS id DCA093E9595\n\tfor <linux-cifs@vger.kernel.org>; Thu,  2 Apr 2026 14:20:15 +0000 (UTC)"],"ARC-Seal":"i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116;\n\tt=1775139618; cv=none;\n b=eWGO2uQZ0Sd6Xqe7OqcTTOt/HnTqG2610k5JJxTCYwj4sj8VpAEdFsIKSPs209mGye5cpCkO0fllbRa75yvFCCVZtZEa67MCU522J1XSfdoxHeTxz+R2RHWfRH4XlLdNfdSWhwPGvSyIoAqX2maUDBva0bTDdAIWX5U1ALQjm1Y=","ARC-Message-Signature":"i=1; a=rsa-sha256; d=subspace.kernel.org;\n\ts=arc-20240116; t=1775139618; c=relaxed/simple;\n\tbh=J+Qf7F9HmvgTzO/kqWdgbA/+v0kJjp5pOAXyHiGB/rQ=;\n\th=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References:\n\t MIME-Version;\n b=Kf7jZ4aKg3Zm5reuvfc/K/iErTvve1mMWI1H8xiHRIkhGw/4TbrvgWvVFgObXOtVqm8yibkG/mSbfItw31YBHto29jNRmWZ9xqnvplYxYKeltFLYYouS+Z/2dQtWhbMnn8p0vz0xkd1x8PWQxxoCllCpH5mqJUTGtAzoc8ScS3c=","ARC-Authentication-Results":"i=1; smtp.subspace.kernel.org;\n dmarc=pass (p=none dis=none) header.from=linux.dev;\n spf=pass smtp.mailfrom=linux.dev;\n dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev\n header.b=I9dATJdk; arc=none smtp.client-ip=91.218.175.177","X-Report-Abuse":"Please report any abuse attempt to abuse@migadu.com and\n include these headers.","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1;\n\tt=1775139613;\n\th=from:from:reply-to:subject:subject:date:date:message-id:message-id:\n\t to:to:cc:cc:mime-version:mime-version:\n\t content-transfer-encoding:content-transfer-encoding:\n\t in-reply-to:in-reply-to:references:references;\n\tbh=wie3XPMhf984dwwEtLh87sNo0dnbHepfftrFNxTOpsw=;\n\tb=I9dATJdkxyNxWIh8DkjgDWWw2kUtyZcSjxtrle7NU/xmbtE+57gWshwzhohBTat4XTxmWI\n\tBQnwf3dTsIH2bfgxOmOqZ2IuLxwUDrNfibT6fA1oGR44tmx0w5wLlY6dzPzsK2/mHdLlJ8\n\tF/pQ7kivuty55FnUNuKRe25i2YQ6cmM=","From":"huiwen.he@linux.dev","To":"smfrench@gmail.com,\n\tlinkinjeon@kernel.org,\n\tdhowells@redhat.com,\n\tchenxiaosong@kylinos.cn,\n\tchenxiaosong@chenxiaosong.com,\n\ttangyouling@kylinos.cn","Cc":"linux-cifs@vger.kernel.org,\n\tHuiwen He <hehuiwen@kylinos.cn>","Subject":"[PATCH v3 11/13] smb/client: use binary search for SMB1 DOS/SRV error\n mapping","Date":"Thu,  2 Apr 2026 14:18:37 +0000","Message-ID":"<20260402141839.461257-12-huiwen.he@linux.dev>","In-Reply-To":"<20260402141839.461257-1-huiwen.he@linux.dev>","References":"<20260402141839.461257-1-huiwen.he@linux.dev>","Precedence":"bulk","X-Mailing-List":"linux-cifs@vger.kernel.org","List-Id":"<linux-cifs.vger.kernel.org>","List-Subscribe":"<mailto:linux-cifs+subscribe@vger.kernel.org>","List-Unsubscribe":"<mailto:linux-cifs+unsubscribe@vger.kernel.org>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","X-Migadu-Flow":"FLOW_OUT"},"content":"From: Huiwen He <hehuiwen@kylinos.cn>\n\nCurrently, map_smb_to_linux_error() uses linear searches for both\nmapping_table_ERRDOS[] and mapping_table_ERRSRV[].\n\nRefactor this by introducing search_mapping_table_ERRDOS() and\nsearch_mapping_table_ERRSRV() that implements binary search(as the tables\nare sorted).This improves lookup performance and reduces code duplication.\n\nAlso remove the sentinel entries from the mapping tables as they are no\nlonger needed with ARRAY_SIZE().\n\nSigned-off-by: Huiwen He <hehuiwen@kylinos.cn>\nReviewed-by: ChenXiaoSong <chenxiaosong@kylinos.cn>\n---\n fs/smb/client/smb1maperror.c | 67 ++++++++++++++++++++----------------\n 1 file changed, 37 insertions(+), 30 deletions(-)","diff":"diff --git a/fs/smb/client/smb1maperror.c b/fs/smb/client/smb1maperror.c\nindex 294ac9646bff..28e1c84fa83b 100644\n--- a/fs/smb/client/smb1maperror.c\n+++ b/fs/smb/client/smb1maperror.c\n@@ -9,6 +9,7 @@\n  *   Copyright (C) Luke Kenneth Casson Leighton 1997-2001.\n  */\n \n+#include <linux/bsearch.h>\n #include \"cifsproto.h\"\n #include \"smb1proto.h\"\n #include \"smberr.h\"\n@@ -20,13 +21,24 @@ struct smb_to_posix_error {\n \tint posix_code;\n };\n \n+static __always_inline int smb1_posix_error_cmp(const void *_key, const void *_pivot)\n+{\n+\t__u16 key = *(__u16 *)_key;\n+\tconst struct smb_to_posix_error *pivot = _pivot;\n+\n+\tif (key < pivot->smb_err)\n+\t\treturn -1;\n+\tif (key > pivot->smb_err)\n+\t\treturn 1;\n+\treturn 0;\n+}\n+\n static const struct smb_to_posix_error mapping_table_ERRDOS[] = {\n /*\n  * Automatically generated by the `gen_smb1_mapping` script,\n  * sorted by DOS error code (ascending).\n  */\n #include \"smb1_err_dos_map.c\"\n-\t{0, 0}\n };\n \n static const struct smb_to_posix_error mapping_table_ERRSRV[] = {\n@@ -35,7 +47,6 @@ static const struct smb_to_posix_error mapping_table_ERRSRV[] = {\n  * sorted by SRV error code (ascending).\n  */\n #include \"smb1_err_srv_map.c\"\n-\t{0, 0}\n };\n \n /*****************************************************************************\n@@ -72,14 +83,32 @@ search_ntstatus_to_dos_map(__u32 ntstatus)\n \t\t\t\tntstatus_to_dos_cmp);\n }\n \n+static const struct smb_to_posix_error *\n+search_mapping_table_ERRDOS(__u16 smb_err)\n+{\n+\treturn __inline_bsearch(&smb_err, mapping_table_ERRDOS,\n+\t\t\t\tARRAY_SIZE(mapping_table_ERRDOS),\n+\t\t\t\tsizeof(struct smb_to_posix_error),\n+\t\t\t\tsmb1_posix_error_cmp);\n+}\n+\n+static const struct smb_to_posix_error *\n+search_mapping_table_ERRSRV(__u16 smb_err)\n+{\n+\treturn __inline_bsearch(&smb_err, mapping_table_ERRSRV,\n+\t\t\t\tARRAY_SIZE(mapping_table_ERRSRV),\n+\t\t\t\tsizeof(struct smb_to_posix_error),\n+\t\t\t\tsmb1_posix_error_cmp);\n+}\n+\n int\n map_smb_to_linux_error(char *buf, bool logErr)\n {\n \tstruct smb_hdr *smb = (struct smb_hdr *)buf;\n-\tunsigned int i;\n \tint rc = -EIO;\t/* if transport error smb error may not be set */\n \t__u8 smberrclass;\n \t__u16 smberrcode;\n+\tconst struct smb_to_posix_error *err_map = NULL;\n \n \t/* BB if NT Status codes - map NT BB */\n \n@@ -112,38 +141,16 @@ map_smb_to_linux_error(char *buf, bool logErr)\n \n \t/* old style errors */\n \n-\t/* DOS class smb error codes - map DOS */\n \tif (smberrclass == ERRDOS) {\n+\t\t/* DOS class smb error codes - map DOS */\n \t\t/* 1 byte field no need to byte reverse */\n-\t\tfor (i = 0;\n-\t\t     i <\n-\t\t     sizeof(mapping_table_ERRDOS) /\n-\t\t     sizeof(struct smb_to_posix_error); i++) {\n-\t\t\tif (mapping_table_ERRDOS[i].smb_err == 0)\n-\t\t\t\tbreak;\n-\t\t\telse if (mapping_table_ERRDOS[i].smb_err ==\n-\t\t\t\t\t\t\t\tsmberrcode) {\n-\t\t\t\trc = mapping_table_ERRDOS[i].posix_code;\n-\t\t\t\tbreak;\n-\t\t\t}\n-\t\t\t/* else try next error mapping one to see if match */\n-\t\t}\n+\t\terr_map = search_mapping_table_ERRDOS(smberrcode);\n \t} else if (smberrclass == ERRSRV) {\n \t\t/* server class of error codes */\n-\t\tfor (i = 0;\n-\t\t     i <\n-\t\t     sizeof(mapping_table_ERRSRV) /\n-\t\t     sizeof(struct smb_to_posix_error); i++) {\n-\t\t\tif (mapping_table_ERRSRV[i].smb_err == 0)\n-\t\t\t\tbreak;\n-\t\t\telse if (mapping_table_ERRSRV[i].smb_err ==\n-\t\t\t\t\t\t\t\tsmberrcode) {\n-\t\t\t\trc = mapping_table_ERRSRV[i].posix_code;\n-\t\t\t\tbreak;\n-\t\t\t}\n-\t\t\t/* else try next error mapping to see if match */\n-\t\t}\n+\t\terr_map = search_mapping_table_ERRSRV(smberrcode);\n \t}\n+\tif (err_map)\n+\t\trc = err_map->posix_code;\n \t/* else ERRHRD class errors or junk  - return EIO */\n \n \t/* special cases for NT status codes which cannot be translated to DOS codes */\n","prefixes":["v3","11/13"]}