forked from allspace/eunfs3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
readdir.c
363 lines (305 loc) · 9.36 KB
/
readdir.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
/*
* UNFS3 readdir routine
* (C) 2004, Pascal Schmidt
* see file LICENSE for license details
*/
#include "config.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <rpc/rpc.h>
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "nfs.h"
#include "mount.h"
#include "fh.h"
#include "attr.h"
#include "readdir.h"
#include "backend.h"
#include "Config/exports.h"
#include "daemon.h"
#include "error.h"
/*
* maximum number of entries in readdir results
*
* this is 4096 / 28 (the minimum size of an entry3)
*/
#define MAX_ENTRIES 143
/*
* static READDIR3resok size with XDR overhead
*
* 88 bytes attributes, 8 bytes verifier, 4 bytes value_follows for
* first entry, 4 bytes eof flag
*/
#define RESOK_SIZE 104
/*
* static entry3 size with XDR overhead
*
* 8 bytes fileid, 4 bytes name length, 8 bytes cookie, 4 byte value_follows
*/
#define ENTRY_SIZE 24
/*
* size of a name with XDR overhead
*
* XDR pads to multiple of 4 bytes
*/
#define NAME_SIZE(x) (((strlen((x))+3)/4)*4)
uint32 directory_hash(const char *path)
{
backend_dirstream *search;
struct dirent *this;
uint32 hval = 0;
search = backend_opendir(path);
if (!search) {
return 0;
}
while ((this = backend_readdir(search)) != NULL) {
hval = fnv1a_32(this->d_name, hval);
}
backend_closedir(search);
return hval;
}
/*
* perform a READDIR operation
*
* fh_decomp must be called directly before to fill the stat cache
*/
READDIR3res read_dir(const char *path, cookie3 cookie, cookieverf3 verf,
count3 count)
{
READDIR3res result;
READDIR3resok resok;
cookie3 upper;
static entry3 entry[MAX_ENTRIES];
backend_statstruct buf;
int res;
backend_dirstream *search;
struct dirent *this;
count3 i, real_count;
static char obj[NFS_MAXPATHLEN * MAX_ENTRIES];
char scratch[NFS_MAXPATHLEN];
/* check upper part of cookie */
upper = cookie & 0xFFFFFFFF00000000ULL;
if (cookie != 0 && upper != rcookie) {
/* ignore cookie if unexpected so we restart from the beginning */
cookie = 0;
}
cookie &= 0xFFFFFFFFULL;
/* we refuse to return more than 4k from READDIR */
if (count > 4096)
count = 4096;
/* account for size of information heading resok structure */
real_count = RESOK_SIZE;
/* We are always returning zero as a cookie verifier. One reason for this
is that stat() on Windows seems to return cached st_mtime values,
which gives spurious NFS3ERR_BAD_COOKIEs. Btw, here's what Peter
Staubach has to say about cookie verifiers:
"From my viewpoint, the cookieverifier was a failed experiment in NFS
Version 3. The semantics were never well understood nor supported by
many local file systems. The Solaris NFS server always returns zeros
in the cookieverifier field." */
memset(verf, 0, NFS3_COOKIEVERFSIZE);
search = backend_opendir(path);
if (!search) {
if ((exports_opts & OPT_REMOVABLE) && (export_point(path))) {
/* Removable media export point; probably no media inserted.
Return empty directory. */
memset(resok.cookieverf, 0, NFS3_COOKIEVERFSIZE);
resok.reply.entries = NULL;
resok.reply.eof = TRUE;
result.status = NFS3_OK;
result.READDIR3res_u.resok = resok;
return result;
} else {
result.status = readdir_err();
return result;
}
}
this = backend_readdir(search);
/* We cannot use telldir()/seekdir(), since the value from telldir() is
not valid after closedir(). */
for (i = 0; i < cookie; i++)
if (this)
this = backend_readdir(search);
i = 0;
entry[0].name = NULL;
while (this && real_count < count && i < MAX_ENTRIES) {
if (i > 0)
entry[i - 1].nextentry = &entry[i];
if (strlen(path) + strlen(this->d_name) + 1 < NFS_MAXPATHLEN) {
if (strcmp(path, "/") == 0)
sprintf(scratch, "/%s", this->d_name);
else
sprintf(scratch, "%s/%s", path, this->d_name);
res = backend_lstat(scratch, &buf);
if (res == -1) {
result.status = readdir_err();
backend_closedir(search);
return result;
}
strcpy(&obj[i * NFS_MAXPATHLEN], this->d_name);
#if defined(WIN32) || defined(AFS_SUPPORT)
/* See comment in attr.c:get_post_buf */
entry[i].fileid = (buf.st_ino >> 32) ^ (buf.st_ino & 0xffffffff);
#else
entry[i].fileid = buf.st_ino;
#endif
entry[i].name = &obj[i * NFS_MAXPATHLEN];
entry[i].cookie = (cookie + 1 + i) | rcookie;
entry[i].nextentry = NULL;
/* account for entry size */
real_count += ENTRY_SIZE + NAME_SIZE(this->d_name);
/* whoops, overflowed the maximum size */
if (real_count > count && i > 0)
entry[i - 1].nextentry = NULL;
else {
/* advance to next entry */
this = backend_readdir(search);
}
i++;
} else {
result.status = NFS3ERR_IO;
backend_closedir(search);
return result;
}
}
backend_closedir(search);
if (entry[0].name)
resok.reply.entries = &entry[0];
else
resok.reply.entries = NULL;
if (this)
resok.reply.eof = FALSE;
else
resok.reply.eof = TRUE;
memcpy(resok.cookieverf, verf, NFS3_COOKIEVERFSIZE);
result.status = NFS3_OK;
result.READDIR3res_u.resok = resok;
return result;
}
READDIRPLUS3res read_dir_plus(const char *path, cookie3 cookie, cookieverf3 verf,
count3 dircount, count3 maxcount, struct svc_req * req)
{
READDIRPLUS3res result;
READDIRPLUS3resok resok;
cookie3 upper;
static entryplus3 entry[MAX_ENTRIES];
backend_statstruct buf;
int res;
backend_dirstream *search;
struct dirent *this;
count3 i, real_count;
static char obj[NFS_MAXPATHLEN * MAX_ENTRIES];
char scratch[NFS_MAXPATHLEN];
/* check upper part of cookie */
upper = cookie & 0xFFFFFFFF00000000ULL;
if (cookie != 0 && upper != rcookie) {
/* ignore cookie if unexpected so we restart from the beginning */
cookie = 0;
}
cookie &= 0xFFFFFFFFULL;
/* we refuse to return more than 4k from READDIR */
if (dircount > 4096)
dircount = 4096;
if(dircount==0 || maxcount==0)
{
memset(resok.cookieverf, 0, NFS3_COOKIEVERFSIZE);
resok.reply.entries = NULL;
resok.reply.eof = TRUE;
result.status = NFS3_OK;
result.READDIRPLUS3res_u.resok = resok;
return result;
}
/* account for size of information heading resok structure */
real_count = RESOK_SIZE; //size is the same with READDIR3resok
/* We are always returning zero as a cookie verifier. One reason for this
is that stat() on Windows seems to return cached st_mtime values,
which gives spurious NFS3ERR_BAD_COOKIEs. Btw, here's what Peter
Staubach has to say about cookie verifiers:
"From my viewpoint, the cookieverifier was a failed experiment in NFS
Version 3. The semantics were never well understood nor supported by
many local file systems. The Solaris NFS server always returns zeros
in the cookieverifier field." */
memset(verf, 0, NFS3_COOKIEVERFSIZE);
search = backend_opendir(path);
if (!search) {
if ((exports_opts & OPT_REMOVABLE) && (export_point(path))) {
/* Removable media export point; probably no media inserted.
Return empty directory. */
memset(resok.cookieverf, 0, NFS3_COOKIEVERFSIZE);
resok.reply.entries = NULL;
resok.reply.eof = TRUE;
result.status = NFS3_OK;
result.READDIRPLUS3res_u.resok = resok;
return result;
} else {
result.status = readdir_err();
return result;
}
}
this = backend_readdir(search);
/* We cannot use telldir()/seekdir(), since the value from telldir() is
not valid after closedir(). */
for (i = 0; i < cookie; i++)
if (this)
this = backend_readdir(search);
i = 0;
entry[0].name = NULL;
while (this && real_count < dircount && i < MAX_ENTRIES) {
if (i > 0)
entry[i - 1].nextentry = &entry[i];
if (strlen(path) + strlen(this->d_name) + 1 < NFS_MAXPATHLEN) {
if (strcmp(path, "/") == 0)
sprintf(scratch, "/%s", this->d_name);
else
sprintf(scratch, "%s/%s", path, this->d_name);
res = backend_lstat(scratch, &buf);
if (res == -1) {
result.status = readdir_err();
backend_closedir(search);
return result;
}
strcpy(&obj[i * NFS_MAXPATHLEN], this->d_name);
#ifdef WIN32
/* See comment in attr.c:get_post_buf */
entry[i].fileid = (buf.st_ino >> 32) ^ (buf.st_ino & 0xffffffff);
#else
entry[i].fileid = buf.st_ino;
#endif
entry[i].name = &obj[i * NFS_MAXPATHLEN];
entry[i].cookie = (cookie + 1 + i) | rcookie;
entry[i].nextentry = NULL;
entry[i].name_attributes = get_post_stat(path, req);
/* account for entry size */
real_count += ENTRY_SIZE + NAME_SIZE(this->d_name);
/* whoops, overflowed the maximum size */
if (real_count > dircount && i > 0)
entry[i - 1].nextentry = NULL;
else {
/* advance to next entry */
this = backend_readdir(search);
}
i++;
} else {
result.status = NFS3ERR_IO;
backend_closedir(search);
return result;
}
}
backend_closedir(search);
if (entry[0].name)
resok.reply.entries = &entry[0];
else
resok.reply.entries = NULL;
if (this)
resok.reply.eof = FALSE;
else
resok.reply.eof = TRUE;
memcpy(resok.cookieverf, verf, NFS3_COOKIEVERFSIZE);
result.status = NFS3_OK;
result.READDIRPLUS3res_u.resok = resok;
return result;
}