Whamcloud - gitweb
ed63ea95be0747ad341481eb80c93a9b583c258c
[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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  *
31  * lustre/utils/llverfs.c
32  *
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
37  * 1. full mode
38  * 2. partial mode
39  *
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.
47  *
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.
55  */
56
57 #ifndef _GNU_SOURCE
58 #define _GNU_SOURCE
59 #endif
60 #ifndef LUSTRE_UTILS
61 #define LUSTRE_UTILS
62 #endif
63 #ifndef _LARGEFILE64_SOURCE
64 #define _LARGEFILE64_SOURCE
65 #endif
66 #ifndef _FILE_OFFSET_BITS
67 #define _FILE_OFFSET_BITS 64
68 #endif
69
70 #include <features.h>
71 #include <stdlib.h>
72 #include <stdio.h>
73 #include <string.h>
74 #include <ctype.h>
75 #include <fcntl.h>
76 #include <unistd.h>
77 #include <limits.h>
78 #include <errno.h>
79 #include <fcntl.h>
80 #include <getopt.h>
81 #include <time.h>
82 #include <dirent.h>
83 #include <mntent.h>
84 #include <sys/types.h>
85 #include <sys/stat.h>
86 #include <sys/vfs.h>
87 #include <sys/time.h>
88 #include <gnu/stubs.h>
89 #include <gnu/stubs.h>
90
91 #ifdef HAVE_EXT2FS_EXT2FS_H
92 #  include <e2p/e2p.h>
93 #  include <ext2fs/ext2fs.h>
94 #else
95 #  ifndef EXT2_TOPDIR_FL
96 #    define EXT2_TOPDIR_FL              0x00020000 /* Top of directory tree */
97 #  endif
98 static int fsetflags(const char *path, unsigned int flag)
99 {
100         char cmd[PATH_MAX + 128];
101         int rc;
102
103         if (flag != EXT2_TOPDIR_FL) {
104                 rc = EOPNOTSUPP;
105                 goto out;
106         }
107
108         snprintf(cmd, sizeof(cmd), "chattr +T %s", path);
109
110         rc = system(cmd);
111         if (rc > 0) {
112                 rc = WEXITSTATUS(rc);
113 out:
114                 errno = rc;
115         }
116
117         return rc;
118 }
119 #endif
120
121 #define ONE_MB (1024 * 1024)
122 #define ONE_GB ((unsigned long long)(1024 * 1024 * 1024))
123 #define BLOCKSIZE 4096
124
125 /* Structure for writing test pattern */
126 struct block_data {
127         unsigned long long bd_offset;
128         unsigned long long bd_time;
129         unsigned long long bd_inode;
130 };
131 static char *progname;              /* name by which this program was run. */
132 static unsigned verbose = 1;        /* prints offset in kB, operation rate */
133 static int readoption;              /* run test in read-only (verify) mode */
134 static int writeoption;             /* run test in write_only mode */
135 char *testdir;                      /* name of device to be tested. */
136 static unsigned full = 1;           /* flag to full check */
137 static int error_count;             /* number of IO errors hit during run */
138 char filecount[PATH_MAX];           /* file with total number of files written*/
139 static unsigned long num_files;     /* Total number of files for read/write */
140 static loff_t file_size = 4*ONE_GB; /* Size of each file */
141 static unsigned files_in_dir = 32;  /* number of files in each directioy */
142 static unsigned num_dirs = 30000;   /* total number of directories */
143 const int dirmode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
144 static int isatty_flag;
145 static int perms =  S_IRWXU | S_IRGRP | S_IROTH;
146
147 static struct option const long_opts[] = {
148         { .val = 'c',   .name = "chunksize",    .has_arg = required_argument },
149         { .val = 'h',   .name = "help",         .has_arg = no_argument },
150         { .val = 'l',   .name = "long",         .has_arg = no_argument },
151         { .val = 'l',   .name = "full",         .has_arg = no_argument },
152         { .val = 'o',   .name = "offset",       .has_arg = required_argument },
153         { .val = 'p',   .name = "partial",      .has_arg = required_argument },
154         { .val = 'q',   .name = "quiet",        .has_arg = required_argument },
155         { .val = 'r',   .name = "read",         .has_arg = no_argument },
156         { .val = 's',   .name = "filesize",     .has_arg = no_argument },
157         { .val = 't',   .name = "timestamp",    .has_arg = required_argument },
158         { .val = 'v',   .name = "verbose",      .has_arg = no_argument },
159         { .val = 'w',   .name = "write",        .has_arg = no_argument },
160         { .name = NULL } };
161
162 /*
163  * Usages: displays help information, whenever user supply --help option in
164  * command or enters incorrect command line.
165  */
166 void usage(int status)
167 {
168         if (status != 0) {
169                 printf("\nUsage: %s [OPTION]... <filesystem path> ...\n",
170                        progname);
171                 printf("Filesystem verification tool.\n"
172                        "\t-t {seconds}, --timestamp,  set test time"
173                        "(default=current time())\n"
174                        "\t-o {offset}, --offset, directory starting offset"
175                        " from which tests should start\n"
176                        "\t-r, --read, run in verify mode\n"
177                        "\t-w, --write, run in test-pattern mode, default=rw\n"
178                        "\t-v, --verbose\n"
179                        "\t-p, --partial, for partial check (1MB files)\n"
180                        "\t-l, --long, --full check (4GB file with 4k blocks)\n"
181                        "\t-c, --chunksize, IO chunk size in MB (default=1)\n"
182                        "\t-s, --filesize, file size in MB (default=4096)\n"
183                        "\t-h, --help, display this help and exit\n");
184         }
185         exit(status);
186 }
187
188 /*
189  * open_file: Opens file in specified mode and returns fd.
190  */
191 static int open_file(const char *file, int flag)
192 {
193         int fd = open(file, flag, perms);
194         if (fd < 0) {
195                 fprintf(stderr, "\n%s: Open '%s' failed:%s\n",
196                         progname, file, strerror(errno));
197         }
198         return (fd);
199 }
200
201 /*
202  * Verify_chunk: Verifies test pattern in each 4kB (BLOCKSIZE) is correct.
203  * Returns 0 if test offset and timestamp is correct otherwise 1.
204  */
205 int verify_chunk(char *chunk_buf, const size_t chunksize,
206                  unsigned long long chunk_off, const unsigned long long time_st,
207                  const unsigned long long inode_st, const char *file)
208 {
209         struct block_data *bd;
210         char *chunk_end;
211
212         for (chunk_end = chunk_buf + chunksize - sizeof(*bd);
213              (char *)chunk_buf < chunk_end;
214              chunk_buf += BLOCKSIZE, chunk_off += BLOCKSIZE) {
215                 bd = (struct block_data *)chunk_buf;
216                 if ((bd->bd_offset == chunk_off) && (bd->bd_time == time_st) &&
217                     (bd->bd_inode == inode_st))
218                         continue;
219                 fprintf(stderr, "\n%s: verify %s failed offset/timestamp/inode "
220                         "%llu/%llu/%llu: found %llu/%llu/%llu instead\n",
221                         progname, file, chunk_off, time_st, inode_st,
222                         bd->bd_offset, bd->bd_time, bd->bd_inode);
223                 return 1;
224         }
225         return 0;
226 }
227
228 /*
229  * fill_chunk: Fills the chunk with current or user specified timestamp
230  * and offset. The test pattern is filled at the beginning of
231  * each 4kB(BLOCKSIZE) blocks in chunk_buf.
232  */
233 void fill_chunk(char *chunk_buf, size_t chunksize, loff_t chunk_off,
234                 const time_t time_st, const ino_t inode_st)
235 {
236         struct block_data *bd;
237         char *chunk_end;
238
239         for (chunk_end = chunk_buf + chunksize - sizeof(*bd);
240              (char *)chunk_buf < chunk_end;
241              chunk_buf += BLOCKSIZE, chunk_off += BLOCKSIZE) {
242                 bd = (struct block_data *)chunk_buf;
243                 bd->bd_offset = chunk_off;
244                 bd->bd_time = time_st;
245                 bd->bd_inode = inode_st;
246         }
247 }
248
249 /*
250  * Write a chunk to disk, handling errors, interrupted writes, etc.
251  *
252  * If there is an IO error hit during the write, it is possible that
253  * this will just show up as a short write, and a subsequent write
254  * will return the actual error.  We want to continue in the face of
255  * minor media errors so that we can validate the whole device if
256  * possible, but if there are many errors we don't want to loop forever.
257  *
258  * The error count will be returned upon exit to ensure that the
259  * media errors are detected even if nobody is looking at the output.
260  *
261  * Returns 0 on success, or -ve errno on failure.
262  */
263 int write_retry(int fd, const char *chunk_buf, size_t nrequested,
264                 unsigned long long offset, const char *file)
265 {
266         long nwritten;
267
268 retry:
269         nwritten = write(fd, chunk_buf, nrequested);
270         if (nwritten < 0) {
271                 if (errno != ENOSPC) {
272                         fprintf(stderr, "\n%s: write %s@%llu+%zi failed: %s\n",
273                                 progname, file, offset, nrequested,
274                                 strerror(errno));
275                         if (error_count++ < 100)
276                                 return 0;
277                 }
278                 return -errno;
279         }
280         if (nwritten < nrequested) {
281                 fprintf(stderr, "\n%s: write %s@%llu+%zi short: %ld written\n",
282                         progname, file, offset, nrequested, nwritten);
283                 offset += nwritten;
284                 chunk_buf += nwritten;
285                 nrequested -= nwritten;
286                 goto retry;
287         }
288
289         return 0;
290 }
291
292 /*
293  * write_chunks: write the chunk_buf on the device. The number of write
294  * operations are based on the parameters write_end, offset, and chunksize.
295  *
296  * Returns 0 on success, or -ve error number on failure.
297  */
298 int write_chunks(int fd, unsigned long long offset,unsigned long long write_end,
299                  char *chunk_buf, size_t chunksize, const time_t time_st,
300                  const ino_t inode_st, const char *file)
301 {
302         unsigned long long stride;
303
304         stride = full ? chunksize : (ONE_GB - chunksize);
305         for (offset = offset & ~(chunksize - 1); offset < write_end;
306              offset += stride) {
307                 int ret;
308
309                 if (stride != chunksize && lseek64(fd, offset, SEEK_SET) < 0) {
310                         fprintf(stderr, "\n%s: lseek66(%s+%llu) failed: %s\n",
311                                 progname, file, offset, strerror(errno));
312                         return -errno;
313                 }
314                 if (offset + chunksize > write_end)
315                         chunksize = write_end - offset;
316                 if (!full && offset > chunksize) {
317                         fill_chunk(chunk_buf, chunksize, offset, time_st,
318                                    inode_st);
319                         ret = write_retry(fd, chunk_buf, chunksize,offset,file);
320                         if (ret < 0)
321                                 return ret;
322                         offset += chunksize;
323                         if (offset + chunksize > write_end)
324                                 chunksize = write_end - offset;
325                 }
326                 fill_chunk(chunk_buf, chunksize, offset, time_st, inode_st);
327                 ret = write_retry(fd, chunk_buf, chunksize, offset, file);
328                 if (ret < 0)
329                         return ret;
330         }
331         return 0;
332 }
333
334 /*
335  * read_chunk: reads the chunk_buf from the device. The number of read
336  * operations are based on the parameters read_end, offset, and chunksize.
337  */
338 int read_chunks(int fd, unsigned long long offset, unsigned long long read_end,
339                 char *chunk_buf, size_t chunksize, const time_t time_st,
340                 const ino_t inode_st, const char *file)
341 {
342         unsigned long long stride;
343
344         stride = full ? chunksize : (ONE_GB - chunksize);
345         for (offset = offset & ~(chunksize - 1); offset < read_end;
346              offset += stride) {
347                 ssize_t nread;
348
349                 if (stride != chunksize && lseek64(fd, offset, SEEK_SET) < 0) {
350                         fprintf(stderr, "\n%s: lseek64(%s+%llu) failed: %s\n",
351                                 progname, file, offset, strerror(errno));
352                         return 1;
353                 }
354                 if (offset + chunksize > read_end)
355                         chunksize = read_end - offset;
356
357                 if (!full && offset > chunksize) {
358                         nread = read(fd, chunk_buf, chunksize);
359                         if (nread < 0) {
360                                 fprintf(stderr,"\n%s: read %s@%llu+%zi failed: "
361                                         "%s\n", progname, file, offset,
362                                         chunksize, strerror(errno));
363                                 error_count++;
364                                 return 1;
365                         }
366                         if (nread < chunksize) {
367                                 fprintf(stderr, "\n%s: read %s@%llu+%zi short: "
368                                         "%zi read\n", progname, file, offset,
369                                         chunksize, nread);
370                                 error_count++;
371                         }
372                         if (verify_chunk(chunk_buf, nread, offset, time_st,
373                                          inode_st, file) != 0) {
374                                 return 1;
375                         }
376                         offset += chunksize;
377
378                         /* Need to reset position after read error */
379                         if (nread < chunksize &&
380                             lseek64(fd, offset, SEEK_SET) == -1) {
381                                 fprintf(stderr,
382                                         "\n%s: lseek64(%s@%llu) failed: %s\n",
383                                         progname, file, offset,strerror(errno));
384                                 return 1;
385                         }
386                         if (offset + chunksize >= read_end)
387                                 chunksize = read_end - offset;
388                 }
389                 nread = read(fd, chunk_buf, chunksize);
390                 if (nread < 0) {
391                         fprintf(stderr, "\n%s: read %s@%llu+%zi failed: %s\n",
392                                 progname, file, offset, chunksize,
393                                 strerror(errno));
394                         error_count++;
395                         return 1;
396                 }
397                 if (nread < chunksize) {
398                         fprintf(stderr, "\n%s: read %s@%llu+%zi short: "
399                                 "%zi read\n", progname, file, offset,
400                                 chunksize, nread);
401                         error_count++;
402                 }
403
404                 if (verify_chunk(chunk_buf, nread, offset, time_st,
405                                  inode_st, file) != 0) {
406                         return 1;
407                 }
408         }
409         return 0;
410 }
411
412 /*
413  * new_file: prepares new filename using file counter and current dir.
414  */
415 char *new_file(char *tempfile, char *cur_dir, int file_num)
416 {
417         snprintf(tempfile, PATH_MAX, "%s/file%03d", cur_dir, file_num);
418         return tempfile;
419 }
420
421 /*
422  * new_dir: prepares new dir name using dir counters.
423  */
424 char *new_dir(char *tempdir, int dir_num)
425 {
426         snprintf(tempdir, PATH_MAX, "%s/llverfs_dir%05d", testdir, dir_num);
427         return tempdir;
428 }
429
430 /*
431  * calc_total_bytes: calculates total bytes that need to be
432  * written into or read from the filesystem.
433  */
434 static unsigned long long calc_total_bytes(const char *op)
435 {
436         unsigned long long total_bytes = 0;
437         struct statfs64 statbuf;
438
439         if (full) {
440                 if (statfs64(testdir, &statbuf) == 0) {
441                         if (strcmp(op, "write") == 0)
442                                 total_bytes = (unsigned long long)
443                                         (statbuf.f_bavail * statbuf.f_bsize);
444                         else if (strcmp(op, "read") == 0)
445                                 total_bytes = (unsigned long long)
446                                         (statbuf.f_blocks * statbuf.f_bsize);
447                         else {
448                                 fprintf(stderr, "\n%s: invalid operation: %s\n",
449                                         progname, op);
450                                 return -1;
451                         }
452                 } else {
453                         fprintf(stderr, "\n%s: unable to stat %s: %s\n",
454                                 progname, testdir, strerror(errno));
455                         return -errno;
456                 }
457         } else {
458                 total_bytes = num_dirs * files_in_dir * file_size;
459         }
460
461         return total_bytes;
462 }
463
464 /*
465  * show_rate: displays the current read/write file name and performance,
466  * along with an estimate of how long the whole read/write operation
467  * will continue.
468  */
469 void show_rate(char *op, char *filename, const struct timeval *start_time,
470                const unsigned long long total_bytes,
471                const unsigned long long curr_bytes)
472 {
473         static struct timeval last_time;
474         static unsigned long long last_bytes;
475         static char last_op;
476         struct timeval curr_time;
477         double curr_delta, overall_delta, curr_rate, overall_rate;
478         double remain_time;
479         int remain_hours, remain_minutes, remain_seconds;
480
481         if (last_op != op[0]) {
482                 last_bytes = 0;
483                 last_time = *start_time;
484                 last_op = op[0];
485         }
486
487         gettimeofday(&curr_time, NULL);
488
489         curr_delta = (curr_time.tv_sec - last_time.tv_sec) +
490                 (double)(curr_time.tv_usec - last_time.tv_usec) / 1000000;
491
492         overall_delta = (curr_time.tv_sec - start_time->tv_sec) +
493                 (double)(curr_time.tv_usec - start_time->tv_usec) / 1000000;
494
495         curr_rate = (curr_bytes - last_bytes) / curr_delta;
496         overall_rate = curr_bytes / overall_delta;
497
498         if (curr_rate == 0) {
499                 last_time = curr_time;
500                 return;
501         }
502         remain_time = (total_bytes - curr_bytes) / curr_rate;
503
504         remain_hours = remain_time / 3600;
505         remain_minutes = (remain_time - remain_hours * 3600) / 60;
506         remain_seconds = (remain_time - remain_hours * 3600 -
507                 remain_minutes * 60);
508
509         if (curr_delta > 4 || verbose > 2) {
510                 if (isatty_flag)
511                         printf("\r");
512
513                 printf("%s: %s, current: %5g MB/s, overall: %5g MB/s, "
514                        "ETA: %u:%02u:%02u", op, filename,
515                        curr_rate / ONE_MB, overall_rate / ONE_MB,
516                        remain_hours, remain_minutes, remain_seconds);
517
518                 if (isatty_flag)
519                         fflush(stdout);
520                 else
521                         printf("\n");
522
523                 last_time = curr_time;
524                 last_bytes = curr_bytes;
525         }
526 }
527
528 /*
529  * dir_write: This function writes directories and files on device.
530  * it works for both full and partial modes.
531  */
532 static int dir_write(char *chunk_buf, size_t chunksize,
533                      time_t time_st, unsigned long dir_num)
534 {
535         char tempfile[PATH_MAX];
536         char tempdir[PATH_MAX];
537         FILE *countfile;
538         struct stat64 file;
539         int file_num = 999999999;
540         ino_t inode_st = 0;
541         struct timeval start_time;
542         unsigned long long total_bytes;
543         unsigned long long curr_bytes = 0;
544         int rc = 0;
545
546         if (!full && fsetflags(testdir, EXT2_TOPDIR_FL))
547                 fprintf(stderr,
548                         "\n%s: can't set TOPDIR_FL on %s: %s (ignoring)",
549                         progname, testdir, strerror(errno));
550
551         countfile = fopen(filecount, "w");
552         if (countfile == NULL) {
553                 fprintf(stderr, "\n%s: creating %s failed :%s\n",
554                         progname, filecount, strerror(errno));
555                 return 5;
556         }
557         /* reserve space for the countfile */
558         if (fprintf(countfile, "%lu", num_files) < 1 ||
559             fflush(countfile) != 0) {
560                 fprintf(stderr, "\n%s: writing %s failed :%s\n",
561                         progname, filecount, strerror(errno));
562                 rc = 6;
563                 goto out;
564         }
565
566         /* calculate total bytes that need to be written */
567         total_bytes = calc_total_bytes("write");
568         if (total_bytes <= 0) {
569                 fprintf(stderr, "\n%s: unable to calculate total bytes\n",
570                         progname);
571                 rc = 7;
572                 goto out;
573         }
574
575         if (!full && (dir_num != 0))
576                 total_bytes -= dir_num * files_in_dir * file_size;
577
578         gettimeofday(&start_time, NULL);
579         for (; dir_num < num_dirs; num_files++, file_num++) {
580                 int fd, ret;
581
582                 if (file_num >= files_in_dir) {
583                         file_num = 0;
584                         if (mkdir(new_dir(tempdir, dir_num), dirmode) < 0) {
585                                 if (errno == ENOSPC)
586                                         break;
587                                 if (errno != EEXIST) {
588                                         fprintf(stderr, "\n%s: mkdir %s : %s\n",
589                                                 progname, tempdir,
590                                                 strerror(errno));
591                                         rc = 1;
592                                         goto out;
593                                 }
594                         }
595                         dir_num++;
596                 }
597
598                 fd = open_file(new_file(tempfile, tempdir, file_num),
599                                O_WRONLY | O_CREAT | O_TRUNC | O_LARGEFILE);
600                 if (fd >= 0) {
601                         if (fstat64(fd, &file) == 0) {
602                                 inode_st = file.st_ino;
603                         } else {
604                                 fprintf(stderr, "\n%s: write stat '%s': %s",
605                                         progname, tempfile, strerror(errno));
606                                 close(fd);
607                                 break;
608                         }
609                 } else {
610                         break;
611                 }
612
613                 ret = write_chunks(fd, 0, file_size, chunk_buf, chunksize,
614                                    time_st, inode_st, tempfile);
615                 close(fd);
616                 if (ret < 0) {
617                         if (ret != -ENOSPC) {
618                                 rc = 1;
619                                 goto out;
620                         }
621                         curr_bytes = total_bytes;
622                         break;
623                 }
624
625                 curr_bytes += file_size;
626                 if (verbose > 1)
627                         show_rate("write", tempfile, &start_time,
628                                   total_bytes, curr_bytes);
629
630                 fseek(countfile, 0, SEEK_SET);
631                 if (fprintf(countfile, "%lu", num_files) < 1 ||
632                     fflush(countfile) != 0) {
633                         fprintf(stderr, "\n%s: writing %s failed :%s\n",
634                                 progname, filecount, strerror(errno));
635                 }
636         }
637
638         verbose += 2;
639         show_rate("write_done", tempfile, &start_time, total_bytes, curr_bytes);
640         printf("\n");
641         verbose -= 2;
642
643 out:
644         fclose(countfile);
645
646         return rc;
647 }
648
649 /*
650  * dir_read: This function reads directories and files on device.
651  * it works for both full and partial modes.
652  */
653 static int dir_read(char *chunk_buf, size_t chunksize,
654                     time_t time_st, unsigned long dir_num)
655 {
656         char tempfile[PATH_MAX];
657         char tempdir[PATH_MAX];
658         unsigned long count = 0;
659         struct stat64 file;
660         int file_num = 0;
661         ino_t inode_st = 0;
662         struct timeval start_time;
663         unsigned long long total_bytes;
664         unsigned long long curr_bytes = 0;
665
666         /* calculate total bytes that need to be read */
667         total_bytes = calc_total_bytes("read");
668         if (total_bytes <= 0) {
669                 fprintf(stderr, "\n%s: unable to calculate total bytes\n",
670                         progname);
671                 return 1;
672         }
673
674         if (dir_num != 0)
675                 total_bytes -= dir_num * files_in_dir * file_size;
676
677         gettimeofday(&start_time, NULL);
678         for (count = 0; count < num_files && dir_num < num_dirs; count++) {
679                 int fd, ret;
680
681                 if (file_num == 0) {
682                         new_dir(tempdir, dir_num);
683                         dir_num++;
684                 }
685
686                 fd = open_file(new_file(tempfile, tempdir, file_num),
687                                O_RDONLY | O_LARGEFILE);
688                 if (fd >= 0) {
689                         if (fstat64(fd, &file) == 0) {
690                                 inode_st = file.st_ino;
691                         } else {
692                                 fprintf(stderr, "\n%s: read stat '%s': %s\n",
693                                         progname, tempfile, strerror(errno));
694                                 close(fd);
695                                 return 1;
696                         }
697                 } else {
698                         break;
699                 }
700
701                 if (count == num_files)
702                         file_size = file.st_size;
703                 ret = read_chunks(fd, 0, file_size, chunk_buf, chunksize,
704                                   time_st, inode_st, tempfile);
705                 close(fd);
706                 if (ret)
707                         return 1;
708
709                 curr_bytes += file_size;
710                 if (verbose > 1)
711                         show_rate("read", tempfile, &start_time,
712                                   total_bytes, curr_bytes);
713
714                 if (++file_num >= files_in_dir)
715                         file_num = 0;
716         }
717         verbose += 2;
718         show_rate("read_done", tempfile, &start_time, total_bytes, curr_bytes);
719         printf("\n");
720         verbose -= 2;
721
722         return 0;
723 }
724
725 int main(int argc, char **argv)
726 {
727         time_t time_st = 0;             /* Default timestamp */
728         size_t chunksize = ONE_MB;      /* IO chunk size(defailt=1MB) */
729         char *chunk_buf;                /* chunk buffer */
730         int error = 0;
731         FILE *countfile = NULL;
732         unsigned long dir_num = 0, dir_num_orig = 0;/* starting directory */
733         int c;
734
735         progname = strrchr(argv[0], '/') ? strrchr(argv[0], '/') + 1 : argv[0];
736         while ((c = getopt_long(argc, argv, "c:hlo:pqrs:t:vw",
737                                       long_opts, NULL)) != -1) {
738                 switch (c) {
739                 case 'c':
740                         chunksize = strtoul(optarg, NULL, 0) * ONE_MB;
741                         if (chunksize == 0) {
742                                 fprintf(stderr, "%s: bad chunk size '%s'\n",
743                                         optarg, progname);
744                                 return -1;
745                         }
746                         break;
747                 case 'l':
748                         full = 1;
749                         break;
750                 case 'o': /* offset */
751                         dir_num = strtoul(optarg, NULL, 0);
752                         break;
753                 case 'p':
754                         file_size = ONE_MB;
755                         chunksize = ONE_MB;
756                         files_in_dir = 1;
757                         full = 0;
758                         break;
759                 case 'q':
760                         verbose = 0;
761                         break;
762                 case 'r':
763                         readoption = 1;
764                         break;
765                 case 's':
766                         file_size = strtoul(optarg, NULL, 0) * ONE_MB;
767                         if (file_size == 0) {
768                                 fprintf(stderr, "%s: bad file size '%s'\n",
769                                         optarg, progname);
770                                 return -1;
771                         }
772                         break;
773                 case 't':
774                         time_st = (time_t)strtoul(optarg, NULL, 0);
775                         break;
776                 case 'v':
777                         verbose++;
778                         break;
779                 case 'w':
780                         writeoption = 1;
781                         break;
782
783                 case 'h':
784                 default:
785                         usage(1);
786                         return 0;
787                 }
788         }
789         testdir = argv[optind];
790
791         if (!testdir) {
792                 fprintf(stderr, "%s: pathname not given\n", progname);
793                 usage(1);
794                 return -1;
795         }
796         if (!readoption && !writeoption) {
797                 readoption = 1;
798                 writeoption = 1;
799         }
800         if (!time_st)
801                 (void) time(&time_st);
802         printf("Timestamp: %lu\n", (unsigned long )time_st);
803         isatty_flag = isatty(STDOUT_FILENO);
804
805         if (!full) {
806 #ifdef HAVE_EXT2FS_EXT2FS_H
807                 struct mntent *tempmnt;
808                 FILE *fp = NULL;
809                 ext2_filsys fs;
810
811                 if ((fp = setmntent("/etc/mtab", "r")) == NULL) {
812                         fprintf(stderr, "%s: fail to open /etc/mtab in read mode :%s\n",
813                                 progname, strerror(errno));
814                         goto guess;
815                 }
816
817                 /* find device name using filesystem */
818                 while ((tempmnt = getmntent(fp)) != NULL) {
819                         if (strcmp(tempmnt->mnt_dir, testdir) == 0)
820                                 break;
821                 }
822
823                 if (tempmnt == NULL) {
824                         fprintf(stderr, "%s: no device found for '%s'\n",
825                                 progname, testdir);
826                         endmntent(fp);
827                         goto guess;
828                 }
829
830                 if (ext2fs_open(tempmnt->mnt_fsname, 0, 0, 0,
831                                 unix_io_manager, &fs)) {
832                         fprintf(stderr, "%s: unable to open ext3 fs on '%s'\n",
833                                 progname, testdir);
834                         endmntent(fp);
835                         goto guess;
836                 }
837                 endmntent(fp);
838
839                 num_dirs = (fs->super->s_blocks_count +
840                             fs->super->s_blocks_per_group - 1) /
841                         fs->super->s_blocks_per_group;
842                 if (verbose)
843                         printf("ext3 block groups: %u, fs blocks: %u "
844                                "blocks per group: %u\n",
845                                num_dirs, fs->super->s_blocks_count,
846                                fs->super->s_blocks_per_group);
847                 ext2fs_close(fs);
848 #else
849                 goto guess;
850 #endif
851                 if (0) { /* ugh */
852                         struct statfs64 statbuf;
853 guess:
854                         /*
855                          * Most extN filesystems are formatted with 128MB/group
856                          * (32k bitmap = 4KB blocksize * 8 bits/block) * 4KB,
857                          * so this is a relatively safe default (somewhat more
858                          * or less doesn't make a huge difference for testing).
859                          *
860                          * We want to create one directory per group, together
861                          * with the "TOPDIR" feature, so that the directories
862                          * are spread across the whole block device.
863                          */
864                         if (statfs64(testdir, &statbuf) == 0) {
865                                 num_dirs = 1 + (long long)statbuf.f_blocks *
866                                         statbuf.f_bsize / (128ULL * ONE_MB);
867                                 if (verbose)
868                                         printf("dirs: %u, fs blocks: %llu\n",
869                                                num_dirs,
870                                                (long long)statbuf.f_blocks);
871                         } else {
872                                 fprintf(stderr, "%s: unable to stat '%s': %s\n",
873                                         progname, testdir, strerror(errno));
874                                 if (verbose)
875                                         printf("dirs: %u\n", num_dirs);
876                         }
877                 }
878         }
879         chunk_buf = (char *)calloc(chunksize, 1);
880         if (chunk_buf == NULL) {
881                 fprintf(stderr, "Memory allocation failed for chunk_buf\n");
882                 return 4;
883         }
884         snprintf(filecount, sizeof(filecount), "%s/%s.filecount",
885                  testdir, progname);
886         if (writeoption) {
887                 (void)mkdir(testdir, dirmode);
888
889                 unlink(filecount);
890                 if (dir_num != 0) {
891                         num_files = dir_num * files_in_dir;
892                         if (verbose)
893                                 printf("\n%s: %lu files already written\n",
894                                        progname, num_files);
895                 }
896                 if (dir_write(chunk_buf, chunksize, time_st, dir_num)) {
897                         error = 3;
898                         goto out;
899                 }
900                 dir_num = dir_num_orig;
901         }
902         if (readoption) {
903                 if (!writeoption) {
904                         countfile = fopen(filecount, "r");
905                         if (countfile == NULL ||
906                             fscanf(countfile, "%lu", &num_files) != 1 ||
907                             num_files == 0) {
908                                 fprintf(stderr, "\n%s: reading %s failed :%s\n",
909                                         progname, filecount, strerror(errno));
910                                 num_files = num_dirs * files_in_dir;
911                         } else {
912                                 num_files -= (dir_num * files_in_dir);
913                         }
914                         if (countfile)
915                                 fclose(countfile);
916                 }
917                 if (dir_read(chunk_buf, chunksize, time_st, dir_num)) {
918                         fprintf(stderr, "\n%s: Data verification failed\n",
919                                 progname) ;
920                         error = 2;
921                         goto out;
922                 }
923         }
924         error = error_count;
925 out:
926         free(chunk_buf);
927         return error;
928 }