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