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