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.67 (Berkeley) 11/01/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 <miscfs/specfs/specdev.h> 40 41 #include <netinet/in.h> 42 #ifdef ISO 43 #include <netiso/iso.h> 44 #endif 45 46 #define TRUE 1 47 #define FALSE 0 48 49 /* 50 * Data items converted to xdr at startup, since they are constant 51 * This is kinda hokey, but may save a little time doing byte swaps 52 */ 53 u_long nfs_procids[NFS_NPROCS]; 54 u_long nfs_xdrneg1; 55 u_long rpc_call, rpc_vers, rpc_reply, rpc_msgdenied, rpc_autherr, 56 rpc_mismatch, rpc_auth_unix, rpc_msgaccepted, rpc_rejectedcred, 57 rpc_auth_kerb; 58 u_long nfs_vers, nfs_prog, nfs_true, nfs_false; 59 60 /* And other global data */ 61 static u_long nfs_xid = 0; 62 enum vtype ntov_type[7] = { VNON, VREG, VDIR, VBLK, VCHR, VLNK, VNON }; 63 extern struct proc *nfs_iodwant[NFS_MAXASYNCDAEMON]; 64 extern struct queue_entry nfs_bufq; 65 extern struct nfsreq nfsreqh; 66 extern int nqnfs_piggy[NFS_NPROCS]; 67 extern struct nfsrtt nfsrtt; 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 queue_init(&nfs_bufq); 590 nfs_xdrneg1 = txdr_unsigned(-1); 591 nfs_nhinit(); /* Init the nfsnode table */ 592 nfsrv_init(0); /* Init server data structures */ 593 nfsrv_initcache(); /* Init the server request cache */ 594 595 /* 596 * Initialize the nqnfs server stuff. 597 */ 598 if (nqnfsstarttime == 0) { 599 nqnfsstarttime = boottime.tv_sec + nqsrv_maxlease 600 + nqsrv_clockskew + nqsrv_writeslack; 601 NQLOADNOVRAM(nqnfsstarttime); 602 nqnfs_prog = txdr_unsigned(NQNFS_PROG); 603 nqnfs_vers = txdr_unsigned(NQNFS_VER1); 604 nqthead.th_head[0] = &nqthead; 605 nqthead.th_head[1] = &nqthead; 606 nqfhead = hashinit(NQLCHSZ, M_NQLEASE, &nqfheadhash); 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 register struct nfsnode *np, *nq, **nhpp; 641 register long t1; 642 caddr_t dpos, cp2; 643 int error = 0, isnq; 644 struct mbuf *md; 645 enum vtype vtyp; 646 u_short vmode; 647 long rdev; 648 struct timespec mtime; 649 struct vnode *nvp; 650 651 md = *mdp; 652 dpos = *dposp; 653 t1 = (mtod(md, caddr_t) + md->m_len) - dpos; 654 isnq = (VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NQNFS); 655 if (error = nfsm_disct(&md, &dpos, NFSX_FATTR(isnq), 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 if (isnq) { 663 rdev = fxdr_unsigned(long, fp->fa_nqrdev); 664 fxdr_nqtime(&fp->fa_nqmtime, &mtime); 665 } else { 666 rdev = fxdr_unsigned(long, fp->fa_nfsrdev); 667 fxdr_nfstime(&fp->fa_nfsmtime, &mtime); 668 } 669 /* 670 * If v_type == VNON it is a new node, so fill in the v_type, 671 * n_mtime fields. Check to see if it represents a special 672 * device, and if so, check for a possible alias. Once the 673 * correct vnode has been obtained, fill in the rest of the 674 * information. 675 */ 676 np = VTONFS(vp); 677 if (vp->v_type == VNON) { 678 if (vtyp == VCHR && rdev == 0xffffffff) 679 vp->v_type = vtyp = VFIFO; 680 else 681 vp->v_type = vtyp; 682 if (vp->v_type == VFIFO) { 683 #ifdef FIFO 684 extern int (**fifo_nfsv2nodeop_p)(); 685 vp->v_op = fifo_nfsv2nodeop_p; 686 #else 687 return (EOPNOTSUPP); 688 #endif /* FIFO */ 689 } 690 if (vp->v_type == VCHR || vp->v_type == VBLK) { 691 vp->v_op = spec_nfsv2nodeop_p; 692 if (nvp = checkalias(vp, (dev_t)rdev, vp->v_mount)) { 693 /* 694 * Discard unneeded vnode, but save its nfsnode. 695 */ 696 if (nq = np->n_forw) 697 nq->n_back = np->n_back; 698 *np->n_back = nq; 699 nvp->v_data = vp->v_data; 700 vp->v_data = NULL; 701 vp->v_op = spec_vnodeop_p; 702 vrele(vp); 703 vgone(vp); 704 /* 705 * Reinitialize aliased node. 706 */ 707 np->n_vnode = nvp; 708 nhpp = (struct nfsnode **)nfs_hash(&np->n_fh); 709 if (nq = *nhpp) 710 nq->n_back = &np->n_forw; 711 np->n_forw = nq; 712 np->n_back = nhpp; 713 *nhpp = np; 714 *vpp = vp = nvp; 715 } 716 } 717 np->n_mtime = mtime.ts_sec; 718 } 719 vap = &np->n_vattr; 720 vap->va_type = vtyp; 721 vap->va_mode = (vmode & 07777); 722 vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink); 723 vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid); 724 vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid); 725 vap->va_rdev = (dev_t)rdev; 726 vap->va_mtime = mtime; 727 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0]; 728 if (isnq) { 729 fxdr_hyper(&fp->fa_nqsize, &vap->va_size); 730 vap->va_blocksize = fxdr_unsigned(long, fp->fa_nqblocksize); 731 fxdr_hyper(&fp->fa_nqbytes, &vap->va_bytes); 732 vap->va_fileid = fxdr_unsigned(long, fp->fa_nqfileid); 733 fxdr_nqtime(&fp->fa_nqatime, &vap->va_atime); 734 vap->va_flags = fxdr_unsigned(u_long, fp->fa_nqflags); 735 fxdr_nqtime(&fp->fa_nqctime, &vap->va_ctime); 736 vap->va_gen = fxdr_unsigned(u_long, fp->fa_nqgen); 737 fxdr_hyper(&fp->fa_nqfilerev, &vap->va_filerev); 738 } else { 739 vap->va_size = fxdr_unsigned(u_long, fp->fa_nfssize); 740 vap->va_blocksize = fxdr_unsigned(long, fp->fa_nfsblocksize); 741 vap->va_bytes = fxdr_unsigned(long, fp->fa_nfsblocks) * NFS_FABLKSIZE; 742 vap->va_fileid = fxdr_unsigned(long, fp->fa_nfsfileid); 743 vap->va_atime.ts_sec = fxdr_unsigned(long, fp->fa_nfsatime.nfs_sec); 744 vap->va_atime.ts_nsec = 0; 745 vap->va_flags = fxdr_unsigned(u_long, fp->fa_nfsatime.nfs_usec); 746 vap->va_ctime.ts_sec = fxdr_unsigned(long, fp->fa_nfsctime.nfs_sec); 747 vap->va_ctime.ts_nsec = 0; 748 vap->va_gen = fxdr_unsigned(u_long, fp->fa_nfsctime.nfs_usec); 749 vap->va_filerev = 0; 750 } 751 if (vap->va_size > np->n_size) { 752 np->n_size = vap->va_size; 753 vnode_pager_setsize(vp, (u_long)np->n_size); 754 } 755 np->n_attrstamp = time.tv_sec; 756 *dposp = dpos; 757 *mdp = md; 758 if (vaper != NULL) { 759 bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(*vap)); 760 if ((np->n_flag & NMODIFIED) && np->n_size > vap->va_size) 761 vaper->va_size = np->n_size; 762 if (np->n_flag & NCHG) { 763 if (np->n_flag & NACC) { 764 vaper->va_atime.ts_sec = np->n_atim.tv_sec; 765 vaper->va_atime.ts_nsec = 766 np->n_atim.tv_usec * 1000; 767 } 768 if (np->n_flag & NUPD) { 769 vaper->va_mtime.ts_sec = np->n_mtim.tv_sec; 770 vaper->va_mtime.ts_nsec = 771 np->n_mtim.tv_usec * 1000; 772 } 773 } 774 } 775 return (0); 776 } 777 778 /* 779 * Check the time stamp 780 * If the cache is valid, copy contents to *vap and return 0 781 * otherwise return an error 782 */ 783 nfs_getattrcache(vp, vap) 784 register struct vnode *vp; 785 struct vattr *vap; 786 { 787 register struct nfsnode *np; 788 789 np = VTONFS(vp); 790 if (VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NQLOOKLEASE) { 791 if (!NQNFS_CKCACHABLE(vp, NQL_READ) || np->n_attrstamp == 0) { 792 nfsstats.attrcache_misses++; 793 return (ENOENT); 794 } 795 } else if ((time.tv_sec - np->n_attrstamp) >= NFS_ATTRTIMEO(np)) { 796 nfsstats.attrcache_misses++; 797 return (ENOENT); 798 } 799 nfsstats.attrcache_hits++; 800 bcopy((caddr_t)&np->n_vattr,(caddr_t)vap,sizeof(struct vattr)); 801 if ((np->n_flag & NMODIFIED) == 0) { 802 np->n_size = vap->va_size; 803 vnode_pager_setsize(vp, (u_long)np->n_size); 804 } else if (np->n_size > vap->va_size) 805 vap->va_size = np->n_size; 806 if (np->n_flag & NCHG) { 807 if (np->n_flag & NACC) { 808 vap->va_atime.ts_sec = np->n_atim.tv_sec; 809 vap->va_atime.ts_nsec = np->n_atim.tv_usec * 1000; 810 } 811 if (np->n_flag & NUPD) { 812 vap->va_mtime.ts_sec = np->n_mtim.tv_sec; 813 vap->va_mtime.ts_nsec = np->n_mtim.tv_usec * 1000; 814 } 815 } 816 return (0); 817 } 818 819 /* 820 * Set up nameidata for a lookup() call and do it 821 */ 822 nfs_namei(ndp, fhp, len, slp, nam, mdp, dposp, p) 823 register struct nameidata *ndp; 824 fhandle_t *fhp; 825 int len; 826 struct nfssvc_sock *slp; 827 struct mbuf *nam; 828 struct mbuf **mdp; 829 caddr_t *dposp; 830 struct proc *p; 831 { 832 register int i, rem; 833 register struct mbuf *md; 834 register char *fromcp, *tocp; 835 struct vnode *dp; 836 int error, rdonly; 837 struct componentname *cnp = &ndp->ni_cnd; 838 839 MALLOC(cnp->cn_pnbuf, char *, len + 1, M_NAMEI, M_WAITOK); 840 /* 841 * Copy the name from the mbuf list to ndp->ni_pnbuf 842 * and set the various ndp fields appropriately. 843 */ 844 fromcp = *dposp; 845 tocp = cnp->cn_pnbuf; 846 md = *mdp; 847 rem = mtod(md, caddr_t) + md->m_len - fromcp; 848 cnp->cn_hash = 0; 849 for (i = 0; i < len; i++) { 850 while (rem == 0) { 851 md = md->m_next; 852 if (md == NULL) { 853 error = EBADRPC; 854 goto out; 855 } 856 fromcp = mtod(md, caddr_t); 857 rem = md->m_len; 858 } 859 if (*fromcp == '\0' || *fromcp == '/') { 860 error = EINVAL; 861 goto out; 862 } 863 if (*fromcp & 0200) 864 if ((*fromcp&0377) == ('/'|0200) || cnp->cn_nameiop != DELETE) { 865 error = EINVAL; 866 goto out; 867 } 868 cnp->cn_hash += (unsigned char)*fromcp; 869 *tocp++ = *fromcp++; 870 rem--; 871 } 872 *tocp = '\0'; 873 *mdp = md; 874 *dposp = fromcp; 875 len = nfsm_rndup(len)-len; 876 if (len > 0) { 877 if (rem >= len) 878 *dposp += len; 879 else if (error = nfs_adv(mdp, dposp, len, rem)) 880 goto out; 881 } 882 ndp->ni_pathlen = tocp - cnp->cn_pnbuf; 883 cnp->cn_nameptr = cnp->cn_pnbuf; 884 /* 885 * Extract and set starting directory. 886 */ 887 if (error = nfsrv_fhtovp(fhp, FALSE, &dp, ndp->ni_cnd.cn_cred, slp, 888 nam, &rdonly)) 889 goto out; 890 if (dp->v_type != VDIR) { 891 vrele(dp); 892 error = ENOTDIR; 893 goto out; 894 } 895 ndp->ni_startdir = dp; 896 if (rdonly) 897 cnp->cn_flags |= (NOCROSSMOUNT | RDONLY); 898 else 899 cnp->cn_flags |= NOCROSSMOUNT; 900 /* 901 * And call lookup() to do the real work 902 */ 903 cnp->cn_proc = p; 904 if (error = lookup(ndp)) 905 goto out; 906 /* 907 * Check for encountering a symbolic link 908 */ 909 if (cnp->cn_flags & ISSYMLINK) { 910 if ((cnp->cn_flags & LOCKPARENT) && ndp->ni_pathlen == 1) 911 vput(ndp->ni_dvp); 912 else 913 vrele(ndp->ni_dvp); 914 vput(ndp->ni_vp); 915 ndp->ni_vp = NULL; 916 error = EINVAL; 917 goto out; 918 } 919 /* 920 * Check for saved name request 921 */ 922 if (cnp->cn_flags & (SAVENAME | SAVESTART)) { 923 cnp->cn_flags |= HASBUF; 924 return (0); 925 } 926 out: 927 FREE(cnp->cn_pnbuf, M_NAMEI); 928 return (error); 929 } 930 931 /* 932 * A fiddled version of m_adj() that ensures null fill to a long 933 * boundary and only trims off the back end 934 */ 935 void 936 nfsm_adj(mp, len, nul) 937 struct mbuf *mp; 938 register int len; 939 int nul; 940 { 941 register struct mbuf *m; 942 register int count, i; 943 register char *cp; 944 945 /* 946 * Trim from tail. Scan the mbuf chain, 947 * calculating its length and finding the last mbuf. 948 * If the adjustment only affects this mbuf, then just 949 * adjust and return. Otherwise, rescan and truncate 950 * after the remaining size. 951 */ 952 count = 0; 953 m = mp; 954 for (;;) { 955 count += m->m_len; 956 if (m->m_next == (struct mbuf *)0) 957 break; 958 m = m->m_next; 959 } 960 if (m->m_len > len) { 961 m->m_len -= len; 962 if (nul > 0) { 963 cp = mtod(m, caddr_t)+m->m_len-nul; 964 for (i = 0; i < nul; i++) 965 *cp++ = '\0'; 966 } 967 return; 968 } 969 count -= len; 970 if (count < 0) 971 count = 0; 972 /* 973 * Correct length for chain is "count". 974 * Find the mbuf with last data, adjust its length, 975 * and toss data from remaining mbufs on chain. 976 */ 977 for (m = mp; m; m = m->m_next) { 978 if (m->m_len >= count) { 979 m->m_len = count; 980 if (nul > 0) { 981 cp = mtod(m, caddr_t)+m->m_len-nul; 982 for (i = 0; i < nul; i++) 983 *cp++ = '\0'; 984 } 985 break; 986 } 987 count -= m->m_len; 988 } 989 while (m = m->m_next) 990 m->m_len = 0; 991 } 992 993 /* 994 * nfsrv_fhtovp() - convert a fh to a vnode ptr (optionally locked) 995 * - look up fsid in mount list (if not found ret error) 996 * - get vp and export rights by calling VFS_FHTOVP() 997 * - if cred->cr_uid == 0 or MNT_EXPORTANON set it to credanon 998 * - if not lockflag unlock it with VOP_UNLOCK() 999 */ 1000 nfsrv_fhtovp(fhp, lockflag, vpp, cred, slp, nam, rdonlyp) 1001 fhandle_t *fhp; 1002 int lockflag; 1003 struct vnode **vpp; 1004 struct ucred *cred; 1005 struct nfssvc_sock *slp; 1006 struct mbuf *nam; 1007 int *rdonlyp; 1008 { 1009 register struct mount *mp; 1010 register struct nfsuid *uidp; 1011 struct ucred *credanon; 1012 int error, exflags; 1013 1014 *vpp = (struct vnode *)0; 1015 if ((mp = getvfs(&fhp->fh_fsid)) == NULL) 1016 return (ESTALE); 1017 if (error = VFS_FHTOVP(mp, &fhp->fh_fid, nam, vpp, &exflags, &credanon)) 1018 return (error); 1019 /* 1020 * Check/setup credentials. 1021 */ 1022 if (exflags & MNT_EXKERB) { 1023 uidp = slp->ns_uidh[NUIDHASH(cred->cr_uid)]; 1024 while (uidp) { 1025 if (uidp->nu_uid == cred->cr_uid) 1026 break; 1027 uidp = uidp->nu_hnext; 1028 } 1029 if (uidp) { 1030 if (cred->cr_ref != 1) 1031 panic("nsrv fhtovp"); 1032 *cred = uidp->nu_cr; 1033 } else 1034 return (NQNFS_AUTHERR); 1035 } else if (cred->cr_uid == 0 || (exflags & MNT_EXPORTANON)) 1036 *cred = *credanon; 1037 if (exflags & MNT_EXRDONLY) 1038 *rdonlyp = 1; 1039 else 1040 *rdonlyp = 0; 1041 if (!lockflag) 1042 VOP_UNLOCK(*vpp); 1043 return (0); 1044 } 1045 1046 /* 1047 * This function compares two net addresses by family and returns TRUE 1048 * if they are the same host. 1049 * If there is any doubt, return FALSE. 1050 * The AF_INET family is handled as a special case so that address mbufs 1051 * don't need to be saved to store "struct in_addr", which is only 4 bytes. 1052 */ 1053 netaddr_match(family, haddr, nam) 1054 int family; 1055 union nethostaddr *haddr; 1056 struct mbuf *nam; 1057 { 1058 register struct sockaddr_in *inetaddr; 1059 1060 switch (family) { 1061 case AF_INET: 1062 inetaddr = mtod(nam, struct sockaddr_in *); 1063 if (inetaddr->sin_family == AF_INET && 1064 inetaddr->sin_addr.s_addr == haddr->had_inetaddr) 1065 return (1); 1066 break; 1067 #ifdef ISO 1068 case AF_ISO: 1069 { 1070 register struct sockaddr_iso *isoaddr1, *isoaddr2; 1071 1072 isoaddr1 = mtod(nam, struct sockaddr_iso *); 1073 isoaddr2 = mtod(haddr->had_nam, struct sockaddr_iso *); 1074 if (isoaddr1->siso_family == AF_ISO && 1075 isoaddr1->siso_nlen > 0 && 1076 isoaddr1->siso_nlen == isoaddr2->siso_nlen && 1077 SAME_ISOADDR(isoaddr1, isoaddr2)) 1078 return (1); 1079 break; 1080 } 1081 #endif /* ISO */ 1082 default: 1083 break; 1084 }; 1085 return (0); 1086 } 1087