[LTP] [PATCH] tst_find_backing_dev: match mount point if major/minor can't be found

Jan Stancek jstancek@redhat.com
Wed Apr 27 14:08:45 CEST 2022


Test fails on btrfs, because tst_find_backing_dev doesn't find
major/minor returned by stat()

Per https://lwn.net/Articles/866582
"btrfs allocates a separate device number (the usual major/minor pair)
for each subvolume ... and call to on a file within a subvolume will
return a device number that does not exist in files like mountinfo."

As fallback, if major/minor can't be found, use best match of mount path.

Signed-off-by: Jan Stancek <jstancek@redhat.com>
---
 lib/tst_device.c | 31 +++++++++++++++++++++----------
 1 file changed, 21 insertions(+), 10 deletions(-)

diff --git a/lib/tst_device.c b/lib/tst_device.c
index d296f9118cde..e560ec97460b 100644
--- a/lib/tst_device.c
+++ b/lib/tst_device.c
@@ -506,14 +506,22 @@ unsigned long tst_dev_bytes_written(const char *dev)
 	return dev_bytes_written;
 }
 
+static int count_match_len(const char *first, const char *second)
+{
+	int len = 0;
+
+	while (*first && *first++ == *second++)
+		len++;
+
+	return len;
+}
+
 void tst_find_backing_dev(const char *path, char *dev)
 {
 	struct stat buf;
 	FILE *file;
-	char line[PATH_MAX];
-	char *pre = NULL;
-	char *next = NULL;
-	unsigned int dev_major, dev_minor, line_mjr, line_mnr;
+	char line[PATH_MAX], mnt_point[PATH_MAX], mnt_source[PATH_MAX];
+	unsigned int dev_major, dev_minor, line_mjr, line_mnr, best_match_len = 0;
 
 	if (stat(path, &buf) < 0)
 		tst_brkm(TWARN | TERRNO, NULL, "stat() failed");
@@ -524,17 +532,20 @@ void tst_find_backing_dev(const char *path, char *dev)
 	*dev = '\0';
 
 	while (fgets(line, sizeof(line), file)) {
-		if (sscanf(line, "%*d %*d %d:%d", &line_mjr, &line_mnr) != 2)
+		if (sscanf(line, "%*d %*d %d:%d %*s %s %*s %*s %*s %*s %s",
+			&line_mjr, &line_mnr, mnt_point, mnt_source) != 4)
 			continue;
 
 		if (line_mjr == dev_major && line_mnr == dev_minor) {
-			pre = strstr(line, " - ");
-			pre = strtok_r(pre, " ", &next);
-			pre = strtok_r(NULL, " ", &next);
-			pre = strtok_r(NULL, " ", &next);
-			strcpy(dev, pre);
+			strcpy(dev, mnt_source);
 			break;
 		}
+
+		unsigned int len = count_match_len(path, mnt_point);
+		if (len > best_match_len) {
+			strcpy(dev, mnt_source);
+			best_match_len = len;
+		}
 	}
 
 	SAFE_FCLOSE(NULL, file);
-- 
2.27.0



More information about the ltp mailing list