Skip to content

Commit f8f4acb

Browse files
drosen-googletytso
authored andcommitted
ext4: use generic casefolding support
This switches ext4 over to the generic support provided in libfs. Since casefolded dentries behave the same in ext4 and f2fs, we decrease the maintenance burden by unifying them, and any optimizations will immediately apply to both. Signed-off-by: Daniel Rosenberg <drosen@google.com> Reviewed-by: Eric Biggers <ebiggers@google.com> Link: https://lore.kernel.org/r/20201028050820.1636571-1-drosen@google.com Signed-off-by: Theodore Ts'o <tytso@mit.edu>
1 parent d7dce9e commit f8f4acb

5 files changed

Lines changed: 17 additions & 93 deletions

File tree

‎fs/ext4/dir.c‎

Lines changed: 2 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -669,68 +669,8 @@ const struct file_operations ext4_dir_operations = {
669669
};
670670

671671
#ifdef CONFIG_UNICODE
672-
static int ext4_d_compare(const struct dentry *dentry, unsigned int len,
673-
const char *str, const struct qstr *name)
674-
{
675-
struct qstr qstr = {.name = str, .len = len };
676-
const struct dentry *parent = READ_ONCE(dentry->d_parent);
677-
const struct inode *inode = d_inode_rcu(parent);
678-
char strbuf[DNAME_INLINE_LEN];
679-
680-
if (!inode || !IS_CASEFOLDED(inode) ||
681-
!EXT4_SB(inode->i_sb)->s_encoding) {
682-
if (len != name->len)
683-
return -1;
684-
return memcmp(str, name->name, len);
685-
}
686-
687-
/*
688-
* If the dentry name is stored in-line, then it may be concurrently
689-
* modified by a rename. If this happens, the VFS will eventually retry
690-
* the lookup, so it doesn't matter what ->d_compare() returns.
691-
* However, it's unsafe to call utf8_strncasecmp() with an unstable
692-
* string. Therefore, we have to copy the name into a temporary buffer.
693-
*/
694-
if (len <= DNAME_INLINE_LEN - 1) {
695-
memcpy(strbuf, str, len);
696-
strbuf[len] = 0;
697-
qstr.name = strbuf;
698-
/* prevent compiler from optimizing out the temporary buffer */
699-
barrier();
700-
}
701-
702-
return ext4_ci_compare(inode, name, &qstr, false);
703-
}
704-
705-
static int ext4_d_hash(const struct dentry *dentry, struct qstr *str)
706-
{
707-
const struct ext4_sb_info *sbi = EXT4_SB(dentry->d_sb);
708-
const struct unicode_map *um = sbi->s_encoding;
709-
const struct inode *inode = d_inode_rcu(dentry);
710-
unsigned char *norm;
711-
int len, ret = 0;
712-
713-
if (!inode || !IS_CASEFOLDED(inode) || !um)
714-
return 0;
715-
716-
norm = kmalloc(PATH_MAX, GFP_ATOMIC);
717-
if (!norm)
718-
return -ENOMEM;
719-
720-
len = utf8_casefold(um, str, norm, PATH_MAX);
721-
if (len < 0) {
722-
if (ext4_has_strict_mode(sbi))
723-
ret = -EINVAL;
724-
goto out;
725-
}
726-
str->hash = full_name_hash(dentry, norm, len);
727-
out:
728-
kfree(norm);
729-
return ret;
730-
}
731-
732672
const struct dentry_operations ext4_dentry_ops = {
733-
.d_hash = ext4_d_hash,
734-
.d_compare = ext4_d_compare,
673+
.d_hash = generic_ci_d_hash,
674+
.d_compare = generic_ci_d_compare,
735675
};
736676
#endif

‎fs/ext4/ext4.h‎

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1443,14 +1443,6 @@ struct ext4_super_block {
14431443

14441444
#define EXT4_ENC_UTF8_12_1 1
14451445

1446-
/*
1447-
* Flags for ext4_sb_info.s_encoding_flags.
1448-
*/
1449-
#define EXT4_ENC_STRICT_MODE_FL (1 << 0)
1450-
1451-
#define ext4_has_strict_mode(sbi) \
1452-
(sbi->s_encoding_flags & EXT4_ENC_STRICT_MODE_FL)
1453-
14541446
/*
14551447
* fourth extended-fs super-block data in memory
14561448
*/
@@ -1500,10 +1492,6 @@ struct ext4_sb_info {
15001492
struct kobject s_kobj;
15011493
struct completion s_kobj_unregister;
15021494
struct super_block *s_sb;
1503-
#ifdef CONFIG_UNICODE
1504-
struct unicode_map *s_encoding;
1505-
__u16 s_encoding_flags;
1506-
#endif
15071495

15081496
/* Journaling */
15091497
struct journal_s *s_journal;

‎fs/ext4/hash.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ int ext4fs_dirhash(const struct inode *dir, const char *name, int len,
275275
struct dx_hash_info *hinfo)
276276
{
277277
#ifdef CONFIG_UNICODE
278-
const struct unicode_map *um = EXT4_SB(dir->i_sb)->s_encoding;
278+
const struct unicode_map *um = dir->i_sb->s_encoding;
279279
int r, dlen;
280280
unsigned char *buff;
281281
struct qstr qstr = {.name = name, .len = len };

‎fs/ext4/namei.c‎

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1285,8 +1285,8 @@ static void dx_insert_block(struct dx_frame *frame, u32 hash, ext4_lblk_t block)
12851285
int ext4_ci_compare(const struct inode *parent, const struct qstr *name,
12861286
const struct qstr *entry, bool quick)
12871287
{
1288-
const struct ext4_sb_info *sbi = EXT4_SB(parent->i_sb);
1289-
const struct unicode_map *um = sbi->s_encoding;
1288+
const struct super_block *sb = parent->i_sb;
1289+
const struct unicode_map *um = sb->s_encoding;
12901290
int ret;
12911291

12921292
if (quick)
@@ -1298,7 +1298,7 @@ int ext4_ci_compare(const struct inode *parent, const struct qstr *name,
12981298
/* Handle invalid character sequence as either an error
12991299
* or as an opaque byte sequence.
13001300
*/
1301-
if (ext4_has_strict_mode(sbi))
1301+
if (sb_has_strict_encoding(sb))
13021302
return -EINVAL;
13031303

13041304
if (name->len != entry->len)
@@ -1315,7 +1315,7 @@ void ext4_fname_setup_ci_filename(struct inode *dir, const struct qstr *iname,
13151315
{
13161316
int len;
13171317

1318-
if (!IS_CASEFOLDED(dir) || !EXT4_SB(dir->i_sb)->s_encoding) {
1318+
if (!IS_CASEFOLDED(dir) || !dir->i_sb->s_encoding) {
13191319
cf_name->name = NULL;
13201320
return;
13211321
}
@@ -1324,7 +1324,7 @@ void ext4_fname_setup_ci_filename(struct inode *dir, const struct qstr *iname,
13241324
if (!cf_name->name)
13251325
return;
13261326

1327-
len = utf8_casefold(EXT4_SB(dir->i_sb)->s_encoding,
1327+
len = utf8_casefold(dir->i_sb->s_encoding,
13281328
iname, cf_name->name,
13291329
EXT4_NAME_LEN);
13301330
if (len <= 0) {
@@ -1361,7 +1361,7 @@ static inline bool ext4_match(const struct inode *parent,
13611361
#endif
13621362

13631363
#ifdef CONFIG_UNICODE
1364-
if (EXT4_SB(parent->i_sb)->s_encoding && IS_CASEFOLDED(parent)) {
1364+
if (parent->i_sb->s_encoding && IS_CASEFOLDED(parent)) {
13651365
if (fname->cf_name.name) {
13661366
struct qstr cf = {.name = fname->cf_name.name,
13671367
.len = fname->cf_name.len};
@@ -2180,9 +2180,6 @@ static int ext4_add_entry(handle_t *handle, struct dentry *dentry,
21802180
struct buffer_head *bh = NULL;
21812181
struct ext4_dir_entry_2 *de;
21822182
struct super_block *sb;
2183-
#ifdef CONFIG_UNICODE
2184-
struct ext4_sb_info *sbi;
2185-
#endif
21862183
struct ext4_filename fname;
21872184
int retval;
21882185
int dx_fallback=0;
@@ -2199,9 +2196,8 @@ static int ext4_add_entry(handle_t *handle, struct dentry *dentry,
21992196
return -EINVAL;
22002197

22012198
#ifdef CONFIG_UNICODE
2202-
sbi = EXT4_SB(sb);
2203-
if (ext4_has_strict_mode(sbi) && IS_CASEFOLDED(dir) &&
2204-
sbi->s_encoding && utf8_validate(sbi->s_encoding, &dentry->d_name))
2199+
if (sb_has_strict_encoding(sb) && IS_CASEFOLDED(dir) &&
2200+
sb->s_encoding && utf8_validate(sb->s_encoding, &dentry->d_name))
22052201
return -EINVAL;
22062202
#endif
22072203

‎fs/ext4/super.c‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1288,7 +1288,7 @@ static void ext4_put_super(struct super_block *sb)
12881288
fs_put_dax(sbi->s_daxdev);
12891289
fscrypt_free_dummy_policy(&sbi->s_dummy_enc_policy);
12901290
#ifdef CONFIG_UNICODE
1291-
utf8_unload(sbi->s_encoding);
1291+
utf8_unload(sb->s_encoding);
12921292
#endif
12931293
kfree(sbi);
12941294
}
@@ -4303,7 +4303,7 @@ static int ext4_fill_super(struct super_block *sb, void *data, int silent)
43034303
goto failed_mount;
43044304

43054305
#ifdef CONFIG_UNICODE
4306-
if (ext4_has_feature_casefold(sb) && !sbi->s_encoding) {
4306+
if (ext4_has_feature_casefold(sb) && !sb->s_encoding) {
43074307
const struct ext4_sb_encodings *encoding_info;
43084308
struct unicode_map *encoding;
43094309
__u16 encoding_flags;
@@ -4334,8 +4334,8 @@ static int ext4_fill_super(struct super_block *sb, void *data, int silent)
43344334
"%s-%s with flags 0x%hx", encoding_info->name,
43354335
encoding_info->version?:"\b", encoding_flags);
43364336

4337-
sbi->s_encoding = encoding;
4338-
sbi->s_encoding_flags = encoding_flags;
4337+
sb->s_encoding = encoding;
4338+
sb->s_encoding_flags = encoding_flags;
43394339
}
43404340
#endif
43414341

@@ -4975,7 +4975,7 @@ static int ext4_fill_super(struct super_block *sb, void *data, int silent)
49754975
}
49764976

49774977
#ifdef CONFIG_UNICODE
4978-
if (sbi->s_encoding)
4978+
if (sb->s_encoding)
49794979
sb->s_d_op = &ext4_dentry_ops;
49804980
#endif
49814981

@@ -5184,7 +5184,7 @@ static int ext4_fill_super(struct super_block *sb, void *data, int silent)
51845184
crypto_free_shash(sbi->s_chksum_driver);
51855185

51865186
#ifdef CONFIG_UNICODE
5187-
utf8_unload(sbi->s_encoding);
5187+
utf8_unload(sb->s_encoding);
51885188
#endif
51895189

51905190
#ifdef CONFIG_QUOTA

0 commit comments

Comments
 (0)