The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/nfs/nfs_socket.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*      $NetBSD: nfs_socket.c,v 1.141 2006/11/09 09:53:57 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.141 2006/11/09 09:53:57 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 = MOWNER_INIT("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(void *arg)
 1625 {
 1626         struct nfsreq *rep;
 1627         struct mbuf *m;
 1628         struct socket *so;
 1629         struct nfsmount *nmp;
 1630         int timeo;
 1631         int s, error;
 1632 #ifdef NFSSERVER
 1633         struct timeval tv;
 1634         struct nfssvc_sock *slp;
 1635         static long lasttime = 0;
 1636         u_quad_t cur_usec;
 1637 #endif
 1638 
 1639         s = splsoftnet();
 1640         TAILQ_FOREACH(rep, &nfs_reqq, r_chain) {
 1641                 nmp = rep->r_nmp;
 1642                 if (rep->r_mrep || (rep->r_flags & R_SOFTTERM))
 1643                         continue;
 1644                 if (nfs_sigintr(nmp, rep, rep->r_lwp)) {
 1645                         rep->r_flags |= R_SOFTTERM;
 1646                         continue;
 1647                 }
 1648                 if (rep->r_rtt >= 0) {
 1649                         rep->r_rtt++;
 1650                         if (nmp->nm_flag & NFSMNT_DUMBTIMR)
 1651                                 timeo = nmp->nm_timeo;
 1652                         else
 1653                                 timeo = NFS_RTO(nmp, proct[rep->r_procnum]);
 1654                         if (nmp->nm_timeouts > 0)
 1655                                 timeo *= nfs_backoff[nmp->nm_timeouts - 1];
 1656                         if (rep->r_rtt <= timeo)
 1657                                 continue;
 1658                         if (nmp->nm_timeouts <
 1659                             (sizeof(nfs_backoff) / sizeof(nfs_backoff[0])))
 1660                                 nmp->nm_timeouts++;
 1661                 }
 1662                 /*
 1663                  * Check for server not responding
 1664                  */
 1665                 if ((rep->r_flags & R_TPRINTFMSG) == 0 &&
 1666                      rep->r_rexmit > nmp->nm_deadthresh) {
 1667                         nfs_msg(rep->r_lwp,
 1668                             nmp->nm_mountp->mnt_stat.f_mntfromname,
 1669                             "not responding");
 1670                         rep->r_flags |= R_TPRINTFMSG;
 1671                 }
 1672                 if (rep->r_rexmit >= rep->r_retry) {    /* too many */
 1673                         nfsstats.rpctimeouts++;
 1674                         rep->r_flags |= R_SOFTTERM;
 1675                         continue;
 1676                 }
 1677                 if (nmp->nm_sotype != SOCK_DGRAM) {
 1678                         if (++rep->r_rexmit > NFS_MAXREXMIT)
 1679                                 rep->r_rexmit = NFS_MAXREXMIT;
 1680                         continue;
 1681                 }
 1682                 if ((so = nmp->nm_so) == NULL)
 1683                         continue;
 1684 
 1685                 /*
 1686                  * If there is enough space and the window allows..
 1687                  *      Resend it
 1688                  * Set r_rtt to -1 in case we fail to send it now.
 1689                  */
 1690                 rep->r_rtt = -1;
 1691                 if (sbspace(&so->so_snd) >= rep->r_mreq->m_pkthdr.len &&
 1692                    ((nmp->nm_flag & NFSMNT_DUMBTIMR) ||
 1693                     (rep->r_flags & R_SENT) ||
 1694                     nmp->nm_sent < nmp->nm_cwnd) &&
 1695                    (m = m_copym(rep->r_mreq, 0, M_COPYALL, M_DONTWAIT))){
 1696                         if (so->so_state & SS_ISCONNECTED)
 1697                             error = (*so->so_proto->pr_usrreq)(so, PRU_SEND, m,
 1698                             (struct mbuf *)0, (struct mbuf *)0, (struct lwp *)0);
 1699                         else
 1700                             error = (*so->so_proto->pr_usrreq)(so, PRU_SEND, m,
 1701                             nmp->nm_nam, (struct mbuf *)0, (struct lwp *)0);
 1702                         if (error) {
 1703                                 if (NFSIGNORE_SOERROR(nmp->nm_soflags, error)) {
 1704 #ifdef DEBUG
 1705                                         printf("nfs_timer: ignoring error %d\n",
 1706                                                 error);
 1707 #endif
 1708                                         so->so_error = 0;
 1709                                 }
 1710                         } else {
 1711                                 /*
 1712                                  * Iff first send, start timing
 1713                                  * else turn timing off, backoff timer
 1714                                  * and divide congestion window by 2.
 1715                                  */
 1716                                 if (rep->r_flags & R_SENT) {
 1717                                         rep->r_flags &= ~R_TIMING;
 1718                                         if (++rep->r_rexmit > NFS_MAXREXMIT)
 1719                                                 rep->r_rexmit = NFS_MAXREXMIT;
 1720                                         nmp->nm_cwnd >>= 1;
 1721                                         if (nmp->nm_cwnd < NFS_CWNDSCALE)
 1722                                                 nmp->nm_cwnd = NFS_CWNDSCALE;
 1723                                         nfsstats.rpcretries++;
 1724                                 } else {
 1725                                         rep->r_flags |= R_SENT;
 1726                                         nmp->nm_sent += NFS_CWNDSCALE;
 1727                                 }
 1728                                 rep->r_rtt = 0;
 1729                         }
 1730                 }
 1731         }
 1732 
 1733 #ifdef NFSSERVER
 1734         /*
 1735          * Call the nqnfs server timer once a second to handle leases.
 1736          */
 1737         if (lasttime != time_second) {
 1738                 lasttime = time_second;
 1739                 nqnfs_serverd();
 1740         }
 1741 
 1742         /*
 1743          * Scan the write gathering queues for writes that need to be
 1744          * completed now.
 1745          */
 1746         getmicrotime(&tv);
 1747         cur_usec = (u_quad_t)tv.tv_sec * 1000000 + (u_quad_t)tv.tv_usec;
 1748         TAILQ_FOREACH(slp, &nfssvc_sockhead, ns_chain) {
 1749             if (LIST_FIRST(&slp->ns_tq) &&
 1750                 LIST_FIRST(&slp->ns_tq)->nd_time <= cur_usec)
 1751                 nfsrv_wakenfsd(slp);
 1752         }
 1753 #endif /* NFSSERVER */
 1754         splx(s);
 1755         callout_schedule(&nfs_timer_ch, nfs_ticks);
 1756 }
 1757 
 1758 /*ARGSUSED*/
 1759 void
 1760 nfs_exit(struct proc *p, void *v)
 1761 {
 1762         struct nfsreq *rp;
 1763         int s = splsoftnet();
 1764 
 1765         TAILQ_FOREACH(rp, &nfs_reqq, r_chain) {
 1766                 if (rp->r_lwp && rp->r_lwp->l_proc == p)
 1767                         TAILQ_REMOVE(&nfs_reqq, rp, r_chain);
 1768         }
 1769         splx(s);
 1770 }
 1771 
 1772 /*
 1773  * Test for a termination condition pending on the process.
 1774  * This is used for NFSMNT_INT mounts.
 1775  */
 1776 int
 1777 nfs_sigintr(nmp, rep, l)
 1778         struct nfsmount *nmp;
 1779         struct nfsreq *rep;
 1780         struct lwp *l;
 1781 {
 1782         sigset_t ss;
 1783 
 1784         if (rep && (rep->r_flags & R_SOFTTERM))
 1785                 return (EINTR);
 1786         if (!(nmp->nm_flag & NFSMNT_INT))
 1787                 return (0);
 1788         if (l) {
 1789                 sigpending1(l->l_proc, &ss);
 1790 #if 0
 1791                 sigminusset(&l->l_proc->p_sigctx.ps_sigignore, &ss);
 1792 #endif
 1793                 if (sigismember(&ss, SIGINT) || sigismember(&ss, SIGTERM) ||
 1794                     sigismember(&ss, SIGKILL) || sigismember(&ss, SIGHUP) ||
 1795                     sigismember(&ss, SIGQUIT))
 1796                         return (EINTR);
 1797         }
 1798         return (0);
 1799 }
 1800 
 1801 /*
 1802  * Lock a socket against others.
 1803  * Necessary for STREAM sockets to ensure you get an entire rpc request/reply
 1804  * and also to avoid race conditions between the processes with nfs requests
 1805  * in progress when a reconnect is necessary.
 1806  */
 1807 int
 1808 nfs_sndlock(flagp, rep)
 1809         int *flagp;
 1810         struct nfsreq *rep;
 1811 {
 1812         struct lwp *l;
 1813         int slpflag = 0, slptimeo = 0;
 1814 
 1815         if (rep) {
 1816                 l = rep->r_lwp;
 1817                 if (rep->r_nmp->nm_flag & NFSMNT_INT)
 1818                         slpflag = PCATCH;
 1819         } else
 1820                 l = (struct lwp *)0;
 1821         while (*flagp & NFSMNT_SNDLOCK) {
 1822                 if (rep && nfs_sigintr(rep->r_nmp, rep, l))
 1823                         return (EINTR);
 1824                 *flagp |= NFSMNT_WANTSND;
 1825                 (void) tsleep((caddr_t)flagp, slpflag | (PZERO - 1), "nfsndlck",
 1826                         slptimeo);
 1827                 if (slpflag == PCATCH) {
 1828                         slpflag = 0;
 1829                         slptimeo = 2 * hz;
 1830                 }
 1831         }
 1832         *flagp |= NFSMNT_SNDLOCK;
 1833         return (0);
 1834 }
 1835 
 1836 /*
 1837  * Unlock the stream socket for others.
 1838  */
 1839 void
 1840 nfs_sndunlock(flagp)
 1841         int *flagp;
 1842 {
 1843 
 1844         if ((*flagp & NFSMNT_SNDLOCK) == 0)
 1845                 panic("nfs sndunlock");
 1846         *flagp &= ~NFSMNT_SNDLOCK;
 1847         if (*flagp & NFSMNT_WANTSND) {
 1848                 *flagp &= ~NFSMNT_WANTSND;
 1849                 wakeup((caddr_t)flagp);
 1850         }
 1851 }
 1852 
 1853 int
 1854 nfs_rcvlock(rep)
 1855         struct nfsreq *rep;
 1856 {
 1857         struct nfsmount *nmp = rep->r_nmp;
 1858         int *flagp = &nmp->nm_iflag;
 1859         int slpflag, slptimeo = 0;
 1860         int error = 0;
 1861 
 1862         if (*flagp & NFSMNT_DISMNT)
 1863                 return EIO;
 1864 
 1865         if (*flagp & NFSMNT_INT)
 1866                 slpflag = PCATCH;
 1867         else
 1868                 slpflag = 0;
 1869         simple_lock(&nmp->nm_slock);
 1870         while (*flagp & NFSMNT_RCVLOCK) {
 1871                 if (nfs_sigintr(rep->r_nmp, rep, rep->r_lwp)) {
 1872                         error = EINTR;
 1873                         goto quit;
 1874                 }
 1875                 *flagp |= NFSMNT_WANTRCV;
 1876                 nmp->nm_waiters++;
 1877                 (void) ltsleep(flagp, slpflag | (PZERO - 1), "nfsrcvlk",
 1878                         slptimeo, &nmp->nm_slock);
 1879                 nmp->nm_waiters--;
 1880                 if (*flagp & NFSMNT_DISMNT) {
 1881                         wakeup(&nmp->nm_waiters);
 1882                         error = EIO;
 1883                         goto quit;
 1884                 }
 1885                 /* If our reply was received while we were sleeping,
 1886                  * then just return without taking the lock to avoid a
 1887                  * situation where a single iod could 'capture' the
 1888                  * receive lock.
 1889                  */
 1890                 if (rep->r_mrep != NULL) {
 1891                         error = EALREADY;
 1892                         goto quit;
 1893                 }
 1894                 if (slpflag == PCATCH) {
 1895                         slpflag = 0;
 1896                         slptimeo = 2 * hz;
 1897                 }
 1898         }
 1899         *flagp |= NFSMNT_RCVLOCK;
 1900 quit:
 1901         simple_unlock(&nmp->nm_slock);
 1902         return error;
 1903 }
 1904 
 1905 /*
 1906  * Unlock the stream socket for others.
 1907  */
 1908 void
 1909 nfs_rcvunlock(nmp)
 1910         struct nfsmount *nmp;
 1911 {
 1912         int *flagp = &nmp->nm_iflag;
 1913 
 1914         simple_lock(&nmp->nm_slock);
 1915         if ((*flagp & NFSMNT_RCVLOCK) == 0)
 1916                 panic("nfs rcvunlock");
 1917         *flagp &= ~NFSMNT_RCVLOCK;
 1918         if (*flagp & NFSMNT_WANTRCV) {
 1919                 *flagp &= ~NFSMNT_WANTRCV;
 1920                 wakeup((caddr_t)flagp);
 1921         }
 1922         simple_unlock(&nmp->nm_slock);
 1923 }
 1924 
 1925 /*
 1926  * Parse an RPC request
 1927  * - verify it
 1928  * - allocate and fill in the cred.
 1929  */
 1930 int
 1931 nfs_getreq(nd, nfsd, has_header)
 1932         struct nfsrv_descript *nd;
 1933         struct nfsd *nfsd;
 1934         int has_header;
 1935 {
 1936         int len, i;
 1937         u_int32_t *tl;
 1938         int32_t t1;
 1939         struct uio uio;
 1940         struct iovec iov;
 1941         caddr_t dpos, cp2, cp;
 1942         u_int32_t nfsvers, auth_type;
 1943         uid_t nickuid;
 1944         int error = 0, nqnfs = 0, ticklen;
 1945         struct mbuf *mrep, *md;
 1946         struct nfsuid *nuidp;
 1947         struct timeval tvin, tvout;
 1948 
 1949         memset(&tvout, 0, sizeof tvout);        /* XXX gcc */
 1950 
 1951         KASSERT(nd->nd_cr == NULL);
 1952         mrep = nd->nd_mrep;
 1953         md = nd->nd_md;
 1954         dpos = nd->nd_dpos;
 1955         if (has_header) {
 1956                 nfsm_dissect(tl, u_int32_t *, 10 * NFSX_UNSIGNED);
 1957                 nd->nd_retxid = fxdr_unsigned(u_int32_t, *tl++);
 1958                 if (*tl++ != rpc_call) {
 1959                         m_freem(mrep);
 1960                         return (EBADRPC);
 1961                 }
 1962         } else
 1963                 nfsm_dissect(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
 1964         nd->nd_repstat = 0;
 1965         nd->nd_flag = 0;
 1966         if (*tl++ != rpc_vers) {
 1967                 nd->nd_repstat = ERPCMISMATCH;
 1968                 nd->nd_procnum = NFSPROC_NOOP;
 1969                 return (0);
 1970         }
 1971         if (*tl != nfs_prog) {
 1972                 if (*tl == nqnfs_prog)
 1973                         nqnfs++;
 1974                 else {
 1975                         nd->nd_repstat = EPROGUNAVAIL;
 1976                         nd->nd_procnum = NFSPROC_NOOP;
 1977                         return (0);
 1978                 }
 1979         }
 1980         tl++;
 1981         nfsvers = fxdr_unsigned(u_int32_t, *tl++);
 1982         if (((nfsvers < NFS_VER2 || nfsvers > NFS_VER3) && !nqnfs) ||
 1983                 (nfsvers != NQNFS_VER3 && nqnfs)) {
 1984                 nd->nd_repstat = EPROGMISMATCH;
 1985                 nd->nd_procnum = NFSPROC_NOOP;
 1986                 return (0);
 1987         }
 1988         if (nqnfs)
 1989                 nd->nd_flag = (ND_NFSV3 | ND_NQNFS);
 1990         else if (nfsvers == NFS_VER3)
 1991                 nd->nd_flag = ND_NFSV3;
 1992         nd->nd_procnum = fxdr_unsigned(u_int32_t, *tl++);
 1993         if (nd->nd_procnum == NFSPROC_NULL)
 1994                 return (0);
 1995         if (nd->nd_procnum >= NFS_NPROCS ||
 1996                 (!nqnfs && nd->nd_procnum >= NQNFSPROC_GETLEASE) ||
 1997                 (!nd->nd_flag && nd->nd_procnum > NFSV2PROC_STATFS)) {
 1998                 nd->nd_repstat = EPROCUNAVAIL;
 1999                 nd->nd_procnum = NFSPROC_NOOP;
 2000                 return (0);
 2001         }
 2002         if ((nd->nd_flag & ND_NFSV3) == 0)
 2003                 nd->nd_procnum = nfsv3_procid[nd->nd_procnum];
 2004         auth_type = *tl++;
 2005         len = fxdr_unsigned(int, *tl++);
 2006         if (len < 0 || len > RPCAUTH_MAXSIZ) {
 2007                 m_freem(mrep);
 2008                 return (EBADRPC);
 2009         }
 2010 
 2011         nd->nd_flag &= ~ND_KERBAUTH;
 2012         /*
 2013          * Handle auth_unix or auth_kerb.
 2014          */
 2015         if (auth_type == rpc_auth_unix) {
 2016                 uid_t uid;
 2017                 gid_t gid, *grbuf;
 2018 
 2019                 nd->nd_cr = kauth_cred_alloc();
 2020                 len = fxdr_unsigned(int, *++tl);
 2021                 if (len < 0 || len > NFS_MAXNAMLEN) {
 2022                         m_freem(mrep);
 2023                         error = EBADRPC;
 2024                         goto errout;
 2025                 }
 2026                 nfsm_adv(nfsm_rndup(len));
 2027                 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
 2028 
 2029                 uid = fxdr_unsigned(uid_t, *tl++);
 2030                 gid = fxdr_unsigned(gid_t, *tl++);
 2031                 kauth_cred_setuid(nd->nd_cr, uid);
 2032                 kauth_cred_seteuid(nd->nd_cr, uid);
 2033                 kauth_cred_setsvuid(nd->nd_cr, uid);
 2034                 kauth_cred_setgid(nd->nd_cr, gid);
 2035                 kauth_cred_setegid(nd->nd_cr, gid);
 2036                 kauth_cred_setsvgid(nd->nd_cr, gid);
 2037 
 2038                 len = fxdr_unsigned(int, *tl);
 2039                 if (len < 0 || len > RPCAUTH_UNIXGIDS) {
 2040                         m_freem(mrep);
 2041                         error = EBADRPC;
 2042                         goto errout;
 2043                 }
 2044                 nfsm_dissect(tl, u_int32_t *, (len + 2) * NFSX_UNSIGNED);
 2045 
 2046                 grbuf = malloc(len * sizeof(gid_t), M_TEMP, M_WAITOK);
 2047                 for (i = 0; i < len; i++) {
 2048                         if (i < NGROUPS) /* XXX elad */
 2049                                 grbuf[i] = fxdr_unsigned(gid_t, *tl++);
 2050                         else
 2051                                 tl++;
 2052                 }
 2053                 kauth_cred_setgroups(nd->nd_cr, grbuf, min(len, NGROUPS), -1);
 2054                 free(grbuf, M_TEMP);
 2055 
 2056                 len = fxdr_unsigned(int, *++tl);
 2057                 if (len < 0 || len > RPCAUTH_MAXSIZ) {
 2058                         m_freem(mrep);
 2059                         error = EBADRPC;
 2060                         goto errout;
 2061                 }
 2062                 if (len > 0)
 2063                         nfsm_adv(nfsm_rndup(len));
 2064         } else if (auth_type == rpc_auth_kerb) {
 2065                 switch (fxdr_unsigned(int, *tl++)) {
 2066                 case RPCAKN_FULLNAME:
 2067                         ticklen = fxdr_unsigned(int, *tl);
 2068                         *((u_int32_t *)nfsd->nfsd_authstr) = *tl;
 2069                         uio.uio_resid = nfsm_rndup(ticklen) + NFSX_UNSIGNED;
 2070                         nfsd->nfsd_authlen = uio.uio_resid + NFSX_UNSIGNED;
 2071                         if (uio.uio_resid > (len - 2 * NFSX_UNSIGNED)) {
 2072                                 m_freem(mrep);
 2073                                 error = EBADRPC;
 2074                                 goto errout;
 2075                         }
 2076                         uio.uio_offset = 0;
 2077                         uio.uio_iov = &iov;
 2078                         uio.uio_iovcnt = 1;
 2079                         UIO_SETUP_SYSSPACE(&uio);
 2080                         iov.iov_base = (caddr_t)&nfsd->nfsd_authstr[4];
 2081                         iov.iov_len = RPCAUTH_MAXSIZ - 4;
 2082                         nfsm_mtouio(&uio, uio.uio_resid);
 2083                         nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
 2084                         if (*tl++ != rpc_auth_kerb ||
 2085                                 fxdr_unsigned(int, *tl) != 4 * NFSX_UNSIGNED) {
 2086                                 printf("Bad kerb verifier\n");
 2087                                 nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADVERF);
 2088                                 nd->nd_procnum = NFSPROC_NOOP;
 2089                                 return (0);
 2090                         }
 2091                         nfsm_dissect(cp, caddr_t, 4 * NFSX_UNSIGNED);
 2092                         tl = (u_int32_t *)cp;
 2093                         if (fxdr_unsigned(int, *tl) != RPCAKN_FULLNAME) {
 2094                                 printf("Not fullname kerb verifier\n");
 2095                                 nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADVERF);
 2096                                 nd->nd_procnum = NFSPROC_NOOP;
 2097                                 return (0);
 2098                         }
 2099                         cp += NFSX_UNSIGNED;
 2100                         memcpy(nfsd->nfsd_verfstr, cp, 3 * NFSX_UNSIGNED);
 2101                         nfsd->nfsd_verflen = 3 * NFSX_UNSIGNED;
 2102                         nd->nd_flag |= ND_KERBFULL;
 2103                         nfsd->nfsd_flag |= NFSD_NEEDAUTH;
 2104                         break;
 2105                 case RPCAKN_NICKNAME:
 2106                         if (len != 2 * NFSX_UNSIGNED) {
 2107                                 printf("Kerb nickname short\n");
 2108                                 nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADCRED);
 2109                                 nd->nd_procnum = NFSPROC_NOOP;
 2110                                 return (0);
 2111                         }
 2112                         nickuid = fxdr_unsigned(uid_t, *tl);
 2113                         nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
 2114                         if (*tl++ != rpc_auth_kerb ||
 2115                                 fxdr_unsigned(int, *tl) != 3 * NFSX_UNSIGNED) {
 2116                                 printf("Kerb nick verifier bad\n");
 2117                                 nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADVERF);
 2118                                 nd->nd_procnum = NFSPROC_NOOP;
 2119                                 return (0);
 2120                         }
 2121                         nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
 2122                         tvin.tv_sec = *tl++;
 2123                         tvin.tv_usec = *tl;
 2124 
 2125                         LIST_FOREACH(nuidp, NUIDHASH(nfsd->nfsd_slp, nickuid),
 2126                             nu_hash) {
 2127                                 if (kauth_cred_geteuid(nuidp->nu_cr) == nickuid &&
 2128                                     (!nd->nd_nam2 ||
 2129                                      netaddr_match(NU_NETFAM(nuidp),
 2130                                       &nuidp->nu_haddr, nd->nd_nam2)))
 2131                                         break;
 2132                         }
 2133                         if (!nuidp) {
 2134                                 nd->nd_repstat =
 2135                                         (NFSERR_AUTHERR|AUTH_REJECTCRED);
 2136                                 nd->nd_procnum = NFSPROC_NOOP;
 2137                                 return (0);
 2138                         }
 2139 
 2140                         /*
 2141                          * Now, decrypt the timestamp using the session key
 2142                          * and validate it.
 2143                          */
 2144 #ifdef NFSKERB
 2145                         XXX
 2146 #endif
 2147 
 2148                         tvout.tv_sec = fxdr_unsigned(long, tvout.tv_sec);
 2149                         tvout.tv_usec = fxdr_unsigned(long, tvout.tv_usec);
 2150                         if (nuidp->nu_expire < time_second ||
 2151                             nuidp->nu_timestamp.tv_sec > tvout.tv_sec ||
 2152                             (nuidp->nu_timestamp.tv_sec == tvout.tv_sec &&
 2153                              nuidp->nu_timestamp.tv_usec > tvout.tv_usec)) {
 2154                                 nuidp->nu_expire = 0;
 2155                                 nd->nd_repstat =
 2156                                     (NFSERR_AUTHERR|AUTH_REJECTVERF);
 2157                                 nd->nd_procnum = NFSPROC_NOOP;
 2158                                 return (0);
 2159                         }
 2160                         kauth_cred_hold(nuidp->nu_cr);
 2161                         nd->nd_cr = nuidp->nu_cr;
 2162                         nd->nd_flag |= ND_KERBNICK;
 2163                 }
 2164         } else {
 2165                 nd->nd_repstat = (NFSERR_AUTHERR | AUTH_REJECTCRED);
 2166                 nd->nd_procnum = NFSPROC_NOOP;
 2167                 return (0);
 2168         }
 2169 
 2170         /*
 2171          * For nqnfs, get piggybacked lease request.
 2172          */
 2173         if (nqnfs && nd->nd_procnum != NQNFSPROC_EVICTED) {
 2174                 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
 2175                 nd->nd_flag |= fxdr_unsigned(int, *tl);
 2176                 if (nd->nd_flag & ND_LEASE) {
 2177                         nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
 2178                         nd->nd_duration = fxdr_unsigned(u_int32_t, *tl);
 2179                 } else
 2180                         nd->nd_duration = NQ_MINLEASE;
 2181         } else
 2182                 nd->nd_duration = NQ_MINLEASE;
 2183         nd->nd_md = md;
 2184         nd->nd_dpos = dpos;
 2185         KASSERT((nd->nd_cr == NULL && (nfsd->nfsd_flag & NFSD_NEEDAUTH) != 0)
 2186              || (nd->nd_cr != NULL && (nfsd->nfsd_flag & NFSD_NEEDAUTH) == 0));
 2187         return (0);
 2188 nfsmout:
 2189 errout:
 2190         KASSERT(error != 0);
 2191         if (nd->nd_cr != NULL) {
 2192                 kauth_cred_free(nd->nd_cr);
 2193                 nd->nd_cr = NULL;
 2194         }
 2195         return (error);
 2196 }
 2197 
 2198 int
 2199 nfs_msg(l, server, msg)
 2200         struct lwp *l;
 2201         const char *server, *msg;
 2202 {
 2203         tpr_t tpr;
 2204 
 2205         if (l)
 2206                 tpr = tprintf_open(l->l_proc);
 2207         else
 2208                 tpr = NULL;
 2209         tprintf(tpr, "nfs server %s: %s\n", server, msg);
 2210         tprintf_close(tpr);
 2211         return (0);
 2212 }
 2213 
 2214 #ifdef NFSSERVER
 2215 int (*nfsrv3_procs[NFS_NPROCS]) __P((struct nfsrv_descript *,
 2216                                     struct nfssvc_sock *, struct lwp *,
 2217                                     struct mbuf **)) = {
 2218         nfsrv_null,
 2219         nfsrv_getattr,
 2220         nfsrv_setattr,
 2221         nfsrv_lookup,
 2222         nfsrv3_access,
 2223         nfsrv_readlink,
 2224         nfsrv_read,
 2225         nfsrv_write,
 2226         nfsrv_create,
 2227         nfsrv_mkdir,
 2228         nfsrv_symlink,
 2229         nfsrv_mknod,
 2230         nfsrv_remove,
 2231         nfsrv_rmdir,
 2232         nfsrv_rename,
 2233         nfsrv_link,
 2234         nfsrv_readdir,
 2235         nfsrv_readdirplus,
 2236         nfsrv_statfs,
 2237         nfsrv_fsinfo,
 2238         nfsrv_pathconf,
 2239         nfsrv_commit,
 2240         nqnfsrv_getlease,
 2241         nqnfsrv_vacated,
 2242         nfsrv_noop,
 2243         nfsrv_noop
 2244 };
 2245 
 2246 /*
 2247  * Socket upcall routine for the nfsd sockets.
 2248  * The caddr_t arg is a pointer to the "struct nfssvc_sock".
 2249  * Essentially do as much as possible non-blocking, else punt and it will
 2250  * be called with M_WAIT from an nfsd.
 2251  */
 2252 void
 2253 nfsrv_rcv(so, arg, waitflag)
 2254         struct socket *so;
 2255         caddr_t arg;
 2256         int waitflag;
 2257 {
 2258         struct nfssvc_sock *slp = (struct nfssvc_sock *)arg;
 2259         struct mbuf *m;
 2260         struct mbuf *mp, *nam;
 2261         struct uio auio;
 2262         int flags, error;
 2263         int setflags = 0;
 2264 
 2265         error = nfsdsock_lock(slp, (waitflag != M_DONTWAIT));
 2266         if (error) {
 2267                 setflags |= SLP_NEEDQ;
 2268                 goto dorecs_unlocked;
 2269         }
 2270 
 2271         KASSERT(so == slp->ns_so);
 2272 #define NFS_TEST_HEAVY
 2273 #ifdef NFS_TEST_HEAVY
 2274         /*
 2275          * Define this to test for nfsds handling this under heavy load.
 2276          *
 2277          * XXX it isn't safe to call so_receive from so_upcall context.
 2278          */
 2279         if (waitflag == M_DONTWAIT) {
 2280                 setflags |= SLP_NEEDQ;
 2281                 goto dorecs;
 2282         }
 2283 #endif
 2284         simple_lock(&slp->ns_lock);
 2285         slp->ns_flag &= ~SLP_NEEDQ;
 2286         simple_unlock(&slp->ns_lock);
 2287         if (so->so_type == SOCK_STREAM) {
 2288 #ifndef NFS_TEST_HEAVY
 2289                 /*
 2290                  * If there are already records on the queue, defer soreceive()
 2291                  * to an nfsd so that there is feedback to the TCP layer that
 2292                  * the nfs servers are heavily loaded.
 2293                  */
 2294                 if (slp->ns_rec && waitflag == M_DONTWAIT) {
 2295                         setflags |= SLP_NEEDQ;
 2296                         goto dorecs;
 2297                 }
 2298 #endif
 2299 
 2300                 /*
 2301                  * Do soreceive().
 2302                  */
 2303                 auio.uio_resid = 1000000000;
 2304                 /* not need to setup uio_vmspace */
 2305                 flags = MSG_DONTWAIT;
 2306                 error = (*so->so_receive)(so, &nam, &auio, &mp, NULL, &flags);
 2307                 if (error || mp == NULL) {
 2308                         if (error == EWOULDBLOCK)
 2309                                 setflags |= SLP_NEEDQ;
 2310                         else
 2311                                 setflags |= SLP_DISCONN;
 2312                         goto dorecs;
 2313                 }
 2314                 m = mp;
 2315                 if (slp->ns_rawend) {
 2316                         slp->ns_rawend->m_next = m;
 2317                         slp->ns_cc += 1000000000 - auio.uio_resid;
 2318                 } else {
 2319                         slp->ns_raw = m;
 2320                         slp->ns_cc = 1000000000 - auio.uio_resid;
 2321                 }
 2322                 while (m->m_next)
 2323                         m = m->m_next;
 2324                 slp->ns_rawend = m;
 2325 
 2326                 /*
 2327                  * Now try and parse record(s) out of the raw stream data.
 2328                  */
 2329                 error = nfsrv_getstream(slp, waitflag);
 2330                 if (error) {
 2331                         if (error == EPERM)
 2332                                 setflags |= SLP_DISCONN;
 2333                         else
 2334                                 setflags |= SLP_NEEDQ;
 2335                 }
 2336         } else {
 2337                 do {
 2338                         auio.uio_resid = 1000000000;
 2339                         /* not need to setup uio_vmspace */
 2340                         flags = MSG_DONTWAIT;
 2341                         error = (*so->so_receive)(so, &nam, &auio, &mp, NULL,
 2342                             &flags);
 2343                         if (mp) {
 2344                                 if (nam) {
 2345                                         m = nam;
 2346                                         m->m_next = mp;
 2347                                 } else
 2348                                         m = mp;
 2349                                 if (slp->ns_recend)
 2350                                         slp->ns_recend->m_nextpkt = m;
 2351                                 else
 2352                                         slp->ns_rec = m;
 2353                                 slp->ns_recend = m;
 2354                                 m->m_nextpkt = (struct mbuf *)0;
 2355                         }
 2356                         if (error) {
 2357                                 if ((so->so_proto->pr_flags & PR_CONNREQUIRED)
 2358                                     && error != EWOULDBLOCK) {
 2359                                         setflags |= SLP_DISCONN;
 2360                                         goto dorecs;
 2361                                 }
 2362                         }
 2363                 } while (mp);
 2364         }
 2365 dorecs:
 2366         nfsdsock_unlock(slp);
 2367 
 2368 dorecs_unlocked:
 2369         /*
 2370          * Now try and process the request records, non-blocking.
 2371          */
 2372         if (setflags) {
 2373                 simple_lock(&slp->ns_lock);
 2374                 slp->ns_flag |= setflags;
 2375                 simple_unlock(&slp->ns_lock);
 2376         }
 2377         if (waitflag == M_DONTWAIT &&
 2378             (slp->ns_rec || (slp->ns_flag & (SLP_DISCONN | SLP_NEEDQ)) != 0)) {
 2379                 nfsrv_wakenfsd(slp);
 2380         }
 2381 }
 2382 
 2383 int
 2384 nfsdsock_lock(struct nfssvc_sock *slp, boolean_t waitok)
 2385 {
 2386 
 2387         simple_lock(&slp->ns_lock);
 2388         while ((slp->ns_flag & (SLP_BUSY|SLP_VALID)) == SLP_BUSY) {
 2389                 if (!waitok) {
 2390                         simple_unlock(&slp->ns_lock);
 2391                         return EWOULDBLOCK;
 2392                 }
 2393                 slp->ns_flag |= SLP_WANT;
 2394                 ltsleep(&slp->ns_flag, PSOCK, "nslock", 0, &slp->ns_lock);
 2395         }
 2396         if ((slp->ns_flag & SLP_VALID) == 0) {
 2397                 simple_unlock(&slp->ns_lock);
 2398                 return EINVAL;
 2399         }
 2400         slp->ns_flag |= SLP_BUSY;
 2401         simple_unlock(&slp->ns_lock);
 2402 
 2403         return 0;
 2404 }
 2405 
 2406 void
 2407 nfsdsock_unlock(struct nfssvc_sock *slp)
 2408 {
 2409 
 2410         KASSERT((slp->ns_flag & SLP_BUSY) != 0);
 2411 
 2412         simple_lock(&slp->ns_lock);
 2413         if ((slp->ns_flag & SLP_WANT) != 0) {
 2414                 wakeup(&slp->ns_flag);
 2415         }
 2416         slp->ns_flag &= ~(SLP_BUSY|SLP_WANT);
 2417         simple_unlock(&slp->ns_lock);
 2418 }
 2419 
 2420 int
 2421 nfsdsock_drain(struct nfssvc_sock *slp)
 2422 {
 2423         int error = 0;
 2424 
 2425         simple_lock(&slp->ns_lock);
 2426         if ((slp->ns_flag & SLP_VALID) == 0) {
 2427                 error = EINVAL;
 2428                 goto done;
 2429         }
 2430         slp->ns_flag &= ~SLP_VALID;
 2431         while ((slp->ns_flag & SLP_BUSY) != 0) {
 2432                 slp->ns_flag |= SLP_WANT;
 2433                 ltsleep(&slp->ns_flag, PSOCK, "nsdrain", 0, &slp->ns_lock);
 2434         }
 2435 done:
 2436         simple_unlock(&slp->ns_lock);
 2437 
 2438         return error;
 2439 }
 2440 
 2441 /*
 2442  * Try and extract an RPC request from the mbuf data list received on a
 2443  * stream socket. The "waitflag" argument indicates whether or not it
 2444  * can sleep.
 2445  */
 2446 int
 2447 nfsrv_getstream(slp, waitflag)
 2448         struct nfssvc_sock *slp;
 2449         int waitflag;
 2450 {
 2451         struct mbuf *m, **mpp;
 2452         struct mbuf *recm;
 2453         u_int32_t recmark;
 2454         int error = 0;
 2455 
 2456         for (;;) {
 2457                 if (slp->ns_reclen == 0) {
 2458                         if (slp->ns_cc < NFSX_UNSIGNED) {
 2459                                 break;
 2460                         }
 2461                         m = slp->ns_raw;
 2462                         m_copydata(m, 0, NFSX_UNSIGNED, (caddr_t)&recmark);
 2463                         m_adj(m, NFSX_UNSIGNED);
 2464                         slp->ns_cc -= NFSX_UNSIGNED;
 2465                         recmark = ntohl(recmark);
 2466                         slp->ns_reclen = recmark & ~0x80000000;
 2467                         if (recmark & 0x80000000)
 2468                                 slp->ns_flag |= SLP_LASTFRAG;
 2469                         else
 2470                                 slp->ns_flag &= ~SLP_LASTFRAG;
 2471                         if (slp->ns_reclen > NFS_MAXPACKET) {
 2472                                 error = EPERM;
 2473                                 break;
 2474                         }
 2475                 }
 2476 
 2477                 /*
 2478                  * Now get the record part.
 2479                  *
 2480                  * Note that slp->ns_reclen may be 0.  Linux sometimes
 2481                  * generates 0-length records.
 2482                  */
 2483                 if (slp->ns_cc == slp->ns_reclen) {
 2484                         recm = slp->ns_raw;
 2485                         slp->ns_raw = slp->ns_rawend = (struct mbuf *)0;
 2486                         slp->ns_cc = slp->ns_reclen = 0;
 2487                 } else if (slp->ns_cc > slp->ns_reclen) {
 2488                         recm = slp->ns_raw;
 2489                         m = m_split(recm, slp->ns_reclen, waitflag);
 2490                         if (m == NULL) {
 2491                                 error = EWOULDBLOCK;
 2492                                 break;
 2493                         }
 2494                         m_claimm(recm, &nfs_mowner);
 2495                         slp->ns_raw = m;
 2496                         if (m->m_next == NULL)
 2497                                 slp->ns_rawend = m;
 2498                         slp->ns_cc -= slp->ns_reclen;
 2499                         slp->ns_reclen = 0;
 2500                 } else {
 2501                         break;
 2502                 }
 2503 
 2504                 /*
 2505                  * Accumulate the fragments into a record.
 2506                  */
 2507                 mpp = &slp->ns_frag;
 2508                 while (*mpp)
 2509                         mpp = &((*mpp)->m_next);
 2510                 *mpp = recm;
 2511                 if (slp->ns_flag & SLP_LASTFRAG) {
 2512                         if (slp->ns_recend)
 2513                                 slp->ns_recend->m_nextpkt = slp->ns_frag;
 2514                         else
 2515                                 slp->ns_rec = slp->ns_frag;
 2516                         slp->ns_recend = slp->ns_frag;
 2517                         slp->ns_frag = (struct mbuf *)0;
 2518                 }
 2519         }
 2520 
 2521         return error;
 2522 }
 2523 
 2524 /*
 2525  * Parse an RPC header.
 2526  */
 2527 int
 2528 nfsrv_dorec(slp, nfsd, ndp)
 2529         struct nfssvc_sock *slp;
 2530         struct nfsd *nfsd;
 2531         struct nfsrv_descript **ndp;
 2532 {
 2533         struct mbuf *m, *nam;
 2534         struct nfsrv_descript *nd;
 2535         int error;
 2536 
 2537         *ndp = NULL;
 2538 
 2539         if (nfsdsock_lock(slp, TRUE)) {
 2540                 return ENOBUFS;
 2541         }
 2542         m = slp->ns_rec;
 2543         if (m == NULL) {
 2544                 nfsdsock_unlock(slp);
 2545                 return ENOBUFS;
 2546         }
 2547         slp->ns_rec = m->m_nextpkt;
 2548         if (slp->ns_rec)
 2549                 m->m_nextpkt = NULL;
 2550         else
 2551                 slp->ns_recend = NULL;
 2552         nfsdsock_unlock(slp);
 2553 
 2554         if (m->m_type == MT_SONAME) {
 2555                 nam = m;
 2556                 m = m->m_next;
 2557                 nam->m_next = NULL;
 2558         } else
 2559                 nam = NULL;
 2560         nd = nfsdreq_alloc();
 2561         nd->nd_md = nd->nd_mrep = m;
 2562         nd->nd_nam2 = nam;
 2563         nd->nd_dpos = mtod(m, caddr_t);
 2564         error = nfs_getreq(nd, nfsd, TRUE);
 2565         if (error) {
 2566                 m_freem(nam);
 2567                 nfsdreq_free(nd);
 2568                 return (error);
 2569         }
 2570         *ndp = nd;
 2571         nfsd->nfsd_nd = nd;
 2572         return (0);
 2573 }
 2574 
 2575 /*
 2576  * Search for a sleeping nfsd and wake it up.
 2577  * SIDE EFFECT: If none found, set NFSD_CHECKSLP flag, so that one of the
 2578  * running nfsds will go look for the work in the nfssvc_sock list.
 2579  */
 2580 void
 2581 nfsrv_wakenfsd(slp)
 2582         struct nfssvc_sock *slp;
 2583 {
 2584         struct nfsd *nd;
 2585 
 2586         if ((slp->ns_flag & SLP_VALID) == 0)
 2587                 return;
 2588         simple_lock(&nfsd_slock);
 2589         if (slp->ns_flag & SLP_DOREC) {
 2590                 simple_unlock(&nfsd_slock);
 2591                 return;
 2592         }
 2593         nd = SLIST_FIRST(&nfsd_idle_head);
 2594         if (nd) {
 2595                 SLIST_REMOVE_HEAD(&nfsd_idle_head, nfsd_idle);
 2596                 simple_unlock(&nfsd_slock);
 2597 
 2598                 if (nd->nfsd_slp)
 2599                         panic("nfsd wakeup");
 2600                 slp->ns_sref++;
 2601                 nd->nfsd_slp = slp;
 2602                 wakeup(nd);
 2603                 return;
 2604         }
 2605         slp->ns_flag |= SLP_DOREC;
 2606         nfsd_head_flag |= NFSD_CHECKSLP;
 2607         TAILQ_INSERT_TAIL(&nfssvc_sockpending, slp, ns_pending);
 2608         simple_unlock(&nfsd_slock);
 2609 }
 2610 
 2611 int
 2612 nfsdsock_sendreply(struct nfssvc_sock *slp, struct nfsrv_descript *nd)
 2613 {
 2614         int error;
 2615 
 2616         if (nd->nd_mrep != NULL) {
 2617                 m_freem(nd->nd_mrep);
 2618                 nd->nd_mrep = NULL;
 2619         }
 2620 
 2621         simple_lock(&slp->ns_lock);
 2622         if ((slp->ns_flag & SLP_SENDING) != 0) {
 2623                 SIMPLEQ_INSERT_TAIL(&slp->ns_sendq, nd, nd_sendq);
 2624                 simple_unlock(&slp->ns_lock);
 2625                 return 0;
 2626         }
 2627         KASSERT(SIMPLEQ_EMPTY(&slp->ns_sendq));
 2628         slp->ns_flag |= SLP_SENDING;
 2629         simple_unlock(&slp->ns_lock);
 2630 
 2631 again:
 2632         error = nfs_send(slp->ns_so, nd->nd_nam2, nd->nd_mreq, NULL, curlwp);
 2633         if (nd->nd_nam2) {
 2634                 m_free(nd->nd_nam2);
 2635         }
 2636         nfsdreq_free(nd);
 2637 
 2638         simple_lock(&slp->ns_lock);
 2639         KASSERT((slp->ns_flag & SLP_SENDING) != 0);
 2640         nd = SIMPLEQ_FIRST(&slp->ns_sendq);
 2641         if (nd != NULL) {
 2642                 SIMPLEQ_REMOVE_HEAD(&slp->ns_sendq, nd_sendq);
 2643                 simple_unlock(&slp->ns_lock);
 2644                 goto again;
 2645         }
 2646         slp->ns_flag &= ~SLP_SENDING;
 2647         simple_unlock(&slp->ns_lock);
 2648 
 2649         return error;
 2650 }
 2651 #endif /* NFSSERVER */
 2652 
 2653 #if defined(NFSSERVER) || (defined(NFS) && !defined(NFS_V2_ONLY))
 2654 static struct pool nfs_srvdesc_pool;
 2655 
 2656 void
 2657 nfsdreq_init(void)
 2658 {
 2659 
 2660         pool_init(&nfs_srvdesc_pool, sizeof(struct nfsrv_descript),
 2661             0, 0, 0, "nfsrvdescpl", &pool_allocator_nointr);
 2662 }
 2663 
 2664 struct nfsrv_descript *
 2665 nfsdreq_alloc(void)
 2666 {
 2667         struct nfsrv_descript *nd;
 2668 
 2669         nd = pool_get(&nfs_srvdesc_pool, PR_WAITOK);
 2670         nd->nd_cr = NULL;
 2671         return nd;
 2672 }
 2673 
 2674 void
 2675 nfsdreq_free(struct nfsrv_descript *nd)
 2676 {
 2677         kauth_cred_t cr;
 2678 
 2679         cr = nd->nd_cr;
 2680         if (cr != NULL) {
 2681                 kauth_cred_free(cr);
 2682         }
 2683         pool_put(&nfs_srvdesc_pool, nd);
 2684 }
 2685 #endif /* defined(NFSSERVER) || (defined(NFS) && !defined(NFS_V2_ONLY)) */

Cache object: 6ed12c21f5a31661d6a538502c09a0d3


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.