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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
|
/*
Copyright (C) 1996, 1998, 1999, 2000 Free Software Foundation, Inc.
Written by Michael I. Bushnell, p/BSG.
This file is part of the GNU Hurd.
The GNU Hurd 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, or (at
your option) any later version.
The GNU Hurd 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., 59 Temple Place - Suite 330, Boston, MA 02111, USA. */
#include <string.h>
#include <sys/mman.h>
#include <hurd/fsys.h>
#include <assert.h>
#include <string.h>
#include <hurd/io.h>
#include <hurd/auth.h>
#include "nfsd.h"
#undef TRUE
#undef FALSE
#define malloc spoogie_woogie /* ugh^2. */
#include <rpc/types.h>
#include <rpc/auth.h>
#undef malloc
#define IDHASH_TABLE_SIZE 1024
#define FHHASH_TABLE_SIZE 1024
#define REPLYHASH_TABLE_SIZE 1024
static struct idspec *idhashtable[IDHASH_TABLE_SIZE];
spin_lock_t idhashlock = SPIN_LOCK_INITIALIZER;
static int nfreeids;
static int leastidlastuse;
/* Compare I against the specified set of users/groups. */
/* Use of int in decl of UIDS and GIDS is correct here; that's
the NFS type because they come in in known 32 bit slots. */
static int
idspec_compare (struct idspec *i, int nuids, int ngids,
int *uids, int *gids)
{
if (i->nuids != nuids
|| i->ngids != ngids)
return 0;
assert (sizeof (int) == sizeof (uid_t));
if (bcmp (i->uids, uids, nuids * sizeof (uid_t))
|| bcmp (i->gids, gids, ngids * sizeof (gid_t)))
return 0;
return 1;
}
/* Compute a hash value for a given user spec */
static int
idspec_hash (int nuids, int ngids, int *uids, int *gids)
{
int hash, n;
hash = nuids + ngids;
for (n = 0; n < ngids; n++)
hash += gids[n];
for (n = 0; n < nuids; n++)
hash += uids[n];
hash %= IDHASH_TABLE_SIZE;
return hash;
}
/* Lookup a user spec in the hash table and allocate a reference */
static struct idspec *
idspec_lookup (int nuids, int ngids, int *uids, int *gids)
{
int hash;
struct idspec *i;
hash = idspec_hash (nuids, ngids, uids, gids);
spin_lock (&idhashlock);
for (i = idhashtable[hash]; i; i = i->next)
if (idspec_compare (i, nuids, ngids, uids, gids))
{
i->references++;
if (i->references == 1)
nfreeids--;
spin_unlock (&idhashlock);
return i;
}
assert (sizeof (uid_t) == sizeof (int));
i = malloc (sizeof (struct idspec));
i->nuids = nuids;
i->ngids = ngids;
i->uids = malloc (nuids * sizeof (uid_t));
i->gids = malloc (ngids * sizeof (gid_t));
bcopy (uids, i->uids, nuids * sizeof (uid_t));
bcopy (gids, i->gids, ngids * sizeof (gid_t));
i->references = 1;
i->next = idhashtable[hash];
if (idhashtable[hash])
idhashtable[hash]->prevp = &i->next;
i->prevp = &idhashtable[hash];
idhashtable[hash] = i;
spin_unlock (&idhashlock);
return i;
}
int *
process_cred (int *p, struct idspec **credp)
{
int type;
int len;
int *uid;
int *gids;
int ngids;
int firstgid;
int i;
type = ntohl (*p++);
if (type != AUTH_UNIX)
{
int size = ntohl (*p++);
*credp = idspec_lookup (0, 0, 0, 0);
p += INTSIZE (size);
}
else
{
p++; /* skip size */
p++; /* skip seconds */
len = ntohl (*p++);
p += INTSIZE (len); /* skip hostname */
uid = p++; /* remember loc of uid */
*uid = ntohl (*uid);
firstgid = *p++; /* remember first gid */
gids = p; /* here's where the array will start */
ngids = ntohl (*p++);
/* Now swap the first gid to be the first element of the array */
*gids = firstgid;
ngids++; /* and count it */
/* And byteswap the gids */
for (i = 0; i < ngids; i++)
gids[i] = ntohl (gids[i]);
p += ngids - 1;
*credp = idspec_lookup (1, ngids, uid, gids);
}
/* Next is the verf field; skip it entirely */
p++; /* skip id */
len = htonl (*p++);
p += INTSIZE (len);
return p;
}
void
cred_rele (struct idspec *i)
{
spin_lock (&idhashlock);
i->references--;
if (i->references == 0)
{
i->lastuse = mapped_time->seconds;
if (i->lastuse < leastidlastuse || nfreeids == 0)
leastidlastuse = i->lastuse;
nfreeids++;
}
spin_unlock (&idhashlock);
}
void
cred_ref (struct idspec *i)
{
spin_lock (&idhashlock);
assert (i->references);
i->references++;
spin_unlock (&idhashlock);
}
void
scan_creds ()
{
struct idspec *i;
int n;
int newleast = mapped_time->seconds;
spin_lock (&idhashlock);
if (mapped_time->seconds - leastidlastuse > ID_KEEP_TIMEOUT)
for (n = 0; n < IDHASH_TABLE_SIZE && nfreeids; n++)
for (i = idhashtable[n]; i && nfreeids; i = i->next)
{
if (!i->references
&& mapped_time->seconds - i->lastuse > ID_KEEP_TIMEOUT)
{
nfreeids--;
*i->prevp = i->next;
if (i->next)
i->next->prevp = i->prevp;
free (i->uids);
free (i->gids);
free (i);
}
else if (!i->references && newleast > i->lastuse)
newleast = i->lastuse;
}
/* If we didn't bail early, then this is valid */
if (nfreeids)
leastidlastuse = newleast;
spin_unlock (&idhashlock);
}
static struct cache_handle *fhhashtable[FHHASH_TABLE_SIZE];
struct mutex fhhashlock = MUTEX_INITIALIZER;
static int nfreefh;
static int leastfhlastuse;
static int
fh_hash (char *fhandle, struct idspec *i)
{
int hash = 0, n;
for (n = 0; n < NFS2_FHSIZE; n++)
hash += fhandle[n];
hash += (int) i >> 6;
return hash % FHHASH_TABLE_SIZE;
}
int *
lookup_cache_handle (int *p, struct cache_handle **cp, struct idspec *i)
{
int hash;
struct cache_handle *c;
fsys_t fsys;
file_t port;
hash = fh_hash ((char *)p, i);
mutex_lock (&fhhashlock);
for (c = fhhashtable[hash]; c; c = c->next)
if (c->ids == i && ! bcmp (c->handle, p, NFS2_FHSIZE))
{
if (c->references == 0)
nfreefh--;
c->references++;
mutex_unlock (&fhhashlock);
*cp = c;
return p + NFS2_FHSIZE / sizeof (int);
}
/* Not found */
/* First four bytes are our internal table of filesystems */
fsys = lookup_filesystem (*p);
if (fsys == MACH_PORT_NULL
|| fsys_getfile (fsys, i->uids, i->nuids, i->gids, i->ngids,
(char *)(p + 1), NFS2_FHSIZE - sizeof (int), &port))
{
mutex_unlock (&fhhashlock);
*cp = 0;
return p + NFS2_FHSIZE / sizeof (int);
}
c = malloc (sizeof (struct cache_handle));
bcopy (p, c->handle, NFS2_FHSIZE);
cred_ref (i);
c->ids = i;
c->port = port;
c->references = 1;
c->next = fhhashtable[hash];
if (c->next)
c->next->prevp = &c->next;
c->prevp = &fhhashtable[hash];
fhhashtable[hash] = c;
mutex_unlock (&fhhashlock);
*cp = c;
return p + NFS2_FHSIZE / sizeof (int);
}
void
cache_handle_rele (struct cache_handle *c)
{
mutex_lock (&fhhashlock);
c->references--;
if (c->references == 0)
{
c->lastuse = mapped_time->seconds;
if (c->lastuse < leastfhlastuse || nfreefh == 0)
leastfhlastuse = c->lastuse;
nfreefh++;
}
mutex_unlock (&fhhashlock);
}
void
scan_fhs ()
{
struct cache_handle *c;
int n;
int newleast = mapped_time->seconds;
mutex_lock (&fhhashlock);
if (mapped_time->seconds - leastfhlastuse > FH_KEEP_TIMEOUT)
for (n = 0; n < FHHASH_TABLE_SIZE && nfreefh; n++)
for (c = fhhashtable[n]; c && nfreefh; c = c->next)
{
if (!c->references
&& mapped_time->seconds - c->lastuse > FH_KEEP_TIMEOUT)
{
nfreefh--;
*c->prevp = c->next;
if (c->next)
c->next->prevp = c->prevp;
cred_rele (c->ids);
mach_port_deallocate (mach_task_self (), c->port);
free (c);
}
else if (!c->references && newleast > c->lastuse)
newleast = c->lastuse;
}
/* If we didn't bail early, then this is valid. */
if (nfreefh)
leastfhlastuse = newleast;
mutex_unlock (&fhhashlock);
}
struct cache_handle *
create_cached_handle (int fs, struct cache_handle *credc, file_t userport)
{
char fhandle[NFS2_FHSIZE];
error_t err;
struct cache_handle *c;
int hash;
char *bp = fhandle + sizeof (int);
size_t handlelen = NFS2_FHSIZE - sizeof (int);
mach_port_t newport, ref;
/* Authenticate USERPORT so that we can call file_getfh on it. */
ref = mach_reply_port ();
/* MAKE_SEND is safe becaue we destroy REF ourselves. */
if (io_reauthenticate (userport, ref, MACH_MSG_TYPE_MAKE_SEND)
|| auth_user_authenticate (authserver, ref, MACH_MSG_TYPE_MAKE_SEND,
&newport))
{
/* Reauthentication has failed, but maybe the filesystem will let
us call file_getfh anyway. */
newport = userport;
}
else
mach_port_deallocate (mach_task_self (), userport);
mach_port_destroy (mach_task_self (), ref);
/* Fetch the file handle */
*(int *)fhandle = fs;
err = file_getfh (newport, &bp, &handlelen);
mach_port_deallocate (mach_task_self (), newport);
if (err || handlelen != NFS2_FHSIZE - sizeof (int))
return 0;
if (bp != fhandle + sizeof (int))
{
bcopy (bp, fhandle + sizeof (int), NFS2_FHSIZE - sizeof (int));
munmap (bp, handlelen);
}
/* Cache it */
hash = fh_hash (fhandle, credc->ids);
mutex_lock (&fhhashlock);
for (c = fhhashtable[hash]; c; c = c->next)
if (c->ids == credc->ids && ! bcmp (fhandle, c->handle, NFS2_FHSIZE))
{
/* Return this one */
if (c->references == 0)
nfreefh--;
c->references++;
mutex_unlock (&fhhashlock);
return c;
}
/* Always call fsys_getfile so that we don't depend on the
particular open modes of the port passed in. */
err = fsys_getfile (lookup_filesystem (fs),
credc->ids->uids, credc->ids->nuids,
credc->ids->gids, credc->ids->ngids,
fhandle + sizeof (int), NFS2_FHSIZE - sizeof (int),
&newport);
if (err)
{
mutex_unlock (&fhhashlock);
return 0;
}
/* Create it anew */
c = malloc (sizeof (struct cache_handle));
bcopy (fhandle, c->handle, NFS2_FHSIZE);
cred_ref (credc->ids);
c->ids = credc->ids;
c->port = newport;
c->references = 1;
/* And add it to the hash table */
c->next = fhhashtable[hash];
if (c->next)
c->next->prevp = &c->next;
c->prevp = &fhhashtable[hash];
fhhashtable[hash] = c;
mutex_unlock (&fhhashlock);
return c;
}
static struct cached_reply *replyhashtable [REPLYHASH_TABLE_SIZE];
static spin_lock_t replycachelock = SPIN_LOCK_INITIALIZER;
static int nfreereplies;
static int leastreplylastuse;
/* Check the list of cached replies to see if this is a replay of a
previous transaction; if so, return the cache record. Otherwise,
create a new cache record. */
struct cached_reply *
check_cached_replies (int xid,
struct sockaddr_in *sender)
{
struct cached_reply *cr;
int hash;
hash = xid % REPLYHASH_TABLE_SIZE;
spin_lock (&replycachelock);
for (cr = replyhashtable[hash]; cr; cr = cr->next)
if (cr->xid == xid
&& !bcmp (sender, &cr->source, sizeof (struct sockaddr_in)))
{
cr->references++;
if (cr->references == 1)
nfreereplies--;
spin_unlock (&replycachelock);
mutex_lock (&cr->lock);
return cr;
}
cr = malloc (sizeof (struct cached_reply));
mutex_init (&cr->lock);
mutex_lock (&cr->lock);
bcopy (sender, &cr->source, sizeof (struct sockaddr_in));
cr->xid = xid;
cr->data = 0;
cr->next = replyhashtable[hash];
if (replyhashtable[hash])
replyhashtable[hash]->prevp = &cr->next;
cr->prevp = &replyhashtable[hash];
replyhashtable[hash] = cr;
spin_unlock (&replycachelock);
return cr;
}
/* A cached reply returned by check_cached_replies is now no longer
needed by its caller. */
void
release_cached_reply (struct cached_reply *cr)
{
mutex_unlock (&cr->lock);
spin_lock (&replycachelock);
cr->references--;
if (cr->references == 0)
{
cr->lastuse = mapped_time->seconds;
if (cr->lastuse < leastreplylastuse || nfreereplies == 0)
leastreplylastuse = cr->lastuse;
nfreereplies++;
}
spin_unlock (&replycachelock);
}
void
scan_replies ()
{
struct cached_reply *cr;
int n;
int newleast = mapped_time->seconds;
spin_lock (&replycachelock);
if (mapped_time->seconds - leastreplylastuse > REPLY_KEEP_TIMEOUT)
for (n = 0; n < REPLYHASH_TABLE_SIZE && nfreereplies; n++)
for (cr = replyhashtable[n]; cr && nfreereplies; cr = cr->next)
{
if (!cr->references
&& mapped_time->seconds - cr->lastuse > REPLY_KEEP_TIMEOUT)
{
nfreereplies--;
*cr->prevp = cr->next;
if (cr->next)
cr->next->prevp = cr->prevp;
if (cr->data)
free (cr->data);
}
else if (!cr->references && newleast > cr->lastuse)
newleast = cr->lastuse;
}
/* If we didn't bail early, then this is valid */
if (nfreereplies)
leastreplylastuse = newleast;
spin_unlock (&replycachelock);
}
|