Skip to content

Commit 2c2eb7a

Browse files
drosen-googleJaegeuk Kim
authored andcommitted
f2fs: Support case-insensitive file name lookups
Modeled after commit b886ee3 ("ext4: Support case-insensitive file name lookups") """ This patch implements the actual support for case-insensitive file name lookups in f2fs, based on the feature bit and the encoding stored in the superblock. A filesystem that has the casefold feature set is able to configure directories with the +F (F2FS_CASEFOLD_FL) attribute, enabling lookups to succeed in that directory in a case-insensitive fashion, i.e: match a directory entry even if the name used by userspace is not a byte per byte match with the disk name, but is an equivalent case-insensitive version of the Unicode string. This operation is called a case-insensitive file name lookup. The feature is configured as an inode attribute applied to directories and inherited by its children. This attribute can only be enabled on empty directories for filesystems that support the encoding feature, thus preventing collision of file names that only differ by case. * dcache handling: For a +F directory, F2Fs only stores the first equivalent name dentry used in the dcache. This is done to prevent unintentional duplication of dentries in the dcache, while also allowing the VFS code to quickly find the right entry in the cache despite which equivalent string was used in a previous lookup, without having to resort to ->lookup(). d_hash() of casefolded directories is implemented as the hash of the casefolded string, such that we always have a well-known bucket for all the equivalencies of the same string. d_compare() uses the utf8_strncasecmp() infrastructure, which handles the comparison of equivalent, same case, names as well. For now, negative lookups are not inserted in the dcache, since they would need to be invalidated anyway, because we can't trust missing file dentries. This is bad for performance but requires some leveraging of the vfs layer to fix. We can live without that for now, and so does everyone else. * on-disk data: Despite using a specific version of the name as the internal representation within the dcache, the name stored and fetched from the disk is a byte-per-byte match with what the user requested, making this implementation 'name-preserving'. i.e. no actual information is lost when writing to storage. DX is supported by modifying the hashes used in +F directories to make them case/encoding-aware. The new disk hashes are calculated as the hash of the full casefolded string, instead of the string directly. This allows us to efficiently search for file names in the htree without requiring the user to provide an exact name. * Dealing with invalid sequences: By default, when a invalid UTF-8 sequence is identified, ext4 will treat it as an opaque byte sequence, ignoring the encoding and reverting to the old behavior for that unique file. This means that case-insensitive file name lookup will not work only for that file. An optional bit can be set in the superblock telling the filesystem code and userspace tools to enforce the encoding. When that optional bit is set, any attempt to create a file name using an invalid UTF-8 sequence will fail and return an error to userspace. * Normalization algorithm: The UTF-8 algorithms used to compare strings in f2fs is implemented in fs/unicode, and is based on a previous version developed by SGI. It implements the Canonical decomposition (NFD) algorithm described by the Unicode specification 12.1, or higher, combined with the elimination of ignorable code points (NFDi) and full case-folding (CF) as documented in fs/unicode/utf8_norm.c. NFD seems to be the best normalization method for F2FS because: - It has a lower cost than NFC/NFKC (which requires decomposing to NFD as an intermediary step) - It doesn't eliminate important semantic meaning like compatibility decompositions. Although: - This implementation is not completely linguistic accurate, because different languages have conflicting rules, which would require the specialization of the filesystem to a given locale, which brings all sorts of problems for removable media and for users who use more than one language. """ Signed-off-by: Daniel Rosenberg <drosen@google.com> Reviewed-by: Chao Yu <yuchao0@huawei.com> Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
1 parent 5aba543 commit 2c2eb7a

8 files changed

Lines changed: 204 additions & 20 deletions

File tree

‎fs/f2fs/dir.c‎

Lines changed: 115 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <linux/fs.h>
99
#include <linux/f2fs_fs.h>
1010
#include <linux/sched/signal.h>
11+
#include <linux/unicode.h>
1112
#include "f2fs.h"
1213
#include "node.h"
1314
#include "acl.h"
@@ -81,7 +82,8 @@ static unsigned long dir_block_index(unsigned int level,
8182
return bidx;
8283
}
8384

84-
static struct f2fs_dir_entry *find_in_block(struct page *dentry_page,
85+
static struct f2fs_dir_entry *find_in_block(struct inode *dir,
86+
struct page *dentry_page,
8587
struct fscrypt_name *fname,
8688
f2fs_hash_t namehash,
8789
int *max_slots,
@@ -93,21 +95,57 @@ static struct f2fs_dir_entry *find_in_block(struct page *dentry_page,
9395

9496
dentry_blk = (struct f2fs_dentry_block *)page_address(dentry_page);
9597

96-
make_dentry_ptr_block(NULL, &d, dentry_blk);
98+
make_dentry_ptr_block(dir, &d, dentry_blk);
9799
de = f2fs_find_target_dentry(fname, namehash, max_slots, &d);
98100
if (de)
99101
*res_page = dentry_page;
100102

101103
return de;
102104
}
103105

106+
#ifdef CONFIG_UNICODE
107+
/*
108+
* Test whether a case-insensitive directory entry matches the filename
109+
* being searched for.
110+
*
111+
* Returns: 0 if the directory entry matches, more than 0 if it
112+
* doesn't match or less than zero on error.
113+
*/
114+
int f2fs_ci_compare(const struct inode *parent, const struct qstr *name,
115+
const struct qstr *entry)
116+
{
117+
const struct f2fs_sb_info *sbi = F2FS_SB(parent->i_sb);
118+
const struct unicode_map *um = sbi->s_encoding;
119+
int ret;
120+
121+
ret = utf8_strncasecmp(um, name, entry);
122+
if (ret < 0) {
123+
/* Handle invalid character sequence as either an error
124+
* or as an opaque byte sequence.
125+
*/
126+
if (f2fs_has_strict_mode(sbi))
127+
return -EINVAL;
128+
129+
if (name->len != entry->len)
130+
return 1;
131+
132+
return !!memcmp(name->name, entry->name, name->len);
133+
}
134+
135+
return ret;
136+
}
137+
#endif
138+
104139
struct f2fs_dir_entry *f2fs_find_target_dentry(struct fscrypt_name *fname,
105140
f2fs_hash_t namehash, int *max_slots,
106141
struct f2fs_dentry_ptr *d)
107142
{
108143
struct f2fs_dir_entry *de;
109144
unsigned long bit_pos = 0;
110145
int max_len = 0;
146+
#ifdef CONFIG_UNICODE
147+
struct qstr entry;
148+
#endif
111149

112150
if (max_slots)
113151
*max_slots = 0;
@@ -119,16 +157,28 @@ struct f2fs_dir_entry *f2fs_find_target_dentry(struct fscrypt_name *fname,
119157
}
120158

121159
de = &d->dentry[bit_pos];
160+
#ifdef CONFIG_UNICODE
161+
entry.name = d->filename[bit_pos];
162+
entry.len = de->name_len;
163+
#endif
122164

123165
if (unlikely(!de->name_len)) {
124166
bit_pos++;
125167
continue;
126168
}
169+
if (de->hash_code == namehash) {
170+
#ifdef CONFIG_UNICODE
171+
if (F2FS_SB(d->inode->i_sb)->s_encoding &&
172+
IS_CASEFOLDED(d->inode) &&
173+
!f2fs_ci_compare(d->inode,
174+
fname->usr_fname, &entry))
175+
goto found;
127176

128-
if (de->hash_code == namehash &&
129-
fscrypt_match_name(fname, d->filename[bit_pos],
130-
le16_to_cpu(de->name_len)))
131-
goto found;
177+
#endif
178+
if (fscrypt_match_name(fname, d->filename[bit_pos],
179+
le16_to_cpu(de->name_len)))
180+
goto found;
181+
}
132182

133183
if (max_slots && max_len > *max_slots)
134184
*max_slots = max_len;
@@ -157,7 +207,7 @@ static struct f2fs_dir_entry *find_in_level(struct inode *dir,
157207
struct f2fs_dir_entry *de = NULL;
158208
bool room = false;
159209
int max_slots;
160-
f2fs_hash_t namehash = f2fs_dentry_hash(&name, fname);
210+
f2fs_hash_t namehash = f2fs_dentry_hash(dir, &name, fname);
161211

162212
nbucket = dir_buckets(level, F2FS_I(dir)->i_dir_level);
163213
nblock = bucket_blocks(level);
@@ -179,8 +229,8 @@ static struct f2fs_dir_entry *find_in_level(struct inode *dir,
179229
}
180230
}
181231

182-
de = find_in_block(dentry_page, fname, namehash, &max_slots,
183-
res_page);
232+
de = find_in_block(dir, dentry_page, fname, namehash,
233+
&max_slots, res_page);
184234
if (de)
185235
break;
186236

@@ -250,6 +300,14 @@ struct f2fs_dir_entry *f2fs_find_entry(struct inode *dir,
250300
struct fscrypt_name fname;
251301
int err;
252302

303+
#ifdef CONFIG_UNICODE
304+
if (f2fs_has_strict_mode(F2FS_I_SB(dir)) && IS_CASEFOLDED(dir) &&
305+
utf8_validate(F2FS_I_SB(dir)->s_encoding, child)) {
306+
*res_page = ERR_PTR(-EINVAL);
307+
return NULL;
308+
}
309+
#endif
310+
253311
err = fscrypt_setup_filename(dir, child, 1, &fname);
254312
if (err) {
255313
if (err == -ENOENT)
@@ -504,7 +562,7 @@ int f2fs_add_regular_entry(struct inode *dir, const struct qstr *new_name,
504562

505563
level = 0;
506564
slots = GET_DENTRY_SLOTS(new_name->len);
507-
dentry_hash = f2fs_dentry_hash(new_name, NULL);
565+
dentry_hash = f2fs_dentry_hash(dir, new_name, NULL);
508566

509567
current_depth = F2FS_I(dir)->i_current_depth;
510568
if (F2FS_I(dir)->chash == dentry_hash) {
@@ -943,3 +1001,50 @@ const struct file_operations f2fs_dir_operations = {
9431001
.compat_ioctl = f2fs_compat_ioctl,
9441002
#endif
9451003
};
1004+
1005+
#ifdef CONFIG_UNICODE
1006+
static int f2fs_d_compare(const struct dentry *dentry, unsigned int len,
1007+
const char *str, const struct qstr *name)
1008+
{
1009+
struct qstr qstr = {.name = str, .len = len };
1010+
1011+
if (!IS_CASEFOLDED(dentry->d_parent->d_inode)) {
1012+
if (len != name->len)
1013+
return -1;
1014+
return memcmp(str, name, len);
1015+
}
1016+
1017+
return f2fs_ci_compare(dentry->d_parent->d_inode, name, &qstr);
1018+
}
1019+
1020+
static int f2fs_d_hash(const struct dentry *dentry, struct qstr *str)
1021+
{
1022+
struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
1023+
const struct unicode_map *um = sbi->s_encoding;
1024+
unsigned char *norm;
1025+
int len, ret = 0;
1026+
1027+
if (!IS_CASEFOLDED(dentry->d_inode))
1028+
return 0;
1029+
1030+
norm = f2fs_kmalloc(sbi, PATH_MAX, GFP_ATOMIC);
1031+
if (!norm)
1032+
return -ENOMEM;
1033+
1034+
len = utf8_casefold(um, str, norm, PATH_MAX);
1035+
if (len < 0) {
1036+
if (f2fs_has_strict_mode(sbi))
1037+
ret = -EINVAL;
1038+
goto out;
1039+
}
1040+
str->hash = full_name_hash(dentry, norm, len);
1041+
out:
1042+
kvfree(norm);
1043+
return ret;
1044+
}
1045+
1046+
const struct dentry_operations f2fs_dentry_ops = {
1047+
.d_hash = f2fs_d_hash,
1048+
.d_compare = f2fs_d_compare,
1049+
};
1050+
#endif

‎fs/f2fs/f2fs.h‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2367,13 +2367,16 @@ static inline void f2fs_change_bit(unsigned int nr, char *addr)
23672367
#define F2FS_INDEX_FL 0x00001000 /* hash-indexed directory */
23682368
#define F2FS_DIRSYNC_FL 0x00010000 /* dirsync behaviour (directories only) */
23692369
#define F2FS_PROJINHERIT_FL 0x20000000 /* Create with parents projid */
2370+
#define F2FS_CASEFOLD_FL 0x40000000 /* Casefolded file */
23702371

23712372
/* Flags that should be inherited by new inodes from their parent. */
23722373
#define F2FS_FL_INHERITED (F2FS_SYNC_FL | F2FS_NODUMP_FL | F2FS_NOATIME_FL | \
2373-
F2FS_DIRSYNC_FL | F2FS_PROJINHERIT_FL)
2374+
F2FS_DIRSYNC_FL | F2FS_PROJINHERIT_FL | \
2375+
F2FS_CASEFOLD_FL)
23742376

23752377
/* Flags that are appropriate for regular files (all but dir-specific ones). */
2376-
#define F2FS_REG_FLMASK (~(F2FS_DIRSYNC_FL | F2FS_PROJINHERIT_FL))
2378+
#define F2FS_REG_FLMASK (~(F2FS_DIRSYNC_FL | F2FS_PROJINHERIT_FL | \
2379+
F2FS_CASEFOLD_FL))
23772380

23782381
/* Flags that are appropriate for non-directories/regular files. */
23792382
#define F2FS_OTHER_FLMASK (F2FS_NODUMP_FL | F2FS_NOATIME_FL)
@@ -2933,6 +2936,10 @@ int f2fs_update_extension_list(struct f2fs_sb_info *sbi, const char *name,
29332936
bool hot, bool set);
29342937
struct dentry *f2fs_get_parent(struct dentry *child);
29352938

2939+
extern int f2fs_ci_compare(const struct inode *parent,
2940+
const struct qstr *name,
2941+
const struct qstr *entry);
2942+
29362943
/*
29372944
* dir.c
29382945
*/
@@ -2996,8 +3003,8 @@ int f2fs_sanity_check_ckpt(struct f2fs_sb_info *sbi);
29963003
/*
29973004
* hash.c
29983005
*/
2999-
f2fs_hash_t f2fs_dentry_hash(const struct qstr *name_info,
3000-
struct fscrypt_name *fname);
3006+
f2fs_hash_t f2fs_dentry_hash(const struct inode *dir,
3007+
const struct qstr *name_info, struct fscrypt_name *fname);
30013008

30023009
/*
30033010
* node.c
@@ -3440,6 +3447,9 @@ static inline void f2fs_destroy_root_stats(void) { }
34403447
#endif
34413448

34423449
extern const struct file_operations f2fs_dir_operations;
3450+
#ifdef CONFIG_UNICODE
3451+
extern const struct dentry_operations f2fs_dentry_ops;
3452+
#endif
34433453
extern const struct file_operations f2fs_file_operations;
34443454
extern const struct inode_operations f2fs_file_inode_operations;
34453455
extern const struct address_space_operations f2fs_dblock_aops;

‎fs/f2fs/file.c‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1664,6 +1664,13 @@ static int f2fs_setflags_common(struct inode *inode, u32 iflags, u32 mask)
16641664
if (IS_NOQUOTA(inode))
16651665
return -EPERM;
16661666

1667+
if ((iflags ^ fi->i_flags) & F2FS_CASEFOLD_FL) {
1668+
if (!f2fs_sb_has_casefold(F2FS_I_SB(inode)))
1669+
return -EOPNOTSUPP;
1670+
if (!f2fs_empty_dir(inode))
1671+
return -ENOTEMPTY;
1672+
}
1673+
16671674
fi->i_flags = iflags | (fi->i_flags & ~mask);
16681675

16691676
if (fi->i_flags & F2FS_PROJINHERIT_FL)
@@ -1698,6 +1705,7 @@ static const struct {
16981705
{ F2FS_INDEX_FL, FS_INDEX_FL },
16991706
{ F2FS_DIRSYNC_FL, FS_DIRSYNC_FL },
17001707
{ F2FS_PROJINHERIT_FL, FS_PROJINHERIT_FL },
1708+
{ F2FS_CASEFOLD_FL, FS_CASEFOLD_FL },
17011709
};
17021710

17031711
#define F2FS_GETTABLE_FS_FL ( \
@@ -1711,7 +1719,8 @@ static const struct {
17111719
FS_PROJINHERIT_FL | \
17121720
FS_ENCRYPT_FL | \
17131721
FS_INLINE_DATA_FL | \
1714-
FS_NOCOW_FL)
1722+
FS_NOCOW_FL | \
1723+
FS_CASEFOLD_FL)
17151724

17161725
#define F2FS_SETTABLE_FS_FL ( \
17171726
FS_SYNC_FL | \
@@ -1720,7 +1729,8 @@ static const struct {
17201729
FS_NODUMP_FL | \
17211730
FS_NOATIME_FL | \
17221731
FS_DIRSYNC_FL | \
1723-
FS_PROJINHERIT_FL)
1732+
FS_PROJINHERIT_FL | \
1733+
FS_CASEFOLD_FL)
17241734

17251735
/* Convert f2fs on-disk i_flags to FS_IOC_{GET,SET}FLAGS flags */
17261736
static inline u32 f2fs_iflags_to_fsflags(u32 iflags)

‎fs/f2fs/hash.c‎

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <linux/f2fs_fs.h>
1515
#include <linux/cryptohash.h>
1616
#include <linux/pagemap.h>
17+
#include <linux/unicode.h>
1718

1819
#include "f2fs.h"
1920

@@ -67,7 +68,7 @@ static void str2hashbuf(const unsigned char *msg, size_t len,
6768
*buf++ = pad;
6869
}
6970

70-
f2fs_hash_t f2fs_dentry_hash(const struct qstr *name_info,
71+
static f2fs_hash_t __f2fs_dentry_hash(const struct qstr *name_info,
7172
struct fscrypt_name *fname)
7273
{
7374
__u32 hash;
@@ -103,3 +104,37 @@ f2fs_hash_t f2fs_dentry_hash(const struct qstr *name_info,
103104
f2fs_hash = cpu_to_le32(hash & ~F2FS_HASH_COL_BIT);
104105
return f2fs_hash;
105106
}
107+
108+
f2fs_hash_t f2fs_dentry_hash(const struct inode *dir,
109+
const struct qstr *name_info, struct fscrypt_name *fname)
110+
{
111+
#ifdef CONFIG_UNICODE
112+
struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb);
113+
const struct unicode_map *um = sbi->s_encoding;
114+
int r, dlen;
115+
unsigned char *buff;
116+
struct qstr folded;
117+
118+
if (!name_info->len || !IS_CASEFOLDED(dir))
119+
goto opaque_seq;
120+
121+
buff = f2fs_kzalloc(sbi, sizeof(char) * PATH_MAX, GFP_KERNEL);
122+
if (!buff)
123+
return -ENOMEM;
124+
125+
dlen = utf8_casefold(um, name_info, buff, PATH_MAX);
126+
if (dlen < 0) {
127+
kvfree(buff);
128+
goto opaque_seq;
129+
}
130+
folded.name = buff;
131+
folded.len = dlen;
132+
r = __f2fs_dentry_hash(&folded, fname);
133+
134+
kvfree(buff);
135+
return r;
136+
137+
opaque_seq:
138+
#endif
139+
return __f2fs_dentry_hash(name_info, fname);
140+
}

‎fs/f2fs/inline.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ struct f2fs_dir_entry *f2fs_find_in_inline_dir(struct inode *dir,
320320
return NULL;
321321
}
322322

323-
namehash = f2fs_dentry_hash(&name, fname);
323+
namehash = f2fs_dentry_hash(dir, &name, fname);
324324

325325
inline_dentry = inline_data_addr(dir, ipage);
326326

@@ -580,7 +580,7 @@ int f2fs_add_inline_entry(struct inode *dir, const struct qstr *new_name,
580580

581581
f2fs_wait_on_page_writeback(ipage, NODE, true, true);
582582

583-
name_hash = f2fs_dentry_hash(new_name, NULL);
583+
name_hash = f2fs_dentry_hash(dir, new_name, NULL);
584584
f2fs_update_dentry(ino, mode, &d, new_name, name_hash, bit_pos);
585585

586586
set_page_dirty(ipage);

‎fs/f2fs/inode.c‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,11 @@ void f2fs_set_inode_flags(struct inode *inode)
4646
new_fl |= S_DIRSYNC;
4747
if (file_is_encrypt(inode))
4848
new_fl |= S_ENCRYPTED;
49+
if (flags & F2FS_CASEFOLD_FL)
50+
new_fl |= S_CASEFOLD;
4951
inode_set_flags(inode, new_fl,
5052
S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC|
51-
S_ENCRYPTED);
53+
S_ENCRYPTED|S_CASEFOLD);
5254
}
5355

5456
static void __get_inode_rdev(struct inode *inode, struct f2fs_inode *ri)

0 commit comments

Comments
 (0)