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

Cache object: db3e24179f84b19d14ea6b86c7168b8b


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