1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
32 * Copyright (c) 2011 Whamcloud, Inc.
36 * This file is part of Lustre, http://www.lustre.org/
37 * Lustre is a trademark of Sun Microsystems, Inc.
39 * lustre/utils/llverfs.c
41 * Filesystem Verification Tool.
42 * This program tests the correct operation of large filesystems and
43 * the underlying block storage device(s).
44 * This tool have two working modes
48 * In full mode, the program creates a subdirectory in the test
49 * filesystem, writes n(files_in_dir, default=32) large(4GB) files to
50 * the directory with the test pattern at the start of each 4kb block.
51 * The test pattern contains timestamp, relative file offset and per
52 * file unique identifier(inode number). This continues until the
53 * whole filesystem is full and then the tool verifies that the data
54 * in all of the test files is correct.
56 * In partial mode, the tool creates test directories with the
57 * EXT3_TOPDIR_FL flag set (if supported) to spread the directory data
58 * around the block device instead of localizing it in a single place.
59 * The number of directories equals to the number of block groups in the
60 * filesystem (e.g. 65536 directories for 8TB ext3/ext4 filesystem) and
61 * then writes a single 1MB file in each directory. The tool then verifies
62 * that the data in each file is correct.
71 #ifndef _LARGEFILE64_SOURCE
72 #define _LARGEFILE64_SOURCE
74 #ifndef _FILE_OFFSET_BITS
75 #define _FILE_OFFSET_BITS 64
92 #include <sys/types.h>
96 #include <gnu/stubs.h>
97 #include <gnu/stubs.h>
99 #ifdef HAVE_EXT2FS_EXT2FS_H
100 # include <e2p/e2p.h>
101 # include <ext2fs/ext2fs.h>
104 #define ONE_MB (1024 * 1024)
105 #define ONE_GB ((unsigned long long)(1024 * 1024 * 1024))
106 #define BLOCKSIZE 4096
108 /* Structure for writing test pattern */
110 unsigned long long bd_offset;
111 unsigned long long bd_time;
112 unsigned long long bd_inode;
114 static char *progname; /* name by which this program was run. */
115 static unsigned verbose = 1; /* prints offset in kB, operation rate */
116 static int readoption; /* run test in read-only (verify) mode */
117 static int writeoption; /* run test in write_only mode */
118 char *testdir; /* name of device to be tested. */
119 static unsigned full = 1; /* flag to full check */
120 static int error_count; /* number of IO errors hit during run */
121 char filecount[PATH_MAX]; /* file with total number of files written*/
122 static unsigned long num_files; /* Total number of files for read/write */
123 static loff_t file_size = 4*ONE_GB; /* Size of each file */
124 static unsigned files_in_dir = 32; /* number of files in each directioy */
125 static unsigned num_dirs = 30000; /* total number of directories */
126 const int dirmode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
127 static int isatty_flag;
128 static int perms = S_IRWXU | S_IRGRP | S_IROTH;
130 static struct option const longopts[] =
132 { "chunksize", required_argument, 0, 'c' },
133 { "help", no_argument, 0, 'h' },
134 { "offset", required_argument, 0, 'o' },
135 { "long", no_argument, 0, 'l' },
136 { "full", no_argument, 0, 'l' },
137 { "partial", required_argument, 0, 'p' },
138 { "quiet", required_argument, 0, 'q' },
139 { "read", no_argument, 0, 'r' },
140 { "filesize", no_argument, 0, 's' },
141 { "timestamp", required_argument, 0, 't' },
142 { "verbose", no_argument, 0, 'v' },
143 { "write", no_argument, 0, 'w' },
148 * Usages: displays help information, whenever user supply --help option in
149 * command or enters incorrect command line.
151 void usage(int status)
154 printf("\nUsage: %s [OPTION]... <filesystem path> ...\n",
156 printf("Filesystem verification tool.\n"
157 "\t-t {seconds}, --timestamp, set test time"
158 "(default=current time())\n"
159 "\t-o {offset}, --offset, directory starting offset"
160 " from which tests should start\n"
161 "\t-r, --read, run in verify mode\n"
162 "\t-w, --write, run in test-pattern mode, default=rw\n"
164 "\t-p, --partial, for partial check (1MB files)\n"
165 "\t-l, --long, --full check (4GB file with 4k blocks)\n"
166 "\t-c, --chunksize, IO chunk size in MB (default=1)\n"
167 "\t-s, --filesize, file size in MB (default=4096)\n"
168 "\t-h, --help, display this help and exit\n");
174 * open_file: Opens file in specified mode and returns fd.
176 static int open_file(const char *file, int flag)
178 int fd = open(file, flag, perms);
180 fprintf(stderr, "\n%s: Open '%s' failed:%s\n",
181 progname, file, strerror(errno));
187 * Verify_chunk: Verifies test pattern in each 4kB (BLOCKSIZE) is correct.
188 * Returns 0 if test offset and timestamp is correct otherwise 1.
190 int verify_chunk(char *chunk_buf, const size_t chunksize,
191 unsigned long long chunk_off, const unsigned long long time_st,
192 const unsigned long long inode_st, const char *file)
194 struct block_data *bd;
197 for (chunk_end = chunk_buf + chunksize - sizeof(*bd);
198 (char *)chunk_buf < chunk_end;
199 chunk_buf += BLOCKSIZE, chunk_off += BLOCKSIZE) {
200 bd = (struct block_data *)chunk_buf;
201 if ((bd->bd_offset == chunk_off) && (bd->bd_time == time_st) &&
202 (bd->bd_inode == inode_st))
204 fprintf(stderr, "\n%s: verify %s failed offset/timestamp/inode "
205 "%llu/%llu/%llu: found %llu/%llu/%llu instead\n",
206 progname, file, chunk_off, time_st, inode_st,
207 bd->bd_offset, bd->bd_time, bd->bd_inode);
214 * fill_chunk: Fills the chunk with current or user specified timestamp
215 * and offset. The test pattern is filled at the beginning of
216 * each 4kB(BLOCKSIZE) blocks in chunk_buf.
218 void fill_chunk(char *chunk_buf, size_t chunksize, loff_t chunk_off,
219 const time_t time_st, const ino_t inode_st)
221 struct block_data *bd;
224 for (chunk_end = chunk_buf + chunksize - sizeof(*bd);
225 (char *)chunk_buf < chunk_end;
226 chunk_buf += BLOCKSIZE, chunk_off += BLOCKSIZE) {
227 bd = (struct block_data *)chunk_buf;
228 bd->bd_offset = chunk_off;
229 bd->bd_time = time_st;
230 bd->bd_inode = inode_st;
235 * Write a chunk to disk, handling errors, interrupted writes, etc.
237 * If there is an IO error hit during the write, it is possible that
238 * this will just show up as a short write, and a subsequent write
239 * will return the actual error. We want to continue in the face of
240 * minor media errors so that we can validate the whole device if
241 * possible, but if there are many errors we don't want to loop forever.
243 * The error count will be returned upon exit to ensure that the
244 * media errors are detected even if nobody is looking at the output.
246 * Returns 0 on success, or -ve errno on failure.
248 int write_retry(int fd, const char *chunk_buf, size_t nrequested,
249 unsigned long long offset, const char *file)
254 nwritten = write(fd, chunk_buf, nrequested);
256 if (errno != ENOSPC) {
257 fprintf(stderr, "\n%s: write %s@%llu+%zi failed: %s\n",
258 progname, file, offset, nrequested,
260 if (error_count++ < 100)
265 if (nwritten < nrequested) {
266 fprintf(stderr, "\n%s: write %s@%llu+%zi short: %ld written\n",
267 progname, file, offset, nrequested, nwritten);
269 chunk_buf += nwritten;
270 nrequested -= nwritten;
278 * write_chunks: write the chunk_buf on the device. The number of write
279 * operations are based on the parameters write_end, offset, and chunksize.
281 * Returns 0 on success, or -ve error number on failure.
283 int write_chunks(int fd, unsigned long long offset,unsigned long long write_end,
284 char *chunk_buf, size_t chunksize, const time_t time_st,
285 const ino_t inode_st, const char *file)
287 unsigned long long stride;
289 stride = full ? chunksize : (ONE_GB - chunksize);
290 for (offset = offset & ~(chunksize - 1); offset < write_end;
294 if (lseek64(fd, offset, SEEK_SET) == -1) {
295 fprintf(stderr, "\n%s: lseek64(%s+%llu) failed: %s\n",
296 progname, file, offset, strerror(errno));
299 if (offset + chunksize > write_end)
300 chunksize = write_end - offset;
301 if (!full && offset > chunksize) {
302 fill_chunk(chunk_buf, chunksize, offset, time_st,
304 ret = write_retry(fd, chunk_buf, chunksize,offset,file);
308 if (offset + chunksize > write_end)
309 chunksize = write_end - offset;
311 fill_chunk(chunk_buf, chunksize, offset, time_st, inode_st);
312 ret = write_retry(fd, chunk_buf, chunksize, offset, file);
320 * read_chunk: reads the chunk_buf from the device. The number of read
321 * operations are based on the parameters read_end, offset, and chunksize.
323 int read_chunks(int fd, unsigned long long offset, unsigned long long read_end,
324 char *chunk_buf, size_t chunksize, const time_t time_st,
325 const ino_t inode_st, const char *file)
327 unsigned long long stride;
329 stride = full ? chunksize : (ONE_GB - chunksize);
330 for (offset = offset & ~(chunksize - 1); offset < read_end;
334 if (lseek64(fd, offset, SEEK_SET) == -1) {
335 fprintf(stderr, "\n%s: lseek64(%s+%llu) failed: %s\n",
336 progname, file, offset, strerror(errno));
339 if (offset + chunksize > read_end)
340 chunksize = read_end - offset;
342 if (!full && offset > chunksize) {
343 nread = read(fd, chunk_buf, chunksize);
345 fprintf(stderr,"\n%s: read %s@%llu+%zi failed: "
346 "%s\n", progname, file, offset,
347 chunksize, strerror(errno));
351 if (nread < chunksize) {
352 fprintf(stderr, "\n%s: read %s@%llu+%zi short: "
353 "%zi read\n", progname, file, offset,
357 if (verify_chunk(chunk_buf, nread, offset, time_st,
358 inode_st, file) != 0) {
363 /* Need to reset position after read error */
364 if (nread < chunksize &&
365 lseek64(fd, offset, SEEK_SET) == -1) {
367 "\n%s: lseek64(%s@%llu) failed: %s\n",
368 progname, file, offset,strerror(errno));
371 if (offset + chunksize >= read_end)
372 chunksize = read_end - offset;
374 nread = read(fd, chunk_buf, chunksize);
376 fprintf(stderr, "\n%s: read %s@%llu+%zi failed: %s\n",
377 progname, file, offset, chunksize,
382 if (nread < chunksize) {
383 fprintf(stderr, "\n%s: read %s@%llu+%zi short: "
384 "%zi read\n", progname, file, offset,
389 if (verify_chunk(chunk_buf, nread, offset, time_st,
390 inode_st, file) != 0) {
398 * new_file: prepares new filename using file counter and current dir.
400 char *new_file(char *tempfile, char *cur_dir, int file_num)
402 sprintf(tempfile, "%s/file%03d", cur_dir, file_num);
407 * new_dir: prepares new dir name using dir counters.
409 char *new_dir(char *tempdir, int dir_num)
411 sprintf(tempdir, "%s/dir%05d", testdir, dir_num);
416 * calc_total_bytes: calculates total bytes that need to be
417 * written into or read from the filesystem.
419 static unsigned long long calc_total_bytes(const char *op)
421 unsigned long long total_bytes = 0;
422 struct statfs64 statbuf;
425 if (statfs64(testdir, &statbuf) == 0) {
426 if (strcmp(op, "write") == 0)
427 total_bytes = (unsigned long long)
428 (statbuf.f_bavail * statbuf.f_bsize);
429 else if (strcmp(op, "read") == 0)
430 total_bytes = (unsigned long long)
431 (statbuf.f_blocks * statbuf.f_bsize);
433 fprintf(stderr, "\n%s: invalid operation: %s\n",
438 fprintf(stderr, "\n%s: unable to stat %s: %s\n",
439 progname, testdir, strerror(errno));
443 total_bytes = num_dirs * files_in_dir * file_size;
450 * show_rate: displays the current read/write file name and performance,
451 * along with an estimate of how long the whole read/write operation
454 void show_rate(char *op, char *filename, const struct timeval *start_time,
455 const unsigned long long total_bytes,
456 const unsigned long long curr_bytes)
458 static struct timeval last_time;
459 static unsigned long long last_bytes;
461 struct timeval curr_time;
462 double curr_delta, overall_delta, curr_rate, overall_rate;
463 double total_time, remain_time;
464 int remain_hours, remain_minutes, remain_seconds;
466 if (last_time.tv_sec == 0)
467 last_time = *start_time;
469 if (last_op != op[0]) {
471 last_time = *start_time;
475 gettimeofday(&curr_time, NULL);
477 curr_delta = (curr_time.tv_sec - last_time.tv_sec) +
478 (double)(curr_time.tv_usec - last_time.tv_usec) / 1000000;
480 overall_delta = (curr_time.tv_sec - start_time->tv_sec) +
481 (double)(curr_time.tv_usec - start_time->tv_usec) / 1000000;
483 curr_rate = (curr_bytes - last_bytes) / curr_delta;
484 overall_rate = curr_bytes / overall_delta;
486 if (curr_rate == 0) {
487 last_time = curr_time;
490 total_time = total_bytes / curr_rate;
491 remain_time = total_time - overall_delta;
493 remain_hours = remain_time / 3600;
494 remain_minutes = (remain_time - remain_hours * 3600) / 60;
495 remain_seconds = (remain_time - remain_hours * 3600 -
496 remain_minutes * 60);
498 if (curr_delta > 4 || verbose > 2) {
502 printf("%s filename: %s, current %5g MB/s, overall %5g MB/s, "
503 "est %u:%u:%u left", op, filename,
504 curr_rate / ONE_MB, overall_rate / ONE_MB,
505 remain_hours, remain_minutes, remain_seconds);
513 last_time = curr_time;
514 last_bytes = curr_bytes;
518 * dir_write: This function writes directories and files on device.
519 * it works for both full and partial modes.
521 static int dir_write(char *chunk_buf, size_t chunksize,
522 time_t time_st, unsigned long dir_num)
524 char tempfile[PATH_MAX];
525 char tempdir[PATH_MAX];
528 int file_num = 999999999;
530 struct timeval start_time;
531 unsigned long long total_bytes;
532 unsigned long long curr_bytes = 0;
534 #ifdef HAVE_EXT2FS_EXT2FS_H
535 if (!full && fsetflags(testdir, EXT2_TOPDIR_FL))
537 "\n%s: can't set TOPDIR_FL on %s: %s (ignoring)",
538 progname, testdir, strerror(errno));
540 countfile = fopen(filecount, "w");
541 if (countfile == NULL) {
542 fprintf(stderr, "\n%s: creating %s failed :%s\n",
543 progname, filecount, strerror(errno));
546 /* reserve space for the countfile */
547 if (fprintf(countfile, "%lu", num_files) < 1 ||
548 fflush(countfile) != 0) {
549 fprintf(stderr, "\n%s: writing %s failed :%s\n",
550 progname, filecount, strerror(errno));
554 /* calculate total bytes that need to be written */
555 total_bytes = calc_total_bytes("write");
556 if (total_bytes <= 0) {
557 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",
587 fd = open_file(new_file(tempfile, tempdir, file_num),
588 O_WRONLY | O_CREAT | O_TRUNC | O_LARGEFILE);
590 if (fstat64(fd, &file) == 0) {
591 inode_st = file.st_ino;
593 fprintf(stderr, "\n%s: write stat '%s': %s",
594 progname, tempfile, strerror(errno));
602 ret = write_chunks(fd, 0, file_size, chunk_buf, chunksize,
603 time_st, inode_st, tempfile);
608 curr_bytes = total_bytes;
612 curr_bytes += file_size;
614 show_rate("write", tempfile, &start_time,
615 total_bytes, curr_bytes);
617 fseek(countfile, 0, SEEK_SET);
618 if (fprintf(countfile, "%lu", num_files) < 1 ||
619 fflush(countfile) != 0) {
620 fprintf(stderr, "\n%s: writing %s failed :%s\n",
621 progname, filecount, strerror(errno));
628 show_rate("write", tempfile, &start_time,
629 total_bytes, curr_bytes);
630 printf("\nwrite complete\n");
638 * dir_read: This function reads directories and files on device.
639 * it works for both full and partial modes.
641 static int dir_read(char *chunk_buf, size_t chunksize,
642 time_t time_st, unsigned long dir_num)
644 char tempfile[PATH_MAX];
645 char tempdir[PATH_MAX];
646 unsigned long count = 0;
650 struct timeval start_time;
651 unsigned long long total_bytes;
652 unsigned long long curr_bytes = 0;
654 /* calculate total bytes that need to be read */
655 total_bytes = calc_total_bytes("read");
656 if (total_bytes <= 0) {
657 fprintf(stderr, "\n%s: unable to calculate total bytes\n",
663 total_bytes -= dir_num * files_in_dir * file_size;
665 gettimeofday(&start_time, NULL);
666 for (count = 0; count < num_files && dir_num < num_dirs; count++) {
670 if (dir_num == num_dirs - 1)
673 new_dir(tempdir, dir_num);
677 fd = open_file(new_file(tempfile, tempdir, file_num),
678 O_RDONLY | O_LARGEFILE);
680 if (fstat64(fd, &file) == 0) {
681 inode_st = file.st_ino;
683 fprintf(stderr, "\n%s: read stat '%s': %s\n",
684 progname, tempfile, strerror(errno));
692 if (count == num_files)
693 file_size = file.st_size;
694 ret = read_chunks(fd, 0, file_size, chunk_buf, chunksize,
695 time_st, inode_st, tempfile);
700 curr_bytes += file_size;
702 show_rate("read", tempfile, &start_time,
703 total_bytes, curr_bytes);
705 if (++file_num >= files_in_dir)
710 show_rate("read", tempfile, &start_time,
711 total_bytes, curr_bytes);
712 printf("\nread complete\n");
718 int main(int argc, char **argv)
720 time_t time_st = 0; /* Default timestamp */
721 size_t chunksize = ONE_MB; /* IO chunk size(defailt=1MB) */
722 char *chunk_buf; /* chunk buffer */
724 FILE *countfile = NULL;
725 unsigned long dir_num = 0, dir_num_orig = 0;/* starting directory */
728 progname = strrchr(argv[0], '/') ? strrchr(argv[0], '/') + 1 : argv[0];
729 while ((c = getopt_long(argc, argv, "c:hlo:pqrs:t:vw",
730 longopts, NULL)) != -1) {
733 chunksize = strtoul(optarg, NULL, 0) * ONE_MB;
734 if (chunksize == 0) {
735 fprintf(stderr, "%s: bad chunk size '%s'\n",
743 case 'o': /* offset */
744 dir_num = strtoul(optarg, NULL, 0);
759 file_size = strtoul(optarg, NULL, 0) * ONE_MB;
760 if (file_size == 0) {
761 fprintf(stderr, "%s: bad file size '%s'\n",
767 time_st = (time_t)strtoul(optarg, NULL, 0);
782 testdir = argv[optind];
785 fprintf(stderr, "%s: pathname not given\n", progname);
789 if (!readoption && !writeoption) {
794 (void) time(&time_st);
795 printf("Timestamp: %lu\n", (unsigned long )time_st);
796 isatty_flag = isatty(STDOUT_FILENO);
799 #ifdef HAVE_EXT2FS_EXT2FS_H
800 struct mntent *tempmnt;
804 if ((fp = setmntent("/etc/mtab", "r")) == NULL){
805 fprintf(stderr, "%s: fail to open /etc/mtab in read"
806 "mode :%s\n", progname, strerror(errno));
810 /* find device name using filesystem */
811 while ((tempmnt = getmntent(fp)) != NULL) {
812 if (strcmp(tempmnt->mnt_dir, testdir) == 0)
816 if (tempmnt == NULL) {
817 fprintf(stderr, "%s: no device found for '%s'\n",
823 if (ext2fs_open(tempmnt->mnt_fsname, 0, 0, 0,
824 unix_io_manager, &fs)) {
825 fprintf(stderr, "%s: unable to open ext3 fs on '%s'\n",
832 num_dirs = (fs->super->s_blocks_count +
833 fs->super->s_blocks_per_group - 1) /
834 fs->super->s_blocks_per_group;
836 printf("ext3 block groups: %u, fs blocks: %u "
837 "blocks per group: %u\n",
838 num_dirs, fs->super->s_blocks_count,
839 fs->super->s_blocks_per_group);
845 struct statfs64 statbuf;
847 if (statfs64(testdir, &statbuf) == 0) {
848 num_dirs = (long long)statbuf.f_blocks *
849 statbuf.f_bsize / (128ULL << 20);
851 printf("dirs: %u, fs blocks: %llu\n",
853 (long long)statbuf.f_blocks);
855 fprintf(stderr, "%s: unable to stat '%s': %s\n",
856 progname, testdir, strerror(errno));
858 printf("dirs: %u\n", num_dirs);
862 chunk_buf = (char *)calloc(chunksize, 1);
863 if (chunk_buf == NULL) {
864 fprintf(stderr, "Memory allocation failed for chunk_buf\n");
867 sprintf(filecount, "%s/%s.filecount", testdir, progname);
869 (void)mkdir(testdir, dirmode);
873 num_files = dir_num * files_in_dir;
875 printf("\n%s: %lu files already written\n",
876 progname, num_files);
878 if (dir_write(chunk_buf, chunksize, time_st, dir_num)) {
882 dir_num = dir_num_orig;
886 countfile = fopen(filecount, "r");
887 if (countfile == NULL ||
888 fscanf(countfile, "%lu", &num_files) != 1 ||
890 fprintf(stderr, "\n%s: reading %s failed :%s\n",
891 progname, filecount, strerror(errno));
892 num_files = num_dirs * files_in_dir;
894 num_files -= (dir_num * files_in_dir);
899 if (dir_read(chunk_buf, chunksize, time_st, dir_num)) {
900 fprintf(stderr, "\n%s: Data verification failed\n",