xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/fsync/fsync02.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) Wayne Boyer, International Business Machines  Corp., 2001
4  * Copyright (c) 2019 SUSE LLC, Jozef Pupava <[email protected]>
5  */
6 
7 /*
8  * Test Description:
9  *  Test fsync() return value on test file
10  *  fsync() has to finish within TIME_LIMIT.
11  */
12 
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <sys/types.h>
16 #include <sys/statvfs.h>
17 #include <fcntl.h>
18 #include <sys/resource.h>
19 #include <time.h>
20 #include "tst_test.h"
21 
22 #define BLOCKSIZE 8192
23 #define MAXBLKS 65536
24 #define BUF_SIZE 2048
25 
26 char tempfile[40] = "";
27 char pbuf[BUF_SIZE];
28 int fd;
29 off_t max_blks = MAXBLKS;
30 int time_limit = 120;
31 
32 struct statvfs stat_buf;
33 
setup(void)34 static void setup(void) {
35 	/* free blocks avail to non-superuser */
36 	unsigned long f_bavail;
37 
38 	if (tst_is_virt(VIRT_ANY)) {
39 		tst_res(TINFO, "Running in a VM, multiply the time_limit by 2");
40 		time_limit *= 2;
41 	}
42 
43 	fd = SAFE_OPEN("tempfile", O_RDWR | O_CREAT | O_TRUNC, 0777);
44 
45 	if (fstatvfs(fd, &stat_buf) != 0) {
46 		tst_brk(TBROK, "fstatvfs failed");
47 	}
48 
49 	f_bavail = (stat_buf.f_bavail * stat_buf.f_bsize) / BLOCKSIZE;
50 	if (f_bavail && (f_bavail < MAXBLKS)) {
51 		max_blks = f_bavail;
52 	}
53 
54 #ifdef LARGEFILE
55 	SAFE_FCNTL(fd, F_SETFL, O_LARGEFILE);
56 	SAFE_WRITE(SAFE_WRITE_ALL, fd, pbuf, BUF_SIZE);
57 #endif
58 }
59 
run(void)60 static void run(void) {
61 	off_t offset;
62 	int i;
63 	int max_block = 0;
64 	int data_blocks = 0;
65 	time_t time_start, time_end;
66 	double time_delta;
67 	long int random_number;
68 
69 	random_number = rand();
70 	max_block = random_number % max_blks + 1;
71 	data_blocks = random_number % max_block;
72 
73 	for (i = 1; i <= data_blocks; i++) {
74 		offset = i * ((BLOCKSIZE * max_block) / data_blocks);
75 		offset -= BUF_SIZE;
76 		SAFE_LSEEK(fd, offset, SEEK_SET);
77 		SAFE_WRITE(SAFE_WRITE_ALL, fd, pbuf, BUF_SIZE);
78 	}
79 	time_start = time(0);
80 
81 	TEST(fsync(fd));
82 
83 	time_end = time(0);
84 
85 	if (time_end == -1) {
86 		tst_res(TFAIL | TTERRNO, "getting end time failed");
87 	} else if (TST_RET == -1) {
88 		tst_res(TFAIL | TTERRNO, "fsync failed");
89 	} else if (TST_RET != 0) {
90 		tst_res(TFAIL | TTERRNO,
91 		"fsync failed with unexpected return value");
92 	} else if (time_end < time_start) {
93 		tst_res(TFAIL,
94 		"timer broken end %ld < start %ld",
95 		time_end, time_start);
96 	} else if ((time_delta =
97 		difftime(time_end, time_start)) > time_limit) {
98 		tst_res(TFAIL,
99 		"fsync took too long: %lf seconds; "
100 		"max_block: %d; data_blocks: %d",
101 		time_delta, max_block, data_blocks);
102 	} else {
103 		tst_res(TPASS,
104 		"fsync succeeded in an acceptable amount of time");
105 	}
106 	SAFE_FTRUNCATE(fd, 0);
107 }
108 
cleanup(void)109 static void cleanup(void) {
110 	SAFE_CLOSE(fd);
111 }
112 
113 static struct tst_test test = {
114 	.test_all = run,
115 	.setup = setup,
116 	.cleanup = cleanup,
117 	.needs_tmpdir = 1,
118 	.max_runtime = 300,
119 };
120