Skip to contentSkip to conversation

tst_device: Scan /sys/block/* for stat file

PATCH New Cyril Hrubis 3 participants 5 messages Jan 8, 2020 – Jan 15, 2020 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tst_device: Scan /sys/block/* for stat file

The current tst_dev_bytes_written() function works only for simple cases where the block device is not divided into partitions. This patch fixes that scannning the sysfiles for pattern /sys/block/*/devname/stat.

---
 lib/tst_device.c | 33 ++++++++++++++++++++++++++++-----
 1 file changed, 28 insertions(+), 5 deletions(-)
lib/tst_device.c +28 -5
index 10f71901d..aca769559 100644
@@ -373,16 +373,39 @@ int tst_umount(const char *path)
 	return -1;
 }
 
+int find_stat_file(const char *dev, char *path, size_t path_len)
+{
+	const char *devname = strrchr(dev, '/') + 1;
+
+	snprintf(path, path_len, "/sys/block/%s/stat", devname);
+
+	if (!access(path, F_OK))
+		return 1;
+
+	DIR *dir = SAFE_OPENDIR(NULL, "/sys/block/");
+	struct dirent *ent;
+
+	while ((ent = readdir(dir))) {
+		snprintf(path, path_len, "/sys/block/%s/%s/stat", ent->d_name, devname);
+
+		fprintf(stderr, "%s\n", path);
+
+		if (!access(path, F_OK)) {
+			SAFE_CLOSEDIR(NULL, dir);
+			return 1;
+		}
+	}
+
+	SAFE_CLOSEDIR(NULL, dir);
+	return 0;
+}
+
 unsigned long tst_dev_bytes_written(const char *dev)
 {
-	struct stat st;
 	unsigned long dev_sec_write = 0, dev_bytes_written, io_ticks = 0;
 	char dev_stat_path[1024];
 
-	snprintf(dev_stat_path, sizeof(dev_stat_path), "/sys/block/%s/stat",
-		 strrchr(dev, '/') + 1);
-
-	if (stat(dev_stat_path, &st) != 0)
+	if (!find_stat_file(dev, dev_stat_path, sizeof(dev_stat_path)))
 		tst_brkm(TCONF, NULL, "Test device stat file: %s not found",
 			 dev_stat_path);
 
Signature · 1 line
2.23.0
#1 lore raw

PATCH 1/1 tst_device: Scan /sys/block/* for stat file

Hi

Quoted 26 lines

The current tst_dev_bytes_written() function works only for simple cases where the block device is not divided into partitions. This patch fixes that scannning the sysfiles for pattern /sys/block/*/devname/stat.

---
  lib/tst_device.c | 33 ++++++++++++++++++++++++++++-----
  1 file changed, 28 insertions(+), 5 deletions(-)
lib/tst_device.c +7 -0
index 10f71901d..aca769559 100644
@@ -373,16 +373,39 @@ int tst_umount(const char *path)
  	return -1;
  }
 
+int find_stat_file(const char *dev, char *path, size_t path_len)
+{
+	const char *devname = strrchr(dev, '/') + 1;
+
+	snprintf(path, path_len, "/sys/block/%s/stat", devname);
+
+	if (!access(path, F_OK))

lib/tst_device.c int tst_umount(const char *path) · line 392

Quoted 10 lines
lib/tst_device.c +10 -0
+		return 1;
+
+	DIR *dir = SAFE_OPENDIR(NULL, "/sys/block/");
+	struct dirent *ent;
+
+	while ((ent = readdir(dir))) {
+		snprintf(path, path_len, "/sys/block/%s/%s/stat", ent->d_name, devname);
+
+		fprintf(stderr, "%s\n", path);
+

It will make many noise when using .all_filesystem and we can remove it. Other than, it looks good to me.

Quoted 25 lines
lib/tst_device.c +11 -5
+		if (!access(path, F_OK)) {
+			SAFE_CLOSEDIR(NULL, dir);
+			return 1;
+		}
+	}
+
+	SAFE_CLOSEDIR(NULL, dir);
+	return 0;
+}
+
  unsigned long tst_dev_bytes_written(const char *dev)
  {
-	struct stat st;
  	unsigned long dev_sec_write = 0, dev_bytes_written, io_ticks = 0;
  	char dev_stat_path[1024];
 
-	snprintf(dev_stat_path, sizeof(dev_stat_path), "/sys/block/%s/stat",
-		 strrchr(dev, '/') + 1);
-
-	if (stat(dev_stat_path, &st) != 0)
+	if (!find_stat_file(dev, dev_stat_path, sizeof(dev_stat_path)))
  		tst_brkm(TCONF, NULL, "Test device stat file: %s not found",
  			 dev_stat_path);
 
#2 lore raw

PATCH 1/1 tst_device: Scan /sys/block/* for stat file

Hi!

Quoted 30 lines
Quoted 30 lines

The current tst_dev_bytes_written() function works only for simple cases where the block device is not divided into partitions. This patch fixes that scannning the sysfiles for pattern /sys/block/*/devname/stat.

---
  lib/tst_device.c | 33 ++++++++++++++++++++++++++++-----
  1 file changed, 28 insertions(+), 5 deletions(-)
lib/tst_device.c +11 -0
index 10f71901d..aca769559 100644
@@ -373,16 +373,39 @@ int tst_umount(const char *path)
  	return -1;
  }
 
+int find_stat_file(const char *dev, char *path, size_t path_len)
+{
+	const char *devname = strrchr(dev, '/') + 1;
+
+	snprintf(path, path_len, "/sys/block/%s/stat", devname);
+
+	if (!access(path, F_OK))
+		return 1;
+
+	DIR *dir = SAFE_OPENDIR(NULL, "/sys/block/");
+	struct dirent *ent;
Quoted 8 lines

lib/tst_device.c int tst_umount(const char *path) · line 392

Quoted 6 lines
lib/tst_device.c +6 -0
+
+	while ((ent = readdir(dir))) {
+		snprintf(path, path_len, "/sys/block/%s/%s/stat", ent->d_name, devname);
+
+		fprintf(stderr, "%s\n", path);
+

It will make many noise when using .all_filesystem and we can remove it. Other than, it looks good to me.

That's forgotten debug print, I should have removed that before sending.

Signature · 2 lines
Cyril Hrubis
[email protected]
#3 lore raw

PATCH 1/1 tst_device: Scan /sys/block/* for stat file

Hi,

...

Quoted 20 lines
Quoted 11 lines
Quoted 11 lines
lib/tst_device.c +11 -0
+int find_stat_file(const char *dev, char *path, size_t path_len)
+{
+	const char *devname = strrchr(dev, '/') + 1;
+
+	snprintf(path, path_len, "/sys/block/%s/stat", devname);
+
+	if (!access(path, F_OK))
+		return 1;
+
+	DIR *dir = SAFE_OPENDIR(NULL, "/sys/block/");
+	struct dirent *ent;
Quoted 8 lines

lib/tst_device.c int tst_umount(const char *path) · line 392

Quoted 6 lines
lib/tst_device.c +6 -0
+
+	while ((ent = readdir(dir))) {
+		snprintf(path, path_len, "/sys/block/%s/%s/stat", ent->d_name, devname);
+
+		fprintf(stderr, "%s\n", path);
+

It will make many noise when using .all_filesystem and we can remove it. Other than, it looks good to me.

Quoted 1 line

That's forgotten debug print, I should have removed that before sending.

Kind regards,
Petr

#4 lore raw

PATCH 1/1 tst_device: Scan /sys/block/* for stat file

Hi!

Quoted 3 lines
Quoted 1 line

That's forgotten debug print, I should have removed that before sending.

Pushed with the forgotten debug print removed.

Signature · 2 lines
Cyril Hrubis
[email protected]