xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/fallocate/fallocate02.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker /******************************************************************************
2*49cdfc7eSAndroid Build Coastguard Worker  *	Copyright (c) International Business Machines  Corp., 2007
3*49cdfc7eSAndroid Build Coastguard Worker  *	Author: Sharyathi Nagesh <[email protected]>
4*49cdfc7eSAndroid Build Coastguard Worker  ******************************************************************************/
5*49cdfc7eSAndroid Build Coastguard Worker 
6*49cdfc7eSAndroid Build Coastguard Worker /***************************************************************************
7*49cdfc7eSAndroid Build Coastguard Worker  * This program is free software;  you can redistribute it and/or modify
8*49cdfc7eSAndroid Build Coastguard Worker  * it under the terms of the GNU General Public License as published by
9*49cdfc7eSAndroid Build Coastguard Worker  * the Free Software Foundation; either version 2 of the License, or
10*49cdfc7eSAndroid Build Coastguard Worker  * (at your option) any later version.
11*49cdfc7eSAndroid Build Coastguard Worker  *
12*49cdfc7eSAndroid Build Coastguard Worker  * This program is distributed in the hope that it will be useful,
13*49cdfc7eSAndroid Build Coastguard Worker  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14*49cdfc7eSAndroid Build Coastguard Worker  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15*49cdfc7eSAndroid Build Coastguard Worker  * GNU Library General Public License for more details.
16*49cdfc7eSAndroid Build Coastguard Worker  *
17*49cdfc7eSAndroid Build Coastguard Worker  * You should have received a copy of the GNU General Public License
18*49cdfc7eSAndroid Build Coastguard Worker  * along with this program; if not, write to the Free Software
19*49cdfc7eSAndroid Build Coastguard Worker  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20*49cdfc7eSAndroid Build Coastguard Worker ***************************************************************************/
21*49cdfc7eSAndroid Build Coastguard Worker 
22*49cdfc7eSAndroid Build Coastguard Worker /*
23*49cdfc7eSAndroid Build Coastguard Worker  * DESCRIPTION
24*49cdfc7eSAndroid Build Coastguard Worker  *	check fallocate() with various error conditions that should produce
25*49cdfc7eSAndroid Build Coastguard Worker  *	EBADF, EINVAL and EFBIG.
26*49cdfc7eSAndroid Build Coastguard Worker  */
27*49cdfc7eSAndroid Build Coastguard Worker 
28*49cdfc7eSAndroid Build Coastguard Worker #define _GNU_SOURCE
29*49cdfc7eSAndroid Build Coastguard Worker 
30*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
31*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
32*49cdfc7eSAndroid Build Coastguard Worker #include <endian.h>
33*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
34*49cdfc7eSAndroid Build Coastguard Worker #include <sys/stat.h>
35*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
36*49cdfc7eSAndroid Build Coastguard Worker #include <fcntl.h>
37*49cdfc7eSAndroid Build Coastguard Worker #include <inttypes.h>
38*49cdfc7eSAndroid Build Coastguard Worker #include <sys/utsname.h>
39*49cdfc7eSAndroid Build Coastguard Worker #include <limits.h>
40*49cdfc7eSAndroid Build Coastguard Worker 
41*49cdfc7eSAndroid Build Coastguard Worker #include "test.h"
42*49cdfc7eSAndroid Build Coastguard Worker #include "safe_macros.h"
43*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/fallocate.h"
44*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/abisize.h"
45*49cdfc7eSAndroid Build Coastguard Worker 
46*49cdfc7eSAndroid Build Coastguard Worker #define BLOCKS_WRITTEN		12
47*49cdfc7eSAndroid Build Coastguard Worker #ifdef TEST_DEFAULT
48*49cdfc7eSAndroid Build Coastguard Worker # define DEFAULT_TEST_MODE	0
49*49cdfc7eSAndroid Build Coastguard Worker #else
50*49cdfc7eSAndroid Build Coastguard Worker # define DEFAULT_TEST_MODE	1
51*49cdfc7eSAndroid Build Coastguard Worker #endif
52*49cdfc7eSAndroid Build Coastguard Worker #define OFFSET			12
53*49cdfc7eSAndroid Build Coastguard Worker #define FNAMER			"test_file1"
54*49cdfc7eSAndroid Build Coastguard Worker #define FNAMEW			"test_file2"
55*49cdfc7eSAndroid Build Coastguard Worker #define BLOCK_SIZE		1024
56*49cdfc7eSAndroid Build Coastguard Worker #define MAX_FILESIZE            (LLONG_MAX / 1024)
57*49cdfc7eSAndroid Build Coastguard Worker 
58*49cdfc7eSAndroid Build Coastguard Worker static void setup(void);
59*49cdfc7eSAndroid Build Coastguard Worker static void fallocate_verify(int);
60*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void);
61*49cdfc7eSAndroid Build Coastguard Worker 
62*49cdfc7eSAndroid Build Coastguard Worker static int fdw;
63*49cdfc7eSAndroid Build Coastguard Worker static int fdr;
64*49cdfc7eSAndroid Build Coastguard Worker 
65*49cdfc7eSAndroid Build Coastguard Worker static struct test_data_t {
66*49cdfc7eSAndroid Build Coastguard Worker 	int *fd;
67*49cdfc7eSAndroid Build Coastguard Worker 	char *fname;
68*49cdfc7eSAndroid Build Coastguard Worker 	int mode;
69*49cdfc7eSAndroid Build Coastguard Worker 	loff_t offset;
70*49cdfc7eSAndroid Build Coastguard Worker 	loff_t len;
71*49cdfc7eSAndroid Build Coastguard Worker 	int error;
72*49cdfc7eSAndroid Build Coastguard Worker } test_data[] = {
73*49cdfc7eSAndroid Build Coastguard Worker 	{&fdr, FNAMER, DEFAULT_TEST_MODE, 0, 1, EBADF},
74*49cdfc7eSAndroid Build Coastguard Worker 	{&fdw, FNAMEW, DEFAULT_TEST_MODE, -1, 1, EINVAL},
75*49cdfc7eSAndroid Build Coastguard Worker 	{&fdw, FNAMEW, DEFAULT_TEST_MODE, 1, -1, EINVAL},
76*49cdfc7eSAndroid Build Coastguard Worker 	{&fdw, FNAMEW, DEFAULT_TEST_MODE, BLOCKS_WRITTEN, 0, EINVAL},
77*49cdfc7eSAndroid Build Coastguard Worker 	{&fdw, FNAMEW, DEFAULT_TEST_MODE, BLOCKS_WRITTEN, -1, EINVAL},
78*49cdfc7eSAndroid Build Coastguard Worker 	{&fdw, FNAMEW, DEFAULT_TEST_MODE, -(BLOCKS_WRITTEN+OFFSET), 1, EINVAL},
79*49cdfc7eSAndroid Build Coastguard Worker #if defined(TST_ABI64) || _FILE_OFFSET_BITS == 64
80*49cdfc7eSAndroid Build Coastguard Worker 	{&fdw, FNAMEW, DEFAULT_TEST_MODE, MAX_FILESIZE, 1, EFBIG},
81*49cdfc7eSAndroid Build Coastguard Worker 	{&fdw, FNAMEW, DEFAULT_TEST_MODE, 1, MAX_FILESIZE, EFBIG},
82*49cdfc7eSAndroid Build Coastguard Worker #endif
83*49cdfc7eSAndroid Build Coastguard Worker };
84*49cdfc7eSAndroid Build Coastguard Worker 
85*49cdfc7eSAndroid Build Coastguard Worker TCID_DEFINE(fallocate02);
86*49cdfc7eSAndroid Build Coastguard Worker int TST_TOTAL = ARRAY_SIZE(test_data);
87*49cdfc7eSAndroid Build Coastguard Worker 
main(int ac,char ** av)88*49cdfc7eSAndroid Build Coastguard Worker int main(int ac, char **av)
89*49cdfc7eSAndroid Build Coastguard Worker {
90*49cdfc7eSAndroid Build Coastguard Worker 	int lc;
91*49cdfc7eSAndroid Build Coastguard Worker 	int i;
92*49cdfc7eSAndroid Build Coastguard Worker 
93*49cdfc7eSAndroid Build Coastguard Worker 	tst_parse_opts(ac, av, NULL, NULL);
94*49cdfc7eSAndroid Build Coastguard Worker 
95*49cdfc7eSAndroid Build Coastguard Worker 	setup();
96*49cdfc7eSAndroid Build Coastguard Worker 
97*49cdfc7eSAndroid Build Coastguard Worker 	for (lc = 0; TEST_LOOPING(lc); lc++) {
98*49cdfc7eSAndroid Build Coastguard Worker 
99*49cdfc7eSAndroid Build Coastguard Worker 		tst_count = 0;
100*49cdfc7eSAndroid Build Coastguard Worker 
101*49cdfc7eSAndroid Build Coastguard Worker 		for (i = 0; i < TST_TOTAL; i++)
102*49cdfc7eSAndroid Build Coastguard Worker 			fallocate_verify(i);
103*49cdfc7eSAndroid Build Coastguard Worker 	}
104*49cdfc7eSAndroid Build Coastguard Worker 
105*49cdfc7eSAndroid Build Coastguard Worker 	cleanup();
106*49cdfc7eSAndroid Build Coastguard Worker 
107*49cdfc7eSAndroid Build Coastguard Worker 	tst_exit();
108*49cdfc7eSAndroid Build Coastguard Worker }
109*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)110*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
111*49cdfc7eSAndroid Build Coastguard Worker {
112*49cdfc7eSAndroid Build Coastguard Worker 	int i;
113*49cdfc7eSAndroid Build Coastguard Worker 
114*49cdfc7eSAndroid Build Coastguard Worker 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
115*49cdfc7eSAndroid Build Coastguard Worker 
116*49cdfc7eSAndroid Build Coastguard Worker 	TEST_PAUSE;
117*49cdfc7eSAndroid Build Coastguard Worker 
118*49cdfc7eSAndroid Build Coastguard Worker 	tst_tmpdir();
119*49cdfc7eSAndroid Build Coastguard Worker 
120*49cdfc7eSAndroid Build Coastguard Worker 	fdr = SAFE_OPEN(cleanup, FNAMER, O_RDONLY | O_CREAT, S_IRUSR);
121*49cdfc7eSAndroid Build Coastguard Worker 
122*49cdfc7eSAndroid Build Coastguard Worker 	fdw = SAFE_OPEN(cleanup, FNAMEW, O_RDWR | O_CREAT, S_IRWXU);
123*49cdfc7eSAndroid Build Coastguard Worker 
124*49cdfc7eSAndroid Build Coastguard Worker 	char buf[BLOCK_SIZE];
125*49cdfc7eSAndroid Build Coastguard Worker 	memset(buf, 'A', BLOCK_SIZE);
126*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < BLOCKS_WRITTEN; i++)
127*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_WRITE(cleanup, SAFE_WRITE_ALL, fdw, buf, BLOCK_SIZE);
128*49cdfc7eSAndroid Build Coastguard Worker }
129*49cdfc7eSAndroid Build Coastguard Worker 
fallocate_verify(int i)130*49cdfc7eSAndroid Build Coastguard Worker static void fallocate_verify(int i)
131*49cdfc7eSAndroid Build Coastguard Worker {
132*49cdfc7eSAndroid Build Coastguard Worker 	TEST(fallocate(*test_data[i].fd, test_data[i].mode,
133*49cdfc7eSAndroid Build Coastguard Worker 		       test_data[i].offset * BLOCK_SIZE,
134*49cdfc7eSAndroid Build Coastguard Worker 		       test_data[i].len * BLOCK_SIZE));
135*49cdfc7eSAndroid Build Coastguard Worker 	if (TEST_ERRNO != test_data[i].error) {
136*49cdfc7eSAndroid Build Coastguard Worker 		if (TEST_ERRNO == EOPNOTSUPP ||
137*49cdfc7eSAndroid Build Coastguard Worker 		    TEST_ERRNO == ENOSYS) {
138*49cdfc7eSAndroid Build Coastguard Worker 			tst_brkm(TCONF, cleanup,
139*49cdfc7eSAndroid Build Coastguard Worker 				 "fallocate system call is not implemented");
140*49cdfc7eSAndroid Build Coastguard Worker 		}
141*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TFAIL | TTERRNO,
142*49cdfc7eSAndroid Build Coastguard Worker 			 "fallocate(%s:%d, %d, %" PRId64 ", %" PRId64 ") "
143*49cdfc7eSAndroid Build Coastguard Worker 			 "failed, expected errno:%d", test_data[i].fname,
144*49cdfc7eSAndroid Build Coastguard Worker 			 *test_data[i].fd, test_data[i].mode,
145*49cdfc7eSAndroid Build Coastguard Worker 			 test_data[i].offset * BLOCK_SIZE,
146*49cdfc7eSAndroid Build Coastguard Worker 			 test_data[i].len * BLOCK_SIZE, test_data[i].error);
147*49cdfc7eSAndroid Build Coastguard Worker 	} else {
148*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TPASS | TTERRNO,
149*49cdfc7eSAndroid Build Coastguard Worker 			 "fallocate(%s:%d, %d, %" PRId64 ", %" PRId64 ") "
150*49cdfc7eSAndroid Build Coastguard Worker 			 "returned %d", test_data[i].fname, *test_data[i].fd,
151*49cdfc7eSAndroid Build Coastguard Worker 			 test_data[i].mode, test_data[i].offset * BLOCK_SIZE,
152*49cdfc7eSAndroid Build Coastguard Worker 			 test_data[i].len * BLOCK_SIZE, TEST_ERRNO);
153*49cdfc7eSAndroid Build Coastguard Worker 	}
154*49cdfc7eSAndroid Build Coastguard Worker }
155*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)156*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
157*49cdfc7eSAndroid Build Coastguard Worker {
158*49cdfc7eSAndroid Build Coastguard Worker 	if (fdw > 0)
159*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(NULL, fdw);
160*49cdfc7eSAndroid Build Coastguard Worker 	if (fdr > 0)
161*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(NULL, fdr);
162*49cdfc7eSAndroid Build Coastguard Worker 
163*49cdfc7eSAndroid Build Coastguard Worker 	tst_rmdir();
164*49cdfc7eSAndroid Build Coastguard Worker }
165