2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Rick Macklem at The University of Guelph.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
36 * @(#)nfs_nqlease.c 8.9 (Berkeley) 5/20/95
37 * $FreeBSD: src/sys/nfs/nfs_nqlease.c,v 1.50 2000/02/13 03:32:05 peter Exp $
38 * $DragonFly: src/sys/vfs/nfs/Attic/nfs_nqlease.c,v 1.26 2005/06/06 15:09:38 drhodus Exp $
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.
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>
61 #include <sys/systm.h>
63 #include <sys/socket.h>
64 #include <sys/socketvar.h>
65 #include <sys/protosw.h>
67 #include <vm/vm_zone.h>
69 #include <netinet/in.h>
73 #include "nfsm_subs.h"
79 #include <sys/thread2.h>
81 static MALLOC_DEFINE(M_NQMHOST, "NQNFS Host", "Nqnfs host address table");
83 time_t nqnfsstarttime = (time_t)0;
84 int nqsrv_clockskew = NQ_CLOCKSKEW;
85 int nqsrv_writeslack = NQ_WRITESLACK;
86 int nqsrv_maxlease = NQ_MAXLEASE;
88 static int nqsrv_maxnumlease = NQ_MAXNUMLEASE;
91 struct vop_lease_args;
94 static int nqsrv_cmpnam (struct nfssvc_sock *, struct sockaddr *,
96 static int nqnfs_vacated (struct vnode *vp, struct ucred *cred);
97 static void nqsrv_addhost (struct nqhost *lph, struct nfssvc_sock *slp,
98 struct sockaddr *nam);
99 static void nqsrv_instimeq (struct nqlease *lp, u_int32_t duration);
100 static void nqsrv_locklease (struct nqlease *lp);
101 static void nqsrv_send_eviction (struct vnode *vp, struct nqlease *lp,
102 struct nfssvc_sock *slp,
103 struct sockaddr *nam,
105 static void nqsrv_unlocklease (struct nqlease *lp);
106 static void nqsrv_waitfor_expiry (struct nqlease *lp);
108 extern void nqnfs_lease_updatetime (int deltat);
111 * Signifies which rpcs can have piggybacked lease requests
113 int nqnfs_piggy[NFS_NPROCS] = {
142 extern nfstype nfsv2_type[9];
143 extern nfstype nfsv3_type[9];
144 extern struct nfsstats nfsstats;
151 * Get or check for a lease for "vp", based on ND_CHECK flag.
152 * The rules are as follows:
153 * - if a current non-caching lease, reply non-caching
154 * - if a current lease for same host only, extend lease
155 * - if a read cachable lease and a read lease request
156 * add host to list any reply cachable
157 * - else { set non-cachable for read-write sharing }
158 * send eviction notice messages to all other hosts that have lease
159 * wait for lease termination { either by receiving vacated messages
160 * from all the other hosts or expiry
162 * modify lease to non-cachable
163 * - else if no current lease, issue new one
165 * - return boolean TRUE iff nam should be m_freem()'d
166 * NB: Since nqnfs_serverd() is called from a timer, any potential tsleep()
167 * in here must be framed by nqsrv_locklease() and nqsrv_unlocklease().
168 * nqsrv_locklease() is coded such that at least one of LC_LOCKED and
169 * LC_WANTED is set whenever a process is tsleeping in it. The exception
170 * is when a new lease is being allocated, since it is not in the timer
171 * queue yet. (Ditto for the splsoftclock() and splx(s) calls)
174 nqsrv_getlease(struct vnode *vp, u_int32_t *duration, int flags,
175 struct nfssvc_sock *slp, struct thread *td,
176 struct sockaddr *nam, int *cachablep, u_quad_t *frev,
180 struct nqfhhashhead *lpp = NULL;
181 struct nqhost *lph = NULL;
188 if (vp->v_type != VREG && vp->v_type != VDIR && vp->v_type != VLNK)
190 if (*duration > nqsrv_maxlease)
191 *duration = nqsrv_maxlease;
192 error = VOP_GETATTR(vp, &vattr, td);
195 *frev = vattr.va_filerev;
198 if ((flags & ND_CHECK) == 0)
199 nfsstats.srvnqnfs_getleases++;
202 * Find the lease by searching the hash list.
204 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
205 error = VFS_VPTOFH(vp, &fh.fh_fid);
210 lpp = NQFHHASH(fh.fh_fid.fid_data);
211 for (lp = lpp->lh_first; lp != 0; lp = lp->lc_hash.le_next)
212 if (fh.fh_fsid.val[0] == lp->lc_fsid.val[0] &&
213 fh.fh_fsid.val[1] == lp->lc_fsid.val[1] &&
214 !bcmp(fh.fh_fid.fid_data, lp->lc_fiddata,
215 fh.fh_fid.fid_len - sizeof (int32_t))) {
225 if ((lp->lc_flag & LC_NONCACHABLE) ||
226 (lp->lc_morehosts == (struct nqm *)0 &&
227 nqsrv_cmpnam(slp, nam, &lp->lc_host)))
229 if ((flags & ND_READ) && (lp->lc_flag & LC_WRITE) == 0) {
230 if (flags & ND_CHECK)
232 if (nqsrv_cmpnam(slp, nam, &lp->lc_host))
235 if (lp->lc_morehosts) {
236 lph = lp->lc_morehosts->lpm_hosts;
237 lphp = &lp->lc_morehosts->lpm_next;
240 lphp = &lp->lc_morehosts;
243 while (ok && (lph->lph_flag & LC_VALID)) {
244 if (nqsrv_cmpnam(slp, nam, lph))
246 if (++i == LC_MOREHOSTSIZ) {
249 lph = (*lphp)->lpm_hosts;
250 lphp = &((*lphp)->lpm_next);
258 *lphp = (struct nqm *)
259 malloc(sizeof (struct nqm),
260 M_NQMHOST, M_WAITOK);
261 bzero((caddr_t)*lphp, sizeof (struct nqm));
262 lph = (*lphp)->lpm_hosts;
264 nqsrv_addhost(lph, slp, nam);
265 nqsrv_unlocklease(lp);
267 lp->lc_flag |= LC_NONCACHABLE;
269 nqsrv_send_eviction(vp, lp, slp, nam, cred);
270 nqsrv_waitfor_expiry(lp);
271 nqsrv_unlocklease(lp);
275 * Update the lease and return
277 if ((flags & ND_CHECK) == 0)
278 nqsrv_instimeq(lp, *duration);
279 if (lp->lc_flag & LC_NONCACHABLE)
283 if (flags & ND_WRITE)
284 lp->lc_flag |= LC_WRITTEN;
290 if (flags & ND_CHECK)
295 * The value of nqsrv_maxnumlease should be set generously, so that
296 * the following "printf" happens infrequently.
298 if (nfsstats.srvnqnfs_leases > nqsrv_maxnumlease) {
299 printf("Nqnfs server, too many leases\n");
301 (void) tsleep((caddr_t)&lbolt, 0, "nqsrvnuml", 0);
302 } while (nfsstats.srvnqnfs_leases > nqsrv_maxnumlease);
304 MALLOC(lp, struct nqlease *, sizeof (struct nqlease), M_NQLEASE, M_WAITOK);
305 bzero((caddr_t)lp, sizeof (struct nqlease));
306 if (flags & ND_WRITE)
307 lp->lc_flag |= (LC_WRITE | LC_WRITTEN);
308 nqsrv_addhost(&lp->lc_host, slp, nam);
310 lp->lc_fsid = fh.fh_fsid;
311 bcopy(fh.fh_fid.fid_data, lp->lc_fiddata,
312 fh.fh_fid.fid_len - sizeof (int32_t));
314 panic("nfs_nqlease.c: Phoney lpp");
315 LIST_INSERT_HEAD(lpp, lp, lc_hash);
318 nqsrv_instimeq(lp, *duration);
321 if (++nfsstats.srvnqnfs_leases > nfsstats.srvnqnfs_maxleases)
322 nfsstats.srvnqnfs_maxleases = nfsstats.srvnqnfs_leases;
327 * Local lease check for server syscalls.
328 * Just set up args and let nqsrv_getlease() do the rest.
329 * nqnfs_vop_lease_check() is the VOP_LEASE() form of the same routine.
330 * Ifdef'd code in nfsnode.h renames these routines to whatever a particular
334 nqnfs_lease_check(struct vnode *vp, struct thread *td,
335 struct ucred *cred, int flag)
337 u_int32_t duration = 0;
341 nqsrv_getlease(vp, &duration, ND_CHECK | flag, NQLOCALSLP,
342 td, (struct sockaddr *)0, &cache, &frev, cred);
347 * nqnfs_vop_lease_check(struct vnode *a_vp, struct thread *a_td,
348 * struct ucred *a_cred, int a_flag)
351 nqnfs_vop_lease_check(struct vop_lease_args *ap)
353 u_int32_t duration = 0;
357 (void) nqsrv_getlease(ap->a_vp, &duration, ND_CHECK | ap->a_flag,
358 NQLOCALSLP, ap->a_td, (struct sockaddr *)0,
359 &cache, &frev, ap->a_cred);
365 * Add a host to an nqhost structure for a lease.
368 nqsrv_addhost(struct nqhost *lph, struct nfssvc_sock *slp, struct sockaddr *nam)
370 struct sockaddr_in *saddr;
373 if (slp == NQLOCALSLP) {
374 lph->lph_flag |= (LC_VALID | LC_LOCAL);
379 if (nsso && nsso->so_proto->pr_protocol == IPPROTO_UDP) {
380 saddr = (struct sockaddr_in *)nam;
381 lph->lph_flag |= (LC_VALID | LC_UDP);
382 lph->lph_inetaddr = saddr->sin_addr.s_addr;
383 lph->lph_port = saddr->sin_port;
385 lph->lph_flag |= (LC_VALID | LC_SREF);
391 * Update the lease expiry time and position it in the timer queue correctly.
394 nqsrv_instimeq(struct nqlease *lp, u_int32_t duration)
399 newexpiry = time_second + duration + nqsrv_clockskew;
400 if (lp->lc_expiry == newexpiry)
402 if (lp->lc_timer.cqe_next != 0) {
403 CIRCLEQ_REMOVE(&nqtimerhead, lp, lc_timer);
405 lp->lc_expiry = newexpiry;
408 * Find where in the queue it should be.
410 tlp = nqtimerhead.cqh_last;
411 while (tlp != (void *)&nqtimerhead && tlp->lc_expiry > newexpiry)
412 tlp = tlp->lc_timer.cqe_prev;
414 if (tlp == nqtimerhead.cqh_last)
415 NQSTORENOVRAM(newexpiry);
416 #endif /* HASNVRAM */
417 if (tlp == (void *)&nqtimerhead) {
418 CIRCLEQ_INSERT_HEAD(&nqtimerhead, lp, lc_timer);
420 CIRCLEQ_INSERT_AFTER(&nqtimerhead, tlp, lp, lc_timer);
425 * Compare the requesting host address with the lph entry in the lease.
426 * Return true iff it is the same.
427 * This is somewhat messy due to the union in the nqhost structure.
428 * The local host is indicated by the special value of NQLOCALSLP for slp.
431 nqsrv_cmpnam(struct nfssvc_sock *slp, struct sockaddr *nam, struct nqhost *lph)
433 struct sockaddr_in *saddr;
434 struct sockaddr *addr;
435 union nethostaddr lhaddr;
439 if (slp == NQLOCALSLP) {
440 if (lph->lph_flag & LC_LOCAL)
446 if (nsso && nsso->so_proto->pr_protocol == IPPROTO_UDP) {
451 if (lph->lph_flag & LC_UDP) {
452 ret = netaddr_match(AF_INET, &lph->lph_haddr, addr);
454 if ((lph->lph_slp->ns_flag & SLP_VALID) == 0)
456 saddr = (struct sockaddr_in *)lph->lph_slp->ns_nam;
457 if (saddr->sin_family == AF_INET)
458 lhaddr.had_inetaddr = saddr->sin_addr.s_addr;
460 lhaddr.had_nam = lph->lph_slp->ns_nam;
461 ret = netaddr_match(saddr->sin_family, &lhaddr, addr);
467 * Send out eviction notice messages to all other hosts for the lease.
470 nqsrv_send_eviction(struct vnode *vp, struct nqlease *lp,
471 struct nfssvc_sock *slp, struct sockaddr *nam,
474 struct nqhost *lph = &lp->lc_host;
476 struct nqm *lphnext = lp->lc_morehosts;
477 struct mbuf *m, *mreq, *mb, *mb2, *mheadend;
478 struct sockaddr *nam2;
479 struct sockaddr_in *saddr;
484 int len = 1, ok = 1, i = 0;
486 while (ok && (lph->lph_flag & LC_VALID)) {
487 if (nqsrv_cmpnam(slp, nam, lph)) {
488 lph->lph_flag |= LC_VACATED;
489 } else if ((lph->lph_flag & (LC_LOCAL | LC_VACATED)) == 0) {
494 so = lph->lph_slp->ns_so;
495 if (lph->lph_flag & LC_UDP) {
496 MALLOC(nam2, struct sockaddr *,
497 sizeof *nam2, M_SONAME, M_WAITOK);
498 saddr = (struct sockaddr_in *)nam2;
499 saddr->sin_len = sizeof *saddr;
500 saddr->sin_family = AF_INET;
501 saddr->sin_addr.s_addr = lph->lph_inetaddr;
502 saddr->sin_port = lph->lph_port;
503 } else if (lph->lph_slp->ns_flag & SLP_VALID) {
504 nam2 = (struct sockaddr *)0;
508 sotype = so->so_type;
509 if (so->so_proto->pr_flags & PR_CONNREQUIRED)
510 solockp = &lph->lph_slp->ns_solock;
511 nfsm_reqhead((struct vnode *)0, NQNFSPROC_EVICTED,
512 NFSX_V3FH + NFSX_UNSIGNED);
513 fhp = &nfh.fh_generic;
514 bzero((caddr_t)fhp, sizeof(nfh));
515 fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
516 VFS_VPTOFH(vp, &fhp->fh_fid);
517 nfsm_srvfhtom(fhp, 1);
524 if (siz <= 0 || siz > NFS_MAXPACKET) {
525 printf("mbuf siz=%d\n",siz);
526 panic("Bad nfs svc reply");
528 m = nfsm_rpchead(cred, (NFSMNT_NFSV3 | NFSMNT_NQNFS),
530 RPCAUTH_UNIX, 5 * NFSX_UNSIGNED, (char *)0,
531 0, (char *)NULL, mreq, siz, &mheadend, &xid);
533 * For stream protocols, prepend a Sun RPC
536 if (sotype == SOCK_STREAM) {
537 M_PREPEND(m, NFSX_UNSIGNED, MB_WAIT);
539 printf("nfs_nqlease: M_PREPEND failed\n");
540 *mtod(m, u_int32_t *) = htonl(0x80000000 |
541 (m->m_pkthdr.len - NFSX_UNSIGNED));
544 * nfs_sndlock if PR_CONNREQUIRED XXX
547 if ((lph->lph_flag & LC_UDP) == 0 &&
548 ((lph->lph_slp->ns_flag & SLP_VALID) == 0 ||
549 nfs_slplock(lph->lph_slp, 0) == 0)) {
552 (void) nfs_send(so, nam2, m,
555 nfs_slpunlock(lph->lph_slp);
557 if (lph->lph_flag & LC_UDP)
558 FREE(nam2, M_SONAME);
564 len = LC_MOREHOSTSIZ;
565 lph = lphnext->lpm_hosts;
566 lphnext = lphnext->lpm_next;
575 * Wait for the lease to expire.
576 * This will occur when all clients have sent "vacated" messages to
577 * this server OR when it expires do to timeout.
580 nqsrv_waitfor_expiry(struct nqlease *lp)
588 if (time_second > lp->lc_expiry)
591 lphnext = lp->lc_morehosts;
595 while (ok && (lph->lph_flag & LC_VALID)) {
596 if ((lph->lph_flag & (LC_LOCAL | LC_VACATED)) == 0) {
597 lp->lc_flag |= LC_EXPIREDWANTED;
598 (void) tsleep((caddr_t)&lp->lc_flag, 0, "nqexp", 0);
604 len = LC_MOREHOSTSIZ;
605 lph = lphnext->lpm_hosts;
606 lphnext = lphnext->lpm_next;
615 * Nqnfs server timer that maintains the server lease queue.
616 * Scan the lease queue for expired entries:
617 * - when one is found, wakeup anyone waiting for it
618 * else dequeue and free
625 struct nqlease *nextlp;
626 struct nqm *lphnext, *olphnext;
629 for (lp = nqtimerhead.cqh_first; lp != (void *)&nqtimerhead;
631 if (lp->lc_expiry >= time_second)
633 nextlp = lp->lc_timer.cqe_next;
634 if (lp->lc_flag & LC_EXPIREDWANTED) {
635 lp->lc_flag &= ~LC_EXPIREDWANTED;
636 wakeup((caddr_t)&lp->lc_flag);
637 } else if ((lp->lc_flag & (LC_LOCKED | LC_WANTED)) == 0) {
639 * Make a best effort at keeping a write caching lease long
640 * enough by not deleting it until it has been explicitly
641 * vacated or there have been no writes in the previous
642 * write_slack seconds since expiry and the nfsds are not
643 * all busy. The assumption is that if the nfsds are not
644 * all busy now (no queue of nfs requests), then the client
645 * would have been able to do at least one write to the
646 * file during the last write_slack seconds if it was still
647 * trying to push writes to the server.
649 if ((lp->lc_flag & (LC_WRITE | LC_VACATED)) == LC_WRITE &&
650 ((lp->lc_flag & LC_WRITTEN) || nfsd_waiting == 0)) {
651 lp->lc_flag &= ~LC_WRITTEN;
652 nqsrv_instimeq(lp, nqsrv_writeslack);
654 CIRCLEQ_REMOVE(&nqtimerhead, lp, lc_timer);
655 LIST_REMOVE(lp, lc_hash);
657 * This soft reference may no longer be valid, but
658 * no harm done. The worst case is if the vnode was
659 * recycled and has another valid lease reference,
660 * which is dereferenced prematurely.
662 lp->lc_vp->v_lease = (struct nqlease *)0;
664 lphnext = lp->lc_morehosts;
665 olphnext = (struct nqm *)0;
669 while (ok && (lph->lph_flag & LC_VALID)) {
670 if (lph->lph_flag & LC_SREF)
671 nfsrv_slpderef(lph->lph_slp);
674 free((caddr_t)olphnext, M_NQMHOST);
675 olphnext = (struct nqm *)0;
680 len = LC_MOREHOSTSIZ;
681 lph = lphnext->lpm_hosts;
682 lphnext = lphnext->lpm_next;
688 FREE((caddr_t)lp, M_NQLEASE);
690 free((caddr_t)olphnext, M_NQMHOST);
691 nfsstats.srvnqnfs_leases--;
698 * Called from nfssvc_nfsd() for a getlease rpc request.
699 * Do the from/to xdr translation and call nqsrv_getlease() to
703 nqnfsrv_getlease(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
704 struct thread *td, struct mbuf **mrq)
706 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
707 struct sockaddr *nam = nfsd->nd_nam;
708 caddr_t dpos = nfsd->nd_dpos;
709 struct ucred *cred = &nfsd->nd_cr;
710 struct nfs_fattr *fp;
712 struct vattr *vap = &va;
722 struct mbuf *mb, *mb2, *mreq;
723 int flags, rdonly, cache;
725 fhp = &nfh.fh_generic;
727 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
728 flags = fxdr_unsigned(int, *tl++);
729 nfsd->nd_duration = fxdr_unsigned(int, *tl);
730 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly,
731 (nfsd->nd_flag & ND_KERBAUTH), TRUE);
736 if (rdonly && flags == ND_WRITE) {
741 (void) nqsrv_getlease(vp, &nfsd->nd_duration, flags, slp, td,
742 nam, &cache, &frev, cred);
743 error = VOP_GETATTR(vp, vap, td);
745 nfsm_reply(NFSX_V3FATTR + 4 * NFSX_UNSIGNED);
746 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
747 *tl++ = txdr_unsigned(cache);
748 *tl++ = txdr_unsigned(nfsd->nd_duration);
749 txdr_hyper(frev, tl);
750 nfsm_build(fp, struct nfs_fattr *, NFSX_V3FATTR);
751 nfsm_srvfillattr(vap, fp);
756 * Called from nfssvc_nfsd() when a "vacated" message is received from a
757 * client. Find the entry and expire it.
760 nqnfsrv_vacated(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
761 struct thread *td, struct mbuf **mrq)
763 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
764 struct sockaddr *nam = nfsd->nd_nam;
765 caddr_t dpos = nfsd->nd_dpos;
768 struct nqlease *tlp = (struct nqlease *)0;
774 struct mbuf *mreq, *mb;
775 int error = 0, i, len, ok, gotit = 0, cache = 0;
779 fhp = &nfh.fh_generic;
783 * Find the lease by searching the hash list.
785 for (lp = NQFHHASH(fhp->fh_fid.fid_data)->lh_first; lp != 0;
786 lp = lp->lc_hash.le_next)
787 if (fhp->fh_fsid.val[0] == lp->lc_fsid.val[0] &&
788 fhp->fh_fsid.val[1] == lp->lc_fsid.val[1] &&
789 !bcmp(fhp->fh_fid.fid_data, lp->lc_fiddata,
800 lphnext = lp->lc_morehosts;
802 while (ok && (lph->lph_flag & LC_VALID)) {
803 if (nqsrv_cmpnam(slp, nam, lph)) {
804 lph->lph_flag |= LC_VACATED;
810 len = LC_MOREHOSTSIZ;
812 lph = lphnext->lpm_hosts;
813 lphnext = lphnext->lpm_next;
819 if ((lp->lc_flag & LC_EXPIREDWANTED) && gotit) {
820 lp->lc_flag &= ~LC_EXPIREDWANTED;
821 wakeup((caddr_t)&lp->lc_flag);
829 #endif /* NFS_NOSERVER */
832 * Client get lease rpc function.
835 nqnfs_getlease(struct vnode *vp, int rwflag, struct thread *td)
841 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
842 caddr_t bpos, dpos, cp2;
845 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
849 nfsstats.rpccnt[NQNFSPROC_GETLEASE]++;
850 mb = mreq = nfsm_reqh(vp, NQNFSPROC_GETLEASE, NFSX_V3FH+2*NFSX_UNSIGNED,
853 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
854 *tl++ = txdr_unsigned(rwflag);
855 *tl = txdr_unsigned(nmp->nm_leaseterm);
856 reqtime = time_second;
857 nfsm_request(vp, NQNFSPROC_GETLEASE, td, nfs_vpcred(vp, rwflag));
859 nfsm_dissect(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
860 cachable = fxdr_unsigned(int, *tl++);
861 reqtime += fxdr_unsigned(int, *tl++);
862 if (reqtime > time_second) {
863 frev = fxdr_hyper(tl);
864 nqnfs_clientlease(nmp, np, rwflag, cachable, reqtime, frev);
865 nfsm_loadattr(vp, (struct vattr *)0);
867 error = NQNFS_EXPIRED;
876 * Client vacated message function.
879 nqnfs_vacated(struct vnode *vp, struct ucred *cred)
888 struct mbuf *m, *mreq, *mb, *mb2, *mheadend;
889 struct nfsmount *nmp;
892 nmp = VFSTONFS(vp->v_mount);
893 nfsstats.rpccnt[NQNFSPROC_VACATED]++;
894 nfsm_reqhead(vp, NQNFSPROC_VACATED, NFSX_FH(1));
902 m = nfsm_rpchead(cred, nmp->nm_flag, NQNFSPROC_VACATED,
903 RPCAUTH_UNIX, 5 * NFSX_UNSIGNED, (char *)0,
904 0, (char *)NULL, mreq, i, &mheadend, &xid);
905 if (nmp->nm_sotype == SOCK_STREAM) {
906 M_PREPEND(m, NFSX_UNSIGNED, MB_WAIT);
909 *mtod(m, u_int32_t *) = htonl(0x80000000 | (m->m_pkthdr.len -
915 if (nmp->nm_soflags & PR_CONNREQUIRED)
916 (void) nfs_sndlock(&myrep);
917 (void) nfs_send(nmp->nm_so, nmp->nm_nam, m, &myrep);
918 if (nmp->nm_soflags & PR_CONNREQUIRED)
919 nfs_sndunlock(&myrep);
925 * Called for client side callbacks
928 nqnfs_callback(struct nfsmount *nmp, struct mbuf *mrep, struct mbuf *md,
938 struct nfssvc_sock *slp;
939 struct nfsrv_descript ndesc;
940 struct nfsrv_descript *nfsd = &ndesc;
941 struct mbuf **mrq = (struct mbuf **)0, *mb, *mreq;
942 int error = 0, cache = 0;
949 nfsd->nd_mrep = mrep;
951 nfsd->nd_dpos = dpos;
952 error = nfs_getreq(nfsd, &tnfsd, FALSE);
956 dpos = nfsd->nd_dpos;
957 if (nfsd->nd_procnum != NQNFSPROC_EVICTED) {
961 fhp = &nfh.fh_generic;
964 error = nfs_nget(nmp->nm_mountp, (nfsfh_t *)fhp, NFSX_V3FH, &np);
968 if (np->n_timer.cqe_next != 0) {
970 np->n_flag |= NQNFSEVICTED;
971 if (nmp->nm_timerhead.cqh_first != np) {
972 CIRCLEQ_REMOVE(&nmp->nm_timerhead, np, n_timer);
973 CIRCLEQ_INSERT_HEAD(&nmp->nm_timerhead, np, n_timer);
982 * Nqnfs client helper daemon. Runs once a second to expire leases.
983 * It also get authorization strings for "kerb" mounts.
984 * It must start at the beginning of the list again after any potential
985 * "sleep" since nfs_reclaim() called from vclean() can pull a node off
986 * the list asynchronously.
989 nqnfs_clientd(struct nfsmount *nmp, struct ucred *cred, struct nfsd_cargs *ncd,
990 int flag, caddr_t argp, struct thread *td)
995 struct nfsuid *nuidp, *nnuidp;
999 * First initialize some variables
1003 * If an authorization string is being passed in, get it.
1005 if ((flag & NFSSVC_GOTAUTH) &&
1006 (nmp->nm_state & (NFSSTA_WAITAUTH | NFSSTA_DISMNT)) == 0) {
1007 if (nmp->nm_state & NFSSTA_HASAUTH)
1009 if ((flag & NFSSVC_AUTHINFAIL) == 0) {
1010 if (ncd->ncd_authlen <= nmp->nm_authlen &&
1011 ncd->ncd_verflen <= nmp->nm_verflen &&
1012 !copyin(ncd->ncd_authstr,nmp->nm_authstr,ncd->ncd_authlen)&&
1013 !copyin(ncd->ncd_verfstr,nmp->nm_verfstr,ncd->ncd_verflen)){
1014 nmp->nm_authtype = ncd->ncd_authtype;
1015 nmp->nm_authlen = ncd->ncd_authlen;
1016 nmp->nm_verflen = ncd->ncd_verflen;
1018 nmp->nm_key = ncd->ncd_key;
1021 nmp->nm_state |= NFSSTA_AUTHERR;
1023 nmp->nm_state |= NFSSTA_AUTHERR;
1024 nmp->nm_state |= NFSSTA_HASAUTH;
1025 wakeup((caddr_t)&nmp->nm_authlen);
1027 nmp->nm_state |= NFSSTA_WAITAUTH;
1030 * Loop every second updating queue until there is a termination sig.
1032 while ((nmp->nm_state & NFSSTA_DISMNT) == 0) {
1033 if (nmp->nm_flag & NFSMNT_NQNFS) {
1035 * If there are no outstanding requests (and therefore no
1036 * processes in nfs_reply) and there is data in the receive
1037 * queue, poke for callbacks.
1039 if (TAILQ_EMPTY(&nfs_reqq) && nmp->nm_so &&
1040 nmp->nm_so->so_rcv.sb_cc > 0) {
1041 myrep.r_flags = R_GETONEREP;
1043 myrep.r_mrep = (struct mbuf *)0;
1049 * Loop through the leases, updating as required.
1051 np = nmp->nm_timerhead.cqh_first;
1052 while (np != (void *)&nmp->nm_timerhead &&
1053 (nmp->nm_state & NFSSTA_DISMINPROG) == 0) {
1056 if (np->n_expiry < time_second) {
1057 if (vget(vp, LK_EXCLUSIVE, td) == 0) {
1058 nmp->nm_inprog = vp;
1059 if (vpid == vp->v_id) {
1060 CIRCLEQ_REMOVE(&nmp->nm_timerhead, np, n_timer);
1061 np->n_timer.cqe_next = 0;
1062 if (np->n_flag & (NLMODIFIED | NQNFSEVICTED)) {
1063 if (np->n_flag & NQNFSEVICTED) {
1064 if (vp->v_type == VDIR)
1066 cache_inval_vp(vp, 0);
1067 (void) nfs_vinvalbuf(vp,
1069 np->n_flag &= ~NQNFSEVICTED;
1070 (void) nqnfs_vacated(vp, cred);
1071 } else if (vp->v_type == VREG) {
1072 (void) VOP_FSYNC(vp, MNT_WAIT, td);
1073 np->n_flag &= ~NLMODIFIED;
1078 nmp->nm_inprog = NULLVP;
1080 } else if ((np->n_expiry - NQ_RENEWAL) < time_second) {
1081 if ((np->n_flag & (NQNFSWRITE | NQNFSNONCACHE))
1083 !RB_EMPTY(&vp->v_rbdirty_tree) &&
1084 vget(vp, LK_EXCLUSIVE, td) == 0) {
1085 nmp->nm_inprog = vp;
1086 if (vpid == vp->v_id &&
1087 nqnfs_getlease(vp, ND_WRITE, td)==0)
1088 np->n_brev = np->n_lrev;
1090 nmp->nm_inprog = NULLVP;
1094 if (np == nmp->nm_timerhead.cqh_first)
1096 np = nmp->nm_timerhead.cqh_first;
1101 * Get an authorization string, if required.
1103 if ((nmp->nm_state & (NFSSTA_WAITAUTH | NFSSTA_DISMNT | NFSSTA_HASAUTH)) == 0) {
1104 ncd->ncd_authuid = nmp->nm_authuid;
1105 if (copyout((caddr_t)ncd, argp, sizeof (struct nfsd_cargs)))
1106 nmp->nm_state |= NFSSTA_WAITAUTH;
1112 * Wait a bit (no pun) and do it again.
1114 if ((nmp->nm_state & NFSSTA_DISMNT) == 0 &&
1115 (nmp->nm_state & (NFSSTA_WAITAUTH | NFSSTA_HASAUTH))) {
1116 error = tsleep((caddr_t)&nmp->nm_authstr, PCATCH,
1117 "nqnfstimr", hz / 3);
1118 if (error == EINTR || error == ERESTART)
1119 (void) dounmount(nmp->nm_mountp, 0, td);
1124 * Finally, we can free up the mount structure.
1126 TAILQ_FOREACH_MUTABLE(nuidp, &nmp->nm_uidlruhead, nu_lru, nnuidp) {
1127 LIST_REMOVE(nuidp, nu_hash);
1128 TAILQ_REMOVE(&nmp->nm_uidlruhead, nuidp, nu_lru);
1129 free((caddr_t)nuidp, M_NFSUID);
1131 nfs_free_mount(nmp);
1132 if (error == EWOULDBLOCK)
1137 #endif /* NFS_NOSERVER */
1140 * Adjust all timer queue expiry times when the time of day clock is changed.
1141 * Called from the settimeofday() syscall.
1143 static int nqnfs_lease_updatetime_callback(struct mount *mp, void *data);
1146 nqnfs_lease_updatetime(int deltat)
1150 if (nqnfsstarttime != 0)
1151 nqnfsstarttime += deltat;
1153 for (lp = nqtimerhead.cqh_first; lp != (void *)&nqtimerhead;
1154 lp = lp->lc_timer.cqe_next)
1155 lp->lc_expiry += deltat;
1159 * Search the mount list for all nqnfs mounts and do their timer
1162 mountlist_scan(nqnfs_lease_updatetime_callback,
1163 &deltat, MNTSCAN_FORWARD);
1168 nqnfs_lease_updatetime_callback(struct mount *mp, void *data)
1170 int deltat = *(int *)data;
1171 struct nfsmount *nmp;
1174 if (mp->mnt_stat.f_type == nfs_mount_type) {
1176 if (nmp->nm_flag & NFSMNT_NQNFS) {
1177 for (np = nmp->nm_timerhead.cqh_first;
1178 np != (void *)&nmp->nm_timerhead;
1179 np = np->n_timer.cqe_next) {
1180 np->n_expiry += deltat;
1187 #ifndef NFS_NOSERVER
1189 * Lock a server lease.
1192 nqsrv_locklease(struct nqlease *lp)
1195 while (lp->lc_flag & LC_LOCKED) {
1196 lp->lc_flag |= LC_WANTED;
1197 (void) tsleep((caddr_t)lp, 0, "nqlc", 0);
1199 lp->lc_flag |= LC_LOCKED;
1200 lp->lc_flag &= ~LC_WANTED;
1204 * Unlock a server lease.
1207 nqsrv_unlocklease(struct nqlease *lp)
1210 lp->lc_flag &= ~LC_LOCKED;
1211 if (lp->lc_flag & LC_WANTED)
1212 wakeup((caddr_t)lp);
1214 #endif /* NFS_NOSERVER */
1217 * Update a client lease.
1220 nqnfs_clientlease(struct nfsmount *nmp, struct nfsnode *np, int rwflag,
1221 int cachable, time_t expiry, u_quad_t frev)
1225 if (np->n_timer.cqe_next != 0) {
1226 CIRCLEQ_REMOVE(&nmp->nm_timerhead, np, n_timer);
1227 if (rwflag == ND_WRITE)
1228 np->n_flag |= NQNFSWRITE;
1229 } else if (rwflag == ND_READ)
1230 np->n_flag &= ~NQNFSWRITE;
1232 np->n_flag |= NQNFSWRITE;
1234 np->n_flag &= ~NQNFSNONCACHE;
1236 np->n_flag |= NQNFSNONCACHE;
1237 np->n_expiry = expiry;
1239 tp = nmp->nm_timerhead.cqh_last;
1240 while (tp != (void *)&nmp->nm_timerhead && tp->n_expiry > np->n_expiry)
1241 tp = tp->n_timer.cqe_prev;
1242 if (tp == (void *)&nmp->nm_timerhead) {
1243 CIRCLEQ_INSERT_HEAD(&nmp->nm_timerhead, np, n_timer);
1245 CIRCLEQ_INSERT_AFTER(&nmp->nm_timerhead, tp, np, n_timer);