Skip to content

Commit 2216660

Browse files
josefbacikkdave
authored andcommitted
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>
1 parent 004151b commit 2216660

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

fs/btrfs/bio.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ static struct btrfs_bio *btrfs_split_bio(struct btrfs_fs_info *fs_info,
9494
if (bbio_has_ordered_extent(bbio)) {
9595
refcount_inc(&orig_bbio->ordered->refs);
9696
bbio->ordered = orig_bbio->ordered;
97+
bbio->orig_logical = orig_bbio->orig_logical;
98+
orig_bbio->orig_logical += map_length;
9799
}
98100
bbio->csum_search_commit_root = orig_bbio->csum_search_commit_root;
99101
atomic_inc(&orig_bbio->pending_ios);
@@ -765,6 +767,14 @@ static bool btrfs_submit_chunk(struct btrfs_bio *bbio, int mirror_num)
765767
goto end_bbio;
766768
}
767769

770+
/*
771+
* For fscrypt writes we will get the encrypted bio after we've
772+
* remapped our bio to the physical disk location, so we need to
773+
* save the original bytenr so we know what we're checksumming.
774+
*/
775+
if (bio_op(bio) == REQ_OP_WRITE && is_data_bbio(bbio))
776+
bbio->orig_logical = logical;
777+
768778
map_length = min(map_length, length);
769779
if (use_append)
770780
map_length = btrfs_append_map_length(bbio, map_length);

fs/btrfs/bio.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ struct btrfs_bio {
5252
* - pointer to the checksums for this bio
5353
* - original physical address from the allocator
5454
* (for zone append only)
55+
* - original logical address, used for checksumming fscrypt bios.
5556
*/
5657
struct {
5758
struct btrfs_ordered_extent *ordered;
@@ -60,6 +61,7 @@ struct btrfs_bio {
6061
struct completion csum_done;
6162
struct bvec_iter csum_saved_iter;
6263
u64 orig_physical;
64+
u64 orig_logical;
6365
};
6466

6567
/* For metadata reads: parentness verification. */

fs/btrfs/file-item.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -824,7 +824,7 @@ int btrfs_csum_one_bio(struct btrfs_bio *bbio, bool async)
824824
if (!sums)
825825
return -ENOMEM;
826826

827-
sums->logical = bio->bi_iter.bi_sector << SECTOR_SHIFT;
827+
sums->logical = bbio->orig_logical;
828828
sums->len = bio->bi_iter.bi_size;
829829
INIT_LIST_HEAD(&sums->list);
830830
bbio->sums = sums;

0 commit comments

Comments
 (0)