Skip to content

Commit b886ee3

Browse files
Gabriel Krisman Bertazitytso
authored andcommitted
ext4: Support case-insensitive file name lookups
This patch implements the actual support for case-insensitive file name lookups in ext4, 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 (EXT4_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, Ext4 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 ext4 is implemented lives 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 EXT4 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: Gabriel Krisman Bertazi <[email protected]> Signed-off-by: Theodore Ts'o <[email protected]>
1 parent c83ad55 commit b886ee3

File tree

10 files changed

+223
-21
lines changed

10 files changed

+223
-21
lines changed

fs/ext4/dir.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <linux/buffer_head.h>
2727
#include <linux/slab.h>
2828
#include <linux/iversion.h>
29+
#include <linux/unicode.h>
2930
#include "ext4.h"
3031
#include "xattr.h"
3132

@@ -660,3 +661,50 @@ const struct file_operations ext4_dir_operations = {
660661
.open = ext4_dir_open,
661662
.release = ext4_release_dir,
662663
};
664+
665+
#ifdef CONFIG_UNICODE
666+
static int ext4_d_compare(const struct dentry *dentry, unsigned int len,
667+
const char *str, const struct qstr *name)
668+
{
669+
struct qstr qstr = {.name = str, .len = len };
670+
671+
if (!IS_CASEFOLDED(dentry->d_parent->d_inode)) {
672+
if (len != name->len)
673+
return -1;
674+
return !memcmp(str, name, len);
675+
}
676+
677+
return ext4_ci_compare(dentry->d_parent->d_inode, name, &qstr);
678+
}
679+
680+
static int ext4_d_hash(const struct dentry *dentry, struct qstr *str)
681+
{
682+
const struct ext4_sb_info *sbi = EXT4_SB(dentry->d_sb);
683+
const struct unicode_map *um = sbi->s_encoding;
684+
unsigned char *norm;
685+
int len, ret = 0;
686+
687+
if (!IS_CASEFOLDED(dentry->d_inode))
688+
return 0;
689+
690+
norm = kmalloc(PATH_MAX, GFP_ATOMIC);
691+
if (!norm)
692+
return -ENOMEM;
693+
694+
len = utf8_casefold(um, str, norm, PATH_MAX);
695+
if (len < 0) {
696+
if (ext4_has_strict_mode(sbi))
697+
ret = -EINVAL;
698+
goto out;
699+
}
700+
str->hash = full_name_hash(dentry, norm, len);
701+
out:
702+
kfree(norm);
703+
return ret;
704+
}
705+
706+
const struct dentry_operations ext4_dentry_ops = {
707+
.d_hash = ext4_d_hash,
708+
.d_compare = ext4_d_compare,
709+
};
710+
#endif

fs/ext4/ext4.h

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -399,10 +399,11 @@ struct flex_groups {
399399
#define EXT4_EOFBLOCKS_FL 0x00400000 /* Blocks allocated beyond EOF */
400400
#define EXT4_INLINE_DATA_FL 0x10000000 /* Inode has inline data. */
401401
#define EXT4_PROJINHERIT_FL 0x20000000 /* Create with parents projid */
402+
#define EXT4_CASEFOLD_FL 0x40000000 /* Casefolded file */
402403
#define EXT4_RESERVED_FL 0x80000000 /* reserved for ext4 lib */
403404

404-
#define EXT4_FL_USER_VISIBLE 0x304BDFFF /* User visible flags */
405-
#define EXT4_FL_USER_MODIFIABLE 0x204BC0FF /* User modifiable flags */
405+
#define EXT4_FL_USER_VISIBLE 0x704BDFFF /* User visible flags */
406+
#define EXT4_FL_USER_MODIFIABLE 0x604BC0FF /* User modifiable flags */
406407

407408
/* Flags we can manipulate with through EXT4_IOC_FSSETXATTR */
408409
#define EXT4_FL_XFLAG_VISIBLE (EXT4_SYNC_FL | \
@@ -417,10 +418,10 @@ struct flex_groups {
417418
EXT4_SYNC_FL | EXT4_NODUMP_FL | EXT4_NOATIME_FL |\
418419
EXT4_NOCOMPR_FL | EXT4_JOURNAL_DATA_FL |\
419420
EXT4_NOTAIL_FL | EXT4_DIRSYNC_FL |\
420-
EXT4_PROJINHERIT_FL)
421+
EXT4_PROJINHERIT_FL | EXT4_CASEFOLD_FL)
421422

422423
/* Flags that are appropriate for regular files (all but dir-specific ones). */
423-
#define EXT4_REG_FLMASK (~(EXT4_DIRSYNC_FL | EXT4_TOPDIR_FL))
424+
#define EXT4_REG_FLMASK (~(EXT4_DIRSYNC_FL | EXT4_TOPDIR_FL | EXT4_CASEFOLD_FL))
424425

425426
/* Flags that are appropriate for non-directories/regular files. */
426427
#define EXT4_OTHER_FLMASK (EXT4_NODUMP_FL | EXT4_NOATIME_FL)
@@ -2393,8 +2394,8 @@ extern int ext4_check_all_de(struct inode *dir, struct buffer_head *bh,
23932394
extern int ext4_sync_file(struct file *, loff_t, loff_t, int);
23942395

23952396
/* hash.c */
2396-
extern int ext4fs_dirhash(const char *name, int len, struct
2397-
dx_hash_info *hinfo);
2397+
extern int ext4fs_dirhash(const struct inode *dir, const char *name, int len,
2398+
struct dx_hash_info *hinfo);
23982399

23992400
/* ialloc.c */
24002401
extern struct inode *__ext4_new_inode(handle_t *, struct inode *, umode_t,
@@ -2990,6 +2991,10 @@ static inline void ext4_unlock_group(struct super_block *sb,
29902991
/* dir.c */
29912992
extern const struct file_operations ext4_dir_operations;
29922993

2994+
#ifdef CONFIG_UNICODE
2995+
extern const struct dentry_operations ext4_dentry_ops;
2996+
#endif
2997+
29932998
/* file.c */
29942999
extern const struct inode_operations ext4_file_inode_operations;
29953000
extern const struct file_operations ext4_file_operations;
@@ -3082,6 +3087,10 @@ extern void initialize_dirent_tail(struct ext4_dir_entry_tail *t,
30823087
extern int ext4_handle_dirty_dirent_node(handle_t *handle,
30833088
struct inode *inode,
30843089
struct buffer_head *bh);
3090+
extern int ext4_ci_compare(const struct inode *parent,
3091+
const struct qstr *name,
3092+
const struct qstr *entry);
3093+
30853094
#define S_SHIFT 12
30863095
static const unsigned char ext4_type_by_mode[(S_IFMT >> S_SHIFT) + 1] = {
30873096
[S_IFREG >> S_SHIFT] = EXT4_FT_REG_FILE,

fs/ext4/hash.c

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77

88
#include <linux/fs.h>
9+
#include <linux/unicode.h>
910
#include <linux/compiler.h>
1011
#include <linux/bitops.h>
1112
#include "ext4.h"
@@ -196,7 +197,8 @@ static void str2hashbuf_unsigned(const char *msg, int len, __u32 *buf, int num)
196197
* represented, and whether or not the returned hash is 32 bits or 64
197198
* bits. 32 bit hashes will return 0 for the minor hash.
198199
*/
199-
int ext4fs_dirhash(const char *name, int len, struct dx_hash_info *hinfo)
200+
static int __ext4fs_dirhash(const char *name, int len,
201+
struct dx_hash_info *hinfo)
200202
{
201203
__u32 hash;
202204
__u32 minor_hash = 0;
@@ -268,3 +270,33 @@ int ext4fs_dirhash(const char *name, int len, struct dx_hash_info *hinfo)
268270
hinfo->minor_hash = minor_hash;
269271
return 0;
270272
}
273+
274+
int ext4fs_dirhash(const struct inode *dir, const char *name, int len,
275+
struct dx_hash_info *hinfo)
276+
{
277+
#ifdef CONFIG_UNICODE
278+
const struct unicode_map *um = EXT4_SB(dir->i_sb)->s_encoding;
279+
int r, dlen;
280+
unsigned char *buff;
281+
struct qstr qstr = {.name = name, .len = len };
282+
283+
if (len && IS_CASEFOLDED(dir)) {
284+
buff = kzalloc(sizeof(char) * PATH_MAX, GFP_KERNEL);
285+
if (!buff)
286+
return -ENOMEM;
287+
288+
dlen = utf8_casefold(um, &qstr, buff, PATH_MAX);
289+
if (dlen < 0) {
290+
kfree(buff);
291+
goto opaque_seq;
292+
}
293+
294+
r = __ext4fs_dirhash(buff, dlen, hinfo);
295+
296+
kfree(buff);
297+
return r;
298+
}
299+
opaque_seq:
300+
#endif
301+
return __ext4fs_dirhash(name, len, hinfo);
302+
}

fs/ext4/ialloc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ static int find_group_orlov(struct super_block *sb, struct inode *parent,
455455
if (qstr) {
456456
hinfo.hash_version = DX_HASH_HALF_MD4;
457457
hinfo.seed = sbi->s_hash_seed;
458-
ext4fs_dirhash(qstr->name, qstr->len, &hinfo);
458+
ext4fs_dirhash(parent, qstr->name, qstr->len, &hinfo);
459459
grp = hinfo.hash;
460460
} else
461461
grp = prandom_u32();

fs/ext4/inline.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1407,7 +1407,7 @@ int htree_inlinedir_to_tree(struct file *dir_file,
14071407
}
14081408
}
14091409

1410-
ext4fs_dirhash(de->name, de->name_len, hinfo);
1410+
ext4fs_dirhash(dir, de->name, de->name_len, hinfo);
14111411
if ((hinfo->hash < start_hash) ||
14121412
((hinfo->hash == start_hash) &&
14131413
(hinfo->minor_hash < start_minor_hash)))

fs/ext4/inode.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4742,9 +4742,11 @@ void ext4_set_inode_flags(struct inode *inode)
47424742
new_fl |= S_DAX;
47434743
if (flags & EXT4_ENCRYPT_FL)
47444744
new_fl |= S_ENCRYPTED;
4745+
if (flags & EXT4_CASEFOLD_FL)
4746+
new_fl |= S_CASEFOLD;
47454747
inode_set_flags(inode, new_fl,
47464748
S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC|S_DAX|
4747-
S_ENCRYPTED);
4749+
S_ENCRYPTED|S_CASEFOLD);
47484750
}
47494751

47504752
static blkcnt_t ext4_inode_blocks(struct ext4_inode *raw_inode,

fs/ext4/ioctl.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ static int ext4_ioctl_setflags(struct inode *inode,
278278
struct ext4_iloc iloc;
279279
unsigned int oldflags, mask, i;
280280
unsigned int jflag;
281+
struct super_block *sb = inode->i_sb;
281282

282283
/* Is it quota file? Do not allow user to mess with it */
283284
if (ext4_is_quota_file(inode))
@@ -322,6 +323,23 @@ static int ext4_ioctl_setflags(struct inode *inode,
322323
goto flags_out;
323324
}
324325

326+
if ((flags ^ oldflags) & EXT4_CASEFOLD_FL) {
327+
if (!ext4_has_feature_casefold(sb)) {
328+
err = -EOPNOTSUPP;
329+
goto flags_out;
330+
}
331+
332+
if (!S_ISDIR(inode->i_mode)) {
333+
err = -ENOTDIR;
334+
goto flags_out;
335+
}
336+
337+
if (!ext4_empty_dir(inode)) {
338+
err = -ENOTEMPTY;
339+
goto flags_out;
340+
}
341+
}
342+
325343
handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
326344
if (IS_ERR(handle)) {
327345
err = PTR_ERR(handle);

0 commit comments

Comments
 (0)