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

Cache object: 949e6216f059e92c30cac35043c9f543


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