4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
23 * Copyright (c) 2019, 2020, Whamcloud.
26 * This file is part of Lustre, http://www.lustre.org/
29 #include "llite_internal.h"
31 #ifdef HAVE_LUSTRE_CRYPTO
33 static int ll_get_context(struct inode *inode, void *ctx, size_t len)
35 struct dentry *dentry;
38 if (hlist_empty(&inode->i_dentry))
41 hlist_for_each_entry(dentry, &inode->i_dentry, d_alias) {
45 rc = ll_vfs_getxattr(dentry, inode, LL_XATTR_NAME_ENCRYPTION_CONTEXT,
51 static int ll_set_context(struct inode *inode, const void *ctx, size_t len,
54 unsigned int ext_flags;
55 struct dentry *dentry;
56 struct md_op_data *op_data;
57 struct ptlrpc_request *req = NULL;
63 ext_flags = ll_inode_to_ext_flags(inode->i_flags) | LUSTRE_ENCRYPT_FL;
64 dentry = (struct dentry *)fs_data;
66 /* Encrypting the root directory is not allowed */
67 if (inode->i_ino == inode->i_sb->s_root->d_inode->i_ino)
70 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
71 LUSTRE_OPC_ANY, NULL);
73 return PTR_ERR(op_data);
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);
83 rc = ll_vfs_setxattr(dentry, inode, LL_XATTR_NAME_ENCRYPTION_CONTEXT,
84 ctx, len, XATTR_CREATE);
88 ll_update_inode_flags(inode, ext_flags);
92 inline bool ll_sbi_has_test_dummy_encryption(struct ll_sb_info *sbi)
94 return unlikely(sbi->ll_flags & LL_SBI_TEST_DUMMY_ENCRYPTION);
97 static bool ll_dummy_context(struct inode *inode)
99 struct ll_sb_info *sbi = ll_i2sbi(inode);
101 return sbi ? ll_sbi_has_test_dummy_encryption(sbi) : false;
104 inline bool ll_sbi_has_encrypt(struct ll_sb_info *sbi)
106 return sbi->ll_flags & LL_SBI_ENCRYPT;
109 inline void ll_sbi_set_encrypt(struct ll_sb_info *sbi, bool set)
112 sbi->ll_flags |= LL_SBI_ENCRYPT;
115 ~(LL_SBI_ENCRYPT | LL_SBI_TEST_DUMMY_ENCRYPTION);
118 static bool ll_empty_dir(struct inode *inode)
120 /* used by llcrypt_ioctl_set_policy(), because a policy can only be set
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.
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,
138 #else /* !HAVE_LUSTRE_CRYPTO */
139 inline bool ll_sbi_has_test_dummy_encryption(struct ll_sb_info *sbi)
144 inline bool ll_sbi_has_encrypt(struct ll_sb_info *sbi)
149 inline void ll_sbi_set_encrypt(struct ll_sb_info *sbi, bool set)