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/
31 * lustre/utils/llverfs.c
33 * Filesystem Verification Tool.
34 * This program tests the correct operation of large filesystems and
35 * the underlying block storage device(s).
36 * This tool have two working modes
40 * In full mode, the program creates a subdirectory in the test
41 * filesystem, writes n(files_in_dir, default=32) large(4GB) files to
42 * the directory with the test pattern at the start of each 4kb block.
43 * The test pattern contains timestamp, relative file offset and per
44 * file unique identifier(inode number). This continues until the
45 * whole filesystem is full and then the tool verifies that the data
46 * in all of the test files is correct.
48 * In partial mode, the tool creates test directories with the
49 * EXT3_TOPDIR_FL flag set (if supported) to spread the directory data
50 * around the block device instead of localizing it in a single place.
51 * The number of directories equals to the number of block groups in the
52 * filesystem (e.g. 65536 directories for 8TB ext3/ext4 filesystem) and
53 * then writes a single 1MB file in each directory. The tool then verifies
54 * that the data in each file is correct.
63 #ifndef _LARGEFILE64_SOURCE
64 #define _LARGEFILE64_SOURCE
66 #ifndef _FILE_OFFSET_BITS
67 #define _FILE_OFFSET_BITS 64
84 #include <sys/types.h>
88 #include <gnu/stubs.h>
89 #include <gnu/stubs.h>
91 #ifdef HAVE_EXT2FS_EXT2FS_H
93 # include <ext2fs/ext2fs.h>
95 # ifndef EXT2_TOPDIR_FL
96 # define EXT2_TOPDIR_FL 0x00020000 /* Top of directory tree */
98 static int fsetflags(const char *path, unsigned int flag)
100 char cmd[PATH_MAX + 128];
103 if (flag != EXT2_TOPDIR_FL) {
108 snprintf(cmd, sizeof(cmd), "chattr +T %s", path);
112 rc = WEXITSTATUS(rc);
121 #define ONE_MB (1024 * 1024)
122 #define ONE_GB ((unsigned long long)(1024 * 1024 * 1024))
123 #define BLOCKSIZE 4096
125 /* Structure for writing test pattern */
127 unsigned long long bd_offset;
128 unsigned long long bd_time;
129 unsigned long long bd_inode;
131 static char *progname; /* name by which this program was run. */
132 static unsigned verbose = 1; /* prints offset in kB, operation rate */
133 static int readoption; /* run test in read-only (verify) mode */
134 static int writeoption; /* run test in write_only mode */
135 char *testdir; /* name of device to be tested. */
136 static unsigned full = 1; /* flag to full check */
137 static int error_count; /* number of IO errors hit during run */
138 char filecount[PATH_MAX]; /* file with total number of files written*/
139 static unsigned long num_files; /* Total number of files for read/write */
140 static loff_t file_size = 4*ONE_GB; /* Size of each file */
141 static unsigned files_in_dir = 32; /* number of files in each directioy */
142 static unsigned num_dirs = 30000; /* total number of directories */
143 const int dirmode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
144 static int isatty_flag;
145 static int perms = S_IRWXU | S_IRGRP | S_IROTH;
147 static struct option const long_opts[] = {
148 { .val = 'c', .name = "chunksize", .has_arg = required_argument },
149 { .val = 'h', .name = "help", .has_arg = no_argument },
150 { .val = 'l', .name = "long", .has_arg = no_argument },
151 { .val = 'l', .name = "full", .has_arg = no_argument },
152 { .val = 'o', .name = "offset", .has_arg = required_argument },
153 { .val = 'p', .name = "partial", .has_arg = required_argument },
154 { .val = 'q', .name = "quiet", .has_arg = required_argument },
155 { .val = 'r', .name = "read", .has_arg = no_argument },
156 { .val = 's', .name = "filesize", .has_arg = no_argument },
157 { .val = 't', .name = "timestamp", .has_arg = required_argument },
158 { .val = 'v', .name = "verbose", .has_arg = no_argument },
159 { .val = 'w', .name = "write", .has_arg = no_argument },
163 * Usages: displays help information, whenever user supply --help option in
164 * command or enters incorrect command line.
166 void usage(int status)
169 printf("\nUsage: %s [OPTION]... <filesystem path> ...\n",
171 printf("Filesystem verification tool.\n"
172 "\t-t {seconds}, --timestamp, set test time"
173 "(default=current time())\n"
174 "\t-o {offset}, --offset, directory starting offset"
175 " from which tests should start\n"
176 "\t-r, --read, run in verify mode\n"
177 "\t-w, --write, run in test-pattern mode, default=rw\n"
179 "\t-p, --partial, for partial check (1MB files)\n"
180 "\t-l, --long, --full check (4GB file with 4k blocks)\n"
181 "\t-c, --chunksize, IO chunk size in MB (default=1)\n"
182 "\t-s, --filesize, file size in MB (default=4096)\n"
183 "\t-h, --help, display this help and exit\n");
189 * open_file: Opens file in specified mode and returns fd.
191 static int open_file(const char *file, int flag)
193 int fd = open(file, flag, perms);
195 fprintf(stderr, "\n%s: Open '%s' failed:%s\n",
196 progname, file, strerror(errno));
202 * Verify_chunk: Verifies test pattern in each 4kB (BLOCKSIZE) is correct.
203 * Returns 0 if test offset and timestamp is correct otherwise 1.
205 int verify_chunk(char *chunk_buf, const size_t chunksize,
206 unsigned long long chunk_off, const unsigned long long time_st,
207 const unsigned long long inode_st, const char *file)
209 struct block_data *bd;
212 for (chunk_end = chunk_buf + chunksize - sizeof(*bd);
213 (char *)chunk_buf < chunk_end;
214 chunk_buf += BLOCKSIZE, chunk_off += BLOCKSIZE) {
215 bd = (struct block_data *)chunk_buf;
216 if ((bd->bd_offset == chunk_off) && (bd->bd_time == time_st) &&
217 (bd->bd_inode == inode_st))
219 fprintf(stderr, "\n%s: verify %s failed offset/timestamp/inode "
220 "%llu/%llu/%llu: found %llu/%llu/%llu instead\n",
221 progname, file, chunk_off, time_st, inode_st,
222 bd->bd_offset, bd->bd_time, bd->bd_inode);
229 * fill_chunk: Fills the chunk with current or user specified timestamp
230 * and offset. The test pattern is filled at the beginning of
231 * each 4kB(BLOCKSIZE) blocks in chunk_buf.
233 void fill_chunk(char *chunk_buf, size_t chunksize, loff_t chunk_off,
234 const time_t time_st, const ino_t inode_st)
236 struct block_data *bd;
239 for (chunk_end = chunk_buf + chunksize - sizeof(*bd);
240 (char *)chunk_buf < chunk_end;
241 chunk_buf += BLOCKSIZE, chunk_off += BLOCKSIZE) {
242 bd = (struct block_data *)chunk_buf;
243 bd->bd_offset = chunk_off;
244 bd->bd_time = time_st;
245 bd->bd_inode = inode_st;
250 * Write a chunk to disk, handling errors, interrupted writes, etc.
252 * If there is an IO error hit during the write, it is possible that
253 * this will just show up as a short write, and a subsequent write
254 * will return the actual error. We want to continue in the face of
255 * minor media errors so that we can validate the whole device if
256 * possible, but if there are many errors we don't want to loop forever.
258 * The error count will be returned upon exit to ensure that the
259 * media errors are detected even if nobody is looking at the output.
261 * Returns 0 on success, or -ve errno on failure.
263 int write_retry(int fd, const char *chunk_buf, size_t nrequested,
264 unsigned long long offset, const char *file)
269 nwritten = write(fd, chunk_buf, nrequested);
271 if (errno != ENOSPC) {
272 fprintf(stderr, "\n%s: write %s@%llu+%zi failed: %s\n",
273 progname, file, offset, nrequested,
275 if (error_count++ < 100)
280 if (nwritten < nrequested) {
281 fprintf(stderr, "\n%s: write %s@%llu+%zi short: %ld written\n",
282 progname, file, offset, nrequested, nwritten);
284 chunk_buf += nwritten;
285 nrequested -= nwritten;
293 * write_chunks: write the chunk_buf on the device. The number of write
294 * operations are based on the parameters write_end, offset, and chunksize.
296 * Returns 0 on success, or -ve error number on failure.
298 int write_chunks(int fd, unsigned long long offset,unsigned long long write_end,
299 char *chunk_buf, size_t chunksize, const time_t time_st,
300 const ino_t inode_st, const char *file)
302 unsigned long long stride;
304 stride = full ? chunksize : (ONE_GB - chunksize);
305 for (offset = offset & ~(chunksize - 1); offset < write_end;
309 if (stride != chunksize && lseek64(fd, offset, SEEK_SET) < 0) {
310 fprintf(stderr, "\n%s: lseek66(%s+%llu) failed: %s\n",
311 progname, file, offset, strerror(errno));
314 if (offset + chunksize > write_end)
315 chunksize = write_end - offset;
316 if (!full && offset > chunksize) {
317 fill_chunk(chunk_buf, chunksize, offset, time_st,
319 ret = write_retry(fd, chunk_buf, chunksize,offset,file);
323 if (offset + chunksize > write_end)
324 chunksize = write_end - offset;
326 fill_chunk(chunk_buf, chunksize, offset, time_st, inode_st);
327 ret = write_retry(fd, chunk_buf, chunksize, offset, file);
335 * read_chunk: reads the chunk_buf from the device. The number of read
336 * operations are based on the parameters read_end, offset, and chunksize.
338 int read_chunks(int fd, unsigned long long offset, unsigned long long read_end,
339 char *chunk_buf, size_t chunksize, const time_t time_st,
340 const ino_t inode_st, const char *file)
342 unsigned long long stride;
344 stride = full ? chunksize : (ONE_GB - chunksize);
345 for (offset = offset & ~(chunksize - 1); offset < read_end;
349 if (stride != chunksize && lseek64(fd, offset, SEEK_SET) < 0) {
350 fprintf(stderr, "\n%s: lseek64(%s+%llu) failed: %s\n",
351 progname, file, offset, strerror(errno));
354 if (offset + chunksize > read_end)
355 chunksize = read_end - offset;
357 if (!full && offset > chunksize) {
358 nread = read(fd, chunk_buf, chunksize);
360 fprintf(stderr,"\n%s: read %s@%llu+%zi failed: "
361 "%s\n", progname, file, offset,
362 chunksize, strerror(errno));
366 if (nread < chunksize) {
367 fprintf(stderr, "\n%s: read %s@%llu+%zi short: "
368 "%zi read\n", progname, file, offset,
372 if (verify_chunk(chunk_buf, nread, offset, time_st,
373 inode_st, file) != 0) {
378 /* Need to reset position after read error */
379 if (nread < chunksize &&
380 lseek64(fd, offset, SEEK_SET) == -1) {
382 "\n%s: lseek64(%s@%llu) failed: %s\n",
383 progname, file, offset,strerror(errno));
386 if (offset + chunksize >= read_end)
387 chunksize = read_end - offset;
389 nread = read(fd, chunk_buf, chunksize);
391 fprintf(stderr, "\n%s: read %s@%llu+%zi failed: %s\n",
392 progname, file, offset, chunksize,
397 if (nread < chunksize) {
398 fprintf(stderr, "\n%s: read %s@%llu+%zi short: "
399 "%zi read\n", progname, file, offset,
404 if (verify_chunk(chunk_buf, nread, offset, time_st,
405 inode_st, file) != 0) {
413 * new_file: prepares new filename using file counter and current dir.
415 char *new_file(char *tempfile, char *cur_dir, int file_num)
417 snprintf(tempfile, PATH_MAX, "%s/file%03d", cur_dir, file_num);
422 * new_dir: prepares new dir name using dir counters.
424 char *new_dir(char *tempdir, int dir_num)
426 snprintf(tempdir, PATH_MAX, "%s/llverfs_dir%05d", testdir, dir_num);
431 * calc_total_bytes: calculates total bytes that need to be
432 * written into or read from the filesystem.
434 static unsigned long long calc_total_bytes(const char *op)
436 unsigned long long total_bytes = 0;
437 struct statfs64 statbuf;
440 if (statfs64(testdir, &statbuf) == 0) {
441 if (strcmp(op, "write") == 0)
442 total_bytes = (unsigned long long)
443 (statbuf.f_bavail * statbuf.f_bsize);
444 else if (strcmp(op, "read") == 0)
445 total_bytes = (unsigned long long)
446 (statbuf.f_blocks * statbuf.f_bsize);
448 fprintf(stderr, "\n%s: invalid operation: %s\n",
453 fprintf(stderr, "\n%s: unable to stat %s: %s\n",
454 progname, testdir, strerror(errno));
458 total_bytes = num_dirs * files_in_dir * file_size;
465 * show_rate: displays the current read/write file name and performance,
466 * along with an estimate of how long the whole read/write operation
469 void show_rate(char *op, char *filename, const struct timeval *start_time,
470 const unsigned long long total_bytes,
471 const unsigned long long curr_bytes)
473 static struct timeval last_time;
474 static unsigned long long last_bytes;
476 struct timeval curr_time;
477 double curr_delta, overall_delta, curr_rate, overall_rate;
479 int remain_hours, remain_minutes, remain_seconds;
481 if (last_op != op[0]) {
483 last_time = *start_time;
487 gettimeofday(&curr_time, NULL);
489 curr_delta = (curr_time.tv_sec - last_time.tv_sec) +
490 (double)(curr_time.tv_usec - last_time.tv_usec) / 1000000;
492 overall_delta = (curr_time.tv_sec - start_time->tv_sec) +
493 (double)(curr_time.tv_usec - start_time->tv_usec) / 1000000;
495 curr_rate = (curr_bytes - last_bytes) / curr_delta;
496 overall_rate = curr_bytes / overall_delta;
498 if (curr_rate == 0) {
499 last_time = curr_time;
502 remain_time = (total_bytes - curr_bytes) / curr_rate;
504 remain_hours = remain_time / 3600;
505 remain_minutes = (remain_time - remain_hours * 3600) / 60;
506 remain_seconds = (remain_time - remain_hours * 3600 -
507 remain_minutes * 60);
509 if (curr_delta > 4 || verbose > 2) {
513 printf("%s: %s, current: %5g MB/s, overall: %5g MB/s, "
514 "ETA: %u:%02u:%02u", op, filename,
515 curr_rate / ONE_MB, overall_rate / ONE_MB,
516 remain_hours, remain_minutes, remain_seconds);
523 last_time = curr_time;
524 last_bytes = curr_bytes;
529 * dir_write: This function writes directories and files on device.
530 * it works for both full and partial modes.
532 static int dir_write(char *chunk_buf, size_t chunksize,
533 time_t time_st, unsigned long dir_num)
535 char tempfile[PATH_MAX];
536 char tempdir[PATH_MAX];
539 int file_num = 999999999;
541 struct timeval start_time;
542 unsigned long long total_bytes;
543 unsigned long long curr_bytes = 0;
546 if (!full && fsetflags(testdir, EXT2_TOPDIR_FL))
548 "\n%s: can't set TOPDIR_FL on %s: %s (ignoring)",
549 progname, testdir, strerror(errno));
551 countfile = fopen(filecount, "w");
552 if (countfile == NULL) {
553 fprintf(stderr, "\n%s: creating %s failed :%s\n",
554 progname, filecount, strerror(errno));
557 /* reserve space for the countfile */
558 if (fprintf(countfile, "%lu", num_files) < 1 ||
559 fflush(countfile) != 0) {
560 fprintf(stderr, "\n%s: writing %s failed :%s\n",
561 progname, filecount, strerror(errno));
566 /* calculate total bytes that need to be written */
567 total_bytes = calc_total_bytes("write");
568 if (total_bytes <= 0) {
569 fprintf(stderr, "\n%s: unable to calculate total bytes\n",
575 if (!full && (dir_num != 0))
576 total_bytes -= dir_num * files_in_dir * file_size;
578 gettimeofday(&start_time, NULL);
579 for (; dir_num < num_dirs; num_files++, file_num++) {
582 if (file_num >= files_in_dir) {
584 if (mkdir(new_dir(tempdir, dir_num), dirmode) < 0) {
587 if (errno != EEXIST) {
588 fprintf(stderr, "\n%s: mkdir %s : %s\n",
598 fd = open_file(new_file(tempfile, tempdir, file_num),
599 O_WRONLY | O_CREAT | O_TRUNC | O_LARGEFILE);
601 if (fstat64(fd, &file) == 0) {
602 inode_st = file.st_ino;
604 fprintf(stderr, "\n%s: write stat '%s': %s",
605 progname, tempfile, strerror(errno));
613 ret = write_chunks(fd, 0, file_size, chunk_buf, chunksize,
614 time_st, inode_st, tempfile);
617 if (ret != -ENOSPC) {
621 curr_bytes = total_bytes;
625 curr_bytes += file_size;
627 show_rate("write", tempfile, &start_time,
628 total_bytes, curr_bytes);
630 fseek(countfile, 0, SEEK_SET);
631 if (fprintf(countfile, "%lu", num_files) < 1 ||
632 fflush(countfile) != 0) {
633 fprintf(stderr, "\n%s: writing %s failed :%s\n",
634 progname, filecount, strerror(errno));
639 show_rate("write_done", tempfile, &start_time, total_bytes, curr_bytes);
650 * dir_read: This function reads directories and files on device.
651 * it works for both full and partial modes.
653 static int dir_read(char *chunk_buf, size_t chunksize,
654 time_t time_st, unsigned long dir_num)
656 char tempfile[PATH_MAX];
657 char tempdir[PATH_MAX];
658 unsigned long count = 0;
662 struct timeval start_time;
663 unsigned long long total_bytes;
664 unsigned long long curr_bytes = 0;
666 /* calculate total bytes that need to be read */
667 total_bytes = calc_total_bytes("read");
668 if (total_bytes <= 0) {
669 fprintf(stderr, "\n%s: unable to calculate total bytes\n",
675 total_bytes -= dir_num * files_in_dir * file_size;
677 gettimeofday(&start_time, NULL);
678 for (count = 0; count < num_files && dir_num < num_dirs; count++) {
682 new_dir(tempdir, dir_num);
686 fd = open_file(new_file(tempfile, tempdir, file_num),
687 O_RDONLY | O_LARGEFILE);
689 if (fstat64(fd, &file) == 0) {
690 inode_st = file.st_ino;
692 fprintf(stderr, "\n%s: read stat '%s': %s\n",
693 progname, tempfile, strerror(errno));
701 if (count == num_files)
702 file_size = file.st_size;
703 ret = read_chunks(fd, 0, file_size, chunk_buf, chunksize,
704 time_st, inode_st, tempfile);
709 curr_bytes += file_size;
711 show_rate("read", tempfile, &start_time,
712 total_bytes, curr_bytes);
714 if (++file_num >= files_in_dir)
718 show_rate("read_done", tempfile, &start_time, total_bytes, curr_bytes);
725 int main(int argc, char **argv)
727 time_t time_st = 0; /* Default timestamp */
728 size_t chunksize = ONE_MB; /* IO chunk size(defailt=1MB) */
729 char *chunk_buf; /* chunk buffer */
731 FILE *countfile = NULL;
732 unsigned long dir_num = 0, dir_num_orig = 0;/* starting directory */
735 progname = strrchr(argv[0], '/') ? strrchr(argv[0], '/') + 1 : argv[0];
736 while ((c = getopt_long(argc, argv, "c:hlo:pqrs:t:vw",
737 long_opts, NULL)) != -1) {
740 chunksize = strtoul(optarg, NULL, 0) * ONE_MB;
741 if (chunksize == 0) {
742 fprintf(stderr, "%s: bad chunk size '%s'\n",
750 case 'o': /* offset */
751 dir_num = strtoul(optarg, NULL, 0);
766 file_size = strtoul(optarg, NULL, 0) * ONE_MB;
767 if (file_size == 0) {
768 fprintf(stderr, "%s: bad file size '%s'\n",
774 time_st = (time_t)strtoul(optarg, NULL, 0);
789 testdir = argv[optind];
792 fprintf(stderr, "%s: pathname not given\n", progname);
796 if (!readoption && !writeoption) {
801 (void) time(&time_st);
802 printf("Timestamp: %lu\n", (unsigned long )time_st);
803 isatty_flag = isatty(STDOUT_FILENO);
806 #ifdef HAVE_EXT2FS_EXT2FS_H
807 struct mntent *tempmnt;
811 if ((fp = setmntent("/etc/mtab", "r")) == NULL) {
812 fprintf(stderr, "%s: fail to open /etc/mtab in read mode :%s\n",
813 progname, strerror(errno));
817 /* find device name using filesystem */
818 while ((tempmnt = getmntent(fp)) != NULL) {
819 if (strcmp(tempmnt->mnt_dir, testdir) == 0)
823 if (tempmnt == NULL) {
824 fprintf(stderr, "%s: no device found for '%s'\n",
830 if (ext2fs_open(tempmnt->mnt_fsname, 0, 0, 0,
831 unix_io_manager, &fs)) {
832 fprintf(stderr, "%s: unable to open ext3 fs on '%s'\n",
839 num_dirs = (fs->super->s_blocks_count +
840 fs->super->s_blocks_per_group - 1) /
841 fs->super->s_blocks_per_group;
843 printf("ext3 block groups: %u, fs blocks: %u "
844 "blocks per group: %u\n",
845 num_dirs, fs->super->s_blocks_count,
846 fs->super->s_blocks_per_group);
852 struct statfs64 statbuf;
855 * Most extN filesystems are formatted with 128MB/group
856 * (32k bitmap = 4KB blocksize * 8 bits/block) * 4KB,
857 * so this is a relatively safe default (somewhat more
858 * or less doesn't make a huge difference for testing).
860 * We want to create one directory per group, together
861 * with the "TOPDIR" feature, so that the directories
862 * are spread across the whole block device.
864 if (statfs64(testdir, &statbuf) == 0) {
865 num_dirs = 1 + (long long)statbuf.f_blocks *
866 statbuf.f_bsize / (128ULL * ONE_MB);
868 printf("dirs: %u, fs blocks: %llu\n",
870 (long long)statbuf.f_blocks);
872 fprintf(stderr, "%s: unable to stat '%s': %s\n",
873 progname, testdir, strerror(errno));
875 printf("dirs: %u\n", num_dirs);
879 chunk_buf = (char *)calloc(chunksize, 1);
880 if (chunk_buf == NULL) {
881 fprintf(stderr, "Memory allocation failed for chunk_buf\n");
884 snprintf(filecount, sizeof(filecount), "%s/%s.filecount",
887 (void)mkdir(testdir, dirmode);
891 num_files = dir_num * files_in_dir;
893 printf("\n%s: %lu files already written\n",
894 progname, num_files);
896 if (dir_write(chunk_buf, chunksize, time_st, dir_num)) {
900 dir_num = dir_num_orig;
904 countfile = fopen(filecount, "r");
905 if (countfile == NULL ||
906 fscanf(countfile, "%lu", &num_files) != 1 ||
908 fprintf(stderr, "\n%s: reading %s failed :%s\n",
909 progname, filecount, strerror(errno));
910 num_files = num_dirs * files_in_dir;
912 num_files -= (dir_num * files_in_dir);
917 if (dir_read(chunk_buf, chunksize, time_st, dir_num)) {
918 fprintf(stderr, "\n%s: Data verification failed\n",