All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] xfs_io: implement 'inode' command V5
@ 2015-11-26 15:46 Carlos Maiolino
  2015-11-30 13:22 ` Brian Foster
  0 siblings, 1 reply; 4+ messages in thread
From: Carlos Maiolino @ 2015-11-26 15:46 UTC (permalink / raw)
  To: xfs

Implements a new xfs_io command, named 'inode', which is supposed to be
used to query information about inode's existence and its physical size
in the filesystem.

Supported options:

Default:     -- Return true(1) or false(0) if any inode greater than
                32bits has been found in the filesystem
-v           -- verbose mode
                Display the number and the physical size (in bits)
                of the largest inode in the filesystem
[num]        -- Return true(1) or false(0) if the inode [num] is in use
-n [num]     -- Return the next valid inode after [num]

No manpage sent because there were changes in the supported options and its
descriptions.
I'll send the manpage after the options and descriptions are reviewed.

- Changelog

V3:
	- Merge all 3 patches from the V2 together in a single patch
	- Rework of '-n [num]' and 'num' only arguments algorithm
	- Argument -n now relies on bulkreq.count to check for next inodes, not
	  on bstat.bs_ino anymore.
	- for loop in ret_lsize or ret_largest case, now relies on count being 0
	  to break the loop

V4:
	- Refactor inode_f function to reduce its size and easier logic
	- Implement error handlers for invalid command combination (hopefully
	  all invalid combinations).
	- use a single xfs_inogrp array for keep track of inodes
	- Fix missing newline in inode_help()
	- Rewrite help message in inode_help()
	- Fix indentation

V5:
	- Reduce the amount of options
	- remove igrp_rec variable, and use igroup[lastgrp] directly to get
	  information from the last inode groups returned by ioctl

Signed-off-by: Carlos Maiolino <cmaiolino@redhat.com>
---
 io/open.c | 143 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 143 insertions(+)

diff --git a/io/open.c b/io/open.c
index ac5a5e0..1e38ea8 100644
--- a/io/open.c
+++ b/io/open.c
@@ -20,6 +20,7 @@
 #include "input.h"
 #include "init.h"
 #include "io.h"
+#include "libxfs.h"
 
 #ifndef __O_TMPFILE
 #if defined __alpha__
@@ -44,6 +45,7 @@ static cmdinfo_t statfs_cmd;
 static cmdinfo_t chproj_cmd;
 static cmdinfo_t lsproj_cmd;
 static cmdinfo_t extsize_cmd;
+static cmdinfo_t inode_cmd;
 static prid_t prid;
 static long extsize;
 
@@ -750,6 +752,136 @@ statfs_f(
 	return 0;
 }
 
+static void
+inode_help(void)
+{
+	printf(_(
+"\n"
+"Query physical information about the inode"
+"\n"
+" Default:	-- Return true(1) or false(0) if any inode greater than\n"
+"		   32bits has been found in the filesystem\n"
+" -v		-- verbose mode\n"
+"		   Display the number and the physical size (in bits)\n"
+"		   of the largest inode in the filesystem\n"
+"[num]		-- Return true(1) or false(0) if the inode [num] is in use\n"
+" -n [num]	-- Return the next valid inode after [num]\n"
+"\n"));
+}
+
+static int
+inode_f(
+	  int			argc,
+	  char			**argv)
+{
+	__s32			count = 0;
+	__s32			lastgrp = 0;
+	__u64			last = 0;
+	__u64			lastino = 0;
+	__u64			userino = 0;
+	char			*p;
+	int			c;
+	int			verbose = 0;
+	int			ret_next = 0;
+	int			cmd = 0;
+	struct xfs_inogrp	igroup[1024];
+	struct xfs_fsop_bulkreq	bulkreq;
+	struct xfs_bstat	bstat;
+
+	while ((c = getopt(argc, argv, "nv")) != EOF) {
+		switch (c) {
+		case 'v':
+			verbose = 1;
+			break;
+		case 'n':
+			ret_next = 1;
+			break;
+		default:
+			return command_usage(&inode_cmd);
+		}
+	}
+
+	if (ret_next && verbose)
+		return command_usage(&inode_cmd);
+
+	if (optind < argc) {
+		if (verbose)
+			return command_usage(&inode_cmd);
+
+		if (ret_next) {
+			cmd = XFS_IOC_FSBULKSTAT;
+		} else {
+			if (argc > 2)
+				return command_usage(&inode_cmd);
+			else
+				cmd = XFS_IOC_FSBULKSTAT_SINGLE;
+		}
+
+		userino = strtoull(argv[optind], &p, 10);
+		if ((*p != '\0')) {
+			printf(_("[num] must be a numeric value\n"));
+			exitcode = 1;
+			return 0;
+		}
+
+		bulkreq.lastip = &userino;
+		bulkreq.icount = 1;
+		bulkreq.ubuffer = &bstat;
+		bulkreq.ocount = &count;
+
+		if (xfsctl(file->name, file->fd, cmd, &bulkreq)) {
+			if (errno == EINVAL) {
+				if (!ret_next)
+					printf("0\n");
+			} else {
+				perror("xfsctl");
+			}
+			exitcode = 1;
+			return 0;
+		}
+
+		if (ret_next) {
+			printf("%llu\n", bstat.bs_ino);
+			return 0;
+		} else {
+			/* Inode number used*/
+			printf("1\n");
+			return 0;
+		}
+	}
+
+	bulkreq.lastip = &last;
+	bulkreq.icount = 1024; /* User-defined maybe!? */
+	bulkreq.ubuffer = &igroup;
+	bulkreq.ocount = &count;
+
+	for (;;) {
+		if (xfsctl(file->name, file->fd, XFS_IOC_FSINUMBERS,
+				&bulkreq)) {
+			perror("XFS_IOC_FSINUMBERS");
+			exitcode = 1;
+			return 0;
+		}
+
+		if (count == 0)
+			break;
+
+		lastgrp = count;
+	}
+
+	lastgrp--;
+	lastino = igroup[lastgrp].xi_startino +
+		  xfs_highbit64(igroup[lastgrp].xi_allocmask);
+
+	if (verbose)
+		printf("%llu:%d\n", lastino,
+			lastino > XFS_MAXINUMBER_32 ? 64 : 32);
+	else
+		printf("%d\n", lastino > XFS_MAXINUMBER_32 ? 1 : 0);
+
+	return 0;
+}
+
 void
 open_init(void)
 {
@@ -815,6 +947,16 @@ open_init(void)
 		_("get/set preferred extent size (in bytes) for the open file");
 	extsize_cmd.help = extsize_help;
 
+	inode_cmd.name = "inode";
+	inode_cmd.cfunc = inode_f;
+	inode_cmd.args = _("[-n | -v] [num]");
+	inode_cmd.argmin = 0;
+	inode_cmd.argmax = 2;
+	inode_cmd.flags = CMD_NOMAP_OK;
+	inode_cmd.oneline =
+		_("Query inode number usage in the filesystem");
+	inode_cmd.help = inode_help;
+
 	add_command(&open_cmd);
 	add_command(&stat_cmd);
 	add_command(&close_cmd);
@@ -822,4 +964,5 @@ open_init(void)
 	add_command(&chproj_cmd);
 	add_command(&lsproj_cmd);
 	add_command(&extsize_cmd);
+	add_command(&inode_cmd);
 }
-- 
2.4.3

_______________________________________________
xfs mailing list
xfs@oss.sgi.com
http://oss.sgi.com/mailman/listinfo/xfs

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

* Re: [PATCH] xfs_io: implement 'inode' command V5
  2015-11-26 15:46 [PATCH] xfs_io: implement 'inode' command V5 Carlos Maiolino
@ 2015-11-30 13:22 ` Brian Foster
  2015-11-30 14:26   ` Carlos Maiolino
  0 siblings, 1 reply; 4+ messages in thread
From: Brian Foster @ 2015-11-30 13:22 UTC (permalink / raw)
  To: Carlos Maiolino; +Cc: xfs

On Thu, Nov 26, 2015 at 04:46:35PM +0100, Carlos Maiolino wrote:
> Implements a new xfs_io command, named 'inode', which is supposed to be
> used to query information about inode's existence and its physical size
> in the filesystem.
> 
> Supported options:
> 
> Default:     -- Return true(1) or false(0) if any inode greater than
>                 32bits has been found in the filesystem
> -v           -- verbose mode
>                 Display the number and the physical size (in bits)
>                 of the largest inode in the filesystem
> [num]        -- Return true(1) or false(0) if the inode [num] is in use
> -n [num]     -- Return the next valid inode after [num]
> 
> No manpage sent because there were changes in the supported options and its
> descriptions.
> I'll send the manpage after the options and descriptions are reviewed.
> 
> - Changelog
> 
> V3:
> 	- Merge all 3 patches from the V2 together in a single patch
> 	- Rework of '-n [num]' and 'num' only arguments algorithm
> 	- Argument -n now relies on bulkreq.count to check for next inodes, not
> 	  on bstat.bs_ino anymore.
> 	- for loop in ret_lsize or ret_largest case, now relies on count being 0
> 	  to break the loop
> 
> V4:
> 	- Refactor inode_f function to reduce its size and easier logic
> 	- Implement error handlers for invalid command combination (hopefully
> 	  all invalid combinations).
> 	- use a single xfs_inogrp array for keep track of inodes
> 	- Fix missing newline in inode_help()
> 	- Rewrite help message in inode_help()
> 	- Fix indentation
> 
> V5:
> 	- Reduce the amount of options
> 	- remove igrp_rec variable, and use igroup[lastgrp] directly to get
> 	  information from the last inode groups returned by ioctl
> 
> Signed-off-by: Carlos Maiolino <cmaiolino@redhat.com>
> ---
>  io/open.c | 143 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 143 insertions(+)
> 
> diff --git a/io/open.c b/io/open.c
> index ac5a5e0..1e38ea8 100644
> --- a/io/open.c
> +++ b/io/open.c
> @@ -20,6 +20,7 @@
>  #include "input.h"
>  #include "init.h"
>  #include "io.h"
> +#include "libxfs.h"
>  
>  #ifndef __O_TMPFILE
>  #if defined __alpha__
> @@ -44,6 +45,7 @@ static cmdinfo_t statfs_cmd;
>  static cmdinfo_t chproj_cmd;
>  static cmdinfo_t lsproj_cmd;
>  static cmdinfo_t extsize_cmd;
> +static cmdinfo_t inode_cmd;
>  static prid_t prid;
>  static long extsize;
>  
> @@ -750,6 +752,136 @@ statfs_f(
>  	return 0;
>  }
>  
> +static void
> +inode_help(void)
> +{
> +	printf(_(
> +"\n"
> +"Query physical information about the inode"
> +"\n"
> +" Default:	-- Return true(1) or false(0) if any inode greater than\n"
> +"		   32bits has been found in the filesystem\n"
> +" -v		-- verbose mode\n"
> +"		   Display the number and the physical size (in bits)\n"
> +"		   of the largest inode in the filesystem\n"
> +"[num]		-- Return true(1) or false(0) if the inode [num] is in use\n"
> +" -n [num]	-- Return the next valid inode after [num]\n"
> +"\n"));
> +}
> +
> +static int
> +inode_f(
> +	  int			argc,
> +	  char			**argv)
> +{
> +	__s32			count = 0;
> +	__s32			lastgrp = 0;
> +	__u64			last = 0;
> +	__u64			lastino = 0;
> +	__u64			userino = 0;
> +	char			*p;
> +	int			c;
> +	int			verbose = 0;
> +	int			ret_next = 0;
> +	int			cmd = 0;
> +	struct xfs_inogrp	igroup[1024];
> +	struct xfs_fsop_bulkreq	bulkreq;
> +	struct xfs_bstat	bstat;
> +
> +	while ((c = getopt(argc, argv, "nv")) != EOF) {

I think we want "n:v" here since -n expects an argument, even if we
don't process the arg here.

> +		switch (c) {
> +		case 'v':
> +			verbose = 1;
> +			break;
> +		case 'n':
> +			ret_next = 1;
> +			break;
> +		default:
> +			return command_usage(&inode_cmd);
> +		}
> +	}
> +
> +	if (ret_next && verbose)
> +		return command_usage(&inode_cmd);
> +

Why is this not supported? Hmm, I see that -n returns an inode number
and otherwise we print 0/1 or <inode>:<size> with -v. Perhaps this would
be easier if the command semantics/output were more consistent. E.g., 

"inode": print 0/1 based on largest inode size
"inode -v": print <ino>:<size> of largest inode
"inode <ino>": print <ino> if inode exists
"inode -v <ino>": print <ino>:<size> if inode exists
"inode -n <ino>": print <next ino> if next inode exists
"inode -nv <ino>": print <next ino>:<size> if next inode exists

In other words, the default behavior is to identify the 32-bit/64-bit
state of the fs. If an inode is provided, we print the inode number if
the inode exists. The -n flags alters this behavior to find the next
inode. The -v flag alters the previous two situations to also print the
inode size.

> +	if (optind < argc) {

A comment above this check to explain what it means (i.e., user passed
an inode number) would be nice.

> +		if (verbose)
> +			return command_usage(&inode_cmd);

Also, why is this not supported (see above)?

> +
> +		if (ret_next) {
> +			cmd = XFS_IOC_FSBULKSTAT;
> +		} else {
> +			if (argc > 2)
> +				return command_usage(&inode_cmd);
> +			else
> +				cmd = XFS_IOC_FSBULKSTAT_SINGLE;
> +		}
> +
> +		userino = strtoull(argv[optind], &p, 10);
> +		if ((*p != '\0')) {
> +			printf(_("[num] must be a numeric value\n"));
> +			exitcode = 1;
> +			return 0;
> +		}
> +
> +		bulkreq.lastip = &userino;
> +		bulkreq.icount = 1;
> +		bulkreq.ubuffer = &bstat;
> +		bulkreq.ocount = &count;
> +
> +		if (xfsctl(file->name, file->fd, cmd, &bulkreq)) {
> +			if (errno == EINVAL) {
> +				if (!ret_next)
> +					printf("0\n");
> +			} else {
> +				perror("xfsctl");
> +			}
> +			exitcode = 1;
> +			return 0;
> +		}
> +
> +		if (ret_next) {
> +			printf("%llu\n", bstat.bs_ino);
> +			return 0;
> +		} else {
> +			/* Inode number used*/
> +			printf("1\n");
> +			return 0;
> +		}

The return 0 can go after the if/else.

> +	}
> +

	/*
	 * The user has not provided an inode number. Therefore, find
	 * the largest inode in the fs.
	 */

Brian

> +	bulkreq.lastip = &last;
> +	bulkreq.icount = 1024; /* User-defined maybe!? */
> +	bulkreq.ubuffer = &igroup;
> +	bulkreq.ocount = &count;
> +
> +	for (;;) {
> +		if (xfsctl(file->name, file->fd, XFS_IOC_FSINUMBERS,
> +				&bulkreq)) {
> +			perror("XFS_IOC_FSINUMBERS");
> +			exitcode = 1;
> +			return 0;
> +		}
> +
> +		if (count == 0)
> +			break;
> +
> +		lastgrp = count;
> +	}
> +
> +	lastgrp--;
> +	lastino = igroup[lastgrp].xi_startino +
> +		  xfs_highbit64(igroup[lastgrp].xi_allocmask);
> +
> +	if (verbose)
> +		printf("%llu:%d\n", lastino,
> +			lastino > XFS_MAXINUMBER_32 ? 64 : 32);
> +	else
> +		printf("%d\n", lastino > XFS_MAXINUMBER_32 ? 1 : 0);
> +
> +	return 0;
> +}
> +
>  void
>  open_init(void)
>  {
> @@ -815,6 +947,16 @@ open_init(void)
>  		_("get/set preferred extent size (in bytes) for the open file");
>  	extsize_cmd.help = extsize_help;
>  
> +	inode_cmd.name = "inode";
> +	inode_cmd.cfunc = inode_f;
> +	inode_cmd.args = _("[-n | -v] [num]");
> +	inode_cmd.argmin = 0;
> +	inode_cmd.argmax = 2;
> +	inode_cmd.flags = CMD_NOMAP_OK;
> +	inode_cmd.oneline =
> +		_("Query inode number usage in the filesystem");
> +	inode_cmd.help = inode_help;
> +
>  	add_command(&open_cmd);
>  	add_command(&stat_cmd);
>  	add_command(&close_cmd);
> @@ -822,4 +964,5 @@ open_init(void)
>  	add_command(&chproj_cmd);
>  	add_command(&lsproj_cmd);
>  	add_command(&extsize_cmd);
> +	add_command(&inode_cmd);
>  }
> -- 
> 2.4.3
> 
> _______________________________________________
> xfs mailing list
> xfs@oss.sgi.com
> http://oss.sgi.com/mailman/listinfo/xfs

_______________________________________________
xfs mailing list
xfs@oss.sgi.com
http://oss.sgi.com/mailman/listinfo/xfs

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

* Re: [PATCH] xfs_io: implement 'inode' command V5
  2015-11-30 13:22 ` Brian Foster
@ 2015-11-30 14:26   ` Carlos Maiolino
  2015-11-30 16:16     ` Brian Foster
  0 siblings, 1 reply; 4+ messages in thread
From: Carlos Maiolino @ 2015-11-30 14:26 UTC (permalink / raw)
  To: Brian Foster; +Cc: xfs

> 
> I think we want "n:v" here since -n expects an argument, even if we
> don't process the arg here.

Using getopt() to handle the -n argument, will make the inode command having 2
different entry points for the same argument, i.e. the inode number. One as an
argument for -n, and another as an argument for the command itself, like:

inode -n <num>
inode <num>

We need to handle [num] as a stand-alone argument anyway, so, I just don't think
we need to handle the same argument in different ways, which I achieved by not
using [num] as a getopt() argument, but instead, handling [num] 'manually'
according to the options used in getopt().

Not sure if I could be clear or get things more confused :)


> > +	if (ret_next && verbose)
> > +		return command_usage(&inode_cmd);
> > +
> 
> Why is this not supported? Hmm, I see that -n returns an inode number
> and otherwise we print 0/1 or <inode>:<size> with -v. Perhaps this would
> be easier if the command semantics/output were more consistent. E.g., 
> 
> "inode": print 0/1 based on largest inode size
> "inode -v": print <ino>:<size> of largest inode
> "inode <ino>": print <ino> if inode exists
> "inode -v <ino>": print <ino>:<size> if inode exists

I thought about this, but I decided to not do it because the command looks a bit
redundant for me when 'inode <ino' was returning 0 or 1. Returning the inode
number itself, if it exists, makes more sense to have a -v option here too.

> "inode -n <ino>": print <next ino> if next inode exists
> "inode -nv <ino>": print <next ino>:<size> if next inode exists

Just FYI, if the 'next inode' doesn't exist (i.e. using the last fs inode as
argument), the ioctl will return 0 in bstat.bs_ino, which, I choose to leave it
as-is, and adding this observation to the man page, instead of returning a
messag like "no more inodes in the fs".

I decided to leave it as-is, because for usage would be easier to parse a '0'
return value from -n argument, than parsing an error message which has the same
meaning of a zeroed return.


Anyway, I'm going add -v to the another options, just please take a look at my
replies regarding the 'inode -n' return value and the reason I didn't use
getopt() to handle -n argument and if you agree or not, so I'll rewrite the
patch to v6 based on this.

Cheers o>

-- 
Carlos

_______________________________________________
xfs mailing list
xfs@oss.sgi.com
http://oss.sgi.com/mailman/listinfo/xfs

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

* Re: [PATCH] xfs_io: implement 'inode' command V5
  2015-11-30 14:26   ` Carlos Maiolino
@ 2015-11-30 16:16     ` Brian Foster
  0 siblings, 0 replies; 4+ messages in thread
From: Brian Foster @ 2015-11-30 16:16 UTC (permalink / raw)
  To: xfs

On Mon, Nov 30, 2015 at 03:26:22PM +0100, Carlos Maiolino wrote:
> > 
> > I think we want "n:v" here since -n expects an argument, even if we
> > don't process the arg here.
> 
> Using getopt() to handle the -n argument, will make the inode command having 2
> different entry points for the same argument, i.e. the inode number. One as an
> argument for -n, and another as an argument for the command itself, like:
> 
> inode -n <num>
> inode <num>
> 
> We need to handle [num] as a stand-alone argument anyway, so, I just don't think
> we need to handle the same argument in different ways, which I achieved by not
> using [num] as a getopt() argument, but instead, handling [num] 'manually'
> according to the options used in getopt().
> 
> Not sure if I could be clear or get things more confused :)
> 

Sure, but I'm just referring to the error case when the user passes -n
without an argument. This should return an error but it doesn't at the
moment. I'm assuming that using "n:" would ensure the error message is
printed without disrupting the other code (e.g., continue to process
[num] manually even though "n:" is passed to getopt()). Is that the
case? If not, the error could be detected/handled manually as well.

Either way, a comment would also be useful here to document the special
handling as you note above.

> 
> > > +	if (ret_next && verbose)
> > > +		return command_usage(&inode_cmd);
> > > +
> > 
> > Why is this not supported? Hmm, I see that -n returns an inode number
> > and otherwise we print 0/1 or <inode>:<size> with -v. Perhaps this would
> > be easier if the command semantics/output were more consistent. E.g., 
> > 
> > "inode": print 0/1 based on largest inode size
> > "inode -v": print <ino>:<size> of largest inode
> > "inode <ino>": print <ino> if inode exists
> > "inode -v <ino>": print <ino>:<size> if inode exists
> 
> I thought about this, but I decided to not do it because the command looks a bit
> redundant for me when 'inode <ino' was returning 0 or 1. Returning the inode
> number itself, if it exists, makes more sense to have a -v option here too.
> 

Not sure I follow... AFAICT the command semantics change depending on
whether an inode number is passed or not (irrespective of -n and -v). If
not, we're looking to see if the largest inode is 32-bit or 64-bit. If
an inode number is passed, we're checking to see if an inode exists.

Brian

> > "inode -n <ino>": print <next ino> if next inode exists
> > "inode -nv <ino>": print <next ino>:<size> if next inode exists
> 
> Just FYI, if the 'next inode' doesn't exist (i.e. using the last fs inode as
> argument), the ioctl will return 0 in bstat.bs_ino, which, I choose to leave it
> as-is, and adding this observation to the man page, instead of returning a
> messag like "no more inodes in the fs".
> 
> I decided to leave it as-is, because for usage would be easier to parse a '0'
> return value from -n argument, than parsing an error message which has the same
> meaning of a zeroed return.
> 
> 
> Anyway, I'm going add -v to the another options, just please take a look at my
> replies regarding the 'inode -n' return value and the reason I didn't use
> getopt() to handle -n argument and if you agree or not, so I'll rewrite the
> patch to v6 based on this.
> 
> Cheers o>
> 
> -- 
> Carlos

_______________________________________________
xfs mailing list
xfs@oss.sgi.com
http://oss.sgi.com/mailman/listinfo/xfs

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

end of thread, other threads:[~2015-11-30 16:16 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-11-26 15:46 [PATCH] xfs_io: implement 'inode' command V5 Carlos Maiolino
2015-11-30 13:22 ` Brian Foster
2015-11-30 14:26   ` Carlos Maiolino
2015-11-30 16:16     ` Brian Foster

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.