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