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>
diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c
index 9bed99f..6ab50f8 100644
--- a/fs/ext4/namei.c
+++ b/fs/ext4/namei.c
@@ -607,11 +607,12 @@
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 @@
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 @@
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;