Whamcloud - gitweb
LU-9897 utils: remove llverfs dependency on libext2fs
[fs/lustre-release.git] / lustre / utils / llverfs.c
index 6d7a85d..5e328e1 100644 (file)
@@ -1,6 +1,4 @@
-/* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
- * vim:expandtab:shiftwidth=8:tabstop=8:
- *
+/*
  * GPL HEADER START
  *
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * You should have received a copy of the GNU General Public License
  * version 2 along with this program; If not, see
- * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
- *
- * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
- * CA 95054 USA or visit www.sun.com if you need additional information or
- * have any questions.
+ * http://www.gnu.org/licenses/gpl-2.0.html
  *
  * GPL HEADER END
  */
 /*
- * Copyright  2009 Sun Microsystems, Inc. All rights reserved
+ * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
  * Use is subject to license terms.
+ *
+ * Copyright (c) 2011, Intel Corporation.
  */
 /*
  * This file is part of Lustre, http://www.lustre.org/
  * the underlying block storage device(s).
  * This tool have two working modes
  * 1. full mode
- * 2. fast mode
+ * 2. partial mode
  *
  * In full mode, the program creates a subdirectory in the test
- * fileysytem, writes n(files_in_dir, default=16) large(4GB) files to
+ * filesystem, writes n(files_in_dir, default=32) large(4GB) files to
  * the directory with the test pattern at the start of each 4kb block.
  * The test pattern contains timestamp, relative file offset and per
- * file unique idenfifier(inode number).  This continues until the
+ * file unique identifier(inode number).  This continues until the
  * whole filesystem is full and then the tool verifies that the data
  * in all of the test files is correct.
  *
- * In fast mode, the tool creates test directories with the
+ * In partial mode, the tool creates test directories with the
  * EXT3_TOPDIR_FL flag set (if supported) to spread the directory data
  * around the block device instead of localizing it in a single place.
  * The number of directories equals to the number of block groups in the
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <sys/vfs.h>
+#include <sys/time.h>
 #include <gnu/stubs.h>
 #include <gnu/stubs.h>
 
 #ifdef HAVE_EXT2FS_EXT2FS_H
 #  include <e2p/e2p.h>
 #  include <ext2fs/ext2fs.h>
+#else
+#  ifndef EXT2_TOPDIR_FL
+#    define EXT2_TOPDIR_FL             0x00020000 /* Top of directory tree */
+#  endif
+static int fsetflags(const char *path, unsigned int flag)
+{
+       char cmd[PATH_MAX + 128];
+       int rc;
+
+       if (flag != EXT2_TOPDIR_FL) {
+               rc = EOPNOTSUPP;
+               goto out;
+       }
+
+       snprintf(cmd, sizeof(cmd), "chattr +T %s", path);
+
+       rc = system(cmd);
+       if (rc > 0) {
+               rc = WEXITSTATUS(rc);
+out:
+               errno = rc;
+       }
+
+       return rc;
+}
 #endif
 
 #define ONE_MB (1024 * 1024)
@@ -123,22 +145,20 @@ const int dirmode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
 static int isatty_flag;
 static int perms =  S_IRWXU | S_IRGRP | S_IROTH;
 
-static struct option const longopts[] =
-{
-       { "chunksize", required_argument, 0, 'c' },
-       { "help", no_argument, 0, 'h' },
-       { "offset", required_argument, 0, 'o' },
-       { "long", no_argument, 0, 'l' },
-       { "full", no_argument, 0, 'l' },
-       { "partial", required_argument, 0, 'p' },
-       { "quiet", required_argument, 0, 'q' },
-       { "read", no_argument, 0, 'r' },
-       { "filesize", no_argument, 0, 's' },
-       { "timestamp", required_argument, 0, 't' },
-       { "verbose", no_argument, 0, 'v' },
-       { "write", no_argument, 0, 'w' },
-       { 0, 0, 0, 0}
-};
+static struct option const long_opts[] = {
+       { .val = 'c',   .name = "chunksize",    .has_arg = required_argument },
+       { .val = 'h',   .name = "help",         .has_arg = no_argument },
+       { .val = 'l',   .name = "long",         .has_arg = no_argument },
+       { .val = 'l',   .name = "full",         .has_arg = no_argument },
+       { .val = 'o',   .name = "offset",       .has_arg = required_argument },
+       { .val = 'p',   .name = "partial",      .has_arg = required_argument },
+       { .val = 'q',   .name = "quiet",        .has_arg = required_argument },
+       { .val = 'r',   .name = "read",         .has_arg = no_argument },
+       { .val = 's',   .name = "filesize",     .has_arg = no_argument },
+       { .val = 't',   .name = "timestamp",    .has_arg = required_argument },
+       { .val = 'v',   .name = "verbose",      .has_arg = no_argument },
+       { .val = 'w',   .name = "write",        .has_arg = no_argument },
+       { .name = NULL } };
 
 /*
  * Usages: displays help information, whenever user supply --help option in
@@ -149,7 +169,7 @@ void usage(int status)
        if (status != 0) {
                printf("\nUsage: %s [OPTION]... <filesystem path> ...\n",
                       progname);
-               printf("ext3 filesystem verification tool.\n"
+               printf("Filesystem verification tool.\n"
                       "\t-t {seconds}, --timestamp,  set test time"
                       "(default=current time())\n"
                       "\t-o {offset}, --offset, directory starting offset"
@@ -175,7 +195,6 @@ static int open_file(const char *file, int flag)
        if (fd < 0) {
                fprintf(stderr, "\n%s: Open '%s' failed:%s\n",
                        progname, file, strerror(errno));
-               exit(3);
        }
        return (fd);
 }
@@ -209,7 +228,7 @@ int verify_chunk(char *chunk_buf, const size_t chunksize,
 
 /*
  * fill_chunk: Fills the chunk with current or user specified timestamp
- * and  offset. The test patters is filled at the beginning of
+ * and offset. The test pattern is filled at the beginning of
  * each 4kB(BLOCKSIZE) blocks in chunk_buf.
  */
 void fill_chunk(char *chunk_buf, size_t chunksize, loff_t chunk_off,
@@ -263,6 +282,7 @@ retry:
                fprintf(stderr, "\n%s: write %s@%llu+%zi short: %ld written\n",
                        progname, file, offset, nrequested, nwritten);
                offset += nwritten;
+               chunk_buf += nwritten;
                nrequested -= nwritten;
                goto retry;
        }
@@ -287,8 +307,8 @@ int write_chunks(int fd, unsigned long long offset,unsigned long long write_end,
             offset += stride) {
                int ret;
 
-               if (lseek64(fd, offset, SEEK_SET) == -1) {
-                       fprintf(stderr, "\n%s: lseek64(%s+%llu) failed: %s\n",
+               if (stride != chunksize && lseek64(fd, offset, SEEK_SET) < 0) {
+                       fprintf(stderr, "\n%s: lseek66(%s+%llu) failed: %s\n",
                                progname, file, offset, strerror(errno));
                        return -errno;
                }
@@ -327,7 +347,7 @@ int read_chunks(int fd, unsigned long long offset, unsigned long long read_end,
             offset += stride) {
                ssize_t nread;
 
-               if (lseek64(fd, offset, SEEK_SET) == -1) {
+               if (stride != chunksize && lseek64(fd, offset, SEEK_SET) < 0) {
                        fprintf(stderr, "\n%s: lseek64(%s+%llu) failed: %s\n",
                                progname, file, offset, strerror(errno));
                        return 1;
@@ -395,7 +415,7 @@ int read_chunks(int fd, unsigned long long offset, unsigned long long read_end,
  */
 char *new_file(char *tempfile, char *cur_dir, int file_num)
 {
-       sprintf(tempfile, "%s/file%03d", cur_dir, file_num);
+       snprintf(tempfile, PATH_MAX, "%s/file%03d", cur_dir, file_num);
        return tempfile;
 }
 
@@ -404,36 +424,111 @@ char *new_file(char *tempfile, char *cur_dir, int file_num)
  */
 char *new_dir(char *tempdir, int dir_num)
 {
-       sprintf(tempdir, "%s/dir%05d", testdir, dir_num);
+       snprintf(tempdir, PATH_MAX, "%s/llverfs_dir%05d", testdir, dir_num);
        return tempdir;
 }
 
 /*
- * show_filename: Displays name of current file read/write
+ * calc_total_bytes: calculates total bytes that need to be
+ * written into or read from the filesystem.
+ */
+static unsigned long long calc_total_bytes(const char *op)
+{
+       unsigned long long total_bytes = 0;
+       struct statfs64 statbuf;
+
+       if (full) {
+               if (statfs64(testdir, &statbuf) == 0) {
+                       if (strcmp(op, "write") == 0)
+                               total_bytes = (unsigned long long)
+                                       (statbuf.f_bavail * statbuf.f_bsize);
+                       else if (strcmp(op, "read") == 0)
+                               total_bytes = (unsigned long long)
+                                       (statbuf.f_blocks * statbuf.f_bsize);
+                       else {
+                               fprintf(stderr, "\n%s: invalid operation: %s\n",
+                                       progname, op);
+                               return -1;
+                       }
+               } else {
+                       fprintf(stderr, "\n%s: unable to stat %s: %s\n",
+                               progname, testdir, strerror(errno));
+                       return -errno;
+               }
+       } else {
+               total_bytes = num_dirs * files_in_dir * file_size;
+       }
+
+       return total_bytes;
+}
+
+/*
+ * show_rate: displays the current read/write file name and performance,
+ * along with an estimate of how long the whole read/write operation
+ * will continue.
  */
-void show_filename(char *op, char *filename)
+void show_rate(char *op, char *filename, const struct timeval *start_time,
+              const unsigned long long total_bytes,
+              const unsigned long long curr_bytes)
 {
-       static time_t last;
-       time_t now;
-       double diff;
+       static struct timeval last_time;
+       static unsigned long long last_bytes;
+       static char last_op;
+       struct timeval curr_time;
+       double curr_delta, overall_delta, curr_rate, overall_rate;
+       double remain_time;
+       int remain_hours, remain_minutes, remain_seconds;
+
+       if (last_op != op[0]) {
+               last_bytes = 0;
+               last_time = *start_time;
+               last_op = op[0];
+       }
+
+       gettimeofday(&curr_time, NULL);
+
+       curr_delta = (curr_time.tv_sec - last_time.tv_sec) +
+               (double)(curr_time.tv_usec - last_time.tv_usec) / 1000000;
 
-       now = time(NULL);
-       diff = now - last;
-       if (diff > 4 || verbose > 2) {
+       overall_delta = (curr_time.tv_sec - start_time->tv_sec) +
+               (double)(curr_time.tv_usec - start_time->tv_usec) / 1000000;
+
+       curr_rate = (curr_bytes - last_bytes) / curr_delta;
+       overall_rate = curr_bytes / overall_delta;
+
+       if (curr_rate == 0) {
+               last_time = curr_time;
+               return;
+       }
+       remain_time = (total_bytes - curr_bytes) / curr_rate;
+
+       remain_hours = remain_time / 3600;
+       remain_minutes = (remain_time - remain_hours * 3600) / 60;
+       remain_seconds = (remain_time - remain_hours * 3600 -
+               remain_minutes * 60);
+
+       if (curr_delta > 4 || verbose > 2) {
                if (isatty_flag)
                        printf("\r");
-               printf("%s File name: %s          ", op, filename);
+
+               printf("%s: %s, current: %5g MB/s, overall: %5g MB/s, "
+                      "ETA: %u:%02u:%02u", op, filename,
+                      curr_rate / ONE_MB, overall_rate / ONE_MB,
+                      remain_hours, remain_minutes, remain_seconds);
+
                if (isatty_flag)
                        fflush(stdout);
                else
                        printf("\n");
-               last = now;
+
+               last_time = curr_time;
+               last_bytes = curr_bytes;
        }
 }
 
 /*
  * dir_write: This function writes directories and files on device.
- * it works for both full and fast modes.
+ * it works for both full and partial modes.
  */
 static int dir_write(char *chunk_buf, size_t chunksize,
                     time_t time_st, unsigned long dir_num)
@@ -444,13 +539,16 @@ static int dir_write(char *chunk_buf, size_t chunksize,
        struct stat64 file;
        int file_num = 999999999;
        ino_t inode_st = 0;
+       struct timeval start_time;
+       unsigned long long total_bytes;
+       unsigned long long curr_bytes = 0;
+       int rc = 0;
 
-#ifdef HAVE_EXT2FS_EXT2FS_H
        if (!full && fsetflags(testdir, EXT2_TOPDIR_FL))
                fprintf(stderr,
                        "\n%s: can't set TOPDIR_FL on %s: %s (ignoring)",
                        progname, testdir, strerror(errno));
-#endif
+
        countfile = fopen(filecount, "w");
        if (countfile == NULL) {
                fprintf(stderr, "\n%s: creating %s failed :%s\n",
@@ -462,15 +560,27 @@ static int dir_write(char *chunk_buf, size_t chunksize,
            fflush(countfile) != 0) {
                fprintf(stderr, "\n%s: writing %s failed :%s\n",
                        progname, filecount, strerror(errno));
-               return 6;
+               rc = 6;
+               goto out;
+       }
+
+       /* calculate total bytes that need to be written */
+       total_bytes = calc_total_bytes("write");
+       if (total_bytes <= 0) {
+               fprintf(stderr, "\n%s: unable to calculate total bytes\n",
+                       progname);
+               rc = 7;
+               goto out;
        }
+
+       if (!full && (dir_num != 0))
+               total_bytes -= dir_num * files_in_dir * file_size;
+
+       gettimeofday(&start_time, NULL);
        for (; dir_num < num_dirs; num_files++, file_num++) {
                int fd, ret;
 
                if (file_num >= files_in_dir) {
-                       if (dir_num == num_dirs - 1)
-                               break;
-
                        file_num = 0;
                        if (mkdir(new_dir(tempdir, dir_num), dirmode) < 0) {
                                if (errno == ENOSPC)
@@ -479,34 +589,45 @@ static int dir_write(char *chunk_buf, size_t chunksize,
                                        fprintf(stderr, "\n%s: mkdir %s : %s\n",
                                                progname, tempdir,
                                                strerror(errno));
-                                       return 1;
+                                       rc = 1;
+                                       goto out;
                                }
                        }
                        dir_num++;
                }
+
                fd = open_file(new_file(tempfile, tempdir, file_num),
                               O_WRONLY | O_CREAT | O_TRUNC | O_LARGEFILE);
-
-               if (fd >= 0 && fstat64(fd, &file) == 0) {
-                       inode_st = file.st_ino;
+               if (fd >= 0) {
+                       if (fstat64(fd, &file) == 0) {
+                               inode_st = file.st_ino;
+                       } else {
+                               fprintf(stderr, "\n%s: write stat '%s': %s",
+                                       progname, tempfile, strerror(errno));
+                               close(fd);
+                               break;
+                       }
                } else {
-                       fprintf(stderr, "\n%s: write stat64 to file %s: %s",
-                               progname, tempfile, strerror(errno));
-                       exit(1);
+                       break;
                }
 
-               if (verbose > 1)
-                       show_filename("write", tempfile);
-
                ret = write_chunks(fd, 0, file_size, chunk_buf, chunksize,
                                   time_st, inode_st, tempfile);
                close(fd);
                if (ret < 0) {
-                       if (ret != -ENOSPC)
-                               return 1;
+                       if (ret != -ENOSPC) {
+                               rc = 1;
+                               goto out;
+                       }
+                       curr_bytes = total_bytes;
                        break;
                }
 
+               curr_bytes += file_size;
+               if (verbose > 1)
+                       show_rate("write", tempfile, &start_time,
+                                 total_bytes, curr_bytes);
+
                fseek(countfile, 0, SEEK_SET);
                if (fprintf(countfile, "%lu", num_files) < 1 ||
                    fflush(countfile) != 0) {
@@ -514,21 +635,21 @@ static int dir_write(char *chunk_buf, size_t chunksize,
                                progname, filecount, strerror(errno));
                }
        }
-       fclose(countfile);
 
-       if (verbose) {
-               verbose++;
-               show_filename("write", tempfile);
-               printf("\nwrite complete\n");
-               verbose--;
-       }
+       verbose += 2;
+       show_rate("write_done", tempfile, &start_time, total_bytes, curr_bytes);
+       printf("\n");
+       verbose -= 2;
 
-       return 0;
+out:
+       fclose(countfile);
+
+       return rc;
 }
 
 /*
  * dir_read: This function reads directories and files on device.
- * it works for both full and fast modes.
+ * it works for both full and partial modes.
  */
 static int dir_read(char *chunk_buf, size_t chunksize,
                    time_t time_st, unsigned long dir_num)
@@ -539,31 +660,45 @@ static int dir_read(char *chunk_buf, size_t chunksize,
        struct stat64 file;
        int file_num = 0;
        ino_t inode_st = 0;
+       struct timeval start_time;
+       unsigned long long total_bytes;
+       unsigned long long curr_bytes = 0;
+
+       /* calculate total bytes that need to be read */
+       total_bytes = calc_total_bytes("read");
+       if (total_bytes <= 0) {
+               fprintf(stderr, "\n%s: unable to calculate total bytes\n",
+                       progname);
+               return 1;
+       }
 
+       if (dir_num != 0)
+               total_bytes -= dir_num * files_in_dir * file_size;
+
+       gettimeofday(&start_time, NULL);
        for (count = 0; count < num_files && dir_num < num_dirs; count++) {
                int fd, ret;
 
                if (file_num == 0) {
-                       if (dir_num == num_dirs - 1)
-                               break;
-
                        new_dir(tempdir, dir_num);
                        dir_num++;
                }
 
                fd = open_file(new_file(tempfile, tempdir, file_num),
                               O_RDONLY | O_LARGEFILE);
-               if (fd >= 0 && fstat64(fd, &file) == 0) {
-                       inode_st = file.st_ino;
+               if (fd >= 0) {
+                       if (fstat64(fd, &file) == 0) {
+                               inode_st = file.st_ino;
+                       } else {
+                               fprintf(stderr, "\n%s: read stat '%s': %s\n",
+                                       progname, tempfile, strerror(errno));
+                               close(fd);
+                               return 1;
+                       }
                } else {
-                       fprintf(stderr, "\n%s: read stat64 file '%s': %s\n",
-                               progname, tempfile, strerror(errno));
-                       return 1;
+                       break;
                }
 
-               if (verbose > 1)
-                       show_filename("read", tempfile);
-
                if (count == num_files)
                        file_size = file.st_size;
                ret = read_chunks(fd, 0, file_size, chunk_buf, chunksize,
@@ -572,15 +707,19 @@ static int dir_read(char *chunk_buf, size_t chunksize,
                if (ret)
                        return 1;
 
+               curr_bytes += file_size;
+               if (verbose > 1)
+                       show_rate("read", tempfile, &start_time,
+                                 total_bytes, curr_bytes);
+
                if (++file_num >= files_in_dir)
                        file_num = 0;
        }
-       if (verbose > 1){
-               verbose++;
-               show_filename("read", tempfile);
-               printf("\nread complete\n");
-               verbose--;
-       }
+       verbose += 2;
+       show_rate("read_done", tempfile, &start_time, total_bytes, curr_bytes);
+       printf("\n");
+       verbose -= 2;
+
        return 0;
 }
 
@@ -596,13 +735,13 @@ int main(int argc, char **argv)
 
        progname = strrchr(argv[0], '/') ? strrchr(argv[0], '/') + 1 : argv[0];
        while ((c = getopt_long(argc, argv, "c:hlo:pqrs:t:vw",
-                                     longopts, NULL)) != -1) {
+                                     long_opts, NULL)) != -1) {
                switch (c) {
                case 'c':
-                       chunksize = strtoul(optarg, NULL, 0) * ONE_MB;
-                       if (chunksize == 0) {
-                               fprintf(stderr, "%s: bad chunk size '%s'\n",
-                                       optarg, progname);
+                       chunksize = strtoul(optarg, NULL, 0) * ONE_MB;
+                       if (chunksize == 0) {
+                               fprintf(stderr, "%s: bad chunk size '%s'\n",
+                                       optarg, progname);
                                return -1;
                        }
                        break;
@@ -670,7 +809,7 @@ int main(int argc, char **argv)
                FILE *fp = NULL;
                ext2_filsys fs;
 
-               if ((fp = setmntent("/etc/mtab", "r")) == NULL){
+               if ((fp = setmntent("/etc/mtab", "r")) == NULL) {
                        fprintf(stderr, "%s: fail to open /etc/mtab in read"
                                "mode :%s\n", progname, strerror(errno));
                        goto guess;
@@ -713,9 +852,19 @@ int main(int argc, char **argv)
                if (0) { /* ugh */
                        struct statfs64 statbuf;
 guess:
+                       /*
+                        * Most extN filesystems are formatted with 128MB/group
+                        * (32k bitmap = 4KB blocksize * 8 bits/block) * 4KB,
+                        * so this is a relatively safe default (somewhat more
+                        * or less doesn't make a huge difference for testing).
+                        *
+                        * We want to create one directory per group, together
+                        * with the "TOPDIR" feature, so that the directories
+                        * are spread across the whole block device.
+                        */
                        if (statfs64(testdir, &statbuf) == 0) {
-                               num_dirs = (long long)statbuf.f_blocks *
-                                       statbuf.f_bsize / (128ULL << 20);
+                               num_dirs = 1 + (long long)statbuf.f_blocks *
+                                       statbuf.f_bsize / (128ULL * ONE_MB);
                                if (verbose)
                                        printf("dirs: %u, fs blocks: %llu\n",
                                               num_dirs,
@@ -733,7 +882,8 @@ guess:
                fprintf(stderr, "Memory allocation failed for chunk_buf\n");
                return 4;
        }
-       sprintf(filecount, "%s/%s.filecount", testdir, progname);
+       snprintf(filecount, sizeof(filecount), "%s/%s.filecount",
+                testdir, progname);
        if (writeoption) {
                (void)mkdir(testdir, dirmode);