Whamcloud - gitweb
LU-10948 llite: Introduce inode open heat counter
[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 (is_root_inode(inode))
105                 return -EPERM;
106
107         dentry = (struct dentry *)fs_data;
108         set_bit(LLIF_SET_ENC_CTX, &ll_i2info(inode)->lli_flags);
109         rc = ll_vfs_setxattr(dentry, inode, LL_XATTR_NAME_ENCRYPTION_CONTEXT,
110                              ctx, len, XATTR_CREATE);
111         if (rc)
112                 return rc;
113
114         return ll_set_encflags(inode, (void *)ctx, len, false);
115 }
116
117 void llcrypt_free_ctx(void *encctx, __u32 size)
118 {
119         if (encctx)
120                 OBD_FREE(encctx, size);
121 }
122
123 bool ll_sbi_has_test_dummy_encryption(struct ll_sb_info *sbi)
124 {
125         return unlikely(sbi->ll_flags & LL_SBI_TEST_DUMMY_ENCRYPTION);
126 }
127
128 static bool ll_dummy_context(struct inode *inode)
129 {
130         struct ll_sb_info *sbi = ll_i2sbi(inode);
131
132         return sbi ? ll_sbi_has_test_dummy_encryption(sbi) : false;
133 }
134
135 bool ll_sbi_has_encrypt(struct ll_sb_info *sbi)
136 {
137         return sbi->ll_flags & LL_SBI_ENCRYPT;
138 }
139
140 void ll_sbi_set_encrypt(struct ll_sb_info *sbi, bool set)
141 {
142         if (set)
143                 sbi->ll_flags |= LL_SBI_ENCRYPT;
144         else
145                 sbi->ll_flags &=
146                         ~(LL_SBI_ENCRYPT | LL_SBI_TEST_DUMMY_ENCRYPTION);
147 }
148
149 static bool ll_empty_dir(struct inode *inode)
150 {
151         /* used by llcrypt_ioctl_set_policy(), because a policy can only be set
152          * on an empty dir.
153          */
154         /* Here we choose to return true, meaning we always call .set_context.
155          * Then we rely on server side, with mdd_fix_attr() that calls
156          * mdd_dir_is_empty() when setting encryption flag on directory.
157          */
158         return true;
159 }
160
161 const struct llcrypt_operations lustre_cryptops = {
162         .key_prefix             = "lustre:",
163         .get_context            = ll_get_context,
164         .set_context            = ll_set_context,
165         .dummy_context          = ll_dummy_context,
166         .empty_dir              = ll_empty_dir,
167         .max_namelen            = NAME_MAX,
168 };
169 #else /* !HAVE_LUSTRE_CRYPTO */
170 int ll_set_encflags(struct inode *inode, void *encctx, __u32 encctxlen,
171                     bool preload)
172 {
173         return 0;
174 }
175
176 void llcrypt_free_ctx(void *encctx, __u32 size)
177 {
178 }
179
180 bool ll_sbi_has_test_dummy_encryption(struct ll_sb_info *sbi)
181 {
182         return false;
183 }
184
185 bool ll_sbi_has_encrypt(struct ll_sb_info *sbi)
186 {
187         return false;
188 }
189
190 void ll_sbi_set_encrypt(struct ll_sb_info *sbi, bool set)
191 {
192 }
193 #endif
194