[LTP] [PATCH v5] readahead02.c: Fix check input fsize

Wei Gao wegao@suse.com
Wed Jan 18 04:44:09 CET 2023


We run the test with a loop device it will fail with ENOSPC if we
pass -s bigger than the loop device, we should at least check if
the device is large enough in the test setup.The test should make
use of use tst_parse_filesize() so that we can pass sizes with
units e.g. -s 128M.

Signed-off-by: Wei Gao <wegao@suse.com>
Suggested-by: Cyril Hrubis <chrubis@suse.cz>
---
 .../kernel/syscalls/readahead/readahead02.c   | 64 ++++++++++++-------
 1 file changed, 40 insertions(+), 24 deletions(-)

diff --git a/testcases/kernel/syscalls/readahead/readahead02.c b/testcases/kernel/syscalls/readahead/readahead02.c
index 7acf4bb18..4770f55e7 100644
--- a/testcases/kernel/syscalls/readahead/readahead02.c
+++ b/testcases/kernel/syscalls/readahead/readahead02.c
@@ -40,10 +40,10 @@ static char testfile[PATH_MAX] = "testfile";
 #define PROC_IO_FNAME "/proc/self/io"
 #define DEFAULT_FILESIZE (64 * 1024 * 1024)
 
-static size_t testfile_size = DEFAULT_FILESIZE;
+static long long testfile_size = DEFAULT_FILESIZE;
 static char *opt_fsizestr;
 static int pagesize;
-static unsigned long cached_max;
+static long long cached_max;
 static int ovl_mounted;
 static int readahead_length  = 4096;
 static char sys_bdi_ra_path[PATH_MAX];
@@ -51,12 +51,12 @@ static int orig_bdi_limit;
 
 static const char mntpoint[] = OVL_BASE_MNTPOINT;
 
-static int libc_readahead(int fd, off_t offset, size_t len)
+static int libc_readahead(int fd, off_t offset, long long len)
 {
 	return readahead(fd, offset, len);
 }
 
-static int fadvise_willneed(int fd, off_t offset, size_t len)
+static int fadvise_willneed(int fd, off_t offset, long long len)
 {
 	/* Should have the same effect as readahead() syscall */
 	errno = posix_fadvise(fd, offset, len, POSIX_FADV_WILLNEED);
@@ -69,7 +69,7 @@ static struct tcase {
 	int use_overlay;
 	int use_fadvise;
 	/* Use either readahead() syscall or POSIX_FADV_WILLNEED */
-	int (*readahead)(int fd, off_t offset, size_t len);
+	int (*readahead)(int fd, off_t offset, long long len);
 } tcases[] = {
 	{ "readahead on file", 0, 0, libc_readahead },
 	{ "readahead on overlayfs file", 1, 0, libc_readahead },
@@ -121,11 +121,11 @@ static void create_testfile(int use_overlay)
 {
 	int fd;
 	char *tmp;
-	size_t i;
+	long long i;
 
 	sprintf(testfile, "%s/testfile",
 		use_overlay ? OVL_MNT : OVL_BASE_MNTPOINT);
-	tst_res(TINFO, "creating test file of size: %zu", testfile_size);
+	tst_res(TINFO, "creating test file of size: %lli", testfile_size);
 	tmp = SAFE_MALLOC(pagesize);
 
 	/* round to page size */
@@ -151,12 +151,12 @@ static void create_testfile(int use_overlay)
  * @cached: returns cached kB from /proc/meminfo
  */
 static int read_testfile(struct tcase *tc, int do_readahead,
-			 const char *fname, size_t fsize,
-			 unsigned long *read_bytes, long long *usec,
-			 unsigned long *cached)
+			 const char *fname, long long fsize,
+			 long long *read_bytes, long long *usec,
+			 long long *cached)
 {
 	int fd;
-	size_t i = 0;
+	long long i = 0;
 	long read_bytes_start;
 	unsigned char *p, tmp;
 	off_t offset = 0;
@@ -173,8 +173,8 @@ static int read_testfile(struct tcase *tc, int do_readahead,
 
 			i++;
 			offset += readahead_length;
-		} while ((size_t)offset < fsize);
-		tst_res(TINFO, "readahead calls made: %zu", i);
+		} while ((long long)offset < fsize);
+		tst_res(TINFO, "readahead calls made: %lli", i);
 		*cached = get_cached_size();
 
 		/* offset of file shouldn't change after readahead */
@@ -214,9 +214,9 @@ static int read_testfile(struct tcase *tc, int do_readahead,
 
 static void test_readahead(unsigned int n)
 {
-	unsigned long read_bytes, read_bytes_ra;
+	long long read_bytes, read_bytes_ra;
 	long long usec, usec_ra;
-	unsigned long cached_high, cached_low, cached, cached_ra;
+	long long cached_high, cached_low, cached, cached_ra;
 	int ret;
 	struct tcase *tc = &tcases[n];
 
@@ -286,8 +286,8 @@ static void test_readahead(unsigned int n)
 	tst_res(TINFO, "read_testfile(0) took: %lli usec", usec);
 	tst_res(TINFO, "read_testfile(1) took: %lli usec", usec_ra);
 	if (has_file(PROC_IO_FNAME, 0)) {
-		tst_res(TINFO, "read_testfile(0) read: %ld bytes", read_bytes);
-		tst_res(TINFO, "read_testfile(1) read: %ld bytes",
+		tst_res(TINFO, "read_testfile(0) read: %lli bytes", read_bytes);
+		tst_res(TINFO, "read_testfile(1) read: %lli bytes",
 			read_bytes_ra);
 		/* actual number of read bytes depends on total RAM */
 		if (read_bytes_ra < read_bytes)
@@ -299,9 +299,9 @@ static void test_readahead(unsigned int n)
 			" unable to determine read bytes during test");
 	}
 
-	tst_res(TINFO, "cache can hold at least: %ld kB", cached_max);
-	tst_res(TINFO, "read_testfile(0) used cache: %ld kB", cached);
-	tst_res(TINFO, "read_testfile(1) used cache: %ld kB", cached_ra);
+	tst_res(TINFO, "cache can hold at least: %lli kB", cached_max);
+	tst_res(TINFO, "read_testfile(0) used cache: %lli kB", cached);
+	tst_res(TINFO, "read_testfile(1) used cache: %lli kB", cached_ra);
 
 	if (cached_max * 1024 >= testfile_size) {
 		/*
@@ -366,11 +366,28 @@ static void setup_readahead_length(void)
 
 static void setup(void)
 {
-	if (opt_fsizestr) {
-		testfile_size = SAFE_STRTOL(opt_fsizestr, 1, INT_MAX);
-		tst_set_max_runtime(1 + testfile_size / (DEFAULT_FILESIZE/32));
+	/*
+	 * Acutaly dev size will reduced after create filesystem, for example
+	 * system set default dev size is 300M but acutal dev size is 280M
+	 * "df -h" example resulst in case dev size = 300M:
+	 * /dev/loop1      280M  272M     0 100% /tmp/LTP_reaNP2ElW/mntpoint
+	 * Around 7% space loss so we use dev_szie * 0.9 as dev real usage size
+	 * Test case will create two files within dev so we need div 2 to get
+	 * max file size
+	 */
+
+	long long fsize_max = tst_device->size * 0.9 / 2 * 1024 * 1024;
+
+	/* At least two pagesize for test case */
+	pagesize = getpagesize();
+	long long fsize_min = pagesize * 2;
+
+	if (tst_parse_filesize(opt_fsizestr, &testfile_size, fsize_min, fsize_max)) {
+		tst_brk(TBROK, "invalid input filesize '%s'", opt_fsizestr);
 	}
 
+	tst_set_max_runtime(1 + testfile_size / (DEFAULT_FILESIZE/32));
+
 	if (access(PROC_IO_FNAME, F_OK))
 		tst_brk(TCONF, "Requires " PROC_IO_FNAME);
 
@@ -380,7 +397,6 @@ static void setup(void)
 	/* check if readahead is supported */
 	tst_syscall(__NR_readahead, 0, 0, 0);
 
-	pagesize = getpagesize();
 
 	setup_readahead_length();
 	tst_res(TINFO, "readahead length: %d", readahead_length);
-- 
2.35.3



More information about the ltp mailing list