Received: by 2002:a89:2c3:0:b0:1ed:23cc:44d1 with SMTP id d3csp303226lqs; Tue, 5 Mar 2024 02:29:14 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWmOB0LmcY9HedHLWp7GXpHN2ZEkYxy9Bk4wfGzvID2GrAeTVcTSwlTzNNtUMgltWnl0e4EfRQvIObYSnd0yYb6NjIFIJ9bekWLN+eW9Q== X-Google-Smtp-Source: AGHT+IF5j23cNJdtPsqUXrrSBobdvDKwbrSauEm+eAsdPLIJGJYTK2E0I+BGsefsO1cVGxi80DYz X-Received: by 2002:a05:6a20:3ba8:b0:19e:b6bd:f6a5 with SMTP id b40-20020a056a203ba800b0019eb6bdf6a5mr1145195pzh.45.1709634554357; Tue, 05 Mar 2024 02:29:14 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709634554; cv=pass; d=google.com; s=arc-20160816; b=S/rh/Mh3X+0POmr2rPZJOT1t7bzB+Yk0akFyKs0IHKo9MByQZkmz74cwabb8acE0kj X9AlRnvN2/5I7B/DgKgaenNz6psPPelg+q88khO1EteTSFgxRqmSiN+tw45rxZTgj+e8 Ha5Ufn2sFRSoGGUlkv+5pgw+nYfmT9a9EhJjx0iwL0DHflSRY3za8aadnXdLTaXPtqE3 q/9pX9MvuKY4ClgU8tIYB8vUJhe2NI0VKdzEohgCLs2VeX36/TkuyB+VRUtjzuIzcj/D 3CJcRyhoibD04A8ZGbGysVMqSXQ8B8NsB01DDWIycx6DSB3uazi8e2H7ah7+sZfXZkbt jK1A== 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=GV5CCe3jIX4usqcAr2fHoouIzVmRNQzTJGc3CO7pfaQ=; fh=zf0NNkRrVzzNLxoWnmq9qpFBuYVKEQmjRPQIjT6JsCY=; b=f05Y55Z31cJMVMnmyWKdXO1yxX5S3osNIY4O4Mu8cUR5gTP+3Pf1JhU8IZ3s7EN+cJ y6C1WROy1u9jz3yTif0zeQjtSSabR+iIX6czp/2+VDioe3mFxQbiz5RHwnUGkw/M00id JDEXFySTj76PsFk1KhfrZ9y/o8PqBNsN7ASyOw26mEsuM5sSF+0yIlWGbp22DDFX28XR BKNUY/y6Muwf8VZEv3O1u/0oqScxkU8v0JFLuawH9KvaoCEREuVN9JzVMcCFd5JEwVa0 jvAPn1BdlYOOqiWUYdKaz0x6YxpDSJ+CTA3gAtZ/6ZpTIOnKTrFPo8/Lr6E+gFWNBAeu 8FJQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=GL165t1g; 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-ext4+bounces-1511-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-ext4+bounces-1511-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=collabora.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id nv11-20020a17090b1b4b00b0029a370d6f78si12099792pjb.46.2024.03.05.02.29.14 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Mar 2024 02:29:14 -0800 (PST) Received-SPF: pass (google.com: domain of linux-ext4+bounces-1511-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=GL165t1g; 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-ext4+bounces-1511-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-ext4+bounces-1511-linux.lists.archive=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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 09C382812C0 for ; Tue, 5 Mar 2024 10:29:14 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 868D665BAC; Tue, 5 Mar 2024 10:16:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=collabora.com header.i=@collabora.com header.b="GL165t1g" X-Original-To: linux-ext4@vger.kernel.org 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 645C964CF6; Tue, 5 Mar 2024 10:16:36 +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=1709633798; cv=none; b=S6MulPVoG0uJr7lQGzs9hxteMPgC6OX94Tr+MzzNmKy+XydUpbADlCrqqATS9bCYozIZWQMXIJzRKOmYN3JvsxptyoCgANVn6mOcNNAGzIti6MvSknVwNz/Ma8IWPMa/DX7OoK5W+2pcHtGvcEVVIy23IjrwH1t8wfyRWngRe5w= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709633798; c=relaxed/simple; bh=hL8ihAfFpfzoj6hdBXWj+le+F9Ydulkjh3HMUPqy2Nc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=SyVXOlg6O4QIsxOg3QXufh4pX3zl6z86xh2NDEK2qk+JRYQhJ4Ey4Vs8E4u46E/sv0zrbxryq3rDAk5CdJT0Gm/pWKDrUhWmfybCGOzGH6AHMTbNSzDOyLM0yjYTxuQx9BGrZNwyw5y+j4Rk8zjCA4CTf9wQvCJngS/2qC+0HGM= 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=GL165t1g; 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=1709633794; bh=hL8ihAfFpfzoj6hdBXWj+le+F9Ydulkjh3HMUPqy2Nc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=GL165t1gPDrxVp9tLw/bauVetrhEfoqdYCOLRwI+u2mZMGzLflOEZegmJpkR82+hQ oYRH5CVym9IkGBEdx62rhmYgImxgHxqCileh8hj5xPOUbt/SbfV6urhcSkrTzgYen1 vfKPg3ucWGtizQ2FUE7uq3MF8AkwE8sQBTHgMFgd2vNrlUIn52cL8F8mI3B9ao68Ql wjEVIZY7A+WLvJADuYIn1R4WGNRlVPo6EY8c2W0wDunFTyf8VI3VwI5ZWtDVnGpYij dvoVbBfDiR4j2OSpT0EwBxmPgUH4oPBRwZkV/MZ+oeRaKOtF21GeCHiZtcLlJAZrGb 1EQZrjeP6Zy/g== Received: from eugen-station.domain.com (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 7589F37820CC; Tue, 5 Mar 2024 10:16:31 +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 Subject: [PATCH v13 3/9] libfs: Introduce case-insensitive string comparison helper Date: Tue, 5 Mar 2024 12:16:02 +0200 Message-Id: <20240305101608.67943-4-eugen.hristev@collabora.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240305101608.67943-1-eugen.hristev@collabora.com> References: <20240305101608.67943-1-eugen.hristev@collabora.com> Precedence: bulk X-Mailing-List: linux-ext4@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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. Signed-off-by: Gabriel Krisman Bertazi [eugen.hristev@collabora.com: rework to first test the exact match] Signed-off-by: Eugen Hristev --- fs/libfs.c | 81 ++++++++++++++++++++++++++++++++++++++++++++++ include/linux/fs.h | 4 +++ 2 files changed, 85 insertions(+) diff --git a/fs/libfs.c b/fs/libfs.c index c297953db948..c107c24f33b9 100644 --- a/fs/libfs.c +++ b/fs/libfs.c @@ -1776,6 +1776,87 @@ static const struct dentry_operations generic_ci_dentry_ops = { .d_revalidate = fscrypt_d_revalidate, #endif }; + +/** + * generic_ci_match() - Match a name (case-insensitively) with a dirent. + * This is a filesystem helper for comparison with directory entries. + * generic_ci_d_compare should be used in VFS' ->d_compare instead. + * + * @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 = 0; + + 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; + } + + /* + * Attempt a case-sensitive match first. It is cheaper and + * should cover most lookups, including all the sane + * applications that expect a case-sensitive filesystem. + */ + if (folded_name->name) { + if (dirent.len == folded_name->len && + !memcmp(folded_name->name, dirent.name, dirent.len)) { + match = 1; + goto out; + } + res = utf8_strncasecmp_folded(um, folded_name, &dirent); + } else { + if (dirent.len == name->len && + !memcmp(name->name, dirent.name, dirent.len) && + (!sb_has_strict_encoding(sb) || !utf8_validate(um, name))) { + match = 1; + goto out; + } + res = utf8_strncasecmp(um, name, &dirent); + } + +out: + kfree(decrypted_name.name); + if (match) /* matched by direct comparison */ + return 1; + else if (!res) /* matched by utf8 comparison */ + return 1; + else if (res < 0) /* error on utf8 comparison */ + return res; + return 0; /* no match */ +} +EXPORT_SYMBOL(generic_ci_match); #endif #ifdef CONFIG_FS_ENCRYPTION diff --git a/include/linux/fs.h b/include/linux/fs.h index ff1338109b54..690b37e1db95 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -3281,6 +3281,10 @@ extern int generic_file_fsync(struct file *, loff_t, loff_t, int); extern int generic_check_addressable(unsigned, u64); extern void generic_set_sb_d_ops(struct super_block *sb); +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) { -- 2.34.1