Merge tag 'ecryptfs-4.7-rc7-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git...
[cascardo/linux.git] / fs / ecryptfs / crypto.c
index d09cb4c..e5e29f8 100644 (file)
@@ -45,7 +45,7 @@
  * ecryptfs_to_hex
  * @dst: Buffer to take hex character representation of contents of
  *       src; must be at least of size (src_size * 2)
- * @src: Buffer to be converted to a hex string respresentation
+ * @src: Buffer to be converted to a hex string representation
  * @src_size: number of bytes to convert
  */
 void ecryptfs_to_hex(char *dst, char *src, size_t src_size)
@@ -60,7 +60,7 @@ void ecryptfs_to_hex(char *dst, char *src, size_t src_size)
  * ecryptfs_from_hex
  * @dst: Buffer to take the bytes from src hex; must be at least of
  *       size (src_size / 2)
- * @src: Buffer to be converted from a hex string respresentation to raw value
+ * @src: Buffer to be converted from a hex string representation to raw value
  * @dst_size: size of dst buffer, or number of hex characters pairs to convert
  */
 void ecryptfs_from_hex(char *dst, char *src, int dst_size)
@@ -105,19 +105,7 @@ static int ecryptfs_calculate_md5(char *dst,
        struct crypto_shash *tfm;
        int rc = 0;
 
-       mutex_lock(&crypt_stat->cs_hash_tfm_mutex);
        tfm = crypt_stat->hash_tfm;
-       if (!tfm) {
-               tfm = crypto_alloc_shash(ECRYPTFS_DEFAULT_HASH, 0, 0);
-               if (IS_ERR(tfm)) {
-                       rc = PTR_ERR(tfm);
-                       ecryptfs_printk(KERN_ERR, "Error attempting to "
-                                       "allocate crypto context; rc = [%d]\n",
-                                       rc);
-                       goto out;
-               }
-               crypt_stat->hash_tfm = tfm;
-       }
        rc = ecryptfs_hash_digest(tfm, src, len, dst);
        if (rc) {
                printk(KERN_ERR
@@ -126,7 +114,6 @@ static int ecryptfs_calculate_md5(char *dst,
                goto out;
        }
 out:
-       mutex_unlock(&crypt_stat->cs_hash_tfm_mutex);
        return rc;
 }
 
@@ -207,16 +194,29 @@ out:
  *
  * Initialize the crypt_stat structure.
  */
-void
-ecryptfs_init_crypt_stat(struct ecryptfs_crypt_stat *crypt_stat)
+int ecryptfs_init_crypt_stat(struct ecryptfs_crypt_stat *crypt_stat)
 {
+       struct crypto_shash *tfm;
+       int rc;
+
+       tfm = crypto_alloc_shash(ECRYPTFS_DEFAULT_HASH, 0, 0);
+       if (IS_ERR(tfm)) {
+               rc = PTR_ERR(tfm);
+               ecryptfs_printk(KERN_ERR, "Error attempting to "
+                               "allocate crypto context; rc = [%d]\n",
+                               rc);
+               return rc;
+       }
+
        memset((void *)crypt_stat, 0, sizeof(struct ecryptfs_crypt_stat));
        INIT_LIST_HEAD(&crypt_stat->keysig_list);
        mutex_init(&crypt_stat->keysig_list_mutex);
        mutex_init(&crypt_stat->cs_mutex);
        mutex_init(&crypt_stat->cs_tfm_mutex);
-       mutex_init(&crypt_stat->cs_hash_tfm_mutex);
+       crypt_stat->hash_tfm = tfm;
        crypt_stat->flags |= ECRYPTFS_STRUCT_INITIALIZED;
+
+       return 0;
 }
 
 /**
@@ -953,7 +953,7 @@ struct ecryptfs_cipher_code_str_map_elem {
 };
 
 /* Add support for additional ciphers by adding elements here. The
- * cipher_code is whatever OpenPGP applicatoins use to identify the
+ * cipher_code is whatever OpenPGP applications use to identify the
  * ciphers. List in order of probability. */
 static struct ecryptfs_cipher_code_str_map_elem
 ecryptfs_cipher_code_str_map[] = {
@@ -1141,12 +1141,13 @@ ecryptfs_write_metadata_to_contents(struct inode *ecryptfs_inode,
 
 static int
 ecryptfs_write_metadata_to_xattr(struct dentry *ecryptfs_dentry,
+                                struct inode *ecryptfs_inode,
                                 char *page_virt, size_t size)
 {
        int rc;
 
-       rc = ecryptfs_setxattr(ecryptfs_dentry, ECRYPTFS_XATTR_NAME, page_virt,
-                              size, 0);
+       rc = ecryptfs_setxattr(ecryptfs_dentry, ecryptfs_inode,
+                              ECRYPTFS_XATTR_NAME, page_virt, size, 0);
        return rc;
 }
 
@@ -1215,8 +1216,8 @@ int ecryptfs_write_metadata(struct dentry *ecryptfs_dentry,
                goto out_free;
        }
        if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
-               rc = ecryptfs_write_metadata_to_xattr(ecryptfs_dentry, virt,
-                                                     size);
+               rc = ecryptfs_write_metadata_to_xattr(ecryptfs_dentry, ecryptfs_inode,
+                                                     virt, size);
        else
                rc = ecryptfs_write_metadata_to_contents(ecryptfs_inode, virt,
                                                         virt_len);
@@ -1369,7 +1370,9 @@ int ecryptfs_read_xattr_region(char *page_virt, struct inode *ecryptfs_inode)
        ssize_t size;
        int rc = 0;
 
-       size = ecryptfs_getxattr_lower(lower_dentry, ECRYPTFS_XATTR_NAME,
+       size = ecryptfs_getxattr_lower(lower_dentry,
+                                      ecryptfs_inode_to_lower(ecryptfs_inode),
+                                      ECRYPTFS_XATTR_NAME,
                                       page_virt, ECRYPTFS_DEFAULT_EXTENT_SIZE);
        if (size < 0) {
                if (unlikely(ecryptfs_verbosity > 0))
@@ -1391,6 +1394,7 @@ int ecryptfs_read_and_validate_xattr_region(struct dentry *dentry,
        int rc;
 
        rc = ecryptfs_getxattr_lower(ecryptfs_dentry_to_lower(dentry),
+                                    ecryptfs_inode_to_lower(inode),
                                     ECRYPTFS_XATTR_NAME, file_size,
                                     ECRYPTFS_SIZE_AND_MARKER_BYTES);
        if (rc < ECRYPTFS_SIZE_AND_MARKER_BYTES)
@@ -1406,7 +1410,7 @@ int ecryptfs_read_and_validate_xattr_region(struct dentry *dentry,
  *
  * Common entry point for reading file metadata. From here, we could
  * retrieve the header information from the header region of the file,
- * the xattr region of the file, or some other repostory that is
+ * the xattr region of the file, or some other repository that is
  * stored separately from the file itself. The current implementation
  * supports retrieving the metadata information from the file contents
  * and from the xattr region.