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