2 * Copyright (c) 2000-2003, 2010 SGI
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 #define power_of_2(x) ((x) && !((x) & ((x) - 1)))
24 #define DEFAULT_FILESIZE ((uint64_t) (256 * 1024 * 1024))
25 #define DEFAULT_BLOCKSIZE 512
27 #define SETBIT(ARRAY, N) ((ARRAY)[(N)/8] |= (1 << ((N)%8)))
28 #define BITVAL(ARRAY, N) ((ARRAY)[(N)/8] & (1 << ((N)%8)))
30 /* Bit-vector array showing which blocks have been written */
31 static unsigned char *valid;
33 static uint64_t filesize;
34 static uint64_t fileoffset;
36 static unsigned int blocksize;
43 static int extsize; /* used only for real-time */
47 #define READ_XFER 256 /* blocks 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 # define xfscntl(filename, fd, cmd, arg) \
58 xfsctl((filename), (fd), XFS_IOC_ ## cmd, (arg))
64 "usage: %s [-l filesize] [-b blocksize] [-c count]\n"
65 "\t\t[-o write_offset] [-s seed] [-r [-x extentsize]]\n"
66 "\t\t[-w] [-v] [-d] [-a] [-p] [-t] filename\n\n",
68 fprintf(stderr, "\tdefault filesize is %" PRIu64 " bytes\n",
70 fprintf(stderr, "\tdefault blocksize is %u bytes\n",
72 fprintf(stderr, "\tdefault count is %d block-sized writes\n",
73 (int) (DEFAULT_FILESIZE / DEFAULT_BLOCKSIZE));
74 fprintf(stderr, "\tdefault write_offset is %" PRIu64 " bytes\n",
79 /* Returns filename if successful or a null pointer if an error occurs */
81 parseargs(int argc, char *argv[])
86 filesize = DEFAULT_FILESIZE;
87 blocksize = DEFAULT_BLOCKSIZE;
88 count = (int) filesize / blocksize;
93 while ((ch = getopt(argc, argv, "b:l:s:c:o:x:vwdrapt")) != EOF) {
95 case 'b': blocksize = atoi(optarg); break;
96 case 'l': filesize = strtoull(optarg, NULL, 16); break;
97 case 's': seed = atoi(optarg); break;
98 case 'c': count = atoi(optarg); break;
99 case 'o': fileoffset = strtoull(optarg, NULL, 16); break;
100 case 'x': extsize = atoi(optarg); break;
101 case 'v': verbose++; break;
102 case 'w': wsync++; break;
103 case 'd': direct++; break;
104 case 'r': rt++; direct++; break;
105 case 'a': alloconly++; break;
106 case 'p': preserve++; break;
107 case 't': test++; preserve++; break;
108 default: usage(argv[0]); break;
111 if (optind != argc - 1)
114 if ((filesize % blocksize) != 0) {
115 filesize -= filesize % blocksize;
116 printf("filesize not a multiple of blocksize, "
117 "reducing filesize to %llu\n",
118 (unsigned long long) filesize);
120 if ((fileoffset % blocksize) != 0) {
121 fileoffset -= fileoffset % blocksize;
122 printf("fileoffset not a multiple of blocksize, "
123 "reducing fileoffset to %llu\n",
124 (unsigned long long) fileoffset);
126 if (count > (filesize/blocksize)) {
127 count = (filesize/blocksize);
128 printf("count of blocks written is too large, "
129 "setting to %d\n", count);
130 } else if (count < 1) {
132 printf("count of blocks written is too small, "
133 "setting to %d\n", count);
135 printf("randholes: Seed = %d (use \"-s %d\" "
136 "to re-execute this test)\n", seed, seed);
139 printf("randholes: blocksize=%d, filesize=%llu, seed=%d\n"
140 "randholes: count=%d, offset=%llu, extsize=%d\n",
141 blocksize, (unsigned long long)filesize, seed,
142 count, (unsigned long long)fileoffset, extsize);
143 printf("randholes: verbose=%d, wsync=%d, direct=%d, "
144 "rt=%d, alloconly=%d, preserve=%d, test=%d\n",
145 verbose, wsync, direct ? 1 : 0, rt, alloconly, preserve, test);
147 /* Last argument is the file name. Return it. */
149 return argv[optind]; /* Success */
153 * Determine the next random block number to which to write.
154 * If an already-written block is selected, choose the next
155 * unused higher-numbered block. Returns the block number,
156 * or -1 if we exhaust available blocks looking for an unused
162 int block, numblocks;
164 numblocks = filesize / blocksize;
165 block = random() % numblocks;
167 while (BITVAL(valid, block)) {
168 if (++block == numblocks) {
169 printf("returning block -1\n");
177 dumpblock(int *buffer, uint64_t offset, int blocksize)
181 for (i = 0; i < (blocksize / 16); i++) {
182 printf("%llx: 0x%08x 0x%08x 0x%08x 0x%08x\n",
183 (unsigned long long) offset, *buffer, *(buffer + 1),
184 *(buffer + 2), *(buffer + 3));
191 writeblks(char *fname, int fd, size_t alignment)
200 ret = posix_memalign((void **) &buffer, alignment, blocksize);
202 fprintf(stderr, "posix_memalign: %s\n", strerror(ret));
205 memset(buffer, 0, blocksize);
209 * Avoid allocation patterns being perturbed by different speculative
210 * preallocation beyond EOF configurations by first truncating the file
211 * to the expected maximum file size.
213 if (ftruncate(fd, filesize) < 0) {
219 if (verbose && ((count % 100) == 0)) {
229 offset = (uint64_t) block * blocksize;
233 fl.l_start = fileoffset + offset;
234 fl.l_len = blocksize;
237 if (xfscntl(fname, fd, RESVSP64, &fl) < 0) {
238 perror("xfsnctl(RESVSP64)");
243 SETBIT(valid, block);
245 if (lseek64(fd, fileoffset + offset, SEEK_SET) < 0) {
250 * Before writing, record offset at the base
251 * of the buffer and at offset 256 bytes
252 * into it. We'll verify this when we read
255 *(uint64_t *) buffer = fileoffset + offset;
256 *(uint64_t *) (buffer + 256) = fileoffset + offset;
258 if (write(fd, buffer, blocksize) < blocksize) {
264 printf("%swriting data at offset=%llx\n",
266 (unsigned long long) (fileoffset + offset));
274 readblks(int fd, size_t alignment)
278 unsigned int xfer, block, i;
283 xfer = READ_XFER*blocksize;
284 err = posix_memalign((void **) &buffer, alignment, xfer);
286 fprintf(stderr, "posix_memalign: %s\n", strerror(err));
289 memset(buffer, 0, xfer);
293 if (lseek64(fd, fileoffset, SEEK_SET) < 0) {
299 while (offset < filesize) {
300 if ((i = read(fd, buffer, xfer) < xfer)) {
307 for (i = 0; i < READ_XFER; i++) {
312 if (verbose && ((block % 100) == 0)) {
317 want = BITVAL(valid, block) ? offset : 0;
318 first = *(uint64_t *) tmp;
319 second = *(uint64_t *) (tmp + 256);
320 if (first != want || second != want) {
321 printf("mismatched data at offset=0x%" PRIx64
322 ", expected 0x%" PRIx64
324 " and 0x%" PRIx64 "\n",
325 fileoffset + offset, want,
330 printf("block %d blocksize %d\n", block,
332 dumpblock((int *)tmp, fileoffset + offset,
349 * Determine the memory alignment required for I/O buffers. For
350 * direct I/O we request the needed information from the file
351 * system; otherwise pointer alignment is fine. Returns the
352 * alignment multiple, or 0 if an error occurs.
355 get_alignment(char *filename, int fd)
357 struct dioattr dioattr;
360 return sizeof (void *);
362 memset(&dioattr, 0, sizeof dioattr);
363 if (xfscntl(filename, fd, DIOINFO, &dioattr) < 0) {
364 perror("xfscntl(FIOINFO)");
368 /* Make sure the alignment meets the needs of posix_memalign() */
370 if (dioattr.d_mem % sizeof (void *) || ! power_of_2(dioattr.d_mem)) {
376 * Also make sure user doesn't specify a block size that's
377 * incompatible with the underlying file system.
379 if (! dioattr.d_miniosz) {
380 perror("miniosz == 0!");
383 if (blocksize % dioattr.d_miniosz) {
384 fprintf(stderr, "blocksize %d must be a multiple of "
385 "%d for direct I/O\n", blocksize, dioattr.d_miniosz);
389 return (size_t) dioattr.d_mem;
393 realtime_setup(char *filename, int fd)
395 struct fsxattr rtattr;
397 (void) memset(&rtattr, 0, sizeof rtattr);
398 if (xfscntl(filename, fd, FSGETXATTR, &rtattr) < 0) {
399 perror("FSGETXATTR)");
402 if ((rtattr.fsx_xflags & XFS_XFLAG_REALTIME) == 0 ||
403 (extsize && rtattr.fsx_extsize != extsize * blocksize)) {
404 rtattr.fsx_xflags |= XFS_XFLAG_REALTIME;
406 rtattr.fsx_extsize = extsize * blocksize;
407 if (xfscntl(filename, fd, FSSETXATTR, &rtattr) < 0) {
408 perror("FSSETXATTR)");
417 main(int argc, char *argv[])
426 filename = parseargs(argc, argv);
431 * Allocate a bitmap big enough to track the range of
432 * blocks we'll be dealing with.
434 size = (filesize / blocksize) / 8 + 1;
435 valid = malloc(size);
436 if ((valid = malloc(size)) == NULL) {
440 memset(valid, 0, size);
442 /* Lots of arguments affect how we open the file */
443 oflags = test ? O_RDONLY : O_RDWR|O_CREAT;
444 oflags |= preserve ? 0 : O_TRUNC;
445 oflags |= wsync ? O_SYNC : 0;
446 oflags |= direct ? O_DIRECT : 0;
449 * Open the file, write rand block in random places, read them all
450 * back to check for correctness, then close the file.
452 if ((fd = open(filename, oflags, 0666)) < 0) {
456 if (rt && realtime_setup(filename, fd))
458 alignment = get_alignment(filename, fd);
462 printf("write%s\n", test ? " (skipped)" : "");
463 writeblks(filename, fd, alignment);
465 printf("readback\n");
466 errors = readblks(fd, alignment);
475 printf("randholes: %d errors found during readback\n", errors);
479 printf("randholes: ok\n");