linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 0/2] btrfs-progs: Introduce open_btrfs_dir wrapper
@ 2015-08-26  9:04 Zhao Lei
  2015-08-26  9:04 ` [PATCH v2 1/2] " Zhao Lei
  2015-08-26  9:04 ` [PATCH v2 2/2] btrfs-progs: use open_btrfs_dir for btrfs device command Zhao Lei
  0 siblings, 2 replies; 5+ messages in thread
From: Zhao Lei @ 2015-08-26  9:04 UTC (permalink / raw)
  To: linux-btrfs; +Cc: Zhao Lei

This patch introduce open_btrfs_dir() to open a dir in btrfs
filesystem.

It can be used for several tools in btrfs-progs.

Changelog v1-v2:
1: Add error message for open_file failed case

Zhao Lei (2):
  btrfs-progs: Introduce open_btrfs_dir wrapper
  btrfs-progs: use open_btrfs_dir for btrfs device command

 cmds-device.c | 16 +++++-----------
 utils.c       | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 utils.h       |  1 +
 3 files changed, 62 insertions(+), 11 deletions(-)

-- 
1.8.5.1


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v2 1/2] btrfs-progs: Introduce open_btrfs_dir wrapper
  2015-08-26  9:04 [PATCH v2 0/2] btrfs-progs: Introduce open_btrfs_dir wrapper Zhao Lei
@ 2015-08-26  9:04 ` Zhao Lei
  2015-08-26 15:56   ` David Sterba
  2015-08-26  9:04 ` [PATCH v2 2/2] btrfs-progs: use open_btrfs_dir for btrfs device command Zhao Lei
  1 sibling, 1 reply; 5+ messages in thread
From: Zhao Lei @ 2015-08-26  9:04 UTC (permalink / raw)
  To: linux-btrfs; +Cc: Zhao Lei

This patch introduce open_btrfs_dir() to open a dir in btrfs
filesystem.

It can be used for several tools in btrfs-progs.

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
---
 utils.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 utils.h |  1 +
 2 files changed, 57 insertions(+)

diff --git a/utils.c b/utils.c
index 1dfcc2d..eb9a605 100644
--- a/utils.c
+++ b/utils.c
@@ -36,6 +36,8 @@
 #include <limits.h>
 #include <blkid/blkid.h>
 #include <sys/vfs.h>
+#include <sys/statfs.h>
+#include <linux/magic.h>
 
 #include "kerncompat.h"
 #include "radix-tree.h"
@@ -1081,6 +1083,60 @@ int open_path_or_dev_mnt(const char *path, DIR **dirstream)
 	return fdmnt;
 }
 
+/*
+ * Do following check before open_file_or_dir():
+ * 1: path is in a btrfs filesystem
+ * 2: path is a dir
+ */
+int open_btrfs_dir(const char *path, DIR **dirstream, int output)
+{
+	struct statfs stfs;
+	struct stat st;
+	int ret;
+
+	if (statfs(path, &stfs) != 0) {
+		if (output)
+			fprintf(stderr,
+				"ERROR: can't access '%s', %s\n",
+				path, strerror(errno));
+		return -1;
+	}
+
+	if (stfs.f_type != BTRFS_SUPER_MAGIC) {
+		if (output)
+			fprintf(stderr,
+				"ERROR: not btrfs filesystem: %s\n",
+				path);
+		return -2;
+	}
+
+	if (stat(path, &st) != 0) {
+		if (output)
+			fprintf(stderr,
+				"ERROR: can't access '%s', %s\n",
+				path, strerror(errno));
+		return -1;
+	}
+
+	if (!S_ISDIR(st.st_mode)) {
+		if (output)
+			fprintf(stderr,
+				"ERROR: not directory: %s\n",
+				path);
+		return -3;
+	}
+
+	ret = open_file_or_dir(path, dirstream);
+	if (ret < 0) {
+		if (output)
+			fprintf(stderr,
+				"ERROR: can't access '%s', %s\n",
+				path, strerror(errno));
+	}
+
+	return ret;
+}
+
 /* checks if a device is a loop device */
 static int is_loop_device (const char* device) {
 	struct stat statbuf;
diff --git a/utils.h b/utils.h
index 8ec23c9..87c0d08 100644
--- a/utils.h
+++ b/utils.h
@@ -158,6 +158,7 @@ int is_block_device(const char *file);
 int is_mount_point(const char *file);
 int check_arg_type(const char *input);
 int open_path_or_dev_mnt(const char *path, DIR **dirstream);
+int open_btrfs_dir(const char *path, DIR **dirstream, int output);
 u64 btrfs_device_size(int fd, struct stat *st);
 /* Helper to always get proper size of the destination string */
 #define strncpy_null(dest, src) __strncpy__null(dest, src, sizeof(dest))
-- 
1.8.5.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH v2 2/2] btrfs-progs: use open_btrfs_dir for btrfs device command
  2015-08-26  9:04 [PATCH v2 0/2] btrfs-progs: Introduce open_btrfs_dir wrapper Zhao Lei
  2015-08-26  9:04 ` [PATCH v2 1/2] " Zhao Lei
@ 2015-08-26  9:04 ` Zhao Lei
  2015-08-26 15:58   ` David Sterba
  1 sibling, 1 reply; 5+ messages in thread
From: Zhao Lei @ 2015-08-26  9:04 UTC (permalink / raw)
  To: linux-btrfs; +Cc: Zhao Lei

We can use open_btrfs_dir() to check whether target dir is
in btrfs's mount point before open, instead of checking it in kernel
space of ioctl, and return fuzzy error message.

Before patch:
  # (/mnt/tmp is not btrfs mountpoint)
  #
  # btrfs device add -f /dev/sda13 /mnt/tmp
  ERROR: error adding the device '/dev/sda13' - Inappropriate ioctl for device
  #

After patch:
  # btrfs device add -f /dev/sda13 /mnt/tmp
  ERROR: not btrfs filesystem: /mnt/tmp
  #

Similar fix for device remove and device usage.

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
---
 cmds-device.c | 16 +++++-----------
 1 file changed, 5 insertions(+), 11 deletions(-)

diff --git a/cmds-device.c b/cmds-device.c
index 00e362a..2dba050 100644
--- a/cmds-device.c
+++ b/cmds-device.c
@@ -84,11 +84,9 @@ static int cmd_device_add(int argc, char **argv)
 
 	mntpnt = argv[optind + argc - 1];
 
-	fdmnt = open_file_or_dir(mntpnt, &dirstream);
-	if (fdmnt < 0) {
-		fprintf(stderr, "ERROR: can't access '%s'\n", mntpnt);
+	fdmnt = open_btrfs_dir(mntpnt, &dirstream, 1);
+	if (fdmnt < 0)
 		return 1;
-	}
 
 	for (i = optind; i < optind + argc - 1; i++){
 		struct btrfs_ioctl_vol_args ioctl_args;
@@ -157,11 +155,9 @@ static int _cmd_device_remove(int argc, char **argv,
 
 	mntpnt = argv[argc - 1];
 
-	fdmnt = open_file_or_dir(mntpnt, &dirstream);
-	if (fdmnt < 0) {
-		fprintf(stderr, "ERROR: can't access '%s'\n", mntpnt);
+	fdmnt = open_btrfs_dir(mntpnt, &dirstream, 1);
+	if (fdmnt < 0)
 		return 1;
-	}
 
 	for(i=1 ; i < argc - 1; i++ ){
 		struct	btrfs_ioctl_vol_args arg;
@@ -586,10 +582,8 @@ int cmd_device_usage(int argc, char **argv)
 		if (more_than_one)
 			printf("\n");
 
-		fd = open_file_or_dir(argv[i], &dirstream);
+		fd = open_btrfs_dir(argv[i], &dirstream, 1);
 		if (fd < 0) {
-			fprintf(stderr, "ERROR: can't access '%s'\n",
-				argv[1]);
 			ret = 1;
 			goto out;
 		}
-- 
1.8.5.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH v2 1/2] btrfs-progs: Introduce open_btrfs_dir wrapper
  2015-08-26  9:04 ` [PATCH v2 1/2] " Zhao Lei
@ 2015-08-26 15:56   ` David Sterba
  0 siblings, 0 replies; 5+ messages in thread
From: David Sterba @ 2015-08-26 15:56 UTC (permalink / raw)
  To: Zhao Lei; +Cc: linux-btrfs

On Wed, Aug 26, 2015 at 05:04:22PM +0800, Zhao Lei wrote:
> This patch introduce open_btrfs_dir() to open a dir in btrfs
> filesystem.
> 
> It can be used for several tools in btrfs-progs.
> 
> Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>

I've renamed it to btrfs_open_dir and tweaked some error messages and
applied, thanks.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2 2/2] btrfs-progs: use open_btrfs_dir for btrfs device command
  2015-08-26  9:04 ` [PATCH v2 2/2] btrfs-progs: use open_btrfs_dir for btrfs device command Zhao Lei
@ 2015-08-26 15:58   ` David Sterba
  0 siblings, 0 replies; 5+ messages in thread
From: David Sterba @ 2015-08-26 15:58 UTC (permalink / raw)
  To: Zhao Lei; +Cc: linux-btrfs

On Wed, Aug 26, 2015 at 05:04:23PM +0800, Zhao Lei wrote:
> We can use open_btrfs_dir() to check whether target dir is
> in btrfs's mount point before open, instead of checking it in kernel
> space of ioctl, and return fuzzy error message.
> 
> Before patch:
>   # (/mnt/tmp is not btrfs mountpoint)
>   #
>   # btrfs device add -f /dev/sda13 /mnt/tmp
>   ERROR: error adding the device '/dev/sda13' - Inappropriate ioctl for device
>   #
> 
> After patch:
>   # btrfs device add -f /dev/sda13 /mnt/tmp
>   ERROR: not btrfs filesystem: /mnt/tmp
>   #
> 
> Similar fix for device remove and device usage.
> 
> Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>

Applied, thanks.

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2015-08-26 15:59 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-08-26  9:04 [PATCH v2 0/2] btrfs-progs: Introduce open_btrfs_dir wrapper Zhao Lei
2015-08-26  9:04 ` [PATCH v2 1/2] " Zhao Lei
2015-08-26 15:56   ` David Sterba
2015-08-26  9:04 ` [PATCH v2 2/2] btrfs-progs: use open_btrfs_dir for btrfs device command Zhao Lei
2015-08-26 15:58   ` David Sterba

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).