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 | |
| 21 | /* |
| 22 | * NAME |
| 23 | * ftest08.c -- test single file io (tsfio.c by rbk) (ported from SPIE, |
| 24 | * section2/filesuite/ftest10.c, by Airong Zhang) |
| 25 | * |
| 26 | * this is the same as ftest5, except that it uses lseek64 |
| 27 | * |
| 28 | * CALLS |
| 29 | * fsync, sync, lseek64, read, write |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 30 | * |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 31 | * |
| 32 | * ALGORITHM |
| 33 | * Several child processes doing random seeks, read/write |
| 34 | * operations on the same file. |
| 35 | * |
| 36 | * |
| 37 | * RESTRICTIONS |
| 38 | * Runs a long time with default args - can take others on input |
| 39 | * line. Use with "term mode". |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 40 | * |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 41 | */ |
| 42 | |
| 43 | #define _XOPEN_SOURCE 500 |
subrata_modak | 04f47a1 | 2009-09-18 17:44:08 +0000 | [diff] [blame] | 44 | #define _LARGEFILE64_SOURCE 1 |
| 45 | #include <stdio.h> |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 46 | #include <sys/types.h> |
| 47 | #include <sys/param.h> |
| 48 | #include <sys/wait.h> |
| 49 | #include <sys/file.h> |
| 50 | #include <sys/fcntl.h> |
| 51 | #include <sys/stat.h> |
| 52 | #include <sys/uio.h> |
vapier | f81795e | 2006-02-15 06:28:58 +0000 | [diff] [blame] | 53 | #include <errno.h> |
subrata_modak | 04f47a1 | 2009-09-18 17:44:08 +0000 | [diff] [blame] | 54 | #include <signal.h> |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 55 | #include <unistd.h> |
root | ad99f10 | 2010-02-19 01:24:05 -0800 | [diff] [blame] | 56 | #include <inttypes.h> |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 57 | #include "test.h" |
subrata_modak | 04f47a1 | 2009-09-18 17:44:08 +0000 | [diff] [blame] | 58 | #include "libftest.h" |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 59 | |
| 60 | char *TCID = "ftest08"; |
| 61 | int TST_TOTAL = 1; |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 62 | |
| 63 | #define PASSED 1 |
| 64 | #define FAILED 0 |
| 65 | |
subrata_modak | 04f47a1 | 2009-09-18 17:44:08 +0000 | [diff] [blame] | 66 | #define MAXCHILD 25 |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 67 | #define K_1 1024 |
| 68 | #define K_2 2048 |
| 69 | #define K_4 4096 |
| 70 | #define MAXIOVCNT 16 |
| 71 | |
subrata_modak | 04f47a1 | 2009-09-18 17:44:08 +0000 | [diff] [blame] | 72 | static void init(void); |
| 73 | static void runtest(void); |
| 74 | static void dotest(int, int, int); |
| 75 | static void domisc(int, int); |
| 76 | static void term(int sig); |
| 77 | static void cleanup(void); |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 78 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 79 | static int csize; /* chunk size */ |
| 80 | static int iterations; /* # total iterations */ |
| 81 | static off64_t max_size; /* max file size */ |
| 82 | static int misc_intvl; /* for doing misc things; 0 ==> no */ |
| 83 | static int nchild; /* number of child processes */ |
subrata_modak | 04f47a1 | 2009-09-18 17:44:08 +0000 | [diff] [blame] | 84 | static int parent_pid; |
| 85 | static int pidlist[MAXCHILD]; |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 86 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 87 | static char filename[MAXPATHLEN]; |
| 88 | static int local_flag; |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 89 | |
subrata_modak | 04f47a1 | 2009-09-18 17:44:08 +0000 | [diff] [blame] | 90 | int main(int ac, char *av[]) |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 91 | { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 92 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 93 | const char *msg; |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 94 | |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 95 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Cyril Hrubis | ee7667e | 2014-09-24 13:14:56 +0200 | [diff] [blame] | 96 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 97 | |
| 98 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 99 | |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 100 | local_flag = PASSED; |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 101 | init(); |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 102 | runtest(); |
| 103 | |
subrata_modak | 04f47a1 | 2009-09-18 17:44:08 +0000 | [diff] [blame] | 104 | if (local_flag == PASSED) |
| 105 | tst_resm(TPASS, "Test passed."); |
| 106 | else |
| 107 | tst_resm(TFAIL, "Test failed."); |
| 108 | } |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 109 | |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 110 | cleanup(); |
Garrett Cooper | 7d716be | 2010-12-19 07:44:35 -0800 | [diff] [blame] | 111 | tst_exit(); |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 112 | } |
| 113 | |
subrata_modak | 04f47a1 | 2009-09-18 17:44:08 +0000 | [diff] [blame] | 114 | static void init(void) |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 115 | { |
| 116 | int fd; |
| 117 | char wdbuf[MAXPATHLEN]; |
| 118 | |
| 119 | parent_pid = getpid(); |
| 120 | tst_tmpdir(); |
subrata_modak | 04f47a1 | 2009-09-18 17:44:08 +0000 | [diff] [blame] | 121 | |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 122 | /* |
| 123 | * Make a filename for the test. |
| 124 | */ |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 125 | if (!filename[0]) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 126 | sprintf(filename, "%s/ftest08.%d", getcwd(wdbuf, MAXPATHLEN), |
| 127 | getpid()); |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 128 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 129 | fd = open(filename, O_RDWR | O_CREAT | O_TRUNC, 0666); |
subrata_modak | 04f47a1 | 2009-09-18 17:44:08 +0000 | [diff] [blame] | 130 | |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 131 | if (fd < 0) { |
Cyril Hrubis | 526fdf8 | 2014-12-04 14:35:01 +0100 | [diff] [blame] | 132 | tst_brkm(TBROK, NULL, "Error %d creating file %s", errno, |
| 133 | filename); |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 134 | } |
subrata_modak | 04f47a1 | 2009-09-18 17:44:08 +0000 | [diff] [blame] | 135 | |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 136 | close(fd); |
| 137 | |
| 138 | /* |
| 139 | * Default values for run conditions. |
| 140 | */ |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 141 | iterations = 10; |
| 142 | nchild = 5; |
| 143 | csize = K_2; /* should run with 1, 2, and 4 K sizes */ |
| 144 | max_size = K_1 * K_1; |
| 145 | misc_intvl = 10; |
| 146 | |
subrata_modak | 04f47a1 | 2009-09-18 17:44:08 +0000 | [diff] [blame] | 147 | if (sigset(SIGTERM, term) == SIG_ERR) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 148 | tst_brkm(TBROK | TERRNO, NULL, "first sigset failed"); |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | } |
| 152 | |
subrata_modak | 04f47a1 | 2009-09-18 17:44:08 +0000 | [diff] [blame] | 153 | static void runtest(void) |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 154 | { |
Garrett Cooper | 7a73eab | 2010-11-09 22:45:42 -0800 | [diff] [blame] | 155 | int child, count, fd, i, nwait, status; |
| 156 | |
| 157 | nwait = 0; |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 158 | |
subrata_modak | 04f47a1 | 2009-09-18 17:44:08 +0000 | [diff] [blame] | 159 | for (i = 0; i < nchild; i++) { |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 160 | |
subrata_modak | 04f47a1 | 2009-09-18 17:44:08 +0000 | [diff] [blame] | 161 | if ((child = fork()) == 0) { |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 162 | fd = open(filename, O_RDWR); |
| 163 | if (fd < 0) { |
Cyril Hrubis | 526fdf8 | 2014-12-04 14:35:01 +0100 | [diff] [blame] | 164 | tst_brkm(TFAIL, |
| 165 | NULL, |
| 166 | "\tTest[%d]: error %d openning %s.", |
| 167 | i, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 168 | errno, filename); |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 169 | } |
subrata_modak | 04f47a1 | 2009-09-18 17:44:08 +0000 | [diff] [blame] | 170 | dotest(nchild, i, fd); |
subrata_modak | 9320b11 | 2008-08-27 11:26:51 +0000 | [diff] [blame] | 171 | close(fd); |
subrata_modak | 04f47a1 | 2009-09-18 17:44:08 +0000 | [diff] [blame] | 172 | tst_exit(); |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 173 | } |
subrata_modak | 04f47a1 | 2009-09-18 17:44:08 +0000 | [diff] [blame] | 174 | |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 175 | if (child < 0) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 176 | tst_brkm(TBROK | TERRNO, NULL, "fork failed"); |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 177 | } else { |
| 178 | pidlist[i] = child; |
| 179 | nwait++; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | /* |
| 184 | * Wait for children to finish. |
| 185 | */ |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 186 | count = 0; |
subrata_modak | 04f47a1 | 2009-09-18 17:44:08 +0000 | [diff] [blame] | 187 | while ((child = wait(&status)) != -1 || errno == EINTR) { |
| 188 | if (child > 0) { |
vapier | af64a87 | 2006-02-15 06:47:36 +0000 | [diff] [blame] | 189 | //tst_resm(TINFO, "\tTest{%d} exited status = 0x%x", child, status); |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 190 | if (status) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 191 | tst_resm(TFAIL, |
| 192 | "\tExpected 0 exit status - failed."); |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 193 | local_flag = FAILED; |
| 194 | } |
| 195 | ++count; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | /* |
| 200 | * Should have collected all children. |
| 201 | */ |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 202 | if (count != nwait) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 203 | tst_resm(TFAIL, "\tWrong # children waited on, count = %d", |
| 204 | count); |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 205 | local_flag = FAILED; |
| 206 | } |
| 207 | |
| 208 | unlink(filename); |
subrata_modak | 04f47a1 | 2009-09-18 17:44:08 +0000 | [diff] [blame] | 209 | sync(); |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | /* |
| 213 | * dotest() |
| 214 | * Children execute this. |
| 215 | * |
| 216 | * Randomly read/mod/write chunks with known pattern and check. |
| 217 | * When fill sectors, iterate. |
| 218 | */ |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 219 | #define NMISC 2 |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 220 | enum m_type { m_fsync, m_sync }; |
| 221 | char *m_str[] = { "fsync", "sync" }; |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 222 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 223 | int misc_cnt[NMISC]; /* counts # of each kind of misc */ |
| 224 | int misc_flag; |
| 225 | int nchunks; |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 226 | |
| 227 | #define CHUNK(i) ((((off64_t)i) * testers + me) * csize) |
| 228 | #define NEXTMISC ((rand() % misc_intvl) + 5) |
| 229 | |
subrata_modak | 04f47a1 | 2009-09-18 17:44:08 +0000 | [diff] [blame] | 230 | static void dotest(int testers, int me, int fd) |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 231 | { |
Cyril Hrubis | 82ddd5f | 2013-06-04 19:33:07 +0200 | [diff] [blame] | 232 | char *bits; |
subrata_modak | 04f47a1 | 2009-09-18 17:44:08 +0000 | [diff] [blame] | 233 | char val, val0; |
| 234 | int count, collide, chunk, whenmisc, xfr, i; |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 235 | |
| 236 | /* Stuff for the readv call */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 237 | struct iovec r_iovec[MAXIOVCNT]; |
| 238 | int r_ioveclen; |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 239 | |
| 240 | /* Stuff for the writev call */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 241 | struct iovec val0_iovec[MAXIOVCNT]; |
| 242 | struct iovec val_iovec[MAXIOVCNT]; |
| 243 | int w_ioveclen; |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 244 | |
| 245 | nchunks = max_size / (testers * csize); |
Garrett Cooper | f01306b | 2010-11-05 11:14:08 -0700 | [diff] [blame] | 246 | whenmisc = 0; |
subrata_modak | 04f47a1 | 2009-09-18 17:44:08 +0000 | [diff] [blame] | 247 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 248 | if ((bits = malloc((nchunks + 7) / 8)) == NULL) { |
Cyril Hrubis | 526fdf8 | 2014-12-04 14:35:01 +0100 | [diff] [blame] | 249 | tst_brkm(TBROK, NULL, "\tmalloc failed(bits)"); |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 250 | } |
subrata_modak | 04f47a1 | 2009-09-18 17:44:08 +0000 | [diff] [blame] | 251 | |
subrata_modak | 04f47a1 | 2009-09-18 17:44:08 +0000 | [diff] [blame] | 252 | /* Allocate memory for the iovec buffers and init the iovec arrays */ |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 253 | r_ioveclen = w_ioveclen = csize / MAXIOVCNT; |
| 254 | |
subrata_modak | 04f47a1 | 2009-09-18 17:44:08 +0000 | [diff] [blame] | 255 | /* Please note that the above statement implies that csize |
| 256 | * be evenly divisible by MAXIOVCNT. |
| 257 | */ |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 258 | for (i = 0; i < MAXIOVCNT; i++) { |
subrata_modak | 04f47a1 | 2009-09-18 17:44:08 +0000 | [diff] [blame] | 259 | if ((r_iovec[i].iov_base = malloc(r_ioveclen)) == NULL) { |
Cyril Hrubis | 526fdf8 | 2014-12-04 14:35:01 +0100 | [diff] [blame] | 260 | tst_brkm(TBROK, NULL, "\tmalloc failed(iov_base)"); |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 261 | } |
| 262 | r_iovec[i].iov_len = r_ioveclen; |
| 263 | |
| 264 | /* Allocate unused memory areas between all the buffers to |
| 265 | * make things more diffult for the OS. |
| 266 | */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 267 | if (malloc((i + 1) * 8) == NULL) { |
Cyril Hrubis | 526fdf8 | 2014-12-04 14:35:01 +0100 | [diff] [blame] | 268 | tst_brkm(TBROK, NULL, "\tmalloc failed((i+1)*8)"); |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 269 | } |
subrata_modak | 04f47a1 | 2009-09-18 17:44:08 +0000 | [diff] [blame] | 270 | |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 271 | if ((val0_iovec[i].iov_base = malloc(w_ioveclen)) == NULL) { |
Cyril Hrubis | 526fdf8 | 2014-12-04 14:35:01 +0100 | [diff] [blame] | 272 | tst_brkm(TBROK, NULL, "\tmalloc failed(val0_iovec)"); |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 273 | } |
subrata_modak | 04f47a1 | 2009-09-18 17:44:08 +0000 | [diff] [blame] | 274 | |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 275 | val0_iovec[i].iov_len = w_ioveclen; |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 276 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 277 | if (malloc((i + 1) * 8) == NULL) { |
Cyril Hrubis | 526fdf8 | 2014-12-04 14:35:01 +0100 | [diff] [blame] | 278 | tst_brkm(TBROK, NULL, "\tmalloc failed((i+1)*8)"); |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 279 | } |
subrata_modak | 04f47a1 | 2009-09-18 17:44:08 +0000 | [diff] [blame] | 280 | |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 281 | if ((val_iovec[i].iov_base = malloc(w_ioveclen)) == NULL) { |
Cyril Hrubis | 526fdf8 | 2014-12-04 14:35:01 +0100 | [diff] [blame] | 282 | tst_brkm(TBROK, NULL, "\tmalloc failed(iov_base)"); |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 283 | } |
| 284 | val_iovec[i].iov_len = w_ioveclen; |
| 285 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 286 | if (malloc((i + 1) * 8) == NULL) { |
Cyril Hrubis | 526fdf8 | 2014-12-04 14:35:01 +0100 | [diff] [blame] | 287 | tst_brkm(TBROK, NULL, "\tmalloc failed(((i+1)*8)"); |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 288 | } |
| 289 | } |
| 290 | |
| 291 | /* |
| 292 | * No init sectors; file-sys makes 0 to start. |
| 293 | */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 294 | val = (64 / testers) * me + 1; |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 295 | val0 = 0; |
| 296 | |
| 297 | /* |
| 298 | * For each iteration: |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 299 | * zap bits array |
| 300 | * loop: |
| 301 | * pick random chunk, read it. |
| 302 | * if corresponding bit off { |
| 303 | * verify == 0. (sparse file) |
| 304 | * ++count; |
| 305 | * } else |
| 306 | * verify == val. |
| 307 | * write "val" on it. |
| 308 | * repeat until count = nchunks. |
| 309 | * ++val. |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 310 | */ |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 311 | srand(getpid()); |
subrata_modak | 04f47a1 | 2009-09-18 17:44:08 +0000 | [diff] [blame] | 312 | |
| 313 | if (misc_intvl) |
| 314 | whenmisc = NEXTMISC; |
| 315 | |
| 316 | while (iterations-- > 0) { |
| 317 | for (i = 0; i < NMISC; i++) |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 318 | misc_cnt[i] = 0; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 319 | memset(bits, 0, (nchunks + 7) / 8); |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 320 | /* Have to fill the val0 and val iov buffers in a different manner |
| 321 | */ |
subrata_modak | 04f47a1 | 2009-09-18 17:44:08 +0000 | [diff] [blame] | 322 | for (i = 0; i < MAXIOVCNT; i++) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 323 | memset(val0_iovec[i].iov_base, val0, |
| 324 | val0_iovec[i].iov_len); |
| 325 | memset(val_iovec[i].iov_base, val, |
| 326 | val_iovec[i].iov_len); |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 327 | |
| 328 | } |
subrata_modak | 04f47a1 | 2009-09-18 17:44:08 +0000 | [diff] [blame] | 329 | |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 330 | count = 0; |
| 331 | collide = 0; |
subrata_modak | 04f47a1 | 2009-09-18 17:44:08 +0000 | [diff] [blame] | 332 | |
| 333 | while (count < nchunks) { |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 334 | chunk = rand() % nchunks; |
| 335 | /* |
| 336 | * Read it. |
| 337 | */ |
subrata_modak | 04f47a1 | 2009-09-18 17:44:08 +0000 | [diff] [blame] | 338 | if (lseek64(fd, CHUNK(chunk), 0) < 0) { |
Cyril Hrubis | 526fdf8 | 2014-12-04 14:35:01 +0100 | [diff] [blame] | 339 | tst_brkm(TFAIL, |
| 340 | NULL, "\tTest[%d]: lseek64(0) fail at %" |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 341 | PRIx64 "x, errno = %d.", me, |
| 342 | CHUNK(chunk), errno); |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 343 | } |
| 344 | if ((xfr = readv(fd, &r_iovec[0], MAXIOVCNT)) < 0) { |
Cyril Hrubis | 526fdf8 | 2014-12-04 14:35:01 +0100 | [diff] [blame] | 345 | tst_brkm(TFAIL, |
| 346 | NULL, "\tTest[%d]: readv fail at %" PRIx64 |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 347 | "x, errno = %d.", me, CHUNK(chunk), |
| 348 | errno); |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 349 | } |
| 350 | /* |
| 351 | * If chunk beyond EOF just write on it. |
| 352 | * Else if bit off, haven't seen it yet. |
| 353 | * Else, have. Verify values. |
| 354 | */ |
| 355 | if (xfr == 0) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 356 | bits[chunk / 8] |= (1 << (chunk % 8)); |
| 357 | } else if ((bits[chunk / 8] & (1 << (chunk % 8))) == 0) { |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 358 | if (xfr != csize) { |
Cyril Hrubis | 526fdf8 | 2014-12-04 14:35:01 +0100 | [diff] [blame] | 359 | tst_brkm(TFAIL, |
| 360 | NULL, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 361 | "\tTest[%d]: xfr=%d != %d, zero read.", |
| 362 | me, xfr, csize); |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 363 | } |
subrata_modak | 04f47a1 | 2009-09-18 17:44:08 +0000 | [diff] [blame] | 364 | for (i = 0; i < MAXIOVCNT; i++) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 365 | if (memcmp |
| 366 | (r_iovec[i].iov_base, |
| 367 | val0_iovec[i].iov_base, |
| 368 | r_iovec[i].iov_len)) { |
| 369 | tst_resm(TFAIL, |
| 370 | "\tTest[%d] bad verify @ 0x%" |
| 371 | PRIx64 |
| 372 | " for val %d count %d xfr %d.", |
| 373 | me, CHUNK(chunk), val0, |
| 374 | count, xfr); |
subrata_modak | 04f47a1 | 2009-09-18 17:44:08 +0000 | [diff] [blame] | 375 | ft_dumpiov(&r_iovec[i]); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 376 | ft_dumpbits(bits, |
| 377 | (nchunks + 7) / 8); |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 378 | tst_exit(); |
| 379 | } |
| 380 | } |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 381 | bits[chunk / 8] |= (1 << (chunk % 8)); |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 382 | ++count; |
| 383 | } else { |
| 384 | if (xfr != csize) { |
Cyril Hrubis | 526fdf8 | 2014-12-04 14:35:01 +0100 | [diff] [blame] | 385 | tst_brkm(TFAIL, |
| 386 | NULL, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 387 | "\tTest[%d]: xfr=%d != %d, val read.", |
| 388 | me, xfr, csize); |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 389 | } |
| 390 | ++collide; |
subrata_modak | 04f47a1 | 2009-09-18 17:44:08 +0000 | [diff] [blame] | 391 | for (i = 0; i < MAXIOVCNT; i++) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 392 | if (memcmp |
| 393 | (r_iovec[i].iov_base, |
| 394 | val_iovec[i].iov_base, |
| 395 | r_iovec[i].iov_len)) { |
| 396 | tst_resm(TFAIL, |
| 397 | "\tTest[%d] bad verify @ 0x%" |
| 398 | PRIx64 |
| 399 | " for val %d count %d xfr %d.", |
| 400 | me, CHUNK(chunk), val, |
| 401 | count, xfr); |
subrata_modak | 04f47a1 | 2009-09-18 17:44:08 +0000 | [diff] [blame] | 402 | ft_dumpiov(&r_iovec[i]); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 403 | ft_dumpbits(bits, |
| 404 | (nchunks + 7) / 8); |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 405 | tst_exit(); |
| 406 | } |
| 407 | } |
| 408 | } |
| 409 | /* |
| 410 | * Write it. |
| 411 | */ |
subrata_modak | 04f47a1 | 2009-09-18 17:44:08 +0000 | [diff] [blame] | 412 | if (lseek64(fd, -xfr, 1) < 0) { |
Cyril Hrubis | 526fdf8 | 2014-12-04 14:35:01 +0100 | [diff] [blame] | 413 | tst_brkm(TFAIL, |
| 414 | NULL, "\tTest[%d]: lseek64(1) fail at %" |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 415 | PRIx64 ", errno = %d.", me, |
| 416 | CHUNK(chunk), errno); |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 417 | } |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 418 | if ((xfr = |
| 419 | writev(fd, &val_iovec[0], MAXIOVCNT)) < csize) { |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 420 | if (errno == ENOSPC) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 421 | tst_resm(TFAIL, |
| 422 | "\tTest[%d]: no space, exiting.", |
| 423 | me); |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 424 | fsync(fd); |
| 425 | tst_exit(); |
| 426 | } |
Cyril Hrubis | 526fdf8 | 2014-12-04 14:35:01 +0100 | [diff] [blame] | 427 | tst_brkm(TFAIL, |
| 428 | NULL, "\tTest[%d]: writev fail at %" PRIx64 |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 429 | "x xfr %d, errno = %d.", me, |
| 430 | CHUNK(chunk), xfr, errno); |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 431 | } |
| 432 | /* |
| 433 | * If hit "misc" interval, do it. |
| 434 | */ |
| 435 | if (misc_intvl && --whenmisc <= 0) { |
subrata_modak | 04f47a1 | 2009-09-18 17:44:08 +0000 | [diff] [blame] | 436 | domisc(me, fd); |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 437 | whenmisc = NEXTMISC; |
| 438 | } |
| 439 | if (count + collide > 2 * nchunks) |
| 440 | break; |
| 441 | } |
| 442 | |
| 443 | /* |
| 444 | * End of iteration, maybe before doing all chunks. |
| 445 | */ |
| 446 | |
| 447 | if (count < nchunks) { |
vapier | af64a87 | 2006-02-15 06:47:36 +0000 | [diff] [blame] | 448 | //tst_resm(TINFO, "\tTest{%d} val %d stopping @ %d, collide = {%d}.", |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 449 | // me, val, count, collide); |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 450 | for (i = 0; i < nchunks; i++) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 451 | if ((bits[i / 8] & (1 << (i % 8))) == 0) { |
| 452 | if (lseek64(fd, CHUNK(i), 0) < |
| 453 | (off64_t) 0) { |
Cyril Hrubis | 526fdf8 | 2014-12-04 14:35:01 +0100 | [diff] [blame] | 454 | tst_brkm(TFAIL, |
| 455 | NULL, "\tTest[%d]: lseek64 fail at %" |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 456 | PRIx64 |
| 457 | "x, errno = %d.", me, |
| 458 | CHUNK(i), errno); |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 459 | } |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 460 | if (writev(fd, &val_iovec[0], MAXIOVCNT) |
| 461 | != csize) { |
Cyril Hrubis | 526fdf8 | 2014-12-04 14:35:01 +0100 | [diff] [blame] | 462 | tst_brkm(TFAIL, |
| 463 | NULL, "\tTest[%d]: writev fail at %" |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 464 | PRIx64 |
| 465 | "x, errno = %d.", me, |
| 466 | CHUNK(i), errno); |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 467 | } |
| 468 | } |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | fsync(fd); |
subrata_modak | 04f47a1 | 2009-09-18 17:44:08 +0000 | [diff] [blame] | 473 | ++misc_cnt[m_fsync]; |
vapier | af64a87 | 2006-02-15 06:47:36 +0000 | [diff] [blame] | 474 | //tst_resm(TINFO, "\tTest[%d] val %d done, count = %d, collide = %d.", |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 475 | // me, val, count, collide); |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 476 | //for (i = 0; i < NMISC; i++) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 477 | // tst_resm(TINFO, "\t\tTest[%d]: %d %s's.", me, misc_cnt[i], m_str[i]); |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 478 | val0 = val++; |
| 479 | } |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | /* |
| 483 | * domisc() |
| 484 | * Inject misc syscalls into the thing. |
| 485 | */ |
subrata_modak | 04f47a1 | 2009-09-18 17:44:08 +0000 | [diff] [blame] | 486 | static void domisc(int me, int fd) |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 487 | { |
subrata_modak | 04f47a1 | 2009-09-18 17:44:08 +0000 | [diff] [blame] | 488 | enum m_type type; |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 489 | |
| 490 | if (misc_flag) { |
| 491 | type = m_fsync; |
| 492 | misc_flag = 0; |
| 493 | } else { |
| 494 | type = m_sync;; |
| 495 | misc_flag = 1; |
| 496 | } |
subrata_modak | 04f47a1 | 2009-09-18 17:44:08 +0000 | [diff] [blame] | 497 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 498 | switch (type) { |
| 499 | case m_fsync: |
| 500 | if (fsync(fd) < 0) { |
Cyril Hrubis | 526fdf8 | 2014-12-04 14:35:01 +0100 | [diff] [blame] | 501 | tst_brkm(TFAIL, NULL, "\tTest[%d]: fsync error %d.", |
| 502 | me, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 503 | errno); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 504 | } |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 505 | break; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 506 | case m_sync: |
| 507 | sync(); |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 508 | break; |
| 509 | } |
subrata_modak | 04f47a1 | 2009-09-18 17:44:08 +0000 | [diff] [blame] | 510 | |
| 511 | ++misc_cnt[type]; |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 512 | } |
| 513 | |
subrata_modak | 04f47a1 | 2009-09-18 17:44:08 +0000 | [diff] [blame] | 514 | static void term(int sig LTP_ATTRIBUTE_UNUSED) |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 515 | { |
subrata_modak | 04f47a1 | 2009-09-18 17:44:08 +0000 | [diff] [blame] | 516 | int i; |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 517 | |
vapier | af64a87 | 2006-02-15 06:47:36 +0000 | [diff] [blame] | 518 | tst_resm(TINFO, "\tterm -[%d]- got sig term.", getpid()); |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 519 | |
| 520 | if (parent_pid == getpid()) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 521 | for (i = 0; i < nchild; i++) |
subrata_modak | 04f47a1 | 2009-09-18 17:44:08 +0000 | [diff] [blame] | 522 | if (pidlist[i]) |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 523 | kill(pidlist[i], SIGTERM); |
subrata_modak | 04f47a1 | 2009-09-18 17:44:08 +0000 | [diff] [blame] | 524 | return; |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | tst_exit(); |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 528 | } |
| 529 | |
subrata_modak | 04f47a1 | 2009-09-18 17:44:08 +0000 | [diff] [blame] | 530 | void cleanup(void) |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 531 | { |
robbiew | 24e30ab | 2003-01-07 20:53:21 +0000 | [diff] [blame] | 532 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 533 | tst_rmdir(); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 534 | } |