summaryrefslogtreecommitdiff
path: root/src/randholes.c
blob: 648cabf7c4694f01661b9f94752b55ae0d875d70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (c) 2000-2003, 2010 SGI
 * All Rights Reserved.
 */
 
#include <inttypes.h>

#include "global.h"

#define	power_of_2(x)	((x) && !((x) & ((x) - 1)))
#define	DEFAULT_FILESIZE	((uint64_t) (256 * 1024 * 1024))
#define	DEFAULT_BLOCKSIZE	512

#define	SETBIT(ARRAY, N)	((ARRAY)[(N)/8] |= (1 << ((N)%8)))
#define	BITVAL(ARRAY, N)	((ARRAY)[(N)/8] & (1 << ((N)%8)))

/* Bit-vector array showing which blocks have been written */
static unsigned char	*valid;

static uint64_t filesize;
static uint64_t fileoffset;

static unsigned int blocksize;
static int count;
static int verbose;
static int wsync;
static int direct;
static int alloconly;
static int rt;
static int extsize;	/* used only for real-time */
static int preserve;
static int test;

#define	READ_XFER	256	/* blocks to read at a time when checking */

/*
 * Define xfscntl() to mask the difference between the Linux
 * and the Irix fcntl() interfaces to XFS for user space.  The
 * "cmd" argument is just the last part of the command, e.g.
 * pass FSGETXATTR in place of either XFS_IOC_FSGETXATTR (Linux)
 * F_FSGETXATTR (Irix).
 *
 */
#  define xfscntl(filename, fd, cmd, arg) \
		xfsctl((filename), (fd), XFS_IOC_ ## cmd, (arg))

static void
usage(char *progname)
{
	fprintf(stderr,
		"usage: %s [-l filesize] [-b blocksize] [-c count]\n"
		"\t\t[-o write_offset] [-s seed] [-r [-x extentsize]]\n"
		"\t\t[-w] [-v] [-d] [-a] [-p] [-t] filename\n\n",
		progname);
	fprintf(stderr, "\tdefault filesize is %" PRIu64 " bytes\n",
		DEFAULT_FILESIZE);
	fprintf(stderr, "\tdefault blocksize is %u bytes\n",
		DEFAULT_BLOCKSIZE);
	fprintf(stderr, "\tdefault count is %d block-sized writes\n",
		(int) (DEFAULT_FILESIZE / DEFAULT_BLOCKSIZE));
	fprintf(stderr, "\tdefault write_offset is %" PRIu64 " bytes\n",
		(uint64_t) 0);
	exit(1);
}

/* Returns filename if successful or a null pointer if an error occurs */
static char *
parseargs(int argc, char *argv[])
{
	int seed;
	int ch;

	filesize = DEFAULT_FILESIZE;
	blocksize = DEFAULT_BLOCKSIZE;
	count = (int) filesize / blocksize;
	verbose = 0;
	wsync = 0;
	seed = time(NULL);
	test = 0;
	while ((ch = getopt(argc, argv, "b:l:s:c:o:x:vwdrapt")) != EOF) {
		switch(ch) {
		case 'b':	blocksize  = atoi(optarg);	break;
		case 'l':	filesize   = strtoull(optarg, NULL, 16); break;
		case 's':	seed       = atoi(optarg);	break;
		case 'c':	count      = atoi(optarg);	break;
		case 'o':	fileoffset = strtoull(optarg, NULL, 16); break;
		case 'x':	extsize    = atoi(optarg);	break;
		case 'v':	verbose++;			break;
		case 'w':	wsync++;			break;
		case 'd':	direct++;			break;
		case 'r':	rt++; direct++;			break;
		case 'a':	alloconly++;			break;
		case 'p':	preserve++;			break;
		case 't':	test++; preserve++;		break;
		default:	usage(argv[0]);			break;
		}
	}
	if (optind != argc - 1)
		usage(argv[0]);

	if ((filesize % blocksize) != 0) {
		filesize -= filesize % blocksize;
		printf("filesize not a multiple of blocksize, "
			"reducing filesize to %llu\n",
		       (unsigned long long) filesize);
	}
	if ((fileoffset % blocksize) != 0) {
		fileoffset -= fileoffset % blocksize;
		printf("fileoffset not a multiple of blocksize, "
			"reducing fileoffset to %llu\n",
		       (unsigned long long) fileoffset);
	}
	if (count > (filesize/blocksize)) {
		count = (filesize/blocksize);
		printf("count of blocks written is too large, "
			"setting to %d\n", count);
	} else if (count < 1) {
		count = 1;
		printf("count of blocks written is too small, "
			"setting to %d\n", count);
	}
	printf("randholes: Seed = %d (use \"-s %d\" "
		"to re-execute this test)\n", seed, seed);
	srandom(seed);

        printf("randholes: blocksize=%d, filesize=%llu, seed=%d\n"
               "randholes: count=%d, offset=%llu, extsize=%d\n",
                blocksize, (unsigned long long)filesize, seed,
	       count, (unsigned long long)fileoffset, extsize);
        printf("randholes: verbose=%d, wsync=%d, direct=%d, "
		"rt=%d, alloconly=%d, preserve=%d, test=%d\n",
                verbose, wsync, direct ? 1 : 0, rt, alloconly, preserve, test);

	/* Last argument is the file name.  Return it. */

	return argv[optind];	/* Success */
}

/*
 * Determine the next random block number to which to write.
 * If an already-written block is selected, choose the next
 * unused higher-numbered block.  Returns the block number,
 * or -1 if we exhaust available blocks looking for an unused
 * one.
 */
static int
findblock(void)
{
	int block, numblocks;

	numblocks = filesize / blocksize;
	block = random() % numblocks;

	while (BITVAL(valid, block)) {
		if (++block == numblocks) {
			printf("returning block -1\n");
			return -1;
		}
	}
	return block;
}

static void
dumpblock(int *buffer, uint64_t offset, int blocksize)
{
	int	i;

	for (i = 0; i < (blocksize / 16); i++) {
		printf("%llx: 0x%08x 0x%08x 0x%08x 0x%08x\n",
		       (unsigned long long) offset, *buffer, *(buffer + 1),
		       *(buffer + 2), *(buffer + 3));
		offset += 16;
		buffer += 4;
	}
}

static void
writeblks(char *fname, int fd, size_t alignment)
{
	uint64_t offset;
	char *buffer = NULL;
	int block;
	int ret;
	struct flock64 fl;

	if (!test) {
		ret = posix_memalign((void **) &buffer, alignment, blocksize);
		if (ret) {
			fprintf(stderr, "posix_memalign: %s\n", strerror(ret));
			exit(1);
		}
		memset(buffer, 0, blocksize);
	}

	/*
	 * Avoid allocation patterns being perturbed by different speculative
	 * preallocation beyond EOF configurations by first truncating the file
	 * to the expected maximum file size.
	 */
	if (ftruncate(fd, filesize) < 0) {
		perror("ftruncate");
		exit(EXIT_FAILURE);
	}

	do {
		if (verbose && ((count % 100) == 0)) {
			printf(".");
			fflush(stdout);
		}
		block = findblock();
		if (block < 0) {
		    perror("findblock");
		    exit(1);
		}

		offset = (uint64_t) block * blocksize;
		if (alloconly) {
                        if (test) continue;
                        
			fl.l_start = fileoffset + offset;
			fl.l_len = blocksize;
			fl.l_whence = 0;

			if (xfscntl(fname, fd, RESVSP64, &fl) < 0) {
				perror("xfsnctl(RESVSP64)");
				exit(1);
			}
			continue;
		}
		SETBIT(valid, block);
                if (!test) {
		        if (lseek64(fd, fileoffset + offset, SEEK_SET) < 0) {
			        perror("lseek");
			        exit(1);
		        }
			/*
			 * Before writing, record offset at the base
			 * of the buffer and at offset 256 bytes
			 * into it.  We'll verify this when we read
			 * it back in again.
			 */
			*(uint64_t *) buffer = fileoffset + offset;
			*(uint64_t *) (buffer + 256) = fileoffset + offset;

		        if (write(fd, buffer, blocksize) < blocksize) {
			        perror("write");
			        exit(1);
		        }
                }
		if (verbose > 1) {
			printf("%swriting data at offset=%llx\n",
				test ? "NOT " : "",
				(unsigned long long) (fileoffset + offset));
		}
	} while (--count);

	free(buffer);
}

static int
readblks(int fd, size_t alignment)
{
	uint64_t offset;
	char *buffer, *tmp;
	unsigned int xfer, block, i;
        int err=0;

	if (alloconly)
		return 0;
	xfer = READ_XFER*blocksize;
	err = posix_memalign((void **) &buffer, alignment, xfer);
	if (err) {
		fprintf(stderr, "posix_memalign: %s\n", strerror(err));
		exit(1);
	}
	memset(buffer, 0, xfer);
	if (verbose)
		printf("\n");

	if (lseek64(fd, fileoffset, SEEK_SET) < 0) {
		perror("lseek");
		exit(1);
	}
	block = 0;
	offset = 0;
	while (offset < filesize) {
		if ((i = read(fd, buffer, xfer) < xfer)) {
			if (i < 2)
				break;
			perror("read");
			exit(1);
		}
		tmp = buffer;
		for (i = 0; i < READ_XFER; i++) {
			uint64_t want;
			uint64_t first;
			uint64_t second;

			if (verbose && ((block % 100) == 0)) {
				printf("+");
				fflush(stdout);
			}

			want = BITVAL(valid, block) ? offset : 0;
			first = *(uint64_t *) tmp;
			second = *(uint64_t *) (tmp + 256);
			if (first != want || second != want) {
				printf("mismatched data at offset=0x%" PRIx64
					", expected 0x%" PRIx64
					", got 0x%" PRIx64
					" and 0x%" PRIx64 "\n",
					 fileoffset + offset, want,
					 first, second);
				err++;
			}
			if (verbose > 2) {
				printf("block %d blocksize %d\n", block,
				       blocksize);
				dumpblock((int *)tmp, fileoffset + offset,
					  blocksize);
			}

			block++;
			offset += blocksize;
			tmp += blocksize;
		}
	}
	if (verbose)
		printf("\n");

	free(buffer);
        return err;
}

/*
 * Determine the memory alignment required for I/O buffers.  For
 * direct I/O we request the needed information from the file
 * system; otherwise pointer alignment is fine.  Returns the
 * alignment multiple, or 0 if an error occurs.
 */
static size_t
get_alignment(char *filename, int fd)
{
	struct dioattr dioattr;

	if (! direct)
		return sizeof (void *);

	memset(&dioattr, 0, sizeof dioattr);
	if (xfscntl(filename, fd, DIOINFO, &dioattr) < 0) {
		perror("xfscntl(FIOINFO)");
		return 0;
	}

	/* Make sure the alignment meets the needs of posix_memalign() */

	if (dioattr.d_mem % sizeof (void *) || ! power_of_2(dioattr.d_mem)) {
		perror("d_mem bad");
		return 0;
	}

	/*
	 * Also make sure user doesn't specify a block size that's
	 * incompatible with the underlying file system.
	 */
	if (! dioattr.d_miniosz) {
		perror("miniosz == 0!");
		return 0;
	}
	if (blocksize % dioattr.d_miniosz) {
		fprintf(stderr, "blocksize %d must be a multiple of "
			"%d for direct I/O\n", blocksize, dioattr.d_miniosz);
		return 0;
	}

	return (size_t) dioattr.d_mem;
}

static int
realtime_setup(char *filename, int fd)
{
	struct fsxattr rtattr;

	(void) memset(&rtattr, 0, sizeof rtattr);
	if (xfscntl(filename, fd, FSGETXATTR, &rtattr) < 0) {
		perror("FSGETXATTR)");
		return 1;
	}
	if ((rtattr.fsx_xflags & XFS_XFLAG_REALTIME) == 0 ||
	    (extsize && rtattr.fsx_extsize != extsize * blocksize)) {
		rtattr.fsx_xflags |= XFS_XFLAG_REALTIME;
		if (extsize)
			rtattr.fsx_extsize = extsize * blocksize;
		if (xfscntl(filename, fd, FSSETXATTR, &rtattr) < 0) {
			perror("FSSETXATTR)");
			return 1;
		}
	}

	return 0;
}

int
main(int argc, char *argv[])
{
	char	*filename;
	size_t	size;
	int	oflags;
	int	fd;
	size_t	alignment;
	int	errors;

	filename = parseargs(argc, argv);
	if (! filename)
		return 1;

	/*
	 * Allocate a bitmap big enough to track the range of
	 * blocks we'll be dealing with.
	 */
	size = (filesize / blocksize) / 8 + 1;
	valid = malloc(size);
	if ((valid = malloc(size)) == NULL) {
		perror("malloc");
		return 1;
	}
	memset(valid, 0, size);

	/* Lots of arguments affect how we open the file */
	oflags = test ? O_RDONLY : O_RDWR|O_CREAT;
	oflags |= preserve ? 0 : O_TRUNC;
	oflags |= wsync ? O_SYNC : 0;
	oflags |= direct ? O_DIRECT : 0;

	/*
	 * Open the file, write rand block in random places, read them all
	 * back to check for correctness, then close the file.
	 */
	if ((fd = open(filename, oflags, 0666)) < 0) {
		perror("open");
		return 1;
	}
	if (rt && realtime_setup(filename, fd))
		return 1;
	alignment = get_alignment(filename, fd);
	if (! alignment)
		return 1;

        printf("write%s\n", test ? " (skipped)" : "");
	writeblks(filename, fd, alignment);

        printf("readback\n");
	errors = readblks(fd, alignment);

	if (close(fd) < 0) {
		perror("close");
		return 1;
	}
	free(valid);

        if (errors) {
		printf("randholes: %d errors found during readback\n", errors);
		return 2;
        }

	printf("randholes: ok\n");

	return 0;
}