Stefan Behrens
2012-May-25 14:07 UTC
[PATCH v5 0/3] Btrfs-progs: support get/reset device stats via ioctl
"btrfs device stats" is used to retrieve and print the device stats. "btrfs device stats -z" is used to atomically retrieve, reset and print the stats. In order to share two utility functions between scrub and the dev stats code, these two functions are moved to utils.c and renamed. Since these functions are using open_file_or_dir(), and since the linking against utils.o and common.o was different, open_file_or_dir() was moved from common.c to utils.c. And since that function makes use of the function dirfd(3), the required XOPEN version was raised from 6 to 7. Changes v1->v2: - Remove a verbose printf() - Cast u64 to unsigned long long for printf() - Update the man page Changes v2->v3: - Rebase on Chris'' current master branch - Split the patch into three seperate patches because after rebasing, open_file_or_dir() was moved and additional changes had been necessary Changes v3->v4: - Add padding at end of ioctl structure Changes v4->v5: - The statistic members in the ioctl are now organized as an array of 64 bit values. Symbolic names for the array indexes are defined in an enum, which also defines the max value Stefan Behrens (3): Btrfs-progs: move open_file_or_dir() to utils.c Btrfs-progs: make two utility functions globally available Btrfs-progs: add command to get/reset device stats via ioctl Makefile | 4 +- btrfsctl.c | 28 ------------- cmds-balance.c | 1 + cmds-device.c | 118 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ cmds-inspect.c | 1 + cmds-scrub.c | 72 +-------------------------------- cmds-subvolume.c | 1 + commands.h | 3 -- common.c | 46 --------------------- ctree.h | 6 +++ ioctl.h | 33 +++++++++++++++ man/btrfs.8.in | 14 +++++++ print-tree.c | 6 +++ utils.c | 97 +++++++++++++++++++++++++++++++++++++++++++- utils.h | 7 ++++ 15 files changed, 286 insertions(+), 151 deletions(-) delete mode 100644 common.c -- 1.7.10.2 -- To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
Stefan Behrens
2012-May-25 14:07 UTC
[PATCH v5 1/3] Btrfs-progs: move open_file_or_dir() to utils.c
This is a preparation step to add support for device stats. The definition of the function open_file_or_dir() is moved from common.c to utils.c in order to be able to share some common code between scrub and the device stats in the following step. That common code uses open_file_or_dir(). Since open_file_or_dir() makes use of the function dirfd(3), the required XOPEN version was raised from 6 to 7. Signed-off-by: Stefan Behrens <sbehrens@giantdisaster.de> --- Makefile | 4 ++-- btrfsctl.c | 28 ---------------------------- cmds-balance.c | 1 + cmds-inspect.c | 1 + cmds-subvolume.c | 1 + commands.h | 3 --- common.c | 46 ---------------------------------------------- utils.c | 31 +++++++++++++++++++++++++++++-- utils.h | 3 +++ 9 files changed, 37 insertions(+), 81 deletions(-) diff --git a/Makefile b/Makefile index 79818e6..fe2b432 100644 --- a/Makefile +++ b/Makefile @@ -39,8 +39,8 @@ all: version $(progs) manpages version: bash version.sh -btrfs: $(objects) btrfs.o help.o common.o $(cmds_objects) - $(CC) $(CFLAGS) -o btrfs btrfs.o help.o common.o $(cmds_objects) \ +btrfs: $(objects) btrfs.o help.o $(cmds_objects) + $(CC) $(CFLAGS) -o btrfs btrfs.o help.o $(cmds_objects) \ $(objects) $(LDFLAGS) $(LIBS) -lpthread calc-size: $(objects) calc-size.o diff --git a/btrfsctl.c b/btrfsctl.c index d45e2a7..f0584f3 100644 --- a/btrfsctl.c +++ b/btrfsctl.c @@ -63,34 +63,6 @@ static void print_usage(void) exit(1); } -static int open_file_or_dir(const char *fname) -{ - int ret; - struct stat st; - DIR *dirstream; - int fd; - - ret = stat(fname, &st); - if (ret < 0) { - perror("stat:"); - exit(1); - } - if (S_ISDIR(st.st_mode)) { - dirstream = opendir(fname); - if (!dirstream) { - perror("opendir"); - exit(1); - } - fd = dirfd(dirstream); - } else { - fd = open(fname, O_RDWR); - } - if (fd < 0) { - perror("open"); - exit(1); - } - return fd; -} int main(int ac, char **av) { char *fname = NULL; diff --git a/cmds-balance.c b/cmds-balance.c index 38a7426..5793b5c 100644 --- a/cmds-balance.c +++ b/cmds-balance.c @@ -26,6 +26,7 @@ #include "ctree.h" #include "ioctl.h" #include "volumes.h" +#include "utils.h" #include "commands.h" diff --git a/cmds-inspect.c b/cmds-inspect.c index 2f0228f..7a8785b 100644 --- a/cmds-inspect.c +++ b/cmds-inspect.c @@ -22,6 +22,7 @@ #include "kerncompat.h" #include "ioctl.h" +#include "utils.h" #include "commands.h" diff --git a/cmds-subvolume.c b/cmds-subvolume.c index 950fa8f..8ecd3f4 100644 --- a/cmds-subvolume.c +++ b/cmds-subvolume.c @@ -26,6 +26,7 @@ #include "kerncompat.h" #include "ioctl.h" +#include "utils.h" #include "commands.h" diff --git a/commands.h b/commands.h index a303a50..aea4cb1 100644 --- a/commands.h +++ b/commands.h @@ -79,9 +79,6 @@ void help_ambiguous_token(const char *arg, const struct cmd_group *grp); void help_command_group(const struct cmd_group *grp, int argc, char **argv); -/* common.c */ -int open_file_or_dir(const char *fname); - extern const struct cmd_group subvolume_cmd_group; extern const struct cmd_group filesystem_cmd_group; extern const struct cmd_group balance_cmd_group; diff --git a/common.c b/common.c deleted file mode 100644 index 03f6570..0000000 --- a/common.c +++ /dev/null @@ -1,46 +0,0 @@ -/* - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public - * License v2 as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public - * License along with this program; if not, write to the - * Free Software Foundation, Inc., 59 Temple Place - Suite 330, - * Boston, MA 021110-1307, USA. - */ - -#include <sys/types.h> -#include <sys/stat.h> -#include <dirent.h> -#include <fcntl.h> - -int open_file_or_dir(const char *fname) -{ - int ret; - struct stat st; - DIR *dirstream; - int fd; - - ret = stat(fname, &st); - if (ret < 0) { - return -1; - } - if (S_ISDIR(st.st_mode)) { - dirstream = opendir(fname); - if (!dirstream) { - return -2; - } - fd = dirfd(dirstream); - } else { - fd = open(fname, O_RDWR); - } - if (fd < 0) { - return -3; - } - return fd; -} diff --git a/utils.c b/utils.c index ee7fa1b..6157115 100644 --- a/utils.c +++ b/utils.c @@ -16,8 +16,9 @@ * Boston, MA 021110-1307, USA. */ -#define _XOPEN_SOURCE 600 -#define __USE_XOPEN2K +#define _XOPEN_SOURCE 700 +#define __USE_XOPEN2K8 +#define __XOPEN2K8 /* due to an error in dirent.h, to get dirfd() */ #include <stdio.h> #include <stdlib.h> #ifndef __CHECKER__ @@ -1206,3 +1207,29 @@ scan_again: return 0; } +int open_file_or_dir(const char *fname) +{ + int ret; + struct stat st; + DIR *dirstream; + int fd; + + ret = stat(fname, &st); + if (ret < 0) { + return -1; + } + if (S_ISDIR(st.st_mode)) { + dirstream = opendir(fname); + if (!dirstream) { + return -2; + } + fd = dirfd(dirstream); + } else { + fd = open(fname, O_RDWR); + } + if (fd < 0) { + return -3; + } + return fd; +} + diff --git a/utils.h b/utils.h index c5f55e1..e281002 100644 --- a/utils.h +++ b/utils.h @@ -19,6 +19,8 @@ #ifndef __UTILS__ #define __UTILS__ +#include "ctree.h" + #define BTRFS_MKFS_SYSTEM_GROUP_SIZE (4 * 1024 * 1024) int make_btrfs(int fd, const char *device, const char *label, @@ -46,4 +48,5 @@ int check_label(char *input); int get_mountpt(char *dev, char *mntpt, size_t size); int btrfs_scan_block_devices(int run_ioctl); +int open_file_or_dir(const char *fname); #endif -- 1.7.10.2 -- To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
Stefan Behrens
2012-May-25 14:07 UTC
[PATCH v5 2/3] Btrfs-progs: make two utility functions globally available
Two convenient utility functions that have so far been local to scrub are moved to utils.c. They will be used in the device stats code in a following commit. Signed-off-by: Stefan Behrens <sbehrens@giantdisaster.de> --- cmds-scrub.c | 72 ++-------------------------------------------------------- utils.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++ utils.h | 4 ++++ 3 files changed, 72 insertions(+), 70 deletions(-) diff --git a/cmds-scrub.c b/cmds-scrub.c index c4503f4..37a9890 100644 --- a/cmds-scrub.c +++ b/cmds-scrub.c @@ -967,74 +967,6 @@ static struct scrub_file_record *last_dev_scrub( return NULL; } -static int scrub_device_info(int fd, u64 devid, - struct btrfs_ioctl_dev_info_args *di_args) -{ - int ret; - - di_args->devid = devid; - memset(&di_args->uuid, ''\0'', sizeof(di_args->uuid)); - - ret = ioctl(fd, BTRFS_IOC_DEV_INFO, di_args); - return ret ? -errno : 0; -} - -static int scrub_fs_info(int fd, char *path, - struct btrfs_ioctl_fs_info_args *fi_args, - struct btrfs_ioctl_dev_info_args **di_ret) -{ - int ret = 0; - int ndevs = 0; - int i = 1; - struct btrfs_fs_devices *fs_devices_mnt = NULL; - struct btrfs_ioctl_dev_info_args *di_args; - char mp[BTRFS_PATH_NAME_MAX + 1]; - - memset(fi_args, 0, sizeof(*fi_args)); - - ret = ioctl(fd, BTRFS_IOC_FS_INFO, fi_args); - if (ret && errno == EINVAL) { - /* path is no mounted btrfs. try if it''s a device */ - ret = check_mounted_where(fd, path, mp, sizeof(mp), - &fs_devices_mnt); - if (!ret) - return -EINVAL; - if (ret < 0) - return ret; - fi_args->num_devices = 1; - fi_args->max_id = fs_devices_mnt->latest_devid; - i = fs_devices_mnt->latest_devid; - memcpy(fi_args->fsid, fs_devices_mnt->fsid, BTRFS_FSID_SIZE); - close(fd); - fd = open_file_or_dir(mp); - if (fd < 0) - return -errno; - } else if (ret) { - return -errno; - } - - if (!fi_args->num_devices) - return 0; - - di_args = *di_ret = malloc(fi_args->num_devices * sizeof(*di_args)); - if (!di_args) - return -errno; - - for (; i <= fi_args->max_id; ++i) { - BUG_ON(ndevs >= fi_args->num_devices); - ret = scrub_device_info(fd, i, &di_args[ndevs]); - if (ret == -ENODEV) - continue; - if (ret) - return ret; - ++ndevs; - } - - BUG_ON(ndevs == 0); - - return 0; -} - int mkdir_p(char *path) { int i; @@ -1155,7 +1087,7 @@ static int scrub_start(int argc, char **argv, int resume) return 12; } - ret = scrub_fs_info(fdmnt, path, &fi_args, &di_args); + ret = get_fs_info(fdmnt, path, &fi_args, &di_args); if (ret) { ERR(!do_quiet, "ERROR: getting dev info for scrub failed: " "%s\n", strerror(-ret)); @@ -1621,7 +1553,7 @@ static int cmd_scrub_status(int argc, char **argv) return 12; } - ret = scrub_fs_info(fdmnt, path, &fi_args, &di_args); + ret = get_fs_info(fdmnt, path, &fi_args, &di_args); if (ret) { fprintf(stderr, "ERROR: getting dev info for scrub failed: " "%s\n", strerror(-ret)); diff --git a/utils.c b/utils.c index 6157115..037f64b 100644 --- a/utils.c +++ b/utils.c @@ -1233,3 +1233,69 @@ int open_file_or_dir(const char *fname) return fd; } +int get_device_info(int fd, u64 devid, + struct btrfs_ioctl_dev_info_args *di_args) +{ + int ret; + + di_args->devid = devid; + memset(&di_args->uuid, ''\0'', sizeof(di_args->uuid)); + + ret = ioctl(fd, BTRFS_IOC_DEV_INFO, di_args); + return ret ? -errno : 0; +} + +int get_fs_info(int fd, char *path, struct btrfs_ioctl_fs_info_args *fi_args, + struct btrfs_ioctl_dev_info_args **di_ret) +{ + int ret = 0; + int ndevs = 0; + int i = 1; + struct btrfs_fs_devices *fs_devices_mnt = NULL; + struct btrfs_ioctl_dev_info_args *di_args; + char mp[BTRFS_PATH_NAME_MAX + 1]; + + memset(fi_args, 0, sizeof(*fi_args)); + + ret = ioctl(fd, BTRFS_IOC_FS_INFO, fi_args); + if (ret && (errno == EINVAL || errno == ENOTTY)) { + /* path is not a mounted btrfs. Try if it''s a device */ + ret = check_mounted_where(fd, path, mp, sizeof(mp), + &fs_devices_mnt); + if (!ret) + return -EINVAL; + if (ret < 0) + return ret; + fi_args->num_devices = 1; + fi_args->max_id = fs_devices_mnt->latest_devid; + i = fs_devices_mnt->latest_devid; + memcpy(fi_args->fsid, fs_devices_mnt->fsid, BTRFS_FSID_SIZE); + close(fd); + fd = open_file_or_dir(mp); + if (fd < 0) + return -errno; + } else if (ret) { + return -errno; + } + + if (!fi_args->num_devices) + return 0; + + di_args = *di_ret = malloc(fi_args->num_devices * sizeof(*di_args)); + if (!di_args) + return -errno; + + for (; i <= fi_args->max_id; ++i) { + BUG_ON(ndevs >= fi_args->num_devices); + ret = get_device_info(fd, i, &di_args[ndevs]); + if (ret == -ENODEV) + continue; + if (ret) + return ret; + ndevs++; + } + + BUG_ON(ndevs == 0); + + return 0; +} diff --git a/utils.h b/utils.h index e281002..e33c231 100644 --- a/utils.h +++ b/utils.h @@ -49,4 +49,8 @@ int get_mountpt(char *dev, char *mntpt, size_t size); int btrfs_scan_block_devices(int run_ioctl); int open_file_or_dir(const char *fname); +int get_device_info(int fd, u64 devid, + struct btrfs_ioctl_dev_info_args *di_args); +int get_fs_info(int fd, char *path, struct btrfs_ioctl_fs_info_args *fi_args, + struct btrfs_ioctl_dev_info_args **di_ret); #endif -- 1.7.10.2 -- To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
Stefan Behrens
2012-May-25 14:07 UTC
[PATCH v5 3/3] Btrfs-progs: add command to get/reset device stats via ioctl
"btrfs device stats" is used to retrieve and print the device stats. "btrfs device stats -z" is used to atomically retrieve, reset and print the stats. Signed-off-by: Stefan Behrens <sbehrens@giantdisaster.de> --- cmds-device.c | 118 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ctree.h | 6 +++ ioctl.h | 33 ++++++++++++++++ man/btrfs.8.in | 14 +++++++ print-tree.c | 6 +++ 5 files changed, 177 insertions(+) diff --git a/cmds-device.c b/cmds-device.c index db625a6..7621cc0 100644 --- a/cmds-device.c +++ b/cmds-device.c @@ -246,11 +246,129 @@ static int cmd_scan_dev(int argc, char **argv) return 0; } +static const char * const cmd_dev_stats_usage[] = { + "btrfs device stats [-z] <path>|<device>", + "Show current device IO stats. -z to reset stats afterwards.", + NULL +}; + +static int cmd_dev_stats(int argc, char **argv) +{ + char *path; + struct btrfs_ioctl_fs_info_args fi_args; + struct btrfs_ioctl_dev_info_args *di_args = NULL; + int ret; + int fdmnt; + int i; + char c; + int fdres = -1; + int err = 0; + int cmd = BTRFS_IOC_GET_DEV_STATS; + + optind = 1; + while ((c = getopt(argc, argv, "z")) != -1) { + switch (c) { + case ''z'': + cmd = BTRFS_IOC_GET_AND_RESET_DEV_STATS; + break; + case ''?'': + default: + fprintf(stderr, "ERROR: device stat args invalid.\n" + " device stat [-z] <path>|<device>\n" + " -z to reset stats after reading.\n"); + return 1; + } + } + + if (optind + 1 != argc) { + fprintf(stderr, "ERROR: device stat needs path|device as single" + " argument\n"); + return 1; + } + + path = argv[optind]; + + fdmnt = open_file_or_dir(path); + if (fdmnt < 0) { + fprintf(stderr, "ERROR: can''t access ''%s''\n", path); + return 12; + } + + ret = get_fs_info(fdmnt, path, &fi_args, &di_args); + if (ret) { + fprintf(stderr, "ERROR: getting dev info for devstats failed: " + "%s\n", strerror(-ret)); + err = 1; + goto out; + } + if (!fi_args.num_devices) { + fprintf(stderr, "ERROR: no devices found\n"); + err = 1; + goto out; + } + + for (i = 0; i < fi_args.num_devices; i++) { + struct btrfs_ioctl_get_dev_stats args = {0}; + __u8 path[BTRFS_DEVICE_PATH_NAME_MAX + 1]; + + strncpy((char *)path, (char *)di_args[i].path, + BTRFS_DEVICE_PATH_NAME_MAX); + path[BTRFS_DEVICE_PATH_NAME_MAX] = ''\0''; + + args.devid = di_args[i].devid; + args.nr_items = BTRFS_DEV_STAT_VALUES_MAX; + + if (ioctl(fdmnt, cmd, &args) < 0) { + fprintf(stderr, "ERROR: ioctl(%s) on %s failed: %s\n", + BTRFS_IOC_GET_AND_RESET_DEV_STATS == cmd ? + "BTRFS_IOC_GET_AND_RESET_DEV_STATS" : + "BTRFS_IOC_GET_DEV_STATS", + path, strerror(errno)); + err = 1; + } else { + if (args.nr_items >= BTRFS_DEV_STAT_WRITE_ERRS + 1) + printf("[%s].write_io_errs %llu\n", + path, + (unsigned long long) args.values[ + BTRFS_DEV_STAT_WRITE_ERRS]); + if (args.nr_items >= BTRFS_DEV_STAT_READ_ERRS + 1) + printf("[%s].read_io_errs %llu\n", + path, + (unsigned long long) args.values[ + BTRFS_DEV_STAT_READ_ERRS]); + if (args.nr_items >= BTRFS_DEV_STAT_FLUSH_ERRS + 1) + printf("[%s].flush_io_errs %llu\n", + path, + (unsigned long long) args.values[ + BTRFS_DEV_STAT_FLUSH_ERRS]); + if (args.nr_items >= BTRFS_DEV_STAT_CORRUPTION_ERRS + 1) + printf("[%s].corruption_errs %llu\n", + path, + (unsigned long long) args.values[ + BTRFS_DEV_STAT_CORRUPTION_ERRS]); + if (args.nr_items >= BTRFS_DEV_STAT_GENERATION_ERRS + 1) + printf("[%s].generation_errs %llu\n", + path, + (unsigned long long) args.values[ + BTRFS_DEV_STAT_GENERATION_ERRS]); + } + } + +out: + free(di_args); + close(fdmnt); + if (fdres > -1) + close(fdres); + + return err; +} + const struct cmd_group device_cmd_group = { device_cmd_group_usage, NULL, { { "add", cmd_add_dev, cmd_add_dev_usage, NULL, 0 }, { "delete", cmd_rm_dev, cmd_rm_dev_usage, NULL, 0 }, { "scan", cmd_scan_dev, cmd_scan_dev_usage, NULL, 0 }, + { "stats", cmd_dev_stats, cmd_dev_stats_usage, NULL, 0 }, { 0, 0, 0, 0, 0 } } }; diff --git a/ctree.h b/ctree.h index 6545c50..86a652d 100644 --- a/ctree.h +++ b/ctree.h @@ -943,6 +943,12 @@ struct btrfs_root { #define BTRFS_BALANCE_ITEM_KEY 248 /* + * Persistantly stores the io stats in the device tree. + * One key for all stats, (0, BTRFS_DEV_STATS_KEY, devid). + */ +#define BTRFS_DEV_STATS_KEY 249 + +/* * string items are for debugging. They just store a short string of * data in the FS */ diff --git a/ioctl.h b/ioctl.h index f2e5d8d..ffc3c29 100644 --- a/ioctl.h +++ b/ioctl.h @@ -272,6 +272,35 @@ struct btrfs_ioctl_logical_ino_args { __u64 inodes; }; +enum btrfs_dev_stat_values { + /* disk I/O failure stats */ + BTRFS_DEV_STAT_WRITE_ERRS, /* EIO or EREMOTEIO from lower layers */ + BTRFS_DEV_STAT_READ_ERRS, /* EIO or EREMOTEIO from lower layers */ + BTRFS_DEV_STAT_FLUSH_ERRS, /* EIO or EREMOTEIO from lower layers */ + + /* stats for indirect indications for I/O failures */ + BTRFS_DEV_STAT_CORRUPTION_ERRS, /* checksum error, bytenr error or + * contents is illegal: this is an + * indication that the block was damaged + * during read or write, or written to + * wrong location or read from wrong + * location */ + BTRFS_DEV_STAT_GENERATION_ERRS, /* an indication that blocks have not + * been written */ + + BTRFS_DEV_STAT_VALUES_MAX +}; + +struct btrfs_ioctl_get_dev_stats { + __u64 devid; /* in */ + __u64 nr_items; /* in/out */ + + /* out values: */ + __u64 values[BTRFS_DEV_STAT_VALUES_MAX]; + + __u64 unused[128 - 2 - BTRFS_DEV_STAT_VALUES_MAX]; /* pad to 1k */ +}; + /* BTRFS_IOC_SNAP_CREATE is no longer used by the btrfs command */ #define BTRFS_IOC_SNAP_CREATE _IOW(BTRFS_IOCTL_MAGIC, 1, \ struct btrfs_ioctl_vol_args) @@ -330,5 +359,9 @@ struct btrfs_ioctl_logical_ino_args { struct btrfs_ioctl_ino_path_args) #define BTRFS_IOC_LOGICAL_INO _IOWR(BTRFS_IOCTL_MAGIC, 36, \ struct btrfs_ioctl_ino_path_args) +#define BTRFS_IOC_GET_DEV_STATS _IOWR(BTRFS_IOCTL_MAGIC, 52, \ + struct btrfs_ioctl_get_dev_stats) +#define BTRFS_IOC_GET_AND_RESET_DEV_STATS _IOWR(BTRFS_IOCTL_MAGIC, 53, \ + struct btrfs_ioctl_get_dev_stats) #endif diff --git a/man/btrfs.8.in b/man/btrfs.8.in index be478e0..c903fee 100644 --- a/man/btrfs.8.in +++ b/man/btrfs.8.in @@ -35,6 +35,8 @@ btrfs \- control a btrfs filesystem .PP \fBbtrfs\fP \fBdevice show\fP\fI [--all-devices|<uuid>|<label>]\fP .PP +\fBbtrfs\fP \fBdevice stats\fP [-z] {\fI<path>\fP|\fI<device>\fP} +.PP \fBbtrfs\fP \fBdevice add\fP\fI <device> [<device>...] <path> \fP .PP \fBbtrfs\fP \fBdevice delete\fP\fI <device> [<device>...] <path> \fP @@ -214,6 +216,18 @@ Balance the chunks of the filesystem identified by \fI<path>\fR across the devices. .TP +\fBdevice stats\fP [-z] {\fI<path>\fP|\fI<device>\fP} +Read and print the device IO stats for all devices of the filesystem +identified by \fI<path>\fR or for a single \fI<device>\fR. + +.RS +\fIOptions\fR +.TP +.B -z +Reset stats to zero after reading them. +.RE +.TP + \fBdevice add\fR\fI <dev> [<dev>..] <path>\fR Add device(s) to the filesystem identified by \fI<path>\fR. .TP diff --git a/print-tree.c b/print-tree.c index fc134c0..ff8c5e1 100644 --- a/print-tree.c +++ b/print-tree.c @@ -357,6 +357,9 @@ static void print_key_type(u8 type) case BTRFS_STRING_ITEM_KEY: printf("STRING_ITEM"); break; + case BTRFS_DEV_STATS_KEY: + printf("DEV_STATS_ITEM"); + break; default: printf("UNKNOWN"); }; @@ -609,6 +612,9 @@ void btrfs_print_leaf(struct btrfs_root *root, struct extent_buffer *l) str = l->data + btrfs_item_ptr_offset(l, i); printf("\t\titem data %.*s\n", btrfs_item_size(l, item), str); break; + case BTRFS_DEV_STATS_KEY: + printf("\t\tdevice stats\n"); + break; }; fflush(stdout); } -- 1.7.10.2 -- To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
Hugo Mills
2012-Jun-06 18:16 UTC
Re: [PATCH v5 2/3] Btrfs-progs: make two utility functions globally available
On Fri, May 25, 2012 at 04:07:17PM +0200, Stefan Behrens wrote:> Two convenient utility functions that have so far been local to scrub are > moved to utils.c. > They will be used in the device stats code in a following commit. >[snip]> - > -static int scrub_fs_info(int fd, char *path, > - struct btrfs_ioctl_fs_info_args *fi_args, > - struct btrfs_ioctl_dev_info_args **di_ret) > -{ > - int ret = 0; > - int ndevs = 0; > - int i = 1; > - struct btrfs_fs_devices *fs_devices_mnt = NULL; > - struct btrfs_ioctl_dev_info_args *di_args; > - char mp[BTRFS_PATH_NAME_MAX + 1]; > - > - memset(fi_args, 0, sizeof(*fi_args)); > - > - ret = ioctl(fd, BTRFS_IOC_FS_INFO, fi_args); > - if (ret && errno == EINVAL) {what''s removed here...> - /* path is no mounted btrfs. try if it''s a device */ > - ret = check_mounted_where(fd, path, mp, sizeof(mp), > - &fs_devices_mnt); > - if (!ret) > - return -EINVAL; > - if (ret < 0) > - return ret; > - fi_args->num_devices = 1; > - fi_args->max_id = fs_devices_mnt->latest_devid; > - i = fs_devices_mnt->latest_devid; > - memcpy(fi_args->fsid, fs_devices_mnt->fsid, BTRFS_FSID_SIZE); > - close(fd); > - fd = open_file_or_dir(mp); > - if (fd < 0) > - return -errno; > - } else if (ret) { > - return -errno; > - } > - > - if (!fi_args->num_devices) > - return 0; > - > - di_args = *di_ret = malloc(fi_args->num_devices * sizeof(*di_args)); > - if (!di_args) > - return -errno; > - > - for (; i <= fi_args->max_id; ++i) { > - BUG_ON(ndevs >= fi_args->num_devices); > - ret = scrub_device_info(fd, i, &di_args[ndevs]); > - if (ret == -ENODEV) > - continue; > - if (ret) > - return ret; > - ++ndevs; > - }[snip]> diff --git a/utils.c b/utils.c > index 6157115..037f64b 100644 > --- a/utils.c > +++ b/utils.c > @@ -1233,3 +1233,69 @@ int open_file_or_dir(const char *fname) > return fd; > } > > +int get_device_info(int fd, u64 devid, > + struct btrfs_ioctl_dev_info_args *di_args) > +{ > + int ret; > + > + di_args->devid = devid; > + memset(&di_args->uuid, ''\0'', sizeof(di_args->uuid)); > + > + ret = ioctl(fd, BTRFS_IOC_DEV_INFO, di_args); > + return ret ? -errno : 0; > +} > + > +int get_fs_info(int fd, char *path, struct btrfs_ioctl_fs_info_args *fi_args, > + struct btrfs_ioctl_dev_info_args **di_ret) > +{ > + int ret = 0; > + int ndevs = 0; > + int i = 1; > + struct btrfs_fs_devices *fs_devices_mnt = NULL; > + struct btrfs_ioctl_dev_info_args *di_args; > + char mp[BTRFS_PATH_NAME_MAX + 1]; > + > + memset(fi_args, 0, sizeof(*fi_args)); > + > + ret = ioctl(fd, BTRFS_IOC_FS_INFO, fi_args); > + if (ret && (errno == EINVAL || errno == ENOTTY)) {... isn''t what''s added here. (And the same for the function name). It''s possibly a minor point, but I did end up having to do a diff with a Mark-I eyeball just to get a merge of this code right. Hugo.> + /* path is not a mounted btrfs. Try if it''s a device */ > + ret = check_mounted_where(fd, path, mp, sizeof(mp), > + &fs_devices_mnt); > + if (!ret) > + return -EINVAL; > + if (ret < 0) > + return ret; > + fi_args->num_devices = 1; > + fi_args->max_id = fs_devices_mnt->latest_devid; > + i = fs_devices_mnt->latest_devid; > + memcpy(fi_args->fsid, fs_devices_mnt->fsid, BTRFS_FSID_SIZE); > + close(fd); > + fd = open_file_or_dir(mp); > + if (fd < 0) > + return -errno; > + } else if (ret) { > + return -errno; > + } > + > + if (!fi_args->num_devices) > + return 0; > + > + di_args = *di_ret = malloc(fi_args->num_devices * sizeof(*di_args)); > + if (!di_args) > + return -errno; > + > + for (; i <= fi_args->max_id; ++i) { > + BUG_ON(ndevs >= fi_args->num_devices); > + ret = get_device_info(fd, i, &di_args[ndevs]); > + if (ret == -ENODEV) > + continue; > + if (ret) > + return ret; > + ndevs++; > + } > + > + BUG_ON(ndevs == 0); > + > + return 0; > +} > diff --git a/utils.h b/utils.h > index e281002..e33c231 100644 > --- a/utils.h > +++ b/utils.h > @@ -49,4 +49,8 @@ int get_mountpt(char *dev, char *mntpt, size_t size); > > int btrfs_scan_block_devices(int run_ioctl); > int open_file_or_dir(const char *fname); > +int get_device_info(int fd, u64 devid, > + struct btrfs_ioctl_dev_info_args *di_args); > +int get_fs_info(int fd, char *path, struct btrfs_ioctl_fs_info_args *fi_args, > + struct btrfs_ioctl_dev_info_args **di_ret); > #endif-- === Hugo Mills: hugo@... carfax.org.uk | darksatanic.net | lug.org.uk == PGP key: 515C238D from wwwkeys.eu.pgp.net or http://www.carfax.org.uk --- Happiness is mandatory. Are you happy? ---
Stefan Behrens
2012-Jun-07 12:35 UTC
Re: [PATCH v5 2/3] Btrfs-progs: make two utility functions globally available
On Wed, 6 Jun 2012 19:16:26 +0100, Hugo Mills wrote:> On Fri, May 25, 2012 at 04:07:17PM +0200, Stefan Behrens wrote: >> Two convenient utility functions that have so far been local to scrub are >> moved to utils.c. >> They will be used in the device stats code in a following commit. >> > [snip] >> - >> -static int scrub_fs_info(int fd, char *path, >> - struct btrfs_ioctl_fs_info_args *fi_args, >> - struct btrfs_ioctl_dev_info_args **di_ret) >> -{ >> - int ret = 0; >> - int ndevs = 0; >> - int i = 1; >> - struct btrfs_fs_devices *fs_devices_mnt = NULL; >> - struct btrfs_ioctl_dev_info_args *di_args; >> - char mp[BTRFS_PATH_NAME_MAX + 1]; >> - >> - memset(fi_args, 0, sizeof(*fi_args)); >> - >> - ret = ioctl(fd, BTRFS_IOC_FS_INFO, fi_args); >> - if (ret && errno == EINVAL) { > > what''s removed here... > >> - /* path is no mounted btrfs. try if it''s a device */ >> - ret = check_mounted_where(fd, path, mp, sizeof(mp), >> - &fs_devices_mnt); >> - if (!ret) >> - return -EINVAL; >> - if (ret < 0) >> - return ret; >> - fi_args->num_devices = 1; >> - fi_args->max_id = fs_devices_mnt->latest_devid; >> - i = fs_devices_mnt->latest_devid; >> - memcpy(fi_args->fsid, fs_devices_mnt->fsid, BTRFS_FSID_SIZE); >> - close(fd); >> - fd = open_file_or_dir(mp); >> - if (fd < 0) >> - return -errno; >> - } else if (ret) { >> - return -errno; >> - } >> - >> - if (!fi_args->num_devices) >> - return 0; >> - >> - di_args = *di_ret = malloc(fi_args->num_devices * sizeof(*di_args)); >> - if (!di_args) >> - return -errno; >> - >> - for (; i <= fi_args->max_id; ++i) { >> - BUG_ON(ndevs >= fi_args->num_devices); >> - ret = scrub_device_info(fd, i, &di_args[ndevs]); >> - if (ret == -ENODEV) >> - continue; >> - if (ret) >> - return ret; >> - ++ndevs; >> - } > > [snip] > >> diff --git a/utils.c b/utils.c >> index 6157115..037f64b 100644 >> --- a/utils.c >> +++ b/utils.c >> @@ -1233,3 +1233,69 @@ int open_file_or_dir(const char *fname) >> return fd; >> } >> >> +int get_device_info(int fd, u64 devid, >> + struct btrfs_ioctl_dev_info_args *di_args) >> +{ >> + int ret; >> + >> + di_args->devid = devid; >> + memset(&di_args->uuid, ''\0'', sizeof(di_args->uuid)); >> + >> + ret = ioctl(fd, BTRFS_IOC_DEV_INFO, di_args); >> + return ret ? -errno : 0; >> +} >> + >> +int get_fs_info(int fd, char *path, struct btrfs_ioctl_fs_info_args *fi_args, >> + struct btrfs_ioctl_dev_info_args **di_ret) >> +{ >> + int ret = 0; >> + int ndevs = 0; >> + int i = 1; >> + struct btrfs_fs_devices *fs_devices_mnt = NULL; >> + struct btrfs_ioctl_dev_info_args *di_args; >> + char mp[BTRFS_PATH_NAME_MAX + 1]; >> + >> + memset(fi_args, 0, sizeof(*fi_args)); >> + >> + ret = ioctl(fd, BTRFS_IOC_FS_INFO, fi_args); >> + if (ret && (errno == EINVAL || errno == ENOTTY)) { > > ... isn''t what''s added here. (And the same for the function name). > It''s possibly a minor point, but I did end up having to do a diff with > a Mark-I eyeball just to get a merge of this code right. > > Hugo.Your displeasure with these "hidden" changes is understood. Thanks!> >> + /* path is not a mounted btrfs. Try if it''s a device */ >> + ret = check_mounted_where(fd, path, mp, sizeof(mp), >> + &fs_devices_mnt); >> + if (!ret) >> + return -EINVAL; >> + if (ret < 0) >> + return ret; >> + fi_args->num_devices = 1; >> + fi_args->max_id = fs_devices_mnt->latest_devid; >> + i = fs_devices_mnt->latest_devid; >> + memcpy(fi_args->fsid, fs_devices_mnt->fsid, BTRFS_FSID_SIZE); >> + close(fd); >> + fd = open_file_or_dir(mp); >> + if (fd < 0) >> + return -errno; >> + } else if (ret) { >> + return -errno; >> + } >> + >> + if (!fi_args->num_devices) >> + return 0; >> + >> + di_args = *di_ret = malloc(fi_args->num_devices * sizeof(*di_args)); >> + if (!di_args) >> + return -errno; >> + >> + for (; i <= fi_args->max_id; ++i) { >> + BUG_ON(ndevs >= fi_args->num_devices); >> + ret = get_device_info(fd, i, &di_args[ndevs]); >> + if (ret == -ENODEV) >> + continue; >> + if (ret) >> + return ret; >> + ndevs++; >> + } >> + >> + BUG_ON(ndevs == 0); >> + >> + return 0; >> +} >> diff --git a/utils.h b/utils.h >> index e281002..e33c231 100644 >> --- a/utils.h >> +++ b/utils.h >> @@ -49,4 +49,8 @@ int get_mountpt(char *dev, char *mntpt, size_t size); >> >> int btrfs_scan_block_devices(int run_ioctl); >> int open_file_or_dir(const char *fname); >> +int get_device_info(int fd, u64 devid, >> + struct btrfs_ioctl_dev_info_args *di_args); >> +int get_fs_info(int fd, char *path, struct btrfs_ioctl_fs_info_args *fi_args, >> + struct btrfs_ioctl_dev_info_args **di_ret); >> #endif >-- To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
Goffredo Baroncelli
2012-Jun-07 19:38 UTC
Re: [PATCH v5 1/3] Btrfs-progs: move open_file_or_dir() to utils.c
Hi Stefan, On 05/25/2012 04:07 PM, Stefan Behrens wrote:> This is a preparation step to add support for device stats. The definition > of the function open_file_or_dir() is moved from common.c to utils.c in > order to be able to share some common code between scrub and the device > stats in the following step. That common code uses open_file_or_dir(). > Since open_file_or_dir() makes use of the function dirfd(3), the required > XOPEN version was raised from 6 to 7. > > Signed-off-by: Stefan Behrens <sbehrens@giantdisaster.de> > --- > Makefile | 4 ++-- > btrfsctl.c | 28 ---------------------------- > cmds-balance.c | 1 + > cmds-inspect.c | 1 + > cmds-subvolume.c | 1 + > commands.h | 3 --- > common.c | 46 ---------------------------------------------- > utils.c | 31 +++++++++++++++++++++++++++++-- > utils.h | 3 +++ > 9 files changed, 37 insertions(+), 81 deletions(-) > > diff --git a/Makefile b/Makefile > index 79818e6..fe2b432 100644 > --- a/Makefile > +++ b/Makefile > @@ -39,8 +39,8 @@ all: version $(progs) manpages > version: > bash version.sh > > -btrfs: $(objects) btrfs.o help.o common.o $(cmds_objects) > - $(CC) $(CFLAGS) -o btrfs btrfs.o help.o common.o $(cmds_objects) \ > +btrfs: $(objects) btrfs.o help.o $(cmds_objects) > + $(CC) $(CFLAGS) -o btrfs btrfs.o help.o $(cmds_objects) \ > $(objects) $(LDFLAGS) $(LIBS) -lpthread > > calc-size: $(objects) calc-size.o > diff --git a/btrfsctl.c b/btrfsctl.c > index d45e2a7..f0584f3 100644 > --- a/btrfsctl.c > +++ b/btrfsctl.c > @@ -63,34 +63,6 @@ static void print_usage(void) > exit(1); > } > > -static int open_file_or_dir(const char *fname) > -{ > - int ret; > - struct stat st; > - DIR *dirstream; > - int fd; > - > - ret = stat(fname, &st); > - if (ret < 0) { > - perror("stat:"); > - exit(1); > - } > - if (S_ISDIR(st.st_mode)) { > - dirstream = opendir(fname); > - if (!dirstream) { > - perror("opendir"); > - exit(1); > - } > - fd = dirfd(dirstream); > - } else { > - fd = open(fname, O_RDWR); > - } > - if (fd < 0) { > - perror("open"); > - exit(1); > - } > - return fd; > -}I suppose that you want to remove open_file_or_dir() from btrfsctl.c and use the one provided by utils.c (or common.c). However I have to point out that these two function are a bit different: the first one does exit() in case of error; the second one returns an error. In any case I think that it is time to deprecate and remove btrfsctl. BR Goffredo> int main(int ac, char **av) > { > char *fname = NULL; > diff --git a/cmds-balance.c b/cmds-balance.c > index 38a7426..5793b5c 100644 > --- a/cmds-balance.c > +++ b/cmds-balance.c > @@ -26,6 +26,7 @@ > #include "ctree.h" > #include "ioctl.h" > #include "volumes.h" > +#include "utils.h" > > #include "commands.h" > > diff --git a/cmds-inspect.c b/cmds-inspect.c > index 2f0228f..7a8785b 100644 > --- a/cmds-inspect.c > +++ b/cmds-inspect.c > @@ -22,6 +22,7 @@ > > #include "kerncompat.h" > #include "ioctl.h" > +#include "utils.h" > > #include "commands.h" > > diff --git a/cmds-subvolume.c b/cmds-subvolume.c > index 950fa8f..8ecd3f4 100644 > --- a/cmds-subvolume.c > +++ b/cmds-subvolume.c > @@ -26,6 +26,7 @@ > > #include "kerncompat.h" > #include "ioctl.h" > +#include "utils.h" > > #include "commands.h" > > diff --git a/commands.h b/commands.h > index a303a50..aea4cb1 100644 > --- a/commands.h > +++ b/commands.h > @@ -79,9 +79,6 @@ void help_ambiguous_token(const char *arg, const struct cmd_group *grp); > > void help_command_group(const struct cmd_group *grp, int argc, char **argv); > > -/* common.c */ > -int open_file_or_dir(const char *fname); > - > extern const struct cmd_group subvolume_cmd_group; > extern const struct cmd_group filesystem_cmd_group; > extern const struct cmd_group balance_cmd_group; > diff --git a/common.c b/common.c > deleted file mode 100644 > index 03f6570..0000000 > --- a/common.c > +++ /dev/null > @@ -1,46 +0,0 @@ > -/* > - * This program is free software; you can redistribute it and/or > - * modify it under the terms of the GNU General Public > - * License v2 as published by the Free Software Foundation. > - * > - * This program is distributed in the hope that it will be useful, > - * but WITHOUT ANY WARRANTY; without even the implied warranty of > - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU > - * General Public License for more details. > - * > - * You should have received a copy of the GNU General Public > - * License along with this program; if not, write to the > - * Free Software Foundation, Inc., 59 Temple Place - Suite 330, > - * Boston, MA 021110-1307, USA. > - */ > - > -#include <sys/types.h> > -#include <sys/stat.h> > -#include <dirent.h> > -#include <fcntl.h> > - > -int open_file_or_dir(const char *fname) > -{ > - int ret; > - struct stat st; > - DIR *dirstream; > - int fd; > - > - ret = stat(fname, &st); > - if (ret < 0) { > - return -1; > - } > - if (S_ISDIR(st.st_mode)) { > - dirstream = opendir(fname); > - if (!dirstream) { > - return -2; > - } > - fd = dirfd(dirstream); > - } else { > - fd = open(fname, O_RDWR); > - } > - if (fd < 0) { > - return -3; > - } > - return fd; > -} > diff --git a/utils.c b/utils.c > index ee7fa1b..6157115 100644 > --- a/utils.c > +++ b/utils.c > @@ -16,8 +16,9 @@ > * Boston, MA 021110-1307, USA. > */ > > -#define _XOPEN_SOURCE 600 > -#define __USE_XOPEN2K > +#define _XOPEN_SOURCE 700 > +#define __USE_XOPEN2K8 > +#define __XOPEN2K8 /* due to an error in dirent.h, to get dirfd() */ > #include <stdio.h> > #include <stdlib.h> > #ifndef __CHECKER__ > @@ -1206,3 +1207,29 @@ scan_again: > return 0; > } > > +int open_file_or_dir(const char *fname) > +{ > + int ret; > + struct stat st; > + DIR *dirstream; > + int fd; > + > + ret = stat(fname, &st); > + if (ret < 0) { > + return -1; > + } > + if (S_ISDIR(st.st_mode)) { > + dirstream = opendir(fname); > + if (!dirstream) { > + return -2; > + } > + fd = dirfd(dirstream); > + } else { > + fd = open(fname, O_RDWR); > + } > + if (fd < 0) { > + return -3; > + } > + return fd; > +} > + > diff --git a/utils.h b/utils.h > index c5f55e1..e281002 100644 > --- a/utils.h > +++ b/utils.h > @@ -19,6 +19,8 @@ > #ifndef __UTILS__ > #define __UTILS__ > > +#include "ctree.h" > + > #define BTRFS_MKFS_SYSTEM_GROUP_SIZE (4 * 1024 * 1024) > > int make_btrfs(int fd, const char *device, const char *label, > @@ -46,4 +48,5 @@ int check_label(char *input); > int get_mountpt(char *dev, char *mntpt, size_t size); > > int btrfs_scan_block_devices(int run_ioctl); > +int open_file_or_dir(const char *fname); > #endif-- To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
Stefan Behrens
2012-Jun-08 07:30 UTC
Re: [PATCH v5 1/3] Btrfs-progs: move open_file_or_dir() to utils.c
On Thu, 07 Jun 2012 21:38:25 +0200, Goffredo Baroncelli wrote:> Hi Stefan, > > On 05/25/2012 04:07 PM, Stefan Behrens wrote: >> This is a preparation step to add support for device stats. The definition >> of the function open_file_or_dir() is moved from common.c to utils.c in >> order to be able to share some common code between scrub and the device >> stats in the following step. That common code uses open_file_or_dir(). >> Since open_file_or_dir() makes use of the function dirfd(3), the required >> XOPEN version was raised from 6 to 7. >> >> Signed-off-by: Stefan Behrens <sbehrens@giantdisaster.de> >> --- >> Makefile | 4 ++-- >> btrfsctl.c | 28 ---------------------------- >> cmds-balance.c | 1 + >> cmds-inspect.c | 1 + >> cmds-subvolume.c | 1 + >> commands.h | 3 --- >> common.c | 46 ---------------------------------------------- >> utils.c | 31 +++++++++++++++++++++++++++++-- >> utils.h | 3 +++ >> 9 files changed, 37 insertions(+), 81 deletions(-) >> >> diff --git a/Makefile b/Makefile >> index 79818e6..fe2b432 100644 >> --- a/Makefile >> +++ b/Makefile >> @@ -39,8 +39,8 @@ all: version $(progs) manpages >> version: >> bash version.sh >> >> -btrfs: $(objects) btrfs.o help.o common.o $(cmds_objects) >> - $(CC) $(CFLAGS) -o btrfs btrfs.o help.o common.o $(cmds_objects) \ >> +btrfs: $(objects) btrfs.o help.o $(cmds_objects) >> + $(CC) $(CFLAGS) -o btrfs btrfs.o help.o $(cmds_objects) \ >> $(objects) $(LDFLAGS) $(LIBS) -lpthread >> >> calc-size: $(objects) calc-size.o >> diff --git a/btrfsctl.c b/btrfsctl.c >> index d45e2a7..f0584f3 100644 >> --- a/btrfsctl.c >> +++ b/btrfsctl.c >> @@ -63,34 +63,6 @@ static void print_usage(void) >> exit(1); >> } >> >> -static int open_file_or_dir(const char *fname) >> -{ >> - int ret; >> - struct stat st; >> - DIR *dirstream; >> - int fd; >> - >> - ret = stat(fname, &st); >> - if (ret < 0) { >> - perror("stat:"); >> - exit(1); >> - } >> - if (S_ISDIR(st.st_mode)) { >> - dirstream = opendir(fname); >> - if (!dirstream) { >> - perror("opendir"); >> - exit(1); >> - } >> - fd = dirfd(dirstream); >> - } else { >> - fd = open(fname, O_RDWR); >> - } >> - if (fd < 0) { >> - perror("open"); >> - exit(1); >> - } >> - return fd; >> -} > > I suppose that you want to remove open_file_or_dir() from btrfsctl.c and > use the one provided by utils.c (or common.c). However I have to point > out that these two function are a bit different: the first one does > exit() in case of error; the second one returns an error. > > In any case I think that it is time to deprecate and remove btrfsctl. > > BR > GoffredoHi Goffredo, Yes, I was aware of this difference but did not want to spend _any_ additional effort in the deprecated btrfsctl tool. Maybe it would have been better to just disable its default build in the Makefile. Does anybody have any objections against removing the btrfsctl, btrfs-show and btrfs-vol tools from the "all" target of Makefile?>> int main(int ac, char **av) >> { >> char *fname = NULL; >> diff --git a/cmds-balance.c b/cmds-balance.c >> index 38a7426..5793b5c 100644 >> --- a/cmds-balance.c >> +++ b/cmds-balance.c >> @@ -26,6 +26,7 @@ >> #include "ctree.h" >> #include "ioctl.h" >> #include "volumes.h" >> +#include "utils.h" >> >> #include "commands.h" >> >> diff --git a/cmds-inspect.c b/cmds-inspect.c >> index 2f0228f..7a8785b 100644 >> --- a/cmds-inspect.c >> +++ b/cmds-inspect.c >> @@ -22,6 +22,7 @@ >> >> #include "kerncompat.h" >> #include "ioctl.h" >> +#include "utils.h" >> >> #include "commands.h" >> >> diff --git a/cmds-subvolume.c b/cmds-subvolume.c >> index 950fa8f..8ecd3f4 100644 >> --- a/cmds-subvolume.c >> +++ b/cmds-subvolume.c >> @@ -26,6 +26,7 @@ >> >> #include "kerncompat.h" >> #include "ioctl.h" >> +#include "utils.h" >> >> #include "commands.h" >> >> diff --git a/commands.h b/commands.h >> index a303a50..aea4cb1 100644 >> --- a/commands.h >> +++ b/commands.h >> @@ -79,9 +79,6 @@ void help_ambiguous_token(const char *arg, const struct cmd_group *grp); >> >> void help_command_group(const struct cmd_group *grp, int argc, char **argv); >> >> -/* common.c */ >> -int open_file_or_dir(const char *fname); >> - >> extern const struct cmd_group subvolume_cmd_group; >> extern const struct cmd_group filesystem_cmd_group; >> extern const struct cmd_group balance_cmd_group; >> diff --git a/common.c b/common.c >> deleted file mode 100644 >> index 03f6570..0000000 >> --- a/common.c >> +++ /dev/null >> @@ -1,46 +0,0 @@ >> -/* >> - * This program is free software; you can redistribute it and/or >> - * modify it under the terms of the GNU General Public >> - * License v2 as published by the Free Software Foundation. >> - * >> - * This program is distributed in the hope that it will be useful, >> - * but WITHOUT ANY WARRANTY; without even the implied warranty of >> - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU >> - * General Public License for more details. >> - * >> - * You should have received a copy of the GNU General Public >> - * License along with this program; if not, write to the >> - * Free Software Foundation, Inc., 59 Temple Place - Suite 330, >> - * Boston, MA 021110-1307, USA. >> - */ >> - >> -#include <sys/types.h> >> -#include <sys/stat.h> >> -#include <dirent.h> >> -#include <fcntl.h> >> - >> -int open_file_or_dir(const char *fname) >> -{ >> - int ret; >> - struct stat st; >> - DIR *dirstream; >> - int fd; >> - >> - ret = stat(fname, &st); >> - if (ret < 0) { >> - return -1; >> - } >> - if (S_ISDIR(st.st_mode)) { >> - dirstream = opendir(fname); >> - if (!dirstream) { >> - return -2; >> - } >> - fd = dirfd(dirstream); >> - } else { >> - fd = open(fname, O_RDWR); >> - } >> - if (fd < 0) { >> - return -3; >> - } >> - return fd; >> -} >> diff --git a/utils.c b/utils.c >> index ee7fa1b..6157115 100644 >> --- a/utils.c >> +++ b/utils.c >> @@ -16,8 +16,9 @@ >> * Boston, MA 021110-1307, USA. >> */ >> >> -#define _XOPEN_SOURCE 600 >> -#define __USE_XOPEN2K >> +#define _XOPEN_SOURCE 700 >> +#define __USE_XOPEN2K8 >> +#define __XOPEN2K8 /* due to an error in dirent.h, to get dirfd() */ >> #include <stdio.h> >> #include <stdlib.h> >> #ifndef __CHECKER__ >> @@ -1206,3 +1207,29 @@ scan_again: >> return 0; >> } >> >> +int open_file_or_dir(const char *fname) >> +{ >> + int ret; >> + struct stat st; >> + DIR *dirstream; >> + int fd; >> + >> + ret = stat(fname, &st); >> + if (ret < 0) { >> + return -1; >> + } >> + if (S_ISDIR(st.st_mode)) { >> + dirstream = opendir(fname); >> + if (!dirstream) { >> + return -2; >> + } >> + fd = dirfd(dirstream); >> + } else { >> + fd = open(fname, O_RDWR); >> + } >> + if (fd < 0) { >> + return -3; >> + } >> + return fd; >> +} >> + >> diff --git a/utils.h b/utils.h >> index c5f55e1..e281002 100644 >> --- a/utils.h >> +++ b/utils.h >> @@ -19,6 +19,8 @@ >> #ifndef __UTILS__ >> #define __UTILS__ >> >> +#include "ctree.h" >> + >> #define BTRFS_MKFS_SYSTEM_GROUP_SIZE (4 * 1024 * 1024) >> >> int make_btrfs(int fd, const char *device, const char *label, >> @@ -46,4 +48,5 @@ int check_label(char *input); >> int get_mountpt(char *dev, char *mntpt, size_t size); >> >> int btrfs_scan_block_devices(int run_ioctl); >> +int open_file_or_dir(const char *fname); >> #endif >-- To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
Anand Jain
2012-Oct-31 10:34 UTC
Re: [PATCH v5 1/3] Btrfs-progs: move open_file_or_dir() to utils.c
Stefan, This is useful. Do you have something which can apply on the latest. Thanks, Anand On 25/05/12 22:07, Stefan Behrens wrote:> This is a preparation step to add support for device stats. The definition > of the function open_file_or_dir() is moved from common.c to utils.c in > order to be able to share some common code between scrub and the device > stats in the following step. That common code uses open_file_or_dir(). > Since open_file_or_dir() makes use of the function dirfd(3), the required > XOPEN version was raised from 6 to 7. > > Signed-off-by: Stefan Behrens <sbehrens@giantdisaster.de> > --- > Makefile | 4 ++-- > btrfsctl.c | 28 ---------------------------- > cmds-balance.c | 1 + > cmds-inspect.c | 1 + > cmds-subvolume.c | 1 + > commands.h | 3 --- > common.c | 46 ---------------------------------------------- > utils.c | 31 +++++++++++++++++++++++++++++-- > utils.h | 3 +++ > 9 files changed, 37 insertions(+), 81 deletions(-) > > diff --git a/Makefile b/Makefile > index 79818e6..fe2b432 100644 > --- a/Makefile > +++ b/Makefile > @@ -39,8 +39,8 @@ all: version $(progs) manpages > version: > bash version.sh > > -btrfs: $(objects) btrfs.o help.o common.o $(cmds_objects) > - $(CC) $(CFLAGS) -o btrfs btrfs.o help.o common.o $(cmds_objects) \ > +btrfs: $(objects) btrfs.o help.o $(cmds_objects) > + $(CC) $(CFLAGS) -o btrfs btrfs.o help.o $(cmds_objects) \ > $(objects) $(LDFLAGS) $(LIBS) -lpthread > > calc-size: $(objects) calc-size.o > diff --git a/btrfsctl.c b/btrfsctl.c > index d45e2a7..f0584f3 100644 > --- a/btrfsctl.c > +++ b/btrfsctl.c > @@ -63,34 +63,6 @@ static void print_usage(void) > exit(1); > } > > -static int open_file_or_dir(const char *fname) > -{ > - int ret; > - struct stat st; > - DIR *dirstream; > - int fd; > - > - ret = stat(fname, &st); > - if (ret < 0) { > - perror("stat:"); > - exit(1); > - } > - if (S_ISDIR(st.st_mode)) { > - dirstream = opendir(fname); > - if (!dirstream) { > - perror("opendir"); > - exit(1); > - } > - fd = dirfd(dirstream); > - } else { > - fd = open(fname, O_RDWR); > - } > - if (fd < 0) { > - perror("open"); > - exit(1); > - } > - return fd; > -} > int main(int ac, char **av) > { > char *fname = NULL; > diff --git a/cmds-balance.c b/cmds-balance.c > index 38a7426..5793b5c 100644 > --- a/cmds-balance.c > +++ b/cmds-balance.c > @@ -26,6 +26,7 @@ > #include "ctree.h" > #include "ioctl.h" > #include "volumes.h" > +#include "utils.h" > > #include "commands.h" > > diff --git a/cmds-inspect.c b/cmds-inspect.c > index 2f0228f..7a8785b 100644 > --- a/cmds-inspect.c > +++ b/cmds-inspect.c > @@ -22,6 +22,7 @@ > > #include "kerncompat.h" > #include "ioctl.h" > +#include "utils.h" > > #include "commands.h" > > diff --git a/cmds-subvolume.c b/cmds-subvolume.c > index 950fa8f..8ecd3f4 100644 > --- a/cmds-subvolume.c > +++ b/cmds-subvolume.c > @@ -26,6 +26,7 @@ > > #include "kerncompat.h" > #include "ioctl.h" > +#include "utils.h" > > #include "commands.h" > > diff --git a/commands.h b/commands.h > index a303a50..aea4cb1 100644 > --- a/commands.h > +++ b/commands.h > @@ -79,9 +79,6 @@ void help_ambiguous_token(const char *arg, const struct cmd_group *grp); > > void help_command_group(const struct cmd_group *grp, int argc, char **argv); > > -/* common.c */ > -int open_file_or_dir(const char *fname); > - > extern const struct cmd_group subvolume_cmd_group; > extern const struct cmd_group filesystem_cmd_group; > extern const struct cmd_group balance_cmd_group; > diff --git a/common.c b/common.c > deleted file mode 100644 > index 03f6570..0000000 > --- a/common.c > +++ /dev/null > @@ -1,46 +0,0 @@ > -/* > - * This program is free software; you can redistribute it and/or > - * modify it under the terms of the GNU General Public > - * License v2 as published by the Free Software Foundation. > - * > - * This program is distributed in the hope that it will be useful, > - * but WITHOUT ANY WARRANTY; without even the implied warranty of > - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU > - * General Public License for more details. > - * > - * You should have received a copy of the GNU General Public > - * License along with this program; if not, write to the > - * Free Software Foundation, Inc., 59 Temple Place - Suite 330, > - * Boston, MA 021110-1307, USA. > - */ > - > -#include <sys/types.h> > -#include <sys/stat.h> > -#include <dirent.h> > -#include <fcntl.h> > - > -int open_file_or_dir(const char *fname) > -{ > - int ret; > - struct stat st; > - DIR *dirstream; > - int fd; > - > - ret = stat(fname, &st); > - if (ret < 0) { > - return -1; > - } > - if (S_ISDIR(st.st_mode)) { > - dirstream = opendir(fname); > - if (!dirstream) { > - return -2; > - } > - fd = dirfd(dirstream); > - } else { > - fd = open(fname, O_RDWR); > - } > - if (fd < 0) { > - return -3; > - } > - return fd; > -} > diff --git a/utils.c b/utils.c > index ee7fa1b..6157115 100644 > --- a/utils.c > +++ b/utils.c > @@ -16,8 +16,9 @@ > * Boston, MA 021110-1307, USA. > */ > > -#define _XOPEN_SOURCE 600 > -#define __USE_XOPEN2K > +#define _XOPEN_SOURCE 700 > +#define __USE_XOPEN2K8 > +#define __XOPEN2K8 /* due to an error in dirent.h, to get dirfd() */ > #include <stdio.h> > #include <stdlib.h> > #ifndef __CHECKER__ > @@ -1206,3 +1207,29 @@ scan_again: > return 0; > } > > +int open_file_or_dir(const char *fname) > +{ > + int ret; > + struct stat st; > + DIR *dirstream; > + int fd; > + > + ret = stat(fname, &st); > + if (ret < 0) { > + return -1; > + } > + if (S_ISDIR(st.st_mode)) { > + dirstream = opendir(fname); > + if (!dirstream) { > + return -2; > + } > + fd = dirfd(dirstream); > + } else { > + fd = open(fname, O_RDWR); > + } > + if (fd < 0) { > + return -3; > + } > + return fd; > +} > + > diff --git a/utils.h b/utils.h > index c5f55e1..e281002 100644 > --- a/utils.h > +++ b/utils.h > @@ -19,6 +19,8 @@ > #ifndef __UTILS__ > #define __UTILS__ > > +#include "ctree.h" > + > #define BTRFS_MKFS_SYSTEM_GROUP_SIZE (4 * 1024 * 1024) > > int make_btrfs(int fd, const char *device, const char *label, > @@ -46,4 +48,5 @@ int check_label(char *input); > int get_mountpt(char *dev, char *mntpt, size_t size); > > int btrfs_scan_block_devices(int run_ioctl); > +int open_file_or_dir(const char *fname); > #endif >-- To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
Stefan Behrens
2012-Oct-31 11:37 UTC
Re: [PATCH v5 1/3] Btrfs-progs: move open_file_or_dir() to utils.c
On Wed, 31 Oct 2012 18:34:05 +0800, Anand Jain wrote:> This is useful. Do you have something which can apply on the latest.No. But your message is a good reminder to me to rebase and resend the btrfs-progs patchset for the device stats support. It''s already 5 month old and was not integrated at that time. Now it''s outdated and I need to rework and resend it. The patch you mention is a part of the device stats patchset. If you need this "move open_file_or_dir() to utils.c" thing quickly (in the next two weeks), please send such a patch yourself (but not like I did it, Hugo was absolutely right to get upset when I was moving and changing a function in one go <http://www.spinics.net/lists/linux-btrfs/msg17067.html>). Stefan> > On 25/05/12 22:07, Stefan Behrens wrote: >> This is a preparation step to add support for device stats. The >> definition >> of the function open_file_or_dir() is moved from common.c to utils.c in >> order to be able to share some common code between scrub and the device >> stats in the following step. That common code uses open_file_or_dir(). >> Since open_file_or_dir() makes use of the function dirfd(3), the required >> XOPEN version was raised from 6 to 7. >> >> Signed-off-by: Stefan Behrens <sbehrens@giantdisaster.de> >> --- >> Makefile | 4 ++-- >> btrfsctl.c | 28 ---------------------------- >> cmds-balance.c | 1 + >> cmds-inspect.c | 1 + >> cmds-subvolume.c | 1 + >> commands.h | 3 --- >> common.c | 46 ---------------------------------------------- >> utils.c | 31 +++++++++++++++++++++++++++++-- >> utils.h | 3 +++ >> 9 files changed, 37 insertions(+), 81 deletions(-)-- To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
Goffredo Baroncelli
2012-Oct-31 12:37 UTC
Re: [PATCH v5 1/3] Btrfs-progs: move open_file_or_dir() to utils.c
On Wed, Oct 31, 2012 at 11:34 AM, Anand Jain <Anand.Jain@oracle.com> wrote:> > Stefan, > > This is useful. Do you have something which can apply on the latest.The patch below has one problem. The functions open_file_or_dir() in btrfsctl.c and in common.c are differents. The former has few perror() which are missing in the latter. I am working in a change which moves both the open_file_or_dir() and the scrub_fs_info() / scrub_device_info() pairs in utils.c. It was necessary because I used both in a my application. I hope to issue the patch for the end of the next week. In order to address the issue of the perror, I renamed in btrfsctl.c the function open_file_or_dir() in btrfsctl_open_file_or_dir() in order to avoid name collision. G.Baroncelli -- gpg @keyserver.linux.it: Goffredo Baroncelli (kreijackATinwind.it> Key fingerprint BBF5 1610 0B64 DAC6 5F7D 17B2 0EDA 9B37 8B82 E0B5> > Thanks, Anand > > On 25/05/12 22:07, Stefan Behrens wrote: >> >> This is a preparation step to add support for device stats. The definition >> of the function open_file_or_dir() is moved from common.c to utils.c in >> order to be able to share some common code between scrub and the device >> stats in the following step. That common code uses open_file_or_dir(). >> Since open_file_or_dir() makes use of the function dirfd(3), the required >> XOPEN version was raised from 6 to 7. >> >> Signed-off-by: Stefan Behrens <sbehrens@giantdisaster.de> >> --- >> Makefile | 4 ++-- >> btrfsctl.c | 28 ---------------------------- >> cmds-balance.c | 1 + >> cmds-inspect.c | 1 + >> cmds-subvolume.c | 1 + >> commands.h | 3 --- >> common.c | 46 ---------------------------------------------- >> utils.c | 31 +++++++++++++++++++++++++++++-- >> utils.h | 3 +++ >> 9 files changed, 37 insertions(+), 81 deletions(-) >> >> diff --git a/Makefile b/Makefile >> index 79818e6..fe2b432 100644 >> --- a/Makefile >> +++ b/Makefile >> @@ -39,8 +39,8 @@ all: version $(progs) manpages >> version: >> bash version.sh >> >> -btrfs: $(objects) btrfs.o help.o common.o $(cmds_objects) >> - $(CC) $(CFLAGS) -o btrfs btrfs.o help.o common.o $(cmds_objects) \ >> +btrfs: $(objects) btrfs.o help.o $(cmds_objects) >> + $(CC) $(CFLAGS) -o btrfs btrfs.o help.o $(cmds_objects) \ >> $(objects) $(LDFLAGS) $(LIBS) -lpthread >> >> calc-size: $(objects) calc-size.o >> diff --git a/btrfsctl.c b/btrfsctl.c >> index d45e2a7..f0584f3 100644 >> --- a/btrfsctl.c >> +++ b/btrfsctl.c >> @@ -63,34 +63,6 @@ static void print_usage(void) >> exit(1); >> } >> >> -static int open_file_or_dir(const char *fname) >> -{ >> - int ret; >> - struct stat st; >> - DIR *dirstream; >> - int fd; >> - >> - ret = stat(fname, &st); >> - if (ret < 0) { >> - perror("stat:"); >> - exit(1); >> - } >> - if (S_ISDIR(st.st_mode)) { >> - dirstream = opendir(fname); >> - if (!dirstream) { >> - perror("opendir"); >> - exit(1); >> - } >> - fd = dirfd(dirstream); >> - } else { >> - fd = open(fname, O_RDWR); >> - } >> - if (fd < 0) { >> - perror("open"); >> - exit(1); >> - } >> - return fd; >> -} >> int main(int ac, char **av) >> { >> char *fname = NULL; >> diff --git a/cmds-balance.c b/cmds-balance.c >> index 38a7426..5793b5c 100644 >> --- a/cmds-balance.c >> +++ b/cmds-balance.c >> @@ -26,6 +26,7 @@ >> #include "ctree.h" >> #include "ioctl.h" >> #include "volumes.h" >> +#include "utils.h" >> >> #include "commands.h" >> >> diff --git a/cmds-inspect.c b/cmds-inspect.c >> index 2f0228f..7a8785b 100644 >> --- a/cmds-inspect.c >> +++ b/cmds-inspect.c >> @@ -22,6 +22,7 @@ >> >> #include "kerncompat.h" >> #include "ioctl.h" >> +#include "utils.h" >> >> #include "commands.h" >> >> diff --git a/cmds-subvolume.c b/cmds-subvolume.c >> index 950fa8f..8ecd3f4 100644 >> --- a/cmds-subvolume.c >> +++ b/cmds-subvolume.c >> @@ -26,6 +26,7 @@ >> >> #include "kerncompat.h" >> #include "ioctl.h" >> +#include "utils.h" >> >> #include "commands.h" >> >> diff --git a/commands.h b/commands.h >> index a303a50..aea4cb1 100644 >> --- a/commands.h >> +++ b/commands.h >> @@ -79,9 +79,6 @@ void help_ambiguous_token(const char *arg, const struct >> cmd_group *grp); >> >> void help_command_group(const struct cmd_group *grp, int argc, char >> **argv); >> >> -/* common.c */ >> -int open_file_or_dir(const char *fname); >> - >> extern const struct cmd_group subvolume_cmd_group; >> extern const struct cmd_group filesystem_cmd_group; >> extern const struct cmd_group balance_cmd_group; >> diff --git a/common.c b/common.c >> deleted file mode 100644 >> index 03f6570..0000000 >> --- a/common.c >> +++ /dev/null >> @@ -1,46 +0,0 @@ >> -/* >> - * This program is free software; you can redistribute it and/or >> - * modify it under the terms of the GNU General Public >> - * License v2 as published by the Free Software Foundation. >> - * >> - * This program is distributed in the hope that it will be useful, >> - * but WITHOUT ANY WARRANTY; without even the implied warranty of >> - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU >> - * General Public License for more details. >> - * >> - * You should have received a copy of the GNU General Public >> - * License along with this program; if not, write to the >> - * Free Software Foundation, Inc., 59 Temple Place - Suite 330, >> - * Boston, MA 021110-1307, USA. >> - */ >> - >> -#include <sys/types.h> >> -#include <sys/stat.h> >> -#include <dirent.h> >> -#include <fcntl.h> >> - >> -int open_file_or_dir(const char *fname) >> -{ >> - int ret; >> - struct stat st; >> - DIR *dirstream; >> - int fd; >> - >> - ret = stat(fname, &st); >> - if (ret < 0) { >> - return -1; >> - } >> - if (S_ISDIR(st.st_mode)) { >> - dirstream = opendir(fname); >> - if (!dirstream) { >> - return -2; >> - } >> - fd = dirfd(dirstream); >> - } else { >> - fd = open(fname, O_RDWR); >> - } >> - if (fd < 0) { >> - return -3; >> - } >> - return fd; >> -} >> diff --git a/utils.c b/utils.c >> index ee7fa1b..6157115 100644 >> --- a/utils.c >> +++ b/utils.c >> @@ -16,8 +16,9 @@ >> * Boston, MA 021110-1307, USA. >> */ >> >> -#define _XOPEN_SOURCE 600 >> -#define __USE_XOPEN2K >> +#define _XOPEN_SOURCE 700 >> +#define __USE_XOPEN2K8 >> +#define __XOPEN2K8 /* due to an error in dirent.h, to get dirfd() */ >> #include <stdio.h> >> #include <stdlib.h> >> #ifndef __CHECKER__ >> @@ -1206,3 +1207,29 @@ scan_again: >> return 0; >> } >> >> +int open_file_or_dir(const char *fname) >> +{ >> + int ret; >> + struct stat st; >> + DIR *dirstream; >> + int fd; >> + >> + ret = stat(fname, &st); >> + if (ret < 0) { >> + return -1; >> + } >> + if (S_ISDIR(st.st_mode)) { >> + dirstream = opendir(fname); >> + if (!dirstream) { >> + return -2; >> + } >> + fd = dirfd(dirstream); >> + } else { >> + fd = open(fname, O_RDWR); >> + } >> + if (fd < 0) { >> + return -3; >> + } >> + return fd; >> +} >> + >> diff --git a/utils.h b/utils.h >> index c5f55e1..e281002 100644 >> --- a/utils.h >> +++ b/utils.h >> @@ -19,6 +19,8 @@ >> #ifndef __UTILS__ >> #define __UTILS__ >> >> +#include "ctree.h" >> + >> #define BTRFS_MKFS_SYSTEM_GROUP_SIZE (4 * 1024 * 1024) >> >> int make_btrfs(int fd, const char *device, const char *label, >> @@ -46,4 +48,5 @@ int check_label(char *input); >> int get_mountpt(char *dev, char *mntpt, size_t size); >> >> int btrfs_scan_block_devices(int run_ioctl); >> +int open_file_or_dir(const char *fname); >> #endif >> > -- > To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in > the body of a message to majordomo@vger.kernel.org > More majordomo info at http://vger.kernel.org/majordomo-info.html-- To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html