Support fof Casefolding and Encryption
Subsystems (8)
v7 cover Support fof Casefolding and Encryption
These patches are all on top of torvalds/master
Ext4 and F2FS currently both support casefolding and encryption, but not at the same time. These patches aim to rectify that.
I moved the identical casefolding dcache operations for ext4 and f2fs into fs/libfs.c, as all filesystems using casefolded names will want them.
I've also adjust fscrypt to not set it's d_revalidate operation during it's prepare lookup, instead having the calling filesystem set it up. This is done to that the filesystem may have it's own dentry_operations. Also added a helper function in libfs.c that will work for filesystems supporting both casefolding and fscrypt.
For Ext4, since the hash for encrypted casefolded directory names cannot be computed without the key, we need to store the hash on disk. We only do so for encrypted and casefolded directories to avoid on disk format changes. Previously encryption and casefolding could not be on the same filesystem, and we're relaxing that requirement. F2fs is a bit more straightforward since it already stores hashes on disk.
I've updated the related tools with just enough to enable the feature. I still need to adjust ext4's fsck's, although without access to the keys, neither fsck will be able to verify the hashes of casefolded and encrypted names.
v7 chances:
Moved dentry operations from unicode to libfs, added new iterator function to unicode to allow this.
Added libfs function for setting dentries to remove code duplication between ext4 and f2fs.
v6 changes:
Went back to using dentry_operations for casefolding. Provided standard implementations in fs/unicode, avoiding extra allocation in d_hash op. Moved fscrypt d_ops setting to be filesystem's responsibility to maintain compatibility with casefolding and overlayfs if casefolding is not used fixes some f2fs error handling
v4-5: patches submitted on fscrypt
v3 changes:
fscrypt patch only creates hash key if it will be needed. Rebased on top of fscrypt branch, reconstified match functions in ext4/f2fs
v2 changes:
fscrypt moved to separate thread to rebase on fscrypt dev branch addressed feedback, plus some minor fixes
Daniel Rosenberg (8):
unicode: Add utf8_casefold_iter
fs: Add standard casefolding support
f2fs: Use generic casefolding support
ext4: Use generic casefolding support
fscrypt: Have filesystems handle their d_ops
f2fs: Handle casefolding with Encryption
ext4: Hande casefolding with encryption
ext4: Optimize match for casefolded encrypted dirs
Documentation/filesystems/ext4/directory.rst | 27 ++ fs/crypto/fname.c | 7 +- fs/crypto/fscrypt_private.h | 1 - fs/crypto/hooks.c | 1 - fs/ext4/dir.c | 78 +---- fs/ext4/ext4.h | 93 ++++-- fs/ext4/hash.c | 26 +- fs/ext4/ialloc.c | 5 +- fs/ext4/inline.c | 41 ++- fs/ext4/namei.c | 325 ++++++++++++------- fs/ext4/super.c | 21 +- fs/f2fs/dir.c | 127 +++----- fs/f2fs/f2fs.h | 15 +- fs/f2fs/hash.c | 25 +- fs/f2fs/inline.c | 9 +- fs/f2fs/namei.c | 1 + fs/f2fs/super.c | 17 +- fs/f2fs/sysfs.c | 10 +- fs/libfs.c | 127 ++++++++ fs/ubifs/dir.c | 18 + fs/unicode/utf8-core.c | 25 +- include/linux/f2fs_fs.h | 3 - include/linux/fs.h | 24 ++ include/linux/fscrypt.h | 6 +- include/linux/unicode.h | 10 + 25 files changed, 671 insertions(+), 371 deletions(-)
Signature · 1 line
2.25.0.341.g760bfbb309-goog
PATCH v7 1/8 unicode: Add utf8_casefold_iter
This function will allow other uses of unicode to act upon a casefolded string without needing to allocate their own copy of one.
The actor function can return an nonzero value to exit early.
--- fs/unicode/utf8-core.c | 25 ++++++++++++++++++++++++- include/linux/unicode.h | 10 ++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-)
fs/unicode/utf8-core.c +24 -1
@@ -122,9 +122,32 @@ int utf8_casefold(const struct unicode_map *um, const struct qstr *str, }return -EINVAL ; } -EXPORT_SYMBOL (utf8_casefold); +int utf8_casefold_iter (const struct unicode_map *um,const struct qstr *str, +struct utf8_itr_context *ctx) +{ +const struct utf8data *data =utf8nfdicf (um->version); +struct utf8cursor cur; +int c; +int res =0 ; +int pos =0 ; + +if (utf8ncursor (&cur, data, str->name, str->len) <0 ) +return -EINVAL ; + +while ((c =utf8byte (&cur))) { +if (c <0 ) +return c; + res = ctx->actor (ctx, c, pos); + pos++; +if (res) +return res; + } +return res; +} +EXPORT_SYMBOL (utf8_casefold_iter); +int utf8_normalize (const struct unicode_map *um,const struct qstr *str,unsigned char *dest,size_t dlen) {
include/linux/unicode.h +10 -0
@@ -10,6 +10,13 @@ struct unicode_map {int version; }; +struct utf8_itr_context ; +typedef int (*utf8_itr_actor_t )(struct utf8_itr_context *,int byte,int pos); + +struct utf8_itr_context { +utf8_itr_actor_t actor; +}; +int utf8_validate (const struct unicode_map *um,const struct qstr *str); int utf8_strncmp(const struct unicode_map *um,
@@ -27,6 +34,9 @@ int utf8_normalize(const struct unicode_map *um, const struct qstr *str,int utf8_casefold (const struct unicode_map *um,const struct qstr *str,unsigned char *dest,size_t dlen); +int utf8_casefold_iter (const struct unicode_map *um,const struct qstr *str, +struct utf8_itr_context *ctx); +struct unicode_map *utf8_load (const char *version);void utf8_unload (struct unicode_map *um);
Signature · 1 line
2.25.0.341.g760bfbb309-goog
PATCH v7 2/8 fs: Add standard casefolding support
In reply to Daniel RosenbergThis 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.
--- fs/libfs.c | 77 ++++++++++++++++++++++++++++++++++++++++++++++ include/linux/fs.h | 22 +++++++++++++ 2 files changed, 99 insertions(+)
fs/libfs.c +77 -0
@@ -20,6 +20,9 @@#include <linux/fs_context.h> #include <linux/pseudo_fs.h> #include <linux/fsnotify.h> +#include <linux/unicode.h> +#include <linux/fscrypt.h> +#include <linux/stringhash.h> #include <linux/uaccess.h>
@@ -1361,3 +1364,77 @@ 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); + +struct hash_ctx { +struct utf8_itr_context ctx; +unsigned long hash; +}; + +static int do_generic_ci_hash (struct utf8_itr_context *ctx,int byte,int pos) +{ + struct hash_ctx *hctx = container_of(ctx, struct hash_ctx, ctx); + + hctx->hash =partial_name_hash ((unsigned char )byte, hctx->hash); +return 0 ; +} + +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 ; +struct hash_ctx hctx; + +if (!inode || !needs_casefold (inode)) +return 0 ; + + hctx.hash =init_name_hash (dentry); + hctx.ctx.actor = do_generic_ci_hash; + ret =utf8_casefold_iter (um, str, &hctx.ctx); +if (ret <0 ) + goto err; + str->hash =end_name_hash (hctx.hash); + +return 0 ; +err: +if (sb_has_enc_strict_mode (sb)) + ret = -EINVAL ; +return ret; +} +EXPORT_SYMBOL (generic_ci_d_hash); +#endif
include/linux/fs.h +22 -0
@@ -1382,6 +1382,12 @@ extern int send_sigurg(struct fown_struct *fown);#define SB_ACTIVE (1<<30)#define SB_NOUSER (1<<31) +/* These flags relate to encoding and casefolding */ +#define SB_ENC_STRICT_MODE_FL (1 << 0) + +#define sb_has_enc_strict_mode (sb) \ + (sb->s_encoding_flags & SB_ENC_STRICT_MODE_FL) +/* * Umount options */
@@ -1449,6 +1455,10 @@ struct super_block {#endif #ifdef CONFIG_FS_VERITY const struct fsverity_operations *s_vop; +#endif +#ifdef CONFIG_UNICODE +struct unicode_map *s_encoding; +__u16 s_encoding_flags;#endif struct hlist_bl_head s_roots;/* alternate root dentries for NFS */ struct list_head s_mounts;/* list of mounts; _not_ for fs use */
@@ -3361,6 +3371,18 @@ extern int generic_file_fsync(struct file *, loff_t, loff_t, int);extern int generic_check_addressable (unsigned ,u64 ); +#ifdef CONFIG_UNICODE +extern int generic_ci_d_hash (const struct dentry *dentry,struct qstr *str); +extern int generic_ci_d_compare (const struct dentry *dentry,unsigned int len, +const char *str,const struct qstr *name); +extern bool needs_casefold (const struct inode *dir); +#else +static inline bool needs_casefold (const struct inode *dir) +{ +return 0 ; +} +#endif +#ifdef CONFIG_MIGRATION extern int buffer_migrate_page(struct address_space *, struct page *, struct page *,
Signature · 1 line
2.25.0.341.g760bfbb309-goog
PATCH v7 3/8 f2fs: Use generic casefolding support
In reply to Daniel RosenbergThis switches f2fs over to the generic support provided in commit 65832afbeaaf ("fs: Add standard casefolding support")
--- fs/f2fs/dir.c | 63 +++++++---------------------------------- fs/f2fs/f2fs.h | 4 --- fs/f2fs/hash.c | 2 +- fs/f2fs/super.c | 10 +++---- fs/f2fs/sysfs.c | 10 ++++--- include/linux/f2fs_fs.h | 3 -- 6 files changed, 22 insertions(+), 70 deletions(-)
fs/f2fs/dir.c +10 -53
@@ -114,8 +114,8 @@ static struct f2fs_dir_entry *find_in_block(struct inode *dir,int f2fs_ci_compare (const struct inode *parent,const struct qstr *name,const struct qstr *entry,bool quick) { -const struct f2fs_sb_info *sbi =F2FS_SB (parent->i_sb); -const struct unicode_map *um = sbi->s_encoding; +const struct super_block *sb = parent->i_sb; +const struct unicode_map *um = sb->s_encoding;int ret;if (quick)
@@ -127,7 +127,7 @@ int f2fs_ci_compare(const struct inode *parent, const struct qstr *name,/* Handle invalid character sequence as either an error * or as an opaque byte sequence. */ -if (f2fs_has_strict_mode (sbi)) +if (sb_has_enc_strict_mode (sb))return -EINVAL ;if (name->len != entry->len)
@@ -154,7 +154,7 @@ static void f2fs_fname_setup_ci_filename(struct inode *dir,if (!cf_name->name)return ; - cf_name->len =utf8_casefold (sbi->s_encoding, + cf_name->len =utf8_casefold (dir->i_sb->s_encoding, iname, cf_name->name,F2FS_NAME_LEN );if ((int )cf_name->len <=0 ) {
@@ -173,7 +173,7 @@ static inline bool f2fs_match_name(struct f2fs_dentry_ptr *d, {#ifdef CONFIG_UNICODE struct inode *parent = d->inode; -struct f2fs_sb_info *sbi =F2FS_I_SB (parent); +struct super_block *sb = parent->i_sb;struct qstr entry;#endif
@@ -184,7 +184,7 @@ static inline bool f2fs_match_name(struct f2fs_dentry_ptr *d, entry.name = d->filename[bit_pos]; entry.len = de->name_len; -if (sbi->s_encoding &&IS_CASEFOLDED (parent)) { +if (sb->s_encoding &&IS_CASEFOLDED (parent)) {if (cf_str->name) {struct qstr cf = {.name = cf_str->name, .len = cf_str->len};
@@ -357,8 +357,8 @@ struct f2fs_dir_entry *f2fs_find_entry(struct inode *dir,int err;#ifdef CONFIG_UNICODE -if (f2fs_has_strict_mode (F2FS_I_SB (dir)) &&IS_CASEFOLDED (dir) && -utf8_validate (F2FS_I_SB (dir)->s_encoding, child)) { +if (sb_has_enc_strict_mode (dir->i_sb) &&IS_CASEFOLDED (dir) && +utf8_validate (dir->i_sb->s_encoding, child)) { *res_page =ERR_PTR (-EINVAL );return NULL ; }
@@ -1079,51 +1079,8 @@ const struct file_operations f2fs_dir_operations = { };#ifdef CONFIG_UNICODE -static int f2fs_d_compare (const struct dentry *dentry,unsigned int len, -const char *str,const struct qstr *name) -{ -struct qstr qstr = {.name = str, .len = len }; -const struct dentry *parent =READ_ONCE (dentry->d_parent); -const struct inode *inode =READ_ONCE (parent->d_inode); - -if (!inode || !IS_CASEFOLDED (inode)) { -if (len != name->len) -return -1 ; -return memcmp (str, name->name, len); - } - -return f2fs_ci_compare (inode, name, &qstr, false); -} - -static int f2fs_d_hash (const struct dentry *dentry,struct qstr *str) -{ -struct f2fs_sb_info *sbi =F2FS_SB (dentry->d_sb); -const struct unicode_map *um = sbi->s_encoding; -const struct inode *inode =READ_ONCE (dentry->d_inode); -unsigned char *norm; -int len, ret =0 ; - -if (!inode || !IS_CASEFOLDED (inode)) -return 0 ; - - norm =f2fs_kmalloc (sbi,PATH_MAX ,GFP_ATOMIC ); -if (!norm) -return -ENOMEM ; - - len =utf8_casefold (um, str, norm,PATH_MAX ); -if (len <0 ) { -if (f2fs_has_strict_mode (sbi)) - ret = -EINVAL ; - goto out; - } - str->hash =full_name_hash (dentry, norm, len); -out: -kvfree (norm); -return ret; -} -const struct dentry_operations f2fs_dentry_ops = { - .d_hash = f2fs_d_hash, - .d_compare = f2fs_d_compare, + .d_hash = generic_ci_d_hash, + .d_compare = generic_ci_d_compare, };#endif
fs/f2fs/f2fs.h +0 -4
@@ -1284,10 +1284,6 @@ struct f2fs_sb_info {int valid_super_block;/* valid super block no */ unsigned long s_flag;/* flags for sbi */ struct mutex writepages;/* mutex for writepages() */ -#ifdef CONFIG_UNICODE -struct unicode_map *s_encoding; -__u16 s_encoding_flags; -#endif #ifdef CONFIG_BLK_DEV_ZONED unsigned int blocks_per_blkz;/* F2FS blocks per zone */
fs/f2fs/hash.c +1 -1
@@ -110,7 +110,7 @@ f2fs_hash_t f2fs_dentry_hash(const struct inode *dir, {#ifdef CONFIG_UNICODE struct f2fs_sb_info *sbi =F2FS_SB (dir->i_sb); -const struct unicode_map *um = sbi->s_encoding; +const struct unicode_map *um = dir->i_sb->s_encoding;int r, dlen;unsigned char *buff;struct qstr folded;
fs/f2fs/super.c +5 -5
@@ -1213,7 +1213,7 @@ static void f2fs_put_super(struct super_block *sb)for (i =0 ; i <NR_PAGE_TYPE ; i++)kvfree (sbi->write_io[i]);#ifdef CONFIG_UNICODE -utf8_unload (sbi->s_encoding); +utf8_unload (sb->s_encoding);#endif kvfree (sbi); }
@@ -3223,7 +3223,7 @@ static int f2fs_scan_devices(struct f2fs_sb_info *sbi)static int f2fs_setup_casefold (struct f2fs_sb_info *sbi) {#ifdef CONFIG_UNICODE -if (f2fs_sb_has_casefold (sbi) && !sbi->s_encoding) { +if (f2fs_sb_has_casefold (sbi) && !sbi->sb->s_encoding) {const struct f2fs_sb_encodings *encoding_info;struct unicode_map *encoding;__u16 encoding_flags;
@@ -3254,8 +3254,8 @@ static int f2fs_setup_casefold(struct f2fs_sb_info *sbi)"%s-%s with flags 0x%hx" , encoding_info->name, encoding_info->version?:"\b", encoding_flags); - sbi->s_encoding = encoding; - sbi->s_encoding_flags = encoding_flags; + sbi->sb->s_encoding = encoding; + sbi->sb->s_encoding_flags = encoding_flags; sbi->sb->s_d_op = &f2fs_dentry_ops; }#else
@@ -3742,7 +3742,7 @@ static int f2fs_fill_super(struct super_block *sb, void *data, int silent)kvfree (sbi->write_io[i]);#ifdef CONFIG_UNICODE -utf8_unload (sbi->s_encoding); +utf8_unload (sb->s_encoding);#endif free_options: #ifdef CONFIG_QUOTA
fs/f2fs/sysfs.c +6 -4
@@ -175,12 +175,14 @@ static ssize_t encoding_show(struct f2fs_attr *a, struct f2fs_sb_info *sbi, char *buf) { #ifdef CONFIG_UNICODE + struct super_block *sb = sbi->sb; +if (f2fs_sb_has_casefold (sbi))return snprintf (buf,PAGE_SIZE ,"%s (%d.%d.%d)\n" , - sbi->s_encoding->charset, - (sbi->s_encoding->version >>16 ) &0xff , - (sbi->s_encoding->version >>8 ) &0xff , - sbi->s_encoding->version & 0xff); + sb->s_encoding->charset, + (sb->s_encoding->version >>16 ) &0xff , + (sb->s_encoding->version >>8 ) &0xff , + sb->s_encoding->version &0xff );#endif return sprintf (buf,"(none)" ); }
include/linux/f2fs_fs.h +0 -3
@@ -38,9 +38,6 @@#define F2FS_MAX_QUOTAS 3#define F2FS_ENC_UTF8_12_1 1 -#define F2FS_ENC_STRICT_MODE_FL (1 << 0) -#define f2fs_has_strict_mode (sbi) \ - (sbi->s_encoding_flags & F2FS_ENC_STRICT_MODE_FL)#define F2FS_IO_SIZE (sbi) (1 << F2FS_OPTION(sbi).write_io_size_bits)/* Blocks */ #define F2FS_IO_SIZE_KB (sbi) (1 << (F2FS_OPTION(sbi).write_io_size_bits + 2))/* KB */
Signature · 1 line
2.25.0.341.g760bfbb309-goog
PATCH v7 5/8 fscrypt: Have filesystems handle their d_ops
In reply to Daniel RosenbergThis shifts the responsibility of setting up dentry operations from fscrypt to the individual filesystems, allowing them to have their own operations while still setting fscrypt's d_revalidate as appropriate.
Also added helper function to libfs to unify ext4 and f2fs implementations.
--- fs/crypto/fname.c | 7 ++---- fs/crypto/fscrypt_private.h | 1 - fs/crypto/hooks.c | 1 - fs/ext4/dir.c | 7 ------ fs/ext4/namei.c | 1 + fs/ext4/super.c | 5 ---- fs/f2fs/dir.c | 7 ------ fs/f2fs/f2fs.h | 3 --- fs/f2fs/namei.c | 1 + fs/f2fs/super.c | 1 - fs/libfs.c | 50 +++++++++++++++++++++++++++++++++++++ fs/ubifs/dir.c | 18 +++++++++++++ include/linux/fs.h | 2 ++ include/linux/fscrypt.h | 6 +++-- 14 files changed, 78 insertions(+), 32 deletions(-)
fs/crypto/fname.c +2 -5
@@ -543,7 +543,7 @@ EXPORT_SYMBOL_GPL(fscrypt_fname_siphash);* Validate dentries in encrypted directories to make sure we aren't potentially * caching stale dentries after a key has been added. */ -static int fscrypt_d_revalidate (struct dentry *dentry,unsigned int flags) +int fscrypt_d_revalidate (struct dentry *dentry,unsigned int flags) {struct dentry *dir;int err;
@@ -582,7 +582,4 @@ static int fscrypt_d_revalidate(struct dentry *dentry, unsigned int flags)return valid; } - -const struct dentry_operations fscrypt_d_ops = { - .d_revalidate = fscrypt_d_revalidate, -}; +EXPORT_SYMBOL (fscrypt_d_revalidate);
fs/crypto/fscrypt_private.h +0 -1
@@ -250,7 +250,6 @@ extern int fscrypt_fname_encrypt(const struct inode *inode,extern bool fscrypt_fname_encrypted_size (const struct inode *inode,u32 orig_len,u32 max_len,u32 *encrypted_len_ret); -extern const struct dentry_operations fscrypt_d_ops;/* hkdf.c */
fs/crypto/hooks.c +0 -1
@@ -118,7 +118,6 @@ int __fscrypt_prepare_lookup(struct inode *dir, struct dentry *dentry,spin_lock (&dentry->d_lock); dentry->d_flags |=DCACHE_ENCRYPTED_NAME ;spin_unlock (&dentry->d_lock); -d_set_d_op (dentry, &fscrypt_d_ops); }return err; }
fs/ext4/dir.c +0 -7
@@ -665,10 +665,3 @@ const struct file_operations ext4_dir_operations = { .open = ext4_dir_open, .release = ext4_release_dir, }; - -#ifdef CONFIG_UNICODE -const struct dentry_operations ext4_dentry_ops = { - .d_hash = generic_ci_d_hash, - .d_compare = generic_ci_d_compare, -}; -#endif
fs/ext4/namei.c +1 -0
@@ -1614,6 +1614,7 @@ static struct buffer_head *ext4_lookup_entry(struct inode *dir,struct buffer_head *bh; err =ext4_fname_prepare_lookup (dir, dentry, &fname); +generic_set_encrypted_ci_d_ops (dir, dentry);if (err == -ENOENT )return NULL ;if (err)
fs/ext4/super.c +0 -5
@@ -4542,11 +4542,6 @@ static int ext4_fill_super(struct super_block *sb, void *data, int silent) goto failed_mount4; } -#ifdef CONFIG_UNICODE -if (sb->s_encoding) - sb->s_d_op = &ext4_dentry_ops; -#endif - sb->s_root =d_make_root (root);if (!sb->s_root) {ext4_msg (sb,KERN_ERR ,"get root dentry failed" );
fs/f2fs/dir.c +0 -7
@@ -1077,10 +1077,3 @@ const struct file_operations f2fs_dir_operations = { .compat_ioctl = f2fs_compat_ioctl,#endif }; - -#ifdef CONFIG_UNICODE -const struct dentry_operations f2fs_dentry_ops = { - .d_hash = generic_ci_d_hash, - .d_compare = generic_ci_d_compare, -}; -#endif
fs/f2fs/f2fs.h +0 -3
@@ -3626,9 +3626,6 @@ static inline void update_sit_info(struct f2fs_sb_info *sbi) {}#endif extern const struct file_operations f2fs_dir_operations; -#ifdef CONFIG_UNICODE -extern const struct dentry_operations f2fs_dentry_ops; -#endif extern const struct file_operations f2fs_file_operations;extern const struct inode_operations f2fs_file_inode_operations;extern const struct address_space_operations f2fs_dblock_aops;
fs/f2fs/namei.c +1 -0
@@ -494,6 +494,7 @@ static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry, } err =fscrypt_prepare_lookup (dir, dentry, &fname); +generic_set_encrypted_ci_d_ops (dir, dentry);if (err == -ENOENT ) goto out_splice;if (err)
fs/f2fs/super.c +0 -1
@@ -3256,7 +3256,6 @@ static int f2fs_setup_casefold(struct f2fs_sb_info *sbi) sbi->sb->s_encoding = encoding; sbi->sb->s_encoding_flags = encoding_flags; - sbi->sb->s_d_op = &f2fs_dentry_ops; }#else if (f2fs_sb_has_casefold (sbi)) {
fs/libfs.c +50 -0
54 lines not on this page · raw message
fs/ubifs/dir.c +18 -0
@@ -196,6 +196,7 @@ static int dbg_check_name(const struct ubifs_info *c,return 0 ; } +static void ubifs_set_d_ops (struct inode *dir,struct dentry *dentry);static struct dentry *ubifs_lookup (struct inode *dir,struct dentry *dentry,unsigned int flags) {
@@ -209,6 +210,7 @@ static struct dentry *ubifs_lookup(struct inode *dir, struct dentry *dentry,dbg_gen ("'%pd' in dir ino %lu" , dentry, dir->i_ino); err =fscrypt_prepare_lookup (dir, dentry, &nm); +ubifs_set_d_ops (dir, dentry);if (err == -ENOENT )return d_splice_alias (NULL , dentry);if (err)
@@ -1655,3 +1657,19 @@ const struct file_operations ubifs_dir_operations = { .compat_ioctl = ubifs_compat_ioctl,#endif }; + +#ifdef CONFIG_FS_ENCRYPTION +static const struct dentry_operations ubifs_encrypted_dentry_ops = { + .d_revalidate = fscrypt_d_revalidate, +}; +#endif + +static void ubifs_set_d_ops (struct inode *dir,struct dentry *dentry) +{ +#ifdef CONFIG_FS_ENCRYPTION +if (dentry->d_flags &DCACHE_ENCRYPTED_NAME ) { +d_set_d_op (dentry, &ubifs_encrypted_dentry_ops); +return ; + } +#endif +}
include/linux/fs.h +2 -0
@@ -3382,6 +3382,8 @@ static inline bool needs_casefold(const struct inode *dir)return 0 ; }#endif +extern void generic_set_encrypted_ci_d_ops (struct inode *dir, +struct dentry *dentry);#ifdef CONFIG_MIGRATION extern int buffer_migrate_page(struct address_space *,
include/linux/fscrypt.h +4 -2
@@ -134,6 +134,7 @@ static inline struct page *fscrypt_pagecache_page(struct page *bounce_page) }extern void fscrypt_free_bounce_page (struct page *bounce_page); +extern int fscrypt_d_revalidate (struct dentry *dentry,unsigned int flags);/* policy.c */ extern int fscrypt_ioctl_set_policy (struct file *,const void __user *);
@@ -595,8 +596,9 @@ static inline int fscrypt_prepare_rename(struct inode *old_dir,* filenames are presented in encrypted form. Therefore, we'll try to set up * the directory's encryption key, but even without it the lookup can continue. * -* This also installs a custom ->d_revalidate() method which will invalidate the -* dentry if it was created without the key and the key is later added. +* After calling this function, a filesystem should ensure that it's dentry +* operations contain fscrypt_d_revalidate if DCACHE_ENCRYPTED_NAME was set, +* so that the dentry can be invalidated if the key is later added. * * Return: 0 on success; -ENOENT if key is unavailable but the filename isn't a * correctly formed encoded ciphertext name, so a negative dentry should be
Signature · 1 line
2.25.0.341.g760bfbb309-goog
PATCH v7 4/8 ext4: Use generic casefolding support
In reply to Daniel RosenbergThis switches ext4 over to the generic support provided in commit 65832afbeaaf ("fs: Add standard casefolding support")
--- fs/ext4/dir.c | 48 ++---------------------------------------------- fs/ext4/ext4.h | 12 ------------ fs/ext4/hash.c | 2 +- fs/ext4/namei.c | 20 ++++++++------------ fs/ext4/super.c | 12 ++++++------ 5 files changed, 17 insertions(+), 77 deletions(-)
fs/ext4/dir.c +2 -46
@@ -667,52 +667,8 @@ const struct file_operations ext4_dir_operations = { };#ifdef CONFIG_UNICODE -static int ext4_d_compare (const struct dentry *dentry,unsigned int len, -const char *str,const struct qstr *name) -{ -struct qstr qstr = {.name = str, .len = len }; -const struct dentry *parent =READ_ONCE (dentry->d_parent); -const struct inode *inode =READ_ONCE (parent->d_inode); - -if (!inode || !IS_CASEFOLDED (inode) || - !EXT4_SB (inode->i_sb)->s_encoding) { -if (len != name->len) -return -1 ; -return memcmp (str, name->name, len); - } - -return ext4_ci_compare (inode, name, &qstr, false); -} - -static int ext4_d_hash (const struct dentry *dentry,struct qstr *str) -{ -const struct ext4_sb_info *sbi =EXT4_SB (dentry->d_sb); -const struct unicode_map *um = sbi->s_encoding; -const struct inode *inode =READ_ONCE (dentry->d_inode); -unsigned char *norm; -int len, ret =0 ; - -if (!inode || !IS_CASEFOLDED (inode) || !um) -return 0 ; - - norm =kmalloc (PATH_MAX ,GFP_ATOMIC ); -if (!norm) -return -ENOMEM ; - - len =utf8_casefold (um, str, norm,PATH_MAX ); -if (len <0 ) { -if (ext4_has_strict_mode (sbi)) - ret = -EINVAL ; - goto out; - } - str->hash =full_name_hash (dentry, norm, len); -out: -kfree (norm); -return ret; -} -const struct dentry_operations ext4_dentry_ops = { - .d_hash = ext4_d_hash, - .d_compare = ext4_d_compare, + .d_hash = generic_ci_d_hash, + .d_compare = generic_ci_d_compare, };#endif
fs/ext4/ext4.h +0 -12
@@ -1372,14 +1372,6 @@ struct ext4_super_block {#define EXT4_ENC_UTF8_12_1 1 -/* -* Flags for ext4_sb_info.s_encoding_flags. -*/ -#define EXT4_ENC_STRICT_MODE_FL (1 << 0) - -#define ext4_has_strict_mode (sbi) \ - (sbi->s_encoding_flags & EXT4_ENC_STRICT_MODE_FL) -/* * fourth extended-fs super-block data in memory */
@@ -1429,10 +1421,6 @@ struct ext4_sb_info {struct kobject s_kobj;struct completion s_kobj_unregister;struct super_block *s_sb; -#ifdef CONFIG_UNICODE -struct unicode_map *s_encoding; -__u16 s_encoding_flags; -#endif /* Journaling */ struct journal_s *s_journal;
fs/ext4/hash.c +1 -1
@@ -275,7 +275,7 @@ int ext4fs_dirhash(const struct inode *dir, const char *name, int len, struct dx_hash_info *hinfo) {#ifdef CONFIG_UNICODE -const struct unicode_map *um =EXT4_SB (dir->i_sb)->s_encoding; +const struct unicode_map *um = dir->i_sb->s_encoding;int r, dlen;unsigned char *buff;struct qstr qstr = {.name = name, .len = len };
fs/ext4/namei.c +8 -12
@@ -1286,8 +1286,8 @@ static void dx_insert_block(struct dx_frame *frame, u32 hash, ext4_lblk_t block)int ext4_ci_compare (const struct inode *parent,const struct qstr *name,const struct qstr *entry,bool quick) { -const struct ext4_sb_info *sbi =EXT4_SB (parent->i_sb); -const struct unicode_map *um = sbi->s_encoding; +const struct super_block *sb = parent->i_sb; +const struct unicode_map *um = sb->s_encoding;int ret;if (quick)
@@ -1299,7 +1299,7 @@ int ext4_ci_compare(const struct inode *parent, const struct qstr *name,/* Handle invalid character sequence as either an error * or as an opaque byte sequence. */ -if (ext4_has_strict_mode (sbi)) +if (sb_has_enc_strict_mode (sb))return -EINVAL ;if (name->len != entry->len)
@@ -1316,7 +1316,7 @@ void ext4_fname_setup_ci_filename(struct inode *dir, const struct qstr *iname, {int len; -if (!IS_CASEFOLDED (dir) || !EXT4_SB (dir->i_sb)->s_encoding) { +if (!needs_casefold (dir)) { cf_name->name =NULL ;return ; }
@@ -1325,7 +1325,7 @@ void ext4_fname_setup_ci_filename(struct inode *dir, const struct qstr *iname,if (!cf_name->name)return ; - len =utf8_casefold (EXT4_SB (dir->i_sb)->s_encoding, + len =utf8_casefold (dir->i_sb->s_encoding, iname, cf_name->name,EXT4_NAME_LEN );if (len <=0 ) {
@@ -1362,7 +1362,7 @@ static inline bool ext4_match(const struct inode *parent,#endif #ifdef CONFIG_UNICODE -if (EXT4_SB (parent->i_sb)->s_encoding &&IS_CASEFOLDED (parent)) { +if (needs_casefold (parent)) {if (fname->cf_name.name) {struct qstr cf = {.name = fname->cf_name.name, .len = fname->cf_name.len};
@@ -2170,9 +2170,6 @@ static int ext4_add_entry(handle_t *handle, struct dentry *dentry,struct buffer_head *bh =NULL ;struct ext4_dir_entry_2 *de;struct super_block *sb; -#ifdef CONFIG_UNICODE -struct ext4_sb_info *sbi; -#endif struct ext4_filename fname;int retval;int dx_fallback=0 ;
@@ -2189,9 +2186,8 @@ static int ext4_add_entry(handle_t *handle, struct dentry *dentry,return -EINVAL ;#ifdef CONFIG_UNICODE - sbi =EXT4_SB (sb); -if (ext4_has_strict_mode (sbi) &&IS_CASEFOLDED (dir) && - sbi->s_encoding &&utf8_validate (sbi->s_encoding, &dentry->d_name)) +if (sb_has_enc_strict_mode (sb) &&IS_CASEFOLDED (dir) && + sb->s_encoding &&utf8_validate (sb->s_encoding, &dentry->d_name))return -EINVAL ;#endif
fs/ext4/super.c +6 -6
@@ -1102,7 +1102,7 @@ static void ext4_put_super(struct super_block *sb)kfree (sbi->s_blockgroup_lock);fs_put_dax (sbi->s_daxdev);#ifdef CONFIG_UNICODE -utf8_unload (sbi->s_encoding); +utf8_unload (sb->s_encoding);#endif kfree (sbi); }
@@ -3896,7 +3896,7 @@ static int ext4_fill_super(struct super_block *sb, void *data, int silent) goto failed_mount;#ifdef CONFIG_UNICODE -if (ext4_has_feature_casefold (sb) && !sbi->s_encoding) { +if (ext4_has_feature_casefold (sb) && !sb->s_encoding) {const struct ext4_sb_encodings *encoding_info;struct unicode_map *encoding;__u16 encoding_flags;
@@ -3927,8 +3927,8 @@ static int ext4_fill_super(struct super_block *sb, void *data, int silent)"%s-%s with flags 0x%hx" , encoding_info->name, encoding_info->version?:"\b", encoding_flags); - sbi->s_encoding = encoding; - sbi->s_encoding_flags = encoding_flags; + sb->s_encoding = encoding; + sb->s_encoding_flags = encoding_flags; }#endif
@@ -4543,7 +4543,7 @@ static int ext4_fill_super(struct super_block *sb, void *data, int silent) }#ifdef CONFIG_UNICODE -if (sbi->s_encoding) +if (sb->s_encoding) sb->s_d_op = &ext4_dentry_ops;#endif
@@ -4729,7 +4729,7 @@ static int ext4_fill_super(struct super_block *sb, void *data, int silent)crypto_free_shash (sbi->s_chksum_driver);#ifdef CONFIG_UNICODE -utf8_unload (sbi->s_encoding); +utf8_unload (sb->s_encoding);#endif #ifdef CONFIG_QUOTA
Signature · 1 line
2.25.0.341.g760bfbb309-goog
PATCH v7 6/8 f2fs: Handle casefolding with Encryption
In reply to Daniel RosenbergThis expands f2fs's casefolding support to include encrypted directories. For encrypted directories, we use the siphash of the casefolded name. This ensures there is no direct way to go from an unencrypted name to the stored hash on disk without knowledge of the encryption policy keys.
Additionally, we switch to using the vfs layer's casefolding support instead of storing this information inside of f2fs's private data.
--- fs/f2fs/dir.c | 65 ++++++++++++++++++++++++++++++++---------------- fs/f2fs/f2fs.h | 8 +++--- fs/f2fs/hash.c | 23 +++++++++++------ fs/f2fs/inline.c | 9 ++++--- fs/f2fs/super.c | 6 ----- 5 files changed, 68 insertions(+), 43 deletions(-)
fs/f2fs/dir.c +43 -22
@@ -112,30 +112,50 @@ static struct f2fs_dir_entry *find_in_block(struct inode *dir,* doesn't match or less than zero on error. */ int f2fs_ci_compare (const struct inode *parent,const struct qstr *name, -const struct qstr *entry,bool quick) +unsigned char *name2,size_t len,bool quick) {const struct super_block *sb = parent->i_sb;const struct unicode_map *um = sb->s_encoding; +const struct fscrypt_str crypt_entry =FSTR_INIT (name2, len); +struct fscrypt_str decrypted_entry; +struct qstr decrypted; +struct qstr entry =QSTR_INIT (name2, len); +struct qstr *tocheck;int ret; + decrypted_entry.name =NULL ; + +if (IS_ENCRYPTED (parent) &&fscrypt_has_encryption_key (parent)) { + decrypted_entry.name =kmalloc (len,GFP_ATOMIC ); + decrypted.name = decrypted_entry.name; + decrypted_entry.len = len; + decrypted.len = len; +if (!decrypted.name) +return -ENOMEM ; + ret =fscrypt_fname_disk_to_usr (parent,0 ,0 , &crypt_entry, + &decrypted_entry); +if (ret <0 ) + goto out; + } + tocheck = decrypted_entry.name ? &decrypted : &entry; +if (quick) - ret =utf8_strncasecmp_folded (um, name, entry); + ret =utf8_strncasecmp_folded (um, name, tocheck);else - ret =utf8_strncasecmp (um, name, entry); - + ret =utf8_strncasecmp (um, name, tocheck);if (ret <0 ) {/* Handle invalid character sequence as either an error * or as an opaque byte sequence. */ if (sb_has_enc_strict_mode (sb)) -return -EINVAL ; - -if (name->len != entry->len) -return 1 ; - -return !!memcmp (name->name, entry->name, name->len); + ret = -EINVAL ; +else if (name->len != len) + ret =1 ; +else + ret = !!memcmp (name->name, tocheck->name, len); } - +out: +kfree (decrypted_entry.name);return ret; }
@@ -173,24 +193,24 @@ static inline bool f2fs_match_name(struct f2fs_dentry_ptr *d, {#ifdef CONFIG_UNICODE struct inode *parent = d->inode; -struct super_block *sb = parent->i_sb; -struct qstr entry; +unsigned char *name; +int len;#endif if (de->hash_code != namehash)return false;#ifdef CONFIG_UNICODE - entry.name = d->filename[bit_pos]; - entry.len = de->name_len; + name = d->filename[bit_pos]; + len = de->name_len; -if (sb->s_encoding &&IS_CASEFOLDED (parent)) { +if (needs_casefold (parent)) {if (cf_str->name) {struct qstr cf = {.name = cf_str->name, .len = cf_str->len}; -return !f2fs_ci_compare (parent, &cf, &entry, true); +return !f2fs_ci_compare (parent, &cf, name, len, true); } -return !f2fs_ci_compare (parent, fname->usr_fname, &entry, +return !f2fs_ci_compare (parent, fname->usr_fname, name, len, false); }#endif
@@ -616,13 +636,13 @@ void f2fs_update_dentry(nid_t ino, umode_t mode, struct f2fs_dentry_ptr *d,int f2fs_add_regular_entry (struct inode *dir,const struct qstr *new_name,const struct qstr *orig_name, +f2fs_hash_t dentry_hash,struct inode *inode,nid_t ino,umode_t mode) {unsigned int bit_pos;unsigned int level;unsigned int current_depth;unsigned long bidx, block; -f2fs_hash_t dentry_hash;unsigned int nbucket, nblock;struct page *dentry_page =NULL ;struct f2fs_dentry_block *dentry_blk =NULL ;
@@ -632,7 +652,6 @@ int f2fs_add_regular_entry(struct inode *dir, const struct qstr *new_name, level =0 ; slots =GET_DENTRY_SLOTS (new_name->len); - dentry_hash =f2fs_dentry_hash (dir, new_name,NULL ); current_depth =F2FS_I (dir)->i_current_depth;if (F2FS_I (dir)->chash == dentry_hash) {
@@ -718,17 +737,19 @@ int f2fs_add_dentry(struct inode *dir, struct fscrypt_name *fname, struct inode *inode, nid_t ino, umode_t mode) { struct qstr new_name; +f2fs_hash_t dentry_hash;int err = -EAGAIN ; new_name.name =fname_name (fname); new_name.len =fname_len (fname);if (f2fs_has_inline_dentry (dir)) - err =f2fs_add_inline_entry (dir, &new_name, fname->usr_fname, + err =f2fs_add_inline_entry (dir, &new_name, fname, inode, ino, mode); + dentry_hash =f2fs_dentry_hash (dir, &new_name, fname);if (err == -EAGAIN ) err =f2fs_add_regular_entry (dir, &new_name, fname->usr_fname, - inode, ino, mode); + dentry_hash, inode, ino, mode);f2fs_update_time (F2FS_I_SB (dir),REQ_TIME );return err;
fs/f2fs/f2fs.h +4 -4
@@ -3075,7 +3075,7 @@ struct dentry *f2fs_get_parent(struct dentry *child);extern int f2fs_ci_compare (const struct inode *parent,const struct qstr *name, -const struct qstr *entry, +unsigned char *name2,size_t len,bool quick);/*
@@ -3111,7 +3111,7 @@ void f2fs_update_dentry(nid_t ino, umode_t mode, struct f2fs_dentry_ptr *d, const struct qstr *name, f2fs_hash_t name_hash, unsigned int bit_pos);int f2fs_add_regular_entry (struct inode *dir,const struct qstr *new_name, -const struct qstr *orig_name, +const struct qstr *orig_name,f2fs_hash_t dentry_hash,struct inode *inode,nid_t ino,umode_t mode);int f2fs_add_dentry (struct inode *dir,struct fscrypt_name *fname,struct inode *inode,nid_t ino,umode_t mode);
@@ -3144,7 +3144,7 @@ int f2fs_sanity_check_ckpt(struct f2fs_sb_info *sbi);* hash.c */ f2fs_hash_t f2fs_dentry_hash (const struct inode *dir, -const struct qstr *name_info,struct fscrypt_name *fname); +const struct qstr *name_info,const struct fscrypt_name *fname);/* * node.c
@@ -3656,7 +3656,7 @@ struct f2fs_dir_entry *f2fs_find_in_inline_dir(struct inode *dir,int f2fs_make_empty_inline_dir (struct inode *inode,struct inode *parent,struct page *ipage);int f2fs_add_inline_entry (struct inode *dir,const struct qstr *new_name, -const struct qstr *orig_name, +const struct fscrypt_name *fname,struct inode *inode,nid_t ino,umode_t mode); void f2fs_delete_inline_entry(struct f2fs_dir_entry *dentry, struct page *page, struct inode *dir,
fs/f2fs/hash.c +16 -7
@@ -68,8 +68,9 @@ static void str2hashbuf(const unsigned char *msg, size_t len, *buf++ = pad; } -static f2fs_hash_t __f2fs_dentry_hash (const struct qstr *name_info, -struct fscrypt_name *fname) +static f2fs_hash_t __f2fs_dentry_hash (const struct inode *dir, +const struct qstr *name_info, +const struct fscrypt_name *fname) {__u32 hash;f2fs_hash_t f2fs_hash;
@@ -85,6 +86,11 @@ static f2fs_hash_t __f2fs_dentry_hash(const struct qstr *name_info,if (is_dot_dotdot (name_info))return 0 ; +if (IS_CASEFOLDED (dir) &&IS_ENCRYPTED (dir)) { + f2fs_hash =fscrypt_fname_siphash (dir, name_info); +return f2fs_hash; + } +/* Initialize the default seed for the hash checksum functions */ buf[0 ] =0x67452301 ; buf[1 ] =0xefcdab89 ;
@@ -106,7 +112,7 @@ static f2fs_hash_t __f2fs_dentry_hash(const struct qstr *name_info, }f2fs_hash_t f2fs_dentry_hash (const struct inode *dir, -const struct qstr *name_info,struct fscrypt_name *fname) +const struct qstr *name_info,const struct fscrypt_name *fname) {#ifdef CONFIG_UNICODE struct f2fs_sb_info *sbi =F2FS_SB (dir->i_sb);
@@ -114,27 +120,30 @@ f2fs_hash_t f2fs_dentry_hash(const struct inode *dir,int r, dlen;unsigned char *buff;struct qstr folded; +const struct qstr *name = fname ? fname->usr_fname : name_info;if (!name_info->len || !IS_CASEFOLDED (dir)) goto opaque_seq; +if (IS_ENCRYPTED (dir) && !fscrypt_has_encryption_key (dir)) + goto opaque_seq; + buff =f2fs_kzalloc (sbi,sizeof (char ) *PATH_MAX ,GFP_KERNEL );if (!buff)return -ENOMEM ; - - dlen =utf8_casefold (um, name_info, buff,PATH_MAX ); + dlen =utf8_casefold (um, name, buff,PATH_MAX );if (dlen <0 ) {kvfree (buff); goto opaque_seq; } folded.name = buff; folded.len = dlen; - r =__f2fs_dentry_hash (&folded, fname); + r =__f2fs_dentry_hash (dir, &folded, fname);kvfree (buff);return r; opaque_seq:#endif -return __f2fs_dentry_hash (name_info, fname); +return __f2fs_dentry_hash (dir, name_info, fname); }
fs/f2fs/inline.c +5 -4
@@ -465,8 +465,8 @@ static int f2fs_add_inline_entries(struct inode *dir, void *inline_dentry) ino =le32_to_cpu (de->ino); fake_mode =f2fs_get_de_type (de) <<S_SHIFT ; - err =f2fs_add_regular_entry (dir, &new_name,NULL ,NULL , - ino, fake_mode); + err =f2fs_add_regular_entry (dir, &new_name,NULL , + de->hash_code,NULL , ino, fake_mode);if (err) goto punch_dentry_pages;
@@ -578,7 +578,7 @@ int f2fs_try_convert_inline_dir(struct inode *dir, struct dentry *dentry) }int f2fs_add_inline_entry (struct inode *dir,const struct qstr *new_name, -const struct qstr *orig_name, +const struct fscrypt_name *fname,struct inode *inode,nid_t ino,umode_t mode) {struct f2fs_sb_info *sbi =F2FS_I_SB (dir);
@@ -589,6 +589,7 @@ int f2fs_add_inline_entry(struct inode *dir, const struct qstr *new_name,struct f2fs_dentry_ptr d;int slots =GET_DENTRY_SLOTS (new_name->len);struct page *page =NULL ; +const struct qstr *orig_name = fname->usr_fname;int err =0 ; ipage =f2fs_get_node_page (sbi, dir->i_ino);
@@ -619,7 +620,7 @@ int f2fs_add_inline_entry(struct inode *dir, const struct qstr *new_name,f2fs_wait_on_page_writeback (ipage,NODE , true, true); - name_hash =f2fs_dentry_hash (dir, new_name,NULL ); + name_hash =f2fs_dentry_hash (dir, new_name, fname);f2fs_update_dentry (ino, mode, &d, new_name, name_hash, bit_pos);set_page_dirty (ipage);
fs/f2fs/super.c +0 -6
@@ -3228,12 +3228,6 @@ static int f2fs_setup_casefold(struct f2fs_sb_info *sbi)struct unicode_map *encoding;__u16 encoding_flags; -if (f2fs_sb_has_encrypt (sbi)) { -f2fs_err (sbi, -"Can't mount with encoding and encryption" ); -return -EINVAL ; - } -if (f2fs_sb_read_encoding (sbi->raw_super, &encoding_info, &encoding_flags)) { f2fs_err(sbi,
Signature · 1 line
2.25.0.341.g760bfbb309-goog
PATCH v7 7/8 ext4: Hande casefolding with encryption
In reply to Daniel RosenbergThis adds support for encryption with casefolding.
Since the name on disk is case preserving, and also encrypted, we can no longer just recompute the hash on the fly. Additionally, to avoid leaking extra information from the hash of the unencrypted name, we use siphash via an fscrypt v2 policy.
The hash is stored at the end of the directory entry for all entries inside of an encrypted and casefolded directory apart from those that deal with '.' and '..'. This way, the change is backwards compatible with existing ext4 filesystems.
--- Documentation/filesystems/ext4/directory.rst | 27 ++ fs/ext4/dir.c | 27 +- fs/ext4/ext4.h | 64 +++- fs/ext4/hash.c | 24 +- fs/ext4/ialloc.c | 5 +- fs/ext4/inline.c | 41 +-- fs/ext4/namei.c | 291 +++++++++++++------ fs/ext4/super.c | 6 - 8 files changed, 343 insertions(+), 142 deletions(-)
Documentation/filesystems/ext4/directory.rst +27 -0
@@ -121,6 +121,31 @@ The directory file type is one of the following values: * - 0x7 - Symbolic link. +To support directories that are both encrypted and casefolded directories, we +must also include hash information in the directory entry. We append +``ext4_extended_dir_entry_2`` to ``ext4_dir_entry_2`` except for the entries +for dot and dotdot, which are kept the same. The structure follows immediately +after ``name`` and is included in the size listed by ``rec_len`` If a directory +entry uses this extension, it may be up to 271 bytes. + +.. list-table:: + :widths: 8 8 24 40 + :header-rows: 1 + + * - Offset + - Size + - Name + - Description + * - 0x0 + - \_\_le32 + - hash + - The hash of the directory name + * - 0x4 + - \_\_le32 + - minor\_hash + - The minor hash of the directory name + + In order to add checksums to these classic directory blocks, a phony ``struct ext4_dir_entry`` is placed at the end of each leaf block to hold the checksum. The directory entry is 12 bytes long. The inode
@@ -322,6 +347,8 @@ The directory hash is one of the following values: - Half MD4, unsigned. * - 0x5 - Tea, unsigned. + * - 0x6 + - Siphash. Interior nodes of an htree are recorded as ``struct dx_node``, which is also the full length of a data block:
fs/ext4/dir.c +17 -10
@@ -30,6 +30,8 @@#include "ext4.h" #include "xattr.h" +#define DOTDOT_OFFSET 12 +static int ext4_dx_readdir (struct file *,struct dir_context *);/**
@@ -67,22 +69,26 @@ int __ext4_check_dir_entry(const char *function, unsigned int line, struct inode *dir, struct file *filp, struct ext4_dir_entry_2 *de, struct buffer_head *bh, char *buf, int size, + ext4_lblk_t lblk, unsigned int offset) { const char *error_msg = NULL;const int rlen =ext4_rec_len_from_disk (de->rec_len, dir->i_sb->s_blocksize);const int next_offset = ((char *) de - buf) + rlen; +bool fake = (lblk ==0 ) && (offset <=DOTDOT_OFFSET ); -if (unlikely (rlen <EXT4_DIR_REC_LEN (1 ))) +if (unlikely (rlen <ext4_dir_rec_len (1 , fake ?NULL : dir))) error_msg ="rec_len is smaller than minimal" ;else if (unlikely (rlen %4 !=0 )) error_msg ="rec_len % 4 != 0" ; -else if (unlikely (rlen <EXT4_DIR_REC_LEN (de->name_len))) +else if (unlikely (rlen <ext4_dir_rec_len (de->name_len, + fake ?NULL : dir))) error_msg ="rec_len is too small for name_len" ;else if (unlikely (next_offset > size)) error_msg ="directory entry overrun" ; -else if (unlikely (next_offset > size -EXT4_DIR_REC_LEN (1 ) && +else if (unlikely (next_offset > size -ext4_dir_rec_len (1 , + fake ?NULL : dir) && next_offset != size)) error_msg ="directory entry too close to block end" ; else if (unlikely(le32_to_cpu(de->inode) >
@@ -94,15 +100,15 @@ int __ext4_check_dir_entry(const char *function, unsigned int line,if (filp)ext4_error_file (filp, function, line, bh->b_blocknr,"bad entry in directory: %s - offset=%u, " -"inode=%u, rec_len=%d, name_len=%d, size=%d" , +"inode=%u, rec_len=%d, lblk=%d, size=%d" , error_msg, offset,le32_to_cpu (de->inode), - rlen, de->name_len, size); + rlen, lblk, size);else ext4_error_inode (dir, function, line, bh->b_blocknr,"bad entry in directory: %s - offset=%u, " -"inode=%u, rec_len=%d, name_len=%d, size=%d" , +"inode=%u, rec_len=%d, lblk=%d, size=%d" , error_msg, offset,le32_to_cpu (de->inode), - rlen, de->name_len, size); + rlen, lblk, size);return 1 ; }
@@ -224,7 +230,8 @@ static int ext4_readdir(struct file *file, struct dir_context *ctx)* failure will be detected in the * dirent test below. */ if (ext4_rec_len_from_disk (de->rec_len, - sb->s_blocksize) <EXT4_DIR_REC_LEN (1 )) + sb->s_blocksize) <ext4_dir_rec_len (1 , + inode))break ; i +=ext4_rec_len_from_disk (de->rec_len, sb->s_blocksize);
@@ -240,7 +247,7 @@ static int ext4_readdir(struct file *file, struct dir_context *ctx) de = (struct ext4_dir_entry_2 *) (bh->b_data + offset);if (ext4_check_dir_entry (inode, file, de, bh, bh->b_data, bh->b_size, - offset)) { + map.m_lblk, offset)) {/* * On error, skip to the next block */
@@ -641,7 +648,7 @@ int ext4_check_all_de(struct inode *dir, struct buffer_head *bh, void *buf, top = buf + buf_size;while ((char *) de < top) {if (ext4_check_dir_entry (dir,NULL , de, bh, - buf, buf_size, offset)) + buf, buf_size,0 , offset))return -EFSCORRUPTED ; rlen =ext4_rec_len_from_disk (de->rec_len, buf_size); de = (struct ext4_dir_entry_2 *)((char *)de + rlen);
fs/ext4/ext4.h +56 -8
131 lines not on this page · raw message
fs/ext4/hash.c +20 -4
@@ -197,7 +197,7 @@ static void str2hashbuf_unsigned(const char *msg, int len, __u32 *buf, int num)* represented, and whether or not the returned hash is 32 bits or 64 * bits. 32 bit hashes will return 0 for the minor hash. */ -static int __ext4fs_dirhash (const char *name,int len, +static int __ext4fs_dirhash (const struct inode *dir,const char *name,int len,struct dx_hash_info *hinfo) {__u32 hash;
@@ -259,6 +259,22 @@ static int __ext4fs_dirhash(const char *name, int len, hash = buf[0 ]; minor_hash = buf[1 ];break ; +case DX_HASH_SIPHASH : + { +struct qstr qname =QSTR_INIT (name, len); +__u64 combined_hash; + +if (fscrypt_has_encryption_key (dir)) { + combined_hash =fscrypt_fname_siphash (dir, &qname); + }else { +ext4_warning_inode (dir,"Siphash requires key" ); +return -1 ; + } + + hash = (__u32)(combined_hash >>32 ); + minor_hash = (__u32 )combined_hash; +break ; + }default : hinfo->hash =0 ;return -1 ;
@@ -280,7 +296,7 @@ int ext4fs_dirhash(const struct inode *dir, const char *name, int len,unsigned char *buff;struct qstr qstr = {.name = name, .len = len }; -if (len &&IS_CASEFOLDED (dir) && um) { +if (len &&needs_casefold (dir) && um) { buff =kzalloc (sizeof (char ) *PATH_MAX ,GFP_KERNEL );if (!buff)return -ENOMEM ;
@@ -291,12 +307,12 @@ int ext4fs_dirhash(const struct inode *dir, const char *name, int len, goto opaque_seq; } - r =__ext4fs_dirhash (buff, dlen, hinfo); + r =__ext4fs_dirhash (dir, buff, dlen, hinfo);kfree (buff);return r; } opaque_seq: #endif -return __ext4fs_dirhash (name, len, hinfo); +return __ext4fs_dirhash (dir, name, len, hinfo); }
fs/ext4/ialloc.c +4 -1
@@ -451,7 +451,10 @@ static int find_group_orlov(struct super_block *sb, struct inode *parent,int ret =-1 ;if (qstr) { - hinfo.hash_version =DX_HASH_HALF_MD4 ; +if (ext4_hash_in_dirent (parent)) + hinfo.hash_version =DX_HASH_SIPHASH ; +else + hinfo.hash_version =DX_HASH_HALF_MD4 ; hinfo.seed = sbi->s_hash_seed;ext4fs_dirhash (parent, qstr->name, qstr->len, &hinfo); grp = hinfo.hash;
fs/ext4/inline.c +23 -18
130 lines not on this page · raw message
fs/ext4/namei.c +196 -95
806 lines not on this page · raw message
fs/ext4/super.c +0 -6
@@ -3901,12 +3901,6 @@ static int ext4_fill_super(struct super_block *sb, void *data, int silent)struct unicode_map *encoding;__u16 encoding_flags; -if (ext4_has_feature_encrypt (sb)) { -ext4_msg (sb,KERN_ERR , -"Can't mount with encoding and encryption" ); - goto failed_mount; - } -if (ext4_sb_read_encoding (es, &encoding_info, &encoding_flags)) { ext4_msg(sb, KERN_ERR,
Signature · 1 line
2.25.0.341.g760bfbb309-goog
PATCH v7 8/8 ext4: Optimize match for casefolded encrypted dirs
In reply to Daniel RosenbergMatching names with casefolded encrypting directories requires decrypting entries to confirm case since we are case preserving. We can avoid needing to decrypt if our hash values don't match.
--- fs/ext4/ext4.h | 17 ++++++++------- fs/ext4/namei.c | 55 ++++++++++++++++++++++++++----------------------- 2 files changed, 38 insertions(+), 34 deletions(-)
fs/ext4/ext4.h +9 -8
@@ -2447,9 +2447,9 @@ extern unsigned ext4_free_clusters_after_init(struct super_block *sb,ext4_fsblk_t ext4_inode_to_goal_block (struct inode *);#ifdef CONFIG_UNICODE -extern void ext4_fname_setup_ci_filename (struct inode *dir, +extern int ext4_fname_setup_ci_filename (struct inode *dir,const struct qstr *iname, -struct fscrypt_str *fname); +struct ext4_filename *fname);#endif #ifdef CONFIG_FS_ENCRYPTION
@@ -2480,9 +2480,9 @@ static inline int ext4_fname_setup_filename(struct inode *dir,ext4_fname_from_fscrypt_name (fname, &name);#ifdef CONFIG_UNICODE -ext4_fname_setup_ci_filename (dir, iname, &fname->cf_name); + err =ext4_fname_setup_ci_filename (dir, iname, fname);#endif -return 0 ; +return err; } static inline int ext4_fname_prepare_lookup(struct inode *dir,
@@ -2499,9 +2499,9 @@ static inline int ext4_fname_prepare_lookup(struct inode *dir,ext4_fname_from_fscrypt_name (fname, &name);#ifdef CONFIG_UNICODE -ext4_fname_setup_ci_filename (dir, &dentry->d_name, &fname->cf_name); + err =ext4_fname_setup_ci_filename (dir, &dentry->d_name, fname);#endif -return 0 ; +return err; }static inline void ext4_fname_free_filename (struct ext4_filename *fname)
@@ -2526,15 +2526,16 @@ static inline int ext4_fname_setup_filename(struct inode *dir,int lookup, struct ext4_filename *fname) { + int err = 0; fname->usr_fname = iname; fname->disk_name.name = (unsigned char *) iname->name; fname->disk_name.len = iname->len;#ifdef CONFIG_UNICODE -ext4_fname_setup_ci_filename (dir, iname, &fname->cf_name); + err =ext4_fname_setup_ci_filename (dir, iname, fname);#endif -return 0 ; +return err; } static inline int ext4_fname_prepare_lookup(struct inode *dir,
fs/ext4/namei.c +29 -26
106 lines not on this page · raw message
Signature · 1 line
2.25.0.341.g760bfbb309-goog
Al Viro
maintainerRe: [PATCH v7 2/8] fs: Add standard casefolding support
In reply to Daniel RosenbergQuoted 4 lines · Daniel Rosenberg
fs/libfs.c +4
-0+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);
fs/libfs.c bool is_empty_dir_inode(struct inode *inode) · line 1389
Quoted 10 lines
fs/libfs.c +10
-0+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);
Again, is that safe in case when the contents of the string str points to keeps changing under you?
Re: [PATCH v7 2/8] fs: Add standard casefolding support
Quoted 6 lines · Al Viro
On Fri, Feb 07, 2020 at 05:35:46PM -0800, Daniel Rosenberg wrote:
Again, is that safe in case when the contents of the string str points to keeps changing under you?
I'm not sure what you mean. I thought it was safe to use the str and len passed into d_compare. Even if it gets changed under RCU conditions I thought there was some code to ensure that the name/len pair passed in is consistent, and any other inconsistencies would get caught by d_seq later. Are there unsafe code paths that can follow?
Al Viro
maintainerRe: [PATCH v7 2/8] fs: Add standard casefolding support
Quoted 5 lines · Daniel Rosenberg
Quoted 4 lines · Al Viro
On Fri, Feb 07, 2020 at 05:35:46PM -0800, Daniel Rosenberg wrote:
Quoted 8 lines
Quoted 2 lines
Again, is that safe in case when the contents of the string str points to keeps changing under you?
I'm not sure what you mean. I thought it was safe to use the str and len passed into
d_compare. Even if it gets changed under RCU conditions I thought there was some code to ensure that the name/len pair passed in is consistent, and any other inconsistencies would get caught byd_seqlater. Are there unsafe code paths that can follow?
If you ever fetch the same byte twice, you might see different values. You need a fairly careful use of READ_ONCE() or equivalents to make sure that you don't get screwed over by that.
Sure, ->d_seq mismatch will throw the result out, but you need to make sure you won't oops/step on uninitialized memory/etc. in process.
It's not impossible to get right, but it's not trivial and you need all code working with that much more careful than normal for string handling.
Eric Biggers
maintainerRe: [PATCH v7 1/8] unicode: Add utf8_casefold_iter
In reply to Daniel RosenbergQuoted 35 lines · Daniel Rosenberg
This function will allow other uses of unicode to act upon a casefolded string without needing to allocate their own copy of one.
The actor function can return an nonzero value to exit early.
Signed-off-by Daniel Rosenberg <[email protected]>--- fs/unicode/utf8-core.c | 25 ++++++++++++++++++++++++- include/linux/unicode.h | 10 ++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-)fs/unicode/utf8-core.c +13
-1@@ -122,9 +122,32 @@ int utf8_casefold(const struct unicode_map *um, const struct qstr *str, }return -EINVAL ; } -EXPORT_SYMBOL (utf8_casefold); +int utf8_casefold_iter (const struct unicode_map *um,const struct qstr *str, +struct utf8_itr_context *ctx) +{ +const struct utf8data *data =utf8nfdicf (um->version); +struct utf8cursor cur; +int c; +int res =0 ; +int pos =0 ; + +if (utf8ncursor (&cur, data, str->name, str->len) <0 ) +return -EINVAL ; + +while ((c =utf8byte (&cur))) {
fs/unicode/utf8-core.c int utf8_casefold(const struct unicode_map *um, const struct qstr *str, · line 149
Quoted 10 lines
fs/unicode/utf8-core.c +10
-0+if (c <0 ) +return c; + res = ctx->actor (ctx, c, pos); + pos++; +if (res) +return res; + } +return res; +} +EXPORT_SYMBOL (utf8_casefold_iter);
Indirect function calls are expensive these days for various reasons, including Spectre mitigations and CFI. Are you sure it's okay from a performance perspective to make an indirect call for every byte of the pathname?
fs/unicode/utf8-core.c int utf8_casefold(const struct unicode_map *um, const struct qstr *str, · line 150
Quoted 1 line
fs/unicode/utf8-core.c +1
-0+typedef int (*utf8_itr_actor_t )(struct utf8_itr_context *,int byte,int pos);
The byte argument probably should be 'u8', to avoid confusion about whether it's a byte or a Unicode codepoint.
- Eric
Eric Biggers
maintainerRe: [PATCH v7 2/8] fs: Add standard casefolding support
In reply to Daniel RosenbergQuoted 5 lines · Daniel Rosenberg
This adds general supporting functions for filesystems that use utf8 casefolding. It provides standard
dentry_operationsand adds the necessary structures in structsuper_blockto allow this standardization.Ext4 and F2fs are switch to these implementations.
I think you mean that ext4 and f2fs *will be switched* to these implementations? It's later in the series, not in this patch.
fs/libfs.c bool is_empty_dir_inode(struct inode *inode) · line 1374
Quoted 7 lines
fs/libfs.c +7
-0+#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);
Can you add kerneldoc comments to all the new functions that are exported to modules?
Quoted 29 lines
fs/libfs.c +29
-0+struct hash_ctx { +struct utf8_itr_context ctx; +unsigned long hash; +}; + +static int do_generic_ci_hash (struct utf8_itr_context *ctx,int byte,int pos) +{ + struct hash_ctx *hctx = container_of(ctx, struct hash_ctx, ctx); + + hctx->hash =partial_name_hash ((unsigned char )byte, hctx->hash); +return 0 ; +} + +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 ; +struct hash_ctx hctx; + +if (!inode || !needs_casefold (inode)) +return 0 ; + + hctx.hash =init_name_hash (dentry); + hctx.ctx.actor = do_generic_ci_hash; + ret =utf8_casefold_iter (um, str, &hctx.ctx); +if (ret <0 ) + goto err;
fs/libfs.c bool is_empty_dir_inode(struct inode *inode) · line 1440
Quoted 10 lines
fs/libfs.c +10
-0+ str->hash =end_name_hash (hctx.hash); + +return 0 ; +err: +if (sb_has_enc_strict_mode (sb)) + ret = -EINVAL ; + return ret; +} +EXPORT_SYMBOL (generic_ci_d_hash); +#endif
This breaks the !strict_mode case by starting to fail lookups of names that aren't valid Unicode, instead of falling back to the standard case-sensitive behavior.
There is an xfstest for casefolding; is this bug not caught by it (in which case the test needs to be improved)? Or did you just not run it?
include/linux/fs.h extern int send_sigurg(struct fown_struct *fown); · line 1386
Quoted 10 lines
include/linux/fs.h +2
-0@@ -1382,6 +1382,12 @@ extern int send_sigurg(struct fown_struct *fown);#define SB_ACTIVE (1<<30)#define SB_NOUSER (1<<31) +/* These flags relate to encoding and casefolding */ +#define SB_ENC_STRICT_MODE_FL (1 << 0)
It would be helpful if the comment mentioned that these flags are stored on-disk (and therefore can't be re-numbered, unlike the other flags defined nearby).
include/linux/fs.h struct super_block { · line 1462
Quoted 4 lines
include/linux/fs.h +3
-0+#ifdef CONFIG_UNICODE +struct unicode_map *s_encoding; +__u16 s_encoding_flags;#endif
This isn't a UAPI header, so 's_encoding_flags' should use u16, not __u16.
And for that matter, 's_encoding_flags' will be pointer-sized due to padding anyway, so maybe just make it 'unsigned int'?
include/linux/fs.h extern int generic_file_fsync(struct file *, loff_t, loff_t, int); · line 3384
Quoted 5 lines
include/linux/fs.h +5
-0+static inline bool needs_casefold (const struct inode *dir) +{ +return 0 ; +} +#endif
Use false instead of 0 for 'bool'.
- Eric
Eric Biggers
maintainerRe: [PATCH v7 3/8] f2fs: Use generic casefolding support
In reply to Daniel RosenbergQuoted 2 lines · Daniel Rosenberg
This switches f2fs over to the generic support provided in commit
65832afbeaaf("fs: Add standard casefolding support")
Referring to earlier patches in a series by commit ID isn't a good idea because the commit ID is unknown until the patch is applied.
- Eric
Eric Biggers
maintainerRe: [PATCH v7 5/8] fscrypt: Have filesystems handle their d_ops
In reply to Daniel RosenbergQuoted 8 lines · Daniel Rosenberg
This shifts the responsibility of setting up dentry operations from fscrypt to the individual filesystems, allowing them to have their own operations while still setting fscrypt's
d_revalidateas appropriate.Also added helper function to libfs to unify ext4 and f2fs implementations.
Signed-off-by Daniel Rosenberg <[email protected]>
Quoted 16 lines
--- fs/crypto/fname.c | 7 ++---- fs/crypto/fscrypt_private.h | 1 - fs/crypto/hooks.c | 1 - fs/ext4/dir.c | 7 ------ fs/ext4/namei.c | 1 + fs/ext4/super.c | 5 ---- fs/f2fs/dir.c | 7 ------ fs/f2fs/f2fs.h | 3 --- fs/f2fs/namei.c | 1 + fs/f2fs/super.c | 1 - fs/libfs.c | 50 +++++++++++++++++++++++++++++++++++++ fs/ubifs/dir.c | 18 +++++++++++++ include/linux/fs.h | 2 ++ include/linux/fscrypt.h | 6 +++-- 14 files changed, 78 insertions(+), 32 deletions(-)
This patch is hard to review because it does a lot of different things and touches a lot of different kernel subsystems. Can you please split it up? At least you could do:
1. Export fscrypt_d_revalidate()
2. Add the libfs functions
3. The rest
But maybe you can think of a way to split up (3) too. (It might not be possible because d_set_d_op() can't be called again to override the dentry_operations that __fscrypt_prepare_lookup() sets, until that part is removed.)
Quoted 6 lines
fs/ext4/dir.c +0
-0
fs/ext4/dir.c const struct file_operations ext4_dir_operations = { · line 674
Quoted 10 lines
fs/ext4/dir.c +0
-7@@ -665,10 +665,3 @@ const struct file_operations ext4_dir_operations = { .open = ext4_dir_open, .release = ext4_release_dir, }; - -#ifdef CONFIG_UNICODE -const struct dentry_operations ext4_dentry_ops = { - .d_hash = generic_ci_d_hash, - .d_compare = generic_ci_d_compare, -}; -#endif
Please remove the declaration of ext4_dentry_ops from ext4.h too.
Quoted 17 lines
fs/ext4/namei.c +1
-0@@ -1614,6 +1614,7 @@ static struct buffer_head *ext4_lookup_entry(struct inode *dir,struct buffer_head *bh; err =ext4_fname_prepare_lookup (dir, dentry, &fname); +generic_set_encrypted_ci_d_ops (dir, dentry);if (err == -ENOENT )return NULL ;if (err)fs/ext4/super.c +0
-0
fs/ext4/super.c static int ext4_fill_super(struct super_block *sb, void *data, int silent) · line 4548
Quoted 7 lines
fs/ext4/super.c +0
-4@@ -4542,11 +4542,6 @@ static int ext4_fill_super(struct super_block *sb, void *data, int silent) goto failed_mount4; } -#ifdef CONFIG_UNICODE -if (sb->s_encoding) - sb->s_d_op = &ext4_dentry_ops; -#endif
This changes the place that the dentry operations for casefolding are set, but that's not mentioned in the commit message. In fact, the commit message doesn't mention casefolding at all. Can you please explain it? Does this result in any behavior change?
Likewise for f2fs.
Quoted 23 lines
fs/libfs.c +15
-0@@ -1437,4 +1437,54 @@ int generic_ci_d_hash(const struct dentry *dentry, struct qstr *str)return ret; }EXPORT_SYMBOL (generic_ci_d_hash); + +static const struct dentry_operations generic_ci_dentry_ops = { + .d_hash = generic_ci_d_hash, + .d_compare = generic_ci_d_compare, +}; +#endif + +#ifdef CONFIG_FS_ENCRYPTION +static const struct dentry_operations generic_encrypted_dentry_ops = { + .d_revalidate = fscrypt_d_revalidate, +}; +#endif + +#if IS_ENABLED (CONFIG_UNICODE ) &&IS_ENABLED (CONFIG_FS_ENCRYPTION ) +static const struct dentry_operations generic_encrypted_ci_dentry_ops = {
fs/libfs.c int generic_ci_d_hash(const struct dentry *dentry, struct qstr *str) · line 1464
Quoted 10 lines
fs/libfs.c +10
-0+ .d_hash = generic_ci_d_hash, + .d_compare = generic_ci_d_compare, + .d_revalidate = fscrypt_d_revalidate, +}; +#endif + +/** +* generic_set_encrypted_ci_d_ops - helper for setting d_ops for given dentry +* @dir: parent of dentry whose ops to set +* @dentry: detnry to set ops on
Typo: "detnry"
fs/libfs.c int generic_ci_d_hash(const struct dentry *dentry, struct qstr *str) · line 1468
Quoted 4 lines
fs/libfs.c +4
-0+* +* This function sets the dentry ops for the given dentry to handle both +* casefolding and encryption of the dentry name. +*/
It would be helpful if this comment explained why the two features are handled differently. For encryption the dentry_operations are only set for certain dentries, while for casefolding they're set for all dentries on the filesystem.
Quoted 52 lines
fs/libfs.c +21
-0+void generic_set_encrypted_ci_d_ops (struct inode *dir,struct dentry *dentry) +{ +#ifdef CONFIG_FS_ENCRYPTION +if (dentry->d_flags &DCACHE_ENCRYPTED_NAME ) { +#ifdef CONFIG_UNICODE +if (dir->i_sb->s_encoding) { +d_set_d_op (dentry, &generic_encrypted_ci_dentry_ops); +return ; + }#endif +d_set_d_op (dentry, &generic_encrypted_dentry_ops); +return ; + } +#endif +#ifdef CONFIG_UNICODE +if (dir->i_sb->s_encoding) { +d_set_d_op (dentry, &generic_ci_dentry_ops); +return ; + } +#endif +} +EXPORT_SYMBOL (generic_set_encrypted_ci_d_ops);fs/ubifs/dir.c +8
-0@@ -196,6 +196,7 @@ static int dbg_check_name(const struct ubifs_info *c,return 0 ; } +static void ubifs_set_d_ops (struct inode *dir,struct dentry *dentry);static struct dentry *ubifs_lookup (struct inode *dir,struct dentry *dentry,unsigned int flags) {@@ -209,6 +210,7 @@ static struct dentry *ubifs_lookup(struct inode *dir, struct dentry *dentry,dbg_gen ("'%pd' in dir ino %lu" , dentry, dir->i_ino); err =fscrypt_prepare_lookup (dir, dentry, &nm); +ubifs_set_d_ops (dir, dentry);if (err == -ENOENT )return d_splice_alias (NULL , dentry);if (err)@@ -1655,3 +1657,19 @@ const struct file_operations ubifs_dir_operations = { .compat_ioctl = ubifs_compat_ioctl,#endif }; + +#ifdef CONFIG_FS_ENCRYPTION +static const struct dentry_operations ubifs_encrypted_dentry_ops = { + .d_revalidate = fscrypt_d_revalidate, +}; +#endif
fs/ubifs/dir.c const struct file_operations ubifs_dir_operations = { · line 1675
Quoted 10 lines
fs/ubifs/dir.c +10
-0+ +static void ubifs_set_d_ops (struct inode *dir,struct dentry *dentry) +{ +#ifdef CONFIG_FS_ENCRYPTION +if (dentry->d_flags &DCACHE_ENCRYPTED_NAME ) { +d_set_d_op (dentry, &ubifs_encrypted_dentry_ops); +return ; + } +#endif +}
Why can't UBIFS just use the new helper function you're adding to libfs.c? UBIFS doesn't support casefolding, but that just means that the ci_dentry_ops won't be used; only the generic_encrypted_dentry_ops will be.
include/linux/fscrypt.h static inline struct page *fscrypt_pagecache_page(struct page *bounce_page) · line 137
Quoted 9 lines
include/linux/fscrypt.h +1
-0@@ -134,6 +134,7 @@ static inline struct page *fscrypt_pagecache_page(struct page *bounce_page) }extern void fscrypt_free_bounce_page (struct page *bounce_page); +extern int fscrypt_d_revalidate (struct dentry *dentry,unsigned int flags);
When possible, can you please keep the order of declarations in line with the definitions? That means putting the declaration of fscrypt_d_revalidate() in the "fname.c" section after fscrypt_fname_siphash(), not here.
include/linux/fscrypt.h static inline int fscrypt_prepare_rename(struct inode *old_dir, · line 601
Quoted 12 lines
include/linux/fscrypt.h +3
-2/* policy.c */ extern int fscrypt_ioctl_set_policy (struct file *,const void __user *);@@ -595,8 +596,9 @@ static inline int fscrypt_prepare_rename(struct inode *old_dir,* filenames are presented in encrypted form. Therefore, we'll try to set up * the directory's encryption key, but even without it the lookup can continue. * -* This also installs a custom ->d_revalidate() method which will invalidate the -* dentry if it was created without the key and the key is later added. +* After calling this function, a filesystem should ensure that it's dentry +* operations contain fscrypt_d_revalidate if DCACHE_ENCRYPTED_NAME was set, +* so that the dentry can be invalidated if the key is later added.
"it's" => "its"
- Eric
Eric Biggers
maintainerRe: [PATCH v7 6/8] f2fs: Handle casefolding with Encryption
In reply to Daniel RosenbergQuoted 73 lines · Daniel Rosenberg
This expands f2fs's casefolding support to include encrypted directories. For encrypted directories, we use the siphash of the casefolded name. This ensures there is no direct way to go from an unencrypted name to the stored hash on disk without knowledge of the encryption policy keys.
Additionally, we switch to using the vfs layer's casefolding support instead of storing this information inside of f2fs's private data.
Signed-off-by Daniel Rosenberg <[email protected]>--- fs/f2fs/dir.c | 65 ++++++++++++++++++++++++++++++++---------------- fs/f2fs/f2fs.h | 8 +++--- fs/f2fs/hash.c | 23 +++++++++++------ fs/f2fs/inline.c | 9 ++++--- fs/f2fs/super.c | 6 ----- 5 files changed, 68 insertions(+), 43 deletions(-)fs/f2fs/dir.c +25
-10@@ -112,30 +112,50 @@ static struct f2fs_dir_entry *find_in_block(struct inode *dir,* doesn't match or less than zero on error. */ int f2fs_ci_compare (const struct inode *parent,const struct qstr *name, -const struct qstr *entry,bool quick) +unsigned char *name2,size_t len,bool quick) {const struct super_block *sb = parent->i_sb;const struct unicode_map *um = sb->s_encoding; +const struct fscrypt_str crypt_entry =FSTR_INIT (name2, len); +struct fscrypt_str decrypted_entry; +struct qstr decrypted; +struct qstr entry =QSTR_INIT (name2, len); +struct qstr *tocheck;int ret; + decrypted_entry.name =NULL ; + +if (IS_ENCRYPTED (parent) &&fscrypt_has_encryption_key (parent)) { + decrypted_entry.name =kmalloc (len,GFP_ATOMIC ); + decrypted.name = decrypted_entry.name; + decrypted_entry.len = len; + decrypted.len = len; +if (!decrypted.name) +return -ENOMEM ; + ret =fscrypt_fname_disk_to_usr (parent,0 ,0 , &crypt_entry, + &decrypted_entry); +if (ret <0 ) + goto out; + } + tocheck = decrypted_entry.name ? &decrypted : &entry; +if (quick) - ret =utf8_strncasecmp_folded (um, name, entry); + ret =utf8_strncasecmp_folded (um, name, tocheck);else - ret =utf8_strncasecmp (um, name, entry); - + ret =utf8_strncasecmp (um, name, tocheck);if (ret <0 ) {/* Handle invalid character sequence as either an error * or as an opaque byte sequence. */ if (sb_has_enc_strict_mode (sb)) -return -EINVAL ; - -if (name->len != entry->len) -return 1 ; - -return !!memcmp (name->name, entry->name, name->len); + ret = -EINVAL ;
fs/f2fs/dir.c static struct f2fs_dir_entry *find_in_block(struct inode *dir, · line 160
Quoted 10 lines
fs/f2fs/dir.c +6
-1+else if (name->len != len) + ret =1 ; +else + ret = !!memcmp (name->name, tocheck->name, len); } - +out: +kfree (decrypted_entry.name);return ret; }
The case-sensitive fallback is broken with encrypted filenames; it's checking the length of the encrypted filename rather than the decrypted filename. The decrypted name may be shorter.
Can you please improve your testing to catch bugs like this?
IMO, part of the problem is that there are multiple lengths here, so the variable named 'len' is ambiguous. Can you please clean this function up to name things properly? Also, the 'tocheck' variable is unnecessary, and it's confusing having both 'decrypted' and 'decrypted_entry', and to decrypt conditionally when fscrypt_has_encryption_key() since that's already required.
How about:
int f2fs_ci_compare(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;
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_ATOMIC);
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 (quick)
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 (sb_has_enc_strict_mode(sb))
ret = -EINVAL;
else if (name->len != entry.len)
ret = 1;
else
ret = !!memcmp(name->name, entry.name, entry.len);
}
out:
kfree(decrypted_name.name);
return ret;
}Of course, all this applies to ext4_ci_compare() as well.
- Eric
Eric Biggers
maintainerRe: [PATCH v7 6/8] f2fs: Handle casefolding with Encryption
In reply to Daniel RosenbergQuoted 8 lines · Daniel Rosenberg
fs/f2fs/dir.c +2
-2@@ -173,24 +193,24 @@ static inline bool f2fs_match_name(struct f2fs_dentry_ptr *d, {#ifdef CONFIG_UNICODE struct inode *parent = d->inode; -struct super_block *sb = parent->i_sb; -struct qstr entry; +unsigned char *name; +int len;
fs/f2fs/dir.c static inline bool f2fs_match_name(struct f2fs_dentry_ptr *d, · line 205
Quoted 10 lines
fs/f2fs/dir.c +2
-2#endif if (de->hash_code != namehash)return false;#ifdef CONFIG_UNICODE - entry.name = d->filename[bit_pos]; - entry.len = de->name_len; + name = d->filename[bit_pos]; + len = de->name_len;
This is missing le16_to_cpu().
Quoted 14 lines
fs/f2fs/dir.c +1
-1int f2fs_add_regular_entry (struct inode *dir,const struct qstr *new_name,const struct qstr *orig_name, +f2fs_hash_t dentry_hash,struct inode *inode,nid_t ino,umode_t mode) {unsigned int bit_pos;unsigned int level;unsigned int current_depth;unsigned long bidx, block; -f2fs_hash_t dentry_hash;unsigned int nbucket, nblock;struct page *dentry_page =NULL ;fs/f2fs/dir.c +0
-0struct f2fs_dentry_block *dentry_blk =NULL ;
fs/f2fs/dir.c int f2fs_add_regular_entry(struct inode *dir, const struct qstr *new_name, · line 635
Quoted 4 lines
fs/f2fs/dir.c +0
-1@@ -632,7 +652,6 @@ int f2fs_add_regular_entry(struct inode *dir, const struct qstr *new_name, level =0 ; slots =GET_DENTRY_SLOTS (new_name->len); - dentry_hash =f2fs_dentry_hash (dir, new_name,NULL );
Why was the call to f2fs_dentry_hash() moved to the caller, but for f2fs_add_inline_entry() a different approach was taken?
Quoted 4 lines
fs/f2fs/dir.c +0
-0@@ -718,17 +737,19 @@ int f2fs_add_dentry(struct inode *dir, struct fscrypt_name *fname, struct inode *inode, nid_t ino, umode_t mode) { struct qstr new_name;
fs/f2fs/dir.c int f2fs_add_dentry(struct inode *dir, struct fscrypt_name *fname, · line 748
Quoted 10 lines
fs/f2fs/dir.c +2
-1+f2fs_hash_t dentry_hash;int err = -EAGAIN ; new_name.name =fname_name (fname); new_name.len =fname_len (fname);if (f2fs_has_inline_dentry (dir)) - err =f2fs_add_inline_entry (dir, &new_name, fname->usr_fname, + err =f2fs_add_inline_entry (dir, &new_name, fname, inode, ino, mode);
I'm really confused. Why are you passing around both new_name and fname? We already have new_name == fname.disk_name. So isn't just the 'struct fscrypt_name' sufficient?
Quoted 7 lines
fs/f2fs/hash.c +3
-0+static f2fs_hash_t __f2fs_dentry_hash (const struct inode *dir, +const struct qstr *name_info, +const struct fscrypt_name *fname) {__u32 hash;f2fs_hash_t f2fs_hash;
fs/f2fs/hash.c static f2fs_hash_t __f2fs_dentry_hash(const struct qstr *name_info, · line 92
Quoted 7 lines
fs/f2fs/hash.c +4
-0@@ -85,6 +86,11 @@ static f2fs_hash_t __f2fs_dentry_hash(const struct qstr *name_info,if (is_dot_dotdot (name_info))return 0 ; +if (IS_CASEFOLDED (dir) &&IS_ENCRYPTED (dir)) { + f2fs_hash =fscrypt_fname_siphash (dir, name_info); +return f2fs_hash; + }
This is missing cpu_to_le32().
Also, above we have:
/* encrypted bigname case */
if (fname && !fname->disk_name.name)
return cpu_to_le32(fname->hash);That won't work with encrypted+casefolded directories without the key, because now sometimes the hash from the no-key name is needed even when the disk_name is available. This will cause a crash in fscrypt_fname_siphash() being called without the key. I think you want:
if (fname && fname->is_ciphertext_name)
return cpu_to_le32(fname->hash);Can you please write xfstests for encrypt+casefold?
- Eric
Al Viro
maintainerRe: [PATCH v7 6/8] f2fs: Handle casefolding with Encryption
In reply to Eric BiggersQuoted 4 lines · Eric Biggers
How about:
int f2fs_ci_compare(const struct inode *parent, const struct qstr *name,
u8*de_name,size_tde_name_len, bool quick)
Quoted 8 lines
{
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;
if (IS_ENCRYPTED(parent)) {oops. parent->d_inode is unstable here; could have become NULL by that point.
Quoted 8 lines
if (quick)
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.
*/Really? How would the callers possibly tell mismatch from an error? And if they could, would would they *do* with that error, seeing that it might be an effect of a race with rename()?
Again, ->d_compare() is NOT given a stable name. Or *parent. Or (parent->d_inode).
Eric Biggers
maintainerRe: [PATCH v7 7/8] ext4: Hande casefolding with encryption
In reply to Daniel RosenbergQuoted 13 lines · Daniel Rosenberg
This adds support for encryption with casefolding.
Since the name on disk is case preserving, and also encrypted, we can no longer just recompute the hash on the fly. Additionally, to avoid leaking extra information from the hash of the unencrypted name, we use siphash via an fscrypt v2 policy.
The hash is stored at the end of the directory entry for all entries inside of an encrypted and casefolded directory apart from those that deal with '.' and '..'. This way, the change is backwards compatible with existing ext4 filesystems.
Signed-off-by Daniel Rosenberg <[email protected]>
Quoted 10 lines
--- Documentation/filesystems/ext4/directory.rst | 27 ++ fs/ext4/dir.c | 27 +- fs/ext4/ext4.h | 64 +++- fs/ext4/hash.c | 24 +- fs/ext4/ialloc.c | 5 +- fs/ext4/inline.c | 41 +-- fs/ext4/namei.c | 291 +++++++++++++------ fs/ext4/super.c | 6 - 8 files changed, 343 insertions(+), 142 deletions(-)
How was this tested? I tried it (using a patched version of 'mke2fs' that allows the encrypt and encoding options to be combined), and I immediately got an ext4 error about a bad directory entry:
~/e2fsprogs/misc/mke2fs -F -t ext4 -O encrypt -E encoding=utf8 /dev/vdb mount /dev/vdb /mnt
fscrypt setup /mnt
mkdir /mnt/dir
# (assumes /etc/fscrypt.conf contains policy_version 2)
echo hunter2 | fscrypt encrypt /mnt/dir --quiet --source=custom_passphrase --name=dir chattr +F /mnt/dir
echo contents > /mnt/dir/file
umount /mnt
mount /dev/vdb /mnt
ls /mnt/dir/
[ 391.292067] EXT4-fs error (device vdb): htree_dirblock_to_tree:1038: inode #8193: block 4251: comm ls: bad entry in directory: directory entry too close to block end - offset=80, inode=18, rec_len=4004, lblk=0, size=4096
Eric Biggers
maintainerRe: [PATCH v7 6/8] f2fs: Handle casefolding with Encryption
In reply to Al ViroQuoted 27 lines · Al Viro
Quoted 4 lines · Eric Biggers
How about:
int f2fs_ci_compare(const struct inode *parent, const struct qstr *name,
u8*de_name,size_tde_name_len, bool quick)Quoted 8 lines
{ 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; if (IS_ENCRYPTED(parent)) {oops.
parent->d_inodeis unstable here; could have become NULL by that point.Quoted 8 lines
if (quick) 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. */
Quoted 7 lines
Really? How would the callers possibly tell mismatch from an error? And if they could, would would they *do* with that error, seeing that it might be an effect of a race with
rename()?Again, ->d_compare() is NOT given a stable name. Or *parent. Or (
parent->d_inode).
After the patch earlier in the series that created generic_ci_d_compare() and switched f2fs to use it, f2fs_ci_compare() is only called when the filesystem is actually searching a directory, not from ->d_compare(). So the names and parent->d_inode are stable in it.
But, that also means the GFP_ATOMIC isn't needed, and f2fs_ci_compare() should be made 'static'.
- Eric
Eric Biggers
maintainerQuoted 1 line · Daniel Rosenberg
Support fof Casefolding and Encryption
You should fix the typo in the subject in the next version. I assumed you'd notice, but both v6 and v7 have this...
- Eric
Eric Biggers
maintainerRe: [PATCH v7 2/8] fs: Add standard casefolding support
In reply to Al ViroQuoted 19 lines · Al Viro
Quoted 5 lines · Daniel Rosenberg
Quoted 4 lines · Al Viro
On Fri, Feb 07, 2020 at 05:35:46PM -0800, Daniel Rosenberg wrote:
Quoted 8 lines
Quoted 2 lines
Again, is that safe in case when the contents of the string str points to keeps changing under you?
I'm not sure what you mean. I thought it was safe to use the str and len passed into
d_compare. Even if it gets changed under RCU conditions I thought there was some code to ensure that the name/len pair passed in is consistent, and any other inconsistencies would get caught byd_seqlater. Are there unsafe code paths that can follow?If you ever fetch the same byte twice, you might see different values. You need a fairly careful use of
READ_ONCE()or equivalents to make sure that you don't get screwed over by that.
Quoted 5 lines
Sure, ->d_seq mismatch will throw the result out, but you need to make sure you won't oops/step on uninitialized memory/etc. in process.
It's not impossible to get right, but it's not trivial and you need all code working with that much more careful than normal for string handling.
It looks like this is a real problem, not just a "theoretical" data race. For example, see:
utf8ncursor():
/* The first byte of s may not be an utf8 continuation. */
if (len > 0 && (*s & 0xC0) == 0x80)
return -1;and then utf8byte():
} else if ((*u8c->s & 0xC0) == 0x80) {
/* This is a continuation of the current character. */
if (!u8c->p)
u8c->len--;
return (unsigned char)*u8c->s++;The first byte of the string is checked in two different functions, so it's very likely to be loaded twice. In between, it could change from a non-continuation byte to a continuation byte. That would cause the string length to be decremented from 0 to UINT_MAX. Then utf8_strncasecmp() would run beyond the bounds of the string until something happened to mismatch.
That's just an example that I found right away; there are probably more.
IMO, this needs to be fixed before anyone can actually use the ext4 and f2fs casefolding stuff.
I don't know the best solution. One option is to fix fs/unicode/ to handle concurrently modified strings. Another could be to see what it would take to serialize lookups and renames for casefolded directories...
- Eric
Eric Biggers
maintainerRe: [PATCH v7 2/8] fs: Add standard casefolding support
Quoted 47 lines · Eric Biggers
Quoted 19 lines · Al Viro
Quoted 5 lines · Daniel Rosenberg
Quoted 4 lines · Al Viro
On Fri, Feb 07, 2020 at 05:35:46PM -0800, Daniel Rosenberg wrote:
Quoted 8 lines
Quoted 2 lines
Again, is that safe in case when the contents of the string str points to keeps changing under you?
I'm not sure what you mean. I thought it was safe to use the str and len passed into
d_compare. Even if it gets changed under RCU conditions I thought there was some code to ensure that the name/len pair passed in is consistent, and any other inconsistencies would get caught byd_seqlater. Are there unsafe code paths that can follow?If you ever fetch the same byte twice, you might see different values. You need a fairly careful use of
READ_ONCE()or equivalents to make sure that you don't get screwed over by that.Quoted 5 lines
Sure, ->d_seq mismatch will throw the result out, but you need to make sure you won't oops/step on uninitialized memory/etc. in process.
It's not impossible to get right, but it's not trivial and you need all code working with that much more careful than normal for string handling.
It looks like this is a real problem, not just a "theoretical" data race. For example, see:
utf8ncursor():/* The first byte of s may not be an utf8 continuation. */ if (len > 0 && (*s & 0xC0) == 0x80) return -1;and then
utf8byte():} else if ((*u8c->s & 0xC0) == 0x80) { /* This is a continuation of the current character. */ if (!u8c->p) u8c->len--; return (unsigned char)*u8c->s++;The first byte of the string is checked in two different functions, so it's very likely to be loaded twice. In between, it could change from a non-continuation byte to a continuation byte. That would cause the string length to be decremented from 0 to
UINT_MAX. Thenutf8_strncasecmp()would run beyond the bounds of the string until something happened to mismatch.
Quoted 9 lines
That's just an example that I found right away; there are probably more.
IMO, this needs to be fixed before anyone can actually use the ext4 and f2fs casefolding stuff.
I don't know the best solution. One option is to fix
fs/unicode/to handle concurrently modified strings. Another could be to see what it would take to serialize lookups and renames for casefolded directories...
Or (just throwing another idea out there) the dentry's name could be copied to a temporary buffer in ->d_compare(). The simplest version would be:
u8 _name[NAME_MAX]; memcpy(_name, name, len); name = _name;
Though, 255 bytes is a bit large for a stack buffer (so for long names it may need kmalloc with GFP_ATOMIC), and technically it would need a special version of memcpy() to be guaranteed safe from compiler optimizations (though I expect this would work in practice).
Alternatively, take_dentry_name_snapshot() kind of does this already, except that it takes a dentry and not a (name, len) pair.
- Eric
Quoted 8 lines · Eric Biggers
Quoted 1 line · Daniel Rosenberg
Support fof Casefolding and Encryption
You should fix the typo in the subject in the next version. I assumed you'd notice, but both v6 and v7 have this...
- Eric
Yeah, noticed just after I sent v7 :'(
Re: [PATCH v7 1/8] unicode: Add utf8_casefold_iter
In reply to Eric BiggersQuoted 11 lines · Eric Biggers
Indirect function calls are expensive these days for various reasons, including Spectre mitigations and CFI. Are you sure it's okay from a performance perspective to make an indirect call for every byte of the pathname?
Quoted 1 line
+typedef int (*utf8_itr_actor_t)(struct
utf8_itr_context*, int byte, int pos);The byte argument probably should be 'u8', to avoid confusion about whether it's a byte or a Unicode codepoint.
- Eric
Gabriel, what do you think here? I could change it to either exposing the things necessary to do the hashing in libfs, or instead of the general purpose iterator, just have a hash function inside of unicode that will compute the hash given a seed value.
-Daniel
Re: [PATCH v7 1/8] unicode: Add utf8_casefold_iter
Quoted 11 lines · Daniel Rosenberg
Quoted 10 lines · Eric Biggers
Indirect function calls are expensive these days for various reasons, including Spectre mitigations and CFI. Are you sure it's okay from a performance perspective to make an indirect call for every byte of the pathname?
Quoted 1 line
+typedef int (*utf8_itr_actor_t)(struct
utf8_itr_context*, int byte, int pos);The byte argument probably should be 'u8', to avoid confusion about whether it's a byte or a Unicode codepoint.
just for the record, we use int utf8byte because it can fail error codes, but that is not the case here. It should be u8.
Quoted 5 lines
Gabriel, what do you think here? I could change it to either exposing the things necessary to do the hashing in libfs, or instead of the general purpose iterator, just have a hash function inside of unicode that will compute the hash given a seed value.
Sorry for the delay, I'm away on a long vacation and intentionally staying away from my laptop :)
Eric has a very good point, if not prohibitively, it is unnecessarily expensive for a hot path. Why not expose utf8ncursor and utf8byte to libfs and implement the hash in libfs?
Signature · 1 line
Gabriel Krisman Bertazi
Re: [PATCH v7 2/8] fs: Add standard casefolding support
In reply to Eric BiggersQuoted 14 lines · Eric Biggers
Or (just throwing another idea out there) the dentry's name could be copied to a temporary buffer in ->d_compare(). The simplest version would be:
u8 _name[NAME_MAX]; memcpy(_name, name, len); name = _name;Though, 255 bytes is a bit large for a stack buffer (so for long names it may need kmalloc with
GFP_ATOMIC), and technically it would need a special version ofmemcpy()to be guaranteed safe from compiler optimizations (though I expect this would work in practice).
Quoted 4 lines
Alternatively,
take_dentry_name_snapshot()kind of does this already, except that it takes a dentry and not a (name, len) pair.- Eric
If we want to use take_dentry_name_snapshot, we'd need to do it before calling the dentry op, since we get the dentry as a const. It would do exactly what we want, in that it either takes a reference on the long name, or copies the short name, although it does so under a spinlock. I'm guessing we don't want to add that overhead for all d_compare/d_hash's. I suppose it could just take a snapshot if it falls under needs_casefold, but that feels a bit silly to me.
i don't think utf8cursor/utf8byte could be modified to be RCU safe apart from a copy. As part of normalization there's some sorting that goes on to ensure that different encodings of the same characters can be matched, and I think those can technically be arbitrarily long, so we'd possibly end up needing the copy anyways.
So, I see two possible fixes.
1. Use take_dentry_name_snapshot along the RCU paths to calling d_hash and d_compare, at least when needs_casefold is true.
2. Within d_hash/d_compare, create a copy of the name if it is a short name.
For 1, it adds some overhead in general, which I'm sure we'd want to avoid. For 2, I don't think we know we're in RCU mode, so we'd need to always copy short filenames. I'm also unsure if it's valid to assume that name given is stable if it is not the same as dentry->d_iname. If it is, we only need to worry about copying DNAME_INLINE_LEN bytes at max there. For memcpy, is there a different version that we'd want to use for option 2?
-Daniel