Skip to content

Commit 3a3b787

Browse files
adam900710kdave
authored andcommitted
btrfs-progs: mkfs/rootdir: factor out subvol validation code into a helper
Currently we do the validation of subvolume parameters inside mkfs/main.c, but considering all things like structure rootdir_subvol are all inside rootdir.[ch], it's better to move the validation part into rootdir.[ch]. Extract the validation part into a helper, btrfs_mkfs_validate_subvols(), into rootdir.[ch]. Furthermore since we're here, also slightly enhance the duplicated subvolume check, so that the runtime is halved. Signed-off-by: Qu Wenruo <wqu@suse.com>
1 parent c763c4e commit 3a3b787

File tree

3 files changed

+51
-44
lines changed

3 files changed

+51
-44
lines changed

mkfs/main.c

Lines changed: 3 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1527,50 +1527,9 @@ int BOX_MAIN(mkfs)(int argc, char **argv)
15271527
}
15281528
}
15291529

1530-
list_for_each_entry(rds, &subvols, list) {
1531-
char path[PATH_MAX];
1532-
struct rootdir_subvol *rds2;
1533-
1534-
if (path_cat_out(path, source_dir, rds->dir)) {
1535-
error("path invalid: %s", path);
1536-
ret = 1;
1537-
goto error;
1538-
}
1539-
1540-
if (!realpath(path, rds->full_path)) {
1541-
error("could not get canonical path: %s", rds->dir);
1542-
ret = 1;
1543-
goto error;
1544-
}
1545-
1546-
if (!path_exists(rds->full_path)) {
1547-
error("subvolume path does not exist: %s", rds->dir);
1548-
ret = 1;
1549-
goto error;
1550-
}
1551-
1552-
if (!path_is_dir(rds->full_path)) {
1553-
error("subvolume is not a directory: %s", rds->dir);
1554-
ret = 1;
1555-
goto error;
1556-
}
1557-
1558-
if (!path_is_in_dir(source_dir, rds->full_path)) {
1559-
error("subvolume %s is not a child of %s", rds->dir, source_dir);
1560-
ret = 1;
1561-
goto error;
1562-
}
1563-
1564-
for (rds2 = list_first_entry(&subvols, struct rootdir_subvol, list);
1565-
rds2 != rds;
1566-
rds2 = list_next_entry(rds2, list)) {
1567-
if (strcmp(rds2->full_path, rds->full_path) == 0) {
1568-
error("subvolume specified more than once: %s", rds->dir);
1569-
ret = 1;
1570-
goto error;
1571-
}
1572-
}
1573-
}
1530+
ret = btrfs_mkfs_validate_subvols(source_dir, &subvols);
1531+
if (ret < 0)
1532+
goto error;
15741533

15751534
list_for_each_entry(rif, &inode_flags_list, list) {
15761535
char path[PATH_MAX];

mkfs/rootdir.c

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,6 +1077,52 @@ static ssize_t zstd_compress_inline_extent(char *buf, u64 size, char **comp_buf)
10771077
}
10781078
#endif
10791079

1080+
int btrfs_mkfs_validate_subvols(const char *source_dir, struct list_head *subvols)
1081+
{
1082+
struct rootdir_subvol *rds;
1083+
1084+
list_for_each_entry(rds, subvols, list) {
1085+
char path[PATH_MAX];
1086+
struct rootdir_subvol *rds2;
1087+
int ret;
1088+
1089+
ret = path_cat_out(path, source_dir, rds->dir);
1090+
if (ret < 0) {
1091+
errno = -ret;
1092+
error("path invalid '%s': %m", path);
1093+
return ret;
1094+
}
1095+
if (!realpath(path, rds->full_path)) {
1096+
ret = -errno;
1097+
error("could not get canonical path of '%s': %m", rds->dir);
1098+
return ret;
1099+
}
1100+
ret = path_exists(rds->full_path);
1101+
if (ret < 0) {
1102+
error("subvolume path does not exist: %s", rds->dir);
1103+
return ret;
1104+
}
1105+
ret = path_is_dir(rds->full_path);
1106+
if (ret < 0) {
1107+
error("subvolume is not a directory: %s", rds->dir);
1108+
return ret;
1109+
}
1110+
list_for_each_entry(rds2, subvols, list) {
1111+
/*
1112+
* Only compare entries before us, So we won't compare
1113+
* the same pair twice.
1114+
*/
1115+
if (rds2 == rds)
1116+
break;
1117+
if (strcmp(rds2->full_path, rds->full_path) == 0) {
1118+
error("subvolume specified more than once: %s", rds->dir);
1119+
return -EINVAL;
1120+
}
1121+
}
1122+
}
1123+
return 0;
1124+
}
1125+
10801126
static int add_file_items(struct btrfs_trans_handle *trans,
10811127
struct btrfs_root *root,
10821128
struct btrfs_inode_item *btrfs_inode, u64 objectid,

mkfs/rootdir.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ struct btrfs_root;
3939

4040
struct rootdir_subvol {
4141
struct list_head list;
42+
/* The path inside the source_dir. */
4243
char dir[PATH_MAX];
4344
char full_path[PATH_MAX];
4445
bool is_default;
@@ -59,6 +60,7 @@ struct rootdir_inode_flags_entry {
5960
bool nodatasum;
6061
};
6162

63+
int btrfs_mkfs_validate_subvols(const char *source_dir, struct list_head *subvols);
6264
int btrfs_mkfs_fill_dir(struct btrfs_trans_handle *trans, const char *source_dir,
6365
struct btrfs_root *root, struct list_head *subvols,
6466
struct list_head *inode_flags_list,

0 commit comments

Comments
 (0)