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 sprintf(fname, "%s/file%d", dir, fnum);
96 fd = open(fname, O_RDWR|O_CREAT|O_TRUNC | (use_sync?O_SYNC:0), 0644);
107 resv.l_len = file_size;
109 #ifdef XFS_IOC_RESVSP64
110 if ((xfsctl(fname, fd, XFS_IOC_RESVSP64, &resv)) < 0) {
116 if ((fcntl(fd, F_RESVSP64, &resv)) < 0) {
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);
136 if (ftruncate(fd, file_size) != 0) {
140 p = mmap(NULL, file_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
141 if (p == (char *)-1) {
145 for (size=0; size<file_size; size += block_size * do_frags) {
146 gen_buffer(p+size, loop, child, fnum, size);
148 munmap(p, file_size);
156 check that a file has the right data
158 static void check_file(const char *dir, int loop, int child, int fnum)
164 buf = x_malloc(block_size);
166 sprintf(fname, "%s/file%d", dir, fnum);
167 fd = open(fname, O_RDONLY);
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);
178 check_buffer(buf, loop, child, fnum, size);
186 recursive directory traversal - used for cleanup
187 fn() is called on all files/dirs in the tree
189 void traverse(const char *dir, int (*fn)(const char *))
197 while ((de = readdir(d))) {
201 if (strcmp(de->d_name,".") == 0) continue;
202 if (strcmp(de->d_name,"..") == 0) continue;
204 sprintf(fname, "%s/%s", dir, de->d_name);
205 if (lstat(fname, &st)) {
210 if (S_ISDIR(st.st_mode)) {
220 /* the main child function - this creates/checks the file for one child */
221 static void run_child(int child)
226 sprintf(dir, "%s/child%d", base_dir, child);
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);
235 if (mkdir(dir, 0755) != 0) {
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);
245 for (i=0;i<num_files;i++) {
246 check_file(dir, loop, child, i);
250 /* cleanup afterwards */
251 printf("Child %d cleaning up %s\n", child, dir);
252 traverse(dir, remove);
258 static void usage(void)
261 "Usage: fstest [options]\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"
271 " -S use synchronous IO\n"
272 " -P preallocate space\n"
273 " -h show this help message\n");
277 int main(int argc, char *argv[])
282 int num_children = 1;
285 while ((c = getopt(argc, argv, "FPn:s:f:p:l:b:Shm")) != -1) {
291 num_children = strtol(optarg, NULL, 0);
294 block_size = strtol(optarg, NULL, 0);
297 num_files = strtol(optarg, NULL, 0);
300 file_size = strtol(optarg, NULL, 0);
315 loop_count = strtol(optarg, NULL, 0);
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);
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);
339 printf("Total data size %.1f Mbyte\n",
340 num_files * num_children * 1.0e-6 * file_size);
342 /* fork and run run_child() for each child */
343 for (i=0;i<num_children;i++) {
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);
361 printf("fstest failed with status %d\n", ret);