4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or https://opensource.org/licenses/CDDL-1.0.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
28 * Copyright (c) 2012, 2014 by Delphix. All rights reserved.
37 #include <sys/types.h>
39 #include <sys/statvfs.h>
41 #include <sys/ioctl.h>
43 #include <sys/param.h>
48 #define FSIZE 256*1024*1024
51 /* Initialize Globals */
52 static long fsize
= FSIZE
;
53 static size_t bsize
= BSIZE
;
56 static uint_t seed
= 0;
58 static int errflag
= 0;
59 static off_t offset
= 0;
60 static char *filename
= NULL
;
62 static void usage(char *execname
);
63 static void parse_options(int argc
, char *argv
[]);
64 static void do_write(int fd
);
65 static void do_trunc(int fd
);
70 (void) fprintf(stderr
,
71 "usage: %s [-b blocksize] [-c count] [-f filesize]"
72 " [-o offset] [-s seed] [-r] [-v] filename\n", execname
);
77 main(int argc
, char *argv
[])
82 parse_options(argc
, argv
);
84 fd
= open(filename
, O_RDWR
|O_CREAT
|O_TRUNC
, 0666);
90 for (i
= 0; count
== 0 || i
< count
; i
++) {
100 parse_options(int argc
, char *argv
[])
105 extern int optind
, optopt
;
107 count
= fsize
/ bsize
;
108 seed
= (uint_t
)time(NULL
);
109 while ((c
= getopt(argc
, argv
, "b:c:f:o:rs:v")) != -1) {
112 bsize
= atoi(optarg
);
116 count
= atoi(optarg
);
120 fsize
= atoi(optarg
);
124 offset
= atoi(optarg
);
140 (void) fprintf(stderr
,
141 "Option -%c requires an operand\n", optopt
);
146 (void) fprintf(stderr
,
147 "Unrecognized option: -%c\n", optopt
);
153 (void) usage(argv
[0]);
156 if (argc
<= optind
) {
157 (void) fprintf(stderr
,
158 "No filename specified\n");
161 filename
= argv
[optind
];
164 (void) fprintf(stderr
, "Seed = %d\n", seed
);
176 buf
= (char *)calloc(1, bsize
);
177 rbuf
= (char *)calloc(1, bsize
);
178 if (buf
== NULL
|| rbuf
== NULL
) {
183 roffset
= random() % fsize
;
184 if (lseek64(fd
, (offset
+ roffset
), SEEK_SET
) < 0) {
189 (void) strcpy(buf
, "ZFS Test Suite Truncation Test");
190 if (write(fd
, buf
, bsize
) < bsize
) {
196 if (lseek64(fd
, (offset
+ roffset
), SEEK_SET
) < 0) {
201 if (read(fd
, rbuf
, bsize
) < bsize
) {
206 if (memcmp(buf
, rbuf
, bsize
) != 0) {
212 (void) fprintf(stderr
,
213 "Wrote to offset %" PRId64
"\n", (offset
+ roffset
));
215 (void) fprintf(stderr
,
216 "Read back from offset %" PRId64
"\n",
230 roffset
= random() % fsize
;
231 if (ftruncate64(fd
, (offset
+ roffset
)) < 0) {
237 (void) fprintf(stderr
, "Truncated at offset %" PRId64
"\n",