1 /* $NetBSD: nfs_socket.c,v 1.136 2006/06/30 09:55:06 yamt Exp $ */ 2 3 /* 4 * Copyright (c) 1989, 1991, 1993, 1995 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * Rick Macklem at The University of Guelph. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * @(#)nfs_socket.c 8.5 (Berkeley) 3/30/95 35 */ 36 37 /* 38 * Socket operations for use by nfs 39 */ 40 41 #include <sys/cdefs.h> 42 __KERNEL_RCSID(0, "$NetBSD: nfs_socket.c,v 1.136 2006/06/30 09:55:06 yamt Exp $"); 43 44 #include "fs_nfs.h" 45 #include "opt_nfs.h" 46 #include "opt_nfsserver.h" 47 #include "opt_mbuftrace.h" 48 #include "opt_inet.h" 49 50 #include <sys/param.h> 51 #include <sys/systm.h> 52 #include <sys/callout.h> 53 #include <sys/proc.h> 54 #include <sys/mount.h> 55 #include <sys/kernel.h> 56 #include <sys/mbuf.h> 57 #include <sys/vnode.h> 58 #include <sys/domain.h> 59 #include <sys/protosw.h> 60 #include <sys/socket.h> 61 #include <sys/socketvar.h> 62 #include <sys/syslog.h> 63 #include <sys/tprintf.h> 64 #include <sys/namei.h> 65 #include <sys/signal.h> 66 #include <sys/signalvar.h> 67 #include <sys/kauth.h> 68 69 #include <netinet/in.h> 70 #include <netinet/tcp.h> 71 72 #include <nfs/rpcv2.h> 73 #include <nfs/nfsproto.h> 74 #include <nfs/nfs.h> 75 #include <nfs/xdr_subs.h> 76 #include <nfs/nfsm_subs.h> 77 #include <nfs/nfsmount.h> 78 #include <nfs/nfsnode.h> 79 #include <nfs/nfsrtt.h> 80 #include <nfs/nqnfs.h> 81 #include <nfs/nfs_var.h> 82 83 MALLOC_DEFINE(M_NFSREQ, "NFS req", "NFS request header"); 84 #ifdef MBUFTRACE 85 struct mowner nfs_mowner = { "nfs" }; 86 #endif 87 88 /* 89 * Estimate rto for an nfs rpc sent via. an unreliable datagram. 90 * Use the mean and mean deviation of rtt for the appropriate type of rpc 91 * for the frequent rpcs and a default for the others. 92 * The justification for doing "other" this way is that these rpcs 93 * happen so infrequently that timer est. would probably be stale. 94 * Also, since many of these rpcs are 95 * non-idempotent, a conservative timeout is desired. 96 * getattr, lookup - A+2D 97 * read, write - A+4D 98 * other - nm_timeo 99 */ 100 #define NFS_RTO(n, t) \ 101 ((t) == 0 ? (n)->nm_timeo : \ 102 ((t) < 3 ? \ 103 (((((n)->nm_srtt[t-1] + 3) >> 2) + (n)->nm_sdrtt[t-1] + 1) >> 1) : \ 104 ((((n)->nm_srtt[t-1] + 7) >> 3) + (n)->nm_sdrtt[t-1] + 1))) 105 #define NFS_SRTT(r) (r)->r_nmp->nm_srtt[proct[(r)->r_procnum] - 1] 106 #define NFS_SDRTT(r) (r)->r_nmp->nm_sdrtt[proct[(r)->r_procnum] - 1] 107 /* 108 * External data, mostly RPC constants in XDR form 109 */ 110 extern u_int32_t rpc_reply, rpc_msgdenied, rpc_mismatch, rpc_vers, 111 rpc_auth_unix, rpc_msgaccepted, rpc_call, rpc_autherr, 112 rpc_auth_kerb; 113 extern u_int32_t nfs_prog, nqnfs_prog; 114 extern time_t nqnfsstarttime; 115 extern const int nfsv3_procid[NFS_NPROCS]; 116 extern int nfs_ticks; 117 118 /* 119 * Defines which timer to use for the procnum. 120 * 0 - default 121 * 1 - getattr 122 * 2 - lookup 123 * 3 - read 124 * 4 - write 125 */ 126 static const int proct[NFS_NPROCS] = { 127 [NFSPROC_NULL] = 0, 128 [NFSPROC_GETATTR] = 1, 129 [NFSPROC_SETATTR] = 0, 130 [NFSPROC_LOOKUP] = 2, 131 [NFSPROC_ACCESS] = 1, 132 [NFSPROC_READLINK] = 3, 133 [NFSPROC_READ] = 3, 134 [NFSPROC_WRITE] = 4, 135 [NFSPROC_CREATE] = 0, 136 [NFSPROC_MKDIR] = 0, 137 [NFSPROC_SYMLINK] = 0, 138 [NFSPROC_MKNOD] = 0, 139 [NFSPROC_REMOVE] = 0, 140 [NFSPROC_RMDIR] = 0, 141 [NFSPROC_RENAME] = 0, 142 [NFSPROC_LINK] = 0, 143 [NFSPROC_READDIR] = 3, 144 [NFSPROC_READDIRPLUS] = 3, 145 [NFSPROC_FSSTAT] = 0, 146 [NFSPROC_FSINFO] = 0, 147 [NFSPROC_PATHCONF] = 0, 148 [NFSPROC_COMMIT] = 0, 149 [NQNFSPROC_GETLEASE] = 0, 150 [NQNFSPROC_VACATED] = 0, 151 [NQNFSPROC_EVICTED] = 0, 152 [NFSPROC_NOOP] = 0, 153 }; 154 155 /* 156 * There is a congestion window for outstanding rpcs maintained per mount 157 * point. The cwnd size is adjusted in roughly the way that: 158 * Van Jacobson, Congestion avoidance and Control, In "Proceedings of 159 * SIGCOMM '88". ACM, August 1988. 160 * describes for TCP. The cwnd size is chopped in half on a retransmit timeout 161 * and incremented by 1/cwnd when each rpc reply is received and a full cwnd 162 * of rpcs is in progress. 163 * (The sent count and cwnd are scaled for integer arith.) 164 * Variants of "slow start" were tried and were found to be too much of a 165 * performance hit (ave. rtt 3 times larger), 166 * I suspect due to the large rtt that nfs rpcs have. 167 */ 168 #define NFS_CWNDSCALE 256 169 #define NFS_MAXCWND (NFS_CWNDSCALE * 32) 170 static const int nfs_backoff[8] = { 2, 4, 8, 16, 32, 64, 128, 256, }; 171 int nfsrtton = 0; 172 struct nfsrtt nfsrtt; 173 struct nfsreqhead nfs_reqq; 174 175 struct callout nfs_timer_ch = CALLOUT_INITIALIZER_SETFUNC(nfs_timer, NULL); 176 177 /* 178 * Initialize sockets and congestion for a new NFS connection. 179 * We do not free the sockaddr if error. 180 */ 181 int 182 nfs_connect(nmp, rep, l) 183 struct nfsmount *nmp; 184 struct nfsreq *rep; 185 struct lwp *l; 186 { 187 struct socket *so; 188 int s, error, rcvreserve, sndreserve; 189 struct sockaddr *saddr; 190 struct sockaddr_in *sin; 191 #ifdef INET6 192 struct sockaddr_in6 *sin6; 193 #endif 194 struct mbuf *m; 195 196 nmp->nm_so = (struct socket *)0; 197 saddr = mtod(nmp->nm_nam, struct sockaddr *); 198 error = socreate(saddr->sa_family, &nmp->nm_so, 199 nmp->nm_sotype, nmp->nm_soproto, l); 200 if (error) 201 goto bad; 202 so = nmp->nm_so; 203 #ifdef MBUFTRACE 204 so->so_mowner = &nfs_mowner; 205 so->so_rcv.sb_mowner = &nfs_mowner; 206 so->so_snd.sb_mowner = &nfs_mowner; 207 #endif 208 nmp->nm_soflags = so->so_proto->pr_flags; 209 210 /* 211 * Some servers require that the client port be a reserved port number. 212 */ 213 if (saddr->sa_family == AF_INET && (nmp->nm_flag & NFSMNT_RESVPORT)) { 214 m = m_get(M_WAIT, MT_SOOPTS); 215 MCLAIM(m, so->so_mowner); 216 *mtod(m, int32_t *) = IP_PORTRANGE_LOW; 217 m->m_len = sizeof(int32_t); 218 if ((error = sosetopt(so, IPPROTO_IP, IP_PORTRANGE, m))) 219 goto bad; 220 m = m_get(M_WAIT, MT_SONAME); 221 MCLAIM(m, so->so_mowner); 222 sin = mtod(m, struct sockaddr_in *); 223 sin->sin_len = m->m_len = sizeof (struct sockaddr_in); 224 sin->sin_family = AF_INET; 225 sin->sin_addr.s_addr = INADDR_ANY; 226 sin->sin_port = 0; 227 error = sobind(so, m, &lwp0); 228 m_freem(m); 229 if (error) 230 goto bad; 231 } 232 #ifdef INET6 233 if (saddr->sa_family == AF_INET6 && (nmp->nm_flag & NFSMNT_RESVPORT)) { 234 m = m_get(M_WAIT, MT_SOOPTS); 235 MCLAIM(m, so->so_mowner); 236 *mtod(m, int32_t *) = IPV6_PORTRANGE_LOW; 237 m->m_len = sizeof(int32_t); 238 if ((error = sosetopt(so, IPPROTO_IPV6, IPV6_PORTRANGE, m))) 239 goto bad; 240 m = m_get(M_WAIT, MT_SONAME); 241 MCLAIM(m, so->so_mowner); 242 sin6 = mtod(m, struct sockaddr_in6 *); 243 sin6->sin6_len = m->m_len = sizeof (struct sockaddr_in6); 244 sin6->sin6_family = AF_INET6; 245 sin6->sin6_addr = in6addr_any; 246 sin6->sin6_port = 0; 247 error = sobind(so, m, &lwp0); 248 m_freem(m); 249 if (error) 250 goto bad; 251 } 252 #endif 253 254 /* 255 * Protocols that do not require connections may be optionally left 256 * unconnected for servers that reply from a port other than NFS_PORT. 257 */ 258 if (nmp->nm_flag & NFSMNT_NOCONN) { 259 if (nmp->nm_soflags & PR_CONNREQUIRED) { 260 error = ENOTCONN; 261 goto bad; 262 } 263 } else { 264 error = soconnect(so, nmp->nm_nam, l); 265 if (error) 266 goto bad; 267 268 /* 269 * Wait for the connection to complete. Cribbed from the 270 * connect system call but with the wait timing out so 271 * that interruptible mounts don't hang here for a long time. 272 */ 273 s = splsoftnet(); 274 while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) { 275 (void) tsleep((caddr_t)&so->so_timeo, PSOCK, 276 "nfscn1", 2 * hz); 277 if ((so->so_state & SS_ISCONNECTING) && 278 so->so_error == 0 && rep && 279 (error = nfs_sigintr(nmp, rep, rep->r_lwp)) != 0){ 280 so->so_state &= ~SS_ISCONNECTING; 281 splx(s); 282 goto bad; 283 } 284 } 285 if (so->so_error) { 286 error = so->so_error; 287 so->so_error = 0; 288 splx(s); 289 goto bad; 290 } 291 splx(s); 292 } 293 if (nmp->nm_flag & (NFSMNT_SOFT | NFSMNT_INT)) { 294 so->so_rcv.sb_timeo = (5 * hz); 295 so->so_snd.sb_timeo = (5 * hz); 296 } else { 297 /* 298 * enable receive timeout to detect server crash and reconnect. 299 * otherwise, we can be stuck in soreceive forever. 300 */ 301 so->so_rcv.sb_timeo = (5 * hz); 302 so->so_snd.sb_timeo = 0; 303 } 304 if (nmp->nm_sotype == SOCK_DGRAM) { 305 sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR) * 2; 306 rcvreserve = (max(nmp->nm_rsize, nmp->nm_readdirsize) + 307 NFS_MAXPKTHDR) * 2; 308 } else if (nmp->nm_sotype == SOCK_SEQPACKET) { 309 sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR) * 2; 310 rcvreserve = (max(nmp->nm_rsize, nmp->nm_readdirsize) + 311 NFS_MAXPKTHDR) * 2; 312 } else { 313 if (nmp->nm_sotype != SOCK_STREAM) 314 panic("nfscon sotype"); 315 if (so->so_proto->pr_flags & PR_CONNREQUIRED) { 316 m = m_get(M_WAIT, MT_SOOPTS); 317 MCLAIM(m, so->so_mowner); 318 *mtod(m, int32_t *) = 1; 319 m->m_len = sizeof(int32_t); 320 sosetopt(so, SOL_SOCKET, SO_KEEPALIVE, m); 321 } 322 if (so->so_proto->pr_protocol == IPPROTO_TCP) { 323 m = m_get(M_WAIT, MT_SOOPTS); 324 MCLAIM(m, so->so_mowner); 325 *mtod(m, int32_t *) = 1; 326 m->m_len = sizeof(int32_t); 327 sosetopt(so, IPPROTO_TCP, TCP_NODELAY, m); 328 } 329 sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR + 330 sizeof (u_int32_t)) * 2; 331 rcvreserve = (nmp->nm_rsize + NFS_MAXPKTHDR + 332 sizeof (u_int32_t)) * 2; 333 } 334 error = soreserve(so, sndreserve, rcvreserve); 335 if (error) 336 goto bad; 337 so->so_rcv.sb_flags |= SB_NOINTR; 338 so->so_snd.sb_flags |= SB_NOINTR; 339 340 /* Initialize other non-zero congestion variables */ 341 nmp->nm_srtt[0] = nmp->nm_srtt[1] = nmp->nm_srtt[2] = nmp->nm_srtt[3] = 342 NFS_TIMEO << 3; 343 nmp->nm_sdrtt[0] = nmp->nm_sdrtt[1] = nmp->nm_sdrtt[2] = 344 nmp->nm_sdrtt[3] = 0; 345 nmp->nm_cwnd = NFS_MAXCWND / 2; /* Initial send window */ 346 nmp->nm_sent = 0; 347 nmp->nm_timeouts = 0; 348 return (0); 349 350 bad: 351 nfs_disconnect(nmp); 352 return (error); 353 } 354 355 /* 356 * Reconnect routine: 357 * Called when a connection is broken on a reliable protocol. 358 * - clean up the old socket 359 * - nfs_connect() again 360 * - set R_MUSTRESEND for all outstanding requests on mount point 361 * If this fails the mount point is DEAD! 362 * nb: Must be called with the nfs_sndlock() set on the mount point. 363 */ 364 int 365 nfs_reconnect(rep, l) 366 struct nfsreq *rep; 367 struct lwp *l; 368 { 369 struct nfsreq *rp; 370 struct nfsmount *nmp = rep->r_nmp; 371 int error; 372 373 nfs_disconnect(nmp); 374 while ((error = nfs_connect(nmp, rep, l)) != 0) { 375 if (error == EINTR || error == ERESTART) 376 return (EINTR); 377 (void) tsleep((caddr_t)&lbolt, PSOCK, "nfscn2", 0); 378 } 379 380 /* 381 * Loop through outstanding request list and fix up all requests 382 * on old socket. 383 */ 384 TAILQ_FOREACH(rp, &nfs_reqq, r_chain) { 385 if (rp->r_nmp == nmp) { 386 if ((rp->r_flags & R_MUSTRESEND) == 0) 387 rp->r_flags |= R_MUSTRESEND | R_REXMITTED; 388 rp->r_rexmit = 0; 389 } 390 } 391 return (0); 392 } 393 394 /* 395 * NFS disconnect. Clean up and unlink. 396 */ 397 void 398 nfs_disconnect(nmp) 399 struct nfsmount *nmp; 400 { 401 struct socket *so; 402 int drain = 0; 403 404 if (nmp->nm_so) { 405 so = nmp->nm_so; 406 nmp->nm_so = (struct socket *)0; 407 soshutdown(so, 2); 408 drain = (nmp->nm_iflag & NFSMNT_DISMNT) != 0; 409 if (drain) { 410 /* 411 * soshutdown() above should wake up the current 412 * listener. 413 * Now wake up those waiting for the receive lock, and 414 * wait for them to go away unhappy, to prevent *nmp 415 * from evaporating while they're sleeping. 416 */ 417 while (nmp->nm_waiters > 0) { 418 wakeup (&nmp->nm_iflag); 419 (void) tsleep(&nmp->nm_waiters, PVFS, 420 "nfsdis", 0); 421 } 422 } 423 soclose(so); 424 } 425 #ifdef DIAGNOSTIC 426 if (drain && (nmp->nm_waiters > 0)) 427 panic("nfs_disconnect: waiters left after drain?"); 428 #endif 429 } 430 431 void 432 nfs_safedisconnect(nmp) 433 struct nfsmount *nmp; 434 { 435 struct nfsreq dummyreq; 436 437 memset(&dummyreq, 0, sizeof(dummyreq)); 438 dummyreq.r_nmp = nmp; 439 nfs_rcvlock(&dummyreq); /* XXX ignored error return */ 440 nfs_disconnect(nmp); 441 nfs_rcvunlock(nmp); 442 } 443 444 /* 445 * This is the nfs send routine. For connection based socket types, it 446 * must be called with an nfs_sndlock() on the socket. 447 * "rep == NULL" indicates that it has been called from a server. 448 * For the client side: 449 * - return EINTR if the RPC is terminated, 0 otherwise 450 * - set R_MUSTRESEND if the send fails for any reason 451 * - do any cleanup required by recoverable socket errors (? ? ?) 452 * For the server side: 453 * - return EINTR or ERESTART if interrupted by a signal 454 * - return EPIPE if a connection is lost for connection based sockets (TCP...) 455 * - do any cleanup required by recoverable socket errors (? ? ?) 456 */ 457 int 458 nfs_send(so, nam, top, rep, l) 459 struct socket *so; 460 struct mbuf *nam; 461 struct mbuf *top; 462 struct nfsreq *rep; 463 struct lwp *l; 464 { 465 struct mbuf *sendnam; 466 int error, soflags, flags; 467 468 /* XXX nfs_doio()/nfs_request() calls with rep->r_lwp == NULL */ 469 if (l == NULL && rep->r_lwp == NULL) 470 l = curlwp; 471 472 if (rep) { 473 if (rep->r_flags & R_SOFTTERM) { 474 m_freem(top); 475 return (EINTR); 476 } 477 if ((so = rep->r_nmp->nm_so) == NULL) { 478 rep->r_flags |= R_MUSTRESEND; 479 m_freem(top); 480 return (0); 481 } 482 rep->r_flags &= ~R_MUSTRESEND; 483 soflags = rep->r_nmp->nm_soflags; 484 } else 485 soflags = so->so_proto->pr_flags; 486 if ((soflags & PR_CONNREQUIRED) || (so->so_state & SS_ISCONNECTED)) 487 sendnam = (struct mbuf *)0; 488 else 489 sendnam = nam; 490 if (so->so_type == SOCK_SEQPACKET) 491 flags = MSG_EOR; 492 else 493 flags = 0; 494 495 error = (*so->so_send)(so, sendnam, (struct uio *)0, top, 496 (struct mbuf *)0, flags, l); 497 if (error) { 498 if (rep) { 499 if (error == ENOBUFS && so->so_type == SOCK_DGRAM) { 500 /* 501 * We're too fast for the network/driver, 502 * and UDP isn't flowcontrolled. 503 * We need to resend. This is not fatal, 504 * just try again. 505 * 506 * Could be smarter here by doing some sort 507 * of a backoff, but this is rare. 508 */ 509 rep->r_flags |= R_MUSTRESEND; 510 } else { 511 if (error != EPIPE) 512 log(LOG_INFO, 513 "nfs send error %d for %s\n", 514 error, 515 rep->r_nmp->nm_mountp-> 516 mnt_stat.f_mntfromname); 517 /* 518 * Deal with errors for the client side. 519 */ 520 if (rep->r_flags & R_SOFTTERM) 521 error = EINTR; 522 else 523 rep->r_flags |= R_MUSTRESEND; 524 } 525 } else { 526 /* 527 * See above. This error can happen under normal 528 * circumstances and the log is too noisy. 529 * The error will still show up in nfsstat. 530 */ 531 if (error != ENOBUFS || so->so_type != SOCK_DGRAM) 532 log(LOG_INFO, "nfsd send error %d\n", error); 533 } 534 535 /* 536 * Handle any recoverable (soft) socket errors here. (? ? ?) 537 */ 538 if (error != EINTR && error != ERESTART && 539 error != EWOULDBLOCK && error != EPIPE) 540 error = 0; 541 } 542 return (error); 543 } 544 545 #ifdef NFS 546 /* 547 * Receive a Sun RPC Request/Reply. For SOCK_DGRAM, the work is all 548 * done by soreceive(), but for SOCK_STREAM we must deal with the Record 549 * Mark and consolidate the data into a new mbuf list. 550 * nb: Sometimes TCP passes the data up to soreceive() in long lists of 551 * small mbufs. 552 * For SOCK_STREAM we must be very careful to read an entire record once 553 * we have read any of it, even if the system call has been interrupted. 554 */ 555 int 556 nfs_receive(rep, aname, mp, l) 557 struct nfsreq *rep; 558 struct mbuf **aname; 559 struct mbuf **mp; 560 struct lwp *l; 561 { 562 struct socket *so; 563 struct uio auio; 564 struct iovec aio; 565 struct mbuf *m; 566 struct mbuf *control; 567 u_int32_t len; 568 struct mbuf **getnam; 569 int error, sotype, rcvflg; 570 571 /* 572 * Set up arguments for soreceive() 573 */ 574 *mp = (struct mbuf *)0; 575 *aname = (struct mbuf *)0; 576 sotype = rep->r_nmp->nm_sotype; 577 578 /* 579 * For reliable protocols, lock against other senders/receivers 580 * in case a reconnect is necessary. 581 * For SOCK_STREAM, first get the Record Mark to find out how much 582 * more there is to get. 583 * We must lock the socket against other receivers 584 * until we have an entire rpc request/reply. 585 */ 586 if (sotype != SOCK_DGRAM) { 587 error = nfs_sndlock(&rep->r_nmp->nm_iflag, rep); 588 if (error) 589 return (error); 590 tryagain: 591 /* 592 * Check for fatal errors and resending request. 593 */ 594 /* 595 * Ugh: If a reconnect attempt just happened, nm_so 596 * would have changed. NULL indicates a failed 597 * attempt that has essentially shut down this 598 * mount point. 599 */ 600 if (rep->r_mrep || (rep->r_flags & R_SOFTTERM)) { 601 nfs_sndunlock(&rep->r_nmp->nm_iflag); 602 return (EINTR); 603 } 604 so = rep->r_nmp->nm_so; 605 if (!so) { 606 error = nfs_reconnect(rep, l); 607 if (error) { 608 nfs_sndunlock(&rep->r_nmp->nm_iflag); 609 return (error); 610 } 611 goto tryagain; 612 } 613 while (rep->r_flags & R_MUSTRESEND) { 614 m = m_copym(rep->r_mreq, 0, M_COPYALL, M_WAIT); 615 nfsstats.rpcretries++; 616 rep->r_rtt = 0; 617 rep->r_flags &= ~R_TIMING; 618 error = nfs_send(so, rep->r_nmp->nm_nam, m, rep, l); 619 if (error) { 620 if (error == EINTR || error == ERESTART || 621 (error = nfs_reconnect(rep, l)) != 0) { 622 nfs_sndunlock(&rep->r_nmp->nm_iflag); 623 return (error); 624 } 625 goto tryagain; 626 } 627 } 628 nfs_sndunlock(&rep->r_nmp->nm_iflag); 629 if (sotype == SOCK_STREAM) { 630 aio.iov_base = (caddr_t) &len; 631 aio.iov_len = sizeof(u_int32_t); 632 auio.uio_iov = &aio; 633 auio.uio_iovcnt = 1; 634 auio.uio_rw = UIO_READ; 635 auio.uio_offset = 0; 636 auio.uio_resid = sizeof(u_int32_t); 637 UIO_SETUP_SYSSPACE(&auio); 638 do { 639 rcvflg = MSG_WAITALL; 640 error = (*so->so_receive)(so, (struct mbuf **)0, &auio, 641 (struct mbuf **)0, (struct mbuf **)0, &rcvflg); 642 if (error == EWOULDBLOCK && rep) { 643 if (rep->r_flags & R_SOFTTERM) 644 return (EINTR); 645 /* 646 * if it seems that the server died after it 647 * received our request, set EPIPE so that 648 * we'll reconnect and retransmit requests. 649 */ 650 if (rep->r_rexmit >= rep->r_nmp->nm_retry) { 651 nfsstats.rpctimeouts++; 652 error = EPIPE; 653 } 654 } 655 } while (error == EWOULDBLOCK); 656 if (!error && auio.uio_resid > 0) { 657 /* 658 * Don't log a 0 byte receive; it means 659 * that the socket has been closed, and 660 * can happen during normal operation 661 * (forcible unmount or Solaris server). 662 */ 663 if (auio.uio_resid != sizeof (u_int32_t)) 664 log(LOG_INFO, 665 "short receive (%lu/%lu) from nfs server %s\n", 666 (u_long)sizeof(u_int32_t) - auio.uio_resid, 667 (u_long)sizeof(u_int32_t), 668 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname); 669 error = EPIPE; 670 } 671 if (error) 672 goto errout; 673 len = ntohl(len) & ~0x80000000; 674 /* 675 * This is SERIOUS! We are out of sync with the sender 676 * and forcing a disconnect/reconnect is all I can do. 677 */ 678 if (len > NFS_MAXPACKET) { 679 log(LOG_ERR, "%s (%d) from nfs server %s\n", 680 "impossible packet length", 681 len, 682 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname); 683 error = EFBIG; 684 goto errout; 685 } 686 auio.uio_resid = len; 687 do { 688 rcvflg = MSG_WAITALL; 689 error = (*so->so_receive)(so, (struct mbuf **)0, 690 &auio, mp, (struct mbuf **)0, &rcvflg); 691 } while (error == EWOULDBLOCK || error == EINTR || 692 error == ERESTART); 693 if (!error && auio.uio_resid > 0) { 694 if (len != auio.uio_resid) 695 log(LOG_INFO, 696 "short receive (%lu/%d) from nfs server %s\n", 697 (u_long)len - auio.uio_resid, len, 698 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname); 699 error = EPIPE; 700 } 701 } else { 702 /* 703 * NB: Since uio_resid is big, MSG_WAITALL is ignored 704 * and soreceive() will return when it has either a 705 * control msg or a data msg. 706 * We have no use for control msg., but must grab them 707 * and then throw them away so we know what is going 708 * on. 709 */ 710 auio.uio_resid = len = 100000000; /* Anything Big */ 711 /* not need to setup uio_vmspace */ 712 do { 713 rcvflg = 0; 714 error = (*so->so_receive)(so, (struct mbuf **)0, 715 &auio, mp, &control, &rcvflg); 716 if (control) 717 m_freem(control); 718 if (error == EWOULDBLOCK && rep) { 719 if (rep->r_flags & R_SOFTTERM) 720 return (EINTR); 721 } 722 } while (error == EWOULDBLOCK || 723 (!error && *mp == NULL && control)); 724 if ((rcvflg & MSG_EOR) == 0) 725 printf("Egad!!\n"); 726 if (!error && *mp == NULL) 727 error = EPIPE; 728 len -= auio.uio_resid; 729 } 730 errout: 731 if (error && error != EINTR && error != ERESTART) { 732 m_freem(*mp); 733 *mp = (struct mbuf *)0; 734 if (error != EPIPE) 735 log(LOG_INFO, 736 "receive error %d from nfs server %s\n", 737 error, 738 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname); 739 error = nfs_sndlock(&rep->r_nmp->nm_iflag, rep); 740 if (!error) 741 error = nfs_reconnect(rep, l); 742 if (!error) 743 goto tryagain; 744 else 745 nfs_sndunlock(&rep->r_nmp->nm_iflag); 746 } 747 } else { 748 if ((so = rep->r_nmp->nm_so) == NULL) 749 return (EACCES); 750 if (so->so_state & SS_ISCONNECTED) 751 getnam = (struct mbuf **)0; 752 else 753 getnam = aname; 754 auio.uio_resid = len = 1000000; 755 /* not need to setup uio_vmspace */ 756 do { 757 rcvflg = 0; 758 error = (*so->so_receive)(so, getnam, &auio, mp, 759 (struct mbuf **)0, &rcvflg); 760 if (error == EWOULDBLOCK && 761 (rep->r_flags & R_SOFTTERM)) 762 return (EINTR); 763 } while (error == EWOULDBLOCK); 764 len -= auio.uio_resid; 765 if (!error && *mp == NULL) 766 error = EPIPE; 767 } 768 if (error) { 769 m_freem(*mp); 770 *mp = (struct mbuf *)0; 771 } 772 return (error); 773 } 774 775 /* 776 * Implement receipt of reply on a socket. 777 * We must search through the list of received datagrams matching them 778 * with outstanding requests using the xid, until ours is found. 779 */ 780 /* ARGSUSED */ 781 int 782 nfs_reply(myrep, lwp) 783 struct nfsreq *myrep; 784 struct lwp *lwp; 785 { 786 struct nfsreq *rep; 787 struct nfsmount *nmp = myrep->r_nmp; 788 int32_t t1; 789 struct mbuf *mrep, *nam, *md; 790 u_int32_t rxid, *tl; 791 caddr_t dpos, cp2; 792 int error; 793 794 /* 795 * Loop around until we get our own reply 796 */ 797 for (;;) { 798 /* 799 * Lock against other receivers so that I don't get stuck in 800 * sbwait() after someone else has received my reply for me. 801 * Also necessary for connection based protocols to avoid 802 * race conditions during a reconnect. 803 */ 804 error = nfs_rcvlock(myrep); 805 if (error == EALREADY) 806 return (0); 807 if (error) 808 return (error); 809 /* 810 * Get the next Rpc reply off the socket 811 */ 812 nmp->nm_waiters++; 813 error = nfs_receive(myrep, &nam, &mrep, lwp); 814 nfs_rcvunlock(nmp); 815 if (error) { 816 817 if (nmp->nm_iflag & NFSMNT_DISMNT) { 818 /* 819 * Oops, we're going away now.. 820 */ 821 nmp->nm_waiters--; 822 wakeup (&nmp->nm_waiters); 823 return error; 824 } 825 nmp->nm_waiters--; 826 /* 827 * Ignore routing errors on connectionless protocols? ? 828 */ 829 if (NFSIGNORE_SOERROR(nmp->nm_soflags, error)) { 830 nmp->nm_so->so_error = 0; 831 #ifdef DEBUG 832 printf("nfs_reply: ignoring error %d\n", error); 833 #endif 834 if (myrep->r_flags & R_GETONEREP) 835 return (0); 836 continue; 837 } 838 return (error); 839 } 840 nmp->nm_waiters--; 841 if (nam) 842 m_freem(nam); 843 844 /* 845 * Get the xid and check that it is an rpc reply 846 */ 847 md = mrep; 848 dpos = mtod(md, caddr_t); 849 nfsm_dissect(tl, u_int32_t *, 2*NFSX_UNSIGNED); 850 rxid = *tl++; 851 if (*tl != rpc_reply) { 852 #ifndef NFS_V2_ONLY 853 if (nmp->nm_flag & NFSMNT_NQNFS) { 854 if (nqnfs_callback(nmp, mrep, md, dpos, 855 myrep->r_lwp)) 856 nfsstats.rpcinvalid++; 857 } else 858 #endif 859 { 860 nfsstats.rpcinvalid++; 861 m_freem(mrep); 862 } 863 nfsmout: 864 if (myrep->r_flags & R_GETONEREP) 865 return (0); 866 continue; 867 } 868 869 /* 870 * Loop through the request list to match up the reply 871 * Iff no match, just drop the datagram 872 */ 873 TAILQ_FOREACH(rep, &nfs_reqq, r_chain) { 874 if (rep->r_mrep == NULL && rxid == rep->r_xid) { 875 /* Found it.. */ 876 rep->r_mrep = mrep; 877 rep->r_md = md; 878 rep->r_dpos = dpos; 879 if (nfsrtton) { 880 struct rttl *rt; 881 882 rt = &nfsrtt.rttl[nfsrtt.pos]; 883 rt->proc = rep->r_procnum; 884 rt->rto = NFS_RTO(nmp, proct[rep->r_procnum]); 885 rt->sent = nmp->nm_sent; 886 rt->cwnd = nmp->nm_cwnd; 887 rt->srtt = nmp->nm_srtt[proct[rep->r_procnum] - 1]; 888 rt->sdrtt = nmp->nm_sdrtt[proct[rep->r_procnum] - 1]; 889 rt->fsid = nmp->nm_mountp->mnt_stat.f_fsidx; 890 getmicrotime(&rt->tstamp); 891 if (rep->r_flags & R_TIMING) 892 rt->rtt = rep->r_rtt; 893 else 894 rt->rtt = 1000000; 895 nfsrtt.pos = (nfsrtt.pos + 1) % NFSRTTLOGSIZ; 896 } 897 /* 898 * Update congestion window. 899 * Do the additive increase of 900 * one rpc/rtt. 901 */ 902 if (nmp->nm_cwnd <= nmp->nm_sent) { 903 nmp->nm_cwnd += 904 (NFS_CWNDSCALE * NFS_CWNDSCALE + 905 (nmp->nm_cwnd >> 1)) / nmp->nm_cwnd; 906 if (nmp->nm_cwnd > NFS_MAXCWND) 907 nmp->nm_cwnd = NFS_MAXCWND; 908 } 909 rep->r_flags &= ~R_SENT; 910 nmp->nm_sent -= NFS_CWNDSCALE; 911 /* 912 * Update rtt using a gain of 0.125 on the mean 913 * and a gain of 0.25 on the deviation. 914 */ 915 if (rep->r_flags & R_TIMING) { 916 /* 917 * Since the timer resolution of 918 * NFS_HZ is so course, it can often 919 * result in r_rtt == 0. Since 920 * r_rtt == N means that the actual 921 * rtt is between N+dt and N+2-dt ticks, 922 * add 1. 923 */ 924 t1 = rep->r_rtt + 1; 925 t1 -= (NFS_SRTT(rep) >> 3); 926 NFS_SRTT(rep) += t1; 927 if (t1 < 0) 928 t1 = -t1; 929 t1 -= (NFS_SDRTT(rep) >> 2); 930 NFS_SDRTT(rep) += t1; 931 } 932 nmp->nm_timeouts = 0; 933 break; 934 } 935 } 936 /* 937 * If not matched to a request, drop it. 938 * If it's mine, get out. 939 */ 940 if (rep == 0) { 941 nfsstats.rpcunexpected++; 942 m_freem(mrep); 943 } else if (rep == myrep) { 944 if (rep->r_mrep == NULL) 945 panic("nfsreply nil"); 946 return (0); 947 } 948 if (myrep->r_flags & R_GETONEREP) 949 return (0); 950 } 951 } 952 953 /* 954 * nfs_request - goes something like this 955 * - fill in request struct 956 * - links it into list 957 * - calls nfs_send() for first transmit 958 * - calls nfs_receive() to get reply 959 * - break down rpc header and return with nfs reply pointed to 960 * by mrep or error 961 * nb: always frees up mreq mbuf list 962 */ 963 int 964 nfs_request(np, mrest, procnum, lwp, cred, mrp, mdp, dposp, rexmitp) 965 struct nfsnode *np; 966 struct mbuf *mrest; 967 int procnum; 968 struct lwp *lwp; 969 kauth_cred_t cred; 970 struct mbuf **mrp; 971 struct mbuf **mdp; 972 caddr_t *dposp; 973 int *rexmitp; 974 { 975 struct mbuf *m, *mrep; 976 struct nfsreq *rep; 977 u_int32_t *tl; 978 int i; 979 struct nfsmount *nmp = VFSTONFS(np->n_vnode->v_mount); 980 struct mbuf *md, *mheadend; 981 char nickv[RPCX_NICKVERF]; 982 time_t reqtime, waituntil; 983 caddr_t dpos, cp2; 984 int t1, s, error = 0, mrest_len, auth_len, auth_type; 985 int trylater_delay = NFS_TRYLATERDEL, failed_auth = 0; 986 int verf_len, verf_type; 987 u_int32_t xid; 988 char *auth_str, *verf_str; 989 NFSKERBKEY_T key; /* save session key */ 990 kauth_cred_t acred; 991 #ifndef NFS_V2_ONLY 992 int nqlflag, cachable; 993 u_quad_t frev; 994 #endif 995 struct mbuf *mrest_backup = NULL; 996 kauth_cred_t origcred = NULL; /* XXX: gcc */ 997 boolean_t retry_cred = TRUE; 998 boolean_t use_opencred = (np->n_flag & NUSEOPENCRED) != 0; 999 1000 if (rexmitp != NULL) 1001 *rexmitp = 0; 1002 1003 acred = kauth_cred_alloc(); 1004 1005 tryagain_cred: 1006 KASSERT(cred != NULL); 1007 MALLOC(rep, struct nfsreq *, sizeof(struct nfsreq), M_NFSREQ, M_WAITOK); 1008 rep->r_nmp = nmp; 1009 rep->r_lwp = lwp; 1010 rep->r_procnum = procnum; 1011 i = 0; 1012 m = mrest; 1013 while (m) { 1014 i += m->m_len; 1015 m = m->m_next; 1016 } 1017 mrest_len = i; 1018 1019 /* 1020 * Get the RPC header with authorization. 1021 */ 1022 kerbauth: 1023 verf_str = auth_str = (char *)0; 1024 if (nmp->nm_flag & NFSMNT_KERB) { 1025 verf_str = nickv; 1026 verf_len = sizeof (nickv); 1027 auth_type = RPCAUTH_KERB4; 1028 memset((caddr_t)key, 0, sizeof (key)); 1029 if (failed_auth || nfs_getnickauth(nmp, cred, &auth_str, 1030 &auth_len, verf_str, verf_len)) { 1031 error = nfs_getauth(nmp, rep, cred, &auth_str, 1032 &auth_len, verf_str, &verf_len, key); 1033 if (error) { 1034 free((caddr_t)rep, M_NFSREQ); 1035 m_freem(mrest); 1036 KASSERT(kauth_cred_getrefcnt(acred) == 1); 1037 kauth_cred_free(acred); 1038 return (error); 1039 } 1040 } 1041 retry_cred = FALSE; 1042 } else { 1043 /* AUTH_UNIX */ 1044 uid_t uid; 1045 gid_t gid; 1046 1047 /* 1048 * on the most unix filesystems, permission checks are 1049 * done when the file is open(2)'ed. 1050 * ie. once a file is successfully open'ed, 1051 * following i/o operations never fail with EACCES. 1052 * we try to follow the semantics as far as possible. 1053 * 1054 * note that we expect that the nfs server always grant 1055 * accesses by the file's owner. 1056 */ 1057 origcred = cred; 1058 switch (procnum) { 1059 case NFSPROC_READ: 1060 case NFSPROC_WRITE: 1061 case NFSPROC_COMMIT: 1062 uid = np->n_vattr->va_uid; 1063 gid = np->n_vattr->va_gid; 1064 if (kauth_cred_geteuid(cred) == uid && 1065 kauth_cred_getegid(cred) == gid) { 1066 retry_cred = FALSE; 1067 break; 1068 } 1069 if (use_opencred) 1070 break; 1071 kauth_cred_setuid(acred, uid); 1072 kauth_cred_seteuid(acred, uid); 1073 kauth_cred_setsvuid(acred, uid); 1074 kauth_cred_setgid(acred, gid); 1075 kauth_cred_setegid(acred, gid); 1076 kauth_cred_setsvgid(acred, gid); 1077 cred = acred; 1078 break; 1079 default: 1080 retry_cred = FALSE; 1081 break; 1082 } 1083 /* 1084 * backup mbuf chain if we can need it later to retry. 1085 * 1086 * XXX maybe we can keep a direct reference to 1087 * mrest without doing m_copym, but it's ...ugly. 1088 */ 1089 if (retry_cred) 1090 mrest_backup = m_copym(mrest, 0, M_COPYALL, M_WAIT); 1091 auth_type = RPCAUTH_UNIX; 1092 /* XXX elad - ngroups */ 1093 auth_len = (((kauth_cred_ngroups(cred) > nmp->nm_numgrps) ? 1094 nmp->nm_numgrps : kauth_cred_ngroups(cred)) << 2) + 1095 5 * NFSX_UNSIGNED; 1096 } 1097 m = nfsm_rpchead(cred, nmp->nm_flag, procnum, auth_type, auth_len, 1098 auth_str, verf_len, verf_str, mrest, mrest_len, &mheadend, &xid); 1099 if (auth_str) 1100 free(auth_str, M_TEMP); 1101 1102 /* 1103 * For stream protocols, insert a Sun RPC Record Mark. 1104 */ 1105 if (nmp->nm_sotype == SOCK_STREAM) { 1106 M_PREPEND(m, NFSX_UNSIGNED, M_WAIT); 1107 *mtod(m, u_int32_t *) = htonl(0x80000000 | 1108 (m->m_pkthdr.len - NFSX_UNSIGNED)); 1109 } 1110 rep->r_mreq = m; 1111 rep->r_xid = xid; 1112 tryagain: 1113 if (nmp->nm_flag & NFSMNT_SOFT) 1114 rep->r_retry = nmp->nm_retry; 1115 else 1116 rep->r_retry = NFS_MAXREXMIT + 1; /* past clip limit */ 1117 rep->r_rtt = rep->r_rexmit = 0; 1118 if (proct[procnum] > 0) 1119 rep->r_flags = R_TIMING; 1120 else 1121 rep->r_flags = 0; 1122 rep->r_mrep = NULL; 1123 1124 /* 1125 * Do the client side RPC. 1126 */ 1127 nfsstats.rpcrequests++; 1128 /* 1129 * Chain request into list of outstanding requests. Be sure 1130 * to put it LAST so timer finds oldest requests first. 1131 */ 1132 s = splsoftnet(); 1133 TAILQ_INSERT_TAIL(&nfs_reqq, rep, r_chain); 1134 1135 /* Get send time for nqnfs */ 1136 reqtime = time_second; 1137 1138 /* 1139 * If backing off another request or avoiding congestion, don't 1140 * send this one now but let timer do it. If not timing a request, 1141 * do it now. 1142 */ 1143 if (nmp->nm_so && (nmp->nm_sotype != SOCK_DGRAM || 1144 (nmp->nm_flag & NFSMNT_DUMBTIMR) || 1145 nmp->nm_sent < nmp->nm_cwnd)) { 1146 splx(s); 1147 if (nmp->nm_soflags & PR_CONNREQUIRED) 1148 error = nfs_sndlock(&nmp->nm_iflag, rep); 1149 if (!error) { 1150 m = m_copym(rep->r_mreq, 0, M_COPYALL, M_WAIT); 1151 error = nfs_send(nmp->nm_so, nmp->nm_nam, m, rep, lwp); 1152 if (nmp->nm_soflags & PR_CONNREQUIRED) 1153 nfs_sndunlock(&nmp->nm_iflag); 1154 } 1155 if (!error && (rep->r_flags & R_MUSTRESEND) == 0) { 1156 nmp->nm_sent += NFS_CWNDSCALE; 1157 rep->r_flags |= R_SENT; 1158 } 1159 } else { 1160 splx(s); 1161 rep->r_rtt = -1; 1162 } 1163 1164 /* 1165 * Wait for the reply from our send or the timer's. 1166 */ 1167 if (!error || error == EPIPE) 1168 error = nfs_reply(rep, lwp); 1169 1170 /* 1171 * RPC done, unlink the request. 1172 */ 1173 s = splsoftnet(); 1174 TAILQ_REMOVE(&nfs_reqq, rep, r_chain); 1175 splx(s); 1176 1177 /* 1178 * Decrement the outstanding request count. 1179 */ 1180 if (rep->r_flags & R_SENT) { 1181 rep->r_flags &= ~R_SENT; /* paranoia */ 1182 nmp->nm_sent -= NFS_CWNDSCALE; 1183 } 1184 1185 if (rexmitp != NULL) { 1186 int rexmit; 1187 1188 if (nmp->nm_sotype != SOCK_DGRAM) 1189 rexmit = (rep->r_flags & R_REXMITTED) != 0; 1190 else 1191 rexmit = rep->r_rexmit; 1192 *rexmitp = rexmit; 1193 } 1194 1195 /* 1196 * If there was a successful reply and a tprintf msg. 1197 * tprintf a response. 1198 */ 1199 if (!error && (rep->r_flags & R_TPRINTFMSG)) 1200 nfs_msg(rep->r_lwp, nmp->nm_mountp->mnt_stat.f_mntfromname, 1201 "is alive again"); 1202 mrep = rep->r_mrep; 1203 md = rep->r_md; 1204 dpos = rep->r_dpos; 1205 if (error) 1206 goto nfsmout; 1207 1208 /* 1209 * break down the rpc header and check if ok 1210 */ 1211 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED); 1212 if (*tl++ == rpc_msgdenied) { 1213 if (*tl == rpc_mismatch) 1214 error = EOPNOTSUPP; 1215 else if ((nmp->nm_flag & NFSMNT_KERB) && *tl++ == rpc_autherr) { 1216 if (!failed_auth) { 1217 failed_auth++; 1218 mheadend->m_next = (struct mbuf *)0; 1219 m_freem(mrep); 1220 m_freem(rep->r_mreq); 1221 goto kerbauth; 1222 } else 1223 error = EAUTH; 1224 } else 1225 error = EACCES; 1226 m_freem(mrep); 1227 goto nfsmout; 1228 } 1229 1230 /* 1231 * Grab any Kerberos verifier, otherwise just throw it away. 1232 */ 1233 verf_type = fxdr_unsigned(int, *tl++); 1234 i = fxdr_unsigned(int32_t, *tl); 1235 if ((nmp->nm_flag & NFSMNT_KERB) && verf_type == RPCAUTH_KERB4) { 1236 error = nfs_savenickauth(nmp, cred, i, key, &md, &dpos, mrep); 1237 if (error) 1238 goto nfsmout; 1239 } else if (i > 0) 1240 nfsm_adv(nfsm_rndup(i)); 1241 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); 1242 /* 0 == ok */ 1243 if (*tl == 0) { 1244 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); 1245 if (*tl != 0) { 1246 error = fxdr_unsigned(int, *tl); 1247 switch (error) { 1248 case NFSERR_PERM: 1249 error = EPERM; 1250 break; 1251 1252 case NFSERR_NOENT: 1253 error = ENOENT; 1254 break; 1255 1256 case NFSERR_IO: 1257 error = EIO; 1258 break; 1259 1260 case NFSERR_NXIO: 1261 error = ENXIO; 1262 break; 1263 1264 case NFSERR_ACCES: 1265 error = EACCES; 1266 if (!retry_cred) 1267 break; 1268 m_freem(mrep); 1269 m_freem(rep->r_mreq); 1270 FREE(rep, M_NFSREQ); 1271 use_opencred = !use_opencred; 1272 if (mrest_backup == NULL) { 1273 /* m_copym failure */ 1274 KASSERT( 1275 kauth_cred_getrefcnt(acred) == 1); 1276 kauth_cred_free(acred); 1277 return ENOMEM; 1278 } 1279 mrest = mrest_backup; 1280 mrest_backup = NULL; 1281 cred = origcred; 1282 error = 0; 1283 retry_cred = FALSE; 1284 goto tryagain_cred; 1285 1286 case NFSERR_EXIST: 1287 error = EEXIST; 1288 break; 1289 1290 case NFSERR_XDEV: 1291 error = EXDEV; 1292 break; 1293 1294 case NFSERR_NODEV: 1295 error = ENODEV; 1296 break; 1297 1298 case NFSERR_NOTDIR: 1299 error = ENOTDIR; 1300 break; 1301 1302 case NFSERR_ISDIR: 1303 error = EISDIR; 1304 break; 1305 1306 case NFSERR_INVAL: 1307 error = EINVAL; 1308 break; 1309 1310 case NFSERR_FBIG: 1311 error = EFBIG; 1312 break; 1313 1314 case NFSERR_NOSPC: 1315 error = ENOSPC; 1316 break; 1317 1318 case NFSERR_ROFS: 1319 error = EROFS; 1320 break; 1321 1322 case NFSERR_MLINK: 1323 error = EMLINK; 1324 break; 1325 1326 case NFSERR_TIMEDOUT: 1327 error = ETIMEDOUT; 1328 break; 1329 1330 case NFSERR_NAMETOL: 1331 error = ENAMETOOLONG; 1332 break; 1333 1334 case NFSERR_NOTEMPTY: 1335 error = ENOTEMPTY; 1336 break; 1337 1338 case NFSERR_DQUOT: 1339 error = EDQUOT; 1340 break; 1341 1342 case NFSERR_STALE: 1343 /* 1344 * If the File Handle was stale, invalidate the 1345 * lookup cache, just in case. 1346 */ 1347 error = ESTALE; 1348 cache_purge(NFSTOV(np)); 1349 break; 1350 1351 case NFSERR_REMOTE: 1352 error = EREMOTE; 1353 break; 1354 1355 case NFSERR_WFLUSH: 1356 case NFSERR_BADHANDLE: 1357 case NFSERR_NOT_SYNC: 1358 case NFSERR_BAD_COOKIE: 1359 error = EINVAL; 1360 break; 1361 1362 case NFSERR_NOTSUPP: 1363 error = ENOTSUP; 1364 break; 1365 1366 case NFSERR_TOOSMALL: 1367 case NFSERR_SERVERFAULT: 1368 case NFSERR_BADTYPE: 1369 error = EINVAL; 1370 break; 1371 1372 case NFSERR_TRYLATER: 1373 if ((nmp->nm_flag & NFSMNT_NFSV3) == 0) 1374 break; 1375 m_freem(mrep); 1376 error = 0; 1377 waituntil = time_second + trylater_delay; 1378 while (time_second < waituntil) 1379 (void) tsleep((caddr_t)&lbolt, 1380 PSOCK, "nqnfstry", 0); 1381 trylater_delay *= NFS_TRYLATERDELMUL; 1382 if (trylater_delay > NFS_TRYLATERDELMAX) 1383 trylater_delay = NFS_TRYLATERDELMAX; 1384 /* 1385 * RFC1813: 1386 * The client should wait and then try 1387 * the request with a new RPC transaction ID. 1388 */ 1389 nfs_renewxid(rep); 1390 goto tryagain; 1391 1392 default: 1393 #ifdef DIAGNOSTIC 1394 printf("Invalid rpc error code %d\n", error); 1395 #endif 1396 error = EINVAL; 1397 break; 1398 } 1399 1400 if (nmp->nm_flag & NFSMNT_NFSV3) { 1401 *mrp = mrep; 1402 *mdp = md; 1403 *dposp = dpos; 1404 error |= NFSERR_RETERR; 1405 } else 1406 m_freem(mrep); 1407 goto nfsmout; 1408 } 1409 1410 /* 1411 * note which credential worked to minimize number of retries. 1412 */ 1413 if (use_opencred) 1414 np->n_flag |= NUSEOPENCRED; 1415 else 1416 np->n_flag &= ~NUSEOPENCRED; 1417 1418 #ifndef NFS_V2_ONLY 1419 /* 1420 * For nqnfs, get any lease in reply 1421 */ 1422 if (nmp->nm_flag & NFSMNT_NQNFS) { 1423 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); 1424 if (*tl) { 1425 nqlflag = fxdr_unsigned(int, *tl); 1426 nfsm_dissect(tl, u_int32_t *, 4*NFSX_UNSIGNED); 1427 cachable = fxdr_unsigned(int, *tl++); 1428 reqtime += fxdr_unsigned(int, *tl++); 1429 if (reqtime > time_second) { 1430 frev = fxdr_hyper(tl); 1431 nqnfs_clientlease(nmp, np, nqlflag, 1432 cachable, reqtime, frev); 1433 } 1434 } 1435 } 1436 #endif 1437 *mrp = mrep; 1438 *mdp = md; 1439 *dposp = dpos; 1440 1441 KASSERT(error == 0); 1442 goto nfsmout; 1443 } 1444 m_freem(mrep); 1445 error = EPROTONOSUPPORT; 1446 nfsmout: 1447 KASSERT(kauth_cred_getrefcnt(acred) == 1); 1448 kauth_cred_free(acred); 1449 m_freem(rep->r_mreq); 1450 free((caddr_t)rep, M_NFSREQ); 1451 m_freem(mrest_backup); 1452 return (error); 1453 } 1454 #endif /* NFS */ 1455 1456 /* 1457 * Generate the rpc reply header 1458 * siz arg. is used to decide if adding a cluster is worthwhile 1459 */ 1460 int 1461 nfs_rephead(siz, nd, slp, err, cache, frev, mrq, mbp, bposp) 1462 int siz; 1463 struct nfsrv_descript *nd; 1464 struct nfssvc_sock *slp; 1465 int err; 1466 int cache; 1467 u_quad_t *frev; 1468 struct mbuf **mrq; 1469 struct mbuf **mbp; 1470 caddr_t *bposp; 1471 { 1472 u_int32_t *tl; 1473 struct mbuf *mreq; 1474 caddr_t bpos; 1475 struct mbuf *mb; 1476 1477 mreq = m_gethdr(M_WAIT, MT_DATA); 1478 MCLAIM(mreq, &nfs_mowner); 1479 mb = mreq; 1480 /* 1481 * If this is a big reply, use a cluster else 1482 * try and leave leading space for the lower level headers. 1483 */ 1484 siz += RPC_REPLYSIZ; 1485 if (siz >= max_datalen) { 1486 m_clget(mreq, M_WAIT); 1487 } else 1488 mreq->m_data += max_hdr; 1489 tl = mtod(mreq, u_int32_t *); 1490 mreq->m_len = 6 * NFSX_UNSIGNED; 1491 bpos = ((caddr_t)tl) + mreq->m_len; 1492 *tl++ = txdr_unsigned(nd->nd_retxid); 1493 *tl++ = rpc_reply; 1494 if (err == ERPCMISMATCH || (err & NFSERR_AUTHERR)) { 1495 *tl++ = rpc_msgdenied; 1496 if (err & NFSERR_AUTHERR) { 1497 *tl++ = rpc_autherr; 1498 *tl = txdr_unsigned(err & ~NFSERR_AUTHERR); 1499 mreq->m_len -= NFSX_UNSIGNED; 1500 bpos -= NFSX_UNSIGNED; 1501 } else { 1502 *tl++ = rpc_mismatch; 1503 *tl++ = txdr_unsigned(RPC_VER2); 1504 *tl = txdr_unsigned(RPC_VER2); 1505 } 1506 } else { 1507 *tl++ = rpc_msgaccepted; 1508 1509 /* 1510 * For Kerberos authentication, we must send the nickname 1511 * verifier back, otherwise just RPCAUTH_NULL. 1512 */ 1513 if (nd->nd_flag & ND_KERBFULL) { 1514 struct nfsuid *nuidp; 1515 struct timeval ktvin, ktvout; 1516 1517 memset(&ktvout, 0, sizeof ktvout); /* XXX gcc */ 1518 1519 LIST_FOREACH(nuidp, 1520 NUIDHASH(slp, kauth_cred_geteuid(nd->nd_cr)), 1521 nu_hash) { 1522 if (kauth_cred_geteuid(nuidp->nu_cr) == 1523 kauth_cred_geteuid(nd->nd_cr) && 1524 (!nd->nd_nam2 || netaddr_match( 1525 NU_NETFAM(nuidp), &nuidp->nu_haddr, 1526 nd->nd_nam2))) 1527 break; 1528 } 1529 if (nuidp) { 1530 ktvin.tv_sec = 1531 txdr_unsigned(nuidp->nu_timestamp.tv_sec 1532 - 1); 1533 ktvin.tv_usec = 1534 txdr_unsigned(nuidp->nu_timestamp.tv_usec); 1535 1536 /* 1537 * Encrypt the timestamp in ecb mode using the 1538 * session key. 1539 */ 1540 #ifdef NFSKERB 1541 XXX 1542 #endif 1543 1544 *tl++ = rpc_auth_kerb; 1545 *tl++ = txdr_unsigned(3 * NFSX_UNSIGNED); 1546 *tl = ktvout.tv_sec; 1547 nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED); 1548 *tl++ = ktvout.tv_usec; 1549 *tl++ = txdr_unsigned( 1550 kauth_cred_geteuid(nuidp->nu_cr)); 1551 } else { 1552 *tl++ = 0; 1553 *tl++ = 0; 1554 } 1555 } else { 1556 *tl++ = 0; 1557 *tl++ = 0; 1558 } 1559 switch (err) { 1560 case EPROGUNAVAIL: 1561 *tl = txdr_unsigned(RPC_PROGUNAVAIL); 1562 break; 1563 case EPROGMISMATCH: 1564 *tl = txdr_unsigned(RPC_PROGMISMATCH); 1565 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 1566 if (nd->nd_flag & ND_NQNFS) { 1567 *tl++ = txdr_unsigned(3); 1568 *tl = txdr_unsigned(3); 1569 } else { 1570 *tl++ = txdr_unsigned(2); 1571 *tl = txdr_unsigned(3); 1572 } 1573 break; 1574 case EPROCUNAVAIL: 1575 *tl = txdr_unsigned(RPC_PROCUNAVAIL); 1576 break; 1577 case EBADRPC: 1578 *tl = txdr_unsigned(RPC_GARBAGE); 1579 break; 1580 default: 1581 *tl = 0; 1582 if (err != NFSERR_RETVOID) { 1583 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); 1584 if (err) 1585 *tl = txdr_unsigned(nfsrv_errmap(nd, err)); 1586 else 1587 *tl = 0; 1588 } 1589 break; 1590 }; 1591 } 1592 1593 /* 1594 * For nqnfs, piggyback lease as requested. 1595 */ 1596 if ((nd->nd_flag & ND_NQNFS) && err == 0) { 1597 if (nd->nd_flag & ND_LEASE) { 1598 nfsm_build(tl, u_int32_t *, 5 * NFSX_UNSIGNED); 1599 *tl++ = txdr_unsigned(nd->nd_flag & ND_LEASE); 1600 *tl++ = txdr_unsigned(cache); 1601 *tl++ = txdr_unsigned(nd->nd_duration); 1602 txdr_hyper(*frev, tl); 1603 } else { 1604 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); 1605 *tl = 0; 1606 } 1607 } 1608 if (mrq != NULL) 1609 *mrq = mreq; 1610 *mbp = mb; 1611 *bposp = bpos; 1612 if (err != 0 && err != NFSERR_RETVOID) 1613 nfsstats.srvrpc_errs++; 1614 return (0); 1615 } 1616 1617 /* 1618 * Nfs timer routine 1619 * Scan the nfsreq list and retranmit any requests that have timed out 1620 * To avoid retransmission attempts on STREAM sockets (in the future) make 1621 * sure to set the r_retry field to 0 (implies nm_retry == 0). 1622 */ 1623 void 1624 nfs_timer(arg) 1625 void *arg; /* never used */ 1626 { 1627 struct nfsreq *rep; 1628 struct mbuf *m; 1629 struct socket *so; 1630 struct nfsmount *nmp; 1631 int timeo; 1632 int s, error; 1633 #ifdef NFSSERVER 1634 struct timeval tv; 1635 struct nfssvc_sock *slp; 1636 static long lasttime = 0; 1637 u_quad_t cur_usec; 1638 #endif 1639 1640 s = splsoftnet(); 1641 TAILQ_FOREACH(rep, &nfs_reqq, r_chain) { 1642 nmp = rep->r_nmp; 1643 if (rep->r_mrep || (rep->r_flags & R_SOFTTERM)) 1644 continue; 1645 if (nfs_sigintr(nmp, rep, rep->r_lwp)) { 1646 rep->r_flags |= R_SOFTTERM; 1647 continue; 1648 } 1649 if (rep->r_rtt >= 0) { 1650 rep->r_rtt++; 1651 if (nmp->nm_flag & NFSMNT_DUMBTIMR) 1652 timeo = nmp->nm_timeo; 1653 else 1654 timeo = NFS_RTO(nmp, proct[rep->r_procnum]); 1655 if (nmp->nm_timeouts > 0) 1656 timeo *= nfs_backoff[nmp->nm_timeouts - 1]; 1657 if (rep->r_rtt <= timeo) 1658 continue; 1659 if (nmp->nm_timeouts < 1660 (sizeof(nfs_backoff) / sizeof(nfs_backoff[0]))) 1661 nmp->nm_timeouts++; 1662 } 1663 /* 1664 * Check for server not responding 1665 */ 1666 if ((rep->r_flags & R_TPRINTFMSG) == 0 && 1667 rep->r_rexmit > nmp->nm_deadthresh) { 1668 nfs_msg(rep->r_lwp, 1669 nmp->nm_mountp->mnt_stat.f_mntfromname, 1670 "not responding"); 1671 rep->r_flags |= R_TPRINTFMSG; 1672 } 1673 if (rep->r_rexmit >= rep->r_retry) { /* too many */ 1674 nfsstats.rpctimeouts++; 1675 rep->r_flags |= R_SOFTTERM; 1676 continue; 1677 } 1678 if (nmp->nm_sotype != SOCK_DGRAM) { 1679 if (++rep->r_rexmit > NFS_MAXREXMIT) 1680 rep->r_rexmit = NFS_MAXREXMIT; 1681 continue; 1682 } 1683 if ((so = nmp->nm_so) == NULL) 1684 continue; 1685 1686 /* 1687 * If there is enough space and the window allows.. 1688 * Resend it 1689 * Set r_rtt to -1 in case we fail to send it now. 1690 */ 1691 rep->r_rtt = -1; 1692 if (sbspace(&so->so_snd) >= rep->r_mreq->m_pkthdr.len && 1693 ((nmp->nm_flag & NFSMNT_DUMBTIMR) || 1694 (rep->r_flags & R_SENT) || 1695 nmp->nm_sent < nmp->nm_cwnd) && 1696 (m = m_copym(rep->r_mreq, 0, M_COPYALL, M_DONTWAIT))){ 1697 if (so->so_state & SS_ISCONNECTED) 1698 error = (*so->so_proto->pr_usrreq)(so, PRU_SEND, m, 1699 (struct mbuf *)0, (struct mbuf *)0, (struct lwp *)0); 1700 else 1701 error = (*so->so_proto->pr_usrreq)(so, PRU_SEND, m, 1702 nmp->nm_nam, (struct mbuf *)0, (struct lwp *)0); 1703 if (error) { 1704 if (NFSIGNORE_SOERROR(nmp->nm_soflags, error)) { 1705 #ifdef DEBUG 1706 printf("nfs_timer: ignoring error %d\n", 1707 error); 1708 #endif 1709 so->so_error = 0; 1710 } 1711 } else { 1712 /* 1713 * Iff first send, start timing 1714 * else turn timing off, backoff timer 1715 * and divide congestion window by 2. 1716 */ 1717 if (rep->r_flags & R_SENT) { 1718 rep->r_flags &= ~R_TIMING; 1719 if (++rep->r_rexmit > NFS_MAXREXMIT) 1720 rep->r_rexmit = NFS_MAXREXMIT; 1721 nmp->nm_cwnd >>= 1; 1722 if (nmp->nm_cwnd < NFS_CWNDSCALE) 1723 nmp->nm_cwnd = NFS_CWNDSCALE; 1724 nfsstats.rpcretries++; 1725 } else { 1726 rep->r_flags |= R_SENT; 1727 nmp->nm_sent += NFS_CWNDSCALE; 1728 } 1729 rep->r_rtt = 0; 1730 } 1731 } 1732 } 1733 1734 #ifdef NFSSERVER 1735 /* 1736 * Call the nqnfs server timer once a second to handle leases. 1737 */ 1738 if (lasttime != time_second) { 1739 lasttime = time_second; 1740 nqnfs_serverd(); 1741 } 1742 1743 /* 1744 * Scan the write gathering queues for writes that need to be 1745 * completed now. 1746 */ 1747 getmicrotime(&tv); 1748 cur_usec = (u_quad_t)tv.tv_sec * 1000000 + (u_quad_t)tv.tv_usec; 1749 TAILQ_FOREACH(slp, &nfssvc_sockhead, ns_chain) { 1750 if (LIST_FIRST(&slp->ns_tq) && 1751 LIST_FIRST(&slp->ns_tq)->nd_time <= cur_usec) 1752 nfsrv_wakenfsd(slp); 1753 } 1754 #endif /* NFSSERVER */ 1755 splx(s); 1756 callout_schedule(&nfs_timer_ch, nfs_ticks); 1757 } 1758 1759 /*ARGSUSED*/ 1760 void 1761 nfs_exit(p, v) 1762 struct proc *p; 1763 void *v; 1764 { 1765 struct nfsreq *rp; 1766 int s = splsoftnet(); 1767 1768 TAILQ_FOREACH(rp, &nfs_reqq, r_chain) { 1769 if (rp->r_lwp && rp->r_lwp->l_proc == p) 1770 TAILQ_REMOVE(&nfs_reqq, rp, r_chain); 1771 } 1772 splx(s); 1773 } 1774 1775 /* 1776 * Test for a termination condition pending on the process. 1777 * This is used for NFSMNT_INT mounts. 1778 */ 1779 int 1780 nfs_sigintr(nmp, rep, l) 1781 struct nfsmount *nmp; 1782 struct nfsreq *rep; 1783 struct lwp *l; 1784 { 1785 sigset_t ss; 1786 1787 if (rep && (rep->r_flags & R_SOFTTERM)) 1788 return (EINTR); 1789 if (!(nmp->nm_flag & NFSMNT_INT)) 1790 return (0); 1791 if (l) { 1792 sigpending1(l->l_proc, &ss); 1793 #if 0 1794 sigminusset(&l->l_proc->p_sigctx.ps_sigignore, &ss); 1795 #endif 1796 if (sigismember(&ss, SIGINT) || sigismember(&ss, SIGTERM) || 1797 sigismember(&ss, SIGKILL) || sigismember(&ss, SIGHUP) || 1798 sigismember(&ss, SIGQUIT)) 1799 return (EINTR); 1800 } 1801 return (0); 1802 } 1803 1804 /* 1805 * Lock a socket against others. 1806 * Necessary for STREAM sockets to ensure you get an entire rpc request/reply 1807 * and also to avoid race conditions between the processes with nfs requests 1808 * in progress when a reconnect is necessary. 1809 */ 1810 int 1811 nfs_sndlock(flagp, rep) 1812 int *flagp; 1813 struct nfsreq *rep; 1814 { 1815 struct lwp *l; 1816 int slpflag = 0, slptimeo = 0; 1817 1818 if (rep) { 1819 l = rep->r_lwp; 1820 if (rep->r_nmp->nm_flag & NFSMNT_INT) 1821 slpflag = PCATCH; 1822 } else 1823 l = (struct lwp *)0; 1824 while (*flagp & NFSMNT_SNDLOCK) { 1825 if (rep && nfs_sigintr(rep->r_nmp, rep, l)) 1826 return (EINTR); 1827 *flagp |= NFSMNT_WANTSND; 1828 (void) tsleep((caddr_t)flagp, slpflag | (PZERO - 1), "nfsndlck", 1829 slptimeo); 1830 if (slpflag == PCATCH) { 1831 slpflag = 0; 1832 slptimeo = 2 * hz; 1833 } 1834 } 1835 *flagp |= NFSMNT_SNDLOCK; 1836 return (0); 1837 } 1838 1839 /* 1840 * Unlock the stream socket for others. 1841 */ 1842 void 1843 nfs_sndunlock(flagp) 1844 int *flagp; 1845 { 1846 1847 if ((*flagp & NFSMNT_SNDLOCK) == 0) 1848 panic("nfs sndunlock"); 1849 *flagp &= ~NFSMNT_SNDLOCK; 1850 if (*flagp & NFSMNT_WANTSND) { 1851 *flagp &= ~NFSMNT_WANTSND; 1852 wakeup((caddr_t)flagp); 1853 } 1854 } 1855 1856 int 1857 nfs_rcvlock(rep) 1858 struct nfsreq *rep; 1859 { 1860 struct nfsmount *nmp = rep->r_nmp; 1861 int *flagp = &nmp->nm_iflag; 1862 int slpflag, slptimeo = 0; 1863 int error = 0; 1864 1865 if (*flagp & NFSMNT_DISMNT) 1866 return EIO; 1867 1868 if (*flagp & NFSMNT_INT) 1869 slpflag = PCATCH; 1870 else 1871 slpflag = 0; 1872 simple_lock(&nmp->nm_slock); 1873 while (*flagp & NFSMNT_RCVLOCK) { 1874 if (nfs_sigintr(rep->r_nmp, rep, rep->r_lwp)) { 1875 error = EINTR; 1876 goto quit; 1877 } 1878 *flagp |= NFSMNT_WANTRCV; 1879 nmp->nm_waiters++; 1880 (void) ltsleep(flagp, slpflag | (PZERO - 1), "nfsrcvlk", 1881 slptimeo, &nmp->nm_slock); 1882 nmp->nm_waiters--; 1883 if (*flagp & NFSMNT_DISMNT) { 1884 wakeup(&nmp->nm_waiters); 1885 error = EIO; 1886 goto quit; 1887 } 1888 /* If our reply was received while we were sleeping, 1889 * then just return without taking the lock to avoid a 1890 * situation where a single iod could 'capture' the 1891 * receive lock. 1892 */ 1893 if (rep->r_mrep != NULL) { 1894 error = EALREADY; 1895 goto quit; 1896 } 1897 if (slpflag == PCATCH) { 1898 slpflag = 0; 1899 slptimeo = 2 * hz; 1900 } 1901 } 1902 *flagp |= NFSMNT_RCVLOCK; 1903 quit: 1904 simple_unlock(&nmp->nm_slock); 1905 return error; 1906 } 1907 1908 /* 1909 * Unlock the stream socket for others. 1910 */ 1911 void 1912 nfs_rcvunlock(nmp) 1913 struct nfsmount *nmp; 1914 { 1915 int *flagp = &nmp->nm_iflag; 1916 1917 simple_lock(&nmp->nm_slock); 1918 if ((*flagp & NFSMNT_RCVLOCK) == 0) 1919 panic("nfs rcvunlock"); 1920 *flagp &= ~NFSMNT_RCVLOCK; 1921 if (*flagp & NFSMNT_WANTRCV) { 1922 *flagp &= ~NFSMNT_WANTRCV; 1923 wakeup((caddr_t)flagp); 1924 } 1925 simple_unlock(&nmp->nm_slock); 1926 } 1927 1928 /* 1929 * Parse an RPC request 1930 * - verify it 1931 * - allocate and fill in the cred. 1932 */ 1933 int 1934 nfs_getreq(nd, nfsd, has_header) 1935 struct nfsrv_descript *nd; 1936 struct nfsd *nfsd; 1937 int has_header; 1938 { 1939 int len, i; 1940 u_int32_t *tl; 1941 int32_t t1; 1942 struct uio uio; 1943 struct iovec iov; 1944 caddr_t dpos, cp2, cp; 1945 u_int32_t nfsvers, auth_type; 1946 uid_t nickuid; 1947 int error = 0, nqnfs = 0, ticklen; 1948 struct mbuf *mrep, *md; 1949 struct nfsuid *nuidp; 1950 struct timeval tvin, tvout; 1951 1952 memset(&tvout, 0, sizeof tvout); /* XXX gcc */ 1953 1954 KASSERT(nd->nd_cr == NULL); 1955 mrep = nd->nd_mrep; 1956 md = nd->nd_md; 1957 dpos = nd->nd_dpos; 1958 if (has_header) { 1959 nfsm_dissect(tl, u_int32_t *, 10 * NFSX_UNSIGNED); 1960 nd->nd_retxid = fxdr_unsigned(u_int32_t, *tl++); 1961 if (*tl++ != rpc_call) { 1962 m_freem(mrep); 1963 return (EBADRPC); 1964 } 1965 } else 1966 nfsm_dissect(tl, u_int32_t *, 8 * NFSX_UNSIGNED); 1967 nd->nd_repstat = 0; 1968 nd->nd_flag = 0; 1969 if (*tl++ != rpc_vers) { 1970 nd->nd_repstat = ERPCMISMATCH; 1971 nd->nd_procnum = NFSPROC_NOOP; 1972 return (0); 1973 } 1974 if (*tl != nfs_prog) { 1975 if (*tl == nqnfs_prog) 1976 nqnfs++; 1977 else { 1978 nd->nd_repstat = EPROGUNAVAIL; 1979 nd->nd_procnum = NFSPROC_NOOP; 1980 return (0); 1981 } 1982 } 1983 tl++; 1984 nfsvers = fxdr_unsigned(u_int32_t, *tl++); 1985 if (((nfsvers < NFS_VER2 || nfsvers > NFS_VER3) && !nqnfs) || 1986 (nfsvers != NQNFS_VER3 && nqnfs)) { 1987 nd->nd_repstat = EPROGMISMATCH; 1988 nd->nd_procnum = NFSPROC_NOOP; 1989 return (0); 1990 } 1991 if (nqnfs) 1992 nd->nd_flag = (ND_NFSV3 | ND_NQNFS); 1993 else if (nfsvers == NFS_VER3) 1994 nd->nd_flag = ND_NFSV3; 1995 nd->nd_procnum = fxdr_unsigned(u_int32_t, *tl++); 1996 if (nd->nd_procnum == NFSPROC_NULL) 1997 return (0); 1998 if (nd->nd_procnum >= NFS_NPROCS || 1999 (!nqnfs && nd->nd_procnum >= NQNFSPROC_GETLEASE) || 2000 (!nd->nd_flag && nd->nd_procnum > NFSV2PROC_STATFS)) { 2001 nd->nd_repstat = EPROCUNAVAIL; 2002 nd->nd_procnum = NFSPROC_NOOP; 2003 return (0); 2004 } 2005 if ((nd->nd_flag & ND_NFSV3) == 0) 2006 nd->nd_procnum = nfsv3_procid[nd->nd_procnum]; 2007 auth_type = *tl++; 2008 len = fxdr_unsigned(int, *tl++); 2009 if (len < 0 || len > RPCAUTH_MAXSIZ) { 2010 m_freem(mrep); 2011 return (EBADRPC); 2012 } 2013 2014 nd->nd_flag &= ~ND_KERBAUTH; 2015 /* 2016 * Handle auth_unix or auth_kerb. 2017 */ 2018 if (auth_type == rpc_auth_unix) { 2019 uid_t uid; 2020 gid_t gid, *grbuf; 2021 2022 nd->nd_cr = kauth_cred_alloc(); 2023 len = fxdr_unsigned(int, *++tl); 2024 if (len < 0 || len > NFS_MAXNAMLEN) { 2025 m_freem(mrep); 2026 error = EBADRPC; 2027 goto errout; 2028 } 2029 nfsm_adv(nfsm_rndup(len)); 2030 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED); 2031 2032 uid = fxdr_unsigned(uid_t, *tl++); 2033 gid = fxdr_unsigned(gid_t, *tl++); 2034 kauth_cred_setuid(nd->nd_cr, uid); 2035 kauth_cred_setgid(nd->nd_cr, gid); 2036 kauth_cred_seteuid(nd->nd_cr, uid); 2037 kauth_cred_setsvuid(nd->nd_cr, gid); 2038 kauth_cred_setegid(nd->nd_cr, uid); 2039 kauth_cred_setsvgid(nd->nd_cr, gid); 2040 2041 len = fxdr_unsigned(int, *tl); 2042 if (len < 0 || len > RPCAUTH_UNIXGIDS) { 2043 m_freem(mrep); 2044 error = EBADRPC; 2045 goto errout; 2046 } 2047 nfsm_dissect(tl, u_int32_t *, (len + 2) * NFSX_UNSIGNED); 2048 2049 grbuf = malloc(len * sizeof(gid_t), M_TEMP, M_WAITOK); 2050 for (i = 0; i < len; i++) { 2051 if (i < NGROUPS) /* XXX elad */ 2052 grbuf[i] = fxdr_unsigned(gid_t, *tl++); 2053 else 2054 tl++; 2055 } 2056 kauth_cred_setgroups(nd->nd_cr, grbuf, min(len, NGROUPS), -1); 2057 free(grbuf, M_TEMP); 2058 2059 len = fxdr_unsigned(int, *++tl); 2060 if (len < 0 || len > RPCAUTH_MAXSIZ) { 2061 m_freem(mrep); 2062 error = EBADRPC; 2063 goto errout; 2064 } 2065 if (len > 0) 2066 nfsm_adv(nfsm_rndup(len)); 2067 } else if (auth_type == rpc_auth_kerb) { 2068 switch (fxdr_unsigned(int, *tl++)) { 2069 case RPCAKN_FULLNAME: 2070 ticklen = fxdr_unsigned(int, *tl); 2071 *((u_int32_t *)nfsd->nfsd_authstr) = *tl; 2072 uio.uio_resid = nfsm_rndup(ticklen) + NFSX_UNSIGNED; 2073 nfsd->nfsd_authlen = uio.uio_resid + NFSX_UNSIGNED; 2074 if (uio.uio_resid > (len - 2 * NFSX_UNSIGNED)) { 2075 m_freem(mrep); 2076 error = EBADRPC; 2077 goto errout; 2078 } 2079 uio.uio_offset = 0; 2080 uio.uio_iov = &iov; 2081 uio.uio_iovcnt = 1; 2082 UIO_SETUP_SYSSPACE(&uio); 2083 iov.iov_base = (caddr_t)&nfsd->nfsd_authstr[4]; 2084 iov.iov_len = RPCAUTH_MAXSIZ - 4; 2085 nfsm_mtouio(&uio, uio.uio_resid); 2086 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 2087 if (*tl++ != rpc_auth_kerb || 2088 fxdr_unsigned(int, *tl) != 4 * NFSX_UNSIGNED) { 2089 printf("Bad kerb verifier\n"); 2090 nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADVERF); 2091 nd->nd_procnum = NFSPROC_NOOP; 2092 return (0); 2093 } 2094 nfsm_dissect(cp, caddr_t, 4 * NFSX_UNSIGNED); 2095 tl = (u_int32_t *)cp; 2096 if (fxdr_unsigned(int, *tl) != RPCAKN_FULLNAME) { 2097 printf("Not fullname kerb verifier\n"); 2098 nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADVERF); 2099 nd->nd_procnum = NFSPROC_NOOP; 2100 return (0); 2101 } 2102 cp += NFSX_UNSIGNED; 2103 memcpy(nfsd->nfsd_verfstr, cp, 3 * NFSX_UNSIGNED); 2104 nfsd->nfsd_verflen = 3 * NFSX_UNSIGNED; 2105 nd->nd_flag |= ND_KERBFULL; 2106 nfsd->nfsd_flag |= NFSD_NEEDAUTH; 2107 break; 2108 case RPCAKN_NICKNAME: 2109 if (len != 2 * NFSX_UNSIGNED) { 2110 printf("Kerb nickname short\n"); 2111 nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADCRED); 2112 nd->nd_procnum = NFSPROC_NOOP; 2113 return (0); 2114 } 2115 nickuid = fxdr_unsigned(uid_t, *tl); 2116 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 2117 if (*tl++ != rpc_auth_kerb || 2118 fxdr_unsigned(int, *tl) != 3 * NFSX_UNSIGNED) { 2119 printf("Kerb nick verifier bad\n"); 2120 nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADVERF); 2121 nd->nd_procnum = NFSPROC_NOOP; 2122 return (0); 2123 } 2124 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED); 2125 tvin.tv_sec = *tl++; 2126 tvin.tv_usec = *tl; 2127 2128 LIST_FOREACH(nuidp, NUIDHASH(nfsd->nfsd_slp, nickuid), 2129 nu_hash) { 2130 if (kauth_cred_geteuid(nuidp->nu_cr) == nickuid && 2131 (!nd->nd_nam2 || 2132 netaddr_match(NU_NETFAM(nuidp), 2133 &nuidp->nu_haddr, nd->nd_nam2))) 2134 break; 2135 } 2136 if (!nuidp) { 2137 nd->nd_repstat = 2138 (NFSERR_AUTHERR|AUTH_REJECTCRED); 2139 nd->nd_procnum = NFSPROC_NOOP; 2140 return (0); 2141 } 2142 2143 /* 2144 * Now, decrypt the timestamp using the session key 2145 * and validate it. 2146 */ 2147 #ifdef NFSKERB 2148 XXX 2149 #endif 2150 2151 tvout.tv_sec = fxdr_unsigned(long, tvout.tv_sec); 2152 tvout.tv_usec = fxdr_unsigned(long, tvout.tv_usec); 2153 if (nuidp->nu_expire < time_second || 2154 nuidp->nu_timestamp.tv_sec > tvout.tv_sec || 2155 (nuidp->nu_timestamp.tv_sec == tvout.tv_sec && 2156 nuidp->nu_timestamp.tv_usec > tvout.tv_usec)) { 2157 nuidp->nu_expire = 0; 2158 nd->nd_repstat = 2159 (NFSERR_AUTHERR|AUTH_REJECTVERF); 2160 nd->nd_procnum = NFSPROC_NOOP; 2161 return (0); 2162 } 2163 kauth_cred_hold(nuidp->nu_cr); 2164 nd->nd_cr = nuidp->nu_cr; 2165 nd->nd_flag |= ND_KERBNICK; 2166 } 2167 } else { 2168 nd->nd_repstat = (NFSERR_AUTHERR | AUTH_REJECTCRED); 2169 nd->nd_procnum = NFSPROC_NOOP; 2170 return (0); 2171 } 2172 2173 /* 2174 * For nqnfs, get piggybacked lease request. 2175 */ 2176 if (nqnfs && nd->nd_procnum != NQNFSPROC_EVICTED) { 2177 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); 2178 nd->nd_flag |= fxdr_unsigned(int, *tl); 2179 if (nd->nd_flag & ND_LEASE) { 2180 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); 2181 nd->nd_duration = fxdr_unsigned(u_int32_t, *tl); 2182 } else 2183 nd->nd_duration = NQ_MINLEASE; 2184 } else 2185 nd->nd_duration = NQ_MINLEASE; 2186 nd->nd_md = md; 2187 nd->nd_dpos = dpos; 2188 KASSERT((nd->nd_cr == NULL && (nfsd->nfsd_flag & NFSD_NEEDAUTH) != 0) 2189 || (nd->nd_cr != NULL && (nfsd->nfsd_flag & NFSD_NEEDAUTH) == 0)); 2190 return (0); 2191 nfsmout: 2192 errout: 2193 KASSERT(error != 0); 2194 if (nd->nd_cr != NULL) { 2195 kauth_cred_free(nd->nd_cr); 2196 nd->nd_cr = NULL; 2197 } 2198 return (error); 2199 } 2200 2201 int 2202 nfs_msg(l, server, msg) 2203 struct lwp *l; 2204 const char *server, *msg; 2205 { 2206 tpr_t tpr; 2207 2208 if (l) 2209 tpr = tprintf_open(l->l_proc); 2210 else 2211 tpr = NULL; 2212 tprintf(tpr, "nfs server %s: %s\n", server, msg); 2213 tprintf_close(tpr); 2214 return (0); 2215 } 2216 2217 #ifdef NFSSERVER 2218 int (*nfsrv3_procs[NFS_NPROCS]) __P((struct nfsrv_descript *, 2219 struct nfssvc_sock *, struct lwp *, 2220 struct mbuf **)) = { 2221 nfsrv_null, 2222 nfsrv_getattr, 2223 nfsrv_setattr, 2224 nfsrv_lookup, 2225 nfsrv3_access, 2226 nfsrv_readlink, 2227 nfsrv_read, 2228 nfsrv_write, 2229 nfsrv_create, 2230 nfsrv_mkdir, 2231 nfsrv_symlink, 2232 nfsrv_mknod, 2233 nfsrv_remove, 2234 nfsrv_rmdir, 2235 nfsrv_rename, 2236 nfsrv_link, 2237 nfsrv_readdir, 2238 nfsrv_readdirplus, 2239 nfsrv_statfs, 2240 nfsrv_fsinfo, 2241 nfsrv_pathconf, 2242 nfsrv_commit, 2243 nqnfsrv_getlease, 2244 nqnfsrv_vacated, 2245 nfsrv_noop, 2246 nfsrv_noop 2247 }; 2248 2249 /* 2250 * Socket upcall routine for the nfsd sockets. 2251 * The caddr_t arg is a pointer to the "struct nfssvc_sock". 2252 * Essentially do as much as possible non-blocking, else punt and it will 2253 * be called with M_WAIT from an nfsd. 2254 */ 2255 void 2256 nfsrv_rcv(so, arg, waitflag) 2257 struct socket *so; 2258 caddr_t arg; 2259 int waitflag; 2260 { 2261 struct nfssvc_sock *slp = (struct nfssvc_sock *)arg; 2262 struct mbuf *m; 2263 struct mbuf *mp, *nam; 2264 struct uio auio; 2265 int flags, error; 2266 int setflags = 0; 2267 2268 error = nfsdsock_lock(slp, (waitflag != M_DONTWAIT)); 2269 if (error) { 2270 setflags |= SLP_NEEDQ; 2271 goto dorecs_unlocked; 2272 } 2273 2274 KASSERT(so == slp->ns_so); 2275 #define NFS_TEST_HEAVY 2276 #ifdef NFS_TEST_HEAVY 2277 /* 2278 * Define this to test for nfsds handling this under heavy load. 2279 * 2280 * XXX it isn't safe to call so_receive from so_upcall context. 2281 */ 2282 if (waitflag == M_DONTWAIT) { 2283 setflags |= SLP_NEEDQ; 2284 goto dorecs; 2285 } 2286 #endif 2287 simple_lock(&slp->ns_lock); 2288 slp->ns_flag &= ~SLP_NEEDQ; 2289 simple_unlock(&slp->ns_lock); 2290 if (so->so_type == SOCK_STREAM) { 2291 #ifndef NFS_TEST_HEAVY 2292 /* 2293 * If there are already records on the queue, defer soreceive() 2294 * to an nfsd so that there is feedback to the TCP layer that 2295 * the nfs servers are heavily loaded. 2296 */ 2297 if (slp->ns_rec && waitflag == M_DONTWAIT) { 2298 setflags |= SLP_NEEDQ; 2299 goto dorecs; 2300 } 2301 #endif 2302 2303 /* 2304 * Do soreceive(). 2305 */ 2306 auio.uio_resid = 1000000000; 2307 /* not need to setup uio_vmspace */ 2308 flags = MSG_DONTWAIT; 2309 error = (*so->so_receive)(so, &nam, &auio, &mp, NULL, &flags); 2310 if (error || mp == NULL) { 2311 if (error == EWOULDBLOCK) 2312 setflags |= SLP_NEEDQ; 2313 else 2314 setflags |= SLP_DISCONN; 2315 goto dorecs; 2316 } 2317 m = mp; 2318 if (slp->ns_rawend) { 2319 slp->ns_rawend->m_next = m; 2320 slp->ns_cc += 1000000000 - auio.uio_resid; 2321 } else { 2322 slp->ns_raw = m; 2323 slp->ns_cc = 1000000000 - auio.uio_resid; 2324 } 2325 while (m->m_next) 2326 m = m->m_next; 2327 slp->ns_rawend = m; 2328 2329 /* 2330 * Now try and parse record(s) out of the raw stream data. 2331 */ 2332 error = nfsrv_getstream(slp, waitflag); 2333 if (error) { 2334 if (error == EPERM) 2335 setflags |= SLP_DISCONN; 2336 else 2337 setflags |= SLP_NEEDQ; 2338 } 2339 } else { 2340 do { 2341 auio.uio_resid = 1000000000; 2342 /* not need to setup uio_vmspace */ 2343 flags = MSG_DONTWAIT; 2344 error = (*so->so_receive)(so, &nam, &auio, &mp, NULL, 2345 &flags); 2346 if (mp) { 2347 if (nam) { 2348 m = nam; 2349 m->m_next = mp; 2350 } else 2351 m = mp; 2352 if (slp->ns_recend) 2353 slp->ns_recend->m_nextpkt = m; 2354 else 2355 slp->ns_rec = m; 2356 slp->ns_recend = m; 2357 m->m_nextpkt = (struct mbuf *)0; 2358 } 2359 if (error) { 2360 if ((so->so_proto->pr_flags & PR_CONNREQUIRED) 2361 && error != EWOULDBLOCK) { 2362 setflags |= SLP_DISCONN; 2363 goto dorecs; 2364 } 2365 } 2366 } while (mp); 2367 } 2368 dorecs: 2369 nfsdsock_unlock(slp); 2370 2371 dorecs_unlocked: 2372 /* 2373 * Now try and process the request records, non-blocking. 2374 */ 2375 if (setflags) { 2376 simple_lock(&slp->ns_lock); 2377 slp->ns_flag |= setflags; 2378 simple_unlock(&slp->ns_lock); 2379 } 2380 if (waitflag == M_DONTWAIT && 2381 (slp->ns_rec || (slp->ns_flag & (SLP_DISCONN | SLP_NEEDQ)) != 0)) { 2382 nfsrv_wakenfsd(slp); 2383 } 2384 } 2385 2386 int 2387 nfsdsock_lock(struct nfssvc_sock *slp, boolean_t waitok) 2388 { 2389 2390 simple_lock(&slp->ns_lock); 2391 while ((slp->ns_flag & (SLP_BUSY|SLP_VALID)) == SLP_BUSY) { 2392 if (!waitok) { 2393 simple_unlock(&slp->ns_lock); 2394 return EWOULDBLOCK; 2395 } 2396 slp->ns_flag |= SLP_WANT; 2397 ltsleep(&slp->ns_flag, PSOCK, "nslock", 0, &slp->ns_lock); 2398 } 2399 if ((slp->ns_flag & SLP_VALID) == 0) { 2400 simple_unlock(&slp->ns_lock); 2401 return EINVAL; 2402 } 2403 slp->ns_flag |= SLP_BUSY; 2404 simple_unlock(&slp->ns_lock); 2405 2406 return 0; 2407 } 2408 2409 void 2410 nfsdsock_unlock(struct nfssvc_sock *slp) 2411 { 2412 2413 KASSERT((slp->ns_flag & SLP_BUSY) != 0); 2414 2415 simple_lock(&slp->ns_lock); 2416 if ((slp->ns_flag & SLP_WANT) != 0) { 2417 wakeup(&slp->ns_flag); 2418 } 2419 slp->ns_flag &= ~(SLP_BUSY|SLP_WANT); 2420 simple_unlock(&slp->ns_lock); 2421 } 2422 2423 int 2424 nfsdsock_drain(struct nfssvc_sock *slp) 2425 { 2426 int error = 0; 2427 2428 simple_lock(&slp->ns_lock); 2429 if ((slp->ns_flag & SLP_VALID) == 0) { 2430 error = EINVAL; 2431 goto done; 2432 } 2433 slp->ns_flag &= ~SLP_VALID; 2434 while ((slp->ns_flag & SLP_BUSY) != 0) { 2435 slp->ns_flag |= SLP_WANT; 2436 ltsleep(&slp->ns_flag, PSOCK, "nsdrain", 0, &slp->ns_lock); 2437 } 2438 done: 2439 simple_unlock(&slp->ns_lock); 2440 2441 return error; 2442 } 2443 2444 /* 2445 * Try and extract an RPC request from the mbuf data list received on a 2446 * stream socket. The "waitflag" argument indicates whether or not it 2447 * can sleep. 2448 */ 2449 int 2450 nfsrv_getstream(slp, waitflag) 2451 struct nfssvc_sock *slp; 2452 int waitflag; 2453 { 2454 struct mbuf *m, **mpp; 2455 struct mbuf *recm; 2456 u_int32_t recmark; 2457 int error = 0; 2458 2459 for (;;) { 2460 if (slp->ns_reclen == 0) { 2461 if (slp->ns_cc < NFSX_UNSIGNED) { 2462 break; 2463 } 2464 m = slp->ns_raw; 2465 m_copydata(m, 0, NFSX_UNSIGNED, (caddr_t)&recmark); 2466 m_adj(m, NFSX_UNSIGNED); 2467 slp->ns_cc -= NFSX_UNSIGNED; 2468 recmark = ntohl(recmark); 2469 slp->ns_reclen = recmark & ~0x80000000; 2470 if (recmark & 0x80000000) 2471 slp->ns_flag |= SLP_LASTFRAG; 2472 else 2473 slp->ns_flag &= ~SLP_LASTFRAG; 2474 if (slp->ns_reclen > NFS_MAXPACKET) { 2475 error = EPERM; 2476 break; 2477 } 2478 } 2479 2480 /* 2481 * Now get the record part. 2482 * 2483 * Note that slp->ns_reclen may be 0. Linux sometimes 2484 * generates 0-length records. 2485 */ 2486 if (slp->ns_cc == slp->ns_reclen) { 2487 recm = slp->ns_raw; 2488 slp->ns_raw = slp->ns_rawend = (struct mbuf *)0; 2489 slp->ns_cc = slp->ns_reclen = 0; 2490 } else if (slp->ns_cc > slp->ns_reclen) { 2491 recm = slp->ns_raw; 2492 m = m_split(recm, slp->ns_reclen, waitflag); 2493 if (m == NULL) { 2494 error = EWOULDBLOCK; 2495 break; 2496 } 2497 m_claimm(recm, &nfs_mowner); 2498 slp->ns_raw = m; 2499 if (m->m_next == NULL) 2500 slp->ns_rawend = m; 2501 slp->ns_cc -= slp->ns_reclen; 2502 slp->ns_reclen = 0; 2503 } else { 2504 break; 2505 } 2506 2507 /* 2508 * Accumulate the fragments into a record. 2509 */ 2510 mpp = &slp->ns_frag; 2511 while (*mpp) 2512 mpp = &((*mpp)->m_next); 2513 *mpp = recm; 2514 if (slp->ns_flag & SLP_LASTFRAG) { 2515 if (slp->ns_recend) 2516 slp->ns_recend->m_nextpkt = slp->ns_frag; 2517 else 2518 slp->ns_rec = slp->ns_frag; 2519 slp->ns_recend = slp->ns_frag; 2520 slp->ns_frag = (struct mbuf *)0; 2521 } 2522 } 2523 2524 return error; 2525 } 2526 2527 /* 2528 * Parse an RPC header. 2529 */ 2530 int 2531 nfsrv_dorec(slp, nfsd, ndp) 2532 struct nfssvc_sock *slp; 2533 struct nfsd *nfsd; 2534 struct nfsrv_descript **ndp; 2535 { 2536 struct mbuf *m, *nam; 2537 struct nfsrv_descript *nd; 2538 int error; 2539 2540 *ndp = NULL; 2541 2542 if (nfsdsock_lock(slp, TRUE)) { 2543 return ENOBUFS; 2544 } 2545 m = slp->ns_rec; 2546 if (m == NULL) { 2547 nfsdsock_unlock(slp); 2548 return ENOBUFS; 2549 } 2550 slp->ns_rec = m->m_nextpkt; 2551 if (slp->ns_rec) 2552 m->m_nextpkt = NULL; 2553 else 2554 slp->ns_recend = NULL; 2555 nfsdsock_unlock(slp); 2556 2557 if (m->m_type == MT_SONAME) { 2558 nam = m; 2559 m = m->m_next; 2560 nam->m_next = NULL; 2561 } else 2562 nam = NULL; 2563 nd = nfsdreq_alloc(); 2564 nd->nd_md = nd->nd_mrep = m; 2565 nd->nd_nam2 = nam; 2566 nd->nd_dpos = mtod(m, caddr_t); 2567 error = nfs_getreq(nd, nfsd, TRUE); 2568 if (error) { 2569 m_freem(nam); 2570 nfsdreq_free(nd); 2571 return (error); 2572 } 2573 *ndp = nd; 2574 nfsd->nfsd_nd = nd; 2575 return (0); 2576 } 2577 2578 /* 2579 * Search for a sleeping nfsd and wake it up. 2580 * SIDE EFFECT: If none found, set NFSD_CHECKSLP flag, so that one of the 2581 * running nfsds will go look for the work in the nfssvc_sock list. 2582 */ 2583 void 2584 nfsrv_wakenfsd(slp) 2585 struct nfssvc_sock *slp; 2586 { 2587 struct nfsd *nd; 2588 2589 if ((slp->ns_flag & SLP_VALID) == 0) 2590 return; 2591 simple_lock(&nfsd_slock); 2592 if (slp->ns_flag & SLP_DOREC) { 2593 simple_unlock(&nfsd_slock); 2594 return; 2595 } 2596 nd = SLIST_FIRST(&nfsd_idle_head); 2597 if (nd) { 2598 SLIST_REMOVE_HEAD(&nfsd_idle_head, nfsd_idle); 2599 simple_unlock(&nfsd_slock); 2600 2601 if (nd->nfsd_slp) 2602 panic("nfsd wakeup"); 2603 slp->ns_sref++; 2604 nd->nfsd_slp = slp; 2605 wakeup(nd); 2606 return; 2607 } 2608 slp->ns_flag |= SLP_DOREC; 2609 nfsd_head_flag |= NFSD_CHECKSLP; 2610 TAILQ_INSERT_TAIL(&nfssvc_sockpending, slp, ns_pending); 2611 simple_unlock(&nfsd_slock); 2612 } 2613 2614 int 2615 nfsdsock_sendreply(struct nfssvc_sock *slp, struct nfsrv_descript *nd) 2616 { 2617 int error; 2618 2619 if (nd->nd_mrep != NULL) { 2620 m_freem(nd->nd_mrep); 2621 nd->nd_mrep = NULL; 2622 } 2623 2624 simple_lock(&slp->ns_lock); 2625 if ((slp->ns_flag & SLP_SENDING) != 0) { 2626 SIMPLEQ_INSERT_TAIL(&slp->ns_sendq, nd, nd_sendq); 2627 simple_unlock(&slp->ns_lock); 2628 return 0; 2629 } 2630 KASSERT(SIMPLEQ_EMPTY(&slp->ns_sendq)); 2631 slp->ns_flag |= SLP_SENDING; 2632 simple_unlock(&slp->ns_lock); 2633 2634 again: 2635 error = nfs_send(slp->ns_so, nd->nd_nam2, nd->nd_mreq, NULL, curlwp); 2636 if (nd->nd_nam2) { 2637 m_free(nd->nd_nam2); 2638 } 2639 nfsdreq_free(nd); 2640 2641 simple_lock(&slp->ns_lock); 2642 KASSERT((slp->ns_flag & SLP_SENDING) != 0); 2643 nd = SIMPLEQ_FIRST(&slp->ns_sendq); 2644 if (nd != NULL) { 2645 SIMPLEQ_REMOVE_HEAD(&slp->ns_sendq, nd_sendq); 2646 simple_unlock(&slp->ns_lock); 2647 goto again; 2648 } 2649 slp->ns_flag &= ~SLP_SENDING; 2650 simple_unlock(&slp->ns_lock); 2651 2652 return error; 2653 } 2654 #endif /* NFSSERVER */ 2655 2656 #if defined(NFSSERVER) || (defined(NFS) && !defined(NFS_V2_ONLY)) 2657 static struct pool nfs_srvdesc_pool; 2658 2659 void 2660 nfsdreq_init(void) 2661 { 2662 2663 pool_init(&nfs_srvdesc_pool, sizeof(struct nfsrv_descript), 2664 0, 0, 0, "nfsrvdescpl", &pool_allocator_nointr); 2665 } 2666 2667 struct nfsrv_descript * 2668 nfsdreq_alloc(void) 2669 { 2670 struct nfsrv_descript *nd; 2671 2672 nd = pool_get(&nfs_srvdesc_pool, PR_WAITOK); 2673 nd->nd_cr = NULL; 2674 return nd; 2675 } 2676 2677 void 2678 nfsdreq_free(struct nfsrv_descript *nd) 2679 { 2680 kauth_cred_t cr; 2681 2682 cr = nd->nd_cr; 2683 if (cr != NULL) { 2684 KASSERT(kauth_cred_getrefcnt(cr) == 1); 2685 kauth_cred_free(cr); 2686 } 2687 pool_put(&nfs_srvdesc_pool, nd); 2688 } 2689 #endif /* defined(NFSSERVER) || (defined(NFS) && !defined(NFS_V2_ONLY)) */ 2690