From cfbcfaa4e454e56ff1964c3cdb77f03bd9cb37ec Mon Sep 17 00:00:00 2001 From: David Sterba Date: Fri, 30 Apr 2021 00:07:15 +0200 Subject: [PATCH] btrfs-progs: mkfs: move btrfs_make_root_dir from utils.c The helper is used in several tools but logically belongs to mkfs, so put it to the common section. Signed-off-by: David Sterba --- Makefile | 3 ++- check/main.c | 1 + common/utils.c | 39 --------------------------------------- common/utils.h | 3 --- mkfs/common.c | 40 ++++++++++++++++++++++++++++++++++++++++ mkfs/common.h | 5 +++++ 6 files changed, 48 insertions(+), 43 deletions(-) diff --git a/Makefile b/Makefile index 914722f9..d79b5d5c 100644 --- a/Makefile +++ b/Makefile @@ -188,7 +188,8 @@ libbtrfsutil_objects = libbtrfsutil/errors.o libbtrfsutil/filesystem.o \ libbtrfsutil/subvolume.o libbtrfsutil/qgroup.o \ libbtrfsutil/stubs.o convert_objects = convert/main.o convert/common.o convert/source-fs.o \ - convert/source-ext2.o convert/source-reiserfs.o + convert/source-ext2.o convert/source-reiserfs.o \ + mkfs/common.o mkfs_objects = mkfs/main.o mkfs/common.o mkfs/rootdir.o image_objects = image/main.o image/sanitize.o all_objects = $(objects) $(cmds_objects) $(libbtrfs_objects) $(convert_objects) \ diff --git a/check/main.c b/check/main.c index 0b73e45a..5ad2d211 100644 --- a/check/main.c +++ b/check/main.c @@ -47,6 +47,7 @@ #include "check/mode-lowmem.h" #include "check/qgroup-verify.h" #include "common/open-utils.h" +#include "mkfs/common.h" u64 bytes_used = 0; u64 total_csum_bytes = 0; diff --git a/common/utils.c b/common/utils.c index 84e48a60..fcf86f82 100644 --- a/common/utils.c +++ b/common/utils.c @@ -59,45 +59,6 @@ static unsigned short rand_seed[3]; struct btrfs_config bconf; -int btrfs_make_root_dir(struct btrfs_trans_handle *trans, - struct btrfs_root *root, u64 objectid) -{ - int ret; - struct btrfs_inode_item inode_item; - time_t now = time(NULL); - - memset(&inode_item, 0, sizeof(inode_item)); - btrfs_set_stack_inode_generation(&inode_item, trans->transid); - btrfs_set_stack_inode_size(&inode_item, 0); - btrfs_set_stack_inode_nlink(&inode_item, 1); - btrfs_set_stack_inode_nbytes(&inode_item, root->fs_info->nodesize); - btrfs_set_stack_inode_mode(&inode_item, S_IFDIR | 0755); - btrfs_set_stack_timespec_sec(&inode_item.atime, now); - btrfs_set_stack_timespec_nsec(&inode_item.atime, 0); - btrfs_set_stack_timespec_sec(&inode_item.ctime, now); - btrfs_set_stack_timespec_nsec(&inode_item.ctime, 0); - btrfs_set_stack_timespec_sec(&inode_item.mtime, now); - btrfs_set_stack_timespec_nsec(&inode_item.mtime, 0); - btrfs_set_stack_timespec_sec(&inode_item.otime, now); - btrfs_set_stack_timespec_nsec(&inode_item.otime, 0); - - if (root->fs_info->tree_root == root) - btrfs_set_super_root_dir(root->fs_info->super_copy, objectid); - - ret = btrfs_insert_inode(trans, root, objectid, &inode_item); - if (ret) - goto error; - - ret = btrfs_insert_inode_ref(trans, root, "..", 2, objectid, objectid, 0); - if (ret) - goto error; - - btrfs_set_root_dirid(&root->root_item, objectid); - ret = 0; -error: - return ret; -} - struct pending_dir { struct list_head list; char name[PATH_MAX]; diff --git a/common/utils.h b/common/utils.h index cadf3b58..e4532d41 100644 --- a/common/utils.h +++ b/common/utils.h @@ -42,9 +42,6 @@ enum exclusive_operation { BTRFS_EXCLOP_UNKNOWN = -1, }; -int btrfs_make_root_dir(struct btrfs_trans_handle *trans, - struct btrfs_root *root, u64 objectid); - enum btrfs_csum_type parse_csum_type(const char *s); u64 parse_size_from_string(const char *s); u64 parse_qgroupid(const char *p); diff --git a/mkfs/common.c b/mkfs/common.c index 7ffac8da..74a60ee6 100644 --- a/mkfs/common.c +++ b/mkfs/common.c @@ -22,6 +22,7 @@ #include "kernel-shared/ctree.h" #include "kernel-shared/disk-io.h" #include "kernel-shared/volumes.h" +#include "kernel-shared/transaction.h" #include "kernel-shared/zoned.h" #include "common/utils.h" #include "common/path-utils.h" @@ -490,6 +491,45 @@ out: return ret; } +int btrfs_make_root_dir(struct btrfs_trans_handle *trans, + struct btrfs_root *root, u64 objectid) +{ + int ret; + struct btrfs_inode_item inode_item; + time_t now = time(NULL); + + memset(&inode_item, 0, sizeof(inode_item)); + btrfs_set_stack_inode_generation(&inode_item, trans->transid); + btrfs_set_stack_inode_size(&inode_item, 0); + btrfs_set_stack_inode_nlink(&inode_item, 1); + btrfs_set_stack_inode_nbytes(&inode_item, root->fs_info->nodesize); + btrfs_set_stack_inode_mode(&inode_item, S_IFDIR | 0755); + btrfs_set_stack_timespec_sec(&inode_item.atime, now); + btrfs_set_stack_timespec_nsec(&inode_item.atime, 0); + btrfs_set_stack_timespec_sec(&inode_item.ctime, now); + btrfs_set_stack_timespec_nsec(&inode_item.ctime, 0); + btrfs_set_stack_timespec_sec(&inode_item.mtime, now); + btrfs_set_stack_timespec_nsec(&inode_item.mtime, 0); + btrfs_set_stack_timespec_sec(&inode_item.otime, now); + btrfs_set_stack_timespec_nsec(&inode_item.otime, 0); + + if (root->fs_info->tree_root == root) + btrfs_set_super_root_dir(root->fs_info->super_copy, objectid); + + ret = btrfs_insert_inode(trans, root, objectid, &inode_item); + if (ret) + goto error; + + ret = btrfs_insert_inode_ref(trans, root, "..", 2, objectid, objectid, 0); + if (ret) + goto error; + + btrfs_set_root_dirid(&root->root_item, objectid); + ret = 0; +error: + return ret; +} + /* * Btrfs minimum size calculation is complicated, it should include at least: * 1. system group size diff --git a/mkfs/common.h b/mkfs/common.h index 4d86f5ef..ea87c3ca 100644 --- a/mkfs/common.h +++ b/mkfs/common.h @@ -38,6 +38,9 @@ #define BTRFS_MKFS_DEFAULT_DATA_MULTI_DEVICE 0 /* SINGLE */ #define BTRFS_MKFS_DEFAULT_META_MULTI_DEVICE BTRFS_BLOCK_GROUP_RAID1 +struct btrfs_trans_handle; +struct btrfs_root; + /* * Tree root blocks created during mkfs */ @@ -79,6 +82,8 @@ struct btrfs_mkfs_config { }; int make_btrfs(int fd, struct btrfs_mkfs_config *cfg); +int btrfs_make_root_dir(struct btrfs_trans_handle *trans, + struct btrfs_root *root, u64 objectid); u64 btrfs_min_dev_size(u32 nodesize, int mixed, u64 meta_profile, u64 data_profile); int test_minimum_size(const char *file, u64 min_dev_size);