Skip to content

Commit b34a4dd

Browse files
adam900710kdave
authored andcommitted
btrfs-progs: convert: simplify insert_temp_root_item()
The function requires parameters @slot and @itemoff to record where the next item should land. But this is overkilled, as after inserting an item, the temporary extent buffer will have its header nritems and the item pointer updated. We can use that header nritems and item pointer to get where the next item should land. This removes the external counter to record @slot and @itemoff. Signed-off-by: Qu Wenruo <wqu@suse.com> Signed-off-by: David Sterba <dsterba@suse.com>
1 parent 788324f commit b34a4dd

File tree

1 file changed

+24
-19
lines changed

1 file changed

+24
-19
lines changed

convert/common.c

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -189,17 +189,29 @@ static int setup_temp_extent_buffer(struct extent_buffer *buf,
189189
return 0;
190190
}
191191

192+
static u32 get_item_offset(const struct extent_buffer *eb,
193+
const struct btrfs_mkfs_config *cfg)
194+
{
195+
u32 slot = btrfs_header_nritems(eb);
196+
197+
if (slot)
198+
return btrfs_item_offset(eb, slot - 1);
199+
else
200+
return cfg->leaf_data_size;
201+
}
202+
192203
static void insert_temp_root_item(struct extent_buffer *buf,
193204
struct btrfs_mkfs_config *cfg,
194-
int *slot, u32 *itemoff, u64 objectid,
195-
u64 bytenr)
205+
u64 objectid, u64 bytenr)
196206
{
197207
struct btrfs_root_item root_item;
198208
struct btrfs_inode_item *inode_item;
199209
struct btrfs_disk_key disk_key;
210+
u32 slot = btrfs_header_nritems(buf);
211+
u32 itemoff = get_item_offset(buf, cfg);
200212

201-
btrfs_set_header_nritems(buf, *slot + 1);
202-
(*itemoff) -= sizeof(root_item);
213+
btrfs_set_header_nritems(buf, slot + 1);
214+
itemoff -= sizeof(root_item);
203215
memset(&root_item, 0, sizeof(root_item));
204216
inode_item = &root_item.inode;
205217
btrfs_set_stack_inode_generation(inode_item, 1);
@@ -217,13 +229,12 @@ static void insert_temp_root_item(struct extent_buffer *buf,
217229
btrfs_set_disk_key_objectid(&disk_key, objectid);
218230
btrfs_set_disk_key_offset(&disk_key, 0);
219231

220-
btrfs_set_item_key(buf, &disk_key, *slot);
221-
btrfs_set_item_offset(buf, *slot, *itemoff);
222-
btrfs_set_item_size(buf, *slot, sizeof(root_item));
232+
btrfs_set_item_key(buf, &disk_key, slot);
233+
btrfs_set_item_offset(buf, slot, itemoff);
234+
btrfs_set_item_size(buf, slot, sizeof(root_item));
223235
write_extent_buffer(buf, &root_item,
224-
btrfs_item_ptr_offset(buf, *slot),
236+
btrfs_item_ptr_offset(buf, slot),
225237
sizeof(root_item));
226-
(*slot)++;
227238
}
228239

229240
/*
@@ -252,8 +263,6 @@ static int setup_temp_root_tree(int fd, struct btrfs_mkfs_config *cfg,
252263
u64 dev_bytenr, u64 fs_bytenr, u64 csum_bytenr)
253264
{
254265
struct extent_buffer *buf = NULL;
255-
u32 itemoff = cfg->leaf_data_size;
256-
int slot = 0;
257266
int ret;
258267

259268
/*
@@ -271,14 +280,10 @@ static int setup_temp_root_tree(int fd, struct btrfs_mkfs_config *cfg,
271280
if (ret < 0)
272281
goto out;
273282

274-
insert_temp_root_item(buf, cfg, &slot, &itemoff,
275-
BTRFS_EXTENT_TREE_OBJECTID, extent_bytenr);
276-
insert_temp_root_item(buf, cfg, &slot, &itemoff,
277-
BTRFS_DEV_TREE_OBJECTID, dev_bytenr);
278-
insert_temp_root_item(buf, cfg, &slot, &itemoff,
279-
BTRFS_FS_TREE_OBJECTID, fs_bytenr);
280-
insert_temp_root_item(buf, cfg, &slot, &itemoff,
281-
BTRFS_CSUM_TREE_OBJECTID, csum_bytenr);
283+
insert_temp_root_item(buf, cfg, BTRFS_EXTENT_TREE_OBJECTID, extent_bytenr);
284+
insert_temp_root_item(buf, cfg, BTRFS_DEV_TREE_OBJECTID, dev_bytenr);
285+
insert_temp_root_item(buf, cfg, BTRFS_FS_TREE_OBJECTID, fs_bytenr);
286+
insert_temp_root_item(buf, cfg, BTRFS_CSUM_TREE_OBJECTID, csum_bytenr);
282287

283288
ret = write_temp_extent_buffer(fd, buf, root_bytenr, cfg);
284289
out:

0 commit comments

Comments
 (0)