The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [RFC PATCH] f2fs: fold casefolded names of F2FS_NAME_LEN bytes
@ 2026-11-03  3:07 Zhan Xusheng
  2026-11-16 12:21 ` Zhan Xusheng
  2026-11-16 19:48 ` Eric Biggers
  0 siblings, 2 replies; 4+ messages in thread
From: Zhan Xusheng @ 2026-11-03  3:07 UTC (permalink / raw)
  To: jaegeuk, chao
  Cc: linux-f2fs-devel, linux-kernel, ebiggers, andrealmeid,
	qkrwngud825, zhanxusheng

From: Zhan Xusheng <zhanxusheng@xiaomi.com>

utf8_casefold() needs room for the terminating NUL, but
f2fs_init_casefolded_name() gives it only F2FS_NAME_LEN bytes, so a
name that folds to exactly 255 bytes is not folded. 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.

Fold into F2FS_NAME_LEN + 1 bytes. Existing entries for these names
are hashed by the name as given, so on a miss also try that hash.

Fixes: 43c780ba2624 ("f2fs: rework filename handling")
Signed-off-by: Zhan Xusheng <zhanxusheng@xiaomi.com>
---
RFC because new entries for these names get a different hash on disk.
Results in a VM, with dentries dropped before each stat:

                                 'A'x255  'a'x255  'C'x255  'c'x255
  current kernel creates 'A'        ok    ENOENT
  this patch creates 'C', 'c'       ok    ENOENT     ok       ok
                                    (one 'c' entry)
  back on the current kernel        ok    ENOENT   ENOENT   ENOENT
  strict, current kernel        create 'S' x 255: EINVAL
  strict, this patch            create ok, found in both cases

Each fsck.f2fs version matches only one side. 1.16 folds into PATH_MAX
and rewrote 'A' to the folded hash, after which the current kernel
cannot find it. The dev branch rewrote 'C' to the raw hash, after
which this patch finds it only as 'C'. fsck would need to accept both
hashes for these names. Encrypted directories are not tested.

Is the second lookup acceptable? The alternative is to keep hashing
these names as given, which fixes the EINVAL but leaves them
case-sensitive.

 fs/f2fs/dir.c   | 21 ++++++++++++++++++++-
 fs/f2fs/super.c |  2 +-
 2 files changed, 21 insertions(+), 2 deletions(-)

diff --git a/fs/f2fs/dir.c b/fs/f2fs/dir.c
index 70847313ca6a..05ae0fa2e22b 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_NAME_LEN + 1);
 		if (len <= 0) {
 			kmem_cache_free(f2fs_cf_name_slab, buf);
 			if (sb_has_strict_encoding(sb))
@@ -359,6 +359,10 @@ struct f2fs_dir_entry *__f2fs_find_entry(struct inode *dir,
 	unsigned int max_depth;
 	unsigned int level;
 	bool use_hash = true;
+#if IS_ENABLED(CONFIG_UNICODE)
+	struct f2fs_filename raw;
+	bool raw_tried = false;
+#endif
 
 	*dentry_block = NULL;
 
@@ -389,6 +393,21 @@ struct f2fs_dir_entry *__f2fs_find_entry(struct inode *dir,
 
 out:
 #if IS_ENABLED(CONFIG_UNICODE)
+	/*
+	 * Entries for names that fold to F2FS_NAME_LEN bytes used to be
+	 * hashed by the name as given.  Try that hash too.
+	 */
+	if (!de && !IS_ERR(*dentry_block) && use_hash && !raw_tried &&
+	    IS_CASEFOLDED(dir) && fname->cf_name.name &&
+	    fname->cf_name.len == F2FS_NAME_LEN) {
+		raw = *fname;
+		raw.cf_name.name = NULL;
+		f2fs_hash_filename(dir, &raw);
+		raw.cf_name = fname->cf_name;
+		fname = &raw;
+		raw_tried = true;
+		goto start_find_entry;
+	}
 	if (f2fs_should_fallback_to_linear(dir) &&
 		IS_CASEFOLDED(dir) && !de && use_hash) {
 		use_hash = false;
diff --git a/fs/f2fs/super.c b/fs/f2fs/super.c
index 78dbf1e92285..4172429da862 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_NAME_LEN + 1);
 	return f2fs_cf_name_slab ? 0 : -ENOMEM;
 }
 
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [RFC PATCH] f2fs: fold casefolded names of F2FS_NAME_LEN bytes
  2026-11-03  3:07 [RFC PATCH] f2fs: fold casefolded names of F2FS_NAME_LEN bytes Zhan Xusheng
@ 2026-11-16 12:21 ` Zhan Xusheng
  2026-11-16 19:48 ` Eric Biggers
  1 sibling, 0 replies; 4+ messages in thread
From: Zhan Xusheng @ 2026-11-16 12:21 UTC (permalink / raw)
  To: jaegeuk, chao
  Cc: ebiggers, andrealmeid, qkrwngud825, linux-f2fs-devel,
	linux-kernel, zhanxusheng

Hi Jaegeuk, Chao,

Gentle ping.  The open question is the hash change: a new entry for
such a name is hashed by its folded form, so a kernel without this
patch cannot find it ("back on the current kernel" above).  Is that
acceptable?

Thanks,
Zhan Xusheng

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [RFC PATCH] f2fs: fold casefolded names of F2FS_NAME_LEN bytes
  2026-11-03  3:07 [RFC PATCH] f2fs: fold casefolded names of F2FS_NAME_LEN bytes Zhan Xusheng
  2026-11-16 12:21 ` Zhan Xusheng
@ 2026-11-16 19:48 ` Eric Biggers
  2026-11-16 20:17   ` Gabriel Krisman Bertazi
  1 sibling, 1 reply; 4+ messages in thread
From: Eric Biggers @ 2026-11-16 19:48 UTC (permalink / raw)
  To: Zhan Xusheng
  Cc: jaegeuk, chao, linux-f2fs-devel, linux-kernel, andrealmeid,
	qkrwngud825, zhanxusheng, Gabriel Krisman Bertazi

[ +Cc Gabriel, who maintains fs/unicode/ ]

On Tue, Nov 03, 2026 at 11:07:12AM +0800, Zhan Xusheng wrote:
> From: Zhan Xusheng <zhanxusheng@xiaomi.com>
> 
> utf8_casefold() needs room for the terminating NUL, but
> f2fs_init_casefolded_name() gives it only F2FS_NAME_LEN bytes, so a
> name that folds to exactly 255 bytes is not folded. 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.
> 
> Fold into F2FS_NAME_LEN + 1 bytes. Existing entries for these names
> are hashed by the name as given, so on a miss also try that hash.
> 
> Fixes: 43c780ba2624 ("f2fs: rework filename handling")
> Signed-off-by: Zhan Xusheng <zhanxusheng@xiaomi.com>
> ---
> RFC because new entries for these names get a different hash on disk.
> Results in a VM, with dentries dropped before each stat:
> 
>                                  'A'x255  'a'x255  'C'x255  'c'x255
>   current kernel creates 'A'        ok    ENOENT
>   this patch creates 'C', 'c'       ok    ENOENT     ok       ok
>                                     (one 'c' entry)
>   back on the current kernel        ok    ENOENT   ENOENT   ENOENT
>   strict, current kernel        create 'S' x 255: EINVAL
>   strict, this patch            create ok, found in both cases
> 
> Each fsck.f2fs version matches only one side. 1.16 folds into PATH_MAX
> and rewrote 'A' to the folded hash, after which the current kernel
> cannot find it. The dev branch rewrote 'C' to the raw hash, after
> which this patch finds it only as 'C'. fsck would need to accept both
> hashes for these names. Encrypted directories are not tested.
> 
> Is the second lookup acceptable? The alternative is to keep hashing
> these names as given, which fixes the EINVAL but leaves them
> case-sensitive.

Sorry about this bug!

I think it's actually worse than you say, and this commit doesn't fix it
entirely.  A filename can "casefold" (really NFD canonical decomposition
+ Default_Ignorable_Code_Point stripping + full casefolding) to much
more than F2FS_NAME_LEN bytes.  The decomposition stage in particular
can result in significant expansion, up to 3 * F2FS_NAME_LEN.

For ASCII though it is indeed as you say: they cannot expand, and only
max-length filenames are affected due to the off-by-one caused by
utf8_casefold() unexpectedly including a NUL terminator.

I think this shows that the 'casefold' feature` isn't really used for
anything other than ASCII in practice, and all the complexity of Unicode
normalization wasn't needed in the first place...

But I suppose this should be fixed anyway.  The approach of this patch
should generally be okay if you increase the buffer to the required
'3 * F2FS_NAME_LEN + 1'.

However, please check the Sashiko comments
https://sashiko.dev/#/patchset/20261103030712.1514800-1-zhanxusheng1024%40gmail.com
as some issues were found with the implementation.

- Eric

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [RFC PATCH] f2fs: fold casefolded names of F2FS_NAME_LEN bytes
  2026-11-16 19:48 ` Eric Biggers
@ 2026-11-16 20:17   ` Gabriel Krisman Bertazi
  0 siblings, 0 replies; 4+ messages in thread
From: Gabriel Krisman Bertazi @ 2026-11-16 20:17 UTC (permalink / raw)
  To: Eric Biggers, Zhan Xusheng
  Cc: jaegeuk, chao, linux-f2fs-devel, linux-kernel, andrealmeid,
	qkrwngud825, zhanxusheng

Eric Biggers <ebiggers@kernel.org> writes:

> [ +Cc Gabriel, who maintains fs/unicode/ ]
>
> On Tue, Nov 03, 2026 at 11:07:12AM +0800, Zhan Xusheng wrote:
>> From: Zhan Xusheng <zhanxusheng@xiaomi.com>
>> 
>> utf8_casefold() needs room for the terminating NUL, but
>> f2fs_init_casefolded_name() gives it only F2FS_NAME_LEN bytes, so a
>> name that folds to exactly 255 bytes is not folded. 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.
>> 
>> Fold into F2FS_NAME_LEN + 1 bytes. Existing entries for these names
>> are hashed by the name as given, so on a miss also try that hash.
>> 
>> Fixes: 43c780ba2624 ("f2fs: rework filename handling")
>> Signed-off-by: Zhan Xusheng <zhanxusheng@xiaomi.com>
>> ---
>> RFC because new entries for these names get a different hash on disk.
>> Results in a VM, with dentries dropped before each stat:
>> 
>>                                  'A'x255  'a'x255  'C'x255  'c'x255
>>   current kernel creates 'A'        ok    ENOENT
>>   this patch creates 'C', 'c'       ok    ENOENT     ok       ok
>>                                     (one 'c' entry)
>>   back on the current kernel        ok    ENOENT   ENOENT   ENOENT
>>   strict, current kernel        create 'S' x 255: EINVAL
>>   strict, this patch            create ok, found in both cases
>> 
>> Each fsck.f2fs version matches only one side. 1.16 folds into PATH_MAX
>> and rewrote 'A' to the folded hash, after which the current kernel
>> cannot find it. The dev branch rewrote 'C' to the raw hash, after
>> which this patch finds it only as 'C'. fsck would need to accept both
>> hashes for these names. Encrypted directories are not tested.
>> 
>> Is the second lookup acceptable? The alternative is to keep hashing
>> these names as given, which fixes the EINVAL but leaves them
>> case-sensitive.
>
> Sorry about this bug!
>
> I think it's actually worse than you say, and this commit doesn't fix it
> entirely.  A filename can "casefold" (really NFD canonical decomposition
> + Default_Ignorable_Code_Point stripping + full casefolding) to much
> more than F2FS_NAME_LEN bytes.  The decomposition stage in particular
> can result in significant expansion, up to 3 * F2FS_NAME_LEN.

This was a known limitation, the actual limit for casefolded names is
much less than 255, it is actually the size of the folded version of the
string.  I still suspect this limit is hardly a real problem in
practice.  The bug is the inconsistency in creating a casefolded dentry
and then failing to hash the same dentry. That's a bug in the
implementation.

> For ASCII though it is indeed as you say: they cannot expand, and only
> max-length filenames are affected due to the off-by-one caused by
> utf8_casefold() unexpectedly including a NUL terminator.
>
> I think this shows that the 'casefold' feature` isn't really used for
> anything other than ASCII in practice, and all the complexity of Unicode
> normalization wasn't needed in the first place...

I wish. We can debate whether it is a "good" usecase or not all day, but
from prior bug reports it is clear people are using casefolding way
beyond ASCII or even accented characters.  For an extreme example, see
the variant selectors mess from when we tried to change the casefolding
of ignorable code-points a couple years ago.  Clearly there were
mistakes in supporting the entire unicode map, but ASCII if far from
enough for anyone who speaks !english.

> But I suppose this should be fixed anyway.  The approach of this patch
> should generally be okay if you increase the buffer to the required
> '3 * F2FS_NAME_LEN + 1'.
>
> However, please check the Sashiko comments
> https://sashiko.dev/#/patchset/20261103030712.1514800-1-zhanxusheng1024%40gmail.com
> as some issues were found with the implementation.
>
> - Eric

-- 
Gabriel Krisman Bertazi

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-11-16 20:17 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-03  3:07 [RFC PATCH] f2fs: fold casefolded names of F2FS_NAME_LEN bytes Zhan Xusheng
2026-11-16 12:21 ` Zhan Xusheng
2026-11-16 19:48 ` Eric Biggers
2026-11-16 20:17   ` Gabriel Krisman Bertazi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox