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