Adds a new fstest invocation test (074).
[xfstests-dev.git] / src / fstest.c
1 /* Verification tool, designed to detect data corruption on a filesystem
2
3    tridge@samba.org, March 2002
4    
5    XFS space preallocation changes -- lord@sgi.com, April 2003
6  */
7 #include <dirent.h>
8 #include <sys/mman.h>
9 #include <sys/wait.h>
10 #include <xfs/libxfs.h>
11
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 = ".";
18 static int use_mmap;
19 static int do_prealloc;
20 static int use_sync;
21 static int do_frags = 1;
22
23 typedef unsigned char uchar;
24
25 #ifndef MIN
26 #define MIN(a,b) ((a)<(b)?(a):(b))
27 #endif
28
29 static void *x_malloc(int size)
30 {
31         void *ret = malloc(size);
32         if (!ret) {
33                 fprintf(stderr,"Out of memory for size %d!\n", size);
34                 exit(1);
35         }
36         return ret;
37 }
38
39
40 /* generate a buffer for a particular child, fnum etc. Just use a simple buffer
41    to make debugging easy 
42 */
43 static void gen_buffer(uchar *buf, int loop, int child, int fnum, int ofs)
44 {
45         uchar v = (loop+child+fnum+(ofs/block_size)) % 256;
46         memset(buf, v, block_size);
47 }
48
49 /* 
50    check if a buffer from disk is correct
51 */
52 static void check_buffer(uchar *buf, int loop, int child, int fnum, int ofs)
53 {
54         uchar *buf2;
55
56         buf2 = x_malloc(block_size);
57
58         gen_buffer(buf2, loop, child, fnum, ofs);
59         
60         if (memcmp(buf, buf2, block_size) != 0) {
61                 int i, j;
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",
64                         child, fnum, ofs+i);
65
66                 printf("Correct:   ");
67                 for (j=0;j<MIN(20, block_size-i);j++) {
68                         printf("%02x ", buf2[j+i]);
69                 }
70                 printf("\n");
71
72                 printf("Incorrect: ");
73                 for (j=0;j<MIN(20, block_size-i);j++) {
74                         printf("%02x ", buf[j+i]);
75                 }
76                 for (j=i;buf[j] == buf2[j] && j<block_size;j++) ;
77                 printf("Corruption length: %d\n", j - i);
78                 printf("\n");
79                 exit(1);
80         }
81
82         free(buf2);
83 }
84
85 /*
86   create a file with a known data set for a child
87  */
88 static void create_file(const char *dir, int loop, int child, int fnum)
89 {
90         uchar *buf;
91         int size, fd;
92         char fname[1024];
93
94         buf = x_malloc(block_size);
95         sprintf(fname, "%s/file%d", dir, fnum);
96         fd = open(fname, O_RDWR|O_CREAT|O_TRUNC | (use_sync?O_SYNC:0), 0644);
97         if (fd == -1) {
98                 perror(fname);
99                 exit(1);
100         }
101
102         if (do_prealloc) {
103                 xfs_flock64_t   resv;
104
105                 resv.l_whence = 0;
106                 resv.l_start = 0;
107                 resv.l_len = file_size;
108
109                 if ((xfsctl(fname, fd, XFS_IOC_RESVSP, &resv)) < 0) {
110                         perror(fname);
111                         exit(1);
112                 }
113         }
114                 
115         if (!use_mmap) {
116                 for (size=0; size<file_size; size += block_size * do_frags) {
117                         gen_buffer(buf, loop, child, fnum, size);
118                         if (pwrite(fd, buf, block_size, size) != block_size) {
119                                 fprintf(stderr,"Write failed at offset %d\n", size);
120                                 exit(1);
121                         }
122                 }
123         } else {
124                 char *p;
125                 if (ftruncate(fd, file_size) != 0) {
126                         perror("ftruncate");
127                         exit(1);
128                 }
129                 p = mmap(NULL, file_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
130                 if (p == (char *)-1) {
131                         perror("mmap");
132                         exit(1);
133                 }
134                 for (size=0; size<file_size; size += block_size * do_frags) {
135                         gen_buffer(p+size, loop, child, fnum, size);
136                 }
137                 munmap(p, file_size);
138         }
139
140         free(buf);
141         close(fd);
142 }
143
144 /* 
145    check that a file has the right data
146  */
147 static void check_file(const char *dir, int loop, int child, int fnum)
148 {
149         uchar *buf;
150         int size, fd;
151         char fname[1024];
152
153         buf = x_malloc(block_size);
154
155         sprintf(fname, "%s/file%d", dir, fnum);
156         fd = open(fname, O_RDONLY);
157         if (fd == -1) {
158                 perror(fname);
159                 exit(1);
160         }
161
162         for (size=0; size<file_size; size += block_size * do_frags) {
163                 if (pread(fd, buf, block_size, size) != block_size) {
164                         fprintf(stderr,"read failed at offset %d\n", size);
165                         exit(1);
166                 }
167                 check_buffer(buf, loop, child, fnum, size);
168         }
169
170         free(buf);
171         close(fd);
172 }
173
174 /* 
175    recursive directory traversal - used for cleanup
176    fn() is called on all files/dirs in the tree
177  */
178 void traverse(const char *dir, int (*fn)(const char *))
179 {
180         DIR *d;
181         struct dirent *de;
182
183         d = opendir(dir);
184         if (!d) return;
185
186         while ((de = readdir(d))) {
187                 char fname[1024];
188                 struct stat st;
189
190                 if (strcmp(de->d_name,".") == 0) continue;
191                 if (strcmp(de->d_name,"..") == 0) continue;
192
193                 sprintf(fname, "%s/%s", dir, de->d_name);
194                 if (lstat(fname, &st)) {
195                         perror(fname);
196                         continue;
197                 }
198
199                 if (S_ISDIR(st.st_mode)) {
200                         traverse(fname, fn);
201                 }
202
203                 fn(fname);
204         }
205
206         closedir(d);
207 }
208
209 /* the main child function - this creates/checks the file for one child */
210 static void run_child(int child)
211 {
212         int i, loop;
213         char dir[1024];
214
215         sprintf(dir, "%s/child%d", base_dir, child);
216
217         /* cleanup any old files */
218         if (remove(dir) != 0 && errno != ENOENT) {
219                 printf("Child %d cleaning %s\n", child, dir);
220                 traverse(dir, remove);
221                 remove(dir);
222         }
223
224         if (mkdir(dir, 0755) != 0) {
225                 perror(dir);
226                 exit(1);
227         }
228
229         for (loop = 0; loop < loop_count; loop++) {
230                 printf("Child %d loop %d\n", child, loop);
231                 for (i=0;i<num_files;i++) {
232                         create_file(dir, loop, child, i);
233                 }
234                 for (i=0;i<num_files;i++) {
235                         check_file(dir, loop, child, i);
236                 }
237         }
238
239         /* cleanup afterwards */
240         printf("Child %d cleaning up %s\n", child, dir);
241         traverse(dir, remove);
242         remove(dir);
243
244         exit(0);
245 }
246
247 static void usage(void)
248 {
249         printf("\n"
250 "Usage: fstest [options]\n"
251 "\n"
252 " -F                    generate files with holes\n"
253 " -n num_children       set number of child processes\n"
254 " -f num_files          set number of files\n"
255 " -s file_size          set file sizes\n"
256 " -b block_size         set block (IO) size\n"
257 " -p path               set base path\n"
258 " -l loops              set loop count\n"
259 " -m                    use mmap\n"
260 " -S                    use synchronous IO\n"
261 " -P                    preallocate space\n"
262 " -h                    show this help message\n");
263 }
264
265 /* main program */
266 int main(int argc, char *argv[])
267 {
268         int c;
269         extern char *optarg;
270         extern int optind;
271         int num_children = 1;
272         int i, status, ret;
273
274         while ((c = getopt(argc, argv, "FPn:s:f:p:l:b:Shm")) != -1) {
275                 switch (c) {
276                 case 'F':
277                         do_frags = 2;
278                         break;
279                 case 'n':
280                         num_children = strtol(optarg, NULL, 0);
281                         break;
282                 case 'b':
283                         block_size = strtol(optarg, NULL, 0);
284                         break;
285                 case 'f':
286                         num_files = strtol(optarg, NULL, 0);
287                         break;
288                 case 's':
289                         file_size = strtol(optarg, NULL, 0);
290                         break;
291                 case 'p':
292                         base_dir = optarg;
293                         break;
294                 case 'm':
295                         use_mmap = 1;
296                         break;
297                 case 'P':
298                         do_prealloc = 1;
299                         break;
300                 case 'S':
301                         use_sync = 1;
302                         break;
303                 case 'l':
304                         loop_count = strtol(optarg, NULL, 0);
305                         break;
306                 case 'h':
307                         usage();
308                         exit(0);
309                 default:
310                         usage();
311                         exit(1);
312                 }
313         }
314
315         argc -= optind;
316         argv += optind;
317
318         /* round up the file size */
319         if (file_size % block_size != 0) {
320                 file_size = (file_size + (block_size-1)) / block_size;
321                 file_size *= block_size;
322                 printf("Rounded file size to %d\n", file_size);
323         }
324
325         printf("num_children=%d file_size=%d num_files=%d loop_count=%d block_size=%d\nmmap=%d sync=%d prealloc=%d\n",
326                num_children, file_size, num_files, loop_count, block_size, use_mmap, use_sync, do_prealloc);
327
328         printf("Total data size %.1f Mbyte\n",
329                num_files * num_children * 1.0e-6 * file_size);
330
331         /* fork and run run_child() for each child */
332         for (i=0;i<num_children;i++) {
333                 if (fork() == 0) {
334                         run_child(i);
335                         exit(0);
336                 }
337         }
338
339         ret = 0;
340
341         /* wait for children to exit */
342         while (waitpid(0, &status, 0) == 0 || errno != ECHILD) {
343                 if (WEXITSTATUS(status) != 0) {
344                         ret = WEXITSTATUS(status);
345                         printf("Child exited with status %d\n", ret);
346                 }
347         }
348
349         if (ret != 0) {
350                 printf("fstest failed with status %d\n", ret);
351         }
352
353         return ret;
354 }