4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/utils/llverfs.c
34 * Filesystem Verification Tool.
35 * This program tests the correct operation of large filesystems and
36 * the underlying block storage device(s).
37 * This tool have two working modes
41 * In full mode, the program creates a subdirectory in the test
42 * filesystem, writes n(files_in_dir, default=32) large(4GB) files to
43 * the directory with the test pattern at the start of each 4kb block.
44 * The test pattern contains timestamp, relative file offset and per
45 * file unique identifier(inode number). This continues until the
46 * whole filesystem is full and then the tool verifies that the data
47 * in all of the test files is correct.
49 * In partial mode, the tool creates test directories with the
50 * EXT3_TOPDIR_FL flag set (if supported) to spread the directory data
51 * around the block device instead of localizing it in a single place.
52 * The number of directories equals to the number of block groups in the
53 * filesystem (e.g. 65536 directories for 8TB ext3/ext4 filesystem) and
54 * then writes a single 1MB file in each directory. The tool then verifies
55 * that the data in each file is correct.
64 #ifndef _LARGEFILE64_SOURCE
65 #define _LARGEFILE64_SOURCE
67 #ifndef _FILE_OFFSET_BITS
68 #define _FILE_OFFSET_BITS 64
85 #include <sys/types.h>
89 #include <gnu/stubs.h>
90 #include <gnu/stubs.h>
92 #ifdef HAVE_EXT2FS_EXT2FS_H
94 # include <ext2fs/ext2fs.h>
97 #define ONE_MB (1024 * 1024)
98 #define ONE_GB ((unsigned long long)(1024 * 1024 * 1024))
99 #define BLOCKSIZE 4096
101 /* Structure for writing test pattern */
103 unsigned long long bd_offset;
104 unsigned long long bd_time;
105 unsigned long long bd_inode;
107 static char *progname; /* name by which this program was run. */
108 static unsigned verbose = 1; /* prints offset in kB, operation rate */
109 static int readoption; /* run test in read-only (verify) mode */
110 static int writeoption; /* run test in write_only mode */
111 char *testdir; /* name of device to be tested. */
112 static unsigned full = 1; /* flag to full check */
113 static int error_count; /* number of IO errors hit during run */
114 char filecount[PATH_MAX]; /* file with total number of files written*/
115 static unsigned long num_files; /* Total number of files for read/write */
116 static loff_t file_size = 4*ONE_GB; /* Size of each file */
117 static unsigned files_in_dir = 32; /* number of files in each directioy */
118 static unsigned num_dirs = 30000; /* total number of directories */
119 const int dirmode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
120 static int isatty_flag;
121 static int perms = S_IRWXU | S_IRGRP | S_IROTH;
123 static struct option const long_opts[] = {
124 { .val = 'c', .name = "chunksize", .has_arg = required_argument },
125 { .val = 'h', .name = "help", .has_arg = no_argument },
126 { .val = 'l', .name = "long", .has_arg = no_argument },
127 { .val = 'l', .name = "full", .has_arg = no_argument },
128 { .val = 'o', .name = "offset", .has_arg = required_argument },
129 { .val = 'p', .name = "partial", .has_arg = required_argument },
130 { .val = 'q', .name = "quiet", .has_arg = required_argument },
131 { .val = 'r', .name = "read", .has_arg = no_argument },
132 { .val = 's', .name = "filesize", .has_arg = no_argument },
133 { .val = 't', .name = "timestamp", .has_arg = required_argument },
134 { .val = 'v', .name = "verbose", .has_arg = no_argument },
135 { .val = 'w', .name = "write", .has_arg = no_argument },
139 * Usages: displays help information, whenever user supply --help option in
140 * command or enters incorrect command line.
142 void usage(int status)
145 printf("\nUsage: %s [OPTION]... <filesystem path> ...\n",
147 printf("Filesystem verification tool.\n"
148 "\t-t {seconds}, --timestamp, set test time"
149 "(default=current time())\n"
150 "\t-o {offset}, --offset, directory starting offset"
151 " from which tests should start\n"
152 "\t-r, --read, run in verify mode\n"
153 "\t-w, --write, run in test-pattern mode, default=rw\n"
155 "\t-p, --partial, for partial check (1MB files)\n"
156 "\t-l, --long, --full check (4GB file with 4k blocks)\n"
157 "\t-c, --chunksize, IO chunk size in MB (default=1)\n"
158 "\t-s, --filesize, file size in MB (default=4096)\n"
159 "\t-h, --help, display this help and exit\n");
165 * open_file: Opens file in specified mode and returns fd.
167 static int open_file(const char *file, int flag)
169 int fd = open(file, flag, perms);
171 fprintf(stderr, "\n%s: Open '%s' failed:%s\n",
172 progname, file, strerror(errno));
178 * Verify_chunk: Verifies test pattern in each 4kB (BLOCKSIZE) is correct.
179 * Returns 0 if test offset and timestamp is correct otherwise 1.
181 int verify_chunk(char *chunk_buf, const size_t chunksize,
182 unsigned long long chunk_off, const unsigned long long time_st,
183 const unsigned long long inode_st, const char *file)
185 struct block_data *bd;
188 for (chunk_end = chunk_buf + chunksize - sizeof(*bd);
189 (char *)chunk_buf < chunk_end;
190 chunk_buf += BLOCKSIZE, chunk_off += BLOCKSIZE) {
191 bd = (struct block_data *)chunk_buf;
192 if ((bd->bd_offset == chunk_off) && (bd->bd_time == time_st) &&
193 (bd->bd_inode == inode_st))
195 fprintf(stderr, "\n%s: verify %s failed offset/timestamp/inode "
196 "%llu/%llu/%llu: found %llu/%llu/%llu instead\n",
197 progname, file, chunk_off, time_st, inode_st,
198 bd->bd_offset, bd->bd_time, bd->bd_inode);
205 * fill_chunk: Fills the chunk with current or user specified timestamp
206 * and offset. The test pattern is filled at the beginning of
207 * each 4kB(BLOCKSIZE) blocks in chunk_buf.
209 void fill_chunk(char *chunk_buf, size_t chunksize, loff_t chunk_off,
210 const time_t time_st, const ino_t inode_st)
212 struct block_data *bd;
215 for (chunk_end = chunk_buf + chunksize - sizeof(*bd);
216 (char *)chunk_buf < chunk_end;
217 chunk_buf += BLOCKSIZE, chunk_off += BLOCKSIZE) {
218 bd = (struct block_data *)chunk_buf;
219 bd->bd_offset = chunk_off;
220 bd->bd_time = time_st;
221 bd->bd_inode = inode_st;
226 * Write a chunk to disk, handling errors, interrupted writes, etc.
228 * If there is an IO error hit during the write, it is possible that
229 * this will just show up as a short write, and a subsequent write
230 * will return the actual error. We want to continue in the face of
231 * minor media errors so that we can validate the whole device if
232 * possible, but if there are many errors we don't want to loop forever.
234 * The error count will be returned upon exit to ensure that the
235 * media errors are detected even if nobody is looking at the output.
237 * Returns 0 on success, or -ve errno on failure.
239 int write_retry(int fd, const char *chunk_buf, size_t nrequested,
240 unsigned long long offset, const char *file)
245 nwritten = write(fd, chunk_buf, nrequested);
247 if (errno != ENOSPC) {
248 fprintf(stderr, "\n%s: write %s@%llu+%zi failed: %s\n",
249 progname, file, offset, nrequested,
251 if (error_count++ < 100)
256 if (nwritten < nrequested) {
257 fprintf(stderr, "\n%s: write %s@%llu+%zi short: %ld written\n",
258 progname, file, offset, nrequested, nwritten);
260 chunk_buf += nwritten;
261 nrequested -= nwritten;
269 * write_chunks: write the chunk_buf on the device. The number of write
270 * operations are based on the parameters write_end, offset, and chunksize.
272 * Returns 0 on success, or -ve error number on failure.
274 int write_chunks(int fd, unsigned long long offset,unsigned long long write_end,
275 char *chunk_buf, size_t chunksize, const time_t time_st,
276 const ino_t inode_st, const char *file)
278 unsigned long long stride;
280 stride = full ? chunksize : (ONE_GB - chunksize);
281 for (offset = offset & ~(chunksize - 1); offset < write_end;
285 if (lseek64(fd, offset, SEEK_SET) == -1) {
286 fprintf(stderr, "\n%s: lseek64(%s+%llu) failed: %s\n",
287 progname, file, offset, strerror(errno));
290 if (offset + chunksize > write_end)
291 chunksize = write_end - offset;
292 if (!full && offset > chunksize) {
293 fill_chunk(chunk_buf, chunksize, offset, time_st,
295 ret = write_retry(fd, chunk_buf, chunksize,offset,file);
299 if (offset + chunksize > write_end)
300 chunksize = write_end - offset;
302 fill_chunk(chunk_buf, chunksize, offset, time_st, inode_st);
303 ret = write_retry(fd, chunk_buf, chunksize, offset, file);
311 * read_chunk: reads the chunk_buf from the device. The number of read
312 * operations are based on the parameters read_end, offset, and chunksize.
314 int read_chunks(int fd, unsigned long long offset, unsigned long long read_end,
315 char *chunk_buf, size_t chunksize, const time_t time_st,
316 const ino_t inode_st, const char *file)
318 unsigned long long stride;
320 stride = full ? chunksize : (ONE_GB - chunksize);
321 for (offset = offset & ~(chunksize - 1); offset < read_end;
325 if (lseek64(fd, offset, SEEK_SET) == -1) {
326 fprintf(stderr, "\n%s: lseek64(%s+%llu) failed: %s\n",
327 progname, file, offset, strerror(errno));
330 if (offset + chunksize > read_end)
331 chunksize = read_end - offset;
333 if (!full && offset > chunksize) {
334 nread = read(fd, chunk_buf, chunksize);
336 fprintf(stderr,"\n%s: read %s@%llu+%zi failed: "
337 "%s\n", progname, file, offset,
338 chunksize, strerror(errno));
342 if (nread < chunksize) {
343 fprintf(stderr, "\n%s: read %s@%llu+%zi short: "
344 "%zi read\n", progname, file, offset,
348 if (verify_chunk(chunk_buf, nread, offset, time_st,
349 inode_st, file) != 0) {
354 /* Need to reset position after read error */
355 if (nread < chunksize &&
356 lseek64(fd, offset, SEEK_SET) == -1) {
358 "\n%s: lseek64(%s@%llu) failed: %s\n",
359 progname, file, offset,strerror(errno));
362 if (offset + chunksize >= read_end)
363 chunksize = read_end - offset;
365 nread = read(fd, chunk_buf, chunksize);
367 fprintf(stderr, "\n%s: read %s@%llu+%zi failed: %s\n",
368 progname, file, offset, chunksize,
373 if (nread < chunksize) {
374 fprintf(stderr, "\n%s: read %s@%llu+%zi short: "
375 "%zi read\n", progname, file, offset,
380 if (verify_chunk(chunk_buf, nread, offset, time_st,
381 inode_st, file) != 0) {
389 * new_file: prepares new filename using file counter and current dir.
391 char *new_file(char *tempfile, char *cur_dir, int file_num)
393 sprintf(tempfile, "%s/file%03d", cur_dir, file_num);
398 * new_dir: prepares new dir name using dir counters.
400 char *new_dir(char *tempdir, int dir_num)
402 sprintf(tempdir, "%s/dir%05d", testdir, dir_num);
407 * calc_total_bytes: calculates total bytes that need to be
408 * written into or read from the filesystem.
410 static unsigned long long calc_total_bytes(const char *op)
412 unsigned long long total_bytes = 0;
413 struct statfs64 statbuf;
416 if (statfs64(testdir, &statbuf) == 0) {
417 if (strcmp(op, "write") == 0)
418 total_bytes = (unsigned long long)
419 (statbuf.f_bavail * statbuf.f_bsize);
420 else if (strcmp(op, "read") == 0)
421 total_bytes = (unsigned long long)
422 (statbuf.f_blocks * statbuf.f_bsize);
424 fprintf(stderr, "\n%s: invalid operation: %s\n",
429 fprintf(stderr, "\n%s: unable to stat %s: %s\n",
430 progname, testdir, strerror(errno));
434 total_bytes = num_dirs * files_in_dir * file_size;
441 * show_rate: displays the current read/write file name and performance,
442 * along with an estimate of how long the whole read/write operation
445 void show_rate(char *op, char *filename, const struct timeval *start_time,
446 const unsigned long long total_bytes,
447 const unsigned long long curr_bytes)
449 static struct timeval last_time;
450 static unsigned long long last_bytes;
452 struct timeval curr_time;
453 double curr_delta, overall_delta, curr_rate, overall_rate;
454 double total_time, remain_time;
455 int remain_hours, remain_minutes, remain_seconds;
457 if (last_time.tv_sec == 0)
458 last_time = *start_time;
460 if (last_op != op[0]) {
462 last_time = *start_time;
466 gettimeofday(&curr_time, NULL);
468 curr_delta = (curr_time.tv_sec - last_time.tv_sec) +
469 (double)(curr_time.tv_usec - last_time.tv_usec) / 1000000;
471 overall_delta = (curr_time.tv_sec - start_time->tv_sec) +
472 (double)(curr_time.tv_usec - start_time->tv_usec) / 1000000;
474 curr_rate = (curr_bytes - last_bytes) / curr_delta;
475 overall_rate = curr_bytes / overall_delta;
477 if (curr_rate == 0) {
478 last_time = curr_time;
481 total_time = total_bytes / curr_rate;
482 remain_time = total_time - overall_delta;
484 remain_hours = remain_time / 3600;
485 remain_minutes = (remain_time - remain_hours * 3600) / 60;
486 remain_seconds = (remain_time - remain_hours * 3600 -
487 remain_minutes * 60);
489 if (curr_delta > 4 || verbose > 2) {
493 printf("%s filename: %s, current %5g MB/s, overall %5g MB/s, "
494 "est %u:%u:%u left", op, filename,
495 curr_rate / ONE_MB, overall_rate / ONE_MB,
496 remain_hours, remain_minutes, remain_seconds);
504 last_time = curr_time;
505 last_bytes = curr_bytes;
509 * dir_write: This function writes directories and files on device.
510 * it works for both full and partial modes.
512 static int dir_write(char *chunk_buf, size_t chunksize,
513 time_t time_st, unsigned long dir_num)
515 char tempfile[PATH_MAX];
516 char tempdir[PATH_MAX];
519 int file_num = 999999999;
521 struct timeval start_time;
522 unsigned long long total_bytes;
523 unsigned long long curr_bytes = 0;
526 #ifdef HAVE_EXT2FS_EXT2FS_H
527 if (!full && fsetflags(testdir, EXT2_TOPDIR_FL))
529 "\n%s: can't set TOPDIR_FL on %s: %s (ignoring)",
530 progname, testdir, strerror(errno));
532 countfile = fopen(filecount, "w");
533 if (countfile == NULL) {
534 fprintf(stderr, "\n%s: creating %s failed :%s\n",
535 progname, filecount, strerror(errno));
538 /* reserve space for the countfile */
539 if (fprintf(countfile, "%lu", num_files) < 1 ||
540 fflush(countfile) != 0) {
541 fprintf(stderr, "\n%s: writing %s failed :%s\n",
542 progname, filecount, strerror(errno));
547 /* calculate total bytes that need to be written */
548 total_bytes = calc_total_bytes("write");
549 if (total_bytes <= 0) {
550 fprintf(stderr, "\n%s: unable to calculate total bytes\n",
556 if (!full && (dir_num != 0))
557 total_bytes -= dir_num * files_in_dir * file_size;
559 gettimeofday(&start_time, NULL);
560 for (; dir_num < num_dirs; num_files++, file_num++) {
563 if (file_num >= files_in_dir) {
564 if (dir_num == num_dirs - 1)
568 if (mkdir(new_dir(tempdir, dir_num), dirmode) < 0) {
571 if (errno != EEXIST) {
572 fprintf(stderr, "\n%s: mkdir %s : %s\n",
582 fd = open_file(new_file(tempfile, tempdir, file_num),
583 O_WRONLY | O_CREAT | O_TRUNC | O_LARGEFILE);
585 if (fstat64(fd, &file) == 0) {
586 inode_st = file.st_ino;
588 fprintf(stderr, "\n%s: write stat '%s': %s",
589 progname, tempfile, strerror(errno));
597 ret = write_chunks(fd, 0, file_size, chunk_buf, chunksize,
598 time_st, inode_st, tempfile);
601 if (ret != -ENOSPC) {
605 curr_bytes = total_bytes;
609 curr_bytes += file_size;
611 show_rate("write", tempfile, &start_time,
612 total_bytes, curr_bytes);
614 fseek(countfile, 0, SEEK_SET);
615 if (fprintf(countfile, "%lu", num_files) < 1 ||
616 fflush(countfile) != 0) {
617 fprintf(stderr, "\n%s: writing %s failed :%s\n",
618 progname, filecount, strerror(errno));
624 show_rate("write", tempfile, &start_time,
625 total_bytes, curr_bytes);
626 printf("\nwrite complete\n");
637 * dir_read: This function reads directories and files on device.
638 * it works for both full and partial modes.
640 static int dir_read(char *chunk_buf, size_t chunksize,
641 time_t time_st, unsigned long dir_num)
643 char tempfile[PATH_MAX];
644 char tempdir[PATH_MAX];
645 unsigned long count = 0;
649 struct timeval start_time;
650 unsigned long long total_bytes;
651 unsigned long long curr_bytes = 0;
653 /* calculate total bytes that need to be read */
654 total_bytes = calc_total_bytes("read");
655 if (total_bytes <= 0) {
656 fprintf(stderr, "\n%s: unable to calculate total bytes\n",
662 total_bytes -= dir_num * files_in_dir * file_size;
664 gettimeofday(&start_time, NULL);
665 for (count = 0; count < num_files && dir_num < num_dirs; count++) {
669 if (dir_num == num_dirs - 1)
672 new_dir(tempdir, dir_num);
676 fd = open_file(new_file(tempfile, tempdir, file_num),
677 O_RDONLY | O_LARGEFILE);
679 if (fstat64(fd, &file) == 0) {
680 inode_st = file.st_ino;
682 fprintf(stderr, "\n%s: read stat '%s': %s\n",
683 progname, tempfile, strerror(errno));
691 if (count == num_files)
692 file_size = file.st_size;
693 ret = read_chunks(fd, 0, file_size, chunk_buf, chunksize,
694 time_st, inode_st, tempfile);
699 curr_bytes += file_size;
701 show_rate("read", tempfile, &start_time,
702 total_bytes, curr_bytes);
704 if (++file_num >= files_in_dir)
709 show_rate("read", tempfile, &start_time,
710 total_bytes, curr_bytes);
711 printf("\nread complete\n");
717 int main(int argc, char **argv)
719 time_t time_st = 0; /* Default timestamp */
720 size_t chunksize = ONE_MB; /* IO chunk size(defailt=1MB) */
721 char *chunk_buf; /* chunk buffer */
723 FILE *countfile = NULL;
724 unsigned long dir_num = 0, dir_num_orig = 0;/* starting directory */
727 progname = strrchr(argv[0], '/') ? strrchr(argv[0], '/') + 1 : argv[0];
728 while ((c = getopt_long(argc, argv, "c:hlo:pqrs:t:vw",
729 long_opts, NULL)) != -1) {
732 chunksize = strtoul(optarg, NULL, 0) * ONE_MB;
733 if (chunksize == 0) {
734 fprintf(stderr, "%s: bad chunk size '%s'\n",
742 case 'o': /* offset */
743 dir_num = strtoul(optarg, NULL, 0);
758 file_size = strtoul(optarg, NULL, 0) * ONE_MB;
759 if (file_size == 0) {
760 fprintf(stderr, "%s: bad file size '%s'\n",
766 time_st = (time_t)strtoul(optarg, NULL, 0);
781 testdir = argv[optind];
784 fprintf(stderr, "%s: pathname not given\n", progname);
788 if (!readoption && !writeoption) {
793 (void) time(&time_st);
794 printf("Timestamp: %lu\n", (unsigned long )time_st);
795 isatty_flag = isatty(STDOUT_FILENO);
798 #ifdef HAVE_EXT2FS_EXT2FS_H
799 struct mntent *tempmnt;
803 if ((fp = setmntent("/etc/mtab", "r")) == NULL){
804 fprintf(stderr, "%s: fail to open /etc/mtab in read"
805 "mode :%s\n", progname, strerror(errno));
809 /* find device name using filesystem */
810 while ((tempmnt = getmntent(fp)) != NULL) {
811 if (strcmp(tempmnt->mnt_dir, testdir) == 0)
815 if (tempmnt == NULL) {
816 fprintf(stderr, "%s: no device found for '%s'\n",
822 if (ext2fs_open(tempmnt->mnt_fsname, 0, 0, 0,
823 unix_io_manager, &fs)) {
824 fprintf(stderr, "%s: unable to open ext3 fs on '%s'\n",
831 num_dirs = (fs->super->s_blocks_count +
832 fs->super->s_blocks_per_group - 1) /
833 fs->super->s_blocks_per_group;
835 printf("ext3 block groups: %u, fs blocks: %u "
836 "blocks per group: %u\n",
837 num_dirs, fs->super->s_blocks_count,
838 fs->super->s_blocks_per_group);
844 struct statfs64 statbuf;
846 if (statfs64(testdir, &statbuf) == 0) {
847 num_dirs = (long long)statbuf.f_blocks *
848 statbuf.f_bsize / (128ULL << 20);
850 printf("dirs: %u, fs blocks: %llu\n",
852 (long long)statbuf.f_blocks);
854 fprintf(stderr, "%s: unable to stat '%s': %s\n",
855 progname, testdir, strerror(errno));
857 printf("dirs: %u\n", num_dirs);
861 chunk_buf = (char *)calloc(chunksize, 1);
862 if (chunk_buf == NULL) {
863 fprintf(stderr, "Memory allocation failed for chunk_buf\n");
866 snprintf(filecount, sizeof(filecount), "%s/%s.filecount",
869 (void)mkdir(testdir, dirmode);
873 num_files = dir_num * files_in_dir;
875 printf("\n%s: %lu files already written\n",
876 progname, num_files);
878 if (dir_write(chunk_buf, chunksize, time_st, dir_num)) {
882 dir_num = dir_num_orig;
886 countfile = fopen(filecount, "r");
887 if (countfile == NULL ||
888 fscanf(countfile, "%lu", &num_files) != 1 ||
890 fprintf(stderr, "\n%s: reading %s failed :%s\n",
891 progname, filecount, strerror(errno));
892 num_files = num_dirs * files_in_dir;
894 num_files -= (dir_num * files_in_dir);
899 if (dir_read(chunk_buf, chunksize, time_st, dir_num)) {
900 fprintf(stderr, "\n%s: Data verification failed\n",