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