From patchwork Mon Feb 19 07:44:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eugen Hristev X-Patchwork-Id: 202919 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:693c:2685:b0:108:e6aa:91d0 with SMTP id mn5csp1134253dyc; Sun, 18 Feb 2024 23:46:56 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVkDgsK+20AW4/mvOk66dOLoQBzfm68oYLLpPk/71VwE7anqHOJCsNjLmkapnA1yE8jhbJChf0t6jXsXIVIuZs21ySFmw== X-Google-Smtp-Source: AGHT+IFvFPtYUAXSElXDSFG0hU101ORNFQM1otAMo91ITAwSjgwZSgM6/hcAHis6ZscN87wHgdDT X-Received: by 2002:a17:906:a0ce:b0:a3d:8f37:c37f with SMTP id bh14-20020a170906a0ce00b00a3d8f37c37fmr8686195ejb.6.1708328816673; Sun, 18 Feb 2024 23:46:56 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708328816; cv=pass; d=google.com; s=arc-20160816; b=Wgovay26tFHN18rfJ+jSm5Y4sAeb+ONWd9GfjNJ1L2NqWu91SLqdYdEGc0I4UM5UKR iaDI8Go2VSYrOCsO7bdnfxI97qZet2wkdS54AxAbxOnCRxr82K/0mhq2LxpOXnMLKfDh f06de6j6zKYXjOKFAvkoa+4UYrgf3pclTpKHrz2J4jJ25V9UFYSeLC+5RM8jnwDjewoP Y/deJLpo44GSVaYRGX0PczU2wtBjJSbKas/i+4mWahm6PTSihAL0YhwKUhtOZNfoDaAq Fa/PKHi9+wt7iFngb9KSuyJpgykBlnbykrZ9jZDhjOrtYbH1XdvDQ33H2hg90kht+vE3 Gguw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=korWbqxX5Hr+uz5CdvBvEBzW1AwHDFAjwdIk4aB34GU=; fh=YX4HbHS7wMJfdu6Xp+2MgHIyDiEardnLcvtWgzaTy/w=; b=lHVIyVLPyn4ja6Q7OuNYMYWfCwoga6K/m572jGGdlOq2oGqTT3E+UihRI+pzT0GbPq JMj2A5x1N8fVNt2et1BXKsQYluftS16+oEjGcCHvSN7EP+Y+agijHUwJOE0/fFegcgAj e+MettknVbhyqdtDMKlAIOZ3K2YnHFZBCnj75yhuMBb14i4JLhbJaEV119cEyndZwjVU T4cxRESSL/NPukd39M3xdaVQ6AgWFjnG8DEGowGmJt2I86g0M+LtfUdoaZSYJ8M68rMD hGYsA6cILB9ZaTSpKsiPGHdm9BBWzPLXhDKeOGgScOUipAGRxYbAbH5qyITKPQHdmngA RDhQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=Sznpx52p; arc=pass (i=1 spf=pass spfdomain=collabora.com dkim=pass dkdomain=collabora.com dmarc=pass fromdomain=collabora.com); spf=pass (google.com: domain of linux-kernel+bounces-70854-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-70854-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=collabora.com Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id go44-20020a1709070dac00b00a3e544f1944si1315822ejc.1021.2024.02.18.23.46.56 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 18 Feb 2024 23:46:56 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-70854-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=Sznpx52p; arc=pass (i=1 spf=pass spfdomain=collabora.com dkim=pass dkdomain=collabora.com dmarc=pass fromdomain=collabora.com); spf=pass (google.com: domain of linux-kernel+bounces-70854-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-70854-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=collabora.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 190731F2130E for ; Mon, 19 Feb 2024 07:46:56 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 794C823763; Mon, 19 Feb 2024 07:45:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=collabora.com header.i=@collabora.com header.b="Sznpx52p" 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 4B1FC20DEB; Mon, 19 Feb 2024 07:45:24 +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=1708328725; cv=none; b=UJ2Rk4qPKp/Do6JtbYyVoOdrwj90m+5U8ijwYtM+jjTGwEK+1ZVLeUNU9T9XmphGJLi3bZ+g0pSG++Ntm1MM9v7Df8OFE9a3cBmszCr7HrpQR0WkY4CXpPrrGXiwi3lz0t+6iI3c4jAdhsVTjoUUUT2upgpPIT4kSSU+PvPCCUg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708328725; c=relaxed/simple; bh=U7EXJvUhnoJ1TPYIL6nsMvXQA9YnijdjvHehr/j4+oM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Z8F7KcaMFHAwWeKgClMhRInj8YWDcXixR1U50ehP8QYolreCWOGwQ/BzItvCLmw2AwAJhrAYdxHhInhH8lzODWYKCOVEe1GNEifsHHV98v9Y40P6NH2V+pdKd/oWSPUGMvngKBm8ZNKoJZhXtYvy0NyO0yFxtJvxwaIA9nHS20Q= 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=Sznpx52p; 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 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1708328722; bh=U7EXJvUhnoJ1TPYIL6nsMvXQA9YnijdjvHehr/j4+oM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Sznpx52purKWjq33L2Kl8iu8Arwz3fqSmqIUQY29gH7mYz1ibYFDy00XcrRWKJZW3 UccyJSWZbZrS6sTMIF4cxQL7qhWiUHji9VMnlC/gffjAuSopQOvzJ5vcfcQ4y/g7h8 tYm4uQ8xW+2AlRDJq9i2RurK2T5/dOMOa+X7JvplxeY6npz8Z9VF5HQexDOCGTQqSH 3pVCuGEdAmfC11XnGBK/W6wYTzS54bA15EdnV95AqijDO0KRcdtHt1vTW2Sp2Q17V+ gGVm8QjalKYJkjnutZx3Q+X1FavDe94JLYr64m4upPqtaJ325jX+o+fsxdEyNJItzP jIkEWTNrTMEbA== 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 95D8B378201D; Mon, 19 Feb 2024 07:45:18 +0000 (UTC) From: Eugen Hristev To: tytso@mit.edu, adilger.kernel@dilger.ca, linux-ext4@vger.kernel.org, jaegeuk@kernel.org, chao@kernel.org, linux-f2fs-devel@lists.sourceforge.net, linux-fsdevel@vger.kernel.org Cc: linux-kernel@vger.kernel.org, kernel@collabora.com, eugen.hristev@collabora.com, viro@zeniv.linux.org.uk, brauner@kernel.org, jack@suse.cz, krisman@suse.de, Gabriel Krisman Bertazi , Eric Biggers Subject: [PATCH v11 3/8] libfs: Introduce case-insensitive string comparison helper Date: Mon, 19 Feb 2024 09:44:52 +0200 Message-Id: <20240219074457.24578-4-eugen.hristev@collabora.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240219074457.24578-1-eugen.hristev@collabora.com> References: <20240219074457.24578-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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791312597312216668 X-GMAIL-MSGID: 1791312597312216668 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 [eugen.hristev@collabora.com: port to 6.8-rc4] Signed-off-by: Eugen Hristev --- fs/libfs.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++ include/linux/fs.h | 4 +++ 2 files changed, 71 insertions(+) diff --git a/fs/libfs.c b/fs/libfs.c index bb18884ff20e..31c4cd156576 100644 --- a/fs/libfs.c +++ b/fs/libfs.c @@ -1773,6 +1773,73 @@ static const struct dentry_operations generic_ci_dentry_ops = { .d_hash = generic_ci_d_hash, .d_compare = 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 = parent->i_sb; + const struct unicode_map *um = sb->s_encoding; + struct fscrypt_str decrypted_name = FSTR_INIT(NULL, de_name_len); + struct qstr dirent = QSTR_INIT(de_name, de_name_len); + int res, match = false; + + if (IS_ENCRYPTED(parent)) { + const struct fscrypt_str encrypted_name = + FSTR_INIT((u8 *) de_name, de_name_len); + + if (WARN_ON_ONCE(!fscrypt_has_encryption_key(parent))) + return -EINVAL; + + decrypted_name.name = kmalloc(de_name_len, GFP_KERNEL); + if (!decrypted_name.name) + return -ENOMEM; + res = fscrypt_fname_disk_to_usr(parent, 0, 0, &encrypted_name, + &decrypted_name); + if (res < 0) + goto out; + dirent.name = decrypted_name.name; + dirent.len = decrypted_name.len; + } + + if (folded_name->name) + res = utf8_strncasecmp_folded(um, folded_name, &dirent); + else + res = utf8_strncasecmp(um, name, &dirent); + + if (!res) + match = 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 = 0; + match = ((name->len == dirent.len) && + !memcmp(name->name, dirent.name, dirent.len)); + } + +out: + kfree(decrypted_name.name); + return (res >= 0) ? match : res; +} +EXPORT_SYMBOL(generic_ci_match); #endif #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); 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); static inline bool sb_has_encoding(const struct super_block *sb) {