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 int misc_intvl;		/* 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 	misc_intvl = 10;
151 
152 	if (sigset(SIGTERM, term) == SIG_ERR) {
153 		tst_brkm(TBROK | TERRNO, NULL,
154 			 "sigset (signo = SIGTERM) failed");
155 	}
156 
157 	local_flag = PASSED;
158 }
159 
runtest(void)160 static void runtest(void)
161 {
162 	int child, count, i, nwait, pid, status;
163 
164 	nwait = 0;
165 
166 	for (i = 0; i < nchild; i++) {
167 		test_name[0] = 'a' + i;
168 		test_name[1] = '\0';
169 		fd = SAFE_OPEN(NULL, test_name, O_RDWR | O_CREAT | O_TRUNC,
170 			       0666);
171 
172 		if ((child = fork()) == 0) {
173 			dotest(nchild, i, fd);
174 			tst_exit();
175 		}
176 
177 		close(fd);
178 
179 		if (child < 0) {
180 			tst_brkm(TBROK | TERRNO, NULL, "fork failed");
181 		} else {
182 			pidlist[i] = child;
183 			nwait++;
184 		}
185 	}
186 
187 	/*
188 	 * Wait for children to finish.
189 	 */
190 	count = 0;
191 	while (1) {
192 		if ((child = wait(&status)) >= 0) {
193 			if (status != 0) {
194 				tst_resm(TFAIL,
195 					 "\tTest{%d} failed, expected 0 exit.",
196 					 child);
197 				local_flag = FAILED;
198 			}
199 			++count;
200 		} else if (errno != EINTR)
201 			break;
202 	}
203 
204 	/*
205 	 * Should have collected all children.
206 	 */
207 	if (count != nwait) {
208 		tst_resm(TFAIL, "\tWrong # children waited on, count = %d",
209 			 count);
210 		local_flag = FAILED;
211 	}
212 
213 	chdir(homedir);
214 	pid = fork();
215 
216 	if (pid < 0) {
217 		tst_brkm(TBROK | TERRNO, sync, "fork failed");
218 		tst_exit();
219 	}
220 
221 	if (pid == 0) {
222 		execl("/bin/rm", "rm", "-rf", fuss, NULL);
223 		exit(1);
224 	}
225 
226 	wait(&status);
227 
228 	if (status) {
229 		tst_resm(TINFO, "CAUTION - ftest05, '%s' may not be removed",
230 			 fuss);
231 	}
232 
233 	sync();
234 }
235 
236 /*
237  * dotest()
238  *	Children execute this.
239  *
240  * Randomly read/mod/write chunks with known pattern and check.
241  * When fill sectors, iterate.
242  */
243 
244 #define	NMISC	4
245 enum m_type { m_fsync, m_trunc, m_sync, m_fstat };
246 char *m_str[] = { "fsync", "trunc", "sync", "fstat" };
247 
248 int misc_cnt[NMISC];		/* counts # of each kind of misc */
249 int file_max;			/* file-max size */
250 int nchunks;
251 int last_trunc = -1;
252 int tr_flag;
253 enum m_type type = m_fsync;
254 
255 #define	CHUNK(i)	(((off64_t)i) * csize)
256 #define	NEXTMISC	((rand() % misc_intvl) + 5)
257 
dotest(int testers,int me,int fd)258 static void dotest(int testers, int me, int fd)
259 {
260 	int i, count, collide, chunk, whenmisc, xfr;
261 	char *bits, *hold_bits, *buf, *val_buf, *zero_buf;
262 	char val;
263 	struct stat stat;
264 
265 	nchunks = max_size / csize;
266 
267 	if ((bits = calloc((nchunks + 7) / 8, 1)) == NULL) {
268 		perror("\tmalloc (bits)");
269 		exit(1);
270 	}
271 
272 	if ((hold_bits = calloc((nchunks + 7) / 8, 1)) == NULL) {
273 		perror("\tmalloc (bold_bits)");
274 		exit(1);
275 	}
276 
277 	if ((buf = (calloc(csize, 1))) == NULL) {
278 		perror("\tmalloc (buf)");
279 		exit(1);
280 	}
281 
282 	if ((val_buf = (calloc(csize, 1))) == NULL) {
283 		perror("\tmalloc (val_buf)");
284 		exit(1);
285 	}
286 
287 	if ((zero_buf = (calloc(csize, 1))) == NULL) {
288 		perror("\tmalloc (zero_buf)");
289 		exit(1);
290 	}
291 
292 	/*
293 	 * No init sectors; allow file to be sparse.
294 	 */
295 	val = (64 / testers) * me + 1;
296 
297 	/*
298 	 * For each iteration:
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.
310 	 */
311 
312 	srand(getpid());
313 	if (misc_intvl)
314 		whenmisc = NEXTMISC;
315 	while (iterations-- > 0) {
316 		for (i = 0; i < NMISC; i++)
317 			misc_cnt[i] = 0;
318 		ftruncate(fd, 0);
319 		file_max = 0;
320 		memset(bits, 0, (nchunks + 7) / 8);
321 		memset(hold_bits, 0, (nchunks + 7) / 8);
322 		memset(val_buf, val, csize);
323 		memset(zero_buf, 0, csize);
324 		count = 0;
325 		collide = 0;
326 		while (count < nchunks) {
327 			chunk = rand() % nchunks;
328 			/*
329 			 * Read it.
330 			 */
331 			if (lseek64(fd, CHUNK(chunk), 0) < (off64_t) 0) {
332 				tst_brkm(TFAIL | TERRNO, NULL,
333 					 "\tTest[%d]: lseek64(0) fail at %Lx",
334 					 me, CHUNK(chunk));
335 			}
336 			if ((xfr = read(fd, buf, csize)) < 0) {
337 				tst_brkm(TFAIL | TERRNO, NULL,
338 					 "\tTest[%d]: read fail at %Lx",
339 					 me, CHUNK(chunk));
340 			}
341 			/*
342 			 * If chunk beyond EOF just write on it.
343 			 * Else if bit off, haven't seen it yet.
344 			 * Else, have.  Verify values.
345 			 */
346 			//printf("%li %d", CHUNK(chunk), file_max );
347 			if (CHUNK(chunk) >= file_max) {
348 				bits[chunk / 8] |= (1 << (chunk % 8));
349 				++count;
350 			} else if ((bits[chunk / 8] & (1 << (chunk % 8))) == 0) {
351 				if (xfr != csize) {
352 					//tst_resm(TINFO, "\tTest[%d]: xfr=%d != %d, zero read.",
353 					//      me, xfr, csize);
354 					tst_exit();
355 				}
356 				if (memcmp(buf, zero_buf, csize)) {
357 					tst_resm(TFAIL,
358 						 "\tTest[%d] bad verify @ 0x%Lx for val %d count %d xfr %d file_max 0x%x, should be %d.",
359 						 me, CHUNK(chunk), val, count,
360 						 xfr, file_max, zero_buf[0]);
361 					tst_resm(TINFO,
362 						 "\tTest[%d]: last_trunc = 0x%x",
363 						 me, last_trunc);
364 					fstat(fd, &stat);
365 					tst_resm(TINFO,
366 						 "\tStat: size=%llx, ino=%x",
367 						 stat.st_size, (unsigned)stat.st_ino);
368 					sync();
369 					ft_dumpbuf(buf, csize);
370 					ft_dumpbits(bits, (nchunks + 7) / 8);
371 					ft_orbits(hold_bits, bits,
372 						  (nchunks + 7) / 8);
373 					tst_resm(TINFO, "\tHold ");
374 					ft_dumpbits(hold_bits,
375 						    (nchunks + 7) / 8);
376 					tst_exit();
377 				}
378 				bits[chunk / 8] |= (1 << (chunk % 8));
379 				++count;
380 			} else {
381 				if (xfr != csize) {
382 					tst_brkm(TFAIL, NULL,
383 						 "\tTest[%d]: xfr=%d != %d, val read.",
384 						 me, xfr, csize);
385 				}
386 				++collide;
387 				if (memcmp(buf, val_buf, csize)) {
388 					tst_resm(TFAIL,
389 						 "\tTest[%d] bad verify @ 0x%Lx for val %d count %d xfr %d file_max 0x%x.",
390 						 me, CHUNK(chunk), val, count,
391 						 xfr, file_max);
392 					tst_resm(TINFO,
393 						 "\tTest[%d]: last_trunc = 0x%x",
394 						 me, last_trunc);
395 					fstat(fd, &stat);
396 					tst_resm(TINFO,
397 						 "\tStat: size=%llx, ino=%x",
398 						 stat.st_size, (unsigned)stat.st_ino);
399 					sync();
400 					ft_dumpbuf(buf, csize);
401 					ft_dumpbits(bits, (nchunks + 7) / 8);
402 					ft_orbits(hold_bits, bits,
403 						  (nchunks + 7) / 8);
404 					tst_resm(TINFO, "\tHold ");
405 					ft_dumpbits(hold_bits,
406 						    (nchunks + 7) / 8);
407 					tst_exit();
408 				}
409 			}
410 			/*
411 			 * Write it.
412 			 */
413 			if (lseek64(fd, -((off64_t) xfr), 1) < (off64_t) 0) {
414 				tst_brkm(TFAIL | TERRNO, NULL,
415 					 "\tTest[%d]: lseek64(1) fail at %Lx",
416 					 me, CHUNK(chunk));
417 			}
418 			if ((xfr = write(fd, val_buf, csize)) < csize) {
419 				if (errno == ENOSPC) {
420 					tst_resm(TFAIL,
421 						 "\tTest[%d]: no space, exiting.",
422 						 me);
423 					fsync(fd);
424 				} else {
425 					tst_resm(TFAIL | TERRNO,
426 						 "\tTest[%d]: write fail at %Lx xfr %d",
427 						 me, CHUNK(chunk), xfr);
428 				}
429 				tst_exit();
430 			}
431 			if (CHUNK(chunk) + csize > file_max)
432 				file_max = CHUNK(chunk) + csize;
433 			/*
434 			 * If hit "misc" interval, do it.
435 			 */
436 			if (misc_intvl && --whenmisc <= 0) {
437 				ft_orbits(hold_bits, bits, (nchunks + 7) / 8);
438 				domisc(me, fd, bits);
439 				whenmisc = NEXTMISC;
440 			}
441 			if (count + collide > 2 * nchunks)
442 				break;
443 		}
444 
445 		/*
446 		 * End of iteration, maybe before doing all chunks.
447 		 */
448 
449 		fsync(fd);
450 		++misc_cnt[m_fsync];
451 		//tst_resm(TINFO, "\tTest{%d} val %d done, count = %d, collide = {%d}",
452 		//              me, val, count, collide);
453 		//for (i = 0; i < NMISC; i++)
454 		//      tst_resm(TINFO, "\t\tTest{%d}: {%d} %s's.", me, misc_cnt[i], m_str[i]);
455 		++val;
456 	}
457 }
458 
459 /*
460  * domisc()
461  *	Inject misc syscalls into the thing.
462  */
domisc(int me,int fd,char * bits)463 static void domisc(int me, int fd, char *bits)
464 {
465 	int chunk;
466 	struct stat sb;
467 
468 	if (type > m_fstat)
469 		type = m_fsync;
470 
471 	switch (type) {
472 	case m_fsync:
473 		if (fsync(fd) < 0) {
474 			tst_resm(TFAIL | TERRNO, "\tTest[%d]: fsync error", me);
475 		}
476 		break;
477 	case m_trunc:
478 		chunk = rand() % (file_max / csize);
479 		file_max = CHUNK(chunk);
480 		last_trunc = file_max;
481 		if (tr_flag) {
482 			if (ftruncate(fd, file_max) < 0) {
483 				tst_brkm(TFAIL | TERRNO, NULL,
484 					 "\tTest[%d]: ftruncate error @ 0x%x.",
485 					 me, file_max);
486 			}
487 			tr_flag = 0;
488 		} else {
489 			if (truncate(test_name, file_max) < 0) {
490 				tst_brkm(TFAIL | TERRNO, NULL,
491 					 "\tTest[%d]: truncate error @ 0x%x.",
492 					 me, file_max);
493 			}
494 			tr_flag = 1;
495 		}
496 		for (; chunk % 8 != 0; chunk++)
497 			bits[chunk / 8] &= ~(1 << (chunk % 8));
498 		for (; chunk < nchunks; chunk += 8)
499 			bits[chunk / 8] = 0;
500 		break;
501 	case m_sync:
502 		sync();
503 		break;
504 	case m_fstat:
505 		if (fstat(fd, &sb) < 0) {
506 			tst_brkm(TFAIL | TERRNO, NULL,
507 				 "\tTest[%d]: fstat() error.", me);
508 		}
509 		if (sb.st_size != file_max) {
510 			tst_brkm(TFAIL, NULL,
511 				 "\tTest[%d]: fstat() mismatch; st_size=%"
512 				 PRIx64 ",file_max=%x.", me,
513 				 (int64_t) sb.st_size, file_max);
514 		}
515 		break;
516 	}
517 	++misc_cnt[type];
518 	++type;
519 }
520 
521 /* term()
522  *
523  *	This is called when a SIGTERM signal arrives.
524  */
term(int sig LTP_ATTRIBUTE_UNUSED)525 static void term(int sig LTP_ATTRIBUTE_UNUSED)
526 {
527 	int i;
528 
529 	tst_resm(TINFO, "\tterm -[%d]- got sig term.", getpid());
530 
531 	/*
532 	 * If run by hand we like to have the parent send the signal to
533 	 * the child processes.  This makes life easy.
534 	 */
535 	if (parent_pid == getpid()) {
536 		for (i = 0; i < nchild; i++)
537 			if (pidlist[i])	/* avoid embarassment */
538 				kill(pidlist[i], SIGTERM);
539 		return;
540 	}
541 
542 	tst_resm(TINFO, "\tunlinking '%s'", test_name);
543 
544 	close(fd);
545 
546 	if (unlink(test_name))
547 		tst_resm(TBROK, "Unlink of '%s' failed, errno = %d.",
548 			 test_name, errno);
549 	else
550 		tst_resm(TINFO, "Unlink of '%s' successful.", test_name);
551 
552 	tst_exit();
553 }
554 
cleanup(void)555 static void cleanup(void)
556 {
557 
558 	tst_rmdir();
559 	tst_exit();
560 }
561