Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Inline encryption support for fscrypt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright 2019 Google LLC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  * With "inline encryption", the block layer handles the decryption/encryption
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  * as part of the bio, instead of the filesystem doing the crypto itself via
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  * crypto API.  See Documentation/block/inline-encryption.rst.  fscrypt still
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  * provides the key and IV to use.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/blk-crypto.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/blkdev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/buffer_head.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/keyslot-manager.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/sched/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <linux/uio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include "fscrypt_private.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) struct fscrypt_blk_crypto_key {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	struct blk_crypto_key base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	int num_devs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	struct request_queue *devs[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) static int fscrypt_get_num_devices(struct super_block *sb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	if (sb->s_cop->get_num_devices)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 		return sb->s_cop->get_num_devices(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) static void fscrypt_get_devices(struct super_block *sb, int num_devs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 				struct request_queue **devs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	if (num_devs == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 		devs[0] = bdev_get_queue(sb->s_bdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 		sb->s_cop->get_devices(sb, devs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) static unsigned int fscrypt_get_dun_bytes(const struct fscrypt_info *ci)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	struct super_block *sb = ci->ci_inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	unsigned int flags = fscrypt_policy_flags(&ci->ci_policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	int ino_bits = 64, lblk_bits = 64;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	if (flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 		return offsetofend(union fscrypt_iv, nonce);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	if (flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 		return sizeof(__le64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	if (flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 		return sizeof(__le32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	/* Default case: IVs are just the file logical block number */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	if (sb->s_cop->get_ino_and_lblk_bits)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 		sb->s_cop->get_ino_and_lblk_bits(sb, &ino_bits, &lblk_bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	return DIV_ROUND_UP(lblk_bits, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) /* Enable inline encryption for this file if supported. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) int fscrypt_select_encryption_impl(struct fscrypt_info *ci,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 				   bool is_hw_wrapped_key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	const struct inode *inode = ci->ci_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	struct super_block *sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	struct blk_crypto_config crypto_cfg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	int num_devs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	struct request_queue **devs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	/* The file must need contents encryption, not filenames encryption */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	if (!S_ISREG(inode->i_mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	/* The crypto mode must have a blk-crypto counterpart */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	if (ci->ci_mode->blk_crypto_mode == BLK_ENCRYPTION_MODE_INVALID)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	/* The filesystem must be mounted with -o inlinecrypt */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	if (!(sb->s_flags & SB_INLINECRYPT))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	 * When a page contains multiple logically contiguous filesystem blocks,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	 * some filesystem code only calls fscrypt_mergeable_bio() for the first
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	 * block in the page. This is fine for most of fscrypt's IV generation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	 * strategies, where contiguous blocks imply contiguous IVs. But it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	 * doesn't work with IV_INO_LBLK_32. For now, simply exclude
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	 * IV_INO_LBLK_32 with blocksize != PAGE_SIZE from inline encryption.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	if ((fscrypt_policy_flags(&ci->ci_policy) &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	     FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	    sb->s_blocksize != PAGE_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	 * On all the filesystem's devices, blk-crypto must support the crypto
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	 * configuration that the file would use.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	crypto_cfg.crypto_mode = ci->ci_mode->blk_crypto_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	crypto_cfg.data_unit_size = sb->s_blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	crypto_cfg.dun_bytes = fscrypt_get_dun_bytes(ci);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	crypto_cfg.is_hw_wrapped = is_hw_wrapped_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	num_devs = fscrypt_get_num_devices(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	devs = kmalloc_array(num_devs, sizeof(*devs), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	if (!devs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	fscrypt_get_devices(sb, num_devs, devs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	for (i = 0; i < num_devs; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 		if (!blk_crypto_config_supported(devs[i], &crypto_cfg))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 			goto out_free_devs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	ci->ci_inlinecrypt = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) out_free_devs:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	kfree(devs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) int fscrypt_prepare_inline_crypt_key(struct fscrypt_prepared_key *prep_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 				     const u8 *raw_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 				     unsigned int raw_key_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 				     bool is_hw_wrapped,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 				     const struct fscrypt_info *ci)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	const struct inode *inode = ci->ci_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	struct super_block *sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	enum blk_crypto_mode_num crypto_mode = ci->ci_mode->blk_crypto_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	int num_devs = fscrypt_get_num_devices(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	int queue_refs = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	struct fscrypt_blk_crypto_key *blk_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	blk_key = kzalloc(struct_size(blk_key, devs, num_devs), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	if (!blk_key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	blk_key->num_devs = num_devs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	fscrypt_get_devices(sb, num_devs, blk_key->devs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	BUILD_BUG_ON(FSCRYPT_MAX_HW_WRAPPED_KEY_SIZE >
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 		     BLK_CRYPTO_MAX_WRAPPED_KEY_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	err = blk_crypto_init_key(&blk_key->base, raw_key, raw_key_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 				  is_hw_wrapped, crypto_mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 				  fscrypt_get_dun_bytes(ci), sb->s_blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		fscrypt_err(inode, "error %d initializing blk-crypto key", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	 * We have to start using blk-crypto on all the filesystem's devices.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	 * We also have to save all the request_queue's for later so that the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	 * key can be evicted from them.  This is needed because some keys
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	 * aren't destroyed until after the filesystem was already unmounted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	 * (namely, the per-mode keys in struct fscrypt_master_key).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	for (i = 0; i < num_devs; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 		if (!blk_get_queue(blk_key->devs[i])) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 			fscrypt_err(inode, "couldn't get request_queue");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 			err = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 			goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 		queue_refs++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 		err = blk_crypto_start_using_key(&blk_key->base,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 						 blk_key->devs[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 		if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 			fscrypt_err(inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 				    "error %d starting to use blk-crypto", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 			goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	 * Pairs with the smp_load_acquire() in fscrypt_is_key_prepared().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	 * I.e., here we publish ->blk_key with a RELEASE barrier so that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	 * concurrent tasks can ACQUIRE it.  Note that this concurrency is only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	 * possible for per-mode keys, not for per-file keys.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	smp_store_release(&prep_key->blk_key, blk_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	for (i = 0; i < queue_refs; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 		blk_put_queue(blk_key->devs[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	kfree_sensitive(blk_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) void fscrypt_destroy_inline_crypt_key(struct fscrypt_prepared_key *prep_key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	struct fscrypt_blk_crypto_key *blk_key = prep_key->blk_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	if (blk_key) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 		for (i = 0; i < blk_key->num_devs; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 			blk_crypto_evict_key(blk_key->devs[i], &blk_key->base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 			blk_put_queue(blk_key->devs[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 		kfree_sensitive(blk_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) int fscrypt_derive_raw_secret(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 			      const u8 *wrapped_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 			      unsigned int wrapped_key_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 			      u8 *raw_secret, unsigned int raw_secret_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	struct request_queue *q;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	q = bdev_get_queue(sb->s_bdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	if (!q->ksm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 		return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	return blk_ksm_derive_raw_secret(q->ksm, wrapped_key, wrapped_key_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 					 raw_secret, raw_secret_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) bool __fscrypt_inode_uses_inline_crypto(const struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	return inode->i_crypt_info->ci_inlinecrypt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) EXPORT_SYMBOL_GPL(__fscrypt_inode_uses_inline_crypto);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) static void fscrypt_generate_dun(const struct fscrypt_info *ci, u64 lblk_num,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 				 u64 dun[BLK_CRYPTO_DUN_ARRAY_SIZE])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	union fscrypt_iv iv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	fscrypt_generate_iv(&iv, lblk_num, ci);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	BUILD_BUG_ON(FSCRYPT_MAX_IV_SIZE > BLK_CRYPTO_MAX_IV_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	memset(dun, 0, BLK_CRYPTO_MAX_IV_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	for (i = 0; i < ci->ci_mode->ivsize/sizeof(dun[0]); i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 		dun[i] = le64_to_cpu(iv.dun[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)  * fscrypt_set_bio_crypt_ctx() - prepare a file contents bio for inline crypto
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253)  * @bio: a bio which will eventually be submitted to the file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)  * @inode: the file's inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)  * @first_lblk: the first file logical block number in the I/O
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)  * @gfp_mask: memory allocation flags - these must be a waiting mask so that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257)  *					bio_crypt_set_ctx can't fail.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259)  * If the contents of the file should be encrypted (or decrypted) with inline
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)  * encryption, then assign the appropriate encryption context to the bio.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)  * Normally the bio should be newly allocated (i.e. no pages added yet), as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263)  * otherwise fscrypt_mergeable_bio() won't work as intended.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)  * The encryption context will be freed automatically when the bio is freed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)  * This function also handles setting bi_skip_dm_default_key when needed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) void fscrypt_set_bio_crypt_ctx(struct bio *bio, const struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 			       u64 first_lblk, gfp_t gfp_mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	const struct fscrypt_info *ci;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	u64 dun[BLK_CRYPTO_DUN_ARRAY_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	if (fscrypt_inode_should_skip_dm_default_key(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 		bio_set_skip_dm_default_key(bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	if (!fscrypt_inode_uses_inline_crypto(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	ci = inode->i_crypt_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	fscrypt_generate_dun(ci, first_lblk, dun);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	bio_crypt_set_ctx(bio, &ci->ci_enc_key.blk_key->base, dun, gfp_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) EXPORT_SYMBOL_GPL(fscrypt_set_bio_crypt_ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) /* Extract the inode and logical block number from a buffer_head. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) static bool bh_get_inode_and_lblk_num(const struct buffer_head *bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 				      const struct inode **inode_ret,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 				      u64 *lblk_num_ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	struct page *page = bh->b_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	const struct address_space *mapping;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	const struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	 * The ext4 journal (jbd2) can submit a buffer_head it directly created
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	 * for a non-pagecache page.  fscrypt doesn't care about these.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	mapping = page_mapping(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	if (!mapping)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	inode = mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	*inode_ret = inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	*lblk_num_ret = ((u64)page->index << (PAGE_SHIFT - inode->i_blkbits)) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 			(bh_offset(bh) >> inode->i_blkbits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312)  * fscrypt_set_bio_crypt_ctx_bh() - prepare a file contents bio for inline
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313)  *				    crypto
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314)  * @bio: a bio which will eventually be submitted to the file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315)  * @first_bh: the first buffer_head for which I/O will be submitted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316)  * @gfp_mask: memory allocation flags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318)  * Same as fscrypt_set_bio_crypt_ctx(), except this takes a buffer_head instead
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319)  * of an inode and block number directly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) void fscrypt_set_bio_crypt_ctx_bh(struct bio *bio,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 				  const struct buffer_head *first_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 				  gfp_t gfp_mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	const struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	u64 first_lblk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	if (bh_get_inode_and_lblk_num(first_bh, &inode, &first_lblk))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 		fscrypt_set_bio_crypt_ctx(bio, inode, first_lblk, gfp_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) EXPORT_SYMBOL_GPL(fscrypt_set_bio_crypt_ctx_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334)  * fscrypt_mergeable_bio() - test whether data can be added to a bio
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335)  * @bio: the bio being built up
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336)  * @inode: the inode for the next part of the I/O
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337)  * @next_lblk: the next file logical block number in the I/O
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339)  * When building a bio which may contain data which should undergo inline
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340)  * encryption (or decryption) via fscrypt, filesystems should call this function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341)  * to ensure that the resulting bio contains only contiguous data unit numbers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342)  * This will return false if the next part of the I/O cannot be merged with the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343)  * bio because either the encryption key would be different or the encryption
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344)  * data unit numbers would be discontiguous.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346)  * fscrypt_set_bio_crypt_ctx() must have already been called on the bio.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348)  * This function also returns false if the next part of the I/O would need to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349)  * have a different value for the bi_skip_dm_default_key flag.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351)  * Return: true iff the I/O is mergeable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) bool fscrypt_mergeable_bio(struct bio *bio, const struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 			   u64 next_lblk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	const struct bio_crypt_ctx *bc = bio->bi_crypt_context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	u64 next_dun[BLK_CRYPTO_DUN_ARRAY_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	if (!!bc != fscrypt_inode_uses_inline_crypto(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	if (bio_should_skip_dm_default_key(bio) !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	    fscrypt_inode_should_skip_dm_default_key(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	if (!bc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	 * Comparing the key pointers is good enough, as all I/O for each key
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	 * uses the same pointer.  I.e., there's currently no need to support
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	 * merging requests where the keys are the same but the pointers differ.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	if (bc->bc_key != &inode->i_crypt_info->ci_enc_key.blk_key->base)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	fscrypt_generate_dun(inode->i_crypt_info, next_lblk, next_dun);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	return bio_crypt_dun_is_contiguous(bc, bio->bi_iter.bi_size, next_dun);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) EXPORT_SYMBOL_GPL(fscrypt_mergeable_bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381)  * fscrypt_mergeable_bio_bh() - test whether data can be added to a bio
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382)  * @bio: the bio being built up
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383)  * @next_bh: the next buffer_head for which I/O will be submitted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385)  * Same as fscrypt_mergeable_bio(), except this takes a buffer_head instead of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386)  * an inode and block number directly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388)  * Return: true iff the I/O is mergeable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) bool fscrypt_mergeable_bio_bh(struct bio *bio,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 			      const struct buffer_head *next_bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	const struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 	u64 next_lblk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 	if (!bh_get_inode_and_lblk_num(next_bh, &inode, &next_lblk))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 		return !bio->bi_crypt_context &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 		       !bio_should_skip_dm_default_key(bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 	return fscrypt_mergeable_bio(bio, inode, next_lblk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) EXPORT_SYMBOL_GPL(fscrypt_mergeable_bio_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405)  * fscrypt_dio_supported() - check whether a direct I/O request is unsupported
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406)  *			     due to encryption constraints
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407)  * @iocb: the file and position the I/O is targeting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408)  * @iter: the I/O data segment(s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410)  * Return: true if direct I/O is supported
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) bool fscrypt_dio_supported(struct kiocb *iocb, struct iov_iter *iter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 	const struct inode *inode = file_inode(iocb->ki_filp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 	const unsigned int blocksize = i_blocksize(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	/* If the file is unencrypted, no veto from us. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	if (!fscrypt_needs_contents_encryption(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 	/* We only support direct I/O with inline crypto, not fs-layer crypto */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 	if (!fscrypt_inode_uses_inline_crypto(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 	 * Since the granularity of encryption is filesystem blocks, the I/O
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	 * must be block aligned -- not just disk sector aligned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	if (!IS_ALIGNED(iocb->ki_pos | iov_iter_alignment(iter), blocksize))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) EXPORT_SYMBOL_GPL(fscrypt_dio_supported);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437)  * fscrypt_limit_io_blocks() - limit I/O blocks to avoid discontiguous DUNs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438)  * @inode: the file on which I/O is being done
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439)  * @lblk: the block at which the I/O is being started from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440)  * @nr_blocks: the number of blocks we want to submit starting at @pos
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442)  * Determine the limit to the number of blocks that can be submitted in the bio
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443)  * targeting @pos without causing a data unit number (DUN) discontinuity.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445)  * This is normally just @nr_blocks, as normally the DUNs just increment along
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446)  * with the logical blocks.  (Or the file is not encrypted.)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448)  * In rare cases, fscrypt can be using an IV generation method that allows the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449)  * DUN to wrap around within logically continuous blocks, and that wraparound
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450)  * will occur.  If this happens, a value less than @nr_blocks will be returned
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451)  * so that the wraparound doesn't occur in the middle of the bio.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453)  * Return: the actual number of blocks that can be submitted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) u64 fscrypt_limit_io_blocks(const struct inode *inode, u64 lblk, u64 nr_blocks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 	const struct fscrypt_info *ci = inode->i_crypt_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 	u32 dun;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 	if (!fscrypt_inode_uses_inline_crypto(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 		return nr_blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 	if (nr_blocks <= 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 		return nr_blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 	if (!(fscrypt_policy_flags(&ci->ci_policy) &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 	      FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 		return nr_blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 	/* With IV_INO_LBLK_32, the DUN can wrap around from U32_MAX to 0. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 	dun = ci->ci_hashed_ino + lblk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 	return min_t(u64, nr_blocks, (u64)U32_MAX + 1 - dun);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) EXPORT_SYMBOL_GPL(fscrypt_limit_io_blocks);