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