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.45 (Berkeley) 01/14/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_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 struct vnodeops spec_nfsv2nodeops, spec_vnodeops; 642 register struct nfsnode *np; 643 register long t1; 644 caddr_t dpos, cp2; 645 int error = 0; 646 struct mbuf *md; 647 enum vtype vtyp; 648 u_short vmode; 649 long rdev; 650 struct timeval mtime; 651 struct vnode *nvp; 652 653 md = *mdp; 654 dpos = *dposp; 655 t1 = (mtod(md, caddr_t) + md->m_len) - dpos; 656 if (error = nfsm_disct(&md, &dpos, NFSX_FATTR, t1, TRUE, &cp2)) 657 return (error); 658 fp = (struct nfsv2_fattr *)cp2; 659 vtyp = nfstov_type(fp->fa_type); 660 vmode = fxdr_unsigned(u_short, fp->fa_mode); 661 if (vtyp == VNON) 662 vtyp = IFTOVT(vmode); 663 rdev = fxdr_unsigned(long, fp->fa_rdev); 664 fxdr_time(&fp->fa_mtime, &mtime); 665 /* 666 * If v_type == VNON it is a new node, so fill in the v_type, 667 * n_mtime fields. Check to see if it represents a special 668 * device, and if so, check for a possible alias. Once the 669 * correct vnode has been obtained, fill in the rest of the 670 * information. 671 */ 672 np = VTONFS(vp); 673 if (vp->v_type == VNON) { 674 if (vtyp == VCHR && rdev == 0xffffffff) 675 vp->v_type = vtyp = VFIFO; 676 else 677 vp->v_type = vtyp; 678 if (vp->v_type == VFIFO) { 679 #ifdef FIFO 680 extern struct vnodeops fifo_nfsv2nodeops; 681 vp->v_op = &fifo_nfsv2nodeops; 682 #else 683 return (EOPNOTSUPP); 684 #endif /* FIFO */ 685 } 686 if (vp->v_type == VCHR || vp->v_type == VBLK) { 687 vp->v_op = &spec_nfsv2nodeops; 688 if (nvp = checkalias(vp, (dev_t)rdev, vp->v_mount)) { 689 /* 690 * Discard unneeded vnode, but save its nfsnode. 691 */ 692 remque(np); 693 nvp->v_data = vp->v_data; 694 vp->v_data = NULL; 695 vp->v_op = &spec_vnodeops; 696 vrele(vp); 697 vgone(vp); 698 /* 699 * Reinitialize aliased node. 700 */ 701 np->n_vnode = nvp; 702 insque(np, nfs_hash(&np->n_fh)); 703 *vpp = vp = nvp; 704 } 705 } 706 if ((VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NQNFS) == 0) 707 np->n_mtime = mtime.tv_sec; 708 } 709 vap = &np->n_vattr; 710 vap->va_type = vtyp; 711 vap->va_mode = (vmode & 07777); 712 vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink); 713 vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid); 714 vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid); 715 vap->va_size = fxdr_unsigned(u_long, fp->fa_size); 716 if ((np->n_flag & NMODIFIED) == 0 || vap->va_size > np->n_size) { 717 np->n_size = vap->va_size; 718 vnode_pager_setsize(vp, np->n_size); 719 } 720 vap->va_blocksize = fxdr_unsigned(long, fp->fa_blocksize); 721 vap->va_rdev = (dev_t)rdev; 722 vap->va_bytes = fxdr_unsigned(long, fp->fa_blocks) * NFS_FABLKSIZE; 723 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0]; 724 vap->va_fileid = fxdr_unsigned(long, fp->fa_fileid); 725 vap->va_atime.tv_sec = fxdr_unsigned(long, fp->fa_atime.tv_sec); 726 vap->va_atime.tv_usec = 0; 727 vap->va_flags = fxdr_unsigned(u_long, fp->fa_atime.tv_usec); 728 vap->va_mtime = mtime; 729 vap->va_ctime.tv_sec = fxdr_unsigned(long, fp->fa_ctime.tv_sec); 730 vap->va_ctime.tv_usec = 0; 731 vap->va_gen = fxdr_unsigned(u_long, fp->fa_ctime.tv_usec); 732 #ifdef _NOQUAD 733 vap->va_size_rsv = 0; 734 vap->va_bytes_rsv = 0; 735 #endif 736 np->n_attrstamp = time.tv_sec; 737 *dposp = dpos; 738 *mdp = md; 739 if (vaper != NULL) { 740 bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(*vap)); 741 if ((np->n_flag & NMODIFIED) && (np->n_size > vap->va_size)) 742 vaper->va_size = np->n_size; 743 } 744 return (0); 745 } 746 747 /* 748 * Check the time stamp 749 * If the cache is valid, copy contents to *vap and return 0 750 * otherwise return an error 751 */ 752 nfs_getattrcache(vp, vap) 753 register struct vnode *vp; 754 struct vattr *vap; 755 { 756 register struct nfsnode *np; 757 758 np = VTONFS(vp); 759 if (VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NQNFS) { 760 if (!NQNFS_CKCACHABLE(vp, NQL_READ) || np->n_attrstamp == 0) { 761 nfsstats.attrcache_misses++; 762 return (ENOENT); 763 } 764 } else if ((time.tv_sec - np->n_attrstamp) >= NFS_ATTRTIMEO) { 765 nfsstats.attrcache_misses++; 766 return (ENOENT); 767 } 768 nfsstats.attrcache_hits++; 769 bcopy((caddr_t)&np->n_vattr,(caddr_t)vap,sizeof(struct vattr)); 770 if ((np->n_flag & NMODIFIED) == 0) { 771 np->n_size = vap->va_size; 772 vnode_pager_setsize(vp, np->n_size); 773 } else if (np->n_size > vap->va_size) 774 vap->va_size = np->n_size; 775 return (0); 776 } 777 778 /* 779 * Set up nameidata for a lookup() call and do it 780 */ 781 nfs_namei(ndp, fhp, len, slp, nam, mdp, dposp, p) 782 register struct nameidata *ndp; 783 fhandle_t *fhp; 784 int len; 785 struct nfssvc_sock *slp; 786 struct mbuf *nam; 787 struct mbuf **mdp; 788 caddr_t *dposp; 789 struct proc *p; 790 { 791 register int i, rem; 792 register struct mbuf *md; 793 register char *fromcp, *tocp; 794 struct vnode *dp; 795 int flag, error, rdonly; 796 797 flag = ndp->ni_nameiop & OPMASK; 798 MALLOC(ndp->ni_pnbuf, char *, len + 1, M_NAMEI, M_WAITOK); 799 /* 800 * Copy the name from the mbuf list to ndp->ni_pnbuf 801 * and set the various ndp fields appropriately. 802 */ 803 fromcp = *dposp; 804 tocp = ndp->ni_pnbuf; 805 md = *mdp; 806 rem = mtod(md, caddr_t) + md->m_len - fromcp; 807 ndp->ni_hash = 0; 808 for (i = 0; i < len; i++) { 809 while (rem == 0) { 810 md = md->m_next; 811 if (md == NULL) { 812 error = EBADRPC; 813 goto out; 814 } 815 fromcp = mtod(md, caddr_t); 816 rem = md->m_len; 817 } 818 if (*fromcp == '\0' || *fromcp == '/') { 819 error = EINVAL; 820 goto out; 821 } 822 if (*fromcp & 0200) 823 if ((*fromcp&0377) == ('/'|0200) || flag != DELETE) { 824 error = EINVAL; 825 goto out; 826 } 827 ndp->ni_hash += (unsigned char)*fromcp; 828 *tocp++ = *fromcp++; 829 rem--; 830 } 831 *tocp = '\0'; 832 *mdp = md; 833 *dposp = fromcp; 834 len = nfsm_rndup(len)-len; 835 if (len > 0) { 836 if (rem >= len) 837 *dposp += len; 838 else if (error = nfs_adv(mdp, dposp, len, rem)) 839 goto out; 840 } 841 ndp->ni_pathlen = tocp - ndp->ni_pnbuf; 842 ndp->ni_ptr = ndp->ni_pnbuf; 843 /* 844 * Extract and set starting directory. 845 */ 846 if (error = nfsrv_fhtovp(fhp, FALSE, &dp, ndp->ni_cred, slp, nam, &rdonly)) 847 goto out; 848 if (dp->v_type != VDIR) { 849 vrele(dp); 850 error = ENOTDIR; 851 goto out; 852 } 853 ndp->ni_startdir = dp; 854 if (rdonly) 855 ndp->ni_nameiop |= (NOCROSSMOUNT | RDONLY); 856 else 857 ndp->ni_nameiop |= NOCROSSMOUNT; 858 /* 859 * And call lookup() to do the real work 860 */ 861 if (error = lookup(ndp, p)) 862 goto out; 863 /* 864 * Check for encountering a symbolic link 865 */ 866 if (ndp->ni_more) { 867 if ((ndp->ni_nameiop & LOCKPARENT) && ndp->ni_pathlen == 1) 868 vput(ndp->ni_dvp); 869 else 870 vrele(ndp->ni_dvp); 871 vput(ndp->ni_vp); 872 ndp->ni_vp = NULL; 873 error = EINVAL; 874 goto out; 875 } 876 /* 877 * Check for saved name request 878 */ 879 if (ndp->ni_nameiop & (SAVENAME | SAVESTART)) { 880 ndp->ni_nameiop |= HASBUF; 881 return (0); 882 } 883 out: 884 FREE(ndp->ni_pnbuf, M_NAMEI); 885 return (error); 886 } 887 888 /* 889 * A fiddled version of m_adj() that ensures null fill to a long 890 * boundary and only trims off the back end 891 */ 892 void 893 nfsm_adj(mp, len, nul) 894 struct mbuf *mp; 895 register int len; 896 int nul; 897 { 898 register struct mbuf *m; 899 register int count, i; 900 register char *cp; 901 902 /* 903 * Trim from tail. Scan the mbuf chain, 904 * calculating its length and finding the last mbuf. 905 * If the adjustment only affects this mbuf, then just 906 * adjust and return. Otherwise, rescan and truncate 907 * after the remaining size. 908 */ 909 count = 0; 910 m = mp; 911 for (;;) { 912 count += m->m_len; 913 if (m->m_next == (struct mbuf *)0) 914 break; 915 m = m->m_next; 916 } 917 if (m->m_len > len) { 918 m->m_len -= len; 919 if (nul > 0) { 920 cp = mtod(m, caddr_t)+m->m_len-nul; 921 for (i = 0; i < nul; i++) 922 *cp++ = '\0'; 923 } 924 return; 925 } 926 count -= len; 927 if (count < 0) 928 count = 0; 929 /* 930 * Correct length for chain is "count". 931 * Find the mbuf with last data, adjust its length, 932 * and toss data from remaining mbufs on chain. 933 */ 934 for (m = mp; m; m = m->m_next) { 935 if (m->m_len >= count) { 936 m->m_len = count; 937 if (nul > 0) { 938 cp = mtod(m, caddr_t)+m->m_len-nul; 939 for (i = 0; i < nul; i++) 940 *cp++ = '\0'; 941 } 942 break; 943 } 944 count -= m->m_len; 945 } 946 while (m = m->m_next) 947 m->m_len = 0; 948 } 949 950 /* 951 * nfsrv_fhtovp() - convert a fh to a vnode ptr (optionally locked) 952 * - look up fsid in mount list (if not found ret error) 953 * - check that it is exported 954 * - get vp by calling VFS_FHTOVP() macro 955 * - if not lockflag unlock it with VOP_UNLOCK() 956 * - if cred->cr_uid == 0 or MNT_EXPORTANON set it to neth_anon 957 */ 958 nfsrv_fhtovp(fhp, lockflag, vpp, cred, slp, nam, rdonlyp) 959 fhandle_t *fhp; 960 int lockflag; 961 struct vnode **vpp; 962 struct ucred *cred; 963 struct nfssvc_sock *slp; 964 struct mbuf *nam; 965 int *rdonlyp; 966 { 967 register struct mount *mp; 968 register struct netaddrhash *np; 969 register struct ufsmount *ump; 970 register struct nfsuid *uidp; 971 struct sockaddr *saddr; 972 int error; 973 974 *vpp = (struct vnode *)0; 975 if ((mp = getvfs(&fhp->fh_fsid)) == NULL) 976 return (ESTALE); 977 if ((mp->mnt_flag & MNT_EXPORTED) == 0) 978 return (EACCES); 979 980 /* 981 * Get the export permission structure for this <mp, client> tuple. 982 */ 983 ump = VFSTOUFS(mp); 984 if (nam) { 985 986 /* 987 * First search for a network match. 988 */ 989 np = ump->um_netaddr[NETMASK_HASH]; 990 while (np) { 991 if (nfs_netaddr_match(np->neth_family, &np->neth_haddr, 992 &np->neth_hmask, nam)) 993 break; 994 np = np->neth_next; 995 } 996 997 /* 998 * If not found, try for an address match. 999 */ 1000 if (np == (struct netaddrhash *)0) { 1001 saddr = mtod(nam, struct sockaddr *); 1002 np = ump->um_netaddr[NETADDRHASH(saddr)]; 1003 while (np) { 1004 if (nfs_netaddr_match(np->neth_family, &np->neth_haddr, 1005 (struct netaddrhash *)0, nam)) 1006 break; 1007 np = np->neth_next; 1008 } 1009 } 1010 } else 1011 np = (struct netaddrhash *)0; 1012 if (np == (struct netaddrhash *)0) { 1013 1014 /* 1015 * If no address match, use the default if it exists. 1016 */ 1017 if ((mp->mnt_flag & MNT_DEFEXPORTED) == 0) 1018 return (EACCES); 1019 np = &ump->um_defexported; 1020 } 1021 1022 /* 1023 * Check/setup credentials. 1024 */ 1025 if (np->neth_exflags & MNT_EXKERB) { 1026 uidp = slp->ns_uidh[NUIDHASH(cred->cr_uid)]; 1027 while (uidp) { 1028 if (uidp->nu_uid == cred->cr_uid) 1029 break; 1030 uidp = uidp->nu_hnext; 1031 } 1032 if (uidp) { 1033 if (cred->cr_ref != 1) 1034 panic("nsrv fhtovp"); 1035 *cred = uidp->nu_cr; 1036 } else 1037 return (NQNFS_AUTHERR); 1038 } else if (cred->cr_uid == 0 || (np->neth_exflags & MNT_EXPORTANON)) 1039 *cred = np->neth_anon; 1040 if (error = VFS_FHTOVP(mp, &fhp->fh_fid, 0, vpp)) 1041 return (ESTALE); 1042 if (np->neth_exflags & MNT_EXRDONLY) 1043 *rdonlyp = 1; 1044 else 1045 *rdonlyp = 0; 1046 if (!lockflag) 1047 VOP_UNLOCK(*vpp); 1048 return (0); 1049 } 1050