proc->thread stage 5: BUF/VFS clearance! Remove the ucred argument from
[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.4 2003/06/26 05:55:18 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, td, nam, cachablep, frev, cred)
174         struct vnode *vp;
175         u_int32_t *duration;
176         int flags;
177         struct nfssvc_sock *slp;
178         struct thread *td;
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, td);
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(struct vnode *vp, struct thread *td,
341         struct ucred *cred, int flag)
342 {
343         u_int32_t duration = 0;
344         int cache;
345         u_quad_t frev;
346
347         (void) nqsrv_getlease(vp, &duration, ND_CHECK | flag, NQLOCALSLP,
348                 td, (struct sockaddr *)0, &cache, &frev, cred);
349 }
350
351 int
352 nqnfs_vop_lease_check(ap)
353         struct vop_lease_args /* {
354                 struct vnode *a_vp;
355                 struct thread *a_td;
356                 struct ucred *a_cred;
357                 int a_flag;
358         } */ *ap;
359 {
360         u_int32_t duration = 0;
361         int cache;
362         u_quad_t frev;
363
364         (void) nqsrv_getlease(ap->a_vp, &duration, ND_CHECK | ap->a_flag,
365                               NQLOCALSLP, ap->a_td, (struct sockaddr *)0,
366                               &cache, &frev, ap->a_cred);
367         return (0);
368 }
369
370
371 /*
372  * Add a host to an nqhost structure for a lease.
373  */
374 static void
375 nqsrv_addhost(lph, slp, nam)
376         register struct nqhost *lph;
377         struct nfssvc_sock *slp;
378         struct sockaddr *nam;
379 {
380         struct sockaddr_in *saddr;
381         struct socket *nsso;
382
383         if (slp == NQLOCALSLP) {
384                 lph->lph_flag |= (LC_VALID | LC_LOCAL);
385                 return;
386         }
387         nsso = slp->ns_so;
388         lph->lph_slp = slp;
389         if (nsso && nsso->so_proto->pr_protocol == IPPROTO_UDP) {
390                 saddr = (struct sockaddr_in *)nam;
391                 lph->lph_flag |= (LC_VALID | LC_UDP);
392                 lph->lph_inetaddr = saddr->sin_addr.s_addr;
393                 lph->lph_port = saddr->sin_port;
394         } else {
395                 lph->lph_flag |= (LC_VALID | LC_SREF);
396                 slp->ns_sref++;
397         }
398 }
399
400 /*
401  * Update the lease expiry time and position it in the timer queue correctly.
402  */
403 static void
404 nqsrv_instimeq(lp, duration)
405         register struct nqlease *lp;
406         u_int32_t duration;
407 {
408         register struct nqlease *tlp;
409         time_t newexpiry;
410
411         newexpiry = time_second + duration + nqsrv_clockskew;
412         if (lp->lc_expiry == newexpiry)
413                 return;
414         if (lp->lc_timer.cqe_next != 0) {
415                 CIRCLEQ_REMOVE(&nqtimerhead, lp, lc_timer);
416         }
417         lp->lc_expiry = newexpiry;
418
419         /*
420          * Find where in the queue it should be.
421          */
422         tlp = nqtimerhead.cqh_last;
423         while (tlp != (void *)&nqtimerhead && tlp->lc_expiry > newexpiry)
424                 tlp = tlp->lc_timer.cqe_prev;
425 #ifdef HASNVRAM
426         if (tlp == nqtimerhead.cqh_last)
427                 NQSTORENOVRAM(newexpiry);
428 #endif /* HASNVRAM */
429         if (tlp == (void *)&nqtimerhead) {
430                 CIRCLEQ_INSERT_HEAD(&nqtimerhead, lp, lc_timer);
431         } else {
432                 CIRCLEQ_INSERT_AFTER(&nqtimerhead, tlp, lp, lc_timer);
433         }
434 }
435
436 /*
437  * Compare the requesting host address with the lph entry in the lease.
438  * Return true iff it is the same.
439  * This is somewhat messy due to the union in the nqhost structure.
440  * The local host is indicated by the special value of NQLOCALSLP for slp.
441  */
442 static int
443 nqsrv_cmpnam(slp, nam, lph)
444         register struct nfssvc_sock *slp;
445         struct sockaddr *nam;
446         register struct nqhost *lph;
447 {
448         register struct sockaddr_in *saddr;
449         struct sockaddr *addr;
450         union nethostaddr lhaddr;
451         struct socket *nsso;
452         int ret;
453
454         if (slp == NQLOCALSLP) {
455                 if (lph->lph_flag & LC_LOCAL)
456                         return (1);
457                 else
458                         return (0);
459         }
460         nsso = slp->ns_so;
461         if (nsso && nsso->so_proto->pr_protocol == IPPROTO_UDP) {
462                 addr = nam;
463         } else {
464                 addr = slp->ns_nam;
465         }
466         if (lph->lph_flag & LC_UDP) {
467                 ret = netaddr_match(AF_INET, &lph->lph_haddr, addr);
468         } else {
469                 if ((lph->lph_slp->ns_flag & SLP_VALID) == 0)
470                         return (0);
471                 saddr = (struct sockaddr_in *)lph->lph_slp->ns_nam;
472                 if (saddr->sin_family == AF_INET)
473                         lhaddr.had_inetaddr = saddr->sin_addr.s_addr;
474                 else
475                         lhaddr.had_nam = lph->lph_slp->ns_nam;
476                 ret = netaddr_match(saddr->sin_family, &lhaddr, addr);
477         }
478         return (ret);
479 }
480
481 /*
482  * Send out eviction notice messages to all other hosts for the lease.
483  */
484 static void
485 nqsrv_send_eviction(vp, lp, slp, nam, cred)
486         struct vnode *vp;
487         register struct nqlease *lp;
488         struct nfssvc_sock *slp;
489         struct sockaddr *nam;
490         struct ucred *cred;
491 {
492         register struct nqhost *lph = &lp->lc_host;
493         register int siz;
494         struct nqm *lphnext = lp->lc_morehosts;
495         struct mbuf *m, *mreq, *mb, *mb2, *mheadend;
496         struct sockaddr *nam2;
497         struct sockaddr_in *saddr;
498         nfsfh_t nfh;
499         fhandle_t *fhp;
500         caddr_t bpos, cp;
501         u_int32_t xid, *tl;
502         int len = 1, ok = 1, i = 0;
503
504         while (ok && (lph->lph_flag & LC_VALID)) {
505                 if (nqsrv_cmpnam(slp, nam, lph)) {
506                         lph->lph_flag |= LC_VACATED;
507                 } else if ((lph->lph_flag & (LC_LOCAL | LC_VACATED)) == 0) {
508                         struct socket *so;
509                         int sotype;
510                         int *solockp = NULL;
511
512                         so = lph->lph_slp->ns_so;
513                         if (lph->lph_flag & LC_UDP) {
514                                 MALLOC(nam2, struct sockaddr *,
515                                        sizeof *nam2, M_SONAME, M_WAITOK);
516                                 saddr = (struct sockaddr_in *)nam2;
517                                 saddr->sin_len = sizeof *saddr;
518                                 saddr->sin_family = AF_INET;
519                                 saddr->sin_addr.s_addr = lph->lph_inetaddr;
520                                 saddr->sin_port = lph->lph_port;
521                         } else if (lph->lph_slp->ns_flag & SLP_VALID) {
522                                 nam2 = (struct sockaddr *)0;
523                         } else {
524                                 goto nextone;
525                         }
526                         sotype = so->so_type;
527                         if (so->so_proto->pr_flags & PR_CONNREQUIRED)
528                                 solockp = &lph->lph_slp->ns_solock;
529                         nfsm_reqhead((struct vnode *)0, NQNFSPROC_EVICTED,
530                                 NFSX_V3FH + NFSX_UNSIGNED);
531                         fhp = &nfh.fh_generic;
532                         bzero((caddr_t)fhp, sizeof(nfh));
533                         fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
534                         VFS_VPTOFH(vp, &fhp->fh_fid);
535                         nfsm_srvfhtom(fhp, 1);
536                         m = mreq;
537                         siz = 0;
538                         while (m) {
539                                 siz += m->m_len;
540                                 m = m->m_next;
541                         }
542                         if (siz <= 0 || siz > NFS_MAXPACKET) {
543                                 printf("mbuf siz=%d\n",siz);
544                                 panic("Bad nfs svc reply");
545                         }
546                         m = nfsm_rpchead(cred, (NFSMNT_NFSV3 | NFSMNT_NQNFS),
547                                 NQNFSPROC_EVICTED,
548                                 RPCAUTH_UNIX, 5 * NFSX_UNSIGNED, (char *)0,
549                                 0, (char *)NULL, mreq, siz, &mheadend, &xid);
550                         /*
551                          * For stream protocols, prepend a Sun RPC
552                          * Record Mark.
553                          */
554                         if (sotype == SOCK_STREAM) {
555                                 M_PREPEND(m, NFSX_UNSIGNED, M_WAIT);
556                                 *mtod(m, u_int32_t *) = htonl(0x80000000 |
557                                         (m->m_pkthdr.len - NFSX_UNSIGNED));
558                         }
559                         /*
560                          * nfs_sndlock if PR_CONNREQUIRED XXX
561                          */
562
563                         if ((lph->lph_flag & LC_UDP) == 0 &&
564                             ((lph->lph_slp->ns_flag & SLP_VALID) == 0 ||
565                             nfs_slplock(lph->lph_slp, 0) == 0)) {
566                                 m_freem(m);
567                         } else {
568                                 (void) nfs_send(so, nam2, m,
569                                                 (struct nfsreq *)0);
570                                 if (solockp)
571                                         nfs_slpunlock(lph->lph_slp);
572                         }
573                         if (lph->lph_flag & LC_UDP)
574                                 FREE(nam2, M_SONAME);
575                 }
576 nextone:
577                 if (++i == len) {
578                         if (lphnext) {
579                                 i = 0;
580                                 len = LC_MOREHOSTSIZ;
581                                 lph = lphnext->lpm_hosts;
582                                 lphnext = lphnext->lpm_next;
583                         } else
584                                 ok = 0;
585                 } else
586                         lph++;
587         }
588 }
589
590 /*
591  * Wait for the lease to expire.
592  * This will occur when all clients have sent "vacated" messages to
593  * this server OR when it expires do to timeout.
594  */
595 static void
596 nqsrv_waitfor_expiry(lp)
597         register struct nqlease *lp;
598 {
599         register struct nqhost *lph;
600         register int i;
601         struct nqm *lphnext;
602         int len, ok;
603
604 tryagain:
605         if (time_second > lp->lc_expiry)
606                 return;
607         lph = &lp->lc_host;
608         lphnext = lp->lc_morehosts;
609         len = 1;
610         i = 0;
611         ok = 1;
612         while (ok && (lph->lph_flag & LC_VALID)) {
613                 if ((lph->lph_flag & (LC_LOCAL | LC_VACATED)) == 0) {
614                         lp->lc_flag |= LC_EXPIREDWANTED;
615                         (void) tsleep((caddr_t)&lp->lc_flag, PSOCK,
616                                         "nqexp", 0);
617                         goto tryagain;
618                 }
619                 if (++i == len) {
620                         if (lphnext) {
621                                 i = 0;
622                                 len = LC_MOREHOSTSIZ;
623                                 lph = lphnext->lpm_hosts;
624                                 lphnext = lphnext->lpm_next;
625                         } else
626                                 ok = 0;
627                 } else
628                         lph++;
629         }
630 }
631
632 /*
633  * Nqnfs server timer that maintains the server lease queue.
634  * Scan the lease queue for expired entries:
635  * - when one is found, wakeup anyone waiting for it
636  *   else dequeue and free
637  */
638 void
639 nqnfs_serverd()
640 {
641         register struct nqlease *lp;
642         register struct nqhost *lph;
643         struct nqlease *nextlp;
644         struct nqm *lphnext, *olphnext;
645         int i, len, ok;
646
647         for (lp = nqtimerhead.cqh_first; lp != (void *)&nqtimerhead;
648             lp = nextlp) {
649                 if (lp->lc_expiry >= time_second)
650                         break;
651                 nextlp = lp->lc_timer.cqe_next;
652                 if (lp->lc_flag & LC_EXPIREDWANTED) {
653                         lp->lc_flag &= ~LC_EXPIREDWANTED;
654                         wakeup((caddr_t)&lp->lc_flag);
655                 } else if ((lp->lc_flag & (LC_LOCKED | LC_WANTED)) == 0) {
656                     /*
657                      * Make a best effort at keeping a write caching lease long
658                      * enough by not deleting it until it has been explicitly
659                      * vacated or there have been no writes in the previous
660                      * write_slack seconds since expiry and the nfsds are not
661                      * all busy. The assumption is that if the nfsds are not
662                      * all busy now (no queue of nfs requests), then the client
663                      * would have been able to do at least one write to the
664                      * file during the last write_slack seconds if it was still
665                      * trying to push writes to the server.
666                      */
667                     if ((lp->lc_flag & (LC_WRITE | LC_VACATED)) == LC_WRITE &&
668                         ((lp->lc_flag & LC_WRITTEN) || nfsd_waiting == 0)) {
669                         lp->lc_flag &= ~LC_WRITTEN;
670                         nqsrv_instimeq(lp, nqsrv_writeslack);
671                     } else {
672                         CIRCLEQ_REMOVE(&nqtimerhead, lp, lc_timer);
673                         LIST_REMOVE(lp, lc_hash);
674                         /*
675                          * This soft reference may no longer be valid, but
676                          * no harm done. The worst case is if the vnode was
677                          * recycled and has another valid lease reference,
678                          * which is dereferenced prematurely.
679                          */
680                         lp->lc_vp->v_lease = (struct nqlease *)0;
681                         lph = &lp->lc_host;
682                         lphnext = lp->lc_morehosts;
683                         olphnext = (struct nqm *)0;
684                         len = 1;
685                         i = 0;
686                         ok = 1;
687                         while (ok && (lph->lph_flag & LC_VALID)) {
688                                 if (lph->lph_flag & LC_SREF)
689                                         nfsrv_slpderef(lph->lph_slp);
690                                 if (++i == len) {
691                                         if (olphnext) {
692                                                 free((caddr_t)olphnext, M_NQMHOST);
693                                                 olphnext = (struct nqm *)0;
694                                         }
695                                         if (lphnext) {
696                                                 olphnext = lphnext;
697                                                 i = 0;
698                                                 len = LC_MOREHOSTSIZ;
699                                                 lph = lphnext->lpm_hosts;
700                                                 lphnext = lphnext->lpm_next;
701                                         } else
702                                                 ok = 0;
703                                 } else
704                                         lph++;
705                         }
706                         FREE((caddr_t)lp, M_NQLEASE);
707                         if (olphnext)
708                                 free((caddr_t)olphnext, M_NQMHOST);
709                         nfsstats.srvnqnfs_leases--;
710                     }
711                 }
712         }
713 }
714
715 /*
716  * Called from nfssvc_nfsd() for a getlease rpc request.
717  * Do the from/to xdr translation and call nqsrv_getlease() to
718  * do the real work.
719  */
720 int
721 nqnfsrv_getlease(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
722         struct thread *td, struct mbuf **mrq)
723 {
724         struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
725         struct sockaddr *nam = nfsd->nd_nam;
726         caddr_t dpos = nfsd->nd_dpos;
727         struct ucred *cred = &nfsd->nd_cr;
728         register struct nfs_fattr *fp;
729         struct vattr va;
730         register struct vattr *vap = &va;
731         struct vnode *vp;
732         nfsfh_t nfh;
733         fhandle_t *fhp;
734         register u_int32_t *tl;
735         register int32_t t1;
736         u_quad_t frev;
737         caddr_t bpos;
738         int error = 0;
739         char *cp2;
740         struct mbuf *mb, *mb2, *mreq;
741         int flags, rdonly, cache;
742
743         fhp = &nfh.fh_generic;
744         nfsm_srvmtofh(fhp);
745         nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
746         flags = fxdr_unsigned(int, *tl++);
747         nfsd->nd_duration = fxdr_unsigned(int, *tl);
748         error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly,
749                 (nfsd->nd_flag & ND_KERBAUTH), TRUE);
750         if (error) {
751                 nfsm_reply(0);
752                 goto nfsmout;
753         }
754         if (rdonly && flags == ND_WRITE) {
755                 error = EROFS;
756                 vput(vp);
757                 nfsm_reply(0);
758         }
759         (void) nqsrv_getlease(vp, &nfsd->nd_duration, flags, slp, td,
760                 nam, &cache, &frev, cred);
761         error = VOP_GETATTR(vp, vap, td);
762         vput(vp);
763         nfsm_reply(NFSX_V3FATTR + 4 * NFSX_UNSIGNED);
764         nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
765         *tl++ = txdr_unsigned(cache);
766         *tl++ = txdr_unsigned(nfsd->nd_duration);
767         txdr_hyper(frev, tl);
768         nfsm_build(fp, struct nfs_fattr *, NFSX_V3FATTR);
769         nfsm_srvfillattr(vap, fp);
770         nfsm_srvdone;
771 }
772
773 /*
774  * Called from nfssvc_nfsd() when a "vacated" message is received from a
775  * client. Find the entry and expire it.
776  */
777 int
778 nqnfsrv_vacated(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
779         struct thread *td, struct mbuf **mrq)
780 {
781         struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
782         struct sockaddr *nam = nfsd->nd_nam;
783         caddr_t dpos = nfsd->nd_dpos;
784         register struct nqlease *lp;
785         register struct nqhost *lph;
786         struct nqlease *tlp = (struct nqlease *)0;
787         nfsfh_t nfh;
788         fhandle_t *fhp;
789         register u_int32_t *tl;
790         register int32_t t1;
791         struct nqm *lphnext;
792         struct mbuf *mreq, *mb;
793         int error = 0, i, len, ok, gotit = 0, cache = 0;
794         char *cp2, *bpos;
795         u_quad_t frev;
796
797         fhp = &nfh.fh_generic;
798         nfsm_srvmtofh(fhp);
799         m_freem(mrep);
800         /*
801          * Find the lease by searching the hash list.
802          */
803         for (lp = NQFHHASH(fhp->fh_fid.fid_data)->lh_first; lp != 0;
804             lp = lp->lc_hash.le_next)
805                 if (fhp->fh_fsid.val[0] == lp->lc_fsid.val[0] &&
806                     fhp->fh_fsid.val[1] == lp->lc_fsid.val[1] &&
807                     !bcmp(fhp->fh_fid.fid_data, lp->lc_fiddata,
808                           MAXFIDSZ)) {
809                         /* Found it */
810                         tlp = lp;
811                         break;
812                 }
813         if (tlp != 0) {
814                 lp = tlp;
815                 len = 1;
816                 i = 0;
817                 lph = &lp->lc_host;
818                 lphnext = lp->lc_morehosts;
819                 ok = 1;
820                 while (ok && (lph->lph_flag & LC_VALID)) {
821                         if (nqsrv_cmpnam(slp, nam, lph)) {
822                                 lph->lph_flag |= LC_VACATED;
823                                 gotit++;
824                                 break;
825                         }
826                         if (++i == len) {
827                                 if (lphnext) {
828                                         len = LC_MOREHOSTSIZ;
829                                         i = 0;
830                                         lph = lphnext->lpm_hosts;
831                                         lphnext = lphnext->lpm_next;
832                                 } else
833                                         ok = 0;
834                         } else
835                                 lph++;
836                 }
837                 if ((lp->lc_flag & LC_EXPIREDWANTED) && gotit) {
838                         lp->lc_flag &= ~LC_EXPIREDWANTED;
839                         wakeup((caddr_t)&lp->lc_flag);
840                 }
841 nfsmout:
842                 return (EPERM);
843         }
844         return (EPERM);
845 }
846
847 #endif /* NFS_NOSERVER */
848
849 /*
850  * Client get lease rpc function.
851  */
852 int
853 nqnfs_getlease(struct vnode *vp, int rwflag, struct thread *td)
854 {
855         u_int32_t *tl;
856         caddr_t cp;
857         int32_t t1, t2;
858         struct nfsnode *np;
859         struct nfsmount *nmp = VFSTONFS(vp->v_mount);
860         caddr_t bpos, dpos, cp2;
861         time_t reqtime;
862         int error = 0;
863         struct mbuf *mreq, *mrep, *md, *mb, *mb2;
864         int cachable;
865         u_quad_t frev;
866
867         nfsstats.rpccnt[NQNFSPROC_GETLEASE]++;
868         mb = mreq = nfsm_reqh(vp, NQNFSPROC_GETLEASE, NFSX_V3FH+2*NFSX_UNSIGNED,
869                  &bpos);
870         nfsm_fhtom(vp, 1);
871         nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
872         *tl++ = txdr_unsigned(rwflag);
873         *tl = txdr_unsigned(nmp->nm_leaseterm);
874         reqtime = time_second;
875         nfsm_request(vp, NQNFSPROC_GETLEASE, td, NFSVPCRED(vp));
876         np = VTONFS(vp);
877         nfsm_dissect(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
878         cachable = fxdr_unsigned(int, *tl++);
879         reqtime += fxdr_unsigned(int, *tl++);
880         if (reqtime > time_second) {
881                 frev = fxdr_hyper(tl);
882                 nqnfs_clientlease(nmp, np, rwflag, cachable, reqtime, frev);
883                 nfsm_loadattr(vp, (struct vattr *)0);
884         } else
885                 error = NQNFS_EXPIRED;
886         nfsm_reqdone;
887         return (error);
888 }
889
890 #ifndef NFS_NOSERVER 
891 /*
892  * Client vacated message function.
893  */
894 static int
895 nqnfs_vacated(vp, cred)
896         register struct vnode *vp;
897         struct ucred *cred;
898 {
899         register caddr_t cp;
900         register int i;
901         register u_int32_t *tl;
902         register int32_t t2;
903         caddr_t bpos;
904         u_int32_t xid;
905         int error = 0;
906         struct mbuf *m, *mreq, *mb, *mb2, *mheadend;
907         struct nfsmount *nmp;
908         struct nfsreq myrep;
909
910         nmp = VFSTONFS(vp->v_mount);
911         nfsstats.rpccnt[NQNFSPROC_VACATED]++;
912         nfsm_reqhead(vp, NQNFSPROC_VACATED, NFSX_FH(1));
913         nfsm_fhtom(vp, 1);
914         m = mreq;
915         i = 0;
916         while (m) {
917                 i += m->m_len;
918                 m = m->m_next;
919         }
920         m = nfsm_rpchead(cred, nmp->nm_flag, NQNFSPROC_VACATED,
921                 RPCAUTH_UNIX, 5 * NFSX_UNSIGNED, (char *)0,
922                 0, (char *)NULL, mreq, i, &mheadend, &xid);
923         if (nmp->nm_sotype == SOCK_STREAM) {
924                 M_PREPEND(m, NFSX_UNSIGNED, M_WAIT);
925                 *mtod(m, u_int32_t *) = htonl(0x80000000 | (m->m_pkthdr.len -
926                         NFSX_UNSIGNED));
927         }
928         myrep.r_flags = 0;
929         myrep.r_nmp = nmp;
930         if (nmp->nm_soflags & PR_CONNREQUIRED)
931                 (void) nfs_sndlock(&myrep);
932         (void) nfs_send(nmp->nm_so, nmp->nm_nam, m, &myrep);
933         if (nmp->nm_soflags & PR_CONNREQUIRED)
934                 nfs_sndunlock(&myrep);
935 nfsmout:
936         return (error);
937 }
938
939 /*
940  * Called for client side callbacks
941  */
942 int
943 nqnfs_callback(nmp, mrep, md, dpos)
944         struct nfsmount *nmp;
945         struct mbuf *mrep, *md;
946         caddr_t dpos;
947 {
948         register struct vnode *vp;
949         register u_int32_t *tl;
950         register int32_t t1;
951         nfsfh_t nfh;
952         fhandle_t *fhp;
953         struct nfsnode *np;
954         struct nfsd tnfsd;
955         struct nfssvc_sock *slp;
956         struct nfsrv_descript ndesc;
957         register struct nfsrv_descript *nfsd = &ndesc;
958         struct mbuf **mrq = (struct mbuf **)0, *mb, *mreq;
959         int error = 0, cache = 0;
960         char *cp2, *bpos;
961         u_quad_t frev;
962
963 #ifndef nolint
964         slp = NULL;
965 #endif
966         nfsd->nd_mrep = mrep;
967         nfsd->nd_md = md;
968         nfsd->nd_dpos = dpos;
969         error = nfs_getreq(nfsd, &tnfsd, FALSE);
970         if (error)
971                 return (error);
972         md = nfsd->nd_md;
973         dpos = nfsd->nd_dpos;
974         if (nfsd->nd_procnum != NQNFSPROC_EVICTED) {
975                 m_freem(mrep);
976                 return (EPERM);
977         }
978         fhp = &nfh.fh_generic;
979         nfsm_srvmtofh(fhp);
980         m_freem(mrep);
981         error = nfs_nget(nmp->nm_mountp, (nfsfh_t *)fhp, NFSX_V3FH, &np);
982         if (error)
983                 return (error);
984         vp = NFSTOV(np);
985         if (np->n_timer.cqe_next != 0) {
986                 np->n_expiry = 0;
987                 np->n_flag |= NQNFSEVICTED;
988                 if (nmp->nm_timerhead.cqh_first != np) {
989                         CIRCLEQ_REMOVE(&nmp->nm_timerhead, np, n_timer);
990                         CIRCLEQ_INSERT_HEAD(&nmp->nm_timerhead, np, n_timer);
991                 }
992         }
993         vput(vp);
994         nfsm_srvdone;
995 }
996
997
998 /*
999  * Nqnfs client helper daemon. Runs once a second to expire leases.
1000  * It also get authorization strings for "kerb" mounts.
1001  * It must start at the beginning of the list again after any potential
1002  * "sleep" since nfs_reclaim() called from vclean() can pull a node off
1003  * the list asynchronously.
1004  */
1005 int
1006 nqnfs_clientd(nmp, cred, ncd, flag, argp, td)
1007         struct nfsmount *nmp;
1008         struct ucred *cred;
1009         struct nfsd_cargs *ncd;
1010         int flag;
1011         caddr_t argp;
1012         struct thread *td;
1013 {
1014         struct nfsnode *np;
1015         struct vnode *vp;
1016         struct nfsreq myrep;
1017         struct nfsuid *nuidp, *nnuidp;
1018         int error = 0, vpid;
1019
1020         /*
1021          * First initialize some variables
1022          */
1023
1024         /*
1025          * If an authorization string is being passed in, get it.
1026          */
1027         if ((flag & NFSSVC_GOTAUTH) &&
1028             (nmp->nm_state & (NFSSTA_WAITAUTH | NFSSTA_DISMNT)) == 0) {
1029             if (nmp->nm_state & NFSSTA_HASAUTH)
1030                 panic("cld kerb");
1031             if ((flag & NFSSVC_AUTHINFAIL) == 0) {
1032                 if (ncd->ncd_authlen <= nmp->nm_authlen &&
1033                     ncd->ncd_verflen <= nmp->nm_verflen &&
1034                     !copyin(ncd->ncd_authstr,nmp->nm_authstr,ncd->ncd_authlen)&&
1035                     !copyin(ncd->ncd_verfstr,nmp->nm_verfstr,ncd->ncd_verflen)){
1036                     nmp->nm_authtype = ncd->ncd_authtype;
1037                     nmp->nm_authlen = ncd->ncd_authlen;
1038                     nmp->nm_verflen = ncd->ncd_verflen;
1039 #ifdef NFSKERB
1040                     nmp->nm_key = ncd->ncd_key;
1041 #endif
1042                 } else
1043                     nmp->nm_state |= NFSSTA_AUTHERR;
1044             } else
1045                 nmp->nm_state |= NFSSTA_AUTHERR;
1046             nmp->nm_state |= NFSSTA_HASAUTH;
1047             wakeup((caddr_t)&nmp->nm_authlen);
1048         } else
1049             nmp->nm_state |= NFSSTA_WAITAUTH;
1050
1051         /*
1052          * Loop every second updating queue until there is a termination sig.
1053          */
1054         while ((nmp->nm_state & NFSSTA_DISMNT) == 0) {
1055             if (nmp->nm_flag & NFSMNT_NQNFS) {
1056                 /*
1057                  * If there are no outstanding requests (and therefore no
1058                  * processes in nfs_reply) and there is data in the receive
1059                  * queue, poke for callbacks.
1060                  */
1061                 if (nfs_reqq.tqh_first == 0 && nmp->nm_so &&
1062                     nmp->nm_so->so_rcv.sb_cc > 0) {
1063                     myrep.r_flags = R_GETONEREP;
1064                     myrep.r_nmp = nmp;
1065                     myrep.r_mrep = (struct mbuf *)0;
1066                     myrep.r_td = NULL;
1067                     (void) nfs_reply(&myrep);
1068                 }
1069
1070                 /*
1071                  * Loop through the leases, updating as required.
1072                  */
1073                 np = nmp->nm_timerhead.cqh_first;
1074                 while (np != (void *)&nmp->nm_timerhead &&
1075                        (nmp->nm_state & NFSSTA_DISMINPROG) == 0) {
1076                         vp = NFSTOV(np);
1077                         vpid = vp->v_id;
1078                         if (np->n_expiry < time_second) {
1079                            if (vget(vp, LK_EXCLUSIVE, td) == 0) {
1080                              nmp->nm_inprog = vp;
1081                              if (vpid == vp->v_id) {
1082                                 CIRCLEQ_REMOVE(&nmp->nm_timerhead, np, n_timer);
1083                                 np->n_timer.cqe_next = 0;
1084                                 if (np->n_flag & (NMODIFIED | NQNFSEVICTED)) {
1085                                         if (np->n_flag & NQNFSEVICTED) {
1086                                                 if (vp->v_type == VDIR)
1087                                                         nfs_invaldir(vp);
1088                                                 cache_purge(vp);
1089                                                 (void) nfs_vinvalbuf(vp,
1090                                                        V_SAVE, td, 0);
1091                                                 np->n_flag &= ~NQNFSEVICTED;
1092                                                 (void) nqnfs_vacated(vp, cred);
1093                                         } else if (vp->v_type == VREG) {
1094                                                 (void) VOP_FSYNC(vp, MNT_WAIT, td);
1095                                                 np->n_flag &= ~NMODIFIED;
1096                                         }
1097                                 }
1098                               }
1099                               vput(vp);
1100                               nmp->nm_inprog = NULLVP;
1101                             }
1102                         } else if ((np->n_expiry - NQ_RENEWAL) < time_second) {
1103                             if ((np->n_flag & (NQNFSWRITE | NQNFSNONCACHE))
1104                                  == NQNFSWRITE &&
1105                                  !TAILQ_EMPTY(&vp->v_dirtyblkhd) &&
1106                                  vget(vp, LK_EXCLUSIVE, td) == 0) {
1107                                  nmp->nm_inprog = vp;
1108                                  if (vpid == vp->v_id &&
1109                                      nqnfs_getlease(vp, ND_WRITE, td)==0)
1110                                         np->n_brev = np->n_lrev;
1111                                  vput(vp);
1112                                  nmp->nm_inprog = NULLVP;
1113                             }
1114                         } else
1115                                 break;
1116                         if (np == nmp->nm_timerhead.cqh_first)
1117                                 break;
1118                         np = nmp->nm_timerhead.cqh_first;
1119                 }
1120             }
1121
1122             /*
1123              * Get an authorization string, if required.
1124              */
1125             if ((nmp->nm_state & (NFSSTA_WAITAUTH | NFSSTA_DISMNT | NFSSTA_HASAUTH)) == 0) {
1126                 ncd->ncd_authuid = nmp->nm_authuid;
1127                 if (copyout((caddr_t)ncd, argp, sizeof (struct nfsd_cargs)))
1128                         nmp->nm_state |= NFSSTA_WAITAUTH;
1129                 else
1130                         return (ENEEDAUTH);
1131             }
1132
1133             /*
1134              * Wait a bit (no pun) and do it again.
1135              */
1136             if ((nmp->nm_state & NFSSTA_DISMNT) == 0 &&
1137                 (nmp->nm_state & (NFSSTA_WAITAUTH | NFSSTA_HASAUTH))) {
1138                     error = tsleep((caddr_t)&nmp->nm_authstr, PSOCK | PCATCH,
1139                         "nqnfstimr", hz / 3);
1140                     if (error == EINTR || error == ERESTART)
1141                         (void) dounmount(nmp->nm_mountp, 0, td);
1142             }
1143         }
1144
1145         /*
1146          * Finally, we can free up the mount structure.
1147          */
1148         for (nuidp = nmp->nm_uidlruhead.tqh_first; nuidp != 0; nuidp = nnuidp) {
1149                 nnuidp = nuidp->nu_lru.tqe_next;
1150                 LIST_REMOVE(nuidp, nu_hash);
1151                 TAILQ_REMOVE(&nmp->nm_uidlruhead, nuidp, nu_lru);
1152                 free((caddr_t)nuidp, M_NFSUID);
1153         }
1154         nfs_free_mount(nmp);
1155         if (error == EWOULDBLOCK)
1156                 error = 0;
1157         return (error);
1158 }
1159
1160 #endif /* NFS_NOSERVER */
1161
1162 /*
1163  * Adjust all timer queue expiry times when the time of day clock is changed.
1164  * Called from the settimeofday() syscall.
1165  */
1166 void
1167 nqnfs_lease_updatetime(int deltat)
1168 {
1169         struct thread *td = curthread;  /* XXX */
1170         struct nqlease *lp;
1171         struct nfsnode *np;
1172         struct mount *mp, *nxtmp;
1173         struct nfsmount *nmp;
1174         int s;
1175
1176         if (nqnfsstarttime != 0)
1177                 nqnfsstarttime += deltat;
1178         s = splsoftclock();
1179         for (lp = nqtimerhead.cqh_first; lp != (void *)&nqtimerhead;
1180             lp = lp->lc_timer.cqe_next)
1181                 lp->lc_expiry += deltat;
1182         splx(s);
1183
1184         /*
1185          * Search the mount list for all nqnfs mounts and do their timer
1186          * queues.
1187          */
1188         simple_lock(&mountlist_slock);
1189         for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nxtmp) {
1190                 if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock, td)) {
1191                         nxtmp = TAILQ_NEXT(mp, mnt_list);
1192                         continue;
1193                 }
1194                 if (mp->mnt_stat.f_type == nfs_mount_type) {
1195                         nmp = VFSTONFS(mp);
1196                         if (nmp->nm_flag & NFSMNT_NQNFS) {
1197                                 for (np = nmp->nm_timerhead.cqh_first;
1198                                     np != (void *)&nmp->nm_timerhead;
1199                                     np = np->n_timer.cqe_next) {
1200                                         np->n_expiry += deltat;
1201                                 }
1202                         }
1203                 }
1204                 simple_lock(&mountlist_slock);
1205                 nxtmp = TAILQ_NEXT(mp, mnt_list);
1206                 vfs_unbusy(mp, td);
1207         }
1208         simple_unlock(&mountlist_slock);
1209 }
1210
1211 #ifndef NFS_NOSERVER 
1212 /*
1213  * Lock a server lease.
1214  */
1215 static void
1216 nqsrv_locklease(lp)
1217         struct nqlease *lp;
1218 {
1219
1220         while (lp->lc_flag & LC_LOCKED) {
1221                 lp->lc_flag |= LC_WANTED;
1222                 (void) tsleep((caddr_t)lp, PSOCK, "nqlc", 0);
1223         }
1224         lp->lc_flag |= LC_LOCKED;
1225         lp->lc_flag &= ~LC_WANTED;
1226 }
1227
1228 /*
1229  * Unlock a server lease.
1230  */
1231 static void
1232 nqsrv_unlocklease(lp)
1233         struct nqlease *lp;
1234 {
1235
1236         lp->lc_flag &= ~LC_LOCKED;
1237         if (lp->lc_flag & LC_WANTED)
1238                 wakeup((caddr_t)lp);
1239 }
1240 #endif /* NFS_NOSERVER */
1241
1242 /*
1243  * Update a client lease.
1244  */
1245 void
1246 nqnfs_clientlease(nmp, np, rwflag, cachable, expiry, frev)
1247         register struct nfsmount *nmp;
1248         register struct nfsnode *np;
1249         int rwflag, cachable;
1250         time_t expiry;
1251         u_quad_t frev;
1252 {
1253         register struct nfsnode *tp;
1254
1255         if (np->n_timer.cqe_next != 0) {
1256                 CIRCLEQ_REMOVE(&nmp->nm_timerhead, np, n_timer);
1257                 if (rwflag == ND_WRITE)
1258                         np->n_flag |= NQNFSWRITE;
1259         } else if (rwflag == ND_READ)
1260                 np->n_flag &= ~NQNFSWRITE;
1261         else
1262                 np->n_flag |= NQNFSWRITE;
1263         if (cachable)
1264                 np->n_flag &= ~NQNFSNONCACHE;
1265         else
1266                 np->n_flag |= NQNFSNONCACHE;
1267         np->n_expiry = expiry;
1268         np->n_lrev = frev;
1269         tp = nmp->nm_timerhead.cqh_last;
1270         while (tp != (void *)&nmp->nm_timerhead && tp->n_expiry > np->n_expiry)
1271                 tp = tp->n_timer.cqe_prev;
1272         if (tp == (void *)&nmp->nm_timerhead) {
1273                 CIRCLEQ_INSERT_HEAD(&nmp->nm_timerhead, np, n_timer);
1274         } else {
1275                 CIRCLEQ_INSERT_AFTER(&nmp->nm_timerhead, tp, np, n_timer);
1276         }
1277 }