[LTP] [PATCH v2 1/2] fallocate01: Convert to new API
Cyril Hrubis
chrubis@suse.cz
Wed Aug 26 13:00:17 CEST 2026
Hi!
> -int main(int ac, char **av)
> +static void run(unsigned int n)
> {
> - loff_t expected_size;
> - int lc;
> -
> - tst_parse_opts(ac, av, NULL, NULL);
> + struct tcase *tc = &tcases[n];
> + struct stat file_stat;
> + loff_t offset, len, pos, write_offset, expected_size;
>
> - setup();
> + /* Reset the backing file to a pristine 12-block state per run. */
> + SAFE_FTRUNCATE(fd, 0);
> + SAFE_LSEEK(fd, 0, SEEK_SET);
> + populate_file();
>
> - for (lc = 0; TEST_LOOPING(lc); lc++) {
> - tst_count = 0;
> + offset = SAFE_LSEEK(fd, 0, SEEK_END);
> + len = block_size;
> + expected_size = (loff_t)tc->expected_blocks * block_size;
>
> - expected_size = BLOCKS_WRITTEN * block_size + block_size;
> - runtest(0, fd_mode1, expected_size);
> + TEST(fallocate(fd, tc->mode, offset, len));
> + if (TST_RET != 0) {
> + if (TST_ERR == EOPNOTSUPP || TST_ERR == ENOSYS)
> + tst_brk(TCONF, "fallocate() not supported");
>
> - expected_size = BLOCKS_WRITTEN * block_size;
> - runtest(FALLOC_FL_KEEP_SIZE, fd_mode2, expected_size);
> + tst_res(TFAIL | TTERRNO, "fallocate(%s, %lld, %lld) failed",
> + tc->desc, (long long)offset, (long long)len);
> + return;
> }
> + tst_res(TPASS, "fallocate(%s, %lld, %lld) succeeded",
> + tc->desc, (long long)offset, (long long)len);
Can we do a dummy call to fallocate in the test setup and check for the
EOPNOTSUPP and ENOSYS so that we can use TST_EXP_PASS() here instead?
> - cleanup();
> - tst_exit();
> -}
> -
> -/*****************************************************************************
> - * Calls the system call, with appropriate parameters and writes data
> - ******************************************************************************/
> -void runtest(int mode, int fd, loff_t expected_size)
> -{
> - loff_t offset;
> - loff_t len = block_size;
> - loff_t write_offset, lseek_offset;
> - offset = lseek(fd, 0, SEEK_END);
> - struct stat file_stat;
> - errno = 0;
> -
> - TEST(fallocate(fd, mode, offset, len));
> - /* check return code */
> - if (TEST_RETURN != 0) {
> - if (TEST_ERRNO == EOPNOTSUPP || TEST_ERRNO == ENOSYS) {
> - tst_brkm(TCONF, cleanup,
> - "fallocate system call is not implemented");
> - }
> - tst_resm(TFAIL | TTERRNO,
> - "fallocate(%d, %d, %" PRId64 ", %" PRId64 ") failed",
> - fd, mode, offset, len);
> - return;
> + SAFE_FSTAT(fd, &file_stat);
> + if (file_stat.st_size != expected_size) {
> + tst_res(TFAIL, "file size is %lld, expected %lld",
> + (long long)file_stat.st_size, (long long)expected_size);
> } else {
> - tst_resm(TPASS,
> - "fallocate(%d, %d, %" PRId64 ", %" PRId64
> - ") returned %ld", fd, mode, offset, len,
> - TEST_RETURN);
> + tst_res(TPASS, "file size is %lld as expected",
> + (long long)expected_size);
> }
Can we use TST_EXP_EQ macro here?
> - if (fstat(fd, &file_stat) < 0)
> - tst_resm(TFAIL | TERRNO, "fstat failed after fallocate()");
> -
> - if (file_stat.st_size != expected_size)
> - tst_resm(TFAIL | TERRNO,
> - "fstat test fails on fallocate (%d, %d, %" PRId64 ", %"
> - PRId64 ") Failed on mode", fd, mode, offset, len);
> -
> - write_offset = random() % len;
> - lseek_offset = lseek(fd, write_offset, SEEK_CUR);
> - if (lseek_offset != offset + write_offset) {
> - tst_resm(TFAIL | TERRNO,
> - "lseek fails in fallocate(%d, %d, %" PRId64 ", %"
> - PRId64 ") failed on mode", fd, mode, offset, len);
> + write_offset = len / 2;
> + pos = SAFE_LSEEK(fd, write_offset, SEEK_CUR);
> + if (pos != offset + write_offset) {
> + tst_res(TFAIL, "lseek returned %lld, expected %lld",
> + (long long)pos, (long long)(offset + write_offset));
> return;
> }
And here?
> - //Write a character to file at random location
> - TEST(write(fd, "A", 1));
> - /* check return code */
> - if (TEST_RETURN == -1) {
> - tst_resm(TFAIL | TTERRNO,
> - "write fails in fallocate(%d, %d, %" PRId64 ", %"
> - PRId64 ") failed", fd, mode, offset, len);
> - } else {
> - tst_resm(TPASS,
> - "write operation on fallocated(%d, %d, %"
> - PRId64 ", %" PRId64 ") returned %ld", fd, mode,
> - offset, len, TEST_RETURN);
> - }
> +
> + TST_EXP_POSITIVE(write(fd, "A", 1),
> + "write into the newly allocated region");
> +}
> +
> +static void cleanup(void)
> +{
> + if (fd != -1)
> + SAFE_CLOSE(fd);
> }
> +
> +static struct tst_test test = {
> + .setup = setup,
> + .cleanup = cleanup,
> + .test = run,
> + .tcnt = ARRAY_SIZE(tcases),
> + .needs_tmpdir = 1,
> +};
I guess that it would make sense to enable the test for all_filesystems
here.
--
Cyril Hrubis
chrubis@suse.cz
More information about the ltp
mailing list