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