btrfs-progs: factor out btrfs_scan_argv_devices

To prepare for handling command line given devices factor out
btrfs_scan_argv_devices().

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.com>
This commit is contained in:
Anand Jain 2023-06-13 18:26:56 +08:00 committed by David Sterba
parent 60771e72a4
commit e408cfb49b
3 changed files with 43 additions and 34 deletions

View File

@ -327,7 +327,6 @@ static int cmd_inspect_dump_tree(const struct cmd_struct *cmd,
bool roots_only = false;
bool root_backups = false;
int traverse = BTRFS_PRINT_TREE_DEFAULT;
int dev_optind;
u64 block_bytenr;
struct btrfs_root *tree_root_scan;
u64 tree_id = 0;
@ -455,39 +454,9 @@ static int cmd_inspect_dump_tree(const struct cmd_struct *cmd,
if (check_argc_min(argc - optind, 1))
return 1;
dev_optind = optind;
while (dev_optind < argc) {
int fd;
struct btrfs_fs_devices *fs_devices;
u64 num_devices;
ret = check_arg_type(argv[optind]);
if (ret != BTRFS_ARG_BLKDEV && ret != BTRFS_ARG_REG) {
if (ret < 0) {
errno = -ret;
error("invalid argument %s: %m", argv[dev_optind]);
} else {
error("not a block device or regular file: %s",
argv[dev_optind]);
}
}
fd = open(argv[dev_optind], O_RDONLY);
if (fd < 0) {
error("cannot open %s: %m", argv[dev_optind]);
return -EINVAL;
}
ret = btrfs_scan_one_device(fd, argv[dev_optind], &fs_devices,
&num_devices,
BTRFS_SUPER_INFO_OFFSET,
SBREAD_DEFAULT);
close(fd);
if (ret < 0) {
errno = -ret;
error("device scan %s: %m", argv[dev_optind]);
return ret;
}
dev_optind++;
}
ret = btrfs_scan_argv_devices(optind, argc, argv);
if (ret)
return ret;
pr_verbose(LOG_DEFAULT, "%s\n", PACKAGE_STRING);

View File

@ -500,3 +500,42 @@ int btrfs_scan_devices(int verbose)
return 0;
}
int btrfs_scan_argv_devices(int dev_optind, int dev_argc, char **dev_argv)
{
int ret;
while (dev_optind < dev_argc) {
int fd;
u64 num_devices;
struct btrfs_fs_devices *fs_devices;
ret = check_arg_type(dev_argv[dev_optind]);
if (ret != BTRFS_ARG_BLKDEV && ret != BTRFS_ARG_REG) {
if (ret < 0) {
errno = -ret;
error("invalid argument %s: %m", dev_argv[dev_optind]);
} else {
error("not a block device or regular file: %s",
dev_argv[dev_optind]);
}
}
fd = open(dev_argv[dev_optind], O_RDONLY);
if (fd < 0) {
error("cannot open %s: %m", dev_argv[dev_optind]);
return -errno;
}
ret = btrfs_scan_one_device(fd, dev_argv[dev_optind], &fs_devices,
&num_devices,
BTRFS_SUPER_INFO_OFFSET,
SBREAD_DEFAULT);
close(fd);
if (ret < 0) {
errno = -ret;
error("device scan of %s failed: %m", dev_argv[dev_optind]);
return ret;
}
dev_optind++;
}
return 0;
}

View File

@ -45,6 +45,7 @@ struct seen_fsid {
};
int btrfs_scan_devices(int verbose);
int btrfs_scan_argv_devices(int dev_optind, int argc, char **argv);
int btrfs_register_one_device(const char *fname);
int btrfs_register_all_devices(void);
int btrfs_add_to_fsid(struct btrfs_trans_handle *trans,