blob: b26ef7063974191f42cf2b544dc97e3a4ff0cb1c [file] [log] [blame]
subrata_modak122c6032008-01-08 11:01:11 +00001/******************************************************************************
subrata_modak56207ce2009-03-23 13:35:39 +00002 * fallocate02.c
subrata_modak122c6032008-01-08 11:01:11 +00003 * Mon Dec 24 2007
subrata_modak56207ce2009-03-23 13:35:39 +00004 * Copyright (c) International Business Machines Corp., 2007
subrata_modak122c6032008-01-08 11:01:11 +00005 * Emali : sharyathi@in.ibm.com
6 ******************************************************************************/
7
subrata_modak122c6032008-01-08 11:01:11 +00008/***************************************************************************
subrata_modak56207ce2009-03-23 13:35:39 +00009 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Library General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22***************************************************************************/
subrata_modak122c6032008-01-08 11:01:11 +000023
24/*****************************************************************************
subrata_modak56207ce2009-03-23 13:35:39 +000025 *
26 * OS Test - International Business Machines Corp. 2007.
27 *
28 * TEST IDENTIFIER : fallocate02
29 *
30 * EXECUTED BY : anyone
31 *
32 * TEST TITLE : Checks for Errors from fallocate()
33 *
34 * TEST CASE TOTAL : 7
35 *
36 * CPU ARCHITECTURES : PPC,X86, X86_64
37 *
38 * AUTHOR : Sharyathi Nagesh
39 *
40 * CO-PILOT :
41 *
42 * DATE STARTED : 24/12/2007
43 *
44 * TEST CASES
45 * (Tests fallocate() for different test cases as reported in map page)
46 *
47 * INPUT SPECIFICATIONS
48 * No input needs to be specified
49 * fallocate() in-puts are specified through test_data
50 *
51 * OUTPUT SPECIFICATIONS
52 * fallocate Error message matches with the expected error message.
53 *
54 * ENVIRONMENTAL NEEDS
55 * Test Needs to be executed on file system supporting ext4
56 * LTP {TMP} Needs to be set to such a folder
57 *
58 * SPECIAL PROCEDURAL REQUIREMENTS
59 * None
60 *
61 * DETAILED DESCRIPTION
62 * This is a test case for fallocate() system call.
63 * This test suite tests various error messages from fallocate
64 * If the error message received matches with the expected
65 * test is considered passed else test fails
66 * Provided TEST_DEFAULT to switch b/w modes
67 *
68 * Total 7 Test Cases :-
69 * Various error messages from the man page
70 *
71 * Setup:
72 * Setup files on which fallocate is to be called
73 *
74 * Test:
75 * Loop if the proper options are given.
76 * Execute system call
77 * Check return code.
78 * If error obtained matches with the expected error
79 * PASS the test, otherwise TEST FAILS
80 * Provided TEST_DEFAULT to switch b/w modes
81 *
82 * Cleanup:
83 * Cleanup the temporary folder
84 *
subrata_modak122c6032008-01-08 11:01:11 +000085*************************************************************************/
86
87/* Standard Include Files */
88#include <stdio.h>
89#include <stdlib.h>
Garrett Cooper187f2512010-12-19 18:07:59 -080090#include <endian.h>
subrata_modak122c6032008-01-08 11:01:11 +000091#include <errno.h>
92#include <sys/stat.h>
93#include <sys/types.h>
94#include <fcntl.h>
vapier4a5373f2009-08-28 12:40:49 +000095#include <inttypes.h>
subrata_modakdf18bd02008-02-19 09:32:47 +000096#include <sys/utsname.h>
subrata_modak122c6032008-01-08 11:01:11 +000097
98/* Harness Specific Include Files. */
99#include "test.h"
100#include "usctest.h"
101#include "linux_syscall_numbers.h"
102
103#define BLOCKS_WRITTEN 12
104
105#ifdef TEST_DEFAULT
subrata_modak56207ce2009-03-23 13:35:39 +0000106#define DEFAULT_TEST_MODE 0 //DEFAULT MODE
subrata_modak122c6032008-01-08 11:01:11 +0000107#else
subrata_modak56207ce2009-03-23 13:35:39 +0000108#define DEFAULT_TEST_MODE 1 //FALLOC_FL_KEEP_SIZE MODE
subrata_modak122c6032008-01-08 11:01:11 +0000109#endif
110
subrata_modak122c6032008-01-08 11:01:11 +0000111#define OFFSET 12
112
113/*Local Functions*/
114static inline long fallocate();
115void populate_file();
116void create_fifo();
117void create_pipe();
118void get_blocksize(int fd);
119
120/* Extern Global Variables */
subrata_modak122c6032008-01-08 11:01:11 +0000121/* Global Variables */
subrata_modak56207ce2009-03-23 13:35:39 +0000122char *TCID = "fallocate02"; /* test program identifier */
123char fnamew[255]; /* Files used for testing */
124char fnamer[255]; /* Files used for testing */
subrata_modak122c6032008-01-08 11:01:11 +0000125int fdw;
126int fdr;
subrata_modak56207ce2009-03-23 13:35:39 +0000127enum { RFILE, WFILE, PIPE, FIFO };
128struct test_data_t {
subrata_modak122c6032008-01-08 11:01:11 +0000129 int file;
130 int mode;
131 loff_t offset;
132 loff_t len;
subrata_modak4bb656a2009-02-26 12:02:09 +0000133 int error;
134} test_data[] = {
subrata_modak56207ce2009-03-23 13:35:39 +0000135 {
136 RFILE, DEFAULT_TEST_MODE, 0, 1, EBADF}, {
137 WFILE, DEFAULT_TEST_MODE, -1, 1, EINVAL}, {
138 WFILE, DEFAULT_TEST_MODE, 1, -1, EINVAL}, {
139 WFILE, DEFAULT_TEST_MODE, BLOCKS_WRITTEN, 0, EINVAL}, {
140 WFILE, DEFAULT_TEST_MODE, BLOCKS_WRITTEN, -1, EINVAL}, {
141 WFILE, DEFAULT_TEST_MODE, -(BLOCKS_WRITTEN + OFFSET), 1, EINVAL}, {
142 WFILE, DEFAULT_TEST_MODE, BLOCKS_WRITTEN - OFFSET, 1, 0}
subrata_modak122c6032008-01-08 11:01:11 +0000143};
Wanlong Gao354ebb42012-12-07 10:10:04 +0800144
subrata_modak56207ce2009-03-23 13:35:39 +0000145/* total number of tests in this file */
146int TST_TOTAL = sizeof(test_data) / sizeof(test_data[0]);
subrata_modak122c6032008-01-08 11:01:11 +0000147int block_size;
subrata_modak4bb656a2009-02-26 12:02:09 +0000148int buf_size;
subrata_modak122c6032008-01-08 11:01:11 +0000149
subrata_modak122c6032008-01-08 11:01:11 +0000150/******************************************************************************
Garrett Cooper2c282152010-12-16 00:55:50 -0800151 * Performs all one time clean up for this test on successful
subrata_modak4bb656a2009-02-26 12:02:09 +0000152 * completion, premature exit or failure. Closes all temporary
Garrett Cooper2c282152010-12-16 00:55:50 -0800153 * files, removes all temporary directories exits the test with
154 * appropriate return code by calling tst_exit() function.
subrata_modak122c6032008-01-08 11:01:11 +0000155******************************************************************************/
subrata_modak56207ce2009-03-23 13:35:39 +0000156extern void cleanup()
subrata_modak122c6032008-01-08 11:01:11 +0000157{
158 /* Close all open file descriptors. */
vapier4a5373f2009-08-28 12:40:49 +0000159 if (close(fdw) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800160 tst_resm(TWARN | TERRNO, "close(%s) failed", fnamew);
subrata_modakbdbaec52009-02-26 12:14:51 +0000161
vapier4a5373f2009-08-28 12:40:49 +0000162 if (close(fdr) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800163 tst_resm(TWARN | TERRNO, "close(%s) failed", fnamer);
subrata_modakbdbaec52009-02-26 12:14:51 +0000164
subrata_modak56207ce2009-03-23 13:35:39 +0000165 tst_rmdir();
subrata_modak122c6032008-01-08 11:01:11 +0000166
subrata_modak122c6032008-01-08 11:01:11 +0000167}
168
169/*****************************************************************************
Garrett Cooper2c282152010-12-16 00:55:50 -0800170 * Performs all one time setup for this test. This function is
subrata_modak122c6032008-01-08 11:01:11 +0000171 * used to create temporary dirs and temporary files
172 * that may be used in the course of this test
173 ******************************************************************************/
subrata_modak56207ce2009-03-23 13:35:39 +0000174void setup()
subrata_modak122c6032008-01-08 11:01:11 +0000175{
Garrett Cooper2c282152010-12-16 00:55:50 -0800176
subrata_modak56207ce2009-03-23 13:35:39 +0000177 tst_sig(FORK, DEF_HANDLER, cleanup);
subrata_modak122c6032008-01-08 11:01:11 +0000178
subrata_modak56207ce2009-03-23 13:35:39 +0000179 TEST_PAUSE;
subrata_modak122c6032008-01-08 11:01:11 +0000180
subrata_modak56207ce2009-03-23 13:35:39 +0000181 tst_tmpdir();
subrata_modak122c6032008-01-08 11:01:11 +0000182
subrata_modak56207ce2009-03-23 13:35:39 +0000183 sprintf(fnamer, "tfile_read_%d", getpid());
184 sprintf(fnamew, "tfile_write_%d", getpid());
subrata_modak122c6032008-01-08 11:01:11 +0000185
vapier4a5373f2009-08-28 12:40:49 +0000186 fdr = open(fnamer, O_RDONLY | O_CREAT, S_IRUSR);
187 if (fdr == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800188 tst_brkm(TBROK | TERRNO, cleanup,
189 "open(%s, O_RDONLY|O_CREAT, S_IRUSR) failed", fnamer);
vapier4a5373f2009-08-28 12:40:49 +0000190 fdw = open(fnamew, O_RDWR | O_CREAT, S_IRWXU);
191 if (fdw == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800192 tst_brkm(TBROK | TERRNO, cleanup,
193 "open(%s, O_RDWR|O_CREAT, S_IRWXU) failed", fnamew);
subrata_modak122c6032008-01-08 11:01:11 +0000194 get_blocksize(fdr);
195 populate_file();
196}
197
198/*****************************************************************************
199 * Gets the block size for the file system
200 ******************************************************************************/
subrata_modak56207ce2009-03-23 13:35:39 +0000201void get_blocksize(int fd)
subrata_modak122c6032008-01-08 11:01:11 +0000202{
subrata_modak56207ce2009-03-23 13:35:39 +0000203 struct stat file_stat;
subrata_modakbdbaec52009-02-26 12:14:51 +0000204
subrata_modak56207ce2009-03-23 13:35:39 +0000205 if (fstat(fd, &file_stat) < 0)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800206 tst_resm(TFAIL | TERRNO,
vapier4a5373f2009-08-28 12:40:49 +0000207 "fstat failed while getting block_size");
subrata_modakbdbaec52009-02-26 12:14:51 +0000208
subrata_modak56207ce2009-03-23 13:35:39 +0000209 block_size = (int)file_stat.st_blksize;
subrata_modak122c6032008-01-08 11:01:11 +0000210 buf_size = block_size;
211}
subrata_modakbdbaec52009-02-26 12:14:51 +0000212
subrata_modak122c6032008-01-08 11:01:11 +0000213/*****************************************************************************
214 * Writes data into the file
215 ******************************************************************************/
subrata_modak56207ce2009-03-23 13:35:39 +0000216void populate_file()
subrata_modak122c6032008-01-08 11:01:11 +0000217{
subrata_modak56207ce2009-03-23 13:35:39 +0000218 char buf[buf_size + 1];
219 int index;
220 int blocks;
221 int data;
222 for (blocks = 0; blocks < BLOCKS_WRITTEN; blocks++) {
223 for (index = 0; index < buf_size; index++)
224 buf[index] = 'A' + (index % 26);
225 buf[buf_size] = '\0';
vapier4a5373f2009-08-28 12:40:49 +0000226 if ((data = write(fdw, buf, buf_size)) < 0)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800227 tst_brkm(TBROK | TERRNO, cleanup,
vapier4a5373f2009-08-28 12:40:49 +0000228 "Unable to write to %s", fnamew);
subrata_modak56207ce2009-03-23 13:35:39 +0000229 }
subrata_modak122c6032008-01-08 11:01:11 +0000230}
231
subrata_modak122c6032008-01-08 11:01:11 +0000232/*****************************************************************************
233 * Wraper function to call fallocate system call
234 ******************************************************************************/
235static inline long fallocate(int fd, int mode, loff_t offset, loff_t len)
236{
subrata_modak56207ce2009-03-23 13:35:39 +0000237#if __WORDSIZE == 32
Jan Stancek359980f2013-02-15 10:16:05 +0100238 return (long)ltp_syscall(__NR_fallocate, fd, mode,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800239 __LONG_LONG_PAIR((off_t) (offset >> 32),
240 (off_t) offset),
241 __LONG_LONG_PAIR((off_t) (len >> 32),
242 (off_t) len));
Garrett Cooper187f2512010-12-19 18:07:59 -0800243#else
Jan Stancek359980f2013-02-15 10:16:05 +0100244 return ltp_syscall(__NR_fallocate, fd, mode, offset, len);
Garrett Cooper187f2512010-12-19 18:07:59 -0800245#endif
subrata_modak122c6032008-01-08 11:01:11 +0000246}
247
subrata_modak122c6032008-01-08 11:01:11 +0000248/*****************************************************************************
249 * Main function that calls the system call with the appropriate parameters
250 ******************************************************************************/
subrata_modak56207ce2009-03-23 13:35:39 +0000251/* ac: number of command line parameters */
252/* av: pointer to the array of the command line parameters */
253int main(int ac, char **av)
subrata_modak122c6032008-01-08 11:01:11 +0000254{
255
256 int test_index = 0;
257 int lc;
258 int fd;
259 char fname[255], *msg;
subrata_modakbdbaec52009-02-26 12:14:51 +0000260
subrata_modak56207ce2009-03-23 13:35:39 +0000261 /***************************************************************
subrata_modak122c6032008-01-08 11:01:11 +0000262 * parse standard options
263 ***************************************************************/
Garrett Cooper45e285d2010-11-22 12:19:25 -0800264 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -0800265 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
subrata_modakbdbaec52009-02-26 12:14:51 +0000266
subrata_modak122c6032008-01-08 11:01:11 +0000267 /* perform global test setup, call setup() function. */
268 setup();
269
subrata_modak56207ce2009-03-23 13:35:39 +0000270 for (lc = 0; TEST_LOOPING(lc); lc++) {
Garrett Cooper2c282152010-12-16 00:55:50 -0800271
subrata_modak56207ce2009-03-23 13:35:39 +0000272 Tst_count = 0;
273 for (test_index = 0; test_index < TST_TOTAL; test_index++) {
274 switch (test_data[test_index].file) {
275 case RFILE:
276 fd = fdr;
277 strcpy(fname, fnamer);
subrata_modak122c6032008-01-08 11:01:11 +0000278 break;
subrata_modak56207ce2009-03-23 13:35:39 +0000279 case WFILE:
280 fd = fdw;
281 strcpy(fname, fnamew);
subrata_modak122c6032008-01-08 11:01:11 +0000282 break;
vapier6f366782009-11-04 16:34:30 +0000283 default:
Wanlong Gao354ebb42012-12-07 10:10:04 +0800284 tst_brkm(TCONF, cleanup,
285 "invalid test setting");
Garrett Cooper2c282152010-12-16 00:55:50 -0800286 tst_exit();
subrata_modak4bb656a2009-02-26 12:02:09 +0000287 }
subrata_modak56207ce2009-03-23 13:35:39 +0000288
289 TEST(fallocate
290 (fd, test_data[test_index].mode,
291 test_data[test_index].offset * block_size,
292 test_data[test_index].len * block_size));
293 /* check return code */
294 if (TEST_ERRNO != test_data[test_index].error) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800295 if (TEST_ERRNO == EOPNOTSUPP
296 || TEST_ERRNO == ENOSYS) {
subrata_modak56207ce2009-03-23 13:35:39 +0000297 tst_brkm(TCONF, cleanup,
vapier4a5373f2009-08-28 12:40:49 +0000298 "fallocate system call is not implemented");
subrata_modak56207ce2009-03-23 13:35:39 +0000299 }
300 TEST_ERROR_LOG(TEST_ERRNO);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800301 tst_resm(TFAIL | TTERRNO,
302 "fallocate(%s:%d, %d, %" PRId64 ", %"
303 PRId64 ") failed, expected errno:%d",
304 fname, fd, test_data[test_index].mode,
305 test_data[test_index].offset *
306 block_size,
307 test_data[test_index].len * block_size,
308 test_data[test_index].error);
subrata_modak56207ce2009-03-23 13:35:39 +0000309 } else {
310 /* No Verification test, yet... */
311 tst_resm(TPASS,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800312 "fallocate(%s:%d, %d, %" PRId64 ", %"
313 PRId64 ") returned %d", fname, fd,
314 test_data[test_index].mode,
subrata_modak56207ce2009-03-23 13:35:39 +0000315 test_data[test_index].offset *
316 block_size,
317 test_data[test_index].len * block_size,
318 TEST_ERRNO);
subrata_modak122c6032008-01-08 11:01:11 +0000319 }
subrata_modak122c6032008-01-08 11:01:11 +0000320 }
321 }
322 cleanup();
Garrett Cooper2c282152010-12-16 00:55:50 -0800323 tst_exit();
Garrett Cooper187f2512010-12-19 18:07:59 -0800324}