Set/get helpers speedups and cleanups
David Sterba
maintainercover Set/get helpers speedups and cleanups
Set/get helpers are all the functions that read or write b-tree item members, like btrfs_device_type, btrfs_set_device_type, or the variants with token btrfs_token_device_type, defined in ctree.h with BTRFS_SETGET_FUNCS. Most of them are implemented by btrfs_get_8/16/32/64 and btrfs_set_8/16/32/64 functions.
The helpers are used all over the place and can be seen in perf top results quite often, though it's about 0.5-1.5%. This series simplifies the helpers and removes function calls that do the translation from/to the input buffers to/from the extent buffer pages.
I don't have exact number to show the speedup, watching perf top and the same workload at least does not point to a drop, the functions are still in the overall range 0.5-1.5%. The code cleanups result in a straightforward code, also map_private_extent_buffer is gone!!!
David Sterba (19):
btrfs: use the token::eb for all set/get helpers
btrfs: drop eb parameter from set/get token helpers
btrfs: don't use set/get token for single assignment in overwrite_item
btrfs: don't use set/get token in leaf_space_used
btrfs: preset set/get token with first page and drop condition
btrfs: add separate bounds checker for set/get helpers
btrfs: speed up btrfs_get_##bits helpers
btrfs: speed up btrfs_get_token_##bits helpers
btrfs: speed up btrfs_set_##bits helpers
btrfs: speed up btrfs_set_token_##bits helpers
btrfs: speed up and simplify generic_bin_search
btrfs: remove unused map_private_extent_buffer
btrfs: constify extent_buffer in the API functions
btrfs: drop unnecessary offset_in_page in extent buffer helpers
btrfs: optimize split page read in btrfs_get_##bits
btrfs: optimize split page read in btrfs_get_token_##bits
btrfs: optimize split page write in btrfs_set_##bits
btrfs: optimize split page write in btrfs_set_token_##bits
btrfs: update documentation of set/get helpers
fs/btrfs/ctree.c | 98 +++++++----------- fs/btrfs/ctree.h | 58 +++++------ fs/btrfs/extent_io.c | 137 ++++++++----------------- fs/btrfs/extent_io.h | 48 ++++----- fs/btrfs/inode.c | 71 ++++++------- fs/btrfs/struct-funcs.c | 222 ++++++++++++++++++++-------------------- fs/btrfs/tree-log.c | 130 +++++++++++------------ 7 files changed, 337 insertions(+), 427 deletions(-)
Signature · 1 line
2.25.0
David Sterba
maintainerPATCH 1/19 btrfs: use the token::eb for all set/get helpers
The token stores a copy of the extent buffer pointer but does not make any use of it besides sanity checks. We can use it and drop the eb parameter from several functions, this patch only switches the use inside the set/get helpers.
--- fs/btrfs/struct-funcs.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
fs/btrfs/struct-funcs.c +4 -4
@@ -62,12 +62,12 @@ u##bits btrfs_get_token_##bits(const struct extent_buffer *eb, \ res = get_unaligned_le##bits(p + off); \ return res; \ } \ - err = map_private_extent_buffer(eb, offset, size, \ + err = map_private_extent_buffer(token->eb, offset, size, \ &kaddr, &map_start, &map_len); \ if (err) { \ __le##bits leres; \ \ - read_extent_buffer(eb, &leres, offset, size); \ + read_extent_buffer(token->eb, &leres, offset, size); \ return le##bits##_to_cpu(leres); \ } \ p = kaddr + part_offset - map_start; \
@@ -125,13 +125,13 @@ void btrfs_set_token_##bits(struct extent_buffer *eb, \ put_unaligned_le##bits(val, p + off); \ return; \ } \ - err = map_private_extent_buffer(eb, offset, size, \ + err = map_private_extent_buffer(token->eb, offset, size, \ &kaddr, &map_start, &map_len); \ if (err) { \ __le##bits val2; \ \ val2 = cpu_to_le##bits(val); \ - write_extent_buffer(eb, &val2, offset, size); \ + write_extent_buffer(token->eb, &val2, offset, size); \ return; \ } \ p = kaddr + part_offset - map_start; \
Signature · 1 line
2.25.0
Re: [PATCH 01/19] btrfs: use the token::eb for all set/get helpers
Looks good,
David Sterba
maintainerPATCH 2/19 btrfs: drop eb parameter from set/get token helpers
In reply to David SterbaNow that all set/get helpers use the eb from the token, we don't need to pass it to many btrfs_token_*/btrfs_set_token_* helpers, saving some stack space.
--- fs/btrfs/ctree.c | 53 ++++++++--------- fs/btrfs/ctree.h | 27 ++++----- fs/btrfs/inode.c | 71 +++++++++++------------ fs/btrfs/struct-funcs.c | 12 ++-- fs/btrfs/tree-log.c | 125 ++++++++++++++++++---------------------- 5 files changed, 130 insertions(+), 158 deletions(-)
fs/btrfs/ctree.c +23 -30
@@ -3517,9 +3517,9 @@ static int leaf_space_used(struct extent_buffer *l, int start, int nr)btrfs_init_map_token (&token, l); start_item =btrfs_item_nr (start); end_item =btrfs_item_nr (end); - data_len =btrfs_token_item_offset (l, start_item, &token) + -btrfs_token_item_size (l, start_item, &token); - data_len = data_len -btrfs_token_item_offset (l, end_item, &token); + data_len =btrfs_token_item_offset (&token, start_item) + +btrfs_token_item_size (&token, start_item); + data_len = data_len -btrfs_token_item_offset (&token, end_item); data_len +=sizeof (struct btrfs_item ) * nr;WARN_ON (data_len <0 );return data_len;
@@ -3650,8 +3650,8 @@ static noinline int __push_leaf_right(struct btrfs_path *path, push_space =BTRFS_LEAF_DATA_SIZE (fs_info);for (i =0 ; i < right_nritems; i++) { item =btrfs_item_nr (i); - push_space -=btrfs_token_item_size (right, item, &token); -btrfs_set_token_item_offset (right, item, push_space, &token); + push_space -=btrfs_token_item_size (&token, item); +btrfs_set_token_item_offset (&token, item, push_space); } left_nritems -= push_items;
@@ -3859,10 +3859,9 @@ static noinline int __push_leaf_left(struct btrfs_path *path, int data_size, item =btrfs_item_nr (i); - ioff =btrfs_token_item_offset (left, item, &token); -btrfs_set_token_item_offset (left, item, - ioff - (BTRFS_LEAF_DATA_SIZE (fs_info) - old_left_item_size), - &token); + ioff =btrfs_token_item_offset (&token, item); +btrfs_set_token_item_offset (&token, item, + ioff - (BTRFS_LEAF_DATA_SIZE (fs_info) - old_left_item_size)); }btrfs_set_header_nritems (left, old_left_nritems + push_items);
@@ -3892,9 +3891,8 @@ static noinline int __push_leaf_left(struct btrfs_path *path, int data_size,for (i =0 ; i < right_nritems; i++) { item =btrfs_item_nr (i); - push_space = push_space -btrfs_token_item_size (right, - item, &token); -btrfs_set_token_item_offset (right, item, push_space, &token); + push_space = push_space -btrfs_token_item_size (&token, item); +btrfs_set_token_item_offset (&token, item, push_space); }btrfs_mark_buffer_dirty (left);
@@ -4036,9 +4034,8 @@ static noinline void copy_for_split(struct btrfs_trans_handle *trans,struct btrfs_item *item =btrfs_item_nr (i);u32 ioff; - ioff =btrfs_token_item_offset (right, item, &token); -btrfs_set_token_item_offset (right, item, - ioff + rt_data_off, &token); + ioff =btrfs_token_item_offset (&token, item); +btrfs_set_token_item_offset (&token, item, ioff + rt_data_off); }btrfs_set_header_nritems (l, mid);
@@ -4541,9 +4538,8 @@ void btrfs_truncate_item(struct btrfs_path *path, u32 new_size, int from_end)u32 ioff; item =btrfs_item_nr (i); - ioff =btrfs_token_item_offset (leaf, item, &token); -btrfs_set_token_item_offset (leaf, item, - ioff + size_diff, &token); + ioff =btrfs_token_item_offset (&token, item); +btrfs_set_token_item_offset (&token, item, ioff + size_diff); }/* shift the data */
@@ -4640,9 +4636,8 @@ void btrfs_extend_item(struct btrfs_path *path, u32 data_size)u32 ioff; item =btrfs_item_nr (i); - ioff =btrfs_token_item_offset (leaf, item, &token); -btrfs_set_token_item_offset (leaf, item, - ioff - data_size, &token); + ioff =btrfs_token_item_offset (&token, item); +btrfs_set_token_item_offset (&token, item, ioff - data_size); }/* shift the data */
@@ -4718,9 +4713,9 @@ void setup_items_for_insert(struct btrfs_root *root, struct btrfs_path *path,u32 ioff; item =btrfs_item_nr (i); - ioff =btrfs_token_item_offset (leaf, item, &token); -btrfs_set_token_item_offset (leaf, item, - ioff - total_data, &token); + ioff =btrfs_token_item_offset (&token, item); +btrfs_set_token_item_offset (&token, item, + ioff - total_data); }/* shift the items */ memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
@@ -4739,10 +4734,9 @@ void setup_items_for_insert(struct btrfs_root *root, struct btrfs_path *path,btrfs_cpu_key_to_disk (&disk_key, cpu_key + i);btrfs_set_item_key (leaf, &disk_key, slot + i); item =btrfs_item_nr (slot + i); -btrfs_set_token_item_offset (leaf, item, - data_end - data_size[i], &token); +btrfs_set_token_item_offset (&token, item, data_end - data_size[i]); data_end -= data_size[i]; -btrfs_set_token_item_size (leaf, item, data_size[i], &token); +btrfs_set_token_item_size (&token, item, data_size[i]); }btrfs_set_header_nritems (leaf, nritems + nr);
@@ -4930,9 +4924,8 @@ int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,u32 ioff; item =btrfs_item_nr (i); - ioff =btrfs_token_item_offset (leaf, item, &token); -btrfs_set_token_item_offset (leaf, item, - ioff + dsize, &token); + ioff =btrfs_token_item_offset (&token, item); +btrfs_set_token_item_offset (&token, item, ioff + dsize); } memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
fs/btrfs/ctree.h +12 -15
@@ -1340,7 +1340,7 @@ do { \ BTRFS_INODE_ROOT_ITEM_INIT)struct btrfs_map_token { -const struct extent_buffer *eb; +struct extent_buffer *eb;char *kaddr;unsigned long offset; };
@@ -1376,12 +1376,11 @@ static inline void btrfs_init_map_token(struct btrfs_map_token *token, sizeof(((type *)0)->member)))#define DECLARE_BTRFS_SETGET_BITS (bits) \ -u##bits btrfs_get_token_##bits(const struct extent_buffer *eb, \ - const void *ptr, unsigned long off, \ - struct btrfs_map_token *token); \ -void btrfs_set_token_##bits(struct extent_buffer *eb, const void *ptr, \ - unsigned long off, u##bits val, \ - struct btrfs_map_token *token); \ +u##bits btrfs_get_token_##bits(struct btrfs_map_token *token, \ + const void *ptr, unsigned long off); \ +void btrfs_set_token_##bits(struct btrfs_map_token *token, \ + const void *ptr, unsigned long off, \ + u##bits val); \ u##bits btrfs_get_##bits(const struct extent_buffer *eb, \ const void *ptr, unsigned long off); \ void btrfs_set_##bits(struct extent_buffer *eb, void *ptr, \
@@ -1405,19 +1404,17 @@ static inline void btrfs_set_##name(struct extent_buffer *eb, type *s, \ BUILD_BUG_ON(sizeof(u##bits) != sizeof(((type *)0))->member); \ btrfs_set_##bits(eb, s, offsetof(type, member), val); \ } \ -static inline u##bits btrfs_token_##name(const struct extent_buffer *eb,\ - const type *s, \ - struct btrfs_map_token *token) \ +static inline u##bits btrfs_token_##name(struct btrfs_map_token *token, \ + const type *s) \ { \ BUILD_BUG_ON(sizeof(u##bits) != sizeof(((type *)0))->member); \ - return btrfs_get_token_##bits(eb, s, offsetof(type, member), token); \ + return btrfs_get_token_##bits(token, s, offsetof(type, member));\ } \ -static inline void btrfs_set_token_##name(struct extent_buffer *eb, \ - type *s, u##bits val, \ - struct btrfs_map_token *token) \ +static inline void btrfs_set_token_##name(struct btrfs_map_token *token,\ + type *s, u##bits val) \ { \ BUILD_BUG_ON(sizeof(u##bits) != sizeof(((type *)0))->member); \ - btrfs_set_token_##bits(eb, s, offsetof(type, member), val, token); \ + btrfs_set_token_##bits(token, s, offsetof(type, member), val); \ }#define BTRFS_SETGET_HEADER_FUNCS (name, type, member, bits) \
fs/btrfs/inode.c +34 -37
77 lines not on this page · raw message
fs/btrfs/struct-funcs.c +4 -8
@@ -38,9 +38,8 @@ static inline void put_unaligned_le8(u8 val, void *p)*/ #define DEFINE_BTRFS_SETGET_BITS (bits) \ -u##bits btrfs_get_token_##bits(const struct extent_buffer *eb, \ - const void *ptr, unsigned long off, \ - struct btrfs_map_token *token) \ +u##bits btrfs_get_token_##bits(struct btrfs_map_token *token, \ + const void *ptr, unsigned long off) \ { \ unsigned long part_offset = (unsigned long)ptr; \ unsigned long offset = part_offset + off; \
@@ -53,7 +52,6 @@ u##bits btrfs_get_token_##bits(const struct extent_buffer *eb, \ u##bits res; \ \ ASSERT(token); \ - ASSERT(token->eb == eb); \ \ if (token->kaddr && token->offset <= offset && \ (token->offset + PAGE_SIZE >= offset + size)) { \
@@ -101,10 +99,9 @@ u##bits btrfs_get_##bits(const struct extent_buffer *eb, \ res = get_unaligned_le##bits(p + off); \ return res; \ } \ -void btrfs_set_token_##bits(struct extent_buffer *eb, \ +void btrfs_set_token_##bits(struct btrfs_map_token *token, \ const void *ptr, unsigned long off, \ - u##bits val, \ - struct btrfs_map_token *token) \ + u##bits val) \ { \ unsigned long part_offset = (unsigned long)ptr; \ unsigned long offset = part_offset + off; \
@@ -116,7 +113,6 @@ void btrfs_set_token_##bits(struct extent_buffer *eb, \ int size = sizeof(u##bits); \ \ASSERT (token); \ -ASSERT (token->eb == eb); \ \if (token->kaddr && token->offset <= offset && \ (token->offset +PAGE_SIZE >= offset + size)) { \
fs/btrfs/tree-log.c +57 -68
152 lines not on this page · raw message
Signature · 1 line
2.25.0
Re: [PATCH 02/19] btrfs: drop eb parameter from set/get token helpers
fs/btrfs/ctree.c static noinline int __push_leaf_left(struct btrfs_path *path, int data_size, · line 3894
Quoted 1 line · David Sterba
fs/btrfs/ctree.c +1
-0+ push_space = push_space -btrfs_token_item_size (&token, item);
Nit: push_space -= btrfs_token_item_size(&token, item);
Anayways,
Re: [PATCH 02/19] btrfs: drop eb parameter from set/get token helpers
Quoted 4 lines · Johannes Thumshirn
fs/btrfs/ctree.c static noinline int __push_leaf_left(struct btrfs_path *path, int data_size, · line 3894
Quoted 1 line · David Sterba
fs/btrfs/ctree.c +1
-0+ push_space = push_space -btrfs_token_item_size (&token, item);Nit:
push_space-=btrfs_token_item_size(&token, item);
Yeah it could be done that way but I'd rather avoid mixing such small fixups to a patch that's otherwise a mechanical change.
Re: [PATCH 02/19] btrfs: drop eb parameter from set/get token helpers
Quoted 9 lines · David Sterba
Quoted 4 lines · Johannes Thumshirn
fs/btrfs/ctree.c static noinline int __push_leaf_left(struct btrfs_path *path, int data_size, · line 3894
Quoted 1 line · David Sterba
fs/btrfs/ctree.c +1
-0+ push_space = push_space -btrfs_token_item_size (&token, item);Nit:
push_space-=btrfs_token_item_size(&token, item);Yeah it could be done that way but I'd rather avoid mixing such small fixups to a patch that's otherwise a mechanical change.
Fair enough
David Sterba
maintainerPATCH 3/19 btrfs: don't use set/get token for single assignment in overwrite_item
In reply to David SterbaThe set/get token is supposed to cache the last page that was accessed so it speeds up subsequential access to the eb. It does not make sense to use that for just one change, which is the case of inode size in overwrite_item.
--- fs/btrfs/tree-log.c | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-)
fs/btrfs/tree-log.c +2 -7
@@ -505,13 +505,8 @@ static noinline int overwrite_item(struct btrfs_trans_handle *trans,*/ if (S_ISREG (btrfs_inode_mode (eb, src_item)) &&S_ISREG (btrfs_inode_mode (dst_eb, dst_item)) && - ino_size !=0 ) { -struct btrfs_map_token token; - -btrfs_init_map_token (&token, dst_eb); -btrfs_set_token_inode_size (&token, dst_item, - ino_size); - } + ino_size !=0 ) +btrfs_set_inode_size (dst_eb, dst_item, ino_size); goto no_copy; }
Signature · 1 line
2.25.0
Re: [PATCH 03/19] btrfs: don't use set/get token for single assignment in overwrite_item
Looks good,
David Sterba
maintainerPATCH 4/19 btrfs: don't use set/get token in leaf_space_used
In reply to David SterbaThe token is supposed to cache the last page used by the set/get helpers. In leaf_space_used the first and last items are accessed, it's not likely they'd be on the same page so there's some overhead caused updating the token address but not using it.
--- fs/btrfs/ctree.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-)
fs/btrfs/ctree.c +3 -5
@@ -3507,19 +3507,17 @@ static int leaf_space_used(struct extent_buffer *l, int start, int nr) {struct btrfs_item *start_item;struct btrfs_item *end_item; -struct btrfs_map_token token;int data_len;int nritems =btrfs_header_nritems (l);int end =min (nritems, start + nr) -1 ;if (!nr)return 0 ; -btrfs_init_map_token (&token, l); start_item =btrfs_item_nr (start); end_item =btrfs_item_nr (end); - data_len =btrfs_token_item_offset (&token, start_item) + -btrfs_token_item_size (&token, start_item); - data_len = data_len -btrfs_token_item_offset (&token, end_item); + data_len =btrfs_item_offset (l, start_item) + +btrfs_item_size (l, start_item); + data_len = data_len -btrfs_item_offset (l, end_item); data_len +=sizeof (struct btrfs_item ) * nr;WARN_ON (data_len <0 );return data_len;
Signature · 1 line
2.25.0
Re: [PATCH 04/19] btrfs: don't use set/get token in leaf_space_used
fs/btrfs/ctree.c static int leaf_space_used(struct extent_buffer *l, int start, int nr) · line 3520
Quoted 1 line · David Sterba
fs/btrfs/ctree.c +1
-0+ data_len = data_len -btrfs_item_offset (l, end_item);
Nit: Could again be data_len -= btrfs_item_offset(l, end_item); Otherwise,
David Sterba
maintainerPATCH 5/19 btrfs: preset set/get token with first page and drop condition
In reply to David SterbaAll the set/get helpers first check if the token contains a cached address. After first use the address is always valid, but the extra check is done for each call.
The token initialization can optimistically set it to the first extent buffer page, that we know always exists. Then the condition in all btrfs_token_*/btrfs_set_token_* can be simplified by removing the address check from the condition, but for development the assertion still makes sure it's valid.
--- fs/btrfs/ctree.h | 3 ++- fs/btrfs/struct-funcs.c | 8 ++++---- 2 files changed, 6 insertions(+), 5 deletions(-)
fs/btrfs/ctree.h +2 -1
@@ -1352,7 +1352,8 @@ static inline void btrfs_init_map_token(struct btrfs_map_token *token, struct extent_buffer *eb) { token->eb = eb; - token->kaddr =NULL ; + token->kaddr =page_address (eb->pages[0 ]); + token->offset =0 ; }/* some macros to generate set/get functions for the struct fields. This
fs/btrfs/struct-funcs.c +4 -4
@@ -52,8 +52,8 @@ u##bits btrfs_get_token_##bits(struct btrfs_map_token *token, \ u##bits res; \ \ ASSERT(token); \ - \ - if (token->kaddr && token->offset <= offset && \ + ASSERT(token->kaddr); \ + if (token->offset <= offset && \ (token->offset + PAGE_SIZE >= offset + size)) { \ kaddr = token->kaddr; \ p = kaddr + part_offset - token->offset; \
@@ -113,8 +113,8 @@ void btrfs_set_token_##bits(struct btrfs_map_token *token, \ int size = sizeof(u##bits); \ \ASSERT (token); \ - \ -if (token->kaddr && token->offset <= offset && \ +ASSERT (token->kaddr); \ +if (token->offset <= offset && \ (token->offset +PAGE_SIZE >= offset + size)) { \ kaddr = token->kaddr; \ p = kaddr + part_offset - token->offset; \
Signature · 1 line
2.25.0
Re: [PATCH 05/19] btrfs: preset set/get token with first page and drop condition
Looks good,
David Sterba
maintainerPATCH 6/19 btrfs: add separate bounds checker for set/get helpers
In reply to David SterbaThe bounds checking is now done in map_private_extent_buffer but that will be removed in following patches and some sanity checks should still be done.
There are two separate checks to see the kind of out of bounds access:
partial (start offset is in the buffer) or complete (both start and end are out).
--- fs/btrfs/struct-funcs.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+)
fs/btrfs/struct-funcs.c +25 -0
@@ -17,6 +17,27 @@ static inline void put_unaligned_le8(u8 val, void *p) *(u8 *)p = val; } +static bool check_setget_bounds (const struct extent_buffer *eb, +const void *ptr,unsigned off,int size) +{ +const unsigned long member_offset = (unsigned long )ptr + off; + +if (member_offset > eb->len) { +btrfs_warn (eb->fs_info, +"bad eb member start: ptr 0x%lx start %llu member offset %lu size %d" , + (unsigned long )ptr, eb->start, member_offset, size); +return false; + } +if (member_offset + size > eb->len) { +btrfs_warn (eb->fs_info, +"bad eb member end: ptr 0x%lx start %llu member offset %lu size %d" , + (unsigned long )ptr, eb->start, member_offset, size); +return false; + } + +return true; +} +/* * this is some deeply nasty code. *
@@ -53,6 +74,7 @@ u##bits btrfs_get_token_##bits(struct btrfs_map_token *token, \ \ASSERT (token); \ASSERT (token->kaddr); \ +ASSERT (check_setget_bounds (token->eb, ptr, off, size)); \if (token->offset <= offset && \ (token->offset +PAGE_SIZE >= offset + size)) { \ kaddr = token->kaddr; \
@@ -87,6 +109,7 @@ u##bits btrfs_get_##bits(const struct extent_buffer *eb, \ int size = sizeof(u##bits); \ u##bits res; \ \ + ASSERT(check_setget_bounds(eb, ptr, off, size)); \ err = map_private_extent_buffer(eb, offset, size, \ &kaddr, &map_start, &map_len); \ if (err) { \
@@ -114,6 +137,7 @@ void btrfs_set_token_##bits(struct btrfs_map_token *token, \ \ASSERT (token); \ASSERT (token->kaddr); \ +ASSERT (check_setget_bounds (token->eb, ptr, off, size)); \if (token->offset <= offset && \ (token->offset +PAGE_SIZE >= offset + size)) { \ kaddr = token->kaddr; \
@@ -147,6 +171,7 @@ void btrfs_set_##bits(struct extent_buffer *eb, void *ptr, \unsigned long map_len; \ int size = sizeof(u##bits); \ \ +ASSERT (check_setget_bounds (eb, ptr, off, size)); \ err =map_private_extent_buffer (eb, offset, size, \ &kaddr, &map_start, &map_len); \if (err) { \
Signature · 1 line
2.25.0
Re: [PATCH 06/19] btrfs: add separate bounds checker for set/get helpers
Looks good,
David Sterba
maintainerPATCH 7/19 btrfs: speed up btrfs_get_##bits helpers
In reply to David SterbaThe helpers 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.
This saves one function call to map_private_extent_buffer and several unnecessary temporary variables.
--- fs/btrfs/struct-funcs.c | 29 ++++++++++------------------- 1 file changed, 10 insertions(+), 19 deletions(-)
fs/btrfs/struct-funcs.c +10 -19
@@ -99,28 +99,19 @@ u##bits btrfs_get_token_##bits(struct btrfs_map_token *token, \ u##bits btrfs_get_##bits(const struct extent_buffer *eb, \ const void *ptr, unsigned long off) \ { \ - unsigned long part_offset = (unsigned long)ptr; \ - unsigned long offset = part_offset + off; \ - void *p; \ - int err; \ - char *kaddr; \ - unsigned long map_start; \ - unsigned long map_len; \ - int size = sizeof(u##bits); \ - u##bits res; \ + const unsigned long member_offset = (unsigned long)ptr + off; \ + const unsigned long oip = offset_in_page(member_offset); \ + const int size = sizeof(u##bits); \ + __le##bits leres; \ \ ASSERT(check_setget_bounds(eb, ptr, off, size)); \ - err = map_private_extent_buffer(eb, offset, size, \ - &kaddr, &map_start, &map_len); \ - if (err) { \ - __le##bits leres; \ - \ - read_extent_buffer(eb, &leres, offset, size); \ - return le##bits##_to_cpu(leres); \ + if (oip + size <= PAGE_SIZE) { \ + const unsigned long idx = member_offset >> PAGE_SHIFT; \ + const char *kaddr = page_address(eb->pages[idx]); \ + return get_unaligned_le##bits(kaddr + oip); \ } \ - p = kaddr + part_offset - map_start; \ - res = get_unaligned_le##bits(p + off); \ - return res; \ + read_extent_buffer(eb, &leres, member_offset, size); \ + return le##bits##_to_cpu(leres); \ } \ void btrfs_set_token_##bits(struct btrfs_map_token *token, \ const void *ptr, unsigned long off, \
Signature · 1 line
2.25.0
Re: [PATCH 07/19] btrfs: speed up btrfs_get_##bits helpers
Looks good,
David Sterba
maintainerPATCH 8/19 btrfs: speed up btrfs_get_token_##bits helpers
In reply to David SterbaThe 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.
--- fs/btrfs/struct-funcs.c | 43 +++++++++++++++-------------------------- 1 file changed, 16 insertions(+), 27 deletions(-)
fs/btrfs/struct-funcs.c +16 -27
@@ -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, \ const void *ptr, unsigned long off) \ { \ - unsigned long part_offset = (unsigned long)ptr; \ - unsigned long offset = part_offset + off; \ - void *p; \ - int err; \ - char *kaddr; \ - unsigned long map_start; \ - unsigned long map_len; \ - int size = sizeof(u##bits); \ - u##bits res; \ + const unsigned long member_offset = (unsigned long)ptr + off; \ + const unsigned long idx = member_offset >> PAGE_SHIFT; \ + const unsigned long oip = offset_in_page(member_offset); \ + const int size = sizeof(u##bits); \ + __le##bits leres; \ \ ASSERT(token); \ ASSERT(token->kaddr); \ ASSERT(check_setget_bounds(token->eb, ptr, off, size)); \ - if (token->offset <= offset && \ - (token->offset + PAGE_SIZE >= offset + size)) { \ - kaddr = token->kaddr; \ - p = kaddr + part_offset - token->offset; \ - res = get_unaligned_le##bits(p + off); \ - return res; \ + if (token->offset <= member_offset && \ + member_offset + size <= token->offset + PAGE_SIZE) { \ + return get_unaligned_le##bits(token->kaddr + oip); \ } \ - err = map_private_extent_buffer(token->eb, offset, size, \ - &kaddr, &map_start, &map_len); \ - if (err) { \ - __le##bits leres; \ - \ - read_extent_buffer(token->eb, &leres, offset, size); \ - return le##bits##_to_cpu(leres); \ + if (oip + size <= PAGE_SIZE) { \ + token->kaddr = page_address(token->eb->pages[idx]); \ + token->offset = idx << PAGE_SHIFT; \ + return get_unaligned_le##bits(token->kaddr + oip); \ } \ - p = kaddr + part_offset - map_start; \ - res = get_unaligned_le##bits(p + off); \ - token->kaddr = kaddr; \ - token->offset = map_start; \ - return res; \ + token->kaddr = page_address(token->eb->pages[idx + 1]); \ + token->offset = (idx + 1) << PAGE_SHIFT; \ + read_extent_buffer(token->eb, &leres, member_offset, size); \ + return le##bits##_to_cpu(leres); \ } \ u##bits btrfs_get_##bits(const struct extent_buffer *eb, \ const void *ptr, unsigned long off) \
Signature · 1 line
2.25.0
Re: [PATCH 08/19] btrfs: speed up btrfs_get_token_##bits helpers
Looks good,
David Sterba
maintainerPATCH 9/19 btrfs: speed up btrfs_set_##bits helpers
In reply to David SterbaThe helpers 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 put to write data within a page, or fall back to write_extent_buffer that can handle writes 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.
This saves one function call to map_private_extent_buffer and several unnecessary temporary variables.
--- fs/btrfs/struct-funcs.c | 27 ++++++++++----------------- 1 file changed, 10 insertions(+), 17 deletions(-)
fs/btrfs/struct-funcs.c +10 -17
@@ -142,27 +142,20 @@ void btrfs_set_token_##bits(struct btrfs_map_token *token, \ void btrfs_set_##bits(struct extent_buffer *eb, void *ptr, \ unsigned long off, u##bits val) \ { \ - unsigned long part_offset = (unsigned long)ptr; \ - unsigned long offset = part_offset + off; \ - void *p; \ - int err; \ - char *kaddr; \ - unsigned long map_start; \ - unsigned long map_len; \ - int size = sizeof(u##bits); \ + const unsigned long member_offset = (unsigned long)ptr + off; \ + const unsigned long oip = offset_in_page(member_offset); \ + const int size = sizeof(u##bits); \ + __le##bits leres; \ \ ASSERT(check_setget_bounds(eb, ptr, off, size)); \ - err = map_private_extent_buffer(eb, offset, size, \ - &kaddr, &map_start, &map_len); \ - if (err) { \ - __le##bits val2; \ - \ - val2 = cpu_to_le##bits(val); \ - write_extent_buffer(eb, &val2, offset, size); \ + if (oip + size <= PAGE_SIZE) { \ + const unsigned long idx = member_offset >> PAGE_SHIFT; \ + char *kaddr = page_address(eb->pages[idx]); \ + put_unaligned_le##bits(val, kaddr + oip); \ return; \ } \ - p = kaddr + part_offset - map_start; \ - put_unaligned_le##bits(val, p + off); \ + leres = cpu_to_le##bits(val); \ + write_extent_buffer(eb, &leres, member_offset, size); \ }DEFINE_BTRFS_SETGET_BITS (8 )
Signature · 1 line
2.25.0
Re: [PATCH 09/19] btrfs: speed up btrfs_set_##bits helpers
Quoted 1 line · David Sterba
This is all wasteful. We know the number of bytes to read, 1/2/4/8 and
write? ~^
Otherwise,
David Sterba
maintainerPATCH 10/19 btrfs: speed up btrfs_set_token_##bits helpers
In reply to David SterbaThe 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 put to write 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 write will use that.
This saves one function call to map_private_extent_buffer and several unnecessary temporary variables.
--- fs/btrfs/struct-funcs.c | 40 ++++++++++++++++------------------------ 1 file changed, 16 insertions(+), 24 deletions(-)
fs/btrfs/struct-funcs.c +16 -24
@@ -106,38 +106,30 @@ void btrfs_set_token_##bits(struct btrfs_map_token *token, \ const void *ptr, unsigned long off, \ u##bits val) \ { \ - unsigned long part_offset = (unsigned long)ptr; \ - unsigned long offset = part_offset + off; \ - void *p; \ - int err; \ - char *kaddr; \ - unsigned long map_start; \ - unsigned long map_len; \ - int size = sizeof(u##bits); \ + const unsigned long member_offset = (unsigned long)ptr + off; \ + const unsigned long idx = member_offset >> PAGE_SHIFT; \ + const unsigned long oip = offset_in_page(member_offset); \ + const int size = sizeof(u##bits); \ + __le##bits leres; \ \ ASSERT(token); \ ASSERT(token->kaddr); \ ASSERT(check_setget_bounds(token->eb, ptr, off, size)); \ - if (token->offset <= offset && \ - (token->offset + PAGE_SIZE >= offset + size)) { \ - kaddr = token->kaddr; \ - p = kaddr + part_offset - token->offset; \ - put_unaligned_le##bits(val, p + off); \ + if (token->offset <= member_offset && \ + member_offset + size <= token->offset + PAGE_SIZE) { \ + put_unaligned_le##bits(val, token->kaddr + oip); \ return; \ } \ - err = map_private_extent_buffer(token->eb, offset, size, \ - &kaddr, &map_start, &map_len); \ - if (err) { \ - __le##bits val2; \ - \ - val2 = cpu_to_le##bits(val); \ - write_extent_buffer(token->eb, &val2, offset, size); \ + if (oip + size <= PAGE_SIZE) { \ + token->kaddr = page_address(token->eb->pages[idx]); \ + token->offset = idx << PAGE_SHIFT; \ + put_unaligned_le##bits(val, token->kaddr + oip); \ return; \ } \ - p = kaddr + part_offset - map_start; \ - put_unaligned_le##bits(val, p + off); \ - token->kaddr = kaddr; \ - token->offset = map_start; \ + token->kaddr = page_address(token->eb->pages[idx + 1]); \ + token->offset = (idx + 1) << PAGE_SHIFT; \ + leres = cpu_to_le##bits(val); \ + write_extent_buffer(token->eb, &leres, member_offset, size); \ } \ void btrfs_set_##bits(struct extent_buffer *eb, void *ptr, \ unsigned long off, u##bits val) \
Signature · 1 line
2.25.0
Re: [PATCH 10/19] btrfs: speed up btrfs_set_token_##bits helpers
Quoted 1 line · David Sterba
This is all wasteful. We know the number of bytes to read, 1/2/4/8 and
Same question, s/read/write/?
Otherwise,
Re: [PATCH 10/19] btrfs: speed up btrfs_set_token_##bits helpers
Quoted 4 lines · Johannes Thumshirn
Quoted 1 line · David Sterba
This is all wasteful. We know the number of bytes to read, 1/2/4/8 and
Same question, s/read/write/?
Yes, fixed, thanks.
David Sterba
maintainerPATCH 11/19 btrfs: speed up and simplify generic_bin_search
In reply to David SterbaThe bin search jumps over the extent buffer item keys, comparing directly the bytes if the key is in one page, or storing it in a temporary buffer in case it spans two pages.
The mapping start and length are obtained from map_private_extent_buffer, which is heavy weight compared to what we need. We know the key size and can find out the eb page in a simple way. For keys spanning two pages the fallback read_extent_buffer is used.
The temporary variables are reduced and moved to the scope of use.
--- fs/btrfs/ctree.c | 43 +++++++++++++++---------------------------- 1 file changed, 15 insertions(+), 28 deletions(-)
fs/btrfs/ctree.c +15 -28
@@ -1668,15 +1668,8 @@ static noinline int generic_bin_search(struct extent_buffer *eb, {int low =0 ;int high = max; -int mid;int ret; -struct btrfs_disk_key *tmp =NULL ; -struct btrfs_disk_key unaligned; -unsigned long offset; -char *kaddr =NULL ; -unsigned long map_start =0 ; -unsigned long map_len =0 ; -int err; +const int key_size =sizeof (struct btrfs_disk_key );if (low > high) { btrfs_err(eb->fs_info,
@@ -1687,32 +1680,26 @@ static noinline int generic_bin_search(struct extent_buffer *eb, }while (low < high) { +unsigned long oip; +unsigned long offset; +struct btrfs_disk_key *tmp; +struct btrfs_disk_key unaligned; +int mid; + mid = (low + high) /2 ; offset = p + mid * item_size; + oip =offset_in_page (offset); -if (!kaddr || offset < map_start || - (offset +sizeof (struct btrfs_disk_key )) > - map_start + map_len) { - - err =map_private_extent_buffer (eb, offset, -sizeof (struct btrfs_disk_key ), - &kaddr, &map_start, &map_len); - -if (!err) { - tmp = (struct btrfs_disk_key *)(kaddr + offset - - map_start); - }else if (err ==1 ) { -read_extent_buffer (eb, &unaligned, - offset,sizeof (unaligned)); - tmp = &unaligned; - }else { -return err; - } +if (oip + key_size <=PAGE_SIZE ) { +const unsigned long idx = offset >>PAGE_SHIFT ; +char *kaddr =page_address (eb->pages[idx]); + tmp = (struct btrfs_disk_key *)(kaddr + oip); }else { - tmp = (struct btrfs_disk_key *)(kaddr + offset - - map_start); +read_extent_buffer (eb, &unaligned, offset, key_size); + tmp = &unaligned; } + ret =comp_keys (tmp, key);if (ret <0 )
Signature · 1 line
2.25.0
Re: [PATCH 11/19] btrfs: speed up and simplify generic_bin_search
Nice simplification,
but one unrelated question, generic_bin_search() sounds so, well generic. Maybe we should rename it to __btrfs_bin_search(), given it's only called by btrfs_bin_search().
David Sterba
maintainerPATCH 12/19 btrfs: remove unused map_private_extent_buffer
In reply to David SterbaAll uses of map_private_extent_buffer have been replaced by more effective way. The set/get helpers have their own bounds checker. The function name was confusing since the non-private helper was removed in a65917156e34 ("Btrfs: stop using highmem for extent_buffers") many years ago.
--- fs/btrfs/extent_io.c | 42 ------------------------------------------ fs/btrfs/extent_io.h | 4 ---- 2 files changed, 46 deletions(-)
fs/btrfs/extent_io.c +0 -42
@@ -5685,48 +5685,6 @@ int read_extent_buffer_to_user(const struct extent_buffer *eb,return ret; } -/* -* return 0 if the item is found within a page. -* return 1 if the item spans two pages. -* return -EINVAL otherwise. -*/ -int map_private_extent_buffer (const struct extent_buffer *eb, -unsigned long start,unsigned long min_len, -char **map,unsigned long *map_start, -unsigned long *map_len) -{ -size_t offset; -char *kaddr; -struct page *p; -size_t start_offset =offset_in_page (eb->start); -unsigned long i = (start_offset + start) >>PAGE_SHIFT ; -unsigned long end_i = (start_offset + start + min_len -1 ) >> -PAGE_SHIFT ; - -if (start + min_len > eb->len) { -WARN (1 ,KERN_ERR "btrfs bad mapping eb start %llu len %lu, wanted %lu %lu\n" , - eb->start, eb->len, start, min_len); -return -EINVAL ; - } - -if (i != end_i) -return 1 ; - -if (i ==0 ) { - offset = start_offset; - *map_start =0 ; - }else { - offset =0 ; - *map_start = ((u64 )i <<PAGE_SHIFT ) - start_offset; - } - - p = eb->pages[i]; - kaddr =page_address (p); - *map = kaddr + offset; - *map_len =PAGE_SIZE - offset; -return 0 ; -} -int memcmp_extent_buffer (const struct extent_buffer *eb,const void *ptrv,unsigned long start,unsigned long len) {
fs/btrfs/extent_io.h +0 -4
@@ -271,10 +271,6 @@ bool set_extent_buffer_dirty(struct extent_buffer *eb);void set_extent_buffer_uptodate (struct extent_buffer *eb);void clear_extent_buffer_uptodate (struct extent_buffer *eb);int extent_buffer_under_io (struct extent_buffer *eb); -int map_private_extent_buffer (const struct extent_buffer *eb, -unsigned long offset,unsigned long min_len, -char **map,unsigned long *map_start, -unsigned long *map_len);void extent_range_clear_dirty_for_io (struct inode *inode,u64 start,u64 end);void extent_range_redirty_for_io (struct inode *inode,u64 start,u64 end); void extent_clear_unlock_delalloc(struct inode *inode, u64 start, u64 end,
Signature · 1 line
2.25.0
Re: [PATCH 12/19] btrfs: remove unused map_private_extent_buffer
So long and thanks for all the fish ;-)
David Sterba
maintainerPATCH 13/19 btrfs: constify extent_buffer in the API functions
In reply to David SterbaThere are many helpers around extent buffers, found in extent_io.h and ctree.h. Most of them can be converted to take constified eb as there are no changes to the extent buffer structure itself but rather the pages.
--- fs/btrfs/ctree.h | 28 +++++++++++++------------- fs/btrfs/extent_io.c | 44 ++++++++++++++++++++++------------------- fs/btrfs/extent_io.h | 44 ++++++++++++++++++++++------------------- fs/btrfs/struct-funcs.c | 2 +- 4 files changed, 63 insertions(+), 55 deletions(-)
fs/btrfs/ctree.h +14 -14
@@ -1384,7 +1384,7 @@ void btrfs_set_token_##bits(struct btrfs_map_token *token, \ u##bits val); \ u##bits btrfs_get_##bits(const struct extent_buffer *eb, \ const void *ptr, unsigned long off); \ -void btrfs_set_##bits(struct extent_buffer *eb, void *ptr, \ +void btrfs_set_##bits(const struct extent_buffer *eb, void *ptr, \ unsigned long off, u##bits val);DECLARE_BTRFS_SETGET_BITS (8 )
@@ -1399,7 +1399,7 @@ static inline u##bits btrfs_##name(const struct extent_buffer *eb, \ BUILD_BUG_ON(sizeof(u##bits) != sizeof(((type *)0))->member); \ return btrfs_get_##bits(eb, s, offsetof(type, member)); \ } \ -static inline void btrfs_set_##name(struct extent_buffer *eb, type *s, \ +static inline void btrfs_set_##name(const struct extent_buffer *eb, type *s, \ u##bits val) \ { \ BUILD_BUG_ON(sizeof(u##bits) != sizeof(((type *)0))->member); \
@@ -1425,7 +1425,7 @@ static inline u##bits btrfs_##name(const struct extent_buffer *eb) \ u##bits res = le##bits##_to_cpu(p->member); \ return res; \ } \ -static inline void btrfs_set_##name(struct extent_buffer *eb, \ +static inline void btrfs_set_##name(const struct extent_buffer *eb, \ u##bits val) \ { \ type *p = page_address(eb->pages[0]); \
@@ -1443,7 +1443,7 @@ static inline void btrfs_set_##name(type *s, u##bits val) \ } -static inline u64 btrfs_device_total_bytes (struct extent_buffer *eb, +static inline u64 btrfs_device_total_bytes (const struct extent_buffer *eb,struct btrfs_dev_item *s) { BUILD_BUG_ON(sizeof(u64) !=
@@ -1451,7 +1451,7 @@ static inline u64 btrfs_device_total_bytes(struct extent_buffer *eb,return btrfs_get_64 (eb, s, offsetof(struct btrfs_dev_item , total_bytes)); } -static inline void btrfs_set_device_total_bytes (struct extent_buffer *eb, +static inline void btrfs_set_device_total_bytes (const struct extent_buffer *eb,struct btrfs_dev_item *s,u64 val) {
@@ -1555,13 +1555,13 @@ static inline char *btrfs_stripe_dev_uuid_nr(struct btrfs_chunk *c, int nr)return btrfs_stripe_dev_uuid (btrfs_stripe_nr (c, nr)); } -static inline u64 btrfs_stripe_offset_nr (struct extent_buffer *eb, +static inline u64 btrfs_stripe_offset_nr (const struct extent_buffer *eb,struct btrfs_chunk *c,int nr) {return btrfs_stripe_offset (eb,btrfs_stripe_nr (c, nr)); } -static inline u64 btrfs_stripe_devid_nr (struct extent_buffer *eb, +static inline u64 btrfs_stripe_devid_nr (const struct extent_buffer *eb,struct btrfs_chunk *c,int nr) {return btrfs_stripe_devid (eb,btrfs_stripe_nr (c, nr));
@@ -1658,14 +1658,14 @@ BTRFS_SETGET_FUNCS(extent_refs_v0, struct btrfs_extent_item_v0, refs, 32); BTRFS_SETGET_FUNCS(tree_block_level, struct btrfs_tree_block_info, level, 8); -static inline void btrfs_tree_block_key (struct extent_buffer *eb, +static inline void btrfs_tree_block_key (const struct extent_buffer *eb,struct btrfs_tree_block_info *item,struct btrfs_disk_key *key) { read_eb_member(eb, item, struct btrfs_tree_block_info, key, key); } -static inline void btrfs_set_tree_block_key (struct extent_buffer *eb, +static inline void btrfs_set_tree_block_key (const struct extent_buffer *eb,struct btrfs_tree_block_info *item,struct btrfs_disk_key *key) {
@@ -1717,7 +1717,7 @@ BTRFS_SETGET_STACK_FUNCS(stack_key_blockptr, struct btrfs_key_ptr, BTRFS_SETGET_STACK_FUNCS(stack_key_generation, struct btrfs_key_ptr, generation,64 ); -static inline u64 btrfs_node_blockptr (struct extent_buffer *eb,int nr) +static inline u64 btrfs_node_blockptr (const struct extent_buffer *eb,int nr) {unsigned long ptr; ptr = offsetof(struct btrfs_node, ptrs) +
@@ -1725,7 +1725,7 @@ static inline u64 btrfs_node_blockptr(struct extent_buffer *eb, int nr)return btrfs_key_blockptr (eb, (struct btrfs_key_ptr *)ptr); } -static inline void btrfs_set_node_blockptr (struct extent_buffer *eb, +static inline void btrfs_set_node_blockptr (const struct extent_buffer *eb,int nr,u64 val) {unsigned long ptr;
@@ -1734,7 +1734,7 @@ static inline void btrfs_set_node_blockptr(struct extent_buffer *eb,btrfs_set_key_blockptr (eb, (struct btrfs_key_ptr *)ptr, val); } -static inline u64 btrfs_node_ptr_generation (struct extent_buffer *eb,int nr) +static inline u64 btrfs_node_ptr_generation (const struct extent_buffer *eb,int nr) {unsigned long ptr; ptr = offsetof(struct btrfs_node, ptrs) +
@@ -1742,7 +1742,7 @@ static inline u64 btrfs_node_ptr_generation(struct extent_buffer *eb, int nr)return btrfs_key_generation (eb, (struct btrfs_key_ptr *)ptr); } -static inline void btrfs_set_node_ptr_generation (struct extent_buffer *eb, +static inline void btrfs_set_node_ptr_generation (const struct extent_buffer *eb,int nr,u64 val) {unsigned long ptr;
@@ -1760,7 +1760,7 @@ static inline unsigned long btrfs_node_key_ptr_offset(int nr)void btrfs_node_key (const struct extent_buffer *eb,struct btrfs_disk_key *disk_key,int nr); -static inline void btrfs_set_node_key (struct extent_buffer *eb, +static inline void btrfs_set_node_key (const struct extent_buffer *eb,struct btrfs_disk_key *disk_key,int nr) {unsigned long ptr;
fs/btrfs/extent_io.c +24 -20
140 lines not on this page · raw message
fs/btrfs/extent_io.h +24 -20
78 lines not on this page · raw message
fs/btrfs/struct-funcs.c +1 -1
@@ -131,7 +131,7 @@ void btrfs_set_token_##bits(struct btrfs_map_token *token, \ leres = cpu_to_le##bits(val); \ write_extent_buffer(token->eb, &leres, member_offset, size); \ } \ -void btrfs_set_##bits(struct extent_buffer *eb, void *ptr, \ +void btrfs_set_##bits(const struct extent_buffer *eb, void *ptr, \ unsigned long off, u##bits val) \ { \ const unsigned long member_offset = (unsigned long)ptr + off; \
Signature · 1 line
2.25.0
Re: [PATCH 13/19] btrfs: constify extent_buffer in the API functions
David Sterba
maintainerPATCH 14/19 btrfs: drop unnecessary offset_in_page in extent buffer helpers
In reply to David SterbaHelpers that iterate over extent buffer pages set up several variables, one of them is finding out offset of the extent buffer start within a page. Right now we have extent buffers aligned to page sizes so this is effectively storing zero. This makes the code harder the follow and can be simplified.
The same change is done in all the helpers:
* remove: size_t start_offset = offset_in_page(eb->start);
* simplify code using start_offset
--- fs/btrfs/extent_io.c | 51 ++++++++++++++++++-------------------------- 1 file changed, 21 insertions(+), 30 deletions(-)
fs/btrfs/extent_io.c +21 -30
@@ -5622,8 +5622,7 @@ void read_extent_buffer(const struct extent_buffer *eb, void *dstv,struct page *page;char *kaddr;char *dst = (char *)dstv; -size_t start_offset =offset_in_page (eb->start); -unsigned long i = (start_offset + start) >>PAGE_SHIFT ; +unsigned long i = start >>PAGE_SHIFT ;if (start + len > eb->len) { WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, wanted %lu %lu\n",
@@ -5632,7 +5631,7 @@ void read_extent_buffer(const struct extent_buffer *eb, void *dstv,return ; } - offset =offset_in_page (start_offset + start); + offset =offset_in_page (start);while (len >0 ) { page = eb->pages[i];
@@ -5657,14 +5656,13 @@ int read_extent_buffer_to_user(const struct extent_buffer *eb,struct page *page;char *kaddr;char __user *dst = (char __user *)dstv; -size_t start_offset =offset_in_page (eb->start); -unsigned long i = (start_offset + start) >>PAGE_SHIFT ; +unsigned long i = start >>PAGE_SHIFT ;int ret =0 ;WARN_ON (start > eb->len);WARN_ON (start + len > eb->start + eb->len); - offset =offset_in_page (start_offset + start); + offset =offset_in_page (start);while (len >0 ) { page = eb->pages[i];
@@ -5693,14 +5691,13 @@ int memcmp_extent_buffer(const struct extent_buffer *eb, const void *ptrv,struct page *page;char *kaddr;char *ptr = (char *)ptrv; -size_t start_offset =offset_in_page (eb->start); -unsigned long i = (start_offset + start) >>PAGE_SHIFT ; +unsigned long i = start >>PAGE_SHIFT ;int ret =0 ;WARN_ON (start > eb->len);WARN_ON (start + len > eb->start + eb->len); - offset =offset_in_page (start_offset + start); + offset =offset_in_page (start);while (len >0 ) { page = eb->pages[i];
@@ -5749,13 +5746,12 @@ void write_extent_buffer(const struct extent_buffer *eb, const void *srcv,struct page *page;char *kaddr;char *src = (char *)srcv; -size_t start_offset =offset_in_page (eb->start); -unsigned long i = (start_offset + start) >>PAGE_SHIFT ; +unsigned long i = start >>PAGE_SHIFT ;WARN_ON (start > eb->len);WARN_ON (start + len > eb->start + eb->len); - offset =offset_in_page (start_offset + start); + offset =offset_in_page (start);while (len >0 ) { page = eb->pages[i];
@@ -5779,13 +5775,12 @@ void memzero_extent_buffer(const struct extent_buffer *eb, unsigned long start,size_t offset;struct page *page;char *kaddr; -size_t start_offset =offset_in_page (eb->start); -unsigned long i = (start_offset + start) >>PAGE_SHIFT ; +unsigned long i = start >>PAGE_SHIFT ;WARN_ON (start > eb->len);WARN_ON (start + len > eb->start + eb->len); - offset =offset_in_page (start_offset + start); + offset =offset_in_page (start);while (len >0 ) { page = eb->pages[i];
@@ -5825,12 +5820,11 @@ void copy_extent_buffer(const struct extent_buffer *dst,size_t offset;struct page *page;char *kaddr; -size_t start_offset =offset_in_page (dst->start); -unsigned long i = (start_offset + dst_offset) >>PAGE_SHIFT ; +unsigned long i = dst_offset >>PAGE_SHIFT ;WARN_ON (src->len != dst_len); - offset =offset_in_page (start_offset + dst_offset); + offset =offset_in_page (dst_offset);while (len >0 ) { page = dst->pages[i];
@@ -5866,7 +5860,6 @@ static inline void eb_bitmap_offset(const struct extent_buffer *eb,unsigned long *page_index, size_t *page_offset) { - size_t start_offset = offset_in_page(eb->start); size_t byte_offset = BIT_BYTE(nr);size_t offset;
@@ -5875,7 +5868,7 @@ static inline void eb_bitmap_offset(const struct extent_buffer *eb,* the bitmap item in the extent buffer + the offset of the byte in the * bitmap item. */ - offset = start_offset + start + byte_offset; + offset = start + byte_offset; *page_index = offset >>PAGE_SHIFT ; *page_offset =offset_in_page (offset);
@@ -6022,7 +6015,6 @@ void memcpy_extent_buffer(const struct extent_buffer *dst,size_t cur;size_t dst_off_in_page;size_t src_off_in_page; -size_t start_offset =offset_in_page (dst->start);unsigned long dst_i;unsigned long src_i;
@@ -6040,11 +6032,11 @@ void memcpy_extent_buffer(const struct extent_buffer *dst, }while (len >0 ) { - dst_off_in_page =offset_in_page (start_offset + dst_offset); - src_off_in_page =offset_in_page (start_offset + src_offset); + dst_off_in_page =offset_in_page (dst_offset); + src_off_in_page =offset_in_page (src_offset); - dst_i = (start_offset + dst_offset) >>PAGE_SHIFT ; - src_i = (start_offset + src_offset) >>PAGE_SHIFT ; + dst_i = dst_offset >>PAGE_SHIFT ; + src_i = src_offset >>PAGE_SHIFT ; cur =min (len, (unsigned long )(PAGE_SIZE - src_off_in_page));
@@ -6070,7 +6062,6 @@ void memmove_extent_buffer(const struct extent_buffer *dst,size_t src_off_in_page;unsigned long dst_end = dst_offset + len -1 ;unsigned long src_end = src_offset + len -1 ; -size_t start_offset =offset_in_page (dst->start);unsigned long dst_i;unsigned long src_i;
@@ -6091,11 +6082,11 @@ void memmove_extent_buffer(const struct extent_buffer *dst,return ; }while (len >0 ) { - dst_i = (start_offset + dst_end) >>PAGE_SHIFT ; - src_i = (start_offset + src_end) >>PAGE_SHIFT ; + dst_i = dst_end >>PAGE_SHIFT ; + src_i = src_end >>PAGE_SHIFT ; - dst_off_in_page =offset_in_page (start_offset + dst_end); - src_off_in_page =offset_in_page (start_offset + src_end); + dst_off_in_page =offset_in_page (dst_end); + src_off_in_page =offset_in_page (src_end); cur = min_t(unsigned long, len, src_off_in_page + 1); cur =min (cur, dst_off_in_page +1 );
Signature · 1 line
2.25.0
Re: [PATCH 14/19] btrfs: drop unnecessary offset_in_page in extent buffer helpers
Looks good,
David Sterba
maintainerPATCH 15/19 btrfs: optimize split page read in btrfs_get_##bits
In reply to David SterbaThe helper read_extent_buffer is called to do read of the data spanning two extent buffer pages. As the size is known, we can do the read directly in two steps. This removes one function call and compiler can optimize memcpy as the sizes are known at compile time.
--- fs/btrfs/struct-funcs.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-)
fs/btrfs/struct-funcs.c +10 -7
@@ -90,17 +90,20 @@ u##bits btrfs_get_##bits(const struct extent_buffer *eb, \ { \const unsigned long member_offset = (unsigned long )ptr + off; \const unsigned long oip =offset_in_page (member_offset); \ +const unsigned long idx = member_offset >>PAGE_SHIFT ; \ +char *kaddr =page_address (eb->pages[idx]); \ const int size = sizeof(u##bits); \ - __le##bits leres; \ + const int part = PAGE_SIZE - oip; \ + u8 lebytes[sizeof(u##bits)]; \ \ ASSERT(check_setget_bounds(eb, ptr, off, size)); \ - if (oip + size <= PAGE_SIZE) { \ - const unsigned long idx = member_offset >> PAGE_SHIFT; \ - const char *kaddr = page_address(eb->pages[idx]); \ + if (oip + size <= PAGE_SIZE) \ return get_unaligned_le##bits(kaddr + oip); \ - } \ - read_extent_buffer(eb, &leres, member_offset, size); \ - return le##bits##_to_cpu(leres); \ + \ + memcpy(lebytes, kaddr + oip, part); \ + kaddr = page_address(eb->pages[idx + 1]); \ + memcpy(lebytes + part, kaddr, size - part); \ + return get_unaligned_le##bits(lebytes); \ } \ void btrfs_set_token_##bits(struct btrfs_map_token *token, \ const void *ptr, unsigned long off, \
Signature · 1 line
2.25.0
Re: [PATCH 15/19] btrfs: optimize split page read in btrfs_get_##bits
Looks good,
David Sterba
maintainerPATCH 16/19 btrfs: optimize split page read in btrfs_get_token_##bits
In reply to David SterbaThe fallback path calls helper read_extent_buffer to do read of the data spanning two extent buffer pages. As the size is known, we can do the read directly in two steps. This removes one function call and compiler can optimize memcpy as the sizes are known at compile time. The cached token address is set to the second page.
--- fs/btrfs/struct-funcs.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-)
fs/btrfs/struct-funcs.c +9 -7
@@ -66,7 +66,8 @@ u##bits btrfs_get_token_##bits(struct btrfs_map_token *token, \const unsigned long idx = member_offset >>PAGE_SHIFT ; \const unsigned long oip =offset_in_page (member_offset); \ const int size = sizeof(u##bits); \ - __le##bits leres; \ + u8 lebytes[sizeof(u##bits)]; \ + const int part = PAGE_SIZE - oip; \ \ ASSERT(token); \ ASSERT(token->kaddr); \
@@ -75,15 +76,16 @@ u##bits btrfs_get_token_##bits(struct btrfs_map_token *token, \ member_offset + size <= token->offset + PAGE_SIZE) { \ return get_unaligned_le##bits(token->kaddr + oip); \ } \ - if (oip + size <= PAGE_SIZE) { \ - token->kaddr = page_address(token->eb->pages[idx]); \ - token->offset = idx << PAGE_SHIFT; \ + token->kaddr = page_address(token->eb->pages[idx]); \ + token->offset = idx << PAGE_SHIFT; \ + if (oip + size <= PAGE_SIZE) \ return get_unaligned_le##bits(token->kaddr + oip); \ - } \ + \ + memcpy(lebytes, token->kaddr + oip, part); \ token->kaddr = page_address(token->eb->pages[idx + 1]); \ token->offset = (idx + 1) << PAGE_SHIFT; \ - read_extent_buffer(token->eb, &leres, member_offset, size); \ - return le##bits##_to_cpu(leres); \ + memcpy(lebytes + part, token->kaddr, size - part); \ + return get_unaligned_le##bits(lebytes); \ } \ u##bits btrfs_get_##bits(const struct extent_buffer *eb, \ const void *ptr, unsigned long off) \
Signature · 1 line
2.25.0
Re: [PATCH 16/19] btrfs: optimize split page read in btrfs_get_token_##bits
Looks good,
David Sterba
maintainerPATCH 17/19 btrfs: optimize split page write in btrfs_set_##bits
In reply to David SterbaThe helper write_extent_buffer is called to do write of the data spanning two extent buffer pages. As the size is known, we can do the write directly in two steps. This removes one function call and compiler can optimize memcpy as the sizes are known at compile time.
--- fs/btrfs/struct-funcs.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-)
fs/btrfs/struct-funcs.c +9 -5
@@ -141,18 +141,22 @@ void btrfs_set_##bits(const struct extent_buffer *eb, void *ptr, \ { \const unsigned long member_offset = (unsigned long )ptr + off; \const unsigned long oip =offset_in_page (member_offset); \ +const unsigned long idx = member_offset >>PAGE_SHIFT ; \ +char *kaddr =page_address (eb->pages[idx]); \ const int size = sizeof(u##bits); \ - __le##bits leres; \ + const int part = PAGE_SIZE - oip; \ + u8 lebytes[sizeof(u##bits)]; \ \ ASSERT(check_setget_bounds(eb, ptr, off, size)); \ if (oip + size <= PAGE_SIZE) { \ - const unsigned long idx = member_offset >> PAGE_SHIFT; \ - char *kaddr = page_address(eb->pages[idx]); \ put_unaligned_le##bits(val, kaddr + oip); \ return; \ } \ - leres = cpu_to_le##bits(val); \ - write_extent_buffer(eb, &leres, member_offset, size); \ + \ + put_unaligned_le##bits(val, lebytes); \ + memcpy(kaddr + oip, lebytes, part); \ + kaddr = page_address(eb->pages[idx + 1]); \ + memcpy(kaddr, lebytes + part, size - part); \ }DEFINE_BTRFS_SETGET_BITS (8 )
Signature · 1 line
2.25.0
Re: [PATCH 17/19] btrfs: optimize split page write in btrfs_set_##bits
Looks good,
David Sterba
maintainerPATCH 18/19 btrfs: optimize split page write in btrfs_set_token_##bits
In reply to David SterbaThe fallback path calls helper write_extent_buffer to do write of the data spanning two extent buffer pages. As the size is known, we can do the write directly in two steps. This removes one function call and compiler can optimize memcpy as the sizes are known at compile time. The cached token address is set to the second page.
--- fs/btrfs/struct-funcs.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-)
fs/btrfs/struct-funcs.c +7 -5
@@ -115,7 +115,8 @@ void btrfs_set_token_##bits(struct btrfs_map_token *token, \const unsigned long idx = member_offset >>PAGE_SHIFT ; \const unsigned long oip =offset_in_page (member_offset); \ const int size = sizeof(u##bits); \ - __le##bits leres; \ + u8 lebytes[sizeof(u##bits)]; \ + const int part = PAGE_SIZE - oip; \ \ ASSERT(token); \ ASSERT(token->kaddr); \
@@ -125,16 +126,17 @@ void btrfs_set_token_##bits(struct btrfs_map_token *token, \ put_unaligned_le##bits(val, token->kaddr + oip); \ return; \ } \ + token->kaddr = page_address(token->eb->pages[idx]); \ + token->offset = idx << PAGE_SHIFT; \ if (oip + size <= PAGE_SIZE) { \ - token->kaddr = page_address(token->eb->pages[idx]); \ - token->offset = idx << PAGE_SHIFT; \ put_unaligned_le##bits(val, token->kaddr + oip); \ return; \ } \ + put_unaligned_le##bits(val, lebytes); \ + memcpy(token->kaddr + oip, lebytes, part); \ token->kaddr = page_address(token->eb->pages[idx + 1]); \ token->offset = (idx + 1) << PAGE_SHIFT; \ - leres = cpu_to_le##bits(val); \ - write_extent_buffer(token->eb, &leres, member_offset, size); \ + memcpy(token->kaddr, lebytes + part, size - part); \ } \ void btrfs_set_##bits(const struct extent_buffer *eb, void *ptr, \ unsigned long off, u##bits val) \
Signature · 1 line
2.25.0
Re: [PATCH 18/19] btrfs: optimize split page write in btrfs_set_token_##bits
Looks good,
David Sterba
maintainerPATCH 19/19 btrfs: update documentation of set/get helpers
In reply to David Sterba--- fs/btrfs/struct-funcs.c | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-)
fs/btrfs/struct-funcs.c +16 -13
@@ -39,23 +39,26 @@ static bool check_setget_bounds(const struct extent_buffer *eb, }/* -* this is some deeply nasty code. +* Macro templates that define helpers to read/write extent buffer data of a +* given size, that are also used via ctree.h for access to item members via +* specialized helpers. * -* The end result is that anyone who #includes ctree.h gets a -* declaration for the btrfs_set_foo functions and btrfs_foo functions, -* which are wrappers of btrfs_set_token_#bits functions and -* btrfs_get_token_#bits functions, which are defined in this file. +* Generic helpers: +* - btrfs_set_8 (for 8/16/32/64) +* - btrfs_get_8 (for 8/16/32/64) * -* These setget functions do all the extent_buffer related mapping -* required to efficiently read and write specific fields in the extent -* buffers. Every pointer to metadata items in btrfs is really just -* an unsigned long offset into the extent buffer which has been -* cast to a specific type. This gives us all the gcc type checking. +* Generic helpes with a token, caching last page address: +* - btrfs_set_token_8 (for 8/16/32/64) +* - btrfs_get_token_8 (for 8/16/32/64) * -* The extent buffer api is used to do the page spanning work required to -* have a metadata blocksize different from the page size. +* The set/get functions handle data spanning two pages transparently, in case +* metadata block size is larger than page. Every pointer to metadata items is +* an offset into the extent buffer page array, cast to a specific type. This +* gives us all the type checking. * -* There are 2 variants defined, one with a token pointer and one without. +* The extent buffer pages stored in the array pages do not form a contiguous +* range, but the API functions assume the linear offset to the range from +* 0 to metadata node size. */ #define DEFINE_BTRFS_SETGET_BITS (bits) \
Signature · 1 line
2.25.0
Re: [PATCH 19/19] btrfs: update documentation of set/get helpers
Quoted 22 lines · David Sterba
Signed-off-by David Sterba <[email protected]>--- fs/btrfs/struct-funcs.c | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-)fs/btrfs/struct-funcs.c +3
-5@@ -39,23 +39,26 @@ static bool check_setget_bounds(const struct extent_buffer *eb, }/* -* this is some deeply nasty code. +* Macro templates that define helpers to read/write extent buffer data of a +* given size, that are also used via ctree.h for access to item members via +* specialized helpers. * -* The end result is that anyone who #includes ctree.h gets a -* declaration for the btrfs_set_foo functions and btrfs_foo functions, -* which are wrappers of btrfs_set_token_#bits functions and -* btrfs_get_token_#bits functions, which are defined in this file.
fs/btrfs/struct-funcs.c static bool check_setget_bounds(const struct extent_buffer *eb, · line 50
Quoted 10 lines
fs/btrfs/struct-funcs.c +4
-5+* Generic helpers: +* - btrfs_set_8 (for 8/16/32/64) +* - btrfs_get_8 (for 8/16/32/64) * -* These setget functions do all the extent_buffer related mapping -* required to efficiently read and write specific fields in the extent -* buffers. Every pointer to metadata items in btrfs is really just -* an unsigned long offset into the extent buffer which has been -* cast to a specific type. This gives us all the gcc type checking. +* Generic helpes with a token, caching last page address:
nit: missing 'r' in 'helpers'. Without having looked into the code It's not obvious what a "token" is in this context, is it worth it perhaps documenting? ( I will take a look later and see if it's self-evident).
fs/btrfs/struct-funcs.c static bool check_setget_bounds(const struct extent_buffer *eb, · line 55
Quoted 7 lines
fs/btrfs/struct-funcs.c +4
-2+* - btrfs_set_token_8 (for 8/16/32/64) +* - btrfs_get_token_8 (for 8/16/32/64) * -* The extent buffer api is used to do the page spanning work required to -* have a metadata blocksize different from the page size. +* The set/get functions handle data spanning two pages transparently, in case +* metadata block size is larger than page. Every pointer to metadata items is
^^^^^
nit: s/metadata/btree/?
fs/btrfs/struct-funcs.c static bool check_setget_bounds(const struct extent_buffer *eb, · line 60
Quoted 6 lines
fs/btrfs/struct-funcs.c +4
-1+* an offset into the extent buffer page array, cast to a specific type. This +* gives us all the type checking. * -* There are 2 variants defined, one with a token pointer and one without. +* The extent buffer pages stored in the array pages do not form a contiguous +* range, but the API functions assume the linear offset to the range from
nit: "contiguous physical range"
Quoted 5 lines
fs/btrfs/struct-funcs.c +1
-0+* 0 to metadata node size. */ #define DEFINE_BTRFS_SETGET_BITS (bits) \
Re: [PATCH 19/19] btrfs: update documentation of set/get helpers
Quoted 29 lines · Nikolay Borisov
Quoted 28 lines · David Sterba
Signed-off-by David Sterba <[email protected]>--- fs/btrfs/struct-funcs.c | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-)fs/btrfs/struct-funcs.c +6
-7@@ -39,23 +39,26 @@ static bool check_setget_bounds(const struct extent_buffer *eb, }/* -* this is some deeply nasty code. +* Macro templates that define helpers to read/write extent buffer data of a +* given size, that are also used via ctree.h for access to item members via +* specialized helpers. * -* The end result is that anyone who #includes ctree.h gets a -* declaration for the btrfs_set_foo functions and btrfs_foo functions, -* which are wrappers of btrfs_set_token_#bits functions and -* btrfs_get_token_#bits functions, which are defined in this file. +* Generic helpers: +* - btrfs_set_8 (for 8/16/32/64) +* - btrfs_get_8 (for 8/16/32/64) * -* These setget functions do all the extent_buffer related mapping -* required to efficiently read and write specific fields in the extent
Quoted 8 lines
fs/btrfs/struct-funcs.c static bool check_setget_bounds(const struct extent_buffer *eb, · line 50
Quoted 4 lines
fs/btrfs/struct-funcs.c +1
-3-* buffers. Every pointer to metadata items in btrfs is really just -* an unsigned long offset into the extent buffer which has been -* cast to a specific type. This gives us all the gcc type checking. +* Generic helpes with a token, caching last page address: nit: missing 'r' in 'helpers'. Without having looked into the code It's not obvious what a "token" is in this context, is it worth it perhaps documenting? ( I will take a look later and see if it's self-evident).
I could write it as
"Generic helpers with a token (a structure caching the address of most recently accessed page)"
The use of 'last' is confusing as it's not the last as in the array.
Quoted 9 lines
fs/btrfs/struct-funcs.c static bool check_setget_bounds(const struct extent_buffer *eb, · line 55
Quoted 7 lines
fs/btrfs/struct-funcs.c +4
-2+* - btrfs_set_token_8 (for 8/16/32/64) +* - btrfs_get_token_8 (for 8/16/32/64) * -* The extent buffer api is used to do the page spanning work required to -* have a metadata blocksize different from the page size. +* The set/get functions handle data spanning two pages transparently, in case +* metadata block size is larger than page. Every pointer to metadata items is ^^^^^nit: s/metadata/btree/?
The terms should be interchangeable, but in the previous sentence it's 'metadata' and this one continues, so I wonder how would 'btree' fit here.
All the structures here are on the higher level, so metadata etc, while b-tree node is the storage.
Quoted 8 lines
fs/btrfs/struct-funcs.c static bool check_setget_bounds(const struct extent_buffer *eb, · line 60
Quoted 6 lines
fs/btrfs/struct-funcs.c +4
-1+* an offset into the extent buffer page array, cast to a specific type. This +* gives us all the type checking. * -* There are 2 variants defined, one with a token pointer and one without. +* The extent buffer pages stored in the array pages do not form a contiguous +* range, but the API functions assume the linear offset to the range from nit: "contiguous physical range"
Ok.
Quoted 5 lines
Quoted 5 lines
fs/btrfs/struct-funcs.c +1
-0+* 0 to metadata node size. */ #define DEFINE_BTRFS_SETGET_BITS (bits) \
Re: [PATCH 19/19] btrfs: update documentation of set/get helpers
Quoted 39 lines · David Sterba
Quoted 29 lines · Nikolay Borisov
Quoted 28 lines · David Sterba
Signed-off-by David Sterba <[email protected]>--- fs/btrfs/struct-funcs.c | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-)fs/btrfs/struct-funcs.c +6
-7@@ -39,23 +39,26 @@ static bool check_setget_bounds(const struct extent_buffer *eb, }/* -* this is some deeply nasty code. +* Macro templates that define helpers to read/write extent buffer data of a +* given size, that are also used via ctree.h for access to item members via +* specialized helpers. * -* The end result is that anyone who #includes ctree.h gets a -* declaration for the btrfs_set_foo functions and btrfs_foo functions, -* which are wrappers of btrfs_set_token_#bits functions and -* btrfs_get_token_#bits functions, which are defined in this file. +* Generic helpers: +* - btrfs_set_8 (for 8/16/32/64) +* - btrfs_get_8 (for 8/16/32/64) * -* These setget functions do all the extent_buffer related mapping -* required to efficiently read and write specific fields in the extent Quoted 8 lines
fs/btrfs/struct-funcs.c static bool check_setget_bounds(const struct extent_buffer *eb, · line 50
Quoted 4 lines
fs/btrfs/struct-funcs.c +1
-3-* buffers. Every pointer to metadata items in btrfs is really just -* an unsigned long offset into the extent buffer which has been -* cast to a specific type. This gives us all the gcc type checking. +* Generic helpes with a token, caching last page address: nit: missing 'r' in 'helpers'. Without having looked into the code It's not obvious what a "token" is in this context, is it worth it perhaps documenting? ( I will take a look later and see if it's self-evident).
Quoted 4 lines
I could write it as
"Generic helpers with a token (a structure caching the address of most recently accessed page)"
Much better.
Quoted 10 lines
The use of 'last' is confusing as it's not the last as in the array.
Quoted 7 lines
Quoted 7 lines
fs/btrfs/struct-funcs.c +4
-2+* - btrfs_set_token_8 (for 8/16/32/64) +* - btrfs_get_token_8 (for 8/16/32/64) * -* The extent buffer api is used to do the page spanning work required to -* have a metadata blocksize different from the page size. +* The set/get functions handle data spanning two pages transparently, in case +* metadata block size is larger than page. Every pointer to metadata items is
Quoted 8 lines
Quoted 2 lines
^^^^^nit: s/metadata/btree/?
The terms should be interchangeable, but in the previous sentence it's 'metadata' and this one continues, so I wonder how would 'btree' fit here.
All the structures here are on the higher level, so metadata etc, while b-tree node is the storage.
Fair enough, I reread the section and it's ok.
Quoted 17 lines
Quoted 9 lines
fs/btrfs/struct-funcs.c static bool check_setget_bounds(const struct extent_buffer *eb, · line 60
Quoted 7 lines
fs/btrfs/struct-funcs.c +4
-1+* an offset into the extent buffer page array, cast to a specific type. This +* gives us all the type checking. * -* There are 2 variants defined, one with a token pointer and one without. +* The extent buffer pages stored in the array pages do not form a contiguous +* range, but the API functions assume the linear offset to the range from nit: "contiguous physical range"
Ok.
Quoted 5 lines
Quoted 5 lines
fs/btrfs/struct-funcs.c +1
-0+* 0 to metadata node size. */ #define DEFINE_BTRFS_SETGET_BITS (bits) \