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