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