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

Cache object: e549de16a52a166edb5191a06bc5ead9


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