Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp1543886iob; Fri, 29 Apr 2022 07:35:51 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwwgNbPn2x2wCXo6y5Lqb8DpfzAYfvFy2lmMcalaL0vYGFIUknaJQLg1vQDwISOxJdPYU4U X-Received: by 2002:a65:63d9:0:b0:374:6b38:c6b3 with SMTP id n25-20020a6563d9000000b003746b38c6b3mr32712176pgv.195.1651242950952; Fri, 29 Apr 2022 07:35:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1651242950; cv=none; d=google.com; s=arc-20160816; b=kuMR5HmOiVoJRl0DN2PT9B6rD41dzYYM1S1Vm57Zri6U34aYhp3jzvNzU5qDQWOLzk mGJ+0U5K1bvnOLann6dMuFT0jSFqarmIAT4PipXOwMPmaUkRgA3utD1j6JZmMuTt/cFN HW5FFsne+i6oeo3duNa3uMfxtVGoGEPLVDSGj+ePV4xWxxZSUef0MBQJKfjP6LL7Zvl1 Yek+n1C0okzUwleH1RODDXy+owt59kBe0GAqUHlDbQgOwMQqfL7RmbDC219A8P5q8dI/ C9NUVHyDEGlIlIBA69CXYuffYlD9KJvxZWzCeQKnIzMHxWEUkeUCc54wM94jUanKxw4Z 7CXw== 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=6xXpE7/g2wMG5ENmnwgHUb+GIjB93dui2dvMsI2bpYc=; b=NQn1IyD8uS9GlbaDFKH7kRpRrFTgU+ndDV/1RPUVg7Y4z+fojp06jqKeS0RrKS7eS4 6lEPvNJ5t6DDn9v+Ft+cELI1mERUQM9urzF/TcbzzhoYRfx2aL4FLpxAB/h9rEv/Dc8R sgMsLdNFHVpvvRwvmNmXHgre70O1wdXT92HrrQNEudEloycGixi1jg+3bVJdk1PmnYSp gRmRJi3tm6kr4JIYVG5Qr1V3gcV/ZJLBEipzEtexBWrwl3sowZOFXrkznOGrj/eGD/pq 26eCBX1B1oUrAJzY5zmvYxKjvrye9t+Ux66UiRgU1We4KRjRlpDkLK/aSYlEy10Gb/oO M0Aw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=dQUMWCvI; spf=pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id k192-20020a636fc9000000b003aa36aa1f4asi7435377pgc.515.2022.04.29.07.35.29; Fri, 29 Apr 2022 07:35:50 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=dQUMWCvI; spf=pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=collabora.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1352798AbiD1WOB (ORCPT + 99 others); Thu, 28 Apr 2022 18:14:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42766 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1352826AbiD1WOA (ORCPT ); Thu, 28 Apr 2022 18:14:00 -0400 Received: from bhuna.collabora.co.uk (bhuna.collabora.co.uk [46.235.227.227]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7723A5C77E for ; Thu, 28 Apr 2022 15:10:44 -0700 (PDT) Received: from [127.0.0.1] (localhost [127.0.0.1]) (Authenticated sender: krisman) with ESMTPSA id 1CF9E1F427E9 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1651183843; bh=FK7rUcRBp1xrSrfIK5fgmYWs74fNFXneETM/YkjPK/k=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=dQUMWCvI88Eq0x/rnYQ34V2sPYxSePsszSMKBMpnRU6BEBKTNbBV8hep15PwIn3r8 pMorb2U9+rRc3mPiiP1hJ6PrLznY1vuD0ZCLNBFNnPCr7Wq/YZ15SnBbahahmyg8Kf AKKFIcKd2Li8hV7NjRA7zRB8Hv8qxMoM3+N48CQ2Vv9+lqaDp5LF98z0HU30SG/Ywn rK/c6IjtT/PgIdKJviP3gIOjnsMX9MhNWLBfHqiAHeaOuBajEcqz2V/CqAyOlHxyVB B+jiaXJuE7lsqOn5anFYzYUYcZLxd+q7PP+l6N9xJBklLhvPSJQmsY6Xrg8mKiFxTx gFXxQWiBSieBw== From: Gabriel Krisman Bertazi To: tytso@mit.edu, adilger.kernel@dilger.ca, jaegeuk@kernel.org Cc: linux-ext4@vger.kernel.org, linux-f2fs-devel@lists.sourceforge.net, ebiggers@kernel.org, Gabriel Krisman Bertazi , kernel@collabora.com Subject: [PATCH v2 1/7] ext4: Match the f2fs ci_compare implementation Date: Thu, 28 Apr 2022 18:10:21 -0400 Message-Id: <20220428221027.269084-2-krisman@collabora.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220428221027.269084-1-krisman@collabora.com> References: <20220428221027.269084-1-krisman@collabora.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_PASS,SPF_PASS, UNPARSEABLE_RELAY autolearn=ham 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 --- changes since v1: - rename to match f2fs naming (Eric) --- fs/ext4/namei.c | 65 ++++++++++++++++++++++++++++++++----------------- 1 file changed, 43 insertions(+), 22 deletions(-) diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c index 767b4bfe39c3..c363f637057d 100644 --- a/fs/ext4/namei.c +++ b/fs/ext4/namei.c @@ -1318,22 +1318,29 @@ static void dx_insert_block(struct dx_frame *frame, u32 hash, ext4_lblk_t block) } #if IS_ENABLED(CONFIG_UNICODE) -/* +/** + * ext4_match_ci() - 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) +static int ext4_match_ci(const struct inode *parent, const struct qstr *name, + u8 *de_name, size_t de_name_len, bool quick) { const struct super_block *sb = parent->i_sb; 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,22 @@ static bool ext4_match(struct inode *parent, return false; } } - return !ext4_ci_compare(parent, &cf, de->name, - de->name_len, true); + ret = ext4_match_ci(parent, &cf, de->name, + de->name_len, true); + } else { + ret = ext4_match_ci(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 !ext4_ci_compare(parent, fname->usr_fname, de->name, - de->name_len, false); + return ret; } #endif -- 2.35.1