This repository has been archived by the owner on Jan 16, 2021. It is now read-only.
/
nfsserv.h
279 lines (244 loc) · 8.04 KB
/
nfsserv.h
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
// -*-c++-*-
/* $Id$ */
/*
*
* Copyright (C) 2000-2002 David Mazieres (dm@uun.org)
*
* 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, 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
*
*/
#ifndef _SFSMISC_NFSSERV_H_
#define _SFSMISC_NFSSERV_H_ 1
/*
* This file adds a layer of indirection between the RPC layer and
* code implementing an NFS3 server. Thus, one can reuse the server
* code with non-NFS3 clients by adding a translation layer (e.g.,
* from UVFS to NFS3, or NFS2 to NFS3). The class adds one fake NFS
* call, CLOSE, which in NFS must be simulated using timing
* heuristics, but can be implemented reliably under UVFS.
*/
#include "arpc.h"
#include "nfs3_prot.h"
#include "nfs3close_prot.h"
#include "blowfish.h"
#include "sfsmisc.h"
#include "qhash.h"
#include "nfstrans.h"
#if 0
enum { lastnfscall = NFSPROC3_COMMIT };
enum { NFSPROC_CLOSE = lastnfscall+1 };
#else
enum { NFSPROC_CLOSE = cl_NFSPROC3_CLOSE };
#endif
template<int N> struct nfs3proc {};
#define defineproc(proc, arg, res) \
template<> struct nfs3proc<proc> { \
typedef arg arg_type; \
typedef res res_type; \
};
NFS_PROGRAM_3_APPLY (defineproc)
defineproc (NFSPROC_CLOSE, nfs_fh3, nfsstat3)
#undef defineproc
struct nfsserv;
struct nfscall {
static const rpcgen_table closert;
const authunix_parms *const aup;
const u_int32_t procno;
void *const argp;
void *resp;
sfs::xdrproc_t xdr_res;
accept_stat acstat;
auth_stat austat;
const time_t rqtime;
bool nocache;
bool nofree;
nfsserv *stopserv;
nfsserv *curserv;
nfscall (const authunix_parms *aup, u_int32_t p, void *a);
virtual ~nfscall () { clearres (); }
void sendreply ();
void setreply (void *, sfs::xdrproc_t = NULL, bool nc = false);
void reply (void *, sfs::xdrproc_t = NULL, bool nc = false);
void seterr (nfsstat3 err);
void error (nfsstat3 err) { seterr (err); sendreply (); }
void reject (accept_stat s) { acstat = s; error (NFS3ERR_IO); }
void reject (auth_stat s) { austat = s; error (NFS3ERR_PERM); }
const rpcgen_table &getrpct () const;
void pinres ();
sfs_aid getaid () const { return aup2aid (aup); }
const authunix_parms *getaup () const { return aup; }
u_int32_t proc () const { return procno; }
void *getvoidarg () { return argp; }
nfs_fh3 *getfh3arg () { return static_cast<nfs_fh3 *> (argp); }
template<class T> T *getarg () {
#ifdef CHECK_BOUNDS
assert ((typeid (T) == typeid (nfs_fh3) && proc () != NFSPROC3_NULL)
|| typeid (T) == *getrpct ().type_arg);
#endif /* !CHECK_BOUNDS */
return static_cast<T *> (getvoidarg ());
}
template<class T> const T *getarg () const {
return const_cast<nfscall *> (this)->template getarg<T> ();
}
void clearres ();
void *getvoidres ();
template<class T> T *getres () { return static_cast<T *> (getvoidres ()); }
};
template<int N> class nfscall_cb : public nfscall {
typedef typename nfs3proc<N>::arg_type *arg_type;
typedef typename nfs3proc<N>::res_type *res_type;
typedef ref<callback<void, res_type> > cb_t;
cb_t cb;
public:
nfscall_cb (const authunix_parms *au, arg_type a, cb_t c, nfsserv *srv);
~nfscall_cb () {
/* Note, if xdr_res is not the default, we could always marshall
* and unmarshall the result to get it in the right type. That
* would be slow, however, and if it were actually happening, it
* would probably indicate something wrong with the code. Thus,
* we force an assertion failure in this case. */
assert (!xdr_res);
(*cb) (static_cast<res_type> (resp));
}
};
struct nfscall_rpc : nfscall {
svccb *sbp;
nfscall_rpc (svccb *sbp);
~nfscall_rpc ();
};
struct nfsserv : public virtual refcount {
typedef callback<void, nfscall *>::ref cb_t;
static const cb_t stalecb;
cb_t cb;
const ptr<nfsserv> nextserv;
explicit nfsserv (ptr<nfsserv> n = NULL);
void setcb (const cb_t &c) { cb = c; }
void mkcb (nfscall *nc) { nc->curserv = this; (*cb) (nc); }
virtual void getcall (nfscall *nc) { mkcb (nc); }
virtual void getreply (nfscall *nc) { nc->sendreply (); }
virtual bool encodefh (nfs_fh3 &fh);
};
template<int N> inline
nfscall_cb<N>::nfscall_cb (const authunix_parms *au, arg_type a, cb_t c,
nfsserv *srv)
: nfscall (au, N, a), cb (c)
{
if ((stopserv = srv))
srv->mkcb (this);
}
class nfsserv_udp : public nfsserv {
int fd;
ptr<axprt> x;
ptr<asrv> s;
void getsbp (svccb *sbp);
public:
nfsserv_udp ();
nfsserv_udp (const rpc_program&);
// void getreply (nfscall *nc);
int getfd () { return fd; }
};
/* Work around some bugs in NFS servers */
class nfsserv_fixup : public nfsserv {
void getattr (nfscall *nc, nfs_fh3 *, getattr3res *res);
public:
explicit nfsserv_fixup (ref<nfsserv> s) : nfsserv (s) {}
void getreply (nfscall *nc);
};
struct nfsserv_link : public nfsserv {
ref<nfsserv> next;
nfsserv_link (const ref<nfsserv> &n) : next (n) {}
virtual void getreply (nfscall *);
};
class nfsdemux : public virtual refcount {
public:
struct nfsserv_cryptfh : public nfsserv {
const ref<nfsdemux> d;
const u_int32_t srvno;
ihash_entry <nfsserv_cryptfh> hlink;
nfsserv_cryptfh (const ref<nfsdemux> &dd, u_int32_t s);
~nfsserv_cryptfh ();
virtual void getcall (nfscall *nc) { mkcb (nc); }
virtual void getreply (nfscall *nc);
bool encodefh (nfs_fh3 &fh);
};
friend class nfsserv_cryptfh;
private:
const ref<nfsserv> ns;
u_int32_t srvnoctr;
ihash<const u_int32_t, nfsserv_cryptfh,
&nfsserv_cryptfh::srvno, &nfsserv_cryptfh::hlink> srvnotab;
void getcall (nfscall *nc);
public:
blowfish fhkey;
nfsdemux (const ref<nfsserv> &n);
ref<nfsserv_cryptfh> servalloc ();
};
/* Attribute caching manipulator.
*
* This server expects
*
* Warning, this server must be pushed after (i.e., later in the
* stream than) any servers that manipulate (e.g., encrypt/decrypt)
* file handles.
*/
class attr_cache {
struct access_dat {
u_int32_t mask;
u_int32_t perm;
access_dat (u_int32_t m, u_int32_t p) : mask (m), perm (p) {}
};
public:
struct attr_dat {
attr_cache *const cache;
const nfs_fh3 fh;
fattr3exp attr;
qhash<sfs_aid, access_dat> access;
ihash_entry<attr_dat> fhlink;
tailq_entry<attr_dat> lrulink;
attr_dat (attr_cache *c, const nfs_fh3 &f, const fattr3exp *a);
~attr_dat ();
void touch ();
void set (const fattr3exp *a, const wcc_attr *w);
bool valid ()
{ return sfs_get_timenow() < implicit_cast<time_t> (attr.expire); }
};
private:
friend class attr_dat;
ihash<const nfs_fh3, attr_dat, &attr_dat::fh, &attr_dat::fhlink> attrs;
static void remove_aid (sfs_aid aid, attr_dat *ad)
{ ad->access.remove (aid); }
public:
~attr_cache () { attrs.deleteall (); }
void flush_attr () { attrs.deleteall (); }
void flush_access (sfs_aid aid) { attrs.traverse (wrap (remove_aid, aid)); }
void flush_access (const nfs_fh3 &fh, sfs_aid);
void attr_enter (const nfs_fh3 &, const fattr3exp *, const wcc_attr *);
void attr_enter (const nfs_fh3 &fh, const ex_fattr3 *a, const wcc_attr *w)
{ attr_enter (fh, reinterpret_cast<const fattr3exp *> (a), w); }
const fattr3exp *attr_lookup (const nfs_fh3 &);
void access_enter (const nfs_fh3 &, sfs_aid aid,
u_int32_t mask, u_int32_t perm);
int32_t access_lookup (const nfs_fh3 &, sfs_aid, u_int32_t mask);
};
class nfsserv_ac : public nfsserv {
public:
attr_cache ac;
explicit nfsserv_ac (ref<nfsserv> s) : nfsserv (s) {}
void getcall (nfscall *nc);
void getreply (nfscall *nc);
};
ref<nfsserv> close_simulate (ref<nfsserv> ns);
#endif /* _SFSMISC_NFSSERV_H_ */