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