Skip to contentSkip to conversation

Add tst_dev_block_size utility

PATCH In review Andrea Cervesato 2 participants 2 messages Feb 1, 2022 Library code

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 Add tst_dev_block_size utility

Added tst_dev_block_size function in tst_device.c and updated c-test-api.txt documentation.

---
In v3 there are mostly some documentation fixes and small fix in unittesting.

 doc/c-test-api.txt            | 11 ++++
 include/tst_device.h          |  9 +++-
 lib/newlib_tests/tst_device.c | 99 ++++++++++++++++++++++++++++-------
 lib/tst_device.c              | 15 ++++++
 4 files changed, 113 insertions(+), 21 deletions(-)
doc/c-test-api.txt +11 -0
index 123d3f1fc..c11404879 100644
@@ -1034,6 +1034,17 @@ uint64_t tst_get_device_size(const char *dev_path);
 This function gets size of the given block device, it checks the 'dev_path' is
 valid first, if yes, return the size in MB, otherwise return -1.
 
+[source,c]
+-------------------------------------------------------------------------------
+#include "tst_test.h"
+
+int tst_dev_block_size(const char *path);
+-------------------------------------------------------------------------------
+
+This function returns the physical device block size for the specific `path`.
+It finds the device where `path` is located and then uses `ioctl` (BLKSSZGET)
+to get a physical device block size.
+
 1.16 Formatting a device with a filesystem
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
include/tst_device.h +8 -1
index 72c560c02..95ccd453e 100644
@@ -112,4 +112,11 @@ void tst_purge_dir(const char *path);
  */
 void tst_find_backing_dev(const char *path, char *dev);
 
-#endif	/* TST_DEVICE_H__ */
+/*
+ * Returns the size of a physical device block size for the specific path
+ * @path   Path to find the block size
+ * @return Size of the block size
+ */
+int tst_dev_block_size(const char *path);
+
+#endif /* TST_DEVICE_H__ */
lib/newlib_tests/tst_device.c +79 -20

126 lines not on this page · raw message

lib/tst_device.c +15 -0
index 73e70d26e..1ef667fa0 100644
@@ -547,3 +547,18 @@ void tst_find_backing_dev(const char *path, char *dev)
 	if (S_ISBLK(buf.st_mode) != 1)
 		tst_brkm(TCONF, NULL, "dev(%s) isn't a block dev", dev);
 }
+
+int tst_dev_block_size(const char *path)
+{
+	int fd;
+	int size;
+	char dev_name[1024];
+
+	tst_find_backing_dev(path, dev_name);
+
+	fd = SAFE_OPEN(NULL, dev_name, O_RDONLY);
+	SAFE_IOCTL(NULL, fd, BLKSSZGET, &size);
+	SAFE_CLOSE(NULL, fd);
+
+	return size;
+}
Signature · 5 lines
2.34.1


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

Hi!
Pushed with a few fixes for the test, thanks.

Actually the LOOP_SET_BLOCK_SIZE ioctl() may fail with EAGAIN if there are dirty blocks on the loop device and as a matter of the fact it did fail for me in a few cases when I executed the test several times. I guess that it all boils down to how the backing file is preallocated, and there are several different options for that...

Anyways I fixed that part by adding the LTP retry wrapper around the ioctl() which makes the test succeed repeatedly.

+ removed the mode for SAFE_OPEN() which is useless without O_CREAT
+ removed needs_tmpdir which is implied by needs_device

Signature · 5 lines
Cyril Hrubis
[email protected]

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp