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