btrfs: add helpers for manipulating leaf items and data
authorJosef Bacik <josef@toxicpanda.com>
Tue, 15 Nov 2022 16:16:17 +0000 (11:16 -0500)
committerDavid Sterba <dsterba@suse.com>
Mon, 5 Dec 2022 17:00:58 +0000 (18:00 +0100)
commit637e3b48c22e52daa645b49b73630af3f09328ae
treebdb71c02f1917fe133729d1328ef8a44613d43c8
parente23efd8e8767165a6103cf0a4fe273f6b9f182f2
btrfs: add helpers for manipulating leaf items and data

We have some gnarly memmove and copy_extent_buffer calls for leaf
manipulation.  This is because our item offsets aren't absolute, they're
based on 0 being where the items start in the leaf, which is after the
btrfs_header.  This means any manipulation of the data requires adding
sizeof(struct btrfs_header) to the offsets we pull from the items.
Moving the items themselves is easier as the helpers are absolute
offsets, however we of course have to call the helpers to get the
offsets for the item numbers.  This makes for
copy_extent_buffer/memmove_extent_buffer calls that are kind of hard to
reason about what's happening.

Fix this by pushing this logic into helpers.  For data we'll only use
the item provided offsets, and the helpers will use the
BTRFS_LEAF_DATA_OFFSET addition for the offsets.  Additionally for the
item manipulation simply pass in the item numbers, and then the helpers
will call the offset helper to get the actual offset into the leaf.

The diffstat makes this look like more code, but that's simply because I
added comments for the helpers, it's net negative for the amount of
code, and is easier to reason.

Signed-off-by: Josef Bacik <josef@toxicpanda.com>
Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/ctree.c