1 /* $NetBSD: nfs_syscalls.c,v 1.36 1999/06/29 22:18:48 wrstuden 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. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the University of 21 * California, Berkeley and its contributors. 22 * 4. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 * 38 * @(#)nfs_syscalls.c 8.5 (Berkeley) 3/30/95 39 */ 40 41 #include "fs_nfs.h" 42 #include "opt_nfsserver.h" 43 #include "opt_iso.h" 44 45 #include <sys/param.h> 46 #include <sys/systm.h> 47 #include <sys/kernel.h> 48 #include <sys/file.h> 49 #include <sys/stat.h> 50 #include <sys/vnode.h> 51 #include <sys/mount.h> 52 #include <sys/proc.h> 53 #include <sys/uio.h> 54 #include <sys/malloc.h> 55 #include <sys/buf.h> 56 #include <sys/mbuf.h> 57 #include <sys/socket.h> 58 #include <sys/socketvar.h> 59 #include <sys/domain.h> 60 #include <sys/protosw.h> 61 #include <sys/namei.h> 62 #include <sys/syslog.h> 63 #include <sys/filedesc.h> 64 65 #include <sys/syscallargs.h> 66 67 #include <netinet/in.h> 68 #include <netinet/tcp.h> 69 #ifdef ISO 70 #include <netiso/iso.h> 71 #endif 72 #include <nfs/xdr_subs.h> 73 #include <nfs/rpcv2.h> 74 #include <nfs/nfsproto.h> 75 #include <nfs/nfs.h> 76 #include <nfs/nfsm_subs.h> 77 #include <nfs/nfsrvcache.h> 78 #include <nfs/nfsmount.h> 79 #include <nfs/nfsnode.h> 80 #include <nfs/nqnfs.h> 81 #include <nfs/nfsrtt.h> 82 #include <nfs/nfs_var.h> 83 84 void nfsrv_zapsock __P((struct nfssvc_sock *)); 85 86 /* Global defs. */ 87 extern int32_t (*nfsrv3_procs[NFS_NPROCS]) __P((struct nfsrv_descript *, 88 struct nfssvc_sock *, 89 struct proc *, struct mbuf **)); 90 extern int nfs_numasync; 91 extern time_t nqnfsstarttime; 92 extern int nqsrv_writeslack; 93 extern int nfsrtton; 94 extern struct nfsstats nfsstats; 95 extern int nfsrvw_procrastinate; 96 struct nfssvc_sock *nfs_udpsock, *nfs_cltpsock; 97 int nuidhash_max = NFS_MAXUIDHASH; 98 int nfsd_waiting = 0; 99 #ifdef NFSSERVER 100 static int nfs_numnfsd = 0; 101 static int notstarted = 1; 102 static int modify_flag = 0; 103 static struct nfsdrt nfsdrt; 104 extern struct nfs_public nfs_pub; 105 #endif 106 107 #define TRUE 1 108 #define FALSE 0 109 110 #ifdef NFS 111 static int nfs_asyncdaemon[NFS_MAXASYNCDAEMON]; 112 #endif 113 114 #ifdef NFSSERVER 115 static void nfsd_rt __P((int, struct nfsrv_descript *, int)); 116 #endif 117 118 /* 119 * NFS server system calls 120 */ 121 122 123 /* 124 * Nfs server pseudo system call for the nfsd's 125 * Based on the flag value it either: 126 * - adds a socket to the selection list 127 * - remains in the kernel as an nfsd 128 * - remains in the kernel as an nfsiod 129 */ 130 int 131 sys_nfssvc(p, v, retval) 132 struct proc *p; 133 void *v; 134 register_t *retval; 135 { 136 register struct sys_nfssvc_args /* { 137 syscallarg(int) flag; 138 syscallarg(caddr_t) argp; 139 } */ *uap = v; 140 int error; 141 #ifdef NFS 142 struct nameidata nd; 143 struct nfsmount *nmp; 144 struct nfsd_cargs ncd; 145 #endif 146 #ifdef NFSSERVER 147 struct file *fp; 148 struct mbuf *nam; 149 struct nfsd_args nfsdarg; 150 struct nfsd_srvargs nfsd_srvargs, *nsd = &nfsd_srvargs; 151 struct nfsd *nfsd; 152 struct nfssvc_sock *slp; 153 struct nfsuid *nuidp; 154 #endif 155 156 /* 157 * Must be super user 158 */ 159 error = suser(p->p_ucred, &p->p_acflag); 160 if(error) 161 return (error); 162 while (nfssvc_sockhead_flag & SLP_INIT) { 163 nfssvc_sockhead_flag |= SLP_WANTINIT; 164 (void) tsleep((caddr_t)&nfssvc_sockhead, PSOCK, "nfsd init", 0); 165 } 166 if (SCARG(uap, flag) & NFSSVC_BIOD) { 167 #ifdef NFS 168 error = nfssvc_iod(p); 169 #else 170 error = ENOSYS; 171 #endif 172 } else if (SCARG(uap, flag) & NFSSVC_MNTD) { 173 #ifndef NFS 174 error = ENOSYS; 175 #else 176 error = copyin(SCARG(uap, argp), (caddr_t)&ncd, sizeof (ncd)); 177 if (error) 178 return (error); 179 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE, 180 ncd.ncd_dirp, p); 181 error = namei(&nd); 182 if (error) 183 return (error); 184 if ((nd.ni_vp->v_flag & VROOT) == 0) 185 error = EINVAL; 186 nmp = VFSTONFS(nd.ni_vp->v_mount); 187 vput(nd.ni_vp); 188 if (error) 189 return (error); 190 if ((nmp->nm_iflag & NFSMNT_MNTD) && 191 (SCARG(uap, flag) & NFSSVC_GOTAUTH) == 0) 192 return (0); 193 nmp->nm_iflag |= NFSMNT_MNTD; 194 error = nqnfs_clientd(nmp, p->p_ucred, &ncd, SCARG(uap, flag), 195 SCARG(uap, argp), p); 196 #endif /* NFS */ 197 } else if (SCARG(uap, flag) & NFSSVC_ADDSOCK) { 198 #ifndef NFSSERVER 199 error = ENOSYS; 200 #else 201 error = copyin(SCARG(uap, argp), (caddr_t)&nfsdarg, 202 sizeof(nfsdarg)); 203 if (error) 204 return (error); 205 /* getsock() will use the descriptor for us */ 206 error = getsock(p->p_fd, nfsdarg.sock, &fp); 207 if (error) 208 return (error); 209 /* 210 * Get the client address for connected sockets. 211 */ 212 if (nfsdarg.name == NULL || nfsdarg.namelen == 0) 213 nam = (struct mbuf *)0; 214 else { 215 error = sockargs(&nam, nfsdarg.name, nfsdarg.namelen, 216 MT_SONAME); 217 if (error) { 218 FILE_UNUSE(fp, NULL); 219 return (error); 220 } 221 } 222 error = nfssvc_addsock(fp, nam); 223 FILE_UNUSE(fp, NULL); 224 #endif /* !NFSSERVER */ 225 } else { 226 #ifndef NFSSERVER 227 error = ENOSYS; 228 #else 229 error = copyin(SCARG(uap, argp), (caddr_t)nsd, sizeof (*nsd)); 230 if (error) 231 return (error); 232 if ((SCARG(uap, flag) & NFSSVC_AUTHIN) && 233 ((nfsd = nsd->nsd_nfsd)) != NULL && 234 (nfsd->nfsd_slp->ns_flag & SLP_VALID)) { 235 slp = nfsd->nfsd_slp; 236 237 /* 238 * First check to see if another nfsd has already 239 * added this credential. 240 */ 241 for (nuidp = NUIDHASH(slp,nsd->nsd_cr.cr_uid)->lh_first; 242 nuidp != 0; nuidp = nuidp->nu_hash.le_next) { 243 if (nuidp->nu_cr.cr_uid == nsd->nsd_cr.cr_uid && 244 (!nfsd->nfsd_nd->nd_nam2 || 245 netaddr_match(NU_NETFAM(nuidp), 246 &nuidp->nu_haddr, nfsd->nfsd_nd->nd_nam2))) 247 break; 248 } 249 if (nuidp) { 250 nfsrv_setcred(&nuidp->nu_cr,&nfsd->nfsd_nd->nd_cr); 251 nfsd->nfsd_nd->nd_flag |= ND_KERBFULL; 252 } else { 253 /* 254 * Nope, so we will. 255 */ 256 if (slp->ns_numuids < nuidhash_max) { 257 slp->ns_numuids++; 258 nuidp = (struct nfsuid *) 259 malloc(sizeof (struct nfsuid), M_NFSUID, 260 M_WAITOK); 261 } else 262 nuidp = (struct nfsuid *)0; 263 if ((slp->ns_flag & SLP_VALID) == 0) { 264 if (nuidp) 265 free((caddr_t)nuidp, M_NFSUID); 266 } else { 267 if (nuidp == (struct nfsuid *)0) { 268 nuidp = slp->ns_uidlruhead.tqh_first; 269 LIST_REMOVE(nuidp, nu_hash); 270 TAILQ_REMOVE(&slp->ns_uidlruhead, nuidp, 271 nu_lru); 272 if (nuidp->nu_flag & NU_NAM) 273 m_freem(nuidp->nu_nam); 274 } 275 nuidp->nu_flag = 0; 276 nuidp->nu_cr = nsd->nsd_cr; 277 if (nuidp->nu_cr.cr_ngroups > NGROUPS) 278 nuidp->nu_cr.cr_ngroups = NGROUPS; 279 nuidp->nu_cr.cr_ref = 1; 280 nuidp->nu_timestamp = nsd->nsd_timestamp; 281 nuidp->nu_expire = time.tv_sec + nsd->nsd_ttl; 282 /* 283 * and save the session key in nu_key. 284 */ 285 memcpy(nuidp->nu_key, nsd->nsd_key, 286 sizeof(nsd->nsd_key)); 287 if (nfsd->nfsd_nd->nd_nam2) { 288 struct sockaddr_in *saddr; 289 290 saddr = mtod(nfsd->nfsd_nd->nd_nam2, 291 struct sockaddr_in *); 292 switch (saddr->sin_family) { 293 case AF_INET: 294 nuidp->nu_flag |= NU_INETADDR; 295 nuidp->nu_inetaddr = 296 saddr->sin_addr.s_addr; 297 break; 298 case AF_ISO: 299 default: 300 nuidp->nu_flag |= NU_NAM; 301 nuidp->nu_nam = m_copym( 302 nfsd->nfsd_nd->nd_nam2, 0, 303 M_COPYALL, M_WAIT); 304 break; 305 }; 306 } 307 TAILQ_INSERT_TAIL(&slp->ns_uidlruhead, nuidp, 308 nu_lru); 309 LIST_INSERT_HEAD(NUIDHASH(slp, nsd->nsd_uid), 310 nuidp, nu_hash); 311 nfsrv_setcred(&nuidp->nu_cr, 312 &nfsd->nfsd_nd->nd_cr); 313 nfsd->nfsd_nd->nd_flag |= ND_KERBFULL; 314 } 315 } 316 } 317 if ((SCARG(uap, flag) & NFSSVC_AUTHINFAIL) && 318 (nfsd = nsd->nsd_nfsd)) 319 nfsd->nfsd_flag |= NFSD_AUTHFAIL; 320 error = nfssvc_nfsd(nsd, SCARG(uap, argp), p); 321 #endif /* !NFSSERVER */ 322 } 323 if (error == EINTR || error == ERESTART) 324 error = 0; 325 return (error); 326 } 327 328 #ifdef NFSSERVER 329 /* 330 * Adds a socket to the list for servicing by nfsds. 331 */ 332 int 333 nfssvc_addsock(fp, mynam) 334 struct file *fp; 335 struct mbuf *mynam; 336 { 337 register struct mbuf *m; 338 register int siz; 339 register struct nfssvc_sock *slp; 340 register struct socket *so; 341 struct nfssvc_sock *tslp; 342 int error, s; 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 tslp = nfs_udpsock; 351 if (tslp->ns_flag & SLP_VALID) { 352 m_freem(mynam); 353 return (EPERM); 354 } 355 #ifdef ISO 356 } else if (so->so_proto->pr_protocol == ISOPROTO_CLTP) { 357 tslp = nfs_cltpsock; 358 if (tslp->ns_flag & SLP_VALID) { 359 m_freem(mynam); 360 return (EPERM); 361 } 362 #endif /* ISO */ 363 } 364 if (so->so_type == SOCK_STREAM) 365 siz = NFS_MAXPACKET + sizeof (u_long); 366 else 367 siz = NFS_MAXPACKET; 368 error = soreserve(so, siz, siz); 369 if (error) { 370 m_freem(mynam); 371 return (error); 372 } 373 374 /* 375 * Set protocol specific options { for now TCP only } and 376 * reserve some space. For datagram sockets, this can get called 377 * repeatedly for the same socket, but that isn't harmful. 378 */ 379 if (so->so_type == SOCK_STREAM) { 380 MGET(m, M_WAIT, MT_SOOPTS); 381 *mtod(m, int32_t *) = 1; 382 m->m_len = sizeof(int32_t); 383 sosetopt(so, SOL_SOCKET, SO_KEEPALIVE, m); 384 } 385 if (so->so_proto->pr_domain->dom_family == AF_INET && 386 so->so_proto->pr_protocol == IPPROTO_TCP) { 387 MGET(m, M_WAIT, MT_SOOPTS); 388 *mtod(m, int32_t *) = 1; 389 m->m_len = sizeof(int32_t); 390 sosetopt(so, IPPROTO_TCP, TCP_NODELAY, m); 391 } 392 so->so_rcv.sb_flags &= ~SB_NOINTR; 393 so->so_rcv.sb_timeo = 0; 394 so->so_snd.sb_flags &= ~SB_NOINTR; 395 so->so_snd.sb_timeo = 0; 396 if (tslp) 397 slp = tslp; 398 else { 399 slp = (struct nfssvc_sock *) 400 malloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK); 401 memset((caddr_t)slp, 0, sizeof (struct nfssvc_sock)); 402 TAILQ_INIT(&slp->ns_uidlruhead); 403 TAILQ_INSERT_TAIL(&nfssvc_sockhead, slp, ns_chain); 404 } 405 slp->ns_so = so; 406 slp->ns_nam = mynam; 407 fp->f_count++; 408 slp->ns_fp = fp; 409 s = splsoftnet(); 410 so->so_upcallarg = (caddr_t)slp; 411 so->so_upcall = nfsrv_rcv; 412 so->so_rcv.sb_flags |= SB_UPCALL; 413 slp->ns_flag = (SLP_VALID | SLP_NEEDQ); 414 nfsrv_wakenfsd(slp); 415 splx(s); 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(nsd, argp, p) 425 struct nfsd_srvargs *nsd; 426 caddr_t argp; 427 struct proc *p; 428 { 429 register struct mbuf *m; 430 register int siz; 431 register struct nfssvc_sock *slp; 432 register struct socket *so; 433 register int *solockp; 434 struct nfsd *nfsd = nsd->nsd_nfsd; 435 struct nfsrv_descript *nd = NULL; 436 struct mbuf *mreq; 437 int error = 0, cacherep, s, sotype, writes_todo; 438 u_quad_t cur_usec; 439 440 #ifndef nolint 441 cacherep = RC_DOIT; 442 writes_todo = 0; 443 #endif 444 s = splsoftnet(); 445 if (nfsd == (struct nfsd *)0) { 446 nsd->nsd_nfsd = nfsd = (struct nfsd *) 447 malloc(sizeof (struct nfsd), M_NFSD, M_WAITOK); 448 memset((caddr_t)nfsd, 0, sizeof (struct nfsd)); 449 nfsd->nfsd_procp = p; 450 TAILQ_INSERT_TAIL(&nfsd_head, nfsd, nfsd_chain); 451 nfs_numnfsd++; 452 } 453 p->p_holdcnt++; 454 /* 455 * Loop getting rpc requests until SIGKILL. 456 */ 457 for (;;) { 458 if ((nfsd->nfsd_flag & NFSD_REQINPROG) == 0) { 459 while (nfsd->nfsd_slp == (struct nfssvc_sock *)0 && 460 (nfsd_head_flag & NFSD_CHECKSLP) == 0) { 461 nfsd->nfsd_flag |= NFSD_WAITING; 462 nfsd_waiting++; 463 error = tsleep((caddr_t)nfsd, PSOCK | PCATCH, 464 "nfsd", 0); 465 nfsd_waiting--; 466 if (error) 467 goto done; 468 } 469 if (nfsd->nfsd_slp == (struct nfssvc_sock *)0 && 470 (nfsd_head_flag & NFSD_CHECKSLP) != 0) { 471 for (slp = nfssvc_sockhead.tqh_first; slp != 0; 472 slp = slp->ns_chain.tqe_next) { 473 if ((slp->ns_flag & (SLP_VALID | SLP_DOREC)) 474 == (SLP_VALID | SLP_DOREC)) { 475 slp->ns_flag &= ~SLP_DOREC; 476 slp->ns_sref++; 477 nfsd->nfsd_slp = slp; 478 break; 479 } 480 } 481 if (slp == 0) 482 nfsd_head_flag &= ~NFSD_CHECKSLP; 483 } 484 if ((slp = nfsd->nfsd_slp) == (struct nfssvc_sock *)0) 485 continue; 486 if (slp->ns_flag & SLP_VALID) { 487 if (slp->ns_flag & SLP_DISCONN) 488 nfsrv_zapsock(slp); 489 else if (slp->ns_flag & SLP_NEEDQ) { 490 slp->ns_flag &= ~SLP_NEEDQ; 491 (void) nfs_sndlock(&slp->ns_solock, 492 (struct nfsreq *)0); 493 nfsrv_rcv(slp->ns_so, (caddr_t)slp, 494 M_WAIT); 495 nfs_sndunlock(&slp->ns_solock); 496 } 497 error = nfsrv_dorec(slp, nfsd, &nd); 498 cur_usec = (u_quad_t)time.tv_sec * 1000000 + 499 (u_quad_t)time.tv_usec; 500 if (error && slp->ns_tq.lh_first && 501 slp->ns_tq.lh_first->nd_time <= cur_usec) { 502 error = 0; 503 cacherep = RC_DOIT; 504 writes_todo = 1; 505 } else 506 writes_todo = 0; 507 nfsd->nfsd_flag |= NFSD_REQINPROG; 508 } 509 } else { 510 error = 0; 511 slp = nfsd->nfsd_slp; 512 } 513 if (error || (slp->ns_flag & SLP_VALID) == 0) { 514 if (nd) { 515 free((caddr_t)nd, M_NFSRVDESC); 516 nd = NULL; 517 } 518 nfsd->nfsd_slp = (struct nfssvc_sock *)0; 519 nfsd->nfsd_flag &= ~NFSD_REQINPROG; 520 nfsrv_slpderef(slp); 521 continue; 522 } 523 splx(s); 524 so = slp->ns_so; 525 sotype = so->so_type; 526 if (so->so_proto->pr_flags & PR_CONNREQUIRED) 527 solockp = &slp->ns_solock; 528 else 529 solockp = (int *)0; 530 if (nd) { 531 nd->nd_starttime = time; 532 if (nd->nd_nam2) 533 nd->nd_nam = nd->nd_nam2; 534 else 535 nd->nd_nam = slp->ns_nam; 536 537 /* 538 * Check to see if authorization is needed. 539 */ 540 if (nfsd->nfsd_flag & NFSD_NEEDAUTH) { 541 nfsd->nfsd_flag &= ~NFSD_NEEDAUTH; 542 nsd->nsd_haddr = mtod(nd->nd_nam, 543 struct sockaddr_in *)->sin_addr.s_addr; 544 nsd->nsd_authlen = nfsd->nfsd_authlen; 545 nsd->nsd_verflen = nfsd->nfsd_verflen; 546 if (!copyout(nfsd->nfsd_authstr,nsd->nsd_authstr, 547 nfsd->nfsd_authlen) && 548 !copyout(nfsd->nfsd_verfstr, nsd->nsd_verfstr, 549 nfsd->nfsd_verflen) && 550 !copyout((caddr_t)nsd, argp, sizeof (*nsd))) { 551 p->p_holdcnt--; 552 return (ENEEDAUTH); 553 } 554 cacherep = RC_DROPIT; 555 } else 556 cacherep = nfsrv_getcache(nd, slp, &mreq); 557 558 /* 559 * Check for just starting up for NQNFS and send 560 * fake "try again later" replies to the NQNFS clients. 561 */ 562 if (notstarted && nqnfsstarttime <= time.tv_sec) { 563 if (modify_flag) { 564 nqnfsstarttime = time.tv_sec + nqsrv_writeslack; 565 modify_flag = 0; 566 } else 567 notstarted = 0; 568 } 569 if (notstarted) { 570 if ((nd->nd_flag & ND_NQNFS) == 0) 571 cacherep = RC_DROPIT; 572 else if (nd->nd_procnum != NFSPROC_WRITE) { 573 nd->nd_procnum = NFSPROC_NOOP; 574 nd->nd_repstat = NQNFS_TRYLATER; 575 cacherep = RC_DOIT; 576 } else 577 modify_flag = 1; 578 } else if (nfsd->nfsd_flag & NFSD_AUTHFAIL) { 579 nfsd->nfsd_flag &= ~NFSD_AUTHFAIL; 580 nd->nd_procnum = NFSPROC_NOOP; 581 nd->nd_repstat = (NFSERR_AUTHERR | AUTH_TOOWEAK); 582 cacherep = RC_DOIT; 583 } 584 } 585 586 /* 587 * Loop to get all the write rpc relies that have been 588 * gathered together. 589 */ 590 do { 591 #ifdef DIAGNOSTIC 592 int lockcount; 593 #endif 594 switch (cacherep) { 595 case RC_DOIT: 596 #ifdef DIAGNOSTIC 597 /* 598 * NFS server procs should neither release 599 * locks already held, nor leave things 600 * locked. Catch this sooner, rather than 601 * later (when we try to relock something we 602 * already have locked). Careful inspection 603 * of the failing routine usually turns up the 604 * lock leak.. once we know what it is.. 605 */ 606 lockcount = p->p_locks; 607 #endif 608 if (writes_todo || (nd->nd_procnum == NFSPROC_WRITE && 609 nfsrvw_procrastinate > 0 && !notstarted)) 610 error = nfsrv_writegather(&nd, slp, 611 nfsd->nfsd_procp, &mreq); 612 else 613 error = (*(nfsrv3_procs[nd->nd_procnum]))(nd, 614 slp, nfsd->nfsd_procp, &mreq); 615 #ifdef DIAGNOSTIC 616 if (p->p_locks != lockcount) { 617 /* 618 * If you see this panic, audit 619 * nfsrv3_procs[nd->nd_procnum] for vnode 620 * locking errors (usually, it's due to 621 * forgetting to vput() something). 622 */ 623 panic("nfsd: locking botch in op %d", 624 nd ? nd->nd_procnum : -1); 625 } 626 #endif 627 if (mreq == NULL) 628 break; 629 if (error) { 630 if (nd->nd_procnum != NQNFSPROC_VACATED) 631 nfsstats.srv_errs++; 632 nfsrv_updatecache(nd, FALSE, mreq); 633 if (nd->nd_nam2) 634 m_freem(nd->nd_nam2); 635 break; 636 } 637 nfsstats.srvrpccnt[nd->nd_procnum]++; 638 nfsrv_updatecache(nd, TRUE, mreq); 639 nd->nd_mrep = (struct mbuf *)0; 640 case RC_REPLY: 641 m = mreq; 642 siz = 0; 643 while (m) { 644 siz += m->m_len; 645 m = m->m_next; 646 } 647 if (siz <= 0 || siz > NFS_MAXPACKET) { 648 printf("mbuf siz=%d\n",siz); 649 panic("Bad nfs svc reply"); 650 } 651 m = mreq; 652 m->m_pkthdr.len = siz; 653 m->m_pkthdr.rcvif = (struct ifnet *)0; 654 /* 655 * For stream protocols, prepend a Sun RPC 656 * Record Mark. 657 */ 658 if (sotype == SOCK_STREAM) { 659 M_PREPEND(m, NFSX_UNSIGNED, M_WAIT); 660 *mtod(m, u_int32_t *) = htonl(0x80000000 | siz); 661 } 662 if (solockp) 663 (void) nfs_sndlock(solockp, (struct nfsreq *)0); 664 if (slp->ns_flag & SLP_VALID) 665 error = nfs_send(so, nd->nd_nam2, m, NULL); 666 else { 667 error = EPIPE; 668 m_freem(m); 669 } 670 if (nfsrtton) 671 nfsd_rt(sotype, nd, cacherep); 672 if (nd->nd_nam2) 673 MFREE(nd->nd_nam2, m); 674 if (nd->nd_mrep) 675 m_freem(nd->nd_mrep); 676 if (error == EPIPE) 677 nfsrv_zapsock(slp); 678 if (solockp) 679 nfs_sndunlock(solockp); 680 if (error == EINTR || error == ERESTART) { 681 free((caddr_t)nd, M_NFSRVDESC); 682 nfsrv_slpderef(slp); 683 s = splsoftnet(); 684 goto done; 685 } 686 break; 687 case RC_DROPIT: 688 if (nfsrtton) 689 nfsd_rt(sotype, nd, cacherep); 690 m_freem(nd->nd_mrep); 691 m_freem(nd->nd_nam2); 692 break; 693 }; 694 if (nd) { 695 FREE((caddr_t)nd, M_NFSRVDESC); 696 nd = NULL; 697 } 698 699 /* 700 * Check to see if there are outstanding writes that 701 * need to be serviced. 702 */ 703 cur_usec = (u_quad_t)time.tv_sec * 1000000 + 704 (u_quad_t)time.tv_usec; 705 s = splsoftclock(); 706 if (slp->ns_tq.lh_first && 707 slp->ns_tq.lh_first->nd_time <= cur_usec) { 708 cacherep = RC_DOIT; 709 writes_todo = 1; 710 } else 711 writes_todo = 0; 712 splx(s); 713 } while (writes_todo); 714 s = splsoftnet(); 715 if (nfsrv_dorec(slp, nfsd, &nd)) { 716 nfsd->nfsd_flag &= ~NFSD_REQINPROG; 717 nfsd->nfsd_slp = NULL; 718 nfsrv_slpderef(slp); 719 } 720 } 721 done: 722 p->p_holdcnt--; 723 TAILQ_REMOVE(&nfsd_head, nfsd, nfsd_chain); 724 splx(s); 725 free((caddr_t)nfsd, M_NFSD); 726 nsd->nsd_nfsd = (struct nfsd *)0; 727 if (--nfs_numnfsd == 0) 728 nfsrv_init(TRUE); /* Reinitialize everything */ 729 return (error); 730 } 731 732 /* 733 * Shut down a socket associated with an nfssvc_sock structure. 734 * Should be called with the send lock set, if required. 735 * The trick here is to increment the sref at the start, so that the nfsds 736 * will stop using it and clear ns_flag at the end so that it will not be 737 * reassigned during cleanup. 738 */ 739 void 740 nfsrv_zapsock(slp) 741 register struct nfssvc_sock *slp; 742 { 743 register struct nfsuid *nuidp, *nnuidp; 744 register struct nfsrv_descript *nwp, *nnwp; 745 struct socket *so; 746 struct file *fp; 747 struct mbuf *m; 748 int s; 749 750 slp->ns_flag &= ~SLP_ALLFLAGS; 751 fp = slp->ns_fp; 752 if (fp) { 753 FILE_USE(fp); 754 slp->ns_fp = (struct file *)0; 755 so = slp->ns_so; 756 so->so_upcall = NULL; 757 so->so_upcallarg = NULL; 758 so->so_rcv.sb_flags &= ~SB_UPCALL; 759 soshutdown(so, 2); 760 closef(fp, (struct proc *)0); 761 if (slp->ns_nam) 762 MFREE(slp->ns_nam, m); 763 m_freem(slp->ns_raw); 764 m_freem(slp->ns_rec); 765 for (nuidp = slp->ns_uidlruhead.tqh_first; nuidp != 0; 766 nuidp = nnuidp) { 767 nnuidp = nuidp->nu_lru.tqe_next; 768 LIST_REMOVE(nuidp, nu_hash); 769 TAILQ_REMOVE(&slp->ns_uidlruhead, nuidp, nu_lru); 770 if (nuidp->nu_flag & NU_NAM) 771 m_freem(nuidp->nu_nam); 772 free((caddr_t)nuidp, M_NFSUID); 773 } 774 s = splsoftclock(); 775 for (nwp = slp->ns_tq.lh_first; nwp; nwp = nnwp) { 776 nnwp = nwp->nd_tq.le_next; 777 LIST_REMOVE(nwp, nd_tq); 778 free((caddr_t)nwp, M_NFSRVDESC); 779 } 780 LIST_INIT(&slp->ns_tq); 781 splx(s); 782 } 783 } 784 785 /* 786 * Derefence a server socket structure. If it has no more references and 787 * is no longer valid, you can throw it away. 788 */ 789 void 790 nfsrv_slpderef(slp) 791 register struct nfssvc_sock *slp; 792 { 793 if (--(slp->ns_sref) == 0 && (slp->ns_flag & SLP_VALID) == 0) { 794 TAILQ_REMOVE(&nfssvc_sockhead, slp, ns_chain); 795 free((caddr_t)slp, M_NFSSVC); 796 } 797 } 798 799 /* 800 * Initialize the data structures for the server. 801 * Handshake with any new nfsds starting up to avoid any chance of 802 * corruption. 803 */ 804 void 805 nfsrv_init(terminating) 806 int terminating; 807 { 808 register struct nfssvc_sock *slp, *nslp; 809 810 if (nfssvc_sockhead_flag & SLP_INIT) 811 panic("nfsd init"); 812 nfssvc_sockhead_flag |= SLP_INIT; 813 if (terminating) { 814 for (slp = nfssvc_sockhead.tqh_first; slp != 0; slp = nslp) { 815 nslp = slp->ns_chain.tqe_next; 816 if (slp->ns_flag & SLP_VALID) 817 nfsrv_zapsock(slp); 818 TAILQ_REMOVE(&nfssvc_sockhead, slp, ns_chain); 819 free((caddr_t)slp, M_NFSSVC); 820 } 821 nfsrv_cleancache(); /* And clear out server cache */ 822 } else 823 nfs_pub.np_valid = 0; 824 825 TAILQ_INIT(&nfssvc_sockhead); 826 nfssvc_sockhead_flag &= ~SLP_INIT; 827 if (nfssvc_sockhead_flag & SLP_WANTINIT) { 828 nfssvc_sockhead_flag &= ~SLP_WANTINIT; 829 wakeup((caddr_t)&nfssvc_sockhead); 830 } 831 832 TAILQ_INIT(&nfsd_head); 833 nfsd_head_flag &= ~NFSD_CHECKSLP; 834 835 nfs_udpsock = (struct nfssvc_sock *) 836 malloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK); 837 memset((caddr_t)nfs_udpsock, 0, sizeof (struct nfssvc_sock)); 838 TAILQ_INIT(&nfs_udpsock->ns_uidlruhead); 839 TAILQ_INSERT_HEAD(&nfssvc_sockhead, nfs_udpsock, ns_chain); 840 841 nfs_cltpsock = (struct nfssvc_sock *) 842 malloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK); 843 memset((caddr_t)nfs_cltpsock, 0, sizeof (struct nfssvc_sock)); 844 TAILQ_INIT(&nfs_cltpsock->ns_uidlruhead); 845 TAILQ_INSERT_TAIL(&nfssvc_sockhead, nfs_cltpsock, ns_chain); 846 } 847 848 /* 849 * Add entries to the server monitor log. 850 */ 851 static void 852 nfsd_rt(sotype, nd, cacherep) 853 int sotype; 854 register struct nfsrv_descript *nd; 855 int cacherep; 856 { 857 register struct drt *rt; 858 859 rt = &nfsdrt.drt[nfsdrt.pos]; 860 if (cacherep == RC_DOIT) 861 rt->flag = 0; 862 else if (cacherep == RC_REPLY) 863 rt->flag = DRT_CACHEREPLY; 864 else 865 rt->flag = DRT_CACHEDROP; 866 if (sotype == SOCK_STREAM) 867 rt->flag |= DRT_TCP; 868 if (nd->nd_flag & ND_NQNFS) 869 rt->flag |= DRT_NQNFS; 870 else if (nd->nd_flag & ND_NFSV3) 871 rt->flag |= DRT_NFSV3; 872 rt->proc = nd->nd_procnum; 873 if (mtod(nd->nd_nam, struct sockaddr *)->sa_family == AF_INET) 874 rt->ipadr = mtod(nd->nd_nam, struct sockaddr_in *)->sin_addr.s_addr; 875 else 876 rt->ipadr = INADDR_ANY; 877 rt->resptime = ((time.tv_sec - nd->nd_starttime.tv_sec) * 1000000) + 878 (time.tv_usec - nd->nd_starttime.tv_usec); 879 rt->tstamp = time; 880 nfsdrt.pos = (nfsdrt.pos + 1) % NFSRTTLOGSIZ; 881 } 882 #endif /* NFSSERVER */ 883 884 #ifdef NFS 885 886 int nfs_defect = 0; 887 /* 888 * Asynchronous I/O daemons for client nfs. 889 * They do read-ahead and write-behind operations on the block I/O cache. 890 * Never returns unless it fails or gets killed. 891 */ 892 int 893 nfssvc_iod(p) 894 struct proc *p; 895 { 896 register struct buf *bp; 897 register int i, myiod; 898 struct nfsmount *nmp; 899 int error = 0; 900 901 /* 902 * Assign my position or return error if too many already running 903 */ 904 myiod = -1; 905 for (i = 0; i < NFS_MAXASYNCDAEMON; i++) 906 if (nfs_asyncdaemon[i] == 0) { 907 myiod = i; 908 break; 909 } 910 if (myiod == -1) 911 return (EBUSY); 912 nfs_asyncdaemon[myiod] = 1; 913 nfs_numasync++; 914 p->p_holdcnt++; 915 /* 916 * Just loop around doin our stuff until SIGKILL 917 */ 918 for (;;) { 919 while (((nmp = nfs_iodmount[myiod]) == NULL 920 || nmp->nm_bufq.tqh_first == NULL) 921 && error == 0) { 922 if (nmp) 923 nmp->nm_bufqiods--; 924 nfs_iodwant[myiod] = p; 925 nfs_iodmount[myiod] = NULL; 926 error = tsleep((caddr_t)&nfs_iodwant[myiod], 927 PWAIT | PCATCH, "nfsidl", 0); 928 } 929 if (error) { 930 if (nmp) 931 nmp->nm_bufqiods--; 932 nfs_iodmount[myiod] = NULL; 933 break; 934 } 935 while ((bp = nmp->nm_bufq.tqh_first) != NULL) { 936 /* Take one off the front of the list */ 937 TAILQ_REMOVE(&nmp->nm_bufq, bp, b_freelist); 938 nmp->nm_bufqlen--; 939 if (nmp->nm_bufqwant && nmp->nm_bufqlen < 2 * nfs_numasync) { 940 nmp->nm_bufqwant = FALSE; 941 wakeup(&nmp->nm_bufq); 942 } 943 if (bp->b_flags & B_READ) 944 (void) nfs_doio(bp, bp->b_rcred, (struct proc *)0); 945 else 946 (void) nfs_doio(bp, bp->b_wcred, (struct proc *)0); 947 /* 948 * If there are more than one iod on this mount, then defect 949 * so that the iods can be shared out fairly between the mounts 950 */ 951 if (nfs_defect && nmp->nm_bufqiods > 1) { 952 nfs_iodmount[myiod] = NULL; 953 nmp->nm_bufqiods--; 954 break; 955 } 956 } 957 } 958 p->p_holdcnt--; 959 nfs_asyncdaemon[myiod] = 0; 960 nfs_numasync--; 961 return (error); 962 } 963 964 965 /* 966 * Get an authorization string for the uid by having the mount_nfs sitting 967 * on this mount point porpous out of the kernel and do it. 968 */ 969 int 970 nfs_getauth(nmp, rep, cred, auth_str, auth_len, verf_str, verf_len, key) 971 register struct nfsmount *nmp; 972 struct nfsreq *rep; 973 struct ucred *cred; 974 char **auth_str; 975 int *auth_len; 976 char *verf_str; 977 int *verf_len; 978 NFSKERBKEY_T key; /* return session key */ 979 { 980 int error = 0; 981 982 while ((nmp->nm_iflag & NFSMNT_WAITAUTH) == 0) { 983 nmp->nm_iflag |= NFSMNT_WANTAUTH; 984 (void) tsleep((caddr_t)&nmp->nm_authtype, PSOCK, 985 "nfsauth1", 2 * hz); 986 error = nfs_sigintr(nmp, rep, rep->r_procp); 987 if (error) { 988 nmp->nm_iflag &= ~NFSMNT_WANTAUTH; 989 return (error); 990 } 991 } 992 nmp->nm_iflag &= ~(NFSMNT_WAITAUTH | NFSMNT_WANTAUTH); 993 nmp->nm_authstr = *auth_str = (char *)malloc(RPCAUTH_MAXSIZ, M_TEMP, M_WAITOK); 994 nmp->nm_authlen = RPCAUTH_MAXSIZ; 995 nmp->nm_verfstr = verf_str; 996 nmp->nm_verflen = *verf_len; 997 nmp->nm_authuid = cred->cr_uid; 998 wakeup((caddr_t)&nmp->nm_authstr); 999 1000 /* 1001 * And wait for mount_nfs to do its stuff. 1002 */ 1003 while ((nmp->nm_iflag & NFSMNT_HASAUTH) == 0 && error == 0) { 1004 (void) tsleep((caddr_t)&nmp->nm_authlen, PSOCK, 1005 "nfsauth2", 2 * hz); 1006 error = nfs_sigintr(nmp, rep, rep->r_procp); 1007 } 1008 if (nmp->nm_iflag & NFSMNT_AUTHERR) { 1009 nmp->nm_iflag &= ~NFSMNT_AUTHERR; 1010 error = EAUTH; 1011 } 1012 if (error) 1013 free((caddr_t)*auth_str, M_TEMP); 1014 else { 1015 *auth_len = nmp->nm_authlen; 1016 *verf_len = nmp->nm_verflen; 1017 memcpy((caddr_t)key, (caddr_t)nmp->nm_key, sizeof (key)); 1018 } 1019 nmp->nm_iflag &= ~NFSMNT_HASAUTH; 1020 nmp->nm_iflag |= NFSMNT_WAITAUTH; 1021 if (nmp->nm_iflag & NFSMNT_WANTAUTH) { 1022 nmp->nm_iflag &= ~NFSMNT_WANTAUTH; 1023 wakeup((caddr_t)&nmp->nm_authtype); 1024 } 1025 return (error); 1026 } 1027 1028 /* 1029 * Get a nickname authenticator and verifier. 1030 */ 1031 int 1032 nfs_getnickauth(nmp, cred, auth_str, auth_len, verf_str, verf_len) 1033 struct nfsmount *nmp; 1034 struct ucred *cred; 1035 char **auth_str; 1036 int *auth_len; 1037 char *verf_str; 1038 int verf_len; 1039 { 1040 register struct nfsuid *nuidp; 1041 register u_int32_t *nickp, *verfp; 1042 struct timeval ktvin, ktvout; 1043 1044 #ifdef DIAGNOSTIC 1045 if (verf_len < (4 * NFSX_UNSIGNED)) 1046 panic("nfs_getnickauth verf too small"); 1047 #endif 1048 for (nuidp = NMUIDHASH(nmp, cred->cr_uid)->lh_first; 1049 nuidp != 0; nuidp = nuidp->nu_hash.le_next) { 1050 if (nuidp->nu_cr.cr_uid == cred->cr_uid) 1051 break; 1052 } 1053 if (!nuidp || nuidp->nu_expire < time.tv_sec) 1054 return (EACCES); 1055 1056 /* 1057 * Move to the end of the lru list (end of lru == most recently used). 1058 */ 1059 TAILQ_REMOVE(&nmp->nm_uidlruhead, nuidp, nu_lru); 1060 TAILQ_INSERT_TAIL(&nmp->nm_uidlruhead, nuidp, nu_lru); 1061 1062 nickp = (u_int32_t *)malloc(2 * NFSX_UNSIGNED, M_TEMP, M_WAITOK); 1063 *nickp++ = txdr_unsigned(RPCAKN_NICKNAME); 1064 *nickp = txdr_unsigned(nuidp->nu_nickname); 1065 *auth_str = (char *)nickp; 1066 *auth_len = 2 * NFSX_UNSIGNED; 1067 1068 /* 1069 * Now we must encrypt the verifier and package it up. 1070 */ 1071 verfp = (u_int32_t *)verf_str; 1072 *verfp++ = txdr_unsigned(RPCAKN_NICKNAME); 1073 if (time.tv_sec > nuidp->nu_timestamp.tv_sec || 1074 (time.tv_sec == nuidp->nu_timestamp.tv_sec && 1075 time.tv_usec > nuidp->nu_timestamp.tv_usec)) 1076 nuidp->nu_timestamp = time; 1077 else 1078 nuidp->nu_timestamp.tv_usec++; 1079 ktvin.tv_sec = txdr_unsigned(nuidp->nu_timestamp.tv_sec); 1080 ktvin.tv_usec = txdr_unsigned(nuidp->nu_timestamp.tv_usec); 1081 1082 /* 1083 * Now encrypt the timestamp verifier in ecb mode using the session 1084 * key. 1085 */ 1086 #ifdef NFSKERB 1087 XXX 1088 #endif 1089 1090 *verfp++ = ktvout.tv_sec; 1091 *verfp++ = ktvout.tv_usec; 1092 *verfp = 0; 1093 return (0); 1094 } 1095 1096 /* 1097 * Save the current nickname in a hash list entry on the mount point. 1098 */ 1099 int 1100 nfs_savenickauth(nmp, cred, len, key, mdp, dposp, mrep) 1101 register struct nfsmount *nmp; 1102 struct ucred *cred; 1103 int len; 1104 NFSKERBKEY_T key; 1105 struct mbuf **mdp; 1106 char **dposp; 1107 struct mbuf *mrep; 1108 { 1109 register struct nfsuid *nuidp; 1110 register u_int32_t *tl; 1111 register int32_t t1; 1112 struct mbuf *md = *mdp; 1113 struct timeval ktvin, ktvout; 1114 u_int32_t nick; 1115 char *dpos = *dposp, *cp2; 1116 int deltasec, error = 0; 1117 1118 if (len == (3 * NFSX_UNSIGNED)) { 1119 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED); 1120 ktvin.tv_sec = *tl++; 1121 ktvin.tv_usec = *tl++; 1122 nick = fxdr_unsigned(u_int32_t, *tl); 1123 1124 /* 1125 * Decrypt the timestamp in ecb mode. 1126 */ 1127 #ifdef NFSKERB 1128 XXX 1129 #endif 1130 ktvout.tv_sec = fxdr_unsigned(long, ktvout.tv_sec); 1131 ktvout.tv_usec = fxdr_unsigned(long, ktvout.tv_usec); 1132 deltasec = time.tv_sec - ktvout.tv_sec; 1133 if (deltasec < 0) 1134 deltasec = -deltasec; 1135 /* 1136 * If ok, add it to the hash list for the mount point. 1137 */ 1138 if (deltasec <= NFS_KERBCLOCKSKEW) { 1139 if (nmp->nm_numuids < nuidhash_max) { 1140 nmp->nm_numuids++; 1141 nuidp = (struct nfsuid *) 1142 malloc(sizeof (struct nfsuid), M_NFSUID, 1143 M_WAITOK); 1144 } else { 1145 nuidp = nmp->nm_uidlruhead.tqh_first; 1146 LIST_REMOVE(nuidp, nu_hash); 1147 TAILQ_REMOVE(&nmp->nm_uidlruhead, nuidp, 1148 nu_lru); 1149 } 1150 nuidp->nu_flag = 0; 1151 nuidp->nu_cr.cr_uid = cred->cr_uid; 1152 nuidp->nu_expire = time.tv_sec + NFS_KERBTTL; 1153 nuidp->nu_timestamp = ktvout; 1154 nuidp->nu_nickname = nick; 1155 memcpy(nuidp->nu_key, key, sizeof (key)); 1156 TAILQ_INSERT_TAIL(&nmp->nm_uidlruhead, nuidp, 1157 nu_lru); 1158 LIST_INSERT_HEAD(NMUIDHASH(nmp, cred->cr_uid), 1159 nuidp, nu_hash); 1160 } 1161 } else 1162 nfsm_adv(nfsm_rndup(len)); 1163 nfsmout: 1164 *mdp = md; 1165 *dposp = dpos; 1166 return (error); 1167 } 1168 #endif /* NFS */ 1169