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