Whamcloud - gitweb
LU-12275 sec: encryption for write path
[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         /* used as encryption unit size */
89         if (S_ISREG(inode->i_mode))
90                 inode->i_blkbits = LUSTRE_ENCRYPTION_BLOCKBITS;
91         ll_update_inode_flags(inode, ext_flags);
92         return 0;
93 }
94
95 inline bool ll_sbi_has_test_dummy_encryption(struct ll_sb_info *sbi)
96 {
97         return unlikely(sbi->ll_flags & LL_SBI_TEST_DUMMY_ENCRYPTION);
98 }
99
100 static bool ll_dummy_context(struct inode *inode)
101 {
102         struct ll_sb_info *sbi = ll_i2sbi(inode);
103
104         return sbi ? ll_sbi_has_test_dummy_encryption(sbi) : false;
105 }
106
107 inline bool ll_sbi_has_encrypt(struct ll_sb_info *sbi)
108 {
109         return sbi->ll_flags & LL_SBI_ENCRYPT;
110 }
111
112 inline void ll_sbi_set_encrypt(struct ll_sb_info *sbi, bool set)
113 {
114         if (set)
115                 sbi->ll_flags |= LL_SBI_ENCRYPT;
116         else
117                 sbi->ll_flags &=
118                         ~(LL_SBI_ENCRYPT | LL_SBI_TEST_DUMMY_ENCRYPTION);
119 }
120
121 static bool ll_empty_dir(struct inode *inode)
122 {
123         /* used by llcrypt_ioctl_set_policy(), because a policy can only be set
124          * on an empty dir.
125          */
126         /* Here we choose to return true, meaning we always call .set_context.
127          * Then we rely on server side, with mdd_fix_attr() that calls
128          * mdd_dir_is_empty() when setting encryption flag on directory.
129          */
130         return true;
131 }
132
133 const struct llcrypt_operations lustre_cryptops = {
134         .key_prefix             = "lustre:",
135         .get_context            = ll_get_context,
136         .set_context            = ll_set_context,
137         .dummy_context          = ll_dummy_context,
138         .empty_dir              = ll_empty_dir,
139         .max_namelen            = NAME_MAX,
140 };
141 #else /* !HAVE_LUSTRE_CRYPTO */
142 inline bool ll_sbi_has_test_dummy_encryption(struct ll_sb_info *sbi)
143 {
144         return false;
145 }
146
147 inline bool ll_sbi_has_encrypt(struct ll_sb_info *sbi)
148 {
149         return false;
150 }
151
152 inline void ll_sbi_set_encrypt(struct ll_sb_info *sbi, bool set)
153 {
154 }
155 #endif
156