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

Cache object: c4788343f2fb2f8f5dfa82b7d60f3793


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