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 = d_find_any_alias(inode);
38 rc = ll_vfs_getxattr(dentry, inode, LL_XATTR_NAME_ENCRYPTION_CONTEXT,
43 /* used as encryption unit size */
44 if (S_ISREG(inode->i_mode))
45 inode->i_blkbits = LUSTRE_ENCRYPTION_BLOCKBITS;
49 int ll_set_encflags(struct inode *inode, void *encctx, __u32 encctxlen,
52 unsigned int ext_flags;
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);
61 if (encctx && encctxlen)
62 rc = ll_xattr_cache_insert(inode,
63 LL_XATTR_NAME_ENCRYPTION_CONTEXT,
68 return preload ? llcrypt_get_encryption_info(inode) : 0;
71 /* ll_set_context has 2 distinct behaviors, depending on the value of inode
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.
83 static int ll_set_context(struct inode *inode, const void *ctx, size_t len,
86 struct dentry *dentry;
90 struct md_op_data *op_data = (struct md_op_data *)fs_data;
95 OBD_ALLOC(op_data->op_file_encctx, len);
96 if (op_data->op_file_encctx == NULL)
98 op_data->op_file_encctx_size = len;
99 memcpy(op_data->op_file_encctx, ctx, len);
103 /* Encrypting the root directory is not allowed */
104 if (inode->i_ino == inode->i_sb->s_root->d_inode->i_ino)
107 dentry = (struct dentry *)fs_data;
108 rc = ll_vfs_setxattr(dentry, inode, LL_XATTR_NAME_ENCRYPTION_CONTEXT,
109 ctx, len, XATTR_CREATE);
113 return ll_set_encflags(inode, (void *)ctx, len, false);
116 inline void llcrypt_free_ctx(void *encctx, __u32 size)
119 OBD_FREE(encctx, size);
122 inline bool ll_sbi_has_test_dummy_encryption(struct ll_sb_info *sbi)
124 return unlikely(sbi->ll_flags & LL_SBI_TEST_DUMMY_ENCRYPTION);
127 static bool ll_dummy_context(struct inode *inode)
129 struct ll_sb_info *sbi = ll_i2sbi(inode);
131 return sbi ? ll_sbi_has_test_dummy_encryption(sbi) : false;
134 inline bool ll_sbi_has_encrypt(struct ll_sb_info *sbi)
136 return sbi->ll_flags & LL_SBI_ENCRYPT;
139 inline void ll_sbi_set_encrypt(struct ll_sb_info *sbi, bool set)
142 sbi->ll_flags |= LL_SBI_ENCRYPT;
145 ~(LL_SBI_ENCRYPT | LL_SBI_TEST_DUMMY_ENCRYPTION);
148 static bool ll_empty_dir(struct inode *inode)
150 /* used by llcrypt_ioctl_set_policy(), because a policy can only be set
153 /* Here we choose to return true, meaning we always call .set_context.
154 * Then we rely on server side, with mdd_fix_attr() that calls
155 * mdd_dir_is_empty() when setting encryption flag on directory.
160 const struct llcrypt_operations lustre_cryptops = {
161 .key_prefix = "lustre:",
162 .get_context = ll_get_context,
163 .set_context = ll_set_context,
164 .dummy_context = ll_dummy_context,
165 .empty_dir = ll_empty_dir,
166 .max_namelen = NAME_MAX,
168 #else /* !HAVE_LUSTRE_CRYPTO */
169 int ll_set_encflags(struct inode *inode, void *encctx, __u32 encctxlen,
175 inline void llcrypt_free_ctx(void *encctx, __u32 size)
179 inline bool ll_sbi_has_test_dummy_encryption(struct ll_sb_info *sbi)
184 inline bool ll_sbi_has_encrypt(struct ll_sb_info *sbi)
189 inline void ll_sbi_set_encrypt(struct ll_sb_info *sbi, bool set)