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
40 #include "test.h"
41 #include "safe_macros.h"
42 #include "lapi/fallocate.h"
43
44 #define BLOCKS_WRITTEN 12
45 #ifdef TEST_DEFAULT
46 # define DEFAULT_TEST_MODE 0
47 #else
48 # define DEFAULT_TEST_MODE 1
49 #endif
50 #define OFFSET 12
51 #define FNAMER "test_file1"
52 #define FNAMEW "test_file2"
53 #define BLOCK_SIZE 1024
54 #define MAX_FILESIZE (LLONG_MAX / 1024)
55
56 static void setup(void);
57 static void fallocate_verify(int);
58 static void cleanup(void);
59
60 static int fdw;
61 static int fdr;
62
63 static struct test_data_t {
64 int *fd;
65 char *fname;
66 int mode;
67 loff_t offset;
68 loff_t len;
69 int error;
70 } test_data[] = {
71 {&fdr, FNAMER, DEFAULT_TEST_MODE, 0, 1, EBADF},
72 {&fdw, FNAMEW, DEFAULT_TEST_MODE, -1, 1, EINVAL},
73 {&fdw, FNAMEW, DEFAULT_TEST_MODE, 1, -1, EINVAL},
74 {&fdw, FNAMEW, DEFAULT_TEST_MODE, BLOCKS_WRITTEN, 0, EINVAL},
75 {&fdw, FNAMEW, DEFAULT_TEST_MODE, BLOCKS_WRITTEN, -1, EINVAL},
76 {&fdw, FNAMEW, DEFAULT_TEST_MODE, -(BLOCKS_WRITTEN+OFFSET), 1, EINVAL},
77 #if __WORDSIZE == 64 || _FILE_OFFSET_BITS == 64
78 {&fdw, FNAMEW, DEFAULT_TEST_MODE, MAX_FILESIZE, 1, EFBIG},
79 {&fdw, FNAMEW, DEFAULT_TEST_MODE, 1, MAX_FILESIZE, EFBIG},
80 #endif
81 };
82
83 TCID_DEFINE(fallocate02);
84 int TST_TOTAL = ARRAY_SIZE(test_data);
85
main(int ac,char ** av)86 int main(int ac, char **av)
87 {
88 int lc;
89 int i;
90
91 tst_parse_opts(ac, av, NULL, NULL);
92
93 setup();
94
95 for (lc = 0; TEST_LOOPING(lc); lc++) {
96
97 tst_count = 0;
98
99 for (i = 0; i < TST_TOTAL; i++)
100 fallocate_verify(i);
101 }
102
103 cleanup();
104
105 tst_exit();
106 }
107
setup(void)108 static void setup(void)
109 {
110 int i;
111
112 tst_sig(NOFORK, DEF_HANDLER, cleanup);
113
114 TEST_PAUSE;
115
116 tst_tmpdir();
117
118 fdr = SAFE_OPEN(cleanup, FNAMER, O_RDONLY | O_CREAT, S_IRUSR);
119
120 fdw = SAFE_OPEN(cleanup, FNAMEW, O_RDWR | O_CREAT, S_IRWXU);
121
122 char buf[BLOCK_SIZE];
123 memset(buf, 'A', BLOCK_SIZE);
124 for (i = 0; i < BLOCKS_WRITTEN; i++)
125 SAFE_WRITE(cleanup, 1, fdw, buf, BLOCK_SIZE);
126 }
127
fallocate_verify(int i)128 static void fallocate_verify(int i)
129 {
130 TEST(fallocate(*test_data[i].fd, test_data[i].mode,
131 test_data[i].offset * BLOCK_SIZE,
132 test_data[i].len * BLOCK_SIZE));
133 if (TEST_ERRNO != test_data[i].error) {
134 if (TEST_ERRNO == EOPNOTSUPP ||
135 TEST_ERRNO == ENOSYS) {
136 tst_brkm(TCONF, cleanup,
137 "fallocate system call is not implemented");
138 }
139 tst_resm(TFAIL | TTERRNO,
140 "fallocate(%s:%d, %d, %" PRId64 ", %" PRId64 ") "
141 "failed, expected errno:%d", test_data[i].fname,
142 *test_data[i].fd, test_data[i].mode,
143 test_data[i].offset * BLOCK_SIZE,
144 test_data[i].len * BLOCK_SIZE, test_data[i].error);
145 } else {
146 tst_resm(TPASS | TTERRNO,
147 "fallocate(%s:%d, %d, %" PRId64 ", %" PRId64 ") "
148 "returned %d", test_data[i].fname, *test_data[i].fd,
149 test_data[i].mode, test_data[i].offset * BLOCK_SIZE,
150 test_data[i].len * BLOCK_SIZE, TEST_ERRNO);
151 }
152 }
153
cleanup(void)154 static void cleanup(void)
155 {
156 if (fdw > 0)
157 SAFE_CLOSE(NULL, fdw);
158 if (fdr > 0)
159 SAFE_CLOSE(NULL, fdr);
160
161 tst_rmdir();
162 }
163