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,
48 /* used as encryption unit size */
49 if (S_ISREG(inode->i_mode))
50 inode->i_blkbits = LUSTRE_ENCRYPTION_BLOCKBITS;
54 static int ll_set_context(struct inode *inode, const void *ctx, size_t len,
57 unsigned int ext_flags;
58 struct dentry *dentry;
59 struct md_op_data *op_data;
60 struct ptlrpc_request *req = NULL;
66 ext_flags = ll_inode_to_ext_flags(inode->i_flags) | LUSTRE_ENCRYPT_FL;
67 dentry = (struct dentry *)fs_data;
69 /* Encrypting the root directory is not allowed */
70 if (inode->i_ino == inode->i_sb->s_root->d_inode->i_ino)
73 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
74 LUSTRE_OPC_ANY, NULL);
76 return PTR_ERR(op_data);
78 op_data->op_attr_flags = LUSTRE_ENCRYPT_FL;
79 op_data->op_xvalid |= OP_XVALID_FLAGS;
80 rc = md_setattr(ll_i2sbi(inode)->ll_md_exp, op_data, NULL, 0, &req);
81 ll_finish_md_op_data(op_data);
82 ptlrpc_req_finished(req);
86 rc = ll_vfs_setxattr(dentry, inode, LL_XATTR_NAME_ENCRYPTION_CONTEXT,
87 ctx, len, XATTR_CREATE);
91 /* used as encryption unit size */
92 if (S_ISREG(inode->i_mode))
93 inode->i_blkbits = LUSTRE_ENCRYPTION_BLOCKBITS;
94 ll_update_inode_flags(inode, ext_flags);
98 inline bool ll_sbi_has_test_dummy_encryption(struct ll_sb_info *sbi)
100 return unlikely(sbi->ll_flags & LL_SBI_TEST_DUMMY_ENCRYPTION);
103 static bool ll_dummy_context(struct inode *inode)
105 struct ll_sb_info *sbi = ll_i2sbi(inode);
107 return sbi ? ll_sbi_has_test_dummy_encryption(sbi) : false;
110 inline bool ll_sbi_has_encrypt(struct ll_sb_info *sbi)
112 return sbi->ll_flags & LL_SBI_ENCRYPT;
115 inline void ll_sbi_set_encrypt(struct ll_sb_info *sbi, bool set)
118 sbi->ll_flags |= LL_SBI_ENCRYPT;
121 ~(LL_SBI_ENCRYPT | LL_SBI_TEST_DUMMY_ENCRYPTION);
124 static bool ll_empty_dir(struct inode *inode)
126 /* used by llcrypt_ioctl_set_policy(), because a policy can only be set
129 /* Here we choose to return true, meaning we always call .set_context.
130 * Then we rely on server side, with mdd_fix_attr() that calls
131 * mdd_dir_is_empty() when setting encryption flag on directory.
136 const struct llcrypt_operations lustre_cryptops = {
137 .key_prefix = "lustre:",
138 .get_context = ll_get_context,
139 .set_context = ll_set_context,
140 .dummy_context = ll_dummy_context,
141 .empty_dir = ll_empty_dir,
142 .max_namelen = NAME_MAX,
144 #else /* !HAVE_LUSTRE_CRYPTO */
145 inline bool ll_sbi_has_test_dummy_encryption(struct ll_sb_info *sbi)
150 inline bool ll_sbi_has_encrypt(struct ll_sb_info *sbi)
155 inline void ll_sbi_set_encrypt(struct ll_sb_info *sbi, bool set)