common/rc: Add extra check for xfs_io -c "chattr" on XFS
[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
8 #include "global.h"
9
10 #include <sys/mman.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(char *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         char *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         char *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                 struct flock64 resv;
104
105                 resv.l_whence = 0;
106                 resv.l_start = 0;
107                 resv.l_len = file_size;
108
109 #ifdef XFS_IOC_RESVSP64
110                 if ((xfsctl(fname, fd, XFS_IOC_RESVSP64, &resv)) < 0) {
111                         perror(fname);
112                         exit(1);
113                 }
114 #else
115 #ifdef F_RESVSP64
116                 if ((fcntl(fd, F_RESVSP64, &resv)) < 0) {
117                         perror(fname);
118                         exit(1);
119                 }
120 #else
121 bozo!
122 #endif
123 #endif
124         }
125                 
126         if (!use_mmap) {
127                 for (size=0; size<file_size; size += block_size * do_frags) {
128                         gen_buffer(buf, loop, child, fnum, size);
129                         if (pwrite(fd, buf, block_size, size) != block_size) {
130                                 fprintf(stderr,"Write failed at offset %d\n", size);
131                                 exit(1);
132                         }
133                 }
134         } else {
135                 char *p;
136                 if (ftruncate(fd, file_size) != 0) {
137                         perror("ftruncate");
138                         exit(1);
139                 }
140                 p = mmap(NULL, file_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
141                 if (p == MAP_FAILED) {
142                         perror("mmap");
143                         exit(1);
144                 }
145                 for (size=0; size<file_size; size += block_size * do_frags) {
146                         gen_buffer(p+size, loop, child, fnum, size);
147                 }
148                 munmap(p, file_size);
149         }
150
151         free(buf);
152         close(fd);
153 }
154
155 /* 
156    check that a file has the right data
157  */
158 static void check_file(const char *dir, int loop, int child, int fnum)
159 {
160         uchar *buf;
161         int size, fd;
162         char fname[1024];
163
164         buf = x_malloc(block_size);
165
166         sprintf(fname, "%s/file%d", dir, fnum);
167         fd = open(fname, O_RDONLY);
168         if (fd == -1) {
169                 perror(fname);
170                 exit(1);
171         }
172
173         for (size=0; size<file_size; size += block_size * do_frags) {
174                 if (pread(fd, buf, block_size, size) != block_size) {
175                         fprintf(stderr,"read failed at offset %d\n", size);
176                         exit(1);
177                 }
178                 check_buffer(buf, loop, child, fnum, size);
179         }
180
181         free(buf);
182         close(fd);
183 }
184
185 /* 
186    recursive directory traversal - used for cleanup
187    fn() is called on all files/dirs in the tree
188  */
189 void traverse(const char *dir, int (*fn)(const char *))
190 {
191         DIR *d;
192         struct dirent *de;
193
194         d = opendir(dir);
195         if (!d) return;
196
197         while ((de = readdir(d))) {
198                 char fname[1024];
199                 struct stat st;
200
201                 if (strcmp(de->d_name,".") == 0) continue;
202                 if (strcmp(de->d_name,"..") == 0) continue;
203
204                 sprintf(fname, "%s/%s", dir, de->d_name);
205                 if (lstat(fname, &st)) {
206                         perror(fname);
207                         continue;
208                 }
209
210                 if (S_ISDIR(st.st_mode)) {
211                         traverse(fname, fn);
212                 }
213
214                 fn(fname);
215         }
216
217         closedir(d);
218 }
219
220 /* the main child function - this creates/checks the file for one child */
221 static void run_child(int child)
222 {
223         int i, loop;
224         char dir[1024];
225
226         sprintf(dir, "%s/child%d", base_dir, child);
227
228         /* cleanup any old files */
229         if (remove(dir) != 0 && errno != ENOENT) {
230                 printf("Child %d cleaning %s\n", child, dir);
231                 traverse(dir, remove);
232                 remove(dir);
233         }
234
235         if (mkdir(dir, 0755) != 0) {
236                 perror(dir);
237                 exit(1);
238         }
239
240         for (loop = 0; loop < loop_count; loop++) {
241                 printf("Child %d loop %d\n", child, loop);
242                 for (i=0;i<num_files;i++) {
243                         create_file(dir, loop, child, i);
244                 }
245                 for (i=0;i<num_files;i++) {
246                         check_file(dir, loop, child, i);
247                 }
248         }
249
250         /* cleanup afterwards */
251         printf("Child %d cleaning up %s\n", child, dir);
252         traverse(dir, remove);
253         remove(dir);
254
255         exit(0);
256 }
257
258 static void usage(void)
259 {
260         printf("\n"
261 "Usage: fstest [options]\n"
262 "\n"
263 " -F                    generate files with holes\n"
264 " -n num_children       set number of child processes\n"
265 " -f num_files          set number of files\n"
266 " -s file_size          set file sizes\n"
267 " -b block_size         set block (IO) size\n"
268 " -p path               set base path\n"
269 " -l loops              set loop count\n"
270 " -m                    use mmap\n"
271 " -S                    use synchronous IO\n"
272 " -P                    preallocate space\n"
273 " -h                    show this help message\n");
274 }
275
276 /* main program */
277 int main(int argc, char *argv[])
278 {
279         int c;
280         extern char *optarg;
281         extern int optind;
282         int num_children = 1;
283         int i, status, ret;
284
285         while ((c = getopt(argc, argv, "FPn:s:f:p:l:b:Shm")) != -1) {
286                 switch (c) {
287                 case 'F':
288                         do_frags = 2;
289                         break;
290                 case 'n':
291                         num_children = strtol(optarg, NULL, 0);
292                         break;
293                 case 'b':
294                         block_size = strtol(optarg, NULL, 0);
295                         break;
296                 case 'f':
297                         num_files = strtol(optarg, NULL, 0);
298                         break;
299                 case 's':
300                         file_size = strtol(optarg, NULL, 0);
301                         break;
302                 case 'p':
303                         base_dir = optarg;
304                         break;
305                 case 'm':
306                         use_mmap = 1;
307                         break;
308                 case 'P':
309                         do_prealloc = 1;
310                         break;
311                 case 'S':
312                         use_sync = 1;
313                         break;
314                 case 'l':
315                         loop_count = strtol(optarg, NULL, 0);
316                         break;
317                 case 'h':
318                         usage();
319                         exit(0);
320                 default:
321                         usage();
322                         exit(1);
323                 }
324         }
325
326         argc -= optind;
327         argv += optind;
328
329         /* round up the file size */
330         if (file_size % block_size != 0) {
331                 file_size = (file_size + (block_size-1)) / block_size;
332                 file_size *= block_size;
333                 printf("Rounded file size to %d\n", file_size);
334         }
335
336         printf("num_children=%d file_size=%d num_files=%d loop_count=%d block_size=%d\nmmap=%d sync=%d prealloc=%d\n",
337                num_children, file_size, num_files, loop_count, block_size, use_mmap, use_sync, do_prealloc);
338
339         printf("Total data size %.1f Mbyte\n",
340                num_files * num_children * 1.0e-6 * file_size);
341
342         /* fork and run run_child() for each child */
343         for (i=0;i<num_children;i++) {
344                 if (fork() == 0) {
345                         run_child(i);
346                         exit(0);
347                 }
348         }
349
350         ret = 0;
351
352         /* wait for children to exit */
353         while (waitpid(0, &status, 0) == 0 || errno != ECHILD) {
354                 if (WEXITSTATUS(status) != 0) {
355                         ret = WEXITSTATUS(status);
356                         printf("Child exited with status %d\n", ret);
357                 }
358         }
359
360         if (ret != 0) {
361                 printf("fstest failed with status %d\n", ret);
362         }
363
364         return ret;
365 }