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