FROMLIST: Add standard casefolding support

This adds general supporting functions for filesystems that use
utf8 casefolding. It provides standard dentry_operations and adds the
necessary structures in struct super_block to allow this standardization.

Ext4 and F2fs are switch to these implementations.

Signed-off-by: Daniel Rosenberg <[email protected]>
Note: Fixed issue with non-strictly enforced fallback hash
Test: Boots, /data/media is case insensitive
Bug: 138322712
Link: https://lore.kernel.org/linux-f2fs-devel/[email protected]/T/#t
Change-Id: I81b5fb5d3ce0259a60712ae2505c1e4b03dbafde
This commit is contained in:
Daniel Rosenberg 2020-01-22 20:05:18 -08:00 • committed by Alistair Delva
commit 7c3784a69e
2 changed files with 83 additions and 0 deletions

View file

@ -19,6 +19,8 @@
#include <linux/buffer_head.h> /* sync_mapping_buffers */
#include <linux/fs_context.h>
#include <linux/pseudo_fs.h>
#include <linux/unicode.h>
#include <linux/fscrypt.h>
#include <linux/uaccess.h>
@ -1291,3 +1293,62 @@ bool is_empty_dir_inode(struct inode *inode)
return (inode->i_fop == &empty_dir_operations) &&
(inode->i_op == &empty_dir_inode_operations);
}
#ifdef CONFIG_UNICODE
bool needs_casefold(const struct inode *dir)
{
return IS_CASEFOLDED(dir) && dir->i_sb->s_encoding &&
(!IS_ENCRYPTED(dir) || fscrypt_has_encryption_key(dir));
}
EXPORT_SYMBOL(needs_casefold);
int generic_ci_d_compare(const struct dentry *dentry, unsigned int len,
const char *str, const struct qstr *name)
{
const struct dentry *parent = READ_ONCE(dentry->d_parent);
const struct inode *inode = READ_ONCE(parent->d_inode);
const struct super_block *sb = dentry->d_sb;
const struct unicode_map *um = sb->s_encoding;
struct qstr entry = QSTR_INIT(str, len);
int ret;
if (!inode || !needs_casefold(inode))
goto fallback;
ret = utf8_strncasecmp(um, name, &entry);
if (ret >= 0)
return ret;
if (sb_has_enc_strict_mode(sb))
return -EINVAL;
fallback:
if (len != name->len)
return 1;
return !!memcmp(str, name->name, len);
}
EXPORT_SYMBOL(generic_ci_d_compare);
int generic_ci_d_hash(const struct dentry *dentry, struct qstr *str)
{
const struct inode *inode = READ_ONCE(dentry->d_inode);
struct super_block *sb = dentry->d_sb;
const struct unicode_map *um = sb->s_encoding;
int ret = 0;
if (!inode || !needs_casefold(inode))
return 0;
ret = utf8_casefold_hash(um, dentry, str);
if (ret < 0)
goto err;
return 0;
err:
if (sb_has_enc_strict_mode(sb))
ret = -EINVAL;
else
ret = 0;
return ret;
}
EXPORT_SYMBOL(generic_ci_d_hash);
#endif