Skip to contentSkip to conversation

f2fs: fold casefolded names of F2FS_NAME_LEN bytes or more

PATCH New Zhan Xusheng 1 participant 1 message Nov 17 F2FS file system

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 f2fs: fold casefolded names of F2FS_NAME_LEN bytes or more

From: Zhan Xusheng <[email protected]>

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")

---
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/[email protected]/

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(-)
fs/f2fs/dir.c +5 -3
index 70847313ca6a..e209cdb58752 100644
@@ -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;
 	}
fs/f2fs/f2fs.h +13 -0
index 4fa4d8042f70..d4a48ef274fd 100644
@@ -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;
fs/f2fs/super.c +1 -1
index 78dbf1e92285..33fdc037d5cc 100644
@@ -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

Signature · 1 line
2.43.0