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 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
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 * ext3 Filesystem Verification Tool.
39 * This program tests the correct operation of ext3 filesystem.
40 * This tool have two working modes
43 * The full mode is basic mode in which program creates a subdirectory
44 * in the test fileysytem, writes n(files_in_dir, default=16) large(4GB) files
45 * to the directory with the test pattern at the start of each 4kb block.
46 * The test pattern contains timestamp, relative file offset and per file
47 * unique idenfifier(inode number). this continues until whole filesystem is
48 * full and then this tooll verifies that the data in all of the test files
50 * In the fast mode the tool creates a test directories with
51 * EXT3_TOPDIR_FL flag set. the number of directories equals to the number
52 * of block groups in the filesystem(e.g. 65536 directories for 8TB filesystem)
53 * and 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>
87 #include <gnu/stubs.h>
88 #include <gnu/stubs.h>
90 #ifdef HAVE_EXT2FS_EXT2FS_H
92 # include <ext2fs/ext2fs.h>
95 #define ONE_MB (1024 * 1024)
96 #define ONE_GB ((unsigned long long)(1024 * 1024 * 1024))
97 #define BLOCKSIZE 4096
99 /* Structure for writing test pattern */
101 unsigned long long bd_offset;
102 unsigned long long bd_time;
103 unsigned long long bd_inode;
105 static char *progname; /* name by which this program was run. */
106 static unsigned verbose = 1; /* prints offset in kB, operation rate */
107 static int readoption; /* run test in read-only (verify) mode */
108 static int writeoption; /* run test in write_only mode */
109 char *testdir; /* name of device to be tested. */
110 static unsigned full = 1; /* flag to full check */
111 static int errno_local; /* local copy of errno */
112 static unsigned long num_files; /* Total number of files for read/write */
113 static loff_t file_size = 4*ONE_GB; /* Size of each file */
114 static unsigned files_in_dir = 32; /* number of files in each directioy */
115 static unsigned num_dirs = 30000; /* total number of directories */
116 const int dirmode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
118 static int isatty_flag;
119 static int perms = S_IRWXU | S_IRGRP | S_IROTH;
121 static struct option const longopts[] =
123 { "chunksize", required_argument, 0, 'c' },
124 { "help", no_argument, 0, 'h' },
125 { "offset", required_argument, 0, 'o' },
126 { "long", no_argument, 0, 'l' },
127 { "partial", required_argument, 0, 'p' },
128 { "quiet", required_argument, 0, 'q' },
129 { "read", no_argument, 0, 'r' },
130 { "timestamp", required_argument, 0, 't' },
131 { "verbose", no_argument, 0, 'v' },
132 { "write", no_argument, 0, 'w' },
137 * Usages: displays help information, whenever user supply --help option in
138 * command or enters incorrect command line.
140 void usage(int status)
144 printf("\nUsage: %s [OPTION]... <filesystem path> ...\n",
146 printf("ext3 filesystem verification tool.\n"
147 "\t-t {seconds} for --timestamp, set test time"
148 "(default=current time())\n"
149 "\t-o {offset} for --offset, directory starting offset"
150 " from which tests should start\n"
151 "\t-r run test in read (verify) mode\n"
152 "\t-w run test in write (test-pattern) mode (default=r&w)\n"
154 "\t-p for --partial, for partial check (1MB files)\n"
155 "\t-l for --long, full check (4GB file with 4k blocks)\n"
156 "\t-c for --chunksize, IO chunk size (default=1048576)\n"
157 "\t-h display this help and exit\n"
158 "\t--help display this help and exit\n");
164 * open_file: Opens file in specified mode and returns fd.
166 static int open_file(const char *file, int flag)
168 fd = open(file, flag, perms);
170 fprintf(stderr, "\n%s: Open '%s' failed:%s\n",
171 progname, file, strerror(errno));
178 * Verify_chunk: Verifies test pattern in each 4kB (BLOCKSIZE) is correct.
179 * Returns 0 if test offset and timestamp is correct otherwise 1.
181 int verify_chunk(char *chunk_buf, size_t chunksize,unsigned long long chunk_off,
182 unsigned long long time_st, unsigned long long inode_st,
185 struct block_data *bd;
188 for (chunk_end = chunk_buf + chunksize - sizeof(*bd);
189 (char *)chunk_buf < chunk_end;
190 chunk_buf += BLOCKSIZE, chunk_off += BLOCKSIZE) {
191 bd = (struct block_data *)chunk_buf;
192 if ((bd->bd_offset == chunk_off) && (bd->bd_time == time_st) &&
193 (bd->bd_inode == inode_st))
195 fprintf(stderr,"\n%s: verify %s failed offset/timestamp/inode "
196 "%llu/%llu/%llu: found %llu/%llu/%llu instead\n",
197 progname, file, chunk_off, time_st, inode_st,
198 bd->bd_offset, bd->bd_time, bd->bd_inode);
205 * fill_chunk: Fills the chunk with current or user specified timestamp
206 * and offset. The test patters is filled at the beginning of
207 * each 4kB(BLOCKSIZE) blocks in chunk_buf.
209 void fill_chunk(char *chunk_buf, size_t chunksize, loff_t chunk_off,
210 time_t time_st, ino_t inode_st)
212 struct block_data *bd;
215 for (chunk_end = chunk_buf + chunksize - sizeof(*bd);
216 (char *)chunk_buf < chunk_end;
217 chunk_buf += BLOCKSIZE, chunk_off += BLOCKSIZE) {
218 bd = (struct block_data *)chunk_buf;
219 bd->bd_offset = chunk_off;
220 bd->bd_time = time_st;
221 bd->bd_inode = inode_st;
226 * write_chunk: write the chunk_buf on the device. The number of write
227 * operations are based on the parameters write_end, offset, and chunksize.
229 int write_chunks(int fd, unsigned long long offset,unsigned long long write_end,
230 char *chunk_buf, size_t chunksize, time_t time_st,
231 ino_t inode_st, const char *file)
233 unsigned long long stride;
235 stride = full ? chunksize : (ONE_GB - chunksize);
236 for (offset = offset & ~(chunksize - 1); offset < write_end;
238 if (lseek64(fd, offset, SEEK_SET) == -1) {
239 fprintf(stderr, "\n%s: lseek64(%s+%llu) failed: %s\n",
240 progname, file, offset, strerror(errno));
243 if (offset + chunksize > write_end)
244 chunksize = write_end - offset;
245 if (!full && offset > chunksize) {
246 fill_chunk(chunk_buf, chunksize, offset, time_st,
248 if (write(fd, chunk_buf, chunksize) < 0) {
249 if (errno == ENOSPC) {
254 "\n%s: write %s+%llu failed: %s\n",
255 progname, file, offset,strerror(errno));
259 if (offset + chunksize > write_end)
260 chunksize = write_end - offset;
262 fill_chunk(chunk_buf, chunksize, offset, time_st, inode_st);
263 if (write(fd, (char *) chunk_buf, chunksize) < 0) {
264 if (errno == ENOSPC) {
268 fprintf(stderr, "\n%s: write %s+%llu failed: %s\n",
269 progname, file, offset, strerror(errno));
277 * read_chunk: reads the chunk_buf from the device. The number of read
278 * operations are based on the parameters read_end, offset, and chunksize.
280 int read_chunks(int fd, unsigned long long offset, unsigned long long read_end,
281 char *chunk_buf, size_t chunksize, time_t time_st,
282 ino_t inode_st, char *file)
284 unsigned long long stride;
286 stride = full ? chunksize : (ONE_GB - chunksize);
287 for (offset = offset & ~(chunksize - 1); offset < read_end;
289 if (lseek64(fd, offset, SEEK_SET) == -1) {
290 fprintf(stderr, "\n%s: lseek64(%s+%llu) failed: %s\n",
291 progname, file, offset, strerror(errno));
294 if (offset + chunksize > read_end)
295 chunksize = read_end - offset;
296 if (!full && offset > chunksize) {
297 if (read(fd, chunk_buf, chunksize) < 0) {
299 "\n%s: read %s+%llu failed: %s\n",
300 progname, file, offset,strerror(errno));
303 if (verify_chunk(chunk_buf, chunksize, offset,
304 time_st, inode_st, file) != 0)
307 if (offset + chunksize >= read_end)
308 chunksize = read_end - offset;
310 if (read(fd, chunk_buf, chunksize) < 0) {
311 fprintf(stderr, "\n%s: read %s+%llu failed: %s\n",
312 progname, file, offset, strerror(errno));
315 if (verify_chunk(chunk_buf, chunksize, offset, time_st,
316 inode_st, file) != 0)
323 * new_file: prepares new filename using file counter and current dir.
325 char *new_file(char *tempfile, char *cur_dir, int file_num)
327 sprintf(tempfile, "%s/file%03d", cur_dir, file_num);
332 * new_dir: prepares new dir name using dir counters.
334 char *new_dir(char *tempdir, int dir_num)
336 sprintf(tempdir, "%s/dir%05d", testdir, dir_num);
341 * show_filename: Displays name of current file read/write
343 void show_filename(char *op, char *filename)
351 if (diff > 4 || verbose > 2) {
354 printf("%s File name: %s ", op, filename);
364 * dir_write: This function writes directories and files on device.
365 * it works for both full and fast modes.
367 static int dir_write(char *chunk_buf, size_t chunksize,
368 time_t time_st, unsigned long dir_num)
370 char tempfile[PATH_MAX];
371 char tempdir[PATH_MAX];
373 int file_num = 999999999;
376 #ifdef HAVE_EXT2FS_EXT2FS_H
377 if (!full && fsetflags(testdir, EXT2_TOPDIR_FL))
379 "\n%s: can't set TOPDIR_FL on %s: %s (ignoring)",
380 progname, testdir, strerror(errno));
382 for (; dir_num < num_dirs; num_files++, file_num++) {
383 if (file_num >= files_in_dir) {
384 if (dir_num == num_dirs - 1)
388 if (mkdir(new_dir(tempdir, dir_num), dirmode) < 0) {
391 if (errno != EEXIST) {
392 fprintf(stderr, "\n%s: mkdir %s : %s\n",
400 fd = open_file(new_file(tempfile, tempdir, file_num),
401 O_WRONLY | O_CREAT | O_TRUNC | O_LARGEFILE);
403 if (fd >= 0 && fstat64(fd, &file) == 0) {
404 inode_st = file.st_ino;
406 fprintf(stderr, "\n%s: write stat64 to file %s: %s",
407 progname, tempfile, strerror(errno));
412 show_filename("write", tempfile);
414 if (write_chunks(fd, 0, file_size, chunk_buf, chunksize,
415 time_st, inode_st, tempfile)) {
421 if (errno_local == ENOSPC)
427 show_filename("write", tempfile);
428 printf("\nwrite complete\n");
436 * dir_read: This function reads directories and files on device.
437 * it works for both full and fast modes.
439 static int dir_read(char *chunk_buf, size_t chunksize,
440 time_t time_st, unsigned long dir_num)
442 char tempfile[PATH_MAX];
443 char tempdir[PATH_MAX];
444 unsigned long count = 0;
449 for (count = 0; count < num_files && dir_num < num_dirs; count++) {
451 if (dir_num == num_dirs - 1)
454 new_dir(tempdir, dir_num);
458 fd = open_file(new_file(tempfile, tempdir, file_num),
459 O_RDONLY | O_LARGEFILE);
460 if (fd >= 0 && fstat64(fd, &file) == 0) {
461 inode_st = file.st_ino;
463 fprintf(stderr, "\n%s: read stat64 file '%s': %s\n",
464 progname, tempfile, strerror(errno));
469 show_filename("read", tempfile);
471 if (count == num_files)
472 file_size = file.st_size;
473 if (read_chunks(fd, 0, file_size, chunk_buf, chunksize,
474 time_st, inode_st, tempfile)) {
480 if (++file_num >= files_in_dir)
485 show_filename("read", tempfile);
486 printf("\nread complete\n");
492 int main(int argc, char **argv)
494 time_t time_st = 0; /* Default timestamp */
495 size_t chunksize = ONE_MB; /* IO chunk size(defailt=1MB) */
496 char *chunk_buf; /* chunk buffer */
498 FILE *countfile = NULL;
499 char filecount[PATH_MAX];
500 unsigned long dir_num = 0, dir_num_orig = 0;/* starting directory */
503 progname = strrchr(argv[0], '/') ? strrchr(argv[0], '/') + 1 : argv[0];
504 while ((c = (char)getopt_long(argc, argv, "t:rwvplo:h",
505 longopts, NULL)) != -1) {
508 chunksize = (strtoul(optarg, NULL, 0) * ONE_MB);
510 fprintf(stderr, "%s: Chunk size value should be"
511 "a multiple of 1MB\n", progname);
518 case 'o': /* offset */
519 dir_num = strtoul(optarg, NULL, 0);
531 time_st = (time_t)strtoul(optarg, NULL, 0);
546 testdir = argv[optind];
549 fprintf(stderr, "%s: pathname not given\n", progname);
553 if (!readoption && !writeoption) {
558 (void) time(&time_st);
559 printf("Timestamp: %lu\n", (unsigned long )time_st);
560 isatty_flag = isatty(STDOUT_FILENO);
563 #ifdef HAVE_EXT2FS_EXT2FS_H
564 struct mntent *tempmnt;
568 if ((fp = setmntent("/etc/mtab", "r")) == NULL){
569 fprintf(stderr, "%s: fail to open /etc/mtab in read"
570 "mode :%s\n", progname, strerror(errno));
574 /* find device name using filesystem */
575 while ((tempmnt = getmntent(fp)) != NULL) {
576 if (strcmp(tempmnt->mnt_dir, testdir) == 0)
580 if (tempmnt == NULL) {
581 fprintf(stderr, "%s: no device found for '%s'\n",
587 if (ext2fs_open(tempmnt->mnt_fsname, 0, 0, 0,
588 unix_io_manager, &fs)) {
589 fprintf(stderr, "%s: unable to open ext3 fs on '%s'\n",
596 num_dirs = (fs->super->s_blocks_count +
597 fs->super->s_blocks_per_group - 1) /
598 fs->super->s_blocks_per_group;
600 printf("ext3 block groups: %u, fs blocks: %u "
601 "blocks per group: %u\n",
602 num_dirs, fs->super->s_blocks_count,
603 fs->super->s_blocks_per_group);
609 struct statfs64 statbuf;
611 if (statfs64(testdir, &statbuf) == 0) {
612 num_dirs = (long long)statbuf.f_blocks *
613 statbuf.f_bsize / (128ULL << 20);
615 printf("dirs: %u, fs blocks: %llu\n",
617 (long long)statbuf.f_blocks);
619 fprintf(stderr, "%s: unable to stat '%s': %s\n",
620 progname, testdir, strerror(errno));
622 printf("dirs: %u\n", num_dirs);
630 chunk_buf = (char *)calloc(chunksize, 1);
631 if (chunk_buf == NULL) {
632 fprintf(stderr, "Memory allocation failed for chunk_buf\n");
635 sprintf(filecount, "%s/%s.filecount", testdir, progname);
637 (void)mkdir(testdir, dirmode);
641 num_files = dir_num * files_in_dir;
643 printf("\n%s: %lu files already written\n",
644 progname, num_files);
646 if (dir_write(chunk_buf, chunksize, time_st, dir_num)) {
650 countfile = fopen(filecount, "w");
651 if (countfile != NULL) {
652 if (fprintf(countfile, "%lu", num_files) < 1 ||
653 fflush(countfile) != 0) {
654 fprintf(stderr, "\n%s: writing %s failed :%s\n",
655 progname, filecount, strerror(errno));
659 dir_num = dir_num_orig;
663 countfile = fopen(filecount, "r");
664 if (countfile == NULL ||
665 fscanf(countfile, "%lu", &num_files) != 1) {
666 fprintf(stderr, "\n%s: reading %s failed :%s\n",
667 progname, filecount, strerror(errno));
668 num_files = num_dirs * files_in_dir;
670 num_files -= (dir_num * files_in_dir);
675 if (dir_read(chunk_buf, chunksize, time_st, dir_num)) {
676 fprintf(stderr, "\n%s: Data verification failed\n",