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