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