-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnfsio.c
432 lines (356 loc) · 10.2 KB
/
nfsio.c
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
/*
Copyright (C) by Ronnie Sahlberg <[email protected]> 2008
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 3 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, see <http://www.gnu.org/licenses/>.
*/
#define _FILE_OFFSET_BITS 64
#include "nfs.h"
#include "dbench.h"
#include <stdio.h>
#include "mount.h"
#include <stdint.h>
#include "libnfs.h"
#define discard_const(ptr) ((void *)((intptr_t)(ptr)))
#define MAX_FILES 200
struct cb_data {
struct nfsio *nfsio;
char *dirname;
};
static void nfs3_deltree(struct dbench_op *op);
static void nfs3_cleanup(struct child_struct *child)
{
char *dname;
struct dbench_op op;
ZERO_STRUCT(op);
asprintf(&dname, "/clients/client%d", child->id);
op.fname = dname;
op.child = child;
nfs3_deltree(&op);
free(dname);
}
static void nfs3_setup(struct child_struct *child)
{
const char *status = "0x00000000";
nfsstat3 res;
child->rate.last_time = timeval_current();
child->rate.last_bytes = 0;
srandom(getpid() ^ time(NULL));
child->private = nfsio_connect(options.server, options.export, options.protocol, global_random + child->id, child->num_clients);
if (child->private == NULL) {
child->failed = 1;
printf("nfsio_connect() failed\n");
exit(10);
}
/* create '/clients' */
res = nfsio_lookup(child->private, "/clients", NULL);
if (res == NFS3ERR_NOENT) {
res = nfsio_mkdir(child->private, "/clients");
if( (res != NFS3_OK) &&
(res != NFS3ERR_EXIST) ) {
printf("Failed to create '/clients' directory. res:%u\n", res);
exit(10);
}
}
}
static void dirent_cb(struct entryplus3 *e, void *private_data)
{
struct cb_data *cbd = private_data;
nfsstat3 res;
char *objname;
if (!strcmp(cbd->dirname,".")) {
return;
}
if (!strcmp(cbd->dirname,"..")) {
return;
}
asprintf(&objname, "%s/%s", cbd->dirname, e->name);
if (objname == NULL) {
printf("Failed to talloc ne object name in dirent_cb\n");
exit(10);
}
if (e->name_attributes.post_op_attr_u.attributes.type == NF3DIR) {
struct cb_data *new_cbd = malloc(sizeof(struct cb_data));
new_cbd->nfsio = cbd->nfsio;
new_cbd->dirname = strdup(objname);
nfsio_readdirplus(cbd->nfsio, objname, dirent_cb, new_cbd);
res = nfsio_rmdir(cbd->nfsio, objname);
if (res != NFS3_OK) {
printf("Failed to remove object : \"%s\" %s (%d)\n", objname, nfs_error(res), res);
free(objname);
free(new_cbd->dirname);
free(new_cbd);
exit(10);
}
free(objname);
free(new_cbd->dirname);
free(new_cbd);
return;
}
res = nfsio_remove(cbd->nfsio, objname);
if (res != NFS3_OK) {
printf("Failed to remove object : \"%s\" %s %d\n", objname, nfs_error(res), res);
free(objname);
exit(10);
}
free(objname);
}
static void nfs3_deltree(struct dbench_op *op)
{
struct cb_data *cbd;
nfsstat3 res;
cbd = malloc(sizeof(struct cb_data));
cbd->nfsio = op->child->private;
cbd->dirname = discard_const(op->fname);
res = nfsio_lookup(cbd->nfsio, cbd->dirname, NULL);
if (res != NFS3ERR_NOENT) {
nfsio_readdirplus(cbd->nfsio, cbd->dirname, dirent_cb, cbd);
nfsio_rmdir(cbd->nfsio, cbd->dirname);
}
res = nfsio_lookup(cbd->nfsio, cbd->dirname, NULL);
if (res != NFS3ERR_NOENT) {
printf("Directory \"%s\" not empty. Aborting\n", cbd->dirname);
free(cbd);
exit(10);
}
free(cbd);
}
static int check_status(int status, const char *expected)
{
if (strcmp(expected, "*") == 0){
return 1;
}
if (strncmp(expected, "0x", 2) == 0) {
return status == strtol(expected, NULL, 16);
}
return 0;
}
static void failed(struct child_struct *child)
{
child->failed = 1;
printf("ERROR: child %d failed at line %d\n", child->id, child->line);
exit(1);
}
static void nfs3_getattr(struct dbench_op *op)
{
nfsstat3 res;
res = nfsio_getattr(op->child->private, op->fname, NULL);
if (!check_status(res, op->status)) {
printf("[%d] GETATTR \"%s\" failed (%x) - expected %s\n",
op->child->line, op->fname, res, op->status);
failed(op->child);
}
}
static void nfs3_lookup(struct dbench_op *op)
{
nfsstat3 res;
res = nfsio_lookup(op->child->private, op->fname, NULL);
if (!check_status(res, op->status)) {
printf("[%d] LOOKUP \"%s\" failed (%x) - expected %s\n",
op->child->line, op->fname, res, op->status);
failed(op->child);
}
}
static void nfs3_create(struct dbench_op *op)
{
nfsstat3 res;
res = nfsio_create(op->child->private, op->fname);
if (!check_status(res, op->status)) {
printf("[%d] CREATE \"%s\" failed (%x) - expected %s\n",
op->child->line, op->fname, res, op->status);
failed(op->child);
}
}
static void nfs3_write(struct dbench_op *op)
{
off_t offset = op->params[0];
int len = op->params[1];
int stable = op->params[2];
nfsstat3 res;
if ((options.trunc_io > 0) && (len > options.trunc_io)) {
len = options.trunc_io;
}
res = nfsio_write(op->child->private, op->fname, rw_buf, offset, len, stable);
if (!check_status(res, op->status)) {
printf("[%d] WRITE \"%s\" failed (%x) - expected %s\n",
op->child->line, op->fname,
res, op->status);
failed(op->child);
}
op->child->bytes += len;
}
static void nfs3_commit(struct dbench_op *op)
{
nfsstat3 res;
res = nfsio_commit(op->child->private, op->fname);
if (!check_status(res, op->status)) {
printf("[%d] COMMIT \"%s\" failed (%x) - expected %s\n",
op->child->line, op->fname, res, op->status);
failed(op->child);
}
}
static void nfs3_read(struct dbench_op *op)
{
off_t offset = op->params[0];
int len = op->params[1];
nfsstat3 res = 0;
if ((options.trunc_io > 0) && (len > options.trunc_io)) {
len = options.trunc_io;
}
res = nfsio_read(op->child->private, op->fname, NULL, offset, len, NULL, NULL);
if (!check_status(res, op->status)) {
printf("[%d] READ \"%s\" failed (%x) - expected %s\n",
op->child->line, op->fname,
res, op->status);
failed(op->child);
}
op->child->bytes += len;
}
static void nfs3_access(struct dbench_op *op)
{
nfsstat3 res;
res = nfsio_access(op->child->private, op->fname, 0, NULL);
if (!check_status(res, op->status)) {
printf("[%d] ACCESS \"%s\" failed (%x) - expected %s\n",
op->child->line, op->fname, res, op->status);
failed(op->child);
}
}
static void nfs3_mkdir(struct dbench_op *op)
{
nfsstat3 res;
res = nfsio_mkdir(op->child->private, op->fname);
if (!check_status(res, op->status)) {
printf("[%d] MKDIR \"%s\" failed (%x) - expected %s\n",
op->child->line, op->fname, res, op->status);
failed(op->child);
}
}
static void nfs3_rmdir(struct dbench_op *op)
{
nfsstat3 res;
res = nfsio_rmdir(op->child->private, op->fname);
if (!check_status(res, op->status)) {
printf("[%d] RMDIR \"%s\" failed (%x) - expected %s\n",
op->child->line, op->fname, res, op->status);
failed(op->child);
}
}
static void nfs3_fsstat(struct dbench_op *op)
{
nfsstat3 res;
res = nfsio_fsstat(op->child->private);
if (!check_status(res, op->status)) {
printf("[%d] FSSTAT failed (%x) - expected %s\n",
op->child->line, res, op->status);
failed(op->child);
}
}
static void nfs3_fsinfo(struct dbench_op *op)
{
nfsstat3 res;
res = nfsio_fsinfo(op->child->private);
if (!check_status(res, op->status)) {
printf("[%d] FSINFO failed (%x) - expected %s\n",
op->child->line, res, op->status);
failed(op->child);
}
}
static void nfs3_symlink(struct dbench_op *op)
{
nfsstat3 res;
res = nfsio_symlink(op->child->private, op->fname, op->fname2);
if (!check_status(res, op->status)) {
printf("[%d] SYMLINK \"%s\"->\"%s\" failed (%x) - expected %s\n",
op->child->line, op->fname, op->fname2,
res, op->status);
failed(op->child);
}
}
static void nfs3_remove(struct dbench_op *op)
{
nfsstat3 res;
res = nfsio_remove(op->child->private, op->fname);
if (!check_status(res, op->status)) {
printf("[%d] REMOVE \"%s\" failed (%x) - expected %s\n",
op->child->line, op->fname, res, op->status);
failed(op->child);
}
}
static void nfs3_readdirplus(struct dbench_op *op)
{
nfsstat3 res;
res = nfsio_readdirplus(op->child->private, op->fname, NULL, NULL);
if (!check_status(res, op->status)) {
printf("[%d] READDIRPLUS \"%s\" failed (%x) - expected %s\n",
op->child->line, op->fname, res, op->status);
failed(op->child);
}
}
static void nfs3_link(struct dbench_op *op)
{
nfsstat3 res;
res = nfsio_link(op->child->private, op->fname, op->fname2);
if (!check_status(res, op->status)) {
printf("[%d] LINK \"%s\"->\"%s\" failed (%x) - expected %s\n",
op->child->line, op->fname, op->fname2,
res, op->status);
failed(op->child);
}
}
static void nfs3_rename(struct dbench_op *op)
{
nfsstat3 res;
res = nfsio_rename(op->child->private, op->fname, op->fname2);
if (!check_status(res, op->status)) {
printf("[%d] RENAME \"%s\"->\"%s\" failed (%x) - expected %s\n",
op->child->line, op->fname, op->fname2,
res, op->status);
failed(op->child);
}
}
static int nfs3_init(void)
{
void *handle;
handle = nfsio_connect(options.server, options.export, options.protocol, global_random, 1);
if (handle == NULL) {
printf("Failed to connect to NFS server\n");
return 1;
}
nfsio_disconnect(handle);
return 0;
}
static struct backend_op ops[] = {
{ "Deltree", nfs3_deltree },
{ "GETATTR3", nfs3_getattr },
{ "LOOKUP3", nfs3_lookup },
{ "CREATE3", nfs3_create },
{ "WRITE3", nfs3_write },
{ "COMMIT3", nfs3_commit },
{ "READ3", nfs3_read },
{ "ACCESS3", nfs3_access },
{ "MKDIR3", nfs3_mkdir },
{ "RMDIR3", nfs3_rmdir },
{ "FSSTAT3", nfs3_fsstat },
{ "FSINFO3", nfs3_fsinfo },
{ "SYMLINK3", nfs3_symlink },
{ "REMOVE3", nfs3_remove },
{ "READDIRPLUS3", nfs3_readdirplus },
{ "RENAME3", nfs3_rename },
{ "LINK3", nfs3_link },
{ NULL, NULL}
};
struct nb_operations nfs_ops = {
.backend_name = "nfsbench",
.init = nfs3_init,
.setup = nfs3_setup,
.cleanup = nfs3_cleanup,
.ops = ops
};