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.24 2005/04/15 19:08:21 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 (struct nfssvc_sock *, struct sockaddr *,
94 static int nqnfs_vacated (struct vnode *vp, struct ucred *cred);
95 static void nqsrv_addhost (struct nqhost *lph, struct nfssvc_sock *slp,
96 struct sockaddr *nam);
97 static void nqsrv_instimeq (struct nqlease *lp, u_int32_t duration);
98 static void nqsrv_locklease (struct nqlease *lp);
99 static void nqsrv_send_eviction (struct vnode *vp, struct nqlease *lp,
100 struct nfssvc_sock *slp,
101 struct sockaddr *nam,
103 static void nqsrv_unlocklease (struct nqlease *lp);
104 static void nqsrv_waitfor_expiry (struct nqlease *lp);
106 extern void nqnfs_lease_updatetime (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 struct nfsstats nfsstats;
149 * Get or check for a lease for "vp", based on ND_CHECK flag.
150 * The rules are as follows:
151 * - if a current non-caching lease, reply non-caching
152 * - if a current lease for same host only, extend lease
153 * - if a read cachable lease and a read lease request
154 * add host to list any reply cachable
155 * - else { set non-cachable for read-write sharing }
156 * send eviction notice messages to all other hosts that have lease
157 * wait for lease termination { either by receiving vacated messages
158 * from all the other hosts or expiry
160 * modify lease to non-cachable
161 * - else if no current lease, issue new one
163 * - return boolean TRUE iff nam should be m_freem()'d
164 * NB: Since nqnfs_serverd() is called from a timer, any potential tsleep()
165 * in here must be framed by nqsrv_locklease() and nqsrv_unlocklease().
166 * nqsrv_locklease() is coded such that at least one of LC_LOCKED and
167 * LC_WANTED is set whenever a process is tsleeping in it. The exception
168 * is when a new lease is being allocated, since it is not in the timer
169 * queue yet. (Ditto for the splsoftclock() and splx(s) calls)
172 nqsrv_getlease(struct vnode *vp, u_int32_t *duration, int flags,
173 struct nfssvc_sock *slp, struct thread *td,
174 struct sockaddr *nam, int *cachablep, u_quad_t *frev,
178 struct nqfhhashhead *lpp = NULL;
179 struct nqhost *lph = NULL;
186 if (vp->v_type != VREG && vp->v_type != VDIR && vp->v_type != VLNK)
188 if (*duration > nqsrv_maxlease)
189 *duration = nqsrv_maxlease;
190 error = VOP_GETATTR(vp, &vattr, td);
193 *frev = vattr.va_filerev;
196 if ((flags & ND_CHECK) == 0)
197 nfsstats.srvnqnfs_getleases++;
200 * Find the lease by searching the hash list.
202 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
203 error = VFS_VPTOFH(vp, &fh.fh_fid);
208 lpp = NQFHHASH(fh.fh_fid.fid_data);
209 for (lp = lpp->lh_first; lp != 0; lp = lp->lc_hash.le_next)
210 if (fh.fh_fsid.val[0] == lp->lc_fsid.val[0] &&
211 fh.fh_fsid.val[1] == lp->lc_fsid.val[1] &&
212 !bcmp(fh.fh_fid.fid_data, lp->lc_fiddata,
213 fh.fh_fid.fid_len - sizeof (int32_t))) {
223 if ((lp->lc_flag & LC_NONCACHABLE) ||
224 (lp->lc_morehosts == (struct nqm *)0 &&
225 nqsrv_cmpnam(slp, nam, &lp->lc_host)))
227 if ((flags & ND_READ) && (lp->lc_flag & LC_WRITE) == 0) {
228 if (flags & ND_CHECK)
230 if (nqsrv_cmpnam(slp, nam, &lp->lc_host))
233 if (lp->lc_morehosts) {
234 lph = lp->lc_morehosts->lpm_hosts;
235 lphp = &lp->lc_morehosts->lpm_next;
238 lphp = &lp->lc_morehosts;
241 while (ok && (lph->lph_flag & LC_VALID)) {
242 if (nqsrv_cmpnam(slp, nam, lph))
244 if (++i == LC_MOREHOSTSIZ) {
247 lph = (*lphp)->lpm_hosts;
248 lphp = &((*lphp)->lpm_next);
256 *lphp = (struct nqm *)
257 malloc(sizeof (struct nqm),
258 M_NQMHOST, M_WAITOK);
259 bzero((caddr_t)*lphp, sizeof (struct nqm));
260 lph = (*lphp)->lpm_hosts;
262 nqsrv_addhost(lph, slp, nam);
263 nqsrv_unlocklease(lp);
265 lp->lc_flag |= LC_NONCACHABLE;
267 nqsrv_send_eviction(vp, lp, slp, nam, cred);
268 nqsrv_waitfor_expiry(lp);
269 nqsrv_unlocklease(lp);
273 * Update the lease and return
275 if ((flags & ND_CHECK) == 0)
276 nqsrv_instimeq(lp, *duration);
277 if (lp->lc_flag & LC_NONCACHABLE)
281 if (flags & ND_WRITE)
282 lp->lc_flag |= LC_WRITTEN;
288 if (flags & ND_CHECK)
293 * The value of nqsrv_maxnumlease should be set generously, so that
294 * the following "printf" happens infrequently.
296 if (nfsstats.srvnqnfs_leases > nqsrv_maxnumlease) {
297 printf("Nqnfs server, too many leases\n");
299 (void) tsleep((caddr_t)&lbolt, 0, "nqsrvnuml", 0);
300 } while (nfsstats.srvnqnfs_leases > nqsrv_maxnumlease);
302 MALLOC(lp, struct nqlease *, sizeof (struct nqlease), M_NQLEASE, M_WAITOK);
303 bzero((caddr_t)lp, sizeof (struct nqlease));
304 if (flags & ND_WRITE)
305 lp->lc_flag |= (LC_WRITE | LC_WRITTEN);
306 nqsrv_addhost(&lp->lc_host, slp, nam);
308 lp->lc_fsid = fh.fh_fsid;
309 bcopy(fh.fh_fid.fid_data, lp->lc_fiddata,
310 fh.fh_fid.fid_len - sizeof (int32_t));
312 panic("nfs_nqlease.c: Phoney lpp");
313 LIST_INSERT_HEAD(lpp, lp, lc_hash);
316 nqsrv_instimeq(lp, *duration);
319 if (++nfsstats.srvnqnfs_leases > nfsstats.srvnqnfs_maxleases)
320 nfsstats.srvnqnfs_maxleases = nfsstats.srvnqnfs_leases;
325 * Local lease check for server syscalls.
326 * Just set up args and let nqsrv_getlease() do the rest.
327 * nqnfs_vop_lease_check() is the VOP_LEASE() form of the same routine.
328 * Ifdef'd code in nfsnode.h renames these routines to whatever a particular
332 nqnfs_lease_check(struct vnode *vp, struct thread *td,
333 struct ucred *cred, int flag)
335 u_int32_t duration = 0;
339 nqsrv_getlease(vp, &duration, ND_CHECK | flag, NQLOCALSLP,
340 td, (struct sockaddr *)0, &cache, &frev, cred);
345 * nqnfs_vop_lease_check(struct vnode *a_vp, struct thread *a_td,
346 * struct ucred *a_cred, int a_flag)
349 nqnfs_vop_lease_check(struct vop_lease_args *ap)
351 u_int32_t duration = 0;
355 (void) nqsrv_getlease(ap->a_vp, &duration, ND_CHECK | ap->a_flag,
356 NQLOCALSLP, ap->a_td, (struct sockaddr *)0,
357 &cache, &frev, ap->a_cred);
363 * Add a host to an nqhost structure for a lease.
366 nqsrv_addhost(struct nqhost *lph, struct nfssvc_sock *slp, struct sockaddr *nam)
368 struct sockaddr_in *saddr;
371 if (slp == NQLOCALSLP) {
372 lph->lph_flag |= (LC_VALID | LC_LOCAL);
377 if (nsso && nsso->so_proto->pr_protocol == IPPROTO_UDP) {
378 saddr = (struct sockaddr_in *)nam;
379 lph->lph_flag |= (LC_VALID | LC_UDP);
380 lph->lph_inetaddr = saddr->sin_addr.s_addr;
381 lph->lph_port = saddr->sin_port;
383 lph->lph_flag |= (LC_VALID | LC_SREF);
389 * Update the lease expiry time and position it in the timer queue correctly.
392 nqsrv_instimeq(struct nqlease *lp, u_int32_t duration)
397 newexpiry = time_second + duration + nqsrv_clockskew;
398 if (lp->lc_expiry == newexpiry)
400 if (lp->lc_timer.cqe_next != 0) {
401 CIRCLEQ_REMOVE(&nqtimerhead, lp, lc_timer);
403 lp->lc_expiry = newexpiry;
406 * Find where in the queue it should be.
408 tlp = nqtimerhead.cqh_last;
409 while (tlp != (void *)&nqtimerhead && tlp->lc_expiry > newexpiry)
410 tlp = tlp->lc_timer.cqe_prev;
412 if (tlp == nqtimerhead.cqh_last)
413 NQSTORENOVRAM(newexpiry);
414 #endif /* HASNVRAM */
415 if (tlp == (void *)&nqtimerhead) {
416 CIRCLEQ_INSERT_HEAD(&nqtimerhead, lp, lc_timer);
418 CIRCLEQ_INSERT_AFTER(&nqtimerhead, tlp, lp, lc_timer);
423 * Compare the requesting host address with the lph entry in the lease.
424 * Return true iff it is the same.
425 * This is somewhat messy due to the union in the nqhost structure.
426 * The local host is indicated by the special value of NQLOCALSLP for slp.
429 nqsrv_cmpnam(struct nfssvc_sock *slp, struct sockaddr *nam, struct nqhost *lph)
431 struct sockaddr_in *saddr;
432 struct sockaddr *addr;
433 union nethostaddr lhaddr;
437 if (slp == NQLOCALSLP) {
438 if (lph->lph_flag & LC_LOCAL)
444 if (nsso && nsso->so_proto->pr_protocol == IPPROTO_UDP) {
449 if (lph->lph_flag & LC_UDP) {
450 ret = netaddr_match(AF_INET, &lph->lph_haddr, addr);
452 if ((lph->lph_slp->ns_flag & SLP_VALID) == 0)
454 saddr = (struct sockaddr_in *)lph->lph_slp->ns_nam;
455 if (saddr->sin_family == AF_INET)
456 lhaddr.had_inetaddr = saddr->sin_addr.s_addr;
458 lhaddr.had_nam = lph->lph_slp->ns_nam;
459 ret = netaddr_match(saddr->sin_family, &lhaddr, addr);
465 * Send out eviction notice messages to all other hosts for the lease.
468 nqsrv_send_eviction(struct vnode *vp, struct nqlease *lp,
469 struct nfssvc_sock *slp, struct sockaddr *nam,
472 struct nqhost *lph = &lp->lc_host;
474 struct nqm *lphnext = lp->lc_morehosts;
475 struct mbuf *m, *mreq, *mb, *mb2, *mheadend;
476 struct sockaddr *nam2;
477 struct sockaddr_in *saddr;
482 int len = 1, ok = 1, i = 0;
484 while (ok && (lph->lph_flag & LC_VALID)) {
485 if (nqsrv_cmpnam(slp, nam, lph)) {
486 lph->lph_flag |= LC_VACATED;
487 } else if ((lph->lph_flag & (LC_LOCAL | LC_VACATED)) == 0) {
492 so = lph->lph_slp->ns_so;
493 if (lph->lph_flag & LC_UDP) {
494 MALLOC(nam2, struct sockaddr *,
495 sizeof *nam2, M_SONAME, M_WAITOK);
496 saddr = (struct sockaddr_in *)nam2;
497 saddr->sin_len = sizeof *saddr;
498 saddr->sin_family = AF_INET;
499 saddr->sin_addr.s_addr = lph->lph_inetaddr;
500 saddr->sin_port = lph->lph_port;
501 } else if (lph->lph_slp->ns_flag & SLP_VALID) {
502 nam2 = (struct sockaddr *)0;
506 sotype = so->so_type;
507 if (so->so_proto->pr_flags & PR_CONNREQUIRED)
508 solockp = &lph->lph_slp->ns_solock;
509 nfsm_reqhead((struct vnode *)0, NQNFSPROC_EVICTED,
510 NFSX_V3FH + NFSX_UNSIGNED);
511 fhp = &nfh.fh_generic;
512 bzero((caddr_t)fhp, sizeof(nfh));
513 fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
514 VFS_VPTOFH(vp, &fhp->fh_fid);
515 nfsm_srvfhtom(fhp, 1);
522 if (siz <= 0 || siz > NFS_MAXPACKET) {
523 printf("mbuf siz=%d\n",siz);
524 panic("Bad nfs svc reply");
526 m = nfsm_rpchead(cred, (NFSMNT_NFSV3 | NFSMNT_NQNFS),
528 RPCAUTH_UNIX, 5 * NFSX_UNSIGNED, (char *)0,
529 0, (char *)NULL, mreq, siz, &mheadend, &xid);
531 * For stream protocols, prepend a Sun RPC
534 if (sotype == SOCK_STREAM) {
535 M_PREPEND(m, NFSX_UNSIGNED, MB_WAIT);
537 printf("nfs_nqlease: M_PREPEND failed\n");
538 *mtod(m, u_int32_t *) = htonl(0x80000000 |
539 (m->m_pkthdr.len - NFSX_UNSIGNED));
542 * nfs_sndlock if PR_CONNREQUIRED XXX
545 if ((lph->lph_flag & LC_UDP) == 0 &&
546 ((lph->lph_slp->ns_flag & SLP_VALID) == 0 ||
547 nfs_slplock(lph->lph_slp, 0) == 0)) {
550 (void) nfs_send(so, nam2, m,
553 nfs_slpunlock(lph->lph_slp);
555 if (lph->lph_flag & LC_UDP)
556 FREE(nam2, M_SONAME);
562 len = LC_MOREHOSTSIZ;
563 lph = lphnext->lpm_hosts;
564 lphnext = lphnext->lpm_next;
573 * Wait for the lease to expire.
574 * This will occur when all clients have sent "vacated" messages to
575 * this server OR when it expires do to timeout.
578 nqsrv_waitfor_expiry(struct nqlease *lp)
586 if (time_second > lp->lc_expiry)
589 lphnext = lp->lc_morehosts;
593 while (ok && (lph->lph_flag & LC_VALID)) {
594 if ((lph->lph_flag & (LC_LOCAL | LC_VACATED)) == 0) {
595 lp->lc_flag |= LC_EXPIREDWANTED;
596 (void) tsleep((caddr_t)&lp->lc_flag, 0, "nqexp", 0);
602 len = LC_MOREHOSTSIZ;
603 lph = lphnext->lpm_hosts;
604 lphnext = lphnext->lpm_next;
613 * Nqnfs server timer that maintains the server lease queue.
614 * Scan the lease queue for expired entries:
615 * - when one is found, wakeup anyone waiting for it
616 * else dequeue and free
623 struct nqlease *nextlp;
624 struct nqm *lphnext, *olphnext;
627 for (lp = nqtimerhead.cqh_first; lp != (void *)&nqtimerhead;
629 if (lp->lc_expiry >= time_second)
631 nextlp = lp->lc_timer.cqe_next;
632 if (lp->lc_flag & LC_EXPIREDWANTED) {
633 lp->lc_flag &= ~LC_EXPIREDWANTED;
634 wakeup((caddr_t)&lp->lc_flag);
635 } else if ((lp->lc_flag & (LC_LOCKED | LC_WANTED)) == 0) {
637 * Make a best effort at keeping a write caching lease long
638 * enough by not deleting it until it has been explicitly
639 * vacated or there have been no writes in the previous
640 * write_slack seconds since expiry and the nfsds are not
641 * all busy. The assumption is that if the nfsds are not
642 * all busy now (no queue of nfs requests), then the client
643 * would have been able to do at least one write to the
644 * file during the last write_slack seconds if it was still
645 * trying to push writes to the server.
647 if ((lp->lc_flag & (LC_WRITE | LC_VACATED)) == LC_WRITE &&
648 ((lp->lc_flag & LC_WRITTEN) || nfsd_waiting == 0)) {
649 lp->lc_flag &= ~LC_WRITTEN;
650 nqsrv_instimeq(lp, nqsrv_writeslack);
652 CIRCLEQ_REMOVE(&nqtimerhead, lp, lc_timer);
653 LIST_REMOVE(lp, lc_hash);
655 * This soft reference may no longer be valid, but
656 * no harm done. The worst case is if the vnode was
657 * recycled and has another valid lease reference,
658 * which is dereferenced prematurely.
660 lp->lc_vp->v_lease = (struct nqlease *)0;
662 lphnext = lp->lc_morehosts;
663 olphnext = (struct nqm *)0;
667 while (ok && (lph->lph_flag & LC_VALID)) {
668 if (lph->lph_flag & LC_SREF)
669 nfsrv_slpderef(lph->lph_slp);
672 free((caddr_t)olphnext, M_NQMHOST);
673 olphnext = (struct nqm *)0;
678 len = LC_MOREHOSTSIZ;
679 lph = lphnext->lpm_hosts;
680 lphnext = lphnext->lpm_next;
686 FREE((caddr_t)lp, M_NQLEASE);
688 free((caddr_t)olphnext, M_NQMHOST);
689 nfsstats.srvnqnfs_leases--;
696 * Called from nfssvc_nfsd() for a getlease rpc request.
697 * Do the from/to xdr translation and call nqsrv_getlease() to
701 nqnfsrv_getlease(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
702 struct thread *td, struct mbuf **mrq)
704 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
705 struct sockaddr *nam = nfsd->nd_nam;
706 caddr_t dpos = nfsd->nd_dpos;
707 struct ucred *cred = &nfsd->nd_cr;
708 struct nfs_fattr *fp;
710 struct vattr *vap = &va;
720 struct mbuf *mb, *mb2, *mreq;
721 int flags, rdonly, cache;
723 fhp = &nfh.fh_generic;
725 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
726 flags = fxdr_unsigned(int, *tl++);
727 nfsd->nd_duration = fxdr_unsigned(int, *tl);
728 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly,
729 (nfsd->nd_flag & ND_KERBAUTH), TRUE);
734 if (rdonly && flags == ND_WRITE) {
739 (void) nqsrv_getlease(vp, &nfsd->nd_duration, flags, slp, td,
740 nam, &cache, &frev, cred);
741 error = VOP_GETATTR(vp, vap, td);
743 nfsm_reply(NFSX_V3FATTR + 4 * NFSX_UNSIGNED);
744 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
745 *tl++ = txdr_unsigned(cache);
746 *tl++ = txdr_unsigned(nfsd->nd_duration);
747 txdr_hyper(frev, tl);
748 nfsm_build(fp, struct nfs_fattr *, NFSX_V3FATTR);
749 nfsm_srvfillattr(vap, fp);
754 * Called from nfssvc_nfsd() when a "vacated" message is received from a
755 * client. Find the entry and expire it.
758 nqnfsrv_vacated(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
759 struct thread *td, struct mbuf **mrq)
761 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
762 struct sockaddr *nam = nfsd->nd_nam;
763 caddr_t dpos = nfsd->nd_dpos;
766 struct nqlease *tlp = (struct nqlease *)0;
772 struct mbuf *mreq, *mb;
773 int error = 0, i, len, ok, gotit = 0, cache = 0;
777 fhp = &nfh.fh_generic;
781 * Find the lease by searching the hash list.
783 for (lp = NQFHHASH(fhp->fh_fid.fid_data)->lh_first; lp != 0;
784 lp = lp->lc_hash.le_next)
785 if (fhp->fh_fsid.val[0] == lp->lc_fsid.val[0] &&
786 fhp->fh_fsid.val[1] == lp->lc_fsid.val[1] &&
787 !bcmp(fhp->fh_fid.fid_data, lp->lc_fiddata,
798 lphnext = lp->lc_morehosts;
800 while (ok && (lph->lph_flag & LC_VALID)) {
801 if (nqsrv_cmpnam(slp, nam, lph)) {
802 lph->lph_flag |= LC_VACATED;
808 len = LC_MOREHOSTSIZ;
810 lph = lphnext->lpm_hosts;
811 lphnext = lphnext->lpm_next;
817 if ((lp->lc_flag & LC_EXPIREDWANTED) && gotit) {
818 lp->lc_flag &= ~LC_EXPIREDWANTED;
819 wakeup((caddr_t)&lp->lc_flag);
827 #endif /* NFS_NOSERVER */
830 * Client get lease rpc function.
833 nqnfs_getlease(struct vnode *vp, int rwflag, struct thread *td)
839 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
840 caddr_t bpos, dpos, cp2;
843 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
847 nfsstats.rpccnt[NQNFSPROC_GETLEASE]++;
848 mb = mreq = nfsm_reqh(vp, NQNFSPROC_GETLEASE, NFSX_V3FH+2*NFSX_UNSIGNED,
851 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
852 *tl++ = txdr_unsigned(rwflag);
853 *tl = txdr_unsigned(nmp->nm_leaseterm);
854 reqtime = time_second;
855 nfsm_request(vp, NQNFSPROC_GETLEASE, td, nfs_vpcred(vp, rwflag));
857 nfsm_dissect(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
858 cachable = fxdr_unsigned(int, *tl++);
859 reqtime += fxdr_unsigned(int, *tl++);
860 if (reqtime > time_second) {
861 frev = fxdr_hyper(tl);
862 nqnfs_clientlease(nmp, np, rwflag, cachable, reqtime, frev);
863 nfsm_loadattr(vp, (struct vattr *)0);
865 error = NQNFS_EXPIRED;
874 * Client vacated message function.
877 nqnfs_vacated(struct vnode *vp, struct ucred *cred)
886 struct mbuf *m, *mreq, *mb, *mb2, *mheadend;
887 struct nfsmount *nmp;
890 nmp = VFSTONFS(vp->v_mount);
891 nfsstats.rpccnt[NQNFSPROC_VACATED]++;
892 nfsm_reqhead(vp, NQNFSPROC_VACATED, NFSX_FH(1));
900 m = nfsm_rpchead(cred, nmp->nm_flag, NQNFSPROC_VACATED,
901 RPCAUTH_UNIX, 5 * NFSX_UNSIGNED, (char *)0,
902 0, (char *)NULL, mreq, i, &mheadend, &xid);
903 if (nmp->nm_sotype == SOCK_STREAM) {
904 M_PREPEND(m, NFSX_UNSIGNED, MB_WAIT);
907 *mtod(m, u_int32_t *) = htonl(0x80000000 | (m->m_pkthdr.len -
913 if (nmp->nm_soflags & PR_CONNREQUIRED)
914 (void) nfs_sndlock(&myrep);
915 (void) nfs_send(nmp->nm_so, nmp->nm_nam, m, &myrep);
916 if (nmp->nm_soflags & PR_CONNREQUIRED)
917 nfs_sndunlock(&myrep);
923 * Called for client side callbacks
926 nqnfs_callback(struct nfsmount *nmp, struct mbuf *mrep, struct mbuf *md,
936 struct nfssvc_sock *slp;
937 struct nfsrv_descript ndesc;
938 struct nfsrv_descript *nfsd = &ndesc;
939 struct mbuf **mrq = (struct mbuf **)0, *mb, *mreq;
940 int error = 0, cache = 0;
947 nfsd->nd_mrep = mrep;
949 nfsd->nd_dpos = dpos;
950 error = nfs_getreq(nfsd, &tnfsd, FALSE);
954 dpos = nfsd->nd_dpos;
955 if (nfsd->nd_procnum != NQNFSPROC_EVICTED) {
959 fhp = &nfh.fh_generic;
962 error = nfs_nget(nmp->nm_mountp, (nfsfh_t *)fhp, NFSX_V3FH, &np);
966 if (np->n_timer.cqe_next != 0) {
968 np->n_flag |= NQNFSEVICTED;
969 if (nmp->nm_timerhead.cqh_first != np) {
970 CIRCLEQ_REMOVE(&nmp->nm_timerhead, np, n_timer);
971 CIRCLEQ_INSERT_HEAD(&nmp->nm_timerhead, np, n_timer);
980 * Nqnfs client helper daemon. Runs once a second to expire leases.
981 * It also get authorization strings for "kerb" mounts.
982 * It must start at the beginning of the list again after any potential
983 * "sleep" since nfs_reclaim() called from vclean() can pull a node off
984 * the list asynchronously.
987 nqnfs_clientd(struct nfsmount *nmp, struct ucred *cred, struct nfsd_cargs *ncd,
988 int flag, caddr_t argp, struct thread *td)
993 struct nfsuid *nuidp, *nnuidp;
997 * First initialize some variables
1001 * If an authorization string is being passed in, get it.
1003 if ((flag & NFSSVC_GOTAUTH) &&
1004 (nmp->nm_state & (NFSSTA_WAITAUTH | NFSSTA_DISMNT)) == 0) {
1005 if (nmp->nm_state & NFSSTA_HASAUTH)
1007 if ((flag & NFSSVC_AUTHINFAIL) == 0) {
1008 if (ncd->ncd_authlen <= nmp->nm_authlen &&
1009 ncd->ncd_verflen <= nmp->nm_verflen &&
1010 !copyin(ncd->ncd_authstr,nmp->nm_authstr,ncd->ncd_authlen)&&
1011 !copyin(ncd->ncd_verfstr,nmp->nm_verfstr,ncd->ncd_verflen)){
1012 nmp->nm_authtype = ncd->ncd_authtype;
1013 nmp->nm_authlen = ncd->ncd_authlen;
1014 nmp->nm_verflen = ncd->ncd_verflen;
1016 nmp->nm_key = ncd->ncd_key;
1019 nmp->nm_state |= NFSSTA_AUTHERR;
1021 nmp->nm_state |= NFSSTA_AUTHERR;
1022 nmp->nm_state |= NFSSTA_HASAUTH;
1023 wakeup((caddr_t)&nmp->nm_authlen);
1025 nmp->nm_state |= NFSSTA_WAITAUTH;
1028 * Loop every second updating queue until there is a termination sig.
1030 while ((nmp->nm_state & NFSSTA_DISMNT) == 0) {
1031 if (nmp->nm_flag & NFSMNT_NQNFS) {
1033 * If there are no outstanding requests (and therefore no
1034 * processes in nfs_reply) and there is data in the receive
1035 * queue, poke for callbacks.
1037 if (TAILQ_EMPTY(&nfs_reqq) && nmp->nm_so &&
1038 nmp->nm_so->so_rcv.sb_cc > 0) {
1039 myrep.r_flags = R_GETONEREP;
1041 myrep.r_mrep = (struct mbuf *)0;
1047 * Loop through the leases, updating as required.
1049 np = nmp->nm_timerhead.cqh_first;
1050 while (np != (void *)&nmp->nm_timerhead &&
1051 (nmp->nm_state & NFSSTA_DISMINPROG) == 0) {
1054 if (np->n_expiry < time_second) {
1055 if (vget(vp, LK_EXCLUSIVE, td) == 0) {
1056 nmp->nm_inprog = vp;
1057 if (vpid == vp->v_id) {
1058 CIRCLEQ_REMOVE(&nmp->nm_timerhead, np, n_timer);
1059 np->n_timer.cqe_next = 0;
1060 if (np->n_flag & (NLMODIFIED | NQNFSEVICTED)) {
1061 if (np->n_flag & NQNFSEVICTED) {
1062 if (vp->v_type == VDIR)
1064 cache_inval_vp(vp, 0);
1065 (void) nfs_vinvalbuf(vp,
1067 np->n_flag &= ~NQNFSEVICTED;
1068 (void) nqnfs_vacated(vp, cred);
1069 } else if (vp->v_type == VREG) {
1070 (void) VOP_FSYNC(vp, MNT_WAIT, td);
1071 np->n_flag &= ~NLMODIFIED;
1076 nmp->nm_inprog = NULLVP;
1078 } else if ((np->n_expiry - NQ_RENEWAL) < time_second) {
1079 if ((np->n_flag & (NQNFSWRITE | NQNFSNONCACHE))
1081 !RB_EMPTY(&vp->v_rbdirty_tree) &&
1082 vget(vp, LK_EXCLUSIVE, td) == 0) {
1083 nmp->nm_inprog = vp;
1084 if (vpid == vp->v_id &&
1085 nqnfs_getlease(vp, ND_WRITE, td)==0)
1086 np->n_brev = np->n_lrev;
1088 nmp->nm_inprog = NULLVP;
1092 if (np == nmp->nm_timerhead.cqh_first)
1094 np = nmp->nm_timerhead.cqh_first;
1099 * Get an authorization string, if required.
1101 if ((nmp->nm_state & (NFSSTA_WAITAUTH | NFSSTA_DISMNT | NFSSTA_HASAUTH)) == 0) {
1102 ncd->ncd_authuid = nmp->nm_authuid;
1103 if (copyout((caddr_t)ncd, argp, sizeof (struct nfsd_cargs)))
1104 nmp->nm_state |= NFSSTA_WAITAUTH;
1110 * Wait a bit (no pun) and do it again.
1112 if ((nmp->nm_state & NFSSTA_DISMNT) == 0 &&
1113 (nmp->nm_state & (NFSSTA_WAITAUTH | NFSSTA_HASAUTH))) {
1114 error = tsleep((caddr_t)&nmp->nm_authstr, PCATCH,
1115 "nqnfstimr", hz / 3);
1116 if (error == EINTR || error == ERESTART)
1117 (void) dounmount(nmp->nm_mountp, 0, td);
1122 * Finally, we can free up the mount structure.
1124 TAILQ_FOREACH_MUTABLE(nuidp, &nmp->nm_uidlruhead, nu_lru, nnuidp) {
1125 LIST_REMOVE(nuidp, nu_hash);
1126 TAILQ_REMOVE(&nmp->nm_uidlruhead, nuidp, nu_lru);
1127 free((caddr_t)nuidp, M_NFSUID);
1129 nfs_free_mount(nmp);
1130 if (error == EWOULDBLOCK)
1135 #endif /* NFS_NOSERVER */
1138 * Adjust all timer queue expiry times when the time of day clock is changed.
1139 * Called from the settimeofday() syscall.
1142 nqnfs_lease_updatetime(int deltat)
1144 struct thread *td = curthread; /* XXX */
1147 struct mount *mp, *nxtmp;
1148 struct nfsmount *nmp;
1152 if (nqnfsstarttime != 0)
1153 nqnfsstarttime += deltat;
1155 for (lp = nqtimerhead.cqh_first; lp != (void *)&nqtimerhead;
1156 lp = lp->lc_timer.cqe_next)
1157 lp->lc_expiry += deltat;
1161 * Search the mount list for all nqnfs mounts and do their timer
1164 lwkt_gettoken(&ilock, &mountlist_token);
1165 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nxtmp) {
1166 if (vfs_busy(mp, LK_NOWAIT, &ilock, td)) {
1167 nxtmp = TAILQ_NEXT(mp, mnt_list);
1170 if (mp->mnt_stat.f_type == nfs_mount_type) {
1172 if (nmp->nm_flag & NFSMNT_NQNFS) {
1173 for (np = nmp->nm_timerhead.cqh_first;
1174 np != (void *)&nmp->nm_timerhead;
1175 np = np->n_timer.cqe_next) {
1176 np->n_expiry += deltat;
1180 lwkt_gettokref(&ilock);
1181 nxtmp = TAILQ_NEXT(mp, mnt_list);
1184 lwkt_reltoken(&ilock);
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);