btrfs-progs: subvolume: use btrfs_open_dir for btrfs subvolume command

We can use btrfs_open_dir() to check whether target dir is
in btrfs's mount point before open, instead of checking it in
kernel space of ioctl, and return fuzzy error message.

Before patch:
  # (/mnt/tmp is not btrfs mountpoint)
  #
  # btrfs subvolume create /mnt/tmp/123
  Create subvolume '/mnt/tmp/123'
  ERROR: cannot create subvolume - Inappropriate ioctl for device
  #

After patch:
  # btrfs subvolume create /mnt/tmp/123
  ERROR: not btrfs filesystem: /mnt/tmp
  #

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
This commit is contained in:
Zhao Lei 2015-10-12 21:22:54 +08:00 committed by David Sterba
parent 86f61d3bb3
commit 2dfb710803

View File

@ -181,11 +181,9 @@ static int cmd_subvol_create(int argc, char **argv)
goto out; goto out;
} }
fddst = open_file_or_dir(dstdir, &dirstream); fddst = btrfs_open_dir(dstdir, &dirstream, 1);
if (fddst < 0) { if (fddst < 0)
fprintf(stderr, "ERROR: can't access '%s'\n", dstdir);
goto out; goto out;
}
printf("Create subvolume '%s/%s'\n", dstdir, newname); printf("Create subvolume '%s/%s'\n", dstdir, newname);
if (inherit) { if (inherit) {
@ -348,9 +346,8 @@ again:
vname = basename(dupvname); vname = basename(dupvname);
free(cpath); free(cpath);
fd = open_file_or_dir(dname, &dirstream); fd = btrfs_open_dir(dname, &dirstream, 1);
if (fd < 0) { if (fd < 0) {
fprintf(stderr, "ERROR: can't access '%s'\n", dname);
ret = 1; ret = 1;
goto out; goto out;
} }
@ -564,7 +561,7 @@ static int cmd_subvol_list(int argc, char **argv)
} }
subvol = argv[optind]; subvol = argv[optind];
fd = open_file_or_dir(subvol, &dirstream); fd = btrfs_open_dir(subvol, &dirstream, 1);
if (fd < 0) { if (fd < 0) {
ret = -1; ret = -1;
fprintf(stderr, "ERROR: can't access '%s'\n", subvol); fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
@ -723,17 +720,13 @@ static int cmd_subvol_snapshot(int argc, char **argv)
goto out; goto out;
} }
fddst = open_file_or_dir(dstdir, &dirstream1); fddst = btrfs_open_dir(dstdir, &dirstream1, 1);
if (fddst < 0) { if (fddst < 0)
fprintf(stderr, "ERROR: can't access '%s'\n", dstdir);
goto out; goto out;
}
fd = open_file_or_dir(subvol, &dirstream2); fd = btrfs_open_dir(subvol, &dirstream2, 1);
if (fd < 0) { if (fd < 0)
fprintf(stderr, "ERROR: can't access '%s'\n", dstdir);
goto out; goto out;
}
if (readonly) { if (readonly) {
args.flags |= BTRFS_SUBVOL_RDONLY; args.flags |= BTRFS_SUBVOL_RDONLY;
@ -791,11 +784,9 @@ static int cmd_subvol_get_default(int argc, char **argv)
usage(cmd_subvol_get_default_usage); usage(cmd_subvol_get_default_usage);
subvol = argv[1]; subvol = argv[1];
fd = open_file_or_dir(subvol, &dirstream); fd = btrfs_open_dir(subvol, &dirstream, 1);
if (fd < 0) { if (fd < 0)
fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
return 1; return 1;
}
ret = btrfs_list_get_default_subvolume(fd, &default_id); ret = btrfs_list_get_default_subvolume(fd, &default_id);
if (ret) { if (ret) {
@ -859,11 +850,9 @@ static int cmd_subvol_set_default(int argc, char **argv)
objectid = arg_strtou64(subvolid); objectid = arg_strtou64(subvolid);
fd = open_file_or_dir(path, &dirstream); fd = btrfs_open_dir(path, &dirstream, 1);
if (fd < 0) { if (fd < 0)
fprintf(stderr, "ERROR: can't access '%s'\n", path);
return 1; return 1;
}
ret = ioctl(fd, BTRFS_IOC_DEFAULT_SUBVOL, &objectid); ret = ioctl(fd, BTRFS_IOC_DEFAULT_SUBVOL, &objectid);
e = errno; e = errno;
@ -906,11 +895,9 @@ static int cmd_subvol_find_new(int argc, char **argv)
return 1; return 1;
} }
fd = open_file_or_dir(subvol, &dirstream); fd = btrfs_open_dir(subvol, &dirstream, 1);
if (fd < 0) { if (fd < 0)
fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
return 1; return 1;
}
ret = ioctl(fd, BTRFS_IOC_SYNC); ret = ioctl(fd, BTRFS_IOC_SYNC);
if (ret < 0) { if (ret < 0) {
@ -980,11 +967,9 @@ static int cmd_subvol_show(int argc, char **argv)
ret = 1; ret = 1;
svpath = get_subvol_name(mnt, fullpath); svpath = get_subvol_name(mnt, fullpath);
fd = open_file_or_dir(fullpath, &dirstream1); fd = btrfs_open_dir(fullpath, &dirstream1, 1);
if (fd < 0) { if (fd < 0)
fprintf(stderr, "ERROR: can't access '%s'\n", fullpath);
goto out; goto out;
}
ret = btrfs_list_get_path_rootid(fd, &sv_id); ret = btrfs_list_get_path_rootid(fd, &sv_id);
if (ret) { if (ret) {
@ -993,11 +978,9 @@ static int cmd_subvol_show(int argc, char **argv)
goto out; goto out;
} }
mntfd = open_file_or_dir(mnt, &dirstream2); mntfd = btrfs_open_dir(mnt, &dirstream2, 1);
if (mntfd < 0) { if (mntfd < 0)
fprintf(stderr, "ERROR: can't access '%s'\n", mnt);
goto out; goto out;
}
if (sv_id == BTRFS_FS_TREE_OBJECTID) { if (sv_id == BTRFS_FS_TREE_OBJECTID) {
printf("%s is btrfs root\n", fullpath); printf("%s is btrfs root\n", fullpath);
@ -1271,9 +1254,8 @@ static int cmd_subvol_sync(int argc, char **argv)
if (check_argc_min(argc - optind, 1)) if (check_argc_min(argc - optind, 1))
usage(cmd_subvol_sync_usage); usage(cmd_subvol_sync_usage);
fd = open_file_or_dir(argv[optind], &dirstream); fd = btrfs_open_dir(argv[optind], &dirstream, 1);
if (fd < 0) { if (fd < 0) {
fprintf(stderr, "ERROR: can't access '%s'\n", argv[optind]);
ret = 1; ret = 1;
goto out; goto out;
} }