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