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