1 /* 2 * Copyright (c) 1989 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_subs.c 7.60 (Berkeley) 07/12/92 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 <netinet/in.h> 40 #ifdef ISO 41 #include <netiso/iso.h> 42 #endif 43 44 #define TRUE 1 45 #define FALSE 0 46 47 /* 48 * Data items converted to xdr at startup, since they are constant 49 * This is kinda hokey, but may save a little time doing byte swaps 50 */ 51 u_long nfs_procids[NFS_NPROCS]; 52 u_long nfs_xdrneg1; 53 u_long rpc_call, rpc_vers, rpc_reply, rpc_msgdenied, rpc_autherr, 54 rpc_mismatch, rpc_auth_unix, rpc_msgaccepted, rpc_rejectedcred, 55 rpc_auth_kerb; 56 u_long nfs_vers, nfs_prog, nfs_true, nfs_false; 57 58 /* And other global data */ 59 static u_long nfs_xid = 0; 60 enum vtype ntov_type[7] = { VNON, VREG, VDIR, VBLK, VCHR, VLNK, VNON }; 61 extern struct proc *nfs_iodwant[NFS_MAXASYNCDAEMON]; 62 extern struct nfsreq nfsreqh; 63 extern int nqnfs_piggy[NFS_NPROCS]; 64 extern struct nfsrtt nfsrtt; 65 extern union nqsrvthead nqthead; 66 extern union nqsrvthead nqfhead[NQLCHSZ]; 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 * If Updateflg == True we can overwrite the first part of the mbuf data 398 * (in this case it can never sleep, so it can be called from interrupt level) 399 * it may however block when Updateflg == False 400 * This is used by the macros nfsm_dissect and nfsm_dissecton for tough 401 * cases. (The macros use the vars. dpos and dpos2) 402 */ 403 nfsm_disct(mdp, dposp, siz, left, updateflg, cp2) 404 struct mbuf **mdp; 405 caddr_t *dposp; 406 int siz; 407 int left; 408 int updateflg; 409 caddr_t *cp2; 410 { 411 register struct mbuf *mp, *mp2; 412 register int siz2, xfer; 413 register caddr_t p; 414 415 mp = *mdp; 416 while (left == 0) { 417 *mdp = mp = mp->m_next; 418 if (mp == NULL) 419 return (EBADRPC); 420 left = mp->m_len; 421 *dposp = mtod(mp, caddr_t); 422 } 423 if (left >= siz) { 424 *cp2 = *dposp; 425 *dposp += siz; 426 } else if (mp->m_next == NULL) { 427 return (EBADRPC); 428 } else if (siz > MHLEN) { 429 panic("nfs S too big"); 430 } else { 431 /* Iff update, you can overwrite, else must alloc new mbuf */ 432 if (updateflg) { 433 NFSMINOFF(mp); 434 } else { 435 MGET(mp2, M_WAIT, MT_DATA); 436 mp2->m_next = mp->m_next; 437 mp->m_next = mp2; 438 mp->m_len -= left; 439 mp = mp2; 440 } 441 *cp2 = p = mtod(mp, caddr_t); 442 bcopy(*dposp, p, left); /* Copy what was left */ 443 siz2 = siz-left; 444 p += left; 445 mp2 = mp->m_next; 446 /* Loop around copying up the siz2 bytes */ 447 while (siz2 > 0) { 448 if (mp2 == NULL) 449 return (EBADRPC); 450 xfer = (siz2 > mp2->m_len) ? mp2->m_len : siz2; 451 if (xfer > 0) { 452 bcopy(mtod(mp2, caddr_t), p, xfer); 453 NFSMADV(mp2, xfer); 454 mp2->m_len -= xfer; 455 p += xfer; 456 siz2 -= xfer; 457 } 458 if (siz2 > 0) 459 mp2 = mp2->m_next; 460 } 461 mp->m_len = siz; 462 *mdp = mp2; 463 *dposp = mtod(mp2, caddr_t); 464 } 465 return (0); 466 } 467 468 /* 469 * Advance the position in the mbuf chain. 470 */ 471 nfs_adv(mdp, dposp, offs, left) 472 struct mbuf **mdp; 473 caddr_t *dposp; 474 int offs; 475 int left; 476 { 477 register struct mbuf *m; 478 register int s; 479 480 m = *mdp; 481 s = left; 482 while (s < offs) { 483 offs -= s; 484 m = m->m_next; 485 if (m == NULL) 486 return (EBADRPC); 487 s = m->m_len; 488 } 489 *mdp = m; 490 *dposp = mtod(m, caddr_t)+offs; 491 return (0); 492 } 493 494 /* 495 * Copy a string into mbufs for the hard cases... 496 */ 497 nfsm_strtmbuf(mb, bpos, cp, siz) 498 struct mbuf **mb; 499 char **bpos; 500 char *cp; 501 long siz; 502 { 503 register struct mbuf *m1, *m2; 504 long left, xfer, len, tlen; 505 u_long *tl; 506 int putsize; 507 508 putsize = 1; 509 m2 = *mb; 510 left = M_TRAILINGSPACE(m2); 511 if (left > 0) { 512 tl = ((u_long *)(*bpos)); 513 *tl++ = txdr_unsigned(siz); 514 putsize = 0; 515 left -= NFSX_UNSIGNED; 516 m2->m_len += NFSX_UNSIGNED; 517 if (left > 0) { 518 bcopy(cp, (caddr_t) tl, left); 519 siz -= left; 520 cp += left; 521 m2->m_len += left; 522 left = 0; 523 } 524 } 525 /* Loop around adding mbufs */ 526 while (siz > 0) { 527 MGET(m1, M_WAIT, MT_DATA); 528 if (siz > MLEN) 529 MCLGET(m1, M_WAIT); 530 m1->m_len = NFSMSIZ(m1); 531 m2->m_next = m1; 532 m2 = m1; 533 tl = mtod(m1, u_long *); 534 tlen = 0; 535 if (putsize) { 536 *tl++ = txdr_unsigned(siz); 537 m1->m_len -= NFSX_UNSIGNED; 538 tlen = NFSX_UNSIGNED; 539 putsize = 0; 540 } 541 if (siz < m1->m_len) { 542 len = nfsm_rndup(siz); 543 xfer = siz; 544 if (xfer < len) 545 *(tl+(xfer>>2)) = 0; 546 } else { 547 xfer = len = m1->m_len; 548 } 549 bcopy(cp, (caddr_t) tl, xfer); 550 m1->m_len = len+tlen; 551 siz -= xfer; 552 cp += xfer; 553 } 554 *mb = m1; 555 *bpos = mtod(m1, caddr_t)+m1->m_len; 556 return (0); 557 } 558 559 /* 560 * Called once to initialize data structures... 561 */ 562 nfs_init() 563 { 564 register int i; 565 union nqsrvthead *lhp; 566 567 nfsrtt.pos = 0; 568 rpc_vers = txdr_unsigned(RPC_VER2); 569 rpc_call = txdr_unsigned(RPC_CALL); 570 rpc_reply = txdr_unsigned(RPC_REPLY); 571 rpc_msgdenied = txdr_unsigned(RPC_MSGDENIED); 572 rpc_msgaccepted = txdr_unsigned(RPC_MSGACCEPTED); 573 rpc_mismatch = txdr_unsigned(RPC_MISMATCH); 574 rpc_autherr = txdr_unsigned(RPC_AUTHERR); 575 rpc_rejectedcred = txdr_unsigned(AUTH_REJECTCRED); 576 rpc_auth_unix = txdr_unsigned(RPCAUTH_UNIX); 577 rpc_auth_kerb = txdr_unsigned(RPCAUTH_NQNFS); 578 nfs_vers = txdr_unsigned(NFS_VER2); 579 nfs_prog = txdr_unsigned(NFS_PROG); 580 nfs_true = txdr_unsigned(TRUE); 581 nfs_false = txdr_unsigned(FALSE); 582 /* Loop thru nfs procids */ 583 for (i = 0; i < NFS_NPROCS; i++) 584 nfs_procids[i] = txdr_unsigned(i); 585 /* Ensure async daemons disabled */ 586 for (i = 0; i < NFS_MAXASYNCDAEMON; i++) 587 nfs_iodwant[i] = (struct proc *)0; 588 nfs_xdrneg1 = txdr_unsigned(-1); 589 nfs_nhinit(); /* Init the nfsnode table */ 590 nfsrv_init(0); /* Init server data structures */ 591 nfsrv_initcache(); /* Init the server request cache */ 592 593 /* 594 * Initialize the nqnfs server stuff. 595 */ 596 if (nqnfsstarttime == 0) { 597 nqnfsstarttime = boottime.tv_sec + nqsrv_maxlease 598 + nqsrv_clockskew + nqsrv_writeslack; 599 NQLOADNOVRAM(nqnfsstarttime); 600 nqnfs_prog = txdr_unsigned(NQNFS_PROG); 601 nqnfs_vers = txdr_unsigned(NQNFS_VER1); 602 nqthead.th_head[0] = &nqthead; 603 nqthead.th_head[1] = &nqthead; 604 for (i = 0; i < NQLCHSZ; i++) { 605 lhp = &nqfhead[i]; 606 lhp->th_head[0] = lhp; 607 lhp->th_head[1] = lhp; 608 } 609 } 610 611 /* 612 * Initialize reply list and start timer 613 */ 614 nfsreqh.r_prev = nfsreqh.r_next = &nfsreqh; 615 nfs_timer(); 616 } 617 618 /* 619 * Attribute cache routines. 620 * nfs_loadattrcache() - loads or updates the cache contents from attributes 621 * that are on the mbuf list 622 * nfs_getattrcache() - returns valid attributes if found in cache, returns 623 * error otherwise 624 */ 625 626 /* 627 * Load the attribute cache (that lives in the nfsnode entry) with 628 * the values on the mbuf list and 629 * Iff vap not NULL 630 * copy the attributes to *vaper 631 */ 632 nfs_loadattrcache(vpp, mdp, dposp, vaper) 633 struct vnode **vpp; 634 struct mbuf **mdp; 635 caddr_t *dposp; 636 struct vattr *vaper; 637 { 638 register struct vnode *vp = *vpp; 639 register struct vattr *vap; 640 register struct nfsv2_fattr *fp; 641 extern int (**spec_nfsv2nodeop_p)(); 642 extern int (**spec_vnodeop_p)(); 643 register struct nfsnode *np; 644 register long t1; 645 caddr_t dpos, cp2; 646 int error = 0; 647 struct mbuf *md; 648 enum vtype vtyp; 649 u_short vmode; 650 long rdev; 651 struct timeval mtime; 652 struct vnode *nvp; 653 654 md = *mdp; 655 dpos = *dposp; 656 t1 = (mtod(md, caddr_t) + md->m_len) - dpos; 657 if (error = nfsm_disct(&md, &dpos, NFSX_FATTR, t1, TRUE, &cp2)) 658 return (error); 659 fp = (struct nfsv2_fattr *)cp2; 660 vtyp = nfstov_type(fp->fa_type); 661 vmode = fxdr_unsigned(u_short, fp->fa_mode); 662 if (vtyp == VNON || vtyp == VREG) 663 vtyp = IFTOVT(vmode); 664 rdev = fxdr_unsigned(long, fp->fa_rdev); 665 fxdr_time(&fp->fa_mtime, &mtime); 666 /* 667 * If v_type == VNON it is a new node, so fill in the v_type, 668 * n_mtime fields. Check to see if it represents a special 669 * device, and if so, check for a possible alias. Once the 670 * correct vnode has been obtained, fill in the rest of the 671 * information. 672 */ 673 np = VTONFS(vp); 674 if (vp->v_type == VNON) { 675 if (vtyp == VCHR && rdev == 0xffffffff) 676 vp->v_type = vtyp = VFIFO; 677 else 678 vp->v_type = vtyp; 679 if (vp->v_type == VFIFO) { 680 #ifdef FIFO 681 extern int (**fifo_nfsv2nodeop_p)(); 682 vp->v_op = fifo_nfsv2nodeop_p; 683 #else 684 return (EOPNOTSUPP); 685 #endif /* FIFO */ 686 } 687 if (vp->v_type == VCHR || vp->v_type == VBLK) { 688 vp->v_op = spec_nfsv2nodeop_p; 689 if (nvp = checkalias(vp, (dev_t)rdev, vp->v_mount)) { 690 /* 691 * Discard unneeded vnode, but save its nfsnode. 692 */ 693 remque(np); 694 nvp->v_data = vp->v_data; 695 vp->v_data = NULL; 696 vp->v_op = spec_vnodeop_p; 697 vrele(vp); 698 vgone(vp); 699 /* 700 * Reinitialize aliased node. 701 */ 702 np->n_vnode = nvp; 703 insque(np, nfs_hash(&np->n_fh)); 704 *vpp = vp = nvp; 705 } 706 } 707 if ((VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NQNFS) == 0) 708 np->n_mtime = mtime.tv_sec; 709 } 710 vap = &np->n_vattr; 711 vap->va_type = vtyp; 712 vap->va_mode = (vmode & 07777); 713 vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink); 714 vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid); 715 vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid); 716 vap->va_size = fxdr_unsigned(u_long, fp->fa_size); 717 if ((np->n_flag & NMODIFIED) == 0 || vap->va_size > np->n_size) { 718 np->n_size = vap->va_size; 719 vnode_pager_setsize(vp, (u_long)np->n_size); 720 } 721 vap->va_blocksize = fxdr_unsigned(long, fp->fa_blocksize); 722 vap->va_rdev = (dev_t)rdev; 723 vap->va_bytes = fxdr_unsigned(long, fp->fa_blocks) * NFS_FABLKSIZE; 724 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0]; 725 vap->va_fileid = fxdr_unsigned(long, fp->fa_fileid); 726 vap->va_atime.ts_sec = fxdr_unsigned(long, fp->fa_atime.tv_sec); 727 vap->va_atime.ts_nsec = 0; 728 vap->va_flags = fxdr_unsigned(u_long, fp->fa_atime.tv_usec); 729 vap->va_mtime.ts_sec = mtime.tv_sec; 730 vap->va_mtime.ts_nsec = mtime.tv_usec * 1000; 731 vap->va_ctime.ts_sec = fxdr_unsigned(long, fp->fa_ctime.tv_sec); 732 vap->va_ctime.ts_nsec = 0; 733 vap->va_gen = fxdr_unsigned(u_long, fp->fa_ctime.tv_usec); 734 np->n_attrstamp = time.tv_sec; 735 *dposp = dpos; 736 *mdp = md; 737 if (vaper != NULL) { 738 bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(*vap)); 739 if ((np->n_flag & NMODIFIED) && (np->n_size > vap->va_size)) 740 vaper->va_size = np->n_size; 741 if (np->n_flag & NCHG) { 742 if (np->n_flag & NACC) { 743 vaper->va_atime.ts_sec = np->n_atim.tv_sec; 744 vaper->va_atime.ts_nsec = 745 np->n_atim.tv_usec * 1000; 746 } 747 if (np->n_flag & NUPD) { 748 vaper->va_mtime.ts_sec = np->n_mtim.tv_sec; 749 vaper->va_mtime.ts_nsec = 750 np->n_mtim.tv_usec * 1000; 751 } 752 } 753 } 754 return (0); 755 } 756 757 /* 758 * Check the time stamp 759 * If the cache is valid, copy contents to *vap and return 0 760 * otherwise return an error 761 */ 762 nfs_getattrcache(vp, vap) 763 register struct vnode *vp; 764 struct vattr *vap; 765 { 766 register struct nfsnode *np; 767 768 np = VTONFS(vp); 769 if (VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NQNFS) { 770 if (!NQNFS_CKCACHABLE(vp, NQL_READ) || np->n_attrstamp == 0) { 771 nfsstats.attrcache_misses++; 772 return (ENOENT); 773 } 774 } else if ((time.tv_sec - np->n_attrstamp) >= NFS_ATTRTIMEO) { 775 nfsstats.attrcache_misses++; 776 return (ENOENT); 777 } 778 nfsstats.attrcache_hits++; 779 bcopy((caddr_t)&np->n_vattr,(caddr_t)vap,sizeof(struct vattr)); 780 if ((np->n_flag & NMODIFIED) == 0) { 781 np->n_size = vap->va_size; 782 vnode_pager_setsize(vp, (u_long)np->n_size); 783 } else if (np->n_size > vap->va_size) 784 vap->va_size = np->n_size; 785 if (np->n_flag & NCHG) { 786 if (np->n_flag & NACC) { 787 vap->va_atime.ts_sec = np->n_atim.tv_sec; 788 vap->va_atime.ts_nsec = np->n_atim.tv_usec * 1000; 789 } 790 if (np->n_flag & NUPD) { 791 vap->va_mtime.ts_sec = np->n_mtim.tv_sec; 792 vap->va_mtime.ts_nsec = np->n_mtim.tv_usec * 1000; 793 } 794 } 795 return (0); 796 } 797 798 /* 799 * Set up nameidata for a lookup() call and do it 800 */ 801 nfs_namei(ndp, fhp, len, slp, nam, mdp, dposp, p) 802 register struct nameidata *ndp; 803 fhandle_t *fhp; 804 int len; 805 struct nfssvc_sock *slp; 806 struct mbuf *nam; 807 struct mbuf **mdp; 808 caddr_t *dposp; 809 struct proc *p; 810 { 811 register int i, rem; 812 register struct mbuf *md; 813 register char *fromcp, *tocp; 814 struct vnode *dp; 815 int error, rdonly; 816 struct componentname *cnp = &ndp->ni_cnd; 817 818 MALLOC(cnp->cn_pnbuf, char *, len + 1, M_NAMEI, M_WAITOK); 819 /* 820 * Copy the name from the mbuf list to ndp->ni_pnbuf 821 * and set the various ndp fields appropriately. 822 */ 823 fromcp = *dposp; 824 tocp = cnp->cn_pnbuf; 825 md = *mdp; 826 rem = mtod(md, caddr_t) + md->m_len - fromcp; 827 cnp->cn_hash = 0; 828 for (i = 0; i < len; i++) { 829 while (rem == 0) { 830 md = md->m_next; 831 if (md == NULL) { 832 error = EBADRPC; 833 goto out; 834 } 835 fromcp = mtod(md, caddr_t); 836 rem = md->m_len; 837 } 838 if (*fromcp == '\0' || *fromcp == '/') { 839 error = EINVAL; 840 goto out; 841 } 842 if (*fromcp & 0200) 843 if ((*fromcp&0377) == ('/'|0200) || cnp->cn_nameiop != DELETE) { 844 error = EINVAL; 845 goto out; 846 } 847 cnp->cn_hash += (unsigned char)*fromcp; 848 *tocp++ = *fromcp++; 849 rem--; 850 } 851 *tocp = '\0'; 852 *mdp = md; 853 *dposp = fromcp; 854 len = nfsm_rndup(len)-len; 855 if (len > 0) { 856 if (rem >= len) 857 *dposp += len; 858 else if (error = nfs_adv(mdp, dposp, len, rem)) 859 goto out; 860 } 861 ndp->ni_pathlen = tocp - cnp->cn_pnbuf; 862 cnp->cn_nameptr = cnp->cn_pnbuf; 863 /* 864 * Extract and set starting directory. 865 */ 866 if (error = nfsrv_fhtovp(fhp, FALSE, &dp, ndp->ni_cnd.cn_cred, slp, 867 nam, &rdonly)) 868 goto out; 869 if (dp->v_type != VDIR) { 870 vrele(dp); 871 error = ENOTDIR; 872 goto out; 873 } 874 ndp->ni_startdir = dp; 875 if (rdonly) 876 cnp->cn_flags |= (NOCROSSMOUNT | RDONLY); 877 else 878 cnp->cn_flags |= NOCROSSMOUNT; 879 /* 880 * And call lookup() to do the real work 881 */ 882 cnp->cn_proc = p; 883 if (error = lookup(ndp)) 884 goto out; 885 /* 886 * Check for encountering a symbolic link 887 */ 888 if (cnp->cn_flags & ISSYMLINK) { 889 if ((cnp->cn_flags & LOCKPARENT) && ndp->ni_pathlen == 1) 890 vput(ndp->ni_dvp); 891 else 892 vrele(ndp->ni_dvp); 893 vput(ndp->ni_vp); 894 ndp->ni_vp = NULL; 895 error = EINVAL; 896 goto out; 897 } 898 /* 899 * Check for saved name request 900 */ 901 if (cnp->cn_flags & (SAVENAME | SAVESTART)) { 902 cnp->cn_flags |= HASBUF; 903 return (0); 904 } 905 out: 906 FREE(cnp->cn_pnbuf, M_NAMEI); 907 return (error); 908 } 909 910 /* 911 * A fiddled version of m_adj() that ensures null fill to a long 912 * boundary and only trims off the back end 913 */ 914 void 915 nfsm_adj(mp, len, nul) 916 struct mbuf *mp; 917 register int len; 918 int nul; 919 { 920 register struct mbuf *m; 921 register int count, i; 922 register char *cp; 923 924 /* 925 * Trim from tail. Scan the mbuf chain, 926 * calculating its length and finding the last mbuf. 927 * If the adjustment only affects this mbuf, then just 928 * adjust and return. Otherwise, rescan and truncate 929 * after the remaining size. 930 */ 931 count = 0; 932 m = mp; 933 for (;;) { 934 count += m->m_len; 935 if (m->m_next == (struct mbuf *)0) 936 break; 937 m = m->m_next; 938 } 939 if (m->m_len > len) { 940 m->m_len -= len; 941 if (nul > 0) { 942 cp = mtod(m, caddr_t)+m->m_len-nul; 943 for (i = 0; i < nul; i++) 944 *cp++ = '\0'; 945 } 946 return; 947 } 948 count -= len; 949 if (count < 0) 950 count = 0; 951 /* 952 * Correct length for chain is "count". 953 * Find the mbuf with last data, adjust its length, 954 * and toss data from remaining mbufs on chain. 955 */ 956 for (m = mp; m; m = m->m_next) { 957 if (m->m_len >= count) { 958 m->m_len = count; 959 if (nul > 0) { 960 cp = mtod(m, caddr_t)+m->m_len-nul; 961 for (i = 0; i < nul; i++) 962 *cp++ = '\0'; 963 } 964 break; 965 } 966 count -= m->m_len; 967 } 968 while (m = m->m_next) 969 m->m_len = 0; 970 } 971 972 /* 973 * nfsrv_fhtovp() - convert a fh to a vnode ptr (optionally locked) 974 * - look up fsid in mount list (if not found ret error) 975 * - get vp and export rights by calling VFS_FHTOVP() 976 * - if cred->cr_uid == 0 or MNT_EXPORTANON set it to credanon 977 * - if not lockflag unlock it with VOP_UNLOCK() 978 */ 979 nfsrv_fhtovp(fhp, lockflag, vpp, cred, slp, nam, rdonlyp) 980 fhandle_t *fhp; 981 int lockflag; 982 struct vnode **vpp; 983 struct ucred *cred; 984 struct nfssvc_sock *slp; 985 struct mbuf *nam; 986 int *rdonlyp; 987 { 988 register struct mount *mp; 989 register struct nfsuid *uidp; 990 struct ucred *credanon; 991 int error, exflags; 992 993 *vpp = (struct vnode *)0; 994 if ((mp = getvfs(&fhp->fh_fsid)) == NULL) 995 return (ESTALE); 996 if (error = VFS_FHTOVP(mp, &fhp->fh_fid, nam, vpp, &exflags, &credanon)) 997 return (error); 998 /* 999 * Check/setup credentials. 1000 */ 1001 if (exflags & MNT_EXKERB) { 1002 uidp = slp->ns_uidh[NUIDHASH(cred->cr_uid)]; 1003 while (uidp) { 1004 if (uidp->nu_uid == cred->cr_uid) 1005 break; 1006 uidp = uidp->nu_hnext; 1007 } 1008 if (uidp) { 1009 if (cred->cr_ref != 1) 1010 panic("nsrv fhtovp"); 1011 *cred = uidp->nu_cr; 1012 } else 1013 return (NQNFS_AUTHERR); 1014 } else if (cred->cr_uid == 0 || (exflags & MNT_EXPORTANON)) 1015 *cred = *credanon; 1016 if (exflags & MNT_EXRDONLY) 1017 *rdonlyp = 1; 1018 else 1019 *rdonlyp = 0; 1020 if (!lockflag) 1021 VOP_UNLOCK(*vpp); 1022 return (0); 1023 } 1024 1025 /* 1026 * This function compares two net addresses by family and returns TRUE 1027 * if they are the same host. 1028 * If there is any doubt, return FALSE. 1029 * The AF_INET family is handled as a special case so that address mbufs 1030 * don't need to be saved to store "struct in_addr", which is only 4 bytes. 1031 */ 1032 netaddr_match(family, haddr, hmask, nam) 1033 int family; 1034 union nethostaddr *haddr; 1035 union nethostaddr *hmask; 1036 struct mbuf *nam; 1037 { 1038 register struct sockaddr_in *inetaddr; 1039 #ifdef ISO 1040 register struct sockaddr_iso *isoaddr1, *isoaddr2; 1041 #endif 1042 1043 1044 switch (family) { 1045 case AF_INET: 1046 inetaddr = mtod(nam, struct sockaddr_in *); 1047 if (inetaddr->sin_family != AF_INET) 1048 return (0); 1049 if (hmask) { 1050 if ((inetaddr->sin_addr.s_addr & hmask->had_inetaddr) == 1051 (haddr->had_inetaddr & hmask->had_inetaddr)) 1052 return (1); 1053 } else if (inetaddr->sin_addr.s_addr == haddr->had_inetaddr) 1054 return (1); 1055 break; 1056 #ifdef ISO 1057 case AF_ISO: 1058 isoaddr1 = mtod(nam, struct sockaddr_iso *); 1059 if (isoaddr1->siso_family != AF_ISO) 1060 return (0); 1061 isoaddr2 = mtod(haddr->had_nam, struct sockaddr_iso *); 1062 if (isoaddr1->siso_nlen > 0 && 1063 isoaddr1->siso_nlen == isoaddr2->siso_nlen && 1064 SAME_ISOADDR(isoaddr1, isoaddr2)) 1065 return (1); 1066 break; 1067 #endif /* ISO */ 1068 default: 1069 break; 1070 }; 1071 return (0); 1072 } 1073 1074 /* 1075 * Generate a hash code for an iso host address. Used by NETADDRHASH() for 1076 * iso addresses. 1077 */ 1078 iso_addrhash(saddr) 1079 struct sockaddr *saddr; 1080 { 1081 #ifdef ISO 1082 register struct sockaddr_iso *siso; 1083 register int i, sum; 1084 1085 sum = 0; 1086 for (i = 0; i < siso->siso_nlen; i++) 1087 sum += siso->siso_data[i]; 1088 return (sum & (NETHASHSZ - 1)); 1089 #else 1090 return (0); 1091 #endif /* ISO */ 1092 } 1093