All of lore.kernel.org
 help / color / mirror / Atom feed
From: Eric Sandeen <sandeen@sandeen.net>
To: "Darrick J. Wong" <darrick.wong@oracle.com>, sandeen@redhat.com
Cc: linux-xfs@vger.kernel.org, djwong@kernel.org
Subject: [RFC PATCH 17/16] xfs_spaceman: only produce info for root of mounted xfs
Date: Wed, 7 Mar 2018 12:34:24 -0600	[thread overview]
Message-ID: <f85deeb7-b9f3-9be2-9684-f241da880302@sandeen.net> (raw)
In-Reply-To: <151993157539.22223.6269629008244777191.stgit@magnolia>

Previously, xfs_info / xfs_growfs would only operate if
pointed at the root of a mounted xfs filesystem, and not a
file within that filesystem. (Although the ioctl will allow it,
the utility does not, to avoid errors and confusion).

When the info capability was moved to xfs_spaceman, this restriction
got lost.

And now that xfs_db can print xfs_info-like info about image files
and devices, the potential exists for misdirecting the request
and running the ioctl on a file and getting the underlying fs
info, rather than using libxfs to parse the image itself, if
the wrong tool is used to point at the file.

Add a new function to (ab)use XFS_IOC_FSINUMBERS to determine
whether the thing we've been pointed at is in fact the root
directory of a mounted xfs filesystem.  If not, don't proceed.

Signed-off-by: Eric Sandeen <sandeen@redhat.com>
---

I need to look, perhaps this could simplify growfs as well.
I'm not sure I've put this in the right place (libfrog?) either,
but for the sake of discsussion ...

diff --git a/include/libfrog.h b/include/libfrog.h
index 2d8055b..f7cd00a 100644
--- a/include/libfrog.h
+++ b/include/libfrog.h
@@ -18,6 +18,9 @@
 #ifndef __LIBFROG_UTIL_H_
 #define __LIBFROG_UTIL_H_
 
+#include <stdbool.h>
+
 unsigned int	log2_roundup(unsigned int i);
+bool		fd_is_xfs_root(int fd);
 
 #endif /* __LIBFROG_UTIL_H_ */
diff --git a/libfrog/util.c b/libfrog/util.c
index 4896e4b..4913f38 100644
--- a/libfrog/util.c
+++ b/libfrog/util.c
@@ -16,6 +16,7 @@
  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  */
 #include "platform_defs.h"
+#include "libxfs.h"
 #include "libfrog.h"
 
 /*
@@ -34,3 +35,44 @@ log2_roundup(unsigned int i)
 	}
 	return rval;
 }
+
+/* Test whether a given fd is the root of a mounted xfs filesystem */
+bool
+fd_is_xfs_root(
+	int			fd)
+{
+	struct xfs_fsop_bulkreq	igrpreq = {0};
+	struct xfs_inogrp	inogrp;
+	__u64			igrp_ino = 0;
+	__s32			igrplen = 0;
+	__u64			first_ino;
+	struct stat		statbuf;
+
+	igrpreq.lastip  = &igrp_ino;
+	igrpreq.icount = 1;
+	igrpreq.ubuffer = &inogrp;
+	igrpreq.ocount  = &igrplen;
+
+	if (ioctl(fd, XFS_IOC_FSINUMBERS, &igrpreq))
+		return false;
+
+	/* find the first allocated inode */
+	while (!ioctl(fd, XFS_IOC_FSINUMBERS, &igrpreq)) {
+		if (inogrp.xi_alloccount)
+			break;
+        }
+
+	/* The ioctl failed ... */
+	if (!inogrp.xi_alloccount)
+		return false;
+
+	first_ino = inogrp.xi_startino + libxfs_lowbit64(inogrp.xi_allocmask);
+
+	if (fstat(fd, &statbuf))
+		return false;
+
+	if (first_ino != statbuf.st_ino)
+		return false;
+
+	return true;
+}
diff --git a/libxfs/libxfs_api_defs.h b/libxfs/libxfs_api_defs.h
index a23a28d..79a0bd7 100644
--- a/libxfs/libxfs_api_defs.h
+++ b/libxfs/libxfs_api_defs.h
@@ -25,6 +25,8 @@
  * it can be included in both the internal and external libxfs header files
  * without introducing any depenencies between the two.
  */
+#define xfs_lowbit32			libxfs_lowbit32
+#define xfs_lowbit64			libxfs_lowbit64
 #define xfs_highbit32			libxfs_highbit32
 #define xfs_highbit64			libxfs_highbit64
 
diff --git a/spaceman/info.c b/spaceman/info.c
index 6557b54..df088f4 100644
--- a/spaceman/info.c
+++ b/spaceman/info.c
@@ -44,6 +44,13 @@ info_f(
 	struct xfs_fsop_geom	geo;
 	int			error;
 
+	if (!fd_is_xfs_root(file->fd)) {
+		fprintf(stderr, _("%s: %s is not a mounted XFS filesystem\n"),
+			progname, file->name);
+		exitcode = 1;
+		return 0;
+	}
+
 	/* get the current filesystem size & geometry */
 	error = ioctl(file->fd, XFS_IOC_FSGEOMETRY, &geo);
 	if (error) {


      parent reply	other threads:[~2018-03-07 18:41 UTC|newest]

Thread overview: 43+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-03-01 19:12 [PATCH 00/16] xfsprogs: misc fixes, geometry refactoring Darrick J. Wong
2018-03-01 19:13 ` [PATCH 01/16] misc: fix gcc 7.3 warnings Darrick J. Wong
2018-03-02 22:11   ` Eric Sandeen
2018-03-01 19:13 ` [PATCH 02/16] xfs_db: don't crash in ablock if there's no inode Darrick J. Wong
2018-03-01 19:13 ` [PATCH 03/16] xfs_scrub: log operational messages when interactive Darrick J. Wong
2018-03-08 19:35   ` [PATCH v2 " Darrick J. Wong
2018-03-08 19:52     ` Eric Sandeen
2018-03-01 19:13 ` [PATCH 04/16] xfs_scrub: don't ask user to run xfs_repair for only warnings Darrick J. Wong
2018-03-06 17:16   ` Eric Sandeen
2018-03-06 17:27     ` Darrick J. Wong
2018-03-06 18:34       ` Eric Sandeen
2018-03-06 18:53         ` Darrick J. Wong
2018-03-06 19:00           ` Eric Sandeen
2018-03-06 23:24             ` Darrick J. Wong
2018-03-08 19:36   ` [PATCH v2 " Darrick J. Wong
2018-03-08 20:20     ` Eric Sandeen
2018-03-01 19:13 ` [PATCH 05/16] xfs_scrub: fix #include ordering to avoid build failure Darrick J. Wong
2018-03-06 17:19   ` Eric Sandeen
2018-03-01 19:13 ` [PATCH 06/16] xfs_scrub: don't try to scan xattrs if bstat says there aren't any Darrick J. Wong
2018-03-06 17:19   ` Eric Sandeen
2018-03-01 19:13 ` [PATCH 07/16] xfs_db: print transaction reservation type information Darrick J. Wong
2018-03-06 19:16   ` Eric Sandeen
2018-03-01 19:13 ` [PATCH 08/16] xfs_repair: don't fail directory repairs when grabbing inodes Darrick J. Wong
2018-03-06 19:23   ` Eric Sandeen
2018-03-06 19:43     ` Darrick J. Wong
2018-03-06 20:55       ` Darrick J. Wong
2018-03-01 19:14 ` [PATCH 09/16] misc: enable link time optimization, if requested Darrick J. Wong
2018-03-07  3:00   ` Eric Sandeen
2018-03-01 19:14 ` [PATCH 10/16] libfrog: refactor fs geometry printing function Darrick J. Wong
2018-03-01 19:14 ` [PATCH 11/16] mkfs: use geometry generation / helper functions Darrick J. Wong
2018-03-01 19:14 ` [PATCH 12/16] xfs_db: add a superblock info command Darrick J. Wong
2018-03-06 19:32   ` Eric Sandeen
2018-03-06 19:34     ` Eric Sandeen
2018-03-06 20:49       ` Darrick J. Wong
2018-03-08  4:14         ` Dave Chinner
2018-03-01 19:14 ` [PATCH 13/16] xfs_spaceman: " Darrick J. Wong
2018-03-01 19:14 ` [PATCH 14/16] xfs_info: move to xfs_spaceman Darrick J. Wong
2018-03-07  3:50   ` Eric Sandeen
2018-03-07 20:33     ` Darrick J. Wong
2018-03-08  4:17       ` Dave Chinner
2018-03-01 19:14 ` [PATCH 15/16] xfs_info: call xfs_db for offline filesystems Darrick J. Wong
2018-03-01 19:14 ` [PATCH 16/16] xfs_growfs: refactor geometry reporting Darrick J. Wong
2018-03-07 18:34 ` Eric Sandeen [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=f85deeb7-b9f3-9be2-9684-f241da880302@sandeen.net \
    --to=sandeen@sandeen.net \
    --cc=darrick.wong@oracle.com \
    --cc=djwong@kernel.org \
    --cc=linux-xfs@vger.kernel.org \
    --cc=sandeen@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.