blob: 0645cdc9d372f725c379e1da6d5840c7a852afae [file] [log] [blame]
subrata_modak122c6032008-01-08 11:01:11 +00001/******************************************************************************
subrata_modak56207ce2009-03-23 13:35:39 +00002 * fallocate03.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 : fallocate03
29 *
30 * EXECUTED BY : anyone
31 *
32 * TEST TITLE : fallocate
33 *
34 * TEST CASE TOTAL : 8
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 * (Working of fallocate on a sparse file)
46 *
47 *
48 * INPUT SPECIFICATIONS
49 * No input needs to be specified
50 * fallocate() in-puts are specified through test_data
51 *
52 * OUTPUT SPECIFICATIONS
53 * Output describing whether test cases passed or failed.
54 *
55 * ENVIRONMENTAL NEEDS
56 * Test Needs to be executed on file system supporting ext4
57 * LTP {TMP} Needs to be set to such a folder
58 *
59 * SPECIAL PROCEDURAL REQUIREMENTS
60 * None
61 *
62 * DETAILED DESCRIPTION
63 * This is a test case for fallocate() system call.
64 * This test suite tests working of fallocate on sparse file
65 * fallocate is tested for different offsets
66 *
67 * Total 8 Test Cases :-
68 * Different offsets with in a sparse file is tested
69 *
70 * Setup:
71 * Setup file on which fallocate is to be called
72 * Set up a file with hole, created through lseek
73 *
74 * Test:
75 * Loop if the proper options are given
76 * Execute system call
77 * Check return code, if system call failed
78 * TEST fails, PASS the test otherwise
79 *
80 * Cleanup:
81 * Cleanup the temporary folder
82 *
subrata_modak122c6032008-01-08 11:01:11 +000083*************************************************************************/
84
85/* Standard Include Files */
86#include <stdio.h>
87#include <stdlib.h>
88#include <errno.h>
89#include <sys/stat.h>
subrata_modak56207ce2009-03-23 13:35:39 +000090#include <sys/types.h> //Can be done with out
subrata_modak122c6032008-01-08 11:01:11 +000091#include <fcntl.h>
92#include <unistd.h>
vapier4a5373f2009-08-28 12:40:49 +000093#include <inttypes.h>
subrata_modakdf18bd02008-02-19 09:32:47 +000094#include <sys/utsname.h>
subrata_modak122c6032008-01-08 11:01:11 +000095
96/* Harness Specific Include Files. */
97#include "test.h"
98#include "usctest.h"
99#include "linux_syscall_numbers.h"
100
101#define BLOCKS_WRITTEN 12
102#define HOLE_SIZE_IN_BLOCKS 12
103#define DEFAULT_MODE 0
subrata_modak56207ce2009-03-23 13:35:39 +0000104#define FALLOC_FL_KEEP_SIZE 1 //Need to be removed once the glibce support is provided
subrata_modak122c6032008-01-08 11:01:11 +0000105#define TRUE 0
106
subrata_modak122c6032008-01-08 11:01:11 +0000107/*Local Functions*/
108static inline long fallocate();
109void get_blocksize(int);
110void populate_file();
subrata_modak4bb656a2009-02-26 12:02:09 +0000111void file_seek(off_t);
subrata_modak122c6032008-01-08 11:01:11 +0000112
113/* Extern Global Variables */
subrata_modak56207ce2009-03-23 13:35:39 +0000114extern int Tst_count; /* counter for tst_xxx routines */
subrata_modak122c6032008-01-08 11:01:11 +0000115
116/* Global Variables */
subrata_modak56207ce2009-03-23 13:35:39 +0000117char *TCID = "fallocate03"; /* test program identifier */
subrata_modak122c6032008-01-08 11:01:11 +0000118char fname[255];
119int fd;
subrata_modak56207ce2009-03-23 13:35:39 +0000120struct test_data_t {
subrata_modak122c6032008-01-08 11:01:11 +0000121 int mode;
122 loff_t offset;
123 loff_t len;
subrata_modak4bb656a2009-02-26 12:02:09 +0000124 int error;
subrata_modak56207ce2009-03-23 13:35:39 +0000125} test_data[] = {
126 {
127 DEFAULT_MODE, 2, 1, TRUE}, {
128 DEFAULT_MODE, BLOCKS_WRITTEN, 1, TRUE}, {
129 DEFAULT_MODE, BLOCKS_WRITTEN + HOLE_SIZE_IN_BLOCKS / 2 - 1, 1, TRUE}, {
130 DEFAULT_MODE, BLOCKS_WRITTEN + HOLE_SIZE_IN_BLOCKS + 1, 1, TRUE}, {
131 FALLOC_FL_KEEP_SIZE, 2, 1, TRUE}, {
132 FALLOC_FL_KEEP_SIZE, BLOCKS_WRITTEN, 1, TRUE}, {
133 FALLOC_FL_KEEP_SIZE, BLOCKS_WRITTEN + HOLE_SIZE_IN_BLOCKS / 2 + 1, 1, TRUE}, {
134 FALLOC_FL_KEEP_SIZE, BLOCKS_WRITTEN + HOLE_SIZE_IN_BLOCKS + 2, 1, TRUE}
subrata_modak122c6032008-01-08 11:01:11 +0000135};
subrata_modak56207ce2009-03-23 13:35:39 +0000136int TST_TOTAL = sizeof(test_data) / sizeof(test_data[0]); /* total number of tests in this file. */
subrata_modak122c6032008-01-08 11:01:11 +0000137int block_size;
138int buf_size;
139
140/******************************************************************************
subrata_modak4bb656a2009-02-26 12:02:09 +0000141 * Performs all one time clean up for this test on successful
142 * completion, premature exit or failure. Closes all temporary
143 * files, removes all temporary directories exits the test with
144 * appropriate return code by calling tst_exit() function.
subrata_modak122c6032008-01-08 11:01:11 +0000145******************************************************************************/
subrata_modak56207ce2009-03-23 13:35:39 +0000146extern void cleanup()
subrata_modak122c6032008-01-08 11:01:11 +0000147{
148 /* Close all open file descriptors. */
vapier4a5373f2009-08-28 12:40:49 +0000149 if (close(fd) == -1)
150 tst_resm(TWARN|TERRNO, "close(%s) failed", fname);
subrata_modak122c6032008-01-08 11:01:11 +0000151
subrata_modak56207ce2009-03-23 13:35:39 +0000152 /* Remove tmp dir and all files in it */
153 tst_rmdir();
subrata_modak122c6032008-01-08 11:01:11 +0000154
155 /* Exit with appropriate return code. */
156 tst_exit();
157}
158
subrata_modak122c6032008-01-08 11:01:11 +0000159/*****************************************************************************
subrata_modak4bb656a2009-02-26 12:02:09 +0000160 * Performs all one time setup for this test. This function is
subrata_modak122c6032008-01-08 11:01:11 +0000161 * used to create temporary dirs and temporary files
162 * that may be used in the course of this test
163 ******************************************************************************/
164
subrata_modak56207ce2009-03-23 13:35:39 +0000165void setup()
subrata_modak122c6032008-01-08 11:01:11 +0000166{
subrata_modak122c6032008-01-08 11:01:11 +0000167 /* Create temporary directories */
168 TEST_PAUSE;
169
170 tst_tmpdir();
subrata_modakbdbaec52009-02-26 12:14:51 +0000171
subrata_modak56207ce2009-03-23 13:35:39 +0000172 sprintf(fname, "tfile_sparse_%d", getpid());
vapier4a5373f2009-08-28 12:40:49 +0000173 fd = open(fname, O_RDWR | O_CREAT, 0700);
174 if (fd == -1)
175 tst_brkm(TBROK|TERRNO, cleanup,
176 "open(%s) failed", fname);
subrata_modakbdbaec52009-02-26 12:14:51 +0000177 get_blocksize(fd);
subrata_modak122c6032008-01-08 11:01:11 +0000178 populate_file();
subrata_modak56207ce2009-03-23 13:35:39 +0000179 file_seek(BLOCKS_WRITTEN + HOLE_SIZE_IN_BLOCKS); /* create holes */
subrata_modak122c6032008-01-08 11:01:11 +0000180 populate_file();
subrata_modak56207ce2009-03-23 13:35:39 +0000181 file_seek(0); /* Rewind */
subrata_modak122c6032008-01-08 11:01:11 +0000182}
183
184/*****************************************************************************
185 * Gets the block size for the file system
186 ******************************************************************************/
subrata_modak56207ce2009-03-23 13:35:39 +0000187void get_blocksize(int fd)
subrata_modak122c6032008-01-08 11:01:11 +0000188{
subrata_modak56207ce2009-03-23 13:35:39 +0000189 struct stat file_stat;
subrata_modak122c6032008-01-08 11:01:11 +0000190
subrata_modak56207ce2009-03-23 13:35:39 +0000191 if (fstat(fd, &file_stat) < 0)
vapier4a5373f2009-08-28 12:40:49 +0000192 tst_resm(TFAIL|TERRNO,
193 "fstat failed while getting block_size");
subrata_modak122c6032008-01-08 11:01:11 +0000194
subrata_modak56207ce2009-03-23 13:35:39 +0000195 block_size = (int)file_stat.st_blksize;
196 buf_size = block_size;
subrata_modak122c6032008-01-08 11:01:11 +0000197}
198
199/*****************************************************************************
200 * Create a Hole in the file
201 ******************************************************************************/
subrata_modak56207ce2009-03-23 13:35:39 +0000202void file_seek(off_t offset)
subrata_modak122c6032008-01-08 11:01:11 +0000203{
204 offset *= block_size;
subrata_modak56207ce2009-03-23 13:35:39 +0000205 lseek(fd, offset, SEEK_SET);
subrata_modak122c6032008-01-08 11:01:11 +0000206}
207
subrata_modak122c6032008-01-08 11:01:11 +0000208/*****************************************************************************
209 * Writes data into the file
210 ******************************************************************************/
subrata_modak56207ce2009-03-23 13:35:39 +0000211void populate_file()
subrata_modak122c6032008-01-08 11:01:11 +0000212{
213 char buf[buf_size + 1];
214 int index;
215 int blocks;
216 int data;
subrata_modak56207ce2009-03-23 13:35:39 +0000217 for (blocks = 0; blocks < BLOCKS_WRITTEN; blocks++) {
subrata_modak4bb656a2009-02-26 12:02:09 +0000218 for (index = 0; index < buf_size; index++)
subrata_modak56207ce2009-03-23 13:35:39 +0000219 buf[index] = 'A' + (index % 26);
220 buf[buf_size] = '\0';
vapier4a5373f2009-08-28 12:40:49 +0000221 if ((data = write(fd, buf, buf_size)) < 0)
222 tst_brkm(TBROK|TERRNO, cleanup,
223 "Unable to write to %s", fname);
subrata_modak122c6032008-01-08 11:01:11 +0000224 }
225}
226
subrata_modak122c6032008-01-08 11:01:11 +0000227/*****************************************************************************
228 * Wraper function to call fallocate system call
229 ******************************************************************************/
230static inline long fallocate(int fd, int mode, loff_t offset, loff_t len)
231{
subrata_modak56207ce2009-03-23 13:35:39 +0000232#if __WORDSIZE == 32
233 struct utsname buf;
234 if (uname(&buf) == 0) {
235 if (!strcmp(buf.machine, "ppc64")
subrata_modakac1e4c12009-04-28 07:13:25 +0000236 || !strcmp(buf.machine, "ppc")
subrata_modak56207ce2009-03-23 13:35:39 +0000237 || !strcmp(buf.machine, "x86_64"))
238 return syscall(__NR_fallocate, fd, mode,
239 (int)(offset >> 32), (int)offset,
240 (int)(len >> 32), (int)len);
241 } else {
242 perror("uname:");
243 return -1;
244 }
245#endif
246 return syscall(__NR_fallocate, fd, mode, offset, len);
subrata_modak122c6032008-01-08 11:01:11 +0000247}
248
249/*****************************************************************************
250 * Main function that calls the system call with the appropriate parameters
251 ******************************************************************************/
subrata_modak56207ce2009-03-23 13:35:39 +0000252/* ac: number of command line parameters */
253/* av: pointer to the array of the command line parameters */
254int main(int ac, char **av)
subrata_modak122c6032008-01-08 11:01:11 +0000255{
256 int test_index = 0;
257 int lc;
258 char *msg;
259
subrata_modak56207ce2009-03-23 13:35:39 +0000260 /***************************************************************
261 * parse standard options
262 ***************************************************************/
263 if ((msg = parse_opts(ac, av, (option_t *) NULL, NULL)) != (char *)NULL)
subrata_modak122c6032008-01-08 11:01:11 +0000264 tst_brkm(TBROK, cleanup, "OPTION PARSING ERROR - %s", msg);
subrata_modakbdbaec52009-02-26 12:14:51 +0000265
subrata_modak56207ce2009-03-23 13:35:39 +0000266 /* perform global test setup, call setup() function */
subrata_modak122c6032008-01-08 11:01:11 +0000267 setup();
268
subrata_modak56207ce2009-03-23 13:35:39 +0000269 for (lc = 0; TEST_LOOPING(lc); lc++) {
270 /* reset Tst_count in case we are looping */
271 Tst_count = 0;
272 for (test_index = 0; test_index < TST_TOTAL; test_index++) {
273 TEST(fallocate
274 (fd, test_data[test_index].mode,
275 test_data[test_index].offset * block_size,
276 test_data[test_index].len * block_size));
subrata_modak122c6032008-01-08 11:01:11 +0000277
subrata_modak56207ce2009-03-23 13:35:39 +0000278 /* check return code */
279 if (TEST_RETURN != test_data[test_index].error) {
vapier6dd41a32009-11-04 16:31:05 +0000280 if (TEST_ERRNO == EOPNOTSUPP || TEST_ERRNO == ENOSYS) {
subrata_modak56207ce2009-03-23 13:35:39 +0000281 tst_resm(TCONF,
vapier4a5373f2009-08-28 12:40:49 +0000282 "fallocate system call is not implemented");
subrata_modak56207ce2009-03-23 13:35:39 +0000283 cleanup(); /* calls tst_exit */
subrata_modak122c6032008-01-08 11:01:11 +0000284 }
subrata_modak56207ce2009-03-23 13:35:39 +0000285 TEST_ERROR_LOG(TEST_ERRNO);
vapier4a5373f2009-08-28 12:40:49 +0000286 tst_resm(TFAIL|TTERRNO,
287 "fallocate(%s, %d, %"PRId64", %"PRId64") failed",
subrata_modak56207ce2009-03-23 13:35:39 +0000288 fname, test_data[test_index].mode,
289 test_data[test_index].offset *
290 block_size,
vapier4a5373f2009-08-28 12:40:49 +0000291 test_data[test_index].len * block_size);
subrata_modak56207ce2009-03-23 13:35:39 +0000292 } else {
293 if (STD_FUNCTIONAL_TEST) {
294 /* No Verification test, yet... */
295 tst_resm(TPASS,
vapier4a5373f2009-08-28 12:40:49 +0000296 "fallocate(%s, %d, %"PRId64", %"PRId64") returned %ld",
subrata_modak56207ce2009-03-23 13:35:39 +0000297 fname,
298 test_data[test_index].mode,
299 test_data[test_index].offset *
300 block_size,
301 test_data[test_index].len *
302 block_size, TEST_RETURN);
303 }
304 }
subrata_modak122c6032008-01-08 11:01:11 +0000305 }
306 }
307 cleanup();
subrata_modak43337a32009-02-26 11:43:51 +0000308 return 0;
subrata_modak122c6032008-01-08 11:01:11 +0000309}