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_syscalls.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, 1993
    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  * 4. Neither the name of the University nor the names of its contributors
   17  *    may be used to endorse or promote products derived from this software
   18  *    without specific prior written permission.
   19  *
   20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   30  * SUCH DAMAGE.
   31  *
   32  *      @(#)nfs_syscalls.c      8.5 (Berkeley) 3/30/95
   33  */
   34 
   35 #include <sys/cdefs.h>
   36 __FBSDID("$FreeBSD: releng/7.4/sys/nfsserver/nfs_syscalls.c 181123 2008-08-01 11:11:12Z rwatson $");
   37 
   38 #include "opt_inet6.h"
   39 
   40 #include <sys/param.h>
   41 #include <sys/systm.h>
   42 #include <sys/sysproto.h>
   43 #include <sys/kernel.h>
   44 #include <sys/sysctl.h>
   45 #include <sys/file.h>
   46 #include <sys/filedesc.h>
   47 #include <sys/vnode.h>
   48 #include <sys/malloc.h>
   49 #include <sys/mount.h>
   50 #include <sys/priv.h>
   51 #include <sys/proc.h>
   52 #include <sys/bio.h>
   53 #include <sys/buf.h>
   54 #include <sys/mbuf.h>
   55 #include <sys/socket.h>
   56 #include <sys/socketvar.h>
   57 #include <sys/domain.h>
   58 #include <sys/protosw.h>
   59 #include <sys/namei.h>
   60 #include <sys/fcntl.h>
   61 #include <sys/lockf.h>
   62 
   63 #include <netinet/in.h>
   64 #include <netinet/tcp.h>
   65 #ifdef INET6
   66 #include <net/if.h>
   67 #include <netinet6/in6_var.h>
   68 #endif
   69 #include <nfs/xdr_subs.h>
   70 #include <nfs/rpcv2.h>
   71 #include <nfs/nfsproto.h>
   72 #include <nfsserver/nfs.h>
   73 #include <nfsserver/nfsm_subs.h>
   74 #include <nfsserver/nfsrvcache.h>
   75 
   76 static MALLOC_DEFINE(M_NFSSVC, "nfss_srvsock", "Nfs server structure");
   77 
   78 MALLOC_DEFINE(M_NFSRVDESC, "nfss_srvdesc", "NFS server socket descriptor");
   79 MALLOC_DEFINE(M_NFSD, "nfss_daemon", "Nfs server daemon structure");
   80 
   81 #define TRUE    1
   82 #define FALSE   0
   83 
   84 SYSCTL_DECL(_vfs_nfsrv);
   85 
   86 int             nfsd_waiting = 0;
   87 int             nfsrv_numnfsd = 0;
   88 static int      notstarted = 1;
   89 
   90 static int      nfs_privport = 0;
   91 SYSCTL_INT(_vfs_nfsrv, NFS_NFSPRIVPORT, nfs_privport, CTLFLAG_RW,
   92             &nfs_privport, 0, "");
   93 SYSCTL_INT(_vfs_nfsrv, OID_AUTO, gatherdelay, CTLFLAG_RW,
   94             &nfsrvw_procrastinate, 0, "");
   95 SYSCTL_INT(_vfs_nfsrv, OID_AUTO, gatherdelay_v3, CTLFLAG_RW,
   96             &nfsrvw_procrastinate_v3, 0, "");
   97 
   98 static int      nfssvc_addsock(struct file *, struct sockaddr *,
   99                     struct thread *);
  100 static void     nfsrv_zapsock(struct nfssvc_sock *slp);
  101 static int      nfssvc_nfsd(struct thread *);
  102 
  103 extern u_long sb_max_adj;
  104 
  105 /*
  106  * NFS server system calls
  107  */
  108 
  109 /*
  110  * Nfs server psuedo system call for the nfsd's
  111  * Based on the flag value it either:
  112  * - adds a socket to the selection list
  113  * - remains in the kernel as an nfsd
  114  * - remains in the kernel as an nfsiod
  115  * For INET6 we suppose that nfsd provides only IN6P_IPV6_V6ONLY sockets
  116  * and that mountd provides
  117  *  - sockaddr with no IPv4-mapped addresses
  118  *  - mask for both INET and INET6 families if there is IPv4-mapped overlap
  119  */
  120 #ifndef _SYS_SYSPROTO_H_
  121 struct nfssvc_args {
  122         int flag;
  123         caddr_t argp;
  124 };
  125 #endif
  126 int
  127 nfssvc(struct thread *td, struct nfssvc_args *uap)
  128 {
  129         struct file *fp;
  130         struct sockaddr *nam;
  131         struct nfsd_args nfsdarg;
  132         int error;
  133 
  134         KASSERT(!mtx_owned(&Giant), ("nfssvc(): called with Giant"));
  135 
  136         error = priv_check(td, PRIV_NFS_DAEMON);
  137         if (error)
  138                 return (error);
  139         NFSD_LOCK();
  140         while (nfssvc_sockhead_flag & SLP_INIT) {
  141                  nfssvc_sockhead_flag |= SLP_WANTINIT;
  142                 (void) msleep(&nfssvc_sockhead, &nfsd_mtx, PSOCK,
  143                     "nfsd init", 0);
  144         }
  145         NFSD_UNLOCK();
  146         if (uap->flag & NFSSVC_ADDSOCK) {
  147                 error = copyin(uap->argp, (caddr_t)&nfsdarg, sizeof(nfsdarg));
  148                 if (error)
  149                         return (error);
  150                 if ((error = fget(td, nfsdarg.sock, &fp)) != 0)
  151                         return (error);
  152                 if (fp->f_type != DTYPE_SOCKET) {
  153                         fdrop(fp, td);
  154                         return (error); /* XXXRW: Should be EINVAL? */
  155                 }
  156                 /*
  157                  * Get the client address for connected sockets.
  158                  */
  159                 if (nfsdarg.name == NULL || nfsdarg.namelen == 0)
  160                         nam = NULL;
  161                 else {
  162                         error = getsockaddr(&nam, nfsdarg.name,
  163                                             nfsdarg.namelen);
  164                         if (error) {
  165                                 fdrop(fp, td);
  166                                 return (error);
  167                         }
  168                 }
  169                 error = nfssvc_addsock(fp, nam, td);
  170                 fdrop(fp, td);
  171         } else if (uap->flag & NFSSVC_NFSD) {
  172                 error = nfssvc_nfsd(td);
  173         } else {
  174                 error = ENXIO;
  175         }
  176         if (error == EINTR || error == ERESTART)
  177                 error = 0;
  178         return (error);
  179 }
  180 
  181 /*
  182  * Adds a socket to the list for servicing by nfsds.
  183  */
  184 static int
  185 nfssvc_addsock(struct file *fp, struct sockaddr *mynam, struct thread *td)
  186 {
  187         int siz;
  188         struct nfssvc_sock *slp;
  189         struct socket *so;
  190         int error;
  191 
  192         so = fp->f_data;
  193 #if 0
  194         /*
  195          * XXXRW: If this code is ever enabled, there's a race when running
  196          * MPSAFE.
  197          */
  198         tslp = NULL;
  199         /*
  200          * Add it to the list, as required.
  201          */
  202         if (so->so_proto->pr_protocol == IPPROTO_UDP) {
  203                 tslp = nfs_udpsock;
  204                 if (tslp->ns_flag & SLP_VALID) {
  205                         if (mynam != NULL)
  206                                 FREE(mynam, M_SONAME);
  207                         return (EPERM);
  208                 }
  209         }
  210 #endif
  211         siz = sb_max_adj;
  212         error = soreserve(so, siz, siz);
  213         if (error) {
  214                 if (mynam != NULL)
  215                         FREE(mynam, M_SONAME);
  216                 return (error);
  217         }
  218 
  219         /*
  220          * Set protocol specific options { for now TCP only } and
  221          * reserve some space. For datagram sockets, this can get called
  222          * repeatedly for the same socket, but that isn't harmful.
  223          */
  224         if (so->so_type == SOCK_STREAM) {
  225                 struct sockopt sopt;
  226                 int val;
  227 
  228                 bzero(&sopt, sizeof sopt);
  229                 sopt.sopt_dir = SOPT_SET;
  230                 sopt.sopt_level = SOL_SOCKET;
  231                 sopt.sopt_name = SO_KEEPALIVE;
  232                 sopt.sopt_val = &val;
  233                 sopt.sopt_valsize = sizeof val;
  234                 val = 1;
  235                 sosetopt(so, &sopt);
  236         }
  237         if (so->so_proto->pr_protocol == IPPROTO_TCP) {
  238                 struct sockopt sopt;
  239                 int val;
  240 
  241                 bzero(&sopt, sizeof sopt);
  242                 sopt.sopt_dir = SOPT_SET;
  243                 sopt.sopt_level = IPPROTO_TCP;
  244                 sopt.sopt_name = TCP_NODELAY;
  245                 sopt.sopt_val = &val;
  246                 sopt.sopt_valsize = sizeof val;
  247                 val = 1;
  248                 sosetopt(so, &sopt);
  249         }
  250         SOCKBUF_LOCK(&so->so_rcv);
  251         so->so_rcv.sb_flags &= ~SB_NOINTR;
  252         so->so_rcv.sb_timeo = 0;
  253         SOCKBUF_UNLOCK(&so->so_rcv);
  254         SOCKBUF_LOCK(&so->so_snd);
  255         so->so_snd.sb_flags &= ~SB_NOINTR;
  256         so->so_snd.sb_timeo = 0;
  257         SOCKBUF_UNLOCK(&so->so_snd);
  258 
  259         slp = (struct nfssvc_sock *)
  260                 malloc(sizeof (struct nfssvc_sock), M_NFSSVC,
  261                 M_WAITOK | M_ZERO);
  262         STAILQ_INIT(&slp->ns_rec);
  263         NFSD_LOCK();
  264         TAILQ_INSERT_TAIL(&nfssvc_sockhead, slp, ns_chain);
  265 
  266         slp->ns_so = so;
  267         slp->ns_nam = mynam;
  268         fhold(fp);
  269         slp->ns_fp = fp;
  270         SOCKBUF_LOCK(&so->so_rcv);
  271         so->so_upcallarg = (caddr_t)slp;
  272         so->so_upcall = nfsrv_rcv;
  273         so->so_rcv.sb_flags |= SB_UPCALL;
  274         SOCKBUF_UNLOCK(&so->so_rcv);
  275         slp->ns_flag = (SLP_VALID | SLP_NEEDQ);
  276         nfsrv_wakenfsd(slp);
  277         NFSD_UNLOCK();
  278         return (0);
  279 }
  280 
  281 /*
  282  * Called by nfssvc() for nfsds. Just loops around servicing rpc requests
  283  * until it is killed by a signal.
  284  */
  285 static int
  286 nfssvc_nfsd(struct thread *td)
  287 {
  288         int siz;
  289         struct nfssvc_sock *slp;
  290         struct nfsd *nfsd;
  291         struct nfsrv_descript *nd = NULL;
  292         struct mbuf *m, *mreq;
  293         int error = 0, cacherep, s, sotype, writes_todo;
  294         int procrastinate;
  295         u_quad_t cur_usec;
  296 
  297 #ifndef nolint
  298         cacherep = RC_DOIT;
  299         writes_todo = 0;
  300 #endif
  301         nfsd = (struct nfsd *)
  302                 malloc(sizeof (struct nfsd), M_NFSD, M_WAITOK | M_ZERO);
  303         s = splnet();
  304         NFSD_LOCK();
  305 
  306         nfsd->nfsd_td = td;
  307         TAILQ_INSERT_TAIL(&nfsd_head, nfsd, nfsd_chain);
  308         nfsrv_numnfsd++;
  309 
  310         /*
  311          * Loop getting rpc requests until SIGKILL.
  312          */
  313         for (;;) {
  314                 if ((nfsd->nfsd_flag & NFSD_REQINPROG) == 0) {
  315                         while (nfsd->nfsd_slp == NULL &&
  316                             (nfsd_head_flag & NFSD_CHECKSLP) == 0) {
  317                                 nfsd->nfsd_flag |= NFSD_WAITING;
  318                                 nfsd_waiting++;
  319                                 error = msleep(nfsd, &nfsd_mtx,
  320                                     PSOCK | PCATCH, "-", 0);
  321                                 nfsd_waiting--;
  322                                 if (error)
  323                                         goto done;
  324                         }
  325                         if (nfsd->nfsd_slp == NULL &&
  326                             (nfsd_head_flag & NFSD_CHECKSLP) != 0) {
  327                                 TAILQ_FOREACH(slp, &nfssvc_sockhead, ns_chain) {
  328                                     if ((slp->ns_flag & (SLP_VALID | SLP_DOREC))
  329                                         == (SLP_VALID | SLP_DOREC)) {
  330                                             slp->ns_flag &= ~SLP_DOREC;
  331                                             slp->ns_sref++;
  332                                             nfsd->nfsd_slp = slp;
  333                                             break;
  334                                     }
  335                                 }
  336                                 if (slp == NULL)
  337                                         nfsd_head_flag &= ~NFSD_CHECKSLP;
  338                         }
  339                         if ((slp = nfsd->nfsd_slp) == NULL)
  340                                 continue;
  341                         if (slp->ns_flag & SLP_VALID) {
  342                                 if (slp->ns_flag & SLP_DISCONN)
  343                                         nfsrv_zapsock(slp);
  344                                 else if (slp->ns_flag & SLP_NEEDQ) {
  345                                         slp->ns_flag &= ~SLP_NEEDQ;
  346                                         (void) nfs_slplock(slp, 1);
  347                                         NFSD_UNLOCK();
  348                                         nfsrv_rcv(slp->ns_so, (caddr_t)slp,
  349                                                 M_TRYWAIT);
  350                                         NFSD_LOCK();
  351                                         nfs_slpunlock(slp);
  352                                 }
  353                                 error = nfsrv_dorec(slp, nfsd, &nd);
  354                                 cur_usec = nfs_curusec();
  355                                 if (error && LIST_FIRST(&slp->ns_tq) &&
  356                                     LIST_FIRST(&slp->ns_tq)->nd_time <= cur_usec) {
  357                                         error = 0;
  358                                         cacherep = RC_DOIT;
  359                                         writes_todo = 1;
  360                                 } else
  361                                         writes_todo = 0;
  362                                 nfsd->nfsd_flag |= NFSD_REQINPROG;
  363                         }
  364                 } else {
  365                         error = 0;
  366                         slp = nfsd->nfsd_slp;
  367                 }
  368                 if (error || (slp->ns_flag & SLP_VALID) == 0) {
  369                         if (nd) {
  370                                 if (nd->nd_cr != NULL)
  371                                         crfree(nd->nd_cr);
  372                                 free((caddr_t)nd, M_NFSRVDESC);
  373                                 nd = NULL;
  374                         }
  375                         nfsd->nfsd_slp = NULL;
  376                         nfsd->nfsd_flag &= ~NFSD_REQINPROG;
  377                         nfsrv_slpderef(slp);
  378                         continue;
  379                 }
  380                 splx(s);
  381                 sotype = slp->ns_so->so_type;
  382                 if (nd) {
  383                     getmicrotime(&nd->nd_starttime);
  384                     if (nd->nd_nam2)
  385                         nd->nd_nam = nd->nd_nam2;
  386                     else
  387                         nd->nd_nam = slp->ns_nam;
  388 
  389                     /*
  390                      * Check to see if authorization is needed.
  391                      */
  392                     cacherep = nfsrv_getcache(nd, &mreq);
  393 
  394                     if (nfs_privport) {
  395                         /* Check if source port is privileged */
  396                         u_short port;
  397                         struct sockaddr *nam = nd->nd_nam;
  398                         struct sockaddr_in *sin;
  399 
  400                         sin = (struct sockaddr_in *)nam;
  401                         /*
  402                          * INET/INET6 - same code:
  403                          *    sin_port and sin6_port are at same offset
  404                          */
  405                         port = ntohs(sin->sin_port);
  406                         if (port >= IPPORT_RESERVED &&
  407                             nd->nd_procnum != NFSPROC_NULL) {
  408 #ifdef INET6
  409                             char b6[INET6_ADDRSTRLEN];
  410 #if defined(KLD_MODULE)
  411         /* Do not use ip6_sprintf: the nfs module should work without INET6. */
  412 #define ip6_sprintf(buf, a) \
  413          (sprintf((buf), "%x:%x:%x:%x:%x:%x:%x:%x", \
  414                   (a)->s6_addr16[0], (a)->s6_addr16[1], \
  415                   (a)->s6_addr16[2], (a)->s6_addr16[3], \
  416                   (a)->s6_addr16[4], (a)->s6_addr16[5], \
  417                   (a)->s6_addr16[6], (a)->s6_addr16[7]), \
  418          (buf))
  419 #endif
  420 #endif
  421                             nd->nd_procnum = NFSPROC_NOOP;
  422                             nd->nd_repstat = (NFSERR_AUTHERR | AUTH_TOOWEAK);
  423                             cacherep = RC_DOIT;
  424                             printf("NFS request from unprivileged port (%s:%d)\n",
  425 #ifdef INET6
  426                                 sin->sin_family == AF_INET6 ?
  427                                     ip6_sprintf(b6, &satosin6(sin)->sin6_addr) :
  428 #if defined(KLD_MODULE)
  429 #undef ip6_sprintf
  430 #endif
  431 #endif
  432                                     inet_ntoa(sin->sin_addr), port);
  433                         }
  434                     }
  435 
  436                 }
  437 
  438                 /*
  439                  * Loop to get all the write rpc relies that have been
  440                  * gathered together.
  441                  */
  442                 do {
  443                     switch (cacherep) {
  444                     case RC_DOIT:
  445                         if (nd && (nd->nd_flag & ND_NFSV3))
  446                             procrastinate = nfsrvw_procrastinate_v3;
  447                         else
  448                             procrastinate = nfsrvw_procrastinate;
  449                         NFSD_UNLOCK();
  450                         if (writes_todo || (!(nd->nd_flag & ND_NFSV3) &&
  451                             nd->nd_procnum == NFSPROC_WRITE &&
  452                             procrastinate > 0 && !notstarted))
  453                             error = nfsrv_writegather(&nd, slp,
  454                                 nfsd->nfsd_td, &mreq);
  455                         else
  456                             error = (*(nfsrv3_procs[nd->nd_procnum]))(nd,
  457                                 slp, nfsd->nfsd_td, &mreq);
  458                         NFSD_LOCK();
  459                         if (mreq == NULL)
  460                                 break;
  461                         if (error != 0 && error != NFSERR_RETVOID) {
  462                                 nfsrvstats.srv_errs++;
  463                                 nfsrv_updatecache(nd, FALSE, mreq);
  464                                 if (nd->nd_nam2)
  465                                         FREE(nd->nd_nam2, M_SONAME);
  466                                 break;
  467                         }
  468                         nfsrvstats.srvrpccnt[nd->nd_procnum]++;
  469                         nfsrv_updatecache(nd, TRUE, mreq);
  470                         nd->nd_mrep = NULL;
  471                         /* FALLTHROUGH */
  472                     case RC_REPLY:
  473                         NFSD_UNLOCK();
  474                         siz = m_length(mreq, NULL);
  475                         if (siz <= 0 || siz > NFS_MAXPACKET) {
  476                                 printf("mbuf siz=%d\n",siz);
  477                                 panic("Bad nfs svc reply");
  478                         }
  479                         m = mreq;
  480                         m->m_pkthdr.len = siz;
  481                         m->m_pkthdr.rcvif = NULL;
  482                         /*
  483                          * For stream protocols, prepend a Sun RPC
  484                          * Record Mark.
  485                          */
  486                         if (sotype == SOCK_STREAM) {
  487                                 M_PREPEND(m, NFSX_UNSIGNED, M_TRYWAIT);
  488                                 *mtod(m, u_int32_t *) = htonl(0x80000000 | siz);
  489                         }
  490                         NFSD_LOCK();
  491                         if (slp->ns_so->so_proto->pr_flags & PR_CONNREQUIRED)
  492                                 (void) nfs_slplock(slp, 1);
  493                         if (slp->ns_flag & SLP_VALID) {
  494                             NFSD_UNLOCK();
  495                             error = nfsrv_send(slp->ns_so, nd->nd_nam2, m);
  496                             NFSD_LOCK();
  497                         } else {
  498                             error = EPIPE;
  499                             m_freem(m);
  500                         }
  501                         if (nd->nd_nam2)
  502                                 FREE(nd->nd_nam2, M_SONAME);
  503                         if (nd->nd_mrep)
  504                                 m_freem(nd->nd_mrep);
  505                         if (error == EPIPE)
  506                                 nfsrv_zapsock(slp);
  507                         if (slp->ns_so->so_proto->pr_flags & PR_CONNREQUIRED)
  508                                 nfs_slpunlock(slp);
  509                         if (error == EINTR || error == ERESTART) {
  510                                 if (nd->nd_cr != NULL)
  511                                         crfree(nd->nd_cr);
  512                                 free((caddr_t)nd, M_NFSRVDESC);
  513                                 nfsrv_slpderef(slp);
  514                                 s = splnet();
  515                                 goto done;
  516                         }
  517                         break;
  518                     case RC_DROPIT:
  519                         m_freem(nd->nd_mrep);
  520                         if (nd->nd_nam2)
  521                                 FREE(nd->nd_nam2, M_SONAME);
  522                         break;
  523                     };
  524                     if (nd) {
  525                         if (nd->nd_cr != NULL)
  526                                 crfree(nd->nd_cr);
  527                         FREE((caddr_t)nd, M_NFSRVDESC);
  528                         nd = NULL;
  529                     }
  530 
  531                     /*
  532                      * Check to see if there are outstanding writes that
  533                      * need to be serviced.
  534                      */
  535                     cur_usec = nfs_curusec();
  536                     s = splsoftclock();
  537                     if (LIST_FIRST(&slp->ns_tq) &&
  538                         LIST_FIRST(&slp->ns_tq)->nd_time <= cur_usec) {
  539                         cacherep = RC_DOIT;
  540                         writes_todo = 1;
  541                     } else
  542                         writes_todo = 0;
  543                     splx(s);
  544                 } while (writes_todo);
  545                 s = splnet();
  546                 if (nfsrv_dorec(slp, nfsd, &nd)) {
  547                         nfsd->nfsd_flag &= ~NFSD_REQINPROG;
  548                         nfsd->nfsd_slp = NULL;
  549                         nfsrv_slpderef(slp);
  550                 }
  551                 mtx_assert(&Giant, MA_NOTOWNED);
  552         }
  553 done:
  554         mtx_assert(&Giant, MA_NOTOWNED);
  555         TAILQ_REMOVE(&nfsd_head, nfsd, nfsd_chain);
  556         splx(s);
  557         free((caddr_t)nfsd, M_NFSD);
  558         if (--nfsrv_numnfsd == 0)
  559                 nfsrv_init(TRUE);       /* Reinitialize everything */
  560         NFSD_UNLOCK();
  561         return (error);
  562 }
  563 
  564 /*
  565  * Shut down a socket associated with an nfssvc_sock structure.
  566  * Should be called with the send lock set, if required.
  567  * The trick here is to increment the sref at the start, so that the nfsds
  568  * will stop using it and clear ns_flag at the end so that it will not be
  569  * reassigned during cleanup.
  570  */
  571 static void
  572 nfsrv_zapsock(struct nfssvc_sock *slp)
  573 {
  574         struct nfsrv_descript *nwp, *nnwp;
  575         struct socket *so;
  576         struct file *fp;
  577         struct nfsrv_rec *rec;
  578         int s;
  579 
  580         NFSD_LOCK_ASSERT();
  581 
  582         /*
  583          * XXXRW: By clearing all flags, other threads/etc should ignore
  584          * this slp and we can safely release nfsd_mtx so we can clean
  585          * up the slp safely.
  586          */
  587         slp->ns_flag &= ~SLP_ALLFLAGS;
  588         fp = slp->ns_fp;
  589         if (fp) {
  590                 NFSD_UNLOCK();
  591                 slp->ns_fp = NULL;
  592                 so = slp->ns_so;
  593                 SOCKBUF_LOCK(&so->so_rcv);
  594                 so->so_rcv.sb_flags &= ~SB_UPCALL;
  595                 so->so_upcall = NULL;
  596                 so->so_upcallarg = NULL;
  597                 SOCKBUF_UNLOCK(&so->so_rcv);
  598                 soshutdown(so, SHUT_RDWR);
  599                 closef(fp, NULL);
  600                 NFSD_LOCK();
  601                 if (slp->ns_nam)
  602                         FREE(slp->ns_nam, M_SONAME);
  603                 m_freem(slp->ns_raw);
  604                 while ((rec = STAILQ_FIRST(&slp->ns_rec)) != NULL) {
  605                         STAILQ_REMOVE_HEAD(&slp->ns_rec, nr_link);
  606                         if (rec->nr_address)
  607                                 FREE(rec->nr_address, M_SONAME);
  608                         m_freem(rec->nr_packet);
  609                         free(rec, M_NFSRVDESC);
  610                 }
  611                 s = splsoftclock();
  612                 for (nwp = LIST_FIRST(&slp->ns_tq); nwp; nwp = nnwp) {
  613                         nnwp = LIST_NEXT(nwp, nd_tq);
  614                         LIST_REMOVE(nwp, nd_tq);
  615                         if (nwp->nd_cr != NULL)
  616                                 crfree(nwp->nd_cr);
  617                         free((caddr_t)nwp, M_NFSRVDESC);
  618                 }
  619                 LIST_INIT(&slp->ns_tq);
  620                 splx(s);
  621         }
  622 }
  623 
  624 /*
  625  * Derefence a server socket structure. If it has no more references and
  626  * is no longer valid, you can throw it away.
  627  */
  628 void
  629 nfsrv_slpderef(struct nfssvc_sock *slp)
  630 {
  631 
  632         NFSD_LOCK_ASSERT();
  633 
  634         if (--(slp->ns_sref) == 0 && (slp->ns_flag & SLP_VALID) == 0) {
  635                 TAILQ_REMOVE(&nfssvc_sockhead, slp, ns_chain);
  636                 free((caddr_t)slp, M_NFSSVC);
  637         }
  638 }
  639 
  640 /*
  641  * Lock a socket against others.
  642  *
  643  * XXXRW: Wait argument is always 1 in the caller.  Replace with a real
  644  * sleep lock?
  645  */
  646 int
  647 nfs_slplock(struct nfssvc_sock *slp, int wait)
  648 {
  649         int *statep = &slp->ns_solock;
  650 
  651         NFSD_LOCK_ASSERT();
  652 
  653         if (!wait && (*statep & NFSRV_SNDLOCK))
  654                 return(0);      /* already locked, fail */
  655         while (*statep & NFSRV_SNDLOCK) {
  656                 *statep |= NFSRV_WANTSND;
  657                 (void) msleep(statep, &nfsd_mtx, PZERO - 1, "nfsslplck", 0);
  658         }
  659         *statep |= NFSRV_SNDLOCK;
  660         return (1);
  661 }
  662 
  663 /*
  664  * Unlock the stream socket for others.
  665  */
  666 void
  667 nfs_slpunlock(struct nfssvc_sock *slp)
  668 {
  669         int *statep = &slp->ns_solock;
  670 
  671         NFSD_LOCK_ASSERT();
  672 
  673         if ((*statep & NFSRV_SNDLOCK) == 0)
  674                 panic("nfs slpunlock");
  675         *statep &= ~NFSRV_SNDLOCK;
  676         if (*statep & NFSRV_WANTSND) {
  677                 *statep &= ~NFSRV_WANTSND;
  678                 wakeup(statep);
  679         }
  680 }
  681 
  682 /*
  683  * Initialize the data structures for the server.
  684  * Handshake with any new nfsds starting up to avoid any chance of
  685  * corruption.
  686  */
  687 void
  688 nfsrv_init(int terminating)
  689 {
  690         struct nfssvc_sock *slp, *nslp;
  691 
  692         NFSD_LOCK_ASSERT();
  693 
  694         if (nfssvc_sockhead_flag & SLP_INIT)
  695                 panic("nfsd init");
  696         nfssvc_sockhead_flag |= SLP_INIT;
  697         if (terminating) {
  698                 TAILQ_FOREACH_SAFE(slp, &nfssvc_sockhead, ns_chain, nslp) {
  699                         if (slp->ns_flag & SLP_VALID)
  700                                 nfsrv_zapsock(slp);
  701                         TAILQ_REMOVE(&nfssvc_sockhead, slp, ns_chain);
  702                         free((caddr_t)slp, M_NFSSVC);
  703                 }
  704                 nfsrv_cleancache();     /* And clear out server cache */
  705         } else
  706                 nfs_pub.np_valid = 0;
  707 
  708         TAILQ_INIT(&nfssvc_sockhead);
  709         nfssvc_sockhead_flag &= ~SLP_INIT;
  710         if (nfssvc_sockhead_flag & SLP_WANTINIT) {
  711                 nfssvc_sockhead_flag &= ~SLP_WANTINIT;
  712                 wakeup(&nfssvc_sockhead);
  713         }
  714 
  715         TAILQ_INIT(&nfsd_head);
  716         nfsd_head_flag &= ~NFSD_CHECKSLP;
  717 
  718 #if 0
  719         nfs_udpsock = (struct nfssvc_sock *)
  720             malloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK | M_ZERO);
  721         STAILQ_INIT(&nfs_udpsock->ns_rec);
  722         TAILQ_INSERT_HEAD(&nfssvc_sockhead, nfs_udpsock, ns_chain);
  723 
  724         nfs_cltpsock = (struct nfssvc_sock *)
  725             malloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK | M_ZERO);
  726         STAILQ_INIT(&nfs_cltpsock->ns_rec);
  727         TAILQ_INSERT_TAIL(&nfssvc_sockhead, nfs_cltpsock, ns_chain);
  728 #endif
  729 }

Cache object: 99f1079e1551c65c65fa0dedb887cd63


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