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