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/nfs/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 /*      $NetBSD: nfs_syscalls.c,v 1.99.2.1 2007/06/05 20:37:21 bouyer Exp $     */
    2 
    3 /*
    4  * Copyright (c) 1989, 1993
    5  *      The Regents of the University of California.  All rights reserved.
    6  *
    7  * This code is derived from software contributed to Berkeley by
    8  * Rick Macklem at The University of Guelph.
    9  *
   10  * Redistribution and use in source and binary forms, with or without
   11  * modification, are permitted provided that the following conditions
   12  * are met:
   13  * 1. Redistributions of source code must retain the above copyright
   14  *    notice, this list of conditions and the following disclaimer.
   15  * 2. Redistributions in binary form must reproduce the above copyright
   16  *    notice, this list of conditions and the following disclaimer in the
   17  *    documentation and/or other materials provided with the distribution.
   18  * 3. Neither the name of the University nor the names of its contributors
   19  *    may be used to endorse or promote products derived from this software
   20  *    without specific prior written permission.
   21  *
   22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   32  * SUCH DAMAGE.
   33  *
   34  *      @(#)nfs_syscalls.c      8.5 (Berkeley) 3/30/95
   35  */
   36 
   37 #include <sys/cdefs.h>
   38 __KERNEL_RCSID(0, "$NetBSD: nfs_syscalls.c,v 1.99.2.1 2007/06/05 20:37:21 bouyer Exp $");
   39 
   40 #include "fs_nfs.h"
   41 #include "opt_nfs.h"
   42 #include "opt_nfsserver.h"
   43 #include "opt_iso.h"
   44 #include "opt_inet.h"
   45 #include "opt_compat_netbsd.h"
   46 
   47 #include <sys/param.h>
   48 #include <sys/systm.h>
   49 #include <sys/kernel.h>
   50 #include <sys/file.h>
   51 #include <sys/stat.h>
   52 #include <sys/vnode.h>
   53 #include <sys/mount.h>
   54 #include <sys/proc.h>
   55 #include <sys/uio.h>
   56 #include <sys/malloc.h>
   57 #include <sys/buf.h>
   58 #include <sys/mbuf.h>
   59 #include <sys/socket.h>
   60 #include <sys/socketvar.h>
   61 #include <sys/signalvar.h>
   62 #include <sys/domain.h>
   63 #include <sys/protosw.h>
   64 #include <sys/namei.h>
   65 #include <sys/syslog.h>
   66 #include <sys/filedesc.h>
   67 #include <sys/kthread.h>
   68 #include <sys/kauth.h>
   69 
   70 #include <sys/sa.h>
   71 #include <sys/syscallargs.h>
   72 
   73 #include <netinet/in.h>
   74 #include <netinet/tcp.h>
   75 #ifdef ISO
   76 #include <netiso/iso.h>
   77 #endif
   78 #include <nfs/xdr_subs.h>
   79 #include <nfs/rpcv2.h>
   80 #include <nfs/nfsproto.h>
   81 #include <nfs/nfs.h>
   82 #include <nfs/nfsm_subs.h>
   83 #include <nfs/nfsrvcache.h>
   84 #include <nfs/nfsmount.h>
   85 #include <nfs/nfsnode.h>
   86 #include <nfs/nqnfs.h>
   87 #include <nfs/nfsrtt.h>
   88 #include <nfs/nfs_var.h>
   89 
   90 /* Global defs. */
   91 extern int32_t (*nfsrv3_procs[NFS_NPROCS]) __P((struct nfsrv_descript *,
   92                                                 struct nfssvc_sock *,
   93                                                 struct lwp *, struct mbuf **));
   94 extern time_t nqnfsstarttime;
   95 extern int nfsrvw_procrastinate;
   96 
   97 struct nfssvc_sock *nfs_udpsock;
   98 #ifdef ISO
   99 struct nfssvc_sock *nfs_cltpsock;
  100 #endif
  101 #ifdef INET6
  102 struct nfssvc_sock *nfs_udp6sock;
  103 #endif
  104 int nuidhash_max = NFS_MAXUIDHASH;
  105 int nfsd_waiting = 0;
  106 #ifdef NFSSERVER
  107 static int nfs_numnfsd = 0;
  108 static int notstarted = 1;
  109 static int modify_flag = 0;
  110 static struct nfsdrt nfsdrt;
  111 #endif
  112 
  113 #ifdef NFSSERVER
  114 struct simplelock nfsd_slock = SIMPLELOCK_INITIALIZER;
  115 struct nfssvc_sockhead nfssvc_sockhead;
  116 struct nfssvc_sockhead nfssvc_sockpending;
  117 struct nfsdhead nfsd_head;
  118 struct nfsdidlehead nfsd_idle_head;
  119 
  120 int nfssvc_sockhead_flag;
  121 int nfsd_head_flag;
  122 #endif
  123 
  124 MALLOC_DEFINE(M_NFSUID, "NFS uid", "Nfs uid mapping structure");
  125 
  126 #ifdef NFS
  127 struct nfs_iod nfs_asyncdaemon[NFS_MAXASYNCDAEMON];
  128 int nfs_niothreads = -1; /* == "0, and has never been set" */
  129 #endif
  130 
  131 #ifdef NFSSERVER
  132 static struct nfssvc_sock *nfsrv_sockalloc __P((void));
  133 static void nfsrv_sockfree __P((struct nfssvc_sock *));
  134 static void nfsd_rt __P((int, struct nfsrv_descript *, int));
  135 #endif
  136 
  137 /*
  138  * NFS server system calls
  139  */
  140 
  141 
  142 /*
  143  * Nfs server pseudo system call for the nfsd's
  144  * Based on the flag value it either:
  145  * - adds a socket to the selection list
  146  * - remains in the kernel as an nfsd
  147  * - remains in the kernel as an nfsiod
  148  */
  149 int
  150 sys_nfssvc(struct lwp *l, void *v, register_t *retval)
  151 {
  152         struct sys_nfssvc_args /* {
  153                 syscallarg(int) flag;
  154                 syscallarg(caddr_t) argp;
  155         } */ *uap = v;
  156         int error;
  157 #ifdef NFS
  158         struct nameidata nd;
  159         struct nfsmount *nmp;
  160         struct nfsd_cargs ncd;
  161 #endif
  162 #ifdef NFSSERVER
  163         int s;
  164         struct file *fp;
  165         struct mbuf *nam;
  166         struct nfsd_args nfsdarg;
  167         struct nfsd_srvargs nfsd_srvargs, *nsd = &nfsd_srvargs;
  168         struct nfsd *nfsd;
  169         struct nfssvc_sock *slp;
  170         struct nfsuid *nuidp;
  171 #endif
  172 
  173         /*
  174          * Must be super user
  175          */
  176         error = kauth_authorize_generic(l->l_cred, KAUTH_GENERIC_ISSUSER,
  177             &l->l_acflag);
  178         if (error)
  179                 return (error);
  180 
  181         /* Initialize NFS server / client shared data. */
  182         nfs_init();
  183 
  184 #ifdef NFSSERVER
  185         s = splsoftnet();
  186         simple_lock(&nfsd_slock);
  187         while (nfssvc_sockhead_flag & SLP_INIT) {
  188                 nfssvc_sockhead_flag |= SLP_WANTINIT;
  189                 (void) ltsleep(&nfssvc_sockhead, PSOCK, "nfsd init", 0,
  190                     &nfsd_slock);
  191         }
  192         simple_unlock(&nfsd_slock);
  193         splx(s);
  194 #endif
  195         if (SCARG(uap, flag) & NFSSVC_BIOD) {
  196 #if defined(NFS) && defined(COMPAT_14)
  197                 error = nfssvc_iod(l);
  198 #else
  199                 error = ENOSYS;
  200 #endif
  201         } else if (SCARG(uap, flag) & NFSSVC_MNTD) {
  202 #ifndef NFS
  203                 error = ENOSYS;
  204 #else
  205                 error = copyin(SCARG(uap, argp), (caddr_t)&ncd, sizeof (ncd));
  206                 if (error)
  207                         return (error);
  208                 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
  209                         ncd.ncd_dirp, l);
  210                 error = namei(&nd);
  211                 if (error)
  212                         return (error);
  213                 if ((nd.ni_vp->v_flag & VROOT) == 0)
  214                         error = EINVAL;
  215                 nmp = VFSTONFS(nd.ni_vp->v_mount);
  216                 vput(nd.ni_vp);
  217                 if (error)
  218                         return (error);
  219                 if ((nmp->nm_iflag & NFSMNT_MNTD) &&
  220                         (SCARG(uap, flag) & NFSSVC_GOTAUTH) == 0)
  221                         return (0);
  222                 nmp->nm_iflag |= NFSMNT_MNTD;
  223                 error = nqnfs_clientd(nmp, l->l_cred, &ncd, SCARG(uap, flag),
  224                         SCARG(uap, argp), l);
  225 #endif /* NFS */
  226         } else if (SCARG(uap, flag) & NFSSVC_ADDSOCK) {
  227 #ifndef NFSSERVER
  228                 error = ENOSYS;
  229 #else
  230                 error = copyin(SCARG(uap, argp), (caddr_t)&nfsdarg,
  231                     sizeof(nfsdarg));
  232                 if (error)
  233                         return (error);
  234                 /* getsock() will use the descriptor for us */
  235                 error = getsock(l->l_proc->p_fd, nfsdarg.sock, &fp);
  236                 if (error)
  237                         return (error);
  238                 /*
  239                  * Get the client address for connected sockets.
  240                  */
  241                 if (nfsdarg.name == NULL || nfsdarg.namelen == 0)
  242                         nam = (struct mbuf *)0;
  243                 else {
  244                         error = sockargs(&nam, nfsdarg.name, nfsdarg.namelen,
  245                                 MT_SONAME);
  246                         if (error) {
  247                                 FILE_UNUSE(fp, NULL);
  248                                 return (error);
  249                         }
  250                 }
  251                 error = nfssvc_addsock(fp, nam);
  252                 FILE_UNUSE(fp, NULL);
  253 #endif /* !NFSSERVER */
  254         } else if (SCARG(uap, flag) & NFSSVC_SETEXPORTSLIST) {
  255 #ifndef NFSSERVER
  256                 error = ENOSYS;
  257 #else
  258                 struct export_args *args;
  259                 struct mountd_exports_list mel;
  260 
  261                 error = copyin(SCARG(uap, argp), &mel, sizeof(mel));
  262                 if (error != 0)
  263                         return error;
  264 
  265                 args = (struct export_args *)malloc(mel.mel_nexports *
  266                     sizeof(struct export_args), M_TEMP, M_WAITOK);
  267                 error = copyin(mel.mel_exports, args, mel.mel_nexports *
  268                     sizeof(struct export_args));
  269                 if (error != 0) {
  270                         free(args, M_TEMP);
  271                         return error;
  272                 }
  273                 mel.mel_exports = args;
  274 
  275                 error = mountd_set_exports_list(&mel, l);
  276 
  277                 free(args, M_TEMP);
  278 #endif /* !NFSSERVER */
  279         } else {
  280 #ifndef NFSSERVER
  281                 error = ENOSYS;
  282 #else
  283                 error = copyin(SCARG(uap, argp), (caddr_t)nsd, sizeof (*nsd));
  284                 if (error)
  285                         return (error);
  286                 if ((SCARG(uap, flag) & NFSSVC_AUTHIN) &&
  287                     ((nfsd = nsd->nsd_nfsd)) != NULL &&
  288                     (nfsd->nfsd_slp->ns_flag & SLP_VALID)) {
  289                         slp = nfsd->nfsd_slp;
  290 
  291                         /*
  292                          * First check to see if another nfsd has already
  293                          * added this credential.
  294                          */
  295                         LIST_FOREACH(nuidp, NUIDHASH(slp, nsd->nsd_cr.cr_uid),
  296                             nu_hash) {
  297                                 if (kauth_cred_geteuid(nuidp->nu_cr) ==
  298                                     nsd->nsd_cr.cr_uid &&
  299                                     (!nfsd->nfsd_nd->nd_nam2 ||
  300                                      netaddr_match(NU_NETFAM(nuidp),
  301                                      &nuidp->nu_haddr, nfsd->nfsd_nd->nd_nam2)))
  302                                         break;
  303                         }
  304                         if (nuidp) {
  305                             kauth_cred_hold(nuidp->nu_cr);
  306                             nfsd->nfsd_nd->nd_cr = nuidp->nu_cr;
  307                             nfsd->nfsd_nd->nd_flag |= ND_KERBFULL;
  308                         } else {
  309                             /*
  310                              * Nope, so we will.
  311                              */
  312                             if (slp->ns_numuids < nuidhash_max) {
  313                                 slp->ns_numuids++;
  314                                 nuidp = (struct nfsuid *)
  315                                    malloc(sizeof (struct nfsuid), M_NFSUID,
  316                                         M_WAITOK);
  317                             } else
  318                                 nuidp = (struct nfsuid *)0;
  319                             if ((slp->ns_flag & SLP_VALID) == 0) {
  320                                 if (nuidp)
  321                                     free((caddr_t)nuidp, M_NFSUID);
  322                             } else {
  323                                 if (nuidp == (struct nfsuid *)0) {
  324                                     nuidp = TAILQ_FIRST(&slp->ns_uidlruhead);
  325                                     LIST_REMOVE(nuidp, nu_hash);
  326                                     TAILQ_REMOVE(&slp->ns_uidlruhead, nuidp,
  327                                         nu_lru);
  328                                     if (nuidp->nu_flag & NU_NAM)
  329                                         m_freem(nuidp->nu_nam);
  330                                 }
  331                                 nuidp->nu_flag = 0;
  332                                 kauth_uucred_to_cred(nuidp->nu_cr,
  333                                     &nsd->nsd_cr);
  334                                 nuidp->nu_timestamp = nsd->nsd_timestamp;
  335                                 nuidp->nu_expire = time_second + nsd->nsd_ttl;
  336                                 /*
  337                                  * and save the session key in nu_key.
  338                                  */
  339                                 memcpy(nuidp->nu_key, nsd->nsd_key,
  340                                     sizeof(nsd->nsd_key));
  341                                 if (nfsd->nfsd_nd->nd_nam2) {
  342                                     struct sockaddr_in *saddr;
  343 
  344                                     saddr = mtod(nfsd->nfsd_nd->nd_nam2,
  345                                          struct sockaddr_in *);
  346                                     switch (saddr->sin_family) {
  347                                     case AF_INET:
  348                                         nuidp->nu_flag |= NU_INETADDR;
  349                                         nuidp->nu_inetaddr =
  350                                              saddr->sin_addr.s_addr;
  351                                         break;
  352                                     case AF_ISO:
  353                                     default:
  354                                         nuidp->nu_flag |= NU_NAM;
  355                                         nuidp->nu_nam = m_copym(
  356                                             nfsd->nfsd_nd->nd_nam2, 0,
  357                                              M_COPYALL, M_WAIT);
  358                                         break;
  359                                     };
  360                                 }
  361                                 TAILQ_INSERT_TAIL(&slp->ns_uidlruhead, nuidp,
  362                                         nu_lru);
  363                                 LIST_INSERT_HEAD(NUIDHASH(slp, nsd->nsd_uid),
  364                                         nuidp, nu_hash);
  365                                 kauth_cred_hold(nuidp->nu_cr);
  366                                 nfsd->nfsd_nd->nd_cr = nuidp->nu_cr;
  367                                 nfsd->nfsd_nd->nd_flag |= ND_KERBFULL;
  368                             }
  369                         }
  370                 }
  371                 if ((SCARG(uap, flag) & NFSSVC_AUTHINFAIL) &&
  372                     (nfsd = nsd->nsd_nfsd))
  373                         nfsd->nfsd_flag |= NFSD_AUTHFAIL;
  374                 error = nfssvc_nfsd(nsd, SCARG(uap, argp), l);
  375 #endif /* !NFSSERVER */
  376         }
  377         if (error == EINTR || error == ERESTART)
  378                 error = 0;
  379         return (error);
  380 }
  381 
  382 #ifdef NFSSERVER
  383 MALLOC_DEFINE(M_NFSD, "NFS daemon", "Nfs server daemon structure");
  384 MALLOC_DEFINE(M_NFSSVC, "NFS srvsock", "Nfs server structure");
  385 
  386 static struct nfssvc_sock *
  387 nfsrv_sockalloc()
  388 {
  389         struct nfssvc_sock *slp;
  390         int s;
  391 
  392         slp = (struct nfssvc_sock *)
  393             malloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK);
  394         memset(slp, 0, sizeof (struct nfssvc_sock));
  395         simple_lock_init(&slp->ns_lock);
  396         TAILQ_INIT(&slp->ns_uidlruhead);
  397         LIST_INIT(&slp->ns_tq);
  398         SIMPLEQ_INIT(&slp->ns_sendq);
  399         s = splsoftnet();
  400         simple_lock(&nfsd_slock);
  401         TAILQ_INSERT_TAIL(&nfssvc_sockhead, slp, ns_chain);
  402         simple_unlock(&nfsd_slock);
  403         splx(s);
  404 
  405         return slp;
  406 }
  407 
  408 static void
  409 nfsrv_sockfree(struct nfssvc_sock *slp)
  410 {
  411 
  412         KASSERT(slp->ns_so == NULL);
  413         KASSERT(slp->ns_fp == NULL);
  414         KASSERT((slp->ns_flag & SLP_VALID) == 0);
  415         free(slp, M_NFSSVC);
  416 }
  417 
  418 /*
  419  * Adds a socket to the list for servicing by nfsds.
  420  */
  421 int
  422 nfssvc_addsock(fp, mynam)
  423         struct file *fp;
  424         struct mbuf *mynam;
  425 {
  426         struct mbuf *m;
  427         int siz;
  428         struct nfssvc_sock *slp;
  429         struct socket *so;
  430         struct nfssvc_sock *tslp;
  431         int error, s;
  432 
  433         so = (struct socket *)fp->f_data;
  434         tslp = (struct nfssvc_sock *)0;
  435         /*
  436          * Add it to the list, as required.
  437          */
  438         if (so->so_proto->pr_protocol == IPPROTO_UDP) {
  439 #ifdef INET6
  440                 if (so->so_proto->pr_domain->dom_family == AF_INET6)
  441                         tslp = nfs_udp6sock;
  442                 else
  443 #endif
  444                 tslp = nfs_udpsock;
  445                 if (tslp->ns_flag & SLP_VALID) {
  446                         m_freem(mynam);
  447                         return (EPERM);
  448                 }
  449 #ifdef ISO
  450         } else if (so->so_proto->pr_protocol == ISOPROTO_CLTP) {
  451                 tslp = nfs_cltpsock;
  452                 if (tslp->ns_flag & SLP_VALID) {
  453                         m_freem(mynam);
  454                         return (EPERM);
  455                 }
  456 #endif /* ISO */
  457         }
  458         if (so->so_type == SOCK_STREAM)
  459                 siz = NFS_MAXPACKET + sizeof (u_long);
  460         else
  461                 siz = NFS_MAXPACKET;
  462         error = soreserve(so, siz, siz);
  463         if (error) {
  464                 m_freem(mynam);
  465                 return (error);
  466         }
  467 
  468         /*
  469          * Set protocol specific options { for now TCP only } and
  470          * reserve some space. For datagram sockets, this can get called
  471          * repeatedly for the same socket, but that isn't harmful.
  472          */
  473         if (so->so_type == SOCK_STREAM) {
  474                 m = m_get(M_WAIT, MT_SOOPTS);
  475                 MCLAIM(m, &nfs_mowner);
  476                 *mtod(m, int32_t *) = 1;
  477                 m->m_len = sizeof(int32_t);
  478                 sosetopt(so, SOL_SOCKET, SO_KEEPALIVE, m);
  479         }
  480         if ((so->so_proto->pr_domain->dom_family == AF_INET
  481 #ifdef INET6
  482             || so->so_proto->pr_domain->dom_family == AF_INET6
  483 #endif
  484             ) &&
  485             so->so_proto->pr_protocol == IPPROTO_TCP) {
  486                 m = m_get(M_WAIT, MT_SOOPTS);
  487                 MCLAIM(m, &nfs_mowner);
  488                 *mtod(m, int32_t *) = 1;
  489                 m->m_len = sizeof(int32_t);
  490                 sosetopt(so, IPPROTO_TCP, TCP_NODELAY, m);
  491         }
  492         so->so_rcv.sb_flags &= ~SB_NOINTR;
  493         so->so_rcv.sb_timeo = 0;
  494         so->so_snd.sb_flags &= ~SB_NOINTR;
  495         so->so_snd.sb_timeo = 0;
  496         if (tslp)
  497                 slp = tslp;
  498         else {
  499                 slp = nfsrv_sockalloc();
  500         }
  501         slp->ns_so = so;
  502         slp->ns_nam = mynam;
  503         fp->f_count++;
  504         slp->ns_fp = fp;
  505         s = splsoftnet();
  506         so->so_upcallarg = (caddr_t)slp;
  507         so->so_upcall = nfsrv_rcv;
  508         so->so_rcv.sb_flags |= SB_UPCALL;
  509         slp->ns_flag = (SLP_VALID | SLP_NEEDQ);
  510         nfsrv_wakenfsd(slp);
  511         splx(s);
  512         return (0);
  513 }
  514 
  515 /*
  516  * Called by nfssvc() for nfsds. Just loops around servicing rpc requests
  517  * until it is killed by a signal.
  518  */
  519 int
  520 nfssvc_nfsd(nsd, argp, l)
  521         struct nfsd_srvargs *nsd;
  522         caddr_t argp;
  523         struct lwp *l;
  524 {
  525         struct timeval tv;
  526         struct mbuf *m;
  527         struct nfssvc_sock *slp;
  528         struct nfsd *nfsd = nsd->nsd_nfsd;
  529         struct nfsrv_descript *nd = NULL;
  530         struct mbuf *mreq;
  531         u_quad_t cur_usec;
  532         int error = 0, cacherep, s, siz, sotype, writes_todo;
  533         struct proc *p = l->l_proc;
  534 
  535 #ifndef nolint
  536         cacherep = RC_DOIT;
  537         writes_todo = 0;
  538 #endif
  539         s = splsoftnet();
  540         if (nfsd == (struct nfsd *)0) {
  541                 nsd->nsd_nfsd = nfsd = (struct nfsd *)
  542                         malloc(sizeof (struct nfsd), M_NFSD, M_WAITOK);
  543                 memset((caddr_t)nfsd, 0, sizeof (struct nfsd));
  544                 nfsd->nfsd_procp = p;
  545                 simple_lock(&nfsd_slock);
  546                 TAILQ_INSERT_TAIL(&nfsd_head, nfsd, nfsd_chain);
  547                 nfs_numnfsd++;
  548                 simple_unlock(&nfsd_slock);
  549         }
  550         PHOLD(l);
  551         /*
  552          * Loop getting rpc requests until SIGKILL.
  553          */
  554         for (;;) {
  555                 if (nfsd->nfsd_slp == NULL) {
  556                         simple_lock(&nfsd_slock);
  557                         while (nfsd->nfsd_slp == NULL &&
  558                             (nfsd_head_flag & NFSD_CHECKSLP) == 0) {
  559                                 SLIST_INSERT_HEAD(&nfsd_idle_head, nfsd,
  560                                     nfsd_idle);
  561                                 nfsd_waiting++;
  562                                 error = ltsleep(nfsd, PSOCK | PCATCH, "nfsd",
  563                                     0, &nfsd_slock);
  564                                 nfsd_waiting--;
  565                                 if (error) {
  566                                         slp = nfsd->nfsd_slp;
  567                                         nfsd->nfsd_slp = NULL;
  568                                         if (!slp)
  569                                                 SLIST_REMOVE(&nfsd_idle_head,
  570                                                     nfsd, nfsd, nfsd_idle);
  571                                         simple_unlock(&nfsd_slock);
  572                                         if (slp) {
  573                                                 nfsrv_wakenfsd(slp);
  574                                                 nfsrv_slpderef(slp);
  575                                         }
  576                                         goto done;
  577                                 }
  578                         }
  579                         if (nfsd->nfsd_slp == NULL &&
  580                             (nfsd_head_flag & NFSD_CHECKSLP) != 0) {
  581                                 slp = TAILQ_FIRST(&nfssvc_sockpending);
  582                                 if (slp) {
  583                                         KASSERT((slp->ns_flag &
  584                                             (SLP_VALID | SLP_DOREC))
  585                                             == (SLP_VALID | SLP_DOREC));
  586                                         TAILQ_REMOVE(&nfssvc_sockpending, slp,
  587                                             ns_pending);
  588                                         slp->ns_flag &= ~SLP_DOREC;
  589                                         slp->ns_sref++;
  590                                         nfsd->nfsd_slp = slp;
  591                                 } else
  592                                         nfsd_head_flag &= ~NFSD_CHECKSLP;
  593                         }
  594                         simple_unlock(&nfsd_slock);
  595                         if ((slp = nfsd->nfsd_slp) == NULL)
  596                                 continue;
  597                         if (slp->ns_flag & SLP_VALID) {
  598                                 if (slp->ns_flag & SLP_DISCONN)
  599                                         nfsrv_zapsock(slp);
  600                                 else if ((slp->ns_flag & SLP_NEEDQ) != 0) {
  601                                         nfsrv_rcv(slp->ns_so, (void *)slp,
  602                                             M_WAIT);
  603                                 }
  604                                 error = nfsrv_dorec(slp, nfsd, &nd);
  605                                 getmicrotime(&tv);
  606                                 cur_usec = (u_quad_t)tv.tv_sec * 1000000 +
  607                                         (u_quad_t)tv.tv_usec;
  608                                 if (error && LIST_FIRST(&slp->ns_tq) &&
  609                                     LIST_FIRST(&slp->ns_tq)->nd_time <=
  610                                     cur_usec) {
  611                                         error = 0;
  612                                         cacherep = RC_DOIT;
  613                                         writes_todo = 1;
  614                                 } else
  615                                         writes_todo = 0;
  616                                 if (error == 0 && slp->ns_rec != NULL) {
  617                                         nfsrv_wakenfsd(slp);
  618                                 }
  619                         }
  620                 } else {
  621                         error = 0;
  622                         slp = nfsd->nfsd_slp;
  623                 }
  624                 if (error || (slp->ns_flag & SLP_VALID) == 0) {
  625                         if (nd) {
  626                                 nfsdreq_free(nd);
  627                                 nd = NULL;
  628                         }
  629                         nfsd->nfsd_slp = NULL;
  630                         nfsrv_slpderef(slp);
  631                         continue;
  632                 }
  633                 splx(s);
  634                 sotype = slp->ns_so->so_type;
  635                 if (nd) {
  636                         getmicrotime(&nd->nd_starttime);
  637                         if (nd->nd_nam2)
  638                                 nd->nd_nam = nd->nd_nam2;
  639                         else
  640                                 nd->nd_nam = slp->ns_nam;
  641 
  642                         /*
  643                          * Check to see if authorization is needed.
  644                          */
  645                         if (nfsd->nfsd_flag & NFSD_NEEDAUTH) {
  646                                 nfsd->nfsd_flag &= ~NFSD_NEEDAUTH;
  647                                 nsd->nsd_haddr = mtod(nd->nd_nam,
  648                                     struct sockaddr_in *)->sin_addr.s_addr;
  649                                 nsd->nsd_authlen = nfsd->nfsd_authlen;
  650                                 nsd->nsd_verflen = nfsd->nfsd_verflen;
  651                                 if (!copyout(nfsd->nfsd_authstr,
  652                                     nsd->nsd_authstr, nfsd->nfsd_authlen) &&
  653                                     !copyout(nfsd->nfsd_verfstr,
  654                                     nsd->nsd_verfstr, nfsd->nfsd_verflen) &&
  655                                     !copyout(nsd, argp, sizeof (*nsd))) {
  656                                         PRELE(l);
  657                                         return (ENEEDAUTH);
  658                                 }
  659                                 cacherep = RC_DROPIT;
  660                         } else
  661                                 cacherep = nfsrv_getcache(nd, slp, &mreq);
  662 
  663                         /*
  664                          * Check for just starting up for NQNFS and send
  665                          * fake "try again later" replies to the NQNFS clients.
  666                          */
  667                         if (notstarted && nqnfsstarttime <= time_second) {
  668                                 if (modify_flag) {
  669                                         nqnfsstarttime =
  670                                             time_second + nqsrv_writeslack;
  671                                         modify_flag = 0;
  672                                 } else
  673                                         notstarted = 0;
  674                         }
  675                         if (notstarted) {
  676                                 if ((nd->nd_flag & ND_NQNFS) == 0)
  677                                         cacherep = RC_DROPIT;
  678                                 else if (nd->nd_procnum != NFSPROC_WRITE) {
  679                                         nd->nd_procnum = NFSPROC_NOOP;
  680                                         nd->nd_repstat = NQNFS_TRYLATER;
  681                                         cacherep = RC_DOIT;
  682                                 } else
  683                                         modify_flag = 1;
  684                         } else if (nfsd->nfsd_flag & NFSD_AUTHFAIL) {
  685                                 nfsd->nfsd_flag &= ~NFSD_AUTHFAIL;
  686                                 nd->nd_procnum = NFSPROC_NOOP;
  687                                 nd->nd_repstat =
  688                                     (NFSERR_AUTHERR | AUTH_TOOWEAK);
  689                                 cacherep = RC_DOIT;
  690                         }
  691                 }
  692 
  693                 /*
  694                  * Loop to get all the write rpc relies that have been
  695                  * gathered together.
  696                  */
  697                 do {
  698 #ifdef DIAGNOSTIC
  699                         int lockcount;
  700 #endif
  701                         switch (cacherep) {
  702                         case RC_DOIT:
  703 #ifdef DIAGNOSTIC
  704                                 /*
  705                                  * NFS server procs should neither release
  706                                  * locks already held, nor leave things
  707                                  * locked.  Catch this sooner, rather than
  708                                  * later (when we try to relock something we
  709                                  * already have locked).  Careful inspection
  710                                  * of the failing routine usually turns up the
  711                                  * lock leak.. once we know what it is..
  712                                  */
  713                                 lockcount = l->l_locks;
  714 #endif
  715                                 mreq = NULL;
  716                                 netexport_rdlock();
  717                                 if (writes_todo || nd == NULL ||
  718                                      (!(nd->nd_flag & ND_NFSV3) &&
  719                                      nd->nd_procnum == NFSPROC_WRITE &&
  720                                      nfsrvw_procrastinate > 0 && !notstarted))
  721                                         error = nfsrv_writegather(&nd, slp,
  722                                             l, &mreq);
  723                                 else
  724                                         error =
  725                                             (*(nfsrv3_procs[nd->nd_procnum]))
  726                                             (nd, slp, l, &mreq);
  727                                 netexport_rdunlock();
  728 #ifdef DIAGNOSTIC
  729                                 if (l->l_locks != lockcount) {
  730                                         /*
  731                                          * If you see this panic, audit
  732                                          * nfsrv3_procs[nd->nd_procnum] for
  733                                          * vnode locking errors (usually, it's
  734                                          * due to forgetting to vput()
  735                                          * something).
  736                                          */
  737 #ifdef DEBUG
  738                                         extern void printlockedvnodes(void);
  739                                         printlockedvnodes();
  740 #endif
  741                                         printf("nfsd: locking botch in op %d"
  742                                             " (before %d, after %d)\n",
  743                                             nd ? nd->nd_procnum : -1,
  744                                             lockcount, l->l_locks);
  745                                 }
  746 #endif
  747                                 if (mreq == NULL) {
  748                                         if (nd != NULL) {
  749                                                 if (nd->nd_nam2)
  750                                                         m_free(nd->nd_nam2);
  751                                                 if (nd->nd_mrep)
  752                                                         m_freem(nd->nd_mrep);
  753                                         }
  754                                         break;
  755                                 }
  756                                 if (error) {
  757                                         if (nd->nd_procnum != NQNFSPROC_VACATED)
  758                                                 nfsstats.srv_errs++;
  759                                         nfsrv_updatecache(nd, FALSE, mreq);
  760                                         if (nd->nd_nam2)
  761                                                 m_freem(nd->nd_nam2);
  762                                         break;
  763                                 }
  764                                 nfsstats.srvrpccnt[nd->nd_procnum]++;
  765                                 nfsrv_updatecache(nd, TRUE, mreq);
  766                                 nd->nd_mrep = (struct mbuf *)0;
  767                         case RC_REPLY:
  768                                 m = mreq;
  769                                 siz = 0;
  770                                 while (m) {
  771                                         siz += m->m_len;
  772                                         m = m->m_next;
  773                                 }
  774                                 if (siz <= 0 || siz > NFS_MAXPACKET) {
  775                                         printf("mbuf siz=%d\n",siz);
  776                                         panic("Bad nfs svc reply");
  777                                 }
  778                                 m = mreq;
  779                                 m->m_pkthdr.len = siz;
  780                                 m->m_pkthdr.rcvif = (struct ifnet *)0;
  781                                 /*
  782                                  * For stream protocols, prepend a Sun RPC
  783                                  * Record Mark.
  784                                  */
  785                                 if (sotype == SOCK_STREAM) {
  786                                         M_PREPEND(m, NFSX_UNSIGNED, M_WAIT);
  787                                         *mtod(m, u_int32_t *) =
  788                                             htonl(0x80000000 | siz);
  789                                 }
  790                                 nd->nd_mreq = m;
  791                                 if (nfsrtton) {
  792                                         nfsd_rt(slp->ns_so->so_type, nd,
  793                                             cacherep);
  794                                 }
  795                                 s = splsoftnet();
  796                                 error = nfsdsock_sendreply(slp, nd);
  797                                 nd = NULL;
  798                                 if (error == EPIPE)
  799                                         nfsrv_zapsock(slp);
  800                                 if (error == EINTR || error == ERESTART) {
  801                                         nfsrv_slpderef(slp);
  802                                         goto done;
  803                                 }
  804                                 splx(s);
  805                                 break;
  806                         case RC_DROPIT:
  807                                 if (nfsrtton)
  808                                         nfsd_rt(sotype, nd, cacherep);
  809                                 m_freem(nd->nd_mrep);
  810                                 m_freem(nd->nd_nam2);
  811                                 break;
  812                         }
  813                         if (nd) {
  814                                 nfsdreq_free(nd);
  815                                 nd = NULL;
  816                         }
  817 
  818                         /*
  819                          * Check to see if there are outstanding writes that
  820                          * need to be serviced.
  821                          */
  822                         getmicrotime(&tv);
  823                         cur_usec = (u_quad_t)tv.tv_sec * 1000000 +
  824                             (u_quad_t)tv.tv_usec;
  825                         s = splsoftclock();
  826                         if (LIST_FIRST(&slp->ns_tq) &&
  827                             LIST_FIRST(&slp->ns_tq)->nd_time <= cur_usec) {
  828                                 cacherep = RC_DOIT;
  829                                 writes_todo = 1;
  830                         } else
  831                                 writes_todo = 0;
  832                         splx(s);
  833                 } while (writes_todo);
  834                 s = splsoftnet();
  835                 if (nfsrv_dorec(slp, nfsd, &nd)) {
  836                         nfsd->nfsd_slp = NULL;
  837                         nfsrv_slpderef(slp);
  838                 }
  839         }
  840 done:
  841         PRELE(l);
  842         simple_lock(&nfsd_slock);
  843         TAILQ_REMOVE(&nfsd_head, nfsd, nfsd_chain);
  844         simple_unlock(&nfsd_slock);
  845         splx(s);
  846         free((caddr_t)nfsd, M_NFSD);
  847         nsd->nsd_nfsd = (struct nfsd *)0;
  848         if (--nfs_numnfsd == 0)
  849                 nfsrv_init(TRUE);       /* Reinitialize everything */
  850         return (error);
  851 }
  852 
  853 /*
  854  * Shut down a socket associated with an nfssvc_sock structure.
  855  * Should be called with the send lock set, if required.
  856  * The trick here is to increment the sref at the start, so that the nfsds
  857  * will stop using it and clear ns_flag at the end so that it will not be
  858  * reassigned during cleanup.
  859  *
  860  * called at splsoftnet.
  861  */
  862 void
  863 nfsrv_zapsock(slp)
  864         struct nfssvc_sock *slp;
  865 {
  866         struct nfsuid *nuidp, *nnuidp;
  867         struct nfsrv_descript *nwp, *nnwp;
  868         struct socket *so;
  869         struct mbuf *m;
  870         int s;
  871 
  872         if (nfsdsock_drain(slp)) {
  873                 return;
  874         }
  875         simple_lock(&nfsd_slock);
  876         if (slp->ns_flag & SLP_DOREC) {
  877                 TAILQ_REMOVE(&nfssvc_sockpending, slp, ns_pending);
  878         }
  879         simple_unlock(&nfsd_slock);
  880 
  881         so = slp->ns_so;
  882         KASSERT(so != NULL);
  883         so->so_upcall = NULL;
  884         so->so_upcallarg = NULL;
  885         so->so_rcv.sb_flags &= ~SB_UPCALL;
  886         soshutdown(so, SHUT_RDWR);
  887 
  888         if (slp->ns_nam)
  889                 m_free(slp->ns_nam);
  890         m_freem(slp->ns_raw);
  891         m = slp->ns_rec;
  892         while (m != NULL) {
  893                 struct mbuf *n;
  894 
  895                 n = m->m_nextpkt;
  896                 m_freem(m);
  897                 m = n;
  898         }
  899         for (nuidp = TAILQ_FIRST(&slp->ns_uidlruhead); nuidp != 0;
  900             nuidp = nnuidp) {
  901                 nnuidp = TAILQ_NEXT(nuidp, nu_lru);
  902                 LIST_REMOVE(nuidp, nu_hash);
  903                 TAILQ_REMOVE(&slp->ns_uidlruhead, nuidp, nu_lru);
  904                 if (nuidp->nu_flag & NU_NAM)
  905                         m_freem(nuidp->nu_nam);
  906                 free((caddr_t)nuidp, M_NFSUID);
  907         }
  908         s = splsoftclock();
  909         for (nwp = LIST_FIRST(&slp->ns_tq); nwp; nwp = nnwp) {
  910                 nnwp = LIST_NEXT(nwp, nd_tq);
  911                 LIST_REMOVE(nwp, nd_tq);
  912                 nfsdreq_free(nwp);
  913         }
  914         splx(s);
  915 }
  916 
  917 /*
  918  * Derefence a server socket structure. If it has no more references and
  919  * is no longer valid, you can throw it away.
  920  */
  921 void
  922 nfsrv_slpderef(slp)
  923         struct nfssvc_sock *slp;
  924 {
  925         LOCK_ASSERT(!simple_lock_held(&nfsd_slock));
  926 
  927         if (--(slp->ns_sref) == 0 && (slp->ns_flag & SLP_VALID) == 0) {
  928                 struct file *fp;
  929                 int s = splsoftnet();
  930                 simple_lock(&nfsd_slock);
  931                 TAILQ_REMOVE(&nfssvc_sockhead, slp, ns_chain);
  932                 simple_unlock(&nfsd_slock);
  933                 splx(s);
  934 
  935                 fp = slp->ns_fp;
  936                 if (fp != NULL) {
  937                         slp->ns_fp = NULL;
  938                         KASSERT(fp != NULL);
  939                         KASSERT(fp->f_data == slp->ns_so);
  940                         simple_lock(&fp->f_slock);
  941                         FILE_USE(fp);
  942                         closef(fp, (struct lwp *)0);
  943                         slp->ns_so = NULL;
  944                 }
  945 
  946                 nfsrv_sockfree(slp);
  947         }
  948 }
  949 
  950 /*
  951  * Initialize the data structures for the server.
  952  * Handshake with any new nfsds starting up to avoid any chance of
  953  * corruption.
  954  */
  955 void
  956 nfsrv_init(terminating)
  957         int terminating;
  958 {
  959         struct nfssvc_sock *slp;
  960         int s;
  961 
  962         s = splsoftnet();
  963         simple_lock(&nfsd_slock);
  964         if (nfssvc_sockhead_flag & SLP_INIT)
  965                 panic("nfsd init");
  966         nfssvc_sockhead_flag |= SLP_INIT;
  967 
  968         if (terminating) {
  969                 while ((slp = TAILQ_FIRST(&nfssvc_sockhead)) != NULL) {
  970                         simple_unlock(&nfsd_slock);
  971                         KASSERT(slp->ns_sref == 0);
  972                         slp->ns_sref++;
  973                         nfsrv_zapsock(slp);
  974                         nfsrv_slpderef(slp);
  975                         simple_lock(&nfsd_slock);
  976                 }
  977                 simple_unlock(&nfsd_slock);
  978                 splx(s);
  979                 nfsrv_cleancache();     /* And clear out server cache */
  980         } else {
  981                 simple_unlock(&nfsd_slock);
  982                 splx(s);
  983                 nfs_pub.np_valid = 0;
  984         }
  985 
  986         TAILQ_INIT(&nfssvc_sockhead);
  987         TAILQ_INIT(&nfssvc_sockpending);
  988         nfssvc_sockhead_flag &= ~SLP_INIT;
  989 
  990         TAILQ_INIT(&nfsd_head);
  991         SLIST_INIT(&nfsd_idle_head);
  992         nfsd_head_flag &= ~NFSD_CHECKSLP;
  993 
  994         nfs_udpsock = nfsrv_sockalloc();
  995 
  996 #ifdef INET6
  997         nfs_udp6sock = nfsrv_sockalloc();
  998 #endif
  999 
 1000 #ifdef ISO
 1001         nfs_cltpsock = nfsrv_sockalloc();
 1002 #endif
 1003 
 1004         simple_lock(&nfsd_slock);
 1005         if (nfssvc_sockhead_flag & SLP_WANTINIT) {
 1006                 nfssvc_sockhead_flag &= ~SLP_WANTINIT;
 1007                 wakeup(&nfssvc_sockhead);
 1008         }
 1009         simple_unlock(&nfsd_slock);
 1010         splx(s);
 1011 }
 1012 
 1013 /*
 1014  * Add entries to the server monitor log.
 1015  */
 1016 static void
 1017 nfsd_rt(sotype, nd, cacherep)
 1018         int sotype;
 1019         struct nfsrv_descript *nd;
 1020         int cacherep;
 1021 {
 1022         struct timeval tv;
 1023         struct drt *rt;
 1024 
 1025         rt = &nfsdrt.drt[nfsdrt.pos];
 1026         if (cacherep == RC_DOIT)
 1027                 rt->flag = 0;
 1028         else if (cacherep == RC_REPLY)
 1029                 rt->flag = DRT_CACHEREPLY;
 1030         else
 1031                 rt->flag = DRT_CACHEDROP;
 1032         if (sotype == SOCK_STREAM)
 1033                 rt->flag |= DRT_TCP;
 1034         if (nd->nd_flag & ND_NQNFS)
 1035                 rt->flag |= DRT_NQNFS;
 1036         else if (nd->nd_flag & ND_NFSV3)
 1037                 rt->flag |= DRT_NFSV3;
 1038         rt->proc = nd->nd_procnum;
 1039         if (mtod(nd->nd_nam, struct sockaddr *)->sa_family == AF_INET)
 1040             rt->ipadr = mtod(nd->nd_nam, struct sockaddr_in *)->sin_addr.s_addr;
 1041         else
 1042             rt->ipadr = INADDR_ANY;
 1043         getmicrotime(&tv);
 1044         rt->resptime = ((tv.tv_sec - nd->nd_starttime.tv_sec) * 1000000) +
 1045                 (tv.tv_usec - nd->nd_starttime.tv_usec);
 1046         rt->tstamp = tv;
 1047         nfsdrt.pos = (nfsdrt.pos + 1) % NFSRTTLOGSIZ;
 1048 }
 1049 #endif /* NFSSERVER */
 1050 
 1051 #ifdef NFS
 1052 
 1053 int nfs_defect = 0;
 1054 /*
 1055  * Asynchronous I/O threads for client nfs.
 1056  * They do read-ahead and write-behind operations on the block I/O cache.
 1057  * Never returns unless it fails or gets killed.
 1058  */
 1059 
 1060 int
 1061 nfssvc_iod(l)
 1062         struct lwp *l;
 1063 {
 1064         struct buf *bp;
 1065         int i;
 1066         struct nfs_iod *myiod;
 1067         struct nfsmount *nmp;
 1068         int error = 0;
 1069         struct proc *p = l->l_proc;
 1070 
 1071         /*
 1072          * Assign my position or return error if too many already running
 1073          */
 1074         myiod = NULL;
 1075         for (i = 0; i < NFS_MAXASYNCDAEMON; i++)
 1076                 if (nfs_asyncdaemon[i].nid_proc == NULL) {
 1077                         myiod = &nfs_asyncdaemon[i];
 1078                         break;
 1079                 }
 1080         if (myiod == NULL)
 1081                 return (EBUSY);
 1082         myiod->nid_proc = p;
 1083         nfs_numasync++;
 1084         PHOLD(l);
 1085         /*
 1086          * Just loop around doing our stuff until SIGKILL
 1087          */
 1088         for (;;) {
 1089                 while (/*CONSTCOND*/ TRUE) {
 1090                         simple_lock(&myiod->nid_slock);
 1091                         nmp = myiod->nid_mount;
 1092                         if (nmp) {
 1093                                 simple_lock(&nmp->nm_slock);
 1094                                 if (!TAILQ_EMPTY(&nmp->nm_bufq)) {
 1095                                         simple_unlock(&myiod->nid_slock);
 1096                                         break;
 1097                                 }
 1098                                 nmp->nm_bufqiods--;
 1099                                 simple_unlock(&nmp->nm_slock);
 1100                         }
 1101                         myiod->nid_want = p;
 1102                         myiod->nid_mount = NULL;
 1103                         error = ltsleep(&myiod->nid_want,
 1104                             PWAIT | PCATCH | PNORELOCK, "nfsidl", 0,
 1105                             &myiod->nid_slock);
 1106                         if (error)
 1107                                 goto quit;
 1108                 }
 1109 
 1110                 while ((bp = TAILQ_FIRST(&nmp->nm_bufq)) != NULL) {
 1111                         /* Take one off the front of the list */
 1112                         TAILQ_REMOVE(&nmp->nm_bufq, bp, b_freelist);
 1113                         nmp->nm_bufqlen--;
 1114                         if (nmp->nm_bufqwant &&
 1115                             nmp->nm_bufqlen < 2 * nfs_numasync) {
 1116                                 nmp->nm_bufqwant = FALSE;
 1117                                 wakeup(&nmp->nm_bufq);
 1118                         }
 1119                         simple_unlock(&nmp->nm_slock);
 1120                         (void)nfs_doio(bp);
 1121                         simple_lock(&nmp->nm_slock);
 1122                         /*
 1123                          * If there are more than one iod on this mount, 
 1124                          * then defect so that the iods can be shared out
 1125                          * fairly between the mounts
 1126                          */
 1127                         if (nfs_defect && nmp->nm_bufqiods > 1) {
 1128                                 myiod->nid_mount = NULL;
 1129                                 nmp->nm_bufqiods--;
 1130                                 break;
 1131                         }
 1132                 }
 1133                 simple_unlock(&nmp->nm_slock);
 1134         }
 1135 quit:
 1136         PRELE(l);
 1137         if (nmp)
 1138                 nmp->nm_bufqiods--;
 1139         myiod->nid_want = NULL;
 1140         myiod->nid_mount = NULL;
 1141         myiod->nid_proc = NULL;
 1142         nfs_numasync--;
 1143 
 1144         return error;
 1145 }
 1146 
 1147 void
 1148 nfs_iodinit()
 1149 {
 1150         int i;
 1151 
 1152         for (i = 0; i < NFS_MAXASYNCDAEMON; i++)
 1153                 simple_lock_init(&nfs_asyncdaemon[i].nid_slock);
 1154 }
 1155 
 1156 void
 1157 start_nfsio(void *arg)
 1158 {
 1159         nfssvc_iod(curlwp);
 1160 
 1161         kthread_exit(0);
 1162 }
 1163 
 1164 void
 1165 nfs_getset_niothreads(set)
 1166         int set;
 1167 {
 1168         int i, have, start;
 1169 
 1170         for (have = 0, i = 0; i < NFS_MAXASYNCDAEMON; i++)
 1171                 if (nfs_asyncdaemon[i].nid_proc != NULL)
 1172                         have++;
 1173 
 1174         if (set) {
 1175                 /* clamp to sane range */
 1176                 nfs_niothreads = max(0, min(nfs_niothreads, NFS_MAXASYNCDAEMON));
 1177 
 1178                 start = nfs_niothreads - have;
 1179 
 1180                 while (start > 0) {
 1181                         kthread_create1(start_nfsio, NULL, NULL, "nfsio");
 1182                         start--;
 1183                 }
 1184 
 1185                 for (i = 0; (start < 0) && (i < NFS_MAXASYNCDAEMON); i++)
 1186                         if (nfs_asyncdaemon[i].nid_proc != NULL) {
 1187                                 psignal(nfs_asyncdaemon[i].nid_proc, SIGKILL);
 1188                                 start++;
 1189                         }
 1190         } else {
 1191                 if (nfs_niothreads >= 0)
 1192                         nfs_niothreads = have;
 1193         }
 1194 }
 1195 
 1196 /*
 1197  * Get an authorization string for the uid by having the mount_nfs sitting
 1198  * on this mount point porpous out of the kernel and do it.
 1199  */
 1200 int
 1201 nfs_getauth(nmp, rep, cred, auth_str, auth_len, verf_str, verf_len, key)
 1202         struct nfsmount *nmp;
 1203         struct nfsreq *rep;
 1204         kauth_cred_t cred;
 1205         char **auth_str;
 1206         int *auth_len;
 1207         char *verf_str;
 1208         int *verf_len;
 1209         NFSKERBKEY_T key;               /* return session key */
 1210 {
 1211         int error = 0;
 1212 
 1213         while ((nmp->nm_iflag & NFSMNT_WAITAUTH) == 0) {
 1214                 nmp->nm_iflag |= NFSMNT_WANTAUTH;
 1215                 (void) tsleep((caddr_t)&nmp->nm_authtype, PSOCK,
 1216                         "nfsauth1", 2 * hz);
 1217                 error = nfs_sigintr(nmp, rep, rep->r_lwp);
 1218                 if (error) {
 1219                         nmp->nm_iflag &= ~NFSMNT_WANTAUTH;
 1220                         return (error);
 1221                 }
 1222         }
 1223         nmp->nm_iflag &= ~(NFSMNT_WAITAUTH | NFSMNT_WANTAUTH);
 1224         nmp->nm_authstr = *auth_str = (char *)malloc(RPCAUTH_MAXSIZ, M_TEMP, M_WAITOK);
 1225         nmp->nm_authlen = RPCAUTH_MAXSIZ;
 1226         nmp->nm_verfstr = verf_str;
 1227         nmp->nm_verflen = *verf_len;
 1228         nmp->nm_authuid = kauth_cred_geteuid(cred);
 1229         wakeup((caddr_t)&nmp->nm_authstr);
 1230 
 1231         /*
 1232          * And wait for mount_nfs to do its stuff.
 1233          */
 1234         while ((nmp->nm_iflag & NFSMNT_HASAUTH) == 0 && error == 0) {
 1235                 (void) tsleep((caddr_t)&nmp->nm_authlen, PSOCK,
 1236                         "nfsauth2", 2 * hz);
 1237                 error = nfs_sigintr(nmp, rep, rep->r_lwp);
 1238         }
 1239         if (nmp->nm_iflag & NFSMNT_AUTHERR) {
 1240                 nmp->nm_iflag &= ~NFSMNT_AUTHERR;
 1241                 error = EAUTH;
 1242         }
 1243         if (error)
 1244                 free((caddr_t)*auth_str, M_TEMP);
 1245         else {
 1246                 *auth_len = nmp->nm_authlen;
 1247                 *verf_len = nmp->nm_verflen;
 1248                 memcpy(key, nmp->nm_key, sizeof (NFSKERBKEY_T));
 1249         }
 1250         nmp->nm_iflag &= ~NFSMNT_HASAUTH;
 1251         nmp->nm_iflag |= NFSMNT_WAITAUTH;
 1252         if (nmp->nm_iflag & NFSMNT_WANTAUTH) {
 1253                 nmp->nm_iflag &= ~NFSMNT_WANTAUTH;
 1254                 wakeup((caddr_t)&nmp->nm_authtype);
 1255         }
 1256         return (error);
 1257 }
 1258 
 1259 /*
 1260  * Get a nickname authenticator and verifier.
 1261  */
 1262 int
 1263 nfs_getnickauth(struct nfsmount *nmp, kauth_cred_t cred, char **auth_str,
 1264     int *auth_len, char *verf_str, int verf_len)
 1265 {
 1266         struct timeval ktvin, ktvout, tv;
 1267         struct nfsuid *nuidp;
 1268         u_int32_t *nickp, *verfp;
 1269 
 1270         memset(&ktvout, 0, sizeof ktvout);      /* XXX gcc */
 1271 
 1272 #ifdef DIAGNOSTIC
 1273         if (verf_len < (4 * NFSX_UNSIGNED))
 1274                 panic("nfs_getnickauth verf too small");
 1275 #endif
 1276         LIST_FOREACH(nuidp, NMUIDHASH(nmp, kauth_cred_geteuid(cred)), nu_hash) {
 1277                 if (kauth_cred_geteuid(nuidp->nu_cr) == kauth_cred_geteuid(cred))
 1278                         break;
 1279         }
 1280         if (!nuidp || nuidp->nu_expire < time_second)
 1281                 return (EACCES);
 1282 
 1283         /*
 1284          * Move to the end of the lru list (end of lru == most recently used).
 1285          */
 1286         TAILQ_REMOVE(&nmp->nm_uidlruhead, nuidp, nu_lru);
 1287         TAILQ_INSERT_TAIL(&nmp->nm_uidlruhead, nuidp, nu_lru);
 1288 
 1289         nickp = (u_int32_t *)malloc(2 * NFSX_UNSIGNED, M_TEMP, M_WAITOK);
 1290         *nickp++ = txdr_unsigned(RPCAKN_NICKNAME);
 1291         *nickp = txdr_unsigned(nuidp->nu_nickname);
 1292         *auth_str = (char *)nickp;
 1293         *auth_len = 2 * NFSX_UNSIGNED;
 1294 
 1295         /*
 1296          * Now we must encrypt the verifier and package it up.
 1297          */
 1298         verfp = (u_int32_t *)verf_str;
 1299         *verfp++ = txdr_unsigned(RPCAKN_NICKNAME);
 1300         getmicrotime(&tv);
 1301         if (tv.tv_sec > nuidp->nu_timestamp.tv_sec ||
 1302             (tv.tv_sec == nuidp->nu_timestamp.tv_sec &&
 1303              tv.tv_usec > nuidp->nu_timestamp.tv_usec))
 1304                 nuidp->nu_timestamp = tv;
 1305         else
 1306                 nuidp->nu_timestamp.tv_usec++;
 1307         ktvin.tv_sec = txdr_unsigned(nuidp->nu_timestamp.tv_sec);
 1308         ktvin.tv_usec = txdr_unsigned(nuidp->nu_timestamp.tv_usec);
 1309 
 1310         /*
 1311          * Now encrypt the timestamp verifier in ecb mode using the session
 1312          * key.
 1313          */
 1314 #ifdef NFSKERB
 1315         XXX
 1316 #endif
 1317 
 1318         *verfp++ = ktvout.tv_sec;
 1319         *verfp++ = ktvout.tv_usec;
 1320         *verfp = 0;
 1321         return (0);
 1322 }
 1323 
 1324 /*
 1325  * Save the current nickname in a hash list entry on the mount point.
 1326  */
 1327 int
 1328 nfs_savenickauth(nmp, cred, len, key, mdp, dposp, mrep)
 1329         struct nfsmount *nmp;
 1330         kauth_cred_t cred;
 1331         int len;
 1332         NFSKERBKEY_T key;
 1333         struct mbuf **mdp;
 1334         char **dposp;
 1335         struct mbuf *mrep;
 1336 {
 1337         struct nfsuid *nuidp;
 1338         u_int32_t *tl;
 1339         int32_t t1;
 1340         struct mbuf *md = *mdp;
 1341         struct timeval ktvin, ktvout;
 1342         u_int32_t nick;
 1343         char *dpos = *dposp, *cp2;
 1344         int deltasec, error = 0;
 1345 
 1346         memset(&ktvout, 0, sizeof ktvout);       /* XXX gcc */
 1347 
 1348         if (len == (3 * NFSX_UNSIGNED)) {
 1349                 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
 1350                 ktvin.tv_sec = *tl++;
 1351                 ktvin.tv_usec = *tl++;
 1352                 nick = fxdr_unsigned(u_int32_t, *tl);
 1353 
 1354                 /*
 1355                  * Decrypt the timestamp in ecb mode.
 1356                  */
 1357 #ifdef NFSKERB
 1358                 XXX
 1359 #endif
 1360                 ktvout.tv_sec = fxdr_unsigned(long, ktvout.tv_sec);
 1361                 ktvout.tv_usec = fxdr_unsigned(long, ktvout.tv_usec);
 1362                 deltasec = time_second - ktvout.tv_sec;
 1363                 if (deltasec < 0)
 1364                         deltasec = -deltasec;
 1365                 /*
 1366                  * If ok, add it to the hash list for the mount point.
 1367                  */
 1368                 if (deltasec <= NFS_KERBCLOCKSKEW) {
 1369                         if (nmp->nm_numuids < nuidhash_max) {
 1370                                 nmp->nm_numuids++;
 1371                                 nuidp = (struct nfsuid *)
 1372                                    malloc(sizeof (struct nfsuid), M_NFSUID,
 1373                                         M_WAITOK);
 1374                         } else {
 1375                                 nuidp = TAILQ_FIRST(&nmp->nm_uidlruhead);
 1376                                 LIST_REMOVE(nuidp, nu_hash);
 1377                                 TAILQ_REMOVE(&nmp->nm_uidlruhead, nuidp,
 1378                                         nu_lru);
 1379                         }
 1380                         nuidp->nu_flag = 0;
 1381                         kauth_cred_seteuid(nuidp->nu_cr, kauth_cred_geteuid(cred));
 1382                         nuidp->nu_expire = time_second + NFS_KERBTTL;
 1383                         nuidp->nu_timestamp = ktvout;
 1384                         nuidp->nu_nickname = nick;
 1385                         memcpy(nuidp->nu_key, key, sizeof (NFSKERBKEY_T));
 1386                         TAILQ_INSERT_TAIL(&nmp->nm_uidlruhead, nuidp,
 1387                                 nu_lru);
 1388                         LIST_INSERT_HEAD(NMUIDHASH(nmp, kauth_cred_geteuid(cred)),
 1389                                 nuidp, nu_hash);
 1390                 }
 1391         } else
 1392                 nfsm_adv(nfsm_rndup(len));
 1393 nfsmout:
 1394         *mdp = md;
 1395         *dposp = dpos;
 1396         return (error);
 1397 }
 1398 #endif /* NFS */

Cache object: 3a37a3f631b34f1abea5cdfb9acb4ed9


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