1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
5 * Use is subject to license terms.
7 * Copyright (c) 2011, Intel Corporation.
12 * This file is part of Lustre, http://www.lustre.org/
14 * lustre/utils/llverfs.c
16 * Filesystem Verification Tool: this program tests the correct operation of
17 * large filesystems and the underlying block storage device(s). For more
18 * information, see the llverfs.8 man page.
28 #ifndef _LARGEFILE64_SOURCE
29 #define _LARGEFILE64_SOURCE
31 #ifndef _FILE_OFFSET_BITS
32 #define _FILE_OFFSET_BITS 64
49 #include <sys/types.h>
53 #include <gnu/stubs.h>
54 #include <gnu/stubs.h>
56 #ifdef HAVE_EXT2FS_EXT2FS_H
58 # include <ext2fs/ext2fs.h>
60 # ifndef EXT2_TOPDIR_FL
61 # define EXT2_TOPDIR_FL 0x00020000 /* Top of directory tree */
63 static int fsetflags(const char *path, unsigned int flag)
65 char cmd[PATH_MAX + 128];
68 if (flag != EXT2_TOPDIR_FL) {
73 snprintf(cmd, sizeof(cmd), "chattr +T %s", path);
86 #define ONE_MB (1024 * 1024)
87 #define ONE_GB ((unsigned long long)(1024 * 1024 * 1024))
88 #define BLOCKSIZE 4096
90 /* Structure for writing test pattern */
92 unsigned long long bd_offset;
93 unsigned long long bd_time;
94 unsigned long long bd_inode;
96 static char *progname; /* name by which this program was run. */
97 static unsigned verbose = 1; /* prints offset in kB, operation rate */
98 static int readoption; /* run test in read-only (verify) mode */
99 static int writeoption; /* run test in write_only mode */
100 char *testdir; /* name of device to be tested. */
101 static unsigned full = 1; /* flag to full check */
102 static int error_count; /* number of IO errors hit during run */
103 char filecount[PATH_MAX]; /* file with total number of files written*/
104 static unsigned long num_files; /* Total number of files for read/write */
105 static loff_t file_size = 4*ONE_GB; /* Size of each file */
106 static unsigned files_in_dir = 32; /* number of files in each directioy */
107 static unsigned int num_dirs; /* total number of directories */
108 const int dirmode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
109 static int isatty_flag;
110 static int perms = S_IRWXU | S_IRGRP | S_IROTH;
112 static struct option const long_opts[] = {
113 { .val = 'c', .name = "chunksize", .has_arg = required_argument },
114 { .val = 'h', .name = "help", .has_arg = no_argument },
115 { .val = 'l', .name = "long", .has_arg = no_argument },
116 { .val = 'l', .name = "full", .has_arg = no_argument },
117 { .val = 'n', .name = "num_dirs", .has_arg = required_argument },
118 { .val = 'o', .name = "offset", .has_arg = required_argument },
119 { .val = 'p', .name = "partial", .has_arg = required_argument },
120 { .val = 'q', .name = "quiet", .has_arg = required_argument },
121 { .val = 'r', .name = "read", .has_arg = no_argument },
122 { .val = 's', .name = "filesize", .has_arg = no_argument },
123 { .val = 't', .name = "timestamp", .has_arg = required_argument },
124 { .val = 'v', .name = "verbose", .has_arg = no_argument },
125 { .val = 'w', .name = "write", .has_arg = no_argument },
129 * Usages: displays help information, whenever user supply --help option in
130 * command or enters incorrect command line.
132 void usage(int status)
135 printf("\nUsage: %s [OPTION]... <filesystem path> ...\n",
137 printf("Filesystem verification tool.\n"
138 "\t-t {seconds}, --timestamp, set test time"
139 "(default=current time())\n"
140 "\t-o {offset}, --offset, directory starting offset"
141 " from which tests should start\n"
142 "\t-r, --read, run in verify mode\n"
143 "\t-w, --write, run in test-pattern mode, default=rw\n"
145 "\t-p, --partial, for partial check (1MB files)\n"
146 "\t-l, --long, --full check (4GB file with 4k blocks)\n"
147 "\t-n, --num_dirs, number of directories to create\n"
148 "\t-c, --chunksize, IO chunk size in MB (default=1)\n"
149 "\t-s, --filesize, file size in MB (default=4096)\n"
150 "\t-h, --help, display this help and exit\n");
156 * open_file: Opens file in specified mode and returns fd.
158 static int open_file(const char *file, int flag)
160 int fd = open(file, flag, perms);
162 fprintf(stderr, "\n%s: Open '%s' failed:%s\n",
163 progname, file, strerror(errno));
169 * Verify_chunk: Verifies test pattern in each 4kB (BLOCKSIZE) is correct.
170 * Returns 0 if test offset and timestamp is correct otherwise 1.
172 int verify_chunk(char *chunk_buf, const size_t chunksize,
173 unsigned long long chunk_off, const unsigned long long time_st,
174 const unsigned long long inode_st, const char *file)
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 if ((bd->bd_offset == chunk_off) && (bd->bd_time == time_st) &&
184 (bd->bd_inode == inode_st))
186 fprintf(stderr, "\n%s: verify %s failed offset/timestamp/inode "
187 "%llu/%llu/%llu: found %llu/%llu/%llu instead\n",
188 progname, file, chunk_off, time_st, inode_st,
189 bd->bd_offset, bd->bd_time, bd->bd_inode);
196 * fill_chunk: Fills the chunk with current or user specified timestamp
197 * and offset. The test pattern is filled at the beginning of
198 * each 4kB(BLOCKSIZE) blocks in chunk_buf.
200 void fill_chunk(char *chunk_buf, size_t chunksize, loff_t chunk_off,
201 const time_t time_st, const ino_t inode_st)
203 struct block_data *bd;
206 for (chunk_end = chunk_buf + chunksize - sizeof(*bd);
207 (char *)chunk_buf < chunk_end;
208 chunk_buf += BLOCKSIZE, chunk_off += BLOCKSIZE) {
209 bd = (struct block_data *)chunk_buf;
210 bd->bd_offset = chunk_off;
211 bd->bd_time = time_st;
212 bd->bd_inode = inode_st;
217 * Write a chunk to disk, handling errors, interrupted writes, etc.
219 * If there is an IO error hit during the write, it is possible that
220 * this will just show up as a short write, and a subsequent write
221 * will return the actual error. We want to continue in the face of
222 * minor media errors so that we can validate the whole device if
223 * possible, but if there are many errors we don't want to loop forever.
225 * The error count will be returned upon exit to ensure that the
226 * media errors are detected even if nobody is looking at the output.
228 * Returns 0 on success, or -ve errno on failure.
230 int write_retry(int fd, const char *chunk_buf, size_t nrequested,
231 unsigned long long offset, const char *file)
236 nwritten = write(fd, chunk_buf, nrequested);
238 if (errno != ENOSPC) {
239 fprintf(stderr, "\n%s: write %s@%llu+%zi failed: %s\n",
240 progname, file, offset, nrequested,
242 if (error_count++ < 100)
247 if (nwritten < nrequested) {
248 fprintf(stderr, "\n%s: write %s@%llu+%zi short: %ld written\n",
249 progname, file, offset, nrequested, nwritten);
251 chunk_buf += nwritten;
252 nrequested -= nwritten;
260 * write_chunks: write the chunk_buf on the device. The number of write
261 * operations are based on the parameters write_end, offset, and chunksize.
263 * Returns 0 on success, or -ve error number on failure.
265 int write_chunks(int fd, unsigned long long offset,unsigned long long write_end,
266 char *chunk_buf, size_t chunksize, const time_t time_st,
267 const ino_t inode_st, const char *file)
269 unsigned long long stride;
271 stride = full ? chunksize : (ONE_GB - chunksize);
272 for (offset = offset & ~(chunksize - 1); offset < write_end;
276 if (stride != chunksize && lseek64(fd, offset, SEEK_SET) < 0) {
277 fprintf(stderr, "\n%s: lseek66(%s+%llu) failed: %s\n",
278 progname, file, offset, strerror(errno));
281 if (offset + chunksize > write_end)
282 chunksize = write_end - offset;
283 if (!full && offset > chunksize) {
284 fill_chunk(chunk_buf, chunksize, offset, time_st,
286 ret = write_retry(fd, chunk_buf, chunksize,offset,file);
290 if (offset + chunksize > write_end)
291 chunksize = write_end - offset;
293 fill_chunk(chunk_buf, chunksize, offset, time_st, inode_st);
294 ret = write_retry(fd, chunk_buf, chunksize, offset, file);
302 * read_chunk: reads the chunk_buf from the device. The number of read
303 * operations are based on the parameters read_end, offset, and chunksize.
305 int read_chunks(int fd, unsigned long long offset, unsigned long long read_end,
306 char *chunk_buf, size_t chunksize, const time_t time_st,
307 const ino_t inode_st, const char *file)
309 unsigned long long stride;
311 stride = full ? chunksize : (ONE_GB - chunksize);
312 for (offset = offset & ~(chunksize - 1); offset < read_end;
316 if (stride != chunksize && lseek64(fd, offset, SEEK_SET) < 0) {
317 fprintf(stderr, "\n%s: lseek64(%s+%llu) failed: %s\n",
318 progname, file, offset, strerror(errno));
321 if (offset + chunksize > read_end)
322 chunksize = read_end - offset;
324 if (!full && offset > chunksize) {
325 nread = read(fd, chunk_buf, chunksize);
327 fprintf(stderr,"\n%s: read %s@%llu+%zi failed: "
328 "%s\n", progname, file, offset,
329 chunksize, strerror(errno));
333 if (nread < chunksize) {
334 fprintf(stderr, "\n%s: read %s@%llu+%zi short: "
335 "%zi read\n", progname, file, offset,
339 if (verify_chunk(chunk_buf, nread, offset, time_st,
340 inode_st, file) != 0) {
345 /* Need to reset position after read error */
346 if (nread < chunksize &&
347 lseek64(fd, offset, SEEK_SET) == -1) {
349 "\n%s: lseek64(%s@%llu) failed: %s\n",
350 progname, file, offset,strerror(errno));
353 if (offset + chunksize >= read_end)
354 chunksize = read_end - offset;
356 nread = read(fd, chunk_buf, chunksize);
358 fprintf(stderr, "\n%s: read %s@%llu+%zi failed: %s\n",
359 progname, file, offset, chunksize,
364 if (nread < chunksize) {
365 fprintf(stderr, "\n%s: read %s@%llu+%zi short: "
366 "%zi read\n", progname, file, offset,
371 if (verify_chunk(chunk_buf, nread, offset, time_st,
372 inode_st, file) != 0) {
380 * new_file: prepares new filename using file counter and current dir.
382 char *new_file(char *tempfile, char *cur_dir, int file_num)
384 snprintf(tempfile, PATH_MAX, "%s/file%03d", cur_dir, file_num);
389 * new_dir: prepares new dir name using dir counters.
391 char *new_dir(char *tempdir, int dir_num)
393 snprintf(tempdir, PATH_MAX, "%s/llverfs_dir%05d", testdir, dir_num);
398 * calc_total_bytes: calculates total bytes that need to be
399 * written into or read from the filesystem.
401 static unsigned long long calc_total_bytes(const char *op)
403 unsigned long long total_bytes = 0;
404 struct statfs64 statbuf;
407 if (statfs64(testdir, &statbuf) == 0) {
408 if (strcmp(op, "write") == 0)
409 total_bytes = (unsigned long long)
410 (statbuf.f_bavail * statbuf.f_bsize);
411 else if (strcmp(op, "read") == 0)
412 total_bytes = (unsigned long long)
413 (statbuf.f_blocks * statbuf.f_bsize);
415 fprintf(stderr, "\n%s: invalid operation: %s\n",
420 fprintf(stderr, "\n%s: unable to stat %s: %s\n",
421 progname, testdir, strerror(errno));
425 total_bytes = num_dirs * files_in_dir * file_size;
432 * show_rate: displays the current read/write file name and performance,
433 * along with an estimate of how long the whole read/write operation
436 void show_rate(char *op, char *filename, const struct timeval *start_time,
437 const unsigned long long total_bytes,
438 const unsigned long long curr_bytes)
440 static struct timeval last_time;
441 static unsigned long long last_bytes;
443 struct timeval curr_time;
444 double curr_delta, overall_delta, curr_rate, overall_rate;
446 int remain_hours, remain_minutes, remain_seconds;
448 if (last_op != op[0]) {
450 last_time = *start_time;
454 gettimeofday(&curr_time, NULL);
456 curr_delta = (curr_time.tv_sec - last_time.tv_sec) +
457 (double)(curr_time.tv_usec - last_time.tv_usec) / 1000000;
459 overall_delta = (curr_time.tv_sec - start_time->tv_sec) +
460 (double)(curr_time.tv_usec - start_time->tv_usec) / 1000000;
462 curr_rate = (curr_bytes - last_bytes) / curr_delta;
463 overall_rate = curr_bytes / overall_delta;
465 if (curr_rate == 0) {
466 last_time = curr_time;
469 remain_time = (total_bytes - curr_bytes) / curr_rate;
471 remain_hours = remain_time / 3600;
472 remain_minutes = (remain_time - remain_hours * 3600) / 60;
473 remain_seconds = (remain_time - remain_hours * 3600 -
474 remain_minutes * 60);
476 if (curr_delta > 4 || verbose > 2) {
480 printf("%s: %s, current: %5g MB/s, overall: %5g MB/s, "
481 "ETA: %u:%02u:%02u", op, filename,
482 curr_rate / ONE_MB, overall_rate / ONE_MB,
483 remain_hours, remain_minutes, remain_seconds);
490 last_time = curr_time;
491 last_bytes = curr_bytes;
496 * dir_write: This function writes directories and files on device.
497 * it works for both full and partial modes.
499 static int dir_write(char *chunk_buf, size_t chunksize,
500 time_t time_st, unsigned long dir_num)
502 char tempfile[PATH_MAX];
503 char tempdir[PATH_MAX];
506 int file_num = 999999999;
508 struct timeval start_time;
509 unsigned long long total_bytes;
510 unsigned long long curr_bytes = 0;
513 if (!full && fsetflags(testdir, EXT2_TOPDIR_FL))
515 "\n%s: can't set TOPDIR_FL on %s: %s (ignoring)",
516 progname, testdir, strerror(errno));
518 countfile = fopen(filecount, "w");
519 if (countfile == NULL) {
520 fprintf(stderr, "\n%s: creating %s failed :%s\n",
521 progname, filecount, strerror(errno));
524 /* reserve space for the countfile */
525 if (fprintf(countfile, "%lu", num_files) < 1 ||
526 fflush(countfile) != 0) {
527 fprintf(stderr, "\n%s: writing %s failed :%s\n",
528 progname, filecount, strerror(errno));
533 /* calculate total bytes that need to be written */
534 total_bytes = calc_total_bytes("write");
535 if (total_bytes <= 0) {
536 fprintf(stderr, "\n%s: unable to calculate total bytes\n",
542 if (!full && (dir_num != 0))
543 total_bytes -= dir_num * files_in_dir * file_size;
545 gettimeofday(&start_time, NULL);
546 for (; dir_num < num_dirs; num_files++, file_num++) {
549 if (file_num >= files_in_dir) {
551 if (mkdir(new_dir(tempdir, dir_num), dirmode) < 0) {
554 if (errno != EEXIST) {
555 fprintf(stderr, "\n%s: mkdir %s : %s\n",
565 fd = open_file(new_file(tempfile, tempdir, file_num),
566 O_WRONLY | O_CREAT | O_TRUNC | O_LARGEFILE);
568 if (fstat64(fd, &file) == 0) {
569 inode_st = file.st_ino;
571 fprintf(stderr, "\n%s: write stat '%s': %s",
572 progname, tempfile, strerror(errno));
580 ret = write_chunks(fd, 0, file_size, chunk_buf, chunksize,
581 time_st, inode_st, tempfile);
584 if (ret != -ENOSPC) {
588 curr_bytes = total_bytes;
592 curr_bytes += file_size;
594 show_rate("write", tempfile, &start_time,
595 total_bytes, curr_bytes);
597 fseek(countfile, 0, SEEK_SET);
598 if (fprintf(countfile, "%lu", num_files) < 1 ||
599 fflush(countfile) != 0) {
600 fprintf(stderr, "\n%s: writing %s failed :%s\n",
601 progname, filecount, strerror(errno));
606 show_rate("write_done", tempfile, &start_time, total_bytes, curr_bytes);
617 * dir_read: This function reads directories and files on device.
618 * it works for both full and partial modes.
620 static int dir_read(char *chunk_buf, size_t chunksize,
621 time_t time_st, unsigned long dir_num)
623 char tempfile[PATH_MAX];
624 char tempdir[PATH_MAX];
625 unsigned long count = 0;
629 struct timeval start_time;
630 unsigned long long total_bytes;
631 unsigned long long curr_bytes = 0;
633 /* calculate total bytes that need to be read */
634 total_bytes = calc_total_bytes("read");
635 if (total_bytes <= 0) {
636 fprintf(stderr, "\n%s: unable to calculate total bytes\n",
642 total_bytes -= dir_num * files_in_dir * file_size;
644 gettimeofday(&start_time, NULL);
645 for (count = 0; count < num_files && dir_num < num_dirs; count++) {
649 new_dir(tempdir, dir_num);
653 fd = open_file(new_file(tempfile, tempdir, file_num),
654 O_RDONLY | O_LARGEFILE);
656 if (fstat64(fd, &file) == 0) {
657 inode_st = file.st_ino;
659 fprintf(stderr, "\n%s: read stat '%s': %s\n",
660 progname, tempfile, strerror(errno));
668 if (count == num_files)
669 file_size = file.st_size;
670 ret = read_chunks(fd, 0, file_size, chunk_buf, chunksize,
671 time_st, inode_st, tempfile);
676 curr_bytes += file_size;
678 show_rate("read", tempfile, &start_time,
679 total_bytes, curr_bytes);
681 if (++file_num >= files_in_dir)
685 show_rate("read_done", tempfile, &start_time, total_bytes, curr_bytes);
692 int main(int argc, char **argv)
694 time_t time_st = 0; /* Default timestamp */
695 size_t chunksize = ONE_MB; /* IO chunk size(defailt=1MB) */
696 char *chunk_buf; /* chunk buffer */
698 FILE *countfile = NULL;
699 unsigned long dir_num = 0, dir_num_orig = 0;/* starting directory */
702 progname = strrchr(argv[0], '/') ? strrchr(argv[0], '/') + 1 : argv[0];
703 while ((c = getopt_long(argc, argv, "c:hln:o:pqrs:t:vw",
704 long_opts, NULL)) != -1) {
707 chunksize = strtoul(optarg, NULL, 0) * ONE_MB;
708 if (chunksize == 0) {
709 fprintf(stderr, "%s: bad chunk size '%s'\n",
718 num_dirs = strtoul(optarg, NULL, 0);
720 case 'o': /* offset */
721 dir_num = strtoul(optarg, NULL, 0);
736 file_size = strtoul(optarg, NULL, 0) * ONE_MB;
737 if (file_size == 0) {
738 fprintf(stderr, "%s: bad file size '%s'\n",
744 time_st = (time_t)strtoul(optarg, NULL, 0);
759 testdir = argv[optind];
762 fprintf(stderr, "%s: pathname not given\n", progname);
766 if (!readoption && !writeoption) {
771 (void) time(&time_st);
772 printf("Timestamp: %lu\n", (unsigned long )time_st);
773 isatty_flag = isatty(STDOUT_FILENO);
775 if (!full && !num_dirs) {
776 #ifdef HAVE_EXT2FS_EXT2FS_H
777 struct mntent *tempmnt;
781 if ((fp = setmntent("/etc/mtab", "r")) == NULL) {
782 fprintf(stderr, "%s: fail to open /etc/mtab in read mode :%s\n",
783 progname, strerror(errno));
787 /* find device name using filesystem */
788 while ((tempmnt = getmntent(fp)) != NULL) {
789 if (strcmp(tempmnt->mnt_dir, testdir) == 0)
793 if (tempmnt == NULL) {
794 fprintf(stderr, "%s: no device found for '%s'\n",
800 if (ext2fs_open(tempmnt->mnt_fsname, 0, 0, 0,
801 unix_io_manager, &fs)) {
802 fprintf(stderr, "%s: unable to open ext3 fs on '%s'\n",
809 num_dirs = (fs->super->s_blocks_count +
810 fs->super->s_blocks_per_group - 1) /
811 fs->super->s_blocks_per_group;
813 printf("ext3 block groups: %u, fs blocks: %u "
814 "blocks per group: %u\n",
815 num_dirs, fs->super->s_blocks_count,
816 fs->super->s_blocks_per_group);
823 struct statfs64 statbuf;
826 * Most extN filesystems are formatted with 128MB/group
827 * (32k bitmap = 4KB blocksize * 8 bits/block) * 4KB,
828 * so this is a relatively safe default (somewhat more
829 * or less doesn't make a huge difference for testing).
831 * We want to create one directory per group, together
832 * with the "TOPDIR" feature, so that the directories
833 * are spread across the whole block device.
835 if (statfs64(testdir, &statbuf) == 0) {
836 num_dirs = 1 + (long long)statbuf.f_blocks *
838 (full ? files_in_dir * file_size : 128*ONE_MB);
840 printf("dirs: %u, fs blocks: %llu\n",
841 num_dirs, (long long)statbuf.f_blocks);
843 fprintf(stderr, "%s: unable to stat '%s': %s\n",
844 progname, testdir, strerror(errno));
848 printf("dirs: %u\n", num_dirs);
851 chunk_buf = (char *)calloc(chunksize, 1);
852 if (chunk_buf == NULL) {
853 fprintf(stderr, "Memory allocation failed for chunk_buf\n");
856 snprintf(filecount, sizeof(filecount), "%s/%s.filecount",
859 (void)mkdir(testdir, dirmode);
863 num_files = dir_num * files_in_dir;
865 printf("\n%s: %lu files already written\n",
866 progname, num_files);
868 if (dir_write(chunk_buf, chunksize, time_st, dir_num)) {
872 dir_num = dir_num_orig;
876 countfile = fopen(filecount, "r");
877 if (countfile == NULL ||
878 fscanf(countfile, "%lu", &num_files) != 1 ||
880 fprintf(stderr, "\n%s: reading %s failed :%s\n",
881 progname, filecount, strerror(errno));
882 num_files = num_dirs * files_in_dir;
884 num_files -= (dir_num * files_in_dir);
889 if (dir_read(chunk_buf, chunksize, time_st, dir_num)) {
890 fprintf(stderr, "\n%s: Data verification failed\n",