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.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, Whamcloud, Inc.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/utils/llverfs.c
38 * Filesystem Verification Tool.
39 * This program tests the correct operation of large filesystems and
40 * the underlying block storage device(s).
41 * This tool have two working modes
45 * In full mode, the program creates a subdirectory in the test
46 * filesystem, writes n(files_in_dir, default=32) large(4GB) files to
47 * the directory with the test pattern at the start of each 4kb block.
48 * The test pattern contains timestamp, relative file offset and per
49 * file unique identifier(inode number). This continues until the
50 * whole filesystem is full and then the tool verifies that the data
51 * in all of the test files is correct.
53 * In partial mode, the tool creates test directories with the
54 * EXT3_TOPDIR_FL flag set (if supported) to spread the directory data
55 * around the block device instead of localizing it in a single place.
56 * The number of directories equals to the number of block groups in the
57 * filesystem (e.g. 65536 directories for 8TB ext3/ext4 filesystem) and
58 * then writes a single 1MB file in each directory. The tool then verifies
59 * that the data in each file is correct.
68 #ifndef _LARGEFILE64_SOURCE
69 #define _LARGEFILE64_SOURCE
71 #ifndef _FILE_OFFSET_BITS
72 #define _FILE_OFFSET_BITS 64
89 #include <sys/types.h>
93 #include <gnu/stubs.h>
94 #include <gnu/stubs.h>
96 #ifdef HAVE_EXT2FS_EXT2FS_H
98 # include <ext2fs/ext2fs.h>
101 #define ONE_MB (1024 * 1024)
102 #define ONE_GB ((unsigned long long)(1024 * 1024 * 1024))
103 #define BLOCKSIZE 4096
105 /* Structure for writing test pattern */
107 unsigned long long bd_offset;
108 unsigned long long bd_time;
109 unsigned long long bd_inode;
111 static char *progname; /* name by which this program was run. */
112 static unsigned verbose = 1; /* prints offset in kB, operation rate */
113 static int readoption; /* run test in read-only (verify) mode */
114 static int writeoption; /* run test in write_only mode */
115 char *testdir; /* name of device to be tested. */
116 static unsigned full = 1; /* flag to full check */
117 static int error_count; /* number of IO errors hit during run */
118 char filecount[PATH_MAX]; /* file with total number of files written*/
119 static unsigned long num_files; /* Total number of files for read/write */
120 static loff_t file_size = 4*ONE_GB; /* Size of each file */
121 static unsigned files_in_dir = 32; /* number of files in each directioy */
122 static unsigned num_dirs = 30000; /* total number of directories */
123 const int dirmode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
124 static int isatty_flag;
125 static int perms = S_IRWXU | S_IRGRP | S_IROTH;
127 static struct option const longopts[] =
129 { "chunksize", required_argument, 0, 'c' },
130 { "help", no_argument, 0, 'h' },
131 { "offset", required_argument, 0, 'o' },
132 { "long", no_argument, 0, 'l' },
133 { "full", no_argument, 0, 'l' },
134 { "partial", required_argument, 0, 'p' },
135 { "quiet", required_argument, 0, 'q' },
136 { "read", no_argument, 0, 'r' },
137 { "filesize", no_argument, 0, 's' },
138 { "timestamp", required_argument, 0, 't' },
139 { "verbose", no_argument, 0, 'v' },
140 { "write", no_argument, 0, 'w' },
145 * Usages: displays help information, whenever user supply --help option in
146 * command or enters incorrect command line.
148 void usage(int status)
151 printf("\nUsage: %s [OPTION]... <filesystem path> ...\n",
153 printf("Filesystem verification tool.\n"
154 "\t-t {seconds}, --timestamp, set test time"
155 "(default=current time())\n"
156 "\t-o {offset}, --offset, directory starting offset"
157 " from which tests should start\n"
158 "\t-r, --read, run in verify mode\n"
159 "\t-w, --write, run in test-pattern mode, default=rw\n"
161 "\t-p, --partial, for partial check (1MB files)\n"
162 "\t-l, --long, --full check (4GB file with 4k blocks)\n"
163 "\t-c, --chunksize, IO chunk size in MB (default=1)\n"
164 "\t-s, --filesize, file size in MB (default=4096)\n"
165 "\t-h, --help, display this help and exit\n");
171 * open_file: Opens file in specified mode and returns fd.
173 static int open_file(const char *file, int flag)
175 int fd = open(file, flag, perms);
177 fprintf(stderr, "\n%s: Open '%s' failed:%s\n",
178 progname, file, strerror(errno));
184 * Verify_chunk: Verifies test pattern in each 4kB (BLOCKSIZE) is correct.
185 * Returns 0 if test offset and timestamp is correct otherwise 1.
187 int verify_chunk(char *chunk_buf, const size_t chunksize,
188 unsigned long long chunk_off, const unsigned long long time_st,
189 const unsigned long long inode_st, const char *file)
191 struct block_data *bd;
194 for (chunk_end = chunk_buf + chunksize - sizeof(*bd);
195 (char *)chunk_buf < chunk_end;
196 chunk_buf += BLOCKSIZE, chunk_off += BLOCKSIZE) {
197 bd = (struct block_data *)chunk_buf;
198 if ((bd->bd_offset == chunk_off) && (bd->bd_time == time_st) &&
199 (bd->bd_inode == inode_st))
201 fprintf(stderr, "\n%s: verify %s failed offset/timestamp/inode "
202 "%llu/%llu/%llu: found %llu/%llu/%llu instead\n",
203 progname, file, chunk_off, time_st, inode_st,
204 bd->bd_offset, bd->bd_time, bd->bd_inode);
211 * fill_chunk: Fills the chunk with current or user specified timestamp
212 * and offset. The test pattern is filled at the beginning of
213 * each 4kB(BLOCKSIZE) blocks in chunk_buf.
215 void fill_chunk(char *chunk_buf, size_t chunksize, loff_t chunk_off,
216 const time_t time_st, const ino_t inode_st)
218 struct block_data *bd;
221 for (chunk_end = chunk_buf + chunksize - sizeof(*bd);
222 (char *)chunk_buf < chunk_end;
223 chunk_buf += BLOCKSIZE, chunk_off += BLOCKSIZE) {
224 bd = (struct block_data *)chunk_buf;
225 bd->bd_offset = chunk_off;
226 bd->bd_time = time_st;
227 bd->bd_inode = inode_st;
232 * Write a chunk to disk, handling errors, interrupted writes, etc.
234 * If there is an IO error hit during the write, it is possible that
235 * this will just show up as a short write, and a subsequent write
236 * will return the actual error. We want to continue in the face of
237 * minor media errors so that we can validate the whole device if
238 * possible, but if there are many errors we don't want to loop forever.
240 * The error count will be returned upon exit to ensure that the
241 * media errors are detected even if nobody is looking at the output.
243 * Returns 0 on success, or -ve errno on failure.
245 int write_retry(int fd, const char *chunk_buf, size_t nrequested,
246 unsigned long long offset, const char *file)
251 nwritten = write(fd, chunk_buf, nrequested);
253 if (errno != ENOSPC) {
254 fprintf(stderr, "\n%s: write %s@%llu+%zi failed: %s\n",
255 progname, file, offset, nrequested,
257 if (error_count++ < 100)
262 if (nwritten < nrequested) {
263 fprintf(stderr, "\n%s: write %s@%llu+%zi short: %ld written\n",
264 progname, file, offset, nrequested, nwritten);
266 chunk_buf += nwritten;
267 nrequested -= nwritten;
275 * write_chunks: write the chunk_buf on the device. The number of write
276 * operations are based on the parameters write_end, offset, and chunksize.
278 * Returns 0 on success, or -ve error number on failure.
280 int write_chunks(int fd, unsigned long long offset,unsigned long long write_end,
281 char *chunk_buf, size_t chunksize, const time_t time_st,
282 const ino_t inode_st, const char *file)
284 unsigned long long stride;
286 stride = full ? chunksize : (ONE_GB - chunksize);
287 for (offset = offset & ~(chunksize - 1); offset < write_end;
291 if (lseek64(fd, offset, SEEK_SET) == -1) {
292 fprintf(stderr, "\n%s: lseek64(%s+%llu) failed: %s\n",
293 progname, file, offset, strerror(errno));
296 if (offset + chunksize > write_end)
297 chunksize = write_end - offset;
298 if (!full && offset > chunksize) {
299 fill_chunk(chunk_buf, chunksize, offset, time_st,
301 ret = write_retry(fd, chunk_buf, chunksize,offset,file);
305 if (offset + chunksize > write_end)
306 chunksize = write_end - offset;
308 fill_chunk(chunk_buf, chunksize, offset, time_st, inode_st);
309 ret = write_retry(fd, chunk_buf, chunksize, offset, file);
317 * read_chunk: reads the chunk_buf from the device. The number of read
318 * operations are based on the parameters read_end, offset, and chunksize.
320 int read_chunks(int fd, unsigned long long offset, unsigned long long read_end,
321 char *chunk_buf, size_t chunksize, const time_t time_st,
322 const ino_t inode_st, const char *file)
324 unsigned long long stride;
326 stride = full ? chunksize : (ONE_GB - chunksize);
327 for (offset = offset & ~(chunksize - 1); offset < read_end;
331 if (lseek64(fd, offset, SEEK_SET) == -1) {
332 fprintf(stderr, "\n%s: lseek64(%s+%llu) failed: %s\n",
333 progname, file, offset, strerror(errno));
336 if (offset + chunksize > read_end)
337 chunksize = read_end - offset;
339 if (!full && offset > chunksize) {
340 nread = read(fd, chunk_buf, chunksize);
342 fprintf(stderr,"\n%s: read %s@%llu+%zi failed: "
343 "%s\n", progname, file, offset,
344 chunksize, strerror(errno));
348 if (nread < chunksize) {
349 fprintf(stderr, "\n%s: read %s@%llu+%zi short: "
350 "%zi read\n", progname, file, offset,
354 if (verify_chunk(chunk_buf, nread, offset, time_st,
355 inode_st, file) != 0) {
360 /* Need to reset position after read error */
361 if (nread < chunksize &&
362 lseek64(fd, offset, SEEK_SET) == -1) {
364 "\n%s: lseek64(%s@%llu) failed: %s\n",
365 progname, file, offset,strerror(errno));
368 if (offset + chunksize >= read_end)
369 chunksize = read_end - offset;
371 nread = read(fd, chunk_buf, chunksize);
373 fprintf(stderr, "\n%s: read %s@%llu+%zi failed: %s\n",
374 progname, file, offset, chunksize,
379 if (nread < chunksize) {
380 fprintf(stderr, "\n%s: read %s@%llu+%zi short: "
381 "%zi read\n", progname, file, offset,
386 if (verify_chunk(chunk_buf, nread, offset, time_st,
387 inode_st, file) != 0) {
395 * new_file: prepares new filename using file counter and current dir.
397 char *new_file(char *tempfile, char *cur_dir, int file_num)
399 sprintf(tempfile, "%s/file%03d", cur_dir, file_num);
404 * new_dir: prepares new dir name using dir counters.
406 char *new_dir(char *tempdir, int dir_num)
408 sprintf(tempdir, "%s/dir%05d", testdir, dir_num);
413 * calc_total_bytes: calculates total bytes that need to be
414 * written into or read from the filesystem.
416 static unsigned long long calc_total_bytes(const char *op)
418 unsigned long long total_bytes = 0;
419 struct statfs64 statbuf;
422 if (statfs64(testdir, &statbuf) == 0) {
423 if (strcmp(op, "write") == 0)
424 total_bytes = (unsigned long long)
425 (statbuf.f_bavail * statbuf.f_bsize);
426 else if (strcmp(op, "read") == 0)
427 total_bytes = (unsigned long long)
428 (statbuf.f_blocks * statbuf.f_bsize);
430 fprintf(stderr, "\n%s: invalid operation: %s\n",
435 fprintf(stderr, "\n%s: unable to stat %s: %s\n",
436 progname, testdir, strerror(errno));
440 total_bytes = num_dirs * files_in_dir * file_size;
447 * show_rate: displays the current read/write file name and performance,
448 * along with an estimate of how long the whole read/write operation
451 void show_rate(char *op, char *filename, const struct timeval *start_time,
452 const unsigned long long total_bytes,
453 const unsigned long long curr_bytes)
455 static struct timeval last_time;
456 static unsigned long long last_bytes;
458 struct timeval curr_time;
459 double curr_delta, overall_delta, curr_rate, overall_rate;
460 double total_time, remain_time;
461 int remain_hours, remain_minutes, remain_seconds;
463 if (last_time.tv_sec == 0)
464 last_time = *start_time;
466 if (last_op != op[0]) {
468 last_time = *start_time;
472 gettimeofday(&curr_time, NULL);
474 curr_delta = (curr_time.tv_sec - last_time.tv_sec) +
475 (double)(curr_time.tv_usec - last_time.tv_usec) / 1000000;
477 overall_delta = (curr_time.tv_sec - start_time->tv_sec) +
478 (double)(curr_time.tv_usec - start_time->tv_usec) / 1000000;
480 curr_rate = (curr_bytes - last_bytes) / curr_delta;
481 overall_rate = curr_bytes / overall_delta;
483 if (curr_rate == 0) {
484 last_time = curr_time;
487 total_time = total_bytes / curr_rate;
488 remain_time = total_time - overall_delta;
490 remain_hours = remain_time / 3600;
491 remain_minutes = (remain_time - remain_hours * 3600) / 60;
492 remain_seconds = (remain_time - remain_hours * 3600 -
493 remain_minutes * 60);
495 if (curr_delta > 4 || verbose > 2) {
499 printf("%s filename: %s, current %5g MB/s, overall %5g MB/s, "
500 "est %u:%u:%u left", op, filename,
501 curr_rate / ONE_MB, overall_rate / ONE_MB,
502 remain_hours, remain_minutes, remain_seconds);
510 last_time = curr_time;
511 last_bytes = curr_bytes;
515 * dir_write: This function writes directories and files on device.
516 * it works for both full and partial modes.
518 static int dir_write(char *chunk_buf, size_t chunksize,
519 time_t time_st, unsigned long dir_num)
521 char tempfile[PATH_MAX];
522 char tempdir[PATH_MAX];
525 int file_num = 999999999;
527 struct timeval start_time;
528 unsigned long long total_bytes;
529 unsigned long long curr_bytes = 0;
532 #ifdef HAVE_EXT2FS_EXT2FS_H
533 if (!full && fsetflags(testdir, EXT2_TOPDIR_FL))
535 "\n%s: can't set TOPDIR_FL on %s: %s (ignoring)",
536 progname, testdir, strerror(errno));
538 countfile = fopen(filecount, "w");
539 if (countfile == NULL) {
540 fprintf(stderr, "\n%s: creating %s failed :%s\n",
541 progname, filecount, strerror(errno));
544 /* reserve space for the countfile */
545 if (fprintf(countfile, "%lu", num_files) < 1 ||
546 fflush(countfile) != 0) {
547 fprintf(stderr, "\n%s: writing %s failed :%s\n",
548 progname, filecount, strerror(errno));
553 /* calculate total bytes that need to be written */
554 total_bytes = calc_total_bytes("write");
555 if (total_bytes <= 0) {
556 fprintf(stderr, "\n%s: unable to calculate total bytes\n",
562 if (!full && (dir_num != 0))
563 total_bytes -= dir_num * files_in_dir * file_size;
565 gettimeofday(&start_time, NULL);
566 for (; dir_num < num_dirs; num_files++, file_num++) {
569 if (file_num >= files_in_dir) {
570 if (dir_num == num_dirs - 1)
574 if (mkdir(new_dir(tempdir, dir_num), dirmode) < 0) {
577 if (errno != EEXIST) {
578 fprintf(stderr, "\n%s: mkdir %s : %s\n",
588 fd = open_file(new_file(tempfile, tempdir, file_num),
589 O_WRONLY | O_CREAT | O_TRUNC | O_LARGEFILE);
591 if (fstat64(fd, &file) == 0) {
592 inode_st = file.st_ino;
594 fprintf(stderr, "\n%s: write stat '%s': %s",
595 progname, tempfile, strerror(errno));
603 ret = write_chunks(fd, 0, file_size, chunk_buf, chunksize,
604 time_st, inode_st, tempfile);
607 if (ret != -ENOSPC) {
611 curr_bytes = total_bytes;
615 curr_bytes += file_size;
617 show_rate("write", tempfile, &start_time,
618 total_bytes, curr_bytes);
620 fseek(countfile, 0, SEEK_SET);
621 if (fprintf(countfile, "%lu", num_files) < 1 ||
622 fflush(countfile) != 0) {
623 fprintf(stderr, "\n%s: writing %s failed :%s\n",
624 progname, filecount, strerror(errno));
630 show_rate("write", tempfile, &start_time,
631 total_bytes, curr_bytes);
632 printf("\nwrite complete\n");
643 * dir_read: This function reads directories and files on device.
644 * it works for both full and partial modes.
646 static int dir_read(char *chunk_buf, size_t chunksize,
647 time_t time_st, unsigned long dir_num)
649 char tempfile[PATH_MAX];
650 char tempdir[PATH_MAX];
651 unsigned long count = 0;
655 struct timeval start_time;
656 unsigned long long total_bytes;
657 unsigned long long curr_bytes = 0;
659 /* calculate total bytes that need to be read */
660 total_bytes = calc_total_bytes("read");
661 if (total_bytes <= 0) {
662 fprintf(stderr, "\n%s: unable to calculate total bytes\n",
668 total_bytes -= dir_num * files_in_dir * file_size;
670 gettimeofday(&start_time, NULL);
671 for (count = 0; count < num_files && dir_num < num_dirs; count++) {
675 if (dir_num == num_dirs - 1)
678 new_dir(tempdir, dir_num);
682 fd = open_file(new_file(tempfile, tempdir, file_num),
683 O_RDONLY | O_LARGEFILE);
685 if (fstat64(fd, &file) == 0) {
686 inode_st = file.st_ino;
688 fprintf(stderr, "\n%s: read stat '%s': %s\n",
689 progname, tempfile, strerror(errno));
697 if (count == num_files)
698 file_size = file.st_size;
699 ret = read_chunks(fd, 0, file_size, chunk_buf, chunksize,
700 time_st, inode_st, tempfile);
705 curr_bytes += file_size;
707 show_rate("read", tempfile, &start_time,
708 total_bytes, curr_bytes);
710 if (++file_num >= files_in_dir)
715 show_rate("read", tempfile, &start_time,
716 total_bytes, curr_bytes);
717 printf("\nread complete\n");
723 int main(int argc, char **argv)
725 time_t time_st = 0; /* Default timestamp */
726 size_t chunksize = ONE_MB; /* IO chunk size(defailt=1MB) */
727 char *chunk_buf; /* chunk buffer */
729 FILE *countfile = NULL;
730 unsigned long dir_num = 0, dir_num_orig = 0;/* starting directory */
733 progname = strrchr(argv[0], '/') ? strrchr(argv[0], '/') + 1 : argv[0];
734 while ((c = getopt_long(argc, argv, "c:hlo:pqrs:t:vw",
735 longopts, NULL)) != -1) {
738 chunksize = strtoul(optarg, NULL, 0) * ONE_MB;
739 if (chunksize == 0) {
740 fprintf(stderr, "%s: bad chunk size '%s'\n",
748 case 'o': /* offset */
749 dir_num = strtoul(optarg, NULL, 0);
764 file_size = strtoul(optarg, NULL, 0) * ONE_MB;
765 if (file_size == 0) {
766 fprintf(stderr, "%s: bad file size '%s'\n",
772 time_st = (time_t)strtoul(optarg, NULL, 0);
787 testdir = argv[optind];
790 fprintf(stderr, "%s: pathname not given\n", progname);
794 if (!readoption && !writeoption) {
799 (void) time(&time_st);
800 printf("Timestamp: %lu\n", (unsigned long )time_st);
801 isatty_flag = isatty(STDOUT_FILENO);
804 #ifdef HAVE_EXT2FS_EXT2FS_H
805 struct mntent *tempmnt;
809 if ((fp = setmntent("/etc/mtab", "r")) == NULL){
810 fprintf(stderr, "%s: fail to open /etc/mtab in read"
811 "mode :%s\n", progname, strerror(errno));
815 /* find device name using filesystem */
816 while ((tempmnt = getmntent(fp)) != NULL) {
817 if (strcmp(tempmnt->mnt_dir, testdir) == 0)
821 if (tempmnt == NULL) {
822 fprintf(stderr, "%s: no device found for '%s'\n",
828 if (ext2fs_open(tempmnt->mnt_fsname, 0, 0, 0,
829 unix_io_manager, &fs)) {
830 fprintf(stderr, "%s: unable to open ext3 fs on '%s'\n",
837 num_dirs = (fs->super->s_blocks_count +
838 fs->super->s_blocks_per_group - 1) /
839 fs->super->s_blocks_per_group;
841 printf("ext3 block groups: %u, fs blocks: %u "
842 "blocks per group: %u\n",
843 num_dirs, fs->super->s_blocks_count,
844 fs->super->s_blocks_per_group);
850 struct statfs64 statbuf;
852 if (statfs64(testdir, &statbuf) == 0) {
853 num_dirs = (long long)statbuf.f_blocks *
854 statbuf.f_bsize / (128ULL << 20);
856 printf("dirs: %u, fs blocks: %llu\n",
858 (long long)statbuf.f_blocks);
860 fprintf(stderr, "%s: unable to stat '%s': %s\n",
861 progname, testdir, strerror(errno));
863 printf("dirs: %u\n", num_dirs);
867 chunk_buf = (char *)calloc(chunksize, 1);
868 if (chunk_buf == NULL) {
869 fprintf(stderr, "Memory allocation failed for chunk_buf\n");
872 snprintf(filecount, sizeof(filecount), "%s/%s.filecount",
875 (void)mkdir(testdir, dirmode);
879 num_files = dir_num * files_in_dir;
881 printf("\n%s: %lu files already written\n",
882 progname, num_files);
884 if (dir_write(chunk_buf, chunksize, time_st, dir_num)) {
888 dir_num = dir_num_orig;
892 countfile = fopen(filecount, "r");
893 if (countfile == NULL ||
894 fscanf(countfile, "%lu", &num_files) != 1 ||
896 fprintf(stderr, "\n%s: reading %s failed :%s\n",
897 progname, filecount, strerror(errno));
898 num_files = num_dirs * files_in_dir;
900 num_files -= (dir_num * files_in_dir);
905 if (dir_read(chunk_buf, chunksize, time_st, dir_num)) {
906 fprintf(stderr, "\n%s: Data verification failed\n",