1 /* 2 * Copyright (c) 1989, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Rick Macklem at The University of Guelph. 7 * 8 * %sccs.include.redist.c% 9 * 10 * @(#)nfs_syscalls.c 8.4 (Berkeley) 08/18/94 11 */ 12 13 #include <sys/param.h> 14 #include <sys/systm.h> 15 #include <sys/kernel.h> 16 #include <sys/file.h> 17 #include <sys/stat.h> 18 #include <sys/vnode.h> 19 #include <sys/mount.h> 20 #include <sys/proc.h> 21 #include <sys/uio.h> 22 #include <sys/malloc.h> 23 #include <sys/buf.h> 24 #include <sys/mbuf.h> 25 #include <sys/socket.h> 26 #include <sys/socketvar.h> 27 #include <sys/domain.h> 28 #include <sys/protosw.h> 29 #include <sys/namei.h> 30 #include <sys/syslog.h> 31 32 #include <netinet/in.h> 33 #include <netinet/tcp.h> 34 #ifdef ISO 35 #include <netiso/iso.h> 36 #endif 37 #include <nfs/rpcv2.h> 38 #include <nfs/nfsv2.h> 39 #include <nfs/nfs.h> 40 #include <nfs/nfsrvcache.h> 41 #include <nfs/nfsmount.h> 42 #include <nfs/nfsnode.h> 43 #include <nfs/nqnfs.h> 44 #include <nfs/nfsrtt.h> 45 46 /* Global defs. */ 47 extern u_long nfs_prog, nfs_vers; 48 extern int (*nfsrv_procs[NFS_NPROCS])(); 49 extern struct proc *nfs_iodwant[NFS_MAXASYNCDAEMON]; 50 extern int nfs_numasync; 51 extern time_t nqnfsstarttime; 52 extern int nqsrv_writeslack; 53 extern int nfsrtton; 54 struct nfssvc_sock *nfs_udpsock, *nfs_cltpsock; 55 int nuidhash_max = NFS_MAXUIDHASH; 56 static int nfs_numnfsd = 0; 57 int nfsd_waiting = 0; 58 static int notstarted = 1; 59 static int modify_flag = 0; 60 static struct nfsdrt nfsdrt; 61 void nfsrv_cleancache(), nfsrv_rcv(), nfsrv_wakenfsd(), nfs_sndunlock(); 62 static void nfsd_rt(); 63 void nfsrv_slpderef(), nfsrv_init(); 64 65 #define TRUE 1 66 #define FALSE 0 67 68 static int nfs_asyncdaemon[NFS_MAXASYNCDAEMON]; 69 /* 70 * NFS server system calls 71 * getfh() lives here too, but maybe should move to kern/vfs_syscalls.c 72 */ 73 74 /* 75 * Get file handle system call 76 */ 77 struct getfh_args { 78 char *fname; 79 fhandle_t *fhp; 80 }; 81 getfh(p, uap, retval) 82 struct proc *p; 83 register struct getfh_args *uap; 84 int *retval; 85 { 86 register struct vnode *vp; 87 fhandle_t fh; 88 int error; 89 struct nameidata nd; 90 91 /* 92 * Must be super user 93 */ 94 if (error = suser(p->p_ucred, &p->p_acflag)) 95 return (error); 96 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE, uap->fname, p); 97 if (error = namei(&nd)) 98 return (error); 99 vp = nd.ni_vp; 100 bzero((caddr_t)&fh, sizeof(fh)); 101 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid; 102 error = VFS_VPTOFH(vp, &fh.fh_fid); 103 vput(vp); 104 if (error) 105 return (error); 106 error = copyout((caddr_t)&fh, (caddr_t)uap->fhp, sizeof (fh)); 107 return (error); 108 } 109 110 /* 111 * Nfs server psuedo system call for the nfsd's 112 * Based on the flag value it either: 113 * - adds a socket to the selection list 114 * - remains in the kernel as an nfsd 115 * - remains in the kernel as an nfsiod 116 */ 117 struct nfssvc_args { 118 int flag; 119 caddr_t argp; 120 }; 121 nfssvc(p, uap, retval) 122 struct proc *p; 123 register struct nfssvc_args *uap; 124 int *retval; 125 { 126 struct nameidata nd; 127 struct file *fp; 128 struct mbuf *nam; 129 struct nfsd_args nfsdarg; 130 struct nfsd_srvargs nfsd_srvargs, *nsd = &nfsd_srvargs; 131 struct nfsd_cargs ncd; 132 struct nfsd *nfsd; 133 struct nfssvc_sock *slp; 134 struct nfsuid *nuidp, **nuh; 135 struct nfsmount *nmp; 136 int error; 137 138 /* 139 * Must be super user 140 */ 141 if (error = suser(p->p_ucred, &p->p_acflag)) 142 return (error); 143 while (nfssvc_sockhead_flag & SLP_INIT) { 144 nfssvc_sockhead_flag |= SLP_WANTINIT; 145 (void) tsleep((caddr_t)&nfssvc_sockhead, PSOCK, "nfsd init", 0); 146 } 147 if (uap->flag & NFSSVC_BIOD) 148 error = nfssvc_iod(p); 149 else if (uap->flag & NFSSVC_MNTD) { 150 if (error = copyin(uap->argp, (caddr_t)&ncd, sizeof (ncd))) 151 return (error); 152 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE, 153 ncd.ncd_dirp, p); 154 if (error = namei(&nd)) 155 return (error); 156 if ((nd.ni_vp->v_flag & VROOT) == 0) 157 error = EINVAL; 158 nmp = VFSTONFS(nd.ni_vp->v_mount); 159 vput(nd.ni_vp); 160 if (error) 161 return (error); 162 if ((nmp->nm_flag & NFSMNT_MNTD) && 163 (uap->flag & NFSSVC_GOTAUTH) == 0) 164 return (0); 165 nmp->nm_flag |= NFSMNT_MNTD; 166 error = nqnfs_clientd(nmp, p->p_ucred, &ncd, uap->flag, 167 uap->argp, p); 168 } else if (uap->flag & NFSSVC_ADDSOCK) { 169 if (error = copyin(uap->argp, (caddr_t)&nfsdarg, 170 sizeof(nfsdarg))) 171 return (error); 172 if (error = getsock(p->p_fd, nfsdarg.sock, &fp)) 173 return (error); 174 /* 175 * Get the client address for connected sockets. 176 */ 177 if (nfsdarg.name == NULL || nfsdarg.namelen == 0) 178 nam = (struct mbuf *)0; 179 else if (error = sockargs(&nam, nfsdarg.name, nfsdarg.namelen, 180 MT_SONAME)) 181 return (error); 182 error = nfssvc_addsock(fp, nam); 183 } else { 184 if (error = copyin(uap->argp, (caddr_t)nsd, sizeof (*nsd))) 185 return (error); 186 if ((uap->flag & NFSSVC_AUTHIN) && (nfsd = nsd->nsd_nfsd) && 187 (nfsd->nd_slp->ns_flag & SLP_VALID)) { 188 slp = nfsd->nd_slp; 189 190 /* 191 * First check to see if another nfsd has already 192 * added this credential. 193 */ 194 for (nuidp = NUIDHASH(slp, nsd->nsd_uid)->lh_first; 195 nuidp != 0; nuidp = nuidp->nu_hash.le_next) { 196 if (nuidp->nu_uid == nsd->nsd_uid) 197 break; 198 } 199 if (nuidp == 0) { 200 /* 201 * Nope, so we will. 202 */ 203 if (slp->ns_numuids < nuidhash_max) { 204 slp->ns_numuids++; 205 nuidp = (struct nfsuid *) 206 malloc(sizeof (struct nfsuid), M_NFSUID, 207 M_WAITOK); 208 } else 209 nuidp = (struct nfsuid *)0; 210 if ((slp->ns_flag & SLP_VALID) == 0) { 211 if (nuidp) 212 free((caddr_t)nuidp, M_NFSUID); 213 } else { 214 if (nuidp == (struct nfsuid *)0) { 215 nuidp = slp->ns_uidlruhead.tqh_first; 216 LIST_REMOVE(nuidp, nu_hash); 217 TAILQ_REMOVE(&slp->ns_uidlruhead, nuidp, 218 nu_lru); 219 } 220 nuidp->nu_cr = nsd->nsd_cr; 221 if (nuidp->nu_cr.cr_ngroups > NGROUPS) 222 nuidp->nu_cr.cr_ngroups = NGROUPS; 223 nuidp->nu_cr.cr_ref = 1; 224 nuidp->nu_uid = nsd->nsd_uid; 225 TAILQ_INSERT_TAIL(&slp->ns_uidlruhead, nuidp, 226 nu_lru); 227 LIST_INSERT_HEAD(NUIDHASH(slp, nsd->nsd_uid), 228 nuidp, nu_hash); 229 } 230 } 231 } 232 if ((uap->flag & NFSSVC_AUTHINFAIL) && (nfsd = nsd->nsd_nfsd)) 233 nfsd->nd_flag |= NFSD_AUTHFAIL; 234 error = nfssvc_nfsd(nsd, uap->argp, p); 235 } 236 if (error == EINTR || error == ERESTART) 237 error = 0; 238 return (error); 239 } 240 241 /* 242 * Adds a socket to the list for servicing by nfsds. 243 */ 244 nfssvc_addsock(fp, mynam) 245 struct file *fp; 246 struct mbuf *mynam; 247 { 248 register struct mbuf *m; 249 register int siz; 250 register struct nfssvc_sock *slp; 251 register struct socket *so; 252 struct nfssvc_sock *tslp; 253 int error, s; 254 255 so = (struct socket *)fp->f_data; 256 tslp = (struct nfssvc_sock *)0; 257 /* 258 * Add it to the list, as required. 259 */ 260 if (so->so_proto->pr_protocol == IPPROTO_UDP) { 261 tslp = nfs_udpsock; 262 if (tslp->ns_flag & SLP_VALID) { 263 m_freem(mynam); 264 return (EPERM); 265 } 266 #ifdef ISO 267 } else if (so->so_proto->pr_protocol == ISOPROTO_CLTP) { 268 tslp = nfs_cltpsock; 269 if (tslp->ns_flag & SLP_VALID) { 270 m_freem(mynam); 271 return (EPERM); 272 } 273 #endif /* ISO */ 274 } 275 if (so->so_type == SOCK_STREAM) 276 siz = NFS_MAXPACKET + sizeof (u_long); 277 else 278 siz = NFS_MAXPACKET; 279 if (error = soreserve(so, siz, siz)) { 280 m_freem(mynam); 281 return (error); 282 } 283 284 /* 285 * Set protocol specific options { for now TCP only } and 286 * reserve some space. For datagram sockets, this can get called 287 * repeatedly for the same socket, but that isn't harmful. 288 */ 289 if (so->so_type == SOCK_STREAM) { 290 MGET(m, M_WAIT, MT_SOOPTS); 291 *mtod(m, int *) = 1; 292 m->m_len = sizeof(int); 293 sosetopt(so, SOL_SOCKET, SO_KEEPALIVE, m); 294 } 295 if (so->so_proto->pr_domain->dom_family == AF_INET && 296 so->so_proto->pr_protocol == IPPROTO_TCP) { 297 MGET(m, M_WAIT, MT_SOOPTS); 298 *mtod(m, int *) = 1; 299 m->m_len = sizeof(int); 300 sosetopt(so, IPPROTO_TCP, TCP_NODELAY, m); 301 } 302 so->so_rcv.sb_flags &= ~SB_NOINTR; 303 so->so_rcv.sb_timeo = 0; 304 so->so_snd.sb_flags &= ~SB_NOINTR; 305 so->so_snd.sb_timeo = 0; 306 if (tslp) 307 slp = tslp; 308 else { 309 slp = (struct nfssvc_sock *) 310 malloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK); 311 bzero((caddr_t)slp, sizeof (struct nfssvc_sock)); 312 slp->ns_uidhashtbl = 313 hashinit(NUIDHASHSIZ, M_NFSSVC, &slp->ns_uidhash); 314 TAILQ_INIT(&slp->ns_uidlruhead); 315 TAILQ_INSERT_TAIL(&nfssvc_sockhead, slp, ns_chain); 316 } 317 slp->ns_so = so; 318 slp->ns_nam = mynam; 319 fp->f_count++; 320 slp->ns_fp = fp; 321 s = splnet(); 322 so->so_upcallarg = (caddr_t)slp; 323 so->so_upcall = nfsrv_rcv; 324 slp->ns_flag = (SLP_VALID | SLP_NEEDQ); 325 nfsrv_wakenfsd(slp); 326 splx(s); 327 return (0); 328 } 329 330 /* 331 * Called by nfssvc() for nfsds. Just loops around servicing rpc requests 332 * until it is killed by a signal. 333 */ 334 nfssvc_nfsd(nsd, argp, p) 335 struct nfsd_srvargs *nsd; 336 caddr_t argp; 337 struct proc *p; 338 { 339 register struct mbuf *m, *nam2; 340 register int siz; 341 register struct nfssvc_sock *slp; 342 register struct socket *so; 343 register int *solockp; 344 struct nfsd *nd = nsd->nsd_nfsd; 345 struct mbuf *mreq, *nam; 346 struct timeval starttime; 347 struct nfsuid *uidp; 348 int error, cacherep, s; 349 int sotype; 350 351 s = splnet(); 352 if (nd == (struct nfsd *)0) { 353 nsd->nsd_nfsd = nd = (struct nfsd *) 354 malloc(sizeof (struct nfsd), M_NFSD, M_WAITOK); 355 bzero((caddr_t)nd, sizeof (struct nfsd)); 356 nd->nd_procp = p; 357 nd->nd_cr.cr_ref = 1; 358 TAILQ_INSERT_TAIL(&nfsd_head, nd, nd_chain); 359 nd->nd_nqlflag = NQL_NOVAL; 360 nfs_numnfsd++; 361 } 362 /* 363 * Loop getting rpc requests until SIGKILL. 364 */ 365 for (;;) { 366 if ((nd->nd_flag & NFSD_REQINPROG) == 0) { 367 while (nd->nd_slp == (struct nfssvc_sock *)0 && 368 (nfsd_head_flag & NFSD_CHECKSLP) == 0) { 369 nd->nd_flag |= NFSD_WAITING; 370 nfsd_waiting++; 371 error = tsleep((caddr_t)nd, PSOCK | PCATCH, "nfsd", 0); 372 nfsd_waiting--; 373 if (error) 374 goto done; 375 } 376 if (nd->nd_slp == (struct nfssvc_sock *)0 && 377 (nfsd_head_flag & NFSD_CHECKSLP) != 0) { 378 for (slp = nfssvc_sockhead.tqh_first; slp != 0; 379 slp = slp->ns_chain.tqe_next) { 380 if ((slp->ns_flag & (SLP_VALID | SLP_DOREC)) 381 == (SLP_VALID | SLP_DOREC)) { 382 slp->ns_flag &= ~SLP_DOREC; 383 slp->ns_sref++; 384 nd->nd_slp = slp; 385 break; 386 } 387 } 388 if (slp == 0) 389 nfsd_head_flag &= ~NFSD_CHECKSLP; 390 } 391 if ((slp = nd->nd_slp) == (struct nfssvc_sock *)0) 392 continue; 393 if (slp->ns_flag & SLP_VALID) { 394 if (slp->ns_flag & SLP_DISCONN) 395 nfsrv_zapsock(slp); 396 else if (slp->ns_flag & SLP_NEEDQ) { 397 slp->ns_flag &= ~SLP_NEEDQ; 398 (void) nfs_sndlock(&slp->ns_solock, 399 (struct nfsreq *)0); 400 nfsrv_rcv(slp->ns_so, (caddr_t)slp, 401 M_WAIT); 402 nfs_sndunlock(&slp->ns_solock); 403 } 404 error = nfsrv_dorec(slp, nd); 405 nd->nd_flag |= NFSD_REQINPROG; 406 } 407 } else { 408 error = 0; 409 slp = nd->nd_slp; 410 } 411 if (error || (slp->ns_flag & SLP_VALID) == 0) { 412 nd->nd_slp = (struct nfssvc_sock *)0; 413 nd->nd_flag &= ~NFSD_REQINPROG; 414 nfsrv_slpderef(slp); 415 continue; 416 } 417 splx(s); 418 so = slp->ns_so; 419 sotype = so->so_type; 420 starttime = time; 421 if (so->so_proto->pr_flags & PR_CONNREQUIRED) 422 solockp = &slp->ns_solock; 423 else 424 solockp = (int *)0; 425 /* 426 * nam == nam2 for connectionless protocols such as UDP 427 * nam2 == NULL for connection based protocols to disable 428 * recent request caching. 429 */ 430 if (nam2 = nd->nd_nam) { 431 nam = nam2; 432 cacherep = RC_CHECKIT; 433 } else { 434 nam = slp->ns_nam; 435 cacherep = RC_DOIT; 436 } 437 438 /* 439 * Check to see if authorization is needed. 440 */ 441 if (nd->nd_flag & NFSD_NEEDAUTH) { 442 static int logauth = 0; 443 444 nd->nd_flag &= ~NFSD_NEEDAUTH; 445 /* 446 * Check for a mapping already installed. 447 */ 448 for (uidp = NUIDHASH(slp, nd->nd_cr.cr_uid)->lh_first; 449 uidp != 0; uidp = uidp->nu_hash.le_next) { 450 if (uidp->nu_uid == nd->nd_cr.cr_uid) 451 break; 452 } 453 if (uidp == 0) { 454 nsd->nsd_uid = nd->nd_cr.cr_uid; 455 if (nam2 && logauth++ == 0) 456 log(LOG_WARNING, "Kerberized NFS using UDP\n"); 457 nsd->nsd_haddr = 458 mtod(nam, struct sockaddr_in *)->sin_addr.s_addr; 459 nsd->nsd_authlen = nd->nd_authlen; 460 if (copyout(nd->nd_authstr, nsd->nsd_authstr, 461 nd->nd_authlen) == 0 && 462 copyout((caddr_t)nsd, argp, sizeof (*nsd)) == 0) 463 return (ENEEDAUTH); 464 cacherep = RC_DROPIT; 465 } 466 } 467 if (cacherep == RC_CHECKIT) 468 cacherep = nfsrv_getcache(nam2, nd, &mreq); 469 470 /* 471 * Check for just starting up for NQNFS and send 472 * fake "try again later" replies to the NQNFS clients. 473 */ 474 if (notstarted && nqnfsstarttime <= time.tv_sec) { 475 if (modify_flag) { 476 nqnfsstarttime = time.tv_sec + nqsrv_writeslack; 477 modify_flag = 0; 478 } else 479 notstarted = 0; 480 } 481 if (notstarted) { 482 if (nd->nd_nqlflag == NQL_NOVAL) 483 cacherep = RC_DROPIT; 484 else if (nd->nd_procnum != NFSPROC_WRITE) { 485 nd->nd_procnum = NFSPROC_NOOP; 486 nd->nd_repstat = NQNFS_TRYLATER; 487 cacherep = RC_DOIT; 488 } else 489 modify_flag = 1; 490 } else if (nd->nd_flag & NFSD_AUTHFAIL) { 491 nd->nd_flag &= ~NFSD_AUTHFAIL; 492 nd->nd_procnum = NFSPROC_NOOP; 493 nd->nd_repstat = NQNFS_AUTHERR; 494 cacherep = RC_DOIT; 495 } 496 497 switch (cacherep) { 498 case RC_DOIT: 499 error = (*(nfsrv_procs[nd->nd_procnum]))(nd, 500 nd->nd_mrep, nd->nd_md, nd->nd_dpos, &nd->nd_cr, 501 nam, &mreq); 502 if (nd->nd_cr.cr_ref != 1) { 503 printf("nfssvc cref=%d\n", nd->nd_cr.cr_ref); 504 panic("nfssvc cref"); 505 } 506 if (error) { 507 if (nd->nd_procnum != NQNFSPROC_VACATED) 508 nfsstats.srv_errs++; 509 if (nam2) { 510 nfsrv_updatecache(nam2, nd, FALSE, mreq); 511 m_freem(nam2); 512 } 513 break; 514 } 515 nfsstats.srvrpccnt[nd->nd_procnum]++; 516 if (nam2) 517 nfsrv_updatecache(nam2, nd, TRUE, mreq); 518 nd->nd_mrep = (struct mbuf *)0; 519 case RC_REPLY: 520 m = mreq; 521 siz = 0; 522 while (m) { 523 siz += m->m_len; 524 m = m->m_next; 525 } 526 if (siz <= 0 || siz > NFS_MAXPACKET) { 527 printf("mbuf siz=%d\n",siz); 528 panic("Bad nfs svc reply"); 529 } 530 m = mreq; 531 m->m_pkthdr.len = siz; 532 m->m_pkthdr.rcvif = (struct ifnet *)0; 533 /* 534 * For stream protocols, prepend a Sun RPC 535 * Record Mark. 536 */ 537 if (sotype == SOCK_STREAM) { 538 M_PREPEND(m, NFSX_UNSIGNED, M_WAIT); 539 *mtod(m, u_long *) = htonl(0x80000000 | siz); 540 } 541 if (solockp) 542 (void) nfs_sndlock(solockp, (struct nfsreq *)0); 543 if (slp->ns_flag & SLP_VALID) 544 error = nfs_send(so, nam2, m, (struct nfsreq *)0); 545 else { 546 error = EPIPE; 547 m_freem(m); 548 } 549 if (nfsrtton) 550 nfsd_rt(&starttime, sotype, nd, nam, cacherep); 551 if (nam2) 552 MFREE(nam2, m); 553 if (nd->nd_mrep) 554 m_freem(nd->nd_mrep); 555 if (error == EPIPE) 556 nfsrv_zapsock(slp); 557 if (solockp) 558 nfs_sndunlock(solockp); 559 if (error == EINTR || error == ERESTART) { 560 nfsrv_slpderef(slp); 561 s = splnet(); 562 goto done; 563 } 564 break; 565 case RC_DROPIT: 566 if (nfsrtton) 567 nfsd_rt(&starttime, sotype, nd, nam, cacherep); 568 m_freem(nd->nd_mrep); 569 m_freem(nam2); 570 break; 571 }; 572 s = splnet(); 573 if (nfsrv_dorec(slp, nd)) { 574 nd->nd_flag &= ~NFSD_REQINPROG; 575 nd->nd_slp = (struct nfssvc_sock *)0; 576 nfsrv_slpderef(slp); 577 } 578 } 579 done: 580 TAILQ_REMOVE(&nfsd_head, nd, nd_chain); 581 splx(s); 582 free((caddr_t)nd, M_NFSD); 583 nsd->nsd_nfsd = (struct nfsd *)0; 584 if (--nfs_numnfsd == 0) 585 nfsrv_init(TRUE); /* Reinitialize everything */ 586 return (error); 587 } 588 589 /* 590 * Asynchronous I/O daemons for client nfs. 591 * They do read-ahead and write-behind operations on the block I/O cache. 592 * Never returns unless it fails or gets killed. 593 */ 594 nfssvc_iod(p) 595 struct proc *p; 596 { 597 register struct buf *bp; 598 register int i, myiod; 599 int error = 0; 600 601 /* 602 * Assign my position or return error if too many already running 603 */ 604 myiod = -1; 605 for (i = 0; i < NFS_MAXASYNCDAEMON; i++) 606 if (nfs_asyncdaemon[i] == 0) { 607 nfs_asyncdaemon[i]++; 608 myiod = i; 609 break; 610 } 611 if (myiod == -1) 612 return (EBUSY); 613 nfs_numasync++; 614 /* 615 * Just loop around doin our stuff until SIGKILL 616 */ 617 for (;;) { 618 while (nfs_bufq.tqh_first == NULL && error == 0) { 619 nfs_iodwant[myiod] = p; 620 error = tsleep((caddr_t)&nfs_iodwant[myiod], 621 PWAIT | PCATCH, "nfsidl", 0); 622 } 623 while ((bp = nfs_bufq.tqh_first) != NULL) { 624 /* Take one off the front of the list */ 625 TAILQ_REMOVE(&nfs_bufq, bp, b_freelist); 626 if (bp->b_flags & B_READ) 627 (void) nfs_doio(bp, bp->b_rcred, (struct proc *)0); 628 else 629 (void) nfs_doio(bp, bp->b_wcred, (struct proc *)0); 630 } 631 if (error) { 632 nfs_asyncdaemon[myiod] = 0; 633 nfs_numasync--; 634 return (error); 635 } 636 } 637 } 638 639 /* 640 * Shut down a socket associated with an nfssvc_sock structure. 641 * Should be called with the send lock set, if required. 642 * The trick here is to increment the sref at the start, so that the nfsds 643 * will stop using it and clear ns_flag at the end so that it will not be 644 * reassigned during cleanup. 645 */ 646 nfsrv_zapsock(slp) 647 register struct nfssvc_sock *slp; 648 { 649 register struct nfsuid *nuidp, *nnuidp; 650 register int i; 651 struct socket *so; 652 struct file *fp; 653 struct mbuf *m; 654 655 slp->ns_flag &= ~SLP_ALLFLAGS; 656 if (fp = slp->ns_fp) { 657 slp->ns_fp = (struct file *)0; 658 so = slp->ns_so; 659 so->so_upcall = NULL; 660 soshutdown(so, 2); 661 closef(fp, (struct proc *)0); 662 if (slp->ns_nam) 663 MFREE(slp->ns_nam, m); 664 m_freem(slp->ns_raw); 665 m_freem(slp->ns_rec); 666 for (nuidp = slp->ns_uidlruhead.tqh_first; nuidp != 0; 667 nuidp = nnuidp) { 668 nnuidp = nuidp->nu_lru.tqe_next; 669 LIST_REMOVE(nuidp, nu_hash); 670 TAILQ_REMOVE(&slp->ns_uidlruhead, nuidp, nu_lru); 671 free((caddr_t)nuidp, M_NFSUID); 672 } 673 } 674 } 675 676 /* 677 * Get an authorization string for the uid by having the mount_nfs sitting 678 * on this mount point porpous out of the kernel and do it. 679 */ 680 nfs_getauth(nmp, rep, cred, auth_type, auth_str, auth_len) 681 register struct nfsmount *nmp; 682 struct nfsreq *rep; 683 struct ucred *cred; 684 int *auth_type; 685 char **auth_str; 686 int *auth_len; 687 { 688 int error = 0; 689 690 while ((nmp->nm_flag & NFSMNT_WAITAUTH) == 0) { 691 nmp->nm_flag |= NFSMNT_WANTAUTH; 692 (void) tsleep((caddr_t)&nmp->nm_authtype, PSOCK, 693 "nfsauth1", 2 * hz); 694 if (error = nfs_sigintr(nmp, rep, rep->r_procp)) { 695 nmp->nm_flag &= ~NFSMNT_WANTAUTH; 696 return (error); 697 } 698 } 699 nmp->nm_flag &= ~(NFSMNT_WAITAUTH | NFSMNT_WANTAUTH); 700 nmp->nm_authstr = *auth_str = (char *)malloc(RPCAUTH_MAXSIZ, M_TEMP, M_WAITOK); 701 nmp->nm_authuid = cred->cr_uid; 702 wakeup((caddr_t)&nmp->nm_authstr); 703 704 /* 705 * And wait for mount_nfs to do its stuff. 706 */ 707 while ((nmp->nm_flag & NFSMNT_HASAUTH) == 0 && error == 0) { 708 (void) tsleep((caddr_t)&nmp->nm_authlen, PSOCK, 709 "nfsauth2", 2 * hz); 710 error = nfs_sigintr(nmp, rep, rep->r_procp); 711 } 712 if (nmp->nm_flag & NFSMNT_AUTHERR) { 713 nmp->nm_flag &= ~NFSMNT_AUTHERR; 714 error = EAUTH; 715 } 716 if (error) 717 free((caddr_t)*auth_str, M_TEMP); 718 else { 719 *auth_type = nmp->nm_authtype; 720 *auth_len = nmp->nm_authlen; 721 } 722 nmp->nm_flag &= ~NFSMNT_HASAUTH; 723 nmp->nm_flag |= NFSMNT_WAITAUTH; 724 if (nmp->nm_flag & NFSMNT_WANTAUTH) { 725 nmp->nm_flag &= ~NFSMNT_WANTAUTH; 726 wakeup((caddr_t)&nmp->nm_authtype); 727 } 728 return (error); 729 } 730 731 /* 732 * Derefence a server socket structure. If it has no more references and 733 * is no longer valid, you can throw it away. 734 */ 735 void 736 nfsrv_slpderef(slp) 737 register struct nfssvc_sock *slp; 738 { 739 if (--(slp->ns_sref) == 0 && (slp->ns_flag & SLP_VALID) == 0) { 740 TAILQ_REMOVE(&nfssvc_sockhead, slp, ns_chain); 741 free((caddr_t)slp, M_NFSSVC); 742 } 743 } 744 745 /* 746 * Initialize the data structures for the server. 747 * Handshake with any new nfsds starting up to avoid any chance of 748 * corruption. 749 */ 750 void 751 nfsrv_init(terminating) 752 int terminating; 753 { 754 register struct nfssvc_sock *slp, *nslp; 755 756 if (nfssvc_sockhead_flag & SLP_INIT) 757 panic("nfsd init"); 758 nfssvc_sockhead_flag |= SLP_INIT; 759 if (terminating) { 760 for (slp = nfssvc_sockhead.tqh_first; slp != 0; slp = nslp) { 761 nslp = slp->ns_chain.tqe_next; 762 if (slp->ns_flag & SLP_VALID) 763 nfsrv_zapsock(slp); 764 TAILQ_REMOVE(&nfssvc_sockhead, slp, ns_chain); 765 free((caddr_t)slp, M_NFSSVC); 766 } 767 nfsrv_cleancache(); /* And clear out server cache */ 768 } 769 770 TAILQ_INIT(&nfssvc_sockhead); 771 nfssvc_sockhead_flag &= ~SLP_INIT; 772 if (nfssvc_sockhead_flag & SLP_WANTINIT) { 773 nfssvc_sockhead_flag &= ~SLP_WANTINIT; 774 wakeup((caddr_t)&nfssvc_sockhead); 775 } 776 777 TAILQ_INIT(&nfsd_head); 778 nfsd_head_flag &= ~NFSD_CHECKSLP; 779 780 nfs_udpsock = (struct nfssvc_sock *) 781 malloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK); 782 bzero((caddr_t)nfs_udpsock, sizeof (struct nfssvc_sock)); 783 nfs_udpsock->ns_uidhashtbl = 784 hashinit(NUIDHASHSIZ, M_NFSSVC, &nfs_udpsock->ns_uidhash); 785 TAILQ_INIT(&nfs_udpsock->ns_uidlruhead); 786 TAILQ_INSERT_HEAD(&nfssvc_sockhead, nfs_udpsock, ns_chain); 787 788 nfs_cltpsock = (struct nfssvc_sock *) 789 malloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK); 790 bzero((caddr_t)nfs_cltpsock, sizeof (struct nfssvc_sock)); 791 nfs_cltpsock->ns_uidhashtbl = 792 hashinit(NUIDHASHSIZ, M_NFSSVC, &nfs_cltpsock->ns_uidhash); 793 TAILQ_INIT(&nfs_cltpsock->ns_uidlruhead); 794 TAILQ_INSERT_TAIL(&nfssvc_sockhead, nfs_cltpsock, ns_chain); 795 } 796 797 /* 798 * Add entries to the server monitor log. 799 */ 800 static void 801 nfsd_rt(startp, sotype, nd, nam, cacherep) 802 struct timeval *startp; 803 int sotype; 804 register struct nfsd *nd; 805 struct mbuf *nam; 806 int cacherep; 807 { 808 register struct drt *rt; 809 810 rt = &nfsdrt.drt[nfsdrt.pos]; 811 if (cacherep == RC_DOIT) 812 rt->flag = 0; 813 else if (cacherep == RC_REPLY) 814 rt->flag = DRT_CACHEREPLY; 815 else 816 rt->flag = DRT_CACHEDROP; 817 if (sotype == SOCK_STREAM) 818 rt->flag |= DRT_TCP; 819 if (nd->nd_nqlflag != NQL_NOVAL) 820 rt->flag |= DRT_NQNFS; 821 rt->proc = nd->nd_procnum; 822 if (mtod(nam, struct sockaddr *)->sa_family == AF_INET) 823 rt->ipadr = mtod(nam, struct sockaddr_in *)->sin_addr.s_addr; 824 else 825 rt->ipadr = INADDR_ANY; 826 rt->resptime = ((time.tv_sec - startp->tv_sec) * 1000000) + 827 (time.tv_usec - startp->tv_usec); 828 rt->tstamp = time; 829 nfsdrt.pos = (nfsdrt.pos + 1) % NFSRTTLOGSIZ; 830 } 831