blob: 3d26f8f6d709ffa25f2d8f228be0b24dfc0cc888 [file] [log] [blame]
subrata_modakb1ef9932008-08-18 10:37:42 +00001/******************************************************************************
2 * sync_file_range01.c
Garrett Cooper5e8be512010-11-22 01:12:27 -08003 * Copyright (c) International Business Machines Corp., 2008
subrata_modakb1ef9932008-08-18 10:37:42 +00004 * Email: bnpoorni@in.ibm.com
subrata_modak56207ce2009-03-23 13:35:39 +00005 *****************************************************************************/
subrata_modakb1ef9932008-08-18 10:37:42 +00006
7/******************************************************************************/
Garrett Cooper5e8be512010-11-22 01:12:27 -08008/* */
subrata_modakb1ef9932008-08-18 10:37:42 +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 */
Garrett Cooper5e8be512010-11-22 01:12:27 -080011/* 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 */
17/* the GNU 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 */
Wanlong Gao4548c6c2012-10-19 18:03:36 +080021/* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */
Garrett Cooper5e8be512010-11-22 01:12:27 -080022/* */
subrata_modakb1ef9932008-08-18 10:37:42 +000023/******************************************************************************/
24
subrata_modakb1ef9932008-08-18 10:37:42 +000025/*****************************************************************************
subrata_modak56207ce2009-03-23 13:35:39 +000026 * TEST IDENTIFIER : sync_file_range01 $
Garrett Cooper5e8be512010-11-22 01:12:27 -080027 * $
28 * EXECUTED BY : anyone $
subrata_modakb1ef9932008-08-18 10:37:42 +000029 *
Garrett Cooper5e8be512010-11-22 01:12:27 -080030 * TEST TITLE : Checks for Errors from sync_file_range()
subrata_modakb1ef9932008-08-18 10:37:42 +000031 *
32 * TEST CASE TOTAL : 5
33 *
Garrett Cooper5e8be512010-11-22 01:12:27 -080034 * CPU ARCHITECTURES : All
subrata_modakb1ef9932008-08-18 10:37:42 +000035 *
Garrett Cooper5e8be512010-11-22 01:12:27 -080036 * AUTHOR : B N Poornima
subrata_modakb1ef9932008-08-18 10:37:42 +000037 *
Garrett Cooper5e8be512010-11-22 01:12:27 -080038 * DATE STARTED : 21/07/2008
subrata_modakb1ef9932008-08-18 10:37:42 +000039 *
40 * TEST CASES
41 * (Tests sync_file_range() for different test cases as reported in the man
42 * page)
43 *
44 * INPUT SPECIFICATIONS
Garrett Cooper5e8be512010-11-22 01:12:27 -080045 * No input needs to be specified
46 * sync_file_data() in-puts are specified through test_data
subrata_modakb1ef9932008-08-18 10:37:42 +000047 *
48 * OUTPUT SPECIFICATIONS
Garrett Cooper5e8be512010-11-22 01:12:27 -080049 * sync_file_data() error message matches with the expected error
subrata_modakb1ef9932008-08-18 10:37:42 +000050 * message.
51 *
52 * ENVIRONMENTAL NEEDS
53 * Kernel version 2.6.17 and above
Garrett Cooper5e8be512010-11-22 01:12:27 -080054 * Kernel version 2.6.22 and above in case of PPC and PPC64
subrata_modakb1ef9932008-08-18 10:37:42 +000055 *
56 * SPECIAL PROCEDURAL REQUIREMENTS
Garrett Cooper5e8be512010-11-22 01:12:27 -080057 * None
subrata_modakb1ef9932008-08-18 10:37:42 +000058 *
59 * DETAILED DESCRIPTION
Garrett Cooper5e8be512010-11-22 01:12:27 -080060 * This is a test case for sync_file_range() system call.
61 * This test suite tests various error messages from the system call
62 * If the error message received matches with the expected
63 * test is considered passed else test fails
subrata_modakb1ef9932008-08-18 10:37:42 +000064 *
Garrett Cooper5e8be512010-11-22 01:12:27 -080065 * Total 5 Test Cases :-
66 * Various error messages from the man page
subrata_modakb1ef9932008-08-18 10:37:42 +000067 *
68 * Setup:
Garrett Cooper5e8be512010-11-22 01:12:27 -080069 * Setup files on which sync_file_range is to be called
subrata_modakb1ef9932008-08-18 10:37:42 +000070 *
71 * Test:
Garrett Cooper5e8be512010-11-22 01:12:27 -080072 * Loop if the proper options are given.
73 * Execute system call
subrata_modakb1ef9932008-08-18 10:37:42 +000074 * Check return code.
Garrett Cooper5e8be512010-11-22 01:12:27 -080075 * If error obtained matches with the expected error
76 * PASS the test, otherwise TEST FAILS
subrata_modakb1ef9932008-08-18 10:37:42 +000077 *
78 * Cleanup:
Garrett Cooper5e8be512010-11-22 01:12:27 -080079 * Cleanup the temporary folder
subrata_modakb1ef9932008-08-18 10:37:42 +000080 *
subrata_modak56207ce2009-03-23 13:35:39 +000081 ******************************************************************************/
subrata_modakb1ef9932008-08-18 10:37:42 +000082#define _GNU_SOURCE
83
Garrett Coopere060c492010-10-23 22:23:13 -070084#include <sys/types.h>
85#include <sys/stat.h>
86#include <sys/utsname.h>
87#include <endian.h>
88#include <errno.h>
89#include <fcntl.h>
subrata_modakb1ef9932008-08-18 10:37:42 +000090#include <stdio.h>
91#include <stdlib.h>
subrata_modakb1ef9932008-08-18 10:37:42 +000092#include <unistd.h>
93
subrata_modakb1ef9932008-08-18 10:37:42 +000094#include "test.h"
subrata_modakb1ef9932008-08-18 10:37:42 +000095#include "linux_syscall_numbers.h"
96
subrata_modak4bb656a2009-02-26 12:02:09 +000097#ifndef SYNC_FILE_RANGE_WAIT_BEFORE
subrata_modakb1ef9932008-08-18 10:37:42 +000098#define SYNC_FILE_RANGE_WAIT_BEFORE 1
subrata_modak56207ce2009-03-23 13:35:39 +000099#define SYNC_FILE_RANGE_WRITE 2 //DUMMY VALUES
subrata_modakb1ef9932008-08-18 10:37:42 +0000100#define SYNC_FILE_RANGE_WAIT_AFTER 4
101#endif
102
103#define SYNC_FILE_RANGE_INVALID 8
104
Cyril Hrubisfdce7d52013-04-04 18:35:48 +0200105char *TCID = "sync_file_range01";
subrata_modak56207ce2009-03-23 13:35:39 +0000106char filename[255]; /* file used for testing */
subrata_modakb1ef9932008-08-18 10:37:42 +0000107char spl_file[] = "/dev/null";
subrata_modak56207ce2009-03-23 13:35:39 +0000108int filed, sfd; /* normal and special fds */
109int bfd = -1; /* Bad file descriptor */
subrata_modakb1ef9932008-08-18 10:37:42 +0000110
subrata_modak56207ce2009-03-23 13:35:39 +0000111struct test_data_t {
112 int *fd;
113 off64_t offset;
114 off64_t nbytes;
115 unsigned int flags;
116 int error;
subrata_modak4bb656a2009-02-26 12:02:09 +0000117} test_data[] = {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800118 {
119 &bfd, 0, 1, SYNC_FILE_RANGE_WRITE, EBADF}, {
120 &sfd, 0, 1, SYNC_FILE_RANGE_WAIT_AFTER, ESPIPE}, {
121 &filed, -1, 1, SYNC_FILE_RANGE_WAIT_BEFORE, EINVAL}, {
122 &filed, 0, -1, SYNC_FILE_RANGE_WRITE, EINVAL}, {
123 &filed, 0, 1, SYNC_FILE_RANGE_INVALID, EINVAL}
subrata_modak56207ce2009-03-23 13:35:39 +0000124};
subrata_modakb1ef9932008-08-18 10:37:42 +0000125
126int TST_TOTAL = sizeof(test_data) / sizeof(test_data[0]);
127
subrata_modakb1ef9932008-08-18 10:37:42 +0000128/* Extern Global Functions */
129/******************************************************************************/
Garrett Cooper5e8be512010-11-22 01:12:27 -0800130/* */
131/* Function: cleanup */
132/* */
subrata_modakb1ef9932008-08-18 10:37:42 +0000133/* Description: Performs all one time clean up for this test on successful */
Garrett Cooper5e8be512010-11-22 01:12:27 -0800134/* completion, premature exit or failure. Closes all temporary */
135/* files, removes all temporary directories exits the test with */
136/* appropriate return code by calling tst_exit() function. */
137/* */
138/* Input: None. */
139/* */
140/* Output: None. */
141/* */
subrata_modakb1ef9932008-08-18 10:37:42 +0000142/* Return: On failure - Exits calling tst_exit(). Non '0' return code. */
Garrett Cooper5e8be512010-11-22 01:12:27 -0800143/* On success - Exits calling tst_exit(). With '0' return code. */
144/* */
subrata_modakb1ef9932008-08-18 10:37:42 +0000145/******************************************************************************/
Mike Frysingerc57fba52014-04-09 18:56:30 -0400146void cleanup(void)
subrata_modakb1ef9932008-08-18 10:37:42 +0000147{
subrata_modakb1ef9932008-08-18 10:37:42 +0000148
subrata_modak56207ce2009-03-23 13:35:39 +0000149 /* close the file we have open */
150 if (close(filed) == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800151 tst_resm(TWARN | TERRNO, "close(%s) failed", filename);
subrata_modak56207ce2009-03-23 13:35:39 +0000152 }
subrata_modakb1ef9932008-08-18 10:37:42 +0000153
subrata_modak56207ce2009-03-23 13:35:39 +0000154 tst_rmdir();
subrata_modakb1ef9932008-08-18 10:37:42 +0000155}
156
subrata_modakb1ef9932008-08-18 10:37:42 +0000157/* Local Functions */
158/******************************************************************************/
Garrett Cooper5e8be512010-11-22 01:12:27 -0800159/* */
160/* Function: setup */
161/* */
subrata_modakb1ef9932008-08-18 10:37:42 +0000162/* Description: Performs all one time setup for this test. This function is */
Garrett Cooper5e8be512010-11-22 01:12:27 -0800163/* typically used to capture signals, create temporary dirs */
164/* and temporary files that may be used in the course of this */
165/* test. */
166/* */
167/* Input: None. */
168/* */
169/* Output: None. */
170/* */
171/* Return: On failure - Exits by calling cleanup(). */
172/* On success - returns 0. */
173/* */
subrata_modakb1ef9932008-08-18 10:37:42 +0000174/******************************************************************************/
Mike Frysingerc57fba52014-04-09 18:56:30 -0400175void setup(void)
subrata_modakb1ef9932008-08-18 10:37:42 +0000176{
Garrett Cooper2c282152010-12-16 00:55:50 -0800177
subrata_modak56207ce2009-03-23 13:35:39 +0000178 tst_sig(NOFORK, DEF_HANDLER, cleanup);
subrata_modakb1ef9932008-08-18 10:37:42 +0000179
subrata_modak56207ce2009-03-23 13:35:39 +0000180 TEST_PAUSE;
subrata_modakb1ef9932008-08-18 10:37:42 +0000181
subrata_modak56207ce2009-03-23 13:35:39 +0000182 tst_tmpdir();
subrata_modakb1ef9932008-08-18 10:37:42 +0000183
subrata_modak56207ce2009-03-23 13:35:39 +0000184 sprintf(filename, "tmpfile_%d", getpid());
185 if ((filed = open(filename, O_RDWR | O_CREAT, 0700)) == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800186 tst_brkm(TBROK | TERRNO, cleanup,
Garrett Cooper53740502010-12-16 00:04:01 -0800187 "open(%s, O_RDWR|O_CREAT,0700) failed", filename);
subrata_modak56207ce2009-03-23 13:35:39 +0000188 }
subrata_modakb1ef9932008-08-18 10:37:42 +0000189
Wanlong Gao354ebb42012-12-07 10:10:04 +0800190 sfd = open(spl_file, O_RDWR | O_CREAT, 0700);
subrata_modakb1ef9932008-08-18 10:37:42 +0000191}
192
193/*****************************************************************************
194 * Wraper function to call sync_file_range system call
195 ******************************************************************************/
subrata_modak56207ce2009-03-23 13:35:39 +0000196static inline long syncfilerange(int fd, off64_t offset, off64_t nbytes,
197 unsigned int flags)
subrata_modakb1ef9932008-08-18 10:37:42 +0000198{
199
Garrett Cooper2e278032010-11-22 01:14:14 -0800200#if (defined(__arm__) || defined(__powerpc__) || defined(__powerpc64__))
201#if (__WORDSIZE == 32)
Garrett Cooper9bed07c2010-10-23 21:56:44 -0700202#if __BYTE_ORDER == __BIG_ENDIAN
Jan Stancek359980f2013-02-15 10:16:05 +0100203 return ltp_syscall(__NR_sync_file_range2, fd, flags,
204 (int)(offset >> 32), (int)offset, (int)(nbytes >> 32),
205 (int)nbytes);
Garrett Cooper9bed07c2010-10-23 21:56:44 -0700206#elif __BYTE_ORDER == __LITTLE_ENDIAN
Jan Stancek359980f2013-02-15 10:16:05 +0100207 return ltp_syscall(__NR_sync_file_range2, fd, flags, (int)offset,
Garrett Cooper9bed07c2010-10-23 21:56:44 -0700208 (int)(offset >> 32), nbytes, (int)(nbytes >> 32));
Garrett Cooper2e278032010-11-22 01:14:14 -0800209#endif
subrata_modak56207ce2009-03-23 13:35:39 +0000210#else
Jan Stancek359980f2013-02-15 10:16:05 +0100211 return ltp_syscall(__NR_sync_file_range2, fd, flags, offset, nbytes);
subrata_modak56207ce2009-03-23 13:35:39 +0000212#endif
Garrett Cooper5e8be512010-11-22 01:12:27 -0800213#else
Jan Stancek359980f2013-02-15 10:16:05 +0100214 return ltp_syscall(__NR_sync_file_range, fd, offset, nbytes, flags);
Garrett Cooper5e8be512010-11-22 01:12:27 -0800215#endif
subrata_modakb1ef9932008-08-18 10:37:42 +0000216
subrata_modak56207ce2009-03-23 13:35:39 +0000217}
subrata_modakb1ef9932008-08-18 10:37:42 +0000218
219/******************************************************************************/
Garrett Cooper5e8be512010-11-22 01:12:27 -0800220/* */
221/* Function: main */
222/* */
subrata_modakb1ef9932008-08-18 10:37:42 +0000223/* Description: Entry point to this test-case. It parses all the command line */
Garrett Cooper5e8be512010-11-22 01:12:27 -0800224/* inputs, calls the global setup and executes the test. It logs */
225/* the test status and results appropriately using the LTP API's */
226/* On successful completion or premature failure, cleanup() func */
227/* is called and test exits with an appropriate return code. */
228/* */
229/* Input: Describe input arguments to this test-case */
230/* -l - Number of iteration */
231/* -v - Prints verbose output */
232/* -V - Prints the version number */
233/* */
234/* Exit: On failure - Exits by calling cleanup(). */
235/* On success - exits with 0 exit value. */
236/* */
subrata_modakb1ef9932008-08-18 10:37:42 +0000237/******************************************************************************/
Garrett Cooper53740502010-12-16 00:04:01 -0800238int main(int ac, char **av)
239{
subrata_modakb1ef9932008-08-18 10:37:42 +0000240
subrata_modak56207ce2009-03-23 13:35:39 +0000241 int test_index = 0;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200242 const char *msg;
subrata_modakb1ef9932008-08-18 10:37:42 +0000243
Garrett Cooper45e285d2010-11-22 12:19:25 -0800244 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -0800245 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
subrata_modakb1ef9932008-08-18 10:37:42 +0000246
subrata_modak56207ce2009-03-23 13:35:39 +0000247#if defined(__powerpc__) || defined(__powerpc64__) /* for PPC, kernel version > 2.6.21 needed */
Garrett Cooperdd65a992010-10-25 23:58:48 -0700248 if (tst_kvercmp(2, 16, 22) < 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800249 tst_brkm(TCONF, NULL,
250 "System doesn't support execution of the test");
subrata_modak56207ce2009-03-23 13:35:39 +0000251 }
subrata_modak5335e9d2008-08-22 21:23:40 +0000252#else
subrata_modak56207ce2009-03-23 13:35:39 +0000253 /* For other archs, need kernel version > 2.6.16 */
subrata_modak5335e9d2008-08-22 21:23:40 +0000254
Garrett Cooperdd65a992010-10-25 23:58:48 -0700255 if (tst_kvercmp(2, 6, 17) < 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800256 tst_brkm(TCONF, NULL,
257 "System doesn't support execution of the test");
subrata_modak56207ce2009-03-23 13:35:39 +0000258 }
subrata_modak5335e9d2008-08-22 21:23:40 +0000259#endif
subrata_modakbdbaec52009-02-26 12:14:51 +0000260
subrata_modak56207ce2009-03-23 13:35:39 +0000261 setup();
subrata_modakb1ef9932008-08-18 10:37:42 +0000262
Garrett Cooper53740502010-12-16 00:04:01 -0800263 for (test_index = 0; test_index < TST_TOTAL; test_index++) {
264 TEST(syncfilerange
265 (*(test_data[test_index].fd),
266 test_data[test_index].offset,
267 test_data[test_index].nbytes,
268 test_data[test_index].flags));
subrata_modakb1ef9932008-08-18 10:37:42 +0000269
Garrett Cooper53740502010-12-16 00:04:01 -0800270 if (TEST_RETURN != -1) {
271 tst_resm(TFAIL,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800272 "call succeeded unexpectedly (%ld != -1)",
273 TEST_RETURN);
Garrett Cooper53740502010-12-16 00:04:01 -0800274 continue;
275 }
subrata_modakb1ef9932008-08-18 10:37:42 +0000276
Garrett Cooper53740502010-12-16 00:04:01 -0800277 if (TEST_ERRNO == test_data[test_index].error) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800278 tst_resm(TPASS | TTERRNO, "got expected error");
Garrett Cooper53740502010-12-16 00:04:01 -0800279 } else {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800280 tst_resm(TFAIL | TTERRNO, "got unexpected error; "
281 "expected %d", test_data[test_index].error);
subrata_modak56207ce2009-03-23 13:35:39 +0000282 }
subrata_modakb1ef9932008-08-18 10:37:42 +0000283
284 }
285
subrata_modak56207ce2009-03-23 13:35:39 +0000286 cleanup();
Garrett Cooper53740502010-12-16 00:04:01 -0800287 tst_exit();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700288}