Linux Test Project
 help / color / mirror / Atom feed
From: Richard Palethorpe via ltp <ltp@lists.linux.it>
To: ltp@lists.linux.it
Cc: Richard Palethorpe <rpalethorpe@suse.com>
Subject: [LTP] [PATCH v2 2/3] API/device: Add func to stat the actual dev mounted to a path
Date: Tue, 29 Mar 2022 08:44:39 +0100	[thread overview]
Message-ID: <20220329074440.26214-2-rpalethorpe@suse.com> (raw)
In-Reply-To: <20220329074440.26214-1-rpalethorpe@suse.com>

BTRFS appears to create "anonymous" block devices for each sub
partition. Stating an inode on this FS (and others) just returns the
anon dev number. Instead we have to find the mounted device in
/proc/self/mounts and stat its special file in /dev/.

Signed-off-by: Richard Palethorpe <rpalethorpe@suse.com>
---
 include/tst_device.h |  6 ++++++
 lib/tst_device.c     | 35 +++++++++++++++++++++++++++++++++++
 2 files changed, 41 insertions(+)

diff --git a/include/tst_device.h b/include/tst_device.h
index 95ccd453e..977427f1c 100644
--- a/include/tst_device.h
+++ b/include/tst_device.h
@@ -8,6 +8,7 @@
 
 #include <unistd.h>
 #include <stdint.h>
+#include <sys/stat.h>
 
 struct tst_device {
 	const char *dev;
@@ -112,6 +113,11 @@ void tst_purge_dir(const char *path);
  */
 void tst_find_backing_dev(const char *path, char *dev);
 
+/*
+ * Stat the device mounted on a given path.
+ */
+void tst_stat_mount_dev(const char *const mnt_path, struct stat *const st);
+
 /*
  * Returns the size of a physical device block size for the specific path
  * @path   Path to find the block size
diff --git a/lib/tst_device.c b/lib/tst_device.c
index 1ef667fa0..d296f9118 100644
--- a/lib/tst_device.c
+++ b/lib/tst_device.c
@@ -25,6 +25,7 @@
 #include <sys/stat.h>
 #include <sys/ioctl.h>
 #include <sys/mount.h>
+#include <mntent.h>
 #include <errno.h>
 #include <unistd.h>
 #include <stdlib.h>
@@ -548,6 +549,40 @@ void tst_find_backing_dev(const char *path, char *dev)
 		tst_brkm(TCONF, NULL, "dev(%s) isn't a block dev", dev);
 }
 
+void tst_stat_mount_dev(const char *const mnt_path, struct stat *const st)
+{
+	struct mntent *mnt;
+	FILE *mntf = setmntent("/proc/self/mounts", "r");
+
+	if (!mntf) {
+		tst_brkm(TBROK | TERRNO, NULL, "Can't open /proc/self/mounts");
+		return;
+	}
+
+	mnt = getmntent(mntf);
+	if (!mnt) {
+		tst_brkm(TBROK | TERRNO, NULL, "Can't read mounts or no mounts?");
+		return;
+	}
+
+	do {
+		if (strcmp(mnt->mnt_dir, mnt_path)) {
+			mnt = getmntent(mntf);
+			continue;
+		}
+
+		if (stat(mnt->mnt_fsname, st)) {
+			tst_brkm(TBROK | TERRNO, NULL,
+				 "Can't stat '%s', mounted at '%s'",
+				 mnt->mnt_fsname, mnt_path);
+		}
+
+		return;
+	} while (mnt);
+
+	tst_brkm(TBROK, NULL, "Could not find mount device");
+}
+
 int tst_dev_block_size(const char *path)
 {
 	int fd;
-- 
2.35.1


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

  reply	other threads:[~2022-03-29  7:45 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-29  7:44 [LTP] [PATCH v2 1/3] API/cgroup: Add io controller Richard Palethorpe via ltp
2022-03-29  7:44 ` Richard Palethorpe via ltp [this message]
2022-04-12  8:46   ` [LTP] [PATCH v2 2/3] API/device: Add func to stat the actual dev mounted to a path Petr Vorel
2022-03-29  7:44 ` [LTP] [PATCH v2 3/3] cgroups: Add first IO controller test Richard Palethorpe via ltp
2022-04-01  8:26   ` Petr Vorel
2022-04-12 11:28     ` Richard Palethorpe
2022-04-12  8:58   ` Petr Vorel
2022-04-12  8:26 ` [LTP] [PATCH v2 1/3] API/cgroup: Add io controller Petr Vorel

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=20220329074440.26214-2-rpalethorpe@suse.com \
    --to=ltp@lists.linux.it \
    --cc=rpalethorpe@suse.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox