Whamcloud - gitweb
LU-16977 utils: access_log_reader accesses beyond batch array
[fs/lustre-release.git] / lustre / utils / llverfs.c
index 0ecf8f2..1d3a041 100644 (file)
@@ -1,62 +1,22 @@
-/* -*- 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.
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 2 only,
- * as published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * General Public License version 2 for more details (a copy is included
- * in the LICENSE file that accompanied this code).
- *
- * 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.
- *
- * GPL HEADER END
- */
+// SPDX-License-Identifier: GPL-2.0
+
 /*
  * 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/
- * Lustre is a trademark of Sun Microsystems, Inc.
  *
  * lustre/utils/llverfs.c
  *
- * Filesystem Verification Tool.
- * This program tests the correct operation of large filesystems and
- * the underlying block storage device(s).
- * This tool have two working modes
- * 1. full mode
- * 2. partial mode
+ * Filesystem Verification Tool: this program tests the correct operation of
+ * large filesystems and the underlying block storage device(s). For more
+ * information, see the llverfs.8 man page.
  *
- * In full mode, the program creates a subdirectory in the test
- * 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 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 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
- * filesystem (e.g. 65536 directories for 8TB ext3/ext4 filesystem) and
- * then writes a single 1MB file in each directory. The tool then verifies
- * that the data in each file is correct.
  */
 
 #ifndef _GNU_SOURCE
 #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)
@@ -119,27 +104,26 @@ char filecount[PATH_MAX];     /* file with total number of files written*/
 static unsigned long num_files;            /* Total number of files for read/write */
 static loff_t file_size = 4*ONE_GB; /* Size of each file */
 static unsigned files_in_dir = 32;  /* number of files in each directioy */
-static unsigned num_dirs = 30000;   /* total number of directories */
+static unsigned int num_dirs;      /* total number of directories */
 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 = 'n',   .name = "num_dirs",     .has_arg = required_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
@@ -160,6 +144,7 @@ void usage(int status)
                       "\t-v, --verbose\n"
                       "\t-p, --partial, for partial check (1MB files)\n"
                       "\t-l, --long, --full check (4GB file with 4k blocks)\n"
+                      "\t-n, --num_dirs, number of directories to create\n"
                       "\t-c, --chunksize, IO chunk size in MB (default=1)\n"
                       "\t-s, --filesize, file size in MB (default=4096)\n"
                       "\t-h, --help, display this help and exit\n");
@@ -288,8 +273,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;
                }
@@ -328,7 +313,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;
@@ -396,7 +381,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;
 }
 
@@ -405,7 +390,7 @@ 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;
 }
 
@@ -457,12 +442,9 @@ void show_rate(char *op, char *filename, const struct timeval *start_time,
        static char last_op;
        struct timeval curr_time;
        double curr_delta, overall_delta, curr_rate, overall_rate;
-       double total_time, remain_time;
+       double remain_time;
        int remain_hours, remain_minutes, remain_seconds;
 
-       if (last_time.tv_sec == 0)
-               last_time = *start_time;
-
        if (last_op != op[0]) {
                last_bytes = 0;
                last_time = *start_time;
@@ -484,8 +466,7 @@ void show_rate(char *op, char *filename, const struct timeval *start_time,
                last_time = curr_time;
                return;
        }
-       total_time = total_bytes / curr_rate;
-       remain_time = total_time - overall_delta;
+       remain_time = (total_bytes - curr_bytes) / curr_rate;
 
        remain_hours = remain_time / 3600;
        remain_minutes = (remain_time - remain_hours * 3600) / 60;
@@ -496,8 +477,8 @@ void show_rate(char *op, char *filename, const struct timeval *start_time,
                if (isatty_flag)
                        printf("\r");
 
-               printf("%s filename: %s, current %5g MB/s, overall %5g MB/s, "
-                      "est %u:%u:%u left", 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);
 
@@ -505,10 +486,10 @@ void show_rate(char *op, char *filename, const struct timeval *start_time,
                        fflush(stdout);
                else
                        printf("\n");
-       }
 
-       last_time = curr_time;
-       last_bytes = curr_bytes;
+               last_time = curr_time;
+               last_bytes = curr_bytes;
+       }
 }
 
 /*
@@ -527,13 +508,13 @@ static int dir_write(char *chunk_buf, size_t chunksize,
        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",
@@ -545,7 +526,8 @@ 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 */
@@ -553,7 +535,8 @@ static int dir_write(char *chunk_buf, size_t chunksize,
        if (total_bytes <= 0) {
                fprintf(stderr, "\n%s: unable to calculate total bytes\n",
                        progname);
-               return 7;
+               rc = 7;
+               goto out;
        }
 
        if (!full && (dir_num != 0))
@@ -564,9 +547,6 @@ static int dir_write(char *chunk_buf, size_t chunksize,
                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)
@@ -575,7 +555,8 @@ 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++;
@@ -600,8 +581,10 @@ static int dir_write(char *chunk_buf, size_t 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;
                }
@@ -618,17 +601,16 @@ static int dir_write(char *chunk_buf, size_t chunksize,
                                progname, filecount, strerror(errno));
                }
        }
-       fclose(countfile);
 
-       if (verbose) {
-               verbose++;
-               show_rate("write", tempfile, &start_time,
-                         total_bytes, curr_bytes);
-               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;
 }
 
 /*
@@ -664,9 +646,6 @@ static int dir_read(char *chunk_buf, size_t chunksize,
                int fd, ret;
 
                if (file_num == 0) {
-                       if (dir_num == num_dirs - 1)
-                               break;
-
                        new_dir(tempdir, dir_num);
                        dir_num++;
                }
@@ -702,13 +681,11 @@ static int dir_read(char *chunk_buf, size_t chunksize,
                if (++file_num >= files_in_dir)
                        file_num = 0;
        }
-       if (verbose > 1){
-               verbose++;
-               show_rate("read", tempfile, &start_time,
-                         total_bytes, curr_bytes);
-               printf("\nread complete\n");
-               verbose--;
-       }
+       verbose += 2;
+       show_rate("read_done", tempfile, &start_time, total_bytes, curr_bytes);
+       printf("\n");
+       verbose -= 2;
+
        return 0;
 }
 
@@ -723,8 +700,8 @@ int main(int argc, char **argv)
        int c;
 
        progname = strrchr(argv[0], '/') ? strrchr(argv[0], '/') + 1 : argv[0];
-       while ((c = getopt_long(argc, argv, "c:hlo:pqrs:t:vw",
-                                     longopts, NULL)) != -1) {
+       while ((c = getopt_long(argc, argv, "c:hln:o:pqrs:t:vw",
+                                     long_opts, NULL)) != -1) {
                switch (c) {
                case 'c':
                        chunksize = strtoul(optarg, NULL, 0) * ONE_MB;
@@ -737,6 +714,9 @@ int main(int argc, char **argv)
                case 'l':
                        full = 1;
                        break;
+               case 'n':
+                       num_dirs = strtoul(optarg, NULL, 0);
+                       break;
                case 'o': /* offset */
                        dir_num = strtoul(optarg, NULL, 0);
                        break;
@@ -792,15 +772,15 @@ int main(int argc, char **argv)
        printf("Timestamp: %lu\n", (unsigned long )time_st);
        isatty_flag = isatty(STDOUT_FILENO);
 
-       if (!full) {
+       if (!full && !num_dirs) {
 #ifdef HAVE_EXT2FS_EXT2FS_H
                struct mntent *tempmnt;
                FILE *fp = NULL;
                ext2_filsys fs;
 
-               if ((fp = setmntent("/etc/mtab", "r")) == NULL){
-                       fprintf(stderr, "%s: fail to open /etc/mtab in read"
-                               "mode :%s\n", progname, strerror(errno));
+               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;
                }
 
@@ -838,22 +818,34 @@ int main(int argc, char **argv)
 #else
                goto guess;
 #endif
-               if (0) { /* ugh */
-                       struct statfs64 statbuf;
+       }
+       if (!num_dirs) {
+               struct statfs64 statbuf;
 guess:
-                       if (statfs64(testdir, &statbuf) == 0) {
-                               num_dirs = (long long)statbuf.f_blocks *
-                                       statbuf.f_bsize / (128ULL << 20);
-                               if (verbose)
-                                       printf("dirs: %u, fs blocks: %llu\n",
-                                              num_dirs,
-                                              (long long)statbuf.f_blocks);
-                       } else {
-                               fprintf(stderr, "%s: unable to stat '%s': %s\n",
-                                       progname, testdir, strerror(errno));
-                               if (verbose)
-                                       printf("dirs: %u\n", num_dirs);
-                       }
+               /*
+                * 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 = 1 + (long long)statbuf.f_blocks *
+                                                 statbuf.f_bsize /
+                               (full ? files_in_dir * file_size : 128*ONE_MB);
+                       if (verbose)
+                               printf("dirs: %u, fs blocks: %llu\n",
+                                      num_dirs, (long long)statbuf.f_blocks);
+               } else {
+                       fprintf(stderr, "%s: unable to stat '%s': %s\n",
+                               progname, testdir, strerror(errno));
+                       if (!num_dirs)
+                               num_dirs = 30000;
+                       if (verbose)
+                               printf("dirs: %u\n", num_dirs);
                }
        }
        chunk_buf = (char *)calloc(chunksize, 1);
@@ -861,7 +853,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);