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