From patchwork Thu Feb 8 20:19:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Goffredo Baroncelli X-Patchwork-Id: 13550448 Received: from smtp.tiscali.it (santino.mail.tiscali.it [213.205.33.245]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 488F63F9F6 for ; Thu, 8 Feb 2024 20:30:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=213.205.33.245 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707424253; cv=none; b=oBtaeVIjspE+vTP/upCfuGKvTkD195MHwOiVhuvc9nkkGUQyXaQoGrqEBa3YCOKishe3xPct5Tr5aT5i/SOTwMGaR/Ta2lvz37HAu/gqHs7FmSdTm+j6upQvNolYwRXNCV/+yvhDWM8R6WOnqQlpgWinzk9lfmzF5j3tDU+PK4w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707424253; c=relaxed/simple; bh=aZgkjjCYJB33WIL9BWo06gnUwNXqvi5yh0pljfgzXc4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=cK6oDwRCF0kfAwdsTTgNEW3FnOeX9OZWSHsNvw0gNwerzexwOF/Qg1OsvJfkEagnCioEIkbUBZL2MMovn6CFnldaPxRbtodxtHiflQajJWa5ksxkJGU/47XmVNFSAk1AqRBeWetNM89sp5mTzj50gSDYR3dNFeURBtkkpEV4Ecg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=tiscali.it; spf=pass smtp.mailfrom=tiscali.it; dkim=pass (1024-bit key) header.d=tiscali.it header.i=@tiscali.it header.b=eoh1ZJBS; arc=none smtp.client-ip=213.205.33.245 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=tiscali.it Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=tiscali.it Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=tiscali.it header.i=@tiscali.it header.b="eoh1ZJBS" Received: from venice.bhome ([84.220.171.3]) by santino.mail.tiscali.it with id kkVe2B00g04l9eU01kVeo0; Thu, 08 Feb 2024 20:29:39 +0000 X-Spam-Final-Verdict: clean X-Spam-State: 0 X-Spam-Score: 0 X-Spam-Verdict: clean x-auth-user: kreijack@tiscali.it From: Goffredo Baroncelli To: linux-btrfs@vger.kernel.org Cc: Goffredo Baroncelli Subject: [PATCH 1/9] Killing dirstream: add helpers Date: Thu, 8 Feb 2024 21:19:19 +0100 Message-ID: <44e4fdd5c74646a72794a91ba9e4078d0d7379ec.1707423567.git.kreijack@inwind.it> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Reply-To: Goffredo Baroncelli Precedence: bulk X-Mailing-List: linux-btrfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tiscali.it; s=smtp; t=1707424179; bh=DZFU/m2tMbjdj1qsBV7DLjEjcvkszvIudGvvAmra66o=; h=From:To:Cc:Subject:Date:In-Reply-To:References:Reply-To; b=eoh1ZJBS2RMeI6iqUiSlcgWZP2sQWHZHRfVZAOfLNZgaH/8g/BOuun8BxoM3hXjT4 Ic3aegyxXi80labtnWLu8nhFvg1frFdzS9oWq2Zv2xja7mrv4JRvO6Bpr0RVkuuohM cPCkEZlTmsv6jWwEnfJpc5I4tTh8I1fpbWVc4oIA= From: Goffredo Baroncelli 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. Signed-off-by: Goffredo Baroncelli --- common/open-utils.c | 81 +++++++++++++++++++++++++++++++++++++++++++++ common/open-utils.h | 5 +++ 2 files changed, 86 insertions(+) diff --git a/common/open-utils.c b/common/open-utils.c index 111a51d9..87f71196 100644 --- a/common/open-utils.c +++ b/common/open-utils.c @@ -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; +} diff --git a/common/open-utils.h b/common/open-utils.h index 3f8c004e..96d99f5d 100644 --- a/common/open-utils.h +++ b/common/open-utils.h @@ -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 From patchwork Thu Feb 8 20:19:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Goffredo Baroncelli X-Patchwork-Id: 13550457 Received: from smtp.tiscali.it (santino.mail.tiscali.it [213.205.33.245]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 488623F9F4 for ; Thu, 8 Feb 2024 20:30:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=213.205.33.245 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707424255; cv=none; b=KchqxyWeuTiaHspXoDLj5cUXrywqmOjyr8AY5pZ+ujx+cqpWkfzdv1YOZU/DtPJXApbAO5XPsrbwksVE+lu3qKq8XrN7MmexDHbz41gaWtW+/IowlUXVO6l7YX/M02BU/e4CazVygDX9KcyTUr16lW1kMDp5dmfPhqcyNX+zjOU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707424255; c=relaxed/simple; bh=2xW5ZrEII79aC2rt6k/AcY7qZURB3ZBSt7HHBJGugG4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=FfZpDNcFfJCx1f776rFbPpsZAzxvmMgNGqxnIqT/loyq8WML3TPKlDDf6ACXoQVwdnolgIk8E1t4HYoc20xQpE79jSi3w5XaqoxEzKJymcz7o3a1+23ODVIftfYLwb6fp80No7pifSmwD6OcX79JhJA2lJ91Jzd8HDIesxX1tDI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=tiscali.it; spf=pass smtp.mailfrom=tiscali.it; dkim=pass (1024-bit key) header.d=tiscali.it header.i=@tiscali.it header.b=meyf9q2A; arc=none smtp.client-ip=213.205.33.245 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=tiscali.it Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=tiscali.it Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=tiscali.it header.i=@tiscali.it header.b="meyf9q2A" Received: from venice.bhome ([84.220.171.3]) by santino.mail.tiscali.it with id kkVe2B00g04l9eU01kVfo5; Thu, 08 Feb 2024 20:29:39 +0000 X-Spam-Final-Verdict: clean X-Spam-State: 0 X-Spam-Score: 0 X-Spam-Verdict: clean x-auth-user: kreijack@tiscali.it From: Goffredo Baroncelli To: linux-btrfs@vger.kernel.org Cc: Goffredo Baroncelli Subject: [PATCH 2/9] Killing dirstream: replace btrfs_open_dir with btrfs_open_dir_fd Date: Thu, 8 Feb 2024 21:19:20 +0100 Message-ID: <304836c4f8172630ec136f97f907ca9acf8c6b59.1707423567.git.kreijack@inwind.it> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Reply-To: Goffredo Baroncelli Precedence: bulk X-Mailing-List: linux-btrfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tiscali.it; s=smtp; t=1707424179; bh=3ENkWBt2suf21JzmhueI3HycRU424+hbo2k5uAw13YY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:Reply-To; b=meyf9q2A2a4nDHmqQKr7OQHFw7u1mpT/43gA/ozOC3KF/SOi+KeAMGlaXSt5OMiYC L2vpOQ8SYJS/l2CZjbVgKnEP+7hn0zsSRUdFXuhNY+Bb2Q6pypD0tUt5i/NHWpyXnp JkYtUTH2P32w51dkrTXdkxg6Gc5drz8YsTthUrXY= From: Goffredo Baroncelli 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. Signed-off-by: Goffredo Baroncelli --- 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(-) diff --git a/cmds/balance.c b/cmds/balance.c index 65c7da0b..7c15729c 100644 --- a/cmds/balance.c +++ b/cmds/balance.c @@ -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"); diff --git a/cmds/device.c b/cmds/device.c index 4b34300b..cc201ff9 100644 --- a/cmds/device.c +++ b/cmds/device.c @@ -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; diff --git a/cmds/filesystem-usage.c b/cmds/filesystem-usage.c index 52e552dc..eaa732c1 100644 --- a/cmds/filesystem-usage.c +++ b/cmds/filesystem-usage.c @@ -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); diff --git a/cmds/filesystem.c b/cmds/filesystem.c index e0e74fbe..9867403f 100644 --- a/cmds/filesystem.c +++ b/cmds/filesystem.c @@ -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: diff --git a/cmds/inspect.c b/cmds/inspect.c index a90c373a..86023270 100644 --- a/cmds/inspect.c +++ b/cmds/inspect.c @@ -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; } diff --git a/cmds/qgroup.c b/cmds/qgroup.c index 68791428..61c6505a 100644 --- a/cmds/qgroup.c +++ b/cmds/qgroup.c @@ -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"); diff --git a/cmds/quota.c b/cmds/quota.c index fa069d79..adf7bf1a 100644 --- a/cmds/quota.c +++ b/cmds/quota.c @@ -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"); diff --git a/cmds/replace.c b/cmds/replace.c index 138a22e4..171a72b4 100644 --- a/cmds/replace.c +++ b/cmds/replace.c @@ -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) diff --git a/cmds/subvolume-list.c b/cmds/subvolume-list.c index 5a91f41d..a26e8b02 100644 --- a/cmds/subvolume-list.c +++ b/cmds/subvolume-list.c @@ -1587,7 +1587,6 @@ static int cmd_subvolume_list(const struct cmd_struct *cmd, int argc, char **arg char *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) diff --git a/cmds/subvolume.c b/cmds/subvolume.c index b01d5c80..cca73379 100644 --- a/cmds/subvolume.c +++ b/cmds/subvolume.c @@ -150,7 +150,6 @@ static int create_one_subvolume(const char *dst, struct btrfs_qgroup_inherit *in char *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, cha int 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, cha if (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 **arg if (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; } From patchwork Thu Feb 8 20:19:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Goffredo Baroncelli X-Patchwork-Id: 13550451 Received: from smtp.tiscali.it (santino.mail.tiscali.it [213.205.33.245]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 489723F9F8 for ; Thu, 8 Feb 2024 20:30:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=213.205.33.245 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707424253; cv=none; b=jfsTTeV9SKkAqrGidWNPwRLwo4vpoX3lFVPgY1IzNreBNv0luU3Gy2HlbgqPyxAfCaYMBxojvUf/zHowTlWqbDd8hPiHnDfJV4Tnxmi9rDPUyYYvB0a9ok0yHs3Qle/1VUvVzlzpT7447+lD5Lo6nfxZDJKOVXh6GMHmAZqX8HM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707424253; c=relaxed/simple; bh=WkFAp1r3lf4xQYVqTfXRD7KJ1BNDbZWPodt4pZKcXWY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=qtFT5sddhdoC/st1EdxFtcg0WUZkRbn5p94mJpg8Gv05ZfcHOgUIaXfneUmpsIqB7VaEgsconh1jGoRrejPEMfQC/AJLI0PdWNJp+c15a2s+zr/zWUMSfJQQBYq0uNG3SpoJDPl4lcAROsUSY5tGX3CaojSXap3PbUXYgLPWay8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=tiscali.it; spf=pass smtp.mailfrom=tiscali.it; dkim=pass (1024-bit key) header.d=tiscali.it header.i=@tiscali.it header.b=30v20QSF; arc=none smtp.client-ip=213.205.33.245 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=tiscali.it Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=tiscali.it Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=tiscali.it header.i=@tiscali.it header.b="30v20QSF" Received: from venice.bhome ([84.220.171.3]) by santino.mail.tiscali.it with id kkVe2B00g04l9eU01kVfoG; Thu, 08 Feb 2024 20:29:39 +0000 X-Spam-Final-Verdict: clean X-Spam-State: 0 X-Spam-Score: 0 X-Spam-Verdict: clean x-auth-user: kreijack@tiscali.it From: Goffredo Baroncelli To: linux-btrfs@vger.kernel.org Cc: Goffredo Baroncelli Subject: [PATCH 3/9] Killing dirstream: replace btrfs_open_dir with btrfs_open_dir_fd Date: Thu, 8 Feb 2024 21:19:21 +0100 Message-ID: <9d7800b5c4b57f30333c0a93ff9d28dc8b387831.1707423567.git.kreijack@inwind.it> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Reply-To: Goffredo Baroncelli Precedence: bulk X-Mailing-List: linux-btrfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tiscali.it; s=smtp; t=1707424179; bh=9Dcrhgyjxj5tFURKk2brlZMKyCkFPhdrsrIBqgE4aY0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:Reply-To; b=30v20QSFQB74u7l0wgXoa9ny/+9rpMOo0Tj9MnF+Ft1Je2UrJBlZaXPjkrIy+Gr+G 3RaAO5QDSDz9MAA1+WO/i9NLqOTAk5PyHBAsl1noj661vRUIrjUwJ0KneR3YEMnSvm AmRcU3ja87YrnCO0yd9QP/AcmYyTLa44VjfUKqLs= From: Goffredo Baroncelli 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). Signed-off-by: Goffredo Baroncelli --- 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(-) diff --git a/cmds/filesystem.c b/cmds/filesystem.c index 9867403f..d37b52a3 100644 --- a/cmds/filesystem.c +++ b/cmds/filesystem.c @@ -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) diff --git a/cmds/subvolume.c b/cmds/subvolume.c index cca73379..65582f67 100644 --- a/cmds/subvolume.c +++ b/cmds/subvolume.c @@ -374,7 +374,6 @@ static int cmd_subvolume_delete(const struct cmd_struct *cmd, int argc, char **a char *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; diff --git a/common/device-scan.c b/common/device-scan.c index c1cd7266..c04c2388 100644 --- a/common/device-scan.c +++ b/common/device-scan.c @@ -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; } diff --git a/common/device-scan.h b/common/device-scan.h index 8a875832..90ec766d 100644 --- a/common/device-scan.h +++ b/common/device-scan.h @@ -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); From patchwork Thu Feb 8 20:19:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Goffredo Baroncelli X-Patchwork-Id: 13550456 Received: from smtp.tiscali.it (santino.mail.tiscali.it [213.205.33.245]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 488A73F9F5 for ; Thu, 8 Feb 2024 20:30:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=213.205.33.245 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707424254; cv=none; b=UhQCm+d+v7dpb//D242PKm1AFORL9FBD7ZPLZqghN4wzG7xDbvCV9mp/l178yh6/o5vjjEDcSATPVjKJa70VtZr8tRNVd0AKqfdXnenPlJPfPsacmNynYcNWmoEMw237ZJP5qrSKrUJpM1Z538WpeErZeGtijVsoAjpbZWIHxA4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707424254; c=relaxed/simple; bh=WgtJuWzhi4zxVkayVTRKYLEv3rCbLQWRpVK6lkZ8eGE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=nYuPNAtrmUpVh2S+rRpDuOFiY2VWJusykdx7zPgy7hOX/Tve3Sbm2oBM+2musWvzOIwkeFR08bo/vazgsQlMzwC1LTJuyuG5haGWNiGpHEY/8GZwmb8TF7buU/BgiA6CDHOQfu92OrKgTB45Pfi/35IwV19diHe+rPeryGJpnZI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=tiscali.it; spf=pass smtp.mailfrom=tiscali.it; dkim=pass (1024-bit key) header.d=tiscali.it header.i=@tiscali.it header.b=lYBdQlm5; arc=none smtp.client-ip=213.205.33.245 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=tiscali.it Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=tiscali.it Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=tiscali.it header.i=@tiscali.it header.b="lYBdQlm5" Received: from venice.bhome ([84.220.171.3]) by santino.mail.tiscali.it with id kkVe2B00g04l9eU01kVfoM; Thu, 08 Feb 2024 20:29:39 +0000 X-Spam-Final-Verdict: clean X-Spam-State: 0 X-Spam-Score: 0 X-Spam-Verdict: clean x-auth-user: kreijack@tiscali.it From: Goffredo Baroncelli To: linux-btrfs@vger.kernel.org Cc: Goffredo Baroncelli Subject: [PATCH 4/9] Killing dirstream: replace open_path_or_dev_mnt with btrfs_open_mnt_fd Date: Thu, 8 Feb 2024 21:19:22 +0100 Message-ID: X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Reply-To: Goffredo Baroncelli Precedence: bulk X-Mailing-List: linux-btrfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tiscali.it; s=smtp; t=1707424179; bh=AYJQe+1TJUuxiFNhf39k7fVVrH++mHgY9oIWRvfe/e4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:Reply-To; b=lYBdQlm5JQH7QxyoRwAuRSk5sljZjZS/eMNI4StXvN8uHzgJ0RbM6RYSLsUqus5Xw Fxg+vnlIYjXz64Ms+Vj7+21fV8VJro/Zv6CxUzaq9j4CCiVyXXAxYzemMTNqjBksxE LcJ+d9UlS/gCBmQp7GQI9F5YvjntuOze6jM1MMkQ= From: Goffredo Baroncelli 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. Signed-off-by: Goffredo Baroncelli --- cmds/device.c | 5 ++--- cmds/replace.c | 7 +++---- cmds/scrub.c | 15 ++++++--------- 3 files changed, 11 insertions(+), 16 deletions(-) diff --git a/cmds/device.c b/cmds/device.c index cc201ff9..5be76595 100644 --- a/cmds/device.c +++ b/cmds/device.c @@ -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); diff --git a/cmds/replace.c b/cmds/replace.c index 171a72b4..f1db9477 100644 --- a/cmds/replace.c +++ b/cmds/replace.c @@ -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: diff --git a/cmds/scrub.c b/cmds/scrub.c index e105ecb2..039a67d1 100644 --- a/cmds/scrub.c +++ b/cmds/scrub.c @@ -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; } From patchwork Thu Feb 8 20:19:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Goffredo Baroncelli X-Patchwork-Id: 13550449 Received: from smtp.tiscali.it (santino.mail.tiscali.it [213.205.33.245]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 486FA3F9E3 for ; Thu, 8 Feb 2024 20:30:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=213.205.33.245 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707424253; cv=none; b=OLvxISdP0LMvnVU/zN5CJT4vbt3F84tAt5OjC+Ivark192k/Gzp6PGjWlUf1A/CAWaYN5aGoAmtWVJrgCCMOk6fNdbLzxbcT9XUwpSzvlxv19LN0uCaD15Wl+d55YYG98tFlDGxMRxmhjrQALvljhmy8FH/MXUIEjfcBS36EkD0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707424253; c=relaxed/simple; bh=iolS9GcuxxCYyqsFPJDGKpGzY/YKpFbE+vgw0p7hjkg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=YWZwpSRj4oV46Z7Euh/NCyc70/jqViepgyGYg/gRTcGL9XJ9Q4ohNppPXHfHp0hiHAZXmZ0OGKQgCKCu5TrwIyPqph4QZswvEVAiI+qPrLlyvQIZxURCEkSKtFuM3K4/p5sZ9lmya3cBrLUm6zZGJQBGjhoylcQ6ahELUeAuBS8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=tiscali.it; spf=pass smtp.mailfrom=tiscali.it; dkim=pass (1024-bit key) header.d=tiscali.it header.i=@tiscali.it header.b=glDMhIri; arc=none smtp.client-ip=213.205.33.245 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=tiscali.it Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=tiscali.it Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=tiscali.it header.i=@tiscali.it header.b="glDMhIri" Received: from venice.bhome ([84.220.171.3]) by santino.mail.tiscali.it with id kkVe2B00g04l9eU01kVfoV; Thu, 08 Feb 2024 20:29:39 +0000 X-Spam-Final-Verdict: clean X-Spam-State: 0 X-Spam-Score: 0 X-Spam-Verdict: clean x-auth-user: kreijack@tiscali.it From: Goffredo Baroncelli To: linux-btrfs@vger.kernel.org Cc: Goffredo Baroncelli Subject: [PATCH 5/9] Killing dirstream: replace open_file_or_dir3 with btrfs_open_fd2 Date: Thu, 8 Feb 2024 21:19:23 +0100 Message-ID: <28a66d261b14f17625bd355f837beb18c7546014.1707423567.git.kreijack@inwind.it> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Reply-To: Goffredo Baroncelli Precedence: bulk X-Mailing-List: linux-btrfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tiscali.it; s=smtp; t=1707424179; bh=CaRC4LGdNXxKgL18ZnqV/Sz8/ySRzSMr1RihS6k6psA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:Reply-To; b=glDMhIrip0Z+0Uft/1Zk1RDq1bhuLE5m47+RGaZDh84/j1vI+Qm0ZY4WDKhzNhU6u kr13qU9EesoIbG9m881uXa0/pAXd4VtRLi2uSPY/vaB/29Owpn5+BW3iUKBcDPH8Cp iAXQBdAvXQvZSiBC2ABWPzPNKGO1Ybk9QENzDk68= From: Goffredo Baroncelli 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. Signed-off-by: Goffredo Baroncelli --- cmds/filesystem.c | 8 +++----- cmds/property.c | 5 ++--- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/cmds/filesystem.c b/cmds/filesystem.c index d37b52a3..408ebe82 100644 --- a/cmds/filesystem.c +++ b/cmds/filesystem.c @@ -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) diff --git a/cmds/property.c b/cmds/property.c index be9bdf63..e189e505 100644 --- a/cmds/property.c +++ b/cmds/property.c @@ -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; } From patchwork Thu Feb 8 20:19:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Goffredo Baroncelli X-Patchwork-Id: 13550454 Received: from smtp.tiscali.it (santino.mail.tiscali.it [213.205.33.245]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 4892E3F9F7 for ; Thu, 8 Feb 2024 20:30:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=213.205.33.245 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707424254; cv=none; b=U0tEyYz3cyo9XecWZNDvfSlT/+zIu8VW7SQEHpsjfGZ+ceAI9CNRpcfcfKUoKIlgDDBmzlBOYFcZVukTiUzjxpj/GzvBDUm4/Ay4EmtloJ3g/aRzCuW+ZZwVFLccO823Oo7ITF9AQnoIB00A4lw5Es2WGijD4eK7K+bl1xeEDNI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707424254; c=relaxed/simple; bh=WyDiTA+5fYt32h2UPz33Jpoaux0oLv32u9Ebd/SJ/Uw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=p38RF2zyMuOGbvVm5Pqm4ikU/7l4Y9PYvSxVswzSi72z5HvkOdwoInkdC7AIE3dJXWGbM3jplLwM1QTU+wZfNW0vGzZ+ZShmTJrXpip/iZSk9SQKR5AtDlKcOseUuO/C6toRTWiXwwGUUFZrU7c8uULl/sEKS0kQ2agFwpNdx+0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=tiscali.it; spf=pass smtp.mailfrom=tiscali.it; dkim=pass (1024-bit key) header.d=tiscali.it header.i=@tiscali.it header.b=ixDL7vpv; arc=none smtp.client-ip=213.205.33.245 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=tiscali.it Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=tiscali.it Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=tiscali.it header.i=@tiscali.it header.b="ixDL7vpv" Received: from venice.bhome ([84.220.171.3]) by santino.mail.tiscali.it with id kkVe2B00g04l9eU01kVfoa; Thu, 08 Feb 2024 20:29:39 +0000 X-Spam-Final-Verdict: clean X-Spam-State: 0 X-Spam-Score: 0 X-Spam-Verdict: clean x-auth-user: kreijack@tiscali.it From: Goffredo Baroncelli To: linux-btrfs@vger.kernel.org Cc: Goffredo Baroncelli Subject: [PATCH 6/9] Killing dirstream: replace btrfs_open_file_or_dir with btrfs_open_file_or_dir_fd Date: Thu, 8 Feb 2024 21:19:24 +0100 Message-ID: <7156cf1438f8f2c6442e6f6ba1fe4158080667f6.1707423567.git.kreijack@inwind.it> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Reply-To: Goffredo Baroncelli Precedence: bulk X-Mailing-List: linux-btrfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tiscali.it; s=smtp; t=1707424179; bh=RJVpd5vU1oCn1gxA81O3c09OEpM7ZuwMgY21vU65iYY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:Reply-To; b=ixDL7vpv5rV6xZVzpPggeQj7/xp0jWORzdGYEdZ2ldnKsFvvgqBqWLg3QGjQhxXWj M2kZ8XyiZvCnr6E+hhDZ0i1Tp2a1A3tVBZR8UWM7Fff4b9QRK+RdYoy/cOi47IJoKF nfJWJ6/u2jlyxEQ5m/sEcHg+j3lU1fOcFdGWmD/o= From: Goffredo Baroncelli 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. Signed-off-by: Goffredo Baroncelli --- cmds/inspect.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/cmds/inspect.c b/cmds/inspect.c index 86023270..4d4e24d2 100644 --- a/cmds/inspect.c +++ b/cmds/inspect.c @@ -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; } From patchwork Thu Feb 8 20:19:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Goffredo Baroncelli X-Patchwork-Id: 13550453 Received: from smtp.tiscali.it (santino.mail.tiscali.it [213.205.33.245]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 487333F9EB for ; Thu, 8 Feb 2024 20:30:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=213.205.33.245 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707424254; cv=none; b=U+VX+65h30BF8U7bKEJI+lHuMlPk0EAS12iohICurzhDhVL/ocFlooUMs36I1Pr3iKevf9Zez0OguRQCB+Q1hCsz0W17VoBUcs3+1NWPjK4dr7QXcTUmIdQl/3zL3ncNzpFAMYmzSgHffBCi+sUXKTc6Ktn9GvBkp1NLONjcgTw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707424254; c=relaxed/simple; bh=iOuGM5eP03WUspaTbuvlLp2oWEjYHeoEOXRTO5n5bF4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=WjB5jrSgXn3Q53zmLfcVGpX74cur8xjAEUPYsIzGjEG7PeQYg5zzHaVTTlHQgy+DaT/uVfRBtfsL2FtaeqIXiYwq69L1Mak3A8Y9ZjTK2wa+B6pvot0iKG4WAgcsCm3NxsqijRBJ464dIhKm0aDoHUabi1Gcv94UdN4r3aY7Gzg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=tiscali.it; spf=pass smtp.mailfrom=tiscali.it; dkim=pass (1024-bit key) header.d=tiscali.it header.i=@tiscali.it header.b=yLRqFdoa; arc=none smtp.client-ip=213.205.33.245 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=tiscali.it Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=tiscali.it Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=tiscali.it header.i=@tiscali.it header.b="yLRqFdoa" Received: from venice.bhome ([84.220.171.3]) by santino.mail.tiscali.it with id kkVe2B00g04l9eU01kVfog; Thu, 08 Feb 2024 20:29:40 +0000 X-Spam-Final-Verdict: clean X-Spam-State: 0 X-Spam-Score: 0 X-Spam-Verdict: clean x-auth-user: kreijack@tiscali.it From: Goffredo Baroncelli To: linux-btrfs@vger.kernel.org Cc: Goffredo Baroncelli Subject: [PATCH 7/9] Killing dirstream: replace open_file_or_dir with btrfs_open_fd2 Date: Thu, 8 Feb 2024 21:19:25 +0100 Message-ID: <6e9ed797da5f24ac25356aedf16c9dbc3af190c6.1707423567.git.kreijack@inwind.it> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Reply-To: Goffredo Baroncelli Precedence: bulk X-Mailing-List: linux-btrfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tiscali.it; s=smtp; t=1707424180; bh=AS24lxN/1pW38pls7Jz4PiKtKYBdmEAQzdJOVM/ZySU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:Reply-To; b=yLRqFdoacFd0o4g/SwfqVGdogErILkxsI2xry0p0ygUw0vfdGZwqF8LQmKdpuUd5z MGNODfaWrVbXC0AjVa9X1Ili0UPWS0+LW40PqvZrh4vom3WAHqPIijBDwrtwsB5zKl TyvsDbeNE2sHPhciuDZi0D5KdrcRcDcrQ0sb5q5g= From: Goffredo Baroncelli 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. Signed-off-by: Goffredo Baroncelli --- cmds/inspect.c | 5 ++--- cmds/scrub.c | 5 ++--- cmds/subvolume.c | 5 ++--- common/utils.c | 5 ++--- 4 files changed, 8 insertions(+), 12 deletions(-) diff --git a/cmds/inspect.c b/cmds/inspect.c index 4d4e24d2..268a902a 100644 --- a/cmds/inspect.c +++ b/cmds/inspect.c @@ -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); diff --git a/cmds/scrub.c b/cmds/scrub.c index 039a67d1..55dacd57 100644 --- a/cmds/scrub.c +++ b/cmds/scrub.c @@ -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; } diff --git a/cmds/subvolume.c b/cmds/subvolume.c index 65582f67..a93dd408 100644 --- a/cmds/subvolume.c +++ b/cmds/subvolume.c @@ -1566,7 +1566,6 @@ static int cmd_subvolume_show(const struct cmd_struct *cmd, int argc, char **arg char *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; } diff --git a/common/utils.c b/common/utils.c index 8d860726..72520641 100644 --- a/common/utils.c +++ b/common/utils.c @@ -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; } From patchwork Thu Feb 8 20:19:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Goffredo Baroncelli X-Patchwork-Id: 13550450 Received: from smtp.tiscali.it (santino.mail.tiscali.it [213.205.33.245]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 487C03F9F2 for ; Thu, 8 Feb 2024 20:30:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=213.205.33.245 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707424253; cv=none; b=lNJ366jMMuApyRpHwYTyGZQGwrHq3xhrbzwgJQE9+Lg+J4iOqyrYuWLYbCo4Pt1rc2m1VBQBLanJB3m606kPUvTeyJ3gsy4KZHQ4yYxRnTKkjoGhVA/x6rMBzhQvPzz40Uro6tj04FsccYAwucXyuoDxUF09VXQhgChBQ5iLZP8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707424253; c=relaxed/simple; bh=4u1qz0HRvOEw2Vbu1xcwtwUaVXvzoaqSlfcd4+ALgDU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=LHtmyn6cvsVNCVoWk4D3wOWIaWOBuxax4eW2Eex+ki3aLjQe6OjXS60Y2PEpe3+7GjSco33Hi432eKJAG64+Aa3fNrMyDx4TO6kx8dmifK1B7N9euLLL3UaBknDtklJ0eO9lpm+sOT6RijNnCHpuoAmVQIqQCZkGxdRgn7riz9o= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=tiscali.it; spf=pass smtp.mailfrom=tiscali.it; dkim=pass (1024-bit key) header.d=tiscali.it header.i=@tiscali.it header.b=rAcSVtjQ; arc=none smtp.client-ip=213.205.33.245 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=tiscali.it Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=tiscali.it Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=tiscali.it header.i=@tiscali.it header.b="rAcSVtjQ" Received: from venice.bhome ([84.220.171.3]) by santino.mail.tiscali.it with id kkVe2B00g04l9eU01kVgoq; Thu, 08 Feb 2024 20:29:40 +0000 X-Spam-Final-Verdict: clean X-Spam-State: 0 X-Spam-Score: 0 X-Spam-Verdict: clean x-auth-user: kreijack@tiscali.it From: Goffredo Baroncelli To: linux-btrfs@vger.kernel.org Cc: Goffredo Baroncelli Subject: [PATCH 8/9] Killing dirstream: remove open_file_or_dir3 from du_add_file Date: Thu, 8 Feb 2024 21:19:26 +0100 Message-ID: <2eec4ed3dd1574cbf17f2ecb1f91b159f8a90a34.1707423567.git.kreijack@inwind.it> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Reply-To: Goffredo Baroncelli Precedence: bulk X-Mailing-List: linux-btrfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tiscali.it; s=smtp; t=1707424180; bh=HMX5Q+prtFhmDqdFdqsZLTZh9iXrSPToocenCVlATt8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:Reply-To; b=rAcSVtjQEaT0n/2S2jrCa0rvXbxnyRysuDwcmxhqPwvKtV3+JzX2nvsBS7Ns3r9QQ i7iHYEtd+6zZ98l3/tS8R0NzjtQYySX2n+LUQSHNJlAWw76n04JQBrmcW2+Bh2SZaw EUaMXie45M5YLZlyioNLZo4ki/ulRTCht+e77ny0= From: Goffredo Baroncelli 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(). Signed-off-by: Goffredo Baroncelli --- cmds/filesystem-du.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/cmds/filesystem-du.c b/cmds/filesystem-du.c index 4982123d..cffeafd5 100644 --- a/cmds/filesystem-du.c +++ b/cmds/filesystem-du.c @@ -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; From patchwork Thu Feb 8 20:19:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Goffredo Baroncelli X-Patchwork-Id: 13550455 Received: from smtp.tiscali.it (santino.mail.tiscali.it [213.205.33.245]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 4881D3F9F3 for ; Thu, 8 Feb 2024 20:30:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=213.205.33.245 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707424254; cv=none; b=pkDpO8tID+SE98kR2V+Ah1osxZRjU8ItwjC//YE8TIR+tSXrNBb4LGPhusksJ9fBZ6GggXHFDwIVFiL8AdnUJ69iKr/A4+neiC7u4dge5cOeehsPR6fjEETfehMxRrCrNYlB5qgNA87TYsir3Msm8IH82a5b2mBKYghtJQ2aJMw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707424254; c=relaxed/simple; bh=TQfY7qt1Vl+M966WJtPE5UwPkaC4To6rSeDlNXn6xWI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=pP2FFzHOslGH4RQldUlfQaugtb25aBERJtnjouyTjr/MTceEopVgkNbe/P31Bt7ZRZq7iMJDoVtuHGkHSlpEiZ8bDHp/dtsdwFWi74fZycdUHkKr09qd6QuJz3mzRKeymdE6zE/0yQEtr9RSp4h/1RbrFJ4N9cA869RD/9udokM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=tiscali.it; spf=pass smtp.mailfrom=tiscali.it; dkim=pass (1024-bit key) header.d=tiscali.it header.i=@tiscali.it header.b=uXbnq3kx; arc=none smtp.client-ip=213.205.33.245 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=tiscali.it Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=tiscali.it Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=tiscali.it header.i=@tiscali.it header.b="uXbnq3kx" Received: from venice.bhome ([84.220.171.3]) by santino.mail.tiscali.it with id kkVe2B00g04l9eU01kVgoz; Thu, 08 Feb 2024 20:29:40 +0000 X-Spam-Final-Verdict: clean X-Spam-State: 0 X-Spam-Score: 0 X-Spam-Verdict: clean x-auth-user: kreijack@tiscali.it From: Goffredo Baroncelli To: linux-btrfs@vger.kernel.org Cc: Goffredo Baroncelli Subject: [PATCH 9/9] Killing dirstream: remove unused functions Date: Thu, 8 Feb 2024 21:19:27 +0100 Message-ID: X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Reply-To: Goffredo Baroncelli Precedence: bulk X-Mailing-List: linux-btrfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tiscali.it; s=smtp; t=1707424180; bh=IXLQqVApg0RwqEpNSgABEr0PZm1Uir4mp3VRUP47pa4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:Reply-To; b=uXbnq3kxExiAwlwR7TJZD/h/0cj9Tafy8uGJHibM4NPubLoHFqk4O9fA6L8NRQpu/ MYWFagJjx/xsBBFykXGd7UCrfp+O2se6rrSp58xWQBUe3jsNNUknSHlOfmmIXhXShr 3waMa6DH550oZ3jWsajBSplg7Azxl/JG9cuYzn1o= From: Goffredo Baroncelli 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() Signed-off-by: Goffredo Baroncelli --- common/open-utils.c | 135 -------------------------------------------- common/open-utils.h | 10 ---- 2 files changed, 145 deletions(-) diff --git a/common/open-utils.c b/common/open-utils.c index 87f71196..cac89bc3 100644 --- a/common/open-utils.c +++ b/common/open-utils.c @@ -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 diff --git a/common/open-utils.h b/common/open-utils.h index 96d99f5d..5642b951 100644 --- a/common/open-utils.h +++ b/common/open-utils.h @@ -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);