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