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