1 /* filesystem verification tool, designed to detect data corruption on a filesystem
3 tridge@samba.org, March 2002
12 #include <sys/types.h>
20 /* variables settable on the command line */
21 static int loop_count = 100;
22 static int num_files = 1;
23 static int file_size = 1024*1024;
24 static int block_size = 1024;
25 static char *base_dir = ".";
29 typedef unsigned char uchar;
32 #define MIN(a,b) ((a)<(b)?(a):(b))
35 static void fatal(char * msg)
41 static ssize_t a_pread(int fd, void *buf, size_t count, off_t offset)
45 struct iocb *iocbp = &iocb;
48 memset(&io_ctx, 0, sizeof(io_ctx));
50 if (io_setup(1, &io_ctx) != 0) {
51 fatal("io_setup failed");
54 io_prep_pread(&iocb, fd, buf, count, offset);
56 if (io_submit(io_ctx, 1, &iocbp) != 1) {
57 fatal("io_submit failed");
60 if (io_getevents(io_ctx, 1, 1, &ev, NULL) != 1) {
61 fatal("io_getevents failed");
66 return iocb.u.c.nbytes;
69 static void *x_malloc(int size)
71 void *ret = malloc(size);
73 fprintf(stderr,"Out of memory for size %d!\n", size);
80 /* generate a buffer for a particular child, fnum etc. Just use a simple buffer
81 to make debugging easy
83 static void gen_buffer(uchar *buf, int loop, int child, int fnum, int ofs)
85 uchar v = (loop+child+fnum+(ofs/block_size)) % 256;
86 memset(buf, v, block_size);
90 check if a buffer from disk is correct
92 static void check_buffer(uchar *buf, int loop, int child, int fnum, int ofs)
96 buf2 = x_malloc(block_size);
98 gen_buffer(buf2, loop, child, fnum, ofs);
100 if (memcmp(buf, buf2, block_size) != 0) {
102 for (i=0;buf[i] == buf2[i] && i<block_size;i++) ;
103 fprintf(stderr,"Corruption in child %d fnum %d at offset %d\n",
107 for (j=0;j<MIN(20, block_size-i);j++) {
108 printf("%02x ", buf2[j+i]);
112 printf("Incorrect: ");
113 for (j=0;j<MIN(20, block_size-i);j++) {
114 printf("%02x ", buf[j+i]);
124 create a file with a known data set for a child
126 static void create_file(const char *dir, int loop, int child, int fnum)
132 buf = x_malloc(block_size);
133 sprintf(fname, "%s/file%d", dir, fnum);
134 fd = open(fname, O_RDWR|O_CREAT|O_TRUNC | (use_sync?O_SYNC:0), 0644);
141 for (size=0; size<file_size; size += block_size) {
142 gen_buffer(buf, loop, child, fnum, size);
143 if (pwrite(fd, buf, block_size, size) != block_size) {
144 fprintf(stderr,"Write failed at offset %d\n", size);
150 if (ftruncate(fd, file_size) != 0) {
154 p = mmap(NULL, file_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
155 if (p == (char *)-1) {
159 for (size=0; size<file_size; size += block_size) {
160 gen_buffer(p+size, loop, child, fnum, size);
162 munmap(p, file_size);
170 check that a file has the right data
172 static void check_file(const char *dir, int loop, int child, int fnum)
178 buf = x_malloc(block_size);
180 sprintf(fname, "%s/file%d", dir, fnum);
181 fd = open(fname, O_RDONLY);
187 for (size=0; size<file_size; size += block_size) {
188 if (a_pread(fd, buf, block_size, size) != block_size) {
189 fprintf(stderr,"read failed at offset %d\n", size);
192 check_buffer(buf, loop, child, fnum, size);
200 revsusive directory traversal - used for cleanup
201 fn() is called on all files/dirs in the tree
203 void traverse(const char *dir, int (*fn)(const char *))
211 while ((de = readdir(d))) {
215 if (strcmp(de->d_name,".") == 0) continue;
216 if (strcmp(de->d_name,"..") == 0) continue;
218 sprintf(fname, "%s/%s", dir, de->d_name);
219 if (lstat(fname, &st)) {
224 if (S_ISDIR(st.st_mode)) {
234 /* the main child function - this creates/checks the file for one child */
235 static void run_child(int child)
240 sprintf(dir, "%s/child%d", base_dir, child);
242 /* cleanup any old files */
243 if (remove(dir) != 0 && errno != ENOENT) {
244 printf("Child %d cleaning %s\n", child, dir);
245 traverse(dir, remove);
249 if (mkdir(dir, 0755) != 0) {
254 for (loop = 0; loop < loop_count; loop++) {
255 printf("Child %d loop %d\n", child, loop);
256 for (i=0;i<num_files;i++) {
257 create_file(dir, loop, child, i);
259 for (i=0;i<num_files;i++) {
260 check_file(dir, loop, child, i);
264 /* cleanup afterwards */
265 printf("Child %d cleaning up %s\n", child, dir);
266 traverse(dir, remove);
272 static void usage(void)
275 "Usage: fstest [options]\n"
277 " -n num_children set number of child processes\n"
278 " -f num_files set number of files\n"
279 " -s file_size set file sizes\n"
280 " -b block_size set block (IO) size\n"
281 " -p path set base path\n"
282 " -l loops set loop count\n"
284 " -S use synchronous IO\n"
285 " -h show this help message\n");
289 int main(int argc, char *argv[])
294 int num_children = 1;
297 while ((c = getopt(argc, argv, "n: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);
321 loop_count = strtol(optarg, NULL, 0);
335 /* round up the file size */
336 if (file_size % block_size != 0) {
337 file_size = (file_size + (block_size-1)) / block_size;
338 file_size *= block_size;
339 printf("Rounded file size to %d\n", file_size);
342 printf("num_children=%d file_size=%d num_files=%d loop_count=%d block_size=%d\nmmap=%d sync=%d\n",
343 num_children, file_size, num_files, loop_count, block_size, use_mmap, use_sync);
345 printf("Total data size %.1f Mbyte\n",
346 num_files * num_children * 1.0e-6 * file_size);
348 /* fork and run run_child() for each child */
349 for (i=0;i<num_children;i++) {
358 /* wait for children to exit */
359 while (waitpid(0, &status, 0) == 0 || errno != ECHILD) {
360 if (WEXITSTATUS(status) != 0) {
361 ret = WEXITSTATUS(status);
362 printf("Child exited with status %d\n", ret);
367 printf("fstest failed with status %d\n", ret);