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

Cache object: 293d30c721d4b564e8aa3d105409b94d


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