Commit 227f3cd0 authored by Josef Bacik's avatar Josef Bacik Committed by David Sterba

btrfs: use btrfs_item_size_nr/btrfs_item_offset_nr everywhere

We have this pattern in a lot of places

	item = btrfs_item_nr(slot);
	btrfs_item_size(leaf, item);

when we could simply use

	btrfs_item_size(leaf, slot);

Fix all callers of btrfs_item_size() and btrfs_item_offset() to use the
_nr variation of the helpers.
Reviewed-by: default avatarQu Wenruo <wqu@suse.com>
Signed-off-by: default avatarJosef Bacik <josef@toxicpanda.com>
Reviewed-by: default avatarDavid Sterba <dsterba@suse.com>
Signed-off-by: default avatarDavid Sterba <dsterba@suse.com>
parent ccae4a19
...@@ -2058,7 +2058,6 @@ static int iterate_inode_refs(u64 inum, struct btrfs_root *fs_root, ...@@ -2058,7 +2058,6 @@ static int iterate_inode_refs(u64 inum, struct btrfs_root *fs_root,
u64 parent = 0; u64 parent = 0;
int found = 0; int found = 0;
struct extent_buffer *eb; struct extent_buffer *eb;
struct btrfs_item *item;
struct btrfs_inode_ref *iref; struct btrfs_inode_ref *iref;
struct btrfs_key found_key; struct btrfs_key found_key;
...@@ -2084,10 +2083,9 @@ static int iterate_inode_refs(u64 inum, struct btrfs_root *fs_root, ...@@ -2084,10 +2083,9 @@ static int iterate_inode_refs(u64 inum, struct btrfs_root *fs_root,
} }
btrfs_release_path(path); btrfs_release_path(path);
item = btrfs_item_nr(slot);
iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref); iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref);
for (cur = 0; cur < btrfs_item_size(eb, item); cur += len) { for (cur = 0; cur < btrfs_item_size_nr(eb, slot); cur += len) {
name_len = btrfs_inode_ref_name_len(eb, iref); name_len = btrfs_inode_ref_name_len(eb, iref);
/* path must be released before calling iterate()! */ /* path must be released before calling iterate()! */
btrfs_debug(fs_root->fs_info, btrfs_debug(fs_root->fs_info,
......
...@@ -2616,19 +2616,14 @@ static noinline int split_node(struct btrfs_trans_handle *trans, ...@@ -2616,19 +2616,14 @@ static noinline int split_node(struct btrfs_trans_handle *trans,
*/ */
static int leaf_space_used(struct extent_buffer *l, int start, int nr) static int leaf_space_used(struct extent_buffer *l, int start, int nr)
{ {
struct btrfs_item *start_item;
struct btrfs_item *end_item;
int data_len; int data_len;
int nritems = btrfs_header_nritems(l); int nritems = btrfs_header_nritems(l);
int end = min(nritems, start + nr) - 1; int end = min(nritems, start + nr) - 1;
if (!nr) if (!nr)
return 0; return 0;
start_item = btrfs_item_nr(start); data_len = btrfs_item_offset_nr(l, start) + btrfs_item_size_nr(l, start);
end_item = btrfs_item_nr(end); data_len = data_len - btrfs_item_offset_nr(l, end);
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; data_len += sizeof(struct btrfs_item) * nr;
WARN_ON(data_len < 0); WARN_ON(data_len < 0);
return data_len; return data_len;
...@@ -2692,8 +2687,6 @@ static noinline int __push_leaf_right(struct btrfs_path *path, ...@@ -2692,8 +2687,6 @@ static noinline int __push_leaf_right(struct btrfs_path *path,
slot = path->slots[1]; slot = path->slots[1];
i = left_nritems - 1; i = left_nritems - 1;
while (i >= nr) { while (i >= nr) {
item = btrfs_item_nr(i);
if (!empty && push_items > 0) { if (!empty && push_items > 0) {
if (path->slots[0] > i) if (path->slots[0] > i)
break; break;
...@@ -2708,7 +2701,7 @@ static noinline int __push_leaf_right(struct btrfs_path *path, ...@@ -2708,7 +2701,7 @@ static noinline int __push_leaf_right(struct btrfs_path *path,
if (path->slots[0] == i) if (path->slots[0] == i)
push_space += data_size; push_space += data_size;
this_item_size = btrfs_item_size(left, item); this_item_size = btrfs_item_size_nr(left, i);
if (this_item_size + sizeof(*item) + push_space > free_space) if (this_item_size + sizeof(*item) + push_space > free_space)
break; break;
...@@ -2919,8 +2912,6 @@ static noinline int __push_leaf_left(struct btrfs_path *path, int data_size, ...@@ -2919,8 +2912,6 @@ static noinline int __push_leaf_left(struct btrfs_path *path, int data_size,
nr = min(right_nritems - 1, max_slot); nr = min(right_nritems - 1, max_slot);
for (i = 0; i < nr; i++) { for (i = 0; i < nr; i++) {
item = btrfs_item_nr(i);
if (!empty && push_items > 0) { if (!empty && push_items > 0) {
if (path->slots[0] < i) if (path->slots[0] < i)
break; break;
...@@ -2935,7 +2926,7 @@ static noinline int __push_leaf_left(struct btrfs_path *path, int data_size, ...@@ -2935,7 +2926,7 @@ static noinline int __push_leaf_left(struct btrfs_path *path, int data_size,
if (path->slots[0] == i) if (path->slots[0] == i)
push_space += data_size; push_space += data_size;
this_item_size = btrfs_item_size(right, item); this_item_size = btrfs_item_size_nr(right, i);
if (this_item_size + sizeof(*item) + push_space > free_space) if (this_item_size + sizeof(*item) + push_space > free_space)
break; break;
...@@ -3502,8 +3493,8 @@ static noinline int split_item(struct btrfs_path *path, ...@@ -3502,8 +3493,8 @@ static noinline int split_item(struct btrfs_path *path,
BUG_ON(btrfs_leaf_free_space(leaf) < sizeof(struct btrfs_item)); BUG_ON(btrfs_leaf_free_space(leaf) < sizeof(struct btrfs_item));
item = btrfs_item_nr(path->slots[0]); item = btrfs_item_nr(path->slots[0]);
orig_offset = btrfs_item_offset(leaf, item); orig_offset = btrfs_item_offset_nr(leaf, path->slots[0]);
item_size = btrfs_item_size(leaf, item); item_size = btrfs_item_size_nr(leaf, path->slots[0]);
buf = kmalloc(item_size, GFP_NOFS); buf = kmalloc(item_size, GFP_NOFS);
if (!buf) if (!buf)
......
...@@ -27,7 +27,6 @@ static struct btrfs_dir_item *insert_with_overflow(struct btrfs_trans_handle ...@@ -27,7 +27,6 @@ static struct btrfs_dir_item *insert_with_overflow(struct btrfs_trans_handle
struct btrfs_fs_info *fs_info = root->fs_info; struct btrfs_fs_info *fs_info = root->fs_info;
int ret; int ret;
char *ptr; char *ptr;
struct btrfs_item *item;
struct extent_buffer *leaf; struct extent_buffer *leaf;
ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size); ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
...@@ -41,10 +40,9 @@ static struct btrfs_dir_item *insert_with_overflow(struct btrfs_trans_handle ...@@ -41,10 +40,9 @@ static struct btrfs_dir_item *insert_with_overflow(struct btrfs_trans_handle
return ERR_PTR(ret); return ERR_PTR(ret);
WARN_ON(ret > 0); WARN_ON(ret > 0);
leaf = path->nodes[0]; leaf = path->nodes[0];
item = btrfs_item_nr(path->slots[0]);
ptr = btrfs_item_ptr(leaf, path->slots[0], char); ptr = btrfs_item_ptr(leaf, path->slots[0], char);
BUG_ON(data_size > btrfs_item_size(leaf, item)); ASSERT(data_size <= btrfs_item_size_nr(leaf, path->slots[0]));
ptr += btrfs_item_size(leaf, item) - data_size; ptr += btrfs_item_size_nr(leaf, path->slots[0]) - data_size;
return (struct btrfs_dir_item *)ptr; return (struct btrfs_dir_item *)ptr;
} }
......
...@@ -256,7 +256,6 @@ static int btrfs_insert_inode_extref(struct btrfs_trans_handle *trans, ...@@ -256,7 +256,6 @@ static int btrfs_insert_inode_extref(struct btrfs_trans_handle *trans,
struct btrfs_path *path; struct btrfs_path *path;
struct btrfs_key key; struct btrfs_key key;
struct extent_buffer *leaf; struct extent_buffer *leaf;
struct btrfs_item *item;
key.objectid = inode_objectid; key.objectid = inode_objectid;
key.type = BTRFS_INODE_EXTREF_KEY; key.type = BTRFS_INODE_EXTREF_KEY;
...@@ -282,9 +281,8 @@ static int btrfs_insert_inode_extref(struct btrfs_trans_handle *trans, ...@@ -282,9 +281,8 @@ static int btrfs_insert_inode_extref(struct btrfs_trans_handle *trans,
goto out; goto out;
leaf = path->nodes[0]; leaf = path->nodes[0];
item = btrfs_item_nr(path->slots[0]);
ptr = (unsigned long)btrfs_item_ptr(leaf, path->slots[0], char); ptr = (unsigned long)btrfs_item_ptr(leaf, path->slots[0], char);
ptr += btrfs_item_size(leaf, item) - ins_len; ptr += btrfs_item_size_nr(leaf, path->slots[0]) - ins_len;
extref = (struct btrfs_inode_extref *)ptr; extref = (struct btrfs_inode_extref *)ptr;
btrfs_set_inode_extref_name_len(path->nodes[0], extref, name_len); btrfs_set_inode_extref_name_len(path->nodes[0], extref, name_len);
......
...@@ -200,7 +200,6 @@ void btrfs_print_leaf(struct extent_buffer *l) ...@@ -200,7 +200,6 @@ void btrfs_print_leaf(struct extent_buffer *l)
struct btrfs_fs_info *fs_info; struct btrfs_fs_info *fs_info;
int i; int i;
u32 type, nr; u32 type, nr;
struct btrfs_item *item;
struct btrfs_root_item *ri; struct btrfs_root_item *ri;
struct btrfs_dir_item *di; struct btrfs_dir_item *di;
struct btrfs_inode_item *ii; struct btrfs_inode_item *ii;
...@@ -224,12 +223,11 @@ void btrfs_print_leaf(struct extent_buffer *l) ...@@ -224,12 +223,11 @@ void btrfs_print_leaf(struct extent_buffer *l)
btrfs_leaf_free_space(l), btrfs_header_owner(l)); btrfs_leaf_free_space(l), btrfs_header_owner(l));
print_eb_refs_lock(l); print_eb_refs_lock(l);
for (i = 0 ; i < nr ; i++) { for (i = 0 ; i < nr ; i++) {
item = btrfs_item_nr(i);
btrfs_item_key_to_cpu(l, &key, i); btrfs_item_key_to_cpu(l, &key, i);
type = key.type; type = key.type;
pr_info("\titem %d key (%llu %u %llu) itemoff %d itemsize %d\n", pr_info("\titem %d key (%llu %u %llu) itemoff %d itemsize %d\n",
i, key.objectid, type, key.offset, i, key.objectid, type, key.offset,
btrfs_item_offset(l, item), btrfs_item_size(l, item)); btrfs_item_offset_nr(l, i), btrfs_item_size_nr(l, i));
switch (type) { switch (type) {
case BTRFS_INODE_ITEM_KEY: case BTRFS_INODE_ITEM_KEY:
ii = btrfs_item_ptr(l, i, struct btrfs_inode_item); ii = btrfs_item_ptr(l, i, struct btrfs_inode_item);
......
...@@ -898,7 +898,6 @@ static int iterate_inode_ref(struct btrfs_root *root, struct btrfs_path *path, ...@@ -898,7 +898,6 @@ static int iterate_inode_ref(struct btrfs_root *root, struct btrfs_path *path,
iterate_inode_ref_t iterate, void *ctx) iterate_inode_ref_t iterate, void *ctx)
{ {
struct extent_buffer *eb = path->nodes[0]; struct extent_buffer *eb = path->nodes[0];
struct btrfs_item *item;
struct btrfs_inode_ref *iref; struct btrfs_inode_ref *iref;
struct btrfs_inode_extref *extref; struct btrfs_inode_extref *extref;
struct btrfs_path *tmp_path; struct btrfs_path *tmp_path;
...@@ -930,8 +929,7 @@ static int iterate_inode_ref(struct btrfs_root *root, struct btrfs_path *path, ...@@ -930,8 +929,7 @@ static int iterate_inode_ref(struct btrfs_root *root, struct btrfs_path *path,
if (found_key->type == BTRFS_INODE_REF_KEY) { if (found_key->type == BTRFS_INODE_REF_KEY) {
ptr = (unsigned long)btrfs_item_ptr(eb, slot, ptr = (unsigned long)btrfs_item_ptr(eb, slot,
struct btrfs_inode_ref); struct btrfs_inode_ref);
item = btrfs_item_nr(slot); total = btrfs_item_size_nr(eb, slot);
total = btrfs_item_size(eb, item);
elem_size = sizeof(*iref); elem_size = sizeof(*iref);
} else { } else {
ptr = btrfs_item_ptr_offset(eb, slot); ptr = btrfs_item_ptr_offset(eb, slot);
...@@ -1018,7 +1016,6 @@ static int iterate_dir_item(struct btrfs_root *root, struct btrfs_path *path, ...@@ -1018,7 +1016,6 @@ static int iterate_dir_item(struct btrfs_root *root, struct btrfs_path *path,
{ {
int ret = 0; int ret = 0;
struct extent_buffer *eb; struct extent_buffer *eb;
struct btrfs_item *item;
struct btrfs_dir_item *di; struct btrfs_dir_item *di;
struct btrfs_key di_key; struct btrfs_key di_key;
char *buf = NULL; char *buf = NULL;
...@@ -1047,11 +1044,10 @@ static int iterate_dir_item(struct btrfs_root *root, struct btrfs_path *path, ...@@ -1047,11 +1044,10 @@ static int iterate_dir_item(struct btrfs_root *root, struct btrfs_path *path,
eb = path->nodes[0]; eb = path->nodes[0];
slot = path->slots[0]; slot = path->slots[0];
item = btrfs_item_nr(slot);
di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item); di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
cur = 0; cur = 0;
len = 0; len = 0;
total = btrfs_item_size(eb, item); total = btrfs_item_size_nr(eb, slot);
num = 0; num = 0;
while (cur < total) { while (cur < total) {
......
...@@ -15,7 +15,6 @@ static int test_btrfs_split_item(u32 sectorsize, u32 nodesize) ...@@ -15,7 +15,6 @@ static int test_btrfs_split_item(u32 sectorsize, u32 nodesize)
struct btrfs_path *path = NULL; struct btrfs_path *path = NULL;
struct btrfs_root *root = NULL; struct btrfs_root *root = NULL;
struct extent_buffer *eb; struct extent_buffer *eb;
struct btrfs_item *item;
char *value = "mary had a little lamb"; char *value = "mary had a little lamb";
char *split1 = "mary had a little"; char *split1 = "mary had a little";
char *split2 = " lamb"; char *split2 = " lamb";
...@@ -61,7 +60,6 @@ static int test_btrfs_split_item(u32 sectorsize, u32 nodesize) ...@@ -61,7 +60,6 @@ static int test_btrfs_split_item(u32 sectorsize, u32 nodesize)
key.offset = 0; key.offset = 0;
btrfs_setup_item_for_insert(root, path, &key, value_len); btrfs_setup_item_for_insert(root, path, &key, value_len);
item = btrfs_item_nr(0);
write_extent_buffer(eb, value, btrfs_item_ptr_offset(eb, 0), write_extent_buffer(eb, value, btrfs_item_ptr_offset(eb, 0),
value_len); value_len);
...@@ -90,8 +88,7 @@ static int test_btrfs_split_item(u32 sectorsize, u32 nodesize) ...@@ -90,8 +88,7 @@ static int test_btrfs_split_item(u32 sectorsize, u32 nodesize)
goto out; goto out;
} }
item = btrfs_item_nr(0); if (btrfs_item_size_nr(eb, 0) != strlen(split1)) {
if (btrfs_item_size(eb, item) != strlen(split1)) {
test_err("invalid len in the first split"); test_err("invalid len in the first split");
ret = -EINVAL; ret = -EINVAL;
goto out; goto out;
...@@ -115,8 +112,7 @@ static int test_btrfs_split_item(u32 sectorsize, u32 nodesize) ...@@ -115,8 +112,7 @@ static int test_btrfs_split_item(u32 sectorsize, u32 nodesize)
goto out; goto out;
} }
item = btrfs_item_nr(1); if (btrfs_item_size_nr(eb, 1) != strlen(split2)) {
if (btrfs_item_size(eb, item) != strlen(split2)) {
test_err("invalid len in the second split"); test_err("invalid len in the second split");
ret = -EINVAL; ret = -EINVAL;
goto out; goto out;
...@@ -147,8 +143,7 @@ static int test_btrfs_split_item(u32 sectorsize, u32 nodesize) ...@@ -147,8 +143,7 @@ static int test_btrfs_split_item(u32 sectorsize, u32 nodesize)
goto out; goto out;
} }
item = btrfs_item_nr(0); if (btrfs_item_size_nr(eb, 0) != strlen(split3)) {
if (btrfs_item_size(eb, item) != strlen(split3)) {
test_err("invalid len in the first split"); test_err("invalid len in the first split");
ret = -EINVAL; ret = -EINVAL;
goto out; goto out;
...@@ -171,8 +166,7 @@ static int test_btrfs_split_item(u32 sectorsize, u32 nodesize) ...@@ -171,8 +166,7 @@ static int test_btrfs_split_item(u32 sectorsize, u32 nodesize)
goto out; goto out;
} }
item = btrfs_item_nr(1); if (btrfs_item_size_nr(eb, 1) != strlen(split4)) {
if (btrfs_item_size(eb, item) != strlen(split4)) {
test_err("invalid len in the second split"); test_err("invalid len in the second split");
ret = -EINVAL; ret = -EINVAL;
goto out; goto out;
...@@ -195,8 +189,7 @@ static int test_btrfs_split_item(u32 sectorsize, u32 nodesize) ...@@ -195,8 +189,7 @@ static int test_btrfs_split_item(u32 sectorsize, u32 nodesize)
goto out; goto out;
} }
item = btrfs_item_nr(2); if (btrfs_item_size_nr(eb, 2) != strlen(split2)) {
if (btrfs_item_size(eb, item) != strlen(split2)) {
test_err("invalid len in the second split"); test_err("invalid len in the second split");
ret = -EINVAL; ret = -EINVAL;
goto out; goto out;
......
...@@ -170,7 +170,6 @@ int btrfs_setxattr(struct btrfs_trans_handle *trans, struct inode *inode, ...@@ -170,7 +170,6 @@ int btrfs_setxattr(struct btrfs_trans_handle *trans, struct inode *inode,
const u16 old_data_len = btrfs_dir_data_len(leaf, di); const u16 old_data_len = btrfs_dir_data_len(leaf, di);
const u32 item_size = btrfs_item_size_nr(leaf, slot); const u32 item_size = btrfs_item_size_nr(leaf, slot);
const u32 data_size = sizeof(*di) + name_len + size; const u32 data_size = sizeof(*di) + name_len + size;
struct btrfs_item *item;
unsigned long data_ptr; unsigned long data_ptr;
char *ptr; char *ptr;
...@@ -196,9 +195,8 @@ int btrfs_setxattr(struct btrfs_trans_handle *trans, struct inode *inode, ...@@ -196,9 +195,8 @@ int btrfs_setxattr(struct btrfs_trans_handle *trans, struct inode *inode,
btrfs_extend_item(path, data_size); btrfs_extend_item(path, data_size);
} }
item = btrfs_item_nr(slot);
ptr = btrfs_item_ptr(leaf, slot, char); ptr = btrfs_item_ptr(leaf, slot, char);
ptr += btrfs_item_size(leaf, item) - data_size; ptr += btrfs_item_size_nr(leaf, slot) - data_size;
di = (struct btrfs_dir_item *)ptr; di = (struct btrfs_dir_item *)ptr;
btrfs_set_dir_data_len(leaf, di, size); btrfs_set_dir_data_len(leaf, di, size);
data_ptr = ((unsigned long)(di + 1)) + name_len; data_ptr = ((unsigned long)(di + 1)) + name_len;
......
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