Whamcloud - gitweb
LU-15644 llog: don't replace llog error with -ENOTDIR
[fs/lustre-release.git] / lustre / utils / llverfs.c
index a32954f..1c9663e 100644 (file)
@@ -1,58 +1,22 @@
-/*
- * 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.gnu.org/licenses/gpl-2.0.html
- *
- * 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
- *
- * 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.
+ * 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 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
@@ -140,7 +104,7 @@ 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;
@@ -150,6 +114,7 @@ static struct option const long_opts[] = {
        { .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 },
@@ -164,7 +129,7 @@ static struct option const long_opts[] = {
  * Usages: displays help information, whenever user supply --help option in
  * command or enters incorrect command line.
  */
-void usage(int status)
+static void usage(int status)
 {
        if (status != 0) {
                printf("\nUsage: %s [OPTION]... <filesystem path> ...\n",
@@ -179,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");
@@ -203,9 +169,11 @@ static int open_file(const char *file, int flag)
  * Verify_chunk: Verifies test pattern in each 4kB (BLOCKSIZE) is correct.
  * Returns 0 if test offset and timestamp is correct otherwise 1.
  */
-int verify_chunk(char *chunk_buf, const size_t chunksize,
-                unsigned long long chunk_off, const unsigned long long time_st,
-                const unsigned long long inode_st, const char *file)
+static int verify_chunk(char *chunk_buf, const size_t chunksize,
+                       unsigned long long chunk_off,
+                       const unsigned long long time_st,
+                       const unsigned long long inode_st,
+                       const char *file)
 {
        struct block_data *bd;
        char *chunk_end;
@@ -231,8 +199,8 @@ int verify_chunk(char *chunk_buf, const size_t chunksize,
  * 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,
-               const time_t time_st, const ino_t inode_st)
+static void fill_chunk(char *chunk_buf, size_t chunksize, loff_t chunk_off,
+                      const time_t time_st, const ino_t inode_st)
 {
        struct block_data *bd;
        char *chunk_end;
@@ -261,8 +229,8 @@ void fill_chunk(char *chunk_buf, size_t chunksize, loff_t chunk_off,
  *
  * Returns 0 on success, or -ve errno on failure.
  */
-int write_retry(int fd, const char *chunk_buf, size_t nrequested,
-               unsigned long long offset, const char *file)
+static int write_retry(int fd, const char *chunk_buf, size_t nrequested,
+                      unsigned long long offset, const char *file)
 {
        long nwritten;
 
@@ -296,9 +264,10 @@ retry:
  *
  * Returns 0 on success, or -ve error number on failure.
  */
-int write_chunks(int fd, unsigned long long offset,unsigned long long write_end,
-                char *chunk_buf, size_t chunksize, const time_t time_st,
-                const ino_t inode_st, const char *file)
+static int write_chunks(int fd, unsigned long long offset,
+                       unsigned long long write_end, char *chunk_buf,
+                       size_t chunksize, const time_t time_st,
+                       const ino_t inode_st, const char *file)
 {
        unsigned long long stride;
 
@@ -336,9 +305,10 @@ int write_chunks(int fd, unsigned long long offset,unsigned long long write_end,
  * read_chunk: reads the chunk_buf from the device. The number of read
  * operations are based on the parameters read_end, offset, and chunksize.
  */
-int read_chunks(int fd, unsigned long long offset, unsigned long long read_end,
-               char *chunk_buf, size_t chunksize, const time_t time_st,
-               const ino_t inode_st, const char *file)
+static int read_chunks(int fd, unsigned long long offset,
+                      unsigned long long read_end, char *chunk_buf,
+                      size_t chunksize, const time_t time_st,
+                      const ino_t inode_st, const char *file)
 {
        unsigned long long stride;
 
@@ -413,18 +383,28 @@ int read_chunks(int fd, unsigned long long offset, unsigned long long read_end,
 /*
  * new_file: prepares new filename using file counter and current dir.
  */
-char *new_file(char *tempfile, char *cur_dir, int file_num)
+static char *new_file(char *tempfile, char *cur_dir, int file_num)
 {
-       snprintf(tempfile, PATH_MAX, "%s/file%03d", cur_dir, file_num);
+       int rc = 0;
+
+       rc = snprintf(tempfile, PATH_MAX, "%s/file%03d", cur_dir, file_num);
+       if (rc >= PATH_MAX || rc < 0)
+               return NULL;
+
        return tempfile;
 }
 
 /*
  * new_dir: prepares new dir name using dir counters.
  */
-char *new_dir(char *tempdir, int dir_num)
+static char *new_dir(char *tempdir, int dir_num)
 {
-       snprintf(tempdir, PATH_MAX, "%s/llverfs_dir%05d", testdir, dir_num);
+       int rc = 0;
+
+       rc = snprintf(tempdir, PATH_MAX, "%s/llverfs_dir%05d", testdir, dir_num);
+       if (rc >= PATH_MAX || rc < 0)
+               return NULL;
+
        return tempdir;
 }
 
@@ -467,9 +447,10 @@ static unsigned long long calc_total_bytes(const char *op)
  * along with an estimate of how long the whole read/write operation
  * will continue.
  */
-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 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 struct timeval last_time;
        static unsigned long long last_bytes;
@@ -734,8 +715,10 @@ 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",
+       while ((c = getopt_long(argc, argv, "c:hln:o:pqrs:t:vw",
                                      long_opts, NULL)) != -1) {
+               unsigned long val;    /* Staging value for num_dirs */
+
                switch (c) {
                case 'c':
                        chunksize = strtoul(optarg, NULL, 0) * ONE_MB;
@@ -748,6 +731,20 @@ int main(int argc, char **argv)
                case 'l':
                        full = 1;
                        break;
+               case 'n':
+                       /* num_dirs cannot be negative */
+                       if (optarg[0] == '-')
+                               goto out_num_dirs;
+                       val = strtoul(optarg, NULL, 0);
+                       if (val == 0 || val > INT_MAX || errno == ERANGE)
+                               goto out_num_dirs;
+                       num_dirs = val;
+                       break;
+out_num_dirs:
+                       fprintf(stderr, "%s: bad directory count '%s'", optarg,
+                               progname);
+                       usage(1);
+                       return -1;
                case 'o': /* offset */
                        dir_num = strtoul(optarg, NULL, 0);
                        break;
@@ -803,7 +800,7 @@ 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;
@@ -849,32 +846,34 @@ int main(int argc, char **argv)
 #else
                goto guess;
 #endif
-               if (0) { /* ugh */
-                       struct statfs64 statbuf;
+       }
+       if (!num_dirs) {
+               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 = 1 + (long long)statbuf.f_blocks *
-                                       statbuf.f_bsize / (128ULL * 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 (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);