btrfs-progs: use btrfs_open_dir in open_path_or_dev_mnt

Use btrfs_open_dir() in open_path_or_dev_mnt() to make the function
return error when target is neither block device nor btrfs mount point.

Also add "verbose" argument to let function output common error
message instead of putting duplicated lines in caller.

Before patch:
  # ./btrfs device stats /mnt/tmp1
  ERROR: getting dev info for devstats failed: Inappropriate ioctl for device
  # ./btrfs replace start /dev/vdd /dev/vde /mnt/tmp1
  ERROR: ioctl(DEV_REPLACE_STATUS) failed on "/mnt/tmp1": Inappropriate ioctl for device

After patch:
  # ./btrfs device stats /mnt/tmp1
  ERROR: not a btrfs filesystem: /mnt/tmp1
  # ./btrfs replace start /dev/vdd /dev/vde /mnt/tmp1
  ERROR: not a btrfs filesystem: /mnt/tmp1

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
master
Zhao Lei 2015-10-12 21:23:02 +08:00 committed by David Sterba
parent 5e1a77c45c
commit 078618d822
5 changed files with 21 additions and 56 deletions

View File

@ -385,18 +385,9 @@ static int cmd_device_stats(int argc, char **argv)
dev_path = argv[optind];
fdmnt = open_path_or_dev_mnt(dev_path, &dirstream);
if (fdmnt < 0) {
if (errno == EINVAL)
fprintf(stderr,
"ERROR: '%s' is not a mounted btrfs device\n",
dev_path);
else
fprintf(stderr, "ERROR: can't access '%s': %s\n",
dev_path, strerror(errno));
fdmnt = open_path_or_dev_mnt(dev_path, &dirstream, 1);
if (fdmnt < 0)
return 1;
}
ret = get_fs_info(dev_path, &fi_args, &di_args);
if (ret) {

View File

@ -170,18 +170,9 @@ static int cmd_replace_start(int argc, char **argv)
usage(cmd_replace_start_usage);
path = argv[optind + 2];
fdmnt = open_path_or_dev_mnt(path, &dirstream);
if (fdmnt < 0) {
if (errno == EINVAL)
fprintf(stderr,
"ERROR: '%s' is not a mounted btrfs device\n",
path);
else
fprintf(stderr, "ERROR: can't access '%s': %s\n",
path, strerror(errno));
fdmnt = open_path_or_dev_mnt(path, &dirstream, 1);
if (fdmnt < 0)
goto leave_with_error;
}
/* check for possible errors before backgrounding */
status_args.cmd = BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS;

View File

@ -1198,17 +1198,9 @@ static int scrub_start(int argc, char **argv, int resume)
path = argv[optind];
fdmnt = open_path_or_dev_mnt(path, &dirstream);
if (fdmnt < 0) {
if (errno == EINVAL)
error_on(!do_quiet, "'%s' is not a mounted btrfs device",
path);
else
error_on(!do_quiet, "can't access '%s': %s",
path, strerror(errno));
fdmnt = open_path_or_dev_mnt(path, &dirstream, !do_quiet);
if (fdmnt < 0)
return 1;
}
ret = get_fs_info(path, &fi_args, &di_args);
if (ret) {
@ -1604,12 +1596,8 @@ static int cmd_scrub_cancel(int argc, char **argv)
path = argv[1];
fdmnt = open_path_or_dev_mnt(path, &dirstream);
fdmnt = open_path_or_dev_mnt(path, &dirstream, 1);
if (fdmnt < 0) {
if (errno == EINVAL)
error("'%s' is not a mounted btrfs device", path);
else
error("can't access '%s': %s", path, strerror(errno));
ret = 1;
goto out;
}
@ -1705,15 +1693,9 @@ static int cmd_scrub_status(int argc, char **argv)
path = argv[optind];
fdmnt = open_path_or_dev_mnt(path, &dirstream);
if (fdmnt < 0) {
if (errno == EINVAL)
error("'%s' is not a mounted btrfs device", path);
else
error("can't access '%s': %s", path, strerror(errno));
fdmnt = open_path_or_dev_mnt(path, &dirstream, 1);
if (fdmnt < 0)
return 1;
}
ret = get_fs_info(path, &fi_args, &di_args);
if (ret) {

21
utils.c
View File

@ -1081,27 +1081,28 @@ out:
*
* On error, return -1, errno should be set.
*/
int open_path_or_dev_mnt(const char *path, DIR **dirstream)
int open_path_or_dev_mnt(const char *path, DIR **dirstream, int verbose)
{
char mp[PATH_MAX];
int fdmnt;
fdmnt = is_block_device(path);
if (fdmnt == 1) {
int ret;
int ret;
if (is_block_device(path)) {
ret = get_btrfs_mount(path, mp, sizeof(mp));
if (ret < 0) {
/* not a mounted btrfs dev */
error_on(verbose, "'%s' is not a mounted btrfs device",
path);
errno = EINVAL;
return -1;
}
fdmnt = open_file_or_dir(mp, dirstream);
} else if (fdmnt == 0) {
fdmnt = open_file_or_dir(path, dirstream);
ret = open_file_or_dir(mp, dirstream);
error_on(verbose && ret < 0, "can't access '%s': %s",
path, strerror(errno));
} else {
ret = btrfs_open_dir(path, dirstream, 1);
}
return fdmnt;
return ret;
}
/*

View File

@ -158,7 +158,7 @@ char *__strncpy__null(char *dest, const char *src, size_t n);
int is_block_device(const char *file);
int is_mount_point(const char *file);
int check_arg_type(const char *input);
int open_path_or_dev_mnt(const char *path, DIR **dirstream);
int open_path_or_dev_mnt(const char *path, DIR **dirstream, int verbose);
int btrfs_open_dir(const char *path, DIR **dirstream, int verbose);
u64 btrfs_device_size(int fd, struct stat *st);
/* Helper to always get proper size of the destination string */