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