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/nfsserver/nfs_srvsock.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: releng/5.1/sys/nfsserver/nfs_srvsock.c 111748 2003-03-02 16:54:40Z des $
   38  */
   39 
   40 #include <sys/cdefs.h>
   41 __FBSDID("$FreeBSD: releng/5.1/sys/nfsserver/nfs_srvsock.c 111748 2003-03-02 16:54:40Z des $");
   42 
   43 /*
   44  * Socket operations for use by nfs
   45  */
   46 
   47 #include <sys/param.h>
   48 #include <sys/systm.h>
   49 #include <sys/kernel.h>
   50 #include <sys/lock.h>
   51 #include <sys/malloc.h>
   52 #include <sys/mbuf.h>
   53 #include <sys/mount.h>
   54 #include <sys/mutex.h>
   55 #include <sys/proc.h>
   56 #include <sys/protosw.h>
   57 #include <sys/signalvar.h>
   58 #include <sys/socket.h>
   59 #include <sys/socketvar.h>
   60 #include <sys/sysctl.h>
   61 #include <sys/syslog.h>
   62 #include <sys/vnode.h>
   63 
   64 #include <netinet/in.h>
   65 #include <netinet/tcp.h>
   66 
   67 #include <nfs/rpcv2.h>
   68 #include <nfs/nfsproto.h>
   69 #include <nfsserver/nfs.h>
   70 #include <nfs/xdr_subs.h>
   71 #include <nfsserver/nfsm_subs.h>
   72 
   73 #define TRUE    1
   74 #define FALSE   0
   75 
   76 static int nfs_realign_test;
   77 static int nfs_realign_count;
   78 
   79 SYSCTL_DECL(_vfs_nfsrv);
   80 
   81 SYSCTL_INT(_vfs_nfsrv, OID_AUTO, realign_test, CTLFLAG_RW, &nfs_realign_test, 0, "");
   82 SYSCTL_INT(_vfs_nfsrv, OID_AUTO, realign_count, CTLFLAG_RW, &nfs_realign_count, 0, "");
   83 
   84 
   85 /*
   86  * There is a congestion window for outstanding rpcs maintained per mount
   87  * point. The cwnd size is adjusted in roughly the way that:
   88  * Van Jacobson, Congestion avoidance and Control, In "Proceedings of
   89  * SIGCOMM '88". ACM, August 1988.
   90  * describes for TCP. The cwnd size is chopped in half on a retransmit timeout
   91  * and incremented by 1/cwnd when each rpc reply is received and a full cwnd
   92  * of rpcs is in progress.
   93  * (The sent count and cwnd are scaled for integer arith.)
   94  * Variants of "slow start" were tried and were found to be too much of a
   95  * performance hit (ave. rtt 3 times larger),
   96  * I suspect due to the large rtt that nfs rpcs have.
   97  */
   98 #define NFS_CWNDSCALE   256
   99 #define NFS_MAXCWND     (NFS_CWNDSCALE * 32)
  100 struct callout_handle   nfsrv_timer_handle;
  101 
  102 static void     nfs_realign(struct mbuf **pm, int hsiz);        /* XXX SHARED */
  103 static int      nfsrv_getstream(struct nfssvc_sock *, int);
  104 
  105 int (*nfsrv3_procs[NFS_NPROCS])(struct nfsrv_descript *nd,
  106                                 struct nfssvc_sock *slp,
  107                                 struct thread *td,
  108                                 struct mbuf **mreqp) = {
  109         nfsrv_null,
  110         nfsrv_getattr,
  111         nfsrv_setattr,
  112         nfsrv_lookup,
  113         nfsrv3_access,
  114         nfsrv_readlink,
  115         nfsrv_read,
  116         nfsrv_write,
  117         nfsrv_create,
  118         nfsrv_mkdir,
  119         nfsrv_symlink,
  120         nfsrv_mknod,
  121         nfsrv_remove,
  122         nfsrv_rmdir,
  123         nfsrv_rename,
  124         nfsrv_link,
  125         nfsrv_readdir,
  126         nfsrv_readdirplus,
  127         nfsrv_statfs,
  128         nfsrv_fsinfo,
  129         nfsrv_pathconf,
  130         nfsrv_commit,
  131         nfsrv_noop
  132 };
  133 
  134 
  135 /*
  136  * Generate the rpc reply header
  137  * siz arg. is used to decide if adding a cluster is worthwhile
  138  */
  139 struct mbuf *
  140 nfs_rephead(int siz, struct nfsrv_descript *nd, int err,
  141     struct mbuf **mbp, caddr_t *bposp)
  142 {
  143         u_int32_t *tl;
  144         struct mbuf *mreq;
  145         caddr_t bpos;
  146         struct mbuf *mb;
  147 
  148         nd->nd_repstat = err;
  149         if (err && (nd->nd_flag & ND_NFSV3) == 0)       /* XXX recheck */
  150                 siz = 0;
  151         MGETHDR(mreq, M_TRYWAIT, MT_DATA);
  152         mb = mreq;
  153         /*
  154          * If this is a big reply, use a cluster else
  155          * try and leave leading space for the lower level headers.
  156          */
  157         mreq->m_len = 6 * NFSX_UNSIGNED;
  158         siz += RPC_REPLYSIZ;
  159         if ((max_hdr + siz) >= MINCLSIZE) {
  160                 MCLGET(mreq, M_TRYWAIT);
  161         } else
  162                 mreq->m_data += min(max_hdr, M_TRAILINGSPACE(mreq));
  163         tl = mtod(mreq, u_int32_t *);
  164         bpos = ((caddr_t)tl) + mreq->m_len;
  165         *tl++ = txdr_unsigned(nd->nd_retxid);
  166         *tl++ = nfsrv_rpc_reply;
  167         if (err == ERPCMISMATCH || (err & NFSERR_AUTHERR)) {
  168                 *tl++ = nfsrv_rpc_msgdenied;
  169                 if (err & NFSERR_AUTHERR) {
  170                         *tl++ = nfsrv_rpc_autherr;
  171                         *tl = txdr_unsigned(err & ~NFSERR_AUTHERR);
  172                         mreq->m_len -= NFSX_UNSIGNED;
  173                         bpos -= NFSX_UNSIGNED;
  174                 } else {
  175                         *tl++ = nfsrv_rpc_mismatch;
  176                         *tl++ = txdr_unsigned(RPC_VER2);
  177                         *tl = txdr_unsigned(RPC_VER2);
  178                 }
  179         } else {
  180                 *tl++ = nfsrv_rpc_msgaccepted;
  181                 /*
  182                  * Send a RPCAUTH_NULL verifier - no Kerberos.
  183                  */
  184                 *tl++ = 0;
  185                 *tl++ = 0;
  186                 switch (err) {
  187                 case EPROGUNAVAIL:
  188                         *tl = txdr_unsigned(RPC_PROGUNAVAIL);
  189                         break;
  190                 case EPROGMISMATCH:
  191                         *tl = txdr_unsigned(RPC_PROGMISMATCH);
  192                         tl = nfsm_build(u_int32_t *, 2 * NFSX_UNSIGNED);
  193                         *tl++ = txdr_unsigned(2);
  194                         *tl = txdr_unsigned(3);
  195                         break;
  196                 case EPROCUNAVAIL:
  197                         *tl = txdr_unsigned(RPC_PROCUNAVAIL);
  198                         break;
  199                 case EBADRPC:
  200                         *tl = txdr_unsigned(RPC_GARBAGE);
  201                         break;
  202                 default:
  203                         *tl = 0;
  204                         if (err != NFSERR_RETVOID) {
  205                                 tl = nfsm_build(u_int32_t *, NFSX_UNSIGNED);
  206                                 if (err)
  207                                     *tl = txdr_unsigned(nfsrv_errmap(nd, err));
  208                                 else
  209                                     *tl = 0;
  210                         }
  211                         break;
  212                 }
  213         }
  214         *mbp = mb;
  215         *bposp = bpos;
  216         if (err != 0 && err != NFSERR_RETVOID)
  217                 nfsrvstats.srvrpc_errs++;
  218         return mreq;
  219 }
  220 
  221 
  222 /*
  223  *      nfs_realign:
  224  *
  225  *      Check for badly aligned mbuf data and realign by copying the unaligned
  226  *      portion of the data into a new mbuf chain and freeing the portions
  227  *      of the old chain that were replaced.
  228  *
  229  *      We cannot simply realign the data within the existing mbuf chain
  230  *      because the underlying buffers may contain other rpc commands and
  231  *      we cannot afford to overwrite them.
  232  *
  233  *      We would prefer to avoid this situation entirely.  The situation does
  234  *      not occur with NFS/UDP and is supposed to only occassionally occur
  235  *      with TCP.  Use vfs.nfs.realign_count and realign_test to check this.
  236  */
  237 static void
  238 nfs_realign(struct mbuf **pm, int hsiz) /* XXX COMMON */
  239 {
  240         struct mbuf *m;
  241         struct mbuf *n = NULL;
  242         int off = 0;
  243 
  244         ++nfs_realign_test;
  245         while ((m = *pm) != NULL) {
  246                 if ((m->m_len & 0x3) || (mtod(m, intptr_t) & 0x3)) {
  247                         MGET(n, M_TRYWAIT, MT_DATA);
  248                         if (m->m_len >= MINCLSIZE) {
  249                                 MCLGET(n, M_TRYWAIT);
  250                         }
  251                         n->m_len = 0;
  252                         break;
  253                 }
  254                 pm = &m->m_next;
  255         }
  256 
  257         /*
  258          * If n is non-NULL, loop on m copying data, then replace the
  259          * portion of the chain that had to be realigned.
  260          */
  261         if (n != NULL) {
  262                 ++nfs_realign_count;
  263                 while (m) {
  264                         m_copyback(n, off, m->m_len, mtod(m, caddr_t));
  265                         off += m->m_len;
  266                         m = m->m_next;
  267                 }
  268                 m_freem(*pm);
  269                 *pm = n;
  270         }
  271 }
  272 
  273 
  274 /*
  275  * Parse an RPC request
  276  * - verify it
  277  * - fill in the cred struct.
  278  */
  279 int
  280 nfs_getreq(struct nfsrv_descript *nd, struct nfsd *nfsd, int has_header)
  281 {
  282         int len, i;
  283         u_int32_t *tl;
  284         caddr_t dpos;
  285         u_int32_t nfsvers, auth_type;
  286         int error = 0;
  287         struct mbuf *mrep, *md;
  288 
  289         mrep = nd->nd_mrep;
  290         md = nd->nd_md;
  291         dpos = nd->nd_dpos;
  292         if (has_header) {
  293                 tl = nfsm_dissect(u_int32_t *, 10 * NFSX_UNSIGNED);
  294                 nd->nd_retxid = fxdr_unsigned(u_int32_t, *tl++);
  295                 if (*tl++ != nfsrv_rpc_call) {
  296                         m_freem(mrep);
  297                         return (EBADRPC);
  298                 }
  299         } else
  300                 tl = nfsm_dissect(u_int32_t *, 8 * NFSX_UNSIGNED);
  301         nd->nd_repstat = 0;
  302         nd->nd_flag = 0;
  303         if (*tl++ != nfsrv_rpc_vers) {
  304                 nd->nd_repstat = ERPCMISMATCH;
  305                 nd->nd_procnum = NFSPROC_NOOP;
  306                 return (0);
  307         }
  308         if (*tl != nfsrv_nfs_prog) {
  309                 nd->nd_repstat = EPROGUNAVAIL;
  310                 nd->nd_procnum = NFSPROC_NOOP;
  311                 return (0);
  312         }
  313         tl++;
  314         nfsvers = fxdr_unsigned(u_int32_t, *tl++);
  315         if (nfsvers < NFS_VER2 || nfsvers > NFS_VER3) {
  316                 nd->nd_repstat = EPROGMISMATCH;
  317                 nd->nd_procnum = NFSPROC_NOOP;
  318                 return (0);
  319         }
  320         nd->nd_procnum = fxdr_unsigned(u_int32_t, *tl++);
  321         if (nd->nd_procnum == NFSPROC_NULL)
  322                 return (0);
  323         if (nfsvers == NFS_VER3) {
  324                 nd->nd_flag = ND_NFSV3;
  325                 if (nd->nd_procnum >= NFS_NPROCS) {
  326                         nd->nd_repstat = EPROCUNAVAIL;
  327                         nd->nd_procnum = NFSPROC_NOOP;
  328                         return (0);
  329                 }
  330         } else {
  331                 if (nd->nd_procnum > NFSV2PROC_STATFS) {
  332                         nd->nd_repstat = EPROCUNAVAIL;
  333                         nd->nd_procnum = NFSPROC_NOOP;
  334                         return (0);
  335                 }
  336                 /* Map the v2 procedure numbers into v3 ones */
  337                 nd->nd_procnum = nfsrv_nfsv3_procid[nd->nd_procnum];
  338         }
  339         auth_type = *tl++;
  340         len = fxdr_unsigned(int, *tl++);
  341         if (len < 0 || len > RPCAUTH_MAXSIZ) {
  342                 m_freem(mrep);
  343                 return (EBADRPC);
  344         }
  345 
  346         /*
  347          * Handle auth_unix;
  348          */
  349         if (auth_type == nfsrv_rpc_auth_unix) {
  350                 len = fxdr_unsigned(int, *++tl);
  351                 if (len < 0 || len > NFS_MAXNAMLEN) {
  352                         m_freem(mrep);
  353                         return (EBADRPC);
  354                 }
  355                 nfsm_adv(nfsm_rndup(len));
  356                 tl = nfsm_dissect(u_int32_t *, 3 * NFSX_UNSIGNED);
  357                 /*
  358                  * XXX: This credential should be managed using crget(9)
  359                  * and related calls.  Right now, this tramples on any
  360                  * extensible data in the ucred, fails to initialize the
  361                  * mutex, and worse.  This must be fixed before FreeBSD
  362                  * 5.0-RELEASE.
  363                  */
  364                 bzero((caddr_t)&nd->nd_cr, sizeof (struct ucred));
  365                 nd->nd_cr.cr_ref = 1;
  366                 nd->nd_cr.cr_uid = fxdr_unsigned(uid_t, *tl++);
  367                 nd->nd_cr.cr_gid = fxdr_unsigned(gid_t, *tl++);
  368                 len = fxdr_unsigned(int, *tl);
  369                 if (len < 0 || len > RPCAUTH_UNIXGIDS) {
  370                         m_freem(mrep);
  371                         return (EBADRPC);
  372                 }
  373                 tl = nfsm_dissect(u_int32_t *, (len + 2) * NFSX_UNSIGNED);
  374                 for (i = 1; i <= len; i++)
  375                     if (i < NGROUPS)
  376                         nd->nd_cr.cr_groups[i] = fxdr_unsigned(gid_t, *tl++);
  377                     else
  378                         tl++;
  379                 nd->nd_cr.cr_ngroups = (len >= NGROUPS) ? NGROUPS : (len + 1);
  380                 if (nd->nd_cr.cr_ngroups > 1)
  381                     nfsrvw_sort(nd->nd_cr.cr_groups, nd->nd_cr.cr_ngroups);
  382                 len = fxdr_unsigned(int, *++tl);
  383                 if (len < 0 || len > RPCAUTH_MAXSIZ) {
  384                         m_freem(mrep);
  385                         return (EBADRPC);
  386                 }
  387                 if (len > 0)
  388                         nfsm_adv(nfsm_rndup(len));
  389         } else {
  390                 nd->nd_repstat = (NFSERR_AUTHERR | AUTH_REJECTCRED);
  391                 nd->nd_procnum = NFSPROC_NOOP;
  392                 return (0);
  393         }
  394 
  395         nd->nd_md = md;
  396         nd->nd_dpos = dpos;
  397         return (0);
  398 nfsmout:
  399         return (error);
  400 }
  401 
  402 /*
  403  * Socket upcall routine for the nfsd sockets.
  404  * The caddr_t arg is a pointer to the "struct nfssvc_sock".
  405  * Essentially do as much as possible non-blocking, else punt and it will
  406  * be called with M_TRYWAIT from an nfsd.
  407  */
  408 void
  409 nfsrv_rcv(struct socket *so, void *arg, int waitflag)
  410 {
  411         struct nfssvc_sock *slp = (struct nfssvc_sock *)arg;
  412         struct mbuf *m;
  413         struct mbuf *mp;
  414         struct sockaddr *nam;
  415         struct uio auio;
  416         int flags, error;
  417 
  418         if ((slp->ns_flag & SLP_VALID) == 0)
  419                 return;
  420 #ifdef notdef
  421         /*
  422          * Define this to test for nfsds handling this under heavy load.
  423          */
  424         if (waitflag == M_DONTWAIT) {
  425                 slp->ns_flag |= SLP_NEEDQ;
  426                 goto dorecs;
  427         }
  428 #endif
  429         auio.uio_td = NULL;
  430         if (so->so_type == SOCK_STREAM) {
  431                 /*
  432                  * If there are already records on the queue, defer soreceive()
  433                  * to an nfsd so that there is feedback to the TCP layer that
  434                  * the nfs servers are heavily loaded.
  435                  */
  436                 if (STAILQ_FIRST(&slp->ns_rec) && waitflag == M_DONTWAIT) {
  437                         slp->ns_flag |= SLP_NEEDQ;
  438                         goto dorecs;
  439                 }
  440 
  441                 /*
  442                  * Do soreceive().
  443                  */
  444                 auio.uio_resid = 1000000000;
  445                 flags = MSG_DONTWAIT;
  446                 error = so->so_proto->pr_usrreqs->pru_soreceive
  447                         (so, &nam, &auio, &mp, NULL, &flags);
  448                 if (error || mp == NULL) {
  449                         if (error == EWOULDBLOCK)
  450                                 slp->ns_flag |= SLP_NEEDQ;
  451                         else
  452                                 slp->ns_flag |= SLP_DISCONN;
  453                         goto dorecs;
  454                 }
  455                 m = mp;
  456                 if (slp->ns_rawend) {
  457                         slp->ns_rawend->m_next = m;
  458                         slp->ns_cc += 1000000000 - auio.uio_resid;
  459                 } else {
  460                         slp->ns_raw = m;
  461                         slp->ns_cc = 1000000000 - auio.uio_resid;
  462                 }
  463                 while (m->m_next)
  464                         m = m->m_next;
  465                 slp->ns_rawend = m;
  466 
  467                 /*
  468                  * Now try and parse record(s) out of the raw stream data.
  469                  */
  470                 error = nfsrv_getstream(slp, waitflag);
  471                 if (error) {
  472                         if (error == EPERM)
  473                                 slp->ns_flag |= SLP_DISCONN;
  474                         else
  475                                 slp->ns_flag |= SLP_NEEDQ;
  476                 }
  477         } else {
  478                 do {
  479                         auio.uio_resid = 1000000000;
  480                         flags = MSG_DONTWAIT;
  481                         error = so->so_proto->pr_usrreqs->pru_soreceive
  482                                 (so, &nam, &auio, &mp, NULL, &flags);
  483                         if (mp) {
  484                                 struct nfsrv_rec *rec;
  485                                 rec = malloc(sizeof(struct nfsrv_rec),
  486                                     M_NFSRVDESC, 
  487                                     waitflag == M_DONTWAIT ? M_NOWAIT : M_WAITOK);
  488                                 if (!rec) {
  489                                         if (nam)
  490                                                 FREE(nam, M_SONAME);
  491                                         m_freem(mp);
  492                                         continue;
  493                                 }
  494                                 nfs_realign(&mp, 10 * NFSX_UNSIGNED);
  495                                 rec->nr_address = nam;
  496                                 rec->nr_packet = mp;
  497                                 STAILQ_INSERT_TAIL(&slp->ns_rec, rec, nr_link);
  498                         }
  499                         if (error) {
  500                                 if ((so->so_proto->pr_flags & PR_CONNREQUIRED)
  501                                         && error != EWOULDBLOCK) {
  502                                         slp->ns_flag |= SLP_DISCONN;
  503                                         goto dorecs;
  504                                 }
  505                         }
  506                 } while (mp);
  507         }
  508 
  509         /*
  510          * Now try and process the request records, non-blocking.
  511          */
  512 dorecs:
  513         if (waitflag == M_DONTWAIT &&
  514                 (STAILQ_FIRST(&slp->ns_rec)
  515                  || (slp->ns_flag & (SLP_NEEDQ | SLP_DISCONN))))
  516                 nfsrv_wakenfsd(slp);
  517 }
  518 
  519 /*
  520  * Try and extract an RPC request from the mbuf data list received on a
  521  * stream socket. The "waitflag" argument indicates whether or not it
  522  * can sleep.
  523  */
  524 static int
  525 nfsrv_getstream(struct nfssvc_sock *slp, int waitflag)
  526 {
  527         struct mbuf *m, **mpp;
  528         char *cp1, *cp2;
  529         int len;
  530         struct mbuf *om, *m2, *recm;
  531         u_int32_t recmark;
  532 
  533         if (slp->ns_flag & SLP_GETSTREAM)
  534                 panic("nfs getstream");
  535         slp->ns_flag |= SLP_GETSTREAM;
  536         for (;;) {
  537             if (slp->ns_reclen == 0) {
  538                 if (slp->ns_cc < NFSX_UNSIGNED) {
  539                         slp->ns_flag &= ~SLP_GETSTREAM;
  540                         return (0);
  541                 }
  542                 m = slp->ns_raw;
  543                 if (m->m_len >= NFSX_UNSIGNED) {
  544                         bcopy(mtod(m, caddr_t), (caddr_t)&recmark, NFSX_UNSIGNED);
  545                         m->m_data += NFSX_UNSIGNED;
  546                         m->m_len -= NFSX_UNSIGNED;
  547                 } else {
  548                         cp1 = (caddr_t)&recmark;
  549                         cp2 = mtod(m, caddr_t);
  550                         while (cp1 < ((caddr_t)&recmark) + NFSX_UNSIGNED) {
  551                                 while (m->m_len == 0) {
  552                                         m = m->m_next;
  553                                         cp2 = mtod(m, caddr_t);
  554                                 }
  555                                 *cp1++ = *cp2++;
  556                                 m->m_data++;
  557                                 m->m_len--;
  558                         }
  559                 }
  560                 slp->ns_cc -= NFSX_UNSIGNED;
  561                 recmark = ntohl(recmark);
  562                 slp->ns_reclen = recmark & ~0x80000000;
  563                 if (recmark & 0x80000000)
  564                         slp->ns_flag |= SLP_LASTFRAG;
  565                 else
  566                         slp->ns_flag &= ~SLP_LASTFRAG;
  567                 if (slp->ns_reclen > NFS_MAXPACKET) {
  568                         slp->ns_flag &= ~SLP_GETSTREAM;
  569                         return (EPERM);
  570                 }
  571             }
  572 
  573             /*
  574              * Now get the record part.
  575              *
  576              * Note that slp->ns_reclen may be 0.  Linux sometimes
  577              * generates 0-length RPCs.
  578              */
  579             recm = NULL;
  580             if (slp->ns_cc == slp->ns_reclen) {
  581                 recm = slp->ns_raw;
  582                 slp->ns_raw = slp->ns_rawend = NULL;
  583                 slp->ns_cc = slp->ns_reclen = 0;
  584             } else if (slp->ns_cc > slp->ns_reclen) {
  585                 len = 0;
  586                 m = slp->ns_raw;
  587                 om = NULL;
  588 
  589                 while (len < slp->ns_reclen) {
  590                         if ((len + m->m_len) > slp->ns_reclen) {
  591                                 m2 = m_copym(m, 0, slp->ns_reclen - len,
  592                                         waitflag);
  593                                 if (m2) {
  594                                         if (om) {
  595                                                 om->m_next = m2;
  596                                                 recm = slp->ns_raw;
  597                                         } else
  598                                                 recm = m2;
  599                                         m->m_data += slp->ns_reclen - len;
  600                                         m->m_len -= slp->ns_reclen - len;
  601                                         len = slp->ns_reclen;
  602                                 } else {
  603                                         slp->ns_flag &= ~SLP_GETSTREAM;
  604                                         return (EWOULDBLOCK);
  605                                 }
  606                         } else if ((len + m->m_len) == slp->ns_reclen) {
  607                                 om = m;
  608                                 len += m->m_len;
  609                                 m = m->m_next;
  610                                 recm = slp->ns_raw;
  611                                 om->m_next = NULL;
  612                         } else {
  613                                 om = m;
  614                                 len += m->m_len;
  615                                 m = m->m_next;
  616                         }
  617                 }
  618                 slp->ns_raw = m;
  619                 slp->ns_cc -= len;
  620                 slp->ns_reclen = 0;
  621             } else {
  622                 slp->ns_flag &= ~SLP_GETSTREAM;
  623                 return (0);
  624             }
  625 
  626             /*
  627              * Accumulate the fragments into a record.
  628              */
  629             mpp = &slp->ns_frag;
  630             while (*mpp)
  631                 mpp = &((*mpp)->m_next);
  632             *mpp = recm;
  633             if (slp->ns_flag & SLP_LASTFRAG) {
  634                 struct nfsrv_rec *rec;
  635                 rec = malloc(sizeof(struct nfsrv_rec), M_NFSRVDESC,
  636                     waitflag == M_DONTWAIT ? M_NOWAIT : M_WAITOK);
  637                 if (!rec) {
  638                     m_freem(slp->ns_frag);
  639                 } else {
  640                     nfs_realign(&slp->ns_frag, 10 * NFSX_UNSIGNED);
  641                     rec->nr_address = NULL;
  642                     rec->nr_packet = slp->ns_frag;
  643                     STAILQ_INSERT_TAIL(&slp->ns_rec, rec, nr_link);
  644                 }
  645                 slp->ns_frag = NULL;
  646             }
  647         }
  648 }
  649 
  650 /*
  651  * Parse an RPC header.
  652  */
  653 int
  654 nfsrv_dorec(struct nfssvc_sock *slp, struct nfsd *nfsd,
  655     struct nfsrv_descript **ndp)
  656 {
  657         struct nfsrv_rec *rec;
  658         struct mbuf *m;
  659         struct sockaddr *nam;
  660         struct nfsrv_descript *nd;
  661         int error;
  662 
  663         *ndp = NULL;
  664         if ((slp->ns_flag & SLP_VALID) == 0 || !STAILQ_FIRST(&slp->ns_rec))
  665                 return (ENOBUFS);
  666         rec = STAILQ_FIRST(&slp->ns_rec);
  667         STAILQ_REMOVE_HEAD(&slp->ns_rec, nr_link);
  668         nam = rec->nr_address;
  669         m = rec->nr_packet;
  670         free(rec, M_NFSRVDESC);
  671         MALLOC(nd, struct nfsrv_descript *, sizeof (struct nfsrv_descript),
  672                 M_NFSRVDESC, M_WAITOK);
  673         nd->nd_md = nd->nd_mrep = m;
  674         nd->nd_nam2 = nam;
  675         nd->nd_dpos = mtod(m, caddr_t);
  676         error = nfs_getreq(nd, nfsd, TRUE);
  677         if (error) {
  678                 if (nam) {
  679                         FREE(nam, M_SONAME);
  680                 }
  681                 free((caddr_t)nd, M_NFSRVDESC);
  682                 return (error);
  683         }
  684         *ndp = nd;
  685         nfsd->nfsd_nd = nd;
  686         return (0);
  687 }
  688 
  689 /*
  690  * Search for a sleeping nfsd and wake it up.
  691  * SIDE EFFECT: If none found, set NFSD_CHECKSLP flag, so that one of the
  692  * running nfsds will go look for the work in the nfssvc_sock list.
  693  */
  694 void
  695 nfsrv_wakenfsd(struct nfssvc_sock *slp)
  696 {
  697         struct nfsd *nd;
  698 
  699         if ((slp->ns_flag & SLP_VALID) == 0)
  700                 return;
  701         TAILQ_FOREACH(nd, &nfsd_head, nfsd_chain) {
  702                 if (nd->nfsd_flag & NFSD_WAITING) {
  703                         nd->nfsd_flag &= ~NFSD_WAITING;
  704                         if (nd->nfsd_slp)
  705                                 panic("nfsd wakeup");
  706                         slp->ns_sref++;
  707                         nd->nfsd_slp = slp;
  708                         wakeup(nd);
  709                         return;
  710                 }
  711         }
  712         slp->ns_flag |= SLP_DOREC;
  713         nfsd_head_flag |= NFSD_CHECKSLP;
  714 }
  715 
  716 /*
  717  * This is the nfs send routine.
  718  * For the server side:
  719  * - return EINTR or ERESTART if interrupted by a signal
  720  * - return EPIPE if a connection is lost for connection based sockets (TCP...)
  721  * - do any cleanup required by recoverable socket errors (?)
  722  */
  723 int
  724 nfsrv_send(struct socket *so, struct sockaddr *nam, struct mbuf *top)
  725 {
  726         struct sockaddr *sendnam;
  727         int error, soflags, flags;
  728 
  729         soflags = so->so_proto->pr_flags;
  730         if ((soflags & PR_CONNREQUIRED) || (so->so_state & SS_ISCONNECTED))
  731                 sendnam = NULL;
  732         else
  733                 sendnam = nam;
  734         if (so->so_type == SOCK_SEQPACKET)
  735                 flags = MSG_EOR;
  736         else
  737                 flags = 0;
  738 
  739         error = so->so_proto->pr_usrreqs->pru_sosend(so, sendnam, 0, top, 0,
  740                                                      flags, curthread/*XXX*/);
  741         if (error == ENOBUFS && so->so_type == SOCK_DGRAM)
  742                 error = 0;
  743 
  744         if (error) {
  745                 log(LOG_INFO, "nfsd send error %d\n", error);
  746 
  747                 /*
  748                  * Handle any recoverable (soft) socket errors here. (?)
  749                  */
  750                 if (error != EINTR && error != ERESTART &&
  751                     error != EWOULDBLOCK && error != EPIPE)
  752                         error = 0;
  753         }
  754         return (error);
  755 }
  756 
  757 /*
  758  * NFS server timer routine.
  759  */
  760 void
  761 nfsrv_timer(void *arg)
  762 {
  763         int s;
  764         struct nfssvc_sock *slp;
  765         u_quad_t cur_usec;
  766 
  767         s = splnet();
  768         /*
  769          * Scan the write gathering queues for writes that need to be
  770          * completed now.
  771          */
  772         cur_usec = nfs_curusec();
  773         TAILQ_FOREACH(slp, &nfssvc_sockhead, ns_chain) {
  774                 if (LIST_FIRST(&slp->ns_tq) &&
  775                     LIST_FIRST(&slp->ns_tq)->nd_time <= cur_usec)
  776                         nfsrv_wakenfsd(slp);
  777         }
  778         splx(s);
  779         nfsrv_timer_handle = timeout(nfsrv_timer, NULL, nfsrv_ticks);
  780 }

Cache object: 9df02f28025f6479b88d8ac8dc2633f8


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