1 /* Verification tool, designed to detect data corruption on a filesystem
3 tridge@samba.org, March 2002
5 XFS space preallocation changes -- lord@sgi.com, April 2003
12 /* variables settable on the command line */
13 static int loop_count = 100;
14 static int num_files = 1;
15 static int file_size = 1024*1024;
16 static int block_size = 1024;
17 static char *base_dir = ".";
19 static int do_prealloc;
21 static int do_frags = 1;
23 typedef unsigned char uchar;
26 #define MIN(a,b) ((a)<(b)?(a):(b))
29 static void *x_malloc(int size)
31 void *ret = malloc(size);
33 fprintf(stderr,"Out of memory for size %d!\n", size);
40 /* generate a buffer for a particular child, fnum etc. Just use a simple buffer
41 to make debugging easy
43 static void gen_buffer(char *buf, int loop, int child, int fnum, int ofs)
45 uchar v = (loop+child+fnum+(ofs/block_size)) % 256;
46 memset(buf, v, block_size);
50 check if a buffer from disk is correct
52 static void check_buffer(uchar *buf, int loop, int child, int fnum, int ofs)
56 buf2 = x_malloc(block_size);
58 gen_buffer(buf2, loop, child, fnum, ofs);
60 if (memcmp(buf, buf2, block_size) != 0) {
62 for (i=0;buf[i] == buf2[i] && i<block_size;i++) ;
63 fprintf(stderr,"Corruption in child %d fnum %d at offset %d\n",
67 for (j=0;j<MIN(20, block_size-i);j++) {
68 printf("%02x ", buf2[j+i]);
72 printf("Incorrect: ");
73 for (j=0;j<MIN(20, block_size-i);j++) {
74 printf("%02x ", buf[j+i]);
76 for (j=i;buf[j] != buf2[j] && j<block_size;j++) ;
77 printf("Corruption length: %d\n", j - i);
86 create a file with a known data set for a child
88 static void create_file(const char *dir, int loop, int child, int fnum)
94 buf = x_malloc(block_size);
95 ret = snprintf(fname, sizeof(fname), "%s/file%d", dir, fnum);
96 if (ret < 0 || ret >= sizeof(fname)) {
97 fprintf(stderr,"file path '%s' too long %d\n", dir, ret);
101 fd = open(fname, O_RDWR|O_CREAT|O_TRUNC | (use_sync?O_SYNC:0), 0644);
112 resv.l_len = file_size;
114 #ifdef XFS_IOC_RESVSP64
115 if ((xfsctl(fname, fd, XFS_IOC_RESVSP64, &resv)) < 0) {
121 if ((fcntl(fd, F_RESVSP64, &resv)) < 0) {
132 for (size=0; size<file_size; size += block_size * do_frags) {
133 gen_buffer(buf, loop, child, fnum, size);
134 if (pwrite(fd, buf, block_size, size) != block_size) {
135 fprintf(stderr,"Write failed at offset %d\n", size);
141 if (ftruncate(fd, file_size) != 0) {
145 p = mmap(NULL, file_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
146 if (p == MAP_FAILED) {
150 for (size=0; size<file_size; size += block_size * do_frags) {
151 gen_buffer(p+size, loop, child, fnum, size);
153 munmap(p, file_size);
161 check that a file has the right data
163 static void check_file(const char *dir, int loop, int child, int fnum)
169 buf = x_malloc(block_size);
171 ret = snprintf(fname, sizeof(fname), "%s/file%d", dir, fnum);
172 if (ret < 0 || ret >= sizeof(fname)) {
173 fprintf(stderr,"file path is '%s' too long %d\n", dir, ret);
176 fd = open(fname, O_RDONLY);
182 for (size=0; size<file_size; size += block_size * do_frags) {
183 if (pread(fd, buf, block_size, size) != block_size) {
184 fprintf(stderr,"read failed at offset %d\n", size);
187 check_buffer(buf, loop, child, fnum, size);
195 recursive directory traversal - used for cleanup
196 fn() is called on all files/dirs in the tree
198 void traverse(const char *dir, int (*fn)(const char *))
206 while ((de = readdir(d))) {
210 if (strcmp(de->d_name,".") == 0) continue;
211 if (strcmp(de->d_name,"..") == 0) continue;
213 sprintf(fname, "%s/%s", dir, de->d_name);
214 if (lstat(fname, &st)) {
219 if (S_ISDIR(st.st_mode)) {
229 /* the main child function - this creates/checks the file for one child */
230 static void run_child(int child)
235 sprintf(dir, "%s/child%d", base_dir, child);
237 /* cleanup any old files */
238 if (remove(dir) != 0 && errno != ENOENT) {
239 printf("Child %d cleaning %s\n", child, dir);
240 traverse(dir, remove);
244 if (mkdir(dir, 0755) != 0) {
249 for (loop = 0; loop < loop_count; loop++) {
250 printf("Child %d loop %d\n", child, loop);
251 for (i=0;i<num_files;i++) {
252 create_file(dir, loop, child, i);
254 for (i=0;i<num_files;i++) {
255 check_file(dir, loop, child, i);
259 /* cleanup afterwards */
260 printf("Child %d cleaning up %s\n", child, dir);
261 traverse(dir, remove);
267 static void usage(void)
270 "Usage: fstest [options]\n"
272 " -F generate files with holes\n"
273 " -n num_children set number of child processes\n"
274 " -f num_files set number of files\n"
275 " -s file_size set file sizes\n"
276 " -b block_size set block (IO) size\n"
277 " -p path set base path\n"
278 " -l loops set loop count\n"
280 " -S use synchronous IO\n"
281 " -P preallocate space\n"
282 " -h show this help message\n");
286 int main(int argc, char *argv[])
291 int num_children = 1;
294 while ((c = getopt(argc, argv, "FPn:s:f:p:l:b:Shm")) != -1) {
300 num_children = strtol(optarg, NULL, 0);
303 block_size = strtol(optarg, NULL, 0);
306 num_files = strtol(optarg, NULL, 0);
309 file_size = strtol(optarg, NULL, 0);
324 loop_count = strtol(optarg, NULL, 0);
338 /* round up the file size */
339 if (file_size % block_size != 0) {
340 file_size = (file_size + (block_size-1)) / block_size;
341 file_size *= block_size;
342 printf("Rounded file size to %d\n", file_size);
345 printf("num_children=%d file_size=%d num_files=%d loop_count=%d block_size=%d\nmmap=%d sync=%d prealloc=%d\n",
346 num_children, file_size, num_files, loop_count, block_size, use_mmap, use_sync, do_prealloc);
348 printf("Total data size %.1f Mbyte\n",
349 num_files * num_children * 1.0e-6 * file_size);
351 /* fork and run run_child() for each child */
352 for (i=0;i<num_children;i++) {
361 /* wait for children to exit */
362 while (waitpid(0, &status, 0) == 0 || errno != ECHILD) {
363 if (WEXITSTATUS(status) != 0) {
364 ret = WEXITSTATUS(status);
365 printf("Child exited with status %d\n", ret);
370 printf("fstest failed with status %d\n", ret);