Commit 8f9da810 authored by David Sterba's avatar David Sterba

btrfs: speed up btrfs_get_token_##bits helpers

The set/get token helpers either use the cached address in the token or
unconditionally call map_private_extent_buffer to get the address of
page containing the requested offset plus the mapping start and length.
Depending on the return value, the fast path uses unaligned read to get
data within a page, or fall back to read_extent_buffer that can handle
reads spanning more pages.

This is all wasteful. We know the number of bytes to read, 1/2/4/8 and
can find out the page. Then simply check if it's contained or the
fallback is needed. The token address is updated to the page, or the on
the next index, expecting that the next read will use that.

This saves one function call to map_private_extent_buffer and several
unnecessary temporary variables.
Reviewed-by: default avatarJohannes Thumshirn <johannes.thumshirn@wdc.com>
Signed-off-by: default avatarDavid Sterba <dsterba@suse.com>
parent 1441ed9b
...@@ -62,39 +62,28 @@ static bool check_setget_bounds(const struct extent_buffer *eb, ...@@ -62,39 +62,28 @@ static bool check_setget_bounds(const struct extent_buffer *eb,
u##bits btrfs_get_token_##bits(struct btrfs_map_token *token, \ u##bits btrfs_get_token_##bits(struct btrfs_map_token *token, \
const void *ptr, unsigned long off) \ const void *ptr, unsigned long off) \
{ \ { \
unsigned long part_offset = (unsigned long)ptr; \ const unsigned long member_offset = (unsigned long)ptr + off; \
unsigned long offset = part_offset + off; \ const unsigned long idx = member_offset >> PAGE_SHIFT; \
void *p; \ const unsigned long oip = offset_in_page(member_offset); \
int err; \ const int size = sizeof(u##bits); \
char *kaddr; \ __le##bits leres; \
unsigned long map_start; \
unsigned long map_len; \
int size = sizeof(u##bits); \
u##bits res; \
\ \
ASSERT(token); \ ASSERT(token); \
ASSERT(token->kaddr); \ ASSERT(token->kaddr); \
ASSERT(check_setget_bounds(token->eb, ptr, off, size)); \ ASSERT(check_setget_bounds(token->eb, ptr, off, size)); \
if (token->offset <= offset && \ if (token->offset <= member_offset && \
(token->offset + PAGE_SIZE >= offset + size)) { \ member_offset + size <= token->offset + PAGE_SIZE) { \
kaddr = token->kaddr; \ return get_unaligned_le##bits(token->kaddr + oip); \
p = kaddr + part_offset - token->offset; \
res = get_unaligned_le##bits(p + off); \
return res; \
} \ } \
err = map_private_extent_buffer(token->eb, offset, size, \ if (oip + size <= PAGE_SIZE) { \
&kaddr, &map_start, &map_len); \ token->kaddr = page_address(token->eb->pages[idx]); \
if (err) { \ token->offset = idx << PAGE_SHIFT; \
__le##bits leres; \ return get_unaligned_le##bits(token->kaddr + oip); \
\
read_extent_buffer(token->eb, &leres, offset, size); \
return le##bits##_to_cpu(leres); \
} \ } \
p = kaddr + part_offset - map_start; \ token->kaddr = page_address(token->eb->pages[idx + 1]); \
res = get_unaligned_le##bits(p + off); \ token->offset = (idx + 1) << PAGE_SHIFT; \
token->kaddr = kaddr; \ read_extent_buffer(token->eb, &leres, member_offset, size); \
token->offset = map_start; \ return le##bits##_to_cpu(leres); \
return res; \
} \ } \
u##bits btrfs_get_##bits(const struct extent_buffer *eb, \ u##bits btrfs_get_##bits(const struct extent_buffer *eb, \
const void *ptr, unsigned long off) \ const void *ptr, unsigned long off) \
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment