summaryrefslogtreecommitdiff
path: root/src/fstest.c
blob: 4f6dc643dd12fa49a385aa047de1d51ba9de483c (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
/* Verification tool, designed to detect data corruption on a filesystem

   tridge@samba.org, March 2002
   
   XFS space preallocation changes -- lord@sgi.com, April 2003
 */

#include "global.h"

#include <sys/mman.h>

/* variables settable on the command line */
static int loop_count = 100;
static int num_files = 1;
static int file_size = 1024*1024;
static int block_size = 1024;
static char *base_dir = ".";
static int use_mmap;
static int do_prealloc;
static int use_sync;
static int do_frags = 1;

typedef unsigned char uchar;

#ifndef MIN
#define MIN(a,b) ((a)<(b)?(a):(b))
#endif

static void *x_malloc(int size)
{
	void *ret = malloc(size);
	if (!ret) {
		fprintf(stderr,"Out of memory for size %d!\n", size);
		exit(1);
	}
	return ret;
}


/* generate a buffer for a particular child, fnum etc. Just use a simple buffer
   to make debugging easy 
*/
static void gen_buffer(char *buf, int loop, int child, int fnum, int ofs)
{
	uchar v = (loop+child+fnum+(ofs/block_size)) % 256;
	memset(buf, v, block_size);
}

/* 
   check if a buffer from disk is correct
*/
static void check_buffer(uchar *buf, int loop, int child, int fnum, int ofs)
{
	char *buf2;

	buf2 = x_malloc(block_size);

	gen_buffer(buf2, loop, child, fnum, ofs);
	
	if (memcmp(buf, buf2, block_size) != 0) {
		int i, j;
		for (i=0;buf[i] == buf2[i] && i<block_size;i++) ;
		fprintf(stderr,"Corruption in child %d fnum %d at offset %d\n",
			child, fnum, ofs+i);

		printf("Correct:   ");
		for (j=0;j<MIN(20, block_size-i);j++) {
			printf("%02x ", buf2[j+i]);
		}
		printf("\n");

		printf("Incorrect: ");
		for (j=0;j<MIN(20, block_size-i);j++) {
			printf("%02x ", buf[j+i]);
		}
		for (j=i;buf[j] != buf2[j] && j<block_size;j++) ;
		printf("Corruption length: %d\n", j - i);
		printf("\n");
		exit(1);
	}

	free(buf2);
}

/*
  create a file with a known data set for a child
 */
static void create_file(const char *dir, int loop, int child, int fnum)
{
	char *buf;
	int size, fd, ret;
	char fname[1024];

	buf = x_malloc(block_size);
	ret = snprintf(fname, sizeof(fname), "%s/file%d", dir, fnum);
	if (ret < 0 || ret >= sizeof(fname)) {
		fprintf(stderr,"file path '%s' too long %d\n", dir, ret);
		exit(1);
	}

	fd = open(fname, O_RDWR|O_CREAT|O_TRUNC | (use_sync?O_SYNC:0), 0644);
	if (fd == -1) {
		perror(fname);
		exit(1);
	}

	if (do_prealloc) {
		struct flock64 resv;

		resv.l_whence = 0;
		resv.l_start = 0;
		resv.l_len = file_size;

#ifdef XFS_IOC_RESVSP64
		if ((xfsctl(fname, fd, XFS_IOC_RESVSP64, &resv)) < 0) {
			perror(fname);
			exit(1);
		}
#else
#ifdef F_RESVSP64
		if ((fcntl(fd, F_RESVSP64, &resv)) < 0) {
			perror(fname);
			exit(1);
		}
#else
bozo!
#endif
#endif
	}
		
	if (!use_mmap) {
		for (size=0; size<file_size; size += block_size * do_frags) {
			gen_buffer(buf, loop, child, fnum, size);
			if (pwrite(fd, buf, block_size, size) != block_size) {
				fprintf(stderr,"Write failed at offset %d\n", size);
				exit(1);
			}
		}
	} else {
		char *p;
		if (ftruncate(fd, file_size) != 0) {
			perror("ftruncate");
			exit(1);
		}
		p = mmap(NULL, file_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
		if (p == MAP_FAILED) {
			perror("mmap");
			exit(1);
		}
		for (size=0; size<file_size; size += block_size * do_frags) {
			gen_buffer(p+size, loop, child, fnum, size);
		}
		munmap(p, file_size);
	}

	free(buf);
	close(fd);
}

/* 
   check that a file has the right data
 */
static void check_file(const char *dir, int loop, int child, int fnum)
{
	uchar *buf;
	int size, fd, ret;
	char fname[1024];

	buf = x_malloc(block_size);

	ret = snprintf(fname, sizeof(fname), "%s/file%d", dir, fnum);
	if (ret < 0 || ret >= sizeof(fname)) {
		fprintf(stderr,"file path is '%s' too long %d\n", dir, ret);
		exit(1);
	}
	fd = open(fname, O_RDONLY);
	if (fd == -1) {
		perror(fname);
		exit(1);
	}

	for (size=0; size<file_size; size += block_size * do_frags) {
		if (pread(fd, buf, block_size, size) != block_size) {
			fprintf(stderr,"read failed at offset %d\n", size);
			exit(1);
		}
		check_buffer(buf, loop, child, fnum, size);
	}

	free(buf);
	close(fd);
}

/* 
   recursive directory traversal - used for cleanup
   fn() is called on all files/dirs in the tree
 */
void traverse(const char *dir, int (*fn)(const char *))
{
	DIR *d;
	struct dirent *de;

	d = opendir(dir);
	if (!d) return;

	while ((de = readdir(d))) {
		char fname[1024];
		struct stat st;

		if (strcmp(de->d_name,".") == 0) continue;
		if (strcmp(de->d_name,"..") == 0) continue;

		sprintf(fname, "%s/%s", dir, de->d_name);
		if (lstat(fname, &st)) {
			perror(fname);
			continue;
		}

		if (S_ISDIR(st.st_mode)) {
			traverse(fname, fn);
		}

		fn(fname);
	}

	closedir(d);
}

/* the main child function - this creates/checks the file for one child */
static void run_child(int child)
{
	int i, loop;
	char dir[1024];

	sprintf(dir, "%s/child%d", base_dir, child);

	/* cleanup any old files */
	if (remove(dir) != 0 && errno != ENOENT) {
		printf("Child %d cleaning %s\n", child, dir);
		traverse(dir, remove);
		remove(dir);
	}

	if (mkdir(dir, 0755) != 0) {
		perror(dir);
		exit(1);
	}

	for (loop = 0; loop < loop_count; loop++) {
		printf("Child %d loop %d\n", child, loop);
		for (i=0;i<num_files;i++) {
			create_file(dir, loop, child, i);
		}
		for (i=0;i<num_files;i++) {
			check_file(dir, loop, child, i);
		}
	}

	/* cleanup afterwards */
	printf("Child %d cleaning up %s\n", child, dir);
	traverse(dir, remove);
	remove(dir);

	exit(0);
}

static void usage(void)
{
	printf("\n"
"Usage: fstest [options]\n"
"\n"
" -F			generate files with holes\n"
" -n num_children       set number of child processes\n"
" -f num_files          set number of files\n"
" -s file_size          set file sizes\n"
" -b block_size         set block (IO) size\n"
" -p path               set base path\n"
" -l loops              set loop count\n"
" -m                    use mmap\n"
" -S                    use synchronous IO\n"
" -P                    preallocate space\n"
" -h                    show this help message\n");
}

/* main program */
int main(int argc, char *argv[])
{
	int c;
	extern char *optarg;
	extern int optind;
	int num_children = 1;
	int i, status, ret;

	while ((c = getopt(argc, argv, "FPn:s:f:p:l:b:Shm")) != -1) {
		switch (c) {
		case 'F':
			do_frags = 2;
			break;
		case 'n':
			num_children = strtol(optarg, NULL, 0);
			break;
		case 'b':
			block_size = strtol(optarg, NULL, 0);
			break;
		case 'f':
			num_files = strtol(optarg, NULL, 0);
			break;
		case 's':
			file_size = strtol(optarg, NULL, 0);
			break;
		case 'p':
			base_dir = optarg;
			break;
		case 'm':
			use_mmap = 1;
			break;
		case 'P':
			do_prealloc = 1;
			break;
		case 'S':
			use_sync = 1;
			break;
		case 'l':
			loop_count = strtol(optarg, NULL, 0);
			break;
		case 'h':
			usage();
			exit(0);
		default:
			usage();
			exit(1);
		}
	}

	argc -= optind;
	argv += optind;

	/* round up the file size */
	if (file_size % block_size != 0) {
		file_size = (file_size + (block_size-1)) / block_size;
		file_size *= block_size;
		printf("Rounded file size to %d\n", file_size);
	}

	printf("num_children=%d file_size=%d num_files=%d loop_count=%d block_size=%d\nmmap=%d sync=%d prealloc=%d\n",
	       num_children, file_size, num_files, loop_count, block_size, use_mmap, use_sync, do_prealloc);

	printf("Total data size %.1f Mbyte\n",
	       num_files * num_children * 1.0e-6 * file_size);

	/* fork and run run_child() for each child */
	for (i=0;i<num_children;i++) {
		if (fork() == 0) {
			run_child(i);
			exit(0);
		}
	}

	ret = 0;

	/* wait for children to exit */
	while (waitpid(0, &status, 0) == 0 || errno != ECHILD) {
		if (WEXITSTATUS(status) != 0) {
			ret = WEXITSTATUS(status);
			printf("Child exited with status %d\n", ret);
		}
	}

	if (ret != 0) {
		printf("fstest failed with status %d\n", ret);
	}

	return ret;
}