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