btrfs: refactor checksum calculations in btrfs_lookup_csums_range()
authorQu Wenruo <wqu@suse.com>
Mon, 14 Nov 2022 00:26:31 +0000 (08:26 +0800)
committerDavid Sterba <dsterba@suse.com>
Mon, 5 Dec 2022 17:00:57 +0000 (18:00 +0100)
commitcb649e81dad429ffbd97a689ac0011599952a668
treee9d9c79e32a5270c36a5f95dbd7b55b8f39b732d
parent9f0eac070d23405f18e7a84820bc3d59b1415bec
btrfs: refactor checksum calculations in btrfs_lookup_csums_range()

The refactoring involves the following parts:

- Introduce bytes_to_csum_size() and csum_size_to_bytes() helpers
  As we have quite some open-coded calculations, some of them are even
  split into two assignments just to fit 80 chars limit.

- Remove the @csum_size parameter from max_ordered_sum_bytes()
  Csum size can be fetched from @fs_info.
  And we will use the csum_size_to_bytes() helper anyway.

- Add a comment explaining how we handle the first search result

- Use newly introduced helpers to cleanup btrfs_lookup_csums_range()

- Move variables declaration to the minimal scope

- Never mix number of sectors with bytes
  There are several locations doing things like:

  size = min_t(size_t, csum_end - start,
     max_ordered_sum_bytes(fs_info));
...
size >>= fs_info->sectorsize_bits

  Or

offset = (start - key.offset) >> fs_info->sectorsize_bits;
offset *= csum_size;

  Make sure these variables can only represent BYTES inside the
  function, by using the above bytes_to_csum_size() helpers.

Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/file-item.c