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