Commit c82f823c authored by David Sterba's avatar David Sterba

btrfs: tie extent buffer and it's token together

Further simplifaction of the get/set helpers is possible when the token
is uniquely tied to an extent buffer. A condition and an assignment can
be avoided.

The initializations are moved closer to the first use when the extent
buffer is valid. There's one exception in __push_leaf_left where the
token is reused.
Signed-off-by: default avatarDavid Sterba <dsterba@suse.com>
parent 48bc3950
...@@ -3574,7 +3574,7 @@ static int leaf_space_used(struct extent_buffer *l, int start, int nr) ...@@ -3574,7 +3574,7 @@ static int leaf_space_used(struct extent_buffer *l, int start, int nr)
if (!nr) if (!nr)
return 0; return 0;
btrfs_init_map_token(&token); btrfs_init_map_token(&token, l);
start_item = btrfs_item_nr(start); start_item = btrfs_item_nr(start);
end_item = btrfs_item_nr(end); end_item = btrfs_item_nr(end);
data_len = btrfs_token_item_offset(l, start_item, &token) + data_len = btrfs_token_item_offset(l, start_item, &token) +
...@@ -3632,8 +3632,6 @@ static noinline int __push_leaf_right(struct btrfs_path *path, ...@@ -3632,8 +3632,6 @@ static noinline int __push_leaf_right(struct btrfs_path *path,
u32 data_end; u32 data_end;
u32 this_item_size; u32 this_item_size;
btrfs_init_map_token(&token);
if (empty) if (empty)
nr = 0; nr = 0;
else else
...@@ -3706,6 +3704,7 @@ static noinline int __push_leaf_right(struct btrfs_path *path, ...@@ -3706,6 +3704,7 @@ static noinline int __push_leaf_right(struct btrfs_path *path,
push_items * sizeof(struct btrfs_item)); push_items * sizeof(struct btrfs_item));
/* update the item pointers */ /* update the item pointers */
btrfs_init_map_token(&token, right);
right_nritems += push_items; right_nritems += push_items;
btrfs_set_header_nritems(right, right_nritems); btrfs_set_header_nritems(right, right_nritems);
push_space = BTRFS_LEAF_DATA_SIZE(fs_info); push_space = BTRFS_LEAF_DATA_SIZE(fs_info);
...@@ -3860,8 +3859,6 @@ static noinline int __push_leaf_left(struct btrfs_path *path, int data_size, ...@@ -3860,8 +3859,6 @@ static noinline int __push_leaf_left(struct btrfs_path *path, int data_size,
u32 old_left_item_size; u32 old_left_item_size;
struct btrfs_map_token token; struct btrfs_map_token token;
btrfs_init_map_token(&token);
if (empty) if (empty)
nr = min(right_nritems, max_slot); nr = min(right_nritems, max_slot);
else else
...@@ -3915,6 +3912,7 @@ static noinline int __push_leaf_left(struct btrfs_path *path, int data_size, ...@@ -3915,6 +3912,7 @@ static noinline int __push_leaf_left(struct btrfs_path *path, int data_size,
old_left_nritems = btrfs_header_nritems(left); old_left_nritems = btrfs_header_nritems(left);
BUG_ON(old_left_nritems <= 0); BUG_ON(old_left_nritems <= 0);
btrfs_init_map_token(&token, left);
old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1); old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
for (i = old_left_nritems; i < old_left_nritems + push_items; i++) { for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
u32 ioff; u32 ioff;
...@@ -3946,6 +3944,8 @@ static noinline int __push_leaf_left(struct btrfs_path *path, int data_size, ...@@ -3946,6 +3944,8 @@ static noinline int __push_leaf_left(struct btrfs_path *path, int data_size,
(btrfs_header_nritems(right) - push_items) * (btrfs_header_nritems(right) - push_items) *
sizeof(struct btrfs_item)); sizeof(struct btrfs_item));
} }
btrfs_init_map_token(&token, right);
right_nritems -= push_items; right_nritems -= push_items;
btrfs_set_header_nritems(right, right_nritems); btrfs_set_header_nritems(right, right_nritems);
push_space = BTRFS_LEAF_DATA_SIZE(fs_info); push_space = BTRFS_LEAF_DATA_SIZE(fs_info);
...@@ -4076,8 +4076,6 @@ static noinline void copy_for_split(struct btrfs_trans_handle *trans, ...@@ -4076,8 +4076,6 @@ static noinline void copy_for_split(struct btrfs_trans_handle *trans,
struct btrfs_disk_key disk_key; struct btrfs_disk_key disk_key;
struct btrfs_map_token token; struct btrfs_map_token token;
btrfs_init_map_token(&token);
nritems = nritems - mid; nritems = nritems - mid;
btrfs_set_header_nritems(right, nritems); btrfs_set_header_nritems(right, nritems);
data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(l); data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(l);
...@@ -4093,6 +4091,7 @@ static noinline void copy_for_split(struct btrfs_trans_handle *trans, ...@@ -4093,6 +4091,7 @@ static noinline void copy_for_split(struct btrfs_trans_handle *trans,
rt_data_off = BTRFS_LEAF_DATA_SIZE(fs_info) - btrfs_item_end_nr(l, mid); rt_data_off = BTRFS_LEAF_DATA_SIZE(fs_info) - btrfs_item_end_nr(l, mid);
btrfs_init_map_token(&token, right);
for (i = 0; i < nritems; i++) { for (i = 0; i < nritems; i++) {
struct btrfs_item *item = btrfs_item_nr(i); struct btrfs_item *item = btrfs_item_nr(i);
u32 ioff; u32 ioff;
...@@ -4576,8 +4575,6 @@ void btrfs_truncate_item(struct btrfs_path *path, u32 new_size, int from_end) ...@@ -4576,8 +4575,6 @@ void btrfs_truncate_item(struct btrfs_path *path, u32 new_size, int from_end)
int i; int i;
struct btrfs_map_token token; struct btrfs_map_token token;
btrfs_init_map_token(&token);
leaf = path->nodes[0]; leaf = path->nodes[0];
slot = path->slots[0]; slot = path->slots[0];
...@@ -4599,6 +4596,7 @@ void btrfs_truncate_item(struct btrfs_path *path, u32 new_size, int from_end) ...@@ -4599,6 +4596,7 @@ void btrfs_truncate_item(struct btrfs_path *path, u32 new_size, int from_end)
* item0..itemN ... dataN.offset..dataN.size .. data0.size * item0..itemN ... dataN.offset..dataN.size .. data0.size
*/ */
/* first correct the data pointers */ /* first correct the data pointers */
btrfs_init_map_token(&token, leaf);
for (i = slot; i < nritems; i++) { for (i = slot; i < nritems; i++) {
u32 ioff; u32 ioff;
item = btrfs_item_nr(i); item = btrfs_item_nr(i);
...@@ -4673,8 +4671,6 @@ void btrfs_extend_item(struct btrfs_path *path, u32 data_size) ...@@ -4673,8 +4671,6 @@ void btrfs_extend_item(struct btrfs_path *path, u32 data_size)
int i; int i;
struct btrfs_map_token token; struct btrfs_map_token token;
btrfs_init_map_token(&token);
leaf = path->nodes[0]; leaf = path->nodes[0];
nritems = btrfs_header_nritems(leaf); nritems = btrfs_header_nritems(leaf);
...@@ -4699,6 +4695,7 @@ void btrfs_extend_item(struct btrfs_path *path, u32 data_size) ...@@ -4699,6 +4695,7 @@ void btrfs_extend_item(struct btrfs_path *path, u32 data_size)
* item0..itemN ... dataN.offset..dataN.size .. data0.size * item0..itemN ... dataN.offset..dataN.size .. data0.size
*/ */
/* first correct the data pointers */ /* first correct the data pointers */
btrfs_init_map_token(&token, leaf);
for (i = slot; i < nritems; i++) { for (i = slot; i < nritems; i++) {
u32 ioff; u32 ioff;
item = btrfs_item_nr(i); item = btrfs_item_nr(i);
...@@ -4750,8 +4747,6 @@ void setup_items_for_insert(struct btrfs_root *root, struct btrfs_path *path, ...@@ -4750,8 +4747,6 @@ void setup_items_for_insert(struct btrfs_root *root, struct btrfs_path *path,
} }
btrfs_unlock_up_safe(path, 1); btrfs_unlock_up_safe(path, 1);
btrfs_init_map_token(&token);
leaf = path->nodes[0]; leaf = path->nodes[0];
slot = path->slots[0]; slot = path->slots[0];
...@@ -4765,6 +4760,7 @@ void setup_items_for_insert(struct btrfs_root *root, struct btrfs_path *path, ...@@ -4765,6 +4760,7 @@ void setup_items_for_insert(struct btrfs_root *root, struct btrfs_path *path,
BUG(); BUG();
} }
btrfs_init_map_token(&token, leaf);
if (slot != nritems) { if (slot != nritems) {
unsigned int old_data = btrfs_item_end_nr(leaf, slot); unsigned int old_data = btrfs_item_end_nr(leaf, slot);
...@@ -4971,9 +4967,6 @@ int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root, ...@@ -4971,9 +4967,6 @@ int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
int wret; int wret;
int i; int i;
u32 nritems; u32 nritems;
struct btrfs_map_token token;
btrfs_init_map_token(&token);
leaf = path->nodes[0]; leaf = path->nodes[0];
last_off = btrfs_item_offset_nr(leaf, slot + nr - 1); last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
...@@ -4985,12 +4978,14 @@ int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root, ...@@ -4985,12 +4978,14 @@ int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
if (slot + nr != nritems) { if (slot + nr != nritems) {
int data_end = leaf_data_end(leaf); int data_end = leaf_data_end(leaf);
struct btrfs_map_token token;
memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET + memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
data_end + dsize, data_end + dsize,
BTRFS_LEAF_DATA_OFFSET + data_end, BTRFS_LEAF_DATA_OFFSET + data_end,
last_off - data_end); last_off - data_end);
btrfs_init_map_token(&token, leaf);
for (i = slot + nr; i < nritems; i++) { for (i = slot + nr; i < nritems; i++) {
u32 ioff; u32 ioff;
......
...@@ -1303,8 +1303,10 @@ struct btrfs_map_token { ...@@ -1303,8 +1303,10 @@ struct btrfs_map_token {
#define BTRFS_BYTES_TO_BLKS(fs_info, bytes) \ #define BTRFS_BYTES_TO_BLKS(fs_info, bytes) \
((bytes) >> (fs_info)->sb->s_blocksize_bits) ((bytes) >> (fs_info)->sb->s_blocksize_bits)
static inline void btrfs_init_map_token (struct btrfs_map_token *token) static inline void btrfs_init_map_token(struct btrfs_map_token *token,
struct extent_buffer *eb)
{ {
token->eb = eb;
token->kaddr = NULL; token->kaddr = NULL;
} }
......
...@@ -3890,7 +3890,7 @@ static void fill_inode_item(struct btrfs_trans_handle *trans, ...@@ -3890,7 +3890,7 @@ static void fill_inode_item(struct btrfs_trans_handle *trans,
{ {
struct btrfs_map_token token; struct btrfs_map_token token;
btrfs_init_map_token(&token); btrfs_init_map_token(&token, leaf);
btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token); btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token); btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
......
...@@ -53,9 +53,9 @@ u##bits btrfs_get_token_##bits(const struct extent_buffer *eb, \ ...@@ -53,9 +53,9 @@ u##bits btrfs_get_token_##bits(const struct extent_buffer *eb, \
u##bits res; \ u##bits res; \
\ \
ASSERT(token); \ ASSERT(token); \
ASSERT(token->eb == eb); \
\ \
if (token->kaddr && token->offset <= offset && \ if (token->kaddr && token->offset <= offset && \
token->eb == eb && \
(token->offset + PAGE_SIZE >= offset + size)) { \ (token->offset + PAGE_SIZE >= offset + size)) { \
kaddr = token->kaddr; \ kaddr = token->kaddr; \
p = kaddr + part_offset - token->offset; \ p = kaddr + part_offset - token->offset; \
...@@ -74,7 +74,6 @@ u##bits btrfs_get_token_##bits(const struct extent_buffer *eb, \ ...@@ -74,7 +74,6 @@ u##bits btrfs_get_token_##bits(const struct extent_buffer *eb, \
res = get_unaligned_le##bits(p + off); \ res = get_unaligned_le##bits(p + off); \
token->kaddr = kaddr; \ token->kaddr = kaddr; \
token->offset = map_start; \ token->offset = map_start; \
token->eb = eb; \
return res; \ return res; \
} \ } \
u##bits btrfs_get_##bits(const struct extent_buffer *eb, \ u##bits btrfs_get_##bits(const struct extent_buffer *eb, \
...@@ -117,9 +116,9 @@ void btrfs_set_token_##bits(struct extent_buffer *eb, \ ...@@ -117,9 +116,9 @@ void btrfs_set_token_##bits(struct extent_buffer *eb, \
int size = sizeof(u##bits); \ int size = sizeof(u##bits); \
\ \
ASSERT(token); \ ASSERT(token); \
ASSERT(token->eb == eb); \
\ \
if (token->kaddr && token->offset <= offset && \ if (token->kaddr && token->offset <= offset && \
token->eb == eb && \
(token->offset + PAGE_SIZE >= offset + size)) { \ (token->offset + PAGE_SIZE >= offset + size)) { \
kaddr = token->kaddr; \ kaddr = token->kaddr; \
p = kaddr + part_offset - token->offset; \ p = kaddr + part_offset - token->offset; \
...@@ -139,7 +138,6 @@ void btrfs_set_token_##bits(struct extent_buffer *eb, \ ...@@ -139,7 +138,6 @@ void btrfs_set_token_##bits(struct extent_buffer *eb, \
put_unaligned_le##bits(val, p + off); \ put_unaligned_le##bits(val, p + off); \
token->kaddr = kaddr; \ token->kaddr = kaddr; \
token->offset = map_start; \ token->offset = map_start; \
token->eb = eb; \
} \ } \
void btrfs_set_##bits(struct extent_buffer *eb, void *ptr, \ void btrfs_set_##bits(struct extent_buffer *eb, void *ptr, \
unsigned long off, u##bits val) \ unsigned long off, u##bits val) \
......
...@@ -506,7 +506,7 @@ static noinline int overwrite_item(struct btrfs_trans_handle *trans, ...@@ -506,7 +506,7 @@ static noinline int overwrite_item(struct btrfs_trans_handle *trans,
ino_size != 0) { ino_size != 0) {
struct btrfs_map_token token; struct btrfs_map_token token;
btrfs_init_map_token(&token); btrfs_init_map_token(&token, dst_eb);
btrfs_set_token_inode_size(dst_eb, dst_item, btrfs_set_token_inode_size(dst_eb, dst_item,
ino_size, &token); ino_size, &token);
} }
...@@ -3842,7 +3842,7 @@ static void fill_inode_item(struct btrfs_trans_handle *trans, ...@@ -3842,7 +3842,7 @@ static void fill_inode_item(struct btrfs_trans_handle *trans,
{ {
struct btrfs_map_token token; struct btrfs_map_token token;
btrfs_init_map_token(&token); btrfs_init_map_token(&token, leaf);
if (log_inode_only) { if (log_inode_only) {
/* set the generation to zero so the recover code /* set the generation to zero so the recover code
...@@ -4302,8 +4302,6 @@ static int log_one_extent(struct btrfs_trans_handle *trans, ...@@ -4302,8 +4302,6 @@ static int log_one_extent(struct btrfs_trans_handle *trans,
if (ret) if (ret)
return ret; return ret;
btrfs_init_map_token(&token);
ret = __btrfs_drop_extents(trans, log, &inode->vfs_inode, path, em->start, ret = __btrfs_drop_extents(trans, log, &inode->vfs_inode, path, em->start,
em->start + em->len, NULL, 0, 1, em->start + em->len, NULL, 0, 1,
sizeof(*fi), &extent_inserted); sizeof(*fi), &extent_inserted);
...@@ -4321,6 +4319,7 @@ static int log_one_extent(struct btrfs_trans_handle *trans, ...@@ -4321,6 +4319,7 @@ static int log_one_extent(struct btrfs_trans_handle *trans,
return ret; return ret;
} }
leaf = path->nodes[0]; leaf = path->nodes[0];
btrfs_init_map_token(&token, leaf);
fi = btrfs_item_ptr(leaf, path->slots[0], fi = btrfs_item_ptr(leaf, path->slots[0],
struct btrfs_file_extent_item); struct btrfs_file_extent_item);
......
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