diff mbox series

[09/15] btrfs: factor out a btrfs_csum_ptr helper

Message ID 20220517145039.3202184-10-hch@lst.de (mailing list archive)
State New, archived
Headers show
Series [01/15] btrfs: introduce a pure data checksum checking helper | expand

Commit Message

Christoph Hellwig May 17, 2022, 2:50 p.m. UTC
Add a helper to find the csum for a byte offset into the csum buffer.

Signed-off-by: Christoph Hellwig <hch@lst.de>
---
 fs/btrfs/ctree.h |  7 +++++++
 fs/btrfs/inode.c | 13 +++----------
 2 files changed, 10 insertions(+), 10 deletions(-)

Comments

Johannes Thumshirn May 17, 2022, 3:24 p.m. UTC | #1
On 17/05/2022 16:52, Christoph Hellwig wrote:
> +static inline u8 *btrfs_csum_ptr(struct btrfs_fs_info *fs_info, u8 *csums,
> +		u64 offset)
> +{
> +	return csums +
> +		((offset >> fs_info->sectorsize_bits) * fs_info->csum_size);

I guess a local variable for holding 'offset >> fs_info->sectorsize_bits'
wouldn't have hurt readability and the compiler would've optimized it away,
but that's just me nitpicking.

Reviewed-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
Christoph Hellwig May 18, 2022, 8:45 a.m. UTC | #2
On Tue, May 17, 2022 at 03:24:26PM +0000, Johannes Thumshirn wrote:
> On 17/05/2022 16:52, Christoph Hellwig wrote:
> > +static inline u8 *btrfs_csum_ptr(struct btrfs_fs_info *fs_info, u8 *csums,
> > +		u64 offset)
> > +{
> > +	return csums +
> > +		((offset >> fs_info->sectorsize_bits) * fs_info->csum_size);
> 
> I guess a local variable for holding 'offset >> fs_info->sectorsize_bits'
> wouldn't have hurt readability and the compiler would've optimized it away,
> but that's just me nitpicking.

I can do that if there is a strong opinion, but I'm not sure it would
help readability at all.
diff mbox series

Patch

diff --git a/fs/btrfs/ctree.h b/fs/btrfs/ctree.h
index 4713d59ed1105..4ab41cb57f216 100644
--- a/fs/btrfs/ctree.h
+++ b/fs/btrfs/ctree.h
@@ -2733,6 +2733,13 @@  int btrfs_get_extent_inline_ref_type(const struct extent_buffer *eb,
 				     enum btrfs_inline_ref_type is_data);
 u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset);
 
+static inline u8 *btrfs_csum_ptr(struct btrfs_fs_info *fs_info, u8 *csums,
+		u64 offset)
+{
+	return csums +
+		((offset >> fs_info->sectorsize_bits) * fs_info->csum_size);
+}
+
 /*
  * Take the number of bytes to be checksummmed and figure out how many leaves
  * it would require to store the csums for that many bytes.
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index cb0ad1971be30..c771136116151 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -3369,15 +3369,12 @@  static int check_data_csum(struct inode *inode, struct btrfs_bio *bbio,
 {
 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
 	u32 len = fs_info->sectorsize;
-	const u32 csum_size = fs_info->csum_size;
-	unsigned int offset_sectors;
 	u8 *csum_expected;
 	u8 csum[BTRFS_CSUM_SIZE];
 
 	ASSERT(pgoff + len <= PAGE_SIZE);
 
-	offset_sectors = bio_offset >> fs_info->sectorsize_bits;
-	csum_expected = ((u8 *)bbio->csum) + offset_sectors * csum_size;
+	csum_expected = btrfs_csum_ptr(fs_info, bbio->csum, bio_offset);
 
 	if (!btrfs_check_data_sector(fs_info, page, pgoff, csum, csum_expected))
 		return 0;
@@ -8007,12 +8004,8 @@  static inline blk_status_t btrfs_submit_dio_bio(struct bio *bio,
 		if (ret)
 			goto err;
 	} else {
-		u64 csum_offset;
-
-		csum_offset = file_offset - dip->file_offset;
-		csum_offset >>= fs_info->sectorsize_bits;
-		csum_offset *= fs_info->csum_size;
-		btrfs_bio(bio)->csum = dip->csums + csum_offset;
+		btrfs_bio(bio)->csum = btrfs_csum_ptr(fs_info, dip->csums,
+				file_offset - dip->file_offset);
 	}
 map:
 	ret = btrfs_map_bio(fs_info, bio, 0);