2 * Copyright (c) 2000-2003 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 unsigned char *valid; /* Bit-vector array showing which blocks have been written */
24 int nvalid; /* number of bytes in valid array */
25 #define SETBIT(ARRAY, N) ((ARRAY)[(N)/8] |= (1 << ((N)%8)))
26 #define BITVAL(ARRAY, N) ((ARRAY)[(N)/8] & (1 << ((N)%8)))
28 #define power_of_2(x) ((x) && !((x) & ((x) - 1)))
29 #define DEFAULT_FILESIZE ((__uint64_t) (256 * 1024 * 1024))
30 #define DEFAULT_BLOCKSIZE 512
34 unsigned int blocksize;
41 int extsize; /* used only for real-time */
44 __uint64_t fileoffset;
47 #define READ_XFER 10 /* block to read at a time when checking */
50 * Define xfscntl() to mask the difference between the Linux
51 * and the Irix fcntl() interfaces to XFS for user space. The
52 * "cmd" argument is just the last part of the command, e.g.
53 * pass FSGETXATTR in place of either XFS_IOC_FSGETXATTR (Linux)
54 * F_FSGETXATTR (Irix).
57 #ifdef __sgi__ /* Irix */
58 # define xfscntl(filename, fd, cmd, arg) \
59 fcntl((fd), F_ ## cmd, (arg))
61 # define xfscntl(filename, fd, cmd, arg) \
62 xfsctl((filename), (fd), XFS_IOC_ ## cmd, (arg))
63 #endif /* ! __sgi__ */
69 "usage: %s [-l filesize] [-b blocksize] [-c count]\n"
70 "\t\t[-o write_offset] [-s seed] [-r [-x extentsize]]\n"
71 "\t\t[-w] [-v] [-d] [-a] [-p] [-t] filename\n\n",
73 fprintf(stderr, "\tdefault filesize is %" PRIu64 " bytes\n",
75 fprintf(stderr, "\tdefault blocksize is %u bytes\n",
77 fprintf(stderr, "\tdefault count is %d block-sized writes\n",
78 (int) (DEFAULT_FILESIZE / DEFAULT_BLOCKSIZE));
79 fprintf(stderr, "\tdefault write_offset is %" PRIu64 " bytes\n",
89 numblocks = filesize / blocksize;
90 block = random() % numblocks;
91 if (BITVAL(valid, block) == 0)
94 for ( ; BITVAL(valid, block) != 0; block++) {
95 if (block == (numblocks-1))
99 printf("returning block -1\n");
104 dumpblock(int *buffer, __uint64_t offset, int blocksize)
108 for (i = 0; i < (blocksize / 16); i++) {
109 printf("%llx: 0x%08x 0x%08x 0x%08x 0x%08x\n",
110 (unsigned long long)offset, *buffer, *(buffer + 1), *(buffer + 2),
118 writeblks(char *fname, int fd, size_t alignment)
126 if (posix_memalign((void **) &buffer, alignment, blocksize)) {
130 memset(buffer, 0, blocksize);
134 if (verbose && ((count % 100) == 0)) {
144 offset = (__uint64_t) block * blocksize;
148 fl.l_start = fileoffset + offset;
149 fl.l_len = blocksize;
152 if (xfscntl(fname, fd, RESVSP64, &fl) < 0) {
153 perror("xfsnctl(RESVSP64)");
158 SETBIT(valid, block);
160 if (lseek64(fd, fileoffset + offset, SEEK_SET) < 0) {
165 * Before writing, record offset at the base
166 * of the buffer and at offset 256 bytes
167 * into it. We'll verify this when we read
170 *(__uint64_t *) buffer = fileoffset + offset;
171 *(__uint64_t *) (buffer + 256) = fileoffset + offset;
173 if (write(fd, buffer, blocksize) < blocksize) {
179 printf("%swriting data at offset=%llx\n",
181 (unsigned long long) (fileoffset + offset));
189 readblks(int fd, size_t alignment)
193 unsigned int xfer, block, i;
198 xfer = READ_XFER*blocksize;
199 if (posix_memalign((void **) &buffer, alignment, xfer)) {
203 memset(buffer, 0, xfer);
207 if (lseek64(fd, fileoffset, SEEK_SET) < 0) {
213 while (offset < filesize) {
214 if ((i = read(fd, buffer, xfer) < xfer)) {
221 for (i = 0; i < READ_XFER; i++) {
226 if (verbose && ((block % 100) == 0)) {
231 want = BITVAL(valid, block) ? offset : 0;
232 first = *(__uint64_t *) tmp;
233 second = *(__uint64_t *) (tmp + 256);
234 if (first != want || second != want) {
235 printf("mismatched data at offset=0x%" PRIx64
236 ", expected 0x%" PRIx64
238 " and 0x%" PRIx64 "\n",
239 fileoffset + offset, want,
244 printf("block %d blocksize %d\n", block,
246 dumpblock((int *)tmp, fileoffset + offset,
263 * Determine the memory alignment required for I/O buffers. For
264 * direct I/O we request the needed information from the file
265 * system; otherwise pointer alignment is fine. Returns the
266 * alignment multiple, or 0 if an error occurs.
269 get_alignment(char *filename, int fd)
271 struct dioattr dioattr;
274 return sizeof (void *);
276 memset(&dioattr, 0, sizeof dioattr);
277 if (xfscntl(filename, fd, DIOINFO, &dioattr) < 0) {
278 perror("xfscntl(FIOINFO)");
282 /* Make sure the alignment meets the needs of posix_memalign() */
284 if (dioattr.d_mem % sizeof (void *) || ! power_of_2(dioattr.d_mem)) {
290 * Also make sure user doesn't specify a block size that's
291 * incompatible with the underlying file system.
293 if (! dioattr.d_miniosz) {
294 perror("miniosz == 0!");
297 if (blocksize % dioattr.d_miniosz) {
298 fprintf(stderr, "blocksize %d must be a multiple of "
299 "%d for direct I/O\n", blocksize, dioattr.d_miniosz);
303 return (size_t) dioattr.d_mem;
307 realtime_setup(char *filename, int fd)
309 struct fsxattr rtattr;
311 (void) memset(&rtattr, 0, sizeof rtattr);
312 if (xfscntl(filename, fd, FSGETXATTR, &rtattr) < 0) {
313 perror("FSGETXATTR)");
316 if ((rtattr.fsx_xflags & XFS_XFLAG_REALTIME) == 0 ||
317 (extsize && rtattr.fsx_extsize != extsize * blocksize)) {
318 rtattr.fsx_xflags |= XFS_XFLAG_REALTIME;
320 rtattr.fsx_extsize = extsize * blocksize;
321 if (xfscntl(filename, fd, FSSETXATTR, &rtattr) < 0) {
322 perror("FSSETXATTR)");
331 main(int argc, char *argv[])
333 int seed, ch, fd, oflags;
334 char *filename = NULL;
338 filesize = DEFAULT_FILESIZE;
339 blocksize = DEFAULT_BLOCKSIZE;
340 count = (int) filesize / blocksize;
345 while ((ch = getopt(argc, argv, "b:l:s:c:o:x:vwdrapt")) != EOF) {
347 case 'b': blocksize = atoi(optarg); break;
348 case 'l': filesize = strtoull(optarg, NULL, 16); break;
349 case 's': seed = atoi(optarg); break;
350 case 'c': count = atoi(optarg); break;
351 case 'o': fileoffset = strtoull(optarg, NULL, 16); break;
352 case 'x': extsize = atoi(optarg); break;
353 case 'v': verbose++; break;
354 case 'w': wsync++; break;
355 case 'd': direct++; break;
356 case 'r': rt++; direct++; break;
357 case 'a': alloconly++; break;
358 case 'p': preserve++; break;
359 case 't': test++; preserve++; break;
360 default: usage(argv[0]); break;
363 if (optind == argc-1)
364 filename = argv[optind];
367 if ((filesize % blocksize) != 0) {
368 filesize -= filesize % blocksize;
369 printf("filesize not a multiple of blocksize, reducing filesize to %llu\n",
370 (unsigned long long)filesize);
372 if ((fileoffset % blocksize) != 0) {
373 fileoffset -= fileoffset % blocksize;
374 printf("fileoffset not a multiple of blocksize, reducing fileoffset to %llu\n",
375 (unsigned long long)fileoffset);
377 if (count > (filesize/blocksize)) {
378 count = (filesize/blocksize);
379 printf("count of blocks written is too large, setting to %d\n",
381 } else if (count < 1) {
383 printf("count of blocks written is too small, setting to %d\n",
386 printf("randholes: Seed = %d (use \"-s %d\" to re-execute this test)\n", seed, seed);
389 printf("randholes: blocksize=%d, filesize=%llu, seed=%d\n"
390 "randholes: count=%d, offset=%llu, extsize=%d\n",
391 blocksize, (unsigned long long)filesize, seed,
392 count, (unsigned long long)fileoffset, extsize);
393 printf("randholes: verbose=%d, wsync=%d, direct=%d, rt=%d, alloconly=%d, preserve=%d, test=%d\n",
394 verbose, wsync, direct ? 1 : 0, rt, alloconly, preserve, test);
397 * Open the file, write rand block in random places, read them all
398 * back to check for correctness, then close the file.
400 nvalid = (filesize / blocksize) / 8 + 1;
401 if ((valid = (unsigned char *)calloc(1, (unsigned)nvalid)) == NULL) {
406 oflags=test?(O_RDONLY):(O_RDWR | O_CREAT);
407 oflags |= (preserve ? 0 : O_TRUNC) |
408 (wsync ? O_SYNC : 0) |
409 (direct ? O_DIRECT : 0);
411 if ((fd = open(filename, oflags, 0666)) < 0) {
416 if (rt && realtime_setup(filename, fd))
419 alignment = get_alignment(filename, fd);
423 printf(test?"write (skipped)\n":"write\n");
424 writeblks(filename, fd, alignment);
425 printf("readback\n");
426 r = readblks(fd, alignment);
434 printf("randholes: %d errors found during readback\n", r);
437 printf("randholes: ok\n");