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