2 /* $Id: nfs4_dev.c,v 1.10 2003/11/05 14:58:59 rees Exp $ */
6 * the regents of the university of michigan
9 * permission is granted to use, copy, create derivative works and redistribute
10 * this software and such derivative works for any purpose, so long as the name
11 * of the university of michigan is not used in any advertising or publicity
12 * pertaining to the use or distribution of this software without specific,
13 * written prior authorization. if the above copyright notice or any other
14 * identification of the university of michigan is included in any copy of any
15 * portion of this software, then the disclaimer below must also be included.
17 * this software is provided as is, without representation from the university
18 * of michigan as to its fitness for any purpose, and without warranty by the
19 * university of michigan of any kind, either express or implied, including
20 * without limitation the implied warranties of merchantability and fitness for
21 * a particular purpose. the regents of the university of michigan shall not be
22 * liable for any damages, including special, indirect, incidental, or
23 * consequential damages, with respect to any claim arising out of or in
24 * connection with the use of the software, even if it has been or is hereafter
25 * advised of the possibility of such damages.
28 #include <sys/param.h>
30 #include <sys/queue.h>
31 #include <sys/malloc.h>
32 #include <sys/kernel.h>
34 #include <sys/mutex.h>
36 #include <sys/systm.h>
39 #include <sys/signalvar.h>
41 #include <nfs4client/nfs4_dev.h>
44 #define NFS4DEV_DEBUG(X...) printf(X)
46 #define NFS4DEV_DEBUG(X...)
49 #define NFS4DEV_NAME "nfs4"
52 MALLOC_DEFINE(M_NFS4DEV, "NFS4 dev", "NFS4 device");
54 struct nfs4dev_upcall {
56 struct nfs4dev_msg up_reqmsg;
59 /* reply (payload only) */
63 int up_copied; /* non-zero when reply has been copied to
66 int up_error; /* non-zero if an error occured */
68 TAILQ_ENTRY(nfs4dev_upcall) up_entry;
72 #define nfs4dev_upcall_get(MP) MALLOC((MP), struct nfs4dev_upcall *, sizeof(struct nfs4dev_upcall), M_NFS4DEV, M_WAITOK | M_ZERO)
74 #define nfs4dev_upcall_put(MP) FREE((MP), M_NFS4DEV)
76 static int nfs4dev_nopen = 0;
77 static struct thread * nfs4dev_reader = NULL;
78 static struct cdev *nfs4device = 0;
79 static struct mtx nfs4dev_daemon_mtx;
81 static int nfs4dev_xid = 0;
82 /* queue of pending upcalls */
83 TAILQ_HEAD(, nfs4dev_upcall) nfs4dev_newq;
84 static struct mtx nfs4dev_newq_mtx;
86 /* queue of upcalls waiting for replys */
87 TAILQ_HEAD(, nfs4dev_upcall) nfs4dev_waitq;
88 static struct mtx nfs4dev_waitq_mtx;
91 static d_open_t nfs4dev_open;
92 static d_close_t nfs4dev_close;
93 static d_ioctl_t nfs4dev_ioctl;
94 static d_poll_t nfs4dev_poll;
96 static struct cdevsw nfs4dev_cdevsw = {
97 #if (__FreeBSD_version > 502102)
98 .d_version = D_VERSION,
99 .d_flags = D_NEEDGIANT,
101 .d_open = nfs4dev_open,
102 .d_close = nfs4dev_close,
103 .d_ioctl = nfs4dev_ioctl,
104 .d_poll = nfs4dev_poll,
105 .d_name = NFS4DEV_NAME,
108 static int nfs4dev_reply(caddr_t);
109 static int nfs4dev_request(caddr_t);
111 /* Userland requests a new operation to service */
113 nfs4dev_request(caddr_t addr)
115 struct nfs4dev_upcall * u;
116 struct nfs4dev_msg * m = (struct nfs4dev_msg *) addr;
118 mtx_lock(&nfs4dev_newq_mtx);
120 if (TAILQ_EMPTY(&nfs4dev_newq)) {
121 mtx_unlock(&nfs4dev_newq_mtx);
125 u = TAILQ_FIRST(&nfs4dev_newq);
126 TAILQ_REMOVE(&nfs4dev_newq, u, up_entry);
127 mtx_unlock(&nfs4dev_newq_mtx);
129 bcopy(&u->up_reqmsg, m, sizeof(struct nfs4dev_msg));
131 mtx_lock(&nfs4dev_waitq_mtx);
132 TAILQ_INSERT_TAIL(&nfs4dev_waitq, u, up_entry);
133 mtx_unlock(&nfs4dev_waitq_mtx);
139 nfs4dev_reply(caddr_t addr)
141 struct nfs4dev_upcall * u;
142 struct nfs4dev_msg * m = (struct nfs4dev_msg *) addr;
145 if (m->msg_vers != NFS4DEV_VERSION) {
146 printf("nfs4dev version mismatch\n");
150 if (m->msg_type > NFS4DEV_MAX_TYPE) {
151 NFS4DEV_DEBUG("nfs4dev: unsupported message type\n");
155 if (m->msg_len == 0 || m->msg_len > NFS4DEV_MSG_MAX_DATALEN) {
156 NFS4DEV_DEBUG("bad message length\n");
160 /* match the reply with a request */
161 mtx_lock(&nfs4dev_waitq_mtx);
162 TAILQ_FOREACH(u, &nfs4dev_waitq, up_entry) {
163 if (m->msg_xid == u->up_reqmsg.msg_xid) {
164 if (m->msg_type == u->up_reqmsg.msg_type)
166 NFS4DEV_DEBUG("nfs4dev: op type mismatch!\n");
170 mtx_unlock(&nfs4dev_waitq_mtx);
172 NFS4DEV_DEBUG("nfs4dev msg op: %d xid: %x not found.\n",
173 m->msg_type, m->msg_xid);
179 TAILQ_REMOVE(&nfs4dev_waitq, u, up_entry);
180 mtx_unlock(&nfs4dev_waitq_mtx);
183 error = m->msg_error;
187 if (m->msg_len > *u->up_replen) {
192 bcopy(m->msg_data, u->up_rep, m->msg_len);
193 *u->up_replen = m->msg_len;
195 u->up_copied = m->msg_len;
208 nfs4dev_xid = arc4random();
209 TAILQ_INIT(&nfs4dev_newq);
210 TAILQ_INIT(&nfs4dev_waitq);
211 mtx_init(&nfs4dev_newq_mtx, "nfs4dev newq", NULL, MTX_DEF);
212 mtx_init(&nfs4dev_waitq_mtx, "nfs4dev waitq", NULL, MTX_DEF);
214 mtx_init(&nfs4dev_daemon_mtx, "nfs4dev state", NULL, MTX_DEF);
216 nfs4device = make_dev(&nfs4dev_cdevsw, CDEV_MINOR, (uid_t)0, (gid_t)0,
217 S_IRUSR | S_IWUSR, "nfs4");
223 struct proc * dead = NULL;
225 mtx_lock(&nfs4dev_daemon_mtx);
227 if (nfs4dev_reader == NULL) {
228 NFS4DEV_DEBUG("nfs4dev uninit(): unregistered reader\n");
230 dead = nfs4dev_reader->td_proc;
233 mtx_unlock(&nfs4dev_daemon_mtx);
236 NFS4DEV_DEBUG("nfs4dev_uninit(): you forgot to kill attached daemon (pid: %u)\n",
239 psignal(dead, SIGTERM);
246 mtx_destroy(&nfs4dev_newq_mtx);
247 mtx_destroy(&nfs4dev_waitq_mtx);
248 mtx_destroy(&nfs4dev_daemon_mtx);
250 destroy_dev(nfs4device);
253 /* device interface functions */
255 nfs4dev_open(struct cdev *dev, int flags, int fmt, d_thread_t *td)
257 if (dev != nfs4device)
260 mtx_lock(&nfs4dev_daemon_mtx);
262 mtx_unlock(&nfs4dev_daemon_mtx);
267 nfs4dev_reader = curthread;
268 mtx_unlock(&nfs4dev_daemon_mtx);
274 nfs4dev_close(struct cdev *dev, int flags, int fmt, d_thread_t *td)
276 struct nfs4dev_upcall * u;
278 if (dev != nfs4device)
281 mtx_lock(&nfs4dev_daemon_mtx);
282 if (!nfs4dev_nopen) {
283 mtx_unlock(&nfs4dev_daemon_mtx);
288 nfs4dev_reader = NULL;
289 mtx_unlock(&nfs4dev_daemon_mtx);
291 mtx_lock(&nfs4dev_waitq_mtx);
293 while (!TAILQ_EMPTY(&nfs4dev_waitq)) {
294 u = TAILQ_FIRST(&nfs4dev_waitq);
295 TAILQ_REMOVE(&nfs4dev_waitq, u, up_entry);
300 mtx_unlock(&nfs4dev_waitq_mtx);
306 nfs4dev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
310 if (dev != nfs4device)
316 if (nfs4dev_reader != curthread)
317 nfs4dev_reader = curthread;
321 error = nfs4dev_request(data);
324 error = nfs4dev_reply(data);
327 NFS4DEV_DEBUG("nfs4dev_ioctl: unkown ioctl cmd %d\n", (int)cmd);
336 nfs4dev_poll(struct cdev *dev, int events, struct thread *td)
340 if (dev != nfs4device)
343 mtx_lock(&nfs4dev_daemon_mtx);
344 if (nfs4dev_nopen == 0) {
345 mtx_unlock(&nfs4dev_daemon_mtx);
348 mtx_unlock(&nfs4dev_daemon_mtx);
352 /* check readable data */
353 mtx_lock(&nfs4dev_newq_mtx);
354 if (!TAILQ_EMPTY(&nfs4dev_newq))
356 mtx_unlock(&nfs4dev_newq_mtx);
358 mtx_lock(&nfs4dev_waitq_mtx);
359 if (!TAILQ_EMPTY(&nfs4dev_waitq))
361 mtx_unlock(&nfs4dev_waitq_mtx);
367 nfs4dev_call(uint32_t type, caddr_t req_data, size_t req_len, caddr_t rep_data, size_t * rep_lenp)
369 struct nfs4dev_upcall * u;
373 mtx_lock(&nfs4dev_daemon_mtx);
374 if (nfs4dev_nopen == 0) {
375 mtx_unlock(&nfs4dev_daemon_mtx);
378 mtx_unlock(&nfs4dev_daemon_mtx);
380 if (type > NFS4DEV_MAX_TYPE)
383 NFS4DEV_DEBUG("upcall %d/%d:%d\n", type, req_len, *rep_lenp);
385 nfs4dev_upcall_get(u);
388 u->up_rep = rep_data;
389 u->up_replen = rep_lenp;
392 u->up_reqmsg.msg_vers = NFS4DEV_VERSION;
393 /* XXX efficient copying */
394 bcopy(req_data, u->up_reqmsg.msg_data, req_len);
395 u->up_reqmsg.msg_len = req_len;
397 mtx_lock(&nfs4dev_newq_mtx);
400 while ((xtmp = arc4random() % 256) == 0);
402 u->up_reqmsg.msg_xid = nfs4dev_xid;
404 TAILQ_INSERT_TAIL(&nfs4dev_newq, u, up_entry);
405 mtx_unlock(&nfs4dev_newq_mtx);
408 NFS4DEV_DEBUG("nfs4dev op: %d xid: %x sleeping\n", u->up_reqmsg.msg_type, u->up_reqmsg.msg_xid);
411 tsleep(u, PLOCK, "nfs4dev", 0);
412 } while (u->up_copied == 0 && u->up_error == 0);
414 /* upcall now removed from the queue */
416 NFS4DEV_DEBUG("nfs4dev prog: %d xid: %x continues...\n",
417 u->up_reqmsg.msg_type, u->up_reqmsg.msg_xid);
421 NFS4DEV_DEBUG("nfs4dev prog: %d xid: %x error: %d\n",
422 u->up_reqmsg.msg_type, u->up_reqmsg.msg_xid, u->up_error);
427 nfs4dev_upcall_put(u);
434 struct nfs4dev_upcall * u;
436 mtx_lock(&nfs4dev_newq_mtx);
437 while (!TAILQ_EMPTY(&nfs4dev_newq)) {
438 u = TAILQ_FIRST(&nfs4dev_newq);
439 TAILQ_REMOVE(&nfs4dev_newq, u, up_entry);
443 mtx_unlock(&nfs4dev_newq_mtx);
445 mtx_lock(&nfs4dev_waitq_mtx);
446 while (!TAILQ_EMPTY(&nfs4dev_waitq)) {
447 u = TAILQ_FIRST(&nfs4dev_waitq);
448 TAILQ_REMOVE(&nfs4dev_waitq, u, up_entry);
452 mtx_unlock(&nfs4dev_waitq_mtx);