Skip to content

Commit bc52579

Browse files
adam900710kdave
authored andcommitted
btrfs-progs: convert: simplify insert_temp_dev_item() and insert_temp_chunk_item()
These functions require 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 b34a4dd commit bc52579

File tree

1 file changed

+21
-25
lines changed

1 file changed

+21
-25
lines changed

convert/common.c

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -292,14 +292,15 @@ static int setup_temp_root_tree(int fd, struct btrfs_mkfs_config *cfg,
292292
}
293293

294294
static int insert_temp_dev_item(int fd, struct extent_buffer *buf,
295-
struct btrfs_mkfs_config *cfg,
296-
int *slot, u32 *itemoff)
295+
struct btrfs_mkfs_config *cfg)
297296
{
298297
struct btrfs_disk_key disk_key;
299298
struct btrfs_dev_item *dev_item;
300299
unsigned char dev_uuid[BTRFS_UUID_SIZE];
301300
unsigned char fsid[BTRFS_FSID_SIZE];
302301
struct btrfs_super_block super;
302+
u32 slot = btrfs_header_nritems(buf);
303+
u32 itemoff = get_item_offset(buf, cfg);
303304
int ret;
304305

305306
ret = pread(fd, &super, BTRFS_SUPER_INFO_SIZE, cfg->super_bytenr);
@@ -308,17 +309,17 @@ static int insert_temp_dev_item(int fd, struct extent_buffer *buf,
308309
goto out;
309310
}
310311

311-
btrfs_set_header_nritems(buf, *slot + 1);
312-
(*itemoff) -= sizeof(*dev_item);
312+
btrfs_set_header_nritems(buf, slot + 1);
313+
itemoff -= sizeof(*dev_item);
313314
/* setup device item 1, 0 is for replace case */
314315
btrfs_set_disk_key_type(&disk_key, BTRFS_DEV_ITEM_KEY);
315316
btrfs_set_disk_key_objectid(&disk_key, BTRFS_DEV_ITEMS_OBJECTID);
316317
btrfs_set_disk_key_offset(&disk_key, 1);
317-
btrfs_set_item_key(buf, &disk_key, *slot);
318-
btrfs_set_item_offset(buf, *slot, *itemoff);
319-
btrfs_set_item_size(buf, *slot, sizeof(*dev_item));
318+
btrfs_set_item_key(buf, &disk_key, slot);
319+
btrfs_set_item_offset(buf, slot, itemoff);
320+
btrfs_set_item_size(buf, slot, sizeof(*dev_item));
320321

321-
dev_item = btrfs_item_ptr(buf, *slot, struct btrfs_dev_item);
322+
dev_item = btrfs_item_ptr(buf, slot, struct btrfs_dev_item);
322323
/* Generate device uuid */
323324
uuid_generate(dev_uuid);
324325
write_extent_buffer(buf, dev_uuid,
@@ -346,19 +347,19 @@ static int insert_temp_dev_item(int fd, struct extent_buffer *buf,
346347
read_extent_buffer(buf, &super.dev_item, (unsigned long)dev_item,
347348
sizeof(*dev_item));
348349
ret = write_temp_super(fd, &super, cfg->super_bytenr);
349-
(*slot)++;
350350
out:
351351
return ret;
352352
}
353353

354354
static int insert_temp_chunk_item(int fd, struct extent_buffer *buf,
355355
struct btrfs_mkfs_config *cfg,
356-
int *slot, u32 *itemoff, u64 start, u64 len,
357-
u64 type)
356+
u64 start, u64 len, u64 type)
358357
{
359358
struct btrfs_chunk *chunk;
360359
struct btrfs_disk_key disk_key;
361360
struct btrfs_super_block sb;
361+
u32 slot = btrfs_header_nritems(buf);
362+
u32 itemoff = get_item_offset(buf, cfg);
362363
int ret = 0;
363364

364365
ret = pread(fd, &sb, BTRFS_SUPER_INFO_SIZE, cfg->super_bytenr);
@@ -367,16 +368,16 @@ static int insert_temp_chunk_item(int fd, struct extent_buffer *buf,
367368
return ret;
368369
}
369370

370-
btrfs_set_header_nritems(buf, *slot + 1);
371-
(*itemoff) -= btrfs_chunk_item_size(1);
371+
btrfs_set_header_nritems(buf, slot + 1);
372+
itemoff -= btrfs_chunk_item_size(1);
372373
btrfs_set_disk_key_type(&disk_key, BTRFS_CHUNK_ITEM_KEY);
373374
btrfs_set_disk_key_objectid(&disk_key, BTRFS_FIRST_CHUNK_TREE_OBJECTID);
374375
btrfs_set_disk_key_offset(&disk_key, start);
375-
btrfs_set_item_key(buf, &disk_key, *slot);
376-
btrfs_set_item_offset(buf, *slot, *itemoff);
377-
btrfs_set_item_size(buf, *slot, btrfs_chunk_item_size(1));
376+
btrfs_set_item_key(buf, &disk_key, slot);
377+
btrfs_set_item_offset(buf, slot, itemoff);
378+
btrfs_set_item_size(buf, slot, btrfs_chunk_item_size(1));
378379

379-
chunk = btrfs_item_ptr(buf, *slot, struct btrfs_chunk);
380+
chunk = btrfs_item_ptr(buf, slot, struct btrfs_chunk);
380381
btrfs_set_chunk_length(buf, chunk, len);
381382
btrfs_set_chunk_owner(buf, chunk, BTRFS_EXTENT_TREE_OBJECTID);
382383
btrfs_set_chunk_stripe_len(buf, chunk, BTRFS_STRIPE_LEN);
@@ -392,7 +393,6 @@ static int insert_temp_chunk_item(int fd, struct extent_buffer *buf,
392393
write_extent_buffer(buf, sb.dev_item.uuid,
393394
(unsigned long)btrfs_stripe_dev_uuid_nr(chunk, 0),
394395
BTRFS_UUID_SIZE);
395-
(*slot)++;
396396

397397
/*
398398
* If it's system chunk, also copy it to super block.
@@ -422,8 +422,6 @@ static int setup_temp_chunk_tree(int fd, struct btrfs_mkfs_config *cfg,
422422
u64 chunk_bytenr)
423423
{
424424
struct extent_buffer *buf = NULL;
425-
u32 itemoff = cfg->leaf_data_size;
426-
int slot = 0;
427425
int ret;
428426

429427
/* Must ensure SYS chunk starts before META chunk */
@@ -440,17 +438,15 @@ static int setup_temp_chunk_tree(int fd, struct btrfs_mkfs_config *cfg,
440438
if (ret < 0)
441439
goto out;
442440

443-
ret = insert_temp_dev_item(fd, buf, cfg, &slot, &itemoff);
441+
ret = insert_temp_dev_item(fd, buf, cfg);
444442
if (ret < 0)
445443
goto out;
446-
ret = insert_temp_chunk_item(fd, buf, cfg, &slot, &itemoff,
447-
sys_chunk_start,
444+
ret = insert_temp_chunk_item(fd, buf, cfg, sys_chunk_start,
448445
BTRFS_MKFS_SYSTEM_GROUP_SIZE,
449446
BTRFS_BLOCK_GROUP_SYSTEM);
450447
if (ret < 0)
451448
goto out;
452-
ret = insert_temp_chunk_item(fd, buf, cfg, &slot, &itemoff,
453-
meta_chunk_start,
449+
ret = insert_temp_chunk_item(fd, buf, cfg, meta_chunk_start,
454450
BTRFS_CONVERT_META_GROUP_SIZE,
455451
BTRFS_BLOCK_GROUP_METADATA);
456452
if (ret < 0)

0 commit comments

Comments
 (0)