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

Cache object: 6257e6a77bc2fb9a26b05da657b6354f


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