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