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