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_nqlease.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_nqlease.c,v 1.74 2006/11/09 09:53:57 yamt Exp $    */
    2 
    3 /*
    4  * Copyright (c) 1992, 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_nqlease.c       8.9 (Berkeley) 5/20/95
   35  */
   36 
   37 /*
   38  * References:
   39  *      Cary G. Gray and David R. Cheriton, "Leases: An Efficient Fault-Tolerant
   40  *              Mechanism for Distributed File Cache Consistency",
   41  *              In Proc. of the Twelfth ACM Symposium on Operating Systems
   42  *              Principals, pg. 202-210, Litchfield Park, AZ, Dec. 1989.
   43  *      Michael N. Nelson, Brent B. Welch and John K. Ousterhout, "Caching
   44  *              in the Sprite Network File System", ACM TOCS 6(1),
   45  *              pages 134-154, February 1988.
   46  *      V. Srinivasan and Jeffrey C. Mogul, "Spritely NFS: Implementation and
   47  *              Performance of Cache-Consistency Protocols", Digital
   48  *              Equipment Corporation WRL Research Report 89/5, May 1989.
   49  */
   50 
   51 #include <sys/cdefs.h>
   52 __KERNEL_RCSID(0, "$NetBSD: nfs_nqlease.c,v 1.74 2006/11/09 09:53:57 yamt Exp $");
   53 
   54 #include "fs_nfs.h"
   55 #include "opt_nfs.h"
   56 #include "opt_nfsserver.h"
   57 #include "opt_inet.h"
   58 
   59 #include <sys/param.h>
   60 #include <sys/vnode.h>
   61 #include <sys/mount.h>
   62 #include <sys/kernel.h>
   63 #include <sys/namei.h>
   64 #include <sys/proc.h>
   65 #include <sys/systm.h>
   66 #include <sys/mbuf.h>
   67 #include <sys/socket.h>
   68 #include <sys/socketvar.h>
   69 #include <sys/file.h>
   70 #include <sys/buf.h>
   71 #include <sys/stat.h>
   72 #include <sys/protosw.h>
   73 #include <sys/signalvar.h>
   74 #include <sys/kauth.h>
   75 
   76 #include <miscfs/syncfs/syncfs.h>
   77 
   78 #include <netinet/in.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/xdr_subs.h>
   84 #include <nfs/nqnfs.h>
   85 #include <nfs/nfsnode.h>
   86 #include <nfs/nfsmount.h>
   87 #include <nfs/nfs_var.h>
   88 
   89 time_t nqnfsstarttime = (time_t)0;
   90 int nqsrv_clockskew = NQ_CLOCKSKEW;
   91 int nqsrv_writeslack = NQ_WRITESLACK;
   92 int nqsrv_maxlease = NQ_MAXLEASE;
   93 int nqsrv_maxnumlease = NQ_MAXNUMLEASE;
   94 
   95 struct nqleasehead nqtimerhead;
   96 struct nqfhhashhead *nqfhhashtbl;
   97 u_long nqfhhash;
   98 
   99 MALLOC_DEFINE(M_NQLEASE, "NQNFS Lease", "Nqnfs lease");
  100 MALLOC_DEFINE(M_NQMHOST, "NQNFS Host", "Nqnfs host address table");
  101 
  102 /*
  103  * Signifies which rpcs can have piggybacked lease requests
  104  */
  105 const int nqnfs_piggy[NFS_NPROCS] = {
  106         0,
  107         0,
  108         ND_WRITE,
  109         ND_READ,
  110         0,
  111         ND_READ,
  112         ND_READ,
  113         ND_WRITE,
  114         0,
  115         0,
  116         0,
  117         0,
  118         0,
  119         0,
  120         0,
  121         0,
  122         ND_READ,
  123         ND_READ,
  124         0,
  125         0,
  126         0,
  127         0,
  128         0,
  129         0,
  130         0,
  131         0,
  132 };
  133 
  134 extern const nfstype nfsv2_type[9];
  135 extern const nfstype nfsv3_type[9];
  136 extern struct nfssvc_sock *nfs_udpsock;
  137 #ifdef ISO
  138 extern struct nfssvc_sock *nfs_cltpsock
  139 #endif
  140 #ifdef INET6
  141 extern struct nfssvc_sock *nfs_udp6sock;
  142 #endif
  143 extern int nfsd_waiting;
  144 extern struct nfsstats nfsstats;
  145 
  146 
  147 #if defined(NFSSERVER)
  148 /*
  149  * Get or check for a lease for "vp", based on ND_CHECK flag.
  150  * The rules are as follows:
  151  * - if a current non-caching lease, reply non-caching
  152  * - if a current lease for same host only, extend lease
  153  * - if a read cachable lease and a read lease request
  154  *      add host to list any reply cachable
  155  * - else { set non-cachable for read-write sharing }
  156  *      send eviction notice messages to all other hosts that have lease
  157  *      wait for lease termination { either by receiving vacated messages
  158  *                                      from all the other hosts or expiry
  159  *                                      via. timeout }
  160  *      modify lease to non-cachable
  161  * - else if no current lease, issue new one
  162  * - reply
  163  * - return boolean TRUE iff nam should be m_freem()'d
  164  * NB: Since nqnfs_serverd() is called from a timer, any potential tsleep()
  165  *     in here must be framed by nqsrv_locklease() and nqsrv_unlocklease().
  166  *     nqsrv_locklease() is coded such that at least one of LC_LOCKED and
  167  *     LC_WANTED is set whenever a process is tsleeping in it. The exception
  168  *     is when a new lease is being allocated, since it is not in the timer
  169  *     queue yet. (Ditto for the splsoftnet() and splx(s) calls)
  170  */
  171 int
  172 nqsrv_getlease(vp, duration, flags, slp, lwp, nam, cachablep, frev, cred)
  173         struct vnode *vp;
  174         u_int32_t *duration;
  175         int flags;
  176         struct nfssvc_sock *slp;
  177         struct lwp *lwp;
  178         struct mbuf *nam;
  179         int *cachablep;
  180         u_quad_t *frev;
  181         kauth_cred_t cred;
  182 {
  183         struct nqlease *lp;
  184         struct nqfhhashhead *lpp = NULL;
  185         struct nqhost *lph = NULL;
  186         struct nqlease *tlp;
  187         struct nqm **lphp;
  188         struct vattr vattr;
  189         nfsrvfh_t nsfh;
  190         int i, ok, error, s;
  191 
  192         if (vp->v_type != VREG && vp->v_type != VDIR && vp->v_type != VLNK)
  193                 return (0);
  194 
  195         nfs_init();
  196 
  197         if (*duration > nqsrv_maxlease)
  198                 *duration = nqsrv_maxlease;
  199         error = VOP_GETATTR(vp, &vattr, cred, lwp);
  200         if (error)
  201                 return (error);
  202         *frev = vattr.va_filerev;
  203         s = splsoftnet();
  204         tlp = vp->v_lease;
  205         if ((flags & ND_CHECK) == 0)
  206                 nfsstats.srvnqnfs_getleases++;
  207         if (tlp == 0) {
  208                 /*
  209                  * Find the lease by searching the hash list.
  210                  */
  211                 error = nfsrv_composefh(vp, &nsfh, TRUE);
  212                 if (error) {
  213                         splx(s);
  214                         return (error);
  215                 }
  216                 lpp = NQFHHASH(NFSRVFH_FHANDLE(&nsfh)->fh_fid.fid_data);
  217                 LIST_FOREACH (lp, lpp, lc_hash) {
  218                         if (!nfsrv_comparefh(&lp->lc_fh, &nsfh)) {
  219                                 /* Found it */
  220                                 lp->lc_vp = vp;
  221                                 vp->v_lease = lp;
  222                                 tlp = lp;
  223                                 break;
  224                         }
  225                 }
  226         } else
  227                 lp = tlp;
  228         if (lp != 0) {
  229                 if ((lp->lc_flag & LC_NONCACHABLE) ||
  230                     (lp->lc_morehosts == (struct nqm *)0 &&
  231                      nqsrv_cmpnam(slp, nam, &lp->lc_host)))
  232                         goto doreply;
  233                 if ((flags & ND_READ) && (lp->lc_flag & LC_WRITE) == 0) {
  234                         if (flags & ND_CHECK)
  235                                 goto doreply;
  236                         if (nqsrv_cmpnam(slp, nam, &lp->lc_host))
  237                                 goto doreply;
  238                         i = 0;
  239                         if (lp->lc_morehosts) {
  240                                 lph = lp->lc_morehosts->lpm_hosts;
  241                                 lphp = &lp->lc_morehosts->lpm_next;
  242                                 ok = 1;
  243                         } else {
  244                                 lphp = &lp->lc_morehosts;
  245                                 ok = 0;
  246                         }
  247                         while (ok && (lph->lph_flag & LC_VALID)) {
  248                                 if (nqsrv_cmpnam(slp, nam, lph))
  249                                         goto doreply;
  250                                 if (++i == LC_MOREHOSTSIZ) {
  251                                         i = 0;
  252                                         if (*lphp) {
  253                                                 lph = (*lphp)->lpm_hosts;
  254                                                 lphp = &((*lphp)->lpm_next);
  255                                         } else
  256                                                 ok = 0;
  257                                 } else
  258                                         lph++;
  259                         }
  260                         nqsrv_locklease(lp);
  261                         if (!ok) {
  262                                 *lphp = (struct nqm *)
  263                                         malloc(sizeof (struct nqm),
  264                                                 M_NQMHOST, M_WAITOK);
  265                                 memset((caddr_t)*lphp, 0, sizeof (struct nqm));
  266                                 lph = (*lphp)->lpm_hosts;
  267                         }
  268                         nqsrv_addhost(lph, slp, nam);
  269                         nqsrv_unlocklease(lp);
  270                 } else {
  271                         lp->lc_flag |= LC_NONCACHABLE;
  272                         nqsrv_locklease(lp);
  273                         nqsrv_send_eviction(vp, lp, slp, nam, cred, lwp);
  274                         nqsrv_waitfor_expiry(lp);
  275                         nqsrv_unlocklease(lp);
  276                 }
  277 doreply:
  278                 /*
  279                  * Update the lease and return
  280                  */
  281                 if ((flags & ND_CHECK) == 0)
  282                         nqsrv_instimeq(lp, *duration);
  283                 if (lp->lc_flag & LC_NONCACHABLE)
  284                         *cachablep = 0;
  285                 else {
  286                         *cachablep = 1;
  287                         if (flags & ND_WRITE)
  288                                 lp->lc_flag |= LC_WRITTEN;
  289                 }
  290                 splx(s);
  291                 return (0);
  292         }
  293         splx(s);
  294         if (flags & ND_CHECK) {
  295                 return (0);
  296         }
  297 
  298         /*
  299          * Allocate new lease
  300          * The value of nqsrv_maxnumlease should be set generously, so that
  301          * the following "printf" happens infrequently.
  302          */
  303         if (nfsstats.srvnqnfs_leases > nqsrv_maxnumlease) {
  304                 printf("Nqnfs server, too many leases\n");
  305                 do {
  306                         (void) tsleep((caddr_t)&lbolt, PSOCK,
  307                                         "nqsrvnuml", 0);
  308                 } while (nfsstats.srvnqnfs_leases > nqsrv_maxnumlease);
  309         }
  310         MALLOC(lp, struct nqlease *, sizeof (struct nqlease), M_NQLEASE, M_WAITOK);
  311         memset((caddr_t)lp, 0, sizeof (struct nqlease));
  312         if (flags & ND_WRITE)
  313                 lp->lc_flag |= (LC_WRITE | LC_WRITTEN);
  314         nqsrv_addhost(&lp->lc_host, slp, nam);
  315         lp->lc_vp = vp;
  316         nfsrv_copyfh(&lp->lc_fh, &nsfh);
  317         if(!lpp)
  318                 panic("nfs_nqlease.c: Phoney lpp");
  319         LIST_INSERT_HEAD(lpp, lp, lc_hash);
  320         vp->v_lease = lp;
  321         s = splsoftnet();
  322         nqsrv_instimeq(lp, *duration);
  323         splx(s);
  324         *cachablep = 1;
  325         if (++nfsstats.srvnqnfs_leases > nfsstats.srvnqnfs_maxleases)
  326                 nfsstats.srvnqnfs_maxleases = nfsstats.srvnqnfs_leases;
  327         return (0);
  328 }
  329 
  330 /*
  331  * Add a host to an nqhost structure for a lease.
  332  */
  333 void
  334 nqsrv_addhost(lph, slp, nam)
  335         struct nqhost *lph;
  336         struct nfssvc_sock *slp;
  337         struct mbuf *nam;
  338 {
  339         struct sockaddr_in *saddr;
  340 
  341         if (slp == NQLOCALSLP)
  342                 lph->lph_flag |= (LC_VALID | LC_LOCAL);
  343         else if (slp == nfs_udpsock) {
  344                 saddr = mtod(nam, struct sockaddr_in *);
  345                 lph->lph_flag |= (LC_VALID | LC_UDP);
  346                 lph->lph_inetaddr = saddr->sin_addr.s_addr;
  347                 lph->lph_port = saddr->sin_port;
  348 #ifdef INET6
  349         } else if (slp == nfs_udp6sock) {
  350                 lph->lph_nam = m_copym(nam, 0, M_COPYALL, M_WAIT);
  351                 lph->lph_flag |= (LC_VALID | LC_UDP6);
  352 #endif
  353 #ifdef ISO
  354         } else if (slp == nfs_cltpsock) {
  355                 lph->lph_nam = m_copym(nam, 0, M_COPYALL, M_WAIT);
  356                 lph->lph_flag |= (LC_VALID | LC_CLTP);
  357 #endif
  358         } else {
  359                 lph->lph_flag |= (LC_VALID | LC_SREF);
  360                 lph->lph_slp = slp;
  361                 slp->ns_sref++;
  362         }
  363 }
  364 
  365 /*
  366  * Update the lease expiry time and position it in the timer queue correctly.
  367  */
  368 void
  369 nqsrv_instimeq(lp, duration)
  370         struct nqlease *lp;
  371         u_int32_t duration;
  372 {
  373         struct nqlease *tlp;
  374         time_t newexpiry;
  375 
  376         newexpiry = time_second + duration + nqsrv_clockskew;
  377         if (lp->lc_expiry == newexpiry)
  378                 return;
  379         if (CIRCLEQ_NEXT(lp, lc_timer) != 0)
  380                 CIRCLEQ_REMOVE(&nqtimerhead, lp, lc_timer);
  381         lp->lc_expiry = newexpiry;
  382 
  383         /*
  384          * Find where in the queue it should be.
  385          */
  386         tlp = CIRCLEQ_LAST(&nqtimerhead);
  387         while (tlp != (void *)&nqtimerhead && tlp->lc_expiry > newexpiry)
  388                 tlp = CIRCLEQ_PREV(tlp, lc_timer);
  389 #ifdef HASNVRAM
  390         if (tlp == CIRCLEQ_LAST(&nqtimerhead))
  391                 NQSTORENOVRAM(newexpiry);
  392 #endif /* HASNVRAM */
  393         if (tlp == (void *)&nqtimerhead) {
  394                 CIRCLEQ_INSERT_HEAD(&nqtimerhead, lp, lc_timer);
  395         } else {
  396                 CIRCLEQ_INSERT_AFTER(&nqtimerhead, tlp, lp, lc_timer);
  397         }
  398 }
  399 
  400 /*
  401  * Compare the requesting host address with the lph entry in the lease.
  402  * Return true iff it is the same.
  403  * This is somewhat messy due to the union in the nqhost structure.
  404  * The local host is indicated by the special value of NQLOCALSLP for slp.
  405  */
  406 int
  407 nqsrv_cmpnam(slp, nam, lph)
  408         struct nfssvc_sock *slp;
  409         struct mbuf *nam;
  410         struct nqhost *lph;
  411 {
  412         struct sockaddr_in *saddr;
  413         struct mbuf *addr;
  414         union nethostaddr lhaddr;
  415         int ret;
  416 
  417         if (slp == NQLOCALSLP) {
  418                 if (lph->lph_flag & LC_LOCAL)
  419                         return (1);
  420                 else
  421                         return (0);
  422         }
  423         if (slp == nfs_udpsock
  424 #ifdef ISO
  425             || slp == nfs_cltpsock
  426 #endif
  427 #ifdef INET6
  428             || slp == nfs_udp6sock
  429 #endif
  430         )
  431                 addr = nam;
  432         else
  433                 addr = slp->ns_nam;
  434         if (lph->lph_flag & LC_UDP)
  435                 ret = netaddr_match(AF_INET, &lph->lph_haddr, addr);
  436         else if (lph->lph_flag & LC_CLTP)
  437                 ret = netaddr_match(AF_ISO, &lph->lph_claddr, addr);
  438         else {
  439                 if ((lph->lph_slp->ns_flag & SLP_VALID) == 0)
  440                         return (0);
  441                 saddr = mtod(lph->lph_slp->ns_nam, struct sockaddr_in *);
  442                 if (saddr->sin_family == AF_INET)
  443                         lhaddr.had_inetaddr = saddr->sin_addr.s_addr;
  444                 else
  445                         lhaddr.had_nam = lph->lph_slp->ns_nam;
  446                 ret = netaddr_match(saddr->sin_family, &lhaddr, addr);
  447         }
  448         return (ret);
  449 }
  450 
  451 /*
  452  * Send out eviction notice messages to all other hosts for the lease.
  453  */
  454 void
  455 nqsrv_send_eviction(vp, lp, slp, nam, cred, l)
  456         struct vnode *vp;
  457         struct nqlease *lp;
  458         struct nfssvc_sock *slp;
  459         struct mbuf *nam;
  460         kauth_cred_t cred;
  461         struct lwp *l;
  462 {
  463         struct nqhost *lph = &lp->lc_host;
  464         struct mbuf *m;
  465         int siz;
  466         struct nqm *lphnext = lp->lc_morehosts;
  467         struct mbuf *mreq, *mb, *nam2, *mheadend;
  468         struct socket *so;
  469         struct sockaddr_in *saddr;
  470         nfsrvfh_t nsfh;
  471         caddr_t bpos, cp;
  472         u_int32_t xid, *tl;
  473         int len = 1, ok = 1, i = 0;
  474         int sotype, *solockp;
  475 
  476         while (ok && (lph->lph_flag & LC_VALID)) {
  477                 if (nqsrv_cmpnam(slp, nam, lph))
  478                         lph->lph_flag |= LC_VACATED;
  479                 else if ((lph->lph_flag & (LC_LOCAL | LC_VACATED)) == 0) {
  480                         int error;
  481 
  482                         if (lph->lph_flag & LC_UDP) {
  483                                 nam2 = m_get(M_WAIT, MT_SONAME);
  484                                 MCLAIM(nam2, &nfs_mowner);
  485                                 saddr = mtod(nam2, struct sockaddr_in *);
  486                                 nam2->m_len = saddr->sin_len =
  487                                         sizeof (struct sockaddr_in);
  488                                 saddr->sin_family = AF_INET;
  489                                 saddr->sin_addr.s_addr = lph->lph_inetaddr;
  490                                 saddr->sin_port = lph->lph_port;
  491                                 so = nfs_udpsock->ns_so;
  492 #ifdef ISO
  493                         } else if (lph->lph_flag & LC_CLTP) {
  494                                 nam2 = lph->lph_nam;
  495                                 so = nfs_cltpsock->ns_so;
  496 #endif
  497 #ifdef INET6
  498                         } else if (lph->lph_flag & LC_UDP6) {
  499                                 nam2 = lph->lph_nam;
  500                                 so = nfs_udp6sock->ns_so;
  501 #endif
  502                         } else if (lph->lph_slp->ns_flag & SLP_VALID) {
  503                                 nam2 = (struct mbuf *)0;
  504                                 so = lph->lph_slp->ns_so;
  505                         } else
  506                                 goto nextone;
  507                         if (!so)
  508                                 goto nextone;
  509                         sotype = so->so_type;
  510                         if (so->so_proto->pr_flags & PR_CONNREQUIRED)
  511                                 solockp = &lph->lph_slp->ns_solock;
  512                         else
  513                                 solockp = (int *)0;
  514                         nfsm_reqhead((struct nfsnode *)0, NQNFSPROC_EVICTED,
  515                                 NFSX_V3FH + NFSX_UNSIGNED);
  516                         error = nfsrv_composefh(vp, &nsfh, 1);
  517                         KASSERT(error == 0); /* XXX */
  518                         nfsm_srvfhtom(&nsfh, 1);
  519                         m = mreq;
  520                         siz = 0;
  521                         while (m) {
  522                                 siz += m->m_len;
  523                                 m = m->m_next;
  524                         }
  525                         if (siz <= 0 || siz > NFS_MAXPACKET) {
  526                                 printf("mbuf siz=%d\n",siz);
  527                                 panic("Bad nfs svc reply");
  528                         }
  529                         m = nfsm_rpchead(cred, (NFSMNT_NFSV3 | NFSMNT_NQNFS),
  530                                 NQNFSPROC_EVICTED,
  531                                 RPCAUTH_UNIX, 5 * NFSX_UNSIGNED, (char *)0,
  532                                 0, (char *)0, mreq, siz, &mheadend, &xid);
  533                         /*
  534                          * For stream protocols, prepend a Sun RPC
  535                          * Record Mark.
  536                          */
  537                         if (sotype == SOCK_STREAM) {
  538                                 M_PREPEND(m, NFSX_UNSIGNED, M_WAIT);
  539                                 *mtod(m, u_int32_t *) = htonl(0x80000000 |
  540                                         (m->m_pkthdr.len - NFSX_UNSIGNED));
  541                         }
  542                         if (((lph->lph_flag & (LC_UDP | LC_CLTP | LC_UDP6))
  543                             == 0 &&
  544                             (lph->lph_slp->ns_flag & SLP_VALID) == 0) ||
  545                             (solockp && (*solockp & NFSMNT_SNDLOCK)))
  546                                 m_freem(m);
  547                         else {
  548                                 if (solockp)
  549                                         *solockp |= NFSMNT_SNDLOCK;
  550                                 (void) nfs_send(so, nam2, m,
  551                                                 (struct nfsreq *)0, l);
  552                                 if (solockp)
  553                                         nfs_sndunlock(solockp);
  554                         }
  555                         if (lph->lph_flag & LC_UDP)
  556                                 (void) m_free(nam2);
  557                 }
  558 nextone:
  559                 if (++i == len) {
  560                         if (lphnext) {
  561                                 i = 0;
  562                                 len = LC_MOREHOSTSIZ;
  563                                 lph = lphnext->lpm_hosts;
  564                                 lphnext = lphnext->lpm_next;
  565                         } else
  566                                 ok = 0;
  567                 } else
  568                         lph++;
  569         }
  570 }
  571 
  572 /*
  573  * Wait for the lease to expire.
  574  * This will occur when all clients have sent "vacated" messages to
  575  * this server OR when it expires do to timeout.
  576  */
  577 void
  578 nqsrv_waitfor_expiry(lp)
  579         struct nqlease *lp;
  580 {
  581         struct nqhost *lph;
  582         int i;
  583         struct nqm *lphnext;
  584         int len, ok;
  585 
  586 tryagain:
  587         if (time_second > lp->lc_expiry)
  588                 return;
  589         lph = &lp->lc_host;
  590         lphnext = lp->lc_morehosts;
  591         len = 1;
  592         i = 0;
  593         ok = 1;
  594         while (ok && (lph->lph_flag & LC_VALID)) {
  595                 if ((lph->lph_flag & (LC_LOCAL | LC_VACATED)) == 0) {
  596                         lp->lc_flag |= LC_EXPIREDWANTED;
  597                         (void) tsleep((caddr_t)&lp->lc_flag, PSOCK,
  598                                         "nqexp", 0);
  599                         goto tryagain;
  600                 }
  601                 if (++i == len) {
  602                         if (lphnext) {
  603                                 i = 0;
  604                                 len = LC_MOREHOSTSIZ;
  605                                 lph = lphnext->lpm_hosts;
  606                                 lphnext = lphnext->lpm_next;
  607                         } else
  608                                 ok = 0;
  609                 } else
  610                         lph++;
  611         }
  612 }
  613 
  614 /*
  615  * Nqnfs server timer that maintains the server lease queue.
  616  * Scan the lease queue for expired entries:
  617  * - when one is found, wakeup anyone waiting for it
  618  *   else dequeue and free
  619  */
  620 void
  621 nqnfs_serverd()
  622 {
  623         struct nqlease *lp;
  624         struct nqhost *lph;
  625         struct nqlease *nextlp;
  626         struct nqm *lphnext, *olphnext;
  627         int i, len, ok;
  628 
  629         for (lp = CIRCLEQ_FIRST(&nqtimerhead); lp != (void *)&nqtimerhead;
  630             lp = nextlp) {
  631                 if (lp->lc_expiry >= time_second)
  632                         break;
  633                 nextlp = CIRCLEQ_NEXT(lp, lc_timer);
  634                 if (lp->lc_flag & LC_EXPIREDWANTED) {
  635                         lp->lc_flag &= ~LC_EXPIREDWANTED;
  636                         wakeup((caddr_t)&lp->lc_flag);
  637                 } else if ((lp->lc_flag & (LC_LOCKED | LC_WANTED)) == 0) {
  638                     /*
  639                      * Make a best effort at keeping a write caching lease long
  640                      * enough by not deleting it until it has been explicitly
  641                      * vacated or there have been no writes in the previous
  642                      * write_slack seconds since expiry and the nfsds are not
  643                      * all busy. The assumption is that if the nfsds are not
  644                      * all busy now (no queue of nfs requests), then the client
  645                      * would have been able to do at least one write to the
  646                      * file during the last write_slack seconds if it was still
  647                      * trying to push writes to the server.
  648                      */
  649                     if ((lp->lc_flag & (LC_WRITE | LC_VACATED)) == LC_WRITE &&
  650                         ((lp->lc_flag & LC_WRITTEN) || nfsd_waiting == 0)) {
  651                         lp->lc_flag &= ~LC_WRITTEN;
  652                         nqsrv_instimeq(lp, nqsrv_writeslack);
  653                     } else {
  654                         CIRCLEQ_REMOVE(&nqtimerhead, lp, lc_timer);
  655                         LIST_REMOVE(lp, lc_hash);
  656                         /*
  657                          * This soft reference may no longer be valid, but
  658                          * no harm done. The worst case is if the vnode was
  659                          * recycled and has another valid lease reference,
  660                          * which is dereferenced prematurely.
  661                          */
  662                         lp->lc_vp->v_lease = (struct nqlease *)0;
  663                         lph = &lp->lc_host;
  664                         lphnext = lp->lc_morehosts;
  665                         olphnext = (struct nqm *)0;
  666                         len = 1;
  667                         i = 0;
  668                         ok = 1;
  669                         while (ok && (lph->lph_flag & LC_VALID)) {
  670                                 if (lph->lph_flag & (LC_CLTP | LC_UDP6))
  671                                         (void) m_free(lph->lph_nam);
  672                                 if (lph->lph_flag & LC_SREF)
  673                                         nfsrv_slpderef(lph->lph_slp);
  674                                 if (++i == len) {
  675                                         if (olphnext) {
  676                                                 free((caddr_t)olphnext, M_NQMHOST);
  677                                                 olphnext = (struct nqm *)0;
  678                                         }
  679                                         if (lphnext) {
  680                                                 olphnext = lphnext;
  681                                                 i = 0;
  682                                                 len = LC_MOREHOSTSIZ;
  683                                                 lph = lphnext->lpm_hosts;
  684                                                 lphnext = lphnext->lpm_next;
  685                                         } else
  686                                                 ok = 0;
  687                                 } else
  688                                         lph++;
  689                         }
  690                         FREE((caddr_t)lp, M_NQLEASE);
  691                         if (olphnext)
  692                                 free((caddr_t)olphnext, M_NQMHOST);
  693                         nfsstats.srvnqnfs_leases--;
  694                     }
  695                 }
  696         }
  697 }
  698 
  699 /*
  700  * Called from nfssvc_nfsd() for a getlease rpc request.
  701  * Do the from/to xdr translation and call nqsrv_getlease() to
  702  * do the real work.
  703  */
  704 int
  705 nqnfsrv_getlease(nfsd, slp, lwp, mrq)
  706         struct nfsrv_descript *nfsd;
  707         struct nfssvc_sock *slp;
  708         struct lwp *lwp;
  709         struct mbuf **mrq;
  710 {
  711         struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
  712         struct mbuf *nam = nfsd->nd_nam;
  713         caddr_t dpos = nfsd->nd_dpos;
  714         kauth_cred_t cred = nfsd->nd_cr;
  715         struct nfs_fattr *fp;
  716         struct vattr va;
  717         struct vnode *vp;
  718         nfsrvfh_t nsfh;
  719         u_int32_t *tl;
  720         int32_t t1;
  721         u_quad_t frev;
  722         caddr_t bpos;
  723         int error = 0;
  724         char *cp2;
  725         struct mbuf *mb, *mreq;
  726         int flags, rdonly, cache = 0;
  727 
  728         nfsm_srvmtofh(&nsfh);
  729         nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
  730         flags = fxdr_unsigned(int, *tl++);
  731         nfsd->nd_duration = fxdr_unsigned(int, *tl);
  732         error = nfsrv_fhtovp(&nsfh, 1, &vp, cred, slp, nam, &rdonly,
  733                 (nfsd->nd_flag & ND_KERBAUTH), FALSE);
  734         if (error) {
  735                 nfsm_reply(0);
  736                 return 0;
  737         }
  738         if (rdonly && flags == ND_WRITE) {
  739                 vput(vp);
  740                 error = EROFS;
  741                 nfsm_reply(0);
  742                 return 0;
  743         }
  744         (void) nqsrv_getlease(vp, &nfsd->nd_duration, flags, slp, lwp,
  745                 nam, &cache, &frev, cred);
  746         error = VOP_GETATTR(vp, &va, cred, lwp);
  747         vput(vp);
  748         nfsm_reply(NFSX_V3FATTR + 4 * NFSX_UNSIGNED);
  749         nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
  750         *tl++ = txdr_unsigned(cache);
  751         *tl++ = txdr_unsigned(nfsd->nd_duration);
  752         txdr_hyper(frev, tl);
  753         nfsm_build(fp, struct nfs_fattr *, NFSX_V3FATTR);
  754         nfsm_srvfillattr(&va, fp);
  755         nfsm_srvdone;
  756 }
  757 
  758 /*
  759  * Called from nfssvc_nfsd() when a "vacated" message is received from a
  760  * client. Find the entry and expire it.
  761  */
  762 int
  763 nqnfsrv_vacated(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
  764     struct lwp *lwp, struct mbuf **mrq)
  765 {
  766         struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
  767         struct mbuf *nam = nfsd->nd_nam;
  768         caddr_t dpos = nfsd->nd_dpos;
  769         struct nqlease *lp;
  770         struct nqhost *lph;
  771         struct nqlease *tlp = (struct nqlease *)0;
  772         nfsrvfh_t nsfh;
  773         fhandle_t *fhp;
  774         u_int32_t *tl;
  775         int32_t t1;
  776         struct nqm *lphnext;
  777         struct mbuf *mreq, *mb;
  778         int error = 0, i, len, ok, gotit = 0, cache = 0;
  779         char *cp2, *bpos;
  780         u_quad_t frev;
  781 
  782         nfsm_srvmtofh(&nsfh);
  783         m_freem(mrep);
  784         fhp = &nsfh.nsfh_u.u_fh;
  785         /*
  786          * Find the lease by searching the hash list.
  787          */
  788         LIST_FOREACH(lp, NQFHHASH(fhp->fh_fid.fid_data), lc_hash) {
  789                 if (!nfsrv_comparefh(&lp->lc_fh, &nsfh)) {
  790                         /* Found it */
  791                         tlp = lp;
  792                         break;
  793                 }
  794         }
  795         if (tlp != 0) {
  796                 lp = tlp;
  797                 len = 1;
  798                 i = 0;
  799                 lph = &lp->lc_host;
  800                 lphnext = lp->lc_morehosts;
  801                 ok = 1;
  802                 while (ok && (lph->lph_flag & LC_VALID)) {
  803                         if (nqsrv_cmpnam(slp, nam, lph)) {
  804                                 lph->lph_flag |= LC_VACATED;
  805                                 gotit++;
  806                                 break;
  807                         }
  808                         if (++i == len) {
  809                                 if (lphnext) {
  810                                         len = LC_MOREHOSTSIZ;
  811                                         i = 0;
  812                                         lph = lphnext->lpm_hosts;
  813                                         lphnext = lphnext->lpm_next;
  814                                 } else
  815                                         ok = 0;
  816                         } else
  817                                 lph++;
  818                 }
  819                 if ((lp->lc_flag & LC_EXPIREDWANTED) && gotit) {
  820                         lp->lc_flag &= ~LC_EXPIREDWANTED;
  821                         wakeup((caddr_t)&lp->lc_flag);
  822                 }
  823 nfsmout:
  824                 return (EPERM);
  825         }
  826         return (EPERM);
  827 }
  828 #endif /* NFSSERVER */
  829 
  830 #if defined(NFS) && !defined(NFS_V2_ONLY)
  831 /*
  832  * Client get lease rpc function.
  833  */
  834 int
  835 nqnfs_getlease(vp, rwflag, cred, l)
  836         struct vnode *vp;
  837         int rwflag;
  838         kauth_cred_t cred;
  839         struct lwp *l;
  840 {
  841         u_int32_t *tl;
  842         caddr_t cp;
  843         int32_t t1, t2;
  844         struct nfsnode *np;
  845         struct nfsmount *nmp = VFSTONFS(vp->v_mount);
  846         caddr_t bpos, dpos, cp2;
  847         time_t reqtime;
  848         int error = 0;
  849         struct mbuf *mreq, *mrep, *md, *mb;
  850         int cachable;
  851         u_quad_t frev;
  852 
  853         nfsstats.rpccnt[NQNFSPROC_GETLEASE]++;
  854         np = VTONFS(vp);
  855         mb = mreq = nfsm_reqh(np, NQNFSPROC_GETLEASE, NFSX_V3FH+2*NFSX_UNSIGNED,
  856                  &bpos);
  857         nfsm_fhtom(np, 1);
  858         nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
  859         *tl++ = txdr_unsigned(rwflag);
  860         *tl = txdr_unsigned(nmp->nm_leaseterm);
  861         reqtime = time_second;
  862         nfsm_request(np, NQNFSPROC_GETLEASE, l, cred);
  863         nfsm_dissect(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
  864         cachable = fxdr_unsigned(int, *tl++);
  865         reqtime += fxdr_unsigned(int, *tl++);
  866         if (reqtime > time_second) {
  867                 frev = fxdr_hyper(tl);
  868                 nqnfs_clientlease(nmp, np, rwflag, cachable, reqtime, frev);
  869                 nfsm_loadattr(vp, (struct vattr *)0, 0);
  870         } else
  871                 error = NQNFS_EXPIRED;
  872         nfsm_reqdone;
  873         return (error);
  874 }
  875 
  876 /*
  877  * Client vacated message function.
  878  */
  879 int
  880 nqnfs_vacated(vp, cred, l)
  881         struct vnode *vp;
  882         kauth_cred_t cred;
  883         struct lwp *l;
  884 {
  885         caddr_t cp;
  886         struct mbuf *m;
  887         int i;
  888         u_int32_t *tl;
  889         int32_t t2;
  890         caddr_t bpos;
  891         u_int32_t xid;
  892         int error = 0;
  893         struct mbuf *mreq, *mb, *mheadend;
  894         struct nfsmount *nmp;
  895         struct nfsreq myrep;
  896         struct nfsnode *np = VTONFS(vp);
  897 
  898         nmp = VFSTONFS(vp->v_mount);
  899         nfsstats.rpccnt[NQNFSPROC_VACATED]++;
  900         nfsm_reqhead(np, NQNFSPROC_VACATED, NFSX_FH(1));
  901         nfsm_fhtom(np, 1);
  902         m = mreq;
  903         i = 0;
  904         while (m) {
  905                 i += m->m_len;
  906                 m = m->m_next;
  907         }
  908         m = nfsm_rpchead(cred, nmp->nm_flag, NQNFSPROC_VACATED,
  909                 RPCAUTH_UNIX, 5 * NFSX_UNSIGNED, (char *)0,
  910                 0, (char *)0, mreq, i, &mheadend, &xid);
  911         if (nmp->nm_sotype == SOCK_STREAM) {
  912                 M_PREPEND(m, NFSX_UNSIGNED, M_WAIT);
  913                 *mtod(m, u_int32_t *) = htonl(0x80000000 | (m->m_pkthdr.len -
  914                         NFSX_UNSIGNED));
  915         }
  916         myrep.r_flags = 0;
  917         myrep.r_nmp = nmp;
  918         if (nmp->nm_soflags & PR_CONNREQUIRED)
  919                 (void) nfs_sndlock(&nmp->nm_iflag, (struct nfsreq *)0);
  920         (void) nfs_send(nmp->nm_so, nmp->nm_nam, m, &myrep, l);
  921         if (nmp->nm_soflags & PR_CONNREQUIRED)
  922                 nfs_sndunlock(&nmp->nm_iflag);
  923 nfsmout:
  924         return (error);
  925 }
  926 
  927 /*
  928  * Called for client side callbacks
  929  */
  930 static int
  931 nqnfs_callback1(struct nfsmount *nmp, struct mbuf *mrep, struct mbuf *md,
  932     caddr_t dpos, struct lwp *l, struct nfsrv_descript *nfsd)
  933 {
  934         struct vnode *vp;
  935         u_int32_t *tl;
  936         int32_t t1;
  937         nfsrvfh_t nsfh;
  938         fhandle_t *fhp;
  939         struct nfsnode *np;
  940         struct nfsd tnfsd;
  941         struct nfssvc_sock *slp;
  942         struct mbuf **mrq = (struct mbuf **)0, *mb, *mreq;
  943         int error = 0, cache = 0;
  944         char *cp2, *bpos;
  945         u_quad_t frev;
  946 
  947 #ifndef nolint
  948         slp = NULL;
  949 #endif
  950         nfsd->nd_mrep = mrep;
  951         nfsd->nd_md = md;
  952         nfsd->nd_dpos = dpos;
  953         error = nfs_getreq(nfsd, &tnfsd, FALSE);
  954         if (error)
  955                 return (error);
  956         md = nfsd->nd_md;
  957         dpos = nfsd->nd_dpos;
  958         if (nfsd->nd_procnum != NQNFSPROC_EVICTED) {
  959                 m_freem(mrep);
  960                 return (EPERM);
  961         }
  962         nfsm_srvmtofh(&nsfh);
  963         m_freem(mrep);
  964         fhp = &nsfh.nsfh_u.u_fh;
  965         error = nfs_nget(nmp->nm_mountp, (nfsfh_t *)fhp, NFSX_V3FH, &np);
  966         if (error)
  967                 return (error);
  968         vp = NFSTOV(np);
  969         if (CIRCLEQ_NEXT(np, n_timer) != 0) {
  970                 np->n_expiry = 0;
  971                 np->n_flag |= NQNFSEVICTED;
  972                 if (CIRCLEQ_FIRST(&nmp->nm_timerhead) != np) {
  973                         CIRCLEQ_REMOVE(&nmp->nm_timerhead, np, n_timer);
  974                         CIRCLEQ_INSERT_HEAD(&nmp->nm_timerhead, np, n_timer);
  975                 }
  976         }
  977         vput(vp);
  978         kauth_cred_free(nfsd->nd_cr);
  979         nfsm_srvdone;
  980 }
  981 
  982 int
  983 nqnfs_callback(struct nfsmount *nmp, struct mbuf *mrep, struct mbuf *md,
  984     caddr_t dpos, struct lwp *l)
  985 {
  986         struct nfsrv_descript *nd;
  987         int error;
  988 
  989         nd = nfsdreq_alloc();
  990         error = nqnfs_callback1(nmp, mrep, md, dpos, l, nd);
  991         nfsdreq_free(nd);
  992 
  993         return error;
  994 }
  995 #endif /* NFS && !NFS_V2_ONLY */
  996 
  997 #ifdef NFS /* Needed in V2_ONLY case for Kerberos stuff */
  998 /*
  999  * Nqnfs client helper daemon. Runs once a second to expire leases.
 1000  * It also get authorization strings for "kerb" mounts.
 1001  * It must start at the beginning of the list again after any potential
 1002  * "sleep" since nfs_reclaim() called from vclean() can pull a node off
 1003  * the list asynchronously.
 1004  */
 1005 int
 1006 nqnfs_clientd(struct nfsmount *nmp, kauth_cred_t cred, struct nfsd_cargs *ncd,
 1007     int flag, caddr_t argp, struct lwp *l)
 1008 {
 1009 #ifndef NFS_V2_ONLY
 1010         struct nfsnode *np;
 1011         struct vnode *vp;
 1012         struct nfsreq myrep;
 1013 #endif
 1014         int error = 0, sleepreturn;
 1015         struct nfsuid *nuidp, *nnuidp;
 1016 
 1017         /*
 1018          * First initialize some variables
 1019          */
 1020 
 1021         /*
 1022          * If an authorization string is being passed in, get it.
 1023          */
 1024         if ((flag & NFSSVC_GOTAUTH) &&
 1025             (nmp->nm_iflag & (NFSMNT_WAITAUTH | NFSMNT_DISMNT)) == 0) {
 1026             if (nmp->nm_iflag & NFSMNT_HASAUTH)
 1027                 panic("cld kerb");
 1028             if ((flag & NFSSVC_AUTHINFAIL) == 0) {
 1029                 if (ncd->ncd_authlen <= nmp->nm_authlen &&
 1030                     ncd->ncd_verflen <= nmp->nm_verflen &&
 1031                     !copyin(ncd->ncd_authstr,nmp->nm_authstr,ncd->ncd_authlen)&&
 1032                     !copyin(ncd->ncd_verfstr,nmp->nm_verfstr,ncd->ncd_verflen)){
 1033                     nmp->nm_authtype = ncd->ncd_authtype;
 1034                     nmp->nm_authlen = ncd->ncd_authlen;
 1035                     nmp->nm_verflen = ncd->ncd_verflen;
 1036 #ifdef NFSKERB
 1037                     nmp->nm_key = ncd->ncd_key;
 1038 #endif
 1039                 } else
 1040                     nmp->nm_iflag |= NFSMNT_AUTHERR;
 1041             } else
 1042                 nmp->nm_iflag |= NFSMNT_AUTHERR;
 1043             nmp->nm_iflag |= NFSMNT_HASAUTH;
 1044             wakeup((caddr_t)&nmp->nm_authlen);
 1045         } else
 1046             nmp->nm_iflag |= NFSMNT_WAITAUTH;
 1047 
 1048         /*
 1049          * Loop every second updating queue until there is a termination sig.
 1050          */
 1051         sleepreturn = 0;
 1052         while ((nmp->nm_iflag & NFSMNT_DISMNT) == 0) {
 1053             if (sleepreturn == EINTR || sleepreturn == ERESTART) {
 1054                 /*
 1055                  * XXX Freeze syncer.  Must do this before locking
 1056                  * the mount point.  See dounmount() for details.
 1057                  */
 1058                 lockmgr(&syncer_lock, LK_EXCLUSIVE, NULL);
 1059                 if (vfs_busy(nmp->nm_mountp, LK_NOWAIT, 0) != 0)
 1060                         lockmgr(&syncer_lock, LK_EXCLUSIVE, NULL);
 1061                 else if (dounmount(nmp->nm_mountp, 0, l) != 0)
 1062                         CLRSIG(l);
 1063                 sleepreturn = 0;
 1064                 continue;
 1065             }
 1066 #ifndef NFS_V2_ONLY
 1067             if (nmp->nm_flag & NFSMNT_NQNFS) {
 1068                 /*
 1069                  * If there are no outstanding requests (and therefore no
 1070                  * processes in nfs_reply) and there is data in the receive
 1071                  * queue, poke for callbacks.
 1072                  */
 1073                 if (TAILQ_EMPTY(&nfs_reqq) && nmp->nm_so &&
 1074                     nmp->nm_so->so_rcv.sb_cc > 0) {
 1075                     myrep.r_flags = R_GETONEREP;
 1076                     myrep.r_nmp = nmp;
 1077                     myrep.r_mrep = (struct mbuf *)0;
 1078                     myrep.r_lwp = (struct lwp *)0;
 1079                     (void) nfs_reply(&myrep, l);
 1080                 }
 1081 
 1082                 /*
 1083                  * Loop through the leases, updating as required.
 1084                  */
 1085                 np = CIRCLEQ_FIRST(&nmp->nm_timerhead);
 1086                 while (np != (void *)&nmp->nm_timerhead &&
 1087                        (nmp->nm_iflag & NFSMNT_DISMINPROG) == 0) {
 1088                         vp = NFSTOV(np);
 1089                         if (np->n_expiry < time_second) {
 1090                            if (vget(vp, LK_EXCLUSIVE) == 0) {
 1091                                 nmp->nm_inprog = vp;
 1092                                 CIRCLEQ_REMOVE(&nmp->nm_timerhead, np, n_timer);
 1093                                 /* mark this off the list */
 1094                                 CIRCLEQ_NEXT(np, n_timer) = 0; /* XXX */
 1095                                 if (np->n_flag & (NMODIFIED | NQNFSEVICTED)) {
 1096                                         if (np->n_flag & NQNFSEVICTED) {
 1097                                                 if (vp->v_type == VDIR)
 1098                                                         nfs_invaldircache(vp,0);
 1099                                                 cache_purge(vp);
 1100                                                 (void) nfs_vinvalbuf(vp,
 1101                                                        V_SAVE, cred, l, 0);
 1102                                                 np->n_flag &= ~NQNFSEVICTED;
 1103                                                 (void) nqnfs_vacated(vp, cred, l);
 1104                                         } else if (vp->v_type == VREG) {
 1105                                                 (void) VOP_FSYNC(vp, cred,
 1106                                                     FSYNC_WAIT, 0, 0, l);
 1107                                                 np->n_flag &= ~NMODIFIED;
 1108                                         }
 1109                                 }
 1110                                 vput(vp);
 1111                                 nmp->nm_inprog = NULLVP;
 1112                             }
 1113                         } else if ((np->n_expiry - NQ_RENEWAL) < time_second) {
 1114                             if ((np->n_flag & (NQNFSWRITE | NQNFSNONCACHE))
 1115                                  == NQNFSWRITE &&
 1116                                  !LIST_EMPTY(&vp->v_dirtyblkhd) &&
 1117                                  vget(vp, LK_EXCLUSIVE) == 0) {
 1118                                  nmp->nm_inprog = vp;
 1119                                  if (nqnfs_getlease(vp, ND_WRITE, cred, l)==0)
 1120                                         np->n_brev = np->n_lrev;
 1121                                  vput(vp);
 1122                                  nmp->nm_inprog = NULLVP;
 1123                             }
 1124                         } else
 1125                                 break;
 1126                         if (np == CIRCLEQ_FIRST(&nmp->nm_timerhead))
 1127                                 break;
 1128                         np = CIRCLEQ_FIRST(&nmp->nm_timerhead);
 1129                 }
 1130             }
 1131 #endif /* !NFS_V2_ONLY */
 1132 
 1133             /*
 1134              * Get an authorization string, if required.
 1135              */
 1136             if ((nmp->nm_iflag & (NFSMNT_WAITAUTH | NFSMNT_DISMNT | NFSMNT_HASAUTH)) == 0) {
 1137                 ncd->ncd_authuid = nmp->nm_authuid;
 1138                 if (copyout((caddr_t)ncd, argp, sizeof (struct nfsd_cargs)))
 1139                         nmp->nm_iflag |= NFSMNT_WAITAUTH;
 1140                 else
 1141                         return (ENEEDAUTH);
 1142             }
 1143 
 1144             /*
 1145              * Wait a bit (no pun) and do it again.
 1146              */
 1147             if ((nmp->nm_iflag & NFSMNT_DISMNT) == 0 &&
 1148                 (nmp->nm_iflag & (NFSMNT_WAITAUTH | NFSMNT_HASAUTH))) {
 1149                     sleepreturn = tsleep((caddr_t)&nmp->nm_authstr,
 1150                         PSOCK | PCATCH, "nqnfstimr", hz / 3);
 1151             }
 1152         }
 1153 
 1154         /*
 1155          * Finally, we can free up the mount structure.
 1156          */
 1157         for (nuidp = TAILQ_FIRST(&nmp->nm_uidlruhead); nuidp != 0;
 1158             nuidp = nnuidp) {
 1159                 nnuidp = TAILQ_NEXT(nuidp, nu_lru);
 1160                 LIST_REMOVE(nuidp, nu_hash);
 1161                 TAILQ_REMOVE(&nmp->nm_uidlruhead, nuidp, nu_lru);
 1162                 free((caddr_t)nuidp, M_NFSUID);
 1163         }
 1164         free((caddr_t)nmp, M_NFSMNT);
 1165         return (error);
 1166 }
 1167 #endif /* NFS */
 1168 
 1169 #if defined(NFS) && !defined(NFS_V2_ONLY)
 1170 /*
 1171  * Update a client lease.
 1172  */
 1173 void
 1174 nqnfs_clientlease(nmp, np, rwflag, cachable, expiry, frev)
 1175         struct nfsmount *nmp;
 1176         struct nfsnode *np;
 1177         int rwflag, cachable;
 1178         time_t expiry;
 1179         u_quad_t frev;
 1180 {
 1181         struct nfsnode *tp;
 1182 
 1183         if (CIRCLEQ_NEXT(np, n_timer) != 0) {
 1184                 CIRCLEQ_REMOVE(&nmp->nm_timerhead, np, n_timer);
 1185                 if (rwflag == ND_WRITE)
 1186                         np->n_flag |= NQNFSWRITE;
 1187         } else if (rwflag == ND_READ)
 1188                 np->n_flag &= ~NQNFSWRITE;
 1189         else
 1190                 np->n_flag |= NQNFSWRITE;
 1191         if (cachable)
 1192                 np->n_flag &= ~NQNFSNONCACHE;
 1193         else
 1194                 np->n_flag |= NQNFSNONCACHE;
 1195         np->n_expiry = expiry;
 1196         np->n_lrev = frev;
 1197         tp = nmp->nm_timerhead.cqh_last;
 1198         while (tp != (void *)&nmp->nm_timerhead && tp->n_expiry > np->n_expiry)
 1199                 tp = tp->n_timer.cqe_prev;
 1200         if (tp == (void *)&nmp->nm_timerhead) {
 1201                 CIRCLEQ_INSERT_HEAD(&nmp->nm_timerhead, np, n_timer);
 1202         } else {
 1203                 CIRCLEQ_INSERT_AFTER(&nmp->nm_timerhead, tp, np, n_timer);
 1204         }
 1205 }
 1206 #endif /* NFS && !NFS_V2_ONLY */
 1207 
 1208 #if defined(NFSSERVER) || (defined(NFS) && !defined(NFS_V2_ONLY))
 1209 /*
 1210  * Adjust all timer queue expiry times when the time of day clock is changed.
 1211  * Called from the settimeofday() syscall.
 1212  */
 1213 void
 1214 nqnfs_lease_updatetime(deltat)
 1215         int deltat;
 1216 {
 1217         struct nqlease *lp;
 1218         struct nfsnode *np;
 1219         struct mount *mp;
 1220         struct nfsmount *nmp;
 1221         int s;
 1222         struct mount *nxtmp;
 1223 
 1224         if (nqnfsstarttime != 0)
 1225                 nqnfsstarttime += deltat;
 1226         s = splsoftnet();
 1227         CIRCLEQ_FOREACH(lp, &nqtimerhead, lc_timer) {
 1228                 lp->lc_expiry += deltat;
 1229         }
 1230         splx(s);
 1231 
 1232         /*
 1233          * Search the mount list for all nqnfs mounts and do their timer
 1234          * queues.
 1235          */
 1236         simple_lock(&mountlist_slock);
 1237         for (mp = CIRCLEQ_FIRST(&mountlist); mp != (void *)&mountlist;
 1238             mp = nxtmp) {
 1239                 if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock)) {
 1240                         nxtmp = CIRCLEQ_NEXT(mp, mnt_list);
 1241                         continue;
 1242                 }
 1243                 if (!strncmp(&mp->mnt_stat.f_fstypename[0], MOUNT_NFS,
 1244                     MFSNAMELEN)) {
 1245                         nmp = VFSTONFS(mp);
 1246                         if (nmp->nm_flag & NFSMNT_NQNFS) {
 1247                                 CIRCLEQ_FOREACH(np, &nmp->nm_timerhead,
 1248                                     n_timer) {
 1249                                         np->n_expiry += deltat;
 1250                                 }
 1251                         }
 1252                 }
 1253                 simple_lock(&mountlist_slock);
 1254                 nxtmp = CIRCLEQ_NEXT(mp, mnt_list);
 1255                 vfs_unbusy(mp);
 1256         }
 1257         simple_unlock(&mountlist_slock);
 1258 }
 1259 
 1260 /*
 1261  * Lock a server lease.
 1262  */
 1263 void
 1264 nqsrv_locklease(lp)
 1265         struct nqlease *lp;
 1266 {
 1267 
 1268         while (lp->lc_flag & LC_LOCKED) {
 1269                 lp->lc_flag |= LC_WANTED;
 1270                 (void) tsleep((caddr_t)lp, PSOCK, "nqlc", 0);
 1271         }
 1272         lp->lc_flag |= LC_LOCKED;
 1273         lp->lc_flag &= ~LC_WANTED;
 1274 }
 1275 
 1276 /*
 1277  * Unlock a server lease.
 1278  */
 1279 void
 1280 nqsrv_unlocklease(lp)
 1281         struct nqlease *lp;
 1282 {
 1283 
 1284         lp->lc_flag &= ~LC_LOCKED;
 1285         if (lp->lc_flag & LC_WANTED)
 1286                 wakeup((caddr_t)lp);
 1287 }
 1288 #endif /* NFSSERVER || NFS && !NFS_V2_ONLY */

Cache object: 0a97d41743382be224cad2f9072b6d19


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