1 /* 2 * Copyright (c) 1989, 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_subs.c 8.3 (Berkeley) 01/04/94 11 */ 12 13 /* 14 * These functions support the macros and help fiddle mbuf chains for 15 * the nfs op functions. They do things like create the rpc header and 16 * copy data between mbuf chains and uio lists. 17 */ 18 #include <sys/param.h> 19 #include <sys/proc.h> 20 #include <sys/systm.h> 21 #include <sys/kernel.h> 22 #include <sys/mount.h> 23 #include <sys/vnode.h> 24 #include <sys/namei.h> 25 #include <sys/mbuf.h> 26 #include <sys/socket.h> 27 #include <sys/stat.h> 28 29 #include <nfs/rpcv2.h> 30 #include <nfs/nfsv2.h> 31 #include <nfs/nfsnode.h> 32 #include <nfs/nfs.h> 33 #include <nfs/xdr_subs.h> 34 #include <nfs/nfsm_subs.h> 35 #include <nfs/nfsmount.h> 36 #include <nfs/nqnfs.h> 37 #include <nfs/nfsrtt.h> 38 39 #include <miscfs/specfs/specdev.h> 40 41 #include <netinet/in.h> 42 #ifdef ISO 43 #include <netiso/iso.h> 44 #endif 45 46 #define TRUE 1 47 #define FALSE 0 48 49 /* 50 * Data items converted to xdr at startup, since they are constant 51 * This is kinda hokey, but may save a little time doing byte swaps 52 */ 53 u_long nfs_procids[NFS_NPROCS]; 54 u_long nfs_xdrneg1; 55 u_long rpc_call, rpc_vers, rpc_reply, rpc_msgdenied, rpc_autherr, 56 rpc_mismatch, rpc_auth_unix, rpc_msgaccepted, rpc_rejectedcred, 57 rpc_auth_kerb; 58 u_long nfs_vers, nfs_prog, nfs_true, nfs_false; 59 60 /* And other global data */ 61 static u_long nfs_xid = 0; 62 enum vtype ntov_type[7] = { VNON, VREG, VDIR, VBLK, VCHR, VLNK, VNON }; 63 extern struct proc *nfs_iodwant[NFS_MAXASYNCDAEMON]; 64 extern struct nfsreq nfsreqh; 65 extern int nqnfs_piggy[NFS_NPROCS]; 66 extern struct nfsrtt nfsrtt; 67 extern time_t nqnfsstarttime; 68 extern u_long nqnfs_prog, nqnfs_vers; 69 extern int nqsrv_clockskew; 70 extern int nqsrv_writeslack; 71 extern int nqsrv_maxlease; 72 73 /* 74 * Create the header for an rpc request packet 75 * The hsiz is the size of the rest of the nfs request header. 76 * (just used to decide if a cluster is a good idea) 77 */ 78 struct mbuf * 79 nfsm_reqh(vp, procid, hsiz, bposp) 80 struct vnode *vp; 81 u_long procid; 82 int hsiz; 83 caddr_t *bposp; 84 { 85 register struct mbuf *mb; 86 register u_long *tl; 87 register caddr_t bpos; 88 struct mbuf *mb2; 89 struct nfsmount *nmp; 90 int nqflag; 91 92 MGET(mb, M_WAIT, MT_DATA); 93 if (hsiz >= MINCLSIZE) 94 MCLGET(mb, M_WAIT); 95 mb->m_len = 0; 96 bpos = mtod(mb, caddr_t); 97 98 /* 99 * For NQNFS, add lease request. 100 */ 101 if (vp) { 102 nmp = VFSTONFS(vp->v_mount); 103 if (nmp->nm_flag & NFSMNT_NQNFS) { 104 nqflag = NQNFS_NEEDLEASE(vp, procid); 105 if (nqflag) { 106 nfsm_build(tl, u_long *, 2*NFSX_UNSIGNED); 107 *tl++ = txdr_unsigned(nqflag); 108 *tl = txdr_unsigned(nmp->nm_leaseterm); 109 } else { 110 nfsm_build(tl, u_long *, NFSX_UNSIGNED); 111 *tl = 0; 112 } 113 } 114 } 115 /* Finally, return values */ 116 *bposp = bpos; 117 return (mb); 118 } 119 120 /* 121 * Build the RPC header and fill in the authorization info. 122 * The authorization string argument is only used when the credentials 123 * come from outside of the kernel. 124 * Returns the head of the mbuf list. 125 */ 126 struct mbuf * 127 nfsm_rpchead(cr, nqnfs, procid, auth_type, auth_len, auth_str, mrest, 128 mrest_len, mbp, xidp) 129 register struct ucred *cr; 130 int nqnfs; 131 int procid; 132 int auth_type; 133 int auth_len; 134 char *auth_str; 135 struct mbuf *mrest; 136 int mrest_len; 137 struct mbuf **mbp; 138 u_long *xidp; 139 { 140 register struct mbuf *mb; 141 register u_long *tl; 142 register caddr_t bpos; 143 register int i; 144 struct mbuf *mreq, *mb2; 145 int siz, grpsiz, authsiz; 146 147 authsiz = nfsm_rndup(auth_len); 148 if (auth_type == RPCAUTH_NQNFS) 149 authsiz += 2 * NFSX_UNSIGNED; 150 MGETHDR(mb, M_WAIT, MT_DATA); 151 if ((authsiz + 10*NFSX_UNSIGNED) >= MINCLSIZE) { 152 MCLGET(mb, M_WAIT); 153 } else if ((authsiz + 10*NFSX_UNSIGNED) < MHLEN) { 154 MH_ALIGN(mb, authsiz + 10*NFSX_UNSIGNED); 155 } else { 156 MH_ALIGN(mb, 8*NFSX_UNSIGNED); 157 } 158 mb->m_len = 0; 159 mreq = mb; 160 bpos = mtod(mb, caddr_t); 161 162 /* 163 * First the RPC header. 164 */ 165 nfsm_build(tl, u_long *, 8*NFSX_UNSIGNED); 166 if (++nfs_xid == 0) 167 nfs_xid++; 168 *tl++ = *xidp = txdr_unsigned(nfs_xid); 169 *tl++ = rpc_call; 170 *tl++ = rpc_vers; 171 if (nqnfs) { 172 *tl++ = txdr_unsigned(NQNFS_PROG); 173 *tl++ = txdr_unsigned(NQNFS_VER1); 174 } else { 175 *tl++ = txdr_unsigned(NFS_PROG); 176 *tl++ = txdr_unsigned(NFS_VER2); 177 } 178 *tl++ = txdr_unsigned(procid); 179 180 /* 181 * And then the authorization cred. 182 */ 183 *tl++ = txdr_unsigned(auth_type); 184 *tl = txdr_unsigned(authsiz); 185 switch (auth_type) { 186 case RPCAUTH_UNIX: 187 nfsm_build(tl, u_long *, auth_len); 188 *tl++ = 0; /* stamp ?? */ 189 *tl++ = 0; /* NULL hostname */ 190 *tl++ = txdr_unsigned(cr->cr_uid); 191 *tl++ = txdr_unsigned(cr->cr_groups[0]); 192 grpsiz = (auth_len >> 2) - 5; 193 *tl++ = txdr_unsigned(grpsiz); 194 for (i = 1; i <= grpsiz; i++) 195 *tl++ = txdr_unsigned(cr->cr_groups[i]); 196 break; 197 case RPCAUTH_NQNFS: 198 nfsm_build(tl, u_long *, 2*NFSX_UNSIGNED); 199 *tl++ = txdr_unsigned(cr->cr_uid); 200 *tl = txdr_unsigned(auth_len); 201 siz = auth_len; 202 while (siz > 0) { 203 if (M_TRAILINGSPACE(mb) == 0) { 204 MGET(mb2, M_WAIT, MT_DATA); 205 if (siz >= MINCLSIZE) 206 MCLGET(mb2, M_WAIT); 207 mb->m_next = mb2; 208 mb = mb2; 209 mb->m_len = 0; 210 bpos = mtod(mb, caddr_t); 211 } 212 i = min(siz, M_TRAILINGSPACE(mb)); 213 bcopy(auth_str, bpos, i); 214 mb->m_len += i; 215 auth_str += i; 216 bpos += i; 217 siz -= i; 218 } 219 if ((siz = (nfsm_rndup(auth_len) - auth_len)) > 0) { 220 for (i = 0; i < siz; i++) 221 *bpos++ = '\0'; 222 mb->m_len += siz; 223 } 224 break; 225 }; 226 nfsm_build(tl, u_long *, 2*NFSX_UNSIGNED); 227 *tl++ = txdr_unsigned(RPCAUTH_NULL); 228 *tl = 0; 229 mb->m_next = mrest; 230 mreq->m_pkthdr.len = authsiz + 10*NFSX_UNSIGNED + mrest_len; 231 mreq->m_pkthdr.rcvif = (struct ifnet *)0; 232 *mbp = mb; 233 return (mreq); 234 } 235 236 /* 237 * copies mbuf chain to the uio scatter/gather list 238 */ 239 nfsm_mbuftouio(mrep, uiop, siz, dpos) 240 struct mbuf **mrep; 241 register struct uio *uiop; 242 int siz; 243 caddr_t *dpos; 244 { 245 register char *mbufcp, *uiocp; 246 register int xfer, left, len; 247 register struct mbuf *mp; 248 long uiosiz, rem; 249 int error = 0; 250 251 mp = *mrep; 252 mbufcp = *dpos; 253 len = mtod(mp, caddr_t)+mp->m_len-mbufcp; 254 rem = nfsm_rndup(siz)-siz; 255 while (siz > 0) { 256 if (uiop->uio_iovcnt <= 0 || uiop->uio_iov == NULL) 257 return (EFBIG); 258 left = uiop->uio_iov->iov_len; 259 uiocp = uiop->uio_iov->iov_base; 260 if (left > siz) 261 left = siz; 262 uiosiz = left; 263 while (left > 0) { 264 while (len == 0) { 265 mp = mp->m_next; 266 if (mp == NULL) 267 return (EBADRPC); 268 mbufcp = mtod(mp, caddr_t); 269 len = mp->m_len; 270 } 271 xfer = (left > len) ? len : left; 272 #ifdef notdef 273 /* Not Yet.. */ 274 if (uiop->uio_iov->iov_op != NULL) 275 (*(uiop->uio_iov->iov_op)) 276 (mbufcp, uiocp, xfer); 277 else 278 #endif 279 if (uiop->uio_segflg == UIO_SYSSPACE) 280 bcopy(mbufcp, uiocp, xfer); 281 else 282 copyout(mbufcp, uiocp, xfer); 283 left -= xfer; 284 len -= xfer; 285 mbufcp += xfer; 286 uiocp += xfer; 287 uiop->uio_offset += xfer; 288 uiop->uio_resid -= xfer; 289 } 290 if (uiop->uio_iov->iov_len <= siz) { 291 uiop->uio_iovcnt--; 292 uiop->uio_iov++; 293 } else { 294 uiop->uio_iov->iov_base += uiosiz; 295 uiop->uio_iov->iov_len -= uiosiz; 296 } 297 siz -= uiosiz; 298 } 299 *dpos = mbufcp; 300 *mrep = mp; 301 if (rem > 0) { 302 if (len < rem) 303 error = nfs_adv(mrep, dpos, rem, len); 304 else 305 *dpos += rem; 306 } 307 return (error); 308 } 309 310 /* 311 * copies a uio scatter/gather list to an mbuf chain... 312 */ 313 nfsm_uiotombuf(uiop, mq, siz, bpos) 314 register struct uio *uiop; 315 struct mbuf **mq; 316 int siz; 317 caddr_t *bpos; 318 { 319 register char *uiocp; 320 register struct mbuf *mp, *mp2; 321 register int xfer, left, mlen; 322 int uiosiz, clflg, rem; 323 char *cp; 324 325 if (siz > MLEN) /* or should it >= MCLBYTES ?? */ 326 clflg = 1; 327 else 328 clflg = 0; 329 rem = nfsm_rndup(siz)-siz; 330 mp = mp2 = *mq; 331 while (siz > 0) { 332 if (uiop->uio_iovcnt <= 0 || uiop->uio_iov == NULL) 333 return (EINVAL); 334 left = uiop->uio_iov->iov_len; 335 uiocp = uiop->uio_iov->iov_base; 336 if (left > siz) 337 left = siz; 338 uiosiz = left; 339 while (left > 0) { 340 mlen = M_TRAILINGSPACE(mp); 341 if (mlen == 0) { 342 MGET(mp, M_WAIT, MT_DATA); 343 if (clflg) 344 MCLGET(mp, M_WAIT); 345 mp->m_len = 0; 346 mp2->m_next = mp; 347 mp2 = mp; 348 mlen = M_TRAILINGSPACE(mp); 349 } 350 xfer = (left > mlen) ? mlen : left; 351 #ifdef notdef 352 /* Not Yet.. */ 353 if (uiop->uio_iov->iov_op != NULL) 354 (*(uiop->uio_iov->iov_op)) 355 (uiocp, mtod(mp, caddr_t)+mp->m_len, xfer); 356 else 357 #endif 358 if (uiop->uio_segflg == UIO_SYSSPACE) 359 bcopy(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer); 360 else 361 copyin(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer); 362 mp->m_len += xfer; 363 left -= xfer; 364 uiocp += xfer; 365 uiop->uio_offset += xfer; 366 uiop->uio_resid -= xfer; 367 } 368 if (uiop->uio_iov->iov_len <= siz) { 369 uiop->uio_iovcnt--; 370 uiop->uio_iov++; 371 } else { 372 uiop->uio_iov->iov_base += uiosiz; 373 uiop->uio_iov->iov_len -= uiosiz; 374 } 375 siz -= uiosiz; 376 } 377 if (rem > 0) { 378 if (rem > M_TRAILINGSPACE(mp)) { 379 MGET(mp, M_WAIT, MT_DATA); 380 mp->m_len = 0; 381 mp2->m_next = mp; 382 } 383 cp = mtod(mp, caddr_t)+mp->m_len; 384 for (left = 0; left < rem; left++) 385 *cp++ = '\0'; 386 mp->m_len += rem; 387 *bpos = cp; 388 } else 389 *bpos = mtod(mp, caddr_t)+mp->m_len; 390 *mq = mp; 391 return (0); 392 } 393 394 /* 395 * Help break down an mbuf chain by setting the first siz bytes contiguous 396 * pointed to by returned val. 397 * This is used by the macros nfsm_dissect and nfsm_dissecton for tough 398 * cases. (The macros use the vars. dpos and dpos2) 399 */ 400 nfsm_disct(mdp, dposp, siz, left, cp2) 401 struct mbuf **mdp; 402 caddr_t *dposp; 403 int siz; 404 int left; 405 caddr_t *cp2; 406 { 407 register struct mbuf *mp, *mp2; 408 register int siz2, xfer; 409 register caddr_t p; 410 411 mp = *mdp; 412 while (left == 0) { 413 *mdp = mp = mp->m_next; 414 if (mp == NULL) 415 return (EBADRPC); 416 left = mp->m_len; 417 *dposp = mtod(mp, caddr_t); 418 } 419 if (left >= siz) { 420 *cp2 = *dposp; 421 *dposp += siz; 422 } else if (mp->m_next == NULL) { 423 return (EBADRPC); 424 } else if (siz > MHLEN) { 425 panic("nfs S too big"); 426 } else { 427 MGET(mp2, M_WAIT, MT_DATA); 428 mp2->m_next = mp->m_next; 429 mp->m_next = mp2; 430 mp->m_len -= left; 431 mp = mp2; 432 *cp2 = p = mtod(mp, caddr_t); 433 bcopy(*dposp, p, left); /* Copy what was left */ 434 siz2 = siz-left; 435 p += left; 436 mp2 = mp->m_next; 437 /* Loop around copying up the siz2 bytes */ 438 while (siz2 > 0) { 439 if (mp2 == NULL) 440 return (EBADRPC); 441 xfer = (siz2 > mp2->m_len) ? mp2->m_len : siz2; 442 if (xfer > 0) { 443 bcopy(mtod(mp2, caddr_t), p, xfer); 444 NFSMADV(mp2, xfer); 445 mp2->m_len -= xfer; 446 p += xfer; 447 siz2 -= xfer; 448 } 449 if (siz2 > 0) 450 mp2 = mp2->m_next; 451 } 452 mp->m_len = siz; 453 *mdp = mp2; 454 *dposp = mtod(mp2, caddr_t); 455 } 456 return (0); 457 } 458 459 /* 460 * Advance the position in the mbuf chain. 461 */ 462 nfs_adv(mdp, dposp, offs, left) 463 struct mbuf **mdp; 464 caddr_t *dposp; 465 int offs; 466 int left; 467 { 468 register struct mbuf *m; 469 register int s; 470 471 m = *mdp; 472 s = left; 473 while (s < offs) { 474 offs -= s; 475 m = m->m_next; 476 if (m == NULL) 477 return (EBADRPC); 478 s = m->m_len; 479 } 480 *mdp = m; 481 *dposp = mtod(m, caddr_t)+offs; 482 return (0); 483 } 484 485 /* 486 * Copy a string into mbufs for the hard cases... 487 */ 488 nfsm_strtmbuf(mb, bpos, cp, siz) 489 struct mbuf **mb; 490 char **bpos; 491 char *cp; 492 long siz; 493 { 494 register struct mbuf *m1, *m2; 495 long left, xfer, len, tlen; 496 u_long *tl; 497 int putsize; 498 499 putsize = 1; 500 m2 = *mb; 501 left = M_TRAILINGSPACE(m2); 502 if (left > 0) { 503 tl = ((u_long *)(*bpos)); 504 *tl++ = txdr_unsigned(siz); 505 putsize = 0; 506 left -= NFSX_UNSIGNED; 507 m2->m_len += NFSX_UNSIGNED; 508 if (left > 0) { 509 bcopy(cp, (caddr_t) tl, left); 510 siz -= left; 511 cp += left; 512 m2->m_len += left; 513 left = 0; 514 } 515 } 516 /* Loop around adding mbufs */ 517 while (siz > 0) { 518 MGET(m1, M_WAIT, MT_DATA); 519 if (siz > MLEN) 520 MCLGET(m1, M_WAIT); 521 m1->m_len = NFSMSIZ(m1); 522 m2->m_next = m1; 523 m2 = m1; 524 tl = mtod(m1, u_long *); 525 tlen = 0; 526 if (putsize) { 527 *tl++ = txdr_unsigned(siz); 528 m1->m_len -= NFSX_UNSIGNED; 529 tlen = NFSX_UNSIGNED; 530 putsize = 0; 531 } 532 if (siz < m1->m_len) { 533 len = nfsm_rndup(siz); 534 xfer = siz; 535 if (xfer < len) 536 *(tl+(xfer>>2)) = 0; 537 } else { 538 xfer = len = m1->m_len; 539 } 540 bcopy(cp, (caddr_t) tl, xfer); 541 m1->m_len = len+tlen; 542 siz -= xfer; 543 cp += xfer; 544 } 545 *mb = m1; 546 *bpos = mtod(m1, caddr_t)+m1->m_len; 547 return (0); 548 } 549 550 /* 551 * Called once to initialize data structures... 552 */ 553 nfs_init() 554 { 555 register int i; 556 557 nfsrtt.pos = 0; 558 rpc_vers = txdr_unsigned(RPC_VER2); 559 rpc_call = txdr_unsigned(RPC_CALL); 560 rpc_reply = txdr_unsigned(RPC_REPLY); 561 rpc_msgdenied = txdr_unsigned(RPC_MSGDENIED); 562 rpc_msgaccepted = txdr_unsigned(RPC_MSGACCEPTED); 563 rpc_mismatch = txdr_unsigned(RPC_MISMATCH); 564 rpc_autherr = txdr_unsigned(RPC_AUTHERR); 565 rpc_rejectedcred = txdr_unsigned(AUTH_REJECTCRED); 566 rpc_auth_unix = txdr_unsigned(RPCAUTH_UNIX); 567 rpc_auth_kerb = txdr_unsigned(RPCAUTH_NQNFS); 568 nfs_vers = txdr_unsigned(NFS_VER2); 569 nfs_prog = txdr_unsigned(NFS_PROG); 570 nfs_true = txdr_unsigned(TRUE); 571 nfs_false = txdr_unsigned(FALSE); 572 /* Loop thru nfs procids */ 573 for (i = 0; i < NFS_NPROCS; i++) 574 nfs_procids[i] = txdr_unsigned(i); 575 /* Ensure async daemons disabled */ 576 for (i = 0; i < NFS_MAXASYNCDAEMON; i++) 577 nfs_iodwant[i] = (struct proc *)0; 578 TAILQ_INIT(&nfs_bufq); 579 nfs_xdrneg1 = txdr_unsigned(-1); 580 nfs_nhinit(); /* Init the nfsnode table */ 581 nfsrv_init(0); /* Init server data structures */ 582 nfsrv_initcache(); /* Init the server request cache */ 583 584 /* 585 * Initialize the nqnfs server stuff. 586 */ 587 if (nqnfsstarttime == 0) { 588 nqnfsstarttime = boottime.tv_sec + nqsrv_maxlease 589 + nqsrv_clockskew + nqsrv_writeslack; 590 NQLOADNOVRAM(nqnfsstarttime); 591 nqnfs_prog = txdr_unsigned(NQNFS_PROG); 592 nqnfs_vers = txdr_unsigned(NQNFS_VER1); 593 nqthead.th_head[0] = &nqthead; 594 nqthead.th_head[1] = &nqthead; 595 nqfhead = hashinit(NQLCHSZ, M_NQLEASE, &nqfheadhash); 596 } 597 598 /* 599 * Initialize reply list and start timer 600 */ 601 nfsreqh.r_prev = nfsreqh.r_next = &nfsreqh; 602 nfs_timer(); 603 } 604 605 /* 606 * Attribute cache routines. 607 * nfs_loadattrcache() - loads or updates the cache contents from attributes 608 * that are on the mbuf list 609 * nfs_getattrcache() - returns valid attributes if found in cache, returns 610 * error otherwise 611 */ 612 613 /* 614 * Load the attribute cache (that lives in the nfsnode entry) with 615 * the values on the mbuf list and 616 * Iff vap not NULL 617 * copy the attributes to *vaper 618 */ 619 nfs_loadattrcache(vpp, mdp, dposp, vaper) 620 struct vnode **vpp; 621 struct mbuf **mdp; 622 caddr_t *dposp; 623 struct vattr *vaper; 624 { 625 register struct vnode *vp = *vpp; 626 register struct vattr *vap; 627 register struct nfsv2_fattr *fp; 628 extern int (**spec_nfsv2nodeop_p)(); 629 register struct nfsnode *np, *nq, **nhpp; 630 register long t1; 631 caddr_t dpos, cp2; 632 int error = 0, isnq; 633 struct mbuf *md; 634 enum vtype vtyp; 635 u_short vmode; 636 long rdev; 637 struct timespec mtime; 638 struct vnode *nvp; 639 640 md = *mdp; 641 dpos = *dposp; 642 t1 = (mtod(md, caddr_t) + md->m_len) - dpos; 643 isnq = (VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NQNFS); 644 if (error = nfsm_disct(&md, &dpos, NFSX_FATTR(isnq), t1, &cp2)) 645 return (error); 646 fp = (struct nfsv2_fattr *)cp2; 647 vtyp = nfstov_type(fp->fa_type); 648 vmode = fxdr_unsigned(u_short, fp->fa_mode); 649 if (vtyp == VNON || vtyp == VREG) 650 vtyp = IFTOVT(vmode); 651 if (isnq) { 652 rdev = fxdr_unsigned(long, fp->fa_nqrdev); 653 fxdr_nqtime(&fp->fa_nqmtime, &mtime); 654 } else { 655 rdev = fxdr_unsigned(long, fp->fa_nfsrdev); 656 fxdr_nfstime(&fp->fa_nfsmtime, &mtime); 657 } 658 /* 659 * If v_type == VNON it is a new node, so fill in the v_type, 660 * n_mtime fields. Check to see if it represents a special 661 * device, and if so, check for a possible alias. Once the 662 * correct vnode has been obtained, fill in the rest of the 663 * information. 664 */ 665 np = VTONFS(vp); 666 if (vp->v_type == VNON) { 667 if (vtyp == VCHR && rdev == 0xffffffff) 668 vp->v_type = vtyp = VFIFO; 669 else 670 vp->v_type = vtyp; 671 if (vp->v_type == VFIFO) { 672 #ifdef FIFO 673 extern int (**fifo_nfsv2nodeop_p)(); 674 vp->v_op = fifo_nfsv2nodeop_p; 675 #else 676 return (EOPNOTSUPP); 677 #endif /* FIFO */ 678 } 679 if (vp->v_type == VCHR || vp->v_type == VBLK) { 680 vp->v_op = spec_nfsv2nodeop_p; 681 if (nvp = checkalias(vp, (dev_t)rdev, vp->v_mount)) { 682 /* 683 * Discard unneeded vnode, but save its nfsnode. 684 */ 685 if (nq = np->n_forw) 686 nq->n_back = np->n_back; 687 *np->n_back = nq; 688 nvp->v_data = vp->v_data; 689 vp->v_data = NULL; 690 vp->v_op = spec_vnodeop_p; 691 vrele(vp); 692 vgone(vp); 693 /* 694 * Reinitialize aliased node. 695 */ 696 np->n_vnode = nvp; 697 nhpp = (struct nfsnode **)nfs_hash(&np->n_fh); 698 if (nq = *nhpp) 699 nq->n_back = &np->n_forw; 700 np->n_forw = nq; 701 np->n_back = nhpp; 702 *nhpp = np; 703 *vpp = vp = nvp; 704 } 705 } 706 np->n_mtime = mtime.ts_sec; 707 } 708 vap = &np->n_vattr; 709 vap->va_type = vtyp; 710 vap->va_mode = (vmode & 07777); 711 vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink); 712 vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid); 713 vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid); 714 vap->va_rdev = (dev_t)rdev; 715 vap->va_mtime = mtime; 716 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0]; 717 if (isnq) { 718 fxdr_hyper(&fp->fa_nqsize, &vap->va_size); 719 vap->va_blocksize = fxdr_unsigned(long, fp->fa_nqblocksize); 720 fxdr_hyper(&fp->fa_nqbytes, &vap->va_bytes); 721 vap->va_fileid = fxdr_unsigned(long, fp->fa_nqfileid); 722 fxdr_nqtime(&fp->fa_nqatime, &vap->va_atime); 723 vap->va_flags = fxdr_unsigned(u_long, fp->fa_nqflags); 724 fxdr_nqtime(&fp->fa_nqctime, &vap->va_ctime); 725 vap->va_gen = fxdr_unsigned(u_long, fp->fa_nqgen); 726 fxdr_hyper(&fp->fa_nqfilerev, &vap->va_filerev); 727 } else { 728 vap->va_size = fxdr_unsigned(u_long, fp->fa_nfssize); 729 vap->va_blocksize = fxdr_unsigned(long, fp->fa_nfsblocksize); 730 vap->va_bytes = fxdr_unsigned(long, fp->fa_nfsblocks) * NFS_FABLKSIZE; 731 vap->va_fileid = fxdr_unsigned(long, fp->fa_nfsfileid); 732 fxdr_nfstime(&fp->fa_nfsatime, &vap->va_atime); 733 vap->va_flags = 0; 734 vap->va_ctime.ts_sec = fxdr_unsigned(long, fp->fa_nfsctime.nfs_sec); 735 vap->va_ctime.ts_nsec = 0; 736 vap->va_gen = fxdr_unsigned(u_long, fp->fa_nfsctime.nfs_usec); 737 vap->va_filerev = 0; 738 } 739 if (vap->va_size != np->n_size) { 740 if (vap->va_type == VREG) { 741 if (np->n_flag & NMODIFIED) { 742 if (vap->va_size < np->n_size) 743 vap->va_size = np->n_size; 744 else 745 np->n_size = vap->va_size; 746 } else 747 np->n_size = vap->va_size; 748 vnode_pager_setsize(vp, (u_long)np->n_size); 749 } else 750 np->n_size = vap->va_size; 751 } 752 np->n_attrstamp = time.tv_sec; 753 *dposp = dpos; 754 *mdp = md; 755 if (vaper != NULL) { 756 bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(*vap)); 757 #ifdef notdef 758 if ((np->n_flag & NMODIFIED) && np->n_size > vap->va_size) 759 if (np->n_size > vap->va_size) 760 vaper->va_size = np->n_size; 761 #endif 762 if (np->n_flag & NCHG) { 763 if (np->n_flag & NACC) { 764 vaper->va_atime.ts_sec = np->n_atim.tv_sec; 765 vaper->va_atime.ts_nsec = 766 np->n_atim.tv_usec * 1000; 767 } 768 if (np->n_flag & NUPD) { 769 vaper->va_mtime.ts_sec = np->n_mtim.tv_sec; 770 vaper->va_mtime.ts_nsec = 771 np->n_mtim.tv_usec * 1000; 772 } 773 } 774 } 775 return (0); 776 } 777 778 /* 779 * Check the time stamp 780 * If the cache is valid, copy contents to *vap and return 0 781 * otherwise return an error 782 */ 783 nfs_getattrcache(vp, vaper) 784 register struct vnode *vp; 785 struct vattr *vaper; 786 { 787 register struct nfsnode *np = VTONFS(vp); 788 register struct vattr *vap; 789 790 if (VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NQLOOKLEASE) { 791 if (!NQNFS_CKCACHABLE(vp, NQL_READ) || np->n_attrstamp == 0) { 792 nfsstats.attrcache_misses++; 793 return (ENOENT); 794 } 795 } else if ((time.tv_sec - np->n_attrstamp) >= NFS_ATTRTIMEO(np)) { 796 nfsstats.attrcache_misses++; 797 return (ENOENT); 798 } 799 nfsstats.attrcache_hits++; 800 vap = &np->n_vattr; 801 if (vap->va_size != np->n_size) { 802 if (vap->va_type == VREG) { 803 if (np->n_flag & NMODIFIED) { 804 if (vap->va_size < np->n_size) 805 vap->va_size = np->n_size; 806 else 807 np->n_size = vap->va_size; 808 } else 809 np->n_size = vap->va_size; 810 vnode_pager_setsize(vp, (u_long)np->n_size); 811 } else 812 np->n_size = vap->va_size; 813 } 814 bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(struct vattr)); 815 #ifdef notdef 816 if ((np->n_flag & NMODIFIED) == 0) { 817 np->n_size = vaper->va_size; 818 vnode_pager_setsize(vp, (u_long)np->n_size); 819 } else if (np->n_size > vaper->va_size) 820 if (np->n_size > vaper->va_size) 821 vaper->va_size = np->n_size; 822 #endif 823 if (np->n_flag & NCHG) { 824 if (np->n_flag & NACC) { 825 vaper->va_atime.ts_sec = np->n_atim.tv_sec; 826 vaper->va_atime.ts_nsec = np->n_atim.tv_usec * 1000; 827 } 828 if (np->n_flag & NUPD) { 829 vaper->va_mtime.ts_sec = np->n_mtim.tv_sec; 830 vaper->va_mtime.ts_nsec = np->n_mtim.tv_usec * 1000; 831 } 832 } 833 return (0); 834 } 835 836 /* 837 * Set up nameidata for a lookup() call and do it 838 */ 839 nfs_namei(ndp, fhp, len, slp, nam, mdp, dposp, p) 840 register struct nameidata *ndp; 841 fhandle_t *fhp; 842 int len; 843 struct nfssvc_sock *slp; 844 struct mbuf *nam; 845 struct mbuf **mdp; 846 caddr_t *dposp; 847 struct proc *p; 848 { 849 register int i, rem; 850 register struct mbuf *md; 851 register char *fromcp, *tocp; 852 struct vnode *dp; 853 int error, rdonly; 854 struct componentname *cnp = &ndp->ni_cnd; 855 856 MALLOC(cnp->cn_pnbuf, char *, len + 1, M_NAMEI, M_WAITOK); 857 /* 858 * Copy the name from the mbuf list to ndp->ni_pnbuf 859 * and set the various ndp fields appropriately. 860 */ 861 fromcp = *dposp; 862 tocp = cnp->cn_pnbuf; 863 md = *mdp; 864 rem = mtod(md, caddr_t) + md->m_len - fromcp; 865 cnp->cn_hash = 0; 866 for (i = 0; i < len; i++) { 867 while (rem == 0) { 868 md = md->m_next; 869 if (md == NULL) { 870 error = EBADRPC; 871 goto out; 872 } 873 fromcp = mtod(md, caddr_t); 874 rem = md->m_len; 875 } 876 if (*fromcp == '\0' || *fromcp == '/') { 877 error = EINVAL; 878 goto out; 879 } 880 cnp->cn_hash += (unsigned char)*fromcp; 881 *tocp++ = *fromcp++; 882 rem--; 883 } 884 *tocp = '\0'; 885 *mdp = md; 886 *dposp = fromcp; 887 len = nfsm_rndup(len)-len; 888 if (len > 0) { 889 if (rem >= len) 890 *dposp += len; 891 else if (error = nfs_adv(mdp, dposp, len, rem)) 892 goto out; 893 } 894 ndp->ni_pathlen = tocp - cnp->cn_pnbuf; 895 cnp->cn_nameptr = cnp->cn_pnbuf; 896 /* 897 * Extract and set starting directory. 898 */ 899 if (error = nfsrv_fhtovp(fhp, FALSE, &dp, ndp->ni_cnd.cn_cred, slp, 900 nam, &rdonly)) 901 goto out; 902 if (dp->v_type != VDIR) { 903 vrele(dp); 904 error = ENOTDIR; 905 goto out; 906 } 907 ndp->ni_startdir = dp; 908 if (rdonly) 909 cnp->cn_flags |= (NOCROSSMOUNT | RDONLY); 910 else 911 cnp->cn_flags |= NOCROSSMOUNT; 912 /* 913 * And call lookup() to do the real work 914 */ 915 cnp->cn_proc = p; 916 if (error = lookup(ndp)) 917 goto out; 918 /* 919 * Check for encountering a symbolic link 920 */ 921 if (cnp->cn_flags & ISSYMLINK) { 922 if ((cnp->cn_flags & LOCKPARENT) && ndp->ni_pathlen == 1) 923 vput(ndp->ni_dvp); 924 else 925 vrele(ndp->ni_dvp); 926 vput(ndp->ni_vp); 927 ndp->ni_vp = NULL; 928 error = EINVAL; 929 goto out; 930 } 931 /* 932 * Check for saved name request 933 */ 934 if (cnp->cn_flags & (SAVENAME | SAVESTART)) { 935 cnp->cn_flags |= HASBUF; 936 return (0); 937 } 938 out: 939 FREE(cnp->cn_pnbuf, M_NAMEI); 940 return (error); 941 } 942 943 /* 944 * A fiddled version of m_adj() that ensures null fill to a long 945 * boundary and only trims off the back end 946 */ 947 void 948 nfsm_adj(mp, len, nul) 949 struct mbuf *mp; 950 register int len; 951 int nul; 952 { 953 register struct mbuf *m; 954 register int count, i; 955 register char *cp; 956 957 /* 958 * Trim from tail. Scan the mbuf chain, 959 * calculating its length and finding the last mbuf. 960 * If the adjustment only affects this mbuf, then just 961 * adjust and return. Otherwise, rescan and truncate 962 * after the remaining size. 963 */ 964 count = 0; 965 m = mp; 966 for (;;) { 967 count += m->m_len; 968 if (m->m_next == (struct mbuf *)0) 969 break; 970 m = m->m_next; 971 } 972 if (m->m_len > len) { 973 m->m_len -= len; 974 if (nul > 0) { 975 cp = mtod(m, caddr_t)+m->m_len-nul; 976 for (i = 0; i < nul; i++) 977 *cp++ = '\0'; 978 } 979 return; 980 } 981 count -= len; 982 if (count < 0) 983 count = 0; 984 /* 985 * Correct length for chain is "count". 986 * Find the mbuf with last data, adjust its length, 987 * and toss data from remaining mbufs on chain. 988 */ 989 for (m = mp; m; m = m->m_next) { 990 if (m->m_len >= count) { 991 m->m_len = count; 992 if (nul > 0) { 993 cp = mtod(m, caddr_t)+m->m_len-nul; 994 for (i = 0; i < nul; i++) 995 *cp++ = '\0'; 996 } 997 break; 998 } 999 count -= m->m_len; 1000 } 1001 while (m = m->m_next) 1002 m->m_len = 0; 1003 } 1004 1005 /* 1006 * nfsrv_fhtovp() - convert a fh to a vnode ptr (optionally locked) 1007 * - look up fsid in mount list (if not found ret error) 1008 * - get vp and export rights by calling VFS_FHTOVP() 1009 * - if cred->cr_uid == 0 or MNT_EXPORTANON set it to credanon 1010 * - if not lockflag unlock it with VOP_UNLOCK() 1011 */ 1012 nfsrv_fhtovp(fhp, lockflag, vpp, cred, slp, nam, rdonlyp) 1013 fhandle_t *fhp; 1014 int lockflag; 1015 struct vnode **vpp; 1016 struct ucred *cred; 1017 struct nfssvc_sock *slp; 1018 struct mbuf *nam; 1019 int *rdonlyp; 1020 { 1021 register struct mount *mp; 1022 register struct nfsuid *uidp; 1023 register int i; 1024 struct ucred *credanon; 1025 int error, exflags; 1026 1027 *vpp = (struct vnode *)0; 1028 if ((mp = getvfs(&fhp->fh_fsid)) == NULL) 1029 return (ESTALE); 1030 if (error = VFS_FHTOVP(mp, &fhp->fh_fid, nam, vpp, &exflags, &credanon)) 1031 return (error); 1032 /* 1033 * Check/setup credentials. 1034 */ 1035 if (exflags & MNT_EXKERB) { 1036 uidp = slp->ns_uidh[NUIDHASH(cred->cr_uid)]; 1037 while (uidp) { 1038 if (uidp->nu_uid == cred->cr_uid) 1039 break; 1040 uidp = uidp->nu_hnext; 1041 } 1042 if (uidp) { 1043 cred->cr_uid = uidp->nu_cr.cr_uid; 1044 for (i = 0; i < uidp->nu_cr.cr_ngroups; i++) 1045 cred->cr_groups[i] = uidp->nu_cr.cr_groups[i]; 1046 } else { 1047 vput(*vpp); 1048 return (NQNFS_AUTHERR); 1049 } 1050 } else if (cred->cr_uid == 0 || (exflags & MNT_EXPORTANON)) { 1051 cred->cr_uid = credanon->cr_uid; 1052 for (i = 0; i < credanon->cr_ngroups && i < NGROUPS; i++) 1053 cred->cr_groups[i] = credanon->cr_groups[i]; 1054 } 1055 if (exflags & MNT_EXRDONLY) 1056 *rdonlyp = 1; 1057 else 1058 *rdonlyp = 0; 1059 if (!lockflag) 1060 VOP_UNLOCK(*vpp); 1061 return (0); 1062 } 1063 1064 /* 1065 * This function compares two net addresses by family and returns TRUE 1066 * if they are the same host. 1067 * If there is any doubt, return FALSE. 1068 * The AF_INET family is handled as a special case so that address mbufs 1069 * don't need to be saved to store "struct in_addr", which is only 4 bytes. 1070 */ 1071 netaddr_match(family, haddr, nam) 1072 int family; 1073 union nethostaddr *haddr; 1074 struct mbuf *nam; 1075 { 1076 register struct sockaddr_in *inetaddr; 1077 1078 switch (family) { 1079 case AF_INET: 1080 inetaddr = mtod(nam, struct sockaddr_in *); 1081 if (inetaddr->sin_family == AF_INET && 1082 inetaddr->sin_addr.s_addr == haddr->had_inetaddr) 1083 return (1); 1084 break; 1085 #ifdef ISO 1086 case AF_ISO: 1087 { 1088 register struct sockaddr_iso *isoaddr1, *isoaddr2; 1089 1090 isoaddr1 = mtod(nam, struct sockaddr_iso *); 1091 isoaddr2 = mtod(haddr->had_nam, struct sockaddr_iso *); 1092 if (isoaddr1->siso_family == AF_ISO && 1093 isoaddr1->siso_nlen > 0 && 1094 isoaddr1->siso_nlen == isoaddr2->siso_nlen && 1095 SAME_ISOADDR(isoaddr1, isoaddr2)) 1096 return (1); 1097 break; 1098 } 1099 #endif /* ISO */ 1100 default: 1101 break; 1102 }; 1103 return (0); 1104 } 1105