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