liyujie
2025-08-28 b3810562527858a3b3d98ffa6e9c9c5b0f4a9a8e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
 *   Copyright (c) International Business Machines Corp., 2001-2004
 *
 *   This program is free software;  you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
 *   the GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program;  if not, write to the Free Software
 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */
 
#define FILE_OFFSET_BITS 64
 
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <inttypes.h>
#include <assert.h>
 
#include "ffsb.h"
#include "fh.h"
 
#include "config.h"
 
/* !!! ugly */
#ifndef HAVE_OPEN64
#define open64 open
#endif
 
#ifndef HAVE_FSEEKO64
#define lseek64 lseek
#endif
 
/* All these functions read the global mainconfig->bufferedio variable
 * to determine if they are to do buffered i/o or normal.
 *
 * ha, well, they're supposed to anyway...!!! TODO -SR 2006/05/14
 */
 
static void do_stats(struct timeval *start, struct timeval *end,
            ffsb_thread_t * ft, ffsb_fs_t * fs, syscall_t sys)
{
   struct timeval diff;
   uint32_t value = 0;
 
   if (!ft && !fs)
       return;
 
   timersub(end, start, &diff);
 
   value = 1000000 * diff.tv_sec + diff.tv_usec;
 
   if (ft && ft_needs_stats(ft, sys))
       ft_add_stat(ft, sys, value);
   if (fs && fs_needs_stats(fs, sys))
       fs_add_stat(fs, sys, value);
}
 
static int fhopenhelper(char *filename, char *bufflags, int flags,
           ffsb_thread_t * ft, ffsb_fs_t * fs)
{
   int fd = 0;
   struct timeval start, end;
   int need_stats = ft_needs_stats(ft, SYS_OPEN) ||
       fs_needs_stats(fs, SYS_OPEN);
 
   flags |= O_LARGEFILE;
 
   if (need_stats)
       gettimeofday(&start, NULL);
 
   fd = open64(filename, flags, S_IRWXU);
   if (fd < 0) {
       perror(filename);
       exit(0);
   }
 
   if (need_stats) {
       gettimeofday(&end, NULL);
       do_stats(&start, &end, ft, fs, SYS_OPEN);
   }
 
   return fd;
}
 
int fhopenread(char *filename, ffsb_thread_t * ft, ffsb_fs_t * fs)
{
   int flags = O_RDONLY;
   int directio = fs_get_directio(fs);
 
   if (directio)
       flags |= O_DIRECT;
   return fhopenhelper(filename, "r", flags, ft, fs);
}
 
int fhopenappend(char *filename, ffsb_thread_t * ft, ffsb_fs_t * fs)
{
   int flags = O_APPEND | O_WRONLY;
   int directio = fs_get_directio(fs);
 
   if (directio)
       flags |= O_DIRECT;
   return fhopenhelper(filename, "a", flags, ft, fs);
}
 
int fhopenwrite(char *filename, ffsb_thread_t * ft, ffsb_fs_t * fs)
{
   int flags = O_WRONLY;
   int directio = fs_get_directio(fs);
 
   if (directio)
       flags |= O_DIRECT;
   return fhopenhelper(filename, "w", flags, ft, fs);
}
 
int fhopencreate(char *filename, ffsb_thread_t * ft, ffsb_fs_t * fs)
{
   int flags = O_CREAT | O_RDWR | O_TRUNC;
   int directio = fs_get_directio(fs);
 
   if (directio)
       flags |= O_DIRECT;
   return fhopenhelper(filename, "rw", flags, ft, fs);
}
 
void fhread(int fd, void *buf, uint64_t size, ffsb_thread_t * ft,
       ffsb_fs_t * fs)
{
   ssize_t realsize;
   struct timeval start, end;
   int need_stats = ft_needs_stats(ft, SYS_READ) ||
       fs_needs_stats(fs, SYS_READ);
 
   assert(size <= SIZE_MAX);
   if (need_stats)
       gettimeofday(&start, NULL);
   realsize = read(fd, buf, size);
 
   if (need_stats) {
       gettimeofday(&end, NULL);
       do_stats(&start, &end, ft, fs, SYS_READ);
   }
 
   if (realsize != size) {
       printf("Read %lld instead of %llu bytes.\n",
              (unsigned long long)realsize, (unsigned long long)size);
       perror("read");
       exit(1);
   }
}
 
void fhwrite(int fd, void *buf, uint32_t size, ffsb_thread_t * ft,
        ffsb_fs_t * fs)
{
   ssize_t realsize;
   struct timeval start, end;
   int need_stats = ft_needs_stats(ft, SYS_WRITE) ||
       fs_needs_stats(fs, SYS_WRITE);
 
   assert(size <= SIZE_MAX);
   if (need_stats)
       gettimeofday(&start, NULL);
 
   realsize = write(fd, buf, size);
 
   if (need_stats) {
       gettimeofday(&end, NULL);
       do_stats(&start, &end, ft, fs, SYS_WRITE);
   }
 
   if (realsize != size) {
       printf("Wrote %d instead of %d bytes.\n"
              "Probably out of disk space\n", realsize, size);
       perror("write");
       exit(1);
   }
}
 
void fhseek(int fd, uint64_t offset, int whence, ffsb_thread_t * ft,
       ffsb_fs_t * fs)
{
   uint64_t res;
   struct timeval start, end;
   int need_stats = ft_needs_stats(ft, SYS_LSEEK) ||
       fs_needs_stats(fs, SYS_LSEEK);
 
   if ((whence == SEEK_CUR) && (offset == 0))
       return;
 
   if (need_stats)
       gettimeofday(&start, NULL);
 
   res = lseek64(fd, offset, whence);
 
   if (need_stats) {
       gettimeofday(&end, NULL);
       do_stats(&start, &end, ft, fs, SYS_LSEEK);
   }
   if ((whence == SEEK_SET) && (res != offset))
       perror("seek");
 
   if (res == -1) {
       if (whence == SEEK_SET)
           fprintf(stderr, "tried to seek to %lld\n", offset);
       else
           fprintf(stderr, "tried to seek from current "
               "position to %lld\n", offset);
 
       perror("seek");
       exit(1);
   }
}
 
void fhclose(int fd, ffsb_thread_t * ft, ffsb_fs_t * fs)
{
   struct timeval start, end;
   int need_stats = ft_needs_stats(ft, SYS_CLOSE) ||
       fs_needs_stats(fs, SYS_CLOSE);
 
   if (need_stats)
       gettimeofday(&start, NULL);
 
   close(fd);
 
   if (need_stats) {
       gettimeofday(&end, NULL);
       do_stats(&start, &end, ft, fs, SYS_CLOSE);
   }
}
 
void fhstat(char *name, ffsb_thread_t * ft, ffsb_fs_t * fs)
{
   struct timeval start, end;
   struct stat tmp_stat;
 
   int need_stats = ft_needs_stats(ft, SYS_STAT) ||
       fs_needs_stats(fs, SYS_CLOSE);
 
   if (need_stats)
       gettimeofday(&start, NULL);
 
   if (stat(name, &tmp_stat)) {
       fprintf(stderr, "stat call failed for file %s\n", name);
       exit(1);
   }
 
   if (need_stats) {
       gettimeofday(&end, NULL);
       do_stats(&start, &end, ft, fs, SYS_STAT);
   }
}
 
int writefile_helper(int fd, uint64_t size, uint32_t blocksize, char *buf,
            struct ffsb_thread *ft, struct ffsb_fs *fs)
{
   uint64_t iterations, a;
   uint64_t last;
 
   iterations = size / blocksize;
   last = size % blocksize;
 
   for (a = 0; a < iterations; a++)
       fhwrite(fd, buf, blocksize, ft, fs);
 
   if (last) {
       a++;
       fhwrite(fd, buf, last, ft, fs);
   }
   return a;
}