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.8 2003/08/07 21:17:42 dillon 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 static MALLOC_DEFINE(M_NQMHOST, "NQNFS Host", "Nqnfs host address table");
81 time_t nqnfsstarttime = (time_t)0;
82 int nqsrv_clockskew = NQ_CLOCKSKEW;
83 int nqsrv_writeslack = NQ_WRITESLACK;
84 int nqsrv_maxlease = NQ_MAXLEASE;
86 static int nqsrv_maxnumlease = NQ_MAXNUMLEASE;
89 struct vop_lease_args;
92 static int nqsrv_cmpnam __P((struct nfssvc_sock *, struct sockaddr *,
94 static int nqnfs_vacated __P((struct vnode *vp, struct ucred *cred));
95 static void nqsrv_addhost __P((struct nqhost *lph, struct nfssvc_sock *slp,
96 struct sockaddr *nam));
97 static void nqsrv_instimeq __P((struct nqlease *lp, u_int32_t duration));
98 static void nqsrv_locklease __P((struct nqlease *lp));
99 static void nqsrv_send_eviction __P((struct vnode *vp, struct nqlease *lp,
100 struct nfssvc_sock *slp,
101 struct sockaddr *nam,
102 struct ucred *cred));
103 static void nqsrv_unlocklease __P((struct nqlease *lp));
104 static void nqsrv_waitfor_expiry __P((struct nqlease *lp));
106 extern void nqnfs_lease_updatetime __P((int deltat));
109 * Signifies which rpcs can have piggybacked lease requests
111 int nqnfs_piggy[NFS_NPROCS] = {
140 extern nfstype nfsv2_type[9];
141 extern nfstype nfsv3_type[9];
142 extern int nfsd_waiting;
143 extern struct nfsstats nfsstats;
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
161 * modify lease to non-cachable
162 * - else if no current lease, issue new one
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)
173 nqsrv_getlease(vp, duration, flags, slp, td, nam, cachablep, frev, cred)
177 struct nfssvc_sock *slp;
179 struct sockaddr *nam;
185 struct nqfhhashhead *lpp = NULL;
186 struct nqhost *lph = NULL;
193 if (vp->v_type != VREG && vp->v_type != VDIR && vp->v_type != VLNK)
195 if (*duration > nqsrv_maxlease)
196 *duration = nqsrv_maxlease;
197 error = VOP_GETATTR(vp, &vattr, td);
200 *frev = vattr.va_filerev;
203 if ((flags & ND_CHECK) == 0)
204 nfsstats.srvnqnfs_getleases++;
207 * Find the lease by searching the hash list.
209 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
210 error = VFS_VPTOFH(vp, &fh.fh_fid);
215 lpp = NQFHHASH(fh.fh_fid.fid_data);
216 for (lp = lpp->lh_first; lp != 0; lp = lp->lc_hash.le_next)
217 if (fh.fh_fsid.val[0] == lp->lc_fsid.val[0] &&
218 fh.fh_fsid.val[1] == lp->lc_fsid.val[1] &&
219 !bcmp(fh.fh_fid.fid_data, lp->lc_fiddata,
220 fh.fh_fid.fid_len - sizeof (int32_t))) {
230 if ((lp->lc_flag & LC_NONCACHABLE) ||
231 (lp->lc_morehosts == (struct nqm *)0 &&
232 nqsrv_cmpnam(slp, nam, &lp->lc_host)))
234 if ((flags & ND_READ) && (lp->lc_flag & LC_WRITE) == 0) {
235 if (flags & ND_CHECK)
237 if (nqsrv_cmpnam(slp, nam, &lp->lc_host))
240 if (lp->lc_morehosts) {
241 lph = lp->lc_morehosts->lpm_hosts;
242 lphp = &lp->lc_morehosts->lpm_next;
245 lphp = &lp->lc_morehosts;
248 while (ok && (lph->lph_flag & LC_VALID)) {
249 if (nqsrv_cmpnam(slp, nam, lph))
251 if (++i == LC_MOREHOSTSIZ) {
254 lph = (*lphp)->lpm_hosts;
255 lphp = &((*lphp)->lpm_next);
263 *lphp = (struct nqm *)
264 malloc(sizeof (struct nqm),
265 M_NQMHOST, M_WAITOK);
266 bzero((caddr_t)*lphp, sizeof (struct nqm));
267 lph = (*lphp)->lpm_hosts;
269 nqsrv_addhost(lph, slp, nam);
270 nqsrv_unlocklease(lp);
272 lp->lc_flag |= LC_NONCACHABLE;
274 nqsrv_send_eviction(vp, lp, slp, nam, cred);
275 nqsrv_waitfor_expiry(lp);
276 nqsrv_unlocklease(lp);
280 * Update the lease and return
282 if ((flags & ND_CHECK) == 0)
283 nqsrv_instimeq(lp, *duration);
284 if (lp->lc_flag & LC_NONCACHABLE)
288 if (flags & ND_WRITE)
289 lp->lc_flag |= LC_WRITTEN;
295 if (flags & ND_CHECK)
300 * The value of nqsrv_maxnumlease should be set generously, so that
301 * the following "printf" happens infrequently.
303 if (nfsstats.srvnqnfs_leases > nqsrv_maxnumlease) {
304 printf("Nqnfs server, too many leases\n");
306 (void) tsleep((caddr_t)&lbolt, 0, "nqsrvnuml", 0);
307 } while (nfsstats.srvnqnfs_leases > nqsrv_maxnumlease);
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);
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));
319 panic("nfs_nqlease.c: Phoney lpp");
320 LIST_INSERT_HEAD(lpp, lp, lc_hash);
323 nqsrv_instimeq(lp, *duration);
326 if (++nfsstats.srvnqnfs_leases > nfsstats.srvnqnfs_maxleases)
327 nfsstats.srvnqnfs_maxleases = nfsstats.srvnqnfs_leases;
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
339 nqnfs_lease_check(struct vnode *vp, struct thread *td,
340 struct ucred *cred, int flag)
342 u_int32_t duration = 0;
346 (void) nqsrv_getlease(vp, &duration, ND_CHECK | flag, NQLOCALSLP,
347 td, (struct sockaddr *)0, &cache, &frev, cred);
351 nqnfs_vop_lease_check(ap)
352 struct vop_lease_args /* {
355 struct ucred *a_cred;
359 u_int32_t duration = 0;
363 (void) nqsrv_getlease(ap->a_vp, &duration, ND_CHECK | ap->a_flag,
364 NQLOCALSLP, ap->a_td, (struct sockaddr *)0,
365 &cache, &frev, ap->a_cred);
371 * Add a host to an nqhost structure for a lease.
374 nqsrv_addhost(lph, slp, nam)
376 struct nfssvc_sock *slp;
377 struct sockaddr *nam;
379 struct sockaddr_in *saddr;
382 if (slp == NQLOCALSLP) {
383 lph->lph_flag |= (LC_VALID | LC_LOCAL);
388 if (nsso && nsso->so_proto->pr_protocol == IPPROTO_UDP) {
389 saddr = (struct sockaddr_in *)nam;
390 lph->lph_flag |= (LC_VALID | LC_UDP);
391 lph->lph_inetaddr = saddr->sin_addr.s_addr;
392 lph->lph_port = saddr->sin_port;
394 lph->lph_flag |= (LC_VALID | LC_SREF);
400 * Update the lease expiry time and position it in the timer queue correctly.
403 nqsrv_instimeq(lp, duration)
410 newexpiry = time_second + duration + nqsrv_clockskew;
411 if (lp->lc_expiry == newexpiry)
413 if (lp->lc_timer.cqe_next != 0) {
414 CIRCLEQ_REMOVE(&nqtimerhead, lp, lc_timer);
416 lp->lc_expiry = newexpiry;
419 * Find where in the queue it should be.
421 tlp = nqtimerhead.cqh_last;
422 while (tlp != (void *)&nqtimerhead && tlp->lc_expiry > newexpiry)
423 tlp = tlp->lc_timer.cqe_prev;
425 if (tlp == nqtimerhead.cqh_last)
426 NQSTORENOVRAM(newexpiry);
427 #endif /* HASNVRAM */
428 if (tlp == (void *)&nqtimerhead) {
429 CIRCLEQ_INSERT_HEAD(&nqtimerhead, lp, lc_timer);
431 CIRCLEQ_INSERT_AFTER(&nqtimerhead, tlp, lp, lc_timer);
436 * Compare the requesting host address with the lph entry in the lease.
437 * Return true iff it is the same.
438 * This is somewhat messy due to the union in the nqhost structure.
439 * The local host is indicated by the special value of NQLOCALSLP for slp.
442 nqsrv_cmpnam(slp, nam, lph)
443 struct nfssvc_sock *slp;
444 struct sockaddr *nam;
447 struct sockaddr_in *saddr;
448 struct sockaddr *addr;
449 union nethostaddr lhaddr;
453 if (slp == NQLOCALSLP) {
454 if (lph->lph_flag & LC_LOCAL)
460 if (nsso && nsso->so_proto->pr_protocol == IPPROTO_UDP) {
465 if (lph->lph_flag & LC_UDP) {
466 ret = netaddr_match(AF_INET, &lph->lph_haddr, addr);
468 if ((lph->lph_slp->ns_flag & SLP_VALID) == 0)
470 saddr = (struct sockaddr_in *)lph->lph_slp->ns_nam;
471 if (saddr->sin_family == AF_INET)
472 lhaddr.had_inetaddr = saddr->sin_addr.s_addr;
474 lhaddr.had_nam = lph->lph_slp->ns_nam;
475 ret = netaddr_match(saddr->sin_family, &lhaddr, addr);
481 * Send out eviction notice messages to all other hosts for the lease.
484 nqsrv_send_eviction(vp, lp, slp, nam, cred)
487 struct nfssvc_sock *slp;
488 struct sockaddr *nam;
491 struct nqhost *lph = &lp->lc_host;
493 struct nqm *lphnext = lp->lc_morehosts;
494 struct mbuf *m, *mreq, *mb, *mb2, *mheadend;
495 struct sockaddr *nam2;
496 struct sockaddr_in *saddr;
501 int len = 1, ok = 1, i = 0;
503 while (ok && (lph->lph_flag & LC_VALID)) {
504 if (nqsrv_cmpnam(slp, nam, lph)) {
505 lph->lph_flag |= LC_VACATED;
506 } else if ((lph->lph_flag & (LC_LOCAL | LC_VACATED)) == 0) {
511 so = lph->lph_slp->ns_so;
512 if (lph->lph_flag & LC_UDP) {
513 MALLOC(nam2, struct sockaddr *,
514 sizeof *nam2, M_SONAME, M_WAITOK);
515 saddr = (struct sockaddr_in *)nam2;
516 saddr->sin_len = sizeof *saddr;
517 saddr->sin_family = AF_INET;
518 saddr->sin_addr.s_addr = lph->lph_inetaddr;
519 saddr->sin_port = lph->lph_port;
520 } else if (lph->lph_slp->ns_flag & SLP_VALID) {
521 nam2 = (struct sockaddr *)0;
525 sotype = so->so_type;
526 if (so->so_proto->pr_flags & PR_CONNREQUIRED)
527 solockp = &lph->lph_slp->ns_solock;
528 nfsm_reqhead((struct vnode *)0, NQNFSPROC_EVICTED,
529 NFSX_V3FH + NFSX_UNSIGNED);
530 fhp = &nfh.fh_generic;
531 bzero((caddr_t)fhp, sizeof(nfh));
532 fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
533 VFS_VPTOFH(vp, &fhp->fh_fid);
534 nfsm_srvfhtom(fhp, 1);
541 if (siz <= 0 || siz > NFS_MAXPACKET) {
542 printf("mbuf siz=%d\n",siz);
543 panic("Bad nfs svc reply");
545 m = nfsm_rpchead(cred, (NFSMNT_NFSV3 | NFSMNT_NQNFS),
547 RPCAUTH_UNIX, 5 * NFSX_UNSIGNED, (char *)0,
548 0, (char *)NULL, mreq, siz, &mheadend, &xid);
550 * For stream protocols, prepend a Sun RPC
553 if (sotype == SOCK_STREAM) {
554 M_PREPEND(m, NFSX_UNSIGNED, M_WAIT);
555 *mtod(m, u_int32_t *) = htonl(0x80000000 |
556 (m->m_pkthdr.len - NFSX_UNSIGNED));
559 * nfs_sndlock if PR_CONNREQUIRED XXX
562 if ((lph->lph_flag & LC_UDP) == 0 &&
563 ((lph->lph_slp->ns_flag & SLP_VALID) == 0 ||
564 nfs_slplock(lph->lph_slp, 0) == 0)) {
567 (void) nfs_send(so, nam2, m,
570 nfs_slpunlock(lph->lph_slp);
572 if (lph->lph_flag & LC_UDP)
573 FREE(nam2, M_SONAME);
579 len = LC_MOREHOSTSIZ;
580 lph = lphnext->lpm_hosts;
581 lphnext = lphnext->lpm_next;
590 * Wait for the lease to expire.
591 * This will occur when all clients have sent "vacated" messages to
592 * this server OR when it expires do to timeout.
595 nqsrv_waitfor_expiry(lp)
604 if (time_second > lp->lc_expiry)
607 lphnext = lp->lc_morehosts;
611 while (ok && (lph->lph_flag & LC_VALID)) {
612 if ((lph->lph_flag & (LC_LOCAL | LC_VACATED)) == 0) {
613 lp->lc_flag |= LC_EXPIREDWANTED;
614 (void) tsleep((caddr_t)&lp->lc_flag, 0, "nqexp", 0);
620 len = LC_MOREHOSTSIZ;
621 lph = lphnext->lpm_hosts;
622 lphnext = lphnext->lpm_next;
631 * Nqnfs server timer that maintains the server lease queue.
632 * Scan the lease queue for expired entries:
633 * - when one is found, wakeup anyone waiting for it
634 * else dequeue and free
641 struct nqlease *nextlp;
642 struct nqm *lphnext, *olphnext;
645 for (lp = nqtimerhead.cqh_first; lp != (void *)&nqtimerhead;
647 if (lp->lc_expiry >= time_second)
649 nextlp = lp->lc_timer.cqe_next;
650 if (lp->lc_flag & LC_EXPIREDWANTED) {
651 lp->lc_flag &= ~LC_EXPIREDWANTED;
652 wakeup((caddr_t)&lp->lc_flag);
653 } else if ((lp->lc_flag & (LC_LOCKED | LC_WANTED)) == 0) {
655 * Make a best effort at keeping a write caching lease long
656 * enough by not deleting it until it has been explicitly
657 * vacated or there have been no writes in the previous
658 * write_slack seconds since expiry and the nfsds are not
659 * all busy. The assumption is that if the nfsds are not
660 * all busy now (no queue of nfs requests), then the client
661 * would have been able to do at least one write to the
662 * file during the last write_slack seconds if it was still
663 * trying to push writes to the server.
665 if ((lp->lc_flag & (LC_WRITE | LC_VACATED)) == LC_WRITE &&
666 ((lp->lc_flag & LC_WRITTEN) || nfsd_waiting == 0)) {
667 lp->lc_flag &= ~LC_WRITTEN;
668 nqsrv_instimeq(lp, nqsrv_writeslack);
670 CIRCLEQ_REMOVE(&nqtimerhead, lp, lc_timer);
671 LIST_REMOVE(lp, lc_hash);
673 * This soft reference may no longer be valid, but
674 * no harm done. The worst case is if the vnode was
675 * recycled and has another valid lease reference,
676 * which is dereferenced prematurely.
678 lp->lc_vp->v_lease = (struct nqlease *)0;
680 lphnext = lp->lc_morehosts;
681 olphnext = (struct nqm *)0;
685 while (ok && (lph->lph_flag & LC_VALID)) {
686 if (lph->lph_flag & LC_SREF)
687 nfsrv_slpderef(lph->lph_slp);
690 free((caddr_t)olphnext, M_NQMHOST);
691 olphnext = (struct nqm *)0;
696 len = LC_MOREHOSTSIZ;
697 lph = lphnext->lpm_hosts;
698 lphnext = lphnext->lpm_next;
704 FREE((caddr_t)lp, M_NQLEASE);
706 free((caddr_t)olphnext, M_NQMHOST);
707 nfsstats.srvnqnfs_leases--;
714 * Called from nfssvc_nfsd() for a getlease rpc request.
715 * Do the from/to xdr translation and call nqsrv_getlease() to
719 nqnfsrv_getlease(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
720 struct thread *td, struct mbuf **mrq)
722 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
723 struct sockaddr *nam = nfsd->nd_nam;
724 caddr_t dpos = nfsd->nd_dpos;
725 struct ucred *cred = &nfsd->nd_cr;
726 struct nfs_fattr *fp;
728 struct vattr *vap = &va;
738 struct mbuf *mb, *mb2, *mreq;
739 int flags, rdonly, cache;
741 fhp = &nfh.fh_generic;
743 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
744 flags = fxdr_unsigned(int, *tl++);
745 nfsd->nd_duration = fxdr_unsigned(int, *tl);
746 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly,
747 (nfsd->nd_flag & ND_KERBAUTH), TRUE);
752 if (rdonly && flags == ND_WRITE) {
757 (void) nqsrv_getlease(vp, &nfsd->nd_duration, flags, slp, td,
758 nam, &cache, &frev, cred);
759 error = VOP_GETATTR(vp, vap, td);
761 nfsm_reply(NFSX_V3FATTR + 4 * NFSX_UNSIGNED);
762 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
763 *tl++ = txdr_unsigned(cache);
764 *tl++ = txdr_unsigned(nfsd->nd_duration);
765 txdr_hyper(frev, tl);
766 nfsm_build(fp, struct nfs_fattr *, NFSX_V3FATTR);
767 nfsm_srvfillattr(vap, fp);
772 * Called from nfssvc_nfsd() when a "vacated" message is received from a
773 * client. Find the entry and expire it.
776 nqnfsrv_vacated(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
777 struct thread *td, struct mbuf **mrq)
779 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
780 struct sockaddr *nam = nfsd->nd_nam;
781 caddr_t dpos = nfsd->nd_dpos;
784 struct nqlease *tlp = (struct nqlease *)0;
790 struct mbuf *mreq, *mb;
791 int error = 0, i, len, ok, gotit = 0, cache = 0;
795 fhp = &nfh.fh_generic;
799 * Find the lease by searching the hash list.
801 for (lp = NQFHHASH(fhp->fh_fid.fid_data)->lh_first; lp != 0;
802 lp = lp->lc_hash.le_next)
803 if (fhp->fh_fsid.val[0] == lp->lc_fsid.val[0] &&
804 fhp->fh_fsid.val[1] == lp->lc_fsid.val[1] &&
805 !bcmp(fhp->fh_fid.fid_data, lp->lc_fiddata,
816 lphnext = lp->lc_morehosts;
818 while (ok && (lph->lph_flag & LC_VALID)) {
819 if (nqsrv_cmpnam(slp, nam, lph)) {
820 lph->lph_flag |= LC_VACATED;
826 len = LC_MOREHOSTSIZ;
828 lph = lphnext->lpm_hosts;
829 lphnext = lphnext->lpm_next;
835 if ((lp->lc_flag & LC_EXPIREDWANTED) && gotit) {
836 lp->lc_flag &= ~LC_EXPIREDWANTED;
837 wakeup((caddr_t)&lp->lc_flag);
845 #endif /* NFS_NOSERVER */
848 * Client get lease rpc function.
851 nqnfs_getlease(struct vnode *vp, int rwflag, struct thread *td)
857 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
858 caddr_t bpos, dpos, cp2;
861 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
865 nfsstats.rpccnt[NQNFSPROC_GETLEASE]++;
866 mb = mreq = nfsm_reqh(vp, NQNFSPROC_GETLEASE, NFSX_V3FH+2*NFSX_UNSIGNED,
869 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
870 *tl++ = txdr_unsigned(rwflag);
871 *tl = txdr_unsigned(nmp->nm_leaseterm);
872 reqtime = time_second;
873 nfsm_request(vp, NQNFSPROC_GETLEASE, td, NFSVPCRED(vp));
875 nfsm_dissect(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
876 cachable = fxdr_unsigned(int, *tl++);
877 reqtime += fxdr_unsigned(int, *tl++);
878 if (reqtime > time_second) {
879 frev = fxdr_hyper(tl);
880 nqnfs_clientlease(nmp, np, rwflag, cachable, reqtime, frev);
881 nfsm_loadattr(vp, (struct vattr *)0);
883 error = NQNFS_EXPIRED;
890 * Client vacated message function.
893 nqnfs_vacated(vp, cred)
904 struct mbuf *m, *mreq, *mb, *mb2, *mheadend;
905 struct nfsmount *nmp;
908 nmp = VFSTONFS(vp->v_mount);
909 nfsstats.rpccnt[NQNFSPROC_VACATED]++;
910 nfsm_reqhead(vp, NQNFSPROC_VACATED, NFSX_FH(1));
918 m = nfsm_rpchead(cred, nmp->nm_flag, NQNFSPROC_VACATED,
919 RPCAUTH_UNIX, 5 * NFSX_UNSIGNED, (char *)0,
920 0, (char *)NULL, mreq, i, &mheadend, &xid);
921 if (nmp->nm_sotype == SOCK_STREAM) {
922 M_PREPEND(m, NFSX_UNSIGNED, M_WAIT);
923 *mtod(m, u_int32_t *) = htonl(0x80000000 | (m->m_pkthdr.len -
928 if (nmp->nm_soflags & PR_CONNREQUIRED)
929 (void) nfs_sndlock(&myrep);
930 (void) nfs_send(nmp->nm_so, nmp->nm_nam, m, &myrep);
931 if (nmp->nm_soflags & PR_CONNREQUIRED)
932 nfs_sndunlock(&myrep);
938 * Called for client side callbacks
941 nqnfs_callback(nmp, mrep, md, dpos)
942 struct nfsmount *nmp;
943 struct mbuf *mrep, *md;
953 struct nfssvc_sock *slp;
954 struct nfsrv_descript ndesc;
955 struct nfsrv_descript *nfsd = &ndesc;
956 struct mbuf **mrq = (struct mbuf **)0, *mb, *mreq;
957 int error = 0, cache = 0;
964 nfsd->nd_mrep = mrep;
966 nfsd->nd_dpos = dpos;
967 error = nfs_getreq(nfsd, &tnfsd, FALSE);
971 dpos = nfsd->nd_dpos;
972 if (nfsd->nd_procnum != NQNFSPROC_EVICTED) {
976 fhp = &nfh.fh_generic;
979 error = nfs_nget(nmp->nm_mountp, (nfsfh_t *)fhp, NFSX_V3FH, &np);
983 if (np->n_timer.cqe_next != 0) {
985 np->n_flag |= NQNFSEVICTED;
986 if (nmp->nm_timerhead.cqh_first != np) {
987 CIRCLEQ_REMOVE(&nmp->nm_timerhead, np, n_timer);
988 CIRCLEQ_INSERT_HEAD(&nmp->nm_timerhead, np, n_timer);
997 * Nqnfs client helper daemon. Runs once a second to expire leases.
998 * It also get authorization strings for "kerb" mounts.
999 * It must start at the beginning of the list again after any potential
1000 * "sleep" since nfs_reclaim() called from vclean() can pull a node off
1001 * the list asynchronously.
1004 nqnfs_clientd(nmp, cred, ncd, flag, argp, td)
1005 struct nfsmount *nmp;
1007 struct nfsd_cargs *ncd;
1014 struct nfsreq myrep;
1015 struct nfsuid *nuidp, *nnuidp;
1016 int error = 0, vpid;
1019 * First initialize some variables
1023 * If an authorization string is being passed in, get it.
1025 if ((flag & NFSSVC_GOTAUTH) &&
1026 (nmp->nm_state & (NFSSTA_WAITAUTH | NFSSTA_DISMNT)) == 0) {
1027 if (nmp->nm_state & NFSSTA_HASAUTH)
1029 if ((flag & NFSSVC_AUTHINFAIL) == 0) {
1030 if (ncd->ncd_authlen <= nmp->nm_authlen &&
1031 ncd->ncd_verflen <= nmp->nm_verflen &&
1032 !copyin(ncd->ncd_authstr,nmp->nm_authstr,ncd->ncd_authlen)&&
1033 !copyin(ncd->ncd_verfstr,nmp->nm_verfstr,ncd->ncd_verflen)){
1034 nmp->nm_authtype = ncd->ncd_authtype;
1035 nmp->nm_authlen = ncd->ncd_authlen;
1036 nmp->nm_verflen = ncd->ncd_verflen;
1038 nmp->nm_key = ncd->ncd_key;
1041 nmp->nm_state |= NFSSTA_AUTHERR;
1043 nmp->nm_state |= NFSSTA_AUTHERR;
1044 nmp->nm_state |= NFSSTA_HASAUTH;
1045 wakeup((caddr_t)&nmp->nm_authlen);
1047 nmp->nm_state |= NFSSTA_WAITAUTH;
1050 * Loop every second updating queue until there is a termination sig.
1052 while ((nmp->nm_state & NFSSTA_DISMNT) == 0) {
1053 if (nmp->nm_flag & NFSMNT_NQNFS) {
1055 * If there are no outstanding requests (and therefore no
1056 * processes in nfs_reply) and there is data in the receive
1057 * queue, poke for callbacks.
1059 if (nfs_reqq.tqh_first == 0 && nmp->nm_so &&
1060 nmp->nm_so->so_rcv.sb_cc > 0) {
1061 myrep.r_flags = R_GETONEREP;
1063 myrep.r_mrep = (struct mbuf *)0;
1065 (void) nfs_reply(&myrep);
1069 * Loop through the leases, updating as required.
1071 np = nmp->nm_timerhead.cqh_first;
1072 while (np != (void *)&nmp->nm_timerhead &&
1073 (nmp->nm_state & NFSSTA_DISMINPROG) == 0) {
1076 if (np->n_expiry < time_second) {
1077 if (vget(vp, LK_EXCLUSIVE, td) == 0) {
1078 nmp->nm_inprog = vp;
1079 if (vpid == vp->v_id) {
1080 CIRCLEQ_REMOVE(&nmp->nm_timerhead, np, n_timer);
1081 np->n_timer.cqe_next = 0;
1082 if (np->n_flag & (NMODIFIED | NQNFSEVICTED)) {
1083 if (np->n_flag & NQNFSEVICTED) {
1084 if (vp->v_type == VDIR)
1087 (void) nfs_vinvalbuf(vp,
1089 np->n_flag &= ~NQNFSEVICTED;
1090 (void) nqnfs_vacated(vp, cred);
1091 } else if (vp->v_type == VREG) {
1092 (void) VOP_FSYNC(vp, MNT_WAIT, td);
1093 np->n_flag &= ~NMODIFIED;
1098 nmp->nm_inprog = NULLVP;
1100 } else if ((np->n_expiry - NQ_RENEWAL) < time_second) {
1101 if ((np->n_flag & (NQNFSWRITE | NQNFSNONCACHE))
1103 !TAILQ_EMPTY(&vp->v_dirtyblkhd) &&
1104 vget(vp, LK_EXCLUSIVE, td) == 0) {
1105 nmp->nm_inprog = vp;
1106 if (vpid == vp->v_id &&
1107 nqnfs_getlease(vp, ND_WRITE, td)==0)
1108 np->n_brev = np->n_lrev;
1110 nmp->nm_inprog = NULLVP;
1114 if (np == nmp->nm_timerhead.cqh_first)
1116 np = nmp->nm_timerhead.cqh_first;
1121 * Get an authorization string, if required.
1123 if ((nmp->nm_state & (NFSSTA_WAITAUTH | NFSSTA_DISMNT | NFSSTA_HASAUTH)) == 0) {
1124 ncd->ncd_authuid = nmp->nm_authuid;
1125 if (copyout((caddr_t)ncd, argp, sizeof (struct nfsd_cargs)))
1126 nmp->nm_state |= NFSSTA_WAITAUTH;
1132 * Wait a bit (no pun) and do it again.
1134 if ((nmp->nm_state & NFSSTA_DISMNT) == 0 &&
1135 (nmp->nm_state & (NFSSTA_WAITAUTH | NFSSTA_HASAUTH))) {
1136 error = tsleep((caddr_t)&nmp->nm_authstr, PCATCH,
1137 "nqnfstimr", hz / 3);
1138 if (error == EINTR || error == ERESTART)
1139 (void) dounmount(nmp->nm_mountp, 0, td);
1144 * Finally, we can free up the mount structure.
1146 for (nuidp = nmp->nm_uidlruhead.tqh_first; nuidp != 0; nuidp = nnuidp) {
1147 nnuidp = nuidp->nu_lru.tqe_next;
1148 LIST_REMOVE(nuidp, nu_hash);
1149 TAILQ_REMOVE(&nmp->nm_uidlruhead, nuidp, nu_lru);
1150 free((caddr_t)nuidp, M_NFSUID);
1152 nfs_free_mount(nmp);
1153 if (error == EWOULDBLOCK)
1158 #endif /* NFS_NOSERVER */
1161 * Adjust all timer queue expiry times when the time of day clock is changed.
1162 * Called from the settimeofday() syscall.
1165 nqnfs_lease_updatetime(int deltat)
1167 struct thread *td = curthread; /* XXX */
1170 struct mount *mp, *nxtmp;
1171 struct nfsmount *nmp;
1174 if (nqnfsstarttime != 0)
1175 nqnfsstarttime += deltat;
1177 for (lp = nqtimerhead.cqh_first; lp != (void *)&nqtimerhead;
1178 lp = lp->lc_timer.cqe_next)
1179 lp->lc_expiry += deltat;
1183 * Search the mount list for all nqnfs mounts and do their timer
1186 lwkt_gettoken(&mountlist_token);
1187 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nxtmp) {
1188 if (vfs_busy(mp, LK_NOWAIT, &mountlist_token, td)) {
1189 nxtmp = TAILQ_NEXT(mp, mnt_list);
1192 if (mp->mnt_stat.f_type == nfs_mount_type) {
1194 if (nmp->nm_flag & NFSMNT_NQNFS) {
1195 for (np = nmp->nm_timerhead.cqh_first;
1196 np != (void *)&nmp->nm_timerhead;
1197 np = np->n_timer.cqe_next) {
1198 np->n_expiry += deltat;
1202 lwkt_gettoken(&mountlist_token);
1203 nxtmp = TAILQ_NEXT(mp, mnt_list);
1206 lwkt_reltoken(&mountlist_token);
1209 #ifndef NFS_NOSERVER
1211 * Lock a server lease.
1218 while (lp->lc_flag & LC_LOCKED) {
1219 lp->lc_flag |= LC_WANTED;
1220 (void) tsleep((caddr_t)lp, 0, "nqlc", 0);
1222 lp->lc_flag |= LC_LOCKED;
1223 lp->lc_flag &= ~LC_WANTED;
1227 * Unlock a server lease.
1230 nqsrv_unlocklease(lp)
1234 lp->lc_flag &= ~LC_LOCKED;
1235 if (lp->lc_flag & LC_WANTED)
1236 wakeup((caddr_t)lp);
1238 #endif /* NFS_NOSERVER */
1241 * Update a client lease.
1244 nqnfs_clientlease(nmp, np, rwflag, cachable, expiry, frev)
1245 struct nfsmount *nmp;
1247 int rwflag, cachable;
1253 if (np->n_timer.cqe_next != 0) {
1254 CIRCLEQ_REMOVE(&nmp->nm_timerhead, np, n_timer);
1255 if (rwflag == ND_WRITE)
1256 np->n_flag |= NQNFSWRITE;
1257 } else if (rwflag == ND_READ)
1258 np->n_flag &= ~NQNFSWRITE;
1260 np->n_flag |= NQNFSWRITE;
1262 np->n_flag &= ~NQNFSNONCACHE;
1264 np->n_flag |= NQNFSNONCACHE;
1265 np->n_expiry = expiry;
1267 tp = nmp->nm_timerhead.cqh_last;
1268 while (tp != (void *)&nmp->nm_timerhead && tp->n_expiry > np->n_expiry)
1269 tp = tp->n_timer.cqe_prev;
1270 if (tp == (void *)&nmp->nm_timerhead) {
1271 CIRCLEQ_INSERT_HEAD(&nmp->nm_timerhead, np, n_timer);
1273 CIRCLEQ_INSERT_AFTER(&nmp->nm_timerhead, tp, np, n_timer);