Import Upstream version 1.8.5
[hcoop/debian/openafs.git] / src / tests / fsx.c
1 /*
2 * Copyright (C) 1991, NeXT Computer, Inc. All Rights Reserverd.
3 *
4 * File: fsx.c
5 * Author: Avadis Tevanian, Jr.
6 *
7 * File system exerciser.
8 *
9 * Rewritten 8/98 by Conrad Minshall.
10 */
11
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #if defined(sun) || defined(_UWIN) || defined(__linux)
15 # include <sys/param.h>
16 # include <limits.h>
17 # include <time.h>
18 # include <strings.h>
19 # define MAP_FILE 0
20 #else
21 # include <sys/dirent.h>
22 #endif
23 #include <sys/file.h>
24 #include <sys/mman.h>
25 #include <limits.h>
26 #include <err.h>
27 #include <signal.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include <stdarg.h>
33 #include <errno.h>
34 #include <fcntl.h>
35
36 #if !defined L_SET
37 # define L_SET SEEK_SET
38 #endif
39 #if !defined L_INCR
40 # define L_INCR SEEK_CUR
41 #endif
42 #if !defined L_XTND
43 # define L_XTND SEEK_END
44 #endif
45
46 #define NUMPRINTCOLUMNS 32 /* # columns of data to print on each line */
47
48 /*
49 * A log entry is an operation and a bunch of arguments.
50 */
51
52 struct log_entry {
53 int operation;
54 int args[3];
55 };
56
57 #define LOGSIZE 1000
58
59 struct log_entry oplog[LOGSIZE]; /* the log */
60 int logptr = 0; /* current position in log */
61 int logcount = 0; /* total ops */
62
63 /*
64 * Define operations
65 */
66
67 #define OP_READ 1
68 #define OP_WRITE 2
69 #define OP_TRUNCATE 3
70 #define OP_CLOSEOPEN 4
71 #define OP_MAPREAD 5
72 #define OP_MAPWRITE 6
73 #define OP_SKIPPED 7
74
75 #ifndef PAGE_SIZE
76 #define PAGE_SIZE 4096
77 #endif
78 #define PAGE_MASK (PAGE_SIZE - 1)
79
80 char *original_buf; /* a pointer to the original data */
81 char *good_buf; /* a pointer to the correct data */
82 char *temp_buf; /* a pointer to the current data */
83 char *fname; /* name of our test file */
84 int fd; /* fd for our test file */
85
86 off_t file_size = 0;
87 off_t biggest = 0;
88 char state[256];
89 unsigned long testcalls = 0; /* calls to function "test" */
90
91 unsigned long simulatedopcount = 0; /* -b flag */
92 int closeprob = 0; /* -c flag */
93 int debug = 0; /* -d flag */
94 unsigned long debugstart = 0; /* -D flag */
95 unsigned long maxfilelen = 256 * 1024; /* -l flag */
96 int sizechecks = 1; /* -n flag disables them */
97 int maxoplen = 64 * 1024; /* -o flag */
98 int quiet = 0; /* -q flag */
99 unsigned long progressinterval = 0; /* -p flag */
100 int readbdy = 1; /* -r flag */
101 int style = 0; /* -s flag */
102 int truncbdy = 1; /* -t flag */
103 int writebdy = 1; /* -w flag */
104 long monitorstart = -1; /* -m flag */
105 long monitorend = -1; /* -m flag */
106 int lite = 0; /* -L flag */
107 long numops = -1; /* -N flag */
108 int randomoplen = 1; /* -O flag disables it */
109 int seed = 1; /* -S flag */
110 int mapped_writes = 1; /* -W flag disables */
111 int mapped_reads = 1; /* -R flag disables it */
112 int fsxgoodfd = 0;
113 FILE *fsxlogf = NULL;
114 int badoff = -1;
115 int closeopen = 0;
116
117
118 void
119 prt(char *fmt, ...)
120 {
121 va_list args;
122
123 va_start(args, fmt);
124 vfprintf(stdout, fmt, args);
125 if (fsxlogf)
126 vfprintf(fsxlogf, fmt, args);
127 va_end(args);
128 }
129
130 void
131 prterr(char *prefix)
132 {
133 prt("%s%s%s\n", prefix, prefix ? ": " : "", strerror(errno));
134 }
135
136
137 void
138 log4(int operation, int arg0, int arg1, int arg2)
139 {
140 struct log_entry *le;
141
142 le = &oplog[logptr];
143 le->operation = operation;
144 if (closeopen)
145 le->operation = ~le->operation;
146 le->args[0] = arg0;
147 le->args[1] = arg1;
148 le->args[2] = arg2;
149 logptr++;
150 logcount++;
151 if (logptr >= LOGSIZE)
152 logptr = 0;
153 }
154
155
156 void
157 logdump(void)
158 {
159 int i, count, down;
160 struct log_entry *lp;
161
162 prt("LOG DUMP (%d total operations):\n", logcount);
163 if (logcount < LOGSIZE) {
164 i = 0;
165 count = logcount;
166 } else {
167 i = logptr;
168 count = LOGSIZE;
169 }
170 for (; count > 0; count--) {
171 int opnum;
172
173 opnum = i + 1 + (logcount / LOGSIZE) * LOGSIZE;
174 prt("%d(%d mod 256): ", opnum, opnum % 256);
175 lp = &oplog[i];
176 if ((closeopen = lp->operation < 0))
177 lp->operation = ~lp->operation;
178
179 switch (lp->operation) {
180 case OP_MAPREAD:
181 prt("MAPREAD\t0x%x thru 0x%x\t(0x%x bytes)", lp->args[0],
182 lp->args[0] + lp->args[1] - 1, lp->args[1]);
183 if (badoff >= lp->args[0] && badoff < lp->args[0] + lp->args[1])
184 prt("\t***RRRR***");
185 break;
186 case OP_MAPWRITE:
187 prt("MAPWRITE 0x%x thru 0x%x\t(0x%x bytes)", lp->args[0],
188 lp->args[0] + lp->args[1] - 1, lp->args[1]);
189 if (badoff >= lp->args[0] && badoff < lp->args[0] + lp->args[1])
190 prt("\t******WWWW");
191 break;
192 case OP_READ:
193 prt("READ\t0x%x thru 0x%x\t(0x%x bytes)", lp->args[0],
194 lp->args[0] + lp->args[1] - 1, lp->args[1]);
195 if (badoff >= lp->args[0] && badoff < lp->args[0] + lp->args[1])
196 prt("\t***RRRR***");
197 break;
198 case OP_WRITE:
199 prt("WRITE\t0x%x thru 0x%x\t(0x%x bytes)", lp->args[0],
200 lp->args[0] + lp->args[1] - 1, lp->args[1]);
201 if (lp->args[0] > lp->args[2])
202 prt(" HOLE");
203 else if (lp->args[0] + lp->args[1] > lp->args[2])
204 prt(" EXTEND");
205 if ((badoff >= lp->args[0] || badoff >= lp->args[2])
206 && badoff < lp->args[0] + lp->args[1])
207 prt("\t***WWWW");
208 break;
209 case OP_TRUNCATE:
210 down = lp->args[0] < lp->args[1];
211 prt("TRUNCATE %s\tfrom 0x%x to 0x%x", down ? "DOWN" : "UP",
212 lp->args[1], lp->args[0]);
213 if (badoff >= lp->args[!down] && badoff < lp->args[!!down])
214 prt("\t******WWWW");
215 break;
216 case OP_SKIPPED:
217 prt("SKIPPED (no operation)");
218 break;
219 default:
220 prt("BOGUS LOG ENTRY (operation code = %d)!", lp->operation);
221 }
222 if (closeopen)
223 prt("\n\t\tCLOSE/OPEN");
224 prt("\n");
225 i++;
226 if (i == LOGSIZE)
227 i = 0;
228 }
229 }
230
231
232 void
233 save_buffer(char *buffer, off_t bufferlength, int fd)
234 {
235 off_t ret;
236 ssize_t byteswritten;
237
238 if (fd <= 0 || bufferlength == 0)
239 return;
240
241 if (bufferlength > SSIZE_MAX) {
242 prt("fsx flaw: overflow in save_buffer\n");
243 exit(67);
244 }
245 if (lite) {
246 off_t size_by_seek = lseek(fd, (off_t) 0, L_XTND);
247 if (size_by_seek == (off_t) - 1)
248 prterr("save_buffer: lseek eof");
249 else if (bufferlength > size_by_seek) {
250 warn("save_buffer: .fsxgood file too short... will save 0x%qx bytes instead of 0x%qx\n", (unsigned long long)size_by_seek, (unsigned long long)bufferlength);
251 bufferlength = size_by_seek;
252 }
253 }
254
255 ret = lseek(fd, (off_t) 0, SEEK_SET);
256 if (ret == (off_t) - 1)
257 prterr("save_buffer: lseek 0");
258
259 byteswritten = write(fd, buffer, (size_t) bufferlength);
260 if (byteswritten != bufferlength) {
261 if (byteswritten == -1)
262 prterr("save_buffer write");
263 else
264 warn("save_buffer: short write, 0x%x bytes instead of 0x%qx\n",
265 (unsigned)byteswritten, (unsigned long long)bufferlength);
266 }
267 }
268
269
270 void
271 report_failure(int status)
272 {
273 logdump();
274
275 if (fsxgoodfd) {
276 if (good_buf) {
277 save_buffer(good_buf, file_size, fsxgoodfd);
278 prt("Correct content saved for comparison\n");
279 prt("(maybe hexdump \"%s\" vs \"%s.fsxgood\")\n", fname, fname);
280 }
281 close(fsxgoodfd);
282 }
283 exit(status);
284 }
285
286
287 #define short_at(cp) ((unsigned short)((*((unsigned char *)(cp)) << 8) | \
288 *(((unsigned char *)(cp)) + 1)))
289
290 void
291 check_buffers(unsigned offset, unsigned size)
292 {
293 unsigned char c, t;
294 unsigned i = 0;
295 unsigned n = 0;
296 unsigned op = 0;
297 unsigned bad = 0;
298
299 if (bcmp(good_buf + offset, temp_buf, size) != 0) {
300 prt("READ BAD DATA: offset = 0x%x, size = 0x%x\n", offset, size);
301 prt("OFFSET\tGOOD\tBAD\tRANGE\n");
302 while (size > 0) {
303 c = good_buf[offset];
304 t = temp_buf[i];
305 if (c != t) {
306 if (n == 0) {
307 bad = short_at(&temp_buf[i]);
308 prt("0x%5x\t0x%04x\t0x%04x", offset,
309 short_at(&good_buf[offset]), bad);
310 op = temp_buf[offset & 1 ? i + 1 : i];
311 }
312 n++;
313 badoff = offset;
314 }
315 offset++;
316 i++;
317 size--;
318 }
319 if (n) {
320 prt("\t0x%5x\n", n);
321 if (bad)
322 prt("operation# (mod 256) for the bad data may be %u\n",
323 ((unsigned)op & 0xff));
324 else
325 prt("operation# (mod 256) for the bad data unknown, check HOLE and EXTEND ops\n");
326 } else
327 prt("????????????????\n");
328 report_failure(110);
329 }
330 }
331
332
333 void
334 check_size(void)
335 {
336 struct stat statbuf;
337 off_t size_by_seek;
338
339 if (fstat(fd, &statbuf)) {
340 prterr("check_size: fstat");
341 statbuf.st_size = -1;
342 }
343 size_by_seek = lseek(fd, (off_t) 0, L_XTND);
344 if (file_size != statbuf.st_size || file_size != size_by_seek) {
345 prt("Size error: expected 0x%qx stat 0x%qx seek 0x%qx\n",
346 (unsigned long long)file_size,
347 (unsigned long long)statbuf.st_size,
348 (unsigned long long)size_by_seek);
349 report_failure(120);
350 }
351 }
352
353
354 void
355 check_trunc_hack(void)
356 {
357 struct stat statbuf;
358
359 ftruncate(fd, (off_t) 0);
360 ftruncate(fd, (off_t) 100000);
361 fstat(fd, &statbuf);
362 if (statbuf.st_size != (off_t) 100000) {
363 prt("no extend on truncate! not posix!\n");
364 exit(130);
365 }
366 ftruncate(fd, 0);
367 }
368
369
370 void
371 doread(unsigned offset, unsigned size)
372 {
373 off_t ret;
374 unsigned iret;
375
376 offset -= offset % readbdy;
377 if (size == 0) {
378 if (!quiet && testcalls > simulatedopcount)
379 prt("skipping zero size read\n");
380 log4(OP_SKIPPED, OP_READ, offset, size);
381 return;
382 }
383 if (size + offset > file_size) {
384 if (!quiet && testcalls > simulatedopcount)
385 prt("skipping seek/read past end of file\n");
386 log4(OP_SKIPPED, OP_READ, offset, size);
387 return;
388 }
389
390 log4(OP_READ, offset, size, 0);
391
392 if (testcalls <= simulatedopcount)
393 return;
394
395 if (!quiet
396 && ((progressinterval && testcalls % progressinterval == 0)
397 || (debug
398 && (monitorstart == -1 || offset + size > monitorstart)
399 && (monitorend == -1 || offset <= monitorend))))
400 prt("%lu read\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls, offset,
401 offset + size - 1, size);
402 ret = lseek(fd, (off_t) offset, SEEK_SET);
403 if (ret == (off_t) - 1) {
404 prterr("doread: lseek");
405 report_failure(140);
406 }
407 iret = read(fd, temp_buf, size);
408 if (iret != size) {
409 if (iret == -1)
410 prterr("doread: read");
411 else
412 prt("short read: 0x%x bytes instead of 0x%x\n", iret, size);
413 report_failure(141);
414 }
415 check_buffers(offset, size);
416 }
417
418
419 void
420 domapread(unsigned offset, unsigned size)
421 {
422 unsigned pg_offset;
423 unsigned map_size;
424 char *p;
425
426 offset -= offset % readbdy;
427 if (size == 0) {
428 if (!quiet && testcalls > simulatedopcount)
429 prt("skipping zero size read\n");
430 log4(OP_SKIPPED, OP_MAPREAD, offset, size);
431 return;
432 }
433 if (size + offset > file_size) {
434 if (!quiet && testcalls > simulatedopcount)
435 prt("skipping seek/read past end of file\n");
436 log4(OP_SKIPPED, OP_MAPREAD, offset, size);
437 return;
438 }
439
440 log4(OP_MAPREAD, offset, size, 0);
441
442 if (testcalls <= simulatedopcount)
443 return;
444
445 if (!quiet
446 && ((progressinterval && testcalls % progressinterval == 0)
447 || (debug
448 && (monitorstart == -1 || offset + size > monitorstart)
449 && (monitorend == -1 || offset <= monitorend))))
450 prt("%lu mapread\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls, offset,
451 offset + size - 1, size);
452
453 pg_offset = offset & PAGE_MASK;
454 map_size = pg_offset + size;
455
456 if ((p =
457 (char *)mmap(0, map_size, PROT_READ, MAP_FILE, fd,
458 (off_t) (offset - pg_offset))) == (char *)-1) {
459 prterr("domapread: mmap");
460 report_failure(190);
461 }
462 memcpy(temp_buf, p + pg_offset, size);
463 if (munmap(p, map_size) != 0) {
464 prterr("domapread: munmap");
465 report_failure(191);
466 }
467
468 check_buffers(offset, size);
469 }
470
471
472 void
473 gendata(char *original_buf, char *good_buf, unsigned offset, unsigned size)
474 {
475 while (size--) {
476 good_buf[offset] = testcalls % 256;
477 if (offset % 2)
478 good_buf[offset] += original_buf[offset];
479 offset++;
480 }
481 }
482
483
484 void
485 dowrite(unsigned offset, unsigned size)
486 {
487 off_t ret;
488 unsigned iret;
489
490 offset -= offset % writebdy;
491 if (size == 0) {
492 if (!quiet && testcalls > simulatedopcount)
493 prt("skipping zero size write\n");
494 log4(OP_SKIPPED, OP_WRITE, offset, size);
495 return;
496 }
497
498 log4(OP_WRITE, offset, size, file_size);
499
500 gendata(original_buf, good_buf, offset, size);
501 if (file_size < offset + size) {
502 if (file_size < offset)
503 bzero(good_buf + file_size, offset - file_size);
504 file_size = offset + size;
505 if (lite) {
506 warn("Lite file size bug in fsx!");
507 report_failure(149);
508 }
509 }
510
511 if (testcalls <= simulatedopcount)
512 return;
513
514 if (!quiet
515 && ((progressinterval && testcalls % progressinterval == 0)
516 || (debug
517 && (monitorstart == -1 || offset + size > monitorstart)
518 && (monitorend == -1 || offset <= monitorend))))
519 prt("%lu write\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls, offset,
520 offset + size - 1, size);
521 ret = lseek(fd, (off_t) offset, SEEK_SET);
522 if (ret == (off_t) - 1) {
523 prterr("dowrite: lseek");
524 report_failure(150);
525 }
526 iret = write(fd, good_buf + offset, size);
527 if (iret != size) {
528 if (iret == -1)
529 prterr("dowrite: write");
530 else
531 prt("short write: 0x%x bytes instead of 0x%x\n", iret, size);
532 report_failure(151);
533 }
534 }
535
536
537 void
538 domapwrite(unsigned offset, unsigned size)
539 {
540 unsigned pg_offset;
541 unsigned map_size;
542 off_t cur_filesize;
543 char *p;
544
545 offset -= offset % writebdy;
546 if (size == 0) {
547 if (!quiet && testcalls > simulatedopcount)
548 prt("skipping zero size write\n");
549 log4(OP_SKIPPED, OP_MAPWRITE, offset, size);
550 return;
551 }
552 cur_filesize = file_size;
553
554 log4(OP_MAPWRITE, offset, size, 0);
555
556 gendata(original_buf, good_buf, offset, size);
557 if (file_size < offset + size) {
558 if (file_size < offset)
559 bzero(good_buf + file_size, offset - file_size);
560 file_size = offset + size;
561 if (lite) {
562 warn("Lite file size bug in fsx!");
563 report_failure(200);
564 }
565 }
566
567 if (testcalls <= simulatedopcount)
568 return;
569
570 if (!quiet
571 && ((progressinterval && testcalls % progressinterval == 0)
572 || (debug
573 && (monitorstart == -1 || offset + size > monitorstart)
574 && (monitorend == -1 || offset <= monitorend))))
575 prt("%lu mapwrite\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
576 offset, offset + size - 1, size);
577
578 if (file_size > cur_filesize) {
579 if (ftruncate(fd, file_size) == -1) {
580 prterr("domapwrite: ftruncate");
581 exit(201);
582 }
583 }
584 pg_offset = offset & PAGE_MASK;
585 map_size = pg_offset + size;
586
587 if ((p =
588 (char *)mmap(0, map_size, PROT_READ | PROT_WRITE,
589 MAP_FILE | MAP_SHARED, fd,
590 (off_t) (offset - pg_offset))) == (char *)-1) {
591 prterr("domapwrite: mmap");
592 report_failure(202);
593 }
594 memcpy(p + pg_offset, good_buf + offset, size);
595 if (msync(p, map_size, 0) != 0) {
596 prterr("domapwrite: msync");
597 report_failure(203);
598 }
599 if (munmap(p, map_size) != 0) {
600 prterr("domapwrite: munmap");
601 report_failure(204);
602 }
603 }
604
605
606 void
607 dotruncate(unsigned size)
608 {
609 int oldsize = file_size;
610
611 size -= size % truncbdy;
612 if (size > biggest) {
613 biggest = size;
614 if (!quiet && testcalls > simulatedopcount)
615 prt("truncating to largest ever: 0x%x\n", size);
616 }
617
618 log4(OP_TRUNCATE, size, (unsigned)file_size, 0);
619
620 if (size > file_size)
621 bzero(good_buf + file_size, size - file_size);
622 file_size = size;
623
624 if (testcalls <= simulatedopcount)
625 return;
626
627 if ((progressinterval && testcalls % progressinterval == 0)
628 || (debug
629 && (monitorstart == -1 || monitorend == -1 || size <= monitorend)))
630 prt("%lu trunc\tfrom 0x%x to 0x%x\n", testcalls, oldsize, size);
631 if (ftruncate(fd, (off_t) size) == -1) {
632 prt("ftruncate1: %x\n", size);
633 prterr("dotruncate: ftruncate");
634 report_failure(160);
635 }
636 }
637
638
639 void
640 writefileimage(void)
641 {
642 ssize_t iret;
643
644 if (lseek(fd, (off_t) 0, SEEK_SET) == (off_t) - 1) {
645 prterr("writefileimage: lseek");
646 report_failure(171);
647 }
648 iret = write(fd, good_buf, file_size);
649 if ((off_t) iret != file_size) {
650 if (iret == -1)
651 prterr("writefileimage: write");
652 else
653 prt("short write: 0x%x bytes instead of 0x%qx\n", iret,
654 (unsigned long long)file_size);
655 report_failure(172);
656 }
657 if (lite ? 0 : ftruncate(fd, file_size) == -1) {
658 prt("ftruncate2: %qx\n", (unsigned long long)file_size);
659 prterr("writefileimage: ftruncate");
660 report_failure(173);
661 }
662 }
663
664
665 void
666 docloseopen(void)
667 {
668 if (testcalls <= simulatedopcount)
669 return;
670
671 if (debug)
672 prt("%lu close/open\n", testcalls);
673 if (close(fd)) {
674 prterr("docloseopen: close");
675 report_failure(180);
676 }
677 fd = open(fname, O_RDWR, 0);
678 if (fd < 0) {
679 prterr("docloseopen: open");
680 report_failure(181);
681 }
682 }
683
684
685 void
686 test(void)
687 {
688 unsigned long offset;
689 unsigned long size = maxoplen;
690 unsigned long rv = random();
691 unsigned long op = rv % (3 + !lite + mapped_writes);
692
693 /* turn off the map read if necessary */
694
695 if (op == 2 && !mapped_reads)
696 op = 0;
697
698 if (simulatedopcount > 0 && testcalls == simulatedopcount)
699 writefileimage();
700
701 testcalls++;
702
703 closeopen = (rv >> 3) < (1 << 28) / closeprob;
704
705 if (debugstart > 0 && testcalls >= debugstart)
706 debug = 1;
707
708 if (!quiet && testcalls < simulatedopcount && testcalls % 100000 == 0)
709 prt("%lu...\n", testcalls);
710
711 /*
712 * READ: op = 0
713 * WRITE: op = 1
714 * MAPREAD: op = 2
715 * TRUNCATE: op = 3
716 * MAPWRITE: op = 3 or 4
717 */
718 if (lite ? 0 : op == 3 && (style & 1) == 0) /* vanilla truncate? */
719 dotruncate(random() % maxfilelen);
720 else {
721 if (randomoplen)
722 size = random() % (maxoplen + 1);
723 if (lite ? 0 : op == 3)
724 dotruncate(size);
725 else {
726 offset = random();
727 if (op == 1 || op == (lite ? 3 : 4)) {
728 offset %= maxfilelen;
729 if (offset + size > maxfilelen)
730 size = maxfilelen - offset;
731 if (op != 1)
732 domapwrite(offset, size);
733 else
734 dowrite(offset, size);
735 } else {
736 if (file_size)
737 offset %= file_size;
738 else
739 offset = 0;
740 if (offset + size > file_size)
741 size = file_size - offset;
742 if (op != 0)
743 domapread(offset, size);
744 else
745 doread(offset, size);
746 }
747 }
748 }
749 if (sizechecks && testcalls > simulatedopcount)
750 check_size();
751 if (closeopen)
752 docloseopen();
753 }
754
755
756 void
757 cleanup(int sig)
758 {
759 if (sig)
760 prt("signal %d\n", sig);
761 prt("testcalls = %lu\n", testcalls);
762 exit(sig);
763 }
764
765
766 void
767 usage(void)
768 {
769 fprintf(stdout, "usage: %s",
770 "fsx [-dnqLOW] [-b opnum] [-c Prob] [-l flen] [-m start:end] [-o oplen] [-p progressinterval] [-r readbdy] [-s style] [-t truncbdy] [-w writebdy] [-D startingop] [-N numops] [-P dirpath] [-S seed] fname\n\
771 -b opnum: beginning operation number (default 1)\n\
772 -c P: 1 in P chance of file close+open at each op (default infinity)\n\
773 -d: debug output for all operations\n\
774 -l flen: the upper bound on file size (default 262144)\n\
775 -m startop:endop: monitor (print debug output) specified byte range (default 0:infinity)\n\
776 -n: no verifications of file size\n\
777 -o oplen: the upper bound on operation size (default 65536)\n\
778 -p progressinterval: debug output at specified operation interval\n\
779 -q: quieter operation\n\
780 -r readbdy: 4096 would make reads page aligned (default 1)\n\
781 -s style: 1 gives smaller truncates (default 0)\n\
782 -t truncbdy: 4096 would make truncates page aligned (default 1)\n\
783 -w writebdy: 4096 would make writes page aligned (default 1)\n\
784 -D startingop: debug output starting at specified operation\n\
785 -L: fsxLite - no file creations & no file size changes\n\
786 -N numops: total # operations to do (default infinity)\n\
787 -O: use oplen (see -o flag) for every op (default random)\n\
788 -P: save .fsxlog and .fsxgood files in dirpath (default ./)\n\
789 -S seed: for random # generator (default 1) 0 gets timestamp\n\
790 -W: mapped write operations DISabled\n\
791 -R: read() system calls only (mapped reads disabled)\n\
792 fname: this filename is REQUIRED (no default)\n");
793 exit(90);
794 }
795
796
797 int
798 getnum(char *s, char **e)
799 {
800 int ret = -1;
801
802 *e = NULL;
803 ret = strtol(s, e, 0);
804 if (*e)
805 switch (**e) {
806 case 'b':
807 case 'B':
808 ret *= 512;
809 *e = *e + 1;
810 break;
811 case 'k':
812 case 'K':
813 ret *= 1024;
814 *e = *e + 1;
815 break;
816 case 'm':
817 case 'M':
818 ret *= 1024 * 1024;
819 *e = *e + 1;
820 break;
821 case 'w':
822 case 'W':
823 ret *= 4;
824 *e = *e + 1;
825 break;
826 }
827 return (ret);
828 }
829
830
831 int
832 main(int argc, char **argv)
833 {
834 int i, style, ch;
835 char *endp;
836 char goodfile[1024];
837 char logfile[1024];
838
839 goodfile[0] = 0;
840 logfile[0] = 0;
841
842 setvbuf(stdout, NULL, _IOLBF, 0); /* line buffered stdout */
843
844 while ((ch = getopt(argc, argv, "b:c:dl:m:no:p:qr:s:t:w:D:LN:OP:RS:W"))
845 != EOF)
846 switch (ch) {
847 case 'b':
848 simulatedopcount = getnum(optarg, &endp);
849 if (!quiet)
850 fprintf(stdout, "Will begin at operation %ld\n",
851 simulatedopcount);
852 if (simulatedopcount == 0)
853 usage();
854 simulatedopcount -= 1;
855 break;
856 case 'c':
857 closeprob = getnum(optarg, &endp);
858 if (!quiet)
859 fprintf(stdout, "Chance of close/open is 1 in %d\n",
860 closeprob);
861 if (closeprob <= 0)
862 usage();
863 break;
864 case 'd':
865 debug = 1;
866 break;
867 case 'l':
868 maxfilelen = getnum(optarg, &endp);
869 if (maxfilelen <= 0)
870 usage();
871 break;
872 case 'm':
873 monitorstart = getnum(optarg, &endp);
874 if (monitorstart < 0)
875 usage();
876 if (!endp || *endp++ != ':')
877 usage();
878 monitorend = getnum(endp, &endp);
879 if (monitorend < 0)
880 usage();
881 if (monitorend == 0)
882 monitorend = -1; /* aka infinity */
883 debug = 1;
884 case 'n':
885 sizechecks = 0;
886 break;
887 case 'o':
888 maxoplen = getnum(optarg, &endp);
889 if (maxoplen <= 0)
890 usage();
891 break;
892 case 'p':
893 progressinterval = getnum(optarg, &endp);
894 if (progressinterval < 0)
895 usage();
896 break;
897 case 'q':
898 quiet = 1;
899 break;
900 case 'r':
901 readbdy = getnum(optarg, &endp);
902 if (readbdy <= 0)
903 usage();
904 break;
905 case 's':
906 style = getnum(optarg, &endp);
907 if (style < 0 || style > 1)
908 usage();
909 break;
910 case 't':
911 truncbdy = getnum(optarg, &endp);
912 if (truncbdy <= 0)
913 usage();
914 break;
915 case 'w':
916 writebdy = getnum(optarg, &endp);
917 if (writebdy <= 0)
918 usage();
919 break;
920 case 'D':
921 debugstart = getnum(optarg, &endp);
922 if (debugstart < 1)
923 usage();
924 break;
925 case 'L':
926 lite = 1;
927 break;
928 case 'N':
929 numops = getnum(optarg, &endp);
930 if (numops < 0)
931 usage();
932 break;
933 case 'O':
934 randomoplen = 0;
935 break;
936 case 'P':
937 strncpy(goodfile, optarg, sizeof(goodfile));
938 strcat(goodfile, "/");
939 strncpy(logfile, optarg, sizeof(logfile));
940 strcat(logfile, "/");
941 break;
942 case 'R':
943 mapped_reads = 0;
944 break;
945 case 'S':
946 seed = getnum(optarg, &endp);
947 if (seed == 0)
948 seed = time(0) % 10000;
949 if (!quiet)
950 fprintf(stdout, "Seed set to %d\n", seed);
951 if (seed < 0)
952 usage();
953 break;
954 case 'W':
955 mapped_writes = 0;
956 if (!quiet)
957 fprintf(stdout, "mapped writes DISABLED\n");
958 break;
959
960 default:
961 usage();
962 /* NOTREACHED */
963 }
964 argc -= optind;
965 argv += optind;
966 if (argc != 1)
967 usage();
968 fname = argv[0];
969
970 signal(SIGHUP, cleanup);
971 signal(SIGINT, cleanup);
972 signal(SIGPIPE, cleanup);
973 signal(SIGALRM, cleanup);
974 signal(SIGTERM, cleanup);
975 signal(SIGXCPU, cleanup);
976 signal(SIGXFSZ, cleanup);
977 signal(SIGVTALRM, cleanup);
978 signal(SIGUSR1, cleanup);
979 signal(SIGUSR2, cleanup);
980
981 initstate(seed, state, 256);
982 setstate(state);
983 fd = open(fname, O_RDWR | (lite ? 0 : O_CREAT | O_TRUNC), 0666);
984 if (fd < 0) {
985 prterr(fname);
986 exit(91);
987 }
988 strncat(goodfile, fname, 256);
989 strcat(goodfile, ".fsxgood");
990 fsxgoodfd = open(goodfile, O_RDWR | O_CREAT | O_TRUNC, 0666);
991 if (fsxgoodfd < 0) {
992 prterr(goodfile);
993 exit(92);
994 }
995 strncat(logfile, fname, 256);
996 strcat(logfile, ".fsxlog");
997 fsxlogf = fopen(logfile, "w");
998 if (fsxlogf == NULL) {
999 prterr(logfile);
1000 exit(93);
1001 }
1002 if (lite) {
1003 off_t ret;
1004 file_size = maxfilelen = lseek(fd, (off_t) 0, L_XTND);
1005 if (file_size == (off_t) - 1) {
1006 prterr(fname);
1007 warn("main: lseek eof");
1008 exit(94);
1009 }
1010 ret = lseek(fd, (off_t) 0, SEEK_SET);
1011 if (ret == (off_t) - 1) {
1012 prterr(fname);
1013 warn("main: lseek 0");
1014 exit(95);
1015 }
1016 }
1017 original_buf = (char *)malloc(maxfilelen);
1018 for (i = 0; i < maxfilelen; i++)
1019 original_buf[i] = random() % 256;
1020 good_buf = calloc(1, maxfilelen);
1021 temp_buf = calloc(1, maxoplen);
1022 if (lite) { /* zero entire existing file */
1023 ssize_t written;
1024
1025 written = write(fd, good_buf, (size_t) maxfilelen);
1026 if (written != maxfilelen) {
1027 if (written == -1) {
1028 prterr(fname);
1029 warn("main: error on write");
1030 } else
1031 warn("main: short write, 0x%x bytes instead of 0x%x\n",
1032 (unsigned)written, (unsigned)maxfilelen);
1033 exit(98);
1034 }
1035 } else
1036 check_trunc_hack();
1037
1038 while (numops == -1 || numops--)
1039 test();
1040
1041 if (close(fd)) {
1042 prterr("close");
1043 report_failure(99);
1044 }
1045 prt("All operations completed A-OK!\n");
1046
1047 exit(0);
1048 return 0;
1049 }