btrfs: add orig_logical to btrfs_bio for encryption

When checksumming the encrypted bio on writes we need to know which
logical address this checksum is for.  At the point where we get the
encrypted bio the bi_sector is the physical location on the target disk,
so we need to save the original logical offset in the btrfs_bio.  Then
we can use this when checksumming the bio instead of the
bio->iter.bi_sector.

Note: The patch was taken from v5 of fscrypt patchset
(https://lore.kernel.org/linux-btrfs/cover.1706116485.git.josef@toxicpanda.com/)
which was handled over time by various people: Omar Sandoval, Sweet Tea
Dorminy, Josef Bacik.

Signed-off-by: Josef Bacik <josef@toxicpanda.com>
Signed-off-by: Daniel Vacek <neelx@suse.com>
Reviewed-by: David Sterba <dsterba@suse.com>
[ add note ]
Signed-off-by: David Sterba <dsterba@suse.com>
pull/1354/merge
Josef Bacik 2025-11-18 17:08:40 +01:00 committed by David Sterba
parent 45d99129b6
commit bd45e9e3f6
3 changed files with 13 additions and 1 deletions

View File

@ -94,6 +94,8 @@ static struct btrfs_bio *btrfs_split_bio(struct btrfs_fs_info *fs_info,
if (bbio_has_ordered_extent(bbio)) { if (bbio_has_ordered_extent(bbio)) {
refcount_inc(&orig_bbio->ordered->refs); refcount_inc(&orig_bbio->ordered->refs);
bbio->ordered = orig_bbio->ordered; bbio->ordered = orig_bbio->ordered;
bbio->orig_logical = orig_bbio->orig_logical;
orig_bbio->orig_logical += map_length;
} }
bbio->csum_search_commit_root = orig_bbio->csum_search_commit_root; bbio->csum_search_commit_root = orig_bbio->csum_search_commit_root;
atomic_inc(&orig_bbio->pending_ios); atomic_inc(&orig_bbio->pending_ios);
@ -765,6 +767,14 @@ static bool btrfs_submit_chunk(struct btrfs_bio *bbio, int mirror_num)
goto end_bbio; goto end_bbio;
} }
/*
* For fscrypt writes we will get the encrypted bio after we've remapped
* our bio to the physical disk location, so we need to save the
* original bytenr so we know what we're checksumming.
*/
if (bio_op(bio) == REQ_OP_WRITE && is_data_bbio(bbio))
bbio->orig_logical = logical;
map_length = min(map_length, length); map_length = min(map_length, length);
if (use_append) if (use_append)
map_length = btrfs_append_map_length(bbio, map_length); map_length = btrfs_append_map_length(bbio, map_length);

View File

@ -52,6 +52,7 @@ struct btrfs_bio {
* - pointer to the checksums for this bio * - pointer to the checksums for this bio
* - original physical address from the allocator * - original physical address from the allocator
* (for zone append only) * (for zone append only)
* - original logical address, used for checksumming fscrypt bios
*/ */
struct { struct {
struct btrfs_ordered_extent *ordered; struct btrfs_ordered_extent *ordered;
@ -60,6 +61,7 @@ struct btrfs_bio {
struct completion csum_done; struct completion csum_done;
struct bvec_iter csum_saved_iter; struct bvec_iter csum_saved_iter;
u64 orig_physical; u64 orig_physical;
u64 orig_logical;
}; };
/* For metadata reads: parentness verification. */ /* For metadata reads: parentness verification. */

View File

@ -824,7 +824,7 @@ int btrfs_csum_one_bio(struct btrfs_bio *bbio, bool async)
if (!sums) if (!sums)
return -ENOMEM; return -ENOMEM;
sums->logical = bio->bi_iter.bi_sector << SECTOR_SHIFT; sums->logical = bbio->orig_logical;
sums->len = bio->bi_iter.bi_size; sums->len = bio->bi_iter.bi_size;
INIT_LIST_HEAD(&sums->list); INIT_LIST_HEAD(&sums->list);
bbio->sums = sums; bbio->sums = sums;