Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp2439567pxp; Mon, 21 Mar 2022 20:30:50 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxDFS/jmArknig4DF7r7hKfzTCefjvJ4vLOQd9G/oQss3JOMi8qM8mXK75PC0BioUymVVSd X-Received: by 2002:a17:90b:3e88:b0:1bf:3bd0:4b5f with SMTP id rj8-20020a17090b3e8800b001bf3bd04b5fmr2573226pjb.106.1647919849933; Mon, 21 Mar 2022 20:30:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1647919849; cv=none; d=google.com; s=arc-20160816; b=h2FoV0jJ9SIXfATwNzVPSPF2L3/382ve0hzTtnoyThz0vuX1AZPC2vBv3WvEt98ejh bSDtei0DFLSQjiSsbgwaaXHv8VJt7EuKMriwjHehlUmC2SP3o2P9ZCgKbLns1X+a44gJ EHsO2QduzkU05mZl9Jc3VyD46lTzWITn2kX/wW0PljQjWi9Gzu/m62bWN3xaNP7bWrnT /sCK8SIXhQp6uxRBKYGA7UkGl1qYMmZrb4Jql4agVIjs9pRpd5jXYqqZKYE+wQnlUUYd jtlrP+Huhiy6F6gTnGCOE+H37Fxw9YiaC/9dszS3/CosZJ7o6HlGIKuIOJ8eqWrp2R5z ScjQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=FP+65ucbvtmQyss2TdNOr4OFO5NUEiXEmvJt6Nxohrc=; b=XdW1yG/gg1ObzVwhPiSRN1lODIYqnVFhQnDZhy/oSAHMJ22QNlQIB51SEmeMAjMzNP KNCts4cvenqwzmm7wP3ro8SF3PRwtDDgkCazFLvayx09c9bM7u8E9JKgxTa1dkp5khyG TNpeahPA37sd1B0bbUJLrmBZWVcv2qZ2jNp+aMhhMuJv1pwWOu+VGGNPu1HC8VqpU6eg nDsT+DaZR47GqDlqLEn2IDRDze5uyjGtC3ROXaxM+vVYrJBlDmDC5dlohpjPlkueCWhS 5anxFfc1dCGZVx/Io9NcZlU/RLKhTICgXyNfLzumJJ9yxEx5IgiSpewwJTDw9VY5oHct aSSA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=VMV50pMK; spf=softfail (google.com: domain of transitioning linux-ext4-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=collabora.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id z123-20020a626581000000b004fa3a8dffb9si8503893pfb.112.2022.03.21.20.30.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Mar 2022 20:30:49 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-ext4-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=VMV50pMK; spf=softfail (google.com: domain of transitioning linux-ext4-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=collabora.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 83076204A87; Mon, 21 Mar 2022 20:00:24 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235736AbiCVDBs (ORCPT + 99 others); Mon, 21 Mar 2022 23:01:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51418 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235718AbiCVDBo (ORCPT ); Mon, 21 Mar 2022 23:01:44 -0400 Received: from bhuna.collabora.co.uk (bhuna.collabora.co.uk [IPv6:2a00:1098:0:82:1000:25:2eeb:e3e3]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 347361FE564 for ; Mon, 21 Mar 2022 20:00:18 -0700 (PDT) Received: from [127.0.0.1] (localhost [127.0.0.1]) (Authenticated sender: krisman) with ESMTPSA id D50E21F41640 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1647918017; bh=9Gvy4Q6vMFRJVdm/DEWf0uzjdE0FiEbo+TJFE9FDuYU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=VMV50pMKCOUEBV9F8w1natXIZcuNjlbcR3UM3GnBQmr+rctPJVftlmJ61y+Q/9o36 qxDrzhwuGOvsOrwhXazSgmiotx6ItY7SwDOY+NrW1Lc+UbP6kiauvClDcAHSxKpkqC v03jRcqQe3/EU3mIzXaly2aiG7iyuPvwWH3p0v+yo6LhGC0Hx2MlMKtVpsRfTrr8ol Tj4IYT3NBE4diGFtWVxkQDNingJNyI9jq/lqMpIRcVB+4GSQ0njPj/XSWwEmHW7k4q B8tdhIsi74ylyJjx7kWqGZsj2po1CkrPtC/SWpc1ADWakFLm9WNX8aqyYAF26Z9oGm jxE7QVicwDqQA== From: Gabriel Krisman Bertazi To: tytso@mit.edu Cc: ebiggers@kernel.org, jaegeuk@kernel.org, linux-ext4@vger.kernel.org, Gabriel Krisman Bertazi , kernel@collabora.com Subject: [PATCH 1/5] ext4: Match the f2fs ci_compare implementation Date: Mon, 21 Mar 2022 23:00:00 -0400 Message-Id: <20220322030004.148560-2-krisman@collabora.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220322030004.148560-1-krisman@collabora.com> References: <20220322030004.148560-1-krisman@collabora.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE, UNPARSEABLE_RELAY autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-ext4@vger.kernel.org ext4_ci_compare originally follows utf8_*_strcmp, which means return zero on match. This means that every usage of that in ext4 negates the return. Turn it into a predicate function, let it follow the kernel convention and return true on match, which means it's now the same as its f2fs counterpart and can be extracted into generic code. This change also makes it more obvious that we are ignoring error handling in ext4_match, which can occur since casefolding support (bad utf8 name due to disk corruption on strict mode causes -EINVAL) and casefold+encryption (-ENOMEM). For now, keep the behavior. It is handled by the following patches. While we are there, change the comment to the kernel-doc style. Signed-off-by: Gabriel Krisman Bertazi --- fs/ext4/namei.c | 62 +++++++++++++++++++++++++++++++++---------------- 1 file changed, 42 insertions(+), 20 deletions(-) diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c index 8cf0a924a49b..24ea3bb446d0 100644 --- a/fs/ext4/namei.c +++ b/fs/ext4/namei.c @@ -1318,13 +1318,20 @@ static void dx_insert_block(struct dx_frame *frame, u32 hash, ext4_lblk_t block) } #if IS_ENABLED(CONFIG_UNICODE) -/* +/** + * ext4_ci_compare() - Match (case-insensitive) a name with a dirent. + * @parent: Inode of the parent of the dentry. + * @name: name under lookup. + * @de_name: Dirent name. + * @de_name_len: dirent name length. + * @quick: whether @name is already casefolded. + * * Test whether a case-insensitive directory entry matches the filename - * being searched for. If quick is set, assume the name being looked up - * is already in the casefolded form. + * being searched. If quick is set, the @name being looked up is + * already in the casefolded form. * - * Returns: 0 if the directory entry matches, more than 0 if it - * doesn't match or less than zero on error. + * Return: > 0 if the directory entry matches, 0 if it doesn't match, or + * < 0 on error. */ static int ext4_ci_compare(const struct inode *parent, const struct qstr *name, u8 *de_name, size_t de_name_len, bool quick) @@ -1333,7 +1340,7 @@ static int ext4_ci_compare(const struct inode *parent, const struct qstr *name, const struct unicode_map *um = sb->s_encoding; struct fscrypt_str decrypted_name = FSTR_INIT(NULL, de_name_len); struct qstr entry = QSTR_INIT(de_name, de_name_len); - int ret; + int ret, match = false; if (IS_ENCRYPTED(parent)) { const struct fscrypt_str encrypted_name = @@ -1354,20 +1361,22 @@ static int ext4_ci_compare(const struct inode *parent, const struct qstr *name, ret = utf8_strncasecmp_folded(um, name, &entry); else ret = utf8_strncasecmp(um, name, &entry); - if (ret < 0) { - /* Handle invalid character sequence as either an error - * or as an opaque byte sequence. + + if (!ret) + match = true; + else if (ret < 0 && !sb_has_strict_encoding(sb)) { + /* + * In non-strict mode, fallback to a byte comparison if + * the names have invalid characters. */ - if (sb_has_strict_encoding(sb)) - ret = -EINVAL; - else if (name->len != entry.len) - ret = 1; - else - ret = !!memcmp(name->name, entry.name, entry.len); + ret = 0; + match = ((name->len == entry.len) && + !memcmp(name->name, entry.name, entry.len)); } + out: kfree(decrypted_name.name); - return ret; + return (ret >= 0) ? match : ret; } int ext4_fname_setup_ci_filename(struct inode *dir, const struct qstr *iname, @@ -1418,6 +1427,7 @@ static bool ext4_match(struct inode *parent, struct ext4_dir_entry_2 *de) { struct fscrypt_name f; + int ret; if (!de->inode) return false; @@ -1442,11 +1452,23 @@ static bool ext4_match(struct inode *parent, return false; } } - return !ext4_ci_compare(parent, &cf, de->name, - de->name_len, true); + ret = ext4_ci_compare(parent, &cf, de->name, + de->name_len, true); + } else { + ret = ext4_ci_compare(parent, fname->usr_fname, + de->name, de->name_len, false); } - return !ext4_ci_compare(parent, fname->usr_fname, de->name, - de->name_len, false); + + if (ret < 0) { + /* + * Treat comparison errors as not a match. The + * only case where it happens is on a disk + * corruption or ENOMEM. + */ + return false; + } + return ret; + } #endif -- 2.35.1