btrfs-progs: Introduce open_btrfs_dir wrapper
v2 cover btrfs-progs: Introduce open_btrfs_dir wrapper
This patch introduce open_btrfs_dir() to open a dir in btrfs filesystem.
It can be used for several tools in btrfs-progs.
Changelog v1-v2:
1: Add error message for open_file failed case
Zhao Lei (2):
btrfs-progs: Introduce open_btrfs_dir wrapper
btrfs-progs: use open_btrfs_dir for btrfs device command
cmds-device.c | 16 +++++----------- utils.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ utils.h | 1 + 3 files changed, 62 insertions(+), 11 deletions(-)
Signature · 1 line
1.8.5.1
PATCH v2 1/2 btrfs-progs: Introduce open_btrfs_dir wrapper
This patch introduce open_btrfs_dir() to open a dir in btrfs filesystem.
It can be used for several tools in btrfs-progs.
--- utils.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ utils.h | 1 + 2 files changed, 57 insertions(+)
utils.c +56 -0
@@ -36,6 +36,8 @@#include <limits.h> #include <blkid/blkid.h> #include <sys/vfs.h> +#include <sys/statfs.h> +#include <linux/magic.h> #include "kerncompat.h" #include "radix-tree.h"
@@ -1081,6 +1083,60 @@ int open_path_or_dev_mnt(const char *path, DIR **dirstream)return fdmnt; } +/* +* Do following check before open_file_or_dir(): +* 1: path is in a btrfs filesystem +* 2: path is a dir +*/ +int open_btrfs_dir (const char *path,DIR **dirstream,int output) +{ +struct statfs stfs; +struct stat st; +int ret; + +if (statfs (path, &stfs) !=0 ) { +if (output) +fprintf (stderr, +"ERROR: can't access '%s', %s\n" , + path,strerror (errno)); +return -1 ; + } + +if (stfs.f_type !=BTRFS_SUPER_MAGIC ) { +if (output) +fprintf (stderr, +"ERROR: not btrfs filesystem: %s\n" , + path); +return -2 ; + } + +if (stat (path, &st) !=0 ) { +if (output) +fprintf (stderr, +"ERROR: can't access '%s', %s\n" , + path,strerror (errno)); +return -1 ; + } + +if (!S_ISDIR (st.st_mode)) { +if (output) +fprintf (stderr, +"ERROR: not directory: %s\n" , + path); +return -3 ; + } + + ret =open_file_or_dir (path, dirstream); +if (ret <0 ) { +if (output) +fprintf (stderr, +"ERROR: can't access '%s', %s\n" , + path,strerror (errno)); + } + +return ret; +} +/* checks if a device is a loop device */ static int is_loop_device (const char * device) {struct stat statbuf;
utils.h +1 -0
@@ -158,6 +158,7 @@ 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_btrfs_dir (const char *path,DIR **dirstream,int output);u64 btrfs_device_size (int fd,struct stat *st);/* Helper to always get proper size of the destination string */ #define strncpy_null (dest, src) __strncpy__null(dest, src, sizeof(dest))
Signature · 1 line
1.8.5.1
Quoted 6 lines · Zhao Lei
This patch introduce
open_btrfs_dir()to open a dir in btrfs filesystem.It can be used for several tools in btrfs-progs.
Signed-off-by Zhao Lei <[email protected]>
I've renamed it to btrfs_open_dir and tweaked some error messages and applied, thanks.
PATCH v2 2/2 btrfs-progs: use open_btrfs_dir for btrfs device command
In reply to Zhao LeiWe can use open_btrfs_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 device add -f /dev/sda13 /mnt/tmp ERROR: error adding the device '/dev/sda13' - Inappropriate ioctl for device #
After patch:
# btrfs device add -f /dev/sda13 /mnt/tmp ERROR: not btrfs filesystem: /mnt/tmp #
Similar fix for device remove and device usage.
--- cmds-device.c | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-)
cmds-device.c +5 -11
@@ -84,11 +84,9 @@ static int cmd_device_add(int argc, char **argv) mntpnt = argv[optind + argc -1 ]; - fdmnt =open_file_or_dir (mntpnt, &dirstream); -if (fdmnt <0 ) { -fprintf (stderr,"ERROR: can't access '%s'\n" , mntpnt); + fdmnt =open_btrfs_dir (mntpnt, &dirstream,1 ); +if (fdmnt <0 )return 1 ; - }for (i = optind; i < optind + argc -1 ; i++){struct btrfs_ioctl_vol_args ioctl_args;
@@ -157,11 +155,9 @@ static int _cmd_device_remove(int argc, char **argv, mntpnt = argv[argc -1 ]; - fdmnt =open_file_or_dir (mntpnt, &dirstream); -if (fdmnt <0 ) { -fprintf (stderr,"ERROR: can't access '%s'\n" , mntpnt); + fdmnt =open_btrfs_dir (mntpnt, &dirstream,1 ); +if (fdmnt <0 )return 1 ; - }for (i=1 ; i < argc -1 ; i++ ){struct btrfs_ioctl_vol_args arg;
@@ -586,10 +582,8 @@ int cmd_device_usage(int argc, char **argv)if (more_than_one)printf ("\n" ); - fd =open_file_or_dir (argv[i], &dirstream); + fd =open_btrfs_dir (argv[i], &dirstream,1 );if (fd <0 ) { -fprintf (stderr,"ERROR: can't access '%s'\n" , - argv[1 ]); ret =1 ; goto out; }
Signature · 1 line
1.8.5.1
David Sterba
AppliedRe: [PATCH v2 2/2] btrfs-progs: use open_btrfs_dir for btrfs device command
Quoted 11 lines · Zhao Lei
We can use
open_btrfs_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/tmpis not btrfs mountpoint)
## btrfs device add -f /dev/sda13 /mnt/tmp ERROR: error adding the device '/dev/sda13' - Inappropriate ioctl for device #
Quoted 8 lines
After patch:
# btrfs device add -f /dev/sda13 /mnt/tmp ERROR: not btrfs filesystem: /mnt/tmp #Similar fix for device remove and device usage.
Signed-off-by Zhao Lei <[email protected]>
Applied, thanks.