Whamcloud - gitweb
LU-13683 lfs: return -ENOENT when invoked on non-existing file
[fs/lustre-release.git] / contrib / scripts / crypto_patches / 0009_lsi_cop.patch
1 Replace (struct super_block *)->s_cop with (struct lustre_sb_info *)->lsi_cop.
2 This is needed so that we can use llcrypt on kernels that do not have
3 (struct super_block *)->s_cop.
4
5 --- a/libcfs/include/libcfs/crypto/llcrypt.h
6 +++ b/libcfs/include/libcfs/crypto/llcrypt.h
7 @@ -20,6 +20,7 @@
8  #include <linux/fs.h>
9  #include <linux/mm.h>
10  #include <linux/slab.h>
11 +#include <lustre_disk.h>
12  #include <uapi/linux/llcrypt.h>
13  
14  #define LL_CRYPTO_BLOCK_SIZE           16
15 @@ -88,8 +89,13 @@ static inline bool llcrypt_has_encryptio
16  
17  static inline bool llcrypt_dummy_context_enabled(struct inode *inode)
18  {
19 -       return inode->i_sb->s_cop->dummy_context &&
20 -               inode->i_sb->s_cop->dummy_context(inode);
21 +       struct lustre_sb_info *lsi = s2lsi(inode->i_sb);
22 +
23 +       if (unlikely(!lsi))
24 +               return false;
25 +
26 +       return lsi->lsi_cop->dummy_context &&
27 +               lsi->lsi_cop->dummy_context(inode);
28  }
29  
30  /*
31 @@ -275,9 +281,12 @@ extern const char *llcrypt_get_symlink(s
32                                        unsigned int max_size,
33                                        struct delayed_call *done);
34  static inline void llcrypt_set_ops(struct super_block *sb,
35 -                                  const struct llcrypt_operations *s_cop)
36 +                                  const struct llcrypt_operations *lsi_cop)
37  {
38 -       sb->s_cop = s_cop;
39 +       struct lustre_sb_info *lsi = s2lsi(sb);
40 +
41 +       if (lsi)
42 +               lsi->lsi_cop = lsi_cop;
43  }
44  #else  /* !CONFIG_LL_ENCRYPTION */
45  
46 @@ -557,7 +566,7 @@ static inline const char *llcrypt_get_sy
47  }
48  
49  static inline void llcrypt_set_ops(struct super_block *sb,
50 -                                  const struct llcrypt_operations *s_cop)
51 +                                  const struct llcrypt_operations *lsi_cop)
52  {
53  }
54  
55 --- a/libcfs/libcfs/crypto/fname.c
56 +++ b/libcfs/libcfs/crypto/fname.c
57 @@ -333,8 +333,12 @@ int llcrypt_setup_filename(struct inode
58                 return ret;
59  
60         if (llcrypt_has_encryption_key(dir)) {
61 +               struct lustre_sb_info *lsi = s2lsi(dir->i_sb);
62 +
63                 if (!llcrypt_fname_encrypted_size(dir, iname->len,
64 -                                                 dir->i_sb->s_cop->max_namelen,
65 +                                                 lsi ?
66 +                                                   lsi->lsi_cop->max_namelen :
67 +                                                   NAME_MAX,
68                                                   &fname->crypto_buf.len))
69                         return -ENAMETOOLONG;
70                 fname->crypto_buf.name = kmalloc(fname->crypto_buf.len,
71 --- a/libcfs/libcfs/crypto/keysetup.c
72 +++ b/libcfs/libcfs/crypto/keysetup.c
73 @@ -424,16 +424,20 @@ int llcrypt_get_encryption_info(struct i
74         union llcrypt_context ctx;
75         struct llcrypt_mode *mode;
76         struct key *master_key = NULL;
77 +       struct lustre_sb_info *lsi = s2lsi(inode->i_sb);
78         int res;
79  
80         if (llcrypt_has_encryption_key(inode))
81                 return 0;
82  
83 -       res = llcrypt_initialize(inode->i_sb->s_cop->flags);
84 +       if (!lsi)
85 +               return -ENOKEY;
86 +
87 +       res = llcrypt_initialize(lsi->lsi_cop->flags);
88         if (res)
89                 return res;
90  
91 -       res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
92 +       res = lsi->lsi_cop->get_context(inode, &ctx, sizeof(ctx));
93         if (res < 0) {
94                 if (!llcrypt_dummy_context_enabled(inode) ||
95                     IS_ENCRYPTED(inode)) {
96 --- a/libcfs/libcfs/crypto/keysetup_v1.c
97 +++ b/libcfs/libcfs/crypto/keysetup_v1.c
98 @@ -325,10 +325,16 @@ int llcrypt_setup_v1_file_key_via_subscr
99         key = find_and_lock_process_key(LLCRYPT_KEY_DESC_PREFIX,
100                                         ci->ci_policy.v1.master_key_descriptor,
101                                         ci->ci_mode->keysize, &payload);
102 -       if (key == ERR_PTR(-ENOKEY) && ci->ci_inode->i_sb->s_cop->key_prefix) {
103 -               key = find_and_lock_process_key(ci->ci_inode->i_sb->s_cop->key_prefix,
104 -                                               ci->ci_policy.v1.master_key_descriptor,
105 -                                               ci->ci_mode->keysize, &payload);
106 +       if (key == ERR_PTR(-ENOKEY)) {
107 +               struct lustre_sb_info *lsi = s2lsi(ci->ci_inode->i_sb);
108 +
109 +               if (lsi && lsi->lsi_cop->key_prefix) {
110 +                       key =
111 +                           find_and_lock_process_key(lsi->lsi_cop->key_prefix,
112 +                                                     ci->ci_policy.v1.master_key_descriptor,
113 +                                                     ci->ci_mode->keysize,
114 +                                                     &payload);
115 +               }
116         }
117         if (IS_ERR(key))
118                 return PTR_ERR(key);
119 --- a/libcfs/libcfs/crypto/policy.c
120 +++ b/libcfs/libcfs/crypto/policy.c
121 @@ -209,6 +209,7 @@ static int llcrypt_get_policy(struct ino
122  {
123         const struct llcrypt_info *ci;
124         union llcrypt_context ctx;
125 +       struct lustre_sb_info *lsi = s2lsi(inode->i_sb);
126         int ret;
127  
128         ci = READ_ONCE(inode->i_crypt_info);
129 @@ -221,7 +222,10 @@ static int llcrypt_get_policy(struct ino
130         if (!IS_ENCRYPTED(inode))
131                 return -ENODATA;
132  
133 -       ret = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
134 +       if (!lsi)
135 +               return -ENODATA;
136 +
137 +       ret = lsi->lsi_cop->get_context(inode, &ctx, sizeof(ctx));
138         if (ret < 0)
139                 return (ret == -ERANGE) ? -EINVAL : ret;
140  
141 @@ -233,6 +237,7 @@ static int set_encryption_policy(struct
142  {
143         union llcrypt_context ctx;
144         int ctxsize;
145 +       struct lustre_sb_info *lsi = s2lsi(inode->i_sb);
146         int err;
147  
148         if (!llcrypt_supported_policy(policy, inode))
149 @@ -267,7 +272,10 @@ static int set_encryption_policy(struct
150  
151         ctxsize = llcrypt_new_context_from_policy(&ctx, policy);
152  
153 -       return inode->i_sb->s_cop->set_context(inode, &ctx, ctxsize, NULL);
154 +       if (!lsi)
155 +               return -EINVAL;
156 +
157 +       return lsi->lsi_cop->set_context(inode, &ctx, ctxsize, NULL);
158  }
159  
160  int llcrypt_ioctl_set_policy(struct file *filp, const void __user *arg)
161 @@ -313,11 +321,13 @@ int llcrypt_ioctl_set_policy(struct file
162  
163         ret = llcrypt_get_policy(inode, &existing_policy);
164         if (ret == -ENODATA) {
165 +               struct lustre_sb_info *lsi = s2lsi(inode->i_sb);
166 +
167                 if (!S_ISDIR(inode->i_mode))
168                         ret = -ENOTDIR;
169                 else if (IS_DEADDIR(inode))
170                         ret = -ENOENT;
171 -               else if (!inode->i_sb->s_cop->empty_dir(inode))
172 +               else if (lsi && !lsi->lsi_cop->empty_dir(inode))
173                         ret = -ENOTEMPTY;
174                 else
175                         ret = set_encryption_policy(inode, &policy);
176 @@ -472,6 +482,7 @@ int llcrypt_inherit_context(struct inode
177         union llcrypt_context ctx;
178         int ctxsize;
179         struct llcrypt_info *ci;
180 +       struct lustre_sb_info *lsi = s2lsi(parent->i_sb);
181         int res;
182  
183         res = llcrypt_get_encryption_info(parent);
184 @@ -482,10 +493,13 @@ int llcrypt_inherit_context(struct inode
185         if (ci == NULL)
186                 return -ENOKEY;
187  
188 +       if (!lsi)
189 +               return -ENOKEY;
190 +
191         ctxsize = llcrypt_new_context_from_policy(&ctx, &ci->ci_policy);
192  
193         BUILD_BUG_ON(sizeof(ctx) != LLCRYPT_SET_CONTEXT_MAX_SIZE);
194 -       res = parent->i_sb->s_cop->set_context(child, &ctx, ctxsize, fs_data);
195 +       res = lsi->lsi_cop->set_context(child, &ctx, ctxsize, fs_data);
196         if (res)
197                 return res;
198         return preload ? llcrypt_get_encryption_info(child): 0;