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 DEFAULT_FILESIZE ((__uint64_t) (256 * 1024 * 1024))
29 #define DEFAULT_BLOCKSIZE 512
33 unsigned int blocksize;
40 int extsize; /* used only for real-time */
43 __uint64_t fileoffset;
45 struct fsxattr rtattr;
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)
129 buffer = memalign(diob.d_mem, blocksize);
131 buffer = malloc(blocksize);
132 if (buffer == NULL) {
136 memset(buffer, 0, blocksize);
140 if (verbose && ((count % 100) == 0)) {
150 offset = (__uint64_t) block * blocksize;
154 fl.l_start = fileoffset + offset;
155 fl.l_len = blocksize;
158 if (xfscntl(fname, fd, RESVSP64, &fl) < 0) {
159 perror("xfsnctl(RESVSP64)");
164 SETBIT(valid, block);
166 if (lseek64(fd, fileoffset + offset, SEEK_SET) < 0) {
171 * Before writing, record offset at the base
172 * of the buffer and at offset 256 bytes
173 * into it. We'll verify this when we read
176 *(__uint64_t *) buffer = fileoffset + offset;
177 *(__uint64_t *) (buffer + 256) = fileoffset + offset;
179 if (write(fd, buffer, blocksize) < blocksize) {
185 printf("%swriting data at offset=%llx\n",
187 (unsigned long long) (fileoffset + offset));
197 unsigned long offset;
199 unsigned int xfer, block, i;
204 xfer = READ_XFER*blocksize;
206 buffer = memalign(diob.d_mem, xfer);
208 buffer = malloc(xfer);
209 if (buffer == NULL) {
213 memset(buffer, 0, xfer);
217 if (lseek64(fd, fileoffset, SEEK_SET) < 0) {
221 for (offset = 0, block = 0; offset < filesize; offset += xfer) {
222 if ((i = read(fd, buffer, xfer) < xfer)) {
228 for (tmp = buffer, i = 0; i < READ_XFER; i++, block++, tmp += blocksize) {
229 if (verbose && ((block % 100) == 0)) {
233 if (BITVAL(valid, block) == 0) {
234 if ((*(__uint64_t *)tmp != 0LL) ||
235 (*(__uint64_t *)(tmp+256) != 0LL)) {
236 printf("mismatched data at offset=%llx, expected 0x%llx, got 0x%llx and 0x%llx\n",
237 (unsigned long long)fileoffset + block * blocksize,
239 *(unsigned long long *)tmp,
240 *(unsigned long long *)(tmp+256));
244 if ( (*(__uint64_t *)tmp !=
245 fileoffset + block * blocksize) ||
246 (*(__uint64_t *)(tmp+256) !=
247 fileoffset + block * blocksize) ) {
248 printf("mismatched data at offset=%llx, "
249 "expected 0x%llx, got 0x%llx and 0x%llx\n",
250 (unsigned long long)fileoffset + block * blocksize,
251 (unsigned long long)fileoffset + block * blocksize,
252 *(unsigned long long *)tmp,
253 *(unsigned long long *)(tmp+256));
258 printf("block %d blocksize %d\n", block,
260 dumpblock((int *)tmp,
261 fileoffset + block * blocksize,
274 direct_setup(char *filename, int fd)
276 if (xfscntl(filename, fd, DIOINFO, &diob) < 0) {
277 perror("xfscntl(FIOINFO)");
280 if (blocksize % diob.d_miniosz) {
281 fprintf(stderr, "blocksize %d must be a multiple of "
282 "%d for direct I/O\n", blocksize, diob.d_miniosz);
290 main(int argc, char *argv[])
292 int seed, ch, fd, oflags;
293 char *filename = NULL;
296 filesize = DEFAULT_FILESIZE;
297 blocksize = DEFAULT_BLOCKSIZE;
298 count = (int) filesize / blocksize;
303 while ((ch = getopt(argc, argv, "b:l:s:c:o:x:vwdrapt")) != EOF) {
305 case 'b': blocksize = atoi(optarg); break;
306 case 'l': filesize = strtoull(optarg, NULL, 16); break;
307 case 's': seed = atoi(optarg); break;
308 case 'c': count = atoi(optarg); break;
309 case 'o': fileoffset = strtoull(optarg, NULL, 16); break;
310 case 'x': extsize = atoi(optarg); break;
311 case 'v': verbose++; break;
312 case 'w': wsync++; break;
313 case 'd': direct++; break;
314 case 'r': rt++; break;
315 case 'a': alloconly++; break;
316 case 'p': preserve++; break;
317 case 't': test++; preserve++; break;
318 default: usage(argv[0]); break;
321 if (optind == argc-1)
322 filename = argv[optind];
325 if ((filesize % blocksize) != 0) {
326 filesize -= filesize % blocksize;
327 printf("filesize not a multiple of blocksize, reducing filesize to %llu\n",
328 (unsigned long long)filesize);
330 if ((fileoffset % blocksize) != 0) {
331 fileoffset -= fileoffset % blocksize;
332 printf("fileoffset not a multiple of blocksize, reducing fileoffset to %llu\n",
333 (unsigned long long)fileoffset);
335 if (count > (filesize/blocksize)) {
336 count = (filesize/blocksize);
337 printf("count of blocks written is too large, setting to %d\n",
339 } else if (count < 1) {
341 printf("count of blocks written is too small, setting to %d\n",
344 printf("randholes: Seed = %d (use \"-s %d\" to re-execute this test)\n", seed, seed);
347 printf("randholes: blocksize=%d, filesize=%llu, seed=%d\n"
348 "randholes: count=%d, offset=%llu, extsize=%d\n",
349 blocksize, (unsigned long long)filesize, seed,
350 count, (unsigned long long)fileoffset, extsize);
351 printf("randholes: verbose=%d, wsync=%d, direct=%d, rt=%d, alloconly=%d, preserve=%d, test=%d\n",
352 verbose, wsync, direct, rt, alloconly, preserve, test);
355 * Open the file, write rand block in random places, read them all
356 * back to check for correctness, then close the file.
358 nvalid = (filesize / blocksize) / 8 + 1;
359 if ((valid = (unsigned char *)calloc(1, (unsigned)nvalid)) == NULL) {
366 oflags=test?(O_RDONLY):(O_RDWR | O_CREAT);
367 oflags |= (preserve ? 0 : O_TRUNC) |
368 (wsync ? O_SYNC : 0) |
369 (direct ? O_DIRECT : 0);
371 if ((fd = open(filename, oflags, 0666)) < 0) {
377 if (xfscntl(filename, fd, FSGETXATTR, &rtattr) < 0) {
378 perror("xfsnctl(FSGETXATTR)");
381 if ((rtattr.fsx_xflags & XFS_XFLAG_REALTIME) == 0 ||
382 (extsize && rtattr.fsx_extsize != extsize * blocksize)) {
383 rtattr.fsx_xflags |= XFS_XFLAG_REALTIME;
385 rtattr.fsx_extsize = extsize * blocksize;
386 if (xfscntl(filename, fd, FSSETXATTR, &rtattr) < 0) {
387 perror("xfscntl(FSSETXATTR)");
393 if (direct && direct_setup(filename, fd))
396 printf(test?"write (skipped)\n":"write\n");
397 writeblks(filename, fd);
398 printf("readback\n");
407 printf("randholes: %d errors found during readback\n", r);
410 printf("randholes: ok\n");