ext4 crypto: use per-inode tfm structure

As suggested by Herbert Xu, we shouldn't allocate a new tfm each time
we read or write a page.  Instead we can use a single tfm hanging off
the inode's crypt_info structure for all of our encryption needs for
that inode, since the tfm can be used by multiple crypto requests in
parallel.

Also use cmpxchg() to avoid races that could result in crypt_info
structure getting doubly allocated or doubly freed.

Signed-off-by: Theodore Ts'o <tytso@mit.edu>
This commit is contained in:
Theodore Ts'o
2015-05-31 13:34:22 -04:00
parent 71dea01ea2
commit c936e1ec28
9 changed files with 96 additions and 156 deletions

View File

@@ -607,11 +607,12 @@ static struct stats dx_show_leaf(struct inode *dir,
char *name;
struct ext4_str fname_crypto_str
= {.name = NULL, .len = 0};
int res;
int res = 0;
name = de->name;
len = de->name_len;
res = ext4_setup_fname_crypto(dir);
if (ext4_encrypted_inode(inode))
res = ext4_get_encryption_info(dir);
if (res) {
printk(KERN_WARNING "Error setting up"
" fname crypto: %d\n", res);
@@ -953,12 +954,12 @@ static int htree_dirblock_to_tree(struct file *dir_file,
EXT4_DIR_REC_LEN(0));
#ifdef CONFIG_EXT4_FS_ENCRYPTION
/* Check if the directory is encrypted */
err = ext4_setup_fname_crypto(dir);
if (err) {
brelse(bh);
return err;
}
if (ext4_encrypted_inode(dir)) {
err = ext4_get_encryption_info(dir);
if (err < 0) {
brelse(bh);
return err;
}
err = ext4_fname_crypto_alloc_buffer(dir, EXT4_NAME_LEN,
&fname_crypto_str);
if (err < 0) {
@@ -3108,7 +3109,7 @@ static int ext4_symlink(struct inode *dir,
err = ext4_inherit_context(dir, inode);
if (err)
goto err_drop_inode;
err = ext4_setup_fname_crypto(inode);
err = ext4_get_encryption_info(inode);
if (err)
goto err_drop_inode;
istr.name = (const unsigned char *) symname;