From nobody Sun Feb 8 14:22:58 2026 Received: from madrid.collaboradmins.com (madrid.collaboradmins.com [46.235.227.194]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2A72069313; Thu, 8 Feb 2024 06:45:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=46.235.227.194 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707374713; cv=none; b=qFFJQT6sGdQYjPI4n/QjmDbp5H2DJuS14OfVzLmvVJwdUZMxLaNBgjXao1PRU3lYd/X6CHi1Z4yFe+HIYiY25gos2AH19+XfUA9+RYG9D3M54To4ITKsUMzgGRjanLXSQGjFJ1hKREZjs2KQqbUjqsUK+clrgPUjPF5Rn9sXiCA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707374713; c=relaxed/simple; bh=z2xXaS0+/d8JUZt7vHBl6tVYdew9NWG64RIjh0LO0O8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=lf2ho9H2QJU98ZcR6KRxIt4uO5nQdvC5XSNV0jAPbU315B2583dUFUKtJiYqJLD6HTKitSN/KnOWrCRbONaZeKGQ+wDWvXnrmULHStzWvzQRtu+kXKwuGqACO+0YlZc3rzJk9gdkN7chAYtIqJSr+pt3uPcXIJ5oFcaeKgLZ5kw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=collabora.com; spf=pass smtp.mailfrom=collabora.com; dkim=pass (2048-bit key) header.d=collabora.com header.i=@collabora.com header.b=L2FWO1Lt; arc=none smtp.client-ip=46.235.227.194 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=collabora.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=collabora.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=collabora.com header.i=@collabora.com header.b="L2FWO1Lt" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1707374710; bh=z2xXaS0+/d8JUZt7vHBl6tVYdew9NWG64RIjh0LO0O8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=L2FWO1LtN1PJ9pspTv+w5NTJ8ASxKkSRc7w5HoQhJXqyhSkESPoR8jl6ocuGtMjKR TqMZJ0xmxKipeCK7KtmlppU1i59jlzFX7+yap0fPtsG4RrRcYKXpjJOnwiwuChi5UJ 8A7z52mgd4k5pxn13ni9wMsqod4lTyqeOxmaRsw246lmF3TYnHXc4TfYDWmHUbHuFM IlQmaug+CBKMkc93ggU2Ra9rH6M/bQu5p3WKVzo9bWaDGjaW8P0yJoN42OZWnfILjm 4CmObalmGnHkTFIMsX40xRQEWqmaXE0CK+n6itHLYXdzY6DsJ3OqR365RDCR3/xkAq 9/w+6/FM2ratQ== Received: from eugen-station.. (cola.collaboradmins.com [195.201.22.229]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: ehristev) by madrid.collaboradmins.com (Postfix) with ESMTPSA id 0A2253782098; Thu, 8 Feb 2024 06:45:05 +0000 (UTC) From: Eugen Hristev To: tytso@mit.edu, adilger.kernel@dilger.ca, jaegeuk@kernel.org, chao@kernel.org, viro@zeniv.linux.org.uk, brauner@kernel.org, linux-ext4@vger.kernel.org, linux-f2fs-devel@lists.sourceforge.net Cc: jack@suse.cz, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, kernel@collabora.com, eugen.hristev@collabora.com, Gabriel Krisman Bertazi , Eric Biggers Subject: [RESEND PATCH v9 1/3] libfs: Introduce case-insensitive string comparison helper Date: Thu, 8 Feb 2024 08:43:32 +0200 Message-Id: <20240208064334.268216-2-eugen.hristev@collabora.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240208064334.268216-1-eugen.hristev@collabora.com> References: <20240208064334.268216-1-eugen.hristev@collabora.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Gabriel Krisman Bertazi generic_ci_match can be used by case-insensitive filesystems to compare strings under lookup with dirents in a case-insensitive way. This function is currently reimplemented by each filesystem supporting casefolding, so this reduces code duplication in filesystem-specific code. Reviewed-by: Eric Biggers Signed-off-by: Gabriel Krisman Bertazi Signed-off-by: Eugen Hristev --- fs/libfs.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++ include/linux/fs.h | 4 +++ 2 files changed, 72 insertions(+) diff --git a/fs/libfs.c b/fs/libfs.c index bb18884ff20e..f80cb982ac89 100644 --- a/fs/libfs.c +++ b/fs/libfs.c @@ -1773,6 +1773,74 @@ static const struct dentry_operations generic_ci_den= try_ops =3D { .d_hash =3D generic_ci_d_hash, .d_compare =3D generic_ci_d_compare, }; + +/** + * generic_ci_match() - Match a name (case-insensitively) with a dirent. + * @parent: Inode of the parent of the dirent under comparison + * @name: name under lookup. + * @folded_name: Optional pre-folded name under lookup + * @de_name: Dirent name. + * @de_name_len: dirent name length. + * + * + * Test whether a case-insensitive directory entry matches the filename + * being searched. If @folded_name is provided, it is used instead of + * recalculating the casefold of @name. + * + * Return: > 0 if the directory entry matches, 0 if it doesn't match, or + * < 0 on error. + */ +int generic_ci_match(const struct inode *parent, + const struct qstr *name, + const struct qstr *folded_name, + const u8 *de_name, u32 de_name_len) +{ + const struct super_block *sb =3D parent->i_sb; + const struct unicode_map *um =3D sb->s_encoding; + struct fscrypt_str decrypted_name =3D FSTR_INIT(NULL, de_name_len); + struct qstr dirent =3D QSTR_INIT(de_name, de_name_len); + int res, match =3D false; + + if (IS_ENCRYPTED(parent)) { + const struct fscrypt_str encrypted_name =3D + FSTR_INIT((u8 *) de_name, de_name_len); + + if (WARN_ON_ONCE(!fscrypt_has_encryption_key(parent))) + return -EINVAL; + + decrypted_name.name =3D kmalloc(de_name_len, GFP_KERNEL); + if (!decrypted_name.name) + return -ENOMEM; + res =3D fscrypt_fname_disk_to_usr(parent, 0, 0, &encrypted_name, + &decrypted_name); + if (res < 0) + goto out; + dirent.name =3D decrypted_name.name; + dirent.len =3D decrypted_name.len; + } + + if (folded_name->name) + res =3D utf8_strncasecmp_folded(um, folded_name, &dirent); + else + res =3D utf8_strncasecmp(um, name, &dirent); + + if (!res) + match =3D true; + else if (res < 0 && !sb_has_strict_encoding(sb)) { + /* + * In non-strict mode, fallback to a byte comparison if + * the names have invalid characters. + */ + res =3D 0; + match =3D ((name->len =3D=3D dirent.len) && + !memcmp(name->name, dirent.name, dirent.len)); + } + +out: + kfree(decrypted_name.name); + return (res >=3D 0) ? match : res; +} +EXPORT_SYMBOL(generic_ci_match); #endif =20 #ifdef CONFIG_FS_ENCRYPTION diff --git a/include/linux/fs.h b/include/linux/fs.h index 820b93b2917f..7af691ff8d44 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -3296,6 +3296,10 @@ extern int generic_file_fsync(struct file *, loff_t,= loff_t, int); extern int generic_check_addressable(unsigned, u64); =20 extern void generic_set_encrypted_ci_d_ops(struct dentry *dentry); +extern int generic_ci_match(const struct inode *parent, + const struct qstr *name, + const struct qstr *folded_name, + const u8 *de_name, u32 de_name_len); =20 static inline bool sb_has_encoding(const struct super_block *sb) { --=20 2.34.1