nfs_syscalls.c revision 1.142 1 /* $NetBSD: nfs_syscalls.c,v 1.142 2008/11/19 18:36:09 ad Exp $ */
2
3 /*
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Rick Macklem at The University of Guelph.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * @(#)nfs_syscalls.c 8.5 (Berkeley) 3/30/95
35 */
36
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: nfs_syscalls.c,v 1.142 2008/11/19 18:36:09 ad Exp $");
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/file.h>
44 #include <sys/stat.h>
45 #include <sys/vnode.h>
46 #include <sys/mount.h>
47 #include <sys/proc.h>
48 #include <sys/uio.h>
49 #include <sys/malloc.h>
50 #include <sys/kmem.h>
51 #include <sys/buf.h>
52 #include <sys/mbuf.h>
53 #include <sys/socket.h>
54 #include <sys/socketvar.h>
55 #include <sys/signalvar.h>
56 #include <sys/domain.h>
57 #include <sys/protosw.h>
58 #include <sys/namei.h>
59 #include <sys/syslog.h>
60 #include <sys/filedesc.h>
61 #include <sys/kthread.h>
62 #include <sys/kauth.h>
63 #include <sys/syscallargs.h>
64
65 #include <netinet/in.h>
66 #include <netinet/tcp.h>
67 #include <nfs/xdr_subs.h>
68 #include <nfs/rpcv2.h>
69 #include <nfs/nfsproto.h>
70 #include <nfs/nfs.h>
71 #include <nfs/nfsm_subs.h>
72 #include <nfs/nfsrvcache.h>
73 #include <nfs/nfsmount.h>
74 #include <nfs/nfsnode.h>
75 #include <nfs/nfsrtt.h>
76 #include <nfs/nfs_var.h>
77
78 extern int32_t (*nfsrv3_procs[NFS_NPROCS]) __P((struct nfsrv_descript *,
79 struct nfssvc_sock *,
80 struct lwp *, struct mbuf **));
81 extern int nfsrvw_procrastinate;
82 extern int nuidhash_max;
83
84 static int nfs_numnfsd = 0;
85 static struct nfsdrt nfsdrt;
86 kmutex_t nfsd_lock;
87 struct nfssvc_sockhead nfssvc_sockhead;
88 kcondvar_t nfsd_initcv;
89 struct nfssvc_sockhead nfssvc_sockpending;
90 struct nfsdhead nfsd_head;
91 struct nfsdidlehead nfsd_idle_head;
92
93 int nfssvc_sockhead_flag;
94 int nfsd_head_flag;
95
96 struct nfssvc_sock *nfs_udpsock;
97 struct nfssvc_sock *nfs_udp6sock;
98
99 static struct nfssvc_sock *nfsrv_sockalloc __P((void));
100 static void nfsrv_sockfree __P((struct nfssvc_sock *));
101 static void nfsd_rt __P((int, struct nfsrv_descript *, int));
102
103 /*
104 * NFS server system calls
105 */
106
107
108 /*
109 * Nfs server pseudo system call for the nfsd's
110 * Based on the flag value it either:
111 * - adds a socket to the selection list
112 * - remains in the kernel as an nfsd
113 * - remains in the kernel as an nfsiod
114 */
115 int
116 sys_nfssvc(struct lwp *l, const struct sys_nfssvc_args *uap, register_t *retval)
117 {
118 /* {
119 syscallarg(int) flag;
120 syscallarg(void *) argp;
121 } */
122 int error;
123 file_t *fp;
124 struct mbuf *nam;
125 struct nfsd_args nfsdarg;
126 struct nfsd_srvargs nfsd_srvargs, *nsd = &nfsd_srvargs;
127 struct nfsd *nfsd;
128 struct nfssvc_sock *slp;
129 struct nfsuid *nuidp;
130
131 /*
132 * Must be super user
133 */
134 error = kauth_authorize_network(l->l_cred, KAUTH_NETWORK_NFS,
135 KAUTH_REQ_NETWORK_NFS_SVC, NULL, NULL, NULL);
136 if (error)
137 return (error);
138
139 mutex_enter(&nfsd_lock);
140 while (nfssvc_sockhead_flag & SLP_INIT) {
141 cv_wait(&nfsd_initcv, &nfsd_lock);
142 }
143 mutex_exit(&nfsd_lock);
144
145 if (SCARG(uap, flag) & NFSSVC_BIOD) {
146 /* Dummy implementation of nfsios for 1.4 and earlier. */
147 error = kpause("nfsbiod", true, 0, NULL);
148 } else if (SCARG(uap, flag) & NFSSVC_MNTD) {
149 error = ENOSYS;
150 } else if (SCARG(uap, flag) & NFSSVC_ADDSOCK) {
151 error = copyin(SCARG(uap, argp), (void *)&nfsdarg,
152 sizeof(nfsdarg));
153 if (error)
154 return (error);
155 /* getsock() will use the descriptor for us */
156 if ((fp = fd_getfile(nfsdarg.sock)) == NULL)
157 return (EBADF);
158 if (fp->f_type != DTYPE_SOCKET) {
159 fd_putfile(nfsdarg.sock);
160 return (ENOTSOCK);
161 }
162 if (error)
163 return (error);
164 /*
165 * Get the client address for connected sockets.
166 */
167 if (nfsdarg.name == NULL || nfsdarg.namelen == 0)
168 nam = (struct mbuf *)0;
169 else {
170 error = sockargs(&nam, nfsdarg.name, nfsdarg.namelen,
171 MT_SONAME);
172 if (error) {
173 fd_putfile(nfsdarg.sock);
174 return (error);
175 }
176 }
177 error = nfssvc_addsock(fp, nam);
178 fd_putfile(nfsdarg.sock);
179 } else if (SCARG(uap, flag) & NFSSVC_SETEXPORTSLIST) {
180 struct export_args *args;
181 struct mountd_exports_list mel;
182
183 error = copyin(SCARG(uap, argp), &mel, sizeof(mel));
184 if (error != 0)
185 return error;
186
187 args = (struct export_args *)malloc(mel.mel_nexports *
188 sizeof(struct export_args), M_TEMP, M_WAITOK);
189 error = copyin(mel.mel_exports, args, mel.mel_nexports *
190 sizeof(struct export_args));
191 if (error != 0) {
192 free(args, M_TEMP);
193 return error;
194 }
195 mel.mel_exports = args;
196
197 error = mountd_set_exports_list(&mel, l);
198
199 free(args, M_TEMP);
200 } else {
201 error = copyin(SCARG(uap, argp), (void *)nsd, sizeof (*nsd));
202 if (error)
203 return (error);
204 if ((SCARG(uap, flag) & NFSSVC_AUTHIN) &&
205 ((nfsd = nsd->nsd_nfsd)) != NULL &&
206 (nfsd->nfsd_slp->ns_flags & SLP_VALID)) {
207 slp = nfsd->nfsd_slp;
208
209 /*
210 * First check to see if another nfsd has already
211 * added this credential.
212 */
213 LIST_FOREACH(nuidp, NUIDHASH(slp, nsd->nsd_cr.cr_uid),
214 nu_hash) {
215 if (kauth_cred_geteuid(nuidp->nu_cr) ==
216 nsd->nsd_cr.cr_uid &&
217 (!nfsd->nfsd_nd->nd_nam2 ||
218 netaddr_match(NU_NETFAM(nuidp),
219 &nuidp->nu_haddr, nfsd->nfsd_nd->nd_nam2)))
220 break;
221 }
222 if (nuidp) {
223 kauth_cred_hold(nuidp->nu_cr);
224 nfsd->nfsd_nd->nd_cr = nuidp->nu_cr;
225 nfsd->nfsd_nd->nd_flag |= ND_KERBFULL;
226 } else {
227 /*
228 * Nope, so we will.
229 */
230 if (slp->ns_numuids < nuidhash_max) {
231 slp->ns_numuids++;
232 nuidp = kmem_alloc(sizeof(*nuidp), KM_SLEEP);
233 } else
234 nuidp = (struct nfsuid *)0;
235 if ((slp->ns_flags & SLP_VALID) == 0) {
236 if (nuidp)
237 kmem_free(nuidp, sizeof(*nuidp));
238 } else {
239 if (nuidp == (struct nfsuid *)0) {
240 nuidp = TAILQ_FIRST(&slp->ns_uidlruhead);
241 LIST_REMOVE(nuidp, nu_hash);
242 TAILQ_REMOVE(&slp->ns_uidlruhead, nuidp,
243 nu_lru);
244 if (nuidp->nu_flag & NU_NAM)
245 m_freem(nuidp->nu_nam);
246 }
247 nuidp->nu_flag = 0;
248 kauth_uucred_to_cred(nuidp->nu_cr,
249 &nsd->nsd_cr);
250 nuidp->nu_timestamp = nsd->nsd_timestamp;
251 nuidp->nu_expire = time_second + nsd->nsd_ttl;
252 /*
253 * and save the session key in nu_key.
254 */
255 memcpy(nuidp->nu_key, nsd->nsd_key,
256 sizeof(nsd->nsd_key));
257 if (nfsd->nfsd_nd->nd_nam2) {
258 struct sockaddr_in *saddr;
259
260 saddr = mtod(nfsd->nfsd_nd->nd_nam2,
261 struct sockaddr_in *);
262 switch (saddr->sin_family) {
263 case AF_INET:
264 nuidp->nu_flag |= NU_INETADDR;
265 nuidp->nu_inetaddr =
266 saddr->sin_addr.s_addr;
267 break;
268 case AF_INET6:
269 nuidp->nu_flag |= NU_NAM;
270 nuidp->nu_nam = m_copym(
271 nfsd->nfsd_nd->nd_nam2, 0,
272 M_COPYALL, M_WAIT);
273 break;
274 default:
275 return EAFNOSUPPORT;
276 };
277 }
278 TAILQ_INSERT_TAIL(&slp->ns_uidlruhead, nuidp,
279 nu_lru);
280 LIST_INSERT_HEAD(NUIDHASH(slp, nsd->nsd_uid),
281 nuidp, nu_hash);
282 kauth_cred_hold(nuidp->nu_cr);
283 nfsd->nfsd_nd->nd_cr = nuidp->nu_cr;
284 nfsd->nfsd_nd->nd_flag |= ND_KERBFULL;
285 }
286 }
287 }
288 if ((SCARG(uap, flag) & NFSSVC_AUTHINFAIL) &&
289 (nfsd = nsd->nsd_nfsd))
290 nfsd->nfsd_flag |= NFSD_AUTHFAIL;
291 error = nfssvc_nfsd(nsd, SCARG(uap, argp), l);
292 }
293 if (error == EINTR || error == ERESTART)
294 error = 0;
295 return (error);
296 }
297
298 MALLOC_DEFINE(M_NFSD, "NFS daemon", "Nfs server daemon structure");
299
300 static struct nfssvc_sock *
301 nfsrv_sockalloc()
302 {
303 struct nfssvc_sock *slp;
304
305 slp = kmem_alloc(sizeof(*slp), KM_SLEEP);
306 memset(slp, 0, sizeof (struct nfssvc_sock));
307 mutex_init(&slp->ns_lock, MUTEX_DRIVER, IPL_SOFTNET);
308 mutex_init(&slp->ns_alock, MUTEX_DRIVER, IPL_SOFTNET);
309 cv_init(&slp->ns_cv, "nfsdsock");
310 TAILQ_INIT(&slp->ns_uidlruhead);
311 LIST_INIT(&slp->ns_tq);
312 SIMPLEQ_INIT(&slp->ns_sendq);
313 mutex_enter(&nfsd_lock);
314 TAILQ_INSERT_TAIL(&nfssvc_sockhead, slp, ns_chain);
315 mutex_exit(&nfsd_lock);
316
317 return slp;
318 }
319
320 static void
321 nfsrv_sockfree(struct nfssvc_sock *slp)
322 {
323
324 KASSERT(slp->ns_so == NULL);
325 KASSERT(slp->ns_fp == NULL);
326 KASSERT((slp->ns_flags & SLP_VALID) == 0);
327 mutex_destroy(&slp->ns_lock);
328 mutex_destroy(&slp->ns_alock);
329 cv_destroy(&slp->ns_cv);
330 kmem_free(slp, sizeof(*slp));
331 }
332
333 /*
334 * Adds a socket to the list for servicing by nfsds.
335 */
336 int
337 nfssvc_addsock(fp, mynam)
338 file_t *fp;
339 struct mbuf *mynam;
340 {
341 int siz;
342 struct nfssvc_sock *slp;
343 struct socket *so;
344 struct nfssvc_sock *tslp;
345 int error;
346 int val;
347
348 so = (struct socket *)fp->f_data;
349 tslp = (struct nfssvc_sock *)0;
350 /*
351 * Add it to the list, as required.
352 */
353 if (so->so_proto->pr_protocol == IPPROTO_UDP) {
354 if (so->so_proto->pr_domain->dom_family == AF_INET6)
355 tslp = nfs_udp6sock;
356 else {
357 tslp = nfs_udpsock;
358 if (tslp->ns_flags & SLP_VALID) {
359 m_freem(mynam);
360 return (EPERM);
361 }
362 }
363 }
364 if (so->so_type == SOCK_STREAM)
365 siz = NFS_MAXPACKET + sizeof (u_long);
366 else
367 siz = NFS_MAXPACKET;
368 solock(so);
369 error = soreserve(so, siz, siz);
370 sounlock(so);
371 if (error) {
372 m_freem(mynam);
373 return (error);
374 }
375
376 /*
377 * Set protocol specific options { for now TCP only } and
378 * reserve some space. For datagram sockets, this can get called
379 * repeatedly for the same socket, but that isn't harmful.
380 */
381 if (so->so_type == SOCK_STREAM) {
382 val = 1;
383 so_setsockopt(NULL, so, SOL_SOCKET, SO_KEEPALIVE, &val,
384 sizeof(val));
385 }
386 if ((so->so_proto->pr_domain->dom_family == AF_INET ||
387 so->so_proto->pr_domain->dom_family == AF_INET6) &&
388 so->so_proto->pr_protocol == IPPROTO_TCP) {
389 val = 1;
390 so_setsockopt(NULL, so, IPPROTO_TCP, TCP_NODELAY, &val,
391 sizeof(val));
392 }
393 solock(so);
394 so->so_rcv.sb_flags &= ~SB_NOINTR;
395 so->so_rcv.sb_timeo = 0;
396 so->so_snd.sb_flags &= ~SB_NOINTR;
397 so->so_snd.sb_timeo = 0;
398 sounlock(so);
399 if (tslp) {
400 slp = tslp;
401 } else {
402 slp = nfsrv_sockalloc();
403 }
404 slp->ns_so = so;
405 slp->ns_nam = mynam;
406 mutex_enter(&fp->f_lock);
407 fp->f_count++;
408 mutex_exit(&fp->f_lock);
409 slp->ns_fp = fp;
410 slp->ns_flags = SLP_VALID;
411 slp->ns_aflags = SLP_A_NEEDQ;
412 slp->ns_gflags = 0;
413 slp->ns_sflags = 0;
414 solock(so);
415 so->so_upcallarg = (void *)slp;
416 so->so_upcall = nfsrv_soupcall;
417 so->so_rcv.sb_flags |= SB_UPCALL;
418 sounlock(so);
419 nfsrv_wakenfsd(slp);
420 return (0);
421 }
422
423 /*
424 * Called by nfssvc() for nfsds. Just loops around servicing rpc requests
425 * until it is killed by a signal.
426 */
427 int
428 nfssvc_nfsd(nsd, argp, l)
429 struct nfsd_srvargs *nsd;
430 void *argp;
431 struct lwp *l;
432 {
433 struct timeval tv;
434 struct mbuf *m;
435 struct nfssvc_sock *slp;
436 struct nfsd *nfsd = nsd->nsd_nfsd;
437 struct nfsrv_descript *nd = NULL;
438 struct mbuf *mreq;
439 u_quad_t cur_usec;
440 int error = 0, cacherep, siz, sotype, writes_todo;
441 struct proc *p = l->l_proc;
442 int s;
443 bool doreinit;
444
445 #ifndef nolint
446 cacherep = RC_DOIT;
447 writes_todo = 0;
448 #endif
449 uvm_lwp_hold(l);
450 if (nfsd == NULL) {
451 nsd->nsd_nfsd = nfsd = kmem_alloc(sizeof(*nfsd), KM_SLEEP);
452 memset(nfsd, 0, sizeof (struct nfsd));
453 cv_init(&nfsd->nfsd_cv, "nfsd");
454 nfsd->nfsd_procp = p;
455 mutex_enter(&nfsd_lock);
456 while ((nfssvc_sockhead_flag & SLP_INIT) != 0) {
457 KASSERT(nfs_numnfsd == 0);
458 cv_wait(&nfsd_initcv, &nfsd_lock);
459 }
460 TAILQ_INSERT_TAIL(&nfsd_head, nfsd, nfsd_chain);
461 nfs_numnfsd++;
462 mutex_exit(&nfsd_lock);
463 }
464 /*
465 * Loop getting rpc requests until SIGKILL.
466 */
467 for (;;) {
468 bool dummy;
469
470 if ((curcpu()->ci_schedstate.spc_flags & SPCF_SHOULDYIELD)
471 != 0) {
472 preempt();
473 }
474 if (nfsd->nfsd_slp == NULL) {
475 mutex_enter(&nfsd_lock);
476 while (nfsd->nfsd_slp == NULL &&
477 (nfsd_head_flag & NFSD_CHECKSLP) == 0) {
478 SLIST_INSERT_HEAD(&nfsd_idle_head, nfsd,
479 nfsd_idle);
480 error = cv_wait_sig(&nfsd->nfsd_cv, &nfsd_lock);
481 if (error) {
482 slp = nfsd->nfsd_slp;
483 nfsd->nfsd_slp = NULL;
484 if (!slp)
485 SLIST_REMOVE(&nfsd_idle_head,
486 nfsd, nfsd, nfsd_idle);
487 mutex_exit(&nfsd_lock);
488 if (slp) {
489 nfsrv_wakenfsd(slp);
490 nfsrv_slpderef(slp);
491 }
492 goto done;
493 }
494 }
495 if (nfsd->nfsd_slp == NULL &&
496 (nfsd_head_flag & NFSD_CHECKSLP) != 0) {
497 slp = TAILQ_FIRST(&nfssvc_sockpending);
498 if (slp) {
499 KASSERT((slp->ns_gflags & SLP_G_DOREC)
500 != 0);
501 TAILQ_REMOVE(&nfssvc_sockpending, slp,
502 ns_pending);
503 slp->ns_gflags &= ~SLP_G_DOREC;
504 slp->ns_sref++;
505 nfsd->nfsd_slp = slp;
506 } else
507 nfsd_head_flag &= ~NFSD_CHECKSLP;
508 }
509 KASSERT(nfsd->nfsd_slp == NULL ||
510 nfsd->nfsd_slp->ns_sref > 0);
511 mutex_exit(&nfsd_lock);
512 if ((slp = nfsd->nfsd_slp) == NULL)
513 continue;
514 if (slp->ns_flags & SLP_VALID) {
515 bool more;
516
517 if (nfsdsock_testbits(slp, SLP_A_NEEDQ)) {
518 nfsrv_rcv(slp);
519 }
520 if (nfsdsock_testbits(slp, SLP_A_DISCONN)) {
521 nfsrv_zapsock(slp);
522 }
523 error = nfsrv_dorec(slp, nfsd, &nd, &more);
524 getmicrotime(&tv);
525 cur_usec = (u_quad_t)tv.tv_sec * 1000000 +
526 (u_quad_t)tv.tv_usec;
527 writes_todo = 0;
528 if (error) {
529 struct nfsrv_descript *nd2;
530
531 mutex_enter(&nfsd_lock);
532 nd2 = LIST_FIRST(&slp->ns_tq);
533 if (nd2 != NULL &&
534 nd2->nd_time <= cur_usec) {
535 error = 0;
536 cacherep = RC_DOIT;
537 writes_todo = 1;
538 }
539 mutex_exit(&nfsd_lock);
540 }
541 if (error == 0 && more) {
542 nfsrv_wakenfsd(slp);
543 }
544 }
545 } else {
546 error = 0;
547 slp = nfsd->nfsd_slp;
548 }
549 KASSERT(slp != NULL);
550 KASSERT(nfsd->nfsd_slp == slp);
551 if (error || (slp->ns_flags & SLP_VALID) == 0) {
552 if (nd) {
553 nfsdreq_free(nd);
554 nd = NULL;
555 }
556 nfsd->nfsd_slp = NULL;
557 nfsrv_slpderef(slp);
558 continue;
559 }
560 sotype = slp->ns_so->so_type;
561 if (nd) {
562 getmicrotime(&nd->nd_starttime);
563 if (nd->nd_nam2)
564 nd->nd_nam = nd->nd_nam2;
565 else
566 nd->nd_nam = slp->ns_nam;
567
568 /*
569 * Check to see if authorization is needed.
570 */
571 if (nfsd->nfsd_flag & NFSD_NEEDAUTH) {
572 nfsd->nfsd_flag &= ~NFSD_NEEDAUTH;
573 nsd->nsd_haddr = mtod(nd->nd_nam,
574 struct sockaddr_in *)->sin_addr.s_addr;
575 nsd->nsd_authlen = nfsd->nfsd_authlen;
576 nsd->nsd_verflen = nfsd->nfsd_verflen;
577 if (!copyout(nfsd->nfsd_authstr,
578 nsd->nsd_authstr, nfsd->nfsd_authlen) &&
579 !copyout(nfsd->nfsd_verfstr,
580 nsd->nsd_verfstr, nfsd->nfsd_verflen) &&
581 !copyout(nsd, argp, sizeof (*nsd))) {
582 uvm_lwp_rele(l);
583 return (ENEEDAUTH);
584 }
585 cacherep = RC_DROPIT;
586 } else
587 cacherep = nfsrv_getcache(nd, slp, &mreq);
588
589 if (nfsd->nfsd_flag & NFSD_AUTHFAIL) {
590 nfsd->nfsd_flag &= ~NFSD_AUTHFAIL;
591 nd->nd_procnum = NFSPROC_NOOP;
592 nd->nd_repstat =
593 (NFSERR_AUTHERR | AUTH_TOOWEAK);
594 cacherep = RC_DOIT;
595 }
596 }
597
598 /*
599 * Loop to get all the write rpc relies that have been
600 * gathered together.
601 */
602 do {
603 switch (cacherep) {
604 case RC_DOIT:
605 mreq = NULL;
606 netexport_rdlock();
607 if (writes_todo || nd == NULL ||
608 (!(nd->nd_flag & ND_NFSV3) &&
609 nd->nd_procnum == NFSPROC_WRITE &&
610 nfsrvw_procrastinate > 0))
611 error = nfsrv_writegather(&nd, slp,
612 l, &mreq);
613 else
614 error =
615 (*(nfsrv3_procs[nd->nd_procnum]))
616 (nd, slp, l, &mreq);
617 netexport_rdunlock();
618 if (mreq == NULL) {
619 if (nd != NULL) {
620 if (nd->nd_nam2)
621 m_free(nd->nd_nam2);
622 }
623 break;
624 }
625 if (error) {
626 nfsstats.srv_errs++;
627 nfsrv_updatecache(nd, false, mreq);
628 if (nd->nd_nam2)
629 m_freem(nd->nd_nam2);
630 break;
631 }
632 nfsstats.srvrpccnt[nd->nd_procnum]++;
633 nfsrv_updatecache(nd, true, mreq);
634 nd->nd_mrep = (struct mbuf *)0;
635 case RC_REPLY:
636 m = mreq;
637 siz = 0;
638 while (m) {
639 siz += m->m_len;
640 m = m->m_next;
641 }
642 if (siz <= 0 || siz > NFS_MAXPACKET) {
643 printf("mbuf siz=%d\n",siz);
644 panic("Bad nfs svc reply");
645 }
646 m = mreq;
647 m->m_pkthdr.len = siz;
648 m->m_pkthdr.rcvif = (struct ifnet *)0;
649 /*
650 * For stream protocols, prepend a Sun RPC
651 * Record Mark.
652 */
653 if (sotype == SOCK_STREAM) {
654 M_PREPEND(m, NFSX_UNSIGNED, M_WAIT);
655 *mtod(m, u_int32_t *) =
656 htonl(0x80000000 | siz);
657 }
658 nd->nd_mreq = m;
659 if (nfsrtton) {
660 nfsd_rt(slp->ns_so->so_type, nd,
661 cacherep);
662 }
663 error = nfsdsock_sendreply(slp, nd);
664 nd = NULL;
665 if (error == EPIPE)
666 nfsrv_zapsock(slp);
667 if (error == EINTR || error == ERESTART) {
668 nfsd->nfsd_slp = NULL;
669 nfsrv_slpderef(slp);
670 goto done;
671 }
672 break;
673 case RC_DROPIT:
674 if (nfsrtton)
675 nfsd_rt(sotype, nd, cacherep);
676 m_freem(nd->nd_mrep);
677 m_freem(nd->nd_nam2);
678 break;
679 }
680 if (nd) {
681 nfsdreq_free(nd);
682 nd = NULL;
683 }
684
685 /*
686 * Check to see if there are outstanding writes that
687 * need to be serviced.
688 */
689 getmicrotime(&tv);
690 cur_usec = (u_quad_t)tv.tv_sec * 1000000 +
691 (u_quad_t)tv.tv_usec;
692 s = splsoftclock();
693 if (LIST_FIRST(&slp->ns_tq) &&
694 LIST_FIRST(&slp->ns_tq)->nd_time <= cur_usec) {
695 cacherep = RC_DOIT;
696 writes_todo = 1;
697 } else
698 writes_todo = 0;
699 splx(s);
700 } while (writes_todo);
701 if (nfsrv_dorec(slp, nfsd, &nd, &dummy)) {
702 nfsd->nfsd_slp = NULL;
703 nfsrv_slpderef(slp);
704 }
705 }
706 done:
707 mutex_enter(&nfsd_lock);
708 TAILQ_REMOVE(&nfsd_head, nfsd, nfsd_chain);
709 doreinit = --nfs_numnfsd == 0;
710 if (doreinit)
711 nfssvc_sockhead_flag |= SLP_INIT;
712 mutex_exit(&nfsd_lock);
713 cv_destroy(&nfsd->nfsd_cv);
714 kmem_free(nfsd, sizeof(*nfsd));
715 nsd->nsd_nfsd = NULL;
716 if (doreinit)
717 nfsrv_init(true); /* Reinitialize everything */
718 uvm_lwp_rele(l);
719 return (error);
720 }
721
722 /*
723 * Shut down a socket associated with an nfssvc_sock structure.
724 * Should be called with the send lock set, if required.
725 * The trick here is to increment the sref at the start, so that the nfsds
726 * will stop using it and clear ns_flag at the end so that it will not be
727 * reassigned during cleanup.
728 *
729 * called at splsoftnet.
730 */
731 void
732 nfsrv_zapsock(slp)
733 struct nfssvc_sock *slp;
734 {
735 struct nfsuid *nuidp, *nnuidp;
736 struct nfsrv_descript *nwp;
737 struct socket *so;
738 struct mbuf *m;
739
740 if (nfsdsock_drain(slp)) {
741 return;
742 }
743 mutex_enter(&nfsd_lock);
744 if (slp->ns_gflags & SLP_G_DOREC) {
745 TAILQ_REMOVE(&nfssvc_sockpending, slp, ns_pending);
746 slp->ns_gflags &= ~SLP_G_DOREC;
747 }
748 mutex_exit(&nfsd_lock);
749
750 so = slp->ns_so;
751 KASSERT(so != NULL);
752 solock(so);
753 so->so_upcall = NULL;
754 so->so_upcallarg = NULL;
755 so->so_rcv.sb_flags &= ~SB_UPCALL;
756 soshutdown(so, SHUT_RDWR);
757 sounlock(so);
758
759 if (slp->ns_nam)
760 m_free(slp->ns_nam);
761 m_freem(slp->ns_raw);
762 m = slp->ns_rec;
763 while (m != NULL) {
764 struct mbuf *n;
765
766 n = m->m_nextpkt;
767 m_freem(m);
768 m = n;
769 }
770 for (nuidp = TAILQ_FIRST(&slp->ns_uidlruhead); nuidp != 0;
771 nuidp = nnuidp) {
772 nnuidp = TAILQ_NEXT(nuidp, nu_lru);
773 LIST_REMOVE(nuidp, nu_hash);
774 TAILQ_REMOVE(&slp->ns_uidlruhead, nuidp, nu_lru);
775 if (nuidp->nu_flag & NU_NAM)
776 m_freem(nuidp->nu_nam);
777 kmem_free(nuidp, sizeof(*nuidp));
778 }
779 mutex_enter(&nfsd_lock);
780 while ((nwp = LIST_FIRST(&slp->ns_tq)) != NULL) {
781 LIST_REMOVE(nwp, nd_tq);
782 mutex_exit(&nfsd_lock);
783 nfsdreq_free(nwp);
784 mutex_enter(&nfsd_lock);
785 }
786 mutex_exit(&nfsd_lock);
787 }
788
789 /*
790 * Derefence a server socket structure. If it has no more references and
791 * is no longer valid, you can throw it away.
792 */
793 void
794 nfsrv_slpderef(slp)
795 struct nfssvc_sock *slp;
796 {
797 uint32_t ref;
798
799 mutex_enter(&nfsd_lock);
800 KASSERT(slp->ns_sref > 0);
801 ref = --slp->ns_sref;
802 mutex_exit(&nfsd_lock);
803 if (ref == 0 && (slp->ns_flags & SLP_VALID) == 0) {
804 file_t *fp;
805
806 mutex_enter(&nfsd_lock);
807 KASSERT((slp->ns_gflags & SLP_G_DOREC) == 0);
808 TAILQ_REMOVE(&nfssvc_sockhead, slp, ns_chain);
809 mutex_exit(&nfsd_lock);
810
811 fp = slp->ns_fp;
812 if (fp != NULL) {
813 slp->ns_fp = NULL;
814 KASSERT(fp != NULL);
815 KASSERT(fp->f_data == slp->ns_so);
816 KASSERT(fp->f_count > 0);
817 closef(fp);
818 slp->ns_so = NULL;
819 }
820
821 nfsrv_sockfree(slp);
822 }
823 }
824
825 /*
826 * Initialize the data structures for the server.
827 * Handshake with any new nfsds starting up to avoid any chance of
828 * corruption.
829 */
830 void
831 nfsrv_init(terminating)
832 int terminating;
833 {
834 struct nfssvc_sock *slp;
835
836 if (!terminating) {
837 mutex_init(&nfsd_lock, MUTEX_DRIVER, IPL_SOFTNET);
838 cv_init(&nfsd_initcv, "nfsdinit");
839 }
840
841 mutex_enter(&nfsd_lock);
842 if (!terminating && (nfssvc_sockhead_flag & SLP_INIT) != 0)
843 panic("nfsd init");
844 nfssvc_sockhead_flag |= SLP_INIT;
845
846 if (terminating) {
847 KASSERT(SLIST_EMPTY(&nfsd_idle_head));
848 KASSERT(TAILQ_EMPTY(&nfsd_head));
849 while ((slp = TAILQ_FIRST(&nfssvc_sockhead)) != NULL) {
850 mutex_exit(&nfsd_lock);
851 KASSERT(slp->ns_sref == 0);
852 slp->ns_sref++;
853 nfsrv_zapsock(slp);
854 nfsrv_slpderef(slp);
855 mutex_enter(&nfsd_lock);
856 }
857 KASSERT(TAILQ_EMPTY(&nfssvc_sockpending));
858 mutex_exit(&nfsd_lock);
859 nfsrv_cleancache(); /* And clear out server cache */
860 } else {
861 mutex_exit(&nfsd_lock);
862 nfs_pub.np_valid = 0;
863 }
864
865 TAILQ_INIT(&nfssvc_sockhead);
866 TAILQ_INIT(&nfssvc_sockpending);
867
868 TAILQ_INIT(&nfsd_head);
869 SLIST_INIT(&nfsd_idle_head);
870 nfsd_head_flag &= ~NFSD_CHECKSLP;
871
872 nfs_udpsock = nfsrv_sockalloc();
873 nfs_udp6sock = nfsrv_sockalloc();
874
875 mutex_enter(&nfsd_lock);
876 nfssvc_sockhead_flag &= ~SLP_INIT;
877 cv_broadcast(&nfsd_initcv);
878 mutex_exit(&nfsd_lock);
879 }
880
881 void
882 nfsrv_fini(void)
883 {
884
885 nfsrv_init(true);
886 cv_destroy(&nfsd_initcv);
887 mutex_destroy(&nfsd_lock);
888 }
889
890 /*
891 * Add entries to the server monitor log.
892 */
893 static void
894 nfsd_rt(sotype, nd, cacherep)
895 int sotype;
896 struct nfsrv_descript *nd;
897 int cacherep;
898 {
899 struct timeval tv;
900 struct drt *rt;
901
902 rt = &nfsdrt.drt[nfsdrt.pos];
903 if (cacherep == RC_DOIT)
904 rt->flag = 0;
905 else if (cacherep == RC_REPLY)
906 rt->flag = DRT_CACHEREPLY;
907 else
908 rt->flag = DRT_CACHEDROP;
909 if (sotype == SOCK_STREAM)
910 rt->flag |= DRT_TCP;
911 if (nd->nd_flag & ND_NFSV3)
912 rt->flag |= DRT_NFSV3;
913 rt->proc = nd->nd_procnum;
914 if (mtod(nd->nd_nam, struct sockaddr *)->sa_family == AF_INET)
915 rt->ipadr = mtod(nd->nd_nam, struct sockaddr_in *)->sin_addr.s_addr;
916 else
917 rt->ipadr = INADDR_ANY;
918 getmicrotime(&tv);
919 rt->resptime = ((tv.tv_sec - nd->nd_starttime.tv_sec) * 1000000) +
920 (tv.tv_usec - nd->nd_starttime.tv_usec);
921 rt->tstamp = tv;
922 nfsdrt.pos = (nfsdrt.pos + 1) % NFSRTTLOGSIZ;
923 }
924