2022-04-29 21:04:27

by Gabriel Krisman Bertazi

[permalink] [raw]
Subject: [PATCH v3 0/7] Clean up the case-insenstive lookup path

This is the v3 of this series. It fixes a build error when building
filesystems as a module on the previous series.

* v2

This is the v2 of this series. it applies Eric's comments and extend
the series to complete the merge of generic_ci_match for ext4 and f2fs.

* Original cover letter

The case-insensitive implementations in f2fs and ext4 have quite a bit
of duplicated code. This series simplifies the ext4 version, with the
goal of extracting ext4_ci_compare into a helper library that can be
used by both filesystems.

While there, I noticed we can leverage the utf8 functions to detect
encoded names that are corrupted in the filesystem. The final patch
adds an ext4 error on that scenario, to mark the filesystem as
corrupted.

This series survived passes of xfstests -g quick.

Gabriel Krisman Bertazi (7):
ext4: Match the f2fs ci_compare implementation
ext4: Simplify the handling of cached insensitive names
ext4: Implement ci comparison using unicode_name
ext4: Simplify hash check on ext4_match
ext4: Log error when lookup of encoded dentry fails
ext4: Move ext4_match_ci into libfs
f2fs: Reuse generic_ci_match for ci comparisons

fs/ext4/ext4.h | 2 +-
fs/ext4/namei.c | 110 ++++++++++++++-------------------------------
fs/f2fs/dir.c | 58 ++----------------------
fs/libfs.c | 61 +++++++++++++++++++++++++
include/linux/fs.h | 8 ++++
5 files changed, 107 insertions(+), 132 deletions(-)

--
2.35.1


2022-04-29 22:00:29

by Gabriel Krisman Bertazi

[permalink] [raw]
Subject: [PATCH v3 6/7] ext4: Move ext4_match_ci into libfs

Matching case-insensitive names is a generic operation and can be shared
with f2fs. Move it next to the rest of the shared casefold fs code.

Signed-off-by: Gabriel Krisman Bertazi <[email protected]>
---
fs/ext4/namei.c | 62 +---------------------------------------------
fs/libfs.c | 61 +++++++++++++++++++++++++++++++++++++++++++++
include/linux/fs.h | 3 +++
3 files changed, 65 insertions(+), 61 deletions(-)

diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c
index d53c8d101099..df44ea626fad 100644
--- a/fs/ext4/namei.c
+++ b/fs/ext4/namei.c
@@ -1318,66 +1318,6 @@ static void dx_insert_block(struct dx_frame *frame, u32 hash, ext4_lblk_t block)
}

#if IS_ENABLED(CONFIG_UNICODE)
-/**
- * ext4_match_ci() - Match (case-insensitive) a name with a dirent.
- * @parent: Inode of the parent of the dentry.
- * @uname: name under lookup.
- * @de_name: Dirent name.
- * @de_name_len: dirent name length.
- *
- * Test whether a case-insensitive directory entry matches the filename
- * being searched.
- *
- * Return: > 0 if the directory entry matches, 0 if it doesn't match, or
- * < 0 on error.
- */
-static int ext4_match_ci(const struct inode *parent,
- const struct unicode_name *uname,
- u8 *de_name, size_t de_name_len)
-{
- const struct super_block *sb = parent->i_sb;
- const struct unicode_map *um = sb->s_encoding;
- struct fscrypt_str decrypted_name = FSTR_INIT(NULL, de_name_len);
- struct qstr entry = QSTR_INIT(de_name, de_name_len);
- int ret, match = false;
-
- if (IS_ENCRYPTED(parent)) {
- const struct fscrypt_str encrypted_name =
- FSTR_INIT(de_name, de_name_len);
-
- decrypted_name.name = kmalloc(de_name_len, GFP_KERNEL);
- if (!decrypted_name.name)
- return -ENOMEM;
- ret = fscrypt_fname_disk_to_usr(parent, 0, 0, &encrypted_name,
- &decrypted_name);
- if (ret < 0)
- goto out;
- entry.name = decrypted_name.name;
- entry.len = decrypted_name.len;
- }
-
- if (uname->folded_name->name)
- ret = utf8_strncasecmp_folded(um, uname->folded_name, &entry);
- else
- ret = utf8_strncasecmp(um, uname->usr_name, &entry);
-
- if (!ret)
- match = true;
- else if (ret < 0 && !sb_has_strict_encoding(sb)) {
- /*
- * In non-strict mode, fallback to a byte comparison if
- * the names have invalid characters.
- */
- ret = 0;
- match = ((uname->usr_name->len == entry.len) &&
- !memcmp(uname->usr_name->name, entry.name, entry.len));
- }
-
-out:
- kfree(decrypted_name.name);
- return (ret >= 0) ? match : ret;
-}
-
int ext4_fname_setup_ci_filename(struct inode *dir, const struct qstr *iname,
struct ext4_filename *name)
{
@@ -1450,7 +1390,7 @@ static bool ext4_match(struct inode *parent,
u.folded_name = &fname->cf_name;
u.usr_name = fname->usr_fname;

- ret = ext4_match_ci(parent, &u, de->name, de->name_len);
+ ret = generic_ci_match(parent, &u, de->name, de->name_len);
if (ret < 0) {
/*
* Treat comparison errors as not a match. The
diff --git a/fs/libfs.c b/fs/libfs.c
index 974125270a42..c14b3fa615f5 100644
--- a/fs/libfs.c
+++ b/fs/libfs.c
@@ -1465,6 +1465,67 @@ static const struct dentry_operations generic_ci_dentry_ops = {
.d_hash = generic_ci_d_hash,
.d_compare = generic_ci_d_compare,
};
+
+/**
+ * generic_ci_match() - Match (case-insensitive) a name with a dirent.
+ * @parent: Inode of the parent of the dentry.
+ * @uname: name under lookup.
+ * @de_name: Dirent name.
+ * @de_name_len: dirent name length.
+ *
+ * Test whether a case-insensitive directory entry matches the filename
+ * being searched.
+ *
+ * Return: > 0 if the directory entry matches, 0 if it doesn't match, or
+ * < 0 on error.
+ */
+int generic_ci_match(const struct inode *parent,
+ const struct unicode_name *uname,
+ u8 *de_name, size_t de_name_len)
+{
+ const struct super_block *sb = parent->i_sb;
+ const struct unicode_map *um = sb->s_encoding;
+ struct fscrypt_str decrypted_name = FSTR_INIT(NULL, de_name_len);
+ struct qstr entry = QSTR_INIT(de_name, de_name_len);
+ int ret, match = false;
+
+ if (IS_ENCRYPTED(parent)) {
+ const struct fscrypt_str encrypted_name =
+ FSTR_INIT(de_name, de_name_len);
+
+ decrypted_name.name = kmalloc(de_name_len, GFP_KERNEL);
+ if (!decrypted_name.name)
+ return -ENOMEM;
+ ret = fscrypt_fname_disk_to_usr(parent, 0, 0, &encrypted_name,
+ &decrypted_name);
+ if (ret < 0)
+ goto out;
+ entry.name = decrypted_name.name;
+ entry.len = decrypted_name.len;
+ }
+
+ if (uname->folded_name->name)
+ ret = utf8_strncasecmp_folded(um, uname->folded_name, &entry);
+ else
+ ret = utf8_strncasecmp(um, uname->usr_name, &entry);
+
+ if (!ret)
+ match = true;
+ else if (ret < 0 && !sb_has_strict_encoding(sb)) {
+ /*
+ * In non-strict mode, fallback to a byte comparison if
+ * the names have invalid characters.
+ */
+ ret = 0;
+ match = ((uname->usr_name->len == entry.len) &&
+ !memcmp(uname->usr_name->name, entry.name, entry.len));
+ }
+
+out:
+ kfree(decrypted_name.name);
+ return (ret >= 0) ? match : ret;
+}
+EXPORT_SYMBOL(generic_ci_match);
#endif

#ifdef CONFIG_FS_ENCRYPTION
diff --git a/include/linux/fs.h b/include/linux/fs.h
index 3f76a18a5f40..6a750b8704c9 100644
--- a/include/linux/fs.h
+++ b/include/linux/fs.h
@@ -3364,6 +3364,9 @@ struct unicode_name {
};

extern void generic_set_encrypted_ci_d_ops(struct dentry *dentry);
+extern int generic_ci_match(const struct inode *parent,
+ const struct unicode_name *uname, u8 *de_name,
+ size_t de_name_len);

#ifdef CONFIG_MIGRATION
extern int buffer_migrate_page(struct address_space *,
--
2.35.1

2022-04-29 22:34:11

by Gabriel Krisman Bertazi

[permalink] [raw]
Subject: [PATCH v3 2/7] ext4: Simplify the handling of cached insensitive names

Keeping it as qstr avoids the unnecessary conversion in ext4_match

Signed-off-by: Gabriel Krisman Bertazi <[email protected]>

--
Changes since v1:
- Simplify hunk (eric)
---
fs/ext4/ext4.h | 2 +-
fs/ext4/namei.c | 22 +++++++++++-----------
2 files changed, 12 insertions(+), 12 deletions(-)

diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
index a743b1e3b89e..93a28fcb2e22 100644
--- a/fs/ext4/ext4.h
+++ b/fs/ext4/ext4.h
@@ -2490,7 +2490,7 @@ struct ext4_filename {
struct fscrypt_str crypto_buf;
#endif
#if IS_ENABLED(CONFIG_UNICODE)
- struct fscrypt_str cf_name;
+ struct qstr cf_name;
#endif
};

diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c
index c363f637057d..c1a8a09369d1 100644
--- a/fs/ext4/namei.c
+++ b/fs/ext4/namei.c
@@ -1382,7 +1382,8 @@ static int ext4_match_ci(const struct inode *parent, const struct qstr *name,
int ext4_fname_setup_ci_filename(struct inode *dir, const struct qstr *iname,
struct ext4_filename *name)
{
- struct fscrypt_str *cf_name = &name->cf_name;
+ struct qstr *cf_name = &name->cf_name;
+ unsigned char *buf;
struct dx_hash_info *hinfo = &name->hinfo;
int len;

@@ -1392,18 +1393,18 @@ int ext4_fname_setup_ci_filename(struct inode *dir, const struct qstr *iname,
return 0;
}

- cf_name->name = kmalloc(EXT4_NAME_LEN, GFP_NOFS);
- if (!cf_name->name)
+ buf = kmalloc(EXT4_NAME_LEN, GFP_NOFS);
+ if (!buf)
return -ENOMEM;

- len = utf8_casefold(dir->i_sb->s_encoding,
- iname, cf_name->name,
- EXT4_NAME_LEN);
+ len = utf8_casefold(dir->i_sb->s_encoding, iname, buf, EXT4_NAME_LEN);
if (len <= 0) {
- kfree(cf_name->name);
- cf_name->name = NULL;
+ kfree(buf);
+ buf = NULL;
}
+ cf_name->name = buf;
cf_name->len = (unsigned) len;
+
if (!IS_ENCRYPTED(dir))
return 0;

@@ -1442,8 +1443,6 @@ static bool ext4_match(struct inode *parent,
if (parent->i_sb->s_encoding && IS_CASEFOLDED(parent) &&
(!IS_ENCRYPTED(parent) || fscrypt_has_encryption_key(parent))) {
if (fname->cf_name.name) {
- struct qstr cf = {.name = fname->cf_name.name,
- .len = fname->cf_name.len};
if (IS_ENCRYPTED(parent)) {
if (fname->hinfo.hash != EXT4_DIRENT_HASH(de) ||
fname->hinfo.minor_hash !=
@@ -1452,7 +1451,8 @@ static bool ext4_match(struct inode *parent,
return false;
}
}
- ret = ext4_match_ci(parent, &cf, de->name,
+
+ ret = ext4_match_ci(parent, &fname->cf_name, de->name,
de->name_len, true);
} else {
ret = ext4_match_ci(parent, fname->usr_fname,
--
2.35.1

2022-04-30 05:45:22

by Gabriel Krisman Bertazi

[permalink] [raw]
Subject: [PATCH v3 1/7] ext4: Match the f2fs ci_compare implementation

ext4_ci_compare originally follows utf8_*_strcmp, which means return
zero on match. This means that every usage of that in ext4 negates
the return.

Turn it into a predicate function, let it follow the kernel convention
and return true on match, which means it's now the same as its f2fs
counterpart and can be extracted into generic code.

This change also makes it more obvious that we are ignoring error
handling in ext4_match, which can occur since casefolding support (bad
utf8 name due to disk corruption on strict mode causes -EINVAL) and
casefold+encryption (-ENOMEM). For now, keep the behavior. It is
handled by the following patches.

While we are there, change the comment to the kernel-doc style.

Signed-off-by: Gabriel Krisman Bertazi <[email protected]>
---
changes since v1:
- rename to match f2fs naming (Eric)
---
fs/ext4/namei.c | 65 ++++++++++++++++++++++++++++++++-----------------
1 file changed, 43 insertions(+), 22 deletions(-)

diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c
index 767b4bfe39c3..c363f637057d 100644
--- a/fs/ext4/namei.c
+++ b/fs/ext4/namei.c
@@ -1318,22 +1318,29 @@ static void dx_insert_block(struct dx_frame *frame, u32 hash, ext4_lblk_t block)
}

#if IS_ENABLED(CONFIG_UNICODE)
-/*
+/**
+ * ext4_match_ci() - Match (case-insensitive) a name with a dirent.
+ * @parent: Inode of the parent of the dentry.
+ * @name: name under lookup.
+ * @de_name: Dirent name.
+ * @de_name_len: dirent name length.
+ * @quick: whether @name is already casefolded.
+ *
* Test whether a case-insensitive directory entry matches the filename
- * being searched for. If quick is set, assume the name being looked up
- * is already in the casefolded form.
+ * being searched. If quick is set, the @name being looked up is
+ * already in the casefolded form.
*
- * Returns: 0 if the directory entry matches, more than 0 if it
- * doesn't match or less than zero on error.
+ * Return: > 0 if the directory entry matches, 0 if it doesn't match, or
+ * < 0 on error.
*/
-static int ext4_ci_compare(const struct inode *parent, const struct qstr *name,
- u8 *de_name, size_t de_name_len, bool quick)
+static int ext4_match_ci(const struct inode *parent, const struct qstr *name,
+ u8 *de_name, size_t de_name_len, bool quick)
{
const struct super_block *sb = parent->i_sb;
const struct unicode_map *um = sb->s_encoding;
struct fscrypt_str decrypted_name = FSTR_INIT(NULL, de_name_len);
struct qstr entry = QSTR_INIT(de_name, de_name_len);
- int ret;
+ int ret, match = false;

if (IS_ENCRYPTED(parent)) {
const struct fscrypt_str encrypted_name =
@@ -1354,20 +1361,22 @@ static int ext4_ci_compare(const struct inode *parent, const struct qstr *name,
ret = utf8_strncasecmp_folded(um, name, &entry);
else
ret = utf8_strncasecmp(um, name, &entry);
- if (ret < 0) {
- /* Handle invalid character sequence as either an error
- * or as an opaque byte sequence.
+
+ if (!ret)
+ match = true;
+ else if (ret < 0 && !sb_has_strict_encoding(sb)) {
+ /*
+ * In non-strict mode, fallback to a byte comparison if
+ * the names have invalid characters.
*/
- if (sb_has_strict_encoding(sb))
- ret = -EINVAL;
- else if (name->len != entry.len)
- ret = 1;
- else
- ret = !!memcmp(name->name, entry.name, entry.len);
+ ret = 0;
+ match = ((name->len == entry.len) &&
+ !memcmp(name->name, entry.name, entry.len));
}
+
out:
kfree(decrypted_name.name);
- return ret;
+ return (ret >= 0) ? match : ret;
}

int ext4_fname_setup_ci_filename(struct inode *dir, const struct qstr *iname,
@@ -1418,6 +1427,7 @@ static bool ext4_match(struct inode *parent,
struct ext4_dir_entry_2 *de)
{
struct fscrypt_name f;
+ int ret;

if (!de->inode)
return false;
@@ -1442,11 +1452,22 @@ static bool ext4_match(struct inode *parent,
return false;
}
}
- return !ext4_ci_compare(parent, &cf, de->name,
- de->name_len, true);
+ ret = ext4_match_ci(parent, &cf, de->name,
+ de->name_len, true);
+ } else {
+ ret = ext4_match_ci(parent, fname->usr_fname,
+ de->name, de->name_len, false);
+ }
+
+ if (ret < 0) {
+ /*
+ * Treat comparison errors as not a match. The
+ * only case where it happens is on a disk
+ * corruption or ENOMEM.
+ */
+ return false;
}
- return !ext4_ci_compare(parent, fname->usr_fname, de->name,
- de->name_len, false);
+ return ret;
}
#endif

--
2.35.1

2022-04-30 09:29:07

by Gabriel Krisman Bertazi

[permalink] [raw]
Subject: [PATCH v3 4/7] ext4: Simplify hash check on ext4_match

The existence of fname->cf_name.name requires s_encoding & IS_CASEFOLDED,
therefore this can be simplified.

Signed-off-by: Gabriel Krisman Bertazi <[email protected]>
---
fs/ext4/namei.c | 18 ++++++------------
1 file changed, 6 insertions(+), 12 deletions(-)

diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c
index 5102652b5af4..e450e52eef48 100644
--- a/fs/ext4/namei.c
+++ b/fs/ext4/namei.c
@@ -1440,19 +1440,13 @@ static bool ext4_match(struct inode *parent,
#endif

#if IS_ENABLED(CONFIG_UNICODE)
- if (parent->i_sb->s_encoding && IS_CASEFOLDED(parent) &&
- (!IS_ENCRYPTED(parent) || fscrypt_has_encryption_key(parent))) {
- if (fname->cf_name.name) {
- if (IS_ENCRYPTED(parent)) {
- if (fname->hinfo.hash != EXT4_DIRENT_HASH(de) ||
- fname->hinfo.minor_hash !=
- EXT4_DIRENT_MINOR_HASH(de)) {
-
- return false;
- }
- }
- }
+ if (IS_ENCRYPTED(parent) && fname->cf_name.name) {
+ if (fname->hinfo.hash != EXT4_DIRENT_HASH(de) ||
+ fname->hinfo.minor_hash != EXT4_DIRENT_MINOR_HASH(de))
+ return false;
+ }

+ if (parent->i_sb->s_encoding && IS_CASEFOLDED(parent)) {
u.folded_name = &fname->cf_name;
u.usr_name = fname->usr_fname;

--
2.35.1

2022-05-01 10:36:22

by Gabriel Krisman Bertazi

[permalink] [raw]
Subject: [PATCH v3 3/7] ext4: Implement ci comparison using unicode_name

By using a new type here, we can hide most of the caching casefold logic
from ext4. The condition in ext4_match is now quite redundant, but this
is addressed in the next patch.

This doesn't use ext4_filename to keep it generic, since the function
will be moved to libfs to be shared with f2fs.

Signed-off-by: Gabriel Krisman Bertazi <[email protected]>

--
Changes since v1:
- Instead of (ab)using fscrypt_name, create a new type (ebiggers).
---
fs/ext4/namei.c | 32 +++++++++++++++-----------------
include/linux/fs.h | 5 +++++
2 files changed, 20 insertions(+), 17 deletions(-)

diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c
index c1a8a09369d1..5102652b5af4 100644
--- a/fs/ext4/namei.c
+++ b/fs/ext4/namei.c
@@ -1321,20 +1321,19 @@ static void dx_insert_block(struct dx_frame *frame, u32 hash, ext4_lblk_t block)
/**
* ext4_match_ci() - Match (case-insensitive) a name with a dirent.
* @parent: Inode of the parent of the dentry.
- * @name: name under lookup.
+ * @uname: name under lookup.
* @de_name: Dirent name.
* @de_name_len: dirent name length.
- * @quick: whether @name is already casefolded.
*
* Test whether a case-insensitive directory entry matches the filename
- * being searched. If quick is set, the @name being looked up is
- * already in the casefolded form.
+ * being searched.
*
* Return: > 0 if the directory entry matches, 0 if it doesn't match, or
* < 0 on error.
*/
-static int ext4_match_ci(const struct inode *parent, const struct qstr *name,
- u8 *de_name, size_t de_name_len, bool quick)
+static int ext4_match_ci(const struct inode *parent,
+ const struct unicode_name *uname,
+ u8 *de_name, size_t de_name_len)
{
const struct super_block *sb = parent->i_sb;
const struct unicode_map *um = sb->s_encoding;
@@ -1357,10 +1356,10 @@ static int ext4_match_ci(const struct inode *parent, const struct qstr *name,
entry.len = decrypted_name.len;
}

- if (quick)
- ret = utf8_strncasecmp_folded(um, name, &entry);
+ if (uname->folded_name->name)
+ ret = utf8_strncasecmp_folded(um, uname->folded_name, &entry);
else
- ret = utf8_strncasecmp(um, name, &entry);
+ ret = utf8_strncasecmp(um, uname->usr_name, &entry);

if (!ret)
match = true;
@@ -1370,8 +1369,8 @@ static int ext4_match_ci(const struct inode *parent, const struct qstr *name,
* the names have invalid characters.
*/
ret = 0;
- match = ((name->len == entry.len) &&
- !memcmp(name->name, entry.name, entry.len));
+ match = ((uname->usr_name->len == entry.len) &&
+ !memcmp(uname->usr_name->name, entry.name, entry.len));
}

out:
@@ -1427,6 +1426,7 @@ static bool ext4_match(struct inode *parent,
const struct ext4_filename *fname,
struct ext4_dir_entry_2 *de)
{
+ struct unicode_name u;
struct fscrypt_name f;
int ret;

@@ -1451,14 +1451,12 @@ static bool ext4_match(struct inode *parent,
return false;
}
}
-
- ret = ext4_match_ci(parent, &fname->cf_name, de->name,
- de->name_len, true);
- } else {
- ret = ext4_match_ci(parent, fname->usr_fname,
- de->name, de->name_len, false);
}

+ u.folded_name = &fname->cf_name;
+ u.usr_name = fname->usr_fname;
+
+ ret = ext4_match_ci(parent, &u, de->name, de->name_len);
if (ret < 0) {
/*
* Treat comparison errors as not a match. The
diff --git a/include/linux/fs.h b/include/linux/fs.h
index e2d892b201b0..3f76a18a5f40 100644
--- a/include/linux/fs.h
+++ b/include/linux/fs.h
@@ -3358,6 +3358,11 @@ extern int generic_file_fsync(struct file *, loff_t, loff_t, int);

extern int generic_check_addressable(unsigned, u64);

+struct unicode_name {
+ const struct qstr *folded_name;
+ const struct qstr *usr_name;
+};
+
extern void generic_set_encrypted_ci_d_ops(struct dentry *dentry);

#ifdef CONFIG_MIGRATION
--
2.35.1

2022-05-02 17:11:09

by Gabriel Krisman Bertazi

[permalink] [raw]
Subject: [PATCH v3 5/7] ext4: Log error when lookup of encoded dentry fails

If the volume is in strict mode, ext4_ci_compare can report a broken
encoding name. This will not trigger on a bad lookup, which is caught
earlier, only if the actual disk name is bad.

Signed-off-by: Gabriel Krisman Bertazi <[email protected]>

---

Changes since v1:
- reword error message "file in directory" -> "filename" (Eric)
---
fs/ext4/namei.c | 3 +++
1 file changed, 3 insertions(+)

diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c
index e450e52eef48..d53c8d101099 100644
--- a/fs/ext4/namei.c
+++ b/fs/ext4/namei.c
@@ -1457,6 +1457,9 @@ static bool ext4_match(struct inode *parent,
* only case where it happens is on a disk
* corruption or ENOMEM.
*/
+ if (ret == -EINVAL)
+ EXT4_ERROR_INODE(parent,
+ "Bad encoded filename");
return false;
}
return ret;
--
2.35.1

2022-05-03 00:56:02

by Gabriel Krisman Bertazi

[permalink] [raw]
Subject: [PATCH v3 7/7] f2fs: Reuse generic_ci_match for ci comparisons

Now that ci_match is part of libfs, make f2fs reuse it instead of having
a different implementation.

Signed-off-by: Gabriel Krisman Bertazi <[email protected]>
---
fs/f2fs/dir.c | 58 ++++-----------------------------------------------
1 file changed, 4 insertions(+), 54 deletions(-)

diff --git a/fs/f2fs/dir.c b/fs/f2fs/dir.c
index 166f08623362..c39b3abbf99e 100644
--- a/fs/f2fs/dir.c
+++ b/fs/f2fs/dir.c
@@ -208,69 +208,19 @@ static struct f2fs_dir_entry *find_in_block(struct inode *dir,
return f2fs_find_target_dentry(&d, fname, max_slots);
}

-#if IS_ENABLED(CONFIG_UNICODE)
-/*
- * Test whether a case-insensitive directory entry matches the filename
- * being searched for.
- *
- * Returns 1 for a match, 0 for no match, and -errno on an error.
- */
-static int f2fs_match_ci_name(const struct inode *dir, const struct qstr *name,
- const u8 *de_name, u32 de_name_len)
-{
- const struct super_block *sb = dir->i_sb;
- const struct unicode_map *um = sb->s_encoding;
- struct fscrypt_str decrypted_name = FSTR_INIT(NULL, de_name_len);
- struct qstr entry = QSTR_INIT(de_name, de_name_len);
- int res;
-
- if (IS_ENCRYPTED(dir)) {
- const struct fscrypt_str encrypted_name =
- FSTR_INIT((u8 *)de_name, de_name_len);
-
- if (WARN_ON_ONCE(!fscrypt_has_encryption_key(dir)))
- return -EINVAL;
-
- decrypted_name.name = kmalloc(de_name_len, GFP_KERNEL);
- if (!decrypted_name.name)
- return -ENOMEM;
- res = fscrypt_fname_disk_to_usr(dir, 0, 0, &encrypted_name,
- &decrypted_name);
- if (res < 0)
- goto out;
- entry.name = decrypted_name.name;
- entry.len = decrypted_name.len;
- }
-
- res = utf8_strncasecmp_folded(um, name, &entry);
- /*
- * In strict mode, ignore invalid names. In non-strict mode,
- * fall back to treating them as opaque byte sequences.
- */
- if (res < 0 && !sb_has_strict_encoding(sb)) {
- res = name->len == entry.len &&
- memcmp(name->name, entry.name, name->len) == 0;
- } else {
- /* utf8_strncasecmp_folded returns 0 on match */
- res = (res == 0);
- }
-out:
- kfree(decrypted_name.name);
- return res;
-}
-#endif /* CONFIG_UNICODE */
-
static inline int f2fs_match_name(const struct inode *dir,
const struct f2fs_filename *fname,
const u8 *de_name, u32 de_name_len)
{
struct fscrypt_name f;
+ struct unicode_name u;

#if IS_ENABLED(CONFIG_UNICODE)
if (fname->cf_name.name) {
struct qstr cf = FSTR_TO_QSTR(&fname->cf_name);
-
- return f2fs_match_ci_name(dir, &cf, de_name, de_name_len);
+ u.folded_name = &cf;
+ u.usr_name = fname->usr_fname;
+ return generic_ci_match(dir, &u, (u8*) de_name, de_name_len);
}
#endif
f.usr_fname = fname->usr_fname;
--
2.35.1