Skip to contentSkip to conversation

Fix tst_find_backing_dev when no initramfs

PATCH In review Alessandro Carminati 4 participants 7 messages Oct 17, 2022 – Oct 27, 2022 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Fix tst_find_backing_dev when no initramfs

mount_root() is the kernel function responsible for mounting the primary rootfs.
A dynamic there, prevents the /dev/root device node in the not yet mounted files system. For this reason, in the embedded system that starts without an initramfs, or however a proper initscript, the /dev/root device appears into the mount table in the / line.
The test tries to open this /dev/root and fails with a warning. This patch aims to fix this situation.

typo fixes

---
 lib/tst_device.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
lib/tst_device.c +20 -0
index 8419b80c3..c3427eb31 100644
@@ -526,6 +526,8 @@ void tst_find_backing_dev(const char *path, char *dev)
 	unsigned int dev_major, dev_minor, line_mjr, line_mnr;
 	unsigned int len, best_match_len = 1;
 	char mnt_point[PATH_MAX];
+	char tmpbuf1[PATH_MAX];
+	char tmpbuf2[PATH_MAX];
 
 	if (stat(path, &buf) < 0)
 		tst_brkm(TWARN | TERRNO, NULL, "stat() failed");
@@ -562,6 +564,24 @@ void tst_find_backing_dev(const char *path, char *dev)
 	if (!*dev)
 		tst_brkm(TBROK, NULL, "Cannot find block device for %s", path);
 
+	if (stat(dev, &buf) < 0) {
+		if (strcmp("/dev/root", dev) != 0) {
+			tst_brkm(TWARN | TERRNO, NULL, "stat(%s) failed", dev);
+		} else {
+			sprintf(tmpbuf1, "/sys/dev/block/%d:%d/uevent", dev_major, dev_minor);
+			file = SAFE_FOPEN(NULL, tmpbuf1, "r");
+			while (fgets(line, sizeof(line), file)) {
+				if (sscanf(line, "%[^=]=%s", tmpbuf1, tmpbuf2) != 2)
+					continue;
+				if (strcmp("DEVNAME", tmpbuf1) == 0) {
+					sprintf(dev, "/dev/%s", tmpbuf2);
+					break;
+				}
+			}
+			SAFE_FCLOSE(NULL, file);
+		}
+	}
+
 	if (stat(dev, &buf) < 0)
 		tst_brkm(TWARN | TERRNO, NULL, "stat(%s) failed", dev);
 
Signature · 5 lines
2.34.1


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#1 lore raw

Hello,

Quoted 8 lines · Alessandro Carminati

mount_root() is the kernel function responsible for mounting the primary rootfs.
A dynamic there, prevents the /dev/root device node in the not yet mounted files system. For this reason, in the embedded system that starts without an initramfs, or however a proper initscript, the /dev/root device appears into the mount table in the / line.
The test tries to open this /dev/root and fails with a warning. This patch aims to fix this situation.

Thanks I probably would have hit this issue sooner or later.

Quoted 13 lines

typo fixes

---
 lib/tst_device.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
lib/tst_device.c +0 -0
index 8419b80c3..c3427eb31 100644

lib/tst_device.c void tst_find_backing_dev(const char *path, char *dev) · line 530

Quoted 5 lines
lib/tst_device.c +2 -0
@@ -526,6 +526,8 @@ void tst_find_backing_dev(const char *path, char *dev)
 	unsigned int dev_major, dev_minor, line_mjr, line_mnr;
 	unsigned int len, best_match_len = 1;
 	char mnt_point[PATH_MAX];
+	char tmpbuf1[PATH_MAX];
+	char tmpbuf2[PATH_MAX];

It would be more readable to use three buffers and give them meaningful names.

lib/tst_device.c void tst_find_backing_dev(const char *path, char *dev) · line 567

Quoted 8 lines
lib/tst_device.c +1 -0
 	if (stat(path, &buf) < 0)
 		tst_brkm(TWARN | TERRNO, NULL, "stat() failed");
@@ -562,6 +564,24 @@ void tst_find_backing_dev(const char *path, char *dev)
 	if (!*dev)
 		tst_brkm(TBROK, NULL, "Cannot find block device for %s", path);
 
+	if (stat(dev, &buf) < 0) {

As there is no harm in calling stat twice; could just do

if (stat(dev, &buf) < 0) && strcmp("/dev/root", dev) == 0) {

Or even replace with strcmp with errno == ENOENT and use this as a general fallback.

lib/tst_device.c void tst_find_backing_dev(const char *path, char *dev) · line 570

Quoted 3 lines
lib/tst_device.c +3 -0
+		if (strcmp("/dev/root", dev) != 0) {
+			tst_brkm(TWARN | TERRNO, NULL, "stat(%s) failed", dev);
+		} else {

Then remove this if statement

Quoted 20 lines
lib/tst_device.c +14 -0
+			sprintf(tmpbuf1, "/sys/dev/block/%d:%d/uevent",
 dev_major, dev_minor);
+			file = SAFE_FOPEN(NULL, tmpbuf1, "r");
+			while (fgets(line, sizeof(line), file)) {
+				if (sscanf(line, "%[^=]=%s", tmpbuf1, tmpbuf2) != 2)
+					continue;
+				if (strcmp("DEVNAME", tmpbuf1) == 0) {
+					sprintf(dev, "/dev/%s", tmpbuf2);
+					break;
+				}
+			}
+			SAFE_FCLOSE(NULL, file);
+		}
+	}
+
 	if (stat(dev, &buf) < 0)
 		tst_brkm(TWARN | TERRNO, NULL, "stat(%s) failed", dev);
Signature · 2 lines
-- 
2.34.1
Signature · 5 lines
Thank you,
Richard.

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#2 lore raw

mount_root() is the kernel function responsible for mounting the primary rootfs.
A dynamic there, prevents the /dev/root device node in the not yet mounted files system. For this reason, in the embedded system that starts without an initramfs, or however a proper initscript, the /dev/root device appears into the mount table in the / line.
The test ioctl_loop05 tries to open this /dev/root and fails with a warning. This patch aims to fix this situation.

---
 lib/tst_device.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
lib/tst_device.c +16 -0
index 8419b80c3..c67328db3 100644
@@ -526,6 +526,8 @@ void tst_find_backing_dev(const char *path, char *dev)
        unsigned int dev_major, dev_minor, line_mjr, line_mnr;
        unsigned int len, best_match_len = 1;
        char mnt_point[PATH_MAX];
+       char devnum_info_fn[PATH_MAX];
+       char new_dev_fn_buf[PATH_MAX];
 
        if (stat(path, &buf) < 0)
                tst_brkm(TWARN | TERRNO, NULL, "stat() failed");
@@ -562,6 +564,20 @@ void tst_find_backing_dev(const char *path, char *dev)
        if (!*dev)
                tst_brkm(TBROK, NULL, "Cannot find block device for %s", path);
 
+       if (stat(dev, &buf) < 0) && (strcmp("/dev/root", dev)==0) {
+               sprintf(devnum_info_fn, "/sys/dev/block/%d:%d/uevent", dev_major, dev_minor);
+               file = SAFE_FOPEN(NULL, devnum_info_fn, "r");
+               while (fgets(line, sizeof(line), file)) {
+                       if (sscanf(line, "%[^=]=%s", devnum_info_fn, new_dev_fn_buf) != 2)
+                               continue;
+                       if (strcmp("DEVNAME", devnum_info_fn) == 0) {
+                               sprintf(dev, "/dev/%s", new_dev_fn_buf);
+                               break;
+                       }
+               }
+               SAFE_FCLOSE(NULL, file);
+       }
+
        if (stat(dev, &buf) < 0)
                tst_brkm(TWARN | TERRNO, NULL, "stat(%s) failed", dev);
 

--
2.34.1

Quoted 83 lines · Richard Palethorpe

Hello,

Quoted 8 lines · Alessandro Carminati

mount_root() is the kernel function responsible for mounting the primary rootfs.
A dynamic there, prevents the /dev/root device node in the not yet mounted files system. For this reason, in the embedded system that starts without an initramfs, or however a proper initscript, the /dev/root device appears into the mount table in the / line.
The test tries to open this /dev/root and fails with a warning. This patch aims to fix this situation.

Thanks I probably would have hit this issue sooner or later.

Quoted 13 lines

typo fixes

---
 lib/tst_device.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
lib/tst_device.c +0 -0
index 8419b80c3..c3427eb31 100644

lib/tst_device.c void tst_find_backing_dev(const char *path, char *dev) · line 530

Quoted 5 lines
lib/tst_device.c +2 -0
@@ -526,6 +526,8 @@ void tst_find_backing_dev(const char *path, char *dev)
 	unsigned int dev_major, dev_minor, line_mjr, line_mnr;
 	unsigned int len, best_match_len = 1;
 	char mnt_point[PATH_MAX];
+	char tmpbuf1[PATH_MAX];
+	char tmpbuf2[PATH_MAX];

It would be more readable to use three buffers and give them meaningful names.

lib/tst_device.c void tst_find_backing_dev(const char *path, char *dev) · line 567

Quoted 8 lines
lib/tst_device.c +1 -0
 	if (stat(path, &buf) < 0)
 		tst_brkm(TWARN | TERRNO, NULL, "stat() failed");
@@ -562,6 +564,24 @@ void tst_find_backing_dev(const char *path, char *dev)
 	if (!*dev)
 		tst_brkm(TBROK, NULL, "Cannot find block device for %s", path);
 
+	if (stat(dev, &buf) < 0) {

As there is no harm in calling stat twice; could just do

if (stat(dev, &buf) < 0) && strcmp("/dev/root", dev) == 0) {

Or even replace with strcmp with errno == ENOENT and use this as a general fallback.

lib/tst_device.c void tst_find_backing_dev(const char *path, char *dev) · line 570

Quoted 3 lines
lib/tst_device.c +3 -0
+		if (strcmp("/dev/root", dev) != 0) {
+			tst_brkm(TWARN | TERRNO, NULL, "stat(%s) failed", dev);
+		} else {

Then remove this if statement

Quoted 20 lines
lib/tst_device.c +14 -0
+			sprintf(tmpbuf1, "/sys/dev/block/%d:%d/uevent",
 dev_major, dev_minor);
+			file = SAFE_FOPEN(NULL, tmpbuf1, "r");
+			while (fgets(line, sizeof(line), file)) {
+				if (sscanf(line, "%[^=]=%s", tmpbuf1, tmpbuf2) != 2)
+					continue;
+				if (strcmp("DEVNAME", tmpbuf1) == 0) {
+					sprintf(dev, "/dev/%s", tmpbuf2);
+					break;
+				}
+			}
+			SAFE_FCLOSE(NULL, file);
+		}
+	}
+
 	if (stat(dev, &buf) < 0)
 		tst_brkm(TWARN | TERRNO, NULL, "stat(%s) failed", dev);
Signature · 2 lines
-- 
2.34.1
Signature · 2 lines
Thank you,
Richard.
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#3 lore raw

PATCH 1/1 tst_find_backing_dev: Get dev name from /sys/dev/block/*/uevent

mountinfo doesn't always contain the correct device name. For example /dev/root may be displayed, but not exist in devtmpfs[1].

The unevent file in sysfs is another way of finding the device name from the major and minor numbers. Possibly it always displays the proper device name.

One caveat is the sysfs can be disabled, so this commit does not remove the mountinfo method altogether, but leaves it as a fallback.

Alessandro Carminati originally sent two patches[1] which added the uevent file method as a fallback. However it seems like the better method.

[1]: https://lore.kernel.org/ltp/[email protected]/

---

Alessandro, it seems you tried to edit the last patch by hand? In any case it did not apply and I ended up making some other changes. So I took the liberty of submitting a new patch.

Thanks,
Richard.

 lib/tst_device.c | 20 ++++++++++++++++++--
 1 file changed, 18 insertions(+), 2 deletions(-)
lib/tst_device.c +18 -2
index 8419b80c3..676903fff 100644
@@ -526,14 +526,30 @@ void tst_find_backing_dev(const char *path, char *dev)
 	unsigned int dev_major, dev_minor, line_mjr, line_mnr;
 	unsigned int len, best_match_len = 1;
 	char mnt_point[PATH_MAX];
+	char uevent_path[PATH_MAX];
+	char dev_name[NAME_MAX];
 
 	if (stat(path, &buf) < 0)
 		tst_brkm(TWARN | TERRNO, NULL, "stat() failed");
 
+	*dev = '\0';
 	dev_major = major(buf.st_dev);
 	dev_minor = minor(buf.st_dev);
+
+	sprintf(uevent_path,
+		"/sys/dev/block/%d:%d/uevent", dev_major, dev_minor);
+
+	if (!access(uevent_path, R_OK)) {
+		FILE_LINES_SCANF(NULL, uevent_path, "DEVNAME=%s", dev_name);
+
+		if (dev_name[0])
+			sprintf(dev, "/dev/%s", dev_name);
+	}
+
+	if (!stat(dev, &buf))
+		goto out;
+
 	file = SAFE_FOPEN(NULL, "/proc/self/mountinfo", "r");
-	*dev = '\0';
 
 	while (fgets(line, sizeof(line), file)) {
 		if (sscanf(line, "%*d %*d %d:%d %*s %s",
@@ -564,7 +580,7 @@ void tst_find_backing_dev(const char *path, char *dev)
 
 	if (stat(dev, &buf) < 0)
 		tst_brkm(TWARN | TERRNO, NULL, "stat(%s) failed", dev);
-
+out:
 	if (S_ISBLK(buf.st_mode) != 1)
 		tst_brkm(TCONF, NULL, "dev(%s) isn't a block dev", dev);
 }
Signature · 5 lines
2.36.1


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#4 lore raw

Re: [LTP] [PATCH] tst_find_backing_dev: Get dev name from /sys/dev/block/*/uevent

Hello,

Thank you this new version of the patch.
in this moment doc/c-test-api.txt is out of sync with the code, I can handle its fix.

Regards
Alessandro

Quoted 82 lines · On Tue, Oct 25, 2022, 16:59 Richard Palethorpe <[email protected]> wrote:

mountinfo doesn't always contain the correct device name. For example /dev/root may be displayed, but not exist in devtmpfs[1].

The unevent file in sysfs is another way of finding the device name from the major and minor numbers. Possibly it always displays the proper device name.

One caveat is the sysfs can be disabled, so this commit does not remove the mountinfo method altogether, but leaves it as a fallback.

Alessandro Carminati originally sent two patches[1] which added the uevent file method as a fallback. However it seems like the better method.

[1]: https://lore.kernel.org/ltp/[email protected]/

---

Alessandro, it seems you tried to edit the last patch by hand? In any case it did not apply and I ended up making some other changes. So I took the liberty of submitting a new patch.

Thanks,
Richard.

 lib/tst_device.c | 20 ++++++++++++++++++--
 1 file changed, 18 insertions(+), 2 deletions(-)
lib/tst_device.c +17 -1
index 8419b80c3..676903fff 100644
@@ -526,14 +526,30 @@ void tst_find_backing_dev(const char *path, char
 *dev)
        unsigned int dev_major, dev_minor, line_mjr, line_mnr;
        unsigned int len, best_match_len = 1;
        char mnt_point[PATH_MAX];
+       char uevent_path[PATH_MAX];
+       char dev_name[NAME_MAX];
 
        if (stat(path, &buf) < 0)
                tst_brkm(TWARN | TERRNO, NULL, "stat() failed");
 
+       *dev = '\0';
        dev_major = major(buf.st_dev);
        dev_minor = minor(buf.st_dev);
+
+       sprintf(uevent_path,
+               "/sys/dev/block/%d:%d/uevent", dev_major, dev_minor);
+
+       if (!access(uevent_path, R_OK)) {
+               FILE_LINES_SCANF(NULL, uevent_path, "DEVNAME=%s",
 dev_name);
+
+               if (dev_name[0])
+                       sprintf(dev, "/dev/%s", dev_name);
+       }
+
+       if (!stat(dev, &buf))
+               goto out;
+
        file = SAFE_FOPEN(NULL, "/proc/self/mountinfo", "r");
-       *dev = '\0';
 
        while (fgets(line, sizeof(line), file)) {
                if (sscanf(line, "%*d %*d %d:%d %*s %s",
lib/tst_device.c +1 -2
@@ -564,7 +580,7 @@ void tst_find_backing_dev(const char *path, char *dev)
 
        if (stat(dev, &buf) < 0)
                tst_brkm(TWARN | TERRNO, NULL, "stat(%s) failed", dev);
-
+out:
        if (S_ISBLK(buf.st_mode) != 1)
                tst_brkm(TCONF, NULL, "dev(%s) isn't a block dev", dev);
 }
--

2.36.1

Signature · 2 lines
-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Acked-by on v1 1/1
#5 lore raw

Re: [LTP] [PATCH] tst_find_backing_dev: Get dev name from /sys/dev/block/*/uevent

In reply to Richard Palethorpe via ltp
Quoted 5 lines · Richard Palethorpe

mountinfo doesn't always contain the correct device name. For example /dev/root may be displayed, but not exist in devtmpfs[1].

The unevent file in sysfs is another way of finding the device name

^^ small typo here

Quoted 2 lines

from the major and minor numbers. Possibly it always displays the proper device name.

major/minor numbers may still fail for btrfs, but that issue exists outside of scope of this patch.

Quoted 3 lines

One caveat is the sysfs can be disabled, so this commit does not remove the mountinfo method altogether, but leaves it as a fallback.

Could we add some TINFO message to output, so we know where dev_name came from? (uevent vs mountinfo)

Quoted 10 lines

Alessandro Carminati originally sent two patches[1] which added the uevent file method as a fallback. However it seems like the better method.

[1]: https://lore.kernel.org/ltp/[email protected]/

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#6 lore raw

Re: [LTP] [PATCH] tst_find_backing_dev: Get dev name from /sys/dev/block/*/uevent

Hello,

Just proposed a new patch version for the same topic
https://lore.kernel.org/ltp/[email protected]/

cheers
Alessandro Carminati

Il giorno mer 26 ott 2022 alle ore 12:56 Jan Stancek <[email protected]> ha scritto:

Quoted 36 lines
Quoted 5 lines · Richard Palethorpe

mountinfo doesn't always contain the correct device name. For example /dev/root may be displayed, but not exist in devtmpfs[1].

The unevent file in sysfs is another way of finding the device name

^^ small typo here

Quoted 2 lines

from the major and minor numbers. Possibly it always displays the proper device name.

major/minor numbers may still fail for btrfs, but that issue exists outside of scope of this patch.

Quoted 3 lines

One caveat is the sysfs can be disabled, so this commit does not remove the mountinfo method altogether, but leaves it as a fallback.

Could we add some TINFO message to output, so we know where dev_name came from? (uevent vs mountinfo)

Quoted 10 lines

Alessandro Carminati originally sent two patches[1] which added the uevent file method as a fallback. However it seems like the better method.

[1]: https://lore.kernel.org/ltp/[email protected]/

Signature · 2 lines
-- 
Mailing list info: https://lists.linux.it/listinfo/ltp