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

Cache object: 49e6d8f6da884f839e5264d5031759f7


[ 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.