Goffredo Baroncelli
2012-Jun-08 19:12 UTC
[RFC][btrfs-progs] add command btrfs filesystem info
Hi all the aim of this patch is to add the command "btrfs filesystem info" to show some filesystem information. Example: $ sudo btrfs-progs/btrfs filesystem info /mnt/test Path: /mnt/test Max ID: 4 UUID: 1c7e4ba6-aebc-4b39-90ef-c61315fb74d1 Num devices: 3 Dev ID: 2 UUID: cdbda186-735e-49cd-babc-e84fffdb4105 Bytes used: 511705088 Total bytes: 1667235840 Path: /dev/loop1 Dev ID: 3 UUID: a4cebf4e-f760-49a7-b960-142b2c14d9e6 Bytes used: 0 Total bytes: 1111490560 Path: /dev/loop2 Dev ID: 4 UUID: 61bd5825-f86e-4708-8a5b-f48764b5c3a5 Bytes used: 0 Total bytes: 1111490560 Path: /dev/loop3 I make that because it is not so trivial to get these information otherwise. To get the information, I made public and used: scrub_device_info() and scrub_fs_info(). So, I moved the functions to the file utils.c changing scrub_ to get_. I was inspired by a patch of Stefan Behrens. Next enhancements: - remove the root privileges to launch the command (require change in kernel space) - show also the label of the filesystem (same as above) Please review; as usual, comments are welcome. You can pull the changes from the reposiotry http://cassiopea.homelinux.net/git/btrfs-progs-unstable.git branch filesystem-info BR G.Baroncelli Makefile | 8 +++--- cmds-filesystem.c | 59 ++++++++++++++++++++++++++++++++++++++++++++ cmds-scrub.c | 72 +----------------------------------------------------- man/btrfs.8.in | 8 ++++++ utils.c | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++ utils.h | 4 +++ 6 files changed, 145 insertions(+), 74 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-Jun-08 19:12 UTC
[PATCH 1/3] Btrfs-progs: make two utility functions globally available
This patch is ispired by a Stefan Behrens one. --- Makefile | 8 +++---- cmds-scrub.c | 72 ++-------------------------------------------------------- utils.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ utils.h | 4 ++++ 4 files changed, 78 insertions(+), 74 deletions(-) diff --git a/Makefile b/Makefile index 79818e6..0915021 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ CFLAGS = -g -O0 objects = ctree.o disk-io.o radix-tree.o extent-tree.o print-tree.o \ root-tree.o dir-item.o file-item.o inode-item.o \ inode-map.o crc32c.o rbtree.o extent-cache.o extent_io.o \ - volumes.o utils.o btrfs-list.o btrfslabel.o repair.o + volumes.o utils.o btrfs-list.o btrfslabel.o repair.o common.o cmds_objects = cmds-subvolume.o cmds-filesystem.o cmds-device.o cmds-scrub.o \ cmds-inspect.o cmds-balance.o @@ -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 @@ -52,7 +52,7 @@ btrfs-find-root: $(objects) find-root.o btrfs-restore: $(objects) restore.o gcc $(CFLAGS) -o btrfs-restore restore.o $(objects) $(LDFLAGS) $(LIBS) $(RESTORE_LIBS) -btrfsctl: $(objects) btrfsctl.o +btrfsctl: $(objects) btrfsctl.o $(CC) $(CFLAGS) -o btrfsctl btrfsctl.o $(objects) $(LDFLAGS) $(LIBS) btrfs-vol: $(objects) btrfs-vol.o 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 ee7fa1b..d916912 100644 --- a/utils.c +++ b/utils.c @@ -44,6 +44,7 @@ #include "utils.h" #include "volumes.h" #include "ioctl.h" +#include "commands.h" #ifdef __CHECKER__ #define BLKGETSIZE64 0 @@ -1206,3 +1207,70 @@ scan_again: return 0; } +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; +} + +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) { + /* 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; +} diff --git a/utils.h b/utils.h index c5f55e1..01220a2 100644 --- a/utils.h +++ b/utils.h @@ -46,4 +46,8 @@ int check_label(char *input); int get_mountpt(char *dev, char *mntpt, size_t size); int btrfs_scan_block_devices(int run_ioctl); +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 -- 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
Add btrfs filesystem info command, which is capable to show some btrfs filesystem information. --- cmds-filesystem.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/cmds-filesystem.c b/cmds-filesystem.c index 1f53d1c..9acc715 100644 --- a/cmds-filesystem.c +++ b/cmds-filesystem.c @@ -519,6 +519,64 @@ static int cmd_label(int argc, char **argv) return get_label(argv[1]); } +static const char * const cmd_info_usage[] = { + "btrfs filesystem info <path>", + "Get filesystem information", + NULL +}; + +static int cmd_info(int argc, char **argv){ + + struct btrfs_ioctl_fs_info_args fi_args; + struct btrfs_ioctl_dev_info_args *di_ret=NULL; + int ret,i; + char uuidbuf[37]; + int fd; + + if( argc != 2){ + fprintf(stderr, "ERROR: missing argument\n"); + return 102; + } + + fd = open_file_or_dir(argv[1]); + if( fd < 0 ){ + fprintf(stderr, "ERROR: cannot open ''%s''\n", argv[1]); + return 103; + } + + ret = get_fs_info(fd, argv[1], &fi_args, &di_ret); + if( ret ){ + int e = errno; + fprintf(stderr, "ERROR: cannot get information about ''%s''\n", + argv[1]); + fprintf(stderr, "ERROR: errno=%d, error=%s\n", + e, strerror(e) ); + close(fd); + if(di_ret != NULL ) + free(di_ret); + return 104; + } + + printf("Path: %s\n",argv[1]); + printf("Max ID: %llu\n", fi_args.max_id); + uuid_unparse(fi_args.fsid, uuidbuf); + printf("UUID: %s\n", uuidbuf); + + printf("\nNum devices: %llu\n", fi_args.num_devices); + for( i = 0 ; i < fi_args.num_devices ; i++ ){ + printf(" Dev ID: %llu\n", di_ret[i].devid); + uuid_unparse(di_ret[i].uuid, uuidbuf); + printf(" UUID: %s\n", uuidbuf); + printf(" Bytes used: %llu\n", di_ret[i].bytes_used ); + printf(" Total bytes: %llu\n", di_ret[i].total_bytes ); + printf(" Path: %s\n", di_ret[i].path); + } + + close(fd); + free(di_ret); + return 0; +} + const struct cmd_group filesystem_cmd_group = { filesystem_cmd_group_usage, NULL, { { "df", cmd_df, cmd_df_usage, NULL, 0 }, @@ -528,6 +586,7 @@ const struct cmd_group filesystem_cmd_group = { { "balance", cmd_balance, NULL, &balance_cmd_group, 1 }, { "resize", cmd_resize, cmd_resize_usage, NULL, 0 }, { "label", cmd_label, cmd_label_usage, NULL, 0 }, + { "info", cmd_info, cmd_info_usage, NULL, 0 }, { 0, 0, 0, 0, 0 }, } }; -- 1.7.10 -- 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
Update the man page to document the "btrfs filesystem info <path>" command. --- man/btrfs.8.in | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/man/btrfs.8.in b/man/btrfs.8.in index be478e0..6d96bf7 100644 --- a/man/btrfs.8.in +++ b/man/btrfs.8.in @@ -25,6 +25,8 @@ btrfs \- control a btrfs filesystem .PP \fBbtrfs\fP \fBfilesystem defrag\fP\fI [options] <file>|<dir> [<file>|<dir>...]\fP .PP +\fBbtrfs\fP \fBfilesystem info\fP\fI <path>\fP +.PP \fBbtrfs\fP \fBsubvolume find-new\fP\fI <subvolume> <last_gen>\fP .PP \fBbtrfs\fP \fBfilesystem balance\fP\fI <path> \fP @@ -154,6 +156,12 @@ The start position and the number of bytes to deframention can be specified by \ NOTE: defragmenting with kernels up to 2.6.37 will unlink COW-ed copies of data, don''t use it if you use snapshots, have de-duplicated your data or made copies with \fBcp --reflink\fP. +.TP + +\fBfilesystem info\fP \fI<path>\fR +Get filesystem info. +.TP + \fBsubvolume find-new\fR\fI <subvolume> <last_gen>\fR List the recently modified files in a subvolume, after \fI<last_gen>\fR ID. .TP -- 1.7.10 -- 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
What kind of "info" does it give? This is really unhelpful in a man page, it just tells the obvious. On Fri, Jun 8, 2012 at 3:12 PM, Goffredo Baroncelli <kreijack@inwind.it> wrote:> > Update the man page to document the "btrfs filesystem info <path>" > command. > --- > man/btrfs.8.in | 8 ++++++++ > 1 file changed, 8 insertions(+) > > diff --git a/man/btrfs.8.in b/man/btrfs.8.in > index be478e0..6d96bf7 100644 > --- a/man/btrfs.8.in > +++ b/man/btrfs.8.in > @@ -25,6 +25,8 @@ btrfs \- control a btrfs filesystem > .PP > \fBbtrfs\fP \fBfilesystem defrag\fP\fI [options] <file>|<dir> [<file>|<dir>...]\fP > .PP > +\fBbtrfs\fP \fBfilesystem info\fP\fI <path>\fP > +.PP > \fBbtrfs\fP \fBsubvolume find-new\fP\fI <subvolume> <last_gen>\fP > .PP > \fBbtrfs\fP \fBfilesystem balance\fP\fI <path> \fP > @@ -154,6 +156,12 @@ The start position and the number of bytes to deframention can be specified by \ > NOTE: defragmenting with kernels up to 2.6.37 will unlink COW-ed copies of data, don''t > use it if you use snapshots, have de-duplicated your data or made copies with > \fBcp --reflink\fP. > +.TP > + > +\fBfilesystem info\fP \fI<path>\fR > +Get filesystem info. > +.TP > + > \fBsubvolume find-new\fR\fI <subvolume> <last_gen>\fR > List the recently modified files in a subvolume, after \fI<last_gen>\fR ID. > .TP > -- > 1.7.10 > > -- > 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
Goffredo Baroncelli
2012-Jun-09 05:36 UTC
Re: [PATCH 3/3] Add btrfs filesystem info man page.
On 06/08/2012 09:21 PM, Jérôme Poulin wrote:> What kind of "info" does it give? This is really unhelpful in a man > page, it just tells the obvious.Definitely, this is one of the point of this patch to improve.> > On Fri, Jun 8, 2012 at 3:12 PM, Goffredo Baroncelli <kreijack@inwind.it> wrote: >> >> Update the man page to document the "btrfs filesystem info <path>" >> command. >> --- >> man/btrfs.8.in | 8 ++++++++ >> 1 file changed, 8 insertions(+) >> >> diff --git a/man/btrfs.8.in b/man/btrfs.8.in >> index be478e0..6d96bf7 100644 >> --- a/man/btrfs.8.in >> +++ b/man/btrfs.8.in >> @@ -25,6 +25,8 @@ btrfs \- control a btrfs filesystem >> .PP >> \fBbtrfs\fP \fBfilesystem defrag\fP\fI [options] <file>|<dir> [<file>|<dir>...]\fP >> .PP >> +\fBbtrfs\fP \fBfilesystem info\fP\fI <path>\fP >> +.PP >> \fBbtrfs\fP \fBsubvolume find-new\fP\fI <subvolume> <last_gen>\fP >> .PP >> \fBbtrfs\fP \fBfilesystem balance\fP\fI <path> \fP >> @@ -154,6 +156,12 @@ The start position and the number of bytes to deframention can be specified by \ >> NOTE: defragmenting with kernels up to 2.6.37 will unlink COW-ed copies of data, don''t >> use it if you use snapshots, have de-duplicated your data or made copies with >> \fBcp --reflink\fP. >> +.TP >> + >> +\fBfilesystem info\fP \fI<path>\fR >> +Get filesystem info. >> +.TP >> + >> \fBsubvolume find-new\fR\fI <subvolume> <last_gen>\fR >> List the recently modified files in a subvolume, after \fI<last_gen>\fR ID. >> .TP >> -- >> 1.7.10 >> >> -- >> 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 > . >-- 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