blob: 05939a860695e1c2514a5615307f440460acfd6b [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>
90#include <errno.h>
91#include <sys/stat.h>
92#include <sys/types.h>
93#include <fcntl.h>
vapier4a5373f2009-08-28 12:40:49 +000094#include <inttypes.h>
subrata_modakdf18bd02008-02-19 09:32:47 +000095#include <sys/utsname.h>
subrata_modak122c6032008-01-08 11:01:11 +000096
97/* Harness Specific Include Files. */
98#include "test.h"
99#include "usctest.h"
100#include "linux_syscall_numbers.h"
101
102#define BLOCKS_WRITTEN 12
103
104#ifdef TEST_DEFAULT
subrata_modak56207ce2009-03-23 13:35:39 +0000105#define DEFAULT_TEST_MODE 0 //DEFAULT MODE
subrata_modak122c6032008-01-08 11:01:11 +0000106#else
subrata_modak56207ce2009-03-23 13:35:39 +0000107#define DEFAULT_TEST_MODE 1 //FALLOC_FL_KEEP_SIZE MODE
subrata_modak122c6032008-01-08 11:01:11 +0000108#endif
109
subrata_modak122c6032008-01-08 11:01:11 +0000110#define OFFSET 12
111
112/*Local Functions*/
113static inline long fallocate();
114void populate_file();
115void create_fifo();
116void create_pipe();
117void get_blocksize(int fd);
118
119/* Extern Global Variables */
subrata_modak56207ce2009-03-23 13:35:39 +0000120extern int Tst_count; /* counter for tst_xxx routines */
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};
subrata_modak56207ce2009-03-23 13:35:39 +0000144/* total number of tests in this file */
145int TST_TOTAL = sizeof(test_data) / sizeof(test_data[0]);
subrata_modak122c6032008-01-08 11:01:11 +0000146int block_size;
subrata_modak4bb656a2009-02-26 12:02:09 +0000147int buf_size;
subrata_modak122c6032008-01-08 11:01:11 +0000148
subrata_modak122c6032008-01-08 11:01:11 +0000149/******************************************************************************
subrata_modak4bb656a2009-02-26 12:02:09 +0000150 * Performs all one time clean up for this test on successful
151 * completion, premature exit or failure. Closes all temporary
152 * files, removes all temporary directories exits the test with
153 * appropriate return code by calling tst_exit() function.
subrata_modak122c6032008-01-08 11:01:11 +0000154******************************************************************************/
subrata_modak56207ce2009-03-23 13:35:39 +0000155extern void cleanup()
subrata_modak122c6032008-01-08 11:01:11 +0000156{
157 /* Close all open file descriptors. */
vapier4a5373f2009-08-28 12:40:49 +0000158 if (close(fdw) == -1)
159 tst_resm(TWARN|TERRNO, "close(%s) failed", fnamew);
subrata_modakbdbaec52009-02-26 12:14:51 +0000160
vapier4a5373f2009-08-28 12:40:49 +0000161 if (close(fdr) == -1)
162 tst_resm(TWARN|TERRNO, "close(%s) failed", fnamer);
subrata_modakbdbaec52009-02-26 12:14:51 +0000163
subrata_modak56207ce2009-03-23 13:35:39 +0000164 /* Remove tmp dir and all files in it */
165 tst_rmdir();
subrata_modak122c6032008-01-08 11:01:11 +0000166
167 /* Exit with appropriate return code. */
168 tst_exit();
169
170}
171
172/*****************************************************************************
subrata_modak4bb656a2009-02-26 12:02:09 +0000173 * Performs all one time setup for this test. This function is
subrata_modak122c6032008-01-08 11:01:11 +0000174 * used to create temporary dirs and temporary files
175 * that may be used in the course of this test
176 ******************************************************************************/
subrata_modak56207ce2009-03-23 13:35:39 +0000177void setup()
subrata_modak122c6032008-01-08 11:01:11 +0000178{
subrata_modak56207ce2009-03-23 13:35:39 +0000179 /* capture signals */
180 tst_sig(FORK, DEF_HANDLER, cleanup);
subrata_modak122c6032008-01-08 11:01:11 +0000181
subrata_modak56207ce2009-03-23 13:35:39 +0000182 /* Pause if that option was specified */
183 TEST_PAUSE;
subrata_modak122c6032008-01-08 11:01:11 +0000184
subrata_modak56207ce2009-03-23 13:35:39 +0000185 /* make a temp directory and cd to it */
186 tst_tmpdir();
subrata_modak122c6032008-01-08 11:01:11 +0000187
subrata_modak56207ce2009-03-23 13:35:39 +0000188 sprintf(fnamer, "tfile_read_%d", getpid());
189 sprintf(fnamew, "tfile_write_%d", getpid());
subrata_modak122c6032008-01-08 11:01:11 +0000190
vapier4a5373f2009-08-28 12:40:49 +0000191 fdr = open(fnamer, O_RDONLY | O_CREAT, S_IRUSR);
192 if (fdr == -1)
193 tst_brkm(TBROK|TERRNO, cleanup,
194 "open(%s, O_RDONLY|O_CREAT, S_IRUSR) failed",
195 fnamer);
196 fdw = open(fnamew, O_RDWR | O_CREAT, S_IRWXU);
197 if (fdw == -1)
198 tst_brkm(TBROK|TERRNO, cleanup,
199 "open(%s, O_RDWR|O_CREAT, S_IRWXU) failed",
200 fnamew);
subrata_modak122c6032008-01-08 11:01:11 +0000201 get_blocksize(fdr);
202 populate_file();
203}
204
205/*****************************************************************************
206 * Gets the block size for the file system
207 ******************************************************************************/
subrata_modak56207ce2009-03-23 13:35:39 +0000208void get_blocksize(int fd)
subrata_modak122c6032008-01-08 11:01:11 +0000209{
subrata_modak56207ce2009-03-23 13:35:39 +0000210 struct stat file_stat;
subrata_modakbdbaec52009-02-26 12:14:51 +0000211
subrata_modak56207ce2009-03-23 13:35:39 +0000212 if (fstat(fd, &file_stat) < 0)
vapier4a5373f2009-08-28 12:40:49 +0000213 tst_resm(TFAIL|TERRNO,
214 "fstat failed while getting block_size");
subrata_modakbdbaec52009-02-26 12:14:51 +0000215
subrata_modak56207ce2009-03-23 13:35:39 +0000216 block_size = (int)file_stat.st_blksize;
subrata_modak122c6032008-01-08 11:01:11 +0000217 buf_size = block_size;
218}
subrata_modakbdbaec52009-02-26 12:14:51 +0000219
subrata_modak122c6032008-01-08 11:01:11 +0000220/*****************************************************************************
221 * Writes data into the file
222 ******************************************************************************/
subrata_modak56207ce2009-03-23 13:35:39 +0000223void populate_file()
subrata_modak122c6032008-01-08 11:01:11 +0000224{
subrata_modak56207ce2009-03-23 13:35:39 +0000225 char buf[buf_size + 1];
226 int index;
227 int blocks;
228 int data;
229 for (blocks = 0; blocks < BLOCKS_WRITTEN; blocks++) {
230 for (index = 0; index < buf_size; index++)
231 buf[index] = 'A' + (index % 26);
232 buf[buf_size] = '\0';
vapier4a5373f2009-08-28 12:40:49 +0000233 if ((data = write(fdw, buf, buf_size)) < 0)
234 tst_brkm(TBROK|TERRNO, cleanup,
235 "Unable to write to %s", fnamew);
subrata_modak56207ce2009-03-23 13:35:39 +0000236 }
subrata_modak122c6032008-01-08 11:01:11 +0000237}
238
subrata_modak122c6032008-01-08 11:01:11 +0000239/*****************************************************************************
240 * Wraper function to call fallocate system call
241 ******************************************************************************/
242static inline long fallocate(int fd, int mode, loff_t offset, loff_t len)
243{
subrata_modak56207ce2009-03-23 13:35:39 +0000244#if __WORDSIZE == 32
245 struct utsname buf;
246 if (uname(&buf) == 0) {
247 if (!strcmp(buf.machine, "ppc64")
subrata_modakac1e4c12009-04-28 07:13:25 +0000248 || !strcmp(buf.machine, "ppc")
subrata_modak56207ce2009-03-23 13:35:39 +0000249 || !strcmp(buf.machine, "x86_64"))
250 return syscall(__NR_fallocate, fd, mode,
251 (int)(offset >> 32), (int)offset,
252 (int)(len >> 32), (int)len);
253 } else {
254 perror("uname:");
255 return -1;
256 }
257#endif
258 return syscall(__NR_fallocate, fd, mode, offset, len);
subrata_modak122c6032008-01-08 11:01:11 +0000259}
260
subrata_modak122c6032008-01-08 11:01:11 +0000261/*****************************************************************************
262 * Main function that calls the system call with the appropriate parameters
263 ******************************************************************************/
subrata_modak56207ce2009-03-23 13:35:39 +0000264/* ac: number of command line parameters */
265/* av: pointer to the array of the command line parameters */
266int main(int ac, char **av)
subrata_modak122c6032008-01-08 11:01:11 +0000267{
268
269 int test_index = 0;
270 int lc;
271 int fd;
272 char fname[255], *msg;
subrata_modakbdbaec52009-02-26 12:14:51 +0000273
subrata_modak56207ce2009-03-23 13:35:39 +0000274 /***************************************************************
subrata_modak122c6032008-01-08 11:01:11 +0000275 * parse standard options
276 ***************************************************************/
subrata_modak56207ce2009-03-23 13:35:39 +0000277 if ((msg = parse_opts(ac, av, (option_t *) NULL, NULL)) != (char *)NULL)
subrata_modak122c6032008-01-08 11:01:11 +0000278 tst_brkm(TBROK, cleanup, "OPTION PARSING ERROR - %s", msg);
subrata_modakbdbaec52009-02-26 12:14:51 +0000279
subrata_modak122c6032008-01-08 11:01:11 +0000280 /* perform global test setup, call setup() function. */
281 setup();
282
subrata_modak56207ce2009-03-23 13:35:39 +0000283 for (lc = 0; TEST_LOOPING(lc); lc++) {
284 /* reset Tst_count in case we are looping. */
285 Tst_count = 0;
286 for (test_index = 0; test_index < TST_TOTAL; test_index++) {
287 switch (test_data[test_index].file) {
288 case RFILE:
289 fd = fdr;
290 strcpy(fname, fnamer);
subrata_modak122c6032008-01-08 11:01:11 +0000291 break;
subrata_modak56207ce2009-03-23 13:35:39 +0000292 case WFILE:
293 fd = fdw;
294 strcpy(fname, fnamew);
subrata_modak122c6032008-01-08 11:01:11 +0000295 break;
subrata_modak4bb656a2009-02-26 12:02:09 +0000296 }
subrata_modak56207ce2009-03-23 13:35:39 +0000297
298 TEST(fallocate
299 (fd, test_data[test_index].mode,
300 test_data[test_index].offset * block_size,
301 test_data[test_index].len * block_size));
302 /* check return code */
303 if (TEST_ERRNO != test_data[test_index].error) {
vapier6dd41a32009-11-04 16:31:05 +0000304 if (TEST_ERRNO == EOPNOTSUPP || TEST_ERRNO == ENOSYS) {
subrata_modak56207ce2009-03-23 13:35:39 +0000305 tst_brkm(TCONF, cleanup,
vapier4a5373f2009-08-28 12:40:49 +0000306 "fallocate system call is not implemented");
subrata_modak56207ce2009-03-23 13:35:39 +0000307 }
308 TEST_ERROR_LOG(TEST_ERRNO);
vapier4a5373f2009-08-28 12:40:49 +0000309 tst_resm(TFAIL|TTERRNO, "fallocate(%s:%d, %d, %"PRId64", %"PRId64") failed, expected errno:%d",
subrata_modak56207ce2009-03-23 13:35:39 +0000310 fname, fd, test_data[test_index].mode,
311 test_data[test_index].offset * block_size,
312 test_data[test_index].len * block_size,
vapier4a5373f2009-08-28 12:40:49 +0000313 test_data[test_index].error);
subrata_modak56207ce2009-03-23 13:35:39 +0000314 } else {
315 /* No Verification test, yet... */
316 tst_resm(TPASS,
vapier4a5373f2009-08-28 12:40:49 +0000317 "fallocate(%s:%d, %d, %"PRId64", %"PRId64") returned %d",
subrata_modak56207ce2009-03-23 13:35:39 +0000318 fname, fd, test_data[test_index].mode,
319 test_data[test_index].offset *
320 block_size,
321 test_data[test_index].len * block_size,
322 TEST_ERRNO);
subrata_modak122c6032008-01-08 11:01:11 +0000323 }
subrata_modak122c6032008-01-08 11:01:11 +0000324 }
325 }
326 cleanup();
subrata_modak43337a32009-02-26 11:43:51 +0000327 return 0;
subrata_modak122c6032008-01-08 11:01:11 +0000328}