Remove unused dirstream variable
v2 cover Remove unused dirstream variable
From: Goffredo Baroncelli <[email protected]>
For historical reason, the helpers [btrfs_]open_[file_or_]dir() work with directory returning the 'fd' and a 'dirstream' variable returned by opendir(3).
If the path is a file, the 'fd' is computed from open(2) and dirstream is set to NULL. If the path is a directory, first the directory is opened by opendir(3), then the 'fd' is computed using dirfd(3).
However the 'dirstream' returned by opendir(3) is left open until 'fd' is not needed anymore.
In near every case the 'dirstream' variable is not used. Only 'fd' is used.
A call to close_file_or_dir() freed both 'fd' and 'dirstream'.
Aim of this patch set is to getrid of this complexity; when the path of a directory is passed, the fd is get directly using open(path, O_RDONLY):
so we don't need to use readdir(3) and to maintain the not used variable 'dirstream'.
So each call of a legacy [btrfs_]open_[file_or_]dir() helper is replaced by a call to the new btrfs_open_[file_or_]dir_fd() functions. These functions return only the file descriptor.
Also close_file_or_dir() is not needed anymore.
The first patch, introduces the new helpers; the last patch removed the unused older helpers. The intermediate patches updated the code.
The 3rd patch updated also the add_seen_fsid() function. Before it stored the dirstream variable. But now it is not needed anymore. So we removed a parameter of the functions and a field in the structure.
In the 8th patch, the only occurrences where 'dirstream' is used was
corrected: the dirstream is computed using fdopendir(3), and the cleanup is updated according.
The results is:
- removed 7 functions
- add 4 new functions
- removed ~100 lines
- removed 44 occurrences of an unused 'dirstream' variable.
Changelog:
09/dec/2023: V1: first issue
08/feb/2024: V2
- rebased over 6.7
- swapped the tests order inside the btrfs_open_fd2() function to prevent the failure of the test 003-fi-resize-args
- removed two new occurences of open_file_or_dir() inside cmd_scrub_limit() (introduced after V1 pateches set)
BR
G.Baroncelli
Goffredo Baroncelli (9):
Killing dirstream: add helpers
Killing dirstream: replace btrfs_open_dir with btrfs_open_dir_fd Killing dirstream: replace btrfs_open_dir with btrfs_open_dir_fd Killing dirstream: replace open_path_or_dev_mnt with btrfs_open_mnt_fd Killing dirstream: replace open_file_or_dir3 with btrfs_open_fd2 Killing dirstream: replace btrfs_open_file_or_dir with
btrfs_open_file_or_dir_fd
Killing dirstream: replace open_file_or_dir with btrfs_open_fd2 Killing dirstream: remove open_file_or_dir3 from du_add_file Killing dirstream: remove unused functions
cmds/balance.c | 27 ++++----- cmds/device.c | 26 ++++----- cmds/filesystem-du.c | 18 +++++- cmds/filesystem-usage.c | 5 +- cmds/filesystem.c | 26 ++++----- cmds/inspect.c | 35 +++++------- cmds/property.c | 5 +- cmds/qgroup.c | 29 ++++------ cmds/quota.c | 16 +++--- cmds/replace.c | 17 +++--- cmds/scrub.c | 20 +++---- cmds/subvolume-list.c | 5 +- cmds/subvolume.c | 44 ++++++--------- common/device-scan.c | 6 +- common/device-scan.h | 4 +- common/open-utils.c | 118 +++++++++++----------------------------- common/open-utils.h | 13 ++--- common/utils.c | 5 +- 18 files changed, 162 insertions(+), 257 deletions(-)
Signature · 1 line
2.43.0
PATCH v2 1/9 Killing dirstream: add helpers
From: Goffredo Baroncelli <[email protected]>
For historical reason the helpers [btrfs_]open_dir... return also the 'DIR *dirstream' value when a dir is opened.
However this is never used. So avoid calling diropen() and return only the fd.
This is a preparatory patch that adds some helpers.
--- common/open-utils.c | 81 +++++++++++++++++++++++++++++++++++++++++++++ common/open-utils.h | 5 +++ 2 files changed, 86 insertions(+)
common/open-utils.c +81 -0
@@ -318,3 +318,84 @@ void close_file_or_dir(int fd, DIR *dirstream) } +/* +* Do the following checks before calling open: +* 1: path is in a btrfs filesystem +*/ +int btrfs_open_fd2 (const char *path,bool verbose,bool read_write,bool dir_only) +{ +struct statfs stfs; +struct stat st; +int ret; + +if (stat (path, &st) !=0 ) { +error_on (verbose,"cannot access '%s': %m" , path); +return -1 ; + } + +if (dir_only && !S_ISDIR (st.st_mode)) { +error_on (verbose,"not a directory: %s" , path); +return -3 ; + } + +if (statfs (path, &stfs) !=0 ) { +error_on (verbose,"cannot access '%s': %m" , path); +return -1 ; + } + +if (stfs.f_type !=BTRFS_SUPER_MAGIC ) { +error_on (verbose,"not a btrfs filesystem: %s" , path); +return -2 ; + } + +if (S_ISDIR (st.st_mode) || !read_write) + ret =open (path,O_RDONLY ); +else + ret =open (path,O_RDWR ); + +if (ret <0 ) { +error_on (verbose,"cannot access '%s': %m" , path); + } + +return ret; +} + +int btrfs_open_file_or_dir_fd (const char *path) +{ +return btrfs_open_fd2 (path, true, true, false); +} + +int btrfs_open_dir_fd (const char *path) +{ +return btrfs_open_fd2 (path, true, true, true); +} + + +/* +* Given a pathname, return a filehandle to: +* the original pathname or, +* if the pathname is a mounted btrfs device, to its mountpoint. +* +* On error, return -1, errno should be set. +*/ +int btrfs_open_mnt_fd (const char *path,bool verbose) +{ +char mp[PATH_MAX ]; +int ret; + +if (path_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 ; + } + ret =btrfs_open_fd2 (mp, verbose, true, true); + }else { + ret =btrfs_open_dir_fd (path); + } + +return ret; +}
common/open-utils.h +5 -0
@@ -39,4 +39,9 @@ int btrfs_open_file_or_dir(const char *path, DIR **dirstream, int verbose);void close_file_or_dir (int fd,DIR *dirstream); +int btrfs_open_fd2 (const char *path,bool verbose,bool read_write,bool dir_only); +int btrfs_open_file_or_dir_fd (const char *path); +int btrfs_open_dir_fd (const char *path); +int btrfs_open_mnt_fd (const char *path,bool verbose); +#endif
Signature · 1 line
2.43.0
PATCH v2 2/9 Killing dirstream: replace btrfs_open_dir with btrfs_open_dir_fd
In reply to Goffredo BaroncelliFrom: Goffredo Baroncelli <[email protected]>
For historical reason the helpers [btrfs_]open_dir... return also the 'DIR *dirstream' value when a dir is opened.
However this is never used. So avoid calling diropen() and return only the fd.
This patch replaces btrfs_open_dir() with btrfs_open_dir_fd() removing any reference to the unused/useless dirstream variables.
--- cmds/balance.c | 27 +++++++++++---------------- cmds/device.c | 21 +++++++++------------ cmds/filesystem-usage.c | 5 ++--- cmds/filesystem.c | 14 ++++++-------- cmds/inspect.c | 25 ++++++++++--------------- cmds/qgroup.c | 29 ++++++++++++----------------- cmds/quota.c | 16 +++++++--------- cmds/replace.c | 10 ++++------ cmds/subvolume-list.c | 5 ++--- cmds/subvolume.c | 31 +++++++++++++------------------ 10 files changed, 76 insertions(+), 107 deletions(-)
cmds/balance.c +11 -16
@@ -299,9 +299,8 @@ static int do_balance(const char *path, struct btrfs_ioctl_balance_args *args, {int fd;int ret; -DIR *dirstream =NULL ; - fd =btrfs_open_dir (path, &dirstream,1 ); + fd =btrfs_open_dir_fd (path);if (fd <0 )return 1 ;
@@ -309,7 +308,7 @@ static int do_balance(const char *path, struct btrfs_ioctl_balance_args *args,if (ret !=0 ) {if (ret <0 )error ("unable to check status of exclusive operation: %m" ); -close_file_or_dir (fd, dirstream); +close (fd);return 1 ; }
@@ -348,7 +347,7 @@ static int do_balance(const char *path, struct btrfs_ioctl_balance_args *args, } out: -close_file_or_dir (fd, dirstream); +close (fd);return ret; }
@@ -606,7 +605,6 @@ static int cmd_balance_pause(const struct cmd_struct *cmd,const char *path;int fd;int ret; -DIR *dirstream =NULL ;clean_args_no_options (cmd, argc, argv);
@@ -615,7 +613,7 @@ static int cmd_balance_pause(const struct cmd_struct *cmd, path = argv[optind]; - fd =btrfs_open_dir (path, &dirstream,1 ); + fd =btrfs_open_dir_fd (path);if (fd <0 )return 1 ;
@@ -630,7 +628,7 @@ static int cmd_balance_pause(const struct cmd_struct *cmd, }btrfs_warn_multiple_profiles (fd); -close_file_or_dir (fd, dirstream); +close (fd);return ret; } static DEFINE_SIMPLE_COMMAND(balance_pause, "pause");
@@ -647,7 +645,6 @@ static int cmd_balance_cancel(const struct cmd_struct *cmd,const char *path;int fd;int ret; -DIR *dirstream =NULL ;clean_args_no_options (cmd, argc, argv);
@@ -656,7 +653,7 @@ static int cmd_balance_cancel(const struct cmd_struct *cmd, path = argv[optind]; - fd =btrfs_open_dir (path, &dirstream,1 ); + fd =btrfs_open_dir_fd (path);if (fd <0 )return 1 ;
@@ -671,7 +668,7 @@ static int cmd_balance_cancel(const struct cmd_struct *cmd, }btrfs_warn_multiple_profiles (fd); -close_file_or_dir (fd, dirstream); +close (fd);return ret; } static DEFINE_SIMPLE_COMMAND(balance_cancel, "cancel");
@@ -689,7 +686,6 @@ static int cmd_balance_resume(const struct cmd_struct *cmd, {struct btrfs_ioctl_balance_args args;const char *path; -DIR *dirstream =NULL ;int fd;int ret;
@@ -700,7 +696,7 @@ static int cmd_balance_resume(const struct cmd_struct *cmd, path = argv[optind]; - fd =btrfs_open_dir (path, &dirstream,1 ); + fd =btrfs_open_dir_fd (path);if (fd <0 )return 1 ;
@@ -734,7 +730,7 @@ static int cmd_balance_resume(const struct cmd_struct *cmd, args.stat.completed, args.stat.considered); } -close_file_or_dir (fd, dirstream); +close (fd);return ret; } static DEFINE_SIMPLE_COMMAND(balance_resume, "resume");
@@ -760,7 +756,6 @@ static int cmd_balance_status(const struct cmd_struct *cmd, {struct btrfs_ioctl_balance_args args;const char *path; -DIR *dirstream =NULL ;int fd;int ret;
@@ -790,7 +785,7 @@ static int cmd_balance_status(const struct cmd_struct *cmd, path = argv[optind]; - fd =btrfs_open_dir (path, &dirstream,1 ); + fd =btrfs_open_dir_fd (path);if (fd <0 )return 2 ;
@@ -828,7 +823,7 @@ static int cmd_balance_status(const struct cmd_struct *cmd, ret =1 ; out: -close_file_or_dir (fd, dirstream); +close (fd);return ret; } static DEFINE_SIMPLE_COMMAND(balance_status, "status");
cmds/device.c +9 -12
@@ -62,7 +62,6 @@ static int cmd_device_add(const struct cmd_struct *cmd, {char *mntpnt;int i, fdmnt, ret =0 ; -DIR *dirstream =NULL ;bool discard = true;bool force = false;int last_dev;
@@ -105,7 +104,7 @@ static int cmd_device_add(const struct cmd_struct *cmd, last_dev = argc -1 ; mntpnt = argv[last_dev]; - fdmnt =btrfs_open_dir (mntpnt, &dirstream,1 ); + fdmnt =btrfs_open_dir_fd (mntpnt);if (fdmnt <0 )return 1 ;
@@ -113,7 +112,7 @@ static int cmd_device_add(const struct cmd_struct *cmd,if (ret !=0 ) {if (ret <0 )error ("unable to check status of exclusive operation: %m" ); -close_file_or_dir (fdmnt, dirstream); +close (fdmnt);return 1 ; }
@@ -181,7 +180,7 @@ static int cmd_device_add(const struct cmd_struct *cmd, error_out:btrfs_warn_multiple_profiles (fdmnt); -close_file_or_dir (fdmnt, dirstream); +close (fdmnt);return !!ret; } static DEFINE_SIMPLE_COMMAND(device_add, "add");
@@ -191,7 +190,6 @@ static int _cmd_device_remove(const struct cmd_struct *cmd, {char *mntpnt;int i, fdmnt, ret =0 ; -DIR *dirstream =NULL ;bool enqueue = false;bool cancel = false;bool force = false;
@@ -227,7 +225,7 @@ static int _cmd_device_remove(const struct cmd_struct *cmd, mntpnt = argv[argc -1 ]; - fdmnt =btrfs_open_dir (mntpnt, &dirstream,1 ); + fdmnt =btrfs_open_dir_fd (mntpnt);if (fdmnt <0 )return 1 ;
@@ -236,7 +234,7 @@ static int _cmd_device_remove(const struct cmd_struct *cmd,if (cancel) {error ("cancel requested but another device specified: %s\n" , argv[i]); -close_file_or_dir (fdmnt, dirstream); +close (fdmnt);return 1 ; }if (strcmp ("cancel" , argv[i]) ==0 ) {
@@ -271,7 +269,7 @@ static int _cmd_device_remove(const struct cmd_struct *cmd,if (ret <0 )error ("unable to check status of exclusive operation: %m" ); -close_file_or_dir (fdmnt, dirstream); +close (fdmnt);return 1 ; } }
@@ -337,7 +335,7 @@ static int _cmd_device_remove(const struct cmd_struct *cmd, }btrfs_warn_multiple_profiles (fdmnt); -close_file_or_dir (fdmnt, dirstream); +close (fdmnt);return !!ret; }
@@ -900,12 +898,11 @@ static int cmd_device_usage(const struct cmd_struct *cmd, int argc, char **argv)for (i = optind; i < argc; i++) {int fd; -DIR *dirstream =NULL ;if (i >1 )pr_verbose (LOG_DEFAULT ,"\n" ); - fd =btrfs_open_dir (argv[i], &dirstream,1 ); + fd =btrfs_open_dir_fd (argv[i]);if (fd <0 ) { ret =1 ;break ;
@@ -913,7 +910,7 @@ static int cmd_device_usage(const struct cmd_struct *cmd, int argc, char **argv) ret =_cmd_device_usage (fd, argv[i], unit_mode);btrfs_warn_multiple_profiles (fd); -close_file_or_dir (fd, dirstream); +close (fd);if (ret)break ;
cmds/filesystem-usage.c +2 -3
@@ -1232,11 +1232,10 @@ static int cmd_filesystem_usage(const struct cmd_struct *cmd,for (i = optind; i < argc; i++) {int fd; -DIR *dirstream =NULL ;struct array chunkinfos = {0 };struct array devinfos = {0 }; - fd =btrfs_open_dir (argv[i], &dirstream,1 ); + fd =btrfs_open_dir_fd (argv[i]);if (fd <0 ) { ret =1 ; goto out;
@@ -1256,7 +1255,7 @@ static int cmd_filesystem_usage(const struct cmd_struct *cmd, ret =print_filesystem_usage_by_chunk (fd, &chunkinfos, &devinfos, argv[i], unit_mode, tabular); cleanup: -close_file_or_dir (fd, dirstream); +close (fd);array_free_elements (&chunkinfos);array_free (&chunkinfos);array_free_elements (&devinfos);
cmds/filesystem.c +6 -8
@@ -147,7 +147,6 @@ static int cmd_filesystem_df(const struct cmd_struct *cmd,int ret;int fd;char *path; -DIR *dirstream =NULL ;unsigned unit_mode; unit_mode =get_unit_mode_from_arg (&argc, argv,1 );
@@ -159,7 +158,7 @@ static int cmd_filesystem_df(const struct cmd_struct *cmd, path = argv[optind]; - fd =btrfs_open_dir (path, &dirstream,1 ); + fd =btrfs_open_dir_fd (path);if (fd <0 )return 1 ;
@@ -177,7 +176,7 @@ static int cmd_filesystem_df(const struct cmd_struct *cmd, }btrfs_warn_multiple_profiles (fd); -close_file_or_dir (fd, dirstream); +close (fd);return !!ret; } static DEFINE_COMMAND_WITH_FLAGS(filesystem_df, "df", CMD_FORMAT_JSON);
@@ -1362,7 +1361,6 @@ static int cmd_filesystem_resize(const struct cmd_struct *cmd,struct btrfs_ioctl_vol_args args;int fd, res, len, e;char *amount, *path; -DIR *dirstream =NULL ;u64 devid;int ret;bool enqueue = false;
@@ -1400,7 +1398,7 @@ static int cmd_filesystem_resize(const struct cmd_struct *cmd, cancel = (strcmp ("cancel" , amount) ==0 ); - fd =btrfs_open_dir (path, &dirstream,1 ); + fd =btrfs_open_dir_fd (path);if (fd <0 ) {/* The path is a directory */ if (fd ==-3 ) {
@@ -1423,14 +1421,14 @@ static int cmd_filesystem_resize(const struct cmd_struct *cmd,if (ret <0 )error ("unable to check status of exclusive operation: %m" ); -close_file_or_dir (fd, dirstream); +close (fd);return 1 ; } } ret =check_resize_args (amount, path, &devid);if (ret !=0 ) { -close_file_or_dir (fd, dirstream); +close (fd);return 1 ; }
@@ -1445,7 +1443,7 @@ static int cmd_filesystem_resize(const struct cmd_struct *cmd,pr_verbose (LOG_VERBOSE ,"adjust resize argument to: %s\n" , args.name); res =ioctl (fd,BTRFS_IOC_RESIZE , &args); e = errno; -close_file_or_dir (fd, dirstream); +close (fd);if ( res <0 ){switch (e) {case EFBIG :
cmds/inspect.c +10 -15
@@ -109,7 +109,6 @@ static int cmd_inspect_inode_resolve(const struct cmd_struct *cmd, {int fd;int ret; -DIR *dirstream =NULL ; optind =0 ;while (1 ) {
@@ -129,12 +128,12 @@ static int cmd_inspect_inode_resolve(const struct cmd_struct *cmd,if (check_argc_exact (argc - optind,2 ))return 1 ; - fd =btrfs_open_dir (argv[optind +1 ], &dirstream,1 ); + fd =btrfs_open_dir_fd (argv[optind +1 ]);if (fd <0 )return 1 ; ret =__ino_to_path_fd (arg_strtou64 (argv[optind]), fd, argv[optind+1 ]); -close_file_or_dir (fd, dirstream); +close (fd);return !!ret; }
@@ -169,7 +168,6 @@ static int cmd_inspect_logical_resolve(const struct cmd_struct *cmd,u64 size =SZ_64K ;char full_path[PATH_MAX ];char *path_ptr; -DIR *dirstream =NULL ;u64 flags =0 ;unsigned long request =BTRFS_IOC_LOGICAL_INO ;
@@ -214,7 +212,7 @@ static int cmd_inspect_logical_resolve(const struct cmd_struct *cmd, loi.flags = flags; loi.inodes =ptr_to_u64 (inodes); - fd =btrfs_open_dir (argv[optind +1 ], &dirstream,1 ); + fd =btrfs_open_dir_fd (argv[optind +1 ]);if (fd <0 ) { ret =12 ; goto out;
@@ -247,7 +245,6 @@ static int cmd_inspect_logical_resolve(const struct cmd_struct *cmd,u64 offset = inodes->val[i+1 ];u64 root = inodes->val[i+2 ];int path_fd; -DIR *dirs =NULL ;if (getpath) {char mount_path[PATH_MAX ];
@@ -296,7 +293,7 @@ static int cmd_inspect_logical_resolve(const struct cmd_struct *cmd,strncpy (mount_path, mounted,PATH_MAX );free (mounted); - path_fd =btrfs_open_dir (mount_path, &dirs,1 ); + path_fd =btrfs_open_dir_fd (mount_path);if (path_fd <0 ) { ret = -ENOENT ; goto out;
@@ -304,7 +301,7 @@ static int cmd_inspect_logical_resolve(const struct cmd_struct *cmd, } ret =__ino_to_path_fd (inum, path_fd, mount_path);if (path_fd != fd) -close_file_or_dir (path_fd, dirs); +close (path_fd); }else {pr_verbose (LOG_DEFAULT ,"inode %llu offset %llu root %llu\n" , inum, offset, root);
@@ -312,7 +309,7 @@ static int cmd_inspect_logical_resolve(const struct cmd_struct *cmd, } out: -close_file_or_dir (fd, dirstream); +close (fd);free (inodes);return !!ret; }
@@ -331,14 +328,13 @@ static int cmd_inspect_subvolid_resolve(const struct cmd_struct *cmd,int fd =-1 ;u64 subvol_id;char path[PATH_MAX ]; -DIR *dirstream =NULL ;clean_args_no_options (cmd, argc, argv);if (check_argc_exact (argc - optind,2 ))return 1 ; - fd =btrfs_open_dir (argv[optind +1 ], &dirstream,1 ); + fd =btrfs_open_dir_fd (argv[optind +1 ]);if (fd <0 ) { ret = -ENOENT ; goto out;
@@ -356,7 +352,7 @@ static int cmd_inspect_subvolid_resolve(const struct cmd_struct *cmd,pr_verbose (LOG_DEFAULT ,"%s\n" , path); out: -close_file_or_dir (fd, dirstream); +close (fd);return !!ret; } static DEFINE_SIMPLE_COMMAND(inspect_subvolid_resolve, "subvolid-resolve");
@@ -655,7 +651,6 @@ static int cmd_inspect_min_dev_size(const struct cmd_struct *cmd, {int ret;int fd =-1 ; -DIR *dirstream =NULL ;u64 devid =1 ; optind =0 ;
@@ -682,14 +677,14 @@ static int cmd_inspect_min_dev_size(const struct cmd_struct *cmd,if (check_argc_exact (argc - optind,1 ))return 1 ; - fd =btrfs_open_dir (argv[optind], &dirstream,1 ); + fd =btrfs_open_dir_fd (argv[optind]);if (fd <0 ) { ret = -ENOENT ; goto out; } ret =print_min_dev_size (fd, devid); -close_file_or_dir (fd, dirstream); +close (fd); out:return !!ret; }
cmds/qgroup.c +12 -17
@@ -1741,7 +1741,6 @@ static int _cmd_qgroup_assign(const struct cmd_struct *cmd, int assign,bool rescan = true;char *path;struct btrfs_ioctl_qgroup_assign_args args; -DIR *dirstream =NULL ; optind =0 ;while (1 ) {
@@ -1786,7 +1785,7 @@ static int _cmd_qgroup_assign(const struct cmd_struct *cmd, int assign,error ("bad relation requested: %s" , path);return 1 ; } - fd =btrfs_open_dir (path, &dirstream,1 ); + fd =btrfs_open_dir_fd (path);if (fd <0 )return 1 ;
@@ -1795,7 +1794,7 @@ static int _cmd_qgroup_assign(const struct cmd_struct *cmd, int assign,error ("unable to assign quota group: %s" , errno ==ENOTCONN ?"quota not enabled" :strerror (errno)); -close_file_or_dir (fd, dirstream); +close (fd);return 1 ; }
@@ -1821,7 +1820,7 @@ static int _cmd_qgroup_assign(const struct cmd_struct *cmd, int assign, ret =0 ; } } -close_file_or_dir (fd, dirstream); +close (fd);return ret; }
@@ -1831,7 +1830,6 @@ static int _cmd_qgroup_create(int create, int argc, char **argv)int fd;char *path;struct btrfs_ioctl_qgroup_create_args args; -DIR *dirstream =NULL ;if (check_argc_exact (argc - optind,2 ))return 1 ;
@@ -1845,12 +1843,12 @@ static int _cmd_qgroup_create(int create, int argc, char **argv) } path = argv[optind +1 ]; - fd =btrfs_open_dir (path, &dirstream,1 ); + fd =btrfs_open_dir_fd (path);if (fd <0 )return 1 ; ret =ioctl (fd,BTRFS_IOC_QGROUP_CREATE , &args); -close_file_or_dir (fd, dirstream); +close (fd);if (ret <0 ) { error("unable to %s quota group: %s", create ? "create":"destroy",
@@ -1958,7 +1956,6 @@ static int cmd_qgroup_show(const struct cmd_struct *cmd, int argc, char **argv)char *path;int ret =0 ;int fd; -DIR *dirstream =NULL ;u64 qgroupid;int filter_flag =0 ;unsigned unit_mode;
@@ -2031,7 +2028,7 @@ static int cmd_qgroup_show(const struct cmd_struct *cmd, int argc, char **argv)return 1 ; path = argv[optind]; - fd =btrfs_open_dir (path, &dirstream,1 ); + fd =btrfs_open_dir_fd (path);if (fd <0 ) {free (filter_set);free (comparer_set);
@@ -2049,7 +2046,7 @@ static int cmd_qgroup_show(const struct cmd_struct *cmd, int argc, char **argv)if (ret <0 ) { errno = -ret;error ("cannot resolve rootid for %s: %m" , path); -close_file_or_dir (fd, dirstream); +close (fd); goto out; }if (filter_flag &0x1 )
@@ -2062,7 +2059,7 @@ static int cmd_qgroup_show(const struct cmd_struct *cmd, int argc, char **argv) qgroupid); } ret =show_qgroups (fd, filter_set, comparer_set); -close_file_or_dir (fd, dirstream); +close (fd);free (filter_set);free (comparer_set);
@@ -2089,7 +2086,6 @@ static int cmd_qgroup_limit(const struct cmd_struct *cmd, int argc, char **argv)unsigned long long size;bool compressed = false;bool exclusive = false; -DIR *dirstream =NULL ;enum btrfs_util_error err; optind =0 ;
@@ -2147,12 +2143,12 @@ static int cmd_qgroup_limit(const struct cmd_struct *cmd, int argc, char **argv) }else usage (cmd,1 ); - fd =btrfs_open_dir (path, &dirstream,1 ); + fd =btrfs_open_dir_fd (path);if (fd <0 )return 1 ; ret =ioctl (fd,BTRFS_IOC_QGROUP_LIMIT , &args); -close_file_or_dir (fd, dirstream); +close (fd);if (ret <0 ) { error("unable to limit requested quota group: %s", errno == ENOTCONN ? "quota not enabled"
@@ -2180,7 +2176,6 @@ static int cmd_qgroup_clear_stale(const struct cmd_struct *cmd, int argc, char *int ret =0 ;int fd;char *path =NULL ; -DIR *dirstream =NULL ;struct qgroup_lookup qgroup_lookup;struct rb_node *node;struct btrfs_qgroup *entry;
@@ -2190,7 +2185,7 @@ static int cmd_qgroup_clear_stale(const struct cmd_struct *cmd, int argc, char * path = argv[optind]; - fd =btrfs_open_dir (path, &dirstream,1 ); + fd =btrfs_open_dir_fd (path);if (fd <0 )return 1 ;
@@ -2226,7 +2221,7 @@ static int cmd_qgroup_clear_stale(const struct cmd_struct *cmd, int argc, char * } out: -close_file_or_dir (fd, dirstream); +close (fd);return !!ret; } static DEFINE_SIMPLE_COMMAND(qgroup_clear_stale, "clear-stale");
cmds/quota.c +7 -9
@@ -41,17 +41,16 @@ static int quota_ctl(int cmd, char *path)int ret =0 ;int fd;struct btrfs_ioctl_quota_ctl_args args; -DIR *dirstream =NULL ;memset (&args,0 ,sizeof (args)); args.cmd = cmd; - fd =btrfs_open_dir (path, &dirstream,1 ); + fd =btrfs_open_dir_fd (path);if (fd <0 )return 1 ; ret =ioctl (fd,BTRFS_IOC_QUOTA_CTL , &args); -close_file_or_dir (fd, dirstream); +close (fd);if (ret <0 ) {error ("quota command failed: %m" );return 1 ;
@@ -148,7 +147,6 @@ static int cmd_quota_rescan(const struct cmd_struct *cmd, int argc, char **argv)char *path =NULL ;struct btrfs_ioctl_quota_rescan_args args;unsigned long ioctlnum =BTRFS_IOC_QUOTA_RESCAN ; -DIR *dirstream =NULL ;bool wait_for_completion = false; optind =0 ;
@@ -193,7 +191,7 @@ static int cmd_quota_rescan(const struct cmd_struct *cmd, int argc, char **argv)memset (&args,0 ,sizeof (args)); path = argv[optind]; - fd =btrfs_open_dir (path, &dirstream,1 ); + fd =btrfs_open_dir_fd (path);if (fd <0 )return 1 ;
@@ -203,7 +201,7 @@ static int cmd_quota_rescan(const struct cmd_struct *cmd, int argc, char **argv) }if (ioctlnum ==BTRFS_IOC_QUOTA_RESCAN_STATUS ) { -close_file_or_dir (fd, dirstream); +close (fd);if (ret <0 ) {error ("could not obtain quota rescan status: %m" );return 1 ;
@@ -221,7 +219,7 @@ static int cmd_quota_rescan(const struct cmd_struct *cmd, int argc, char **argv)fflush (stdout); }else if (ret <0 && (!wait_for_completion || e !=EINPROGRESS )) {error ("quota rescan failed: %m" ); -close_file_or_dir (fd, dirstream); +close (fd);return 1 ; }
@@ -230,12 +228,12 @@ static int cmd_quota_rescan(const struct cmd_struct *cmd, int argc, char **argv) e = errno;if (ret <0 ) {error ("quota rescan wait failed: %m" ); -close_file_or_dir (fd, dirstream); +close (fd);return 1 ; } } -close_file_or_dir (fd, dirstream); +close (fd);return 0 ; } static DEFINE_SIMPLE_COMMAND(quota_rescan, "rescan");
cmds/replace.c +4 -6
@@ -378,7 +378,6 @@ static int cmd_replace_status(const struct cmd_struct *cmd,char *path;int once =0 ;int ret; -DIR *dirstream =NULL ; optind =0 ;while ((c =getopt (argc, argv,"1" )) !=-1 ) {
@@ -395,12 +394,12 @@ static int cmd_replace_status(const struct cmd_struct *cmd,return 1 ; path = argv[optind]; - fd =btrfs_open_dir (path, &dirstream,1 ); + fd =btrfs_open_dir_fd (path);if (fd <0 )return 1 ; ret =print_replace_status (fd, path, once); -close_file_or_dir (fd, dirstream); +close (fd);return !!ret; } static DEFINE_SIMPLE_COMMAND(replace_status, "status");
@@ -546,7 +545,6 @@ static int cmd_replace_cancel(const struct cmd_struct *cmd,int c;int fd;char *path; -DIR *dirstream =NULL ; optind =0 ;while ((c =getopt (argc, argv,"" )) !=-1 ) {
@@ -561,14 +559,14 @@ static int cmd_replace_cancel(const struct cmd_struct *cmd,return 1 ; path = argv[optind]; - fd =btrfs_open_dir (path, &dirstream,1 ); + fd =btrfs_open_dir_fd (path);if (fd <0 )return 1 ; args.cmd =BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL ; args.result =BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_RESULT ; ret =ioctl (fd,BTRFS_IOC_DEV_REPLACE , &args); -close_file_or_dir (fd, dirstream); +close (fd);if (ret <0 ) {error ("ioctl(DEV_REPLACE_CANCEL) failed on \"%s\": %m" , path);if (args.result !=BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_RESULT )
cmds/subvolume-list.c +2 -3
@@ -1587,7 +1587,6 @@ static int cmd_subvolume_list(const struct cmd_struct *cmd, int argc, char **argchar *subvol;bool is_list_all = false;bool is_only_in_path = false; -DIR *dirstream =NULL ;enum btrfs_list_layout layout =BTRFS_LIST_LAYOUT_DEFAULT ; filter_set =btrfs_list_alloc_filter_set ();
@@ -1689,7 +1688,7 @@ static int cmd_subvolume_list(const struct cmd_struct *cmd, int argc, char **arg goto out; subvol = argv[optind]; - fd =btrfs_open_dir (subvol, &dirstream,1 ); + fd =btrfs_open_dir_fd (subvol);if (fd <0 ) { ret =-1 ;error ("can't access '%s'" , subvol);
@@ -1729,7 +1728,7 @@ static int cmd_subvolume_list(const struct cmd_struct *cmd, int argc, char **arg layout, !is_list_all && !is_only_in_path, NULL); out: -close_file_or_dir (fd, dirstream); +close (fd);if (filter_set)free (filter_set);if (comparer_set)
cmds/subvolume.c +13 -18
@@ -150,7 +150,6 @@ static int create_one_subvolume(const char *dst, struct btrfs_qgroup_inherit *inchar *dupdir =NULL ;char *newname;char *dstdir; -DIR *dirstream =NULL ; ret =path_is_dir (dst);if (ret <0 && ret != -ENOENT ) {
@@ -224,7 +223,7 @@ static int create_one_subvolume(const char *dst, struct btrfs_qgroup_inherit *in } } - fddst =btrfs_open_dir (dstdir, &dirstream,1 ); + fddst =btrfs_open_dir_fd (dstdir);if (fddst <0 ) { ret = fddst; goto out;
@@ -256,7 +255,7 @@ static int create_one_subvolume(const char *dst, struct btrfs_qgroup_inherit *in } out: -close_file_or_dir (fddst, dirstream); +close (fddst);free (dupname);free (dupdir);
@@ -684,7 +683,6 @@ static int cmd_subvolume_snapshot(const struct cmd_struct *cmd, int argc, char *enum btrfs_util_error err;struct btrfs_ioctl_vol_args_v2 args;struct btrfs_qgroup_inherit *inherit =NULL ; -DIR *dirstream1 =NULL , *dirstream2 =NULL ;memset (&args,0 ,sizeof (args)); optind =0 ;
@@ -771,11 +769,11 @@ static int cmd_subvolume_snapshot(const struct cmd_struct *cmd, int argc, char * goto out; } - fddst =btrfs_open_dir (dstdir, &dirstream1,1 ); + fddst =btrfs_open_dir_fd (dstdir);if (fddst <0 ) goto out; - fd =btrfs_open_dir (subvol, &dirstream2,1 ); + fd =btrfs_open_dir_fd (subvol);if (fd <0 ) goto out;
@@ -810,8 +808,8 @@ static int cmd_subvolume_snapshot(const struct cmd_struct *cmd, int argc, char * retval =0 ;/* success */ out: -close_file_or_dir (fddst, dirstream1); -close_file_or_dir (fd, dirstream2); +close (fddst); +close (fd);free (inherit);free (dupname);free (dupdir);
@@ -835,7 +833,6 @@ static int cmd_subvolume_get_default(const struct cmd_struct *cmd, int argc, chaint fd =-1 ;int ret =1 ;uint64_t default_id; -DIR *dirstream =NULL ;enum btrfs_util_error err;struct btrfs_util_subvolume_info subvol;struct format_ctx fctx;
@@ -846,7 +843,7 @@ static int cmd_subvolume_get_default(const struct cmd_struct *cmd, int argc, chaif (check_argc_exact (argc - optind,1 ))return 1 ; - fd =btrfs_open_dir (argv[1 ], &dirstream,1 ); + fd =btrfs_open_dir_fd (argv[1 ]);if (fd <0 )return 1 ;
@@ -898,7 +895,7 @@ static int cmd_subvolume_get_default(const struct cmd_struct *cmd, int argc, cha ret =0 ; out: -close_file_or_dir (fd, dirstream); +close (fd);return ret; }#if EXPERIMENTAL
@@ -1369,7 +1366,6 @@ static int cmd_subvolume_find_new(const struct cmd_struct *cmd, int argc, char *int ret;char *subvol;u64 last_gen; -DIR *dirstream =NULL ;enum btrfs_util_error err;clean_args_no_options (cmd, argc, argv);
@@ -1386,19 +1382,19 @@ static int cmd_subvolume_find_new(const struct cmd_struct *cmd, int argc, char *return 1 ; } - fd =btrfs_open_dir (subvol, &dirstream,1 ); + fd =btrfs_open_dir_fd (subvol);if (fd <0 )return 1 ; err =btrfs_util_sync_fd (fd);if (err) {error_btrfs_util (err); -close_file_or_dir (fd, dirstream); +close (fd);return 1 ; } ret =btrfs_list_find_updated_files (fd,0 , last_gen); -close_file_or_dir (fd, dirstream); +close (fd);return !!ret; } static DEFINE_SIMPLE_COMMAND(subvolume_find_new, "find-new");
@@ -1795,7 +1791,6 @@ static int cmd_subvolume_sync(const struct cmd_struct *cmd, int argc, char **arg {int fd =-1 ;int ret =1 ; -DIR *dirstream =NULL ;uint64_t *ids =NULL ;size_t id_count, i;int sleep_interval =1 ;
@@ -1825,7 +1820,7 @@ static int cmd_subvolume_sync(const struct cmd_struct *cmd, int argc, char **argif (check_argc_min (argc - optind,1 ))return 1 ; - fd =btrfs_open_dir (argv[optind], &dirstream,1 ); + fd =btrfs_open_dir_fd (argv[optind]);if (fd <0 ) { ret =1 ; goto out;
@@ -1878,7 +1873,7 @@ static int cmd_subvolume_sync(const struct cmd_struct *cmd, int argc, char **arg out:free (ids); -close_file_or_dir (fd, dirstream); +close (fd);return !!ret; }
Signature · 1 line
2.43.0
PATCH v2 3/9 Killing dirstream: replace btrfs_open_dir with btrfs_open_dir_fd
In reply to Goffredo BaroncelliFrom: Goffredo Baroncelli <[email protected]>
For historical reason the helpers [btrfs_]open_dir... return also the 'DIR *dirstream' value when a dir is opened.
However this is never used. So avoid calling diropen() and return only the fd.
This patch replaces the last btrfs_open_dir() call with btrfs_open_dir_fd() removing any reference to the unused/useless dirstream variables.
At the same time this patch updates the add_seen_fsid() function removing any reference to dir stream (again this is never used).
--- cmds/filesystem.c | 4 ++-- cmds/subvolume.c | 8 +++----- common/device-scan.c | 6 ++---- common/device-scan.h | 4 +--- 4 files changed, 8 insertions(+), 14 deletions(-)
cmds/filesystem.c +2 -2
@@ -307,7 +307,7 @@ static void print_one_uuid(struct btrfs_fs_devices *fs_devices,u64 devs_found =0 ;u64 total; -if (add_seen_fsid (fs_devices->fsid, seen_fsid_hash,-1 ,NULL )) +if (add_seen_fsid (fs_devices->fsid, seen_fsid_hash,-1 ))return ;uuid_unparse (fs_devices->fsid, uuidbuf);
@@ -352,7 +352,7 @@ static int print_one_fs(struct btrfs_ioctl_fs_info_args *fs_info,struct btrfs_ioctl_dev_info_args *tmp_dev_info;int ret; - ret =add_seen_fsid (fs_info->fsid, seen_fsid_hash,-1 ,NULL ); + ret =add_seen_fsid (fs_info->fsid, seen_fsid_hash,-1 );if (ret == -EEXIST )return 0 ;else if (ret)
cmds/subvolume.c +3 -5
@@ -374,7 +374,6 @@ static int cmd_subvolume_delete(const struct cmd_struct *cmd, int argc, char **achar *dupdname =NULL ;char *dupvname =NULL ;char *path =NULL ; -DIR *dirstream =NULL ;int commit_mode =0 ;bool subvol_path_not_found = false;u8 fsid[BTRFS_FSID_SIZE ];
@@ -498,7 +497,7 @@ again:if (subvolid >0 ) dname = dupvname; - fd =btrfs_open_dir (dname, &dirstream,1 ); + fd =btrfs_open_dir_fd (dname);if (fd <0 ) { ret =1 ; goto out;
@@ -592,7 +591,7 @@ again: goto out; } -if (add_seen_fsid (fsid, seen_fsid_hash, fd, dirstream) ==0 ) { +if (add_seen_fsid (fsid, seen_fsid_hash, fd) ==0 ) {uuid_unparse (fsid, uuidbuf);pr_verbose (LOG_INFO ," new fs is found for '%s', fsid: %s\n" , path, uuidbuf);
@@ -606,10 +605,9 @@ again: } out: -close_file_or_dir (fd, dirstream); +close (fd); keep_fd: fd =-1 ; - dirstream =NULL ;free (dupdname);free (dupvname); dupdname =NULL ;
common/device-scan.c +2 -4
@@ -313,8 +313,7 @@ int is_seen_fsid(u8 *fsid, struct seen_fsid *seen_fsid_hash[])return 0 ; } -int add_seen_fsid (u8 *fsid,struct seen_fsid *seen_fsid_hash[], -int fd,DIR *dirstream) +int add_seen_fsid (u8 *fsid,struct seen_fsid *seen_fsid_hash[],int fd) {u8 hash = fsid[0 ];int slot = hash %SEEN_FSID_HASH_SIZE ;
@@ -342,7 +341,6 @@ insert: alloc->next =NULL ;memcpy (alloc->fsid, fsid,BTRFS_FSID_SIZE ); alloc->fd = fd; - alloc->dirstream = dirstream;if (seen) seen->next = alloc;
@@ -362,7 +360,7 @@ void free_seen_fsid(struct seen_fsid *seen_fsid_hash[]) seen = seen_fsid_hash[slot];while (seen) { next = seen->next; -close_file_or_dir (seen->fd, seen->dirstream); +close (seen->fd);free (seen); seen = next; }
common/device-scan.h +1 -3
@@ -41,7 +41,6 @@ struct btrfs_trans_handle;struct seen_fsid {u8 fsid[BTRFS_FSID_SIZE ];struct seen_fsid *next; -DIR *dirstream;int fd; };
@@ -56,8 +55,7 @@ int btrfs_add_to_fsid(struct btrfs_trans_handle *trans,int btrfs_device_already_in_root (struct btrfs_root *root,int fd,int super_offset);int is_seen_fsid (u8 *fsid,struct seen_fsid *seen_fsid_hash[]); -int add_seen_fsid (u8 *fsid,struct seen_fsid *seen_fsid_hash[], -int fd,DIR *dirstream); +int add_seen_fsid (u8 *fsid,struct seen_fsid *seen_fsid_hash[],int fd);void free_seen_fsid (struct seen_fsid *seen_fsid_hash[]);int test_uuid_unique (const char *uuid_str);
Signature · 1 line
2.43.0
PATCH v2 4/9 Killing dirstream: replace open_path_or_dev_mnt with btrfs_open_mnt_fd
In reply to Goffredo BaroncelliFrom: Goffredo Baroncelli <[email protected]>
For historical reason the helpers [btrfs_]open_dir... return also the 'DIR *dirstream' value when a dir is opened.
However this is never used. So avoid calling diropen() and return only the fd.
This patch replace open_path_or_dev_mnt() with btrfs_open_mnt_fd() removing any reference to the unused/useless dirstream variables.
--- cmds/device.c | 5 ++--- cmds/replace.c | 7 +++---- cmds/scrub.c | 15 ++++++--------- 3 files changed, 11 insertions(+), 16 deletions(-)
cmds/device.c +2 -3
@@ -717,7 +717,6 @@ static int cmd_device_stats(const struct cmd_struct *cmd, int argc, char **argv)bool free_table = false;bool tabular = false;__u64 flags =0 ; -DIR *dirstream =NULL ;struct format_ctx fctx; optind =0 ;
@@ -753,7 +752,7 @@ static int cmd_device_stats(const struct cmd_struct *cmd, int argc, char **argv) dev_path = argv[optind]; - fdmnt =open_path_or_dev_mnt (dev_path, &dirstream,1 ); + fdmnt =btrfs_open_mnt_fd (dev_path, true);if (fdmnt <0 )return 1 ;
@@ -839,7 +838,7 @@ static int cmd_device_stats(const struct cmd_struct *cmd, int argc, char **argv) out:free (di_args); -close_file_or_dir (fdmnt, dirstream); +close (fdmnt);if (free_table)table_free (table);
cmds/replace.c +3 -4
@@ -136,7 +136,6 @@ static int cmd_replace_start(const struct cmd_struct *cmd,bool force_using_targetdev = false;u64 dstdev_block_count;bool do_not_background = false; -DIR *dirstream =NULL ;u64 srcdev_size;u64 dstdev_size;bool enqueue = false;
@@ -184,7 +183,7 @@ static int cmd_replace_start(const struct cmd_struct *cmd,return 1 ; path = argv[optind +2 ]; - fdmnt =open_path_or_dev_mnt (path, &dirstream,1 ); + fdmnt =btrfs_open_mnt_fd (path, true);if (fdmnt <0 ) goto leave_with_error;
@@ -200,7 +199,7 @@ static int cmd_replace_start(const struct cmd_struct *cmd,if (ret !=0 ) {if (ret <0 )error ("unable to check status of exclusive operation: %m" ); -close_file_or_dir (fdmnt, dirstream); +close (fdmnt); goto leave_with_error; }
@@ -348,7 +347,7 @@ static int cmd_replace_start(const struct cmd_struct *cmd, goto leave_with_error; } } -close_file_or_dir (fdmnt, dirstream); +close (fdmnt);return 0 ; leave_with_error:
cmds/scrub.c +6 -9
@@ -1269,7 +1269,6 @@ static int scrub_start(const struct cmd_struct *cmd, int argc, char **argv,pthread_mutex_t spc_write_mutex =PTHREAD_MUTEX_INITIALIZER ;void *terr;u64 devid; -DIR *dirstream =NULL ;bool force = false;bool nothing_to_resume = false;
@@ -1326,7 +1325,7 @@ static int scrub_start(const struct cmd_struct *cmd, int argc, char **argv, path = argv[optind]; - fdmnt =open_path_or_dev_mnt (path, &dirstream, !do_quiet); + fdmnt =btrfs_open_mnt_fd (path, !do_quiet);if (fdmnt <0 )return 1 ;
@@ -1698,7 +1697,7 @@ out:if (sock_path[0 ])unlink (sock_path); } -close_file_or_dir (fdmnt, dirstream); +close (fdmnt);if (err)return 1 ;
@@ -1751,7 +1750,6 @@ static int cmd_scrub_cancel(const struct cmd_struct *cmd, int argc, char **argv)char *path;int ret;int fdmnt =-1 ; -DIR *dirstream =NULL ;clean_args_no_options (cmd, argc, argv);
@@ -1760,7 +1758,7 @@ static int cmd_scrub_cancel(const struct cmd_struct *cmd, int argc, char **argv) path = argv[optind]; - fdmnt =open_path_or_dev_mnt (path, &dirstream,1 ); + fdmnt =btrfs_open_mnt_fd (path, true);if (fdmnt <0 ) { ret =1 ; goto out;
@@ -1782,7 +1780,7 @@ static int cmd_scrub_cancel(const struct cmd_struct *cmd, int argc, char **argv)pr_verbose (LOG_DEFAULT ,"scrub cancelled\n" ); out: -close_file_or_dir (fdmnt, dirstream); +close (fdmnt);return ret; } static DEFINE_SIMPLE_COMMAND(scrub_cancel, "cancel");
@@ -1841,7 +1839,6 @@ static int cmd_scrub_status(const struct cmd_struct *cmd, int argc, char **argv)char fsid[BTRFS_UUID_UNPARSED_SIZE ];int fdres =-1 ;int err =0 ; -DIR *dirstream =NULL ; unit_mode =get_unit_mode_from_arg (&argc, argv,0 );
@@ -1864,7 +1861,7 @@ static int cmd_scrub_status(const struct cmd_struct *cmd, int argc, char **argv) path = argv[optind]; - fdmnt =open_path_or_dev_mnt (path, &dirstream,1 ); + fdmnt =btrfs_open_mnt_fd (path, true);if (fdmnt <0 )return 1 ;
@@ -1978,7 +1975,7 @@ out:free (si_args);if (fdres >-1 )close (fdres); -close_file_or_dir (fdmnt, dirstream); +close (fdmnt);return !!err; }
Signature · 1 line
2.43.0
PATCH v2 5/9 Killing dirstream: replace open_file_or_dir3 with btrfs_open_fd2
In reply to Goffredo BaroncelliFrom: Goffredo Baroncelli <[email protected]>
For historical reason the helpers [btrfs_]open_dir... return also the 'DIR *dirstream' value when a dir is opened.
However this is never used. So avoid calling diropen() and return only the fd.
This patch replace open_file_or_dir3() with btrfs_open_fd2() removing any reference to the unused/useless dirstream variables. btrfs_open_fd2() is needed because sometime the caller need to set the RDONLY/RDWRITE mode, and to avoid spourios diagnosis messages.
--- cmds/filesystem.c | 8 +++----- cmds/property.c | 5 ++--- 2 files changed, 5 insertions(+), 8 deletions(-)
cmds/filesystem.c +3 -5
@@ -1004,7 +1004,6 @@ static int cmd_filesystem_defrag(const struct cmd_struct *cmd,bool recursive = false;int ret =0 ;int compress_type =BTRFS_COMPRESS_NONE ; -DIR *dirstream;/* * Kernel 4.19+ supports defragmention of files open read-only,
@@ -1142,8 +1141,7 @@ static int cmd_filesystem_defrag(const struct cmd_struct *cmd,struct stat st;int defrag_err =0 ; - dirstream =NULL ; - fd =open_file_or_dir3 (argv[i], &dirstream, defrag_open_mode); + fd =btrfs_open_fd2 (argv[i], false, defrag_open_mode==O_RDWR , false);if (fd <0 ) {error ("cannot open %s: %m" , argv[i]); ret = -errno;
@@ -1177,7 +1175,7 @@ static int cmd_filesystem_defrag(const struct cmd_struct *cmd,error ("defrag range ioctl not supported in this kernel version, 2.6.33 and newer is required" ); defrag_global_errors++; -close_file_or_dir (fd, dirstream); +close (fd);break ; }if (ret) {
@@ -1189,7 +1187,7 @@ static int cmd_filesystem_defrag(const struct cmd_struct *cmd, next:if (ret) defrag_global_errors++; -close_file_or_dir (fd, dirstream); +close (fd); }if (defrag_global_errors)
cmds/property.c +2 -3
@@ -175,12 +175,11 @@ static int prop_compression(enum prop_object_type type,int ret;ssize_t sret;int fd =-1 ; -DIR *dirstream =NULL ;char *buf =NULL ;char *xattr_name =NULL ;int open_flags = value ?O_RDWR :O_RDONLY ; - fd =open_file_or_dir3 (object, &dirstream, open_flags); + fd =btrfs_open_fd2 (object, false, open_flags ==O_RDWR , false);if (fd ==-1 ) { ret = -errno;error ("failed to open %s: %m" , object);
@@ -232,7 +231,7 @@ out:free (xattr_name);free (buf);if (fd >=0 ) -close_file_or_dir (fd, dirstream); +close (fd);return ret; }
Signature · 1 line
2.43.0
PATCH v2 6/9 Killing dirstream: replace btrfs_open_file_or_dir with btrfs_open_file_or_dir_fd
In reply to Goffredo BaroncelliFrom: Goffredo Baroncelli <[email protected]>
For historical reason the helpers [btrfs_]open_dir... return also the 'DIR *dirstream' value when a dir is opened.
However this is never used. So avoid calling diropen() and return only the fd.
This patch replace btrfs_open_file_or_dir() with btrfs_open_file_or_dir_fd() removing any reference to the unused/useless dirstream variables.
--- cmds/inspect.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
cmds/inspect.c +2 -3
@@ -369,14 +369,13 @@ static int cmd_inspect_rootid(const struct cmd_struct *cmd,int ret;int fd =-1 ;u64 rootid; -DIR *dirstream =NULL ;clean_args_no_options (cmd, argc, argv);if (check_argc_exact (argc - optind,1 ))return 1 ; - fd =btrfs_open_file_or_dir (argv[optind], &dirstream,1 ); + fd =btrfs_open_file_or_dir_fd (argv[optind]);if (fd <0 ) { ret = -ENOENT ; goto out;
@@ -391,7 +390,7 @@ static int cmd_inspect_rootid(const struct cmd_struct *cmd,pr_verbose (LOG_DEFAULT ,"%llu\n" , rootid); out: -close_file_or_dir (fd, dirstream); +close (fd);return !!ret; }
Signature · 1 line
2.43.0
PATCH v2 7/9 Killing dirstream: replace open_file_or_dir with btrfs_open_fd2
In reply to Goffredo BaroncelliFrom: Goffredo Baroncelli <[email protected]>
For historical reason the helpers [btrfs_]open_dir... return also the 'DIR *dirstream' value when a dir is opened.
However this is never used. So avoid calling diropen() and return only the fd.
This patch replace open_file_or_dir() with btrfs_open_fd2() removing any reference to the unused/useless dirstream variables. btrfs_open_fd2() is required to avoid spourious error messages.
--- cmds/inspect.c | 5 ++--- cmds/scrub.c | 5 ++--- cmds/subvolume.c | 5 ++--- common/utils.c | 5 ++--- 4 files changed, 8 insertions(+), 12 deletions(-)
cmds/inspect.c +2 -3
@@ -1020,7 +1020,6 @@ static int cmd_inspect_list_chunks(const struct cmd_struct *cmd,int ret;int fd;int i; -DIR *dirstream =NULL ;unsigned unit_mode;char *sortmode =NULL ;bool with_usage = true;
@@ -1083,7 +1082,7 @@ static int cmd_inspect_list_chunks(const struct cmd_struct *cmd, path = argv[optind]; - fd =open_file_or_dir (path, &dirstream); + fd =btrfs_open_fd2 (path, false, true, false);if (fd <0 ) {error ("cannot access '%s': %m" , path);return 1 ;
@@ -1187,7 +1186,7 @@ static int cmd_inspect_list_chunks(const struct cmd_struct *cmd, } ret =print_list_chunks (&ctx, sortmode, unit_mode, with_usage, with_empty); -close_file_or_dir (fd, dirstream); +close (fd); out_nomem:free (ctx.stats);
cmds/scrub.c +2 -3
@@ -1999,7 +1999,6 @@ static int cmd_scrub_limit(const struct cmd_struct *cmd, int argc, char **argv)struct string_table *table =NULL ;int ret;int fd =-1 ; -DIR *dirstream =NULL ;int cols, idx;u64 opt_devid =0 ;bool devid_set = false;
@@ -2060,7 +2059,7 @@ static int cmd_scrub_limit(const struct cmd_struct *cmd, int argc, char **argv)return 1 ; } - fd =open_file_or_dir (argv[optind], &dirstream); + fd =btrfs_open_fd2 (argv[optind], false, true, false);if (fd <0 )return 1 ;
@@ -2182,7 +2181,7 @@ static int cmd_scrub_limit(const struct cmd_struct *cmd, int argc, char **argv) out:if (table)table_free (table); -close_file_or_dir (fd, dirstream); +close (fd);return !!ret; }
cmds/subvolume.c +2 -3
@@ -1566,7 +1566,6 @@ static int cmd_subvolume_show(const struct cmd_struct *cmd, int argc, char **argchar *fullpath =NULL ;int fd =-1 ;int ret =1 ; -DIR *dirstream1 =NULL ;int by_rootid =0 ;int by_uuid =0 ;u64 rootid_arg =0 ;
@@ -1624,7 +1623,7 @@ static int cmd_subvolume_show(const struct cmd_struct *cmd, int argc, char **arg goto out; } - fd =open_file_or_dir (fullpath, &dirstream1); + fd =btrfs_open_fd2 (fullpath, false, true, false);if (fd <0 ) {error ("can't access '%s'" , fullpath); goto out;
@@ -1762,7 +1761,7 @@ out2: out:free (subvol_path); -close_file_or_dir (fd, dirstream1); +close (fd);free (fullpath);return !!ret; }
common/utils.c +2 -3
@@ -211,7 +211,6 @@ int get_fs_info(const char *path, struct btrfs_ioctl_fs_info_args *fi_args,struct btrfs_ioctl_dev_info_args *di_args;struct btrfs_ioctl_dev_info_args tmp;char mp[PATH_MAX ]; -DIR *dirstream =NULL ;memset (fi_args,0 ,sizeof (*fi_args));
@@ -251,7 +250,7 @@ int get_fs_info(const char *path, struct btrfs_ioctl_fs_info_args *fi_args, }/* at this point path must not be for a block device */ - fd =open_file_or_dir (path, &dirstream); + fd =btrfs_open_fd2 (path, false, true, false);if (fd <0 ) { ret = -errno; goto out;
@@ -317,7 +316,7 @@ int get_fs_info(const char *path, struct btrfs_ioctl_fs_info_args *fi_args, } out: -close_file_or_dir (fd, dirstream); +close (fd);return ret; }
Signature · 1 line
2.43.0
PATCH v2 8/9 Killing dirstream: remove open_file_or_dir3 from du_add_file
In reply to Goffredo BaroncelliFrom: Goffredo Baroncelli <[email protected]>
For historical reason the helpers [btrfs_]open_dir... return also the 'DIR *dirstream' value when a dir is opened.
However this is never used. So avoid calling diropen() and return only the fd.
This patch replace the last reference to btrfs_open_file_or_dir3() with btrfs_open_fd2().
--- cmds/filesystem-du.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-)
cmds/filesystem-du.c +16 -2
@@ -456,7 +456,7 @@ static int du_add_file(const char *filename, int dirfd, ret =sprintf (pathp,"/%s" , filename); pathp += ret; - fd =open_file_or_dir3 (path, &dirstream,O_RDONLY ); + fd =btrfs_open_fd2 (path, false, false, false);if (fd <0 ) { ret = -errno; goto out;
@@ -489,6 +489,12 @@ static int du_add_file(const char *filename, int dirfd, }else if (S_ISDIR (st.st_mode)) {struct rb_root *root = shared_extents; + dirstream =fdopendir (fd); +if (!dirstream) { + ret = -errno; + goto out_close; + } +/* * We collect shared extents in an rb_root, the top * level caller will not pass a root down, so use the
@@ -542,7 +548,15 @@ static int du_add_file(const char *filename, int dirfd, *ret_shared = file_shared; out_close: -close_file_or_dir (fd, dirstream); +/* +* if dirstream is not NULL, it is derived by fd, so it is enough +* to close the former +*/ +if (dirstream) +closedir (dirstream); +else +close (fd); + out:/* reset path to just before this element */ pathp = pathtmp;
Signature · 1 line
2.43.0
PATCH v2 9/9 Killing dirstream: remove unused functions
In reply to Goffredo BaroncelliFrom: Goffredo Baroncelli <[email protected]>
Remove the following unused functions:
- btrfs_open_dir()
- open_file_or_dir()
- btrfs_open_file_or_dir()
- btrfs_open()
- open_path_or_dev_mnt()
- open_file_or_dir3()
- close_file_or_dir()
--- common/open-utils.c | 135 -------------------------------------------- common/open-utils.h | 10 ---- 2 files changed, 145 deletions(-)
common/open-utils.c +0 -135
@@ -183,141 +183,6 @@ out:return ret; } -/* -* Given a pathname, return a filehandle to: -* the original pathname or, -* if the pathname is a mounted btrfs device, to its mountpoint. -* -* On error, return -1, errno should be set. -*/ -int open_path_or_dev_mnt (const char *path,DIR **dirstream,int verbose) -{ -char mp[PATH_MAX ]; -int ret; - -if (path_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 ; - } - ret =open_file_or_dir (mp, dirstream); -error_on (verbose && ret <0 ,"can't access '%s': %m" , - path); - }else { - ret =btrfs_open_dir (path, dirstream,1 ); - } - -return ret; -} - -/* -* Do the following checks before calling open_file_or_dir(): -* 1: path is in a btrfs filesystem -* 2: path is a directory if dir_only is 1 -*/ -int btrfs_open (const char *path,DIR **dirstream,int verbose,int dir_only) -{ -struct statfs stfs; -struct stat st; -int ret; - -if (stat (path, &st) !=0 ) { -error_on (verbose,"cannot access '%s': %m" , path); -return -1 ; - } - -if (dir_only && !S_ISDIR (st.st_mode)) { -error_on (verbose,"not a directory: %s" , path); -return -3 ; - } - -if (statfs (path, &stfs) !=0 ) { -error_on (verbose,"cannot access '%s': %m" , path); -return -1 ; - } - -if (stfs.f_type !=BTRFS_SUPER_MAGIC ) { -error_on (verbose,"not a btrfs filesystem: %s" , path); -return -2 ; - } - - ret =open_file_or_dir (path, dirstream); -if (ret <0 ) { -error_on (verbose,"cannot access '%s': %m" , path); - } - -return ret; -} - -int btrfs_open_dir (const char *path,DIR **dirstream,int verbose) -{ -return btrfs_open (path, dirstream, verbose,1 ); -} - -int btrfs_open_file_or_dir (const char *path,DIR **dirstream,int verbose) -{ -return btrfs_open (path, dirstream, verbose,0 ); -} - -int open_file_or_dir3 (const char *fname,DIR **dirstream,int open_flags) -{ -int ret; -struct stat st; -int fd; - - ret =stat (fname, &st); -if (ret <0 ) { -return -1 ; - } -if (S_ISDIR (st.st_mode)) { - *dirstream =opendir (fname); -if (!*dirstream) -return -1 ; - fd =dirfd (*dirstream); - }else if (S_ISREG (st.st_mode) ||S_ISLNK (st.st_mode)) { - fd =open (fname, open_flags); - }else { -/* -* we set this on purpose, in case the caller output -* strerror(errno) as success -*/ - errno =EINVAL ; -return -1 ; - } -if (fd <0 ) { - fd =-1 ; -if (*dirstream) { -closedir (*dirstream); - *dirstream =NULL ; - } - } -return fd; -} - -int open_file_or_dir (const char *fname,DIR **dirstream) -{ -return open_file_or_dir3 (fname, dirstream,O_RDWR ); -} - -void close_file_or_dir (int fd,DIR *dirstream) -{ -int old_errno; - - old_errno = errno; -if (dirstream) { -closedir (dirstream); - }else if (fd >=0 ) { -close (fd); - } - - errno = old_errno; -} - -/* * Do the following checks before calling open: * 1: path is in a btrfs filesystem
common/open-utils.h +0 -10
@@ -28,16 +28,6 @@ int check_mounted_where(int fd, const char *file, char *where, int size, bool noscan);int check_mounted (const char * file);int get_btrfs_mount (const char *dev,char *mp,size_t mp_size); -int open_path_or_dev_mnt (const char *path,DIR **dirstream,int verbose); - -int open_file_or_dir3 (const char *fname,DIR **dirstream,int open_flags); -int open_file_or_dir (const char *fname,DIR **dirstream); - -int btrfs_open (const char *path,DIR **dirstream,int verbose,int dir_only); -int btrfs_open_dir (const char *path,DIR **dirstream,int verbose); -int btrfs_open_file_or_dir (const char *path,DIR **dirstream,int verbose); - -void close_file_or_dir (int fd,DIR *dirstream);int btrfs_open_fd2 (const char *path,bool verbose,bool read_write,bool dir_only);int btrfs_open_file_or_dir_fd (const char *path);
Signature · 1 line
2.43.0
Quoted 60 lines · Goffredo Baroncelli
From: Goffredo Baroncelli <[email protected]>
For historical reason, the helpers [btrfs_]open_[file_or_]dir() work with directory returning the 'fd' and a 'dirstream' variable returned by opendir(3).
If the path is a file, the 'fd' is computed from open(2) and dirstream is set to NULL. If the path is a directory, first the directory is opened by opendir(3), then the 'fd' is computed using dirfd(3).
However the 'dirstream' returned by opendir(3) is left open until 'fd' is not needed anymore.In near every case the 'dirstream' variable is not used. Only 'fd' is used.
A call to
close_file_or_dir()freed both 'fd' and 'dirstream'.Aim of this patch set is to getrid of this complexity; when the path of a directory is passed, the fd is get directly using
open(path, O_RDONLY):
so we don't need to use readdir(3) and to maintain the not used variable 'dirstream'.So each call of a legacy [btrfs_]open_[file_or_]dir() helper is replaced by a call to the new btrfs_open_[file_or_]dir_fd() functions. These functions return only the file descriptor.
Also
close_file_or_dir()is not needed anymore.The first patch, introduces the new helpers; the last patch removed the unused older helpers. The intermediate patches updated the code.
The 3rd patch updated also the
add_seen_fsid()function. Before it stored the dirstream variable. But now it is not needed anymore. So we removed a parameter of the functions and a field in the structure.In the 8th patch, the only occurrences where 'dirstream' is used was
corrected: the dirstream is computed using fdopendir(3), and the cleanup is updated according.The results is:
- removed 7 functions
- add 4 new functions
- removed ~100 lines
- removed 44 occurrences of an unused 'dirstream' variable.Changelog:
09/dec/2023: V1: first issue
08/feb/2024: V2
- rebased over 6.7
- swapped the tests order inside thebtrfs_open_fd2()function to prevent the failure of the test 003-fi-resize-args
- removed two new occurences ofopen_file_or_dir()insidecmd_scrub_limit()(introduced after V1 pateches set)BR
G.Baroncelli
Quoted 11 lines
Goffredo Baroncelli (9):
Killing dirstream: add helpers
Killing dirstream: replacebtrfs_open_dirwithbtrfs_open_dir_fdKilling dirstream: replacebtrfs_open_dirwithbtrfs_open_dir_fdKilling dirstream: replaceopen_path_or_dev_mntwithbtrfs_open_mnt_fdKilling dirstream: replaceopen_file_or_dir3withbtrfs_open_fd2Killing dirstream: replacebtrfs_open_file_or_dirwith
btrfs_open_file_or_dir_fd
Killing dirstream: replaceopen_file_or_dirwithbtrfs_open_fd2Killing dirstream: removeopen_file_or_dir3fromdu_add_fileKilling dirstream: remove unused functions
Added to devel with some updates, thanks.