]>
git.proxmox.com Git - mirror_zfs-debian.git/blob - tests/zfs-tests/cmd/file_write/file_write.c
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 http://www.opensolaris.org/os/licensing.
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.
27 #include "../file_common.h"
31 #include <sys/types.h>
37 typedef unsigned char uchar_t
;
38 typedef long long longlong_t
;
39 typedef longlong_t offset_t
;
41 static unsigned char bigbuffer
[BIGBUFFERSIZE
];
44 * Writes (or appends) a given value to a file repeatedly.
45 * See header file for defaults.
48 static void usage(char *);
51 * psudo-randomize the buffer
53 void randomize_buffer(int block_size
) {
55 char rnd
= rand() & 0xff;
56 for (i
= 0; i
< block_size
; i
++)
61 main(int argc
, char **argv
)
69 int64_t good_writes
= 0;
75 int write_count
= BIGFILESIZE
;
76 uchar_t fillchar
= DATA
;
77 int block_size
= BLOCKSZ
;
78 char *filename
= NULL
;
79 char *operation
= NULL
;
80 offset_t noffset
, offset
= 0;
88 while ((c
= getopt(argc
, argv
, "b:c:d:s:f:o:vwr")) != -1) {
91 block_size
= atoi(optarg
);
94 write_count
= atoi(optarg
);
98 fillchar
= 'R'; /* R = random data */
100 fillchar
= atoi(optarg
);
103 offset
= atoll(optarg
);
121 (void) printf("unknown arg %c\n", optopt
);
128 * Validate Parameters
131 (void) printf("Filename not specified (-f <file>)\n");
136 (void) printf("Operation not specified (-o <operation>).\n");
140 if (block_size
> BIGBUFFERSIZE
) {
141 (void) printf("block_size is too large max==%d.\n",
147 usage(prog
); /* no return */
152 * Prepare the buffer and determine the requested operation
154 nxtfillchar
= fillchar
;
160 for (i
= 0; i
< block_size
; i
++) {
161 bigbuffer
[i
] = nxtfillchar
;
164 if ((k
% DATA_RANGE
) == 0) {
168 } else if (fillchar
== 'R') {
169 nxtfillchar
= rand() & 0xff;
174 * using the strncmp of operation will make the operation match the
175 * first shortest match - as the operations are unique from the first
176 * character this means that we match single character operations
178 if ((strncmp(operation
, "create", strlen(operation
) + 1)) == 0 ||
179 (strncmp(operation
, "overwrite", strlen(operation
) + 1)) == 0) {
180 oflag
= (O_RDWR
|O_CREAT
);
181 } else if ((strncmp(operation
, "append", strlen(operation
) + 1)) == 0) {
182 oflag
= (O_RDWR
|O_APPEND
);
184 (void) printf("valid operations are <create|append> not '%s'\n",
190 oflag
= oflag
| O_RSYNC
;
194 oflag
= oflag
| O_SYNC
;
198 * Given an operation (create/overwrite/append), open the file
199 * accordingly and perform a write of the appropriate type.
201 if ((bigfd
= open(filename
, oflag
, 0666)) == -1) {
202 (void) printf("open %s: failed [%s]%d. Aborting!\n", filename
,
203 strerror(errno
), errno
);
206 noffset
= lseek64(bigfd
, offset
, SEEK_SET
);
207 if (noffset
!= offset
) {
208 (void) printf("llseek %s (%lld/%lld) failed [%s]%d.Aborting!\n",
209 filename
, offset
, noffset
, strerror(errno
), errno
);
214 (void) printf("%s: block_size = %d, write_count = %d, "
215 "offset = %lld, ", filename
, block_size
,
216 write_count
, offset
);
217 if (fillchar
== 'R') {
218 (void) printf("data = [random]\n");
220 (void) printf("data = %s%d\n",
221 (fillchar
== 0) ? "0->" : "",
222 (fillchar
== 0) ? DATA_RANGE
: fillchar
);
226 for (i
= 0; i
< write_count
; i
++) {
229 randomize_buffer(block_size
);
231 if ((n
= write(bigfd
, &bigbuffer
, block_size
)) == -1) {
232 (void) printf("write failed (%ld), good_writes = %"
233 PRId64
", " "error: %s[%d]\n",
234 (long)n
, good_writes
,
243 (void) printf("Success: good_writes = %" PRId64
"(%"
244 PRId64
")\n", good_writes
, (good_writes
* block_size
));
253 (void) printf("Usage: %s [-v] -o {create,overwrite,append} -f file_name"
255 "\t[-s offset] [-c write_count] [-d data]\n\n"
256 "Where [data] equal to zero causes chars "
257 "0->%d to be repeated throughout, or [data]\n"
258 "equal to 'R' for psudorandom data.\n",