• 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  *	ftest05.c -- test file I/O (ported from SPIE, section2/filesuite/ftest6.c, by Airong Zhang)
24  *
25  * 	this is the same as ftest1, except that it uses lseek64
26  *
27  * CALLS
28  *	lseek64, read, write
29  *	truncate, ftruncate, fsync, sync, fstat
30  *
31  * ALGORITHM
32  *	A bitmap is used to map pieces of a file.
33  *      Loop: pick a random piece of the file
34  *            if we haven't seen it before make sure it is zero,
35  *            write pattern
36  *            if we have seen it before make sure correct pattern.
37  *
38  *      This was originally written by rbk - was program tfio.c
39  *	Modified by dale to integrate with test suites.
40  *
41  * RESTRICTIONS
42  *	Runs a long time with default args - can take others on input
43  *	line.  Use with "term mode".
44  *	If run on vax the ftruncate will not be random - will always go to
45  *	start of file.  NOTE: produces a very high load average!!
46  *
47  * CAUTION!!
48  *	If a file is supplied to this program with the "-f" option
49  *	it will be removed with a system("rm -rf filename") call.
50  *
51  */
52 
53 #define _XOPEN_SOURCE 500
54 #define _LARGEFILE64_SOURCE 1
55 #include <stdio.h>
56 #include <sys/types.h>
57 #include <sys/wait.h>
58 #include <sys/stat.h>
59 #include <errno.h>
60 #include <fcntl.h>
61 #include <signal.h>
62 #include <unistd.h>
63 #include <inttypes.h>
64 #include <sys/param.h>
65 #include "test.h"
66 #include "safe_macros.h"
67 #include "libftest.h"
68 
69 char *TCID = "ftest05";
70 int TST_TOTAL = 1;
71 
72 static void setup(void);
73 static void runtest();
74 static void dotest(int, int, int);
75 static void domisc(int, int, char *);
76 static void term(int sig);
77 static void cleanup(void);
78 
79 #define PASSED 1
80 #define FAILED 0
81 
82 #define MAXCHILD	25
83 #define K_1		1024
84 #define K_2		2048
85 #define K_4		4096
86 
87 static int csize;		/* chunk size */
88 static int iterations;		/* # total iterations */
89 static off64_t max_size;	/* max file size */
90 static const int misc_intvl = 10;		/* for doing misc things; 0 ==> no */
91 static int nchild;		/* how many children */
92 static int fd;			/* file descriptor used by child */
93 static int parent_pid;
94 static int pidlist[MAXCHILD];
95 static char test_name[2];	/* childs test directory name */
96 
97 static char fuss[MAXPATHLEN];	/* directory to do this in */
98 static char homedir[MAXPATHLEN];	/* where we started */
99 
100 static int local_flag;
101 
main(int ac,char * av[])102 int main(int ac, char *av[])
103 {
104 	int lc;
105 
106 	tst_parse_opts(ac, av, NULL, NULL);
107 
108 	setup();
109 
110 	local_flag = PASSED;
111 
112 	for (lc = 0; TEST_LOOPING(lc); lc++) {
113 
114 		runtest();
115 
116 		if (local_flag == PASSED)
117 			tst_resm(TPASS, "Test passed.");
118 		else
119 			tst_resm(TFAIL, "Test failed.");
120 	}
121 
122 	cleanup();
123 	return 1;
124 }
125 
setup(void)126 static void setup(void)
127 {
128 	/*
129 	 * Make a directory to do this in; ignore error if already exists.
130 	 * Save starting directory.
131 	 */
132 	tst_tmpdir();
133 	getcwd(homedir, sizeof(homedir));
134 	parent_pid = getpid();
135 
136 	if (!fuss[0])
137 		sprintf(fuss, "./ftest05.%d", getpid());
138 
139 	mkdir(fuss, 0755);
140 
141 	SAFE_CHDIR(NULL, fuss);
142 
143 	/*
144 	 * Default values for run conditions.
145 	 */
146 	iterations = 10;
147 	nchild = 5;
148 	csize = K_2;		/* should run with 1, 2, and 4 K sizes */
149 	max_size = K_1 * K_1;
150 
151 	if (sigset(SIGTERM, term) == SIG_ERR) {
152 		tst_brkm(TBROK | TERRNO, NULL,
153 			 "sigset (signo = SIGTERM) failed");
154 	}
155 
156 	local_flag = PASSED;
157 }
158 
runtest(void)159 static void runtest(void)
160 {
161 	int child, count, i, nwait, pid, status;
162 
163 	nwait = 0;
164 
165 	for (i = 0; i < nchild; i++) {
166 		test_name[0] = 'a' + i;
167 		test_name[1] = '\0';
168 		fd = SAFE_OPEN(NULL, test_name, O_RDWR | O_CREAT | O_TRUNC,
169 			       0666);
170 
171 		if ((child = fork()) == 0) {
172 			dotest(nchild, i, fd);
173 			tst_exit();
174 		}
175 
176 		close(fd);
177 
178 		if (child < 0) {
179 			tst_brkm(TBROK | TERRNO, NULL, "fork failed");
180 		} else {
181 			pidlist[i] = child;
182 			nwait++;
183 		}
184 	}
185 
186 	/*
187 	 * Wait for children to finish.
188 	 */
189 	count = 0;
190 	while (1) {
191 		if ((child = wait(&status)) >= 0) {
192 			if (status != 0) {
193 				tst_resm(TFAIL,
194 					 "\tTest{%d} failed, expected 0 exit.",
195 					 child);
196 				local_flag = FAILED;
197 			}
198 			++count;
199 		} else if (errno != EINTR)
200 			break;
201 	}
202 
203 	/*
204 	 * Should have collected all children.
205 	 */
206 	if (count != nwait) {
207 		tst_resm(TFAIL, "\tWrong # children waited on, count = %d",
208 			 count);
209 		local_flag = FAILED;
210 	}
211 
212 	chdir(homedir);
213 	pid = fork();
214 
215 	if (pid < 0) {
216 		tst_brkm(TBROK | TERRNO, sync, "fork failed");
217 		tst_exit();
218 	}
219 
220 	if (pid == 0) {
221 		execl("/bin/rm", "rm", "-rf", fuss, NULL);
222 		exit(1);
223 	}
224 
225 	wait(&status);
226 
227 	if (status) {
228 		tst_resm(TINFO, "CAUTION - ftest05, '%s' may not be removed",
229 			 fuss);
230 	}
231 
232 	sync();
233 }
234 
235 /*
236  * dotest()
237  *	Children execute this.
238  *
239  * Randomly read/mod/write chunks with known pattern and check.
240  * When fill sectors, iterate.
241  */
242 
243 #define	NMISC	4
244 enum m_type { m_fsync, m_trunc, m_sync, m_fstat };
245 char *m_str[] = { "fsync", "trunc", "sync", "fstat" };
246 
247 int misc_cnt[NMISC];		/* counts # of each kind of misc */
248 int file_max;			/* file-max size */
249 int nchunks;
250 int last_trunc = -1;
251 int tr_flag;
252 enum m_type type = m_fsync;
253 
254 #define	CHUNK(i)	(((off64_t)i) * csize)
255 #define	NEXTMISC	((rand() % misc_intvl) + 5)
256 
dotest(int testers,int me,int fd)257 static void dotest(int testers, int me, int fd)
258 {
259 	int i, count, collide, chunk, whenmisc, xfr;
260 	char *bits, *hold_bits, *buf, *val_buf, *zero_buf;
261 	char val;
262 	struct stat stat;
263 
264 	nchunks = max_size / csize;
265 
266 	if ((bits = calloc((nchunks + 7) / 8, 1)) == NULL) {
267 		perror("\tmalloc (bits)");
268 		exit(1);
269 	}
270 
271 	if ((hold_bits = calloc((nchunks + 7) / 8, 1)) == NULL) {
272 		perror("\tmalloc (bold_bits)");
273 		exit(1);
274 	}
275 
276 	if ((buf = (calloc(csize, 1))) == NULL) {
277 		perror("\tmalloc (buf)");
278 		exit(1);
279 	}
280 
281 	if ((val_buf = (calloc(csize, 1))) == NULL) {
282 		perror("\tmalloc (val_buf)");
283 		exit(1);
284 	}
285 
286 	if ((zero_buf = (calloc(csize, 1))) == NULL) {
287 		perror("\tmalloc (zero_buf)");
288 		exit(1);
289 	}
290 
291 	/*
292 	 * No init sectors; allow file to be sparse.
293 	 */
294 	val = (64 / testers) * me + 1;
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 
311 	srand(getpid());
312 	if (misc_intvl)
313 		whenmisc = NEXTMISC;
314 	while (iterations-- > 0) {
315 		for (i = 0; i < NMISC; i++)
316 			misc_cnt[i] = 0;
317 		ftruncate(fd, 0);
318 		file_max = 0;
319 		memset(bits, 0, (nchunks + 7) / 8);
320 		memset(hold_bits, 0, (nchunks + 7) / 8);
321 		memset(val_buf, val, csize);
322 		memset(zero_buf, 0, csize);
323 		count = 0;
324 		collide = 0;
325 		while (count < nchunks) {
326 			chunk = rand() % nchunks;
327 			/*
328 			 * Read it.
329 			 */
330 			if (lseek64(fd, CHUNK(chunk), 0) < (off64_t) 0) {
331 				tst_brkm(TFAIL | TERRNO, NULL,
332 					 "\tTest[%d]: lseek64(0) fail at %Lx",
333 					 me, CHUNK(chunk));
334 			}
335 			if ((xfr = read(fd, buf, csize)) < 0) {
336 				tst_brkm(TFAIL | TERRNO, NULL,
337 					 "\tTest[%d]: read fail at %Lx",
338 					 me, CHUNK(chunk));
339 			}
340 			/*
341 			 * If chunk beyond EOF just write on it.
342 			 * Else if bit off, haven't seen it yet.
343 			 * Else, have.  Verify values.
344 			 */
345 			//printf("%li %d", CHUNK(chunk), file_max );
346 			if (CHUNK(chunk) >= file_max) {
347 				bits[chunk / 8] |= (1 << (chunk % 8));
348 				++count;
349 			} else if ((bits[chunk / 8] & (1 << (chunk % 8))) == 0) {
350 				if (xfr != csize) {
351 					//tst_resm(TINFO, "\tTest[%d]: xfr=%d != %d, zero read.",
352 					//      me, xfr, csize);
353 					tst_exit();
354 				}
355 				if (memcmp(buf, zero_buf, csize)) {
356 					tst_resm(TFAIL,
357 						 "\tTest[%d] bad verify @ 0x%Lx for val %d count %d xfr %d file_max 0x%x, should be %d.",
358 						 me, CHUNK(chunk), val, count,
359 						 xfr, file_max, zero_buf[0]);
360 					tst_resm(TINFO,
361 						 "\tTest[%d]: last_trunc = 0x%x",
362 						 me, last_trunc);
363 					fstat(fd, &stat);
364 					tst_resm(TINFO,
365 						 "\tStat: size=%llx, ino=%x",
366 						 stat.st_size, (unsigned)stat.st_ino);
367 					sync();
368 					ft_dumpbuf(buf, csize);
369 					ft_dumpbits(bits, (nchunks + 7) / 8);
370 					ft_orbits(hold_bits, bits,
371 						  (nchunks + 7) / 8);
372 					tst_resm(TINFO, "\tHold ");
373 					ft_dumpbits(hold_bits,
374 						    (nchunks + 7) / 8);
375 					tst_exit();
376 				}
377 				bits[chunk / 8] |= (1 << (chunk % 8));
378 				++count;
379 			} else {
380 				if (xfr != csize) {
381 					tst_brkm(TFAIL, NULL,
382 						 "\tTest[%d]: xfr=%d != %d, val read.",
383 						 me, xfr, csize);
384 				}
385 				++collide;
386 				if (memcmp(buf, val_buf, csize)) {
387 					tst_resm(TFAIL,
388 						 "\tTest[%d] bad verify @ 0x%Lx for val %d count %d xfr %d file_max 0x%x.",
389 						 me, CHUNK(chunk), val, count,
390 						 xfr, file_max);
391 					tst_resm(TINFO,
392 						 "\tTest[%d]: last_trunc = 0x%x",
393 						 me, last_trunc);
394 					fstat(fd, &stat);
395 					tst_resm(TINFO,
396 						 "\tStat: size=%llx, ino=%x",
397 						 stat.st_size, (unsigned)stat.st_ino);
398 					sync();
399 					ft_dumpbuf(buf, csize);
400 					ft_dumpbits(bits, (nchunks + 7) / 8);
401 					ft_orbits(hold_bits, bits,
402 						  (nchunks + 7) / 8);
403 					tst_resm(TINFO, "\tHold ");
404 					ft_dumpbits(hold_bits,
405 						    (nchunks + 7) / 8);
406 					tst_exit();
407 				}
408 			}
409 			/*
410 			 * Write it.
411 			 */
412 			if (lseek64(fd, -((off64_t) xfr), 1) < (off64_t) 0) {
413 				tst_brkm(TFAIL | TERRNO, NULL,
414 					 "\tTest[%d]: lseek64(1) fail at %Lx",
415 					 me, CHUNK(chunk));
416 			}
417 			if ((xfr = write(fd, val_buf, csize)) < csize) {
418 				if (errno == ENOSPC) {
419 					tst_resm(TFAIL,
420 						 "\tTest[%d]: no space, exiting.",
421 						 me);
422 					fsync(fd);
423 				} else {
424 					tst_resm(TFAIL | TERRNO,
425 						 "\tTest[%d]: write fail at %Lx xfr %d",
426 						 me, CHUNK(chunk), xfr);
427 				}
428 				tst_exit();
429 			}
430 			if (CHUNK(chunk) + csize > file_max)
431 				file_max = CHUNK(chunk) + csize;
432 			/*
433 			 * If hit "misc" interval, do it.
434 			 */
435 			if (misc_intvl && --whenmisc <= 0) {
436 				ft_orbits(hold_bits, bits, (nchunks + 7) / 8);
437 				domisc(me, fd, bits);
438 				whenmisc = NEXTMISC;
439 			}
440 			if (count + collide > 2 * nchunks)
441 				break;
442 		}
443 
444 		/*
445 		 * End of iteration, maybe before doing all chunks.
446 		 */
447 
448 		fsync(fd);
449 		++misc_cnt[m_fsync];
450 		//tst_resm(TINFO, "\tTest{%d} val %d done, count = %d, collide = {%d}",
451 		//              me, val, count, collide);
452 		//for (i = 0; i < NMISC; i++)
453 		//      tst_resm(TINFO, "\t\tTest{%d}: {%d} %s's.", me, misc_cnt[i], m_str[i]);
454 		++val;
455 	}
456 }
457 
458 /*
459  * domisc()
460  *	Inject misc syscalls into the thing.
461  */
domisc(int me,int fd,char * bits)462 static void domisc(int me, int fd, char *bits)
463 {
464 	int chunk;
465 	struct stat sb;
466 
467 	if (type > m_fstat)
468 		type = m_fsync;
469 
470 	switch (type) {
471 	case m_fsync:
472 		if (fsync(fd) < 0) {
473 			tst_resm(TFAIL | TERRNO, "\tTest[%d]: fsync error", me);
474 		}
475 		break;
476 	case m_trunc:
477 		chunk = rand() % (file_max / csize);
478 		file_max = CHUNK(chunk);
479 		last_trunc = file_max;
480 		if (tr_flag) {
481 			if (ftruncate(fd, file_max) < 0) {
482 				tst_brkm(TFAIL | TERRNO, NULL,
483 					 "\tTest[%d]: ftruncate error @ 0x%x.",
484 					 me, file_max);
485 			}
486 			tr_flag = 0;
487 		} else {
488 			if (truncate(test_name, file_max) < 0) {
489 				tst_brkm(TFAIL | TERRNO, NULL,
490 					 "\tTest[%d]: truncate error @ 0x%x.",
491 					 me, file_max);
492 			}
493 			tr_flag = 1;
494 		}
495 		for (; chunk % 8 != 0; chunk++)
496 			bits[chunk / 8] &= ~(1 << (chunk % 8));
497 		for (; chunk < nchunks; chunk += 8)
498 			bits[chunk / 8] = 0;
499 		break;
500 	case m_sync:
501 		sync();
502 		break;
503 	case m_fstat:
504 		if (fstat(fd, &sb) < 0) {
505 			tst_brkm(TFAIL | TERRNO, NULL,
506 				 "\tTest[%d]: fstat() error.", me);
507 		}
508 		if (sb.st_size != file_max) {
509 			tst_brkm(TFAIL, NULL,
510 				 "\tTest[%d]: fstat() mismatch; st_size=%"
511 				 PRIx64 ",file_max=%x.", me,
512 				 (int64_t) sb.st_size, file_max);
513 		}
514 		break;
515 	}
516 	++misc_cnt[type];
517 	++type;
518 }
519 
520 /* term()
521  *
522  *	This is called when a SIGTERM signal arrives.
523  */
term(int sig LTP_ATTRIBUTE_UNUSED)524 static void term(int sig LTP_ATTRIBUTE_UNUSED)
525 {
526 	int i;
527 
528 	tst_resm(TINFO, "\tterm -[%d]- got sig term.", getpid());
529 
530 	/*
531 	 * If run by hand we like to have the parent send the signal to
532 	 * the child processes.  This makes life easy.
533 	 */
534 	if (parent_pid == getpid()) {
535 		for (i = 0; i < nchild; i++)
536 			if (pidlist[i])	/* avoid embarassment */
537 				kill(pidlist[i], SIGTERM);
538 		return;
539 	}
540 
541 	tst_resm(TINFO, "\tunlinking '%s'", test_name);
542 
543 	close(fd);
544 
545 	if (unlink(test_name))
546 		tst_resm(TBROK, "Unlink of '%s' failed, errno = %d.",
547 			 test_name, errno);
548 	else
549 		tst_resm(TINFO, "Unlink of '%s' successful.", test_name);
550 
551 	tst_exit();
552 }
553 
cleanup(void)554 static void cleanup(void)
555 {
556 
557 	tst_rmdir();
558 	tst_exit();
559 }
560