robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright (c) International Business Machines Corp., 2002 |
subrata_modak | 04f47a1 | 2009-09-18 17:44:08 +0000 | [diff] [blame] | 4 | * Copyright (c) Cyril Hrubis chrubis@suse.cz 2009 |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 14 | * the GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
Wanlong Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 19 | */ |
| 20 | |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 21 | /* |
| 22 | * NAME |
| 23 | * ftest01.c -- test file I/O (ported from SPIE section2, filesuite, by Airong Zhang) |
| 24 | * |
| 25 | * CALLS |
| 26 | * lseek, read, write |
| 27 | * truncate, ftruncate, fsync, sync, fstat |
| 28 | * |
| 29 | * ALGORITHM |
| 30 | * A bitmap is used to map pieces of a file. |
| 31 | * Loop: pick a random piece of the file |
| 32 | * if we haven't seen it before make sure it is zero, |
| 33 | * write pattern |
| 34 | * if we have seen it before make sure correct pattern. |
| 35 | * |
| 36 | * This was originally written by rbk - was program tfio.c |
| 37 | * Modified by dale to integrate with test suites. |
| 38 | * |
| 39 | * RESTRICTIONS |
| 40 | * Runs a long time with default args - can take others on input |
| 41 | * line. Use with "term mode". |
| 42 | * If run on vax the ftruncate will not be random - will always go to |
| 43 | * start of file. NOTE: produces a very high load average!! |
| 44 | * |
| 45 | * CAUTION!! |
| 46 | * If a file is supplied to this program with the "-f" option |
| 47 | * it will be removed with a system("rm -rf filename") call. |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 48 | * |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 49 | */ |
| 50 | #define _GNU_SOURCE 1 |
subrata_modak | f17ac84 | 2009-09-07 09:06:31 +0000 | [diff] [blame] | 51 | #include <stdio.h> |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 52 | #include <sys/types.h> |
| 53 | #include <sys/wait.h> |
| 54 | #include <sys/stat.h> |
vapier | f81795e | 2006-02-15 06:28:58 +0000 | [diff] [blame] | 55 | #include <errno.h> |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 56 | #include <fcntl.h> |
subrata_modak | f17ac84 | 2009-09-07 09:06:31 +0000 | [diff] [blame] | 57 | #include <signal.h> |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 58 | #include <unistd.h> |
subrata_modak | f17ac84 | 2009-09-07 09:06:31 +0000 | [diff] [blame] | 59 | #include <inttypes.h> |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 60 | #include "test.h" |
subrata_modak | 04f47a1 | 2009-09-18 17:44:08 +0000 | [diff] [blame] | 61 | #include "libftest.h" |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 62 | |
| 63 | char *TCID = "ftest01"; |
| 64 | int TST_TOTAL = 1; |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 65 | |
subrata_modak | f17ac84 | 2009-09-07 09:06:31 +0000 | [diff] [blame] | 66 | static void setup(void); |
| 67 | static void runtest(void); |
| 68 | static void dotest(int, int, int); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 69 | static void domisc(int, int, char *); |
subrata_modak | f17ac84 | 2009-09-07 09:06:31 +0000 | [diff] [blame] | 70 | static void cleanup(void); |
subrata_modak | 04f47a1 | 2009-09-18 17:44:08 +0000 | [diff] [blame] | 71 | static void term(int sig); |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 72 | |
| 73 | #define PASSED 1 |
| 74 | #define FAILED 0 |
| 75 | |
subrata_modak | f17ac84 | 2009-09-07 09:06:31 +0000 | [diff] [blame] | 76 | #define MAXCHILD 25 |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 77 | #define K_1 1024 |
| 78 | #define K_2 2048 |
| 79 | #define K_4 4096 |
| 80 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 81 | static int csize; /* chunk size */ |
| 82 | static int iterations; /* # total iterations */ |
| 83 | static int max_size; /* max file size */ |
| 84 | static int misc_intvl; /* for doing misc things; 0 ==> no */ |
| 85 | static int nchild; /* how many children */ |
| 86 | static int fd; /* file descriptor used by child */ |
subrata_modak | 04f47a1 | 2009-09-18 17:44:08 +0000 | [diff] [blame] | 87 | static int parent_pid; |
| 88 | static int pidlist[MAXCHILD]; |
| 89 | static char test_name[2]; |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 90 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 91 | static char fuss[MAXPATHLEN]; /* directory to do this in */ |
| 92 | static char homedir[MAXPATHLEN]; /* where we started */ |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 93 | |
subrata_modak | 04f47a1 | 2009-09-18 17:44:08 +0000 | [diff] [blame] | 94 | static int local_flag; |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 95 | |
subrata_modak | 04f47a1 | 2009-09-18 17:44:08 +0000 | [diff] [blame] | 96 | int main(int ac, char *av[]) |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 97 | { |
subrata_modak | f17ac84 | 2009-09-07 09:06:31 +0000 | [diff] [blame] | 98 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 99 | const char *msg; |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 100 | |
Garrett Cooper | 72521cf | 2010-12-18 06:31:47 -0800 | [diff] [blame] | 101 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Cyril Hrubis | ee7667e | 2014-09-24 13:14:56 +0200 | [diff] [blame] | 102 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 103 | |
| 104 | setup(); |
| 105 | |
| 106 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 107 | |
| 108 | runtest(); |
| 109 | |
subrata_modak | f17ac84 | 2009-09-07 09:06:31 +0000 | [diff] [blame] | 110 | if (local_flag == PASSED) |
vapier | af64a87 | 2006-02-15 06:47:36 +0000 | [diff] [blame] | 111 | tst_resm(TPASS, "Test passed."); |
subrata_modak | f17ac84 | 2009-09-07 09:06:31 +0000 | [diff] [blame] | 112 | else |
vapier | af64a87 | 2006-02-15 06:47:36 +0000 | [diff] [blame] | 113 | tst_resm(TFAIL, "Test failed."); |
subrata_modak | f17ac84 | 2009-09-07 09:06:31 +0000 | [diff] [blame] | 114 | } |
| 115 | |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 116 | cleanup(); |
Garrett Cooper | 72521cf | 2010-12-18 06:31:47 -0800 | [diff] [blame] | 117 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 118 | |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 119 | } |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 120 | |
subrata_modak | f17ac84 | 2009-09-07 09:06:31 +0000 | [diff] [blame] | 121 | static void setup(void) |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 122 | { |
| 123 | |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 124 | tst_tmpdir(); |
subrata_modak | f17ac84 | 2009-09-07 09:06:31 +0000 | [diff] [blame] | 125 | getcwd(homedir, sizeof(homedir)); |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 126 | parent_pid = getpid(); |
| 127 | |
| 128 | if (!fuss[0]) |
| 129 | sprintf(fuss, "./ftest1.%d", getpid()); |
| 130 | |
| 131 | mkdir(fuss, 0755); |
| 132 | |
Garrett Cooper | 72521cf | 2010-12-18 06:31:47 -0800 | [diff] [blame] | 133 | if (chdir(fuss) < 0) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 134 | tst_brkm(TBROK | TERRNO, NULL, "chdir failed"); |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 135 | |
subrata_modak | f17ac84 | 2009-09-07 09:06:31 +0000 | [diff] [blame] | 136 | /* |
| 137 | * Default values for run conditions. |
| 138 | */ |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 139 | iterations = 10; |
| 140 | nchild = 5; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 141 | csize = K_2; /* should run with 1, 2, and 4 K sizes */ |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 142 | max_size = K_1 * K_1; |
| 143 | misc_intvl = 10; |
| 144 | |
subrata_modak | f17ac84 | 2009-09-07 09:06:31 +0000 | [diff] [blame] | 145 | if (sigset(SIGTERM, term) == SIG_ERR) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 146 | tst_brkm(TBROK | TERRNO, NULL, "sigset failed"); |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | local_flag = PASSED; |
| 150 | } |
| 151 | |
subrata_modak | f17ac84 | 2009-09-07 09:06:31 +0000 | [diff] [blame] | 152 | static void runtest(void) |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 153 | { |
Garrett Cooper | 7a73eab | 2010-11-09 22:45:42 -0800 | [diff] [blame] | 154 | pid_t pid; |
| 155 | int i, child, count, nwait, status; |
| 156 | |
| 157 | nwait = 0; |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 158 | |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 159 | for (i = 0; i < nchild; i++) { |
subrata_modak | f17ac84 | 2009-09-07 09:06:31 +0000 | [diff] [blame] | 160 | |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 161 | test_name[0] = 'a' + i; |
| 162 | test_name[1] = '\0'; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 163 | fd = open(test_name, O_RDWR | O_CREAT | O_TRUNC, 0666); |
subrata_modak | f17ac84 | 2009-09-07 09:06:31 +0000 | [diff] [blame] | 164 | |
| 165 | if (fd < 0) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 166 | tst_brkm(TBROK | TERRNO, NULL, "Can't create %s/%s", |
| 167 | fuss, test_name); |
subrata_modak | f17ac84 | 2009-09-07 09:06:31 +0000 | [diff] [blame] | 168 | |
| 169 | if ((child = fork()) == 0) { |
| 170 | dotest(nchild, i, fd); |
| 171 | exit(0); |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 172 | } |
subrata_modak | f17ac84 | 2009-09-07 09:06:31 +0000 | [diff] [blame] | 173 | |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 174 | close(fd); |
subrata_modak | f17ac84 | 2009-09-07 09:06:31 +0000 | [diff] [blame] | 175 | |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 176 | if (child < 0) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 177 | tst_brkm(TBROK | TERRNO, NULL, "fork failed"); |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 178 | } else { |
| 179 | pidlist[i] = child; |
| 180 | nwait++; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | /* |
| 185 | * Wait for children to finish. |
| 186 | */ |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 187 | count = 0; |
subrata_modak | f17ac84 | 2009-09-07 09:06:31 +0000 | [diff] [blame] | 188 | while (1) { |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 189 | if ((child = wait(&status)) >= 0) { |
| 190 | if (status) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 191 | tst_resm(TFAIL, |
| 192 | "Test{%d} failed, expected 0 exit", |
| 193 | child); |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 194 | local_flag = FAILED; |
| 195 | } |
| 196 | ++count; |
subrata_modak | f17ac84 | 2009-09-07 09:06:31 +0000 | [diff] [blame] | 197 | } else { |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 198 | if (errno != EINTR) |
| 199 | break; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | /* |
| 204 | * Should have collected all children. |
| 205 | */ |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 206 | if (count != nwait) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 207 | tst_resm(TFAIL, "Wrong # children waited on, count = %d", |
| 208 | count); |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 209 | local_flag = FAILED; |
| 210 | } |
| 211 | |
subrata_modak | f17ac84 | 2009-09-07 09:06:31 +0000 | [diff] [blame] | 212 | if (local_flag == PASSED) |
vapier | af64a87 | 2006-02-15 06:47:36 +0000 | [diff] [blame] | 213 | tst_resm(TPASS, "Test passed in fork and wait."); |
subrata_modak | f17ac84 | 2009-09-07 09:06:31 +0000 | [diff] [blame] | 214 | else |
vapier | af64a87 | 2006-02-15 06:47:36 +0000 | [diff] [blame] | 215 | tst_resm(TFAIL, "Test failed in fork and wait."); |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 216 | |
| 217 | chdir(homedir); |
| 218 | pid = fork(); |
subrata_modak | f17ac84 | 2009-09-07 09:06:31 +0000 | [diff] [blame] | 219 | |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 220 | if (pid < 0) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 221 | tst_brkm(TBROK | TERRNO, sync, "fork failed"); |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 222 | tst_exit(); |
| 223 | } |
subrata_modak | f17ac84 | 2009-09-07 09:06:31 +0000 | [diff] [blame] | 224 | |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 225 | if (pid == 0) { |
robbiew | 7fdd514 | 2005-02-07 19:39:54 +0000 | [diff] [blame] | 226 | execl("/bin/rm", "rm", "-rf", fuss, NULL); |
Garrett Cooper | 7a73eab | 2010-11-09 22:45:42 -0800 | [diff] [blame] | 227 | exit(1); |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | wait(&status); |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 231 | |
subrata_modak | f17ac84 | 2009-09-07 09:06:31 +0000 | [diff] [blame] | 232 | if (status) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 233 | tst_resm(TINFO, "CAUTION - ftest1, '%s' may not be removed", |
| 234 | fuss); |
subrata_modak | f17ac84 | 2009-09-07 09:06:31 +0000 | [diff] [blame] | 235 | |
| 236 | sync(); |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | /* |
| 240 | * dotest() |
| 241 | * Children execute this. |
| 242 | * |
| 243 | * Randomly read/mod/write chunks with known pattern and check. |
| 244 | * When fill sectors, iterate. |
| 245 | */ |
| 246 | |
| 247 | #define NMISC 4 |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 248 | enum m_type { m_fsync, m_trunc, m_sync, m_fstat }; |
| 249 | char *m_str[] = { "fsync", "trunc", "sync", "fstat" }; |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 250 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 251 | int misc_cnt[NMISC]; /* counts # of each kind of misc */ |
| 252 | int file_max; /* file-max size */ |
| 253 | int nchunks; |
| 254 | int last_trunc = -1; |
| 255 | int tr_flag; |
| 256 | enum m_type type = m_fsync; |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 257 | |
| 258 | #define CHUNK(i) ((i) * csize) |
| 259 | #define NEXTMISC ((rand() % misc_intvl) + 5) |
| 260 | |
Garrett Cooper | 72521cf | 2010-12-18 06:31:47 -0800 | [diff] [blame] | 261 | /* XXX (garrcoop): should not be using libltp as it runs forked. */ |
subrata_modak | f17ac84 | 2009-09-07 09:06:31 +0000 | [diff] [blame] | 262 | static void dotest(int testers, int me, int fd) |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 263 | { |
subrata_modak | f17ac84 | 2009-09-07 09:06:31 +0000 | [diff] [blame] | 264 | char *bits, *hold_bits, *buf, *val_buf, *zero_buf; |
| 265 | char val; |
| 266 | int count, collide, chunk, whenmisc, xfr, i; |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 267 | |
| 268 | nchunks = max_size / csize; |
subrata_modak | f17ac84 | 2009-09-07 09:06:31 +0000 | [diff] [blame] | 269 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 270 | if ((bits = calloc((nchunks + 7) / 8, 1)) == 0) { |
Cyril Hrubis | 526fdf8 | 2014-12-04 14:35:01 +0100 | [diff] [blame] | 271 | tst_brkm(TBROK, |
| 272 | NULL, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 273 | "Test broken due to inability of malloc(bits)."); |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 274 | } |
subrata_modak | f17ac84 | 2009-09-07 09:06:31 +0000 | [diff] [blame] | 275 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 276 | if ((hold_bits = calloc((nchunks + 7) / 8, 1)) == 0) { |
Cyril Hrubis | 526fdf8 | 2014-12-04 14:35:01 +0100 | [diff] [blame] | 277 | tst_brkm(TBROK, |
| 278 | NULL, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 279 | "Test broken due to inability of malloc(hold_bits)."); |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 280 | } |
subrata_modak | f17ac84 | 2009-09-07 09:06:31 +0000 | [diff] [blame] | 281 | |
| 282 | if ((buf = (calloc(csize, 1))) == 0) { |
Cyril Hrubis | 526fdf8 | 2014-12-04 14:35:01 +0100 | [diff] [blame] | 283 | tst_brkm(TBROK, NULL, |
| 284 | "Test broken due to inability of malloc(buf)."); |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 285 | } |
subrata_modak | f17ac84 | 2009-09-07 09:06:31 +0000 | [diff] [blame] | 286 | |
| 287 | if ((val_buf = (calloc(csize, 1))) == 0) { |
Cyril Hrubis | 526fdf8 | 2014-12-04 14:35:01 +0100 | [diff] [blame] | 288 | tst_brkm(TBROK, |
| 289 | NULL, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 290 | "Test broken due to inability of malloc(val_buf)."); |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 291 | } |
subrata_modak | f17ac84 | 2009-09-07 09:06:31 +0000 | [diff] [blame] | 292 | |
| 293 | if ((zero_buf = (calloc(csize, 1))) == 0) { |
Cyril Hrubis | 526fdf8 | 2014-12-04 14:35:01 +0100 | [diff] [blame] | 294 | tst_brkm(TBROK, |
| 295 | NULL, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 296 | "Test broken due to inability of malloc(zero_buf)."); |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | /* |
| 300 | * No init sectors; allow file to be sparse. |
| 301 | */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 302 | val = (64 / testers) * me + 1; |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 303 | |
| 304 | /* |
| 305 | * For each iteration: |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 306 | * zap bits array |
| 307 | * loop: |
| 308 | * pick random chunk, read it. |
| 309 | * if corresponding bit off { |
| 310 | * verify == 0. (sparse file) |
| 311 | * ++count; |
| 312 | * } else |
| 313 | * verify == val. |
| 314 | * write "val" on it. |
| 315 | * repeat until count = nchunks. |
| 316 | * ++val. |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 317 | */ |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 318 | srand(getpid()); |
subrata_modak | f17ac84 | 2009-09-07 09:06:31 +0000 | [diff] [blame] | 319 | |
| 320 | if (misc_intvl) |
| 321 | whenmisc = NEXTMISC; |
| 322 | |
| 323 | while (iterations-- > 0) { |
| 324 | for (i = 0; i < NMISC; i++) |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 325 | misc_cnt[i] = 0; |
| 326 | ftruncate(fd, 0); |
| 327 | file_max = 0; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 328 | memset(bits, 0, (nchunks + 7) / 8); |
| 329 | memset(hold_bits, 0, (nchunks + 7) / 8); |
subrata_modak | 04f47a1 | 2009-09-18 17:44:08 +0000 | [diff] [blame] | 330 | memset(val_buf, val, csize); |
| 331 | memset(zero_buf, 0, csize); |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 332 | count = 0; |
| 333 | collide = 0; |
subrata_modak | f17ac84 | 2009-09-07 09:06:31 +0000 | [diff] [blame] | 334 | while (count < nchunks) { |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 335 | chunk = rand() % nchunks; |
| 336 | /* |
| 337 | * Read it. |
| 338 | */ |
subrata_modak | f17ac84 | 2009-09-07 09:06:31 +0000 | [diff] [blame] | 339 | if (lseek(fd, CHUNK(chunk), 0) < 0) { |
Cyril Hrubis | 526fdf8 | 2014-12-04 14:35:01 +0100 | [diff] [blame] | 340 | tst_brkm(TFAIL, |
| 341 | NULL, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 342 | "Test[%d]: lseek(0) fail at %x, errno = %d.", |
| 343 | me, CHUNK(chunk), errno); |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 344 | } |
| 345 | if ((xfr = read(fd, buf, csize)) < 0) { |
Cyril Hrubis | 526fdf8 | 2014-12-04 14:35:01 +0100 | [diff] [blame] | 346 | tst_brkm(TFAIL, |
| 347 | NULL, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 348 | "Test[%d]: read fail at %x, errno = %d.", |
| 349 | me, CHUNK(chunk), errno); |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 350 | } |
| 351 | /* |
| 352 | * If chunk beyond EOF just write on it. |
| 353 | * Else if bit off, haven't seen it yet. |
| 354 | * Else, have. Verify values. |
| 355 | */ |
| 356 | if (CHUNK(chunk) >= file_max) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 357 | bits[chunk / 8] |= (1 << (chunk % 8)); |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 358 | ++count; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 359 | } else if ((bits[chunk / 8] & (1 << (chunk % 8))) == 0) { |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 360 | if (xfr != csize) { |
Cyril Hrubis | 526fdf8 | 2014-12-04 14:35:01 +0100 | [diff] [blame] | 361 | tst_brkm(TFAIL, |
| 362 | NULL, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 363 | "Test[%d]: xfr=%d != %d, zero read.", |
| 364 | me, xfr, csize); |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 365 | } |
| 366 | if (memcmp(buf, zero_buf, csize)) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 367 | tst_resm(TFAIL, |
| 368 | "Test[%d] bad verify @ 0x%x for val %d " |
| 369 | "count %d xfr %d file_max 0x%x, should be %d.", |
| 370 | me, CHUNK(chunk), val, count, |
| 371 | xfr, file_max, zero_buf[0]); |
| 372 | tst_resm(TFAIL, |
| 373 | "Test[%d]: last_trunc = 0x%x.", |
| 374 | me, last_trunc); |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 375 | sync(); |
subrata_modak | 04f47a1 | 2009-09-18 17:44:08 +0000 | [diff] [blame] | 376 | ft_dumpbuf(buf, csize); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 377 | ft_dumpbits(bits, (nchunks + 7) / 8); |
| 378 | ft_orbits(hold_bits, bits, |
| 379 | (nchunks + 7) / 8); |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 380 | tst_resm(TINFO, "Hold "); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 381 | ft_dumpbits(hold_bits, |
| 382 | (nchunks + 7) / 8); |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 383 | tst_exit(); |
| 384 | } |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 385 | bits[chunk / 8] |= (1 << (chunk % 8)); |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 386 | ++count; |
| 387 | } else { |
| 388 | if (xfr != csize) { |
Cyril Hrubis | 526fdf8 | 2014-12-04 14:35:01 +0100 | [diff] [blame] | 389 | tst_brkm(TFAIL, |
| 390 | NULL, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 391 | "\tTest[%d]: xfr=%d != %d, val read.", |
| 392 | me, xfr, csize); |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 393 | } |
| 394 | ++collide; |
| 395 | if (memcmp(buf, val_buf, csize)) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 396 | tst_resm(TFAIL, |
| 397 | "Test[%d] bad verify @ 0x%x for val %d " |
| 398 | "count %d xfr %d file_max 0x%x.", |
| 399 | me, CHUNK(chunk), val, count, |
| 400 | xfr, file_max); |
| 401 | tst_resm(TFAIL, |
| 402 | "Test[%d]: last_trunc = 0x%x.", |
| 403 | me, last_trunc); |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 404 | sync(); |
subrata_modak | 04f47a1 | 2009-09-18 17:44:08 +0000 | [diff] [blame] | 405 | ft_dumpbuf(buf, csize); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 406 | ft_dumpbits(bits, (nchunks + 7) / 8); |
| 407 | ft_orbits(hold_bits, bits, |
| 408 | (nchunks + 7) / 8); |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 409 | tst_resm(TINFO, "Hold "); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 410 | ft_dumpbits(hold_bits, |
| 411 | (nchunks + 7) / 8); |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 412 | tst_exit(); |
| 413 | } |
| 414 | } |
| 415 | /* |
| 416 | * Write it. |
| 417 | */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 418 | if (lseek(fd, -xfr, 1) < 0) { |
Cyril Hrubis | 526fdf8 | 2014-12-04 14:35:01 +0100 | [diff] [blame] | 419 | tst_brkm(TFAIL, |
| 420 | NULL, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 421 | "Test[%d]: lseek(1) fail at %x, errno = %d.", |
| 422 | me, CHUNK(chunk), errno); |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 423 | } |
| 424 | if ((xfr = write(fd, val_buf, csize)) < csize) { |
| 425 | if (errno == ENOSPC) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 426 | tst_resm(TFAIL, |
| 427 | "Test[%d]: no space, exiting.", |
| 428 | me); |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 429 | fsync(fd); |
| 430 | tst_exit(); |
| 431 | } |
Cyril Hrubis | 526fdf8 | 2014-12-04 14:35:01 +0100 | [diff] [blame] | 432 | tst_brkm(TFAIL, |
| 433 | NULL, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 434 | "Test[%d]: write fail at %x xfr %d, errno = %d.", |
| 435 | me, CHUNK(chunk), xfr, errno); |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 436 | } |
| 437 | if (CHUNK(chunk) + csize > file_max) |
| 438 | file_max = CHUNK(chunk) + csize; |
| 439 | /* |
| 440 | * If hit "misc" interval, do it. |
| 441 | */ |
| 442 | if (misc_intvl && --whenmisc <= 0) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 443 | ft_orbits(hold_bits, bits, (nchunks + 7) / 8); |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 444 | domisc(me, fd, bits); |
| 445 | whenmisc = NEXTMISC; |
| 446 | } |
| 447 | if (count + collide > 2 * nchunks) |
| 448 | break; |
| 449 | } |
| 450 | |
| 451 | /* |
| 452 | * End of iteration, maybe before doing all chunks. |
| 453 | */ |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 454 | fsync(fd); |
subrata_modak | f17ac84 | 2009-09-07 09:06:31 +0000 | [diff] [blame] | 455 | ++misc_cnt[m_fsync]; |
vapier | af64a87 | 2006-02-15 06:47:36 +0000 | [diff] [blame] | 456 | //tst_resm(TINFO, "Test{%d} val %d done, count = %d, collide = {%d}", |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 457 | // me, val, count, collide); |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 458 | //for (i = 0; i < NMISC; i++) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 459 | // tst_resm(TINFO, "Test{%d}: {%d} %s's.", me, misc_cnt[i], m_str[i]); |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 460 | ++val; |
| 461 | } |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | /* |
| 465 | * domisc() |
| 466 | * Inject misc syscalls into the thing. |
| 467 | */ |
subrata_modak | f17ac84 | 2009-09-07 09:06:31 +0000 | [diff] [blame] | 468 | static void domisc(int me, int fd, char *bits) |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 469 | { |
subrata_modak | f17ac84 | 2009-09-07 09:06:31 +0000 | [diff] [blame] | 470 | int chunk; |
| 471 | struct stat sb; |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 472 | |
subrata_modak | f17ac84 | 2009-09-07 09:06:31 +0000 | [diff] [blame] | 473 | if (type > m_fstat) |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 474 | type = m_fsync; |
subrata_modak | f17ac84 | 2009-09-07 09:06:31 +0000 | [diff] [blame] | 475 | switch (type) { |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 476 | case m_fsync: |
| 477 | if (fsync(fd) < 0) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 478 | tst_brkm(TFAIL | TERRNO, NULL, |
| 479 | "Test[%d]: fsync failed.", me); |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 480 | } |
| 481 | break; |
| 482 | case m_trunc: |
| 483 | chunk = rand() % (file_max / csize); |
| 484 | file_max = CHUNK(chunk); |
| 485 | last_trunc = file_max; |
| 486 | if (tr_flag) { |
| 487 | if (ftruncate(fd, file_max) < 0) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 488 | tst_brkm(TFAIL | TERRNO, NULL, |
| 489 | "Test[%d]: ftruncate failed @ 0x%x.", |
| 490 | me, file_max); |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 491 | } |
| 492 | tr_flag = 0; |
| 493 | } else { |
| 494 | if (truncate(test_name, file_max) < 0) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 495 | tst_brkm(TFAIL | TERRNO, NULL, |
| 496 | "Test[%d]: truncate failed @ 0x%x.", |
| 497 | me, file_max); |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 498 | } |
| 499 | tr_flag = 1; |
| 500 | } |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 501 | for (; chunk % 8 != 0; chunk++) |
| 502 | bits[chunk / 8] &= ~(1 << (chunk % 8)); |
subrata_modak | f17ac84 | 2009-09-07 09:06:31 +0000 | [diff] [blame] | 503 | for (; chunk < nchunks; chunk += 8) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 504 | bits[chunk / 8] = 0; |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 505 | break; |
| 506 | case m_sync: |
| 507 | sync(); |
| 508 | break; |
| 509 | case m_fstat: |
Garrett Cooper | 7d716be | 2010-12-19 07:44:35 -0800 | [diff] [blame] | 510 | if (fstat(fd, &sb) < 0) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 511 | tst_brkm(TFAIL | TERRNO, NULL, |
| 512 | "\tTest[%d]: fstat failed", me); |
Garrett Cooper | 7d716be | 2010-12-19 07:44:35 -0800 | [diff] [blame] | 513 | if (sb.st_size != file_max) |
| 514 | tst_brkm(TFAIL, NULL, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 515 | "\tTest[%d]: fstat() mismatch; st_size=%lu, " |
| 516 | "file_max=%x.", me, sb.st_size, file_max); |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 517 | break; |
| 518 | } |
subrata_modak | f17ac84 | 2009-09-07 09:06:31 +0000 | [diff] [blame] | 519 | |
| 520 | ++misc_cnt[type]; |
| 521 | ++type; |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 522 | } |
| 523 | |
subrata_modak | f17ac84 | 2009-09-07 09:06:31 +0000 | [diff] [blame] | 524 | /* |
| 525 | * SIGTERM signal handler. |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 526 | */ |
subrata_modak | 04f47a1 | 2009-09-18 17:44:08 +0000 | [diff] [blame] | 527 | static void term(int sig LTP_ATTRIBUTE_UNUSED) |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 528 | { |
subrata_modak | f17ac84 | 2009-09-07 09:06:31 +0000 | [diff] [blame] | 529 | int i; |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 530 | |
vapier | af64a87 | 2006-02-15 06:47:36 +0000 | [diff] [blame] | 531 | tst_resm(TINFO, "\tterm -[%d]- got sig term.", getpid()); |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 532 | |
| 533 | /* |
| 534 | * If run by hand we like to have the parent send the signal to |
subrata_modak | f17ac84 | 2009-09-07 09:06:31 +0000 | [diff] [blame] | 535 | * the child processes. |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 536 | */ |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 537 | if (parent_pid == getpid()) { |
subrata_modak | f17ac84 | 2009-09-07 09:06:31 +0000 | [diff] [blame] | 538 | for (i = 0; i < nchild; i++) |
| 539 | if (pidlist[i]) |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 540 | kill(pidlist[i], SIGTERM); |
| 541 | tst_exit(); |
| 542 | } |
| 543 | |
vapier | af64a87 | 2006-02-15 06:47:36 +0000 | [diff] [blame] | 544 | tst_resm(TINFO, "\tunlinking '%s'", test_name); |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 545 | |
| 546 | close(fd); |
subrata_modak | f17ac84 | 2009-09-07 09:06:31 +0000 | [diff] [blame] | 547 | |
Garrett Cooper | 7d716be | 2010-12-19 07:44:35 -0800 | [diff] [blame] | 548 | if (unlink(test_name) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 549 | tst_resm(TBROK | TERRNO, "unlink failed"); |
subrata_modak | f17ac84 | 2009-09-07 09:06:31 +0000 | [diff] [blame] | 550 | |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 551 | tst_exit(); |
| 552 | } |
| 553 | |
subrata_modak | f17ac84 | 2009-09-07 09:06:31 +0000 | [diff] [blame] | 554 | static void cleanup(void) |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 555 | { |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 556 | |
| 557 | tst_rmdir(); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 558 | } |