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 longopts[] =
125 { "chunksize", required_argument, 0, 'c' },
126 { "help", no_argument, 0, 'h' },
127 { "offset", required_argument, 0, 'o' },
128 { "long", no_argument, 0, 'l' },
129 { "full", no_argument, 0, 'l' },
130 { "partial", required_argument, 0, 'p' },
131 { "quiet", required_argument, 0, 'q' },
132 { "read", no_argument, 0, 'r' },
133 { "filesize", no_argument, 0, 's' },
134 { "timestamp", required_argument, 0, 't' },
135 { "verbose", no_argument, 0, 'v' },
136 { "write", no_argument, 0, 'w' },
141 * Usages: displays help information, whenever user supply --help option in
142 * command or enters incorrect command line.
144 void usage(int status)
147 printf("\nUsage: %s [OPTION]... <filesystem path> ...\n",
149 printf("Filesystem verification tool.\n"
150 "\t-t {seconds}, --timestamp, set test time"
151 "(default=current time())\n"
152 "\t-o {offset}, --offset, directory starting offset"
153 " from which tests should start\n"
154 "\t-r, --read, run in verify mode\n"
155 "\t-w, --write, run in test-pattern mode, default=rw\n"
157 "\t-p, --partial, for partial check (1MB files)\n"
158 "\t-l, --long, --full check (4GB file with 4k blocks)\n"
159 "\t-c, --chunksize, IO chunk size in MB (default=1)\n"
160 "\t-s, --filesize, file size in MB (default=4096)\n"
161 "\t-h, --help, display this help and exit\n");
167 * open_file: Opens file in specified mode and returns fd.
169 static int open_file(const char *file, int flag)
171 int fd = open(file, flag, perms);
173 fprintf(stderr, "\n%s: Open '%s' failed:%s\n",
174 progname, file, strerror(errno));
180 * Verify_chunk: Verifies test pattern in each 4kB (BLOCKSIZE) is correct.
181 * Returns 0 if test offset and timestamp is correct otherwise 1.
183 int verify_chunk(char *chunk_buf, const size_t chunksize,
184 unsigned long long chunk_off, const unsigned long long time_st,
185 const unsigned long long inode_st, const char *file)
187 struct block_data *bd;
190 for (chunk_end = chunk_buf + chunksize - sizeof(*bd);
191 (char *)chunk_buf < chunk_end;
192 chunk_buf += BLOCKSIZE, chunk_off += BLOCKSIZE) {
193 bd = (struct block_data *)chunk_buf;
194 if ((bd->bd_offset == chunk_off) && (bd->bd_time == time_st) &&
195 (bd->bd_inode == inode_st))
197 fprintf(stderr, "\n%s: verify %s failed offset/timestamp/inode "
198 "%llu/%llu/%llu: found %llu/%llu/%llu instead\n",
199 progname, file, chunk_off, time_st, inode_st,
200 bd->bd_offset, bd->bd_time, bd->bd_inode);
207 * fill_chunk: Fills the chunk with current or user specified timestamp
208 * and offset. The test pattern is filled at the beginning of
209 * each 4kB(BLOCKSIZE) blocks in chunk_buf.
211 void fill_chunk(char *chunk_buf, size_t chunksize, loff_t chunk_off,
212 const time_t time_st, const ino_t inode_st)
214 struct block_data *bd;
217 for (chunk_end = chunk_buf + chunksize - sizeof(*bd);
218 (char *)chunk_buf < chunk_end;
219 chunk_buf += BLOCKSIZE, chunk_off += BLOCKSIZE) {
220 bd = (struct block_data *)chunk_buf;
221 bd->bd_offset = chunk_off;
222 bd->bd_time = time_st;
223 bd->bd_inode = inode_st;
228 * Write a chunk to disk, handling errors, interrupted writes, etc.
230 * If there is an IO error hit during the write, it is possible that
231 * this will just show up as a short write, and a subsequent write
232 * will return the actual error. We want to continue in the face of
233 * minor media errors so that we can validate the whole device if
234 * possible, but if there are many errors we don't want to loop forever.
236 * The error count will be returned upon exit to ensure that the
237 * media errors are detected even if nobody is looking at the output.
239 * Returns 0 on success, or -ve errno on failure.
241 int write_retry(int fd, const char *chunk_buf, size_t nrequested,
242 unsigned long long offset, const char *file)
247 nwritten = write(fd, chunk_buf, nrequested);
249 if (errno != ENOSPC) {
250 fprintf(stderr, "\n%s: write %s@%llu+%zi failed: %s\n",
251 progname, file, offset, nrequested,
253 if (error_count++ < 100)
258 if (nwritten < nrequested) {
259 fprintf(stderr, "\n%s: write %s@%llu+%zi short: %ld written\n",
260 progname, file, offset, nrequested, nwritten);
262 chunk_buf += nwritten;
263 nrequested -= nwritten;
271 * write_chunks: write the chunk_buf on the device. The number of write
272 * operations are based on the parameters write_end, offset, and chunksize.
274 * Returns 0 on success, or -ve error number on failure.
276 int write_chunks(int fd, unsigned long long offset,unsigned long long write_end,
277 char *chunk_buf, size_t chunksize, const time_t time_st,
278 const ino_t inode_st, const char *file)
280 unsigned long long stride;
282 stride = full ? chunksize : (ONE_GB - chunksize);
283 for (offset = offset & ~(chunksize - 1); offset < write_end;
287 if (lseek64(fd, offset, SEEK_SET) == -1) {
288 fprintf(stderr, "\n%s: lseek64(%s+%llu) failed: %s\n",
289 progname, file, offset, strerror(errno));
292 if (offset + chunksize > write_end)
293 chunksize = write_end - offset;
294 if (!full && offset > chunksize) {
295 fill_chunk(chunk_buf, chunksize, offset, time_st,
297 ret = write_retry(fd, chunk_buf, chunksize,offset,file);
301 if (offset + chunksize > write_end)
302 chunksize = write_end - offset;
304 fill_chunk(chunk_buf, chunksize, offset, time_st, inode_st);
305 ret = write_retry(fd, chunk_buf, chunksize, offset, file);
313 * read_chunk: reads the chunk_buf from the device. The number of read
314 * operations are based on the parameters read_end, offset, and chunksize.
316 int read_chunks(int fd, unsigned long long offset, unsigned long long read_end,
317 char *chunk_buf, size_t chunksize, const time_t time_st,
318 const ino_t inode_st, const char *file)
320 unsigned long long stride;
322 stride = full ? chunksize : (ONE_GB - chunksize);
323 for (offset = offset & ~(chunksize - 1); offset < read_end;
327 if (lseek64(fd, offset, SEEK_SET) == -1) {
328 fprintf(stderr, "\n%s: lseek64(%s+%llu) failed: %s\n",
329 progname, file, offset, strerror(errno));
332 if (offset + chunksize > read_end)
333 chunksize = read_end - offset;
335 if (!full && offset > chunksize) {
336 nread = read(fd, chunk_buf, chunksize);
338 fprintf(stderr,"\n%s: read %s@%llu+%zi failed: "
339 "%s\n", progname, file, offset,
340 chunksize, strerror(errno));
344 if (nread < chunksize) {
345 fprintf(stderr, "\n%s: read %s@%llu+%zi short: "
346 "%zi read\n", progname, file, offset,
350 if (verify_chunk(chunk_buf, nread, offset, time_st,
351 inode_st, file) != 0) {
356 /* Need to reset position after read error */
357 if (nread < chunksize &&
358 lseek64(fd, offset, SEEK_SET) == -1) {
360 "\n%s: lseek64(%s@%llu) failed: %s\n",
361 progname, file, offset,strerror(errno));
364 if (offset + chunksize >= read_end)
365 chunksize = read_end - offset;
367 nread = read(fd, chunk_buf, chunksize);
369 fprintf(stderr, "\n%s: read %s@%llu+%zi failed: %s\n",
370 progname, file, offset, chunksize,
375 if (nread < chunksize) {
376 fprintf(stderr, "\n%s: read %s@%llu+%zi short: "
377 "%zi read\n", progname, file, offset,
382 if (verify_chunk(chunk_buf, nread, offset, time_st,
383 inode_st, file) != 0) {
391 * new_file: prepares new filename using file counter and current dir.
393 char *new_file(char *tempfile, char *cur_dir, int file_num)
395 sprintf(tempfile, "%s/file%03d", cur_dir, file_num);
400 * new_dir: prepares new dir name using dir counters.
402 char *new_dir(char *tempdir, int dir_num)
404 sprintf(tempdir, "%s/dir%05d", testdir, dir_num);
409 * calc_total_bytes: calculates total bytes that need to be
410 * written into or read from the filesystem.
412 static unsigned long long calc_total_bytes(const char *op)
414 unsigned long long total_bytes = 0;
415 struct statfs64 statbuf;
418 if (statfs64(testdir, &statbuf) == 0) {
419 if (strcmp(op, "write") == 0)
420 total_bytes = (unsigned long long)
421 (statbuf.f_bavail * statbuf.f_bsize);
422 else if (strcmp(op, "read") == 0)
423 total_bytes = (unsigned long long)
424 (statbuf.f_blocks * statbuf.f_bsize);
426 fprintf(stderr, "\n%s: invalid operation: %s\n",
431 fprintf(stderr, "\n%s: unable to stat %s: %s\n",
432 progname, testdir, strerror(errno));
436 total_bytes = num_dirs * files_in_dir * file_size;
443 * show_rate: displays the current read/write file name and performance,
444 * along with an estimate of how long the whole read/write operation
447 void show_rate(char *op, char *filename, const struct timeval *start_time,
448 const unsigned long long total_bytes,
449 const unsigned long long curr_bytes)
451 static struct timeval last_time;
452 static unsigned long long last_bytes;
454 struct timeval curr_time;
455 double curr_delta, overall_delta, curr_rate, overall_rate;
456 double total_time, remain_time;
457 int remain_hours, remain_minutes, remain_seconds;
459 if (last_time.tv_sec == 0)
460 last_time = *start_time;
462 if (last_op != op[0]) {
464 last_time = *start_time;
468 gettimeofday(&curr_time, NULL);
470 curr_delta = (curr_time.tv_sec - last_time.tv_sec) +
471 (double)(curr_time.tv_usec - last_time.tv_usec) / 1000000;
473 overall_delta = (curr_time.tv_sec - start_time->tv_sec) +
474 (double)(curr_time.tv_usec - start_time->tv_usec) / 1000000;
476 curr_rate = (curr_bytes - last_bytes) / curr_delta;
477 overall_rate = curr_bytes / overall_delta;
479 if (curr_rate == 0) {
480 last_time = curr_time;
483 total_time = total_bytes / curr_rate;
484 remain_time = total_time - overall_delta;
486 remain_hours = remain_time / 3600;
487 remain_minutes = (remain_time - remain_hours * 3600) / 60;
488 remain_seconds = (remain_time - remain_hours * 3600 -
489 remain_minutes * 60);
491 if (curr_delta > 4 || verbose > 2) {
495 printf("%s filename: %s, current %5g MB/s, overall %5g MB/s, "
496 "est %u:%u:%u left", op, filename,
497 curr_rate / ONE_MB, overall_rate / ONE_MB,
498 remain_hours, remain_minutes, remain_seconds);
506 last_time = curr_time;
507 last_bytes = curr_bytes;
511 * dir_write: This function writes directories and files on device.
512 * it works for both full and partial modes.
514 static int dir_write(char *chunk_buf, size_t chunksize,
515 time_t time_st, unsigned long dir_num)
517 char tempfile[PATH_MAX];
518 char tempdir[PATH_MAX];
521 int file_num = 999999999;
523 struct timeval start_time;
524 unsigned long long total_bytes;
525 unsigned long long curr_bytes = 0;
528 #ifdef HAVE_EXT2FS_EXT2FS_H
529 if (!full && fsetflags(testdir, EXT2_TOPDIR_FL))
531 "\n%s: can't set TOPDIR_FL on %s: %s (ignoring)",
532 progname, testdir, strerror(errno));
534 countfile = fopen(filecount, "w");
535 if (countfile == NULL) {
536 fprintf(stderr, "\n%s: creating %s failed :%s\n",
537 progname, filecount, strerror(errno));
540 /* reserve space for the countfile */
541 if (fprintf(countfile, "%lu", num_files) < 1 ||
542 fflush(countfile) != 0) {
543 fprintf(stderr, "\n%s: writing %s failed :%s\n",
544 progname, filecount, strerror(errno));
549 /* calculate total bytes that need to be written */
550 total_bytes = calc_total_bytes("write");
551 if (total_bytes <= 0) {
552 fprintf(stderr, "\n%s: unable to calculate total bytes\n",
558 if (!full && (dir_num != 0))
559 total_bytes -= dir_num * files_in_dir * file_size;
561 gettimeofday(&start_time, NULL);
562 for (; dir_num < num_dirs; num_files++, file_num++) {
565 if (file_num >= files_in_dir) {
566 if (dir_num == num_dirs - 1)
570 if (mkdir(new_dir(tempdir, dir_num), dirmode) < 0) {
573 if (errno != EEXIST) {
574 fprintf(stderr, "\n%s: mkdir %s : %s\n",
584 fd = open_file(new_file(tempfile, tempdir, file_num),
585 O_WRONLY | O_CREAT | O_TRUNC | O_LARGEFILE);
587 if (fstat64(fd, &file) == 0) {
588 inode_st = file.st_ino;
590 fprintf(stderr, "\n%s: write stat '%s': %s",
591 progname, tempfile, strerror(errno));
599 ret = write_chunks(fd, 0, file_size, chunk_buf, chunksize,
600 time_st, inode_st, tempfile);
603 if (ret != -ENOSPC) {
607 curr_bytes = total_bytes;
611 curr_bytes += file_size;
613 show_rate("write", tempfile, &start_time,
614 total_bytes, curr_bytes);
616 fseek(countfile, 0, SEEK_SET);
617 if (fprintf(countfile, "%lu", num_files) < 1 ||
618 fflush(countfile) != 0) {
619 fprintf(stderr, "\n%s: writing %s failed :%s\n",
620 progname, filecount, strerror(errno));
626 show_rate("write", tempfile, &start_time,
627 total_bytes, curr_bytes);
628 printf("\nwrite complete\n");
639 * dir_read: This function reads directories and files on device.
640 * it works for both full and partial modes.
642 static int dir_read(char *chunk_buf, size_t chunksize,
643 time_t time_st, unsigned long dir_num)
645 char tempfile[PATH_MAX];
646 char tempdir[PATH_MAX];
647 unsigned long count = 0;
651 struct timeval start_time;
652 unsigned long long total_bytes;
653 unsigned long long curr_bytes = 0;
655 /* calculate total bytes that need to be read */
656 total_bytes = calc_total_bytes("read");
657 if (total_bytes <= 0) {
658 fprintf(stderr, "\n%s: unable to calculate total bytes\n",
664 total_bytes -= dir_num * files_in_dir * file_size;
666 gettimeofday(&start_time, NULL);
667 for (count = 0; count < num_files && dir_num < num_dirs; count++) {
671 if (dir_num == num_dirs - 1)
674 new_dir(tempdir, dir_num);
678 fd = open_file(new_file(tempfile, tempdir, file_num),
679 O_RDONLY | O_LARGEFILE);
681 if (fstat64(fd, &file) == 0) {
682 inode_st = file.st_ino;
684 fprintf(stderr, "\n%s: read stat '%s': %s\n",
685 progname, tempfile, strerror(errno));
693 if (count == num_files)
694 file_size = file.st_size;
695 ret = read_chunks(fd, 0, file_size, chunk_buf, chunksize,
696 time_st, inode_st, tempfile);
701 curr_bytes += file_size;
703 show_rate("read", tempfile, &start_time,
704 total_bytes, curr_bytes);
706 if (++file_num >= files_in_dir)
711 show_rate("read", tempfile, &start_time,
712 total_bytes, curr_bytes);
713 printf("\nread complete\n");
719 int main(int argc, char **argv)
721 time_t time_st = 0; /* Default timestamp */
722 size_t chunksize = ONE_MB; /* IO chunk size(defailt=1MB) */
723 char *chunk_buf; /* chunk buffer */
725 FILE *countfile = NULL;
726 unsigned long dir_num = 0, dir_num_orig = 0;/* starting directory */
729 progname = strrchr(argv[0], '/') ? strrchr(argv[0], '/') + 1 : argv[0];
730 while ((c = getopt_long(argc, argv, "c:hlo:pqrs:t:vw",
731 longopts, NULL)) != -1) {
734 chunksize = strtoul(optarg, NULL, 0) * ONE_MB;
735 if (chunksize == 0) {
736 fprintf(stderr, "%s: bad chunk size '%s'\n",
744 case 'o': /* offset */
745 dir_num = strtoul(optarg, NULL, 0);
760 file_size = strtoul(optarg, NULL, 0) * ONE_MB;
761 if (file_size == 0) {
762 fprintf(stderr, "%s: bad file size '%s'\n",
768 time_st = (time_t)strtoul(optarg, NULL, 0);
783 testdir = argv[optind];
786 fprintf(stderr, "%s: pathname not given\n", progname);
790 if (!readoption && !writeoption) {
795 (void) time(&time_st);
796 printf("Timestamp: %lu\n", (unsigned long )time_st);
797 isatty_flag = isatty(STDOUT_FILENO);
800 #ifdef HAVE_EXT2FS_EXT2FS_H
801 struct mntent *tempmnt;
805 if ((fp = setmntent("/etc/mtab", "r")) == NULL){
806 fprintf(stderr, "%s: fail to open /etc/mtab in read"
807 "mode :%s\n", progname, strerror(errno));
811 /* find device name using filesystem */
812 while ((tempmnt = getmntent(fp)) != NULL) {
813 if (strcmp(tempmnt->mnt_dir, testdir) == 0)
817 if (tempmnt == NULL) {
818 fprintf(stderr, "%s: no device found for '%s'\n",
824 if (ext2fs_open(tempmnt->mnt_fsname, 0, 0, 0,
825 unix_io_manager, &fs)) {
826 fprintf(stderr, "%s: unable to open ext3 fs on '%s'\n",
833 num_dirs = (fs->super->s_blocks_count +
834 fs->super->s_blocks_per_group - 1) /
835 fs->super->s_blocks_per_group;
837 printf("ext3 block groups: %u, fs blocks: %u "
838 "blocks per group: %u\n",
839 num_dirs, fs->super->s_blocks_count,
840 fs->super->s_blocks_per_group);
846 struct statfs64 statbuf;
848 if (statfs64(testdir, &statbuf) == 0) {
849 num_dirs = (long long)statbuf.f_blocks *
850 statbuf.f_bsize / (128ULL << 20);
852 printf("dirs: %u, fs blocks: %llu\n",
854 (long long)statbuf.f_blocks);
856 fprintf(stderr, "%s: unable to stat '%s': %s\n",
857 progname, testdir, strerror(errno));
859 printf("dirs: %u\n", num_dirs);
863 chunk_buf = (char *)calloc(chunksize, 1);
864 if (chunk_buf == NULL) {
865 fprintf(stderr, "Memory allocation failed for chunk_buf\n");
868 snprintf(filecount, sizeof(filecount), "%s/%s.filecount",
871 (void)mkdir(testdir, dirmode);
875 num_files = dir_num * files_in_dir;
877 printf("\n%s: %lu files already written\n",
878 progname, num_files);
880 if (dir_write(chunk_buf, chunksize, time_st, dir_num)) {
884 dir_num = dir_num_orig;
888 countfile = fopen(filecount, "r");
889 if (countfile == NULL ||
890 fscanf(countfile, "%lu", &num_files) != 1 ||
892 fprintf(stderr, "\n%s: reading %s failed :%s\n",
893 progname, filecount, strerror(errno));
894 num_files = num_dirs * files_in_dir;
896 num_files -= (dir_num * files_in_dir);
901 if (dir_read(chunk_buf, chunksize, time_st, dir_num)) {
902 fprintf(stderr, "\n%s: Data verification failed\n",