2 * ext3 Filesystem Verification Tool.
3 * This program tests the correct operation of ext3 filesystem.
4 * This tool have two working modes
7 * The full mode is basic mode in which program creates a subdirectory
8 * in the test fileysytem, writes n(files_in_dir, default=16) large(4GB) files
9 * to the directory with the test pattern at the start of each 4kb block.
10 * The test pattern contains timestamp, relative file offset and per file
11 * unique idenfifier(inode number). this continues until whole filesystem is
12 * full and then this tooll verifies that the data in all of the test files
14 * In the fast mode the tool creates a test directories with
15 * EXT3_TOPDIR_FL flag set. the number of directories equals to the number
16 * of block groups in the filesystem(e.g. 65536 directories for 8TB filesystem)
17 * and then writes a single 1MB file in each directory. The tool then verifies
18 * that the data in each file is correct.
27 #ifndef _LARGEFILE64_SOURCE
28 #define _LARGEFILE64_SOURCE
30 #ifndef _FILE_OFFSET_BITS
31 #define _FILE_OFFSET_BITS 64
48 #include <sys/types.h>
51 #include <gnu/stubs.h>
52 #include <gnu/stubs.h>
54 #ifdef HAVE_EXT2FS_EXT2FS_H
56 # include <ext2fs/ext2fs.h>
59 #define ONE_MB (1024 * 1024)
60 #define ONE_GB ((unsigned long long)(1024 * 1024 * 1024))
61 #define BLOCKSIZE 4096
63 /* Structure for writing test pattern */
65 unsigned long long bd_offset;
66 unsigned long long bd_time;
67 unsigned long long bd_inode;
69 static char *progname; /* name by which this program was run. */
70 static unsigned verbose = 1; /* prints offset in kB, operation rate */
71 static int readoption; /* run test in read-only (verify) mode */
72 static int writeoption; /* run test in write_only mode */
73 char *testdir; /* name of device to be tested. */
74 static unsigned full = 1; /* flag to full check */
75 static int errno_local; /* local copy of errno */
76 static unsigned long num_files; /* Total number of files for read/write */
77 static loff_t file_size = 4*ONE_GB; /* Size of each file */
78 static unsigned files_in_dir = 32; /* number of files in each directioy */
79 static unsigned num_dirs = 30000; /* total number of directories */
80 const int dirmode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
82 static int isatty_flag;
83 static int perms = S_IRWXU | S_IRGRP | S_IROTH;
85 static struct option const longopts[] =
87 { "chunksize", required_argument, 0, 'c' },
88 { "help", no_argument, 0, 'h' },
89 { "offset", required_argument, 0, 'o' },
90 { "long", no_argument, 0, 'l' },
91 { "partial", required_argument, 0, 'p' },
92 { "quiet", required_argument, 0, 'q' },
93 { "read", no_argument, 0, 'r' },
94 { "timestamp", required_argument, 0, 't' },
95 { "verbose", no_argument, 0, 'v' },
96 { "write", no_argument, 0, 'w' },
101 * Usages: displays help information, whenever user supply --help option in
102 * command or enters incorrect command line.
104 void usage(int status)
108 printf("\nUsage: %s [OPTION]... <filesystem path> ...\n",
110 printf("ext3 filesystem verification tool.\n"
111 "\t-t {seconds} for --timestamp, set test time"
112 "(default=current time())\n"
113 "\t-o {offset} for --offset, directory starting offset"
114 " from which tests should start\n"
115 "\t-r run test in read (verify) mode\n"
116 "\t-w run test in write (test-pattern) mode (default=r&w)\n"
118 "\t-p for --partial, for partial check (1MB files)\n"
119 "\t-l for --long, full check (4GB file with 4k blocks)\n"
120 "\t-c for --chunksize, IO chunk size (default=1048576)\n"
121 "\t-h display this help and exit\n"
122 "\t--help display this help and exit\n");
128 * open_file: Opens file in specified mode and returns fd.
130 static int open_file(const char *file, int flag)
132 fd = open(file, flag, perms);
134 fprintf(stderr, "\n%s: Open '%s' failed:%s\n",
135 progname, file, strerror(errno));
142 * Verify_chunk: Verifies test pattern in each 4kB (BLOCKSIZE) is correct.
143 * Returns 0 if test offset and timestamp is correct otherwise 1.
145 int verify_chunk(char *chunk_buf, size_t chunksize,unsigned long long chunk_off,
146 unsigned long long time_st, unsigned long long inode_st,
149 struct block_data *bd;
152 for (chunk_end = chunk_buf + chunksize - sizeof(*bd);
153 (char *)chunk_buf < chunk_end;
154 chunk_buf += BLOCKSIZE, chunk_off += BLOCKSIZE) {
155 bd = (struct block_data *)chunk_buf;
156 if ((bd->bd_offset == chunk_off) && (bd->bd_time == time_st) &&
157 (bd->bd_inode == inode_st))
159 fprintf(stderr,"\n%s: verify %s failed offset/timestamp/inode "
160 "%llu/%llu/%llu: found %llu/%llu/%llu instead\n",
161 progname, file, chunk_off, time_st, inode_st,
162 bd->bd_offset, bd->bd_time, bd->bd_inode);
169 * fill_chunk: Fills the chunk with current or user specified timestamp
170 * and offset. The test patters is filled at the beginning of
171 * each 4kB(BLOCKSIZE) blocks in chunk_buf.
173 void fill_chunk(char *chunk_buf, size_t chunksize, loff_t chunk_off,
174 time_t time_st, ino_t inode_st)
176 struct block_data *bd;
179 for (chunk_end = chunk_buf + chunksize - sizeof(*bd);
180 (char *)chunk_buf < chunk_end;
181 chunk_buf += BLOCKSIZE, chunk_off += BLOCKSIZE) {
182 bd = (struct block_data *)chunk_buf;
183 bd->bd_offset = chunk_off;
184 bd->bd_time = time_st;
185 bd->bd_inode = inode_st;
190 * write_chunk: write the chunk_buf on the device. The number of write
191 * operations are based on the parameters write_end, offset, and chunksize.
193 int write_chunks(int fd, unsigned long long offset,unsigned long long write_end,
194 char *chunk_buf, size_t chunksize, time_t time_st,
195 ino_t inode_st, const char *file)
197 unsigned long long stride;
199 stride = full ? chunksize : (ONE_GB - chunksize);
200 for (offset = offset & ~(chunksize - 1); offset < write_end;
202 if (lseek64(fd, offset, SEEK_SET) == -1) {
203 fprintf(stderr, "\n%s: lseek64(%s+%llu) failed: %s\n",
204 progname, file, offset, strerror(errno));
207 if (offset + chunksize > write_end)
208 chunksize = write_end - offset;
209 if (!full && offset > chunksize) {
210 fill_chunk(chunk_buf, chunksize, offset, time_st,
212 if (write(fd, chunk_buf, chunksize) < 0) {
213 if (errno == ENOSPC) {
218 "\n%s: write %s+%llu failed: %s\n",
219 progname, file, offset,strerror(errno));
223 if (offset + chunksize > write_end)
224 chunksize = write_end - offset;
226 fill_chunk(chunk_buf, chunksize, offset, time_st, inode_st);
227 if (write(fd, (char *) chunk_buf, chunksize) < 0) {
228 if (errno == ENOSPC) {
232 fprintf(stderr, "\n%s: write %s+%llu failed: %s\n",
233 progname, file, offset, strerror(errno));
241 * read_chunk: reads the chunk_buf from the device. The number of read
242 * operations are based on the parameters read_end, offset, and chunksize.
244 int read_chunks(int fd, unsigned long long offset, unsigned long long read_end,
245 char *chunk_buf, size_t chunksize, time_t time_st,
246 ino_t inode_st, char *file)
248 unsigned long long stride;
250 stride = full ? chunksize : (ONE_GB - chunksize);
251 for (offset = offset & ~(chunksize - 1); offset < read_end;
253 if (lseek64(fd, offset, SEEK_SET) == -1) {
254 fprintf(stderr, "\n%s: lseek64(%s+%llu) failed: %s\n",
255 progname, file, offset, strerror(errno));
258 if (offset + chunksize > read_end)
259 chunksize = read_end - offset;
260 if (!full && offset > chunksize) {
261 if (read(fd, chunk_buf, chunksize) < 0) {
263 "\n%s: read %s+%llu failed: %s\n",
264 progname, file, offset,strerror(errno));
267 if (verify_chunk(chunk_buf, chunksize, offset,
268 time_st, inode_st, file) != 0)
271 if (offset + chunksize >= read_end)
272 chunksize = read_end - offset;
274 if (read(fd, chunk_buf, chunksize) < 0) {
275 fprintf(stderr, "\n%s: read %s+%llu failed: %s\n",
276 progname, file, offset, strerror(errno));
279 if (verify_chunk(chunk_buf, chunksize, offset, time_st,
280 inode_st, file) != 0)
287 * new_file: prepares new filename using file counter and current dir.
289 char *new_file(char *tempfile, char *cur_dir, int file_num)
291 sprintf(tempfile, "%s/file%03d", cur_dir, file_num);
296 * new_dir: prepares new dir name using dir counters.
298 char *new_dir(char *tempdir, int dir_num)
300 sprintf(tempdir, "%s/dir%05d", testdir, dir_num);
305 * show_filename: Displays name of current file read/write
307 void show_filename(char *op, char *filename)
315 if (diff > 4 || verbose > 2) {
318 printf("%s File name: %s ", op, filename);
328 * dir_write: This function writes directories and files on device.
329 * it works for both full and fast modes.
331 static int dir_write(char *chunk_buf, size_t chunksize,
332 time_t time_st, unsigned long dir_num)
334 char tempfile[PATH_MAX];
335 char tempdir[PATH_MAX];
337 int file_num = 999999999;
340 #ifdef HAVE_EXT2FS_EXT2FS_H
341 if (!full && fsetflags(testdir, EXT2_TOPDIR_FL))
343 "\n%s: can't set TOPDIR_FL on %s: %s (ignoring)",
344 progname, testdir, strerror(errno));
346 for (; dir_num < num_dirs; num_files++, file_num++) {
347 if (file_num >= files_in_dir) {
348 if (dir_num == num_dirs - 1)
352 if (mkdir(new_dir(tempdir, dir_num), dirmode) < 0) {
355 if (errno != EEXIST) {
356 fprintf(stderr, "\n%s: mkdir %s : %s\n",
364 fd = open_file(new_file(tempfile, tempdir, file_num),
365 O_WRONLY | O_CREAT | O_TRUNC | O_LARGEFILE);
367 if (fd >= 0 && fstat64(fd, &file) == 0) {
368 inode_st = file.st_ino;
370 fprintf(stderr, "\n%s: write stat64 to file %s: %s",
371 progname, tempfile, strerror(errno));
376 show_filename("write", tempfile);
378 if (write_chunks(fd, 0, file_size, chunk_buf, chunksize,
379 time_st, inode_st, tempfile)) {
385 if (errno_local == ENOSPC)
391 show_filename("write", tempfile);
392 printf("\nwrite complete\n");
400 * dir_read: This function reads directories and files on device.
401 * it works for both full and fast modes.
403 static int dir_read(char *chunk_buf, size_t chunksize,
404 time_t time_st, unsigned long dir_num)
406 char tempfile[PATH_MAX];
407 char tempdir[PATH_MAX];
408 unsigned long count = 0;
413 for (count = 0; count < num_files && dir_num < num_dirs; count++) {
415 if (dir_num == num_dirs - 1)
418 new_dir(tempdir, dir_num);
422 fd = open_file(new_file(tempfile, tempdir, file_num),
423 O_RDONLY | O_LARGEFILE);
424 if (fd >= 0 && fstat64(fd, &file) == 0) {
425 inode_st = file.st_ino;
427 fprintf(stderr, "\n%s: read stat64 file '%s': %s\n",
428 progname, tempfile, strerror(errno));
433 show_filename("read", tempfile);
435 if (count == num_files)
436 file_size = file.st_size;
437 if (read_chunks(fd, 0, file_size, chunk_buf, chunksize,
438 time_st, inode_st, tempfile)) {
444 if (++file_num >= files_in_dir)
449 show_filename("read", tempfile);
450 printf("\nread complete\n");
456 int main(int argc, char **argv)
458 time_t time_st = 0; /* Default timestamp */
459 size_t chunksize = ONE_MB; /* IO chunk size(defailt=1MB) */
460 char *chunk_buf; /* chunk buffer */
462 FILE *countfile = NULL;
463 char filecount[PATH_MAX];
464 unsigned long dir_num = 0, dir_num_orig = 0;/* starting directory */
467 progname = strrchr(argv[0], '/') ? strrchr(argv[0], '/') + 1 : argv[0];
468 while ((c = (char)getopt_long(argc, argv, "t:rwvplo:h",
469 longopts, NULL)) != -1) {
472 chunksize = (strtoul(optarg, NULL, 0) * ONE_MB);
474 fprintf(stderr, "%s: Chunk size value should be"
475 "a multiple of 1MB\n", progname);
482 case 'o': /* offset */
483 dir_num = strtoul(optarg, NULL, 0);
495 time_st = (time_t)strtoul(optarg, NULL, 0);
510 testdir = argv[optind];
513 fprintf(stderr, "%s: pathname not given\n", progname);
517 if (!readoption && !writeoption) {
522 (void) time(&time_st);
523 printf("Timestamp: %lu\n", (unsigned long )time_st);
524 isatty_flag = isatty(STDOUT_FILENO);
527 #ifdef HAVE_EXT2FS_EXT2FS_H
528 struct mntent *tempmnt;
532 if ((fp = setmntent("/etc/mtab", "r")) == NULL){
533 fprintf(stderr, "%s: fail to open /etc/mtab in read"
534 "mode :%s\n", progname, strerror(errno));
538 /* find device name using filesystem */
539 while ((tempmnt = getmntent(fp)) != NULL) {
540 if (strcmp(tempmnt->mnt_dir, testdir) == 0)
544 if (tempmnt == NULL) {
545 fprintf(stderr, "%s: no device found for '%s'\n",
551 if (ext2fs_open(tempmnt->mnt_fsname, 0, 0, 0,
552 unix_io_manager, &fs)) {
553 fprintf(stderr, "%s: unable to open ext3 fs on '%s'\n",
560 num_dirs = (fs->super->s_blocks_count +
561 fs->super->s_blocks_per_group - 1) /
562 fs->super->s_blocks_per_group;
564 printf("ext3 block groups: %u, fs blocks: %u "
565 "blocks per group: %u\n",
566 num_dirs, fs->super->s_blocks_count,
567 fs->super->s_blocks_per_group);
573 struct statfs64 statbuf;
575 if (statfs64(testdir, &statbuf) == 0) {
576 num_dirs = (long long)statbuf.f_blocks *
577 statbuf.f_bsize / (128ULL << 20);
579 printf("dirs: %u, fs blocks: %llu\n",
581 (long long)statbuf.f_blocks);
583 fprintf(stderr, "%s: unable to stat '%s': %s\n",
584 progname, testdir, strerror(errno));
586 printf("dirs: %u\n", num_dirs);
594 chunk_buf = (char *)calloc(chunksize, 1);
595 if (chunk_buf == NULL) {
596 fprintf(stderr, "Memory allocation failed for chunk_buf\n");
599 sprintf(filecount, "%s/%s.filecount", testdir, progname);
601 (void)mkdir(testdir, dirmode);
605 num_files = dir_num * files_in_dir;
607 printf("\n%s: %lu files already written\n",
608 progname, num_files);
610 if (dir_write(chunk_buf, chunksize, time_st, dir_num)) {
614 countfile = fopen(filecount, "w");
615 if (countfile != NULL) {
616 if (fprintf(countfile, "%lu", num_files) < 1 ||
617 fflush(countfile) != 0) {
618 fprintf(stderr, "\n%s: writing %s failed :%s\n",
619 progname, filecount, strerror(errno));
623 dir_num = dir_num_orig;
627 countfile = fopen(filecount, "r");
628 if (countfile == NULL ||
629 fscanf(countfile, "%lu", &num_files) != 1) {
630 fprintf(stderr, "\n%s: reading %s failed :%s\n",
631 progname, filecount, strerror(errno));
632 num_files = num_dirs * files_in_dir;
634 num_files -= (dir_num * files_in_dir);
639 if (dir_read(chunk_buf, chunksize, time_st, dir_num)) {
640 fprintf(stderr, "\n%s: Data verification failed\n",