subrata_modak | 122c603 | 2008-01-08 11:01:11 +0000 | [diff] [blame] | 1 | /****************************************************************************** |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 2 | * fallocate03.c |
subrata_modak | 122c603 | 2008-01-08 11:01:11 +0000 | [diff] [blame] | 3 | * Mon Dec 24 2007 |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 4 | * Copyright (c) International Business Machines Corp., 2007 |
subrata_modak | 122c603 | 2008-01-08 11:01:11 +0000 | [diff] [blame] | 5 | * Emali : sharyathi@in.ibm.com |
| 6 | ******************************************************************************/ |
| 7 | |
subrata_modak | 122c603 | 2008-01-08 11:01:11 +0000 | [diff] [blame] | 8 | /*************************************************************************** |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 9 | * 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_modak | 122c603 | 2008-01-08 11:01:11 +0000 | [diff] [blame] | 23 | |
| 24 | /***************************************************************************** |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 25 | * |
| 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_modak | 122c603 | 2008-01-08 11:01:11 +0000 | [diff] [blame] | 83 | *************************************************************************/ |
| 84 | |
| 85 | /* Standard Include Files */ |
| 86 | #include <stdio.h> |
| 87 | #include <stdlib.h> |
| 88 | #include <errno.h> |
| 89 | #include <sys/stat.h> |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 90 | #include <sys/types.h> //Can be done with out |
subrata_modak | 122c603 | 2008-01-08 11:01:11 +0000 | [diff] [blame] | 91 | #include <fcntl.h> |
| 92 | #include <unistd.h> |
vapier | 4a5373f | 2009-08-28 12:40:49 +0000 | [diff] [blame] | 93 | #include <inttypes.h> |
subrata_modak | df18bd0 | 2008-02-19 09:32:47 +0000 | [diff] [blame] | 94 | #include <sys/utsname.h> |
subrata_modak | 122c603 | 2008-01-08 11:01:11 +0000 | [diff] [blame] | 95 | |
| 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_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 104 | #define FALLOC_FL_KEEP_SIZE 1 //Need to be removed once the glibce support is provided |
subrata_modak | 122c603 | 2008-01-08 11:01:11 +0000 | [diff] [blame] | 105 | #define TRUE 0 |
| 106 | |
subrata_modak | 122c603 | 2008-01-08 11:01:11 +0000 | [diff] [blame] | 107 | /*Local Functions*/ |
| 108 | static inline long fallocate(); |
| 109 | void get_blocksize(int); |
| 110 | void populate_file(); |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 111 | void file_seek(off_t); |
subrata_modak | 122c603 | 2008-01-08 11:01:11 +0000 | [diff] [blame] | 112 | |
| 113 | /* Extern Global Variables */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 114 | extern int Tst_count; /* counter for tst_xxx routines */ |
subrata_modak | 122c603 | 2008-01-08 11:01:11 +0000 | [diff] [blame] | 115 | |
| 116 | /* Global Variables */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 117 | char *TCID = "fallocate03"; /* test program identifier */ |
subrata_modak | 122c603 | 2008-01-08 11:01:11 +0000 | [diff] [blame] | 118 | char fname[255]; |
| 119 | int fd; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 120 | struct test_data_t { |
subrata_modak | 122c603 | 2008-01-08 11:01:11 +0000 | [diff] [blame] | 121 | int mode; |
| 122 | loff_t offset; |
| 123 | loff_t len; |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 124 | int error; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 125 | } 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_modak | 122c603 | 2008-01-08 11:01:11 +0000 | [diff] [blame] | 135 | }; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 136 | int TST_TOTAL = sizeof(test_data) / sizeof(test_data[0]); /* total number of tests in this file. */ |
subrata_modak | 122c603 | 2008-01-08 11:01:11 +0000 | [diff] [blame] | 137 | int block_size; |
| 138 | int buf_size; |
| 139 | |
| 140 | /****************************************************************************** |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 141 | * 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_modak | 122c603 | 2008-01-08 11:01:11 +0000 | [diff] [blame] | 145 | ******************************************************************************/ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 146 | extern void cleanup() |
subrata_modak | 122c603 | 2008-01-08 11:01:11 +0000 | [diff] [blame] | 147 | { |
| 148 | /* Close all open file descriptors. */ |
vapier | 4a5373f | 2009-08-28 12:40:49 +0000 | [diff] [blame] | 149 | if (close(fd) == -1) |
| 150 | tst_resm(TWARN|TERRNO, "close(%s) failed", fname); |
subrata_modak | 122c603 | 2008-01-08 11:01:11 +0000 | [diff] [blame] | 151 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 152 | /* Remove tmp dir and all files in it */ |
| 153 | tst_rmdir(); |
subrata_modak | 122c603 | 2008-01-08 11:01:11 +0000 | [diff] [blame] | 154 | |
| 155 | /* Exit with appropriate return code. */ |
| 156 | tst_exit(); |
| 157 | } |
| 158 | |
subrata_modak | 122c603 | 2008-01-08 11:01:11 +0000 | [diff] [blame] | 159 | /***************************************************************************** |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 160 | * Performs all one time setup for this test. This function is |
subrata_modak | 122c603 | 2008-01-08 11:01:11 +0000 | [diff] [blame] | 161 | * used to create temporary dirs and temporary files |
| 162 | * that may be used in the course of this test |
| 163 | ******************************************************************************/ |
| 164 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 165 | void setup() |
subrata_modak | 122c603 | 2008-01-08 11:01:11 +0000 | [diff] [blame] | 166 | { |
subrata_modak | 122c603 | 2008-01-08 11:01:11 +0000 | [diff] [blame] | 167 | /* Create temporary directories */ |
| 168 | TEST_PAUSE; |
| 169 | |
| 170 | tst_tmpdir(); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 171 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 172 | sprintf(fname, "tfile_sparse_%d", getpid()); |
vapier | 4a5373f | 2009-08-28 12:40:49 +0000 | [diff] [blame] | 173 | fd = open(fname, O_RDWR | O_CREAT, 0700); |
| 174 | if (fd == -1) |
| 175 | tst_brkm(TBROK|TERRNO, cleanup, |
| 176 | "open(%s) failed", fname); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 177 | get_blocksize(fd); |
subrata_modak | 122c603 | 2008-01-08 11:01:11 +0000 | [diff] [blame] | 178 | populate_file(); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 179 | file_seek(BLOCKS_WRITTEN + HOLE_SIZE_IN_BLOCKS); /* create holes */ |
subrata_modak | 122c603 | 2008-01-08 11:01:11 +0000 | [diff] [blame] | 180 | populate_file(); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 181 | file_seek(0); /* Rewind */ |
subrata_modak | 122c603 | 2008-01-08 11:01:11 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | /***************************************************************************** |
| 185 | * Gets the block size for the file system |
| 186 | ******************************************************************************/ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 187 | void get_blocksize(int fd) |
subrata_modak | 122c603 | 2008-01-08 11:01:11 +0000 | [diff] [blame] | 188 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 189 | struct stat file_stat; |
subrata_modak | 122c603 | 2008-01-08 11:01:11 +0000 | [diff] [blame] | 190 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 191 | if (fstat(fd, &file_stat) < 0) |
vapier | 4a5373f | 2009-08-28 12:40:49 +0000 | [diff] [blame] | 192 | tst_resm(TFAIL|TERRNO, |
| 193 | "fstat failed while getting block_size"); |
subrata_modak | 122c603 | 2008-01-08 11:01:11 +0000 | [diff] [blame] | 194 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 195 | block_size = (int)file_stat.st_blksize; |
| 196 | buf_size = block_size; |
subrata_modak | 122c603 | 2008-01-08 11:01:11 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | /***************************************************************************** |
| 200 | * Create a Hole in the file |
| 201 | ******************************************************************************/ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 202 | void file_seek(off_t offset) |
subrata_modak | 122c603 | 2008-01-08 11:01:11 +0000 | [diff] [blame] | 203 | { |
| 204 | offset *= block_size; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 205 | lseek(fd, offset, SEEK_SET); |
subrata_modak | 122c603 | 2008-01-08 11:01:11 +0000 | [diff] [blame] | 206 | } |
| 207 | |
subrata_modak | 122c603 | 2008-01-08 11:01:11 +0000 | [diff] [blame] | 208 | /***************************************************************************** |
| 209 | * Writes data into the file |
| 210 | ******************************************************************************/ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 211 | void populate_file() |
subrata_modak | 122c603 | 2008-01-08 11:01:11 +0000 | [diff] [blame] | 212 | { |
| 213 | char buf[buf_size + 1]; |
| 214 | int index; |
| 215 | int blocks; |
| 216 | int data; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 217 | for (blocks = 0; blocks < BLOCKS_WRITTEN; blocks++) { |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 218 | for (index = 0; index < buf_size; index++) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 219 | buf[index] = 'A' + (index % 26); |
| 220 | buf[buf_size] = '\0'; |
vapier | 4a5373f | 2009-08-28 12:40:49 +0000 | [diff] [blame] | 221 | if ((data = write(fd, buf, buf_size)) < 0) |
| 222 | tst_brkm(TBROK|TERRNO, cleanup, |
| 223 | "Unable to write to %s", fname); |
subrata_modak | 122c603 | 2008-01-08 11:01:11 +0000 | [diff] [blame] | 224 | } |
| 225 | } |
| 226 | |
subrata_modak | 122c603 | 2008-01-08 11:01:11 +0000 | [diff] [blame] | 227 | /***************************************************************************** |
| 228 | * Wraper function to call fallocate system call |
| 229 | ******************************************************************************/ |
| 230 | static inline long fallocate(int fd, int mode, loff_t offset, loff_t len) |
| 231 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 232 | #if __WORDSIZE == 32 |
| 233 | struct utsname buf; |
| 234 | if (uname(&buf) == 0) { |
| 235 | if (!strcmp(buf.machine, "ppc64") |
subrata_modak | ac1e4c1 | 2009-04-28 07:13:25 +0000 | [diff] [blame] | 236 | || !strcmp(buf.machine, "ppc") |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 237 | || !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_modak | 122c603 | 2008-01-08 11:01:11 +0000 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | /***************************************************************************** |
| 250 | * Main function that calls the system call with the appropriate parameters |
| 251 | ******************************************************************************/ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 252 | /* ac: number of command line parameters */ |
| 253 | /* av: pointer to the array of the command line parameters */ |
| 254 | int main(int ac, char **av) |
subrata_modak | 122c603 | 2008-01-08 11:01:11 +0000 | [diff] [blame] | 255 | { |
| 256 | int test_index = 0; |
| 257 | int lc; |
| 258 | char *msg; |
| 259 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 260 | /*************************************************************** |
| 261 | * parse standard options |
| 262 | ***************************************************************/ |
| 263 | if ((msg = parse_opts(ac, av, (option_t *) NULL, NULL)) != (char *)NULL) |
subrata_modak | 122c603 | 2008-01-08 11:01:11 +0000 | [diff] [blame] | 264 | tst_brkm(TBROK, cleanup, "OPTION PARSING ERROR - %s", msg); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 265 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 266 | /* perform global test setup, call setup() function */ |
subrata_modak | 122c603 | 2008-01-08 11:01:11 +0000 | [diff] [blame] | 267 | setup(); |
| 268 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 269 | 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_modak | 122c603 | 2008-01-08 11:01:11 +0000 | [diff] [blame] | 277 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 278 | /* check return code */ |
| 279 | if (TEST_RETURN != test_data[test_index].error) { |
vapier | 6dd41a3 | 2009-11-04 16:31:05 +0000 | [diff] [blame^] | 280 | if (TEST_ERRNO == EOPNOTSUPP || TEST_ERRNO == ENOSYS) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 281 | tst_resm(TCONF, |
vapier | 4a5373f | 2009-08-28 12:40:49 +0000 | [diff] [blame] | 282 | "fallocate system call is not implemented"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 283 | cleanup(); /* calls tst_exit */ |
subrata_modak | 122c603 | 2008-01-08 11:01:11 +0000 | [diff] [blame] | 284 | } |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 285 | TEST_ERROR_LOG(TEST_ERRNO); |
vapier | 4a5373f | 2009-08-28 12:40:49 +0000 | [diff] [blame] | 286 | tst_resm(TFAIL|TTERRNO, |
| 287 | "fallocate(%s, %d, %"PRId64", %"PRId64") failed", |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 288 | fname, test_data[test_index].mode, |
| 289 | test_data[test_index].offset * |
| 290 | block_size, |
vapier | 4a5373f | 2009-08-28 12:40:49 +0000 | [diff] [blame] | 291 | test_data[test_index].len * block_size); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 292 | } else { |
| 293 | if (STD_FUNCTIONAL_TEST) { |
| 294 | /* No Verification test, yet... */ |
| 295 | tst_resm(TPASS, |
vapier | 4a5373f | 2009-08-28 12:40:49 +0000 | [diff] [blame] | 296 | "fallocate(%s, %d, %"PRId64", %"PRId64") returned %ld", |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 297 | 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_modak | 122c603 | 2008-01-08 11:01:11 +0000 | [diff] [blame] | 305 | } |
| 306 | } |
| 307 | cleanup(); |
subrata_modak | 43337a3 | 2009-02-26 11:43:51 +0000 | [diff] [blame] | 308 | return 0; |
subrata_modak | 122c603 | 2008-01-08 11:01:11 +0000 | [diff] [blame] | 309 | } |