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