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