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