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 int num_dirs; /* 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 = 'n', .name = "num_dirs", .has_arg = required_argument },
153 { .val = 'o', .name = "offset", .has_arg = required_argument },
154 { .val = 'p', .name = "partial", .has_arg = required_argument },
155 { .val = 'q', .name = "quiet", .has_arg = required_argument },
156 { .val = 'r', .name = "read", .has_arg = no_argument },
157 { .val = 's', .name = "filesize", .has_arg = no_argument },
158 { .val = 't', .name = "timestamp", .has_arg = required_argument },
159 { .val = 'v', .name = "verbose", .has_arg = no_argument },
160 { .val = 'w', .name = "write", .has_arg = no_argument },
164 * Usages: displays help information, whenever user supply --help option in
165 * command or enters incorrect command line.
167 void usage(int status)
170 printf("\nUsage: %s [OPTION]... <filesystem path> ...\n",
172 printf("Filesystem verification tool.\n"
173 "\t-t {seconds}, --timestamp, set test time"
174 "(default=current time())\n"
175 "\t-o {offset}, --offset, directory starting offset"
176 " from which tests should start\n"
177 "\t-r, --read, run in verify mode\n"
178 "\t-w, --write, run in test-pattern mode, default=rw\n"
180 "\t-p, --partial, for partial check (1MB files)\n"
181 "\t-l, --long, --full check (4GB file with 4k blocks)\n"
182 "\t-n, --num_dirs, number of directories to create\n"
183 "\t-c, --chunksize, IO chunk size in MB (default=1)\n"
184 "\t-s, --filesize, file size in MB (default=4096)\n"
185 "\t-h, --help, display this help and exit\n");
191 * open_file: Opens file in specified mode and returns fd.
193 static int open_file(const char *file, int flag)
195 int fd = open(file, flag, perms);
197 fprintf(stderr, "\n%s: Open '%s' failed:%s\n",
198 progname, file, strerror(errno));
204 * Verify_chunk: Verifies test pattern in each 4kB (BLOCKSIZE) is correct.
205 * Returns 0 if test offset and timestamp is correct otherwise 1.
207 int verify_chunk(char *chunk_buf, const size_t chunksize,
208 unsigned long long chunk_off, const unsigned long long time_st,
209 const unsigned long long inode_st, const char *file)
211 struct block_data *bd;
214 for (chunk_end = chunk_buf + chunksize - sizeof(*bd);
215 (char *)chunk_buf < chunk_end;
216 chunk_buf += BLOCKSIZE, chunk_off += BLOCKSIZE) {
217 bd = (struct block_data *)chunk_buf;
218 if ((bd->bd_offset == chunk_off) && (bd->bd_time == time_st) &&
219 (bd->bd_inode == inode_st))
221 fprintf(stderr, "\n%s: verify %s failed offset/timestamp/inode "
222 "%llu/%llu/%llu: found %llu/%llu/%llu instead\n",
223 progname, file, chunk_off, time_st, inode_st,
224 bd->bd_offset, bd->bd_time, bd->bd_inode);
231 * fill_chunk: Fills the chunk with current or user specified timestamp
232 * and offset. The test pattern is filled at the beginning of
233 * each 4kB(BLOCKSIZE) blocks in chunk_buf.
235 void fill_chunk(char *chunk_buf, size_t chunksize, loff_t chunk_off,
236 const time_t time_st, const ino_t inode_st)
238 struct block_data *bd;
241 for (chunk_end = chunk_buf + chunksize - sizeof(*bd);
242 (char *)chunk_buf < chunk_end;
243 chunk_buf += BLOCKSIZE, chunk_off += BLOCKSIZE) {
244 bd = (struct block_data *)chunk_buf;
245 bd->bd_offset = chunk_off;
246 bd->bd_time = time_st;
247 bd->bd_inode = inode_st;
252 * Write a chunk to disk, handling errors, interrupted writes, etc.
254 * If there is an IO error hit during the write, it is possible that
255 * this will just show up as a short write, and a subsequent write
256 * will return the actual error. We want to continue in the face of
257 * minor media errors so that we can validate the whole device if
258 * possible, but if there are many errors we don't want to loop forever.
260 * The error count will be returned upon exit to ensure that the
261 * media errors are detected even if nobody is looking at the output.
263 * Returns 0 on success, or -ve errno on failure.
265 int write_retry(int fd, const char *chunk_buf, size_t nrequested,
266 unsigned long long offset, const char *file)
271 nwritten = write(fd, chunk_buf, nrequested);
273 if (errno != ENOSPC) {
274 fprintf(stderr, "\n%s: write %s@%llu+%zi failed: %s\n",
275 progname, file, offset, nrequested,
277 if (error_count++ < 100)
282 if (nwritten < nrequested) {
283 fprintf(stderr, "\n%s: write %s@%llu+%zi short: %ld written\n",
284 progname, file, offset, nrequested, nwritten);
286 chunk_buf += nwritten;
287 nrequested -= nwritten;
295 * write_chunks: write the chunk_buf on the device. The number of write
296 * operations are based on the parameters write_end, offset, and chunksize.
298 * Returns 0 on success, or -ve error number on failure.
300 int write_chunks(int fd, unsigned long long offset,unsigned long long write_end,
301 char *chunk_buf, size_t chunksize, const time_t time_st,
302 const ino_t inode_st, const char *file)
304 unsigned long long stride;
306 stride = full ? chunksize : (ONE_GB - chunksize);
307 for (offset = offset & ~(chunksize - 1); offset < write_end;
311 if (stride != chunksize && lseek64(fd, offset, SEEK_SET) < 0) {
312 fprintf(stderr, "\n%s: lseek66(%s+%llu) failed: %s\n",
313 progname, file, offset, strerror(errno));
316 if (offset + chunksize > write_end)
317 chunksize = write_end - offset;
318 if (!full && offset > chunksize) {
319 fill_chunk(chunk_buf, chunksize, offset, time_st,
321 ret = write_retry(fd, chunk_buf, chunksize,offset,file);
325 if (offset + chunksize > write_end)
326 chunksize = write_end - offset;
328 fill_chunk(chunk_buf, chunksize, offset, time_st, inode_st);
329 ret = write_retry(fd, chunk_buf, chunksize, offset, file);
337 * read_chunk: reads the chunk_buf from the device. The number of read
338 * operations are based on the parameters read_end, offset, and chunksize.
340 int read_chunks(int fd, unsigned long long offset, unsigned long long read_end,
341 char *chunk_buf, size_t chunksize, const time_t time_st,
342 const ino_t inode_st, const char *file)
344 unsigned long long stride;
346 stride = full ? chunksize : (ONE_GB - chunksize);
347 for (offset = offset & ~(chunksize - 1); offset < read_end;
351 if (stride != chunksize && lseek64(fd, offset, SEEK_SET) < 0) {
352 fprintf(stderr, "\n%s: lseek64(%s+%llu) failed: %s\n",
353 progname, file, offset, strerror(errno));
356 if (offset + chunksize > read_end)
357 chunksize = read_end - offset;
359 if (!full && offset > chunksize) {
360 nread = read(fd, chunk_buf, chunksize);
362 fprintf(stderr,"\n%s: read %s@%llu+%zi failed: "
363 "%s\n", progname, file, offset,
364 chunksize, strerror(errno));
368 if (nread < chunksize) {
369 fprintf(stderr, "\n%s: read %s@%llu+%zi short: "
370 "%zi read\n", progname, file, offset,
374 if (verify_chunk(chunk_buf, nread, offset, time_st,
375 inode_st, file) != 0) {
380 /* Need to reset position after read error */
381 if (nread < chunksize &&
382 lseek64(fd, offset, SEEK_SET) == -1) {
384 "\n%s: lseek64(%s@%llu) failed: %s\n",
385 progname, file, offset,strerror(errno));
388 if (offset + chunksize >= read_end)
389 chunksize = read_end - offset;
391 nread = read(fd, chunk_buf, chunksize);
393 fprintf(stderr, "\n%s: read %s@%llu+%zi failed: %s\n",
394 progname, file, offset, chunksize,
399 if (nread < chunksize) {
400 fprintf(stderr, "\n%s: read %s@%llu+%zi short: "
401 "%zi read\n", progname, file, offset,
406 if (verify_chunk(chunk_buf, nread, offset, time_st,
407 inode_st, file) != 0) {
415 * new_file: prepares new filename using file counter and current dir.
417 char *new_file(char *tempfile, char *cur_dir, int file_num)
419 snprintf(tempfile, PATH_MAX, "%s/file%03d", cur_dir, file_num);
424 * new_dir: prepares new dir name using dir counters.
426 char *new_dir(char *tempdir, int dir_num)
428 snprintf(tempdir, PATH_MAX, "%s/llverfs_dir%05d", testdir, dir_num);
433 * calc_total_bytes: calculates total bytes that need to be
434 * written into or read from the filesystem.
436 static unsigned long long calc_total_bytes(const char *op)
438 unsigned long long total_bytes = 0;
439 struct statfs64 statbuf;
442 if (statfs64(testdir, &statbuf) == 0) {
443 if (strcmp(op, "write") == 0)
444 total_bytes = (unsigned long long)
445 (statbuf.f_bavail * statbuf.f_bsize);
446 else if (strcmp(op, "read") == 0)
447 total_bytes = (unsigned long long)
448 (statbuf.f_blocks * statbuf.f_bsize);
450 fprintf(stderr, "\n%s: invalid operation: %s\n",
455 fprintf(stderr, "\n%s: unable to stat %s: %s\n",
456 progname, testdir, strerror(errno));
460 total_bytes = num_dirs * files_in_dir * file_size;
467 * show_rate: displays the current read/write file name and performance,
468 * along with an estimate of how long the whole read/write operation
471 void show_rate(char *op, char *filename, const struct timeval *start_time,
472 const unsigned long long total_bytes,
473 const unsigned long long curr_bytes)
475 static struct timeval last_time;
476 static unsigned long long last_bytes;
478 struct timeval curr_time;
479 double curr_delta, overall_delta, curr_rate, overall_rate;
481 int remain_hours, remain_minutes, remain_seconds;
483 if (last_op != op[0]) {
485 last_time = *start_time;
489 gettimeofday(&curr_time, NULL);
491 curr_delta = (curr_time.tv_sec - last_time.tv_sec) +
492 (double)(curr_time.tv_usec - last_time.tv_usec) / 1000000;
494 overall_delta = (curr_time.tv_sec - start_time->tv_sec) +
495 (double)(curr_time.tv_usec - start_time->tv_usec) / 1000000;
497 curr_rate = (curr_bytes - last_bytes) / curr_delta;
498 overall_rate = curr_bytes / overall_delta;
500 if (curr_rate == 0) {
501 last_time = curr_time;
504 remain_time = (total_bytes - curr_bytes) / curr_rate;
506 remain_hours = remain_time / 3600;
507 remain_minutes = (remain_time - remain_hours * 3600) / 60;
508 remain_seconds = (remain_time - remain_hours * 3600 -
509 remain_minutes * 60);
511 if (curr_delta > 4 || verbose > 2) {
515 printf("%s: %s, current: %5g MB/s, overall: %5g MB/s, "
516 "ETA: %u:%02u:%02u", op, filename,
517 curr_rate / ONE_MB, overall_rate / ONE_MB,
518 remain_hours, remain_minutes, remain_seconds);
525 last_time = curr_time;
526 last_bytes = curr_bytes;
531 * dir_write: This function writes directories and files on device.
532 * it works for both full and partial modes.
534 static int dir_write(char *chunk_buf, size_t chunksize,
535 time_t time_st, unsigned long dir_num)
537 char tempfile[PATH_MAX];
538 char tempdir[PATH_MAX];
541 int file_num = 999999999;
543 struct timeval start_time;
544 unsigned long long total_bytes;
545 unsigned long long curr_bytes = 0;
548 if (!full && fsetflags(testdir, EXT2_TOPDIR_FL))
550 "\n%s: can't set TOPDIR_FL on %s: %s (ignoring)",
551 progname, testdir, strerror(errno));
553 countfile = fopen(filecount, "w");
554 if (countfile == NULL) {
555 fprintf(stderr, "\n%s: creating %s failed :%s\n",
556 progname, filecount, strerror(errno));
559 /* reserve space for the countfile */
560 if (fprintf(countfile, "%lu", num_files) < 1 ||
561 fflush(countfile) != 0) {
562 fprintf(stderr, "\n%s: writing %s failed :%s\n",
563 progname, filecount, strerror(errno));
568 /* calculate total bytes that need to be written */
569 total_bytes = calc_total_bytes("write");
570 if (total_bytes <= 0) {
571 fprintf(stderr, "\n%s: unable to calculate total bytes\n",
577 if (!full && (dir_num != 0))
578 total_bytes -= dir_num * files_in_dir * file_size;
580 gettimeofday(&start_time, NULL);
581 for (; dir_num < num_dirs; num_files++, file_num++) {
584 if (file_num >= files_in_dir) {
586 if (mkdir(new_dir(tempdir, dir_num), dirmode) < 0) {
589 if (errno != EEXIST) {
590 fprintf(stderr, "\n%s: mkdir %s : %s\n",
600 fd = open_file(new_file(tempfile, tempdir, file_num),
601 O_WRONLY | O_CREAT | O_TRUNC | O_LARGEFILE);
603 if (fstat64(fd, &file) == 0) {
604 inode_st = file.st_ino;
606 fprintf(stderr, "\n%s: write stat '%s': %s",
607 progname, tempfile, strerror(errno));
615 ret = write_chunks(fd, 0, file_size, chunk_buf, chunksize,
616 time_st, inode_st, tempfile);
619 if (ret != -ENOSPC) {
623 curr_bytes = total_bytes;
627 curr_bytes += file_size;
629 show_rate("write", tempfile, &start_time,
630 total_bytes, curr_bytes);
632 fseek(countfile, 0, SEEK_SET);
633 if (fprintf(countfile, "%lu", num_files) < 1 ||
634 fflush(countfile) != 0) {
635 fprintf(stderr, "\n%s: writing %s failed :%s\n",
636 progname, filecount, strerror(errno));
641 show_rate("write_done", tempfile, &start_time, total_bytes, curr_bytes);
652 * dir_read: This function reads directories and files on device.
653 * it works for both full and partial modes.
655 static int dir_read(char *chunk_buf, size_t chunksize,
656 time_t time_st, unsigned long dir_num)
658 char tempfile[PATH_MAX];
659 char tempdir[PATH_MAX];
660 unsigned long count = 0;
664 struct timeval start_time;
665 unsigned long long total_bytes;
666 unsigned long long curr_bytes = 0;
668 /* calculate total bytes that need to be read */
669 total_bytes = calc_total_bytes("read");
670 if (total_bytes <= 0) {
671 fprintf(stderr, "\n%s: unable to calculate total bytes\n",
677 total_bytes -= dir_num * files_in_dir * file_size;
679 gettimeofday(&start_time, NULL);
680 for (count = 0; count < num_files && dir_num < num_dirs; count++) {
684 new_dir(tempdir, dir_num);
688 fd = open_file(new_file(tempfile, tempdir, file_num),
689 O_RDONLY | O_LARGEFILE);
691 if (fstat64(fd, &file) == 0) {
692 inode_st = file.st_ino;
694 fprintf(stderr, "\n%s: read stat '%s': %s\n",
695 progname, tempfile, strerror(errno));
703 if (count == num_files)
704 file_size = file.st_size;
705 ret = read_chunks(fd, 0, file_size, chunk_buf, chunksize,
706 time_st, inode_st, tempfile);
711 curr_bytes += file_size;
713 show_rate("read", tempfile, &start_time,
714 total_bytes, curr_bytes);
716 if (++file_num >= files_in_dir)
720 show_rate("read_done", tempfile, &start_time, total_bytes, curr_bytes);
727 int main(int argc, char **argv)
729 time_t time_st = 0; /* Default timestamp */
730 size_t chunksize = ONE_MB; /* IO chunk size(defailt=1MB) */
731 char *chunk_buf; /* chunk buffer */
733 FILE *countfile = NULL;
734 unsigned long dir_num = 0, dir_num_orig = 0;/* starting directory */
737 progname = strrchr(argv[0], '/') ? strrchr(argv[0], '/') + 1 : argv[0];
738 while ((c = getopt_long(argc, argv, "c:hln:o:pqrs:t:vw",
739 long_opts, NULL)) != -1) {
742 chunksize = strtoul(optarg, NULL, 0) * ONE_MB;
743 if (chunksize == 0) {
744 fprintf(stderr, "%s: bad chunk size '%s'\n",
753 num_dirs = strtoul(optarg, NULL, 0);
755 case 'o': /* offset */
756 dir_num = strtoul(optarg, NULL, 0);
771 file_size = strtoul(optarg, NULL, 0) * ONE_MB;
772 if (file_size == 0) {
773 fprintf(stderr, "%s: bad file size '%s'\n",
779 time_st = (time_t)strtoul(optarg, NULL, 0);
794 testdir = argv[optind];
797 fprintf(stderr, "%s: pathname not given\n", progname);
801 if (!readoption && !writeoption) {
806 (void) time(&time_st);
807 printf("Timestamp: %lu\n", (unsigned long )time_st);
808 isatty_flag = isatty(STDOUT_FILENO);
810 if (!full && !num_dirs) {
811 #ifdef HAVE_EXT2FS_EXT2FS_H
812 struct mntent *tempmnt;
816 if ((fp = setmntent("/etc/mtab", "r")) == NULL) {
817 fprintf(stderr, "%s: fail to open /etc/mtab in read mode :%s\n",
818 progname, strerror(errno));
822 /* find device name using filesystem */
823 while ((tempmnt = getmntent(fp)) != NULL) {
824 if (strcmp(tempmnt->mnt_dir, testdir) == 0)
828 if (tempmnt == NULL) {
829 fprintf(stderr, "%s: no device found for '%s'\n",
835 if (ext2fs_open(tempmnt->mnt_fsname, 0, 0, 0,
836 unix_io_manager, &fs)) {
837 fprintf(stderr, "%s: unable to open ext3 fs on '%s'\n",
844 num_dirs = (fs->super->s_blocks_count +
845 fs->super->s_blocks_per_group - 1) /
846 fs->super->s_blocks_per_group;
848 printf("ext3 block groups: %u, fs blocks: %u "
849 "blocks per group: %u\n",
850 num_dirs, fs->super->s_blocks_count,
851 fs->super->s_blocks_per_group);
858 struct statfs64 statbuf;
861 * Most extN filesystems are formatted with 128MB/group
862 * (32k bitmap = 4KB blocksize * 8 bits/block) * 4KB,
863 * so this is a relatively safe default (somewhat more
864 * or less doesn't make a huge difference for testing).
866 * We want to create one directory per group, together
867 * with the "TOPDIR" feature, so that the directories
868 * are spread across the whole block device.
870 if (statfs64(testdir, &statbuf) == 0) {
871 num_dirs = 1 + (long long)statbuf.f_blocks *
873 (full ? files_in_dir * file_size : 128*ONE_MB);
875 printf("dirs: %u, fs blocks: %llu\n",
876 num_dirs, (long long)statbuf.f_blocks);
878 fprintf(stderr, "%s: unable to stat '%s': %s\n",
879 progname, testdir, strerror(errno));
883 printf("dirs: %u\n", num_dirs);
886 chunk_buf = (char *)calloc(chunksize, 1);
887 if (chunk_buf == NULL) {
888 fprintf(stderr, "Memory allocation failed for chunk_buf\n");
891 snprintf(filecount, sizeof(filecount), "%s/%s.filecount",
894 (void)mkdir(testdir, dirmode);
898 num_files = dir_num * files_in_dir;
900 printf("\n%s: %lu files already written\n",
901 progname, num_files);
903 if (dir_write(chunk_buf, chunksize, time_st, dir_num)) {
907 dir_num = dir_num_orig;
911 countfile = fopen(filecount, "r");
912 if (countfile == NULL ||
913 fscanf(countfile, "%lu", &num_files) != 1 ||
915 fprintf(stderr, "\n%s: reading %s failed :%s\n",
916 progname, filecount, strerror(errno));
917 num_files = num_dirs * files_in_dir;
919 num_files -= (dir_num * files_in_dir);
924 if (dir_read(chunk_buf, chunksize, time_st, dir_num)) {
925 fprintf(stderr, "\n%s: Data verification failed\n",