nfs_nfsiod.c revision 58345
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 58345 2000-03-20 10:44:49Z phk $ 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/buf.h> 52#include <sys/mbuf.h> 53#include <sys/socket.h> 54#include <sys/socketvar.h> 55#include <sys/domain.h> 56#include <sys/protosw.h> 57#include <sys/namei.h> 58#include <vm/vm_zone.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 = getsock(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 return (error); 210 } 211 error = nfssvc_addsock(fp, nam, p); 212 } else { 213 error = copyin(uap->argp, (caddr_t)nsd, sizeof (*nsd)); 214 if (error) 215 return (error); 216 if ((uap->flag & NFSSVC_AUTHIN) && 217 ((nfsd = nsd->nsd_nfsd)) != NULL && 218 (nfsd->nfsd_slp->ns_flag & SLP_VALID)) { 219 slp = nfsd->nfsd_slp; 220 221 /* 222 * First check to see if another nfsd has already 223 * added this credential. 224 */ 225 for (nuidp = NUIDHASH(slp,nsd->nsd_cr.cr_uid)->lh_first; 226 nuidp != 0; nuidp = nuidp->nu_hash.le_next) { 227 if (nuidp->nu_cr.cr_uid == nsd->nsd_cr.cr_uid && 228 (!nfsd->nfsd_nd->nd_nam2 || 229 netaddr_match(NU_NETFAM(nuidp), 230 &nuidp->nu_haddr, nfsd->nfsd_nd->nd_nam2))) 231 break; 232 } 233 if (nuidp) { 234 nfsrv_setcred(&nuidp->nu_cr,&nfsd->nfsd_nd->nd_cr); 235 nfsd->nfsd_nd->nd_flag |= ND_KERBFULL; 236 } else { 237 /* 238 * Nope, so we will. 239 */ 240 if (slp->ns_numuids < nuidhash_max) { 241 slp->ns_numuids++; 242 nuidp = (struct nfsuid *) 243 malloc(sizeof (struct nfsuid), M_NFSUID, 244 M_WAITOK); 245 } else 246 nuidp = (struct nfsuid *)0; 247 if ((slp->ns_flag & SLP_VALID) == 0) { 248 if (nuidp) 249 free((caddr_t)nuidp, M_NFSUID); 250 } else { 251 if (nuidp == (struct nfsuid *)0) { 252 nuidp = slp->ns_uidlruhead.tqh_first; 253 LIST_REMOVE(nuidp, nu_hash); 254 TAILQ_REMOVE(&slp->ns_uidlruhead, nuidp, 255 nu_lru); 256 if (nuidp->nu_flag & NU_NAM) 257 FREE(nuidp->nu_nam, M_SONAME); 258 } 259 nuidp->nu_flag = 0; 260 nuidp->nu_cr = nsd->nsd_cr; 261 if (nuidp->nu_cr.cr_ngroups > NGROUPS) 262 nuidp->nu_cr.cr_ngroups = NGROUPS; 263 nuidp->nu_cr.cr_ref = 1; 264 nuidp->nu_timestamp = nsd->nsd_timestamp; 265 nuidp->nu_expire = time_second + nsd->nsd_ttl; 266 /* 267 * and save the session key in nu_key. 268 */ 269 bcopy(nsd->nsd_key, nuidp->nu_key, 270 sizeof (nsd->nsd_key)); 271 if (nfsd->nfsd_nd->nd_nam2) { 272 struct sockaddr_in *saddr; 273 274 saddr = (struct sockaddr_in *) 275 nfsd->nfsd_nd->nd_nam2; 276 switch (saddr->sin_family) { 277 case AF_INET: 278 nuidp->nu_flag |= NU_INETADDR; 279 nuidp->nu_inetaddr = 280 saddr->sin_addr.s_addr; 281 break; 282 case AF_ISO: 283 default: 284 nuidp->nu_flag |= NU_NAM; 285 nuidp->nu_nam = 286 dup_sockaddr(nfsd->nfsd_nd-> 287 nd_nam2, 1); 288 break; 289 }; 290 } 291 TAILQ_INSERT_TAIL(&slp->ns_uidlruhead, nuidp, 292 nu_lru); 293 LIST_INSERT_HEAD(NUIDHASH(slp, nsd->nsd_uid), 294 nuidp, nu_hash); 295 nfsrv_setcred(&nuidp->nu_cr, 296 &nfsd->nfsd_nd->nd_cr); 297 nfsd->nfsd_nd->nd_flag |= ND_KERBFULL; 298 } 299 } 300 } 301 if ((uap->flag & NFSSVC_AUTHINFAIL) && (nfsd = nsd->nsd_nfsd)) 302 nfsd->nfsd_flag |= NFSD_AUTHFAIL; 303 error = nfssvc_nfsd(nsd, uap->argp, p); 304 } 305#endif /* NFS_NOSERVER */ 306 if (error == EINTR || error == ERESTART) 307 error = 0; 308 return (error); 309} 310 311#ifndef NFS_NOSERVER 312/* 313 * Adds a socket to the list for servicing by nfsds. 314 */ 315static int 316nfssvc_addsock(fp, mynam, p) 317 struct file *fp; 318 struct sockaddr *mynam; 319 struct proc *p; 320{ 321 register int siz; 322 register struct nfssvc_sock *slp; 323 register struct socket *so; 324 int error, s; 325 326 so = (struct socket *)fp->f_data; 327#if 0 328 tslp = (struct nfssvc_sock *)0; 329 /* 330 * Add it to the list, as required. 331 */ 332 if (so->so_proto->pr_protocol == IPPROTO_UDP) { 333 tslp = nfs_udpsock; 334 if (tslp->ns_flag & SLP_VALID) { 335 if (mynam != NULL) 336 FREE(mynam, M_SONAME); 337 return (EPERM); 338 } 339 } 340#endif 341 if (so->so_type == SOCK_STREAM) 342 siz = NFS_MAXPACKET + sizeof (u_long); 343 else 344 siz = NFS_MAXPACKET; 345 error = soreserve(so, siz, siz); 346 if (error) { 347 if (mynam != NULL) 348 FREE(mynam, M_SONAME); 349 return (error); 350 } 351 352 /* 353 * Set protocol specific options { for now TCP only } and 354 * reserve some space. For datagram sockets, this can get called 355 * repeatedly for the same socket, but that isn't harmful. 356 */ 357 if (so->so_type == SOCK_STREAM) { 358 struct sockopt sopt; 359 int val; 360 361 bzero(&sopt, sizeof sopt); 362 sopt.sopt_level = SOL_SOCKET; 363 sopt.sopt_name = SO_KEEPALIVE; 364 sopt.sopt_val = &val; 365 sopt.sopt_valsize = sizeof val; 366 val = 1; 367 sosetopt(so, &sopt); 368 } 369 if (so->so_proto->pr_domain->dom_family == AF_INET && 370 so->so_proto->pr_protocol == IPPROTO_TCP) { 371 struct sockopt sopt; 372 int val; 373 374 bzero(&sopt, sizeof sopt); 375 sopt.sopt_level = IPPROTO_TCP; 376 sopt.sopt_name = TCP_NODELAY; 377 sopt.sopt_val = &val; 378 sopt.sopt_valsize = sizeof val; 379 val = 1; 380 sosetopt(so, &sopt); 381 } 382 so->so_rcv.sb_flags &= ~SB_NOINTR; 383 so->so_rcv.sb_timeo = 0; 384 so->so_snd.sb_flags &= ~SB_NOINTR; 385 so->so_snd.sb_timeo = 0; 386 387 slp = (struct nfssvc_sock *) 388 malloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK); 389 bzero((caddr_t)slp, sizeof (struct nfssvc_sock)); 390 STAILQ_INIT(&slp->ns_rec); 391 TAILQ_INIT(&slp->ns_uidlruhead); 392 TAILQ_INSERT_TAIL(&nfssvc_sockhead, slp, ns_chain); 393 394 slp->ns_so = so; 395 slp->ns_nam = mynam; 396 fp->f_count++; 397 slp->ns_fp = fp; 398 s = splnet(); 399 so->so_upcallarg = (caddr_t)slp; 400 so->so_upcall = nfsrv_rcv; 401 so->so_rcv.sb_flags |= SB_UPCALL; 402 slp->ns_flag = (SLP_VALID | SLP_NEEDQ); 403 nfsrv_wakenfsd(slp); 404 splx(s); 405 return (0); 406} 407 408/* 409 * Called by nfssvc() for nfsds. Just loops around servicing rpc requests 410 * until it is killed by a signal. 411 */ 412static int 413nfssvc_nfsd(nsd, argp, p) 414 struct nfsd_srvargs *nsd; 415 caddr_t argp; 416 struct proc *p; 417{ 418 register int siz; 419 register struct nfssvc_sock *slp; 420 struct nfsd *nfsd = nsd->nsd_nfsd; 421 struct nfsrv_descript *nd = NULL; 422 struct mbuf *m, *mreq; 423 int error = 0, cacherep, s, sotype, writes_todo; 424 int procrastinate; 425 u_quad_t cur_usec; 426 427#ifndef nolint 428 cacherep = RC_DOIT; 429 writes_todo = 0; 430#endif 431 if (nfsd == (struct nfsd *)0) { 432 nsd->nsd_nfsd = nfsd = (struct nfsd *) 433 malloc(sizeof (struct nfsd), M_NFSD, M_WAITOK); 434 bzero((caddr_t)nfsd, sizeof (struct nfsd)); 435 s = splnet(); 436 nfsd->nfsd_procp = p; 437 TAILQ_INSERT_TAIL(&nfsd_head, nfsd, nfsd_chain); 438 nfs_numnfsd++; 439 } else 440 s = splnet(); 441 442 /* 443 * Loop getting rpc requests until SIGKILL. 444 */ 445 for (;;) { 446 if ((nfsd->nfsd_flag & NFSD_REQINPROG) == 0) { 447 while (nfsd->nfsd_slp == (struct nfssvc_sock *)0 && 448 (nfsd_head_flag & NFSD_CHECKSLP) == 0) { 449 nfsd->nfsd_flag |= NFSD_WAITING; 450 nfsd_waiting++; 451 error = tsleep((caddr_t)nfsd, PSOCK | PCATCH, 452 "nfsd", 0); 453 nfsd_waiting--; 454 if (error) 455 goto done; 456 } 457 if (nfsd->nfsd_slp == (struct nfssvc_sock *)0 && 458 (nfsd_head_flag & NFSD_CHECKSLP) != 0) { 459 for (slp = nfssvc_sockhead.tqh_first; slp != 0; 460 slp = slp->ns_chain.tqe_next) { 461 if ((slp->ns_flag & (SLP_VALID | SLP_DOREC)) 462 == (SLP_VALID | SLP_DOREC)) { 463 slp->ns_flag &= ~SLP_DOREC; 464 slp->ns_sref++; 465 nfsd->nfsd_slp = slp; 466 break; 467 } 468 } 469 if (slp == 0) 470 nfsd_head_flag &= ~NFSD_CHECKSLP; 471 } 472 if ((slp = nfsd->nfsd_slp) == (struct nfssvc_sock *)0) 473 continue; 474 if (slp->ns_flag & SLP_VALID) { 475 if (slp->ns_flag & SLP_DISCONN) 476 nfsrv_zapsock(slp); 477 else if (slp->ns_flag & SLP_NEEDQ) { 478 slp->ns_flag &= ~SLP_NEEDQ; 479 (void) nfs_slplock(slp, 1); 480 nfsrv_rcv(slp->ns_so, (caddr_t)slp, 481 M_WAIT); 482 nfs_slpunlock(slp); 483 } 484 error = nfsrv_dorec(slp, nfsd, &nd); 485 cur_usec = nfs_curusec(); 486 if (error && slp->ns_tq.lh_first && 487 slp->ns_tq.lh_first->nd_time <= cur_usec) { 488 error = 0; 489 cacherep = RC_DOIT; 490 writes_todo = 1; 491 } else 492 writes_todo = 0; 493 nfsd->nfsd_flag |= NFSD_REQINPROG; 494 } 495 } else { 496 error = 0; 497 slp = nfsd->nfsd_slp; 498 } 499 if (error || (slp->ns_flag & SLP_VALID) == 0) { 500 if (nd) { 501 free((caddr_t)nd, M_NFSRVDESC); 502 nd = NULL; 503 } 504 nfsd->nfsd_slp = (struct nfssvc_sock *)0; 505 nfsd->nfsd_flag &= ~NFSD_REQINPROG; 506 nfsrv_slpderef(slp); 507 continue; 508 } 509 splx(s); 510 sotype = slp->ns_so->so_type; 511 if (nd) { 512 getmicrotime(&nd->nd_starttime); 513 if (nd->nd_nam2) 514 nd->nd_nam = nd->nd_nam2; 515 else 516 nd->nd_nam = slp->ns_nam; 517 518 /* 519 * Check to see if authorization is needed. 520 */ 521 if (nfsd->nfsd_flag & NFSD_NEEDAUTH) { 522 nfsd->nfsd_flag &= ~NFSD_NEEDAUTH; 523 nsd->nsd_haddr = 524 ((struct sockaddr_in *) 525 nd->nd_nam)->sin_addr.s_addr; 526 nsd->nsd_authlen = nfsd->nfsd_authlen; 527 nsd->nsd_verflen = nfsd->nfsd_verflen; 528 if (!copyout(nfsd->nfsd_authstr,nsd->nsd_authstr, 529 nfsd->nfsd_authlen) && 530 !copyout(nfsd->nfsd_verfstr, nsd->nsd_verfstr, 531 nfsd->nfsd_verflen) && 532 !copyout((caddr_t)nsd, argp, sizeof (*nsd))) 533 return (ENEEDAUTH); 534 cacherep = RC_DROPIT; 535 } else 536 cacherep = nfsrv_getcache(nd, slp, &mreq); 537 538 /* 539 * Check for just starting up for NQNFS and send 540 * fake "try again later" replies to the NQNFS clients. 541 */ 542 if (notstarted && nqnfsstarttime <= time_second) { 543 if (modify_flag) { 544 nqnfsstarttime = time_second + nqsrv_writeslack; 545 modify_flag = 0; 546 } else 547 notstarted = 0; 548 } 549 if (notstarted) { 550 if ((nd->nd_flag & ND_NQNFS) == 0) 551 cacherep = RC_DROPIT; 552 else if (nd->nd_procnum != NFSPROC_WRITE) { 553 nd->nd_procnum = NFSPROC_NOOP; 554 nd->nd_repstat = NQNFS_TRYLATER; 555 cacherep = RC_DOIT; 556 } else 557 modify_flag = 1; 558 } else if (nfsd->nfsd_flag & NFSD_AUTHFAIL) { 559 nfsd->nfsd_flag &= ~NFSD_AUTHFAIL; 560 nd->nd_procnum = NFSPROC_NOOP; 561 nd->nd_repstat = (NFSERR_AUTHERR | AUTH_TOOWEAK); 562 cacherep = RC_DOIT; 563 } else if (nfs_privport) { 564 /* Check if source port is privileged */ 565 u_short port; 566 struct sockaddr *nam = nd->nd_nam; 567 struct sockaddr_in *sin; 568 569 sin = (struct sockaddr_in *)nam; 570 port = ntohs(sin->sin_port); 571 if (port >= IPPORT_RESERVED && 572 nd->nd_procnum != NFSPROC_NULL) { 573 nd->nd_procnum = NFSPROC_NOOP; 574 nd->nd_repstat = (NFSERR_AUTHERR | AUTH_TOOWEAK); 575 cacherep = RC_DOIT; 576 printf("NFS request from unprivileged port (%s:%d)\n", 577 inet_ntoa(sin->sin_addr), port); 578 } 579 } 580 581 } 582 583 /* 584 * Loop to get all the write rpc relies that have been 585 * gathered together. 586 */ 587 do { 588 switch (cacherep) { 589 case RC_DOIT: 590 if (nd && (nd->nd_flag & ND_NFSV3)) 591 procrastinate = nfsrvw_procrastinate_v3; 592 else 593 procrastinate = nfsrvw_procrastinate; 594 if (writes_todo || (nd->nd_procnum == NFSPROC_WRITE && 595 procrastinate > 0 && !notstarted)) 596 error = nfsrv_writegather(&nd, slp, 597 nfsd->nfsd_procp, &mreq); 598 else 599 error = (*(nfsrv3_procs[nd->nd_procnum]))(nd, 600 slp, nfsd->nfsd_procp, &mreq); 601 if (mreq == NULL) 602 break; 603 if (error != 0 && error != NFSERR_RETVOID) { 604 if (nd->nd_procnum != NQNFSPROC_VACATED) 605 nfsstats.srv_errs++; 606 nfsrv_updatecache(nd, FALSE, mreq); 607 if (nd->nd_nam2) 608 FREE(nd->nd_nam2, M_SONAME); 609 break; 610 } 611 nfsstats.srvrpccnt[nd->nd_procnum]++; 612 nfsrv_updatecache(nd, TRUE, mreq); 613 nd->nd_mrep = (struct mbuf *)0; 614 case RC_REPLY: 615 m = mreq; 616 siz = 0; 617 while (m) { 618 siz += m->m_len; 619 m = m->m_next; 620 } 621 if (siz <= 0 || siz > NFS_MAXPACKET) { 622 printf("mbuf siz=%d\n",siz); 623 panic("Bad nfs svc reply"); 624 } 625 m = mreq; 626 m->m_pkthdr.len = siz; 627 m->m_pkthdr.rcvif = (struct ifnet *)0; 628 /* 629 * For stream protocols, prepend a Sun RPC 630 * Record Mark. 631 */ 632 if (sotype == SOCK_STREAM) { 633 M_PREPEND(m, NFSX_UNSIGNED, M_WAIT); 634 *mtod(m, u_int32_t *) = htonl(0x80000000 | siz); 635 } 636 if (slp->ns_so->so_proto->pr_flags & PR_CONNREQUIRED) 637 (void) nfs_slplock(slp, 1); 638 if (slp->ns_flag & SLP_VALID) 639 error = nfs_send(slp->ns_so, nd->nd_nam2, m, NULL); 640 else { 641 error = EPIPE; 642 m_freem(m); 643 } 644 if (nfsrtton) 645 nfsd_rt(sotype, nd, cacherep); 646 if (nd->nd_nam2) 647 FREE(nd->nd_nam2, M_SONAME); 648 if (nd->nd_mrep) 649 m_freem(nd->nd_mrep); 650 if (error == EPIPE) 651 nfsrv_zapsock(slp); 652 if (slp->ns_so->so_proto->pr_flags & PR_CONNREQUIRED) 653 nfs_slpunlock(slp); 654 if (error == EINTR || error == ERESTART) { 655 free((caddr_t)nd, M_NFSRVDESC); 656 nfsrv_slpderef(slp); 657 s = splnet(); 658 goto done; 659 } 660 break; 661 case RC_DROPIT: 662 if (nfsrtton) 663 nfsd_rt(sotype, nd, cacherep); 664 m_freem(nd->nd_mrep); 665 if (nd->nd_nam2) 666 FREE(nd->nd_nam2, M_SONAME); 667 break; 668 }; 669 if (nd) { 670 FREE((caddr_t)nd, M_NFSRVDESC); 671 nd = NULL; 672 } 673 674 /* 675 * Check to see if there are outstanding writes that 676 * need to be serviced. 677 */ 678 cur_usec = nfs_curusec(); 679 s = splsoftclock(); 680 if (slp->ns_tq.lh_first && 681 slp->ns_tq.lh_first->nd_time <= cur_usec) { 682 cacherep = RC_DOIT; 683 writes_todo = 1; 684 } else 685 writes_todo = 0; 686 splx(s); 687 } while (writes_todo); 688 s = splnet(); 689 if (nfsrv_dorec(slp, nfsd, &nd)) { 690 nfsd->nfsd_flag &= ~NFSD_REQINPROG; 691 nfsd->nfsd_slp = NULL; 692 nfsrv_slpderef(slp); 693 } 694 } 695done: 696 TAILQ_REMOVE(&nfsd_head, nfsd, nfsd_chain); 697 splx(s); 698 free((caddr_t)nfsd, M_NFSD); 699 nsd->nsd_nfsd = (struct nfsd *)0; 700 if (--nfs_numnfsd == 0) 701 nfsrv_init(TRUE); /* Reinitialize everything */ 702 return (error); 703} 704 705/* 706 * Shut down a socket associated with an nfssvc_sock structure. 707 * Should be called with the send lock set, if required. 708 * The trick here is to increment the sref at the start, so that the nfsds 709 * will stop using it and clear ns_flag at the end so that it will not be 710 * reassigned during cleanup. 711 */ 712static void 713nfsrv_zapsock(slp) 714 register struct nfssvc_sock *slp; 715{ 716 register struct nfsuid *nuidp, *nnuidp; 717 register struct nfsrv_descript *nwp, *nnwp; 718 struct socket *so; 719 struct file *fp; 720 struct nfsrv_rec *rec; 721 int s; 722 723 slp->ns_flag &= ~SLP_ALLFLAGS; 724 fp = slp->ns_fp; 725 if (fp) { 726 slp->ns_fp = (struct file *)0; 727 so = slp->ns_so; 728 so->so_rcv.sb_flags &= ~SB_UPCALL; 729 so->so_upcall = NULL; 730 so->so_upcallarg = NULL; 731 soshutdown(so, 2); 732 closef(fp, (struct proc *)0); 733 if (slp->ns_nam) 734 FREE(slp->ns_nam, M_SONAME); 735 m_freem(slp->ns_raw); 736 while ((rec = STAILQ_FIRST(&slp->ns_rec)) != NULL) { 737 STAILQ_REMOVE_HEAD(&slp->ns_rec, nr_link); 738 if (rec->nr_address) 739 FREE(rec->nr_address, M_SONAME); 740 m_freem(rec->nr_packet); 741 free(rec, M_NFSRVDESC); 742 } 743 for (nuidp = slp->ns_uidlruhead.tqh_first; nuidp != 0; 744 nuidp = nnuidp) { 745 nnuidp = nuidp->nu_lru.tqe_next; 746 LIST_REMOVE(nuidp, nu_hash); 747 TAILQ_REMOVE(&slp->ns_uidlruhead, nuidp, nu_lru); 748 if (nuidp->nu_flag & NU_NAM) 749 FREE(nuidp->nu_nam, M_SONAME); 750 free((caddr_t)nuidp, M_NFSUID); 751 } 752 s = splsoftclock(); 753 for (nwp = slp->ns_tq.lh_first; nwp; nwp = nnwp) { 754 nnwp = nwp->nd_tq.le_next; 755 LIST_REMOVE(nwp, nd_tq); 756 free((caddr_t)nwp, M_NFSRVDESC); 757 } 758 LIST_INIT(&slp->ns_tq); 759 splx(s); 760 } 761} 762 763/* 764 * Derefence a server socket structure. If it has no more references and 765 * is no longer valid, you can throw it away. 766 */ 767void 768nfsrv_slpderef(slp) 769 register struct nfssvc_sock *slp; 770{ 771 if (--(slp->ns_sref) == 0 && (slp->ns_flag & SLP_VALID) == 0) { 772 TAILQ_REMOVE(&nfssvc_sockhead, slp, ns_chain); 773 free((caddr_t)slp, M_NFSSVC); 774 } 775} 776 777/* 778 * Lock a socket against others. 779 */ 780int 781nfs_slplock(slp, wait) 782 register struct nfssvc_sock *slp; 783 int wait; 784{ 785 int *statep = &slp->ns_solock; 786 787 if (!wait && (*statep & NFSSTA_SNDLOCK)) 788 return(0); /* already locked, fail */ 789 while (*statep & NFSSTA_SNDLOCK) { 790 *statep |= NFSSTA_WANTSND; 791 (void) tsleep((caddr_t)statep, PZERO - 1, "nfsslplck", 0); 792 } 793 *statep |= NFSSTA_SNDLOCK; 794 return (1); 795} 796 797/* 798 * Unlock the stream socket for others. 799 */ 800void 801nfs_slpunlock(slp) 802 register struct nfssvc_sock *slp; 803{ 804 int *statep = &slp->ns_solock; 805 806 if ((*statep & NFSSTA_SNDLOCK) == 0) 807 panic("nfs slpunlock"); 808 *statep &= ~NFSSTA_SNDLOCK; 809 if (*statep & NFSSTA_WANTSND) { 810 *statep &= ~NFSSTA_WANTSND; 811 wakeup((caddr_t)statep); 812 } 813} 814 815/* 816 * Initialize the data structures for the server. 817 * Handshake with any new nfsds starting up to avoid any chance of 818 * corruption. 819 */ 820void 821nfsrv_init(terminating) 822 int terminating; 823{ 824 register struct nfssvc_sock *slp, *nslp; 825 826 if (nfssvc_sockhead_flag & SLP_INIT) 827 panic("nfsd init"); 828 nfssvc_sockhead_flag |= SLP_INIT; 829 if (terminating) { 830 for (slp = nfssvc_sockhead.tqh_first; slp != 0; slp = nslp) { 831 nslp = slp->ns_chain.tqe_next; 832 if (slp->ns_flag & SLP_VALID) 833 nfsrv_zapsock(slp); 834 TAILQ_REMOVE(&nfssvc_sockhead, slp, ns_chain); 835 free((caddr_t)slp, M_NFSSVC); 836 } 837 nfsrv_cleancache(); /* And clear out server cache */ 838 } else 839 nfs_pub.np_valid = 0; 840 841 TAILQ_INIT(&nfssvc_sockhead); 842 nfssvc_sockhead_flag &= ~SLP_INIT; 843 if (nfssvc_sockhead_flag & SLP_WANTINIT) { 844 nfssvc_sockhead_flag &= ~SLP_WANTINIT; 845 wakeup((caddr_t)&nfssvc_sockhead); 846 } 847 848 TAILQ_INIT(&nfsd_head); 849 nfsd_head_flag &= ~NFSD_CHECKSLP; 850 851#if 0 852 nfs_udpsock = (struct nfssvc_sock *) 853 malloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK); 854 bzero((caddr_t)nfs_udpsock, sizeof (struct nfssvc_sock)); 855 STAILQ_INIT(&nfs_udpsock->ns_rec); 856 TAILQ_INIT(&nfs_udpsock->ns_uidlruhead); 857 TAILQ_INSERT_HEAD(&nfssvc_sockhead, nfs_udpsock, ns_chain); 858 859 nfs_cltpsock = (struct nfssvc_sock *) 860 malloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK); 861 bzero((caddr_t)nfs_cltpsock, sizeof (struct nfssvc_sock)); 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