* [PATCH v3 0/3] f2fs: support encrypted inline data
@ 2026-06-15 12:55 LiaoYuanhong-vivo
2026-06-15 12:55 ` [PATCH v3 1/3] fscrypt: prepare software keys for filesystem-managed data units LiaoYuanhong-vivo
2026-06-15 19:37 ` [PATCH v3 0/3] f2fs: support encrypted inline data Eric Biggers
0 siblings, 2 replies; 3+ messages in thread
From: LiaoYuanhong-vivo @ 2026-06-15 12:55 UTC (permalink / raw)
To: Jaegeuk Kim, Chao Yu, Jonathan Corbet, Shuah Khan, Eric Biggers,
Theodore Y. Ts'o, open list:F2FS FILE SYSTEM, open list,
open list:DOCUMENTATION,
open list:FSCRYPT: FILE SYSTEM LEVEL ENCRYPTION SUPPORT
Cc: LiaoYuanhong-vivo
F2FS currently disables inline data for encrypted regular files because the
inline payload is stored in the inode block and does not go through the
regular bio-based fscrypt path. This wastes space for small encrypted
files on Android devices using F2FS inlinecrypt.
This series adds an encrypted_inline_data on-disk feature for F2FS.
With this feature enabled, encrypted regular files may keep small contents
in the inode block. The inline payload is encrypted before being stored in
the inode and decrypted back into page-cache plaintext on read.
The fscrypt changes are scoped to filesystem-managed data-unit crypto.
F2FS first asks fscrypt whether the inode's key/policy supports this path.
It prepares the software transform only when encrypted inline payloads are
read or written. Inlinecrypt support is limited to v2 IV_INO_LBLK_64 and
IV_INO_LBLK_32 policies, including the hardware-wrapped key configurations
supported by fscrypt. Per-file inlinecrypt keys and DIRECT_KEY policies
are not supported for encrypted inline data.
The basic encrypted inline-data tests pass. The test creates encrypted
small files and verifies that they retain inline data. It also checks
normal read/write correctness and confirms from the raw inode block that
the inline payload does not contain plaintext.
Changes in v3:
- Support fscrypt's v2 IV_INO_LBLK_64/32 hardware-wrapped key
configurations.
- Drop DIRECT_KEY support for encrypted inline data.
- Refresh comments and documentation for the updated key support matrix.
LiaoYuanhong-vivo (3):
fscrypt: prepare software keys for filesystem-managed data units
f2fs: support encrypted inline data
Documentation: f2fs: document encrypted inline data
Documentation/ABI/testing/sysfs-fs-f2fs | 5 +-
Documentation/filesystems/f2fs.rst | 30 ++++
fs/crypto/crypto.c | 47 +++++++
fs/crypto/fscrypt_private.h | 3 +-
fs/crypto/keysetup.c | 174 ++++++++++++++++++++++++
fs/f2fs/Kconfig | 14 ++
fs/f2fs/data.c | 8 +-
fs/f2fs/f2fs.h | 37 ++++-
fs/f2fs/file.c | 24 +++-
fs/f2fs/inline.c | 134 ++++++++++++++++--
fs/f2fs/super.c | 12 ++
fs/f2fs/sysfs.c | 8 ++
include/linux/fscrypt.h | 24 ++++
13 files changed, 497 insertions(+), 23 deletions(-)
--
2.34.1
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH v3 1/3] fscrypt: prepare software keys for filesystem-managed data units
2026-06-15 12:55 [PATCH v3 0/3] f2fs: support encrypted inline data LiaoYuanhong-vivo
@ 2026-06-15 12:55 ` LiaoYuanhong-vivo
2026-06-15 19:37 ` [PATCH v3 0/3] f2fs: support encrypted inline data Eric Biggers
1 sibling, 0 replies; 3+ messages in thread
From: LiaoYuanhong-vivo @ 2026-06-15 12:55 UTC (permalink / raw)
To: Eric Biggers, Theodore Y. Ts'o, Jaegeuk Kim,
open list:FSCRYPT: FILE SYSTEM LEVEL ENCRYPTION SUPPORT,
open list
Cc: LiaoYuanhong-vivo
Some filesystems store small data regions inside filesystem metadata rather
than submitting them through the normal bio path. F2FS inline data is one
such case. When encrypted file contents normally use blk-crypto, these
filesystem-managed regions still need software fscrypt handling because no
data bio is submitted for them.
Add helpers for this case. fscrypt_supports_data_unit_inplace() lets a
filesystem check whether an inode's current key and policy can support this
path without preparing a software transform.
fscrypt_prepare_data_unit_inplace() prepares the transform when the
filesystem actually needs to read or write such a region.
fscrypt_crypt_data_unit_inplace() encrypts or decrypts an in-page region by
fscrypt data-unit size.
Inlinecrypt support is limited to v2 IV_INO_LBLK_64 and IV_INO_LBLK_32
policies, including hardware-wrapped key configurations supported by
fscrypt. Per-file inlinecrypt keys and DIRECT_KEY policies are not
supported for this path.
Signed-off-by: LiaoYuanhong-vivo <liaoyuanhong@vivo.com>
---
Changes in v3:
- Support fscrypt's v2 IV_INO_LBLK_64/32 hardware-wrapped key
configurations.
- Drop DIRECT_KEY support.
fs/crypto/crypto.c | 47 ++++++++++
fs/crypto/fscrypt_private.h | 3 +-
fs/crypto/keysetup.c | 174 ++++++++++++++++++++++++++++++++++++
include/linux/fscrypt.h | 24 +++++
4 files changed, 247 insertions(+), 1 deletion(-)
diff --git a/fs/crypto/crypto.c b/fs/crypto/crypto.c
index 570a2231c945..c4f3ad8f82c9 100644
--- a/fs/crypto/crypto.c
+++ b/fs/crypto/crypto.c
@@ -208,6 +208,53 @@ struct page *fscrypt_encrypt_pagecache_blocks(struct folio *folio,
}
EXPORT_SYMBOL(fscrypt_encrypt_pagecache_blocks);
+/**
+ * fscrypt_crypt_data_unit_inplace() - En/decrypt data units in-place
+ * @inode: The inode to which these data units belong
+ * @page: The page containing the data units to en/decrypt
+ * @len: Size of the region to en/decrypt. This must be a multiple of
+ * FSCRYPT_CONTENTS_ALIGNMENT.
+ * @offs: Byte offset within @page at which the region begins
+ * @index: Fscrypt data unit index of the start of the region
+ * @encrypt: True to encrypt, false to decrypt
+ *
+ * Return: 0 on success; -errno on failure
+ */
+int fscrypt_crypt_data_unit_inplace(const struct inode *inode,
+ struct page *page, unsigned int len,
+ unsigned int offs, u64 index, bool encrypt)
+{
+ const struct fscrypt_inode_info *ci = fscrypt_get_inode_info_raw(inode);
+ unsigned int du_size;
+ unsigned int i;
+ int err;
+
+ /*
+ * Pairs with the smp_store_release() that publishes ->tfm after the
+ * software transform has been fully initialized.
+ */
+ if (!ci || !smp_load_acquire(&ci->ci_enc_key.tfm))
+ return -EOPNOTSUPP;
+ du_size = 1U << ci->ci_data_unit_bits;
+
+ if (WARN_ON_ONCE(len <= 0))
+ return -EINVAL;
+ if (WARN_ON_ONCE(!IS_ALIGNED(len | offs, FSCRYPT_CONTENTS_ALIGNMENT)))
+ return -EINVAL;
+
+ for (i = 0; i < len; i += du_size, index++) {
+ unsigned int todo = min(du_size, len - i);
+
+ err = fscrypt_crypt_data_unit(ci,
+ encrypt ? FS_ENCRYPT : FS_DECRYPT,
+ index, page, page, todo, offs + i);
+ if (err)
+ return err;
+ }
+ return 0;
+}
+EXPORT_SYMBOL(fscrypt_crypt_data_unit_inplace);
+
/**
* fscrypt_encrypt_block_inplace() - Encrypt a filesystem block in-place
* @inode: The inode to which this block belongs
diff --git a/fs/crypto/fscrypt_private.h b/fs/crypto/fscrypt_private.h
index 8d3c278a7591..b5c0b881fd4b 100644
--- a/fs/crypto/fscrypt_private.h
+++ b/fs/crypto/fscrypt_private.h
@@ -236,7 +236,8 @@ struct fscrypt_symlink_data {
* @tfm: crypto API transform object
* @blk_key: key for blk-crypto
*
- * Normally only one of the fields will be non-NULL.
+ * Most users need only one prepared form. Inline-crypto users that also need
+ * filesystem-layer software crypto for non-bio data regions may prepare both.
*/
struct fscrypt_prepared_key {
struct crypto_sync_skcipher *tfm;
diff --git a/fs/crypto/keysetup.c b/fs/crypto/keysetup.c
index ce327bfdada4..399b442578cc 100644
--- a/fs/crypto/keysetup.c
+++ b/fs/crypto/keysetup.c
@@ -400,6 +400,180 @@ static int fscrypt_setup_v2_file_key(struct fscrypt_inode_info *ci,
return 0;
}
+static int fscrypt_prepare_data_unit_inplace_software_key(
+ struct fscrypt_prepared_key *prep_key,
+ const u8 *raw_key,
+ const struct fscrypt_inode_info *ci)
+{
+ struct crypto_sync_skcipher *tfm;
+
+ /* Pairs with the smp_store_release() below. */
+ if (smp_load_acquire(&prep_key->tfm))
+ return 0;
+ tfm = fscrypt_allocate_skcipher(ci->ci_mode, raw_key, ci->ci_inode);
+ if (IS_ERR(tfm))
+ return PTR_ERR(tfm);
+ /* Pairs with the smp_load_acquire() above and other ->tfm readers. */
+ smp_store_release(&prep_key->tfm, tfm);
+ return 0;
+}
+
+static int fscrypt_prepare_per_mode_data_unit_inplace_key(
+ struct fscrypt_inode_info *ci,
+ struct fscrypt_master_key *mk,
+ struct fscrypt_prepared_key *keys,
+ u8 hkdf_context, bool include_fs_uuid)
+{
+ const struct super_block *sb = ci->ci_inode->i_sb;
+ struct fscrypt_mode *mode = ci->ci_mode;
+ const u8 mode_num = mode - fscrypt_modes;
+ struct fscrypt_prepared_key *prep_key;
+ u8 mode_key[FSCRYPT_MAX_RAW_KEY_SIZE];
+ u8 hkdf_info[sizeof(mode_num) + sizeof(sb->s_uuid)];
+ unsigned int hkdf_infolen = 0;
+ int err;
+
+ if (WARN_ON_ONCE(mode_num > FSCRYPT_MODE_MAX))
+ return -EINVAL;
+
+ prep_key = &keys[mode_num];
+
+ BUILD_BUG_ON(sizeof(mode_num) != 1);
+ BUILD_BUG_ON(sizeof(sb->s_uuid) != 16);
+ BUILD_BUG_ON(sizeof(hkdf_info) != 17);
+ hkdf_info[hkdf_infolen++] = mode_num;
+ if (include_fs_uuid) {
+ memcpy(&hkdf_info[hkdf_infolen], &sb->s_uuid,
+ sizeof(sb->s_uuid));
+ hkdf_infolen += sizeof(sb->s_uuid);
+ }
+
+ fscrypt_hkdf_expand(&mk->mk_secret.hkdf, hkdf_context, hkdf_info,
+ hkdf_infolen, mode_key, mode->keysize);
+ err = fscrypt_prepare_data_unit_inplace_software_key(prep_key,
+ mode_key, ci);
+ memzero_explicit(mode_key, mode->keysize);
+ if (!err)
+ ci->ci_enc_key = *prep_key;
+ return err;
+}
+
+/**
+ * fscrypt_supports_data_unit_inplace() - check data-unit crypto support
+ * @inode: an encrypted regular file inode
+ *
+ * Check whether filesystem-managed data regions can use fscrypt contents
+ * encryption for this inode. This path is limited to v2 IV_INO_LBLK
+ * policies, including hardware-wrapped key configurations supported by
+ * fscrypt. Per-file inlinecrypt keys and DIRECT_KEY policies are
+ * unsupported.
+ */
+bool fscrypt_supports_data_unit_inplace(const struct inode *inode)
+{
+ struct fscrypt_inode_info *ci = fscrypt_get_inode_info_raw(inode);
+ struct fscrypt_master_key *mk;
+ u8 flags;
+ bool supported;
+
+ if (!ci)
+ return false;
+ /*
+ * Pairs with the smp_store_release() that publishes ->tfm after the
+ * software transform has been fully initialized.
+ */
+ if (smp_load_acquire(&ci->ci_enc_key.tfm))
+ return true;
+ if (!fscrypt_using_inline_encryption(ci))
+ return false;
+
+ mk = ci->ci_master_key;
+ if (!mk)
+ return false;
+
+ down_read(&mk->mk_sem);
+ if (!mk->mk_present ||
+ ci->ci_policy.version != FSCRYPT_POLICY_V2) {
+ supported = false;
+ goto out;
+ }
+
+ flags = ci->ci_policy.v2.flags;
+ supported = flags & (FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64 |
+ FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32);
+out:
+ up_read(&mk->mk_sem);
+ return supported;
+}
+EXPORT_SYMBOL_GPL(fscrypt_supports_data_unit_inplace);
+
+/**
+ * fscrypt_prepare_data_unit_inplace() - prepare software data-unit crypto
+ * @inode: an encrypted regular file inode
+ *
+ * Prepare the software transform used by filesystem-managed data regions that
+ * need fscrypt contents encryption but do not go through a data bio. If the
+ * inode already uses filesystem-layer encryption, the normal contents key is
+ * already prepared. If the inode uses blk-crypto with a v2 IV_INO_LBLK
+ * policy, this prepares the corresponding software transform for the
+ * filesystem-managed data region.
+ */
+int fscrypt_prepare_data_unit_inplace(const struct inode *inode)
+{
+ struct fscrypt_inode_info *ci = fscrypt_get_inode_info_raw(inode);
+ struct fscrypt_master_key *mk;
+ u8 flags;
+ int err = 0;
+
+ if (!ci)
+ return -ENOKEY;
+ /*
+ * Pairs with the smp_store_release() that publishes ->tfm after the
+ * software transform has been fully initialized.
+ */
+ if (smp_load_acquire(&ci->ci_enc_key.tfm))
+ return 0;
+ if (!fscrypt_using_inline_encryption(ci))
+ return -EOPNOTSUPP;
+
+ mk = ci->ci_master_key;
+ if (!mk)
+ return -EOPNOTSUPP;
+
+ down_read(&mk->mk_sem);
+ if (!mk->mk_present) {
+ err = -ENOKEY;
+ goto out_unlock;
+ }
+ if (ci->ci_policy.version != FSCRYPT_POLICY_V2) {
+ err = -EOPNOTSUPP;
+ goto out_unlock;
+ }
+
+ mutex_lock(&fscrypt_mode_key_setup_mutex);
+ /* Pairs with fscrypt_prepare_data_unit_inplace_software_key(). */
+ if (smp_load_acquire(&ci->ci_enc_key.tfm))
+ goto out_mutex;
+
+ flags = ci->ci_policy.v2.flags;
+ if (flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64) {
+ err = fscrypt_prepare_per_mode_data_unit_inplace_key(ci, mk,
+ mk->mk_iv_ino_lblk_64_keys,
+ HKDF_CONTEXT_IV_INO_LBLK_64_KEY, true);
+ } else if (flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32) {
+ err = fscrypt_prepare_per_mode_data_unit_inplace_key(ci, mk,
+ mk->mk_iv_ino_lblk_32_keys,
+ HKDF_CONTEXT_IV_INO_LBLK_32_KEY, true);
+ } else {
+ err = -EOPNOTSUPP;
+ }
+out_mutex:
+ mutex_unlock(&fscrypt_mode_key_setup_mutex);
+out_unlock:
+ up_read(&mk->mk_sem);
+ return err;
+}
+EXPORT_SYMBOL_GPL(fscrypt_prepare_data_unit_inplace);
+
/*
* Check whether the size of the given master key (@mk) is appropriate for the
* encryption settings which a particular file will use (@ci).
diff --git a/include/linux/fscrypt.h b/include/linux/fscrypt.h
index 54712ec61ffb..2702bf1018c1 100644
--- a/include/linux/fscrypt.h
+++ b/include/linux/fscrypt.h
@@ -346,6 +346,12 @@ void fscrypt_enqueue_decrypt_work(struct work_struct *);
struct page *fscrypt_encrypt_pagecache_blocks(struct folio *folio,
size_t len, size_t offs, gfp_t gfp_flags);
+
+bool fscrypt_supports_data_unit_inplace(const struct inode *inode);
+int fscrypt_prepare_data_unit_inplace(const struct inode *inode);
+int fscrypt_crypt_data_unit_inplace(const struct inode *inode,
+ struct page *page, unsigned int len,
+ unsigned int offs, u64 index, bool encrypt);
int fscrypt_encrypt_block_inplace(const struct inode *inode, struct page *page,
unsigned int len, unsigned int offs,
u64 lblk_num);
@@ -519,6 +525,24 @@ static inline struct page *fscrypt_encrypt_pagecache_blocks(struct folio *folio,
return ERR_PTR(-EOPNOTSUPP);
}
+static inline bool fscrypt_supports_data_unit_inplace(const struct inode *inode)
+{
+ return false;
+}
+
+static inline int fscrypt_prepare_data_unit_inplace(const struct inode *inode)
+{
+ return -EOPNOTSUPP;
+}
+
+static inline int fscrypt_crypt_data_unit_inplace(const struct inode *inode,
+ struct page *page, unsigned int len,
+ unsigned int offs, u64 index,
+ bool encrypt)
+{
+ return -EOPNOTSUPP;
+}
+
static inline int fscrypt_encrypt_block_inplace(const struct inode *inode,
struct page *page,
unsigned int len,
--
2.34.1
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH v3 0/3] f2fs: support encrypted inline data
2026-06-15 12:55 [PATCH v3 0/3] f2fs: support encrypted inline data LiaoYuanhong-vivo
2026-06-15 12:55 ` [PATCH v3 1/3] fscrypt: prepare software keys for filesystem-managed data units LiaoYuanhong-vivo
@ 2026-06-15 19:37 ` Eric Biggers
1 sibling, 0 replies; 3+ messages in thread
From: Eric Biggers @ 2026-06-15 19:37 UTC (permalink / raw)
To: LiaoYuanhong-vivo
Cc: Jaegeuk Kim, Chao Yu, Jonathan Corbet, Shuah Khan,
Theodore Y. Ts'o, open list:F2FS FILE SYSTEM, open list,
open list:DOCUMENTATION,
open list:FSCRYPT: FILE SYSTEM LEVEL ENCRYPTION SUPPORT,
linux-ext4
[+Cc linux-ext4@vger.kernel.org]
On Mon, Jun 15, 2026 at 08:55:12PM +0800, LiaoYuanhong-vivo wrote:
> F2FS currently disables inline data for encrypted regular files because the
> inline payload is stored in the inode block and does not go through the
> regular bio-based fscrypt path. This wastes space for small encrypted
> files on Android devices using F2FS inlinecrypt.
>
> This series adds an encrypted_inline_data on-disk feature for F2FS.
> With this feature enabled, encrypted regular files may keep small contents
> in the inode block. The inline payload is encrypted before being stored in
> the inode and decrypted back into page-cache plaintext on read.
>
> The fscrypt changes are scoped to filesystem-managed data-unit crypto.
> F2FS first asks fscrypt whether the inode's key/policy supports this path.
> It prepares the software transform only when encrypted inline payloads are
> read or written. Inlinecrypt support is limited to v2 IV_INO_LBLK_64 and
> IV_INO_LBLK_32 policies, including the hardware-wrapped key configurations
> supported by fscrypt. Per-file inlinecrypt keys and DIRECT_KEY policies
> are not supported for encrypted inline data.
I still think we should hold off on this, for the reasons I gave at
https://lore.kernel.org/r/20260515184124.GA4903@quark/
As soon as you start using hardware-wrapped keys this will become
irrelevant, as it can't be used in that case. I see you added "support"
for that case anyway by deriving contents encryption keys from the
sw_secret. But that bypasses the security model, which isn't okay.
I'm also working to simplify ext4 and f2fs's file contents encryption
implementation by standardizing on blk-crypto. That aligns well with
what btrfs encryption is going to do as well. So this isn't a great
time to be making f2fs's file contents encryption implementation even
more complex by going in a different direction.
If there was demand for this feature from the ext4 side for
general-purpose Linux distros as well, that would make it a bit more
appealing, as it would show broader demand. But with the proposal being
f2fs-specific and effectively just for Android devices that *don't* use
wrapped keys, that feels too narrow for the added complexity.
This proposal also lacks test cases in xfstests and/or Android's
vts_kernel_encryption_test that verify that the inline data is actually
being encrypted correctly. Those tests are essential, and we *must not*
add new file contents encryption implementations without such tests.
- Eric
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-06-15 19:37 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-06-15 12:55 [PATCH v3 0/3] f2fs: support encrypted inline data LiaoYuanhong-vivo
2026-06-15 12:55 ` [PATCH v3 1/3] fscrypt: prepare software keys for filesystem-managed data units LiaoYuanhong-vivo
2026-06-15 19:37 ` [PATCH v3 0/3] f2fs: support encrypted inline data Eric Biggers
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox