Skip to contentSkip to conversation

tst_device.c: Handle Android path for backing device

PATCH Superseded Edward Liaw via ltp 2 participants 10 messages Mar 20, 2023 – Mar 24, 2023 Library code

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover tst_device.c: Handle Android path for backing device

e1b1ae66b240 ("tst_find_backing_dev: Get dev name from /sys/dev/block/*/uevent") added a hardcoded path to check for the backing device in /dev. On Android, it needs to check /dev/block.

The set_dev_path function was renamed to set_dev_loop_path and its return value was changed from 1 on success to 0 on success to be more consistent with other functions. A check was added to tst_find_free_loopdev in the event that the loop device is not ready yet, which appears to happen occasionally on Android.

v1->v2:
Changed the function signature of tst_find_backing_dev to add the length of the path string. Updated all references for this function to include the added parameter.

Edward Liaw (3):
tst_device.c: Use PATH_MAX more consistently
set_dev_loop_path: Refactor set_dev_path and check return value
tst_find_backing_dev: Also check /dev/block/ for backing device

 doc/c-test-api.txt                            |  2 +-
 include/tst_device.h                          |  3 +-
 lib/newlib_tests/tst_device.c                 |  2 +-
 lib/tst_device.c                              | 59 ++++++++++++-------
 .../kernel/syscalls/ioctl/ioctl_loop05.c      |  2 +-
 5 files changed, 42 insertions(+), 26 deletions(-)
Signature · 5 lines
2.40.0.rc1.284.g88254d51c5-goog


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

PATCH v2 1/3 tst_device.c: Use PATH_MAX more consistently

Replace hardcoded 1024 path sizes with PATH_MAX.

---
 lib/tst_device.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
lib/tst_device.c +4 -4
index 48d7e3ab6..a61c5a748 100644
@@ -50,7 +50,7 @@
 #define UUID_STR_SZ 37
 #define UUID_FMT "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x"
 
-static char dev_path[1024];
+static char dev_path[PATH_MAX];
 static int device_acquired;
 static unsigned long prev_dev_sec_write;
 
@@ -79,7 +79,7 @@ int tst_find_free_loopdev(char *path, size_t path_len)
 {
 	int ctl_fd, dev_fd, rc, i;
 	struct loop_info loopinfo;
-	char buf[1024];
+	char buf[PATH_MAX];
 
 	/* since Linux 3.1 */
 	ctl_fd = open(LOOP_CONTROL_FILE, O_RDWR);
@@ -489,7 +489,7 @@ int find_stat_file(const char *dev, char *path, size_t path_len)
 unsigned long tst_dev_bytes_written(const char *dev)
 {
 	unsigned long dev_sec_write = 0, dev_bytes_written, io_ticks = 0;
-	char dev_stat_path[1024];
+	char dev_stat_path[PATH_MAX];
 
 	if (!find_stat_file(dev, dev_stat_path, sizeof(dev_stat_path)))
 		tst_brkm(TCONF, NULL, "Test device stat file: %s not found",
@@ -641,7 +641,7 @@ int tst_dev_block_size(const char *path)
 {
 	int fd;
 	int size;
-	char dev_name[1024];
+	char dev_name[PATH_MAX];
 
 	tst_find_backing_dev(path, dev_name);
 
Signature · 5 lines
2.40.0.rc1.284.g88254d51c5-goog


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

Re: [LTP] [PATCH v2 1/3] tst_device.c: Use PATH_MAX more consistently

Hi Edward,

obviously correct, merged.

Kind regards,
Petr

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

PATCH v2 2/3 set_dev_loop_path: Refactor set_dev_path and check return value

In reply to Edward Liaw via ltp

tst_find_free_loopdev does not check the return value of set_dev_path and will return the last attempted path even if it does not pass a stat check. set_dev_path also has a return value that is not consistent with the other functions in this file.

Renames the function to set_dev_loop_path, the const array to dev_loop_variants and changes the return value to 0 on success and 1 on failure. Check the return value when called in tst_find_free_loopdev for failure to acquire a loop device.

---
 lib/tst_device.c | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)
lib/tst_device.c +9 -9
index a61c5a748..ae665f7b6 100644
@@ -54,25 +54,25 @@ static char dev_path[PATH_MAX];
 static int device_acquired;
 static unsigned long prev_dev_sec_write;
 
-static const char *dev_variants[] = {
+static const char *dev_loop_variants[] = {
 	"/dev/loop%i",
 	"/dev/loop/%i",
 	"/dev/block/loop%i"
 };
 
-static int set_dev_path(int dev, char *path, size_t path_len)
+static int set_dev_loop_path(int dev, char *path, size_t path_len)
 {
 	unsigned int i;
 	struct stat st;
 
-	for (i = 0; i < ARRAY_SIZE(dev_variants); i++) {
-		snprintf(path, path_len, dev_variants[i], dev);
+	for (i = 0; i < ARRAY_SIZE(dev_loop_variants); i++) {
+		snprintf(path, path_len, dev_loop_variants[i], dev);
 
 		if (stat(path, &st) == 0 && S_ISBLK(st.st_mode))
-			return 1;
+			return 0;
 	}
 
-	return 0;
+	return 1;
 }
 
 int tst_find_free_loopdev(char *path, size_t path_len)
@@ -88,8 +88,8 @@ int tst_find_free_loopdev(char *path, size_t path_len)
 		rc = ioctl(ctl_fd, LOOP_CTL_GET_FREE);
 		close(ctl_fd);
 		if (rc >= 0) {
-			if (path)
-				set_dev_path(rc, path, path_len);
+			if (!path || set_dev_loop_path(rc, path, path_len) != 0)
+				tst_brkm(TBROK, NULL, "Could not stat loop device %i", rc);
 			tst_resm(TINFO, "Found free device %d '%s'",
 				rc, path ?: "");
 			return rc;
@@ -116,7 +116,7 @@ int tst_find_free_loopdev(char *path, size_t path_len)
 	 */
 	for (i = 0; i < 256; i++) {
 
-		if (!set_dev_path(i, buf, sizeof(buf)))
+		if (set_dev_loop_path(i, buf, sizeof(buf)) != 0)
 			continue;
 
 		dev_fd = open(buf, O_RDONLY);
Signature · 5 lines
2.40.0.rc1.284.g88254d51c5-goog


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

Re: [LTP] [PATCH v2 2/3] set_dev_loop_path: Refactor set_dev_path and check return value

Hi Edward,

Quoted 4 lines

tst_find_free_loopdev does not check the return value of set_dev_path and will return the last attempted path even if it does not pass a stat check. set_dev_path also has a return value that is not consistent with the other functions in this file.

This change and change of return is a bit burden in loop rename changes. I'm ok it's in single patch, but it'd be more readable if it were separate.

Quoted 47 lines

Renames the function to set_dev_loop_path, the const array to dev_loop_variants and changes the return value to 0 on success and 1 on failure. Check the return value when called in tst_find_free_loopdev for failure to acquire a loop device.

---
 lib/tst_device.c | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)
lib/tst_device.c +6 -6
index a61c5a748..ae665f7b6 100644
@@ -54,25 +54,25 @@ static char dev_path[PATH_MAX];
 static int device_acquired;
 static unsigned long prev_dev_sec_write;
 
-static const char *dev_variants[] = {
+static const char *dev_loop_variants[] = {
 	"/dev/loop%i",
 	"/dev/loop/%i",
 	"/dev/block/loop%i"
 };
 
-static int set_dev_path(int dev, char *path, size_t path_len)
+static int set_dev_loop_path(int dev, char *path, size_t path_len)
 {
 	unsigned int i;
 	struct stat st;
 
-	for (i = 0; i < ARRAY_SIZE(dev_variants); i++) {
-		snprintf(path, path_len, dev_variants[i], dev);
+	for (i = 0; i < ARRAY_SIZE(dev_loop_variants); i++) {
+		snprintf(path, path_len, dev_loop_variants[i], dev);
 
 		if (stat(path, &st) == 0 && S_ISBLK(st.st_mode))
-			return 1;
+			return 0;
 	}
 
-	return 0;
+	return 1;
 }
 
 int tst_find_free_loopdev(char *path, size_t path_len)

lib/tst_device.c int tst_find_free_loopdev(char *path, size_t path_len) · line 92

Quoted 7 lines
lib/tst_device.c +2 -2
@@ -88,8 +88,8 @@ int tst_find_free_loopdev(char *path, size_t path_len)
 		rc = ioctl(ctl_fd, LOOP_CTL_GET_FREE);
 		close(ctl_fd);
 		if (rc >= 0) {
-			if (path)
-				set_dev_path(rc, path, path_len);
+			if (!path || set_dev_loop_path(rc, path, path_len) != 0)
+				tst_brkm(TBROK, NULL, "Could not stat loop device %i", rc);

set_dev_path() is going to be called only if non-NULL path (see include/tst_device.h). I haven't found a test which uses it this way, but shouldn't it be checking path, instead of !path?

	if (path && set_dev_loop_path(rc, path, path_len) != 0)

Kind regards,
Petr

Quoted 12 lines
lib/tst_device.c +1 -1
 			tst_resm(TINFO, "Found free device %d '%s'",
 				rc, path ?: "");
 			return rc;
@@ -116,7 +116,7 @@ int tst_find_free_loopdev(char *path, size_t path_len)
 	 */
 	for (i = 0; i < 256; i++) {
 
-		if (!set_dev_path(i, buf, sizeof(buf)))
+		if (set_dev_loop_path(i, buf, sizeof(buf)) != 0)
 			continue;
 
 		dev_fd = open(buf, O_RDONLY);
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#5 lore raw

Re: [LTP] [PATCH v2 2/3] set_dev_loop_path: Refactor set_dev_path and check return value

Quoted 10 lines · Petr Vorel

Hi Edward,

Quoted 4 lines

tst_find_free_loopdev does not check the return value of set_dev_path and will return the last attempted path even if it does not pass a stat check. set_dev_path also has a return value that is not consistent with the other functions in this file.

This change and change of return is a bit burden in loop rename changes. I'm ok it's in single patch, but it'd be more readable if it were separate.

Not a problem, I will split it.

Quoted 8 lines

set_dev_path() is going to be called only if non-NULL path (see include/tst_device.h). I haven't found a test which uses it this way, but shouldn't it be checking path, instead of !path?

        if (path && set_dev_loop_path(rc, path, path_len) != 0)

Kind regards,
Petr

Oops, I missed that in the comment and thought a NULL path should be checked as an error. You are right, I will change it. Also, I wasn't sure if I should be explicit with the "!= 0".

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

Re: [LTP] [PATCH v2 2/3] set_dev_loop_path: Refactor set_dev_path and check return value

Quoted 9 lines · Petr Vorel
Quoted 7 lines

Hi Edward,

Quoted 4 lines

tst_find_free_loopdev does not check the return value of set_dev_path and will return the last attempted path even if it does not pass a stat check. set_dev_path also has a return value that is not consistent with the other functions in this file.

Quoted 4 lines
Quoted 2 lines

This change and change of return is a bit burden in loop rename changes. I'm ok it's in single patch, but it'd be more readable if it were separate.

Not a problem, I will split it.

+1

Also, please rebase (I pushed some changes) which touch files you also modify.

Quoted 4 lines
Quoted 3 lines

set_dev_path() is going to be called only if non-NULL path (see include/tst_device.h). I haven't found a test which uses it this way, but shouldn't it be checking path, instead of !path?

Quoted 8 lines
Quoted 4 lines
        if (path && set_dev_loop_path(rc, path, path_len) != 0)

Kind regards,
Petr

Oops, I missed that in the comment and thought a NULL path should be checked as an error. You are right, I will change it. Also, I wasn't sure if I should be explicit with the "!= 0".

I guess we are quite ok with just "!". We try to be precise at syscalls testing (to check if the return value on error is exactly -1, not just < 0, but with normal non-testing code like this in tst_kernel.c it's not needed.

Kind regards,
Petr

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

PATCH v2 3/3 tst_find_backing_dev: Also check /dev/block/ for backing device

In reply to Edward Liaw via ltp

On Android, the backing devices are created in /dev/block/ and will not be found using the method added in e1b1ae66b240 ("tst_find_backing_dev:
Get dev name from /sys/dev/block/*/uevent"). Adds a check for /dev/block/%s as well as /dev/%s.

Modified the function signature of tst_find_backing_dev to add the length of the dev path string. Updated the documentation and code that references it.

---
 doc/c-test-api.txt                            |  2 +-
 include/tst_device.h                          |  3 +-
 lib/newlib_tests/tst_device.c                 |  2 +-
 lib/tst_device.c                              | 37 +++++++++++++------
 .../kernel/syscalls/ioctl/ioctl_loop05.c      |  2 +-
 5 files changed, 31 insertions(+), 15 deletions(-)
doc/c-test-api.txt +1 -1
index a7dd59dac..eddb5c893 100644
@@ -1087,7 +1087,7 @@ is created for that intention.
 -------------------------------------------------------------------------------
 #include "tst_test.h"
 
-void tst_find_backing_dev(const char *path, char *dev);
+void tst_find_backing_dev(const char *path, char *dev, size_t dev_len);
 -------------------------------------------------------------------------------
 
 This function finds the block dev that this path belongs to, using uevent in sysfs.
include/tst_device.h +2 -1
index 977427f1c..f0aff4473 100644
@@ -110,8 +110,9 @@ void tst_purge_dir(const char *path);
  * Find the file or path belongs to which block dev
  * @path  Path to find the backing dev
  * @dev   The block dev
+ * @dev_len   The length of the dev string
  */
-void tst_find_backing_dev(const char *path, char *dev);
+void tst_find_backing_dev(const char *path, char *dev, size_t dev_len);
 
 /*
  * Stat the device mounted on a given path.
lib/newlib_tests/tst_device.c +1 -1
index 87cec3961..53099f9bc 100644
@@ -71,7 +71,7 @@ static void test_tst_find_backing_dev(void)
 {
 	char block_dev[100];
 
-	tst_find_backing_dev(mntpoint, block_dev);
+	tst_find_backing_dev(mntpoint, block_dev, sizeof(block_dev));
 
 	if (!strcmp(tst_device->dev, block_dev))
 		tst_res(TPASS, "%s belongs to %s block dev", mntpoint,
lib/tst_device.c +26 -11
index ae665f7b6..dbeae4c03 100644
@@ -60,6 +60,11 @@ static const char *dev_loop_variants[] = {
 	"/dev/block/loop%i"
 };
 
+static const char *dev_variants[] = {
+	"/dev/%s",
+	"/dev/block/%s"
+};
+
 static int set_dev_loop_path(int dev, char *path, size_t path_len)
 {
 	unsigned int i;
@@ -75,6 +80,21 @@ static int set_dev_loop_path(int dev, char *path, size_t path_len)
 	return 1;
 }
 
+static int set_dev_path(char *dev, char *path, size_t path_len)
+{
+	unsigned int i;
+	struct stat st;
+
+	for (i = 0; i < ARRAY_SIZE(dev_variants); i++) {
+		snprintf(path, path_len, dev_variants[i], dev);
+
+		if (stat(path, &st) == 0 && S_ISBLK(st.st_mode))
+			return 0;
+	}
+
+	return 1;
+}
+
 int tst_find_free_loopdev(char *path, size_t path_len)
 {
 	int ctl_fd, dev_fd, rc, i;
@@ -511,7 +531,7 @@ unsigned long tst_dev_bytes_written(const char *dev)
 }
 
 __attribute__((nonnull))
-void tst_find_backing_dev(const char *path, char *dev)
+void tst_find_backing_dev(const char *path, char *dev, size_t dev_len)
 {
 	struct stat buf;
 	struct btrfs_ioctl_fs_info_args args = {0};
@@ -574,7 +594,7 @@ void tst_find_backing_dev(const char *path, char *dev)
 			sprintf(uevent_path, "%s/%s/uevent",
 				bdev_path, d->d_name);
 		} else {
-			tst_brkm(TBROK | TERRNO, NULL, "No backining device found while looking in %s.", bdev_path);
+			tst_brkm(TBROK | TERRNO, NULL, "No backing device found while looking in %s.", bdev_path);
 		}
 
 		if (SAFE_READDIR(NULL, dir))
@@ -590,17 +610,12 @@ void tst_find_backing_dev(const char *path, char *dev)
 	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 (!dev_name[0] || set_dev_path(dev_name, dev, dev_len) != 0)
+			tst_brkm(TBROK, NULL, "Could not stat backing device %s", dev);
+
 	} else {
 		tst_brkm(TBROK, NULL, "uevent file (%s) access failed", uevent_path);
 	}
-
-	if (stat(dev, &buf) < 0)
-		tst_brkm(TWARN | TERRNO, NULL, "stat(%s) failed", dev);
-
-	if (S_ISBLK(buf.st_mode) != 1)
-		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)
@@ -643,7 +658,7 @@ int tst_dev_block_size(const char *path)
 	int size;
 	char dev_name[PATH_MAX];
 
-	tst_find_backing_dev(path, dev_name);
+	tst_find_backing_dev(path, dev_name, sizeof(dev_name));
 
 	fd = SAFE_OPEN(NULL, dev_name, O_RDONLY);
 	SAFE_IOCTL(NULL, fd, BLKSSZGET, &size);
testcases/kernel/syscalls/ioctl/ioctl_loop05.c +1 -1
index b4427f331..3a5d5afef 100644
@@ -125,7 +125,7 @@ static void setup(void)
 	 *   needn't transform transfer.
 	 */
 	sprintf(backing_file_path, "%s/test.img", tst_get_tmpdir());
-	tst_find_backing_dev(backing_file_path, bd_path);
+	tst_find_backing_dev(backing_file_path, bd_path, sizeof(bd_path));
 	block_devfd = SAFE_OPEN(bd_path, O_RDWR);
 	SAFE_IOCTL(block_devfd, BLKSSZGET, &logical_block_size);
 	tst_res(TINFO, "backing dev(%s) logical_block_size is %d", bd_path, logical_block_size);
Signature · 5 lines
2.40.0.rc1.284.g88254d51c5-goog


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

Re: [LTP] [PATCH v2 3/3] tst_find_backing_dev: Also check /dev/block/ for backing device

Hi Edward,

Quoted 4 lines

On Android, the backing devices are created in /dev/block/ and will not be found using the method added in e1b1ae66b240 ("tst_find_backing_dev:
Get dev name from /sys/dev/block/*/uevent"). Adds a check for /dev/block/%s as well as /dev/%s.

Fixes e1b1ae66b240 ("tst_find_backing_dev: Get dev name from /sys/dev/block/*/uevent")

Kind regards,
Petr

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#9 lore raw
In reply to Edward Liaw via ltp
Quoted 3 lines

e1b1ae66b240 ("tst_find_backing_dev: Get dev name from /sys/dev/block/*/uevent") added a hardcoded path to check for the backing device in /dev. On Android, it needs to check /dev/block.

Cc the original autor.

I guess both problems were unnoticed because they are kind of corner cases (minimal Linux and Android). But sure we're happily accept patches to fix them.

Kind regards,
Petr

Quoted 22 lines

The set_dev_path function was renamed to set_dev_loop_path and its return value was changed from 1 on success to 0 on success to be more consistent with other functions. A check was added to tst_find_free_loopdev in the event that the loop device is not ready yet, which appears to happen occasionally on Android.

v1->v2:
Changed the function signature of tst_find_backing_dev to add the length of the path string. Updated all references for this function to include the added parameter.

Edward Liaw (3):
tst_device.c: Use PATH_MAX more consistently
set_dev_loop_path: Refactor set_dev_path and check return value
tst_find_backing_dev: Also check /dev/block/ for backing device

 doc/c-test-api.txt                            |  2 +-
 include/tst_device.h                          |  3 +-
 lib/newlib_tests/tst_device.c                 |  2 +-
 lib/tst_device.c                              | 59 ++++++++++++-------
 .../kernel/syscalls/ioctl/ioctl_loop05.c      |  2 +-
 5 files changed, 42 insertions(+), 26 deletions(-)
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp