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