1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2001 Silicon Graphics, Inc.
15 void usage(char *progname);
16 void writeblk(int fd);
17 void truncfile(int fd);
22 fprintf(stderr, "usage: %s [-l filesize] [-b blocksize] [-c count] [-s seed] [-o fileoffset (hex)] [-v] filename\n",
28 main(int argc, char *argv[])
31 char *filename = NULL;
33 filesize = 256*1024*1024;
35 count = filesize/blocksize;
39 while ((ch = getopt(argc, argv, "b:l:s:c:o:v")) != EOF) {
41 case 'b': blocksize = atoi(optarg); break;
42 case 'l': filesize = atol(optarg); break;
43 case 's': seed = atoi(optarg); break;
44 case 'c': count = atoi(optarg); break;
45 case 'o': fileoffset = strtoll(optarg, NULL, 16); break;
46 case 'v': verbose++; break;
47 default: usage(argv[0]); break;
51 filename = argv[optind];
54 printf("Seed = %d (use \"-s %d\" to re-execute this test)\n", seed, seed);
58 * Open the file, write rand block in random places, truncate the file,
59 * repeat ad-nauseum, then close the file.
61 if ((fd = open(filename, O_RDWR|O_CREAT|O_TRUNC, 0666)) < 0) {
65 for (i = 0; i < count; i++) {
68 if (verbose && ((i % 100) == 0)) {
84 static char *buffer = NULL;
86 if ((buffer == NULL) && ((buffer = calloc(1, blocksize)) == NULL)) {
91 offset = random() % filesize;
92 if (lseek64(fd, (off64_t)(fileoffset + offset), SEEK_SET) < 0) {
96 *(long *)buffer = *(long *)(buffer+256) = (long)offset;
97 if (write(fd, buffer, blocksize) < blocksize) {
102 printf("writing data at offset=%llx\n",
103 (unsigned long long)(fileoffset + offset));
111 offset = random() % filesize;
112 if (ftruncate64(fd, (off64_t)(fileoffset + offset)) < 0) {
117 printf("truncated file to offset %llx\n",
118 (unsigned long long)(fileoffset + offset));