Whamcloud - gitweb
LU-12275 sec: enable client side encryption
[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;
36         int rc;
37
38         if (hlist_empty(&inode->i_dentry))
39                 return -ENODATA;
40
41         hlist_for_each_entry(dentry, &inode->i_dentry, d_alias) {
42                 break;
43         }
44
45         rc = ll_vfs_getxattr(dentry, inode, LL_XATTR_NAME_ENCRYPTION_CONTEXT,
46                              ctx, len);
47
48         return rc;
49 }
50
51 static int ll_set_context(struct inode *inode, const void *ctx, size_t len,
52                           void *fs_data)
53 {
54         unsigned int ext_flags;
55         struct dentry *dentry;
56         struct md_op_data *op_data;
57         struct ptlrpc_request *req = NULL;
58         int rc;
59
60         if (inode == NULL)
61                 return 0;
62
63         ext_flags = ll_inode_to_ext_flags(inode->i_flags) | LUSTRE_ENCRYPT_FL;
64         dentry = (struct dentry *)fs_data;
65
66         /* Encrypting the root directory is not allowed */
67         if (inode->i_ino == inode->i_sb->s_root->d_inode->i_ino)
68                 return -EPERM;
69
70         op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
71                                      LUSTRE_OPC_ANY, NULL);
72         if (IS_ERR(op_data))
73                 return PTR_ERR(op_data);
74
75         op_data->op_attr_flags = LUSTRE_ENCRYPT_FL;
76         op_data->op_xvalid |= OP_XVALID_FLAGS;
77         rc = md_setattr(ll_i2sbi(inode)->ll_md_exp, op_data, NULL, 0, &req);
78         ll_finish_md_op_data(op_data);
79         ptlrpc_req_finished(req);
80         if (rc)
81                 return rc;
82
83         rc = ll_vfs_setxattr(dentry, inode, LL_XATTR_NAME_ENCRYPTION_CONTEXT,
84                              ctx, len, XATTR_CREATE);
85         if (rc)
86                 return rc;
87
88         ll_update_inode_flags(inode, ext_flags);
89         return 0;
90 }
91
92 inline bool ll_sbi_has_test_dummy_encryption(struct ll_sb_info *sbi)
93 {
94         return unlikely(sbi->ll_flags & LL_SBI_TEST_DUMMY_ENCRYPTION);
95 }
96
97 static bool ll_dummy_context(struct inode *inode)
98 {
99         struct ll_sb_info *sbi = ll_i2sbi(inode);
100
101         return sbi ? ll_sbi_has_test_dummy_encryption(sbi) : false;
102 }
103
104 inline bool ll_sbi_has_encrypt(struct ll_sb_info *sbi)
105 {
106         return sbi->ll_flags & LL_SBI_ENCRYPT;
107 }
108
109 inline void ll_sbi_set_encrypt(struct ll_sb_info *sbi, bool set)
110 {
111         if (set)
112                 sbi->ll_flags |= LL_SBI_ENCRYPT;
113         else
114                 sbi->ll_flags &=
115                         ~(LL_SBI_ENCRYPT | LL_SBI_TEST_DUMMY_ENCRYPTION);
116 }
117
118 static bool ll_empty_dir(struct inode *inode)
119 {
120         /* used by llcrypt_ioctl_set_policy(), because a policy can only be set
121          * on an empty dir.
122          */
123         /* Here we choose to return true, meaning we always call .set_context.
124          * Then we rely on server side, with mdd_fix_attr() that calls
125          * mdd_dir_is_empty() when setting encryption flag on directory.
126          */
127         return true;
128 }
129
130 const struct llcrypt_operations lustre_cryptops = {
131         .key_prefix             = "lustre:",
132         .get_context            = ll_get_context,
133         .set_context            = ll_set_context,
134         .dummy_context          = ll_dummy_context,
135         .empty_dir              = ll_empty_dir,
136         .max_namelen            = NAME_MAX,
137 };
138 #else /* !HAVE_LUSTRE_CRYPTO */
139 inline bool ll_sbi_has_test_dummy_encryption(struct ll_sb_info *sbi)
140 {
141         return false;
142 }
143
144 inline bool ll_sbi_has_encrypt(struct ll_sb_info *sbi)
145 {
146         return false;
147 }
148
149 inline void ll_sbi_set_encrypt(struct ll_sb_info *sbi, bool set)
150 {
151 }
152 #endif
153