Whamcloud - gitweb
LU-12275 sec: add llcrypt as file encryption library
[fs/lustre-release.git] / contrib / scripts / crypto_patches / 0010_llcrypt_info.patch
1 Replace use of inode->i_crypt_info with inode->i_private, cast as a struct llcrypt_info.
2 This is required in order to be able to support encryption on kernels that lack
3 inode->i_crypt_info field.
4
5 --- a/libcfs/include/libcfs/crypto/llcrypt.h
6 +++ b/libcfs/include/libcfs/crypto/llcrypt.h
7 @@ -81,11 +81,7 @@ struct llcrypt_ctx {
8         u8 flags;                               /* Flags */
9  };
10  
11 -static inline bool llcrypt_has_encryption_key(const struct inode *inode)
12 -{
13 -       /* pairs with cmpxchg_release() in llcrypt_get_encryption_info() */
14 -       return READ_ONCE(inode->i_crypt_info) != NULL;
15 -}
16 +extern bool llcrypt_has_encryption_key(const struct inode *inode);
17  
18  static inline bool llcrypt_dummy_context_enabled(struct inode *inode)
19  {
20 --- a/libcfs/libcfs/crypto/crypto.c
21 +++ b/libcfs/libcfs/crypto/crypto.c
22 @@ -158,7 +158,7 @@ int llcrypt_crypt_block(const struct ino
23         struct skcipher_request *req = NULL;
24         DECLARE_CRYPTO_WAIT(wait);
25         struct scatterlist dst, src;
26 -       struct llcrypt_info *ci = inode->i_crypt_info;
27 +       struct llcrypt_info *ci = llcrypt_info(inode);
28         struct crypto_skcipher *tfm = ci->ci_ctfm;
29         int res = 0;
30  
31 --- a/libcfs/libcfs/crypto/fname.c
32 +++ b/libcfs/libcfs/crypto/fname.c
33 @@ -39,7 +39,7 @@ int fname_encrypt(struct inode *inode, c
34  {
35         struct skcipher_request *req = NULL;
36         DECLARE_CRYPTO_WAIT(wait);
37 -       struct llcrypt_info *ci = inode->i_crypt_info;
38 +       struct llcrypt_info *ci = llcrypt_info(inode);
39         struct crypto_skcipher *tfm = ci->ci_ctfm;
40         union llcrypt_iv iv;
41         struct scatterlist sg;
42 @@ -92,7 +92,7 @@ static int fname_decrypt(struct inode *i
43         struct skcipher_request *req = NULL;
44         DECLARE_CRYPTO_WAIT(wait);
45         struct scatterlist src_sg, dst_sg;
46 -       struct llcrypt_info *ci = inode->i_crypt_info;
47 +       struct llcrypt_info *ci = llcrypt_info(inode);
48         struct crypto_skcipher *tfm = ci->ci_ctfm;
49         union llcrypt_iv iv;
50         int res;
51 @@ -181,7 +181,7 @@ static int base64_decode(const char *src
52  bool llcrypt_fname_encrypted_size(const struct inode *inode, u32 orig_len,
53                                   u32 max_len, u32 *encrypted_len_ret)
54  {
55 -       const struct llcrypt_info *ci = inode->i_crypt_info;
56 +       const struct llcrypt_info *ci = llcrypt_info(inode);
57         int padding = 4 << (llcrypt_policy_flags(&ci->ci_policy) &
58                             LLCRYPT_POLICY_FLAGS_PAD_MASK);
59         u32 encrypted_len;
60 --- a/libcfs/libcfs/crypto/keysetup.c
61 +++ b/libcfs/libcfs/crypto/keysetup.c
62 @@ -501,7 +501,8 @@ int llcrypt_get_encryption_info(struct i
63         if (res)
64                 goto out;
65  
66 -       if (cmpxchg_release(&inode->i_crypt_info, NULL, crypt_info) == NULL) {
67 +       if (cmpxchg_release(&(llcrypt_info_nocast(inode)), NULL,
68 +                           crypt_info) == NULL) {
69                 if (master_key) {
70                         struct llcrypt_master_key *mk =
71                                 master_key->payload.data[0];
72 @@ -538,8 +539,8 @@ EXPORT_SYMBOL(llcrypt_get_encryption_inf
73   */
74  void llcrypt_put_encryption_info(struct inode *inode)
75  {
76 -       put_crypt_info(inode->i_crypt_info);
77 -       inode->i_crypt_info = NULL;
78 +       put_crypt_info(llcrypt_info(inode));
79 +       llcrypt_info_nocast(inode) = NULL;
80  }
81  EXPORT_SYMBOL(llcrypt_put_encryption_info);
82  
83 @@ -569,9 +570,10 @@ EXPORT_SYMBOL(llcrypt_free_inode);
84   */
85  int llcrypt_drop_inode(struct inode *inode)
86  {
87 -       const struct llcrypt_info *ci = READ_ONCE(inode->i_crypt_info);
88 +       const struct llcrypt_info *ci;
89         const struct llcrypt_master_key *mk;
90  
91 +       ci = (struct llcrypt_info *)READ_ONCE(llcrypt_info_nocast(inode));
92         /*
93          * If ci is NULL, then the inode doesn't have an encryption key set up
94          * so it's irrelevant.  If ci_master_key is NULL, then the master key
95 @@ -593,3 +595,10 @@ int llcrypt_drop_inode(struct inode *ino
96         return !is_master_key_secret_present(&mk->mk_secret);
97  }
98  EXPORT_SYMBOL_GPL(llcrypt_drop_inode);
99 +
100 +inline bool llcrypt_has_encryption_key(const struct inode *inode)
101 +{
102 +       /* pairs with cmpxchg_release() in llcrypt_get_encryption_info() */
103 +       return READ_ONCE(llcrypt_info_nocast(inode)) != NULL;
104 +}
105 +EXPORT_SYMBOL_GPL(llcrypt_has_encryption_key);
106 --- a/libcfs/libcfs/crypto/llcrypt_private.h
107 +++ b/libcfs/libcfs/crypto/llcrypt_private.h
108 @@ -19,6 +19,9 @@
109  #define CRYPTO_TFM_REQ_FORBID_WEAK_KEYS CRYPTO_TFM_REQ_WEAK_KEY
110  #endif
111  
112 +#define llcrypt_info(inode)         ((struct llcrypt_info *)(inode)->i_private)
113 +#define llcrypt_info_nocast(inode)   ((inode)->i_private)
114 +
115  #define CONST_STRLEN(str)      (sizeof(str) - 1)
116  
117  #define FS_KEY_DERIVATION_NONCE_SIZE   16
118 @@ -160,8 +163,8 @@ struct llcrypt_symlink_data {
119   * llcrypt_info - the "encryption key" for an inode
120   *
121   * When an encrypted file's key is made available, an instance of this struct is
122 - * allocated and stored in ->i_crypt_info.  Once created, it remains until the
123 - * inode is evicted.
124 + * allocated and stored in '(struct llcrypt_info *)inode->i_private'.
125 + * Once created, it remains until the inode is evicted.
126   */
127  struct llcrypt_info {
128  
129 --- a/libcfs/libcfs/crypto/policy.c
130 +++ b/libcfs/libcfs/crypto/policy.c
131 @@ -212,7 +212,7 @@ static int llcrypt_get_policy(struct ino
132         struct lustre_sb_info *lsi = s2lsi(inode->i_sb);
133         int ret;
134  
135 -       ci = READ_ONCE(inode->i_crypt_info);
136 +       ci = (struct llcrypt_info *)READ_ONCE(llcrypt_info_nocast(inode));
137         if (ci) {
138                 /* key available, use the cached policy */
139                 *policy = ci->ci_policy;
140 @@ -472,7 +472,7 @@ EXPORT_SYMBOL(llcrypt_has_permitted_cont
141   * @parent: Parent inode from which the context is inherited.
142   * @child:  Child inode that inherits the context from @parent.
143   * @fs_data:  private data given by FS.
144 - * @preload:  preload child i_crypt_info if true
145 + * @preload:  preload child crypt info if true
146   *
147   * Return: 0 on success, -errno on failure
148   */
149 @@ -489,7 +489,7 @@ int llcrypt_inherit_context(struct inode
150         if (res < 0)
151                 return res;
152  
153 -       ci = READ_ONCE(parent->i_crypt_info);
154 +       ci = (struct llcrypt_info *)READ_ONCE(llcrypt_info_nocast(parent));
155         if (ci == NULL)
156                 return -ENOKEY;
157