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