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