Received: by 2002:a05:7412:cfc7:b0:fc:a2b0:25d7 with SMTP id by7csp1574773rdb; Mon, 19 Feb 2024 23:36:59 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXqSuOQb8cBJ0nO6McYHLZVOhm6RtTR/f3cN9tfk3y9ytjbUXAA7o7umByO15wOl4I0T4MWlQZd2bSLNSS9vf9H1f3L8A9/jRykl7VDMA== X-Google-Smtp-Source: AGHT+IEWFomEjxaU8fE/TlUCzP8cmHI0Dh3qs/pYeSOz0LGs5Tn7OnK0gumRSFIfmE/eK/7HgGHf X-Received: by 2002:a05:622a:1991:b0:42c:39b2:a7c5 with SMTP id u17-20020a05622a199100b0042c39b2a7c5mr22171641qtc.26.1708414619177; Mon, 19 Feb 2024 23:36:59 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708414619; cv=pass; d=google.com; s=arc-20160816; b=SOv6BG7+GK+bV/v48XjC6Ov57SYi2v2LUXq7J0iJewTP1+WOcKhTWv2Wy7Milc5qEi JCVVVQuP9vBVkgNpYr94+hxY5trrcUjlT7loDoJJ7cUBA3paz6LFK7sYHBiKpGTLN39h ANBviirOy8RmRu7cnvDBaEKBRvP9DBHsAMtASDxYgPJwlnh27v8emIEZsd0eloX+S1d4 swMi3H2XQ3BYIIR9cxbkqtrVbqegsS8vKTP3ZCCKaV+lMlrDBidEnb7wq4O4p7yJu1cw JmdRMPaXOib+OBLjWiDH+Hmw5RXzQMRy0e2skvSc/AVqSDuLXNspmuQCnlz6imccrZMV m0GQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id:dkim-signature; bh=hMg3u/lCaBQz6zZhifqyWWw+4lDjPJ6FQ66fleME1Oc=; fh=2n8gCL0uJx7XxBaHZalSAXHFuBDpZYFcVvEW8xNDKcg=; b=wlbL3NV9FC+p3w6KKhET7yJ+CLeNbT1mAGIpGnsIBaCewiniwbN4ErXBQeXcuGokh6 JPX2bpLmhvw3el1KLb/mt32odpqyjal+rbgDIHCAJ9QoTI7CUUibwH8z3bkqQmAfe5Et ObpzxZktYxr3wJk8kLffkJwWIfuGct/otMYa+38qVUwQFneWU+Ibnd851ClUwdM1Nt36 K6va2iIYTuvx8+Ru76v0sIOtR+G+gOklmncTi0yKFFkaCPmg5nez1e3TQZzhvYcuFIe5 W+csUpgXENE3L+m2k3JOKO4+7C7atQnx5v5gtMJHdxcIP01oXk828w4UYsP7pNN8asRA Boyw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=Q6A95PPJ; 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-1289-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-ext4+bounces-1289-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=collabora.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id y3-20020a05622a004300b0042dc8b116cbsi8952341qtw.133.2024.02.19.23.36.59 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 Feb 2024 23:36:59 -0800 (PST) Received-SPF: pass (google.com: domain of linux-ext4+bounces-1289-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=Q6A95PPJ; 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-1289-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-ext4+bounces-1289-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id CFBED1C22416 for ; Tue, 20 Feb 2024 07:36:58 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 754155A7A7; Tue, 20 Feb 2024 07:36:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=collabora.com header.i=@collabora.com header.b="Q6A95PPJ" 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 2A64E3D69; Tue, 20 Feb 2024 07:36:48 +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=1708414611; cv=none; b=L0hONE478cgtPixTOf3LAyjrdoK0KjlwYNR3A1cllZJm3m9hMtmDkWG+2LHKIr9KPunAue2vq+FbXTwTEgO4R50THtkb3zkcmFiYyy+sETrCMgrRukCa4CNH1TbDPEMmsroIi+tnmF71xPv1bASc/TCD/PjZXF61IyzY22DfDXU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708414611; c=relaxed/simple; bh=mFSD/wFGr7jkZ9Vc1opThnXPxIp9JYkLSf/lZISocp4=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=BBp0sQ5pmjpcMvnK2JqnXPpPR0yi7gy0eQ2gU4oDNoIEeIUBih1apoYs/Ch7n6wQY+eLE5Kw7VTRHfGZBue6X6qW8/AcP7Y1oBlvjWwWKU1suP0NbUrVDimZUiKcSBrm3NGXVkBhPcGBHuttHEYAmPVmO4Z41fHOog4Ne2Afb5M= 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=Q6A95PPJ; 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=1708414607; bh=mFSD/wFGr7jkZ9Vc1opThnXPxIp9JYkLSf/lZISocp4=; h=Date:Subject:To:Cc:References:From:In-Reply-To:From; b=Q6A95PPJBIUtHUCT2w7izYRmIV2+dv1xuwysXTdFcO1C5vMnTjfQ/RS4p2ehZvAHw N44F1r0HL3GDdqcfbhwDwni1HtKy0rfgis5dKJ2Jhetv4rB9ug0PyMVYkydhWHUeOo eUd7GHy5td1IY3uY673aSys/XKFTI+FwhIta/TAN6+ygQURwb8e8FLZwKpVYeetBih sL5LAAdDqczBL0u9hDcBB2mVK5bofxismA848yszTtdyT7PwmMC8uW+jAqNl65j6yc cm6Kcshdk74TjG86xVTQAxCExKF+wOFP59h2HGKZy5i5eFu0YwTNJZeW7TMA5cRlhy YRc105s7+QKLw== Received: from [100.90.194.27] (cola.collaboradmins.com [195.201.22.229]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 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 8BB1E37804B2; Tue, 20 Feb 2024 07:36:43 +0000 (UTC) Message-ID: Date: Tue, 20 Feb 2024 09:36:40 +0200 Precedence: bulk X-Mailing-List: linux-ext4@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v10 3/8] libfs: Introduce case-insensitive string comparison helper To: Gabriel Krisman Bertazi Cc: 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, linux-kernel@vger.kernel.org, kernel@collabora.com, viro@zeniv.linux.org.uk, brauner@kernel.org, jack@suse.cz, Gabriel Krisman Bertazi References: <20240215042654.359210-1-eugen.hristev@collabora.com> <20240215042654.359210-4-eugen.hristev@collabora.com> <87zfw0bd6y.fsf@mailhost.krisman.be> <50d2afaa-fd7e-4772-ac84-24e8994bfba8@collabora.com> <87msrwbj18.fsf@mailhost.krisman.be> Content-Language: en-US From: Eugen Hristev In-Reply-To: <87msrwbj18.fsf@mailhost.krisman.be> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit On 2/19/24 16:55, Gabriel Krisman Bertazi wrote: > Eugen Hristev writes: > >> On 2/16/24 18:12, Gabriel Krisman Bertazi wrote: >>> Eugen Hristev writes: >>> >>>> 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 | 80 ++++++++++++++++++++++++++++++++++++++++++++++ >>>> include/linux/fs.h | 4 +++ >>>> 2 files changed, 84 insertions(+) >>>> >>>> diff --git a/fs/libfs.c b/fs/libfs.c >>>> index bb18884ff20e..82871fa1b066 100644 >>>> --- a/fs/libfs.c >>>> +++ b/fs/libfs.c >>>> @@ -1773,6 +1773,86 @@ 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. >>>> + * 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. >>>> + * >>>> + * >>> >>> Since this need a respin, mind dropping the extra empty line here? >>> >>>> + * 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; >>>> + >>>> + 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. >>>> + * >>> >>> >>>> + * This comparison is safe under RCU because the caller >>>> + * guarantees the consistency between str and len. See >>>> + * __d_lookup_rcu_op_compare() for details. >>>> + */ >>> >>> This paragraph doesn't really make sense here. It is originally from >>> the d_compare hook, which can be called under RCU, but there is no RCU >>> here. Also, here we are comparing the dirent with the >>> name-under-lookup, name which is already safe. >>> >>> >>>> + if (folded_name->name) { >>>> + if (dirent.len == folded_name->len && >>>> + !memcmp(folded_name->name, dirent.name, dirent.len)) { >>>> + res = 1; >>>> + goto out; >>>> + } >>>> + res = !utf8_strncasecmp_folded(um, folded_name, &dirent); >>> >>> Hmm, second thought on this. This will ignore errors from utf8_strncasecmp*, >>> which CAN happen for the first time here, if the dirent itself is >>> corrupted on disk (exactly why we have patch 6). Yes, ext4_match will drop the >>> error, but we want to propagate it from here, such that the warning on >>> patch 6 can trigger. >>> >>> This is why I did that match dance on the original submission. Sorry >>> for suggesting it. We really want to get the error from utf8 and >>> propagate it if it is negative. basically: >>> >>> res > 0: match >>> res == 0: no match. >>> res < 0: propagate error and let the caller handle it >> >> In that case I will revert to the original v9 implementation and send a v11 to >> handle that. > > Please, note that the memcmp optimization is still valid. On match, we > know the name is valid utf8. It is just a matter of propagating the > error code from utf8 to the caller if we need to call it. > Okay, I am changing it. By the way, is this supposed to work like this on case-insensitive directories ? user@debian-rockchip-rock5b-rk3588:~$ ls -la /media/CI_dir/*cuc ls: cannot access '/media/CI_dir/*cuc': No such file or directory user@debian-rockchip-rock5b-rk3588:~$ ls -la /media/CI_dir/*CUC -rw-r--r-- 1 root root 0 Feb 12 17:47 /media/CI_dir/CUC user@debian-rockchip-rock5b-rk3588:~$ ls -la /media/CI_dir/cuc -rw-r--r-- 1 root root 0 Feb 12 17:47 /media/CI_dir/cuc user@debian-rockchip-rock5b-rk3588:~$ basically wildcards don't work. Thanks, Eugen