Whamcloud - gitweb
LU-3365 lmv: support DNE with HSM.
[fs/lustre-release.git] / lustre / utils / llverfs.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9  *
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).
15  *
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
19  *
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
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/utils/llverfs.c
37  *
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
42  * 1. full mode
43  * 2. partial mode
44  *
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.
52  *
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.
60  */
61
62 #ifndef _GNU_SOURCE
63 #define _GNU_SOURCE
64 #endif
65 #ifndef LUSTRE_UTILS
66 #define LUSTRE_UTILS
67 #endif
68 #ifndef _LARGEFILE64_SOURCE
69 #define _LARGEFILE64_SOURCE
70 #endif
71 #ifndef _FILE_OFFSET_BITS
72 #define _FILE_OFFSET_BITS 64
73 #endif
74
75 #include <features.h>
76 #include <stdlib.h>
77 #include <stdio.h>
78 #include <string.h>
79 #include <ctype.h>
80 #include <fcntl.h>
81 #include <unistd.h>
82 #include <limits.h>
83 #include <errno.h>
84 #include <fcntl.h>
85 #include <getopt.h>
86 #include <time.h>
87 #include <dirent.h>
88 #include <mntent.h>
89 #include <sys/types.h>
90 #include <sys/stat.h>
91 #include <sys/vfs.h>
92 #include <sys/time.h>
93 #include <gnu/stubs.h>
94 #include <gnu/stubs.h>
95
96 #ifdef HAVE_EXT2FS_EXT2FS_H
97 #  include <e2p/e2p.h>
98 #  include <ext2fs/ext2fs.h>
99 #endif
100
101 #define ONE_MB (1024 * 1024)
102 #define ONE_GB ((unsigned long long)(1024 * 1024 * 1024))
103 #define BLOCKSIZE 4096
104
105 /* Structure for writing test pattern */
106 struct block_data {
107         unsigned long long bd_offset;
108         unsigned long long bd_time;
109         unsigned long long bd_inode;
110 };
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;
126
127 static struct option const longopts[] =
128 {
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' },
141         { 0, 0, 0, 0}
142 };
143
144 /*
145  * Usages: displays help information, whenever user supply --help option in
146  * command or enters incorrect command line.
147  */
148 void usage(int status)
149 {
150         if (status != 0) {
151                 printf("\nUsage: %s [OPTION]... <filesystem path> ...\n",
152                        progname);
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"
160                        "\t-v, --verbose\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");
166         }
167         exit(status);
168 }
169
170 /*
171  * open_file: Opens file in specified mode and returns fd.
172  */
173 static int open_file(const char *file, int flag)
174 {
175         int fd = open(file, flag, perms);
176         if (fd < 0) {
177                 fprintf(stderr, "\n%s: Open '%s' failed:%s\n",
178                         progname, file, strerror(errno));
179         }
180         return (fd);
181 }
182
183 /*
184  * Verify_chunk: Verifies test pattern in each 4kB (BLOCKSIZE) is correct.
185  * Returns 0 if test offset and timestamp is correct otherwise 1.
186  */
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)
190 {
191         struct block_data *bd;
192         char *chunk_end;
193
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))
200                         continue;
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);
205                 return 1;
206         }
207         return 0;
208 }
209
210 /*
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.
214  */
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)
217 {
218         struct block_data *bd;
219         char *chunk_end;
220
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;
228         }
229 }
230
231 /*
232  * Write a chunk to disk, handling errors, interrupted writes, etc.
233  *
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.
239  *
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.
242  *
243  * Returns 0 on success, or -ve errno on failure.
244  */
245 int write_retry(int fd, const char *chunk_buf, size_t nrequested,
246                 unsigned long long offset, const char *file)
247 {
248         long nwritten;
249
250 retry:
251         nwritten = write(fd, chunk_buf, nrequested);
252         if (nwritten < 0) {
253                 if (errno != ENOSPC) {
254                         fprintf(stderr, "\n%s: write %s@%llu+%zi failed: %s\n",
255                                 progname, file, offset, nrequested,
256                                 strerror(errno));
257                         if (error_count++ < 100)
258                                 return 0;
259                 }
260                 return -errno;
261         }
262         if (nwritten < nrequested) {
263                 fprintf(stderr, "\n%s: write %s@%llu+%zi short: %ld written\n",
264                         progname, file, offset, nrequested, nwritten);
265                 offset += nwritten;
266                 chunk_buf += nwritten;
267                 nrequested -= nwritten;
268                 goto retry;
269         }
270
271         return 0;
272 }
273
274 /*
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.
277  *
278  * Returns 0 on success, or -ve error number on failure.
279  */
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)
283 {
284         unsigned long long stride;
285
286         stride = full ? chunksize : (ONE_GB - chunksize);
287         for (offset = offset & ~(chunksize - 1); offset < write_end;
288              offset += stride) {
289                 int ret;
290
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));
294                         return -errno;
295                 }
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,
300                                    inode_st);
301                         ret = write_retry(fd, chunk_buf, chunksize,offset,file);
302                         if (ret < 0)
303                                 return ret;
304                         offset += chunksize;
305                         if (offset + chunksize > write_end)
306                                 chunksize = write_end - offset;
307                 }
308                 fill_chunk(chunk_buf, chunksize, offset, time_st, inode_st);
309                 ret = write_retry(fd, chunk_buf, chunksize, offset, file);
310                 if (ret < 0)
311                         return ret;
312         }
313         return 0;
314 }
315
316 /*
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.
319  */
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)
323 {
324         unsigned long long stride;
325
326         stride = full ? chunksize : (ONE_GB - chunksize);
327         for (offset = offset & ~(chunksize - 1); offset < read_end;
328              offset += stride) {
329                 ssize_t nread;
330
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));
334                         return 1;
335                 }
336                 if (offset + chunksize > read_end)
337                         chunksize = read_end - offset;
338
339                 if (!full && offset > chunksize) {
340                         nread = read(fd, chunk_buf, chunksize);
341                         if (nread < 0) {
342                                 fprintf(stderr,"\n%s: read %s@%llu+%zi failed: "
343                                         "%s\n", progname, file, offset,
344                                         chunksize, strerror(errno));
345                                 error_count++;
346                                 return 1;
347                         }
348                         if (nread < chunksize) {
349                                 fprintf(stderr, "\n%s: read %s@%llu+%zi short: "
350                                         "%zi read\n", progname, file, offset,
351                                         chunksize, nread);
352                                 error_count++;
353                         }
354                         if (verify_chunk(chunk_buf, nread, offset, time_st,
355                                          inode_st, file) != 0) {
356                                 return 1;
357                         }
358                         offset += chunksize;
359
360                         /* Need to reset position after read error */
361                         if (nread < chunksize &&
362                             lseek64(fd, offset, SEEK_SET) == -1) {
363                                 fprintf(stderr,
364                                         "\n%s: lseek64(%s@%llu) failed: %s\n",
365                                         progname, file, offset,strerror(errno));
366                                 return 1;
367                         }
368                         if (offset + chunksize >= read_end)
369                                 chunksize = read_end - offset;
370                 }
371                 nread = read(fd, chunk_buf, chunksize);
372                 if (nread < 0) {
373                         fprintf(stderr, "\n%s: read %s@%llu+%zi failed: %s\n",
374                                 progname, file, offset, chunksize,
375                                 strerror(errno));
376                         error_count++;
377                         return 1;
378                 }
379                 if (nread < chunksize) {
380                         fprintf(stderr, "\n%s: read %s@%llu+%zi short: "
381                                 "%zi read\n", progname, file, offset,
382                                 chunksize, nread);
383                         error_count++;
384                 }
385
386                 if (verify_chunk(chunk_buf, nread, offset, time_st,
387                                  inode_st, file) != 0) {
388                         return 1;
389                 }
390         }
391         return 0;
392 }
393
394 /*
395  * new_file: prepares new filename using file counter and current dir.
396  */
397 char *new_file(char *tempfile, char *cur_dir, int file_num)
398 {
399         sprintf(tempfile, "%s/file%03d", cur_dir, file_num);
400         return tempfile;
401 }
402
403 /*
404  * new_dir: prepares new dir name using dir counters.
405  */
406 char *new_dir(char *tempdir, int dir_num)
407 {
408         sprintf(tempdir, "%s/dir%05d", testdir, dir_num);
409         return tempdir;
410 }
411
412 /*
413  * calc_total_bytes: calculates total bytes that need to be
414  * written into or read from the filesystem.
415  */
416 static unsigned long long calc_total_bytes(const char *op)
417 {
418         unsigned long long total_bytes = 0;
419         struct statfs64 statbuf;
420
421         if (full) {
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);
429                         else {
430                                 fprintf(stderr, "\n%s: invalid operation: %s\n",
431                                         progname, op);
432                                 return -1;
433                         }
434                 } else {
435                         fprintf(stderr, "\n%s: unable to stat %s: %s\n",
436                                 progname, testdir, strerror(errno));
437                         return -errno;
438                 }
439         } else {
440                 total_bytes = num_dirs * files_in_dir * file_size;
441         }
442
443         return total_bytes;
444 }
445
446 /*
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
449  * will continue.
450  */
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)
454 {
455         static struct timeval last_time;
456         static unsigned long long last_bytes;
457         static char last_op;
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;
462
463         if (last_time.tv_sec == 0)
464                 last_time = *start_time;
465
466         if (last_op != op[0]) {
467                 last_bytes = 0;
468                 last_time = *start_time;
469                 last_op = op[0];
470         }
471
472         gettimeofday(&curr_time, NULL);
473
474         curr_delta = (curr_time.tv_sec - last_time.tv_sec) +
475                 (double)(curr_time.tv_usec - last_time.tv_usec) / 1000000;
476
477         overall_delta = (curr_time.tv_sec - start_time->tv_sec) +
478                 (double)(curr_time.tv_usec - start_time->tv_usec) / 1000000;
479
480         curr_rate = (curr_bytes - last_bytes) / curr_delta;
481         overall_rate = curr_bytes / overall_delta;
482
483         if (curr_rate == 0) {
484                 last_time = curr_time;
485                 return;
486         }
487         total_time = total_bytes / curr_rate;
488         remain_time = total_time - overall_delta;
489
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);
494
495         if (curr_delta > 4 || verbose > 2) {
496                 if (isatty_flag)
497                         printf("\r");
498
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);
503
504                 if (isatty_flag)
505                         fflush(stdout);
506                 else
507                         printf("\n");
508         }
509
510         last_time = curr_time;
511         last_bytes = curr_bytes;
512 }
513
514 /*
515  * dir_write: This function writes directories and files on device.
516  * it works for both full and partial modes.
517  */
518 static int dir_write(char *chunk_buf, size_t chunksize,
519                      time_t time_st, unsigned long dir_num)
520 {
521         char tempfile[PATH_MAX];
522         char tempdir[PATH_MAX];
523         FILE *countfile;
524         struct stat64 file;
525         int file_num = 999999999;
526         ino_t inode_st = 0;
527         struct timeval start_time;
528         unsigned long long total_bytes;
529         unsigned long long curr_bytes = 0;
530         int rc = 0;
531
532 #ifdef HAVE_EXT2FS_EXT2FS_H
533         if (!full && fsetflags(testdir, EXT2_TOPDIR_FL))
534                 fprintf(stderr,
535                         "\n%s: can't set TOPDIR_FL on %s: %s (ignoring)",
536                         progname, testdir, strerror(errno));
537 #endif
538         countfile = fopen(filecount, "w");
539         if (countfile == NULL) {
540                 fprintf(stderr, "\n%s: creating %s failed :%s\n",
541                         progname, filecount, strerror(errno));
542                 return 5;
543         }
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));
549                 rc = 6;
550                 goto out;
551         }
552
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",
557                         progname);
558                 rc = 7;
559                 goto out;
560         }
561
562         if (!full && (dir_num != 0))
563                 total_bytes -= dir_num * files_in_dir * file_size;
564
565         gettimeofday(&start_time, NULL);
566         for (; dir_num < num_dirs; num_files++, file_num++) {
567                 int fd, ret;
568
569                 if (file_num >= files_in_dir) {
570                         if (dir_num == num_dirs - 1)
571                                 break;
572
573                         file_num = 0;
574                         if (mkdir(new_dir(tempdir, dir_num), dirmode) < 0) {
575                                 if (errno == ENOSPC)
576                                         break;
577                                 if (errno != EEXIST) {
578                                         fprintf(stderr, "\n%s: mkdir %s : %s\n",
579                                                 progname, tempdir,
580                                                 strerror(errno));
581                                         rc = 1;
582                                         goto out;
583                                 }
584                         }
585                         dir_num++;
586                 }
587
588                 fd = open_file(new_file(tempfile, tempdir, file_num),
589                                O_WRONLY | O_CREAT | O_TRUNC | O_LARGEFILE);
590                 if (fd >= 0) {
591                         if (fstat64(fd, &file) == 0) {
592                                 inode_st = file.st_ino;
593                         } else {
594                                 fprintf(stderr, "\n%s: write stat '%s': %s",
595                                         progname, tempfile, strerror(errno));
596                                 close(fd);
597                                 break;
598                         }
599                 } else {
600                         break;
601                 }
602
603                 ret = write_chunks(fd, 0, file_size, chunk_buf, chunksize,
604                                    time_st, inode_st, tempfile);
605                 close(fd);
606                 if (ret < 0) {
607                         if (ret != -ENOSPC) {
608                                 rc = 1;
609                                 goto out;
610                         }
611                         curr_bytes = total_bytes;
612                         break;
613                 }
614
615                 curr_bytes += file_size;
616                 if (verbose > 1)
617                         show_rate("write", tempfile, &start_time,
618                                   total_bytes, curr_bytes);
619
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));
625                 }
626         }
627
628         if (verbose) {
629                 verbose++;
630                 show_rate("write", tempfile, &start_time,
631                           total_bytes, curr_bytes);
632                 printf("\nwrite complete\n");
633                 verbose--;
634         }
635
636 out:
637         fclose(countfile);
638
639         return rc;
640 }
641
642 /*
643  * dir_read: This function reads directories and files on device.
644  * it works for both full and partial modes.
645  */
646 static int dir_read(char *chunk_buf, size_t chunksize,
647                     time_t time_st, unsigned long dir_num)
648 {
649         char tempfile[PATH_MAX];
650         char tempdir[PATH_MAX];
651         unsigned long count = 0;
652         struct stat64 file;
653         int file_num = 0;
654         ino_t inode_st = 0;
655         struct timeval start_time;
656         unsigned long long total_bytes;
657         unsigned long long curr_bytes = 0;
658
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",
663                         progname);
664                 return 1;
665         }
666
667         if (dir_num != 0)
668                 total_bytes -= dir_num * files_in_dir * file_size;
669
670         gettimeofday(&start_time, NULL);
671         for (count = 0; count < num_files && dir_num < num_dirs; count++) {
672                 int fd, ret;
673
674                 if (file_num == 0) {
675                         if (dir_num == num_dirs - 1)
676                                 break;
677
678                         new_dir(tempdir, dir_num);
679                         dir_num++;
680                 }
681
682                 fd = open_file(new_file(tempfile, tempdir, file_num),
683                                O_RDONLY | O_LARGEFILE);
684                 if (fd >= 0) {
685                         if (fstat64(fd, &file) == 0) {
686                                 inode_st = file.st_ino;
687                         } else {
688                                 fprintf(stderr, "\n%s: read stat '%s': %s\n",
689                                         progname, tempfile, strerror(errno));
690                                 close(fd);
691                                 return 1;
692                         }
693                 } else {
694                         break;
695                 }
696
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);
701                 close(fd);
702                 if (ret)
703                         return 1;
704
705                 curr_bytes += file_size;
706                 if (verbose > 1)
707                         show_rate("read", tempfile, &start_time,
708                                   total_bytes, curr_bytes);
709
710                 if (++file_num >= files_in_dir)
711                         file_num = 0;
712         }
713         if (verbose > 1){
714                 verbose++;
715                 show_rate("read", tempfile, &start_time,
716                           total_bytes, curr_bytes);
717                 printf("\nread complete\n");
718                 verbose--;
719         }
720         return 0;
721 }
722
723 int main(int argc, char **argv)
724 {
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 */
728         int error = 0;
729         FILE *countfile = NULL;
730         unsigned long dir_num = 0, dir_num_orig = 0;/* starting directory */
731         int c;
732
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) {
736                 switch (c) {
737                 case 'c':
738                         chunksize = strtoul(optarg, NULL, 0) * ONE_MB;
739                         if (chunksize == 0) {
740                                 fprintf(stderr, "%s: bad chunk size '%s'\n",
741                                         optarg, progname);
742                                 return -1;
743                         }
744                         break;
745                 case 'l':
746                         full = 1;
747                         break;
748                 case 'o': /* offset */
749                         dir_num = strtoul(optarg, NULL, 0);
750                         break;
751                 case 'p':
752                         file_size = ONE_MB;
753                         chunksize = ONE_MB;
754                         files_in_dir = 1;
755                         full = 0;
756                         break;
757                 case 'q':
758                         verbose = 0;
759                         break;
760                 case 'r':
761                         readoption = 1;
762                         break;
763                 case 's':
764                         file_size = strtoul(optarg, NULL, 0) * ONE_MB;
765                         if (file_size == 0) {
766                                 fprintf(stderr, "%s: bad file size '%s'\n",
767                                         optarg, progname);
768                                 return -1;
769                         }
770                         break;
771                 case 't':
772                         time_st = (time_t)strtoul(optarg, NULL, 0);
773                         break;
774                 case 'v':
775                         verbose++;
776                         break;
777                 case 'w':
778                         writeoption = 1;
779                         break;
780
781                 case 'h':
782                 default:
783                         usage(1);
784                         return 0;
785                 }
786         }
787         testdir = argv[optind];
788
789         if (!testdir) {
790                 fprintf(stderr, "%s: pathname not given\n", progname);
791                 usage(1);
792                 return -1;
793         }
794         if (!readoption && !writeoption) {
795                 readoption = 1;
796                 writeoption = 1;
797         }
798         if (!time_st)
799                 (void) time(&time_st);
800         printf("Timestamp: %lu\n", (unsigned long )time_st);
801         isatty_flag = isatty(STDOUT_FILENO);
802
803         if (!full) {
804 #ifdef HAVE_EXT2FS_EXT2FS_H
805                 struct mntent *tempmnt;
806                 FILE *fp = NULL;
807                 ext2_filsys fs;
808
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));
812                         goto guess;
813                 }
814
815                 /* find device name using filesystem */
816                 while ((tempmnt = getmntent(fp)) != NULL) {
817                         if (strcmp(tempmnt->mnt_dir, testdir) == 0)
818                                 break;
819                 }
820
821                 if (tempmnt == NULL) {
822                         fprintf(stderr, "%s: no device found for '%s'\n",
823                                 progname, testdir);
824                         endmntent(fp);
825                         goto guess;
826                 }
827
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",
831                                 progname, testdir);
832                         endmntent(fp);
833                         goto guess;
834                 }
835                 endmntent(fp);
836
837                 num_dirs = (fs->super->s_blocks_count +
838                             fs->super->s_blocks_per_group - 1) /
839                         fs->super->s_blocks_per_group;
840                 if (verbose)
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);
845                 ext2fs_close(fs);
846 #else
847                 goto guess;
848 #endif
849                 if (0) { /* ugh */
850                         struct statfs64 statbuf;
851 guess:
852                         if (statfs64(testdir, &statbuf) == 0) {
853                                 num_dirs = (long long)statbuf.f_blocks *
854                                         statbuf.f_bsize / (128ULL << 20);
855                                 if (verbose)
856                                         printf("dirs: %u, fs blocks: %llu\n",
857                                                num_dirs,
858                                                (long long)statbuf.f_blocks);
859                         } else {
860                                 fprintf(stderr, "%s: unable to stat '%s': %s\n",
861                                         progname, testdir, strerror(errno));
862                                 if (verbose)
863                                         printf("dirs: %u\n", num_dirs);
864                         }
865                 }
866         }
867         chunk_buf = (char *)calloc(chunksize, 1);
868         if (chunk_buf == NULL) {
869                 fprintf(stderr, "Memory allocation failed for chunk_buf\n");
870                 return 4;
871         }
872         snprintf(filecount, sizeof(filecount), "%s/%s.filecount",
873                  testdir, progname);
874         if (writeoption) {
875                 (void)mkdir(testdir, dirmode);
876
877                 unlink(filecount);
878                 if (dir_num != 0) {
879                         num_files = dir_num * files_in_dir;
880                         if (verbose)
881                                 printf("\n%s: %lu files already written\n",
882                                        progname, num_files);
883                 }
884                 if (dir_write(chunk_buf, chunksize, time_st, dir_num)) {
885                         error = 3;
886                         goto out;
887                 }
888                 dir_num = dir_num_orig;
889         }
890         if (readoption) {
891                 if (!writeoption) {
892                         countfile = fopen(filecount, "r");
893                         if (countfile == NULL ||
894                             fscanf(countfile, "%lu", &num_files) != 1 ||
895                             num_files == 0) {
896                                 fprintf(stderr, "\n%s: reading %s failed :%s\n",
897                                         progname, filecount, strerror(errno));
898                                 num_files = num_dirs * files_in_dir;
899                         } else {
900                                 num_files -= (dir_num * files_in_dir);
901                         }
902                         if (countfile)
903                                 fclose(countfile);
904                 }
905                 if (dir_read(chunk_buf, chunksize, time_st, dir_num)) {
906                         fprintf(stderr, "\n%s: Data verification failed\n",
907                                 progname) ;
908                         error = 2;
909                         goto out;
910                 }
911         }
912         error = error_count;
913 out:
914         free(chunk_buf);
915         return error;
916 }