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