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
|
/* $FreeBSD$ */
/* $Id: nfs4_dev.c,v 1.10 2003/11/05 14:58:59 rees Exp $ */
/*-
* copyright (c) 2003
* the regents of the university of michigan
* all rights reserved
*
* permission is granted to use, copy, create derivative works and redistribute
* this software and such derivative works for any purpose, so long as the name
* of the university of michigan is not used in any advertising or publicity
* pertaining to the use or distribution of this software without specific,
* written prior authorization. if the above copyright notice or any other
* identification of the university of michigan is included in any copy of any
* portion of this software, then the disclaimer below must also be included.
*
* this software is provided as is, without representation from the university
* of michigan as to its fitness for any purpose, and without warranty by the
* university of michigan of any kind, either express or implied, including
* without limitation the implied warranties of merchantability and fitness for
* a particular purpose. the regents of the university of michigan shall not be
* liable for any damages, including special, indirect, incidental, or
* consequential damages, with respect to any claim arising out of or in
* connection with the use of the software, even if it has been or is hereafter
* advised of the possibility of such damages.
*/
#include <sys/param.h>
#include <sys/conf.h>
#include <sys/queue.h>
#include <sys/malloc.h>
#include <sys/kernel.h>
#include <sys/poll.h>
#include <sys/mutex.h>
#include <sys/stat.h>
#include <sys/systm.h>
#include <sys/proc.h>
#include <sys/wait.h>
#include <sys/signalvar.h>
#include <nfs4client/nfs4_dev.h>
#ifdef NFS4DEVVERBOSE
#define NFS4DEV_DEBUG(X...) printf(X)
#else
#define NFS4DEV_DEBUG(X...)
#endif
#define NFS4DEV_NAME "nfs4"
#define CDEV_MINOR 1
MALLOC_DEFINE(M_NFS4DEV, "nfs4_dev", "NFS4 device");
struct nfs4dev_upcall {
/* request msg */
struct nfs4dev_msg up_reqmsg;
size_t up_reqmsglen;
/* reply (payload only) */
caddr_t up_rep;
size_t * up_replen;
int up_copied; /* non-zero when reply has been copied to
'*up_rep' */
int up_error; /* non-zero if an error occured */
TAILQ_ENTRY(nfs4dev_upcall) up_entry;
};
#define nfs4dev_upcall_get(MP) MALLOC((MP), struct nfs4dev_upcall *, sizeof(struct nfs4dev_upcall), M_NFS4DEV, M_WAITOK | M_ZERO)
#define nfs4dev_upcall_put(MP) FREE((MP), M_NFS4DEV)
static int nfs4dev_nopen = 0;
static struct thread * nfs4dev_reader = NULL;
static struct cdev *nfs4device = 0;
static struct mtx nfs4dev_daemon_mtx;
static int nfs4dev_xid = 0;
/* queue of pending upcalls */
TAILQ_HEAD(, nfs4dev_upcall) nfs4dev_newq;
static struct mtx nfs4dev_newq_mtx;
/* queue of upcalls waiting for replys */
TAILQ_HEAD(, nfs4dev_upcall) nfs4dev_waitq;
static struct mtx nfs4dev_waitq_mtx;
/* dev hooks */
static d_open_t nfs4dev_open;
static d_close_t nfs4dev_close;
static d_ioctl_t nfs4dev_ioctl;
static d_poll_t nfs4dev_poll;
static struct cdevsw nfs4dev_cdevsw = {
#if (__FreeBSD_version > 502102)
.d_version = D_VERSION,
.d_flags = D_NEEDGIANT,
#endif
.d_open = nfs4dev_open,
.d_close = nfs4dev_close,
.d_ioctl = nfs4dev_ioctl,
.d_poll = nfs4dev_poll,
.d_name = NFS4DEV_NAME,
};
static int nfs4dev_reply(caddr_t);
static int nfs4dev_request(caddr_t);
/* Userland requests a new operation to service */
static int
nfs4dev_request(caddr_t addr)
{
struct nfs4dev_upcall * u;
struct nfs4dev_msg * m = (struct nfs4dev_msg *) addr;
mtx_lock(&nfs4dev_newq_mtx);
if (TAILQ_EMPTY(&nfs4dev_newq)) {
mtx_unlock(&nfs4dev_newq_mtx);
return EAGAIN;
}
u = TAILQ_FIRST(&nfs4dev_newq);
TAILQ_REMOVE(&nfs4dev_newq, u, up_entry);
mtx_unlock(&nfs4dev_newq_mtx);
bcopy(&u->up_reqmsg, m, sizeof(struct nfs4dev_msg));
mtx_lock(&nfs4dev_waitq_mtx);
TAILQ_INSERT_TAIL(&nfs4dev_waitq, u, up_entry);
mtx_unlock(&nfs4dev_waitq_mtx);
return 0;
}
static int
nfs4dev_reply(caddr_t addr)
{
struct nfs4dev_upcall * u;
struct nfs4dev_msg * m = (struct nfs4dev_msg *) addr;
int error;
if (m->msg_vers != NFS4DEV_VERSION) {
printf("nfs4dev version mismatch\n");
return EINVAL;
}
if (m->msg_type > NFS4DEV_MAX_TYPE) {
NFS4DEV_DEBUG("nfs4dev: unsupported message type\n");
return EINVAL;
}
if (m->msg_len < sizeof(*m) - NFS4DEV_MSG_MAX_DATALEN ||
m->msg_len > NFS4DEV_MSG_MAX_DATALEN) {
NFS4DEV_DEBUG("bad message length\n");
return EINVAL;
}
/* match the reply with a request */
mtx_lock(&nfs4dev_waitq_mtx);
TAILQ_FOREACH(u, &nfs4dev_waitq, up_entry) {
if (m->msg_xid == u->up_reqmsg.msg_xid) {
if (m->msg_type == u->up_reqmsg.msg_type)
goto found;
NFS4DEV_DEBUG("nfs4dev: op type mismatch!\n");
break;
}
}
mtx_unlock(&nfs4dev_waitq_mtx);
NFS4DEV_DEBUG("nfs4dev msg op: %d xid: %x not found.\n",
m->msg_type, m->msg_xid);
error = EIO;
goto bad;
found:
TAILQ_REMOVE(&nfs4dev_waitq, u, up_entry);
mtx_unlock(&nfs4dev_waitq_mtx);
if (m->msg_error) {
error = m->msg_error;
goto bad;
}
if (m->msg_len > *u->up_replen) {
error = EFAULT;
goto bad;
}
bcopy(m->msg_data, u->up_rep, m->msg_len);
*u->up_replen = m->msg_len;
u->up_copied = m->msg_len;
wakeup(u);
return 0;
bad:
if (u) {
u->up_error = error;
wakeup(u);
}
return error;
}
void
nfs4dev_init(void)
{
nfs4dev_xid = arc4random();
TAILQ_INIT(&nfs4dev_newq);
TAILQ_INIT(&nfs4dev_waitq);
mtx_init(&nfs4dev_newq_mtx, "nfs4dev newq", NULL, MTX_DEF);
mtx_init(&nfs4dev_waitq_mtx, "nfs4dev waitq", NULL, MTX_DEF);
mtx_init(&nfs4dev_daemon_mtx, "nfs4dev state", NULL, MTX_DEF);
nfs4device = make_dev(&nfs4dev_cdevsw, CDEV_MINOR, (uid_t)0, (gid_t)0,
S_IRUSR | S_IWUSR, "nfs4");
}
void
nfs4dev_uninit(void)
{
struct proc * dead = NULL;
mtx_lock(&nfs4dev_daemon_mtx);
if (nfs4dev_nopen) {
if (nfs4dev_reader == NULL) {
NFS4DEV_DEBUG("nfs4dev uninit(): unregistered reader\n");
} else {
dead = nfs4dev_reader->td_proc;
}
}
mtx_unlock(&nfs4dev_daemon_mtx);
if (dead != NULL) {
NFS4DEV_DEBUG("nfs4dev_uninit(): you forgot to kill attached daemon (pid: %u)\n",
dead->p_pid);
PROC_LOCK(dead);
psignal(dead, SIGTERM);
PROC_UNLOCK(dead);
}
/* XXX moot? */
nfs4dev_purge();
mtx_destroy(&nfs4dev_newq_mtx);
mtx_destroy(&nfs4dev_waitq_mtx);
mtx_destroy(&nfs4dev_daemon_mtx);
destroy_dev(nfs4device);
}
/* device interface functions */
static int
nfs4dev_open(struct cdev *dev, int flags, int fmt, d_thread_t *td)
{
if (dev != nfs4device)
return ENODEV;
mtx_lock(&nfs4dev_daemon_mtx);
if (nfs4dev_nopen) {
mtx_unlock(&nfs4dev_daemon_mtx);
return EBUSY;
}
nfs4dev_nopen++;
nfs4dev_reader = curthread;
mtx_unlock(&nfs4dev_daemon_mtx);
return (0);
}
static int
nfs4dev_close(struct cdev *dev, int flags, int fmt, d_thread_t *td)
{
struct nfs4dev_upcall * u;
if (dev != nfs4device)
return ENODEV;
mtx_lock(&nfs4dev_daemon_mtx);
if (!nfs4dev_nopen) {
mtx_unlock(&nfs4dev_daemon_mtx);
return ENOENT;
}
nfs4dev_nopen--;
nfs4dev_reader = NULL;
mtx_unlock(&nfs4dev_daemon_mtx);
mtx_lock(&nfs4dev_waitq_mtx);
while (!TAILQ_EMPTY(&nfs4dev_waitq)) {
u = TAILQ_FIRST(&nfs4dev_waitq);
TAILQ_REMOVE(&nfs4dev_waitq, u, up_entry);
u->up_error = EINTR;
wakeup(u);
}
mtx_unlock(&nfs4dev_waitq_mtx);
return 0;
}
static int
nfs4dev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
{
int error;
if (dev != nfs4device)
return ENODEV;
if (data == NULL)
return EFAULT;
if (nfs4dev_reader != curthread)
nfs4dev_reader = curthread;
switch (cmd) {
case NFS4DEVIOCGET:
error = nfs4dev_request(data);
break;
case NFS4DEVIOCPUT:
error = nfs4dev_reply(data);
break;
default:
NFS4DEV_DEBUG("nfs4dev_ioctl: unkown ioctl cmd %d\n", (int)cmd);
error = EOPNOTSUPP;
break;
}
return error;
}
static int
nfs4dev_poll(struct cdev *dev, int events, struct thread *td)
{
int revents;
if (dev != nfs4device)
return EINVAL;
mtx_lock(&nfs4dev_daemon_mtx);
if (nfs4dev_nopen == 0) {
mtx_unlock(&nfs4dev_daemon_mtx);
return 0;
}
mtx_unlock(&nfs4dev_daemon_mtx);
revents = 0;
/* check readable data */
mtx_lock(&nfs4dev_newq_mtx);
if (!TAILQ_EMPTY(&nfs4dev_newq))
revents |= POLLIN;
mtx_unlock(&nfs4dev_newq_mtx);
mtx_lock(&nfs4dev_waitq_mtx);
if (!TAILQ_EMPTY(&nfs4dev_waitq))
revents |= POLLOUT;
mtx_unlock(&nfs4dev_waitq_mtx);
return revents;
}
int
nfs4dev_call(uint32_t type, caddr_t req_data, size_t req_len, caddr_t rep_data, size_t * rep_lenp)
{
struct nfs4dev_upcall * u;
int error = 0;
unsigned int xtmp;
mtx_lock(&nfs4dev_daemon_mtx);
if (nfs4dev_nopen == 0) {
mtx_unlock(&nfs4dev_daemon_mtx);
return EINVAL;
}
mtx_unlock(&nfs4dev_daemon_mtx);
if (type > NFS4DEV_MAX_TYPE)
return EOPNOTSUPP;
NFS4DEV_DEBUG("upcall %d/%d:%d\n", type, req_len, *rep_lenp);
nfs4dev_upcall_get(u);
u->up_error = 0;
u->up_rep = rep_data;
u->up_replen = rep_lenp;
u->up_copied = 0;
u->up_reqmsg.msg_vers = NFS4DEV_VERSION;
/* XXX efficient copying */
bcopy(req_data, u->up_reqmsg.msg_data, req_len);
u->up_reqmsg.msg_len = req_len;
mtx_lock(&nfs4dev_newq_mtx);
/* get new XID */
while ((xtmp = arc4random() % 256) == 0);
nfs4dev_xid += xtmp;
u->up_reqmsg.msg_xid = nfs4dev_xid;
TAILQ_INSERT_TAIL(&nfs4dev_newq, u, up_entry);
mtx_unlock(&nfs4dev_newq_mtx);
NFS4DEV_DEBUG("nfs4dev op: %d xid: %x sleeping\n", u->up_reqmsg.msg_type, u->up_reqmsg.msg_xid);
do {
tsleep(u, PLOCK, "nfs4dev", 0);
} while (u->up_copied == 0 && u->up_error == 0);
/* upcall now removed from the queue */
NFS4DEV_DEBUG("nfs4dev prog: %d xid: %x continues...\n",
u->up_reqmsg.msg_type, u->up_reqmsg.msg_xid);
if (u->up_error) {
error = u->up_error;
NFS4DEV_DEBUG("nfs4dev prog: %d xid: %x error: %d\n",
u->up_reqmsg.msg_type, u->up_reqmsg.msg_xid, u->up_error);
goto out;
}
out:
nfs4dev_upcall_put(u);
return error;
}
void
nfs4dev_purge(void)
{
struct nfs4dev_upcall * u;
mtx_lock(&nfs4dev_newq_mtx);
while (!TAILQ_EMPTY(&nfs4dev_newq)) {
u = TAILQ_FIRST(&nfs4dev_newq);
TAILQ_REMOVE(&nfs4dev_newq, u, up_entry);
u->up_error = EINTR;
wakeup(u);
}
mtx_unlock(&nfs4dev_newq_mtx);
mtx_lock(&nfs4dev_waitq_mtx);
while (!TAILQ_EMPTY(&nfs4dev_waitq)) {
u = TAILQ_FIRST(&nfs4dev_waitq);
TAILQ_REMOVE(&nfs4dev_waitq, u, up_entry);
u->up_error = EINTR;
wakeup(u);
}
mtx_unlock(&nfs4dev_waitq_mtx);
}
|