[LTP] [PATCH v2 2/3] API/device: Add func to stat the actual dev mounted to a path

Richard Palethorpe rpalethorpe@suse.com
Tue Mar 29 09:44:39 CEST 2022


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



More information about the ltp mailing list