Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

fscrypt: replace raw loads of info pointer with helper function

Add and use a helper function fscrypt_get_inode_info_raw(). It loads an
inode's fscrypt info pointer using a raw dereference, which is
appropriate when the caller knows the key setup already happened.

This eliminates most occurrences of inode::i_crypt_info in the source,
in preparation for replacing that with a filesystem-specific field.

Co-developed-by: Christian Brauner <brauner@kernel.org>
Signed-off-by: Eric Biggers <ebiggers@kernel.org>
Link: https://lore.kernel.org/20250810075706.172910-2-ebiggers@kernel.org
Signed-off-by: Christian Brauner <brauner@kernel.org>

authored by

Eric Biggers and committed by
Christian Brauner
6c9468aa 8f5ae30d

+43 -21
+1 -1
fs/crypto/bio.c
··· 113 113 int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk, 114 114 sector_t pblk, unsigned int len) 115 115 { 116 - const struct fscrypt_inode_info *ci = inode->i_crypt_info; 116 + const struct fscrypt_inode_info *ci = fscrypt_get_inode_info_raw(inode); 117 117 const unsigned int du_bits = ci->ci_data_unit_bits; 118 118 const unsigned int du_size = 1U << du_bits; 119 119 const unsigned int du_per_page_bits = PAGE_SHIFT - du_bits;
+8 -6
fs/crypto/crypto.c
··· 173 173 size_t len, size_t offs, gfp_t gfp_flags) 174 174 { 175 175 const struct inode *inode = folio->mapping->host; 176 - const struct fscrypt_inode_info *ci = inode->i_crypt_info; 176 + const struct fscrypt_inode_info *ci = fscrypt_get_inode_info_raw(inode); 177 177 const unsigned int du_bits = ci->ci_data_unit_bits; 178 178 const unsigned int du_size = 1U << du_bits; 179 179 struct page *ciphertext_page; ··· 232 232 { 233 233 if (WARN_ON_ONCE(inode->i_sb->s_cop->supports_subblock_data_units)) 234 234 return -EOPNOTSUPP; 235 - return fscrypt_crypt_data_unit(inode->i_crypt_info, FS_ENCRYPT, 236 - lblk_num, page, page, len, offs); 235 + return fscrypt_crypt_data_unit(fscrypt_get_inode_info_raw(inode), 236 + FS_ENCRYPT, lblk_num, page, page, len, 237 + offs); 237 238 } 238 239 EXPORT_SYMBOL(fscrypt_encrypt_block_inplace); 239 240 ··· 256 255 size_t offs) 257 256 { 258 257 const struct inode *inode = folio->mapping->host; 259 - const struct fscrypt_inode_info *ci = inode->i_crypt_info; 258 + const struct fscrypt_inode_info *ci = fscrypt_get_inode_info_raw(inode); 260 259 const unsigned int du_bits = ci->ci_data_unit_bits; 261 260 const unsigned int du_size = 1U << du_bits; 262 261 u64 index = ((u64)folio->index << (PAGE_SHIFT - du_bits)) + ··· 306 305 { 307 306 if (WARN_ON_ONCE(inode->i_sb->s_cop->supports_subblock_data_units)) 308 307 return -EOPNOTSUPP; 309 - return fscrypt_crypt_data_unit(inode->i_crypt_info, FS_DECRYPT, 310 - lblk_num, page, page, len, offs); 308 + return fscrypt_crypt_data_unit(fscrypt_get_inode_info_raw(inode), 309 + FS_DECRYPT, lblk_num, page, page, len, 310 + offs); 311 311 } 312 312 EXPORT_SYMBOL(fscrypt_decrypt_block_inplace); 313 313
+6 -5
fs/crypto/fname.c
··· 94 94 int fscrypt_fname_encrypt(const struct inode *inode, const struct qstr *iname, 95 95 u8 *out, unsigned int olen) 96 96 { 97 - const struct fscrypt_inode_info *ci = inode->i_crypt_info; 97 + const struct fscrypt_inode_info *ci = fscrypt_get_inode_info_raw(inode); 98 98 struct crypto_sync_skcipher *tfm = ci->ci_enc_key.tfm; 99 99 SYNC_SKCIPHER_REQUEST_ON_STACK(req, tfm); 100 100 union fscrypt_iv iv; ··· 138 138 const struct fscrypt_str *iname, 139 139 struct fscrypt_str *oname) 140 140 { 141 - const struct fscrypt_inode_info *ci = inode->i_crypt_info; 141 + const struct fscrypt_inode_info *ci = fscrypt_get_inode_info_raw(inode); 142 142 struct crypto_sync_skcipher *tfm = ci->ci_enc_key.tfm; 143 143 SYNC_SKCIPHER_REQUEST_ON_STACK(req, tfm); 144 144 union fscrypt_iv iv; ··· 274 274 bool fscrypt_fname_encrypted_size(const struct inode *inode, u32 orig_len, 275 275 u32 max_len, u32 *encrypted_len_ret) 276 276 { 277 - return __fscrypt_fname_encrypted_size(&inode->i_crypt_info->ci_policy, 278 - orig_len, max_len, 277 + const struct fscrypt_inode_info *ci = fscrypt_get_inode_info_raw(inode); 278 + 279 + return __fscrypt_fname_encrypted_size(&ci->ci_policy, orig_len, max_len, 279 280 encrypted_len_ret); 280 281 } 281 282 EXPORT_SYMBOL_GPL(fscrypt_fname_encrypted_size); ··· 544 543 */ 545 544 u64 fscrypt_fname_siphash(const struct inode *dir, const struct qstr *name) 546 545 { 547 - const struct fscrypt_inode_info *ci = dir->i_crypt_info; 546 + const struct fscrypt_inode_info *ci = fscrypt_get_inode_info_raw(dir); 548 547 549 548 WARN_ON_ONCE(!ci->ci_dirhash_key_initialized); 550 549
+1 -1
fs/crypto/hooks.c
··· 199 199 err = fscrypt_require_key(inode); 200 200 if (err) 201 201 return err; 202 - ci = inode->i_crypt_info; 202 + ci = fscrypt_get_inode_info_raw(inode); 203 203 if (ci->ci_policy.version != FSCRYPT_POLICY_V2) 204 204 return -EINVAL; 205 205 mk = ci->ci_master_key;
+7 -5
fs/crypto/inline_crypt.c
··· 263 263 264 264 bool __fscrypt_inode_uses_inline_crypto(const struct inode *inode) 265 265 { 266 - return inode->i_crypt_info->ci_inlinecrypt; 266 + return fscrypt_get_inode_info_raw(inode)->ci_inlinecrypt; 267 267 } 268 268 EXPORT_SYMBOL_GPL(__fscrypt_inode_uses_inline_crypto); 269 269 ··· 307 307 308 308 if (!fscrypt_inode_uses_inline_crypto(inode)) 309 309 return; 310 - ci = inode->i_crypt_info; 310 + ci = fscrypt_get_inode_info_raw(inode); 311 311 312 312 fscrypt_generate_dun(ci, first_lblk, dun); 313 313 bio_crypt_set_ctx(bio, ci->ci_enc_key.blk_key, dun, gfp_mask); ··· 385 385 u64 next_lblk) 386 386 { 387 387 const struct bio_crypt_ctx *bc = bio->bi_crypt_context; 388 + const struct fscrypt_inode_info *ci; 388 389 u64 next_dun[BLK_CRYPTO_DUN_ARRAY_SIZE]; 389 390 390 391 if (!!bc != fscrypt_inode_uses_inline_crypto(inode)) 391 392 return false; 392 393 if (!bc) 393 394 return true; 395 + ci = fscrypt_get_inode_info_raw(inode); 394 396 395 397 /* 396 398 * Comparing the key pointers is good enough, as all I/O for each key 397 399 * uses the same pointer. I.e., there's currently no need to support 398 400 * merging requests where the keys are the same but the pointers differ. 399 401 */ 400 - if (bc->bc_key != inode->i_crypt_info->ci_enc_key.blk_key) 402 + if (bc->bc_key != ci->ci_enc_key.blk_key) 401 403 return false; 402 404 403 - fscrypt_generate_dun(inode->i_crypt_info, next_lblk, next_dun); 405 + fscrypt_generate_dun(ci, next_lblk, next_dun); 404 406 return bio_crypt_dun_is_contiguous(bc, bio->bi_iter.bi_size, next_dun); 405 407 } 406 408 EXPORT_SYMBOL_GPL(fscrypt_mergeable_bio); ··· 504 502 if (nr_blocks <= 1) 505 503 return nr_blocks; 506 504 507 - ci = inode->i_crypt_info; 505 + ci = fscrypt_get_inode_info_raw(inode); 508 506 if (!(fscrypt_policy_flags(&ci->ci_policy) & 509 507 FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32)) 510 508 return nr_blocks;
+4 -3
fs/crypto/policy.c
··· 727 727 err = fscrypt_require_key(dir); 728 728 if (err) 729 729 return ERR_PTR(err); 730 - return &dir->i_crypt_info->ci_policy; 730 + return &fscrypt_get_inode_info_raw(dir)->ci_policy; 731 731 } 732 732 733 733 return fscrypt_get_dummy_policy(dir->i_sb); ··· 746 746 */ 747 747 int fscrypt_context_for_new_inode(void *ctx, struct inode *inode) 748 748 { 749 - struct fscrypt_inode_info *ci = inode->i_crypt_info; 749 + struct fscrypt_inode_info *ci = fscrypt_get_inode_info_raw(inode); 750 750 751 751 BUILD_BUG_ON(sizeof(union fscrypt_context) != 752 752 FSCRYPT_SET_CONTEXT_MAX_SIZE); ··· 771 771 */ 772 772 int fscrypt_set_context(struct inode *inode, void *fs_data) 773 773 { 774 - struct fscrypt_inode_info *ci = inode->i_crypt_info; 774 + struct fscrypt_inode_info *ci; 775 775 union fscrypt_context ctx; 776 776 int ctxsize; 777 777 ··· 783 783 * This may be the first time the inode number is available, so do any 784 784 * delayed key setup that requires the inode number. 785 785 */ 786 + ci = fscrypt_get_inode_info_raw(inode); 786 787 if (ci->ci_policy.version == FSCRYPT_POLICY_V2 && 787 788 (ci->ci_policy.v2.flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32)) 788 789 fscrypt_hash_inode_number(ci, ci->ci_master_key);
+16
include/linux/fscrypt.h
··· 195 195 int fscrypt_d_revalidate(struct inode *dir, const struct qstr *name, 196 196 struct dentry *dentry, unsigned int flags); 197 197 198 + /* 199 + * Load the inode's fscrypt info pointer, using a raw dereference. Since this 200 + * uses a raw dereference with no memory barrier, it is appropriate to use only 201 + * when the caller knows the inode's key setup already happened, resulting in 202 + * non-NULL fscrypt info. E.g., the file contents en/decryption functions use 203 + * this, since fscrypt_file_open() set up the key. 204 + */ 205 + static inline struct fscrypt_inode_info * 206 + fscrypt_get_inode_info_raw(const struct inode *inode) 207 + { 208 + struct fscrypt_inode_info *ci = inode->i_crypt_info; 209 + 210 + VFS_WARN_ON_ONCE(ci == NULL); 211 + return ci; 212 + } 213 + 198 214 static inline struct fscrypt_inode_info * 199 215 fscrypt_get_inode_info(const struct inode *inode) 200 216 {