[PATCH v6 40/43] btrfs: support encryption with log replay

From: Daniel Vacek

Date: Fri Feb 06 2026 - 13:44:33 EST


From: Josef Bacik <josef@xxxxxxxxxxxxxx>

Log replay needs a few tweaks in order to make sure everything works
with encryption.

1. Copy in the fscrypt context if we find one in the log.
2. Set NEEDS_FULL_SYNC when we update the inode context so all of the
items are copied into the log at fsync time.

This makes replay of encrypted files work properly.

Signed-off-by: Josef Bacik <josef@xxxxxxxxxxxxxx>
Signed-off-by: Daniel Vacek <neelx@xxxxxxxx>
---

v5: https://lore.kernel.org/linux-btrfs/d1ada7ac632c2ab554a840c7ba29b53a93b9855f.1706116485.git.josef@xxxxxxxxxxxxxx/
* Adapted to the redesigned encryption context storing.
- No need to specially handle the extent item now.
It is not any different to before and the
new context items are simply copied.
---
fs/btrfs/fscrypt.c | 1 +
fs/btrfs/tree-log.c | 4 +++-
2 files changed, 4 insertions(+), 1 deletion(-)

diff --git a/fs/btrfs/fscrypt.c b/fs/btrfs/fscrypt.c
index bcb86cbaa171..cbdeaa75a868 100644
--- a/fs/btrfs/fscrypt.c
+++ b/fs/btrfs/fscrypt.c
@@ -145,6 +145,7 @@ static int btrfs_fscrypt_set_context(struct inode *inode, const void *ctx,
goto out_err;
}

+ set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
leaf = path->nodes[0];
ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);

diff --git a/fs/btrfs/tree-log.c b/fs/btrfs/tree-log.c
index 0d01e31a4592..fa75531f9633 100644
--- a/fs/btrfs/tree-log.c
+++ b/fs/btrfs/tree-log.c
@@ -2922,7 +2922,9 @@ static int replay_one_buffer(struct extent_buffer *eb,
continue;

/* these keys are simply copied */
- if (wc->log_key.type == BTRFS_XATTR_ITEM_KEY) {
+ if (wc->log_key.type == BTRFS_XATTR_ITEM_KEY ||
+ wc->log_key.type == BTRFS_FSCRYPT_INODE_CTX_KEY ||
+ wc->log_key.type == BTRFS_FSCRYPT_CTX_KEY) {
ret = overwrite_item(wc);
if (ret)
break;
--
2.51.0