1 /* $NetBSD: nfs_syscalls.c,v 1.146 2009/03/15 17:20:10 cegger 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.146 2009/03/15 17:20:10 cegger 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 /* 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 static struct nfssvc_sock * 299 nfsrv_sockalloc(void) 300 { 301 struct nfssvc_sock *slp; 302 303 slp = kmem_alloc(sizeof(*slp), KM_SLEEP); 304 memset(slp, 0, sizeof (struct nfssvc_sock)); 305 mutex_init(&slp->ns_lock, MUTEX_DRIVER, IPL_SOFTNET); 306 mutex_init(&slp->ns_alock, MUTEX_DRIVER, IPL_SOFTNET); 307 cv_init(&slp->ns_cv, "nfsdsock"); 308 TAILQ_INIT(&slp->ns_uidlruhead); 309 LIST_INIT(&slp->ns_tq); 310 SIMPLEQ_INIT(&slp->ns_sendq); 311 mutex_enter(&nfsd_lock); 312 TAILQ_INSERT_TAIL(&nfssvc_sockhead, slp, ns_chain); 313 mutex_exit(&nfsd_lock); 314 315 return slp; 316 } 317 318 static void 319 nfsrv_sockfree(struct nfssvc_sock *slp) 320 { 321 322 KASSERT(slp->ns_so == NULL); 323 KASSERT(slp->ns_fp == NULL); 324 KASSERT((slp->ns_flags & SLP_VALID) == 0); 325 mutex_destroy(&slp->ns_lock); 326 mutex_destroy(&slp->ns_alock); 327 cv_destroy(&slp->ns_cv); 328 kmem_free(slp, sizeof(*slp)); 329 } 330 331 /* 332 * Adds a socket to the list for servicing by nfsds. 333 */ 334 int 335 nfssvc_addsock(file_t *fp, struct mbuf *mynam) 336 { 337 int siz; 338 struct nfssvc_sock *slp; 339 struct socket *so; 340 struct nfssvc_sock *tslp; 341 int error; 342 int val; 343 344 so = (struct socket *)fp->f_data; 345 tslp = (struct nfssvc_sock *)0; 346 /* 347 * Add it to the list, as required. 348 */ 349 if (so->so_proto->pr_protocol == IPPROTO_UDP) { 350 if (so->so_proto->pr_domain->dom_family == AF_INET6) 351 tslp = nfs_udp6sock; 352 else { 353 tslp = nfs_udpsock; 354 if (tslp->ns_flags & SLP_VALID) { 355 m_freem(mynam); 356 return (EPERM); 357 } 358 } 359 } 360 if (so->so_type == SOCK_STREAM) 361 siz = NFS_MAXPACKET + sizeof (u_long); 362 else 363 siz = NFS_MAXPACKET; 364 solock(so); 365 error = soreserve(so, siz, siz); 366 sounlock(so); 367 if (error) { 368 m_freem(mynam); 369 return (error); 370 } 371 372 /* 373 * Set protocol specific options { for now TCP only } and 374 * reserve some space. For datagram sockets, this can get called 375 * repeatedly for the same socket, but that isn't harmful. 376 */ 377 if (so->so_type == SOCK_STREAM) { 378 val = 1; 379 so_setsockopt(NULL, so, SOL_SOCKET, SO_KEEPALIVE, &val, 380 sizeof(val)); 381 } 382 if ((so->so_proto->pr_domain->dom_family == AF_INET || 383 so->so_proto->pr_domain->dom_family == AF_INET6) && 384 so->so_proto->pr_protocol == IPPROTO_TCP) { 385 val = 1; 386 so_setsockopt(NULL, so, IPPROTO_TCP, TCP_NODELAY, &val, 387 sizeof(val)); 388 } 389 solock(so); 390 so->so_rcv.sb_flags &= ~SB_NOINTR; 391 so->so_rcv.sb_timeo = 0; 392 so->so_snd.sb_flags &= ~SB_NOINTR; 393 so->so_snd.sb_timeo = 0; 394 sounlock(so); 395 if (tslp) { 396 slp = tslp; 397 } else { 398 slp = nfsrv_sockalloc(); 399 } 400 slp->ns_so = so; 401 slp->ns_nam = mynam; 402 mutex_enter(&fp->f_lock); 403 fp->f_count++; 404 mutex_exit(&fp->f_lock); 405 slp->ns_fp = fp; 406 slp->ns_flags = SLP_VALID; 407 slp->ns_aflags = SLP_A_NEEDQ; 408 slp->ns_gflags = 0; 409 slp->ns_sflags = 0; 410 solock(so); 411 so->so_upcallarg = (void *)slp; 412 so->so_upcall = nfsrv_soupcall; 413 so->so_rcv.sb_flags |= SB_UPCALL; 414 sounlock(so); 415 nfsrv_wakenfsd(slp); 416 return (0); 417 } 418 419 /* 420 * Called by nfssvc() for nfsds. Just loops around servicing rpc requests 421 * until it is killed by a signal. 422 */ 423 int 424 nfssvc_nfsd(struct nfsd_srvargs *nsd, void *argp, struct lwp *l) 425 { 426 struct timeval tv; 427 struct mbuf *m; 428 struct nfssvc_sock *slp; 429 struct nfsd *nfsd = nsd->nsd_nfsd; 430 struct nfsrv_descript *nd = NULL; 431 struct mbuf *mreq; 432 u_quad_t cur_usec; 433 int error = 0, cacherep, siz, sotype, writes_todo; 434 struct proc *p = l->l_proc; 435 int s; 436 bool doreinit; 437 438 #ifndef nolint 439 cacherep = RC_DOIT; 440 writes_todo = 0; 441 #endif 442 uvm_lwp_hold(l); 443 if (nfsd == NULL) { 444 nsd->nsd_nfsd = nfsd = kmem_alloc(sizeof(*nfsd), KM_SLEEP); 445 memset(nfsd, 0, sizeof (struct nfsd)); 446 cv_init(&nfsd->nfsd_cv, "nfsd"); 447 nfsd->nfsd_procp = p; 448 mutex_enter(&nfsd_lock); 449 while ((nfssvc_sockhead_flag & SLP_INIT) != 0) { 450 KASSERT(nfs_numnfsd == 0); 451 cv_wait(&nfsd_initcv, &nfsd_lock); 452 } 453 TAILQ_INSERT_TAIL(&nfsd_head, nfsd, nfsd_chain); 454 nfs_numnfsd++; 455 mutex_exit(&nfsd_lock); 456 } 457 /* 458 * Loop getting rpc requests until SIGKILL. 459 */ 460 for (;;) { 461 bool dummy; 462 463 if ((curcpu()->ci_schedstate.spc_flags & SPCF_SHOULDYIELD) 464 != 0) { 465 preempt(); 466 } 467 if (nfsd->nfsd_slp == NULL) { 468 mutex_enter(&nfsd_lock); 469 while (nfsd->nfsd_slp == NULL && 470 (nfsd_head_flag & NFSD_CHECKSLP) == 0) { 471 SLIST_INSERT_HEAD(&nfsd_idle_head, nfsd, 472 nfsd_idle); 473 error = cv_wait_sig(&nfsd->nfsd_cv, &nfsd_lock); 474 if (error) { 475 slp = nfsd->nfsd_slp; 476 nfsd->nfsd_slp = NULL; 477 if (!slp) 478 SLIST_REMOVE(&nfsd_idle_head, 479 nfsd, nfsd, nfsd_idle); 480 mutex_exit(&nfsd_lock); 481 if (slp) { 482 nfsrv_wakenfsd(slp); 483 nfsrv_slpderef(slp); 484 } 485 goto done; 486 } 487 } 488 if (nfsd->nfsd_slp == NULL && 489 (nfsd_head_flag & NFSD_CHECKSLP) != 0) { 490 slp = TAILQ_FIRST(&nfssvc_sockpending); 491 if (slp) { 492 KASSERT((slp->ns_gflags & SLP_G_DOREC) 493 != 0); 494 TAILQ_REMOVE(&nfssvc_sockpending, slp, 495 ns_pending); 496 slp->ns_gflags &= ~SLP_G_DOREC; 497 slp->ns_sref++; 498 nfsd->nfsd_slp = slp; 499 } else 500 nfsd_head_flag &= ~NFSD_CHECKSLP; 501 } 502 KASSERT(nfsd->nfsd_slp == NULL || 503 nfsd->nfsd_slp->ns_sref > 0); 504 mutex_exit(&nfsd_lock); 505 if ((slp = nfsd->nfsd_slp) == NULL) 506 continue; 507 if (slp->ns_flags & SLP_VALID) { 508 bool more; 509 510 if (nfsdsock_testbits(slp, SLP_A_NEEDQ)) { 511 nfsrv_rcv(slp); 512 } 513 if (nfsdsock_testbits(slp, SLP_A_DISCONN)) { 514 nfsrv_zapsock(slp); 515 } 516 error = nfsrv_dorec(slp, nfsd, &nd, &more); 517 getmicrotime(&tv); 518 cur_usec = (u_quad_t)tv.tv_sec * 1000000 + 519 (u_quad_t)tv.tv_usec; 520 writes_todo = 0; 521 if (error) { 522 struct nfsrv_descript *nd2; 523 524 mutex_enter(&nfsd_lock); 525 nd2 = LIST_FIRST(&slp->ns_tq); 526 if (nd2 != NULL && 527 nd2->nd_time <= cur_usec) { 528 error = 0; 529 cacherep = RC_DOIT; 530 writes_todo = 1; 531 } 532 mutex_exit(&nfsd_lock); 533 } 534 if (error == 0 && more) { 535 nfsrv_wakenfsd(slp); 536 } 537 } 538 } else { 539 error = 0; 540 slp = nfsd->nfsd_slp; 541 } 542 KASSERT(slp != NULL); 543 KASSERT(nfsd->nfsd_slp == slp); 544 if (error || (slp->ns_flags & SLP_VALID) == 0) { 545 if (nd) { 546 nfsdreq_free(nd); 547 nd = NULL; 548 } 549 nfsd->nfsd_slp = NULL; 550 nfsrv_slpderef(slp); 551 continue; 552 } 553 sotype = slp->ns_so->so_type; 554 if (nd) { 555 getmicrotime(&nd->nd_starttime); 556 if (nd->nd_nam2) 557 nd->nd_nam = nd->nd_nam2; 558 else 559 nd->nd_nam = slp->ns_nam; 560 561 /* 562 * Check to see if authorization is needed. 563 */ 564 if (nfsd->nfsd_flag & NFSD_NEEDAUTH) { 565 nfsd->nfsd_flag &= ~NFSD_NEEDAUTH; 566 nsd->nsd_haddr = mtod(nd->nd_nam, 567 struct sockaddr_in *)->sin_addr.s_addr; 568 nsd->nsd_authlen = nfsd->nfsd_authlen; 569 nsd->nsd_verflen = nfsd->nfsd_verflen; 570 if (!copyout(nfsd->nfsd_authstr, 571 nsd->nsd_authstr, nfsd->nfsd_authlen) && 572 !copyout(nfsd->nfsd_verfstr, 573 nsd->nsd_verfstr, nfsd->nfsd_verflen) && 574 !copyout(nsd, argp, sizeof (*nsd))) { 575 uvm_lwp_rele(l); 576 return (ENEEDAUTH); 577 } 578 cacherep = RC_DROPIT; 579 } else 580 cacherep = nfsrv_getcache(nd, slp, &mreq); 581 582 if (nfsd->nfsd_flag & NFSD_AUTHFAIL) { 583 nfsd->nfsd_flag &= ~NFSD_AUTHFAIL; 584 nd->nd_procnum = NFSPROC_NOOP; 585 nd->nd_repstat = 586 (NFSERR_AUTHERR | AUTH_TOOWEAK); 587 cacherep = RC_DOIT; 588 } 589 } 590 591 /* 592 * Loop to get all the write rpc relies that have been 593 * gathered together. 594 */ 595 do { 596 switch (cacherep) { 597 case RC_DOIT: 598 mreq = NULL; 599 netexport_rdlock(); 600 if (writes_todo || nd == NULL || 601 (!(nd->nd_flag & ND_NFSV3) && 602 nd->nd_procnum == NFSPROC_WRITE && 603 nfsrvw_procrastinate > 0)) 604 error = nfsrv_writegather(&nd, slp, 605 l, &mreq); 606 else 607 error = 608 (*(nfsrv3_procs[nd->nd_procnum])) 609 (nd, slp, l, &mreq); 610 netexport_rdunlock(); 611 if (mreq == NULL) { 612 if (nd != NULL) { 613 if (nd->nd_nam2) 614 m_free(nd->nd_nam2); 615 } 616 break; 617 } 618 if (error) { 619 nfsstats.srv_errs++; 620 nfsrv_updatecache(nd, false, mreq); 621 if (nd->nd_nam2) 622 m_freem(nd->nd_nam2); 623 break; 624 } 625 nfsstats.srvrpccnt[nd->nd_procnum]++; 626 nfsrv_updatecache(nd, true, mreq); 627 nd->nd_mrep = (struct mbuf *)0; 628 case RC_REPLY: 629 m = mreq; 630 siz = 0; 631 while (m) { 632 siz += m->m_len; 633 m = m->m_next; 634 } 635 if (siz <= 0 || siz > NFS_MAXPACKET) { 636 printf("mbuf siz=%d\n",siz); 637 panic("Bad nfs svc reply"); 638 } 639 m = mreq; 640 m->m_pkthdr.len = siz; 641 m->m_pkthdr.rcvif = (struct ifnet *)0; 642 /* 643 * For stream protocols, prepend a Sun RPC 644 * Record Mark. 645 */ 646 if (sotype == SOCK_STREAM) { 647 M_PREPEND(m, NFSX_UNSIGNED, M_WAIT); 648 *mtod(m, u_int32_t *) = 649 htonl(0x80000000 | siz); 650 } 651 nd->nd_mreq = m; 652 if (nfsrtton) { 653 nfsd_rt(slp->ns_so->so_type, nd, 654 cacherep); 655 } 656 error = nfsdsock_sendreply(slp, nd); 657 nd = NULL; 658 if (error == EPIPE) 659 nfsrv_zapsock(slp); 660 if (error == EINTR || error == ERESTART) { 661 nfsd->nfsd_slp = NULL; 662 nfsrv_slpderef(slp); 663 goto done; 664 } 665 break; 666 case RC_DROPIT: 667 if (nfsrtton) 668 nfsd_rt(sotype, nd, cacherep); 669 m_freem(nd->nd_mrep); 670 m_freem(nd->nd_nam2); 671 break; 672 } 673 if (nd) { 674 nfsdreq_free(nd); 675 nd = NULL; 676 } 677 678 /* 679 * Check to see if there are outstanding writes that 680 * need to be serviced. 681 */ 682 getmicrotime(&tv); 683 cur_usec = (u_quad_t)tv.tv_sec * 1000000 + 684 (u_quad_t)tv.tv_usec; 685 s = splsoftclock(); 686 if (LIST_FIRST(&slp->ns_tq) && 687 LIST_FIRST(&slp->ns_tq)->nd_time <= cur_usec) { 688 cacherep = RC_DOIT; 689 writes_todo = 1; 690 } else 691 writes_todo = 0; 692 splx(s); 693 } while (writes_todo); 694 if (nfsrv_dorec(slp, nfsd, &nd, &dummy)) { 695 nfsd->nfsd_slp = NULL; 696 nfsrv_slpderef(slp); 697 } 698 } 699 done: 700 mutex_enter(&nfsd_lock); 701 TAILQ_REMOVE(&nfsd_head, nfsd, nfsd_chain); 702 doreinit = --nfs_numnfsd == 0; 703 if (doreinit) 704 nfssvc_sockhead_flag |= SLP_INIT; 705 mutex_exit(&nfsd_lock); 706 cv_destroy(&nfsd->nfsd_cv); 707 kmem_free(nfsd, sizeof(*nfsd)); 708 nsd->nsd_nfsd = NULL; 709 if (doreinit) 710 nfsrv_init(true); /* Reinitialize everything */ 711 uvm_lwp_rele(l); 712 return (error); 713 } 714 715 /* 716 * Shut down a socket associated with an nfssvc_sock structure. 717 * Should be called with the send lock set, if required. 718 * The trick here is to increment the sref at the start, so that the nfsds 719 * will stop using it and clear ns_flag at the end so that it will not be 720 * reassigned during cleanup. 721 * 722 * called at splsoftnet. 723 */ 724 void 725 nfsrv_zapsock(struct nfssvc_sock *slp) 726 { 727 struct nfsuid *nuidp, *nnuidp; 728 struct nfsrv_descript *nwp; 729 struct socket *so; 730 struct mbuf *m; 731 732 if (nfsdsock_drain(slp)) { 733 return; 734 } 735 mutex_enter(&nfsd_lock); 736 if (slp->ns_gflags & SLP_G_DOREC) { 737 TAILQ_REMOVE(&nfssvc_sockpending, slp, ns_pending); 738 slp->ns_gflags &= ~SLP_G_DOREC; 739 } 740 mutex_exit(&nfsd_lock); 741 742 so = slp->ns_so; 743 KASSERT(so != NULL); 744 solock(so); 745 so->so_upcall = NULL; 746 so->so_upcallarg = NULL; 747 so->so_rcv.sb_flags &= ~SB_UPCALL; 748 soshutdown(so, SHUT_RDWR); 749 sounlock(so); 750 751 if (slp->ns_nam) 752 m_free(slp->ns_nam); 753 m_freem(slp->ns_raw); 754 m = slp->ns_rec; 755 while (m != NULL) { 756 struct mbuf *n; 757 758 n = m->m_nextpkt; 759 m_freem(m); 760 m = n; 761 } 762 for (nuidp = TAILQ_FIRST(&slp->ns_uidlruhead); nuidp != 0; 763 nuidp = nnuidp) { 764 nnuidp = TAILQ_NEXT(nuidp, nu_lru); 765 LIST_REMOVE(nuidp, nu_hash); 766 TAILQ_REMOVE(&slp->ns_uidlruhead, nuidp, nu_lru); 767 if (nuidp->nu_flag & NU_NAM) 768 m_freem(nuidp->nu_nam); 769 kmem_free(nuidp, sizeof(*nuidp)); 770 } 771 mutex_enter(&nfsd_lock); 772 while ((nwp = LIST_FIRST(&slp->ns_tq)) != NULL) { 773 LIST_REMOVE(nwp, nd_tq); 774 mutex_exit(&nfsd_lock); 775 nfsdreq_free(nwp); 776 mutex_enter(&nfsd_lock); 777 } 778 mutex_exit(&nfsd_lock); 779 } 780 781 /* 782 * Derefence a server socket structure. If it has no more references and 783 * is no longer valid, you can throw it away. 784 */ 785 void 786 nfsrv_slpderef(struct nfssvc_sock *slp) 787 { 788 uint32_t ref; 789 790 mutex_enter(&nfsd_lock); 791 KASSERT(slp->ns_sref > 0); 792 ref = --slp->ns_sref; 793 mutex_exit(&nfsd_lock); 794 if (ref == 0 && (slp->ns_flags & SLP_VALID) == 0) { 795 file_t *fp; 796 797 mutex_enter(&nfsd_lock); 798 KASSERT((slp->ns_gflags & SLP_G_DOREC) == 0); 799 TAILQ_REMOVE(&nfssvc_sockhead, slp, ns_chain); 800 mutex_exit(&nfsd_lock); 801 802 fp = slp->ns_fp; 803 if (fp != NULL) { 804 slp->ns_fp = NULL; 805 KASSERT(fp != NULL); 806 KASSERT(fp->f_data == slp->ns_so); 807 KASSERT(fp->f_count > 0); 808 closef(fp); 809 slp->ns_so = NULL; 810 } 811 812 nfsrv_sockfree(slp); 813 } 814 } 815 816 /* 817 * Initialize the data structures for the server. 818 * Handshake with any new nfsds starting up to avoid any chance of 819 * corruption. 820 */ 821 void 822 nfsrv_init(int terminating) 823 { 824 struct nfssvc_sock *slp; 825 826 if (!terminating) { 827 mutex_init(&nfsd_lock, MUTEX_DRIVER, IPL_SOFTNET); 828 cv_init(&nfsd_initcv, "nfsdinit"); 829 } 830 831 mutex_enter(&nfsd_lock); 832 if (!terminating && (nfssvc_sockhead_flag & SLP_INIT) != 0) 833 panic("nfsd init"); 834 nfssvc_sockhead_flag |= SLP_INIT; 835 836 if (terminating) { 837 KASSERT(SLIST_EMPTY(&nfsd_idle_head)); 838 KASSERT(TAILQ_EMPTY(&nfsd_head)); 839 while ((slp = TAILQ_FIRST(&nfssvc_sockhead)) != NULL) { 840 mutex_exit(&nfsd_lock); 841 KASSERT(slp->ns_sref == 0); 842 slp->ns_sref++; 843 nfsrv_zapsock(slp); 844 nfsrv_slpderef(slp); 845 mutex_enter(&nfsd_lock); 846 } 847 KASSERT(TAILQ_EMPTY(&nfssvc_sockpending)); 848 mutex_exit(&nfsd_lock); 849 nfsrv_cleancache(); /* And clear out server cache */ 850 } else { 851 mutex_exit(&nfsd_lock); 852 nfs_pub.np_valid = 0; 853 } 854 855 TAILQ_INIT(&nfssvc_sockhead); 856 TAILQ_INIT(&nfssvc_sockpending); 857 858 TAILQ_INIT(&nfsd_head); 859 SLIST_INIT(&nfsd_idle_head); 860 nfsd_head_flag &= ~NFSD_CHECKSLP; 861 862 nfs_udpsock = nfsrv_sockalloc(); 863 nfs_udp6sock = nfsrv_sockalloc(); 864 865 mutex_enter(&nfsd_lock); 866 nfssvc_sockhead_flag &= ~SLP_INIT; 867 cv_broadcast(&nfsd_initcv); 868 mutex_exit(&nfsd_lock); 869 } 870 871 void 872 nfsrv_fini(void) 873 { 874 875 nfsrv_init(true); 876 cv_destroy(&nfsd_initcv); 877 mutex_destroy(&nfsd_lock); 878 } 879 880 /* 881 * Add entries to the server monitor log. 882 */ 883 static void 884 nfsd_rt(int sotype, struct nfsrv_descript *nd, int cacherep) 885 { 886 struct timeval tv; 887 struct drt *rt; 888 889 rt = &nfsdrt.drt[nfsdrt.pos]; 890 if (cacherep == RC_DOIT) 891 rt->flag = 0; 892 else if (cacherep == RC_REPLY) 893 rt->flag = DRT_CACHEREPLY; 894 else 895 rt->flag = DRT_CACHEDROP; 896 if (sotype == SOCK_STREAM) 897 rt->flag |= DRT_TCP; 898 if (nd->nd_flag & ND_NFSV3) 899 rt->flag |= DRT_NFSV3; 900 rt->proc = nd->nd_procnum; 901 if (mtod(nd->nd_nam, struct sockaddr *)->sa_family == AF_INET) 902 rt->ipadr = mtod(nd->nd_nam, struct sockaddr_in *)->sin_addr.s_addr; 903 else 904 rt->ipadr = INADDR_ANY; 905 getmicrotime(&tv); 906 rt->resptime = ((tv.tv_sec - nd->nd_starttime.tv_sec) * 1000000) + 907 (tv.tv_usec - nd->nd_starttime.tv_usec); 908 rt->tstamp = tv; 909 nfsdrt.pos = (nfsdrt.pos + 1) % NFSRTTLOGSIZ; 910 } 911