From: Zhan Xusheng <zhanxusheng1024@gmail.com>
To: jaegeuk@kernel.org, chao@kernel.org
Cc: ebiggers@kernel.org, krisman@kernel.org, andrealmeid@igalia.com,
qkrwngud825@gmail.com, linux-f2fs-devel@lists.sourceforge.net,
linux-kernel@vger.kernel.org, zhanxusheng@xiaomi.com
Subject: [PATCH v2] f2fs: fold casefolded names of F2FS_NAME_LEN bytes or more
Date: Tue, 17 Nov 2026 14:39:59 +0800 [thread overview]
Message-ID: <20261117063959.2925223-1-zhanxusheng1024@gmail.com> (raw)
From: Zhan Xusheng <zhanxusheng@xiaomi.com>
f2fs_init_casefolded_name() folds into an F2FS_NAME_LEN byte buffer,
but utf8_casefold() also writes a terminating NUL, and canonical
decomposition can make a name up to three times longer. A name that
folds to F2FS_NAME_LEN bytes or more is therefore not folded, and is
hashed and compared as given. In a casefolded directory, creating
'A' x 255 fails with EINVAL under utf8:strict. Without strict, after
creating 'A' x 255, stat of 'a' x 255 returns ENOENT and creating it
adds a second entry, and 'Å' x 127 (254 bytes) cannot be found as
'å' x 127.
Fold into 3 * F2FS_NAME_LEN + 1 bytes. Entries created before this
for such names are hashed by the name as given, so search linearly
for them before concluding that the name is absent.
Fixes: 43c780ba2624 ("f2fs: rework filename handling")
Signed-off-by: Zhan Xusheng <zhanxusheng@xiaomi.com>
---
v2:
- fold into 3 * F2FS_NAME_LEN + 1 bytes, as decomposition can expand
a name to three times its length (Eric)
- find entries hashed by the name as given with the existing linear
search instead of a second hashed lookup, which used the hash of the
name as typed and missed entries created with other case, could
create a duplicate that shadowed them, and was skipped during
roll-forward recovery after cf_name is freed (Sashiko)
- don't fall back after the hashed lookup failed with an error, which
the existing fallback also did not check (Sashiko)
RFC: https://lore.kernel.org/all/20261103030712.1514800-1-zhanxusheng1024@gmail.com/
Only names that fold to 255 bytes or more pay for the linear search,
on a miss, which includes every create of such a name.
Tested in a VM on f2fs/dev 9b12299b437e with lookup_mode=perf (the
default) and lookup_mode=compat, and in a directory with 3000 more
entries (depth 5), same results. Entries created by the
unpatched kernel, then looked up and created by each kernel:
unpatched patched
stat 'a' x 255 ENOENT found
touch 'a' x 255 2nd entry no new entry
'Å' x 85 as decomposed (255B) ENOENT found
'Å' x 127 as 'å' x 127 ENOENT found
utf8:strict, create 'S' x 255 EINVAL ok
utf8:strict, create U+D7A3 x 85
(folds to 765 bytes) EINVAL ok
rm + recreate 'A' x 255, fsync,
crash, roll-forward - one entry, new data
Entries created by the patched kernel are hashed by the folded name.
An unpatched kernel finds them only by their exact name and only with
lookup_mode=compat. fsck.f2fs, which folds into PATH_MAX, computes the
same hash for them, and still rewrites the hash of the old entries.
Encrypted directories are not tested.
---
fs/f2fs/dir.c | 8 +++++---
fs/f2fs/f2fs.h | 13 +++++++++++++
fs/f2fs/super.c | 2 +-
3 files changed, 19 insertions(+), 4 deletions(-)
diff --git a/fs/f2fs/dir.c b/fs/f2fs/dir.c
index 70847313ca6a..e209cdb58752 100644
--- a/fs/f2fs/dir.c
+++ b/fs/f2fs/dir.c
@@ -76,7 +76,7 @@ int f2fs_init_casefolded_name(const struct inode *dir,
return -ENOMEM;
len = utf8_casefold(sb->s_encoding, fname->usr_fname,
- buf, F2FS_NAME_LEN);
+ buf, F2FS_CF_NAME_LEN);
if (len <= 0) {
kmem_cache_free(f2fs_cf_name_slab, buf);
if (sb_has_strict_encoding(sb))
@@ -86,6 +86,7 @@ int f2fs_init_casefolded_name(const struct inode *dir,
}
fname->cf_name.name = buf;
fname->cf_name.len = len;
+ fname->cf_name_long = len >= F2FS_NAME_LEN;
}
return 0;
@@ -389,8 +390,9 @@ struct f2fs_dir_entry *__f2fs_find_entry(struct inode *dir,
out:
#if IS_ENABLED(CONFIG_UNICODE)
- if (f2fs_should_fallback_to_linear(dir) &&
- IS_CASEFOLDED(dir) && !de && use_hash) {
+ if ((f2fs_should_fallback_to_linear(dir) || fname->cf_name_long) &&
+ IS_CASEFOLDED(dir) && !de && !IS_ERR(*dentry_block) &&
+ use_hash) {
use_hash = false;
goto start_find_entry;
}
diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
index 4fa4d8042f70..d4a48ef274fd 100644
--- a/fs/f2fs/f2fs.h
+++ b/fs/f2fs/f2fs.h
@@ -661,9 +661,22 @@ struct f2fs_filename {
* we fall back to treating the name as an opaque byte sequence.
*/
struct qstr cf_name;
+ /*
+ * The casefolded name is F2FS_NAME_LEN bytes or longer. Older kernels
+ * could not fold such names and hashed them as given, so a lookup has
+ * to search linearly before it can conclude that the name is absent.
+ * This stays set after cf_name is freed.
+ */
+ bool cf_name_long;
#endif
};
+/*
+ * Canonical decomposition can make a name up to three times longer, and
+ * utf8_casefold() also writes a terminating NUL.
+ */
+#define F2FS_CF_NAME_LEN (3 * F2FS_NAME_LEN + 1)
+
struct f2fs_dentry_ptr {
struct inode *inode;
void *bitmap;
diff --git a/fs/f2fs/super.c b/fs/f2fs/super.c
index 78dbf1e92285..33fdc037d5cc 100644
--- a/fs/f2fs/super.c
+++ b/fs/f2fs/super.c
@@ -505,7 +505,7 @@ struct kmem_cache *f2fs_cf_name_slab;
static int __init f2fs_create_casefold_cache(void)
{
f2fs_cf_name_slab = f2fs_kmem_cache_create("f2fs_casefolded_name",
- F2FS_NAME_LEN);
+ F2FS_CF_NAME_LEN);
return f2fs_cf_name_slab ? 0 : -ENOMEM;
}
base-commit: 9b12299b437e0cc3ebf402e7b36a89cc69151238
--
2.43.0
reply other threads:[~2026-11-17 6:40 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261117063959.2925223-1-zhanxusheng1024@gmail.com \
--to=zhanxusheng1024@gmail.com \
--cc=andrealmeid@igalia.com \
--cc=chao@kernel.org \
--cc=ebiggers@kernel.org \
--cc=jaegeuk@kernel.org \
--cc=krisman@kernel.org \
--cc=linux-f2fs-devel@lists.sourceforge.net \
--cc=linux-kernel@vger.kernel.org \
--cc=qkrwngud825@gmail.com \
--cc=zhanxusheng@xiaomi.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®