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