Whamcloud - gitweb
LU-6142 lustre: remove non-static 'inline' markings.
[fs/lustre-release.git] / lustre / llite / crypto.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2019, 2020, Whamcloud.
24  */
25 /*
26  * This file is part of Lustre, http://www.lustre.org/
27  */
28
29 #include "llite_internal.h"
30
31 #ifdef HAVE_LUSTRE_CRYPTO
32
33 static int ll_get_context(struct inode *inode, void *ctx, size_t len)
34 {
35         struct dentry *dentry = d_find_any_alias(inode);
36         int rc;
37
38         rc = ll_vfs_getxattr(dentry, inode, LL_XATTR_NAME_ENCRYPTION_CONTEXT,
39                              ctx, len);
40         if (dentry)
41                 dput(dentry);
42
43         /* used as encryption unit size */
44         if (S_ISREG(inode->i_mode))
45                 inode->i_blkbits = LUSTRE_ENCRYPTION_BLOCKBITS;
46         return rc;
47 }
48
49 int ll_set_encflags(struct inode *inode, void *encctx, __u32 encctxlen,
50                     bool preload)
51 {
52         unsigned int ext_flags;
53         int rc = 0;
54
55         /* used as encryption unit size */
56         if (S_ISREG(inode->i_mode))
57                 inode->i_blkbits = LUSTRE_ENCRYPTION_BLOCKBITS;
58         ext_flags = ll_inode_to_ext_flags(inode->i_flags) | LUSTRE_ENCRYPT_FL;
59         ll_update_inode_flags(inode, ext_flags);
60
61         if (encctx && encctxlen)
62                 rc = ll_xattr_cache_insert(inode,
63                                            LL_XATTR_NAME_ENCRYPTION_CONTEXT,
64                                            encctx, encctxlen);
65         if (rc)
66                 return rc;
67
68         return preload ? llcrypt_get_encryption_info(inode) : 0;
69 }
70
71 /* ll_set_context has 2 distinct behaviors, depending on the value of inode
72  * parameter:
73  * - inode is NULL:
74  *   passed fs_data is a struct md_op_data *. We need to store enc ctx in
75  *   op_data, so that it will be sent along to the server with the request that
76  *   the caller is preparing, thus saving a setxattr request.
77  * - inode is not NULL:
78  *   normal case in which passed fs_data is a struct dentry *, letting proceed
79  *   with setxattr operation.
80  *   This use case should only be used when explicitly setting a new encryption
81  *   policy on an existing, empty directory.
82  */
83 static int ll_set_context(struct inode *inode, const void *ctx, size_t len,
84                           void *fs_data)
85 {
86         struct dentry *dentry;
87         int rc;
88
89         if (inode == NULL) {
90                 struct md_op_data *op_data = (struct md_op_data *)fs_data;
91
92                 if (!op_data)
93                         return -EINVAL;
94
95                 OBD_ALLOC(op_data->op_file_encctx, len);
96                 if (op_data->op_file_encctx == NULL)
97                         return -ENOMEM;
98                 op_data->op_file_encctx_size = len;
99                 memcpy(op_data->op_file_encctx, ctx, len);
100                 return 0;
101         }
102
103         /* Encrypting the root directory is not allowed */
104         if (inode->i_ino == inode->i_sb->s_root->d_inode->i_ino)
105                 return -EPERM;
106
107         dentry = (struct dentry *)fs_data;
108         rc = ll_vfs_setxattr(dentry, inode, LL_XATTR_NAME_ENCRYPTION_CONTEXT,
109                              ctx, len, XATTR_CREATE);
110         if (rc)
111                 return rc;
112
113         return ll_set_encflags(inode, (void *)ctx, len, false);
114 }
115
116 void llcrypt_free_ctx(void *encctx, __u32 size)
117 {
118         if (encctx)
119                 OBD_FREE(encctx, size);
120 }
121
122 bool ll_sbi_has_test_dummy_encryption(struct ll_sb_info *sbi)
123 {
124         return unlikely(sbi->ll_flags & LL_SBI_TEST_DUMMY_ENCRYPTION);
125 }
126
127 static bool ll_dummy_context(struct inode *inode)
128 {
129         struct ll_sb_info *sbi = ll_i2sbi(inode);
130
131         return sbi ? ll_sbi_has_test_dummy_encryption(sbi) : false;
132 }
133
134 bool ll_sbi_has_encrypt(struct ll_sb_info *sbi)
135 {
136         return sbi->ll_flags & LL_SBI_ENCRYPT;
137 }
138
139 void ll_sbi_set_encrypt(struct ll_sb_info *sbi, bool set)
140 {
141         if (set)
142                 sbi->ll_flags |= LL_SBI_ENCRYPT;
143         else
144                 sbi->ll_flags &=
145                         ~(LL_SBI_ENCRYPT | LL_SBI_TEST_DUMMY_ENCRYPTION);
146 }
147
148 static bool ll_empty_dir(struct inode *inode)
149 {
150         /* used by llcrypt_ioctl_set_policy(), because a policy can only be set
151          * on an empty dir.
152          */
153         /* Here we choose to return true, meaning we always call .set_context.
154          * Then we rely on server side, with mdd_fix_attr() that calls
155          * mdd_dir_is_empty() when setting encryption flag on directory.
156          */
157         return true;
158 }
159
160 const struct llcrypt_operations lustre_cryptops = {
161         .key_prefix             = "lustre:",
162         .get_context            = ll_get_context,
163         .set_context            = ll_set_context,
164         .dummy_context          = ll_dummy_context,
165         .empty_dir              = ll_empty_dir,
166         .max_namelen            = NAME_MAX,
167 };
168 #else /* !HAVE_LUSTRE_CRYPTO */
169 int ll_set_encflags(struct inode *inode, void *encctx, __u32 encctxlen,
170                     bool preload)
171 {
172         return 0;
173 }
174
175 void llcrypt_free_ctx(void *encctx, __u32 size)
176 {
177 }
178
179 bool ll_sbi_has_test_dummy_encryption(struct ll_sb_info *sbi)
180 {
181         return false;
182 }
183
184 bool ll_sbi_has_encrypt(struct ll_sb_info *sbi)
185 {
186         return false;
187 }
188
189 void ll_sbi_set_encrypt(struct ll_sb_info *sbi, bool set)
190 {
191 }
192 #endif
193