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