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 #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",
84 /* Returns filename if successful or a null pointer if an error occurs */
86 parseargs(int argc, char *argv[])
91 filesize = DEFAULT_FILESIZE;
92 blocksize = DEFAULT_BLOCKSIZE;
93 count = (int) filesize / blocksize;
98 while ((ch = getopt(argc, argv, "b:l:s:c:o:x:vwdrapt")) != EOF) {
100 case 'b': blocksize = atoi(optarg); break;
101 case 'l': filesize = strtoull(optarg, NULL, 16); break;
102 case 's': seed = atoi(optarg); break;
103 case 'c': count = atoi(optarg); break;
104 case 'o': fileoffset = strtoull(optarg, NULL, 16); break;
105 case 'x': extsize = atoi(optarg); break;
106 case 'v': verbose++; break;
107 case 'w': wsync++; break;
108 case 'd': direct++; break;
109 case 'r': rt++; direct++; break;
110 case 'a': alloconly++; break;
111 case 'p': preserve++; break;
112 case 't': test++; preserve++; break;
113 default: usage(argv[0]); break;
116 if (optind != argc - 1)
119 if ((filesize % blocksize) != 0) {
120 filesize -= filesize % blocksize;
121 printf("filesize not a multiple of blocksize, "
122 "reducing filesize to %llu\n",
123 (unsigned long long) filesize);
125 if ((fileoffset % blocksize) != 0) {
126 fileoffset -= fileoffset % blocksize;
127 printf("fileoffset not a multiple of blocksize, "
128 "reducing fileoffset to %llu\n",
129 (unsigned long long) fileoffset);
131 if (count > (filesize/blocksize)) {
132 count = (filesize/blocksize);
133 printf("count of blocks written is too large, "
134 "setting to %d\n", count);
135 } else if (count < 1) {
137 printf("count of blocks written is too small, "
138 "setting to %d\n", count);
140 printf("randholes: Seed = %d (use \"-s %d\" "
141 "to re-execute this test)\n", seed, seed);
144 printf("randholes: blocksize=%d, filesize=%llu, seed=%d\n"
145 "randholes: count=%d, offset=%llu, extsize=%d\n",
146 blocksize, (unsigned long long)filesize, seed,
147 count, (unsigned long long)fileoffset, extsize);
148 printf("randholes: verbose=%d, wsync=%d, direct=%d, "
149 "rt=%d, alloconly=%d, preserve=%d, test=%d\n",
150 verbose, wsync, direct ? 1 : 0, rt, alloconly, preserve, test);
152 /* Last argument is the file name. Return it. */
154 return argv[optind]; /* Success */
158 * Determine the next random block number to which to write.
159 * If an already-written block is selected, choose the next
160 * unused higher-numbered block. Returns the block number,
161 * or -1 if we exhaust available blocks looking for an unused
167 int block, numblocks;
169 numblocks = filesize / blocksize;
170 block = random() % numblocks;
172 while (BITVAL(valid, block)) {
173 if (++block == numblocks) {
174 printf("returning block -1\n");
182 dumpblock(int *buffer, __uint64_t offset, int blocksize)
186 for (i = 0; i < (blocksize / 16); i++) {
187 printf("%llx: 0x%08x 0x%08x 0x%08x 0x%08x\n",
188 (unsigned long long) offset, *buffer, *(buffer + 1),
189 *(buffer + 2), *(buffer + 3));
196 writeblks(char *fname, int fd, size_t alignment)
204 if (posix_memalign((void **) &buffer, alignment, blocksize)) {
208 memset(buffer, 0, blocksize);
212 if (verbose && ((count % 100) == 0)) {
222 offset = (__uint64_t) block * blocksize;
226 fl.l_start = fileoffset + offset;
227 fl.l_len = blocksize;
230 if (xfscntl(fname, fd, RESVSP64, &fl) < 0) {
231 perror("xfsnctl(RESVSP64)");
236 SETBIT(valid, block);
238 if (lseek64(fd, fileoffset + offset, SEEK_SET) < 0) {
243 * Before writing, record offset at the base
244 * of the buffer and at offset 256 bytes
245 * into it. We'll verify this when we read
248 *(__uint64_t *) buffer = fileoffset + offset;
249 *(__uint64_t *) (buffer + 256) = fileoffset + offset;
251 if (write(fd, buffer, blocksize) < blocksize) {
257 printf("%swriting data at offset=%llx\n",
259 (unsigned long long) (fileoffset + offset));
267 readblks(int fd, size_t alignment)
271 unsigned int xfer, block, i;
276 xfer = READ_XFER*blocksize;
277 if (posix_memalign((void **) &buffer, alignment, xfer)) {
281 memset(buffer, 0, xfer);
285 if (lseek64(fd, fileoffset, SEEK_SET) < 0) {
291 while (offset < filesize) {
292 if ((i = read(fd, buffer, xfer) < xfer)) {
299 for (i = 0; i < READ_XFER; i++) {
304 if (verbose && ((block % 100) == 0)) {
309 want = BITVAL(valid, block) ? offset : 0;
310 first = *(__uint64_t *) tmp;
311 second = *(__uint64_t *) (tmp + 256);
312 if (first != want || second != want) {
313 printf("mismatched data at offset=0x%" PRIx64
314 ", expected 0x%" PRIx64
316 " and 0x%" PRIx64 "\n",
317 fileoffset + offset, want,
322 printf("block %d blocksize %d\n", block,
324 dumpblock((int *)tmp, fileoffset + offset,
341 * Determine the memory alignment required for I/O buffers. For
342 * direct I/O we request the needed information from the file
343 * system; otherwise pointer alignment is fine. Returns the
344 * alignment multiple, or 0 if an error occurs.
347 get_alignment(char *filename, int fd)
349 struct dioattr dioattr;
352 return sizeof (void *);
354 memset(&dioattr, 0, sizeof dioattr);
355 if (xfscntl(filename, fd, DIOINFO, &dioattr) < 0) {
356 perror("xfscntl(FIOINFO)");
360 /* Make sure the alignment meets the needs of posix_memalign() */
362 if (dioattr.d_mem % sizeof (void *) || ! power_of_2(dioattr.d_mem)) {
368 * Also make sure user doesn't specify a block size that's
369 * incompatible with the underlying file system.
371 if (! dioattr.d_miniosz) {
372 perror("miniosz == 0!");
375 if (blocksize % dioattr.d_miniosz) {
376 fprintf(stderr, "blocksize %d must be a multiple of "
377 "%d for direct I/O\n", blocksize, dioattr.d_miniosz);
381 return (size_t) dioattr.d_mem;
385 realtime_setup(char *filename, int fd)
387 struct fsxattr rtattr;
389 (void) memset(&rtattr, 0, sizeof rtattr);
390 if (xfscntl(filename, fd, FSGETXATTR, &rtattr) < 0) {
391 perror("FSGETXATTR)");
394 if ((rtattr.fsx_xflags & XFS_XFLAG_REALTIME) == 0 ||
395 (extsize && rtattr.fsx_extsize != extsize * blocksize)) {
396 rtattr.fsx_xflags |= XFS_XFLAG_REALTIME;
398 rtattr.fsx_extsize = extsize * blocksize;
399 if (xfscntl(filename, fd, FSSETXATTR, &rtattr) < 0) {
400 perror("FSSETXATTR)");
409 main(int argc, char *argv[])
418 filename = parseargs(argc, argv);
423 * Allocate a bitmap big enough to track the range of
424 * blocks we'll be dealing with.
426 size = (filesize / blocksize) / 8 + 1;
427 valid = malloc(size);
428 if ((valid = malloc(size)) == NULL) {
433 /* Lots of arguments affect how we open the file */
434 oflags = test ? O_RDONLY : O_RDWR|O_CREAT;
435 oflags |= preserve ? 0 : O_TRUNC;
436 oflags |= wsync ? O_SYNC : 0;
437 oflags |= direct ? O_DIRECT : 0;
440 * Open the file, write rand block in random places, read them all
441 * back to check for correctness, then close the file.
443 if ((fd = open(filename, oflags, 0666)) < 0) {
447 if (rt && realtime_setup(filename, fd))
449 alignment = get_alignment(filename, fd);
453 printf("write%s\n", test ? " (skipped)" : "");
454 writeblks(filename, fd, alignment);
456 printf("readback\n");
457 errors = readblks(fd, alignment);
466 printf("randholes: %d errors found during readback\n", errors);
470 printf("randholes: ok\n");