From: Daniel Vacek <neelx@suse.com>
To: Chris Mason <clm@fb.com>, Josef Bacik <josef@toxicpanda.com>,
Eric Biggers <ebiggers@kernel.org>,
"Theodore Y. Ts'o" <tytso@mit.edu>,
Jaegeuk Kim <jaegeuk@kernel.org>, Jens Axboe <axboe@kernel.dk>,
David Sterba <dsterba@suse.com>
Cc: linux-block@vger.kernel.org, Daniel Vacek <neelx@suse.com>,
linux-fscrypt@vger.kernel.org, linux-btrfs@vger.kernel.org,
linux-kernel@vger.kernel.org
Subject: [PATCH v7 29/43] btrfs: set the bio fscrypt context when applicable
Date: Wed, 13 May 2026 10:53:03 +0200 [thread overview]
Message-ID: <20260513085340.3673127-30-neelx@suse.com> (raw)
In-Reply-To: <20260513085340.3673127-1-neelx@suse.com>
From: Josef Bacik <josef@toxicpanda.com>
Now that we have the fscrypt_info plumbed through everywhere, add the
code to setup the bio encryption context from the extent context.
We use the per-extent fscrypt_extent_info for encryption/decryption.
We use the offset into the extent as the lblk for fscrypt. So the start
of the extent has the lblk of 0, 4k into the extent has the lblk of 4k,
etc. This is done to allow things like relocation to continue to work
properly.
Signed-off-by: Josef Bacik <josef@toxicpanda.com>
Signed-off-by: Daniel Vacek <neelx@suse.com>
---
v7 changes:
* s/\<submit_bio(/blk_crypto_\0/ in btrfs_submit_dev_bio() due to commit
bb8e2019ad613 ("blk-crypto: handle the fallback above the block layer")
* Follow upstream fscrypt cleanup and use byte offsets instead of
logical block numbers in the new api. As a result remove the
btrfs_set_bio_crypt_ctx_from_extent() and btrfs_mergeable_encrypted_bio()
helpers and directly use fscrypt_set_bio_crypt_ctx_from_extent()
and fscrypt_mergeable_extent_bio().
v6 changes:
* The dio-related changes in inode.c were moved to direct-io.c
as upstream refactored in the meantime.
* Open-code orig_start to start - offset.
* Get the inode from bbio instead of from page mapping
as we no longer have the page in btrfs_bio_is_contig().
* Directly use the file_offset passed to btrfs_bio_is_contig(), no need
to compute it locally. We even no longer have the page and offset.
v5: https://lore.kernel.org/linux-btrfs/9ca883acc746b5aba980d1d19317168a7491aba6.1706116485.git.josef@toxicpanda.com/
---
fs/btrfs/bio.c | 2 +-
fs/btrfs/compression.c | 4 +++
fs/btrfs/direct-io.c | 10 ++++++
fs/btrfs/extent_io.c | 72 +++++++++++++++++++++++++++++++++++++++++-
4 files changed, 86 insertions(+), 2 deletions(-)
diff --git a/fs/btrfs/bio.c b/fs/btrfs/bio.c
index cc0bd03048ba..689cc41409e5 100644
--- a/fs/btrfs/bio.c
+++ b/fs/btrfs/bio.c
@@ -535,7 +535,7 @@ static void btrfs_submit_dev_bio(struct btrfs_device *dev, struct bio *bio)
if (bio->bi_opf & REQ_BTRFS_CGROUP_PUNT)
blkcg_punt_bio_submit(bio);
else
- submit_bio(bio);
+ blk_crypto_submit_bio(bio);
}
static void btrfs_submit_mirrored_bio(struct btrfs_io_context *bioc, int dev_nr)
diff --git a/fs/btrfs/compression.c b/fs/btrfs/compression.c
index b2393a48a8fe..e235a2ac5838 100644
--- a/fs/btrfs/compression.c
+++ b/fs/btrfs/compression.c
@@ -33,6 +33,7 @@
#include "subpage.h"
#include "messages.h"
#include "super.h"
+#include "fscrypt.h"
static struct bio_set btrfs_compressed_bioset;
@@ -333,6 +334,8 @@ void btrfs_submit_compressed_write(struct btrfs_ordered_extent *ordered,
cb->bbio.bio.bi_iter.bi_sector = ordered->disk_bytenr >> SECTOR_SHIFT;
cb->bbio.ordered = ordered;
+ fscrypt_set_bio_crypt_ctx_from_extent(&cb->bbio.bio, ordered->fscrypt_info, 0, GFP_NOFS);
+
btrfs_submit_bbio(&cb->bbio, 0);
}
@@ -590,6 +593,7 @@ void btrfs_submit_compressed_read(struct btrfs_bio *bbio)
cb->orig_bbio = bbio;
cb->bbio.csum_search_commit_root = bbio->csum_search_commit_root;
+ fscrypt_set_bio_crypt_ctx_from_extent(&cb->bbio.bio, em->fscrypt_info, 0, GFP_NOFS);
btrfs_free_extent_map(em);
for (int i = 0; i * min_folio_size < compressed_len; i++) {
diff --git a/fs/btrfs/direct-io.c b/fs/btrfs/direct-io.c
index e8b1ce4a3a11..4891bf233536 100644
--- a/fs/btrfs/direct-io.c
+++ b/fs/btrfs/direct-io.c
@@ -12,6 +12,7 @@
#include "volumes.h"
#include "bio.h"
#include "ordered-data.h"
+#include "fscrypt.h"
struct btrfs_dio_data {
ssize_t submitted;
@@ -727,6 +728,8 @@ static void btrfs_dio_submit_io(const struct iomap_iter *iter, struct bio *bio,
struct btrfs_dio_private *dip =
container_of(bbio, struct btrfs_dio_private, bbio);
struct btrfs_dio_data *dio_data = iter->private;
+ struct fscrypt_extent_info *fscrypt_info = NULL;
+ u64 offset = 0;
btrfs_bio_init(bbio, BTRFS_I(iter->inode), file_offset,
btrfs_dio_end_io, bio->bi_private);
@@ -746,6 +749,9 @@ static void btrfs_dio_submit_io(const struct iomap_iter *iter, struct bio *bio,
if (iter->flags & IOMAP_WRITE) {
int ret;
+ fscrypt_info = dio_data->ordered->fscrypt_info;
+ offset = file_offset - dio_data->ordered->orig_offset;
+
ret = btrfs_extract_ordered_extent(bbio, dio_data->ordered);
if (ret) {
btrfs_finish_ordered_extent(dio_data->ordered,
@@ -755,8 +761,12 @@ static void btrfs_dio_submit_io(const struct iomap_iter *iter, struct bio *bio,
iomap_dio_bio_end_io(bio);
return;
}
+ } else {
+ fscrypt_info = dio_data->fscrypt_info;
+ offset = file_offset - dio_data->orig_start;
}
+ fscrypt_set_bio_crypt_ctx_from_extent(&bbio->bio, fscrypt_info, offset, GFP_NOFS);
btrfs_submit_bbio(bbio, 0);
}
diff --git a/fs/btrfs/extent_io.c b/fs/btrfs/extent_io.c
index 2819989f895f..5e9cdcdc996a 100644
--- a/fs/btrfs/extent_io.c
+++ b/fs/btrfs/extent_io.c
@@ -35,6 +35,7 @@
#include "dev-replace.h"
#include "super.h"
#include "transaction.h"
+#include "fscrypt.h"
static struct kmem_cache *extent_buffer_cache;
@@ -150,6 +151,10 @@ struct btrfs_bio_ctrl {
* inside the same inode.
*/
u64 last_em_start;
+
+ /* This is set for reads and we have encryption. */
+ struct fscrypt_extent_info *fscrypt_info;
+ u64 orig_start;
};
/*
@@ -710,9 +715,28 @@ static int alloc_eb_folio_array(struct extent_buffer *eb, bool nofail)
static bool btrfs_bio_is_contig(struct btrfs_bio_ctrl *bio_ctrl,
u64 disk_bytenr, loff_t file_offset)
{
- struct bio *bio = &bio_ctrl->bbio->bio;
+ struct btrfs_bio *bbio = bio_ctrl->bbio;
+ struct bio *bio = &bbio->bio;
+ struct inode *inode = &bbio->inode->vfs_inode;
const sector_t sector = disk_bytenr >> SECTOR_SHIFT;
+ if (IS_ENCRYPTED(inode)) {
+ u64 offset = 0;
+ struct fscrypt_extent_info *fscrypt_info = NULL;
+
+ /* bio_ctrl->fscrypt_info is only set in the READ case. */
+ if (bio_ctrl->fscrypt_info) {
+ fscrypt_info = bio_ctrl->fscrypt_info;
+ offset = file_offset - bio_ctrl->orig_start;
+ } else if (bbio->ordered) {
+ fscrypt_info = bbio->ordered->fscrypt_info;
+ offset = file_offset - bbio->ordered->orig_offset;
+ }
+
+ if (!fscrypt_mergeable_extent_bio(bio, fscrypt_info, offset))
+ return false;
+ }
+
if (bio_ctrl->compress_type != BTRFS_COMPRESS_NONE) {
/*
* For compression, all IO should have its logical bytenr set
@@ -735,6 +759,8 @@ static void alloc_new_bio(struct btrfs_inode *inode,
{
struct btrfs_fs_info *fs_info = inode->root->fs_info;
struct btrfs_bio *bbio;
+ struct fscrypt_extent_info *fscrypt_info = NULL;
+ u64 offset = 0;
bbio = btrfs_bio_alloc(BIO_MAX_VECS, bio_ctrl->opf, inode,
file_offset, bio_ctrl->end_io_func, NULL);
@@ -754,6 +780,8 @@ static void alloc_new_bio(struct btrfs_inode *inode,
ordered->file_offset +
ordered->disk_num_bytes - file_offset);
bbio->ordered = ordered;
+ fscrypt_info = ordered->fscrypt_info;
+ offset = file_offset - ordered->orig_offset;
}
/*
@@ -764,7 +792,12 @@ static void alloc_new_bio(struct btrfs_inode *inode,
*/
bio_set_dev(&bbio->bio, fs_info->fs_devices->latest_dev->bdev);
wbc_init_bio(bio_ctrl->wbc, &bbio->bio);
+ } else {
+ fscrypt_info = bio_ctrl->fscrypt_info;
+ offset = file_offset - bio_ctrl->orig_start;
}
+
+ fscrypt_set_bio_crypt_ctx_from_extent(&bbio->bio, fscrypt_info, offset, GFP_NOFS);
}
/*
@@ -810,6 +843,19 @@ static void submit_extent_folio(struct btrfs_bio_ctrl *bio_ctrl,
len = bio_ctrl->len_to_oe_boundary;
}
+ /*
+ * Encryption has to allocate bounce buffers to encrypt the bio,
+ * and we need to make sure that it doesn't split the bio so we
+ * retain all of our special info in the btrfs_bio, so submit
+ * any bio that gets up to BIO_MAX_VECS worth of segments.
+ */
+ if (IS_ENCRYPTED(&inode->vfs_inode) &&
+ bio_data_dir(&bio_ctrl->bbio->bio) == WRITE &&
+ bio_segments(&bio_ctrl->bbio->bio) == BIO_MAX_VECS) {
+ submit_one_bio(bio_ctrl);
+ continue;
+ }
+
if (!bio_add_folio(&bio_ctrl->bbio->bio, folio, len, pg_offset)) {
/* bio full: move on to a new one */
submit_one_bio(bio_ctrl);
@@ -1031,6 +1077,8 @@ static int btrfs_do_readpage(struct folio *folio, struct extent_map **em_cached,
u64 block_start;
u64 em_gen;
+ bio_ctrl->fscrypt_info = NULL;
+
ASSERT(IS_ALIGNED(cur, fs_info->sectorsize));
if (cur >= last_byte) {
folio_zero_range(folio, pg_offset, end - cur + 1);
@@ -1120,6 +1168,21 @@ static int btrfs_do_readpage(struct folio *folio, struct extent_map **em_cached,
bio_ctrl->last_em_start = em->start;
+ /*
+ * We use the extent offset for the IV when decrypting the page,
+ * so we have to set the extent_offset based on the orig_start
+ * for this extent. Also save the fscrypt_info so the bio ctx
+ * can be set properly. If this inode isn't encrypted this
+ * won't do anything.
+ *
+ * If we're compressed we'll handle all of this in
+ * btrfs_submit_compressed_read.
+ */
+ if (compress_type == BTRFS_COMPRESS_NONE) {
+ bio_ctrl->orig_start = em->start - em->offset;
+ bio_ctrl->fscrypt_info = fscrypt_get_extent_info(em->fscrypt_info);
+ }
+
em_gen = em->generation;
btrfs_free_extent_map(em);
em = NULL;
@@ -1128,11 +1191,17 @@ static int btrfs_do_readpage(struct folio *folio, struct extent_map **em_cached,
if (block_start == EXTENT_MAP_HOLE) {
folio_zero_range(folio, pg_offset, blocksize);
end_folio_read(vi, folio, true, cur, blocksize);
+
+ /* This shouldn't be set, but clear it just in case. */
+ fscrypt_put_extent_info(bio_ctrl->fscrypt_info);
continue;
}
/* the get_extent function already copied into the folio */
if (block_start == EXTENT_MAP_INLINE) {
end_folio_read(vi, folio, true, cur, blocksize);
+
+ /* This shouldn't be set, but clear it just in case. */
+ fscrypt_put_extent_info(bio_ctrl->fscrypt_info);
continue;
}
@@ -1145,6 +1214,7 @@ static int btrfs_do_readpage(struct folio *folio, struct extent_map **em_cached,
submit_one_bio(bio_ctrl);
submit_extent_folio(bio_ctrl, disk_bytenr, folio, blocksize,
pg_offset, em_gen);
+ fscrypt_put_extent_info(bio_ctrl->fscrypt_info);
}
return 0;
}
--
2.53.0
next prev parent reply other threads:[~2026-05-13 8:55 UTC|newest]
Thread overview: 44+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-05-13 8:52 [PATCH v7 00/43] btrfs: add fscrypt support Daniel Vacek
2026-05-13 8:52 ` [PATCH v7 01/43] fscrypt: add per-extent encryption support Daniel Vacek
2026-05-13 8:52 ` [PATCH v7 02/43] fscrypt: allow inline encryption for extent based encryption Daniel Vacek
2026-05-13 8:52 ` [PATCH v7 03/43] fscrypt: add a __fscrypt_file_open helper Daniel Vacek
2026-05-13 8:52 ` [PATCH v7 04/43] fscrypt: conditionally don't wipe mk secret until the last active user is done Daniel Vacek
2026-05-13 8:52 ` [PATCH v7 05/43] blk-crypto: add a process bio callback Daniel Vacek
2026-05-13 8:52 ` [PATCH v7 06/43] fscrypt: add a process_bio hook to fscrypt_operations Daniel Vacek
2026-05-13 8:52 ` [PATCH v7 07/43] fscrypt: expose fscrypt_nokey_name Daniel Vacek
2026-05-13 8:52 ` [PATCH v7 08/43] fscrypt: add documentation about extent encryption Daniel Vacek
2026-05-13 8:52 ` [PATCH v7 09/43] btrfs: add infrastructure for safe em freeing Daniel Vacek
2026-05-13 8:52 ` [PATCH v7 10/43] btrfs: start using fscrypt hooks Daniel Vacek
2026-05-13 8:52 ` [PATCH v7 11/43] btrfs: add inode encryption contexts Daniel Vacek
2026-05-13 8:52 ` [PATCH v7 12/43] btrfs: add new FEATURE_INCOMPAT_ENCRYPT flag Daniel Vacek
2026-05-13 8:52 ` [PATCH v7 13/43] btrfs: adapt readdir for encrypted and nokey names Daniel Vacek
2026-05-13 8:52 ` [PATCH v7 14/43] btrfs: handle " Daniel Vacek
2026-05-13 8:52 ` [PATCH v7 15/43] btrfs: implement fscrypt ioctls Daniel Vacek
2026-05-13 8:52 ` [PATCH v7 16/43] btrfs: select encryption dependencies if FS_ENCRYPTION Daniel Vacek
2026-05-13 8:52 ` [PATCH v7 17/43] btrfs: add get_devices hook for fscrypt Daniel Vacek
2026-05-13 8:52 ` [PATCH v7 18/43] btrfs: set file extent encryption excplicitly Daniel Vacek
2026-05-13 8:52 ` [PATCH v7 19/43] btrfs: add fscrypt_info and encryption_type to extent_map Daniel Vacek
2026-05-13 8:52 ` [PATCH v7 20/43] btrfs: add fscrypt_info and encryption_type to ordered_extent Daniel Vacek
2026-05-13 8:52 ` [PATCH v7 21/43] btrfs: plumb through setting the fscrypt_info for ordered extents Daniel Vacek
2026-05-13 8:52 ` [PATCH v7 22/43] btrfs: populate the ordered_extent with the fscrypt context Daniel Vacek
2026-05-13 8:52 ` [PATCH v7 23/43] btrfs: keep track of fscrypt info and orig_start for dio reads Daniel Vacek
2026-05-13 8:52 ` [PATCH v7 24/43] btrfs: add extent encryption context tree item type Daniel Vacek
2026-05-13 8:52 ` [PATCH v7 25/43] btrfs: pass through fscrypt_extent_info to the file extent helpers Daniel Vacek
2026-05-13 8:53 ` [PATCH v7 26/43] btrfs: implement the fscrypt extent encryption hooks Daniel Vacek
2026-05-13 8:53 ` [PATCH v7 27/43] btrfs: setup fscrypt_extent_info for new extents Daniel Vacek
2026-05-13 8:53 ` [PATCH v7 28/43] btrfs: populate ordered_extent with the orig offset Daniel Vacek
2026-05-13 8:53 ` Daniel Vacek [this message]
2026-05-13 8:53 ` [PATCH v7 30/43] btrfs: add a bio argument to btrfs_csum_one_bio Daniel Vacek
2026-05-13 8:53 ` [PATCH v7 31/43] btrfs: limit encrypted writes to 256 segments Daniel Vacek
2026-05-13 8:53 ` [PATCH v7 32/43] btrfs: implement process_bio cb for fscrypt Daniel Vacek
2026-05-13 8:53 ` [PATCH v7 33/43] btrfs: implement read repair for encryption Daniel Vacek
2026-05-13 8:53 ` [PATCH v7 34/43] btrfs: add test_dummy_encryption support Daniel Vacek
2026-05-13 8:53 ` [PATCH v7 35/43] btrfs: make btrfs_ref_to_path handle encrypted filenames Daniel Vacek
2026-05-13 8:53 ` [PATCH v7 36/43] btrfs: deal with encrypted symlinks in send Daniel Vacek
2026-05-13 8:53 ` [PATCH v7 37/43] btrfs: decrypt file names for send Daniel Vacek
2026-05-13 8:53 ` [PATCH v7 38/43] btrfs: load the inode context before sending writes Daniel Vacek
2026-05-13 8:53 ` [PATCH v7 39/43] btrfs: set the appropriate free space settings in reconfigure Daniel Vacek
2026-05-13 8:53 ` [PATCH v7 40/43] btrfs: support encryption with log replay Daniel Vacek
2026-05-13 8:53 ` [PATCH v7 41/43] btrfs: disable auto defrag on encrypted files Daniel Vacek
2026-05-13 8:53 ` [PATCH v7 42/43] btrfs: disable encryption on RAID5/6 Daniel Vacek
2026-05-13 8:53 ` [PATCH v7 43/43] btrfs: disable send if we have encryption enabled Daniel Vacek
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260513085340.3673127-30-neelx@suse.com \
--to=neelx@suse.com \
--cc=axboe@kernel.dk \
--cc=clm@fb.com \
--cc=dsterba@suse.com \
--cc=ebiggers@kernel.org \
--cc=jaegeuk@kernel.org \
--cc=josef@toxicpanda.com \
--cc=linux-block@vger.kernel.org \
--cc=linux-btrfs@vger.kernel.org \
--cc=linux-fscrypt@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=tytso@mit.edu \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox