Whamcloud - gitweb
LU-16202 build: bio_alloc takes struct block_device
[fs/lustre-release.git] / libcfs / libcfs / crypto / crypto.c
index 379affb..3d18715 100644 (file)
 #include <crypto/skcipher.h>
 #include "llcrypt_private.h"
 
+#ifdef HAVE_CIPHER_H
+#include <crypto/internal/cipher.h>
+
+MODULE_IMPORT_NS(CRYPTO_INTERNAL);
+#endif
+
 static unsigned int num_prealloc_crypto_pages = 32;
 static unsigned int num_prealloc_crypto_ctxs = 128;
 
@@ -166,6 +172,13 @@ int llcrypt_crypt_block(const struct inode *inode, llcrypt_direction_t rw,
        struct crypto_skcipher *tfm = ci->ci_ctfm;
        int res = 0;
 
+       if (tfm == NULL) {
+               if (dest_page != src_page)
+                       memcpy(page_address(dest_page), page_address(src_page),
+                              PAGE_SIZE);
+               return 0;
+       }
+
        if (WARN_ON_ONCE(len <= 0))
                return -EINVAL;
        if (WARN_ON_ONCE(len % LL_CRYPTO_BLOCK_SIZE != 0))
@@ -259,9 +272,10 @@ struct page *llcrypt_encrypt_pagecache_blocks(struct page *page,
 EXPORT_SYMBOL(llcrypt_encrypt_pagecache_blocks);
 
 /**
- * llcrypt_encrypt_block_inplace() - Encrypt a filesystem block in-place
+ * llcrypt_encrypt_block() - Encrypt a filesystem block in a page
  * @inode:     The inode to which this block belongs
- * @page:      The page containing the block to encrypt
+ * @src:       The page containing the block to encrypt
+ * @dst:       The page which will contain the encrypted data
  * @len:       Size of block to encrypt.  Doesn't need to be a multiple of the
  *             fs block size, but must be a multiple of LL_CRYPTO_BLOCK_SIZE.
  * @offs:      Byte offset within @page at which the block to encrypt begins
@@ -272,17 +286,18 @@ EXPORT_SYMBOL(llcrypt_encrypt_pagecache_blocks);
  * Encrypt a possibly-compressed filesystem block that is located in an
  * arbitrary page, not necessarily in the original pagecache page.  The @inode
  * and @lblk_num must be specified, as they can't be determined from @page.
+ * The decrypted data will be stored in @dst.
  *
  * Return: 0 on success; -errno on failure
  */
-int llcrypt_encrypt_block_inplace(const struct inode *inode, struct page *page,
-                                 unsigned int len, unsigned int offs,
-                                 u64 lblk_num, gfp_t gfp_flags)
+int llcrypt_encrypt_block(const struct inode *inode, struct page *src,
+                         struct page *dst, unsigned int len, unsigned int offs,
+                         u64 lblk_num, gfp_t gfp_flags)
 {
-       return llcrypt_crypt_block(inode, FS_ENCRYPT, lblk_num, page, page,
+       return llcrypt_crypt_block(inode, FS_ENCRYPT, lblk_num, src, dst,
                                   len, offs, gfp_flags);
 }
-EXPORT_SYMBOL(llcrypt_encrypt_block_inplace);
+EXPORT_SYMBOL(llcrypt_encrypt_block);
 
 /**
  * llcrypt_decrypt_pagecache_blocks() - Decrypt filesystem blocks in a pagecache page
@@ -328,9 +343,10 @@ int llcrypt_decrypt_pagecache_blocks(struct page *page, unsigned int len,
 EXPORT_SYMBOL(llcrypt_decrypt_pagecache_blocks);
 
 /**
- * llcrypt_decrypt_block_inplace() - Decrypt a filesystem block in-place
+ * llcrypt_decrypt_block() - Cache a decrypted filesystem block in a page
  * @inode:     The inode to which this block belongs
- * @page:      The page containing the block to decrypt
+ * @src:       The page containing the block to decrypt
+ * @dst:       The page which will contain the plain data
  * @len:       Size of block to decrypt.  Doesn't need to be a multiple of the
  *             fs block size, but must be a multiple of LL_CRYPTO_BLOCK_SIZE.
  * @offs:      Byte offset within @page at which the block to decrypt begins
@@ -340,17 +356,18 @@ EXPORT_SYMBOL(llcrypt_decrypt_pagecache_blocks);
  * Decrypt a possibly-compressed filesystem block that is located in an
  * arbitrary page, not necessarily in the original pagecache page.  The @inode
  * and @lblk_num must be specified, as they can't be determined from @page.
+ * The encrypted data will be stored in @dst.
  *
  * Return: 0 on success; -errno on failure
  */
-int llcrypt_decrypt_block_inplace(const struct inode *inode, struct page *page,
-                                 unsigned int len, unsigned int offs,
-                                 u64 lblk_num)
+int llcrypt_decrypt_block(const struct inode *inode, struct page *src,
+                         struct page *dst, unsigned int len, unsigned int offs,
+                         u64 lblk_num, gfp_t gfp_flags)
 {
-       return llcrypt_crypt_block(inode, FS_DECRYPT, lblk_num, page, page,
-                                  len, offs, GFP_NOFS);
+       return llcrypt_crypt_block(inode, FS_DECRYPT, lblk_num, src, dst,
+                                  len, offs, gfp_flags);
 }
-EXPORT_SYMBOL(llcrypt_decrypt_block_inplace);
+EXPORT_SYMBOL(llcrypt_decrypt_block);
 
 /*
  * Validate dentries in encrypted directories to make sure we aren't potentially