1 /* $OpenBSD: nfs_socket.c,v 1.12 1997/07/28 19:54:17 deraadt Exp $ */ 2 /* $NetBSD: nfs_socket.c,v 1.27 1996/04/15 20:20:00 thorpej Exp $ */ 3 4 /* 5 * Copyright (c) 1989, 1991, 1993, 1995 6 * The Regents of the University of California. All rights reserved. 7 * 8 * This code is derived from software contributed to Berkeley by 9 * Rick Macklem at The University of Guelph. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgement: 21 * This product includes software developed by the University of 22 * California, Berkeley and its contributors. 23 * 4. Neither the name of the University nor the names of its contributors 24 * may be used to endorse or promote products derived from this software 25 * without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 37 * SUCH DAMAGE. 38 * 39 * @(#)nfs_socket.c 8.5 (Berkeley) 3/30/95 40 */ 41 42 /* 43 * Socket operations for use by nfs 44 */ 45 46 #include <sys/param.h> 47 #include <sys/systm.h> 48 #include <sys/proc.h> 49 #include <sys/mount.h> 50 #include <sys/kernel.h> 51 #include <sys/mbuf.h> 52 #include <sys/vnode.h> 53 #include <sys/domain.h> 54 #include <sys/protosw.h> 55 #include <sys/socket.h> 56 #include <sys/socketvar.h> 57 #include <sys/syslog.h> 58 #include <sys/tprintf.h> 59 #include <sys/namei.h> 60 61 #include <netinet/in.h> 62 #include <netinet/tcp.h> 63 64 #include <nfs/rpcv2.h> 65 #include <nfs/nfsproto.h> 66 #include <nfs/nfs.h> 67 #include <nfs/xdr_subs.h> 68 #include <nfs/nfsm_subs.h> 69 #include <nfs/nfsmount.h> 70 #include <nfs/nfsnode.h> 71 #include <nfs/nfsrtt.h> 72 #include <nfs/nqnfs.h> 73 #include <nfs/nfs_var.h> 74 75 #define TRUE 1 76 #define FALSE 0 77 78 /* 79 * Estimate rto for an nfs rpc sent via. an unreliable datagram. 80 * Use the mean and mean deviation of rtt for the appropriate type of rpc 81 * for the frequent rpcs and a default for the others. 82 * The justification for doing "other" this way is that these rpcs 83 * happen so infrequently that timer est. would probably be stale. 84 * Also, since many of these rpcs are 85 * non-idempotent, a conservative timeout is desired. 86 * getattr, lookup - A+2D 87 * read, write - A+4D 88 * other - nm_timeo 89 */ 90 #define NFS_RTO(n, t) \ 91 ((t) == 0 ? (n)->nm_timeo : \ 92 ((t) < 3 ? \ 93 (((((n)->nm_srtt[t-1] + 3) >> 2) + (n)->nm_sdrtt[t-1] + 1) >> 1) : \ 94 ((((n)->nm_srtt[t-1] + 7) >> 3) + (n)->nm_sdrtt[t-1] + 1))) 95 #define NFS_SRTT(r) (r)->r_nmp->nm_srtt[proct[(r)->r_procnum] - 1] 96 #define NFS_SDRTT(r) (r)->r_nmp->nm_sdrtt[proct[(r)->r_procnum] - 1] 97 /* 98 * External data, mostly RPC constants in XDR form 99 */ 100 extern u_int32_t rpc_reply, rpc_msgdenied, rpc_mismatch, rpc_vers, 101 rpc_auth_unix, rpc_msgaccepted, rpc_call, rpc_autherr, 102 rpc_auth_kerb; 103 extern u_int32_t nfs_prog, nqnfs_prog; 104 extern time_t nqnfsstarttime; 105 extern struct nfsstats nfsstats; 106 extern int nfsv3_procid[NFS_NPROCS]; 107 extern int nfs_ticks; 108 109 /* 110 * Defines which timer to use for the procnum. 111 * 0 - default 112 * 1 - getattr 113 * 2 - lookup 114 * 3 - read 115 * 4 - write 116 */ 117 static int proct[NFS_NPROCS] = { 118 0, 1, 0, 2, 1, 3, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 0, 0, 0, 0, 0, 119 0, 0, 0, 120 }; 121 122 /* 123 * There is a congestion window for outstanding rpcs maintained per mount 124 * point. The cwnd size is adjusted in roughly the way that: 125 * Van Jacobson, Congestion avoidance and Control, In "Proceedings of 126 * SIGCOMM '88". ACM, August 1988. 127 * describes for TCP. The cwnd size is chopped in half on a retransmit timeout 128 * and incremented by 1/cwnd when each rpc reply is received and a full cwnd 129 * of rpcs is in progress. 130 * (The sent count and cwnd are scaled for integer arith.) 131 * Variants of "slow start" were tried and were found to be too much of a 132 * performance hit (ave. rtt 3 times larger), 133 * I suspect due to the large rtt that nfs rpcs have. 134 */ 135 #define NFS_CWNDSCALE 256 136 #define NFS_MAXCWND (NFS_CWNDSCALE * 32) 137 static int nfs_backoff[8] = { 2, 4, 8, 16, 32, 64, 128, 256, }; 138 int nfsrtton = 0; 139 struct nfsrtt nfsrtt; 140 141 /* 142 * Initialize sockets and congestion for a new NFS connection. 143 * We do not free the sockaddr if error. 144 */ 145 int 146 nfs_connect(nmp, rep) 147 register struct nfsmount *nmp; 148 struct nfsreq *rep; 149 { 150 register struct socket *so; 151 int s, error, rcvreserve, sndreserve; 152 struct sockaddr *saddr; 153 struct sockaddr_in *sin; 154 struct mbuf *m; 155 156 nmp->nm_so = (struct socket *)0; 157 saddr = mtod(nmp->nm_nam, struct sockaddr *); 158 error = socreate(saddr->sa_family, &nmp->nm_so, nmp->nm_sotype, 159 nmp->nm_soproto); 160 if (error) 161 goto bad; 162 so = nmp->nm_so; 163 nmp->nm_soflags = so->so_proto->pr_flags; 164 165 /* 166 * Some servers require that the client port be a reserved port number. 167 * We always allocate a reserved port, as this prevents filehandle 168 * disclosure through UDP port capture. 169 */ 170 if (saddr->sa_family == AF_INET) { 171 struct mbuf *mopt; 172 int *ip; 173 174 MGET(mopt, M_WAIT, MT_SOOPTS); 175 mopt->m_len = sizeof(int); 176 ip = mtod(mopt, int *); 177 *ip = IP_PORTRANGE_LOW; 178 error = sosetopt(so, IPPROTO_IP, IP_PORTRANGE, mopt); 179 if (error) 180 goto bad; 181 182 MGET(m, M_WAIT, MT_SONAME); 183 sin = mtod(m, struct sockaddr_in *); 184 sin->sin_len = m->m_len = sizeof (struct sockaddr_in); 185 sin->sin_family = AF_INET; 186 sin->sin_addr.s_addr = INADDR_ANY; 187 sin->sin_port = htons(0); 188 error = sobind(so, m); 189 m_freem(m); 190 if (error) 191 goto bad; 192 193 MGET(mopt, M_WAIT, MT_SOOPTS); 194 mopt->m_len = sizeof(int); 195 ip = mtod(mopt, int *); 196 *ip = IP_PORTRANGE_DEFAULT; 197 error = sosetopt(so, IPPROTO_IP, IP_PORTRANGE, mopt); 198 if (error) 199 goto bad; 200 } 201 202 /* 203 * Protocols that do not require connections may be optionally left 204 * unconnected for servers that reply from a port other than NFS_PORT. 205 */ 206 if (nmp->nm_flag & NFSMNT_NOCONN) { 207 if (nmp->nm_soflags & PR_CONNREQUIRED) { 208 error = ENOTCONN; 209 goto bad; 210 } 211 } else { 212 error = soconnect(so, nmp->nm_nam); 213 if (error) 214 goto bad; 215 216 /* 217 * Wait for the connection to complete. Cribbed from the 218 * connect system call but with the wait timing out so 219 * that interruptible mounts don't hang here for a long time. 220 */ 221 s = splsoftnet(); 222 while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) { 223 (void) tsleep((caddr_t)&so->so_timeo, PSOCK, 224 "nfscon", 2 * hz); 225 if ((so->so_state & SS_ISCONNECTING) && 226 so->so_error == 0 && rep && 227 (error = nfs_sigintr(nmp, rep, rep->r_procp)) != 0){ 228 so->so_state &= ~SS_ISCONNECTING; 229 splx(s); 230 goto bad; 231 } 232 } 233 if (so->so_error) { 234 error = so->so_error; 235 so->so_error = 0; 236 splx(s); 237 goto bad; 238 } 239 splx(s); 240 } 241 if (nmp->nm_flag & (NFSMNT_SOFT | NFSMNT_INT)) { 242 so->so_rcv.sb_timeo = (5 * hz); 243 so->so_snd.sb_timeo = (5 * hz); 244 } else { 245 so->so_rcv.sb_timeo = 0; 246 so->so_snd.sb_timeo = 0; 247 } 248 if (nmp->nm_sotype == SOCK_DGRAM) { 249 sndreserve = nmp->nm_wsize + NFS_MAXPKTHDR; 250 rcvreserve = max(nmp->nm_rsize, nmp->nm_readdirsize) + 251 NFS_MAXPKTHDR; 252 } else if (nmp->nm_sotype == SOCK_SEQPACKET) { 253 sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR) * 2; 254 rcvreserve = (max(nmp->nm_rsize, nmp->nm_readdirsize) + 255 NFS_MAXPKTHDR) * 2; 256 } else { 257 if (nmp->nm_sotype != SOCK_STREAM) 258 panic("nfscon sotype"); 259 if (so->so_proto->pr_flags & PR_CONNREQUIRED) { 260 MGET(m, M_WAIT, MT_SOOPTS); 261 *mtod(m, int32_t *) = 1; 262 m->m_len = sizeof(int32_t); 263 sosetopt(so, SOL_SOCKET, SO_KEEPALIVE, m); 264 } 265 if (so->so_proto->pr_protocol == IPPROTO_TCP) { 266 MGET(m, M_WAIT, MT_SOOPTS); 267 *mtod(m, int32_t *) = 1; 268 m->m_len = sizeof(int32_t); 269 sosetopt(so, IPPROTO_TCP, TCP_NODELAY, m); 270 } 271 sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR + 272 sizeof (u_int32_t)) * 2; 273 rcvreserve = (nmp->nm_rsize + NFS_MAXPKTHDR + 274 sizeof (u_int32_t)) * 2; 275 } 276 error = soreserve(so, sndreserve, rcvreserve); 277 if (error) 278 goto bad; 279 so->so_rcv.sb_flags |= SB_NOINTR; 280 so->so_snd.sb_flags |= SB_NOINTR; 281 282 /* Initialize other non-zero congestion variables */ 283 nmp->nm_srtt[0] = nmp->nm_srtt[1] = nmp->nm_srtt[2] = nmp->nm_srtt[3] = 284 nmp->nm_srtt[4] = (NFS_TIMEO << 3); 285 nmp->nm_sdrtt[0] = nmp->nm_sdrtt[1] = nmp->nm_sdrtt[2] = 286 nmp->nm_sdrtt[3] = nmp->nm_sdrtt[4] = 0; 287 nmp->nm_cwnd = NFS_MAXCWND / 2; /* Initial send window */ 288 nmp->nm_sent = 0; 289 nmp->nm_timeouts = 0; 290 return (0); 291 292 bad: 293 nfs_disconnect(nmp); 294 return (error); 295 } 296 297 /* 298 * Reconnect routine: 299 * Called when a connection is broken on a reliable protocol. 300 * - clean up the old socket 301 * - nfs_connect() again 302 * - set R_MUSTRESEND for all outstanding requests on mount point 303 * If this fails the mount point is DEAD! 304 * nb: Must be called with the nfs_sndlock() set on the mount point. 305 */ 306 int 307 nfs_reconnect(rep) 308 register struct nfsreq *rep; 309 { 310 register struct nfsreq *rp; 311 register struct nfsmount *nmp = rep->r_nmp; 312 int error; 313 314 nfs_disconnect(nmp); 315 while ((error = nfs_connect(nmp, rep)) != 0) { 316 if (error == EINTR || error == ERESTART) 317 return (EINTR); 318 (void) tsleep((caddr_t)&lbolt, PSOCK, "nfscon", 0); 319 } 320 321 /* 322 * Loop through outstanding request list and fix up all requests 323 * on old socket. 324 */ 325 for (rp = nfs_reqq.tqh_first; rp != 0; rp = rp->r_chain.tqe_next) { 326 if (rp->r_nmp == nmp) 327 rp->r_flags |= R_MUSTRESEND; 328 } 329 return (0); 330 } 331 332 /* 333 * NFS disconnect. Clean up and unlink. 334 */ 335 void 336 nfs_disconnect(nmp) 337 register struct nfsmount *nmp; 338 { 339 register struct socket *so; 340 341 if (nmp->nm_so) { 342 so = nmp->nm_so; 343 nmp->nm_so = (struct socket *)0; 344 soshutdown(so, 2); 345 soclose(so); 346 } 347 } 348 349 /* 350 * This is the nfs send routine. For connection based socket types, it 351 * must be called with an nfs_sndlock() on the socket. 352 * "rep == NULL" indicates that it has been called from a server. 353 * For the client side: 354 * - return EINTR if the RPC is terminated, 0 otherwise 355 * - set R_MUSTRESEND if the send fails for any reason 356 * - do any cleanup required by recoverable socket errors (???) 357 * For the server side: 358 * - return EINTR or ERESTART if interrupted by a signal 359 * - return EPIPE if a connection is lost for connection based sockets (TCP...) 360 * - do any cleanup required by recoverable socket errors (???) 361 */ 362 int 363 nfs_send(so, nam, top, rep) 364 register struct socket *so; 365 struct mbuf *nam; 366 register struct mbuf *top; 367 struct nfsreq *rep; 368 { 369 struct mbuf *sendnam; 370 int error, soflags, flags; 371 372 if (rep) { 373 if (rep->r_flags & R_SOFTTERM) { 374 m_freem(top); 375 return (EINTR); 376 } 377 if ((so = rep->r_nmp->nm_so) == NULL) { 378 rep->r_flags |= R_MUSTRESEND; 379 m_freem(top); 380 return (0); 381 } 382 rep->r_flags &= ~R_MUSTRESEND; 383 soflags = rep->r_nmp->nm_soflags; 384 } else 385 soflags = so->so_proto->pr_flags; 386 if ((soflags & PR_CONNREQUIRED) || (so->so_state & SS_ISCONNECTED)) 387 sendnam = (struct mbuf *)0; 388 else 389 sendnam = nam; 390 if (so->so_type == SOCK_SEQPACKET) 391 flags = MSG_EOR; 392 else 393 flags = 0; 394 395 error = sosend(so, sendnam, (struct uio *)0, top, 396 (struct mbuf *)0, flags); 397 if (error) { 398 if (rep) { 399 log(LOG_INFO, "nfs send error %d for server %s\n",error, 400 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname); 401 /* 402 * Deal with errors for the client side. 403 */ 404 if (rep->r_flags & R_SOFTTERM) 405 error = EINTR; 406 else 407 rep->r_flags |= R_MUSTRESEND; 408 } else 409 log(LOG_INFO, "nfsd send error %d\n", error); 410 411 /* 412 * Handle any recoverable (soft) socket errors here. (???) 413 */ 414 if (error != EINTR && error != ERESTART && 415 error != EWOULDBLOCK && error != EPIPE) 416 error = 0; 417 } 418 return (error); 419 } 420 421 #ifdef NFSCLIENT 422 /* 423 * Receive a Sun RPC Request/Reply. For SOCK_DGRAM, the work is all 424 * done by soreceive(), but for SOCK_STREAM we must deal with the Record 425 * Mark and consolidate the data into a new mbuf list. 426 * nb: Sometimes TCP passes the data up to soreceive() in long lists of 427 * small mbufs. 428 * For SOCK_STREAM we must be very careful to read an entire record once 429 * we have read any of it, even if the system call has been interrupted. 430 */ 431 int 432 nfs_receive(rep, aname, mp) 433 register struct nfsreq *rep; 434 struct mbuf **aname; 435 struct mbuf **mp; 436 { 437 register struct socket *so; 438 struct uio auio; 439 struct iovec aio; 440 register struct mbuf *m; 441 struct mbuf *control; 442 u_int32_t len; 443 struct mbuf **getnam; 444 int error, sotype, rcvflg; 445 struct proc *p = curproc; /* XXX */ 446 447 /* 448 * Set up arguments for soreceive() 449 */ 450 *mp = (struct mbuf *)0; 451 *aname = (struct mbuf *)0; 452 sotype = rep->r_nmp->nm_sotype; 453 454 /* 455 * For reliable protocols, lock against other senders/receivers 456 * in case a reconnect is necessary. 457 * For SOCK_STREAM, first get the Record Mark to find out how much 458 * more there is to get. 459 * We must lock the socket against other receivers 460 * until we have an entire rpc request/reply. 461 */ 462 if (sotype != SOCK_DGRAM) { 463 error = nfs_sndlock(&rep->r_nmp->nm_flag, rep); 464 if (error) 465 return (error); 466 tryagain: 467 /* 468 * Check for fatal errors and resending request. 469 */ 470 /* 471 * Ugh: If a reconnect attempt just happened, nm_so 472 * would have changed. NULL indicates a failed 473 * attempt that has essentially shut down this 474 * mount point. 475 */ 476 if (rep->r_mrep || (rep->r_flags & R_SOFTTERM)) { 477 nfs_sndunlock(&rep->r_nmp->nm_flag); 478 return (EINTR); 479 } 480 so = rep->r_nmp->nm_so; 481 if (!so) { 482 error = nfs_reconnect(rep); 483 if (error) { 484 nfs_sndunlock(&rep->r_nmp->nm_flag); 485 return (error); 486 } 487 goto tryagain; 488 } 489 while (rep->r_flags & R_MUSTRESEND) { 490 m = m_copym(rep->r_mreq, 0, M_COPYALL, M_WAIT); 491 nfsstats.rpcretries++; 492 error = nfs_send(so, rep->r_nmp->nm_nam, m, rep); 493 if (error) { 494 if (error == EINTR || error == ERESTART || 495 (error = nfs_reconnect(rep)) != 0) { 496 nfs_sndunlock(&rep->r_nmp->nm_flag); 497 return (error); 498 } 499 goto tryagain; 500 } 501 } 502 nfs_sndunlock(&rep->r_nmp->nm_flag); 503 if (sotype == SOCK_STREAM) { 504 aio.iov_base = (caddr_t) &len; 505 aio.iov_len = sizeof(u_int32_t); 506 auio.uio_iov = &aio; 507 auio.uio_iovcnt = 1; 508 auio.uio_segflg = UIO_SYSSPACE; 509 auio.uio_rw = UIO_READ; 510 auio.uio_offset = 0; 511 auio.uio_resid = sizeof(u_int32_t); 512 auio.uio_procp = p; 513 do { 514 rcvflg = MSG_WAITALL; 515 error = soreceive(so, (struct mbuf **)0, &auio, 516 (struct mbuf **)0, (struct mbuf **)0, &rcvflg); 517 if (error == EWOULDBLOCK && rep) { 518 if (rep->r_flags & R_SOFTTERM) 519 return (EINTR); 520 } 521 } while (error == EWOULDBLOCK); 522 if (!error && auio.uio_resid > 0) { 523 log(LOG_INFO, 524 "short receive (%d/%d) from nfs server %s\n", 525 sizeof(u_int32_t) - auio.uio_resid, 526 sizeof(u_int32_t), 527 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname); 528 error = EPIPE; 529 } 530 if (error) 531 goto errout; 532 len = ntohl(len) & ~0x80000000; 533 /* 534 * This is SERIOUS! We are out of sync with the sender 535 * and forcing a disconnect/reconnect is all I can do. 536 */ 537 if (len > NFS_MAXPACKET) { 538 log(LOG_ERR, "%s (%d) from nfs server %s\n", 539 "impossible packet length", 540 len, 541 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname); 542 error = EFBIG; 543 goto errout; 544 } 545 auio.uio_resid = len; 546 do { 547 rcvflg = MSG_WAITALL; 548 error = soreceive(so, (struct mbuf **)0, 549 &auio, mp, (struct mbuf **)0, &rcvflg); 550 } while (error == EWOULDBLOCK || error == EINTR || 551 error == ERESTART); 552 if (!error && auio.uio_resid > 0) { 553 log(LOG_INFO, 554 "short receive (%d/%d) from nfs server %s\n", 555 len - auio.uio_resid, len, 556 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname); 557 error = EPIPE; 558 } 559 } else { 560 /* 561 * NB: Since uio_resid is big, MSG_WAITALL is ignored 562 * and soreceive() will return when it has either a 563 * control msg or a data msg. 564 * We have no use for control msg., but must grab them 565 * and then throw them away so we know what is going 566 * on. 567 */ 568 auio.uio_resid = len = 100000000; /* Anything Big */ 569 auio.uio_procp = p; 570 do { 571 rcvflg = 0; 572 error = soreceive(so, (struct mbuf **)0, 573 &auio, mp, &control, &rcvflg); 574 if (control) 575 m_freem(control); 576 if (error == EWOULDBLOCK && rep) { 577 if (rep->r_flags & R_SOFTTERM) 578 return (EINTR); 579 } 580 } while (error == EWOULDBLOCK || 581 (!error && *mp == NULL && control)); 582 if ((rcvflg & MSG_EOR) == 0) 583 printf("Egad!!\n"); 584 if (!error && *mp == NULL) 585 error = EPIPE; 586 len -= auio.uio_resid; 587 } 588 errout: 589 if (error && error != EINTR && error != ERESTART) { 590 m_freem(*mp); 591 *mp = (struct mbuf *)0; 592 if (error != EPIPE) 593 log(LOG_INFO, 594 "receive error %d from nfs server %s\n", 595 error, 596 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname); 597 error = nfs_sndlock(&rep->r_nmp->nm_flag, rep); 598 if (!error) 599 error = nfs_reconnect(rep); 600 if (!error) 601 goto tryagain; 602 } 603 } else { 604 if ((so = rep->r_nmp->nm_so) == NULL) 605 return (EACCES); 606 if (so->so_state & SS_ISCONNECTED) 607 getnam = (struct mbuf **)0; 608 else 609 getnam = aname; 610 auio.uio_resid = len = 1000000; 611 auio.uio_procp = p; 612 do { 613 rcvflg = 0; 614 error = soreceive(so, getnam, &auio, mp, 615 (struct mbuf **)0, &rcvflg); 616 if (error == EWOULDBLOCK && 617 (rep->r_flags & R_SOFTTERM)) 618 return (EINTR); 619 } while (error == EWOULDBLOCK); 620 len -= auio.uio_resid; 621 } 622 if (error) { 623 m_freem(*mp); 624 *mp = (struct mbuf *)0; 625 } 626 /* 627 * Search for any mbufs that are not a multiple of 4 bytes long 628 * or with m_data not longword aligned. 629 * These could cause pointer alignment problems, so copy them to 630 * well aligned mbufs. 631 */ 632 nfs_realign(*mp, 5 * NFSX_UNSIGNED); 633 return (error); 634 } 635 636 /* 637 * Implement receipt of reply on a socket. 638 * We must search through the list of received datagrams matching them 639 * with outstanding requests using the xid, until ours is found. 640 */ 641 /* ARGSUSED */ 642 int 643 nfs_reply(myrep) 644 struct nfsreq *myrep; 645 { 646 register struct nfsreq *rep; 647 register struct nfsmount *nmp = myrep->r_nmp; 648 register int32_t t1; 649 struct mbuf *mrep, *nam, *md; 650 u_int32_t rxid, *tl; 651 caddr_t dpos, cp2; 652 int error; 653 654 /* 655 * Loop around until we get our own reply 656 */ 657 for (;;) { 658 /* 659 * Lock against other receivers so that I don't get stuck in 660 * sbwait() after someone else has received my reply for me. 661 * Also necessary for connection based protocols to avoid 662 * race conditions during a reconnect. 663 */ 664 error = nfs_rcvlock(myrep); 665 if (error) 666 return (error); 667 /* Already received, bye bye */ 668 if (myrep->r_mrep != NULL) { 669 nfs_rcvunlock(&nmp->nm_flag); 670 return (0); 671 } 672 /* 673 * Get the next Rpc reply off the socket 674 */ 675 error = nfs_receive(myrep, &nam, &mrep); 676 nfs_rcvunlock(&nmp->nm_flag); 677 if (error) { 678 679 /* 680 * Ignore routing errors on connectionless protocols?? 681 */ 682 if (NFSIGNORE_SOERROR(nmp->nm_soflags, error)) { 683 nmp->nm_so->so_error = 0; 684 if (myrep->r_flags & R_GETONEREP) 685 return (0); 686 continue; 687 } 688 return (error); 689 } 690 if (nam) 691 m_freem(nam); 692 693 /* 694 * Get the xid and check that it is an rpc reply 695 */ 696 md = mrep; 697 dpos = mtod(md, caddr_t); 698 nfsm_dissect(tl, u_int32_t *, 2*NFSX_UNSIGNED); 699 rxid = *tl++; 700 if (*tl != rpc_reply) { 701 if (nmp->nm_flag & NFSMNT_NQNFS) { 702 if (nqnfs_callback(nmp, mrep, md, dpos)) 703 nfsstats.rpcinvalid++; 704 } else { 705 nfsstats.rpcinvalid++; 706 m_freem(mrep); 707 } 708 nfsmout: 709 if (myrep->r_flags & R_GETONEREP) 710 return (0); 711 continue; 712 } 713 714 /* 715 * Loop through the request list to match up the reply 716 * Iff no match, just drop the datagram 717 */ 718 for (rep = nfs_reqq.tqh_first; rep != 0; 719 rep = rep->r_chain.tqe_next) { 720 if (rep->r_mrep == NULL && rxid == rep->r_xid) { 721 /* Found it.. */ 722 rep->r_mrep = mrep; 723 rep->r_md = md; 724 rep->r_dpos = dpos; 725 if (nfsrtton) { 726 struct rttl *rt; 727 728 rt = &nfsrtt.rttl[nfsrtt.pos]; 729 rt->proc = rep->r_procnum; 730 rt->rto = NFS_RTO(nmp, proct[rep->r_procnum]); 731 rt->sent = nmp->nm_sent; 732 rt->cwnd = nmp->nm_cwnd; 733 rt->srtt = nmp->nm_srtt[proct[rep->r_procnum] - 1]; 734 rt->sdrtt = nmp->nm_sdrtt[proct[rep->r_procnum] - 1]; 735 rt->fsid = nmp->nm_mountp->mnt_stat.f_fsid; 736 rt->tstamp = time; 737 if (rep->r_flags & R_TIMING) 738 rt->rtt = rep->r_rtt; 739 else 740 rt->rtt = 1000000; 741 nfsrtt.pos = (nfsrtt.pos + 1) % NFSRTTLOGSIZ; 742 } 743 /* 744 * Update congestion window. 745 * Do the additive increase of 746 * one rpc/rtt. 747 */ 748 if (nmp->nm_cwnd <= nmp->nm_sent) { 749 nmp->nm_cwnd += 750 (NFS_CWNDSCALE * NFS_CWNDSCALE + 751 (nmp->nm_cwnd >> 1)) / nmp->nm_cwnd; 752 if (nmp->nm_cwnd > NFS_MAXCWND) 753 nmp->nm_cwnd = NFS_MAXCWND; 754 } 755 rep->r_flags &= ~R_SENT; 756 nmp->nm_sent -= NFS_CWNDSCALE; 757 /* 758 * Update rtt using a gain of 0.125 on the mean 759 * and a gain of 0.25 on the deviation. 760 */ 761 if (rep->r_flags & R_TIMING) { 762 /* 763 * Since the timer resolution of 764 * NFS_HZ is so course, it can often 765 * result in r_rtt == 0. Since 766 * r_rtt == N means that the actual 767 * rtt is between N+dt and N+2-dt ticks, 768 * add 1. 769 */ 770 t1 = rep->r_rtt + 1; 771 t1 -= (NFS_SRTT(rep) >> 3); 772 NFS_SRTT(rep) += t1; 773 if (t1 < 0) 774 t1 = -t1; 775 t1 -= (NFS_SDRTT(rep) >> 2); 776 NFS_SDRTT(rep) += t1; 777 } 778 nmp->nm_timeouts = 0; 779 break; 780 } 781 } 782 /* 783 * If not matched to a request, drop it. 784 * If it's mine, get out. 785 */ 786 if (rep == 0) { 787 nfsstats.rpcunexpected++; 788 m_freem(mrep); 789 } else if (rep == myrep) { 790 if (rep->r_mrep == NULL) 791 panic("nfsreply nil"); 792 return (0); 793 } 794 if (myrep->r_flags & R_GETONEREP) 795 return (0); 796 } 797 } 798 799 /* 800 * nfs_request - goes something like this 801 * - fill in request struct 802 * - links it into list 803 * - calls nfs_send() for first transmit 804 * - calls nfs_receive() to get reply 805 * - break down rpc header and return with nfs reply pointed to 806 * by mrep or error 807 * nb: always frees up mreq mbuf list 808 */ 809 int 810 nfs_request(vp, mrest, procnum, procp, cred, mrp, mdp, dposp) 811 struct vnode *vp; 812 struct mbuf *mrest; 813 int procnum; 814 struct proc *procp; 815 struct ucred *cred; 816 struct mbuf **mrp; 817 struct mbuf **mdp; 818 caddr_t *dposp; 819 { 820 register struct mbuf *m, *mrep; 821 register struct nfsreq *rep; 822 register u_int32_t *tl; 823 register int i; 824 struct nfsmount *nmp; 825 struct mbuf *md, *mheadend; 826 struct nfsnode *np; 827 char nickv[RPCX_NICKVERF]; 828 time_t reqtime, waituntil; 829 caddr_t dpos, cp2; 830 int t1, nqlflag, cachable, s, error = 0, mrest_len, auth_len, auth_type; 831 int trylater_delay = NQ_TRYLATERDEL, trylater_cnt = 0, failed_auth = 0; 832 int verf_len, verf_type; 833 u_int32_t xid; 834 u_quad_t frev; 835 char *auth_str, *verf_str; 836 NFSKERBKEY_T key; /* save session key */ 837 838 nmp = VFSTONFS(vp->v_mount); 839 MALLOC(rep, struct nfsreq *, sizeof(struct nfsreq), M_NFSREQ, M_WAITOK); 840 rep->r_nmp = nmp; 841 rep->r_vp = vp; 842 rep->r_procp = procp; 843 rep->r_procnum = procnum; 844 i = 0; 845 m = mrest; 846 while (m) { 847 i += m->m_len; 848 m = m->m_next; 849 } 850 mrest_len = i; 851 852 /* 853 * Get the RPC header with authorization. 854 */ 855 kerbauth: 856 verf_str = auth_str = (char *)0; 857 if (nmp->nm_flag & NFSMNT_KERB) { 858 verf_str = nickv; 859 verf_len = sizeof (nickv); 860 auth_type = RPCAUTH_KERB4; 861 bzero((caddr_t)key, sizeof (key)); 862 if (failed_auth || nfs_getnickauth(nmp, cred, &auth_str, 863 &auth_len, verf_str, verf_len)) { 864 error = nfs_getauth(nmp, rep, cred, &auth_str, 865 &auth_len, verf_str, &verf_len, key); 866 if (error) { 867 free((caddr_t)rep, M_NFSREQ); 868 m_freem(mrest); 869 return (error); 870 } 871 } 872 } else { 873 auth_type = RPCAUTH_UNIX; 874 auth_len = (((cred->cr_ngroups > nmp->nm_numgrps) ? 875 nmp->nm_numgrps : cred->cr_ngroups) << 2) + 876 5 * NFSX_UNSIGNED; 877 } 878 m = nfsm_rpchead(cred, nmp->nm_flag, procnum, auth_type, auth_len, 879 auth_str, verf_len, verf_str, mrest, mrest_len, &mheadend, &xid); 880 if (auth_str) 881 free(auth_str, M_TEMP); 882 883 /* 884 * For stream protocols, insert a Sun RPC Record Mark. 885 */ 886 if (nmp->nm_sotype == SOCK_STREAM) { 887 M_PREPEND(m, NFSX_UNSIGNED, M_WAIT); 888 *mtod(m, u_int32_t *) = htonl(0x80000000 | 889 (m->m_pkthdr.len - NFSX_UNSIGNED)); 890 } 891 rep->r_mreq = m; 892 rep->r_xid = xid; 893 tryagain: 894 if (nmp->nm_flag & NFSMNT_SOFT) 895 rep->r_retry = nmp->nm_retry; 896 else 897 rep->r_retry = NFS_MAXREXMIT + 1; /* past clip limit */ 898 rep->r_rtt = rep->r_rexmit = 0; 899 if (proct[procnum] > 0) 900 rep->r_flags = R_TIMING; 901 else 902 rep->r_flags = 0; 903 rep->r_mrep = NULL; 904 905 /* 906 * Do the client side RPC. 907 */ 908 nfsstats.rpcrequests++; 909 /* 910 * Chain request into list of outstanding requests. Be sure 911 * to put it LAST so timer finds oldest requests first. 912 */ 913 s = splsoftclock(); 914 TAILQ_INSERT_TAIL(&nfs_reqq, rep, r_chain); 915 916 /* Get send time for nqnfs */ 917 reqtime = time.tv_sec; 918 919 /* 920 * If backing off another request or avoiding congestion, don't 921 * send this one now but let timer do it. If not timing a request, 922 * do it now. 923 */ 924 if (nmp->nm_so && (nmp->nm_sotype != SOCK_DGRAM || 925 (nmp->nm_flag & NFSMNT_DUMBTIMR) || 926 nmp->nm_sent < nmp->nm_cwnd)) { 927 splx(s); 928 if (nmp->nm_soflags & PR_CONNREQUIRED) 929 error = nfs_sndlock(&nmp->nm_flag, rep); 930 if (!error) { 931 m = m_copym(m, 0, M_COPYALL, M_WAIT); 932 error = nfs_send(nmp->nm_so, nmp->nm_nam, m, rep); 933 if (nmp->nm_soflags & PR_CONNREQUIRED) 934 nfs_sndunlock(&nmp->nm_flag); 935 } 936 if (!error && (rep->r_flags & R_MUSTRESEND) == 0) { 937 nmp->nm_sent += NFS_CWNDSCALE; 938 rep->r_flags |= R_SENT; 939 } 940 } else { 941 splx(s); 942 rep->r_rtt = -1; 943 } 944 945 /* 946 * Wait for the reply from our send or the timer's. 947 */ 948 if (!error || error == EPIPE) 949 error = nfs_reply(rep); 950 951 /* 952 * RPC done, unlink the request. 953 */ 954 s = splsoftclock(); 955 TAILQ_REMOVE(&nfs_reqq, rep, r_chain); 956 splx(s); 957 958 /* 959 * Decrement the outstanding request count. 960 */ 961 if (rep->r_flags & R_SENT) { 962 rep->r_flags &= ~R_SENT; /* paranoia */ 963 nmp->nm_sent -= NFS_CWNDSCALE; 964 } 965 966 /* 967 * If there was a successful reply and a tprintf msg. 968 * tprintf a response. 969 */ 970 if (!error && (rep->r_flags & R_TPRINTFMSG)) 971 nfs_msg(rep->r_procp, nmp->nm_mountp->mnt_stat.f_mntfromname, 972 "is alive again"); 973 mrep = rep->r_mrep; 974 md = rep->r_md; 975 dpos = rep->r_dpos; 976 if (error) { 977 m_freem(rep->r_mreq); 978 free((caddr_t)rep, M_NFSREQ); 979 return (error); 980 } 981 982 /* 983 * break down the rpc header and check if ok 984 */ 985 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED); 986 if (*tl++ == rpc_msgdenied) { 987 if (*tl == rpc_mismatch) 988 error = EOPNOTSUPP; 989 else if ((nmp->nm_flag & NFSMNT_KERB) && *tl++ == rpc_autherr) { 990 if (!failed_auth) { 991 failed_auth++; 992 mheadend->m_next = (struct mbuf *)0; 993 m_freem(mrep); 994 m_freem(rep->r_mreq); 995 goto kerbauth; 996 } else 997 error = EAUTH; 998 } else 999 error = EACCES; 1000 m_freem(mrep); 1001 m_freem(rep->r_mreq); 1002 free((caddr_t)rep, M_NFSREQ); 1003 return (error); 1004 } 1005 1006 /* 1007 * Grab any Kerberos verifier, otherwise just throw it away. 1008 */ 1009 verf_type = fxdr_unsigned(int, *tl++); 1010 i = fxdr_unsigned(int32_t, *tl); 1011 if ((nmp->nm_flag & NFSMNT_KERB) && verf_type == RPCAUTH_KERB4) { 1012 error = nfs_savenickauth(nmp, cred, i, key, &md, &dpos, mrep); 1013 if (error) 1014 goto nfsmout; 1015 } else if (i > 0) 1016 nfsm_adv(nfsm_rndup(i)); 1017 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); 1018 /* 0 == ok */ 1019 if (*tl == 0) { 1020 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); 1021 if (*tl != 0) { 1022 error = fxdr_unsigned(int, *tl); 1023 if ((nmp->nm_flag & NFSMNT_NFSV3) && 1024 error == NFSERR_TRYLATER) { 1025 m_freem(mrep); 1026 error = 0; 1027 waituntil = time.tv_sec + trylater_delay; 1028 while (time.tv_sec < waituntil) 1029 (void) tsleep((caddr_t)&lbolt, 1030 PSOCK, "nqnfstry", 0); 1031 trylater_delay *= nfs_backoff[trylater_cnt]; 1032 if (trylater_cnt < 7) 1033 trylater_cnt++; 1034 goto tryagain; 1035 } 1036 1037 /* 1038 * If the File Handle was stale, invalidate the 1039 * lookup cache, just in case. 1040 */ 1041 if (error == ESTALE) 1042 cache_purge(vp); 1043 if (nmp->nm_flag & NFSMNT_NFSV3) { 1044 *mrp = mrep; 1045 *mdp = md; 1046 *dposp = dpos; 1047 error |= NFSERR_RETERR; 1048 } else 1049 m_freem(mrep); 1050 m_freem(rep->r_mreq); 1051 free((caddr_t)rep, M_NFSREQ); 1052 return (error); 1053 } 1054 1055 /* 1056 * For nqnfs, get any lease in reply 1057 */ 1058 if (nmp->nm_flag & NFSMNT_NQNFS) { 1059 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); 1060 if (*tl) { 1061 np = VTONFS(vp); 1062 nqlflag = fxdr_unsigned(int, *tl); 1063 nfsm_dissect(tl, u_int32_t *, 4*NFSX_UNSIGNED); 1064 cachable = fxdr_unsigned(int, *tl++); 1065 reqtime += fxdr_unsigned(int, *tl++); 1066 if (reqtime > time.tv_sec) { 1067 fxdr_hyper(tl, &frev); 1068 nqnfs_clientlease(nmp, np, nqlflag, 1069 cachable, reqtime, frev); 1070 } 1071 } 1072 } 1073 *mrp = mrep; 1074 *mdp = md; 1075 *dposp = dpos; 1076 m_freem(rep->r_mreq); 1077 FREE((caddr_t)rep, M_NFSREQ); 1078 return (0); 1079 } 1080 m_freem(mrep); 1081 error = EPROTONOSUPPORT; 1082 nfsmout: 1083 m_freem(rep->r_mreq); 1084 free((caddr_t)rep, M_NFSREQ); 1085 return (error); 1086 } 1087 #endif /* NFSCLIENT */ 1088 1089 /* 1090 * Generate the rpc reply header 1091 * siz arg. is used to decide if adding a cluster is worthwhile 1092 */ 1093 int 1094 nfs_rephead(siz, nd, slp, err, cache, frev, mrq, mbp, bposp) 1095 int siz; 1096 struct nfsrv_descript *nd; 1097 struct nfssvc_sock *slp; 1098 int err; 1099 int cache; 1100 u_quad_t *frev; 1101 struct mbuf **mrq; 1102 struct mbuf **mbp; 1103 caddr_t *bposp; 1104 { 1105 register u_int32_t *tl; 1106 register struct mbuf *mreq; 1107 caddr_t bpos; 1108 struct mbuf *mb, *mb2; 1109 1110 MGETHDR(mreq, M_WAIT, MT_DATA); 1111 mb = mreq; 1112 /* 1113 * If this is a big reply, use a cluster else 1114 * try and leave leading space for the lower level headers. 1115 */ 1116 siz += RPC_REPLYSIZ; 1117 if (siz >= MINCLSIZE) { 1118 MCLGET(mreq, M_WAIT); 1119 } else 1120 mreq->m_data += max_hdr; 1121 tl = mtod(mreq, u_int32_t *); 1122 mreq->m_len = 6 * NFSX_UNSIGNED; 1123 bpos = ((caddr_t)tl) + mreq->m_len; 1124 *tl++ = txdr_unsigned(nd->nd_retxid); 1125 *tl++ = rpc_reply; 1126 if (err == ERPCMISMATCH || (err & NFSERR_AUTHERR)) { 1127 *tl++ = rpc_msgdenied; 1128 if (err & NFSERR_AUTHERR) { 1129 *tl++ = rpc_autherr; 1130 *tl = txdr_unsigned(err & ~NFSERR_AUTHERR); 1131 mreq->m_len -= NFSX_UNSIGNED; 1132 bpos -= NFSX_UNSIGNED; 1133 } else { 1134 *tl++ = rpc_mismatch; 1135 *tl++ = txdr_unsigned(RPC_VER2); 1136 *tl = txdr_unsigned(RPC_VER2); 1137 } 1138 } else { 1139 *tl++ = rpc_msgaccepted; 1140 1141 /* 1142 * For Kerberos authentication, we must send the nickname 1143 * verifier back, otherwise just RPCAUTH_NULL. 1144 */ 1145 if (nd->nd_flag & ND_KERBFULL) { 1146 register struct nfsuid *nuidp; 1147 struct timeval ktvin, ktvout; 1148 1149 for (nuidp = NUIDHASH(slp, nd->nd_cr.cr_uid)->lh_first; 1150 nuidp != 0; nuidp = nuidp->nu_hash.le_next) { 1151 if (nuidp->nu_cr.cr_uid == nd->nd_cr.cr_uid && 1152 (!nd->nd_nam2 || netaddr_match(NU_NETFAM(nuidp), 1153 &nuidp->nu_haddr, nd->nd_nam2))) 1154 break; 1155 } 1156 if (nuidp) { 1157 ktvin.tv_sec = 1158 txdr_unsigned(nuidp->nu_timestamp.tv_sec - 1); 1159 ktvin.tv_usec = 1160 txdr_unsigned(nuidp->nu_timestamp.tv_usec); 1161 1162 /* 1163 * Encrypt the timestamp in ecb mode using the 1164 * session key. 1165 */ 1166 #ifdef NFSKERB 1167 XXX 1168 #endif 1169 1170 *tl++ = rpc_auth_kerb; 1171 *tl++ = txdr_unsigned(3 * NFSX_UNSIGNED); 1172 *tl = ktvout.tv_sec; 1173 nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED); 1174 *tl++ = ktvout.tv_usec; 1175 *tl++ = txdr_unsigned(nuidp->nu_cr.cr_uid); 1176 } else { 1177 *tl++ = 0; 1178 *tl++ = 0; 1179 } 1180 } else { 1181 *tl++ = 0; 1182 *tl++ = 0; 1183 } 1184 switch (err) { 1185 case EPROGUNAVAIL: 1186 *tl = txdr_unsigned(RPC_PROGUNAVAIL); 1187 break; 1188 case EPROGMISMATCH: 1189 *tl = txdr_unsigned(RPC_PROGMISMATCH); 1190 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 1191 if (nd->nd_flag & ND_NQNFS) { 1192 *tl++ = txdr_unsigned(3); 1193 *tl = txdr_unsigned(3); 1194 } else { 1195 *tl++ = txdr_unsigned(2); 1196 *tl = txdr_unsigned(3); 1197 } 1198 break; 1199 case EPROCUNAVAIL: 1200 *tl = txdr_unsigned(RPC_PROCUNAVAIL); 1201 break; 1202 case EBADRPC: 1203 *tl = txdr_unsigned(RPC_GARBAGE); 1204 break; 1205 default: 1206 *tl = 0; 1207 if (err != NFSERR_RETVOID) { 1208 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); 1209 if (err) 1210 *tl = txdr_unsigned(nfsrv_errmap(nd, err)); 1211 else 1212 *tl = 0; 1213 } 1214 break; 1215 }; 1216 } 1217 1218 /* 1219 * For nqnfs, piggyback lease as requested. 1220 */ 1221 if ((nd->nd_flag & ND_NQNFS) && err == 0) { 1222 if (nd->nd_flag & ND_LEASE) { 1223 nfsm_build(tl, u_int32_t *, 5 * NFSX_UNSIGNED); 1224 *tl++ = txdr_unsigned(nd->nd_flag & ND_LEASE); 1225 *tl++ = txdr_unsigned(cache); 1226 *tl++ = txdr_unsigned(nd->nd_duration); 1227 txdr_hyper(frev, tl); 1228 } else { 1229 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); 1230 *tl = 0; 1231 } 1232 } 1233 *mrq = mreq; 1234 if (mrq != NULL) 1235 *mbp = mb; 1236 *bposp = bpos; 1237 if (err != 0 && err != NFSERR_RETVOID) 1238 nfsstats.srvrpc_errs++; 1239 return (0); 1240 } 1241 1242 /* 1243 * Nfs timer routine 1244 * Scan the nfsreq list and retranmit any requests that have timed out 1245 * To avoid retransmission attempts on STREAM sockets (in the future) make 1246 * sure to set the r_retry field to 0 (implies nm_retry == 0). 1247 */ 1248 void 1249 nfs_timer(arg) 1250 void *arg; /* never used */ 1251 { 1252 register struct nfsreq *rep; 1253 register struct mbuf *m; 1254 register struct socket *so; 1255 register struct nfsmount *nmp; 1256 register int timeo; 1257 int s, error; 1258 #ifdef NFSSERVER 1259 register struct nfssvc_sock *slp; 1260 static long lasttime = 0; 1261 u_quad_t cur_usec; 1262 #endif 1263 1264 s = splsoftnet(); 1265 for (rep = nfs_reqq.tqh_first; rep != 0; rep = rep->r_chain.tqe_next) { 1266 nmp = rep->r_nmp; 1267 if (rep->r_mrep || (rep->r_flags & R_SOFTTERM)) 1268 continue; 1269 if (nfs_sigintr(nmp, rep, rep->r_procp)) { 1270 rep->r_flags |= R_SOFTTERM; 1271 continue; 1272 } 1273 if (rep->r_rtt >= 0) { 1274 rep->r_rtt++; 1275 if (nmp->nm_flag & NFSMNT_DUMBTIMR) 1276 timeo = nmp->nm_timeo; 1277 else 1278 timeo = NFS_RTO(nmp, proct[rep->r_procnum]); 1279 if (nmp->nm_timeouts > 0) 1280 timeo *= nfs_backoff[nmp->nm_timeouts - 1]; 1281 if (rep->r_rtt <= timeo) 1282 continue; 1283 if (nmp->nm_timeouts < 8) 1284 nmp->nm_timeouts++; 1285 } 1286 /* 1287 * Check for server not responding 1288 */ 1289 if ((rep->r_flags & R_TPRINTFMSG) == 0 && 1290 rep->r_rexmit > nmp->nm_deadthresh) { 1291 nfs_msg(rep->r_procp, 1292 nmp->nm_mountp->mnt_stat.f_mntfromname, 1293 "not responding"); 1294 rep->r_flags |= R_TPRINTFMSG; 1295 } 1296 if (rep->r_rexmit >= rep->r_retry) { /* too many */ 1297 nfsstats.rpctimeouts++; 1298 rep->r_flags |= R_SOFTTERM; 1299 continue; 1300 } 1301 if (nmp->nm_sotype != SOCK_DGRAM) { 1302 if (++rep->r_rexmit > NFS_MAXREXMIT) 1303 rep->r_rexmit = NFS_MAXREXMIT; 1304 continue; 1305 } 1306 if ((so = nmp->nm_so) == NULL) 1307 continue; 1308 1309 /* 1310 * If there is enough space and the window allows.. 1311 * Resend it 1312 * Set r_rtt to -1 in case we fail to send it now. 1313 */ 1314 rep->r_rtt = -1; 1315 if (sbspace(&so->so_snd) >= rep->r_mreq->m_pkthdr.len && 1316 ((nmp->nm_flag & NFSMNT_DUMBTIMR) || 1317 (rep->r_flags & R_SENT) || 1318 nmp->nm_sent < nmp->nm_cwnd) && 1319 (m = m_copym(rep->r_mreq, 0, M_COPYALL, M_DONTWAIT))){ 1320 if ((nmp->nm_flag & NFSMNT_NOCONN) == 0) 1321 error = (*so->so_proto->pr_usrreq)(so, PRU_SEND, m, 1322 (struct mbuf *)0, (struct mbuf *)0); 1323 else 1324 error = (*so->so_proto->pr_usrreq)(so, PRU_SEND, m, 1325 nmp->nm_nam, (struct mbuf *)0); 1326 if (error) { 1327 if (NFSIGNORE_SOERROR(nmp->nm_soflags, error)) 1328 so->so_error = 0; 1329 } else { 1330 /* 1331 * Iff first send, start timing 1332 * else turn timing off, backoff timer 1333 * and divide congestion window by 2. 1334 */ 1335 if (rep->r_flags & R_SENT) { 1336 rep->r_flags &= ~R_TIMING; 1337 if (++rep->r_rexmit > NFS_MAXREXMIT) 1338 rep->r_rexmit = NFS_MAXREXMIT; 1339 nmp->nm_cwnd >>= 1; 1340 if (nmp->nm_cwnd < NFS_CWNDSCALE) 1341 nmp->nm_cwnd = NFS_CWNDSCALE; 1342 nfsstats.rpcretries++; 1343 } else { 1344 rep->r_flags |= R_SENT; 1345 nmp->nm_sent += NFS_CWNDSCALE; 1346 } 1347 rep->r_rtt = 0; 1348 } 1349 } 1350 } 1351 1352 #ifdef NFSSERVER 1353 /* 1354 * Call the nqnfs server timer once a second to handle leases. 1355 */ 1356 if (lasttime != time.tv_sec) { 1357 lasttime = time.tv_sec; 1358 nqnfs_serverd(); 1359 } 1360 1361 /* 1362 * Scan the write gathering queues for writes that need to be 1363 * completed now. 1364 */ 1365 cur_usec = (u_quad_t)time.tv_sec * 1000000 + (u_quad_t)time.tv_usec; 1366 for (slp = nfssvc_sockhead.tqh_first; slp != 0; 1367 slp = slp->ns_chain.tqe_next) { 1368 if (slp->ns_tq.lh_first && slp->ns_tq.lh_first->nd_time<=cur_usec) 1369 nfsrv_wakenfsd(slp); 1370 } 1371 #endif /* NFSSERVER */ 1372 splx(s); 1373 timeout(nfs_timer, (void *)0, nfs_ticks); 1374 } 1375 1376 /* 1377 * Test for a termination condition pending on the process. 1378 * This is used for NFSMNT_INT mounts. 1379 */ 1380 int 1381 nfs_sigintr(nmp, rep, p) 1382 struct nfsmount *nmp; 1383 struct nfsreq *rep; 1384 register struct proc *p; 1385 { 1386 1387 if (rep && (rep->r_flags & R_SOFTTERM)) 1388 return (EINTR); 1389 if (!(nmp->nm_flag & NFSMNT_INT)) 1390 return (0); 1391 if (p && p->p_siglist && 1392 (((p->p_siglist & ~p->p_sigmask) & ~p->p_sigignore) & 1393 NFSINT_SIGMASK)) 1394 return (EINTR); 1395 return (0); 1396 } 1397 1398 /* 1399 * Lock a socket against others. 1400 * Necessary for STREAM sockets to ensure you get an entire rpc request/reply 1401 * and also to avoid race conditions between the processes with nfs requests 1402 * in progress when a reconnect is necessary. 1403 */ 1404 int 1405 nfs_sndlock(flagp, rep) 1406 register int *flagp; 1407 struct nfsreq *rep; 1408 { 1409 struct proc *p; 1410 int slpflag = 0, slptimeo = 0; 1411 1412 if (rep) { 1413 p = rep->r_procp; 1414 if (rep->r_nmp->nm_flag & NFSMNT_INT) 1415 slpflag = PCATCH; 1416 } else 1417 p = (struct proc *)0; 1418 while (*flagp & NFSMNT_SNDLOCK) { 1419 if (nfs_sigintr(rep->r_nmp, rep, p)) 1420 return (EINTR); 1421 *flagp |= NFSMNT_WANTSND; 1422 (void) tsleep((caddr_t)flagp, slpflag | (PZERO - 1), "nfsndlck", 1423 slptimeo); 1424 if (slpflag == PCATCH) { 1425 slpflag = 0; 1426 slptimeo = 2 * hz; 1427 } 1428 } 1429 *flagp |= NFSMNT_SNDLOCK; 1430 return (0); 1431 } 1432 1433 /* 1434 * Unlock the stream socket for others. 1435 */ 1436 void 1437 nfs_sndunlock(flagp) 1438 register int *flagp; 1439 { 1440 1441 if ((*flagp & NFSMNT_SNDLOCK) == 0) 1442 panic("nfs sndunlock"); 1443 *flagp &= ~NFSMNT_SNDLOCK; 1444 if (*flagp & NFSMNT_WANTSND) { 1445 *flagp &= ~NFSMNT_WANTSND; 1446 wakeup((caddr_t)flagp); 1447 } 1448 } 1449 1450 int 1451 nfs_rcvlock(rep) 1452 register struct nfsreq *rep; 1453 { 1454 register int *flagp = &rep->r_nmp->nm_flag; 1455 int slpflag, slptimeo = 0; 1456 1457 if (*flagp & NFSMNT_INT) 1458 slpflag = PCATCH; 1459 else 1460 slpflag = 0; 1461 while (*flagp & NFSMNT_RCVLOCK) { 1462 if (nfs_sigintr(rep->r_nmp, rep, rep->r_procp)) 1463 return (EINTR); 1464 *flagp |= NFSMNT_WANTRCV; 1465 (void) tsleep((caddr_t)flagp, slpflag | (PZERO - 1), "nfsrcvlk", 1466 slptimeo); 1467 if (slpflag == PCATCH) { 1468 slpflag = 0; 1469 slptimeo = 2 * hz; 1470 } 1471 } 1472 *flagp |= NFSMNT_RCVLOCK; 1473 return (0); 1474 } 1475 1476 /* 1477 * Unlock the stream socket for others. 1478 */ 1479 void 1480 nfs_rcvunlock(flagp) 1481 register int *flagp; 1482 { 1483 1484 if ((*flagp & NFSMNT_RCVLOCK) == 0) 1485 panic("nfs rcvunlock"); 1486 *flagp &= ~NFSMNT_RCVLOCK; 1487 if (*flagp & NFSMNT_WANTRCV) { 1488 *flagp &= ~NFSMNT_WANTRCV; 1489 wakeup((caddr_t)flagp); 1490 } 1491 } 1492 1493 /* 1494 * Check for badly aligned mbuf data areas and 1495 * realign data in an mbuf list by copying the data areas up, as required. 1496 */ 1497 void 1498 nfs_realign(m, hsiz) 1499 register struct mbuf *m; 1500 int hsiz; 1501 { 1502 register struct mbuf *m2; 1503 register int siz, mlen, olen; 1504 register caddr_t tcp, fcp; 1505 struct mbuf *mnew; 1506 1507 while (m) { 1508 /* 1509 * This never happens for UDP, rarely happens for TCP 1510 * but frequently happens for iso transport. 1511 */ 1512 if ((m->m_len & 0x3) || (mtod(m, long) & 0x3)) { 1513 olen = m->m_len; 1514 fcp = mtod(m, caddr_t); 1515 if ((long)fcp & 0x3) { 1516 m->m_flags &= ~M_PKTHDR; 1517 if (m->m_flags & M_EXT) 1518 m->m_data = m->m_ext.ext_buf + 1519 ((m->m_ext.ext_size - olen) & ~0x3); 1520 else 1521 m->m_data = m->m_dat; 1522 } 1523 m->m_len = 0; 1524 tcp = mtod(m, caddr_t); 1525 mnew = m; 1526 m2 = m->m_next; 1527 1528 /* 1529 * If possible, only put the first invariant part 1530 * of the RPC header in the first mbuf. 1531 */ 1532 mlen = M_TRAILINGSPACE(m); 1533 if (olen <= hsiz && mlen > hsiz) 1534 mlen = hsiz; 1535 1536 /* 1537 * Loop through the mbuf list consolidating data. 1538 */ 1539 while (m) { 1540 while (olen > 0) { 1541 if (mlen == 0) { 1542 m2->m_flags &= ~M_PKTHDR; 1543 if (m2->m_flags & M_EXT) 1544 m2->m_data = m2->m_ext.ext_buf; 1545 else 1546 m2->m_data = m2->m_dat; 1547 m2->m_len = 0; 1548 mlen = M_TRAILINGSPACE(m2); 1549 tcp = mtod(m2, caddr_t); 1550 mnew = m2; 1551 m2 = m2->m_next; 1552 } 1553 siz = min(mlen, olen); 1554 if (tcp != fcp) 1555 bcopy(fcp, tcp, siz); 1556 mnew->m_len += siz; 1557 mlen -= siz; 1558 olen -= siz; 1559 tcp += siz; 1560 fcp += siz; 1561 } 1562 m = m->m_next; 1563 if (m) { 1564 olen = m->m_len; 1565 fcp = mtod(m, caddr_t); 1566 } 1567 } 1568 1569 /* 1570 * Finally, set m_len == 0 for any trailing mbufs that have 1571 * been copied out of. 1572 */ 1573 while (m2) { 1574 m2->m_len = 0; 1575 m2 = m2->m_next; 1576 } 1577 return; 1578 } 1579 m = m->m_next; 1580 } 1581 } 1582 1583 /* 1584 * Parse an RPC request 1585 * - verify it 1586 * - fill in the cred struct. 1587 */ 1588 int 1589 nfs_getreq(nd, nfsd, has_header) 1590 register struct nfsrv_descript *nd; 1591 struct nfsd *nfsd; 1592 int has_header; 1593 { 1594 register int len, i; 1595 register u_int32_t *tl; 1596 register int32_t t1; 1597 struct uio uio; 1598 struct iovec iov; 1599 caddr_t dpos, cp2, cp; 1600 u_int32_t nfsvers, auth_type; 1601 uid_t nickuid; 1602 int error = 0, nqnfs = 0, ticklen; 1603 struct mbuf *mrep, *md; 1604 register struct nfsuid *nuidp; 1605 struct timeval tvin, tvout; 1606 1607 mrep = nd->nd_mrep; 1608 md = nd->nd_md; 1609 dpos = nd->nd_dpos; 1610 if (has_header) { 1611 nfsm_dissect(tl, u_int32_t *, 10 * NFSX_UNSIGNED); 1612 nd->nd_retxid = fxdr_unsigned(u_int32_t, *tl++); 1613 if (*tl++ != rpc_call) { 1614 m_freem(mrep); 1615 return (EBADRPC); 1616 } 1617 } else 1618 nfsm_dissect(tl, u_int32_t *, 8 * NFSX_UNSIGNED); 1619 nd->nd_repstat = 0; 1620 nd->nd_flag = 0; 1621 if (*tl++ != rpc_vers) { 1622 nd->nd_repstat = ERPCMISMATCH; 1623 nd->nd_procnum = NFSPROC_NOOP; 1624 return (0); 1625 } 1626 if (*tl != nfs_prog) { 1627 if (*tl == nqnfs_prog) 1628 nqnfs++; 1629 else { 1630 nd->nd_repstat = EPROGUNAVAIL; 1631 nd->nd_procnum = NFSPROC_NOOP; 1632 return (0); 1633 } 1634 } 1635 tl++; 1636 nfsvers = fxdr_unsigned(u_int32_t, *tl++); 1637 if (((nfsvers < NFS_VER2 || nfsvers > NFS_VER3) && !nqnfs) || 1638 (nfsvers != NQNFS_VER3 && nqnfs)) { 1639 nd->nd_repstat = EPROGMISMATCH; 1640 nd->nd_procnum = NFSPROC_NOOP; 1641 return (0); 1642 } 1643 if (nqnfs) 1644 nd->nd_flag = (ND_NFSV3 | ND_NQNFS); 1645 else if (nfsvers == NFS_VER3) 1646 nd->nd_flag = ND_NFSV3; 1647 nd->nd_procnum = fxdr_unsigned(u_int32_t, *tl++); 1648 if (nd->nd_procnum == NFSPROC_NULL) 1649 return (0); 1650 if (nd->nd_procnum >= NFS_NPROCS || 1651 (!nqnfs && nd->nd_procnum >= NQNFSPROC_GETLEASE) || 1652 (!nd->nd_flag && nd->nd_procnum > NFSV2PROC_STATFS)) { 1653 nd->nd_repstat = EPROCUNAVAIL; 1654 nd->nd_procnum = NFSPROC_NOOP; 1655 return (0); 1656 } 1657 if ((nd->nd_flag & ND_NFSV3) == 0) 1658 nd->nd_procnum = nfsv3_procid[nd->nd_procnum]; 1659 auth_type = *tl++; 1660 len = fxdr_unsigned(int, *tl++); 1661 if (len < 0 || len > RPCAUTH_MAXSIZ) { 1662 m_freem(mrep); 1663 return (EBADRPC); 1664 } 1665 1666 nd->nd_flag &= ~ND_KERBAUTH; 1667 /* 1668 * Handle auth_unix or auth_kerb. 1669 */ 1670 if (auth_type == rpc_auth_unix) { 1671 len = fxdr_unsigned(int, *++tl); 1672 if (len < 0 || len > NFS_MAXNAMLEN) { 1673 m_freem(mrep); 1674 return (EBADRPC); 1675 } 1676 nfsm_adv(nfsm_rndup(len)); 1677 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED); 1678 bzero((caddr_t)&nd->nd_cr, sizeof (struct ucred)); 1679 nd->nd_cr.cr_ref = 1; 1680 nd->nd_cr.cr_uid = fxdr_unsigned(uid_t, *tl++); 1681 nd->nd_cr.cr_gid = fxdr_unsigned(gid_t, *tl++); 1682 len = fxdr_unsigned(int, *tl); 1683 if (len < 0 || len > RPCAUTH_UNIXGIDS) { 1684 m_freem(mrep); 1685 return (EBADRPC); 1686 } 1687 nfsm_dissect(tl, u_int32_t *, (len + 2) * NFSX_UNSIGNED); 1688 for (i = 0; i < len; i++) 1689 if (i < NGROUPS) 1690 nd->nd_cr.cr_groups[i] = fxdr_unsigned(gid_t, *tl++); 1691 else 1692 tl++; 1693 nd->nd_cr.cr_ngroups = (len > NGROUPS) ? NGROUPS : len; 1694 if (nd->nd_cr.cr_ngroups > 1) 1695 nfsrvw_sort(nd->nd_cr.cr_groups, nd->nd_cr.cr_ngroups); 1696 len = fxdr_unsigned(int, *++tl); 1697 if (len < 0 || len > RPCAUTH_MAXSIZ) { 1698 m_freem(mrep); 1699 return (EBADRPC); 1700 } 1701 if (len > 0) 1702 nfsm_adv(nfsm_rndup(len)); 1703 } else if (auth_type == rpc_auth_kerb) { 1704 switch (fxdr_unsigned(int, *tl++)) { 1705 case RPCAKN_FULLNAME: 1706 ticklen = fxdr_unsigned(int, *tl); 1707 *((u_int32_t *)nfsd->nfsd_authstr) = *tl; 1708 uio.uio_resid = nfsm_rndup(ticklen) + NFSX_UNSIGNED; 1709 nfsd->nfsd_authlen = uio.uio_resid + NFSX_UNSIGNED; 1710 if (uio.uio_resid > (len - 2 * NFSX_UNSIGNED)) { 1711 m_freem(mrep); 1712 return (EBADRPC); 1713 } 1714 uio.uio_offset = 0; 1715 uio.uio_iov = &iov; 1716 uio.uio_iovcnt = 1; 1717 uio.uio_segflg = UIO_SYSSPACE; 1718 iov.iov_base = (caddr_t)&nfsd->nfsd_authstr[4]; 1719 iov.iov_len = RPCAUTH_MAXSIZ - 4; 1720 nfsm_mtouio(&uio, uio.uio_resid); 1721 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 1722 if (*tl++ != rpc_auth_kerb || 1723 fxdr_unsigned(int, *tl) != 4 * NFSX_UNSIGNED) { 1724 printf("Bad kerb verifier\n"); 1725 nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADVERF); 1726 nd->nd_procnum = NFSPROC_NOOP; 1727 return (0); 1728 } 1729 nfsm_dissect(cp, caddr_t, 4 * NFSX_UNSIGNED); 1730 tl = (u_int32_t *)cp; 1731 if (fxdr_unsigned(int, *tl) != RPCAKN_FULLNAME) { 1732 printf("Not fullname kerb verifier\n"); 1733 nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADVERF); 1734 nd->nd_procnum = NFSPROC_NOOP; 1735 return (0); 1736 } 1737 cp += NFSX_UNSIGNED; 1738 bcopy(cp, nfsd->nfsd_verfstr, 3 * NFSX_UNSIGNED); 1739 nfsd->nfsd_verflen = 3 * NFSX_UNSIGNED; 1740 nd->nd_flag |= ND_KERBFULL; 1741 nfsd->nfsd_flag |= NFSD_NEEDAUTH; 1742 break; 1743 case RPCAKN_NICKNAME: 1744 if (len != 2 * NFSX_UNSIGNED) { 1745 printf("Kerb nickname short\n"); 1746 nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADCRED); 1747 nd->nd_procnum = NFSPROC_NOOP; 1748 return (0); 1749 } 1750 nickuid = fxdr_unsigned(uid_t, *tl); 1751 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 1752 if (*tl++ != rpc_auth_kerb || 1753 fxdr_unsigned(int, *tl) != 3 * NFSX_UNSIGNED) { 1754 printf("Kerb nick verifier bad\n"); 1755 nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADVERF); 1756 nd->nd_procnum = NFSPROC_NOOP; 1757 return (0); 1758 } 1759 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED); 1760 tvin.tv_sec = *tl++; 1761 tvin.tv_usec = *tl; 1762 1763 for (nuidp = NUIDHASH(nfsd->nfsd_slp,nickuid)->lh_first; 1764 nuidp != 0; nuidp = nuidp->nu_hash.le_next) { 1765 if (nuidp->nu_cr.cr_uid == nickuid && 1766 (!nd->nd_nam2 || 1767 netaddr_match(NU_NETFAM(nuidp), 1768 &nuidp->nu_haddr, nd->nd_nam2))) 1769 break; 1770 } 1771 if (!nuidp) { 1772 nd->nd_repstat = 1773 (NFSERR_AUTHERR|AUTH_REJECTCRED); 1774 nd->nd_procnum = NFSPROC_NOOP; 1775 return (0); 1776 } 1777 1778 /* 1779 * Now, decrypt the timestamp using the session key 1780 * and validate it. 1781 */ 1782 #ifdef NFSKERB 1783 XXX 1784 #endif 1785 1786 tvout.tv_sec = fxdr_unsigned(long, tvout.tv_sec); 1787 tvout.tv_usec = fxdr_unsigned(long, tvout.tv_usec); 1788 if (nuidp->nu_expire < time.tv_sec || 1789 nuidp->nu_timestamp.tv_sec > tvout.tv_sec || 1790 (nuidp->nu_timestamp.tv_sec == tvout.tv_sec && 1791 nuidp->nu_timestamp.tv_usec > tvout.tv_usec)) { 1792 nuidp->nu_expire = 0; 1793 nd->nd_repstat = 1794 (NFSERR_AUTHERR|AUTH_REJECTVERF); 1795 nd->nd_procnum = NFSPROC_NOOP; 1796 return (0); 1797 } 1798 nfsrv_setcred(&nuidp->nu_cr, &nd->nd_cr); 1799 nd->nd_flag |= ND_KERBNICK; 1800 }; 1801 } else { 1802 nd->nd_repstat = (NFSERR_AUTHERR | AUTH_REJECTCRED); 1803 nd->nd_procnum = NFSPROC_NOOP; 1804 return (0); 1805 } 1806 1807 /* 1808 * For nqnfs, get piggybacked lease request. 1809 */ 1810 if (nqnfs && nd->nd_procnum != NQNFSPROC_EVICTED) { 1811 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); 1812 nd->nd_flag |= fxdr_unsigned(int, *tl); 1813 if (nd->nd_flag & ND_LEASE) { 1814 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); 1815 nd->nd_duration = fxdr_unsigned(u_int32_t, *tl); 1816 } else 1817 nd->nd_duration = NQ_MINLEASE; 1818 } else 1819 nd->nd_duration = NQ_MINLEASE; 1820 nd->nd_md = md; 1821 nd->nd_dpos = dpos; 1822 return (0); 1823 nfsmout: 1824 return (error); 1825 } 1826 1827 int 1828 nfs_msg(p, server, msg) 1829 struct proc *p; 1830 char *server, *msg; 1831 { 1832 tpr_t tpr; 1833 1834 if (p) 1835 tpr = tprintf_open(p); 1836 else 1837 tpr = NULL; 1838 tprintf(tpr, "nfs server %s: %s\n", server, msg); 1839 tprintf_close(tpr); 1840 return (0); 1841 } 1842 1843 #ifdef NFSSERVER 1844 int (*nfsrv3_procs[NFS_NPROCS]) __P((struct nfsrv_descript *, 1845 struct nfssvc_sock *, struct proc *, 1846 struct mbuf **)) = { 1847 nfsrv_null, 1848 nfsrv_getattr, 1849 nfsrv_setattr, 1850 nfsrv_lookup, 1851 nfsrv3_access, 1852 nfsrv_readlink, 1853 nfsrv_read, 1854 nfsrv_write, 1855 nfsrv_create, 1856 nfsrv_mkdir, 1857 nfsrv_symlink, 1858 nfsrv_mknod, 1859 nfsrv_remove, 1860 nfsrv_rmdir, 1861 nfsrv_rename, 1862 nfsrv_link, 1863 nfsrv_readdir, 1864 nfsrv_readdirplus, 1865 nfsrv_statfs, 1866 nfsrv_fsinfo, 1867 nfsrv_pathconf, 1868 nfsrv_commit, 1869 nqnfsrv_getlease, 1870 nqnfsrv_vacated, 1871 nfsrv_noop, 1872 nfsrv_noop 1873 }; 1874 1875 /* 1876 * Socket upcall routine for the nfsd sockets. 1877 * The caddr_t arg is a pointer to the "struct nfssvc_sock". 1878 * Essentially do as much as possible non-blocking, else punt and it will 1879 * be called with M_WAIT from an nfsd. 1880 */ 1881 void 1882 nfsrv_rcv(so, arg, waitflag) 1883 struct socket *so; 1884 caddr_t arg; 1885 int waitflag; 1886 { 1887 register struct nfssvc_sock *slp = (struct nfssvc_sock *)arg; 1888 register struct mbuf *m; 1889 struct mbuf *mp, *nam; 1890 struct uio auio; 1891 int flags, error; 1892 1893 if ((slp->ns_flag & SLP_VALID) == 0) 1894 return; 1895 #ifdef notdef 1896 /* 1897 * Define this to test for nfsds handling this under heavy load. 1898 */ 1899 if (waitflag == M_DONTWAIT) { 1900 slp->ns_flag |= SLP_NEEDQ; goto dorecs; 1901 } 1902 #endif 1903 auio.uio_procp = NULL; 1904 if (so->so_type == SOCK_STREAM) { 1905 /* 1906 * If there are already records on the queue, defer soreceive() 1907 * to an nfsd so that there is feedback to the TCP layer that 1908 * the nfs servers are heavily loaded. 1909 */ 1910 if (slp->ns_rec && waitflag == M_DONTWAIT) { 1911 slp->ns_flag |= SLP_NEEDQ; 1912 goto dorecs; 1913 } 1914 1915 /* 1916 * Do soreceive(). 1917 */ 1918 auio.uio_resid = 1000000000; 1919 flags = MSG_DONTWAIT; 1920 error = soreceive(so, &nam, &auio, &mp, (struct mbuf **)0, &flags); 1921 if (error || mp == (struct mbuf *)0) { 1922 if (error == EWOULDBLOCK) 1923 slp->ns_flag |= SLP_NEEDQ; 1924 else 1925 slp->ns_flag |= SLP_DISCONN; 1926 goto dorecs; 1927 } 1928 m = mp; 1929 if (slp->ns_rawend) { 1930 slp->ns_rawend->m_next = m; 1931 slp->ns_cc += 1000000000 - auio.uio_resid; 1932 } else { 1933 slp->ns_raw = m; 1934 slp->ns_cc = 1000000000 - auio.uio_resid; 1935 } 1936 while (m->m_next) 1937 m = m->m_next; 1938 slp->ns_rawend = m; 1939 1940 /* 1941 * Now try and parse record(s) out of the raw stream data. 1942 */ 1943 error = nfsrv_getstream(slp, waitflag); 1944 if (error) { 1945 if (error == EPERM) 1946 slp->ns_flag |= SLP_DISCONN; 1947 else 1948 slp->ns_flag |= SLP_NEEDQ; 1949 } 1950 } else { 1951 do { 1952 auio.uio_resid = 1000000000; 1953 flags = MSG_DONTWAIT; 1954 error = soreceive(so, &nam, &auio, &mp, 1955 (struct mbuf **)0, &flags); 1956 if (mp) { 1957 nfs_realign(mp, 10 * NFSX_UNSIGNED); 1958 if (nam) { 1959 m = nam; 1960 m->m_next = mp; 1961 } else 1962 m = mp; 1963 if (slp->ns_recend) 1964 slp->ns_recend->m_nextpkt = m; 1965 else 1966 slp->ns_rec = m; 1967 slp->ns_recend = m; 1968 m->m_nextpkt = (struct mbuf *)0; 1969 } 1970 if (error) { 1971 if ((so->so_proto->pr_flags & PR_CONNREQUIRED) 1972 && error != EWOULDBLOCK) { 1973 slp->ns_flag |= SLP_DISCONN; 1974 goto dorecs; 1975 } 1976 } 1977 } while (mp); 1978 } 1979 1980 /* 1981 * Now try and process the request records, non-blocking. 1982 */ 1983 dorecs: 1984 if (waitflag == M_DONTWAIT && 1985 (slp->ns_rec || (slp->ns_flag & (SLP_NEEDQ | SLP_DISCONN)))) 1986 nfsrv_wakenfsd(slp); 1987 } 1988 1989 /* 1990 * Try and extract an RPC request from the mbuf data list received on a 1991 * stream socket. The "waitflag" argument indicates whether or not it 1992 * can sleep. 1993 */ 1994 int 1995 nfsrv_getstream(slp, waitflag) 1996 register struct nfssvc_sock *slp; 1997 int waitflag; 1998 { 1999 register struct mbuf *m, **mpp; 2000 register char *cp1, *cp2; 2001 register int len; 2002 struct mbuf *om, *m2, *recm = NULL; 2003 u_int32_t recmark; 2004 2005 if (slp->ns_flag & SLP_GETSTREAM) 2006 panic("nfs getstream"); 2007 slp->ns_flag |= SLP_GETSTREAM; 2008 for (;;) { 2009 if (slp->ns_reclen == 0) { 2010 if (slp->ns_cc < NFSX_UNSIGNED) { 2011 slp->ns_flag &= ~SLP_GETSTREAM; 2012 return (0); 2013 } 2014 m = slp->ns_raw; 2015 if (m->m_len >= NFSX_UNSIGNED) { 2016 bcopy(mtod(m, caddr_t), (caddr_t)&recmark, NFSX_UNSIGNED); 2017 m->m_data += NFSX_UNSIGNED; 2018 m->m_len -= NFSX_UNSIGNED; 2019 } else { 2020 cp1 = (caddr_t)&recmark; 2021 cp2 = mtod(m, caddr_t); 2022 while (cp1 < ((caddr_t)&recmark) + NFSX_UNSIGNED) { 2023 while (m->m_len == 0) { 2024 m = m->m_next; 2025 cp2 = mtod(m, caddr_t); 2026 } 2027 *cp1++ = *cp2++; 2028 m->m_data++; 2029 m->m_len--; 2030 } 2031 } 2032 slp->ns_cc -= NFSX_UNSIGNED; 2033 recmark = ntohl(recmark); 2034 slp->ns_reclen = recmark & ~0x80000000; 2035 if (recmark & 0x80000000) 2036 slp->ns_flag |= SLP_LASTFRAG; 2037 else 2038 slp->ns_flag &= ~SLP_LASTFRAG; 2039 if (slp->ns_reclen > NFS_MAXPACKET) { 2040 slp->ns_flag &= ~SLP_GETSTREAM; 2041 return (EPERM); 2042 } 2043 } 2044 2045 /* 2046 * Now get the record part. 2047 */ 2048 if (slp->ns_cc == slp->ns_reclen) { 2049 recm = slp->ns_raw; 2050 slp->ns_raw = slp->ns_rawend = (struct mbuf *)0; 2051 slp->ns_cc = slp->ns_reclen = 0; 2052 } else if (slp->ns_cc > slp->ns_reclen) { 2053 len = 0; 2054 m = slp->ns_raw; 2055 om = (struct mbuf *)0; 2056 while (len < slp->ns_reclen) { 2057 if ((len + m->m_len) > slp->ns_reclen) { 2058 m2 = m_copym(m, 0, slp->ns_reclen - len, 2059 waitflag); 2060 if (m2) { 2061 if (om) { 2062 om->m_next = m2; 2063 recm = slp->ns_raw; 2064 } else 2065 recm = m2; 2066 m->m_data += slp->ns_reclen - len; 2067 m->m_len -= slp->ns_reclen - len; 2068 len = slp->ns_reclen; 2069 } else { 2070 slp->ns_flag &= ~SLP_GETSTREAM; 2071 return (EWOULDBLOCK); 2072 } 2073 } else if ((len + m->m_len) == slp->ns_reclen) { 2074 om = m; 2075 len += m->m_len; 2076 m = m->m_next; 2077 recm = slp->ns_raw; 2078 om->m_next = (struct mbuf *)0; 2079 } else { 2080 om = m; 2081 len += m->m_len; 2082 m = m->m_next; 2083 } 2084 } 2085 slp->ns_raw = m; 2086 slp->ns_cc -= len; 2087 slp->ns_reclen = 0; 2088 } else { 2089 slp->ns_flag &= ~SLP_GETSTREAM; 2090 return (0); 2091 } 2092 2093 /* 2094 * Accumulate the fragments into a record. 2095 */ 2096 mpp = &slp->ns_frag; 2097 while (*mpp) 2098 mpp = &((*mpp)->m_next); 2099 *mpp = recm; 2100 if (slp->ns_flag & SLP_LASTFRAG) { 2101 nfs_realign(slp->ns_frag, 10 * NFSX_UNSIGNED); 2102 if (slp->ns_recend) 2103 slp->ns_recend->m_nextpkt = slp->ns_frag; 2104 else 2105 slp->ns_rec = slp->ns_frag; 2106 slp->ns_recend = slp->ns_frag; 2107 slp->ns_frag = (struct mbuf *)0; 2108 } 2109 } 2110 } 2111 2112 /* 2113 * Parse an RPC header. 2114 */ 2115 int 2116 nfsrv_dorec(slp, nfsd, ndp) 2117 register struct nfssvc_sock *slp; 2118 struct nfsd *nfsd; 2119 struct nfsrv_descript **ndp; 2120 { 2121 register struct mbuf *m, *nam; 2122 register struct nfsrv_descript *nd; 2123 int error; 2124 2125 *ndp = NULL; 2126 if ((slp->ns_flag & SLP_VALID) == 0 || 2127 (m = slp->ns_rec) == (struct mbuf *)0) 2128 return (ENOBUFS); 2129 slp->ns_rec = m->m_nextpkt; 2130 if (slp->ns_rec) 2131 m->m_nextpkt = (struct mbuf *)0; 2132 else 2133 slp->ns_recend = (struct mbuf *)0; 2134 if (m->m_type == MT_SONAME) { 2135 nam = m; 2136 m = m->m_next; 2137 nam->m_next = NULL; 2138 } else 2139 nam = NULL; 2140 MALLOC(nd, struct nfsrv_descript *, sizeof (struct nfsrv_descript), 2141 M_NFSRVDESC, M_WAITOK); 2142 nd->nd_md = nd->nd_mrep = m; 2143 nd->nd_nam2 = nam; 2144 nd->nd_dpos = mtod(m, caddr_t); 2145 error = nfs_getreq(nd, nfsd, TRUE); 2146 if (error) { 2147 m_freem(nam); 2148 free((caddr_t)nd, M_NFSRVDESC); 2149 return (error); 2150 } 2151 *ndp = nd; 2152 nfsd->nfsd_nd = nd; 2153 return (0); 2154 } 2155 2156 2157 /* 2158 * Search for a sleeping nfsd and wake it up. 2159 * SIDE EFFECT: If none found, set NFSD_CHECKSLP flag, so that one of the 2160 * running nfsds will go look for the work in the nfssvc_sock list. 2161 */ 2162 void 2163 nfsrv_wakenfsd(slp) 2164 struct nfssvc_sock *slp; 2165 { 2166 register struct nfsd *nd; 2167 2168 if ((slp->ns_flag & SLP_VALID) == 0) 2169 return; 2170 for (nd = nfsd_head.tqh_first; nd != 0; nd = nd->nfsd_chain.tqe_next) { 2171 if (nd->nfsd_flag & NFSD_WAITING) { 2172 nd->nfsd_flag &= ~NFSD_WAITING; 2173 if (nd->nfsd_slp) 2174 panic("nfsd wakeup"); 2175 slp->ns_sref++; 2176 nd->nfsd_slp = slp; 2177 wakeup((caddr_t)nd); 2178 return; 2179 } 2180 } 2181 slp->ns_flag |= SLP_DOREC; 2182 nfsd_head_flag |= NFSD_CHECKSLP; 2183 } 2184 #endif /* NFSSERVER */ 2185