Skip to contentSkip to conversation

Btrfs-progs: add btrfs filesystem disk-info command

PATCH In review Josef Bacik 2 participants 4 messages Sep 28, 2010 – Sep 29, 2010 Btrfs file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Btrfs-progs: add btrfs filesystem disk-info command

This command just implements the BTRFS_IOC_DISK_INFO ioctl. It gets all the devices for a filesystem, reads /proc/partitions for the right dev info and spits out pretty names.

---
 btrfs.c      |    4 ++
 btrfs_cmds.c |  101 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 btrfs_cmds.h |    1 +
 ioctl.h      |    8 ++++
 4 files changed, 114 insertions(+), 0 deletions(-)
btrfs.c +4 -0
index ab5e57f..22b241b 100644
@@ -91,6 +91,10 @@ static struct Command commands[] = {
 	  "filesystem df", "<path>\n"
 		"Show space usage information for a mount point\n."
 	},
+	{ do_disk_info, 1,
+	  "filesystem disk-info", "<path>\n"
+		"Show disks contained within this filesysten."
+	},
 	{ do_balance, 1,
 	  "filesystem balance", "<path>\n"
 		"Balance the chunks across the device."
btrfs_cmds.c +101 -0

107 lines not on this page · raw message

btrfs_cmds.h +1 -0
index 7bde191..0b5038a 100644
@@ -31,4 +31,5 @@ int do_set_default_subvol(int nargs, char **argv);
 int list_subvols(int fd);
 int do_df_filesystem(int nargs, char **argv);
 int find_updated_files(int fd, u64 root_id, u64 oldest_gen);
+int do_disk_info(int nargs, char **argv);
 int do_find_newer(int argc, char **argv);
ioctl.h +8 -0
index 776d7a9..4ace64f 100644
@@ -19,6 +19,7 @@
 #ifndef __IOCTL_
 #define __IOCTL_
 #include <asm/types.h>
+#include <sys/types.h>
 #include <linux/ioctl.h>
 
 #define BTRFS_IOCTL_MAGIC 0x94
@@ -132,6 +133,11 @@ struct btrfs_ioctl_space_args {
 	struct btrfs_ioctl_space_info spaces[0];
 };
 
+struct btrfs_ioctl_disk_info_args {
+	__u32 num_devices;
+	__u64 devices[0];
+};
+
 #define BTRFS_IOC_SNAP_CREATE _IOW(BTRFS_IOCTL_MAGIC, 1, \
 				   struct btrfs_ioctl_vol_args)
 #define BTRFS_IOC_DEFRAG _IOW(BTRFS_IOCTL_MAGIC, 2, \
@@ -169,4 +175,6 @@ struct btrfs_ioctl_space_args {
 #define BTRFS_IOC_DEFAULT_SUBVOL _IOW(BTRFS_IOCTL_MAGIC, 19, u64)
 #define BTRFS_IOC_SPACE_INFO _IOWR(BTRFS_IOCTL_MAGIC, 20, \
 				    struct btrfs_ioctl_space_args)
+#define BTRFS_IOC_DISK_INFO _IOWR(BTRFS_IOCTL_MAGIC, 21, \
+				  struct btrfs_ioctl_disk_info_args)
 #endif
Signature · 1 line
1.6.6.1
#1 lore raw

Hi Josef,

please, if you add or change a command pay attention to update the man page too (man/btrfs.8.in).

See my comments below

Quoted 8 lines · Josef Bacik

This command just implements the BTRFS_IOC_DISK_INFO ioctl. It gets all the devices for a filesystem, reads /proc/partitions for the right dev info and spits out pretty names.

---
 btrfs.c      |    4 ++
 btrfs_cmds.c |  101 

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Quoted 41 lines
 btrfs_cmds.h |    1 +
 ioctl.h      |    8 ++++
 4 files changed, 114 insertions(+), 0 deletions(-)
btrfs.c +4 -0
index ab5e57f..22b241b 100644
@@ -91,6 +91,10 @@ static struct Command commands[] = {
 	  "filesystem df", "<path>\n"
 		"Show space usage information for a mount point\n."
 	},
+	{ do_disk_info, 1,
+	  "filesystem disk-info", "<path>\n"
+		"Show disks contained within this filesysten."
+	},
 	{ do_balance, 1,
 	  "filesystem balance", "<path>\n"
 		"Balance the chunks across the device."
btrfs_cmds.c +14 -0
index 683aec0..b1a4d8d 100644
@@ -835,6 +835,107 @@ int do_set_default_subvol(int nargs, char **argv)
 	return 0;
 }
 
+int do_disk_info(int nargs, char **argv)
+{
+	struct btrfs_ioctl_disk_info_args *args;
+	char *path = argv[1];
+	int devs;
+	int fd;
+	int ret;
+	int i;
+
+	fd = open_file_or_dir(path);
+	if (fd < 0) {
+		fprintf(stderr, "Error: can't access '%s'\n", path);
+		return 12;
+	}

btrfs_cmds.c int do_set_default_subvol(int nargs, char **argv) · line 861

Quoted 10 lines
btrfs_cmds.c +10 -0
+
+	args = malloc(sizeof(*args));
+	if (!args)
+		return -ENOMEM;
+
+	args->num_devices = 0;
+
+	ret = ioctl(fd, BTRFS_IOC_DISK_INFO, args);
+	if (ret) {
+		printf("failed ioctl\n");

Please use

		fprintf(stderr,"ERROR: failed ioctl\n");
Quoted 4 lines
btrfs_cmds.c +4 -0
+		free(args);
+		return ret;
+	}
+

btrfs_cmds.c int do_set_default_subvol(int nargs, char **argv) · line 875

Quoted 10 lines
btrfs_cmds.c +10 -0
+	if (!args->num_devices) {
+		printf("no devs\n");
+		free(args);
+		return 0;
+	}
+
+	devs = args->num_devices;
+	args = realloc(args, sizeof(*args) + (devs * sizeof(dev_t)));
+	if (!args) {
+		printf("enomem\n");

Please use

		fprintf(stderr,"ERROR: not enough memory\n");

btrfs_cmds.c int do_set_default_subvol(int nargs, char **argv) · line 882

Quoted 7 lines
btrfs_cmds.c +7 -0
+		return -ENOMEM;
+	}
+	args->num_devices = devs;
+
+	ret = ioctl(fd, BTRFS_IOC_DISK_INFO, args);
+	if (ret) {
+		printf("ioctl failed second time\n");

Please use

		fprintf(stderr,"ERROR: ioctl failed (second time)\n");

btrfs_cmds.c int do_set_default_subvol(int nargs, char **argv) · line 888

Quoted 6 lines
btrfs_cmds.c +6 -0
+		free(args);
+		return -ENOMEM;
+	}
+	close(fd);
+
+	fd = open_file_or_dir("/proc/partitions");

Why not

	f = fopen("/proc/partitions","r")

So you can avoid "f = fdopen(fd,"r")" and the "goto print"

btrfs_cmds.c int do_set_default_subvol(int nargs, char **argv) · line 895

Quoted 7 lines
btrfs_cmds.c +7 -0
+	if (fd < 0) {
+print:
+		printf("Devices in %s\n", path);
+		for (i = 0; i < args->num_devices; i++)
+			printf("\t%d:%d\n", major(args->devices[i]),
+			       minor(args->devices[i]));
+		close(fd);
		^^^^^^^^^

why close fd ? ... ok, in case of fdopen(fd,"r") == 0

btrfs_cmds.c int do_set_default_subvol(int nargs, char **argv) · line 906

Quoted 12 lines
btrfs_cmds.c +11 -0
+	} else {
+		int major, minor;
+		unsigned long long blocks;
+		char buf[128];
+		char name[32];
+		FILE *f;
+		int found_devices = 0;
+
 
+		f = fdopen(fd, "r");
+		if (!f)
+			goto print;

See my comments above

btrfs_cmds.c int do_set_default_subvol(int nargs, char **argv)

Quoted 11 lines
btrfs_cmds.c +11 -0
+
+		printf("Devices in %s\n", path);
+		while (!feof(f)) {
+			char *blah;
+
+			memset(name, 0, 32);
+			memset(buf, 0, 128);
+
+			blah = fgets(buf, 128, f);
+
+			ret = sscanf(buf, "%d %d %llu %32s", &major, &minor, 

&blocks, name);

Quoted 73 lines
btrfs_cmds.c +21 -0
+			if (!ret)
+				continue;
+
+			for (i = 0; i < args->num_devices; i++) {
+				if (major(args->devices[i]) != major)
+					continue;
+				if (minor(args->devices[i]) != minor)
+					continue;
+				found_devices++;
+				printf("\t/dev/%s\n", name);
+				break;
+			}
+			if (found_devices == args->num_devices)
+				break;
+		}
+		fclose(f);
+	}
+	free(args);
+	return 0;
+}
+
 int do_df_filesystem(int nargs, char **argv)
 {
btrfs_cmds.c +0 -0
 	struct btrfs_ioctl_space_args *sargs;
btrfs_cmds.h +1 -0
index 7bde191..0b5038a 100644
@@ -31,4 +31,5 @@ int do_set_default_subvol(int nargs, char **argv);
 int list_subvols(int fd);
 int do_df_filesystem(int nargs, char **argv);
 int find_updated_files(int fd, u64 root_id, u64 oldest_gen);
+int do_disk_info(int nargs, char **argv);
 int do_find_newer(int argc, char **argv);
ioctl.h +8 -0
index 776d7a9..4ace64f 100644
@@ -19,6 +19,7 @@
 #ifndef __IOCTL_
 #define __IOCTL_
 #include <asm/types.h>
+#include <sys/types.h>
 #include <linux/ioctl.h>
 
 #define BTRFS_IOCTL_MAGIC 0x94
@@ -132,6 +133,11 @@ struct btrfs_ioctl_space_args {
 	struct btrfs_ioctl_space_info spaces[0];
 };
 
+struct btrfs_ioctl_disk_info_args {
+	__u32 num_devices;
+	__u64 devices[0];
+};
+
 #define BTRFS_IOC_SNAP_CREATE _IOW(BTRFS_IOCTL_MAGIC, 1, \
 				   struct btrfs_ioctl_vol_args)
 #define BTRFS_IOC_DEFRAG _IOW(BTRFS_IOCTL_MAGIC, 2, \
@@ -169,4 +175,6 @@ struct btrfs_ioctl_space_args {
 #define BTRFS_IOC_DEFAULT_SUBVOL _IOW(BTRFS_IOCTL_MAGIC, 19, u64)
 #define BTRFS_IOC_SPACE_INFO _IOWR(BTRFS_IOCTL_MAGIC, 20, \
 				    struct btrfs_ioctl_space_args)
+#define BTRFS_IOC_DISK_INFO _IOWR(BTRFS_IOCTL_MAGIC, 21, \
+				  struct btrfs_ioctl_disk_info_args)
 #endif
Signature · 6 lines
1.6.6.1

--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Signature · 2 lines
gpg key@ keyserver.linux.it: Goffredo Baroncelli (ghigo) <[email protected]>
Key fingerprint = 4769 7E51 5293 D36C 814E  C054 BF04 F161 3DC5 0512
#2 lore raw
Quoted 5 lines · Goffredo Baroncelli

Hi Josef,

please, if you add or change a command pay attention to update the man page too (man/btrfs.8.in).

Hrm I missed that with git add, I'll fix that up.

Quoted 64 lines

See my comments below

Quoted 8 lines · Josef Bacik

This command just implements the BTRFS_IOC_DISK_INFO ioctl. It gets all the devices for a filesystem, reads /proc/partitions for the right dev info and spits out pretty names.

---
 btrfs.c      |    4 ++
 btrfs_cmds.c |  101 

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Quoted 41 lines
 btrfs_cmds.h |    1 +
 ioctl.h      |    8 ++++
 4 files changed, 114 insertions(+), 0 deletions(-)
btrfs.c +4 -0
index ab5e57f..22b241b 100644
@@ -91,6 +91,10 @@ static struct Command commands[] = {
 	  "filesystem df", "<path>\n"
 		"Show space usage information for a mount point\n."
 	},
+	{ do_disk_info, 1,
+	  "filesystem disk-info", "<path>\n"
+		"Show disks contained within this filesysten."
+	},
 	{ do_balance, 1,
 	  "filesystem balance", "<path>\n"
 		"Balance the chunks across the device."
btrfs_cmds.c +14 -0
index 683aec0..b1a4d8d 100644
@@ -835,6 +835,107 @@ int do_set_default_subvol(int nargs, char **argv)
 	return 0;
 }
 
+int do_disk_info(int nargs, char **argv)
+{
+	struct btrfs_ioctl_disk_info_args *args;
+	char *path = argv[1];
+	int devs;
+	int fd;
+	int ret;
+	int i;
+
+	fd = open_file_or_dir(path);
+	if (fd < 0) {
+		fprintf(stderr, "Error: can't access '%s'\n", path);
+		return 12;
+	}

btrfs_cmds.c int do_set_default_subvol(int nargs, char **argv) · line 861

Quoted 10 lines
btrfs_cmds.c +10 -0
+
+	args = malloc(sizeof(*args));
+	if (!args)
+		return -ENOMEM;
+
+	args->num_devices = 0;
+
+	ret = ioctl(fd, BTRFS_IOC_DISK_INFO, args);
+	if (ret) {
+		printf("failed ioctl\n");

Please use

Quoted 2 lines
		fprintf(stderr,"ERROR: failed ioctl\n");

Ugh oops sorry, forgot to get rid of my debug printing, I'll kill that.

Quoted 15 lines
Quoted 4 lines
btrfs_cmds.c +4 -0
+		free(args);
+		return ret;
+	}
+

btrfs_cmds.c int do_set_default_subvol(int nargs, char **argv) · line 875

Quoted 10 lines
btrfs_cmds.c +10 -0
+	if (!args->num_devices) {
+		printf("no devs\n");
+		free(args);
+		return 0;
+	}
+
+	devs = args->num_devices;
+	args = realloc(args, sizeof(*args) + (devs * sizeof(dev_t)));
+	if (!args) {
+		printf("enomem\n");

Please use

Quoted 1 line
		fprintf(stderr,"ERROR: not enough memory\n");

Yeah another debug print.

Quoted 31 lines

btrfs_cmds.c int do_set_default_subvol(int nargs, char **argv) · line 882

Quoted 7 lines
btrfs_cmds.c +7 -0
+		return -ENOMEM;
+	}
+	args->num_devices = devs;
+
+	ret = ioctl(fd, BTRFS_IOC_DISK_INFO, args);
+	if (ret) {
+		printf("ioctl failed second time\n");

Please use

		fprintf(stderr,"ERROR: ioctl failed (second time)\n");

btrfs_cmds.c int do_set_default_subvol(int nargs, char **argv) · line 888

Quoted 6 lines
btrfs_cmds.c +6 -0
+		free(args);
+		return -ENOMEM;
+	}
+	close(fd);
+
+	fd = open_file_or_dir("/proc/partitions");

Why not

	f = fopen("/proc/partitions","r")

So you can avoid "f = fdopen(fd,"r")" and the "goto print"

btrfs_cmds.c int do_set_default_subvol(int nargs, char **argv) · line 895

Quoted 7 lines
btrfs_cmds.c +7 -0
+	if (fd < 0) {
+print:
+		printf("Devices in %s\n", path);
+		for (i = 0; i < args->num_devices; i++)
+			printf("\t%d:%d\n", major(args->devices[i]),
+			       minor(args->devices[i]));
+		close(fd);
		^^^^^^^^^
Quoted 2 lines

why close fd ? ... ok, in case of fdopen(fd,"r") == 0

Yeah I didn't think this through well, I'll fix it up. I mostly just sent this as an afterthought. When we figure out how the interface is going to look I'll fix everything up and re-send. Thanks,

Josef

#3 lore raw
Quoted 4 lines · Josef Bacik
Quoted 3 lines · Goffredo Baroncelli

Hi Josef,

please, if you add or change a command pay attention to update the man

page

Quoted 4 lines
Quoted 2 lines

too (man/btrfs.8.in).

Hrm I missed that with git add, I'll fix that up.

[...]

Quoted 9 lines
Quoted 7 lines

btrfs_cmds.c int do_set_default_subvol(int nargs, char **argv) · line 861

Quoted 4 lines
btrfs_cmds.c +4 -0
+
+	ret = ioctl(fd, BTRFS_IOC_DISK_INFO, args);
+	if (ret) {
+		printf("failed ioctl\n");

Please use

		fprintf(stderr,"ERROR: failed ioctl\n");

Ugh oops sorry, forgot to get rid of my debug printing, I'll kill that.

If you use this command on a non btrfs filesystem, this is the first point of failure. So please don't kill it.

Signature · 2 lines
gpg key@ keyserver.linux.it: Goffredo Baroncelli (ghigo) <[email protected]>
Key fingerprint = 4769 7E51 5293 D36C 814E  C054 BF04 F161 3DC5 0512