Re: [RFC PATCH] f2fs: fold casefolded names of F2FS_NAME_LEN bytes
From: Eric Biggers
Date: Mon Nov 16 2026 - 14:48:52 EST
[ +Cc Gabriel, who maintains fs/unicode/ ]
On Tue, Nov 03, 2026 at 11:07:12AM +0800, Zhan Xusheng wrote:
> From: Zhan Xusheng <zhanxusheng@xxxxxxxxxx>
>
> 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@xxxxxxxxxx>
> ---
> 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