From patchwork Mon Sep 2 22:55:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Andr=C3=A9_Almeida?= X-Patchwork-Id: 13787775 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 85473CA0ED3 for ; Mon, 2 Sep 2024 22:55:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1B0D08D0117; Mon, 2 Sep 2024 18:55:51 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 1614B6B03DC; Mon, 2 Sep 2024 18:55:51 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0026C8D0117; Mon, 2 Sep 2024 18:55:50 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id D27E26B03DB for ; Mon, 2 Sep 2024 18:55:50 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 5129F401F0 for ; Mon, 2 Sep 2024 22:55:50 +0000 (UTC) X-FDA: 82521307260.10.8C8D6C8 Received: from fanzine2.igalia.com (fanzine.igalia.com [178.60.130.6]) by imf09.hostedemail.com (Postfix) with ESMTP id 885EC14000C for ; Mon, 2 Sep 2024 22:55:48 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=igalia.com header.s=20170329 header.b=SCwErnN1; spf=pass (imf09.hostedemail.com: domain of andrealmeid@igalia.com designates 178.60.130.6 as permitted sender) smtp.mailfrom=andrealmeid@igalia.com; dmarc=pass (policy=none) header.from=igalia.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1725317725; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=MIHofFtxayC4m6Wwh0Y/aHwAJtRNJjHOKhG4ip79qTs=; b=QogsAEssPL86ufOJDmBXP6O+9R8tDNtYQ5DGKsvAr8JlaSo40WnmLIl3mkvv/kM63+OUR6 +ry3plxf6g3ODpvtsy4nl9i57PiaDfQ46OfgU8o8tMRdXpdB47J88fGqnEdUR9jbG/p38G p7LiB6TvWah67dQaAIAzZNQx2pMddN4= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=igalia.com header.s=20170329 header.b=SCwErnN1; spf=pass (imf09.hostedemail.com: domain of andrealmeid@igalia.com designates 178.60.130.6 as permitted sender) smtp.mailfrom=andrealmeid@igalia.com; dmarc=pass (policy=none) header.from=igalia.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1725317725; a=rsa-sha256; cv=none; b=gudk9SR5xOd0YSMpuvJeJqzEEjIfwORY4QWd7ZHXiWVP5Bq9+boogIFrbpXxxJ2Q9vUyn4 pXzWjlb+ulrwDXISBGeQ/E8yFFJBhepn+e5pb8Z69Mch9lDVAoKndJ6yo5Es+L9gmhYotL 3+RluaQhxnZd4M6bda7eF3OqeR4OaCw= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=Content-Transfer-Encoding:Content-Type:MIME-Version:References: In-Reply-To:Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=MIHofFtxayC4m6Wwh0Y/aHwAJtRNJjHOKhG4ip79qTs=; b=SCwErnN1HCVhCKEx6e3mD5Th7s +DrdbC/khwxG0T3+am2/iYUCjanHt+ea8KOMESeyYR/G38RTPLa/xZKtYJ9/7mXJR+aVUi++xhHJm nuj8rm9HQbJyjS2uij9IKoFLoTLtgLnNDa+/xC+ES51AESVYAmewtkhcW3DeUt4MSCkZBQpV6u0aV EHfIHZ/g1Aaw/0amDX8L9b0Er5xN/hGgQBSSdFPzqTN+vA2lWjBIWZemPcXp+ITE6V82dxg8rNIMZ Oes8GI7vEt8WWMXGVT/+xURGP4W3QZoaZL3RHromEGq9a6yjBEWHdXvxw99a3lpgKMicxZJ/VKgXy HmUL3Q2A==; Received: from [177.172.122.98] (helo=localhost.localdomain) by fanzine2.igalia.com with esmtpsa (Cipher TLS1.3:ECDHE_X25519__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim) id 1slFxU-008VrL-Bi; Tue, 03 Sep 2024 00:55:40 +0200 From: =?utf-8?q?Andr=C3=A9_Almeida?= To: Hugh Dickins , Andrew Morton , Alexander Viro , Christian Brauner , Jan Kara , krisman@kernel.org Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, kernel-dev@igalia.com, Daniel Rosenberg , smcv@collabora.com, Christoph Hellwig , =?utf-8?q?Andr=C3=A9_Almeida?= Subject: [PATCH v2 5/8] tmpfs: Add casefold lookup support Date: Mon, 2 Sep 2024 19:55:07 -0300 Message-ID: <20240902225511.757831-6-andrealmeid@igalia.com> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20240902225511.757831-1-andrealmeid@igalia.com> References: <20240902225511.757831-1-andrealmeid@igalia.com> MIME-Version: 1.0 X-Rspam-User: X-Stat-Signature: cj8wk5bfqytsuzwajndhqyzwgixxxxmi X-Rspamd-Queue-Id: 885EC14000C X-Rspamd-Server: rspam11 X-HE-Tag: 1725317748-196178 X-HE-Meta: U2FsdGVkX1+fvsFdg3syN26M6HK4RoTIpWgcIAB/a6yplR0HHCqhfd17aRWQM5AZLwF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/bCHWJKQo17L3lCNbJ6kTXIPGcc8US4N6jtVXs08VuXkuQFQNEU/9PkIrFD05agu+2J9jxPyWxUm9DFACLATZxmQib9guWOul4t/82KehIOeipVKiXOQPbiuZ7deauyNobzW5FNzl6WijP4Tig== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Enable casefold lookup in tmpfs, based on the enconding defined by userspace. That means that instead of comparing byte per byte a file name, it compares to a case-insensitive equivalent of the Unicode string. * Dcache handling There's a special need when dealing with case-insensitive dentries. First of all, we currently invalidated every negative casefold dentries. That happens because currently VFS code has no proper support to deal with that, giving that it could incorrectly reuse a previous filename for a new file that has a casefold match. For instance, this could happen: $ mkdir DIR $ rm -r DIR $ mkdir dir $ ls DIR/ And would be perceived as inconsistency from userspace point of view, because even that we match files in a case-insensitive manner, we still honor whatever is the initial filename. Along with that, tmpfs stores only the first equivalent name dentry used in the dcache, preventing duplications of dentries in the dcache. The d_compare() version for casefold files uses a normalized string, so the filename under lookup will be compared to another normalized string for the existing file, achieving a casefolded lookup. * Enabling casefold via mount options Most filesystems have their data stored in disk, so casefold option need to be enabled when building a filesystem on a device (via mkfs). However, as tmpfs is a RAM backed filesystem, there's no disk information and thus no mkfs to store information about casefold. For tmpfs, create casefold options for mounting. Userspace can then enable casefold support for a mount point using: $ mount -t tmpfs -o casefold=utf8-12.1.0 fs_name mount_dir/ Userspace must set what Unicode standard is aiming to. The available options depends on what the kernel Unicode subsystem supports. And for strict encoding: $ mount -t tmpfs -o casefold=utf8-12.1.0,strict_encoding fs_name mount_dir/ Strict encoding means that tmpfs will refuse to create invalid UTF-8 sequences. When this option is not enabled, any invalid sequence will be treated as an opaque byte sequence, ignoring the encoding thus not being able to be looked up in a case-insensitive way. Signed-off-by: André Almeida --- mm/shmem.c | 115 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 111 insertions(+), 4 deletions(-) diff --git a/mm/shmem.c b/mm/shmem.c index 5a77acf6ac6a..0f918010bc54 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -40,6 +40,8 @@ #include #include #include +#include +#include #include "swap.h" static struct vfsmount *shm_mnt __ro_after_init; @@ -123,6 +125,8 @@ struct shmem_options { bool noswap; unsigned short quota_types; struct shmem_quota_limits qlimits; + struct unicode_map *encoding; + bool strict_encoding; #define SHMEM_SEEN_BLOCKS 1 #define SHMEM_SEEN_INODES 2 #define SHMEM_SEEN_HUGE 4 @@ -3427,6 +3431,11 @@ shmem_mknod(struct mnt_idmap *idmap, struct inode *dir, if (IS_ERR(inode)) return PTR_ERR(inode); +#if IS_ENABLED(CONFIG_UNICODE) + if (!utf8_check_strict_name(dir, &dentry->d_name)) + return -EINVAL; +#endif + error = simple_acl_create(dir, inode); if (error) goto out_iput; @@ -3442,7 +3451,12 @@ shmem_mknod(struct mnt_idmap *idmap, struct inode *dir, dir->i_size += BOGO_DIRENT_SIZE; inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir)); inode_inc_iversion(dir); - d_instantiate(dentry, inode); + + if (IS_CASEFOLDED(dir)) + d_add(dentry, inode); + else + d_instantiate(dentry, inode); + dget(dentry); /* Extra count - pin the dentry in core */ return error; @@ -3533,7 +3547,10 @@ static int shmem_link(struct dentry *old_dentry, struct inode *dir, inc_nlink(inode); ihold(inode); /* New dentry reference */ dget(dentry); /* Extra pinning count for the created dentry */ - d_instantiate(dentry, inode); + if (IS_CASEFOLDED(dir)) + d_add(dentry, inode); + else + d_instantiate(dentry, inode); out: return ret; } @@ -3553,6 +3570,14 @@ static int shmem_unlink(struct inode *dir, struct dentry *dentry) inode_inc_iversion(dir); drop_nlink(inode); dput(dentry); /* Undo the count from "create" - does all the work */ + + /* + * For now, VFS can't deal with case-insensitive negative dentries, so + * we invalidate them + */ + if (IS_CASEFOLDED(dir)) + d_invalidate(dentry); + return 0; } @@ -3697,7 +3722,10 @@ static int shmem_symlink(struct mnt_idmap *idmap, struct inode *dir, dir->i_size += BOGO_DIRENT_SIZE; inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir)); inode_inc_iversion(dir); - d_instantiate(dentry, inode); + if (IS_CASEFOLDED(dir)) + d_add(dentry, inode); + else + d_instantiate(dentry, inode); dget(dentry); return 0; @@ -4050,6 +4078,8 @@ enum shmem_param { Opt_usrquota_inode_hardlimit, Opt_grpquota_block_hardlimit, Opt_grpquota_inode_hardlimit, + Opt_casefold, + Opt_strict_encoding, }; static const struct constant_table shmem_param_enums_huge[] = { @@ -4081,9 +4111,47 @@ const struct fs_parameter_spec shmem_fs_parameters[] = { fsparam_string("grpquota_block_hardlimit", Opt_grpquota_block_hardlimit), fsparam_string("grpquota_inode_hardlimit", Opt_grpquota_inode_hardlimit), #endif + fsparam_string("casefold", Opt_casefold), + fsparam_flag ("strict_encoding", Opt_strict_encoding), {} }; +#if IS_ENABLED(CONFIG_UNICODE) +static int shmem_parse_opt_casefold(struct fs_context *fc, struct fs_parameter *param) +{ + struct shmem_options *ctx = fc->fs_private; + unsigned int maj = 0, min = 0, rev = 0, version_number; + char version[10]; + int ret; + struct unicode_map *encoding; + + if (strncmp(param->string, "utf8-", 5)) + return invalfc(fc, "Only utf8 encondings are supported"); + ret = strscpy(version, param->string + 5, sizeof(version)); + if (ret < 0) + return invalfc(fc, "Invalid enconding argument: %s", + param->string); + + ret = utf8_parse_version(version, &maj, &min, &rev); + if (ret) + return invalfc(fc, "Invalid utf8 version: %s", version); + version_number = UNICODE_AGE(maj, min, rev); + encoding = utf8_load(version_number); + if (IS_ERR(encoding)) + return invalfc(fc, "Invalid utf8 version: %s", version); + pr_info("tmpfs: Using encoding provided by mount options: %s\n", + param->string); + ctx->encoding = encoding; + + return 0; +} +#else +static int shmem_parse_opt_casefold(struct fs_context *fc, struct fs_parameter *param) +{ + return invalfc(fc, "tmpfs: No kernel support for casefold filesystems\n"); +} +#endif + static int shmem_parse_one(struct fs_context *fc, struct fs_parameter *param) { struct shmem_options *ctx = fc->fs_private; @@ -4242,6 +4310,11 @@ static int shmem_parse_one(struct fs_context *fc, struct fs_parameter *param) "Group quota inode hardlimit too large."); ctx->qlimits.grpquota_ihardlimit = size; break; + case Opt_casefold: + return shmem_parse_opt_casefold(fc, param); + case Opt_strict_encoding: + ctx->strict_encoding = true; + break; } return 0; @@ -4471,6 +4544,11 @@ static void shmem_put_super(struct super_block *sb) { struct shmem_sb_info *sbinfo = SHMEM_SB(sb); +#if IS_ENABLED(CONFIG_UNICODE) + if (sb->s_encoding) + utf8_unload(sb->s_encoding); +#endif + #ifdef CONFIG_TMPFS_QUOTA shmem_disable_quotas(sb); #endif @@ -4515,6 +4593,16 @@ static int shmem_fill_super(struct super_block *sb, struct fs_context *fc) } sb->s_export_op = &shmem_export_ops; sb->s_flags |= SB_NOSEC | SB_I_VERSION; + +#if IS_ENABLED(CONFIG_UNICODE) + if (ctx->encoding) { + sb->s_encoding = ctx->encoding; + generic_set_sb_d_ops(sb); + if (ctx->strict_encoding) + sb->s_encoding_flags = SB_ENC_STRICT_MODE_FL; + } +#endif + #else sb->s_flags |= SB_NOUSER; #endif @@ -4704,11 +4792,28 @@ static const struct inode_operations shmem_inode_operations = { #endif }; +static struct dentry *shmem_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags) +{ + if (dentry->d_name.len > NAME_MAX) + return ERR_PTR(-ENAMETOOLONG); + + /* + * For now, VFS can't deal with case-insensitive negative dentries, so + * we prevent them from being created + */ + if (IS_CASEFOLDED(dir)) + return NULL; + + d_add(dentry, NULL); + + return NULL; +} + static const struct inode_operations shmem_dir_inode_operations = { #ifdef CONFIG_TMPFS .getattr = shmem_getattr, .create = shmem_create, - .lookup = simple_lookup, + .lookup = shmem_lookup, .link = shmem_link, .unlink = shmem_unlink, .symlink = shmem_symlink, @@ -4791,6 +4896,8 @@ int shmem_init_fs_context(struct fs_context *fc) ctx->uid = current_fsuid(); ctx->gid = current_fsgid(); + ctx->encoding = NULL; + fc->fs_private = ctx; fc->ops = &shmem_fs_context_ops; return 0;