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

Cache object: 7904dc73127de27450906e7da0536d33


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