1 /* filesystem verification tool, designed to detect data corruption on a filesystem
3 tridge@samba.org, March 2002
12 #include <sys/types.h>
19 /* variables settable on the command line */
20 static int loop_count = 100;
21 static int num_files = 1;
22 static int file_size = 1024*1024;
23 static int block_size = 1024;
24 static char *base_dir = ".";
28 typedef unsigned char uchar;
31 #define MIN(a,b) ((a)<(b)?(a):(b))
34 static void *x_malloc(int size)
36 void *ret = malloc(size);
38 fprintf(stderr,"Out of memory for size %d!\n", size);
45 /* generate a buffer for a particular child, fnum etc. Just use a simple buffer
46 to make debugging easy
48 static void gen_buffer(uchar *buf, int loop, int child, int fnum, int ofs)
50 uchar v = (loop+child+fnum+(ofs/block_size)) % 256;
51 memset(buf, v, block_size);
55 check if a buffer from disk is correct
57 static void check_buffer(uchar *buf, int loop, int child, int fnum, int ofs)
61 buf2 = x_malloc(block_size);
63 gen_buffer(buf2, loop, child, fnum, ofs);
65 if (memcmp(buf, buf2, block_size) != 0) {
67 for (i=0;buf[i] == buf2[i] && i<block_size;i++) ;
68 fprintf(stderr,"Corruption in child %d fnum %d at offset %d\n",
72 for (j=0;j<MIN(20, block_size-i);j++) {
73 printf("%02x ", buf2[j+i]);
77 printf("Incorrect: ");
78 for (j=0;j<MIN(20, block_size-i);j++) {
79 printf("%02x ", buf[j+i]);
89 create a file with a known data set for a child
91 static void create_file(const char *dir, int loop, int child, int fnum)
97 buf = x_malloc(block_size);
98 sprintf(fname, "%s/file%d", dir, fnum);
99 fd = open(fname, O_RDWR|O_CREAT|O_TRUNC | (use_sync?O_SYNC:0), 0644);
106 for (size=0; size<file_size; size += block_size) {
107 gen_buffer(buf, loop, child, fnum, size);
108 if (pwrite(fd, buf, block_size, size) != block_size) {
109 fprintf(stderr,"Write failed at offset %d\n", size);
115 if (ftruncate(fd, file_size) != 0) {
119 p = mmap(NULL, file_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
120 if (p == (char *)-1) {
124 for (size=0; size<file_size; size += block_size) {
125 gen_buffer(p+size, loop, child, fnum, size);
127 munmap(p, file_size);
135 check that a file has the right data
137 static void check_file(const char *dir, int loop, int child, int fnum)
143 buf = x_malloc(block_size);
145 sprintf(fname, "%s/file%d", dir, fnum);
146 fd = open(fname, O_RDONLY);
152 for (size=0; size<file_size; size += block_size) {
153 if (pread(fd, buf, block_size, size) != block_size) {
154 fprintf(stderr,"read failed at offset %d\n", size);
157 check_buffer(buf, loop, child, fnum, size);
165 revsusive directory traversal - used for cleanup
166 fn() is called on all files/dirs in the tree
168 void traverse(const char *dir, int (*fn)(const char *))
176 while ((de = readdir(d))) {
180 if (strcmp(de->d_name,".") == 0) continue;
181 if (strcmp(de->d_name,"..") == 0) continue;
183 sprintf(fname, "%s/%s", dir, de->d_name);
184 if (lstat(fname, &st)) {
189 if (S_ISDIR(st.st_mode)) {
199 /* the main child function - this creates/checks the file for one child */
200 static void run_child(int child)
205 sprintf(dir, "%s/child%d", base_dir, child);
207 /* cleanup any old files */
208 if (remove(dir) != 0 && errno != ENOENT) {
209 printf("Child %d cleaning %s\n", child, dir);
210 traverse(dir, remove);
214 if (mkdir(dir, 0755) != 0) {
219 for (loop = 0; loop < loop_count; loop++) {
220 printf("Child %d loop %d\n", child, loop);
221 for (i=0;i<num_files;i++) {
222 create_file(dir, loop, child, i);
224 for (i=0;i<num_files;i++) {
225 check_file(dir, loop, child, i);
229 /* cleanup afterwards */
230 printf("Child %d cleaning up %s\n", child, dir);
231 traverse(dir, remove);
237 static void usage(void)
240 "Usage: fstest [options]\n"
242 " -n num_children set number of child processes\n"
243 " -f num_files set number of files\n"
244 " -s file_size set file sizes\n"
245 " -b block_size set block (IO) size\n"
246 " -p path set base path\n"
247 " -l loops set loop count\n"
249 " -S use synchronous IO\n"
250 " -h show this help message\n");
254 int main(int argc, char *argv[])
259 int num_children = 1;
262 while ((c = getopt(argc, argv, "n:s:f:p:l:b:Shm")) != -1) {
265 num_children = strtol(optarg, NULL, 0);
268 block_size = strtol(optarg, NULL, 0);
271 num_files = strtol(optarg, NULL, 0);
274 file_size = strtol(optarg, NULL, 0);
286 loop_count = strtol(optarg, NULL, 0);
300 /* round up the file size */
301 if (file_size % block_size != 0) {
302 file_size = (file_size + (block_size-1)) / block_size;
303 file_size *= block_size;
304 printf("Rounded file size to %d\n", file_size);
307 printf("num_children=%d file_size=%d num_files=%d loop_count=%d block_size=%d\nmmap=%d sync=%d\n",
308 num_children, file_size, num_files, loop_count, block_size, use_mmap, use_sync);
310 printf("Total data size %.1f Mbyte\n",
311 num_files * num_children * 1.0e-6 * file_size);
313 /* fork and run run_child() for each child */
314 for (i=0;i<num_children;i++) {
323 /* wait for children to exit */
324 while (waitpid(0, &status, 0) == 0 || errno != ECHILD) {
325 if (WEXITSTATUS(status) != 0) {
326 ret = WEXITSTATUS(status);
327 printf("Child exited with status %d\n", ret);
332 printf("fstest failed with status %d\n", ret);